A* Pathfinding Project
4.0.3
The A* Pathfinding Project for Unity 3D
|
Base class for all path types. More...
Base class for all path types.
Public Member Functions | |
void | BlockUntilCalculated () |
Blocks until this path has been calculated and returned. | |
void | Claim (System.Object o) |
Claim this path (pooling). | |
void | Error () |
Aborts the path because of an error. | |
PathState | GetState () |
Returns the state of the path in the pathfinding pipeline. | |
float | GetTotalLength () |
Total Length of the path. | |
bool | IsDone () |
Returns if this path is done calculating. | |
void | Release (System.Object o, bool silent=false) |
Releases a path claim (pooling). | |
IEnumerator | WaitForPath () |
Waits until this path has been calculated and returned. | |
Public Member Functions inherited from IPathInternals | |
void | AdvanceState (PathState s) |
void | CalculateStep (long targetTick) |
void | Cleanup () |
void | Initialize () |
void | OnEnterPool () |
void | Prepare () |
void | PrepareBase (PathHandler handler) |
void | Reset () |
void | ReturnPath () |
Public Attributes | |
OnPathDelegate | callback |
Callback to call when the path is complete. | |
int | enabledTags = -1 |
Which graph tags are traversable. | |
Heuristic | heuristic |
Determines which heuristic to use. | |
float | heuristicScale = 1F |
Scale of the heuristic values. | |
OnPathDelegate | immediateCallback |
Immediate callback to call when the path is complete. | |
NNConstraint | nnConstraint = PathNNConstraint.Default |
Constraint for how to search for nodes. | |
List< GraphNode > | path |
Holds the path as a Node array. | |
ITraversalProvider | traversalProvider |
Provides additional traversal information to a path request. | |
List< Vector3 > | vectorPath |
Holds the (possibly post processed) path as a Vector3 list. | |
Protected Member Functions | |
abstract void | CalculateStep (long targetTick) |
Calculates the until it is complete or the time has progressed past targetTick. | |
virtual void | Cleanup () |
Always called after the path has been calculated. | |
void | DebugStringPrefix (PathLog logMode, System.Text.StringBuilder text) |
Writes text shared for all overrides of DebugString to the string builder. | |
void | DebugStringSuffix (PathLog logMode, System.Text.StringBuilder text) |
Writes text shared for all overrides of DebugString to the string builder. | |
abstract void | Initialize () |
Initializes the path. | |
virtual void | OnEnterPool () |
Called when the path enters the pool. | |
abstract void | Prepare () |
Called before the path is started. | |
void | PrepareBase (PathHandler pathHandler) |
Prepares low level path variables for calculation. | |
virtual void | Reset () |
Reset all values to their default values. | |
virtual void | ReturnPath () |
Calls callback to return the calculated path. | |
virtual void | Trace (PathNode from) |
Traces the calculated path from the end node to the start. | |
Protected Attributes | |
PathNode | currentR |
The node currently being processed. | |
bool | hasBeenReset |
True if the Reset function has been called. | |
Int3 | hTarget |
Target to use for H score calculations. | |
GraphNode | hTargetNode |
Target to use for H score calculation. | |
int[] | internalTagPenalties |
The tag penalties that are actually used. | |
int[] | manualTagPenalties |
Tag penalties set by other scripts. | |
PathHandler | pathHandler |
Data for the thread calculating this path. | |
int | searchedNodes |
Number of nodes this path has searched. | |
Package Functions | |
uint | CalculateHScore (GraphNode node) |
Estimated cost from the specified node to the target. | |
bool | CanTraverse (GraphNode node) |
Returns if the node can be traversed. | |
virtual string | DebugString (PathLog logMode) |
Returns a string with information about it. | |
void | ForceLogError (string msg) |
Logs an error and calls Error(). | |
virtual uint | GetConnectionSpecialCost (GraphNode a, GraphNode b, uint currentCost) |
May be called by graph nodes to get a special cost for some connections. | |
Int3 | GetHTarget () |
uint | GetTagPenalty (int tag) |
Returns penalty for the given tag. | |
uint | GetTraversalCost (GraphNode node) |
void | Log (string msg) |
Appends a message to the errorLog. | |
void | LogError (string msg) |
Appends msg to errorLog and logs msg to the console. | |
void | ReleaseSilent (System.Object o) |
Releases the path silently (pooling). | |
Package Attributes | |
float | duration |
How long it took to calculate this path in milliseconds. | |
Path | next |
Internal linked list implementation. | |
Properties | |
PathCompleteState | CompleteState [get, set] |
Current state of the path. | |
bool | error [get] |
If the path failed, this is true. | |
string | errorLog [get] |
Log messages with info about any errors. | |
virtual bool | FloodingPath [get] |
True for paths that want to search all nodes and not jump over nodes as optimizations. | |
PathHandler IPathInternals. | PathHandler [get] |
ushort | pathID [get, set] |
ID of this path. | |
PathState | PipelineState [get, set] |
Returns the state of the path in the pathfinding pipeline. | |
bool IPathInternals. | Pooled [get, set] |
True if the path is currently pooled. | |
bool | recycled [get] |
True if the path is currently recycled (i.e in the path pool). | |
int[] | tagPenalties [get, set] |
Penalties for each tag. | |
Properties inherited from IPathInternals | |
PathHandler | PathHandler [get] |
bool | Pooled [get, set] |
Private Member Functions | |
void IPathInternals. | AdvanceState (PathState s) |
Threadsafe increment of the state. | |
void IPathInternals. | CalculateStep (long targetTick) |
void IPathInternals. | Cleanup () |
void | ErrorCheck () |
Does some error checking. | |
void IPathInternals. | Initialize () |
void IPathInternals. | OnEnterPool () |
void IPathInternals. | Prepare () |
void IPathInternals. | PrepareBase (PathHandler handler) |
void IPathInternals. | Reset () |
void IPathInternals. | ReturnPath () |
Private Attributes | |
string | _errorLog = "" |
Additional info on what went wrong. | |
List< System.Object > | claimed = new List<System.Object>() |
List of claims on this path with reference objects. | |
bool | releasedNotSilent |
True if the path has been released with a non-silent call yet. | |
System.Object | stateLock = new object() |
Static Private Attributes | |
static readonly int[] | ZeroTagPenalties = new int[32] |
List of zeroes to use as default tag penalties. | |
|
private |
Threadsafe increment of the state.
void BlockUntilCalculated | ( | ) |
Blocks until this path has been calculated and returned.
Normally it takes a few frames for a path to be calculated and returned. This function will ensure that the path will be calculated when this function returns and that the callback for that path has been called.
Use this function only if you really need to. There is a point to spreading path calculations out over several frames. It smoothes out the framerate and makes sure requesting a large number of paths at the same time does not cause lag.
|
package |
Estimated cost from the specified node to the target.
|
protectedpure virtual |
Calculates the until it is complete or the time has progressed past targetTick.
Implemented in MultiTargetPath, ABPath, RandomPath, FloodPath, ConstantPath, FloodPathTracer, and XPath.
|
private |
|
package |
Returns if the node can be traversed.
This per default equals to if the node is walkable and if the node's tag is included in enabledTags
void Claim | ( | System.Object | o | ) |
Claim this path (pooling).
A claim on a path will ensure that it is not pooled. If you are using a path, you will want to claim it when you first get it and then release it when you will not use it anymore. When there are no claims on the path, it will be reset and put in a pool.
This is essentially just reference counting.
The object passed to this method is merely used as a way to more easily detect when pooling is not done correctly. It can be any object, when used from a movement script you can just pass "this". This class will throw an exception if you try to call Claim on the same path twice with the same object (which is usually not what you want) or if you try to call Release with an object that has not been used in a Claim call for that path. The object passed to the Claim method needs to be the same as the one you pass to this method.
|
protectedvirtual |
Always called after the path has been calculated.
Guaranteed to be called before other paths have been calculated on the same thread. Use for cleaning up things like node tagging and similar.
Reimplemented in MultiTargetPath, ABPath, and ConstantPath.
|
private |
|
packagevirtual |
Returns a string with information about it.
More information is emitted when logMode == Heavy. An empty string is returned if logMode == None or logMode == OnlyErrors and this path did not fail.
Reimplemented in MultiTargetPath, and ABPath.
|
protected |
Writes text shared for all overrides of DebugString to the string builder.
|
protected |
Writes text shared for all overrides of DebugString to the string builder.
void Error | ( | ) |
|
private |
Does some error checking.
Makes sure the user isn't using old code paths and that no major errors have been done.
An | exception if any errors are found |
|
package |
Logs an error and calls Error().
This is called only if something is very wrong or the user is doing something he/she really should not be doing.
May be called by graph nodes to get a special cost for some connections.
Nodes may call it when PathNode.flag2 is set to true, for example mesh nodes, which have a very large area can be marked on the start and end nodes, this method will be called to get the actual cost for moving from the start position to its neighbours instead of as would otherwise be the case, from the start node's position to its neighbours. The position of a node and the actual start point on the node can vary quite a lot.
The default behaviour of this method is to return the previous cost of the connection, essentiall making no change at all.
This method should return the same regardless of the order of a and b. That is f(a,b) == f(b,a) should hold.
a | Moving from this node |
b | Moving to this node |
currentCost | The cost of moving between the nodes. Return this value if there is no meaningful special cost to return. |
Reimplemented in ABPath.
|
package |
PathState GetState | ( | ) |
Returns the state of the path in the pathfinding pipeline.
|
package |
Returns penalty for the given tag.
tag | A value between 0 (inclusive) and 32 (exclusive). |
float GetTotalLength | ( | ) |
Total Length of the path.
Calculates the total length of the vectorPath. Cache this rather than call this function every time since it will calculate the length every time, not just return a cached value.
|
package |
|
protectedpure virtual |
Initializes the path.
Sets up the open list and adds the first node to it
Implemented in MultiTargetPath, ABPath, RandomPath, FloodPath, ConstantPath, and FloodPathTracer.
|
private |
bool IsDone | ( | ) |
Returns if this path is done calculating.
|
package |
Appends a message to the errorLog.
Nothing is logged to the console.
|
package |
Appends msg to errorLog and logs msg to the console.
Debug.Log call is only made if AstarPath.logPathResults is not equal to None and not equal to InGame. Consider calling Error() along with this call.
|
protectedvirtual |
Called when the path enters the pool.
This method should release e.g pooled lists and other pooled resources The base version of this method releases vectorPath and path lists. Reset() will be called after this function, not before.
Reimplemented in MultiTargetPath, and ConstantPath.
|
private |
|
protectedpure virtual |
Called before the path is started.
Called right before Initialize
Implemented in ABPath, MultiTargetPath, RandomPath, FloodPath, and ConstantPath.
|
private |
|
protected |
Prepares low level path variables for calculation.
Called before a path search will take place. Always called before the Prepare, Initialize and CalculateStep functions
|
private |
void Release | ( | System.Object | o, |
bool | silent = false |
||
) |
Releases a path claim (pooling).
Removes the claim of the path by the specified object. When the claim count reaches zero, the path will be pooled, all variables will be cleared and the path will be put in a pool to be used again. This is great for memory since less allocations are made.
If the silent parameter is true, this method will remove the claim by the specified object but the path will not be pooled if the claim count reches zero unless a Release call (not silent) has been made earlier. This is used by the internal pathfinding components such as Seeker and AstarPath so that they will not cause paths to be pooled. This enables users to skip the claim/release calls if they want without the path being pooled by the Seeker or AstarPath and thus causing strange bugs.
|
package |
Releases the path silently (pooling).
|
protectedvirtual |
Reset all values to their default values.
Reimplemented in ABPath, FloodPath, ConstantPath, RandomPath, FloodPathTracer, and XPath.
|
private |
|
protectedvirtual |
Calls callback to return the calculated path.
Reimplemented in MultiTargetPath, and RandomPath.
|
private |
|
protectedvirtual |
Traces the calculated path from the end node to the start.
This will build an array (path) of the nodes this path will pass through and also set the vectorPath array to the path arrays positions. Assumes the vectorPath and path are empty and not null (which will be the case for a correctly initialized path).
Reimplemented in MultiTargetPath.
IEnumerator WaitForPath | ( | ) |
Waits until this path has been calculated and returned.
Allows for very easy scripting.
System.InvalidOperationException | if the path is not started. Send the path to Seeker.StartPath or AstarPath.StartPath before calling this function. |
|
private |
Additional info on what went wrong.
OnPathDelegate callback |
Callback to call when the path is complete.
This is usually sent to the Seeker component which post processes the path and then calls a callback to the script which requested the path
|
private |
List of claims on this path with reference objects.
|
protected |
The node currently being processed.
|
package |
How long it took to calculate this path in milliseconds.
int enabledTags = -1 |
Which graph tags are traversable.
This is a bitmask so -1 = all bits set = all tags traversable. For example, to set bit 5 to true, you would do
To set it to false, you would do
The Seeker has a popup field where you can set which tags to use.
|
protected |
True if the Reset function has been called.
Used to alert users when they are doing something wrong.
Heuristic heuristic |
Determines which heuristic to use.
float heuristicScale = 1F |
Scale of the heuristic values.
|
protected |
Target to use for H score calculations.
OnPathDelegate immediateCallback |
Immediate callback to call when the path is complete.
|
protected |
The tag penalties that are actually used.
If manualTagPenalties is null, this will be ZeroTagPenalties
|
protected |
Tag penalties set by other scripts.
|
package |
Internal linked list implementation.
NNConstraint nnConstraint = PathNNConstraint.Default |
Constraint for how to search for nodes.
List<GraphNode> path |
Holds the path as a Node array.
All nodes the path traverses. This may not be the same nodes as the post processed path traverses.
|
protected |
Data for the thread calculating this path.
|
private |
|
protected |
Number of nodes this path has searched.
|
private |
ITraversalProvider traversalProvider |
Provides additional traversal information to a path request.
List<Vector3> vectorPath |
Holds the (possibly post processed) path as a Vector3 list.
|
staticprivate |
List of zeroes to use as default tag penalties.
|
getset |
Current state of the path.
|
get |
If the path failed, this is true.
|
get |
Log messages with info about any errors.
|
getpackage |
True for paths that want to search all nodes and not jump over nodes as optimizations.
This disables Jump Point Search when that is enabled to prevent e.g ConstantPath and FloodPath to become completely useless.
|
getprivate |
|
getsetpackage |
ID of this path.
Used to distinguish between different paths
|
getsetpackage |
Returns the state of the path in the pathfinding pipeline.
|
getsetprivate |
True if the path is currently pooled.
Do not set this value. Only read. It is used internally.
|
getpackage |
True if the path is currently recycled (i.e in the path pool).
Do not set this value. Only read. It is used internally.
|
getset |
Penalties for each tag.
Tag 0 which is the default tag, will have added a penalty of tagPenalties[0]. These should only be positive values since the A* algorithm cannot handle negative penalties.