BattleExchangeVariant.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. std::vector<const battle::Unit *> adjacentStacks = getAdjacentUnits(enemy);
  178. auto closestStack = *vstd::minElementByFun(adjacentStacks, [&](const battle::Unit * u) -> int64_t
  179. {
  180. return dists.distToNearestNeighbour(activeStack, u) * 100000 - activeStack->getTotalHealth();
  181. });
  182. auto distance = dists.distToNearestNeighbour(activeStack, closestStack);
  183. if(distance >= GameConstants::BFIELD_SIZE)
  184. continue;
  185. if(distance <= speed)
  186. continue;
  187. auto turnsToRich = (distance - 1) / speed + 1;
  188. auto hexes = closestStack->getSurroundingHexes();
  189. for(auto hex : hexes)
  190. {
  191. auto bai = BattleAttackInfo(activeStack, closestStack, cb->battleCanShoot(activeStack));
  192. auto attack = AttackPossibility::evaluate(bai, hex, hb);
  193. attack.shootersBlockedDmg = 0; // we do not want to count on it, it is not for sure
  194. auto score = calculateExchange(attack, targets, hb) / turnsToRich;
  195. if(result.score < score)
  196. {
  197. result.score = score;
  198. result.positions = closestStack->getAttackableHexes(activeStack);
  199. }
  200. }
  201. }
  202. return result;
  203. }
  204. std::vector<const battle::Unit *> BattleExchangeEvaluator::getAdjacentUnits(const battle::Unit * blockerUnit)
  205. {
  206. std::queue<const battle::Unit *> queue;
  207. std::vector<const battle::Unit *> checkedStacks;
  208. queue.push(blockerUnit);
  209. while(!queue.empty())
  210. {
  211. auto stack = queue.front();
  212. queue.pop();
  213. checkedStacks.push_back(stack);
  214. auto hexes = stack->getSurroundingHexes();
  215. for(auto hex : hexes)
  216. {
  217. auto neighbor = cb->battleGetStackByPos(hex);
  218. if(neighbor && neighbor->unitSide() == stack->unitSide() && !vstd::contains(checkedStacks, neighbor))
  219. {
  220. queue.push(neighbor);
  221. checkedStacks.push_back(neighbor);
  222. }
  223. }
  224. }
  225. return checkedStacks;
  226. }
  227. std::vector<const battle::Unit *> BattleExchangeEvaluator::getExchangeUnits(
  228. const AttackPossibility & ap,
  229. PotentialTargets & targets,
  230. HypotheticBattle & hb)
  231. {
  232. auto hexes = ap.attack.defender->getHexes();
  233. if(!ap.attack.shooting) hexes.push_back(ap.from);
  234. std::vector<const battle::Unit *> exchangeUnits;
  235. std::vector<const battle::Unit *> allReachableUnits;
  236. for(auto hex : hexes)
  237. {
  238. vstd::concatenate(allReachableUnits, reachabilityMap[hex]);
  239. }
  240. vstd::removeDuplicates(allReachableUnits);
  241. auto copy = allReachableUnits;
  242. for(auto unit : copy)
  243. {
  244. for(auto adjacentUnit : getAdjacentUnits(unit))
  245. {
  246. auto unitWithBonuses = hb.battleGetUnitByID(adjacentUnit->unitId());
  247. if(vstd::contains(targets.unreachableEnemies, adjacentUnit)
  248. && !vstd::contains(allReachableUnits, unitWithBonuses))
  249. {
  250. allReachableUnits.push_back(unitWithBonuses);
  251. }
  252. }
  253. }
  254. vstd::removeDuplicates(allReachableUnits);
  255. if(!vstd::contains(allReachableUnits, ap.attack.attacker))
  256. {
  257. allReachableUnits.push_back(ap.attack.attacker);
  258. }
  259. if(allReachableUnits.size() < 2)
  260. {
  261. #if BATTLE_TRACE_LEVEL>=1
  262. logAi->trace("Reachability map contains only %d stacks", allReachableUnits.size());
  263. #endif
  264. return exchangeUnits;
  265. }
  266. for(int turn = 0; turn < turnOrder.size(); turn++)
  267. {
  268. for(auto unit : turnOrder[turn])
  269. {
  270. if(vstd::contains(allReachableUnits, unit))
  271. exchangeUnits.push_back(unit);
  272. }
  273. }
  274. return exchangeUnits;
  275. }
  276. int64_t BattleExchangeEvaluator::calculateExchange(
  277. const AttackPossibility & ap,
  278. PotentialTargets & targets,
  279. HypotheticBattle & hb)
  280. {
  281. #if BATTLE_TRACE_LEVEL>=1
  282. logAi->trace("Battle exchange at %lld", ap.attack.shooting ? ap.dest : ap.from);
  283. #endif
  284. std::vector<const battle::Unit *> ourStacks;
  285. std::vector<const battle::Unit *> enemyStacks;
  286. enemyStacks.push_back(ap.attack.defender);
  287. std::vector<const battle::Unit *> exchangeUnits = getExchangeUnits(ap, targets, hb);
  288. if(exchangeUnits.empty())
  289. {
  290. return 0;
  291. }
  292. HypotheticBattle exchangeBattle(env.get(), cb);
  293. BattleExchangeVariant v;
  294. auto melleeAttackers = ourStacks;
  295. vstd::removeDuplicates(melleeAttackers);
  296. vstd::erase_if(melleeAttackers, [&](const battle::Unit * u) -> bool
  297. {
  298. return !cb->battleCanShoot(u);
  299. });
  300. for(auto unit : exchangeUnits)
  301. {
  302. bool isOur = cb->battleMatchOwner(ap.attack.attacker, unit, true);
  303. auto & attackerQueue = isOur ? ourStacks : enemyStacks;
  304. if(!vstd::contains(attackerQueue, unit))
  305. {
  306. attackerQueue.push_back(unit);
  307. }
  308. }
  309. bool canUseAp = true;
  310. for(auto activeUnit : exchangeUnits)
  311. {
  312. bool isOur = cb->battleMatchOwner(ap.attack.attacker, activeUnit, true);
  313. battle::Units & attackerQueue = isOur ? ourStacks : enemyStacks;
  314. battle::Units & oppositeQueue = isOur ? enemyStacks : ourStacks;
  315. auto attacker = exchangeBattle.getForUpdate(activeUnit->unitId());
  316. if(!attacker->alive())
  317. {
  318. #if BATTLE_TRACE_LEVEL>=1
  319. logAi->trace( "Attacker is dead");
  320. #endif
  321. continue;
  322. }
  323. auto targetUnit = ap.attack.defender;
  324. if(!isOur || !exchangeBattle.getForUpdate(targetUnit->unitId())->alive())
  325. {
  326. auto estimateAttack = [&](const battle::Unit * u) -> int64_t
  327. {
  328. auto stackWithBonuses = exchangeBattle.getForUpdate(u->unitId());
  329. auto score = v.trackAttack(
  330. attacker,
  331. stackWithBonuses,
  332. exchangeBattle.battleCanShoot(stackWithBonuses.get()),
  333. isOur,
  334. *cb,
  335. true);
  336. #if BATTLE_TRACE_LEVEL>=1
  337. logAi->trace("Best target selector %s->%s score = %lld", attacker->getDescription(), u->getDescription(), score);
  338. #endif
  339. return score;
  340. };
  341. if(!oppositeQueue.empty())
  342. {
  343. targetUnit = *vstd::maxElementByFun(oppositeQueue, estimateAttack);
  344. }
  345. else
  346. {
  347. auto reachable = exchangeBattle.battleGetUnitsIf([&](const battle::Unit * u) -> bool
  348. {
  349. if(!u->alive() || u->unitSide() == attacker->unitSide())
  350. return false;
  351. return vstd::contains_if(reachabilityMap[u->getPosition()], [&](const battle::Unit * other) -> bool
  352. {
  353. return attacker->unitId() == other->unitId();
  354. });
  355. });
  356. if(!reachable.empty())
  357. {
  358. targetUnit = *vstd::maxElementByFun(reachable, estimateAttack);
  359. }
  360. else
  361. {
  362. #if BATTLE_TRACE_LEVEL>=1
  363. logAi->trace("Battle queue is empty and no reachable enemy.");
  364. #endif
  365. continue;
  366. }
  367. }
  368. }
  369. auto defender = exchangeBattle.getForUpdate(targetUnit->unitId());
  370. auto shooting = cb->battleCanShoot(attacker.get());
  371. const int totalAttacks = attacker->getTotalAttacks(shooting);
  372. if(canUseAp && activeUnit == ap.attack.attacker && targetUnit == ap.attack.defender)
  373. {
  374. v.trackAttack(ap, exchangeBattle);
  375. }
  376. else
  377. {
  378. for(int i = 0; i < totalAttacks; i++)
  379. {
  380. v.trackAttack(attacker, defender, shooting, isOur, exchangeBattle);
  381. if(!attacker->alive() || !defender->alive())
  382. break;
  383. }
  384. }
  385. canUseAp = false;
  386. vstd::erase_if(attackerQueue, [&](const battle::Unit * u) -> bool
  387. {
  388. return !exchangeBattle.getForUpdate(u->unitId())->alive();
  389. });
  390. vstd::erase_if(oppositeQueue, [&](const battle::Unit * u) -> bool
  391. {
  392. return !exchangeBattle.getForUpdate(u->unitId())->alive();
  393. });
  394. }
  395. // avoid blocking path for stronger stack by weaker stack
  396. // the method checks if all stacks can be placed around enemy
  397. v.adjustPositions(melleeAttackers, ap, reachabilityMap);
  398. #if BATTLE_TRACE_LEVEL>=1
  399. logAi->trace("Exchange score: %lld", v.getScore());
  400. #endif
  401. return v.getScore();
  402. }
  403. void BattleExchangeVariant::adjustPositions(
  404. std::vector<const battle::Unit*> attackers,
  405. const AttackPossibility & ap,
  406. std::map<BattleHex, battle::Units> & reachabilityMap)
  407. {
  408. auto hexes = ap.attack.defender->getSurroundingHexes();
  409. boost::sort(attackers, [&](const battle::Unit * u1, const battle::Unit * u2) -> bool
  410. {
  411. if(attackerValue[u1->unitId()].isRetalitated && !attackerValue[u2->unitId()].isRetalitated)
  412. return true;
  413. if(attackerValue[u2->unitId()].isRetalitated && !attackerValue[u1->unitId()].isRetalitated)
  414. return false;
  415. return attackerValue[u1->unitId()].value > attackerValue[u2->unitId()].value;
  416. });
  417. if(!ap.attack.shooting)
  418. {
  419. vstd::erase_if_present(hexes, ap.from);
  420. vstd::erase_if_present(hexes, ap.attack.attacker->occupiedHex(ap.attack.attackerPos));
  421. }
  422. int64_t notRealizedDamage = 0;
  423. for(auto unit : attackers)
  424. {
  425. if(unit->unitId() == ap.attack.attacker->unitId())
  426. continue;
  427. if(!vstd::contains_if(hexes, [&](BattleHex h) -> bool
  428. {
  429. return vstd::contains(reachabilityMap[h], unit);
  430. }))
  431. {
  432. notRealizedDamage += attackerValue[unit->unitId()].value;
  433. continue;
  434. }
  435. auto desiredPosition = vstd::minElementByFun(hexes, [&](BattleHex h) -> int64_t
  436. {
  437. auto score = vstd::contains(reachabilityMap[h], unit)
  438. ? reachabilityMap[h].size()
  439. : 0;
  440. if(unit->doubleWide())
  441. {
  442. auto backHex = unit->occupiedHex(h);
  443. if(vstd::contains(hexes, backHex))
  444. score += reachabilityMap[backHex].size();
  445. }
  446. return score;
  447. });
  448. hexes.erase(desiredPosition);
  449. }
  450. if(notRealizedDamage > ap.attackValue() && notRealizedDamage > attackerValue[ap.attack.attacker->unitId()].value)
  451. {
  452. dpsScore = EvaluationResult::INEFFECTIVE_SCORE;
  453. }
  454. }
  455. void BattleExchangeEvaluator::updateReachabilityMap(HypotheticBattle & hb)
  456. {
  457. const int TURN_DEPTH = 2;
  458. turnOrder.clear();
  459. hb.battleGetTurnOrder(turnOrder, std::numeric_limits<int>::max(), TURN_DEPTH);
  460. reachabilityMap.clear();
  461. for(int turn = 0; turn < turnOrder.size(); turn++)
  462. {
  463. auto & turnQueue = turnOrder[turn];
  464. HypotheticBattle turnBattle(env.get(), cb);
  465. for(const battle::Unit * unit : turnQueue)
  466. {
  467. if(turnBattle.battleCanShoot(unit))
  468. {
  469. for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
  470. {
  471. reachabilityMap[hex].push_back(unit);
  472. }
  473. continue;
  474. }
  475. auto unitReachability = turnBattle.getReachability(unit);
  476. for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
  477. {
  478. bool reachable = unitReachability.distances[hex] <= unit->Speed(turn);
  479. if(!reachable && unitReachability.accessibility[hex] == EAccessibility::ALIVE_STACK)
  480. {
  481. const battle::Unit * hexStack = cb->battleGetUnitByPos(hex);
  482. if(hexStack && cb->battleMatchOwner(unit, hexStack, false))
  483. {
  484. for(BattleHex neighbor : hex.neighbouringTiles())
  485. {
  486. reachable = unitReachability.distances[neighbor] <= unit->Speed(turn);
  487. if(reachable) break;
  488. }
  489. }
  490. }
  491. if(reachable)
  492. {
  493. reachabilityMap[hex].push_back(unit);
  494. }
  495. }
  496. }
  497. }
  498. }
  499. // avoid blocking path for stronger stack by weaker stack
  500. bool BattleExchangeEvaluator::checkPositionBlocksOurStacks(HypotheticBattle & hb, const battle::Unit * activeUnit, BattleHex position)
  501. {
  502. const int BLOCKING_THRESHOLD = 70;
  503. const int BLOCKING_OWN_ATTACK_PENALTY = 100;
  504. const int BLOCKING_OWN_MOVE_PENALTY = 1;
  505. float blockingScore = 0;
  506. auto activeUnitDamage = activeUnit->getMinDamage(hb.battleCanShoot(activeUnit)) * activeUnit->getCount();
  507. for(int turn = 0; turn < turnOrder.size(); turn++)
  508. {
  509. auto & turnQueue = turnOrder[turn];
  510. HypotheticBattle turnBattle(env.get(), cb);
  511. auto unitToUpdate = turnBattle.getForUpdate(activeUnit->unitId());
  512. unitToUpdate->setPosition(position);
  513. for(const battle::Unit * unit : turnQueue)
  514. {
  515. if(unit->unitId() == unitToUpdate->unitId() || cb->battleMatchOwner(unit, activeUnit, false))
  516. continue;
  517. auto blockedUnitDamage = unit->getMinDamage(hb.battleCanShoot(unit)) * unit->getCount();
  518. auto ratio = blockedUnitDamage / (blockedUnitDamage + activeUnitDamage);
  519. auto unitReachability = turnBattle.getReachability(unit);
  520. for(BattleHex hex = BattleHex::TOP_LEFT; hex.isValid(); hex = hex + 1)
  521. {
  522. bool enemyUnit = false;
  523. bool reachable = unitReachability.distances[hex] <= unit->Speed(turn);
  524. if(!reachable && unitReachability.accessibility[hex] == EAccessibility::ALIVE_STACK)
  525. {
  526. const battle::Unit * hexStack = turnBattle.battleGetUnitByPos(hex);
  527. if(hexStack && cb->battleMatchOwner(unit, hexStack, false))
  528. {
  529. enemyUnit = true;
  530. for(BattleHex neighbor : hex.neighbouringTiles())
  531. {
  532. reachable = unitReachability.distances[neighbor] <= unit->Speed(turn);
  533. if(reachable) break;
  534. }
  535. }
  536. }
  537. if(!reachable && vstd::contains(reachabilityMap[hex], unit))
  538. {
  539. blockingScore += ratio * (enemyUnit ? BLOCKING_OWN_ATTACK_PENALTY : BLOCKING_OWN_MOVE_PENALTY);
  540. }
  541. }
  542. }
  543. }
  544. #if BATTLE_TRACE_LEVEL>=1
  545. logAi->trace("Position %d, blocking score %f", position.hex, blockingScore);
  546. #endif
  547. return blockingScore > BLOCKING_THRESHOLD;
  548. }