inventory.h 11 KB

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