]> git.lizzy.rs Git - dragonfireclient.git/blob - src/inventory.h
Fix interlaced 3d mode second image being flipped on compiling against irrlicht ...
[dragonfireclient.git] / src / inventory.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef INVENTORY_HEADER
21 #define INVENTORY_HEADER
22
23 #include <iostream>
24 #include <string>
25 #include <vector>
26 #include "irrlichttypes.h"
27 #include "debug.h"
28 #include "itemdef.h"
29
30 struct ToolCapabilities;
31
32 struct ItemStack
33 {
34         ItemStack(): name(""), count(0), wear(0), metadata("") {}
35         ItemStack(std::string name_, u16 count_,
36                         u16 wear, std::string metadata_,
37                         IItemDefManager *itemdef);
38         ~ItemStack() {}
39
40         // Serialization
41         void serialize(std::ostream &os) const;
42         void deSerialize(std::istream &is, IItemDefManager *itemdef);
43         void deSerialize(const std::string &s, IItemDefManager *itemdef);
44
45         // Returns the string used for inventory
46         std::string getItemString() const;
47
48         /*
49                 Quantity methods
50         */
51
52         bool empty() const
53         {
54                 return count == 0;
55         }
56
57         void clear()
58         {
59                 name = "";
60                 count = 0;
61                 wear = 0;
62                 metadata = "";
63         }
64
65         void add(u16 n)
66         {
67                 count += n;
68         }
69
70         void remove(u16 n)
71         {
72                 assert(count >= n);
73                 count -= n;
74                 if(count == 0)
75                         clear(); // reset name, wear and metadata too
76         }
77
78         // Maximum size of a stack
79         u16 getStackMax(IItemDefManager *itemdef) const
80         {
81                 s16 max = itemdef->get(name).stack_max;
82                 return (max >= 0) ? max : 0;
83         }
84
85         // Number of items that can be added to this stack
86         u16 freeSpace(IItemDefManager *itemdef) const
87         {
88                 u16 max = getStackMax(itemdef);
89                 if(count > max)
90                         return 0;
91                 return max - count;
92         }
93
94         // Returns false if item is not known and cannot be used
95         bool isKnown(IItemDefManager *itemdef) const
96         {
97                 return itemdef->isKnown(name);
98         }
99
100         // Returns a pointer to the item definition struct,
101         // or a fallback one (name="unknown") if the item is unknown.
102         const ItemDefinition& getDefinition(
103                         IItemDefManager *itemdef) const
104         {
105                 return itemdef->get(name);
106         }
107
108         // Get tool digging properties, or those of the hand if not a tool
109         const ToolCapabilities& getToolCapabilities(
110                         IItemDefManager *itemdef) const
111         {
112                 ToolCapabilities *cap;
113                 cap = itemdef->get(name).tool_capabilities;
114                 if(cap == NULL)
115                         cap = itemdef->get("").tool_capabilities;
116                 assert(cap != NULL);
117                 return *cap;
118         }
119
120         // Wear out (only tools)
121         // Returns true if the item is (was) a tool
122         bool addWear(s32 amount, IItemDefManager *itemdef)
123         {
124                 if(getDefinition(itemdef).type == ITEM_TOOL)
125                 {
126                         if(amount > 65535 - wear)
127                                 clear();
128                         else if(amount < -wear)
129                                 wear = 0;
130                         else
131                                 wear += amount;
132                         return true;
133                 }
134                 else
135                 {
136                         return false;
137                 }
138         }
139
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(const ItemStack &newitem,
145                         IItemDefManager *itemdef);
146
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(const ItemStack &newitem,
151                         ItemStack *restitem,  // may be NULL
152                         IItemDefManager *itemdef) const;
153
154         // Takes some items.
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);
158
159         // Similar to takeItem, but keeps this ItemStack intact.
160         ItemStack peekItem(u32 peekcount) const;
161
162         /*
163                 Properties
164         */
165         std::string name;
166         u16 count;
167         u16 wear;
168         std::string metadata;
169 };
170
171 class InventoryList
172 {
173 public:
174         InventoryList(std::string name, u32 size, IItemDefManager *itemdef);
175         ~InventoryList();
176         void clearItems();
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);
182
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
187         {
188                 return !(*this == other);
189         }
190
191         const std::string &getName() const;
192         u32 getSize() const;
193         u32 getWidth() const;
194         // Count used slots
195         u32 getUsedSlots() const;
196         u32 getFreeSlots() const;
197
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);
203         // Delete item
204         void deleteItem(u32 i);
205
206         // Adds an item to a suitable place. Returns leftover item (possibly empty).
207         ItemStack addItem(const ItemStack &newitem);
208
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);
214
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;
220
221         // Checks whether there is room for a given item
222         bool roomForItem(const ItemStack &item) const;
223
224         // Checks whether the given count of the given item name
225         // exists in this inventory list.
226         bool containsItem(const ItemStack &item) const;
227
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
231         // many as possible.
232         // Returns the items that were actually removed.
233         ItemStack removeItem(const ItemStack &item);
234
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);
239
240         // Similar to takeItem, but keeps the slot intact.
241         ItemStack peekItem(u32 i, u32 peekcount) const;
242
243         // Move an item to a different list (or a different stack in the same list)
244         // count is the maximum number of items to move (0 for everything)
245         void moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count = 0);
246
247 private:
248         std::vector<ItemStack> m_items;
249         u32 m_size, m_width;
250         std::string m_name;
251         IItemDefManager *m_itemdef;
252 };
253
254 class Inventory
255 {
256 public:
257         ~Inventory();
258
259         void clear();
260         void clearContents();
261
262         Inventory(IItemDefManager *itemdef);
263         Inventory(const Inventory &other);
264         Inventory & operator = (const Inventory &other);
265         bool operator == (const Inventory &other) const;
266         bool operator != (const Inventory &other) const
267         {
268                 return !(*this == other);
269         }
270
271         void serialize(std::ostream &os) const;
272         void deSerialize(std::istream &is);
273
274         InventoryList * addList(const std::string &name, u32 size);
275         InventoryList * getList(const std::string &name);
276         const InventoryList * getList(const std::string &name) const;
277         std::vector<const InventoryList*> getLists();
278         bool deleteList(const std::string &name);
279         // A shorthand for adding items. Returns leftover item (possibly empty).
280         ItemStack addItem(const std::string &listname, const ItemStack &newitem)
281         {
282                 InventoryList *list = getList(listname);
283                 if(list == NULL)
284                         return newitem;
285                 return list->addItem(newitem);
286         }
287         
288 private:
289         // -1 if not found
290         const s32 getListIndex(const std::string &name) const;
291
292         std::vector<InventoryList*> m_lists;
293         IItemDefManager *m_itemdef;
294 };
295
296 #endif
297