Empeld
Empeld plugin documentation.
essentials.action.Pathing.Engines.AStarEngine Class Reference

The AStar implementation of 3D path-finding More...

Inheritance diagram for essentials.action.Pathing.Engines.AStarEngine:
essentials.action.Pathing.IPathingEngine

Public Member Functions

 AStarEngine (Vector3i start, Vector3i end, IPathCostEngine costEngine, Vector3i[] neighborSet)
 
Path GetResult ()
 Returns the full resulting path. Throws if incomplete More...
 
Path GetIncrementalPath ()
 Gets the currently known best-path to the result. Depending on the implementation, may be called to get the best-path in the case where the overall search has failed. More...
 
void Iterate ()
 Iterate the engine (Call this in a loop until finished) More...
 
Path Compute ()
 Full path compute (Iterate until done). Returns Path or null More...
 

Properties

static readonly Vector3i
 All neighbors More...
 
Vector3i Start [get]
 
Vector3i End [get]
 
PathEngineState State [get]
 
- Properties inherited from essentials.action.Pathing.IPathingEngine
Vector3i Start [get]
 Starting position More...
 
Vector3i End [get]
 Ending position More...
 
PathEngineState State [get]
 The current state of the path resolution More...
 

Detailed Description

The AStar implementation of 3D path-finding

Constructor & Destructor Documentation

◆ AStarEngine()

essentials.action.Pathing.Engines.AStarEngine.AStarEngine ( Vector3i  start,
Vector3i  end,
IPathCostEngine  costEngine,
Vector3i []  neighborSet 
)

Attribute: _start

= new CostNode(_start, _startingHeuristic, 0, null);

72  {
73  _start = start;
74  _end = end;
75  _costEngine = costEngine;
76  _neighbors = neighborSet;
77  _state = PathEngineState.Working;
78 
79  _startingHeuristic = _costEngine.GetHeuristic(_start, _end);
80  _openNodes[_start] = new CostNode(_start, _startingHeuristic, 0, null);
81  }
PathEngineState
Definition: PathEngineState.cs:5
int GetHeuristic(Vector3i src, Vector3i dst)
Gets the heuristic cost between two points (to better predict which way to search) ...

Member Function Documentation

◆ Compute()

Path essentials.action.Pathing.Engines.AStarEngine.Compute ( )

Full path compute (Iterate until done). Returns Path or null

Returns
The compute.

Implements essentials.action.Pathing.IPathingEngine.

169  {
170  while(_state == PathEngineState.Working)
171  {
172  Iterate();
173  }
174 
175  if (_state == PathEngineState.Complete)
176  {
177  return GetResult();
178  }
179  return null;
180  }
PathEngineState
Definition: PathEngineState.cs:5
void Iterate()
Iterate the engine (Call this in a loop until finished)
Definition: AStarEngine.cs:117
Path GetResult()
Returns the full resulting path. Throws if incomplete
Definition: AStarEngine.cs:83

◆ GetIncrementalPath()

Path essentials.action.Pathing.Engines.AStarEngine.GetIncrementalPath ( )

Gets the currently known best-path to the result. Depending on the implementation, may be called to get the best-path in the case where the overall search has failed.

Returns
The incremental path.

Implements essentials.action.Pathing.IPathingEngine.

94  {
95  var path = new List<Vector3i>();
96  var current = GetCurrentNode();
97  do
98  {
99  path.Add(current.Point);
100  current = current.Parent;
101  } while(current != null);
102  path.Reverse();
103  return new Path(path);
104  }

◆ GetResult()

Path essentials.action.Pathing.Engines.AStarEngine.GetResult ( )

Returns the full resulting path. Throws if incomplete

Exceptions
PathEngineException
Returns
The result.

Implements essentials.action.Pathing.IPathingEngine.

84  {
85  if (_state != PathEngineState.Complete)
86  {
87  throw new PathEngineException("Uncomplete pathing");
88  }
89 
90  return GetIncrementalPath();
91  }
PathEngineState
Definition: PathEngineState.cs:5
Path GetIncrementalPath()
Gets the currently known best-path to the result. Depending on the implementation, may be called to get the best-path in the case where the overall search has failed.
Definition: AStarEngine.cs:93

◆ Iterate()

void essentials.action.Pathing.Engines.AStarEngine.Iterate ( )

Iterate the engine (Call this in a loop until finished)

Attribute: newPoint

.ActualCost)

Attribute: newPoint

= new CostNode(newPoint, heuristicCost, newCost, current);

Implements essentials.action.Pathing.IPathingEngine.

118  {
119  if (_state != PathEngineState.Working)
120  return;
121 
122  if (_openNodes.Count == 0)
123  {
124  _state = PathEngineState.Failed;
125  return;
126  }
127 
128  var current = GetCurrentNode();
129  if (current.Point == _end)
130  {
131  _state = PathEngineState.Complete;
132  return;
133  }
134 
135  if (current.HeuristicCost >= _startingHeuristic * 2 && _iterationCount > 50)
136  {
137  _state = PathEngineState.Failed;
138  return;
139  }
140 
141  _openNodes.Remove(current.Point);
142  _closedPoints.Add(current.Point);
143 
144  foreach(var offset in _neighbors)
145  {
146  var newPoint = current.Point + offset;
147  if (_closedPoints.Contains(newPoint))
148  continue;
149 
150  int moveCost = _costEngine.GetActualCost(current.Point, newPoint);
151  if (moveCost < 0)
152  {
153  _closedPoints.Add(newPoint);
154  continue;
155  }
156 
157  int newCost = current.ActualCost + moveCost;
158  if (!_openNodes.ContainsKey(newPoint) || newCost < _openNodes[newPoint].ActualCost)
159  {
160  int heuristicCost = newCost + _costEngine.GetHeuristic(newPoint, _end);
161  _openNodes[newPoint] = new CostNode(newPoint, heuristicCost, newCost, current);
162  }
163  }
164 
165  _iterationCount++;
166  }
PathEngineState
Definition: PathEngineState.cs:5
int GetHeuristic(Vector3i src, Vector3i dst)
Gets the heuristic cost between two points (to better predict which way to search) ...
int GetActualCost(Vector3i src, Vector3i dst)
Gets the actual cost traversing between two points If less than 0, treated as impassible. Otherwise cost is relative

Property Documentation

◆ End

Vector3i essentials.action.Pathing.Engines.AStarEngine.End
get

◆ Start

Vector3i essentials.action.Pathing.Engines.AStarEngine.Start
get

◆ State

PathEngineState essentials.action.Pathing.Engines.AStarEngine.State
get

◆ Vector3i

static readonly essentials.action.Pathing.Engines.AStarEngine.Vector3i
static

All neighbors

Only neighbors that are not on the diagnol (Except Z axis, since step-up and step-down)

Attribute: ] NEIGHBORS_ALL = new Vector3i[

Attribute: ] NEIGHBORS_AXIS_ALIGNED = new Vector3i[


The documentation for this class was generated from the following file: