-
Notifications
You must be signed in to change notification settings - Fork 1
/
AStarPathfinder.cs
189 lines (172 loc) · 6.21 KB
/
AStarPathfinder.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using Priority_Queue;
using System;
using System.Collections.Generic;
using System.Threading;
public class AStarPathfinder<T> where T : class, IAstarNode<T>
{
private const int MUTEX_TIMEOUT = 5000;
private FastPriorityQueue<DataNode<T>> orderedTestList;
private static AStarPathfinder<T> _instance;
private DataSet<T>[] fullDataTraveled=null;
private Mutex mut = new Mutex();
private IComparer<DataNode<T>> Comparer;
/// <summary>
/// Get an instance of the pathfinder. The tile should be passed as a function
/// </summary>
public static AStarPathfinder<T> Instance
{
get{
if (_instance== null)
{
_instance = new AStarPathfinder<T>();
}
return _instance;
}
}
private AStarPathfinder()
{
orderedTestList = new FastPriorityQueue<DataNode<T>>(16);
Comparer = Comparer<DataNode<T>>.Default;
}
/// <summary>
/// Finds the distance to a given tile, given A* processing
/// </summary>
/// <param name="cur">The start tile</param>
/// <param name="dest">The end tile</param>
/// <returns></returns>
public int DistanceTo(T cur, T dest, float tweakParam)
{
if (mut.WaitOne(MUTEX_TIMEOUT))
{
int ret= PopulatePath(cur, dest, tweakParam).distTraveled;
mut.ReleaseMutex();
return ret;
} else
{
throw new TimeoutException("Failed to obtain mutex lock");
}
}
/// <summary>
/// Find a path from the current tile to the destination. Assumes there is a path to the destination
/// </summary>
/// <param name="cur">The start tile</param>
/// <param name="dest">The end tile</param>
/// <returns></returns>
public List<T> FindPath(T cur, T dest,float tweakParam)
{
if (mut.WaitOne(MUTEX_TIMEOUT))
{
DataSet<T> curTest = PopulatePath(cur, dest, tweakParam);
List<T> ret = new List<T>();
while (curTest.prevId != -1)
{
ret.Insert(0, curTest.current);
curTest = fullDataTraveled[curTest.prevId];
}
mut.ReleaseMutex();
return ret;
}
else
{
throw new TimeoutException("Failed to obtain mutex lock");
}
}
/// <summary>
/// Populates the PathInfo from the current tile to the destination. Assumes there is a path to the destination
/// </summary>
/// <param name="cur">The start tile</param>
/// <param name="dest">The end tile</param>
/// <returns>Populated PathInfo</returns>
public PathInfo<T> FindPathInfo(T cur, T dest, float tweakParam)
{
if (mut.WaitOne(MUTEX_TIMEOUT))
{
DataSet<T> curTest = PopulatePath(cur, dest, tweakParam);
List<T> path = new List<T>();
List<int> distance = new List<int>();
while (curTest.prevId != -1)
{
distance.Add(curTest.distTraveled);
path.Insert(0, curTest.current);
curTest = fullDataTraveled[curTest.prevId];
}
distance.Reverse();
PathInfo<T> ret = new PathInfo<T>(path, distance.ToArray());
mut.ReleaseMutex();
return ret;
}
else
{
throw new TimeoutException("Failed to obtain mutex lock");
}
}
int[] heuristics=new int[0];
int[] blankHeuristics;
DataSet<T>[] blankTraveled;
private DataSet<T> PopulatePath(T cur, T dest, float tweakParam)
{
orderedTestList.Clear();
if (heuristics.Length != cur.MaxAStarIndex)
{
fullDataTraveled = new DataSet<T>[cur.MaxAStarIndex];
blankTraveled = new DataSet<T>[cur.MaxAStarIndex];
} else
{
Array.Copy(blankTraveled, fullDataTraveled, cur.MaxAStarIndex);
}
Tuple<T, int>[] set;
if (heuristics.Length != cur.MaxAStarIndex)
{
heuristics = new int[cur.MaxAStarIndex];
blankHeuristics = new int[cur.MaxAStarIndex];
} else
{
Array.Copy(blankHeuristics, heuristics, cur.MaxAStarIndex);
}
DataSet<T> curTest = new DataSet<T>(cur, -1, 0, 0);
fullDataTraveled[cur.AStarIndex]= curTest;
int heuristic;
int distanceTo;
while (curTest.current.AStarIndex != dest.AStarIndex)
{
set = curTest.current.GetNeighborsAstarDistance(tweakParam);
foreach (Tuple<T,int> neighbor in set)
{
distanceTo = curTest.distTraveled + neighbor.Second;
if (heuristics[neighbor.First.AStarIndex]>0)
{
heuristic = heuristics[neighbor.First.AStarIndex];
}
else
{
heuristic = neighbor.First.GetAstarHeuristic(dest, tweakParam);
heuristics[neighbor.First.AStarIndex]= heuristic;
}
if (fullDataTraveled[neighbor.First.AStarIndex]!=null)
{
//A quicker path was found to the tile
if (distanceTo + heuristic< fullDataTraveled[neighbor.First.AStarIndex].node.Priority)
{
orderedTestList.UpdatePriority(fullDataTraveled[neighbor.First.AStarIndex].node, distanceTo + heuristic);
fullDataTraveled[neighbor.First.AStarIndex].Repopulate(curTest.current.AStarIndex, distanceTo);
}
}
else
{
fullDataTraveled[neighbor.First.AStarIndex] = new DataSet<T>(neighbor.First, curTest.current.AStarIndex, distanceTo, distanceTo + heuristic);
Enqueue(fullDataTraveled[neighbor.First.AStarIndex].node);
}
}
curTest = fullDataTraveled[orderedTestList.Dequeue().index];
}
return curTest;
}
private void Enqueue(DataNode<T> ds)
{
if (orderedTestList.Count==orderedTestList.MaxSize)
{
orderedTestList.Resize(orderedTestList.MaxSize * 2);
}
orderedTestList.Enqueue(ds, ds.Priority);
}
}