]> git.lizzy.rs Git - minetest-m13.git/blob - src/inventorymanager.h
Add Minetest-M13 source
[minetest-m13.git] / src / inventorymanager.h
1 /*
2 Minetest-m13
3 Copyright (C) 2010-2011 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 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.
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 General Public License for more details.
14
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.
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         } type;
36
37         std::string name; // PLAYER
38         v3s16 p; // NODEMETA
39
40         InventoryLocation()
41         {
42                 setUndefined();
43         }
44         void setUndefined()
45         {
46                 type = UNDEFINED;
47         }
48         void setCurrentPlayer()
49         {
50                 type = CURRENT_PLAYER;
51         }
52         void setPlayer(const std::string &name_)
53         {
54                 type = PLAYER;
55                 name = name_;
56         }
57         void setNodeMeta(v3s16 p_)
58         {
59                 type = NODEMETA;
60                 p = p_;
61         }
62
63         void applyCurrentPlayer(const std::string &name_)
64         {
65                 if(type == CURRENT_PLAYER)
66                         setPlayer(name_);
67         }
68
69         std::string dump() const;
70         void serialize(std::ostream &os) const;
71         void deSerialize(std::istream &is);
72         void deSerialize(std::string s);
73 };
74
75 struct InventoryAction;
76
77 class InventoryManager
78 {
79 public:
80         InventoryManager(){}
81         virtual ~InventoryManager(){}
82         
83         // Get an inventory or set it modified (so it will be updated over
84         // network or so)
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){}
88
89         // Used on the client to send an action to the server
90         virtual void inventoryAction(InventoryAction *a){}
91 };
92
93 #define IACTION_MOVE 0
94 #define IACTION_DROP 1
95 #define IACTION_CRAFT 2
96
97 struct InventoryAction
98 {
99         static InventoryAction * deSerialize(std::istream &is);
100         
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;
106 };
107
108 struct IMoveAction : public InventoryAction
109 {
110         // count=0 means "everything"
111         u16 count;
112         InventoryLocation from_inv;
113         std::string from_list;
114         s16 from_i;
115         InventoryLocation to_inv;
116         std::string to_list;
117         s16 to_i;
118         
119         IMoveAction()
120         {
121                 count = 0;
122                 from_i = -1;
123                 to_i = -1;
124         }
125         
126         IMoveAction(std::istream &is);
127
128         u16 getType() const
129         {
130                 return IACTION_MOVE;
131         }
132
133         void serialize(std::ostream &os) const
134         {
135                 os<<"Move ";
136                 os<<count<<" ";
137                 os<<from_inv.dump()<<" ";
138                 os<<from_list<<" ";
139                 os<<from_i<<" ";
140                 os<<to_inv.dump()<<" ";
141                 os<<to_list<<" ";
142                 os<<to_i;
143         }
144
145         void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
146
147         void clientApply(InventoryManager *mgr, IGameDef *gamedef);
148 };
149
150 struct IDropAction : public InventoryAction
151 {
152         // count=0 means "everything"
153         u16 count;
154         InventoryLocation from_inv;
155         std::string from_list;
156         s16 from_i;
157         
158         IDropAction()
159         {
160                 count = 0;
161                 from_i = -1;
162         }
163         
164         IDropAction(std::istream &is);
165
166         u16 getType() const
167         {
168                 return IACTION_DROP;
169         }
170
171         void serialize(std::ostream &os) const
172         {
173                 os<<"Drop ";
174                 os<<count<<" ";
175                 os<<from_inv.dump()<<" ";
176                 os<<from_list<<" ";
177                 os<<from_i;
178         }
179
180         void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
181
182         void clientApply(InventoryManager *mgr, IGameDef *gamedef);
183 };
184
185 struct ICraftAction : public InventoryAction
186 {
187         // count=0 means "everything"
188         u16 count;
189         InventoryLocation craft_inv;
190         
191         ICraftAction()
192         {
193                 count = 0;
194         }
195         
196         ICraftAction(std::istream &is);
197
198         u16 getType() const
199         {
200                 return IACTION_CRAFT;
201         }
202
203         void serialize(std::ostream &os) const
204         {
205                 os<<"Craft ";
206                 os<<count<<" ";
207                 os<<craft_inv.dump()<<" ";
208         }
209
210         void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
211
212         void clientApply(InventoryManager *mgr, IGameDef *gamedef);
213 };
214
215 // Crafting helper
216 bool getCraftingResult(Inventory *inv, ItemStack& result,
217                 bool decrementInput, IGameDef *gamedef);
218
219 #endif
220