4abe5e73d029b0537e5d4f115668b1bf12a22110
[oweals/minetest.git] / src / inventorymanager.h
1 /*
2 Minetest-c55
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
25 // Should probably somehow replace InventoryContext over time
26 struct InventoryLocation
27 {
28         enum Type{
29                 UNDEFINED,
30                 PLAYER,
31                 NODEMETA
32         } type;
33
34         std::string name; // PLAYER
35         v3s16 p; // NODEMETA
36
37         void setPlayer(const std::string &name_)
38         {
39                 type = PLAYER;
40                 name = name_;
41         }
42         void setNodeMeta(v3s16 p_)
43         {
44                 type = NODEMETA;
45                 p = p_;
46         }
47 };
48
49 class Player;
50
51 struct InventoryContext
52 {
53         Player *current_player;
54         
55         InventoryContext():
56                 current_player(NULL)
57         {}
58 };
59
60 struct InventoryAction;
61
62 class InventoryManager
63 {
64 public:
65         InventoryManager(){}
66         virtual ~InventoryManager(){}
67         
68         // Get an inventory or set it modified (so it will be updated over
69         // network or so)
70         virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
71         virtual void setInventoryModified(const InventoryLocation &loc){}
72
73         // Used on the client to send an action to the server
74         virtual void inventoryAction(InventoryAction *a){}
75
76         // (Deprecated; these wrap to the latter ones)
77         // Get a pointer to an inventory specified by id. id can be:
78         // - "current_player"
79         // - "nodemeta:X,Y,Z"
80         Inventory* getInventory(InventoryContext *c, std::string id);
81         // Used on the server by InventoryAction::apply and other stuff
82         void inventoryModified(InventoryContext *c, std::string id);
83 };
84
85 #define IACTION_MOVE 0
86 #define IACTION_DROP 1
87
88 struct InventoryAction
89 {
90         static InventoryAction * deSerialize(std::istream &is);
91         
92         virtual u16 getType() const = 0;
93         virtual void serialize(std::ostream &os) const = 0;
94         virtual void apply(InventoryContext *c, InventoryManager *mgr,
95                         ServerEnvironment *env) = 0;
96 };
97
98 struct IMoveAction : public InventoryAction
99 {
100         // count=0 means "everything"
101         u16 count;
102         std::string from_inv;
103         std::string from_list;
104         s16 from_i;
105         std::string to_inv;
106         std::string to_list;
107         s16 to_i;
108         
109         IMoveAction()
110         {
111                 count = 0;
112                 from_i = -1;
113                 to_i = -1;
114         }
115         
116         IMoveAction(std::istream &is);
117
118         u16 getType() const
119         {
120                 return IACTION_MOVE;
121         }
122
123         void serialize(std::ostream &os) const
124         {
125                 os<<"Move ";
126                 os<<count<<" ";
127                 os<<from_inv<<" ";
128                 os<<from_list<<" ";
129                 os<<from_i<<" ";
130                 os<<to_inv<<" ";
131                 os<<to_list<<" ";
132                 os<<to_i;
133         }
134
135         void apply(InventoryContext *c, InventoryManager *mgr,
136                         ServerEnvironment *env);
137 };
138
139 struct IDropAction : public InventoryAction
140 {
141         // count=0 means "everything"
142         u16 count;
143         std::string from_inv;
144         std::string from_list;
145         s16 from_i;
146         
147         IDropAction()
148         {
149                 count = 0;
150                 from_i = -1;
151         }
152         
153         IDropAction(std::istream &is);
154
155         u16 getType() const
156         {
157                 return IACTION_DROP;
158         }
159
160         void serialize(std::ostream &os) const
161         {
162                 os<<"Drop ";
163                 os<<count<<" ";
164                 os<<from_inv<<" ";
165                 os<<from_list<<" ";
166                 os<<from_i;
167         }
168
169         void apply(InventoryContext *c, InventoryManager *mgr,
170                         ServerEnvironment *env);
171 };
172
173 /*
174         Craft checking system
175 */
176
177 enum ItemSpecType
178 {
179         ITEM_NONE,
180         ITEM_MATERIAL,
181         ITEM_CRAFT,
182         ITEM_TOOL,
183         ITEM_MBO
184 };
185
186 struct ItemSpec
187 {
188         enum ItemSpecType type;
189         // Only other one of these is used
190         std::string name;
191         u16 num;
192
193         ItemSpec():
194                 type(ITEM_NONE)
195         {
196         }
197         ItemSpec(enum ItemSpecType a_type, std::string a_name):
198                 type(a_type),
199                 name(a_name),
200                 num(65535)
201         {
202         }
203         ItemSpec(enum ItemSpecType a_type, u16 a_num):
204                 type(a_type),
205                 name(""),
206                 num(a_num)
207         {
208         }
209
210         bool checkItem(const InventoryItem *item) const;
211 };
212
213 /*
214         items: a pointer to an array of 9 pointers to items
215         specs: a pointer to an array of 9 ItemSpecs
216 */
217 bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);
218
219 /*
220         items: a pointer to an array of 9 pointers to items
221         specs: a pointer to an array of 9 pointers to items
222 */
223 bool checkItemCombination(const InventoryItem * const * items,
224                 const InventoryItem * const * specs);
225
226
227 #endif
228