2022-01-27 09:00:27 +02:00
|
|
|
/*
|
|
|
|
* BattleAI.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "BattleExchangeVariant.h"
|
|
|
|
#include "../../lib/CStack.h"
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
AttackerValue::AttackerValue()
|
|
|
|
{
|
|
|
|
value = 0;
|
|
|
|
isRetalitated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MoveTarget::MoveTarget()
|
2023-10-03 07:16:12 +02:00
|
|
|
: positions(), cachedAttack(), score(EvaluationResult::INEFFECTIVE_SCORE), scorePerTurn(EvaluationResult::INEFFECTIVE_SCORE)
|
2022-04-11 08:12:41 +02:00
|
|
|
{
|
2023-08-08 17:37:42 +02:00
|
|
|
turnsToRich = 1;
|
2022-04-11 08:12:41 +02:00
|
|
|
}
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
float BattleExchangeVariant::trackAttack(
|
|
|
|
const AttackPossibility & ap,
|
|
|
|
std::shared_ptr<HypotheticBattle> hb,
|
|
|
|
DamageCache & damageCache)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-08-26 12:06:41 +02:00
|
|
|
auto attacker = hb->getForUpdate(ap.attack.attacker->unitId());
|
|
|
|
|
|
|
|
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
|
|
|
|
static const auto selectorBlocksRetaliation = Selector::type()(BonusType::BLOCKS_RETALIATION);
|
|
|
|
const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
|
|
|
|
|
|
|
|
float attackValue = 0;
|
2022-01-27 09:00:27 +02:00
|
|
|
auto affectedUnits = ap.affectedUnits;
|
|
|
|
|
|
|
|
affectedUnits.push_back(ap.attackerState);
|
|
|
|
|
|
|
|
for(auto affectedUnit : affectedUnits)
|
|
|
|
{
|
2023-08-26 12:06:41 +02:00
|
|
|
auto unitToUpdate = hb->getForUpdate(affectedUnit->unitId());
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
if(unitToUpdate->unitSide() == attacker->unitSide())
|
|
|
|
{
|
|
|
|
if(unitToUpdate->unitId() == attacker->unitId())
|
|
|
|
{
|
|
|
|
auto defender = hb->getForUpdate(ap.attack.defender->unitId());
|
|
|
|
|
|
|
|
if(!defender->alive() || counterAttacksBlocked || ap.attack.shooting || !defender->ableToRetaliate())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto retaliationDamage = damageCache.getDamage(defender.get(), unitToUpdate.get(), hb);
|
|
|
|
auto attackerDamageReduce = AttackPossibility::calculateDamageReduce(defender.get(), unitToUpdate.get(), retaliationDamage, damageCache, hb);
|
|
|
|
|
|
|
|
attackValue -= attackerDamageReduce;
|
2023-10-03 07:16:12 +02:00
|
|
|
dpsScore.ourDamageReduce += attackerDamageReduce;
|
2023-08-26 12:06:41 +02:00
|
|
|
attackerValue[unitToUpdate->unitId()].isRetalitated = true;
|
|
|
|
|
|
|
|
unitToUpdate->damage(retaliationDamage);
|
|
|
|
defender->afterAttack(false, true);
|
|
|
|
|
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
|
|
|
logAi->trace(
|
|
|
|
"%s -> %s, ap retalitation, %s, dps: %2f, score: %2f",
|
|
|
|
defender->getDescription(),
|
|
|
|
unitToUpdate->getDescription(),
|
|
|
|
ap.attack.shooting ? "shot" : "mellee",
|
|
|
|
retaliationDamage,
|
|
|
|
attackerDamageReduce);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto collateralDamage = damageCache.getDamage(attacker.get(), unitToUpdate.get(), hb);
|
|
|
|
auto collateralDamageReduce = AttackPossibility::calculateDamageReduce(attacker.get(), unitToUpdate.get(), collateralDamage, damageCache, hb);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
attackValue -= collateralDamageReduce;
|
2023-10-03 07:16:12 +02:00
|
|
|
dpsScore.ourDamageReduce += collateralDamageReduce;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
unitToUpdate->damage(collateralDamage);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2023-08-26 12:06:41 +02:00
|
|
|
logAi->trace(
|
|
|
|
"%s -> %s, ap collateral, %s, dps: %2f, score: %2f",
|
|
|
|
attacker->getDescription(),
|
|
|
|
unitToUpdate->getDescription(),
|
|
|
|
ap.attack.shooting ? "shot" : "mellee",
|
|
|
|
collateralDamage,
|
|
|
|
collateralDamageReduce);
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2023-08-26 12:06:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int64_t attackDamage = damageCache.getDamage(attacker.get(), unitToUpdate.get(), hb);
|
|
|
|
float defenderDamageReduce = AttackPossibility::calculateDamageReduce(attacker.get(), unitToUpdate.get(), attackDamage, damageCache, hb);
|
|
|
|
|
|
|
|
attackValue += defenderDamageReduce;
|
2023-10-03 07:16:12 +02:00
|
|
|
dpsScore.enemyDamageReduce += defenderDamageReduce;
|
2023-08-26 12:06:41 +02:00
|
|
|
attackerValue[attacker->unitId()].value += defenderDamageReduce;
|
|
|
|
|
|
|
|
unitToUpdate->damage(attackDamage);
|
|
|
|
|
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
|
|
|
logAi->trace(
|
|
|
|
"%s -> %s, ap attack, %s, dps: %2f, score: %2f",
|
|
|
|
attacker->getDescription(),
|
|
|
|
unitToUpdate->getDescription(),
|
|
|
|
ap.attack.shooting ? "shot" : "mellee",
|
|
|
|
attackDamage,
|
|
|
|
defenderDamageReduce);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 21:04:26 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL >= 1
|
|
|
|
logAi->trace("ap shooters blocking: %lld", ap.shootersBlockedDmg);
|
|
|
|
#endif
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
attackValue += ap.shootersBlockedDmg;
|
2023-10-03 07:16:12 +02:00
|
|
|
dpsScore.enemyDamageReduce += ap.shootersBlockedDmg;
|
2023-08-26 12:06:41 +02:00
|
|
|
attacker->afterAttack(ap.attack.shooting, false);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
return attackValue;
|
|
|
|
}
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
float BattleExchangeVariant::trackAttack(
|
2022-01-27 09:00:27 +02:00
|
|
|
std::shared_ptr<StackWithBonuses> attacker,
|
|
|
|
std::shared_ptr<StackWithBonuses> defender,
|
|
|
|
bool shooting,
|
|
|
|
bool isOurAttack,
|
2023-08-08 17:37:42 +02:00
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<HypotheticBattle> hb,
|
2022-01-27 09:00:27 +02:00
|
|
|
bool evaluateOnly)
|
|
|
|
{
|
|
|
|
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
|
2023-05-01 00:20:01 +02:00
|
|
|
static const auto selectorBlocksRetaliation = Selector::type()(BonusType::BLOCKS_RETALIATION);
|
2022-01-27 09:00:27 +02:00
|
|
|
const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
int64_t attackDamage = damageCache.getDamage(attacker.get(), defender.get(), hb);
|
2023-08-26 12:06:41 +02:00
|
|
|
float defenderDamageReduce = AttackPossibility::calculateDamageReduce(attacker.get(), defender.get(), attackDamage, damageCache, hb);
|
|
|
|
float attackerDamageReduce = 0;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(!evaluateOnly)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2022-01-27 09:00:27 +02:00
|
|
|
logAi->trace(
|
2023-08-26 12:06:41 +02:00
|
|
|
"%s -> %s, normal attack, %s, dps: %lld, %2f",
|
2022-01-27 09:00:27 +02:00
|
|
|
attacker->getDescription(),
|
|
|
|
defender->getDescription(),
|
|
|
|
shooting ? "shot" : "mellee",
|
|
|
|
attackDamage,
|
2022-10-17 18:47:16 +02:00
|
|
|
defenderDamageReduce);
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(isOurAttack)
|
|
|
|
{
|
2023-10-03 07:16:12 +02:00
|
|
|
dpsScore.enemyDamageReduce += defenderDamageReduce;
|
2022-10-17 18:47:16 +02:00
|
|
|
attackerValue[attacker->unitId()].value += defenderDamageReduce;
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
else
|
2023-10-03 07:16:12 +02:00
|
|
|
dpsScore.ourDamageReduce += defenderDamageReduce;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
defender->damage(attackDamage);
|
|
|
|
attacker->afterAttack(shooting, false);
|
|
|
|
}
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
if(!evaluateOnly && defender->alive() && defender->ableToRetaliate() && !counterAttacksBlocked && !shooting)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-08-08 17:37:42 +02:00
|
|
|
auto retaliationDamage = damageCache.getDamage(defender.get(), attacker.get(), hb);
|
|
|
|
attackerDamageReduce = AttackPossibility::calculateDamageReduce(defender.get(), attacker.get(), retaliationDamage, damageCache, hb);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2023-08-26 12:06:41 +02:00
|
|
|
logAi->trace(
|
|
|
|
"%s -> %s, retaliation, dps: %lld, %2f",
|
|
|
|
defender->getDescription(),
|
|
|
|
attacker->getDescription(),
|
|
|
|
retaliationDamage,
|
|
|
|
attackerDamageReduce);
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
if(isOurAttack)
|
|
|
|
{
|
2023-10-03 07:16:12 +02:00
|
|
|
dpsScore.ourDamageReduce += attackerDamageReduce;
|
2023-08-26 12:06:41 +02:00
|
|
|
attackerValue[attacker->unitId()].isRetalitated = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-03 07:16:12 +02:00
|
|
|
dpsScore.enemyDamageReduce += attackerDamageReduce;
|
2023-08-26 12:06:41 +02:00
|
|
|
attackerValue[defender->unitId()].value += attackerDamageReduce;
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
2023-08-26 12:06:41 +02:00
|
|
|
|
|
|
|
attacker->damage(retaliationDamage);
|
|
|
|
defender->afterAttack(false, true);
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
auto score = defenderDamageReduce - attackerDamageReduce;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2022-01-27 09:00:27 +02:00
|
|
|
if(!score)
|
|
|
|
{
|
2023-10-03 07:16:12 +02:00
|
|
|
logAi->trace("Attack has zero score def:%2f att:%2f", defenderDamageReduce, attackerDamageReduce);
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
float BattleExchangeEvaluator::scoreValue(const BattleScore & score) const
|
|
|
|
{
|
|
|
|
return score.enemyDamageReduce * getPositiveEffectMultiplier() - score.ourDamageReduce * getNegativeEffectMultiplier();
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
EvaluationResult BattleExchangeEvaluator::findBestTarget(
|
|
|
|
const battle::Unit * activeStack,
|
|
|
|
PotentialTargets & targets,
|
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<HypotheticBattle> hb)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
|
|
|
EvaluationResult result(targets.bestAction());
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
if(!activeStack->waited() && !activeStack->acquireState()->hadMorale)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2022-01-27 09:00:27 +02:00
|
|
|
logAi->trace("Evaluating waited attack for %s", activeStack->getDescription());
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
auto hbWaited = std::make_shared<HypotheticBattle>(env.get(), hb);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
hbWaited->getForUpdate(activeStack->unitId())->waiting = true;
|
|
|
|
hbWaited->getForUpdate(activeStack->unitId())->waitedThisTurn = true;
|
|
|
|
|
|
|
|
updateReachabilityMap(hbWaited);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
for(auto & ap : targets.possibleAttacks)
|
|
|
|
{
|
2023-10-13 21:04:26 +02:00
|
|
|
float score = evaluateExchange(ap, 0, targets, damageCache, hbWaited);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(score > result.score)
|
|
|
|
{
|
|
|
|
result.score = score;
|
|
|
|
result.bestAttack = ap;
|
|
|
|
result.wait = true;
|
2023-10-13 21:04:26 +02:00
|
|
|
|
|
|
|
#if BATTLE_TRACE_LEVEL >= 1
|
|
|
|
logAi->trace("New high score %2f", result.score);
|
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
|
|
|
logAi->trace("Evaluating normal attack for %s", activeStack->getDescription());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
updateReachabilityMap(hb);
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
if(result.bestAttack.attack.shooting && hb->battleHasShootingPenalty(activeStack, result.bestAttack.dest))
|
|
|
|
{
|
|
|
|
if(!canBeHitThisTurn(result.bestAttack))
|
|
|
|
return result; // lets wait
|
|
|
|
}
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
for(auto & ap : targets.possibleAttacks)
|
|
|
|
{
|
2023-10-13 21:04:26 +02:00
|
|
|
float score = evaluateExchange(ap, 0, targets, damageCache, hb);
|
2023-08-26 12:06:41 +02:00
|
|
|
|
2023-10-13 21:04:26 +02:00
|
|
|
if(score > result.score || score == result.score && result.wait)
|
2023-08-26 12:06:41 +02:00
|
|
|
{
|
|
|
|
result.score = score;
|
|
|
|
result.bestAttack = ap;
|
|
|
|
result.wait = false;
|
2023-10-13 21:04:26 +02:00
|
|
|
|
|
|
|
#if BATTLE_TRACE_LEVEL >= 1
|
|
|
|
logAi->trace("New high score %2f", result.score);
|
|
|
|
#endif
|
2023-08-26 12:06:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
MoveTarget BattleExchangeEvaluator::findMoveTowardsUnreachable(
|
|
|
|
const battle::Unit * activeStack,
|
|
|
|
PotentialTargets & targets,
|
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<HypotheticBattle> hb)
|
2022-04-11 08:12:41 +02:00
|
|
|
{
|
|
|
|
MoveTarget result;
|
2023-10-03 07:16:12 +02:00
|
|
|
BattleExchangeVariant ev;
|
2022-04-11 08:12:41 +02:00
|
|
|
|
|
|
|
if(targets.unreachableEnemies.empty())
|
|
|
|
return result;
|
|
|
|
|
2023-05-02 00:05:59 +02:00
|
|
|
auto speed = activeStack->speed();
|
2022-12-10 12:11:14 +02:00
|
|
|
|
|
|
|
if(speed == 0)
|
|
|
|
return result;
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
updateReachabilityMap(hb);
|
|
|
|
|
|
|
|
auto dists = cb->getReachability(activeStack);
|
|
|
|
|
|
|
|
for(const battle::Unit * enemy : targets.unreachableEnemies)
|
|
|
|
{
|
|
|
|
std::vector<const battle::Unit *> adjacentStacks = getAdjacentUnits(enemy);
|
|
|
|
auto closestStack = *vstd::minElementByFun(adjacentStacks, [&](const battle::Unit * u) -> int64_t
|
|
|
|
{
|
|
|
|
return dists.distToNearestNeighbour(activeStack, u) * 100000 - activeStack->getTotalHealth();
|
|
|
|
});
|
|
|
|
|
|
|
|
auto distance = dists.distToNearestNeighbour(activeStack, closestStack);
|
|
|
|
|
|
|
|
if(distance >= GameConstants::BFIELD_SIZE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(distance <= speed)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto turnsToRich = (distance - 1) / speed + 1;
|
|
|
|
auto hexes = closestStack->getSurroundingHexes();
|
2023-10-03 07:16:12 +02:00
|
|
|
auto enemySpeed = closestStack->speed();
|
|
|
|
auto speedRatio = speed / static_cast<float>(enemySpeed);
|
2023-10-13 21:04:26 +02:00
|
|
|
auto multiplier = speedRatio > 1 ? 1 : speedRatio;
|
|
|
|
|
|
|
|
if(enemy->canShoot())
|
|
|
|
multiplier *= 1.5f;
|
2022-04-11 08:12:41 +02:00
|
|
|
|
|
|
|
for(auto hex : hexes)
|
|
|
|
{
|
2023-01-14 19:03:18 +02:00
|
|
|
// FIXME: provide distance info for Jousting bonus
|
|
|
|
auto bai = BattleAttackInfo(activeStack, closestStack, 0, cb->battleCanShoot(activeStack));
|
2023-08-08 17:37:42 +02:00
|
|
|
auto attack = AttackPossibility::evaluate(bai, hex, damageCache, hb);
|
2022-04-11 08:12:41 +02:00
|
|
|
|
|
|
|
attack.shootersBlockedDmg = 0; // we do not want to count on it, it is not for sure
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
auto score = calculateExchange(attack, turnsToRich, targets, damageCache, hb);
|
2023-10-13 21:04:26 +02:00
|
|
|
auto scorePerTurn = BattleScore(score.enemyDamageReduce * std::sqrt(multiplier / turnsToRich), score.ourDamageReduce);
|
2022-04-11 08:12:41 +02:00
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
if(result.scorePerTurn < scoreValue(scorePerTurn))
|
2022-04-11 08:12:41 +02:00
|
|
|
{
|
2023-10-03 07:16:12 +02:00
|
|
|
result.scorePerTurn = scoreValue(scorePerTurn);
|
|
|
|
result.score = scoreValue(score);
|
2022-04-11 08:12:41 +02:00
|
|
|
result.positions = closestStack->getAttackableHexes(activeStack);
|
2023-08-08 17:37:42 +02:00
|
|
|
result.cachedAttack = attack;
|
|
|
|
result.turnsToRich = turnsToRich;
|
2022-04-11 08:12:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const battle::Unit *> BattleExchangeEvaluator::getAdjacentUnits(const battle::Unit * blockerUnit)
|
|
|
|
{
|
|
|
|
std::queue<const battle::Unit *> queue;
|
|
|
|
std::vector<const battle::Unit *> checkedStacks;
|
|
|
|
|
|
|
|
queue.push(blockerUnit);
|
|
|
|
|
|
|
|
while(!queue.empty())
|
|
|
|
{
|
|
|
|
auto stack = queue.front();
|
|
|
|
|
|
|
|
queue.pop();
|
|
|
|
checkedStacks.push_back(stack);
|
|
|
|
|
|
|
|
auto hexes = stack->getSurroundingHexes();
|
|
|
|
for(auto hex : hexes)
|
|
|
|
{
|
2023-04-01 01:58:40 +02:00
|
|
|
auto neighbor = cb->battleGetUnitByPos(hex);
|
2022-04-11 08:12:41 +02:00
|
|
|
|
|
|
|
if(neighbor && neighbor->unitSide() == stack->unitSide() && !vstd::contains(checkedStacks, neighbor))
|
|
|
|
{
|
|
|
|
queue.push(neighbor);
|
|
|
|
checkedStacks.push_back(neighbor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return checkedStacks;
|
|
|
|
}
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
ReachabilityData BattleExchangeEvaluator::getExchangeUnits(
|
2022-04-11 08:12:41 +02:00
|
|
|
const AttackPossibility & ap,
|
2023-10-13 21:04:26 +02:00
|
|
|
uint8_t turn,
|
2022-04-11 08:12:41 +02:00
|
|
|
PotentialTargets & targets,
|
2023-08-08 17:37:42 +02:00
|
|
|
std::shared_ptr<HypotheticBattle> hb)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-10-03 07:16:12 +02:00
|
|
|
ReachabilityData result;
|
|
|
|
|
2023-10-13 21:04:26 +02:00
|
|
|
auto hexes = ap.attack.defender->getSurroundingHexes();
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(!ap.attack.shooting) hexes.push_back(ap.from);
|
|
|
|
|
|
|
|
std::vector<const battle::Unit *> allReachableUnits;
|
|
|
|
|
|
|
|
for(auto hex : hexes)
|
|
|
|
{
|
2023-10-13 21:04:26 +02:00
|
|
|
vstd::concatenate(allReachableUnits, turn == 0 ? reachabilityMap[hex] : getOneTurnReachableUnits(turn, hex));
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vstd::removeDuplicates(allReachableUnits);
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
auto copy = allReachableUnits;
|
|
|
|
for(auto unit : copy)
|
|
|
|
{
|
|
|
|
for(auto adjacentUnit : getAdjacentUnits(unit))
|
|
|
|
{
|
2023-08-08 17:37:42 +02:00
|
|
|
auto unitWithBonuses = hb->battleGetUnitByID(adjacentUnit->unitId());
|
2022-04-11 08:12:41 +02:00
|
|
|
|
|
|
|
if(vstd::contains(targets.unreachableEnemies, adjacentUnit)
|
|
|
|
&& !vstd::contains(allReachableUnits, unitWithBonuses))
|
|
|
|
{
|
|
|
|
allReachableUnits.push_back(unitWithBonuses);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vstd::removeDuplicates(allReachableUnits);
|
|
|
|
|
|
|
|
if(!vstd::contains(allReachableUnits, ap.attack.attacker))
|
|
|
|
{
|
|
|
|
allReachableUnits.push_back(ap.attack.attacker);
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
if(allReachableUnits.size() < 2)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2022-01-27 09:00:27 +02:00
|
|
|
logAi->trace("Reachability map contains only %d stacks", allReachableUnits.size());
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto unit : allReachableUnits)
|
|
|
|
{
|
|
|
|
auto accessible = !unit->canShoot();
|
|
|
|
|
|
|
|
if(!accessible)
|
|
|
|
{
|
|
|
|
for(auto hex : unit->getSurroundingHexes())
|
|
|
|
{
|
|
|
|
if(ap.attack.defender->coversPos(hex))
|
|
|
|
{
|
|
|
|
accessible = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(accessible)
|
|
|
|
result.melleeAccessible.push_back(unit);
|
|
|
|
else
|
|
|
|
result.shooters.push_back(unit);
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for(int turn = 0; turn < turnOrder.size(); turn++)
|
|
|
|
{
|
|
|
|
for(auto unit : turnOrder[turn])
|
|
|
|
{
|
|
|
|
if(vstd::contains(allReachableUnits, unit))
|
2023-10-03 07:16:12 +02:00
|
|
|
result.units.push_back(unit);
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
vstd::erase_if(result.units, [&](const battle::Unit * u) -> bool
|
2023-08-19 09:22:34 +02:00
|
|
|
{
|
|
|
|
return !hb->battleGetUnitByID(u->unitId())->alive();
|
|
|
|
});
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
return result;
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
float BattleExchangeEvaluator::evaluateExchange(
|
|
|
|
const AttackPossibility & ap,
|
2023-10-13 21:04:26 +02:00
|
|
|
uint8_t turn,
|
2023-10-03 07:16:12 +02:00
|
|
|
PotentialTargets & targets,
|
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<HypotheticBattle> hb)
|
|
|
|
{
|
2023-10-13 21:04:26 +02:00
|
|
|
if(ap.from.hex == 127)
|
|
|
|
{
|
|
|
|
logAi->trace("x");
|
|
|
|
}
|
|
|
|
|
|
|
|
BattleScore score = calculateExchange(ap, turn, targets, damageCache, hb);
|
|
|
|
|
|
|
|
#if BATTLE_TRACE_LEVEL >= 1
|
|
|
|
logAi->trace(
|
|
|
|
"calculateExchange score +%2f -%2fx%2f = %2f",
|
|
|
|
score.enemyDamageReduce,
|
|
|
|
score.ourDamageReduce,
|
|
|
|
getNegativeEffectMultiplier(),
|
|
|
|
scoreValue(score));
|
|
|
|
#endif
|
2023-10-03 07:16:12 +02:00
|
|
|
|
|
|
|
return scoreValue(score);
|
|
|
|
}
|
|
|
|
|
|
|
|
BattleScore BattleExchangeEvaluator::calculateExchange(
|
2022-04-11 08:12:41 +02:00
|
|
|
const AttackPossibility & ap,
|
2023-10-13 21:04:26 +02:00
|
|
|
uint8_t turn,
|
2022-04-11 08:12:41 +02:00
|
|
|
PotentialTargets & targets,
|
2023-08-08 17:37:42 +02:00
|
|
|
DamageCache & damageCache,
|
|
|
|
std::shared_ptr<HypotheticBattle> hb)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2023-08-26 12:06:41 +02:00
|
|
|
logAi->trace("Battle exchange at %d", ap.attack.shooting ? ap.dest.hex : ap.from.hex);
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-04-08 13:05:47 +02:00
|
|
|
if(cb->battleGetMySide() == BattlePerspective::LEFT_SIDE
|
|
|
|
&& cb->battleGetGateState() == EGateState::BLOCKED
|
2023-08-19 21:35:44 +02:00
|
|
|
&& ap.attack.defender->coversPos(BattleHex::GATE_BRIDGE))
|
2023-04-08 13:05:47 +02:00
|
|
|
{
|
2023-10-03 07:16:12 +02:00
|
|
|
return BattleScore(EvaluationResult::INEFFECTIVE_SCORE, 0);
|
2023-04-08 13:05:47 +02:00
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
std::vector<const battle::Unit *> ourStacks;
|
|
|
|
std::vector<const battle::Unit *> enemyStacks;
|
|
|
|
|
2023-08-19 09:22:34 +02:00
|
|
|
if(hb->battleGetUnitByID(ap.attack.defender->unitId())->alive())
|
|
|
|
enemyStacks.push_back(ap.attack.defender);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
ReachabilityData exchangeUnits = getExchangeUnits(ap, turn, targets, hb);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
if(exchangeUnits.units.empty())
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-10-03 07:16:12 +02:00
|
|
|
return BattleScore();
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
auto exchangeBattle = std::make_shared<HypotheticBattle>(env.get(), hb);
|
2023-10-03 07:16:12 +02:00
|
|
|
BattleExchangeVariant v;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
for(auto unit : exchangeUnits.units)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-05-10 08:06:53 +02:00
|
|
|
if(unit->isTurret())
|
|
|
|
continue;
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
bool isOur = exchangeBattle->battleMatchOwner(ap.attack.attacker, unit, true);
|
2022-01-27 09:00:27 +02:00
|
|
|
auto & attackerQueue = isOur ? ourStacks : enemyStacks;
|
2023-10-13 21:04:26 +02:00
|
|
|
auto u = exchangeBattle->getForUpdate(unit->unitId());
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-10-13 21:04:26 +02:00
|
|
|
if(u->alive() && !vstd::contains(attackerQueue, unit))
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
|
|
|
attackerQueue.push_back(unit);
|
2023-10-13 21:04:26 +02:00
|
|
|
|
|
|
|
#if BATTLE_TRACE_LEVEL
|
|
|
|
logAi->trace("Exchanging: %s", u->getDescription());
|
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-19 09:22:34 +02:00
|
|
|
auto melleeAttackers = ourStacks;
|
|
|
|
|
|
|
|
vstd::removeDuplicates(melleeAttackers);
|
|
|
|
vstd::erase_if(melleeAttackers, [&](const battle::Unit * u) -> bool
|
|
|
|
{
|
|
|
|
return !cb->battleCanShoot(u);
|
|
|
|
});
|
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
bool canUseAp = true;
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
for(auto activeUnit : exchangeUnits.units)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-08-08 17:37:42 +02:00
|
|
|
bool isOur = exchangeBattle->battleMatchOwner(ap.attack.attacker, activeUnit, true);
|
2022-01-27 09:00:27 +02:00
|
|
|
battle::Units & attackerQueue = isOur ? ourStacks : enemyStacks;
|
|
|
|
battle::Units & oppositeQueue = isOur ? enemyStacks : ourStacks;
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
auto attacker = exchangeBattle->getForUpdate(activeUnit->unitId());
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
if(!attacker->alive())
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2022-04-11 08:12:41 +02:00
|
|
|
logAi->trace( "Attacker is dead");
|
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto targetUnit = ap.attack.defender;
|
|
|
|
|
2023-08-19 09:22:34 +02:00
|
|
|
if(!isOur || !exchangeBattle->battleGetUnitByID(targetUnit->unitId())->alive())
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-08-26 12:06:41 +02:00
|
|
|
auto estimateAttack = [&](const battle::Unit * u) -> float
|
2022-04-11 08:12:41 +02:00
|
|
|
{
|
2023-08-08 17:37:42 +02:00
|
|
|
auto stackWithBonuses = exchangeBattle->getForUpdate(u->unitId());
|
2022-04-11 08:12:41 +02:00
|
|
|
auto score = v.trackAttack(
|
|
|
|
attacker,
|
|
|
|
stackWithBonuses,
|
2023-08-08 17:37:42 +02:00
|
|
|
exchangeBattle->battleCanShoot(stackWithBonuses.get()),
|
2022-04-11 08:12:41 +02:00
|
|
|
isOur,
|
2023-08-08 17:37:42 +02:00
|
|
|
damageCache,
|
|
|
|
hb,
|
2022-04-11 08:12:41 +02:00
|
|
|
true);
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2023-10-13 21:04:26 +02:00
|
|
|
logAi->trace("Best target selector %s->%s score = %2f", attacker->getDescription(), stackWithBonuses->getDescription(), score);
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return score;
|
|
|
|
};
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
auto unitsInOppositeQueueExceptInaccessible = oppositeQueue;
|
|
|
|
|
|
|
|
vstd::erase_if(unitsInOppositeQueueExceptInaccessible, [&](const battle::Unit * u)->bool
|
|
|
|
{
|
|
|
|
return vstd::contains(exchangeUnits.shooters, u);
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!unitsInOppositeQueueExceptInaccessible.empty())
|
2022-04-11 08:12:41 +02:00
|
|
|
{
|
2023-10-03 07:16:12 +02:00
|
|
|
targetUnit = *vstd::maxElementByFun(unitsInOppositeQueueExceptInaccessible, estimateAttack);
|
2022-04-11 08:12:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-08 17:37:42 +02:00
|
|
|
auto reachable = exchangeBattle->battleGetUnitsIf([&](const battle::Unit * u) -> bool
|
2022-04-11 08:12:41 +02:00
|
|
|
{
|
2023-08-19 09:22:34 +02:00
|
|
|
if(u->unitSide() == attacker->unitSide())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!exchangeBattle->getForUpdate(u->unitId())->alive())
|
2022-04-11 08:12:41 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return vstd::contains_if(reachabilityMap[u->getPosition()], [&](const battle::Unit * other) -> bool
|
|
|
|
{
|
|
|
|
return attacker->unitId() == other->unitId();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!reachable.empty())
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2022-04-11 08:12:41 +02:00
|
|
|
targetUnit = *vstd::maxElementByFun(reachable, estimateAttack);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2022-04-11 08:12:41 +02:00
|
|
|
logAi->trace("Battle queue is empty and no reachable enemy.");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
auto defender = exchangeBattle->getForUpdate(targetUnit->unitId());
|
|
|
|
auto shooting = exchangeBattle->battleCanShoot(attacker.get());
|
2022-01-27 09:00:27 +02:00
|
|
|
const int totalAttacks = attacker->getTotalAttacks(shooting);
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
if(canUseAp && activeUnit->unitId() == ap.attack.attacker->unitId()
|
|
|
|
&& targetUnit->unitId() == ap.attack.defender->unitId())
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-08-26 12:06:41 +02:00
|
|
|
v.trackAttack(ap, exchangeBattle, damageCache);
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int i = 0; i < totalAttacks; i++)
|
|
|
|
{
|
2023-08-08 17:37:42 +02:00
|
|
|
v.trackAttack(attacker, defender, shooting, isOur, damageCache, exchangeBattle);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(!attacker->alive() || !defender->alive())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
canUseAp = false;
|
|
|
|
|
|
|
|
vstd::erase_if(attackerQueue, [&](const battle::Unit * u) -> bool
|
|
|
|
{
|
2023-08-19 09:22:34 +02:00
|
|
|
return !exchangeBattle->battleGetUnitByID(u->unitId())->alive();
|
2022-01-27 09:00:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
vstd::erase_if(oppositeQueue, [&](const battle::Unit * u) -> bool
|
|
|
|
{
|
2023-08-19 09:22:34 +02:00
|
|
|
return !exchangeBattle->battleGetUnitByID(u->unitId())->alive();
|
2022-01-27 09:00:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
// avoid blocking path for stronger stack by weaker stack
|
|
|
|
// the method checks if all stacks can be placed around enemy
|
2023-10-13 21:04:26 +02:00
|
|
|
std::map<BattleHex, battle::Units> reachabilityMap;
|
|
|
|
|
|
|
|
auto hexes = ap.attack.defender->getSurroundingHexes();
|
|
|
|
|
|
|
|
for(auto hex : hexes)
|
|
|
|
reachabilityMap[hex] = getOneTurnReachableUnits(turn, hex);
|
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
v.adjustPositions(melleeAttackers, ap, reachabilityMap);
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2023-10-03 07:16:12 +02:00
|
|
|
logAi->trace("Exchange score: enemy: %2f, our -%2f", v.getScore().enemyDamageReduce, v.getScore().ourDamageReduce);
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
return v.getScore();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleExchangeVariant::adjustPositions(
|
|
|
|
std::vector<const battle::Unit*> attackers,
|
|
|
|
const AttackPossibility & ap,
|
|
|
|
std::map<BattleHex, battle::Units> & reachabilityMap)
|
|
|
|
{
|
|
|
|
auto hexes = ap.attack.defender->getSurroundingHexes();
|
|
|
|
|
|
|
|
boost::sort(attackers, [&](const battle::Unit * u1, const battle::Unit * u2) -> bool
|
|
|
|
{
|
|
|
|
if(attackerValue[u1->unitId()].isRetalitated && !attackerValue[u2->unitId()].isRetalitated)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(attackerValue[u2->unitId()].isRetalitated && !attackerValue[u1->unitId()].isRetalitated)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return attackerValue[u1->unitId()].value > attackerValue[u2->unitId()].value;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!ap.attack.shooting)
|
|
|
|
{
|
|
|
|
vstd::erase_if_present(hexes, ap.from);
|
|
|
|
vstd::erase_if_present(hexes, ap.attack.attacker->occupiedHex(ap.attack.attackerPos));
|
|
|
|
}
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
float notRealizedDamage = 0;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
for(auto unit : attackers)
|
|
|
|
{
|
|
|
|
if(unit->unitId() == ap.attack.attacker->unitId())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!vstd::contains_if(hexes, [&](BattleHex h) -> bool
|
|
|
|
{
|
|
|
|
return vstd::contains(reachabilityMap[h], unit);
|
|
|
|
}))
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
notRealizedDamage += attackerValue[unit->unitId()].value;
|
2022-01-27 09:00:27 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-08-26 12:06:41 +02:00
|
|
|
auto desiredPosition = vstd::minElementByFun(hexes, [&](BattleHex h) -> float
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
|
|
|
auto score = vstd::contains(reachabilityMap[h], unit)
|
|
|
|
? reachabilityMap[h].size()
|
2022-10-17 18:47:16 +02:00
|
|
|
: 0;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(unit->doubleWide())
|
|
|
|
{
|
|
|
|
auto backHex = unit->occupiedHex(h);
|
|
|
|
|
|
|
|
if(vstd::contains(hexes, backHex))
|
|
|
|
score += reachabilityMap[backHex].size();
|
|
|
|
}
|
|
|
|
|
|
|
|
return score;
|
|
|
|
});
|
|
|
|
|
|
|
|
hexes.erase(desiredPosition);
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
if(notRealizedDamage > ap.attackValue() && notRealizedDamage > attackerValue[ap.attack.attacker->unitId()].value)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-10-03 07:16:12 +02:00
|
|
|
dpsScore = BattleScore(EvaluationResult::INEFFECTIVE_SCORE, 0);
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
bool BattleExchangeEvaluator::canBeHitThisTurn(const AttackPossibility & ap)
|
|
|
|
{
|
|
|
|
for(auto pos : ap.attack.attacker->getSurroundingHexes())
|
|
|
|
{
|
|
|
|
for(auto u : reachabilityMap[pos])
|
|
|
|
{
|
|
|
|
if(u->unitSide() != ap.attack.attacker->unitSide())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-13 21:04:26 +02:00
|
|
|
void BattleExchangeEvaluator::updateReachabilityMap(std::shared_ptr<HypotheticBattle> hb)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
const int TURN_DEPTH = 2;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
turnOrder.clear();
|
2023-10-13 21:04:26 +02:00
|
|
|
|
2023-08-08 17:37:42 +02:00
|
|
|
hb->battleGetTurnOrder(turnOrder, std::numeric_limits<int>::max(), TURN_DEPTH);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-10-13 21:04:26 +02:00
|
|
|
for(auto turn : turnOrder)
|
|
|
|
{
|
|
|
|
for(auto u : turn)
|
|
|
|
{
|
|
|
|
if(!vstd::contains(reachabilityCache, u->unitId()))
|
|
|
|
{
|
|
|
|
reachabilityCache[u->unitId()] = hb->getReachability(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-10-13 21:04:26 +02:00
|
|
|
reachabilityMap[hex] = getOneTurnReachableUnits(0, hex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const battle::Unit *> BattleExchangeEvaluator::getOneTurnReachableUnits(uint8_t turn, BattleHex hex)
|
|
|
|
{
|
|
|
|
std::vector<const battle::Unit *> result;
|
|
|
|
|
|
|
|
for(int i = 0; i < turnOrder.size(); i++, turn++)
|
|
|
|
{
|
|
|
|
auto & turnQueue = turnOrder[i];
|
2022-01-27 09:00:27 +02:00
|
|
|
HypotheticBattle turnBattle(env.get(), cb);
|
|
|
|
|
|
|
|
for(const battle::Unit * unit : turnQueue)
|
|
|
|
{
|
2023-05-10 08:06:53 +02:00
|
|
|
if(unit->isTurret())
|
|
|
|
continue;
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
if(turnBattle.battleCanShoot(unit))
|
|
|
|
{
|
2023-10-13 21:04:26 +02:00
|
|
|
result.push_back(unit);
|
2022-04-11 08:12:41 +02:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-10-13 21:04:26 +02:00
|
|
|
auto unitSpeed = unit->speed(turn);
|
|
|
|
auto radius = unitSpeed * (turn + 1);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-10-13 21:04:26 +02:00
|
|
|
ReachabilityInfo unitReachability = vstd::getOrCompute(
|
|
|
|
reachabilityCache,
|
|
|
|
unit->unitId(),
|
|
|
|
[&](ReachabilityInfo & data)
|
|
|
|
{
|
|
|
|
data = turnBattle.getReachability(unit);
|
|
|
|
});
|
|
|
|
|
|
|
|
bool reachable = unitReachability.distances[hex] <= radius;
|
|
|
|
|
|
|
|
if(!reachable && unitReachability.accessibility[hex] == EAccessibility::ALIVE_STACK)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-10-13 21:04:26 +02:00
|
|
|
const battle::Unit * hexStack = cb->battleGetUnitByPos(hex);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-10-13 21:04:26 +02:00
|
|
|
if(hexStack && cb->battleMatchOwner(unit, hexStack, false))
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-10-13 21:04:26 +02:00
|
|
|
for(BattleHex neighbor : hex.neighbouringTiles())
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-10-13 21:04:26 +02:00
|
|
|
reachable = unitReachability.distances[neighbor] <= radius;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-10-13 21:04:26 +02:00
|
|
|
if(reachable) break;
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-13 21:04:26 +02:00
|
|
|
}
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2023-10-13 21:04:26 +02:00
|
|
|
if(reachable)
|
|
|
|
{
|
|
|
|
result.push_back(unit);
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-13 21:04:26 +02:00
|
|
|
|
|
|
|
return result;
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
// avoid blocking path for stronger stack by weaker stack
|
2022-01-27 09:00:27 +02:00
|
|
|
bool BattleExchangeEvaluator::checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * activeUnit, BattleHex position)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
const int BLOCKING_THRESHOLD = 70;
|
|
|
|
const int BLOCKING_OWN_ATTACK_PENALTY = 100;
|
|
|
|
const int BLOCKING_OWN_MOVE_PENALTY = 1;
|
|
|
|
|
|
|
|
float blockingScore = 0;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
auto activeUnitDamage = activeUnit->getMinDamage(hb.battleCanShoot(activeUnit)) * activeUnit->getCount();
|
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
for(int turn = 0; turn < turnOrder.size(); turn++)
|
|
|
|
{
|
|
|
|
auto & turnQueue = turnOrder[turn];
|
|
|
|
HypotheticBattle turnBattle(env.get(), cb);
|
|
|
|
|
|
|
|
auto unitToUpdate = turnBattle.getForUpdate(activeUnit->unitId());
|
|
|
|
unitToUpdate->setPosition(position);
|
|
|
|
|
|
|
|
for(const battle::Unit * unit : turnQueue)
|
|
|
|
{
|
|
|
|
if(unit->unitId() == unitToUpdate->unitId() || cb->battleMatchOwner(unit, activeUnit, false))
|
|
|
|
continue;
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
auto blockedUnitDamage = unit->getMinDamage(hb.battleCanShoot(unit)) * unit->getCount();
|
2022-10-17 18:47:16 +02:00
|
|
|
auto ratio = blockedUnitDamage / (blockedUnitDamage + activeUnitDamage);
|
2022-04-11 08:12:41 +02:00
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
auto unitReachability = turnBattle.getReachability(unit);
|
|
|
|
|
|
|
|
for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
|
|
|
|
{
|
|
|
|
bool enemyUnit = false;
|
2023-05-02 00:05:59 +02:00
|
|
|
bool reachable = unitReachability.distances[hex] <= unit->speed(turn);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(!reachable && unitReachability.accessibility[hex] == EAccessibility::ALIVE_STACK)
|
|
|
|
{
|
|
|
|
const battle::Unit * hexStack = turnBattle.battleGetUnitByPos(hex);
|
|
|
|
|
|
|
|
if(hexStack && cb->battleMatchOwner(unit, hexStack, false))
|
|
|
|
{
|
|
|
|
enemyUnit = true;
|
|
|
|
|
|
|
|
for(BattleHex neighbor : hex.neighbouringTiles())
|
|
|
|
{
|
2023-05-02 00:05:59 +02:00
|
|
|
reachable = unitReachability.distances[neighbor] <= unit->speed(turn);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(reachable) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!reachable && vstd::contains(reachabilityMap[hex], unit))
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
blockingScore += ratio * (enemyUnit ? BLOCKING_OWN_ATTACK_PENALTY : BLOCKING_OWN_MOVE_PENALTY);
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
|
|
|
logAi->trace("Position %d, blocking score %f", position.hex, blockingScore);
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
return blockingScore > BLOCKING_THRESHOLD;
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|