-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAINavMeshGenerator.cs
589 lines (529 loc) · 18.1 KB
/
AINavMeshGenerator.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using Unity.Jobs;
using Unity.Collections;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Node
{
public bool valid = true;
public Vector2 position;
public readonly Node[] connections;
public float gCost;
public float hCost;
public float fCost { get { return gCost + hCost; } }
public Node parent;
public GameObject associatedObject;
public Node(Vector2 pos)
{
position = pos;
connections = new Node[8];
}
public void Reset()
{
valid = true;
associatedObject = null;
gCost = 0;
hCost = 0;
parent = null;
}
public bool AnyConnectionsBad()
{
for (int i = 0; i < connections.Length; i++)
{
if(connections[i] == null || !connections[i].valid)
{
return true;
}
}
return false;
}
}
[ExecuteInEditMode]
public class AINavMeshGenerator : SerializedMonoBehaviour
{
enum Directions { Right, DownRight, Down, DownLeft, Left, UpLeft, Up, UpRight }
[OdinSerialize]
private float updateInterval = 0.1f;
[OdinSerialize]
private float pointDistributionSize = 0.5f;
[OdinSerialize]
LayerMask destroyNodeMask;
[OdinSerialize]
LayerMask obstacleMask;
public Rect size;
public static AINavMeshGenerator instance;
private float updateTimer = 0;
private List<Node> grid = null;
private List<Node> Grid
{
get
{
if(grid == null)
{
GenerateNewGrid();
}
return grid;
}
}
private Dictionary<Vector2, Node> positionNodeDictionary = new Dictionary<Vector2, Node>();
public static Pathfinder pathfinder = null;
public void GenerateNewGrid()
{
FillOutGrid();
DestroyBadNodes();
CheckForBadNodes();
}
public LayerMask GetAvoidanceMasks()
{
return destroyNodeMask | obstacleMask;
}
private void Awake()
{
instance = this;
pathfinder = new Pathfinder(this);
}
private void Start()
{
GenerateNewGrid();
updateTimer = updateInterval;
}
private void FillOutGrid()
{
grid = new List<Node>();
positionNodeDictionary.Clear();
Vector2 currentPoint = new Vector2((size.x - size.width / 2) + pointDistributionSize, (size.y + size.height / 2) - pointDistributionSize);
int iteration = 0;
bool alternate = false;
bool cacheIteration = false;
int length = -1;
int yLength = 0;
while (true)
{
iteration++;
Node newNode = new Node(currentPoint);
Grid.Add(newNode);
positionNodeDictionary.Add(currentPoint, newNode);
currentPoint += new Vector2(pointDistributionSize * 2, 0);
if (currentPoint.x > size.x + size.width / 2)
{
if(length != -1)
{
while(iteration < length)
{
Node extraNode = new Node(currentPoint);
Grid.Add(extraNode);
iteration++;
}
}
else
{
Node extraNode = new Node(currentPoint);
Grid.Add(extraNode);
}
currentPoint = new Vector2((size.x - size.width / 2) + (alternate ? pointDistributionSize : 0), currentPoint.y - pointDistributionSize);
alternate = !alternate;
cacheIteration = true;
yLength++;
}
if (currentPoint.y < size.y - size.height / 2)
{
break;
}
if(cacheIteration)
{
if(length == -1)
{
length = iteration + 1;
}
iteration = 0;
cacheIteration = false;
}
}
for (int i = 0; i < Grid.Count; i++)
{
for (int direction = 0; direction < Grid[i].connections.Length; direction++)
{
Grid[i].connections[direction] = GetNodeFromDirection(i, (Directions)direction, length);
}
}
}
private void DestroyBadNodes()
{
//First check if each node is inside a destroy mask
for (int i = Grid.Count - 1; i >= 0; i--)
{
Collider2D hit = Physics2D.OverlapCircle(Grid[i].position, 0.01f, destroyNodeMask);
if (hit != null)
{
//At this point, we know this node is bad, and we must destroy it. For humanity itself.
for (int j = 0; j < Grid[i].connections.Length; j++)
{
//Go through all the connections to this node
if (Grid[i].connections[j] != null)
{
for (int k = 0; k < Grid[i].connections[j].connections.Length; k++)
{
//Set the nodes connections reference to this node to null, because it no longer exists.
//Is that confusing? It sounds confusing.
if (Grid[i].connections[j].connections[k] != null)
{
if (Grid[i].connections[j].connections[k] == Grid[i])
{
Grid[i].connections[j].connections[k] = null;
}
}
}
}
}
Grid.RemoveAt(i);
}
}
}
private void CheckForBadNodes()
{
for (int i = 0; i < Grid.Count; i++)
{
Grid[i].Reset();
}
//Make any node with a destroyed outside have an extra layer barrier around it, so that they dont get too close to walls
for (int i = 0; i < Grid.Count; i++)
{
if (Grid[i].valid)
{
for (int j = 0; j < Grid[i].connections.Length; j++)
{
Node connection = Grid[i].connections[j];
if (connection == null)
{
Grid[i].valid = false;
}
}
}
}
//Then check if the node is inside a normal mask to disable it.
for (int i = 0; i < Grid.Count; i++)
{
if(Grid[i].valid)
{
Collider2D hit = Physics2D.OverlapCircle(Grid[i].position, 0.05f, obstacleMask);
if (hit != null)
{
Grid[i].valid = false;
Grid[i].associatedObject = hit.transform.gameObject;
}
}
}
}
Node GetNodeFromDirection(int nodeIndex, Directions direction, int length)
{
int index = -1;
bool isStartOfRow = (nodeIndex + 1) % length == 1;
bool isEndOfRow = (nodeIndex + 1) % length == 0;
bool isOddRow = (((nodeIndex + 1) - Mathf.FloorToInt((nodeIndex) % length)) / length) % 2 == 0;
switch (direction)
{
case Directions.Right:
if (isEndOfRow) return null;
index = nodeIndex + 1;
break;
case Directions.DownRight:
if (isEndOfRow && isOddRow) return null;
index = nodeIndex + length + (isOddRow ? 1 : 0);
break;
case Directions.Down:
index = nodeIndex + length * 2;
break;
case Directions.DownLeft:
if (isStartOfRow && !isOddRow) return null;
index = nodeIndex + (length - (isOddRow ? 0 : 1));
break;
case Directions.Left:
if (isStartOfRow) return null;
index = nodeIndex - 1;
break;
case Directions.UpLeft:
if (isStartOfRow && !isOddRow) return null;
index = nodeIndex - (length + (isOddRow ? 0 : 1));
break;
case Directions.Up:
index = nodeIndex - length * 2;
break;
case Directions.UpRight:
if (isEndOfRow && isOddRow) return null;
index = nodeIndex - (length - (isOddRow ? 1 : 0));
break;
}
if (index >= 0 && index < Grid.Count)
{
return Grid[index];
}
else
{
return null;
}
}
public Node FindClosestNode(Vector2 position, bool mustBeGood = false, GameObject associatedObject = null)
{
Node closest = null;
float current = float.MaxValue;
for (int i = 0; i < Grid.Count; i++)
{
if(!mustBeGood || Grid[i].valid || associatedObject == Grid[i].associatedObject)
{
float distance = Vector2.Distance(Grid[i].position, position);
if (distance < current)
{
current = distance;
closest = Grid[i];
}
}
}
return closest;
}
public void ClearGrid()
{
Grid.Clear();
}
void Update()
{
if(Grid == null)
{
GenerateNewGrid();
}
//We update the bad nodes constantly, so as objects or enemies move, the grid automatically adjusts itself.
updateTimer -= Time.deltaTime;
if(updateTimer <= 0)
{
updateTimer = updateInterval;
CheckForBadNodes();
}
}
void OnDrawGizmosSelected()
{
if(Grid == null)
{
return;
}
for (int i = 0; i < Grid.Count; i++)
{
for (int j = 0; j < Grid[i].connections.Length; j++)
{
if(Grid[i].connections[j] != null)
{
Gizmos.color = Grid[i].valid && Grid[i].connections[j].valid ? Color.green : Color.red;
Gizmos.DrawLine(Grid[i].position, Grid[i].connections[j].position);
}
}
}
for (int i = 0; i < Grid.Count; i++)
{
Gizmos.color = Grid[i].valid ? Color.blue : Color.red;
Gizmos.DrawCube(Grid[i].position, Vector3.one * 0.2f);
}
}
}
public class Pathfinder
{
AINavMeshGenerator generator;
List<Node> openSet = new List<Node>();
HashSet<Node> closedSet = new HashSet<Node>();
List<Node> path = new List<Node>();
public Pathfinder(AINavMeshGenerator gen)
{
generator = gen;
}
private float Heuristic(Node one, Node two)
{
return Vector2.Distance(one.position, two.position);
}
private Node[] GetAStar(Vector2 currentPosition, Vector2 destination, GameObject obj = null)
{
Node start = generator.FindClosestNode(currentPosition, false, obj);
Node end = generator.FindClosestNode(destination, true, obj);
if (start == null || end == null)
{
return null;
}
openSet.Clear();
closedSet.Clear();
openSet.Add(start);
while (openSet.Count > 0)
{
Node current = openSet[0];
//Evaluate costs
for (int i = 1; i < openSet.Count; i++)
{
if (openSet[i].fCost < current.fCost || openSet[i].fCost == current.fCost)
{
if (openSet[i].hCost < current.hCost)
{
current = openSet[i];
}
}
}
openSet.Remove(current);
closedSet.Add(current);
if (current.Equals(end))
{
break;
}
//Go through neighbors
foreach (Node neighbor in current.connections.Where(x => x != null))
{
//The associated object check is so the enemy ignores pathing through it's own bad sector
if ((!neighbor.valid && neighbor.associatedObject != obj) || closedSet.Contains(neighbor))
{
continue;
}
float newCost = current.gCost + Heuristic(current, neighbor);
if (newCost < neighbor.gCost || !openSet.Contains(neighbor))
{
neighbor.gCost = newCost;
neighbor.hCost = Heuristic(neighbor, end);
neighbor.parent = current;
if (!openSet.Contains(neighbor))
{
openSet.Add(neighbor);
}
}
}
}
if(end.parent == null)
{
return null;
}
//Calculate path
path.Clear();
Node currentCheck = end;
while (!path.Contains(currentCheck) && currentCheck != null)
{
path.Add(currentCheck);
currentCheck = currentCheck.parent;
}
path.Reverse();
if(path[0] != start)
{
return null;
}
return path.ToArray();
}
private List<Vector2> ShortenPointsByVisibility(Node[] points)
{
//If we have small amount of points, dont bother with all this.
//if(points.Length < 2)
{
List<Vector2> p = new List<Vector2>();
for (int i = 0; i < points.Length; i++)
{
p.Add(points[i].position);
}
return p;
}
List<Vector2> corners = new List<Vector2>();
corners.Add(points[0].position);
Node start = points[0];
Node end = points[1];
//Go through all the points, starting at 1 (since we already set our initial start to the first point)
for (int i = 1; i < points.Length; i++)
{
//Set the end to our current point and check if its in a bad spot to hang out in town.
end = null;
end = points[i];
bool inBadArea = end.AnyConnectionsBad();
if(inBadArea)
{
//If it's a bad boy, we add it to our corners, so we walk this way.
corners.Add(end.position);
//Then start anew.
start = null;
start = end;
}
}
//Add that last rebel into the mix for sure.
corners.Add(points[points.Length - 1].position);
return corners;
}
public Vector2[] FindPath(Vector2 currentPosition, Vector2 destination, GameObject associatedObject = null)
{
Node[] points = GetAStar(currentPosition, destination, associatedObject);
if(points == null)
{
return null;
}
List<Vector2> shortPath = ShortenPointsByVisibility(points);
//shortPath.Insert(0, currentPosition);
return shortPath.ToArray();
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(AINavMeshGenerator))]
public class AINavMeshGeneratorEditor : Editor
{
void OnSceneGUI()
{
AINavMeshGenerator source = target as AINavMeshGenerator;
Rect rect = RectUtils.ResizeRect(source.size,
Handles.CubeHandleCap,
Color.green, new Color(1, 1, 1, 0.5f),
HandleUtility.GetHandleSize(Vector3.zero) * 0.1f,
0.1f);
source.size = rect;
}
public override void OnInspectorGUI()
{
AINavMeshGenerator source = target as AINavMeshGenerator;
base.OnInspectorGUI();
if (GUILayout.Button("Generate grid"))
{
source.GenerateNewGrid();
}
if(GUILayout.Button("Clear grid"))
{
source.ClearGrid();
}
}
}
public class RectUtils
{
public static Rect ResizeRect(Rect rect, Handles.CapFunction capFunc, Color capCol, Color fillCol, float capSize, float snap)
{
Vector2 halfRectSize = new Vector2(rect.size.x * 0.5f, rect.size.y * 0.5f);
Vector3[] rectangleCorners =
{
new Vector3(rect.position.x - halfRectSize.x, rect.position.y - halfRectSize.y, 0), // Bottom Left
new Vector3(rect.position.x + halfRectSize.x, rect.position.y - halfRectSize.y, 0), // Bottom Right
new Vector3(rect.position.x + halfRectSize.x, rect.position.y + halfRectSize.y, 0), // Top Right
new Vector3(rect.position.x - halfRectSize.x, rect.position.y + halfRectSize.y, 0) // Top Left
};
Handles.color = fillCol;
Handles.DrawSolidRectangleWithOutline(rectangleCorners, new Color(fillCol.r, fillCol.g, fillCol.b, 0.25f), capCol);
Vector3[] handlePoints =
{
new Vector3(rect.position.x - halfRectSize.x, rect.position.y, 0), // Left
new Vector3(rect.position.x + halfRectSize.x, rect.position.y, 0), // Right
new Vector3(rect.position.x, rect.position.y + halfRectSize.y, 0), // Top
new Vector3(rect.position.x, rect.position.y - halfRectSize.y, 0) // Bottom
};
Handles.color = capCol;
var newSize = rect.size;
var newPosition = rect.position;
var leftHandle = Handles.Slider(handlePoints[0], -Vector3.right, capSize, capFunc, snap).x - handlePoints[0].x;
var rightHandle = Handles.Slider(handlePoints[1], Vector3.right, capSize, capFunc, snap).x - handlePoints[1].x;
var topHandle = Handles.Slider(handlePoints[2], Vector3.up, capSize, capFunc, snap).y - handlePoints[2].y;
var bottomHandle = Handles.Slider(handlePoints[3], -Vector3.up, capSize, capFunc, snap).y - handlePoints[3].y;
newSize = new Vector2(
Mathf.Max(.1f, newSize.x - leftHandle + rightHandle),
Mathf.Max(.1f, newSize.y + topHandle - bottomHandle));
newPosition = new Vector2(
newPosition.x + leftHandle * .5f + rightHandle * .5f,
newPosition.y + topHandle * .5f + bottomHandle * .5f);
return new Rect(newPosition.x, newPosition.y, newSize.x, newSize.y);
}
}
#endif