-
Notifications
You must be signed in to change notification settings - Fork 7
/
create2.cpp
3063 lines (2734 loc) · 95.9 KB
/
create2.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: create2.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 creation algorithms, specifically algorithms that
** produce orthogonal Mazes, however which aren't standard 2D Mazes.
**
** Created: 11/22/1996.
** Last code change: 8/29/2023.
*/
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#include "graphics.h"
#include "color.h"
#include "threed.h"
#include "maze.h"
/*
******************************************************************************
** Virtual Fractal Maze Routines
******************************************************************************
*/
enum _virtualmazetype {
vmtNested = 0, // Nested cell fractal Maze
vmtBinary = 1, // Binary tree Maze
vmtDivision = 2, // Recursive division Maze
vmtUnicursal = 3, // Random Labyrinth
vmtClassical = 4, // Classical Labyrinth
vmtHilbert = 5, // Hilbert curve Labyrinth
};
// Quickly create a 2x2 passage Maze in a bitmap. The bitmap is assumed to be
// of appropriate size to contain it. Used to quickly create fractal Mazes
// that nest 2x2 Mazes.
void CMaz::TwoByTwoGenerate()
{
int d;
BitmapOff();
LineX(0, 4, 0, fOn); LineX(0, 4, 4, fOn);
LineY(0, 1, 3, fOn); LineY(4, 1, 3, fOn);
Set1(2, 2);
// Randomly connect the center vertex to one of the four boundary walls.
d = RndDir();
Set1(2 + xoff[d], 2 + yoff[d]);
MakeEntranceExit(0);
UpdateDisplay();
}
// Create a section of a fractal Maze. Recursively calls itself to create the
// subsections or Mazes within each cell of the Maze at this nesting level.
void CMaz::FractalGenerate(CMaz &bT, int z, int xp, int yp)
{
int xsMax = 1, ysMax = 1, x, y, i;
z--;
for (i = 0; i < z; i++) {
xsMax *= ms.xFractal; ysMax *= ms.yFractal;
}
// Create the Maze for this nesting level.
if (ms.xFractal != 2 || ms.yFractal != 2)
bT.CreateMazePerfect();
else
bT.TwoByTwoGenerate();
// Each Maze contains a top and left edge (but no bottom or right edge) with
// a random entrance in those edges. Those entrances may or may not be
// visible when this Maze is nested within surrounding Mazes. For the
// outermost Maze, don't have either entrance (they'll be added later).
if (z+1 < ms.zFractal)
bT.Set0(0, (Rnd(1, ms.yFractal) << 1) - 1);
else
bT.Set1(ms.xEntrance, ms.yEntrance);
// Render the Maze at appropriate zoom level and location.
BlockMoveMaze(bT, 0, 0, bT.m_x-2, bT.m_y-2, xp << 1, yp << 1,
xsMax << 1, 0, 0, ysMax << 1);
// Recursively create a Maze within each cell of this Maze.
if (z > 0)
for (y = 0; y < ms.yFractal; y++)
for (x = 0; x < ms.xFractal; x++)
FractalGenerate(bT, z, xp + x * xsMax, yp + y * ysMax);
}
// Create a new nested fractal Maze, composed of a Maze with smaller Mazes
// recursively nested within each cell. The fractal Maze's size is defined by
// the Fractal fields in the Create Settings dialog.
flag CMaz::CreateMazeFractal()
{
CMaz bT;
char sz[cchSzMax];
int xsMax = 1, ysMax = 1, i;
// Ensure the fractal Maze will fit within a bitmap.
for (i = 0; i < ms.zFractal; i++) {
if ((xsMax * ms.xFractal << 1) + 1 > xBitmap ||
(ysMax * ms.yFractal << 1) + 1 > yBitmap) {
sprintf(S(sz),
"%d by %d fractal Maze can't have a nesting level greater than %d.",
ms.xFractal, ms.yFractal, i); PrintSz_W(sz);
return fFalse;
}
xsMax *= ms.xFractal; ysMax *= ms.yFractal;
}
// Create the bitmap for the fractal Maze, and a temporary internal bitmap
// to store the Mazes at each level.
if (!FBitmapSizeSet((xsMax << 1) + 1, (ysMax << 1) + 1))
return fFalse;
if (!bT.FAllocate((ms.xFractal << 1) + 1, (ms.yFractal << 1) + 1, this))
return fFalse;
BitmapOff();
UpdateDisplay();
bT.SetXyh();
// Create the fractal Maze here.
i = ms.nEntrancePos; ms.nEntrancePos = epRandom;
FractalGenerate(bT, ms.zFractal, 0, 0);
ms.nEntrancePos = i;
// Draw the right and bottom boundary walls.
SetXyh();
LineY(xh, 0, yh-1, fOn);
LineX(0, xh, yh, fOn);
MakeEntranceExit(0);
return fTrue;
}
// Draw one subsection of a virtual fractal Maze using given random number
// seed. Called from FractalPartial() to create sections of the fractal Maze.
void CMaz::FractalPartialGenerate(CMaz &bT, int z, long x0, long y0,
int xp, int yp, int nRndSeed, long *pxEntrance, long *pxExit)
{
ulong rgl[4];
long xTotal, yTotal, xsMax = 1, ysMax = 1, xT, yT, xEntrance, xExit;
int xpMax = 1, ypMax = 1, xs, ys, i, nSav, nSav2, nT;
flag fX, fY;
for (i = 0; i < z; i++) {
xpMax *= ms.xFractal; ypMax *= ms.yFractal;
}
for (i = z; i < ms.zFractal; i++) {
xsMax *= ms.xFractal; ysMax *= ms.yFractal;
}
xTotal = xsMax * xpMax; yTotal = ysMax * ypMax;
InitRndL(nRndSeed);
// Figure out where the entrance and exit to the fractal Maze should be.
switch (ms.nEntrancePos) {
case 0:
xEntrance = 0;
xExit = xTotal - 1;
break;
case 1:
xEntrance = (xTotal - 1) >> 1;
xExit = xEntrance + !FOdd(xTotal);
break;
case 2:
xEntrance = Rnd(0, xTotal - 1);
xExit = xTotal - 1 - xEntrance;
break;
case 3:
xEntrance = Rnd(0, xTotal - 1);
xExit = Rnd(0, xTotal - 1);
break;
}
*pxEntrance = xEntrance; *pxExit = xExit;
// Leave the section blank if it's outside the bounds of the whole Maze.
if (x0 >= 0 && x0 < xsMax && y0 >= 0 && y0 < ysMax) {
if (ms.nFractalT <= vmtNested) {
// Create fractal section using hunt and kill algorithm
xT = xsMax; yT = ysMax;
fX = fY = fFalse;
// Get information about the Mazes surrounding starting nesting level.
for (i = z; i < ms.zFractal; i++) {
if (ms.xFractal != 2 || ms.yFractal != 2)
bT.CreateMazePerfect();
else
bT.TwoByTwoGenerate();
if (i > z)
bT.Set0(0, (Rnd(1, ms.yFractal) << 1) - 1);
else
bT.Set1(ms.xEntrance, ms.yEntrance);
xT /= ms.xFractal; yT /= ms.yFractal;
xs = x0 / xT % ms.xFractal; ys = y0 / yT % ms.yFractal;
// The top and/or left edges of the section should be solid if the
// section is adjacent to any surrounding Maze boundary, and that
// surrounding Maze has a wall segment at the appropriate position.
if (y0 % yT == 0)
fX |= bT.Get((xs << 1) + 1, ys << 1);
if (x0 % xT == 0)
fY |= bT.Get(xs << 1, (ys << 1) + 1);
// Set the random number seed for this subsection.
rgl[0] = nRndSeed; rgl[1] = i; rgl[2] = x0 / xT; rgl[3] = y0 / yT;
InitRndRgl(rgl, 4);
}
// Create the fractal Maze section here.
nSav = ms.nEntrancePos; ms.nEntrancePos = epRandom;
FractalGenerate(bT, z, xp, yp);
ms.nEntrancePos = nSav;
} else {
// Create fractal section using binary tree algorithm
rgl[0] = nRndSeed; rgl[1] = z; rgl[2] = x0; rgl[3] = y0;
InitRndRgl(rgl, 4);
nSav = ms.nEntrancePos; ms.nEntrancePos = epCorner;
nSav2 = ms.nRndRun; ms.nRndRun = 0;
bT.CreateMazeBinary();
ms.nEntrancePos = nSav; ms.nRndRun = nSav2;
bT.Set1(1, 0);
xT = (x0 > 0) << 1; yT = (y0 > 0) << 1;
if (ms.nFractalT == vmtBinary)
BlockMove(bT, xT, yT, bT.m_x-4 + xT, bT.m_y-4 + yT, xp << 1, yp << 1);
else {
// This binary tree Maze will be expanded into a unicursal Maze
xT = (x0 < xsMax-1) << 1;
BlockMoveMaze(bT, xT, yT,
bT.m_x/2-2+xT + (FOdd(xpMax)*(FOdd(x0) + (x0 == xsMax-1)) << 1),
bT.m_y/2-2+yT + (FOdd(ypMax)*(FOdd(y0) + (y0 == ysMax-1)) << 1),
(xp + xpMax + FOdd(xpMax)*(FOdd(x0 == xsMax-1)*2 -FOdd(~x0))) << 1,
(yp - 1 - FOdd(ypMax)*FOdd(y0)) << 1, -4, 0, 0, 4);
if (x0 == 0 && FBetween(y0, 0, ysMax-1))
LineY(xp << 1, yp << 1,
(yp + ypMax - FOdd(~ypMax & (y0 == ysMax-1))) << 1, fOn);
if (y0 == ysMax-1 && FBetween(x0, 0, xsMax-1)) {
nT = FOdd(ypMax & (y0 == ysMax-1));
LineX(xp << 1, (xp + xpMax) << 1, (yp + ypMax - 1 + nT) << 1, fOn);
LineX(xp << 1, (xp + xpMax) << 1, (yp + ypMax + nT) << 1, fOff);
}
}
fX = fY = fFalse;
}
} else {
// Ensure the boundary walls at the right and bottom get drawn.
fX = y0 == ysMax && FBetween(x0, 0, xsMax-1);
fY = x0 == xsMax && FBetween(y0, 0, ysMax-1);
}
// Draw lines at the top and/or left of the section, if there should be no
// passage between cells at this nesting level.
if (fX)
LineX(xp << 1, (xp + xpMax) << 1, yp << 1, fOn);
if (fY)
LineY(xp << 1, yp << 1, (yp + ypMax) << 1, fOn);
// Draw the entrance or exit to the whole Maze if visible.
if (y0 == 0 && x0 == xEntrance / xpMax)
Set0(((xp + LMod(xEntrance, xpMax)) << 1) + 1, yp << 1);
if (y0 == ysMax && x0 == xExit / xpMax)
Set0(((xp + LMod(xExit, xpMax)) << 1) + 1, yp << 1);
}
// Create a grid of subsections of a virtual fractal Maze, where only part of
// the fractal Maze is rendered. Implements the FractalPart operation.
flag CMaz::FractalPartial(long x0, long y0, int z, int nRndSeed,
long *pxEntrance, long *pxExit)
{
CMaz bT;
char sz[cchSzMax];
int xpMax = 1, ypMax = 1, x, y, i, x1, x2, y1, y2, m1, n1;
long xT, yT;
// Ensure the subsections of the fractal Maze to make will fit in a bitmap.
for (i = 0; i < z; i++) {
if ((long)xpMax * ms.xFractal * 8 > xBitmap ||
(long)ypMax * ms.yFractal * 8 > yBitmap) {
sprintf(S(sz),
"%d by %d fractal section can't have nesting level greater than %d.",
ms.xFractal, ms.yFractal, i); PrintSz_W(sz);
return fFalse;
}
xpMax *= ms.xFractal; ypMax *= ms.yFractal;
}
// Ensure the total size of the fractal Maze doesn't overflow 32 bits.
xT = xpMax; yT = ypMax;
for (i = z; i < ms.zFractal; i++) {
if (xT * ms.xFractal / ms.xFractal != xT ||
yT * ms.yFractal / ms.yFractal != yT) {
sprintf(S(sz),
"%d by %d fractal Maze can't have total nesting greater than %d.",
ms.xFractal, ms.yFractal, i); PrintSz_W(sz);
return fFalse;
}
xT *= ms.xFractal; yT *= ms.yFractal;
}
// Create the bitmap for the fractal Maze, and a temporary internal bitmap
// to store the Mazes at each level.
if (!FBitmapSizeSet(xpMax * 8, ypMax * 8))
return fFalse;
if (ms.nFractalT <= vmtBinary || ms.nFractalT == vmtUnicursal) {
if (ms.nFractalT <= vmtNested) {
if (!bT.FAllocate((ms.xFractal << 1) + 1, (ms.yFractal << 1) + 1, this))
return fFalse;
} else {
if (!bT.FAllocate((xpMax << 1) + 3, (ypMax << 1) + 3, this))
return fFalse;
}
BitmapOff();
UpdateDisplay();
bT.SetXyh();
// Create a 4x4 grid of fractal Maze sections.
for (y = 0; y < 4; y++)
for (x = 0; x < 4; x++)
FractalPartialGenerate(bT, z, x0 - 1 + x, y0 - 1 + y,
x * xpMax, y * ypMax, nRndSeed, pxEntrance, pxExit);
// For unicursal Mazes, do adjustments after all raw sections in place.
if (ms.nFractalT == vmtUnicursal) {
SetXyh();
xl = FOdd(xpMax & ~x0)*2;
yl = !FOdd(ypMax & ~y0)*2;
UnicursalApply(fFalse);
xl = yl = 0;
m1 = (x0 - 1)*xpMax; n1 = (y0 - 1)*ypMax;
x1 = NMax(-1 - m1, 0); x2 = NMin(xT - m1 + 1, m_x);
if (x1 < x2) {
LineX(x1 << 1, x2 << 1, (-1 - n1) << 1, fOff);
LineX(x1 << 1, x2 << 1, (yT - n1 + FOdd(ypMax)) << 1, fOff);
}
y1 = NMax(-1 - n1, 0); y2 = NMin(yT - n1, m_y);
if (y1 < y2)
LineY((xT - m1 + FOdd(xpMax)) << 1, y1 << 1, y2 << 1, fOff);
if (!FOdd(xpMax & ypMax)) {
Set0(((xT-1 - m1) << 1) + 1, (yT-1 - n1) << 1);
Set0(((xT-1 - m1) << 1) - 1, (yT-1 - n1) << 1);
Set1((xT-1 - m1) << 1, ((yT-1 - n1) << 1) - 1);
} else
Set0(((xT - m1) << 1) - 1, (yT - n1) << 1);
*pxEntrance = 0;
*pxExit = xT - 2 + FOdd(xpMax);
}
// Recursive division and Labyrinth virtual Mazes can be made all at once.
} else if (ms.nFractalT == vmtDivision) {
DivisionPartial(xT, yT, (x0 - 1)*xpMax, (y0 - 1)*ypMax,
nRndSeed, pxEntrance, pxExit);
} else if (ms.nFractalT == vmtHilbert) {
Assert(xT == yT);
LabyrinthHilbertPartial(ms.zFractal, (x0 - 1)*xpMax, (y0 - 1)*ypMax);
*pxEntrance = 0; *pxExit = xT - 1;
} else {
Assert(xT == yT);
i = (xT >> 3) - 1;
LabyrinthClassicalPartial(i, (x0 - 1)*xpMax, (y0 - 1)*ypMax);
*pxEntrance = *pxExit = (i << 2) + 3;
}
return fTrue;
}
#define VX2(x) (((x) < x1 ? -1 : ((x) > x2 ? m_x : (x)-x1))*2)
#define VY2(y) (((y) < y1 ? -1 : ((y) > y2 ? m_y : (y)-y1))*2)
// Create a subsection of a virtual Maze, in which only part of the Maze is
// rendered, using the recursive division algorithm.
flag CMaz::DivisionPartial(int xs, int ys, int x1, int y1,
int nRndSeed, long *pxEntrance, long *pxExit)
{
ulong rgl[5];
RC *rgrect, rect, *pr;
int x2, y2, xEntrance, xExit, x, y;
flag f;
if (!FEnsureMazeSize(3, femsOddSize | femsNoResize | femsMinSize))
return fFalse;
rgrect = RgAllocate(ms.nCrackPass*4, RC);
if (rgrect == NULL)
return fFalse;
// Start with one rectangle in stack covering the entire Maze.
InitRndL(nRndSeed);
x2 = x1 + (m_x >> 1); y2 = y1 + (m_y >> 1);
rect.x1 = rect.y1 = 0; rect.x2 = xs; rect.y2 = ys;
pr = rgrect;
*pr = rect;
BitmapOff();
// Do Edge(-x1*2, -y1*2, (xs-x1)*2, (ys-y1)*2) without overflowing 32 bits.
if (FBetween(0, y1, y2)) LineX(VX2(0), VX2(xs), VY2(0), fOn);
if (FBetween(0, x1, x2)) LineY(VX2(0), VY2(0), VY2(ys), fOn);
if (FBetween(ys, y1, y2)) LineX(VX2(0), VX2(xs), VY2(ys), fOn);
if (FBetween(xs, x1, x2)) LineY(VX2(xs), VY2(0), VY2(ys), fOn);
// Figure out where the entrance and exit to the Maze should be.
switch (ms.nEntrancePos) {
case 0:
xEntrance = 0;
xExit = xs - 1;
break;
case 1:
xEntrance = (xs - 1) >> 1;
xExit = xEntrance + !FOdd(xs);
break;
case 2:
xEntrance = Rnd(0, xs - 1);
xExit = xs - 1 - xEntrance;
break;
case 3:
xEntrance = Rnd(0, xs - 1);
xExit = Rnd(0, xs - 1);
break;
}
*pxEntrance = xEntrance; *pxExit = xExit;
Set0((xEntrance-x1)*2+1, -y1*2);
Set0((xExit -x1)*2+1, (ys-y1)*2);
UpdateDisplay();
// Continue while there's at least one rectangle on the stack.
while (pr >= rgrect) {
rect = *pr;
if (rect.x2 - rect.x1 < 2 || rect.y2 - rect.y1 < 2 ||
rect.x1 > x2 || rect.x2 < x1 || rect.y1 > y2 || rect.y2 < y1 ||
pr-rgrect >= ms.nCrackPass*4 - 1) {
// If rectangle is too small, or is outside visible viewport, skip it.
pr--;
continue;
}
if (fCellMax)
break;
// Set the random number seed for this subrectangle.
rgl[0] = nRndSeed;
rgl[1] = rect.x1; rgl[2] = rect.y1; rgl[3] = rect.x2; rgl[4] = rect.y2;
InitRndRgl(rgl, 5);
// Determine whether to divide current rectangle horizontal or vertical.
if (ms.nRndRun)
f = Rnd(-50, 49) >= ms.nRndBias;
else
f = Rnd(0, ((rect.x2 - rect.x1) >> 1) + ((rect.y2 - rect.y1) >> 1)) +
ms.nRndBias <= ((rect.x2 - rect.x1) >> 1);
// Divide current rectangle and push two subrectangles on stack.
if (f) {
x = Rnd(rect.x1 + 1, rect.x2 - 1);
if (FBetween(x, x1, x2)) {
LineY(VX2(x), VY2(rect.y1)+1, VY2(rect.y2)-1, fOn);
Set0(VX2(x), VY2(Rnd(rect.y1+1, rect.y2-1)) + 1);
}
pr->x1 = x; pr->y1 = rect.y1; pr->x2 = rect.x2; pr->y2 = rect.y2;
pr++;
pr->x1 = rect.x1; pr->y1 = rect.y1; pr->x2 = x; pr->y2 = rect.y2;
} else {
y = Rnd(rect.y1 + 1, rect.y2 - 1);
if (FBetween(y, y1, y2)) {
LineX(VX2(rect.x1)+1, VX2(rect.x2)-1, VY2(y), fOn);
Set0(VX2(Rnd(rect.x1+1, rect.x2-1)) + 1, VY2(y));
}
pr->x1 = rect.x1; pr->y1 = y; pr->x2 = rect.x2; pr->y2 = rect.y2;
pr++;
pr->x1 = rect.x1; pr->y1 = rect.y1; pr->x2 = rect.x2; pr->y2 = y;
}
}
DeallocateP(rgrect);
return fTrue;
}
/*
******************************************************************************
** 3D Maze Routines
******************************************************************************
*/
CONST KV rgkvClar[DIRS] = {kvRed, kvBlue, kvGreen, kvYellow};
// Return the status of a pixel in a 3D Maze. Daedalus 3D Maze bitmaps don't
// have ceilings above the top level or floors below the bottom, so assume
// them here.
bit CMaz::Get3M(int x, int y, int z) CONST
{
if (x < 0 || x >= m_x3 || y < 0 || y >= m_y3 || z < -1 || z > Odd(m_z3))
return fOff;
if (z == -1 || z == Odd(m_z3))
return fOn;
x = X2(x, z); y = Y2(y, z);
return _Get(x, y);
}
// Return the status of a pixel in a 3D Maze, where the 3D Maze is assumed to
// be in the perspective inside view. This is like Get3M() except each floor
// extends forever throughout the plane. This prevents the Maze from appearing
// suspended in space, with just the top level sticking out of the ground.
bit CMaz::Get3I(int x, int y, int z) CONST
{
if (x < 0 || x >= m_x3 || y < 0 || y >= m_y3 || z < 0 || z >= Odd(m_z3))
return z & 1;
x = X2(x, z); y = Y2(y, z);
return _Get(x, y);
}
// Create a new perfect 3D Maze in the bitmap, stored as a sequence of 2D
// levels. The dimensions of the Maze are taken from the Bitmap Size dialog.
flag CMaz::CreateMaze3D()
{
int tx, ty, tz, x, y, z, xnew, ynew, znew,
fHunt = fFalse, pass = 0, d, i;
long count;
if (!FCubeSizeSet(m_x3, m_y3, m_z3, m_w3))
return fFalse;
BitmapOff();
if (!FEnsureMazeSize(3, femsNoResize))
return fFalse;
tx = Even(m_x3); ty = Even(m_y3); tz = Even(m_z3);
CubeBlock(0, 0, 0, tx-2, ty-2, tz-2, fOn);
MakeEntranceExit(4);
x = y = 1; z = 0;
Set30(x, y, z);
count = (long)((tx - 1) >> 1) * ((ty - 1) >> 1) * (tz >> 1) - 1;
UpdateDisplay();
if (count <= 0)
goto LDone;
// Use the Hunt and Kill algorithm to create the 3D Maze.
loop {
if (ms.nRndBias > 0 && Rnd(0, ms.nRndBias) > 0)
i = DIRS1;
else
i = DIRS3-1;
d = Rnd(0, i);
if (!Get3(x, y, z)) {
for (i = 0; i < DIRS3; i++) {
xnew = x + (xoff3[d] << 1);
ynew = y + (yoff3[d] << 1);
znew = z + (zoff3[d] << 1);
if (FLegalMaze3(xnew, ynew, znew) && Get3(xnew, ynew, znew))
break;
d++;
if (d >= DIRS3)
d = 0;
}
if (ms.fRiver)
fHunt = (i >= DIRS3);
else {
fHunt = i > 0;
if (i < DIRS3)
pass = 0;
}
}
if (!fHunt) {
if (fCellMax)
break;
Set30((x + xnew) >> 1, (y + ynew) >> 1, (z + znew) >> 1);
Set30(xnew, ynew, znew);
x = xnew; y = ynew; z = znew;
pass = 0;
count--;
if (count <= 0)
goto LDone;
} else {
x += 2;
if (x >= tx-1) {
x = 1;
y += 2;
if (y >= ty-1) {
y = 1;
z += 2;
if (z >= tz) {
z = 0;
pass++;
if (pass > 1) {
Assert(fFalse);
goto LDone;
}
UpdateDisplay();
}
}
}
}
}
LDone:
return fTrue;
}
// Create a new perfect 3D Maze in the bitmap using the Binary Tree algorithm
// (technically a trinary tree in this case) by carving passages.
flag CMaz::CreateMazeBinary3D()
{
int nPercent = 50 + ms.nRndBias, nDir, xInc, iMax,
tx, ty, tz, x, y, z, i, d;
if (!FCubeSizeSet(m_x3, m_y3, m_z3, m_w3))
return fFalse;
BitmapOff();
if (!FEnsureMazeSize(3, femsNoResize))
return fFalse;
tx = Even(m_x3); ty = Even(m_y3); tz = Even(m_z3);
CubeBlock(0, 0, 0, tx-2, ty-2, tz-2, fOn);
MakeEntranceExit(4);
UpdateDisplay();
nDir = ms.nRndRun <= 0 ? -1 : ms.nRndRun >= 100;
iMax = (tx-2) >> 1;
for (z = 0; z < tz; z += 2)
for (y = 1; y < ty-1; y += 2) {
xInc = (nDir < 0 || (nDir == 0 && Rnd(0, 99) < ms.nRndRun)) ? 1 : -1;
x = xInc < 0 ? tx-3 : 1;
for (i = 0; i < iMax; i++) {
// For each cell, carve a passage north, west, or up.
if (fCellMax)
break;
Set30(x, y, z);;
if (i <= 0 && y <= 1 && z <= 0)
goto LNext;
do {
d = Rnd(0, 2);
} while ((d == 0 && i <= 0) || (d == 1 && y <= 1) ||
(d == 2 && z <= 0));
if (d < 2)
Set30(x - (d ^ 1)*xInc, y - d, z);
else
Set30(x, y, z - 1);
LNext:
x += (xInc << 1);
}
}
return fTrue;
}
// Create a new perfect 3D Maze in the bitmap using the Sidewinder algorithm,
// by carving passages.
flag CMaz::CreateMazeSidewinder3D()
{
int nPercent = 50 + ms.nRndBias, tx, ty, tz, x, y, z, dx, f;
if (!FCubeSizeSet(m_x3, m_y3, m_z3, m_w3))
return fFalse;
BitmapOff();
if (!FEnsureMazeSize(3, femsNoResize))
return fFalse;
tx = Even(m_x3); ty = Even(m_y3); tz = Even(m_z3);
CubeBlock(0, 0, 0, tx-2, ty-2, tz-2, fOn);
MakeEntranceExit(4);
LineX(1, tx-3, 1, fOff);
UpdateDisplay();
for (z = 0; z < tz; z += 2)
for (y = 1; y < ty-1; y += 2) {
if (y <= 1 && z <= 0)
continue;
for (x = 1; x < tx-1; x += 2) {
// For each cell, decide whether to carve a passage along x axis.
dx = 0;
while (x < tx-3 && Rnd(0, 99) < nPercent)
dx++, x += 2;
if (fCellMax)
break;
// If no x passage, then carve a randomly positioned y or z passage.
f = y <= 1 ? 0 : (z <= 0 ? 1 : Rnd(0, 1));
if (dx == 0) {
Set30(x, y-f, z-!f);
Set30(x, y, z);
} else {
Set30(x - (Rnd(0, dx) << 1), y-f, z-!f);
LineX(X2(x - (dx << 1), z), X2(x, z), Y2(y, z), fOff);
}
}
}
return fTrue;
}
// Create a new perfect 3D Maze in the bitmap using recursive division. This
// adds planes or walls, recursively dividing the Maze into smaller cubes.
flag CMaz::CreateMazeDivision3D()
{
RC3 *rgcube, cube, *pr;
int x, y, z, n;
if (!FCubeSizeSet(m_x3, m_y3, m_z3, m_w3))
return fFalse;
BitmapOff();
if (!FEnsureMazeSize(3, femsNoResize))
return fFalse;
rgcube = RgAllocate((m_x3 + m_y3 + m_z3) >> 1, RC3);
if (rgcube == NULL)
return fFalse;
// Start with one cube in stack covering the entire Maze.
cube.x1 = cube.y1 = 0; cube.z1 = -1;
cube.x2 = m_x3-1 & ~1; cube.y2 = m_y3-1 & ~1; cube.z2 = (m_z3-1 & ~1) + 1;
pr = rgcube;
*pr = cube;
BitmapOff();
CubeBlock(0, 0, 0, cube.x2, 0, cube.z2-1, fOn);
CubeBlock(0, cube.y2, 0, cube.x2, cube.y2, cube.z2-1, fOn);
CubeBlock(0, 0, 0, 0, cube.y2, cube.z2-1, fOn);
CubeBlock(cube.x2, 0, 0, cube.x2, cube.y2, cube.z2-1, fOn);
MakeEntranceExit(4);
UpdateDisplay();
// Continue while there's at least one cube on the stack.
while (pr >= rgcube) {
cube = *pr;
x = cube.x2 - cube.x1; y = cube.y2 - cube.y1; z = cube.z2 - cube.z1;
if ((x < 4) + (y < 4) + (z < 4) >= 2) {
pr--;
continue;
}
if (fCellMax)
break;
// Determine whether to divide current cube along x or y or z axis.
if (x < 4)
n = (Rnd(0, y + z + ms.nRndBias) <= y) + 1;
else if (y < 4)
n = (Rnd(0, x + z + ms.nRndBias) <= x) << 1;
else if (z < 4)
n = (Rnd(0, x + y) <= x);
else {
n = Rnd(0, x + y + z + ms.nRndBias);
n = (n > x) + (n > x + y);
}
// Divide current cube and push two subcubes on stack.
if (n <= 0) {
x = RndSkip(cube.x1 + 2, cube.x2 - 2);
CubeBlock(x, cube.y1+1, cube.z1+1, x, cube.y2-1, cube.z2-1, fOn);
Set30(x, RndSkip(cube.y1+1, cube.y2-1), RndSkip(cube.z1+1, cube.z2-1));
pr->x1 = x; pr->y1 = cube.y1; pr->z1 = cube.z1;
pr->x2 = cube.x2; pr->y2 = cube.y2; pr->z2 = cube.z2;
pr++;
pr->x1 = cube.x1; pr->y1 = cube.y1; pr->z1 = cube.z1;
pr->x2 = x; pr->y2 = cube.y2; pr->z2 = cube.z2;
} else if (n == 1) {
y = RndSkip(cube.y1 + 2, cube.y2 - 2);
CubeBlock(cube.x1+1, y, cube.z1+1, cube.x2-1, y, cube.z2-1, fOn);
Set30(RndSkip(cube.x1+1, cube.x2-1), y, RndSkip(cube.z1+1, cube.z2-1));
pr->x1 = cube.x1; pr->y1 = y; pr->z1 = cube.z1;
pr->x2 = cube.x2; pr->y2 = cube.y2; pr->z2 = cube.z2;
pr++;
pr->x1 = cube.x1; pr->y1 = cube.y1; pr->z1 = cube.z1;
pr->x2 = cube.x2; pr->y2 = y; pr->z2 = cube.z2;
} else {
z = RndSkip(cube.z1 + 2, cube.z2 - 2);
CubeBlock(cube.x1+1, cube.y1+1, z, cube.x2-1, cube.y2-1, z, fOn);
Set30(RndSkip(cube.x1+1, cube.x2-1), RndSkip(cube.y1+1, cube.y2-1), z);
pr->x1 = cube.x1; pr->y1 = cube.y1; pr->z1 = z;
pr->x2 = cube.x2; pr->y2 = cube.y2; pr->z2 = cube.z2;
pr++;
pr->x1 = cube.x1; pr->y1 = cube.y1; pr->z1 = cube.z1;
pr->x2 = cube.x2; pr->y2 = cube.y2; pr->z2 = z;
}
}
DeallocateP(rgcube);
return fTrue;
}
// Create a section of a 3D fractal Maze. Recursively calls itself to create
// subsections or Mazes within each cell of the Maze at this nesting level.
void CMaz::FractalGenerate3D(CMaz &bT, int w, int xp, int yp, int zp)
{
int xFractal = ms.xFractal, yFractal = ms.xFractal, zFractal = ms.yFractal,
xsMax = 1, ysMax = 1, zsMax = 1, x, y, z, i;
w--;
for (i = 0; i < w; i++) {
xsMax *= xFractal; ysMax *= yFractal; zsMax *= zFractal;
}
// Create the Maze for this nesting level.
bT.CreateMaze3D();
bT.Set1(ms.xEntrance, ms.yEntrance);
for (z = bT.m_z3 - 2; z >= 0; z--)
bT.CubeMove(bT, 0, 0, z, bT.m_x3-1, bT.m_y3-1, z, 0, 0, z+1);
bT.CubeBlock(0, 0, 0, bT.m_x3-1, bT.m_y3-1, 0, fOn);
// Each Maze contains north & west & top faces (but not south & east &
// bottom faces) with a random passage upon those faces. Those passages may
// or may not be visible when this Maze is nested within surrounding Mazes.
// For the outermost Maze, don't have any passages (they'll be added later).
if (w+1 < ms.zFractal) {
bT.Set30(0, (Rnd(1, yFractal) << 1) - 1, (Rnd(1, zFractal) << 1) - 1);
bT.Set30((Rnd(1, xFractal) << 1) - 1, 0, (Rnd(1, zFractal) << 1) - 1);
bT.Set30((Rnd(1, xFractal) << 1) - 1, (Rnd(1, yFractal) << 1) - 1, 0);
}
// Render the Maze at appropriate zoom level and location.
BlockMoveMaze3(bT, 0, 0, 0, bT.m_x3-1, bT.m_y3-1, bT.m_z3-1,
xp << 1, yp << 1, zp << 1, xsMax << 1, ysMax << 1, zsMax << 1);
// Recursively create a Maze within each cell of this Maze.
if (w > 0)
for (z = 0; z < zFractal; z++)
for (y = 0; y < yFractal; y++)
for (x = 0; x < xFractal; x++)
FractalGenerate3D(bT, w,
xp + x * xsMax, yp + y * ysMax, zp + z * zsMax);
}
// Create a new 3D nested fractal Maze, composed of a Maze with smaller Mazes
// recursively nested within each cell. The fractal Maze's size is defined by
// the Fractal fields in the Create Settings dialog.
flag CMaz::CreateMazeFractal3D()
{
CMaz bT;
char sz[cchSzMax];
int xFractal = ms.xFractal, yFractal = ms.xFractal, zFractal = ms.yFractal,
xsMax = 1, ysMax = 1, zsMax = 1, i;
// Ensure the fractal Maze will fit within a bitmap.
for (i = 0; i < ms.zFractal; i++) {
if (xsMax * xFractal / xFractal != xsMax ||
ysMax * yFractal / yFractal != ysMax ||
zsMax * zFractal / zFractal != zsMax) {
sprintf(S(sz),
"%d by %d by %d fractal Maze with %d levels per row "
"can't have a nesting level greater than %d.",
xFractal, yFractal, zFractal, m_w3, i); PrintSz_W(sz);
return fFalse;
}
xsMax *= xFractal; ysMax *= yFractal; zsMax *= zFractal;
}
// Create the bitmap for the fractal Maze, and a temporary internal bitmap
// to store the Mazes at each level.
if (!FCubeSizeSet((xsMax << 1) + 1, (ysMax << 1) + 1, (zsMax << 1),
m_w3))
return fFalse;
if (!bT.FAllocateCube((xFractal << 1) + 1, (yFractal << 1) + 1,
(zFractal << 1), m_w3))
return fFalse;
BitmapOff();
UpdateDisplay();
// Create the fractal Maze here.
i = ms.nEntrancePos; ms.nEntrancePos = epCorner;
FractalGenerate3D(bT, ms.zFractal, 0, 0, 0);
ms.nEntrancePos = i;
// Draw the bottom and east and south boundary walls.
CubeMove(*this, 0, 0, 1, m_x3-1, m_y3-1, m_z3-1, 0, 0, 0);
CubeBlock(0, 0, m_z3-1, m_x3-1, m_y3-1, m_z3-1, fOff);
CubeBlock(xsMax << 1, 0, 0, xsMax << 1, (ysMax << 1) - 1, (zsMax << 1) - 2,
fOn);
CubeBlock(0, ysMax << 1, 0, xsMax << 1, ysMax << 1, (zsMax << 1) - 2, fOn);
MakeEntranceExit(4);
return fTrue;
}
// Draw one subsection of a 3D virtual fractal Maze using given random number
// seed. Called from FractalPartial3D() to create sections of a fractal Maze.
void CMaz::FractalPartialGenerate3D(CMaz &bT, int z, long x0, long y0,
long z0, int xp, int yp, int zp, int nRndSeed,
long *pxEntrance, long *pxExit)
{
ulong rgl[5];
long xFractal = ms.xFractal, yFractal = ms.xFractal, zFractal = ms.yFractal,
xTotal, yTotal, zTotal, xsMax = 1, ysMax = 1, zsMax = 1, xT, yT, zT,
xEntrance, xExit;
int xpMax = 1, ypMax = 1, zpMax = 1, xs, ys, zs, i, nSav, nSav2;
flag fX, fY, fZ;
for (i = 0; i < z; i++) {
xpMax *= xFractal; ypMax *= yFractal; zpMax *= zFractal;
}
for (i = z; i < ms.zFractal; i++) {
xsMax *= xFractal; ysMax *= yFractal; zsMax *= zFractal;
}
xTotal = xsMax * xpMax; yTotal = ysMax * ypMax; zTotal = zsMax * ypMax;
InitRndL(nRndSeed);
// Figure out where the entrance and exit to the fractal Maze should be.
switch (ms.nEntrancePos) {
case 0:
xEntrance = 0;
xExit = xTotal - 1;
break;
case 1:
xEntrance = (xTotal - 1) >> 1;
xExit = xEntrance + !FOdd(xTotal);
break;
case 2:
xEntrance = Rnd(0, xTotal - 1);
xExit = xTotal - 1 - xEntrance;
break;
case 3:
xEntrance = Rnd(0, xTotal - 1);
xExit = Rnd(0, xTotal - 1);
break;
}
*pxEntrance = xEntrance; *pxExit = xExit;
// Leave the section blank if it's outside the bounds of the whole Maze.
if (x0 >= 0 && x0 < xsMax && y0 >= 0 && y0 < ysMax &&
z0 >= 0 && z0 < zsMax) {
if (ms.nFractalT <= vmtNested) {
// Create fractal section using hunt and kill algorithm
xT = xsMax; yT = ysMax; zT = zsMax;
fX = fY = fZ = fFalse;
// Get information about the Mazes surrounding starting nesting level.
for (i = z; i < ms.zFractal; i++) {
bT.CreateMaze3D();
if (i > z)
bT.Set0(0, (Rnd(1, yFractal) << 1) - 1);
else
bT.Set1(ms.xEntrance, ms.yEntrance);
xT /= xFractal; yT /= yFractal; zT /= zFractal;
xs = x0 / xT % xFractal;
ys = y0 / yT % yFractal;
zs = z0 / zT % zFractal;
// The north/west/top faces of the section should be solid if the
// section is adjacent to any surrounding Maze boundary, and that
// surrounding Maze has a wall panel at the appropriate position.
if (y0 % yT == 0)
fX |= bT.Get3((xs << 1) + 1, ys << 1, zs << 1);
if (x0 % xT == 0)
fY |= bT.Get3(xs << 1, (ys << 1) + 1, zs << 1);
if (z0 % zT == 0)
fZ |= bT.Get3(xs << 1, ys << 1, (zs << 1) + 1);
// Set the random number seed for this subsection.
rgl[0] = nRndSeed; rgl[1] = i;
rgl[2] = x0 / xT; rgl[3] = y0 / yT; rgl[4] = z0 / zT;
InitRndRgl(rgl, 5);
}
// Create the fractal Maze section here.
nSav = ms.nEntrancePos; ms.nEntrancePos = epRandom;
FractalGenerate3D(bT, z, xp, yp, zp);
ms.nEntrancePos = nSav;
} else {
// Create fractal section using binary tree algorithm
rgl[0] = nRndSeed; rgl[1] = z; rgl[2] = x0; rgl[3] = y0; rgl[4] = z0;
InitRndRgl(rgl, 5);
nSav = ms.nEntrancePos; ms.nEntrancePos = epCorner;
nSav2 = ms.nRndRun; ms.nRndRun = 0;
bT.CreateMazeBinary3D();
ms.nEntrancePos = nSav; ms.nRndRun = nSav2;