forked from jwvhewitt/gearhead-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arenascript.pp
4448 lines (3867 loc) · 139 KB
/
arenascript.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 arenascript;
{ This unit holds the scripting language for GearHead. }
{ It's pretty similar to the scripts developed for DeadCold. }
{ Basically, certain game events will add a trigger to the }
{ list. In the main combat procedure, if there are any pending }
{ triggers, they will be checked against the events list to }
{ see if anything happens. }
{ Both the triggers and the event scripts will be stored as }
{ normal string attributes. }
{ This unit also handles conversations with NPCs, since those }
{ are written using the scripting language and may use any }
{ commands available there + a few special commands. }
{
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
{$IFDEF SDLMODE}
uses gears,locale,sdlmenus,sdl,sdlgfx;
{$ELSE}
uses gears,locale,conmenus;
{$ENDIF}
const
NAG_ScriptVar = 0;
Max_Plots_Per_Story = 5;
var
{ This gear pointer will be created if a dynamic scene is requested. }
SCRIPT_DynamicEncounter: GearPtr;
{ **************************************** }
{ *** INTERACTION GLOBAL VARIABLES *** }
{ **************************************** }
{ These variables hold information that may be needed anywhere }
{ while interaction is taking place, but are undefined if }
{ interaction is not taking place. }
{ IntMenu should let procedures know whether or not interaction }
{ is currently happening or not- if IntMenu <> Nil, we're in the }
{ middle of a conversation and all other interaction variables }
{ should have good values. }
IntMenu: RPGMenuPtr; { Interaction Menu }
I_PC,I_NPC: GearPtr; { Pointers to the PC & NPC Chara gears }
I_Endurance: Integer; { How much of the PC's crap the NPC is }
{ willing to take. When it reaches 0, the NPC says goodbye. }
I_Rumors: SAttPtr; { List of rumors. }
Grabbed_Gear: GearPtr; { This gear can be acted upon by }
{ generic commands. }
lancemate_tactics_persona: GearPtr; { Persona for setting lancemate tactics. }
{$IFDEF SDLMODE}
Procedure MechaSelectionMenu( GB: GameBoardPtr; LList,PC: GearPtr; Z: DynamicRect );
{$ELSE}
Procedure MechaSelectionMenu( LList,PC: GearPtr; Z: Integer );
{$ENDIF}
Procedure BrowseMemoType( GB: GameBoardPtr; Tag: String );
Function Calculate_Threat_Points( Level,Percent: Integer ): LongInt;
Function ScriptValue( var Event: String; GB: GameBoardPtr; Scene: GearPtr ): LongInt;
Function AS_GetString( Source: GearPtr; Key: String ): String;
Function ScriptMessage( msg_label: String; GB: GameBoardPtr; Source: GearPtr ): String;
Procedure InvokeEvent( Event: String; GB: GameBoardPtr; Source: GearPtr; var Trigger: String );
Procedure AddLancemate( GB: GameBoardPtr; NPC: GearPtr );
Procedure RemoveLancemate( GB: GameBoardPtr; NPC: GearPtr );
Procedure HandleInteract( GB: GameBoardPtr; PC,NPC,Interact: GearPtr );
Function TriggerGearScript( GB: GameBoardPtr; Source: GearPtr; var Trigger: String ): Boolean;
Function CheckTriggerAlongPath( var T: String; GB: GameBoardPtr; Plot: GearPtr; CheckAll: Boolean ): Boolean;
Procedure HandleTriggers( GB: GameBoardPtr );
implementation
{$IFDEF SDLMODE}
uses action,arenacfe,ability,damage,gearutil,ghchars,ghparser,ghmodule,
ghprop,ghweapon,grabgear,interact,menugear,playwright,rpgdice,
services,texutil,ui4gh,wmonster,sdlinfo,sdlmap,backpack;
{$ELSE}
uses action,arenacfe,ability,damage,gearutil,ghchars,ghparser,ghmodule,
ghprop,ghweapon,grabgear,interact,menugear,playwright,rpgdice,backpack,
services,texutil,ui4gh,wmonster,congfx,coninfo,conmap,context;
{$ENDIF}
const
CMD_Chat = -2;
CMD_Join = -3;
CMD_Quit = -4;
Debug_On: Boolean = False;
var
script_macros,value_macros: SAttPtr;
NeedGC: Boolean;
{$IFDEF SDLMODE}
ASRD_InfoGear: GearPtr;
ASRD_GameBoard: GameBoardPtr;
ASRD_MemoMessage: String;
Procedure ArenaScriptReDraw;
{ Redraw the combat screen for some menu usage. }
begin
if ASRD_GameBoard <> Nil then SDLCombatDisplay( ASRD_GameBoard );
end;
Procedure MemoPageReDraw;
{ Redraw the combat screen for some menu usage. }
begin
if ASRD_GameBoard <> Nil then SDLCombatDisplay( ASRD_GameBoard );
SetupMemoDisplay;
GameMsg( ASRD_MemoMessage , ZONE_MemoText.GetRect() , InfoGreen );
end;
{$ENDIF}
{$IFDEF SDLMODE}
Procedure MechaSelectionMenu( GB: GameBoardPtr; LList,PC: GearPtr; Z: DynamicRect );
{$ELSE}
Procedure MechaSelectionMenu( LList,PC: GearPtr; Z: Integer );
{$ENDIF}
{ Create a menu by which the player can select a mecha to use. }
var
RPM: RPGMenuPtr;
msg: String;
M: GearPtr;
N: Integer;
begin
{ Step one - Create the menu. }
RPM := CreateRPGMenu( MenuItem , MenuSelect , Z );
RPM^.Mode := RPMNoCancel;
M := LList;
N := 1;
while M <> Nil do begin
if ( M^.G = GG_Mecha ) and ( NAttValue( M^.NA , NAG_Location , NAS_Team ) = NAV_DefPlayerTeam ) then begin
msg := FullGearName( M );
AddRPGMenuItem( RPM , msg , N );
end;
Inc( N );
M := M^.Next;
end;
if RPM^.NumItem <> 0 then begin
{$IFDEF SDLMODE}
ASRD_GameBoard := GB;
ASRD_InfoGear := PC;
N := SelectMenu( RPM , @ArenaScriptReDraw );
{$ELSE}
N := SelectMenu( RPM );
{$ENDIF}
if N <> -1 then begin
{ Find the gear that was selected, and set its }
{ pilot attribute. }
M := RetrieveGearSib( LList , N );
AssociatePilotMek( LList , PC , M );
{ Print an informative message for the user. }
DialogMsg( MsgString( 'SELECTMECHA_RESULT1' ) + GearName( M ) + MsgString( 'SELECTMECHA_RESULT2' ) );
end;
end else begin
DialogMsg( MsgString( 'SELECTMECHA_NoMeks' ) );
end;
DisposeRPGMenu( RPM );
end;
Procedure BrowseMemoType( GB: GameBoardPtr; Tag: String );
{ Create a list, then browse the memos based upon this }
{ TAG type. Possible options are MEMO, NEWS, and EMAIL. }
var
MemoList,M: SAttPtr;
Procedure CreateMemoList( Part: GearPtr; Tag: String );
{ Look through all gears in the structure recursively, }
{ looking for MEMO string attributes to store in our list. }
var
msg: String;
begin
while Part <> Nil do begin
if Part^.G <> GG_AbsolutelyNothing then begin
msg := SAttValue( Part^.SA , Tag );
if msg <> '' then StoreSAtt( MemoList , msg );
CreateMemoList( Part^.SubCom , Tag );
CreateMemoList( Part^.InvCom , Tag );
end;
Part := Part^.Next;
end;
end;
Procedure BrowseList;
{ Actually browse the created list. }
var
RPM: RPGMenuPtr;
N,D: Integer;
begin
if MemoList <> Nil then begin
SetupMemoDisplay;
RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_MemoMenu );
AddRPGMenuItem( RPM , MsgString( 'MEMO_Next' ) , 1 );
AddRPGMenuItem( RPM , MsgString( 'MEMO_Prev' ) , 2 );
AddRPGMenuKey( RPM , KeyMap[ KMC_East ].KCode , 1 );
AddRPGMenuKey( RPM , KeyMap[ KMC_West ].KCode , 2 );
AlphaKeyMenu( RPM );
RPM^.Mode := RPMNoCleanup;
N := 1;
repeat
M := RetrieveSAtt( MemoList , N );
{$IFDEF SDLMODE}
ASRD_InfoGear := Nil;
ASRD_GameBoard := GB;
ASRD_MemoMessage := M^.Info;
D := SelectMenu( RPM , @MemoPageRedraw );
{$ELSE}
GameMsg( M^.Info , ZONE_MemoText , InfoGreen );
D := SelectMenu( RPM );
{$ENDIF}
if D = 1 then begin
N := N + 1;
if N > NumSAtts( MemoList ) then N := 1;
end else if D = 2 then begin
N := N - 1;
if N < 1 then N := NumSAtts( MemoList );
end;
until D = -1;
DisposeSAtt( MemoList );
DisposeRPGMenu( RPM );
end;
end;
begin
{ Error check first - we need the GB and the scene for this. }
if ( GB = Nil ) or ( GB^.Scene = Nil ) then Exit;
MemoList := Nil;
CreateMemoList( FindRoot( GB^.Scene ) , Tag );
if MemoList = Nil then StoreSAtt( MemoList , ReplaceHash( MsgString( 'MEMO_None' ) , LowerCase( Tag ) ) );
BrowseList;
end;
Function YesNoMenu( GB: GameBoardPtr; Prompt,YesMsg,NoMsg: String ): Boolean;
{ This will open up a small window in the middle of the map }
{ display, then prompt the user for a choice. }
{ Return TRUE if the "yes" option was selected, or FALSE if }
{ the "no" option was selected. }
{ This function performs no screen cleanup. }
var
rpm: RPGMenuPtr;
N: Integer;
begin
RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_YesNoMenu );
AddRPGMenuItem( RPM , YesMsg , 1 );
AddRPGMenuItem( RPM , NoMsg , -1 );
RPM^.Mode := RPMNoCancel;
{$IFDEF SDLMODE}
ASRD_InfoGear := Nil;
ASRD_GameBoard := GB;
ASRD_MemoMessage := Prompt;
N := SelectMenu( RPM , @MemoPageRedraw );
{$ELSE}
SetupYesNoDisplay;
GameMsg( Prompt , ZONE_YesNoPrompt , InfoGreen );
N := SelectMenu( RPM );
{$ENDIF}
DisposeRPGMenu( RPM );
{ Do cleanup before branching. }
{$IFNDEF SDLMODE}
DisplayMap( GB );
{$ENDIF}
YesNoMenu := N <> -1;
end;
Function SceneName( GB: GameBoardPtr; ID: Integer ): String;
{ Find the name of the scene with the given ID. If no such }
{ scene can be found, return a value that should let the player }
{ know an error has been commited. }
var
Part: GearPtr;
begin
if ( GB = Nil ) or ( GB^.Scene = Nil ) then begin
SceneName := 'XXX';
end else begin
{ Look for the scene along the subcomponents of the }
{ adventure. This is to make sure we don't accidentally }
{ pick a dynamic scene with the right ID. }
Part := FindActualScene( GB , ID );
SceneName := GearName( Part );
end;
end;
Function FindRandomMekID( GB: GameBoardPtr; Team: Integer ): LongInt;
{ Locate a random mek belonging to TEAM. }
var
NumMeks,N,T,MID: Integer;
Mek: GearPtr;
begin
{ Start out by finding out how many meks belong to this team }
{ anyways. }
NumMeks := NumOperationalMasters( GB , Team );
MID := 0;
{ If there are actually members on this team, select one randomly. }
if NumMeks > 0 then begin
{ Decide what mek to take, and initialize the }
{ search variables. }
N := Random( NumMeks ) + 1;
T := 0;
Mek := GB^.Meks;
while Mek <> Nil do begin
if ( NAttValue( Mek^.NA , NAG_Location , NAS_Team ) = Team ) and GearOperational( Mek ) then begin
Inc( T );
if T = N then MID := NAttValue( Mek^.NA , NAG_EpisodeData , NAS_UID );
end;
Mek := Mek^.Next;
end;
end;
FindRandomMekID := MID;
end;
Function FindRandomPilotID( GB: GameBoardPtr; Team: Integer ): LongInt;
{ Locate a random pilot belonging to TEAM. }
var
NumMeks,N,T,MID: Integer;
Mek,P: GearPtr;
begin
{ Start out by finding out how many meks belong to this team }
{ anyways. }
NumMeks := NumOperationalMasters( GB , Team );
MID := 0;
{ If there are actually members on this team, select one randomly. }
if NumMeks > 0 then begin
{ Decide what mek to take, and initialize the }
{ search variables. }
N := Random( NumMeks ) + 1;
T := 0;
Mek := GB^.Meks;
while Mek <> Nil do begin
if ( NAttValue( Mek^.NA , NAG_Location , NAS_Team ) = Team ) and GearOperational( Mek ) then begin
Inc( T );
if T = N then begin
P := LocatePilot( Mek );
if P <> Nil then MID := NAttValue( P^.NA , NAG_EpisodeData , NAS_UID );
end;
end;
Mek := Mek^.Next;
end;
end;
FindRandomPilotID := MID;
end;
Function FindRootUID( GB: GameBoardPtr; ID: LongInt ): LongInt;
{ Find the ID of the root of the specified gear. }
var
Part: GearPtr;
begin
{ First, find the part being pointed to. }
Part := LocateMekByUID( GB , ID );
{ Locate its root. }
if Part <> Nil then begin
Part := FindRoot( Part );
{ Return the root's UID. }
FindRootUID := NAttValue( Part^.NA , NAG_EpisodeData , NAS_UID );
{ If there was an error locating the part, return 0. }
end else FindRootUID := 0;
end;
Function FindFacMem( ID: Integer; GB: GameBoardPtr; Scene: GearPtr ): Integer;
{ This function will return how many NPCs belong to the }
{ specified faction. }
Function WorkHorse( Part: GearPtr ): Integer;
{ This is the function which does all the real work. }
var
it: Integer;
begin
it := 0;
while Part <> Nil do begin
if ( Part^.G = GG_Character ) and ( NAttValue( Part^.NA , NAG_Personal , NAS_FactionID ) = ID ) then begin
Inc( It );
end;
if Part^.SubCom <> Nil then it := it + WorkHorse( Part^.SubCom );
if Part^.InvCom <> Nil then it := it + WorkHorse( Part^.InvCom );
Part := Part^.Next;
end;
WorkHorse := it;
end;
begin
Scene := FindRoot( Scene );
FindFacMem := WorkHorse( Scene ) + WorkHorse( GB^.Meks );
end;
Function FindFacScene( ID: Integer; Scene: GearPtr ): Integer;
{ This function will return how many Scenes belong to the }
{ specified faction. }
Function WorkHorse( Part: GearPtr ): Integer;
{ This is the function which does all the real work. }
var
it: Integer;
begin
it := 0;
while Part <> Nil do begin
if ( Part^.G = GG_Scene ) and ( NAttValue( Part^.NA , NAG_Personal , NAS_FactionID ) = ID ) then begin
Inc( It );
end;
if Part^.SubCom <> Nil then it := it + WorkHorse( Part^.SubCom );
if Part^.InvCom <> Nil then it := it + WorkHorse( Part^.InvCom );
Part := Part^.Next;
end;
WorkHorse := it;
end;
begin
Scene := FindRoot( Scene );
FindFacScene := WorkHorse( Scene );
end;
Function NumPCMeks( GB: GameBoardPtr ): Integer;
{ Return the number of mecha belonging to team 1. }
{ It doesn't matter if they're on the board or not, nor whether or }
{ not they are destroyed. }
var
M: GearPtr;
N: Integer;
begin
N := 0;
if GB <> Nil then begin
M := GB^.Meks;
while M <> Nil do begin
{ If this is a mecha, and it belongs to team 1, }
{ increment the counter. }
if ( M^.G = GG_Mecha ) and ( NAttValue( M^.NA , NAG_Location , NAS_Team ) = NAV_DefPlayerTeam ) then Inc( N );
M := M^.Next;
end;
end;
NumPCMeks := N;
end;
Function FindPCScale( GB: GameBoardPtr ): Integer;
{ Return the scale of the PC. Generally this will be 0 if the }
{ PC is on foot, 1 or 2 if the PC is in a mecha, unless the PC }
{ is a storm giant or a zentradi in which case anything goes. }
var
PC: GearPtr;
begin
PC := GG_LocatePC( GB );
if PC <> Nil then begin
FindPCScale := FindRoot( PC )^.Scale;
end else begin
FindPCScale := 0;
end;
end;
Function FindHostileFactions( GB: GameBoardPtr; Source: GearPtr ): Integer;
{ COunt the number of hostile factions present in this adventure. }
{ A hostile faction is defined as one which has the MILITARY type, }
{ which isn't INACTIVE, and which has ACTIVE MILITARY enemies. }
Function IsMilitaryFaction( Fac: GearPtr ): Boolean;
{ The faction is MILITARY if it says so in its TYPE string. }
begin
IsMilitaryFaction := AStringHasBString( SAttValue( Fac^.SA , 'TYPE' ) , 'MILITARY' );
end;
Function MeetsTheRequirements( Fac: GearPtr ): Boolean;
{ Return TRUE if the provided faction meets all the }
{ requirements listed above, or FALSE otherwise. }
var
it: Boolean;
F2: GearPtr;
NA: NAttPtr;
Enemies: Integer;
begin
it := IsMilitaryFaction( Fac ) and not FactionIsInactive( Fac );
if it then begin
{ Check through each of the faction's relations }
{ to see if it has any active, military enemies. }
NA := Fac^.NA;
Enemies := 0;
while NA <> Nil do begin
{ If this numeric attribute is a faction score, }
{ and it's an enemy, check the state of that enemy. }
if ( NA^.G = NAG_FactionScore ) and ( NA^.V < 0 ) then begin
F2 := GG_LocateFaction( NA^.S , GB , Source );
if F2 <> Nil then begin
if IsMilitaryFaction( F2 ) and not FactionIsInactive( F2 ) then Inc( Enemies );
end;
end;
NA := NA^.Next;
end;
{ If there are no active, military enemies then }
{ this faction doesn't count as currently hostile. }
if Enemies = 0 then it := False;
end;
MeetsTheRequirements := it;
end;
Function CheckAlongPath( Part: GearPtr ): Integer;
{ Check along this path of sibling gears, counting up }
{ all the factions that meet our requirements. }
var
it: Integer;
begin
{ Initialize count to 0. }
it := 0;
while Part <> Nil do begin
if ( Part^.G = GG_Faction ) and MeetsTheRequirements( Part ) then inc( it );
it := it + CheckAlongPath( Part^.SubCom );
it := it + CheckAlongPath( Part^.InvCom );
Part := Part^.Next;
end;
CheckAlongPath := it;
end;
begin
if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then begin
FindHostileFactions := CheckAlongPath( FindRoot( GB^.Scene ) );
end else begin
FindHostileFactions := CheckAlongPath( FindRoot( Source ) );
end;
end;
Function Calculate_Threat_Points( Level,Percent: Integer ): LongInt;
{ Calculate an appropriate threat value, based upon the modified }
{ encounter level ( usually PCRep + d50 ) and the % scale factor. }
var
it: LongInt;
begin
if Level < 0 then Level := 0
else if Level > 300 then Level := 300;
{ For low level encounters, use a linear equation. }
if Level < 42 then begin
it := 50 + Level * 125;
{ Higher on, switch to the quadratic. }
{ This equation provides similar values to the standard values }
{ previously used in the combat encounters. }
end else begin
it := 25 * Level * Level - 1850 * Level + 39000;
end;
{ Modify for the percent requested. }
it := it * Percent;
Calculate_Threat_Points := it;
end;
Function Calculate_Reward_Value( GB: GameBoardPtr; ThreatLevel,Percent: LongInt ): LongInt;
{ Return an appropriate reward value, based on the listed }
{ threat level and percent scale. }
var
RV: LongInt;
PC: GearPtr;
begin
{ Calculate the base reward value. }
RV := ThreatLevel div 100 * Percent div 100;
if RV < 100 then RV := 100;
{ Modify this for the PC's talents. }
if GB <> Nil then begin
PC := GG_LocatePC( GB );
if HasTalent( PC , NAS_BusinessSense ) then RV := ( RV * 5 ) div 4;
end;
Calculate_Reward_Value := RV;
end;
Procedure InitiateMacro( var Event: String; ProtoMacro: String );
{ Initialize the provided macro, and stick it to the head of }
{ event. To initialize just go through it word by word, replacing }
{ all question marks with words taken from EVENT. }
function NeededParameters( cmd: String ): Integer;
{ Return the number of parameters needed by this function. }
const
NumStandardFunctions = 15;
StdFunName: Array [1..NumStandardFunctions] of string = (
'-', 'GNATT', 'GSTAT', 'FACMEM', 'FACSCENE',
'SKROLL', 'THREAT', 'REWARD', 'ESCENE', 'RANGE',
'FXPNEEDED','WMTHREAT','*','MAPTILE','PCSKILLVAL'
);
StdFunParam: Array [1..NumStandardFunctions] of byte = (
1,2,1,1,1,1,2,2,1,2,
1,1,2,2,1
);
var
it,T: Integer;
Mac: String;
begin
it := 0;
CMD := UpCase( CMD );
{ If a hardwired function, look up the number of parameters }
{ from the above list. }
for t := 1 to NumStandardFunctions do begin
if CMD = StdFunName[ t ] then it := StdFunParam[ t ];
end;
{ If a macro function, derive the number of parameters }
{ from the number of ?s in the macro. }
if it = 0 then begin
Mac := SAttValue( Value_Macros , Cmd );
if Mac <> '' then begin
for t := 1 to Length( Mac ) do begin
if Mac[ T ] = '?' then Inc( it );
end;
end;
end;
NeededParameters := it;
end;
function GetSwapText: String;
{ Grab the swap text from EVENT. If it's a function }
{ that we just grabbed, better grab the parameters as }
{ well. Oh, bother... }
var
it,cmd: String;
N: Integer;
begin
{ Grab the beginning of the swap text. }
it := ExtractWord( Event );
{ Check to see if it's a function. Get rid of the - first }
{ since it'll screw up our check. }
cmd := it;
if ( Length( cmd ) > 1 ) and ( cmd[1] = '-' ) then DeleteFirstChar( cmd );
N := NeededParameters( cmd );
While N > 0 do begin
it := it + ' ' + GetSwapText();
Dec( N );
end;
GetSwapText := it;
end;
var
Cmd,NewMacro,LastWord: String;
begin
NewMacro := '';
LastWord := '';
while ProtoMacro <> '' do begin
cmd := ExtractWord( ProtoMacro );
if cmd = '?' then begin
LastWord := GetSwapText;
cmd := LastWord;
end else if cmd = '!' then begin
cmd := LastWord;
end;
NewMacro := NewMacro + ' ' + cmd;
end;
Event := NewMacro + ' ' + Event;
end;
Function SV_Range( GB: GameBoardPtr; UID1,UID2: LongInt ): Integer;
{ Locate the two gears pointed to by the UIDs, then calculate }
{ the range between them. If one or both are NIL, or if one or }
{ both are off the map, return 0. }
var
M1,M2: GearPtr;
begin
{ Error check. }
if GB = Nil then Exit( 0 );
M1 := LocateMekByUID( GB , UID1 );
M2 := LocateMekByUID( GB , UID2 );
if ( M1 <> Nil ) and OnTheMap( M1 ) and ( M2 <> Nil ) and OnTheMap( M2 ) then begin
SV_Range := Range( GB , M1 , M2 );
end else begin
SV_Range := 0;
end;
end;
Function SV_PCSkillVal( GB: GameBoardPtr; Skill: Integer ): Integer;
{ Return the PC's base skill value. This used to be easy until those }
{ stupid lancemates came along... Check all PCs and lancemates, and }
{ return the highest value. }
var
M,PC: GearPtr;
HiSkill,T: Integer;
begin
{ Error check. }
if GB = Nil then Exit( 0 );
M := GB^.Meks;
HiSkill := 0;
while M <> Nil do begin
T := NAttValue( M^.NA , NAG_Location , NAS_Team );
if GearActive( M ) and ( ( T = NAV_DefPlayerTeam ) or ( T = NAV_LancemateTeam ) ) then begin
PC := LocatePilot( M );
if PC <> Nil then begin
T := NAttValue( PC^.NA , NAG_Skill , SKill );
if T > HiSkill then HiSkill := T;
end;
end;
M := M^.Next;
end;
SV_PCSkillVal := HiSkill;
end;
Function ScriptValue( var Event: String; GB: GameBoardPtr; Scene: GearPtr ): LongInt;
{ Normally, numerical values will be stored as constants. }
{ Sometimes we may want to do algebra, or use the result of }
{ scenario variables as the parameters for commands. That's }
{ what this function is for. }
var
Old_Grabbed_Gear,PC: GearPtr;
VCode,VC2: LongInt;
SV: LongInt;
SMsg,S2: String;
begin
{ Save the grabbed gear, to restore it later. }
Old_Grabbed_Gear := Grabbed_Gear;
SMsg := UpCase(ExtractWord( Event ));
SV := 0;
{ If the first character is one of the value commands, }
{ process the string as appropriate. }
if SAttValue( Value_Macros , SMsg ) <> '' then begin
{ Install the macro, then re-call this procedure to get }
{ the results. }
InitiateMacro( Event , SAttValue( Value_Macros , SMsg ) );
SV := ScriptValue( Event , gb , scene );
end else if ( SMsg = 'GNATT' ) then begin
{ Get a Numeric Attribute from the currently grabbed gear. }
VCode := ScriptValue( Event , GB , Scene );
VC2 := ScriptValue( Event , GB , Scene );
if Grabbed_Gear <> Nil then begin
SV := NAttValue( Grabbed_Gear^.NA , VCode , VC2 );
end;
end else if ( SMsg = 'GSTAT' ) then begin
{ Get a Numeric Attribute from the currently grabbed gear. }
VCode := ScriptValue( Event , GB , Scene );
if ( Grabbed_Gear <> Nil ) then begin
if ( VCode >= 1 ) and ( VCode <= NumGearStats ) then begin
SV := Grabbed_Gear^.Stat[ VCode ];
end;
end;
end else if ( SMsg = 'FXPNEEDED' ) then begin
{ Return how many faction XP points needed for next level. }
VCode := ScriptValue( Event , GB , Scene );
SV := ( VCode + 1 ) * 5;
end else if ( SMsg = 'MAPTILE' ) then begin
{ Return how many faction XP points needed for next level. }
VCode := ScriptValue( Event , GB , Scene );
VC2 := ScriptValue( Event , GB , Scene );
if ( GB <> Nil ) and OnTheMap( VCode , VC2 ) then begin
SV := GB^.Map[ VCode , VC2 ].terr;
end else begin
SV := 0;
end;
end else if Attempt_Gear_Grab( SMsg , Event , GB , Scene ) then begin
{ The correct Grabbed_Gear was set by the above call, }
{ so just recurse to find the value we want. Am I looming }
{ closer to functional programming or something? }
SV := ScriptValue( Event , gb , scene );
end else if ( SMsg = 'COMTIME' ) then begin
{ Return the current combat time. }
if ( GB <> Nil ) then begin
SV := GB^.ComTime;
end;
end else if ( SMsg = 'NEXTDAY' ) then begin
{ Return the start of the next day. }
if ( GB <> Nil ) then begin
SV := GB^.ComTime + 86400 - GB^.ComTime mod 86400;
end;
end else if ( SMsg = 'PCSKILLVAL' ) then begin
{ Return the PC team's highest skill value. }
VCode := ScriptValue( Event , GB , Scene );
SV := SV_PCSkillVal( GB , VCode );
end else if ( SMsg = 'SCENEID' ) then begin
{ Return the current scene's unique ID. }
{ Only do this if we're in the real scene! Return 0 for }
{ a temporary or otherwise fake scene. }
if ( GB <> Nil ) and ( GB^.Scene <> Nil ) and IsSubCom( GB^.Scene ) then begin
SV := GB^.Scene^.S;
end;
end else if ( SMsg = 'GSCENE' ) then begin
{ Return the current scene's unique ID. }
{ Only do this if we're in the real scene! Return 0 for }
{ a temporary or otherwise fake scene. }
if ( GB <> Nil ) and ( GB^.Scene <> Nil ) and (Grabbed_Gear <> Nil) then begin
SV := FindGearScene( Grabbed_Gear, GB );
end;
end else if ( SMsg = 'FACMEM' ) then begin
{ Return the number of members of the requested faction. }
if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then begin
VCode := ScriptValue( Event , GB , Scene );
SV := FindFacMem( VCode , GB , GB^.Scene );
end;
end else if ( SMsg = 'FACSCENE' ) then begin
{ Return the number of scenes controlled by this faction. }
if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then begin
VCode := ScriptValue( Event , GB , Scene );
SV := FindFacScene( VCode , GB^.Scene );
end;
end else if ( SMsg = 'HOSTILEFACTIONS' ) then begin
{ Return the number of hostile factions in play. }
if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then begin
SV := FindHostileFactions( GB , Scene );
end;
end else if ( SMsg = 'REACT' ) then begin
{ Return the current reaction score between PC & NPC. }
if ( IntMenu <> Nil ) then begin
if GB = Nil then begin
SV := ReactionScore( Nil , I_PC , I_NPC );
end else begin
SV := ReactionScore( GB^.Scene , I_PC , I_NPC );
end;
end;
end else if ( SMsg = 'SKROLL' ) then begin
{ Return a skill roll from the PC. }
VCode := ScriptValue( Event , GB , Scene );
if ( VCode >= 1 ) and ( VCode <= NumSkill ) then begin
SV := RollStep( TeamSkill( GB , NAV_DefPlayerTeam , VCode ) );
end else SV := 0;
PC := GG_LocatePC( GB );
if PC <> Nil then DoleSkillExperience( PC , VCode , 5 );
end else if SMsg = 'PCMEKS' then begin
SV := NumPCMeks( GB );
end else if ( SMsg = 'THREAT' ) then begin
VCode := ScriptValue( Event , GB , Scene );
VC2 := ScriptValue( Event , GB , Scene );
SV := Calculate_Threat_Points( VCode , VC2 );
end else if ( SMsg = '*' ) then begin
VCode := ScriptValue( Event , GB , Scene );
VC2 := ScriptValue( Event , GB , Scene );
SV := VCode * VC2;
end else if ( SMsg = 'WMTHREAT' ) then begin
VCode := ScriptValue( Event , GB , Scene );
if VCode < 4 then VCOde := 4;
SV := VCode div 2;
end else if ( SMsg = 'REWARD' ) then begin
VCode := ScriptValue( Event , GB , Scene );
VC2 := ScriptValue( Event , GB , Scene );
SV := Calculate_Reward_Value( GB , VCode , VC2 );
end else if ( SMsg = 'RANGE' ) then begin
VCode := ScriptValue( Event , GB , Scene );
VC2 := ScriptValue( Event , GB , Scene );
SV := SV_Range( GB , VCode , VC2 );
end else if SMsg = 'PCSCALE' then begin
SV := FindPCScale( GB );
end else if ( SMsg = 'ESCENE' ) then begin
{ Find out what element to find the scene for. }
if ( Scene <> Nil ) then begin
VCode := ExtractValue( Event );
{ Find out what plot we're dealing with. }
Scene := PlotMaster( Scene );
if ( Scene <> Nil ) and ( VCode >= 1 ) and ( VCode <= NumGearStats ) then begin
SV := ElementLocation( FindRoot( Scene ) , Scene , VCode , GB );
end;
end;
end else if ( SMsg[1] = 'D' ) then begin
{ Roll a die, return the result. }
DeleteFirstChar( SMsg );
Event := SMsg + ' ' + Event;
VCode := ScriptValue( Event , GB , Scene );;
if VCode < 1 then VCode := 1;
SV := Random( VCode ) + 1;
{ As a last resort, see if the first character shows up in the }
{ scripts file. If so, use that. }
end else if SAttValue( Value_Macros , SMsg[1] ) <> '' then begin
{ Install the macro, then re-call this procedure to get }
{ the results. }
S2 := SMsg[ 1 ];
DeleteFirstChar( SMsg );
Event := SMsg + ' ' + Event;
InitiateMacro( Event , SAttValue( Value_Macros , S2 ) );
SV := ScriptValue( Event , gb , scene );
end else if ( SMsg[1] = '?' ) and ( gb <> Nil ) then begin
{ Return a randomly picked gear from the game board. }
DeleteFirstChar( SMsg );
if UpCase(SMsg[1]) = 'M' then begin
DeleteFirstChar( SMsg );
SV := FindRandomMekID( GB , ScriptValue( SMsg , gb , scene ) );
end else begin
DeleteFirstChar( SMsg );
SV := FindRandomPilotID( GB , ScriptValue( SMsg , gb , scene ) );
end;
end else if ( SMsg[1] = 'T' ) and ( gb <> Nil ) then begin
{ Return the number of gears on the provided team. }
DeleteFirstChar( SMsg );
VCode := ExtractValue( SMsg );
SV := NumActiveMasters( GB , VCode );
end else if ( SMsg[1] = '@' ) and ( gb <> Nil ) then begin
{ Return the root gear of the gear indicated by the }
{ rest of this expression. Return 0 if there's an error. }
DeleteFirstChar( SMsg );
VCode := ScriptValue( SMsg , gb , scene );
if VCode <> 0 then SV := FindRootUID( GB , VCode )
else SV := 0;
end else if SMsg[1] = '-' then begin
{ We want the negative of the value to follow. }
DeleteFirstChar( SMsg );
event := SMsg + ' ' + event;
SV := -ScriptValue( event , gb , scene );
end else begin
{ No command was given, so this must be a constant value. }
S2 := SMsg;
SV := ExtractValue( SMsg );
if ( SV = 0 ) and ( S2 <> '' ) and ( S2 <> '0' ) then begin
DialogMsg( 'WARNING: Script value ' + S2 + ' in ' + GearName( Scene ) );
DialogMsg( 'CONTEXT: ' + event );
end;
end;
{ Restore the grabbed gear before exiting. }
Grabbed_Gear := Old_Grabbed_Gear;
ScriptValue := SV;
end;
Function AS_GetString( Source: GearPtr; Key: String ): String;
{ Check the SOURCE for a SAtt with the provided KEY. }
{ If none can be found, search the default list for SOURCE's type. }
{ Normally, getting a string attribute could be handled simply by the }
{ SAttValue function. But, I had some trouble with my doors getting so }
{ #$#@%! huge, so I decided to write the function as a space-saver. }
var
msg: String;
begin
if Source <> Nil then begin
msg := SAttValue( Source^.SA , Key );
if ( msg = '' ) and ( Source^.G = GG_MetaTerrain ) and ( Source^.S >= 1 ) and ( Source^.S <= NumBasicMetaTerrain ) then begin
msg := SAttValue( Meta_Terrain_Scripts[ Source^.S ] , Key );