File: effects.pp

package info (click to toggle)
gearhead 1.100-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 5,756 kB
  • ctags: 11
  • sloc: pascal: 34,876; makefile: 59; sh: 12
file content (2580 lines) | stat: -rw-r--r-- 95,733 bytes parent folder | download
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
unit effects;
	{ previosly attacker.pp }
	{ This unit handles attack, defense, and spells in GearHead. }
	{ Spells? What is this, a FRPG? Well, that's just how I usually }
	{ describe "special effects" such as healing, status changes, etc. }
	{ Eventually it may also describe psi powers and whatnot. }

	{ This unit does not concern itself with UI, so requesting }
	{ attacks and informing the user of their outcome }
	{ has to be done elsewhere. The EFFECTS_History variable }
	{ points to a list of SATTs describing the last processed }
	{ effect in full. It's up to the calling procedure to pass }
	{ this info on the user. }


	{ I've been prettying up this unit for the eventual introduction }
	{ of effects other than attacking. Getting rid of useless global }
	{ history variables, combining code when appropriate, removing }
	{ dependancy upon direct properties of gears... Still a long way }
	{ to go before this unit can be called "elegant", but at least }
	{ it's worlds better than pcaction.pp... }

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

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

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

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

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


interface

uses gears,locale;

const
	MOSMeasure = 5;	{ One Measure Of Success is gained for beating }
			{ the opponent's defense roll by this many points. }

	PenaltyPerDepth = 5;	{ Penalty for making called shots, per distance from root level. }
	PenaltyPerScale = 4;
	ClicksPerPenalty = 20;	{ For every 20dpr of a target's speed, there's a -1 modifier. }
	StopPenalty = 3;	{ Stopped mecha are easier to hit. }
	ImmobilePenalty = 15;	{ Broken down mecha are even easier. }

	StopBonus = 3;		{ It's easier to aim if you're standing still. }
	RunPenalty = 3;		{ It's harder to aim if you're traveling at full speed. }

	Parry_Bonus = 3;	{ Bonus to parrying an attack. }

	UnderwaterAttackPenalty = 3;	{ It's harder to aim if you're underwater. }

	Non_Weapon_MOS_Penalty = 2;	{ Non-weapons are less effective against armor. }
					{ This mostly applies to modules. }

	FlyingPenalty = 3;	{ Penalty for firing at an airborne unit without AntiAir weapon. }
	HighGroundBonus = 1;	{ Bonus for firing at a target lower than self. }

	CritHitMinTar = 25;	{ Minimum target number for Spot Weakness rolls. }
	LongRangePenalty = 3;
	ShortRangeBonus = 2;

	Has_Minimum_Range = 6;	{ Weapons with a range greater than this have a minimum useful range. }
			{ Attacks against targets within this minimum range happen at a penalty. }

	{ Animation directions are stored in the history list. }
	{ An animation direction may require some additional information, }
	{ as detailed below. }
	SAtt_Anim_Direction = 'ANIM_';
	
	GS_Shot = 1;		{ X1 Y1 Z1  X2 Y2 Z2 }
	GS_DamagingHit = 2;	{ X Y Z }
	GS_ArmorDefHit = 3;	{ X Y Z }
	GS_Parry = 4;		{ X Y Z }
	GS_Dodge = 5;		{ X Y Z }
	GS_Backlash = 6;	{ X Y Z }
	GS_AreaAttack = 7;	{ X Y Z }

	{ Maximum length of line when adding list of destroyed parts. }
	Damage_List_Text_Length = 170;

	FX_DoDamage = 0;
	FX_CauseStatusFX = 1;
	FX_DoHealing = 2;
	FX_Overload = 3;


var
	{ The variables in this unit hold values which may then be }
	{ examined by any calling procedures to determine the outcome }
	{ of the attack. }
	ATTACK_AttackRoll,ATTACK_HiDefRoll: Integer;
	ATTACK_Error,ATTACK_ItHit,ATTACK_Dodge,ATTACK_Parry,ATTACK_Resist: Boolean;
	ATTACK_DamageDone: Integer;
	ATTACK_MOS: Integer;	{ Attack Margin Of Success }
	ATTACK_NumberOfHits: Integer;
	ATTACK_AttSkillRank,ATTACK_DefSkillRank: Integer;
	ATTACK_AttackerDamage: Integer;

	ATTACK_History: SAttPtr;


Function ReadyToFire( GB: GameBoardPtr; User,Weapon: GearPtr ): Boolean;

Function ArcCheck( X0,Y0,D0,X1,Y1,A: Integer ): Boolean;
Function ArcCheck( GB: GameBoardPtr; Master , Weapon: GearPtr; X,Y: Integer ): Boolean;
Function ArcCheck( GB: GameBoardPtr; Master , Weapon , Target: GearPtr ): Boolean;
Function RangeArcCheck( GB: GameBoardPtr; Master , Weapon: GearPtr; X,Y,Z: Integer ): Boolean;
Function RangeArcCheck( GB: GameBoardPtr; Master , Weapon , Target: GearPtr ): Boolean;

Function AttackSkillNeeded( Attacker: GearPtr ): Integer;
Function CalcTotalModifiers( gb: GameBoardPtr; Attacker,Target: GearPtr; AtOp: Integer; AtAt: String ): Integer;

Procedure DestroyTerrain( GB: GameBoardPtr; X,Y: Integer );

Function BlastRadius( GB: GameBoardPtr; Attacker: GearPtr; AList: String ): Integer;

Procedure Explosion( GB: GameBoardPtr; X0,Y0,DC,R: Integer );

Procedure DoAttack( GB: GameBoardPtr; Attacker,Target: GearPtr; X,Y,Z,AtOp,AMod: Integer);

Procedure HandleEffectString( GB: GameBoardPtr; Target: GearPtr; FX_String,FX_Desc: String );


implementation

uses ability,action,damage,gearutil,ghchars,ghmodule,ghguard,ghparser,
     ghprop,ghsensor,ghsupport,ghweapon,movement,rpgdice,skilluse,texutil;

Type
	AttackFlags = Record
		CantCallShot: Boolean;
		CanDodge,CanBlock,CanParry,CanResist,CanIntercept,CanECM: Boolean;
		AtAt: String;
	end;

	EffectRequest = Record
		FXName: String;
		FXType: Integer;
		Originator,Weapon,Target: GearPtr;
		TX,TY: Integer;
		FXDice,FXOption,FXSkill,FXMod: Integer;
		AF: AttackFlags;
		FXDesc: String;
	end;

var
	EFFECTS_Event_Order: Integer;

	ATTACK_AMaster: GearPtr;
	ATTACK_TMaster,ATTACK_TPilot: GearPtr;

	ATTACK_TMasterOK: Boolean;		{ TRUE if master OK at start. }
	ATTACK_TPilotOK: Boolean;		{ TRUE if pilot OK at start. }
	ATTACK_TMasterMove: Boolean;		{ TRUE if master mobile at start. }

	FX_Messages: SAttPtr;

	Stencil: Array [ 1..XMax , 1..YMax ] of Boolean;


Procedure RecordAnnouncement( msg: String );
	{ Record an announcememnt in the history list. }
begin
	AddSAtt( ATTACK_History , 'ANNOUNCE_' + BStr( EFFECTS_Event_Order ) + '_' , msg );
end;

Procedure Add_Shot_Precisely( GB: GameBoardPtr; X0,Y0,Z0,X1,Y1,Z1: Integer );
	{ Add a shot animation to the history list. }
var
	msg: String;
begin
	msg := BStr( GS_Shot ) + ' ' + BStr( X0 ) + ' ' + BStr( Y0 ) + ' ' + BStr( Z0 );
	msg := msg + ' ' + BStr( X1 ) + ' ' + BStr( Y1 ) + ' ' + BStr( Z1 );

	AddSAtt( ATTACK_History , SAtt_Anim_Direction + BStr( EFFECTS_Event_Order ) + '_' , msg );
end;

Procedure Add_Shot_Animation( GB: GameBoardPtr; Attacker , Target: GearPtr );
	{ Add a shot animation to the history list. }
var
	P0,P1: Point;
begin
	Attacker := FindRoot( Attacker );
	Target := FindRoot( Target );

	P0 := GearCurrentLocation( Attacker );
	P0.Z := MekAltitude( GB , FindRoot( Attacker ) );

	P1 := GearCurrentLocation( Target );
	P1.Z := MekAltitude( GB , FindRoot( Target ) );

	Add_Shot_Precisely( GB , P0.X , P0.Y , P0.Z , P1.X , P1.Y , P1.Z );
end;

Procedure Add_Point_Animation( X,Y,Z: Integer; CMD: Integer );
	{ Add a shot animation to the history list. }
var
	msg: String;
begin
	msg := BStr( cmd ) + ' ' + BStr( X ) + ' ' + BStr( Y ) + ' ' + BStr( Z );

	AddSAtt( ATTACK_History , SAtt_Anim_Direction + BStr( EFFECTS_Event_Order ) + '_' , msg );
end;

Procedure Add_Mek_Animation( GB: GameBoardPtr; Target: GearPtr; CMD: Integer );
	{ Add a shot animation to the history list. }
var
	P: Point;
begin
	{ Find the location of the target, and just pass that on to }
	{ the point animation procedure above. }
	Target := FindRoot( Target );
	P := GearCurrentLocation( Target );
	Add_Point_Animation( P.X , P.Y , MekAltitude( GB , Target ) , CMD );
end;


Procedure ClearAttackHistory;
	{ Get rid of any history variables leftover from previous attacks. }
begin
	DisposeSAtt( ATTACK_History );
	EFFECTS_Event_Order := 0;
end;


Procedure INDICATE_Latest_Attack( GB: GameBoardPtr );
	{ Take all the stuff from the attack history values, and store it in the history }
	{ list. }
var
	msg: String;
	DP: SAttPtr;
begin
	{ Msg always starts with the target's name. }
	msg := PilotName( ATTACK_TMaster );

	{ If a part can't take damage, don't record a damage announcement. }
	if GearMaxDamage( ATTACK_TMAster ) = 0 then begin
		Exit;
	end else if ATTACK_Error then msg := msg + 'Error!'
	else if ATTACK_ItHit then begin
		if ATTACK_MOS > 3 then begin
			msg := msg + SAttValue( FX_Messages , 'CFE_CritHit' );

		end else begin
			msg := msg + SAttValue( FX_Messages , 'CFE_NormHit' );
		end;

		{ indicate number of hits, if appropriate. }
		if ATTACK_NumberOfHits > 1 then begin
			msg := msg + BStr( ATTACK_NumberOfHits ) + SAttValue( FX_Messages , 'CFE_MultiHit' );
		end;

		{ indicate damage done. }
		if ATTACK_DamageDone > 0 then begin
			msg := msg + SAttValue( FX_Messages , 'CFE_Damage1' ) + BStr( ATTACK_DamageDone ) + SAttValue( FX_Messages , 'CFE_Damage2' );

			msg := msg + '.';

			{ Add the list of destroyed parts now. }
			DP := Destroyed_Parts_List;
			while ( DP <> Nil ) and ( Length( msg ) < Damage_List_Text_Length ) do begin
				msg := msg + ' ' + DP^.Info + SAttValue( FX_Messages , 'CFE_Destroyed' );
				DP := DP^.Next;
			end;
		end else begin
			msg := msg + SAttValue( FX_Messages , 'CFE_NoDamage' );
		end;


	end else if ATTACK_Parry then begin
		if ( ATTACK_AttSkillRank + 1 + Random( 8 ) ) < ATTACK_DefSkillRank then begin
			msg := msg + SAttValue( FX_Messages , 'CFE_EasyParry' );

		end else if ( ATTACK_AttSkillRank - 1 - Random( 8 ) ) > ATTACK_DefSkillRank then begin
			msg := msg + SAttValue( FX_Messages , 'CFE_HardParry' );

		end else begin
			msg := msg + SAttValue( FX_Messages , 'CFE_Parry' );
		end;

	end else if ATTACK_Resist then begin
		if ( ATTACK_AttSkillRank + 1 + Random( 8 ) ) < ATTACK_DefSkillRank then begin
			msg := msg + SAttValue( FX_Messages , 'CFE_EasyResist' );

		end else if ( ATTACK_AttSkillRank - 1 - Random( 8 ) ) > ATTACK_DefSkillRank then begin
			msg := msg + SAttValue( FX_Messages , 'CFE_HardResist' );

		end else begin
			msg := msg + SAttValue( FX_Messages , 'CFE_Resist' );
		end;

	end else begin
		if ( ATTACK_AttSkillRank + 1 + Random( 8 ) ) < ATTACK_DefSkillRank then begin
			msg := msg + SAttValue( FX_Messages , 'CFE_EasyEvade' );

		end else if ( ATTACK_AttSkillRank - 1 - Random( 8 ) ) > ATTACK_DefSkillRank then begin
			msg := msg + SAttValue( FX_Messages , 'CFE_HardEvade' );

		end else begin
			msg := msg + SAttValue( FX_Messages , 'CFE_Evade' );
		end;
	end;

	if ATTACK_TMasterOK and Destroyed(ATTACK_TMaster) then msg := msg + ' ' + GearName( ATTACK_TMaster ) + ' is destroyed!';

	{ Gotta check that the attack hit, since if the DAMAGE procedure }
	{ wasn't called the history variables might be holding leftover }
	{ information. }
	if ATTACK_TPilotOK and ATTACK_ItHit then begin
		if DAMAGE_EjectRoll then begin
			if DAMAGE_PilotDied then msg := msg + ' ' + GearName( ATTACK_TPilot ) + ' didn''t eject in time!'
			else if DAMAGE_EjectOK then msg := msg + ' ' + GearName( ATTACK_TPilot ) + ' ejected!';
		end else if Destroyed(ATTACK_TPilot) then msg := msg + ' ' + GearName( ATTACK_TPilot ) + ' died!';
	end;

	RecordAnnouncement( msg );

	{ Add the correct animation. }
	if ATTACK_Parry or ATTACK_Resist then begin
		Add_Mek_Animation( GB , ATTACK_TMaster , GS_Parry );
	end else if ATTACK_ItHit then begin
		if ( ATTACK_DamageDone > 0 ) then begin
			Add_Mek_Animation( GB , ATTACK_TMaster , GS_DamagingHit );
		end else begin
			Add_Mek_Animation( GB , ATTACK_TMaster , GS_ArmorDefHit );
		end;
	end else begin
		Add_Mek_Animation( GB , ATTACK_TMaster , GS_Dodge );
	end;
	if ATTACK_AttackerDamage > 0 then begin
		Add_Mek_Animation( GB , ATTACK_AMaster , GS_Backlash );
	end;
end;

Procedure INDICATE_Attack_Effect( GB: GameBoardPtr; EffectDesc: String );
	{ Indicate effect stuff here. }
var
	msg: String;
	DP: SAttPtr;
begin
	{ Miscellaneous effect only reported if target suffered damage. }
	if GearMaxDamage( ATTACK_TMAster ) = 0 then begin
		Exit;
	end else if ATTACK_ItHit and ( ATTACK_DamageDone > 0 ) then begin
		{ Msg always starts with the target's name. }
		msg := ReplaceHash( EffectDesc , PilotName( ATTACK_TMaster ) );
		msg := ReplaceHash( msg , BStr( ATTACK_DamageDone ) );

		{ Add the list of destroyed parts now. }
		DP := Destroyed_Parts_List;
		while ( DP <> Nil ) and ( Length( msg ) < Damage_List_Text_Length ) do begin
			msg := msg + ' ' + DP^.Info + SAttValue( FX_Messages , 'CFE_Destroyed' );
			DP := DP^.Next;
		end;

		if ATTACK_TMasterOK and Destroyed(ATTACK_TMaster) then msg := msg + ' ' + GearName( ATTACK_TMaster ) + ' is destroyed!';

		if ATTACK_TPilotOK then begin
			if DAMAGE_EjectRoll then begin
				if DAMAGE_PilotDied then msg := msg + ' ' + GearName( ATTACK_TPilot ) + ' didn''t eject in time!'
				else if DAMAGE_EjectOK then msg := msg + ' ' + GearName( ATTACK_TPilot ) + ' ejected!';
			end else if Destroyed(ATTACK_TPilot) then msg := msg + ' ' + GearName( ATTACK_TPilot ) + ' died!';
		end;

		RecordAnnouncement( msg );

		Add_Mek_Animation( GB , ATTACK_TMaster , GS_DamagingHit );
	end;
	if ATTACK_AttackerDamage > 0 then begin
		Add_Mek_Animation( GB , ATTACK_AMaster , GS_Backlash );
	end;
end;

Function InGeneralInventory( Part: GearPtr ): Boolean;
	{ If in the general inventory, it may be thrown. }
begin
	InGeneralInventory := IsInvCom( Part ) and ( Part^.Parent = FindMaster( Part ) );
end;

Function MustBeThrown( GB: GameBoardPtr; Master, Weapon: GearPtr; TX,TY: Integer ): Boolean;
	{ Return TRUE if WEAPON must be thrown in order to hit TARGET. }
	{ Return FALSE if WEAPON could be used normally, i.e. not thrown. }
	{ A weapon will only be thrown if it could not be used against }
	{ the target otherwise- this is because throwing a weapon is a }
	{ pain in the arse. You've got to go pick it up afterwards. }
begin
	if Weapon^.G = GG_Ammo then begin
		{ If you're attacking with ammo, that better be a grenade. }
		MustBeThrown := True;
	end else if InGeneralInventory( Weapon ) then begin
		{ If this weapon is in the general inventory, it must }
		{ have been thrown. }
		MustBeThrown := True;
	end else begin
		{ If the attack range is greater than the regular weapon }
		{ range, then the weapon must have been thrown. }
		MustBeThrown := ( ThrowingRange( GB , Master , Weapon ) > 0 ) and OnTheMap( TX , TY ) and ( Range( Master , TX , TY ) > WeaponRange( GB , Weapon ) );
	end;
end;

Function ReadyToFire( GB: GameBoardPtr; User,Weapon: GearPtr ): Boolean;
	{ Return TRUE if the gear in question is ready to perform an attack, }
	{ or FALSE if it is currently unable to do so. }
	{ Check to make sure that... }
	{     1) ATTACKER is a functional part. }
	{     2) ATTACKER is a part that can be used in attack. }
	{     3) ATTACKER has sufficient ammunition to attack. }
	{     4) COMTIME is greater or equal to the RECHARGE time. }
	{     5) ATTACKER is mounted in a usable limb. }
	{     6) ATTACKER's MASTER is the same as ATTACKER's ROOT. }
var
	AttackOK: Boolean;
begin
	{ This first check will return false if Weapon is nil, so I won't check here. }
	{ Throwable weapons don't have to be in a good module- they can }
	{ be in the general inventory. }
	{ v0.909 - Added check for safety switch. }
	if ( Weapon <> Nil ) and ( User <> Nil ) and ( ThrowingRange( GB, USer, Weapon ) > 0 ) then begin
		AttackOK := PartActive( Weapon ) and ( NAttValue( Weapon^.NA , NAG_WeaponModifier , NAS_SafetySwitch ) = 0 );
	end else begin
		AttackOK := PartActive( Weapon ) and InGoodModule( Weapon ) and ( NAttValue( Weapon^.NA , NAG_WeaponModifier , NAS_SafetySwitch ) = 0 );
	end;

	if AttackOK then begin
		{ Applicability Check }
		if ( Weapon^.G = GG_Weapon ) then begin
			{ Normally, all weapons may be used to attack. Duh. }
			{ However, ballistic and missile weapons can't attack }
			{ if they have no ammo left. }
			if ( Weapon^.S = GS_Ballistic ) or ( Weapon^.S = GS_Missile ) then begin
				if LocateGoodAmmo( Weapon ) = Nil then AttackOK := False;
			end;

		end else if Weapon^.G = GG_Module then begin
			{ Only Arms, Legs, and Tails may be used. }
			if ( Weapon^.S <> GS_Arm ) and ( Weapon^.S <> GS_Leg ) and ( Weapon^.S <> GS_Tail ) then begin
				AttackOK := False;
			end;

		end else if Weapon^.G = GG_Ammo then begin
			if Weapon^.S <> GS_Grenade then AttackOK := False;
			if not InGeneralInventory( Weapon ) then AttackOK := False;

		end else begin
			{ No other parts may be used to attack. So, }
			{ set AttackOK to False. }
			AttackOK := False;

		end;

		{ ComTime Check }
		if NAttValue( Weapon^.NA , NAG_WeaponModifier , NAS_Recharge ) > GB^.ComTime then AttackOK := False;

		{ If yer piloting a mecha can't punch the other guy yourself Check }
		if FindMaster( Weapon ) <> FindRoot( Weapon ) then AttackOK := False;
	end;

	ReadyToFire := AttackOK;
end;

Function ArcCheck( X0,Y0,D0,X1,Y1,A: Integer ): Boolean;
	{ CHeck that point X1,Y1 falls within Arc A as relative to }
	{ point X0,Y0 with direction D0. }
var
	OK: Boolean;
begin
	if A = ARC_360 then OK := True
	else if A = ARC_F90 then begin
		if CheckArc( X0 , Y0 , X1 , Y1 , D0 ) or CheckArc( X0 , Y0 , X1 , Y1 , ( D0 + 7 ) mod 8 ) then OK := True
		else OK := False;
	end else begin
		{ ASSERT -> A is ARC_F180 }
		if CheckArc( X0 , Y0 , X1 , Y1 , D0 ) or CheckArc( X0 , Y0 , X1 , Y1 , ( D0 + 7 ) mod 8 ) or CheckArc( X0 , Y0 , X1 , Y1 , ( D0 + 1 ) mod 8 ) or CheckArc( X0 , Y0 , X1 , Y1 , ( D0 + 6 ) mod 8 ) then OK := True
		else OK := False;
	end;

	ArcCheck := OK;
end;

Function ArcCheck( GB: GameBoardPtr; Master , Weapon: GearPtr; X,Y: Integer ): Boolean;
	{ Return TRUE if the target is in an appropriate fire arc for }
	{ WEAPON, or FALSE otherwise. }
var
	X0 , Y0 , D: Integer;	{ Position of the firer. }
	A: Integer;	{ Range and Arc of the attack. }
begin
	if ( Master = Nil ) or ( Weapon = Nil ) then Exit( False );

	if Master^.Parent <> Nil then Master := FindRoot( Master );

	X0 := NAttValue( Master^.NA , NAG_Location , NAS_X );
	Y0 := NAttValue( Master^.NA , NAG_Location , NAS_Y );

	D := NAttValue( Master^.NA , NAG_Location , NAS_D );
	A := WeaponArc( Weapon );

	{ Now check Range and Arc. }
	ArcCheck := ArcCheck( X0 , Y0 , D , X , Y , A );
end;

Function ArcCheck( GB: GameBoardPtr; Master , Weapon , Target: GearPtr ): Boolean;
	{ Return TRUE if the target is in an appropriate fire arc for }
	{ WEAPON, or FALSE otherwise. }
var
	X0 , Y0 , D , X , Y: Integer;	{ Position of the firer. }
	A: Integer;	{ Range and Arc of the attack. }
begin
	if ( Target = Nil ) or ( Master = Nil ) or ( Weapon = Nil ) then Exit( False );

	if Target^.Parent <> Nil then Target := FindRoot( Target );
	if Master^.Parent <> Nil then Master := FindRoot( Master );

	X := NAttValue( Target^.NA , NAG_Location , NAS_X );
	Y := NAttValue( Target^.NA , NAG_Location , NAS_Y );
	X0 := NAttValue( Master^.NA , NAG_Location , NAS_X );
	Y0 := NAttValue( Master^.NA , NAG_Location , NAS_Y );

	D := NAttValue( Master^.NA , NAG_Location , NAS_D );
	A := WeaponArc( Weapon );

	{ Now check Range and Arc. }
	ArcCheck := ArcCheck( X0 , Y0 , D , X , Y , A );
end;

Function RangeArcCheck( GB: GameBoardPtr; Master , Weapon: GearPtr; X,Y,Z: Integer ): Boolean;
	{ Check the range, arc, and cover between the listed gear and the listed tile. }
	{ Returns true if the shot can take place, false otherwise. }
var
	X0,Y0,D,A: Integer;
	rng: Integer;	{ Range and Arc of the attack. }
	OK: Boolean;
begin
	{ Calculate Range and Arc. }
	rng := WeaponRange( GB , Weapon );

	X0 := NAttValue( Master^.NA , NAG_Location , NAS_X );
	Y0 := NAttValue( Master^.NA , NAG_Location , NAS_Y );
	D := NAttValue( Master^.NA , NAG_Location , NAS_D );
	A := WeaponArc( Weapon );

	OK := ArcCheck( X0 , Y0 , D , X , Y , A );

	{ If out of range, no shot is possible. }
	if OK and ( Range( Master , X , Y ) > rng ) then begin
		{ OK is false, unless the target is within throwing range. }
		OK := ThrowingRange( GB , Master , Weapon ) >= Range( Master , X , Y );
	end;

	{ If Line of Sight is blocked, no shot is possible. }
	if OK and ( CalcObscurement( X0 , Y0 , MekALtitude( GB , Master ) , X , Y , Z , gb ) = -1 ) then OK := False;

	RangeArcCheck := OK;
end;

Function RangeArcCheck( GB: GameBoardPtr; Master , Weapon , Target: GearPtr ): Boolean;
	{ Check the range, arc, and cover between the listed gear and the listed tile. }
	{ Returns true if the shot can take place, false otherwise. }
var
	X , Y: Integer;	{ Position of the firer. }
begin
	{ Determine initial values for all the stuff. }
	if Target = Nil then Exit( False );
	if Target^.Parent <> Nil then Target := FindRoot( Target );
	X := NAttValue( Target^.NA , NAG_Location , NAS_X );
	Y := NAttValue( Target^.NA , NAG_Location , NAS_Y );

	RangeArcCheck := RangeArcCheck( GB , Master , Weapon , X , Y , MekALtitude( GB , Target ) );
end;

Function RechargeTime( Attacker: GearPtr; AtOp: Integer ): Integer;
	{ Return the modified recharge time for this weapon. }
var
	WAO: GearPtr;
	it: Integer;
begin
	if Attacker^.G = GG_Weapon then begin
		it := Attacker^.Stat[STAT_Recharge];
	end else begin
		it := 2;
	end;

	{ Modify for weapon token. }
	WAO := Attacker^.InvCom;
	while WAO <> Nil do begin
		if ( WAO^.G = GG_WeaponAddOn ) and NotDestroyed( WAO ) then begin
			it := it + WAO^.Stat[ STAT_Recharge ];
		end;
		WAO := WAO^.Next;
	end;

	if ( Attacker^.G = GG_Weapon ) and ( Attacker^.S = GS_BeamGun ) and ( AtOp > 0 ) then begin
		{ Beamguns which use rapid fire take MUCH longer to recharge than normal. }
		RechargeTime := 2 * ClicksPerRound div it;
	end else begin
		RechargeTime := ClicksPerRound div it;
	end;
end;

Function ClearAttack( GB: GameBoardPtr; Attacker: GearPtr ; var AtOp: Integer ): Boolean;
	{ This function sets up the weapon for performing an attack. }
	{ It reduces ammo count by an appropriate amount. }
	{ It sets the RECHARGE attribute. }
	{ Return TRUE if everything is okay, FALSE if there's some reason }
	{ why this attack can't take place. }
	{ Note that this function does not do a range check. }
var
	AttackOK: Boolean;
	Ammo: GearPtr;
begin
	{ First, make sure that this attack can even take place. }
	{ Check to make sure that ATTACKER is active. }
	AttackOK := ReadyToFire( GB , FindRoot( Attacker ) , Attacker );

	if AttackOK and ( Attacker^.G = GG_Weapon ) then begin

		{ Do an ammunition check for projectile weapons and missiles. }
		if ( Attacker^.S = GS_Missile ) or (( Attacker^.S = GS_Ballistic ) and ( Attacker^.Stat[STAT_Magazine] > 0 )) then begin
			{ Locate the ammo to be used. }
			Ammo := LocateGoodAmmo( Attacker );

			if Ammo <> Nil then begin
				{ Reduce the ammo count by an appropriate amount. }

				{ AtOp is the number of missiles being fired. }
				{ If this goes over the number of missiles present, correct that problem. }
				if ( AtOp > 0 ) then begin
					if ( Ammo^.Stat[STAT_AmmoPresent] - NAttValue( Ammo^.NA , NAG_WeaponModifier , NAS_AmmoSpent ) ) < (AtOp + 1) then begin
						AtOp := ( Ammo^.Stat[STAT_AmmoPresent] - NAttValue( Ammo^.NA , NAG_WeaponModifier , NAS_AmmoSpent ) ) - 1;
					end;
				end;

				{ Do the actual ammo count thing here. }
				AddNAtt( Ammo^.NA , NAG_WeaponModifier , NAS_AmmoSpent , AtOp + 1 );

			end else begin
				{ This weapon has no ammo. The attack cannot proceed. }
				AttackOK := False;

			end;
		end;

	end else if Attacker^.G = GG_Ammo then begin
		{ Grenades don't get a choice what AtOp they use. }
		AtOp := Attacker^.Stat[ STAT_BurstValue ];
	end;

	{ Set the recharge time now. }
	if AttackOK then begin
		SetNAtt( Attacker^.NA , NAG_WeaponModifier , NAS_Recharge , GB^.ComTime + RechargeTime( Attacker , AtOp ) );
	end;

	ClearAttack := AttackOK;
end;

Function AttackSkillNeeded( Attacker: GearPtr ): Integer;
	{ Return the index number of the skill used to attack with this }
	{ particular weapon. }
var
	ASkill: Integer;
	AMaster: GearPtr;
begin
	{ The skills for human-scale and mecha-scale are set up in }
	{ the same order, with the mecha skills being 1 to 5 and the }
	{ personal skills being 6 to 10. So, just find the skill number }
	{ based on ATTACKER's type, then add +5 if the master is a }
	{ character instead of a mecha. }


	if Attacker^.G = GG_Weapon then begin
		if ( Attacker^.S = GS_Melee ) or ( Attacker^.S = GS_EMelee ) then begin
			{ Use armed combat/weapons skill. }
			ASkill := 3;
		end else if ( Attacker^.S = GS_Ballistic ) or ( Attacker^.S = GS_BeamGun ) then begin
			{ Use gunnery/small arms if DC is 10 or less, }
			{ use artillery/heavy if DC is 11 or greater. }
			if Attacker^.V <= 10 then begin
				ASkill := 1;
			end else begin
				ASkill := 2;
			end;
		end else begin
			{ Must be a missile launcher- use heavy/artillery skill. }
			ASkill := 2;
		end;

	end else if Attacker^.G = GG_Ammo then begin
		{ Grenades use Heavy Weapons/Artillery skill by }
		{ default, but may redefine this freely. I did so }
		{ to allow the creation of shurikens, cans of mace, }
		{ and other derivitaves from the grenade code. }
		ASkill := Attacker^.Stat[ STAT_GrenadeSkill ];

	end else begin
		{ Not a weapon- use Fighting/Martial Arts. }
		ASkill := 4;
	end;

	{ If the master is a character, add +5 to the skill index. }
	AMaster := FindMaster( Attacker );
	if ( AMaster <> Nil ) and ( AMaster^.G = GG_Character ) then begin
		ASkill := ASkill + 5;
	end;

	{ Return the value we found. }
	AttackSkillNeeded := ASkill;
end;

Function AttemptShieldBlock(GB: GameBoardPtr; TMaster , Attacker: GearPtr; SkRoll: Integer ): Integer;
	{ Attempt to block an attack using a shield. Return the defense }
	{ roll result, or 0 if no shield could be found. }
var
	DefGear: GearPtr;
	DefSkill,DefRoll: Integer;

	Procedure SeekShield( Part: GearPtr );
		{ Seek a shield which is capable of parrying an attack. }
	begin
		while ( Part <> Nil ) do begin
			if NotDestroyed( Part ) then begin
				if ( Part^.G = GG_Shield ) and InGoodModule( Part ) then begin
					if ( NAttValue( Part^.NA , NAG_WeaponModifier , NAS_Recharge ) <= GB^.ComTime ) and ( ( Attacker = Nil ) or ( Part^.Scale >= Attacker^.Scale ) ) then begin
						if DefGear = Nil then DefGear := Part;
					end;
				end;
				if ( Part^.SubCom <> Nil ) then SeekShield( Part^.SubCom );
				if ( Part^.InvCom <> Nil ) then SeekShield( Part^.InvCom );
			end;
			Part := Part^.Next;
		end;
	end;
begin
	{ Try to find a shield. }
	DefGear := Nil;
	DefRoll := 0;
	SeekShield( TMaster^.SubCom );

	{ If a shield is found, proceed with the defense roll... }
	if DefGear <> Nil then begin
		{ Find the appropriate skill value. }
		if TMaster^.G = GG_Mecha then begin
			{ For mecha, this will be Mecha Fighting }
			DefSkill := 4;
		end else begin
			{ For characters, this will be Armed Combat }
			DefSkill := 8;
		end;

		ATTACK_DefSkillRank := SkillValue( TMaster , DefSkill );

		{ Set the recharge time for the shield. }
		SetNAtt( DefGear^.NA , NAG_WeaponModifier , NAS_Recharge , GB^.ComTime + ( ClicksPerRound div 3 ) );

		{ Give some skill-specific experience points. }
		DoleSkillExperience( TMaster , DefSkill , XPA_SK_Basic );

		{ Make the skill roll + Shield Bonus }
		DefRoll := RollStep( ATTACK_DefSkillRank ) + DefGear^.Stat[ STAT_ShieldBonus ];

		{ If the parry was successful, there will be some after-effects. }
		if DefRoll >= SkRoll then begin
			{ The shield is going to take damage from the hit, whether it was an }
			{ energy shield or a beam shield- but beam shields only take damage }
			{ from energy weapons. }
			if ATtacker <> Nil then begin
				if DefGear^.S = GS_EnergyShield then begin
					if CanDamageBeamShield( Attacker ) then begin
						DamageGear( GB , DefGear , Attacker , Attacker^.V , 0 , 1 , '' );
					end;
				end else begin
					{ Physical shields take damage from everything. }
					DamageGear( GB , DefGear , Attacker , Attacker^.V , 0 , 1 , '' );
				end;

				{ An energy shield will do damage back to any CC weapon that hits it. }
				if ( DefGear^.S = GS_EnergyShield ) then begin
					if ( Attacker^.G = GG_Module ) or ( Attacker^.S = GS_Melee ) then begin
						ATTACK_AttackerDamage := ATTACK_AttackerDamage + DamageGear( GB , Attacker , DefGear , DefGear^.V , 0 , 1 , '' );
					end;
				end;
			end;

		end;
	end;

	AttemptShieldBlock := DefRoll;
end;

Function AttemptParry(GB: GameBoardPtr; TMaster , Attacker: GearPtr; SkRoll: Integer ): Integer;
	{ Try to parry this attack, if it is in fact parryable. }
var
	DefGear: GearPtr;
	DefSkill,DefRoll: Integer;
	Procedure SeekParryWeapon( Part: GearPtr );
		{ Seek a weapon which is capable of parrying an attack. }
	begin
		while ( Part <> Nil ) do begin
			if ( Part^.G = GG_Weapon ) and (( Part^.S = GS_Melee ) or ( Part^.S = GS_EMelee )) then begin
				if ReadyToFire( GB , Nil , Part ) and InGoodModule( Part ) and ( ( Attacker = Nil ) or ( Part^.Scale >= Attacker^.Scale ) ) then begin
					if DefGear = Nil then DefGear := Part
					else if Part^.Stat[STAT_Accuracy] > DefGear^.Stat[STAT_Accuracy] then DefGear := Part;
				end;
			end;
			if ( Part^.SubCom <> Nil ) then SeekParryWeapon( Part^.SubCom );
			if ( Part^.InvCom <> Nil ) then SeekParryWeapon( Part^.InvCom );
			Part := Part^.Next;
		end;
	end;

begin
	DefRoll := 0;

	{ Search for a usable CC weapon. }
	DefGear := Nil;
	SeekParryWeapon( TMaster^.SubCom );

	{ If one was found, do the parry attempt. }
	if DefGear <> Nil then begin
		{ Make an attack roll to parry. }
		DefSkill := AttackSkillNeeded( DefGear );

		ATTACK_DefSkillRank := SkillValue( TMaster , DefSkill );

		DefRoll := RollStep( ATTACK_DefSkillRank + Parry_Bonus ) + DefGear^.Stat[ STAT_Accuracy ];

		{ Give some skill-specific experience points. }
		DoleSkillExperience( TMaster , DefSkill , XPA_SK_Basic );

		{ If the parry was successful, there will be some after-effects. }
		if DefRoll >= SkRoll then begin
			{ After a succeful parry, weapon is "tapped". }
			DefSkill := 0;
			ClearAttack( GB , DefGear , DefSkill );

			{ If the parrying weapon is not an energy weapon, }
			{ it will take damage from the parrying attempt. }
			if Attacker <> Nil then begin
				if ( DefGear^.S <> GS_EMelee ) then begin
					if ( Attacker^.G = GG_Weapon ) and ( Attacker^.S = GS_EMelee ) then begin
						DamageGear( GB , DefGear , Attacker , Attacker^.V , 0 , 1 , '' );
					end else begin
						DamageGear( GB , DefGear , Attacker , 1 , 0 , 1 , '' );
					end;

				{ If the parrying weapon is an energy weapon, then }
				{ the attacker's weapon is going to take damage unless }
				{ it too is an energy weapon. }
				end else if ( Attacker^.G <> GG_Weapon ) or ( Attacker^.S <> GS_Emelee ) then begin
					ATTACK_AttackerDamage := ATTACK_AttackerDamage + DamageGear( GB , Attacker , DefGear , DefGear^.V , 0 , 1 , '' );
				end;
			end;
		end;
	end;

	{ Return the resultant defense roll. }
	AttemptParry := DefRoll;
end;

Function AttemptIntercept(GB: GameBoardPtr; TMaster , Attacker: GearPtr; SkRoll: Integer ): Integer;
	{ Try to intercept this attack. }
var
	DefGear: GearPtr;
	DefSkill,DefRoll: Integer;
	Procedure SeekInterceptWeapon( Part: GearPtr );
		{ Seek a weapon which is capable of intercepting an attack. }
	begin
		while ( Part <> Nil ) do begin
			if ( Part^.G = GG_Weapon ) and HasAttackAttribute( WeaponAttackAttributes( Part ) , AA_Intercept ) then begin
				if ReadyToFire( GB , Nil , Part ) and InGoodModule( Part ) and ( ( Attacker = Nil ) or ( Part^.Scale >= Attacker^.Scale ) ) then begin
					if DefGear = Nil then DefGear := Part
					else if Part^.Stat[STAT_Accuracy] > DefGear^.Stat[STAT_Accuracy] then DefGear := Part;
				end;
			end;
			if ( Part^.SubCom <> Nil ) then SeekInterceptWeapon( Part^.SubCom );
			if ( Part^.InvCom <> Nil ) then SeekInterceptWeapon( Part^.InvCom );
			Part := Part^.Next;
		end;
	end;

begin
	DefRoll := 0;

	{ Search for a usable CC weapon. }
	DefGear := Nil;
	SeekInterceptWeapon( TMaster^.SubCom );

	{ If one was found, do the parry attempt. }
	if DefGear <> Nil then begin
		{ Make an attack roll to parry. }
		DefSkill := AttackSkillNeeded( DefGear );

		ATTACK_DefSkillRank := SkillValue( TMaster , DefSkill );

		DefRoll := RollStep( ATTACK_DefSkillRank + DefGear^.V ) + DefGear^.Stat[ STAT_Accuracy ] + DefGear^.Stat[ STAT_BurstValue ];

		{ Give some skill-specific experience points. }
		DoleSkillExperience( TMaster , DefSkill , XPA_SK_Basic );

		{ If the parry was successful, there will be some after-effects. }
		if DefRoll >= SkRoll then begin
			{ After a succeful parry, weapon is "tapped". }
			ClearAttack( GB , DefGear , DefSkill );
		end;
	end;

	{ Return the resultant defense roll. }
	AttemptIntercept := DefRoll;
end;

Function AttemptEWBlock(GB: GameBoardPtr; TMaster , Attacker: GearPtr; SkRoll: Integer ): Integer;
	{ Try to stop this attack using Electronic Counter-Measures. }
var
	DefGear: GearPtr;
	DefRoll: Integer;
begin
	DefRoll := 0;

	{ Search for a usable CC weapon. }
	DefGear := SeekActiveIntrinsic( TMaster , GG_Sensor , GS_ECM );

	{ If one was found, do the parry attempt. }
	if DefGear <> Nil then begin
		{ Make an attack roll to block. }
		ATTACK_DefSkillRank := SkillValue( TMaster , 17 );

		DefRoll := RollStep( ATTACK_DefSkillRank + DefGear^.V - 5 );

		{ Give some skill-specific experience points. }
		DoleSkillExperience( TMaster , 17 , XPA_SK_Basic );
	end;

	{ Return the resultant defense roll. }
	AttemptEWBlock := DefRoll;
end;

Function AttemptResist(GB: GameBoardPtr; TMaster: GearPtr ): Integer;
	{ Attempt to resist damage using either the RESISTANCE or }
	{ ELECTRONIC WARFARE skills, depending upon whether the target }
	{ is a character or a mecha. }
begin
	if TMaster^.G = GG_MEcha then begin
		{ Mecha use ELECTRONIC WARFARE. }
		ATTACK_DefSkillRank := SkillValue( TMaster , 17 );
		DoleSkillExperience( TMaster , 17 , XPA_SK_Basic );
	end else begin
		{ Characters use RESISTANCE. }
		ATTACK_DefSkillRank := SkillValue( TMaster , 36 );
		DoleSkillExperience( TMaster , 36 , XPA_SK_Basic );
	end;

	{ Return the resultant defense roll. }
	AttemptResist := RollStep( ATTACK_DefSkillRank );
end;

Function AttemptDefenses( GB: GameBoardPtr; TMaster,Attacker: GearPtr; SkRoll: Integer; AF: AttackFlags ): Integer;
	{ The target has just been attacked. Roll any appropriate }
	{ defenses. Return the highest defense roll. }
var
	DefRoll,HiDefRoll: Integer;
begin
	{ First, check to see if this attack will be ineffective. }
	if HasAttackAttribute( AF.AtAt , AA_NoMetal ) and ( NAttValue( TMaster^.NA , NAG_GearOps , NAS_Material ) = NAV_Metal ) then begin
		ATTACK_Dodge := True;
		Exit( 256 );
	end;

	{ All attacks get a dodge attempt. }
	{ Make the dodge roll, then dole appropriate experience. }
	HiDefRoll := 0;
	ATTACK_Dodge := False;
	ATTACK_Parry := False;
	ATTACK_Resist := False;
	if AF.CanDodge then begin
		if TMaster^.G = GG_MEcha then begin
			{ Mecha use Mecha Piloting. }
			ATTACK_DefSkillRank := SkillValue( TMaster , 5 );
			DoleSkillExperience( TMaster , 5 , XPA_SK_Basic );

			{ Adjust the dodge skill value for talents. }
			if ( NAttValue( TMaster^.NA , NAG_Action , NAS_MoveMode ) = MM_Walk ) and HasTalent( TMaster , NAS_SureFooted ) then begin
				ATTACK_DefSkillRank := ATTACK_DefSkillRank + 2;
			end else if ( NAttValue( TMaster^.NA , NAG_Action , NAS_MoveMode ) = MM_Fly ) and HasTalent( TMaster , NAS_BornToFly ) then begin
				ATTACK_DefSkillRank := ATTACK_DefSkillRank + 3;
			end else if ( NAttValue( TMaster^.NA , NAG_Action , NAS_MoveMode ) = MM_Roll ) and HasTalent( TMaster , NAS_RoadHog ) then begin
				ATTACK_DefSkillRank := ATTACK_DefSkillRank + 2;
			end;
		end else begin
			{ Characters use Dodge. }
			ATTACK_DefSkillRank := SkillValue( TMaster , 10 );
			DoleSkillExperience( TMaster , 10 , XPA_SK_Basic );
		end;
		HiDefRoll := RollStep( ATTACK_DefSkillRank );
		ATTACK_Dodge := HiDefRoll >= SkRoll;
	end;

	{ Attempt ECM defense. }
	if AF.CanECM and ( HiDefRoll < SkRoll ) then begin
		DefRoll := AttemptEWBlock( GB , TMaster , ATtacker , SkRoll );
		if DefRoll > HiDefRoll then begin
			HiDefRoll := DefRoll;
			ATTACK_Dodge := HiDefRoll >= SkRoll;
		end;
	end;

	{ Attempt physical shield parry, if charged. }
	if AF.CanBlock and ( HiDefRoll < SkRoll ) then begin
		DefRoll := AttemptShieldBlock( GB , TMaster , ATtacker , SkRoll );
		if DefRoll > HiDefRoll then begin
			HiDefRoll := DefRoll;
			ATTACK_Parry := HiDefRoll >= SkRoll;
		end;
	end;

	{ Attempt anti-missile intercept. }
	if AF.CanIntercept and ( HiDefRoll < SkRoll ) then begin
		DefRoll := AttemptIntercept( GB , TMaster , ATtacker , SkRoll );
		if DefRoll > HiDefRoll then begin
			HiDefRoll := DefRoll;
			ATTACK_Parry := HiDefRoll >= SkRoll;
		end;
	end;

	{ If a close combat attack, attempt a parry with any active }
	{ CC weapon. }
	if AF.CanParry and ( HiDefRoll < SkRoll ) then begin
		DefRoll := AttemptParry( GB , TMaster , ATtacker , SkRoll );
		if DefRoll > HiDefRoll then begin
			HiDefRoll := DefRoll;
			ATTACK_Parry := HiDefRoll >= SkRoll;
		end;
	end;

	{ If resistable, try to resist. }
	if AF.CanResist and ( HiDefRoll < SkRoll ) then begin
		DefRoll := AttemptResist( GB , TMaster );
		if DefRoll > HiDefRoll then begin
			HiDefRoll := DefRoll;
			ATTACK_Resist := HiDefRoll >= SkRoll;
		end;
	end;

	{ Attempt HapKiDo block. }
	{ Can only do this if it's a character being attacked, the }
	{ talent is know, the character isn't tired... }
	if AF.CanBlock and ( HiDefRoll < SkRoll ) and ( TMaster^.G = GG_CHaracter ) and HasTalent( TMaster , NAS_HapKiDo ) and ( CharCurrentStamina( TMaster ) > 0 ) then begin
		ATTACK_DefSkillRank := SkillValue( TMaster , 9 );
		DefRoll := RollStep( ATTACK_DefSkillRank );
		AddStaminaDown( TMaster , 1 );
		if DefRoll > HiDefRoll then begin
			HiDefRoll := DefRoll;
			ATTACK_Parry := HiDefRoll >= SkRoll;
		end;
	end;

	{ Attempt Stunt Driving dodge. }
	{ Can only do this if it's a mecha being attacked, the }
	{ talent is know, and they're moving at full speed, and the }
	{ pilot has stamina points left... }
	if AF.CanDodge and ( HiDefRoll < SkRoll ) and ( TMaster^.G = GG_Mecha ) and HasTalent( TMaster , NAS_StuntDriving ) and ( NAttValue( TMaster^.NA , NAG_Action , NAS_MoveAction ) = NAV_FullSpeed ) and ( CurrentStamina( TMaster ) > 0 ) then begin
		ATTACK_DefSkillRank := SkillValue( TMaster , 5 );
		DefRoll := RollStep( ATTACK_DefSkillRank );
		AddStaminaDown( TMaster , 1 );
		if DefRoll > HiDefRoll then begin
			HiDefRoll := DefRoll;
			ATTACK_Dodge := HiDefRoll >= SkRoll;
		end;
	end;


	{ If defense was successful, may drain a point of stamina. }
	{ If the defense wasn't successful, no point adding insult }
	{ to injury. }
	if ( HiDefRoll > SkRoll ) and ( Random( 3 ) = 1 ) then begin
		AddStaminaDown( TMaster , 1 );
	end;

	{ Return the highest rolled value. The ATTACK_HiDefRoll variable is set }
	{ in the calling procedure. }
	AttemptDefenses := HiDefRoll;
end;


Function CalcTotalModifiers( gb: GameBoardPtr; Attacker,Target: GearPtr; AtOp: Integer; AtAt: String ): Integer;
	{ Calculate the total modifiers to this attack roll. }
var
	SkRoll,Spd,ZA,ZT: Integer;
	AMaster,TMaster,AModule,AShield: GearPtr;
begin
	SkRoll := 0;
	AMaster := FindRoot( Attacker );
	TMaster := FindRoot( Target );

	{ Add the weapon accuracy, and possibly Attack Options. }
	if Attacker^.G = GG_Weapon then begin
		SkRoll := SkRoll + Attacker^.Stat[STAT_Accuracy];

		{ Add a modifier for any weapon add-ons that might be attached. }
		{ I'll use the AShield var for this instead of declaring a new variable... }
		AShield := Attacker^.InvCom;
		while AShield <> Nil do begin
			if ( AShield^.G = GG_WeaponAddOn ) and NotDestroyed( AShield ) then begin
				SkRoll := SkRoll + AShield^.Stat[STAT_Accuracy];
			end;
			AShield := AShield^.Next;
		end;

		{ Missiles use sensor rating instead of targeting rating. }
		if ( Attacker^.S = GS_Missile ) and ( AMaster^.G = GG_Mecha ) then begin
			SkRoll := SkRoll - MechaTargeting( AMaster ) + MechaSensorRating( AMaster );
		end;

		if ( Attacker^.S = GS_Ballistic ) or ( Attacker^.S = GS_BeamGun ) or ( Attacker^.S = GS_Missile ) then begin
			if AtOp > 0 then begin
				if AtOp < 10 then SkRoll := SkRoll + ( AtOp div 2 )
				else SkRoll := SkRoll + 5;
			end;
		end;

	end else begin
		{ Modules and other non-weapon attacking parts suffer }
		{ a -2 to their hot rolls. }
		SkRoll := SkRoll - 2;
	end;

	{ Modify the attack roll for wielded shields. }
	AModule := FindModule( Attacker );
	if AModule <> Nil then begin
		AShield := SeekGearByG( AModule^.InvCom , GG_Shield );
		if ( AShield <> Nil ) and ( AShield <> Attacker^.Parent ) then begin
			SkRoll := SkRoll - 5 - AShield^.Stat[ STAT_ShieldBonus ];
		end;
	end;

	{ Modify the attack score for scale and target depth. }
	{ Depth refers to the subcomponent level that TARGET is at... }
	if not HasAttackAttribute( AtAt , AA_BlastAttack ) then begin
		if Attacker^.Scale <> Target^.Scale then begin
			SkRoll := SkRoll - ( Attacker^.Scale - Target^.Scale ) * PenaltyPerScale;
		end;
		SkRoll := SkRoll - GearDepth( Target ) * PenaltyPerDepth;
	end;

	{ Modify the attack roll for target and attacker movement. }
	{ The modifier from the attacker is based on MoveAction, }
	{ while the modifier for the defender is based upon actual speed. }
	Spd := NAttValue( AMaster^.NA , NAG_Action , NAS_MoveAction );
	if Spd = NAV_Stop then SkRoll := SkRoll + StopBonus
	else if Spd = NAV_FullSpeed then SkRoll := SkRoll - RunPenalty;

	Spd := CalcRelativeSpeed( TMaster , GB );
	if Spd > 0 then begin
		{ Convert the speed from ClicksPerHex to HexPerRound }
		Spd := ( ClicksPerRound * 10 ) div Spd;
		SkRoll := SkRoll - ( Spd div ClicksPerPenalty );
	end else begin
		if BaseMoveRate( TMaster ) > 0 then SkRoll := SkRoll + StopPenalty
		else SkRoll := SkRoll + ImmobilePenalty;
	end;

	{ Modify for attack attributes. }
	if HasAttackAttribute( AtAt , AA_STRAIN ) and ( AMaster <> Nil ) and ( CurrentStamina( AMaster ) < 1 ) then SkRoll := SkRoll - 10;
	if HasAttackAttribute( AtAt , AA_COMPLEX ) and ( AMaster <> Nil ) and ( CurrentMental( AMaster ) < 1 ) then SkRoll := SkRoll - 10;

	{ Do the modifiers that only count if both meks are on the game board. }
	if OnTheMap( AMaster ) and OnTheMap( TMaster ) then begin
		if not HasAttackAttribute( AtAt , AA_BlastAttack ) then begin
			{ Adjust the attack roll for obscurement between attacker & target. }
			{ Yeah, I'm reusing the SPD variable for cover. Big deal. }
			Spd := CalcObscurement( AMaster , TMaster , GB );
			if Spd > 0 then begin
				SkRoll := SkRoll - Spd;
			end;
		end;

		{ If the firer is underwater, this will be a more difficult shot. }
		if MekAltitude( gb , AMaster ) < 0 then SkRoll := SkRoll - UnderwaterAttackPenalty;

		{ Add range modifier. }
		{ Still using the SPD variable for all these other uses... }
		SPD := Range( gb , AMaster , TMaster );
		if ( SPD > 1 ) and ( Attacker^.G = GG_Weapon ) and ( Attacker^.Stat[ STAT_Range ] <> 0 ) then begin
			{ Apply penalty for within minumum range }
			if ( Attacker^.Stat[STAT_Range] > HAS_MINIMUM_RANGE ) and ( Spd <= ( Attacker^.Stat[STAT_Range] div 2 ) ) then begin
				{ For every square inside minimum range, there's a -1 attack penalty. Sound familiar? }
				SkRoll := SkRoll - ( Attacker^.Stat[STAT_Range] div 2 ) + Spd - 1;

			end else if SPD <= Attacker^.Stat[STAT_Range] then SkRoll := SkRoll + ShortRangeBonus
			else if SPD > Attacker^.Stat[STAT_Range] * 2 then SkRoll := SkRoll - LongRangePenalty;
		end;

		{ Add altitude modifier. Attacking an airborne mecha is more difficult, }
		{ unless the ANTIAIR attribute is had. If the attacker is higher than the }
		{ defender there's a slight bonus there as well. }
		ZA := MekAltitude( GB , AMaster );
		ZT := MekAltitude( GB , TMaster );
		if ( ZT = 5 ) and ( ZA <> 5 ) and not HasAttackAttribute( AtAt , AA_AntiAir ) then SkRoll := SkRoll - FlyingPenalty;
		if ZA > ZT then SkRoll := SkRoll + HighGroundBonus;
	end;

	CalcTotalModifiers := SkRoll;
end;

Procedure DestroyTerrain( GB: GameBoardPtr; X,Y: Integer );
	{ Destroy the terrain in this spot. Pretty simple actually. }
var
	Smoke: GearPtr;
begin
	{ Start with an error check... }
	if not OnTheMap( X , Y ) then Exit;

	{ If this terrain has a DESTROYED type set, change the tile. }
	if TerrMan[ GB^.map[X,Y].terr ].Destroyed <> 0 then GB^.map[X,Y].terr := TerrMan[ GB^.map[X,Y].terr ].Destroyed;

	{ If terrain is destroyed, it will probably cause smoke and maybe fire. }
	if Random( 2 ) = 1 then begin
		Smoke := LoadNewSTC( 'SMOKE-1' );
		if Smoke <> Nil then begin
			Smoke^.Scale := GB^.Scale;
			AppendGear( GB^.Meks , Smoke );
			Smoke^.Stat[ STAT_CloudDuration ] := RollStep( 5 );
			SetNAtt( Smoke^.NA , NAG_Location , NAS_X , X );
			SetNAtt( Smoke^.NA , NAG_Location , NAS_Y , Y );
		end;
	end;
	if ( Random( 3 ) = 1 ) and TerrMan[ GB^.map[X,Y].terr ].Flammable then begin
		Smoke := LoadNewSTC( 'FIRE-1' );
		if Smoke <> Nil then begin
			Smoke^.Scale := GB^.Scale;
			AppendGear( GB^.Meks , Smoke );
			SetNAtt( Smoke^.NA , NAG_Location , NAS_X , X );
			SetNAtt( Smoke^.NA , NAG_Location , NAS_Y , Y );
		end;
		SetTrigger( GB , 'FIRE!' );
	end;
end;

Procedure SceneryChewing( GB: GameBoardPtr; X,Y,DC: Integer; Accident: Boolean; AtAt: String );
	{ Tile X,Y has been hit. Try and damage it. }
	{ Set ACCIDENT to TRUE if the tile is not the primary target of }
	{ the attack, FALSE if it is. }
begin
	{ Start with an error check... }
	if not OnTheMap( X , Y ) then Exit;

	if HasAttackAttribute( AtAt , AA_Brutal ) then begin
		DC := DC * 2;
	end;
	if HasAttackAttribute( AtAt , AA_BlastAttack ) then begin
		DC := DC + Random( DC + 1 );
	end;

	{ Modify for accidental damage. If not intentionally trying }
	{ to cause terrain damage, it becomes far less likely to happen. }
	if ( DC > 0 ) and Accident then DC := Random( DC );

	if ( DC > 0 ) and ( Random( DC ) >= TerrMan[ GB^.map[X,Y].terr ].DMG ) and ( TerrMan[ GB^.map[X,Y].terr ].DMG > 0 ) then begin
		{ Demolish the scenery... unless there's an error in the definitions. }
		if ( TerrMan[ GB^.map[X,Y].terr ].Destroyed > 0 ) and ( TerrMan[ GB^.map[X,Y].terr ].Destroyed <= NumTerr ) then begin
			DestroyTerrain( GB , X , Y );
		end;
	end;
end;

Procedure ProcessStatusEffect( GB: GameBoardPtr; var EReq: EffectRequest );
	{ Stick a status effect on the target, unless it resists. }
var
	TMaster: GearPtr;
	AttackRoll,DefRoll: Integer;
begin
	{ Can only process this effect if we have a target and a valid }
	{ status effect to process. }
	TMaster := FindRoot( EReq.Target );

	if GearActive( TMaster ) and ( EReq.FXOption >= 1 ) and ( EReq.FXOption <= Num_Status_FX ) then begin
		{ If a weapon is defined, it must be of a scale at least as great as the target. }
		if ( EReq.Weapon <> Nil ) and ( EReq.Weapon^.Scale < TMaster^.Scale ) then Exit;

		if SX_Vunerability[ EReq.FXOption , NAttValue( TMaster^.NA , NAG_GearOps , NAS_Material ) ] then begin
			AttackRoll := RollStep( EReq.FXSkill );
			DefRoll := AttemptDefenses( GB , EReq.Target , EReq.Weapon , AttackRoll , EReq.AF );
			if AttackRoll > DefRoll then begin
				AddNAtt( TMaster^.NA , NAG_StatusEffect , EReq.FXOption , EReq.FXDice );
				RecordAnnouncement( ReplaceHash( SAttValue( FX_Messages , 'Status_Announce' + BStr( EReq.FXOption ) ) , GearName( TMaster ) ) );
			end;
		end;
	end;
end;

Procedure ProcessHealingEffect( GB: GameBoardPtr; var EReq: EffectRequest );
	{ Do healing on target. }
var
	TMaster: GearPtr;
	RepairRoll,D0,D1: LongInt;
	msg: String;
begin
	{ Can only process this effect if we have a target. }
	TMaster := FindRoot( EReq.Target );
	if ( TMaster <> Nil ) then begin
		{ Record how much repairable damage we started with... }
		D0 := TotalRepairableDamage( TMaster , EReq.FXOption );

		{ Do some repairs. }
		RepairRoll := RollStep( EReq.FXDice ) + EReq.FXMod;
		ApplyRepairPoints( TMaster , EReq.FXOption , RepairRoll );

		{ Find out how much repairable damage we have now... }
		D1 := TotalRepairableDamage( TMaster , EReq.FXOption );

		{ Record the announcement, if any healing done. }
		if ( D0 - D1 ) > 0 then begin
			msg := ReplaceHash( SAttValue( FX_Messages , 'Healing_Announce' ) , GearName( TMaster ) );
			msg := ReplaceHash( msg , BStr( D0 - D1 ) );
			RecordAnnouncement( msg );
		end;
	end;
end;

Procedure ProcessOverloadEffect( GB: GameBoardPtr; var EReq: EffectRequest );
	{ Apply overload to the target, unless it resists. }
var
	TMaster: GearPtr;
	AttackRoll,DefRoll: Integer;
begin
	{ Can only process this effect if we have a target and a valid }
	{ status effect to process. }
	TMaster := FindRoot( EReq.Target );
	if GearActive( TMaster ) and ( TMaster^.G = GG_Mecha ) then begin
		AttackRoll := RollStep( EReq.FXSkill );
		DefRoll := AttemptDefenses( GB , EReq.Target , EReq.Weapon , AttackRoll , EReq.AF );
		if AttackRoll > DefRoll then begin
			AddNAtt( TMaster^.NA , NAG_Condition , NAS_Overload , EReq.FXDice + Random( EReq.FXDice ) + ( ( AttackRoll - DefRoll ) div 3 )  + 5 );
			RecordAnnouncement( ReplaceHash( SAttValue( FX_Messages , 'Status_Overload' ) , GearName( TMaster ) ) );
		end else begin
			AddNAtt( TMaster^.NA , NAG_Condition , NAS_Overload , Random( EReq.FXDice ) + 2 );
		end;
	end;
end;

Procedure ProcessAttackEffect( GB: GameBoardPtr; AReq: EffectRequest );
	{ Process a general attack effect against TARGET. }
var
	CritHit,CritTar: Integer;
	TargetOK: Boolean;
	HiDefRoll: Integer;
	T: Integer;
begin
	{ Initialize values. }
	ATTACK_TPilot := LocatePilot( AReq.Target );
	if ( ATTACK_TPilot = Nil ) or Destroyed( ATTACK_TPilot ) then ATTACK_TPilotOK := False
	else ATTACK_TPilotOK := True;

	if Areq.AF.CantCallShot then begin
		{ Can't aim for a subcomponent. }
		AReq.Target := FindRoot( AReq.Target );
	end;

	ATTACK_AMaster := FindRoot( AReq.Originator );
	ATTACK_TMaster := FindRoot( Areq.Target );

	ATTACK_Error := False;

	{ See whether or not the gear is already destroyed, then }
	{ whether or not it is already immobilized. }
	if ( ATTACK_TMaster = Nil ) or Destroyed( ATTACK_TMaster ) then ATTACK_TMasterOK := False
	{ If the master is a character type, set TMasterOK to false, so it }
	{ doesn't go printing a 'Josh is destroyed! Josh is killed!' message. }
	else if ATTACK_TMaster = ATTACK_TPilot then ATTACK_TMasterOK := False
	else ATTACK_TMasterOK := True;
	ATTACK_TMasterMove := BaseMoveRate( ATTACK_TMaster ) > 0;

	ATTACK_AttackerDamage := 0;

	ATTACK_AttSkillRank := AReq.FXSkill;

	{ Record whether or not the target is operational at the start of the attack. }
	TargetOK := GearOperational( AReq.Target );

	ATTACK_AttackRoll := RollStep( AReq.FXSkill ) + AReq.FXMod;

	{ Start attempting TMaster's defenses against the attack. Use all }
	{ appropriate defenses. }
	ATTACK_HiDefRoll := AttemptDefenses( GB , ATTACK_TMaster , AReq.Weapon , ATTACK_AttackRoll , AReq.AF );

	{ *********************************************** }
	{ *** DETERMINE WHETHER A HIT WAS MADE OR NOT *** }
	{ *********************************************** }
	{ Normally just beating the attack roll means that the defender }
	{ got away, but with area effect attacks it's not so easy. }
	if HasAreaEffect( AReq.AF.AtAt ) then begin
		{ For every time the defense roll beat the attack roll, }
		{ the damage of the attack is reduced by half. }
		if ATTACK_AttackRoll < 1 then ATTACK_AttackRoll := 1;
		HiDefRoll := ATTACK_HiDefRoll;
		while HiDefRoll > ATTACK_AttackRoll do begin
			HiDefRoll := HiDefRoll div 2;
			AReq.FXDice := AReq.FXDice div 2;
		end;
		ATTACK_ItHit := AReq.FXDice > 0;
		ATTACK_Dodge := Not ATTACK_ItHit;
	end else begin
		ATTACK_ItHit := ATTACK_HiDefRoll < ATTACK_AttackRoll;
	end;

	{ ******************************************** }
	{ *** PERFORM RESULTS OF SUCCESSFUL ATTACK *** }
	{ ******************************************** }
	{ If the attack made it through all of TMaster's defenses, }
	{ actually do the damage. }
	if ATTACK_ItHit then begin
		if AReq.Originator <> Nil then begin
			{ The attack hit. Give the attacker his 2 XP for hitting. }
			DoleExperienceFromTarget( ATTACK_AMaster , ATTACK_TMaster , XPA_AttackHit );

			{ If this was a difficult shot, give extra XP. }
			if ( AReq.FXSkill + AReq.FXMod ) < 5 then begin
				DoleExperienceFromTarget( ATTACK_AMaster , ATTACK_TMaster , ( 7 - AReq.FXSkill - AReq.FXMod ) div 2 );
			end;
		end;


		{ Calculate the Measure of Success and the number of hits. }
		{ Measure of Success applies only against masters. When }
		{ attacking an inanimate object, it's assumed that finesse }
		{ plays a very minor role, unless the attacker is a GATECRASHER. }
		if IsMasterGear( AReq.Target ) and ( AReq.Target^.G <> GG_Prop ) then begin
			ATTACK_MOS := ( ATTACK_AttackRoll - ATTACK_HiDefRoll ) div MOSMeasure;
		end else if HasTalent( AReq.Originator , NAS_GateCrasher ) then begin
			ATTACK_MOS := 2;
		end else begin
			ATTACK_MOS := 0;
		end;
		ATTACK_NumberOfHits := 1 + AReq.FXOption;


		{ At this point in time, before MOS gets modified for }
		{ specific weapon systems, dole the MOS XP. }
		if ( AReq.Originator <> Nil ) and ( ATTACK_MOS > 0 ) and NotDestroyed( AReq.Target ) then begin
			DoleExperienceFromTarget( AReq.Originator , ATTACK_TMaster , ATTACK_MOS * XPA_PerMOS );
			if AReq.Weapon <> Nil then DoleSkillExperience( AReq.Originator , AttackSkillNeeded( AReq.Weapon ) , XPA_SK_Critical );
		end;

		{ Perform modifications which only count if }
		{ we have a pointer to the weapon. }
		if ( AReq.Weapon <> Nil ) then begin
			{ Modify number of hits by weapon type and AtAt. }
			if AReq.Weapon^.G = GG_Weapon then begin
				if ( AReq.Weapon^.S = GS_Ballistic ) or ( AReq.Weapon^.S = GS_BeamGun ) or ( AReq.Weapon^.S = GS_Missile ) then begin
					if AReq.FXOption > 0 then begin
						ATTACK_NumberOfHits := ATTACK_AttackRoll - ATTACK_HiDefRoll;
						if AReq.FXOption > 9 then begin
							if ATTACK_NumberOfHits > 10 then ATTACK_NumberOfHits := 10;
							ATTACK_NumberOfHits := ( ( AReq.FXOption + 1 ) * ATTACK_NumberOfHits ) div 10;
						end else begin
							if ATTACK_NumberOfHits > (AReq.FXOption + 1) then ATTACK_NumberOfHits := AReq.FXOption + 1;
						end;
					end;
				end else if ( AReq.Weapon^.S = GS_Melee ) then begin
					{ Close combat weapons can trade a high MOS for multiple hits. }
					if ATTACK_MOS > 6 then begin
						ATTACK_NumberOfHits := ATTACK_NumberOfHits + ATTACK_MOS - 6;
						ATTACK_MOS := 6;
					end;
					while ( ATTACK_MOS > 0 ) and ( Random(5) = 1 ) do begin
						Inc( ATTACK_NumberOfHits );
						Dec( ATTACK_MOS );
					end;
				end else if ( AReq.Weapon^.S = GS_EMelee ) then begin
					{ Close combat weapons can trade a high MOS for multiple hits. }
					while ( ATTACK_MOS > 0 ) and ( Random(6) = 1 ) do begin
						Inc( ATTACK_NumberOfHits );
						Dec( ATTACK_MOS );
					end;
				end;
			end else if AReq.Weapon^.G = GG_Module then begin
				{ Fighting attacks have a higher chance of scoring }
				{ multiple hits on a good roll. }
				if ATTACK_MOS > 6 then begin
					ATTACK_NumberOfHits := ATTACK_NumberOfHits + ATTACK_MOS - 6;
					ATTACK_MOS := 6;
				end;
				while ( ATTACK_MOS > 2 ) and ( Random(4) <> 1 ) do begin
					Inc( ATTACK_NumberOfHits );
					Dec( ATTACK_MOS );
				end;

				{ Modify the MOS for KungFu. }
				{ This will be modified again later for being a nonweapon... }
				if HasTalent( ATTACK_AMaster , NAS_KungFu ) then ATTACK_MOS := ATTACK_MOS + Non_Weapon_MOS_Penalty + 1;
			end;
	
			{ Modify MOS and DC for non-weapons and EMWs. }
			if AReq.Weapon^.G <> GG_Weapon then begin
				ATTACK_MOS := ATTACK_MOS - Non_Weapon_MOS_Penalty;
				if ATTACK_MOS < 0 then begin
					AReq.FXDice := AReq.FXDice + ATTACK_MOS;
					if AReq.FXDice < 1 then AReq.FXDice := 1;
				end;

			end else if ( AReq.Weapon^.G = GG_Weapon ) and ( AReq.Weapon^.S = GS_EMelee ) then begin
				ATTACK_MOS := ATTACK_MOS + 2;
			end;
			if HasAttackAttribute( AReq.AF.AtAt , AA_ArmorPiercing ) then ATTACK_MOS := ATTACK_MOS + 2
			else if HasAttackAttribute( AReq.AF.AtAt , AA_ArmorIgnore ) then ATTACK_MOS := ATTACK_MOS + 12;
		end;

		{ If called shots are illegal right now, reduce MOS }
		{ by 2 to represent the general lack of precision. }
		if AReq.AF.CantCallShot then begin
			ATTACK_MOS := ATTACK_MOS - 2;

		end else if AReq.Originator <> Nil then begin
			{ Modify MOS for Critical Hit skill. }
			{ Use variable SPD to represent the critical hit target # }
			CritHit := RollStep( SkillValue( AReq.Originator , 18 ));
			CritTar := ATTACK_HiDefRoll;

			{ If the high defense roll was lower than the Critical }
			{ Hit Minimum Target number, raise it. }
			if CritTar < CritHitMinTar then CritTar := CritHitMinTar;
			if CritHit > CritTar then begin
				{ Note that the MOSMeasure is doubled for Critical Hit skill use, since this is kinda like bonus MOS. }
				{ Also note that this bonus does apply to non-master targets. }
				ATTACK_MOS := ATTACK_MOS + ( ( CritHit - CritTar ) div MOSMeasure ) + 1;
			end;

			{ If the originator has Spot Weakness skill, modify damage for that. }
			if HasSkill( AReq.Originator , 18 ) then begin
				if HasTalent( AReq.Originator , NAS_Sniper ) then begin
					AReq.FXDice := AReq.FXDice + SkillRank( AReq.Originator , 18 );
				end else begin
					AReq.FXDice := AReq.FXDice + ( SkillRank( AReq.Originator , 18 ) div 2 );
				end;
			end;

			{ Modify MOS for miscellaneous other talents. }
			{ ANATOMIST talent - +1 MOS vs Meat targets }
			if HasTalent( AReq.Originator , NAS_Anatomist ) and ( NAttValue( AReq.Target^.NA , NAG_GearOps , NAS_Material ) = NAV_Meat ) then begin
				ATTACK_MOS := ATTACK_MOS + 1;
			end;
		end;

		{ Modify MOS for the "HARD AS NAILS", "HULL DOWN" talents. }
		if ( ATTACK_TMaster^.G = GG_Character ) and HasTalent( ATTACK_TMaster , NAS_HardAsNails ) then ATTACK_MOS := ATTACK_MOS - 2;
		if (ATTACK_TMaster^.G = GG_Mecha) and HasTalent(ATTACK_TMaster,NAS_HullDown) and ((NAttValue(ATTACK_TMaster^.NA,NAG_Action,NAS_MoveMode)= MM_WALK) or (NAttValue(ATTACK_TMaster^.NA,NAG_Action,NAS_MoveMode)=MM_ROLL)) then ATTACK_MOS := ATTACK_MOS - 3;

		ATTACK_DamageDone := DamageGear( GB , AReq.Target , AReq.Weapon , AReq.FXDice , ATTACK_MOS , ATTACK_NumberOfHits , AReq.AF.AtAt );

		{ If, at the beginning of this attack, the target was }
		{ functioning, check to see if the attacker gets extra }
		{ experience for taking the target out. }
		if ( AReq.Originator <> Nil ) and TargetOK then begin
			if AReq.Target^.G = GG_Mecha then begin
				if not GearOperational( AReq.Target ) then DoleExperience( AReq.Originator , AReq.Target , XPA_DestroyMaster );
			end else if IsMasterGear( AReq.Target ) then begin
				if Destroyed( AReq.Target ) then DoleExperience( AReq.Originator , AReq.Target , XPA_DestroyMaster );
			end else begin
				{ Destroying a non-master gear only gives 1 XP. }
				if Destroyed( AReq.Target ) then DoleExperience( AReq.Originator , XPA_DestroyThing );
			end;
		end;

	end else begin
		{ The attack missed. Set Damage to 0, and give 2XP to the }
		{ target for having avoided the attack. }
		ATTACK_DamageDone := 0;
		DoleExperienceFromTarget( ATTACK_TMaster , ATTACK_AMaster , XPA_AvoidAttack );
	end;

	{ Store the results of this attack. }
	if AReq.FXDesc = '' then begin
		INDICATE_Latest_Attack( GB );
	end else begin
		INDICATE_Attack_Effect( GB , AReq.FXDesc );
	end;

	{ Cause status effects as appropriate. }
	if ATTACK_ItHit then begin
		{ Set the skill value for ancilliary effects. }
		{ If the attacker is a mecha, use EW skill. Otherwise leave }
		{ the same skill value as was used in the attack. }
		if ( ATTACK_AMaster <> Nil ) and ( ATTACK_AMaster^.G = GG_Mecha ) then begin
			AReq.FXSkill := SkillValue( ATTACK_AMaster , 17 );
		end;

		{ If this weapon causes overload, do that now. }
		If HasAttackAttribute( AReq.AF.AtAt , AA_Overload ) then begin
			AReq.FXType := FX_Overload;
			AReq.AF.CanDodge := False;
			AReq.AF.CanParry := False;
			AReq.AF.CanBlock := False;
			AReq.AF.CanIntercept := False;
			AReq.AF.CanECM := False;
			AReq.AF.CanResist := True;
			ProcessOverloadEffect( GB , AReq );
		end;

		AReq.FXType := FX_CauseStatusFX;
		AReq.FXDice := 1;
		AReq.AF.CanDodge := False;
		AReq.AF.CanParry := False;
		AReq.AF.CanBlock := False;
		AReq.AF.CanIntercept := False;
		AReq.AF.CanECM := False;
		AReq.AF.CanResist := True;

		{ Check each status effect in order. }
		for T := 1 to Num_Status_FX do begin
			AReq.FXOption := T;
			if AStringHasBString( AReq.AF.AtAt , SX_Name[ AReq.FXOption ] ) then ProcessStatusEffect( GB , AReq );
		end;
	end;

	{ Chew the scenery. }
	if ( AReq.Weapon = Nil ) or ( AReq.Weapon^.Scale >= GB^.Scale ) then begin
		SceneryChewing( GB, AReq.TX,AReq.TY,AReq.FXDice, Not ATTACK_ItHit , AReq.AF.AtAt );
	end;

	{ If the mek's move mode has been disabled, it will crash here. }
	if ATTACK_TMasterMove and (BaseMoveRate( ATTACK_TMaster ) = 0) then begin
		Crash( GB , ATTACK_TMaster );
		AddSAtt( ATTACK_History , 'ANNOUNCE_' + BStr( EFFECTS_Event_Order + 1 ) + '_' , PilotName( ATTACK_TMaster ) + ' has crashed!' );
	end;
end;

Procedure InvokeEffect( GB: GameBoardPtr; var EReq: EffectRequest );
	{ Check the effect type, then do something appropriate based }
	{ upon that. }
begin
	Case EReq.FXType of
		FX_DoDamage:	ProcessAttackEffect( GB , Ereq );
		FX_CauseStatusFX:	ProcessStatusEffect( GB , EReq );
		FX_DoHealing:	ProcessHealingEffect( GB , EReq );
		FX_Overload: 	ProcessOverloadEffect( GB , EReq );
	end;
end;

Procedure FunkyMartialArts( var AReq: EffectRequest );
	{ This attack may well get some special bonuses. }
const
	NumFMABase = 10;
	Num_Funky_Things = 10;
	FT_Cost: Array [1..Num_Funky_Things] of Byte = (
		2, 2, 3, 5, 3,
		3, 4, 2, 1, 3
	);
	FT_AA: Array [1..Num_Funky_Things] of String[15] = (
	'','','SCATTER','HYPER','ARMORPIERCING',
	'BRUTAL','STONE','HAYWIRE','STUN',''
	);
	FT_Heroic = 1;
	FT_Zen = 2;
	FT_Snake = 10;
var
	SkRk,TP: Integer;
	Adjective,Noun: SAttPtr;
	msg,C: String;
	Function CanGetFunkyThing( N: Integer ): Boolean;
		{ Return TRUE if the attacker can do this funky thing, based on }
		{ Technique Points and whatever else, or FALSE if he can't. }
	begin
		if FT_Cost[ N ] <= TP then begin
			if N = FT_Heroic then begin
				{ Can only perform a heroic attack if heroic. }
				CanGetFunkyThing := NAttValue( AReq.Originator^.NA , NAG_CharDescription , NAS_Heroic ) > 10;
			end else if N = FT_Zen then begin
				{ Can only perform a mystic attack if spiritual. }
				CanGetFunkyThing := NAttValue( AReq.Originator^.NA , NAG_CharDescription , NAS_Pragmatic ) < -10;
			end else begin
				{ Nothing else has any special requirements. }
				CanGetFunkyThing := True;
			end;
		end else begin
			{ If not enough points, can't do this thing. }
			CanGetFunkyThing := False;
		end;
	end;
	Procedure ApplyFunkyThing( N: Integer );
		{ Apply the funky thing to the attack request; reduce the total number }
		{ of technique points; store a noun and an adjective to describe this }
		{ attack. }
	begin
		TP := TP - FT_Cost[ N ];
		AReq.AF.AtAt := AReq.AF.AtAt + ' ' + FT_AA[ N ];
		if Random( 2 ) = 1 then StoreSAtt( Adjective , SAttValue( FX_Messages , 'FMAFT_A' + BStr( N ) ) )
		else StoreSAtt( Noun , SAttValue( FX_Messages , 'FMAFT_N' + BStr( N ) ) );

		{ Add bonuses for special things here. }
		if N = FT_Heroic then begin
			{ A heroic attack increases damage done based on the character's heroism. }
			AReq.FXDice := AReq.FXDice + ( NAttValue( AReq.Originator^.NA , NAG_CharDescription , NAS_Heroic ) div 5 );
		end else if N = FT_Zen then begin
			{ A zen attack increases accuracy based on the character's spirituality. }
			AReq.FXSkill := AReq.FXSkill + ( Abs( NAttValue( AReq.Originator^.NA , NAG_CharDescription , NAS_Pragmatic ) ) div 10 );
		end else if N = FT_Snake then begin
			AReq.AF.CanParry := False;
			AReq.AF.CanBlock := False;
		end;
	end;
begin
	{ First, make sure we have an originator, and that it's a character. }
	if ( AReq.Originator = Nil ) or ( AReq.Originator^.G <> GG_Character ) then exit;

	{ The attacker must have a martial arts skill of at least 5 to benefit. }
	SkRk := NAttValue( AReq.Originator^.NA , NAG_Skill , 9 ) - 4;
	if SkRk < 1 then Exit;

	{ TP is Technique Points. }
	TP := 0;

	{ The number of technique points is determined semi-randomly from the skill rank. }
	while SkRk > 0 do begin
		if SkRk < 2 then begin
			if Random( 2 ) < SkRk then Inc( TP );
		end else if Random( 3 ) <> 1 then Inc( TP );
		SkRk := SkRk - 2;
	end;

	if CurrentMental( AReq.Originator ) > ( 10 + Random( 20 ) ) then begin
		inc( TP );
		AddMentalDown( AReq.Originator , 1 );
	end;

	{ If any technique points were gained, put them to good use here. }
	{ Technique points can buy attack improvements: attack attributes, various bonuses, }
	{  status effects... }
	if TP > 0 then begin
		{ Initialize the variables needed for our attack name generator. }
		Adjective := Nil;
		Noun := Nil;
		if ( AReq.Weapon^.S = GS_Arm ) and ( Random( 5 ) <> 1 ) then begin
			Msg := SATtValue( FX_Messages , 'FMA_Name_Punch_' + BStr( Random( NumFMABase ) + 1 ) );
		end else if ( AReq.Weapon^.S = GS_Leg ) and ( Random( 5 ) <> 1 ) then begin
			Msg := SATtValue( FX_Messages , 'FMA_Name_Kick_' + BStr( Random( NumFMABase ) + 1 ) );
		end else begin
			Msg := SATtValue( FX_Messages , 'FMA_Name_Misc_' + BStr( Random( NumFMABase ) + 1 ) );
		end;

		while TP > 0 do begin
			SkRk := Random( Num_Funky_Things ) + 1;
			if CanGetFunkyThing( SkRk ) then begin
				ApplyFunkyThing( SkRk );
			end else begin
				{ If the thing chosen can't be gotten, just give a bonus }
				{ to damage. }
				Inc( AReq.FXDice );
				Dec( TP );
			end;
		end;

		AReq.FXName := '';
		while msg <> '' do begin
			C := ExtractWord( msg );
			if C = '%A' then begin
				if Adjective <> Nil then begin
					AReq.FXName := AReq.FXName + ' ' + SelectRandomSAtt( Adjective )^.Info;
				end else begin
					AReq.FXName := AReq.FXName + ' ' + SAttValue( FX_Messages , 'FMAFT_MISCA' + BStr( Random( 5 ) + 1 ) );
				end;
			end else if C = '%N' then begin
				if Noun <> Nil then begin
					AReq.FXName := AReq.FXName + ' ' + SelectRandomSAtt( Noun )^.Info;
				end else begin
					AReq.FXName := AReq.FXName + ' ' + SAttValue( FX_Messages , 'FMAFT_MISCN' + BStr( Random( 5 ) + 1 ) );
				end;
			end else begin
				AReq.FXName := AReq.FXName + ' ' + C;
			end;
		end;

		DisposeSAtt( Adjective );
		DisposeSAtt( Noun );

	end;
end;

Function FillAttackRequest( GB: GameBoardPtr; Attacker,Target: GearPtr; AtOp,AMod: Integer; AtAt: String; Accident: Boolean ): EffectRequest;
	{ Fill out the attack request based on the information provided. }
var
	AttackSkill: Integer;
	AReq: EffectRequest;
begin
	{ Fill out the effect request. }
	AReq.FXName := GearName( Attacker );
	AReq.FXType := FX_DoDamage;
	AReq.Target := Target;
	AReq.Originator := FindRoot( Attacker );
	AReq.Weapon := Attacker;
	AReq.AF.CantCallShot := NoCalledShots( AtAt , AtOp );
	AReq.AF.CanDodge := True;
	AReq.AF.CanBlock := not HasAttackAttribute( AtAt , AA_Flail );
	if Attacker <> Nil then begin
		AReq.AF.CanParry := ( ( Attacker^.G <> GG_Weapon ) or ( Attacker^.S = GS_Melee ) or ( Attacker^.S = GS_EMelee ) ) and not HasAttackAttribute( AtAt , AA_Flail );
		AReq.AF.CanIntercept := ( Attacker^.G = GG_Weapon ) and ( Attacker^.S = GS_Missile );
		AReq.AF.CanECM := ( Attacker^.G = GG_Weapon ) and (( Attacker^.S = GS_Ballistic) or ( Attacker^.S = GS_BeamGun ) or ( Attacker^.S = GS_Missile ));
	end;
	AReq.AF.CanResist := False;
	AReq.AF.AtAt := AtAt;
	if Target <> Nil then begin
		AReq.TX := NAttValue( FindRoot( Target )^.NA , NAG_Location , NAS_X );
		AReq.TY := NAttValue( FindRoot( Target )^.NA , NAG_Location , NAS_Y );
		AReq.FXMod := CalcTotalModifiers( gb , Attacker , Target , AtOp , AtAt ) + AMod;
	end else begin
		AReq.FXMod := AMod;
	end;
	AReq.FXDice := WeaponDC( Attacker , AtOp );

	AttackSkill := AttackSkillNeeded( Attacker );
	AReq.FXSkill := SkillValue( AReq.Originator , AttackSkill );

	AReq.FXOption := AtOp;
	AReq.FXDesc := '';

	{ If this is a martial arts attack, some extra stuff is going to go on now. }
	if ( AttackSkill = 9 ) then begin
		FunkyMartialArts( AReq );
	end;

	{ Add the surprise attack bonuses. }
	if ( Areq.Originator <> Nil ) and ( Target <> Nil ) and not MekCanSeeTarget( GB , FindRoot( Target ) , AReq.Originator ) then begin
		if HasTalent( Areq.Originator , NAS_Ninjitsu ) then begin
			AReq.FXMod := AReq.FXMod + MOSMeasure * 2;
			AReq.FXDice := AReq.FXDice * 3 div 2;
		end else begin
			AReq.FXMod := AReq.FXMod + MOSMeasure;
			AReq.FXDice := AReq.FXDice * 5 div 4;
		end;
	end;

	FillAttackRequest := AReq;
end;

Procedure AttackSingleTarget( GB: GameBoardPtr; Attacker,Target: GearPtr; AtOp,AMod: Integer; AtAt: String; Accident: Boolean );
	{ This procedure will attack a single target. }
	{ ACCIDENT is true if the attacker isn't really trying to hit }
	{  the target, such as when a friendly unit accidentally gets }
	{  in the way of an attack. }
var
	AReq: EffectRequest;
begin
	{ Error check. }
	if ( Attacker = Nil ) or ( Target = Nil ) then begin
		ATTACK_Error := True;
		exit;
	end;

	AReq := FillAttackRequest( GB,Attacker,Target,AtOp,AMod,AtAt,Accident );

	{ Give a meager skill experience bonus. }
	DoleSkillExperience( AReq.Originator , AttackSkillNeeded( Attacker ) , XPA_SK_Basic );

	{ Call the attack processor. }
	ProcessAttackEffect( GB , AReq );
end;

Procedure DoDirectFireAttack( GB: GameBoardPtr; Attacker,Target: GearPtr; X,Y,Z,AtOp,AMod: Integer; AtAt: String );
	{ Perform a direct fire attack. Direct fire is the basic attack type. }
var
	msg: String;
	X0,Y0,Z0: Integer;
	AReq: EffectRequest;
begin
	{ If an initial shot animation is required, add that now. }
	X0 := NAttValue( FindMaster( Attacker )^.NA , NAG_Location , NAS_X );
	Y0 := NAttValue( FindMaster( Attacker )^.NA , NAG_Location , NAS_Y );
	Z0 := MekALtitude( GB , FindRoot( Attacker ) );
	Add_Shot_Precisely( GB , X0 , Y0 , Z0 , X , Y , Z );

	AReq := FillAttackRequest( GB,Attacker,Target,AtOp,AMod,AtAt,False );

	{ Actually perform the attack. }
	if Target <> Nil then begin
		msg := PilotName( FindMaster( Attacker ) ) + ' attacks ' + PilotName( FindMaster( Target ) )+' with ' + AReq.FXName + '.';
		RecordAnnouncement( msg );
		Inc( EFFECTS_Event_Order );
		{ Give a meager skill experience bonus. }
		DoleSkillExperience( AReq.Originator , AttackSkillNeeded( Attacker ) , XPA_SK_Basic );
		ProcessAttackEffect( GB , AReq );
	end else if Attacker^.Scale >= GB^.Scale then begin
		msg := PilotName( FindMaster( Attacker ) ) + ' fires ' + GearName( Attacker ) + '.';
		RecordAnnouncement( msg );
		Inc( EFFECTS_Event_Order );
		Add_Point_Animation( X , Y , Z , GS_DamagingHit );
		SceneryChewing( GB , X , Y , WeaponDC( Attacker , AtOp ) + 2 , False , AtAt );
	end;
end;

Function MekIsTargetInRadius( GB: GameBoardPtr; Mek,Attacker,Weapon,Spotter: GearPtr; X,Y,R: Integer ): Boolean;
	{ Used by the NumTargetsInRadius and FindTargetInRadius functions. }
	{ Returns TRUE is Mek is an enemy of ATTACKER, is visible by }
	{ SPOTTER, and is within the prescribed screen area. }
begin
	Spotter := FindRoot( SPotter );
	MekIsTargetInRadius := AreEnemies( GB , Attacker , Mek ) and MekCanSeeTarget( GB , Spotter , Mek ) and RangeArcCheck( GB , Attacker , Weapon , Mek ) and ( Range( Mek , X , Y ) <= R ) and GearOperational( Mek );
end;

Function NumTargetsInRadius( GB: GameBoardPtr; Attacker,Weapon,Spotter: GearPtr; X,Y,R: Integer ): Integer;
	{ Determine the number of targets within the radius which can be }
	{ seen by SPOTTER and are enemies of ATTACKER. }
var
	N: Integer;
	M: GearPtr;
begin
	N := 0;
	M := GB^.Meks;
	while M <> Nil do begin
		if MekIsTargetInRadius( GB, M, Attacker, Weapon, Spotter, X, Y, R ) then Inc( N );
		M := M^.Next;
	end;
	NumTargetsInRadius := N;
end;

Function SwarmRadius( GB: GameBoardPtr; Attacker: GearPtr ): Integer;
	{ Return the radius at which this weapon swarms. Default value }
	{ is one-half the regular short range. }
begin
	if Attacker^.G = GG_Ammo then begin
		{ Thrown weapons have an effectively infinite swarm }
		{ spread. Why? Because I want shurikens to be cool. }
		SwarmRadius := 5;
	end else begin
		SwarmRadius := ( WeaponRange( GB , Attacker ) + 5 ) div 6;
	end;
end;

Procedure DoSwarmAttack( GB: GameBoardPtr; Attacker: GearPtr; X,Y,AtOp,AMod: Integer; AtAt: String );
	{ Perform a swarm attack. This attack will target the primary }
	{ target and also others within range. }
var
	msg: String;
	Mek: GearPtr;
	N,T,AtOp2,R: Integer;
begin
	msg := PilotName( FindMaster( Attacker ) ) + ' fires ' + GearName( Attacker ) + '.';
	RecordAnnouncement( msg );

	R := SwarmRadius( GB , Attacker );
	N := NumTargetsInRadius( GB , FindRoot( Attacker ) , Attacker , FindRoot( Attacker ) , X , Y , R );

	if N > 0 then begin
		Mek := GB^.Meks;
		T := 1;
		while Mek <> Nil do begin
			AtOp2 := ( AtOp + 1 ) div N;
			if T <= ( ( AtOp + 1 ) mod N ) then Inc( AtOp2 );

			if MekIsTargetInRadius( GB, Mek, FindRoot( Attacker ) , Attacker, FindRoot( Attacker ), X, Y, R ) and ( AtOp2 > 0 ) then begin
				Add_Shot_Animation( GB , Attacker , Mek );
				Inc( EFFECTS_Event_Order );
				AttackSingleTarget( GB , Attacker , Mek , AtOp2 - 1 , AMod , AtAt , False );
				Dec( EFFECTS_Event_Order );
				Inc( T );
			end;

			mek := Mek^.Next;
		end;
	end;

	Inc( EFFECTS_Event_Order );
end;

Function BlastRadius( GB: GameBoardPtr; Attacker: GearPtr; AList: String ): Integer;
	{ Return the blast radius of this weapon. }
var
	AA: String;
	R,T: Integer;
begin
	{ Initialize radius to 0. }
	R := 0;

	{ Move through the string looking for the BLAST attribute. }
	{ The radius should be right after it. }
	while ( AList <> '' ) and ( R = 0 ) do begin
		AA := UpCase( ExtractWord( AList ) );
		if AA = AA_Name[ AA_BlastAttack ] then R := ExtractValue( AList );
	end;

	if Attacker^.Scale > GB^.Scale then begin
		for t := 1 to ( Attacker^.Scale - GB^.Scale ) do r := r * 2;
	end else begin
		{ The weapon scale must be smaller then the }
		{ game board scale. }
		for t := 1 to ( GB^.Scale - Attacker^.Scale ) do r := r div 2;
	end;


	{ Error check on the blast radius's range. }
	if R < 1 then R := 1
	else if R > Max_Blast_Rating then R := Max_Blast_Rating;

	{ Return the result. }
	BlastRadius := R;
end;

Procedure ClearStencil;
	{ Clear the stencil, i.e. show no squares covered by effects. }
var
	X,Y: Integer;
begin
	for X := 1 to XMax do begin
		for Y := 1 to YMax do begin
			Stencil[ X , Y ] := False;
		end;
	end;
end;

Procedure DrawBlastEffect( GB: GameBoardPtr; X0,Y0,Z0,RNG: Integer );
	{ Calculate all the squares targeted by a blast effect centered }
	{ upon X0,Y0 with radius RNG. Store the results of the calculation }
	{ in the STENCIL array. }
const
	DBA_True = 1;
	DBA_False = -1;
	DBA_Maybe = 0;
var
	temp: Array [ -Max_Blast_Rating..Max_Blast_Rating , -Max_Blast_Rating..Max_Blast_Rating ] of integer;
	x,y: Integer;

	Procedure CheckLine(XT,YT: Integer);
	var
		t: Integer;	{A counter, and a terrain type.}
		Wall: Boolean;	{Have we hit a wall yet?}
		p: Point;
	begin
		{Check every point on the line from the origin to XT,YT,}
		{recording the results in the Temp array.}

		{ The variable WALL represents a boundary that cannot be }
		{ blasted through. }
		Wall := false;

		for t := 1 to rng do begin
			{Locate the next point on the line.}
			p := SolveLine(0,0,XT,YT,t);

			{Determine the terrain of this tile.}
			if OnTheMap( p.X + X0 , p.Y + Y0 ) then begin
				{If we have already encountered a wall, mark this square as UPV_False}
				if Wall then temp[p.x,p.y] := DBA_False;

				Case temp[p.x,p.y] of
					DBA_False: Break; {This LoS is blocked. No use searching any further.}
					DBA_Maybe: begin  {We will mark this one as true, but check for a wall later.}
						temp[p.x,p.y] := DBA_True;
						end;
					{If we got a DBA_True, we just skip merrily along without doing anything.}
				end;

				{If this current square is a wall,}
				{or if we have too much obscurement to see,}
				{set Wall to true.}
				if TileBlocksLOS( GB , p.X + X0 , p.Y + Y0 , Z0 ) then Wall := True;
			end;
		end;
	end;

begin
	{ Start by updating the shadow map. This is needed for the }
	{ TILEBLOCKSLOS function. }
	UpdateShadowMap( GB );

	{ Also clear the stencil. }
	ClearStencil;

	{ Error check. }
	if not OnTheMap( X0 , Y0 ) then exit;

	{Set every square in the temp array to Maybe.}
	for x := -Max_Blast_Rating to Max_Blast_Rating do begin
		for y := -Max_Blast_Rating to Max_Blast_Rating do begin
			temp[x,y] := DBA_Maybe;
		end;
	end;

	{Set the origin to True.}
	temp[0,0] := DBA_True;

	{ If the origin blocks the blast, it will be the only tile }
	{ affected. }
	if not TileBlocksLOS( GB , X0 , Y0 , Z0 ) then begin
		{Check the 4 cardinal directions}
		CheckLine( 0,  rng );
		CheckLine( 0, -rng );
		CheckLine(  rng, 0 );
		CheckLine( -rng, 0 );

		{Check the 4 diagonal directions}
		CheckLine(rng,rng);
		CheckLine(rng,-rng);
		CheckLine(-rng,rng);
		CheckLine(-rng,-rng);

		For X := -rng + 1 to -1 do begin
			Checkline(X,-rng);
			CheckLine(X,rng);
		end;

		For X := rng -1 downto 1 do begin
			Checkline(X,-rng);
			CheckLine(X,rng);
		end;


		For Y := -rng + 1 to -1 do begin
			Checkline(rng,Y);
			CheckLine(-rng,Y);
		end;

		For Y := rng - 1 downto 1 do begin
			CheckLine(rng,Y);
			CheckLine(-rng,Y);
		end;
	end;

	{ Copy over the TEMP array into the STENCIL array. }
	for x := -Max_Blast_Rating to Max_Blast_Rating do begin
		for y := -Max_Blast_Rating to Max_Blast_Rating do begin
			if OnTheMap( X0 + X , Y0 + Y ) and ( temp[ X , Y ] = DBA_True ) and ( Range( 0 , 0 , X , Y ) <= rng ) then begin
				Stencil[ X0 + X , Y0 + Y ] := True;
			end;
		end;
	end;
end;

Procedure DoBlastAttack( GB: GameBoardPtr; Attacker: GearPtr; X0,Y0,AtOp,AMod: Integer; AtAt: String );
	{ Do a blast radius attack. }
	{ Please note that this procedure is pretty much the LOS code }
	{ ripped from DeadCold with the attack stubs sloppily patched in. }
var
	X,Y,Rng,N,TT: Integer;
	AP,OP: Point;
	msg: String;
	Mek: GearPtr;
begin
	{ First, check for deviation of the shot. }
	AP := GearCurrentLocation( Attacker );
	AP.Z := MekAltitude( GB , FindRoot( Attacker ) );
	OP.X := X0;
	OP.Y := Y0;
	OP.Z := MekAltitude( GB , FindRoot( Attacker ) );

	{ If the attacker isn't exact enough,  move the shot around. }
	rng := Range( AP.X , AP.Y , OP.X , OP.Y );
	if ( rng > 2 ) and ( RollStep( SkillValue( FindRoot( Attacker ) , AttackSkillNeeded( Attacker ) ) ) < rng ) then begin
		OP.X := OP.X + Random( 3 ) - Random( 3 );
		OP.Y := OP.Y + Random( 3 ) - Random( 3 );
	end;

	{ Start by making the initial display. }
	msg := PilotName( FindMaster( Attacker ) ) + ' fires ' + GearName( Attacker ) + '.';
	RecordAnnouncement( msg );
	Add_Shot_Precisely( GB , AP.X , AP.Y , AP.Z , OP.X , OP.Y , OP.Z );
	Inc( EFFECTS_Event_Order );

	{ Calculate the range. }
	rng := BlastRadius( GB , ATtacker , AtAt );

	{ Generate the blast stencil. }
	DrawBlastEffect( GB , OP.X , OP.Y , OP.Z , Rng );

	{ Check the created LOS array, and perform the attack in the }
	{ requested squares. }
	for x := 1 to XMax do begin
		for y := 1 to YMax do begin
			if stencil[x,y] then begin
				{ This tile gets attacked. }
				N := NumGearsXY( GB , X , Y );
				if N > 0 then begin
					for tt := 1 to N do begin
						Mek := FindGearXY( GB , X , Y , TT );
						if not Destroyed( Mek ) then AttackSingleTarget( GB , Attacker , Mek , AtOp , AMod , AtAt , False );
					end;
				end else begin
					SceneryChewing( GB, X, Y, WeaponDC( Attacker , AtOp ), False, AtAt );
				end;

				Add_Point_Animation( X, Y, OP.Z, GS_AreaAttack );
			end;
		end; { FOR Y }
	end; { FOR X }
end;

Procedure Explosion( GB: GameBoardPtr; X0,Y0,DC,R: Integer );
	{ Make an explosion using the given coordinates, damage, and radius. }
var
	N,TT,X,Y: Integer;
	AReq: EffectRequest;
begin
	ClearAttackHistory;
	{ Fill out the effect request. }
	AReq.FXType := FX_DoDamage;
	AReq.Target := Nil;
	AReq.Originator := Nil;
	AReq.Weapon := Nil;
	AReq.AF.CantCallShot := True;
	AReq.AF.CanDodge := True;
	AReq.AF.CanBlock := True;
	AReq.AF.CanParry := False;
	AReq.AF.CanResist := False;
	AReq.AF.CanIntercept := False;
	AReq.AF.CanECM := False;
	AReq.FXDice := DC;
	AReq.FXSkill := 10;
	AReq.FXMod := 0;
	AReq.FXOption := 0;
	AReq.AF.CantCallShot := True;
	AReq.AF.CanDodge := True;
	AReq.AF.CanBlock := True;
	AReq.AF.CanParry := False;
	AReq.AF.CanIntercept := False;
	AReq.AF.CanECM := False;
	AReq.AF.AtAt := 'BLAST';
	AReq.FXDesc := '';

	{ Generate the blast stencil. }
	DrawBlastEffect( GB , X0 , Y0 , TerrMan[GB^.Map[X0,Y0].terr].altitude , R );

	{ Check the created LOS array, and perform the attack in the }
	{ requested squares. }
	for X := 1 to XMax do begin
		for Y := 1 to YMax do begin
			if stencil[X,Y] then begin
				{ This tile gets attacked. }
				N := NumGearsXY( GB , X , Y );
				if N > 0 then begin
				        AReq.Tx := X;
 				        AReq.Ty := Y;

					for tt := 1 to N do begin
						AReq.Target := FindGearXY( GB , X , Y , TT );
						if not Destroyed( AReq.Target ) then ProcessAttackEffect( GB, AReq );
					end;
				end;

				SceneryChewing( GB, X, Y,AReq.FXDice, True , AReq.AF.AtAt );

				Add_Point_Animation( X,  Y, TerrMan[GB^.Map[X,Y].terr].altitude, GS_AreaAttack );
			end;
		end; { FOR Y }
	end; { FOR X }
end;

Procedure DoLineAttack( GB: GameBoardPtr; Attacker: GearPtr; X,Y,Z,AtOp,AMod: Integer; AtAt: String );
	{ This procedure will do a line attack. Such an attack starts at }
	{ firer's position and attacks every target along its line of }
	{ fire. }
var
	msg: String;
	P1,P2,P: point;
	Rng,T,N,TT: Integer;
	Mek: GearPtr;
begin
	msg := PilotName( FindMaster( Attacker ) ) + ' fires ' + GearName( Attacker ) + '.';
	RecordAnnouncement( msg );
	Inc( EFFECTS_Event_Order );

	P1 := GearCurrentLocation( Attacker );
	P1.Z := MekAltitude( GB , Attacker );
	P2.X := X;
	P2.Y := Y;
	P2.Z := Z;

	p.Z := P1.Z;

	{ Total range for the line attack is equal to 2/3 the regular }
	{ weapon range, but WeaponRange knows this. }
	rng := WeaponRange( GB , Attacker );
	T := 0;

	{ Move through each square in the range until either we are blocked }
	{ or until we have reached the maximum range. }
	{ Start by updating the shadow map. This is needed for the }
	{ TILEBLOCKSLOS function. }
	UpdateShadowMap( GB );

	while ( T < rng ) do begin
		Inc( T );
		P := SolveLine( P1.X , P1.Y , P1.Z , P2.X , P2.Y , P2.Z , T );
		if OnTheMap( P.X , P.Y ) then begin
			N := NumGearsXY( GB , P.X , P.Y );
			if N > 0 then begin
				for tt := 1 to N do begin
					Mek := FindGearXY( GB , P.X , P.Y , TT );
					if ( MekAltitude( GB , Mek ) = P.Z ) and NotDestroyed( Mek ) then AttackSingleTarget( GB , Attacker , Mek , AtOp , AMod , AtAt , False );
				end;
			end;

			Add_Point_Animation( P.X, P.Y, P.Z, GS_AreaAttack );
			if TileBlocksLOS( GB , P.X , P.Y , P.Z ) then T := rng;
		end;
	end;

	Inc( EFFECTS_Event_Order );
end;

Procedure DoSTCAttack( GB: GameBoardPtr; Attacker: GearPtr; X,Y,AtOp,AMod: Integer; AtAt: String );
	{ Perform a non-damaging STC attack. }
	{ Instead of doing damage, this attack will produce a number of STC items on the gameboard. }
var
	msg: String;
	Proto,P: GearPtr;
	X2,Y2,N,T,AtOp2,R: Integer;
	AP,OP: Point;
	SRS: NAttPtr;	{ Side Reaction Score. }
begin
	msg := PilotName( FindMaster( Attacker ) ) + ' fires ' + GearName( Attacker ) + '.';
	RecordAnnouncement( msg );

	ClearStencil;

	AP := GearCurrentLocation( Attacker );
	AP.Z := MekAltitude( GB , FindRoot( Attacker ) );
	OP.X := X;
	OP.Y := Y;
	OP.Z := MekAltitude( GB , FindRoot( Attacker ) );

	{ Depending on what other attack attributes this weapon has, fill the stencil. }
	if AtOp > 0 then begin
		Stencil[ X , Y ] := True;
		Add_Shot_Precisely( GB , AP.X , AP.Y , AP.Z , OP.X , OP.Y , OP.Z );

		for t := 1 to AtOp do begin
			X2 := X + Random( 4 ) - Random( 4 );
			Y2 := Y + Random( 4 ) - Random( 4 );
			if OnTheMap( X2 , Y2 ) then begin
				Stencil[ X2 , Y2 ] := True;
				Add_Shot_Precisely( GB , AP.X , AP.Y , AP.Z , OP.X , OP.Y , OP.Z );
			end;
		end;
	end else if HasAttackAttribute( AtAt , AA_BlastAttack ) then begin
		{ Calculate the range. }
		r := BlastRadius( GB , ATtacker , AtAt );

		{ Generate the blast stencil. }
		DrawBlastEffect( GB , X , Y , MekAltitude( GB , FindRoot( Attacker ) ) , R );

		Add_Shot_Precisely( GB , AP.X , AP.Y , AP.Z , OP.X , OP.Y , OP.Z );
	end else if HasAttackAttribute( AtAt , AA_LineAttack ) then begin
		T := 0;
		r := ( WeaponRange( GB , Attacker ) * 2 ) div 3;
		while ( T < r ) do begin
			Inc( T );
			OP := SolveLine( AP.X , AP.Y , AP.Z , X , Y , AP.Z , T );
			if OnTheMap( OP.X , OP.Y ) then begin
				Stencil[ OP.X , OP.Y ] := True;
				Add_Point_Animation( OP.X, OP.Y, OP.Z, GS_AreaAttack );
				if TileBlocksLOS( GB , OP.X , OP.Y , OP.Z ) then T := r;
			end;
		end;

	end else begin
		Stencil[ X , Y ] := True;
		Add_Shot_Precisely( GB , AP.X , AP.Y , AP.Z , OP.X , OP.Y , OP.Z );
	end;

	{ Next, actually add the items. }
	if HasAttackAttribute( AtAt , AA_Smoke ) then begin
		Proto := LoadNewSTC( 'SMOKE-1' );
		Proto^.Stat[ STAT_CloudDuration ] := Attacker^.V * 5;
		Proto^.Scale := Attacker^.Scale;
	end else if HasAttackAttribute( AtAt , AA_Gas ) then begin
		Proto := LoadNewSTC( 'GAS-1' );
		Proto^.Stat[ STAT_CloudDuration ] := Attacker^.V * 5;
		Proto^.Scale := Attacker^.Scale;
	end else if HasAttackAttribute( AtAt , AA_Drone ) then begin
		{ Step one- decide on the team for our drones! }
		R := NAttValue( FindRoot( ATtacker )^.NA , NAG_Location , NAS_Team );
		if ( R = NAV_DefPlayerTeam ) or ( R = NAV_LancemateTeam ) then begin
			T := -1;
		end else begin
			T := R;
		end;

		{ Finally, load and initialize the drone itself. }
		Proto := LoadNewSTC( 'DRONE-1' );
		Rescale( Proto , Attacker^.Scale );
		SetNAtt( Proto^.NA , NAG_Skill , 6 , Attacker^.V div 2 );
		SetNAtt( Proto^.NA , NAG_Skill , 10 , ( Attacker^.V + 1 ) div 2 );
		SetNAtt( Proto^.NA , NAG_Location , NAS_Team , T );
		GearUp( Proto );
	end else Exit;

	{ Error check }
	if Proto = Nil then Exit;

	for X := 1 to XMax do begin
		for Y := 1 to YMax do begin
			if Stencil[ X , Y ] then begin
				P := CloneGear( Proto );
				SetNAtt( P^.NA , NAG_Location , NAS_X , X );
				SetNAtt( P^.NA , NAG_Location , NAS_Y , Y );
				SetNAtt( P^.NA , NAG_Location , NAS_D , Random( 8 ) );
				AppendGear( GB^.Meks , P );
				SetNAtt( P^.NA , NAG_EpisodeData, NAS_UID, MaxIdTag( GB^.Meks , NAG_EpisodeData, NAS_UID ) + 1 );
			end;
		end;
	end;

	DisposeGear( Proto );
	Inc( EFFECTS_Event_Order );
end;


Procedure PostAttackCleanup( GB: GameBoardPtr; Attacker: GearPtr; TX,TY,TZ: Integer );
	{ Deal with whatever needs to be dealt with. }
var
	Master,Engine: GearPtr;
	P: Point;
	OverLoad,T: Integer;
begin
	Master := FindRoot( Attacker );
	P.X := TX;
	P.Y := TY;

	{ If MASTER is a mecha, deal with OVERLOAD here. }
	if ( Master <> Nil ) and ( Master^.G = GG_Mecha ) and ( Attacker^.G = GG_Weapon ) then begin
		if ( Attacker^.S = GS_EMelee ) or ( Attacker^.S = GS_BeamGun ) then begin
			OverLoad := Attacker^.V + Attacker^.Stat[ STAT_BurstValue ];
			if HasAttackAttribute( WeaponAttackAttributes( Attacker ) , AA_Hyper ) then OverLoad := OverLoad * 3;
			if HasAreaEffect( Attacker ) then OverLoad := OverLoad * 2;
			if Master^.Scale > Attacker^.Scale then for t := 1 to ( Master^.Scale - Attacker^.Scale ) do Overload := Overload div 4;
			Engine := SeekGear( Master , GG_Support , GS_Engine );
			if ( Engine <> Nil ) and ( Engine^.Stat[ STAT_EngineSubType ] = EST_HighOutput ) then Overload := Overload div 2;
			OverLoad := OverLoad  - Master^.V;
			if OverLoad < 1 then OverLoad := 1;
			AddNAtt( Master^.NA , NAG_Condition , NAS_Overload , OverLoad );
		end;
	end;

	if HasAttackAttribute( WeaponAttackAttributes( Attacker ) , AA_STRAIN ) and ( Master <> Nil ) then AddStaminaDown( Master , 10 );
	if HasAttackAttribute( WeaponAttackAttributes( Attacker ) , AA_COMPLEX ) and ( Master <> Nil ) then AddMentalDown( Master , 10 );

	{ If the weapon was thrown, deal with that here. }
	if MustBeThrown( GB , Master , Attacker , P.X , P.Y ) then begin
		if Attacker^.G = GG_Ammo then begin
			{ Lower the ammo count. }
			AddNAtt( Attacker^.NA , NAG_WeaponModifier , NAS_AmmoSpent , 1 );
			if ( Attacker^.Stat[STAT_AmmoPresent] - NAttValue( Attacker^.NA , NAG_WeaponModifier , NAS_AmmoSpent ) ) < 1 then begin
				if IsInvCom( Attacker ) then begin
					RemoveGear( Attacker^.Parent^.InvCom , Attacker );
				end else if IsSubCom( Attacker ) then begin
					RemoveGear( Attacker^.Parent^.SubCom , Attacker );
				end;
			end;

		end else if not HasAttackAttribute( WeaponAttackAttributes( Attacker ) , AA_Returning ) then begin
			if IsInvCom( Attacker ) then begin
				DelinkGear( Attacker^.Parent^.InvCom , Attacker );
				AppendGear( GB^.Meks , Attacker );
				SetNAtt( Attacker^.NA , NAG_Location , NAS_X , P.X );
				SetNAtt( Attacker^.NA , NAG_Location , NAS_Y , P.Y );
				SetNAtt( Attacker^.NA , NAG_Location , NAS_Team , NAttValue( Master^.NA , NAG_Location , NAS_Team ) );

			end else if IsSubCom( Attacker ) then begin
				DelinkGear( Attacker^.Parent^.SubCom , Attacker );
				AppendGear( GB^.Meks , Attacker );
				SetNAtt( Attacker^.NA , NAG_Location , NAS_X , P.X );
				SetNAtt( Attacker^.NA , NAG_Location , NAS_Y , P.Y );
				SetNAtt( Attacker^.NA , NAG_Location , NAS_Team , NAttValue( Master^.NA , NAG_Location , NAS_Team ) );

			end;
		end else begin
			Inc( EFFECTS_Event_Order );
			Add_Shot_Precisely( GB , TX , TY , TZ , NAttValue( Master^.NA , NAG_Location , NAS_X ) , NAttValue( Master^.NA , NAG_Location , NAS_Y ) , MekALtitude( GB , Master ) );
		end;

	end;
end;


Procedure DoAttack( GB: GameBoardPtr; Attacker,Target: GearPtr; X,Y,Z,AtOp,AMod: Integer);
	{ ATTACKER is attacking TARGET. }
	{ Attacker points to the exact gear which is doing the attack- }
	{ generally, either a weapon or a module. Target will usually be }
	{ a root level gear, unless the attacker is making a called shot. }
	{ ATOP: Attack Options. }
	{ AMod: Attack Modifier. }
var
	AtAt: String;
	Master: GearPtr;
begin
	ClearAttackHistory;

	{ Attack attributes must be determined before the attack is cleared, }
	{ since if the attack uses the last bullet in a projectile/missile }
	{ weapon's magazine then the ammo AtAt's won't be properly recognized. }
	AtAt := WeaponAttackAttributes( Attacker );

	{ Clear the weapon for usage. }
	if ClearAttack( GB , Attacker , AtOp ) then begin
		if Target <> Nil then begin
			X := NAttValue( FindRoot( Target )^.NA , NAG_LOcation , NAS_X );
			Y := NAttValue( FindRoot( Target )^.NA , NAG_LOcation , NAS_Y );
			Z := MekAltitude( GB , FindRoot( Target ) );
		end;

		if NonDamagingAttack( AtAt ) then begin
			DoSTCAttack( GB , Attacker , X , Y , AtOp , AMod , AtAt );
		end else if HasAttackAttribute( AtAt , AA_SwarmAttack ) then begin
			DoSwarmAttack( GB , Attacker , X , Y , AtOp , AMod , AtAt );
		end else if HasAttackAttribute( AtAt , AA_BlastAttack ) then begin
			DoBlastAttack( GB , Attacker , X , Y , AtOp , AMod , AtAt );
		end else if HasAttackAttribute( AtAt , AA_LineAttack ) then begin
			DoLineAttack( GB , Attacker , X , Y , Z , AtOp , AMod , AtAt );
		end else begin
			DoDirectFireAttack( GB , Attacker , Target , X , Y , Z , AtOp , AMod , AtAt );
		end;

		Master := FindRoot( Attacker );
		if Master <> Nil then begin
			{ Set the calltime for the next attack. }
			SetNAtt( Master^.NA , NAG_Action , NAS_CallTime , GB^.ComTime + ReactionTime( Master ) );

			{ Update the alleigances of everyone involved. }
			if Target <> Nil then DeclarationOfHostilities( GB , NAttValue( Master^.NA , NAG_Location , NAS_Team ) , NAttValue( FindRoot( Target )^.NA , NAG_Location , NAS_Team ) );
		end;

		{ Perform cleanup duties. }
		PostAttackCleanup( GB , Attacker , X , Y , Z );
	end else begin
		ATTACK_Error := True;
	end;
end;

Procedure HandleEffectString( GB: GameBoardPtr; Target: GearPtr; FX_String,FX_Desc: String );
	{ An effect has been triggered. Do the required operations, then }
	{ store the effects in the effect list. }
var
	EReq: EffectRequest;
begin
	ClearAttackHistory;

	EReq.FXType := ExtractValue( FX_String );
	EReq.Originator := Nil;
	EReq.Weapon := Nil;
	EReq.Target := Target;
	EReq.TX := NAttValue( FindRoot( Target )^.NA , NAG_Location , NAS_X );
	EReq.TY := NAttValue( FindRoot( Target )^.NA , NAG_Location , NAS_Y );
	EReq.FXDice := ExtractValue( FX_String );
	EReq.FXOption := ExtractValue( FX_String );
	EReq.FXSkill := ExtractValue( FX_String );
	EReq.FXMod := 0;
	EReq.AF.CantCallShot := AStringHasBString( FX_String , 'CANTCALLSHOT' );
	EReq.AF.CanDodge := AStringHasBString( FX_String , 'CanDodge' );
	EReq.AF.CanBlock := AStringHasBString( FX_String , 'CanBlock' );
	EReq.AF.CanParry := AStringHasBString( FX_String , 'CanParry' );
	EReq.AF.CanResist := AStringHasBString( FX_String , 'CanResist' );
	EReq.AF.CanIntercept := AStringHasBString( FX_String , 'CanIntercept' );
	EReq.AF.CanECM := AStringHasBString( FX_String , 'CanECM' );
	EReq.AF.AtAt := FX_String;
	EReq.FXDesc := FX_Desc;

	InvokeEffect( GB , EReq );
end;


initialization
	{ Set the history list to 0, for now. }
	ATTACK_History := Nil;
	EFFECTS_Event_Order := 0;

	FX_Messages := LoadStringList( Effects_Message_File );

finalization
	DisposeSAtt( FX_Messages );
	DisposeSAtt( ATTACK_History );

end.