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;;
137 struct IMoveAction : public InventoryAction
139 // count=0 means "everything"
141 InventoryLocation from_inv;
142 std::string from_list;
144 InventoryLocation to_inv;
147 bool move_somewhere = false;
149 // treat these as private
150 // related to movement to somewhere
151 bool caused_by_move_somewhere = false;
154 IMoveAction() = default;
156 IMoveAction(std::istream &is, bool somewhere);
158 IAction getType() const
160 return IAction::Move;
163 void serialize(std::ostream &os) const
168 os << "MoveSomewhere ";
170 os << from_inv.dump() << " ";
171 os << from_list << " ";
173 os << to_inv.dump() << " ";
179 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
181 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
184 struct IDropAction : public InventoryAction
186 // count=0 means "everything"
188 InventoryLocation from_inv;
189 std::string from_list;
192 IDropAction() = default;
194 IDropAction(std::istream &is);
196 IAction getType() const
198 return IAction::Drop;
201 void serialize(std::ostream &os) const
205 os<<from_inv.dump()<<" ";
210 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
212 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
215 struct ICraftAction : public InventoryAction
217 // count=0 means "everything"
219 InventoryLocation craft_inv;
221 ICraftAction() = default;
223 ICraftAction(std::istream &is);
225 IAction getType() const
227 return IAction::Craft;
230 void serialize(std::ostream &os) const
234 os<<craft_inv.dump()<<" ";
237 void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
239 void clientApply(InventoryManager *mgr, IGameDef *gamedef);
243 bool getCraftingResult(Inventory *inv, ItemStack &result,
244 std::vector<ItemStack> &output_replacements,
245 bool decrementInput, IGameDef *gamedef);