-
Notifications
You must be signed in to change notification settings - Fork 16
/
randmaps.pp
3314 lines (2945 loc) · 110 KB
/
randmaps.pp
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
unit RandMaps;
{ ******************************* }
{ *** NEW SPECIFICATIONS *** }
{ ******************************* }
{ Every feature, both the SCENE gear and the MAP FEATURES, }
{ needs three SAtts defined: PARAM describes the rendering }
{ parameters to be sent to the actual drawing routine, while }
{ SELECTOR holds the parameters to be sent to the sub-area }
{ selection routine. GAPFILL describes how to plug empty spaces. }
{ If these strings are not defined in the scene/feature gear, }
{ a default value is obtained from the GameData/randmaps.txt file }
{ based upon the scene/feature's listed style. }
{ All map features are to be recursive. Bottom level features }
{ fit into the SCENE gear. }
{
GearHead2, a roguelike mecha CRPG
Copyright (C) 2005 Joseph Hewitt
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.
The full text of the LGPL can be found in license.txt.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
{$LONGSTRINGS ON}
interface
uses gears,locale,playwright;
const
DEFAULT_FLOOR_TYPE = 20;
DEFAULT_WALL_TYPE = 23;
SPECIAL_ShowAll = 'SHOWALL';
SPECIAL_ConvertDoors = 'CELL';
SPECIAL_Unchartable = 'UNCHARTABLE';
DW_Horizontal = 1;
DW_Vertical = 2;
{ NAtts for components. }
NAG_ComponentDesc = -8;
NAS_CompUID = 1;
NAS_ELementID = 2; { Used by the minimap to identify components. }
{ CELL FORMAT: X Y W H D }
{ Direction 0 means that the minimap will have the same orientation as }
{ when drawn out in a design file; dirs 1 through 3 rotate 90 degrees clockwise. }
{ Predrawn maps come with gears that describe their cells. }
STAT_PDMC_X = 1;
STAT_PDMC_Y = 2;
STAT_PDMC_W = 3;
STAT_PDMC_H = 4;
STAT_PDMC_D = 5;
var
High_Component_ID: Integer;
Function LoadPredrawnMap( const FName: String ): GameBoardPtr;
Procedure SavePredrawnMap( GB: GameBoardPtr; const FName: String );
Function NewSubZone( MF: GearPtr ): GearPtr;
Procedure DrawMiniMap( GB: GameBoardPtr; MF: GearPtr; MapDesc: String; D: Integer );
{ WARNING: AddContent is for internal use only!!! }
Function AddContent( CType: String; GB: GameboardPtr; Source,Zone: GearPtr; P: String; var Cells: SAttPtr; SCheck,STerr: Integer ): Boolean;
function RandomMap( Scene: GearPtr ): GameBoardPtr;
implementation
uses gearutil,ghprop,rpgdice,texutil,gearparser,narration,ui4gh,arenascript,ghchars,sysutils,ability,
{$IFDEF ASCII}
vidgfx;
{$ELSE}
sdlgfx;
{$ENDIF}
var
Standard_Param_List: SAttPtr;
random_scene_content,super_prop_list: GearPtr;
UniqueZoneNum: Integer;
Function LoadPredrawnMap( const FName: String ): GameBoardPtr;
{ Load the requested map feature from disk. If no such map can be found, }
{ return a blank map instead. }
var
F: Text;
W,H: Integer;
it: GameBoardPtr;
begin
if FileExists( Series_Directory + FName ) then begin
Assign( F , Series_Directory + FName );
Reset( F );
ReadLn( F , W );
ReadLn( F , H );
it := NewMap( W , H );
it^.Map := ReadMap( F , W , H );
it^.Meks := ReadCGears( F );
Close( F );
end else begin
it := NewMap( 50 , 50 );
end;
LoadPredrawnMap := it;
end;
Procedure SavePredrawnMap( GB: GameBoardPtr; const FName: String );
{ Save this map to disk using the provided filename. }
var
F: Text;
begin
Assign( F , Series_Directory + FName );
Rewrite( F );
{ Width and height get written first. }
writeln( F , GB^.Map_Width );
writeln( F , GB^.Map_Height );
{ Write the map data. This part should be easy. }
WriteMap( GB^.Map , F );
{ Write the cell data. This part may take a bit more work. }
WriteCGears( F , GB^.Meks );
Close( F );
end;
Function IsLegalTerrain( T: Integer ): Boolean;
{ Return TRUE if T is a legal terrain type, or FALSE otherwise. }
begin
IsLegalTerrain := ( T >= 1 ) and ( T <= NumTerr );
end;
Function GetSpecial( MF: GearPtr ): String;
{ Retrieve the special string for this map feature, }
{ convert it to uppercase and return it. }
begin
GetSpecial := UpCase( SAttValue( MF^.SA , 'SPECIAL' ) );
end;
Function RectPointOverlap( X1,Y1,X2,Y2,PX,PY: Integer ): Boolean;
{ Return TRUE if point PX,PY is located inside the provided }
{ rectangle, FALSE if it isn't. }
begin
RectPointOverlap := ( PX >= X1 ) and ( PX <= X2 ) and ( PY >= Y1 ) and ( PY <= Y2 );
end;
Function RectRectOverlap( X1,Y1,W1,H1,X2,Y2,W2,H2: Integer ): Boolean;
{ Return TRUE if the two rectangles described by X,Y,Width,Height }
{ overlap, FALSE if they don't. }
var
OL: Boolean;
XB,YB: Integer;
begin
OL := False;
{ Check all points of the first rectangle against the second. }
for XB := X1 to (X1 + W1 - 1 ) do begin
for YB := Y1 to (Y1 + H1 - 1 ) do begin
Ol := OL or RectPointOverlap( X2 , Y2 , X2 + W2 - 1 , Y2 + H2 - 1 , XB , YB );
end;
end;
RectRectOverlap := OL;
end;
Function RegionClear( GB: GameBoardPtr; SCheck,STerr,X,Y,W,H: Integer ): Boolean;
{ Return TRUE if the specified region counts as clear for the purpose }
{ of sticking a map feature there, FALSE if it doesn't. }
Function InclusiveRegionClear: Boolean;
{ Return TRUE if this region contains at least one }
{ tile of STERR, false otherwise. }
var
IsClear: Boolean;
TX,TY: Integer;
begin
IsClear := False;
for TX := ( X - 1 ) to ( X + W ) do begin
for TY := ( Y - 1 ) to ( Y + H ) do begin
if OnTheMap( GB , TX , TY ) then begin
if TileTerrain( GB , TX , TY ) = STerr then begin
IsClear := True;
end;
end;
end;
end;
InclusiveRegionClear := IsClear;
end;
Function ExclusiveRegionClear: Boolean;
{ Return TRUE if this region is free from tiles }
{ of type STERR, false otherwise. }
var
IsClear: Boolean;
TX,TY: Integer;
begin
IsClear := True;
for TX := ( X - 1 ) to ( X + W ) do begin
for TY := ( Y - 1 ) to ( Y + H ) do begin
if OnTheMap( GB , TX , TY ) then begin
if TileTerrain( GB , TX,TY ) = STerr then begin
IsClear := False;
end;
end;
end;
end;
ExclusiveRegionClear := IsClear;
end;
begin
{ Call the appropriate checking routine based upon what kind of }
{ map generator we're dealing with. }
if SCheck > 0 then begin
RegionClear := InclusiveRegionClear;
end else if SCheck < 0 then begin
RegionClear := ExclusiveRegionClear;
end else begin
RegionClear := True;
end;
end;
Function RandomPointWithinBounds( Container: GearPtr; W,H: Integer ): Point;
{ Select a placement point within the bounds of this container. }
var
P: Point;
begin
if ( Container = Nil ) or IsAScene( Container ) then begin
P.X := Random( Container^.STAT[ STAT_MAPWIDTH ] - 3 - W ) + 3;
P.Y := Random( Container^.STAT[ STAT_MAPHEIGHT ] - 3 - H ) + 3;
end else begin
P.X := Container^.Stat[ STAT_XPos ] + 1;
if W < ( Container^.Stat[ STAT_MFWidth ] - 3 ) then P.X := P.X + Random( Container^.Stat[ STAT_MFWidth ] - W - 4 );
P.Y := Container^.Stat[ STAT_YPos ] + 1;
if H < ( Container^.Stat[ STAT_MFHeight ] - 3 ) then P.Y := P.Y + Random( Container^.Stat[ STAT_MFHeight ] - H - 4 );
end;
RandomPointWithinBounds := P;
end;
Function PlacementPointIsGood( GB: GameBoardPtr; Container: GearPtr; SCheck,STerr,X0,Y0,W,H: Integer ): Boolean;
{ Return TRUE if the specified area is free for adding a new }
{ map feature, or FALSE otherwise. }
var
BadPosition: Boolean;
MF2: GearPtr;
begin
{ Assume it isn't a bad position until shown otherwise. }
BadPosition := False;
{ Check One - see if this position intersects with any }
{ other map feature at this same depth. }
{ Only those map features which have }
{ already been placed need be checked. }
if Container <> Nil then begin
MF2 := Container^.SubCom;
while MF2 <> Nil do begin
if ( MF2^.G = GG_MapFeature ) and OnTheMap( GB , MF2^.Stat[ STAT_XPos ] , MF2^.Stat[ STAT_YPos ] ) then begin
BadPosition := BadPosition or RectRectOverlap( X0 - 1 , Y0 - 1 , W + 2 , H + 2 , MF2^.Stat[ STAT_XPos ] , MF2^.Stat[ STAT_YPos ] , MF2^.Stat[ STAT_MFWidth ] , MF2^.Stat[ STAT_MFHeight ] );
end;
MF2 := MF2^.Next;
end;
end;
{ Check Two - see if this position is in a "clear" area }
{ of the map. }
if not BadPosition then BadPosition := not RegionClear( GB , SCheck , STerr , X0 , Y0 , W , H );
{ So, the placement point is good if X,Y isn't a bad position. }
PlacementPointIsGood := not BadPosition;
end;
Function SelectPlacementPoint( GB: GameBoardPtr; Container,MF: GearPtr; var Cells: SAttPtr; SCheck,STerr: Integer ): Boolean;
{ Attempt to find a decent place to put map feature MF. }
{ - It should not intersect with any other map feature }
{ currently placed. }
{ - It should be at least one tile from the edge of the container }
{ on all sides. }
{ - It should be placed in an area that is considered "clear", }
{ depending upon the SCheck, STerr values. }
const
MaxTries = 10000;
var
Tries: Integer;
P: Point;
TheCell: SAttPtr;
begin
{ If we have been provided with a list of cells, then in theory }
{ or work here has already been done for us. Pick one of the cells }
{ at random and return that. On the other hand, if we have no }
{ cells, we'll need to search for a free spot ourselves. }
if not OnTheMap( GB , MF^.Stat[ STAT_XPos ] , MF^.Stat[ STAT_YPos ] ) then begin
if Cells <> Nil then begin
{ Select a cell at random. }
TheCell := SelectRandomSAtt( Cells );
{ Extract the needed info from this cell. }
MF^.Stat[ STAT_XPos ] := ExtractValue( TheCell^.Info );
MF^.Stat[ STAT_YPos ] := ExtractValue( TheCell^.Info );
MF^.Stat[ STAT_MFWidth ] := ExtractValue( TheCell^.Info );
MF^.Stat[ STAT_MFHeight ] := ExtractValue( TheCell^.Info );
SetNAtt( MF^.NA , NAG_Location , NAS_D , ExtractValue( TheCell^.Info ) );
{ Delete this cell, to prevent it from being chosen again. }
RemoveSAtt( Cells , TheCell );
SelectPlacementPoint := True;
end else begin
Tries := 0;
repeat
P := RandomPointWithinBounds( Container , MF^.Stat[ STAT_MFWidth ] , MF^.Stat[ STAT_MFHeight ] );
Inc( Tries );
{ If we've been trying and trying with no success, }
{ get rid of the terrain check condition and just go }
{ on nonintersection. }
if Tries > 9000 then SCheck := 0;
until ( Tries > MaxTries ) or PlacementPointIsGood( GB , Container , SCheck , STerr , P.X , P.Y , MF^.Stat[ STAT_MFWidth ] , MF^.Stat[ STAT_MFHeight ] );
MF^.Stat[ STAT_XPos ] := P.X;
MF^.Stat[ STAT_YPos ] := P.Y;
SelectPlacementPoint := Tries <= MaxTries;
end;
end;
end;
Function DecideTerrainType( MF: GearPtr; var Cmd: String; D: Integer ): Integer;
{ Given the default provided by the instruction string and the }
{ value stored in the map feature gear, decide what terrain type }
{ to use for the current operation. }
var
it: Integer;
begin
it := ExtractValue( CMD );
if ( MF <> Nil ) and ( D >= 5 ) and ( D <= NumGearStats ) and IsLegalTerrain( MF^.Stat[ D ] ) then begin
it := MF^.Stat[ D ];
end;
DecideTerrainType := it;
end;
Procedure DrawTerrain( GB: GameBoardPtr; X,Y,T1,T2: Integer );
{ Draw a terrain type into the designated tile. If two terrain }
{ types have been provided, pick one of them randomly. }
begin
if OnTheMap( GB , X , Y ) then begin
if ( Random( 3 ) = 1 ) and ( T2 <> 0 ) then SetTerrain( GB , X,Y , T2 )
else SetTerrain( GB , X,Y , T1 );
end;
end;
Procedure RectFill( GB: GameBoardPtr; T1,T2,X0,Y0,W,H: Integer );
{ Fill a rectangular area with the specified terrain. }
{ This is needed by several of the commands, so here it is }
{ as a separate procedure. }
var
X,Y: Integer;
begin
for X := X0 to ( X0 + W - 1 ) do begin
for Y := Y0 to ( Y0 + H - 1 ) do begin
if OnTheMap( GB , X , Y ) then begin
DrawTerrain( GB , X , Y , T1 , T2 );
end;
end;
end;
end;
Procedure ProcessFill( GB: GameBoardPtr; MF: GearPtr; var Cmd: String; X0,Y0,W,H: Integer );
{ Just fill this region with a terrain type. }
var
T1,T2: Integer;
begin
T1 := DecideTerrainType( MF , Cmd , STAT_MFFloor );
T2 := DecideTerrainType( MF , Cmd , STAT_MFMarble );
{ Fill in the building area with the floor terrain. }
RectFill( GB , T1 , T2 , X0 , Y0 , W , H );
end;
Procedure InstallDoor( GB: GameBoardPtr; MF: GearPtr; X,Y,LockVal,HideVal: Integer );
{ Add a standard door to the map at the specified location with the specified options. }
function LocalWall: Integer;
{ Take a look at the four neighboring squares to locate }
{ a wall. }
var
D,T: Integer;
begin
D := 0;
while D <= 8 do begin
T := TileTerrain( GB , X + AngDir[ D , 1 ] , Y + AngDir[ D , 2 ] );
D := D + 2;
if TerrMan[ T ].Pass < -99 then D := 10;
end;
LocalWall := T;
end;
var
NewDoor,DoorPrototype: GearPtr;
begin
if MF <> Nil then DoorPrototype := SeekCurrentLevelGear( MF^.SubCom , GG_MetaTerrain , GS_MetaDoor )
else DoorPrototype := Nil;
if DoorPrototype <> Nil then begin
NewDoor := CloneGear( DoorPrototype );
end else begin
NewDoor := NewGear( Nil );
NewDoor^.G := GG_MetaTerrain;
NewDoor^.S := GS_MetaDoor;
NewDoor^.V := 5;
InitGear( NewDoor );
end;
SetNAtt( NewDoor^.NA , NAG_Location , NAS_X , X );
SetNAtt( NewDoor^.NA , NAG_Location , NAS_Y , Y );
{ if MF <> Nil then begin
Name := SAttValue( MF^.SA , 'NAME' );
if Name <> '' then begin
SetSAtt( NewDoor^.SA , 'NAME <' + MsgString( 'RANDMAPS_DoorSign' ) + Name + '>' );
end;
end;}
NewDoor^.Stat[ STAT_Lock ] := LockVal;
if HideVal > 0 then begin
DrawTerrain( GB , X , Y , LocalWall , 0 );
NewDoor^.Stat[ STAT_MetaVisibility ] := HideVal;
end;
InsertInvCom( GB^.Scene , NewDoor );
end;
Procedure AddDoor( GB: GameBoardPtr; MF: GearPtr; X,Y: Integer );
{ Add a standard door to the map at the specified location. }
var
Roll,Chance,LockVal,HideVal: Integer;
begin
LockVal := 0;
HideVal := 0;
if MF <> Nil then begin
{ Possibly make the door either LOCKED or SECRET, }
{ depending on the random chances stored in the MF. }
Chance := NAttValue( MF^.NA , NAG_Narrative , NAS_LockedDoorChance );
if Chance > 0 then begin
Roll := Random( 100 );
if Roll < Chance then begin
LockVal := ( Roll div 10 ) + 3;
end;
end;
Chance := NAttValue( MF^.NA , NAG_Narrative , NAS_SecretDoorChance );
if Chance > 0 then begin
Roll := Random( 100 );
if Roll < Chance then begin
HideVal := ( Roll div 8 ) + 2;
end;
end;
end;
InstallDoor( GB , MF , X , Y , LockVal , HideVal );
end;
Procedure AddHiddenEntrance( GB: GameBoardPtr; X,Y,D: Integer );
{ Add a hidden entrance back to the parent map. This is used generally }
{ to control where the PC will enter a scene. }
var
Entry,OS: GearPtr; { Entrance, and Originating SCene }
begin
Entry := LoadNewSTC( 'HIDDEN_ENTRANCE' );
{ The destination in the hidden entrance should have the same value as the }
{ scene being entered from. However, if this is a metascene or dynamic scene, }
{ it's gonna take some work to find out what that is. }
if GB^.Scene^.G = GG_MetaScene then begin
OS := FindSceneEntrance( FindRoot( GB^.Scene ) , GB , RealSceneID( GB^.Scene ) );
if OS <> Nil then Entry^.Stat[ STAT_Destination ] := FindSceneID( OS , GB );
end else if IsInvCom( GB^.Scene ) then begin
Entry^.Stat[ STAT_Destination ] := GB^.Scene^.S;
end else begin
Entry^.Stat[ STAT_Destination ] := GB^.Scene^.Parent^.S;
end;
SetNAtt( Entry^.NA , NAG_Location , NAS_X , X );
SetNAtt( Entry^.NA , NAG_Location , NAS_Y , Y );
{ Store the entry direction for this entrance. }
SetNAtt( GB^.Scene^.NA , NAG_EntryDirections , Entry^.Stat[ STAT_Destination ] , D + 1 );
InsertInvCom( GB^.Scene , Entry );
end;
Procedure ConvertDoors( GB: GameBoardPtr; DoorPrototype: GearPtr; X0,Y0,W,H: Integer );
{ Convert any doors within the specified range to the door }
{ prototype requested. }
var
map: Array[ 1..MaxMapWidth , 1..MaxMapWidth ] of Boolean;
M,M2,D2: GearPtr;
X,Y: Integer;
begin
{ For this procedure to work, we must have the scene and }
{ a prototype door. }
if ( GB = Nil ) or ( GB^.Scene = Nil ) or ( DoorPrototype = Nil ) then Exit;
{ Clear our replacement map. }
{ Set each tile to TRUE; change to FALSE once the door at this }
{ spot has been replaced. This should keep us from repeatedly }
{ replacing the same door over and over in an endless loop. }
for x := 1 to MaxMapWidth do begin
for y := 1 to MaxMapWidth do begin
map[ X,Y] := True;
end;
end;
M := GB^.Scene^.InvCom;
while M <> Nil do begin
M2 := M^.Next;
if ( M^.G = GG_MetaTerrain ) and ( M^.S = GS_MetaDoor ) then begin
{ This is a door. Check it out. }
X := NAttValue( M^.NA , NAG_Location , NAS_X );
Y := NAttValue( M^.NA , NAG_Location , NAS_Y );
if OnTheMap( GB , X , Y ) and Map[ X , Y ] and RectPointOverlap( X0 - 1 , Y0 - 1 , X0 + W , Y0 + H , X , Y ) then begin
D2 := CloneGear( DoorPrototype );
RemoveGear( GB^.Scene^.InvCom , M );
SetNAtt( D2^.NA , NAG_Location , NAS_X , X );
SetNAtt( D2^.NA , NAG_Location , NAS_Y , Y );
InsertInvCom( GB^.Scene , D2 );
Map[ X , Y ] := False;
end;
end;
M := M2;
end;
end;
Procedure DrawWall( GB: GameBoardPtr; MF: GearPtr; Terrain,X0,Y0,W,H: Integer; AddGaps,AddDoors: Boolean );
{ Do the grunt work of drawing the wall. }
var
DX,DY: Integer; { Door Position }
X,Y: Integer;
Procedure DrawWallNow;
begin
if OnTheMap( GB , X , Y ) then begin
{ If this is the door position, deal with that. }
if AddGaps and ( MF <> Nil ) and ( X = DX ) and ( Y = DY ) then begin
if AddDoors then begin
SetTerrain( GB , X , Y , TERRAIN_Threshold );
AddDoor( GB , MF , X , Y );
end else begin
SetTerrain( GB , X , Y , TERRAIN_OpenGround );
end;
{ If this isn't the door position, draw a wall. }
end else begin
SetTerrain( GB , X , Y , Terrain );
end;
end;
end;
begin
{ Top wall. }
DX := Random( W - 2 ) + X0 + 1;
Y := Y0;
DY := Y0;
for X := X0 to ( X0 + W - 1 ) do begin
DrawWallNow;
end;
{ Bottom wall. }
DX := Random( W - 2 ) + X0 + 1;
Y := Y0 + H - 1;
DY := Y;
for X := X0 to ( X0 + W - 1 ) do begin
DrawWallNow;
end;
{ Right wall. }
DY := Random( H - 2 ) + Y0 + 1;
X := X0 + W - 1;
DX := X;
for Y := Y0 to ( Y0 + H - 1 ) do begin
DrawWallNow;
end;
{ Left wall. }
DY := Random( H - 2 ) + Y0 + 1;
X := X0;
DX := X;
for Y := Y0 to ( Y0 + H - 1 ) do begin
DrawWallNow;
end;
end;
Procedure ProcessWall( GB: GameBoardPtr; MF: GearPtr; var Cmd: String; X0,Y0,W,H: Integer; AddGaps,AddDoors: Boolean );
{ Draw a wall around this map feature. Use the MFBORDER terrain, if }
{ appropriate. }
var
terrain: Integer;
begin
{ Decide on what terrain to use for the walls. }
Terrain := DecideTerrainType( MF , Cmd , STAT_MFBorder );
{ Call the wall-drawer. }
DrawWall( GB, MF, Terrain,X0,Y0,W,H, AddGaps,AddDoors );
{ Maybe add the exit, if one was requested. }
if ASTringHasBString( GetSpecial( MF ) , 'ADDEXIT' ) then begin
if ( MF^.G = GG_Scene ) or ( MF^.G = GG_MetaScene ) then begin
Terrain := MF^.Stat[ STAT_MFFloor ];
if ( Terrain < 1 ) or ( Terrain > NumTerr ) then Terrain := TERRAIN_Floor;
SetTerrain( GB , X0 , Y0 + H div 2 + 1 , TERRAIN );
SetTerrain( GB , X0 , Y0 + H div 2 , TERRAIN );
SetTerrain( GB , X0 , Y0 + H div 2 - 1 , TERRAIN );
AddHiddenEntrance( GB , X0 , Y0 + H div 2 , 0 );
end else begin
DrawTerrain( GB , X0 , Y0 + H div 2 , TERRAIN_Threshold , 0 );
AddDoor( GB , MF , X0 , Y0 + H div 2 );
end;
end;
end;
Function WallCoverage( GB: GameBoardPtr; X0,Y0,W,H,WallType: Integer ): Integer;
{ Return the percentage of the map covered in walls. }
var
X,Y,Walls,Tiles: Integer;
begin
Walls := 0;
Tiles := 0;
for X := X0 to ( X0 + W - 1 ) do begin
for Y := Y0 to ( Y0 + H - 1 ) do begin
if TileTerrain( GB,X,Y ) = WallType then Inc( Walls );
Inc( Tiles );
end;
end;
WallCoverage := ( Walls * 100 ) div Tiles;
end;
Procedure DrawTerrainWithWobblyPen( GB: GameBoardPtr; X,Y,Floor1,Floor2: Integer );
{ Draw terrain in this spot, and possibly also in the four adjacent spots. }
const
cardinal_dir: Array [0..3,1..2] of SmallInt = (
(1,0),(0,1),(-1,0),(0,-1)
);
var
t: Integer;
begin
DrawTerrain( GB , X , Y , Floor1 , Floor2 );
for t := 0 to 3 do begin
if ( Random( 3 ) <> 1 ) then begin
DrawTerrain( GB , X + cardinal_dir[ t , 1 ] , Y + cardinal_dir[ t , 2 ] , Floor1 , Floor2 );
end;
end;
end;
Function ProcessCavern( GB: GameBoardPtr; MF: GearPtr; var Cmd: String; X0,Y0,W,H: Integer ): SAttPtr;
{ Construct a cavern with several chambers. }
begin
{ Step One: Place the chambers. }
{ Step Two: Connect the rooms in order. }
end;
Procedure DebugMap( GB: GameBoardPtr; X0,Y0,Wall,Floor1,Floor2: Integer );
{ Spit out a debugging map of the gameboard. }
var
X,Y,Terr: Integer;
msg: String;
F: Text;
begin
Assign( F , 'debugmap.txt' );
Rewrite( F );
for Y := 1 to GB^.Map_Height do begin
msg := '';
for X := 1 to GB^.Map_Width do begin
if ( X = X0 ) and ( Y = Y0 ) then begin
msg := msg + '@';
end else begin
Terr := TileTerrain( GB , X , Y );
if Terr = Wall then msg := msg + '#'
else if Terr = Floor1 then msg := msg + '.'
else if Terr = Floor2 then msg := msg + ','
else msg := msg + '?';
end;
end;
writeln( F , msg );
end;
Close( F );
end;
Procedure ProcessCarve( GB: GameBoardPtr; MF: GearPtr; var Cmd: String; X0,Y0,W,H: Integer );
{ This should draw a cave using the 'L' method. }
var
Floor1,Floor2,Wall: Integer;
P: Point;
Procedure DrawAnL( X , Y: Integer );
var
X1,X2,Y1,Y2,XT,YT: Integer;
begin
{ Determine X0,X1,Y0,Y1 points. }
if Random( 2 ) = 1 then begin
X1 := X - 5 - Random( 16 );
X2 := X;
end else begin
X1 := X;
X2 := X + 5 + Random( 16 );
end;
if Random( 2 ) = 1 then begin
Y1 := Y - 5 - Random( 16 );
Y2 := Y;
end else begin
Y1 := Y;
Y2 := Y + 5 + Random( 16 );
end;
for XT := X1 to X2 do begin
DrawTerrainWithWobblyPen( GB , XT , Y , Floor1 , Floor2 );
end;
for YT := Y1 to Y2 do begin
DrawTerrainWithWobblyPen( GB , X , YT , Floor1 , Floor2 );
end;
end;
Procedure AddWayOut;
{ Far out! Add an exit to this map feature. }
{ If MF is a scene, add a hidden gate to the parent map. }
{ Otherwise add a door. }
Function IsGoodEntrance( X, Y, D: Integer; var P: Point ): Boolean;
{ Check this point and direction to make sure }
{ that it links up to a part of the maze. }
var
XYFTerr: Integer;
FoundFloor: Boolean;
MiniMap: String;
begin
{ The entrance must start at a wall; I don't want }
{ any double doors on the same tile. }
if TileTerrain( GB , X , Y ) <> Wall then Exit( False );
{ If we're starting at a wall, check to make sure }
{ this entrance will connect to the maze. }
FoundFloor := False;
{ Keep searching until we find a floor tile or exit }
{ the bounding box. }
repeat
X := X + AngDir[ D , 1 ];
Y := Y + AngDir[ D , 2 ];
XYFTerr := TileTerrain( GB , X , Y );
FoundFloor := ( XYFTerr = Floor1 ) or ( XYFTerr = Floor2 );
until FoundFloor or not RectPointOverlap( X0 , Y0 , X0 + W - 1 , Y0 + H - 1 , X , Y );
if FoundFloor then begin
P.X := X;
P.Y := Y;
end;
IsGoodEntrance := FoundFloor;
end;
Procedure DrawBlock( X1,Y1: Integer );
{ Draw a 3x3 block centered on X,Y. }
var
X,Y: Integer;
begin
for X := ( X1 - 1 ) to ( X1 + 1 ) do begin
for Y := ( Y1 - 1 ) to ( Y1 + 1 ) do begin
DrawTerrain( GB , X , Y , Floor1 , Floor2 );
end;
end;
end;
Procedure RenderEntrance( E_X0 , E_Y0 , D: Integer; EndPoint: Point );
{ Render the entrance passageway. }
var
X,Y: Integer;
begin
{ Add the hallway. }
X := E_X0;
Y := E_Y0;
repeat
X := X + AngDir[ D , 1 ];
Y := Y + AngDir[ D , 2 ];
DrawBlock( X , Y );
until (( X = EndPoint.X ) and ( Y = EndPoint.Y )) or not RectPointOverlap( X0 , Y0 , X0 + W - 1 , Y0 + H - 1 , X , Y );
{ Add the door. }
if ( MF^.G = GG_Scene ) or ( MF^.G = GG_MetaScene ) then begin
AddHiddenEntrance( GB , E_X0 , E_Y0 , 0 );
end else begin
DrawTerrain( GB , E_X0 , E_Y0 , TERRAIN_Threshold , 0 );
if ( D mod 4 ) = 0 then begin
DrawTerrain( GB , E_X0 , E_Y0 + 1 , Wall , 0 );
DrawTerrain( GB , E_X0 , E_Y0 - 1 , Wall , 0 );
end else begin
DrawTerrain( GB , E_X0 + 1 , E_Y0 , Wall , 0 );
DrawTerrain( GB , E_X0 - 1 , E_Y0 , Wall , 0 );
end;
AddDoor( GB , MF , E_X0 , E_Y0 );
end;
end;
var
Tries,DX,DY: Integer;
P: Point;
begin
{ This may take several attempts to get a good entrance... }
Tries := 50;
while Tries > 0 do begin
{ Decide on a random direction and entry point. }
Case Random( 4 ) of
0: begin
DX := X0 + Random( W - 6 ) + 3;
DY := Y0;
if IsGoodEntrance( DX , DY , 2 , P ) then begin
RenderEntrance( DX , DY , 2 , P );
Tries := -1;
end;
end;
1: begin
DX := X0 + Random( W - 6 ) + 3;
DY := Y0 + H - 1;
if IsGoodEntrance( DX , DY , 6 , P ) then begin
RenderEntrance( DX , DY , 6 , P );
Tries := -1;
end;
end;
2: begin
DX := X0;
DY := Y0 + Random( H - 6 ) + 3;
if IsGoodEntrance( DX , DY , 0 , P ) then begin
RenderEntrance( DX , DY , 0 , P );
Tries := -1;
end;
end;
else begin
DX := X0 + W - 1;
DY := Y0 + Random( H - 6 ) + 3;
if IsGoodEntrance( DX , DY , 4 , P ) then begin
RenderEntrance( DX , DY , 4 , P );
Tries := -1;
end;
end;
end;
Dec( Tries );
end;
end;
begin
Floor1 := DecideTerrainType( MF , Cmd , STAT_MFFloor );
Floor2 := DecideTerrainType( MF , Cmd , STAT_MFMarble );
Wall := DecideTerrainType( MF , Cmd , STAT_MFBorder );
{ Fill in entire area with rocks. }
RectFill( GB , Wall , 0 , X0 , Y0 , W , H );
{ Draw L's until the map is sufficiently perforated. }
DrawAnL( X0 + ( W div 2 ) , Y0 + ( H div 2 ) );
while WallCoverage( GB , X0 , Y0 , W , H , Wall ) > 50 do begin
P.X := X0 + Random( W - 2 ) + 1;
P.Y := Y0 + Random( H - 2 ) + 1;
if OnTheMap( GB , P.X , P.Y ) and ( TileTerrain( GB,P.X,P.Y ) <> Wall ) then DrawAnL( P.X , P.Y );
end;
{ Seal off the edges. }
DrawWall( GB, MF, Wall, X0, Y0, W, H, False, False );
{ At the end, if a way out was requested, draw it. }
if AStringHasBSTring( GetSpecial( MF ) , 'ADDEXIT' ) then AddWayOut;
end;
Procedure ProcessScatter( GB: GameBoardPtr; MF: GearPtr; var Cmd: String; X0,Y0,W,H: Integer );
{ Do a scattering of terrain. Useful for forests, hills, etc. }
var
T1,T2,T3: Integer;
N,T: LongInt;
X,Y: Integer;
begin
{ Begin by reading the terrain definitions. }
T1 := DecideTerrainType( MF , Cmd , STAT_MFFloor );
T2 := DecideTerrainType( MF , Cmd , STAT_MFMarble );
T3 := DecideTerrainType( MF , Cmd , STAT_MFSpecial );
{ Calculate how many iterations to do. }
N := W * H div 2;
for t := 1 to N do begin
{ Pick a random point within the bounds. }
X := X0 + ( W div 2 ) + Random( ( W + 1 ) div 2 ) - Random( ( W + 1 ) div 2 );
Y := Y0 + ( H div 2 ) + Random( ( H + 1 ) div 2 ) - Random( ( H + 1 ) div 2 );
if OnTheMap( GB , X , Y ) then begin
{ Check the terrain at this spot, then move up to the next terrain. }
if ( TileTerrain( GB , X , Y ) = T1 ) and IsLegalTerrain( T2 ) then begin
SetTerrain( GB , X , Y , T2 )
end else if ( TileTerrain( GB , X , Y ) = T2 ) and IsLegalTerrain( T3 ) then begin
SetTerrain( GB , X , Y , T3 )
end else if ( TileTerrain( GB , X , Y ) <> T3 ) and IsLegalTerrain( T1 ) then begin
SetTerrain( GB , X , Y , T1 )
end;
end;
end;
end;
Procedure ProcessEllipse( GB: GameBoardPtr; MF: GearPtr; var Cmd: String; X0,Y0,W,H: Integer );
{ Do a vaguely ellipsoid area of terrain. }
var
T1,T2,T3: Integer;
X,Y,MX,MY,SX,SY,SR: Integer;
begin
{ Begin by reading the terrain definitions. }
T1 := DecideTerrainType( MF , Cmd , STAT_MFFloor );
T2 := DecideTerrainType( MF , Cmd , STAT_MFMarble );
T3 := DecideTerrainType( MF , Cmd , STAT_MFSpecial );
MX := X0 + ( ( W - 1 ) div 2 );
MY := Y0 + ( ( H - 1 ) div 2 );
for X := X0 to ( X0 + W - 1 ) do begin
for Y := Y0 to ( Y0 + H - 1 ) do begin
if OnTheMap( GB , X , Y ) then begin
{ Calculate scaled X and Y values. }
{ Scale things for a circle of radius 100. }
SX := Abs( X - MX ) * 100 div W;
SY := Abs( Y - MY ) * 100 div H;
{ Calculate the radius to this spot. }
SR := Range( 0 , 0 , SX , SY );
if ( SR <= 17 ) and IsLegalTerrain( T3 ) then begin
SetTerrain( GB , X , Y , T3 );
end else if ( SR <= 33 ) and IsLegalTerrain( T2 ) then begin
SetTerrain( GB , X , Y , T2 );
end else if ( SR <= 50 ) and IsLegalTerrain( T1 ) then begin
SetTerrain( GB , X , Y , T1 );
end;
end;
end;
end;
end;
Function SeekRoom( MF: GearPtr; Desig: String ): GearPtr;
{ Seek a sub-mapfeature with the provided deignation. }
{ Return NIL if no such map feature could be found. }
begin
Desig := UpCase( Desig );
MF := MF^.SubCom;
while ( MF <> Nil ) and ( UpCase( SAttValue( MF^.SA , 'DESIG' ) ) <> Desig ) do MF := MF^.Next;
SeekRoom := MF;
end;
Function ProcessLattice( GB: GameBoardPtr; MF: GearPtr; var Cmd: String; X0,Y0,W,H: Integer ): SAttPtr;
{ Draw a grid of lines on the map. }
{ GH2- The lattice will be of 5x5 cells separated by corridors of width 3. }
var
LineTerr,FieldTerr,MarbleTerr,X,Y,CX,CY,L_W,L_H,X_Offset,Y_Offset: Integer;
CornerCell: GearPtr;
Cells: SAttPtr;
begin
FieldTerr := DecideTerrainType( MF , Cmd , STAT_MFFloor );
MarbleTerr := DecideTerrainType( MF , Cmd , STAT_MFMarble );
LineTerr := DecideTerrainType( MF , Cmd , STAT_MFSpecial );
Cells := Nil;
{ Fill in entire area with the corridor terrain type. }
RectFill( GB , LineTerr , 0 , X0 , Y0 , W , H );
{ Start drawing boxes. }
{ The first cell needs 7 tiles, plus 8 tiles for each additional cell. }
L_W := ( W + 1 ) div 8;
L_H := ( H + 1 ) div 8;
{ There should be at least one tile's worth of corridor surrounding the cells, and depending }
{ on the map size there may be as many as 4. Determine the offset to the first cell. }
X_Offset := ( W - ( L_W * 8 - 3 ) ) div 2 + X0;
Y_Offset := ( H - ( L_H * 8 - 3 ) ) div 2 + Y0;
{ I think we're ready to start generating. }
for CX := 1 to L_W do begin
for CY := 1 to L_H do begin
{ Calculate the coordinates of our next cell. }
X := ( CX - 1 ) * 8 + X_Offset;
Y := ( CY - 1 ) * 8 + Y_Offset;
{ If this is one of the four corners, seek the corner cell. }
if ( CX = 1 ) and ( CY = 1 ) then begin
CornerCell := SeekRoom( MF , 'NW' );