1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-03 13:01:33 +02:00

rewrite CGHeroInstance::canCastThisSpell

This commit is contained in:
AlexVinS 2014-11-13 17:57:35 +03:00
parent 3fcb1254f7
commit 7af4f44237
3 changed files with 46 additions and 34 deletions

View File

@ -65,6 +65,39 @@ CSpell::~CSpell()
delete mechanics;
}
bool CSpell::isCastableBy(const IBonusBearer * caster, bool hasSpellBook, const std::set<SpellID> & spellBook) const
{
if(!hasSpellBook)
return false;
const bool inSpellBook = vstd::contains(spellBook, id);
const bool isBonus = caster->hasBonusOfType(Bonus::SPELL, id);
bool inTome = false;
for(const SpellSchoolInfo & cnf : SPELL_SCHOOL_CONFIG)
{
if(school.at(cnf.id) && caster->hasBonusOfType(cnf.knoledgeBonus))
{
inTome = true;
break;
}
}
if (isSpecialSpell())
{
if (inSpellBook)
{//hero has this spell in spellbook
logGlobal->errorStream() << "Special spell in spellbook "<<name;
}
return isBonus;
}
else
{
return inSpellBook || inTome || isBonus || caster->hasBonusOfType(Bonus::SPELLS_OF_LEVEL, level);
}
}
const CSpell::LevelInfo & CSpell::getLevelInfo(const int level) const
{
if(level < 0 || level >= GameConstants::SPELL_SCHOOL_LEVELS)

View File

@ -36,7 +36,8 @@ struct SpellSchoolInfo
Bonus::BonusType damagePremyBonus;
Bonus::BonusType immunityBonus;
std::string jsonName;
SecondarySkill::ESecondarySkill skill;
SecondarySkill::ESecondarySkill skill;
Bonus::BonusType knoledgeBonus;
};
static const SpellSchoolInfo SPELL_SCHOOL_CONFIG[4] =
@ -46,28 +47,32 @@ static const SpellSchoolInfo SPELL_SCHOOL_CONFIG[4] =
Bonus::AIR_SPELL_DMG_PREMY,
Bonus::AIR_IMMUNITY,
"air",
SecondarySkill::AIR_MAGIC
SecondarySkill::AIR_MAGIC,
Bonus::AIR_SPELLS
},
{
ESpellSchool::FIRE,
Bonus::FIRE_SPELL_DMG_PREMY,
Bonus::FIRE_IMMUNITY,
"fire",
SecondarySkill::FIRE_MAGIC
SecondarySkill::FIRE_MAGIC,
Bonus::FIRE_SPELLS
},
{
ESpellSchool::WATER,
Bonus::WATER_SPELL_DMG_PREMY,
Bonus::WATER_IMMUNITY,
"water",
SecondarySkill::WATER_MAGIC
SecondarySkill::WATER_MAGIC,
Bonus::WATER_SPELLS
},
{
ESpellSchool::EARTH,
Bonus::EARTH_SPELL_DMG_PREMY,
Bonus::EARTH_IMMUNITY,
"earth",
SecondarySkill::EARTH_MAGIC
SecondarySkill::EARTH_MAGIC,
Bonus::EARTH_SPELLS
}
};
@ -178,6 +183,8 @@ public:
CSpell();
~CSpell();
bool isCastableBy(const IBonusBearer * caster, bool hasSpellBook, const std::set<SpellID> & spellBook) const;
std::vector<BattleHex> rangeInHexes(BattleHex centralHex, ui8 schoolLvl, ui8 side, bool *outDroppedHexes = nullptr ) const; //convert range to specific hexes; last optional out parameter is set to true, if spell would cover unavailable hexes (that are not included in ret)
si16 mainEffectAnim; //main spell effect animation, in AC format (or -1 when none)

View File

@ -888,35 +888,7 @@ ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell, int *outSelectedSc
bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
{
if(!getArt(ArtifactPosition::SPELLBOOK)) //if hero has no spellbook
return false;
if (spell->isSpecialSpell())
{
if (vstd::contains(spells, spell->id))
{//hero has this spell in spellbook
logGlobal->errorStream() << "Special spell in spellbook "<<spell->name;
}
if (hasBonusOfType(Bonus::SPELL, spell->id))
return true;
return false;
}
else
{
if(vstd::contains(spells, spell->id) //hero has this spell in spellbook
|| (spell->air && hasBonusOfType(Bonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
|| (spell->fire && hasBonusOfType(Bonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
|| (spell->water && hasBonusOfType(Bonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
|| (spell->earth && hasBonusOfType(Bonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
|| hasBonusOfType(Bonus::SPELL, spell->id)
|| hasBonusOfType(Bonus::SPELLS_OF_LEVEL, spell->level)
)
return true;
return false;
}
return spell->isCastableBy(this, nullptr !=getArt(ArtifactPosition::SPELLBOOK), spells);
}
/**