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;
36 ItemStack(const std::string &name_, u16 count_,
37 u16 wear, IItemDefManager *itemdef);
42 void serialize(std::ostream &os) const;
43 // Deserialization. Pass itemdef unless you don't want aliases resolved.
44 void deSerialize(std::istream &is, IItemDefManager *itemdef = NULL);
45 void deSerialize(const std::string &s, IItemDefManager *itemdef = NULL);
47 // Returns the string used for inventory
48 std::string getItemString() const;
74 assert(count >= n); // Pre-condition
77 clear(); // reset name, wear and metadata too
80 // Maximum size of a stack
81 u16 getStackMax(IItemDefManager *itemdef) const
83 return itemdef->get(name).stack_max;
86 // Number of items that can be added to this stack
87 u16 freeSpace(IItemDefManager *itemdef) const
89 u16 max = getStackMax(itemdef);
95 // Returns false if item is not known and cannot be used
96 bool isKnown(IItemDefManager *itemdef) const
98 return itemdef->isKnown(name);
101 // Returns a pointer to the item definition struct,
102 // or a fallback one (name="unknown") if the item is unknown.
103 const ItemDefinition& getDefinition(
104 IItemDefManager *itemdef) const
106 return itemdef->get(name);
109 // Get tool digging properties, or those of the hand if not a tool
110 const ToolCapabilities& getToolCapabilities(
111 IItemDefManager *itemdef) const
113 ToolCapabilities *cap;
114 cap = itemdef->get(name).tool_capabilities;
116 cap = itemdef->get("").tool_capabilities;
121 // Wear out (only tools)
122 // Returns true if the item is (was) a tool
123 bool addWear(s32 amount, IItemDefManager *itemdef)
125 if(getDefinition(itemdef).type == ITEM_TOOL)
127 if(amount > 65535 - wear)
129 else if(amount < -wear)
141 // If possible, adds newitem to this item.
142 // If cannot be added at all, returns the item back.
143 // If can be added partly, decremented item is returned back.
144 // If can be added fully, empty item is returned.
145 ItemStack addItem(ItemStack newitem, IItemDefManager *itemdef);
147 // Checks whether newitem could be added.
148 // If restitem is non-NULL, it receives the part of newitem that
149 // would be left over after adding.
150 bool itemFits(ItemStack newitem,
151 ItemStack *restitem, // may be NULL
152 IItemDefManager *itemdef) const;
155 // If there are not enough, takes as many as it can.
156 // Returns empty item if couldn't take any.
157 ItemStack takeItem(u32 takecount);
159 // Similar to takeItem, but keeps this ItemStack intact.
160 ItemStack peekItem(u32 peekcount) const;
165 std::string name = "";
168 ItemStackMetadata metadata;
174 InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
175 ~InventoryList() = default;
177 void setSize(u32 newsize);
178 void setWidth(u32 newWidth);
179 void setName(const std::string &name);
180 void serialize(std::ostream &os) const;
181 void deSerialize(std::istream &is);
183 InventoryList(const InventoryList &other);
184 InventoryList & operator = (const InventoryList &other);
185 bool operator == (const InventoryList &other) const;
186 bool operator != (const InventoryList &other) const
188 return !(*this == other);
191 const std::string &getName() const;
193 u32 getWidth() const;
195 u32 getUsedSlots() const;
196 u32 getFreeSlots() const;
198 // Get reference to item
199 const ItemStack& getItem(u32 i) const;
200 ItemStack& getItem(u32 i);
201 // Returns old item. Parameter can be an empty item.
202 ItemStack changeItem(u32 i, const ItemStack &newitem);
204 void deleteItem(u32 i);
206 // Adds an item to a suitable place. Returns leftover item (possibly empty).
207 ItemStack addItem(const ItemStack &newitem);
209 // If possible, adds item to given slot.
210 // If cannot be added at all, returns the item back.
211 // If can be added partly, decremented item is returned back.
212 // If can be added fully, empty item is returned.
213 ItemStack addItem(u32 i, const ItemStack &newitem);
215 // Checks whether the item could be added to the given slot
216 // If restitem is non-NULL, it receives the part of newitem that
217 // would be left over after adding.
218 bool itemFits(const u32 i, const ItemStack &newitem,
219 ItemStack *restitem = NULL) const;
221 // Checks whether there is room for a given item
222 bool roomForItem(const ItemStack &item) const;
224 // Checks whether the given count of the given item
225 // exists in this inventory list.
226 // If match_meta is false, only the items' names are compared.
227 bool containsItem(const ItemStack &item, bool match_meta) const;
229 // Removes the given count of the given item name from
230 // this inventory list. Walks the list in reverse order.
231 // If not as many items exist as requested, removes as
233 // Returns the items that were actually removed.
234 ItemStack removeItem(const ItemStack &item);
236 // Takes some items from a slot.
237 // If there are not enough, takes as many as it can.
238 // Returns empty item if couldn't take any.
239 ItemStack takeItem(u32 i, u32 takecount);
241 // Move an item to a different list (or a different stack in the same list)
242 // count is the maximum number of items to move (0 for everything)
243 // returns number of moved items
244 u32 moveItem(u32 i, InventoryList *dest, u32 dest_i,
245 u32 count = 0, bool swap_if_needed = true, bool *did_swap = NULL);
247 // like moveItem, but without a fixed destination index
248 // also with optional rollback recording
249 void moveItemSomewhere(u32 i, InventoryList *dest, u32 count);
252 std::vector<ItemStack> m_items;
256 IItemDefManager *m_itemdef;
265 void clearContents();
267 Inventory(IItemDefManager *itemdef);
268 Inventory(const Inventory &other);
269 Inventory & operator = (const Inventory &other);
270 bool operator == (const Inventory &other) const;
271 bool operator != (const Inventory &other) const
273 return !(*this == other);
276 void serialize(std::ostream &os) const;
277 void deSerialize(std::istream &is);
279 InventoryList * addList(const std::string &name, u32 size);
280 InventoryList * getList(const std::string &name);
281 const InventoryList * getList(const std::string &name) const;
282 std::vector<const InventoryList*> getLists();
283 bool deleteList(const std::string &name);
284 // A shorthand for adding items. Returns leftover item (possibly empty).
285 ItemStack addItem(const std::string &listname, const ItemStack &newitem)
288 InventoryList *list = getList(listname);
291 return list->addItem(newitem);
294 bool checkModified() const
299 void setModified(const bool x)
306 const s32 getListIndex(const std::string &name) const;
308 std::vector<InventoryList*> m_lists;
309 IItemDefManager *m_itemdef;
310 bool m_dirty = false;