3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef INVENTORYMANAGER_HEADER
21 #define INVENTORYMANAGER_HEADER
23 #include "inventory.h"
26 class ServerActiveObject;
28 struct InventoryLocation
38 std::string name; // PLAYER, DETACHED
49 void setCurrentPlayer()
51 type = CURRENT_PLAYER;
53 void setPlayer(const std::string &name_)
58 void setNodeMeta(v3s16 p_)
63 void setDetached(const std::string &name_)
69 void applyCurrentPlayer(const std::string &name_)
71 if(type == CURRENT_PLAYER)
75 std::string dump() const;
76 void serialize(std::ostream &os) const;
77 void deSerialize(std::istream &is);
78 void deSerialize(std::string s);
81 struct InventoryAction;
83 class InventoryManager
87 virtual ~InventoryManager(){}
89 // Get an inventory (server and client)
90 virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
91 // Set modified (will be saved and sent over network; only on server)
92 virtual void setInventoryModified(const InventoryLocation &loc){}
93 // Send inventory action to server (only on client)
94 virtual void inventoryAction(InventoryAction *a){}
97 #define IACTION_MOVE 0
98 #define IACTION_DROP 1
99 #define IACTION_CRAFT 2
101 struct InventoryAction
103 static InventoryAction * deSerialize(std::istream &is);
105 virtual u16 getType() const = 0;
106 virtual void serialize(std::ostream &os) const = 0;
107 virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
108 IGameDef *gamedef) = 0;
109 virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef) = 0;
110 virtual ~InventoryAction() {};
113 struct IMoveAction : public InventoryAction
115 // count=0 means "everything"
117 InventoryLocation from_inv;
118 std::string from_list;
120 InventoryLocation to_inv;
131 IMoveAction(std::istream &is);
138 void serialize(std::ostream &os) const
142 os<<from_inv.dump()<<" ";
145 os<<to_inv.dump()<<" ";
150 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
152 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
155 struct IDropAction : public InventoryAction
157 // count=0 means "everything"
159 InventoryLocation from_inv;
160 std::string from_list;
169 IDropAction(std::istream &is);
176 void serialize(std::ostream &os) const
180 os<<from_inv.dump()<<" ";
185 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
187 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
190 struct ICraftAction : public InventoryAction
192 // count=0 means "everything"
194 InventoryLocation craft_inv;
201 ICraftAction(std::istream &is);
205 return IACTION_CRAFT;
208 void serialize(std::ostream &os) const
212 os<<craft_inv.dump()<<" ";
215 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
217 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
221 bool getCraftingResult(Inventory *inv, ItemStack& result,
222 bool decrementInput, IGameDef *gamedef);