1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-10-31 00:07:39 +02:00

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:
Victor Luchits
2019-06-28 21:05:25 +03:00
parent 604246bc7a
commit be10694b73
11 changed files with 392 additions and 172 deletions

View File

@@ -252,22 +252,53 @@ void CStack::prepareAttacked(BattleStackAttacked & bsa, vstd::RNG & rand, std::s
bsa.newState.operation = UnitChanges::EOperation::RESET_STATE;
}
bool CStack::isMeleeAttackPossible(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos, BattleHex defenderPos)
std::vector<BattleHex> CStack::meleeAttackHexes(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos, BattleHex defenderPos)
{
if(!attackerPos.isValid())
int mask = 0;
std::vector<BattleHex> res;
if (!attackerPos.isValid())
attackerPos = attacker->getPosition();
if(!defenderPos.isValid())
if (!defenderPos.isValid())
defenderPos = defender->getPosition();
return
(BattleHex::mutualPosition(attackerPos, defenderPos) >= 0)//front <=> front
|| (attacker->doubleWide()//back <=> front
&& BattleHex::mutualPosition(attackerPos + (attacker->unitSide() == BattleSide::ATTACKER ? -1 : 1), defenderPos) >= 0)
|| (defender->doubleWide()//front <=> back
&& BattleHex::mutualPosition(attackerPos, defenderPos + (defender->unitSide() == BattleSide::ATTACKER ? -1 : 1)) >= 0)
|| (defender->doubleWide() && attacker->doubleWide()//back <=> back
&& BattleHex::mutualPosition(attackerPos + (attacker->unitSide() == BattleSide::ATTACKER ? -1 : 1), defenderPos + (defender->unitSide() == BattleSide::ATTACKER ? -1 : 1)) >= 0);
BattleHex otherAttackerPos = attackerPos + (attacker->unitSide() == BattleSide::ATTACKER ? -1 : 1);
BattleHex otherDefenderPos = defenderPos + (defender->unitSide() == BattleSide::ATTACKER ? -1 : 1);
if(BattleHex::mutualPosition(attackerPos, defenderPos) >= 0) { //front <=> front
if((mask & 1) == 0) {
mask |= 1;
res.push_back(defenderPos);
}
}
if (attacker->doubleWide() //back <=> front
&& BattleHex::mutualPosition(otherAttackerPos, defenderPos) >= 0) {
if((mask & 1) == 0) {
mask |= 1;
res.push_back(defenderPos);
}
}
if (defender->doubleWide()//front <=> back
&& BattleHex::mutualPosition(attackerPos, otherDefenderPos) >= 0) {
if((mask & 2) == 0) {
mask |= 2;
res.push_back(otherDefenderPos);
}
}
if (defender->doubleWide() && attacker->doubleWide()//back <=> back
&& BattleHex::mutualPosition(otherAttackerPos, otherDefenderPos) >= 0) {
if((mask & 2) == 0) {
mask |= 2;
res.push_back(otherDefenderPos);
}
}
return res;
}
bool CStack::isMeleeAttackPossible(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos, BattleHex defenderPos)
{
return !meleeAttackHexes(attacker, defender, attackerPos, defenderPos).empty();
}
std::string CStack::getName() const

View File

@@ -55,6 +55,7 @@ public:
std::vector<si32> activeSpells() const; //returns vector of active spell IDs sorted by time of cast
const CGHeroInstance * getMyHero() const; //if stack belongs to hero (directly or was by him summoned) returns hero, nullptr otherwise
static std::vector<BattleHex> meleeAttackHexes(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID);
static bool isMeleeAttackPossible(const battle::Unit * attacker, const battle::Unit * defender, BattleHex attackerPos = BattleHex::INVALID, BattleHex defenderPos = BattleHex::INVALID);
BattleHex::EDir destShiftDir() const;

View File

@@ -19,6 +19,8 @@ BattleAttackInfo::BattleAttackInfo(const battle::Unit * Attacker, const battle::
chargedFields = 0;
additiveBonus = 0.0;
multBonus = 1.0;
attackerPos = BattleHex::INVALID;
defenderPos = BattleHex::INVALID;
}
BattleAttackInfo BattleAttackInfo::reverse() const
@@ -26,6 +28,7 @@ BattleAttackInfo BattleAttackInfo::reverse() const
BattleAttackInfo ret = *this;
std::swap(ret.attacker, ret.defender);
std::swap(ret.defenderPos, ret.attackerPos);
ret.shooting = false;
ret.chargedFields = 0;

