Table of Contents

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>.GetCost

The default function for cost determination of a path point

defaultCost float

The default cost for a path point

defaultMaxTries int

The default amount of tries before path finding is aborted

defaultAdditionalNeighbors AStar<T>.CollectAdditionalNeighbors

The default AStar<T>.CollectAdditionalNeighbors function.

Fields

DefaultAdditionalNeighbors

public AStar<T>.CollectAdditionalNeighbors DefaultAdditionalNeighbors

Field Value

AStar<T>.CollectAdditionalNeighbors

DefaultCost

The default cost for a path point.

public float DefaultCost

Field Value

float

DefaultCostFunction

The default cost function that determines the cost for each path finding position.

public AStar<T>.GetCost DefaultCostFunction

Field Value

AStar<T>.GetCost

DefaultMaxTries

The default amount of maximum tries that will be used before path finding is aborted.

public int DefaultMaxTries

Field Value

int

Properties

LastTimeNeeded

The amount of time required for finding the last queried path

public TimeSpan LastTimeNeeded { get; }

Property Value

TimeSpan

LastTriesNeeded

The amount of tries required for finding the last queried path

public int LastTriesNeeded { get; }

Property Value

int

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 T

The 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 T

The point to start path finding at

goal T

The point to find a path to

costFunction AStar<T>.GetCost

The 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>.CollectAdditionalNeighbors

A 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 T

The start position.

position T

The 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 T

The 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 float

The total cost that was calculated for the path, or PositiveInfinity if no path was found.

costFunction AStar<T>.GetCost

The 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>.CollectAdditionalNeighbors

A function that determines a set of additional neighbors to be considered for a given point.

Returns

bool

Whether a path was found.