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.
22 #include "inventory.h"
25 class ServerActiveObject;
27 struct InventoryLocation
37 std::string name; // PLAYER, DETACHED
48 void setCurrentPlayer()
50 type = CURRENT_PLAYER;
52 void setPlayer(const std::string &name_)
57 void setNodeMeta(const v3s16 &p_)
62 void setDetached(const std::string &name_)
68 bool operator==(const InventoryLocation &other) const
70 if(type != other.type)
78 return (name == other.name);
80 return (p == other.p);
82 return (name == other.name);
86 bool operator!=(const InventoryLocation &other) const
88 return !(*this == other);
91 void applyCurrentPlayer(const std::string &name_)
93 if(type == CURRENT_PLAYER)
97 std::string dump() const;
98 void serialize(std::ostream &os) const;
99 void deSerialize(std::istream &is);
100 void deSerialize(std::string s);
103 struct InventoryAction;
105 class InventoryManager
108 InventoryManager() = default;
109 virtual ~InventoryManager() = default;
111 // Get an inventory (server and client)
112 virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
113 // Set modified (will be saved and sent over network; only on server)
114 virtual void setInventoryModified(const InventoryLocation &loc, bool playerSend = true){}
115 // Send inventory action to server (only on client)
116 virtual void inventoryAction(InventoryAction *a){}
119 enum class IAction : u16 {
125 struct InventoryAction
127 static InventoryAction *deSerialize(std::istream &is);
129 virtual IAction getType() const = 0;
130 virtual void serialize(std::ostream &os) const = 0;
131 virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
132 IGameDef *gamedef) = 0;
133 virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef) = 0;
134 virtual ~InventoryAction() = default;;
139 InventoryLocation from_inv;
140 std::string from_list;
142 InventoryLocation to_inv;
147 struct IMoveAction : public InventoryAction, public MoveAction
149 // count=0 means "everything"
151 bool move_somewhere = false;
153 // treat these as private
154 // related to movement to somewhere
155 bool caused_by_move_somewhere = false;
158 IMoveAction() = default;
160 IMoveAction(std::istream &is, bool somewhere);
162 IAction getType() const
164 return IAction::Move;
167 void serialize(std::ostream &os) const
172 os << "MoveSomewhere ";
174 os << from_inv.dump() << " ";
175 os << from_list << " ";
177 os << to_inv.dump() << " ";
183 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
185 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
188 struct IDropAction : public InventoryAction, public MoveAction
190 // count=0 means "everything"
193 IDropAction() = default;
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;
222 ICraftAction() = default;
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);