File: arenascript.pp

package info (click to toggle)
gearhead 1.310-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 10,028 kB
  • sloc: pascal: 36,375; makefile: 78
file content (4448 lines) | stat: -rw-r--r-- 141,967 bytes parent folder | download | duplicates (3)
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
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
unit arenascript;
	{ This unit holds the scripting language for GearHead. }
	{ It's pretty similar to the scripts developed for DeadCold. }
	{ Basically, certain game events will add a trigger to the }
	{ list. In the main combat procedure, if there are any pending }
	{ triggers, they will be checked against the events list to }
	{ see if anything happens. }

	{ Both the triggers and the event scripts will be stored as }
	{ normal string attributes. }

	{ This unit also handles conversations with NPCs, since those }
	{ are written using the scripting language and may use any }
	{ commands available there + a few special commands. }

{
	GearHead: Arena, a roguelike mecha CRPG
	Copyright (C) 2005 Joseph Hewitt

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

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

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

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

interface

{$IFDEF SDLMODE}
uses dos,gears,locale,sdlmenus,sdl,sdlgfx;
{$ELSE}
uses dos,gears,locale,conmenus;
{$ENDIF}

const
	NAG_ScriptVar = 0;
	Max_Plots_Per_Story = 5;

var
	{ This gear pointer will be created if a dynamic scene is requested. }
	SCRIPT_DynamicEncounter: GearPtr;

	{ **************************************** }
	{ ***  INTERACTION  GLOBAL  VARIABLES  *** }
	{ **************************************** }
	{ These variables hold information that may be needed anywhere }
	{ while interaction is taking place, but are undefined if }
	{ interaction is not taking place. }
	{ IntMenu should let procedures know whether or not interaction }
	{ is currently happening or not- if IntMenu <> Nil, we're in the }
	{ middle of a conversation and all other interaction variables }
	{ should have good values. }
	IntMenu: RPGMenuPtr;	{ Interaction Menu }
	I_PC,I_NPC: GearPtr;	{ Pointers to the PC & NPC Chara gears }
	I_Endurance: Integer;	{ How much of the PC's crap the NPC is }
		{ willing to take. When it reaches 0, the NPC says goodbye. }
	I_Rumors: SAttPtr;	{ List of rumors. }

	Grabbed_Gear: GearPtr;	{ This gear can be acted upon by }
				{ generic commands. }

	lancemate_tactics_persona: GearPtr;	{ Persona for setting lancemate tactics. }


{$IFDEF SDLMODE}
Procedure MechaSelectionMenu( GB: GameBoardPtr; LList,PC: GearPtr; Z: DynamicRect );
{$ELSE}
Procedure MechaSelectionMenu( LList,PC: GearPtr; Z: Integer );
{$ENDIF}
Procedure BrowseMemoType( GB: GameBoardPtr; Tag: String );

Function Calculate_Threat_Points( Level,Percent: Integer ): LongInt;

Function ScriptValue( var Event: String; GB: GameBoardPtr; Scene: GearPtr ): LongInt;

Function AS_GetString( Source: GearPtr; Key: String ): String;
Function ScriptMessage( msg_label: String; GB: GameBoardPtr; Source: GearPtr ): String;

Procedure InvokeEvent( Event: String; GB: GameBoardPtr; Source: GearPtr; var Trigger: String );

Procedure AddLancemate( GB: GameBoardPtr; NPC: GearPtr );
Procedure RemoveLancemate( GB: GameBoardPtr; NPC: GearPtr );

Procedure HandleInteract( GB: GameBoardPtr; PC,NPC,Interact: GearPtr );
Function TriggerGearScript( GB: GameBoardPtr; Source: GearPtr; var Trigger: String ): Boolean;
Function CheckTriggerAlongPath( var T: String; GB: GameBoardPtr; Plot: GearPtr; CheckAll: Boolean ): Boolean;
Procedure HandleTriggers( GB: GameBoardPtr );


implementation

{$IFDEF SDLMODE}
uses action,arenacfe,ability,damage,gearutil,ghchars,ghparser,ghmodule,
     ghprop,ghweapon,grabgear,interact,menugear,playwright,rpgdice,
     services,texutil,ui4gh,wmonster,sdlinfo,sdlmap,backpack;
{$ELSE}
uses action,arenacfe,ability,damage,gearutil,ghchars,ghparser,ghmodule,
     ghprop,ghweapon,grabgear,interact,menugear,playwright,rpgdice,backpack,
     services,texutil,ui4gh,wmonster,congfx,coninfo,conmap,context;
{$ENDIF}

const
	CMD_Chat = -2;
	CMD_Join = -3;
	CMD_Quit = -4;
	Debug_On: Boolean = False;

var
	script_macros,value_macros: SAttPtr;
    NeedGC: Boolean;
{$IFDEF SDLMODE}
	ASRD_InfoGear: GearPtr;
	ASRD_GameBoard: GameBoardPtr;
	ASRD_MemoMessage: String;



Procedure ArenaScriptReDraw;
	{ Redraw the combat screen for some menu usage. }
begin
	if ASRD_GameBoard <> Nil then SDLCombatDisplay( ASRD_GameBoard );
end;

Procedure MemoPageReDraw;
	{ Redraw the combat screen for some menu usage. }
begin
	if ASRD_GameBoard <> Nil then SDLCombatDisplay( ASRD_GameBoard );
	SetupMemoDisplay;
	GameMsg( ASRD_MemoMessage , ZONE_MemoText.GetRect() , InfoGreen );
end;
{$ENDIF}

{$IFDEF SDLMODE}
Procedure MechaSelectionMenu( GB: GameBoardPtr; LList,PC: GearPtr; Z: DynamicRect );
{$ELSE}
Procedure MechaSelectionMenu( LList,PC: GearPtr; Z: Integer );
{$ENDIF}
	{ Create a menu by which the player can select a mecha to use. }
var
	RPM: RPGMenuPtr;
	msg: String;
	M: GearPtr;
	N: Integer;
begin
	{ Step one - Create the menu. }
	RPM := CreateRPGMenu( MenuItem , MenuSelect , Z );
	RPM^.Mode := RPMNoCancel;
	M := LList;
	N := 1;
	while M <> Nil do begin
		if ( M^.G = GG_Mecha ) and ( NAttValue( M^.NA , NAG_Location , NAS_Team ) = NAV_DefPlayerTeam ) then begin
			msg := FullGearName( M );
			AddRPGMenuItem( RPM , msg , N );
		end;

		Inc( N );
		M := M^.Next;
	end;

	if RPM^.NumItem <> 0 then begin
{$IFDEF SDLMODE}
		ASRD_GameBoard := GB;
		ASRD_InfoGear := PC;
		N := SelectMenu( RPM , @ArenaScriptReDraw );
{$ELSE}
		N := SelectMenu( RPM );
{$ENDIF}
		if N <> -1 then begin
			{ Find the gear that was selected, and set its }
			{ pilot attribute. }
			M := RetrieveGearSib( LList , N );
			AssociatePilotMek( LList , PC , M );

			{ Print an informative message for the user. }
			DialogMsg( MsgString( 'SELECTMECHA_RESULT1' ) + GearName( M ) + MsgString( 'SELECTMECHA_RESULT2' ) );
		end;

	end else begin
		DialogMsg( MsgString( 'SELECTMECHA_NoMeks' ) );
	end;

	DisposeRPGMenu( RPM );
end;

Procedure BrowseMemoType( GB: GameBoardPtr; Tag: String );
	{ Create a list, then browse the memos based upon this }
	{ TAG type. Possible options are MEMO, NEWS, and EMAIL. }
var
	MemoList,M: SAttPtr;

	Procedure CreateMemoList( Part: GearPtr; Tag: String );
		{ Look through all gears in the structure recursively, }
		{ looking for MEMO string attributes to store in our list. }
	var
		msg: String;
	begin
		while Part <> Nil do begin
            if Part^.G <> GG_AbsolutelyNothing then begin
			    msg := SAttValue( Part^.SA , Tag );
			    if msg <> '' then StoreSAtt( MemoList , msg );
			    CreateMemoList( Part^.SubCom , Tag );
			    CreateMemoList( Part^.InvCom , Tag );
            end;
			Part := Part^.Next;
		end;
	end;

	Procedure BrowseList;
		{ Actually browse the created list. }
	var
		RPM: RPGMenuPtr;
		N,D: Integer;
	begin
		if MemoList <> Nil then begin
			SetupMemoDisplay;
			RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_MemoMenu );
			AddRPGMenuItem( RPM , MsgString( 'MEMO_Next' ) , 1 );
			AddRPGMenuItem( RPM , MsgString( 'MEMO_Prev' ) , 2 );
			AddRPGMenuKey( RPM , KeyMap[ KMC_East ].KCode , 1 );
			AddRPGMenuKey( RPM , KeyMap[ KMC_West ].KCode , 2 );
			AlphaKeyMenu( RPM );
			RPM^.Mode := RPMNoCleanup;
			N := 1;

			repeat
				M := RetrieveSAtt( MemoList , N );
{$IFDEF SDLMODE}
				ASRD_InfoGear := Nil;
				ASRD_GameBoard := GB;
				ASRD_MemoMessage := M^.Info;
				D := SelectMenu( RPM , @MemoPageRedraw );
{$ELSE}
				GameMsg( M^.Info , ZONE_MemoText , InfoGreen );
				D := SelectMenu( RPM );
{$ENDIF}

				if D = 1 then begin
					N := N + 1;
					if N > NumSAtts( MemoList ) then N := 1;
				end else if D = 2 then begin
					N := N - 1;
					if N < 1 then N := NumSAtts( MemoList );
				end;
			until D = -1;

			DisposeSAtt( MemoList );
			DisposeRPGMenu( RPM );
		end;

	end;
begin
	{ Error check first - we need the GB and the scene for this. }
	if ( GB = Nil ) or ( GB^.Scene = Nil ) then Exit;
	MemoList := Nil;
	CreateMemoList( FindRoot( GB^.Scene ) , Tag );
	if MemoList = Nil then StoreSAtt( MemoList , ReplaceHash( MsgString( 'MEMO_None' ) , LowerCase( Tag ) ) );
	BrowseList;
end;

Function YesNoMenu( GB: GameBoardPtr; Prompt,YesMsg,NoMsg: String ): Boolean;
	{ This will open up a small window in the middle of the map }
	{ display, then prompt the user for a choice. }
	{ Return TRUE if the "yes" option was selected, or FALSE if }
	{ the "no" option was selected. }
	{ This function performs no screen cleanup. }
var
	rpm: RPGMenuPtr;
	N: Integer;
begin
	RPM := CreateRPGMenu( MenuItem , MenuSelect , ZONE_YesNoMenu );
	AddRPGMenuItem( RPM , YesMsg , 1 );
	AddRPGMenuItem( RPM , NoMsg , -1 );
	RPM^.Mode := RPMNoCancel;

{$IFDEF SDLMODE}
	ASRD_InfoGear := Nil;
	ASRD_GameBoard := GB;
	ASRD_MemoMessage := Prompt;
	N := SelectMenu( RPM , @MemoPageRedraw );
{$ELSE}
	SetupYesNoDisplay;
	GameMsg( Prompt , ZONE_YesNoPrompt , InfoGreen );
	N := SelectMenu( RPM );
{$ENDIF}
	DisposeRPGMenu( RPM );

	{ Do cleanup before branching. }
    {$IFNDEF SDLMODE}
	DisplayMap( GB );
    {$ENDIF}

	YesNoMenu := N <> -1;
end;

Function SceneName( GB: GameBoardPtr; ID: Integer ): String;
	{ Find the name of the scene with the given ID. If no such }
	{ scene can be found, return a value that should let the player }
	{ know an error has been commited. }
var
	Part: GearPtr;
begin
	if ( GB = Nil ) or ( GB^.Scene = Nil ) then begin
		SceneName := 'XXX';
	end else begin
		{ Look for the scene along the subcomponents of the }
		{ adventure. This is to make sure we don't accidentally }
		{ pick a dynamic scene with the right ID. }
		Part := FindActualScene( GB , ID );
		SceneName := GearName( Part );
	end;
end;

Function FindRandomMekID( GB: GameBoardPtr; Team: Integer ): LongInt;
	{ Locate a random mek belonging to TEAM. }
var
	NumMeks,N,T,MID: Integer;
	Mek: GearPtr;
begin
	{ Start out by finding out how many meks belong to this team }
	{ anyways. }
	NumMeks := NumOperationalMasters( GB , Team );
	MID := 0;

	{ If there are actually members on this team, select one randomly. }
	if NumMeks > 0 then begin
		{ Decide what mek to take, and initialize the }
		{ search variables. }
		N := Random( NumMeks ) + 1;
		T := 0;
		Mek := GB^.Meks;

		while Mek <> Nil do begin
			if ( NAttValue( Mek^.NA , NAG_Location , NAS_Team ) = Team ) and GearOperational( Mek ) then begin
				Inc( T );
				if T = N then MID := NAttValue( Mek^.NA , NAG_EpisodeData , NAS_UID );
			end;
			Mek := Mek^.Next;
		end;
	end;

	FindRandomMekID := MID;
end;

Function FindRandomPilotID( GB: GameBoardPtr; Team: Integer ): LongInt;
	{ Locate a random pilot belonging to TEAM. }
var
	NumMeks,N,T,MID: Integer;
	Mek,P: GearPtr;
begin
	{ Start out by finding out how many meks belong to this team }
	{ anyways. }
	NumMeks := NumOperationalMasters( GB , Team );
	MID := 0;

	{ If there are actually members on this team, select one randomly. }
	if NumMeks > 0 then begin
		{ Decide what mek to take, and initialize the }
		{ search variables. }
		N := Random( NumMeks ) + 1;
		T := 0;
		Mek := GB^.Meks;

		while Mek <> Nil do begin
			if ( NAttValue( Mek^.NA , NAG_Location , NAS_Team ) = Team ) and GearOperational( Mek ) then begin
				Inc( T );
				if T = N then begin
					P := LocatePilot( Mek );
					if P <> Nil then MID := NAttValue( P^.NA , NAG_EpisodeData , NAS_UID );
				end;
			end;
			Mek := Mek^.Next;
		end;
	end;

	FindRandomPilotID := MID;
end;

Function FindRootUID( GB: GameBoardPtr; ID: LongInt ): LongInt;
	{ Find the ID of the root of the specified gear. }
var
	Part: GearPtr;
begin
	{ First, find the part being pointed to. }
	Part := LocateMekByUID( GB , ID );

	{ Locate its root. }
	if Part <> Nil then begin
		Part := FindRoot( Part );

		{ Return the root's UID. }
		FindRootUID := NAttValue( Part^.NA , NAG_EpisodeData , NAS_UID );

	{ If there was an error locating the part, return 0. }
	end else FindRootUID := 0;
end;

Function FindFacMem( ID: Integer; GB: GameBoardPtr; Scene: GearPtr ): Integer;
	{ This function will return how many NPCs belong to the }
	{ specified faction. }
	Function WorkHorse( Part: GearPtr ): Integer;
		{ This is the function which does all the real work. }
	var
		it: Integer;
	begin
		it := 0;
		while Part <> Nil do begin
			if ( Part^.G = GG_Character ) and ( NAttValue( Part^.NA , NAG_Personal , NAS_FactionID ) = ID ) then begin
				Inc( It );
			end;
			if Part^.SubCom <> Nil then it := it + WorkHorse( Part^.SubCom );
			if Part^.InvCom <> Nil then it := it + WorkHorse( Part^.InvCom );
			Part := Part^.Next;
		end;
		WorkHorse := it;
	end;
begin
	Scene := FindRoot( Scene );
	FindFacMem := WorkHorse( Scene ) + WorkHorse( GB^.Meks );
end;

Function FindFacScene( ID: Integer; Scene: GearPtr ): Integer;
	{ This function will return how many Scenes belong to the }
	{ specified faction. }
	Function WorkHorse( Part: GearPtr ): Integer;
		{ This is the function which does all the real work. }
	var
		it: Integer;
	begin
		it := 0;
		while Part <> Nil do begin
			if ( Part^.G = GG_Scene ) and ( NAttValue( Part^.NA , NAG_Personal , NAS_FactionID ) = ID ) then begin
				Inc( It );
			end;
			if Part^.SubCom <> Nil then it := it + WorkHorse( Part^.SubCom );
			if Part^.InvCom <> Nil then it := it + WorkHorse( Part^.InvCom );
			Part := Part^.Next;
		end;
		WorkHorse := it;
	end;
begin
	Scene := FindRoot( Scene );
	FindFacScene := WorkHorse( Scene );
end;

Function NumPCMeks( GB: GameBoardPtr ): Integer;
	{ Return the number of mecha belonging to team 1. }
	{ It doesn't matter if they're on the board or not, nor whether or }
	{ not they are destroyed. }
var
	M: GearPtr;
	N: Integer;
begin
	N := 0;
	if GB <> Nil then begin
		M := GB^.Meks;
		while M <> Nil do begin
			{ If this is a mecha, and it belongs to team 1, }
			{ increment the counter. }
			if ( M^.G = GG_Mecha ) and ( NAttValue( M^.NA , NAG_Location , NAS_Team ) = NAV_DefPlayerTeam ) then Inc( N );
			M := M^.Next;
		end;
	end;
	NumPCMeks := N;
end;

Function FindPCScale( GB: GameBoardPtr ): Integer;
	{ Return the scale of the PC. Generally this will be 0 if the }
	{ PC is on foot, 1 or 2 if the PC is in a mecha, unless the PC }
	{ is a storm giant or a zentradi in which case anything goes. }
var
	PC: GearPtr;
begin
	PC := GG_LocatePC( GB );
	if PC <> Nil then begin
		FindPCScale := FindRoot( PC )^.Scale;
	end else begin
		FindPCScale := 0;
	end;
end;

Function FindHostileFactions( GB: GameBoardPtr; Source: GearPtr ): Integer;
	{ COunt the number of hostile factions present in this adventure. }
	{ A hostile faction is defined as one which has the MILITARY type, }
	{ which isn't INACTIVE, and which has ACTIVE MILITARY enemies. }
	Function IsMilitaryFaction( Fac: GearPtr ): Boolean;
		{ The faction is MILITARY if it says so in its TYPE string. }
	begin
		IsMilitaryFaction := AStringHasBString( SAttValue( Fac^.SA , 'TYPE' ) , 'MILITARY' );
	end;

	Function MeetsTheRequirements( Fac: GearPtr ): Boolean;
		{ Return TRUE if the provided faction meets all the }
		{ requirements listed above, or FALSE otherwise. }
	var
		it: Boolean;
		F2: GearPtr;
		NA: NAttPtr;
		Enemies: Integer;
	begin
		it := IsMilitaryFaction( Fac ) and not FactionIsInactive( Fac );
		if it then begin
			{ Check through each of the faction's relations }
			{ to see if it has any active, military enemies. }
			NA := Fac^.NA;
			Enemies := 0;

			while NA <> Nil do begin
				{ If this numeric attribute is a faction score, }
				{ and it's an enemy, check the state of that enemy. }
				if ( NA^.G = NAG_FactionScore ) and ( NA^.V < 0 ) then begin
					F2 := GG_LocateFaction( NA^.S , GB , Source );

					if F2 <> Nil then begin
						if IsMilitaryFaction( F2 ) and not FactionIsInactive( F2 ) then Inc( Enemies );
					end;
				end;

				NA := NA^.Next;
			end;

			{ If there are no active, military enemies then }
			{ this faction doesn't count as currently hostile. }
			if Enemies = 0 then it := False;
		end;
		MeetsTheRequirements := it;
	end;
	Function CheckAlongPath( Part: GearPtr ): Integer;
		{ Check along this path of sibling gears, counting up }
		{ all the factions that meet our requirements. }
	var
		it: Integer;
	begin
		{ Initialize count to 0. }
		it := 0;

		while Part <> Nil do begin
			if ( Part^.G = GG_Faction ) and MeetsTheRequirements( Part ) then inc( it );

			it := it + CheckAlongPath( Part^.SubCom );
			it := it + CheckAlongPath( Part^.InvCom );

			Part := Part^.Next;
		end;
		CheckAlongPath := it;
	end;
begin
	if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then begin
		FindHostileFactions := CheckAlongPath( FindRoot( GB^.Scene ) );
	end else begin
		FindHostileFactions := CheckAlongPath( FindRoot( Source ) );
	end;
end;

Function Calculate_Threat_Points( Level,Percent: Integer ): LongInt;
	{ Calculate an appropriate threat value, based upon the modified }
	{ encounter level ( usually PCRep + d50 ) and the % scale factor. }
var
	it: LongInt;
begin
	if Level < 0 then Level := 0
	else if Level > 300 then Level := 300;

	{ For low level encounters, use a linear equation. }
	if Level < 42 then begin
		it := 50 + Level * 125;
	{ Higher on, switch to the quadratic. }
	{ This equation provides similar values to the standard values }
	{ previously used in the combat encounters. }
	end else begin
		it := 25 * Level * Level - 1850 * Level + 39000;
	end;

	{ Modify for the percent requested. }
	it := it * Percent;

	Calculate_Threat_Points := it;
end;

Function Calculate_Reward_Value( GB: GameBoardPtr; ThreatLevel,Percent: LongInt ): LongInt;
	{ Return an appropriate reward value, based on the listed }
	{ threat level and percent scale. }
var
	RV: LongInt;
	PC: GearPtr;
begin
	{ Calculate the base reward value. }
	RV := ThreatLevel div 100 * Percent div 100;
    if RV < 100 then RV := 100;

	{ Modify this for the PC's talents. }
	if GB <> Nil then begin
		PC := GG_LocatePC( GB );
		if HasTalent( PC , NAS_BusinessSense ) then RV := ( RV * 5 ) div 4;
	end;

	Calculate_Reward_Value := RV;
end;

Procedure InitiateMacro( var Event: String; ProtoMacro: String );
	{ Initialize the provided macro, and stick it to the head of }
	{ event. To initialize just go through it word by word, replacing }
	{ all question marks with words taken from EVENT. }
	function NeededParameters( cmd: String ): Integer;
		{ Return the number of parameters needed by this function. }
	const
		NumStandardFunctions = 15;
		StdFunName: Array [1..NumStandardFunctions] of string = (
			'-', 'GNATT', 'GSTAT', 'FACMEM', 'FACSCENE',
			'SKROLL', 'THREAT', 'REWARD', 'ESCENE', 'RANGE',
			'FXPNEEDED','WMTHREAT','*','MAPTILE','PCSKILLVAL'
		);
		StdFunParam: Array [1..NumStandardFunctions] of byte = (
			1,2,1,1,1,1,2,2,1,2,
			1,1,2,2,1
		);
	var
		it,T: Integer;
		Mac: String;
	begin
		it := 0;
		CMD := UpCase( CMD );
		{ If a hardwired function, look up the number of parameters }
		{ from the above list. }
		for t := 1 to NumStandardFunctions do begin 
			if CMD = StdFunName[ t ] then it := StdFunParam[ t ];
		end;

		{ If a macro function, derive the number of parameters }
		{ from the number of ?s in the macro. }
		if it = 0 then begin
			Mac := SAttValue( Value_Macros , Cmd );
			if Mac <> '' then begin
				for t := 1 to Length( Mac ) do begin
					if Mac[ T ] = '?' then Inc( it );
				end;
			end;
		end;
		NeededParameters := it;
	end;
	function  GetSwapText: String;
		{ Grab the swap text from EVENT. If it's a function }
		{ that we just grabbed, better grab the parameters as }
		{ well. Oh, bother... }
	var
		it,cmd: String;
		N: Integer;
	begin
		{ Grab the beginning of the swap text. }
		it := ExtractWord( Event );

		{ Check to see if it's a function. Get rid of the - first }
		{ since it'll screw up our check. }
		cmd := it;
		if ( Length( cmd ) > 1 ) and ( cmd[1] = '-' ) then DeleteFirstChar( cmd );
		N := NeededParameters( cmd );
		While N > 0 do begin
			it := it + ' ' + GetSwapText();
			Dec( N );
		end;
		GetSwapText := it;
	end;
var
	Cmd,NewMacro,LastWord: String;
begin
	NewMacro := '';
	LastWord := '';

	while ProtoMacro <> '' do begin
		cmd := ExtractWord( ProtoMacro );
		if cmd = '?' then begin
			LastWord := GetSwapText;
			cmd := LastWord;
		end else if cmd = '!' then begin
			cmd := LastWord;
		end;
		NewMacro := NewMacro + ' ' + cmd;
	end;

	Event := NewMacro + ' ' + Event;
end;

Function SV_Range( GB: GameBoardPtr; UID1,UID2: LongInt ): Integer;
	{ Locate the two gears pointed to by the UIDs, then calculate }
	{ the range between them. If one or both are NIL, or if one or }
	{ both are off the map, return 0. }
var
	M1,M2: GearPtr;
begin
	{ Error check. }
	if GB = Nil then Exit( 0 );

	M1 := LocateMekByUID( GB , UID1 );
	M2 := LocateMekByUID( GB , UID2 );

	if ( M1 <> Nil ) and OnTheMap( M1 ) and ( M2 <> Nil ) and OnTheMap( M2 ) then begin
		SV_Range := Range( GB , M1 , M2 );
	end else begin
		SV_Range := 0;
	end;
end;

Function SV_PCSkillVal( GB: GameBoardPtr; Skill: Integer ): Integer;
	{ Return the PC's base skill value. This used to be easy until those }
	{ stupid lancemates came along... Check all PCs and lancemates, and }
	{ return the highest value. }
var
	M,PC: GearPtr;
	HiSkill,T: Integer;
begin
	{ Error check. }
	if GB = Nil then Exit( 0 );

	M := GB^.Meks;
	HiSkill := 0;
	while M <> Nil do begin
		T := NAttValue( M^.NA , NAG_Location , NAS_Team );
		if GearActive( M ) and ( ( T = NAV_DefPlayerTeam ) or ( T = NAV_LancemateTeam ) ) then begin
			PC := LocatePilot( M );
			if PC <> Nil then begin
				T := NAttValue( PC^.NA , NAG_Skill , SKill );
				if T > HiSkill then HiSkill := T;
			end;
		end;

		M := M^.Next;
	end;

	SV_PCSkillVal := HiSkill;
end;

Function ScriptValue( var Event: String; GB: GameBoardPtr; Scene: GearPtr ): LongInt;
	{ Normally, numerical values will be stored as constants. }
	{ Sometimes we may want to do algebra, or use the result of }
	{ scenario variables as the parameters for commands. That's }
	{ what this function is for. }
var
	Old_Grabbed_Gear,PC: GearPtr;
	VCode,VC2: LongInt;
	SV: LongInt;
	SMsg,S2: String;
begin
	{ Save the grabbed gear, to restore it later. }
	Old_Grabbed_Gear := Grabbed_Gear;

	SMsg := UpCase(ExtractWord( Event ));
	SV := 0;

	{ If the first character is one of the value commands, }
	{ process the string as appropriate. }
	if SAttValue( Value_Macros , SMsg ) <> '' then begin
		{ Install the macro, then re-call this procedure to get }
		{ the results. }
		InitiateMacro( Event , SAttValue( Value_Macros , SMsg ) );
		SV := ScriptValue( Event , gb , scene );

	end else if ( SMsg = 'GNATT' ) then begin
		{ Get a Numeric Attribute from the currently grabbed gear. }
		VCode := ScriptValue( Event , GB , Scene );
		VC2 := ScriptValue( Event , GB , Scene );
		if Grabbed_Gear <> Nil then begin
			SV := NAttValue( Grabbed_Gear^.NA , VCode , VC2 );
		end;

	end else if ( SMsg = 'GSTAT' ) then begin
		{ Get a Numeric Attribute from the currently grabbed gear. }
		VCode := ScriptValue( Event , GB , Scene );
		if ( Grabbed_Gear <> Nil ) then begin
			if ( VCode >= 1 ) and ( VCode <= NumGearStats ) then begin
				SV := Grabbed_Gear^.Stat[ VCode ];
			end;
		end;

	end else if ( SMsg = 'FXPNEEDED' ) then begin
		{ Return how many faction XP points needed for next level. }
		VCode := ScriptValue( Event , GB , Scene );
		SV := ( VCode + 1 ) * 5;

	end else if ( SMsg = 'MAPTILE' ) then begin
		{ Return how many faction XP points needed for next level. }
		VCode := ScriptValue( Event , GB , Scene );
		VC2 := ScriptValue( Event , GB , Scene );
		if ( GB <> Nil ) and OnTheMap( VCode , VC2 ) then begin
			SV := GB^.Map[ VCode , VC2 ].terr;
		end else begin
			SV := 0;
		end;

	end else if Attempt_Gear_Grab( SMsg , Event , GB , Scene ) then begin
		{ The correct Grabbed_Gear was set by the above call, }
		{ so just recurse to find the value we want. Am I looming }
		{ closer to functional programming or something? }
		SV := ScriptValue( Event , gb , scene );

	end else if ( SMsg = 'COMTIME' ) then begin
		{ Return the current combat time. }
		if ( GB <> Nil ) then begin
			SV := GB^.ComTime;
		end;

	end else if ( SMsg = 'NEXTDAY' ) then begin
		{ Return the start of the next day. }
		if ( GB <> Nil ) then begin
			SV := GB^.ComTime + 86400 - GB^.ComTime mod 86400;
		end;

	end else if ( SMsg = 'PCSKILLVAL' ) then begin
		{ Return the PC team's highest skill value. }
		VCode := ScriptValue( Event , GB , Scene );
		SV := SV_PCSkillVal( GB , VCode );

	end else if ( SMsg = 'SCENEID' ) then begin
		{ Return the current scene's unique ID. }
		{ Only do this if we're in the real scene! Return 0 for }
		{ a temporary or otherwise fake scene. }
		if ( GB <> Nil ) and ( GB^.Scene <> Nil ) and IsSubCom( GB^.Scene ) then begin
			SV := GB^.Scene^.S;
		end;

	end else if ( SMsg = 'GSCENE' ) then begin
		{ Return the current scene's unique ID. }
		{ Only do this if we're in the real scene! Return 0 for }
		{ a temporary or otherwise fake scene. }
		if ( GB <> Nil ) and ( GB^.Scene <> Nil ) and (Grabbed_Gear <> Nil) then begin
			SV := FindGearScene( Grabbed_Gear, GB );
		end;

	end else if ( SMsg = 'FACMEM' ) then begin
		{ Return the number of members of the requested faction. }
		if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then begin
			VCode := ScriptValue( Event , GB , Scene );
			SV := FindFacMem( VCode , GB , GB^.Scene );
		end;

	end else if ( SMsg = 'FACSCENE' ) then begin
		{ Return the number of scenes controlled by this faction. }
		if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then begin
			VCode := ScriptValue( Event , GB , Scene );
			SV := FindFacScene( VCode , GB^.Scene );
		end;

	end else if ( SMsg = 'HOSTILEFACTIONS' ) then begin
		{ Return the number of hostile factions in play. }
		if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then begin
			SV := FindHostileFactions( GB , Scene );
		end;

	end else if ( SMsg = 'REACT' ) then begin
		{ Return the current reaction score between PC & NPC. }
		if ( IntMenu <> Nil ) then begin
			if GB = Nil then begin
				SV := ReactionScore( Nil , I_PC , I_NPC );
			end else begin
				SV := ReactionScore( GB^.Scene , I_PC , I_NPC );
			end;
		end;

	end else if ( SMsg = 'SKROLL' ) then begin
		{ Return a skill roll from the PC. }
		VCode := ScriptValue( Event , GB , Scene );
		if ( VCode >= 1 ) and ( VCode <= NumSkill ) then begin
			SV := RollStep( TeamSkill( GB , NAV_DefPlayerTeam , VCode ) );
		end else SV := 0;
		PC := GG_LocatePC( GB );
		if PC <> Nil then DoleSkillExperience( PC , VCode , 5 );

	end else if SMsg = 'PCMEKS' then begin
		SV := NumPCMeks( GB );

	end else if ( SMsg = 'THREAT' ) then begin
		VCode := ScriptValue( Event , GB , Scene );
		VC2 := ScriptValue( Event , GB , Scene );
		SV := Calculate_Threat_Points( VCode , VC2 );

	end else if ( SMsg = '*' ) then begin
		VCode := ScriptValue( Event , GB , Scene );
		VC2 := ScriptValue( Event , GB , Scene );
		SV := VCode * VC2;

	end else if ( SMsg = 'WMTHREAT' ) then begin
		VCode := ScriptValue( Event , GB , Scene );
		if VCode < 4 then VCOde := 4;
		SV := VCode div 2;

	end else if ( SMsg = 'REWARD' ) then begin
		VCode := ScriptValue( Event , GB , Scene );
		VC2 := ScriptValue( Event , GB , Scene );
		SV := Calculate_Reward_Value( GB , VCode , VC2 );

	end else if ( SMsg = 'RANGE' ) then begin
		VCode := ScriptValue( Event , GB , Scene );
		VC2 := ScriptValue( Event , GB , Scene );
		SV := SV_Range( GB , VCode , VC2 );

	end else if SMsg = 'PCSCALE' then begin
		SV := FindPCScale( GB );

	end else if ( SMsg = 'ESCENE' ) then begin
		{ Find out what element to find the scene for. }
		if ( Scene <> Nil ) then begin
			VCode := ExtractValue( Event );

			{ Find out what plot we're dealing with. }
			Scene := PlotMaster( Scene );

			if ( Scene <> Nil ) and ( VCode >= 1 ) and ( VCode <= NumGearStats ) then begin
				SV := ElementLocation( FindRoot( Scene ) , Scene , VCode , GB );
			end;
		end;

	end else if ( SMsg[1] = 'D' ) then begin
		{ Roll a die, return the result. }
		DeleteFirstChar( SMsg );
		Event := SMsg + ' ' + Event;
		VCode := ScriptValue( Event , GB , Scene );;
		if VCode < 1 then VCode := 1;
		SV := Random( VCode ) + 1;

	{ As a last resort, see if the first character shows up in the }
	{ scripts file. If so, use that. }
	end else if SAttValue( Value_Macros , SMsg[1] ) <> '' then begin
		{ Install the macro, then re-call this procedure to get }
		{ the results. }
		S2 := SMsg[ 1 ];
		DeleteFirstChar( SMsg );
		Event := SMsg + ' ' + Event;
		InitiateMacro( Event , SAttValue( Value_Macros , S2 ) );
		SV := ScriptValue( Event , gb , scene );

	end else if ( SMsg[1] = '?' ) and ( gb <> Nil ) then begin
		{ Return a randomly picked gear from the game board. }
		DeleteFirstChar( SMsg );
		if UpCase(SMsg[1]) = 'M' then begin
			DeleteFirstChar( SMsg );
			SV := FindRandomMekID( GB , ScriptValue( SMsg , gb , scene ) );
		end else begin
			DeleteFirstChar( SMsg );
			SV := FindRandomPilotID( GB , ScriptValue( SMsg , gb , scene ) );
		end;

	end else if ( SMsg[1] = 'T' ) and ( gb <> Nil ) then begin
		{ Return the number of gears on the provided team. }
		DeleteFirstChar( SMsg );
		VCode := ExtractValue( SMsg );
		SV := NumActiveMasters( GB , VCode );

	end else if ( SMsg[1] = '@' ) and ( gb <> Nil ) then begin
		{ Return the root gear of the gear indicated by the }
		{ rest of this expression. Return 0 if there's an error. }
		DeleteFirstChar( SMsg );
		VCode := ScriptValue( SMsg , gb , scene );
		if VCode <> 0 then SV := FindRootUID( GB , VCode )
		else SV := 0;


	end else if SMsg[1] = '-' then begin
		{ We want the negative of the value to follow. }
		DeleteFirstChar( SMsg );
		event := SMsg + ' ' + event;
		SV := -ScriptValue( event , gb , scene );

	end else begin
		{ No command was given, so this must be a constant value. }
		S2 := SMsg;
		SV := ExtractValue( SMsg );

		if ( SV = 0 ) and ( S2 <> '' ) and ( S2 <> '0' ) then begin
			DialogMsg( 'WARNING: Script value ' + S2  + ' in ' + GearName( Scene ) );
			DialogMsg( 'CONTEXT: ' + event );
		end;
	end;

	{ Restore the grabbed gear before exiting. }
	Grabbed_Gear := Old_Grabbed_Gear;

	ScriptValue := SV;
end;

Function AS_GetString( Source: GearPtr; Key: String ): String;
	{ Check the SOURCE for a SAtt with the provided KEY. }
	{ If none can be found, search the default list for SOURCE's type. }
	{ Normally, getting a string attribute could be handled simply by the }
	{ SAttValue function. But, I had some trouble with my doors getting so }
	{ #$#@%! huge, so I decided to write the function as a space-saver. }
var
	msg: String;
begin
	if Source <> Nil then begin
		msg := SAttValue( Source^.SA , Key );
		if ( msg = '' ) and ( Source^.G = GG_MetaTerrain ) and ( Source^.S >= 1 ) and ( Source^.S <= NumBasicMetaTerrain ) then begin
			msg := SAttValue( Meta_Terrain_Scripts[ Source^.S ] , Key );
		end;
	end else begin
		msg := '';
	end;
	AS_GetString := msg;
end;

Procedure AS_SetExit( GB: GameBoardPtr; RC: Integer );
	{ Several things need to be done when exiting the map. }
	{ This procedure should centralize most of them. }
begin
	{ Only process this request if we haven't already set an exit. }
	if ( GB <> Nil ) and ( not GB^.QuitTheGame ) then begin
		GB^.QuitTheGame := True;
		GB^.ReturnCode := RC;
		if GB^.Scene <> Nil then begin
			SCRIPT_Gate_To_Seek := GB^.Scene^.S;
		end;
	end;
end;

Procedure ProcessExit( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ An exit command has been received. }
begin
	AS_SetExit( GB , ScriptValue( Event , GB , Source ) );
end;

Procedure ProcessReturn( GB: GameBoardPtr );
	{ An exit command has been received. }
begin
	{ RETURN should only function if called from a dynamic scene... }
	{ anything else could be disasterous. }
	if ( GB <> Nil ) and ( GB^.Scene <> Nil ) and IsInvCom( GB^.Scene ) then begin
		if GB^.Scene^.S <> 0 then begin
			AS_SetExit( GB , GB^.Scene^.S );
			GB^.Scene^.S := 0;
		end else if not GB^.QuitTheGame then begin
			{ If the current scene = 0, and we received }
			{ a QuitTheGame order, chances are that this }
			{ is an erronious "bounced" request. }
			AS_SetExit( GB , 0 );
		end;
	end;
end;

Function FactionRankName( GB: GameBoardPtr; Source: GearPtr; FID,FRank: Integer ): String;
	{ Return the generic name of rank FRank from faction FID. }
var
	F: GearPtr;
	it: String;
begin
	{ First find the faction. }
	F := GG_LocateFaction( FID , GB , Source );

	{ Do range checking on the FRank score obtained. }
	if FRank < 0 then FRank := 0
	else if FRank > 8 then FRank := 8;

	{ If no faction was found, return the default rank title. }
	if F = Nil then begin
		it := MSgString( 'FacRank_' + BStr( FRank ) );
	end else begin
		{ First check the faction for a name there. }
		{ If the faction has no name set, use the default. }
		it := SAttValue( F^.SA , 'FacRank_' + BStr( FRank ) );
		if it = '' then it := MSgString( 'FacRank_' + BStr( FRank ) );
	end;

	FactionRankName := it;
end;

Function PCRankName( GB: GameBoardPtr; Source: GearPtr ): String;
	{ Return the name of the PC's rank with the PC's faction. }
	{ if the PC has no rank, return the basic string. }
var
	FID: Integer;
	A,F: GearPtr;
begin
	{ The PC's faction ID is located in the adventure gear, so }
	{ locate that first. }
	A := GG_LocateAdventure( GB , Source );
	if A <> Nil then begin
		{ The faction rank score is located in the faction itself. }
		{ So now, let's locate that. }
		FID := NAttValue( A^.NA , NAG_Personal , NAS_FactionID );
		F := GG_LocateFaction( FID , GB , Source );
		if F <> Nil then begin
			{ Call the general Faction Rank function. }
			PCRankName := FactionRankName( GB , Source , FID , NAttValue( F^.NA , NAG_Experience , NAS_FacLevel ) );
		end else begin
			{ No faction found. Return the default value. }
			PCRankName := MsgSTring( 'FACRANK_0' );
		end;
	end else begin
		{ Adventure not found. Return default "peon". }
		PCRankName := MsgSTring( 'FACRANK_0' );
	end;
end;

Procedure FormatMessageString( var msg: String; gb: GameBoardPtr; Scene: GearPtr );
	{ There are a number of formatting commands which may be used }
	{ in an arenascript message string. }
var
	S0,S1,w: String;
	ID,ID2: LongInt;
	Part: GearPtr;
begin
	S0 := msg;
	S1 := '';

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

		if UpCase( W ) = '\MEK' then begin
			{ Insert the name of a specified gear. }
			ID := ScriptValue( S0 , GB , Scene );
			Part := LocateMekByUID( GB , ID );
			if Part <> Nil then begin
				W := GearName( Part );
			end else begin
				W := 'ERROR!!!';
			end;

		end else if UpCase( W ) = '\PILOT' then begin
			{ Insert the name of a specified gear. }
			ID := ScriptValue( S0 , GB , Scene );
			Part := LocateMekByUID( GB , ID );
			if Part <> Nil then begin
				W := PilotName( Part );
			end else begin
				W := 'ERROR!!!';
			end;

		end else if UpCase( W ) = '\SOURCE' then begin
			{ Insert the name of the current source. }
			if Scene <> Nil then begin
				W := GearName( Scene );
			end else begin
				W := 'SOURCE-ERROR!!!';
			end;

		end else if UpCase( W ) = '\ELEMENT' then begin
			{ Insert the name of a specified plot element. }
			ID := ScriptValue( S0 , GB , Scene );
			Part := PlotMaster( Scene );
			if Part <> Nil then begin
				W := ElementName( FindRoot( Scene ) , Part , ID , GB );
			end;

		end else if UpCase( W ) = '\NARRATIVE' then begin
			{ Insert the name of a specified plot element. }
			ID := ScriptValue( S0 , GB , Scene );
			Part := StoryMaster( Scene );
			if Part <> Nil then begin
				W := ElementName( FindRoot( Scene ) , Part , ID , GB );
			end;

		end else if UpCase( W ) = '\PERSONA' then begin
			{ Insert the name of a specified persona. }
			ID := ScriptValue( S0 , GB , Scene );
			Part := GG_LocateNPC( ID , GB , Scene );
			W := GEarName( Part );

		end else if UpCase( W ) = '\SCENE' then begin
			{ Insert the name of a specified scene. }
			ID := ScriptValue( S0 , GB , Scene );
			W := SceneName( GB , ID );

		end else if UpCase( W ) = '\VAL' then begin
			{ Insert the value of a specified variable. }
			ID := ScriptValue( S0 , GB , Scene );
			W := BStr( ID );

		end else if UpCase( W ) = '\PC' then begin
			{ The name of the PC. }
			W := GearName( LocatePilot( GG_LocatePC( GB ) ) );

		end else if UpCase( W ) = '\SPR' then begin
			{ Object Pronoun }
			ID := ScriptValue( S0 , GB , Scene );
			Part := GG_LocateNPC( ID , GB , Scene );
			if Part <> Nil then begin
				W := MsgString( 'SPR_' + BStr( NAttValue( Part^.NA , NAG_CharDescription , NAS_Gender ) ) );
			end else begin
				W := 'it';
			end;

		end else if UpCase( W ) = '\OPR' then begin
			{ Object Pronoun }
			ID := ScriptValue( S0 , GB , Scene );
			Part := GG_LocateNPC( ID , GB , Scene );
			if Part <> Nil then begin
				W := MsgString( 'OPR_' + BStr( NAttValue( Part^.NA , NAG_CharDescription , NAS_Gender ) ) );
			end else begin
				W := 'it';
			end;

		end else if UpCase( W ) = '\PPR' then begin
			{ Object Pronoun }
			ID := ScriptValue( S0 , GB , Scene );
			Part := GG_LocateNPC( ID , GB , Scene );
			if Part <> Nil then begin
				W := MsgString( 'PPR_' + BStr( NAttValue( Part^.NA , NAG_CharDescription , NAS_Gender ) ) );
			end else begin
				W := 'its';
			end;

		end else if UpCase( W ) = '\RANK' then begin
			{ The faction rank of the PC. }
			W := PCRankName( GB , Scene );

		end else if UpCase( W ) = '\FACRANK' then begin
			{ A generic faction rank, not nessecarilt belonging }
			{ to the PC. }
			ID := ScriptValue( S0 , GB , Scene );
			ID2 := ScriptValue( S0 , GB , Scene );

			W := FactionRankName( GB , Scene , ID , ID2 );

		end else if UpCase( W ) = '\DATE' then begin
			ID := ScriptValue( S0 , GB , Scene );

			W := TimeString( ID );
		end;

		if W = '' then begin
			S1 := S1 + ' ';
        end else if S1 = '' then begin
			S1 := W;
        end else if IsPunctuation( W[1] ) or ( S1[Length(S1)] = '$' ) or ( S1[Length(S1)] = '@' ) then begin
			S1 := S1 + W;
		end else begin
			S1 := S1 + ' ' + W;
		end;

	end;

	msg := S1;
end;

Function ConditionAccepted( Event: String; gb: GameBoardPtr; Source: GearPtr ): Boolean;
	{ Run a conditional script. }
	{ If it returns 'ACCEPT', this function returns true. }
var
	T: String;	{ The trigger to be used. }
begin
	{ Error check - an empty condition is always true. }
	if Event = '' then Exit( True );

	{ Generate the trigger. }
	T := 'null';

	{ Execute the provided event. }
	InvokeEvent( Event , GB , Source , T );

	{ If the trigger was changed, that counts as a success. }
	ConditionAccepted := T = 'ACCEPT';
end;

Function ScriptMessage( msg_label: String; GB: GameBoardPtr; Source: GearPtr ): String;
	{ Retrieve and format a message from the source. }
var
	N,T: Integer;
	C,msg: String;
	MList,M: SAttPtr;
begin
	{ Create the list of possible strings. }
	MList := Nil;
	C := AS_GetString( Source , 'C' + msg_label );

	{ The master condition must be accepted in order to continue. }
	if ConditionAccepted( C , GB , Source ) and ( Source <> Nil ) then begin
		msg := AS_GetString( Source , msg_label );
		if msg <> '' then StoreSAtt( MList , msg );

		msg := msg_label + '_';
		N := NumHeadMatches( msg , Source^.SA );
		for t := 1 to N do begin
			M := FindHeadMatch( msg , Source^.SA , T);
			C := SAttValue( Source^.SA , 'C' + RetrieveAPreamble( M^.info ) );
			if ConditionAccepted( C , GB , Source ) then begin
				StoreSAtt( MList , RetrieveAString( M^.Info ) );
			end;
		end;
	end;

	{ If any messages were found, pick one. }
	if MList <> Nil then begin
		msg := SelectRandomSAtt( MList )^.Info;
		DisposeSAtt( MList );
		FormatMessageString( msg , gb , source );
	end else begin
		msg := '';
	end;

	ScriptMessage := Msg;
end;

Function GetTheMessage( head: String; idnum: Integer; GB: GameBoardPtr; Scene: GearPtr ): String;
	{ Just call the SCRIPTMESSAGE with the correct label. }
begin
	GetTheMessage := ScriptMessage( head + BStr( idnum ) , GB , Scene );
end;

Procedure ProcessPrint( var Event: String; GB: GameBoardPtr; Scene: GearPtr );
	{ Locate and then print the specified message. }
var
	msg: String;
	id: Integer;
begin
	id := ScriptValue( Event , GB , Scene );
	msg := getTheMessage( 'msg', id , GB , Scene );
	if msg <> '' then DialogMsg( msg );
end;

Procedure ProcessAlert( var Event: String; GB: GameBoardPtr; Scene: GearPtr );
	{ Locate and then print the specified message. }
var
	id: Integer;
	msg: String;
begin
	id := ScriptValue( Event , GB , Scene );
	msg := getTheMessage( 'msg', id , GB , Scene );
	if msg <> '' then begin
		YesNoMenu( GB , msg , '' , '' );
        {$IFNDEF SDLMODE}
		GFCombatDisplay( GB );
        {$ENDIF}
	end;
end;

Procedure ProcessMemo( var Event: String; GB: GameBoardPtr; Scene: GearPtr );
	{ Locate and then store the specified message. }
var
	id: Integer;
	msg: String;
begin
	id := ScriptValue( Event , GB , Scene );
	msg := getTheMessage( 'msg', id , GB , Scene );
	if ( Scene <> Nil ) then SetSAtt( Scene^.SA , 'MEMO <' + msg + '>' );
end;

Procedure ProcessHistory( var Event: String; GB: GameBoardPtr; Scene: GearPtr );
	{ Locate and then store the specified message. }
var
	id: Integer;
	msg: String;
	Adv: GearPtr;
begin
	id := ScriptValue( Event , GB , Scene );
	msg := getTheMessage( 'msg' , id , GB , Scene );
	Adv := GG_LocateAdventure( GB , Scene );
	if ( msg <> '' ) and ( Adv <> Nil ) then AddSAtt( Adv^.SA , 'HISTORY' , msg );
end;

Procedure ProcessVictory( GB: GameBoardPtr );
	{ Sum up the entire campaign in a list of SAtts, then print to }
	{ a file. }
const
	InvStr = '+';
	SubStr = '>';
var
	VList,SA: SAttPtr;
	PC,Fac,Adv: GearPtr;
	T,V: LongInt;
	msg,fname: String;
	Procedure CheckAlongPath( Part: GearPtr; TabPos,Prefix: String );
		{ CHeck along the path specified, adding info to }
		{ the victory list. }
	var
		msg: String;
	begin
		while Part <> Nil do begin
			if ( Part^.G <> GG_AbsolutelyNothing ) then begin
				StoreSAtt( VList , tabpos + prefix + GearName( Part ) );
				msg := ExtendedDescription( Part );
				if msg <> '' then StoreSAtt( VList , tabpos + ' ' + msg );
			end;
			if Part^.G <> GG_Cockpit then begin
				CheckAlongPath( Part^.InvCom , TabPos + '  ' , InvStr );
				CheckAlongPath( Part^.SubCom , TabPos + '  ' , SubStr );
			end;
			Part := Part^.Next;
		end;
	end;{CheckAlongPath}
begin
	{ Initialize our list to NIL. }
	VList := Nil;

	DialogMsg( MsgString( 'HISTORY_AnnounceVictory' ) );
	EndOfGameMoreKey;

	{ Locate the PC, add PC-specific information. }
	PC := LocatePilot( GG_LocatePC( GB ) );
	if PC <> Nil then begin
		{ Store the  name. }
		fname := GearName( PC );
		StoreSAtt( VList , fname );
		StoreSAtt( VList , JobAgeGenderDesc( PC ) );
		StoreSAtt( VList , TimeString( GB^.ComTime ) );
		StoreSAtt( VList , ' ' );


		{ Store the stats. }
		for t := 1 to 8 do begin
			msg := StatName[ t ];
			while Length( msg ) < 20 do msg := msg + ' ';
			msg := msg + BStr( PC^.Stat[ T ] );
			V := ( PC^.Stat[ T ] + 2 ) div 3;
			if V > 7 then V := 7;
			msg := msg + '  (' + MsgString( 'STATRANK' + BStr( V ) ) + ')';
			StoreSAtt( VList , msg );
		end;
		StoreSAtt( VList , ' ' );

		{ Add info on the PC's XP and credits. }
		msg := MsgString( 'INFO_XP' );
		V := NAttVAlue( PC^.NA , NAG_Experience , NAS_TotalXP );
		msg := msg + ' ' + BStr( V );
		StoreSAtt( VList , msg );

		msg := MsgString( 'INFO_XPLeft' );
		V := V - NAttVAlue( PC^.NA , NAG_Experience , NAS_SpentXP );
		msg := msg + ' ' + BStr( V );
		StoreSAtt( VList , msg );

		msg := MsgString( 'INFO_Credits' );
		V := NAttVAlue( PC^.NA , NAG_Experience , NAS_Credits );
		msg := msg + ' ' + BStr( V );
		StoreSAtt( VList , msg );

		{ Store the faction and rank. }
		Fac := GG_LocateFaction( NAttValue( PC^.NA , NAG_Personal , NAS_FactionID ) , GB , Nil );
		if Fac <> Nil then begin
			msg := ReplaceHash( MsgString( 'HISTORY_FACTION' ) , PCRankName( GB , Nil ) );
			msg := ReplaceHash( msg , GearName( Fac ) );
			StoreSAtt( VList , msg );
			StoreSAtt( VList , ' ' );
		end;

		{ Store the personality traits. }
		for t := 1 to Num_Personality_Traits do begin
			V := NATtValue( PC^.NA , NAG_CharDescription , -T );
			if V <> 0 then begin
				Msg := ReplaceHash( MsgString( 'HISTORY_Traits' ) , PersonalityTraitDesc( T , V ) );
				Msg := ReplaceHash( msg , BStr( Abs( V ) ) );
				StoreSAtt( VList , msg );
			end;
		end;
		StoreSAtt( VList , ' ' );

		{ Store the talents. }
		V := 0;
		for t := 1 to NumTalent do begin
			if HasTalent( PC , T ) then begin
				msg := MsgString( 'TALENT' + BStr( T ) );
				StoreSAtt( VList , msg );
				msg := '  ' + MsgString( 'TALENTDESC' + BStr( T ) );
				StoreSAtt( VList , msg );
				inc( V );
			end;
		end;
		if V > 0 then StoreSAtt( VList , ' ' );

		{ Store the skill ranks. }
		for t := 1 to NumSkill do begin
			V := NATtValue( PC^.NA , NAG_Skill , T );
			if V > 0 then begin
				Msg := ReplaceHash( MsgString( 'HISTORY_Skills' ) , SkillMan[ T ].Name );
				Msg := ReplaceHash( msg , BStr( V ) );
				Msg := ReplaceHash( msg , BStr( SkillValue( PC , T ) ) );
				StoreSAtt( VList , msg );
			end;
		end;
		StoreSAtt( VList , ' ' );

		{ Store info on the PC's body and equipment. }
		CheckAlongPath( PC^.InvCom , '  ' , '+' );
		CheckAlongPath( PC^.SubCom , '  ' , '>' );
		StoreSAtt( VList , ' ' );

	end else begin
		{ No PC found, so filename will be "out.txt". }
		fname := 'out';
	end;

	Adv := FindRoot( GB^.Scene );
	if Adv <> Nil then begin
		{ Once the PC wins, unlock the adventure. }
		Adv^.S := 1;
		SA := Adv^.SA;
		while SA <> Nil do begin
			if UpCase( Copy( SA^.Info , 1 , 7 ) ) = 'HISTORY' then begin
				StoreSAtt( VList , RetrieveAString( SA^.Info ) );
			end;
			SA := SA^.Next;

		end;
	end;

	{ Add info on the PC's mechas. }
	PC := GB^.Meks;
	while PC <> Nil do begin
		if ( NAttValue( PC^.NA , NAG_Location , NAS_Team ) = NAV_DefPlayerTeam ) and ( PC^.G = GG_Mecha ) then begin
			StoreSAtt( VList , FullGearName( PC ) );

			CheckAlongPath( PC^.InvCom , '  ' , '+' );
			CheckAlongPath( PC^.SubCom , '  ' , '>' );

			StoreSAtt( VList , ' ' );
		end;
		PC := PC^.Next;
	end;

	SaveStringList( GetEnv( 'HOME' ) + '/.gearhead/' + FName + '.txt' , VList );
    {$IFDEF SDLMODE}
    ASRD_GameBoard := GB;
    MoreText( VList, 1 , @ArenaScriptRedraw );
    {$ELSE}
	MoreText( VList , 1 );
    {$ENDIF}
	DisposeSAtt( VList );
    {$IFNDEF SDLMODE}
	GFCombatDisplay( GB );
    {$ENDIF}
end;

Procedure ProcessNews( var Event: String; GB: GameBoardPtr; Scene: GearPtr );
	{ Locate and then store the specified message. }
var
	msg: String;
	id: Integer;
begin
	id := ScriptValue( Event , GB , Scene );
	msg := getTheMessage( 'msg' , id , GB , Scene );
	if ( msg <> '' ) and ( Scene <> Nil ) then SetSAtt( Scene^.SA , 'NEWS <' + msg + '>' );
end;

Procedure ProcessEMail( var Event: String; GB: GameBoardPtr; Scene: GearPtr );
	{ Locate and then store the specified message. }
var
	msg: String;
	PC: GearPtr;
	id: Integer;
begin
	id := ScriptValue( Event , GB , Scene );
	msg := getTheMessage( 'msg' , id , GB , Scene );
	if ( msg <> '' ) and ( Scene <> Nil ) then SetSAtt( Scene^.SA , 'EMAIL <' + msg + '>' );
	PC := GG_LocatePC( GB );
	if ( PC <> Nil ) and HasPCommCapability( PC , PCC_EMail ) then DialogMsg( MsgString( 'AS_EMail' ) );
end;

Procedure ProcessValueMessage( var Event: String; GB: GameBoardPtr; Scene: GearPtr );
	{ Locate and then print the specified message. }
var
	msg: String;
	V: LongInt;
begin
	{ FInd the message we're supposed to print. }
	msg := ExtractWord( Event );
	msg := MsgString( msg );

	{ Find the value to insert. }
	V := ScriptValue( Event , GB , Scene );

	{ Insert the value. }
	msg := ReplaceHash( msg , BStr( V ) );

	if msg <> '' then DialogMsg( msg );
end;

Procedure ProcessSay( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Locate and then print the specified message. }
var
	id: Integer;
	msg: String;
begin
	{ Error check- if not in a conversation, call the PRINT }
	{ routine instead. }
	if IntMenu = Nil then begin
		ProcessPrint( Event , GB , Source );
		Exit;
	end;

	id := ScriptValue( Event , GB , Source );
	msg := getTheMessage( 'msg' , id , GB , Source );
	if msg <> '' then begin
{$IFDEF SDLMODE}
		CHAT_Message := msg;
{$ELSE}
		GameMsg( msg , ZONE_InteractMsg , InfoHiLight );
{$ENDIF}
	end;
end;

Procedure ProcessAddChat( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Add a new item to the IntMenu. }
var
	N: Integer;
	Msg: String;
begin
	{ Error check - this command can only work if the IntMenu is }
	{ already allocated. }
	if ( IntMenu <> Nil ) and ( Source <> Nil ) then begin
		{ First, determine the prompt number. }
		N := ScriptValue( Event , GB , Source );

		msg := getthemessage( 'PROMPT' , N , GB , Source );
		DeleteWhiteSpace( msg );
		if Msg <> '' then begin
			AddRPGMenuItem( IntMenu , Msg , N );
			RPMSortAlpha( IntMenu );
		end;
	end;
end;

Procedure ProcessSayAnything;
	{ Print a random message in the interact message area. }
begin
{$IFDEF SDLMODE}
	CHAT_Message := IdleChatter;
{$ELSE}
	GameMsg( IdleChatter , ZONE_InteractMsg , InfoHiLight );
{$ENDIF}
end;

Procedure ProcessGSetNAtt( var Event: String; GB: GameBoardPtr; Scene: GearPtr );
	{ The script is going to assign a value to one of the scene }
	{ variables. }
var
	G,S: Integer;
	V: LongInt;
begin
	{ Find the variable ID number and the value to assign. }
	G := ScriptValue( event , GB , scene );
	S := ScriptValue( event , GB , scene );
	V := ScriptValue( event , GB , scene );
	if Debug_On then dialogmsg( 'GAddNAtt: ' + GearName( Grabbed_Gear ) + ' ' + BStr( G ) + '/' + BStr( S ) + '/' + BStr( V ) );

	if Grabbed_Gear <> Nil then SetNAtt( Grabbed_Gear^.NA , G , S , V );
end;

Procedure ProcessGAddNAtt( var Event: String; GB: GameBoardPtr; Scene: GearPtr );
	{ The script is going to add a value to one of the scene }
	{ variables. }
var
	G,S: Integer;
	V: LongInt;
begin
	{ Find the variable ID number and the value to assign. }
	G := ScriptValue( event , GB , scene );
	S := ScriptValue( event , GB , scene );
	V := ScriptValue( event , GB , scene );
	if Debug_On then dialogmsg( 'GAddNAtt: ' + GearName( Grabbed_Gear ) + ' ' + BStr( G ) + '/' + BStr( S ) + '/' + BStr( V ) );

	if Grabbed_Gear <> Nil then AddNAtt( Grabbed_Gear^.NA , G , S , V );
end;

Procedure ProcessGSetStat( var Event: String; GB: GameBoardPtr; Scene: GearPtr );
	{ The script is going to add a value to one of the scene }
	{ variables. }
var
	Slot,Value: Integer;
begin
	{ Find the variable ID number and the value to assign. }
	Slot := ScriptValue( event , GB , scene );
	Value := ScriptValue( event , GB , scene );

	if Grabbed_Gear <> Nil then begin
		Grabbed_Gear^.Stat[ Slot ] := Value;
		ResizeCharacter(Grabbed_Gear);
	end;
end;

Procedure ProcessGAddStat( var Event: String; GB: GameBoardPtr; Scene: GearPtr );
	{ The script is going to add a value to one of the scene }
	{ variables. }
var
	Slot,Value: Integer;
begin
	{ Find the variable ID number and the value to assign. }
	Slot := ScriptValue( event , GB , scene );
	Value := ScriptValue( event , GB , scene );

	if Grabbed_Gear <> Nil then begin
		Grabbed_Gear^.Stat[ Slot ] := Grabbed_Gear^.Stat[ Slot ] + Value;
		ResizeCharacter(Grabbed_Gear);
	end;
end;

Procedure ProcessGSetSAtt( var Event: String; Source: GearPtr );
	{ Store a string attribute in the grabbed gear. }
var
	Key,Info: String;
begin
	Key := ExtractWord( Event );
	Info := ExtractWord( Event );
	if Source <> Nil then Info := AS_GetString( Source , Info );
	if Grabbed_Gear <> Nil then SetSAtt( Grabbed_Gear^.SA , Key + ' <' + Info + '>' );
end;

Procedure IfSuccess( var Event: String );
	{ An IF call has generated a "TRUE" result. Just get rid of }
	{ any ELSE clause that the event string might still be holding. }
var
	cmd: String;
begin
	{ Extract the next word from the script. }
	cmd := ExtractWord( Event );

	{ If the next word is ELSE, we have to also extract the label. }
	{ If the next word isn't ELSE, better re-assemble the line... }
	if UpCase( cmd ) = 'ELSE' then ExtractWord( Event )
	else Event := cmd + ' ' + Event;
end;

Procedure IfFailure( var Event: String; Scene: GearPtr );
	{ An IF call has generated a "FALSE" result. See if there's }
	{ a defined ELSE clause, and try to load the next line. }
var
	cmd: String;
begin
	{ Extract the next word from the script. }
	cmd := ExtractWord( Event );

	if UpCase( cmd ) = 'ELSE' then begin
		{ There's an else clause. Attempt to jump to the }
		{ specified script line. }
		cmd := ExtractWord( Event );
		Event := AS_GetString( Scene , CMD );

	end else begin
		{ There's no ELSE clause. Just cease execution of this }
		{ line by setting it to an empty string. }
		Event := '';
	end;
end;

Procedure ProcessIfGInPlay( var Event: String; Source: GearPtr );
	{ Return true if the Grabbed_Gear is on the map and operational. }
	{ Return false otherwise. }
begin
	if ( Grabbed_Gear <> Nil ) and OnTheMap( Grabbed_Gear ) and GearOperational( Grabbed_Gear ) then begin
		IfSuccess( Event );
	end else begin
		IfFailure( Event , Source );
	end;
end;

Procedure ProcessIfGOK( var Event: String; Source: GearPtr );
	{ If the grabbed gear is OK, count as true. If it is destroyed, }
	{ or if it can't be found, count as false. }
begin
	if ( Grabbed_Gear <> Nil ) and NotDestroyed( Grabbed_Gear ) then begin
		IfSuccess( Event );
	end else IfFailure( Event , Source );
end;

Procedure ProcessIfGExists( var Event: String; Source: GearPtr );
	{ If the grabbed gear _is_, count as true. If it can't be found, count as false. }
begin
	if ( Grabbed_Gear <> Nil ) then begin
		IfSuccess( Event );
	end else IfFailure( Event , Source );
end;

Procedure ProcessIfGSexy( var Event: String; gb: GameBoardPtr; Source: GearPtr );
	{ If the grabbed gear is sexy to the PC, count as true. If it is not, }
	{ or if it can't be found, count as false. }
var
	PC: GearPtr;
begin
	PC := GG_LOcatePC( GB );
	if ( Grabbed_Gear <> Nil ) and ( PC <> Nil ) and IsSexy( PC , Grabbed_Gear ) then begin
		IfSuccess( Event );
	end else IfFailure( Event , Source );
end;

Procedure ProcessIfGArchEnemy( var Event: String; gb: GameBoardPtr; Source: GearPtr );
	{ If the grabbed gear is an enemy of the PC, or belongs to a faction that's }
	{ an enemy of the PC, count as true. }
var
	Adv: GearPtr;
begin
	Adv := GG_LOcateAdventure( GB , Source );
	if ( Grabbed_Gear <> Nil ) and ( Adv <> Nil ) and IsArchEnemy( Adv , Grabbed_Gear ) then begin
		IfSuccess( Event );
	end else IfFailure( Event , Source );
end;

Procedure ProcessIfGArchAlly( var Event: String; gb: GameBoardPtr; Source: GearPtr );
	{ If the grabbed gear is an ally of the PC, or belongs to a faction that's }
	{ an ally of the PC, count as true. }
var
	Adv: GearPtr;
begin
	Adv := GG_LOcateAdventure( GB , Source );
	if ( Grabbed_Gear <> Nil ) and ( Adv <> Nil ) and IsArchAlly( Adv , Grabbed_Gear ) then begin
		IfSuccess( Event );
	end else IfFailure( Event , Source );
end;

Procedure ProcessIfFaction( var Event: String; gb: GameBoardPtr; Source: GearPtr );
	{ Check to see if the requested faction is active or not. }
var
	FID: Integer;
	Fac: GearPtr;
begin
	{ Locate the requested Faction ID, and from there locate }
	{ the faction gear itself. }
	FID := ScriptValue( Event , GB , Source );
	Fac := GG_LocateFaction( FID , GB , Source );

	{ If the faction was found, see whether or not it's active. }
	if Fac <> Nil then begin
		if FactionIsInactive( Fac ) then IfFailure( Event , Source )
		else IfSuccess( Event );

	{ If said faction cannot be found, it counts as a failure. }
	end else IfFailure( Event , Source );
end;

Procedure ProcessIfStoryless( var Event: String; Source: GearPtr );
	{ Return true if the SOURCE has no story linked. }
	{ Return false otherwise. }
var
	story: GearPtr;
begin
	if Source <> Nil then begin
		story := Source^.InvCom;
		while ( story <> Nil ) and ( story^.G <> GG_Story ) do story := story^.Next;

		if Story = Nil then begin
			IfSuccess( Event );
		end else begin
			IfFailure( Event , Source );
		end;
	end else begin
		IfFailure( Event , Source );
	end;
end;

Procedure ProcessIfEqual( var Event: String; gb: GameBoardPtr; Source: GearPtr );
	{ Two values are supplied as the arguments for this procedure. }
	{ If they are equal, that's a success. }
var
	a,b: LongInt;
begin
	{ Determine the two values. }
	A := ScriptValue( Event , gb , Source );
	B := ScriptValue( Event , gb , Source );

	if A = B then IfSuccess( Event )
	else IfFailure( Event , Source );
end;

Procedure ProcessIfNotEqual( var Event: String; gb: GameBoardPtr; Source: GearPtr );
	{ Two values are supplied as the arguments for this procedure. }
	{ If they are not equal, that's a success. }
var
	a,b: LongInt;
begin
	{ Determine the two values. }
	A := ScriptValue( Event , gb , Source );
	B := ScriptValue( Event , gb , Source );

	if A <> B then IfSuccess( Event )
	else IfFailure( Event , Source );
end;

Procedure ProcessIfGreater( var Event: String; gb: GameBoardPtr; Source: GearPtr );
	{ Two values are supplied as the arguments for this procedure. }
	{ If A > B, that's a success. }
var
	a,b: LongInt;
begin
	{ Determine the two values. }
	A := ScriptValue( Event , gb , Source );
	B := ScriptValue( Event , gb , Source );

	if A > B then IfSuccess( Event )
	else IfFailure( Event , Source );
end;

Procedure ProcessIfKeyItem( var Event: String; gb: GameBoardPtr; Source: GearPtr );
	{ Process TRUE if the specified key item is in the posession of the PC. }
	{ We'll define this as being in the posession of any member of team }
	{ one... Process FALSE if it isn't. }
var
	NID: Integer;
	FoundTheItem: Boolean;
	PC: GearPtr;
begin
	{ Start by assuming FALSE, then go looking for it. }
	FoundTheItem := False;

	{ Find out what Key Item we're looking for. }
	NID := ScriptValue( Event , GB , Source );

	if ( GB <> Nil ) and ( NID <> 0 ) then begin
		{ Search through every gear on the map. }
		PC := GB^.Meks;

		while PC <> Nil do begin
			{ If this gear belongs to the player team, check it }
			{ for the wanted item. }
			if NAttValue( PC^.NA , NAG_Location , NAS_Team ) = NAV_DefPlayerTeam then begin
				{ Set FOUNDTHEITEM to TRUE if the specified key item }
				{ is the PC gear itself, if it's in the subcoms of the PC, }
				{ or if it's somewhere in the inventory of the PC. }
				if NAttValue( PC^.NA , NAG_Narrative , NAS_NID ) = NID then FoundTheItem := True
				else if SeekGearByIDTag( PC^.SubCom , NAG_Narrative , NAS_NID , NID ) <> Nil then FoundTheItem := True
				else if SeekGearByIDTag( PC^.InvCom , NAG_Narrative , NAS_NID , NID ) <> Nil then FoundTheItem := True;
			end;

			{ Move to the next gear to check. }
			PC := PC^.Next;
		end;
	end;

	{ Finally, do something appropriate depending upon whether or not }
	{ the item was found. }
	if FoundTheItem then IfSuccess( Event )
	else IfFailure( Event , Source );
end;

Procedure ProcessIfYesNo( var Event: String; gb: GameBoardPtr; Source: GearPtr );
	{ Two values are supplied as the arguments for this procedure. }
	{ If they are equal, that's a success. }
var
	Desc,YesPrompt,NoPrompt: String;
	it: Boolean;
	ID: Integer;
begin
	{ Find all the needed messages. }
	id := ScriptValue( Event , GB , Source );
	Desc := GetTheMessage( 'msg' , id , GB , Source );
	id := ScriptValue( Event , GB , Source );
	YesPrompt := GetTheMessage( 'msg' , id , GB , Source );
	id := ScriptValue( Event , GB , Source );
	NoPrompt := GetTheMessage( 'msg' , id , GB , Source );

	it := YesNoMenu( GB , Desc , YesPrompt , NoPrompt );

	if it then IfSuccess( Event )
	else IfFailure( Event , Source );
end;

Procedure ProcessIfScene( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Return TRUE if the current scene matches the provided }
	{ description, or FALSE otherwise. }
var
	Desc: String;
begin
	Desc := ExtractWord( Event );
	if Source <> Nil then Desc := AS_GetString( Source , Desc );

	if ( GB <> Nil ) and ( GB^.Scene <> Nil ) and PartMatchesCriteria( SceneDesc( GB^.Scene ) , Desc ) then begin
		IfSuccess( Event );
	end else begin
		IfFailure( Event , Source );
	end;
end;

Procedure ProcessIfNoObjections( var Event: String; gb: GameBoardPtr; Source: GearPtr );
	{ Run a trigger through the narrative gears. }
	{ If none of them BLOCK it, then count the result as TRUE. }
var
	T: String;	{ The trigger to be used. }
	Adv: GearPtr;
begin
	{ Generate the trigger, which is in the same format as for COMPOSE. }
	{ It's a trigger label plus a numeric value. }
	T := ExtractWord( Event );
	T := T + BStr( ScriptValue( Event, GB, Source ) );

	{ Check the trigger along the adventure's invcoms, }
	{ where all the narrative components should be located. }
	Adv := GG_LocateAdventure( GB , Source );
	if Adv <> Nil then begin
		CheckTriggerAlongPath( T, GB, Adv^.InvCom , False );
	end;

	{ If the trigger wasn't blocked, that counts as a success. }
	if T <> '' then IfSuccess( Event )
	else IfFailure( Event , Source );
end;

Procedure ProcessTeamOrders( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ This procedure is used to assign a behavior type to }
	{ every master unit on the designated team. }
const
	OrderParams: Array [0..NumAITypes] of Byte = (
		0,2,1,0,0,1
	);
var
	Team: Integer;
	OrderName: String;
	T,OrderCode: Integer;
	Mek: GearPtr;
	P: Array [1..2] of Integer;
begin
	{ Record the team number. }
	Team := ScriptValue( Event , gb , Source );

	{ Figure out what order we're supposed to be assigning. }
	OrderName := UpCase( ExtractWord( Event ) );
	OrderCode := -1;
	for t := 0 to NumAITypes do begin
		if OrderName = AI_Type_Label[ t ] then OrderCode := T;
	end;

	{ If a valid order was received, process it. }
	if OrderCode > -1 then begin
		for t := 1 to OrderParams[ OrderCode ] do P[T] := ScriptValue( Event , gb , Source );

		{ Go through each of the meks and, if they are part }
		{ of the specified team, assign the specified order. }
		Mek := gb^.Meks;
		while Mek <> Nil do begin
			if NAttValue( Mek^.NA , NAG_Location , NAS_Team ) = Team then begin
				SetNAtt( Mek^.NA , NAG_EpisodeData , NAS_Orders , OrderCode );

				{ DEFAULT BEHAVIOR- If number of params = 1, assume it to be a mek ID. }
				{ If number of params = 2, assume it to be a map location. }
				if OrderParams[ OrderCode ] = 1 then begin
					SetNAtt( Mek^.NA , NAG_EpisodeData , NAS_ATarget , P[1] );
				end else if OrderParams[ OrderCode ] = 2 then begin
					SetNAtt( Mek^.NA , NAG_Location , NAS_GX , P[1] );
					SetNAtt( Mek^.NA , NAG_Location , NAS_GY , P[2] );
				end;
			end;
			Mek := Mek^.Next;
		end;
	end;
end;

Procedure ProcessCompose( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ A new item is going to be added to the scene list. }
var
	Trigger,Ev2: String;
	P: Integer;
begin
	if Source = Nil then exit;

	{ Extract the values we need. }
	Trigger := ExtractWord( Event );
	P := ScriptValue( Event , GB , Source );
	Ev2 := AS_GetString( Source , ExtractWord( Event ) );

	StoreSAtt( Source^.SA , Trigger + BStr( P ) + ' <' + Ev2 + '>' );
end;

Procedure ProcessNewChat;
	{ Reset the dialog menu with the standard options. }
begin
	{ Error check - make sure the interaction menu is active. }
	if IntMenu = Nil then begin
		Exit;

	{ If there are any menu items currently in the list, get rid }
	{ of them. }
	end else if IntMenu^.FirstItem <> Nil then begin
		ClearMenu( IntMenu );
	end;

	AddRPGMenuItem( IntMenu , '[Chat]' , CMD_Chat );
	AddRPGMenuItem( IntMenu , '[Goodbye]' , -1 );
	if ( I_NPC <> Nil ) and ( NAttValue( I_NPC^.NA , NAG_Relationship , 0 ) > 0 ) and ( NAttValue( I_NPC^.NA , NAG_Location , NAS_Team ) <> NAV_LancemateTeam ) then AddRPGMenuItem( IntMenu , '[Join]' , CMD_Join );
	if ( I_NPC <> Nil ) and ( NAttValue( I_NPC^.NA , NAG_Location , NAS_Team ) = NAV_LancemateTeam ) then AddRPGMenuItem( IntMenu , '[Quit Lance]' , CMD_Quit );
	RPMSortAlpha( IntMenu );
end;

Procedure ProcessEndChat;
	{ End this conversation by clearing the menu. }
begin
	{ Error check - make sure the interaction menu is active. }
	if IntMenu = Nil then begin
		Exit;
	end else begin
		ClearMenu( IntMenu );
	end;
end;

Procedure ProcessGoto( var Event: String; Source: GearPtr );
	{ Attempt to jump to a different line of the script. }
	{ If no line label is provided, or if the label can't be }
	{ found, this procedure sets EVENT to an empty string. }
var
	destination: String;
begin
	{ Error check- if there's no defined source, we can't very }
	{ well jump to another line, can we? }
	if Source = Nil then begin
		Event := '';
		Exit;
	end;

	destination := ExtractWord( Event );
	if destination <> '' then begin
		{ Change the event script to the requested line. }
		Event := AS_GetString( Source , destination );
	end else begin
		{ No label was provided. Just return a blank line. }
		Event := '';
	end;
end;

Procedure ProcessSeekTerr( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Assign a value to SCRIPT_Terrain_To_Seek. }
var
	Terrain: Integer;
begin
	Terrain := ScriptValue( event , GB , Source );
	SCRIPT_Terrain_To_Seek := Terrain;
end;

Procedure ProcessShop( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Retrieve the WARES line, then pass it all on to the OpenShop }
	{ procedure. }
var
	Wares: String;
begin
	{ Retrieve the WARES string. }
	Wares := ExtractWord( Event );
	if Wares <> '' then begin
		{ Change the event script to the requested line. }
		Wares := AS_GetString( Source , Wares );
	end;

	{ Pass all info on to the OPENSHOP procedure. }
	OpenShop( GB , I_PC , I_NPC , Wares );
end;

Procedure ProcessSchool( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Retrieve the WARES line, then pass it all on to the OpenSchool }
	{ procedure. }
var
	Wares: String;
begin
	{ Retrieve the WARES string. }
	Wares := ExtractWord( Event );
	if Wares <> '' then begin
		{ Change the event script to the requested line. }
		Wares := AS_GetString( Source , Wares );
	end;

	{ Pass all info on to the OPENSHOP procedure. }
	OpenSchool( GB , I_PC , I_NPC , Wares );
end;

Procedure ProcessExpressDelivery( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Call the ExpressDelivery procedure. }
begin
	{ Pass all info on to the ExpressDelivery procedure. }
	ExpressDelivery( GB , I_PC , I_NPC );
end;

Procedure ProcessShuttle( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Retrieve the WARES line, then pass it all on to the OpenShop }
	{ procedure. }
begin
	{ Pass all info on to the OPENSHOP procedure. }
	OpenShuttle( GB , I_PC , I_NPC );
end;


Procedure ProcessAdvancePlot( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ This particular plot is over- mark it for deletion. }
	{ First, though, check to see if there are any subcomponents that }
	{ need to be moved around. }
var
	N: Integer;
begin
	{ Determine which sub-plot to advance to. }
	N := ScriptValue( event , GB , Source );

	{ If we have a valid SOURCE, attempt to advance the plot. }
	if ( Source <> Nil ) then begin
		{ It's possible that our SOURCE is a PERSONA rather than }
		{ a PLOT, so if SOURCE isn't a PLOT move to its parent. }
		Source := PlotMaster( Source );
		if ( Source <> Nil ) and ( Source^.G = GG_Plot ) then begin
            AdvancePlot( GB , Source^.Parent , Source , N );
            NeedGC := True;
        end;
	end;
end;

Procedure CleanUpStoryPlots( GB: GameBoardPtr; Story: GearPtr );
	{ Give a CLEANUP trigger to all the story plots, then move the }
	{ plots which survive to the adventure invcoms. }
var
	T: String;
	Part,P2,Adv: GearPtr;
begin
	{ Send a CLEANUP trigger to the invcoms. }
	{ This should erase all the plots that want to be erased, }
	{ and leave all the plots which want to be moved. }
	T := 'CLEANUP';
	CheckTriggerAlongPath( T , GB , Story^.InvCom , False );

	{ Check whatever is left over. }
	Part := Story^.InvCom;
	Adv := GG_LocateAdventure( GB , STory );
	while Part <> Nil do begin
		P2 := Part^.Next;

		if Part^.G = GG_Plot then begin
			DelinkGear( Story^.InvCom , Part );
			if Adv <> Nil then begin
				InsertInvCom( Adv , Part );
			end else begin
				DisposeGear( Part );
			end;
		end;

		Part := P2;
	end;
end;

Procedure ProcessEndStory( GB: GameBoardPtr; Source: GearPtr );
	{ This particular story is over- mark it for deletion. }
	{ First, though, pass a CLEANUP trigger to any subcomponents that }
	{ may need to be cleaned up. }
begin
	Source := StoryMaster( Source );
	if ( Source <> Nil ) and ( Source^.G = GG_Story ) then begin
		CleanupStoryPlots( GB , Source );

		{ Mark the story for deletion. }
		Source^.G := GG_AbsolutelyNothing;
        NeedGC := True;
	end;
end;

Procedure ProcessPurgeStory( GB: GameBoardPtr; Source: GearPtr );
	{ Eliminate all plots from this story. }
begin
	{ If we have a valid SOURCE, check the invcoms. }
	if ( Source <> Nil ) and ( Source^.G = GG_Story ) then begin
		{ Send a CLEANUP trigger to the invcoms, }
		{ then move the survivors to the Adventure. }
		CleanupStoryPlots( GB , Source );
	end;
end;

Procedure ProcessTReputation( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Something has happened to affect the PC's reputation. }
	{ Record the change. }
var
	T,R,V: Integer;
begin
	{ Error check - this procedure only works if GB is defined. }
	if ( GB = Nil ) then Exit;

	T := ScriptValue( Event , GB , Source );
	R := ScriptValue( Event , GB , Source );
	V := ScriptValue( Event , GB , Source );

	SetTeamReputation( GB , T , R , V );
end;

Procedure ProcessMechaPrize( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ The player has just won a mecha. Cool! }
var
	FName: String;
	MList,Mek,PC: GearPtr;
begin
	{ ERROR CHECK - We need the gameboard to exist!!! }
	if GB = Nil then Exit;

	{ First, find the file name of the mecha file to look for. }
	FName := ExtractWord( Event );
	if Source <> Nil then begin
		FName := AS_GetString( Source , FName );
	end else begin
		FName := '';
	end;

	MList := LoadGearPattern( FName , Design_Directory );

	{ Next confirm that something was loaded. }
	if MList <> Nil then begin
		{ Something was loaded. Yay! Pick one of the gears }
		{ at random, clone it, stick it on the game board, }
		{ and get rid of the list we loaded. }
		Mek := CloneGear( SelectRandomGear( MList ) );
		DisposeGear( MList );

		SetNAtt( Mek^.NA , NAG_Location , NAS_Team , NAV_DefPlayerTeam );
		DeployMek( GB , Mek , False );
		PC := GG_LocatePC( GB );
		if FindPilotsMecha( GB^.Meks , PC ) = Nil then AssociatePilotMek( GB^.Meks , PC , Mek );
	end;

end;

Procedure ProcessDeleteGG( GB: GameBoardPtr; var Source: GearPtr );
	{ Delete the grabbed gear. }
	{ Only physical gears can be deleted in this way. }
begin
	if ( Grabbed_Gear <> Nil ) and ( Grabbed_Gear^.G >= 0 ) then begin
		{ Make sure we aren't currently using the grabbed gear. }
		if ( IntMenu <> Nil ) and ( I_NPC = Grabbed_Gear ) then begin
			ProcessEndChat;
			I_NPC := Nil;
		end;
		if Source = Grabbed_Gear then begin
			Source := Nil;
		end;

		{ Delete the gear, if it can be found. }
		if IsSubCom( Grabbed_Gear ) then begin
			RemoveGear( Grabbed_Gear^.Parent^.SubCom , Grabbed_Gear );

		end else if IsInvCom( Grabbed_Gear ) then begin
			RemoveGear( Grabbed_Gear^.Parent^.InvCom , Grabbed_Gear );

		end else if ( GB <> Nil ) and IsFoundAlongTrack( GB^.Meks , Grabbed_Gear) then begin
			RemoveGear( GB^.Meks , Grabbed_Gear );

		end;
	end;
end;

Procedure ProcessMoveGG( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Move the grabbed gear to the specified scene. }
	{ Only physical gears can be moved in this way. }
	{ If the specified scene is 0, the gear will be "frozen" isntead. }
var
	SID: Integer;	{ Scene ID. }
	Scene: GearPtr;
	P: Point;
begin
	{ Check to make sure we have a valid gear to move. }
	if ( Grabbed_Gear <> Nil ) and ( Grabbed_Gear^.G >= 0 ) then begin
		{ Attach useful scene-specific information to this gear. }
		if IsMasterGear( Grabbed_Gear ) and ( SAttValue( Grabbed_Gear^.SA , 'TEAMDATA' ) = '' ) then begin
			SetSATt( Grabbed_Gear^.SA , 'TEAMDATA <PASS>' );
		end;

		{ Delink the gear, if it can be found. }
		if IsSubCom( Grabbed_Gear ) then begin
			DelinkGear( Grabbed_Gear^.Parent^.SubCom , Grabbed_Gear );
		end else if IsInvCom( Grabbed_Gear ) then begin
			DelinkGear( Grabbed_Gear^.Parent^.InvCom , Grabbed_Gear );
		end else if ( GB <> Nil ) and IsFoundAlongTrack( GB^.Meks , Grabbed_Gear) then begin
			P := GearCurrentLocation( Grabbed_Gear );
			DelinkGear( GB^.Meks , Grabbed_Gear );
			if IntMenu = Nil then RedrawTile( GB , P.X , P.Y );
		end;
		StripNAtt( Grabbed_Gear , NAG_Location );
		StripNAtt( Grabbed_Gear , NAG_Damage );
		StripNAtt( Grabbed_Gear , NAG_WeaponModifier );
		StripNAtt( Grabbed_Gear , NAG_Condition );
		StripNAtt( Grabbed_Gear , NAG_StatusEffect );

		{ Find the new scene to stick our gear into. }
		SID := ScriptValue( Event , GB , Source );
		if SID > 0 then begin
			Scene := FindActualScene( GB , SID );
		end else begin
			Scene := GG_LocateAdventure( GB , Source );
		end;
		InsertInvCom( Scene , Grabbed_Gear );

		{ Clear the item's ORIGINAL HOME value. }
		SetNAtt( Grabbed_Gear^.NA , NAG_ParaLocation , NAS_OriginalHome , 0 );


		{ If inserting a character, better choose a team. }
		if ( Scene^.G = GG_Scene ) and IsMasterGear( Grabbed_Gear ) then begin
			ChooseTeam( Grabbed_Gear , Scene );
		end;
	end;
end;

Procedure ProcessDeployGG( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Move the grabbed gear to the current scene. }
	{ Only physical gears can be moved in this way. }
var
	Team: Integer;
	Scene: GearPtr;
	P: Point;
begin
	{ Check to make sure we have a valid gear to move. }
	if ( Grabbed_Gear <> Nil ) and ( GB <> Nil ) and ( Grabbed_Gear^.G >= 0 ) then begin
		{ Attach useful scene-specific information to this gear. }
		if IsMasterGear( Grabbed_Gear ) and ( SAttValue( Grabbed_Gear^.SA , 'TEAMDATA' ) = '' ) then begin
			SetSATt( Grabbed_Gear^.SA , 'TEAMDATA <PASS>' );
		end;

		{ Set the item's ORIGINAL HOME value. }
		if (( GB^.Scene = Nil ) or IsInvCom( GB^.Scene )) and ( NAttValue( Grabbed_Gear^.NA , NAG_ParaLocation , NAS_OriginalHome ) = 0 ) then begin
			SetNAtt( Grabbed_Gear^.NA , NAG_ParaLocation , NAS_OriginalHome , FindGearScene( Grabbed_Gear , GB ) );
		end;

		{ Delink the gear, if it can be found. }
		if IsSubCom( Grabbed_Gear ) then begin
			DelinkGear( Grabbed_Gear^.Parent^.SubCom , Grabbed_Gear );
		end else if IsInvCom( Grabbed_Gear ) then begin
			DelinkGear( Grabbed_Gear^.Parent^.InvCom , Grabbed_Gear );
		end else if IsFoundAlongTrack( GB^.Meks , Grabbed_Gear) then begin
			P := GearCurrentLocation( Grabbed_Gear );
			DelinkGear( GB^.Meks , Grabbed_Gear );
			if ( IntMenu = Nil ) and OnTheMap( P.X , P.Y ) then RedrawTile( GB , P.X , P.Y );
		end;
		StripNAtt( Grabbed_Gear , NAG_Location );
		StripNAtt( Grabbed_Gear , NAG_Damage );
		StripNAtt( Grabbed_Gear , NAG_WeaponModifier );
		StripNAtt( Grabbed_Gear , NAG_Condition );
		StripNAtt( Grabbed_Gear , NAG_StatusEffect );

		{ Find the new team for our gear. }
		Team := ScriptValue( Event , GB , Source );
		SetNAtt( Grabbed_Gear^.NA , NAG_Location , NAS_Team , Team );

		{ Stick it on the map, and maybe do a redraw. }
		DeployMek( GB , Grabbed_Gear , True );
		P := GearCurrentLocation( Grabbed_Gear );
		if ( IntMenu = Nil ) and OnTheMap( P.X , P.Y ) then RedrawTile( GB , P.X , P.Y );
	end;
end;

Procedure ProcessDynaGG( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Move the grabbed gear to the dynamic scene. }
	{ Only physical gears can be moved in this way. }
	{ If the specified scene is 0, the gear will be "frozen" isntead. }
var
	TID: Integer;	{ Scene ID, Team ID. }
	Scene: GearPtr;
	P: Point;
begin
	{ Check to make sure we have a valid gear to move. }
	if ( Grabbed_Gear <> Nil ) and ( Grabbed_Gear^.G >= 0 ) and ( SCRIPT_DynamicEncounter <> Nil ) then begin
		{ Attach useful scene-specific information to this gear. }
		if IsMasterGear( Grabbed_Gear ) and ( NAttValue( Grabbed_Gear^.NA , NAG_ParaLocation , NAS_OriginalHome ) = 0 ) then begin
			Scene := FindActualScene( GB , FindGearScene( Grabbed_Gear , GB ) );

			TID := NAttValue( Grabbed_Gear^.NA , NAG_Location , NAS_Team );
			if SCene <> Nil then begin
				{ Record team description. }
				SetSATt( Grabbed_Gear^.SA , 'TEAMDATA <' + TeamDescription( Scene, LocateTeam( Scene , TID ) ) + '>' );

				{ Record the item's orginal home, if not already done. }
				SetNAtt( Grabbed_Gear^.NA , NAG_ParaLocation , NAS_OriginalHome , Scene^.S );
			end else begin
				SetNAtt( Grabbed_Gear^.NA , NAG_ParaLocation , NAS_OriginalHome , -1 );
			end;
		end else begin
			Scene := Nil;
			if NAttValue( Grabbed_Gear^.NA , NAG_ParaLocation , NAS_OriginalHome ) = 0 then SetNAtt( Grabbed_Gear^.NA , NAG_ParaLocation , NAS_OriginalHome , -1 );
		end;


		{ Delink the gear, if it can be found. }
		if IsSubCom( Grabbed_Gear ) then begin
			DelinkGear( Grabbed_Gear^.Parent^.SubCom , Grabbed_Gear );
		end else if IsInvCom( Grabbed_Gear ) then begin
			DelinkGear( Grabbed_Gear^.Parent^.InvCom , Grabbed_Gear );
		end else if ( GB <> Nil ) and IsFoundAlongTrack( GB^.Meks , Grabbed_Gear) then begin
			P := GearCurrentLocation( Grabbed_Gear );
			DelinkGear( GB^.Meks , Grabbed_Gear );
			if IntMenu = Nil then RedrawTile( GB , P.X , P.Y );
		end;
		StripNAtt( Grabbed_Gear , NAG_Location );
		StripNAtt( Grabbed_Gear , NAG_Damage );
		StripNAtt( Grabbed_Gear , NAG_WeaponModifier );
		StripNAtt( Grabbed_Gear , NAG_Condition );
		StripNAtt( Grabbed_Gear , NAG_StatusEffect );

		{ Find out which team to stick the NPC in. }
		TID := ScriptValue( Event , GB , Source );

		{ Perform the insertion. }
		InsertNPCIntoDynamicScene( Grabbed_Gear , SCRIPT_DynamicEncounter , TID );

	end;
end;

Procedure ProcessGiveGG( GB: GameBoardPtr );
	{ Give the grabbed gear to the PC. }
	{ Only physical gears can be moved in this way. }
var
	DelinkOK: Boolean;
	PC: GearPtr;
begin
	PC := GG_LocatePC( GB );

	if ( Grabbed_Gear <> Nil ) and ( Grabbed_Gear^.G >= 0 ) and (( PC = Nil ) or ( FindGearIndex( Grabbed_Gear , PC ) < 0 )) then begin

		{ Delink the gear, if it can be found. }
		if IsSubCom( Grabbed_Gear ) then begin
			DelinkGear( Grabbed_Gear^.Parent^.SubCom , Grabbed_Gear );
			DelinkOK := True;
		end else if IsInvCom( Grabbed_Gear ) then begin
			DelinkGear( Grabbed_Gear^.Parent^.InvCom , Grabbed_Gear );
			DelinkOK := True;
		end else if ( GB <> Nil ) and IsFoundAlongTrack( GB^.Meks , Grabbed_Gear) then begin
			DelinkGear( GB^.Meks , Grabbed_Gear );
			DelinkOK := True;
		end else begin
			DelinkOK := False;
		end;

		if DelinkOK then begin
			GivePartToPC( GB , Grabbed_Gear , PC );
		end;
	end;
end;

Procedure ProcessGNewPart( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Stick an item from the standard items list on the gameboard, }
	{ then make GRABBED_GEAR point to it. }
	{ This function will first look in the STC file, then the Monster }
	{ file, then the NPC file. }
var
	IName: String;
begin
	{ First determine the item's designation. }
	IName := ExtractWord( Event );
	if Source <> Nil then begin
		IName := AS_GetString( Source , IName );
	end;

	{ As long as we have a GB, try to stick the item there. }
	if GB <> Nil then begin
		Grabbed_Gear := LoadNewSTC( IName );
		if Grabbed_Gear = Nil then Grabbed_Gear := LoadNewMonster( IName );
		if Grabbed_Gear = Nil then Grabbed_Gear := LoadNewNPC( IName );

		{ If we found something, stick it on the map. }
		if Grabbed_Gear <> Nil then begin
			{ Clear the designation. }
			SetSAtt( Grabbed_Gear^.SA , 'DESIG <>' );

			{ Deploy the item. }
			DeployMek( GB , Grabbed_Gear , False );
		end;

		{ Any further processing must be done by other commands. }
	end;
end;

Procedure ProcessSetSceneFaction( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ This procedure will take over a scene in the name of a given }
	{ faction. }
const
	New_Home_Type1 = 'Town !M ';
	New_Home_Type2 = 'Town !M 0';
	Garrison_Team_Type = 'TEAMDATA <ally sd>';

	Procedure RemoveToSafeHome( Part,OScene: GearPtr; var LList: GearPtr );
		{ PART needs to be extracted from its current scene and }
		{ moved to a new scene. If no good scene can be found, }
		{ then delete PART. }
	var
		FID: Integer;
		Adv,Scene: GearPtr;
	begin
		{ Delink the NPC from its current location. }
		DelinkGear( LList , Part );

		{ Find the adventure. }
		Adv := FindRoot( GB^.Scene );

		{ Find its current faction number, and try to find a }
		{ scene to send it to. }
		FID := NAttValue( Part^.NA , NAG_Personal , NAS_FactionID );
		Scene := SearchForScene( Adv , Nil , GB , New_Home_Type1 + BStr( FID ) );
		if Scene = Nil then begin
			Scene := SearchForScene( Adv , Nil , GB , New_Home_Type2 );
		end;

		{ If a scene was found, move the NPC there. }
		{ Otherwise delete the NPC. }
		if Scene <> Nil then begin
			SetSATt( Part^.SA , 'TEAMDATA <' + TeamDescription( OScene, LocateTeam( OScene , NAttValue( PART^.NA , NAG_Location , NAS_Team ) ) ) + '>' );

			{ Strip the location data from the character. }
			StripNAtt( Part , NAG_Location );

			{ Move the NPC to the adventure's INV. }
			InsertInvCom( Scene , Part );
			ChooseTeam( Part , Scene );

		end else begin
			DisposeGear( Part );
		end;
	end;

	Procedure ScanForDisposessed( OScene: GearPtr; var LList: GearPtr; NFID: Integer );
		{ Check the list provided for members of an ousted faction. }
		{ Remove those members to safe towns elsewhere, or delete }
		{ them if no safe havens could be found. }
	var
		Part,P2: GearPtr;
		PFID: Integer;
	begin
		Part := LList;
		while Part <> Nil do begin
			P2 := Part^.Next;

			{ If this is a character, and isn't the player }
			{ character, it may need to be relocated. }
			if ( Part^.G = GG_Character ) and ( NAttValue( Part^.NA , NAG_Location , NAS_Team ) <> NAV_DefPlayerTeam ) and ( NAttValue( Part^.NA , NAG_Location , NAS_Team ) <> NAV_LancemateTeam ) then begin

				PFID := NAttValue( Part^.NA , NAG_Personal , NAS_FactionID );
				if ( PFID <> 0 ) and ( PFID <> NFID ) then begin
					{ This character has to be removed from the scene. }
					RemoveToSafeHome( Part , OScene , LList );
				end;
			end else begin
				if Part^.SubCom <> Nil then ScanForDisposessed( OScene , Part^.SubCom , NFID );
				if Part^.InvCom <> Nil then ScanForDisposessed( OScene , Part^.InvCom , NFID );
			end;

			Part := P2;
		end;
	end;

	Procedure AddNewGarrison( SID,FID: Integer );
		{ Add a new garrison to the town. }
	var
		Adv,NPC: GearPtr;
		T: Integer;
	begin
		{ Find the adventure. }
		Adv := FindRoot( GB^.Scene );

		for t := 1 to 3 do begin
			NPC := LoadNewNPC( 'Mecha Pilot' );
			if NPC <> Nil then begin
				SetNAtt( NPC^.NA , NAG_Personal , NAS_CID , NewCID( GB , Adv ) );

				SetSATt( NPC^.SA , 'LOCATIONS <' + BStr( SID ) + '>' );
				SetSATt( NPC^.SA , Garrison_Team_Type );
				SetNAtt( NPC^.NA , NAG_Personal , NAS_FactionID , FID );

				{ Move the NPC to the adventure's INV. }
				InsertInvCom( Adv , NPC );
			end;
		end;
	end;

var
	SID,FID: Integer;
	Scene: GearPtr;

begin
	{ Find the Scene ID and Faction ID. }
	SID := ScriptValue( event , GB , Source );
	FID := ScriptValue( event , GB , Source );

	{ Locate the scene to change the faction of. }
	if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then  begin
		Scene := FindActualScene( GB  , SID );

		if Scene <> Nil then begin
			{ Step One - Set the new FACTION ID. }
			SetNAtt( Scene^.NA , NAG_Personal , NAS_FactionID , FID );

			{ Step Two - Remove all members of other factions, }
			{ moving them to friendlier territories. }
			ScanForDisposessed( Scene , Scene^.InvCom , FID );
			if GB^.Scene = Scene then ScanForDisposessed( Scene , GB^.Meks , FID );

			{ Step Three - Add 3 new faction members to the }
			{ scene. This is the new garrison. }
			AddNewGarrison( SID , FID );
		end;
	end;
end;

Procedure ProcessDeleteFaction( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ The requested FACTION is about to be taken out of the game. }
	{ The gear for the faction isn't deleted (we may need it for }
	{ rumors & stuff & possible recovery) but all characters and }
	{ locations belonging to the faction get their FacID cleared. }
	{ Also, an "INACTIVE" tag is added to the faction's TYPE SAtt. }
	Procedure DFWorkHorse( Part: GearPtr; FID: Integer );
		{ Travel through PART and all of its siblings and children, }
		{ enacting the deletion of FID all the way along. }
	var
		t: String;
	begin
		while Part <> Nil do begin
			if Part^.G = GG_Faction then begin
				{ This part won't have a FID stored, but may }
				{ be the part  we're deleting. }
				if Part^.S = FID then begin
					{ This faction is the faction we're deleting! }
					T := SATtValue( Part^.SA , 'TYPE' );
					if not AStringHasBString( T , 'INACTIVE' ) then SetSAtt( Part^.SA , 'TYPE <' + T + ' INACTIVE>' );
				end;
			end else begin
				if NAttValue( Part^.NA , NAG_Personal , NAS_FactionID ) = FID then begin
					{ Set this part's FACTION ID to zero. }
					SetNAtt( Part^.NA , NAG_Personal , NAS_FactionID , 0 );

					if Part^.G = GG_Character then begin
						AddReputation( Part , 6 , -30 );
					end;
				end;
			end;

			{ Check the sub and inv components. }
			DFWorkHorse( Part^.SubCom , FID );
			DFWorkHorse( Part^.InvCom , FID );

			{ Move to the next part. }
			Part := Part^.Next;
		end;
	end;
var
	FID: Integer;
	Part: GearPtr;
begin
	{ First determine what faction we're supposed to be deleting. }
	FID := ScriptValue( Event , GB , Source );

	{ Locate the root adventure gear next. }
	if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then begin
		Part := FindRoot( GB^.Scene );
	end else begin
		Part := FindRoot( Source );
	end;

	{ If the adventure was found, continue on with the deletion }
	{ of this faction. }
	if ( Part <> Nil ) and ( Part^.G = GG_Adventure ) then begin
		DFWorkHorse( Part , FID );
	end;
end;

Procedure BuildGenericEncounter( GB: GameBoardPtr; Scale: Integer );
	{ Create a SCENE gear, then do everything except stock it with }
	{ enemies. }
var
	Team: GearPtr;
begin
	{ First, if for some reason there's already a dynamic encounter in }
	{ place, get rid of it. }
	if SCRIPT_DynamicEncounter <> Nil then DisposeGear( SCRIPT_DynamicEncounter );

	{ Allocate a new dynamic encounter, then fill in the blanks. }
	SCRIPT_DynamicEncounter := NewGear( Nil );
	SCRIPT_DynamicEncounter^.G := GG_Scene;
	SCRIPT_DynamicEncounter^.Stat[ STAT_MapGenerator ] := 1;
	if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then begin
		{ Copy over the important stuff. }
		SCRIPT_DynamicEncounter^.S := GB^.Scene^.S;

		{ Copy the Faction ID of the original scene in order to prevent }
		{ erroneous readings from SCENEFACTION. I don't really like this }
		{ solution to the problem since it's a bit hackish, but at least }
		{ it's more expandable than the alternatives. }
		SetNAtt( SCRIPT_DynamicEncounter^.NA , NAG_Personal , NAS_FactionID , NAttValue( GB^.Scene^.NA , NAG_Personal , NAS_FactionID ) );
	end;

	SCRIPT_DynamicEncounter^.V := Scale;

	{ Add a TEAM gear for each of the player and the enemy teams. }
	{ We need to do this so that we'll have some control over the placement }
	{ of the player and the enemies. }
	Team := AddGear( SCRIPT_DynamicEncounter^.SubCom , SCRIPT_DynamicEncounter );
	Team^.G := GG_Team;
	Team^.S := NAV_DefPlayerTeam;
	SetNAtt( Team^.NA , NAG_SideReaction , NAV_DefEnemyTeam , NAV_AreEnemies );
	SetNAtt( Team^.NA , NAG_ParaLocation , NAS_X , XMax div 5 );
	SetNAtt( Team^.NA , NAG_ParaLocation , NAS_Y , YMax div 2 );

	Team := AddGear( SCRIPT_DynamicEncounter^.SubCom , SCRIPT_DynamicEncounter );
	Team^.G := GG_Team;
	Team^.S := NAV_DefEnemyTeam;
	SetNAtt( Team^.NA , NAG_SideReaction , NAV_DefPlayerTeam , NAV_AreEnemies );
	SetNAtt( Team^.NA , NAG_ParaLocation , NAS_X , ( XMax * 4 ) div 5 );
	SetNAtt( Team^.NA , NAG_ParaLocation , NAS_Y , YMax div 2 );


	{ Set the exit values in the game board. }
	if GB <> Nil then begin
		AS_SetExit( GB , 0 );

		{ Advance the game clock by one hour. }
		QuickTime( GB , AP_HalfHour + RollStep( 35 ) * 5 );
	end;
end;

Procedure ProcessNewD( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Create a new scene for a dynamic encounter to take place on. }
var
	Scale: Integer;
begin
 	Scale := ScriptValue( Event , GB , Source );
	BuildGenericEncounter( GB , Scale );
end;

Procedure ProcessLoadD( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ A random encounter has been called for. Yay! Fill out the details }
	{ in SCENE_DynamicEncounter, then set the GB's exit code. }
var
	FName: String;
begin
	{ First, if for some reason there's already a dynamic encounter in }
	{ place, get rid of it. }
	if SCRIPT_DynamicEncounter <> Nil then DisposeGear( SCRIPT_DynamicEncounter );

	{ First, find the file name of the scene file to look for. }
	FName := ExtractWord( Event );
	if Source <> Nil then begin
		FName := AS_GetString( Source , FName );
	end else begin
		FName := '';
	end;

	{ Secondly, confirm the file name. }
	if FName <> '' then begin
		SCRIPT_DynamicEncounter := LoadGearPattern( FName , Series_Directory );

		if ( SCRIPT_DynamicEncounter <> Nil ) and ( SCRIPT_DynamicEncounter^.G = GG_Scene ) then begin
			{ Fill in the ID number to be used by RETURN. }
			if ( GB <> Nil ) and ( GB^.Scene <> Nil ) then begin
				SCRIPT_DynamicEncounter^.S := GB^.Scene^.S;

				{ Copy the Faction ID of the original scene in order to prevent }
				{ erroneous readings from SCENEFACTION. I don't really like this }
				{ solution to the problem since it's a bit hackish, but at least }
				{ it's more expandable than the alternatives. }
				SetNAtt( SCRIPT_DynamicEncounter^.NA , NAG_Personal , NAS_FactionID , NAttValue( GB^.Scene^.NA , NAG_Personal , NAS_FactionID ) );

			end;

			{ Set the exit values in the game board. }
			if GB <> Nil then begin
				AS_SetExit( GB , 0 );

				{ Advance the game clock by one hour. }
				QuickTime( GB , AP_HalfHour + RollStep( 35 ) * 5 );
			end;

		end else begin
			DisposeGear( SCRIPT_DynamicEncounter );

		end;
	end;

end; { ProcessLoadD }

Procedure ProcessTStockD( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Fill SCRIPT_DynamicEncounter with enemies. }
var
	TID,UPV: LongInt;
begin
	{ Find out the team, and how many enemies to add. }
	TID := ScriptValue( Event , GB , Source );
 	UPV := ScriptValue( Event , GB , Source );

	{ Error check - Make sure we have a dynamic encounter to stock! }
	if SCRIPT_DynamicEncounter <> Nil then begin
		{ Stick enemies in the scene. }
		StockSceneWithEnemies( SCRIPT_DynamicEncounter , UPV , TID );
	end;
end; { ProcessTStockD }

Procedure ProcessWMecha( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Fill SCRIPT_DynamicEncounter with enemies. }
var
	TID,UPV: LongInt;
	SList: SAttPtr;	{ Shopping list. }
	MList,Mek: GearPtr;
begin
	{ Find out the team, and how many enemies to add. }
	TID := ScriptValue( Event , GB , Source );
 	UPV := ScriptValue( Event , GB , Source );

	if GB <> Nil then begin
		{ Generate the list of mecha. }
		SList := GenerateMechaList( UPV div 2 );

		{ Generate the mecha list. }
		MList := PurchaseForces( SList , UPV );

		{ Get rid of the shopping list. }
		DisposeSAtt( SList );

		{ Deploy the mecha on the map. }
		while MList <> Nil do begin
			{ Delink the first gear from the list. }
			Mek := MList;
			DelinkGear( MList , Mek );

			{ Set its team to the requested value. }
			SetNAtt( Mek^.NA , NAG_Location , NAS_Team , TID );

			{ Place it on the map. }
			DeployMek( GB , Mek , True );
		end;
	end;
end; { ProcessWMecha }

Procedure ProcessTMStockD( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Fill SCRIPT_DynamicEncounter with enemies. }
var
	TID,UPV: LongInt;
	MDesc: String;
begin
	{ Find out the team, and how many enemies to add. }
 	TID := ScriptValue( Event , GB , Source );
 	UPV := ScriptValue( Event , GB , Source );

	{ This UPV is roughly equal to the PC's reputation score, give or take a }
	{ little bit. Convert it to a usable value. }
	if UPV < 5 then UPV := 5;
	UPV := ( UPV * UPV div 250 ) + UPV div 5;

	MDesc := ExtractWord( Event );
	if Source <> Nil then begin
		MDesc := AS_GetString( Source , MDesc );
	end;

	{ Error check - Make sure we have a dynamic encounter to stock! }
	if SCRIPT_DynamicEncounter <> Nil then begin
		{ Stick enemies in the scene. }
		StockSceneWithMonsters( SCRIPT_DynamicEncounter , UPV , TID , MDesc );
	end;
end; { ProcessTMStockD }


Procedure ProcessEncounter( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ This procedure may add one of the global NPCs to the list, }
	{ either as an enemy of or an ally of the PC. }
	{ The HOOK is the conversation gear which will be placed for }
	{ the enemy/ally in the target scene. }
var
	ADV,PC: GearPtr;
	EChance,AChance: Integer;
	MainDesc: String;
begin
	{ Error check - make sure everything is defined first. }
	if ( SCRIPT_DynamicEncounter = Nil ) or ( GB = Nil ) or ( GB^.Scene = Nil ) or ( Source = Nil ) then exit;

	{ Locate the root adventure gear and the player character. }
	ADV := FindRoot( GB^.Scene );
	if ( ADV = Nil ) or ( ADV^.G <> GG_Adventure ) then exit;
	PC := GG_LocatePC( GB );

	{ Get the EChance, AChance, and MainDesc. }
	EChance := ScriptValue( event , GB , Source );
	AChance := ScriptValue( event , GB , Source );
	MainDesc := AS_GetString( Source , ExtractWord( Event ) );
	FormatMessageString( MainDesc , gb , source );

	{ Check randomly to see whether or not to add an ally. }
	if Random( 100 ) < AChance then begin
		AddArchAllyToScene( Adv , SCRIPT_DynamicEncounter , PC );
	end;

	{ Check randomly to see whether or not to add an enemy. }
	if Random( 100 ) < EChance then begin
		AddArchEnemyToScene( Adv , SCRIPT_DynamicEncounter , PC , MainDesc );
	end;


end; { ProcessEncounter }

Function NumberOfPlots( Part: GearPtr ): Integer;
	{ Check the number of plots this PART has loaded. }
var
	P: GearPtr;
	N: Integer;
begin
	P := Part^.InvCom;
	N := 0;

	while P <> Nil do begin
		if P^.G = GG_Plot then Inc( N );
		P := P^.Next;
	end;

	NumberOfPlots := N;
end;

Procedure ProcessStoryLine( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ A new story arc is about to be loaded. }
	{ This may be an invcomponent of an existing story (the source), }
	{ or it may be a global arc which is to be inserted as an invcom }
	{ of the adventure. Once loaded, global arcs are treated like }
	{ regular plots. }
var
	FName: String;
	Adv,Arc: GearPtr;
	LoadOK: Boolean;
begin
	FName := ExtractWord( Event );

	{ Error check - We need the ADVENTURE gear for this!!! }
	Adv := FindRoot( GB^.Scene );
	if ( Adv = Nil ) or ( Adv^.G <> GG_Adventure ) then Exit;

	if ( Source <> Nil ) and ( Source^.G = GG_Story ) and ( NumberOfPlots( Source ) >= Max_Plots_Per_Story ) then begin
		{ Can't load a new plot at this time. }
		IfFailure( Event , Source );

	end else if ( Source <> Nil ) and ( Source^.G = GG_Story ) and ( NumberOfPlots( Source ) >= Max_Plots_Per_Adventure ) then begin
		{ Can't load a new plot at this time. }
		IfFailure( Event , Source );

	end else begin
		{ First, find the file name of the plot file to look for. }
		if Source <> Nil then begin
			FName := AS_GetString( Source , FName );
		end else begin
			FName := '';
		end;

		{ Secondly, confirm the file name. }
		Arc := LoadGearPattern( FName , Series_Directory );

		if Arc <> Nil then begin
			{ Insert the arc, calling IfSuccess or IfFailure. }
			if Source^.G = GG_Story then begin
				LoadOK := InsertStoryArc( Source , Arc , GB );
			end else begin
				LoadOK := InsertGlobalArc( Adv , Arc , GB );
			end;

			if LoadOK then begin
				IfSuccess( Event );
			end else begin
				IfFailure( Event , Source );
			end;

		end else begin
			{ File was not loaded successfully. }
			IfFailure( Event , Source );
		end;
	end;
end;

Procedure ProcessBatchLoadPlot( var Trigger,Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ A number of story arcs are about to be loaded. }
var
	FName: String;
	Adv,Arc: GearPtr;
	N,T: Integer;
begin
	{ Error check - We need the ADVENTURE gear for this!!! }
	Adv := FindRoot( GB^.Scene );
	if ( Adv = Nil ) or ( Adv^.G <> GG_Adventure ) or ( Source = Nil ) then Exit;

	{ First, find the plot pattern to look for. }
	FName := ExtractWord( Event );
	FName := AS_GetString( Source , FName );

	{ Next, find out how many plots to load. }
	N := ScriptValue( Event , GB , Source );

	{ Final error check- based on the config options, maybe exit. }
	if Load_Plots_At_Start and ( UpCase( Trigger ) <> 'START' ) then exit
	else if ( not Load_Plots_At_Start ) and ( UpCase( Trigger ) = 'START' ) then exit;

	for t := 1 to N do begin
		{ Secondly, confirm the file name. }
		Arc := LoadGearPattern( FName , Series_Directory );

		if Arc <> Nil then begin
			{ Insert the arc, calling IfSuccess or IfFailure. }
			if ( Source^.G = GG_Story ) and ( NumberOfPlots( Source ) <= Max_Plots_Per_Story ) then begin
				InsertStoryArc( Source , Arc , GB );
			end else if ( NumberOfPlots( Adv ) <= Max_Plots_Per_Adventure ) then begin
				InsertGlobalArc( Adv , Arc , GB );
			end else begin
				DisposeGear( Arc );
				break;
			end;
		end;
	end;
end;

Procedure ProcessXRanPlot( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ This will load a new plot for the story based upon the story }
	{ "extra random" descriptors. }
var
	FName: String;
	Arc: GearPtr;
	LoadOK: Boolean;
	Enemy,Mystery,BadThing: Integer;
begin
	{ Error check- this command only works from a story. }
	if ( Source = Nil ) or ( Source^.G <> GG_Story ) then begin
		{ Can't load a new Extra-Random plot from this source. }
		IfFailure( Event , Source );

	end else begin
		{ First, find the file name of the plot file to look for. }
		FName := ExtractWord( Event );
		if Source <> Nil then begin
			FName := AS_GetString( Source , FName );
		end else begin
			FName := '';
		end;

		{ Secondly, confirm the file name. }
		Enemy := NAttValue( Source^.NA , NAG_Narrative , NAS_XREnemy );
		Mystery := NAttValue( Source^.NA , NAG_Narrative , NAS_XRMystery );
		BadThing := NAttValue( Source^.NA , NAG_Narrative , NAS_XRBadThing );

		Arc := LoadXRanPlot( FName , Series_Directory , Enemy , Mystery , BadThing );

		{ Finally insert the plot. }
		if Arc <> Nil then begin
			{ Insert the arc, calling IfSuccess or IfFailure. }
			LoadOK := InsertStoryArc( Source , Arc , GB );

			if LoadOK then begin
				IfSuccess( Event );
			end else begin
				IfFailure( Event , Source );
			end;

		end else begin
			{ File was not loaded successfully. }
			IfFailure( Event , Source );
		end;
	end;
end;

Function AS_StoryInsertion( GB: GameBoardPtr; Source: GearPtr; FName: String ): Boolean;
	{ Attempt to load and initialize the requested story. }
var
	Adv,Story: GearPtr;
begin
	Story := LoadGearPattern( FName , Series_Directory );

	if Story <> Nil then begin
		{ Sub in the first element, if needed. }
		if Source^.G = GG_Faction then begin
			Story^.Stat[ 1 ] := Source^.S;
			SetSAtt( Story^.SA , 'ELEMENT1 <F>' );
			Adv := Source;
		end else if Source^.G = GG_Scene then begin
			Story^.Stat[ 1 ] := Source^.S;
			SetSAtt( Story^.SA , 'ELEMENT1 <S>' );
			Adv := GG_LocateAdventure( GB , Source );
		end else if Source^.G = GG_Story then begin
			Adv := Source;
		end else begin
			Adv := GG_LocateAdventure( GB , Source );
		end;

		if InsertStory( Adv , Story , GB ) then begin
			AS_StoryInsertion := True;
		end else begin
			AS_StoryInsertion := False;
		end;

	end else begin
		{ File was not loaded successfully. }
		AS_StoryInsertion := False;
	end;
end;

Procedure ProcessStartStory( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ A new story gear is about to be loaded. }
var
	FName: String;
begin
	{ First, find the file name of the plot file to look for. }
	FName := ExtractWord( Event );
	if Source <> Nil then begin
		FName := AS_GetString( Source , FName );
	end else begin
		FName := '';
	end;

	{ Call the above procedure to see if it works or not. }
	if AS_StoryInsertion( GB , Source , FName ) then begin
		IfSuccess( Event );
	end else begin
		IfFailure( Event , Source );
	end;
end;

Procedure ProcessGlobalStoryPattern( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ A bunch of stories will be loaded at once. }
	{ This command takes two arguments: First, the description for the scene to which }
	{ the stories will be loaded; Second, a search pattern for the story files. }
	{ Stories will continually be added until either stories or scenes run out, }
	{ with no duplication of either. }
var
	FName,SceneDesc: String;
	Adv,Scene: GearPtr;
	FList,SceneList,TempSceneList,F,S,TS: SAttPtr;
	SceneNum,N: Integer;
begin
	{ First, find the scene description and the file pattern. }
	SceneDesc := ExtractWord( Event );
	FName := ExtractWord( Event );
	if Source <> Nil then begin
		FName := AS_GetString( Source , FName );
		SceneDesc := AS_GetString( Source , SceneDesc );
	end else begin
		FName := '';
		SceneDesc := '';
	end;

	{ Create the list of story files. }
	FList := CreateFileList( Series_Directory + FName );

	{ Create the list of scenes that match. }
	{ Storing the scenes as a list of string attributes doesn't make much sense, }
	{ but there are pre-existing functions to handle lists of strings, and those }
	{ make it worthwhile for a lazy guy like me. }
	Adv := GG_LocateAdventure( GB , Source );
	Scene := Adv^.SubCom;
	SceneList := Nil;
	while Scene <> Nil do begin
		if ( Scene^.G = GG_Scene ) and PartMatchesCriteria( SATtValue( Scene^.SA , 'TYPE' ) , SceneDesc ) then begin
			StoreSAtt( SceneList , BStr( Scene^.S ) );
		end;
		Scene := Scene^.Next;
	end;

	{ Keep going until we run out of scenes or run out of stories }
	TempSceneList := Nil;
	while ( SceneList <> Nil ) and ( FList <> Nil ) do
	begin
		{ Select a random filename }
		F := SelectRandomSAtt( FList );
		{ Copy the list of unused scenes to make
		  a temporary list of untried but available
		  scenes.  This allows us to ensure a given
		  Scene/File combination is never retried,
		  while still leaving an discarded Scene available
		  for use by other story files.
		}
		S := SceneList;
		while (S <> Nil) do begin
			StoreSAtt(TempSceneList,S^.Info);
			S := S^.Next;
		end;

 		while (TempSceneList <> Nil) do begin
			TS := SelectRandomSAtt( TempSceneList );
			Val( TS^.Info , SceneNum , N );
			if N <> 0 then SceneNum := 0;
			Scene := FindActualScene( GB , SceneNum );
			if AS_StoryInsertion( GB , Scene , F^.Info ) then begin
				S := SceneList;
				while (S^.Info <> TS^.Info) do S := S^.Next;
				RemoveSAtt(SceneList, S);
				DisposeSAtt( TempSceneList);
			end else begin
				RemoveSAtt( TempSceneList , TS );
			end;
		end;
		RemoveSAtt( FList , F );

	end;
	if FList <> Nil then DisposeSAtt( FList );
	if SceneList <> Nil then DisposeSAtt( SceneList );
end;

Procedure ProcessAttack( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Team 1 is going to attack Team 2. }
var
	t1,T2: Integer;
	Team1,Mek: GearPtr;
begin
	{ Error check - We need that gameboard! }
	if GB = Nil then Exit;

	{ Read the script values. }
	T1 := ScriptValue( Event , GB , Source );
	T2 := ScriptValue( Event , GB , Source );

	{ Find the attacking team, and set the enemy value. }
	Team1 := LocateTeam( GB , T1 );
	if Team1 <> Nil then begin
		SetNAtt( Team1^.NA , NAG_SideReaction , T2 , NAV_AreEnemies );
	end;

	{ Locate each member of the team and set AIType to SEEK AND DESTROY. }
	Mek := GB^.Meks;
	while Mek <> Nil do begin
		if NAttValue( Mek^.NA , NAG_Location , NAS_Team ) = T1 then begin
			SetNAtt( Mek^.NA , NAG_EpisodeData , NAS_Orders , NAV_SeekAndDestroy );
		end;
		Mek := Mek^.Next;
	end;
end;

Procedure ProcessSalvage( GB: GameBoardPtr );
	{ It's looting time!!! Check every mecha on the game board; if it's }
	{ not operational but not destroyed, switch its TEAM to NAV_DefPlayerTeam. }
var
	Mek,M2,PC: GearPtr;
	CanScavenge: Boolean;
begin
	{ ERROR CHECK - GB must be defined!!! }
	if GB = Nil then Exit;

	{ Check to see if the PC has the Scavenger talent. }
	PC := GG_LocatePC( GB );
	CanScavenge := HasTalent( PC , NAS_Scavenger );

	{ Loop through every mek on the board. }
	Mek := GB^.Meks;
	while Mek <> Nil do begin
		if NotDestroyed( Mek ) and ( not GearOperational( Mek ) ) then begin
			{ Remove any pilots that may be in the mek... }
			repeat
				M2 := ExtractPilot( Mek );
				if M2 <> Nil then DeployMek( GB , M2 , False );
			until M2 = Nil;

			SetNAtt( Mek^.NA , NAG_Location , NAS_Team , NAV_DefPlayerTeam );
		end else if ( Mek^.G = GG_Mecha ) and ( not GearOperational( Mek ) ) and CanScavenge and ( NAttValue( Mek^.NA , NAG_Location , NAS_Team ) <> NAV_DefPlayerTeam ) and ( NAttValue( Mek^.NA , NAG_Location , NAS_Team ) <> NAV_LancemateTeam ) then begin
			{ Remove any pilots that may be in the mek... }
			repeat
				M2 := ExtractPilot( Mek );
				if M2 <> Nil then DeployMek( GB , M2 , False );
			until M2 = Nil;

			M2 := SelectRandomGear( Mek^.SubCom );
			if NotDestroyed( M2 ) and CanBeExtracted( M2 ) and ( RollStep( SkillValue( PC , 15 ) ) > 15 ) then begin
				DelinkGear( Mek^.SubCom , M2 );
				SetNAtt( M2^.NA , NAG_Location , NAS_Team , NAV_DefPlayerTeam );
				AppendGear( GB^.Meks , M2 );
			end;
		end;
		Mek := Mek^.Next;
	end;
end;

Procedure ProcessRetreat( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ When this command is invoked, all the functioning masters }
	{ belonging to the listed team are removed from the map. A }
	{ Number Of Units trigger is then set. }
var
	Team: Integer;
	Mek: GearPtr;
	P: Point;
begin
	{ ERROR CHECK - GB must be defined!!! }
	if GB = Nil then Exit;

	{ Find out which team is running away. }
	Team := ScriptValue( event , GB , Source );

	{ Loop through every mek on the board. }
	Mek := GB^.Meks;
	while Mek <> Nil do begin
		if GearOperational( Mek ) and ( NAttValue( Mek^.NA , NAG_Location , NAS_Team ) = Team ) then begin
			P := GearCurrentLocation( Mek );
			SetNAtt( Mek^.NA , NAG_Location , NAS_X , 0 );
			SetNAtt( Mek^.NA , NAG_Location , NAS_Y , 0 );
			RedrawTile( GB , P.X , P.Y );
		end;
		Mek := Mek^.Next;
	end;

	{ Set the trigger. }
	SetTrigger( GB , TRIGGER_NumberOfUnits + BStr( Team ) );
end;

Procedure ProcessAirRaidSiren( GB: GameBoardPtr );
	{ When this command is invoked, all the functioning masters }
	{ belonging to all NPC teams are ordered to run for their lives. }
var
	Mek: GearPtr;
begin
	{ ERROR CHECK - GB must be defined!!! }
	if GB = Nil then Exit;

	{ Loop through every mek on the board. }
	Mek := GB^.Meks;
	while Mek <> Nil do begin
		if GearOperational( Mek ) and ( NAttValue( Mek^.NA , NAG_Location , NAS_Team ) <> NAV_DefPlayerTeam ) then begin
			SetNAtt( Mek^.NA , NAG_EpisodeData , NAS_Orders , NAV_RunAway );
		end;
		Mek := Mek^.Next;
	end;
end;

Procedure ProcessGRunAway( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ When this command is invoked, the grabbed gear is removed }
	{ from the map. A Number Of Units trigger is then set. }
var
	Mek,NPC: GearPtr;
	P: Point;
begin
	{ ERROR CHECK - GB must be defined!!! }
	if ( GB = Nil ) or ( Grabbed_Gear = Nil ) then Exit;

	{ Loop through every mek on the board. }
	Mek := GB^.Meks;
	while Mek <> Nil do begin
		if Mek = Grabbed_Gear then begin
			P := GearCurrentLocation( Mek );
			SetNAtt( Mek^.NA , NAG_Location , NAS_X , 0 );
			SetNAtt( Mek^.NA , NAG_Location , NAS_Y , 0 );
			RedrawTile( GB , P.X , P.Y );

			{ Set the trigger. }
			SetTrigger( GB , TRIGGER_NumberOfUnits + BStr( NAttValue( MEK^.NA , NAG_Location , NAS_Team ) ) );
		end else if IsMasterGear( Mek ) then begin
			NPC := LocatePilot( Mek );
			if ( NPC <> Nil ) and ( NPC = Grabbed_Gear ) then begin
				P := GearCurrentLocation( Mek );
				SetNAtt( Mek^.NA , NAG_Location , NAS_X , 0 );
				SetNAtt( Mek^.NA , NAG_Location , NAS_Y , 0 );
				RedrawTile( GB , P.X , P.Y );

				{ Set the trigger. }
				SetTrigger( GB , TRIGGER_NumberOfUnits + BStr( NAttValue( MEK^.NA , NAG_Location , NAS_Team ) ) );
			end;
		end;
		Mek := Mek^.Next;
	end;
end;


Procedure ProcessTime( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Advance the game clock by a specified amount. }
var
	N: LongInt;
begin
	{ Find out how much to adjust the value by. }
	N := ScriptValue( Event , GB , Source );


	QuickTime( GB , N );
end;

Procedure ProcessForceChat( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Force the player to talk with the specified NPC. }
var
	N: LongInt;
begin
	{ Find out which NPC to speak with. }
	N := ScriptValue( Event , GB , Source );

	if GB <> Nil then begin
		StoreSAtt( GB^.Trig , '!TALK ' + BStr( N ) );
	end;
end;

Procedure ProcessTrigger( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ A new trigger will be placed in the trigger queue. }
var
	BaseTrigger: String;
	N: LongInt;
begin
	{ Find out the trigger's details. }
	BaseTrigger := ExtractWord( Event );
	N := ScriptValue( Event , GB , Source );

	if GB <> Nil then begin
		StoreSAtt( GB^.Trig , BaseTrigger + BStr( N ) );
	end;
end;

Procedure ProcessTransform( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Alter the appearance of the SOURCE gear. }
var
	N: LongInt;
	S: String;
	Procedure SwapSAtts( tag: String );
	begin
		S := AS_GetString ( Source , tag + BStr( N ) );
		if S <> '' then SetSAtt( Source^.SA , tag + ' <' + S + '>' );
	end;
begin
	{ Find out which aspect to change to. }
	N := ScriptValue( Event , GB , Source );

	{Switch all known dispay descriptors. }
	SwapSAtts( 'ROGUECHAR' );
	SwapSAtts( 'NAME' );
	SwapSAtts( 'SDL_SPRITE' );
	SwapSAtts( 'SDL_COLORS' );
	SetNAtt( Source^.NA , NAG_Display , NAS_PrimaryFrame , NAttValue( Source^.NA , NAG_Display , N ) );

	if GB <> Nil then begin
		RedrawTile( GB , Source );
		{ While we're here, redo the shadow map. }
		UpdateShadowMap( GB );
	end;
end;

Procedure ProcessMoreText( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Load and display a text file. }
var
	FName: String;
	txt,L: SAttPtr;
begin
	{ First, find the file name of the text file to look for. }
	FName := ExtractWord( Event );
	if Source <> Nil then begin
		FName := AS_GetString( Source , FName );
	end else begin
		FName := '';
	end;

	{ Secondly, load and display the file. }
	if FName <> '' then begin
		txt := LoadStringList( Series_Directory + FName );


		if txt <> Nil then begin
			{ Process the text. }
			L := txt;
			while L <> Nil do begin
				FormatMessageString( L^.Info , GB , Source );
                if L^.Info = '' then L^.Info := ' ';
				L := L^.Next;
			end;

            {$IFDEF SDLMODE}
            ASRD_GameBoard := GB;
            MoreText( txt, 1 , @ArenaScriptRedraw );
            {$ELSE}
	        MoreText( txt , 1 );
            {$ENDIF}
			DisposeSAtt( txt );

			{ Restore the display. }
            {$IFNDEF SDLMODE}
			if IntMenu <> Nil then begin
				SetupInteractDisplay( TeamColor( GB , I_NPC ) );
				DisplayGearInfo( I_NPC , GB );
				DisplayGearInfo( I_PC , GB , ZONE_Menu );

			end else if GB <> Nil then begin
				GFCombatDisplay( GB );

			end;
            {$ENDIF}
		end;
	end;

end; { ProcessMoreText }

Procedure ProcessMoreMemo( var Event: String; GB: GameBoardPtr );
	{ View messages of a certain type - EMAIL, NEWS, or MEMO. }
var
	Key: String;
begin
	{ First, find the memo key to use. }
	Key := ExtractWord( Event );

	{ Secondly, send this to the memo browser. }
	BrowseMemoType( GB , Key );

	{ Finally, update the display. }
    {$IFNDEF SDLMODE}
	GFCombatDisplay( GB );
    {$ENDIF}
end; { ProcessMoreMemo }

Procedure ProcessSeekGate( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Aim for a specific gate when entering the next level. }
var
	N: LongInt;
begin
	{ Find out which gate we're talking about. }
	N := ScriptValue( Event , GB , Source );

	SCRIPT_Gate_To_Seek := N;
end;

Procedure ProcessUpdateProps( GB: GameBoardPtr );
	{ Just send an UPDATE trigger to all items on the gameboard. }
var
	T: String;
begin
	T := 'UPDATE';
	if GB <> Nil then begin
		CheckTriggerAlongPath( T , GB , GB^.Meks , True );
	end;
end;

Procedure ProcessBlock( var T: String );
	{ Erase the trigger, so as to prevent other narrative gears }
	{ from acting upon it. }
begin
	{ Do I really need to comment this line? }
	T := '';
end;

Procedure ProcessAccept( var T: String );
	{ Set the trigger to ACCEPT so the CONDITIONACCEPTED function }
	{ knows that it's been accepted. }
begin
	{ Do I really need to comment this line? }
	T := 'ACCEPT';
end;

Procedure ProcessBomb( GB: GameBoardPtr );
	{ Drop a bomb on the town. Yay! }
begin
	RandomExplosion( GB );
end;

Procedure ProcessXPV( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Give some experience points to all PCs and lancemates. }
var
	XP,T,N,Ld: LongInt;
	M,PC: GearPtr;
begin
	{ Find out how much to give. }
	XP := ScriptValue( Event , GB , Source );

	{ Search for models to give XP to. }
	{ Do the first pass to count them, the second pass to award them. }
	if GB <> Nil then begin
		N := 0;
		Ld := 0;
		M := GB^.Meks;
		while M <> Nil do begin
			T := NAttValue( M^.NA , NAG_Location , NAS_Team );
			if ( T = NAV_DefPlayerTeam ) then begin
				{ At this time, also record the LEADERSHIP rating. }
				PC := LocatePilot( M );
				if ( PC <> Nil ) and ( NAttValue( PC^.NA , NAG_Skill , 39 ) > Ld ) then Ld := NAttValue( PC^.NA , NAG_Skill , 39 );
				if GearActive( M ) then Inc( N );
			end else if ( T = NAV_LancemateTeam ) and OnTheMap( M ) and GearActive( M ) then begin
				Inc( N );
			end;
			M := M^.Next;
		end;

		{ Based on the number of characters found, modify the XP award downwards. }
		if ( N > 1 ) and ( N > (( Ld + 1 ) div 2 ) ) then begin
			XP := XP div ( N - (( Ld + 1 ) div 2 ) );
			if XP < 1 then XP := 1;
		end;

		{ On the second pass actually give the XP. }
		M := GB^.Meks;
		while M <> Nil do begin
			T := NAttValue( M^.NA , NAG_Location , NAS_Team );
			if ( T = NAV_DefPlayerTeam ) then begin
				DoleExperience( M , XP );
			end else if ( T = NAV_LancemateTeam ) and OnTheMap( M ) then begin
				DoleExperience( M , XP );
			end;
			M := M^.Next;
		end;
	end;

	DialogMsg( ReplaceHash( MSgString( 'AS_XPV' ) , Bstr( XP ) ) );
end;


Procedure ProcessGSkillXP( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Give some skill experience points to the grabbed gear. }
var
	Sk,XP: LongInt;
begin
	{ Find out what skill to give XP for, and how much XP to give. }
	Sk := ScriptValue( Event , GB , Source );
	XP := ScriptValue( Event , GB , Source );

	{ As long as we have a grabbed gear, go for it! }
	if Grabbed_Gear <> Nil then begin
		DoleSkillExperience( Grabbed_Gear , Sk , XP );
	end;
end;

Procedure ProcessGMental( GB: GameBoardPtr );
	{ The grabbed gear is doing something. Make it wait, and spend }
	{ one mental point. }
begin
	{ As long as we have a grabbed gear, go for it! }
	if Grabbed_Gear <> Nil then begin
		WaitAMinute( GB , Grabbed_Gear , ReactionTime( Grabbed_Gear ) * 3 );
		AddMentalDown( Grabbed_Gear , 5 );
	end;
end;

Procedure ProcessGQuitLance( GB: GameBoardPtr );
	{ The grabbed gear will quit the lance. }
begin
	if Grabbed_Gear <> Nil then begin
		RemoveLancemate( GB , Grabbed_Gear );
	end;
end;

Procedure ProcessGSkillLevel( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Set the skill points for the grabbed gear. }
var
	Skill: NAttPtr;
	SkLvl: Integer;
	Pilot: GearPtr;
begin
	{ Find out what level the NPC should be at. }
	SkLvl := ( ScriptValue( Event , GB , Source ) div 7 ) + 3;
	if SkLvl < 1 then SkLvl := 1;

	{ As long as we have a grabbed gear, go for it! }
	Pilot := LocatePilot( Grabbed_Gear );
	if ( Pilot <> Nil ) then begin
		Skill := Pilot^.NA;
		while Skill <> Nil do begin
			if Skill^.G = NAG_Skill then begin
				Skill^.V := SkLvl;
			end;
			Skill := Skill^.Next;
		end;
	end;
end;

Procedure ProcessGAbsoluteLevel( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Set the skill points for the grabbed gear. }
	{ Unlike the above procedure, this one scales the skill points by a % based on }
	{ their current value. }
var
	Skill: NAttPtr;
	SkLvl: Integer;
begin
	{ Find out what level the NPC should be at. }
	SkLvl := ScriptValue( Event , GB , Source ) + 35;
	if SkLvl < 1 then SkLvl := 1;

	{ As long as we have a grabbed gear, go for it! }
	if ( Grabbed_Gear <> Nil ) then begin
		Skill := Grabbed_Gear^.NA;
		while Skill <> Nil do begin
			if Skill^.G = NAG_Skill then begin
				Skill^.V := ( Skill^.V * SkLvl ) div 100;
				if Skill^.V < 1 then Skill^.V := 1;
			end;
			Skill := Skill^.Next;
		end;
	end;
end;

Procedure ProcessGMoraleDmg( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Give some morale points to the grabbed gear. }
var
	M: LongInt;
begin
	{ Find out how much morale change. }
	M := ScriptValue( Event , GB , Source );

	{ As long as we have a grabbed gear, go for it! }
	if Grabbed_Gear <> Nil then begin
		AddMoraleDMG( Grabbed_Gear , M );
	end;
end;

Procedure ProcessDrawTerr( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ Alter a single gameboard tile. }
var
	X,Y,T: LongInt;
begin
	{ Find out where and what to adjust. }
	X := ScriptValue( Event , GB , Source );
	Y := ScriptValue( Event , GB , Source );
	T := ScriptValue( Event , GB , Source );

	if ( GB <> NIl ) and OnTheMap( X , Y ) and ( T >= 1 ) and ( T <= NumTerr ) then begin
		GB^.Map[ X , Y ].terr := T;
		GB^.Map[ X , Y ].visible := False;
	end;
end;

Procedure ProcessMagicMap( GB: GameBoardPtr );
	{ Make every tile on the map visible, then redraw. }
var
	X,Y: Integer;
begin
	for X := 1 to XMax do begin
		for Y := 1 to YMax do begin
			GB^.Map[ X , Y ].Visible := True;
		end;
	end;
    {$IFNDEF SDLMODE}
	GFCombatDisplay( GB );
    {$ENDIF}
end;

Procedure ProcessLoseRenown( GB: GameBoardPtr );
	{ The PC has just done something to lose face. Reduce the RENOWN attribute }
	{ by 25% of its total or 5 points, whichever is more severe. }
var
	PC: GearPtr;
	Renown: Integer;
begin
	PC := LocatePilot( GG_LocatePC( GB ) );
	if PC <> Nil then begin
		Renown := NAttValue( PC^.NA , NAG_CHarDescription , NAS_Renowned );
		if Renown > 23 then begin
			AddReputation( PC , -NAS_Renowned , -( Renown div 4 ) );
		end else begin
			AddReputation( PC , -NAS_Renowned , -5 );
		end;
	end;
end;

Procedure ProcessBonding( var Event: String; GB: GameBoardPtr; Source: GearPtr );
	{ The PC and the NPC are bonding over something. Maybe they'll become friends. }
var
	CID,CReact: LongInt;
    PC,NPC: GearPtr;
begin
	{ Find out which NPC to speak with. }
	CID := ScriptValue( Event , GB , Source );
	PC := LocatePilot( GG_LocatePC( GB ) );
    NPC := GG_LocateNPC( CID, GB, Source );

	if (NPC <> Nil) and (PC <> Nil) and (GB <> Nil) and (NAttValue(NPC^.NA,NAG_Relationship,0) = 0) then begin
        { Check the reaction score with this NPC. If appropriate, become more }
        { than just friends... lancemates! }
        CReact := ReactionScore( GB^.Scene, PC, NPC );
        if ( Random(50) + Random(50) + 45 ) < CReact then begin
            { This NPC will become an ally of some type. }
        	if IsSexy( PC , NPC ) and ( Random( 200 ) < CReact ) then begin
                SetNAtt(NPC^.NA,NAG_Relationship,0,NAV_Lover);
                DialogMsg(ReplaceHash(MsgString('BONDING_LOVE'),SAttValue(NPC^.SA,'NAME')));
            end else begin
                SetNAtt(NPC^.NA,NAG_Relationship,0,NAV_ArchAlly);
                DialogMsg(ReplaceHash(MsgString('BONDING_ALLY'),SAttValue(NPC^.SA,'NAME')));
            end;
        end else begin
            { This NPC is not ready to become an ally. Add some like. }
            if CReact < 0 then AddNAtt( PC^.NA , NAG_ReactionScore, 0, Random(10)+10 );
            AddNAtt( PC^.NA , NAG_ReactionScore, 0, Random( 6 ) + 1 );
        end;
	end;
end;


Procedure CheckMechaEquipped( GB: GameBoardPtr );
	{ A dynamic encounter is about to be entered. The PC is going to }
	{ want a mecha for it, most likely. }
var
	PC,Mek: GearPtr;
begin
	{ Error check - make sure we have a gameboard to start with. }
	if GB = Nil then Exit;

	{ Find the PC. If the PC doesn't have a mek equipped, then }
	{ prompt for one to be equipped. }
	PC := GG_LocatePC( GB );
	if ( PC <> Nil ) and ( PC^.G <> GG_Mecha ) then begin
		Mek := FindPilotsMecha( GB^.Meks , PC );
		if ( Mek = Nil ) and ( NumPCMeks( GB ) > 0 ) then begin
{$IFNDEF SDLMODE}
			DrawZoneBorder( ZONE_YesNoTotal , BorderBlue );
			GameMSG( MsgString( 'ARENASCRIPT_CheckMechaEquipped' ) , ZONE_UsagePrompt , InfoGreen );
{$ENDIF}
{$IFDEF SDLMODE}
			FHQ_SelectMechaForPilot( GB, PC );
{$ELSE}
			MechaSelectionMenu( GB^.Meks , PC , ZONE_UsageMenu );
{$ENDIF}
		end;
	end;
end;

Procedure InvokeEvent( Event: String; GB: GameBoardPtr; Source: GearPtr; var Trigger: String );
	{ Do whatever is requested by game script EVENT. }
	{ SOURCE refers to the virtual gear which is currently being }
	{ used- it may be a SCENE gear, or a CONVERSATION gear, or }
	{ whatever else I might add in the future. }
var
	cmd: String;
begin
	while ( Event <> '' ) do begin
		cmd := UpCase( ExtractWord( Event ) );

		if SAttValue( Script_Macros , cmd ) <> '' then begin
			{ Install the macro. }
			InitiateMacro( Event , SAttValue( Script_Macros , cmd ) );

		end else if not Attempt_Gear_Grab( Cmd , Event , GB , Source ) then begin
			{ If this is a gear-grabbing command, our work here is done. }

		    if cmd = 'EXIT' then ProcessExit( Event , GB , Source )
		    else if cmd = 'BONDWITH' then ProcessBonding( Event , GB , Source )
		    else if cmd = 'GADDNATT' then ProcessGAddNAtt( Event , GB , Source )
		    else if cmd = 'GSETNATT' then ProcessGSetNAtt( Event , GB , Source )
		    else if cmd = 'GADDSTAT' then ProcessGAddStat( Event , GB , Source )
		    else if cmd = 'GSETSTAT' then ProcessGSetStat( Event , GB , Source )
		    else if cmd = 'GSETSATT' then ProcessGSetSAtt( Event , Source )
		    else if cmd = 'DELETEGG' then ProcessDeleteGG( GB , Source )
		    else if cmd = 'MOVEGG' then ProcessMoveGG( Event , GB , Source )
		    else if cmd = 'DEPLOYGG' then ProcessDeployGG( Event , GB , Source )
		    else if cmd = 'DYNAGG' then ProcessDynaGG( Event , GB , Source )
		    else if cmd = 'GIVEGG' then ProcessGiveGG( GB )
		    else if cmd = 'GNEWPART' then ProcessGNewPart( Event , GB , Source )
		    else if cmd = 'RETURN' then ProcessReturn( GB )
		    else if cmd = 'PRINT' then ProcessPrint( Event , GB , Source )
		    else if cmd = 'ALERT' then ProcessAlert( Event , GB , Source )
		    else if cmd = 'MEMO' then ProcessMemo( Event , GB , Source )
		    else if cmd = 'NEWS' then ProcessNews( Event , GB , Source )
		    else if cmd = 'EMAIL' then ProcessEMail( Event , GB , Source )
		    else if cmd = 'HISTORY' then ProcessHistory( Event , GB , Source )
		    else if cmd = 'VICTORY' then ProcessVictory( GB )
		    else if cmd = 'VMSG' then ProcessValueMessage( Event , GB , Source )
		    else if cmd = 'SAY' then ProcessSay( Event , GB , Source )
		    else if cmd = 'SAYANYTHING' then ProcessSayAnything()
		    else if cmd = 'IFGINPLAY' then ProcessIfGInPlay( Event , Source )
		    else if cmd = 'IFGOK' then ProcessIfGOK( Event , Source )
		    else if cmd = 'IFGEXISTS' then ProcessIfGExists( Event , Source )
		    else if cmd = 'IFGSEXY' then ProcessIfGSexy( Event , GB , Source )
		    else if cmd = 'IFGARCHENEMY' then ProcessIfGArchEnemy( Event , GB , Source )
		    else if cmd = 'IFGARCHALLY' then ProcessIfGArchAlly( Event , GB , Source )
		    else if cmd = 'IFFACTION' then ProcessIfFaction( Event , GB , Source )
		    else if cmd = 'IFSCENE' then ProcessIfScene( Event , GB , Source )
		    else if cmd = 'IFKEYITEM' then ProcessIfKeyItem( Event , GB , Source )
		    else if cmd = 'IF=' then ProcessIfEqual( Event , GB , Source )
		    else if cmd = 'IF#' then ProcessIfNotEqual( Event , GB , Source )
		    else if cmd = 'IFG' then ProcessIfGreater( Event , GB , Source )
		    else if cmd = 'IFSTORYLESS' then ProcessIfStoryless( Event , Source )
		    else if cmd = 'IFYESNO' then ProcessIfYesNo( Event , GB , Source )
		    else if cmd = 'IFNOOBJECTIONS' then ProcessIfNoObjections( Event , GB , Source )
		    else if cmd = 'TORD' then ProcessTeamOrders( Event , GB , Source )
		    else if cmd = 'COMPOSE' then ProcessCompose( Event , GB , Source )
		    else if cmd = 'BLOCK' then ProcessBlock( Trigger )
		    else if cmd = 'ACCEPT' then ProcessAccept( Trigger )
		    else if cmd = 'NEWCHAT' then ProcessNewChat
		    else if cmd = 'ENDCHAT' then ProcessEndChat
		    else if cmd = 'GOTO' then ProcessGoto( Event , Source )
		    else if cmd = 'ADDCHAT' then ProcessAddChat( Event , GB , Source )
		    else if cmd = 'SEEKTERR' then ProcessSeekTerr( Event , GB , Source )
		    else if cmd = 'SHOP' then ProcessShop( Event , GB , Source )
		    else if cmd = 'SCHOOL' then ProcessSchool( Event , GB , Source )
		    else if cmd = 'EXPRESSDELIVERY' then ProcessExpressDelivery( Event , GB , Source )
		    else if cmd = 'SHUTTLESERVICE' then ProcessShuttle( Event , GB , Source )
		    else if cmd = 'ADVANCEPLOT' then ProcessAdvancePlot( Event , GB , Source )
		    else if cmd = 'ENDSTORY' then ProcessEndStory( GB , Source )
		    else if cmd = 'PURGESTORY' then ProcessPurgeStory( GB , Source )
		    else if cmd = 'TREPUTATION' then ProcessTReputation( Event , GB , Source )
		    else if cmd = 'XPV' then ProcessXPV( Event , GB , Source )
		    else if cmd = 'MECHAPRIZE' then ProcessMechaPrize( Event , GB , Source )
		    else if cmd = 'SETSCENEFACTION' then ProcessSetSceneFaction( Event , GB , Source )
		    else if cmd = 'DELETEFACTION' then ProcessDeleteFaction( Event , GB , Source )
		    else if cmd = 'NEWD' then ProcessNewD( Event , GB , Source )
		    else if cmd = 'LOADD' then ProcessLoadD( Event , GB , Source )
		    else if cmd = 'TSTOCKD' then ProcessTStockD( Event , GB , Source )
		    else if cmd = 'WMECHA' then ProcessWMecha( Event , GB , Source )
		    else if cmd = 'TMSTOCKD' then ProcessTMStockD( Event , GB , Source )
		    else if cmd = 'ENCOUNTER' then ProcessEncounter( Event , GB , Source )
		    else if cmd = 'STORYLINE' then ProcessStoryLine( Event , GB , Source )
		    else if cmd = 'BATCHLOADPLOT' then ProcessBatchLoadPlot( Trigger , Event , GB , Source )
		    else if cmd = 'XRANPLOT' then ProcessXRanPlot( Event , GB , Source )
		    else if cmd = 'STARTSTORY' then ProcessStartStory( Event , GB , Source )
		    else if cmd = 'GLOBALSTORYPATTERN' then ProcessGlobalStoryPattern( Event , GB , Source )
		    else if cmd = 'ATTACK' then ProcessAttack( Event , GB , Source )
		    else if cmd = 'SALVAGE' then ProcessSalvage( GB )
		    else if cmd = 'RETREAT' then ProcessRetreat( Event , GB , Source )
		    else if cmd = 'GRUNAWAY' then ProcessGRunAway( Event , GB , Source )
		    else if cmd = 'AIRRAIDSIREN' then ProcessAirRaidSiren( GB )
		    else if cmd = 'FORCECHAT' then ProcessForceChat( Event , GB , Source )
		    else if cmd = 'TIME' then ProcessTime( Event , GB , Source )
		    else if cmd = 'TRANSFORM' then ProcessTransform( Event , GB , Source )
		    else if cmd = 'SEEKGATE' then ProcessSeekGate( Event , GB , Source )
		    else if cmd = 'TRIGGER' then ProcessTrigger( Event , GB , Source )
		    else if cmd = 'UPDATEPROPS' then ProcessUpdateProps( GB )
		    else if cmd = 'MORETEXT' then ProcessMoreText( Event , GB , Source )
		    else if cmd = 'MOREMEMO' then ProcessMoreMemo( Event , GB )
		    else if cmd = 'BOMB' then ProcessBomb( GB )
		    else if cmd = 'GSKILLXP' then ProcessGSkillXP( Event , GB , Source )
		    else if cmd = 'GABSOLUTELEVEL' then ProcessGAbsoluteLevel( Event , GB , Source )
		    else if cmd = 'GSKILLLEVEL' then ProcessGSkillLevel( Event , GB , Source )
		    else if cmd = 'GMORALEDMG' then ProcessGMoraleDmg( Event , GB , Source )
		    else if cmd = 'DRAWTERR' then ProcessDrawTerr( Event , GB , Source )
		    else if cmd = 'MAGICMAP' then ProcessMagicMap( GB )
		    else if cmd = 'GMENTAL' then ProcessGMental( GB )
		    else if cmd = 'GQUITLANCE' then ProcessGQuitLance( GB )
		    else if cmd = 'LOSERENOWN' then ProcessLoseRenown( GB )


		    else if cmd <> '' then begin
				    DialogMsg( 'ERROR: Unknown ASL command ' + cmd + ' in ' + GearName( Source ) );
				    DialogMsg( 'CONTEXT: ' + event );
			end;

		end; { If not AttemptGearGrab }
	end;

	{ Process rounding-up events here. }
	if ( SCRIPT_DynamicEncounter <> Nil ) and ( SCRIPT_DynamicEncounter^.V > 0 ) then CheckMechaEquipped( GB );
end;

Procedure HandleChat( GB: GameBoardPtr; var FreeRumors: Integer );
	{ Call the CHAT procedure, then display the string that is returned. }
var
	msg: String;
begin
	msg := DoChatting( GB , I_Rumors , I_PC , I_NPC , I_Endurance , FreeRumors );
{$IFDEF SDLMODE}
	CHAT_Message := msg;
{$ELSE}
	GameMsg( msg , ZONE_InteractMsg , InfoHiLight );
{$ENDIF}
	QuickTime( GB , 16 + Random( 15 ) );
end;

Procedure AddLancemate( GB: GameBoardPtr; NPC: GearPtr );
	{ Add the listed NPC to the PC's lance. }
begin
	{ This NPC will have to quit their current team to do this... }
	{ so, better set a trigger. }
	SetTrigger( GB , TRIGGER_NumberOfUnits + BStr( NAttValue( NPC^.NA , NAG_Location , NAS_Team ) ) );

	SetNAtt( NPC^.NA , NAG_Location , NAS_Team , NAV_LancemateTeam );
end;

Procedure AttemptJoin( GB: GameBoardPtr );
	{ I_NPC will attempt to join the party. Yay! }
var
	LMP: Integer;	{ Lancemate Points needed }
begin
	{ Make sure we've got an NPC to deal with. }
	if I_NPC = Nil then Exit;

	{ Need two more available lancemate points than are currently in use. }
	LMP := LancematesPresent( GB ) + 2;
	if ReactionScore( GB^.Scene , I_PC , I_NPC ) < ( 50 - 2 * I_PC^.Stat[ STAT_Charm ] ) then begin
{$IFDEF SDLMODE}
		CHAT_Message := MsgString( 'JOIN_REFUSE' );
{$ELSE}
		GameMsg( MsgString( 'JOIN_REFUSE' ) , ZONE_InteractMsg , InfoHiLight );
{$ENDIF}
	end else if FindPersonaPlot( FindRoot( GB^.Scene ) , NAttValue( I_NPC^.NA , NAG_Personal , NAS_CID ) ) <> Nil then begin
{$IFDEF SDLMODE}
		CHAT_Message := MsgString( 'JOIN_BUSY' );
{$ELSE}
		GameMsg( MsgString( 'JOIN_BUSY' ) , ZONE_InteractMsg , InfoHiLight );
{$ENDIF}
	end else if LMP > LancematePoints( I_PC ) then begin
{$IFDEF SDLMODE}
		CHAT_Message := MsgString( 'JOIN_NOPOINT' );
{$ELSE}
		GameMsg( MsgString( 'JOIN_NOPOINT' ) , ZONE_InteractMsg , InfoHiLight );
{$ENDIF}

	end else begin
{$IFDEF SDLMODE}
		CHAT_Message := MsgString( 'JOIN_JOIN' );
{$ELSE}
		GameMsg( MsgString( 'JOIN_JOIN' ) , ZONE_InteractMsg , InfoHiLight );
{$ENDIF}
		AddLancemate( GB , I_NPC );
	end;
end;

Procedure RemoveLancemate( GB: GameBoardPtr; NPC: GearPtr );
	{ Remove NPC from the party. }
	{ ERROR CHECK: Lancemates cannot be removed in dynamic scenes! }
begin
	if not IsInvCom( GB^.Scene ) then begin
		SetSAtt( NPC^.SA , 'TEAMDATA <Ally>' );
		ChooseTeam( NPC , GB^.Scene );
	end;
end;

Procedure HandleQuit( GB: GameBoardPtr );
	{ I_NPC will quit the party. }
begin
	if I_NPC = Nil then Exit;
{$IFDEF SDLMODE}
	CHAT_Message := MsgString( 'QUIT_LANCE' );
{$ELSE}
	GameMsg( MsgString( 'QUIT_LANCE' ) , ZONE_InteractMsg , InfoHiLight );
{$ENDIF}
	RemoveLancemate( GB , I_NPC );
end;

{$IFDEF SDLMODE}
Procedure InteractRedraw;
	{ Redraw the screen for whatever interaction is going to go on. }
begin
	SDLCombatDisplay( ASRD_GameBoard );
	SetupInteractDisplay( TeamColor( ASRD_GameBoard , I_NPC ) );
	if I_NPC <> Nil then begin
		DisplayInteractStatus( ASRD_GameBoard , I_NPC , CHAT_React , I_Endurance );
		{DisplayGearInfo( I_NPC , ASRD_GameBoard );}
	end;
	GameMsg( CHAT_Message , ZONE_InteractMsg.GetRect() , InfoHiLight );
end;
{$ENDIF}

Procedure PruneNothings( var LList: GearPtr );
	{ Traverse the list. Anything marked as ABSOLUTELYNOTHING gets deleted, along with }
	{ all of its children gears. That's tough, but that's life... }
var
	L,L2: GearPtr;
begin
	L := LList;
	while L <> Nil do begin
		L2 := L^.Next;

		if L^.G = GG_AbsolutelyNothing then begin
			RemoveGear( LList , L );
		end else begin
			PruneNothings( L^.SubCom );
			PruneNothings( L^.InvCom );
		end;

		L := L2;
	end;
end;

Procedure HandleInteract( GB: GameBoardPtr; PC,NPC,Interact: GearPtr );
	{ The player has just entered a conversation. }
	{ HOW THIS WORKS: The interaction menu is built by an ASL script. }
	{ the player selects one of the provided responses, which will }
	{ either trigger another script ( V >= 0 ) or call one of the }
	{ standard interaction routines ( V < 0 ) }
var
	IntScr: String;		{ Interaction Script }
	N,FreeRumors: Integer;
	RTT: LongInt;		{ ReTalk Time }
	T: String;
    IntRoot: GearPtr;
begin
	{ Start by allocating the menu. }
	IntMenu := CreateRPGMenu( MenuItem , MenuSelect , ZONE_InteractMenu );

	{ Set up the display. }
	SetupInteractDisplay( TeamColor( GB , NPC ) );

    {$IFNDEF SDLMODE}
	DisplayGearInfo( NPC , GB );
	DisplayGearInfo( PC , GB , ZONE_Menu );
    {$ENDIF}

	{ Initialize interaction variables. }
	I_PC := PC;
	I_NPC := NPC;
	I_Rumors := CreateRumorList( GB , PC , NPC );
{$IFDEF SDLMODE}
	ASRD_GameBoard := GB;
{$ENDIF}

	{ If the NPC is fully recharged from talking with you last time, }
	{ get full endurance of 10. Otherwise, only gets partial endurance. }
	if NAttValue( NPC^.NA , NAG_Personal , NAS_ReTalk ) > GB^.ComTime then begin
		I_Endurance := 1;
	end else begin
		I_Endurance := 10;
	end;

	{ Determine the number of "Free" rumors the PC will get. }
	FreeRumors := 0;
	N := ReactionScore( GB^.Scene , PC , NPC );
	if N > 20 then FreeRumors := ( N - 13 ) div 7;
	N := CStat( PC , STAT_Charm );
	if N > 12 then FreeRumors := FreeRumors + Random( N - 11 );

	{ Invoke the greeting event. }
	if ( NAttValue( NPC^.NA , NAG_Location , NAS_Team ) = NAV_LancemateTeam ) and ( Interact <> Nil ) and ( Interact^.Parent <> Nil ) and ( Interact^.Parent^.G = GG_Scene ) then begin
		{ Lancemates won't use their local personas while part of the lance. }
		{ Hence the mother of all conditionals above... }
		IntScr := 'SAYANYTHING NEWCHAT';
	end else if Interact <> Nil then begin
		IntScr := AS_GetString( Interact , 'GREETING' );
	end else begin
		{ If there is no standard greeting, set the event to }
		{ build the default interaction menu. }
		IntScr := 'SAYANYTHING NEWCHAT';
	end;
	T := 'Greeting';
	InvokeEvent( IntScr , GB , Interact , T );

	repeat
		{ Print the NPC description. }
		{ This could change depending upon what the PC does. }
{$IFNDEF SDLMODE}
		{ SDLMode handles InteractStatus in the redraw, so there's no need to }
		{ do it here if we're in SDL mode... }
		DisplayInteractStatus( GB , NPC , ReactionScore( GB^.Scene , PC , NPC ) , I_Endurance );
{$ENDIF}

		if IntMenu^.NumItem > 0 then begin
{$IFDEF SDLMODE}
			ASRD_GameBoard := GB;
			CHAT_React := ReactionScore( GB^.Scene , PC , NPC );
			N := SelectMenu( IntMenu , @InteractRedraw );
{$ELSE}
			N := SelectMenu( IntMenu );
{$ENDIF}
		end else begin
			{ If the menu is empty, we must leave this procedure. }
			{ More importantly, we better not do anything in }
			{ the conditional below... Set N to equal a "goodbye" result. }
			N := -1;
		end;

		if N >= 0 then begin
			{ One of the placed options have been triggered. }
			{ Attempt to find the appropriate script to }
			{ invoke. }
			IntScr := AS_GetString( Interact , 'RESULT' + BStr( N ) );
			InvokeEvent( IntScr , GB , Interact , T );

		{ It wasn't a scripted response chosen. }
		{ Handle one of the standard options. }
		end else if N = CMD_Chat then begin
			HandleChat( GB , FreeRumors );

		end else if N = CMD_Join then begin
			AttemptJoin( GB );

		end else if N = CMD_Quit then begin
			HandleQuit( GB );

		end;

	until ( N = -1 ) or ( IntMenu^.NumItem < 1 ) or ( I_Endurance < 1 ) or ( I_NPC = Nil );

	{ If the menu is empty, pause for a minute. Or at least a keypress. }
	if IntMenu^.NumItem < 1 then begin
{$IFDEF SDLMODE}
		InteractRedraw;
		GHFlip;
{$ENDIF}
		EndOfGameMoreKey;
	end;

	{ If the conversation ended because the NPC ran out of patience, }
	{ store a negative reaction modifier. }
	if I_Endurance < 1 then begin
		AddNAtt( PC^.NA , NAG_ReactionScore , NAttValue( NPC^.NA , NAG_Personal , NAS_CID ) , -5 );

		{ Overchatting is a SOCIABLE action. }
		AddReputation( PC , 3 , 1 );
	end;

	{ Check - If this persona gear is the child of a gear whose type }
	{ is GG_ABSOLUTELYNOTHING, chances are that it used to be a plot }
	{ but it's been advanced by the conversation. Delete it. }
	{if Interact <> Nil then begin
		Interact := FindRoot( Interact );
		PruneNothings( Interact );
	end;}

	{ Set the ReTalk value. }
	{ Base retalk time is 1500 ticks; may be raised or lowered depending }
	{ upon the NPC's ENDURANCE and also how well the NPC likes the PC. }
	if ( I_NPC <> Nil ) and ( SCRIPT_DynamicEncounter = Nil ) then begin
		RTT := GB^.ComTime + 1500 - ( 15 * ReactionScore( GB^.Scene , PC , NPC ) ) - ( 50 * I_Endurance );
		if RTT < ( GB^.ComTime + AP_Minute ) then RTT := GB^.ComTime + AP_Minute;
		SetNAtt( NPC^.NA , NAG_Personal , NAS_ReTalk , RTT );
	end;

	{ Get rid of the menu. }
	DisposeRPGMenu( IntMenu );
	DisposeSAtt( I_Rumors );

    I_NPC := Nil;

	{ Restore the display. }
    {$IFNDEF SDLMODE}
	ClrZone( ZONE_InteractTotal );
    {$ENDIF}
end;

Procedure ForceInteract( GB: GameBoardPtr; CID: LongInt );
	{ Attempt to force the PC to converse with the provided NPC. }
var
	PC,NPC,Interact: GearPtr;
begin
	{ Locate all the required elements. }
	PC := LocatePilot( GG_LocatePC( GB ) );
	NPC := SeekGearByCID( GB^.Meks , CID );
	if NPC = Nil then NPC := SeekGearByCID( FindRoot( GB^.Scene ) , CID );
	Interact := SeekPersona( GB , CID );

	if ( PC <> Nil ) and ( NPC <> Nil ) and NotDestroyed( PC ) then begin
		{ Before initiating the conversation, get rid of the }
		{ recharge timer, since the NPC initiated this chat }
		{ and won't get pissed off. }
		SetNAtt( NPC^.NA , NAG_Personal , NAS_ReTalk , 0 );

		{ Hand everything to the interaction procedure. }
		HandleInteract( GB , PC , NPC , Interact );
	end;
    {$IFNDEF SDLMODE}
	DisplayMap( GB );
    {$ENDIF}
end;

Function TriggerGearScript( GB: GameBoardPtr; Source: GearPtr; var Trigger: String ): Boolean;
	{ Attempt to trigger the requested script in this gear. If the }
	{ script cannot be found, then do nothing. }
var
	E: String;
	it: Boolean;
begin
	it := False;
	if Source <> Nil then begin
		E := AS_GetString( Source , Trigger );
		if E <> '' then begin
			InvokeEvent( E , GB , Source , Trigger );
			it := True;
		end;
	end;
	TriggerGearScript := it;
end;

Function CheckTriggerAlongPath( var T: String; GB: GameBoardPtr; Plot: GearPtr; CheckAll: Boolean ): Boolean;
	{ Check all the active narrative gears in this list (plots, stories, and factions) }
	{ looking for events which match the provided trigger. }
	{ Return TRUE if an event was invoked, or FALSE if no event was encountered. }
var
	P2: GearPtr;
	it,I2: Boolean;
begin
	it := False;
	while ( Plot <> Nil ) and ( T <> '' ) do begin
		P2 := Plot^.Next;
		if CheckAll or ( Plot^.G = GG_Plot ) or ( Plot^.G = GG_Faction ) or ( Plot^.G = GG_Story ) or ( Plot^.G = GG_Adventure ) then begin
			{ FACTIONs and STORYs can hold active plots in their InvCom. }
			if ( Plot^.G = GG_Faction ) or ( Plot^.G = GG_Story ) or ( Plot^.G = GG_Adventure ) then CheckTriggerAlongPath( T , GB , Plot^.InvCom , CheckAll);

			I2 := TriggerGearScript( GB , Plot , T );
			it := it or I2;

			{ The trigger above might have changed the }
			{ structure, so reset P2. }
			P2 := Plot^.Next;

			{ Remove the plot, if it's been advanced. }
			{if Plot^.G = GG_AbsolutelyNothing then RemoveGear( Plot^.Parent^.InvCom , Plot );}
		end;
		Plot := P2;
	end;
	CheckTriggerAlongPath := it;
end;

Procedure DoScriptGC( GB: GameBoardPtr );
    { Get rid of any ABSOLUTELYNOTHINGS that may be hanging about. }
var
    Adv: GearPtr;
begin
    if NeedGC then begin
        Adv := GG_LocateAdventure( GB, Nil );
        if Adv <> Nil then begin
            PruneNothings( Adv^.SubCom );
            PruneNothings( Adv^.InvCom );
            NeedGC := False;
        end;
    end;
end;


Procedure HandleTriggers( GB: GameBoardPtr );
	{ Go through the list of triggers, enacting events if any are }
	{ found. Deallocate the triggers as they are processed. }
var
	TList,TP: SAttPtr;	{ Trigger List , Trigger Pointer }
	E: String;
begin
	IntMenu := Nil;

	{ Only try to implement triggers if this gameboard has a scenario }
	{ defined. }
	if GB^.Scene <> Nil then begin

		{ Some of the events we process might add their own }
		{ triggers to the list. So, we check all the triggers }
		{ currently set, then look at the GB^.Trig list again }
		{ to see if any more got put there. }
		while GB^.Trig <> Nil do begin
			{ Copy the list pointer to TList, clear the }
			{ list pointer from GB, and set the pointer }
			{ to the first trigger. }
			TList := GB^.Trig;
			GB^.Trig := Nil;
			TP := TList;

			while TP <> Nil do begin
				{ Brand New Thing - v0.531 July 18 2002 }
				{ Commands can be embedded in the triggers list. }
				{ The actual point of this is to allow scripts }
				{ to automatically activate interactions & props. }
				if ( Length( TP^.Info ) > 0 ) and ( TP^.Info[1] = '!' ) then begin
					{ Copy the command. }
					E := UpCase( ExtractWord( TP^.Info ) );
					DeleteFirstChar( E );

					if E = 'TALK' then begin
						ForceInteract( GB , ExtractValue( TP^.Info ) );
					end;

					{ Clear this trigger. }
					TP^.Info := '';

				end else if TP^.Info <> '' then begin
					{ If there is a SAtt in the scenario description }
					{ named after this trigger description, it will }
					{ happen now. First, see if such an event exists. }

					{ Check the PLOTS, FACTIONS and STORIES in }
					{ Adventure/InvCom first. }
					if GB^.Scene^.Parent <> Nil then begin
						CheckTriggerAlongPath( TP^.Info , GB , FindRoot( GB^.Scene ) , False );
					end;

					{ Check the current scene last. }
					if TP^.Info <> '' then TriggerGearScript( GB , GB^.Scene , TP^.Info );

				end;

				TP := TP^.Next;
			end;

			{ Get rid of the trigger list. }
			DisposeSAtt( TList );

		end;
        DoScriptGC( GB );
	end;
end;



initialization
	SCRIPT_DynamicEncounter := Nil;
	Grabbed_Gear := Nil;
	Script_Macros := LoadStringList( Script_Macro_File );
	Value_Macros := LoadStringList( Value_Macro_File );

	lancemate_tactics_persona := LoadFile( 'lmtactics.txt' , Data_Directory );

    I_NPC := Nil;

    NeedGC := False;


finalization
	if SCRIPT_DynamicEncounter <> Nil then begin
		DisposeGear( SCRIPT_DynamicEncounter );
	end;
	DisposeSAtt( Script_Macros );
	DisposeSAtt( Value_Macros );
	DisposeGear( lancemate_tactics_persona );

end.