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

BattleAI: damage cache and switch to different model of spells evaluation

This commit is contained in:
Andrii Danylchenko 2023-08-08 18:37:42 +03:00
parent 4500e59713
commit 274bf739b8
11 changed files with 407 additions and 216 deletions

View File

@ -18,6 +18,81 @@ uint64_t averageDmg(const DamageRange & range)
return (range.min + range.max) / 2;
}
void DamageCache::cacheDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb)
{
auto damage = averageDmg(hb->battleEstimateDamage(attacker, defender, 0).damage);
damageCache[attacker->unitId()][defender->unitId()] = damage / attacker->getCount();
}
void DamageCache::buildDamageCache(std::shared_ptr<HypotheticBattle> hb, int side)
{
auto stacks = hb->battleGetUnitsIf([=](const battle::Unit * u) -> bool
{
return true;
});
std::vector<const battle::Unit *> ourUnits, enemyUnits;
for(auto stack : stacks)
{
if(stack->unitSide() == side)
ourUnits.push_back(stack);
else
enemyUnits.push_back(stack);
}
for(auto ourUnit : ourUnits)
{
if(!ourUnit->alive())
continue;
for(auto enemyUnit : enemyUnits)
{
if(enemyUnit->alive())
{
cacheDamage(ourUnit, enemyUnit, hb);
cacheDamage(enemyUnit, ourUnit, hb);
}
}
}
}
int64_t DamageCache::getDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb)
{
auto damage = damageCache[attacker->unitId()][defender->unitId()] * attacker->getCount();
if(damage == 0)
{
cacheDamage(attacker, defender, hb);
damage = damageCache[attacker->unitId()][defender->unitId()] * attacker->getCount();
}
return static_cast<int64_t>(damage);
}
int64_t DamageCache::getOriginalDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb)
{
if(parent)
{
auto attackerDamageMap = parent->damageCache.find(attacker->unitId());
if(attackerDamageMap != parent->damageCache.end())
{
auto targetDamage = attackerDamageMap->second.find(defender->unitId());
if(targetDamage != attackerDamageMap->second.end())
{
return static_cast<int64_t>(targetDamage->second * attacker->getCount());
}
}
}
return getDamage(attacker, defender, hb);
}
AttackPossibility::AttackPossibility(BattleHex from, BattleHex dest, const BattleAttackInfo & attack)
: from(from), dest(dest), attack(attack)
{
@ -42,7 +117,8 @@ int64_t AttackPossibility::calculateDamageReduce(
const battle::Unit * attacker,
const battle::Unit * defender,
uint64_t damageDealt,
const CBattleInfoCallback & cb)
DamageCache & damageCache,
std::shared_ptr<CBattleInfoCallback> state)
{
const float HEALTH_BOUNTY = 0.5;
const float KILL_BOUNTY = 1.0 - HEALTH_BOUNTY;
@ -52,11 +128,11 @@ int64_t AttackPossibility::calculateDamageReduce(
// FIXME: provide distance info for Jousting bonus
auto attackerUnitForMeasurement = attacker;
if(attackerUnitForMeasurement->isTurret())
if(!attackerUnitForMeasurement || attackerUnitForMeasurement->isTurret())
{
auto ourUnits = cb.battleGetUnitsIf([&](const battle::Unit * u) -> bool
auto ourUnits = state->battleGetUnitsIf([&](const battle::Unit * u) -> bool
{
return u->unitSide() == attacker->unitSide() && !u->isTurret();
return u->unitSide() != defender->unitSide() && !u->isTurret();
});
if(ourUnits.empty())
@ -65,15 +141,18 @@ int64_t AttackPossibility::calculateDamageReduce(
attackerUnitForMeasurement = ourUnits.front();
}
auto enemyDamageBeforeAttack = cb.battleEstimateDamage(defender, attackerUnitForMeasurement, 0);
auto enemyDamageBeforeAttack = damageCache.getOriginalDamage(defender, attackerUnitForMeasurement, state);
auto enemiesKilled = damageDealt / defender->getMaxHealth() + (damageDealt % defender->getMaxHealth() >= defender->getFirstHPleft() ? 1 : 0);
auto enemyDamage = averageDmg(enemyDamageBeforeAttack.damage);
auto damagePerEnemy = enemyDamage / (double)defender->getCount();
auto damagePerEnemy = enemyDamageBeforeAttack / (double)defender->getCount();
return (int64_t)(damagePerEnemy * (enemiesKilled * KILL_BOUNTY + damageDealt * HEALTH_BOUNTY / (double)defender->getMaxHealth()));
}
int64_t AttackPossibility::evaluateBlockedShootersDmg(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle & state)
int64_t AttackPossibility::evaluateBlockedShootersDmg(
const BattleAttackInfo & attackInfo,
BattleHex hex,
DamageCache & damageCache,
std::shared_ptr<CBattleInfoCallback> state)
{
int64_t res = 0;
@ -84,10 +163,10 @@ int64_t AttackPossibility::evaluateBlockedShootersDmg(const BattleAttackInfo & a
auto hexes = attacker->getSurroundingHexes(hex);
for(BattleHex tile : hexes)
{
auto st = state.battleGetUnitByPos(tile, true);
if(!st || !state.battleMatchOwner(st, attacker))
auto st = state->battleGetUnitByPos(tile, true);
if(!st || !state->battleMatchOwner(st, attacker))
continue;
if(!state.battleCanShoot(st))
if(!state->battleCanShoot(st))
continue;
// FIXME: provide distance info for Jousting bonus
@ -97,8 +176,8 @@ int64_t AttackPossibility::evaluateBlockedShootersDmg(const BattleAttackInfo & a
BattleAttackInfo meleeAttackInfo(st, attacker, 0, false);
meleeAttackInfo.defenderPos = hex;
auto rangeDmg = state.battleEstimateDamage(rangeAttackInfo);
auto meleeDmg = state.battleEstimateDamage(meleeAttackInfo);
auto rangeDmg = state->battleEstimateDamage(rangeAttackInfo);
auto meleeDmg = state->battleEstimateDamage(meleeAttackInfo);
int64_t gain = averageDmg(rangeDmg.damage) - averageDmg(meleeDmg.damage) + 1;
res += gain;
@ -107,13 +186,17 @@ int64_t AttackPossibility::evaluateBlockedShootersDmg(const BattleAttackInfo & a
return res;
}
AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle & state)
AttackPossibility AttackPossibility::evaluate(
const BattleAttackInfo & attackInfo,
BattleHex hex,
DamageCache & damageCache,
std::shared_ptr<CBattleInfoCallback> state)
{
auto attacker = attackInfo.attacker;
auto defender = attackInfo.defender;
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
static const auto selectorBlocksRetaliation = Selector::type()(BonusType::BLOCKS_RETALIATION);
const auto attackerSide = state.playerToSide(state.battleGetOwner(attacker));
const auto attackerSide = state->playerToSide(state->battleGetOwner(attacker));
const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
AttackPossibility bestAp(hex, BattleHex::INVALID, attackInfo);
@ -141,9 +224,9 @@ AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInf
std::vector<const battle::Unit*> units;
if (attackInfo.shooting)
units = state.getAttackedBattleUnits(attacker, defHex, true, BattleHex::INVALID);
units = state->getAttackedBattleUnits(attacker, defHex, true, BattleHex::INVALID);
else
units = state.getAttackedBattleUnits(attacker, defHex, false, hex);
units = state->getAttackedBattleUnits(attacker, defHex, false, hex);
// ensure the defender is also affected
bool addDefender = true;
@ -172,7 +255,7 @@ AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInf
int64_t damageDealt, damageReceived, defenderDamageReduce, attackerDamageReduce;
DamageEstimation retaliation;
auto attackDmg = state.battleEstimateDamage(ap.attack, &retaliation);
auto attackDmg = state->battleEstimateDamage(ap.attack, &retaliation);
vstd::amin(attackDmg.damage.min, defenderState->getAvailableHealth());
vstd::amin(attackDmg.damage.max, defenderState->getAvailableHealth());
@ -181,7 +264,7 @@ AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInf
vstd::amin(retaliation.damage.max, ap.attackerState->getAvailableHealth());
damageDealt = averageDmg(attackDmg.damage);
defenderDamageReduce = calculateDamageReduce(attacker, defender, damageDealt, state);
defenderDamageReduce = calculateDamageReduce(attacker, defender, damageDealt, damageCache, state);
ap.attackerState->afterAttack(attackInfo.shooting, false);
//FIXME: use ranged retaliation
@ -191,11 +274,11 @@ AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInf
if (!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
{
damageReceived = averageDmg(retaliation.damage);
attackerDamageReduce = calculateDamageReduce(defender, attacker, damageReceived, state);
attackerDamageReduce = calculateDamageReduce(defender, attacker, damageReceived, damageCache, state);
defenderState->afterAttack(attackInfo.shooting, true);
}
bool isEnemy = state.battleMatchOwner(attacker, u);
bool isEnemy = state->battleMatchOwner(attacker, u);
// this includes enemy units as well as attacker units under enemy's mind control
if(isEnemy)
@ -225,7 +308,7 @@ AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInf
}
// check how much damage we gain from blocking enemy shooters on this hex
bestAp.shootersBlockedDmg = evaluateBlockedShootersDmg(attackInfo, hex, state);
bestAp.shootersBlockedDmg = evaluateBlockedShootersDmg(attackInfo, hex, damageCache, state);
#if BATTLE_TRACE_LEVEL>=1
logAi->trace("BattleAI best AP: %s -> %s at %d from %d, affects %d units: d:%lld a:%lld c:%lld s:%lld",

View File

@ -15,6 +15,22 @@
#define BATTLE_TRACE_LEVEL 0
class DamageCache
{
private:
std::unordered_map<uint32_t, std::unordered_map<uint32_t, float>> damageCache;
DamageCache * parent;
public:
DamageCache() : parent(nullptr) {}
DamageCache(DamageCache * parent) : parent(parent) {}
void cacheDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb);
int64_t getDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb);
int64_t getOriginalDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb);
void buildDamageCache(std::shared_ptr<HypotheticBattle> hb, int side);
};
/// <summary>
/// Evaluate attack value of one particular attack taking into account various effects like
/// retaliation, 2-hex breath, collateral damage, shooters blocked damage
@ -40,14 +56,23 @@ public:
int64_t damageDiff() const;
int64_t attackValue() const;
static AttackPossibility evaluate(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle & state);
static AttackPossibility evaluate(
const BattleAttackInfo & attackInfo,
BattleHex hex,
DamageCache & damageCache,
std::shared_ptr<CBattleInfoCallback> state);
static int64_t calculateDamageReduce(
const battle::Unit * attacker,
const battle::Unit * defender,
uint64_t damageDealt,
const CBattleInfoCallback & cb);
DamageCache & damageCache,
std::shared_ptr<CBattleInfoCallback> cb);
private:
static int64_t evaluateBlockedShootersDmg(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle & state);
static int64_t evaluateBlockedShootersDmg(
const BattleAttackInfo & attackInfo,
BattleHex hex,
DamageCache & damageCache,
std::shared_ptr<CBattleInfoCallback> state);
};

View File

@ -44,7 +44,55 @@ SpellTypes spellType(const CSpell * spell)
return SpellTypes::OTHER;
}
std::vector<BattleHex> CBattleAI::getBrokenWallMoatHexes() const
class BattleEvaluator
{
std::unique_ptr<PotentialTargets> targets;
std::shared_ptr<HypotheticBattle> hb;
BattleExchangeEvaluator scoreEvaluator;
std::shared_ptr<CBattleCallback> cb;
std::shared_ptr<Environment> env;
bool activeActionMade = false;
std::optional<AttackPossibility> cachedAttack;
PlayerColor playerID;
int side;
int64_t cachedScore;
DamageCache damageCache;
public:
BattleAction selectStackAction(const CStack * stack);
void attemptCastingSpell(const CStack * stack);
std::optional<PossibleSpellcast> findBestCreatureSpell(const CStack * stack);
BattleAction goTowardsNearest(const CStack * stack, std::vector<BattleHex> hexes);
std::vector<BattleHex> getBrokenWallMoatHexes() const;
void evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps); //for offensive damaging spells only
void print(const std::string & text) const;
BattleEvaluator(std::shared_ptr<Environment> env, std::shared_ptr<CBattleCallback> cb, const battle::Unit * activeStack, PlayerColor playerID, int side)
:scoreEvaluator(cb, env), cachedAttack(), playerID(playerID), side(side), env(env), cb(cb)
{
hb = std::make_shared<HypotheticBattle>(env.get(), cb);
damageCache.buildDamageCache(hb, side);
targets = std::make_unique<PotentialTargets>(activeStack, damageCache, hb);
cachedScore = EvaluationResult::INEFFECTIVE_SCORE;
}
BattleEvaluator(
std::shared_ptr<Environment> env,
std::shared_ptr<CBattleCallback> cb,
std::shared_ptr<HypotheticBattle> hb,
DamageCache & damageCache,
const battle::Unit * activeStack,
PlayerColor playerID,
int side)
:scoreEvaluator(cb, env), cachedAttack(), playerID(playerID), side(side), env(env), cb(cb), hb(hb), damageCache(damageCache)
{
targets = std::make_unique<PotentialTargets>(activeStack, damageCache, hb);
cachedScore = EvaluationResult::INEFFECTIVE_SCORE;
}
};
std::vector<BattleHex> BattleEvaluator::getBrokenWallMoatHexes() const
{
std::vector<BattleHex> result;
@ -110,7 +158,7 @@ BattleAction CBattleAI::useHealingTent(const CStack *stack)
return BattleAction::makeHeal(stack, woundHpToStack.rbegin()->second); //last element of the woundHpToStack is the most wounded stack
}
std::optional<PossibleSpellcast> CBattleAI::findBestCreatureSpell(const CStack *stack)
std::optional<PossibleSpellcast> BattleEvaluator::findBestCreatureSpell(const CStack *stack)
{
//TODO: faerie dragon type spell should be selected by server
SpellID creatureSpellToCast = cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), stack, CBattleInfoCallback::RANDOM_AIMED);
@ -141,40 +189,37 @@ std::optional<PossibleSpellcast> CBattleAI::findBestCreatureSpell(const CStack *
return std::nullopt;
}
BattleAction CBattleAI::selectStackAction(const CStack * stack)
BattleAction BattleEvaluator::selectStackAction(const CStack * stack)
{
//evaluate casting spell for spellcasting stack
std::optional<PossibleSpellcast> bestSpellcast = findBestCreatureSpell(stack);
HypotheticBattle hb(env.get(), cb);
auto moveTarget = scoreEvaluator.findMoveTowardsUnreachable(stack, *targets, damageCache, hb);
auto score = EvaluationResult::INEFFECTIVE_SCORE;
PotentialTargets targets(stack, hb);
BattleExchangeEvaluator scoreEvaluator(cb, env);
auto moveTarget = scoreEvaluator.findMoveTowardsUnreachable(stack, targets, hb);
int64_t score = EvaluationResult::INEFFECTIVE_SCORE;
if(targets.possibleAttacks.empty() && bestSpellcast.has_value())
if(targets->possibleAttacks.empty() && bestSpellcast.has_value())
{
movesSkippedByDefense = 0;
activeActionMade = true;
return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id);
}
if(!targets.possibleAttacks.empty())
if(!targets->possibleAttacks.empty())
{
#if BATTLE_TRACE_LEVEL>=1
logAi->trace("Evaluating attack for %s", stack->getDescription());
#endif
auto evaluationResult = scoreEvaluator.findBestTarget(stack, targets, hb);
auto evaluationResult = scoreEvaluator.findBestTarget(stack, *targets, damageCache, hb);
auto & bestAttack = evaluationResult.bestAttack;
cachedAttack = bestAttack;
cachedScore = evaluationResult.score;
//TODO: consider more complex spellcast evaluation, f.e. because "re-retaliation" during enemy move in same turn for melee attack etc.
if(bestSpellcast.has_value() && bestSpellcast->value > bestAttack.damageDiff())
{
// return because spellcast value is damage dealt and score is dps reduce
movesSkippedByDefense = 0;
activeActionMade = true;
return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id);
}
@ -194,7 +239,7 @@ BattleAction CBattleAI::selectStackAction(const CStack * stack)
bestAttack.attackerDamageReduce, bestAttack.attackValue()
);
if (moveTarget.score <= score)
if (moveTarget.scorePerTurn <= score)
{
if(evaluationResult.wait)
{
@ -202,12 +247,12 @@ BattleAction CBattleAI::selectStackAction(const CStack * stack)
}
else if(bestAttack.attack.shooting)
{
movesSkippedByDefense = 0;
activeActionMade = true;
return BattleAction::makeShotAttack(stack, bestAttack.attack.defender);
}
else
{
movesSkippedByDefense = 0;
activeActionMade = true;
return BattleAction::makeMeleeAttack(stack, bestAttack.attack.defender->getPosition(), bestAttack.from);
}
}
@ -215,9 +260,11 @@ BattleAction CBattleAI::selectStackAction(const CStack * stack)
}
//ThreatMap threatsToUs(stack); // These lines may be usefull but they are't used in the code.
if(moveTarget.score > score)
if(moveTarget.scorePerTurn > score)
{
score = moveTarget.score;
cachedAttack = moveTarget.cachedAttack;
cachedScore = score;
if(stack->waited())
{
@ -238,7 +285,7 @@ BattleAction CBattleAI::selectStackAction(const CStack * stack)
if(brokenWallMoat.size())
{
movesSkippedByDefense = 0;
activeActionMade = true;
if(stack->doubleWide() && vstd::contains(brokenWallMoat, stack->getPosition()))
return BattleAction::makeMove(stack, stack->getPosition().cloneInDirection(BattleHex::RIGHT));
@ -284,7 +331,11 @@ void CBattleAI::activeStack( const CStack * stack )
return;
}
if (attemptCastingSpell())
BattleEvaluator evaluator(env, cb, stack, playerID, side);
result = evaluator.selectStackAction(stack);
if(evaluator.attemptCastingSpell(stack))
return;
logAi->trace("Spellcast attempt completed in %lld", timeElapsed(start));
@ -294,8 +345,6 @@ void CBattleAI::activeStack( const CStack * stack )
cb->battleMakeUnitAction(*action);
return;
}
result = selectStackAction(stack);
}
catch(boost::thread_interrupted &)
{
@ -320,7 +369,7 @@ void CBattleAI::activeStack( const CStack * stack )
cb->battleMakeUnitAction(result);
}
BattleAction CBattleAI::goTowardsNearest(const CStack * stack, std::vector<BattleHex> hexes) const
BattleAction BattleEvaluator::goTowardsNearest(const CStack * stack, std::vector<BattleHex> hexes)
{
auto reachability = cb->getReachability(stack);
auto avHexes = cb->battleGetAvailableHexes(reachability, stack, false);
@ -357,9 +406,6 @@ BattleAction CBattleAI::goTowardsNearest(const CStack * stack, std::vector<Battl
return BattleAction::makeDefend(stack);
}
BattleExchangeEvaluator scoreEvaluator(cb, env);
HypotheticBattle hb(env.get(), cb);
scoreEvaluator.updateReachabilityMap(hb);
if(stack->hasBonusOfType(BonusType::FLYING))
@ -371,7 +417,7 @@ BattleAction CBattleAI::goTowardsNearest(const CStack * stack, std::vector<Battl
obstacleHexes.insert(affectedHexes.cbegin(), affectedHexes.cend());
};
const auto & obstacles = hb.battleGetAllObstacles();
const auto & obstacles = hb->battleGetAllObstacles();
for (const auto & obst: obstacles) {
@ -396,7 +442,7 @@ BattleAction CBattleAI::goTowardsNearest(const CStack * stack, std::vector<Battl
if(vstd::contains(obstacleHexes, hex))
distance += NEGATIVE_OBSTACLE_PENALTY;
return scoreEvaluator.checkPositionBlocksOurStacks(hb, stack, hex) ? BLOCKED_STACK_PENALTY + distance : distance;
return scoreEvaluator.checkPositionBlocksOurStacks(*hb, stack, hex) ? BLOCKED_STACK_PENALTY + distance : distance;
});
return BattleAction::makeMove(stack, *nearestAvailableHex);
@ -412,7 +458,7 @@ BattleAction CBattleAI::goTowardsNearest(const CStack * stack, std::vector<Battl
}
if(vstd::contains(avHexes, currentDest)
&& !scoreEvaluator.checkPositionBlocksOurStacks(hb, stack, currentDest))
&& !scoreEvaluator.checkPositionBlocksOurStacks(*hb, stack, currentDest))
return BattleAction::makeMove(stack, currentDest);
currentDest = reachability.predecessors[currentDest];
@ -468,7 +514,11 @@ BattleAction CBattleAI::useCatapult(const CStack * stack)
return attack;
}
<<<<<<< HEAD
bool CBattleAI::attemptCastingSpell()
=======
void BattleEvaluator::attemptCastingSpell(const CStack * activeStack)
>>>>>>> ea22737e9 (BattleAI: damage cache and switch to different model of spells evaluation)
{
auto hero = cb->battleGetMyHero();
if(!hero)
@ -488,7 +538,7 @@ bool CBattleAI::attemptCastingSpell()
vstd::erase_if(possibleSpells, [](const CSpell *s)
{
return spellType(s) != SpellTypes::BATTLE;
return spellType(s) != SpellTypes::BATTLE || s->getTargetType() == spells::AimType::LOCATION;
});
LOGFL("I know how %d of them works.", possibleSpells.size());
@ -499,7 +549,7 @@ bool CBattleAI::attemptCastingSpell()
{
spells::BattleCast temp(cb.get(), hero, spells::Mode::HERO, spell);
if(!spell->isDamage() && spell->getTargetType() == spells::AimType::LOCATION)
if(spell->getTargetType() == spells::AimType::LOCATION)
continue;
const bool FAST = true;
@ -518,7 +568,7 @@ bool CBattleAI::attemptCastingSpell()
using ValueMap = PossibleSpellcast::ValueMap;
auto evaluateQueue = [&](ValueMap & values, const std::vector<battle::Units> & queue, HypotheticBattle & state, size_t minTurnSpan, bool * enemyHadTurnOut) -> bool
auto evaluateQueue = [&](ValueMap & values, const std::vector<battle::Units> & queue, std::shared_ptr<HypotheticBattle> state, size_t minTurnSpan, bool * enemyHadTurnOut) -> bool
{
bool firstRound = true;
bool enemyHadTurn = false;
@ -529,7 +579,7 @@ bool CBattleAI::attemptCastingSpell()
for(auto & round : queue)
{
if(!firstRound)
state.nextRound(0);//todo: set actual value?
state->nextRound(0);//todo: set actual value?
for(auto unit : round)
{
if(!vstd::contains(values, unit->unitId()))
@ -538,11 +588,11 @@ bool CBattleAI::attemptCastingSpell()
if(!unit->alive())
continue;
if(state.battleGetOwner(unit) != playerID)
if(state->battleGetOwner(unit) != playerID)
{
enemyHadTurn = true;
if(!firstRound || state.battleCastSpells(unit->unitSide()) == 0)
if(!firstRound || state->battleCastSpells(unit->unitSide()) == 0)
{
//enemy could counter our spell at this point
//anyway, we do not know what enemy will do
@ -556,15 +606,15 @@ bool CBattleAI::attemptCastingSpell()
ourTurnSpan++;
}
state.nextTurn(unit->unitId());
state->nextTurn(unit->unitId());
PotentialTargets pt(unit, state);
PotentialTargets pt(unit, damageCache, state);
if(!pt.possibleAttacks.empty())
{
AttackPossibility ap = pt.bestAction();
auto swb = state.getForUpdate(unit->unitId());
auto swb = state->getForUpdate(unit->unitId());
*swb = *ap.attackerState;
if(ap.defenderDamageReduce > 0)
@ -574,7 +624,7 @@ bool CBattleAI::attemptCastingSpell()
for(auto affected : ap.affectedUnits)
{
swb = state.getForUpdate(affected->unitId());
swb = state->getForUpdate(affected->unitId());
*swb = *affected;
if(ap.defenderDamageReduce > 0)
@ -587,7 +637,7 @@ bool CBattleAI::attemptCastingSpell()
auto bav = pt.bestActionValue();
//best action is from effective owner`s point if view, we need to convert to our point if view
if(state.battleGetOwner(unit) != playerID)
if(state->battleGetOwner(unit) != playerID)
bav = -bav;
values[unit->unitId()] += bav;
}
@ -638,13 +688,13 @@ bool CBattleAI::attemptCastingSpell()
{
bool enemyHadTurn = false;
HypotheticBattle state(env.get(), cb);
auto state = std::make_shared<HypotheticBattle>(env.get(), cb);
evaluateQueue(valueOfStack, turnOrder, state, 0, &enemyHadTurn);
if(!enemyHadTurn)
{
auto battleIsFinishedOpt = state.battleIsFinished();
auto battleIsFinishedOpt = state->battleIsFinished();
if(battleIsFinishedOpt)
{
@ -661,74 +711,60 @@ bool CBattleAI::attemptCastingSpell()
auto evaluateSpellcast = [&] (PossibleSpellcast * ps, std::shared_ptr<ScriptsCache>)
{
HypotheticBattle state(env.get(), cb);
auto state = std::make_shared<HypotheticBattle>(env.get(), cb);
spells::BattleCast cast(&state, hero, spells::Mode::HERO, ps->spell);
cast.castEval(state.getServerCallback(), ps->dest);
ValueMap newHealthOfStack;
ValueMap newValueOfStack;
spells::BattleCast cast(state.get(), hero, spells::Mode::HERO, ps->spell);
cast.castEval(state->getServerCallback(), ps->dest);
size_t ourUnits = 0;
auto allUnits = state->battleGetUnitsIf([](const battle::Unit * u) -> bool{ return true; });
std::set<uint32_t> unitIds;
state.battleGetUnitsIf([&](const battle::Unit * u)->bool
{
if(!u->isGhost() && !u->isTurret())
unitIds.insert(u->unitId());
return false;
});
for(auto unitId : unitIds)
{
auto localUnit = state.battleGetUnitByID(unitId);
newHealthOfStack[unitId] = localUnit->getAvailableHealth();
newValueOfStack[unitId] = 0;
if(state.battleGetOwner(localUnit) == playerID && localUnit->alive() && localUnit->willMove())
ourUnits++;
}
size_t minTurnSpan = ourUnits/3; //todo: tweak this
std::vector<battle::Units> newTurnOrder;
state.battleGetTurnOrder(newTurnOrder, amount, 2);
const bool turnSpanOK = evaluateQueue(newValueOfStack, newTurnOrder, state, minTurnSpan, nullptr);
if(turnSpanOK || castNow)
{
int64_t totalGain = 0;
for(auto unitId : unitIds)
auto needFullEval = vstd::contains_if(allUnits, [&](const battle::Unit * u) -> bool
{
auto localUnit = state.battleGetUnitByID(unitId);
auto original = cb->battleGetUnitByID(u->unitId());
return !original || u->speed() != original->speed();
});
auto newValue = getValOr(newValueOfStack, unitId, 0);
auto oldValue = getValOr(valueOfStack, unitId, 0);
DamageCache innerCache(&damageCache);
innerCache.buildDamageCache(state, side);
auto healthDiff = newHealthOfStack[unitId] - healthOfStack[unitId];
if(needFullEval || !cachedAttack)
{
PotentialTargets innerTargets(activeStack, damageCache, state);
BattleExchangeEvaluator innerEvaluator(state, env);
if(localUnit->unitOwner() != playerID)
healthDiff = -healthDiff;
if(!innerTargets.possibleAttacks.empty())
{
innerEvaluator.updateReachabilityMap(state);
if(healthDiff < 0)
{
ps->value = -1;
return; //do not damage own units at all
}
auto newStackAction = innerEvaluator.findBestTarget(activeStack, innerTargets, innerCache, state);
totalGain += (newValue - oldValue + healthDiff);
ps->value = newStackAction.score;
}
else
{
ps->value = 0;
}
ps->value = totalGain;
}
else
{
ps->value = -1;
ps->value = scoreEvaluator.calculateExchange(*cachedAttack, *targets, innerCache, state);
}
for(auto unit : allUnits)
{
auto newHealth = unit->getAvailableHealth();
auto oldHealth = healthOfStack[unit->unitId()];
if(oldHealth != newHealth)
{
auto damage = std::abs(oldHealth - newHealth);
auto originalDefender = cb->battleGetUnitByID(unit->unitId());
auto dpsReduce = AttackPossibility::calculateDamageReduce(nullptr, originalDefender ? originalDefender : unit, damage, innerCache, state);
auto ourUnit = unit->unitSide() == side ? 1 : -1;
auto goodEffect = newHealth > oldHealth ? 1 : -1;
ps->value += ourUnit * goodEffect * dpsReduce;
}
}
};
@ -767,7 +803,7 @@ bool CBattleAI::attemptCastingSpell()
};
auto castToPerform = *vstd::maxElementByFun(possibleCasts, pscValue);
if(castToPerform.value > 0)
if(castToPerform.value > cachedScore)
{
LOGFL("Best spell is %s (value %d). Will cast.", castToPerform.spell->getNameTranslated() % castToPerform.value);
BattleAction spellcast;
@ -777,8 +813,12 @@ bool CBattleAI::attemptCastingSpell()
spellcast.side = side;
spellcast.stackNumber = (!side) ? -1 : -2;
cb->battleMakeSpellAction(spellcast);
<<<<<<< HEAD
movesSkippedByDefense = 0;
return true;
=======
activeActionMade = true;
>>>>>>> ea22737e9 (BattleAI: damage cache and switch to different model of spells evaluation)
}
else
{
@ -788,7 +828,7 @@ bool CBattleAI::attemptCastingSpell()
}
//Below method works only for offensive spells
void CBattleAI::evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps)
void BattleEvaluator::evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps)
{
using ValueMap = PossibleSpellcast::ValueMap;
@ -849,6 +889,11 @@ void CBattleAI::print(const std::string &text) const
logAi->trace("%s Battle AI[%p]: %s", playerID.getStr(), this, text);
}
void BattleEvaluator::print(const std::string & text) const
{
logAi->trace("%s Battle AI[%p]: %s", playerID.getStr(), this, text);
}
std::optional<BattleAction> CBattleAI::considerFleeingOrSurrendering()
{
BattleStateInfoForRetreat bs;

View File

@ -68,9 +68,6 @@ public:
~CBattleAI();
void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB) override;
bool attemptCastingSpell();
void evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps); //for offensive damaging spells only
void activeStack(const CStack * stack) override; //called when it's turn of that stack
void yourTacticPhase(int distance) override;
@ -80,8 +77,6 @@ public:
void print(const std::string &text) const;
BattleAction useCatapult(const CStack *stack);
BattleAction useHealingTent(const CStack *stack);
BattleAction selectStackAction(const CStack * stack);
std::optional<PossibleSpellcast> findBestCreatureSpell(const CStack *stack);
void battleStart(const CCreatureSet * army1, const CCreatureSet * army2, int3 tile, const CGHeroInstance * hero1, const CGHeroInstance * hero2, bool Side, bool replayAllowed) override;
//void actionFinished(const BattleAction &action) override;//occurs AFTER every action taken by any stack or by the hero
@ -98,8 +93,4 @@ public:
//void battleTriggerEffect(const BattleTriggerEffect & bte) override;
//void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side) override; //called by engine when battle starts; side=0 - left, side=1 - right
//void battleCatapultAttacked(const CatapultAttack & ca) override; //called when catapult makes an attack
private:
BattleAction goTowardsNearest(const CStack * stack, std::vector<BattleHex> hexes) const;
std::vector<BattleHex> getBrokenWallMoatHexes() const;
};

View File

@ -18,9 +18,11 @@ AttackerValue::AttackerValue()
}
MoveTarget::MoveTarget()
: positions()
: positions(), cachedAttack()
{
score = EvaluationResult::INEFFECTIVE_SCORE;
scorePerTurn = EvaluationResult::INEFFECTIVE_SCORE;
turnsToRich = 1;
}
int64_t BattleExchangeVariant::trackAttack(const AttackPossibility & ap, HypotheticBattle & state)
@ -61,14 +63,14 @@ int64_t BattleExchangeVariant::trackAttack(
std::shared_ptr<StackWithBonuses> defender,
bool shooting,
bool isOurAttack,
const CBattleInfoCallback & cb,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb,
bool evaluateOnly)
{
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
static const auto selectorBlocksRetaliation = Selector::type()(BonusType::BLOCKS_RETALIATION);
const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
DamageEstimation retaliation;
// FIXME: provide distance info for Jousting bonus
BattleAttackInfo bai(attacker.get(), defender.get(), 0, shooting);
@ -77,9 +79,8 @@ int64_t BattleExchangeVariant::trackAttack(
bai.attackerPos.setXY(8, 5);
}
auto attack = cb.battleEstimateDamage(bai, &retaliation);
int64_t attackDamage = (attack.damage.min + attack.damage.max) / 2;
int64_t defenderDamageReduce = AttackPossibility::calculateDamageReduce(attacker.get(), defender.get(), attackDamage, cb);
int64_t attackDamage = damageCache.getDamage(attacker.get(), defender.get(), hb);
int64_t defenderDamageReduce = AttackPossibility::calculateDamageReduce(attacker.get(), defender.get(), attackDamage, damageCache, hb);
int64_t attackerDamageReduce = 0;
if(!evaluateOnly)
@ -108,36 +109,33 @@ int64_t BattleExchangeVariant::trackAttack(
if(defender->alive() && defender->ableToRetaliate() && !counterAttacksBlocked && !shooting)
{
if(retaliation.damage.max != 0)
{
auto retaliationDamage = (retaliation.damage.min + retaliation.damage.max) / 2;
attackerDamageReduce = AttackPossibility::calculateDamageReduce(defender.get(), attacker.get(), retaliationDamage, cb);
auto retaliationDamage = damageCache.getDamage(defender.get(), attacker.get(), hb);
attackerDamageReduce = AttackPossibility::calculateDamageReduce(defender.get(), attacker.get(), retaliationDamage, damageCache, hb);
if(!evaluateOnly)
{
if(!evaluateOnly)
{
#if BATTLE_TRACE_LEVEL>=1
logAi->trace(
"%s -> %s, retaliation, dps: %lld, %lld",
defender->getDescription(),
attacker->getDescription(),
retaliationDamage,
attackerDamageReduce);
logAi->trace(
"%s -> %s, retaliation, dps: %lld, %lld",
defender->getDescription(),
attacker->getDescription(),
retaliationDamage,
attackerDamageReduce);
#endif
if(isOurAttack)
{
dpsScore -= attackerDamageReduce;
attackerValue[attacker->unitId()].isRetalitated = true;
}
else
{
dpsScore += attackerDamageReduce;
attackerValue[defender->unitId()].value += attackerDamageReduce;
}
attacker->damage(retaliationDamage);
defender->afterAttack(false, true);
if(isOurAttack)
{
dpsScore -= attackerDamageReduce;
attackerValue[attacker->unitId()].isRetalitated = true;
}
else
{
dpsScore += attackerDamageReduce;
attackerValue[defender->unitId()].value += attackerDamageReduce;
}
attacker->damage(retaliationDamage);
defender->afterAttack(false, true);
}
}
@ -153,7 +151,11 @@ int64_t BattleExchangeVariant::trackAttack(
return score;
}
EvaluationResult BattleExchangeEvaluator::findBestTarget(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb)
EvaluationResult BattleExchangeEvaluator::findBestTarget(
const battle::Unit * activeStack,
PotentialTargets & targets,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb)
{
EvaluationResult result(targets.bestAction());
@ -161,7 +163,7 @@ EvaluationResult BattleExchangeEvaluator::findBestTarget(const battle::Unit * ac
for(auto & ap : targets.possibleAttacks)
{
int64_t score = calculateExchange(ap, targets, hb);
int64_t score = calculateExchange(ap, targets, damageCache, hb);
if(score > result.score)
{
@ -176,14 +178,14 @@ EvaluationResult BattleExchangeEvaluator::findBestTarget(const battle::Unit * ac
logAi->trace("Evaluating waited attack for %s", activeStack->getDescription());
#endif
hb.getForUpdate(activeStack->unitId())->waiting = true;
hb.getForUpdate(activeStack->unitId())->waitedThisTurn = true;
hb->getForUpdate(activeStack->unitId())->waiting = true;
hb->getForUpdate(activeStack->unitId())->waitedThisTurn = true;
updateReachabilityMap(hb);
for(auto & ap : targets.possibleAttacks)
{
int64_t score = calculateExchange(ap, targets, hb);
int64_t score = calculateExchange(ap, targets, damageCache, hb);
if(score > result.score)
{
@ -197,7 +199,11 @@ EvaluationResult BattleExchangeEvaluator::findBestTarget(const battle::Unit * ac
return result;
}
MoveTarget BattleExchangeEvaluator::findMoveTowardsUnreachable(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb)
MoveTarget BattleExchangeEvaluator::findMoveTowardsUnreachable(
const battle::Unit * activeStack,
PotentialTargets & targets,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb)
{
MoveTarget result;
BattleExchangeVariant ev;
@ -237,16 +243,20 @@ MoveTarget BattleExchangeEvaluator::findMoveTowardsUnreachable(const battle::Uni
{
// FIXME: provide distance info for Jousting bonus
auto bai = BattleAttackInfo(activeStack, closestStack, 0, cb->battleCanShoot(activeStack));
auto attack = AttackPossibility::evaluate(bai, hex, hb);
auto attack = AttackPossibility::evaluate(bai, hex, damageCache, hb);
attack.shootersBlockedDmg = 0; // we do not want to count on it, it is not for sure
auto score = calculateExchange(attack, targets, hb) / turnsToRich;
auto score = calculateExchange(attack, targets, damageCache, hb);
auto scorePerTurn = score / turnsToRich;
if(result.score < score)
if(result.scorePerTurn < scorePerTurn)
{
result.scorePerTurn = scorePerTurn;
result.score = score;
result.positions = closestStack->getAttackableHexes(activeStack);
result.cachedAttack = attack;
result.turnsToRich = turnsToRich;
}
}
}
@ -287,7 +297,7 @@ std::vector<const battle::Unit *> BattleExchangeEvaluator::getAdjacentUnits(cons
std::vector<const battle::Unit *> BattleExchangeEvaluator::getExchangeUnits(
const AttackPossibility & ap,
PotentialTargets & targets,
HypotheticBattle & hb)
std::shared_ptr<HypotheticBattle> hb)
{
auto hexes = ap.attack.defender->getHexes();
@ -308,7 +318,7 @@ std::vector<const battle::Unit *> BattleExchangeEvaluator::getExchangeUnits(
{
for(auto adjacentUnit : getAdjacentUnits(unit))
{
auto unitWithBonuses = hb.battleGetUnitByID(adjacentUnit->unitId());
auto unitWithBonuses = hb->battleGetUnitByID(adjacentUnit->unitId());
if(vstd::contains(targets.unreachableEnemies, adjacentUnit)
&& !vstd::contains(allReachableUnits, unitWithBonuses))
@ -349,7 +359,8 @@ std::vector<const battle::Unit *> BattleExchangeEvaluator::getExchangeUnits(
int64_t BattleExchangeEvaluator::calculateExchange(
const AttackPossibility & ap,
PotentialTargets & targets,
HypotheticBattle & hb)
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb)
{
#if BATTLE_TRACE_LEVEL>=1
logAi->trace("Battle exchange at %lld", ap.attack.shooting ? ap.dest : ap.from);
@ -374,7 +385,7 @@ int64_t BattleExchangeEvaluator::calculateExchange(
return 0;
}
HypotheticBattle exchangeBattle(env.get(), cb);
auto exchangeBattle = std::make_shared<HypotheticBattle>(env.get(), hb);
BattleExchangeVariant v;
auto melleeAttackers = ourStacks;
@ -389,10 +400,9 @@ int64_t BattleExchangeEvaluator::calculateExchange(
if(unit->isTurret())
continue;
bool isOur = cb->battleMatchOwner(ap.attack.attacker, unit, true);
bool isOur = exchangeBattle->battleMatchOwner(ap.attack.attacker, unit, true);
auto & attackerQueue = isOur ? ourStacks : enemyStacks;
if(!vstd::contains(attackerQueue, unit))
{
attackerQueue.push_back(unit);
@ -403,11 +413,11 @@ int64_t BattleExchangeEvaluator::calculateExchange(
for(auto activeUnit : exchangeUnits)
{
bool isOur = cb->battleMatchOwner(ap.attack.attacker, activeUnit, true);
bool isOur = exchangeBattle->battleMatchOwner(ap.attack.attacker, activeUnit, true);
battle::Units & attackerQueue = isOur ? ourStacks : enemyStacks;
battle::Units & oppositeQueue = isOur ? enemyStacks : ourStacks;
auto attacker = exchangeBattle.getForUpdate(activeUnit->unitId());
auto attacker = exchangeBattle->getForUpdate(activeUnit->unitId());
if(!attacker->alive())
{
@ -420,17 +430,18 @@ int64_t BattleExchangeEvaluator::calculateExchange(
auto targetUnit = ap.attack.defender;
if(!isOur || !exchangeBattle.getForUpdate(targetUnit->unitId())->alive())
if(!isOur || !exchangeBattle->getForUpdate(targetUnit->unitId())->alive())
{
auto estimateAttack = [&](const battle::Unit * u) -> int64_t
{
auto stackWithBonuses = exchangeBattle.getForUpdate(u->unitId());
auto stackWithBonuses = exchangeBattle->getForUpdate(u->unitId());
auto score = v.trackAttack(
attacker,
stackWithBonuses,
exchangeBattle.battleCanShoot(stackWithBonuses.get()),
exchangeBattle->battleCanShoot(stackWithBonuses.get()),
isOur,
*cb,
damageCache,
hb,
true);
#if BATTLE_TRACE_LEVEL>=1
@ -446,7 +457,7 @@ int64_t BattleExchangeEvaluator::calculateExchange(
}
else
{
auto reachable = exchangeBattle.battleGetUnitsIf([&](const battle::Unit * u) -> bool
auto reachable = exchangeBattle->battleGetUnitsIf([&](const battle::Unit * u) -> bool
{
if(!u->alive() || u->unitSide() == attacker->unitSide())
return false;
@ -472,19 +483,19 @@ int64_t BattleExchangeEvaluator::calculateExchange(
}
}
auto defender = exchangeBattle.getForUpdate(targetUnit->unitId());
auto shooting = cb->battleCanShoot(attacker.get());
auto defender = exchangeBattle->getForUpdate(targetUnit->unitId());
auto shooting = exchangeBattle->battleCanShoot(attacker.get());
const int totalAttacks = attacker->getTotalAttacks(shooting);
if(canUseAp && activeUnit == ap.attack.attacker && targetUnit == ap.attack.defender)
if(canUseAp && activeUnit->unitId() == ap.attack.attacker->unitId() && targetUnit->unitId() == ap.attack.defender->unitId())
{
v.trackAttack(ap, exchangeBattle);
v.trackAttack(ap, *exchangeBattle);
}
else
{
for(int i = 0; i < totalAttacks; i++)
{
v.trackAttack(attacker, defender, shooting, isOur, exchangeBattle);
v.trackAttack(attacker, defender, shooting, isOur, damageCache, exchangeBattle);
if(!attacker->alive() || !defender->alive())
break;
@ -495,12 +506,12 @@ int64_t BattleExchangeEvaluator::calculateExchange(
vstd::erase_if(attackerQueue, [&](const battle::Unit * u) -> bool
{
return !exchangeBattle.getForUpdate(u->unitId())->alive();
return !exchangeBattle->getForUpdate(u->unitId())->alive();
});
vstd::erase_if(oppositeQueue, [&](const battle::Unit * u) -> bool
{
return !exchangeBattle.getForUpdate(u->unitId())->alive();
return !exchangeBattle->getForUpdate(u->unitId())->alive();
});
}
@ -581,13 +592,13 @@ void BattleExchangeVariant::adjustPositions(
}
}
void BattleExchangeEvaluator::updateReachabilityMap(HypotheticBattle & hb)
void BattleExchangeEvaluator::updateReachabilityMap( std::shared_ptr<HypotheticBattle> hb)
{
const int TURN_DEPTH = 2;
turnOrder.clear();
hb.battleGetTurnOrder(turnOrder, std::numeric_limits<int>::max(), TURN_DEPTH);
hb->battleGetTurnOrder(turnOrder, std::numeric_limits<int>::max(), TURN_DEPTH);
reachabilityMap.clear();
for(int turn = 0; turn < turnOrder.size(); turn++)

View File

@ -26,7 +26,10 @@ struct AttackerValue
struct MoveTarget
{
int64_t score;
int64_t scorePerTurn;
std::vector<BattleHex> positions;
std::optional<AttackPossibility> cachedAttack;
uint8_t turnsToRich;
MoveTarget();
};
@ -65,7 +68,8 @@ public:
std::shared_ptr<StackWithBonuses> defender,
bool shooting,
bool isOurAttack,
const CBattleInfoCallback & cb,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb,
bool evaluateOnly = false);
int64_t getScore() const { return dpsScore; }
@ -91,11 +95,27 @@ private:
public:
BattleExchangeEvaluator(std::shared_ptr<CBattleInfoCallback> cb, std::shared_ptr<Environment> env): cb(cb), env(env) {}
EvaluationResult findBestTarget(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb);
int64_t calculateExchange(const AttackPossibility & ap, PotentialTargets & targets, HypotheticBattle & hb);
void updateReachabilityMap(HypotheticBattle & hb);
std::vector<const battle::Unit *> getExchangeUnits(const AttackPossibility & ap, PotentialTargets & targets, HypotheticBattle & hb);
EvaluationResult findBestTarget(
const battle::Unit * activeStack,
PotentialTargets & targets,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb);
int64_t calculateExchange(
const AttackPossibility & ap,
PotentialTargets & targets,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb);
void updateReachabilityMap(std::shared_ptr<HypotheticBattle> hb);
std::vector<const battle::Unit *> getExchangeUnits(const AttackPossibility & ap, PotentialTargets & targets, std::shared_ptr<HypotheticBattle> hb);
bool checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * unit, BattleHex position);
MoveTarget findMoveTowardsUnreachable(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb);
MoveTarget findMoveTowardsUnreachable(
const battle::Unit * activeStack,
PotentialTargets & targets,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb);
std::vector<const battle::Unit *> getAdjacentUnits(const battle::Unit * unit);
};

View File

@ -11,11 +11,14 @@
#include "PotentialTargets.h"
#include "../../lib/CStack.h"//todo: remove
PotentialTargets::PotentialTargets(const battle::Unit * attacker, const HypotheticBattle & state)
PotentialTargets::PotentialTargets(
const battle::Unit * attacker,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> state)
{
auto attackerInfo = state.battleGetUnitByID(attacker->unitId());
auto reachability = state.getReachability(attackerInfo);
auto avHexes = state.battleGetAvailableHexes(reachability, attackerInfo, false);
auto attackerInfo = state->battleGetUnitByID(attacker->unitId());
auto reachability = state->getReachability(attackerInfo);
auto avHexes = state->battleGetAvailableHexes(reachability, attackerInfo, false);
//FIXME: this should part of battleGetAvailableHexes
bool forceTarget = false;
@ -25,7 +28,7 @@ PotentialTargets::PotentialTargets(const battle::Unit * attacker, const Hypothet
if(attackerInfo->hasBonusOfType(BonusType::ATTACKS_NEAREST_CREATURE))
{
forceTarget = true;
auto nearest = state.getNearestStack(attackerInfo);
auto nearest = state->getNearestStack(attackerInfo);
if(nearest.first != nullptr)
{
@ -34,14 +37,14 @@ PotentialTargets::PotentialTargets(const battle::Unit * attacker, const Hypothet
}
}
auto aliveUnits = state.battleGetUnitsIf([=](const battle::Unit * unit)
auto aliveUnits = state->battleGetUnitsIf([=](const battle::Unit * unit)
{
return unit->isValidTarget() && unit->unitId() != attackerInfo->unitId();
});
for(auto defender : aliveUnits)
{
if(!forceTarget && !state.battleMatchOwner(attackerInfo, defender))
if(!forceTarget && !state->battleMatchOwner(attackerInfo, defender))
continue;
auto GenerateAttackInfo = [&](bool shooting, BattleHex hex) -> AttackPossibility
@ -49,7 +52,7 @@ PotentialTargets::PotentialTargets(const battle::Unit * attacker, const Hypothet
int distance = hex.isValid() ? reachability.distances[hex] : 0;
auto bai = BattleAttackInfo(attackerInfo, defender, distance, shooting);
return AttackPossibility::evaluate(bai, hex, state);
return AttackPossibility::evaluate(bai, hex, damageCache, state);
};
if(forceTarget)
@ -59,7 +62,7 @@ PotentialTargets::PotentialTargets(const battle::Unit * attacker, const Hypothet
else
unreachableEnemies.push_back(defender);
}
else if(state.battleCanShoot(attackerInfo, defender->getPosition()))
else if(state->battleCanShoot(attackerInfo, defender->getPosition()))
{
possibleAttacks.push_back(GenerateAttackInfo(true, BattleHex::INVALID));
}

View File

@ -17,7 +17,10 @@ public:
std::vector<const battle::Unit *> unreachableEnemies;
PotentialTargets(){};
PotentialTargets(const battle::Unit * attacker, const HypotheticBattle & state);
PotentialTargets(
const battle::Unit * attacker,
DamageCache & damageCache,
std::shared_ptr<HypotheticBattle> hb);
const AttackPossibility & bestAction() const;
int64_t bestActionValue() const;

View File

@ -124,7 +124,7 @@ TConstBonusListPtr StackWithBonuses::getAllBonuses(const CSelector & selector, c
for(const Bonus & bonus : bonusesToUpdate)
{
if(selector(&bonus) && (!limit || !limit(&bonus)))
if(selector(&bonus) && (!limit || limit(&bonus)))
{
if(ret->getFirst(Selector::source(BonusSource::SPELL_EFFECT, bonus.sid).And(Selector::typeSubtype(bonus.type, bonus.subtype))))
{
@ -150,12 +150,18 @@ TConstBonusListPtr StackWithBonuses::getAllBonuses(const CSelector & selector, c
int64_t StackWithBonuses::getTreeVersion() const
{
return owner->getTreeVersion();
auto result = owner->getTreeVersion();
if(bonusesToAdd.empty() && bonusesToUpdate.empty() && bonusesToRemove.empty())
return result;
else
return result + treeVersionLocal;
}
void StackWithBonuses::addUnitBonus(const std::vector<Bonus> & bonus)
{
vstd::concatenate(bonusesToAdd, bonus);
treeVersionLocal++;
}
void StackWithBonuses::updateUnitBonus(const std::vector<Bonus> & bonus)
@ -163,6 +169,7 @@ void StackWithBonuses::updateUnitBonus(const std::vector<Bonus> & bonus)
//TODO: optimize, actualize to last value
vstd::concatenate(bonusesToUpdate, bonus);
treeVersionLocal++;
}
void StackWithBonuses::removeUnitBonus(const std::vector<Bonus> & bonus)
@ -197,6 +204,8 @@ void StackWithBonuses::removeUnitBonus(const CSelector & selector)
vstd::erase_if(bonusesToAdd, [&](const Bonus & b){return selector(&b);});
vstd::erase_if(bonusesToUpdate, [&](const Bonus & b){return selector(&b);});
treeVersionLocal++;
}
std::string StackWithBonuses::getDescription() const

View File

@ -47,6 +47,7 @@ public:
std::vector<Bonus> bonusesToAdd;
std::vector<Bonus> bonusesToUpdate;
std::set<std::shared_ptr<Bonus>> bonusesToRemove;
int treeVersionLocal;
StackWithBonuses(const HypotheticBattle * Owner, const battle::CUnitState * Stack);

View File

@ -138,8 +138,8 @@ void Nullkiller::updateAiState(int pass, bool fast)
{
memory->removeInvisibleObjects(cb.get());
dangerHitMap->calculateTileOwners();
dangerHitMap->updateHitMap();
dangerHitMap->calculateTileOwners();
boost::this_thread::interruption_point();