3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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.
20 #ifndef INVENTORY_HEADER
21 #define INVENTORY_HEADER
27 #include "common_irrlicht.h"
31 struct ToolDiggingProperties;
35 ItemStack(): name(""), count(0), wear(0), metadata("") {}
36 ItemStack(std::string name_, u16 count_,
37 u16 wear, std::string metadata_,
38 IItemDefManager *itemdef);
42 void serialize(std::ostream &os) const;
43 void deSerialize(std::istream &is, IItemDefManager *itemdef);
44 void deSerialize(const std::string &s, IItemDefManager *itemdef);
46 // Returns the string used for inventory
47 std::string getItemString() const;
76 clear(); // reset name, wear and metadata too
79 // Maximum size of a stack
80 u16 getStackMax(IItemDefManager *itemdef) const
82 s16 max = itemdef->get(name).stack_max;
83 return (max >= 0) ? max : 0;
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 ToolDiggingProperties& getToolDiggingProperties(
111 IItemDefManager *itemdef) const
113 ToolDiggingProperties *prop;
114 prop = itemdef->get(name).tool_digging_properties;
116 prop = itemdef->get("").tool_digging_properties;
117 assert(prop != NULL);
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(const ItemStack &newitem,
146 IItemDefManager *itemdef);
148 // Checks whether newitem could be added.
149 // If restitem is non-NULL, it receives the part of newitem that
150 // would be left over after adding.
151 bool itemFits(const ItemStack &newitem,
152 ItemStack *restitem, // may be NULL
153 IItemDefManager *itemdef) const;
156 // If there are not enough, takes as many as it can.
157 // Returns empty item if couldn't take any.
158 ItemStack takeItem(u32 takecount);
160 // Similar to takeItem, but keeps this ItemStack intact.
161 ItemStack peekItem(u32 peekcount) const;
169 std::string metadata;
175 InventoryList(std::string name, u32 size, IItemDefManager *itemdef);
178 void setSize(u32 newsize);
179 void serialize(std::ostream &os) const;
180 void deSerialize(std::istream &is);
182 InventoryList(const InventoryList &other);
183 InventoryList & operator = (const InventoryList &other);
185 const std::string &getName() const;
188 u32 getUsedSlots() const;
189 u32 getFreeSlots() const;
191 // Get reference to item
192 const ItemStack& getItem(u32 i) const;
193 ItemStack& getItem(u32 i);
194 // Returns old item. Parameter can be an empty item.
195 ItemStack changeItem(u32 i, const ItemStack &newitem);
197 void deleteItem(u32 i);
199 // Adds an item to a suitable place. Returns leftover item (possibly empty).
200 ItemStack addItem(const ItemStack &newitem);
202 // If possible, adds item to given slot.
203 // If cannot be added at all, returns the item back.
204 // If can be added partly, decremented item is returned back.
205 // If can be added fully, empty item is returned.
206 ItemStack addItem(u32 i, const ItemStack &newitem);
208 // Checks whether the item could be added to the given slot
209 // If restitem is non-NULL, it receives the part of newitem that
210 // would be left over after adding.
211 bool itemFits(const u32 i, const ItemStack &newitem,
212 ItemStack *restitem = NULL) const;
214 // Checks whether there is room for a given item
215 bool roomForItem(const ItemStack &item) const;
217 // Checks whether the given count of the given item name
218 // exists in this inventory list.
219 bool containsItem(const ItemStack &item) const;
221 // Removes the given count of the given item name from
222 // this inventory list. Walks the list in reverse order.
223 // If not as many items exist as requested, removes as
225 // Returns the items that were actually removed.
226 ItemStack removeItem(const ItemStack &item);
228 // Takes some items from a slot.
229 // If there are not enough, takes as many as it can.
230 // Returns empty item if couldn't take any.
231 ItemStack takeItem(u32 i, u32 takecount);
233 // Similar to takeItem, but keeps the slot intact.
234 ItemStack peekItem(u32 i, u32 peekcount) const;
237 std::vector<ItemStack> m_items;
240 IItemDefManager *m_itemdef;
250 Inventory(IItemDefManager *itemdef);
251 Inventory(const Inventory &other);
252 Inventory & operator = (const Inventory &other);
254 void serialize(std::ostream &os) const;
255 void deSerialize(std::istream &is);
257 InventoryList * addList(const std::string &name, u32 size);
258 InventoryList * getList(const std::string &name);
259 const InventoryList * getList(const std::string &name) const;
260 bool deleteList(const std::string &name);
261 // A shorthand for adding items. Returns leftover item (possibly empty).
262 ItemStack addItem(const std::string &listname, const ItemStack &newitem)
264 InventoryList *list = getList(listname);
267 return list->addItem(newitem);
272 const s32 getListIndex(const std::string &name) const;
274 std::vector<InventoryList*> m_lists;
275 IItemDefManager *m_itemdef;