Luanti 5.15.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 side-effects
252 {
253 invlist->m_resize_locks -= 1;
254 }
255 };
256 using ResizeLocked = std::unique_ptr<InventoryList, ResizeUnlocker>;
257
258 void checkResizeLock();
259
261 {
262 m_resize_locks += 1;
263 return ResizeLocked(this);
264 }
265
266private:
267 std::vector<ItemStack> m_items;
268 std::string m_name;
269 u32 m_size; // always the same as m_items.size()
270 u32 m_width = 0;
272 bool m_dirty = true;
273 int m_resize_locks = 0; // Lua callback sanity
274};
275
277{
278public:
279 ~Inventory();
280
281 void clear();
282
283 Inventory(IItemDefManager *itemdef);
284 Inventory(const Inventory &other);
285 Inventory & operator = (const Inventory &other);
286 bool operator == (const Inventory &other) const;
287 bool operator != (const Inventory &other) const
288 {
289 return !(*this == other);
290 }
291
292 // Never ever serialize to disk using "incremental"!
293 void serialize(std::ostream &os, bool incremental = false) const;
294 void deSerialize(std::istream &is);
295
296 // Creates a new list if none exists or truncates existing lists
297 InventoryList * addList(const std::string &name, u32 size);
298 InventoryList * getList(const std::string &name);
299 const InventoryList * getList(const std::string &name) const;
300 const std::vector<InventoryList *> &getLists() const { return m_lists; }
301 bool deleteList(const std::string &name);
302 // A shorthand for adding items. Returns leftover item (possibly empty).
303 ItemStack addItem(const std::string &listname, const ItemStack &newitem)
304 {
305 InventoryList *list = getList(listname);
306 if(list == NULL)
307 return newitem;
308 return list->addItem(newitem);
309 }
310
311 inline bool checkModified() const
312 {
313 if (m_dirty)
314 return true;
315
316 for (const auto &list : m_lists)
317 if (list->checkModified())
318 return true;
319
320 return false;
321 }
322
323 inline void setModified(bool dirty = true)
324 {
325 m_dirty = dirty;
326 // Set all as handled
327 if (!dirty) {
328 for (const auto &list : m_lists)
329 list->setModified(dirty);
330 }
331 }
332private:
333 // -1 if not found
334 s32 getListIndex(const std::string &name) const;
335
336 std::vector<InventoryList*> m_lists;
338 bool m_dirty = true;
339};
Definition itemdef.h:146
Definition inventory.h:157
bool operator==(const InventoryList &other) const
Definition inventory.cpp:627
u32 getSize() const
Definition inventory.h:177
bool containsItem(const ItemStack &item, bool match_meta) const
Definition inventory.cpp:748
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:672
std::vector< ItemStack > m_items
Definition inventory.h:267
void checkResizeLock()
Definition inventory.cpp:865
bool operator!=(const InventoryList &other) const
Definition inventory.h:171
IItemDefManager * m_itemdef
Definition inventory.h:271
InventoryList(const InventoryList &other)
Definition inventory.h:168
int m_resize_locks
Definition inventory.h:273
bool m_dirty
Definition inventory.h:272
void setName(const std::string &name)
Definition inventory.cpp:529
void setWidth(u32 newWidth)
Definition inventory.cpp:523
bool checkModified() const
Definition inventory.h:245
std::string m_name
Definition inventory.h:268
u32 m_size
Definition inventory.h:269
void deSerialize(std::istream &is)
Definition inventory.cpp:557
u32 getUsedSlots() const
Definition inventory.cpp:642
InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef)
Definition inventory.cpp:491
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:821
ItemStack changeItem(u32 i, const ItemStack &newitem)
Definition inventory.cpp:652
ResizeLocked resizeLock()
Definition inventory.h:260
ItemStack removeItem(const ItemStack &item, bool match_meta)
Definition inventory.cpp:767
void setModified(bool dirty=true)
Definition inventory.h:246
void setSize(u32 newsize)
Definition inventory.cpp:510
InventoryList & operator=(const InventoryList &other)
Definition inventory.cpp:613
void clearItems()
Definition inventory.cpp:499
~InventoryList()=default
void serialize(std::ostream &os, bool incremental) const
Definition inventory.cpp:535
void deleteItem(u32 i)
Definition inventory.cpp:665
const std::string & getName() const
Definition inventory.h:176
bool roomForItem(const ItemStack &item) const
Definition inventory.cpp:735
u32 m_width
Definition inventory.h:270
ItemStack takeItem(u32 i, u32 takecount)
Definition inventory.cpp:787
std::unique_ptr< InventoryList, ResizeUnlocker > ResizeLocked
Definition inventory.h:256
bool itemFits(const u32 i, const ItemStack &newitem, ItemStack *restitem=NULL) const
Definition inventory.cpp:722
void moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
Definition inventory.cpp:798
Definition inventory.h:277
ItemStack addItem(const std::string &listname, const ItemStack &newitem)
Definition inventory.h:303
void clear()
Definition inventory.cpp:883
Inventory(IItemDefManager *itemdef)
Definition inventory.cpp:898
InventoryList * getList(const std::string &name)
Definition inventory.cpp:1051
bool deleteList(const std::string &name)
Definition inventory.cpp:1059
InventoryList * addList(const std::string &name, u32 size)
Definition inventory.cpp:1025
bool operator==(const Inventory &other) const
Definition inventory.cpp:924
bool checkModified() const
Definition inventory.h:311
IItemDefManager * m_itemdef
Definition inventory.h:337
~Inventory()
Definition inventory.cpp:878
const std::vector< InventoryList * > & getLists() const
Definition inventory.h:300
void setModified(bool dirty=true)
Definition inventory.h:323
s32 getListIndex(const std::string &name) const
Definition inventory.cpp:1081
void serialize(std::ostream &os, bool incremental=false) const
Definition inventory.cpp:937
bool m_dirty
Definition inventory.h:338
void deSerialize(std::istream &is)
Definition inventory.cpp:952
Inventory & operator=(const Inventory &other)
Definition inventory.cpp:909
std::vector< InventoryList * > m_lists
Definition inventory.h:336
bool operator!=(const Inventory &other) const
Definition inventory.h:287
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:476
void clear()
Definition inventory.h:56
ItemStack takeItem(u32 takecount)
Definition inventory.cpp:456
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:244
ItemImageDef getInventoryImage(const IItemDefManager *itemdef) const
Definition inventory.cpp:258
bool addWear(s32 amount, const IItemDefManager *itemdef)
Definition inventory.cpp:358
std::string getItemString(bool include_meta=true) const
Definition inventory.cpp:229
bool isKnown(const IItemDefManager *itemdef) const
Definition inventory.cpp:310
bool itemFits(ItemStack newitem, ItemStack *restitem, IItemDefManager *itemdef) const
Definition inventory.cpp:410
const ItemDefinition & getDefinition(const IItemDefManager *itemdef) const
Definition inventory.cpp:315
std::string getDescription(const IItemDefManager *itemdef) const
Definition inventory.cpp:236
ItemStackMetadata metadata
Definition inventory.h:153
const ToolCapabilities & getToolCapabilities(const IItemDefManager *itemdef, const ItemStack *hand=nullptr) const
Definition inventory.cpp:321
ItemStack()=default
~ItemStack()=default
ItemImageDef getWieldOverlay(const IItemDefManager *itemdef) const
Definition inventory.cpp:288
bool empty() const
Definition inventory.h:51
ItemImageDef getWieldImage(const IItemDefManager *itemdef) const
Definition inventory.cpp:278
std::string name
Definition inventory.h:150
bool stacksWith(const ItemStack &other) const
Definition inventory.cpp:449
u16 getStackMax(const IItemDefManager *itemdef) const
Definition inventory.cpp:305
u16 wear
Definition inventory.h:152
ItemImageDef getInventoryOverlay(const IItemDefManager *itemdef) const
Definition inventory.cpp:268
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:349
v3f getWieldScale(const IItemDefManager *itemdef) const
Definition inventory.cpp:298
void remove(u16 n)
Definition inventory.h:69
ItemStack addItem(ItemStack newitem, IItemDefManager *itemdef)
Definition inventory.cpp:373
Definition tool.h:53