aboutsummaryrefslogtreecommitdiff
path: root/src/tool.cpp
Commit message (Expand)AuthorAge
* Serialize tool capabilities JSON without whitespacesfan52021-03-20
* (se)SerializeString: Include max length in the nameSmallJoker2020-10-01
* Value copy / allocation optimizations mostly in server, SAO and serialize codesfan52020-05-27
* Punchwear (improved) (#8959)sfan52019-09-22
* Unify wield item handling (#8677)SmallJoker2019-08-07
* Allow toolcaps to override the built-in times for dig_immediatesfan52019-07-31
* Proselytize the network. Use IEEE F32 (#8030)SmallJoker2019-01-03
* Tool.cpp/.h, lua_api/l_util.cpp: Tidy up code and remove dead codeHybrid Dog2018-01-03
* Tool getDigParams: Fix selecting the best fitting timeHybrid Dog2018-01-03
* Allow overriding tool capabilities through itemstack metadataraymoo2017-10-29
* Bump minimal protocol version to 36 (#6319)SmallJoker2017-08-29
* Modernize source code: last part (#6285)Loïc Blot2017-08-20
* C++11 patchset 2: remove util/cpp11.h and util/cpp11_container.h (#5821)Loïc Blot2017-06-04
* Fix click-digging torches (#5652)you2017-04-28
* Tools: Fix tool digging speed limitHybrid Dog2017-04-19
* Update server min protocol version to v24 (#5411)Loïc Blot2017-03-19
* Make some maps unordered to improve performanceLoic Blot2016-10-05
* Change i++ to ++iDavid Jones2015-08-25
* Fix inversion of full_punch_intervalNovatux2014-07-02
* Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenuKahrl2013-08-14
* New damage system, add damageGroups to ToolCapabilities, bump protocol versionPilzAdam2013-03-29
* Update Copyright YearsSfan52013-02-24
* Change Minetest-c55 to MinetestPilzAdam2013-02-24
* Properly and efficiently use split utility headersPerttu Ahola2012-06-17
* Switch the license to be LGPLv2/later, with small parts still remaining as GP...Perttu Ahola2012-06-05
* Rework tool_capabilities a bit (maxwear->uses, scale dig time according to le...Perttu Ahola2012-03-29
* Digging and placing soundsPerttu Ahola2012-03-24
* dig_immediate=2 -> 0.5 seconds digging timePerttu Ahola2012-03-10
* dig_immediate=3 instead of 1 means 0 secondsPerttu Ahola2012-03-10
* Damage groups WIPPerttu Ahola2012-03-10
* Small fix in getHitParamsPerttu Ahola2012-03-10
* itemgroup.h and ItemGroupList typedefPerttu Ahola2012-03-10
* Tune group digging time stuff a bitPerttu Ahola2012-03-10
* Digging time groups WIPPerttu Ahola2012-03-10
* GameDef compilesPerttu Ahola2011-11-29
* Create framework for getting rid of global definitions of node/tool/item/what...Perttu Ahola2011-11-29
* Move tool stuff to tool.{h,cpp}Perttu Ahola2011-11-29
later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #pragma once #include "itemdef.h" #include "irrlichttypes.h" #include "itemstackmetadata.h" #include <istream> #include <ostream> #include <string> #include <vector> #include <cassert> struct ToolCapabilities; struct ItemStack { ItemStack() = default; ItemStack(const std::string &name_, u16 count_, u16 wear, IItemDefManager *itemdef); ~ItemStack() = default; // Serialization void serialize(std::ostream &os) const; // Deserialization. Pass itemdef unless you don't want aliases resolved. void deSerialize(std::istream &is, IItemDefManager *itemdef = NULL); void deSerialize(const std::string &s, IItemDefManager *itemdef = NULL); // Returns the string used for inventory std::string getItemString() const; /* Quantity methods */ bool empty() const { return count == 0; } void clear() { name = ""; count = 0; wear = 0; metadata.clear(); } void add(u16 n) { count += n; } void remove(u16 n) { assert(count >= n); // Pre-condition count -= n; if(count == 0) clear(); // reset name, wear and metadata too } // Maximum size of a stack u16 getStackMax(IItemDefManager *itemdef) const { return itemdef->get(name).stack_max; } // Number of items that can be added to this stack u16 freeSpace(IItemDefManager *itemdef) const { u16 max = getStackMax(itemdef); if (count >= max) return 0; return max - count; } // Returns false if item is not known and cannot be used 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 { 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 *item_cap = itemdef->get(name).tool_capabilities; if (item_cap == NULL) // Fall back to the hand's tool capabilities item_cap = itemdef->get("").tool_capabilities; assert(item_cap != NULL); return metadata.getToolCapabilities(*item_cap); // Check for override } // Wear out (only tools) // 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) clear(); else if(amount < -wear) wear = 0; else wear += amount; return true; } return false; } // If possible, adds newitem to this item. // If cannot be added at all, returns the item back. // If can be added partly, decremented item is returned back. // If can be added fully, empty item is returned. ItemStack addItem(ItemStack newitem, IItemDefManager *itemdef); // Checks whether newitem could be added. // 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 IItemDefManager *itemdef) const; // Takes some items. // If there are not enough, takes as many as it can. // Returns empty item if couldn't take any. ItemStack takeItem(u32 takecount); // Similar to takeItem, but keeps this ItemStack intact. ItemStack peekItem(u32 peekcount) const; /* Properties */ std::string name = ""; u16 count = 0; u16 wear = 0; ItemStackMetadata metadata; }; class InventoryList { public: InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef); ~InventoryList() = default; void clearItems(); void setSize(u32 newsize); void setWidth(u32 newWidth); void setName(const std::string &name); void serialize(std::ostream &os) const; 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); } const std::string &getName() const; u32 getSize() const; u32 getWidth() const; // Count used slots u32 getUsedSlots() const; u32 getFreeSlots() const; // Get reference to item 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 void deleteItem(u32 i); // Adds an item to a suitable place. Returns leftover item (possibly empty). ItemStack addItem(const ItemStack &newitem); // If possible, adds item to given slot. // If cannot be added at all, returns the item back. // If can be added partly, decremented item is returned back. // If can be added fully, empty item is returned. ItemStack addItem(u32 i, const ItemStack &newitem); // Checks whether the item could be added to the given slot // If restitem is non-NULL, it receives the part of newitem that // would be left over after adding. bool itemFits(const u32 i, const ItemStack &newitem, ItemStack *restitem = NULL) const; // Checks whether there is room for a given item bool roomForItem(const ItemStack &item) const; // Checks whether the given count of the given item // exists in this inventory list. // If match_meta is false, only the items' names are compared. bool containsItem(const ItemStack &item, bool match_meta) const; // Removes the given count of the given item name from // this inventory list. Walks the list in reverse order. // If not as many items exist as requested, removes as // many as possible. // Returns the items that were actually removed. ItemStack removeItem(const ItemStack &item); // Takes some items from a slot. // If there are not enough, takes as many as it can. // Returns empty item if couldn't take any. ItemStack takeItem(u32 i, u32 takecount); // 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); // like moveItem, but without a fixed destination index // also with optional rollback recording void moveItemSomewhere(u32 i, InventoryList *dest, u32 count); private: std::vector<ItemStack> m_items; std::string m_name; u32 m_size; u32 m_width = 0; IItemDefManager *m_itemdef; }; class Inventory { public: ~Inventory(); void clear(); void clearContents(); 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); } void serialize(std::ostream &os) 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(); 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) { m_dirty = true; InventoryList *list = getList(listname); if(list == NULL) return newitem; return list->addItem(newitem); } bool checkModified() const { return m_dirty; } void setModified(const bool x) { m_dirty = x; } private: // -1 if not found const s32 getListIndex(const std::string &name) const; std::vector<InventoryList*> m_lists; IItemDefManager *m_itemdef; bool m_dirty = false; };