1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Merge pull request #4161 from vcmi/fix-4142

#4142 - sometimes Battle AI wants to attack unit which is behind a lo…
This commit is contained in:
Ivan Savenko 2024-06-19 21:28:58 +03:00 committed by GitHub
commit a9cf322f61
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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)
{