X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Finventory.cpp;h=fb2dd245f396039f0c29ad818328dabbd8289c56;hb=1e94a7feaff940494a474ee549d4f2dc61f6a869;hp=3d6707f60684b22a7209bc56456fc7bb00f7571b;hpb=f8c3743991a6897c7133bf35dc2699b8b5f9df7c;p=minetest.git diff --git a/src/inventory.cpp b/src/inventory.cpp index 3d6707f60..fb2dd245f 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -1,32 +1,33 @@ /* -Minetest-c55 -Copyright (C) 2010-2011 celeron55, Perttu Ahola +Minetest +Copyright (C) 2010-2013 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your option) any 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 General Public License for more details. +GNU Lesser General Public License for more details. -You should have received a copy of the GNU General Public License along +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. */ #include "inventory.h" #include "serialization.h" -#include "utility.h" #include "debug.h" #include #include "log.h" #include "itemdef.h" -#include "strfnd.h" +#include "util/strfnd.h" #include "content_mapnode.h" // For loading legacy MaterialItems #include "nameidmapping.h" // For loading legacy MaterialItems +#include "util/serialize.h" +#include "util/string.h" /* ItemStack @@ -34,100 +35,29 @@ 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; i= 0x7f || s[i] == ' ' || s[i] == '\"') - return serializeJsonString(s); - } - return s; -} - -// Parses a string serialized by serializeJsonStringIfNeeded. -static std::string deSerializeJsonStringIfNeeded(std::istream &is) -{ - std::ostringstream tmp_os; - bool expect_initial_quote = true; - bool is_json = false; - bool was_backslash = false; - for(;;) - { - char c = is.get(); - if(is.eof()) - break; - if(expect_initial_quote && c == '"') - { - tmp_os << c; - is_json = true; - } - else if(is_json) - { - tmp_os << c; - if(was_backslash) - was_backslash = false; - else if(c == '\\') - was_backslash = true; - else if(c == '"') - break; // Found end of string - } - else - { - if(c == ' ') - { - // Found end of word - is.unget(); - break; - } - else - { - tmp_os << c; - } - } - expect_initial_quote = false; - } - if(is_json) - { - std::istringstream tmp_is(tmp_os.str(), std::ios::binary); - return deSerializeJsonString(tmp_is); - } - else - return tmp_os.str(); -} - - -ItemStack::ItemStack(std::string name_, u16 count_, - u16 wear_, std::string metadata_, - IItemDefManager *itemdef) +ItemStack::ItemStack(const std::string &name_, u16 count_, + u16 wear_, IItemDefManager *itemdef) : + name(itemdef->getAlias(name_)), + count(count_), + wear(wear_) { - name = itemdef->getAlias(name_); - count = count_; - wear = wear_; - metadata = metadata_; - - if(name.empty() || count == 0) + if (name.empty() || count == 0) clear(); - else if(itemdef->get(name).type == ITEM_TOOL) + else if (itemdef->get(name).type == ITEM_TOOL) count = 1; } void ItemStack::serialize(std::ostream &os) const { - DSTACK(__FUNCTION_NAME); - - if(empty()) + if (empty()) return; // Check how many parts of the itemstring are needed @@ -136,7 +66,7 @@ void ItemStack::serialize(std::ostream &os) const parts = 2; if(wear != 0) parts = 3; - if(metadata != "") + if (!metadata.empty()) parts = 4; os<= 3) os<<" "<= 4) - os<<" "<= 4) { + os << " "; + metadata.serialize(os); + } } void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) { - DSTACK(__FUNCTION_NAME); - clear(); // Read name @@ -162,7 +92,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) std::getline(is, tmp, ' '); if(!tmp.empty()) throw SerializationError("Unexpected text after item name"); - + if(name == "MaterialItem") { // Obsoleted on 2011-07-30 @@ -174,15 +104,16 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) // Convert old materials if(material <= 0xff) material = content_translate_from_19_to_internal(material); - if(material > MAX_CONTENT) + if(material > 0xfff) throw SerializationError("Too large material number"); // Convert old id to name NameIdMapping legacy_nimap; content_mapnode_get_name_id_mapping(&legacy_nimap); legacy_nimap.getName(material, name); - if(name == "") + if(name.empty()) name = "unknown_block"; - name = itemdef->getAlias(name); + if (itemdef) + name = itemdef->getAlias(name); count = materialcount; } else if(name == "MaterialItem2") @@ -193,15 +124,16 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) is>>material; u16 materialcount; is>>materialcount; - if(material > MAX_CONTENT) + if(material > 0xfff) throw SerializationError("Too large material number"); // Convert old id to name NameIdMapping legacy_nimap; content_mapnode_get_name_id_mapping(&legacy_nimap); legacy_nimap.getName(material, name); - if(name == "") + if(name.empty()) name = "unknown_block"; - name = itemdef->getAlias(name); + if (itemdef) + name = itemdef->getAlias(name); count = materialcount; } else if(name == "node" || name == "NodeItem" || name == "MaterialItem3" @@ -215,14 +147,15 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) Strfnd fnd(all); fnd.next("\""); // If didn't skip to end, we have ""s - if(!fnd.atend()){ + if(!fnd.at_end()){ name = fnd.next("\""); } else { // No luck, just read a word then fnd.start(all); name = fnd.next(" "); } fnd.skip_over(" "); - name = itemdef->getAlias(name); + if (itemdef) + name = itemdef->getAlias(name); count = stoi(trim(fnd.next(""))); if(count == 0) count = 1; @@ -242,7 +175,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) Strfnd fnd(all); fnd.next("\""); // If didn't skip to end, we have ""s - if(!fnd.atend()){ + if(!fnd.at_end()){ name = fnd.next("\""); } else { // No luck, just read a word then fnd.start(all); @@ -251,7 +184,8 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) count = 1; // Then read wear fnd.skip_over(" "); - name = itemdef->getAlias(name); + if (itemdef) + name = itemdef->getAlias(name); wear = stoi(trim(fnd.next(""))); } else @@ -261,29 +195,29 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) // The real thing // Apply item aliases - name = itemdef->getAlias(name); + if (itemdef) + name = itemdef->getAlias(name); // 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 = deSerializeJsonStringIfNeeded(is); + metadata.deSerialize(is); // In case fields are added after metadata, skip space here: //std::getline(is, tmp, ' '); @@ -293,9 +227,9 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) } while(false); } - if(name.empty() || count == 0) + if (name.empty() || count == 0) clear(); - else if(itemdef->get(name).type == ITEM_TOOL) + else if (itemdef && itemdef->get(name).type == ITEM_TOOL) count = 1; } @@ -307,17 +241,14 @@ void ItemStack::deSerialize(const std::string &str, IItemDefManager *itemdef) std::string ItemStack::getItemString() const { - // Get item string std::ostringstream os(std::ios::binary); serialize(os); return os.str(); } -ItemStack ItemStack::addItem(const ItemStack &newitem_, - IItemDefManager *itemdef) -{ - ItemStack newitem = newitem_; +ItemStack ItemStack::addItem(ItemStack newitem, IItemDefManager *itemdef) +{ // If the item is empty or the position invalid, bail out if(newitem.empty()) { @@ -326,11 +257,21 @@ 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 differs, bail out - else if(name != newitem.name) + // If item name or metadata differs, bail out + else if (name != newitem.name + || metadata != newitem.metadata) { // cannot be added } @@ -352,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()) @@ -366,10 +306,18 @@ 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 differs, bail out - else if(name != newitem.name) + // If item name or metadata differs, bail out + else if (name != newitem.name + || metadata != newitem.metadata) { // cannot be added } @@ -379,7 +327,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); @@ -426,26 +373,20 @@ ItemStack ItemStack::peekItem(u32 peekcount) const Inventory */ -InventoryList::InventoryList(std::string name, u32 size, IItemDefManager *itemdef) +InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager *itemdef): + m_name(name), + m_size(size), + m_itemdef(itemdef) { - m_name = name; - m_size = size; - m_itemdef = itemdef; clearItems(); - //m_dirty = false; -} - -InventoryList::~InventoryList() -{ } void InventoryList::clearItems() { m_items.clear(); - for(u32 i=0; i> m_width; + if (iss.fail()) + throw SerializationError("incorrect width property"); + } else if(name == "Item") { if(item_i > getSize() - 1) @@ -521,10 +475,6 @@ void InventoryList::deSerialize(std::istream &is) throw SerializationError("too many items"); m_items[item_i++].clear(); } - else - { - throw SerializationError("Unknown inventory identifier"); - } } } @@ -537,6 +487,7 @@ InventoryList & InventoryList::operator = (const InventoryList &other) { m_items = other.m_items; m_size = other.m_size; + m_width = other.m_width; m_name = other.m_name; m_itemdef = other.m_itemdef; //setDirty(true); @@ -544,6 +495,26 @@ InventoryList & InventoryList::operator = (const InventoryList &other) return *this; } +bool InventoryList::operator == (const InventoryList &other) const +{ + if(m_size != other.m_size) + return false; + if(m_width != other.m_width) + 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) - { - 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; @@ -700,15 +672,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; } } @@ -726,18 +694,46 @@ ItemStack InventoryList::takeItem(u32 i, u32 takecount) return taken; } -ItemStack InventoryList::peekItem(u32 i, u32 peekcount) const +void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count) { - if(i >= m_items.size()) - return ItemStack(); + // Take item from source list + ItemStack item1; + if (count == 0) + item1 = changeItem(i, ItemStack()); + else + item1 = takeItem(i, 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; + } + } + + // 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; + } + } - return m_items[i].peekItem(peekcount); + // If we reach this, the item was not fully added + // Add the remaining part back to the source item + addItem(i, item1); } -void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count) +u32 InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, + u32 count, bool swap_if_needed, bool *did_swap) { if(this == dest && i == dest_i) - return; + return count; // Take item from source list ItemStack item1; @@ -747,7 +743,7 @@ void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count) item1 = takeItem(i, count); if(item1.empty()) - return; + return 0; // Try to add the item to destination list u32 oldcount = item1.count; @@ -765,8 +761,11 @@ void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count) // If olditem is returned, nothing was added. // Swap the items - if(nothing_added) - { + if (nothing_added && swap_if_needed) { + // Tell that we swapped + if (did_swap != NULL) { + *did_swap = true; + } // Take item from source list item1 = changeItem(i, ItemStack()); // Adding was not possible, swap the items. @@ -775,6 +774,7 @@ void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count) changeItem(i, item2); } } + return (oldcount - item1.count); } /* @@ -788,20 +788,18 @@ Inventory::~Inventory() void Inventory::clear() { - for(u32 i=0; igetSize(); j++) - { + m_dirty = true; + for (InventoryList *list : m_lists) { + for (u32 j=0; jgetSize(); j++) { list->deleteItem(j); } } @@ -809,12 +807,14 @@ void Inventory::clearContents() Inventory::Inventory(IItemDefManager *itemdef) { + m_dirty = false; m_itemdef = itemdef; } Inventory::Inventory(const Inventory &other) { *this = other; + m_dirty = false; } Inventory & Inventory::operator = (const Inventory &other) @@ -822,21 +822,32 @@ 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); } @@ -858,17 +869,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; @@ -882,13 +892,14 @@ void Inventory::deSerialize(std::istream &is) } else { - throw SerializationError("Unknown inventory identifier"); + throw SerializationError("invalid inventory specifier: " + name); } } } InventoryList * Inventory::addList(const std::string &name, u32 size) { + m_dirty = true; s32 i = getListIndex(name); if(i != -1) { @@ -899,12 +910,14 @@ InventoryList * Inventory::addList(const std::string &name, u32 size) } return m_lists[i]; } - else - { - 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) @@ -915,11 +928,21 @@ InventoryList * Inventory::getList(const std::string &name) return m_lists[i]; } +std::vector Inventory::getLists() +{ + std::vector lists; + for (auto list : m_lists) { + lists.push_back(list); + } + return lists; +} + bool Inventory::deleteList(const std::string &name) { s32 i = getListIndex(name); if(i == -1) return false; + m_dirty = true; delete m_lists[i]; m_lists.erase(m_lists.begin() + i); return true;