X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Finventory.cpp;h=77ecf5876bf36500206ba8b7eb5697c0e7a2c033;hb=e610149c0cc3516b61115541f2f4f78344a0bb2c;hp=24eebba808c156f6d21f39484101386b0ab9196d;hpb=e6a9e6066afc369f01c046de8e3a90a4b042286c;p=dragonfireclient.git diff --git a/src/inventory.cpp b/src/inventory.cpp index 24eebba80..77ecf5876 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "inventory.h" #include "serialization.h" #include "debug.h" +#include #include #include "log.h" #include "itemdef.h" @@ -35,11 +36,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 +131,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 +202,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); @@ -253,12 +247,17 @@ std::string ItemStack::getItemString() const return os.str(); } - -ItemStack ItemStack::addItem(const ItemStack &newitem_, - IItemDefManager *itemdef) +std::string ItemStack::getDescription(IItemDefManager *itemdef) const { - ItemStack newitem = newitem_; + std::string desc = metadata.getString("description"); + if (desc.empty()) + desc = getDefinition(itemdef).description; + return desc.empty() ? name : desc; +} + +ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef) +{ // If the item is empty or the position invalid, bail out if(newitem.empty()) { @@ -267,17 +266,8 @@ ItemStack ItemStack::addItem(const ItemStack &newitem_, // If this is an empty item, it's an easy job. else if(empty()) { - const u16 stackMax = getStackMax(itemdef); - *this = newitem; - - // 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); - } + newitem.clear(); } // If item name or metadata differs, bail out else if (name != newitem.name @@ -303,11 +293,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()) @@ -317,14 +306,7 @@ bool ItemStack::itemFits(const ItemStack &newitem_, // If this is an empty item, it's an easy job. else if(empty()) { - const u16 stackMax = 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); - } + newitem.clear(); } // If item name or metadata differs, bail out else if (name != newitem.name @@ -346,6 +328,7 @@ bool ItemStack::itemFits(const ItemStack &newitem_, if(restitem) *restitem = newitem; + return newitem.empty(); } @@ -392,57 +375,55 @@ 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"); @@ -496,8 +473,18 @@ void InventoryList::deSerialize(std::istream &is) if(item_i > getSize() - 1) throw SerializationError("too many items"); m_items[item_i++].clear(); + } else if (name == "Keep") { + ++item_i; // Unmodified item } } + + // Contents given to deSerialize() were not terminated properly: throw error. + + std::ostringstream ss; + ss << "Malformatted inventory list. list=" + << m_name << ", read " << item_i << " of " << getSize() + << " ItemStacks." << std::endl; + throw SerializationError(ss.str()); } InventoryList::InventoryList(const InventoryList &other) @@ -525,14 +512,9 @@ bool InventoryList::operator == (const InventoryList &other) const return false; if(m_name != other.m_name) return false; - 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 - && (!match_meta || (i->metadata == item.metadata))) { + 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; @@ -698,18 +677,20 @@ bool InventoryList::containsItem(const ItemStack &item, bool match_meta) 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) + ItemStack leftover = removed.addItem(i->takeItem(still_to_remove), + m_itemdef); + // Allow oversized stacks + removed.count += leftover.count; + + if (removed.count == item.count) break; } } + if (!removed.empty()) + setModified(); return removed; } @@ -719,8 +700,8 @@ ItemStack InventoryList::takeItem(u32 i, u32 takecount) return ItemStack(); ItemStack taken = m_items[i].takeItem(takecount); - //if(!taken.empty()) - // setDirty(true); + if (!taken.empty()) + setModified(); return taken; } @@ -736,27 +717,13 @@ void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count) if (item1.empty()) return; - // Try to add the item to destination list - u32 dest_size = dest->getSize(); - // First try all the non-empty slots - for (u32 dest_i = 0; dest_i < dest_size; dest_i++) { - if (!m_items[dest_i].empty()) { - item1 = dest->addItem(dest_i, item1); - if (item1.empty()) return; - } - } + ItemStack leftover; + leftover = dest->addItem(item1); - // Then try all the empty ones - for (u32 dest_i = 0; dest_i < dest_size; dest_i++) { - if (m_items[dest_i].empty()) { - item1 = dest->addItem(dest_i, item1); - if (item1.empty()) return; - } + if (!leftover.empty()) { + // Add the remaining part back to the source item + addItem(i, leftover); } - - // If we reach this, the item was not fully added - // Add the remaining part back to the source item - addItem(i, item1); } u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, @@ -818,37 +785,22 @@ Inventory::~Inventory() void Inventory::clear() { - m_dirty = true; - for(u32 i=0; igetSize(); j++) - { - list->deleteItem(j); - } - } + setModified(); } Inventory::Inventory(IItemDefManager *itemdef) { - m_dirty = false; m_itemdef = itemdef; + setModified(); } Inventory::Inventory(const Inventory &other) { *this = other; - m_dirty = false; } Inventory & Inventory::operator = (const Inventory &other) @@ -856,13 +808,12 @@ Inventory & Inventory::operator = (const Inventory &other) // Gracefully handle self assignment if(this != &other) { - m_dirty = true; clear(); m_itemdef = other.m_itemdef; - for(u32 i=0; igetName()<<" "<getSize()<<"\n"; - list->serialize(os); + //std::cout << "Serialize " << (int)incremental << ", n=" << m_lists.size() << std::endl; + for (const InventoryList *list : m_lists) { + if (!incremental || list->checkModified()) { + os << "List " << list->getName() << " " << list->getSize() << "\n"; + list->serialize(os, incremental); + } else { + os << "KeepList " << list->getName() << "\n"; + } } os<<"EndInventory\n"; @@ -894,10 +848,10 @@ void Inventory::serialize(std::ostream &os) const void Inventory::deSerialize(std::istream &is) { - clear(); + std::vector new_lists; + new_lists.reserve(m_lists.size()); - for(;;) - { + while (is.good()) { std::string line; std::getline(is, line, '\n'); @@ -906,38 +860,68 @@ void Inventory::deSerialize(std::istream &is) std::string name; std::getline(iss, name, ' '); - if(name == "EndInventory") - { - break; - } - // This is a temporary backwards compatibility fix - else if(name == "end") - { - break; + if (name == "EndInventory" || name == "end") { + // Remove all lists that were not sent + for (auto &list : m_lists) { + if (std::find(new_lists.begin(), new_lists.end(), list) != new_lists.end()) + continue; + + delete list; + list = nullptr; + setModified(); + } + m_lists.erase(std::remove(m_lists.begin(), m_lists.end(), + nullptr), m_lists.end()); + return; } - else if(name == "List") - { + + if (name == "List") { std::string listname; u32 listsize; std::getline(iss, listname, ' '); iss>>listsize; - InventoryList *list = new InventoryList(listname, listsize, m_itemdef); + InventoryList *list = getList(listname); + bool create_new = !list; + if (create_new) + list = new InventoryList(listname, listsize, m_itemdef); + else + list->setSize(listsize); list->deSerialize(is); - m_lists.push_back(list); - } - else - { - throw SerializationError("invalid inventory specifier: " + name); + new_lists.push_back(list); + if (create_new) + m_lists.push_back(list); + + } else if (name == "KeepList") { + // Incrementally sent list + std::string listname; + std::getline(iss, listname, ' '); + + InventoryList *list = getList(listname); + if (list) { + new_lists.push_back(list); + } else { + errorstream << "Inventory::deSerialize(): Tried to keep list '" << + listname << "' which is non-existent." << std::endl; + } } + // Any additional fields will throw errors when received by a client + // older than PROTOCOL_VERSION 38 } + + // Contents given to deSerialize() were not terminated properly: throw error. + + std::ostringstream ss; + ss << "Malformatted inventory (damaged?). " + << m_lists.size() << " lists read." << std::endl; + throw SerializationError(ss.str()); } InventoryList * Inventory::addList(const std::string &name, u32 size) { - m_dirty = true; + setModified(); s32 i = getListIndex(name); if(i != -1) { @@ -945,18 +929,20 @@ InventoryList * Inventory::addList(const std::string &name, u32 size) { delete m_lists[i]; m_lists[i] = new InventoryList(name, size, m_itemdef); + m_lists[i]->setModified(); } 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 nullptr; + + InventoryList *list = new InventoryList(name, size, m_itemdef); + list->setModified(); + m_lists.push_back(list); + return list; } InventoryList * Inventory::getList(const std::string &name) @@ -970,9 +956,7 @@ InventoryList * Inventory::getList(const std::string &name) std::vector Inventory::getLists() { std::vector lists; - for(u32 i=0; i