BattleExchangeVariant.cpp 17 KB

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