BattleAI.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*
  2. * BattleAI.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "BattleAI.h"
  12. #include <vstd/RNG.h>
  13. #include "StackWithBonuses.h"
  14. #include "EnemyInfo.h"
  15. #include "../../lib/CStopWatch.h"
  16. #include "../../lib/CThreadHelper.h"
  17. #include "../../lib/spells/CSpellHandler.h"
  18. #include "../../lib/spells/ISpellMechanics.h"
  19. #include "../../lib/CStack.h"//todo: remove
  20. #define LOGL(text) print(text)
  21. #define LOGFL(text, formattingEl) print(boost::str(boost::format(text) % formattingEl))
  22. class RNGStub : public vstd::RNG
  23. {
  24. public:
  25. vstd::TRandI64 getInt64Range(int64_t lower, int64_t upper) override
  26. {
  27. return [=]()->int64_t
  28. {
  29. return (lower + upper)/2;
  30. };
  31. }
  32. vstd::TRand getDoubleRange(double lower, double upper) override
  33. {
  34. return [=]()->double
  35. {
  36. return (lower + upper)/2;
  37. };
  38. }
  39. };
  40. enum class SpellTypes
  41. {
  42. ADVENTURE, BATTLE, OTHER
  43. };
  44. SpellTypes spellType(const CSpell * spell)
  45. {
  46. if(!spell->isCombatSpell() || spell->isCreatureAbility())
  47. return SpellTypes::OTHER;
  48. if(spell->isOffensiveSpell() || spell->hasEffects() || spell->hasBattleEffects())
  49. return SpellTypes::BATTLE;
  50. return SpellTypes::OTHER;
  51. }
  52. CBattleAI::CBattleAI()
  53. : side(-1), wasWaitingForRealize(false), wasUnlockingGs(false)
  54. {
  55. }
  56. CBattleAI::~CBattleAI()
  57. {
  58. if(cb)
  59. {
  60. //Restore previous state of CB - it may be shared with the main AI (like VCAI)
  61. cb->waitTillRealize = wasWaitingForRealize;
  62. cb->unlockGsWhenWaiting = wasUnlockingGs;
  63. }
  64. }
  65. void CBattleAI::init(std::shared_ptr<CBattleCallback> CB)
  66. {
  67. setCbc(CB);
  68. cb = CB;
  69. playerID = *CB->getPlayerID(); //TODO should be sth in callback
  70. wasWaitingForRealize = cb->waitTillRealize;
  71. wasUnlockingGs = CB->unlockGsWhenWaiting;
  72. CB->waitTillRealize = true;
  73. CB->unlockGsWhenWaiting = false;
  74. }
  75. BattleAction CBattleAI::activeStack( const CStack * stack )
  76. {
  77. LOG_TRACE_PARAMS(logAi, "stack: %s", stack->nodeName()) ;
  78. setCbc(cb); //TODO: make solid sure that AIs always use their callbacks (need to take care of event handlers too)
  79. try
  80. {
  81. if(stack->type->idNumber == CreatureID::CATAPULT)
  82. return useCatapult(stack);
  83. if(stack->hasBonusOfType(Bonus::SIEGE_WEAPON) && stack->hasBonusOfType(Bonus::HEALER))
  84. {
  85. auto healingTargets = cb->battleGetStacks(CBattleInfoEssentials::ONLY_MINE);
  86. std::map<int, const CStack*> woundHpToStack;
  87. for(auto stack : healingTargets)
  88. if(auto woundHp = stack->MaxHealth() - stack->getFirstHPleft())
  89. woundHpToStack[woundHp] = stack;
  90. if(woundHpToStack.empty())
  91. return BattleAction::makeDefend(stack);
  92. else
  93. return BattleAction::makeHeal(stack, woundHpToStack.rbegin()->second); //last element of the woundHpToStack is the most wounded stack
  94. }
  95. attemptCastingSpell();
  96. if(auto ret = getCbc()->battleIsFinished())
  97. {
  98. //spellcast may finish battle
  99. //send special preudo-action
  100. BattleAction cancel;
  101. cancel.actionType = EActionType::CANCEL;
  102. return cancel;
  103. }
  104. if(auto action = considerFleeingOrSurrendering())
  105. return *action;
  106. //best action is from effective owner point if view, we are effective owner as we received "activeStack"
  107. //evaluate casting spell for spellcasting stack
  108. boost::optional<PossibleSpellcast> bestSpellcast(boost::none);
  109. //TODO: faerie dragon type spell should be selected by server
  110. SpellID creatureSpellToCast = cb->battleGetRandomStackSpell(CRandomGenerator::getDefault(), stack, CBattleInfoCallback::RANDOM_AIMED);
  111. if(stack->hasBonusOfType(Bonus::SPELLCASTER) && stack->canCast() && creatureSpellToCast != SpellID::NONE)
  112. {
  113. const CSpell * spell = creatureSpellToCast.toSpell();
  114. if(spell->canBeCast(getCbc().get(), spells::Mode::CREATURE_ACTIVE, stack))
  115. {
  116. std::vector<PossibleSpellcast> possibleCasts;
  117. spells::BattleCast temp(getCbc().get(), stack, spells::Mode::CREATURE_ACTIVE, spell);
  118. for(auto & target : temp.findPotentialTargets())
  119. {
  120. PossibleSpellcast ps;
  121. ps.dest = target;
  122. ps.spell = spell;
  123. evaluateCreatureSpellcast(stack, ps);
  124. possibleCasts.push_back(ps);
  125. }
  126. std::sort(possibleCasts.begin(), possibleCasts.end(), [&](const PossibleSpellcast & lhs, const PossibleSpellcast & rhs) { return lhs.value > rhs.value; });
  127. if(!possibleCasts.empty() && possibleCasts.front().value > 0)
  128. {
  129. bestSpellcast = boost::optional<PossibleSpellcast>(possibleCasts.front());
  130. }
  131. }
  132. }
  133. HypotheticBattle hb(getCbc());
  134. PotentialTargets targets(stack, &hb);
  135. if(!targets.possibleAttacks.empty())
  136. {
  137. AttackPossibility bestAttack = targets.bestAction();
  138. //TODO: consider more complex spellcast evaluation, f.e. because "re-retaliation" during enemy move in same turn for melee attack etc.
  139. if(bestSpellcast.is_initialized() && bestSpellcast->value > bestAttack.damageDiff())
  140. return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id);
  141. else if(bestAttack.attack.shooting)
  142. return BattleAction::makeShotAttack(stack, bestAttack.attack.defender);
  143. else
  144. {
  145. auto &target = bestAttack;
  146. logAi->debug("BattleAI: %s -> %s %d from, %d curpos %d dist %d speed %d: %d %d %d",
  147. VLC->creh->creatures.at(target.attackerState->creatureId())->identifier,
  148. VLC->creh->creatures.at(target.affectedUnits[0]->creatureId())->identifier,
  149. (int)target.affectedUnits.size(), (int)target.from, (int)bestAttack.attack.attacker->getPosition().hex,
  150. (int)bestAttack.attack.chargedFields, (int)bestAttack.attack.attacker->Speed(0, true),
  151. (int)target.damageDealt, (int)target.damageReceived, (int)target.attackValue()
  152. );
  153. return BattleAction::makeMeleeAttack(stack, bestAttack.attack.defender->getPosition(), bestAttack.from);
  154. }
  155. }
  156. else if(bestSpellcast.is_initialized())
  157. {
  158. return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id);
  159. }
  160. else
  161. {
  162. if(stack->waited())
  163. {
  164. //ThreatMap threatsToUs(stack); // These lines may be usefull but they are't used in the code.
  165. auto dists = getCbc()->battleGetDistances(stack, stack->getPosition());
  166. if(!targets.unreachableEnemies.empty())
  167. {
  168. const EnemyInfo &ei= *range::min_element(targets.unreachableEnemies, std::bind(isCloser, _1, _2, std::ref(dists)));
  169. if(distToNearestNeighbour(ei.s->getPosition(), dists) < GameConstants::BFIELD_SIZE)
  170. {
  171. return goTowards(stack, ei.s->getPosition());
  172. }
  173. }
  174. }
  175. else
  176. {
  177. return BattleAction::makeWait(stack);
  178. }
  179. }
  180. }
  181. catch(boost::thread_interrupted &)
  182. {
  183. throw;
  184. }
  185. catch(std::exception &e)
  186. {
  187. logAi->error("Exception occurred in %s %s",__FUNCTION__, e.what());
  188. }
  189. return BattleAction::makeDefend(stack);
  190. }
  191. BattleAction CBattleAI::goTowards(const CStack * stack, BattleHex destination)
  192. {
  193. if(!destination.isValid())
  194. {
  195. logAi->error("CBattleAI::goTowards: invalid destination");
  196. return BattleAction::makeDefend(stack);
  197. }
  198. auto reachability = cb->getReachability(stack);
  199. auto avHexes = cb->battleGetAvailableHexes(reachability, stack);
  200. if(vstd::contains(avHexes, destination))
  201. return BattleAction::makeMove(stack, destination);
  202. auto destNeighbours = destination.neighbouringTiles();
  203. if(vstd::contains_if(destNeighbours, [&](BattleHex n) { return stack->coversPos(destination); }))
  204. {
  205. logAi->warn("Warning: already standing on neighbouring tile!");
  206. //We shouldn't even be here...
  207. return BattleAction::makeDefend(stack);
  208. }
  209. vstd::erase_if(destNeighbours, [&](BattleHex hex){ return !reachability.accessibility.accessible(hex, stack); });
  210. if(!avHexes.size() || !destNeighbours.size()) //we are blocked or dest is blocked
  211. {
  212. return BattleAction::makeDefend(stack);
  213. }
  214. if(stack->hasBonusOfType(Bonus::FLYING))
  215. {
  216. // Flying stack doesn't go hex by hex, so we can't backtrack using predecessors.
  217. // We just check all available hexes and pick the one closest to the target.
  218. auto distToDestNeighbour = [&](BattleHex hex) -> int
  219. {
  220. auto nearestNeighbourToHex = vstd::minElementByFun(destNeighbours, [&](BattleHex a)
  221. {return BattleHex::getDistance(a, hex);});
  222. return BattleHex::getDistance(*nearestNeighbourToHex, hex);
  223. };
  224. auto nearestAvailableHex = vstd::minElementByFun(avHexes, distToDestNeighbour);
  225. return BattleAction::makeMove(stack, *nearestAvailableHex);
  226. }
  227. else
  228. {
  229. BattleHex bestNeighbor = destination;
  230. if(distToNearestNeighbour(destination, reachability.distances, &bestNeighbor) > GameConstants::BFIELD_SIZE)
  231. {
  232. return BattleAction::makeDefend(stack);
  233. }
  234. BattleHex currentDest = bestNeighbor;
  235. while(1)
  236. {
  237. if(!currentDest.isValid())
  238. {
  239. logAi->error("CBattleAI::goTowards: internal error");
  240. return BattleAction::makeDefend(stack);
  241. }
  242. if(vstd::contains(avHexes, currentDest))
  243. return BattleAction::makeMove(stack, currentDest);
  244. currentDest = reachability.predecessors[currentDest];
  245. }
  246. }
  247. }
  248. BattleAction CBattleAI::useCatapult(const CStack * stack)
  249. {
  250. throw std::runtime_error("CBattleAI::useCatapult is not implemented.");
  251. }
  252. void CBattleAI::attemptCastingSpell()
  253. {
  254. auto hero = cb->battleGetMyHero();
  255. if(!hero)
  256. return;
  257. if(cb->battleCanCastSpell(hero, spells::Mode::HERO) != ESpellCastProblem::OK)
  258. return;
  259. LOGL("Casting spells sounds like fun. Let's see...");
  260. //Get all spells we can cast
  261. std::vector<const CSpell*> possibleSpells;
  262. vstd::copy_if(VLC->spellh->objects, std::back_inserter(possibleSpells), [hero](const CSpell *s) -> bool
  263. {
  264. return s->canBeCast(getCbc().get(), spells::Mode::HERO, hero);
  265. });
  266. LOGFL("I can cast %d spells.", possibleSpells.size());
  267. vstd::erase_if(possibleSpells, [](const CSpell *s)
  268. {
  269. return spellType(s) != SpellTypes::BATTLE;
  270. });
  271. LOGFL("I know how %d of them works.", possibleSpells.size());
  272. //Get possible spell-target pairs
  273. std::vector<PossibleSpellcast> possibleCasts;
  274. for(auto spell : possibleSpells)
  275. {
  276. spells::BattleCast temp(getCbc().get(), hero, spells::Mode::HERO, spell);
  277. for(auto & target : temp.findPotentialTargets())
  278. {
  279. PossibleSpellcast ps;
  280. ps.dest = target;
  281. ps.spell = spell;
  282. possibleCasts.push_back(ps);
  283. }
  284. }
  285. LOGFL("Found %d spell-target combinations.", possibleCasts.size());
  286. if(possibleCasts.empty())
  287. return;
  288. using ValueMap = PossibleSpellcast::ValueMap;
  289. auto evaluateQueue = [&](ValueMap & values, const std::vector<battle::Units> & queue, HypotheticBattle * state, size_t minTurnSpan, bool * enemyHadTurnOut) -> bool
  290. {
  291. bool firstRound = true;
  292. bool enemyHadTurn = false;
  293. size_t ourTurnSpan = 0;
  294. bool stop = false;
  295. for(auto & round : queue)
  296. {
  297. if(!firstRound)
  298. state->nextRound(0);//todo: set actual value?
  299. for(auto unit : round)
  300. {
  301. if(!vstd::contains(values, unit->unitId()))
  302. values[unit->unitId()] = 0;
  303. if(!unit->alive())
  304. continue;
  305. if(state->battleGetOwner(unit) != playerID)
  306. {
  307. enemyHadTurn = true;
  308. if(!firstRound || state->battleCastSpells(unit->unitSide()) == 0)
  309. {
  310. //enemy could counter our spell at this point
  311. //anyway, we do not know what enemy will do
  312. //just stop evaluation
  313. stop = true;
  314. break;
  315. }
  316. }
  317. else if(!enemyHadTurn)
  318. {
  319. ourTurnSpan++;
  320. }
  321. state->nextTurn(unit->unitId());
  322. PotentialTargets pt(unit, state);
  323. if(!pt.possibleAttacks.empty())
  324. {
  325. AttackPossibility ap = pt.bestAction();
  326. auto swb = state->getForUpdate(unit->unitId());
  327. *swb = *ap.attackerState;
  328. if(ap.damageDealt > 0)
  329. swb->removeUnitBonus(Bonus::UntilAttack);
  330. if(ap.damageReceived > 0)
  331. swb->removeUnitBonus(Bonus::UntilBeingAttacked);
  332. for(auto affected : ap.affectedUnits)
  333. {
  334. swb = state->getForUpdate(affected->unitId());
  335. *swb = *affected;
  336. if(ap.damageDealt > 0)
  337. swb->removeUnitBonus(Bonus::UntilBeingAttacked);
  338. if(ap.damageReceived > 0 && ap.attack.defender->unitId() == affected->unitId())
  339. swb->removeUnitBonus(Bonus::UntilAttack);
  340. }
  341. }
  342. auto bav = pt.bestActionValue();
  343. //best action is from effective owner`s point if view, we need to convert to our point if view
  344. if(state->battleGetOwner(unit) != playerID)
  345. bav = -bav;
  346. values[unit->unitId()] += bav;
  347. }
  348. firstRound = false;
  349. if(stop)
  350. break;
  351. }
  352. if(enemyHadTurnOut)
  353. *enemyHadTurnOut = enemyHadTurn;
  354. return ourTurnSpan >= minTurnSpan;
  355. };
  356. RNGStub rngStub;
  357. ValueMap valueOfStack;
  358. ValueMap healthOfStack;
  359. TStacks all = cb->battleGetAllStacks(false);
  360. size_t ourRemainingTurns = 0;
  361. for(auto unit : all)
  362. {
  363. healthOfStack[unit->unitId()] = unit->getAvailableHealth();
  364. valueOfStack[unit->unitId()] = 0;
  365. if(cb->battleGetOwner(unit) == playerID && unit->canMove() && !unit->moved())
  366. ourRemainingTurns++;
  367. }
  368. LOGFL("I have %d turns left in this round", ourRemainingTurns);
  369. const bool castNow = ourRemainingTurns <= 1;
  370. if(castNow)
  371. print("I should try to cast a spell now");
  372. else
  373. print("I could wait better moment to cast a spell");
  374. auto amount = all.size();
  375. std::vector<battle::Units> turnOrder;
  376. cb->battleGetTurnOrder(turnOrder, amount, 2); //no more than 1 turn after current, each unit at least once
  377. {
  378. bool enemyHadTurn = false;
  379. HypotheticBattle state(cb);
  380. evaluateQueue(valueOfStack, turnOrder, &state, 0, &enemyHadTurn);
  381. if(!enemyHadTurn)
  382. {
  383. auto battleIsFinishedOpt = state.battleIsFinished();
  384. if(battleIsFinishedOpt)
  385. {
  386. print("No need to cast a spell. Battle will finish soon.");
  387. return;
  388. }
  389. }
  390. }
  391. auto evaluateSpellcast = [&] (PossibleSpellcast * ps)
  392. {
  393. HypotheticBattle state(cb);
  394. spells::BattleCast cast(&state, hero, spells::Mode::HERO, ps->spell);
  395. cast.target = ps->dest;
  396. cast.cast(&state, rngStub);
  397. ValueMap newHealthOfStack;
  398. ValueMap newValueOfStack;
  399. size_t ourUnits = 0;
  400. for(auto unit : all)
  401. {
  402. auto unitId = unit->unitId();
  403. auto localUnit = state.battleGetUnitByID(unitId);
  404. newHealthOfStack[unitId] = localUnit->getAvailableHealth();
  405. newValueOfStack[unitId] = 0;
  406. if(state.battleGetOwner(localUnit) == playerID && localUnit->alive() && localUnit->willMove())
  407. ourUnits++;
  408. }
  409. size_t minTurnSpan = ourUnits/3; //todo: tweak this
  410. std::vector<battle::Units> newTurnOrder;
  411. state.battleGetTurnOrder(newTurnOrder, amount, 2);
  412. const bool turnSpanOK = evaluateQueue(newValueOfStack, newTurnOrder, &state, minTurnSpan, nullptr);
  413. if(turnSpanOK || castNow)
  414. {
  415. int64_t totalGain = 0;
  416. for(auto unit : all)
  417. {
  418. auto unitId = unit->unitId();
  419. auto localUnit = state.battleGetUnitByID(unitId);
  420. auto newValue = getValOr(newValueOfStack, unitId, 0);
  421. auto oldValue = getValOr(valueOfStack, unitId, 0);
  422. auto healthDiff = newHealthOfStack[unitId] - healthOfStack[unitId];
  423. if(localUnit->unitOwner() != playerID)
  424. healthDiff = -healthDiff;
  425. if(healthDiff < 0)
  426. {
  427. ps->value = -1;
  428. return; //do not damage own units at all
  429. }
  430. totalGain += (newValue - oldValue + healthDiff);
  431. }
  432. ps->value = totalGain;
  433. }
  434. else
  435. {
  436. ps->value = -1;
  437. }
  438. };
  439. std::vector<std::function<void()>> tasks;
  440. for(PossibleSpellcast & psc : possibleCasts)
  441. tasks.push_back(std::bind(evaluateSpellcast, &psc));
  442. uint32_t threadCount = boost::thread::hardware_concurrency();
  443. if(threadCount == 0)
  444. {
  445. logGlobal->warn("No information of CPU cores available");
  446. threadCount = 1;
  447. }
  448. CStopWatch timer;
  449. CThreadHelper threadHelper(&tasks, threadCount);
  450. threadHelper.run();
  451. LOGFL("Evaluation took %d ms", timer.getDiff());
  452. auto pscValue = [](const PossibleSpellcast &ps) -> int64_t
  453. {
  454. return ps.value;
  455. };
  456. auto castToPerform = *vstd::maxElementByFun(possibleCasts, pscValue);
  457. if(castToPerform.value > 0)
  458. {
  459. LOGFL("Best spell is %s (value %d). Will cast.", castToPerform.spell->name % castToPerform.value);
  460. BattleAction spellcast;
  461. spellcast.actionType = EActionType::HERO_SPELL;
  462. spellcast.actionSubtype = castToPerform.spell->id;
  463. spellcast.setTarget(castToPerform.dest);
  464. spellcast.side = side;
  465. spellcast.stackNumber = (!side) ? -1 : -2;
  466. cb->battleMakeAction(&spellcast);
  467. }
  468. else
  469. {
  470. LOGFL("Best spell is %s. But it is actually useless (value %d).", castToPerform.spell->name % castToPerform.value);
  471. }
  472. }
  473. //Below method works only for offensive spells
  474. void CBattleAI::evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps)
  475. {
  476. using ValueMap = PossibleSpellcast::ValueMap;
  477. RNGStub rngStub;
  478. HypotheticBattle state(getCbc());
  479. TStacks all = getCbc()->battleGetAllStacks(false);
  480. ValueMap healthOfStack;
  481. ValueMap newHealthOfStack;
  482. for(auto unit : all)
  483. {
  484. healthOfStack[unit->unitId()] = unit->getAvailableHealth();
  485. }
  486. spells::BattleCast cast(&state, stack, spells::Mode::CREATURE_ACTIVE, ps.spell);
  487. cast.target = ps.dest;
  488. cast.cast(&state, rngStub);
  489. for(auto unit : all)
  490. {
  491. auto unitId = unit->unitId();
  492. auto localUnit = state.battleGetUnitByID(unitId);
  493. newHealthOfStack[unitId] = localUnit->getAvailableHealth();
  494. }
  495. int64_t totalGain = 0;
  496. for(auto unit : all)
  497. {
  498. auto unitId = unit->unitId();
  499. auto localUnit = state.battleGetUnitByID(unitId);
  500. auto healthDiff = newHealthOfStack[unitId] - healthOfStack[unitId];
  501. if(localUnit->unitOwner() != getCbc()->getPlayerID())
  502. healthDiff = -healthDiff;
  503. if(healthDiff < 0)
  504. {
  505. ps.value = -1;
  506. return; //do not damage own units at all
  507. }
  508. totalGain += healthDiff;
  509. }
  510. ps.value = totalGain;
  511. };
  512. int CBattleAI::distToNearestNeighbour(BattleHex hex, const ReachabilityInfo::TDistances &dists, BattleHex *chosenHex)
  513. {
  514. int ret = 1000000;
  515. for(BattleHex n : hex.neighbouringTiles())
  516. {
  517. if(dists[n] >= 0 && dists[n] < ret)
  518. {
  519. ret = dists[n];
  520. if(chosenHex)
  521. *chosenHex = n;
  522. }
  523. }
  524. return ret;
  525. }
  526. void CBattleAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side)
  527. {
  528. LOG_TRACE(logAi);
  529. side = Side;
  530. }
  531. bool CBattleAI::isCloser(const EnemyInfo &ei1, const EnemyInfo &ei2, const ReachabilityInfo::TDistances &dists)
  532. {
  533. return distToNearestNeighbour(ei1.s->getPosition(), dists) < distToNearestNeighbour(ei2.s->getPosition(), dists);
  534. }
  535. void CBattleAI::print(const std::string &text) const
  536. {
  537. logAi->trace("%s Battle AI[%p]: %s", playerID.getStr(), this, text);
  538. }
  539. boost::optional<BattleAction> CBattleAI::considerFleeingOrSurrendering()
  540. {
  541. if(cb->battleCanSurrender(playerID))
  542. {
  543. }
  544. if(cb->battleCanFlee())
  545. {
  546. }
  547. return boost::none;
  548. }