From 9b0139353ae9a0388bd14159944e127143e9f153 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Tue, 17 Dec 2024 22:41:48 -0300 Subject: [PATCH] fix: supply for charge/decay items at inventory --- src/game/game.cpp | 21 ++++++++++----------- src/items/decay/decay.cpp | 4 +--- src/items/item.cpp | 25 +++++++++++++++++++++++++ src/items/item.hpp | 3 +++ 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/game/game.cpp b/src/game/game.cpp index 068ecad4a50..0464b6123db 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -2397,6 +2397,7 @@ ReturnValue Game::internalRemoveItem(const std::shared_ptr &items, int32_t } if (!test) { + item->playerUpdateSupplyTracker(); int32_t index = cylinder->getThingIndex(item); // remove the item cylinder->removeThing(item, count); @@ -2840,10 +2841,12 @@ std::shared_ptr Game::transformItem(std::shared_ptr item, uint16_t n auto decaying = item->getDecaying(); // If the item is decaying, we need to transform it to the new item - if (decaying > DECAYING_FALSE && item->getDuration() <= 1 && newType.decayTo) { + const auto &itemDuration = item->getDuration(); + if (decaying > DECAYING_FALSE && itemDuration == 0 && newType.decayTo) { g_logger().debug("Decay duration old type {}, transformEquipTo {}, transformDeEquipTo {}", curType.decayTo, curType.transformEquipTo, curType.transformDeEquipTo); g_logger().debug("Decay duration new type decayTo {}, transformEquipTo {}, transformDeEquipTo {}", newType.decayTo, newType.transformEquipTo, newType.transformDeEquipTo); itemId = newType.decayTo; + item->playerUpdateSupplyTracker(); } else if (curType.id != newType.id) { if (newType.group != curType.group) { item->setDefaultSubtype(); @@ -2859,11 +2862,9 @@ std::shared_ptr Game::transformItem(std::shared_ptr item, uint16_t n cylinder->updateThing(item, itemId, count); cylinder->postAddNotification(item, cylinder, itemIndex); - std::shared_ptr quiver = cylinder->getItem(); - if (quiver && quiver->isQuiver() - && quiver->getHoldingPlayer() - && quiver->getHoldingPlayer()->getThing(CONST_SLOT_RIGHT) == quiver) { - quiver->getHoldingPlayer()->sendInventoryItem(CONST_SLOT_RIGHT, quiver); + const auto &cylinderItem = cylinder->getItem(); + if (cylinderItem) { + cylinderItem->sendUpdateQuiver(); } item->startDecaying(); @@ -2871,11 +2872,9 @@ std::shared_ptr Game::transformItem(std::shared_ptr item, uint16_t n } } - std::shared_ptr quiver = cylinder->getItem(); - if (quiver && quiver->isQuiver() - && quiver->getHoldingPlayer() - && quiver->getHoldingPlayer()->getThing(CONST_SLOT_RIGHT) == quiver) { - quiver->getHoldingPlayer()->sendInventoryItem(CONST_SLOT_RIGHT, quiver); + const auto &cylinderItem = cylinder->getItem(); + if (cylinderItem) { + cylinderItem->sendUpdateQuiver(); } // Replacing the the old item with the new while maintaining the old position diff --git a/src/items/decay/decay.cpp b/src/items/decay/decay.cpp index 5421340d9ad..a5ea9fd903d 100644 --- a/src/items/decay/decay.cpp +++ b/src/items/decay/decay.cpp @@ -142,7 +142,6 @@ void Decay::checkDecay() { item->setDuration(item->getDuration()); item->setDecaying(DECAYING_FALSE); } else { - item->setDecaying(DECAYING_FALSE); internalDecayItem(item); } } @@ -161,9 +160,9 @@ void Decay::internalDecayItem(const std::shared_ptr &item) { const ItemType &it = Item::items[item->getID()]; // Remove the item and halt the decay process if a player triggers a bug where the item's decay ID matches its equip or de-equip transformation ID + const auto &player = item->getHoldingPlayer(); if (it.id == it.transformEquipTo || it.id == it.transformDeEquipTo) { g_game().internalRemoveItem(item); - const auto &player = item->getHoldingPlayer(); if (player) { g_logger().error("[{}] - internalDecayItem failed to player {}, item id is same from transform equip/deequip, " " item id: {}, equip to id: '{}', deequip to id '{}'", @@ -173,7 +172,6 @@ void Decay::internalDecayItem(const std::shared_ptr &item) { } if (it.decayTo != 0) { - const auto &player = item->getHoldingPlayer(); if (player) { bool needUpdateSkills = false; for (int32_t i = SKILL_FIRST; i <= SKILL_LAST; ++i) { diff --git a/src/items/item.cpp b/src/items/item.cpp index 14af2cfb47f..8b3a9dfc1a2 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -3465,3 +3465,28 @@ int32_t ItemProperties::getDuration() const { return getAttribute(ItemAttribute_t::DURATION); } } +void Item::sendUpdateQuiver() { + const auto &player = getHoldingPlayer(); + const auto &item = static_self_cast(); + const auto &inventoryItem = player->getInventoryItem(CONST_SLOT_RIGHT); + if (player && isQuiver() && inventoryItem == item) { + player->sendInventoryItem(CONST_SLOT_RIGHT, item); + } +} + +void Item::playerUpdateSupplyTracker() { + const auto &player = getHoldingPlayer(); + if (!player) { + return; + } + + static const std::array slotsToCheck = { CONST_SLOT_NECKLACE, CONST_SLOT_RING }; + const auto &item = static_self_cast(); + for (const Slots_t slot : slotsToCheck) { + const auto &inventoryItem = player->getInventoryItem(slot); + if (inventoryItem && inventoryItem == item) { + player->updateSupplyTracker(item); + break; + } + } +} diff --git a/src/items/item.hpp b/src/items/item.hpp index d199fab15cf..612dd5f795b 100644 --- a/src/items/item.hpp +++ b/src/items/item.hpp @@ -705,6 +705,9 @@ class Item : virtual public Thing, public ItemProperties, public SharedObject { bool canBeMoved() const; void checkDecayMapItemOnMove(); + void sendUpdateQuiver(); + void playerUpdateSupplyTracker(); + protected: std::weak_ptr m_parent;