-
Notifications
You must be signed in to change notification settings - Fork 7
/
daedalus.cpp
3408 lines (2994 loc) · 101 KB
/
daedalus.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: daedalus.cpp
** By Walter D. Pullen, Astara@msn.com, 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 Daedalus specific routines not related to the
** underlying operating system.
**
** Created: 11/18/1993.
** Last code change: 10/30/2024.
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <time.h>
#include <math.h>
#include "resource.h"
#include "util.h"
#include "graphics.h"
#include "color.h"
#include "threed.h"
#include "maze.h"
#include "draw.h"
#include "daedalus.h"
// Global variables
WS ws = {
// Window settings
fFalse, 0, 0, 0, 0, 0,
// File dialog settings
0, 0, fTrue, fFalse, fFalse,
// Event dialog settings
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// Redraw dialog settings
fTrue, fFalse, fTrue, fTrue, fFalse, fFalse, fFalse, 0, 50, fTrue,
// Menu settings
fFalse, fFalse, fFalse, fFalse,
// Macro accessible only settings
nPrintNormal, -1, fFalse, fFalse, 1000, -1,
// Internal settings
NULL, NULL,
fFalse, fTrue, fFalse, fFalse, fFalse, fFalse, fFalse, fFalse, fFalse,
0, 0, cmdCreatePerfect, cmdCreatePerfect, 0, fFalse, fFalse,
NULL, NULL, NULL, NULL, 0, 0, 0, 0, 12, 0, NULL, 0, 0, 0, 0, fFalse,
NULL, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
BM bm = {xStart, yStart, 0, 0, 0, 0, 0, 1, 1, 1,
NULL, 0L, NULL, 0L,
fFalse, fFalse, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
DR dr = {
// Dot dialog settings - Location
0, 0, 0, 2, 0, 0, 0, 2,
// Dot dialog settings - Motion
fFalse, fFalse, fTrue, 0, fFalse, fTrue, fTrue, fFalse, fFalse, fFalse, 0L,
// Dot dialog settings - Editing
fFalse, fFalse, fTrue, fFalse, fFalse, fFalse, nEdgeVoid,
// Inside dialog Settings - Mode & What To Draw
nInsideSmooth,
fTrue, fFalse, fFalse, fFalse, fFalse, fTrue, 20, 8, fTrue, 10,
// Inside dialog Settings - Walls
fTrue, 160, 20, 100, 0, -1, fTrue, 0.4, 0, 100, 55.0, nTransDefault,
// Inside dialog Settings - Smooth & Free Movement
15, 15, 15, 20, 10, 8, 0, 0, 0, 0, fTrue,
// Color dialog settings
kvWhite, kvBlack, kvGray, kvRed, kvDkBlue, kvDkGreen, kvCyan, kvGray,
kvBlack, kvMaize, kvLtGray, kvBrown, kvWhite, kvBlack, fFalse,
// Menu settings
fFalse, fFalse, fFalse,
// Macro accessible only settings
fFalse, fFalse, fFalse, kvGreen, kvYellow, Rgb(255, 255, 223),
-1, -1, -1, -1, -1, -1, fFalse, 94001225, 0, 0, 50, 333,
fFalse, fFalse, -1, fFalse, fFalse, fFalse, fFalse, fFalse, fFalse,
fFalse, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1,
fFalse, fFalse, fFalse, 11, 3412, 4, 427, 0,
// Internal settings
fFalse, 0, 0.0, NULL, 0, NULL, NULL, NULL, NULL, 50, 40, 99999, 0};
// Constant data
CONST int rgdirDiag[DIRS][DIRS] =
{{0, 1, 1, 0}, {1, 1, 2, 2}, {3, 2, 2, 3}, {0, 0, 3, 3}};
CONST int rgcmdMouse[11] = {cmdMove7, cmdMove8, cmdMove9,
cmdMove4, 0, cmdMove6, cmdMove1, cmdMove2, cmdMove3,
cmdMoveU, cmdMoveD};
CONST PT rgptSize[cmdSizeLast - cmdSize01 + 1] =
{{32, 16}, {32, 32}, {64, 32}, {64, 48}, {64, 64},
{128, 64}, {128, 96}, {128, 128}, {128, 192}, {192, 192},
{256, 192}, {256, 384}, {512, 384}, {512, 768}, {1024, 384},
{1024, 768}, {1024, 1024}, {1024, 2048}, {2048, 4096}};
// Script names
CONST char *rgszScript[cmdScriptLast - cmdScript01 + 1] = {
"demos.ds", "wordmaze.ds", "gigamaze.ds", "maze4d.ds", "maze5d.ds",
"dragon.ds", "pacman.ds", "sokoban.ds", "hunger.ds",
"survmaz0.ds", "survmaz1.ds", "survmaz2.ds", "survmaz3.ds", "survmaz4.ds",
"survmaz5.ds", "survmaz6.ds", "survmaz7.ds", "survmaz8.ds", "survmaz9.ds",
"carletn1.ds", "carletn2.ds", "stocker.ds", "glacier.ds", "safari.ds",
"mousemaz.ds", "squared.ds", "mandy.ds", "pentris.ds", "gripsox.ds",
"jareth.ds"};
CONST char *rgszShortcut[cmdScriptLast - cmdScript01 + 1] = {
"Daedalus demos", "Word Mazes", "World's largest Maze",
"4D Mazes", "5D Mazes", "Dragonslayer",
"Pac-Man game", "Sokoban game", "The Hunger Games",
"Survivor Maze game #10",
"Survivor Maze game #1", "Survivor Maze game #2", "Survivor Maze game #3",
"Survivor Maze game #4", "Survivor Maze game #5", "Survivor Maze game #6",
"Survivor Maze game #7", "Survivor Maze game #8", "Survivor Maze game #9",
"Carleton Farm Maze #1", "Carleton Farm Maze #2", "Stocker Farms Maze",
"Glacier Maze game", "Safari Maze", "Mouse Maze game",
"Survivor Squares game", "Mandelbrot set", "Pentris",
"Grippy Socks", "Jareth's Labyrinth"};
// Command lines to run on startup. When embedding a complex script file, in
// it first delete the leading DS, then replace \ with \\, " with \", and ?
// with \?, to result in valid C++ strings.
CONST char *rgszStartup[] = {
NULL}; // Must end with NULL
/*
******************************************************************************
** Function Hooks
******************************************************************************
*/
#ifdef ASSERT
// Assert a condition. If not, display an error message.
void AssertCore(flag f)
{
if (!f && gs.fErrorCheck) {
PrintSz_E("Assert failed!\n");
// Turn off error checking to avoid future assert messages.
gs.fErrorCheck = fFalse;
}
}
#endif
// Read an 8 bit byte from a file.
byte BRead(FILE *file)
{
CONST char *sz;
char ch;
if (file != NULL)
return getc(file);
// If no file, then read from the startup script string array.
sz = ws.rgszStartup[ws.iszStartup];
if (sz == NULL)
return 0;
ch = sz[ws.ichStartup];
if (ch == 0) {
ws.iszStartup++;
ws.ichStartup = 0;
return '\n';
}
ws.ichStartup++;
return ch;
}
// Initialize a new wireframe list in memory of the specified size.
int InitCoordinates(int cCoor)
{
COOR *coor;
flag fExtend = cCoor < 0;
if (cCoor == 0)
cCoor = ds.cCoorPatch;
else if (fExtend)
cCoor = NAbs(cCoor);
if (cCoor == bm.ccoor)
return bm.ccoor;
coor = (COOR *)ReallocateArray(bm.coor, bm.ccoor, sizeof(COOR), cCoor);
if (coor == NULL)
return -1;
if (bm.coor != NULL)
DeallocateP(bm.coor);
bm.coor = coor;
bm.ccoor = cCoor;
if (!fExtend)
ds.cCoorPatch = 0;
return bm.ccoor;
}
// Append a line to the wireframe list in memory.
flag FSetCoordinates(int x1, int y1, int z1, int x2, int y2, int z2, KV kv)
{
if (ds.cCoorPatch >= bm.ccoor && InitCoordinates(bm.ccoor * -2) < 0)
return fFalse;
Assert(ds.cCoorPatch < bm.ccoor);
WriteCoordinates(bm.coor, x1, y1, z1, x2, y2, z2, kv < 0 ? ds.kvTrim : kv);
return fTrue;
}
// Initialize a new patch list in memory of the specified size.
int InitPatch(int cPat)
{
PATCH *patch;
flag fExtend = cPat < 0;
if (cPat == 0)
cPat = ds.cCoorPatch;
else if (fExtend)
cPat = NAbs(cPat);
if (cPat == bm.cpatch)
return bm.cpatch;
patch = (PATCH *)ReallocateArray(bm.patch, bm.cpatch, sizeof(PATCH), cPat);
if (patch == NULL)
return -1;
if (bm.patch != NULL)
DeallocateP(bm.patch);
bm.patch = patch;
bm.cpatch = cPat;
if (!fExtend)
ds.cCoorPatch = 0;
return bm.cpatch;
}
// Append a quadrilateral polygon to the patch list in memory.
flag FSetPatch(int x1, int y1, int z1, int x2, int y2, int z2, KV kv)
{
PATN pat[cPatch];
if (ds.cCoorPatch >= bm.cpatch && InitPatch(bm.cpatch * -2) < 0)
return fFalse;
pat[0].x = x1; pat[0].y = y1; pat[0].z = z1; pat[0].fLine = fTrue;
pat[1].x = x1; pat[1].y = y1; pat[1].z = z2; pat[1].fLine = fTrue;
pat[2].x = x2; pat[2].y = y2; pat[2].z = z2; pat[2].fLine = fTrue;
pat[3].x = x2; pat[3].y = y2; pat[3].z = z1; pat[3].fLine = fTrue;
Assert(ds.cCoorPatch < bm.cpatch);
WritePatch(bm.patch, pat, fFalse, kv < 0 ? ds.kvObject : kv);
return fTrue;
}
// Initialize either the wireframe or patch list in memory to a given size.
int InitCoorPatch(flag fPatch, int cCoorPatch)
{
if (!fPatch)
return InitCoordinates(cCoorPatch);
else
return InitPatch(cCoorPatch);
}
// Append either a line or patch to the wireframe or patch list in memory.
flag FSetCoorPatch(flag fPatch, int x1, int y1, int z1, int x2, int y2, int z2,
KV kv)
{
if (!fPatch)
return FSetCoordinates(x1, y1, z1, x2, y2, z1/*z2*/, kv);
else
return FSetPatch(x1, y1, z1, x2, y2, z2, kv);
}
/*
******************************************************************************
** Daedalus Routines
******************************************************************************
*/
// Read a monochrome or color Windows bitmap from a file.
flag FReadBitmap(FILE *file, flag fNoHeader)
{
int x, y, z, k;
char sz[cchSzMax];
if (!FReadBitmapHeader(file, fNoHeader, &x, &y, &z, &k))
return fFalse;
if (z != 1 && z != 4 && z != 8 && z != 16 && z != 24 && z != 32) {
sprintf(S(sz), "This Windows bitmap has %d bits per pixel.\n"
"Bitmaps must have 1, 4, 8, 16, 24, or 32 bits.\n", z);
PrintSz_W(sz);
return fFalse;
}
if (z == 1) {
if (bm.b.FReadBitmapCore(file, x, y))
FShowColmap(fFalse);
} else {
if (bm.k.FReadColmapCore(file, x, y, z, k))
FShowColmap(fTrue);
}
return fTrue;
}
// Read a monochrome or color Daedalus bitmap from a file.
flag FReadDaedalusBitmap(FILE *file)
{
int x, y, z;
char ch, ch2;
ch = getbyte(); ch2 = getbyte();
if (ch != 'D' || ch2 != 'B') {
PrintSz_W("This file does not look like a Daedalus bitmap.\n");
return fFalse;
}
if (file != NULL)
fscanf(file, "%d%d%d", &x, &y, &z);
else {
ws.iszStartup++;
sscanf(ws.rgszStartup[ws.iszStartup], "%d%d%d", &x, &y, &z);
}
if (z != 1 && z != 24) {
PrintSz_W(
"This Daedalus bitmap is neither monochrome nor 24 bit color.\n");
return fFalse;
}
if (file != NULL) {
skipcrlf();
} else {
ws.iszStartup++;
ws.ichStartup = 0;
}
if (z == 1) {
if (bm.b.FReadDaedalusBitmapCore(file, x, y))
FShowColmap(fFalse);
} else {
if (bm.k.FReadDaedalusBitmapCore(file, x, y))
FShowColmap(fTrue);
}
return fTrue;
}
// Read a file from an open file handle.
flag FReadFile(int wCmd, FILE *file, flag fCloseAfter)
{
size_t cursorPrev = NULL;
char ch1, ch2;
long l;
// For generic open, peek at the first two characters of the file's contents
// to determine what type it is.
if (wCmd == cmdOpen) {
ch1 = getbyte(); ch2 = getbyte();
if (ch1 == 'B' && ch2 == 'M')
wCmd = cmdOpenBitmap;
else if (ch1 == '#' && ch2 == 'd')
wCmd = cmdOpenXbm;
else if (ch1 == 'D' && ch2 == '3')
wCmd = cmdOpen3D;
else if (ch1 == 'D' && ch2 == 'B')
wCmd = cmdOpenDB;
else if (ch1 == 'D' && ch2 == 'S')
wCmd = cmdOpenScript;
else if (ch1 == 'D' && ch2 == 'W')
wCmd = cmdOpenWire;
else if (ch1 == 'D' && ch2 == 'P')
wCmd = cmdOpenPatch;
else if (ch1 >= ' ' && ch2 >= ' ')
wCmd = cmdOpenText;
else if (ch1 == 10 && ch2 <= 5)
wCmd = cmdOpenColmapPaint;
else
wCmd = cmdOpenColmapTarga;
fseek(file, 0, SEEK_SET);
}
// Go load the contents of the file based on its type.
if (ws.fHourglass)
HourglassCursor(&cursorPrev, fTrue);
switch (wCmd) {
case cmdOpenBitmap: FReadBitmap(file, fFalse); break;
case cmdOpenText: bm.b.FReadText(file); break;
case cmdOpenXbm: bm.b.FReadXbm(file); break;
case cmdOpen3D:
if (!dr.f3D) {
dr.f3D = fTrue;
SystemHook(hos3DOn);
}
bm.b.FReadCube(file, bm.b.m_w3);
break;
case cmdOpenDB: FReadDaedalusBitmap(file); break;
case cmdOpenScript: FReadScript(file); break;
case cmdOpenColmapTarga: bm.k.FReadColmapTarga(file); break;
case cmdOpenColmapPaint: bm.k.FReadColmapPaint(file); break;
case cmdOpenWire:
l = ReadWirelist(&bm.coor, file);
if (l >= 0)
bm.ccoor = l;
break;
case cmdOpenPatch:
l = ReadPatchlist(&bm.patch, file);
if (l >= 0)
bm.cpatch = l;
break;
}
if (ws.fHourglass)
HourglassCursor(&cursorPrev, fFalse);
// Close the file if appropriate.
if (fCloseAfter)
fclose(file);
// For some file types, display information about what was just loaded.
switch (wCmd) {
case cmdOpenWire:
ds.fDidPatch = fFalse;
if (bm.coor != NULL)
PrintSzL("Total number of lines read: %ld\n", bm.ccoor);
break;
case cmdOpenPatch:
ds.fDidPatch = fTrue;
if (bm.patch != NULL)
PrintSzL("Total number of patches read: %ld\n", bm.cpatch);
break;
}
return fTrue;
}
// Save a bitmap or a wireframe or patch list to a file.
flag FWriteFile(CONST CMaz &b, CONST CMazK &c, int wCmd, CONST char *sz,
CONST char *szTitle)
{
FILE *file;
flag fBinary;
size_t cursorPrev = NULL;
// Clipboard copy and wallpaper changing save files during their execution.
if (wCmd == cmdCopyText)
wCmd = cmdSaveText;
else if (wCmd == cmdCopyBitmap || wCmd == cmdSaveWallCenter ||
wCmd == cmdSaveWallTile || wCmd == cmdSaveWallStretch ||
wCmd == cmdSaveWallFit || wCmd == cmdSaveWallFill)
wCmd = cmdSaveBitmap;
else if (wCmd == cmdCopyPicture)
wCmd = cmdSavePicture;
// Open the file for writing given its name.
fBinary = (wCmd == cmdSaveBitmap || wCmd == cmdSaveColmapTarga ||
wCmd == cmdSavePicture);
file = FileOpen(sz, fBinary ? "wb" : "w");
if (file == NULL) {
ws.szTitle = szTitle;
PrintSz_E("The file could not be created.");
return fFalse;
}
// Go save the contents of the file based on the specified type.
if (ws.fHourglass)
HourglassCursor(&cursorPrev, fTrue);
switch (wCmd) {
case cmdSaveBitmap:
if (!bm.fColor)
b.WriteBitmap(file, dr.kvOff, dr.kvOn);
else
c.WriteColmap(file);
break;
case cmdSaveText:
if (!dr.f3D) {
if (!bm.fColor)
b.WriteText(file, ws.fTextClip, ws.fLineChar, ws.fTextTab);
else
c.WriteTextColmap(file, ws.fTextClip);
} else
b.WriteText3D(file, ws.fLineChar, ws.fTextTab);
break;
case cmdSaveDOS:
if (!ws.fLineChar)
b.WriteTextDOS(file, 0, ws.fTextClip);
else
b.WriteText2(file, ws.fTextClip);
break;
case cmdSaveDOS2: b.WriteTextDOS(file, 1, ws.fTextClip); break;
case cmdSaveDOS3: b.WriteTextDOS(file, 2, ws.fTextClip); break;
case cmdSaveXbmN: b.WriteXbm(file, sz, 'N'); break;
case cmdSaveXbmC: b.WriteXbm(file, sz, 'C'); break;
case cmdSaveXbmS: b.WriteXbm(file, sz, 'S'); break;
case cmdSaveColmapTarga: c.WriteColmapTarga(file); break;
case cmdSave3DN: b.WriteCube(file, 1, ws.fTextClip); break;
case cmdSave3DC: b.WriteCube(file, 2, ws.fTextClip); break;
case cmdSave3DS: b.WriteCube(file, 3, ws.fTextClip); break;
case cmdSaveDB:
if (!bm.fColor)
b.WriteDaedalusBitmap(file, ws.fTextClip);
else
c.WriteDaedalusBitmap(file, ws.fTextClip);
break;
case cmdSaveWire:
WriteWireframe(file, bm.coor, bm.ccoor);
break;
case cmdSavePatch:
WritePatches(file, bm.patch, bm.cpatch);
break;
case cmdSavePicture:
WriteWireframeMetafile(file, bm.coor, bm.ccoor);
break;
case cmdSaveVector:
WriteWireframeVector(file, bm.coor, bm.ccoor);
break;
}
if (ws.fHourglass)
HourglassCursor(&cursorPrev, fFalse);
fclose(file);
return fTrue;
}
// Change the active bitmap. Switch to showing either the main bitmap or the
// color bitmap.
flag FShowColmap(flag fColor)
{
flag fDirty;
if (fColor && bm.k.FNull()) {
if (!bm.k.FColmapGetFromBitmap(bm.b, dr.kvOff, dr.kvOn))
return fFalse;
}
fDirty = (fColor != bm.fColor);
bm.fColor = fColor;
DoSetVariableW(vosShowColmap, fColor);
if (fDirty)
SystemHook(hosDirtyView);
return fTrue;
}
// Resize the active bitmap. This implements the action of the Size dialog.
void DoSize(int x, int y, flag fShift, flag fClear)
{
CMap *b = PbFocus();
if (!fShift) {
if (x < 0)
x = NAbs(b->m_x + x);
if (y < 0)
y = NAbs(b->m_y + y);
}
if (fClear) {
if (fShift) {
x += b->m_x; y += b->m_y;
}
if (b->FBitmapSizeSet(x, y))
b->BitmapOff();
} else {
if (!fShift)
b->FBitmapResizeTo(x, y);
else
b->FBitmapShiftBy(x, y);
}
if (!bm.fColor)
bm.b.SetXyh();
}
// Set a macro to the given string.
flag DoDefineMacro(int imacro, CONST char *szMacro, int cchMacro,
CONST char *szMenu)
{
char sz[cchSzMax];
if (!FEnsureMacro(imacro + 1))
return fFalse;
// Get rid of the old macro contents.
if (ws.rgszMacro[imacro] != NULL)
DeallocateP(ws.rgszMacro[imacro]);
// Set the new macro contents.
if (szMacro[0] != chNull) {
ws.rgszMacro[imacro] = RgAllocate(cchMacro + 1, char);
if (ws.rgszMacro[imacro] != NULL)
CopyRgchToSz(szMacro, cchMacro, ws.rgszMacro[imacro], cchMacro + 1);
} else
ws.rgszMacro[imacro] = NULL;
// For macros 1 through 48, if given a menu string, change the menu command
// text for that macro to the string.
if (FBetween(imacro, 1, cMacro) && szMenu[0] != chNull) {
sprintf(S(sz), "%s\t%sF%d", szMenu, imacro <= 12 ? "" : (imacro <= 24 ?
"Shift+" : (imacro <= 36 ? "Ctrl+" : "Alt+")), (imacro - 1) % 12 + 1);
DoOperationW(oosMenu, sz, 0, imacro, ~0);
}
return fTrue;
}
// Ensure there are at least the given number of slots available in the macro
// array, reallocating if needed.
flag FEnsureMacro(int cszNew)
{
char **ppchT;
if (cszNew <= ws.cszMacro)
return fTrue;
if (cszNew <= cMacro)
cszNew = cMacro + 1;
ppchT = (char **)ReallocateArray(ws.rgszMacro, ws.cszMacro,
sizeof(char *), cszNew);
if (ppchT == NULL)
return fFalse;
if (ws.rgszMacro != NULL)
DeallocateP(ws.rgszMacro);
ws.rgszMacro = ppchT;
ws.cszMacro = cszNew;
return fTrue;
}
// Ensure there are at least the given number of slots available in the custom
// variable array, reallocating if needed.
flag FEnsureLVar(int clNew)
{
long *plT;
if (clNew <= ws.clVar)
return fTrue;
if (clNew <= cLetter)
clNew = cLetter + 1;
plT = (long *)ReallocateArray(ws.rglVar, ws.clVar, sizeof(long), clNew);
if (plT == NULL)
return fFalse;
if (ws.rglVar != NULL)
DeallocateP(ws.rglVar);
ws.rglVar = plT;
ws.clVar = clNew;
return fTrue;
}
// Ensure there are at least the given number of slots available in the custom
// string array, reallocating if needed.
flag FEnsureSzVar(int cszNew)
{
char **ppchT;
if (cszNew <= ws.cszVar)
return fTrue;
ppchT = (char **)ReallocateArray(ws.rgszVar, ws.cszVar, sizeof(char *),
cszNew);
if (ppchT == NULL)
return fFalse;
if (ws.rgszVar != NULL)
DeallocateP(ws.rgszVar);
ws.rgszVar = ppchT;
ws.cszVar = cszNew;
return fTrue;
}
// Set the given slot in the custom string array to the specified string.
flag SetSzVar(CONST char *sz, int cch, int isz)
{
char *pch;
if (!FEnsureSzVar(isz + 1))
return fFalse;
if (cch < 0)
cch = CchSz(sz);
if (cch > 0) {
pch = RgAllocate(cch + 1, char);
if (pch == NULL)
return fFalse;
CopyRgchToSz(sz, cch, pch, cch + 1);
} else
pch = NULL;
if (ws.rgszVar[isz] != NULL)
DeallocateP(ws.rgszVar[isz]);
ws.rgszVar[isz] = pch;
return fTrue;
}
// Return a pointer to the specified custom monochrome bitmap, as used for
// texture masks.
CMaz *BitmapGetMask(int ib)
{
CMaz *pbT;
int iT;
// Special values allow referencing the standard bitmaps.
if (ib < -3)
return NULL;
switch (ib) {
case -1: return &bm.b; // Bitmap -1 is the main bitmap
case -2: return &bm.b2; // Bitmap -2 is the temporary bitmap
case -3: return &bm.b3; // Bitmap -3 is the extra bitmap
}
// Ensure there are at least the given number of slots available in the
// monochrome bitmap array, reallocating if needed.
if (ib >= ws.cbMask) {
pbT = (CMaz *)ReallocateArray(ws.rgbMask, ws.cbMask,
sizeof(CMaz), ib+1);
if (pbT == NULL)
return NULL;
for (iT = ws.cbMask; iT <= ib; iT++)
new(&pbT[iT]) CMaz(); // Effectively does: pbT[iT].CMaz::CMaz();
if (ws.rgbMask != NULL)
DeallocateP(ws.rgbMask);
ws.rgbMask = pbT;
ws.cbMask = ib+1;
}
return &ws.rgbMask[ib];
}
// Return a pointer to the specified custom color bitmap, as used for
// textures.
CMazK *ColmapGetTexture(int ic)
{
CMazK *pcT;
int iT;
// Special values allow referencing the standard bitmaps.
if (ic < -5)
return NULL;
switch (ic) {
case -1: return &bm.k; // Bitmap -1 is the main color bitmap
case -2: return &bm.k2; // Bitmap -2 is the temporary color bitmap
case -3: return &bm.k3; // Bitmap -3 is the extra color bitmap
case -4: return &bm.kI; // Bitmap -4 is the inside color bitmap
case -5: return &bm.kR; // Bitmap -5 is the inside rainbow bitmap
}
// Ensure there are at least the given number of slots available in the
// color bitmap array, reallocating if needed.
if (ic >= ws.ccTexture) {
pcT = (CMazK *)ReallocateArray(ws.rgcTexture, ws.ccTexture,
sizeof(CMazK), ic+1);
if (pcT == NULL)
return NULL;
for (iT = ws.ccTexture; iT <= ic; iT++)
new(&pcT[iT]) CMazK(); // Effectively does: pcT[iT].CMazK::CMazK();
if (ws.rgcTexture != NULL)
DeallocateP(ws.rgcTexture);
ws.rgcTexture = pcT;
ws.ccTexture = ic+1;
}
return &ws.rgcTexture[ic];
}
// Free all custom texture bitmaps and associated data. This is called from
// the Delete textures command, the ResetProgram operation, and when the
// program terminates.
void DeallocateTextures()
{
int i;
// Free each monochrome bitmap and the list itself.
if (ws.rgbMask != NULL) {
for (i = 0; i < ws.cbMask; i++)
ws.rgbMask[i].Free();
DeallocateP(ws.rgbMask);
ws.rgbMask = NULL;
ws.cbMask = 0;
}
// Free each color bitmap and the list itself.
if (ws.rgcTexture != NULL) {
for (i = 0; i < ws.ccTexture; i++)
ws.rgcTexture[i].Free();
DeallocateP(ws.rgcTexture);
ws.rgcTexture = NULL;
ws.ccTexture = 0;
}
}
// Apply a texture map to the sky and ground background areas of the
// perspective inside view. Implements the Background apply texture command.
flag FTextureBackground()
{
CCol cT, *pc, *c1 = &bm.k;
int iBase;
// Ensure at least one of the color and temporary color bitmaps exist.
iBase = Max(ws.ccTexture, ws.cbMask); iBase = Max(iBase, 1);
if (c1->FNull() && bm.k2.FNull()) {
c1 = &cT;
if (!c1->FColmapGetFromBitmap(bm.b, dr.kvOff, dr.kvOn))
return fFalse;
}
if (ColmapGetTexture(iBase + 1) == NULL)
return fFalse;
// Set the sky/ground of inside view to a copy of the color bitmap, if
// present. Put in next new texture slot to avoid conflicting with others.
if (c1 != NULL) {
pc = ColmapGetTexture(iBase);
if (!pc->FBitmapCopy(*c1))
return fFalse;
dr.nTexture = iBase;
}
// Set the sky/ground of inside view for lower levels of 3D Mazes to a copy
// of the temporary color bitmap, if present.
if (!bm.k2.FNull()) {
pc = ColmapGetTexture(iBase + 1);
if (!pc->FBitmapCopy(bm.k2))
return fFalse;
dr.nTexture2 = iBase + 1;
}
return fTrue;
}
// Apply texture maps to all walls of perspective inside view. Implements the
// Color Walls, Overlay Walls, and Color Overlay Walls apply texture commands.
flag FTextureWall(int nMode, bit o)
{
CCol c1, c2, *pc1 = &bm.k, *pc2 = &bm.k2, *pc;
CMon b1, b2, *pb1 = &bm.b2, *pb2 = &bm.b3, *pb;
int iBase, n, x, y, n1, n2, n3;
flag fShade = dr.rLightFactor != 0.0 && nMode != 2;
// Figure out how many different textures will be used. Narrow Walls on
// doubles this number, also a non-zero Light Factor triples this number.
iBase = Max(ws.ccTexture, ws.cbMask);
n = (1 + dr.fNarrow) * (1 + fShade*2);
if (ColmapGetTexture(iBase + n) == NULL ||
BitmapGetMask(iBase + n) == NULL)
return fFalse;
// Set all pixels in the texture lookup bitmap to reference the textures
// that are about to be defined.
pc = ColmapGetTexture(iBase);
if (!pc->FBitmapSizeSet(bm.b.m_x, bm.b.m_y))
return fFalse;
if (!dr.fNarrow) {
// Narrow Walls off means all pixels have the same texture per side.
pc->BitmapSet(fShade ? NWSE(iBase + 2, iBase + 1, iBase + 3, iBase + 1) :
NWSE(iBase + 1, iBase + 1, iBase + 1, iBase + 1));
} else {
// Narrow Walls on means narrow and normal walls have different textures.
n1 = n2 = n3 = iBase + 2;
if (fShade) {
n2 = iBase + 4; n3 = iBase + 6;
}
for (y = 0; y < pc->m_y; y++)
for (x = 0; x < pc->m_x; x++)
pc->Set(x, y,
NWSE(n2 - FOdd(x), n1 - FOdd(y), n3 - FOdd(x), n1 - FOdd(y)));
}
// Define monochrome overlay textures. Applies to Overlay Walls and Color
// Overlay Walls, but not Color Walls.
if (nMode >= 2) {
// If the overlays are to be white instead of black, set all pixels in the
// overlay lookup bitmap to indicate all overlays are to be white.
if (o) {
pb = BitmapGetMask(0);
if (!pb->FBitmapSizeSet(bm.b.m_x, bm.b.m_y))
return fFalse;
pb->BitmapOn();
}
// Make copies of bitmaps if they're going to be modified.
if (nMode == 3) {
pb1 = &b1; pb2 = &b2;
if (!bm.b2.FNull() && !pb1->FBitmapCopy(bm.b2))
return fFalse;
if (!bm.b3.FNull() && !pb2->FBitmapCopy(bm.b3))
return fFalse;
}
if (pb1->FNull() && pb2->FNull() && !(pb1 = &b1)->FBitmapCopy(bm.b))
return fFalse;
if (nMode == 3) {
pb1->BitmapReverse();
pb2->BitmapReverse();
}
// If the temporary or extra bitmap doesn't exist, copy the other to it,
// truncated or tessellated in proportion to the narrow walls setting.
if (pb1->FNull()) {
if (!(pb1 = &b1)->FAllocate(
NMultDiv(pb2->m_x, dr.zCell, dr.zCellNarrow), pb2->m_y, pb2))
return fFalse;
pb1->BitmapTessellate(*pb2);
} else if (dr.fNarrow && pb2->FNull()) {
if (!(pb2 = &b2)->FAllocate(
NMultDiv(pb1->m_x, dr.zCellNarrow, dr.zCell), pb1->m_y, pb1))
return fFalse;
pb2->BitmapTessellate(*pb1);
}
// Copy bitmaps to overlay textures.
if (!BitmapGetMask(iBase + 1)->FBitmapCopy(*pb1))
return fFalse;
if (dr.fNarrow && !BitmapGetMask(iBase + 2)->FBitmapCopy(*pb2))
return fFalse;
if (fShade) {
if (!BitmapGetMask(iBase + (dr.fNarrow ? 3 : 2))->FBitmapCopy(*pb1))
return fFalse;
if (!BitmapGetMask(iBase + (dr.fNarrow ? 5 : 3))->FBitmapCopy(*pb1))
return fFalse;
if (dr.fNarrow) {
if (!BitmapGetMask(iBase + 4)->FBitmapCopy(*pb2))
return fFalse;
if (!BitmapGetMask(iBase + 6)->FBitmapCopy(*pb2))
return fFalse;
}
}
}
// Define color textures. Applies to Color Walls and Color Overlay Walls,
// but not Overlay Walls.
if (nMode != 2) {
// Make copies of color bitmaps if they're going to be modified.
if (nMode == 3) {
pc1 = &c1; pc2 = &c2;
if (!bm.k.FNull() && !pc1->FBitmapCopy(bm.k))
return fFalse;
if (!bm.k2.FNull() && !pc2->FBitmapCopy(bm.k2))
return fFalse;
}
if (pc1->FNull() && pc2->FNull() &&
!pc1->FColmapGetFromBitmap(bm.b, dr.kvOff, dr.kvOn))
return fFalse;
// If color or temporary color bitmap doesn't exist, copy the other to it,
// truncated or tessellated in proportion to the narrow walls setting.
if (pc1->FNull()) {
if (!(pc1 = &c1)->FAllocate(
NMultDiv(pc2->m_x, dr.zCell, dr.zCellNarrow), pc2->m_y, pc2))
return fFalse;
pc1->BitmapTessellate(*pc2);
} else if (dr.fNarrow && pc2->FNull()) {
if (!(pc2 = &c2)->FAllocate(
NMultDiv(pc1->m_x, dr.zCellNarrow, dr.zCell), pc1->m_y, pc1))
return fFalse;
pc2->BitmapTessellate(*pc1);
}
if (nMode == 3) {
pc1->ColmapOrAndFromBitmap(*pb1, kvWhite, kvBlack, o);
if (dr.fNarrow)
pc2->ColmapOrAndFromBitmap(*pb2, kvWhite, kvBlack, o);
}
// Copy color bitmaps to textures, shaded appropriately.