1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-27 21:49:10 +02:00

Optimize battleAdjacentUnits method

This commit is contained in:
Ivan Savenko 2025-01-10 14:27:12 +00:00
parent a44bbf4527
commit 5375d61d1b
2 changed files with 14 additions and 10 deletions

View File

@ -1716,18 +1716,22 @@ bool CBattleInfoCallback::battleIsUnitBlocked(const battle::Unit * unit) const
return false;
}
std::set<const battle::Unit *> CBattleInfoCallback::battleAdjacentUnits(const battle::Unit * unit) const
battle::Units CBattleInfoCallback::battleAdjacentUnits(const battle::Unit * unit) const
{
std::set<const battle::Unit *> ret;
RETURN_IF_NOT_BATTLE(ret);
RETURN_IF_NOT_BATTLE({});
for(auto hex : unit->getSurroundingHexes())
const auto & hexes = unit->getSurroundingHexes();
const auto & units = battleGetUnitsIf([=](const battle::Unit * unit)
{
if(const auto * neighbour = battleGetUnitByPos(hex, true))
ret.insert(neighbour);
}
const auto & unitHexes = unit->getHexes();
for (const auto & hex : unitHexes)
if (hexes.contains(hex))
return true;
return false;
});
return ret;
return units;
}
SpellID CBattleInfoCallback::getRandomBeneficialSpell(vstd::RNG & rand, const battle::Unit * caster, const battle::Unit * subject) const

View File

@ -95,7 +95,7 @@ public:
bool battleCanShoot(const battle::Unit * attacker, BattleHex dest) const; //determines if stack with given ID shoot at the selected destination
bool battleCanShoot(const battle::Unit * attacker) const; //determines if stack with given ID shoot in principle
bool battleIsUnitBlocked(const battle::Unit * unit) const; //returns true if there is neighboring enemy stack
std::set<const battle::Unit *> battleAdjacentUnits(const battle::Unit * unit) const;
battle::Units battleAdjacentUnits(const battle::Unit * unit) const;
DamageEstimation calculateDmgRange(const BattleAttackInfo & info) const;
@ -173,4 +173,4 @@ protected:
BattleHexArray getStoppers(BattleSide whichSidePerspective) const; //get hexes with stopping obstacles (quicksands)
};
VCMI_LIB_NAMESPACE_END
VCMI_LIB_NAMESPACE_END