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;
75 assert(count >= n); // Pre-condition
78 clear(); // reset name, wear and metadata too
81 // Maximum size of a stack
82 u16 getStackMax(IItemDefManager *itemdef) const
84 return itemdef->get(name).stack_max;
87 // Number of items that can be added to this stack
88 u16 freeSpace(IItemDefManager *itemdef) const
90 u16 max = getStackMax(itemdef);
96 // Returns false if item is not known and cannot be used
97 bool isKnown(IItemDefManager *itemdef) const
99 return itemdef->isKnown(name);
102 // Returns a pointer to the item definition struct,
103 // or a fallback one (name="unknown") if the item is unknown.
104 const ItemDefinition& getDefinition(
105 IItemDefManager *itemdef) const
107 return itemdef->get(name);
110 // Get tool digging properties, or those of the hand if not a tool
111 const ToolCapabilities& getToolCapabilities(
112 IItemDefManager *itemdef) const
114 ToolCapabilities *cap;
115 cap = itemdef->get(name).tool_capabilities;
117 cap = itemdef->get("").tool_capabilities;
122 // Wear out (only tools)
123 // Returns true if the item is (was) a tool
124 bool addWear(s32 amount, IItemDefManager *itemdef)
126 if(getDefinition(itemdef).type == ITEM_TOOL)
128 if(amount > 65535 - wear)
130 else if(amount < -wear)
140 // If possible, adds newitem to this item.
141 // If cannot be added at all, returns the item back.
142 // If can be added partly, decremented item is returned back.
143 // If can be added fully, empty item is returned.
144 ItemStack addItem(ItemStack newitem, IItemDefManager *itemdef);
146 // Checks whether newitem could be added.
147 // If restitem is non-NULL, it receives the part of newitem that
148 // would be left over after adding.
149 bool itemFits(ItemStack newitem,
150 ItemStack *restitem, // may be NULL
151 IItemDefManager *itemdef) const;
154 // If there are not enough, takes as many as it can.
155 // Returns empty item if couldn't take any.
156 ItemStack takeItem(u32 takecount);
158 // Similar to takeItem, but keeps this ItemStack intact.
159 ItemStack peekItem(u32 peekcount) const;
164 std::string name = "";
167 ItemStackMetadata metadata;
173 InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
174 ~InventoryList() = default;
176 void setSize(u32 newsize);
177 void setWidth(u32 newWidth);
178 void setName(const std::string &name);
179 void serialize(std::ostream &os) const;
180 void deSerialize(std::istream &is);
182 InventoryList(const InventoryList &other);
183 InventoryList & operator = (const InventoryList &other);
184 bool operator == (const InventoryList &other) const;
185 bool operator != (const InventoryList &other) const
187 return !(*this == other);
190 const std::string &getName() const;
192 u32 getWidth() const;
194 u32 getUsedSlots() const;
195 u32 getFreeSlots() const;
197 // Get reference to item
198 const ItemStack& getItem(u32 i) const;
199 ItemStack& getItem(u32 i);
200 // Returns old item. Parameter can be an empty item.
201 ItemStack changeItem(u32 i, const ItemStack &newitem);
203 void deleteItem(u32 i);
205 // Adds an item to a suitable place. Returns leftover item (possibly empty).
206 ItemStack addItem(const ItemStack &newitem);
208 // If possible, adds item to given slot.
209 // If cannot be added at all, returns the item back.
210 // If can be added partly, decremented item is returned back.
211 // If can be added fully, empty item is returned.
212 ItemStack addItem(u32 i, const ItemStack &newitem);
214 // Checks whether the item could be added to the given slot
215 // If restitem is non-NULL, it receives the part of newitem that
216 // would be left over after adding.
217 bool itemFits(const u32 i, const ItemStack &newitem,
218 ItemStack *restitem = NULL) const;
220 // Checks whether there is room for a given item
221 bool roomForItem(const ItemStack &item) const;
223 // Checks whether the given count of the given item
224 // exists in this inventory list.
225 // If match_meta is false, only the items' names are compared.
226 bool containsItem(const ItemStack &item, bool match_meta) const;
228 // Removes the given count of the given item name from
229 // this inventory list. Walks the list in reverse order.
230 // If not as many items exist as requested, removes as
232 // Returns the items that were actually removed.
233 ItemStack removeItem(const ItemStack &item);
235 // Takes some items from a slot.
236 // If there are not enough, takes as many as it can.
237 // Returns empty item if couldn't take any.
238 ItemStack takeItem(u32 i, u32 takecount);
240 // Move an item to a different list (or a different stack in the same list)
241 // count is the maximum number of items to move (0 for everything)
242 // returns number of moved items
243 u32 moveItem(u32 i, InventoryList *dest, u32 dest_i,
244 u32 count = 0, bool swap_if_needed = true, bool *did_swap = NULL);
246 // like moveItem, but without a fixed destination index
247 // also with optional rollback recording
248 void moveItemSomewhere(u32 i, InventoryList *dest, u32 count);
251 std::vector<ItemStack> m_items;
255 IItemDefManager *m_itemdef;
264 void clearContents();
266 Inventory(IItemDefManager *itemdef);
267 Inventory(const Inventory &other);
268 Inventory & operator = (const Inventory &other);
269 bool operator == (const Inventory &other) const;
270 bool operator != (const Inventory &other) const
272 return !(*this == other);
275 void serialize(std::ostream &os) const;
276 void deSerialize(std::istream &is);
278 InventoryList * addList(const std::string &name, u32 size);
279 InventoryList * getList(const std::string &name);
280 const InventoryList * getList(const std::string &name) const;
281 std::vector<const InventoryList*> getLists();
282 bool deleteList(const std::string &name);
283 // A shorthand for adding items. Returns leftover item (possibly empty).
284 ItemStack addItem(const std::string &listname, const ItemStack &newitem)
287 InventoryList *list = getList(listname);
290 return list->addItem(newitem);
293 bool checkModified() const
298 void setModified(const bool x)
305 const s32 getListIndex(const std::string &name) const;
307 std::vector<InventoryList*> m_lists;
308 IItemDefManager *m_itemdef;
309 bool m_dirty = false;