mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-13 19:54:17 +02:00
Merge pull request #671 from nullkiller/fix-stupid-ai-attacking-through-obstacles
Fix StupidAI trying to attack through obstacle
This commit is contained in:
@@ -161,7 +161,7 @@ BattleAction CStupidAI::activeStack( const CStack * stack )
|
|||||||
|
|
||||||
if(dists.distToNearestNeighbour(stack, closestEnemy->s) < GameConstants::BFIELD_SIZE)
|
if(dists.distToNearestNeighbour(stack, closestEnemy->s) < GameConstants::BFIELD_SIZE)
|
||||||
{
|
{
|
||||||
return goTowards(stack, closestEnemy->s);
|
return goTowards(stack, closestEnemy->s->getAttackableHexes(stack));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,31 +229,37 @@ void CStupidAI::print(const std::string &text) const
|
|||||||
logAi->trace("CStupidAI [%p]: %s", this, text);
|
logAi->trace("CStupidAI [%p]: %s", this, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
BattleAction CStupidAI::goTowards(const CStack * stack, const CStack * enemy) const
|
BattleAction CStupidAI::goTowards(const CStack * stack, std::vector<BattleHex> hexes) const
|
||||||
{
|
{
|
||||||
auto destination = enemy->getPosition();
|
|
||||||
assert(destination.isValid());
|
|
||||||
auto reachability = cb->getReachability(stack);
|
auto reachability = cb->getReachability(stack);
|
||||||
auto avHexes = cb->battleGetAvailableHexes(reachability, stack);
|
auto avHexes = cb->battleGetAvailableHexes(reachability, stack);
|
||||||
|
|
||||||
if(vstd::contains(avHexes, destination))
|
if(!avHexes.size() || !hexes.size()) //we are blocked or dest is blocked
|
||||||
return BattleAction::makeMove(stack, destination);
|
{
|
||||||
|
return BattleAction::makeDefend(stack);
|
||||||
|
}
|
||||||
|
|
||||||
auto destNeighbours = destination.neighbouringTiles();
|
std::sort(hexes.begin(), hexes.end(), [&](BattleHex h1, BattleHex h2) -> bool
|
||||||
if(vstd::contains_if(destNeighbours, [&](BattleHex n) { return stack->coversPos(destination); }))
|
{
|
||||||
|
return reachability.distances[h1] < reachability.distances[h2];
|
||||||
|
});
|
||||||
|
|
||||||
|
for(auto hex : hexes)
|
||||||
|
{
|
||||||
|
if(vstd::contains(avHexes, hex))
|
||||||
|
return BattleAction::makeMove(stack, hex);
|
||||||
|
|
||||||
|
if(stack->coversPos(hex))
|
||||||
{
|
{
|
||||||
logAi->warn("Warning: already standing on neighbouring tile!");
|
logAi->warn("Warning: already standing on neighbouring tile!");
|
||||||
//We shouldn't even be here...
|
//We shouldn't even be here...
|
||||||
return BattleAction::makeDefend(stack);
|
return BattleAction::makeDefend(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!avHexes.size()) //we are blocked or dest is blocked
|
|
||||||
{
|
|
||||||
return BattleAction::makeDefend(stack);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BattleHex bestNeighbor = destination;
|
BattleHex bestNeighbor = hexes.front();
|
||||||
if(reachability.distToNearestNeighbour(stack, enemy, &bestNeighbor) > GameConstants::BFIELD_SIZE)
|
|
||||||
|
if(reachability.distances[bestNeighbor] > GameConstants::BFIELD_SIZE)
|
||||||
{
|
{
|
||||||
return BattleAction::makeDefend(stack);
|
return BattleAction::makeDefend(stack);
|
||||||
}
|
}
|
||||||
|
@@ -46,6 +46,6 @@ public:
|
|||||||
virtual void loadGame(BinaryDeserializer & h, const int version) override;
|
virtual void loadGame(BinaryDeserializer & h, const int version) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BattleAction goTowards(const CStack * stack, const CStack * enemy) const;
|
BattleAction goTowards(const CStack * stack, std::vector<BattleHex> hexes) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user