From 5d0b4c09a67f279847337aa06bc3066c07f63114 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 8 Jan 2016 20:27:29 +0300 Subject: Impliment craft inventory for moving items one by one. --- src/resources/inventory/complexinventory.cpp | 132 +++++++++++++++++++++++++++ src/resources/inventory/complexinventory.h | 16 ++++ src/resources/inventory/inventory.cpp | 29 +++++- src/resources/inventory/inventory.h | 32 ++++--- 4 files changed, 192 insertions(+), 17 deletions(-) (limited to 'src/resources/inventory') diff --git a/src/resources/inventory/complexinventory.cpp b/src/resources/inventory/complexinventory.cpp index 15d40f499..06a8cf5c0 100644 --- a/src/resources/inventory/complexinventory.cpp +++ b/src/resources/inventory/complexinventory.cpp @@ -22,6 +22,12 @@ #include "resources/inventory/complexinventory.h" +#include "logger.h" + +#include "being/playerinfo.h" + +#include "resources/item/complexitem.h" + #include "debug.h" ComplexInventory::ComplexInventory(const InventoryTypeT type, @@ -33,3 +39,129 @@ ComplexInventory::ComplexInventory(const InventoryTypeT type, ComplexInventory::~ComplexInventory() { } + +bool ComplexInventory::addVirtualItem(const Item *const item, + int index, + const int amount) +{ + if (!item || PlayerInfo::isItemProtected(item->getId())) + return false; + + if (index >= 0 && index < static_cast(mSize)) + { + ComplexItem *citem = nullptr; + if (mItems[index] != nullptr) + { + Item *const item2 = mItems[index]; + if (item->getId() != item2->getId() || + item->getColor() != item2->getColor()) + { // not same id or color + return false; + } + citem = dynamic_cast(mItems[index]); + if (!citem) + { // if in inventory not complex item, converting it to complex + citem = new ComplexItem(item2->getId(), + item2->getType(), + item2->getQuantity(), + item2->getRefine(), + item2->getColor(), + item2->getIdentified(), + item2->getDamaged(), + item2->getFavorite(), + Equipm_false, + Equipped_false); + citem->setTag(item2->getTag()); + delete mItems[index]; + mItems[index] = citem; + } + } + else + { + citem = new ComplexItem(item->getId(), + item->getType(), + 0, + item->getRefine(), + item->getColor(), + item->getIdentified(), + item->getDamaged(), + item->getFavorite(), + Equipm_false, + Equipped_false); + mItems[index] = citem; + } + citem->addChild(item, amount); + } + else + { + index = addItem(item->getId(), + item->getType(), + 1, + 1, + item->getColor(), + item->getIdentified(), + item->getDamaged(), + item->getFavorite(), + Equipm_false, + Equipped_false); + } + if (index == -1) + return false; + + Item *const item2 = getItem(index); + if (item2) + item2->setTag(item->getInvIndex()); + return true; +} + +void ComplexInventory::setItem(const int index, + const int id, + const int type, + const int quantity, + const uint8_t refine, + const ItemColor color, + const Identified identified, + const Damaged damaged, + const Favorite favorite, + const Equipm equipment, + const Equipped equipped) +{ + if (index < 0 || index >= static_cast(mSize)) + { + logger->log("Warning: invalid inventory index: %d", index); + return; + } + + Item *const item1 = mItems[index]; + if (!item1 && id > 0) + { + ComplexItem *const item = new ComplexItem(id, + type, + quantity, + refine, + color, + identified, + damaged, + favorite, + equipment, + equipped); + item->setInvIndex(index); + + Item *const item2 = new Item(id, + type, + quantity, + refine, + color, + identified, + damaged, + favorite, + equipment, + equipped); + item2->setInvIndex(index); + item->addChild(item2, quantity); + + mItems[index] = item; + mUsed ++; + distributeSlotsChangedEvent(); + } +} diff --git a/src/resources/inventory/complexinventory.h b/src/resources/inventory/complexinventory.h index 520779c28..95183cb7f 100644 --- a/src/resources/inventory/complexinventory.h +++ b/src/resources/inventory/complexinventory.h @@ -44,6 +44,22 @@ class ComplexInventory final : public Inventory * Destructor. */ ~ComplexInventory(); + + bool addVirtualItem(const Item *const item, + int index, + const int amount) override final; + + void setItem(const int index, + const int id, + const int type, + const int quantity, + const uint8_t refine, + const ItemColor color, + const Identified identified, + const Damaged damaged, + const Favorite favorite, + const Equipm equipment, + const Equipped equipped) override final; }; #endif // RESOURCES_INVENTORY_COMPLEXINVENTORY_H diff --git a/src/resources/inventory/inventory.cpp b/src/resources/inventory/inventory.cpp index 180a1b4f3..c73d9d8b6 100644 --- a/src/resources/inventory/inventory.cpp +++ b/src/resources/inventory/inventory.cpp @@ -372,7 +372,8 @@ int Inventory::findIndexByTag(const int tag) const } bool Inventory::addVirtualItem(const Item *const item, - int index) + int index, + const int amount) { if (item && !PlayerInfo::isItemProtected(item->getId())) { @@ -383,7 +384,7 @@ bool Inventory::addVirtualItem(const Item *const item, setItem(index, item->getId(), item->getType(), - 1, + amount, 1, item->getColor(), item->getIdentified(), @@ -396,7 +397,7 @@ bool Inventory::addVirtualItem(const Item *const item, { index = addItem(item->getId(), item->getType(), - 1, + amount, 1, item->getColor(), item->getIdentified(), @@ -465,3 +466,25 @@ void Inventory::virtualRestore(const Item *const item, mItems[index]->mQuantity += amount; } } + +void Inventory::moveItem(const int index1, + const int index2) +{ + if (index1 < 0 || + index1 >= static_cast(mSize) || + index2 < 0 || + index2 >= static_cast(mSize)) + { + return; + } + + Item *const item1 = mItems[index1]; + Item *const item2 = mItems[index2]; + if (item1) + item1->setInvIndex(index2); + if (item2) + item2->setInvIndex(index1); + mItems[index1] = item2; + mItems[index2] = item1; + distributeSlotsChangedEvent(); +} diff --git a/src/resources/inventory/inventory.h b/src/resources/inventory/inventory.h index 1e1a97dd4..9186fbd73 100644 --- a/src/resources/inventory/inventory.h +++ b/src/resources/inventory/inventory.h @@ -63,7 +63,7 @@ class Inventory notfinal /** * Destructor. */ - ~Inventory(); + virtual ~Inventory(); /** * Returns the size that this instance is configured for. @@ -103,22 +103,25 @@ class Inventory notfinal /** * Sets the item at the given position. */ - void setItem(const int index, - const int id, - const int type, - const int quantity, - const uint8_t refine, - const ItemColor color, - const Identified identified, - const Damaged damaged, - const Favorite favorite, - const Equipm equipment, - const Equipped equipped); + virtual void setItem(const int index, + const int id, + const int type, + const int quantity, + const uint8_t refine, + const ItemColor color, + const Identified identified, + const Damaged damaged, + const Favorite favorite, + const Equipm equipment, + const Equipped equipped); void setCards(const int index, const int *const cards, const int size) const; + void moveItem(const int index1, + const int index2); + /** * Remove a item from the inventory. */ @@ -176,8 +179,9 @@ class Inventory notfinal int findIndexByTag(const int tag) const; - bool addVirtualItem(const Item *const item, - int index); + virtual bool addVirtualItem(const Item *const item, + int index, + const int amount); void virtualRemove(Item *const item, const int amount); -- cgit v1.2.3-70-g09d2