Class AStarSearch
java.lang.Object
com.github.yellowstonegames.path.AStarSearch
Performs A* search to find the shortest path between two Coord points.
A* is a best-first search algorithm for pathfinding. It uses a heuristic value to reduce the total search space. If the heuristic is too large then the optimal path is not guaranteed to be returned.
This implementation is a thin wrapper around
A* is a best-first search algorithm for pathfinding. It uses a heuristic value to reduce the total search space. If the heuristic is too large then the optimal path is not guaranteed to be returned.
This implementation is a thin wrapper around
CostlyGraph and the other code in the
com.github.yellowstonegames.path package; it is based on code from simple-graphs by earlygrey. The current
version is quite fast, typically outpacing gdx-ai's more-complex IndexedAStarPathfinder by a high margin. Typical
usage of this class involves either the simpler technique of only using '#' for walls or obstructions and
calling AStarSearch(char[][], Heuristic), or the more complex technique that allows variable costs for
different types of terrain, using CostlyGraph.generateAStarCostMap(char[][], IntFloatMap, float) to generate
a cost map.- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CostlyGraphprotected intprotected com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord> protected com.github.yellowstonegames.grid.Coordprotected com.github.yellowstonegames.grid.Coordprotected Heuristic<com.github.yellowstonegames.grid.Coord> protected int -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAStarSearch(char[][] map, Heuristic<com.github.yellowstonegames.grid.Coord> type) Builds a pathing object to run searches on.AStarSearch(float[][] map, Heuristic<com.github.yellowstonegames.grid.Coord> type) Builds a pathing object to run searches on. -
Method Summary
Modifier and TypeMethodDescriptionintintgetWidth()com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord> path(int startx, int starty, int targetx, int targety) Finds an A* path to the target from the start.com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord> path(com.github.yellowstonegames.grid.Coord start, com.github.yellowstonegames.grid.Coord target) Finds an A* path to the target from the start.reinitialize(char[][] map, Heuristic<com.github.yellowstonegames.grid.Coord> type) Resets this pathing object to use a different map and optionally a different Heuristic.reinitialize(float[][] map, Heuristic<com.github.yellowstonegames.grid.Coord> type) Resets this pathing object to use a different map and optionally a different Heuristic.toString()
-
Field Details
-
width
protected int width -
height
protected int height -
start
protected com.github.yellowstonegames.grid.Coord start -
target
protected com.github.yellowstonegames.grid.Coord target -
type
-
graph
-
path
protected com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord> path
-
-
Constructor Details
-
AStarSearch
protected AStarSearch() -
AStarSearch
Builds a pathing object to run searches on.
Values in the map are treated as positive values being legal weights, with higher values being harder to pass through. Any negative value is treated as being an impassible space. A weight of 0 can be moved through at no cost, but this should be used very carefully, if at all. Cost maps are commonly built using theCostlyGraph.generateAStarCostMap(char[][], IntFloatMap, float)andCostlyGraph.generateAStarCostMap(char[][])methods from a 2D char array.
If the type is Manhattan, only the cardinal directions will be used. All other search types will return a result based on diagonal and cardinal pathing (8-way).- Parameters:
map- the search map, as produced byCostlyGraph.generateAStarCostMap(char[][])type- a Heuristic constant; one of the four from fromHeuristic.HEURISTICS
-
AStarSearch
Builds a pathing object to run searches on.
Values in the map are all considered equally passable unless the char is'#', in which case it is considered an impassable wall. ThegetBareDungeon()method in squidplace's DungeonProcessor class is a common way to get a map where only '#' is used to mean a wall.
If the type is Manhattan, only the cardinal directions will be used. All other search types will return a result based on diagonal and cardinal pathing (8-way).- Parameters:
map- a 2D char array where only'#'represents a wall, and anything else is equally passabletype- the manner of search
-
-
Method Details
-
reinitialize
public AStarSearch reinitialize(float[][] map, Heuristic<com.github.yellowstonegames.grid.Coord> type) Resets this pathing object to use a different map and optionally a different Heuristic.
Values in the map are treated as positive values being legal weights, with higher values being harder to pass through. Any negative value is treated as being an impassible space. A weight of 0 can be moved through at no cost, but this should be used very carefully, if at all. Cost maps are commonly built using theCostlyGraph.generateAStarCostMap(char[][], IntFloatMap, float)andCostlyGraph.generateAStarCostMap(char[][])methods from a 2D char array.
If the type is Manhattan, only the cardinal directions will be used. All other search types will return a result based on diagonal and cardinal pathing (8-way).- Parameters:
map- the search map, as produced byCostlyGraph.generateAStarCostMap(char[][])type- the manner of search
-
reinitialize
public AStarSearch reinitialize(char[][] map, Heuristic<com.github.yellowstonegames.grid.Coord> type) Resets this pathing object to use a different map and optionally a different Heuristic.
Values in the map are all considered equally passable unless the char is'#','+', or any box drawing character, in which case it is considered an impassable wall. ThegetBareDungeon()method in squidplace's DungeonProcessor class is a common way to get a map where only '#' is used to mean a wall.
If the type is Manhattan, only the cardinal directions will be used. All other search types will return a result based on diagonal and cardinal pathing (8-way).- Parameters:
map- a 2D char array where only'#'represents a wall, and anything else is equally passabletype- the manner of search
-
path
public com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord> path(int startx, int starty, int targetx, int targety) Finds an A* path to the target from the start. If no path is possible, returns null. Reuses the returned path instead of allocating a new ObjectDeque, so if you call this again, then the previously-returned List could change.- Parameters:
startx- the x coordinate of the start locationstarty- the y coordinate of the start locationtargetx- the x coordinate of the target locationtargety- the y coordinate of the target location- Returns:
- the shortest path, or null if no path is possible
-
path
public com.github.tommyettinger.ds.ObjectDeque<com.github.yellowstonegames.grid.Coord> path(com.github.yellowstonegames.grid.Coord start, com.github.yellowstonegames.grid.Coord target) Finds an A* path to the target from the start. If no path is possible, returns null. Reuses the returned path instead of allocating a new ObjectDeque, so if you call this again, then the previously-returned List could change.- Parameters:
start- the start locationtarget- the target location- Returns:
- the shortest path, or null if no path is possible
-
getWidth
public int getWidth() -
getHeight
public int getHeight() -
toString
-