-
Notifications
You must be signed in to change notification settings - Fork 7
/
solve.cpp
1812 lines (1603 loc) · 54.1 KB
/
solve.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: solve.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 solving algorithms.
**
** Created: 9/4/2000.
** Last code change: 10/30/2024.
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "util.h"
#include "graphics.h"
#include "color.h"
#include "threed.h"
#include "maze.h"
/*
******************************************************************************
** Maze Solving Routines
******************************************************************************
*/
// Solve a Maze by filling in all dead ends, including passages that become
// dead ends once other dead ends are filled.
long CMaz::SolveMazeFillDeadEnds(int xa, int ya, int xb, int yb)
{
int x, y, x2, y2, zInc = 2 - ms.fSolveEveryPixel, d;
long count = 0;
flag fAny;
fAny = ms.fSolveDotExit && (FLegalOff(xa, ya) || FLegalOff(xb, yb));
for (y = xl + 1; y < yh; y += zInc)
for (x = xl + 1; x < xh; x += zInc)
if (!Get(x, y) && Count(x, y) >= DIRS1) {
if (fAny && ((x == xa && y == ya) || (x == xb && y == yb)))
continue;
if (fCellMax)
return count;
count++;
x2 = x; y2 = y;
// For each dead end, fill back to a junction in the current Maze.
do {
Set1(x2, y2);
for (d = 0; d < DIRS && Get(x2 + xoff[d], y2 + yoff[d]); d++)
;
if (d >= DIRS)
break;
x2 += xoff[d]; y2 += yoff[d];
if (!ms.fSolveEveryPixel) {
Set1(x2, y2);
x2 += xoff[d]; y2 += yoff[d];
}
} while (FLegalMaze(x2, y2) && Count(x2, y2) == DIRS1);
}
return count;
}
// Partially solve a Maze by filling in the current set of dead end passages.
// This does not fill passages that become dead ends once other dead ends are
// filled. Calling this until no dead ends are left is the same as calling
// SolveMazeFillDeadEnds() once.
long CMaz::DoFillDeadEnds(int xa, int ya, int xb, int yb)
{
CMaz bCopy;
int x, y, x2, y2, zInc = 2 - ms.fSolveEveryPixel, d;
long count = 0;
flag fAny;
if (!bCopy.FBitmapCopy(*this))
return -1;
fAny = ms.fSolveDotExit && (FLegalOff(xa, ya) || FLegalOff(xb, yb));
for (y = yl + zInc - 1; y <= yh; y += zInc)
for (x = xl + zInc - 1; x <= xh; x += zInc)
if (!Get(x, y) && bCopy.Count(x, y) >= DIRS1) {
if (fAny && ((x == xa && y == ya) || (x == xb && y == yb)))
continue;
if (fCellMax)
goto LDone;
count++;
x2 = x; y2 = y;
// For each dead end, fill back to a junction in the original Maze.
do {
Set1(x2, y2);
for (d = 0; d < DIRS && Get(x2 + xoff[d], y2 + yoff[d]); d++)
;
if (d >= DIRS)
break;
x2 += xoff[d]; y2 += yoff[d];
if (zInc >= 2) {
Set1(x2, y2);
x2 += xoff[d]; y2 += yoff[d];
}
} while (FLegalMaze2(x2, y2) && bCopy.Count(x2, y2) >= 2);
}
LDone:
return count;
}
// Partially solve a Maze by filling in cells at the ends of dead ends. Each
// time this is called, dead ends get slightly shorter. Calling this until
// all dead ends are gone is the same as calling SolveMazeFillDeadEnds() once.
long CMaz::DoMarkDeadEnds(int xa, int ya, int xb, int yb)
{
int x, y, f, zInc = 2 - ms.fSolveEveryPixel;
long count = 0;
flag fAny;
// Loop over each cell, but in two opposite checkerboard patterns, so as not
// to check adjacent cells in sequence. This ensures gradual progress, and
// avoids filling in long straight dead ends all at once.
fAny = ms.fSolveDotExit && (FLegalOff(xa, ya) || FLegalOff(xb, yb));
for (f = 0; f <= 1; f++)
for (y = yl + 1; y < yh; y += zInc)
for (x = xl + 1; x < xh; x += zInc)
if ((((x >> 1) + (y >> 1)) & 1) == f &&
!Get(x, y) && Count(x, y) >= DIRS1) {
if (fAny && ((x == xa && y == ya) || (x == xb && y == yb)))
continue;
if (fCellMax)
return count;
count++;
Set1(x, y);
Set1(x-1, y); Set1(x, y-1); Set1(x+1, y); Set1(x, y+1);
}
return count;
}
// Convert all cul-de-sac or noose passages into dead ends.
long CMaz::DoMarkCulDeSacs(int xa, int ya, int xb, int yb)
{
int x, y, x2, y2, d, dnew, zInc = 2 - ms.fSolveEveryPixel, i, j;
long count = 0;
flag fAny;
fAny = ms.fSolveDotExit && (FLegalOff(xa, ya) || FLegalOff(xb, yb));
for (y = yl + 1; y < yh; y += zInc)
for (x = xl + 1; x < xh; x += zInc)
if (!Get(x, y) && Count(x, y) <= 1) {
// Check an individual junction and see if it's part of a cul-de-sac.
for (i = 0; i < DIRS; i++) {
if (!Get(x + xoff[i], y + yoff[i])) {
// Follow an individual path and see if it reconnects.
d = i;
x2 = x + xoff[d]; y2 = y + yoff[d];
if (!ms.fSolveEveryPixel) {
x2 += xoff[d]; y2 += yoff[d];
}
while (FLegalMaze(x2, y2) && Count(x2, y2) == 2 &&
!(fAny && ((x2 == xa && y2 == ya) || (x2 == xb && y2 == yb)))) {
dnew = d ^ 2;
for (j = 0; j < DIRS1; j++) {
DirInc(dnew);
if (!Get(x2 + xoff[dnew], y2 + yoff[dnew])) {
x2 += xoff[dnew]; y2 += yoff[dnew];
if (!ms.fSolveEveryPixel) {
x2 += xoff[dnew]; y2 += yoff[dnew];
}
break;
}
}
d = dnew;
}
// Add a wall just down that path to make a new long dead end.
if (x2 == x && y2 == y) {
if (fCellMax)
return count;
count++;
Set1(x + xoff[i], y + yoff[i]);
break;
}
}
}
}
return count;
}
// Solve a Maze by filling in all cul-de-sacs, including passages that become
// cul-de-sacs once others are filled.
long CMaz::SolveMazeFillCulDeSacs(int xa, int ya, int xb, int yb)
{
long count = 0, i;
// Convert all cul-de-sacs to dead ends, then fill all dead ends. Repeat the
// process until none are left.
do {
count += (i = DoMarkCulDeSacs(xa, ya, xb, yb));
if (i > 0)
PrintSzL("Cul-de-sacs marked: %ld\n", i);
} while (SolveMazeFillDeadEnds(xa, ya, xb, yb) > 0);
return count;
}
// Delete all walls within blind alleys in a Maze, by removing walls that have
// the same blind alley on either side of them.
long CMaz::DoCrackBlindAlleys(CONST CMaz &bSol)
{
CMaz bNew;
CONST CMaz *pb;
BFSS *rgpt;
int zInc = 2 - ms.fSolveEveryPixel, x, y, x2, y2, xnew, ynew, d;
long iset = 0, ipt;
if (F64K())
return -1;
rgpt = RgAllocate(m_x*m_y, BFSS);
if (rgpt == NULL)
return -1;
ClearPb(rgpt, m_y*m_x * sizeof(BFSS));
// Potentially allow all off pixels in a 2nd bitmap to act as the solution.
if (FBitmapSubset(bSol))
pb = &bSol;
else {
if (!bNew.FBitmapCopy(*this))
return -1;
bNew.SolveMazeFillBlindAlleys(-1, -1, -1, -1);
pb = &bNew;
}
BitmapXor(*pb);
BitmapReverse();
for (y = 1; y < m_y; y += zInc)
for (x = 1; x < m_x; x += zInc)
// For each section of passages that hasn't already been filled, flood
// it and all passages that connect with it with a unique id number.
if (!Get(x, y) && rgpt[(long)y * m_x + x].parent <= 0) {
iset++;
ipt = 0;
x2 = x; y2 = y;
LSet:
rgpt[(long)y2 * m_x + x2].parent = iset;
LNext:
for (d = 0; d < DIRS; d++) {
xnew = x2 + xoff[d]; ynew = y2 + yoff[d];
if (FLegal(xnew, ynew) && !_Get(xnew, ynew) &&
rgpt[(long)ynew * m_x + xnew].parent <= 0) {
FillPush(x2, y2);
x2 = xnew; y2 = ynew;
goto LSet;
}
}
if (ipt > 0) {
FillPop(x2, y2);
goto LNext;
}
}
// For each pair of cells, remove the wall segment there if connected.
for (y = 1; y < m_y; y += zInc)
for (x = 1; x < m_x; x += zInc) {
ipt = rgpt[(long)y * m_x + x].parent;
if (ipt > 0) {
if (x < m_x-2 && ipt == rgpt[(long)y * m_x + (x+2)].parent)
Set0(x+1, y);
if (y < m_y-2 && ipt == rgpt[(long)(y+2) * m_x + x].parent)
Set0(x, y+1);
}
}
BitmapXor(*pb);
BitmapReverse();
DeallocateP(rgpt);
return iset;
}
// Seal off all blind alleys in a Maze, by filling in blind alley passages.
// This does not also fill in the section of passages that may be behind the
// stem, so this will create inaccessible sections out of cul-de-sacs and
// other blind alleys that are more than just dead ends.
long CMaz::DoMarkBlindAlleys()
{
BFSS *rgpt;
int zInc = 2 - ms.fSolveEveryPixel, x, y, x2, y2, xnew, ynew, d;
long count = 0, iset = 0, ipt;
if (F64K())
return -1;
rgpt = RgAllocate(m_x*m_y, BFSS);
if (rgpt == NULL)
return -1;
ClearPb(rgpt, m_y*m_x * sizeof(BFSS));
for (y = 0; y < m_y; y += zInc)
for (x = 0; x < m_x; x += zInc)
// For each section of walls that hasn't already been filled, flood it
// and all walls that connect with it with a unique id number.
if (Get(x, y) && rgpt[(long)y * m_x + x].parent <= 0) {
iset++;
ipt = 0;
x2 = x; y2 = y;
LSet:
rgpt[(long)y2 * m_x + x2].parent = iset;
LNext:
for (d = 0; d < DIRS; d++) {
xnew = x2 + xoff[d]; ynew = y2 + yoff[d];
if (GetFast(xnew, ynew) &&
rgpt[(long)ynew * m_x + xnew].parent <= 0) {
FillPush(x2, y2);
x2 = xnew; y2 = ynew;
goto LSet;
}
}
if (ipt > 0) {
FillPop(x2, y2);
goto LNext;
}
}
// For each pair of wall endpoints, add a wall segment there if connected.
for (y = 0; y < m_y; y += zInc)
for (x = 0; x < m_x; x += zInc) {
ipt = rgpt[(long)y * m_x + x].parent;
if (ipt > 0) {
if (x < m_x-2 && ipt == rgpt[(long)y * m_x + (x+2)].parent)
Set1(x+1, y);
if (y < m_y-2 && ipt == rgpt[(long)(y+2) * m_x + x].parent)
Set1(x, y+1);
}
}
// Fill in the centers of cells blocked with walls on all four sides.
for (y = 1; y < m_y-1; y += zInc)
for (x = 1; x < m_x-1; x += zInc)
if (!Get(x, y) && Count(x, y) == DIRS) {
count++;
Set1(x, y);
}
DeallocateP(rgpt);
return count;
}
// Solve a Maze by filling in all blind alleys. This fills in each blind alley
// stem as well as whatever section of passages may be behind it.
long CMaz::SolveMazeFillBlindAlleys(int xa, int ya, int xb, int yb)
{
CMaz b2;
int x, y, xdir, ydir, x2, y2, zInc = 2 - ms.fSolveEveryPixel,
i, d, dold, dd, dsum;
long count = 0;
flag fAny, fAnyA, fAnyB;
fAnyA = ms.fSolveDotExit && FLegalOff(xa, ya);
fAnyB = ms.fSolveDotExit && FLegalOff(xb, yb);
fAny = fAnyA || fAnyB;
for (y = yl + 1; y < yh; y += zInc)
for (x = xl + 1; x < xh; x += zInc)
if (!Get(x, y) && Count(x, y) <= 1 &&
(!ms.fSolveEveryPixel || FOnMaze(x, y))) {
// Check a junction and see if any blind alleys lead from it.
for (i = 0; i < DIRS; i++) {
xdir = x + xoff[i];
ydir = y + yoff[i];
if (!Get(xdir, ydir)) {
// Follow a path and see if wall following reconnects.
d = i; x2 = xdir; y2 = ydir; dsum = 0;
loop {
dold = d; d = FollowWall(&x2, &y2, d, 1); dd = (d - dold);
dsum += ((dd + 1) & DIRS1) - 1;
if (!FLegalMaze(x2, y2) ||
(x2 == x && y2 == y) ||
(fAny && ((x2 == xa && y2 == ya) || (x2 == xb && y2 == yb))))
break;
if (x2 == xdir && y2 == ydir) {
// Wall following has reconnected. Check for blind alley.
if (dsum >= 0) {
if (fAny && !b2.FBitmapCopy(*this))
return -1;
Set1(x, y);
FFill(xdir, ydir, fOn);
Set0(x, y);
// Undo if flooding set either entrance point.
if (fAny &&
((fAnyA && Get(xa, ya)) || (fAnyB && Get(xb, yb)))) {
FBitmapCopy(b2);
break;
}
count++;
}
break;
}
}
}
}
}
return count;
}
#define STREAMS 10000
typedef struct _collision {
ushort x;
ushort y;
char mode;
char unused;
} COLL;
// Remove many passage loops from a Maze by adding wall segments converting
// them to dead ends. Internally "flood" the Maze with "water", such that
// equal distances from the start are reached at the same time, where whenever
// two "columns of water" come down a passage from opposite sides (indicating
// a loop) add a wall segment where they collide.
long CMaz::DoMarkCollisions(int x, int y)
{
CMaz bNew;
COLL *coll;
int x0, y0, xnew, ynew, streams = 1, i, j, d;
long count = 0;
if (F64K())
return -1;
if (FLegalOff(x, y)) {
x0 = x; y0 = y;
} else {
x0 = xl, y0 = yl;
if (!FFindPassage(&x0, &y0, fFalse))
return -1;
}
if (!bNew.FBitmapCopy(*this))
return -1;
coll = RgAllocate(STREAMS, COLL);
if (coll == NULL)
return -1;
coll[0].x = x0; coll[0].y = y0;
coll[0].mode = 'a';
while (streams > 0) {
for (i = 0; i < streams; i++) {
// For each active (a) stream in the list, mark it dead (d).
if (coll[i].mode == 'a') {
x0 = coll[i].x; y0 = coll[i].y;
coll[i].mode = 'd';
if (Get(x0, y0)) {
// If the pixel under this stream has already been set, then some
// other stream did it, where this is a collision. Let this stream
// die and add a wall if the collision happened in a narrow passage.
if (Count(x0, y0) == DIRS) {
count++;
for (d = 0; d < DIRS; d++) {
xnew = x0 + xoff[d]; ynew = y0 + yoff[d];
if (!bNew.Get(xnew, ynew)) {
bNew.Set1(xnew, ynew);
break;
}
}
}
} else {
// This stream is in new territory that hasn't been flooded yet.
// Create new (n) streams for any off pixels adjacent to it.
Set1(x0, y0);
for (d = 0; d < DIRS; d++) {
xnew = x0 + xoff[d]; ynew = y0 + yoff[d];
if (FLegalMaze(xnew, ynew) && !Get(xnew, ynew)) {
for (j = 0; j < streams && coll[j].mode != 'd'; j++)
;
if (j >= streams)
streams++;
if (streams >= STREAMS) {
count = -1;
goto LDone;
}
coll[j].x = xnew; coll[j].y = ynew;
coll[j].mode = 'n';
}
}
}
}
}
// Convert all new (n) streams to active (a) streams for next iteration.
for (i = 0; i < streams; i++)
if (coll[i].mode == 'n')
coll[i].mode = 'a';
// Ignore dead (d) streams at the end of the list.
while (streams > 0 && coll[streams - 1].mode == 'd')
streams--;
}
LDone:
CopyFrom(bNew);
DeallocateP(coll);
return count;
}
// Solve a Maze by filling in all passages that aren't on a shortest solution
// path.
long CMaz::SolveMazeFillCollisions(int xa, int ya, int xb, int yb)
{
long count = 0, i;
// Convert all collision passages to dead ends, then fill all dead ends.
// Repeat the process until none are left.
do {
count += (i = DoMarkCollisions(xa, ya));
if (i > 0) {
UpdateDisplay();
PrintSzL("Collisions marked: %ld\n", i);
}
} while (SolveMazeFillDeadEnds(xa, ya, xb, yb) > 0);
return count;
}
// Solve a Maze with a recursive backtracking algorithm. This is implemented
// with a stack formed by a line of pixels in another bitmap.
long CMaz::SolveMazeRecursive(int x, int y, int x2, int y2, flag fCorner)
{
CMaz b2;
int xnew, ynew, d, dMax = DIRS + fCorner*DIRS, dInc = 1, dir, i, cf;
long count = 0;
flag fAny, fAny2, fDotsOnly;
fAny2 = FLegalOff(x2, y2) && (x2 != 0 || y2 != 0);
fAny = FLegalOff(x, y);
fDotsOnly = fAny2 && ms.fSolveDotExit;
if (!fAny && !FBitmapFind(&x, &y, fOff))
return -2;
if (!b2.FAllocate(m_x, m_y, this))
return -1;
b2.BitmapOff();
loop {
// Set the current location, and figure out which direction to try first.
LPush:
count++;
Set1(x, y); b2.Set1(x, y);
if (!ms.fRandomPath)
dir = 0;
else {
dir = Rnd(0, dMax-1);
dInc = (Rnd(0, 1) << 1) - 1;
}
// Move in each direction that hasn't been tried yet.
for (d = 0; d < dMax; d++) {
xnew = x + xoff[dir]; ynew = y + yoff[dir];
if (!FLegal(xnew, ynew) || (fAny2 && xnew == x2 && ynew == y2)) {
if (!fDotsOnly ?
fAny || fAny2 || ynew >= m_y : xnew == x2 && ynew == y2) {
// Reached a goal point! Return the path taken to get here.
LFinish:
UpdateDisplay();
FBitmapCopy(b2);
BitmapReverse();
goto LDone;
}
} else if (!Get(xnew, ynew)) {
if (y >= m_y-1 && !fDotsOnly)
goto LFinish;
// Don't move adjacent to earlier parts of the path, to avoid solid
// areas in rooms and ensure the path is always easily followable.
cf = 0;
for (i = 0; i < dMax; i++)
cf += b2.Get(xnew + xoff[i], ynew + yoff[i]);
if (cf == 1) {
x = xnew; y = ynew;
goto LPush;
}
}
LPop:
dir = (dir + dInc) & (dMax-1);
}
// Backtrack: Undo the move just taken, then look for the next path.
count--;
if (count <= 0)
break;
b2.Set0(x, y);
for (d = 0; d < dMax; d++) {
xnew = x + xoff[d]; ynew = y + yoff[d];
if (b2.Get(xnew, ynew))
break;
}
x = xnew; y = ynew;
dir = d ^ 2; d = ms.fRandomPath ? 0 : dir;
goto LPop;
}
LDone:
return count;
}
// Solve a Maze by finding a shortest solution, making the bitmap be one
// solution path leading from start to end.
long CMaz::SolveMazeShortest(int x, int y, int x2, int y2, flag fCorner)
{
BFSS *bfss;
int xnew, ynew, d, dMax = DIRS + fCorner*DIRS, dInc = 1, dir;
long count = 0, iLo = 0, iHi = 1, iMax = 1, i;
flag fAny, fAny2, fDotsOnly;
bfss = RgAllocate(m_x*m_y, BFSS);
if (bfss == NULL)
return -1;
fAny2 = FLegalOff(x2, y2) && (x2 != 0 || y2 != 0);
fAny = FLegalOff(x, y);
fDotsOnly = fAny2 && ms.fSolveDotExit;
if (!fAny)
if (!FBitmapFind(&x, &y, fOff)) {
DeallocateP(bfss);
return -2;
}
Set1(x, y);
bfss[0].x = x; bfss[0].y = y; bfss[0].parent = -1;
// Flood the Maze, where each pixel remembers which pixel filled it.
while (iLo < iHi) {
for (i = iLo; i < iHi; i++) {
x = bfss[i].x; y = bfss[i].y;
if (!ms.fRandomPath)
dir = 0;
else {
dir = Rnd(0, dMax-1);
dInc = (Rnd(0, 1) << 1) - 1;
}
for (d = 0; d < dMax; d++) {
xnew = x + xoff[dir]; ynew = y + yoff[dir];
if (!FLegal(xnew, ynew) || (fAny2 && xnew == x2 && ynew == y2)) {
if (!fDotsOnly ?
fAny || fAny2 || ynew >= m_y : xnew == x2 && ynew == y2) {
// Reached a goal point! Draw a path backwards to the start.
UpdateDisplay();
BitmapOn();
do {
Set0(bfss[i].x, bfss[i].y);
i = bfss[i].parent;
count++;
} while (i >= 0);
goto LDone;
}
} else if (!Get(xnew, ynew)) {
Set1(xnew, ynew);
BfssPush(iMax, xnew, ynew, i);
}
dir = (dir + dInc) & (dMax-1);
}
}
iLo = iHi; iHi = iMax;
}
LDone:
DeallocateP(bfss);
return count;
}
// Solve a Maze by finding all shortest solutions, making the bitmap be all
// the solution paths leading from start to end.
long CMaz::SolveMazeShortest2(int x, int y, int x2, int y2, flag fCorner)
{
PT *rgpt = NULL;
int xnew, ynew, d, d2, dMax = DIRS + fCorner*DIRS;
long *rgl = NULL, count = 0, iLo = 0, iHi = 1, iMax = 1, i;
flag fCount = ms.fCountShortest, fAny, fAny2, fDotsOnly;
if (!FEnsureMazeSize(1, femsNoResize | fems64K))
return fFalse;
rgpt = RgAllocate(m_x*m_y, PT);
if (rgpt == NULL) {
count = -1;
goto LDone;
}
if (fCount) {
rgl = RgAllocate(m_x*m_y, long);
if (rgl == NULL) {
count = -1;
goto LDone;
}
ClearPb(rgl, (long)m_x*m_y*sizeof(PT));
}
fAny2 = FLegalOff(x2, y2) && (x2 != 0 || y2 != 0);
fAny = FLegalOff(x, y);
fDotsOnly = fAny2 && ms.fSolveDotExit;
if (!fAny)
if (!FBitmapFind(&x, &y, fOff)) {
count = -2;
goto LDone;
}
Set1(x, y);
rgpt[0].x = x; rgpt[0].y = y;
// Flood the Maze, where each pixel is put in a list in order it's reached.
while (iLo < iHi) {
count++;
for (i = iLo; i < iHi; i++) {
x = rgpt[i].x; y = rgpt[i].y;
for (d = 0; d < dMax; d++) {
xnew = x + xoff[d]; ynew = y + yoff[d];
if (!FLegal(xnew, ynew) || (fAny2 && xnew == x2 && ynew == y2)) {
if (!fDotsOnly ?
fAny || fAny2 || ynew >= m_y : xnew == x2 && ynew == y2) {
// Reached a goal point! Draw paths backwards to the start.
UpdateDisplay();
BitmapOn();
Set0(x, y);
if (fCount) {
count = 1;
rgl[y * m_x + x] = count;
}
while (i >= 0) {
// Each pixel in the list gets set only if adjacent to a pixel
// already on a solution. Since looping backwards, and pixels
// are in the list in forwards order, this avoids going down
// non-shortest paths that were flooded from opposite ends.
x = rgpt[i].x; y = rgpt[i].y;
for (d = 0; d < dMax; d++) {
xnew = x + xoff[d]; ynew = y + yoff[d];
if (FLegal(xnew, ynew) && !Get(xnew, ynew)) {
Set0(x, y);
// Count the number of ways to reach the current pixel. Each
// pixel is the sum of all the ways leading to it. The
// number of shortest solutions is the sum at the end pixel.
if (fCount) {
count = 0;
for (d2 = 0; d2 < dMax; d2++) {
xnew = x + xoff[d2]; ynew = y + yoff[d2];
if (FLegal(xnew, ynew)) {
count += rgl[ynew * m_x + xnew];
if (count < 0) {
count = 0;
fCount = fFalse;
}
}
}
rgl[y * m_x + x] = count;
}
break;
}
}
i--;
}
goto LDone;
}
} else if (!Get(xnew, ynew)) {
Set1(xnew, ynew);
rgpt[iMax].x = xnew; rgpt[iMax].y = ynew;
iMax++;
}
}
}
iLo = iHi; iHi = iMax;
}
LDone:
if (rgpt != NULL)
DeallocateP(rgpt);
if (rgl != NULL)
DeallocateP(rgl);
return count;
}
// Solve a Maze by following a wall, always taking left or right turns. This
// makes the bitmap be the cells visited from start to end, or just the cells
// visited from start back to start if wall following can't solve this Maze.
// Return the number of cells visited if the Maze was successfully solved.
long CMaz::SolveMazeFollowWall(int x, int y, int dir, int x2, int y2,
flag fRight)
{
CMaz b2;
long count = 0;
int x0, y0;
flag fAny, fAny2;
fAny2 = FLegalOff(x2, y2) && (x2 != 0 || y2 != 0);
fAny = FLegalOff(x, y);
if (!fAny) {
if (!FBitmapFind(&x, &y, fOff))
return -2;
dir = 2;
}
if (!b2.FBitmapCopy(*this))
return -1;
BitmapOn();
x0 = x; y0 = y;
// Keep on taking left turns or right turns until end or start is reached.
while (FLegalMaze(x, y) && (!fAny2 || x != x2 || y != y2)) {
count++;
Set0(x, y);
dir = b2.FollowWall(&x, &y, dir, fRight);
if (x == x0 && y == y0) {
count = 0;
break;
}
}
if (FLegalMaze2(x, y))
Set0(x, y);
return count;
}
// Solve a Maze with the Pledge algorithm. This is implemented by following a
// wall, so is similar to SolveMazeFollowWall, however this has the ability to
// jump between islands when pointed in the starting direction.
long CMaz::SolveMazePledge(int x, int y, int dir, int x2, int y2,
flag fRight)
{
CMaz b2;
long count = 0;
int x0, y0, d, dold, dsum = 0, dd, dInc = fRight ? 1 : -1;
flag fStraight = fFalse, fAny, fAny2;
fAny2 = FLegalOff(x2, y2) && (x2 != 0 || y2 != 0);
fAny = FLegalOff(x, y);
if (!b2.FBitmapCopy(*this))
return -1;
BitmapOn();
if (!fAny) {
if (!b2.FBitmapFind(&x, &y, fOff))
return -2;
dir = 2;
if (!b2.Get(x, y+1)) {
Set0(x, y);
b2.Set1(x, y);
y++;
}
}
x0 = x; y0 = y; d = dir;
// Keep on following wall or going straight, until outer edge is reached.
while (FLegalMaze(x, y) && (!fAny2 || x != x2 || y != y2)) {
count++;
Set0(x, y);
if (!fStraight && dsum == 0 && !b2.Get(x + xoff[dir], y + yoff[dir]))
fStraight = fTrue;
dold = d;
if (fStraight) {
Assert(d == dir);
x += xoff[d]; y += yoff[d];
if (b2.Get(x + xoff[dir], y + yoff[dir])) {
count++;
fStraight = fFalse;
d = d + dInc & DIRS1;
}
} else
d = b2.FollowWall(&x, &y, d, fRight);
dd = (d - dold);
dsum += (dd == 3 ? -1 : (dd == -3 ? 1 : (dd == -2*dInc ? 2*dInc : dd)));
if (NAbs(dsum) > ms.nCrackPass) {
count = 0;
break;
}
}
if (FLegalMaze2(x, y))
Set0(x, y);
return count;
}
// Solve a Maze with the Chain algorithm. This finds a reasonably short path
// between two specific points, by following a direct line between them when
// possible, and following a wall to get farther when that line is blocked.
long CMaz::SolveMazeChain(int x, int y, int x2, int y2)
{
CMaz bCopy, bLine;
long count = 0;
int m[2], n[2], e0[2], e[2], len, lenT, xT, yT, d, xnew, ynew, i;
flag fAny, fAny2, f0, f1, f2, f3;
fAny2 = FLegalOff(x2, y2) && (x2 != 0 || y2 != 0);
fAny = FLegalOff(x, y);
if (!fAny && !FBitmapFind(&x, &y, fOff))
return -2;
if (!fAny2) {
x2 = xl; y2 = yh - !Get(xl, yh);
if (!FFindPassage(&x2, &y2, fFalse))
return -2;
}
if (!bCopy.FBitmapCopy(*this))
return -1;
len = NAbs(x2 - x) + NAbs(y2 - y);
// Draw a line between the start and end points.
if (!bLine.FBitmapCopy(*this))
return -1;
bLine.BitmapOff();
bLine.Line(x, y, x2, y2, fOn);
// Expand the line so it can be followed with orthogonal moves.
for (yT = 0; yT < bLine.m_y-1; yT++)
for (xT = 0; xT < bLine.m_x-1; xT++) {
f0 = bLine.Get(xT, yT); f1 = bLine.Get(xT+1, yT);
f2 = bLine.Get(xT, yT+1); f3 = bLine.Get(xT+1, yT+1);
if (f0 != f1 && f0 == f3 && f1 == f2)
bLine.Set1(xT+f2, yT+1);
}
BitmapOn();
loop {
Set0(x, y);
if (x == x2 && y == y2)
break;
count++;
// Find the next position along the direct line that's closer to the end.
for (d = 0; d < DIRS; d++) {
xnew = x + xoff[d]; ynew = y + yoff[d];
lenT = NAbs(x2 - xnew) + NAbs(y2 - ynew);
if (lenT < len && bLine.Get(xnew, ynew)) {
len = lenT;
break;
}
}
Assert(d < DIRS);
// If next position along the line is not blocked by a wall, move to it.
if (!bCopy.Get(xnew, ynew)) {
x = xnew; y = ynew;
continue;
}
// Send out two wall following robots to find the next spot on the line.
for (i = 0; i < 2; i++) {
m[i] = x; n[i] = y;
e[i] = e0[i] = (d + (i << 1) - 1) & DIRS1;
}
loop {
for (i = 0; i < 2; i++) {
if (e0[i] < 0)
continue;
e[i] = bCopy.FollowWall(&m[i], &n[i], e[i], i);
if (m[i] == x && n[i] == y && e[i] == e0[i]) {
e0[i] = -1;
if (e0[1 - i] < 0)
return 0;
continue;
}
if (bLine.Get(m[i], n[i])) {
lenT = NAbs(x2 - m[i]) + NAbs(y2 - n[i]);
if (lenT < len) {
len = lenT;
goto LNext1;
}