2020-05-09 12:51:00 +02:00
|
|
|
/*
|
|
|
|
* AttackPossibility.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 "AttackPossibility.h"
|
2020-05-09 13:09:32 +02:00
|
|
|
#include "../../lib/CStack.h" // TODO: remove
|
|
|
|
// Eventually only IBattleInfoCallback and battle::Unit should be used,
|
|
|
|
// CUnitState should be private and CStack should be removed completely
|
2020-05-09 12:51:00 +02:00
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
uint64_t averageDmg(const TDmgRange & range)
|
|
|
|
{
|
|
|
|
return (range.first + range.second) / 2;
|
|
|
|
}
|
|
|
|
|
2020-05-09 12:51:00 +02:00
|
|
|
AttackPossibility::AttackPossibility(BattleHex from, BattleHex dest, const BattleAttackInfo & attack)
|
|
|
|
: from(from), dest(dest), attack(attack)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t AttackPossibility::damageDiff() const
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
return defenderDamageReduce - attackerDamageReduce - collateralDamageReduce + shootersBlockedDmg;
|
2020-05-09 12:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t AttackPossibility::attackValue() const
|
|
|
|
{
|
|
|
|
return damageDiff();
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
/// <summary>
|
|
|
|
/// How enemy damage will be reduced by this attack
|
|
|
|
/// Half bounty for kill, half for making damage equal to enemy health
|
|
|
|
/// Bounty - the killed creature average damage calculated against attacker
|
|
|
|
/// </summary>
|
|
|
|
int64_t AttackPossibility::calculateDamageReduce(
|
2022-04-11 08:12:41 +02:00
|
|
|
const battle::Unit * attacker,
|
|
|
|
const battle::Unit * defender,
|
|
|
|
uint64_t damageDealt,
|
2022-10-17 18:47:16 +02:00
|
|
|
const CBattleInfoCallback & cb)
|
2022-04-11 08:12:41 +02:00
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
const float HEALTH_BOUNTY = 0.5;
|
|
|
|
const float KILL_BOUNTY = 1.0 - HEALTH_BOUNTY;
|
|
|
|
|
2022-04-11 08:12:41 +02:00
|
|
|
vstd::amin(damageDealt, defender->getAvailableHealth());
|
|
|
|
|
2023-01-14 19:03:18 +02:00
|
|
|
// FIXME: provide distance info for Jousting bonus
|
|
|
|
auto enemyDamageBeforeAttack = cb.battleEstimateDamage(defender, attacker, 0);
|
2022-04-11 08:12:41 +02:00
|
|
|
auto enemiesKilled = damageDealt / defender->MaxHealth() + (damageDealt % defender->MaxHealth() >= defender->getFirstHPleft() ? 1 : 0);
|
2022-10-17 18:47:16 +02:00
|
|
|
auto enemyDamage = averageDmg(enemyDamageBeforeAttack);
|
|
|
|
auto damagePerEnemy = enemyDamage / (double)defender->getCount();
|
2022-04-11 08:12:41 +02:00
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
return (int64_t)(damagePerEnemy * (enemiesKilled * KILL_BOUNTY + damageDealt * HEALTH_BOUNTY / (double)defender->MaxHealth()));
|
2022-04-11 08:12:41 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
int64_t AttackPossibility::evaluateBlockedShootersDmg(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle & state)
|
2020-05-09 12:51:00 +02:00
|
|
|
{
|
|
|
|
int64_t res = 0;
|
|
|
|
|
|
|
|
if(attackInfo.shooting)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
auto attacker = attackInfo.attacker;
|
|
|
|
auto hexes = attacker->getSurroundingHexes(hex);
|
|
|
|
for(BattleHex tile : hexes)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
auto st = state.battleGetUnitByPos(tile, true);
|
|
|
|
if(!st || !state.battleMatchOwner(st, attacker))
|
2020-05-09 12:51:00 +02:00
|
|
|
continue;
|
2022-10-17 18:47:16 +02:00
|
|
|
if(!state.battleCanShoot(st))
|
2020-05-09 12:51:00 +02:00
|
|
|
continue;
|
|
|
|
|
2023-01-14 19:03:18 +02:00
|
|
|
// FIXME: provide distance info for Jousting bonus
|
|
|
|
BattleAttackInfo rangeAttackInfo(st, attacker, 0, true);
|
2020-05-09 12:51:00 +02:00
|
|
|
rangeAttackInfo.defenderPos = hex;
|
|
|
|
|
2023-01-14 19:03:18 +02:00
|
|
|
BattleAttackInfo meleeAttackInfo(st, attacker, 0, false);
|
2020-05-09 12:51:00 +02:00
|
|
|
meleeAttackInfo.defenderPos = hex;
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
auto rangeDmg = state.battleEstimateDamage(rangeAttackInfo);
|
|
|
|
auto meleeDmg = state.battleEstimateDamage(meleeAttackInfo);
|
2020-05-09 12:51:00 +02:00
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
int64_t gain = averageDmg(rangeDmg) - averageDmg(meleeDmg) + 1;
|
2020-05-09 12:51:00 +02:00
|
|
|
res += gain;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle & state)
|
2020-05-09 12:51:00 +02:00
|
|
|
{
|
|
|
|
auto attacker = attackInfo.attacker;
|
|
|
|
auto defender = attackInfo.defender;
|
|
|
|
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
|
2020-11-11 21:43:40 +02:00
|
|
|
static const auto selectorBlocksRetaliation = Selector::type()(Bonus::BLOCKS_RETALIATION);
|
2022-10-17 18:47:16 +02:00
|
|
|
const auto attackerSide = state.playerToSide(state.battleGetOwner(attacker));
|
2020-05-09 12:51:00 +02:00
|
|
|
const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
|
|
|
|
|
|
|
|
AttackPossibility bestAp(hex, BattleHex::INVALID, attackInfo);
|
|
|
|
|
|
|
|
std::vector<BattleHex> defenderHex;
|
|
|
|
if(attackInfo.shooting)
|
|
|
|
defenderHex = defender->getHexes();
|
|
|
|
else
|
|
|
|
defenderHex = CStack::meleeAttackHexes(attacker, defender, hex);
|
|
|
|
|
|
|
|
for(BattleHex defHex : defenderHex)
|
|
|
|
{
|
2020-05-16 15:14:58 +02:00
|
|
|
if(defHex == hex) // should be impossible but check anyway
|
2020-05-09 12:51:00 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
AttackPossibility ap(hex, defHex, attackInfo);
|
|
|
|
ap.attackerState = attacker->acquireState();
|
|
|
|
ap.shootersBlockedDmg = bestAp.shootersBlockedDmg;
|
|
|
|
|
|
|
|
const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting);
|
|
|
|
|
|
|
|
if (!attackInfo.shooting)
|
|
|
|
ap.attackerState->setPosition(hex);
|
|
|
|
|
|
|
|
std::vector<const battle::Unit*> units;
|
|
|
|
|
|
|
|
if (attackInfo.shooting)
|
2022-10-17 18:47:16 +02:00
|
|
|
units = state.getAttackedBattleUnits(attacker, defHex, true, BattleHex::INVALID);
|
2020-05-09 12:51:00 +02:00
|
|
|
else
|
2022-10-17 18:47:16 +02:00
|
|
|
units = state.getAttackedBattleUnits(attacker, defHex, false, hex);
|
2020-05-09 12:51:00 +02:00
|
|
|
|
|
|
|
// ensure the defender is also affected
|
|
|
|
bool addDefender = true;
|
2020-05-16 15:14:58 +02:00
|
|
|
for(auto unit : units)
|
|
|
|
{
|
|
|
|
if (unit->unitId() == defender->unitId())
|
|
|
|
{
|
2020-05-09 12:51:00 +02:00
|
|
|
addDefender = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-16 15:14:58 +02:00
|
|
|
|
|
|
|
if(addDefender)
|
2020-05-09 12:51:00 +02:00
|
|
|
units.push_back(defender);
|
|
|
|
|
2020-05-16 15:14:58 +02:00
|
|
|
for(auto u : units)
|
|
|
|
{
|
|
|
|
if(!ap.attackerState->alive())
|
2020-05-09 12:51:00 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
auto defenderState = u->acquireState();
|
|
|
|
ap.affectedUnits.push_back(defenderState);
|
|
|
|
|
2020-05-17 10:21:49 +02:00
|
|
|
for(int i = 0; i < totalAttacks; i++)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
int64_t damageDealt, damageReceived, defenderDamageReduce, attackerDamageReduce;
|
2020-05-09 12:51:00 +02:00
|
|
|
|
|
|
|
TDmgRange retaliation(0, 0);
|
2022-10-17 18:47:16 +02:00
|
|
|
auto attackDmg = state.battleEstimateDamage(ap.attack, &retaliation);
|
2020-05-09 12:51:00 +02:00
|
|
|
|
|
|
|
vstd::amin(attackDmg.first, defenderState->getAvailableHealth());
|
|
|
|
vstd::amin(attackDmg.second, defenderState->getAvailableHealth());
|
|
|
|
|
|
|
|
vstd::amin(retaliation.first, ap.attackerState->getAvailableHealth());
|
|
|
|
vstd::amin(retaliation.second, ap.attackerState->getAvailableHealth());
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
damageDealt = averageDmg(attackDmg);
|
|
|
|
defenderDamageReduce = calculateDamageReduce(attacker, defender, damageDealt, state);
|
2020-05-09 12:51:00 +02:00
|
|
|
ap.attackerState->afterAttack(attackInfo.shooting, false);
|
|
|
|
|
|
|
|
//FIXME: use ranged retaliation
|
|
|
|
damageReceived = 0;
|
2022-10-17 18:47:16 +02:00
|
|
|
attackerDamageReduce = 0;
|
2022-01-27 09:00:27 +02:00
|
|
|
|
2020-05-09 12:51:00 +02:00
|
|
|
if (!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
|
|
|
|
{
|
2022-10-17 18:47:16 +02:00
|
|
|
damageReceived = averageDmg(retaliation);
|
|
|
|
attackerDamageReduce = calculateDamageReduce(defender, attacker, damageReceived, state);
|
2020-05-09 12:51:00 +02:00
|
|
|
defenderState->afterAttack(attackInfo.shooting, true);
|
|
|
|
}
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
bool isEnemy = state.battleMatchOwner(attacker, u);
|
2020-05-23 12:38:24 +02:00
|
|
|
|
|
|
|
// this includes enemy units as well as attacker units under enemy's mind control
|
2020-05-09 12:51:00 +02:00
|
|
|
if(isEnemy)
|
2022-10-17 18:47:16 +02:00
|
|
|
ap.defenderDamageReduce += defenderDamageReduce;
|
2020-05-23 12:38:24 +02:00
|
|
|
|
|
|
|
// damaging attacker's units (even those under enemy's mind control) is considered friendly fire
|
|
|
|
if(attackerSide == u->unitSide())
|
2022-10-17 18:47:16 +02:00
|
|
|
ap.collateralDamageReduce += defenderDamageReduce;
|
2020-05-09 12:51:00 +02:00
|
|
|
|
|
|
|
if(u->unitId() == defender->unitId() ||
|
|
|
|
(!attackInfo.shooting && CStack::isMeleeAttackPossible(u, attacker, hex)))
|
|
|
|
{
|
|
|
|
//FIXME: handle RANGED_RETALIATION ?
|
2022-10-17 18:47:16 +02:00
|
|
|
ap.attackerDamageReduce += attackerDamageReduce;
|
2020-05-09 12:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ap.attackerState->damage(damageReceived);
|
|
|
|
defenderState->damage(damageDealt);
|
|
|
|
|
|
|
|
if (!ap.attackerState->alive() || !defenderState->alive())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 10:21:49 +02:00
|
|
|
if(!bestAp.dest.isValid() || ap.attackValue() > bestAp.attackValue())
|
2020-05-09 12:51:00 +02:00
|
|
|
bestAp = ap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check how much damage we gain from blocking enemy shooters on this hex
|
|
|
|
bestAp.shootersBlockedDmg = evaluateBlockedShootersDmg(attackInfo, hex, state);
|
|
|
|
|
2022-10-17 18:47:16 +02:00
|
|
|
logAi->debug("BattleAI best AP: %s -> %s at %d from %d, affects %d units: d:%lld a:%lld c:%lld s:%lld",
|
2023-01-02 18:00:51 +02:00
|
|
|
attackInfo.attacker->unitType()->getJsonKey(),
|
|
|
|
attackInfo.defender->unitType()->getJsonKey(),
|
2020-05-09 12:51:00 +02:00
|
|
|
(int)bestAp.dest, (int)bestAp.from, (int)bestAp.affectedUnits.size(),
|
2022-10-17 18:47:16 +02:00
|
|
|
bestAp.defenderDamageReduce, bestAp.attackerDamageReduce, bestAp.collateralDamageReduce, bestAp.shootersBlockedDmg);
|
2020-05-09 12:51:00 +02:00
|
|
|
|
|
|
|
//TODO other damage related to attack (eg. fire shield and other abilities)
|
|
|
|
return bestAp;
|
|
|
|
}
|