-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathZNavSearch.zs
380 lines (316 loc) · 8.59 KB
/
ZNavSearch.zs
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
Binary heap ported from
https://github.com/donmccurdy/three-pathfinding/blob/main/src/BinaryHeap.js
this is just a utility that makes searching nodes faster
you should have zero reason to change this in any way
*/
Class ZNavBinaryHeap : ZNav
{
array<ZNavNode>content;
virtual int scoreFunction(ZNavNode element)
{
return element.f;
}
void push (ZNavNode element)
{
self.content.push(element);
self.sinkDown( self.content.size() - 1 );
}
void clear()
{
self.content.clear();
}
ZNavNode pop()
{
ZNavNode result = self.content[0];
ZNavNode last = self.content[self.content.size() - 1];
self.content.pop();
if ( self.content.size() > 0 )
{
self.content[0] = last;
bubbleUp(0);
}
return result;
}
void remove ( ZNavNode element )
{
int index = content.find(element);
ZNavNode last = self.content[self.content.size() - 1];
self.content.pop();
if ( index == (content.size() - 1) )
{
content[index] = last;
if ( scoreFunction( last ) < scoreFunction( element ) )
{
sinkDown(index);
} else {
bubbleUp(index);
}
}
}
void rescoreElement ( ZNavNode element )
{
int index = self.content.find(element);
sinkDown(index);
}
int size()
{
return self.content.size();
}
void sinkDown ( int n )
{
if ( n >= self.content.size() ) return;
ZNavNode element = content[n];
while ( n > 0 )
{
int parentN = ((n + 1) >> 1) - 1;
if ( parentN >= content.size() ) break;
ZNavNode parent = content[parentN];
if (scoreFunction (element) < scoreFunction (parent) )
{
content[parentN] = element;
content[n] = parent;
} else {
break;
}
}
}
void bubbleUp( int n )
{
int length = self.content.size();
if (n >= length ) return;
ZNavNode element = self.content[n];
int elemScore = scorefunction(element);
while (true)
{
int child2N = (n + 1) << 1;
int child1N = child2N - 1;
int swap = 0;
bool didSwap = false;
int child1Score;
if (child1N < length)
{
ZNavNode child1 = content[child1N];
child1Score = scorefunction(child1);
if (child1Score < elemScore)
{
swap = child1N;
didSwap = true;
}
}
if (child2N < length)
{
ZNavNode child2 = content[child2N];
int child2Score = scorefunction(child2);
int swapped = (didSwap == false) ? elemScore : child1Score;
if (child2Score < swapped )
{
swap = child2N;
didSwap = true;
}
}
if ( didSwap == true )
{
content[n] = content[swap];
content[swap] = element;
n = swap;
} else {
break;
}
}
}
}
/*
AStar ported from
https://github.com/donmccurdy/three-pathfinding/blob/main/src/AStar.js
this algorithm searches nodes to find the
shortest path between two points on a nav mesh
here, it looks through all the nodes and gets a list of nodes that comprise a path from one
point to another
*/
Class ZNavAStar : ZNav
{
static bool Search (
ZNavGroup group,
ZNavNode startNode,
ZNavNode endNode,
in out array<ZNavNode> nodes
)
{
group.resetNodes();
ZNavBinaryHeap openHeap = new ('ZNavBinaryHeap');
openHeap.push(startNode);
while ( openHeap.size() )
{
ZNavNode currentNode = openHeap.pop();
if (currentNode == endNode)
{
ZNavNode curr = currentNode;
array<ZNavNode>ret;
while (curr.parent)
{
ret.push(curr);
curr = curr.parent;
}
array<ZNavNode>rev;
for (int j = ret.size() - 1; j > -1; j--)
{
curr = ret[j];
rev.push(curr);
}
ret.clear();
nodes.move( rev );
openHeap.destroy();
return true;
}
currentNode.closed = true;
for (int i = 0; i < currentNode.neighborIDs.size(); i++ )
{
int neighborID = currentNode.neighborIDs[i];
ZNavNode neighbor = currentNode.getNeighbor( neighborID );
if (neighborID == currentNode.nodeID)
{
continue;
}
if (neighbor.closed)
{
continue;
}
int gScore = currentNode.g + neighbor.getCost();
// currentNode.neighborCosts[i];
bool beenVisited = neighbor.visited;
if (!beenVisited || gScore < neighbor.g)
{
neighbor.visited = true;
neighbor.parent = currentNode;
neighbor.h = ( neighbor.h == 0 ) ? Math.CheapDistance( neighbor.centroid, endNode.centroid ) : neighbor.h;
neighbor.g = gScore;
neighbor.f = neighbor.g + neighbor.h;
if (!beenVisited)
{
openHeap.push(neighbor);
} else {
openHeap.rescoreElement(neighbor);
}
}
}
}
openHeap.destroy();
return false;
}
}
/*
a navroute is just a list of points
agents can follow these points
if you have a navroute whose size is 3
the first point is the starting position
the second point is between the first and last
and the last is the end position
get() - returns a point on the list
size() - how many points there are total
shift() - removes the first point
pop() -removes the last point
copy() - copy another list of points
clone() - create a new instance of this list and share all of its current values.
clear() - empty the contents of the list
*/
class ZNavRoute : ZNav
{
array<double>navpoints;
//navhelper goal;
bool hasRoute()
{
return self.navpoints.size() > 0;
}
static ZNavRoute create ()
{
ZNavRoute route = new ('ZNavRoute');
return route;
}
ZNavRoute clone()
{
ZNavRoute route = ZNavRoute.create();
route.copy(self);
return route;
}
void copy( ZNavRoute route )
{
self.navpoints.copy( route.navpoints );
//self.goal = route.goal;
}
void dispose()
{
self.clear();
self.destroy();
}
void clear()
{
//goal = null;
navpoints.clear();
}
bool has ( vector3 v )
{
return indexOf (v) > -1;
}
int indexOf (vector3 v)
{
for (int i = 0; i < self.size(); i++)
{
if ( v == self.get(i) )
{
return i;
}
}
return -1;
}
int size ()
{
return navpoints.size() / 3;
}
void delete( int index, int howmany = 1)
{
index*=3;
navpoints.delete(index, 3 * howmany);
}
void push ( vector3 v )
{
navpoints.push(v.x);
navpoints.push(v.y);
navpoints.push(v.z);
}
vector3 pop()
{
int size = self.size();
vector3 ret = self.get( size - 1 );
self.delete(size - 1);
return ret;
}
vector3 shift()
{
vector3 ret = self.get( 0 );
self.delete(0);
return ret;
}
bool shift2()
{
if ( self.size() )
{
self.delete(0);
return true;
}
return false;
}
vector3 get( int index )
{
index*=3;
return (navpoints[index], navpoints[index+1], navpoints[index+2]);
}
void fromPoints ( in array<double> points )
{
self.navpoints.move ( points );
}
/*void addGoal (navhelper goal)
{
self.goal = goal;
}*/
}