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