The default AStar#_estimate_cost()
method is just the Euclidean distance (Vector2#distance_to()
) between the positions associated with the provided points. Its purpose is to allow sub-classes to override the estimation function used by the A* algorithm with something more problem-specific; for example, if doing grid-based movement with no diagonals you'd want "Manhattan distance" instead (sum of strictly vertical and horizontal distance).
The algorithm I believe you want here is mostly just a breadth-first search, though if you squint at it you could call it a variation of Dijkstra's algorithm. Here's a sketch of how you could do it in GDScript:
Create three collections (e.g. Dictionary
) mapping starting starting points to movement costs: one of "reachable points," initially empty; one of "fringe points," initialized to map the starting point to cost 0; and one of "next fringe points," initially empty. Then repeatedly iterate over the fringe collection, doing:
- If the point is already in in the reachable collection with equal or lower cost, stop processing this point.
- Add the point to the reachable collection, with the associated cost.
- Find each neighbor of the fringe point and the total cost to get there via current fringe point. Drop any point above the cost cutoff or already in the reachable collection with equal or lower cost. Otherwise, add the point to the "next fringe" collection, keeping the lower cost if already present.
Then "next fringe" becomes "fringe," set "next fringe" to be a new empty collection, and repeat until the fringe is empty. Then the "reachable" collection will contain all the reachable points.
This algorithm doesn't require the priority queue you need for Dijkstra's algorithm, and so is less theoretically efficient, but should be totally fine for searching on an actual grid.
HTH!