-
Notifications
You must be signed in to change notification settings - Fork 7
/
maze.cpp
2295 lines (2003 loc) · 66.2 KB
/
maze.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.4) File: maze.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-2023 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 utilities, and all Maze operations outside of
** actual creation and solving.
**
** Created: 4/11/1991.
** Last code change: 8/29/2023.
*/
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include "util.h"
#include "graphics.h"
#include "color.h"
#include "threed.h"
#include "maze.h"
#define TRIES 250
CONST char *rgszDir[DIRS] = {"North", "West", "South", "East"};
// Order of directions to take when wall following in a 3D Maze.
CONST int dir3D[DIRS3] = {0, 4, 1, 2, 5, 3};
// DOS characters used by the DOS Text save commands.
CONST char rgchDOS[4] = {' ', '\334', '\337', '\333'};
CONST char rgchDOS2[16] =
{' ', '\263', '\304', '\331', '\263', '\263', '\277', '\264',
'\304', '\300', '\304', '\301', '\332', '\303', '\302', '\305'};
CONST char rgchDOS3[16] =
{' ', '\272', '\315', '\274', '\272', '\272', '\273', '\271',
'\315', '\310', '\315', '\312', '\311', '\314', '\313', '\316'};
MS ms = {
// File settings
0, 0, 0, fFalse, fFalse, 0, 1,
// Random settings
109, 0, 0,
// Dot settings
0,
// Maze settings
fFalse, fFalse, fFalse, fFalse, fFalse, fFalse, fFalse, 239, 0,
3, 3, 1, epRandom,
// Labyrinth settings
5, lcsCircle, 1, fFalse, "3214765", fFalse, 2, 2, 2, 2, lccBalanced, 0,
// Create settings
fTrue, fTrue, fTrue, 13, 2, -1, -1, 0, "a2a3a0a1",
fFalse, fTrue, 10, 1, -100, 15, 15, 0, 4, 4, 3, fFalse,
fFalse, 1000, TRIES, 0, 0, 0, fFalse, fFalse, fFalse, 4,
// Macro accessible only settings
-1, 1, 10, 50, 0,
// Internal settings
1, 0, 1, 0, 0, 0, 0, 0, -1, NULL, fFalse, 0, NULL, 0};
int xl = 0, yl = 0, xh = Odd(xStart), yh = Odd(yStart);
/*
******************************************************************************
** Maze Utilities
******************************************************************************
*/
// Display an error message if the active rectangle section of the bitmap
// isn't at least x by y pixels.
flag FMazeSizeError(int x, int y)
{
if (xh-xl+1 < x || yh-yl+1 < y) {
PrintSzNN_W("Bitmap section needs to be at least %d by %d!\n", x, y);
return fTrue;
}
return fFalse;
}
// Prepare the bitmap before generating a Maze. Resize the bitmap, and adjust
// the active rectangle within it appropriately.
flag CMaz::FEnsureMazeSize(int z, int grfems)
{
int x = m_x, y = m_y, i;
// Ensure the bitmap isn't too large for the Maze.
if ((grfems & fems64K) > 0 && F64K()) {
PrintSz_W("Bitmap needs to be smaller than 65536 by 65536!\n");
return fFalse;
}
// Ensure the bitmap is large enough for the Maze.
if (x < z || y < z) {
if ((grfems & femsMinSize) == 0) {
PrintSzNN_W("Bitmap needs to be at least %d by %d!\n", z, z);
return fFalse;
}
if (x < z)
x = z;
if (y < z)
y = z;
if (!FBitmapSizeSet(x, y))
return fFalse;
}
// If Apply Commands To Rectangle Section is off, or the Maze type in
// question must fill the whole bitmap, make active rectangle whole bitmap.
if (!ms.fSection || (grfems & femsNoSection) > 0) {
if ((grfems & femsNoResize) == 0) {
x = m_x; y = m_y;
if (grfems & femsEvenSize) {
x &= ~1; y &= ~1;
} else if (grfems & femsOddSize) {
x = Odd(x); y = Odd(y);
}
if (grfems & femsEvenSize2) {
i = (grfems & femsOddSize) > 0;
if (x + i & 2)
x -= 2;
if (y + i & 2)
y -= 2;
}
if (!FBitmapSizeSet(x, y))
return fFalse;
SetXyh();
return fTrue;
}
SetXyh();
}
// Adjust or resize the active rectangle.
if (xh < xl)
SwapN(xl, xh);
if (yh < yl)
SwapN(yl, yh);
if (xl < 0)
xl = 0;
else if (xh >= m_x)
xh = m_x - 1;
if (yl < 0)
yl = 0;
else if (yh >= m_y)
yh = m_y - 1;
if (grfems & femsEvenStart) {
if (FOdd(xl))
xl++;
if (FOdd(yl))
yl++;
}
if (grfems & femsEvenSize) {
if (!FOdd(xh - xl))
xh--;
if (!FOdd(yh - yl))
yh--;
} else if (grfems & femsOddSize) {
if (FOdd(xh - xl))
xh--;
if (FOdd(yh - yl))
yh--;
}
if (grfems & femsEvenSize2) {
i = (grfems & femsOddSize) > 0;
if (xh - xl + 1 + i & 2)
xh -= 2;
if (yh - yl + 1 + i & 2)
yh -= 2;
}
if (xh - xl + 1 < z || yh - yl + 1 < z) {
PrintSzNN_W("Section needs to be at least %d by %d!\n", z, z);
return fFalse;
}
return fTrue;
}
// Turn off the bottom row and right column of the active rectangle.
void CMaz::BlockOuter(KV o)
{
LineY(xh, yl, yh-1, o);
LineX(xl, xh, yh, o);
}
// Set or clear all pixels in the active rectangle.
void CMaz::MazeClear(KV o)
{
if (xl <= 0 && yl <= 0 && xh >= m_x-2 && yh >= m_y-2) {
BitmapSet(o);
if (o) {
if (xh == m_x-2)
LineY(xh+1, yl, yh+1, fOff);
if (yh == m_y-2)
LineX(xl, xh+1, yh+1, fOff);
}
} else
Block(xl, yl, xh, yh, o);
if (!o)
Edge(xl, yl, xh, yh, fOn);
}
// Add an entrance (in top row) or exit (in bottom row) at a random location.
// Implements the Add Entrance and Add Exit commands.
int CMaz::AddEntranceExit(flag fExit)
{
int x, y = fExit ? yh - (FOdd(yh - yl) && !Get(0, yh)) : yl, i;
// Search for a location that doesn't already have an opening.
do {
for (i = 0; i < TRIES; i++) {
switch (ms.nEntrancePos) {
case 0:
x = fExit ? (xh-2 | 1) - i*2 : (xl | 1) + i*2;
break;
case 1:
x = (((xl + xh) >> 1) | 1) + (FOdd(i) ? i & ~1 : -i);
break;
default:
x = Rnd(xl, xh-2) | 1;
}
if (!FBetween(x, xl | 1, xh-2 | 1))
break;
if (Get(x, y) && !Get(x, y - fExit*2 + 1)) {
Set0(x, y);
return x;
}
}
y += fExit ? -2 : 2;
} while (FBetween(y, yl, yh));
return -1;
}
// Create the entrance and exit in the specified type of Maze, based on the
// Entrance Positioning maze setting.
void CMaz::MakeEntranceExit(int nType)
{
int nEntrancePos = ms.nEntrancePos, xSpan,
xEntrance, xExit, yEntrance, yExit;
// Figure out how many possible entrance/exit positions there are.
switch (nType)
{
case 0: // Passages every other pixel
case 6:
case 7:
xSpan = (xh - xl) >> 1;
break;
case 1: // Passages every pixel
case 2:
xSpan = xh - xl - 1;
break;
case 3: // Passages every 4 pixels
xSpan = (xh - xl) >> 2;
break;
case 4: // Passages every other pixel (3D Maze)
case 5:
xSpan = ((m_x3 + 1) >> 1) - 1;
break;
default:
Assert(fFalse);
}
// Figure out which positions to use for the entrance and exit.
LRetry:
switch (nEntrancePos)
{
case 0: // Corners
xEntrance = 0;
xExit = xSpan - 1;
break;
case 1: // Middle
xEntrance = (xSpan - 1) >> 1;
xExit = xEntrance + !FOdd(xSpan);
break;
case 2: // Balanced Random
xEntrance = Rnd(0, xSpan-1);
xExit = xSpan - 1 - xEntrance;
break;
case 3: // True Random
xEntrance = Rnd(0, xSpan - 1);
xExit = Rnd(0, xSpan - 1);
break;
default:
Assert(fFalse);
}
// Actually create the entrance and exit at the chosen positions.
switch (nType)
{
case 0:
case 6:
case 7:
xEntrance = xl + (xEntrance << 1) + 1;
xExit = xl + (xExit << 1) + 1;
yEntrance = yl;
yExit = yh-(nType == 7 && FOdd(yh));
break;
case 1:
case 2:
xEntrance = xl + 1 + xEntrance;
xExit = xl + 1 + xExit;
yEntrance = yl;
yExit = yh;
if (nType == 2 &&
(Get(xEntrance, yEntrance + 1) || Get(xExit, yExit - 1))) {
if (nEntrancePos < epRandom)
nEntrancePos++;
goto LRetry;
}
break;
case 3:
xEntrance = xl + (xEntrance << 2) + 3;
xExit = xl + (xExit << 2) + 3;
yEntrance = yl;
yExit = yh;
Set0(xEntrance, yEntrance + 1); Set0(xEntrance, yEntrance + 2);
Set0(xExit, yExit - 1); Set0(xExit, yExit - 2);
break;
case 4:
xEntrance = X2((xEntrance << 1) + 1, 0);
xExit = X2((xExit << 1) + 1, Even(m_z3) - 2);
yEntrance = Y2(0, 0);
yExit = Y2(Even(m_y3) - 2, Even(m_z3) - 2);
break;
case 5:
xEntrance = X4(0, (xEntrance << 1) + 1);
xExit = X4(Even(m_w3) - 2, (xExit << 1) + 1);
yEntrance = Y2(0, 0);
yExit = Y4(Even(m_y3) - 2, Even(m_z3) - 2);
break;
default:
Assert(fFalse);
}
if (nType != 7) {
ms.xEntrance = xEntrance; ms.yEntrance = yEntrance;
Set0(xEntrance, yEntrance);
}
if (nType != 6) {
ms.xExit = xExit; ms.yExit = yExit;
Set0(xExit, yExit);
}
}
// Add or remove a random wall segment in a Maze. Implements the Add Passage
// and Add Wall commands.
flag CMaz::MakeIsolationDetachment(flag fDetach)
{
int x, y, i;
if (FMazeSizeError(6, 6))
return fFalse;
// Search for a passage or wall that can be changed.
for (i = 0; i < TRIES; i++) {
y = Rnd(yl+1, yh-1 - FOdd(yh-yl));
x = xl + 1 + FOdd(y) + (Rnd(0, (xh >> 1) - FOdd(y) - 1) << 1);
if (Get(x, y) == fDetach && FOnWall(x, y) && FOnPassage(x, y)) {
Set(x, y, !fDetach);
return fTrue;
}
}
return fFalse;
}
// Would making a wall at the given coordinates create a new dead end? Used in
// the creation of Braid Mazes and when connecting poles, to avoid dead ends.
flag CMaz::FWouldMakeDeadEnd(int x, int y) CONST
{
if ((x - xl) & 1) {
if ((y - yl) & 1)
return fTrue; // Cell center
else
return Count(x, y - 1) >= 2 || Count(x, y + 1) >= 2;
} else {
if ((y - yl) & 1)
return Count(x - 1, y) >= 2 || Count(x + 1, y) >= 2;
else
return fFalse; // Wall vertex
}
}
// Would making a passage at the given coordinates create a new pole? Called
// from DoCrackDeadEnds to avoid making poles.
flag CMaz::FWouldMakePole(int x, int y) CONST
{
if ((x - xl) & 1) {
if ((y - yl) & 1)
return fFalse; // Cell center
else
return (x > xl+1 && Count(x - 1, y) <= 1) ||
(x < xh-1 && Count(x + 1, y) <= 1);
} else {
if ((y - yl) & 1)
return (y > yl+1 && Count(x, y - 1) <= 1) ||
(y < yh-1 && Count(x, y + 1) <= 1);
else
return fTrue; // Wall vertex
}
}
// Would making a wall at the given coordinates create a new inaccessible
// section? Used in the creation of Braid Mazes to avoid isolated sections.
flag CMaz::FWouldMakeIsolation(int x, int y) CONST
{
int m[2], n[2], d[2], d0[2], i;
// Two wall following robots start in opposite directions from the new wall.
for (i = 0; i < 2; i++) {
m[i] = x; n[i] = y;
d[i] = d0[i] = (i << 1) + FOdd(y - yl);
}
// Keep wall following until one of the robots returns to where it started.
loop {
for (i = 0; i < 2; i++) {
d[i] = FollowWall(&m[i], &n[i], d[i], fTrue);
if (m[i] == x && n[i] == y)
goto LDone;
else if ((n[i] <= yl && d[i] == 0) || (n[i] >= yh && d[i] == 2))
d[i] ^= 2; // Treat entrances as dead ends.
}
}
// If the robot returned from the same direction it was sent down (as
// opposed to returning from a different direction) that passage forms a
// blind alley or the sole path to an entrance, so can't put a wall here.
LDone:
return d[i] != d0[i];
}
// Given a cell coordinate and a direction being faced, return the direction
// to move to follow the left or right wall. Used when wall following.
int CMaz::PeekWall(int x, int y, int z, int dir, flag f3D, int right) CONST
{
int xnew, ynew, znew, i, j;
if (right != 1)
right = -1;
if (!f3D) {
// Search for the first available passage leading out of this cell.
dir += 2 + right;
for (i = 0; i < DIRS; i++) {
dir &= DIRS1;
xnew = x + xoff[dir]; ynew = y + yoff[dir];
if (!Get(xnew, ynew))
return dir;
dir += right;
}
} else {
// 3D wall following is implemented like 2D wall following, with the
// assumption that up is northwest, and down is southeast.
for (i = 0; i < DIRS3; i++)
if (dir == dir3D[i])
break;
j = i + 3 + right;
for (i = 0; i < DIRS3; i++) {
if (j < 0)
j += DIRS3;
else if (j >= DIRS3)
j -= DIRS3;
dir = dir3D[j];
xnew = x + xoff3[dir]; ynew = y + yoff3[dir]; znew = z + zoff3[dir];
if (!Get3I(xnew, ynew, znew))
return dir;
j += right;
}
}
return -1;
}
// Follow a wall. Like PeekWall() but also moves to a neighboring cell.
int CMaz::FollowWall(int *x, int *y, int dir, int right) CONST
{
int xnew, ynew;
// Move forward if possible, then return the new direction to face.
xnew = *x + xoff[dir]; ynew = *y + yoff[dir];
if (!Get(xnew, ynew)) {
*x = xnew; *y = ynew;
}
return PeekWall(*x, *y, 0, dir, fFalse, right);
}
// Return whether the passage leading in the given direction from given
// coordinates forms a blind alley with a reasonably small circumference.
flag CMaz::FBlindAlley(int x, int y, int dir) CONST
{
int x0, y0, d0, i, dOld, dd, dSum = 0;
// Send a wall following robot down the passage. Follow for up to Radar
// Length cells, or until the robot returns to where it started from.
x0 = x; y0 = y; d0 = dir;
for (i = ms.nRadar << 2; i > 0; i--) {
dOld = d0; d0 = FollowWall(&x0, &y0, d0, fTrue); dd = (d0 - dOld);
// If the robot returned from the same direction it was sent down, and it
// turned around in the right direction once, this is a blind alley.
if (x0 == x && y0 == y)
return dOld == (dir + 2 & DIRS1) && dSum >= 0;
dSum += (dd == 3 ? 1 : (dd == -3 ? -1 : (dd == -2 ? 2 : dd)));
}
return fFalse;
}
// Follow a passage one cell in the direction being faced, updating the passed
// in coordinates and returning the new direction. Return -1 when a junction
// is reached. Ignore junction passages forming blind alleys smaller than the
// Radar Length setting. Implements the Follow Passages dot motion mode.
int CMaz::FollowPassage(int *x, int *y, int *z, int dir, flag f3D) CONST
{
int xnew, ynew, znew, dirMax, dirRev, d, cf = 0;
flag f[DIRS3];
// Figure out how many passages lead from this cell.
dirMax = f3D ? DIRS3 : DIRS;
for (d = 0; d < dirMax; d++) {
xnew = *x + xoff3[d]; ynew = *y + yoff3[d];
if (!f3D)
f[d] = Get(xnew, ynew);
else {
znew = *z + zoff3[d];
f[d] = Get3M(xnew, ynew, znew);
}
if (f[d])
cf++;
}
// Ignore passages forming small blind alleys if Radar Length is > 0.
if (cf <= 1 && !f3D) {
for (d = 0; d < DIRS; d++) {
if (!f[d] && ms.nRadar > 0 && FBlindAlley(*x, *y, d)) {
f[d] = fTrue;
cf++;
}
}
}
// If there are more than two passages left, stop here.
if (cf <= (f3D ? 3 : 1))
return -1;
// Figure out which passage to take. Don't turn around unless only option.
dirRev = dir < DIRS ? dir ^ 2 : 9-dir;
for (d = 0; d < dirMax; d++) {
if (!f[d] && d != dirRev)
goto LFound;
}
if (!f[dirRev]) {
d = dirRev;
goto LFound;
}
return -1;
LFound:
*x += xoff3[d]; *y += yoff3[d];
if (f3D)
*z += zoff3[d];
return d;
}
// Given a cell coordinate and direction being faced, randomly return the
// direction of an available passage. Used by the Random dot move command.
int CMaz::PeekRandom(int x, int y, int z, int dir, flag f3D) CONST
{
int xnew, ynew, znew, dirMax, dirRev, i, j, cdir = 0;
flag f[DIRS3];
dirMax = f3D ? DIRS3 : DIRS;
dirRev = dir < DIRS ? dir ^ 2 : 9-dir;
// Figure out how many passages lead from this cell.
for (i = 0; i < dirMax; i++) {
f[i] = fFalse;
xnew = x + xoff3[i]; ynew = y + yoff3[i];
if (!f3D) {
if (FLegal(xnew, ynew) && !Get(xnew, ynew)) {
// Don't allow movement away from a wall into the middle of a room.
for (j = 0; j < DIRS2; j++)
if (Get(xnew + xoff[j], ynew + yoff[j])) {
f[i] = fTrue;
cdir++;
break;
}
}
} else {
znew = z + zoff3[i];
if (FLegalCubeLevel(xnew, ynew) && znew >= 0 && znew < Odd(m_z3) &&
!Get3(xnew, ynew, znew)) {
f[i] = fTrue;
cdir++;
}
}
}
// If no passages lead from this cell, return a random direction.
if (cdir == 0)
return Rnd(0, dirMax);
// Don't turn around unless that's the only option.
if (f[dirRev]) {
f[dirRev] = fFalse;
cdir--;
}
// Randomly pick one of the available directions.
if (cdir > 0) {
j = Rnd(1, cdir);
for (i = 0; i < dirMax; i++)
if (f[i]) {
j--;
if (j < 1)
return i;
}
}
return dirRev;
}
// Update coordinates to the first available off pixel in a row or column.
// Implements the Entrance and Exit dot teleport commands.
flag CMaz::FFindPassage(int *x, int *y, flag fVertical) CONST
{
int m = *x, n = *y;
flag fRet = fFalse;
while (fVertical ? ++n < yh : ++m < xh) {
if (!Get(m, n)) {
fRet = fTrue;
break;
}
}
*x = m; *y = n;
return fRet;
}
// Randomly return the direction of a cell adjacent to the given coordinates
// that's not part of the Maze yet, evenly distributed among the available
// directions. Used by several Maze creation algorithms.
int CMaz::DirFindUncreated(int *x, int *y, flag fWall) CONST
{
int rgdir[DIRS1], xnew, ynew, d, i, cdir;
// Fast case: Try a random direction. If can move there, done already.
d = RndDir();
xnew = *x + xoff2[d]; ynew = *y + yoff2[d];
if (FLegalMaze(xnew, ynew) && (Get(xnew, ynew) ^ fWall)) {
*x = xnew; *y = ynew;
return d;
}
// Standard case: Figure out how many other passages lead from this cell.
cdir = 0;
for (i = 0; i < DIRS1; i++) {
DirInc(d);
xnew = *x + xoff2[d]; ynew = *y + yoff2[d];
if (FLegalMaze2(xnew, ynew) && (Get(xnew, ynew) ^ fWall)) {
rgdir[cdir] = d;
cdir++;
}
}
// Randomly pick one of the available directions, if any.
if (cdir < 1)
return -1;
d = rgdir[Rnd(0, cdir-1)];
*x += xoff2[d]; *y += yoff2[d];
return d;
}
// Return a random one of the four directions. Takes into account the Bias and
// Run random settings. Used by many creation and other Maze algorithms.
int RndDir()
{
if (ms.cRunRnd > 0) {
// If in the middle of a random run, return the previous direction again.
ms.cRunRnd--;
} else {
// Randomly pick a new direction, and a new random run length if any.
if (ms.nRndRun > 0)
ms.cRunRnd = Rnd(0, ms.nRndRun);
ms.dirRnd = Rnd(0, DIRS1 + NAbs(ms.nRndBias)*2);
}
// Return a standard direction.
if (ms.dirRnd < DIRS)
return ms.dirRnd;
// Higher random numbers represent bias. Map them to a standard direction.
return ((ms.dirRnd & 1) << 1) + (ms.nRndBias > 0);
}
/*
******************************************************************************
** Maze Operations
******************************************************************************
*/
// Implements the Add Passages and Add Walls normalize commands.
long CMaz::MazeNormalize(flag fWall)
{
int x, y;
long count = 0;
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize))
return fFalse;
if (!fWall) {
// Ensure all odd coordinate pixels in the middle of cells are off.
for (y = yl + 1; y < yh; y += 2)
for (x = xl + 1; x < xh; x += 2) {
if (Get(x, y)) {
count++;
Set0(x, y);
}
}
} else {
// Ensure all even coordinate pixels at wall endpoints are on.
for (y = yl; y <= yh; y += 2)
for (x = xl; x <= xh; x += 2) {
if (!Get(x, y)) {
count++;
Set1(x, y);
}
}
}
return count;
}
// Zoom the bitmap by a factor of two, doubling its size. Unlike normal zoom,
// here each set pixel becomes a 3x3 instead of a 2x2 section in the new
// bitmap. Implements the Expand Set Maze command.
long CMaz::MazeZoomAndExpandSetCells()
{
CMaz bNew;
int x, y;
long count = 0;
// Quickly zoom the bitmap by 200%.
if (!bNew.FAllocate((m_x << 1) + FOdd(m_x), (m_y << 1) + FOdd(m_y), this))
return -1;
bNew.SetXyh();
bNew.BitmapOff();
for (y = 0; y < (bNew.m_y - 1 & ~1); y++)
for (x = 0; x < (bNew.m_x - 1 & ~1); x++)
if (Get(x >> 1, y >> 1))
bNew.Set1(x, y);
// For each set 2x2 block, turn on extra pixels making it a 3x3 block.
for (y = yl + 1; y < yh; y += 2)
for (x = xl + 1; x < xh; x += 2)
if (bNew.Get(x, y)) {
count++;
bNew.Set1(x + 1, y - 1);
bNew.Set1(x + 1, y );
bNew.Set1(x + 1, y + 1);
bNew.Set1(x , y + 1);
bNew.Set1(x - 1, y + 1);
}
CopyFrom(bNew);
return count;
}
// Make each cell in a Maze that's been bias zoomed such that walls are one
// pixel and passages three pixels thick, look like a small room with one
// pixel wide doors to adjacent cells, or undo such edits by changing modified
// pixels back to the way they started. Implements the Room Thinned command.
long CMaz::MazeRoomifyThinnedCells()
{
int x, y;
long count = 0;
if (!FEnsureMazeSize(5, femsOddSize | femsNoResize))
return fFalse;
for (y = yl + 2; y < yh - 1; y += 4)
for (x = xl + 2; x < xh - 1; x += 4)
if (FOnMaze2(x, y)) {
count++;
Inv(x, y);
if (!Get(x - 2, y)) {
Inv(x - 2, y - 1);
Inv(x - 2, y + 1);
}
if (!Get(x, y - 2)) {
Inv(x - 1, y - 2);
Inv(x + 1, y - 2);
}
if (!Get(x + 2, y) && (x + 6 > xh || !FOnMaze2(x + 4, y))) {
Inv(x + 2, y - 1);
Inv(x + 2, y + 1);
}
if (!Get(x, y + 2) && (y + 6 > yh || !FOnMaze2(x, y + 4))) {
Inv(x - 1, y + 2);
Inv(x + 1, y + 2);
}
}
return count;
}
// Make all wall endpoints in a Maze point in a new random direction.
// Implements the Tweak Endpoints command.
long CMaz::MazeTweakEndpoints()
{
int x, y, dir;
long count = 0;
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize))
return fFalse;
for (y = yl + 2; y < yh - 1; y += 2)
for (x = xl + 2; x < xh - 1; x += 2)
if (FOnMaze2(x, y) && Count(x, y) == 1) {
count++;
Set0(x, y-1); Set0(x-1, y); Set0(x, y+1); Set0(x+1, y);
dir = Rnd(0, DIRS1);
Set1(x + xoff[dir], y + yoff[dir]);
}
return count;
}
#define NBinary2(f0, f1, f2, f3, f4, f5, f6, f7) \
((f0 << 3) | (f1 << 6) | (f2 << 2) | (f3 << 5) | \
(f4 << 1) | (f5 << 4) | (f6 << 0) | (f7 << 7))
// Slightly modify or shake passages in a Maze that are next to solid cells.
// Implements the Tweak Passages command.
long CMaz::MazeTweakPassages()
{
int nCorner, nEdge, nUturn, xspan, yspan, x, y, grf, grfToggle, i;
flag fCorner, fEdge, fUturn, f1, f2, f3, f4;
long total, z, zInc, j, count = 0;
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize))
return fFalse;
nCorner = ms.nTweakPassage / 10 % 10;
nEdge = ms.nTweakPassage % 10;
nUturn = ms.nTweakPassage / 100;
xspan = (xh - xl - 2) >> 1;
yspan = (yh - yl - 2) >> 1;
total = xspan * yspan;
z = Rnd(1, total);
zInc = LPrime(total);
for (j = 0; j < total; j++) {
z += zInc;
while (z >= total)
z -= total;
y = z / xspan;
x = xl + ((z - y * xspan) << 1) + 2;
y = yl + (y << 1) + 2;
if (Get(x, y) && FOnMaze2(x, y) && Count2(x, y) == 4) {
fCorner = nCorner > 0 && Rnd(1, nCorner) == 1;
fEdge = nEdge > 0 && Rnd(1, nEdge) == 1;
fUturn = nUturn > 0 && Rnd(1, nUturn) == 1;
if (!(fCorner || fEdge || fUturn))
continue;
// Figure out the status of pixels surrounding this wall endpoint.
grf = 0;
for (i = 0; i < DIRS2; i++)
grf = (grf << 1) | Get(x + xoff[i], y + yoff[i]);
f1 = Get(x-1, y-2) && Get(x-2, y-1);
f2 = Get(x-1, y+2) && Get(x-2, y+1);
f3 = Get(x+1, y+2) && Get(x+2, y+1);
f4 = Get(x+1, y-2) && Get(x+2, y-1);
// Toggle between types of corner.
if (fCorner) {
if (grf == NBinary2(0,0,0,1,1,1,0,0) && f1) {
grfToggle = NBinary2(1,1,0,1,1,1,0,1);
goto LTweak;
}
if (grf == NBinary2(0,0,0,0,0,1,1,1) && f2) {
grfToggle = NBinary2(0,1,1,1,0,1,1,1);
goto LTweak;
}
if (grf == NBinary2(1,1,0,0,0,0,0,1) && f3) {
grfToggle = NBinary2(1,1,0,1,1,1,0,1);
goto LTweak;
}
if (grf == NBinary2(0,1,1,1,0,0,0,0) && f4) {
grfToggle = NBinary2(0,1,1,1,0,1,1,1);
goto LTweak;
}
}
// Turn straight edges into U-turns.
if (fEdge) {
if (grf == NBinary2(0,1,1,1,1,1,0,0)) {
grfToggle = NBinary2(0,1,1,1,1,1,0,1);
goto LTweak;
}
if (grf == NBinary2(0,0,0,1,1,1,1,1)) {
grfToggle = NBinary2(0,1,0,1,1,1,1,1);
goto LTweak;
}
if (grf == NBinary2(1,1,0,0,0,1,1,1)) {
grfToggle = NBinary2(1,1,0,1,0,1,1,1);
goto LTweak;
}
if (grf == NBinary2(1,1,1,1,0,0,0,1)) {
grfToggle = NBinary2(1,1,1,1,0,1,0,1);
goto LTweak;
}
}
// Turn U-turns into straight edges.
if (fUturn) {
if (grf == NBinary2(0,0,0,0,0,0,0,1) && f2 && f3) {
grfToggle = NBinary2(0,1,1,1,1,1,0,1);
goto LTweak;
}
if (grf == NBinary2(0,1,0,0,0,0,0,0) && f3 && f4) {
grfToggle = NBinary2(0,1,0,1,1,1,1,1);
goto LTweak;
}
if (grf == NBinary2(0,0,0,1,0,0,0,0) && f4 && f1) {
grfToggle = NBinary2(1,1,0,1,0,1,1,1);
goto LTweak;
}
if (grf == NBinary2(0,0,0,0,0,1,0,0) && f1 && f2) {
grfToggle = NBinary2(1,1,1,1,0,1,0,1);
goto LTweak;
}
}
continue;
LTweak:
// Invert pixels next to the wall endpoint to perform the toggle.
count++;
for (i = 0; i < DIRS2; i++)
if (grfToggle & (1 << (DIRS2-1 - i)))
Inv(x + xoff[i], y + yoff[i]);
}
}