-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathAStarPathFinder.java
More file actions
283 lines (232 loc) · 9.73 KB
/
AStarPathFinder.java
File metadata and controls
283 lines (232 loc) · 9.73 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package org.andengine.util.algorithm.path.astar;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.andengine.util.adt.list.ShiftList;
import org.andengine.util.adt.map.LongSparseArray;
import org.andengine.util.adt.queue.IQueue;
import org.andengine.util.adt.queue.SortedQueue;
import org.andengine.util.adt.spatial.bounds.util.IntBoundsUtils;
import org.andengine.util.algorithm.path.ICostFunction;
import org.andengine.util.algorithm.path.IPathFinder;
import org.andengine.util.algorithm.path.IPathFinderMap;
import org.andengine.util.algorithm.path.Path;
/**
* TODO Nodes could be recycle in a pool.
*
* (c) 2010 Nicolas Gramlich
* (c) 2011 Zynga Inc.
*
* @author Nicolas Gramlich
* @author Greg Haynes
* @since 23:16:17 - 16.08.2010
*/
public class AStarPathFinder<T> implements IPathFinder<T> {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Getter & Setter
// ===========================================================
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public Path findPath(final IPathFinderMap<T> pPathFinderMap, final int pXMin, final int pYMin, final int pXMax, final int pYMax, final T pEntity, final int pFromX, final int pFromY, final int pToX, final int pToY, final boolean pAllowDiagonal, final IAStarHeuristic<T> pAStarHeuristic, final ICostFunction<T> pCostFunction) {
return this.findPath(pPathFinderMap, pXMin, pYMin, pXMax, pYMax, pEntity, pFromX, pFromY, pToX, pToY, pAllowDiagonal, pAStarHeuristic, pCostFunction, Float.MAX_VALUE);
}
@Override
public Path findPath(final IPathFinderMap<T> pPathFinderMap, final int pXMin, final int pYMin, final int pXMax, final int pYMax, final T pEntity, final int pFromX, final int pFromY, final int pToX, final int pToY, final boolean pAllowDiagonal, final IAStarHeuristic<T> pAStarHeuristic, final ICostFunction<T> pCostFunction, final float pMaxCost) {
return this.findPath(pPathFinderMap, pXMin, pYMin, pXMax, pYMax, pEntity, pFromX, pFromY, pToX, pToY, pAllowDiagonal, pAStarHeuristic, pCostFunction, pMaxCost, null);
}
@Override
public Path findPath(final IPathFinderMap<T> pPathFinderMap, final int pXMin, final int pYMin, final int pXMax, final int pYMax, final T pEntity, final int pFromX, final int pFromY, final int pToX, final int pToY, final boolean pAllowDiagonal, final IAStarHeuristic<T> pAStarHeuristic, final ICostFunction<T> pCostFunction, final float pMaxCost, final IPathFinderListener<T> pPathFinderListener) {
if(((pFromX == pToX) && (pFromY == pToY)) || pPathFinderMap.isBlocked(pFromX, pFromY, pEntity) || pPathFinderMap.isBlocked(pToX, pToY, pEntity)) {
return null;
}
/* Drag some fields to local variables. */
final Node fromNode = new Node(pFromX, pFromY, pAStarHeuristic.getExpectedRestCost(pPathFinderMap, pEntity, pFromX, pFromY, pToX, pToY));
final long fromNodeID = fromNode.mID;
final long toNodeID = Node.calculateID(pToX, pToY);
final LongSparseArray<Node> visitedNodes = new LongSparseArray<Node>();
final LongSparseArray<Node> openNodes = new LongSparseArray<Node>();
final IQueue<Node> sortedOpenNodes = new SortedQueue<Node>(new ShiftList<Node>());
final boolean allowDiagonalMovement = pAllowDiagonal;
/* Initialize algorithm. */
openNodes.put(fromNodeID, fromNode);
sortedOpenNodes.enter(fromNode);
Node currentNode = null;
while(openNodes.size() > 0) {
/* The first Node in the open list is the one with the lowest cost. */
currentNode = sortedOpenNodes.poll();
final long currentNodeID = currentNode.mID;
if(currentNodeID == toNodeID) {
break;
}
visitedNodes.put(currentNodeID, currentNode);
/* Loop over all neighbors of this position. */
for(int dX = -1; dX <= 1; dX++) {
for(int dY = -1; dY <= 1; dY++) {
if((dX == 0) && (dY == 0)) {
continue;
}
if(!allowDiagonalMovement && (dX != 0) && (dY != 0)) {
continue;
}
final int neighborNodeX = dX + currentNode.mX;
final int neighborNodeY = dY + currentNode.mY;
final long neighborNodeID = Node.calculateID(neighborNodeX, neighborNodeY);
if(!IntBoundsUtils.contains(pXMin, pYMin, pXMax, pYMax, neighborNodeX, neighborNodeY) || pPathFinderMap.isBlocked(neighborNodeX, neighborNodeY, pEntity)) {
continue;
}
if(visitedNodes.indexOfKey(neighborNodeID) >= 0) {
continue;
}
Node neighborNode = openNodes.get(neighborNodeID);
final boolean neighborNodeIsNew;
/* Check if neighbor exists. */
if(neighborNode == null) {
neighborNodeIsNew = true;
neighborNode = new Node(neighborNodeX, neighborNodeY, pAStarHeuristic.getExpectedRestCost(pPathFinderMap, pEntity, neighborNodeX, neighborNodeY, pToX, pToY));
} else {
neighborNodeIsNew = false;
}
/* Update cost of neighbor as cost of current plus step from current to neighbor. */
final float costFromCurrentToNeigbor = pCostFunction.getCost(pPathFinderMap, currentNode.mX, currentNode.mY, neighborNodeX, neighborNodeY, pEntity);
final float neighborNodeCost = currentNode.mCost + costFromCurrentToNeigbor;
if(neighborNodeCost > pMaxCost) {
/* Too expensive -> remove if isn't a new node. */
if(!neighborNodeIsNew) {
openNodes.remove(neighborNodeID);
}
} else {
neighborNode.setParent(currentNode, costFromCurrentToNeigbor);
if(neighborNodeIsNew) {
openNodes.put(neighborNodeID, neighborNode);
} else {
/* Remove so that re-insertion puts it to the correct spot. */
sortedOpenNodes.remove(neighborNode);
}
sortedOpenNodes.enter(neighborNode);
if(pPathFinderListener != null) {
pPathFinderListener.onVisited(pEntity, neighborNodeX, neighborNodeY);
}
}
}
}
}
/* Cleanup. */
// TODO We could just let the GC do its work.
visitedNodes.clear();
openNodes.clear();
sortedOpenNodes.clear();
/* Check if a path was found. */
if(currentNode.mID != toNodeID) {
return null;
}
/* Calculate path length. */
int length = 1;
Node tmp = currentNode;
while(tmp.mID != fromNodeID) {
tmp = tmp.mParent;
length++;
}
/* Traceback path. */
final Path path = new Path(length);
int index = length - 1;
tmp = currentNode;
while(tmp.mID != fromNodeID) {
path.set(index, tmp.mX, tmp.mY);
tmp = tmp.mParent;
index--;
}
path.set(0, pFromX, pFromY);
return path;
}
// ===========================================================
// Methods
// ===========================================================
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
public static final class Node implements Comparable<Node> {
// ===========================================================
// Constants
// ===========================================================
// ===========================================================
// Fields
// ===========================================================
/* package */ Node mParent;
/* package */ final int mX;
/* package */ final int mY;
/* package */ final long mID;
/* package */ final float mExpectedRestCost;
/* package */ float mCost;
/* package */ float mTotalCost;
// ===========================================================
// Constructors
// ===========================================================
public Node(final int pX, final int pY, final float pExpectedRestCost) {
this.mX = pX;
this.mY = pY;
this.mExpectedRestCost = pExpectedRestCost;
this.mID = Node.calculateID(pX, pY);
}
// ===========================================================
// Getter & Setter
// ===========================================================
public void setParent(final Node pParent, final float pCost) {
this.mParent = pParent;
this.mCost = pCost;
this.mTotalCost = pCost + this.mExpectedRestCost;
}
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public int compareTo(final Node pNode) {
final float diff = this.mTotalCost - pNode.mTotalCost;
if(diff > 0) {
return 1;
} else if(diff < 0) {
return -1;
} else {
return 0;
}
}
@Override
public boolean equals(final Object pOther) {
if(this == pOther) {
return true;
} else if(pOther == null) {
return false;
} else if(this.getClass() != pOther.getClass()) {
return false;
}
return this.equals((Node)pOther);
}
@Override
public String toString() {
return this.getClass().getSimpleName() + " [x=" + this.mX + ", y=" + this.mY + "]";
}
// ===========================================================
// Methods
// ===========================================================
public static long calculateID(final int pX, final int pY) {
final ByteBuffer byteBuffer = ByteBuffer.allocateDirect(16);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
return byteBuffer.putInt(pX).putInt(pY).getLong(0);
}
public boolean equals(final Node pNode) {
return this.mID == pNode.mID;
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
}