Class AStar<T>
- Namespace
- MLEM.Pathfinding
- Assembly
- MLEM.dll
This is an abstract implementation of the A* path finding algorithm. This implementation is used by AStar2, a 2-dimensional A* path finding algorithm, and AStar3, a 3-dimensional A* path finding algorithm.
public abstract class AStar<T>
Type Parameters
T
The type of points used for this path
- Inheritance
-
AStar<T>
- Derived
- Inherited Members
Constructors
AStar(GetCost, float, int, CollectAdditionalNeighbors)
Creates a new A* pathfinder with the supplied default settings.
protected AStar(AStar<T>.GetCost defaultCostFunction, float defaultCost, int defaultMaxTries, AStar<T>.CollectAdditionalNeighbors defaultAdditionalNeighbors)
Parameters
defaultCostFunction
AStar<T>.GetCostThe default function for cost determination of a path point
defaultCost
floatThe default cost for a path point
defaultMaxTries
intThe default amount of tries before path finding is aborted
defaultAdditionalNeighbors
AStar<T>.CollectAdditionalNeighborsThe default AStar<T>.CollectAdditionalNeighbors function.
Fields
DefaultAdditionalNeighbors
The default AStar<T>.CollectAdditionalNeighbors function.
public AStar<T>.CollectAdditionalNeighbors DefaultAdditionalNeighbors
Field Value
DefaultCost
The default cost for a path point.
public float DefaultCost
Field Value
DefaultCostFunction
The default cost function that determines the cost for each path finding position.
public AStar<T>.GetCost DefaultCostFunction
Field Value
DefaultMaxTries
The default amount of maximum tries that will be used before path finding is aborted.
public int DefaultMaxTries
Field Value
Properties
LastTimeNeeded
The amount of time required for finding the last queried path
public TimeSpan LastTimeNeeded { get; }
Property Value
LastTriesNeeded
The amount of tries required for finding the last queried path
public int LastTriesNeeded { get; }
Property Value
Methods
CollectNeighbors(T, ISet<T>)
This method should populate a set of positions that are considered neighbors
to the given position
. For example, this method might return directly adjacent positions, diagonal positions, or faraway positions that can be teleported to.
protected abstract void CollectNeighbors(T position, ISet<T> neighbors)
Parameters
position
TThe position whose neighbors to return.
neighbors
ISet<T>The set to populate with neighbors.
FindPath(T, T, GetCost, float?, int?, CollectAdditionalNeighbors)
Finds a path between two points using this pathfinder's default settings or, alternatively, the supplied override settings.
public Stack<T> FindPath(T start, T goal, AStar<T>.GetCost costFunction = null, float? defaultCost = null, int? maxTries = null, AStar<T>.CollectAdditionalNeighbors additionalNeighbors = null)
Parameters
start
TThe point to start path finding at
goal
TThe point to find a path to
costFunction
AStar<T>.GetCostThe function that determines the cost for each path point
defaultCost
float?The default cost for each path point
maxTries
int?The maximum amount of tries before path finding is aborted
additionalNeighbors
AStar<T>.CollectAdditionalNeighborsA function that determines a set of additional neighbors to be considered for a given point.
Returns
- Stack<T>
A stack of path points, where the top item is the first point to go to, or null if no path was found.
GetHeuristicDistance(T, T)
This method should implement a heuristic that determines the total distance between the given start
position and the given second position position
.
Note that this is multiplied with the DefaultCost automatically, so no costs need to be considered in this method's return value.
protected abstract float GetHeuristicDistance(T start, T position)
Parameters
start
TThe start position.
position
TThe position to get the distance to.
Returns
- float
The total distance between the two positions.
TryFindPath(T, ICollection<T>, out Stack<T>, out float, GetCost, float?, int?, CollectAdditionalNeighbors)
Tries to find a path between two points using this pathfinder's default settings or, alternatively, the supplied override settings.
public bool TryFindPath(T start, ICollection<T> goals, out Stack<T> path, out float totalCost, AStar<T>.GetCost costFunction = null, float? defaultCost = null, int? maxTries = null, AStar<T>.CollectAdditionalNeighbors additionalNeighbors = null)
Parameters
start
TThe point to start path finding at
goals
ICollection<T>The points to find a path to, one of which will be chosen as the closest or best destination
path
Stack<T>The path that was found, or null if no path was found.
totalCost
floatThe total cost that was calculated for the path, or PositiveInfinity if no path was found.
costFunction
AStar<T>.GetCostThe function that determines the cost for each path point
defaultCost
float?The default cost for each path point
maxTries
int?The maximum amount of tries before path finding is aborted
additionalNeighbors
AStar<T>.CollectAdditionalNeighborsA function that determines a set of additional neighbors to be considered for a given point.
Returns
- bool
Whether a path was found.