Luanti 5.19.0-dev
Loading...
Searching...
No Matches
inventory.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irrlichttypes.h"
8#include "itemstackmetadata.h"
9#include <istream>
10#include <memory>
11#include <ostream>
12#include <string>
13#include <vector>
14#include <cassert>
15
16struct ItemDefinition;
17struct ItemImageDef;
18struct ToolCapabilities;
19
21{
22 ItemStack() = default;
23
24 ItemStack(const std::string &name_, u16 count_,
25 u16 wear, IItemDefManager *itemdef);
26
27 ~ItemStack() = default;
28
29 // Serialization
30 void serialize(std::ostream &os, bool serialize_meta = true) const;
31 // Deserialization. Pass itemdef unless you don't want aliases resolved.
32 void deSerialize(std::istream &is, IItemDefManager *itemdef = NULL);
33 void deSerialize(const std::string &s, IItemDefManager *itemdef = NULL);
34
35 // Returns the string used for inventory
36 std::string getItemString(bool include_meta = true) const;
37 // Returns the tooltip
38 std::string getDescription(const IItemDefManager *itemdef) const;
39 std::string getShortDescription(const IItemDefManager *itemdef) const;
40
43 ItemImageDef getWieldImage(const IItemDefManager *itemdef) const;
44 ItemImageDef getWieldOverlay(const IItemDefManager *itemdef) const;
45 v3f getWieldScale(const IItemDefManager *itemdef) const;
46
47 /*
48 Quantity methods
49 */
50
51 bool empty() const
52 {
53 return count == 0;
54 }
55
56 void clear()
57 {
58 name = "";
59 count = 0;
60 wear = 0;
62 }
63
64 void add(u16 n)
65 {
66 count += n;
67 }
68
69 void remove(u16 n)
70 {
71 assert(count >= n); // Pre-condition
72 count -= n;
73 if(count == 0)
74 clear(); // reset name, wear and metadata too
75 }
76
77 // Maximum size of a stack
78 u16 getStackMax(const IItemDefManager *itemdef) const;
79
80 // Number of items that can be added to this stack
81 u16 freeSpace(const IItemDefManager *itemdef) const
82 {
83 u16 max = getStackMax(itemdef);
84 if (count >= max)
85 return 0;
86 return max - count;
87 }
88
89 // Returns false if item is not known and cannot be used
90 bool isKnown(const IItemDefManager *itemdef) const;
91
92 // Returns a pointer to the item definition struct,
93 // or a fallback one (name="unknown") if the item is unknown.
95 const IItemDefManager *itemdef) const;
96
97 // Get tool digging properties, or those of the hand if not a tool
98 // If not hand assumes default hand ""
100 const IItemDefManager *itemdef, const ItemStack *hand = nullptr) const;
101
102 const std::optional<WearBarParams> &getWearBarParams(
103 const IItemDefManager *itemdef) const;
104
105 // Wear out (only tools)
106 // Returns true if the item is (was) a tool
107 bool addWear(s32 amount, const IItemDefManager *itemdef);
108
109 // If possible, adds newitem to this item.
110 // If cannot be added at all, returns the item back.
111 // If can be added partly, decremented item is returned back.
112 // If can be added fully, empty item is returned.
113 ItemStack addItem(ItemStack newitem, IItemDefManager *itemdef);
114
115 // Checks whether newitem could be added.
116 // If restitem is non-NULL, it receives the part of newitem that
117 // would be left over after adding.
118 bool itemFits(ItemStack newitem,
119 ItemStack *restitem, // may be NULL
120 IItemDefManager *itemdef) const;
121
122 // Checks if another itemstack would stack with this one.
123 // Does not check if the item actually fits in the stack.
124 bool stacksWith(const ItemStack &other) const;
125
126 // Takes some items.
127 // If there are not enough, takes as many as it can.
128 // Returns empty item if couldn't take any.
129 ItemStack takeItem(u32 takecount);
130
131 // Similar to takeItem, but keeps this ItemStack intact.
132 ItemStack peekItem(u32 peekcount) const;
133
134 bool operator ==(const ItemStack &s) const
135 {
136 return (this->name == s.name &&
137 this->count == s.count &&
138 this->wear == s.wear &&
139 this->metadata == s.metadata);
140 }
141
142 bool operator !=(const ItemStack &s) const
143 {
144 return !(*this == s);
145 }
146
147 /*
148 Properties
149 */
150 std::string name = "";
151 u16 count = 0;
152 u16 wear = 0;
154};
155
157{
158public:
159 InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
160 ~InventoryList() = default;
161 void clearItems();
162 void setSize(u32 newsize);
163 void setWidth(u32 newWidth);
164 void setName(const std::string &name);
165 void serialize(std::ostream &os, bool incremental) const;
166 void deSerialize(std::istream &is);
167
168 InventoryList(const InventoryList &other) { *this = other; }
170 bool operator == (const InventoryList &other) const;
171 bool operator != (const InventoryList &other) const
172 {
173 return !(*this == other);
174 }
175
176 const std::string &getName() const { return m_name; }
177 u32 getSize() const { return static_cast<u32>(m_items.size()); }
178 u32 getWidth() const { return m_width; }
179 // Count used slots
180 u32 getUsedSlots() const;
181
182 // Get reference to item
183 const ItemStack &getItem(u32 i) const
184 {
185 assert(i < m_size); // Pre-condition
186 return m_items[i];
187 }
189 {
190 assert(i < m_size); // Pre-condition
191 return m_items[i];
192 }
193 // Get reference to all items
194 const std::vector<ItemStack> &getItems() const { return m_items; }
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
219 // exists in this inventory list.
220 // If match_meta is false, only the items' names are compared.
221 bool containsItem(const ItemStack &item, bool match_meta) const;
222
223 // Removes the given count of the given item name from
224 // this inventory list. Walks the list in reverse order.
225 // If not as many items exist as requested, removes as
226 // many as possible.
227 // Returns the items that were actually removed.
228 ItemStack removeItem(const ItemStack &item, bool match_meta);
229
230 // Takes some items from a slot.
231 // If there are not enough, takes as many as it can.
232 // Returns empty item if couldn't take any.
233 ItemStack takeItem(u32 i, u32 takecount);
234
235 // Move an item to a different list (or a different stack in the same list)
236 // count is the maximum number of items to move (0 for everything)
237 // returns the moved stack
238 ItemStack moveItem(u32 i, InventoryList *dest, u32 dest_i,
239 u32 count = 0, bool swap_if_needed = true, bool *did_swap = NULL);
240
241 // like moveItem, but without a fixed destination index
242 // also with optional rollback recording
243 void moveItemSomewhere(u32 i, InventoryList *dest, u32 count);
244
245 inline bool checkModified() const { return m_dirty; }
246 inline void setModified(bool dirty = true) { m_dirty = dirty; }
247
248 // Problem: C++ keeps references to InventoryList and ItemStack indices
249 // Until a better solution is found, this serves as a guard to prevent UaF.
252 {
253 if (invlist) {
254 assert(invlist->m_resize_locks > 0);
255 invlist->m_resize_locks -= 1;
256 }
257 }
258 };
259 using ResizeLocked = std::unique_ptr<InventoryList, ResizeUnlocker>;
260
261 void checkResizeLock();
262
264 {
265 m_resize_locks += 1;
266 return ResizeLocked(this);
267 }
268
269private:
270 std::vector<ItemStack> m_items;
271 std::string m_name;
272 u32 m_size; // always the same as m_items.size()
273 u32 m_width = 0;
275 bool m_dirty = true;
276 int m_resize_locks = 0; // Lua callback sanity
277};
278
280{
281public:
282 ~Inventory();
283
284 void clear();
285
286 Inventory(IItemDefManager *itemdef);
287 Inventory(const Inventory &other);
288 Inventory & operator = (const Inventory &other);
289 bool operator == (const Inventory &other) const;
290 bool operator != (const Inventory &other) const
291 {
292 return !(*this == other);
293 }
294
295 // Never ever serialize to disk using "incremental"!
296 void serialize(std::ostream &os, bool incremental = false) const;
297 void deSerialize(std::istream &is);
298
299 // Creates a new list if none exists or truncates existing lists
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 const std::vector<InventoryList *> &getLists() const { return m_lists; }
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 }
335private:
336 // -1 if not found
337 s32 getListIndex(const std::string &name) const;
338
339 std::vector<InventoryList*> m_lists;
341 bool m_dirty = true;
342};
Definition itemdef.h:146
Definition inventory.h:157
bool operator==(const InventoryList &other) const
Definition inventory.cpp:655
u32 getSize() const
Definition inventory.h:177
bool containsItem(const ItemStack &item, bool match_meta) const
Definition inventory.cpp:776
const ItemStack & getItem(u32 i) const
Definition inventory.h:183
const std::vector< ItemStack > & getItems() const
Definition inventory.h:194
ItemStack addItem(const ItemStack &newitem)
Definition inventory.cpp:700
std::vector< ItemStack > m_items
Definition inventory.h:270
void checkResizeLock()
Definition inventory.cpp:893
bool operator!=(const InventoryList &other) const
Definition inventory.h:171
IItemDefManager * m_itemdef
Definition inventory.h:274
InventoryList(const InventoryList &other)
Definition inventory.h:168
int m_resize_locks
Definition inventory.h:276
bool m_dirty
Definition inventory.h:275
void setName(const std::string &name)
Definition inventory.cpp:557
void setWidth(u32 newWidth)
Definition inventory.cpp:551
bool checkModified() const
Definition inventory.h:245
std::string m_name
Definition inventory.h:271
u32 m_size
Definition inventory.h:272
void deSerialize(std::istream &is)
Definition inventory.cpp:585
u32 getUsedSlots() const
Definition inventory.cpp:670
InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef)
Definition inventory.cpp:519
u32 getWidth() const
Definition inventory.h:178
ItemStack & getItem(u32 i)
Definition inventory.h:188
ItemStack moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count=0, bool swap_if_needed=true, bool *did_swap=NULL)
Definition inventory.cpp:849
ItemStack changeItem(u32 i, const ItemStack &newitem)
Definition inventory.cpp:680
ResizeLocked resizeLock()
Definition inventory.h:263
ItemStack removeItem(const ItemStack &item, bool match_meta)
Definition inventory.cpp:795
void setModified(bool dirty=true)
Definition inventory.h:246
void setSize(u32 newsize)
Definition inventory.cpp:538
InventoryList & operator=(const InventoryList &other)
Definition inventory.cpp:641
void clearItems()
Definition inventory.cpp:527
~InventoryList()=default
void serialize(std::ostream &os, bool incremental) const
Definition inventory.cpp:563
void deleteItem(u32 i)
Definition inventory.cpp:693
const std::string & getName() const
Definition inventory.h:176
bool roomForItem(const ItemStack &item) const
Definition inventory.cpp:763
u32 m_width
Definition inventory.h:273
ItemStack takeItem(u32 i, u32 takecount)
Definition inventory.cpp:815
std::unique_ptr< InventoryList, ResizeUnlocker > ResizeLocked
Definition inventory.h:259
bool itemFits(const u32 i, const ItemStack &newitem, ItemStack *restitem=NULL) const
Definition inventory.cpp:750
void moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
Definition inventory.cpp:826
ItemStack addItem(const std::string &listname, const ItemStack &newitem)
Definition inventory.h:306
void clear()
Definition inventory.cpp:911
Inventory(IItemDefManager *itemdef)
Definition inventory.cpp:926
InventoryList * getList(const std::string &name)
Definition inventory.cpp:1079
bool deleteList(const std::string &name)
Definition inventory.cpp:1087
InventoryList * addList(const std::string &name, u32 size)
Definition inventory.cpp:1053
bool operator==(const Inventory &other) const
Definition inventory.cpp:952
bool checkModified() const
Definition inventory.h:314
IItemDefManager * m_itemdef
Definition inventory.h:340
const std::vector< InventoryList * > & getLists() const
Definition inventory.h:303
void setModified(bool dirty=true)
Definition inventory.h:326
s32 getListIndex(const std::string &name) const
Definition inventory.cpp:1109
void serialize(std::ostream &os, bool incremental=false) const
Definition inventory.cpp:965
bool m_dirty
Definition inventory.h:341
void deSerialize(std::istream &is)
Definition inventory.cpp:980
Inventory & operator=(const Inventory &other)
Definition inventory.cpp:937
std::vector< InventoryList * > m_lists
Definition inventory.h:339
bool operator!=(const Inventory &other) const
Definition inventory.h:290
Definition itemstackmetadata.h:14
void clear() override
Definition itemstackmetadata.cpp:23
core::vector3df v3f
Definition irr_v3d.h:11
Definition inventory.h:250
void operator()(InventoryList *invlist)
Definition inventory.h:251
Definition itemdef.h:83
Definition itemdef.h:61
Definition inventory.h:21
void add(u16 n)
Definition inventory.h:64
u16 freeSpace(const IItemDefManager *itemdef) const
Definition inventory.h:81
u16 count
Definition inventory.h:151
ItemStack peekItem(u32 peekcount) const
Definition inventory.cpp:504
void clear()
Definition inventory.h:56
ItemStack takeItem(u32 takecount)
Definition inventory.cpp:484
bool operator!=(const ItemStack &s) const
Definition inventory.h:142
void serialize(std::ostream &os, bool serialize_meta=true) const
Definition inventory.cpp:42
std::string getShortDescription(const IItemDefManager *itemdef) const
Definition inventory.cpp:272
ItemImageDef getInventoryImage(const IItemDefManager *itemdef) const
Definition inventory.cpp:286
bool addWear(s32 amount, const IItemDefManager *itemdef)
Definition inventory.cpp:386
std::string getItemString(bool include_meta=true) const
Definition inventory.cpp:257
bool isKnown(const IItemDefManager *itemdef) const
Definition inventory.cpp:338
bool itemFits(ItemStack newitem, ItemStack *restitem, IItemDefManager *itemdef) const
Definition inventory.cpp:438
const ItemDefinition & getDefinition(const IItemDefManager *itemdef) const
Definition inventory.cpp:343
std::string getDescription(const IItemDefManager *itemdef) const
Definition inventory.cpp:264
ItemStackMetadata metadata
Definition inventory.h:153
const ToolCapabilities & getToolCapabilities(const IItemDefManager *itemdef, const ItemStack *hand=nullptr) const
Definition inventory.cpp:349
ItemStack()=default
~ItemStack()=default
ItemImageDef getWieldOverlay(const IItemDefManager *itemdef) const
Definition inventory.cpp:316
bool empty() const
Definition inventory.h:51
ItemImageDef getWieldImage(const IItemDefManager *itemdef) const
Definition inventory.cpp:306
std::string name
Definition inventory.h:150
bool stacksWith(const ItemStack &other) const
Definition inventory.cpp:477
u16 getStackMax(const IItemDefManager *itemdef) const
Definition inventory.cpp:333
u16 wear
Definition inventory.h:152
ItemImageDef getInventoryOverlay(const IItemDefManager *itemdef) const
Definition inventory.cpp:296
void deSerialize(std::istream &is, IItemDefManager *itemdef=NULL)
Definition inventory.cpp:70
bool operator==(const ItemStack &s) const
Definition inventory.h:134
const std::optional< WearBarParams > & getWearBarParams(const IItemDefManager *itemdef) const
Definition inventory.cpp:377
v3f getWieldScale(const IItemDefManager *itemdef) const
Definition inventory.cpp:326
void remove(u16 n)
Definition inventory.h:69
ItemStack addItem(ItemStack newitem, IItemDefManager *itemdef)
Definition inventory.cpp:401
Definition tool.h:53