-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandardMazeGenerator.cs
More file actions
552 lines (414 loc) · 17.8 KB
/
Copy pathStandardMazeGenerator.cs
File metadata and controls
552 lines (414 loc) · 17.8 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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public struct MazeCell {
public bool RightWall;
public bool DownWall;
}
public enum CardinalDirection {
Up,
Right,
Down,
Left
}
public class PathingNode {
public Vector2 node;
public CardinalDirection direction;
public PathingNode(Vector2 v, CardinalDirection d) {
node = v;
direction = d;
}
public Vector2 LeadsTo() {
Vector2 res = node;
switch (direction) {
case CardinalDirection.Up:
res -= Vector2.up;
break;
case CardinalDirection.Down:
res -= Vector2.down;
break;
case CardinalDirection.Left:
res += Vector2.left;
break;
case CardinalDirection.Right:
res += Vector2.right;
break;
default:
break;
}
return res;
}
public static bool operator ==(PathingNode left, PathingNode right) {
if (left.node != right.node) return false;
if (left.direction != right.direction) return false;
return true;
}
public static bool operator !=(PathingNode left, PathingNode right) {
return !(left == right);
}
}
[System.Serializable]
public class StandardMazeGenerator {
[Header("Data")]
public MazeCell[,] m_cell;
public Vector2 Entrance;
public Vector2 Exit;
public int MazeWidth;
public int MazeLength;
public int SeedValue;
public List<PathingNode> LeftHandPath;
public List<PathingNode> RightHandPath;
public List<PathingNode> DirectPath;
public IEnumerator GenerateDirectPath() {
Vector2 current = Exit;
int[,] grid = new int[MazeWidth, MazeLength];
PriorityQueue<Vector2> queue = new PriorityQueue<Vector2>();
queue.Add(current, 0f);
grid[(int)current.x, (int)current.y] = 1;
float LoopTime = Time.realtimeSinceStartup;
bool solved = false;
while (queue.Count > 0) {
current = queue.Get(0);
queue.RemoveAt(0);
if (current == Entrance) {
solved = true;
break;
}
for (int n = 0; n < 4; n++) {
// doesn't exit this direction, discard.
if (!HasExit(current, (CardinalDirection)n)) continue;
PathingNode d = new PathingNode(current, (CardinalDirection)n);
Vector2 next = d.LeadsTo();
// the other node has already been searched, and is a lower value. Discard.
if (grid[(int)next.x, (int)next.y] != 0 && grid[(int)next.x, (int)next.y] < grid[(int)current.x, (int)current.y] + 1) continue;
// set arrival cost in grid
grid[(int)next.x, (int)next.y] = grid[(int)current.x, (int)current.y] + 1;
// add this node to the queue
float heuristic = Mathf.Abs(next.x - Entrance.x) + Mathf.Abs(next.y - Entrance.y);
queue.Add(next, grid[(int)next.x, (int)next.y] + heuristic);
}
// grid[(int)current.x, (int)current.y];
if (Time.realtimeSinceStartup - LoopTime > 0.1) {
Debug.Log("searching...");
yield return null;
LoopTime = Time.realtimeSinceStartup;
}
}
if (!solved) yield break;
// backtrace...
current = Entrance;
List<PathingNode> path = new List<PathingNode>();
while (current != Exit) {
for (int n = 0; n < 4; n++) {
CardinalDirection c = (CardinalDirection)n;
if (!HasExit(current, c)) continue;
PathingNode d = new PathingNode(current, c);
Vector2 next = d.LeadsTo();
if (grid[(int)next.x, (int)next.y] >= grid[(int)current.x, (int)current.y]) continue;
path.Insert(0, d); // path from exit to entrance...
current = next;
break;
// check if any of the four directions has a node with lower value than the current.
}
if (Time.realtimeSinceStartup - LoopTime > 0.1) {
yield return null;
Debug.Log("backtrace...");
LoopTime = Time.realtimeSinceStartup;
}
}
DirectPath = path;
yield break;
}
// generate a path, always taking the left turn, when possible.
public IEnumerator GenerateLeftHandPath() {
List<PathingNode> path = new List<PathingNode>();
Vector2 current = Entrance;
CardinalDirection direction = CardinalDirection.Left;
float LoopTime = Time.realtimeSinceStartup;
while (current != Exit) {
// check each exit in turn...
for (int n = 0; n < 4; n++) {
CardinalDirection newDirection = (CardinalDirection)(((int)direction + 4 + 1 - n) % 4);
if (HasExit(current, newDirection)) {
direction = newDirection;
PathingNode directionNode = new PathingNode(current, direction);
path.Add(directionNode);
current = directionNode.LeadsTo();
break;
}
}
if (Time.realtimeSinceStartup - LoopTime > 0.1) {
yield return null;
LoopTime = Time.realtimeSinceStartup;
}
if (current.x < 0 || current.y < 0) break;
bool PathLoops = false;
for (int n = 0; n < path.Count - 1; n++) {
if (path[n] == path[path.Count - 1]) {
PathLoops = true;
break;
}
if (Time.realtimeSinceStartup - LoopTime > 0.1) {
Debug.Log("yield: " + path.Count + ": " + current);
yield return null;
LoopTime = Time.realtimeSinceStartup;
}
}
if (PathLoops) break;
}
LeftHandPath = path;
yield break;
}
public IEnumerator GenerateRightHandPath() {
List<PathingNode> path = new List<PathingNode>();
Vector2 current = Entrance;
CardinalDirection direction = CardinalDirection.Right;
float LoopTime = Time.realtimeSinceStartup;
while (current != Exit) {
// check each exit in turn...
for (int n = 0; n < 4; n++) {
CardinalDirection newDirection = (CardinalDirection)(((int)direction + 4 + 3 + n) % 4);
if (HasExit(current, newDirection)) {
direction = newDirection;
PathingNode directionNode = new PathingNode(current, direction);
path.Add(directionNode);
current = directionNode.LeadsTo();
break;
}
}
if (Time.realtimeSinceStartup - LoopTime > 0.1) {
yield return null;
LoopTime = Time.realtimeSinceStartup;
}
if (current.x < 0 || current.y < 0) break;
bool PathLoops = false;
for (int n = 0; n < path.Count - 1; n++) {
if (path[n] == path[path.Count - 1]) {
PathLoops = true;
break;
}
if (Time.realtimeSinceStartup - LoopTime > 0.1) {
// Debug.Log("yield: " + path.Count + ": " + current);
yield return null;
LoopTime = Time.realtimeSinceStartup;
}
}
if (PathLoops) break;
}
RightHandPath = path;
yield break;
}
public void PrepareGridData(int ArgMazeWidth, int ArgMazeLength, float WallBias = 50) {
MazeWidth = ArgMazeWidth;
MazeLength = ArgMazeLength;
int RandomSeed = SeedValue;
if (RandomSeed == 0) {
RandomSeed = Mathf.FloorToInt(Random.Range(0, int.MaxValue));
}
Random.InitState(RandomSeed);
// prepare a blank cell data for population
m_cell = new MazeCell[MazeWidth, MazeLength];
// grouping data
int[,] Grid = new int[MazeWidth, MazeLength];
// for each row
for (int y = 0; y < MazeLength; y++) {
for (int x = 0; x < MazeWidth; x++) {
// if the square doesn't belong to a set yet, add it to a unique set.
if (Grid[x, y] == 0) Grid[x, y] = (y * MazeWidth) + x + 1;
}
// if this isn't the last row
if (y < MazeLength - 1) {
for (int x = 0; x < MazeWidth - 1; x++) {
if (Grid[x, y] == Grid[x + 1, y]) {
// create a right wall
m_cell[x, y].RightWall = true;
} else if (Random.Range(0, 100) > WallBias) {
// create a right wall
m_cell[x, y].RightWall = true;
} else if (x < MazeWidth - 1) {
// if this isn't the last square
// join the next unit to this set
MergeSets(Grid, Grid[x, y], Grid[x + 1, y]);
}
}
} else {
// if this is the last row...
for (int x = 0; x < MazeWidth - 1; x++) {
if (Grid[x, y] == Grid[x + 1, y]) {
// create a right wall
m_cell[x, y].RightWall = true;
} else if (Random.Range(0, 100) > WallBias) {
// create a right wall
m_cell[x, y].RightWall = true;
} else if (x < MazeWidth - 2) {
// join the next unit to this set
MergeSets(Grid, Grid[x, y], Grid[x + 1, y]);
}
}
for (int x = 0; x < MazeWidth - 1; x++) {
if (Grid[x, y] != Grid[x + 1, y]) {
m_cell[x, y].RightWall = false;
// Destroy(CreatedWalls[x]);
}
}
}
Dictionary<int, int> GroupWallsCount = new Dictionary<int, int>();
// bottom wall decisions
if (y != MazeLength)
for (int x = 0; x < MazeWidth; x++) {
bool CreatedBottomWall = false;
// we need to track the number of cells in the group with walls created
if (Alone(Grid, Grid[x, y])) {
// do not create a wall
} else if ((GroupWallsCount.ContainsKey(Grid[x, y])) ? (GroupWallsCount[Grid[x, y]] >= CountSet(Grid, Grid[x, y], y) - 1) : (false)) {
// do not create a wall
} else if (Random.Range(0, 100) < WallBias) {
// create a bottom wall
if (!GroupWallsCount.ContainsKey(Grid[x, y])) {
GroupWallsCount.Add(Grid[x, y], 1);
} else {
GroupWallsCount[Grid[x, y]]++;
}
m_cell[x, y].DownWall = true;
CreatedBottomWall = true;
}
if (y < MazeLength - 1 && !CreatedBottomWall) {
// join the below unit to this set
AddToSet(Grid, x, y + 1, Grid[x, y]);
}
}
for (int x = 0; x < MazeWidth; x++) {
NullSet(Grid, x, y);
}
}
// if this is the bottom of the maze, all bottoms are set true.
for (int x = 0; x < MazeWidth; x++) {
m_cell[x, MazeLength - 1].DownWall = true;
}
// if this is the right edge, all rights are set true.
for (int y = 0; y < MazeLength; y++) {
m_cell[MazeWidth - 1, y].RightWall = true;
}
}
public void MostDistantPoints() {
if (m_cell == null) return;
int LongestPath = 0;
Vector2 start = -1f * Vector2.one, origin = -1f * Vector2.one, destination = -1f * Vector2.one;
for (int x = 0; x < MazeWidth; x++) {
for (int y = 0; y < MazeLength; y++) {
// flood algorithm
int CurLongest = 1;
int[,] Grid = new int[MazeWidth, MazeLength];
for (int m = 0; m < MazeWidth; m++) {
for (int n = 0; n < MazeLength; n++) {
Grid[m, n] = 0;
}
}
origin = new Vector2(x, y);
Vector2 LocalFurtherest = origin;
List<Vector2> search = new List<Vector2>();
search.Add(origin);
Grid[(int)origin.x, (int)origin.y] = 1;
while (search.Count > 0) {
Vector2 Current = search[0];
search.RemoveAt(0);
if (Current.y < 0 || Current.x < 0 || Current.y >= MazeLength || Current.x >= MazeWidth) {
continue;
}
if (Grid[(int)Current.x, (int)Current.y] > CurLongest) {
CurLongest = Grid[(int)Current.x, (int)Current.y];
LocalFurtherest = Current;
}
// we search the four directions.
// if it is zero, we assign it current + 1 and add it to the queue.
// if there is a value there, and it is less than our current value + 1,
// we skip it.
if (Current.y < MazeLength - 1 && !m_cell[(int)Current.x, (int)Current.y].DownWall &&
(Grid[(int)Current.x, (int)Current.y + 1] == 0 || Grid[(int)Current.x, (int)Current.y] + 1 < Grid[(int)Current.x, (int)Current.y + 1])) {
Grid[(int)Current.x, (int)Current.y + 1] = Grid[(int)Current.x, (int)Current.y] + 1;
search.Add(new Vector2((int)Current.x, (int)Current.y + 1));
}
if (Current.x < MazeWidth - 1 && !m_cell[(int)Current.x, (int)Current.y].RightWall &&
(Grid[(int)Current.x + 1, (int)Current.y] == 0 || Grid[(int)Current.x, (int)Current.y] + 1 < Grid[(int)Current.x + 1, (int)Current.y])) {
Grid[(int)Current.x + 1, (int)Current.y] = Grid[(int)Current.x, (int)Current.y] + 1;
search.Add(new Vector2((int)Current.x + 1, (int)Current.y));
}
if (Current.x > 0 && !m_cell[(int)Current.x - 1, (int)Current.y].RightWall &&
(Grid[(int)Current.x - 1, (int)Current.y] == 0 || Grid[(int)Current.x, (int)Current.y] + 1 < Grid[(int)Current.x - 1, (int)Current.y])) {
Grid[(int)Current.x - 1, (int)Current.y] = Grid[(int)Current.x, (int)Current.y] + 1;
search.Add(new Vector2((int)Current.x - 1, (int)Current.y));
}
if (Current.y > 0 && !m_cell[(int)Current.x, (int)Current.y - 1].DownWall &&
(Grid[(int)Current.x, (int)Current.y - 1] == 0 || Grid[(int)Current.x, (int)Current.y] + 1 < Grid[(int)Current.x, (int)Current.y - 1])) {
Grid[(int)Current.x, (int)Current.y - 1] = Grid[(int)Current.x, (int)Current.y] + 1;
search.Add(new Vector2((int)Current.x, (int)Current.y - 1));
}
}
if (CurLongest > LongestPath) {
LongestPath = CurLongest;
start = origin;
destination = LocalFurtherest;
}
}
}
Entrance = start;
Exit = destination;
// Debug.Log("Entrance: " + Entrance + " | Exit: " + Exit + " | Length: " + LongestPath);
}
// add grid[x,y] to Sets[s]
void AddToSet(int[,] Grid, int x, int y, int s) {
// if we aren't a set member, go remove ourselves
Grid[x, y] = s;
}
void NullSet(int[,] Grid, int x, int y) {
Grid[x, y] = 0;
}
// MergeSet one and set two
void MergeSets(int[,] Grid, int Set1, int Set2) {
if (Set1 == 0 || Set2 == 0) return;
for (int x = 0; x < MazeWidth; x++)
for (int y = 0; y < MazeLength; y++) {
if (Grid[x, y] == Set2) Grid[x, y] = Set1;
}
}
int CountSet(int[,] Grid, int Set, int row) {
if (Set == 0) Debug.Log("Counting 0...");
int n = 0;
for (int x = 0; x < MazeWidth; x++) {
if (Grid[x, row] == Set) n++;
}
if (n == 0) Debug.Log("Counted an empty set...");
return n;
}
bool Alone(int[,] Grid, int Set) {
int n = 0;
for (int x = 0; x < MazeWidth; x++) {
for (int y = 0; y < MazeLength; y++) {
if (Grid[x, y] == Set) {
n++;
if (n == 2) return false;
}
}
}
return true;
}
bool HasExit(Vector2 node, CardinalDirection d) {
int x = (int)node.x; int y = (int)node.y;
return HasExit(x, y, d);
}
bool HasExit(int x, int y, CardinalDirection d) {
if (x < 0 || y < 0 || x >= MazeWidth || y >= MazeLength) return false;
switch (d) {
case CardinalDirection.Down:
return !m_cell[x, y].DownWall;
case CardinalDirection.Right:
return !m_cell[x, y].RightWall;
case CardinalDirection.Up:
return HasExit(x, y - 1, CardinalDirection.Down);
case CardinalDirection.Left:
return HasExit(x - 1, y, CardinalDirection.Right);
}
return true;
}
}