X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Finventory.cpp;h=82325997ee1cbc1151f84e354b9ea971927bb799;hb=1b3e4e173624bb2523d4386aeef6987709d9b022;hp=25f524150191d164606b8dfefa8918acc660f2d6;hpb=8f7785771b9e02b1a1daf7a252550d78ea93053d;p=minetest.git diff --git a/src/inventory.cpp b/src/inventory.cpp index 25f524150..82325997e 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -35,11 +35,9 @@ with this program; if not, write to the Free Software Foundation, Inc., static content_t content_translate_from_19_to_internal(content_t c_from) { - for(u32 i=0; igetAlias(name); @@ -136,7 +134,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) NameIdMapping legacy_nimap; content_mapnode_get_name_id_mapping(&legacy_nimap); legacy_nimap.getName(material, name); - if(name == "") + if(name.empty()) name = "unknown_block"; if (itemdef) name = itemdef->getAlias(name); @@ -207,21 +205,20 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) // Read the count std::string count_str; std::getline(is, count_str, ' '); - if(count_str.empty()) - { + if (count_str.empty()) { count = 1; break; } - else - count = stoi(count_str); + + count = stoi(count_str); // Read the wear std::string wear_str; std::getline(is, wear_str, ' '); if(wear_str.empty()) break; - else - wear = stoi(wear_str); + + wear = stoi(wear_str); // Read metadata metadata.deSerialize(is); @@ -254,11 +251,8 @@ std::string ItemStack::getItemString() const } -ItemStack ItemStack::addItem(const ItemStack &newitem_, - IItemDefManager *itemdef) +ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef) { - ItemStack newitem = newitem_; - // If the item is empty or the position invalid, bail out if(newitem.empty()) { @@ -267,8 +261,17 @@ ItemStack ItemStack::addItem(const ItemStack &newitem_, // If this is an empty item, it's an easy job. else if(empty()) { + const u16 stackMax = newitem.getStackMax(itemdef); + *this = newitem; - newitem.clear(); + + // If the item fits fully, delete it + if (count <= stackMax) { + newitem.clear(); + } else { // Else the item does not fit fully. Return the rest. + count = stackMax; + newitem.remove(count); + } } // If item name or metadata differs, bail out else if (name != newitem.name @@ -294,11 +297,10 @@ ItemStack ItemStack::addItem(const ItemStack &newitem_, return newitem; } -bool ItemStack::itemFits(const ItemStack &newitem_, +bool ItemStack::itemFits(ItemStack newitem, ItemStack *restitem, IItemDefManager *itemdef) const { - ItemStack newitem = newitem_; // If the item is empty or the position invalid, bail out if(newitem.empty()) @@ -308,7 +310,14 @@ bool ItemStack::itemFits(const ItemStack &newitem_, // If this is an empty item, it's an easy job. else if(empty()) { - newitem.clear(); + const u16 stackMax = newitem.getStackMax(itemdef); + + // If the item fits fully, delete it + if (newitem.count <= stackMax) { + newitem.clear(); + } else { // Else the item does not fit fully. Return the rest. + newitem.remove(stackMax); + } } // If item name or metadata differs, bail out else if (name != newitem.name @@ -322,7 +331,6 @@ bool ItemStack::itemFits(const ItemStack &newitem_, newitem.clear(); } // Else the item does not fit fully. Return the rest. - // the rest. else { u16 freespace = freeSpace(itemdef); @@ -377,17 +385,12 @@ InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager clearItems(); } -InventoryList::~InventoryList() -{ -} - void InventoryList::clearItems() { m_items.clear(); - for(u32 i=0; i> m_width; if (iss.fail()) throw SerializationError("incorrect width property"); @@ -540,9 +537,8 @@ u32 InventoryList::getWidth() const u32 InventoryList::getUsedSlots() const { u32 num = 0; - for(u32 i=0; i::const_reverse_iterator - i = m_items.rbegin(); - i != m_items.rend(); ++i) - { - if(count == 0) + + for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) { + if (count == 0) break; - if(i->name == item.name) - { - if(i->count >= count) + if (i->name == item.name && (!match_meta || (i->metadata == item.metadata))) { + if (i->count >= count) return true; - else - count -= i->count; + + count -= i->count; } } return false; @@ -683,15 +676,11 @@ bool InventoryList::containsItem(const ItemStack &item) const ItemStack InventoryList::removeItem(const ItemStack &item) { ItemStack removed; - for(std::vector::reverse_iterator - i = m_items.rbegin(); - i != m_items.rend(); ++i) - { - if(i->name == item.name) - { + for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) { + if (i->name == item.name) { u32 still_to_remove = item.count - removed.count; removed.addItem(i->takeItem(still_to_remove), m_itemdef); - if(removed.count == item.count) + if (removed.count == item.count) break; } } @@ -804,9 +793,8 @@ Inventory::~Inventory() void Inventory::clear() { m_dirty = true; - for(u32 i=0; igetSize(); j++) - { + for (InventoryList *list : m_lists) { + for (u32 j=0; jgetSize(); j++) { list->deleteItem(j); } } @@ -844,9 +829,8 @@ Inventory & Inventory::operator = (const Inventory &other) m_dirty = true; clear(); m_itemdef = other.m_itemdef; - for(u32 i=0; igetName()<<" "<getSize()<<"\n"; list->serialize(os); } @@ -891,17 +873,16 @@ void Inventory::deSerialize(std::istream &is) std::string name; std::getline(iss, name, ' '); - if(name == "EndInventory") - { + if (name == "EndInventory") { break; } + // This is a temporary backwards compatibility fix - else if(name == "end") - { + if (name == "end") { break; } - else if(name == "List") - { + + if (name == "List") { std::string listname; u32 listsize; @@ -933,15 +914,14 @@ InventoryList * Inventory::addList(const std::string &name, u32 size) } return m_lists[i]; } - else - { - //don't create list with invalid name - if (name.find(" ") != std::string::npos) return NULL; - InventoryList *list = new InventoryList(name, size, m_itemdef); - m_lists.push_back(list); - return list; - } + + //don't create list with invalid name + if (name.find(' ') != std::string::npos) return NULL; + + InventoryList *list = new InventoryList(name, size, m_itemdef); + m_lists.push_back(list); + return list; } InventoryList * Inventory::getList(const std::string &name) @@ -955,9 +935,7 @@ InventoryList * Inventory::getList(const std::string &name) std::vector Inventory::getLists() { std::vector lists; - for(u32 i=0; i