Client can no longer interact with unloaded inventory lists

* Rename InventoryList to ServerInventoryList
master
Nicole Collings 2020-02-28 18:44:01 -08:00
parent 37f20b9978
commit 7c0e1cefd7
18 changed files with 79 additions and 79 deletions

View File

@ -213,8 +213,8 @@ set(ZEPHA_SRC
game/hud/components/compound/GuiInventoryList.h
game/hud/components/basic/GuiInventoryItem.cpp
game/hud/components/basic/GuiInventoryItem.h
game/inventory/InventoryList.cpp
game/inventory/InventoryList.h
game/inventory/ServerInventoryList.cpp
game/inventory/ServerInventoryList.h
game/inventory/ItemStack.h
server/LocalServerInstance.cpp
server/LocalServerInstance.h

View File

@ -15,7 +15,7 @@
#include "components/basic/GuiGraph.h"
#include "components/basic/GuiText.h"
#include "components/basic/GuiContainer.h"
#include "../inventory/InventoryList.h"
#include "../inventory/ServerInventoryList.h"
class DebugGui : public GuiContainer {
public:

View File

@ -10,7 +10,7 @@
#include "components/basic/GuiRect.h"
#include "components/basic/GuiContainer.h"
#include "../graph/drawable/DrawableGroup.h"
#include "../inventory/InventoryList.h"
#include "../inventory/ServerInventoryList.h"
#include "../entity/Entity.h"
#include "../../util/Util.h"
#include "components/compound/GuiInventoryList.h"

View File

@ -40,10 +40,10 @@ void GuiInventoryList::create(glm::vec2 scale, glm::vec4 padding, glm::ivec2 inn
this->scale = scale;
this->padding = padding;
this->innerPadding = innerPadding;
this->hitbox = glm::ivec2 {
this->hitbox = (list->getWidth() == 0 ? glm::ivec2 {} : glm::ivec2 {
padding.x + list->getWidth() * (innerPadding.x*scale.x),
padding.y + (list->getLength() / list->getWidth()) * (innerPadding.y*scale.y)
};
});
drawContents();
list->setGuiCallback(std::bind(&GuiInventoryList::drawContents, this));
@ -94,7 +94,7 @@ void GuiInventoryList::hoverEvent(bool hovered, glm::ivec2 pos) {
}
void GuiInventoryList::leftClick(bool down, glm::ivec2 pos) {
if (!down) return;
if (!down || list->getWidth() == 0) return;
pos += glm::ivec2(glm::vec2(this->padding.x, this->padding.y) * this->scale);
@ -110,6 +110,8 @@ void GuiInventoryList::leftClick(bool down, glm::ivec2 pos) {
}
void GuiInventoryList::rightClick(bool down, glm::ivec2 pos) {
if (!down || list->getWidth() == 0) return;
pos += glm::ivec2(glm::vec2(this->padding.x, this->padding.y) * this->scale);
glm::ivec2 slot = pos / (glm::ivec2(this->scale) * this->innerPadding);
@ -119,32 +121,32 @@ void GuiInventoryList::rightClick(bool down, glm::ivec2 pos) {
unsigned short index = slot.x + slot.y * list->getWidth();
if (index >= list->getLength()) return;
if (down) {
auto handStack = hand->getStack(0);
if (handStack.count == 0) {
hand->setStack(0, list->splitStack(index, true));
}
else {
auto handStack = hand->getStack(0);
if (handStack.count == 0) {
hand->setStack(0, list->splitStack(index, true));
auto listStack = list->getStack(index);
if (listStack.id == 0 || listStack.id == handStack.id) {
auto overflow = list->placeStack(index, {handStack.id, 1}, true);
handStack.count -= 1;
if (handStack.count == 0) handStack.id = 0;
if (overflow.count != 0) handStack.count += overflow.count;
hand->setStack(0, handStack);
}
else {
auto handStack = hand->getStack(0);
auto listStack = list->getStack(index);
if (listStack.id == 0 || listStack.id == handStack.id) {
auto overflow = list->placeStack(index, {handStack.id, 1}, true);
handStack.count -= 1;
if (handStack.count == 0) handStack.id = 0;
if (overflow.count != 0) handStack.count += overflow.count;
hand->setStack(0, handStack);
}
else {
hand->setStack(0, list->placeStack(index, hand->getStack(0), true));
}
hand->setStack(0, list->placeStack(index, hand->getStack(0), true));
}
}
}
void GuiInventoryList::drawContents() {
if (list->getWidth() == 0) return;
this->hitbox = glm::ivec2 {
padding.x + list->getWidth() * (innerPadding.x*scale.x),
padding.y + (list->getLength() / list->getWidth()) * (innerPadding.y*scale.y)
padding.x + list->getWidth() * (innerPadding.x*scale.x),
padding.y + (list->getLength() / list->getWidth()) * (innerPadding.y*scale.y)
};
empty();

View File

@ -14,10 +14,10 @@ void Inventory::sendDirtyLists() {
}
void Inventory::createList(std::string name, unsigned short length, unsigned short width) {
lists.emplace(name, std::make_shared<InventoryList>(defs, clients, this->name, name, length, width));
lists.emplace(name, std::make_shared<ServerInventoryList>(defs, clients, this->name, name, length, width));
}
std::shared_ptr<InventoryList> Inventory::operator[](std::string name) {
std::shared_ptr<ServerInventoryList> Inventory::operator[](std::string name) {
if (lists.count(name)) return lists[name];
else return nullptr;
}

View File

@ -7,7 +7,7 @@
#include <map>
#include <string>
#include <memory>
#include "InventoryList.h"
#include "ServerInventoryList.h"
class ClientList;
@ -18,12 +18,12 @@ public:
void sendDirtyLists();
void createList(std::string name, unsigned short length, unsigned short width);
std::shared_ptr<InventoryList> operator[](std::string name);
std::shared_ptr<ServerInventoryList> operator[](std::string name);
void removeList(std::string name);
DefinitionAtlas& defs;
std::string name;
private:
ClientList* clients;
std::map<std::string, std::shared_ptr<InventoryList>> lists;
std::map<std::string, std::shared_ptr<ServerInventoryList>> lists;
};

View File

@ -4,7 +4,7 @@
#include "InventoryRefs.h"
#include "InventoryList.h"
#include "ServerInventoryList.h"
InventoryRefs::InventoryRefs(ServerDefinitionAtlas &defs, ClientList* clients) :
defs(defs), clients(clients) {}
@ -24,7 +24,7 @@ std::shared_ptr<Inventory> InventoryRefs::getInv(const std::string &inv) {
return inventories[inv];
}
std::shared_ptr<InventoryList> InventoryRefs::getList(const std::string &inv, const std::string &list) {
std::shared_ptr<ServerInventoryList> InventoryRefs::getList(const std::string &inv, const std::string &list) {
if (!inventories.count(inv)) return nullptr;
return inventories[inv]->operator[](list);
}

View File

@ -7,7 +7,7 @@
#include "../../def/ServerDefinitionAtlas.h"
#include "Inventory.h"
class InventoryList;
class ServerInventoryList;
class InventoryRefs {
public:
@ -17,7 +17,7 @@ public:
std::shared_ptr<Inventory> createInv(const std::string& inv);
std::shared_ptr<Inventory> getInv(const std::string& inv);
std::shared_ptr<InventoryList> getList(const std::string& inv, const std::string& list);
std::shared_ptr<ServerInventoryList> getList(const std::string& inv, const std::string& list);
bool addWatcher(const std::string& inv, const std::string& list, unsigned int cid);
bool removeWatcher(const std::string& inv, const std::string& list, unsigned int cid);

View File

@ -46,8 +46,6 @@ public:
DefinitionAtlas& defs;
private:
bool initialized = false;
void triggerCallback();
std::vector<ItemStack> itemstacks {};

View File

@ -33,7 +33,7 @@ std::shared_ptr<LocalInventory> LocalInventoryRefs::getInv(const std::string& in
std::shared_ptr<LocalInventoryList> LocalInventoryRefs::getList(const std::string& inv, const std::string& list) {
if (!inventories.count(inv)) inventories.insert({inv, {}});
if (inventories[inv]->operator[](list) == nullptr) {
inventories[inv]->createList(list, 1, 1);
inventories[inv]->createList(list, 0, 0);
watchFn(inv, list);
}
return inventories[inv]->operator[](list);

View File

@ -5,14 +5,14 @@
#include <iostream>
#include <algorithm>
#include "InventoryList.h"
#include "ServerInventoryList.h"
#include "../../util/net/Packet.h"
#include "../../util/net/Serializer.h"
#include "../../server/conn/ClientList.h"
#include "../../lua/api/class/LuaItemStack.h"
InventoryList::InventoryList(DefinitionAtlas& defs, ClientList* list, const std::string& invName, const std::string& name, unsigned short size, unsigned short width) :
ServerInventoryList::ServerInventoryList(DefinitionAtlas& defs, ClientList* list, const std::string& invName, const std::string& name, unsigned short size, unsigned short width) :
defs(defs),
name(name),
width(width),
@ -20,26 +20,26 @@ InventoryList::InventoryList(DefinitionAtlas& defs, ClientList* list, const std:
invName(invName),
itemstacks(size) {}
void InventoryList::setLuaCallback(InventoryList::Callback type, sol::function cb) {
void ServerInventoryList::setLuaCallback(ServerInventoryList::Callback type, sol::function cb) {
luaCallbacks[static_cast<size_t>(type)] = cb;
}
sol::function InventoryList::getLuaCallback(InventoryList::Callback type) {
sol::function ServerInventoryList::getLuaCallback(ServerInventoryList::Callback type) {
return luaCallbacks[static_cast<size_t>(type)];
}
ItemStack InventoryList::getStack(unsigned short i) {
ItemStack ServerInventoryList::getStack(unsigned short i) {
return itemstacks[i];
}
void InventoryList::setStack(unsigned short i, const ItemStack &stack) {
void ServerInventoryList::setStack(unsigned short i, const ItemStack &stack) {
if (stack != getStack(i)) {
itemstacks[i] = stack;
setDirty();
}
}
ItemStack InventoryList::placeStack(unsigned short i, const ItemStack &stack, bool playerInitiated) {
ItemStack ServerInventoryList::placeStack(unsigned short i, const ItemStack &stack, bool playerInitiated) {
auto otherStack = getStack(i);
unsigned short allowedTake = otherStack.count;
@ -104,7 +104,7 @@ ItemStack InventoryList::placeStack(unsigned short i, const ItemStack &stack, bo
}
}
ItemStack InventoryList::splitStack(unsigned short i, bool playerInitiated) {
ItemStack ServerInventoryList::splitStack(unsigned short i, bool playerInitiated) {
auto stack = getStack(i);
unsigned short allowedTake = stack.count;
@ -122,7 +122,7 @@ ItemStack InventoryList::splitStack(unsigned short i, bool playerInitiated) {
return {stack.id, takeCount};
}
ItemStack InventoryList::addStack(ItemStack stack, bool playerInitiated) {
ItemStack ServerInventoryList::addStack(ItemStack stack, bool playerInitiated) {
unsigned short maxStack = defs.fromId(stack.id).maxStackSize;
unsigned short i = 0;
@ -158,7 +158,7 @@ ItemStack InventoryList::addStack(ItemStack stack, bool playerInitiated) {
return stack;
}
unsigned short InventoryList::stackFits(const ItemStack &stack) {
unsigned short ServerInventoryList::stackFits(const ItemStack &stack) {
unsigned short maxStack = defs.fromId(stack.id).maxStackSize;
unsigned short i = 0;
@ -183,7 +183,7 @@ unsigned short InventoryList::stackFits(const ItemStack &stack) {
return fits;
}
ItemStack InventoryList::takeStack(ItemStack request, bool playerInitiated) {
ItemStack ServerInventoryList::takeStack(ItemStack request, bool playerInitiated) {
unsigned short i = 0;
unsigned short to_remove = request.count;
@ -208,7 +208,7 @@ ItemStack InventoryList::takeStack(ItemStack request, bool playerInitiated) {
return request;
}
ItemStack InventoryList::removeStack(unsigned short ind, unsigned short count) {
ItemStack ServerInventoryList::removeStack(unsigned short ind, unsigned short count) {
auto stack = getStack(ind);
if (count >= stack.count) {
setStack(ind, {0, 0});
@ -222,23 +222,23 @@ ItemStack InventoryList::removeStack(unsigned short ind, unsigned short count) {
}
}
void InventoryList::setDirty() {
void ServerInventoryList::setDirty() {
dirty = true;
}
unsigned short InventoryList::getLength() {
unsigned short ServerInventoryList::getLength() {
return itemstacks.size();
}
unsigned short InventoryList::getWidth() {
unsigned short ServerInventoryList::getWidth() {
return width;
}
std::string InventoryList::getName() {
std::string ServerInventoryList::getName() {
return name;
}
bool InventoryList::addWatcher(unsigned int cid) {
bool ServerInventoryList::addWatcher(unsigned int cid) {
auto& client = clients->getClient(cid);
if (!client) return false;
@ -248,7 +248,7 @@ bool InventoryList::addWatcher(unsigned int cid) {
sendTo(client);
}
bool InventoryList::removeWatcher(unsigned int cid) {
bool ServerInventoryList::removeWatcher(unsigned int cid) {
for (auto it = watchers.cbegin(); it != watchers.cend();) {
if (*it == cid) {
watchers.erase(it);
@ -259,7 +259,7 @@ bool InventoryList::removeWatcher(unsigned int cid) {
return false;
}
Packet InventoryList::createPacket() {
Packet ServerInventoryList::createPacket() {
Serializer s{};
s.append<std::string>(invName)
.append<std::string>(name)
@ -274,13 +274,13 @@ Packet InventoryList::createPacket() {
return s.packet(PacketType::INVENTORY, false);
}
void InventoryList::sendTo(std::shared_ptr<ServerClient> client) {
void ServerInventoryList::sendTo(std::shared_ptr<ServerClient> client) {
if (!client) return;
auto p = createPacket();
p.sendTo(client->peer, PacketChannel::INVENTORY);
}
void InventoryList::sendAll() {
void ServerInventoryList::sendAll() {
auto p = createPacket();
for (auto it = watchers.cbegin(); it != watchers.cend();) {

View File

@ -14,12 +14,12 @@ class ClientList;
class ServerClient;
class Packet;
class InventoryList {
class ServerInventoryList {
public:
enum class Callback { ALLOW_TAKE, ALLOW_PUT, ON_TAKE, ON_PUT };
InventoryList(DefinitionAtlas& defs, ClientList* list, const std::string& invName,
const std::string& name, unsigned short size, unsigned short width);
ServerInventoryList(DefinitionAtlas& defs, ClientList* list, const std::string& invName,
const std::string& name, unsigned short size, unsigned short width);
unsigned short getLength();
unsigned short getWidth();

View File

@ -11,10 +11,10 @@
namespace {
#ifdef _WIN32
const static char* EXECUTABLE_NAME = "zepha-venus-win.exe";
#elif __APPLE__
const static char* EXECUTABLE_NAME = "zepha-venus-macos";
#else
const static char* EXECUTABLE_NAME = "zepha-venus-linux";
#define _popen popen
#define _pclose pclose
#endif
}

View File

@ -68,10 +68,10 @@ LuaItemStack ServerLuaInventoryList::remove_stack(unsigned short ind, unsigned s
return LuaItemStack(list.removeStack(ind - 1, count), list.defs);
}
void ServerLuaInventoryList::set_callback(InventoryList::Callback t, sol::function fun) {
void ServerLuaInventoryList::set_callback(ServerInventoryList::Callback t, sol::function fun) {
list.setLuaCallback(t, fun);
}
sol::function ServerLuaInventoryList::get_callback(InventoryList::Callback t) {
sol::function ServerLuaInventoryList::get_callback(ServerInventoryList::Callback t) {
return list.getLuaCallback(t);
}

View File

@ -4,15 +4,15 @@
#pragma once
#include "../../../game/inventory/InventoryList.h"
#include "../../../game/inventory/ServerInventoryList.h"
#include "LuaItemStack.h"
class ServerLuaInventoryList {
public:
ServerLuaInventoryList(InventoryList& list) :
ServerLuaInventoryList(ServerInventoryList& list) :
list(list) {}
InventoryList& list;
ServerInventoryList& list;
int get_length();
int get_width();
@ -39,6 +39,6 @@ public:
LuaItemStack remove_stack(unsigned short ind, unsigned short count);
void set_callback(InventoryList::Callback t, sol::function fun);
sol::function get_callback(InventoryList::Callback t);
void set_callback(ServerInventoryList::Callback t, sol::function fun);
sol::function get_callback(ServerInventoryList::Callback t);
};

View File

@ -40,17 +40,17 @@ namespace ServerApi {
"remove_stack", &ServerLuaInventoryList::remove_stack,
"on_put", sol::property(
[](ServerLuaInventoryList l){ return l.get_callback(InventoryList::Callback::ON_PUT); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ON_PUT, f); }),
[](ServerLuaInventoryList l){ return l.get_callback(ServerInventoryList::Callback::ON_PUT); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(ServerInventoryList::Callback::ON_PUT, f); }),
"on_take", sol::property(
[](ServerLuaInventoryList l){ return l.get_callback(InventoryList::Callback::ON_TAKE); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ON_TAKE, f); }),
[](ServerLuaInventoryList l){ return l.get_callback(ServerInventoryList::Callback::ON_TAKE); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(ServerInventoryList::Callback::ON_TAKE, f); }),
"allow_put", sol::property(
[](ServerLuaInventoryList l){ return l.get_callback(InventoryList::Callback::ALLOW_PUT); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ALLOW_PUT, f); }),
[](ServerLuaInventoryList l){ return l.get_callback(ServerInventoryList::Callback::ALLOW_PUT); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(ServerInventoryList::Callback::ALLOW_PUT, f); }),
"allow_take", sol::property(
[](ServerLuaInventoryList l){ return l.get_callback(InventoryList::Callback::ALLOW_TAKE); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(InventoryList::Callback::ALLOW_TAKE, f); })
[](ServerLuaInventoryList l){ return l.get_callback(ServerInventoryList::Callback::ALLOW_TAKE); },
[](ServerLuaInventoryList l, sol::function f){ l.set_callback(ServerInventoryList::Callback::ALLOW_TAKE, f); })
);
}
}

View File

@ -9,7 +9,7 @@
#include <unordered_map>
#include "../../util/Vec.h"
#include "../../game/inventory/InventoryList.h"
#include "../../game/inventory/ServerInventoryList.h"
#include "../../game/inventory/Inventory.h"
class InventoryRefs;

View File

@ -5,6 +5,6 @@ if (zepha.server) {
local yields = get_yield(pos)
if (yields == nil) { return }
player.get_inventory():get_list("main"):add_stack({yields, 1})
player:get_inventory():get_list("main"):add_stack({yields, 1})
})
}