inventory.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3.0 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef INVENTORY_HEADER
  17. #define INVENTORY_HEADER
  18. #include "debug.h"
  19. #include "itemdef.h"
  20. #include "irrlichttypes.h"
  21. #include "itemstackmetadata.h"
  22. #include <istream>
  23. #include <ostream>
  24. #include <string>
  25. #include <vector>
  26. struct ToolCapabilities;
  27. struct ItemStack
  28. {
  29. ItemStack(): name(""), count(0), wear(0) {}
  30. ItemStack(const std::string &name_, u16 count_,
  31. u16 wear, IItemDefManager *itemdef);
  32. ~ItemStack() {}
  33. // Serialization
  34. void serialize(std::ostream &os) const;
  35. // Deserialization. Pass itemdef unless you don't want aliases resolved.
  36. void deSerialize(std::istream &is, IItemDefManager *itemdef = NULL);
  37. void deSerialize(const std::string &s, IItemDefManager *itemdef = NULL);
  38. // Returns the string used for inventory
  39. std::string getItemString() const;
  40. /*
  41. Quantity methods
  42. */
  43. bool empty() const
  44. {
  45. return count == 0;
  46. }
  47. void clear()
  48. {
  49. name = "";
  50. count = 0;
  51. wear = 0;
  52. metadata.clear();
  53. }
  54. void add(u16 n)
  55. {
  56. count += n;
  57. }
  58. void remove(u16 n)
  59. {
  60. assert(count >= n); // Pre-condition
  61. count -= n;
  62. if(count == 0)
  63. clear(); // reset name, wear and metadata too
  64. }
  65. // Maximum size of a stack
  66. u16 getStackMax(IItemDefManager *itemdef) const
  67. {
  68. return itemdef->get(name).stack_max;
  69. }
  70. // Number of items that can be added to this stack
  71. u16 freeSpace(IItemDefManager *itemdef) const
  72. {
  73. u16 max = getStackMax(itemdef);
  74. if (count >= max)
  75. return 0;
  76. return max - count;
  77. }
  78. // Returns false if item is not known and cannot be used
  79. bool isKnown(IItemDefManager *itemdef) const
  80. {
  81. return itemdef->isKnown(name);
  82. }
  83. // Returns a pointer to the item definition struct,
  84. // or a fallback one (name="unknown") if the item is unknown.
  85. const ItemDefinition& getDefinition(
  86. IItemDefManager *itemdef) const
  87. {
  88. return itemdef->get(name);
  89. }
  90. // Get tool digging properties, or those of the hand if not a tool
  91. const ToolCapabilities& getToolCapabilities(
  92. IItemDefManager *itemdef) const
  93. {
  94. ToolCapabilities *cap;
  95. cap = itemdef->get(name).tool_capabilities;
  96. if(cap == NULL)
  97. cap = itemdef->get("").tool_capabilities;
  98. assert(cap != NULL);
  99. return *cap;
  100. }
  101. // Wear out (only tools)
  102. // Returns true if the item is (was) a tool
  103. bool addWear(s32 amount, IItemDefManager *itemdef)
  104. {
  105. if(getDefinition(itemdef).type == ITEM_TOOL)
  106. {
  107. if(amount > 65535 - wear)
  108. clear();
  109. else if(amount < -wear)
  110. wear = 0;
  111. else
  112. wear += amount;
  113. return true;
  114. }
  115. else
  116. {
  117. return false;
  118. }
  119. }
  120. // If possible, adds newitem to this item.
  121. // If cannot be added at all, returns the item back.
  122. // If can be added partly, decremented item is returned back.
  123. // If can be added fully, empty item is returned.
  124. ItemStack addItem(const ItemStack &newitem,
  125. IItemDefManager *itemdef);
  126. // Checks whether newitem could be added.
  127. // If restitem is non-NULL, it receives the part of newitem that
  128. // would be left over after adding.
  129. bool itemFits(const ItemStack &newitem,
  130. ItemStack *restitem, // may be NULL
  131. IItemDefManager *itemdef) const;
  132. // Takes some items.
  133. // If there are not enough, takes as many as it can.
  134. // Returns empty item if couldn't take any.
  135. ItemStack takeItem(u32 takecount);
  136. // Similar to takeItem, but keeps this ItemStack intact.
  137. ItemStack peekItem(u32 peekcount) const;
  138. /*
  139. Properties
  140. */
  141. std::string name;
  142. u16 count;
  143. u16 wear;
  144. ItemStackMetadata metadata;
  145. };
  146. class InventoryList
  147. {
  148. public:
  149. InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef);
  150. ~InventoryList();
  151. void clearItems();
  152. void setSize(u32 newsize);
  153. void setWidth(u32 newWidth);
  154. void setName(const std::string &name);
  155. void serialize(std::ostream &os) const;
  156. void deSerialize(std::istream &is);
  157. InventoryList(const InventoryList &other);
  158. InventoryList & operator = (const InventoryList &other);
  159. bool operator == (const InventoryList &other) const;
  160. bool operator != (const InventoryList &other) const
  161. {
  162. return !(*this == other);
  163. }
  164. const std::string &getName() const;
  165. u32 getSize() const;
  166. u32 getWidth() const;
  167. // Count used slots
  168. u32 getUsedSlots() const;
  169. u32 getFreeSlots() const;
  170. // Get reference to item
  171. const ItemStack& getItem(u32 i) const;
  172. ItemStack& getItem(u32 i);
  173. // Returns old item. Parameter can be an empty item.
  174. ItemStack changeItem(u32 i, const ItemStack &newitem);
  175. // Delete item
  176. void deleteItem(u32 i);
  177. // Adds an item to a suitable place. Returns leftover item (possibly empty).
  178. ItemStack addItem(const ItemStack &newitem);
  179. // If possible, adds item to given slot.
  180. // If cannot be added at all, returns the item back.
  181. // If can be added partly, decremented item is returned back.
  182. // If can be added fully, empty item is returned.
  183. ItemStack addItem(u32 i, const ItemStack &newitem);
  184. // Checks whether the item could be added to the given slot
  185. // If restitem is non-NULL, it receives the part of newitem that
  186. // would be left over after adding.
  187. bool itemFits(const u32 i, const ItemStack &newitem,
  188. ItemStack *restitem = NULL) const;
  189. // Checks whether there is room for a given item
  190. bool roomForItem(const ItemStack &item) const;
  191. // Checks whether the given count of the given item
  192. // exists in this inventory list.
  193. // If match_meta is false, only the items' names are compared.
  194. bool containsItem(const ItemStack &item, bool match_meta) const;
  195. // Removes the given count of the given item name from
  196. // this inventory list. Walks the list in reverse order.
  197. // If not as many items exist as requested, removes as
  198. // many as possible.
  199. // Returns the items that were actually removed.
  200. ItemStack removeItem(const ItemStack &item);
  201. // Takes some items from a slot.
  202. // If there are not enough, takes as many as it can.
  203. // Returns empty item if couldn't take any.
  204. ItemStack takeItem(u32 i, u32 takecount);
  205. // Move an item to a different list (or a different stack in the same list)
  206. // count is the maximum number of items to move (0 for everything)
  207. // returns number of moved items
  208. u32 moveItem(u32 i, InventoryList *dest, u32 dest_i,
  209. u32 count = 0, bool swap_if_needed = true, bool *did_swap = NULL);
  210. // like moveItem, but without a fixed destination index
  211. // also with optional rollback recording
  212. void moveItemSomewhere(u32 i, InventoryList *dest, u32 count);
  213. private:
  214. std::vector<ItemStack> m_items;
  215. std::string m_name;
  216. u32 m_size, m_width;
  217. IItemDefManager *m_itemdef;
  218. };
  219. class Inventory
  220. {
  221. public:
  222. ~Inventory();
  223. void clear();
  224. void clearContents();
  225. Inventory(IItemDefManager *itemdef);
  226. Inventory(const Inventory &other);
  227. Inventory & operator = (const Inventory &other);
  228. bool operator == (const Inventory &other) const;
  229. bool operator != (const Inventory &other) const
  230. {
  231. return !(*this == other);
  232. }
  233. void serialize(std::ostream &os) const;
  234. void deSerialize(std::istream &is);
  235. InventoryList * addList(const std::string &name, u32 size);
  236. InventoryList * getList(const std::string &name);
  237. const InventoryList * getList(const std::string &name) const;
  238. std::vector<const InventoryList*> getLists();
  239. bool deleteList(const std::string &name);
  240. // A shorthand for adding items. Returns leftover item (possibly empty).
  241. ItemStack addItem(const std::string &listname, const ItemStack &newitem)
  242. {
  243. m_dirty = true;
  244. InventoryList *list = getList(listname);
  245. if(list == NULL)
  246. return newitem;
  247. return list->addItem(newitem);
  248. }
  249. bool checkModified() const
  250. {
  251. return m_dirty;
  252. }
  253. void setModified(const bool x)
  254. {
  255. m_dirty = x;
  256. }
  257. private:
  258. // -1 if not found
  259. const s32 getListIndex(const std::string &name) const;
  260. std::vector<InventoryList*> m_lists;
  261. IItemDefManager *m_itemdef;
  262. bool m_dirty;
  263. };
  264. #endif