]> git.lizzy.rs Git - dragonfireclient.git/blob - src/inventorymanager.h
clientmap, clientmedia: code modernization
[dragonfireclient.git] / src / inventorymanager.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef INVENTORYMANAGER_HEADER
21 #define INVENTORYMANAGER_HEADER
22
23 #include "inventory.h"
24 #include <iostream>
25 #include <string>
26 class ServerActiveObject;
27
28 struct InventoryLocation
29 {
30         enum Type{
31                 UNDEFINED,
32                 CURRENT_PLAYER,
33                 PLAYER,
34                 NODEMETA,
35         DETACHED,
36         } type;
37
38         std::string name; // PLAYER, DETACHED
39         v3s16 p; // NODEMETA
40
41         InventoryLocation()
42         {
43                 setUndefined();
44         }
45         void setUndefined()
46         {
47                 type = UNDEFINED;
48         }
49         void setCurrentPlayer()
50         {
51                 type = CURRENT_PLAYER;
52         }
53         void setPlayer(const std::string &name_)
54         {
55                 type = PLAYER;
56                 name = name_;
57         }
58         void setNodeMeta(v3s16 p_)
59         {
60                 type = NODEMETA;
61                 p = p_;
62         }
63         void setDetached(const std::string &name_)
64         {
65                 type = DETACHED;
66                 name = name_;
67         }
68
69         bool operator==(const InventoryLocation &other) const
70         {
71                 if(type != other.type)
72                         return false;
73                 switch(type){
74                 case UNDEFINED:
75                         return false;
76                 case CURRENT_PLAYER:
77                         return true;
78                 case PLAYER:
79                         return (name == other.name);
80                 case NODEMETA:
81                         return (p == other.p);
82                 case DETACHED:
83                         return (name == other.name);
84                 }
85                 return false;
86         }
87         bool operator!=(const InventoryLocation &other) const
88         {
89                 return !(*this == other);
90         }
91
92         void applyCurrentPlayer(const std::string &name_)
93         {
94                 if(type == CURRENT_PLAYER)
95                         setPlayer(name_);
96         }
97
98         std::string dump() const;
99         void serialize(std::ostream &os) const;
100         void deSerialize(std::istream &is);
101         void deSerialize(std::string s);
102 };
103
104 struct InventoryAction;
105
106 class InventoryManager
107 {
108 public:
109         InventoryManager(){}
110         virtual ~InventoryManager(){}
111
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){}
118 };
119
120 enum class IAction : u16 {
121         Move,
122         Drop,
123         Craft
124 };
125
126 struct InventoryAction
127 {
128         static InventoryAction *deSerialize(std::istream &is);
129
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() {};
136 };
137
138 struct IMoveAction : public InventoryAction
139 {
140         // count=0 means "everything"
141         u16 count = 0;
142         InventoryLocation from_inv;
143         std::string from_list;
144         s16 from_i = -1;
145         InventoryLocation to_inv;
146         std::string to_list;
147         s16 to_i = -1;
148         bool move_somewhere = false;
149
150         // treat these as private
151         // related to movement to somewhere
152         bool caused_by_move_somewhere = false;
153         u32 move_count = 0;
154
155         IMoveAction() {}
156
157         IMoveAction(std::istream &is, bool somewhere);
158
159         IAction getType() const
160         {
161                 return IAction::Move;
162         }
163
164         void serialize(std::ostream &os) const
165         {
166                 if (!move_somewhere)
167                         os << "Move ";
168                 else
169                         os << "MoveSomewhere ";
170                 os << count << " ";
171                 os << from_inv.dump() << " ";
172                 os << from_list << " ";
173                 os << from_i << " ";
174                 os << to_inv.dump() << " ";
175                 os << to_list;
176                 if (!move_somewhere)
177                         os << " " << to_i;
178         }
179
180         void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
181
182         void clientApply(InventoryManager *mgr, IGameDef *gamedef);
183 };
184
185 struct IDropAction : public InventoryAction
186 {
187         // count=0 means "everything"
188         u16 count = 0;
189         InventoryLocation from_inv;
190         std::string from_list;
191         s16 from_i = -1;
192
193         IDropAction() {}
194
195         IDropAction(std::istream &is);
196
197         IAction getType() const
198         {
199                 return IAction::Drop;
200         }
201
202         void serialize(std::ostream &os) const
203         {
204                 os<<"Drop ";
205                 os<<count<<" ";
206                 os<<from_inv.dump()<<" ";
207                 os<<from_list<<" ";
208                 os<<from_i;
209         }
210
211         void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
212
213         void clientApply(InventoryManager *mgr, IGameDef *gamedef);
214 };
215
216 struct ICraftAction : public InventoryAction
217 {
218         // count=0 means "everything"
219         u16 count = 0;
220         InventoryLocation craft_inv;
221
222         ICraftAction() {}
223
224         ICraftAction(std::istream &is);
225
226         IAction getType() const
227         {
228                 return IAction::Craft;
229         }
230
231         void serialize(std::ostream &os) const
232         {
233                 os<<"Craft ";
234                 os<<count<<" ";
235                 os<<craft_inv.dump()<<" ";
236         }
237
238         void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
239
240         void clientApply(InventoryManager *mgr, IGameDef *gamedef);
241 };
242
243 // Crafting helper
244 bool getCraftingResult(Inventory *inv, ItemStack &result,
245                 std::vector<ItemStack> &output_replacements,
246                 bool decrementInput, IGameDef *gamedef);
247
248 #endif
249