X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Finventory.cpp;h=cb8faecbc4eba27fd360e848832a0b2682a93ac2;hb=d1df09841d0eac7a88f638676b80ec848522cca5;hp=f4a87bec12d3587cda5f6bc74ca8e8bac0ab12f1;hpb=90e7832408eb313676d40b747ec533c3b07e5c28;p=dragonfireclient.git diff --git a/src/inventory.cpp b/src/inventory.cpp index f4a87bec1..cb8faecbc 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -23,7 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #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" @@ -126,7 +126,7 @@ ItemStack::ItemStack(std::string name_, u16 count_, void ItemStack::serialize(std::ostream &os) const { - DSTACK(__FUNCTION_NAME); + DSTACK(FUNCTION_NAME); if(empty()) return; @@ -151,7 +151,7 @@ void ItemStack::serialize(std::ostream &os) const void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) { - DSTACK(__FUNCTION_NAME); + DSTACK(FUNCTION_NAME); clear(); @@ -163,7 +163,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 @@ -183,7 +183,8 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) legacy_nimap.getName(material, name); if(name == "") name = "unknown_block"; - name = itemdef->getAlias(name); + if (itemdef) + name = itemdef->getAlias(name); count = materialcount; } else if(name == "MaterialItem2") @@ -202,7 +203,8 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef) legacy_nimap.getName(material, name); if(name == "") name = "unknown_block"; - name = itemdef->getAlias(name); + if (itemdef) + name = itemdef->getAlias(name); count = materialcount; } else if(name == "node" || name == "NodeItem" || name == "MaterialItem3" @@ -216,14 +218,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; @@ -243,7 +246,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); @@ -252,7 +255,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 @@ -262,7 +266,8 @@ 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; @@ -294,9 +299,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; } @@ -308,12 +313,12 @@ 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) { @@ -330,8 +335,9 @@ ItemStack ItemStack::addItem(const ItemStack &newitem_, *this = newitem; newitem.clear(); } - // 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 } @@ -369,8 +375,9 @@ bool ItemStack::itemFits(const ItemStack &newitem_, { newitem.clear(); } - // 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 } @@ -473,7 +480,7 @@ void InventoryList::setName(const std::string &name) void InventoryList::serialize(std::ostream &os) const { //os.imbue(std::locale("C")); - + os<<"Width "<::const_reverse_iterator i = m_items.rbegin(); - i != m_items.rend(); i++) + i != m_items.rend(); ++i) { if(count == 0) break; @@ -745,7 +752,7 @@ ItemStack InventoryList::removeItem(const ItemStack &item) ItemStack removed; for(std::vector::reverse_iterator i = m_items.rbegin(); - i != m_items.rend(); i++) + i != m_items.rend(); ++i) { if(i->name == item.name) { @@ -777,11 +784,47 @@ ItemStack InventoryList::peekItem(u32 i, u32 peekcount) const return m_items[i].peekItem(peekcount); } -void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count) +void InventoryList::moveItemSomewhere(u32 i, InventoryList *dest, u32 count) { - if(this == dest && i == dest_i) + // 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; + } + } + + // 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, + u32 count, bool swap_if_needed, bool *did_swap) +{ + if(this == dest && i == dest_i) + return count; + // Take item from source list ItemStack item1; if(count == 0) @@ -790,7 +833,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; @@ -808,8 +851,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. @@ -818,6 +864,7 @@ void InventoryList::moveItem(u32 i, InventoryList *dest, u32 dest_i, u32 count) changeItem(i, item2); } } + return (oldcount - item1.count); } /* @@ -831,6 +878,7 @@ Inventory::~Inventory() void Inventory::clear() { + m_dirty = true; for(u32 i=0; i