diff --git a/AI/BattleAI/BattleEvaluator.cpp b/AI/BattleAI/BattleEvaluator.cpp index 07a221e2c..9c911d942 100644 --- a/AI/BattleAI/BattleEvaluator.cpp +++ b/AI/BattleAI/BattleEvaluator.cpp @@ -259,27 +259,46 @@ BattleAction BattleEvaluator::goTowardsNearest(const CStack * stack, std::vector return BattleAction::makeDefend(stack); } - std::sort(hexes.begin(), hexes.end(), [&](BattleHex h1, BattleHex h2) -> bool - { - return reachability.distances[h1] < reachability.distances[h2]; - }); + std::vector targetHexes = hexes; - for(auto hex : hexes) + for(int i = 0; i < 5; i++) { - if(vstd::contains(avHexes, hex)) + std::sort(targetHexes.begin(), targetHexes.end(), [&](BattleHex h1, BattleHex h2) -> bool + { + return reachability.distances[h1] < reachability.distances[h2]; + }); + + for(auto hex : targetHexes) { - return BattleAction::makeMove(stack, hex); + if(vstd::contains(avHexes, hex)) + { + return BattleAction::makeMove(stack, hex); + } + + if(stack->coversPos(hex)) + { + logAi->warn("Warning: already standing on neighbouring tile!"); + //We shouldn't even be here... + return BattleAction::makeDefend(stack); + } } - if(stack->coversPos(hex)) + if(reachability.distances[targetHexes.front()] <= GameConstants::BFIELD_SIZE) { - logAi->warn("Warning: already standing on neighbouring tile!"); - //We shouldn't even be here... - return BattleAction::makeDefend(stack); + break; } + + std::vector copy = targetHexes; + + for(auto hex : copy) + { + vstd::concatenate(targetHexes, hex.allNeighbouringTiles()); + } + + vstd::removeDuplicates(targetHexes); } - BattleHex bestNeighbor = hexes.front(); + BattleHex bestNeighbor = targetHexes.front(); if(reachability.distances[bestNeighbor] > GameConstants::BFIELD_SIZE) {