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()
|
2022-10-17 18:47:16 +02:00
|
|
|
: positions()
|
2022-04-11 08:12:41 +02:00
|
|
|
{
|
|
|
|
score = EvaluationResult::INEFFECTIVE_SCORE;
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
int64_t BattleExchangeVariant::trackAttack(const AttackPossibility & ap, HypotheticBattle & state)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
|
|
|
auto affectedUnits = ap.affectedUnits;
|
|
|
|
|
|
|
|
affectedUnits.push_back(ap.attackerState);
|
|
|
|
|
|
|
|
for(auto affectedUnit : affectedUnits)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
auto unitToUpdate = state.getForUpdate(affectedUnit->unitId());
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
unitToUpdate->health = affectedUnit->health;
|
|
|
|
unitToUpdate->shots = affectedUnit->shots;
|
|
|
|
unitToUpdate->counterAttacks = affectedUnit->counterAttacks;
|
|
|
|
unitToUpdate->movedThisRound = affectedUnit->movedThisRound;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto attackValue = ap.attackValue();
|
|
|
|
|
|
|
|
dpsScore += attackValue;
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
2022-01-27 09:00:27 +02:00
|
|
|
logAi->trace(
|
2022-10-17 18:47:16 +02:00
|
|
|
"%s -> %s, ap attack, %s, dps: %lld, score: %lld",
|
2022-01-27 09:00:27 +02:00
|
|
|
ap.attack.attacker->getDescription(),
|
|
|
|
ap.attack.defender->getDescription(),
|
|
|
|
ap.attack.shooting ? "shot" : "mellee",
|
|
|
|
ap.damageDealt,
|
|
|
|
attackValue);
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
return attackValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t BattleExchangeVariant::trackAttack(
|
|
|
|
std::shared_ptr<StackWithBonuses> attacker,
|
|
|
|
std::shared_ptr<StackWithBonuses> defender,
|
|
|
|
bool shooting,
|
|
|
|
bool isOurAttack,
|
2022-10-17 18:47:16 +02:00
|
|
|
const CBattleInfoCallback & cb,
|
2022-01-27 09:00:27 +02:00
|
|
|
bool evaluateOnly)
|
|
|
|
{
|
|
|
|
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
|
|
|
|
static const auto selectorBlocksRetaliation = Selector::type()(Bonus::BLOCKS_RETALIATION);
|
|
|
|
const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
|
|
|
|
|
2023-03-24 17:17:17 +02:00
|
|
|
DamageEstimation retaliation;
|
2023-01-14 19:03:18 +02:00
|
|
|
// FIXME: provide distance info for Jousting bonus
|
|
|
|
BattleAttackInfo bai(attacker.get(), defender.get(), 0, shooting);
|
2022-04-11 08:12:41 +02:00
|
|
|
|
|
|
|
if(shooting)
|
|
|
|
{
|
|
|
|
bai.attackerPos.setXY(8, 5);
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
auto attack = cb.battleEstimateDamage(bai, &retaliation);
|
2023-03-24 17:17:17 +02:00
|
|
|
int64_t attackDamage = (attack.damage.min + attack.damage.max) / 2;
|
2022-10-17 18:47:16 +02:00
|
|
|
int64_t defenderDamageReduce = AttackPossibility::calculateDamageReduce(attacker.get(), defender.get(), attackDamage, cb);
|
|
|
|
int64_t 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(
|
2022-10-17 18:47:16 +02:00
|
|
|
"%s -> %s, normal attack, %s, dps: %lld, %lld",
|
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)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
dpsScore += defenderDamageReduce;
|
|
|
|
attackerValue[attacker->unitId()].value += defenderDamageReduce;
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
else
|
2022-10-17 18:47:16 +02:00
|
|
|
dpsScore -= defenderDamageReduce;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
defender->damage(attackDamage);
|
|
|
|
attacker->afterAttack(shooting, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(defender->alive() && defender->ableToRetaliate() && !counterAttacksBlocked && !shooting)
|
|
|
|
{
|
2023-03-24 17:17:17 +02:00
|
|
|
if(retaliation.damage.max != 0)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2023-03-24 17:17:17 +02:00
|
|
|
auto retaliationDamage = (retaliation.damage.min + retaliation.damage.max) / 2;
|
2022-10-17 18:47:16 +02:00
|
|
|
attackerDamageReduce = AttackPossibility::calculateDamageReduce(defender.get(), attacker.get(), retaliationDamage, cb);
|
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(
|
2022-10-17 18:47:16 +02:00
|
|
|
"%s -> %s, retaliation, dps: %lld, %lld",
|
2022-01-27 09:00:27 +02:00
|
|
|
defender->getDescription(),
|
|
|
|
attacker->getDescription(),
|
2022-10-17 18:47:16 +02:00
|
|
|
retaliationDamage,
|
|
|
|
attackerDamageReduce);
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(isOurAttack)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
dpsScore -= attackerDamageReduce;
|
2022-01-27 09:00:27 +02:00
|
|
|
attackerValue[attacker->unitId()].isRetalitated = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
dpsScore += attackerDamageReduce;
|
|
|
|
attackerValue[defender->unitId()].value += attackerDamageReduce;
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
attacker->damage(retaliationDamage);
|
2022-01-27 09:00:27 +02:00
|
|
|
defender->afterAttack(false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
logAi->trace("Attack has zero score d:%lld a:%lld", 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
EvaluationResult BattleExchangeEvaluator::findBestTarget(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb)
|
|
|
|
{
|
|
|
|
EvaluationResult result(targets.bestAction());
|
|
|
|
|
|
|
|
updateReachabilityMap(hb);
|
|
|
|
|
|
|
|
for(auto & ap : targets.possibleAttacks)
|
|
|
|
{
|
2022-04-11 08:12:41 +02:00
|
|
|
int64_t score = calculateExchange(ap, targets, hb);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(score > result.score)
|
|
|
|
{
|
|
|
|
result.score = score;
|
|
|
|
result.bestAttack = ap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!activeStack->waited())
|
|
|
|
{
|
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
|
|
|
|
|
|
|
hb.getForUpdate(activeStack->unitId())->waiting = true;
|
|
|
|
hb.getForUpdate(activeStack->unitId())->waitedThisTurn = true;
|
|
|
|
|
|
|
|
updateReachabilityMap(hb);
|
|
|
|
|
|
|
|
for(auto & ap : targets.possibleAttacks)
|
|
|
|
{
|
2022-04-11 08:12:41 +02:00
|
|
|
int64_t score = calculateExchange(ap, targets, hb);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(score > result.score)
|
|
|
|
{
|
|
|
|
result.score = score;
|
|
|
|
result.bestAttack = ap;
|
|
|
|
result.wait = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
MoveTarget BattleExchangeEvaluator::findMoveTowardsUnreachable(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb)
|
|
|
|
{
|
|
|
|
MoveTarget result;
|
|
|
|
BattleExchangeVariant ev;
|
|
|
|
|
|
|
|
if(targets.unreachableEnemies.empty())
|
|
|
|
return result;
|
|
|
|
|
2022-12-10 12:11:14 +02:00
|
|
|
auto speed = activeStack->Speed();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
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));
|
2022-10-17 18:47:16 +02:00
|
|
|
auto attack = AttackPossibility::evaluate(bai, hex, 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
|
|
|
|
|
|
|
|
auto score = calculateExchange(attack, targets, hb) / turnsToRich;
|
|
|
|
|
|
|
|
if(result.score < score)
|
|
|
|
{
|
|
|
|
result.score = score;
|
|
|
|
result.positions = closestStack->getAttackableHexes(activeStack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:00:27 +02:00
|
|
|
std::vector<const battle::Unit *> BattleExchangeEvaluator::getExchangeUnits(
|
2022-04-11 08:12:41 +02:00
|
|
|
const AttackPossibility & ap,
|
|
|
|
PotentialTargets & targets,
|
|
|
|
HypotheticBattle & hb)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
|
|
|
auto hexes = ap.attack.defender->getHexes();
|
|
|
|
|
|
|
|
if(!ap.attack.shooting) hexes.push_back(ap.from);
|
|
|
|
|
|
|
|
std::vector<const battle::Unit *> exchangeUnits;
|
|
|
|
std::vector<const battle::Unit *> allReachableUnits;
|
|
|
|
|
|
|
|
for(auto hex : hexes)
|
|
|
|
{
|
|
|
|
vstd::concatenate(allReachableUnits, reachabilityMap[hex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
vstd::removeDuplicates(allReachableUnits);
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
auto copy = allReachableUnits;
|
|
|
|
for(auto unit : copy)
|
|
|
|
{
|
|
|
|
for(auto adjacentUnit : getAdjacentUnits(unit))
|
|
|
|
{
|
|
|
|
auto unitWithBonuses = hb.battleGetUnitByID(adjacentUnit->unitId());
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return exchangeUnits;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int turn = 0; turn < turnOrder.size(); turn++)
|
|
|
|
{
|
|
|
|
for(auto unit : turnOrder[turn])
|
|
|
|
{
|
|
|
|
if(vstd::contains(allReachableUnits, unit))
|
|
|
|
exchangeUnits.push_back(unit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exchangeUnits;
|
|
|
|
}
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
int64_t BattleExchangeEvaluator::calculateExchange(
|
|
|
|
const AttackPossibility & ap,
|
|
|
|
PotentialTargets & targets,
|
|
|
|
HypotheticBattle & hb)
|
2022-01-27 09:00:27 +02:00
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
|
|
|
logAi->trace("Battle exchange at %lld", ap.attack.shooting ? ap.dest : ap.from);
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
std::vector<const battle::Unit *> ourStacks;
|
|
|
|
std::vector<const battle::Unit *> enemyStacks;
|
|
|
|
|
|
|
|
enemyStacks.push_back(ap.attack.defender);
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
std::vector<const battle::Unit *> exchangeUnits = getExchangeUnits(ap, targets, hb);
|
2022-01-27 09:00:27 +02:00
|
|
|
|
|
|
|
if(exchangeUnits.empty())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
HypotheticBattle exchangeBattle(env.get(), cb);
|
|
|
|
BattleExchangeVariant v;
|
|
|
|
auto melleeAttackers = ourStacks;
|
|
|
|
|
|
|
|
vstd::removeDuplicates(melleeAttackers);
|
|
|
|
vstd::erase_if(melleeAttackers, [&](const battle::Unit * u) -> bool
|
|
|
|
{
|
|
|
|
return !cb->battleCanShoot(u);
|
|
|
|
});
|
|
|
|
|
|
|
|
for(auto unit : exchangeUnits)
|
|
|
|
{
|
|
|
|
bool isOur = cb->battleMatchOwner(ap.attack.attacker, unit, true);
|
|
|
|
auto & attackerQueue = isOur ? ourStacks : enemyStacks;
|
|
|
|
|
|
|
|
if(!vstd::contains(attackerQueue, unit))
|
|
|
|
{
|
|
|
|
attackerQueue.push_back(unit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool canUseAp = true;
|
|
|
|
|
|
|
|
for(auto activeUnit : exchangeUnits)
|
|
|
|
{
|
|
|
|
bool isOur = cb->battleMatchOwner(ap.attack.attacker, activeUnit, true);
|
|
|
|
battle::Units & attackerQueue = isOur ? ourStacks : enemyStacks;
|
|
|
|
battle::Units & oppositeQueue = isOur ? enemyStacks : ourStacks;
|
|
|
|
|
|
|
|
auto attacker = exchangeBattle.getForUpdate(activeUnit->unitId());
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if(!isOur || !exchangeBattle.getForUpdate(targetUnit->unitId())->alive())
|
|
|
|
{
|
2022-04-11 08:12:41 +02:00
|
|
|
auto estimateAttack = [&](const battle::Unit * u) -> int64_t
|
|
|
|
{
|
|
|
|
auto stackWithBonuses = exchangeBattle.getForUpdate(u->unitId());
|
|
|
|
auto score = v.trackAttack(
|
|
|
|
attacker,
|
|
|
|
stackWithBonuses,
|
|
|
|
exchangeBattle.battleCanShoot(stackWithBonuses.get()),
|
|
|
|
isOur,
|
2022-10-17 18:47:16 +02:00
|
|
|
*cb,
|
2022-04-11 08:12:41 +02:00
|
|
|
true);
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
#if BATTLE_TRACE_LEVEL>=1
|
|
|
|
logAi->trace("Best target selector %s->%s score = %lld", attacker->getDescription(), u->getDescription(), score);
|
2022-04-11 08:12:41 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return score;
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!oppositeQueue.empty())
|
|
|
|
{
|
|
|
|
targetUnit = *vstd::maxElementByFun(oppositeQueue, estimateAttack);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto reachable = exchangeBattle.battleGetUnitsIf([&](const battle::Unit * u) -> bool
|
|
|
|
{
|
|
|
|
if(!u->alive() || u->unitSide() == attacker->unitSide())
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
auto defender = exchangeBattle.getForUpdate(targetUnit->unitId());
|
|
|
|
auto shooting = cb->battleCanShoot(attacker.get());
|
|
|
|
const int totalAttacks = attacker->getTotalAttacks(shooting);
|
|
|
|
|
|
|
|
if(canUseAp && activeUnit == ap.attack.attacker && targetUnit == ap.attack.defender)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
v.trackAttack(ap, exchangeBattle);
|
2022-01-27 09:00:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int i = 0; i < totalAttacks; i++)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
v.trackAttack(attacker, defender, shooting, isOur, 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
|
|
|
|
{
|
|
|
|
return !exchangeBattle.getForUpdate(u->unitId())->alive();
|
|
|
|
});
|
|
|
|
|
|
|
|
vstd::erase_if(oppositeQueue, [&](const battle::Unit * u) -> bool
|
|
|
|
{
|
|
|
|
return !exchangeBattle.getForUpdate(u->unitId())->alive();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
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
|
|
|
|
logAi->trace("Exchange score: %lld", v.getScore());
|
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));
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
int64_t 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto desiredPosition = vstd::minElementByFun(hexes, [&](BattleHex h) -> int64_t
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
dpsScore = EvaluationResult::INEFFECTIVE_SCORE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BattleExchangeEvaluator::updateReachabilityMap(HypotheticBattle & hb)
|
|
|
|
{
|
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();
|
|
|
|
|
|
|
|
hb.battleGetTurnOrder(turnOrder, std::numeric_limits<int>::max(), TURN_DEPTH);
|
2022-01-27 09:00:27 +02:00
|
|
|
reachabilityMap.clear();
|
|
|
|
|
|
|
|
for(int turn = 0; turn < turnOrder.size(); turn++)
|
|
|
|
{
|
|
|
|
auto & turnQueue = turnOrder[turn];
|
|
|
|
HypotheticBattle turnBattle(env.get(), cb);
|
|
|
|
|
|
|
|
for(const battle::Unit * unit : turnQueue)
|
|
|
|
{
|
2022-04-11 08:12:41 +02:00
|
|
|
if(turnBattle.battleCanShoot(unit))
|
|
|
|
{
|
|
|
|
for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
|
|
|
|
{
|
|
|
|
reachabilityMap[hex].push_back(unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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 reachable = unitReachability.distances[hex] <= unit->Speed(turn);
|
|
|
|
|
|
|
|
if(!reachable && unitReachability.accessibility[hex] == EAccessibility::ALIVE_STACK)
|
|
|
|
{
|
|
|
|
const battle::Unit * hexStack = cb->battleGetUnitByPos(hex);
|
|
|
|
|
|
|
|
if(hexStack && cb->battleMatchOwner(unit, hexStack, false))
|
|
|
|
{
|
|
|
|
for(BattleHex neighbor : hex.neighbouringTiles())
|
|
|
|
{
|
|
|
|
reachable = unitReachability.distances[neighbor] <= unit->Speed(turn);
|
|
|
|
|
|
|
|
if(reachable) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(reachable)
|
|
|
|
{
|
|
|
|
reachabilityMap[hex].push_back(unit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
bool reachable = unitReachability.distances[hex] <= unit->Speed(turn);
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
reachable = unitReachability.distances[neighbor] <= unit->Speed(turn);
|
|
|
|
|
|
|
|
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
|
|
|
}
|