From 2127361148d4ea5531a115cc92131a3f956ca528 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 2 Nov 2015 18:10:42 +0300 Subject: Allow add items to npc inventory from same slot multiple times. Added items automatically removed from inventory item amounts. --- src/inventory.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) (limited to 'src/inventory.cpp') diff --git a/src/inventory.cpp b/src/inventory.cpp index 61434754f..29df424e1 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -55,6 +55,7 @@ namespace Inventory::Inventory(const InventoryType::Type type, const int size1) : mInventoryListeners(), + mVirtualRemove(), mType(type), mSize(size1 == -1 ? static_cast( inventoryHandler->getSize(type)) @@ -364,18 +365,15 @@ int Inventory::findIndexByTag(const int tag) const return -1; } -void Inventory::addVirtualItem(const Item *const item, +bool Inventory::addVirtualItem(const Item *const item, int index) { if (item && !PlayerInfo::isItemProtected(item->getId())) { - if (findIndexByTag(item->getInvIndex()) != -1) - return; - - if (index >= 0 && - index < static_cast(mSize) && - mItems[index] == nullptr) + if (index >= 0 && index < static_cast(mSize)) { + if (mItems[index] != nullptr) + return false; setItem(index, item->getId(), item->getType(), @@ -401,8 +399,47 @@ void Inventory::addVirtualItem(const Item *const item, Equipm_false, Equipped_false); } + if (index == -1) + return false; + Item *const item2 = getItem(index); if (item2) item2->setTag(item->getInvIndex()); + return true; + } + return false; +} + +void Inventory::virtualRemove(Item *const item, + const int amount) +{ + if (!item || item->mQuantity < amount) + return; + + const int index = item->getInvIndex(); + const IntMapCIter it = mVirtualRemove.find(index); + if (it == mVirtualRemove.end()) + mVirtualRemove[index] = amount; + else + mVirtualRemove[index] += amount; + item->mQuantity -= amount; +} + +void Inventory::restoreVirtuals() +{ + const int sz = static_cast(mSize); + + logger->log("Inventory::restoreVirtuals 1"); + FOR_EACH (IntMapCIter, it, mVirtualRemove) + { + logger->log("Inventory::restoreVirtuals 2"); + const int index = (*it).first; + if (index < 0 || index >= sz) + continue; + Item *const item = mItems[index]; + if (!item) + continue; + item->mQuantity += (*it).second; } + mVirtualRemove.clear(); } -- cgit v1.2.3-60-g2f50