Explorar o código

vcmi: rework ProxyCaster

Now ProxyCaster can be used without hero and can even cast something
with default values.
Konstantin %!s(int64=2) %!d(string=hai) anos
pai
achega
d90d00eeac

+ 2 - 2
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<const battle::Unit*>(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);

+ 0 - 1
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;
 };
 

+ 0 - 2
lib/spells/BonusCaster.cpp

@@ -24,10 +24,8 @@ namespace spells
 
 BonusCaster::BonusCaster(const Caster * actualCaster_, std::shared_ptr<Bonus> bonus_):
 	ProxyCaster(actualCaster_),
-	actualCaster(actualCaster_),
 	bonus(std::move(bonus_))
 {
-
 }
 
 BonusCaster::~BonusCaster() = default;

+ 0 - 1
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> bonus;
 };
 

+ 44 - 12
lib/spells/ProxyCaster.cpp

@@ -13,6 +13,8 @@
 
 #include "../GameConstants.h"
 
+#include <vcmi/spells/Spell.h>
+
 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<const battle::Unit*> & 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);
 }
 
 }

+ 1 - 1
lib/spells/ProxyCaster.h

@@ -36,7 +36,7 @@ public:
 	void getCastDescription(const Spell * spell, const std::vector<const battle::Unit *> & attacked, MetaString & text) const override;
 	void spendMana(ServerCallback * server, const int32_t spellCost) const override;
 
-private:
+protected:
 	const Caster * actualCaster;
 };