Optimize headers
[oweals/minetest.git] / src / inventory.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 INVENTORY_HEADER
21 #define INVENTORY_HEADER
22
23 #include <iostream>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 #include "irrlichttypes_bloated.h"
28 #include "debug.h"
29 #include "itemdef.h"
30
31 struct ToolCapabilities;
32
33 struct ItemStack
34 {
35         ItemStack(): name(""), count(0), wear(0), metadata("") {}
36         ItemStack(std::string name_, u16 count_,
37                         u16 wear, std::string metadata_,
38                         IItemDefManager *itemdef);
39         ~ItemStack() {}
40
41         // Serialization
42         void serialize(std::ostream &os) const;
43         void deSerialize(std::istream &is, IItemDefManager *itemdef);
44         void deSerialize(const std::string &s, IItemDefManager *itemdef);
45
46         // Returns the string used for inventory
47         std::string getItemString() const;
48
49         /*
50                 Quantity methods
51         */
52
53         bool empty() const
54         {
55                 return count == 0;
56         }
57
58         void clear()
59         {
60                 name = "";
61                 count = 0;
62                 wear = 0;
63                 metadata = "";
64         }
65
66         void add(u16 n)
67         {
68                 count += n;
69         }
70
71         void remove(u16 n)
72         {
73                 assert(count >= n);
74                 count -= n;
75                 if(count == 0)
76                         clear(); // reset name, wear and metadata too
77         }
78
79         // Maximum size of a stack
80         u16 getStackMax(IItemDefManager *itemdef) const
81         {
82                 s16 max = itemdef->get(name).stack_max;
83                 return (max >= 0) ? max : 0;
84         }
85
86         // Number of items that can be added to this stack
87         u16 freeSpace(IItemDefManager *itemdef) const
88         {
89                 u16 max = getStackMax(itemdef);
90                 if(count > max)
91                         return 0;
92                 return max - count;
93         }
94
95         // Returns false if item is not known and cannot be used
96         bool isKnown(IItemDefManager *itemdef) const
97         {
98                 return itemdef->isKnown(name);
99         }
100
101         // Returns a pointer to the item definition struct,
102         // or a fallback one (name="unknown") if the item is unknown.
103         const ItemDefinition& getDefinition(
104                         IItemDefManager *itemdef) const
105         {
106                 return itemdef->get(name);
107         }
108
109         // Get tool digging properties, or those of the hand if not a tool
110         const ToolCapabilities& getToolCapabilities(
111                         IItemDefManager *itemdef) const
112         {
113                 ToolCapabilities *cap;
114                 cap = itemdef->get(name).tool_capabilities;
115                 if(cap == NULL)
116                         cap = itemdef->get("").tool_capabilities;
117                 assert(cap != NULL);
118                 return *cap;
119         }
120
121         // Wear out (only tools)
122         // Returns true if the item is (was) a tool
123         bool addWear(s32 amount, IItemDefManager *itemdef)
124         {
125                 if(getDefinition(itemdef).type == ITEM_TOOL)
126                 {
127                         if(amount > 65535 - wear)
128                                 clear();
129                         else if(amount < -wear)
130                                 wear = 0;
131                         else
132                                 wear += amount;
133                         return true;
134                 }
135                 else
136                 {
137                         return false;
138                 }
139         }
140
141         // If possible, adds newitem to this item.
142         // If cannot be added at all, returns the item back.
143         // If can be added partly, decremented item is returned back.
144         // If can be added fully, empty item is returned.
145         ItemStack addItem(const ItemStack &newitem,
146                         IItemDefManager *itemdef);
147
148         // Checks whether newitem could be added.
149         // If restitem is non-NULL, it receives the part of newitem that
150         // would be left over after adding.
151         bool itemFits(const ItemStack &newitem,
152                         ItemStack *restitem,  // may be NULL
153                         IItemDefManager *itemdef) const;
154
155         // Takes some items.
156         // If there are not enough, takes as many as it can.
157         // Returns empty item if couldn't take any.
158         ItemStack takeItem(u32 takecount);
159
160         // Similar to takeItem, but keeps this ItemStack intact.
161         ItemStack peekItem(u32 peekcount) const;
162
163         /*
164                 Properties
165         */
166         std::string name;
167         u16 count;
168         u16 wear;
169         std::string metadata;
170 };
171
172 class InventoryList
173 {
174 public:
175         InventoryList(std::string name, u32 size, IItemDefManager *itemdef);
176         ~InventoryList();
177         void clearItems();
178         void setSize(u32 newsize);
179         void setName(const std::string &name);
180         void serialize(std::ostream &os) const;
181         void deSerialize(std::istream &is);
182
183         InventoryList(const InventoryList &other);
184         InventoryList & operator = (const InventoryList &other);
185
186         const std::string &getName() const;
187         u32 getSize() const;
188         // Count used slots
189         u32 getUsedSlots() const;
190         u32 getFreeSlots() const;
191
192         // Get reference to item
193         const ItemStack& getItem(u32 i) const;
194         ItemStack& getItem(u32 i);
195         // Returns old item. Parameter can be an empty item.
196         ItemStack changeItem(u32 i, const ItemStack &newitem);
197         // Delete item
198         void deleteItem(u32 i);
199
200         // Adds an item to a suitable place. Returns leftover item (possibly empty).
201         ItemStack addItem(const ItemStack &newitem);
202
203         // If possible, adds item to given slot.
204         // If cannot be added at all, returns the item back.
205         // If can be added partly, decremented item is returned back.
206         // If can be added fully, empty item is returned.
207         ItemStack addItem(u32 i, const ItemStack &newitem);
208
209         // Checks whether the item could be added to the given slot
210         // If restitem is non-NULL, it receives the part of newitem that
211         // would be left over after adding.
212         bool itemFits(const u32 i, const ItemStack &newitem,
213                         ItemStack *restitem = NULL) const;
214
215         // Checks whether there is room for a given item
216         bool roomForItem(const ItemStack &item) const;
217
218         // Checks whether the given count of the given item name
219         // exists in this inventory list.
220         bool containsItem(const ItemStack &item) const;
221
222         // Removes the given count of the given item name from
223         // this inventory list. Walks the list in reverse order.
224         // If not as many items exist as requested, removes as
225         // many as possible.
226         // Returns the items that were actually removed.
227         ItemStack removeItem(const ItemStack &item);
228
229         // Takes some items from a slot.
230         // If there are not enough, takes as many as it can.
231         // Returns empty item if couldn't take any.
232         ItemStack takeItem(u32 i, u32 takecount);
233
234         // Similar to takeItem, but keeps the slot intact.
235         ItemStack peekItem(u32 i, u32 peekcount) const;
236
237         // Move an item to a different list (or a different stack in the same list)
238         // count is the maximum number of items to move (0 for everything)
239         void moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count = 0);
240
241 private:
242         std::vector<ItemStack> m_items;
243         u32 m_size;
244         std::string m_name;
245         IItemDefManager *m_itemdef;
246 };
247
248 class Inventory
249 {
250 public:
251         ~Inventory();
252
253         void clear();
254         void clearContents();
255
256         Inventory(IItemDefManager *itemdef);
257         Inventory(const Inventory &other);
258         Inventory & operator = (const Inventory &other);
259         
260         void serialize(std::ostream &os) const;
261         void deSerialize(std::istream &is);
262
263         InventoryList * addList(const std::string &name, u32 size);
264         InventoryList * getList(const std::string &name);
265         const InventoryList * getList(const std::string &name) const;
266         std::vector<const InventoryList*> getLists();
267         bool deleteList(const std::string &name);
268         // A shorthand for adding items. Returns leftover item (possibly empty).
269         ItemStack addItem(const std::string &listname, const ItemStack &newitem)
270         {
271                 InventoryList *list = getList(listname);
272                 if(list == NULL)
273                         return newitem;
274                 return list->addItem(newitem);
275         }
276         
277 private:
278         // -1 if not found
279         const s32 getListIndex(const std::string &name) const;
280
281         std::vector<InventoryList*> m_lists;
282         IItemDefManager *m_itemdef;
283 };
284
285 #endif
286