Luanti 5.11.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 "itemdef.h"
8#include "irrlichttypes.h"
9#include "itemstackmetadata.h"
10#include <istream>
11#include <ostream>
12#include <string>
13#include <vector>
14#include <cassert>
15
16struct ToolCapabilities;
17
19{
20 ItemStack() = default;
21
22 ItemStack(const std::string &name_, u16 count_,
23 u16 wear, IItemDefManager *itemdef);
24
25 ~ItemStack() = default;
26
27 // Serialization
28 void serialize(std::ostream &os, bool serialize_meta = true) const;
29 // Deserialization. Pass itemdef unless you don't want aliases resolved.
30 void deSerialize(std::istream &is, IItemDefManager *itemdef = NULL);
31 void deSerialize(const std::string &s, IItemDefManager *itemdef = NULL);
32
33 // Returns the string used for inventory
34 std::string getItemString(bool include_meta = true) const;
35 // Returns the tooltip
36 std::string getDescription(const IItemDefManager *itemdef) const;
37 std::string getShortDescription(const IItemDefManager *itemdef) const;
38
39 std::string getInventoryImage(const IItemDefManager *itemdef) const;
40 std::string getInventoryOverlay(const IItemDefManager *itemdef) const;
41 std::string getWieldImage(const IItemDefManager *itemdef) const;
42 std::string getWieldOverlay(const IItemDefManager *itemdef) const;
43 v3f getWieldScale(const IItemDefManager *itemdef) const;
44
45 /*
46 Quantity methods
47 */
48
49 bool empty() const
50 {
51 return count == 0;
52 }
53
54 void clear()
55 {
56 name = "";
57 count = 0;
58 wear = 0;
60 }
61
62 void add(u16 n)
63 {
64 count += n;
65 }
66
67 void remove(u16 n)
68 {
69 assert(count >= n); // Pre-condition
70 count -= n;
71 if(count == 0)
72 clear(); // reset name, wear and metadata too
73 }
74
75 // Maximum size of a stack
76 u16 getStackMax(const IItemDefManager *itemdef) const
77 {
78 return itemdef->get(name).stack_max;
79 }
80
81 // Number of items that can be added to this stack
82 u16 freeSpace(const IItemDefManager *itemdef) const
83 {
84 u16 max = getStackMax(itemdef);
85 if (count >= max)
86 return 0;
87 return max - count;
88 }
89
90 // Returns false if item is not known and cannot be used
91 bool isKnown(const IItemDefManager *itemdef) const
92 {
93 return itemdef->isKnown(name);
94 }
95
96 // Returns a pointer to the item definition struct,
97 // or a fallback one (name="unknown") if the item is unknown.
99 const IItemDefManager *itemdef) const
100 {
101 return itemdef->get(name);
102 }
103
104 // Get tool digging properties, or those of the hand if not a tool
106 const IItemDefManager *itemdef) const
107 {
108 const ToolCapabilities *item_cap =
109 itemdef->get(name).tool_capabilities;
110
111 if (item_cap == NULL)
112 // Fall back to the hand's tool capabilities
113 item_cap = itemdef->get("").tool_capabilities;
114
115 assert(item_cap != NULL);
116 return metadata.getToolCapabilities(*item_cap); // Check for override
117 }
118
119 const std::optional<WearBarParams> &getWearBarParams(
120 const IItemDefManager *itemdef) const
121 {
122 auto &meta_override = metadata.getWearBarParamOverride();
123 if (meta_override.has_value())
124 return meta_override;
125 return itemdef->get(name).wear_bar_params;
126 }
127
128 // Wear out (only tools)
129 // Returns true if the item is (was) a tool
130 bool addWear(s32 amount, const 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 // Checks if another itemstack would stack with this one.
160 // Does not check if the item actually fits in the stack.
161 bool stacksWith(const ItemStack &other) const;
162
163 // Takes some items.
164 // If there are not enough, takes as many as it can.
165 // Returns empty item if couldn't take any.
166 ItemStack takeItem(u32 takecount);
167
168 // Similar to takeItem, but keeps this ItemStack intact.
169 ItemStack peekItem(u32 peekcount) const;
170
171 bool operator ==(const ItemStack &s) const
172 {
173 return (this->name == s.name &&
174 this->count == s.count &&
175 this->wear == s.wear &&
176 this->metadata == s.metadata);
177 }
178
179 bool operator !=(const ItemStack &s) const
180 {
181 return !(*this == s);
182 }
183
184 /*
185 Properties
186 */
187 std::string name = "";
188 u16 count = 0;
189 u16 wear = 0;
191};
192
194{
195public:
196 InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
197 ~InventoryList() = default;
198 void clearItems();
199 void setSize(u32 newsize);
200 void setWidth(u32 newWidth);
201 void setName(const std::string &name);
202 void serialize(std::ostream &os, bool incremental) const;
203 void deSerialize(std::istream &is);
204
205 InventoryList(const InventoryList &other) { *this = other; }
207 bool operator == (const InventoryList &other) const;
208 bool operator != (const InventoryList &other) const
209 {
210 return !(*this == other);
211 }
212
213 const std::string &getName() const { return m_name; }
214 u32 getSize() const { return static_cast<u32>(m_items.size()); }
215 u32 getWidth() const { return m_width; }
216 // Count used slots
217 u32 getUsedSlots() const;
218
219 // Get reference to item
220 const ItemStack &getItem(u32 i) const
221 {
222 assert(i < m_size); // Pre-condition
223 return m_items[i];
224 }
226 {
227 assert(i < m_size); // Pre-condition
228 return m_items[i];
229 }
230 // Get reference to all items
231 const std::vector<ItemStack> &getItems() const { return m_items; }
232 // Returns old item. Parameter can be an empty item.
233 ItemStack changeItem(u32 i, const ItemStack &newitem);
234 // Delete item
235 void deleteItem(u32 i);
236
237 // Adds an item to a suitable place. Returns leftover item (possibly empty).
238 ItemStack addItem(const ItemStack &newitem);
239
240 // If possible, adds item to given slot.
241 // If cannot be added at all, returns the item back.
242 // If can be added partly, decremented item is returned back.
243 // If can be added fully, empty item is returned.
244 ItemStack addItem(u32 i, const ItemStack &newitem);
245
246 // Checks whether the item could be added to the given slot
247 // If restitem is non-NULL, it receives the part of newitem that
248 // would be left over after adding.
249 bool itemFits(const u32 i, const ItemStack &newitem,
250 ItemStack *restitem = NULL) const;
251
252 // Checks whether there is room for a given item
253 bool roomForItem(const ItemStack &item) const;
254
255 // Checks whether the given count of the given item
256 // exists in this inventory list.
257 // If match_meta is false, only the items' names are compared.
258 bool containsItem(const ItemStack &item, bool match_meta) const;
259
260 // Removes the given count of the given item name from
261 // this inventory list. Walks the list in reverse order.
262 // If not as many items exist as requested, removes as
263 // many as possible.
264 // Returns the items that were actually removed.
265 ItemStack removeItem(const ItemStack &item);
266
267 // Takes some items from a slot.
268 // If there are not enough, takes as many as it can.
269 // Returns empty item if couldn't take any.
270 ItemStack takeItem(u32 i, u32 takecount);
271
272 // Move an item to a different list (or a different stack in the same list)
273 // count is the maximum number of items to move (0 for everything)
274 // returns the moved stack
275 ItemStack moveItem(u32 i, InventoryList *dest, u32 dest_i,
276 u32 count = 0, bool swap_if_needed = true, bool *did_swap = NULL);
277
278 // like moveItem, but without a fixed destination index
279 // also with optional rollback recording
280 void moveItemSomewhere(u32 i, InventoryList *dest, u32 count);
281
282 inline bool checkModified() const { return m_dirty; }
283 inline void setModified(bool dirty = true) { m_dirty = dirty; }
284
285 // Problem: C++ keeps references to InventoryList and ItemStack indices
286 // until a better solution is found, this serves as a guard to prevent side-effects
289 {
290 invlist->m_resize_locks -= 1;
291 }
292 };
293 using ResizeLocked = std::unique_ptr<InventoryList, ResizeUnlocker>;
294
295 void checkResizeLock();
296
298 {
299 m_resize_locks += 1;
300 return ResizeLocked(this);
301 }
302
303private:
304 std::vector<ItemStack> m_items;
305 std::string m_name;
306 u32 m_size; // always the same as m_items.size()
307 u32 m_width = 0;
309 bool m_dirty = true;
310 int m_resize_locks = 0; // Lua callback sanity
311};
312
314{
315public:
316 ~Inventory();
317
318 void clear();
319
320 Inventory(IItemDefManager *itemdef);
321 Inventory(const Inventory &other);
322 Inventory & operator = (const Inventory &other);
323 bool operator == (const Inventory &other) const;
324 bool operator != (const Inventory &other) const
325 {
326 return !(*this == other);
327 }
328
329 // Never ever serialize to disk using "incremental"!
330 void serialize(std::ostream &os, bool incremental = false) const;
331 void deSerialize(std::istream &is);
332
333 // Creates a new list if none exists or truncates existing lists
334 InventoryList * addList(const std::string &name, u32 size);
335 InventoryList * getList(const std::string &name);
336 const InventoryList * getList(const std::string &name) const;
337 const std::vector<InventoryList *> &getLists() const { return m_lists; }
338 bool deleteList(const std::string &name);
339 // A shorthand for adding items. Returns leftover item (possibly empty).
340 ItemStack addItem(const std::string &listname, const ItemStack &newitem)
341 {
342 InventoryList *list = getList(listname);
343 if(list == NULL)
344 return newitem;
345 return list->addItem(newitem);
346 }
347
348 inline bool checkModified() const
349 {
350 if (m_dirty)
351 return true;
352
353 for (const auto &list : m_lists)
354 if (list->checkModified())
355 return true;
356
357 return false;
358 }
359
360 inline void setModified(bool dirty = true)
361 {
362 m_dirty = dirty;
363 // Set all as handled
364 if (!dirty) {
365 for (const auto &list : m_lists)
366 list->setModified(dirty);
367 }
368 }
369private:
370 // -1 if not found
371 s32 getListIndex(const std::string &name) const;
372
373 std::vector<InventoryList*> m_lists;
375 bool m_dirty = true;
376};
Definition itemdef.h:129
virtual const ItemDefinition & get(const std::string &name) const =0
virtual bool isKnown(const std::string &name) const =0
Definition inventory.h:194
bool operator==(const InventoryList &other) const
Definition inventory.cpp:558
u32 getSize() const
Definition inventory.h:214
bool containsItem(const ItemStack &item, bool match_meta) const
Definition inventory.cpp:679
const ItemStack & getItem(u32 i) const
Definition inventory.h:220
const std::vector< ItemStack > & getItems() const
Definition inventory.h:231
ItemStack addItem(const ItemStack &newitem)
Definition inventory.cpp:603
std::vector< ItemStack > m_items
Definition inventory.h:304
void checkResizeLock()
Definition inventory.cpp:796
bool operator!=(const InventoryList &other) const
Definition inventory.h:208
IItemDefManager * m_itemdef
Definition inventory.h:308
InventoryList(const InventoryList &other)
Definition inventory.h:205
int m_resize_locks
Definition inventory.h:310
bool m_dirty
Definition inventory.h:309
void setName(const std::string &name)
Definition inventory.cpp:460
void setWidth(u32 newWidth)
Definition inventory.cpp:454
bool checkModified() const
Definition inventory.h:282
ItemStack removeItem(const ItemStack &item)
Definition inventory.cpp:698
std::string m_name
Definition inventory.h:305
u32 m_size
Definition inventory.h:306
void deSerialize(std::istream &is)
Definition inventory.cpp:488
u32 getUsedSlots() const
Definition inventory.cpp:573
InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef)
Definition inventory.cpp:422
u32 getWidth() const
Definition inventory.h:215
ItemStack & getItem(u32 i)
Definition inventory.h:225
ItemStack moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count=0, bool swap_if_needed=true, bool *did_swap=NULL)
Definition inventory.cpp:752
ItemStack changeItem(u32 i, const ItemStack &newitem)
Definition inventory.cpp:583
ResizeLocked resizeLock()
Definition inventory.h:297
void setModified(bool dirty=true)
Definition inventory.h:283
void setSize(u32 newsize)
Definition inventory.cpp:441
InventoryList & operator=(const InventoryList &other)
Definition inventory.cpp:544
void clearItems()
Definition inventory.cpp:430
~InventoryList()=default
void serialize(std::ostream &os, bool incremental) const
Definition inventory.cpp:466
void deleteItem(u32 i)
Definition inventory.cpp:596
const std::string & getName() const
Definition inventory.h:213
bool roomForItem(const ItemStack &item) const
Definition inventory.cpp:666
u32 m_width
Definition inventory.h:307
ItemStack takeItem(u32 i, u32 takecount)
Definition inventory.cpp:718
std::unique_ptr< InventoryList, ResizeUnlocker > ResizeLocked
Definition inventory.h:293
bool itemFits(const u32 i, const ItemStack &newitem, ItemStack *restitem=NULL) const
Definition inventory.cpp:653
void moveItemSomewhere(u32 i, InventoryList *dest, u32 count)
Definition inventory.cpp:729
Definition inventory.h:314
ItemStack addItem(const std::string &listname, const ItemStack &newitem)
Definition inventory.h:340
void clear()
Definition inventory.cpp:814
Inventory(IItemDefManager *itemdef)
Definition inventory.cpp:829
InventoryList * getList(const std::string &name)
Definition inventory.cpp:982
bool deleteList(const std::string &name)
Definition inventory.cpp:990
InventoryList * addList(const std::string &name, u32 size)
Definition inventory.cpp:956
bool operator==(const Inventory &other) const
Definition inventory.cpp:855
bool checkModified() const
Definition inventory.h:348
IItemDefManager * m_itemdef
Definition inventory.h:374
~Inventory()
Definition inventory.cpp:809
const std::vector< InventoryList * > & getLists() const
Definition inventory.h:337
void setModified(bool dirty=true)
Definition inventory.h:360
s32 getListIndex(const std::string &name) const
Definition inventory.cpp:1012
void serialize(std::ostream &os, bool incremental=false) const
Definition inventory.cpp:868
bool m_dirty
Definition inventory.h:375
void deSerialize(std::istream &is)
Definition inventory.cpp:883
Inventory & operator=(const Inventory &other)
Definition inventory.cpp:840
std::vector< InventoryList * > m_lists
Definition inventory.h:373
bool operator!=(const Inventory &other) const
Definition inventory.h:324
Definition itemstackmetadata.h:16
const ToolCapabilities & getToolCapabilities(const ToolCapabilities &default_caps) const
Definition itemstackmetadata.h:29
const std::optional< WearBarParams > & getWearBarParamOverride() const
Definition itemstackmetadata.h:38
void clear() override
Definition itemstackmetadata.cpp:23
core::vector3df v3f
Definition irr_v3d.h:11
@ ITEM_TOOL
Definition itemdef.h:38
Definition inventory.h:287
void operator()(InventoryList *invlist)
Definition inventory.h:288
Definition itemdef.h:66
u16 stack_max
Definition itemdef.h:89
ToolCapabilities * tool_capabilities
Definition itemdef.h:95
std::optional< WearBarParams > wear_bar_params
Definition itemdef.h:97
Definition inventory.h:19
void add(u16 n)
Definition inventory.h:62
std::string getWieldImage(const IItemDefManager *itemdef) const
Definition inventory.cpp:277
u16 freeSpace(const IItemDefManager *itemdef) const
Definition inventory.h:82
std::string getInventoryOverlay(const IItemDefManager *itemdef) const
Definition inventory.cpp:268
u16 count
Definition inventory.h:188
ItemStack peekItem(u32 peekcount) const
Definition inventory.cpp:407
void clear()
Definition inventory.h:54
ItemStack takeItem(u32 takecount)
Definition inventory.cpp:387
bool operator!=(const ItemStack &s) const
Definition inventory.h:179
void serialize(std::ostream &os, bool serialize_meta=true) const
Definition inventory.cpp:43
std::string getShortDescription(const IItemDefManager *itemdef) const
Definition inventory.cpp:245
std::string getWieldOverlay(const IItemDefManager *itemdef) const
Definition inventory.cpp:286
bool addWear(s32 amount, const IItemDefManager *itemdef)
Definition inventory.h:130
std::string getItemString(bool include_meta=true) const
Definition inventory.cpp:230
bool isKnown(const IItemDefManager *itemdef) const
Definition inventory.h:91
bool itemFits(ItemStack newitem, ItemStack *restitem, IItemDefManager *itemdef) const
Definition inventory.cpp:341
const ItemDefinition & getDefinition(const IItemDefManager *itemdef) const
Definition inventory.h:98
std::string getDescription(const IItemDefManager *itemdef) const
Definition inventory.cpp:237
ItemStackMetadata metadata
Definition inventory.h:190
std::string getInventoryImage(const IItemDefManager *itemdef) const
Definition inventory.cpp:259
ItemStack()=default
~ItemStack()=default
bool empty() const
Definition inventory.h:49
std::string name
Definition inventory.h:187
bool stacksWith(const ItemStack &other) const
Definition inventory.cpp:380
u16 getStackMax(const IItemDefManager *itemdef) const
Definition inventory.h:76
u16 wear
Definition inventory.h:189
void deSerialize(std::istream &is, IItemDefManager *itemdef=NULL)
Definition inventory.cpp:71
bool operator==(const ItemStack &s) const
Definition inventory.h:171
const ToolCapabilities & getToolCapabilities(const IItemDefManager *itemdef) const
Definition inventory.h:105
const std::optional< WearBarParams > & getWearBarParams(const IItemDefManager *itemdef) const
Definition inventory.h:119
v3f getWieldScale(const IItemDefManager *itemdef) const
Definition inventory.cpp:295
void remove(u16 n)
Definition inventory.h:67
ItemStack addItem(ItemStack newitem, IItemDefManager *itemdef)
Definition inventory.cpp:304
Definition tool.h:46