3 Copyright (C) 2010-2013 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 bool operator==(const InventoryLocation &other) const
71 if(type != other.type)
79 return (name == other.name);
81 return (p == other.p);
83 return (name == other.name);
87 bool operator!=(const InventoryLocation &other) const
89 return !(*this == other);
92 void applyCurrentPlayer(const std::string &name_)
94 if(type == CURRENT_PLAYER)
98 std::string dump() const;
99 void serialize(std::ostream &os) const;
100 void deSerialize(std::istream &is);
101 void deSerialize(std::string s);
104 struct InventoryAction;
106 class InventoryManager
110 virtual ~InventoryManager(){}
112 // Get an inventory (server and client)
113 virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
114 // Set modified (will be saved and sent over network; only on server)
115 virtual void setInventoryModified(const InventoryLocation &loc){}
116 // Send inventory action to server (only on client)
117 virtual void inventoryAction(InventoryAction *a){}
120 #define IACTION_MOVE 0
121 #define IACTION_DROP 1
122 #define IACTION_CRAFT 2
124 struct InventoryAction
126 static InventoryAction * deSerialize(std::istream &is);
128 virtual u16 getType() const = 0;
129 virtual void serialize(std::ostream &os) const = 0;
130 virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
131 IGameDef *gamedef) = 0;
132 virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef) = 0;
133 virtual ~InventoryAction() {};
136 struct IMoveAction : public InventoryAction
138 // count=0 means "everything"
140 InventoryLocation from_inv;
141 std::string from_list;
143 InventoryLocation to_inv;
154 IMoveAction(std::istream &is);
161 void serialize(std::ostream &os) const
165 os<<from_inv.dump()<<" ";
168 os<<to_inv.dump()<<" ";
173 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
175 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
178 struct IDropAction : public InventoryAction
180 // count=0 means "everything"
182 InventoryLocation from_inv;
183 std::string from_list;
192 IDropAction(std::istream &is);
199 void serialize(std::ostream &os) const
203 os<<from_inv.dump()<<" ";
208 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
210 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
213 struct ICraftAction : public InventoryAction
215 // count=0 means "everything"
217 InventoryLocation craft_inv;
224 ICraftAction(std::istream &is);
228 return IACTION_CRAFT;
231 void serialize(std::ostream &os) const
235 os<<craft_inv.dump()<<" ";
238 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
240 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
244 bool getCraftingResult(Inventory *inv, ItemStack& result,
245 bool decrementInput, IGameDef *gamedef);