fix many errors, warnings and more, refactoring of naming, remove NODE_SIZE constant from original AStar algorithm

This commit is contained in:
Xevion
2020-11-08 17:58:25 -06:00
parent b4e281f158
commit 335a3d7b7f
10 changed files with 152 additions and 108 deletions
+34 -33
View File
@@ -2,66 +2,67 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Algorithms;
public class AStar : Pathfinding {
private Grid grid;
public class AStar : IPathfinding {
private NodeGrid _nodeGrid;
private Stack<Node> path;
private List<Node> openList;
private List<Node> closedList;
public AStar(Grid grid) {
this.grid = grid;
private Stack<Node> _path;
private List<Node> _openList;
private List<Node> _closedList;
public AStar(NodeGrid nodeGrid) {
this._nodeGrid = nodeGrid;
}
public Tuple<List<Grid>, Stack<Node>> FindPath(Vector2 Start, Vector2 End) {
Node start = new Node(new Vector2((int) (Start.X / Node.NODE_SIZE), (int) (Start.Y / Node.NODE_SIZE)), true);
Node end = new Node(new Vector2((int) (End.X / Node.NODE_SIZE), (int) (End.Y / Node.NODE_SIZE)), true);
public Tuple<List<NodeGrid>, Stack<Node>> FindPath(Vector2 Start, Vector2 End) {
var start = new Node(Start, true);
var end = new Node(End, true);
path = new Stack<Node>();
openList = new List<Node>();
closedList = new List<Node>();
_path = new Stack<Node>();
_openList = new List<Node>();
_closedList = new List<Node>();
Node current = start;
// add start node to Open List
openList.Add(start);
_openList.Add(start);
while (openList.Count != 0 && !closedList.Exists(x => x.Position == end.Position)) {
current = openList[0];
openList.Remove(current);
closedList.Add(current);
List<Node> adjacencies = this.grid.GetAdjacentNodes(current);
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);
foreach (Node n in adjacencies) {
if (!closedList.Contains(n) && n.Walkable) {
if (!openList.Contains(n)) {
foreach (Node n in adjacentNodes) {
if (!_closedList.Contains(n) && n.Walkable) {
if (!_openList.Contains(n)) {
n.Parent = current;
n.DistanceToTarget = Math.Abs(n.Position.X - end.Position.X) +
Math.Abs(n.Position.Y - end.Position.Y);
n.DistanceToTarget = NodeGrid.Manhattan(n, end);
n.Cost = n.Weight + n.Parent.Cost;
openList.Add(n);
openList = openList.OrderBy(node => node.F).ToList<Node>();
_openList.Add(n);
_openList = _openList.OrderBy(node => node.F).ToList<Node>();
}
}
}
}
// construct path, if end was not closed return null
if (!closedList.Exists(x => x.Position == end.Position)) {
if (!_closedList.Exists(x => x.Position == end.Position)) {
return null;
}
// if all good, return path
Node temp = closedList[closedList.IndexOf(current)];
Node temp = _closedList[_closedList.IndexOf(current)];
if (temp == null) return null;
do {
path.Push(temp);
_path.Push(temp);
temp = temp.Parent;
} while (temp != start && temp != null);
return path;
return _path;
}
private StateGrid compileState() {
}
private StateGrid compileState() {}
}