View File

@@ -15,11 +15,16 @@ namespace battle
class CUnitState;
}
#include "BattleHex.h"
struct DLL_LINKAGE BattleAttackInfo
{
const battle::Unit * attacker;
const battle::Unit * defender;
BattleHex attackerPos;
BattleHex defenderPos;
bool shooting;
int chargedFields;

View File

@@ -667,37 +667,45 @@ bool CBattleInfoCallback::battleCanAttack(const CStack * stack, const CStack * t
return target->alive();
}
bool CBattleInfoCallback::battleCanShoot(const battle::Unit * attacker, BattleHex dest) const
bool CBattleInfoCallback::battleCanShoot(const battle::Unit * attacker) const
{
RETURN_IF_NOT_BATTLE(false);
if(battleTacticDist()) //no shooting during tactics
if (battleTacticDist()) //no shooting during tactics
return false;
const battle::Unit * defender = battleGetUnitByPos(dest);
if(!attacker || !defender)
if (!attacker)
return false;
if (attacker->creatureIndex() == CreatureID::CATAPULT) //catapult cannot attack creatures
return false;
//forgetfulness
TBonusListPtr forgetfulList = attacker->getBonuses(Selector::type(Bonus::FORGETFULL));
if(!forgetfulList->empty())
if (!forgetfulList->empty())
{
int forgetful = forgetfulList->valOfBonuses(Selector::type(Bonus::FORGETFULL));
//advanced+ level
if(forgetful > 1)
if (forgetful > 1)
return false;
}
if(attacker->creatureIndex() == CreatureID::CATAPULT && defender) //catapult cannot attack creatures
return attacker->canShoot() && (!battleIsUnitBlocked(attacker)
|| attacker->hasBonusOfType(Bonus::FREE_SHOOTING));
}
bool CBattleInfoCallback::battleCanShoot(const battle::Unit * attacker, BattleHex dest) const
{
RETURN_IF_NOT_BATTLE(false);
const battle::Unit * defender = battleGetUnitByPos(dest);
if(!attacker || !defender)
return false;
return attacker->canShoot()
&& battleMatchOwner(attacker, defender)
&& defender->alive()
&& (!battleIsUnitBlocked(attacker)
|| attacker->hasBonusOfType(Bonus::FREE_SHOOTING));
if(battleMatchOwner(attacker, defender) && defender->alive())
return battleCanShoot(attacker);
return false;
}
TDmgRange CBattleInfoCallback::calculateDmgRange(const BattleAttackInfo & info) const
@@ -897,8 +905,11 @@ TDmgRange CBattleInfoCallback::calculateDmgRange(const BattleAttackInfo & info)
if(info.shooting)
{
//wall / distance penalty + advanced air shield
const bool distPenalty = battleHasDistancePenalty(attackerBonuses, info.attacker->getPosition(), info.defender->getPosition());
const bool obstaclePenalty = battleHasWallPenalty(attackerBonuses, info.attacker->getPosition(), info.defender->getPosition());
BattleHex attackerPos = info.attackerPos.isValid() ? info.attackerPos : info.attacker->getPosition();
BattleHex defenderPos = info.defenderPos.isValid() ? info.defenderPos : info.defender->getPosition();
const bool distPenalty = battleHasDistancePenalty(attackerBonuses, attackerPos, defenderPos);
const bool obstaclePenalty = battleHasWallPenalty(attackerBonuses, attackerPos, defenderPos);
if(distPenalty || defenderBonuses->hasBonus(isAdvancedAirShield, cachingStrAdvAirShield))
multBonus *= 0.5;
@@ -1340,11 +1351,11 @@ ReachabilityInfo CBattleInfoCallback::getFlyingReachability(const ReachabilityIn
return ret;
}
AttackableTiles CBattleInfoCallback::getPotentiallyAttackableHexes (const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos) const
AttackableTiles CBattleInfoCallback::getPotentiallyAttackableHexes (const battle::Unit* attacker, BattleHex destinationTile, BattleHex attackerPos) const
{
//does not return hex attacked directly
//TODO: apply rotation to two-hex attackers
bool isAttacker = attacker->side == BattleSide::ATTACKER;
bool isAttacker = attacker->unitSide() == BattleSide::ATTACKER;
AttackableTiles at;
RETURN_IF_NOT_BATTLE(at);
@@ -1369,8 +1380,8 @@ AttackableTiles CBattleInfoCallback::getPotentiallyAttackableHexes (const CStack
{
if((BattleHex::mutualPosition(tile, destinationTile) > -1 && BattleHex::mutualPosition(tile, hex) > -1)) //adjacent both to attacker's head and attacked tile
{
const CStack * st = battleGetStackByPos(tile, true);
if(st && st->owner != attacker->owner) //only hostile stacks - does it work well with Berserk?
auto st = battleGetUnitByPos(tile, true);
if(st && battleMatchOwner(st, attacker)) //only hostile stacks - does it work well with Berserk?
{
at.hostileCreaturePositions.insert(tile);
}
@@ -1391,45 +1402,50 @@ AttackableTiles CBattleInfoCallback::getPotentiallyAttackableHexes (const CStack
for(BattleHex tile : hexes)
{
//friendly stacks can also be damaged by Dragon Breath
if(battleGetStackByPos(tile, true))
auto st = battleGetUnitByPos(tile, true);
if(st && st != attacker)
{
if(battleGetStackByPos(tile, true) != attacker)
at.friendlyCreaturePositions.insert(tile);
at.friendlyCreaturePositions.insert(tile);
}
}
}
else if(attacker->hasBonusOfType(Bonus::TWO_HEX_ATTACK_BREATH) && BattleHex::mutualPosition(destinationTile, hex) > -1) //only adjacent hexes are subject of dragon breath calculation
else if(attacker->hasBonusOfType(Bonus::TWO_HEX_ATTACK_BREATH))
{
std::vector<BattleHex> hexes; //only one, in fact
int pseudoVector = destinationTile.hex - hex;
switch(pseudoVector)
int pos = BattleHex::mutualPosition(destinationTile, hex);
if (pos > -1) //only adjacent hexes are subject of dragon breath calculation
{
case 1:
case -1:
BattleHex::checkAndPush(destinationTile.hex + pseudoVector, hexes);
break;
case WN: //17 //left-down or right-down
case -WN: //-17 //left-up or right-up
case WN + 1: //18 //right-down
case -WN + 1: //-16 //right-up
BattleHex::checkAndPush(destinationTile.hex + pseudoVector + (((hex / WN) % 2) ? 1 : -1), hexes);
break;
case WN - 1: //16 //left-down
case -WN - 1: //-18 //left-up
BattleHex::checkAndPush(destinationTile.hex + pseudoVector + (((hex / WN) % 2) ? 1 : 0), hexes);
break;
}
for(BattleHex tile : hexes)
{
//friendly stacks can also be damaged by Dragon Breath
if(battleGetStackByPos(tile, true))
at.friendlyCreaturePositions.insert(tile);
std::vector<BattleHex> hexes; //only one, in fact
int pseudoVector = destinationTile.hex - hex;
switch (pseudoVector)
{
case 1:
case -1:
BattleHex::checkAndPush(destinationTile.hex + pseudoVector, hexes);
break;
case WN: //17 //left-down or right-down
case -WN: //-17 //left-up or right-up
case WN + 1: //18 //right-down
case -WN + 1: //-16 //right-up
BattleHex::checkAndPush(destinationTile.hex + pseudoVector + (((hex / WN) % 2) ? 1 : -1), hexes);
break;
case WN - 1: //16 //left-down
case -WN - 1: //-18 //left-up
BattleHex::checkAndPush(destinationTile.hex + pseudoVector + (((hex / WN) % 2) ? 1 : 0), hexes);
break;
}
for (BattleHex tile : hexes)
{
//friendly stacks can also be damaged by Dragon Breath
auto st = battleGetUnitByPos(tile, true);
if (st != nullptr)
at.friendlyCreaturePositions.insert(tile);
}
}
}
return at;
}
AttackableTiles CBattleInfoCallback::getPotentiallyShootableHexes(const CStack * attacker, BattleHex destinationTile, BattleHex attackerPos) const
AttackableTiles CBattleInfoCallback::getPotentiallyShootableHexes(const battle::Unit * attacker, BattleHex destinationTile, BattleHex attackerPos) const
{
//does not return hex attacked directly
AttackableTiles at;
@@ -1445,6 +1461,37 @@ AttackableTiles CBattleInfoCallback::getPotentiallyShootableHexes(const CStack *
return at;
}
std::vector<const battle::Unit*> CBattleInfoCallback::getAttackedBattleUnits(const battle::Unit* attacker, BattleHex destinationTile, bool rangedAttack, BattleHex attackerPos) const
{
std::vector<const battle::Unit*> units;
RETURN_IF_NOT_BATTLE(units);
AttackableTiles at;
if (rangedAttack)
at = getPotentiallyShootableHexes(attacker, destinationTile, attackerPos);
else
at = getPotentiallyAttackableHexes(attacker, destinationTile, attackerPos);
units = battleGetUnitsIf([=](const battle::Unit * unit)
{
if (unit->isGhost() || !unit->alive()) {
return false;
}
for (BattleHex hex : battle::Unit::getHexes(unit->getPosition(), unit->doubleWide(), unit->unitSide())) {
if (vstd::contains(at.hostileCreaturePositions, hex)) {
return true;
}
if (vstd::contains(at.friendlyCreaturePositions, hex)) {
return true;
}
}
return false;
});
return units;
}
std::set<const CStack*> CBattleInfoCallback::getAttackedCreatures(const CStack* attacker, BattleHex destinationTile, bool rangedAttack, BattleHex attackerPos) const
{
std::set<const CStack*> attackedCres;

View File

@@ -89,6 +89,7 @@ public:
bool battleCanAttack(const CStack * stack, const CStack * target, BattleHex dest) const; //determines if stack with given ID can attack target at the selected destination
bool battleCanShoot(const battle::Unit * attacker, BattleHex dest) const; //determines if stack with given ID shoot at the selected destination
bool battleCanShoot(const battle::Unit * attacker) const; //determines if stack with given ID shoot in principle
bool battleIsUnitBlocked(const battle::Unit * unit) const; //returns true if there is neighboring enemy stack
std::set<const battle::Unit *> battleAdjacentUnits(const battle::Unit * unit) const;
@@ -123,8 +124,9 @@ public:
bool isInTacticRange(BattleHex dest) const;
si8 battleGetTacticDist() const; //returns tactic distance for calling player or 0 if this player is not in tactic phase (for ALL_KNOWING actual distance for tactic side)
AttackableTiles getPotentiallyAttackableHexes(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos) const; //TODO: apply rotation to two-hex attacker
AttackableTiles getPotentiallyShootableHexes(const CStack* attacker, BattleHex destinationTile, BattleHex attackerPos) const;
AttackableTiles getPotentiallyAttackableHexes(const battle::Unit* attacker, BattleHex destinationTile, BattleHex attackerPos) const; //TODO: apply rotation to two-hex attacker
AttackableTiles getPotentiallyShootableHexes(const battle::Unit* attacker, BattleHex destinationTile, BattleHex attackerPos) const;
std::vector<const battle::Unit *> getAttackedBattleUnits(const battle::Unit* attacker, BattleHex destinationTile, bool rangedAttack, BattleHex attackerPos = BattleHex::INVALID) const; //calculates range of multi-hex attacks
std::set<const CStack*> getAttackedCreatures(const CStack* attacker, BattleHex destinationTile, bool rangedAttack, BattleHex attackerPos = BattleHex::INVALID) const; //calculates range of multi-hex attacks
bool isToReverse(BattleHex hexFrom, BattleHex hexTo, bool curDir /*if true, creature is in attacker's direction*/, bool toDoubleWide, bool toDir) const; //determines if creature should be reversed (it stands on hexFrom and should 'see' hexTo)
bool isToReverseHlp(BattleHex hexFrom, BattleHex hexTo, bool curDir) const; //helper for isToReverse