]> git.lizzy.rs Git - dragonfireclient.git/blob - src/inventory.h
Define operators == and != for ItemStack
[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) 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                 const ToolCapabilities *item_cap =
115                         itemdef->get(name).tool_capabilities;
116
117                 if (item_cap == NULL)
118                         // Fall back to the hand's tool capabilities
119                         item_cap = itemdef->get("").tool_capabilities;
120
121                 assert(item_cap != NULL);
122                 return metadata.getToolCapabilities(*item_cap); // Check for override
123         }
124
125         // Wear out (only tools)
126         // Returns true if the item is (was) a tool
127         bool addWear(s32 amount, IItemDefManager *itemdef)
128         {
129                 if(getDefinition(itemdef).type == ITEM_TOOL)
130                 {
131                         if(amount > 65535 - wear)
132                                 clear();
133                         else if(amount < -wear)
134                                 wear = 0;
135                         else
136                                 wear += amount;
137                         return true;
138                 }
139
140                 return false;
141         }
142
143         // If possible, adds newitem to this item.
144         // If cannot be added at all, returns the item back.
145         // If can be added partly, decremented item is returned back.
146         // If can be added fully, empty item is returned.
147         ItemStack addItem(ItemStack newitem, IItemDefManager *itemdef);
148
149         // Checks whether newitem could be added.
150         // If restitem is non-NULL, it receives the part of newitem that
151         // would be left over after adding.
152         bool itemFits(ItemStack newitem,
153                         ItemStack *restitem,  // may be NULL
154                         IItemDefManager *itemdef) const;
155
156         // Takes some items.
157         // If there are not enough, takes as many as it can.
158         // Returns empty item if couldn't take any.
159         ItemStack takeItem(u32 takecount);
160
161         // Similar to takeItem, but keeps this ItemStack intact.
162         ItemStack peekItem(u32 peekcount) const;
163
164         bool operator ==(const ItemStack &s) const
165         {
166                 return (this->name     == s.name &&
167                                 this->count    == s.count &&
168                                 this->wear     == s.wear &&
169                                 this->metadata == s.metadata);
170         }
171
172         bool operator !=(const ItemStack &s) const
173         {
174                 return !(*this == s);
175         }
176
177         /*
178                 Properties
179         */
180         std::string name = "";
181         u16 count = 0;
182         u16 wear = 0;
183         ItemStackMetadata metadata;
184 };
185
186 class InventoryList
187 {
188 public:
189         InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
190         ~InventoryList() = default;
191         void clearItems();
192         void setSize(u32 newsize);
193         void setWidth(u32 newWidth);
194         void setName(const std::string &name);
195         void serialize(std::ostream &os) const;
196         void deSerialize(std::istream &is);
197
198         InventoryList(const InventoryList &other);
199         InventoryList & operator = (const InventoryList &other);
200         bool operator == (const InventoryList &other) const;
201         bool operator != (const InventoryList &other) const
202         {
203                 return !(*this == other);
204         }
205
206         const std::string &getName() const;
207         u32 getSize() const;
208         u32 getWidth() const;
209         // Count used slots
210         u32 getUsedSlots() const;
211         u32 getFreeSlots() const;
212
213         // Get reference to item
214         const ItemStack& getItem(u32 i) const;
215         ItemStack& getItem(u32 i);
216         // Returns old item. Parameter can be an empty item.
217         ItemStack changeItem(u32 i, const ItemStack &newitem);
218         // Delete item
219         void deleteItem(u32 i);
220
221         // Adds an item to a suitable place. Returns leftover item (possibly empty).
222         ItemStack addItem(const ItemStack &newitem);
223
224         // If possible, adds item to given slot.
225         // If cannot be added at all, returns the item back.
226         // If can be added partly, decremented item is returned back.
227         // If can be added fully, empty item is returned.
228         ItemStack addItem(u32 i, const ItemStack &newitem);
229
230         // Checks whether the item could be added to the given slot
231         // If restitem is non-NULL, it receives the part of newitem that
232         // would be left over after adding.
233         bool itemFits(const u32 i, const ItemStack &newitem,
234                         ItemStack *restitem = NULL) const;
235
236         // Checks whether there is room for a given item
237         bool roomForItem(const ItemStack &item) const;
238
239         // Checks whether the given count of the given item
240         // exists in this inventory list.
241         // If match_meta is false, only the items' names are compared.
242         bool containsItem(const ItemStack &item, bool match_meta) const;
243
244         // Removes the given count of the given item name from
245         // this inventory list. Walks the list in reverse order.
246         // If not as many items exist as requested, removes as
247         // many as possible.
248         // Returns the items that were actually removed.
249         ItemStack removeItem(const ItemStack &item);
250
251         // Takes some items from a slot.
252         // If there are not enough, takes as many as it can.
253         // Returns empty item if couldn't take any.
254         ItemStack takeItem(u32 i, u32 takecount);
255
256         // Move an item to a different list (or a different stack in the same list)
257         // count is the maximum number of items to move (0 for everything)
258         // returns number of moved items
259         u32 moveItem(u32 i, InventoryList *dest, u32 dest_i,
260                 u32 count = 0, bool swap_if_needed = true, bool *did_swap = NULL);
261
262         // like moveItem, but without a fixed destination index
263         // also with optional rollback recording
264         void moveItemSomewhere(u32 i, InventoryList *dest, u32 count);
265
266 private:
267         std::vector<ItemStack> m_items;
268         std::string m_name;
269         u32 m_size;
270         u32 m_width = 0;
271         IItemDefManager *m_itemdef;
272 };
273
274 class Inventory
275 {
276 public:
277         ~Inventory();
278
279         void clear();
280         void clearContents();
281
282         Inventory(IItemDefManager *itemdef);
283         Inventory(const Inventory &other);
284         Inventory & operator = (const Inventory &other);
285         bool operator == (const Inventory &other) const;
286         bool operator != (const Inventory &other) const
287         {
288                 return !(*this == other);
289         }
290
291         void serialize(std::ostream &os) const;
292         void deSerialize(std::istream &is);
293
294         InventoryList * addList(const std::string &name, u32 size);
295         InventoryList * getList(const std::string &name);
296         const InventoryList * getList(const std::string &name) const;
297         std::vector<const InventoryList*> getLists();
298         bool deleteList(const std::string &name);
299         // A shorthand for adding items. Returns leftover item (possibly empty).
300         ItemStack addItem(const std::string &listname, const ItemStack &newitem)
301         {
302                 m_dirty = true;
303                 InventoryList *list = getList(listname);
304                 if(list == NULL)
305                         return newitem;
306                 return list->addItem(newitem);
307         }
308
309         bool checkModified() const
310         {
311                 return m_dirty;
312         }
313
314         void setModified(const bool x)
315         {
316                 m_dirty = x;
317         }
318
319 private:
320         // -1 if not found
321         const s32 getListIndex(const std::string &name) const;
322
323         std::vector<InventoryList*> m_lists;
324         IItemDefManager *m_itemdef;
325         bool m_dirty = false;
326 };