2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* StupidAI.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
|
|
|
|
*
|
|
|
|
*/
|
2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "../../lib/AI_Base.h"
|
2010-12-22 22:14:40 +02:00
|
|
|
#include "StupidAI.h"
|
2017-03-17 17:48:44 +02:00
|
|
|
#include "../../lib/CStack.h"
|
2011-01-07 12:48:31 +02:00
|
|
|
#include "../../CCallback.h"
|
2011-01-08 20:33:40 +02:00
|
|
|
#include "../../lib/CCreatureHandler.h"
|
2023-08-17 18:18:14 +02:00
|
|
|
#include "../../lib/battle/BattleAction.h"
|
2023-08-28 17:59:12 +02:00
|
|
|
#include "../../lib/battle/BattleInfo.h"
|
2011-01-08 20:33:40 +02:00
|
|
|
|
2015-12-29 04:43:33 +02:00
|
|
|
static std::shared_ptr<CBattleCallback> cbc;
|
2010-12-22 22:14:40 +02:00
|
|
|
|
2018-01-13 10:43:26 +02:00
|
|
|
CStupidAI::CStupidAI()
|
2013-06-23 22:35:54 +03:00
|
|
|
: side(-1)
|
2023-07-24 22:01:18 +02:00
|
|
|
, wasWaitingForRealize(false)
|
|
|
|
, wasUnlockingGs(false)
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2010-12-25 03:43:40 +02:00
|
|
|
print("created");
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
2018-01-13 10:43:26 +02:00
|
|
|
CStupidAI::~CStupidAI()
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2010-12-25 03:43:40 +02:00
|
|
|
print("destroyed");
|
2023-07-24 22:01:18 +02:00
|
|
|
if(cb)
|
|
|
|
{
|
|
|
|
//Restore previous state of CB - it may be shared with the main AI (like VCAI)
|
|
|
|
cb->waitTillRealize = wasWaitingForRealize;
|
|
|
|
cb->unlockGsWhenWaiting = wasUnlockingGs;
|
|
|
|
}
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
2022-12-07 21:50:45 +02:00
|
|
|
void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB)
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2010-12-25 03:43:40 +02:00
|
|
|
print("init called, saving ptr to IBattleCallback");
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
env = ENV;
|
2011-01-08 20:33:40 +02:00
|
|
|
cbc = cb = CB;
|
2023-07-24 22:01:18 +02:00
|
|
|
|
|
|
|
wasWaitingForRealize = CB->waitTillRealize;
|
|
|
|
wasUnlockingGs = CB->unlockGsWhenWaiting;
|
|
|
|
CB->waitTillRealize = false;
|
|
|
|
CB->unlockGsWhenWaiting = false;
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
2023-08-20 13:53:25 +02:00
|
|
|
void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AutocombatPreferences autocombatPreferences)
|
|
|
|
{
|
|
|
|
initBattleInterface(ENV, CB);
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::actionFinished(const BattleID & battleID, const BattleAction &action)
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2010-12-25 03:43:40 +02:00
|
|
|
print("actionFinished called");
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::actionStarted(const BattleID & battleID, const BattleAction &action)
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2010-12-25 03:43:40 +02:00
|
|
|
print("actionStarted called");
|
2010-12-22 22:14:40 +02:00
|
|
|
}
|
|
|
|
|
2022-06-11 17:45:34 +02:00
|
|
|
class EnemyInfo
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2022-06-11 17:45:34 +02:00
|
|
|
public:
|
2011-01-08 20:33:40 +02:00
|
|
|
const CStack * s;
|
|
|
|
int adi, adr;
|
2011-12-22 16:05:19 +03:00
|
|
|
std::vector<BattleHex> attackFrom; //for melee fight
|
2016-12-12 20:13:43 +02:00
|
|
|
EnemyInfo(const CStack * _s) : s(_s), adi(0), adr(0)
|
2011-01-08 20:33:40 +02:00
|
|
|
{}
|
2023-08-28 17:59:12 +02:00
|
|
|
void calcDmg(const BattleID & battleID, const CStack * ourStack)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2023-01-14 19:03:18 +02:00
|
|
|
// FIXME: provide distance info for Jousting bonus
|
2023-03-24 17:17:17 +02:00
|
|
|
DamageEstimation retal;
|
2023-08-28 17:59:12 +02:00
|
|
|
DamageEstimation dmg = cbc->getBattle(battleID)->battleEstimateDamage(ourStack, s, 0, &retal);
|
2023-03-24 17:17:17 +02:00
|
|
|
adi = static_cast<int>((dmg.damage.min + dmg.damage.max) / 2);
|
|
|
|
adr = static_cast<int>((retal.damage.min + retal.damage.max) / 2);
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const EnemyInfo& ei) const
|
|
|
|
{
|
|
|
|
return s == ei.s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
bool isMoreProfitable(const EnemyInfo &ei1, const EnemyInfo& ei2)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
|
|
|
return (ei1.adi-ei1.adr) < (ei2.adi - ei2.adr);
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
static bool willSecondHexBlockMoreEnemyShooters(const BattleID & battleID, const BattleHex &h1, const BattleHex &h2)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
|
|
|
int shooters[2] = {0}; //count of shooters on hexes
|
|
|
|
|
|
|
|
for(int i = 0; i < 2; i++)
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
{
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & neighbour : (i ? h2 : h1).neighbouringTiles())
|
2023-08-28 17:59:12 +02:00
|
|
|
if(const auto * s = cbc->getBattle(battleID)->battleGetUnitByPos(neighbour))
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
if(s->isShooter())
|
|
|
|
shooters[i]++;
|
|
|
|
}
|
2011-01-08 20:33:40 +02:00
|
|
|
|
|
|
|
return shooters[0] < shooters[1];
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::yourTacticPhase(const BattleID & battleID, int distance)
|
2023-07-18 20:43:53 +02:00
|
|
|
{
|
2023-08-28 17:59:12 +02:00
|
|
|
cb->battleMakeTacticAction(battleID, BattleAction::makeEndOFTacticPhase(cb->getBattle(battleID)->battleGetTacticsSide()));
|
2023-07-18 20:43:53 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::activeStack(const BattleID & battleID, const CStack * stack)
|
2010-12-22 22:14:40 +02:00
|
|
|
{
|
2023-08-20 22:45:41 +02:00
|
|
|
//boost::this_thread::sleep_for(boost::chrono::seconds(2));
|
2012-08-26 12:07:48 +03:00
|
|
|
print("activeStack called for " + stack->nodeName());
|
2023-08-28 17:59:12 +02:00
|
|
|
ReachabilityInfo dists = cb->getBattle(battleID)->getReachability(stack);
|
2011-01-08 20:33:40 +02:00
|
|
|
std::vector<EnemyInfo> enemiesShootable, enemiesReachable, enemiesUnreachable;
|
|
|
|
|
2023-04-27 19:43:20 +02:00
|
|
|
if(stack->creatureId() == CreatureID::CATAPULT)
|
2012-01-03 04:55:26 +03:00
|
|
|
{
|
|
|
|
BattleAction attack;
|
2016-02-14 13:38:24 +02:00
|
|
|
static const std::vector<int> wallHexes = {50, 183, 182, 130, 78, 29, 12, 95};
|
2017-07-20 06:08:49 +02:00
|
|
|
auto seletectedHex = *RandomGeneratorUtil::nextItem(wallHexes, CRandomGenerator::getDefault());
|
|
|
|
attack.aimToHex(seletectedHex);
|
|
|
|
attack.actionType = EActionType::CATAPULT;
|
2012-01-03 04:55:26 +03:00
|
|
|
attack.side = side;
|
2023-04-27 19:43:20 +02:00
|
|
|
attack.stackNumber = stack->unitId();
|
2012-01-03 04:55:26 +03:00
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
cb->battleMakeUnitAction(battleID, attack);
|
2023-07-18 18:55:59 +02:00
|
|
|
return;
|
2012-01-03 04:55:26 +03:00
|
|
|
}
|
2023-05-01 00:20:01 +02:00
|
|
|
else if(stack->hasBonusOfType(BonusType::SIEGE_WEAPON))
|
2013-08-25 16:03:29 +03:00
|
|
|
{
|
2023-08-28 17:59:12 +02:00
|
|
|
cb->battleMakeUnitAction(battleID, BattleAction::makeDefend(stack));
|
2023-07-18 18:55:59 +02:00
|
|
|
return;
|
2013-08-25 16:03:29 +03:00
|
|
|
}
|
2012-01-03 04:55:26 +03:00
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
for (const CStack *s : cb->getBattle(battleID)->battleGetStacks(CBattleInfoEssentials::ONLY_ENEMY))
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2023-08-28 17:59:12 +02:00
|
|
|
if(cb->getBattle(battleID)->battleCanShoot(stack, s->getPosition()))
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2011-02-14 22:31:53 +02:00
|
|
|
enemiesShootable.push_back(s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-28 17:59:12 +02:00
|
|
|
std::vector<BattleHex> avHexes = cb->getBattle(battleID)->battleGetAvailableHexes(stack, false);
|
2012-08-26 22:13:57 +03:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for (BattleHex hex : avHexes)
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2011-02-14 22:31:53 +02:00
|
|
|
if(CStack::isMeleeAttackPossible(stack, s, hex))
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2011-02-14 22:31:53 +02:00
|
|
|
std::vector<EnemyInfo>::iterator i = std::find(enemiesReachable.begin(), enemiesReachable.end(), s);
|
|
|
|
if(i == enemiesReachable.end())
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2011-02-14 22:31:53 +02:00
|
|
|
enemiesReachable.push_back(s);
|
|
|
|
i = enemiesReachable.begin() + (enemiesReachable.size() - 1);
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
|
2011-02-14 22:31:53 +02:00
|
|
|
i->attackFrom.push_back(hex);
|
|
|
|
}
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
2011-02-14 22:31:53 +02:00
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
if(!vstd::contains(enemiesReachable, s) && s->getPosition().isValid())
|
2011-02-14 22:31:53 +02:00
|
|
|
enemiesUnreachable.push_back(s);
|
2011-01-07 12:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 21:28:01 +02:00
|
|
|
for ( auto & enemy : enemiesReachable )
|
2023-08-28 17:59:12 +02:00
|
|
|
enemy.calcDmg(battleID, stack);
|
2015-12-03 21:28:01 +02:00
|
|
|
|
|
|
|
for ( auto & enemy : enemiesShootable )
|
2023-08-28 17:59:12 +02:00
|
|
|
enemy.calcDmg(battleID, stack);
|
2015-12-03 21:28:01 +02:00
|
|
|
|
2011-01-08 20:33:40 +02:00
|
|
|
if(enemiesShootable.size())
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
2011-01-08 20:33:40 +02:00
|
|
|
const EnemyInfo &ei= *std::max_element(enemiesShootable.begin(), enemiesShootable.end(), isMoreProfitable);
|
2023-08-28 17:59:12 +02:00
|
|
|
cb->battleMakeUnitAction(battleID, BattleAction::makeShotAttack(stack, ei.s));
|
2023-07-18 18:55:59 +02:00
|
|
|
return;
|
2010-12-25 03:43:40 +02:00
|
|
|
}
|
2011-01-08 20:33:40 +02:00
|
|
|
else if(enemiesReachable.size())
|
|
|
|
{
|
|
|
|
const EnemyInfo &ei= *std::max_element(enemiesReachable.begin(), enemiesReachable.end(), &isMoreProfitable);
|
2023-08-28 17:59:12 +02:00
|
|
|
BattleHex targetHex = *std::max_element(ei.attackFrom.begin(), ei.attackFrom.end(), [&](auto a, auto b) { return willSecondHexBlockMoreEnemyShooters(battleID, a, b);});
|
|
|
|
|
|
|
|
cb->battleMakeUnitAction(battleID, BattleAction::makeMeleeAttack(stack, ei.s->getPosition(), targetHex));
|
2023-07-18 18:55:59 +02:00
|
|
|
return;
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
2012-09-28 19:48:29 +03:00
|
|
|
else if(enemiesUnreachable.size()) //due to #955 - a buggy battle may occur when there are no enemies
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2020-11-23 08:40:36 +02:00
|
|
|
auto closestEnemy = vstd::minElementByFun(enemiesUnreachable, [&](const EnemyInfo & ei) -> int
|
2011-01-07 12:48:31 +02:00
|
|
|
{
|
2020-11-23 08:40:36 +02:00
|
|
|
return dists.distToNearestNeighbour(stack, ei.s);
|
|
|
|
});
|
|
|
|
|
|
|
|
if(dists.distToNearestNeighbour(stack, closestEnemy->s) < GameConstants::BFIELD_SIZE)
|
|
|
|
{
|
2023-08-28 17:59:12 +02:00
|
|
|
cb->battleMakeUnitAction(battleID, goTowards(battleID, stack, closestEnemy->s->getAttackableHexes(stack)));
|
2023-07-18 18:55:59 +02:00
|
|
|
return;
|
2011-01-07 12:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
cb->battleMakeUnitAction(battleID, BattleAction::makeDefend(stack));
|
2023-07-18 18:55:59 +02:00
|
|
|
return;
|
2010-12-25 03:43:40 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::battleAttack(const BattleID & battleID, const BattleAttack *ba)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleAttack called");
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::battleStacksAttacked(const BattleID & battleID, const std::vector<BattleStackAttacked> & bsa, bool ranged)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleStacksAttacked called");
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::battleEnd(const BattleID & battleID, const BattleResult *br, QueryID queryID)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleEnd called");
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:59:07 +03:00
|
|
|
// void CStupidAI::battleResultsApplied()
|
2011-05-10 01:20:47 +03:00
|
|
|
// {
|
|
|
|
// print("battleResultsApplied called");
|
|
|
|
// }
|
2010-12-25 03:43:40 +02:00
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::battleNewRoundFirst(const BattleID & battleID, int round)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleNewRoundFirst called");
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::battleNewRound(const BattleID & battleID, int round)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleNewRound called");
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::battleStackMoved(const BattleID & battleID, const CStack * stack, std::vector<BattleHex> dest, int distance, bool teleport)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
2017-07-12 21:01:10 +02:00
|
|
|
print("battleStackMoved called");
|
2010-12-25 03:43:40 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::battleSpellCast(const BattleID & battleID, const BattleSpellCast *sc)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleSpellCast called");
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::battleStacksEffectsSet(const BattleID & battleID, const SetStackEffect & sse)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleStacksEffectsSet called");
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::battleStart(const BattleID & battleID, const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side, bool replayAllowed)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleStart called");
|
|
|
|
side = Side;
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
void CStupidAI::battleCatapultAttacked(const BattleID & battleID, const CatapultAttack & ca)
|
2010-12-25 03:43:40 +02:00
|
|
|
{
|
|
|
|
print("battleCatapultAttacked called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStupidAI::print(const std::string &text) const
|
|
|
|
{
|
2016-08-13 16:44:37 +02:00
|
|
|
logAi->trace("CStupidAI [%p]: %s", this, text);
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:59:12 +02:00
|
|
|
BattleAction CStupidAI::goTowards(const BattleID & battleID, const CStack * stack, std::vector<BattleHex> hexes) const
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2023-08-28 17:59:12 +02:00
|
|
|
auto reachability = cb->getBattle(battleID)->getReachability(stack);
|
|
|
|
auto avHexes = cb->getBattle(battleID)->battleGetAvailableHexes(reachability, stack, false);
|
2012-08-26 12:07:48 +03:00
|
|
|
|
2020-12-15 20:38:01 +02:00
|
|
|
if(!avHexes.size() || !hexes.size()) //we are blocked or dest is blocked
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
2020-12-15 20:38:01 +02:00
|
|
|
std::sort(hexes.begin(), hexes.end(), [&](BattleHex h1, BattleHex h2) -> bool
|
2020-11-23 08:40:36 +02:00
|
|
|
{
|
2020-12-15 20:38:01 +02:00
|
|
|
return reachability.distances[h1] < reachability.distances[h2];
|
|
|
|
});
|
|
|
|
|
|
|
|
for(auto hex : hexes)
|
|
|
|
{
|
|
|
|
if(vstd::contains(avHexes, hex))
|
|
|
|
return BattleAction::makeMove(stack, hex);
|
|
|
|
|
|
|
|
if(stack->coversPos(hex))
|
|
|
|
{
|
|
|
|
logAi->warn("Warning: already standing on neighbouring tile!");
|
|
|
|
//We shouldn't even be here...
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
2020-11-23 08:40:36 +02:00
|
|
|
}
|
2011-01-08 20:33:40 +02:00
|
|
|
|
2020-12-15 20:38:01 +02:00
|
|
|
BattleHex bestNeighbor = hexes.front();
|
|
|
|
|
|
|
|
if(reachability.distances[bestNeighbor] > GameConstants::BFIELD_SIZE)
|
2011-09-06 16:59:26 +03:00
|
|
|
{
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
2023-05-01 00:20:01 +02:00
|
|
|
if(stack->hasBonusOfType(BonusType::FLYING))
|
2012-08-26 12:07:48 +03:00
|
|
|
{
|
|
|
|
// Flying stack doesn't go hex by hex, so we can't backtrack using predecessors.
|
|
|
|
// We just check all available hexes and pick the one closest to the target.
|
2020-11-23 08:40:36 +02:00
|
|
|
auto nearestAvailableHex = vstd::minElementByFun(avHexes, [&](BattleHex hex) -> int
|
2012-08-26 12:07:48 +03:00
|
|
|
{
|
2020-11-23 08:40:36 +02:00
|
|
|
return BattleHex::getDistance(bestNeighbor, hex);
|
|
|
|
});
|
2012-08-26 12:07:48 +03:00
|
|
|
|
|
|
|
return BattleAction::makeMove(stack, *nearestAvailableHex);
|
|
|
|
}
|
|
|
|
else
|
2011-01-08 20:33:40 +02:00
|
|
|
{
|
2012-08-26 12:07:48 +03:00
|
|
|
BattleHex currentDest = bestNeighbor;
|
|
|
|
while(1)
|
|
|
|
{
|
2020-11-23 08:40:36 +02:00
|
|
|
if(!currentDest.isValid())
|
|
|
|
{
|
|
|
|
logAi->error("CBattleAI::goTowards: internal error");
|
|
|
|
return BattleAction::makeDefend(stack);
|
|
|
|
}
|
|
|
|
|
2012-08-26 12:07:48 +03:00
|
|
|
if(vstd::contains(avHexes, currentDest))
|
|
|
|
return BattleAction::makeMove(stack, currentDest);
|
|
|
|
|
|
|
|
currentDest = reachability.predecessors[currentDest];
|
|
|
|
}
|
2011-01-08 20:33:40 +02:00
|
|
|
}
|
|
|
|
}
|