Răsfoiți Sursa

Move SACRIFICE target existence check to mechanics

AlexVinS 10 ani în urmă
părinte
comite
10668974d6

+ 2 - 15
lib/CBattleCallback.cpp

@@ -1635,26 +1635,13 @@ ESpellCastProblem::ESpellCastProblem CBattleInfoCallback::battleCanCastThisSpell
 			const CGHeroInstance * caster = battleGetFightingHero(side);
 			const CSpell::TargetInfo ti = spell->getTargetInfo(caster->getSpellSchoolLevel(spell));
 			bool targetExists = false;
-            bool targetToSacrificeExists = false; // for sacrifice we have to check for 2 targets (one dead to resurrect and one living to destroy)
 
             for(const CStack * stack : battleGetAllStacks()) //dead stacks will be immune anyway
 			{
 				bool immune =  ESpellCastProblem::OK != spell->isImmuneByStack(caster, stack);
 				bool casterStack = stack->owner == caster->getOwner();
 				
-                if(spell->id == SpellID::SACRIFICE)
-                {
-                    if(!immune && casterStack)
-                    {
-                        if(stack->alive())
-                            targetToSacrificeExists = true;
-                        else
-                            targetExists = true;
-                        if(targetExists && targetToSacrificeExists)
-                            break;
-                    }
-                }
-                else if(!immune)
+                if(!immune)
                 {
 					switch (spell->positiveness)
 					{
@@ -1678,7 +1665,7 @@ ESpellCastProblem::ESpellCastProblem CBattleInfoCallback::battleCanCastThisSpell
 					}
                 }
 			}
-            if(!targetExists || (spell->id == SpellID::SACRIFICE && !targetExists && !targetToSacrificeExists))
+            if(!targetExists)
 			{
 				return ESpellCastProblem::NO_APPROPRIATE_TARGET;
 			}

+ 34 - 0
lib/spells/BattleSpellMechanics.cpp

@@ -418,6 +418,40 @@ void RemoveObstacleMechanics::applyBattleEffects(const SpellCastEnvironment * en
 }
 
 ///SpecialRisingSpellMechanics
+ESpellCastProblem::ESpellCastProblem SacrificeMechanics::canBeCasted(const CBattleInfoCallback * cb, PlayerColor player) const
+{
+	// for sacrifice we have to check for 2 targets (one dead to resurrect and one living to destroy)
+	
+	bool targetExists = false;
+	bool targetToSacrificeExists = false;
+
+	for(const CStack * stack : cb->battleGetAllStacks())
+	{
+		//using isImmuneBy directly as this mechanics does not have overridden immunity check
+		//therefore we do not need to check caster and casting mode
+		//TODO: check that we really should check immunity for both stacks
+		const bool immune =  ESpellCastProblem::OK != owner->isImmuneBy(stack);
+		const bool casterStack = stack->owner == player;
+
+		if(!immune && casterStack)
+		{
+			if(stack->alive())
+				targetToSacrificeExists = true;
+			else
+				targetExists = true;
+			if(targetExists && targetToSacrificeExists)
+				break;
+		}
+		
+	}
+	
+	if(targetExists && targetToSacrificeExists)
+		return ESpellCastProblem::OK;
+	else
+		return ESpellCastProblem::NO_APPROPRIATE_TARGET;
+}
+
+
 void SacrificeMechanics::applyBattleEffects(const SpellCastEnvironment * env, BattleSpellCastParameters & parameters, SpellCastContext & ctx) const
 {
 	RisingSpellMechanics::applyBattleEffects(env, parameters, ctx);

+ 3 - 1
lib/spells/BattleSpellMechanics.h

@@ -96,6 +96,8 @@ class DLL_LINKAGE SacrificeMechanics : public RisingSpellMechanics
 {
 public:
 	SacrificeMechanics(CSpell * s): RisingSpellMechanics(s){};
+
+	ESpellCastProblem::ESpellCastProblem canBeCasted(const CBattleInfoCallback * cb, PlayerColor player) const override;
 protected:
 	void applyBattleEffects(const SpellCastEnvironment * env, BattleSpellCastParameters & parameters, SpellCastContext & ctx) const override;
 };
@@ -112,7 +114,7 @@ class DLL_LINKAGE SummonMechanics : public DefaultSpellMechanics
 {
 public:
 	SummonMechanics(CSpell * s): DefaultSpellMechanics(s){};
-	
+
 	ESpellCastProblem::ESpellCastProblem canBeCasted(const CBattleInfoCallback * cb, PlayerColor player) const override;	
 protected:
 	void applyBattleEffects(const SpellCastEnvironment * env, BattleSpellCastParameters & parameters, SpellCastContext & ctx) const override;