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 enum class IAction : u16 {
126 struct InventoryAction
128 static InventoryAction *deSerialize(std::istream &is);
130 virtual IAction getType() const = 0;
131 virtual void serialize(std::ostream &os) const = 0;
132 virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
133 IGameDef *gamedef) = 0;
134 virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef) = 0;
135 virtual ~InventoryAction() {};
138 struct IMoveAction : public InventoryAction
140 // count=0 means "everything"
142 InventoryLocation from_inv;
143 std::string from_list;
145 InventoryLocation to_inv;
148 bool move_somewhere = false;
150 // treat these as private
151 // related to movement to somewhere
152 bool caused_by_move_somewhere = false;
157 IMoveAction(std::istream &is, bool somewhere);
159 IAction getType() const
161 return IAction::Move;
164 void serialize(std::ostream &os) const
169 os << "MoveSomewhere ";
171 os << from_inv.dump() << " ";
172 os << from_list << " ";
174 os << to_inv.dump() << " ";
180 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
182 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
185 struct IDropAction : public InventoryAction
187 // count=0 means "everything"
189 InventoryLocation from_inv;
190 std::string from_list;
195 IDropAction(std::istream &is);
197 IAction getType() const
199 return IAction::Drop;
202 void serialize(std::ostream &os) const
206 os<<from_inv.dump()<<" ";
211 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
213 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
216 struct ICraftAction : public InventoryAction
218 // count=0 means "everything"
220 InventoryLocation craft_inv;
224 ICraftAction(std::istream &is);
226 IAction getType() const
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 std::vector<ItemStack> &output_replacements,
246 bool decrementInput, IGameDef *gamedef);