From d90d00eeac9601fefb65ef26682fd9c3c5337598 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Sun, 19 Mar 2023 02:30:06 +0300 Subject: [PATCH] vcmi: rework ProxyCaster Now ProxyCaster can be used without hero and can even cast something with default values. --- lib/spells/AbilityCaster.cpp | 4 +-- lib/spells/AbilityCaster.h | 1 - lib/spells/BonusCaster.cpp | 2 -- lib/spells/BonusCaster.h | 1 - lib/spells/ProxyCaster.cpp | 56 ++++++++++++++++++++++++++++-------- lib/spells/ProxyCaster.h | 2 +- 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/lib/spells/AbilityCaster.cpp b/lib/spells/AbilityCaster.cpp index 1e9d23c3c..58a5017df 100644 --- a/lib/spells/AbilityCaster.cpp +++ b/lib/spells/AbilityCaster.cpp @@ -22,7 +22,6 @@ namespace spells AbilityCaster::AbilityCaster(const battle::Unit * actualCaster_, int32_t baseSpellLevel_) : ProxyCaster(actualCaster_), - actualCaster(actualCaster_), baseSpellLevel(baseSpellLevel_) { } @@ -32,10 +31,11 @@ AbilityCaster::~AbilityCaster() = default; int32_t AbilityCaster::getSpellSchoolLevel(const Spell * spell, int32_t * outSelectedSchool) const { auto skill = baseSpellLevel; + const auto * unit = dynamic_cast(actualCaster); if(spell->getLevel() > 0) { - vstd::amax(skill, actualCaster->valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 0)); + vstd::amax(skill, unit->valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 0)); } vstd::amax(skill, 0); diff --git a/lib/spells/AbilityCaster.h b/lib/spells/AbilityCaster.h index 2b9c7c2ff..bbfd723d7 100644 --- a/lib/spells/AbilityCaster.h +++ b/lib/spells/AbilityCaster.h @@ -29,7 +29,6 @@ public: void spendMana(ServerCallback * server, const int32_t spellCost) const override; private: - const battle::Unit * actualCaster; int32_t baseSpellLevel; }; diff --git a/lib/spells/BonusCaster.cpp b/lib/spells/BonusCaster.cpp index f3eaaa66e..8e90e658b 100644 --- a/lib/spells/BonusCaster.cpp +++ b/lib/spells/BonusCaster.cpp @@ -24,10 +24,8 @@ namespace spells BonusCaster::BonusCaster(const Caster * actualCaster_, std::shared_ptr bonus_): ProxyCaster(actualCaster_), - actualCaster(actualCaster_), bonus(std::move(bonus_)) { - } BonusCaster::~BonusCaster() = default; diff --git a/lib/spells/BonusCaster.h b/lib/spells/BonusCaster.h index 12f888b77..a8e55f276 100644 --- a/lib/spells/BonusCaster.h +++ b/lib/spells/BonusCaster.h @@ -30,7 +30,6 @@ public: void spendMana(ServerCallback * server, const int spellCost) const override; private: - const Caster * actualCaster; std::shared_ptr bonus; }; diff --git a/lib/spells/ProxyCaster.cpp b/lib/spells/ProxyCaster.cpp index e93afe58c..cc21d94db 100644 --- a/lib/spells/ProxyCaster.cpp +++ b/lib/spells/ProxyCaster.cpp @@ -13,6 +13,8 @@ #include "../GameConstants.h" +#include + VCMI_LIB_NAMESPACE_BEGIN namespace spells @@ -28,62 +30,92 @@ ProxyCaster::~ProxyCaster() = default; int32_t ProxyCaster::getCasterUnitId() const { - return actualCaster->getCasterUnitId(); + if(actualCaster) + return actualCaster->getCasterUnitId(); + + return -1; } int32_t ProxyCaster::getSpellSchoolLevel(const Spell * spell, int32_t * outSelectedSchool) const { - return actualCaster->getSpellSchoolLevel(spell, outSelectedSchool); + if(actualCaster) + return actualCaster->getSpellSchoolLevel(spell, outSelectedSchool); + + return 0; } int32_t ProxyCaster::getEffectLevel(const Spell * spell) const { - return actualCaster->getEffectLevel(spell); + if(actualCaster) + return actualCaster->getEffectLevel(spell); + + return 0; } int64_t ProxyCaster::getSpellBonus(const Spell * spell, int64_t base, const battle::Unit * affectedStack) const { - return actualCaster->getSpellBonus(spell, base, affectedStack); + if(actualCaster) + return actualCaster->getSpellBonus(spell, base, affectedStack); + + return base; } int64_t ProxyCaster::getSpecificSpellBonus(const Spell * spell, int64_t base) const { - return actualCaster->getSpecificSpellBonus(spell, base); + if(actualCaster) + return actualCaster->getSpecificSpellBonus(spell, base); + + return base; } int32_t ProxyCaster::getEffectPower(const Spell * spell) const { - return actualCaster->getEffectPower(spell); + if(actualCaster) + return actualCaster->getEffectPower(spell); + + return spell->getLevelPower(getEffectLevel(spell)); } int32_t ProxyCaster::getEnchantPower(const Spell * spell) const { - return actualCaster->getEnchantPower(spell); + if(actualCaster) + return actualCaster->getEnchantPower(spell); + + return spell->getLevelPower(getEffectLevel(spell)); } int64_t ProxyCaster::getEffectValue(const Spell * spell) const { - return actualCaster->getEffectValue(spell); + if(actualCaster) + return actualCaster->getEffectValue(spell); + + return 0; } PlayerColor ProxyCaster::getCasterOwner() const { - return actualCaster->getCasterOwner(); + if(actualCaster) + return actualCaster->getCasterOwner(); + + return PlayerColor::CANNOT_DETERMINE; } void ProxyCaster::getCasterName(MetaString & text) const { - return actualCaster->getCasterName(text); + if(actualCaster) + actualCaster->getCasterName(text); } void ProxyCaster::getCastDescription(const Spell * spell, const std::vector & attacked, MetaString & text) const { - actualCaster->getCastDescription(spell, attacked, text); + if(actualCaster) + actualCaster->getCastDescription(spell, attacked, text); } void ProxyCaster::spendMana(ServerCallback * server, const int32_t spellCost) const { - actualCaster->spendMana(server, spellCost); + if(actualCaster) + actualCaster->spendMana(server, spellCost); } } diff --git a/lib/spells/ProxyCaster.h b/lib/spells/ProxyCaster.h index b3829da7f..043cd15d2 100644 --- a/lib/spells/ProxyCaster.h +++ b/lib/spells/ProxyCaster.h @@ -36,7 +36,7 @@ public: void getCastDescription(const Spell * spell, const std::vector & attacked, MetaString & text) const override; void spendMana(ServerCallback * server, const int32_t spellCost) const override; -private: +protected: const Caster * actualCaster; };