1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-06 09:09:40 +02:00

get rid of CBattleInfoCallback::battleCanCastThisSpell

This commit is contained in:
AlexVinS
2017-06-05 23:46:55 +03:00
parent 6c308956f9
commit 195e979a18
7 changed files with 30 additions and 44 deletions

View File

@@ -1708,43 +1708,6 @@ std::vector<BattleHex> CBattleInfoCallback::getAttackableBattleHexes() const
return attackableBattleHexes;
}
ESpellCastProblem::ESpellCastProblem CBattleInfoCallback::battleCanCastThisSpell(const ISpellCaster * caster, const CSpell * spell, ECastingMode::ECastingMode mode) const
{
RETURN_IF_NOT_BATTLE(ESpellCastProblem::INVALID);
if(caster == nullptr)
{
logGlobal->errorStream() << "CBattleInfoCallback::battleCanCastThisSpell: no spellcaster.";
return ESpellCastProblem::INVALID;
}
ESpellCastProblem::ESpellCastProblem genProblem = battleCanCastSpell(caster, mode);
if(genProblem != ESpellCastProblem::OK)
return genProblem;
switch(mode)
{
case ECastingMode::HERO_CASTING:
{
const CGHeroInstance * castingHero = dynamic_cast<const CGHeroInstance *>(caster);//todo: unify hero|creature spell cost
if(!castingHero)
{
logGlobal->error("battleCanCastThisSpell: invalid caster");
return ESpellCastProblem::INVALID;
}
if(!castingHero->getArt(ArtifactPosition::SPELLBOOK))
return ESpellCastProblem::NO_SPELLBOOK;
if(!castingHero->canCastThisSpell(spell))
return ESpellCastProblem::HERO_DOESNT_KNOW_SPELL;
if(castingHero->mana < battleGetSpellCost(spell, castingHero)) //not enough mana
return ESpellCastProblem::NOT_ENOUGH_MANA;
}
break;
}
return spell->canBeCast(this, mode, caster);
}
ui32 CBattleInfoCallback::battleGetSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
{
RETURN_IF_NOT_BATTLE(-1);

View File

@@ -293,7 +293,6 @@ public:
si8 battleMaxSpellLevel(ui8 side) const; //calculates minimum spell level possible to be cast on battlefield - takes into account artifacts of both heroes; if no effects are set, 0 is returned
ui32 battleGetSpellCost(const CSpell * sp, const CGHeroInstance * caster) const; //returns cost of given spell
ESpellCastProblem::ESpellCastProblem battleCanCastSpell(const ISpellCaster * caster, ECastingMode::ECastingMode mode) const; //returns true if there are no general issues preventing from casting a spell
ESpellCastProblem::ESpellCastProblem battleCanCastThisSpell(const ISpellCaster * caster, const CSpell * spell, ECastingMode::ECastingMode mode) const; //checks if given player can cast given spell
SpellID battleGetRandomStackSpell(CRandomGenerator & rand, const CStack * stack, ERandomSpell mode) const;
SpellID getRandomBeneficialSpell(CRandomGenerator & rand, const CStack * subject) const;

View File

@@ -157,6 +157,31 @@ ui32 CSpell::calculateDamage(const ISpellCaster * caster, const CStack * affecte
ESpellCastProblem::ESpellCastProblem CSpell::canBeCast(const CBattleInfoCallback * cb, ECastingMode::ECastingMode mode, const ISpellCaster * caster) const
{
ESpellCastProblem::ESpellCastProblem genProblem = cb->battleCanCastSpell(caster, mode);
if(genProblem != ESpellCastProblem::OK)
return genProblem;
switch(mode)
{
case ECastingMode::HERO_CASTING:
{
const CGHeroInstance * castingHero = dynamic_cast<const CGHeroInstance *>(caster);//todo: unify hero|creature spell cost
if(!castingHero)
{
logGlobal->debug("CSpell::canBeCast: invalid caster");
return ESpellCastProblem::NO_HERO_TO_CAST_SPELL;
}
if(!castingHero->getArt(ArtifactPosition::SPELLBOOK))
return ESpellCastProblem::NO_SPELLBOOK;
if(!castingHero->canCastThisSpell(this))
return ESpellCastProblem::HERO_DOESNT_KNOW_SPELL;
if(castingHero->mana < cb->battleGetSpellCost(this, castingHero)) //not enough mana
return ESpellCastProblem::NOT_ENOUGH_MANA;
}
break;
}
if(!isCombatSpell())
return ESpellCastProblem::ADVMAP_SPELL_INSTEAD_OF_BATTLE_SPELL;
@@ -383,7 +408,7 @@ void CSpell::getEffects(std::vector<Bonus> & lst, const int level) const
ESpellCastProblem::ESpellCastProblem CSpell::canBeCastAt(const CBattleInfoCallback * cb, const ISpellCaster * caster, ECastingMode::ECastingMode mode, BattleHex destination) const
{
ESpellCastProblem::ESpellCastProblem problem = cb->battleCanCastThisSpell(caster, this, mode);
ESpellCastProblem::ESpellCastProblem problem = canBeCast(cb, mode, caster);
if(problem != ESpellCastProblem::OK)
return problem;

View File

@@ -89,7 +89,7 @@ void BattleSpellCastParameters::cast(const SpellCastEnvironment * env)
bool BattleSpellCastParameters::castIfPossible(const SpellCastEnvironment * env)
{
if(ESpellCastProblem::OK == cb->battleCanCastThisSpell(caster, spell, mode))
if(ESpellCastProblem::OK == spell->canBeCast(cb, mode, caster))
{
cast(env);
return true;