[Algorithm] Turn-based pathfinding

Hi, I don't know if I should put this in script support/engine support as it is more of an algorithmic question than related to an engine but if you feel like it does not belong here, please move this thread. I'm working on a pathfinding algorithm for units in my battle system (engine is vx ace) and I have issues with computer-controlled units:

Take a grid with cells filled with units or empty such that no two units may share the same position (but some can cross other's position in their paths). Some cells may cost more than others to cross.
Take a unit in position x,y on the grid and you want to reach an empty position x',y' that I will call target.
Assume your unit is limited in move points in a single turn and cannot reach the target this turn.

Running a shortest path algorithm (like Dijkstra or A*) provides an optimal path from your unit to the target, but you have to cut the path returned because it's too long to cross in a single turn. However, the cut path may stop at a non-empty cell.
In that case, you may consider reducing even more the path.

What if you cut your path so much that a better alternative path may exist somewhere else?
How would you compute such better path while trying to minimize the calls to a shortest path algorithm?

Example here: the blue box is your unit, the blue cross is the target, the red boxes are other units you may cross (but not stay at). The light blue trail is the returned path by your first shortest path algorithm call and you can cross 3 cells per turn.
Obviously, here there is a better solution that we can get 'easily' by calling once again our shortest path algorithm and excluding at least the farthest red square. But a second call might not be enough in general.
1746377770174.png
What if you cut your path so much that a better alternative path may exist somewhere else?
That cut length is what needs to be balanced. More range means more information; not enough information means you have to make assumptions, which can be wrong.

Running a shortest path algorithm (like Dijkstra or A*) provides an optimal path
Yes. However, there are ways to alter resource usage on a case-by-case basis, e.g.
  • Weighting - depth-/breadth-first search prioritisation, based on map layout.
  • Jump Point Search - pre-process the map into "corner" tiles, path from corner to corner.
  • D* Lite - run the full path once, splice a sub-path around any obstacle as needed.
For conflicts between allied agents, you could consider some kind of group pathing, especially if all members of the group move at the same speed. You can also fine-tune the algorithm's structure, e.g. using a heap rather than an array to minimise lookup times.

You might find the info here helpful~

Edit: not sure where this might fit best, but for now:

I've moved this thread to Mechanics Design (from General Discussion). Thank you.

What about using the opposite logic?
Calculating all the cells that the battler can reach, then pick the most interesting one.
Thank you for your answers.

So originally, I simply did a Dijkstra (with bounded distance based on my move points) and then picked a position based on "interest" which was getting closer to a target, but because of obstacles my unit could reach a local best position while still being an unsatisfying one.

Which was why I expanded the research to cells that were far away for better pathfinding. I also keep in memory the other positions.
For now I decided to perform a reverse Dijkstra from my chosen target and I then take the empty cells I can reach this turn that are the closest to the target.

EDIT (for more detailed answer):

For conflicts between allied agents, you could consider some kind of group pathing, especially if all members of the group move at the same speed. You can also fine-tune the algorithm's structure, e.g. using a heap rather than an array to minimise lookup times.
Group pathfinding could be interesting, but for now my ais are individualists, they take decisions without considering the decisions of their allies, there could also be 'allied' units that are player-controllable so a grouped strategy to avoid conflicts would not always work.
The heap structure might improve performances, I will consider it.

Thank you again for the link you provided and the algorithms you've suggested, I did not know them and they are well explained!

Original thread: https://forums.rpgmakerweb.com/threads/algorithm-turn-based-pathfinding.177418/