3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
23 #include "irrlichttypes.h"
24 #include "itemstackmetadata.h"
31 struct ToolCapabilities;
35 ItemStack() = default;
37 ItemStack(const std::string &name_, u16 count_,
38 u16 wear, IItemDefManager *itemdef);
40 ~ItemStack() = default;
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);
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;
78 assert(count >= n); // Pre-condition
81 clear(); // reset name, wear and metadata too
84 // Maximum size of a stack
85 u16 getStackMax(IItemDefManager *itemdef) const
87 return itemdef->get(name).stack_max;
90 // Number of items that can be added to this stack
91 u16 freeSpace(IItemDefManager *itemdef) const
93 u16 max = getStackMax(itemdef);
99 // Returns false if item is not known and cannot be used
100 bool isKnown(IItemDefManager *itemdef) const
102 return itemdef->isKnown(name);
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
110 return itemdef->get(name);
113 // Get tool digging properties, or those of the hand if not a tool
114 const ToolCapabilities& getToolCapabilities(
115 IItemDefManager *itemdef) const
117 const ToolCapabilities *item_cap =
118 itemdef->get(name).tool_capabilities;
120 if (item_cap == NULL)
121 // Fall back to the hand's tool capabilities
122 item_cap = itemdef->get("").tool_capabilities;
124 assert(item_cap != NULL);
125 return metadata.getToolCapabilities(*item_cap); // Check for override
128 // Wear out (only tools)
129 // Returns true if the item is (was) a tool
130 bool addWear(s32 amount, IItemDefManager *itemdef)
132 if(getDefinition(itemdef).type == ITEM_TOOL)
134 if(amount > 65535 - wear)
136 else if(amount < -wear)
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);
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;
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);
164 // Similar to takeItem, but keeps this ItemStack intact.
165 ItemStack peekItem(u32 peekcount) const;
167 bool operator ==(const ItemStack &s) const
169 return (this->name == s.name &&
170 this->count == s.count &&
171 this->wear == s.wear &&
172 this->metadata == s.metadata);
175 bool operator !=(const ItemStack &s) const
177 return !(*this == s);
183 std::string name = "";
186 ItemStackMetadata metadata;
192 InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
193 ~InventoryList() = default;
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);
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
206 return !(*this == other);
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; }
213 u32 getUsedSlots() const;
215 // Get reference to item
216 const ItemStack &getItem(u32 i) const
218 assert(i < m_size); // Pre-condition
221 ItemStack &getItem(u32 i)
223 assert(i < m_size); // Pre-condition
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);
231 void deleteItem(u32 i);
233 // Adds an item to a suitable place. Returns leftover item (possibly empty).
234 ItemStack addItem(const ItemStack &newitem);
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);
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;
248 // Checks whether there is room for a given item
249 bool roomForItem(const ItemStack &item) const;
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;
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
260 // Returns the items that were actually removed.
261 ItemStack removeItem(const ItemStack &item);
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);
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);
274 // like moveItem, but without a fixed destination index
275 // also with optional rollback recording
276 void moveItemSomewhere(u32 i, InventoryList *dest, u32 count);
278 inline bool checkModified() const { return m_dirty; }
279 inline void setModified(bool dirty = true) { m_dirty = dirty; }
282 std::vector<ItemStack> m_items;
284 u32 m_size; // always the same as m_items.size()
286 IItemDefManager *m_itemdef;
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
303 return !(*this == other);
306 // Never ever serialize to disk using "incremental"!
307 void serialize(std::ostream &os, bool incremental = false) const;
308 void deSerialize(std::istream &is);
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)
319 InventoryList *list = getList(listname);
322 return list->addItem(newitem);
325 inline bool checkModified() const
330 for (const auto &list : m_lists)
331 if (list->checkModified())
337 inline void setModified(bool dirty = true)
340 // Set all as handled
342 for (const auto &list : m_lists)
343 list->setModified(dirty);
348 s32 getListIndex(const std::string &name) const;
350 std::vector<InventoryList*> m_lists;
351 IItemDefManager *m_itemdef;