Some changes to make the battle AI smarter
- the AI will now consider attacking multiple units - the preferred strategy now is to minimize collateral damage rather than to maximize damage to enemy units alone - attacks that block enemy shooters will be prioritized over other attacks in cases when shooters have weaker melee attacks
This commit is contained in:
1 parent
604246bc7a
commit
be10694b73
11 files changed
+392
-172
No files matched your search
@@ -1,97 +1,188 @@
|
||||
/*
|
||||
* 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"
|
||||
|
||||
AttackPossibility::AttackPossibility(BattleHex tile_, const BattleAttackInfo & attack_)
|
||||
: tile(tile_),
|
||||
attack(attack_)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
int64_t AttackPossibility::damageDiff() const
|
||||
{
|
||||
//TODO: use target priority from HypotheticBattle
|
||||
const auto dealtDmgValue = damageDealt;
|
||||
const auto receivedDmgValue = damageReceived;
|
||||
|
||||
int64_t diff = 0;
|
||||
|
||||
//friendly fire or not
|
||||
if(attack.attacker->unitSide() == attack.defender->unitSide())
|
||||
diff = -dealtDmgValue - receivedDmgValue;
|
||||
else
|
||||
diff = dealtDmgValue - receivedDmgValue;
|
||||
|
||||
//mind control
|
||||
auto actualSide = getCbc()->playerToSide(getCbc()->battleGetOwner(attack.attacker));
|
||||
if(actualSide && actualSide.get() != attack.attacker->unitSide())
|
||||
diff = -diff;
|
||||
return diff;
|
||||
}
|
||||
|
||||
int64_t AttackPossibility::attackValue() const
|
||||
{
|
||||
return damageDiff() + tacticImpact;
|
||||
}
|
||||
|
||||
AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex)
|
||||
{
|
||||
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
|
||||
static const auto selectorBlocksRetaliation = Selector::type(Bonus::BLOCKS_RETALIATION);
|
||||
|
||||
const bool counterAttacksBlocked = attackInfo.attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
|
||||
|
||||
AttackPossibility ap(hex, attackInfo);
|
||||
|
||||
ap.attackerState = attackInfo.attacker->acquireState();
|
||||
|
||||
const int totalAttacks = ap.attackerState->getTotalAttacks(attackInfo.shooting);
|
||||
|
||||
if(!attackInfo.shooting)
|
||||
ap.attackerState->setPosition(hex);
|
||||
|
||||
auto defenderState = attackInfo.defender->acquireState();
|
||||
ap.affectedUnits.push_back(defenderState);
|
||||
|
||||
for(int i = 0; i < totalAttacks; i++)
|
||||
/*
|
||||
* 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"
|
||||
#include "../../lib/CStack.h"//todo: remove
|
||||
|
||||
AttackPossibility::AttackPossibility(BattleHex from, BattleHex dest, const BattleAttackInfo & attack)
|
||||
: from(from), dest(dest), attack(attack)
|
||||
{
|
||||
}
|
||||
|
||||
int64_t AttackPossibility::damageDiff() const
|
||||
{
|
||||
return damageDealt - damageReceived - collateralDamage + shootersBlockedDmg;
|
||||
}
|
||||
|
||||
int64_t AttackPossibility::attackValue() const
|
||||
{
|
||||
return damageDiff();
|
||||
}
|
||||
|
||||
int64_t AttackPossibility::evaluateBlockedShootersDmg(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state)
|
||||
{
|
||||
int64_t res = 0;
|
||||
|
||||
if(attackInfo.shooting)
|
||||
return 0;
|
||||
|
||||
auto attacker = attackInfo.attacker;
|
||||
auto hexes = attacker->getSurroundingHexes(hex);
|
||||
for(BattleHex tile : hexes)
|
||||
{
|
||||
auto st = state->battleGetUnitByPos(tile, true);
|
||||
if(!st || !state->battleMatchOwner(st, attacker))
|
||||
continue;
|
||||
if(!state->battleCanShoot(st))
|
||||
continue;
|
||||
|
||||
BattleAttackInfo rangeAttackInfo(st, attacker, true);
|
||||
rangeAttackInfo.defenderPos = hex;
|
||||
|
||||
BattleAttackInfo meleeAttackInfo(st, attacker, false);
|
||||
meleeAttackInfo.defenderPos = hex;
|
||||
|
||||
auto rangeDmg = getCbc()->battleEstimateDamage(rangeAttackInfo);
|
||||
auto meleeDmg = getCbc()->battleEstimateDamage(meleeAttackInfo);
|
||||
|
||||
int64_t gain = (rangeDmg.first + rangeDmg.second - meleeDmg.first - meleeDmg.second) / 2 + 1;
|
||||
res += gain;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
AttackPossibility AttackPossibility::evaluate(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state)
|
||||
{
|
||||
auto attacker = attackInfo.attacker;
|
||||
auto defender = attackInfo.defender;
|
||||
const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
|
||||
static const auto selectorBlocksRetaliation = Selector::type(Bonus::BLOCKS_RETALIATION);
|
||||
const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
|
||||
const bool mindControlled = [&](const battle::Unit *attacker) -> bool
|
||||
{
|
||||
TDmgRange retaliation(0,0);
|
||||
auto attackDmg = getCbc()->battleEstimateDamage(ap.attack, &retaliation);
|
||||
|
||||
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());
|
||||
|
||||
ap.damageDealt += (attackDmg.first + attackDmg.second) / 2;
|
||||
|
||||
ap.attackerState->afterAttack(attackInfo.shooting, false);
|
||||
|
||||
//FIXME: use ranged retaliation
|
||||
if(!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
|
||||
{
|
||||
ap.damageReceived += (retaliation.first + retaliation.second) / 2;
|
||||
defenderState->afterAttack(attackInfo.shooting, true);
|
||||
}
|
||||
|
||||
ap.attackerState->damage(ap.damageReceived);
|
||||
defenderState->damage(ap.damageDealt);
|
||||
|
||||
if(!ap.attackerState->alive() || !defenderState->alive())
|
||||
break;
|
||||
}
|
||||
|
||||
//TODO other damage related to attack (eg. fire shield and other abilities)
|
||||
|
||||
return ap;
|
||||
}
|
||||
auto actualSide = getCbc()->playerToSide(getCbc()->battleGetOwner(attacker));
|
||||
if (actualSide && actualSide.get() != attacker->unitSide())
|
||||
return true;
|
||||
return false;
|
||||
} (attacker);
|
||||
|
||||
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) {
|
||||
if(defHex == hex) {
|
||||
// should be impossible but check anyway
|
||||
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)
|
||||
units = state->getAttackedBattleUnits(attacker, defHex, true, BattleHex::INVALID);
|
||||
else
|
||||
units = state->getAttackedBattleUnits(attacker, defHex, false, hex);
|
||||
|
||||
// ensure the defender is also affected
|
||||
bool addDefender = true;
|
||||
for(auto unit : units) {
|
||||
if (unit->unitId() == defender->unitId()) {
|
||||
addDefender = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(addDefender) {
|
||||
units.push_back(defender);
|
||||
}
|
||||
|
||||
for(auto u : units) {
|
||||
if(!ap.attackerState->alive()) {
|
||||
break;
|
||||
}
|
||||
|
||||
assert(u->unitId() != attacker->unitId());
|
||||
|
||||
auto defenderState = u->acquireState();
|
||||
ap.affectedUnits.push_back(defenderState);
|
||||
|
||||
for(int i = 0; i < totalAttacks; i++) {
|
||||
si64 damageDealt, damageReceived;
|
||||
|
||||
TDmgRange retaliation(0, 0);
|
||||
auto attackDmg = getCbc()->battleEstimateDamage(ap.attack, &retaliation);
|
||||
|
||||
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());
|
||||
|
||||
damageDealt = (attackDmg.first + attackDmg.second) / 2;
|
||||
ap.attackerState->afterAttack(attackInfo.shooting, false);
|
||||
|
||||
//FIXME: use ranged retaliation
|
||||
damageReceived = 0;
|
||||
if (!attackInfo.shooting && defenderState->ableToRetaliate() && !counterAttacksBlocked)
|
||||
{
|
||||
damageReceived = (retaliation.first + retaliation.second) / 2;
|
||||
defenderState->afterAttack(attackInfo.shooting, true);
|
||||
}
|
||||
|
||||
bool isEnemy = state->battleMatchOwner(attacker, u) && !mindControlled;
|
||||
if(isEnemy)
|
||||
ap.damageDealt += damageDealt;
|
||||
else // friendly fire
|
||||
ap.collateralDamage += damageDealt;
|
||||
|
||||
if(u->unitId() == defender->unitId() ||
|
||||
(!attackInfo.shooting && CStack::isMeleeAttackPossible(u, attacker, hex))) { //FIXME: handle RANGED_RETALIATION ?
|
||||
ap.damageReceived += damageReceived;
|
||||
}
|
||||
|
||||
ap.attackerState->damage(damageReceived);
|
||||
defenderState->damage(damageDealt);
|
||||
|
||||
if (!ap.attackerState->alive() || !defenderState->alive())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!bestAp.dest.isValid() || ap.attackValue() > bestAp.attackValue()) {
|
||||
bestAp = ap;
|
||||
}
|
||||
}
|
||||
|
||||
// check how much damage we gain from blocking enemy shooters on this hex
|
||||
bestAp.shootersBlockedDmg = evaluateBlockedShootersDmg(attackInfo, hex, state);
|
||||
|
||||
logAi->debug("BattleAI best AP: %s -> %s at %d from %d, affects %d units: %d %d %d %s",
|
||||
VLC->creh->creatures.at(attackInfo.attacker->acquireState()->creatureId())->identifier.c_str(),
|
||||
VLC->creh->creatures.at(attackInfo.defender->acquireState()->creatureId())->identifier.c_str(),
|
||||
(int)bestAp.dest, (int)bestAp.from, (int)bestAp.affectedUnits.size(),
|
||||
(int)bestAp.damageDealt, (int)bestAp.damageReceived, (int)bestAp.collateralDamage, (int)bestAp.shootersBlockedDmg);
|
||||
|
||||
//TODO other damage related to attack (eg. fire shield and other abilities)
|
||||
return bestAp;
|
||||
}
|
||||
@@ -16,7 +16,8 @@
|
||||
class AttackPossibility
|
||||
{
|
||||
public:
|
||||
BattleHex tile; //tile from which we attack
|
||||
BattleHex from; //tile from which we attack
|
||||
BattleHex dest; //tile which we attack
|
||||
BattleAttackInfo attack;
|
||||
|
||||
std::shared_ptr<battle::CUnitState> attackerState;
|
||||
@@ -25,12 +26,16 @@ public:
|
||||
|
||||
int64_t damageDealt = 0;
|
||||
int64_t damageReceived = 0; //usually by counter-attack
|
||||
int64_t tacticImpact = 0;
|
||||
int64_t collateralDamage = 0; // friendly fire (usually by two-hex attacks)
|
||||
int64_t shootersBlockedDmg = 0;
|
||||
|
||||
AttackPossibility(BattleHex tile_, const BattleAttackInfo & attack_);
|
||||
AttackPossibility(BattleHex from, BattleHex dest, const BattleAttackInfo & attack_);
|
||||
|
||||
int64_t damageDiff() const;
|
||||
int64_t attackValue() const;
|
||||
|
||||
static AttackPossibility evaluate(const BattleAttackInfo & attackInfo, BattleHex hex);
|
||||
static AttackPossibility evaluate(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state);
|
||||
|
||||
private:
|
||||
static int64_t evaluateBlockedShootersDmg(const BattleAttackInfo & attackInfo, BattleHex hex, const HypotheticBattle * state);
|
||||
};
|
||||
@@ -154,7 +154,8 @@ BattleAction CBattleAI::activeStack( const CStack * stack )
|
||||
HypotheticBattle hb(getCbc());
|
||||
|
||||
PotentialTargets targets(stack, &hb);
|
||||
if(targets.possibleAttacks.size())
|
||||
|
||||
if(!targets.possibleAttacks.empty())
|
||||
{
|
||||
AttackPossibility bestAttack = targets.bestAction();
|
||||
|
||||
@@ -163,8 +164,18 @@ BattleAction CBattleAI::activeStack( const CStack * stack )
|
||||
return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id);
|
||||
else if(bestAttack.attack.shooting)
|
||||
return BattleAction::makeShotAttack(stack, bestAttack.attack.defender);
|
||||
else
|
||||
return BattleAction::makeMeleeAttack(stack, bestAttack.attack.defender->getPosition(), bestAttack.tile);
|
||||
else {
|
||||
auto &target = bestAttack;
|
||||
logAi->debug("BattleAI: %s -> %s %d from, %d curpos %d dist %d speed %d: %d %d %d",
|
||||
VLC->creh->creatures.at(target.attackerState->creatureId())->identifier.c_str(),
|
||||
VLC->creh->creatures.at(target.affectedUnits[0]->creatureId())->identifier.c_str(),
|
||||
(int)target.affectedUnits.size(), (int)target.from, (int)bestAttack.attack.attacker->getPosition().hex,
|
||||
(int)bestAttack.attack.chargedFields, (int)bestAttack.attack.attacker->Speed(0, true),
|
||||
(int)target.damageDealt, (int)target.damageReceived, (int)target.attackValue()
|
||||
);
|
||||
|
||||
return BattleAction::makeMeleeAttack(stack, bestAttack.attack.defender->getPosition(), bestAttack.from);
|
||||
}
|
||||
}
|
||||
else if(bestSpellcast.is_initialized())
|
||||
{
|
||||
|
||||
@@ -53,7 +53,7 @@ PotentialTargets::PotentialTargets(const battle::Unit * attacker, const Hypothet
|
||||
if(hex.isValid() && !shooting)
|
||||
bai.chargedFields = reachability.distances[hex];
|
||||
|
||||
return AttackPossibility::evaluate(bai, hex);
|
||||
return AttackPossibility::evaluate(bai, hex, state);
|
||||
};
|
||||
|
||||
if(forceTarget)
|
||||
@@ -69,21 +69,45 @@ PotentialTargets::PotentialTargets(const battle::Unit * attacker, const Hypothet
|
||||
}
|
||||
else
|
||||
{
|
||||
for(BattleHex hex : avHexes)
|
||||
if(CStack::isMeleeAttackPossible(attackerInfo, defender, hex))
|
||||
possibleAttacks.push_back(GenerateAttackInfo(false, hex));
|
||||
for(BattleHex hex : avHexes) {
|
||||
if(!CStack::isMeleeAttackPossible(attackerInfo, defender, hex))
|
||||
continue;
|
||||
|
||||
auto bai = GenerateAttackInfo(false, hex);
|
||||
if(!bai.affectedUnits.empty())
|
||||
possibleAttacks.push_back(bai);
|
||||
}
|
||||
|
||||
if(!vstd::contains_if(possibleAttacks, [=](const AttackPossibility & pa) { return pa.attack.defender->unitId() == defender->unitId(); }))
|
||||
unreachableEnemies.push_back(defender);
|
||||
}
|
||||
}
|
||||
|
||||
boost::sort(possibleAttacks, [](const AttackPossibility & lhs, const AttackPossibility & rhs) -> bool
|
||||
{
|
||||
if(lhs.collateralDamage < rhs.collateralDamage)
|
||||
return false;
|
||||
if(lhs.collateralDamage > rhs.collateralDamage)
|
||||
return true;
|
||||
return (lhs.damageDealt + lhs.shootersBlockedDmg + lhs.damageReceived > rhs.damageDealt + rhs.shootersBlockedDmg + rhs.damageReceived);
|
||||
});
|
||||
|
||||
if (!possibleAttacks.empty())
|
||||
{
|
||||
auto &bestAp = possibleAttacks[0];
|
||||
|
||||
logGlobal->info("Battle AI best: %s -> %s at %d from %d, affects %d units: %d %d %d %s",
|
||||
VLC->creh->creatures.at(bestAp.attackerState->creatureId())->identifier.c_str(),
|
||||
VLC->creh->creatures.at(state->battleGetUnitByPos(bestAp.dest)->creatureId())->identifier.c_str(),
|
||||
(int)bestAp.dest, (int)bestAp.from, (int)bestAp.affectedUnits.size(),
|
||||
(int)bestAp.damageDealt, (int)bestAp.damageReceived, (int)bestAp.collateralDamage, (int)bestAp.shootersBlockedDmg);
|
||||
}
|
||||
}
|
||||
|
||||
int PotentialTargets::bestActionValue() const
|
||||
int64_t PotentialTargets::bestActionValue() const
|
||||
{
|
||||
if(possibleAttacks.empty())
|
||||
return 0;
|
||||
|
||||
return bestAction().attackValue();
|
||||
}
|
||||
|
||||
@@ -91,6 +115,6 @@ AttackPossibility PotentialTargets::bestAction() const
|
||||
{
|
||||
if(possibleAttacks.empty())
|
||||
throw std::runtime_error("No best action, since we don't have any actions");
|
||||
|
||||
return *vstd::maxElementByFun(possibleAttacks, [](const AttackPossibility &ap) { return ap.attackValue(); } );
|
||||
return possibleAttacks[0];
|
||||
//return *vstd::maxElementByFun(possibleAttacks, [](const AttackPossibility &ap) { return ap.attackValue(); } );
|
||||
}
|
||||
@@ -20,5 +20,5 @@ public:
|
||||
PotentialTargets(const battle::Unit * attacker, const HypotheticBattle * state);
|
||||
|
||||
AttackPossibility bestAction() const;
|
||||
int bestActionValue() const;
|
||||
int64_t bestActionValue() const;
|
||||
};
|
||||
Reference in new issue
Block a user