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