BattleAI.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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.size())
  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. return BattleAction::makeMeleeAttack(stack, bestAttack.attack.defender->getPosition(), bestAttack.tile);
  145. }
  146. else if(bestSpellcast.is_initialized())
  147. {
  148. return BattleAction::makeCreatureSpellcast(stack, bestSpellcast->dest, bestSpellcast->spell->id);
  149. }
  150. else
  151. {
  152. if(stack->waited())
  153. {
  154. //ThreatMap threatsToUs(stack); // These lines may be usefull but they are't used in the code.
  155. auto dists = getCbc()->battleGetDistances(stack, stack->getPosition());
  156. if(!targets.unreachableEnemies.empty())
  157. {
  158. const EnemyInfo &ei= *range::min_element(targets.unreachableEnemies, std::bind(isCloser, _1, _2, std::ref(dists)));
  159. if(distToNearestNeighbour(ei.s->getPosition(), dists) < GameConstants::BFIELD_SIZE)
  160. {
  161. return goTowards(stack, ei.s->getPosition());
  162. }
  163. }
  164. }
  165. else
  166. {
  167. return BattleAction::makeWait(stack);
  168. }
  169. }
  170. }
  171. catch(boost::thread_interrupted &)
  172. {
  173. throw;
  174. }
  175. catch(std::exception &e)
  176. {
  177. logAi->error("Exception occurred in %s %s",__FUNCTION__, e.what());
  178. }
  179. return BattleAction::makeDefend(stack);
  180. }
  181. BattleAction CBattleAI::goTowards(const CStack * stack, BattleHex destination)
  182. {
  183. if(!destination.isValid())
  184. {
  185. logAi->error("CBattleAI::goTowards: invalid destination");
  186. return BattleAction::makeDefend(stack);
  187. }
  188. auto reachability = cb->getReachability(stack);
  189. auto avHexes = cb->battleGetAvailableHexes(reachability, stack);
  190. if(vstd::contains(avHexes, destination))
  191. return BattleAction::makeMove(stack, destination);
  192. auto destNeighbours = destination.neighbouringTiles();
  193. if(vstd::contains_if(destNeighbours, [&](BattleHex n) { return stack->coversPos(destination); }))
  194. {
  195. logAi->warn("Warning: already standing on neighbouring tile!");
  196. //We shouldn't even be here...
  197. return BattleAction::makeDefend(stack);
  198. }
  199. vstd::erase_if(destNeighbours, [&](BattleHex hex){ return !reachability.accessibility.accessible(hex, stack); });
  200. if(!avHexes.size() || !destNeighbours.size()) //we are blocked or dest is blocked
  201. {
  202. return BattleAction::makeDefend(stack);
  203. }
  204. if(stack->hasBonusOfType(Bonus::FLYING))
  205. {
  206. // Flying stack doesn't go hex by hex, so we can't backtrack using predecessors.
  207. // We just check all available hexes and pick the one closest to the target.
  208. auto distToDestNeighbour = [&](BattleHex hex) -> int
  209. {
  210. auto nearestNeighbourToHex = vstd::minElementByFun(destNeighbours, [&](BattleHex a)
  211. {return BattleHex::getDistance(a, hex);});
  212. return BattleHex::getDistance(*nearestNeighbourToHex, hex);
  213. };
  214. auto nearestAvailableHex = vstd::minElementByFun(avHexes, distToDestNeighbour);
  215. return BattleAction::makeMove(stack, *nearestAvailableHex);
  216. }
  217. else
  218. {
  219. BattleHex bestNeighbor = destination;
  220. if(distToNearestNeighbour(destination, reachability.distances, &bestNeighbor) > GameConstants::BFIELD_SIZE)
  221. {
  222. return BattleAction::makeDefend(stack);
  223. }
  224. BattleHex currentDest = bestNeighbor;
  225. while(1)
  226. {
  227. if(!currentDest.isValid())
  228. {
  229. logAi->error("CBattleAI::goTowards: internal error");
  230. return BattleAction::makeDefend(stack);
  231. }
  232. if(vstd::contains(avHexes, currentDest))
  233. return BattleAction::makeMove(stack, currentDest);
  234. currentDest = reachability.predecessors[currentDest];
  235. }
  236. }
  237. }
  238. BattleAction CBattleAI::useCatapult(const CStack * stack)
  239. {
  240. throw std::runtime_error("CBattleAI::useCatapult is not implemented.");
  241. }
  242. void CBattleAI::attemptCastingSpell()
  243. {
  244. auto hero = cb->battleGetMyHero();
  245. if(!hero)
  246. return;
  247. if(cb->battleCanCastSpell(hero, spells::Mode::HERO) != ESpellCastProblem::OK)
  248. return;
  249. LOGL("Casting spells sounds like fun. Let's see...");
  250. //Get all spells we can cast
  251. std::vector<const CSpell*> possibleSpells;
  252. vstd::copy_if(VLC->spellh->objects, std::back_inserter(possibleSpells), [hero](const CSpell *s) -> bool
  253. {
  254. return s->canBeCast(getCbc().get(), spells::Mode::HERO, hero);
  255. });
  256. LOGFL("I can cast %d spells.", possibleSpells.size());
  257. vstd::erase_if(possibleSpells, [](const CSpell *s)
  258. {
  259. return spellType(s) != SpellTypes::BATTLE;
  260. });
  261. LOGFL("I know how %d of them works.", possibleSpells.size());
  262. //Get possible spell-target pairs
  263. std::vector<PossibleSpellcast> possibleCasts;
  264. for(auto spell : possibleSpells)
  265. {
  266. spells::BattleCast temp(getCbc().get(), hero, spells::Mode::HERO, spell);
  267. for(auto & target : temp.findPotentialTargets())
  268. {
  269. PossibleSpellcast ps;
  270. ps.dest = target;
  271. ps.spell = spell;
  272. possibleCasts.push_back(ps);
  273. }
  274. }
  275. LOGFL("Found %d spell-target combinations.", possibleCasts.size());
  276. if(possibleCasts.empty())
  277. return;
  278. using ValueMap = PossibleSpellcast::ValueMap;
  279. auto evaluateQueue = [&](ValueMap & values, const std::vector<battle::Units> & queue, HypotheticBattle * state, size_t minTurnSpan, bool * enemyHadTurnOut) -> bool
  280. {
  281. bool firstRound = true;
  282. bool enemyHadTurn = false;
  283. size_t ourTurnSpan = 0;
  284. bool stop = false;
  285. for(auto & round : queue)
  286. {
  287. if(!firstRound)
  288. state->nextRound(0);//todo: set actual value?
  289. for(auto unit : round)
  290. {
  291. if(!vstd::contains(values, unit->unitId()))
  292. values[unit->unitId()] = 0;
  293. if(!unit->alive())
  294. continue;
  295. if(state->battleGetOwner(unit) != playerID)
  296. {
  297. enemyHadTurn = true;
  298. if(!firstRound || state->battleCastSpells(unit->unitSide()) == 0)
  299. {
  300. //enemy could counter our spell at this point
  301. //anyway, we do not know what enemy will do
  302. //just stop evaluation
  303. stop = true;
  304. break;
  305. }
  306. }
  307. else if(!enemyHadTurn)
  308. {
  309. ourTurnSpan++;
  310. }
  311. state->nextTurn(unit->unitId());
  312. PotentialTargets pt(unit, state);
  313. if(!pt.possibleAttacks.empty())
  314. {
  315. AttackPossibility ap = pt.bestAction();
  316. auto swb = state->getForUpdate(unit->unitId());
  317. *swb = *ap.attackerState;
  318. if(ap.damageDealt > 0)
  319. swb->removeUnitBonus(Bonus::UntilAttack);
  320. if(ap.damageReceived > 0)
  321. swb->removeUnitBonus(Bonus::UntilBeingAttacked);
  322. for(auto affected : ap.affectedUnits)
  323. {
  324. swb = state->getForUpdate(affected->unitId());
  325. *swb = *affected;
  326. if(ap.damageDealt > 0)
  327. swb->removeUnitBonus(Bonus::UntilBeingAttacked);
  328. if(ap.damageReceived > 0 && ap.attack.defender->unitId() == affected->unitId())
  329. swb->removeUnitBonus(Bonus::UntilAttack);
  330. }
  331. }
  332. auto bav = pt.bestActionValue();
  333. //best action is from effective owner`s point if view, we need to convert to our point if view
  334. if(state->battleGetOwner(unit) != playerID)
  335. bav = -bav;
  336. values[unit->unitId()] += bav;
  337. }
  338. firstRound = false;
  339. if(stop)
  340. break;
  341. }
  342. if(enemyHadTurnOut)
  343. *enemyHadTurnOut = enemyHadTurn;
  344. return ourTurnSpan >= minTurnSpan;
  345. };
  346. RNGStub rngStub;
  347. ValueMap valueOfStack;
  348. ValueMap healthOfStack;
  349. TStacks all = cb->battleGetAllStacks(false);
  350. size_t ourRemainingTurns = 0;
  351. for(auto unit : all)
  352. {
  353. healthOfStack[unit->unitId()] = unit->getAvailableHealth();
  354. valueOfStack[unit->unitId()] = 0;
  355. if(cb->battleGetOwner(unit) == playerID && unit->canMove() && !unit->moved())
  356. ourRemainingTurns++;
  357. }
  358. LOGFL("I have %d turns left in this round", ourRemainingTurns);
  359. const bool castNow = ourRemainingTurns <= 1;
  360. if(castNow)
  361. print("I should try to cast a spell now");
  362. else
  363. print("I could wait better moment to cast a spell");
  364. auto amount = all.size();
  365. std::vector<battle::Units> turnOrder;
  366. cb->battleGetTurnOrder(turnOrder, amount, 2); //no more than 1 turn after current, each unit at least once
  367. {
  368. bool enemyHadTurn = false;
  369. HypotheticBattle state(cb);
  370. evaluateQueue(valueOfStack, turnOrder, &state, 0, &enemyHadTurn);
  371. if(!enemyHadTurn)
  372. {
  373. auto battleIsFinishedOpt = state.battleIsFinished();
  374. if(battleIsFinishedOpt)
  375. {
  376. print("No need to cast a spell. Battle will finish soon.");
  377. return;
  378. }
  379. }
  380. }
  381. auto evaluateSpellcast = [&] (PossibleSpellcast * ps)
  382. {
  383. HypotheticBattle state(cb);
  384. spells::BattleCast cast(&state, hero, spells::Mode::HERO, ps->spell);
  385. cast.target = ps->dest;
  386. cast.cast(&state, rngStub);
  387. ValueMap newHealthOfStack;
  388. ValueMap newValueOfStack;
  389. size_t ourUnits = 0;
  390. for(auto unit : all)
  391. {
  392. auto unitId = unit->unitId();
  393. auto localUnit = state.battleGetUnitByID(unitId);
  394. newHealthOfStack[unitId] = localUnit->getAvailableHealth();
  395. newValueOfStack[unitId] = 0;
  396. if(state.battleGetOwner(localUnit) == playerID && localUnit->alive() && localUnit->willMove())
  397. ourUnits++;
  398. }
  399. size_t minTurnSpan = ourUnits/3; //todo: tweak this
  400. std::vector<battle::Units> newTurnOrder;
  401. state.battleGetTurnOrder(newTurnOrder, amount, 2);
  402. const bool turnSpanOK = evaluateQueue(newValueOfStack, newTurnOrder, &state, minTurnSpan, nullptr);
  403. if(turnSpanOK || castNow)
  404. {
  405. int64_t totalGain = 0;
  406. for(auto unit : all)
  407. {
  408. auto unitId = unit->unitId();
  409. auto localUnit = state.battleGetUnitByID(unitId);
  410. auto newValue = getValOr(newValueOfStack, unitId, 0);
  411. auto oldValue = getValOr(valueOfStack, unitId, 0);
  412. auto healthDiff = newHealthOfStack[unitId] - healthOfStack[unitId];
  413. if(localUnit->unitOwner() != playerID)
  414. healthDiff = -healthDiff;
  415. if(healthDiff < 0)
  416. {
  417. ps->value = -1;
  418. return; //do not damage own units at all
  419. }
  420. totalGain += (newValue - oldValue + healthDiff);
  421. }
  422. ps->value = totalGain;
  423. }
  424. else
  425. {
  426. ps->value = -1;
  427. }
  428. };
  429. std::vector<std::function<void()>> tasks;
  430. for(PossibleSpellcast & psc : possibleCasts)
  431. tasks.push_back(std::bind(evaluateSpellcast, &psc));
  432. uint32_t threadCount = boost::thread::hardware_concurrency();
  433. if(threadCount == 0)
  434. {
  435. logGlobal->warn("No information of CPU cores available");
  436. threadCount = 1;
  437. }
  438. CStopWatch timer;
  439. CThreadHelper threadHelper(&tasks, threadCount);
  440. threadHelper.run();
  441. LOGFL("Evaluation took %d ms", timer.getDiff());
  442. auto pscValue = [](const PossibleSpellcast &ps) -> int64_t
  443. {
  444. return ps.value;
  445. };
  446. auto castToPerform = *vstd::maxElementByFun(possibleCasts, pscValue);
  447. if(castToPerform.value > 0)
  448. {
  449. LOGFL("Best spell is %s (value %d). Will cast.", castToPerform.spell->name % castToPerform.value);
  450. BattleAction spellcast;
  451. spellcast.actionType = EActionType::HERO_SPELL;
  452. spellcast.actionSubtype = castToPerform.spell->id;
  453. spellcast.setTarget(castToPerform.dest);
  454. spellcast.side = side;
  455. spellcast.stackNumber = (!side) ? -1 : -2;
  456. cb->battleMakeAction(&spellcast);
  457. }
  458. else
  459. {
  460. LOGFL("Best spell is %s. But it is actually useless (value %d).", castToPerform.spell->name % castToPerform.value);
  461. }
  462. }
  463. //Below method works only for offensive spells
  464. void CBattleAI::evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps)
  465. {
  466. using ValueMap = PossibleSpellcast::ValueMap;
  467. RNGStub rngStub;
  468. HypotheticBattle state(getCbc());
  469. TStacks all = getCbc()->battleGetAllStacks(false);
  470. ValueMap healthOfStack;
  471. ValueMap newHealthOfStack;
  472. for(auto unit : all)
  473. {
  474. healthOfStack[unit->unitId()] = unit->getAvailableHealth();
  475. }
  476. spells::BattleCast cast(&state, stack, spells::Mode::CREATURE_ACTIVE, ps.spell);
  477. cast.target = ps.dest;
  478. cast.cast(&state, rngStub);
  479. for(auto unit : all)
  480. {
  481. auto unitId = unit->unitId();
  482. auto localUnit = state.battleGetUnitByID(unitId);
  483. newHealthOfStack[unitId] = localUnit->getAvailableHealth();
  484. }
  485. int64_t totalGain = 0;
  486. for(auto unit : all)
  487. {
  488. auto unitId = unit->unitId();
  489. auto localUnit = state.battleGetUnitByID(unitId);
  490. auto healthDiff = newHealthOfStack[unitId] - healthOfStack[unitId];
  491. if(localUnit->unitOwner() != getCbc()->getPlayerID())
  492. healthDiff = -healthDiff;
  493. if(healthDiff < 0)
  494. {
  495. ps.value = -1;
  496. return; //do not damage own units at all
  497. }
  498. totalGain += healthDiff;
  499. }
  500. ps.value = totalGain;
  501. };
  502. int CBattleAI::distToNearestNeighbour(BattleHex hex, const ReachabilityInfo::TDistances &dists, BattleHex *chosenHex)
  503. {
  504. int ret = 1000000;
  505. for(BattleHex n : hex.neighbouringTiles())
  506. {
  507. if(dists[n] >= 0 && dists[n] < ret)
  508. {
  509. ret = dists[n];
  510. if(chosenHex)
  511. *chosenHex = n;
  512. }
  513. }
  514. return ret;
  515. }
  516. void CBattleAI::battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool Side)
  517. {
  518. LOG_TRACE(logAi);
  519. side = Side;
  520. }
  521. bool CBattleAI::isCloser(const EnemyInfo &ei1, const EnemyInfo &ei2, const ReachabilityInfo::TDistances &dists)
  522. {
  523. return distToNearestNeighbour(ei1.s->getPosition(), dists) < distToNearestNeighbour(ei2.s->getPosition(), dists);
  524. }
  525. void CBattleAI::print(const std::string &text) const
  526. {
  527. logAi->trace("%s Battle AI[%p]: %s", playerID.getStr(), this, text);
  528. }
  529. boost::optional<BattleAction> CBattleAI::considerFleeingOrSurrendering()
  530. {
  531. if(cb->battleCanSurrender(playerID))
  532. {
  533. }
  534. if(cb->battleCanFlee())
  535. {
  536. }
  537. return boost::none;
  538. }