]> git.lizzy.rs Git - dragonfireclient.git/blob - src/inventorymanager.h
Make shift the default descent control on ladders and when flying
[dragonfireclient.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 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         void applyCurrentPlayer(const std::string &name_)
70         {
71                 if(type == CURRENT_PLAYER)
72                         setPlayer(name_);
73         }
74
75         std::string dump() const;
76         void serialize(std::ostream &os) const;
77         void deSerialize(std::istream &is);
78         void deSerialize(std::string s);
79 };
80
81 struct InventoryAction;
82
83 class InventoryManager
84 {
85 public:
86         InventoryManager(){}
87         virtual ~InventoryManager(){}
88         
89         // Get an inventory (server and client)
90         virtual Inventory* getInventory(const InventoryLocation &loc){return NULL;}
91     // Set modified (will be saved and sent over network; only on server)
92         virtual void setInventoryModified(const InventoryLocation &loc){}
93     // Send inventory action to server (only on client)
94         virtual void inventoryAction(InventoryAction *a){}
95 };
96
97 #define IACTION_MOVE 0
98 #define IACTION_DROP 1
99 #define IACTION_CRAFT 2
100
101 struct InventoryAction
102 {
103         static InventoryAction * deSerialize(std::istream &is);
104         
105         virtual u16 getType() const = 0;
106         virtual void serialize(std::ostream &os) const = 0;
107         virtual void apply(InventoryManager *mgr, ServerActiveObject *player,
108                         IGameDef *gamedef) = 0;
109         virtual void clientApply(InventoryManager *mgr, IGameDef *gamedef) = 0;
110         virtual ~InventoryAction() {};
111 };
112
113 struct IMoveAction : public InventoryAction
114 {
115         // count=0 means "everything"
116         u16 count;
117         InventoryLocation from_inv;
118         std::string from_list;
119         s16 from_i;
120         InventoryLocation to_inv;
121         std::string to_list;
122         s16 to_i;
123         
124         IMoveAction()
125         {
126                 count = 0;
127                 from_i = -1;
128                 to_i = -1;
129         }
130         
131         IMoveAction(std::istream &is);
132
133         u16 getType() const
134         {
135                 return IACTION_MOVE;
136         }
137
138         void serialize(std::ostream &os) const
139         {
140                 os<<"Move ";
141                 os<<count<<" ";
142                 os<<from_inv.dump()<<" ";
143                 os<<from_list<<" ";
144                 os<<from_i<<" ";
145                 os<<to_inv.dump()<<" ";
146                 os<<to_list<<" ";
147                 os<<to_i;
148         }
149
150         void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
151
152         void clientApply(InventoryManager *mgr, IGameDef *gamedef);
153 };
154
155 struct IDropAction : public InventoryAction
156 {
157         // count=0 means "everything"
158         u16 count;
159         InventoryLocation from_inv;
160         std::string from_list;
161         s16 from_i;
162         
163         IDropAction()
164         {
165                 count = 0;
166                 from_i = -1;
167         }
168         
169         IDropAction(std::istream &is);
170
171         u16 getType() const
172         {
173                 return IACTION_DROP;
174         }
175
176         void serialize(std::ostream &os) const
177         {
178                 os<<"Drop ";
179                 os<<count<<" ";
180                 os<<from_inv.dump()<<" ";
181                 os<<from_list<<" ";
182                 os<<from_i;
183         }
184
185         void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
186
187         void clientApply(InventoryManager *mgr, IGameDef *gamedef);
188 };
189
190 struct ICraftAction : public InventoryAction
191 {
192         // count=0 means "everything"
193         u16 count;
194         InventoryLocation craft_inv;
195         
196         ICraftAction()
197         {
198                 count = 0;
199         }
200         
201         ICraftAction(std::istream &is);
202
203         u16 getType() const
204         {
205                 return IACTION_CRAFT;
206         }
207
208         void serialize(std::ostream &os) const
209         {
210                 os<<"Craft ";
211                 os<<count<<" ";
212                 os<<craft_inv.dump()<<" ";
213         }
214
215         void apply(InventoryManager *mgr, ServerActiveObject *player, IGameDef *gamedef);
216
217         void clientApply(InventoryManager *mgr, IGameDef *gamedef);
218 };
219
220 // Crafting helper
221 bool getCraftingResult(Inventory *inv, ItemStack& result,
222                 bool decrementInput, IGameDef *gamedef);
223
224 #endif
225