inventory.h 9.1 KB

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