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. 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. std::vector<const battle::Unit *> ourStacks;
  287. std::vector<const battle::Unit *> enemyStacks;
  288. enemyStacks.push_back(ap.attack.defender);
  289. std::vector<const battle::Unit *> exchangeUnits = getExchangeUnits(ap, targets, hb);
  290. if(exchangeUnits.empty())
  291. {
  292. return 0;
  293. }
  294. HypotheticBattle exchangeBattle(env.get(), cb);
  295. BattleExchangeVariant v;
  296. auto melleeAttackers = ourStacks;
  297. vstd::removeDuplicates(melleeAttackers);
  298. vstd::erase_if(melleeAttackers, [&](const battle::Unit * u) -> bool
  299. {
  300. return !cb->battleCanShoot(u);
  301. });
  302. for(auto unit : exchangeUnits)
  303. {
  304. bool isOur = cb->battleMatchOwner(ap.attack.attacker, unit, true);
  305. auto & attackerQueue = isOur ? ourStacks : enemyStacks;
  306. if(!vstd::contains(attackerQueue, unit))
  307. {
  308. attackerQueue.push_back(unit);
  309. }
  310. }
  311. bool canUseAp = true;
  312. for(auto activeUnit : exchangeUnits)
  313. {
  314. bool isOur = cb->battleMatchOwner(ap.attack.attacker, activeUnit, true);
  315. battle::Units & attackerQueue = isOur ? ourStacks : enemyStacks;
  316. battle::Units & oppositeQueue = isOur ? enemyStacks : ourStacks;
  317. auto attacker = exchangeBattle.getForUpdate(activeUnit->unitId());
  318. if(!attacker->alive())
  319. {
  320. #if BATTLE_TRACE_LEVEL>=1
  321. logAi->trace( "Attacker is dead");
  322. #endif
  323. continue;
  324. }
  325. auto targetUnit = ap.attack.defender;
  326. if(!isOur || !exchangeBattle.getForUpdate(targetUnit->unitId())->alive())
  327. {
  328. auto estimateAttack = [&](const battle::Unit * u) -> int64_t
  329. {
  330. auto stackWithBonuses = exchangeBattle.getForUpdate(u->unitId());
  331. auto score = v.trackAttack(
  332. attacker,
  333. stackWithBonuses,
  334. exchangeBattle.battleCanShoot(stackWithBonuses.get()),
  335. isOur,
  336. *cb,
  337. true);
  338. #if BATTLE_TRACE_LEVEL>=1
  339. logAi->trace("Best target selector %s->%s score = %lld", attacker->getDescription(), u->getDescription(), score);
  340. #endif
  341. return score;
  342. };
  343. if(!oppositeQueue.empty())
  344. {
  345. targetUnit = *vstd::maxElementByFun(oppositeQueue, estimateAttack);
  346. }
  347. else
  348. {
  349. auto reachable = exchangeBattle.battleGetUnitsIf([&](const battle::Unit * u) -> bool
  350. {
  351. if(!u->alive() || u->unitSide() == attacker->unitSide())
  352. return false;
  353. return vstd::contains_if(reachabilityMap[u->getPosition()], [&](const battle::Unit * other) -> bool
  354. {
  355. return attacker->unitId() == other->unitId();
  356. });
  357. });
  358. if(!reachable.empty())
  359. {
  360. targetUnit = *vstd::maxElementByFun(reachable, estimateAttack);
  361. }
  362. else
  363. {
  364. #if BATTLE_TRACE_LEVEL>=1
  365. logAi->trace("Battle queue is empty and no reachable enemy.");
  366. #endif
  367. continue;
  368. }
  369. }
  370. }
  371. auto defender = exchangeBattle.getForUpdate(targetUnit->unitId());
  372. auto shooting = cb->battleCanShoot(attacker.get());
  373. const int totalAttacks = attacker->getTotalAttacks(shooting);
  374. if(canUseAp && activeUnit == ap.attack.attacker && targetUnit == ap.attack.defender)
  375. {
  376. v.trackAttack(ap, exchangeBattle);
  377. }
  378. else
  379. {
  380. for(int i = 0; i < totalAttacks; i++)
  381. {
  382. v.trackAttack(attacker, defender, shooting, isOur, exchangeBattle);
  383. if(!attacker->alive() || !defender->alive())
  384. break;
  385. }
  386. }
  387. canUseAp = false;
  388. vstd::erase_if(attackerQueue, [&](const battle::Unit * u) -> bool
  389. {
  390. return !exchangeBattle.getForUpdate(u->unitId())->alive();
  391. });
  392. vstd::erase_if(oppositeQueue, [&](const battle::Unit * u) -> bool
  393. {
  394. return !exchangeBattle.getForUpdate(u->unitId())->alive();
  395. });
  396. }
  397. // avoid blocking path for stronger stack by weaker stack
  398. // the method checks if all stacks can be placed around enemy
  399. v.adjustPositions(melleeAttackers, ap, reachabilityMap);
  400. #if BATTLE_TRACE_LEVEL>=1
  401. logAi->trace("Exchange score: %lld", v.getScore());
  402. #endif
  403. return v.getScore();
  404. }
  405. void BattleExchangeVariant::adjustPositions(
  406. std::vector<const battle::Unit*> attackers,
  407. const AttackPossibility & ap,
  408. std::map<BattleHex, battle::Units> & reachabilityMap)
  409. {
  410. auto hexes = ap.attack.defender->getSurroundingHexes();
  411. boost::sort(attackers, [&](const battle::Unit * u1, const battle::Unit * u2) -> bool
  412. {
  413. if(attackerValue[u1->unitId()].isRetalitated && !attackerValue[u2->unitId()].isRetalitated)
  414. return true;
  415. if(attackerValue[u2->unitId()].isRetalitated && !attackerValue[u1->unitId()].isRetalitated)
  416. return false;
  417. return attackerValue[u1->unitId()].value > attackerValue[u2->unitId()].value;
  418. });
  419. if(!ap.attack.shooting)
  420. {
  421. vstd::erase_if_present(hexes, ap.from);
  422. vstd::erase_if_present(hexes, ap.attack.attacker->occupiedHex(ap.attack.attackerPos));
  423. }
  424. int64_t notRealizedDamage = 0;
  425. for(auto unit : attackers)
  426. {
  427. if(unit->unitId() == ap.attack.attacker->unitId())
  428. continue;
  429. if(!vstd::contains_if(hexes, [&](BattleHex h) -> bool
  430. {
  431. return vstd::contains(reachabilityMap[h], unit);
  432. }))
  433. {
  434. notRealizedDamage += attackerValue[unit->unitId()].value;
  435. continue;
  436. }
  437. auto desiredPosition = vstd::minElementByFun(hexes, [&](BattleHex h) -> int64_t
  438. {
  439. auto score = vstd::contains(reachabilityMap[h], unit)
  440. ? reachabilityMap[h].size()
  441. : 0;
  442. if(unit->doubleWide())
  443. {
  444. auto backHex = unit->occupiedHex(h);
  445. if(vstd::contains(hexes, backHex))
  446. score += reachabilityMap[backHex].size();
  447. }
  448. return score;
  449. });
  450. hexes.erase(desiredPosition);
  451. }
  452. if(notRealizedDamage > ap.attackValue() && notRealizedDamage > attackerValue[ap.attack.attacker->unitId()].value)
  453. {
  454. dpsScore = EvaluationResult::INEFFECTIVE_SCORE;
  455. }
  456. }
  457. void BattleExchangeEvaluator::updateReachabilityMap(HypotheticBattle & hb)
  458. {
  459. const int TURN_DEPTH = 2;
  460. turnOrder.clear();
  461. hb.battleGetTurnOrder(turnOrder, std::numeric_limits<int>::max(), TURN_DEPTH);
  462. reachabilityMap.clear();
  463. for(int turn = 0; turn < turnOrder.size(); turn++)
  464. {
  465. auto & turnQueue = turnOrder[turn];
  466. HypotheticBattle turnBattle(env.get(), cb);
  467. for(const battle::Unit * unit : turnQueue)
  468. {
  469. if(turnBattle.battleCanShoot(unit))
  470. {
  471. for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
  472. {
  473. reachabilityMap[hex].push_back(unit);
  474. }
  475. continue;
  476. }
  477. auto unitReachability = turnBattle.getReachability(unit);
  478. for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
  479. {
  480. bool reachable = unitReachability.distances[hex] <= unit->Speed(turn);
  481. if(!reachable && unitReachability.accessibility[hex] == EAccessibility::ALIVE_STACK)
  482. {
  483. const battle::Unit * hexStack = cb->battleGetUnitByPos(hex);
  484. if(hexStack && cb->battleMatchOwner(unit, hexStack, false))
  485. {
  486. for(BattleHex neighbor : hex.neighbouringTiles())
  487. {
  488. reachable = unitReachability.distances[neighbor] <= unit->Speed(turn);
  489. if(reachable) break;
  490. }
  491. }
  492. }
  493. if(reachable)
  494. {
  495. reachabilityMap[hex].push_back(unit);
  496. }
  497. }
  498. }
  499. }
  500. }
  501. // avoid blocking path for stronger stack by weaker stack
  502. bool BattleExchangeEvaluator::checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * activeUnit, BattleHex position)
  503. {
  504. const int BLOCKING_THRESHOLD = 70;
  505. const int BLOCKING_OWN_ATTACK_PENALTY = 100;
  506. const int BLOCKING_OWN_MOVE_PENALTY = 1;
  507. float blockingScore = 0;
  508. auto activeUnitDamage = activeUnit->getMinDamage(hb.battleCanShoot(activeUnit)) * activeUnit->getCount();
  509. for(int turn = 0; turn < turnOrder.size(); turn++)
  510. {
  511. auto & turnQueue = turnOrder[turn];
  512. HypotheticBattle turnBattle(env.get(), cb);
  513. auto unitToUpdate = turnBattle.getForUpdate(activeUnit->unitId());
  514. unitToUpdate->setPosition(position);
  515. for(const battle::Unit * unit : turnQueue)
  516. {
  517. if(unit->unitId() == unitToUpdate->unitId() || cb->battleMatchOwner(unit, activeUnit, false))
  518. continue;
  519. auto blockedUnitDamage = unit->getMinDamage(hb.battleCanShoot(unit)) * unit->getCount();
  520. auto ratio = blockedUnitDamage / (blockedUnitDamage + activeUnitDamage);
  521. auto unitReachability = turnBattle.getReachability(unit);
  522. for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
  523. {
  524. bool enemyUnit = false;
  525. bool reachable = unitReachability.distances[hex] <= unit->Speed(turn);
  526. if(!reachable && unitReachability.accessibility[hex] == EAccessibility::ALIVE_STACK)
  527. {
  528. const battle::Unit * hexStack = turnBattle.battleGetUnitByPos(hex);
  529. if(hexStack && cb->battleMatchOwner(unit, hexStack, false))
  530. {
  531. enemyUnit = true;
  532. for(BattleHex neighbor : hex.neighbouringTiles())
  533. {
  534. reachable = unitReachability.distances[neighbor] <= unit->Speed(turn);
  535. if(reachable) break;
  536. }
  537. }
  538. }
  539. if(!reachable && vstd::contains(reachabilityMap[hex], unit))
  540. {
  541. blockingScore += ratio * (enemyUnit ? BLOCKING_OWN_ATTACK_PENALTY : BLOCKING_OWN_MOVE_PENALTY);
  542. }
  543. }
  544. }
  545. }
  546. #if BATTLE_TRACE_LEVEL>=1
  547. logAi->trace("Position %d, blocking score %f", position.hex, blockingScore);
  548. #endif
  549. return blockingScore > BLOCKING_THRESHOLD;
  550. }