summaryrefslogtreecommitdiff
path: root/src/inventory.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2015-11-02 18:10:42 +0300
committerAndrei Karas <akaras@inbox.ru>2015-11-02 18:10:42 +0300
commit2127361148d4ea5531a115cc92131a3f956ca528 (patch)
tree668182e046ff19bbacdc8cfc326bc2fab224b62b /src/inventory.cpp
parentf89de74b1ac1cd9a02a70dab9221d601296da8cd (diff)
downloadmanaverse-2127361148d4ea5531a115cc92131a3f956ca528.tar.gz
manaverse-2127361148d4ea5531a115cc92131a3f956ca528.tar.bz2
manaverse-2127361148d4ea5531a115cc92131a3f956ca528.tar.xz
manaverse-2127361148d4ea5531a115cc92131a3f956ca528.zip
Allow add items to npc inventory from same slot multiple times.
Added items automatically removed from inventory item amounts.
Diffstat (limited to 'src/inventory.cpp')
-rw-r--r--src/inventory.cpp51
1 files changed, 44 insertions, 7 deletions
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<unsigned>(
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<int>(mSize) &&
- mItems[index] == nullptr)
+ if (index >= 0 && index < static_cast<int>(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<int>(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();
}