forked from jwvhewitt/gearhead-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randmaps.pp
1564 lines (1391 loc) · 49.1 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. }
{
GearHead: Arena, 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
}
interface
uses gears,locale;
const
MG_Normal = 0;
MG_City = 1;
MG_Cave = 2;
DEFAULT_FLOOR_TYPE = 20;
DEFAULT_WALL_TYPE = 23;
SPECIAL_StartHere = 'STARTHERE';
SPECIAL_ShowAll = 'SHOWALL';
SPECIAL_ConvertDoors = 'CELL';
DW_Horizontal = 1;
DW_Vertical = 2;
function RandomMap( Scene: GearPtr ): GameBoardPtr;
implementation
{$IFDEF SDLMODE}
uses gearutil,ghprop,rpgdice,texutil,sdlgfx;
{$ELSE}
uses gearutil,ghprop,rpgdice,texutil,context;
{$ENDIF}
var
Standard_Param_List: SAttPtr;
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( TX , TY ) then begin
if GB^.Map[TX,TY].Terr = 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( TX , TY ) then begin
if GB^.Map[TX,TY].Terr = 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 ( Container^.G = GG_Scene ) then begin
P.X := Random( XMax - 3 - W ) + 3;
P.Y := Random( YMax - 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( 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;
Procedure SelectPlacementPoint( GB: GameBoardPtr; Container,MF: GearPtr; var Cells: SAttPtr; SCheck,STerr: Integer );
{ 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 map }
{ on all sides. }
{ - It should be placed in an area that is considered "clear", }
{ depending upon the SCheck, STerr values. }
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 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 );
{ Delete this cell, to prevent it from being chosen again. }
RemoveSAtt( Cells , TheCell );
end else if not OnTheMap( MF^.Stat[ STAT_XPos ] , MF^.Stat[ STAT_YPos ] ) then 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 > 10000 ) 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;
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 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( X , Y ) then begin
if ( Random( 3 ) = 1 ) and ( T2 <> 0 ) then GB^.Map[X,Y].terr := T2
else GB^.Map[X,Y].terr := 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( 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 AddDoor( GB: GameBoardPtr; MF,DoorPrototype: GearPtr; X,Y: Integer );
{ Add a standard door to the map at the specified location. }
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 := GB^.Map[ X + AngDir[ D , 1 ] , Y + AngDir[ D , 2 ] ].terr;
D := D + 2;
if TerrMan[ T ].Pass < -99 then D := 10;
end;
LocalWall := T;
end;
var
NewDoor: GearPtr;
Name: String;
Roll,Chance: Integer;
begin
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;
{ 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
NewDoor^.Stat[ STAT_Lock ] := ( Roll div 4 ) + 5;
end;
end;
Chance := NAttValue( MF^.NA , NAG_Narrative , NAS_SecretDoorChance );
if Chance > 0 then begin
Roll := Random( 100 );
if Roll < Chance then begin
DrawTerrain( GB , X , Y , LocalWall , 0 );
NewDoor^.Stat[ STAT_MetaVisibility ] := ( Roll div 4 ) + 5;
end;
end;
end;
InsertInvCom( GB^.Scene , NewDoor );
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..XMax , 1..YMax ] 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 XMax do begin
for y := 1 to YMax 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( 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; X, Y, L, Style, WallType: Integer; DoorPrototype: GearPtr );
{ Draw a wall starting at X0,Y0 and continuing for L tiles in }
{ the direction indicated by Style. Use WallType as the wall }
{ terrain, and DoorPrototype for the door. }
var
DL: Integer; { Door longitude. The tile at which to add the door. }
T: Integer;
begin
{ Select the door point now. }
DL := Random( L - 2 ) + 2;
for t := 1 to L do begin
{ If our point is on the map, do drawing here. }
if OnTheMap( X , Y ) then begin
{ If this is our door point, do that now. }
if T = DL then begin
GB^.Map[ X , Y ].Terr := TERRAIN_Threshold;
AddDoor( GB , Nil , DoorPrototype , X , Y );
{ Otherwise draw the wall terrain. }
end else begin
GB^.Map[ X , Y ].Terr := WallType;
end;
if Style = DW_Horizontal then begin
Inc( X );
end else begin
Inc( Y );
end;
end;
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
DX,DY: Integer; { Door Position }
Terrain,X,Y: Integer;
Procedure DrawWallNow;
begin
if OnTheMap( 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
GB^.Map[ X , Y ].Terr := TERRAIN_Threshold;
AddDoor( GB , MF , SeekCurrentLevelGear( MF^.SubCom , GG_MetaTerrain , GS_MetaDoor ) , X , Y );
end else begin
GB^.Map[ X , Y ].Terr := 1;
end;
{ If this isn't the door position, draw a wall. }
end else begin
GB^.Map[ X , Y ].Terr := Terrain;
end;
end;
end;
begin
{ Decide on what terrain to use for the walls. }
Terrain := DecideTerrainType( MF , Cmd , STAT_MFBorder );
{ 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;
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 GB^.Map[X,Y].Terr = WallType then Inc( Walls );
Inc( Tiles );
end;
end;
WallCoverage := ( Walls * 100 ) div Tiles;
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 - 3 - Random( 15 );
X2 := X;
end else begin
X1 := X;
X2 := X + 3 + Random( 15 );
end;
if Random( 2 ) = 1 then begin
Y1 := Y - 2 - Random( 10 );
Y2 := Y;
end else begin
Y1 := Y;
Y2 := Y + 2 + Random( 10 );
end;
for XT := X1 to X2 do begin
DrawTerrain( GB , XT , Y , Floor1 , Floor2 );
end;
for YT := Y1 to Y2 do begin
DrawTerrain( GB , X , YT , Floor1 , Floor2 );
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( P.X , P.Y ) and ( GB^.Map[P.X,P.Y].Terr <> Wall ) then DrawAnL( P.X , P.Y );
end;
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( X , Y ) then begin
{ Check the terrain at this spot, then move up to the next terrain. }
if ( GB^.Map[ X , Y ].Terr = T1 ) and IsLegalTerrain( T2 ) then begin
GB^.Map[ X , Y ].Terr := T2
end else if ( GB^.Map[ X , Y ].Terr = T2 ) and IsLegalTerrain( T3 ) then begin
GB^.Map[ X , Y ].Terr := T3
end else if ( GB^.Map[ X , Y ].Terr <> T3 ) and IsLegalTerrain( T1 ) then begin
GB^.Map[ X , Y ].Terr := 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( 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
GB^.Map[ X , Y ].terr := T3;
end else if ( SR <= 33 ) and IsLegalTerrain( T2 ) then begin
GB^.Map[ X , Y ].terr := T2;
end else if ( SR <= 50 ) and IsLegalTerrain( T1 ) then begin
GB^.Map[ X , Y ].terr := T1;
end;
end;
end;
end;
end;
Procedure ProcessLattice( GB: GameBoardPtr; MF: GearPtr; var Cmd: String; X0,Y0,W,H: Integer );
{ Draw a grid of lines on the map. }
var
LineTerr,FieldTerr,X,Y: Integer;
P: Point;
begin
FieldTerr := DecideTerrainType( MF , Cmd , STAT_MFFloor );
LineTerr := DecideTerrainType( MF , Cmd , STAT_MFSpecial );
{ Fill in entire area with the field terrain type. }
RectFill( GB , FieldTerr , 0 , X0 , Y0 , W , H );
{ Draw the vertical lines. }
P.X := X0;
while P.X < ( X0 + W ) do begin
P.X := P.X + 8 + RollStep( 10 );
for x := P.X to ( P.X + 3 ) do begin
if X < ( X0 + W ) then begin
for Y := Y0 to ( Y0 + H - 1 ) do begin
if OnTheMap( X , Y ) then GB^.Map[ X , Y ].Terr := LineTerr;
end;
end;
end;
end;
{ Draw the horizontal lines. }
P.Y := Y0;
while P.Y < ( Y0 + H ) do begin
P.Y := P.Y + 8 + RollStep( 10 );
for Y := P.Y to ( P.Y + 3 ) do begin
if Y < ( Y0 + H ) then begin
for X := X0 to ( X0 + W - 1 ) do begin
if OnTheMap( X , Y ) then GB^.Map[ X , Y ].Terr := LineTerr;
end;
end;
end;
end;
end;
Function ProcessMitose( GB: GameBoardPtr; MF: GearPtr; var Cmd: String; X0,Y0,W,H: Integer ): SAttPtr;
{ The requested area will split in two. A wall will be drawn }
{ between the two halves, and a door placed in the wall. }
{ This division will continue until we have a bunch of little }
{ rooms. }
var
Cells: SAttPtr;
FloorType,WallType: Integer;
ProtoDoor: GearPtr;
Function IsGoodWallAnchor( AX,AY: Integer ): Boolean;
{ This spot is a good anchor point for a wall as long as }
{ there isn't a door. }
begin
IsGoodWallAnchor := OnTheMap( AX , AY ) and ( GB^.Map[ AX , AY ].terr <> TERRAIN_Threshold );
end;
Procedure DivideArea( CX0 , CY0 , CW , CH: Integer );
{ If this area is large enough, divide it into two }
{ smaller areas, then recurse for each of the }
{ sub-areas. If it is not large enough, then just }
{ add its coordinates to the CELLS list. }
Procedure VerticalDivison;
{ Attempt to divide this area with a vertical wall. }
var
MaybeD,D,Tries: Integer;
begin
tries := 0;
D := 0;
repeat
MaybeD := Random( CH - 2 ) + 1;
if IsGoodWallAnchor( CX0 - 1 , CY0 + MaybeD ) and IsGoodWallAnchor( CX0 + CW , CY0 + MaybeD ) then D := MaybeD;
Inc( Tries );
until ( D <> 0 ) or ( Tries > 5 );
{ Check to make sure it's a good place. }
if D <> 0 then begin
{ Draw the wall. }
DrawWall( GB, CX0, CY0 + D , CW, DW_Horizontal, WallType, ProtoDoor );
{ Recurse to the two sub-areas. }
DivideArea( CX0 , CY0 , CW , D );
DivideArea( CX0 , CY0 + D + 1 , CW , CH - D - 1 );
end else begin
{ No room for further divisions. Just store this cell. }
StoreSAtt( Cells , BStr( CX0 ) + ' ' + BStr( CY0 ) + ' ' + BStr( CW ) + ' ' + BStr( CH ) );
end;
end;
Procedure HorizontalDivison;
{ Attempt to divide this area with a vertical wall. }
var
MaybeD,D,Tries: Integer;
begin
tries := 0;
D := 0;
repeat
MaybeD := Random( CW - 2 ) + 1;
if IsGoodWallAnchor( CX0 + MaybeD , CY0 - 1 ) and IsGoodWallAnchor( CX0 + MaybeD , CY0 + CH ) then D := MaybeD;
Inc( Tries );
until ( D <> 0 ) or ( Tries > 5 );
{ Check to make sure it's a good place. }
if D <> 0 then begin
{ Draw the wall. }
DrawWall( GB, CX0 + D , CY0 , CH, DW_Vertical, WallType, ProtoDoor );
{ Recurse to the two sub-areas. }
DivideArea( CX0 , CY0 , D , CH );
DivideArea( CX0 + D + 1 , CY0 , CW - D - 1 , CH );
end else begin
{ No room for further divisions. Just store this cell. }
StoreSAtt( Cells , BStr( CX0 ) + ' ' + BStr( CY0 ) + ' ' + BStr( CW ) + ' ' + BStr( CH ) );
end;
end;
begin
if ( CW > 6 ) and ( CH > 1 ) and ( Random( 2 ) = 1 ) then begin
HorizontalDivison;
end else if ( CH > 6 ) and ( CW > 1 ) and ( Random( 2 ) = 1 ) then begin
VerticalDivison;
end else if ( CW > CH ) and ( CW > 4 ) and ( CH > 1 ) then begin
HorizontalDivison;
end else if ( CH > 4 ) and ( CW > 1 ) then begin
VerticalDivison;
end else begin
{ No room for further divisions. Just store this cell. }
StoreSAtt( Cells , BStr( CX0 ) + ' ' + BStr( CY0 ) + ' ' + BStr( CW ) + ' ' + BStr( CH ) );
end;
end;
begin
{ Initialize values. }
Cells := Nil;
FloorType := DecideTerrainType( MF , Cmd , STAT_MFFloor );
WallType := DecideTerrainType( MF , Cmd , STAT_MFBorder );
ProtoDoor := SeekCurrentLevelGear( MF , GG_MetaTerrain , GS_MetaDoor );
RectFill( GB , FloorType , 0 , X0 + 1 , Y0 + 1 , W - 2 , H - 2 );
DivideArea( X0 + 1 , Y0 + 1 , W - 2 , H - 2 );
ProcessMitose := Cells;
end;
Function ProcessMonkeyMaze( GB: GameBoardPtr; MF: GearPtr; var Cmd: String; X0,Y0,W,H: Integer; MakeExternalDoor: Boolean ): SAttPtr;
{ Draw a maze as featured in my non-hit game, Dungeon Monkey. }
{ This procedure is lifted straight from that game's random map }
{ generator, actually... It's about time GearHead got a new map }
{ type, isn't it? }
var
Cells: SAttPtr;
FloorType,WallType: Integer;
ProtoDoor: GearPtr;
NXMax,NYMax: Integer;
const
VecDir: Array [1..9,1..2] of Integer = (
(-1, 1),( 0, 1),( 1, 1),
(-1, 0),( 0, 0),( 1, 0),
(-1,-1),( 0,-1),( 1,-1)
);
Function GetTerrain( X,Y: Integer ): Integer;
{ Safely return the terrain of this tile. }
begin
if OnTheMap( X , Y ) then begin
GetTerrain := gb^.Map[X,Y].terr;
end else begin
GetTerrain := 1;
end;
end;
Function NodeX( ND: Integer ): Integer;
{ Convert node coordinate ND to an actual map coordinate. }
begin
NodeX := ND * 5 - 3 + X0;
end;
Function NodeY( ND: Integer ): Integer;
{ Convert node coordinate ND to an actual map coordinate. }
begin
NodeY := ND * 5 - 3 + Y0;
end;
Function AllNodesConnected: Boolean;
{ Return TRUE if all the nodes have been connected, or }
{ FALSE if some of them haven't been. }
var
FoundAWall: Boolean;
NX,NY: Integer;
begin
{ At the beginning, we haven't found any walls yet. }
FoundAWall := False;
for NX := 1 to NXMax do begin
for NY := 1 to NYMax do begin
if GetTerrain( NodeX( NX ) , NodeY( NY ) ) = WallType then FoundAWall := True;
end;
end;
AllNodesConnected := Not FoundAWall;
end;
Procedure DrawAnL( NX , NY: Integer );
{ Draw an L-Shaped corridor on the map, centered on }
{ node point NX, NY. }
var
X1,X2,Y1,Y2,XT,YT: Integer;
begin
{ Determine X0,X1,Y0,Y1 points. }
if ( NX > 1 ) and ( ( Random( 2 ) = 1 ) or ( NX = NXMax ) ) then begin
X1 := NodeX( NX ) - ( 5 * ( Random( NX - 1 ) + 1 ) );
X2 := NodeX( NX );
end else begin
X1 := NodeX( NX );
X2 := NodeX( NX ) + ( 5 * ( Random( NXMax - NX ) + 1 ) );
end;
if ( NY > 1 ) and ( ( Random( 2 ) = 1 ) or ( NY = NYMax ) ) then begin
Y1 := NodeY( NY ) - ( 5 * ( Random( NY - 1 ) + 1 ) );
Y2 := NodeY( NY );
end else begin
Y1 := NodeY( NY );
Y2 := NodeY( NY ) + ( 5 * ( Random( NYMax - NY ) + 1 ) );
end;
for XT := X1 to X2 do begin
if ( XT <> X1 ) and ( GetTerrain( XT , NodeY( NY ) ) = FloorType ) then break
else DrawTerrain( GB , XT , NodeY( NY ) , FloorType , 0 );
end;
for YT := Y1 to Y2 do begin
if ( YT <> Y1 ) and ( GetTerrain( NodeX( NX ) , YT ) = FloorType ) then break
else DrawTerrain( GB , NodeX( NX ) , YT , FloorType , 0 );
end;
end; {DrawAnL}
Procedure DrawOneLine( NX , NY: Integer );
{ Draw an regular corridor on the map, centered on }
{ node point NX, NY. }
var
X1,X2,Y1,Y2,XT,YT: Integer;
begin
{ Determine X0,X1,Y0,Y1 points. }
if Random( 2 ) = 1 then begin
if ( NX > 1 ) and ( ( Random( 2 ) = 1 ) or ( NX = NXMax ) ) then begin
X1 := NodeX( NX ) - ( 5 * ( Random( NX - 1 ) + 1 ) );
X2 := NodeX( NX );
end else begin
X1 := NodeX( NX );
X2 := NodeX( NX ) + ( 5 * ( Random( NXMax - NX ) + 1 ) );
end;
for XT := X1 to X2 do begin
if ( XT <> X1 ) and ( GetTerrain( XT , NodeY( NY ) ) = FloorType ) then break
else DrawTerrain( GB , XT , NodeY( NY ) , FloorType , 0 );
end;
end else begin
if ( NY > 1 ) and ( ( Random( 2 ) = 1 ) or ( NY = NYMax ) ) then begin
Y1 := NodeY( NY ) - ( 5 * ( Random( NY - 1 ) + 1 ) );
Y2 := NodeY( NY );
end else begin
Y1 := NodeY( NY );
Y2 := NodeY( NY ) + ( 5 * ( Random( NYMax - NY ) + 1 ) );
end;
for YT := Y1 to Y2 do begin
if ( YT <> Y1 ) and ( GetTerrain( NodeX( NX ) , YT ) = FloorType ) then break
else DrawTerrain( GB , NodeX( NX ) , YT , FloorType , 0 );
end;
end;
end; {DrawOneLine}
Procedure CarveALine( NX , NY: Integer );
{ Starting at the indicated node, attempt to carve a line from }
{ NX,NY to another floor tile. }
var
D: Integer;
X,Y: Integer;
begin
{ Select one of the four cardinal directions at random. }
D := Random( 4 ) * 2 + 2;
{ We'll use the VecDir array to direct the line. }
X := NodeX( NX );
Y := NodeY( NY );
{ Keep traveling until we either find a floor or go off the map. }
repeat
X := X + VecDir[ D , 1 ];
Y := Y + VecDir[ D , 2 ];
until ( GetTerrain( X , Y ) = FloorType ) or ( Not RectPointOverlap( X0 , Y0 , X0 + W - 1 , Y0 + H - 1 , X , Y ) );
{ If we didn't go off the map, we must have hit a floor. Bonus! }
{ Start carving the line in the randomly prescribed direction. }
if RectPointOverlap( X0 , Y0 , X0 + W - 1 , Y0 + H - 1 , X , Y ) then begin
X := NodeX( NX );
Y := NodeY( NY );
repeat
DrawTerrain( GB , X , Y , FloorType , 0 );
X := X + VecDir[ D , 1 ];
Y := Y + VecDir[ D , 2 ];
until ( GetTerrain( X , Y ) = FloorType ) or ( Not OnTheMap( X , Y ) );
end;
end; {CarveALine}
Procedure DrawTheMaze;
{ First step, generate a decent maze. }
var
NX,NY,Tries: Integer;
begin
{ Start with a random point, then draw an "L" there. }
NX := Random( NXMax - 2 ) + 2;
NY := Random( NYMax - 2 ) + 2;
DrawAnL( NX , NY );
Tries := 500;
{ Randomly expand upon this maze until all the nodes have }
{ been connected to one another. }
repeat
for NX := 1 to NXMax do begin
for NY := 1 to NYMax do begin
if GetTerrain( NodeX( NX ) , NodeY( NY ) ) = FloorType then begin
if Random( 12 ) = 1 then DrawOneLine( NX , NY );
end else begin
CarveALine( NX , NY );
end;
end;
end;
Dec( Tries );
until AllNodesConnected or ( Tries < 1 );
end; {DrawTheMaze}
Procedure FillDungeon;
{ Fill the dungeon with rooms. }
var
NodeClear: Array [1..XMax div 5,1..YMax div 5] of Boolean;
{ Lists nodes which have not yet been developed. }
X,Y,NumRooms,TRoom: Integer;
Function SelectClearNode: Point;
{ Select a node which is free for development. }
var
P: Point;
Tries: Integer;
begin
Tries := 500;
repeat
P.X := Random( NXMax ) + 1;
P.Y := Random( NYMax ) + 1;
Dec( Tries );
until NodeClear[ P.X , P.Y ] or ( Tries < 1 );
SelectClearNode := P;
end;
Procedure RoomRenderer( X1,Y1,W,H: Integer );
{ Render a room, adding doors at whimsy. }
Procedure MaybeAddDoor( DX,DY: Integer );
{ Maybe add a door to this spot, or maybe not. }
begin