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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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
37 std::string name; // PLAYER
48 void setCurrentPlayer()
50 type = CURRENT_PLAYER;
52 void setPlayer(const std::string &name_)
57 void setNodeMeta(v3s16 p_)
63 void applyCurrentPlayer(const std::string &name_)
65 if(type == CURRENT_PLAYER)
69 std::string dump() const;
70 void serialize(std::ostream &os) const;
71 void deSerialize(std::istream &is);
72 void deSerialize(std::string s);
75 struct InventoryAction;
77 class InventoryManager
81 virtual ~InventoryManager(){}
83 // Get an inventory or set it modified (so it will be updated over
85 virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
86 virtual std::string getInventoryOwner(const InventoryLocation &loc){return "";}
87 virtual void setInventoryModified(const InventoryLocation &loc){}
89 // Used on the client to send an action to the server
90 virtual void inventoryAction(InventoryAction *a){}
93 #define IACTION_MOVE 0
94 #define IACTION_DROP 1
95 #define IACTION_CRAFT 2
97 struct InventoryAction
99 static InventoryAction * deSerialize(std::istream &is);
101 virtual u16 getType() const = 0;
102 virtual void serialize(std::ostream &os) const = 0;
103 virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
104 IGameDef *gamedef) = 0;
105 virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef) = 0;
108 struct IMoveAction : public InventoryAction
110 // count=0 means "everything"
112 InventoryLocation from_inv;
113 std::string from_list;
115 InventoryLocation to_inv;
126 IMoveAction(std::istream &is);
133 void serialize(std::ostream &os) const
137 os<<from_inv.dump()<<" ";
140 os<<to_inv.dump()<<" ";
145 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
147 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
150 struct IDropAction : public InventoryAction
152 // count=0 means "everything"
154 InventoryLocation from_inv;
155 std::string from_list;
164 IDropAction(std::istream &is);
171 void serialize(std::ostream &os) const
175 os<<from_inv.dump()<<" ";
180 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
182 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
185 struct ICraftAction : public InventoryAction
187 // count=0 means "everything"
189 InventoryLocation craft_inv;
196 ICraftAction(std::istream &is);
200 return IACTION_CRAFT;
203 void serialize(std::ostream &os) const
207 os<<craft_inv.dump()<<" ";
210 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
212 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
216 bool getCraftingResult(Inventory *inv, ItemStack& result,
217 bool decrementInput, IGameDef *gamedef);