From 77abc9412dd2463090ccf460cd6bdd3a4e651753 Mon Sep 17 00:00:00 2001 From: Sarah Wesker Date: Fri, 8 Nov 2024 22:37:33 -0400 Subject: [PATCH] Conditions handled by `std::shared_ptr` --- src/bed.cpp | 2 +- src/combat.cpp | 10 +++---- src/combat.h | 7 +++-- src/condition.cpp | 70 ++++++++++++++++++++++--------------------- src/condition.h | 71 ++++++++++++++++++++++---------------------- src/creature.cpp | 45 +++++++++++----------------- src/creature.h | 14 ++++----- src/game.cpp | 7 ++--- src/game.h | 2 +- src/iologindata.cpp | 6 ++-- src/items.cpp | 12 ++++---- src/items.h | 2 +- src/luascript.cpp | 55 +++++++++++++++++----------------- src/monster.cpp | 5 ++-- src/monsters.cpp | 9 +++--- src/monsters.h | 4 +-- src/movement.cpp | 8 ++--- src/player.cpp | 34 ++++++++++----------- src/player.h | 6 ++-- src/protocolgame.cpp | 6 ++-- src/spells.cpp | 37 +++++++++++------------ 21 files changed, 201 insertions(+), 211 deletions(-) diff --git a/src/bed.cpp b/src/bed.cpp index aab783ec58..d240f4d8e6 100644 --- a/src/bed.cpp +++ b/src/bed.cpp @@ -202,7 +202,7 @@ void BedItem::regeneratePlayer(Player* player) const { const uint32_t sleptTime = time(nullptr) - sleepStart; - Condition* condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT); + std::shared_ptr condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT); if (condition) { uint32_t regen; if (condition->getTicks() != -1) { diff --git a/src/combat.cpp b/src/combat.cpp index 0f8e326270..e777181146 100644 --- a/src/combat.cpp +++ b/src/combat.cpp @@ -672,7 +672,7 @@ void Combat::doCombat(Creature* caster, Creature* target) const if (params.origin != ORIGIN_MELEE) { for (const auto& condition : params.conditionList) { if (caster == target || !target->isImmune(condition->getType())) { - Condition* conditionCopy = condition->clone(); + std::shared_ptr conditionCopy = condition->clone(); conditionCopy->setParam(CONDITION_PARAM_OWNER, caster->getID()); target->addCombatCondition(conditionCopy); } @@ -756,7 +756,7 @@ void Combat::doCombat(Creature* caster, const Position& position) const (caster != creature && Combat::canDoCombat(caster, creature) == RETURNVALUE_NOERROR)) { for (const auto& condition : params.conditionList) { if (caster == creature || !creature->isImmune(condition->getType())) { - Condition* conditionCopy = condition->clone(); + std::shared_ptr conditionCopy = condition->clone(); if (caster) { conditionCopy->setParam(CONDITION_PARAM_OWNER, caster->getID()); } @@ -829,7 +829,7 @@ void Combat::doTargetCombat(Creature* caster, Creature* target, CombatDamage& da if (damage.blockType == BLOCK_NONE || damage.blockType == BLOCK_ARMOR) { for (const auto& condition : params.conditionList) { if (caster == target || !target->isImmune(condition->getType())) { - Condition* conditionCopy = condition->clone(); + std::shared_ptr conditionCopy = condition->clone(); if (caster) { conditionCopy->setParam(CONDITION_PARAM_OWNER, caster->getID()); } @@ -997,7 +997,7 @@ void Combat::doAreaCombat(Creature* caster, const Position& position, const Area if (damage.blockType == BLOCK_NONE || damage.blockType == BLOCK_ARMOR) { for (const auto& condition : params.conditionList) { if (caster == creature || !creature->isImmune(condition->getType())) { - Condition* conditionCopy = condition->clone(); + std::shared_ptr conditionCopy = condition->clone(); if (caster) { conditionCopy->setParam(CONDITION_PARAM_OWNER, caster->getID()); } @@ -1397,7 +1397,7 @@ void MagicField::onStepInField(Creature* creature) const ItemType& it = items[getID()]; if (it.conditionDamage) { - Condition* conditionCopy = it.conditionDamage->clone(); + std::shared_ptr conditionCopy = it.conditionDamage->clone(); uint32_t ownerId = getOwner(); if (ownerId) { bool harmfulField = true; diff --git a/src/combat.h b/src/combat.h index 5bc110e659..40b2a6242c 100644 --- a/src/combat.h +++ b/src/combat.h @@ -41,7 +41,7 @@ class TargetCallback final : public CallBack struct CombatParams { - std::forward_list> conditionList = {}; + std::forward_list> conditionList = {}; std::unique_ptr valueCallback = nullptr; std::unique_ptr tileCallback = nullptr; @@ -115,7 +115,10 @@ class Combat void setArea(AreaCombat* area); bool hasArea() const { return area != nullptr; } - void addCondition(const Condition* condition) { params.conditionList.emplace_front(condition); } + void addCondition(const std::shared_ptr& condition) + { + params.conditionList.emplace_front(condition); + } void clearConditions() { params.conditionList.clear(); } void setPlayerCombatValues(formulaType_t formulaType, double mina, double minb, double maxa, double maxb); void postCombatEffects(Creature* caster, const Position& pos) const { postCombatEffects(caster, pos, params); } diff --git a/src/condition.cpp b/src/condition.cpp index ab82a332cf..216ff13913 100644 --- a/src/condition.cpp +++ b/src/condition.cpp @@ -165,8 +165,9 @@ bool Condition::executeCondition(Creature*, int32_t interval) return getEndTime() >= OTSYS_TIME(); } -Condition* Condition::createCondition(ConditionId_t id, ConditionType_t type, int32_t ticks, int32_t param /* = 0*/, - bool buff /* = false*/, uint32_t subId /* = 0*/, bool aggressive /* = false */) +std::shared_ptr Condition::createCondition(ConditionId_t id, ConditionType_t type, int32_t ticks, + int32_t param /* = 0*/, bool buff /* = false*/, + uint32_t subId /* = 0*/, bool aggressive /* = false */) { switch (type) { case CONDITION_POISON: @@ -177,38 +178,39 @@ Condition* Condition::createCondition(ConditionId_t id, ConditionType_t type, in case CONDITION_DAZZLED: case CONDITION_CURSED: case CONDITION_BLEEDING: - return new ConditionDamage(id, type, buff, subId, aggressive); + return std::make_shared(id, type, buff, subId, aggressive); case CONDITION_HASTE: case CONDITION_PARALYZE: - return new ConditionSpeed(id, type, ticks, buff, subId, param, aggressive); + return std::make_shared(id, type, ticks, buff, subId, param, aggressive); case CONDITION_INVISIBLE: - return new ConditionInvisible(id, type, ticks, buff, subId, aggressive); + return std::make_shared(id, type, ticks, buff, subId, aggressive); case CONDITION_OUTFIT: - return new ConditionOutfit(id, type, ticks, buff, subId, aggressive); + return std::make_shared(id, type, ticks, buff, subId, aggressive); case CONDITION_LIGHT: - return new ConditionLight(id, type, ticks, buff, subId, param & 0xFF, (param & 0xFF00) >> 8, aggressive); + return std::make_shared(id, type, ticks, buff, subId, param & 0xFF, (param & 0xFF00) >> 8, + aggressive); case CONDITION_REGENERATION: - return new ConditionRegeneration(id, type, ticks, buff, subId, aggressive); + return std::make_shared(id, type, ticks, buff, subId, aggressive); case CONDITION_SOUL: - return new ConditionSoul(id, type, ticks, buff, subId, aggressive); + return std::make_shared(id, type, ticks, buff, subId, aggressive); case CONDITION_ATTRIBUTES: - return new ConditionAttributes(id, type, ticks, buff, subId, aggressive); + return std::make_shared(id, type, ticks, buff, subId, aggressive); case CONDITION_SPELLCOOLDOWN: - return new ConditionSpellCooldown(id, type, ticks, buff, subId, aggressive); + return std::make_shared(id, type, ticks, buff, subId, aggressive); case CONDITION_SPELLGROUPCOOLDOWN: - return new ConditionSpellGroupCooldown(id, type, ticks, buff, subId, aggressive); + return std::make_shared(id, type, ticks, buff, subId, aggressive); case CONDITION_DRUNK: - return new ConditionDrunk(id, type, ticks, buff, subId, param, aggressive); + return std::make_shared(id, type, ticks, buff, subId, param, aggressive); case CONDITION_INFIGHT: case CONDITION_EXHAUST_WEAPON: @@ -219,18 +221,18 @@ Condition* Condition::createCondition(ConditionId_t id, ConditionType_t type, in case CONDITION_YELLTICKS: case CONDITION_PACIFIED: case CONDITION_MANASHIELD: - return new ConditionGeneric(id, type, ticks, buff, subId, aggressive); + return std::make_shared(id, type, ticks, buff, subId, aggressive); case CONDITION_ROOT: - return new ConditionGeneric(id, type, ticks, buff, subId, aggressive); + return std::make_shared(id, type, ticks, buff, subId, aggressive); case CONDITION_MANASHIELD_BREAKABLE: - return new ConditionManaShield(id, type, ticks, buff, subId); + return std::make_shared(id, type, ticks, buff, subId); default: return nullptr; } } -Condition* Condition::createCondition(PropStream& propStream) +std::shared_ptr Condition::createCondition(PropStream& propStream) { uint8_t attr; if (!propStream.read(attr) || attr != CONDITIONATTR_TYPE) { @@ -314,7 +316,7 @@ bool Condition::isPersistent() const uint32_t Condition::getIcons() const { return isBuff ? ICON_PARTY_BUFF : 0; } -bool Condition::updateCondition(const Condition* addCondition) +bool Condition::updateCondition(const std::shared_ptr& addCondition) { if (conditionType != addCondition->getType()) { return false; @@ -343,7 +345,7 @@ void ConditionGeneric::endCondition(Creature*) // } -void ConditionGeneric::addCondition(Creature*, const Condition* condition) +void ConditionGeneric::addCondition(Creature*, const std::shared_ptr& condition) { if (updateCondition(condition)) { setTicks(condition->getTicks()); @@ -374,7 +376,7 @@ uint32_t ConditionGeneric::getIcons() const return icons; } -void ConditionAttributes::addCondition(Creature* creature, const Condition* condition) +void ConditionAttributes::addCondition(Creature* creature, const std::shared_ptr& condition) { if (updateCondition(condition)) { setTicks(condition->getTicks()); @@ -832,7 +834,7 @@ int32_t ConditionAttributes::getParam(ConditionParam_t param) } } -void ConditionRegeneration::addCondition(Creature*, const Condition* condition) +void ConditionRegeneration::addCondition(Creature*, const std::shared_ptr& condition) { if (updateCondition(condition)) { setTicks(condition->getTicks()); @@ -1002,7 +1004,7 @@ int32_t ConditionRegeneration::getParam(ConditionParam_t param) } } -void ConditionSoul::addCondition(Creature*, const Condition* condition) +void ConditionSoul::addCondition(Creature*, const std::shared_ptr& condition) { if (updateCondition(condition)) { setTicks(condition->getTicks()); @@ -1210,7 +1212,7 @@ void ConditionDamage::serialize(PropWriteStream& propWriteStream) } } -bool ConditionDamage::updateCondition(const Condition* addCondition) +bool ConditionDamage::updateCondition(const std::shared_ptr& addCondition) { const ConditionDamage& conditionDamage = static_cast(*addCondition); if (conditionDamage.doForceUpdate()) { @@ -1395,7 +1397,7 @@ void ConditionDamage::endCondition(Creature*) // } -void ConditionDamage::addCondition(Creature* creature, const Condition* condition) +void ConditionDamage::addCondition(Creature* creature, const std::shared_ptr& condition) { if (condition->getType() != conditionType) { return; @@ -1616,7 +1618,7 @@ bool ConditionSpeed::executeCondition(Creature* creature, int32_t interval) void ConditionSpeed::endCondition(Creature* creature) { g_game.changeSpeed(creature, -speedDelta); } -void ConditionSpeed::addCondition(Creature* creature, const Condition* condition) +void ConditionSpeed::addCondition(Creature* creature, const std::shared_ptr& condition) { if (conditionType != condition->getType()) { return; @@ -1722,7 +1724,7 @@ void ConditionOutfit::endCondition(Creature* creature) g_game.internalCreatureChangeOutfit(creature, creature->getDefaultOutfit()); } -void ConditionOutfit::addCondition(Creature* creature, const Condition* condition) +void ConditionOutfit::addCondition(Creature* creature, const std::shared_ptr& condition) { if (updateCondition(condition)) { setTicks(condition->getTicks()); @@ -1771,7 +1773,7 @@ void ConditionLight::endCondition(Creature* creature) g_game.changeLight(creature); } -void ConditionLight::addCondition(Creature* creature, const Condition* condition) +void ConditionLight::addCondition(Creature* creature, const std::shared_ptr& condition) { if (updateCondition(condition)) { setTicks(condition->getTicks()); @@ -1871,7 +1873,7 @@ void ConditionLight::serialize(PropWriteStream& propWriteStream) propWriteStream.write(lightChangeInterval); } -void ConditionSpellCooldown::addCondition(Creature* creature, const Condition* condition) +void ConditionSpellCooldown::addCondition(Creature* creature, const std::shared_ptr& condition) { if (updateCondition(condition)) { setTicks(condition->getTicks()); @@ -1900,7 +1902,7 @@ bool ConditionSpellCooldown::startCondition(Creature* creature) return true; } -void ConditionSpellGroupCooldown::addCondition(Creature* creature, const Condition* condition) +void ConditionSpellGroupCooldown::addCondition(Creature* creature, const std::shared_ptr& condition) { if (updateCondition(condition)) { setTicks(condition->getTicks()); @@ -1939,19 +1941,19 @@ bool ConditionDrunk::startCondition(Creature* creature) return true; } -bool ConditionDrunk::updateCondition(const Condition* addCondition) +bool ConditionDrunk::updateCondition(const std::shared_ptr& addCondition) { - const ConditionDrunk* conditionDrunk = static_cast(addCondition); + const auto& conditionDrunk = std::static_pointer_cast(addCondition); return conditionDrunk->drunkenness > drunkenness; } -void ConditionDrunk::addCondition(Creature* creature, const Condition* condition) +void ConditionDrunk::addCondition(Creature* creature, const std::shared_ptr& condition) { if (!updateCondition(condition)) { return; } - const ConditionDrunk* conditionDrunk = static_cast(condition); + const auto& conditionDrunk = std::static_pointer_cast(condition); setTicks(conditionDrunk->getTicks()); creature->setDrunkenness(conditionDrunk->drunkenness); } @@ -1999,7 +2001,7 @@ void ConditionManaShield::endCondition(Creature* creature) } } -void ConditionManaShield::addCondition(Creature* creature, const Condition* addCondition) +void ConditionManaShield::addCondition(Creature* creature, const std::shared_ptr& addCondition) { if (Player* player = creature->getPlayer()) { endCondition(player); diff --git a/src/condition.h b/src/condition.h index b60fc680ca..6d4613dbe0 100644 --- a/src/condition.h +++ b/src/condition.h @@ -76,12 +76,12 @@ class Condition virtual bool startCondition(Creature* creature); virtual bool executeCondition(Creature* creature, int32_t interval); virtual void endCondition(Creature* creature) = 0; - virtual void addCondition(Creature* creature, const Condition* condition) = 0; + virtual void addCondition(Creature* creature, const std::shared_ptr& condition) = 0; virtual uint32_t getIcons() const; ConditionId_t getId() const { return id; } uint32_t getSubId() const { return subId; } - virtual Condition* clone() const = 0; + virtual std::shared_ptr clone() const = 0; ConditionType_t getType() const { return conditionType; } int64_t getEndTime() const { return endTime; } @@ -89,9 +89,10 @@ class Condition void setTicks(int32_t newTicks); bool isAggressive() const { return aggressive; } - static Condition* createCondition(ConditionId_t id, ConditionType_t type, int32_t ticks, int32_t param = 0, - bool buff = false, uint32_t subId = 0, bool aggressive = false); - static Condition* createCondition(PropStream& propStream); + static std::shared_ptr createCondition(ConditionId_t id, ConditionType_t type, int32_t ticks, + int32_t param = 0, bool buff = false, uint32_t subId = 0, + bool aggressive = false); + static std::shared_ptr createCondition(PropStream& propStream); virtual bool setParam(ConditionParam_t param, int32_t value); virtual int32_t getParam(ConditionParam_t param); @@ -104,7 +105,7 @@ class Condition bool isPersistent() const; protected: - virtual bool updateCondition(const Condition* addCondition); + virtual bool updateCondition(const std::shared_ptr& addCondition); int64_t endTime; uint32_t subId; @@ -128,10 +129,10 @@ class ConditionGeneric : public Condition bool startCondition(Creature* creature) override; bool executeCondition(Creature* creature, int32_t interval) override; void endCondition(Creature* creature) override; - void addCondition(Creature* creature, const Condition* condition) override; + void addCondition(Creature* creature, const std::shared_ptr& condition) override; uint32_t getIcons() const override; - ConditionGeneric* clone() const override { return new ConditionGeneric(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } }; class ConditionAttributes final : public ConditionGeneric @@ -145,12 +146,12 @@ class ConditionAttributes final : public ConditionGeneric bool startCondition(Creature* creature) override; bool executeCondition(Creature* creature, int32_t interval) override; void endCondition(Creature* creature) override; - void addCondition(Creature* creature, const Condition* condition) override; + void addCondition(Creature* creature, const std::shared_ptr& condition) override; bool setParam(ConditionParam_t param, int32_t value) override; int32_t getParam(ConditionParam_t param) override; - ConditionAttributes* clone() const override { return new ConditionAttributes(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } // serialization void serialize(PropWriteStream& propWriteStream) override; @@ -182,13 +183,13 @@ class ConditionRegeneration final : public ConditionGeneric ConditionGeneric(id, type, ticks, buff, subId, aggressive) {} - void addCondition(Creature* creature, const Condition* condition) override; + void addCondition(Creature* creature, const std::shared_ptr& condition) override; bool executeCondition(Creature* creature, int32_t interval) override; bool setParam(ConditionParam_t param, int32_t value) override; int32_t getParam(ConditionParam_t param) override; - ConditionRegeneration* clone() const override { return new ConditionRegeneration(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } // serialization void serialize(PropWriteStream& propWriteStream) override; @@ -212,13 +213,13 @@ class ConditionSoul final : public ConditionGeneric ConditionGeneric(id, type, ticks, buff, subId, aggressive) {} - void addCondition(Creature* creature, const Condition* condition) override; + void addCondition(Creature* creature, const std::shared_ptr& condition) override; bool executeCondition(Creature* creature, int32_t interval) override; bool setParam(ConditionParam_t param, int32_t value) override; int32_t getParam(ConditionParam_t param) override; - ConditionSoul* clone() const override { return new ConditionSoul(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } // serialization void serialize(PropWriteStream& propWriteStream) override; @@ -241,7 +242,7 @@ class ConditionInvisible final : public ConditionGeneric bool startCondition(Creature* creature) override; void endCondition(Creature* creature) override; - ConditionInvisible* clone() const override { return new ConditionInvisible(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } }; class ConditionDamage final : public Condition @@ -258,10 +259,10 @@ class ConditionDamage final : public Condition bool startCondition(Creature* creature) override; bool executeCondition(Creature* creature, int32_t interval) override; void endCondition(Creature* creature) override; - void addCondition(Creature* creature, const Condition* condition) override; + void addCondition(Creature* creature, const std::shared_ptr& condition) override; uint32_t getIcons() const override; - ConditionDamage* clone() const override { return new ConditionDamage(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } bool setParam(ConditionParam_t param, int32_t value) override; int32_t getParam(ConditionParam_t param) override; @@ -297,7 +298,7 @@ class ConditionDamage final : public Condition bool getNextDamage(int32_t& damage); bool doDamage(Creature* creature, int32_t healthChange); - bool updateCondition(const Condition* addCondition) override; + bool updateCondition(const std::shared_ptr& addCondition) override; }; class ConditionSpeed final : public Condition @@ -311,10 +312,10 @@ class ConditionSpeed final : public Condition bool startCondition(Creature* creature) override; bool executeCondition(Creature* creature, int32_t interval) override; void endCondition(Creature* creature) override; - void addCondition(Creature* creature, const Condition* condition) override; + void addCondition(Creature* creature, const std::shared_ptr& condition) override; uint32_t getIcons() const override; - ConditionSpeed* clone() const override { return new ConditionSpeed(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } bool setParam(ConditionParam_t param, int32_t value) override; int32_t getParam(ConditionParam_t param) override; @@ -346,9 +347,9 @@ class ConditionOutfit final : public Condition bool startCondition(Creature* creature) override; bool executeCondition(Creature* creature, int32_t interval) override; void endCondition(Creature* creature) override; - void addCondition(Creature* creature, const Condition* condition) override; + void addCondition(Creature* creature, const std::shared_ptr& condition) override; - ConditionOutfit* clone() const override { return new ConditionOutfit(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } void setOutfit(const Outfit_t& outfit); @@ -371,9 +372,9 @@ class ConditionLight final : public Condition bool startCondition(Creature* creature) override; bool executeCondition(Creature* creature, int32_t interval) override; void endCondition(Creature* creature) override; - void addCondition(Creature* creature, const Condition* condition) override; + void addCondition(Creature* creature, const std::shared_ptr& condition) override; - ConditionLight* clone() const override { return new ConditionLight(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } bool setParam(ConditionParam_t param, int32_t value) override; int32_t getParam(ConditionParam_t param) override; @@ -397,9 +398,9 @@ class ConditionSpellCooldown final : public ConditionGeneric {} bool startCondition(Creature* creature) override; - void addCondition(Creature* creature, const Condition* condition) override; + void addCondition(Creature* creature, const std::shared_ptr& condition) override; - ConditionSpellCooldown* clone() const override { return new ConditionSpellCooldown(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } }; class ConditionSpellGroupCooldown final : public ConditionGeneric @@ -411,9 +412,9 @@ class ConditionSpellGroupCooldown final : public ConditionGeneric {} bool startCondition(Creature* creature) override; - void addCondition(Creature* creature, const Condition* condition) override; + void addCondition(Creature* creature, const std::shared_ptr& condition) override; - ConditionSpellGroupCooldown* clone() const override { return new ConditionSpellGroupCooldown(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } }; class ConditionDrunk final : public Condition @@ -432,14 +433,14 @@ class ConditionDrunk final : public Condition void endCondition(Creature* creature) override; bool startCondition(Creature* creature) override; bool setParam(ConditionParam_t param, int32_t value) override; - void addCondition(Creature* creature, const Condition* condition) override; + void addCondition(Creature* creature, const std::shared_ptr& condition) override; - ConditionDrunk* clone() const override { return new ConditionDrunk(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } private: uint8_t drunkenness = 25; - bool updateCondition(const Condition* addCondition) override; + bool updateCondition(const std::shared_ptr& addCondition) override; }; class ConditionManaShield final : public Condition @@ -452,20 +453,20 @@ class ConditionManaShield final : public Condition bool startCondition(Creature* creature) override; void endCondition(Creature* creature) override; - void addCondition(Creature* creature, const Condition* addCondition) override; + void addCondition(Creature* creature, const std::shared_ptr& addCondition) override; uint32_t getIcons() const override; bool setParam(ConditionParam_t param, int32_t value) override; - ConditionManaShield* clone() const override { return new ConditionManaShield(*this); } + std::shared_ptr clone() const override { return std::make_shared(*this); } // serialization void serialize(PropWriteStream& propWriteStream) override; bool unserializeProp(ConditionAttr_t attr, PropStream& propStream) override; int32_t onDamageTaken(Player* player, int32_t manaChange); - uint16_t getManaShield() { return manaShield; } - uint16_t getMaxManaShield() { return maxManaShield; } + uint16_t getManaShield() const { return manaShield; } + uint16_t getMaxManaShield() const { return maxManaShield; } private: uint16_t manaShield = 0; diff --git a/src/creature.cpp b/src/creature.cpp index d260a7ab9d..8c52930202 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -34,10 +34,6 @@ Creature::~Creature() for (Condition* condition : conditions) { condition->endCondition(this); } - - for (auto condition : conditions) { - delete condition; - } } bool Creature::canSee(const Position& myPos, const Position& pos, int32_t viewRangeX, int32_t viewRangeY) @@ -880,7 +876,7 @@ void Creature::onTickCondition(ConditionType_t type, bool& bRemove) } } -void Creature::onCombatRemoveCondition(Condition* condition) { removeCondition(condition); } +void Creature::onCombatRemoveCondition(const std::shared_ptr& condition) { removeCondition(condition); } void Creature::onAttacked() { @@ -958,7 +954,7 @@ bool Creature::setMaster(Creature* newMaster) return true; } -bool Creature::addCondition(Condition* condition, bool force /* = false*/) +bool Creature::addCondition(const std::shared_ptr& condition, bool force /* = false*/) { if (!condition) { return false; @@ -973,10 +969,9 @@ bool Creature::addCondition(Condition* condition, bool force /* = false*/) } } - Condition* prevCond = getCondition(condition->getType(), condition->getId(), condition->getSubId()); + std::shared_ptr prevCond = getCondition(condition->getType(), condition->getId(), condition->getSubId()); if (prevCond) { prevCond->addCondition(this, condition); - delete condition; return true; } @@ -986,11 +981,10 @@ bool Creature::addCondition(Condition* condition, bool force /* = false*/) return true; } - delete condition; return false; } -bool Creature::addCombatCondition(Condition* condition) +bool Creature::addCombatCondition(const std::shared_ptr& condition) { // Caution: condition variable could be deleted after the call to addCondition ConditionType_t type = condition->getType(); @@ -1007,7 +1001,7 @@ void Creature::removeCondition(ConditionType_t type, bool force /* = false*/) { auto it = conditions.begin(), end = conditions.end(); while (it != end) { - Condition* condition = *it; + std::shared_ptr condition = *it; if (condition->getType() != type) { ++it; continue; @@ -1025,7 +1019,6 @@ void Creature::removeCondition(ConditionType_t type, bool force /* = false*/) it = conditions.erase(it); condition->endCondition(this); - delete condition; onEndCondition(type); } @@ -1035,7 +1028,7 @@ void Creature::removeCondition(ConditionType_t type, ConditionId_t conditionId, { auto it = conditions.begin(), end = conditions.end(); while (it != end) { - Condition* condition = *it; + std::shared_ptr condition = *it; if (condition->getType() != type || condition->getId() != conditionId) { ++it; continue; @@ -1053,7 +1046,6 @@ void Creature::removeCondition(ConditionType_t type, ConditionId_t conditionId, it = conditions.erase(it); condition->endCondition(this); - delete condition; onEndCondition(type); } @@ -1061,19 +1053,19 @@ void Creature::removeCondition(ConditionType_t type, ConditionId_t conditionId, void Creature::removeCombatCondition(ConditionType_t type) { - std::vector removeConditions; - for (Condition* condition : conditions) { + std::vector> removeConditions; + for (const auto& condition : conditions) { if (condition->getType() == type) { removeConditions.push_back(condition); } } - for (Condition* condition : removeConditions) { + for (const auto& condition : removeConditions) { onCombatRemoveCondition(condition); } } -void Creature::removeCondition(Condition* condition, bool force /* = false*/) +void Creature::removeCondition(const std::shared_ptr& condition, bool force /* = false*/) { auto it = std::find(conditions.begin(), conditions.end(), condition); if (it == conditions.end()) { @@ -1093,12 +1085,11 @@ void Creature::removeCondition(Condition* condition, bool force /* = false*/) condition->endCondition(this); onEndCondition(condition->getType()); - delete condition; } -Condition* Creature::getCondition(ConditionType_t type) const +std::shared_ptr Creature::getCondition(ConditionType_t type) const { - for (Condition* condition : conditions) { + for (const auto& condition : conditions) { if (condition->getType() == type) { return condition; } @@ -1106,9 +1097,10 @@ Condition* Creature::getCondition(ConditionType_t type) const return nullptr; } -Condition* Creature::getCondition(ConditionType_t type, ConditionId_t conditionId, uint32_t subId /* = 0*/) const +std::shared_ptr Creature::getCondition(ConditionType_t type, ConditionId_t conditionId, + uint32_t subId /* = 0*/) const { - for (Condition* condition : conditions) { + for (const auto& condition : conditions) { if (condition->getType() == type && condition->getId() == conditionId && condition->getSubId() == subId) { return condition; } @@ -1119,7 +1111,7 @@ Condition* Creature::getCondition(ConditionType_t type, ConditionId_t conditionI void Creature::executeConditions(uint32_t interval) { ConditionList tempConditions{conditions}; - for (Condition* condition : tempConditions) { + for (const auto& condition : tempConditions) { auto it = std::find(conditions.begin(), conditions.end(), condition); if (it == conditions.end()) { continue; @@ -1131,7 +1123,6 @@ void Creature::executeConditions(uint32_t interval) conditions.erase(it); condition->endCondition(this); onEndCondition(condition->getType()); - delete condition; } } } @@ -1144,7 +1135,7 @@ bool Creature::hasCondition(ConditionType_t type, uint32_t subId /* = 0*/) const } int64_t timeNow = OTSYS_TIME(); - for (Condition* condition : conditions) { + for (const auto& condition : conditions) { if (condition->getType() != type || condition->getSubId() != subId) { continue; } @@ -1402,7 +1393,7 @@ bool FrozenPathingConditionCall::operator()(const Position& startPos, const Posi bool Creature::isInvisible() const { - return std::find_if(conditions.begin(), conditions.end(), [](const Condition* condition) { + return std::find_if(conditions.begin(), conditions.end(), [](const std::shared_ptr& condition) { return condition->getType() == CONDITION_INVISIBLE; }) != conditions.end(); } diff --git a/src/creature.h b/src/creature.h index 305087587d..0884a528d7 100644 --- a/src/creature.h +++ b/src/creature.h @@ -18,7 +18,7 @@ class Monster; class Npc; class Player; -using ConditionList = std::list; +using ConditionList = std::list>; using CreatureEventList = std::list; using CreatureIconHashMap = std::unordered_map; @@ -224,14 +224,14 @@ class Creature : virtual public Thing virtual uint8_t getSpeechBubble() const { return SPEECHBUBBLE_NONE; } - bool addCondition(Condition* condition, bool force = false); - bool addCombatCondition(Condition* condition); + bool addCondition(const std::shared_ptr& condition, bool force = false); + bool addCombatCondition(const std::shared_ptr& condition); void removeCondition(ConditionType_t type, ConditionId_t conditionId, bool force = false); void removeCondition(ConditionType_t type, bool force = false); - void removeCondition(Condition* condition, bool force = false); + void removeCondition(const std::shared_ptr& condition, bool force = false); void removeCombatCondition(ConditionType_t type); - Condition* getCondition(ConditionType_t type) const; - Condition* getCondition(ConditionType_t type, ConditionId_t conditionId, uint32_t subId = 0) const; + std::shared_ptr getCondition(ConditionType_t type) const; + std::shared_ptr getCondition(ConditionType_t type, ConditionId_t conditionId, uint32_t subId = 0) const; void executeConditions(uint32_t interval); bool hasCondition(ConditionType_t type, uint32_t subId = 0) const; virtual bool isImmune(ConditionType_t type) const; @@ -260,7 +260,7 @@ class Creature : virtual public Thing virtual void onAddCombatCondition(ConditionType_t type); virtual void onEndCondition(ConditionType_t type); void onTickCondition(ConditionType_t type, bool& bRemove); - virtual void onCombatRemoveCondition(Condition* condition); + virtual void onCombatRemoveCondition(const std::shared_ptr& condition); virtual void onAttackedCreature(Creature*, bool = true) {} virtual void onAttacked(); virtual void onAttackedCreatureDrainHealth(Creature* target, int32_t points); diff --git a/src/game.cpp b/src/game.cpp index 01a1ed565a..0f990677c5 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -3654,7 +3654,7 @@ bool Game::playerYell(Player* player, const std::string& text) } } - Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_YELLTICKS, 30000, 0); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_YELLTICKS, 30000, 0); player->addCondition(condition); } @@ -4279,7 +4279,7 @@ bool Game::combatChangeHealth(Creature* attacker, Creature* target, CombatDamage } if (getBoolean(ConfigManager::MANASHIELD_BREAKABLE) && targetPlayer) { - if (ConditionManaShield* conditionManaShield = dynamic_cast( + if (auto conditionManaShield = std::dynamic_pointer_cast( targetPlayer->getCondition(CONDITION_MANASHIELD_BREAKABLE))) { if (int32_t remainingManaDamage = conditionManaShield->onDamageTaken(targetPlayer, manaDamage) != 0) { @@ -5594,11 +5594,10 @@ std::vector Game::getMarketItemList(uint16_t wareId, uint16_t sufficientC return {}; } -void Game::forceAddCondition(uint32_t creatureId, Condition* condition) +void Game::forceAddCondition(uint32_t creatureId, const std::shared_ptr& condition) { Creature* creature = getCreatureByID(creatureId); if (!creature) { - delete condition; return; } diff --git a/src/game.h b/src/game.h index 40881a29a1..e29fce1b7a 100644 --- a/src/game.h +++ b/src/game.h @@ -78,7 +78,7 @@ class Game void start(ServiceManager* manager); - void forceAddCondition(uint32_t creatureId, Condition* condition); + void forceAddCondition(uint32_t creatureId, const std::shared_ptr& condition); void forceRemoveCondition(uint32_t creatureId, ConditionType_t type); bool loadMainMap(const std::string& filename); diff --git a/src/iologindata.cpp b/src/iologindata.cpp index 5d90e837b6..0b5865dd15 100644 --- a/src/iologindata.cpp +++ b/src/iologindata.cpp @@ -194,12 +194,10 @@ bool IOLoginData::loadPlayer(Player* player, DBResult_ptr result) PropStream propStream; propStream.init(conditions.data(), conditions.size()); - Condition* condition = Condition::createCondition(propStream); + auto condition = Condition::createCondition(propStream); while (condition) { if (condition->unserialize(propStream)) { player->storedConditionList.push_front(condition); - } else { - delete condition; } condition = Condition::createCondition(propStream); } @@ -636,7 +634,7 @@ bool IOLoginData::savePlayer(Player* player) // serialize conditions PropWriteStream propWriteStream; - for (Condition* condition : player->conditions) { + for (const auto& condition : player->conditions) { if (condition->isPersistent()) { condition->serialize(propWriteStream); propWriteStream.write(CONDITIONATTR_END); diff --git a/src/items.cpp b/src/items.cpp index d037dabcd5..a782d4115e 100644 --- a/src/items.cpp +++ b/src/items.cpp @@ -1657,23 +1657,23 @@ void Items::parseItemNode(const pugi::xml_node& itemNode, uint16_t id) it.type = ITEM_TYPE_MAGICFIELD; CombatType_t combatType = COMBAT_NONE; - ConditionDamage* conditionDamage = nullptr; + std::shared_ptr conditionDamage = nullptr; tmpStrValue = boost::algorithm::to_lower_copy(valueAttribute.as_string()); if (tmpStrValue == "fire") { - conditionDamage = new ConditionDamage(CONDITIONID_COMBAT, CONDITION_FIRE); + conditionDamage = std::make_shared(CONDITIONID_COMBAT, CONDITION_FIRE); combatType = COMBAT_FIREDAMAGE; } else if (tmpStrValue == "energy") { - conditionDamage = new ConditionDamage(CONDITIONID_COMBAT, CONDITION_ENERGY); + conditionDamage = std::make_shared(CONDITIONID_COMBAT, CONDITION_ENERGY); combatType = COMBAT_ENERGYDAMAGE; } else if (tmpStrValue == "poison") { - conditionDamage = new ConditionDamage(CONDITIONID_COMBAT, CONDITION_POISON); + conditionDamage = std::make_shared(CONDITIONID_COMBAT, CONDITION_POISON); combatType = COMBAT_EARTHDAMAGE; } else if (tmpStrValue == "drown") { - conditionDamage = new ConditionDamage(CONDITIONID_COMBAT, CONDITION_DROWN); + conditionDamage = std::make_shared(CONDITIONID_COMBAT, CONDITION_DROWN); combatType = COMBAT_DROWNDAMAGE; } else if (tmpStrValue == "physical") { - conditionDamage = new ConditionDamage(CONDITIONID_COMBAT, CONDITION_BLEEDING); + conditionDamage = std::make_shared(CONDITIONID_COMBAT, CONDITION_BLEEDING); combatType = COMBAT_PHYSICALDAMAGE; } else { std::cout << "[Warning - Items::parseItemNode] Unknown field value: " diff --git a/src/items.h b/src/items.h index 73f6054bff..8538606429 100644 --- a/src/items.h +++ b/src/items.h @@ -332,7 +332,7 @@ class ItemType std::string vocationString; std::unique_ptr abilities; - std::unique_ptr conditionDamage; + std::shared_ptr conditionDamage; uint32_t attackSpeed = 0; uint32_t weight = 0; diff --git a/src/luascript.cpp b/src/luascript.cpp index d26c126a68..61e2ba8e01 100644 --- a/src/luascript.cpp +++ b/src/luascript.cpp @@ -8522,10 +8522,10 @@ int LuaScriptInterface::luaCreatureGetCondition(lua_State* L) ConditionId_t conditionId = tfs::lua::getNumber(L, 3, CONDITIONID_COMBAT); uint32_t subId = tfs::lua::getNumber(L, 4, 0); - Condition* condition = creature->getCondition(conditionType, conditionId, subId); + auto condition = creature->getCondition(conditionType, conditionId, subId); if (condition) { - tfs::lua::pushUserdata(L, condition); - setWeakMetatable(L, -1, "Condition"); + tfs::lua::pushSharedPtr(L, condition); + setMetatable(L, -1, "Condition"); } else { lua_pushnil(L); } @@ -8536,7 +8536,7 @@ int LuaScriptInterface::luaCreatureAddCondition(lua_State* L) { // creature:addCondition(condition[, force = false]) Creature* creature = tfs::lua::getUserdata(L, 1); - Condition* condition = tfs::lua::getUserdata(L, 2); + const auto& condition = tfs::lua::getSharedPtr(L, 2); if (creature && condition) { bool force = tfs::lua::getBoolean(L, 3, false); tfs::lua::pushBoolean(L, creature->addCondition(condition->clone(), force)); @@ -8556,11 +8556,11 @@ int LuaScriptInterface::luaCreatureRemoveCondition(lua_State* L) return 1; } - Condition* creatureCondition = nullptr; + std::shared_ptr creatureCondition = nullptr; bool force = false; if (lua_isuserdata(L, 2)) { - const Condition* const condition = tfs::lua::getUserdata(L, 2); + const auto& const condition = tfs::lua::getSharedPtr(L, 2); const ConditionType_t conditionType = condition->getType(); const ConditionId_t conditionId = condition->getId(); const uint32_t subId = condition->getSubId(); @@ -8609,7 +8609,7 @@ int LuaScriptInterface::luaCreatureIsImmune(lua_State* L) if (isNumber(L, 2)) { tfs::lua::pushBoolean(L, creature->isImmune(tfs::lua::getNumber(L, 2))); - } else if (Condition* condition = tfs::lua::getUserdata(L, 2)) { + } else if (const auto& condition = tfs::lua::getSharedPtr(L, 2)) { tfs::lua::pushBoolean(L, creature->isImmune(condition->getType())); } else { lua_pushnil(L); @@ -14328,7 +14328,7 @@ int LuaScriptInterface::luaCombatAddCondition(lua_State* L) return 1; } - Condition* condition = tfs::lua::getUserdata(L, 2); + const auto& condition = tfs::lua::getSharedPtr(L, 2); if (condition) { combat->addCondition(condition->clone()); tfs::lua::pushBoolean(L, true); @@ -14480,9 +14480,9 @@ int LuaScriptInterface::luaConditionCreate(lua_State* L) ConditionType_t conditionType = tfs::lua::getNumber(L, 2); ConditionId_t conditionId = tfs::lua::getNumber(L, 3, CONDITIONID_COMBAT); - Condition* condition = Condition::createCondition(conditionId, conditionType, 0, 0); + auto condition = Condition::createCondition(conditionId, conditionType, 0, 0); if (condition) { - tfs::lua::pushUserdata(L, condition); + tfs::lua::pushSharedPtr(L, condition); tfs::lua::setMetatable(L, -1, "Condition"); } else { lua_pushnil(L); @@ -14493,10 +14493,9 @@ int LuaScriptInterface::luaConditionCreate(lua_State* L) int LuaScriptInterface::luaConditionDelete(lua_State* L) { // condition:delete() - Condition** conditionPtr = tfs::lua::getRawUserdata(L, 1); - if (conditionPtr && *conditionPtr) { - delete *conditionPtr; - *conditionPtr = nullptr; + auto& condition = tfs::lua::getSharedPtr(L, 1); + if (condition) { + condition.reset(); } return 0; } @@ -14504,7 +14503,7 @@ int LuaScriptInterface::luaConditionDelete(lua_State* L) int LuaScriptInterface::luaConditionGetId(lua_State* L) { // condition:getId() - Condition* condition = tfs::lua::getUserdata(L, 1); + const auto& condition = tfs::lua::getSharedPtr(L, 1); if (condition) { lua_pushnumber(L, condition->getId()); } else { @@ -14516,7 +14515,7 @@ int LuaScriptInterface::luaConditionGetId(lua_State* L) int LuaScriptInterface::luaConditionGetSubId(lua_State* L) { // condition:getSubId() - Condition* condition = tfs::lua::getUserdata(L, 1); + const auto& condition = tfs::lua::getSharedPtr(L, 1); if (condition) { lua_pushnumber(L, condition->getSubId()); } else { @@ -14528,7 +14527,7 @@ int LuaScriptInterface::luaConditionGetSubId(lua_State* L) int LuaScriptInterface::luaConditionGetType(lua_State* L) { // condition:getType() - Condition* condition = tfs::lua::getUserdata(L, 1); + const auto& condition = tfs::lua::getSharedPtr(L, 1); if (condition) { lua_pushnumber(L, condition->getType()); } else { @@ -14540,7 +14539,7 @@ int LuaScriptInterface::luaConditionGetType(lua_State* L) int LuaScriptInterface::luaConditionGetIcons(lua_State* L) { // condition:getIcons() - Condition* condition = tfs::lua::getUserdata(L, 1); + const auto& condition = tfs::lua::getSharedPtr(L, 1); if (condition) { lua_pushnumber(L, condition->getIcons()); } else { @@ -14552,7 +14551,7 @@ int LuaScriptInterface::luaConditionGetIcons(lua_State* L) int LuaScriptInterface::luaConditionGetEndTime(lua_State* L) { // condition:getEndTime() - Condition* condition = tfs::lua::getUserdata(L, 1); + const auto& condition = tfs::lua::getSharedPtr(L, 1); if (condition) { lua_pushnumber(L, condition->getEndTime()); } else { @@ -14564,9 +14563,9 @@ int LuaScriptInterface::luaConditionGetEndTime(lua_State* L) int LuaScriptInterface::luaConditionClone(lua_State* L) { // condition:clone() - Condition* condition = tfs::lua::getUserdata(L, 1); + const auto& condition = tfs::lua::getSharedPtr(L, 1); if (condition) { - tfs::lua::pushUserdata(L, condition->clone()); + tfs::lua::pushSharedPtr(L, condition->clone()); tfs::lua::setMetatable(L, -1, "Condition"); } else { lua_pushnil(L); @@ -14577,7 +14576,7 @@ int LuaScriptInterface::luaConditionClone(lua_State* L) int LuaScriptInterface::luaConditionGetTicks(lua_State* L) { // condition:getTicks() - Condition* condition = tfs::lua::getUserdata(L, 1); + const auto& condition = tfs::lua::getSharedPtr(L, 1); if (condition) { lua_pushnumber(L, condition->getTicks()); } else { @@ -14590,7 +14589,7 @@ int LuaScriptInterface::luaConditionSetTicks(lua_State* L) { // condition:setTicks(ticks) int32_t ticks = tfs::lua::getNumber(L, 2); - Condition* condition = tfs::lua::getUserdata(L, 1); + const auto& condition = tfs::lua::getSharedPtr(L, 1); if (condition) { condition->setTicks(ticks); tfs::lua::pushBoolean(L, true); @@ -14603,7 +14602,7 @@ int LuaScriptInterface::luaConditionSetTicks(lua_State* L) int LuaScriptInterface::luaConditionSetParameter(lua_State* L) { // condition:setParameter(key, value) - Condition* condition = tfs::lua::getUserdata(L, 1); + const auto& condition = tfs::lua::getSharedPtr(L, 1); if (!condition) { lua_pushnil(L); return 1; @@ -14624,7 +14623,7 @@ int LuaScriptInterface::luaConditionSetParameter(lua_State* L) int LuaScriptInterface::luaConditionGetParameter(lua_State* L) { // condition:getParameter(key) - Condition* condition = tfs::lua::getUserdata(L, 1); + const auto& condition = tfs::lua::getSharedPtr(L, 1); if (!condition) { lua_pushnil(L); return 1; @@ -14647,7 +14646,7 @@ int LuaScriptInterface::luaConditionSetFormula(lua_State* L) double maxa = tfs::lua::getNumber(L, 4); double minb = tfs::lua::getNumber(L, 3); double mina = tfs::lua::getNumber(L, 2); - ConditionSpeed* condition = dynamic_cast(tfs::lua::getUserdata(L, 1)); + auto condition = std::dynamic_pointer_cast(tfs::lua::getSharedPtr(L, 1)); if (condition) { condition->setFormulaVars(mina, minb, maxa, maxb); tfs::lua::pushBoolean(L, true); @@ -14675,7 +14674,7 @@ int LuaScriptInterface::luaConditionSetOutfit(lua_State* L) outfit.lookTypeEx = tfs::lua::getNumber(L, 2); } - ConditionOutfit* condition = dynamic_cast(tfs::lua::getUserdata(L, 1)); + auto condition = std::dynamic_pointer_cast(tfs::lua::getSharedPtr(L, 1)); if (condition) { condition->setOutfit(outfit); tfs::lua::pushBoolean(L, true); @@ -14691,7 +14690,7 @@ int LuaScriptInterface::luaConditionAddDamage(lua_State* L) int32_t value = tfs::lua::getNumber(L, 4); int32_t time = tfs::lua::getNumber(L, 3); int32_t rounds = tfs::lua::getNumber(L, 2); - ConditionDamage* condition = dynamic_cast(tfs::lua::getUserdata(L, 1)); + auto condition = std::dynamic_pointer_cast(tfs::lua::getSharedPtr(L, 1)); if (condition) { tfs::lua::pushBoolean(L, condition->addDamage(rounds, time, value)); } else { diff --git a/src/monster.cpp b/src/monster.cpp index ebe613d704..ac1483aef2 100644 --- a/src/monster.cpp +++ b/src/monster.cpp @@ -710,8 +710,9 @@ void Monster::updateIdleStatus() bool idle = false; if (!isSummon() && targetList.empty()) { // check if there are aggressive conditions - idle = std::find_if(conditions.begin(), conditions.end(), - [](Condition* condition) { return condition->isAggressive(); }) == conditions.end(); + idle = std::find_if(conditions.begin(), conditions.end(), [](const std::shared_ptr& condition) { + return condition->isAggressive(); + }) == conditions.end(); } setIdle(idle); diff --git a/src/monsters.cpp b/src/monsters.cpp index d1024f22fe..0d8b28e357 100644 --- a/src/monsters.cpp +++ b/src/monsters.cpp @@ -79,11 +79,12 @@ bool Monsters::reload() return loadFromXml(true); } -ConditionDamage* Monsters::getDamageCondition(ConditionType_t conditionType, int32_t maxDamage, int32_t minDamage, - int32_t startDamage, uint32_t tickInterval) +std::shared_ptr Monsters::getDamageCondition(ConditionType_t conditionType, int32_t maxDamage, + int32_t minDamage, int32_t startDamage, + uint32_t tickInterval) { - ConditionDamage* condition = - static_cast(Condition::createCondition(CONDITIONID_COMBAT, conditionType, 0, 0)); + auto condition = + std::static_pointer_cast(Condition::createCondition(CONDITIONID_COMBAT, conditionType, 0, 0)); condition->setParam(CONDITION_PARAM_TICKINTERVAL, tickInterval); condition->setParam(CONDITION_PARAM_MINVALUE, minDamage); condition->setParam(CONDITION_PARAM_MAXVALUE, maxDamage); diff --git a/src/monsters.h b/src/monsters.h index 5f71a28b81..cb97fd92e0 100644 --- a/src/monsters.h +++ b/src/monsters.h @@ -265,8 +265,8 @@ class Monsters std::map> bestiary; private: - ConditionDamage* getDamageCondition(ConditionType_t conditionType, int32_t maxDamage, int32_t minDamage, - int32_t startDamage, uint32_t tickInterval); + std::shared_ptr getDamageCondition(ConditionType_t conditionType, int32_t maxDamage, + int32_t minDamage, int32_t startDamage, uint32_t tickInterval); bool deserializeSpell(const pugi::xml_node& node, spellBlock_t& sb, const std::string& description = ""); MonsterType* loadMonster(const std::string& file, const std::string& monsterName, bool reloading = false); diff --git a/src/movement.cpp b/src/movement.cpp index 55412fac6c..795c8976a4 100644 --- a/src/movement.cpp +++ b/src/movement.cpp @@ -702,13 +702,12 @@ ReturnValue MoveEvent::EquipItem(MoveEvent* moveEvent, Player* player, Item* ite } if (it.abilities->invisible) { - Condition* condition = Condition::createCondition(static_cast(slot), CONDITION_INVISIBLE, -1, 0); + auto condition = Condition::createCondition(static_cast(slot), CONDITION_INVISIBLE, -1, 0); player->addCondition(condition); } if (it.abilities->manaShield) { - Condition* condition = - Condition::createCondition(static_cast(slot), CONDITION_MANASHIELD, -1, 0); + auto condition = Condition::createCondition(static_cast(slot), CONDITION_MANASHIELD, -1, 0); player->addCondition(condition); } @@ -722,8 +721,7 @@ ReturnValue MoveEvent::EquipItem(MoveEvent* moveEvent, Player* player, Item* ite } if (it.abilities->regeneration) { - Condition* condition = - Condition::createCondition(static_cast(slot), CONDITION_REGENERATION, -1, 0); + auto condition = Condition::createCondition(static_cast(slot), CONDITION_REGENERATION, -1, 0); if (it.abilities->healthGain != 0) { condition->setParam(CONDITION_PARAM_HEALTHGAIN, it.abilities->healthGain); diff --git a/src/player.cpp b/src/player.cpp index 852a216ff0..ef63ae29f3 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -436,7 +436,7 @@ float Player::getDefenseFactor() const uint32_t Player::getClientIcons() const { uint32_t icons = 0; - for (Condition* condition : conditions) { + for (const auto& condition : conditions) { if (!isSuppress(condition->getType())) { icons |= condition->getIcons(); } @@ -1120,7 +1120,7 @@ void Player::onCreatureAppear(Creature* creature, bool isLogin) sendItems(); onEquipInventory(); - for (Condition* condition : storedConditionList) { + for (const auto& condition : storedConditionList) { addCondition(condition); } storedConditionList.clear(); @@ -1159,7 +1159,7 @@ void Player::onCreatureAppear(Creature* creature, bool isLogin) offlineTime = 0; } - for (Condition* condition : getMuteConditions()) { + for (const auto& condition : getMuteConditions()) { condition->setTicks(condition->getTicks() - (offlineTime * 1000)); if (condition->getTicks() <= 0) { removeCondition(condition); @@ -1375,7 +1375,7 @@ void Player::onCreatureMove(Creature* creature, const Tile* newTile, const Posit if (teleport || oldPos.z != newPos.z) { int32_t ticks = getNumber(ConfigManager::STAIRHOP_DELAY); if (ticks > 0) { - if (Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_PACIFIED, ticks, 0)) { + if (const auto& condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_PACIFIED, ticks, 0)) { addCondition(condition); } } @@ -1578,7 +1578,7 @@ uint32_t Player::isMuted() const } int32_t muteTicks = 0; - for (Condition* condition : conditions) { + for (const auto& condition : conditions) { if (condition->getType() == CONDITION_MUTED && condition->getTicks() > muteTicks) { muteTicks = condition->getTicks(); } @@ -1611,7 +1611,7 @@ void Player::removeMessageBuffer() uint32_t muteTime = 5 * muteCount * muteCount; muteCountMap[guid] = muteCount + 1; - Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_MUTED, muteTime * 1000, 0); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_MUTED, muteTime * 1000, 0); addCondition(condition); sendTextMessage(MESSAGE_STATUS_SMALL, fmt::format("You are muted for {:d} seconds.", muteTime)); @@ -2154,13 +2154,12 @@ void Player::death(Creature* lastHitCreature) auto it = conditions.begin(), end = conditions.end(); while (it != end) { - Condition* condition = *it; + std::shared_ptr condition = *it; if (condition->isPersistent()) { it = conditions.erase(it); condition->endCondition(this); onEndCondition(condition->getType()); - delete condition; } else { ++it; } @@ -2170,13 +2169,12 @@ void Player::death(Creature* lastHitCreature) auto it = conditions.begin(), end = conditions.end(); while (it != end) { - Condition* condition = *it; + std::shared_ptr condition = *it; if (condition->isPersistent()) { it = conditions.erase(it); condition->endCondition(this); onEndCondition(condition->getType()); - delete condition; } else { ++it; } @@ -2256,7 +2254,7 @@ void Player::addInFightTicks(bool pzlock /*= false*/) pzLocked = true; } - Condition* condition = + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_INFIGHT, getNumber(ConfigManager::PZ_LOCKED), 0); addCondition(condition); } @@ -3582,7 +3580,7 @@ void Player::onEndCondition(ConditionType_t type) sendIcons(); } -void Player::onCombatRemoveCondition(Condition* condition) +void Player::onCombatRemoveCondition(const std::shared_ptr& condition) { // Creature::onCombatRemoveCondition(condition); if (condition->getId() > 0) { @@ -3753,8 +3751,8 @@ bool Player::onKilledCreature(Creature* target, bool lastHit /* = true*/) if (lastHit && hasCondition(CONDITION_INFIGHT)) { pzLocked = true; - Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_INFIGHT, - getNumber(ConfigManager::WHITE_SKULL_TIME) * 1000, 0); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_INFIGHT, + getNumber(ConfigManager::WHITE_SKULL_TIME) * 1000, 0); addCondition(condition); } } @@ -4668,10 +4666,10 @@ size_t Player::getMaxDepotItems() const return getNumber(isPremium() ? ConfigManager::DEPOT_PREMIUM_LIMIT : ConfigManager::DEPOT_FREE_LIMIT); } -std::forward_list Player::getMuteConditions() const +std::forward_list> Player::getMuteConditions() const { - std::forward_list muteConditions; - for (Condition* condition : conditions) { + std::forward_list> muteConditions; + for (const auto& condition : conditions) { if (condition->getTicks() <= 0) { continue; } @@ -4720,7 +4718,7 @@ void Player::updateRegeneration() return; } - Condition* condition = getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT); + auto condition = getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT); if (condition) { condition->setParam(CONDITION_PARAM_HEALTHGAIN, vocation->getHealthGainAmount()); condition->setParam(CONDITION_PARAM_HEALTHTICKS, vocation->getHealthGainTicks() * 1000); diff --git a/src/player.h b/src/player.h index 572942aeab..06918a366a 100644 --- a/src/player.h +++ b/src/player.h @@ -486,7 +486,7 @@ class Player final : public Creature, public Cylinder void onAddCondition(ConditionType_t type) override; void onAddCombatCondition(ConditionType_t type) override; void onEndCondition(ConditionType_t type) override; - void onCombatRemoveCondition(Condition* condition) override; + void onCombatRemoveCondition(const std::shared_ptr& condition) override; void onAttackedCreature(Creature* target, bool addFightTicks = true) override; void onAttacked() override; void onAttackedCreatureDrainHealth(Creature* target, int32_t points) override; @@ -1116,7 +1116,7 @@ class Player final : public Creature, public Cylinder void setClientLowLevelBonusDisplay(uint16_t value) { clientLowLevelBonusDisplay = value; } private: - std::forward_list getMuteConditions() const; + std::forward_list> getMuteConditions() const; void checkTradeState(const Item* item); bool hasCapacity(const Item* item, uint32_t count) const; @@ -1177,7 +1177,7 @@ class Player final : public Creature, public Cylinder std::forward_list invitePartyList; std::forward_list modalWindows; std::forward_list learnedInstantSpellList; - std::forward_list + std::forward_list> storedConditionList; // TODO: This variable is only temporarily used when logging in, get rid of it somehow std::string name; diff --git a/src/protocolgame.cpp b/src/protocolgame.cpp index 5edfbb3dcb..bffabea376 100644 --- a/src/protocolgame.cpp +++ b/src/protocolgame.cpp @@ -3554,7 +3554,7 @@ void ProtocolGame::AddPlayerStats(NetworkMessage& msg) msg.add(player->getStaminaMinutes()); msg.add(player->getBaseSpeed() / 2); - Condition* condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT); + auto condition = player->getCondition(CONDITION_REGENERATION, CONDITIONID_DEFAULT); msg.add(condition ? condition->getTicks() / 1000 : 0x00); msg.add(player->getOfflineTrainingTime() / 60 / 1000); @@ -3562,8 +3562,8 @@ void ProtocolGame::AddPlayerStats(NetworkMessage& msg) msg.add(0); // xp boost time (seconds) msg.addByte(0x00); // enables exp boost in the store - if (ConditionManaShield* conditionManaShield = - dynamic_cast(player->getCondition(CONDITION_MANASHIELD_BREAKABLE))) { + if (auto conditionManaShield = + std::dynamic_pointer_cast(player->getCondition(CONDITION_MANASHIELD_BREAKABLE))) { msg.add(conditionManaShield->getManaShield()); msg.add(conditionManaShield->getMaxManaShield()); } else { diff --git a/src/spells.cpp b/src/spells.cpp index 19433cade3..27d2446597 100644 --- a/src/spells.cpp +++ b/src/spells.cpp @@ -737,20 +737,20 @@ void Spell::postCastSpell(Player* player, bool finishedCast /*= true*/, bool pay if (finishedCast) { if (!player->hasFlag(PlayerFlag_HasNoExhaustion)) { if (cooldown > 0) { - Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLCOOLDOWN, - cooldown, 0, false, spellId); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLCOOLDOWN, cooldown, 0, + false, spellId); player->addCondition(condition); } if (groupCooldown > 0) { - Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, - groupCooldown, 0, false, group); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, + groupCooldown, 0, false, group); player->addCondition(condition); } if (secondaryGroupCooldown > 0) { - Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, - secondaryGroupCooldown, 0, false, secondaryGroup); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, + secondaryGroupCooldown, 0, false, secondaryGroup); player->addCondition(condition); } } @@ -853,21 +853,20 @@ bool InstantSpell::playerCastInstant(Player* player, std::string& param) if (!target || target->isRemoved() || target->isDead()) { if (!casterTargetOrDirection) { if (cooldown > 0) { - Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLCOOLDOWN, - cooldown, 0, false, spellId); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLCOOLDOWN, + cooldown, 0, false, spellId); player->addCondition(condition); } if (groupCooldown > 0) { - Condition* condition = Condition::createCondition( - CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, groupCooldown, 0, false, group); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, + groupCooldown, 0, false, group); player->addCondition(condition); } if (secondaryGroupCooldown > 0) { - Condition* condition = - Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, - secondaryGroupCooldown, 0, false, secondaryGroup); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, + secondaryGroupCooldown, 0, false, secondaryGroup); player->addCondition(condition); } @@ -917,20 +916,20 @@ bool InstantSpell::playerCastInstant(Player* player, std::string& param) if (ret != RETURNVALUE_NOERROR) { if (cooldown > 0) { - Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLCOOLDOWN, - cooldown, 0, false, spellId); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLCOOLDOWN, cooldown, + 0, false, spellId); player->addCondition(condition); } if (groupCooldown > 0) { - Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, - groupCooldown, 0, false, group); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, + groupCooldown, 0, false, group); player->addCondition(condition); } if (secondaryGroupCooldown > 0) { - Condition* condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, - secondaryGroupCooldown, 0, false, secondaryGroup); + auto condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, + secondaryGroupCooldown, 0, false, secondaryGroup); player->addCondition(condition); }