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, bool playerSend = true){}
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;
148 // treat these as private
149 // related to movement to somewhere
150 bool caused_by_move_somewhere;
158 move_somewhere = false;
159 caused_by_move_somewhere = false;
163 IMoveAction(std::istream &is, bool somewhere);
170 void serialize(std::ostream &os) const
175 os << "MoveSomewhere ";
177 os << from_inv.dump() << " ";
178 os << from_list << " ";
180 os << to_inv.dump() << " ";
186 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
188 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
191 struct IDropAction : public InventoryAction
193 // count=0 means "everything"
195 InventoryLocation from_inv;
196 std::string from_list;
205 IDropAction(std::istream &is);
212 void serialize(std::ostream &os) const
216 os<<from_inv.dump()<<" ";
221 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
223 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
226 struct ICraftAction : public InventoryAction
228 // count=0 means "everything"
230 InventoryLocation craft_inv;
237 ICraftAction(std::istream &is);
241 return IACTION_CRAFT;
244 void serialize(std::ostream &os) const
248 os<<craft_inv.dump()<<" ";
251 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
253 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
257 bool getCraftingResult(Inventory *inv, ItemStack& result,
258 std::vector<ItemStack> &output_replacements,
259 bool decrementInput, IGameDef *gamedef);