forked from jwvhewitt/gearhead-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaywright.pp
1271 lines (1066 loc) · 40.2 KB
/
playwright.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 playwright;
{ This unit handles the PLOT type of 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;
{ G = GG_Plot }
{ S = ID Number (not nessecarily unique) }
{ Stat[1..8] = Elements (Described by SAtts Element1..Element8) }
Function ElementLocation( Adv,Plot: GearPtr; N: Integer; GB: GameBoardPtr ): Integer;
Function ElementFaction( Adv,Plot: GearPtr; N: Integer; GB: GameBoardPtr ): Integer;
Function ElementName( Adventure,Plot: GearPtr; N: Integer; GB: GameBoardPtr ): String;
Function SceneDesc( Scene: GearPtr ): String;
Function NumFreeScene( Adventure,Plot: GearPtr; GB: GameBoardPtr; Desc: String ): Integer;
Function FindFreeScene( Adventure,Plot: GearPtr; GB: GameBoardPtr; Desc: String; Num: Integer ): GearPtr;
Function SearchForScene( Adventure , Plot: GearPtr; GB: GameBoardPtr; Desc: String ): GearPtr;
Function TeamDescription( Scene,Team: GearPtr ): String;
Procedure ChooseTeam( NPC , Scene: GearPtr );
Function InsertPlot( Adventure,Plot: GearPtr; Debug: Boolean; GB: GameBoardPtr ): Boolean;
Function InsertStory( Slot,Story: GearPtr; GB: GameBoardPtr ): Boolean;
Function InsertGlobalArc( Adventure,ARC: GearPtr; GB: GameBoardPtr ): Boolean;
Function InsertStoryArc( Story,Arc: GearPtr; GB: GameBoardPtr ): Boolean;
Procedure AdvancePlot( GB: GameBoardPtr; Adv,Plot: GearPtr; N: Integer );
Procedure InsertNPCIntoDynamicScene( NPC,Scene: GearPtr; Team: Integer );
Procedure AddArchEnemyToScene( Adventure,Scene,PC: GearPtr; MDesc: String );
Procedure AddArchAllyToScene( Adventure,Scene,PC: GearPtr );
implementation
{$IFDEF SDLMODE}
uses ability,interact,gearutil,ghchars,ghparser,texutil,rpgdice,sdlgfx;
{$ELSE}
uses ability,interact,gearutil,ghchars,ghparser,texutil,rpgdice,context;
{$ENDIF}
var
Fast_Seek_Element: Array [1..NumGearStats] of GearPtr;
Function ElementLocation( Adv,Plot: GearPtr; N: Integer; GB: GameBoardPtr ): Integer;
{ Find the scene number where this element resides. If no such }
{ scene can be found, return 0. }
var
E: GearPtr;
it: Integer;
begin
E := SeekPlotElement( Adv, Plot, N , GB );
{ First, call the normal FindGearScene function. }
it := FindGearScene( E , GB );
ElementLocation := it;
end;
Function ElementFaction( Adv,Plot: GearPtr; N: Integer; GB: GameBoardPtr ): Integer;
{ Find the scene number where this element resides. If no such }
{ scene can be found, return 0. }
var
E: GearPtr;
begin
E := SeekPlotElement( Adv, Plot, N , GB );
ElementFaction := GetFactionID( E );
end;
Function FilterElementDescription( var IDesc: String ): String;
{ Given this element description, break it up into the }
{ intrinsic and relative description strings. }
var
ZeroDesc,cmd,RDesc: String;
begin
ZeroDesc := IDesc;
RDesc := '';
IDesc := '';
while ZeroDesc <> '' do begin
cmd := ExtractWord( ZeroDesc );
if cmd[1] = '!' then begin
RDesc := RDesc + ' ' + cmd;
{ If this relative command requires a value, }
{ copy that over as well. }
{ The two that don't are !Global and !Lancemate. }
if ( UpCase( cmd )[2] <> 'G' ) and ( UpCase( cmd )[2] <> 'L' ) then begin
cmd := ExtractWord( ZeroDesc );
RDesc := RDesc + ' ' + cmd;
end;
end else begin
IDesc := IDesc + ' ' + cmd;
end;
end;
FilterElementDescription := RDesc;
end;
Function PWAreEnemies( Adv, Part: GearPtr; N: Integer ): Boolean;
{ Return TRUE if the faction of element N is an enemy of the }
{ faction of PART. }
var
Fac: GearPtr;
F0,F1: Integer;
begin
F0 := GetFactionID( Fast_Seek_Element[ N ] );
F1 := GetFactionID( Part );
{ A faction is never its own enemy. }
if F0 = F1 then Exit( False );
Fac := SeekFaction( Adv , F1 );
if Fac <> Nil then begin
PWAreEnemies := NAttValue( Fac^.NA , NAG_FactionScore , F0 ) < 0;
end else begin
PWAreEnemies := False;
end;
end;
Function PWAreAllies( Adv, Part: GearPtr; N: Integer ): Boolean;
{ Return TRUE if the faction of element N is an ally of the }
{ faction of PART. }
var
Fac: GearPtr;
F0,F1: Integer;
begin
F0 := GetFactionID( Fast_Seek_Element[ N ] );
F1 := GetFactionID( Part );
{ A faction is always allied with itself. }
if F0 = F1 then Exit( True );
Fac := SeekFaction( Adv , F1 );
if Fac <> Nil then begin
PWAreAllies := NAttValue( Fac^.NA , NAG_FactionScore , F0 ) > 0;
end else begin
PWAreAllies := False;
end;
end;
Function PartMatchesRelativeCriteria( Adv,Plot,Part: GearPtr; GB: GameBoardPtr; Desc: String ): Boolean;
{ Return TRUE if the part matches the relative criteria }
{ provided, or FALSE if it does not. }
var
it: Boolean;
cmd: String;
Q: Char;
begin
{ Assume TRUE unless shown otherwise. }
it := True;
{ Lancemates can only be selected if they're asked for by the plot. }
if AStringHasBString( Desc , '!LANCEMATE' ) then it := ( Part^.G = GG_Character ) and ( NAttValue( Part^.NA , NAG_Location , NAS_Team ) = NAV_LancemateTeam )
else it := ( NAttValue( Part^.NA , NAG_Location , NAS_Team ) <> NAV_LancemateTeam );
{ Check all the bits in the description string. }
While ( Desc <> '' ) and it do begin
Cmd := ExtractWord( Desc );
if Cmd <> '' then begin
DeleteFirstChar( cmd );
Q := UpCase( cmd )[ 1 ];
if ( Q = 'N' ) and ( Plot <> Nil ) then begin
{ L1 must equal L2. }
it := it and ( FindGearScene( Part , GB ) = FindGearScene( Fast_Seek_Element[ ExtractValue( Desc ) ] , GB ) );
end else if ( Q = 'F' ) and ( Plot <> Nil ) then begin
{ L1 must not equal L2. }
it := it and ( FindGearScene( Part , GB ) <> FindGearScene( Fast_Seek_Element[ ExtractValue( Desc ) ] , GB ) );
end else if Q = 'M' then begin
{ MEMBER. This part must belong to the }
{ requested faction. }
it := it and ( GetFactionID( Part ) = ExtractValue( Desc ) );
end else if ( Q = 'C' ) and ( Plot <> Nil ) then begin
{ COMRADE. This part must belong to the }
{ same faction as the requested element. }
it := it and ( GetFactionID( Fast_Seek_Element[ ExtractValue( Desc ) ] ) = GetFactionID( Part ) );
end else if ( Q = 'X' ) and ( Plot <> Nil ) then begin
{ eXclude. This part must not belong to the }
{ same faction as the requested element. }
it := it and ( not PWAreAllies( Adv, Part , ExtractValue( Desc ) ) );
end else if ( Q = 'E' ) and ( Plot <> Nil ) then begin
{ ENEMY. The faction of the requested }
{ element must be hated by this part's }
{ faction. }
it := it and PWAreEnemies( Adv, Part , ExtractValue( Desc ) );
end;
end;
end;
{ Return the result. }
PartMatchesRelativeCriteria := it;
end;
Function NPCMatchesDesc( Adv,Plot,NPC: GearPtr; IDesc,RDesc: String; GB: GameBoardPtr ): Boolean;
{ Return TRUE if the supplied NPC matches this description }
{ string, FALSE otherwise. Note that an extra check is performed }
{ to prevent animals from being chosen for plots, which could }
{ otherwise happen if the animal in question has a Character ID. }
{ Thank you Fluffy the Stegosaurus. }
var
it: Boolean;
begin
{ DESC should contain a string list of all the stuff we want }
{ our NPC to have. Things like gender, personality traits, }
{ et cetera. Most of these things are intrinsic to the NPC, }
{ but some of them are defined relative to other elements of }
{ this plot. }
it := PartMatchesCriteria( XNPCDesc( Adv , NPC ) , IDesc );
if it then it := PartMatchesRelativeCriteria( Adv, Plot, NPC, GB, RDesc );
NPCMatchesDesc := it and ( UpCase( SAttValue( NPC^.SA , 'JOB' )) <> 'ANIMAL' );
end;
Function NumFreeNPC( Adv, Plot: GearPtr; Desc: String; GB: GameBoardPtr ): Integer;
{ This function will count the number of CHARACTER gears }
{ present in ADVENTURE which have a CID, which match the DESC, }
{ and which are not currently involved in a plot. }
var
IDesc,RDesc: String;
Total: Integer;
Function CheckAlongPath( P: GearPtr ): Integer;
var
CID: LongInt;
N: Integer;
begin
N := 0;
while P <> Nil do begin
if ( P^.G = GG_Character ) and NPCMatchesDesc( Adv, Plot, P , IDesc , RDesc , GB ) then begin
{ Next, check to make sure it has an assigned CID. }
CID := NAttValue( P^.NA , NAG_Personal , NAS_CID );
if ( CID <> 0 ) then Inc( N );
end;
N := N + CheckAlongPath( P^.SubCom );
N := N + CheckAlongPath( P^.InvCom );
P := P^.Next;
end;
CheckAlongPath := N;
end;
begin
{ Initialize the total to 0. }
Adv := FindRoot( Adv );
{ Filter the relative description from the instrinsic description. }
IDesc := Desc;
RDesc := FilterElementDescription( IDesc );
Total := CheckAlongPath( Adv^.SubCom );
{ Check the invcomponents of the adventure only if global }
{ NPCs are allowed by the DESC string. }
if AStringHasBString( RDesc , '!G' ) then begin
Total := Total + CheckAlongPath( Adv^.InvCom );
end;
if GB <> Nil then begin
Total := Total + CheckAlongPath( GB^.Meks );
end;
{ Return the value. }
NumFreeNPC := Total;
end;
Function FindFreeNPC( Adventure,Plot: GearPtr; Desc: String; Num: Integer; GB: GameBoardPtr ): GearPtr;
{ Locate the Nth free NPC in the tree. }
var
CID,N: Integer;
RDesc: String;
TheGearWeWant: GearPtr;
{ PROCEDURES BLOCK. }
Procedure CheckAlongPath( Part: GearPtr );
{ CHeck along the path specified. }
begin
while ( Part <> Nil ) and ( TheGearWeWant = Nil ) do begin
{ Increment N if this gear matches our description. }
if ( Part^.G = GG_Character ) and NPCMatchesDesc( Adventure, Plot, Part , Desc , RDesc , GB ) then begin
{ Next, check to make sure it has an assigned CID. }
CID := NAttValue( Part^.NA , NAG_Personal , NAS_CID );
if ( CID <> 0 ) then Inc( N );
end;
if N = Num then TheGearWeWant := Part;
if TheGearWeWant = Nil then CheckAlongPath( Part^.InvCom );
if TheGearWeWant = Nil then CheckAlongPath( Part^.SubCom );
Part := Part^.Next;
end;
end;
begin
TheGearWeWant := Nil;
N := 0;
{ Filter the description into relative and intrinsic bits. }
RDesc := FilterElementDescription( Desc );
{ Part 0 is the master gear itself. }
if Num < 1 then Exit( Adventure );
{ Check the invcomponents of the adventure only if global }
{ NPCs are allowed by the DESC string. }
if AStringHasBString( RDesc , '!G' ) then begin
CheckAlongPath( Adventure^.InvCom );
end;
if TheGearWeWant = Nil then CheckAlongPath( Adventure^.SubCom );
if TheGearWeWant = Nil then CheckAlongPath( GB^.Meks );
FindFreeNPC := TheGearWeWant;
end; { FindFreeNPC }
Function CharacterSearch(Adv, Plot: GearPtr; Desc: String; GB: GameBoardPtr ): GearPtr;
{ Search high and low looking for a character that matches }
{ the provided search description! }
var
NPC: GearPtr;
NumMatches: Integer;
begin
NumMatches := NumFreeNPC( Adv , Plot , Desc , GB );
if NumMatches > 0 then begin
{ Pick one of the free NPCs at random. }
NPC := FindFreeNPC( Adv , Plot , Desc , Random( NumMatches ) + 1 , GB );
end else begin
{ No free NPCs were found. Bummer. }
NPC := Nil;
end;
CharacterSearch := NPC;
end; { Character Search }
Function SceneDesc( Scene: GearPtr ): String;
{ Create a description string for this scene. }
var
it: String;
begin
if ( Scene = Nil ) or ( Scene^.G <> GG_Scene ) then begin
it := '';
end else begin
it := SAttValue( Scene^.SA , 'TYPE' ) + ' SCALE' + BStr( Scene^.V );
end;
SceneDesc := it;
end;
Function NumFreeScene( Adventure,Plot: GearPtr; GB: GameBoardPtr; Desc: String ): Integer;
{ Find out how many scenes match the provided description. }
var
Scene: GearPtr;
RDesc: String;
N: Integer;
begin
Scene := Adventure^.SubCom;
N := 0;
RDesc := FilterElementDescription( Desc );
while Scene <> Nil do begin
if ( Scene^.G = GG_Scene ) and PartMatchesCriteria( SceneDesc( Scene ) , Desc ) and PartMatchesRelativeCriteria( Adventure, Plot, Scene, GB, RDesc ) then Inc( N );
Scene := Scene^.Next;
end;
NumFreeScene := N;
end;
Function FindFreeScene( Adventure,Plot: GearPtr; GB: GameBoardPtr; Desc: String; Num: Integer ): GearPtr;
{ Find the NUM'th scene that matches the provided description. }
var
Scene,S2: GearPtr;
RDesc: String;
begin
Scene := Adventure^.SubCom;
S2 := Nil;
RDesc := FilterElementDescription( Desc );
while Scene <> Nil do begin
if ( Scene^.G = GG_Scene ) and PartMatchesCriteria( SceneDesc( Scene ) , Desc ) and PartMatchesRelativeCriteria( Adventure, Plot, Scene, GB, RDesc ) then begin
Dec( Num );
if Num = 0 then S2 := Scene;
end;
Scene := Scene^.Next;
end;
FindFreeScene := S2;
end;
Function SearchForScene( Adventure , Plot: GearPtr; GB: GameBoardPtr; Desc: String ): GearPtr;
{ Try to find a scene matching the description. }
var
NumElements: Integer;
begin
NumElements := NumFreeScene( Adventure , Plot , GB , Desc );
if NumElements > 0 then begin
{ Pick one of the free scenes at random. }
SearchForScene := FindFreeScene( Adventure , Plot , GB , Desc , Random( NumElements ) + 1 );
end else begin
SearchForScene := Nil;
end;
end;
function FactionDesc( Fac: GearPtr ): String;
{ Return a description of the provided faction. }
var
it: String;
begin
{ Basic description is the faction's TYPE string attribute. }
it := SATtValue( Fac^.SA , 'TYPE' );
FactionDesc := it;
end;
Function NumFreeFaction( Adventure,Plot: GearPtr; GB: GameBoardPtr; Desc: String ): Integer;
{ Find out how many factions match the provided description. }
var
Fac: GearPtr;
N: Integer;
RDesc: String;
begin
Fac := Adventure^.InvCom;
N := 0;
RDesc := FilterElementDescription( Desc );
while Fac <> Nil do begin
if ( Fac^.G = GG_Faction ) and PartMatchesCriteria( FactionDesc( Fac ) , Desc ) and PartMatchesRelativeCriteria( Adventure, Plot, Fac, GB, RDesc ) then Inc( N );
Fac := Fac^.Next;
end;
NumFreeFaction := N;
end;
Function FindFreeFaction( Adventure,Plot: GearPtr; GB: GameBoardPtr; Desc: String; Num: Integer ): GearPtr;
{ Find the NUM'th scene that matches the provided description. }
var
Fac,F2: GearPtr;
RDesc: String;
begin
Fac := Adventure^.InvCom;
F2 := Nil;
RDesc := FilterElementDescription( Desc );
while Fac <> Nil do begin
if ( Fac^.G = GG_Faction ) and PartMatchesCriteria( FactionDesc( Fac ) , Desc ) and PartMatchesRelativeCriteria( Adventure, Plot, Fac, GB, RDesc ) then begin
Dec( Num );
if Num = 0 then F2 := Fac;
end;
Fac := Fac^.Next;
end;
FindFreeFaction := F2;
end;
Function TeamDescription( Scene,Team: GearPtr ): String;
{ Create a description for this team. This is to be used }
{ by the team location routines. }
var
it: String;
begin
{ Start with an empty string. }
it := '';
if Team <> Nil then begin
if AreEnemies( Scene, Team^.S , NAV_DefPlayerTeam ) then begin
it := it + ' enemy';
end else if AreAllies( Scene , Team^.S , NAV_DefPlayerTeam ) then begin
it := it + ' ally';
end;
it := it + ' ' + AI_Type_Label[ Team^.Stat[ STAT_TeamOrders ] ];
if Team^.Stat[ STAT_WanderMon ] > 0 then it := it + ' wmon';
end;
TeamDescription := it;
end;
Function CreateTeam( Scene: GearPtr; TDesc: String ): GearPtr;
{ Make a new team corresponding to the description provided. }
var
Team: GearPtr;
CMD: String;
T: Integer;
begin
Team := NewGear( Nil );
Team^.G := GG_Team;
Team^.S := NewTeamID( Scene );
InsertSubCom( Scene , Team );
{ Set the new team's attributes based upon the remainder of }
{ the PLACE string. }
TDesc := UpCase( TDesc );
while TDesc <> '' do begin
cmd := ExtractWord( TDesc );
if cmd = 'ENEMY' then begin
SetNAtt( Team^.NA , NAG_SideReaction , NAV_DefPlayerTeam , NAV_AreEnemies );
end else if cmd = 'ALLY' then begin
SetNAtt( Team^.NA , NAG_SideReaction , NAV_DefPlayerTeam , NAV_AreAllies );
end else begin
{ This command may be an AIType. }
for t := 0 to NumAITypes do begin
if cmd = AI_Type_Label[ t ] then Team^.Stat[ STAT_TeamOrders ] := t;
end;
end;
end;
CreateTeam := Team;
end;
Function FindMatchingTeam( Scene: GearPtr; TDesc: String ): GearPtr;
{ Locate a team which matches the provided description. }
{ If no such team can be found, return Nil. If more than }
{ one team is found, return one of them, although there's }
{ no guarantee which one. }
var
T,it: GearPtr;
begin
{ Teams are located as root subcomponents of the scene, }
{ so look there. }
T := Scene^.SubCom;
{ Initialize our search bit to NIL. }
it := Nil;
while ( T <> Nil ) and ( it = Nil ) do begin
if ( T^.G = GG_Team ) and ( T^.S <> NAV_DefPlayerTeam ) and PartMatchesCriteria( TeamDescription( Scene , T ) , TDesc ) then begin
it := T;
end;
T := T^.Next;
end;
FindMatchingTeam := it;
end;
Procedure ChooseTeam( NPC , Scene: GearPtr );
{ Find a team which matches the NPC's needs. }
var
TeamData: String;
Team: GearPtr;
begin
TeamData := SAttValue( NPC^.SA , 'TEAMDATA' );
Team := FindMatchingTeam( Scene , TeamData );
{ If no matching team was found, create a new team. }
if Team = Nil then begin
Team := CreateTeam( Scene , TeamData );
end;
{ Store the correct team number in the NPC. }
SetNAtt( NPC^.NA , NAG_Location , NAS_Team , Team^.S );
end;
Procedure DelinkNPCForEncounter( NPC: GearPtr );
{ Delink the provided NPC from its current location. }
{ BUGS: This procedure won't work if the NPC is on the game board. }
{ It's really only used by the AddEnemyToScene and AddAllyToScene }
{ procedures below; a very similar procedure can be found in the }
{ ProcessSetSceneFaction procedure in ArenaScript. I should probably }
{ combine the two into a single procedure some day... }
var
Scene: GearPtr;
begin
{ If the NPC is currently local, add information so that it }
{ will be able to return home. }
Scene := NPC^.Parent;
while ( Scene <> Nil ) and ( Scene^.G <> GG_Scene ) do Scene := Scene^.Parent;
if Scene <> Nil then begin
SetNAtt( NPC^.NA , NAG_ParaLocation , NAS_OriginalHome , Scene^.S );
SetSATt( NPC^.SA , 'TEAMDATA <' + TeamDescription( Scene, LocateTeam( Scene , NAttValue( NPC^.NA , NAG_Location , NAS_Team ) ) ) + '>' );
end else begin
SetNAtt( NPC^.NA , NAG_ParaLocation , NAS_OriginalHome , -1 );
end;
{ Get rid of the NPC's location info. }
StripNAtt( NPC , NAG_Location );
if IsSubCom( NPC ) then DelinkGear( NPC^.Parent^.Subcom , NPC )
else if IsInvCom( NPC ) then DelinkGear( NPC^.Parent^.Invcom , NPC );
end;
Function DeploySceneElement( GB: GameBoardPtr; Adventure,Plot: GearPtr; N: Integer ): GearPtr;
{ Deploy the next element, give it a unique ID number if }
{ appropriate, then return a pointer to the it. }
var
E,Dest: GearPtr; { Element & Destination. }
D: Integer;
Place: String;
ID: LongInt;
begin
{ Find the first uninitialized entry in the list. }
{ This is gonna be our next element. }
E := Plot^.InvCom;
While ( E <> Nil ) and ( SAttValue( E^.SA , 'ELEMENT' ) <> '' ) do begin
E := E^.Next;
end;
if E <> Nil then begin
{ Give our new element a unique ID, and store its ID in the Plot. }
{ Characters get CIDs, everything else gets NIDs. }
if E^.G = GG_Character then begin
ID := NewCID( GB , Adventure );
SetNAtt( E^.NA , NAG_Personal , NAS_CID , ID );
SetSAtt( Plot^.SA , 'ELEMENT' + BStr( N ) + ' <C Prefab>' );
SetSAtt( E^.SA , 'ELEMENT <C Prefab>' );
end else begin
ID := NewNID( GB , Adventure );
SetNAtt( E^.NA , NAG_Narrative , NAS_NID , ID );
SetSAtt( Plot^.SA , 'ELEMENT' + BStr( N ) + ' <I Prefab>' );
SetSAtt( E^.SA , 'ELEMENT <I Prefab>' );
end;
Plot^.Stat[ N ] := ID;
{ Find out if we have to put this element somewhere else. }
Place := SAttValue( E^.SA , 'PLACE' );
{ If we have to put it somewhere, do so now. }
{ Otherwise leave it where it is. }
if Place <> '' then begin
{ Delink the element from the plot. }
DelinkGear( Plot^.InvCom , E );
{ Determine its target location. }
D := ExtractValue( Place );
if ( D >= 1 ) and ( D < N ) then begin
Dest := SeekPlotElement( Adventure , Plot , D , Nil );
if Dest = Nil then begin
{ An invalid location was specified... }
DisposeGear( E );
end else if ( Dest^.G <> GG_Scene ) and IsLegalSlot( Dest , E ) then begin
{ If E can be an InvCom of Dest, stick it there. }
InsertInvCom( Dest , E );
end else begin
{ If Dest isn't a scene, find the scene DEST is in itself }
{ and stick E in there. }
while ( Dest <> Nil ) and ( Dest^.G <> GG_Scene ) do Dest := Dest^.Parent;
if Dest <> Nil then begin
InsertInvCom( Dest , E );
{ If E is a character, this brings us to the next problem: }
{ we need to assign a TEAM for E to be a member of. }
if E^.G = GG_Character then begin
SetSAtt( E^.SA , 'TEAMDATA <' + Place + '>' );
ChooseTeam( E , Dest );
end;
end else begin
{ Couldn't find a SCENE gear. Get rid of E. }
DisposeGear( E );
end;
end;
end;
end;
end;
DeploySceneElement := E;
end;
Function FindElement( Adventure,Plot: GearPtr; N: Integer; GB: GameBoardPtr ; Debug: Boolean ): Boolean;
{ Locate and store the Nth element for this plot. }
{ Return TRUE if a suitable element could be found, or FALSE }
{ if no suitable element exists in the adventure & this plot }
{ will have to be abandoned. }
var
Element: GearPtr;
Desc,EKind: String;
OK: Boolean;
NumElements: Integer;
begin
{ Error check }
if ( N < 1 ) or ( N > NumGearStats ) then Exit( False );
{ Find the description for this element. }
desc := UpCase( SAttValue( Plot^.SA , 'ELEMENT' + BStr( N ) ) );
DeleteWhiteSpace( Desc );
{ Initialize OK to TRUE. }
OK := True;
if desc <> '' then begin
EKind := ExtractWord( Desc );
if EKind[1] = 'C' then begin
{ This element is a CHARACTER. Find one. }
{ IMPORTANT!!!: Character being sought muct not have a plot already!!! }
if ( Plot <> Nil ) and ( Plot^.G = GG_Plot ) then desc := 'NOPLOT ' + desc;
Element := CharacterSearch( Adventure , Plot , Desc , GB );
if Element <> Nil then begin
{ Store the NPC's ID in the plot. }
Plot^.Stat[ N ] := NAttValue( Element^.NA , NAG_Personal , NAS_CID );
Fast_Seek_Element[ N ] := Element;
end else begin
{ No free NPCs were found. Bummer. }
OK := False;
end;
end else if EKind[1] = 'S' then begin
{ This element is a SCENE. Find one. }
{ Pick one of the free scenes at random. }
Element := SearchForScene( Adventure , Plot , GB , Desc );
if Element <> Nil then begin
{ Store the Scene ID in the plot. }
Plot^.Stat[ N ] := Element^.S;
Fast_Seek_Element[ N ] := Element;
end else begin
{ No free scenes were found. Bummer. }
OK := False;
end;
end else if EKind[1] = 'F' then begin
{ Faction element. }
NumElements := NumFreeFaction( Adventure , Plot , GB , Desc );
if NumElements > 0 then begin
{ Pick one of the free scenes at random. }
Element := FindFreeFaction( Adventure , Plot , GB , Desc , Random( NumElements ) + 1 );
Fast_Seek_Element[ N ] := Element;
{ Store the Scene ID in the plot. }
Plot^.Stat[ N ] := Element^.S;
end else begin
{ No free scenes were found. Bummer. }
OK := False;
end;
end else if EKind[1] = 'P' then begin
{ PreFab element. Check Plot/InvCom and }
{ retrieve it. }
Element := DeploySceneElement( GB , Adventure , Plot , N );
Fast_Seek_Element[ N ] := Element;
OK := Element <> Nil;
end;
end;
if Debug or ( GearName( Plot ) = 'DEBUG' ) then begin
if not OK then DialogMsg( 'PLOT ERROR: ' + BStr( N ) + ' Element Not Found!' )
else if desc <> '' then DialogMsg( 'PLOT ELEMENT ' + BStr( N ) + ': ' + BStr( Plot^.Stat[ N ] ) + ' ' + GearName( Element ) );
end;
FindElement := OK;
end;
Function ElementName( Adventure,Plot: GearPtr; N: Integer; GB: GameBoardPtr ): String;
{ Find the name of element N. Return an empty string if no such }
{ element can be found. }
var
Desc: String;
Part: GearPtr;
begin
Desc := UpCase( SAttValue( Plot^.SA , 'ELEMENT' + BStr( N ) ) );
if Desc <> '' then begin
Part := SeekPlotElement( Adventure, Plot, N , GB );
if Part <> Nil then begin
ElementName := GearName( Part );
end else begin
ElementName := '***ERROR***';
end;
end else begin
ElementName := '***NOT DEFINED***';
end;
end;
Procedure FormatRumorString( Adventure,Plot,RBase: GearPtr; GB: GameBoardPtr );
{ Make sure the rumor strings are properly formatted for this plot. }
{ RBase is the gear whose rumor we are currently interested in. }
{ Also, format rumor strings for all sub-components. }
var
R0,R1,W,C: String;
S: GearPtr;
N: Integer;
begin
{ Locate the rumor string. }
R0 := SAttValue( RBase^.SA , 'RUMOR' );
R1 := '';
{ Go through the rumor string replacing element name commands with }
{ the actual element names. }
if R0 <> '' then begin
while R0 <> '' do begin
W := ExtractWord( R0 );
{ If a string begins with !, it's to be replaced. }
if W[1] = '!' then begin
DeleteFirstChar( W );
C := W[1];
DeleteFirstChar( W );
N := ExtractValue( C );
W := ElementName( Adventure , Plot , N , GB ) + W;
end;
R1 := R1 + ' ' + W;
end;
DeleteWhiteSpace( R1 );
SetSAtt( RBase^.SA , 'RUMOR <' + R1 + '>' );
end;
{ Format the rumors of all sub and inv components. }
S := RBase^.SubCom;
while S <> Nil do begin
FormatRumorString( Adventure , Plot , S , GB );
S := S^.Next;
end;
S := RBase^.InvCom;
while S <> Nil do begin
FormatRumorString( Adventure , Plot , S , GB );
S := S^.Next;
end;
end;
Procedure InitPlot( Adventure,Plot: GearPtr; GB: GameBoardPtr );
{ Initialize this plot. }
{ Currently, this procedure has only one purpose- to format the }
{ rumor strings. }
{ GB can be NIL. }
begin
FormatRumorString( Adventure , Plot , Plot , GB );
end;
Function MatchPlotToAdventure( Slot,Plot: GearPtr; GB: GameBoardPtr; Debug: Boolean ): Boolean;
{ This PLOT gear is meant to be inserted into this ADVENTURE gear. }
{ Perform the insertion, select unselected elements, and make sure }
{ that everything fits. }
{ This procedure now also works for Stories. }
var
T: Integer;
E: STring;
Adventure,PFE: GearPtr; { Prefab Element }
EverythingOK,OKNow: Boolean;
begin
{ Error Check }
if ( Plot = Nil ) or ( Slot = Nil ) then Exit;
EverythingOK := True;
{ We need to stick the PLOT into the ADVENTURE to prevent }
{ the FindElement procedure from choosing the same item for }
{ multiple elements. }
InsertInvCom( Slot , Plot );
Adventure := FindRoot( Slot );
{ Select Actors }
{ First clear the FastSeek array. }
for t := 1 to NumGearStats do Fast_Seek_Element[ t ] := Nil;
for t := 1 to NumGearStats do begin
{ If we are inserting an adventure arc instead of a truly }
{ random plot, several of the elements may already have been }
{ assigned. The plot is OK if those elements exist & are OK. }
if ( Plot^.Stat[T] = 0 ) and EverythingOK then begin
OkNow := FindElement( Adventure , Plot , T , GB , Debug );
end else if EverythingOK then begin
Fast_Seek_Element[ T ] := SeekPlotElement( Adventure , Plot , T , GB );
OkNow := Fast_Seek_Element[ T ] <> Nil;
if Debug or ( GearName( Plot ) = 'DEBUG' ) then begin
if not OKNow then DialogMsg( 'PLOT ERROR: ' + GearName( Plot ) + BStr( T ) + ' Predefined Element ' + BStr( Plot^.Stat[t] ) + ' Not Found!' )
else DialogMsg( 'PLOT ELEMENT ' + BStr( T ) + ': ' + ElementName( Adventure , Plot , T , GB ) );
end;
end;
if Debug or ( GearName( Plot ) = 'DEBUG' ) then begin
DialogMsg( BStr( T ) + '=> ' + BStr( Plot^.Stat[ T ] ) );
end;
EverythingOK := EverythingOK and OKNow;
end;
if EverythingOK then begin
{ The plot has been successfully installed into the }
{ adventure. Initialize the stuff... rumor strings }
{ mostly. }
InitPlot( Adventure , Plot , GB );
end else begin
{ This plot won't fit in this adventure. Dispose of it. }
{ First get rid of any already-placed prefab elements. }
for t := 1 to NumGearStats do begin
E := SAttValue( Plot^.SA , 'ELEMENT' + BStr( T ) );
if AStringHasBString( E , 'PREFAB' ) then begin
PFE := SeekPlotElement( Adventure , Plot , T , GB );
if PFE <> Nil then begin
if IsSubCom( PFE ) then begin
RemoveGear( PFE^.Parent^.SubCom , PFE );
end else if IsInvCom( PFE ) then begin
RemoveGear( PFE^.Parent^.InvCom , PFE );
end;
end; {if PFE <> Nil}
end;
end;
RemoveGear( Plot^.Parent^.InvCom , Plot );
end;
MatchPlotToAdventure := EverythingOK;
end;
Function InsertPlot( Adventure,Plot: GearPtr; Debug: Boolean; GB: GameBoardPtr ): Boolean;
{ Stick PLOT into ADVENTURE, selecting Actors and Locations }
{ as required. If everything is found, insert PLOT as an InvCom }
{ of the Adventure. Otherwise, delete it. }
begin
InsertPlot := MatchPlotToAdventure( Adventure , Plot , GB , Debug );
end;
Function InsertStory( Slot,Story: GearPtr; GB: GameBoardPtr ): Boolean;
{ Stick STORY into SLOT, selecting Actors and Locations }
{ as required. If everything is found, insert STORY as an InvCom }
{ of the SLOT. Otherwise, delete it. }
begin
InsertStory := MatchPlotToAdventure( Slot , Story , GB , False );
end;
Function InsertGlobalArc( Adventure,ARC: GearPtr; GB: GameBoardPtr ): Boolean;
{ Stick ARC into ADVENTURE, selecting Actors and Locations }
{ as required. If everything is found, insert PLOT as an InvCom }
{ of the Adventure. Otherwise, delete it. }
var
T,N: Integer;
E,Plot: GearPtr;
begin
{ Step One - If there are any characters requested by this plot, }
{ check to see if they are currently involved in other plots. If so, }
{ remove those other plots from play. }
for t := 1 to NumGearStats do begin
if Arc^.Stat[ T ] <> 0 then begin
{ Clear the arc's stat for now, to keep it from }
{ being returned by SeekPlotElement. }
N := Arc^.Stat[ T ];
Arc^.Stat[ T ] := 0;
E := SeekPlotElement( Adventure , Arc , T , GB );
if ( E <> Nil ) and ( E^.G = GG_Character ) and ( NAttValue( E^.NA , NAG_Personal , NAS_CID ) <> 0 ) then begin
Plot := FindPersonaPlot( Adventure , NAttValue( E^.NA , NAG_Personal , NAS_CID ) );
if Plot <> Nil then RemoveGear( Plot^.Parent^.InvCom , Plot );
end;
Arc^.Stat[ T ] := N;
end;
end;
{ Step Two - Attempt to insert this plot into the adventure. }
InsertGlobalArc := MatchPlotToAdventure( Adventure , ARC , GB , False );
end;
Function InsertStoryArc( Story,Arc: GearPtr; GB: GameBoardPtr ): Boolean;
{ Stick ARC into Story, selecting Actors and Locations }
{ as required. If everything is found, insert PLOT as an InvCom }
{ of the Story. Otherwise, delete it. }
var
Desc: String;
T,N: Integer;