BattleExchangeVariant.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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 "BattleExchangeVariant.h"
  12. #include "../../lib/CStack.h"
  13. AttackerValue::AttackerValue()
  14. {
  15. value = 0;
  16. isRetalitated = false;
  17. }
  18. MoveTarget::MoveTarget()
  19. : positions()
  20. {
  21. score = EvaluationResult::INEFFECTIVE_SCORE;
  22. }
  23. int64_t BattleExchangeVariant::trackAttack(const AttackPossibility & ap, HypotheticBattle & state)
  24. {
  25. auto affectedUnits = ap.affectedUnits;
  26. affectedUnits.push_back(ap.attackerState);
  27. for(auto affectedUnit : affectedUnits)
  28. {
  29. auto unitToUpdate = state.getForUpdate(affectedUnit->unitId());
  30. unitToUpdate->health = affectedUnit->health;
  31. unitToUpdate->shots = affectedUnit->shots;
  32. unitToUpdate->counterAttacks = affectedUnit->counterAttacks;
  33. unitToUpdate->movedThisRound = affectedUnit->movedThisRound;
  34. }
  35. auto attackValue = ap.attackValue();
  36. dpsScore += attackValue;
  37. #if BATTLE_TRACE_LEVEL>=1
  38. logAi->trace(
  39. "%s -> %s, ap attack, %s, dps: %lld, score: %lld",
  40. ap.attack.attacker->getDescription(),
  41. ap.attack.defender->getDescription(),
  42. ap.attack.shooting ? "shot" : "mellee",
  43. ap.damageDealt,
  44. attackValue);
  45. #endif
  46. return attackValue;
  47. }
  48. int64_t BattleExchangeVariant::trackAttack(
  49. std::shared_ptr<StackWithBonuses> attacker,
  50. std::shared_ptr<StackWithBonuses> defender,
  51. bool shooting,
  52. bool isOurAttack,
  53. const CBattleInfoCallback & cb,
  54. bool evaluateOnly)
  55. {
  56. const std::string cachingStringBlocksRetaliation = "type_BLOCKS_RETALIATION";
  57. static const auto selectorBlocksRetaliation = Selector::type()(BonusType::BLOCKS_RETALIATION);
  58. const bool counterAttacksBlocked = attacker->hasBonus(selectorBlocksRetaliation, cachingStringBlocksRetaliation);
  59. DamageEstimation retaliation;
  60. // FIXME: provide distance info for Jousting bonus
  61. BattleAttackInfo bai(attacker.get(), defender.get(), 0, shooting);
  62. if(shooting)
  63. {
  64. bai.attackerPos.setXY(8, 5);
  65. }
  66. auto attack = cb.battleEstimateDamage(bai, &retaliation);
  67. int64_t attackDamage = (attack.damage.min + attack.damage.max) / 2;
  68. int64_t defenderDamageReduce = AttackPossibility::calculateDamageReduce(attacker.get(), defender.get(), attackDamage, cb);
  69. int64_t attackerDamageReduce = 0;
  70. if(!evaluateOnly)
  71. {
  72. #if BATTLE_TRACE_LEVEL>=1
  73. logAi->trace(
  74. "%s -> %s, normal attack, %s, dps: %lld, %lld",
  75. attacker->getDescription(),
  76. defender->getDescription(),
  77. shooting ? "shot" : "mellee",
  78. attackDamage,
  79. defenderDamageReduce);
  80. #endif
  81. if(isOurAttack)
  82. {
  83. dpsScore += defenderDamageReduce;
  84. attackerValue[attacker->unitId()].value += defenderDamageReduce;
  85. }
  86. else
  87. dpsScore -= defenderDamageReduce;
  88. defender->damage(attackDamage);
  89. attacker->afterAttack(shooting, false);
  90. }
  91. if(defender->alive() && defender->ableToRetaliate() && !counterAttacksBlocked && !shooting)
  92. {
  93. if(retaliation.damage.max != 0)
  94. {
  95. auto retaliationDamage = (retaliation.damage.min + retaliation.damage.max) / 2;
  96. attackerDamageReduce = AttackPossibility::calculateDamageReduce(defender.get(), attacker.get(), retaliationDamage, cb);
  97. if(!evaluateOnly)
  98. {
  99. #if BATTLE_TRACE_LEVEL>=1
  100. logAi->trace(
  101. "%s -> %s, retaliation, dps: %lld, %lld",
  102. defender->getDescription(),
  103. attacker->getDescription(),
  104. retaliationDamage,
  105. attackerDamageReduce);
  106. #endif
  107. if(isOurAttack)
  108. {
  109. dpsScore -= attackerDamageReduce;
  110. attackerValue[attacker->unitId()].isRetalitated = true;
  111. }
  112. else
  113. {
  114. dpsScore += attackerDamageReduce;
  115. attackerValue[defender->unitId()].value += attackerDamageReduce;
  116. }
  117. attacker->damage(retaliationDamage);
  118. defender->afterAttack(false, true);
  119. }
  120. }
  121. }
  122. auto score = defenderDamageReduce - attackerDamageReduce;
  123. #if BATTLE_TRACE_LEVEL>=1
  124. if(!score)
  125. {
  126. logAi->trace("Attack has zero score d:%lld a:%lld", defenderDamageReduce, attackerDamageReduce);
  127. }
  128. #endif
  129. return score;
  130. }
  131. EvaluationResult BattleExchangeEvaluator::findBestTarget(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb)
  132. {
  133. EvaluationResult result(targets.bestAction());
  134. updateReachabilityMap(hb);
  135. for(auto & ap : targets.possibleAttacks)
  136. {
  137. int64_t score = calculateExchange(ap, targets, hb);
  138. if(score > result.score)
  139. {
  140. result.score = score;
  141. result.bestAttack = ap;
  142. }
  143. }
  144. if(!activeStack->waited())
  145. {
  146. #if BATTLE_TRACE_LEVEL>=1
  147. logAi->trace("Evaluating waited attack for %s", activeStack->getDescription());
  148. #endif
  149. hb.getForUpdate(activeStack->unitId())->waiting = true;
  150. hb.getForUpdate(activeStack->unitId())->waitedThisTurn = true;
  151. updateReachabilityMap(hb);
  152. for(auto & ap : targets.possibleAttacks)
  153. {
  154. int64_t score = calculateExchange(ap, targets, hb);
  155. if(score > result.score)
  156. {
  157. result.score = score;
  158. result.bestAttack = ap;
  159. result.wait = true;
  160. }
  161. }
  162. }
  163. return result;
  164. }
  165. MoveTarget BattleExchangeEvaluator::findMoveTowardsUnreachable(const battle::Unit * activeStack, PotentialTargets & targets, HypotheticBattle & hb)
  166. {
  167. MoveTarget result;
  168. BattleExchangeVariant ev;
  169. if(targets.unreachableEnemies.empty())
  170. return result;
  171. auto speed = activeStack->speed();
  172. if(speed == 0)
  173. return result;
  174. updateReachabilityMap(hb);
  175. auto dists = cb->getReachability(activeStack);
  176. for(const battle::Unit * enemy : targets.unreachableEnemies)
  177. {
  178. std::vector<const battle::Unit *> adjacentStacks = getAdjacentUnits(enemy);
  179. auto closestStack = *vstd::minElementByFun(adjacentStacks, [&](const battle::Unit * u) -> int64_t
  180. {
  181. return dists.distToNearestNeighbour(activeStack, u) * 100000 - activeStack->getTotalHealth();
  182. });
  183. auto distance = dists.distToNearestNeighbour(activeStack, closestStack);
  184. if(distance >= GameConstants::BFIELD_SIZE)
  185. continue;
  186. if(distance <= speed)
  187. continue;
  188. auto turnsToRich = (distance - 1) / speed + 1;
  189. auto hexes = closestStack->getSurroundingHexes();
  190. for(auto hex : hexes)
  191. {
  192. // FIXME: provide distance info for Jousting bonus
  193. auto bai = BattleAttackInfo(activeStack, closestStack, 0, cb->battleCanShoot(activeStack));
  194. auto attack = AttackPossibility::evaluate(bai, hex, hb);
  195. attack.shootersBlockedDmg = 0; // we do not want to count on it, it is not for sure
  196. auto score = calculateExchange(attack, targets, hb) / turnsToRich;
  197. if(result.score < score)
  198. {
  199. result.score = score;
  200. result.positions = closestStack->getAttackableHexes(activeStack);
  201. }
  202. }
  203. }
  204. return result;
  205. }
  206. std::vector<const battle::Unit *> BattleExchangeEvaluator::getAdjacentUnits(const battle::Unit * blockerUnit)
  207. {
  208. std::queue<const battle::Unit *> queue;
  209. std::vector<const battle::Unit *> checkedStacks;
  210. queue.push(blockerUnit);
  211. while(!queue.empty())
  212. {
  213. auto stack = queue.front();
  214. queue.pop();
  215. checkedStacks.push_back(stack);
  216. auto hexes = stack->getSurroundingHexes();
  217. for(auto hex : hexes)
  218. {
  219. auto neighbor = cb->battleGetUnitByPos(hex);
  220. if(neighbor && neighbor->unitSide() == stack->unitSide() && !vstd::contains(checkedStacks, neighbor))
  221. {
  222. queue.push(neighbor);
  223. checkedStacks.push_back(neighbor);
  224. }
  225. }
  226. }
  227. return checkedStacks;
  228. }
  229. std::vector<const battle::Unit *> BattleExchangeEvaluator::getExchangeUnits(
  230. const AttackPossibility & ap,
  231. PotentialTargets & targets,
  232. HypotheticBattle & hb)
  233. {
  234. auto hexes = ap.attack.defender->getHexes();
  235. if(!ap.attack.shooting) hexes.push_back(ap.from);
  236. std::vector<const battle::Unit *> exchangeUnits;
  237. std::vector<const battle::Unit *> allReachableUnits;
  238. for(auto hex : hexes)
  239. {
  240. vstd::concatenate(allReachableUnits, reachabilityMap[hex]);
  241. }
  242. vstd::removeDuplicates(allReachableUnits);
  243. auto copy = allReachableUnits;
  244. for(auto unit : copy)
  245. {
  246. for(auto adjacentUnit : getAdjacentUnits(unit))
  247. {
  248. auto unitWithBonuses = hb.battleGetUnitByID(adjacentUnit->unitId());
  249. if(vstd::contains(targets.unreachableEnemies, adjacentUnit)
  250. && !vstd::contains(allReachableUnits, unitWithBonuses))
  251. {
  252. allReachableUnits.push_back(unitWithBonuses);
  253. }
  254. }
  255. }
  256. vstd::removeDuplicates(allReachableUnits);
  257. if(!vstd::contains(allReachableUnits, ap.attack.attacker))
  258. {
  259. allReachableUnits.push_back(ap.attack.attacker);
  260. }
  261. if(allReachableUnits.size() < 2)
  262. {
  263. #if BATTLE_TRACE_LEVEL>=1
  264. logAi->trace("Reachability map contains only %d stacks", allReachableUnits.size());
  265. #endif
  266. return exchangeUnits;
  267. }
  268. for(int turn = 0; turn < turnOrder.size(); turn++)
  269. {
  270. for(auto unit : turnOrder[turn])
  271. {
  272. if(vstd::contains(allReachableUnits, unit))
  273. exchangeUnits.push_back(unit);
  274. }
  275. }
  276. return exchangeUnits;
  277. }
  278. int64_t BattleExchangeEvaluator::calculateExchange(
  279. const AttackPossibility & ap,
  280. PotentialTargets & targets,
  281. HypotheticBattle & hb)
  282. {
  283. #if BATTLE_TRACE_LEVEL>=1
  284. logAi->trace("Battle exchange at %lld", ap.attack.shooting ? ap.dest : ap.from);
  285. #endif
  286. if(cb->battleGetMySide() == BattlePerspective::LEFT_SIDE
  287. && cb->battleGetGateState() == EGateState::BLOCKED
  288. && ap.attack.defender->coversPos(ESiegeHex::GATE_BRIDGE))
  289. {
  290. return EvaluationResult::INEFFECTIVE_SCORE;
  291. }
  292. std::vector<const battle::Unit *> ourStacks;
  293. std::vector<const battle::Unit *> enemyStacks;
  294. enemyStacks.push_back(ap.attack.defender);
  295. std::vector<const battle::Unit *> exchangeUnits = getExchangeUnits(ap, targets, hb);
  296. if(exchangeUnits.empty())
  297. {
  298. return 0;
  299. }
  300. HypotheticBattle exchangeBattle(env.get(), cb);
  301. BattleExchangeVariant v;
  302. auto melleeAttackers = ourStacks;
  303. vstd::removeDuplicates(melleeAttackers);
  304. vstd::erase_if(melleeAttackers, [&](const battle::Unit * u) -> bool
  305. {
  306. return !cb->battleCanShoot(u);
  307. });
  308. for(auto unit : exchangeUnits)
  309. {
  310. bool isOur = cb->battleMatchOwner(ap.attack.attacker, unit, true);
  311. auto & attackerQueue = isOur ? ourStacks : enemyStacks;
  312. if(!vstd::contains(attackerQueue, unit))
  313. {
  314. attackerQueue.push_back(unit);
  315. }
  316. }
  317. bool canUseAp = true;
  318. for(auto activeUnit : exchangeUnits)
  319. {
  320. bool isOur = cb->battleMatchOwner(ap.attack.attacker, activeUnit, true);
  321. battle::Units & attackerQueue = isOur ? ourStacks : enemyStacks;
  322. battle::Units & oppositeQueue = isOur ? enemyStacks : ourStacks;
  323. auto attacker = exchangeBattle.getForUpdate(activeUnit->unitId());
  324. if(!attacker->alive())
  325. {
  326. #if BATTLE_TRACE_LEVEL>=1
  327. logAi->trace( "Attacker is dead");
  328. #endif
  329. continue;
  330. }
  331. auto targetUnit = ap.attack.defender;
  332. if(!isOur || !exchangeBattle.getForUpdate(targetUnit->unitId())->alive())
  333. {
  334. auto estimateAttack = [&](const battle::Unit * u) -> int64_t
  335. {
  336. auto stackWithBonuses = exchangeBattle.getForUpdate(u->unitId());
  337. auto score = v.trackAttack(
  338. attacker,
  339. stackWithBonuses,
  340. exchangeBattle.battleCanShoot(stackWithBonuses.get()),
  341. isOur,
  342. *cb,
  343. true);
  344. #if BATTLE_TRACE_LEVEL>=1
  345. logAi->trace("Best target selector %s->%s score = %lld", attacker->getDescription(), u->getDescription(), score);
  346. #endif
  347. return score;
  348. };
  349. if(!oppositeQueue.empty())
  350. {
  351. targetUnit = *vstd::maxElementByFun(oppositeQueue, estimateAttack);
  352. }
  353. else
  354. {
  355. auto reachable = exchangeBattle.battleGetUnitsIf([&](const battle::Unit * u) -> bool
  356. {
  357. if(!u->alive() || u->unitSide() == attacker->unitSide())
  358. return false;
  359. return vstd::contains_if(reachabilityMap[u->getPosition()], [&](const battle::Unit * other) -> bool
  360. {
  361. return attacker->unitId() == other->unitId();
  362. });
  363. });
  364. if(!reachable.empty())
  365. {
  366. targetUnit = *vstd::maxElementByFun(reachable, estimateAttack);
  367. }
  368. else
  369. {
  370. #if BATTLE_TRACE_LEVEL>=1
  371. logAi->trace("Battle queue is empty and no reachable enemy.");
  372. #endif
  373. continue;
  374. }
  375. }
  376. }
  377. auto defender = exchangeBattle.getForUpdate(targetUnit->unitId());
  378. auto shooting = cb->battleCanShoot(attacker.get());
  379. const int totalAttacks = attacker->getTotalAttacks(shooting);
  380. if(canUseAp && activeUnit == ap.attack.attacker && targetUnit == ap.attack.defender)
  381. {
  382. v.trackAttack(ap, exchangeBattle);
  383. }
  384. else
  385. {
  386. for(int i = 0; i < totalAttacks; i++)
  387. {
  388. v.trackAttack(attacker, defender, shooting, isOur, exchangeBattle);
  389. if(!attacker->alive() || !defender->alive())
  390. break;
  391. }
  392. }
  393. canUseAp = false;
  394. vstd::erase_if(attackerQueue, [&](const battle::Unit * u) -> bool
  395. {
  396. return !exchangeBattle.getForUpdate(u->unitId())->alive();
  397. });
  398. vstd::erase_if(oppositeQueue, [&](const battle::Unit * u) -> bool
  399. {
  400. return !exchangeBattle.getForUpdate(u->unitId())->alive();
  401. });
  402. }
  403. // avoid blocking path for stronger stack by weaker stack
  404. // the method checks if all stacks can be placed around enemy
  405. v.adjustPositions(melleeAttackers, ap, reachabilityMap);
  406. #if BATTLE_TRACE_LEVEL>=1
  407. logAi->trace("Exchange score: %lld", v.getScore());
  408. #endif
  409. return v.getScore();
  410. }
  411. void BattleExchangeVariant::adjustPositions(
  412. std::vector<const battle::Unit*> attackers,
  413. const AttackPossibility & ap,
  414. std::map<BattleHex, battle::Units> & reachabilityMap)
  415. {
  416. auto hexes = ap.attack.defender->getSurroundingHexes();
  417. boost::sort(attackers, [&](const battle::Unit * u1, const battle::Unit * u2) -> bool
  418. {
  419. if(attackerValue[u1->unitId()].isRetalitated && !attackerValue[u2->unitId()].isRetalitated)
  420. return true;
  421. if(attackerValue[u2->unitId()].isRetalitated && !attackerValue[u1->unitId()].isRetalitated)
  422. return false;
  423. return attackerValue[u1->unitId()].value > attackerValue[u2->unitId()].value;
  424. });
  425. if(!ap.attack.shooting)
  426. {
  427. vstd::erase_if_present(hexes, ap.from);
  428. vstd::erase_if_present(hexes, ap.attack.attacker->occupiedHex(ap.attack.attackerPos));
  429. }
  430. int64_t notRealizedDamage = 0;
  431. for(auto unit : attackers)
  432. {
  433. if(unit->unitId() == ap.attack.attacker->unitId())
  434. continue;
  435. if(!vstd::contains_if(hexes, [&](BattleHex h) -> bool
  436. {
  437. return vstd::contains(reachabilityMap[h], unit);
  438. }))
  439. {
  440. notRealizedDamage += attackerValue[unit->unitId()].value;
  441. continue;
  442. }
  443. auto desiredPosition = vstd::minElementByFun(hexes, [&](BattleHex h) -> int64_t
  444. {
  445. auto score = vstd::contains(reachabilityMap[h], unit)
  446. ? reachabilityMap[h].size()
  447. : 0;
  448. if(unit->doubleWide())
  449. {
  450. auto backHex = unit->occupiedHex(h);
  451. if(vstd::contains(hexes, backHex))
  452. score += reachabilityMap[backHex].size();
  453. }
  454. return score;
  455. });
  456. hexes.erase(desiredPosition);
  457. }
  458. if(notRealizedDamage > ap.attackValue() && notRealizedDamage > attackerValue[ap.attack.attacker->unitId()].value)
  459. {
  460. dpsScore = EvaluationResult::INEFFECTIVE_SCORE;
  461. }
  462. }
  463. void BattleExchangeEvaluator::updateReachabilityMap(HypotheticBattle & hb)
  464. {
  465. const int TURN_DEPTH = 2;
  466. turnOrder.clear();
  467. hb.battleGetTurnOrder(turnOrder, std::numeric_limits<int>::max(), TURN_DEPTH);
  468. reachabilityMap.clear();
  469. for(int turn = 0; turn < turnOrder.size(); turn++)
  470. {
  471. auto & turnQueue = turnOrder[turn];
  472. HypotheticBattle turnBattle(env.get(), cb);
  473. for(const battle::Unit * unit : turnQueue)
  474. {
  475. if(turnBattle.battleCanShoot(unit))
  476. {
  477. for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
  478. {
  479. reachabilityMap[hex].push_back(unit);
  480. }
  481. continue;
  482. }
  483. auto unitReachability = turnBattle.getReachability(unit);
  484. for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
  485. {
  486. bool reachable = unitReachability.distances[hex] <= unit->speed(turn);
  487. if(!reachable && unitReachability.accessibility[hex] == EAccessibility::ALIVE_STACK)
  488. {
  489. const battle::Unit * hexStack = cb->battleGetUnitByPos(hex);
  490. if(hexStack && cb->battleMatchOwner(unit, hexStack, false))
  491. {
  492. for(BattleHex neighbor : hex.neighbouringTiles())
  493. {
  494. reachable = unitReachability.distances[neighbor] <= unit->speed(turn);
  495. if(reachable) break;
  496. }
  497. }
  498. }
  499. if(reachable)
  500. {
  501. reachabilityMap[hex].push_back(unit);
  502. }
  503. }
  504. }
  505. }
  506. }
  507. // avoid blocking path for stronger stack by weaker stack
  508. bool BattleExchangeEvaluator::checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * activeUnit, BattleHex position)
  509. {
  510. const int BLOCKING_THRESHOLD = 70;
  511. const int BLOCKING_OWN_ATTACK_PENALTY = 100;
  512. const int BLOCKING_OWN_MOVE_PENALTY = 1;
  513. float blockingScore = 0;
  514. auto activeUnitDamage = activeUnit->getMinDamage(hb.battleCanShoot(activeUnit)) * activeUnit->getCount();
  515. for(int turn = 0; turn < turnOrder.size(); turn++)
  516. {
  517. auto & turnQueue = turnOrder[turn];
  518. HypotheticBattle turnBattle(env.get(), cb);
  519. auto unitToUpdate = turnBattle.getForUpdate(activeUnit->unitId());
  520. unitToUpdate->setPosition(position);
  521. for(const battle::Unit * unit : turnQueue)
  522. {
  523. if(unit->unitId() == unitToUpdate->unitId() || cb->battleMatchOwner(unit, activeUnit, false))
  524. continue;
  525. auto blockedUnitDamage = unit->getMinDamage(hb.battleCanShoot(unit)) * unit->getCount();
  526. auto ratio = blockedUnitDamage / (blockedUnitDamage + activeUnitDamage);
  527. auto unitReachability = turnBattle.getReachability(unit);
  528. for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
  529. {
  530. bool enemyUnit = false;
  531. bool reachable = unitReachability.distances[hex] <= unit->speed(turn);
  532. if(!reachable && unitReachability.accessibility[hex] == EAccessibility::ALIVE_STACK)
  533. {
  534. const battle::Unit * hexStack = turnBattle.battleGetUnitByPos(hex);
  535. if(hexStack && cb->battleMatchOwner(unit, hexStack, false))
  536. {
  537. enemyUnit = true;
  538. for(BattleHex neighbor : hex.neighbouringTiles())
  539. {
  540. reachable = unitReachability.distances[neighbor] <= unit->speed(turn);
  541. if(reachable) break;
  542. }
  543. }
  544. }
  545. if(!reachable && vstd::contains(reachabilityMap[hex], unit))
  546. {
  547. blockingScore += ratio * (enemyUnit ? BLOCKING_OWN_ATTACK_PENALTY : BLOCKING_OWN_MOVE_PENALTY);
  548. }
  549. }
  550. }
  551. }
  552. #if BATTLE_TRACE_LEVEL>=1
  553. logAi->trace("Position %d, blocking score %f", position.hex, blockingScore);
  554. #endif
  555. return blockingScore > BLOCKING_THRESHOLD;
  556. }