mirror of
https://github.com/Xevion/Paths.git
synced 2026-01-31 02:25:06 -06:00
implement GridState code with accompanying refactoring, general fixes and cleanup, GetNode, IsValid, RecordState, GetStates etc.
This commit is contained in:
@@ -1,24 +1,33 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using Algorithms;
|
|
||||||
|
|
||||||
public class AStar : IPathfinding {
|
namespace Algorithms {
|
||||||
|
public class AStar : IPathfinding {
|
||||||
private NodeGrid _nodeGrid;
|
private NodeGrid _nodeGrid;
|
||||||
|
|
||||||
private Stack<Node> _path;
|
private Stack<Node> _path;
|
||||||
private List<Node> _openList;
|
private List<Node> _openList;
|
||||||
private List<Node> _closedList;
|
private List<Node> _closedList;
|
||||||
|
private List<GridState> _states;
|
||||||
|
|
||||||
|
private Vector2 _start;
|
||||||
|
private Vector2 _end;
|
||||||
|
|
||||||
public AStar(NodeGrid nodeGrid) {
|
public AStar(NodeGrid nodeGrid) {
|
||||||
this._nodeGrid = nodeGrid;
|
this._nodeGrid = nodeGrid;
|
||||||
|
_states = new List<GridState>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tuple<List<NodeGrid>, Stack<Node>> FindPath(Vector2 Start, Vector2 End) {
|
public Stack<Node> FindPath(Vector2 Start, Vector2 End) {
|
||||||
|
this._start = Start;
|
||||||
|
this._end = End;
|
||||||
|
|
||||||
var start = new Node(Start, true);
|
var start = new Node(Start, true);
|
||||||
var end = new Node(End, true);
|
var end = new Node(End, true);
|
||||||
|
|
||||||
|
RecordState();
|
||||||
|
|
||||||
_path = new Stack<Node>();
|
_path = new Stack<Node>();
|
||||||
_openList = new List<Node>();
|
_openList = new List<Node>();
|
||||||
_closedList = new List<Node>();
|
_closedList = new List<Node>();
|
||||||
@@ -31,8 +40,9 @@ public class AStar : IPathfinding {
|
|||||||
current = _openList[0];
|
current = _openList[0];
|
||||||
_openList.Remove(current);
|
_openList.Remove(current);
|
||||||
_closedList.Add(current);
|
_closedList.Add(current);
|
||||||
List<Node> adjacentNodes = this._nodeGrid.GetAdjacentNodes(current);
|
RecordState();
|
||||||
|
|
||||||
|
IEnumerable<Node> adjacentNodes = this._nodeGrid.GetAdjacentNodes(current);
|
||||||
|
|
||||||
foreach (Node n in adjacentNodes) {
|
foreach (Node n in adjacentNodes) {
|
||||||
if (!_closedList.Contains(n) && n.Walkable) {
|
if (!_closedList.Contains(n) && n.Walkable) {
|
||||||
@@ -40,8 +50,11 @@ public class AStar : IPathfinding {
|
|||||||
n.Parent = current;
|
n.Parent = current;
|
||||||
n.DistanceToTarget = NodeGrid.Manhattan(n, end);
|
n.DistanceToTarget = NodeGrid.Manhattan(n, end);
|
||||||
n.Cost = n.Weight + n.Parent.Cost;
|
n.Cost = n.Weight + n.Parent.Cost;
|
||||||
|
|
||||||
_openList.Add(n);
|
_openList.Add(n);
|
||||||
_openList = _openList.OrderBy(node => node.F).ToList<Node>();
|
_openList = _openList.OrderBy(node => node.F).ToList();
|
||||||
|
|
||||||
|
RecordState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,6 +76,24 @@ public class AStar : IPathfinding {
|
|||||||
return _path;
|
return _path;
|
||||||
}
|
}
|
||||||
|
|
||||||
private StateGrid compileState() {
|
/// <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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,14 +2,12 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using Algorithms;
|
|
||||||
using NUnit.Framework.Constraints;
|
|
||||||
using UnityEngine.SocialPlatforms;
|
|
||||||
|
|
||||||
public class NodeGrid {
|
namespace Algorithms {
|
||||||
|
public class NodeGrid {
|
||||||
private List<List<Node>> grid;
|
private List<List<Node>> grid;
|
||||||
private readonly int _width;
|
public readonly int Width;
|
||||||
private readonly int _height;
|
public readonly int Height;
|
||||||
|
|
||||||
public NodeGrid(int width, int height) {
|
public NodeGrid(int width, int height) {
|
||||||
if (width <= 0)
|
if (width <= 0)
|
||||||
@@ -29,32 +27,51 @@ public class NodeGrid {
|
|||||||
this.grid.Add(list);
|
this.grid.Add(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._width = width;
|
this.Width = width;
|
||||||
this._height = height;
|
this.Height = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NodeGrid(List<List<Node>> grid) {
|
public NodeGrid(List<List<Node>> grid) {
|
||||||
this.grid = grid;
|
this.grid = grid;
|
||||||
|
|
||||||
this._height = this.grid[0].Count;
|
this.Height = this.grid[0].Count;
|
||||||
this._width = this.grid.Count;
|
this.Width = this.grid.Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Node> GetAdjacentNodes(Node node) {
|
public IEnumerable<Node> GetAdjacentNodes(Node node) {
|
||||||
List<Node> temp = new List<Node>();
|
List<Node> temp = new List<Node>();
|
||||||
|
|
||||||
int row = (int) node.Position.Y;
|
int row = (int) node.Position.Y;
|
||||||
int col = (int) node.Position.X;
|
int col = (int) node.Position.X;
|
||||||
|
|
||||||
if (row + 1 < _height) temp.Add(this.grid[col][row + 1]);
|
if (row + 1 < Height) temp.Add(this.grid[col][row + 1]);
|
||||||
if (row - 1 >= 0) temp.Add(this.grid[col][row - 1]);
|
if (row - 1 >= 0) temp.Add(this.grid[col][row - 1]);
|
||||||
if (col - 1 >= 0) temp.Add(this.grid[col - 1][row]);
|
if (col - 1 >= 0) temp.Add(this.grid[col - 1][row]);
|
||||||
if (col + 1 < _width) temp.Add(this.grid[col + 1][row]);
|
if (col + 1 < Width) temp.Add(this.grid[col + 1][row]);
|
||||||
|
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsValid(int x, int y) {
|
||||||
|
return x > 0 && x < this.Width && y > 0 && y < this.Height;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves a node at a given coordinate.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="x">the X (column) coordinate</param>
|
||||||
|
/// <param name="y">the Y (row) coordinate</param>
|
||||||
|
/// <returns>A Node object</returns>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException">when the coordinate given does not exist on the grid</exception>
|
||||||
|
public Node GetNode(int x, int y) {
|
||||||
|
if(!IsValid(x, y))
|
||||||
|
throw new ArgumentOutOfRangeException();
|
||||||
|
|
||||||
|
return this.grid[x][y];
|
||||||
|
}
|
||||||
|
|
||||||
public static float Manhattan(Node first, Node second) {
|
public static float Manhattan(Node first, Node second) {
|
||||||
return Math.Abs(first.Position.X - second.Position.X) + Math.Abs(first.Position.Y - second.Position.Y);
|
return Math.Abs(first.Position.X - second.Position.X) + Math.Abs(first.Position.Y - second.Position.Y);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Numerics;
|
||||||
|
using Algorithms;
|
||||||
|
|
||||||
|
public class GridState {
|
||||||
|
private List<List<GridNodeType>> _grid;
|
||||||
|
|
||||||
|
public GridState(NodeGrid grid, IEnumerable<Node> seen, IEnumerable<Node> expanded, Vector2 start, Vector2 end, IReadOnlyCollection<Node> path) {
|
||||||
|
this._grid = new List<List<GridNodeType>>(grid.Width);
|
||||||
|
|
||||||
|
// Add walls and empty tiles
|
||||||
|
foreach (var x in Enumerable.Range(0, grid.Width - 1)) {
|
||||||
|
this._grid.Add(new List<GridNodeType>(grid.Height));
|
||||||
|
foreach (var y in Enumerable.Range(0, grid.Height - 1)) {
|
||||||
|
Node node = grid.GetNode(x, y);
|
||||||
|
this._grid[x].Add(!node.Walkable ? GridNodeType.Wall : GridNodeType.Empty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add 'seen' tiles
|
||||||
|
foreach (Node seenNode in seen) {
|
||||||
|
this._grid[(int) seenNode.Position.X][(int) seenNode.Position.Y] = GridNodeType.Seen;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add 'expanded' tiles
|
||||||
|
foreach (Node expandedNode in expanded) {
|
||||||
|
this._grid[(int) expandedNode.Position.X][(int) expandedNode.Position.Y] = GridNodeType.Expanded;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set start and end tiles
|
||||||
|
this._grid[(int) start.X][(int) start.Y] = GridNodeType.Start;
|
||||||
|
this._grid[(int) end.X][(int) end.Y] = GridNodeType.End;
|
||||||
|
|
||||||
|
// Add 'path' tiles
|
||||||
|
if (path != null)
|
||||||
|
foreach (Node pathNode in path)
|
||||||
|
this._grid[(int) pathNode.Position.X][(int) pathNode.Position.Y] = GridNodeType.Path;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1a26aa7e514043e8911382088176258b
|
||||||
|
timeCreated: 1604887099
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using Algorithms;
|
using Algorithms;
|
||||||
|
|
||||||
@@ -13,5 +12,12 @@ public interface IPathfinding {
|
|||||||
/// <param name="start">The position from which pathfinding begins</param>
|
/// <param name="start">The position from which pathfinding begins</param>
|
||||||
/// <param name="end">The position trying to be found via pathfinding</param>
|
/// <param name="end">The position trying to be found via pathfinding</param>
|
||||||
/// <returns>A List of NodeGridGrid objects representing the timeline of Pathfinding</returns>
|
/// <returns>A List of NodeGridGrid objects representing the timeline of Pathfinding</returns>
|
||||||
Tuple<List<NodeGrid>, Stack<Node>> FindPath(Vector2 start, Vector2 end);
|
Stack<Node> FindPath(Vector2 start, Vector2 end);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Records the current state of the pathfinding algorithm. Internal usage only.
|
||||||
|
/// </summary>
|
||||||
|
void RecordState();
|
||||||
|
|
||||||
|
List<GridState> GetStates();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user