浏览代码

Merge pull request #4161 from vcmi/fix-4142

#4142 - sometimes Battle AI wants to attack unit which is behind a lo…
Ivan Savenko 1 年之前
父节点
当前提交
a9cf322f61
共有 1 个文件被更改,包括 31 次插入12 次删除
  1. 31 12
      AI/BattleAI/BattleEvaluator.cpp

+ 31 - 12
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<BattleHex> 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<BattleHex> 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)
 	{