Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
PathFinder.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Runtime.InteropServices;
4using UnityEngine;
5
6namespace Algorithms;
7
8[Serializable]
9public class PathFinder : IPathfinder
10{
11 internal struct PathFinderNodeFast
12 {
13 public int F;
14
15 public int G;
16
17 public ushort PX;
18
19 public ushort PZ;
20
21 public byte Status;
22 }
23
24 internal class ComparePFNodeMatrix : IComparer<int>
25 {
27
29 {
30 mMatrix = matrix;
31 }
32
33 public int Compare(int a, int b)
34 {
35 if (mMatrix[a].F > mMatrix[b].F)
36 {
37 return 1;
38 }
39 if (mMatrix[a].F < mMatrix[b].F)
40 {
41 return -1;
42 }
43 return 0;
44 }
45 }
46
47 public bool debug;
48
49 public bool Diagonals;
50
52
53 public float HeavyDiagonals = 1f;
54
55 public bool TieBreaker;
56
58
60
61 private int mHEstimate = 2;
62
64
65 private bool mStop;
66
67 private bool mStopped = true;
68
69 private int mHoriz;
70
71 private bool mReopenCloseNodes = true;
72
74
75 private byte mOpenNodeValue = 1;
76
77 private byte mCloseNodeValue = 2;
78
80
81 [NonSerialized]
82 public int total;
83
84 private int mH;
85
86 private int mLocation;
87
88 private int mNewLocation;
89
90 private ushort mLocationX;
91
92 private ushort mLocationZ;
93
94 private ushort mNewLocationX;
95
96 private ushort mNewLocationZ;
97
98 private int mGridXZ;
99
100 private int mGridX;
101
102 private int mGridZ;
103
104 private int mCloseNodeCounter;
105
106 private bool mFound;
107
108 private sbyte[,] mDirection = new sbyte[8, 2]
109 {
110 { 0, -1 },
111 { 1, 0 },
112 { 0, 1 },
113 { -1, 0 },
114 { -1, -1 },
115 { 1, -1 },
116 { -1, 1 },
117 { 1, 1 }
118 };
119
120 private int mEndLocation;
121
122 private int mStartLocation;
123
124 private int mNewG;
125
126 private byte _weight;
127
129
130 private int index;
131
132 private int mx;
133
134 private int mz;
135
136 [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
137 public unsafe static extern bool ZeroMemory(byte* destination, int length);
138
139 public void Init(IPathfindGrid _grid, WeightCell[,] _weightMap, int size)
140 {
141 grid = _grid;
142 weightMap = _weightMap;
143 mGridX = (ushort)size;
144 mGridZ = (ushort)size;
146 if (mCalcGrid == null || mCalcGrid.Length != mGridXZ)
147 {
150 }
151 }
152
153 public void FindPath(PathProgress path)
154 {
155 moveType = path.moveType;
156 _FindPath(path);
157 if (path.nodes.Count > 0)
158 {
159 PathFinderNode pathFinderNode = path.nodes[path.nodes.Count - 1];
160 if (pathFinderNode.X == path.startPoint.x && pathFinderNode.Z == path.startPoint.z)
161 {
162 path.nodes.RemoveAt(path.nodes.Count - 1);
163 }
164 }
165 if (path.nodes.Count == 0)
166 {
167 path.state = PathProgress.State.Fail;
168 return;
169 }
170 path.state = PathProgress.State.PathReady;
171 path.nodeIndex = path.nodes.Count - 1;
172 }
173
174 private void _FindPath(PathProgress path)
175 {
176 lock (this)
177 {
178 Point startPoint = path.startPoint;
179 Point destPoint = path.destPoint;
180 path.nodes.Clear();
181 mFound = (mStop = (mStopped = false));
183 mOpenNodeValue += 2;
184 mCloseNodeValue += 2;
185 mOpen.Clear();
186 mLocation = (mStartLocation = startPoint.z * mGridX + startPoint.x);
187 mEndLocation = destPoint.z * mGridX + destPoint.x;
188 if (mLocation >= mCalcGrid.Length)
189 {
190 if (debug)
191 {
192 Debug.Log("length over");
193 }
194 }
195 else
196 {
197 if (mEndLocation == mLocation)
198 {
199 return;
200 }
201 mCalcGrid[mLocation].G = 0;
203 mCalcGrid[mLocation].PX = (ushort)startPoint.x;
204 mCalcGrid[mLocation].PZ = (ushort)startPoint.z;
207 while (mOpen.Count > 0 && !mStop)
208 {
209 mLocation = mOpen.Pop();
210 if (mCalcGrid[mLocation].Status == mCloseNodeValue)
211 {
212 continue;
213 }
214 mLocationX = (ushort)(mLocation % mGridX);
215 mLocationZ = (ushort)(mLocation % mGridXZ / mGridX);
216 if (mLocation == mEndLocation)
217 {
219 mFound = true;
220 break;
221 }
223 {
224 mStopped = true;
225 if (path.searchLimit > 1000)
226 {
227 Debug.Log("search limit: " + path.startPoint?.ToString() + " " + path.destPoint);
228 }
229 return;
230 }
232 {
234 }
235 for (int i = 0; i < (Diagonals ? 8 : 4); i++)
236 {
237 mNewLocationX = (ushort)(mLocationX + mDirection[i, 0]);
238 mNewLocationZ = (ushort)(mLocationZ + mDirection[i, 1]);
241 {
242 continue;
243 }
245 {
246 _weight = 1;
247 }
248 else
249 {
250 if (i < 4)
251 {
252 index = ((mNewLocationZ >= mLocationZ) ? ((mNewLocationX > mLocationX) ? 1 : ((mNewLocationZ > mLocationZ) ? 2 : 3)) : 0);
254 }
255 else
256 {
257 mx = mLocationX + mDirection[i, 0];
258 mz = mLocationZ;
259 index = ((mz >= mLocationZ) ? ((mx > mLocationX) ? 1 : ((mz > mLocationZ) ? 2 : 3)) : 0);
261 if (weightMap[mx, mz].IsPathBlocked(moveType))
262 {
263 _weight = 0;
264 }
265 if (_weight > 0)
266 {
267 index = ((mz >= mNewLocationZ) ? ((mx > mNewLocationX) ? 1 : ((mz > mNewLocationZ) ? 2 : 3)) : 0);
269 if (_weight > 0)
270 {
271 mx = mLocationX;
272 mz = mLocationZ + mDirection[i, 1];
273 index = ((mz >= mLocationZ) ? ((mx > mLocationX) ? 1 : ((mz > mLocationZ) ? 2 : 3)) : 0);
275 if (weightMap[mx, mz].IsPathBlocked(moveType))
276 {
277 _weight = 0;
278 }
279 if (_weight > 0)
280 {
281 index = ((mz >= mNewLocationZ) ? ((mx > mNewLocationX) ? 1 : ((mz > mNewLocationZ) ? 2 : 3)) : 0);
283 }
284 }
285 }
286 }
287 if (_weight == 0)
288 {
289 continue;
290 }
293 {
294 continue;
295 }
296 }
299 {
300 if (mNewLocationX - mLocationX != 0 && mHoriz == 0)
301 {
302 mNewG += Math.Abs(mNewLocationX - destPoint.x) + Math.Abs(mNewLocationZ - destPoint.z);
303 }
304 if (mNewLocationZ - mLocationZ != 0 && mHoriz != 0)
305 {
306 mNewG += Math.Abs(mNewLocationX - destPoint.x) + Math.Abs(mNewLocationZ - destPoint.z);
307 }
308 }
310 {
314 switch (mFormula)
315 {
316 default:
317 mH = mHEstimate * (Math.Abs(mNewLocationX - destPoint.x) + Math.Abs(mNewLocationZ - destPoint.z));
318 break;
319 case HeuristicFormula.MaxDXDY:
320 mH = mHEstimate * Math.Max(Math.Abs(mNewLocationX - destPoint.x), Math.Abs(mNewLocationZ - destPoint.z));
321 break;
322 case HeuristicFormula.DiagonalShortCut:
323 {
324 int num = Math.Min(Math.Abs(mNewLocationX - destPoint.x), Math.Abs(mNewLocationZ - destPoint.z));
325 int num2 = Math.Abs(mNewLocationX - destPoint.x) + Math.Abs(mNewLocationZ - destPoint.z);
326 mH = mHEstimate * 2 * num + mHEstimate * (num2 - 2 * num);
327 break;
328 }
329 case HeuristicFormula.Euclidean:
330 mH = (int)((double)mHEstimate * Math.Sqrt(Math.Pow(mNewLocationZ - destPoint.x, 2.0) + Math.Pow(mNewLocationZ - destPoint.z, 2.0)));
331 break;
332 case HeuristicFormula.EuclideanNoSQR:
333 mH = (int)((double)mHEstimate * (Math.Pow(mNewLocationX - destPoint.x, 2.0) + Math.Pow(mNewLocationZ - destPoint.z, 2.0)));
334 break;
335 }
336 if (TieBreaker)
337 {
338 int num3 = mLocationX - destPoint.x;
339 int num4 = mLocationZ - destPoint.z;
340 int num5 = startPoint.x - destPoint.x;
341 int num6 = startPoint.z - destPoint.z;
342 int num7 = Math.Abs(num3 * num6 - num5 * num4);
343 mH = (int)((double)mH + (double)num7 * 0.001);
344 }
348 }
349 }
352 }
353 if (mFound)
354 {
355 int x = destPoint.x;
356 int z = destPoint.z;
357 PathFinderNodeFast pathFinderNodeFast = mCalcGrid[destPoint.z * mGridX + destPoint.x];
359 item.G = pathFinderNodeFast.G;
360 item.PX = pathFinderNodeFast.PX;
361 item.PZ = pathFinderNodeFast.PZ;
362 item.X = destPoint.x;
363 item.Z = destPoint.z;
364 while (item.X != item.PX || item.Z != item.PZ)
365 {
366 path.nodes.Add(item);
367 x = item.PX;
368 z = item.PZ;
369 pathFinderNodeFast = mCalcGrid[z * mGridX + x];
370 item.G = pathFinderNodeFast.G;
371 item.PX = pathFinderNodeFast.PX;
372 item.PZ = pathFinderNodeFast.PZ;
373 item.X = x;
374 item.Z = z;
375 }
376 path.nodes.Add(item);
377 mStopped = true;
378 }
379 else
380 {
381 mStopped = true;
382 }
383 }
384 }
385 }
386}
ComparePFNodeMatrix(PathFinderNodeFast[] matrix)
Definition: PathFinder.cs:28
void FindPath(PathProgress path)
Definition: PathFinder.cs:153
IPathfindGrid grid
Definition: PathFinder.cs:59
HeuristicFormula mFormula
Definition: PathFinder.cs:57
PathManager.MoveType moveType
Definition: PathFinder.cs:79
PathFinderNodeFast[] mCalcGrid
Definition: PathFinder.cs:73
PriorityQueueB< int > mOpen
Definition: PathFinder.cs:63
void Init(IPathfindGrid _grid, WeightCell[,] _weightMap, int size)
Definition: PathFinder.cs:139
WeightCell[,] weightMap
Definition: PathFinder.cs:128
static unsafe bool ZeroMemory(byte *destination, int length)
void _FindPath(PathProgress path)
Definition: PathFinder.cs:174
Point startPoint
Definition: PathProgress.cs:18
Point destPoint
Definition: PathProgress.cs:20
PathManager.MoveType moveType
Definition: PathProgress.cs:32
List< PathFinderNode > nodes
Definition: PathProgress.cs:16
bool ignoreConnection
Definition: PathProgress.cs:30
Definition: Point.cs:9
override string ToString()
Definition: Point.cs:500
int x
Definition: Point.cs:36
int z
Definition: Point.cs:39