File: interact.pp

package info (click to toggle)
gearhead 1.100-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 5,756 kB
  • ctags: 11
  • sloc: pascal: 34,876; makefile: 59; sh: 12
file content (1462 lines) | stat: -rw-r--r-- 50,975 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
unit interact;
	{ This unit contains the rules for using interaction skills, }
	{ such as Conversation, et cetera. }
	{ It also, by reason of necessity, contains some procedures }
	{ related to random plots. The main unit for plots is }
	{ playwright.pp; see that unit for more details. }
{
	GearHead: Arena, a roguelike mecha CRPG
	Copyright (C) 2005 Joseph Hewitt

	This library is free software; you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation; either version 2.1 of the License, or (at
	your option) any later version.

	The full text of the LGPL can be found in license.txt.

	This library is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
	General Public License for more details. 

	You should have received a copy of the GNU Lesser General Public License
	along with this library; if not, write to the Free Software Foundation,
	Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
}

interface

uses gears,locale;

Const
	{ *** PERSONA GEAR *** }
	{ G = GG_Persona                              }
	{ S = Character ID or Plot Element Number     }
	{ V = na                                      }

	{ *** FACTION GEAR *** }
	{ G = GG_Faction                              }
	{ S = Faction ID                              }
	{ V = Undefined                               }

	NAG_Personal = 5;

	NAS_CID = 0;	{ Character ID }
	NAS_FactionID = 2;	{ The character's faction. }
		{ Note that the faction ID for NPCs is stored in the }
		{ character gear, but for the PC it is stored in both }
		{ the character gear and the root Adventure gear. }
	NAS_ReTalk = 1;	{ Busy counter... after talking with an NPC once, }
		{ you can't talk with that same NPC again for a few }
		{ minutes. }
	NAS_RandSeed = 3;	{ Individual seed for shopkeepers. }
	NAS_RestockTime = 4;
	NAS_PickPocketRestock = 5;	{ Recharge time for picking pockets. }

	{ TACTICS SETTINGS }
	NAS_OptMax = 6;
	NAS_OptMin = 7;

	NAS_PerformancePenalty = 8;	{ Harder to impress same NPCs after first few tries. }

	{ This attribute records how well two characters like each other or }
	{ hate each other. The "S" identifier is the CID of the character }
	{ to which this reaction score applies. }
	NAG_ReactionScore = 6;

	{ This attribute records the relationship between two factions. }
	NAG_FactionScore = 8;

	{ This attribute records the relationship this NPC has with }
	{ another NPC in the game. }
	NAG_Relationship = 10;
	{ S descriptor is the CID of the other NPC. }
	{ ... or 0 for the PC. }
	NAV_ArchEnemy = -1;
	NAV_ArchAlly = 1;	{ Is there such a thing as an arch-ally? }
				{ Who really knows. }
		{ If the relationship type is greater than 0, the NPC can join the lance. }
	NAV_Family = 2;
	NAV_Lover = 3;

	ArchEnemyReactionPenalty = 25;

	Same_Faction_Bonus = 10;
	MaxFactionScore = 25;
	MinFactionScore = -50;


Function SeekFaction( Scene: GearPtr; ID: Integer ): GearPtr;
Function GetFactionID( Part: GearPtr ): Integer;
Function FactionIsInactive( Fac: GearPtr ): Boolean;

Function PlotElementID( Plot: GearPtr; Code: Char; ID: LongInt ): Integer;
Function FindPersonaPlot( Adventure: GearPtr; CID: Integer ): GearPtr;
Function SeekPlotElement( Adventure , Plot: GearPtr; N: Integer; GB: GameBoardPtr ): GearPtr;

Function PersonalityCompatability( PC, NPC: GearPtr ): Integer;
Function ReactionScore( Scene, PC, NPC: GearPtr ): Integer;

Function CreateRumorList( GB: gameBoardPtr; PC,NPC: GearPtr ): SAttPtr;
Function IdleChatter: String;
Function IsSexy( PC, NPC: GearPtr ): Boolean;
function DoChatting( GB: GameBoardPtr; var Rumors: SAttPtr; PC,NPC: GearPtr; Var Endurance,FreeRumors: Integer ): String;

Function SeekPersona( GB: GameBoardPtr; CID: LongInt ): GearPtr;
function SeekGearByCID( LList: GearPtr; CID: LongInt ): GearPtr;

Function NewCID( GB: GameBoardPtr; Adventure: GearPtr ): LongInt;
Function NewNID( GB: GameBoardPtr; Adventure: GearPtr ): LongInt;

Function IsArchEnemy( Adv,NPC: GearPtr ): Boolean;
Function IsArchAlly( Adv,NPC: GearPtr ): Boolean;
Function XNPCDesc( Adv,NPC: GearPtr ): String;

Function GenerateEnemyHook( Scene,PC,NPC: GearPtr; Desc: String ): GearPtr;
Function GenerateAllyHook( Scene,PC,NPC: GearPtr ): GearPtr;

Function LancematesPresent( GB: GameBoardPtr ): Integer;

Function FindNPCByKeyWord( GB: GameBoardPtr; KW: String ): GearPtr;


implementation

uses ability,gearutil,ghchars,rpgdice,texutil;

const
	Num_Openings = 7;	{ Number of TraitChatter opening phrases. }
	Num_Improve_Msg = 3;	{ Number of skill improvement messages. }

	Chat_MOS_Measure = 10;


var
	{ Strings for the random conversation generator. }
	Noun_List,Phrase_List,Adjective_List,RLI_List,Chat_Msg_List,Threat_List: SAttPtr;
	Trait_Chatter: Array [1..Num_Personality_Traits,1..2] of SAttPtr;


Function SeekFaction( Scene: GearPtr; ID: Integer ): GearPtr;
	{ Look for a faction corresponding to the provided ID number. }
	{ Return NIL if no such faction is found. }
var
	F: GearPtr;
begin
	{ Error check. }
	if ( Scene = Nil ) or ( ID = 0 ) then Exit( Nil );

	{ Find the root of SCENE, which should be the ADVENTURE. }
	{ The faction should be located along the invcoms. }
	F := FindRoot( Scene )^.InvCom;
	while ( F <> Nil ) and (( F^.G <> GG_Faction ) or ( F^.S <> ID )) do F := F^.Next;

	{ If the faction was not in the normal place, call the }
	{ heavy-duty and cycle-wasteful search routine. }
	if F = Nil then F := SeekGear( FindRoot( Scene ) , GG_Faction , ID );
	SeekFaction := F;
end;

Function GetFactionID( Part: GearPtr ): Integer;
	{ This function will return the Faction ID associated with }
	{ any given part, if appropriate. }
	{ FOr a faction this will be it's "S" descriptor. }
	{ For anything else, faction affiliation is stored as a NAtt. }
begin
	if Part^.G = GG_Faction then begin
		GetFactionID := Part^.S;
	end else begin
		GetFactionID := NAttValue( Part^.NA , NAG_Personal , NAS_FactionID );
	end;
end;

Function FactionIsInactive( Fac: GearPtr ): Boolean;
	{ Return TRUE if this faction has an INACTIVE tag in its }
	{ TYPE string attribute, or FALSE otherwise. }
begin
	FactionIsInactive := AStringHasBString( SATtValue( Fac^.SA , 'TYPE' ) , 'INACTIVE' );
end;

Function PlotElementID( Plot: GearPtr; Code: Char; ID: LongInt ): Integer;
	{ Determine which plot element is referred to by the supplied data. }
	{ CODE indicates what kind of gear we're looking for, while ID }
	{ is the identification number that should be listed in the Plot's }
	{ stats. }
	{ If the supplied ID number cannot be found within this plot, }
	{ return 0. }
var
	t,N: Integer;
	EDesc: String;
begin
	N := 0;
	Code := UpCase( Code );

	for t := 1 to NumGearStats do begin
		if Plot^.Stat[T] = ID then begin
			EDesc := UpCase( SAttValue( Plot^.SA , 'ELEMENT' + BStr( T ) ) );
			DeleteWhiteSpace( EDesc );

			if ( EDesc <> '' ) and ( EDesc[1] = Code ) then begin
				N := T;
			end;
		end;
	end;

	PlotElementID := N;
end;

Function FindMetaPersona( Source: GearPtr; N: Integer ): GearPtr;
	{ Locate the replacement persona from this PLOT or STORY. }
var
	T,Meta: GearPtr;
begin
	T := Source^.SubCom;
	Meta := Nil;
	while T <> Nil do begin
		if ( T^.G = GG_Persona ) and ( T^.S = N ) then Meta := T;	
		T := T^.Next;
	end;
	FindMetaPersona := Meta;
end;

Function SeekPlotAlongPath( Part: GearPtr;  Code: Char; ID: LongInt; SeekType: Integer; NeedsPersona: Boolean ): GearPtr;
	{ Seek a gear which uses the specified element along the given }
	{ path. If no such plot is found return Nil. Recursively search }
	{ all active subcomponents. }
var
	it: GearPtr;
begin
	it := Nil;
	while ( Part <> Nil ) and ( it = Nil ) do begin
		if ( Part^.G = SeekType ) and ( PlotElementID( Part , Code , ID ) <> 0 ) then begin
			if NeedsPersona then begin
				if FindMetaPersona( Part , PlotElementID( Part , Code , ID ) ) <> Nil then begin
					it := Part;
				end;
			end else begin
				it := Part;
			end;
		end else if ( Part^.G = GG_Story ) or ( Part^.G = GG_Faction ) then begin
			it := SeekPlotALongPath( Part^.InvCom , Code , ID , SeekType , NeedsPersona );
		end;

		Part := Part^.Next;
	end;
	SeekPlotAlongPath := it;
end;

Function FindPersonaPlot( Adventure: GearPtr; CID: Integer ): GearPtr;
	{ Search all through ADVENTURE looking for a plot which }
	{ involves PERSONA. If no such plot is found, return NIL. }
begin
	{ Plots should be located along Adventure/InvCom. Plots which }
	{ are not located there are probably sub-plots, so they probably }
	{ don't yet have actors assigned. }
	Adventure := FindRoot( Adventure );
	if ( Adventure = Nil ) or ( Adventure^.G <> GG_Adventure ) then begin
		FindPersonaPlot := Nil;
	end else begin
		FindPersonaPlot := SeekPlotAlongPath( Adventure^.InvCom , 'C' , CID , GG_Plot , False );
	end;
end;

Function FindPersonaStory( Adventure: GearPtr; CID: Integer ): GearPtr;
	{ Search all through ADVENTURE looking for a story which }
	{ involves PERSONA. If no such story is found, return NIL. }
begin
	Adventure := FindRoot( Adventure );
	if ( Adventure = Nil ) or ( Adventure^.G <> GG_Adventure ) then begin
		FindPersonaStory := Nil;
	end else begin
		FindPersonaStory := SeekPlotAlongPath( Adventure^.InvCom , 'C' , CID , GG_Story , True );
	end;
end;

Function SeekPlotElement( Adventure , Plot: GearPtr; N: Integer; GB: GameBoardPtr ): GearPtr;
	{ Find the gear referred to in the N'th element of PLOT. }
	{ If no such element ay be found return Nil. }
var
	Desc: String;
	Part: GearPtr;
begin
	{ Start by locating the element description string. }
	Desc := UpCase( SAttValue( Plot^.SA , 'ELEMENT' + BStr( N ) ) );
	Adventure := FindRoot( Adventure );

	{ Look for the element in the sensible place, given the }
	{ nature of the string. }
	if Desc = '' then begin
		Part := Nil;
	end else if Desc[1] = 'C' then begin
		{ Find a character. }
		Part := SeekGearByCID( Adventure , Plot^.Stat[N] );
		if ( Part = Nil ) and ( GB <> Nil ) then Part := SeekGearByCID( GB^.Meks , Plot^.Stat[N] );
	end else if Desc[1] = 'S' then begin
		{ Find a scene. }
		if GB <> Nil then begin
			Part := FindActualScene( GB , Plot^.Stat[ N ] );
		end else begin
			Part := SeekGear( Adventure , GG_Scene , Plot^.Stat[ N ] );
		end;
	end else if Desc[1] = 'F' then begin
		{ Find a faction. }
		Part := SeekGear( Adventure , GG_Faction , Plot^.Stat[ N ] );
	end else if Desc[1] = 'I' then begin
		{ Find an item. }
		Part := SeekGearByIDTag( Adventure , NAG_Narrative , NAS_NID , Plot^.Stat[ N ] );
		if ( Part = Nil ) and ( GB <> Nil ) then Part := SeekGearByIDTag( GB^.Meks , NAG_Narrative , NAS_NID , Plot^.Stat[ N ] );
	end else begin
		Part := Nil;
	end;

	{ Return the part that was found. }
	SeekPlotElement := Part;
end;

Function PlotUsedHere( Plot: GearPtr; GB: GameBoardPtr ): Boolean;
	{ See whether or not any of the elements of PLOT are in use on }
	{ this game board or in its associated scene. }
var
	PUH,EH: Boolean;
	T: Integer;
	Desc: String;
begin
	{ Error check - Make sure both the plot and the game board are }
	{ defined. }
	if ( Plot = Nil ) or ( GB = Nil ) then begin
		PlotUsedHere := False;
	end else begin
		{ Assume FALSE, then look for any element that's being used }
		{ on the game board. }
		PUH := False;

		{ Search through all the elements. }
		for t := 1 to NumGearStats do begin
			{ Start by locating the element description string. }
			Desc := UpCase( SAttValue( Plot^.SA , 'ELEMENT' + BStr( T ) ) );

			{ Look for the element in the sensible place, given the }
			{ nature of the string. }
			if Desc = '' then begin
				EH := False;
			end else if Desc[1] = 'C' then begin
				{ Find a character. }
				EH := SeekGearByCID( GB^.Meks , Plot^.Stat[T] ) <> Nil;
			end else if Desc[1] = 'S' then begin
				{ Find a scene. }
				if GB^.Scene <> Nil then EH := Plot^.Stat[ T ] = GB^.Scene^.S
				else EH := False;
			end else if Desc[1] = 'I' then begin
				{ Find an item. }
				EH := SeekGearByIDTag( GB^.Meks , NAG_Narrative , NAS_NID , Plot^.Stat[ T ] ) <> Nil;
			end;

			PUH := PUH or EH;
		end;

		{ Return whatever result was found. }
		PlotUsedHere := PUH;
	end;
end;

Function GeneralCompatability( PC1, PC2: GearPtr ): Integer;
	{ This function will determine the general level of }
	{ compatability between two characters. This is the }
	{ modifier which will be applied to most interaction }
	{ rolls. }
	{ It is determined by several things - }
	{  - Similarity of stats and skills }
var
	T,S1,S2: Integer;
	BCS: Integer;	{ Base compatability score }
begin
	{ Error Check - Make sure both PCs are valid gears. }
	if ( PC1 = Nil ) or ( PC2 = Nil ) then begin
		GeneralCompatability := 0;

	{ Error Check - Make sure both PCs are characters. }
	end else if ( PC1^.G <> GG_Character ) or ( PC2^.G <> GG_Character ) then begin
		GeneralCompatability := 0;

	end else begin
		{ Initialize the compatability score to 0. }
		BCS := 0;

		{ Check the stats. Every stat that is wildly different will }
		{ cause a drop in compatability, while every stat which is }
		{ very similar will cause a rise in compatability. }
		for t := 1 to 8 do begin
			if Abs( PC1^.Stat[t] - PC2^.Stat[t] ) > 8 then begin
				Dec( BCS );
			end else if ( PC1^.Stat[t] - PC2^.Stat[t] ) < 3 then begin
				Inc( BCS );
			end;
		end;

		{ Check the skills. Every skill that both PCs have will }
		{ cause a rise in compatability. }
		for t := 1 to NumSkill do begin
			S1 := NAttValue( PC1^.NA , NAG_Skill , T );
			S2 := NAttValue( PC2^.NA , NAG_Skill , T );

			if ( S1 > 10 ) and ( S2 > 10 ) then begin
				BCS := BCS + 3;
			end else if ( S1 > 5 ) and ( S2 > 5 ) then begin
				BCS := BCS + 2;
			end else if ( S1 > 0 ) and ( S2 > 0 ) then begin
				BCS := BCS + 1;
			end;
		end;

		GeneralCompatability := BCS;
	end;
end;

Function PersonalityCompatability( PC, NPC: GearPtr ): Integer;
	{ Calculate the compatability between PC and NPC based on their }
	{ personality traits. }
var
	T,CS: Integer;
	NPC_Score,PC_Score: Integer;
begin
	{ Initialize the Compatability Score to 0. }
	CS := 0;

	{ Loop through all the personality traits. }
	for t := 1 to Num_Personality_Traits do begin
		{ Determine the scores of both PC and NPC with regard to this }
		{ personality trait. }
		PC_Score := NAttValue( PC^.NA , NAG_CharDescription , -T );
		NPC_Score := NAttValue( NPC^.NA , NAG_CharDescription , -T );

		{ If the personality trait being discussed here is Villainousness, }
		{ this always causes a negative reaction. Otherwise, a reaction }
		{ will only happen if both the PC and the NPC have points in }
		{ this trait. }
		if ( T = Abs( NAS_Heroic ) ) and (PC_Score < -10 ) then begin
			CS := CS - Abs( PC_Score ) div 2;

		end else if ( T = Abs( NAS_Renowned ) ) then begin
			{ Being renowned is always good, while being wangtta is }
			{ always bad. }
			if PC_Score > 0 then begin
				CS := CS + ( PC_Score div 10 );
			end else begin
				CS := CS - ( Abs( PC_Score ) div 10 );
			end;

		end else if ( PC_Score <> 0 ) and ( NPC_Score <> 0 ) then begin
			if Sgn( PC_Score ) = Sgn( NPC_Score ) then begin
				{ The traits are in agreement. Increase CS. }
				CS := CS + Abs( PC_Score ) div 10;

			end else if ( Abs( PC_Score ) > 10 ) and ( Abs( NPC_Score ) > 10 ) then begin
				{ The traits are in opposition. Decrease CS. }
				CS := CS - 5;

			end;
		end;
	end;

	PersonalityCompatability := CS;
end;

Function FactionScore( Scene: GearPtr; F0,F1: Integer ): Integer;
	{ Given two factions, return the amount by which they are }
	{ allied to each other or hate each other. }
var
	Fac_0: GearPtr;
	it: Integer;
begin
	if ( F0 = 0 ) or ( F1 = 0 ) then begin
		it := 0;

	end else if F0 = F1 then begin
		it := Same_Faction_Bonus;

	end else begin
		Fac_0 := SeekFaction( Scene , F0 );
		if Fac_0 <> Nil then begin
			it := NAttValue( Fac_0^.NA , NAG_FactionScore , F1 );
		end else begin
			it := 0;
		end;

	end;
	FactionScore := it;
end;

Function FactionCompatability( Scene, PC, NPC: GearPtr ): Integer;
	{ Determine the faction compatability scores between PC and NPC. }
	{ + the PC's reputation with the NPC's faction. }
	{ - if PC is enemy of allied faction. }
	{ - if PC is ally of enemy faction. }
var
	NPC_FID,PC_FID,it: Integer;
	FAC: GearPtr;
begin
	{ Step one - Locate the FACTION information of the NPC, and }
	{ the PC's FACTION ID.. }
	NPC_FID := NAttValue( NPC^.NA , NAG_Personal , NAS_FactionID );
	Fac := SeekFaction( Scene , NPC_FID );
	PC_FID := NAttValue( PC^.NA , NAG_Personal , NAS_FactionID );

	it := FactionScore( Scene , NPC_FID , PC_FID );
	if FAC <> Nil then it := it + PersonalityCompatability( PC , FAC );

	if it > MaxFactionScore then it := MaxFactionScore
	else if it < MinFactionScore then it := MinFactionScore;

	FactionCompatability := it;
end;

Function ReactionScore( Scene, PC, NPC: GearPtr ): Integer;
	{ Return a score in the range of -100..+100 which tells how much }
	{ the NPC likes the PC. }
var
	it,Persona: Integer;
begin
	{ The basic Reaction Score is equal to GENERAL COMPATABILITY + the }
	{ existing reaction modifier. }
	Persona := NAttValue( NPC^.NA , NAG_Personal , NAS_CID );
	it := GeneralCompatability( PC , NPC ) + PersonalityCompatability( PC , NPC ) + NAttValue( PC^.NA , NAG_ReactionScore , Persona );

	{ If the scene is defined, add the faction compatability score. }
	if Scene <> Nil then it := it + FactionCompatability( Scene , PC , NPC );

	{ Make sure IT doesn't go out of bounds. }
	if it > 100 then it := 100
	else if it < -100 then it := -100;

	{ A true archenemy will never have a greater reaction score than 0. }
	if ( NAttValue( NPC^.NA , NAG_Relationship , NAttValue( PC^.NA , NAG_Personal , NAS_CID ) ) = NAV_ArchEnemy ) then begin
		it := it - ArchEnemyReactionPenalty;
		if it > 0 then it := 0;
	end;

	ReactionScore := it;
end;

Function BlowOff: String;
	{ The NPC will just say something mostly useless to the PC. }
begin
	{ At some point in time I will make a lovely procedure that will }
	{ create all sorts of useless chatter. Right now, I'll just return }
	{ the following constant string. }
	BlowOff := 'I really don''t have much time to chat today, I have a lot of things to do.';
end;

function MadLibString( SList: SAttPtr ): String;
	{ Given a list of string attributes, return one of them at random. }
var
	SA: SAttPtr;
begin
	SA := SelectRandomSAtt( SList );
	if SA <> Nil then MadLibString := SA^.Info
	else MadLibString := '***ERROR***';
end;

Function FormatChatString( Msg1: String ): String;
	{ Do formatting on this string, adding nouns, adjectives, }
	{ and threats as needed. }
var
	msg2,w: String;
begin
	msg2 := '';

	while msg1 <> '' do begin
		w := ExtractWord( msg1 );

		if W[1] = '%' then begin
			DeleteFirstChar( W );
			if UpCase( W[1] ) = 'N' then begin
				DeleteFirstChar( W );
				W := MadLibString( Noun_List ) + W;
			end else if UpCase( W[1] ) = 'T' then begin
				DeleteFirstChar( W );
				W := MadLibString( Threat_List ) + W;
			end else begin
				DeleteFirstChar( W );
				W := MadLibString( Adjective_List ) + W;
			end;
		end;

		msg2 := msg2 + ' ' + w;
	end;

	DeleteWhiteSpace( Msg2 );
	FormatChatString := Msg2;
end;

Function IdleChatter: String;
	{ Create a Mad-Libs style line for the NPC to tell the PC. }
	{ Hopefully, these mad-libs will simulate the cheerfully nonsensical }
	{ things that poorly tanslated anime characters often say to }
	{ each other. }
	{ After testing this procedure, the effect is more akin to the }
	{ konglish slogans which adorn stationary & other character goods... }
	{ Close enough! I've got a winner here... }
var
	msg1: String;
begin
	{ Start with a MadLib form in msg1, and nothing in Msg2. }
	{ Transfer the message from M1 to M2 one word at a time, replacing }
	{ nouns and adjectives along the way. }
	msg1 := MadLibString( Phrase_List );

	IdleChatter := FormatChatString( Msg1 );
end;

Function DoTraitChatter( NPC: GearPtr; Trait: Integer ): String;
	{ The NPC needs to say a line which should give some indication }
	{ as to his/her orientation with respect to the listed }
	{ personality trait. }
const
	Num_Phrase_Bases = 3;
var
	Rk,Pro: Integer;
	msg: String;
begin
	{ To start with, find the trait rank. }
	Rk := NAttValue( NPC^.NA , NAG_CharDescription , -Trait );

	{ Insert a basic starting phrase in the message, or perhaps none }
	{ at all... }
	if Random( 10 ) <> 1 then begin
		msg := SAttValue( Chat_Msg_List , 'TRAITCHAT_Lead' + BStr( Random( Num_Openings ) + 1 ) ) + ' ';
	end else begin
		msg := '';
	end;

	if Abs( Rk ) > 10 then begin
		{ Determine which side of the trait the NPC is in favor of. }
		if Rk > 0 then Pro := 1
		else Pro := 2;

		{ The NPC will either say that they like something from their own side, }
		{ or that they dislike something from the other. }
		if Random( 5 ) <> 1 then begin
			{ Like something. }
			msg := msg + SAttValue( Chat_Msg_List , 'TRAITCHAT_Like' + BStr( Random( Num_Phrase_Bases ) + 1 ) ) + ' ' + MadLibString( Trait_Chatter[ Trait , Pro ] ) + '.';

		end else begin
			{ Dislike something. }
			msg := msg + SAttValue( Chat_Msg_List , 'TRAITCHAT_Hate' + BStr( Random( Num_Phrase_Bases ) + 1 ) ) + ' ' + MadLibString( Trait_Chatter[ Trait , 3 - Pro ] ) + '.';

		end;
	end else begin
		Pro := Random( 2 ) + 1;
		msg := msg + SAttValue( Chat_Msg_List , 'TRAITCHAT_Ehhh' + BStr( Random( Num_Phrase_Bases ) + 1 ) ) + ' ' + MadLibString( Trait_Chatter[ Trait , Pro ] ) + '.';

	end;

	DoTraitChatter := Msg;
end;

Function CreateRumorList( GB: gameBoardPtr; PC,NPC: GearPtr ): SAttPtr;
	{ Scour the GB for information which can be passed to the PC. }
var
	InfoList: SAttPtr;
	Part: GearPtr;
{ Procedures Block }
	Procedure ExtractData( P: GearPtr );
		{ Store all relevant info from PART. }
	var
		Rumor: String;
		Trait,Level: Integer;
		Persona: GearPtr;
	begin
		if P <> NPC then begin
			Rumor := SAttValue( P^.SA , 'RUMOR' );
			if Rumor <> '' then StoreSAtt( InfoList , MadLibString( RLI_List ) + ' ' + Rumor );

			if P^.G = GG_Character then begin
				{ At most one personality trait per NPC will be added }
				{ to the list. This is to keep them from overwhelming the }
				{ rumors from plots & other stuff... }
				Trait := Random( Num_Personality_Traits ) + 1;
				Level := NAttValue( P^.NA , NAG_CharDescription , -Trait );
				if Level <> 0 then begin
					if P = PC then begin
						StoreSAtt( InfoList , MadLibString( RLI_List ) + ' you are ' + LowerCase( PersonalityTraitDesc( Trait,Level ) ) + '.' );
					end else begin
						StoreSAtt( InfoList , MadLibString( RLI_List ) + ' ' + GearName( P ) + ' is ' + LowerCase( PersonalityTraitDesc( Trait,Level ) ) + '.' );
					end;
				end;

				Persona := SeekPersona( GB , NAttValue( P^.NA , NAG_Personal , NAS_CID ) );
				if Persona <> Nil then ExtractData( Persona );

			end else if Part^.G = GG_Scene then begin
				{ Include a rumor based on what faction controls this scene. }
				Persona := SeekFaction( GB^.Scene , NAttValue( Part^.NA , NAG_Personal , NAS_FactionID ) );
				if Persona <> Nil then begin
					Rumor := MadLibString( RLI_List ) + ' ';
					Rumor := Rumor + SAttValue( Chat_Msg_List , 'RUMOR_TownFac1' ) + GearName( Persona ) + SAttValue( Chat_Msg_List , 'RUMOR_TownFac2' );
					StoreSAtt( InfoList , Rumor );
				end;

			end else if Part^.G = GG_Faction then begin
				{ If the faction is active, tell about its traits. }
				{ Otherwise, tell that it has been disbanded. }
				if AStringHasBString( SAttValue( P^.SA , 'TYPE' ) , 'INACTIVE' ) then begin
					StoreSAtt( InfoList , MadLibString( RLI_List ) + ' ' + GearName( P ) + SAttValue( chat_msg_list , 'FACTION_IS_INACTIVE' ) );
				end else begin
					Trait := Random( Num_Personality_Traits ) + 1;
					Level := NAttValue( P^.NA , NAG_CharDescription , -Trait );
					if Level <> 0 then begin
						StoreSAtt( InfoList , MadLibString( RLI_List ) + ' ' + GearName( P ) + ' is ' + LowerCase( PersonalityTraitDesc( Trait,Level ) ) + '.' );
					end;
				end;

			end;
		
		end else begin
			{ Include information about the NPC's faction, }
			{ if appropriate. }
			Persona := SeekFaction( GB^.Scene , NAttValue( NPC^.NA , NAG_Personal , NAS_FactionID ) );
			if Persona <> Nil then begin
				Rumor := SAttValue( Chat_Msg_List , 'TRAITCHAT_Lead' + BStr( Random( Num_Openings ) + 1 ) ) + ' ';
				Rumor := Rumor + SAttValue( Chat_Msg_List , 'RUMOR_Membership1' ) + GearName( Persona ) + SAttValue( Chat_Msg_List , 'RUMOR_Membership2' );
				StoreSAtt( InfoList , Rumor );
			end;
		end;
	end;
	Procedure CheckTrackForRumors( P: GearPtr );
		{ Check along this path for rumors, calling the Extract Data }
		{ procedure as needed. }
	begin
		while P <> Nil do begin
			if P^.G = GG_Plot then begin
				if PlotUsedHere( P , GB ) then ExtractData( P );
			end else if ( P^.G = GG_Story ) or ( P^.G = GG_Faction ) then begin
				ExtractData( P );
				CheckTrackForRumors( P^.InvCom );
			end;
			P := P^.Next;
		end;

	end;
begin
	{ Initialize INFOLIST to Nil. String Attributes will be used to store }
	{ all the possible bits of information that might be given to the PC. }
	InfoList := Nil;

	{ Check all objects on the map for RUMOR SAtts. }
	Part := GB^.Meks;
	while Part <> Nil do begin
		ExtractData( Part );
		Part := Part^.Next;
	end;

	{ If this gameboard has a SCENE gear defined, check both the scene }
	{ and all of its level one children for runors. }
	if GB^.Scene <> Nil then begin
		Part := GB^.Scene;
		ExtractData( Part );

		Part := GB^.Scene^.SubCom;
		while Part <> Nil do begin
			ExtractData( Part );
			Part := Part^.Next;
		end;

		Part := GB^.Scene^.InvCom;
		while Part <> Nil do begin
			ExtractData( Part );
			Part := Part^.Next;
		end;

		{ Look for an ADVENTURE gear, then extract rumors from the }
		{ global plots. }
		Part := FindRoot( GB^.Scene );
		if ( Part^.G = GG_Adventure ) and ( not AStringHasBString( SAttValue( GB^.Scene^.SA , 'TYPE' ) , 'ISOLATED' ) ) then begin
			CheckTrackForRumors( Part^.InvCom );
		end;
	end;

	CreateRumorList := InfoList;
end;

function InOpposition( PC , NPC: GearPtr; Trait: Integer ): Boolean;
	{ If the PC and the NPC disagree on this personality TRAIT, }
	{ return TRUE. Otherwise return FALSE. }
var
	T1,T2: Integer;
begin
	T1 := NAttValue( PC^.NA , NAG_CharDescription , -Trait );
	T2 := NAttValue( NPC^.NA , NAG_CharDescription , -Trait );

	if ( Abs( T1 ) > 10 ) and ( Abs( T2 ) > 10 ) then begin
		{ The characters are in opposition if their trait }
		{ values are on opposite sides of 0. }
		InOpposition := Sgn( T1 ) <> Sgn( T2 );
	end else begin
		{ If the traits aren't strongly held by both, then }
		{ no real opposition. }
		InOpposition := False;
	end;
end;

function InHarmony( PC , NPC: GearPtr; Trait: Integer ): Boolean;
	{ If the PC and the NPC agree on this personality TRAIT, }
	{ return TRUE. Otherwise return FALSE. }
var
	T1,T2: Integer;
begin
	T1 := NAttValue( PC^.NA , NAG_CharDescription , -Trait );
	T2 := NAttValue( NPC^.NA , NAG_CharDescription , -Trait );

	if ( Abs( T1 ) > 10 ) and ( Abs( T2 ) > 10 ) then begin
		{ The characters are in opposition if their trait }
		{ values are on opposite sides of 0. }
		InHarmony := Sgn( T1 ) = Sgn( T2 );
	end else begin
		{ If the traits aren't strongly held by both, then }
		{ no real opposition. }
		InHarmony := False;
	end;
end;

Function IsSexy( PC, NPC: GearPtr ): Boolean;
	{ Return TRUE if there are some potential sparks between }
	{ the PC and NPC, or FALSE if there aren't. In this simple }
	{ universe we'll describe that as being if their genders }
	{ aren't equal to each other. }
begin
	IsSexy := ( NAttValue( PC^.NA , NAG_CharDescription , NAS_Gender ) <> NAttValue( NPC^.NA , NAG_CharDescription , NAS_Gender ) ) or HasTalent( PC , NAS_Bishounen );
end;

Function DoleChatExperience( PC,NPC: GearPtr ): String;
	{ Give a skill-specific experience award to either Conversation }
	{ or Flirtation. }
var
	Skill_To_Improve: Integer;
	msg: sTRING;
begin
	{ Give a single point of general experience or a }
	{ skill-specific award. }
	if Random( 2 ) = 1 then begin
		DoleExperience( PC , XPA_GoodChat );
		msg := '';
	end else begin
		Skill_To_Improve := 19;
		if IsSexy( PC , NPC ) and ( Random( 3 ) = 1 ) then Skill_To_Improve := 27;
		if DoleSkillExperience( PC , Skill_To_Improve , XPA_GoodChat ) then begin
			msg := SAttValue( Chat_Msg_List , 'CHAT_Skill' + BStr( Skill_To_Improve ) + '_' + BStr( Random( Num_Improve_Msg ) + 1 ) );
		end else begin
			msg := '';
		end;
	end;
	DoleChatExperience := msg;
end;

function DoChatting( GB: GameBoardPtr; var Rumors: SAttPtr; PC,NPC: GearPtr; Var Endurance,FreeRumors: Integer ): String;
	{ This function will do chatting between the specified PC }
	{ and NPC with the specified persona, adjust the Reaction and }
	{ Endurance variables, then return a string that results }
	{ from the chat session. }
var
	SkRoll,SkTarget,MOS: Integer;
	Persona: Integer;
	msg,alt_msg: String;
	RTemp: SAttPtr;
	Trait: Integer;		{ The personality trait invoked by this conversation. }
	Function TraitWeight( N : Integer ): Integer;
		{ Return a value indicating how strongly this NPC }
		{ feels about this particular personality trait. }
	begin
		TraitWeight := Abs( NAttValue( NPC^.NA , NAG_CharDescription , -N ) ) + 5;
	end;
	Function SelectTraitForChatter: Integer;
		{ Decide what the subject of the conversation is going }
		{ to be based on the NPC's traits. }
	var
		total,N,T: Integer;
	begin
		{ The trait to be used will be determined by the }
		{ weight of the NPC's traits. }
		{ Find the total of the NPC's trait points. }
		total := 0;
		for t := 1 to Num_Personality_Traits do total := total + TraitWeight( T );

		{ Next, select a random value and find a trait based on that. }
		N := Random( Total );
		T := 1;
		while N > TraitWeight( T ) do begin
			N := N - TraitWeight( T );
			Inc( T );
		end;
		SelectTraitForChatter := T;
	end;
	Procedure SelectChatter;
		{ Normally idle chatter has been selected; this procedure may }
		{ select a trait-based interaction instead. }
	begin
		if ( Trait <> 0 ) and ( Random( 2 ) = 1 ) then begin
			{ Trait-Based Chatter. }
			msg := DoTraitChatter( NPC , Trait );
		end else begin
			{ Regular Chatter. }
			msg := IdleChatter;
		end;
	end;
begin
	{ Start by making a social interaction roll for the PC. }
	SkRoll := RollStep( SkillValue( PC , 19 ) );

	{ Apply flirtation bonus to the skill roll, if appropriate. }
	{ The bonus only applies if the PC has ranks in flirtation or is a Jack of all Trades. }
	if IsSexy( PC , NPC ) and ( ( NAttValue( PC^.NA , NAG_Skill , 27 ) > 0 ) or HasTalent( PC , NAS_JackOfAll ) ) then begin
		SkRoll := SkRoll + RollStep( SkillValue( PC , 27 ) );
	end;

	{ Initialize TRAIT to random, and find the NPC's PERSONA value. }
	{ These things will be needed later. }
	if Random( 3 ) <> 1 then begin
		Trait := SelectTraitForChatter;
	end else begin
		Trait := 0;
	end;
	Persona := NAttValue( NPC^.NA , NAG_Personal , NAS_CID );

	{ Determine the effect target number. The more extreme the NPC's }
	{ current opinion of the PC is, the more difficult it will be to }
	{ change that opinion.  In addition, if the opinion is a negative }
	{ one, it'll be even harder to change the opinion. }
	SkTarget := 10 + Abs( ReactionScore( GB^.Scene , PC , NPC ) - 10 ) div 3;

	{ Reduce ENDURANCE. }
	if ( SkRoll > SkTarget ) or ( Random ( 110 ) > ReactionScore( GB^.Scene , PC , NPC ) ) then Dec( Endurance );
	if SkRoll < RollStep( 4 ) then Dec( Endurance );

	{ Finally, decide what the result of all this die rolling will be. }
	{ First see what useful (or useless) information the NPC will share. }
	if ( SkRoll + ReactionScore( GB^.Scene , PC , NPC ) + Random(10) - Random(10) ) < 0 then begin
		msg := BlowOff;

		{ Since the NPC is trying to get rid of the PC, }
		{ decrement ENDURANCE one more time. }
		Dec( Endurance );

	end else if ( FreeRumors > 0 ) and ( Rumors <> Nil ) then begin
		RTemp := SelectRandomSAtt( Rumors );
		msg := RTemp^.info;
		RemoveSAtt( Rumors, RTemp );
		Dec( FreeRumors );

	end else if ( SkRoll + ( ReactionScore( GB^.Scene , PC , NPC ) div  10 ) ) < 10 then begin
		SelectChatter;

	end else begin
		if ( Rumors <> Nil ) and ( SkRoll > ( 10 + Random( 21 ) ) ) then begin
			RTemp := SelectRandomSAtt( Rumors );
			msg := RTemp^.info;
			RemoveSAtt( Rumors, RTemp );
		end else begin
			SelectChatter;
		end;
	end;

	{ Secondly there's a chance that the chatting will improve relations }
	{ between the PC and NPC. If a TRAIT conversation has taken place, }
	{ this could make things harder. }
	if ( Trait <> 0 ) then begin
		if InOpposition( PC , NPC , Trait ) then begin
			SkRoll := SkRoll - SkTarget;
		end else if InHarmony( PC , NPC , Trait ) then begin
			SkRoll := SkRoll + ( ( SkRoll * Abs( NAttValue( PC^.NA , NAG_CharDescription , -Trait ) ) ) div 100 );
		end;
	end;


	if SkRoll > SkTarget then begin
		MOS := 1 + ( SkRoll - SkTarget ) div Chat_MOS_Measure;
		if Persona > 0 then begin
			AddNAtt( PC^.NA , NAG_ReactionScore , Persona , MOS );
			if ( MOS > 1 ) and ( SkTarget > 15 ) then DoleExperience( PC , MOS div 2 );
		end;

	end else if SkRoll < 0 then begin
		{ A negative skill roll means that the reaction is going to worsen. }
		MOS := 1 + Abs( SkRoll ) div 2;

		{ If the PC has the DIPLOMATIC talent, this can be avoided. }
		if HasTalent( PC , NAS_Diplomatic ) and ( RollStep( SkillValue( PC , 19 ) ) > ( NPC^.Stat[ STAT_Ego ] + 2 ) ) then begin
			MOS := 0;
		end;

		AddNAtt( PC^.NA , NAG_ReactionScore , Persona , -MOS );
	end;

	{ If appropriate, dole some experience points out. }
	{ Note that we're doing it down here since flirtation bonus }
	{ should apply. }
	if SkRoll > CHAT_EXPERIENCE_TARGET then begin
		alt_msg := DoleChatExperience( PC , NPC );
		if alt_msg <> '' then msg := alt_msg;
	end;

	DoChatting := msg;
end;


Function SeekPersona( GB: GameBoardPtr; CID: LongInt ): GearPtr;
	{ Seek the closest persona gear with the provided Character ID. }
	{ If this NPC is involved in a plot, use the persona gear from }
	{ the plot if one is provided. Otherwise, seek the PERSONA }
	{ in the GB/Scene gear. }
var
	Plot,Persona: GearPtr;
	N: Integer;
begin
	Persona := Nil;

	{ Use the persona located in the character's PLOT, if appropriate. }
	Plot := FindPersonaPlot( FindRoot( GB^.Scene ) , CID );
	if Plot <> Nil then begin
		{ This character is featured in a plot. The plot may }
		{ well contain a persona for this character to use }
		{ while the plot is in effect. }
		N := PlotElementID( Plot , 'C' , CID );
		Persona := FindMetaPersona( Plot , N );
	end;

	{ Use the persona from the character's STORY next. }
	if Persona = Nil then begin
		Plot := FindPersonaStory( FindRoot( GB^.Scene ) , CID );
		if Plot <> Nil then begin
			N := PlotElementID( Plot , 'C' , CID );
			Persona := FindMetaPersona( Plot , N );
		end;
	end;

	{ Next two places to look - The current scene, and the }
	{ adventure itself. }
	if Persona = Nil then Persona := SeekGear( GB^.Scene , GG_Persona , CID );
	if ( Persona = Nil ) and ( CID > NumGearStats ) then Persona := SeekGear( FindRoot( GB^.Scene ) , GG_Persona , CID );


	SeekPersona := Persona;
end;

function SeekGearByCID( LList: GearPtr; CID: LongInt ): GearPtr;
	{ Seek a gear with the provided ID. If no such gear is }
	{ found, return NIL. }
begin
	SeekGearByCID := SeekGearByIDTag( LList , NAG_Personal , NAS_CID , CID );
end;

Function NewCID( GB: GameBoardPtr; Adventure: GearPtr ): LongInt;
	{ Determine a new, unique CID for a character being added to the }
	{ campaign. To make sure our CID is unique, we'll be making it one }
	{ point higher than the highest CID we can find. }
var
	it,it2: LongInt;
	Procedure CheckAlongPath( LList: GearPtr );
	begin
		while LList <> Nil do begin
			if ( LList^.G = GG_Persona ) and ( LList^.S > it ) then it := LList^.S;
			CheckAlongPath( LList^.SubCom );
			CheckAlongPath( LList^.InvCom );
			LList := LList^.Next;
		end;
	end;
begin
	{ To start with, find the highest ID being used by a character. }
	it := NAttValue( Adventure^.NA , NAG_Narrative , NAS_MaxCID );
	if it = 0 then begin
		IT := MaxIDTag( Adventure , NAG_Personal , NAS_CID );
		if GB <> Nil then begin
			it2 := MaxIDTag( GB^.Meks , NAG_Personal , NAS_CID );
			if it2 > it then it := it2;
		end;

		{ Next, search all the PERSONA gears to make sure none of them }
		{ have one higher. }
		CheckAlongPath( Adventure );
	end;

	{ Return the highest value found, +1. }
	SetNAtt( Adventure^.NA , NAG_Narrative , NAS_MaxCID , it + 1 );
	NewCID := it + 1;
end;

Function NewNID( GB: GameBoardPtr; Adventure: GearPtr ): LongInt;
	{ Determine a new, unique NID for an item being added to the }
	{ campaign. To make sure our NID is unique, we'll be making it one }
	{ point higher than the highest NID we can find. }
var
	it,it2: LongInt;
begin
	{ To start with, find the highest ID being used by a character. }
	it := NAttValue( Adventure^.NA , NAG_Narrative , NAS_MaxNID );
	if it = 0 then begin
		IT := MaxIDTag( Adventure , NAG_Narrative , NAS_NID );
		if GB <> Nil then begin
			it2 := MaxIDTag( GB^.Meks , NAG_Narrative , NAS_NID );
			if it2 > it then it := it2;
		end;
	end;

	{ Return the highest value found, +1. }
	SetNAtt( Adventure^.NA , NAG_Narrative , NAS_MaxNID , it + 1 );
	NewNID := it + 1;
end;

Procedure LoadTraitChatter;
	{ Load the trait chatter elements from disk. }
var
	t: integer;
begin
	for t := 1 to Num_Personality_Traits do begin
		Trait_Chatter[ T , 1 ] := LoadStringList( Trait_Chatter_Base + BStr( T ) + '_1.txt' );
		Trait_Chatter[ T , 2 ] := LoadStringList( Trait_Chatter_Base + BStr( T ) + '_2.txt' );
	end;
end;

Procedure FreeTraitChatter;
	{ Remove the trait chatter elements from memory. }
var
	t: integer;
begin
	for t := 1 to Num_Personality_Traits do begin
		DisposeSAtt( Trait_Chatter[ T , 1 ] );
		DisposeSAtt( Trait_Chatter[ T , 2 ] );
	end;
end;

Function IsArchEnemy( Adv,NPC: GearPtr ): Boolean;
	{ Return TRUE if the NPC is an arch-enemy of the PC, or }
	{ FALSE otherwise. }
	{ The NPC will be an arch-enemy if it has that particular }
	{ relationship set, or if the NPC and the PC belong to }
	{ warring factions. }
var
	it: Boolean;
	PCF,NPCF: Integer;
begin
	it := NATtValue( NPC^.NA , NAG_Relationship , 0 ) = NAV_ArchEnemy;

	{ If this character is not an intrinsic enemy of the PC, maybe }
	{ it will be an enemy because of faction relations. }
	if ( Adv <> Nil ) and not it then begin
		NPCF := GetFactionID( NPC );
		NPC := SeekFaction( Adv , NPCF );
		PCF := NAttValue( FindRoot( Adv )^.NA , NAG_Personal , NAS_FactionID );
		it := ( FactionScore( Adv , NPCF , PCF ) < 0 ) or (( NPC <> Nil ) and ( NAttValue( NPC^.NA , NAG_Relationship , 0 ) = NAV_ArchEnemy ));
	end;

	IsArchEnemy := it;
end;

Function IsArchAlly( Adv,NPC: GearPtr ): Boolean;
	{ Return TRUE if the NPC is an arch-ally of the PC, or }
	{ FALSE otherwise. }
	{ The NPC will be an arch-ally if it has that particular }
	{ relationship set, or if the NPC and the PC belong to }
	{ the same faction. }
var
	it: Boolean;
	PCF,NPCF: Integer;
begin
	it := NATtValue( NPC^.NA , NAG_Relationship , 0 ) = NAV_ArchAlly;

	{ If this character is not an intrinsic ally of the PC, maybe }
	{ it will be an ally because of faction relations. }
	if ( Adv <> Nil ) and not it then begin
		NPCF := GetFactionID( NPC );
		NPC := SeekFaction( Adv , NPCF );
		PCF := NAttValue( FindRoot( Adv )^.NA , NAG_Personal , NAS_FactionID );
		it := ( FactionScore( Adv , NPCF , PCF ) > 0 ) or ( ( NPC <> Nil ) and ( NATtValue( NPC^.NA , NAG_Relationship , 0 ) > 0 ) );
	end;

	IsArchAlly := it;
end;

Function XNPCDesc( Adv,NPC: GearPtr ): String;
	{ Extended NPC description. }
var
	it: String;
begin
	it := NPCTraitDesc( NPC );

	if IsArchEnemy( Adv, NPC ) then it := it + ' ARCHENEMY';
	if NAttValue( NPC^.NA , NAG_Relationship , 0 ) = NAV_Lover then it := it + ' LOVER';
	if NAttValue( NPC^.NA , NAG_Relationship , 0 ) = NAV_Family then it := it + ' FAMILY';
	if IsArchAlly( Adv, NPC ) then it := it + ' ARCHALLY';

	if FindPersonaPlot( Adv , NAttValue( NPC^.NA , NAG_Personal , NAS_CID ) ) = Nil then it := it + ' NOPLOT'
	else it := it + ' YESPLOT';

	XNPCDesc := it;
end;

Function GenerateEnemyHook( Scene,PC,NPC: GearPtr; Desc: String ): GearPtr;
	{ Return a PERSONA gear to be used by the provided NPC }
	{ in the upcoming battle. }
	Function RelativeMessage: String;
		{ Provide a message based upon either the Ally/Enemy }
		{ status of the NPC, or upon the reaction score between }
		{ PC and NPC. }
	var
		R: Integer;
	begin
		if Random( 3 ) <> 1 then begin
			if IsArchAlly( Scene , NPC ) then begin
				RelativeMessage := SAttValue( Chat_Msg_List , 'EHOOK_AreAllies_' + BStr( Random( 3 ) + 1 ) );
			end else if IsArchEnemy( Scene , NPC ) then begin
				RelativeMessage := SAttValue( Chat_Msg_List , 'EHOOK_AreEnemies_' + BStr( Random( 3 ) + 1 ) );
			end else begin
				RelativeMessage := SAttValue( Chat_Msg_List , 'EHOOK_AreNeutral_' + BStr( Random( 3 ) + 1 ) );
			end;
		end else begin
			R := ReactionScore( Scene, PC, NPC );
			if R > ( 35 + Random( 50 ) ) then begin
				RelativeMessage := SAttValue( Chat_Msg_List , 'EHOOK_Like_' + BStr( Random( 3 ) + 1 ) );
			end else if R > ( Random( 30 ) - 10 ) then begin
				RelativeMessage := SAttValue( Chat_Msg_List , 'EHOOK_Ehhh_' + BStr( Random( 3 ) + 1 ) );
			end else begin
				RelativeMessage := SAttValue( Chat_Msg_List , 'EHOOK_Hate_' + BStr( Random( 3 ) + 1 ) );
			end;
		end;
	end;

	Function TraitMessage( T: Integer ): String;
	var
		L: Integer;
	begin
		{ Note that a space is added to the front of the }
		{ trait message for formatting purposes. }
		L := NAttValue( NPC^.NA , NAG_CharDescription , -T );
		if L > 10 then begin
			TraitMessage := ' ' + SAttValue( Chat_Msg_List , 'EHOOK_Trait_' + BStr( T ) + '_1_' + BStr( Random( 3 ) + 1 ) );
		end else if L < -10 then begin
			TraitMessage := ' ' + SAttValue( Chat_Msg_List , 'EHOOK_Trait_' + BStr( T ) + '_2_' + BStr( Random( 3 ) + 1 ) );
		end else begin
			TraitMessage := '';
		end;
	end;

	Function IntimidationTarget: Integer;
		{ Determine how easily this NPC may be scared off. }
	const
		baseTV = 5;
		minimumTV = 10;
	var
		IT,Trait: Integer;
	begin
		{ Difficulcy level is based on the NPC's EGO stat. }
		it := baseTV + CStat( NPC , STAT_Ego );

		{ Certain personality traits can affect the IT. }
		{ LAWFUL characters are less likely to abandon their causes. }
		Trait := NAttValue( NPC^.NA , NAG_CharDescription , NAS_Lawful );
		if Trait > 10 then begin
			it := it + ( Trait div 10 );
		end;

		{ PASSIONATE characters long for battle, }
		{ while EASYGOING characters long for comfort. }
		Trait := NAttValue( NPC^.NA , NAG_CharDescription , NAS_Easygoing );
		if Trait > 25 then begin
			it := it - ( Trait div 25 );
		end else if Trait < -15 then begin
			it := it + ( Abs( Trait ) div 15 );
		end;

		{ RENOWNED characters aren't easily intimidated. }
		Trait := NAttValue( NPC^.NA , NAG_CharDescription , NAS_Renowned ) - NAttValue( PC^.NA , NAG_CharDescription , NAS_Renowned );
		if Trait > 10 then begin
			it := it + ( Trait div 5 );
		end else if Trait < -15 then begin
			it := it - ( Abs( Trait ) div 15 );
		end;

		{ If it's less than the minimum target value, }
		{ set it to at least that much. }
		if it < MinimumTV then it := MinimumTV;

		IntimidationTarget := it;
	end;

var
	Hook: GearPtr;
	greeting,msg1,cmd: String;
	N1,N2: Integer;
begin
	{ Create the gear for the hook. }
	Hook := NewGear( Nil );
	Hook^.G := GG_Persona;
	Hook^.S := NAttValue( NPC^.NA , NAG_Personal , NAS_CID );
	InitGear( Hook );

	greeting := SAttValue( Chat_Msg_List , 'EHook_Greeting' );

	{ Record the intimidation target and XPV. }
	N1 := IntimidationTarget;
	SetNAtt( Hook^.NA , 0 ,  999 , N1 );
	if N1 > 0 then SetNAtt( Hook^.NA , 0 , 1000 , N1 * 50 );


	{ Create Message 1 - the NPC's speech to the player. }
	{ Start with a trait message. If empty, use a relative message }
	{ instead. }
	N1 := Random( Num_Personality_Traits ) + 1;
	msg1 := TraitMessage( N1 );
	if msg1 = '' then msg1 := RelativeMessage;

	{ Add a second trait message which should not conflict with the }
	{ first one. }
	N2 := Random( Num_Personality_Traits - 1 ) + 1;
	if N2 = N1 then Inc( N2 );
	msg1 := msg1 + TraitMessage( N2 );

	{ Define the options. }
	desc := UpCase( desc );
	while desc <> '' do begin
		cmd := ExtractWord( desc );

		if cmd = '+PCRA' then begin
			{ Player can run away. Enemy will give player }
			{ the option to leave. }
			msg1 := msg1 + ' ' + FormatChatString( SAttValue( Chat_Msg_List , 'EHOOK_PCRA_' + BStr( Random( 5 ) + 1 ) ) );
			greeting := greeting + ' AddChat 2';
			SetSAtt( Hook^.SA , 'prompt2 <' + FormatChatString( SAttValue( Chat_Msg_List , 'EHOOK_P_2_' + BStr( Random( 5 ) + 1 ) ) ) + '>' );
			SetSAtt( Hook^.SA , 'result2 <' + SAttValue( Chat_Msg_List , 'EHOOK_R_2' ) + '>' );
			SetSAtt( Hook^.SA , 'msg3 <' + FormatChatString( SAttValue( Chat_Msg_List , 'EHOOK_Msg3_' + BStr( Random( 5 ) + 1 ) ) ) + '>' );

		end else if cmd = '+ECRA' then begin
			{ Enemy can run away. Player will have }
			{ the option to threaten the NPC. }
			greeting := greeting + ' AddChat 3';
			SetSAtt( Hook^.SA , 'prompt3 <' + FormatChatString( SAttValue( Chat_Msg_List , 'EHOOK_P_3_' + BStr( Random( 5 ) + 1 ) ) ) + '>' );
			SetSAtt( Hook^.SA , 'result3 <' + SAttValue( Chat_Msg_List , 'EHOOK_R_3' ) + '>' );
			SetSAtt( Hook^.SA , 'msg4 <' + FormatChatString( SAttValue( Chat_Msg_List , 'EHOOK_Msg4_' + BStr( Random( 5 ) + 1 ) ) ) + '>' );
			SetSAtt( Hook^.SA , 'msg5 <' + FormatChatString( SAttValue( Chat_Msg_List , 'EHOOK_Msg5' ) ) + '>' );

		end;
	end;

	SetSAtt( Hook^.SA , 'greeting <' + greeting + '>' );
	SetSAtt( Hook^.SA , 'msg1 <' + FormatChatString( msg1 ) + '>' );
	SetSAtt( Hook^.SA , 'msg2 <' + FormatChatString( SAttValue( Chat_Msg_List , 'EHOOK_Msg2_' + BStr( Random( 5 ) + 1 ) ) ) + '>' );
	SetSAtt( Hook^.SA , 'prompt1 <' + FormatChatString( SAttValue( Chat_Msg_List , 'EHOOK_P_1_' + BStr( Random( 5 ) + 1 ) ) ) + '>' );
	SetSAtt( Hook^.SA , 'result1 <' + SAttValue( Chat_Msg_List , 'EHook_R_1' ) + '>' );

	GenerateEnemyHook := Hook;
end;

Function GenerateAllyHook( Scene,PC,NPC: GearPtr ): GearPtr;
	{ The only real purpose of this is to let the player know that }
	{ there's another mecha on his side. }
var
	Hook: GearPtr;
begin
	{ Create the gear for the hook. }
	Hook := NewGear( Nil );
	Hook^.G := GG_Persona;
	Hook^.S := NAttValue( NPC^.NA , NAG_Personal , NAS_CID );
	InitGear( Hook );

	SetSAtt( Hook^.SA , 'greeting <' + SAttValue( chat_msg_list , 'AHOOK_Greeting' ) + '>' );
	SetSAtt( Hook^.SA , 'result1 <' + SAttValue( chat_msg_list , 'AHOOK_R_1' ) + '>' );
	SetSAtt( Hook^.SA , 'Msg1 <' + FormatChatString( SAttValue( chat_msg_list , 'AHOOK_MSG1_' + BStr( Random( 3 ) + 1 ) ) ) + '>' );
	SetSAtt( Hook^.SA , 'Msg2 <' + FormatChatString( SAttValue( chat_msg_list , 'AHOOK_MSG2_' + BStr( Random( 3 ) + 1 ) ) ) + '>' );
	SetSAtt( Hook^.SA , 'Prompt1 <' + FormatChatString( SAttValue( chat_msg_list , 'AHOOK_P_1_' + BStr( Random( 5 ) + 1 ) ) ) + '>' );

	GenerateAllyHook := Hook;
end;

Function LancematesPresent( GB: GameBoardPtr ): Integer;
	{ Return the number of points worth of lancemates present. }
	{ This will determine whether or not the PC can recruit more. }
	{ Check for Team-3 gears; add +2 to the total for each mecha or }
	{ person, +1 to the total for each other master. }
var
	M: GearPtr;
	N: Integer;
begin
	M := GB^.Meks;
	N := 0;
	while M <> Nil do begin
		if ( NAttValue( M^.NA , NAG_Location , NAS_Team ) = NAV_LancemateTeam ) and GearActive( M ) then begin
			if ( M^.G = GG_Mecha ) or ( NAttValue( M^.NA , NAG_Personal , NAS_CID ) <> 0 ) then begin
				N := N + 2;
			end else begin
				N := N + 1;
			end;
		end;
		M := M^.Next;
	end;
	LancematesPresent := N;
end;

Function FindNPCByKeyWord( GB: GameBoardPtr; KW: String ): GearPtr;
	{ Attempt to locate a NPC by keyword. The keyword may be the job of the NPC, or }
	{ it may be a phrase listed in the NPC's Persona's KEYWORDS string attribute. }
	Function NPCMatchesKW( NPC: GearPtr ): Boolean;
	var
		desc: String;
		Persona: GearPtr;
	begin
		desc := SAttValue( NPC^.SA , 'JOB' );
		Persona := SeekPersona( GB , NAttValue( NPC^.NA , NAG_Personal , NAS_CID ) );
		if Persona <> Nil then desc := desc + SAttValue( Persona^.SA , 'KEYWORDS' );
		NPCMatchesKW := AStringHasBString( desc , KW );
	end;
var
	N: Integer;
	M: GearPtr;
begin
	{ Pass one: Locate all NPCs who match the keyword provided. }
	M := GB^.Meks;
	N := 0;
	while M <> Nil do begin
		if M^.G = GG_Character then begin
			if NPCMatchesKW( M ) then Inc( N );
		end;
		M := M^.Next;
	end;

	{ Pass two: Pick one at random, and select it. }
	{ ASSERT: M = Nil }
	if N > 0 then begin
		N := Random( N );
		M := GB^.Meks;
		while ( N >= 0 ) and ( M <> Nil ) do begin
			if M^.G = GG_Character then begin
				if NPCMatchesKW( M ) then begin
					Dec( N );
					if N = -1 then break;
				end;
			end;

			M := M^.Next;
		end;
	end;

	{ Return the NPC found. }
	FindNPCByKeyWord := M;
end;

initialization

	Noun_List := LoadStringList( Standard_Nouns_File );
	Phrase_List := LoadStringList( Standard_Phrases_File );
	Adjective_List := LoadStringList( Standard_Adjectives_File );
	RLI_List := LoadStringList( Standard_Rumors_File );
	Threat_List := LoadStringList( Standard_Threats_File );
	Chat_Msg_List := LoadStringList( Standard_Chatter_File );
	LoadTraitChatter;

finalization
	DisposeSAtt( Noun_List );
	DisposeSAtt( Phrase_List );
	DisposeSAtt( Adjective_List );
	DisposeSAtt( RLI_List );
	DisposeSAtt( Threat_List );
	DisposeSAtt( Chat_Msg_List );
	FreeTraitChatter;
end.