inventory.h 8.2 KB

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