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) 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() const;
50 // Returns the tooltip
51 std::string getDescription(IItemDefManager *itemdef) const;
77 assert(count >= n); // Pre-condition
80 clear(); // reset name, wear and metadata too
83 // Maximum size of a stack
84 u16 getStackMax(IItemDefManager *itemdef) const
86 return itemdef->get(name).stack_max;
89 // Number of items that can be added to this stack
90 u16 freeSpace(IItemDefManager *itemdef) const
92 u16 max = getStackMax(itemdef);
98 // Returns false if item is not known and cannot be used
99 bool isKnown(IItemDefManager *itemdef) const
101 return itemdef->isKnown(name);
104 // Returns a pointer to the item definition struct,
105 // or a fallback one (name="unknown") if the item is unknown.
106 const ItemDefinition& getDefinition(
107 IItemDefManager *itemdef) const
109 return itemdef->get(name);
112 // Get tool digging properties, or those of the hand if not a tool
113 const ToolCapabilities& getToolCapabilities(
114 IItemDefManager *itemdef) const
116 const ToolCapabilities *item_cap =
117 itemdef->get(name).tool_capabilities;
119 if (item_cap == NULL)
120 // Fall back to the hand's tool capabilities
121 item_cap = itemdef->get("").tool_capabilities;
123 assert(item_cap != NULL);
124 return metadata.getToolCapabilities(*item_cap); // Check for override
127 // Wear out (only tools)
128 // Returns true if the item is (was) a tool
129 bool addWear(s32 amount, IItemDefManager *itemdef)
131 if(getDefinition(itemdef).type == ITEM_TOOL)
133 if(amount > 65535 - wear)
135 else if(amount < -wear)
145 // If possible, adds newitem to this item.
146 // If cannot be added at all, returns the item back.
147 // If can be added partly, decremented item is returned back.
148 // If can be added fully, empty item is returned.
149 ItemStack addItem(ItemStack newitem, IItemDefManager *itemdef);
151 // Checks whether newitem could be added.
152 // If restitem is non-NULL, it receives the part of newitem that
153 // would be left over after adding.
154 bool itemFits(ItemStack newitem,
155 ItemStack *restitem, // may be NULL
156 IItemDefManager *itemdef) const;
159 // If there are not enough, takes as many as it can.
160 // Returns empty item if couldn't take any.
161 ItemStack takeItem(u32 takecount);
163 // Similar to takeItem, but keeps this ItemStack intact.
164 ItemStack peekItem(u32 peekcount) const;
166 bool operator ==(const ItemStack &s) const
168 return (this->name == s.name &&
169 this->count == s.count &&
170 this->wear == s.wear &&
171 this->metadata == s.metadata);
174 bool operator !=(const ItemStack &s) const
176 return !(*this == s);
182 std::string name = "";
185 ItemStackMetadata metadata;
191 InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
192 ~InventoryList() = default;
194 void setSize(u32 newsize);
195 void setWidth(u32 newWidth);
196 void setName(const std::string &name);
197 void serialize(std::ostream &os, bool incremental) const;
198 void deSerialize(std::istream &is);
200 InventoryList(const InventoryList &other);
201 InventoryList & operator = (const InventoryList &other);
202 bool operator == (const InventoryList &other) const;
203 bool operator != (const InventoryList &other) const
205 return !(*this == other);
208 const std::string &getName() const;
210 u32 getWidth() const;
212 u32 getUsedSlots() const;
213 u32 getFreeSlots() const;
215 // Get reference to item
216 const ItemStack& getItem(u32 i) const;
217 ItemStack& getItem(u32 i);
218 // Returns old item. Parameter can be an empty item.
219 ItemStack changeItem(u32 i, const ItemStack &newitem);
221 void deleteItem(u32 i);
223 // Adds an item to a suitable place. Returns leftover item (possibly empty).
224 ItemStack addItem(const ItemStack &newitem);
226 // If possible, adds item to given slot.
227 // If cannot be added at all, returns the item back.
228 // If can be added partly, decremented item is returned back.
229 // If can be added fully, empty item is returned.
230 ItemStack addItem(u32 i, const ItemStack &newitem);
232 // Checks whether the item could be added to the given slot
233 // If restitem is non-NULL, it receives the part of newitem that
234 // would be left over after adding.
235 bool itemFits(const u32 i, const ItemStack &newitem,
236 ItemStack *restitem = NULL) const;
238 // Checks whether there is room for a given item
239 bool roomForItem(const ItemStack &item) const;
241 // Checks whether the given count of the given item
242 // exists in this inventory list.
243 // If match_meta is false, only the items' names are compared.
244 bool containsItem(const ItemStack &item, bool match_meta) const;
246 // Removes the given count of the given item name from
247 // this inventory list. Walks the list in reverse order.
248 // If not as many items exist as requested, removes as
250 // Returns the items that were actually removed.
251 ItemStack removeItem(const ItemStack &item);
253 // Takes some items from a slot.
254 // If there are not enough, takes as many as it can.
255 // Returns empty item if couldn't take any.
256 ItemStack takeItem(u32 i, u32 takecount);
258 // Move an item to a different list (or a different stack in the same list)
259 // count is the maximum number of items to move (0 for everything)
260 // returns number of moved items
261 u32 moveItem(u32 i, InventoryList *dest, u32 dest_i,
262 u32 count = 0, bool swap_if_needed = true, bool *did_swap = NULL);
264 // like moveItem, but without a fixed destination index
265 // also with optional rollback recording
266 void moveItemSomewhere(u32 i, InventoryList *dest, u32 count);
268 inline bool checkModified() const { return m_dirty; }
269 inline void setModified(bool dirty = true) { m_dirty = dirty; }
272 std::vector<ItemStack> m_items;
276 IItemDefManager *m_itemdef;
287 Inventory(IItemDefManager *itemdef);
288 Inventory(const Inventory &other);
289 Inventory & operator = (const Inventory &other);
290 bool operator == (const Inventory &other) const;
291 bool operator != (const Inventory &other) const
293 return !(*this == other);
296 // Never ever serialize to disk using "incremental"!
297 void serialize(std::ostream &os, bool incremental = false) const;
298 void deSerialize(std::istream &is);
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 std::vector<const InventoryList*> getLists();
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)
308 InventoryList *list = getList(listname);
311 return list->addItem(newitem);
314 inline bool checkModified() const
319 for (const auto &list : m_lists)
320 if (list->checkModified())
326 inline void setModified(bool dirty = true)
329 // Set all as handled
331 for (const auto &list : m_lists)
332 list->setModified(dirty);
337 const s32 getListIndex(const std::string &name) const;
339 std::vector<InventoryList*> m_lists;
340 IItemDefManager *m_itemdef;