-
Notifications
You must be signed in to change notification settings - Fork 7
/
create.cpp
2364 lines (2091 loc) · 68.7 KB
/
create.cpp
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Daedalus (Version 3.5) File: create.cpp
** By Walter D. Pullen, [email protected], http://www.astrolog.org/labyrnth.htm
**
** IMPORTANT NOTICE: Daedalus and all Maze generation and general
** graphics routines used in this program are Copyright (C) 1998-2024 by
** Walter D. Pullen. Permission is granted to freely use, modify, and
** distribute these routines provided these credits and notices remain
** unmodified with any altered or distributed versions of the program.
** The user does have all rights to Mazes and other graphic output
** they make in Daedalus, like a novel created in a word processor.
**
** More formally: This program is free software; you can redistribute it
** and/or modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of the
** License, or (at your option) any later version. This program is
** distributed in the hope that it will be useful and inspiring, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details, a copy of which is in the
** LICENSE.HTM included with Daedalus, and at http://www.gnu.org
**
** This file contains Maze creation algorithms, specifically algorithms that
** produce standard orthogonal 2D Mazes.
**
** Created: 9/4/2000.
** Last code change: 10/30/2024.
*/
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#include "graphics.h"
#include "color.h"
#include "threed.h"
#include "maze.h"
/*
******************************************************************************
** Core Maze Creation Routines
******************************************************************************
*/
// Carve Maze passages adding on to any existing passages in the bitmap, using
// the Hunt and Kill algorithm.
flag CMaz::PerfectGenerate(flag fClear, int xs, int ys)
{
int x, y, xnew, ynew, xInc, yInc, zInc, fHunt = fFalse, pass = 0,
d, d0, d1, dd, i;
long count;
flag fClean = ms.fRiver && ms.fRiverEdge && ms.fRiverFlow;
int dHunt, tHunt, iHunt, cHunt, cHuntMax;
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize))
return fFalse;
xInc = Rnd(0, 1) ? 2 : -2; yInc = Rnd(0, 1) ? 2 : -2;
zInc = ms.nHuntType != 1 || Rnd(0, 1);
x = xs + !FOdd(xs ^ xl); y = ys + !FOdd(ys ^ yl);
Set0(x, y);
count = ((xh - xl) >> 1) * ((yh - yl) >> 1) - 1;
d0 = ms.fRiver || ms.nHuntType >= 2 ? DIRS : 1;
cHuntMax = NMax((xh - xl) >> 1, (yh - yl) >> 1) << 2;
loop {
LNext:
// Simple case: For Mazes covering the whole bitmap section, with no
// special settings active, or for Hunt mode, simply carve into any
// available uncreated cell from the current created cell.
if (fClear && (fClean || fHunt)) {
d = DirFindUncreated(&x, &y, fFalse);
if (d < 0)
goto LHunt;
if (fCellMax)
goto LDone;
Set0(x - xoff[d], y - yoff[d]);
Set0(x, y);
LSet:
fHunt = fFalse;
pass = 0;
if (ms.nHuntType == 1)
inv(zInc);
// Simple termination case: When number of cells created is the number
// in the whole bitmap section, there's clearly nothing left to do.
count--;
if (count <= 0)
goto LDone;
goto LNext;
}
// Complex cases: For Mazes that don't cover the whole bitmap, or when
// some of the Perfect create settings are non-default values.
d = RndDir();
dd = (Rnd(0, 1) << 1) - 1;
d1 = fHunt ? DIRS : d0;
for (i = 0; i < d1; i++) {
xnew = x + xoff2[d];
ynew = y + yoff2[d];
if (!(FLegalMaze2(xnew, ynew) &&
(fClear || FOnMaze(xnew, ynew)))) {
if (!ms.fRiverEdge && !fHunt)
goto LHunt;
} else if (!Get(xnew, ynew)) {
if (!ms.fRiverFlow && !fHunt && i < 2 &&
!Get((x + xnew) >> 1, (y + ynew) >> 1)) {
x = xnew; y = ynew;
goto LNext;
}
} else {
if (!ms.fRiver && fHunt && i > 0) {
pass = 0;
goto LHunt;
}
Set0((x + xnew) >> 1, (y + ynew) >> 1);
Set0(xnew, ynew);
x = xnew; y = ynew;
goto LSet;
}
d = (d + dd) & DIRS1;
}
// Hunt mode: Move to a different created cell.
LHunt:
if (!fHunt) {
fHunt = fTrue;
if (ms.nHuntType >= 2) {
dHunt = Rnd(0, DIRS1);
tHunt = (Rnd(0, 1) << 1) - 1;
cHunt = iHunt = 0;
}
}
// Search in a square spiral pattern from the start cell.
if (ms.nHuntType >= 2) {
do {
x += xoff2[dHunt]; y += yoff2[dHunt];
iHunt++;
if (iHunt > (cHunt >> 1)) {
iHunt = 0;
cHunt++;
if (cHunt > cHuntMax)
goto LDone;
dHunt = (dHunt + tHunt) & DIRS1;
}
} while (!FLegalMaze2(x, y) || Get(x, y) || (!fClear && !FOnMaze(x, y)));
continue;
}
// Search row by row in opposite directions for balance, and also search
// column by column in opposite directions for balance too, to avoid bias.
do {
if (zInc) {
if (x + xInc >= xl && x + xInc <= xh)
x += xInc;
else {
neg(xInc);
if (y + yInc >= yl && y + yInc <= yh)
y += yInc;
else {
neg(yInc);
// A complete pass over the Maze once in Hunt mode means finished.
pass++;
if (pass > 1)
goto LDone;
UpdateDisplay();
}
}
} else {
if (y + yInc >= yl && y + yInc <= yh)
y += yInc;
else {
neg(yInc);
if (x + xInc >= xl && x + xInc <= xh)
x += xInc;
else {
neg(xInc);
// A complete pass over the Maze once in Hunt mode means finished.
pass++;
if (pass > 1)
goto LDone;
UpdateDisplay();
}
}
}
} while (Get(x, y) || (!fClear && !FOnMaze(x, y)));
}
LDone:
return fTrue;
}
// Create a new perfect Maze in the bitmap using the Hunt and Kill algorithm,
// by carving passages.
flag CMaz::CreateMazePerfect()
{
if (ms.fTreeWall)
return CreateMazePerfect2();
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize | femsMinSize))
return fFalse;
MazeClear(fOn);
MakeEntranceExit(0);
UpdateDisplay();
PerfectGenerate(fTrue, Rnd(xl, xh-1), Rnd(yl, yh-1));
return fTrue;
}
// Add all possible wall segments to the Maze, that won't cause a dead end to
// be created, or cause a section of the Maze to become inaccessible. Called
// from CreateMazeBraid() to do most of the work of creating braid Mazes.
long CMaz::BraidConnectWalls()
{
int x, y, xnew, ynew, xspan, yspan, d;
long count = 0, total, i, j, jInc;
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize))
return fFalse;
xspan = (xh - xl) >> 1;
yspan = (yh - yl) >> 1;
total = xspan * yspan * 2;
j = Rnd(1, total);
jInc = LPrime(total);
// Sweep over all potential wall segments in pseudo-random order.
for (i = 0; i < total; i++) {
j += jInc;
while (j >= total)
j -= total;
y = (j >> 1) / xspan;
x = xl + (((j >> 1) - y * xspan) << 1) + 2;
y = yl + (y << 1) + 2;
if (Get(x, y)) {
d = j & 1;
xnew = x + xoff2[d];
ynew = y + yoff2[d];
if (xnew < xh && ynew < yh && Get(xnew, ynew)) {
xnew -= xoff[d];
ynew -= yoff[d];
// If there are two wall endpoints with a passage between, and adding
// a wall there wouldn't cause a dead end or isolation, create it.
if (!Get(xnew, ynew) && !FWouldMakeDeadEnd(xnew, ynew) &&
!FWouldMakeIsolation(xnew, ynew)) {
count++;
Set1(xnew, ynew);
}
}
}
}
return count;
}
// Create a new braid Maze in the bitmap, i.e. a Maze without any dead ends,
// using a wall adding algorithm.
flag CMaz::CreateMazeBraid()
{
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize | femsMinSize))
return fFalse;
// Step 1: Start with the outer wall.
MazeClear(fOff);
// Step 2: Add a grid of wall vertices, then add wall segments touching each
// vertex to ensure there are no open rooms or poles in the Maze.
MazeNormalize(fOn);
UpdateDisplay();
MakeEntranceExit(0);
DoConnectPoles(fFalse);
// Step 2.5: There might still be a pole in the lower right corner.
if (Count(xh-2, yh-2) == 0) {
if (xh-xl > 4 && yh-yl > 2) {
Set1(xh-3, yh-2);
Set0(xh-4, yh-1); Set0(xh-4, yh-3);
} else if (xh-xl > 2 && yh-yl > 4) {
Set1(xh-2, yh-3);
Set0(xh-1, yh-4); Set0(xh-3, yh-4);
}
}
UpdateDisplay();
// Step 3: Add as many wall segments that don't make dead ends as possible.
BraidConnectWalls();
return fTrue;
}
// Create a new braid Maze in the bitmap using a binary algorithm, i.e.
// starting with a template based on the Tilt Maze pattern.
flag CMaz::CreateMazeBraidTilt()
{
int x, y, i, m, n, d, e;
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize | femsMinSize))
return fFalse;
// Step 1: Start with the outer wall.
MazeClear(fOff);
UpdateDisplay();
// Step 2: Create a tilt Maze pattern covering the Maze bitmap.
for (y = yl; y <= yh; y += 4)
for (x = xl; x <= xh; x += 4)
for (i = 0; i <= 1; i++) {
m = x + (i << 1); n = y + (i << 1);
if (n <= yl || n >= yh - 1 || (ms.fTiltDiamond && !Get(m-2, n)))
d = 1;
else if (m <= xl || m >= xh - 1)
d = 0;
else
d = Rnd(0, 1);
d <<= 1; e = d ^ 2;
if (d)
LineX(NMax(m - d, xl), NMin(m + d, xh), n, fOn);
else
LineY(m, NMax(n - e, yl), NMin(n + e, yh), fOn);
}
// Step 2.5: Ensure all poles are set, if needed.
UpdateDisplay();
MakeEntranceExit(0);
if (!ms.fTiltDiamond)
MazeNormalize(fOn);
// Step 3: Remove dead ends the tilt pattern may have placed in the corners.
DoCrackDeadEnds();
if (ms.fTiltDiamond) {
for (i = 0; i < 10; i++) {
if (DoConnectPoles(fTrue) <= 0)
break;
DoCrackDeadEnds();
}
} else
DoConnectPoles(fFalse);
// Step 4: Remove isolated sections to ensure the Maze is solvable.
DoCrackIsolations();
BraidConnectWalls();
return fTrue;
}
// Find and return the location of an uncreated wall vertex in the Maze.
// Called from SpiralMakeTemplate() to start a new spiral.
void CMaz::SpiralMakeNew(ushort *x0, ushort *y0)
{
int xsize, ysize, x, y, count = 0;
long total, i, iInc;
xsize = ((xh - xl) >> 1) - 1;
ysize = ((yh - yl) >> 1) - 1;
total = xsize*ysize;
iInc = LPrime(total);
if (ms.iSpiralIndex < 0)
ms.iSpiralIndex = Rnd(1, total);
do {
// Try a random location several times. If nothing found, then do a
// pseudo-random order scan over all the vertices in the Maze.
if (count < 5) {
count++;
i = Rnd(1, total) - 1;
} else {
ms.iSpiralIndex++;
if (ms.iSpiralIndex >= total)
ms.iSpiralIndex = 0;
i = (ms.iSpiralIndex * iInc) % total;
}
y = i / xsize;
x = xl + (((i - y * xsize) + 1) << 1);
y = yl + ((y + 1) << 1);
} while (Get(x, y));
Set1(x, y);
*x0 = x; *y0 = y;
}
// Create a set of spirals in the open spaces of the bitmap, by growing
// spirals from the center out. Called from CreateMazeSpiral() to create a
// spiral template, i.e. do most of the work of creating spiral Mazes.
int CMaz::SpiralMakeTemplate()
{
PT rgpt[SpiralMax][SpiralWallMax];
int numwalls[SpiralMax], hand[SpiralMax], dir[SpiralMax][SpiralWallMax];
int spirals, wallsleft, x, y, xnew, ynew, count = 0, s, w, i;
long cellstotal = (((xh - xl) >> 1) - 1)*(((yh - yl) >> 1) - 1),
cellsleft = cellstotal;
// Figure out how many spirals to have growing at once.
i = cellsleft / 475;
if (i > ms.cSpiral)
spirals = ms.cSpiral;
else if (i < 1 && cellsleft > 0)
spirals = 1;
else
spirals = i;
// Sprout the initial set of spirals.
ms.iSpiralIndex = -1;
for (s = 0; s < spirals; s++) {
numwalls[s] = 1;
dir[s][0] = Rnd(0, DIRS1);
hand[s] = Rnd(0, 1) ? 1 : -1;
SpiralMakeNew(&rgpt[s][0].x, &rgpt[s][0].y);
cellsleft--;
count++;
}
while (cellsleft > 0) {
for (s = 0; s < spirals; s++) {
wallsleft = numwalls[s];
for (w = numwalls[s]-1; w >= 0; w--) {
if (rgpt[s][w].x == SpiralDead) {
wallsleft--;
continue;
}
LThisWall:
x = rgpt[s][w].x; y = rgpt[s][w].y;
xnew = x + (xoff[dir[s][w]] << 1);
ynew = y + (yoff[dir[s][w]] << 1);
if (!Get(xnew, ynew)) {
Block(x, y, xnew, ynew, fOn);
cellsleft--;
rgpt[s][w].x = xnew;
rgpt[s][w].y = ynew;
// This spiral arm has another wall in front of it.
} else {
// 50% chance it turns and follows it.
if (Rnd(1, 2) == 1) {
dir[s][w] = dir[s][w] - hand[s] & DIRS1;
goto LThisWall;
}
// Else 33% chance it connects with the wall.
if (Rnd(1, 3) > 1)
Block(x, y, xnew, ynew, fOn);
// Otherwise just die here.
rgpt[s][w].x = SpiralDead;
continue;
}
// Take a peek to the side and check if it's time to turn yet.
xnew = rgpt[s][w].x + (xoff[dir[s][w] + hand[s] & DIRS1] << 1);
ynew = rgpt[s][w].y + (yoff[dir[s][w] + hand[s] & DIRS1] << 1);
if (Get(xnew, ynew))
goto LThisWall;
// 25% chance to spawn another wall segment at the turn.
if (Rnd(1, 4) > 1 || numwalls[s] >= ms.cSpiralWall)
continue;
numwalls[s]++;
for (i = numwalls[s]-1; i > w+1; i--) {
rgpt[s][i] = rgpt[s][i - 1];
dir[s][i] = dir[s][i - 1];
}
rgpt[s][w + 1].x = xnew;
rgpt[s][w + 1].y = ynew;
dir[s][w + 1] = dir[s][w];
Set1(xnew, ynew);
cellsleft--;
goto LThisWall;
}
// Turn all spiral arms in the direction of the spiral.
for (w = 0; w < numwalls[s]; w++)
dir[s][w] = dir[s][w] + hand[s] & DIRS1;
// A spiral is dead when it has no arms left. Spawn a new one.
if (!(wallsleft > 0 || cellsleft <= 0 || Rnd(1, 20) > 1)) {
SpiralMakeNew(&rgpt[s][0].x, &rgpt[s][0].y);
cellsleft--;
count++;
numwalls[s] = 1;
}
}
}
UpdateDisplay();
// Toggle the state of random wall segments to make the Maze more random.
if (cellstotal > 0)
for (i = ((xh - xl) + (yh - yl)) / 3 + ms.cRandomAdd; i > 0; i--) {
y = Rnd(yl + 1, yh - 1);
w = 1 + FOdd(y - yl);
x = RndSkip(xl + w, xh - w);
Inv(x, y);
}
return count;
}
// Create a new spiral Maze in the bitmap, formed of interlocking spirals.
flag CMaz::CreateMazeSpiral()
{
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize | femsMinSize))
return fFalse;
// Fill the bitmap with a random spiral template, which doesn't form a Maze.
MazeClear(fOff);
SpiralMakeTemplate();
MakeEntranceExit(0);
UpdateDisplay();
// Run the isolation and loop removers, to make a perfect Maze out of it.
DoCrackIsolations();
UpdateDisplay();
DoConnectDetachments();
return fTrue;
}
// Create a Maze with a diagonal bias, where many walls look like stairs.
flag CMaz::CreateMazeDiagonal()
{
int x, y, m, n, i;
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize | femsMinSize))
return fFalse;
MazeClear(fOff);
UpdateDisplay();
// Create a template formed of a series of stairstep walls filling the
// bitmap, going diagonally from lower left to upper right.
for (n = yh; n > yl; n -= 4) {
x = xl; y = n;
while (y > yl && x < xh) {
Set1(x, y); y--; Set1(x, y); y--;
Set1(x, y); x++; Set1(x, y); x++;
Set1(x, y);
}
}
for (m = xl + 4; m < xh; m += 4) {
x = m; y = yh;
while (y > yl && x < xh) {
Set1(x, y); y--; Set1(x, y); y--;
Set1(x, y); x++; Set1(x, y); x++;
Set1(x, y);
}
}
UpdateDisplay();
// Turn on random wall segments to make the Maze more random.
m = xh - xl + 2; n = yh - yl + 2;
for (i = (m + n) * 4 + ms.cRandomAdd; i > 0; i--) {
x = xl + Rnd(0, (m >> 1) - 2) * 2; y = yl + Rnd(0, (n >> 1) - 2) * 2;
Set1(x, y + 1);
Set1(x + 1, y + 2);
}
MakeEntranceExit(0);
UpdateDisplay();
// Run the isolation remover, to make a perfect Maze out of the template.
DoCrackIsolations();
return fTrue;
}
/*
******************************************************************************
** More Perfect Maze Creation Routines
******************************************************************************
*/
CONST char rgdRecursive[64] = {
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3,
0, 1, 2, 3};
CONST char rgidRecursive[64] = {
33,35,39,41,45,47,27,29,37,40,43,46,25,28,31,34,42,44,24,26,30,32,36,38,
52,53,55,56,58,59,49,50,54,56,57,59,48,50,51,53,57,58,48,49,51,52,54,55,
61,62,63, 60,62,63, 60,61,63, 60,61,62,
-1, -1, -1, -1};
CONST char rgidRecursiveBias[2][4] =
{{2, 3, 12, 13}, {10, 11, 20, 21}};
// Carve Maze passages into a solid shape on the bitmap, using the Recursive
// Backtracking algorithm.
flag CMaz::RecursiveGenerate(int xs, int ys)
{
byte *rgdir;
long stack = 0, count;
int cRunRnd = 0, dirRnd, x, y, xnew, ynew, id, d = -1;
flag fFast = ms.nRndRun <= 0 && ms.nRndBias == 0;
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize))
return fFalse;
count = ((xh - xl) >> 1) * ((yh - yl) >> 1);
rgdir = RgAllocate(count, byte);
if (rgdir == NULL)
return fFalse;
x = xl + ((xs - xl) | 1); y = yl + ((ys - yl) | 1);
Set0(x, y);
count--;
loop {
// Determine direction sequence to take from a newly visited cell.
if (fFast && d >= 0)
id = rgidRecursive[(d ^ 2)*6 + Rnd(0, 5)];
else {
if (cRunRnd > 0)
cRunRnd--;
else {
if (ms.nRndRun > 0)
cRunRnd = Rnd(0, ms.nRndRun);
dirRnd = Rnd(0, 23 + NAbs(ms.nRndBias)*4);
}
if (dirRnd < 24)
id = dirRnd;
else
id = rgidRecursiveBias[ms.nRndBias > 0][dirRnd & DIRS1];
}
// Move to an adjacent cell if any available, pushing the directions
// still available from the current cell on the stack.
loop {
d = rgdRecursive[id];
xnew = x + xoff2[d]; ynew = y + yoff2[d];
if (FLegalMaze2(xnew, ynew) && Get(xnew, ynew)) {
if (fCellMax)
goto LDone;
x = xnew; y = ynew;
Set0(x - xoff[d], y - yoff[d]);
Set0(x, y);
count--;
if (count <= 0) // Done if all cells have been carved into.
goto LDone;
rgdir[stack] = id; stack++;
break;
}
// Select next direction from the earlier chosen direction sequence.
loop {
id = rgidRecursive[id];
if (id >= 0)
break;
// If no directions left, pop the stack and back up to previous cell.
stack--;
if (stack < 0) // Done if stack becomes empty meaning no cells left.
goto LDone;
id = rgdir[stack]; d = rgdRecursive[id];
x -= xoff2[d]; y -= yoff2[d];
}
}
}
LDone:
DeallocateP(rgdir);
return fTrue;
}
// Create a new perfect Maze in the bitmap using the Recursive Backtracking
// algorithm. This carves passages.
flag CMaz::CreateMazeRecursive()
{
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize | femsMinSize))
return fFalse;
MazeClear(fOn);
MakeEntranceExit(0);
UpdateDisplay();
return RecursiveGenerate(Rnd(xl, xh-1), Rnd(yl, yh-1));
}
enum _primcell {
primIn = 0,
primFrontier = 1,
primOut = 2,
primNever = 3,
};
typedef struct _prim {
long zFrontier; // Map cell index to cell coordinate
long set; // Map cell coordinate to cell type
} PRIM;
// Mark a cell as part of the set of cells that's part of the Maze. Called
// from PrimGenerate().
long PrimMakeIn(PRIM *prim, int x, int y, int xs, int ys, long cFrontier)
{
long z;
int xnew, ynew, d;
z = y*xs + x;
prim[z].set = primIn;
// Update the status of the four adjacent cells.
for (d = 0; d < DIRS; d++) {
xnew = x + xoff[d]; ynew = y + yoff[d];
if (xnew >= 0 && ynew >= 0 && xnew < xs && ynew < ys) {
z = ynew*xs + xnew;
if (prim[z].set == primOut) {
prim[z].set = primFrontier;
prim[cFrontier].zFrontier = z;
cFrontier++;
}
}
}
return cFrontier;
}
// Carve Maze passages into a solid shape on the bitmap, or add walls assuming
// the bitmap is clear, using a modified version of Prim's algorithm.
flag CMaz::PrimGenerate(flag fWall, flag fClear, int xp, int yp)
{
PRIM *prim;
long cFrontier = 0, i;
int xbase, ybase, x, y, xs, ys, j, xnew, ynew, d;
Assert(FImplies(fWall, fClear));
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize))
return fFalse;
xs = ((xh - xl) >> 1) + fWall; ys = ((yh - yl) >> 1) + fWall;
prim = RgAllocate(xs*ys, PRIM);
if (prim == NULL)
return fFalse;
xbase = xl + 1 - fWall; ybase = yl + 1 - fWall;
// Figure out which cells are part of the Maze and which should be ignored
// (because they're in the middle of solid blocks).
if (fWall || fClear) {
for (i = xs*ys-1; i >= 0; i--)
prim[i].set = primOut;
} else {
for (y = 0; y < ys; y++)
for (x = 0; x < xs; x++)
prim[y*xs + x].set = primNever -
Get(xbase + (x << 1), ybase + (y << 1));
}
// Figure out which cell or cells to start growing from.
if (!fWall) {
x = xp; y = yp;
Set0(xbase + (x << 1), ybase + (y << 1));
cFrontier = PrimMakeIn(prim, x, y, xs, ys, cFrontier);
} else {
for (x = 0; x < xs; x++) {
prim[x].set = primIn;
prim[(ys-1)*xs + x].set = primIn;
}
for (y = 1; y < ys-1; y++) {
prim[y*xs].set = primIn;
prim[y*xs + (xs-1)].set = primIn;
}
for (x = 0; x < xs; x++) {
cFrontier = PrimMakeIn(prim, x, 0, xs, ys, cFrontier);
cFrontier = PrimMakeIn(prim, x, ys-1, xs, ys, cFrontier);
}
for (y = 1; y < ys-1; y++) {
cFrontier = PrimMakeIn(prim, 0, y, xs, ys, cFrontier);
cFrontier = PrimMakeIn(prim, xs-1, y, xs, ys, cFrontier);
}
}
// Grow into each reachable cell until there are none left.
while (cFrontier > 0) {
if (fCellMax)
goto LDone;
i = Rnd(0, cFrontier - 1);
y = prim[i].zFrontier / xs; x = prim[i].zFrontier % xs;
prim[i].zFrontier = prim[cFrontier-1].zFrontier;
cFrontier--;
Set(xbase + (x << 1), ybase + (y << 1), fWall);
cFrontier = PrimMakeIn(prim, x, y, xs, ys, cFrontier);
// Pick a random unreached cell adjacent to the set of current cells.
d = RndDir();
for (j = 0; j < DIRS; j++) {
xnew = x + xoff[d]; ynew = y + yoff[d];
if (xnew >= 0 && ynew >= 0 && xnew < xs && ynew < ys &&
prim[ynew*xs + xnew].set == primIn) {
Set(xbase + (x << 1) + xoff[d], ybase + (y << 1) + yoff[d], fWall);
break;
}
DirInc(d);
}
}
LDone:
DeallocateP(prim);
return fTrue;
}
// Create a new perfect Maze in the bitmap using a modified version of Prim's
// algorithm. This can carve passages or add walls.
flag CMaz::CreateMazePrim()
{
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize | femsMinSize))
return fFalse;
MazeClear(!ms.fTreeWall);
MakeEntranceExit(0);
UpdateDisplay();
return PrimGenerate(ms.fTreeWall, fTrue,
Rnd(0, ((xh - xl) >> 1) - 1), Rnd(0, ((yh - yl) >> 1) - 1));
}
typedef struct _prim2 {
long x; // Horizontal coordinate of edge
long y; // Vertical coordinate of edge
long iEdge; // Map cell coordinate to edge index/weight
long iHeap; // Map heap index to edge index/weight
} PRIM2;
#define ZPrimXY(x, y) ((y) * xs + ((x) >> 1))
#define FComparePrim(i1, i2) (prim[i1].iHeap < prim[i2].iHeap)
// Given a valid heap and an item at its end, push that item up through the
// heap so its valid. Called from CreateMazePrim2 to add edges to the set.
void PushupPrim(PRIM2 *prim, long i)
{
int n = prim[i].iHeap, iParent;
while (i > 0) {
iParent = ((i + 1) >> 1) - 1;
if (FComparePrim(iParent, i))
break;
prim[i].iHeap = prim[iParent].iHeap;
prim[iParent].iHeap = n;
i = iParent;
}
}
// Create a new perfect Maze in the bitmap using full or simplified versions
// of Prim's algorithm. This can carve passages or add walls.
flag CMaz::CreateMazePrim2(int xp, int yp)
{
PRIM2 *prim = NULL;
long cedgeMax, cedge = 0, iFrontier = 0, i, j, jParent;
int xs, ys, x, y, xnew, ynew, xnew2, ynew2, xInc, yInc, d;
flag fWall = ms.fTreeWall, fRet = fFalse;
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize | femsMinSize))
return fFalse;
MazeClear(!fWall);
MakeEntranceExit(0);
UpdateDisplay();
xs = ((xh - xl) >> 1) + fWall; ys = ((yh - yl) >> 1) + fWall;
cedgeMax = xs*ys << 1;
prim = RgAllocate(cedgeMax, PRIM2);
if (prim == NULL)
goto LExit;
for (i = 0; i < cedgeMax; i++)
prim[i].x = prim[i].y = prim[i].iEdge = prim[i].iHeap = -1;
// Create a list of all passage/wall segments, each with its own weight.
j = fWall << 1;
for (y = 1; y < yh-yl+1; y += 2)
for (x = 2-j; x < xh-xl+j; x += 2) {
prim[cedge].x = x; prim[cedge].y = y;
prim[ZPrimXY(x, y)].iEdge = cedge;
cedge++;
}
for (y = 2-j; y < yh-yl+j; y += 2)
for (x = 1; x < xh-xl+1; x += 2) {
prim[cedge].x = x; prim[cedge].y = y;
prim[ZPrimXY(x, y)].iEdge = cedge;
cedge++;
}
Assert(cedge < cedgeMax);
if (ms.fTreeRandom) {
for (i = 0; i < cedge; i++) {
j = Rnd(0, cedge-1);
// Don't use SwapN here because i may equal j.
d = prim[i].x; prim[i].x = prim[j].x; prim[j].x = d;
d = prim[i].y; prim[i].y = prim[j].y; prim[j].y = d;
}
for (i = 0; i < cedge; i++)
prim[ZPrimXY(prim[i].x, prim[i].y)].iEdge = i;
}
// Start with an initial point or boundary wall of edges.
if (!fWall) {
if (!ms.fSolveDotExit) {
x = RndSkip(1-fWall, xh-xl-1+fWall);
y = RndSkip(1-fWall, yh-yl-1+fWall);
} else {
x = xp; y = yp;
if (fWall) {
x &= ~1; y &= ~1;
} else {
x |= 1; y |= 1;
}
}
} else {
for (i = 0; i <= 1; i++) {
for (x = 2; x < xh-xl; x += 2) {
j = prim[ZPrimXY(x, i ? 1 : yh-yl-1)].iEdge;
if (j < 0)
continue;
prim[iFrontier].iHeap = j;
if (ms.fTreeRandom)
PushupPrim(prim, iFrontier);
iFrontier++;
}
for (y = 2; y < yh-yl; y += 2) {
j = prim[ZPrimXY(i ? 1 : xh-xl-1, y)].iEdge;
if (j < 0)
continue;
prim[iFrontier].iHeap = j;
if (ms.fTreeRandom)
PushupPrim(prim, iFrontier);
iFrontier++;
}
}
goto LNext;
}
loop {
Set(xl + x, yl + y, fWall);
if (fCellMax)
break;
// Add edges surrounding new point to frontier set.
for (d = 0; d < DIRS; d++) {
xnew2 = xl + x + xoff2[d]; ynew2 = yl + y + yoff2[d];
if (!FLegalMaze2(xnew2, ynew2) || GetFast(xnew2, ynew2) == fWall)
continue;
xnew = x + xoff[d]; ynew = y + yoff[d];
i = prim[ZPrimXY(xnew, ynew)].iEdge;
if (i < 0)
continue;
prim[iFrontier].iHeap = i;
if (ms.fTreeRandom)
PushupPrim(prim, iFrontier);
iFrontier++;
}
LNext:
// Select next edge to consider.
if (iFrontier <= 0)
break;
iFrontier--;
if (ms.fTreeRandom) {
// Full: All edge weights are different, so maintain heap.
i = prim[0].iHeap;
prim[0].iHeap = prim[iFrontier].iHeap;
jParent = 0; j = 1;
// Given a valid heap and an item that's replaced its top, push that
// item down through the heap so the heap becomes valid again.
while (j < iFrontier && (FComparePrim(j, jParent)) ||
(j+1 < iFrontier && FComparePrim(j+1, jParent))) {
j += (j+1 < iFrontier && FComparePrim(j+1, j));
SwapN(prim[j].iHeap, prim[jParent].iHeap);
jParent = j; j = (j << 1) + 1;
}
} else {
// Simplified: All edges weights are same, so select randomly.
j = Rnd(0, iFrontier);
i = prim[j].iHeap;
prim[j].iHeap = prim[iFrontier].iHeap;
}
// Carve edge and expand Maze to include new point.
xnew = prim[i].x; ynew = prim[i].y;
xInc = FOdd(ynew) ^ fWall; yInc = !xInc;
if (GetFast(xl + xnew + xInc, yl + ynew + yInc) == fWall) {
neg(xInc); neg(yInc);
}
x = xnew + xInc; y = ynew + yInc;
if (GetFast(xl + x, yl + y) == fWall)
goto LNext;
Set(xl + xnew, yl + ynew, fWall);
}
fRet = fTrue;
LExit:
if (prim != NULL)
DeallocateP(prim);
return fRet;