Elin Modding Docs Doc
Loading...
Searching...
No Matches
PathFinder.cs
1using System;
2using System.Collections.Generic;
3using System.Runtime.InteropServices;
4using UnityEngine;
5
6namespace Algorithms
7{
8 // Token: 0x0200075C RID: 1884
9 [Serializable]
10 public class PathFinder : IPathfinder
11 {
12 // Token: 0x0600366C RID: 13932
13 [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
14 public unsafe static extern bool ZeroMemory(byte* destination, int length);
15
16 // Token: 0x0600366D RID: 13933 RVA: 0x00127E58 File Offset: 0x00126058
17 public void Init(IPathfindGrid _grid, WeightCell[,] _weightMap, int size)
18 {
19 this.grid = _grid;
20 this.weightMap = _weightMap;
21 this.mGridX = (int)((ushort)size);
22 this.mGridZ = (int)((ushort)size);
23 this.mGridXZ = this.mGridX * this.mGridZ;
24 if (this.mCalcGrid == null || this.mCalcGrid.Length != this.mGridXZ)
25 {
26 this.mCalcGrid = new PathFinder.PathFinderNodeFast[this.mGridXZ];
27 this.mOpen = new PriorityQueueB<int>(new PathFinder.ComparePFNodeMatrix(this.mCalcGrid));
28 }
29 }
30
31 // Token: 0x0600366E RID: 13934 RVA: 0x00127ED8 File Offset: 0x001260D8
32 public void FindPath(PathProgress path)
33 {
34 this.moveType = path.moveType;
35 this._FindPath(path);
36 if (path.nodes.Count > 0)
37 {
38 PathFinderNode pathFinderNode = path.nodes[path.nodes.Count - 1];
39 if (pathFinderNode.X == path.startPoint.x && pathFinderNode.Z == path.startPoint.z)
40 {
41 path.nodes.RemoveAt(path.nodes.Count - 1);
42 }
43 }
44 if (path.nodes.Count == 0)
45 {
46 path.state = PathProgress.State.Fail;
47 return;
48 }
49 path.state = PathProgress.State.PathReady;
50 path.nodeIndex = path.nodes.Count - 1;
51 }
52
53 // Token: 0x0600366F RID: 13935 RVA: 0x00127F8C File Offset: 0x0012618C
54 private void _FindPath(PathProgress path)
55 {
56 lock (this)
57 {
58 Point startPoint = path.startPoint;
59 Point destPoint = path.destPoint;
60 path.nodes.Clear();
61 this.mFound = (this.mStop = (this.mStopped = false));
62 this.mCloseNodeCounter = 0;
63 this.mOpenNodeValue += 2;
64 this.mCloseNodeValue += 2;
65 this.mOpen.Clear();
66 this.mLocation = (this.mStartLocation = startPoint.z * this.mGridX + startPoint.x);
67 this.mEndLocation = destPoint.z * this.mGridX + destPoint.x;
68 if (this.mLocation >= this.mCalcGrid.Length)
69 {
70 if (this.debug)
71 {
72 Debug.Log("length over");
73 }
74 }
75 else if (this.mEndLocation != this.mLocation)
76 {
77 this.mCalcGrid[this.mLocation].G = 0;
78 this.mCalcGrid[this.mLocation].F = this.mHEstimate;
79 this.mCalcGrid[this.mLocation].PX = (ushort)startPoint.x;
80 this.mCalcGrid[this.mLocation].PZ = (ushort)startPoint.z;
81 this.mCalcGrid[this.mLocation].Status = this.mOpenNodeValue;
82 this.mOpen.Push(this.mLocation);
83 while (this.mOpen.Count > 0 && !this.mStop)
84 {
85 this.mLocation = this.mOpen.Pop();
86 if (this.mCalcGrid[this.mLocation].Status != this.mCloseNodeValue)
87 {
88 this.mLocationX = (ushort)(this.mLocation % this.mGridX);
89 this.mLocationZ = (ushort)(this.mLocation % this.mGridXZ / this.mGridX);
90 if (this.mLocation == this.mEndLocation)
91 {
92 this.mCalcGrid[this.mLocation].Status = this.mCloseNodeValue;
93 this.mFound = true;
94 break;
95 }
96 if (this.mCloseNodeCounter > path.searchLimit)
97 {
98 this.mStopped = true;
99 if (path.searchLimit > 1000)
100 {
101 string str = "search limit: ";
102 Point startPoint2 = path.startPoint;
103 string str2 = (startPoint2 != null) ? startPoint2.ToString() : null;
104 string str3 = " ";
105 Point destPoint2 = path.destPoint;
106 Debug.Log(str + str2 + str3 + ((destPoint2 != null) ? destPoint2.ToString() : null));
107 }
108 return;
109 }
110 if (this.PunishChangeDirection)
111 {
112 this.mHoriz = (int)(this.mLocationX - this.mCalcGrid[this.mLocation].PX);
113 }
114 for (int i = 0; i < (this.Diagonals ? 8 : 4); i++)
115 {
116 this.mNewLocationX = this.mLocationX + (ushort)this.mDirection[i, 0];
117 this.mNewLocationZ = this.mLocationZ + (ushort)this.mDirection[i, 1];
118 this.mNewLocation = (int)this.mNewLocationZ * this.mGridX + (int)this.mNewLocationX;
119 if ((int)this.mNewLocationX < this.mGridX && (int)this.mNewLocationZ < this.mGridZ && (this.mCalcGrid[this.mNewLocation].Status != this.mCloseNodeValue || this.mReopenCloseNodes))
120 {
121 if (path.ignoreConnection && this.mEndLocation == this.mNewLocation)
122 {
123 this._weight = 1;
124 }
125 else
126 {
127 if (i < 4)
128 {
129 this.index = ((this.mNewLocationZ < this.mLocationZ) ? 0 : ((this.mNewLocationX > this.mLocationX) ? 1 : ((this.mNewLocationZ > this.mLocationZ) ? 2 : 3)));
130 this._weight = this.weightMap[(int)this.mLocationX, (int)this.mLocationZ].weights[this.index];
131 }
132 else
133 {
134 this.mx = (int)(this.mLocationX + (ushort)this.mDirection[i, 0]);
135 this.mz = (int)this.mLocationZ;
136 this.index = ((this.mz < (int)this.mLocationZ) ? 0 : ((this.mx > (int)this.mLocationX) ? 1 : ((this.mz > (int)this.mLocationZ) ? 2 : 3)));
137 this._weight = this.weightMap[(int)this.mLocationX, (int)this.mLocationZ].weights[this.index];
138 if (this.weightMap[this.mx, this.mz].IsPathBlocked(this.moveType))
139 {
140 this._weight = 0;
141 }
142 if (this._weight > 0)
143 {
144 this.index = ((this.mz < (int)this.mNewLocationZ) ? 0 : ((this.mx > (int)this.mNewLocationX) ? 1 : ((this.mz > (int)this.mNewLocationZ) ? 2 : 3)));
145 this._weight = this.weightMap[(int)this.mNewLocationX, (int)this.mNewLocationZ].weights[this.index];
146 if (this._weight > 0)
147 {
148 this.mx = (int)this.mLocationX;
149 this.mz = (int)(this.mLocationZ + (ushort)this.mDirection[i, 1]);
150 this.index = ((this.mz < (int)this.mLocationZ) ? 0 : ((this.mx > (int)this.mLocationX) ? 1 : ((this.mz > (int)this.mLocationZ) ? 2 : 3)));
151 this._weight = this.weightMap[(int)this.mLocationX, (int)this.mLocationZ].weights[this.index];
152 if (this.weightMap[this.mx, this.mz].IsPathBlocked(this.moveType))
153 {
154 this._weight = 0;
155 }
156 if (this._weight > 0)
157 {
158 this.index = ((this.mz < (int)this.mNewLocationZ) ? 0 : ((this.mx > (int)this.mNewLocationX) ? 1 : ((this.mz > (int)this.mNewLocationZ) ? 2 : 3)));
159 this._weight = this.weightMap[(int)this.mNewLocationX, (int)this.mNewLocationZ].weights[this.index];
160 }
161 }
162 }
163 }
164 if (this._weight == 0)
165 {
166 goto IL_A70;
167 }
168 this._weight += this.weightMap[(int)this.mLocationX, (int)this.mLocationZ].baseWeight;
169 if (this.mEndLocation != this.mNewLocation && this.weightMap[(int)this.mNewLocationX, (int)this.mNewLocationZ].IsPathBlocked(this.moveType))
170 {
171 goto IL_A70;
172 }
173 }
174 this.mNewG = this.mCalcGrid[this.mLocation].G + (int)this._weight;
175 if (this.PunishChangeDirection)
176 {
177 if (this.mNewLocationX - this.mLocationX != 0 && this.mHoriz == 0)
178 {
179 this.mNewG += Math.Abs((int)this.mNewLocationX - destPoint.x) + Math.Abs((int)this.mNewLocationZ - destPoint.z);
180 }
181 if (this.mNewLocationZ - this.mLocationZ != 0 && this.mHoriz != 0)
182 {
183 this.mNewG += Math.Abs((int)this.mNewLocationX - destPoint.x) + Math.Abs((int)this.mNewLocationZ - destPoint.z);
184 }
185 }
186 if ((this.mCalcGrid[this.mNewLocation].Status != this.mOpenNodeValue && this.mCalcGrid[this.mNewLocation].Status != this.mCloseNodeValue) || this.mCalcGrid[this.mNewLocation].G > this.mNewG)
187 {
188 this.mCalcGrid[this.mNewLocation].PX = this.mLocationX;
189 this.mCalcGrid[this.mNewLocation].PZ = this.mLocationZ;
190 this.mCalcGrid[this.mNewLocation].G = this.mNewG;
191 switch (this.mFormula)
192 {
193 default:
194 this.mH = this.mHEstimate * (Math.Abs((int)this.mNewLocationX - destPoint.x) + Math.Abs((int)this.mNewLocationZ - destPoint.z));
195 break;
196 case HeuristicFormula.MaxDXDY:
197 this.mH = this.mHEstimate * Math.Max(Math.Abs((int)this.mNewLocationX - destPoint.x), Math.Abs((int)this.mNewLocationZ - destPoint.z));
198 break;
199 case HeuristicFormula.DiagonalShortCut:
200 {
201 int num = Math.Min(Math.Abs((int)this.mNewLocationX - destPoint.x), Math.Abs((int)this.mNewLocationZ - destPoint.z));
202 int num2 = Math.Abs((int)this.mNewLocationX - destPoint.x) + Math.Abs((int)this.mNewLocationZ - destPoint.z);
203 this.mH = this.mHEstimate * 2 * num + this.mHEstimate * (num2 - 2 * num);
204 break;
205 }
206 case HeuristicFormula.Euclidean:
207 this.mH = (int)((double)this.mHEstimate * Math.Sqrt(Math.Pow((double)((int)this.mNewLocationZ - destPoint.x), 2.0) + Math.Pow((double)((int)this.mNewLocationZ - destPoint.z), 2.0)));
208 break;
209 case HeuristicFormula.EuclideanNoSQR:
210 this.mH = (int)((double)this.mHEstimate * (Math.Pow((double)((int)this.mNewLocationX - destPoint.x), 2.0) + Math.Pow((double)((int)this.mNewLocationZ - destPoint.z), 2.0)));
211 break;
212 }
213 if (this.TieBreaker)
214 {
215 int num3 = (int)this.mLocationX - destPoint.x;
216 int num4 = (int)this.mLocationZ - destPoint.z;
217 int num5 = startPoint.x - destPoint.x;
218 int num6 = startPoint.z - destPoint.z;
219 int num7 = Math.Abs(num3 * num6 - num5 * num4);
220 this.mH = (int)((double)this.mH + (double)num7 * 0.001);
221 }
222 this.mCalcGrid[this.mNewLocation].F = this.mNewG + this.mH;
223 this.mOpen.Push(this.mNewLocation);
224 this.mCalcGrid[this.mNewLocation].Status = this.mOpenNodeValue;
225 }
226 }
227 IL_A70:;
228 }
229 this.mCloseNodeCounter++;
230 this.mCalcGrid[this.mLocation].Status = this.mCloseNodeValue;
231 }
232 }
233 if (this.mFound)
234 {
235 int num8 = destPoint.x;
236 int num9 = destPoint.z;
237 PathFinder.PathFinderNodeFast pathFinderNodeFast = this.mCalcGrid[destPoint.z * this.mGridX + destPoint.x];
238 PathFinderNode pathFinderNode;
239 pathFinderNode.G = pathFinderNodeFast.G;
240 pathFinderNode.PX = (int)pathFinderNodeFast.PX;
241 pathFinderNode.PZ = (int)pathFinderNodeFast.PZ;
242 pathFinderNode.X = destPoint.x;
243 pathFinderNode.Z = destPoint.z;
244 while (pathFinderNode.X != pathFinderNode.PX || pathFinderNode.Z != pathFinderNode.PZ)
245 {
246 path.nodes.Add(pathFinderNode);
247 num8 = pathFinderNode.PX;
248 num9 = pathFinderNode.PZ;
249 pathFinderNodeFast = this.mCalcGrid[num9 * this.mGridX + num8];
250 pathFinderNode.G = pathFinderNodeFast.G;
251 pathFinderNode.PX = (int)pathFinderNodeFast.PX;
252 pathFinderNode.PZ = (int)pathFinderNodeFast.PZ;
253 pathFinderNode.X = num8;
254 pathFinderNode.Z = num9;
255 }
256 path.nodes.Add(pathFinderNode);
257 this.mStopped = true;
258 }
259 else
260 {
261 this.mStopped = true;
262 }
263 }
264 }
265 }
266
267 // Token: 0x04001CF1 RID: 7409
268 public bool debug;
269
270 // Token: 0x04001CF2 RID: 7410
271 public bool Diagonals;
272
273 // Token: 0x04001CF3 RID: 7411
274 public bool PunishChangeDirection;
275
276 // Token: 0x04001CF4 RID: 7412
277 public float HeavyDiagonals = 1f;
278
279 // Token: 0x04001CF5 RID: 7413
280 public bool TieBreaker;
281
282 // Token: 0x04001CF6 RID: 7414
283 public HeuristicFormula mFormula = HeuristicFormula.Manhattan;
284
285 // Token: 0x04001CF7 RID: 7415
286 private IPathfindGrid grid;
287
288 // Token: 0x04001CF8 RID: 7416
289 private int mHEstimate = 2;
290
291 // Token: 0x04001CF9 RID: 7417
292 private PriorityQueueB<int> mOpen;
293
294 // Token: 0x04001CFA RID: 7418
295 private bool mStop;
296
297 // Token: 0x04001CFB RID: 7419
298 private bool mStopped = true;
299
300 // Token: 0x04001CFC RID: 7420
301 private int mHoriz;
302
303 // Token: 0x04001CFD RID: 7421
304 private bool mReopenCloseNodes = true;
305
306 // Token: 0x04001CFE RID: 7422
307 private PathFinder.PathFinderNodeFast[] mCalcGrid;
308
309 // Token: 0x04001CFF RID: 7423
310 private byte mOpenNodeValue = 1;
311
312 // Token: 0x04001D00 RID: 7424
313 private byte mCloseNodeValue = 2;
314
315 // Token: 0x04001D01 RID: 7425
316 private PathManager.MoveType moveType;
317
318 // Token: 0x04001D02 RID: 7426
319 [NonSerialized]
320 public int total;
321
322 // Token: 0x04001D03 RID: 7427
323 private int mH;
324
325 // Token: 0x04001D04 RID: 7428
326 private int mLocation;
327
328 // Token: 0x04001D05 RID: 7429
329 private int mNewLocation;
330
331 // Token: 0x04001D06 RID: 7430
332 private ushort mLocationX;
333
334 // Token: 0x04001D07 RID: 7431
335 private ushort mLocationZ;
336
337 // Token: 0x04001D08 RID: 7432
338 private ushort mNewLocationX;
339
340 // Token: 0x04001D09 RID: 7433
341 private ushort mNewLocationZ;
342
343 // Token: 0x04001D0A RID: 7434
344 private int mGridXZ;
345
346 // Token: 0x04001D0B RID: 7435
347 private int mGridX;
348
349 // Token: 0x04001D0C RID: 7436
350 private int mGridZ;
351
352 // Token: 0x04001D0D RID: 7437
353 private int mCloseNodeCounter;
354
355 // Token: 0x04001D0E RID: 7438
356 private bool mFound;
357
358 // Token: 0x04001D0F RID: 7439
359 private sbyte[,] mDirection = new sbyte[,]
360 {
361 {
362 0,
363 -1
364 },
365 {
366 1,
367 0
368 },
369 {
370 0,
371 1
372 },
373 {
374 -1,
375 0
376 },
377 {
378 -1,
379 -1
380 },
381 {
382 1,
383 -1
384 },
385 {
386 -1,
387 1
388 },
389 {
390 1,
391 1
392 }
393 };
394
395 // Token: 0x04001D10 RID: 7440
396 private int mEndLocation;
397
398 // Token: 0x04001D11 RID: 7441
399 private int mStartLocation;
400
401 // Token: 0x04001D12 RID: 7442
402 private int mNewG;
403
404 // Token: 0x04001D13 RID: 7443
405 private byte _weight;
406
407 // Token: 0x04001D14 RID: 7444
408 public WeightCell[,] weightMap;
409
410 // Token: 0x04001D15 RID: 7445
411 private int index;
412
413 // Token: 0x04001D16 RID: 7446
414 private int mx;
415
416 // Token: 0x04001D17 RID: 7447
417 private int mz;
418
419 // Token: 0x02000C48 RID: 3144
420 internal struct PathFinderNodeFast
421 {
422 // Token: 0x0400309A RID: 12442
423 public int F;
424
425 // Token: 0x0400309B RID: 12443
426 public int G;
427
428 // Token: 0x0400309C RID: 12444
429 public ushort PX;
430
431 // Token: 0x0400309D RID: 12445
432 public ushort PZ;
433
434 // Token: 0x0400309E RID: 12446
435 public byte Status;
436 }
437
438 // Token: 0x02000C49 RID: 3145
439 internal class ComparePFNodeMatrix : IComparer<int>
440 {
441 // Token: 0x060046DF RID: 18143 RVA: 0x00162127 File Offset: 0x00160327
442 public ComparePFNodeMatrix(PathFinder.PathFinderNodeFast[] matrix)
443 {
444 this.mMatrix = matrix;
445 }
446
447 // Token: 0x060046E0 RID: 18144 RVA: 0x00162138 File Offset: 0x00160338
448 public int Compare(int a, int b)
449 {
450 if (this.mMatrix[a].F > this.mMatrix[b].F)
451 {
452 return 1;
453 }
454 if (this.mMatrix[a].F < this.mMatrix[b].F)
455 {
456 return -1;
457 }
458 return 0;
459 }
460
461 // Token: 0x0400309F RID: 12447
462 private PathFinder.PathFinderNodeFast[] mMatrix;
463 }
464 }
465}
Definition Point.cs:11