implement GridState code with accompanying refactoring, general fixes and cleanup, GetNode, IsValid, RecordState, GetStates etc.

This commit is contained in:
Xevion
2020-11-08 23:28:27 -06:00
parent 3608f4f460
commit 2d5938415e
5 changed files with 190 additions and 93 deletions
+80 -49
View File
@@ -1,68 +1,99 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Algorithms;
public class AStar : IPathfinding {
private NodeGrid _nodeGrid;
namespace Algorithms {
public class AStar : IPathfinding {
private NodeGrid _nodeGrid;
private Stack<Node> _path;
private List<Node> _openList;
private List<Node> _closedList;
public AStar(NodeGrid nodeGrid) {
this._nodeGrid = nodeGrid;
}
private Stack<Node> _path;
private List<Node> _openList;
private List<Node> _closedList;
private List<GridState> _states;
public Tuple<List<NodeGrid>, Stack<Node>> FindPath(Vector2 Start, Vector2 End) {
var start = new Node(Start, true);
var end = new Node(End, true);
private Vector2 _start;
private Vector2 _end;
_path = new Stack<Node>();
_openList = new List<Node>();
_closedList = new List<Node>();
Node current = start;
public AStar(NodeGrid nodeGrid) {
this._nodeGrid = nodeGrid;
_states = new List<GridState>();
}
// add start node to Open List
_openList.Add(start);
public Stack<Node> FindPath(Vector2 Start, Vector2 End) {
this._start = Start;
this._end = End;
while (_openList.Count != 0 && !_closedList.Exists(x => x.Position == end.Position)) {
current = _openList[0];
_openList.Remove(current);
_closedList.Add(current);
List<Node> adjacentNodes = this._nodeGrid.GetAdjacentNodes(current);
var start = new Node(Start, true);
var end = new Node(End, true);
RecordState();
_path = new Stack<Node>();
_openList = new List<Node>();
_closedList = new List<Node>();
Node current = start;
foreach (Node n in adjacentNodes) {
if (!_closedList.Contains(n) && n.Walkable) {
if (!_openList.Contains(n)) {
n.Parent = current;
n.DistanceToTarget = NodeGrid.Manhattan(n, end);
n.Cost = n.Weight + n.Parent.Cost;
_openList.Add(n);
_openList = _openList.OrderBy(node => node.F).ToList<Node>();
// add start node to Open List
_openList.Add(start);
while (_openList.Count != 0 && !_closedList.Exists(x => x.Position == end.Position)) {
current = _openList[0];
_openList.Remove(current);
_closedList.Add(current);
RecordState();
IEnumerable<Node> adjacentNodes = this._nodeGrid.GetAdjacentNodes(current);
foreach (Node n in adjacentNodes) {
if (!_closedList.Contains(n) && n.Walkable) {
if (!_openList.Contains(n)) {
n.Parent = current;
n.DistanceToTarget = NodeGrid.Manhattan(n, end);
n.Cost = n.Weight + n.Parent.Cost;
_openList.Add(n);
_openList = _openList.OrderBy(node => node.F).ToList();
RecordState();
}
}
}
}
// construct path, if end was not closed return null
if (!_closedList.Exists(x => x.Position == end.Position)) {
return null;
}
// if all good, return path
Node temp = _closedList[_closedList.IndexOf(current)];
if (temp == null) return null;
do {
_path.Push(temp);
temp = temp.Parent;
} while (temp != start && temp != null);
return _path;
}
// construct path, if end was not closed return null
if (!_closedList.Exists(x => x.Position == end.Position)) {
return null;
/// <summary>
/// Records the current state of the pathfinding algorithm in the grid.
/// </summary>
public void RecordState() {
// TODO: Record basic grid node types
// TODO: Record timing information
// TODO: Record pathfinding state information (stages, heuristic, statistical info)
this._states.Add(
new GridState(this._nodeGrid, this._openList, this._closedList, _start, _end, _path)
);
}
// if all good, return path
Node temp = _closedList[_closedList.IndexOf(current)];
if (temp == null) return null;
do {
_path.Push(temp);
temp = temp.Parent;
} while (temp != start && temp != null);
return _path;
}
private StateGrid compileState() {
/// <summary>
/// Returns the current list of grid states of the pathfinding algorithm.
/// </summary>
/// <returns>A list of GridState objects representing the pathfinding algorithm's progress</returns>
public List<GridState> GetStates() {
return this._states;
}
}
}