-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAstar.cs
159 lines (144 loc) · 4.54 KB
/
Astar.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;
namespace AStarSharp
{
public class Node
{
// Change this depending on what the desired size is for each element in the grid
public static int NODE_SIZE = 32;
public Node Parent;
public Vector2 Position;
public Vector2 Center
{
get
{
return new Vector2(Position.X + NODE_SIZE / 2, Position.Y + NODE_SIZE / 2);
}
}
public float DistanceToTarget;
public float Cost;
public float Weight;
public float F
{
get
{
if (DistanceToTarget != -1 && Cost != -1)
return DistanceToTarget + Cost;
else
return -1;
}
}
public bool Walkable;
public Node(Vector2 pos, bool walkable, float weight = 1)
{
Parent = null;
Position = pos;
DistanceToTarget = -1;
Cost = 1;
Weight = weight;
Walkable = walkable;
}
}
public class Astar
{
List<List<Node>> Grid;
int GridRows
{
get
{
return Grid[0].Count;
}
}
int GridCols
{
get
{
return Grid.Count;
}
}
public Astar(List<List<Node>> grid)
{
Grid = grid;
}
public 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);
Stack<Node> Path = new Stack<Node>();
PriorityQueue<Node, float> OpenList = new PriorityQueue<Node,float>();
List<Node> ClosedList = new List<Node>();
List<Node> adjacencies;
Node current = start;
// add start node to Open List
OpenList.Enqueue(start, start.F);
while(OpenList.Count != 0 && !ClosedList.Exists(x => x.Position == end.Position))
{
current = OpenList.Dequeue();
ClosedList.Add(current);
adjacencies = GetAdjacentNodes(current);
foreach(Node n in adjacencies)
{
if (!ClosedList.Contains(n) && n.Walkable)
{
bool isFound = false;
foreach (var oLNode in OpenList.UnorderedItems)
{
if (oLNode.Element == n)
{
isFound = true;
}
}
if (!isFound)
{
n.Parent = current;
n.DistanceToTarget = Math.Abs(n.Position.X - end.Position.X) + Math.Abs(n.Position.Y - end.Position.Y);
n.Cost = n.Weight + n.Parent.Cost;
OpenList.Enqueue(n, n.F);
}
}
}
}
// 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;
}
private List<Node> GetAdjacentNodes(Node n)
{
List<Node> temp = new List<Node>();
int row = (int)n.Position.Y;
int col = (int)n.Position.X;
if(row + 1 < GridRows)
{
temp.Add(Grid[col][row + 1]);
}
if(row - 1 >= 0)
{
temp.Add(Grid[col][row - 1]);
}
if(col - 1 >= 0)
{
temp.Add(Grid[col - 1][row]);
}
if(col + 1 < GridCols)
{
temp.Add(Grid[col + 1][row]);
}
return temp;
}
}
}