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/gui/widgets/itemcontainer.cpp | 19 ++----------------- src/gui/windows/npcdialog.cpp | 20 +------------------- src/inventory.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/inventory.h | 4 ++++ src/item.cpp | 2 +- src/item.h | 2 +- 6 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/gui/widgets/itemcontainer.cpp b/src/gui/widgets/itemcontainer.cpp index 773f43ff7..8fc8b09e4 100644 --- a/src/gui/widgets/itemcontainer.cpp +++ b/src/gui/widgets/itemcontainer.cpp @@ -631,23 +631,8 @@ void ItemContainer::mouseReleased(MouseEvent &event) inventory = PlayerInfo::getInventory(); if (inventory) { - const Item *const item = inventory->getItem(dragDrop.getTag()); - if (item && !PlayerInfo::isItemProtected(item->getId())) - { - const int index = mInventory->addItem(item->getId(), - item->getType(), - 1, - 1, - item->getColor(), - item->getIdentified(), - item->getDamaged(), - item->getFavorite(), - Equipm_false, - Equipped_false); - Item *const item2 = mInventory->getItem(index); - if (item2) - item2->setTag(item->getInvIndex()); - } + mInventory->addVirtualItem(inventory->getItem( + dragDrop.getTag())); } return; } diff --git a/src/gui/windows/npcdialog.cpp b/src/gui/windows/npcdialog.cpp index 51edbaf3b..5cc538e34 100644 --- a/src/gui/windows/npcdialog.cpp +++ b/src/gui/windows/npcdialog.cpp @@ -542,25 +542,7 @@ void NpcDialog::action(const ActionEvent &event) else if (eventId == "add") { if (inventoryWindow) - { - const Item *const item = inventoryWindow->getSelectedItem(); - if (item) - { - const int index = mInventory->addItem(item->getId(), - item->getType(), - 1, - 1, - item->getColor(), - item->getIdentified(), - item->getDamaged(), - item->getFavorite(), - Equipm_false, - Equipped_false); - Item *const item2 = mInventory->getItem(index); - if (item2) - item2->setTag(item->getInvIndex()); - } - } + mInventory->addVirtualItem(inventoryWindow->getSelectedItem()); } else if (eventId.find("skin_") == 0) { 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()); + } +} diff --git a/src/inventory.h b/src/inventory.h index 5fe92a84f..e20010c23 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -172,6 +172,10 @@ class Inventory final void resize(const unsigned int newSize); + int findIndexByTag(const int tag) const; + + void addVirtualItem(const Item *const item); + protected: typedef std::list InventoryListenerList; InventoryListenerList mInventoryListeners; diff --git a/src/item.cpp b/src/item.cpp index 9a534cfad..2f574130c 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -50,6 +50,7 @@ Item::Item(const int id, mId(0), mColor(ItemColor_zero), mQuantity(quantity), + mTag(0), mImage(nullptr), mDescription(), mTags(), @@ -57,7 +58,6 @@ Item::Item(const int id, mRefine(refine), mInvIndex(0), mType(type), - mTag(0), mEquipment(equipment), mEquipped(equipped), mInEquipment(false), diff --git a/src/item.h b/src/item.h index 9f0772286..50f6d3fbd 100644 --- a/src/item.h +++ b/src/item.h @@ -228,6 +228,7 @@ class Item notfinal int mId; /**< Item type id. */ ItemColor mColor; int mQuantity; /**< Number of items. */ + int mTag; protected: Image *mImage; /**< Item image. */ @@ -237,7 +238,6 @@ class Item notfinal uint8_t mRefine; /**< Item refine level. */ int mInvIndex; /**< Inventory index. */ int mType; /**< Item type. */ - int mTag; Equipm mEquipment; /**< Item is equipment. */ Equipped mEquipped; /**< Item is equipped. */ bool mInEquipment; /**< Item is in equipment */ -- cgit v1.2.3-70-g09d2