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