1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Get rid of battleGetPossibleTargets.

This commit is contained in:
AlexVinS 2016-03-01 08:49:29 +03:00
parent 8d2aa2c8c7
commit 3b2a45c8dc
4 changed files with 49 additions and 66 deletions

View File

@ -430,7 +430,7 @@ void CBattleAI::attemptCastingSpell()
std::vector<PossibleSpellcast> possibleCasts;
for(auto spell : possibleSpells)
{
for(auto hex : getTargetsToConsider(spell))
for(auto hex : getTargetsToConsider(spell, hero))
{
PossibleSpellcast ps = {spell, hex};
possibleCasts.push_back(ps);
@ -527,24 +527,52 @@ void CBattleAI::attemptCastingSpell()
cb->battleMakeAction(&spellcast);
}
std::vector<BattleHex> CBattleAI::getTargetsToConsider( const CSpell *spell ) const
std::vector<BattleHex> CBattleAI::getTargetsToConsider(const CSpell * spell, const ISpellCaster * caster) const
{
if(spell->getTargetType() == CSpell::NO_TARGET)
const CSpell::TargetInfo targetInfo(spell, caster->getSpellSchoolLevel(spell));
std::vector<BattleHex> ret;
if(targetInfo.massive || targetInfo.type == CSpell::NO_TARGET)
{
//Spell can be cast anywhere, all hexes are potentially considerable.
std::vector<BattleHex> ret;
for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
if(BattleHex(i).isAvailable())
ret.push_back(i);
return ret;
ret.push_back(BattleHex());
}
else
{
//TODO when massive effect -> doesn't matter where cast
return cbc->battleGetPossibleTargets(playerID, spell);
switch(targetInfo.type)
{
case CSpell::CREATURE:
{
for(const CStack * stack : cbc->battleAliveStacks())
{
bool immune = ESpellCastProblem::OK != spell->isImmuneByStack(caster, stack);
bool casterStack = stack->owner == caster->getOwner();
if(!immune)
switch (spell->positiveness)
{
case CSpell::POSITIVE:
if(casterStack || targetInfo.smart)
ret.push_back(stack->position);
break;
case CSpell::NEUTRAL:
ret.push_back(stack->position);
break;
case CSpell::NEGATIVE:
if(!casterStack || targetInfo.smart)
ret.push_back(stack->position);
break;
}
}
}
break;
default:
break;
}
}
return ret;
}
boost::optional<BattleAction> CBattleAI::considerFleeingOrSurrendering()

View File

@ -51,11 +51,11 @@ static bool willSecondHexBlockMoreEnemyShooters(const BattleHex &h1, const Battl
struct ThreatMap
{
{
std::array<std::vector<BattleAttackInfo>, GameConstants::BFIELD_SIZE> threatMap; // [hexNr] -> enemies able to strike
const CStack *endangered;
std::array<int, GameConstants::BFIELD_SIZE> sufferedDamage;
const CStack *endangered;
std::array<int, GameConstants::BFIELD_SIZE> sufferedDamage;
ThreatMap(const CStack *Endangered);
};
@ -89,7 +89,7 @@ const Val getValOr(const std::map<Key, Val> &Map, const Key &key, const Val2 def
auto i = Map.find(key);
if(i != Map.end())
return i->second;
else
else
return defaultValue;
}
@ -111,8 +111,8 @@ class CBattleAI : public CBattleGameInterface
{
int side;
std::shared_ptr<CBattleCallback> cb;
//Previous setting of cb
//Previous setting of cb
bool wasWaitingForRealize, wasUnlockingGs;
void print(const std::string &text) const;
@ -148,6 +148,6 @@ public:
boost::optional<BattleAction> considerFleeingOrSurrendering();
void attemptCastingSpell();
std::vector<BattleHex> getTargetsToConsider(const CSpell *spell) const;
std::vector<BattleHex> getTargetsToConsider(const CSpell *spell, const ISpellCaster * caster) const;
};

View File

@ -1739,50 +1739,6 @@ ESpellCastProblem::ESpellCastProblem CBattleInfoCallback::battleCanCastThisSpell
return ESpellCastProblem::OK;
}
std::vector<BattleHex> CBattleInfoCallback::battleGetPossibleTargets(PlayerColor player, const CSpell *spell) const
{
std::vector<BattleHex> ret;
RETURN_IF_NOT_BATTLE(ret);
switch(spell->getTargetType())
{
case CSpell::CREATURE:
{
const CGHeroInstance * caster = battleGetFightingHero(playerToSide(player)); //TODO
const CSpell::TargetInfo ti(spell, caster->getSpellSchoolLevel(spell));
for(const CStack * stack : battleAliveStacks())
{
bool immune = ESpellCastProblem::OK != spell->isImmuneByStack(caster, stack);
bool casterStack = stack->owner == caster->getOwner();
if(!immune)
switch (spell->positiveness)
{
case CSpell::POSITIVE:
if(casterStack || ti.smart)
ret.push_back(stack->position);
break;
case CSpell::NEUTRAL:
ret.push_back(stack->position);
break;
case CSpell::NEGATIVE:
if(!casterStack || ti.smart)
ret.push_back(stack->position);
break;
}
}
}
break;
default:
logGlobal->errorStream() << "FIXME " << __FUNCTION__ << " doesn't work with target type " << spell->getTargetType();
}
return ret;
}
ui32 CBattleInfoCallback::battleGetSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
{
RETURN_IF_NOT_BATTLE(-1);

View File

@ -284,7 +284,6 @@ public:
ESpellCastProblem::ESpellCastProblem battleCanCastSpell(PlayerColor player, 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
ESpellCastProblem::ESpellCastProblem battleCanCastThisSpellHere(const ISpellCaster * caster, const CSpell * spell, ECastingMode::ECastingMode mode, BattleHex dest) const; //checks if given player can cast given spell at given tile in given mode
std::vector<BattleHex> battleGetPossibleTargets(PlayerColor player, const CSpell *spell) const;
SpellID battleGetRandomStackSpell(const CStack * stack, ERandomSpell mode) const;
SpellID getRandomBeneficialSpell(const CStack * subject) const;