From 598de45a1e77e5f10c69694f607941336163673f Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 29 Oct 2015 23:56:26 +0300 Subject: Dont allow add same item twice into npc inventory. --- src/inventory.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/inventory.cpp') diff --git a/src/inventory.cpp b/src/inventory.cpp index c83ea5353..eecaacd6c 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -25,6 +25,8 @@ #include "item.h" #include "logger.h" +#include "being/playerinfo.h" + #include "net/inventoryhandler.h" #include "resources/iteminfo.h" @@ -347,3 +349,38 @@ void Inventory::resize(const unsigned int newSize) mItems = new Item*[static_cast(mSize)]; std::fill_n(mItems, mSize, static_cast(nullptr)); } + +int Inventory::findIndexByTag(const int tag) const +{ + for (unsigned i = 0; i < mSize; i++) + { + const Item *const item = mItems[i]; + if (item && item->mTag == tag) + return i; + } + + return -1; +} + +void Inventory::addVirtualItem(const Item *const item) +{ + if (item && !PlayerInfo::isItemProtected(item->getId())) + { + if (findIndexByTag(item->getInvIndex()) != -1) + return; + + const int index = addItem(item->getId(), + item->getType(), + 1, + 1, + item->getColor(), + item->getIdentified(), + item->getDamaged(), + item->getFavorite(), + Equipm_false, + Equipped_false); + Item *const item2 = getItem(index); + if (item2) + item2->setTag(item->getInvIndex()); + } +} -- cgit v1.2.3-60-g2f50