1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

Move SACRIFICE target existence check to mechanics

This commit is contained in:
AlexVinS 2015-04-03 00:35:29 +03:00
parent 0cce1ef2f1
commit 10668974d6
3 changed files with 39 additions and 16 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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;