-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmpbuilder.pp
1873 lines (1698 loc) · 67.8 KB
/
mpbuilder.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 mpbuilder;
{ MEGA PLOT ASSEMBLE! It's like a Voltron of narrative content! }
{ This unit contains the functions and procedures for creating }
{ big amalgamations of components. }
{
GearHead2, a roguelike mecha CRPG
Copyright (C) 2005 Joseph Hewitt
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.
The full text of the LGPL can be found in license.txt.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
}
{$LONGSTRINGS ON}
interface
uses gears,locale,narration;
Type
ElementDesc = Record
EType: Char;
EValue: LongInt;
end;
{ I feel just like Dmitri Mendelev writing this... }
ElementTable = Array [1..Num_Plot_Elements] of ElementDesc;
var
Sub_Plot_List: GearPtr;
Procedure ReplaceStrings( Part: GearPtr; Dictionary: SAttPtr );
Function ComponentMenu( CList: GearPtr; var ShoppingList: NAttPtr ): GearPtr;
Procedure ClearElementTable( var ET: ElementTable );
Function ExpandDungeon( Dung: GearPtr ): GearPtr;
Procedure ConnectScene( Scene: GearPtr; DoInitExits: Boolean );
Function InitMegaPlot( GB: GameBoardPtr; Scope,Slot,Plot: GearPtr; Threat: Integer ): GearPtr;
Function LoadQuestFragments: GearPtr;
Function AddQuest( Adv,City,QPF_Proto: GearPtr; var Quest_Frags: GearPtr; QReq: String ): Boolean;
implementation
uses playwright,texutil,gearutil,gearparser,ghchars,randmaps,
ui4gh,wmonster,rpgdice,ghprop,ability,
{$IFDEF ASCII}
vidgfx,vidmenus;
{$ELSE}
sdlgfx,sdlmenus;
{$ENDIF}
Const
Num_Sub_Plots = 8;
Var
standard_trigger_list: SAttPtr;
changes_used_so_far: String;
MasterEntranceList: GearPtr;
Procedure ComponentMenuRedraw;
{ The redraw for the component selector below. }
begin
ClrScreen;
InfoBox( ZONE_Menu );
InfoBox( ZONE_Info );
InfoBox( ZONE_Caption );
GameMsg( 'Select the next component in the core story.', ZONE_Caption , StdWhite );
RedrawConsole;
end;
Function ComponentMenu( CList: GearPtr; var ShoppingList: NAttPtr ): GearPtr;
{ Select one of the components from a menu. }
var
RPM: RPGMenuPtr;
C: GearPtr;
N: Integer;
SL: NAttPtr;
begin
RPM := CreateRPGMenu( MenuItem, MenuSelect , ZONE_Menu );
AttachMenuDesc( RPM , ZONE_Info );
SL := ShoppingList;
while SL <> Nil do begin
C := RetrieveGearSib( CList , SL^.S );
AddRPGMenuItem( RPM , '[' + BStr( SL^.V ) + ']' + GearName( C ) , SL^.S , SAttValue( C^.SA , 'DESC' ) );
SL := SL^.Next;
end;
N := SelectMenu( RPM , @ComponentMenuRedraw );
SetNAtt( ShoppingList , 0 , N , 0 );
DisposeRPGMenu( RPM );
ComponentMenu := RetrieveGearSib( CList , N );
end;
Function NewPlotID( Adv: GearPtr; IsAQuest: Boolean ): LongInt;
{ Calculate a new unique Plot ID. }
{ Plots have positive PlotIDs, quests have negative PlotIDs. }
begin
{ Increase the previous ID by one, and return that. }
AddNAtt( Adv^.NA , NAG_Narrative , NAS_MaxPlotID , 1 );
if IsAQuest then begin
NewPlotID := -NAttValue( Adv^.NA , NAG_Narrative , NAS_MaxPlotID );
end else begin
NewPlotID := NAttValue( Adv^.NA , NAG_Narrative , NAS_MaxPlotID );
end;
end;
Function NewLayerID( Slot: GearPtr ): LongInt;
{ Calculate a new unique Layer ID. }
begin
{ Increase the previous ID by one, and return that. }
AddNAtt( Slot^.NA , NAG_Narrative , NAS_MaxPlotLayer , 1 );
NewLayerID := NAttValue( Slot^.NA , NAG_Narrative , NAS_MaxPlotLayer );
end;
Procedure ClearElementTable( var ET: ElementTable );
{ Clear this table's stored elements by setting all IDs }
{ to zero. }
var
t: Integer;
begin
for t := 1 to Num_Plot_Elements do begin
ET[t].EValue := 0;
end;
end;
Procedure CreatePlotPlaceholder( Slot , Shard: GearPtr );
{ Create a new plot gear to hold all of SHARD's elements, and store it }
{ in SLOT. IT will be marked with a PlotLayer of -(plotlayer), and this marking }
{ will be used to dispose of all the placeholders after assembly. }
var
it: GearPtr;
T: Integer;
EID: LongInt;
begin
it := NewGear( Slot );
InsertInvCom( Slot , It );
it^.G := GG_Plot;
SetSAtt( it^.SA , 'name <Placeholder>' );
SetNAtt( it^.NA , NAG_Narrative , NAS_PlotLayer , -NAttValue( Shard^.NA , NAG_Narrative , NAS_PlotLayer ) );
for t := 1 to Num_Plot_Elements do begin
EID := ElementID( Shard , T );
if EID <> 0 then begin
SetNAtt( It^.NA , NAG_ElementID , T , EID );
SetSAtt( It^.SA , 'ELEMENT' + BStr( T ) + ' <' + SAttValue( Shard^.SA , 'ELEMENT' + BStr( T ) ) + '>' );
end;
end;
end;
Procedure DeleteSpecificPlaceholder( Slot,Shard: GearPtr );
{ Delete the placeholder belonging specifically to this shard. }
var
p,p2: GearPtr;
plotid: LongInt;
begin
plotid := -NAttValue( Shard^.NA , NAG_Narrative , NAS_PlotLayer );
P := Slot^.InvCom;
while P <> Nil do begin
P2 := P^.Next;
if NAttValue( P^.NA , NAG_Narrative , NAS_PlotLayer ) = PlotID then begin
RemoveGear( Slot^.InvCom , P );
end;
P := P2;
end;
end;
Procedure DeletePlotPlaceholders( Slot: GearPtr );
{ Delete all the plot placeholders. They should be invcoms of SLOT, }
{ and be marked with negative PlotLayers. }
var
PP,PP2: GearPtr;
begin
PP := Slot^.InvCom;
while PP <> Nil do begin
PP2 := PP^.Next;
if NAttValue( PP^.NA , NAG_Narrative , NAS_PlotLayer ) < 0 then RemoveGear( Slot^.InvCom , PP );
PP := PP2;
end;
end;
Function AddSubPlot( GB: GameBoardPtr; Scope,Slot,Plot0,QPF_Proto: GearPtr; var Quest_Frags: GearPtr; SPReq: String; EsSoFar, LayerID, SubPlotSlot: LongInt; IsAQuest,DoDebug: Boolean ): GearPtr; forward;
Function InitShard( GB: GameBoardPtr; Scope,Slot,Shard: GearPtr; var Quest_Frags: GearPtr; EsSoFar,PlotID,LayerID,Threat: LongInt; const ParamIn: ElementTable; IsAQuest,DoDebug: Boolean ): GearPtr;
{ SHARD is a plot fragment candidate. Attempt to add it to the Slot. }
{ Attempt to add its subplots as well. }
{ SHARD can only be added if its number of new elements plus the current }
{ element total is less than the number of total possible elements. }
{ EsSoFar is the number of elements allocated so far. }
{ Before initializing a shard, the following will be done: }
{ - Parameter elements copied over }
{ - Any character gears present will be randomized }
{ Initializing includes the following: }
{ - Set combatant skill levels for quests }
{ Upon successfully initializing a shard, this procedure will then do the following: }
{ - Delink the shard from the Slot, and attach all subplots. }
{ - Create a plot stub and mark it with the PlotID; copy over all elements used by }
{ this shard and place it as Slot's invcom. This stub is to prevent other shards }
{ from selecting characters or items used here. }
{ - Initialize quest metascenes with the PlotID. }
{ - Return the shard list }
{ If installation fails, SHARD will be deleted and NIL will be returned. }
Procedure DisposeSPList( SPList: GearPtr );
{ Delete this subplot list, taking with it any associated placeholders. }
var
SP: GearPtr;
begin
while SPList <> Nil do begin
DeleteSpecificPlaceholder( Slot , SPList );
SP := SPList;
RemoveGear( SPList , SP );
end;
end;
Procedure ScaleRandomTreasure( LList: GearPtr );
{ Scale any random loot values found along this path. }
{ The treasures found as part of a quest should be }
{ commesurate with the Difficulty rating of the quest. }
var
LootValue: LongInt;
begin
while LList <> Nil do begin
if NAttValue( LList^.NA , NAG_Narrative , NAS_RandomLoot ) > 0 then begin
LootValue := Calculate_Threat_Points( Threat , 10 + Random( 15 ) );
SetNAtt( LList^.NA , NAG_Narrative , NAS_RandomLoot , LootValue );
end;
ScaleRandomTreasure( LList^.SubCom );
ScaleRandomTreasure( LList^.InvCom );
LList := LList^.Next;
end;
end;
Procedure PrepQuestCombatants( LList: GearPtr );
{ If this is a quest, scale any combatant NPCs to the proper level. }
begin
while LList <> Nil do begin
if ( LList^.G = GG_Character ) then begin
if NotAnAnimal( Llist ) and IsACombatant( LList ) then begin
SetSkillsAtLevel( LList , Threat );
end;
end;
LList := LList^.Next;
end;
end;
Procedure InitializeMapFeatures( LList: GearPtr );
{ Mark all map features, their subs and invs, with the PlotID of the }
{ parent scene. Why do this? So scripts can then locate the quest }
{ without too much difficulty. }
begin
while LList <> Nil do begin
if NAttValue( LList^.NA , NAG_Narrative , NAS_PlotID ) = 0 then SetNAtt( LList^.NA , NAG_Narrative , NAS_PlotID , PlotID );
if LList^.G = GG_MapFeature then begin
InitializeMapFeatures( LList^.SubCom );
InitializeMapFeatures( LList^.InvCom );
end;
LList := LList^.Next;
end;
end;
Procedure PrepQuestMetascenes( LList: GearPtr );
{ If this is a quest, mark the map features. }
begin
while LList <> Nil do begin
if ( LList^.G = GG_MetaScene ) then begin
{ Also mark all of the scene's things with the PlotID, so scripts can locate }
{ the quest easily. }
InitializeMapFeatures( LList^.SubCom );
InitializeMapFeatures( LList^.InvCom );
end;
LList := LList^.Next;
end;
end;
var
InitOK: Boolean;
T,NumParam,NumElem: Integer;
I,SubPlot,SPList: GearPtr;
SPID: LongInt;
SPReq,original_changes,EDesc: String;
begin
{ Assign the values to this shard. }
SetNAtt( Shard^.NA , NAG_Narrative , NAS_PlotID , PlotID );
SetNAtt( Shard^.NA , NAG_Narrative , NAS_PlotLayer , LayerID );
SetNAtt( Shard^.NA , NAG_Narrative , NAS_DifficultyLevel , Threat );
{ Scale the random treasure, based upon the threat value provided. }
ScaleRandomTreasure( Shard^.SubCom );
ScaleRandomTreasure( Shard^.InvCom );
{ Record the original changes_used_so_far string; if things go sour in this procedure, }
{ we're going to have to restore it. }
original_changes := changes_used_so_far;
{ Add the changes from this shard. }
SPReq := SAttValue( Shard^.SA , 'CHANGES' );
if SPReq <> '' then AddToQuoteString( changes_used_so_far , SPReq );
{ Start by copying over all provided parameters. }
{ Also count the number of parameters passed; it could be useful. }
NumParam := 0;
for t := 1 to Num_Plot_Elements do begin
if ParamIn[ t ].EValue <> 0 then begin
SetNAtt( Shard^.NA , NAG_ElementID , T , ParamIn[ t ].EValue );
SetSAtt( Shard^.SA , 'ELEMENT' + BStr( T ) + ' <' + ParamIn[ t ].EType + '>' );
Inc( NumParam );
end;
end;
{ If this is a quest, check the remaining parameters for artifacts. }
if IsAQuest then begin
for t := ( NumParam + 1 ) to Num_Plot_Elements do begin
EDesc := UpCase( SAttValue( Shard^.SA , 'ELEMENT' + BStr( T ) ) );
if ( EDesc <> '' ) and ( EDesc[1] = 'A' ) then begin
{ This is an artifact request. If no difficulcy context has been }
{ defined, add one ourselves. }
if not AStringHasBString( EDesc , '!' ) then begin
EDesc := EDesc + ' ' + DifficulcyContext( Threat );
SetSAtt( Shard^.SA , 'ELEMENT' + BStr( T ) + ' <' + EDesc + '>' );
end;
end;
end;
end;
{ Next, randomize the NPCs. }
{ Only do this is we aren't constructing a quest; if we are, then the }
{ NPCs have already been individualized. }
if not IsAQuest then begin
I := Shard^.InvCom;
while I <> Nil do begin
{ Character gears have to be individualized. }
if ( I^.G = GG_Character ) and NotAnAnimal( I ) then begin
IndividualizeNPC( I );
end;
I := I^.Next;
end;
end;
{ Initialize the subplot list to prevent trouble later on. }
SPList := Nil;
{ Attempt the basic content insertion routine. }
if DoDebug then SetSAtt( Shard^.SA , 'name <DEBUG>' );
InitOK := InsertSubPlot( Scope, Slot, Shard , GB );
{ If the installation has gone well so far, time to move on. }
if InitOK then begin
{ Count the number of unique elements. If more elements have been }
{ defined than will fit in a single plot, then loading of this subplot }
{ will fail. }
NumElem := 0;
for t := 1 to Num_Plot_Elements do begin
if NAttValue( Shard^.NA , NAG_ElementID , T ) <> 0 then begin
Inc( NumElem );
end;
end;
NumElem := NumElem - NumParam + EsSoFar;
if NumElem <= Num_Plot_Elements then begin
{ We have room for the elements. Good. Now move on by installing the subplots. }
{ Initialize the prefab NPCs for a quest. }
if IsAQuest then begin
PrepQuestCombatants( Shard^.InvCom );
PrepQuestMetascenes( Shard^.SubCom );
end;
{ If any of the needed subplots fail, installation of this shard fails }
{ as well. }
{ Arena missions may not request subplots. Sorry, that's just how it is. }
if Shard^.G <> GG_Scene then begin
for t := 1 to Num_Sub_Plots do begin
SPReq := SAttValue( Shard^.SA , 'SUBPLOT' + BStr( T ) );
if SPReq <> '' then begin
SPID := NewLayerID( Slot );
SubPlot := AddSubPlot( GB , Scope , Slot , Shard , Nil , Quest_Frags , SPReq , NumElem , SPID , T , IsAQuest, DoDebug );
if SubPlot <> Nil then begin
{ A subplot was correctly installed. Add it to the list. }
AppendGear( SPList , SubPlot );
NumElem := NumElem + NAttValue( SubPlot^.NA , NAG_Narrative , NAS_NumSPElementsUsed );
end else begin
{ The subplot request failed, meaning that this shard fails }
{ as well. }
InitOK := False;
RemoveGear( Slot^.InvCom , Shard );
Break;
end;
end;
end;
end;
end else begin
{ We have too many elements to merge back into the main plot. }
if XXRan_Debug then begin
DialogMsg( 'ERROR: ' + GearName( Shard ) + ' has too many elements: ' + BStr( NumElem ) + ' / ' + BStr( EsSoFar ) );
for t := 1 to Num_Plot_Elements do begin
DialogMsg( ' ' + BStr( T ) + ' ' + SAttValue( Shard^.SA , 'NAME_' + BStr( T ) ) + ' ' + BStr( ElementID( Shard , T ) ) );
end;
end;
InitOk := False;
RemoveGear( Slot^.InvCom , Shard );
end;
end;
{ Return our result. }
if InitOk then begin
{ Delink the shard. }
DelinkGear( Slot^.InvCom , Shard );
{ Create the plot placeholder stub, to prevent characters from being }
{ selected by different parts of the same superplot. }
CreatePlotPlaceholder( Slot , Shard );
{ Append the SPList to the shard. }
AppendGear( Shard , SPList );
SetNAtt( Shard^.NA , NAG_Narrative , NAS_NumSPElementsUsed , NumElem - EsSoFar );
InitShard := Shard;
end else begin
{ Initialization failed. Delete the existing subplots and restore the }
{ changes_used_so_far list. }
changes_used_so_far := original_changes;
DisposeSPList( SPList );
InitShard := Nil;
end;
end;
Function QuestIsReusable( Q: GearPtr ): Boolean;
{ Return TRUE if this quest is reusable, or FALSE otherwise. }
begin
QuestIsReusable := AStringHasBString( SAttValue( Q^.SA , 'SPECIAL' ) , 'REUSABLE' );
end;
Function AddSubPlot( GB: GameBoardPtr; Scope,Slot,Plot0,QPF_Proto: GearPtr; var Quest_Frags: GearPtr; SPReq: String; EsSoFar, LayerID, SubPlotSlot: LongInt; IsAQuest,DoDebug: Boolean ): GearPtr;
{ A request has been issued for a subplot. Search through the plot }
{ component list and see if there's anything that matches our criteria. }
{ Plot0 = the plot requesting the subplot. If this is a quest it may be }
{ nil, but don't you dare try pulling that crap otherwise. }
{ QPF_PROTO is a prototype of a prefab element to be inserted into a quest. }
{ QUEST_FRAGS contains a list of quest fragents. If constructing a quest, these may be used }
{ in addition to the regular megaplot comps. Once a quest fragment is used, it cannot usually }
{ be used again. }
Function CreateSubPlotList( const CReq: String ): NAttPtr;
{ Create the list of legal subplots. Start with the regular ones, }
{ then if appropriate add the not-in-use quest fragments. }
{ Regular subplots will be added as positive G,S. }
{ Quest fragments will be added as negative G,S. }
var
CList: NAttPtr;
N,MW: Integer;
C: GearPtr;
begin
{ Start by adding the standard components. }
CList := CreateComponentList( Sub_Plot_List , CReq );
{ Next, add the quest fragments. }
if IsAQuest and ( Quest_Frags <> Nil ) then begin
N := -1;
C := Quest_Frags;
while C <> nil do begin
if NAttValue( C^.NA , NAG_Narrative , NAS_QuestInUse ) = 0 then begin
MW := StringMatchWeight( CReq , SAttValue( C^.SA , 'REQUIRES' ) );
if MW > 0 then begin
SetNAtt( CList , N , N , MW );
end;
end;
Dec( N );
C := C^.Next;
end;
end;
CreateSubPlotList := CList;
end;
Function SelectNextSubPlot( var ShoppingList: NAttPtr ): GearPtr;
{ Select a new subplot prototype, and return a pointer to it. }
{ If a quest fragment is selected, mark it as used. }
{ Previously, we'd mark a quest fragment as used when it was selected and }
{ unmark it if the quest initialization failed. This time around it'll stay }
{ marked for the duration of this quest; if an earlier component can't }
{ initialize the fragment, chances are good that a later component in the }
{ same quest won't be able to either. }
var
it: NAttPtr;
N: Integer;
Proto: GearPtr;
begin
{ Select one of the components, and delete its entry from the }
{ shopping list. }
it := RandomComponentListEntry( ShoppingList );
N := it^.S;
RemoveNAtt( ShoppingList , it );
if N < 0 then begin
Proto := RetrieveGearSib( quest_frags , Abs( N ) );
if not QuestIsReusable( Proto ) then SetNAtt( Proto^.NA , NAG_Narrative , NAS_QuestInUse , 1 );
end else begin
Proto := RetrieveGearSib( sub_plot_list , N );
end;
SelectNextSubPlot := Proto;
end;
var
ShoppingList: NAttPtr;
Context,EDesc,SPContext,changes_list: String;
ParamList: ElementTable;
T,E,Threat: Integer;
Shard,QPF_Clone: GearPtr;
NotFoundMatch: Boolean;
PlotID: LongInt;
IsBranchPlot: Boolean;
begin
{ First determine the context. }
Context := ExtractWord( SPReq );
DeleteWhiteSpace( SPReq );
{ Determine the difficulty rating of this subplot. }
if ( SPReq <> '' ) and ( SPReq[1] = '#' ) then begin
DeleteFirstChar( SPReq );
T := ExtractValue( SPReq );
if T > 0 then Threat := T;
end else if ( Plot0 <> Nil ) then begin
threat := NAttValue( Plot0^.NA , NAG_Narrative , NAS_DifficultyLevel );
end else begin
threat := 10;
end;
{ Next complete the context. }
if Slot^.G = GG_Story then Context := Context + ' ' + StoryContext( GB , Slot );
if IsAQuest then Context := Context + ' ' + QuoteString( SceneContext( Nil , Scope ) ) + ' ' + DifficulcyContext( Threat );
if Plot0 <> Nil then begin
SPContext := SAttValue( Plot0^.SA , 'SPContext' );
if SPContext <> '' then Context := Context + ' ' + SPContext;
end else begin
SPContext := '';
end;
{ Determine whether this is a regular subplot or a branch plot that will start its own narrative thread. }
IsBranchPlot := ( Length( Context ) > 2 ) and ( Context[2] = ':' );
{ This will determine whether we inherit the PlotID from Plot0, or generate a new one. }
if IsBranchPlot or ( Plot0 = Nil ) then PlotID := NewPlotID( FindRoot( Slot ) , IsAQuest )
else PlotID := NAttValue( Plot0^.NA , NAG_Narrative , NAS_PlotID );
{ Store the details for this subplot in Plot0. }
if Plot0 <> Nil then begin
SetNAtt( Plot0^.NA , NAG_SubPlotLayerID , SubPlotSlot , LayerID );
SetNAtt( Plot0^.NA , NAG_SubPlotPlotID , SubPlotSlot , PlotID );
{ Determine the parameters to be sent, and add context info for them. }
{ We only need parameters if Plot0 = Nil, since root quests take no params. }
ClearElementTable( ParamList );
T := 1;
while ( SPReq <> '' ) and ( T <= Num_Plot_Elements ) do begin
E := ExtractValue( SPReq );
if ( E >= 1 ) and ( E <= Num_Plot_Elements ) then begin
{ This element is being shared with the subplot. }
ParamList[t].EValue := ElementID( Plot0 , E );
EDesc := SAttValue( Plot0^.SA , 'ELEMENT' + BStr( E ) );
if EDesc <> '' then ParamList[t].EType := EDesc[1];
AddElementContext( GB , Plot0 , Context , BStr( T )[1] , E );
Inc( T );
end;
end;
end else begin
{ We have no parameters to send. Clear the param list. }
ClearElementTable( ParamList );
end;
{ We have the context. Create the shopping list. }
{ Positive component values are from the main subplot list. Negative }
{ component values point to items from the quest_frags list. }
ShoppingList := CreateSubPlotList( Context );
if XXRan_Debug and ( Slot^.G = GG_Story ) then begin
if NumNAtts( ShoppingList ) < 5 then begin
DialogMsg( '[DEBUG] Only ' + BStr( NumNatts( ShoppingList ) ) + ' components for "' + Context + '".' );
end;
end;
{ Based on this shopping list, search for applocable subplots and attempt to }
{ fit them into the adventure. }
NotFoundMatch := True;
Shard := Nil;
while ( ShoppingList <> Nil ) and NotFoundMatch do begin
if XXRan_Wizard and ( ShoppingList <> Nil ) and ( Slot^.G = GG_Story ) and not IsAQuest then begin
{ DialogMsg( Context );}
Shard := CloneGear( ComponentMenu( Sub_Plot_List , ShoppingList ) );
end else if DoDebug and not IsAQuest then begin
DialogMsg( Context );
Shard := CloneGear( ComponentMenu( Sub_Plot_List , ShoppingList ) );
end else begin
Shard := CloneGear( SelectNextSubPlot( ShoppingList ) );
end;
if Shard <> Nil then begin
{ Make sure this candidate doesn't violate our changes_used_so_far list. }
changes_list := SAttValue( Shard^.SA , 'CHANGES' );
if ( changes_list = '' ) or NoQItemsMatch( changes_used_so_far , changes_list ) then begin
{ Insert the QPF gear, if appropriate. This is naively appended to the }
{ invcoms, so the Element ID must be the last requested prefab. }
if QPF_Proto <> Nil then begin
QPF_Clone := CloneGear( QPF_Proto );
InsertInvCom( Shard , QPF_Clone );
end;
{ See if we can add this one to the list. If not, it will be }
{ deleted by InitShard. }
if SPContext <> '' then SetSAtt( Shard^.SA , 'SPCONTEXT <' + SPContext + '>' );
Shard := InitShard( GB , Scope , Slot , Shard , Quest_Frags , EsSoFar , PlotID , LayerID , Threat , ParamList , IsAQuest , DoDebug );
if Shard <> Nil then NotFoundMatch := False;
end else begin
{ This shard wants to change something we've already changed elsewhere }
{ in this plot. Better not include it; things could get weird. }
DisposeGear( Shard );
end;
end;
end;
{ Get rid of the shopping list. }
DisposeNAtt( ShoppingList );
{ Return our selected subplot. }
AddSubPlot := Shard;
end;
Procedure ReplaceStrings( Part: GearPtr; Dictionary: SAttPtr );
{ We have a dictionary of substitute strings, and a part to do the replacements on. }
var
S: SAttPtr;
begin
S := Part^.SA;
while S <> Nil do begin
ApplyDictionaryToString( S^.Info , Dictionary );
S := S^.Next;
end;
end;
Procedure InitListStrings( LList: GearPtr; Dictionary: SAttPtr );
{ Run LList, all of its siblings and children, through the ReplaceStrings }
{ procedure. }
begin
while LList <> Nil do begin
ReplaceStrings( LList , Dictionary );
InitListStrings( LList^.SubCom , Dictionary );
InitListStrings( LList^.InvCom , Dictionary );
LList := LList^.Next;
end;
end;
Procedure MergeElementLists( MasterPlot , SubPlot: GearPtr );
{ The element list of SUBPLOT should be merged into MASTERPLOT. }
{ If a SUBPLOT element is found in MASTERPLOT already, no need }
{ to merge. Store the master plot element indicies in SubPlot. }
{ Also copy the PLACE strings here. }
var
FirstFreeSlot,T,PlotIndex: Integer;
EID: LongInt;
EDesc: String;
Dictionary: SAttPtr;
begin
{ Locate the first free slot in MasterPlot. }
FirstFreeSlot := 1;
While ( FirstFreeSlot <= Num_Plot_Elements ) and ( ElementID( MasterPlot , FirstFreeSlot ) <> 0 ) do Inc( FirstFreeSlot );
Dictionary := Nil;
{ Go through the elements of SubPlot. Check to see if they are found in }
{ MasterPlot. If so, do nothing. If not, add them. }
for T := 1 to Num_Plot_Elements do begin
EID := ElementID( SubPlot , T );
if EID <> 0 then begin
EDesc := SAttValue( SubPlot^.SA , 'ELEMENT' + BStr( T ) );
PlotIndex := PlotElementID( MasterPlot , EDesc[1] , EID );
if PlotIndex = 0 then begin
{ This element apparently doesn't currently have a }
{ place in this plot. Add it. }
PlotIndex := FirstFreeSlot;
Inc( FirstFreeSlot );
SetNAtt( MasterPlot^.NA , NAG_ElementID , PlotIndex , EID );
SetSAtt( MasterPlot^.SA , 'ELEMENT' + BStr( PlotIndex ) + ' <' + EDesc + '>' );
SetSAtt( MasterPlot^.SA , 'TEAM' + BStr( PlotIndex ) + ' <' + SAttValue( SubPlot^.SA , 'TEAM' + BStr( T ) ) + '>' );
end;
{ We should now have a working PlotIndex. Save it in SubPlot, }
{ and copy over the PLACE to MasterPlot. }
SetNAtt( SubPlot^.NA , NAG_MasterPlotElementIndex , T , PlotIndex );
SetSAtt( Dictionary , '%e' + BStr( T ) + '% <' + BStr( PlotIndex ) + '>' );
end;
end;
InitListStrings( SubPlot , Dictionary );
DisposeSAtt( Dictionary );
{ After initializing the strings, do one more loop to copy over the PLACE info. }
for T := 1 to Num_Plot_Elements do begin
EID := ElementID( SubPlot , T );
if EID <> 0 then begin
PlotIndex := NAttValue( SubPlot^.NA , NAG_MasterPlotElementIndex , T );
EDesc := SAttValue( SubPlot^.SA , 'PLACE' + BStr( T ) );
if EDesc <> '' then SetSAtt( MasterPlot^.SA , 'PLACE' + BStr( PlotIndex ) + ' <' + EDesc + '>' );
end;
end;
end;
Procedure MergePersona( MainPlot , SubPlot , Persona: GearPtr; IsAQuest: Boolean );
{ We have a persona that needs to be merged into the main plot. }
{ If the main plot already has a persona for this character, merge }
{ this new persona in as a megalist. If no persona currently exists, }
{ delink this persona from MainPlot and stick in SubPlot. }
var
MPIndex: Integer;
MainPersona: GearPtr;
begin
{ Determine the index of this element in the main plot. }
MPIndex := NAttValue( SubPlot^.NA , NAG_MasterPlotElementIndex , Persona^.S );
{ Attempt to locate the main persona. }
MainPersona := SeekCurrentLevelGear( MainPlot^.SubCom , GG_Persona , MPIndex );
if MainPersona = Nil then begin
{ No main persona- create one. }
if IsAQuest then begin
MainPersona := LoadNewSTC( 'PERSONA_BLANK' );
end else begin
MainPersona := LoadNewSTC( 'PERSONA_REVERT' );
end;
InsertSubCom( MainPlot , MainPersona );
SetSAtt( MainPersona^.SA , 'SPECIAL <' + SAttValue( Persona^.SA , 'SPECIAL' ) + '>' );
{ Store the PlotID of this layer, since it's the first to provide a persona for this NPC. }
SetNAtt( MainPersona^.NA , NAG_Narrative , NAS_PlotID , NAttValue( SubPlot^.NA , NAG_Narrative , NAS_PlotID ) );
MainPersona^.S := MPIndex;
end;
{ Combine the two plots together. }
BuildMegalist( MainPersona , Persona^.SA );
end;
Procedure MergeMetascene( MainPlot , SubPlot , MS: GearPtr );
{ Combine the sub-metascene with the main metascene. }
{ If no main metascene exists, simply move and relabel the }
{ one provided here. }
var
MPIndex: Integer;
MainScene,Thing: GearPtr;
begin
{ Determine the index of this element in the main plot. }
MPIndex := NAttValue( SubPlot^.NA , NAG_MasterPlotElementIndex , MS^.S );
{ Attempt to locate the main metascene. }
MainScene := SeekCurrentLevelGear( MainPlot^.SubCom , GG_MetaScene , MPIndex );
if MainScene = Nil then begin
{ No main scene- delink, move, and relabel this one. }
DelinkGear( SubPlot^.SubCom , MS );
InsertSubCom( MainPlot , MS );
{ Store the PlotID of this layer, since it's the first to provide details for this metascene. }
SetNAtt( MS^.NA , NAG_Narrative , NAS_PlotID , NAttValue( SubPlot^.NA , NAG_Narrative , NAS_PlotID ) );
MS^.S := MPIndex;
end else begin
{ Combine the two scenes together. }
BuildMegalist( MainScene , MS^.SA );
{ Copy over all InvComs and SubComs. }
while ( MS^.InvCom <> Nil ) do begin
Thing := MS^.InvCom;
DelinkGear( MS^.InvCom , Thing );
InsertInvCom( MainScene , Thing );
end;
while ( MS^.SubCom <> Nil ) do begin
Thing := MS^.SubCom;
DelinkGear( MS^.SubCom , Thing );
InsertSubCom( MainScene , Thing );
end;
end;
end;
Procedure CombinePlots( MasterPlot, SubPlot: GearPtr; IsAQuest: Boolean );
{ Combine SubPlot into MasterPlot, including all elements, scripts, }
{ personas, metascenes, and so on. }
{ - Merge element lists }
{ - Copy PLACE strings from SUBPLOT to MASTERPLOT. }
{ A place defined in a subplot take precedence over anything }
{ defined earlier. }
{ - Megalist scripts }
{ - Megalist personas }
{ - Combine MetaScenes }
{ - Move InvComs }
{ - Add victory points }
var
Thing,T2: GearPtr;
begin
MergeElementLists( MasterPlot , SubPlot );
BuildMegalist( MasterPlot , SubPlot^.SA );
{ Take a look at the things in this subplot. }
{ Deal with them separately, as appropriate. }
Thing := SubPlot^.SubCom;
while Thing <> Nil do begin
T2 := Thing^.Next;
if Thing^.G = GG_Persona then begin
MergePersona( MasterPlot , SubPlot , Thing , IsAQuest );
end else if Thing^.G = GG_MetaScene then begin
MergeMetascene( MasterPlot , SubPlot , Thing );
end;
Thing := T2;
end;
{ Move over the InvComs. }
while SubPlot^.InvCom <> Nil do begin
Thing := SubPlot^.InvCom;
DelinkGear( SubPlot^.InvCom , Thing );
InsertInvCom( MasterPlot , Thing );
end;
{ If the master plot doesn't have a PayRate set, the subplot gets to }
{ set one. }
if NAttValue( MasterPlot^.NA , NAG_ArenaMissionInfo , NAS_PayRate ) = 0 then SetNAtt( MasterPlot^.NA , NAG_ArenaMissionInfo , NAS_PayRate , NAttValue( SubPlot^.NA , NAG_ArenaMissionInfo , NAS_PayRate ) );
end;
Function IsStandardTrigger( const S_Head: String ): Boolean;
{ Return TRUE if S_Head is one of the standard triggers, or FALSE if it }
{ isn't. }
var
ST: SAttPtr;
MatchFound: Boolean;
begin
{ Go through the list of standard triggers; stop when we find a match. }
ST := standard_trigger_list;
MatchFound := False;
while ( ST <> Nil ) and not MatchFound do begin
if HeadMatchesString( ST^.Info , S_Head ) then MatchFound := True;
ST := ST^.Next;
end;
IsStandardTrigger := MatchFound;
end;
Function AssembleMegaPlot( Slot , SPList: GearPtr; var Quest_Frags: GearPtr; IsAQuest: Boolean ): GearPtr;
{ SPList is a list of subplots. Assemble them into a single coherent megaplot. }
{ The first item in the list is the base plot- all other plots get added to it. }
{ - Delete all placeholder stubs from SLOT }
{ - Process each fragment in turn. }
{ - Delink from list }
{ - Sequester the standard scripts }
{ - Do string substitutions }
{ - Combine plots }
{ - If a non-reusable quest fragment, delete the prototype }
{ - Insert the finished plot into slot as an invcom }
Procedure PrepStandardScripts( SubPlot: GearPtr );
{ A subplot's standard scripts (those attached to basic game triggers, as }
{ listed in ASLRef.txt) will only be called when the subplot is active. }
var
sline: SAttPtr; { our counter for moving through the list. }
s_Head,s_Script: String;
begin
sline := SubPlot^.SA;
while sline <> Nil do begin
S_Head := RetrieveAPreamble( sline^.Info );
if IsStandardTrigger( S_Head ) then begin
{ This is a standard script. It needs to be moved to a new location, }
{ and this original line needs to be replace with a redirect. }
{ First, get the script info. }
S_Script := RetrieveAString( sline^.Info );
{ Next, install the redirect. }
sline^.info := S_Head + ' <if= PlotStatus %plotid% %id% else %pop% LTrigger .%id%_' + S_Head + ' Goto %pop%>';
{ Finally, place the original script in its new home. }
SetSAtt( SubPlot^.SA , '.%id%_' + S_Head + ' <' + S_Script + '>' );
end;
sline := sline^.next;
end;
end;
Procedure InitMetasceneFactions( SubPlot: GearPtr );
{ Certain metascenes may have a faction defined. Don't use that numeric faction; }
{ instead, use the element being referred to. }
var
MScene: GearPtr;
FID: Integer;
begin
MScene := SubPlot^.SubCom;
while MScene <> Nil do begin
if MScene^.G = GG_MetaScene then begin
FID := NAttValue( MScene^.NA , NAG_Personal , NAS_FactionID );
if FID <> 0 then begin
SetNAtt( MScene^.NA , NAG_Personal , NAS_FactionID , ElementID( SubPlot , FID ) );
end;
end;
MScene := MScene^.Next;
end;
end;
Procedure DoStringSubstitutions( SubPlot: GearPtr; IsMasterPlot: Boolean );
{ Do the string substitutions for this subplot. Basically, }
{ create the dictionary and pass it on to the substituter. }
var
Dictionary: SAttPtr;
T: Integer;
begin
{ Begin creating. }
Dictionary := Nil;
SetSAtt( Dictionary , '%plotid% <' + BStr( NAttValue( SubPlot^.NA , NAG_Narrative , NAS_PlotID ) ) + '>' );
SetSAtt( Dictionary , '%id% <' + BStr( NAttValue( SubPlot^.NA , NAG_Narrative , NAS_PlotLayer ) ) + '>' );
SetSAtt( Dictionary , '%threat% <' + BStr( NAttValue( SubPlot^.NA , NAG_Narrative , NAS_DifficultyLevel ) ) + '>' );
for t := 1 to Num_Sub_Plots do begin
SetSAtt( Dictionary , '%id' + BStr( T ) + '% <' + Bstr( NAttValue( SubPlot^.NA , NAG_SubPlotLayerID , T ) ) + '>' );
SetSAtt( Dictionary , '%plotid' + BStr( T ) + '% <' + Bstr( NAttValue( SubPlot^.NA , NAG_SubPlotPlotID , T ) ) + '>' );
end;
for t := 1 to Num_Plot_Elements do begin
{ If dealing with the main plot, do substitutions for the Element Indicies now. }
if IsMasterPlot then SetSAtt( Dictionary , '%E' + BStr( T ) + '% <' + BStr( T ) + '>' );
SetSAtt( Dictionary , '%' + BStr( T ) + '% <' + BStr( ElementID( SubPlot , T ) ) + '>' );
SetSAtt( Dictionary , '%name' + BStr( T ) + '% <' + SAttValue( SubPlot^.SA , 'name_' + BStr( T ) ) + '>' );
end;
{ Run the provided subplot through the convertor. }
InitListStrings( SubPlot , Dictionary );
DisposeSAtt( Dictionary );
end;
Procedure InitMPSubs( MasterPlot: GearPtr );
{ All personas and metascenes must be marked with the PlotID }
{ of the subplot wot spawned 'em. }
var
LList: GearPtr;
begin
LList := MasterPlot^.SubCom;
while LList <> Nil do begin
if ( LList^.G = GG_Persona ) or ( LList^.G = GG_MetaScene ) then begin
SetNAtt( LList^.NA , NAG_Narrative , NAS_PlotID , NAttValue( MasterPlot^.NA , NAG_Narrative , NAS_PlotID ) );
end;
LList := LList^.Next;
end;
end;
Procedure DeleteQuestPrototype( Plot: GearPtr );
{ It's possible that this subplot is based on a quest fragment. }
{ If said fragment isn't reusable, remove it from the list. }
var
Frag: GearPtr;
begin
if ( Quest_Frags <> Nil ) and ( Plot^.S > 0 ) then begin
Frag := SeekCurrentLevelGear( Quest_Frags , GG_Plot , Plot^.S );
if ( Frag <> Nil ) and ( not QuestIsReusable( Frag ) ) then begin
RemoveGear( Quest_Frags , Frag );
end;
end;
end;
Procedure ResetQuestPrototypes;
{ Clear the INUSE frag from all remaining prototypes. }
var
Frag: GearPtr;
begin
Frag := Quest_Frags;
while Frag <> Nil do begin
SetNAtt( Frag^.NA , NAG_Narrative , NAS_QuestInUse , 0 );
Frag := Frag^.Next;
end;
end;
var
MasterPlot,SubPlot: GearPtr;
begin
{ Delete the placeholders. }
DeletePlotPlaceholders( Slot );
{ Extract the master plot. It should be the first one in the list. }
MasterPlot := SPList;
DelinkGear( SPList , MasterPlot );
DoStringSubstitutions( MasterPlot , True );
InitMetasceneFactions( MasterPlot );
InitMPSubs( MasterPlot );
InsertInvCom( Slot , MasterPlot );
DeleteQuestPrototype( MasterPlot );
{ Store the PlotID being used. Do so for all subplot IDs as well. }
SetNAtt( MasterPlot^.NA , NAG_PlotStatus , NAttValue( MasterPlot^.NA , NAG_Narrative , NAS_PlotID ) , 1 );
{ Keep processing until we run out of subplots. }
while SPList <> Nil do begin
SubPlot := SPList;
DelinkGear( SPList , SubPlot );
AddSAtt( MasterPlot^.SA , 'SUBPLOT_NAME' , GearName( SubPlot ) );
SetNAtt( MasterPlot^.NA , NAG_PlotStatus , NAttValue( SubPlot^.NA , NAG_Narrative , NAS_PlotID ) , 1 );
{ Do the substitutions for standard triggers here. }
PrepStandardScripts( SubPlot );
InitMetasceneFactions( SubPlot );
DeleteQuestPrototype( SubPlot );
DoStringSubstitutions( SubPlot , False );
CombinePlots( MasterPlot, SubPlot , IsAQuest );
DisposeGear( SubPlot );
end;
{ After assembling the plot, clear any INUSE tags left over on the }
{ quest frags. }