diff options
Diffstat (limited to 'src/inventory.h')
-rw-r--r-- | src/inventory.h | 91 |
1 files changed, 57 insertions, 34 deletions
diff --git a/src/inventory.h b/src/inventory.h index c87e7ecba..67a7859ed 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -34,8 +34,8 @@ struct ItemStack { ItemStack() = default; - ItemStack(const std::string &name_, u16 count_, u16 wear, - IItemDefManager *itemdef); + ItemStack(const std::string &name_, u16 count_, + u16 wear, IItemDefManager *itemdef); ~ItemStack() = default; @@ -54,7 +54,10 @@ struct ItemStack Quantity methods */ - bool empty() const { return count == 0; } + bool empty() const + { + return count == 0; + } void clear() { @@ -64,13 +67,16 @@ struct ItemStack metadata.clear(); } - void add(u16 n) { count += n; } + void add(u16 n) + { + count += n; + } void remove(u16 n) { assert(count >= n); // Pre-condition count -= n; - if (count == 0) + if(count == 0) clear(); // reset name, wear and metadata too } @@ -90,19 +96,25 @@ struct ItemStack } // Returns false if item is not known and cannot be used - bool isKnown(IItemDefManager *itemdef) const { return itemdef->isKnown(name); } + bool isKnown(IItemDefManager *itemdef) const + { + return itemdef->isKnown(name); + } // Returns a pointer to the item definition struct, // or a fallback one (name="unknown") if the item is unknown. - const ItemDefinition &getDefinition(IItemDefManager *itemdef) const + const ItemDefinition& getDefinition( + IItemDefManager *itemdef) const { return itemdef->get(name); } // Get tool digging properties, or those of the hand if not a tool - const ToolCapabilities &getToolCapabilities(IItemDefManager *itemdef) const + const ToolCapabilities& getToolCapabilities( + IItemDefManager *itemdef) const { - const ToolCapabilities *item_cap = itemdef->get(name).tool_capabilities; + const ToolCapabilities *item_cap = + itemdef->get(name).tool_capabilities; if (item_cap == NULL) // Fall back to the hand's tool capabilities @@ -116,10 +128,11 @@ struct ItemStack // Returns true if the item is (was) a tool bool addWear(s32 amount, IItemDefManager *itemdef) { - if (getDefinition(itemdef).type == ITEM_TOOL) { - if (amount > 65535 - wear) + if(getDefinition(itemdef).type == ITEM_TOOL) + { + if(amount > 65535 - wear) clear(); - else if (amount < -wear) + else if(amount < -wear) wear = 0; else wear += amount; @@ -139,7 +152,7 @@ struct ItemStack // If restitem is non-NULL, it receives the part of newitem that // would be left over after adding. bool itemFits(ItemStack newitem, - ItemStack *restitem, // may be NULL + ItemStack *restitem, // may be NULL IItemDefManager *itemdef) const; // Takes some items. @@ -150,13 +163,18 @@ struct ItemStack // Similar to takeItem, but keeps this ItemStack intact. ItemStack peekItem(u32 peekcount) const; - bool operator==(const ItemStack &s) const + bool operator ==(const ItemStack &s) const { - return (this->name == s.name && this->count == s.count && - this->wear == s.wear && this->metadata == s.metadata); + return (this->name == s.name && + this->count == s.count && + this->wear == s.wear && + this->metadata == s.metadata); } - bool operator!=(const ItemStack &s) const { return !(*this == s); } + bool operator !=(const ItemStack &s) const + { + return !(*this == s); + } /* Properties @@ -180,9 +198,12 @@ public: void deSerialize(std::istream &is); InventoryList(const InventoryList &other); - InventoryList &operator=(const InventoryList &other); - bool operator==(const InventoryList &other) const; - bool operator!=(const InventoryList &other) const { return !(*this == other); } + InventoryList & operator = (const InventoryList &other); + bool operator == (const InventoryList &other) const; + bool operator != (const InventoryList &other) const + { + return !(*this == other); + } const std::string &getName() const; u32 getSize() const; @@ -192,8 +213,8 @@ public: u32 getFreeSlots() const; // Get reference to item - const ItemStack &getItem(u32 i) const; - ItemStack &getItem(u32 i); + const ItemStack& getItem(u32 i) const; + ItemStack& getItem(u32 i); // Returns old item. Parameter can be an empty item. ItemStack changeItem(u32 i, const ItemStack &newitem); // Delete item @@ -237,8 +258,8 @@ public: // Move an item to a different list (or a different stack in the same list) // count is the maximum number of items to move (0 for everything) // returns number of moved items - u32 moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count = 0, - bool swap_if_needed = true, bool *did_swap = NULL); + u32 moveItem(u32 i, InventoryList *dest, u32 dest_i, + u32 count = 0, bool swap_if_needed = true, bool *did_swap = NULL); // like moveItem, but without a fixed destination index // also with optional rollback recording @@ -265,24 +286,27 @@ public: Inventory(IItemDefManager *itemdef); Inventory(const Inventory &other); - Inventory &operator=(const Inventory &other); - bool operator==(const Inventory &other) const; - bool operator!=(const Inventory &other) const { return !(*this == other); } + Inventory & operator = (const Inventory &other); + bool operator == (const Inventory &other) const; + bool operator != (const Inventory &other) const + { + return !(*this == other); + } // Never ever serialize to disk using "incremental"! void serialize(std::ostream &os, bool incremental = false) const; void deSerialize(std::istream &is); - InventoryList *addList(const std::string &name, u32 size); - InventoryList *getList(const std::string &name); - const InventoryList *getList(const std::string &name) const; - std::vector<const InventoryList *> getLists(); + InventoryList * addList(const std::string &name, u32 size); + InventoryList * getList(const std::string &name); + const InventoryList * getList(const std::string &name) const; + std::vector<const InventoryList*> getLists(); bool deleteList(const std::string &name); // A shorthand for adding items. Returns leftover item (possibly empty). ItemStack addItem(const std::string &listname, const ItemStack &newitem) { InventoryList *list = getList(listname); - if (list == NULL) + if(list == NULL) return newitem; return list->addItem(newitem); } @@ -308,12 +332,11 @@ public: list->setModified(dirty); } } - private: // -1 if not found const s32 getListIndex(const std::string &name) const; - std::vector<InventoryList *> m_lists; + std::vector<InventoryList*> m_lists; IItemDefManager *m_itemdef; bool m_dirty = true; }; |