public class AStarSearch
extends java.lang.Object
implements java.io.Serializable
CostlyGraph
and the other code in the
squidpony.squidai.graph
package; it replaces an older, much-less-efficient A* implementation with one 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. The only major change in the API is that this version returns an ArrayList
of Coord instead of a Queue of Coord. Typical usage of this class involves either the simpler technique of only using
'#'
for walls or obstructions and calling AStarSearch(char[][], SearchType)
, or the more complex
technique that allows variable costs for different types of terrain, using
DungeonUtility.generateAStarCostMap(char[][], Map, double)
to generate a cost
map; if you used the old AStarSearch, then be advised that the default cost is now 1.0 instead of 0.0.Modifier and Type | Class and Description |
---|---|
static class |
AStarSearch.SearchType
The type of heuristic to use.
|
Modifier and Type | Field and Description |
---|---|
protected CostlyGraph |
graph |
protected int |
height |
protected java.util.ArrayList<Coord> |
path |
protected Coord |
start |
protected Coord |
target |
protected AStarSearch.SearchType |
type |
protected int |
width |
Modifier | Constructor and Description |
---|---|
protected |
AStarSearch() |
|
AStarSearch(char[][] map,
AStarSearch.SearchType type)
Builds a pathing object to run searches on.
|
|
AStarSearch(double[][] map,
AStarSearch.SearchType type)
Builds a pathing object to run searches on.
|
Modifier and Type | Method and Description |
---|---|
int |
getHeight() |
int |
getWidth() |
java.util.ArrayList<Coord> |
path(Coord start,
Coord target)
Finds an A* path to the target from the start.
|
java.util.ArrayList<Coord> |
path(int startx,
int starty,
int targetx,
int targety)
Finds an A* path to the target from the start.
|
AStarSearch |
reinitialize(char[][] map,
AStarSearch.SearchType type)
Resets this pathing object to use a different map and optionally a different SearchType.
|
AStarSearch |
reinitialize(double[][] map,
AStarSearch.SearchType type)
Resets this pathing object to use a different map and optionally a different SearchType.
|
java.lang.String |
toString() |
protected int width
protected int height
protected Coord start
protected Coord target
protected AStarSearch.SearchType type
protected CostlyGraph graph
protected java.util.ArrayList<Coord> path
protected AStarSearch()
public AStarSearch(double[][] map, AStarSearch.SearchType type)
DungeonUtility.generateAStarCostMap(char[][], Map, double)
and
DungeonUtility.generateAStarCostMap(char[][])
methods from a 2D char array.
map
- the search map, as produced by DungeonUtility.generateAStarCostMap(char[][])
type
- the manner of searchpublic AStarSearch(char[][] map, AStarSearch.SearchType type)
'#'
, in which case it is
considered an impassable wall. The DungeonGenerator.getBareDungeon()
method is a common way to get a map
where only '#' is used to mean a wall.
map
- a 2D char array where only '#'
represents a wall, and anything else is equally passabletype
- the manner of searchpublic AStarSearch reinitialize(double[][] map, AStarSearch.SearchType type)
DungeonUtility.generateAStarCostMap(char[][], Map, double)
and
DungeonUtility.generateAStarCostMap(char[][])
methods from a 2D char array.
map
- the search map, as produced by DungeonUtility.generateAStarCostMap(char[][])
type
- the manner of searchpublic AStarSearch reinitialize(char[][] map, AStarSearch.SearchType type)
'#'
, '+'
, or any box
drawing character, in which case it is considered an impassable wall. DungeonGenerator.getBareDungeon()
is a common way to get a map where only '#' is used to mean a wall.
map
- a 2D char array where only '#'
represents a wall, and anything else is equally passabletype
- the manner of searchpublic java.util.ArrayList<Coord> path(int startx, int starty, int targetx, int targety)
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 locationpublic java.util.ArrayList<Coord> path(Coord start, Coord target)
start
- the start locationtarget
- the target locationpublic int getWidth()
public int getHeight()
public java.lang.String toString()
toString
in class java.lang.Object
Copyright © Eben Howard 2012–2022. All rights reserved.