aboutsummaryrefslogtreecommitdiff
path: root/Northstar.Custom/mod/scripts/vscripts/gamemodes/_gamemode_fw.nut
blob: b025ff0a14e638ac4c621b026bafc1a551250c68 (plain)
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
untyped
global function GamemodeFW_Init

// spawn points
global function RateSpawnpointsPilot_FW
global function RateSpawnpointsTitan_FW
//global function RateSpawnpoints_FW

// for battery_port.gnut to work
global function FW_ReplaceMegaTurret

// fw specific titanfalls
global function FW_IsPlayerInFriendlyTerritory
global function FW_IsPlayerInEnemyTerritory

// you need to deal this much damage to trigger "FortWarTowerDamage" score event
const int FW_HARVESTER_DAMAGE_SEGMENT = 1500

// basically needs to match "waves count - bosswaves count"
const int FW_MAX_LEVELS = 3

// to confirm it's a npc from camps..
const string FW_NPC_SCRIPTNAME = "fw_npcsFromCamp"
const int FW_AI_TEAM = TEAM_BOTH
const float WAVE_STATE_TRANSITION_TIME = 5.0

// from sh_gamemode_fw, if half of these npcs cleared in one camp, it gets escalate
const int FW_GRUNT_COUNT = 36//32
const int FW_SPECTRE_COUNT = 24
const int FW_REAPER_COUNT = 2

// max deployment each camp
const int FW_GRUNT_MAX_DEPLOYED = 8
const int FW_SPECTRE_MAX_DEPLOYED = 8
const int FW_REAPER_MAX_DEPLOYED = 1

// if other camps been cleaned many times, we levelDown
const int FW_CAMP_IGNORE_NEEDED = 2

// debounce for showing damaged infos
const float FW_HARVESTER_DAMAGED_DEBOUNCE = 5.0
const float FW_TURRET_DAMAGED_DEBOUNCE = 2.0

global HarvesterStruct& fw_harvesterMlt
global HarvesterStruct& fw_harvesterImc

// these are not using respawn's remaining code( sh_gamemode_fw.nut )!

// respawn already have a FW_TowerData struct! this struct is only for score events
struct HarvesterDamageStruct
{
	float recentDamageTime
	int storedDamage
}

struct TurretSiteStruct
{
	entity site
	entity turret
	entity minimapstate
	string turretflagid
}

// respawn already have a FW_CampData, FW_WaveOrigin and FW_SpawnData struct!
struct CampSiteStruct
{
	entity camp
	entity info
	entity tracker
	array<entity> validDropPodSpawns
	array<entity> validTitanSpawns
	string campId // "A", "B", "C"
	int npcsAlive
	int ignoredSinceLastClean
}

struct CampSpawnStruct
{
	string spawnContent // what npcs to spawn
	int maxSpawnCount // max spawn count on this camp
	int countPerSpawn // how many npcs to deploy per spawn, for droppods most be 4
	int killsToEscalate // how many kills needed to escalate
}

struct
{
	array<HarvesterStruct> harvesters

	// save camp's info_target, we spawn camps after game starts, or player's first life won't show up correct camp icons
	array<entity> camps

	array<entity> fwTerritories

	array<TurretSiteStruct> turretsites

	array<CampSiteStruct> fwCampSites

	// respawn already have a FW_TowerData struct! this table is only for score events
	table< entity, HarvesterDamageStruct > playerDamageHarvester // team, table< player, time >
    
	// this is for saving territory's connecting time, try not to make faction dialogues play together
	table< int, float > teamTerrLastConnectTime // team, time
	
	// unused
	array<entity> etitaninmlt
	array<entity> etitaninimc

	entity harvesterMlt_info
	entity harvesterImc_info

	table<int, CampSpawnStruct> fwNpcLevel // basically use to powerup certian camp, sync with alertLevel
	table< string, table< string, int > > trackedCampNPCSpawns
}file

void function GamemodeFW_Init()
{
	// _battery_port.gnut needs this
	RegisterSignal( "BatteryActivate" )

	AiGameModes_SetGruntWeapons( [ "mp_weapon_rspn101", "mp_weapon_dmr", "mp_weapon_r97", "mp_weapon_lmg" ] )
	AiGameModes_SetSpectreWeapons( [ "mp_weapon_hemlok_smg", "mp_weapon_doubletake", "mp_weapon_mastiff" ] )

	AddCallback_EntitiesDidLoad( LoadEntities )
	AddCallback_GameStateEnter( eGameState.Prematch, OnFWGamePrematch )
	AddCallback_GameStateEnter( eGameState.Playing, OnFWGamePlaying )

	AddSpawnCallback( "item_powerup", FWAddPowerUpIcon )
	AddSpawnCallback( "npc_turret_mega", OnFWTurretSpawned )

	AddCallback_OnClientConnected( OnFWPlayerConnected )
	AddCallback_PlayerClassChanged( OnFWPlayerClassChanged )
	AddCallback_OnPlayerKilled( OnFWPlayerKilled )
	AddCallback_OnPilotBecomesTitan( OnFWPilotBecomesTitan )
	AddCallback_OnTitanBecomesPilot( OnFWTitanBecomesPilot )

	ScoreEvent_SetupEarnMeterValuesForMixedModes()
	SetRecalculateRespawnAsTitanStartPointCallback( FW_ForcedTitanStartPoint )
	SetRecalculateTitanReplacementPointCallback( FW_ReCalculateTitanReplacementPoint )
	SetRequestTitanAllowedCallback( FW_RequestTitanAllowed )
}



//////////////////////////
///// HACK FUNCTIONS /////
//////////////////////////

const array<string> HACK_CLEANUP_MAPS =
[
	"mp_grave",
	"mp_homestead",
	"mp_complex3"
]

//if npcs outside the map try to fire( like in death animation ), it will cause a engine error

// in mp_grave, npcs will sometimes stuck underground
const float GRAVE_CHECK_HEIGHT = 1700 // the map's lowest ground is 1950+, npcs will stuck under -4000 or -400
// in mp_homestead, npcs will sometimes stuck in the sky
const float HOMESTEAD_CHECK_HIEGHT = 8000 // the map's highest part is 7868+, npcs will stuck above 13800+
// in mp_complex3, npcs will sometimes stuck in the sky
const float COMPLEX_CHECK_HEIGHT = 7000 // the map's highest part is 6716+, npcs will stuck above 9700+

// do a hack
void function HACK_ForceDestroyNPCs()
{
	thread HACK_ForceDestroyNPCs_Threaded()
}

void function HACK_ForceDestroyNPCs_Threaded()
{
	string mapName = GetMapName()
	if( !( HACK_CLEANUP_MAPS.contains( mapName ) ) )
		return

	while( true )
	{
		if( mapName == "mp_grave" )
		{
			foreach( entity npc in GetNPCArray() )
			{
				if( npc.GetOrigin().z <= GRAVE_CHECK_HEIGHT )
				{
					npc.ClearParent()
					npc.Destroy()
				}
			}
		}
		if( mapName == "mp_homestead" )
		{
			foreach( entity npc in GetNPCArray() )
			{
				// neither spawning from droppod nor hotdropping
				if( !IsValid( npc.GetParent() ) && !npc.e.isHotDropping )
				{
					if( npc.GetOrigin().z >= HOMESTEAD_CHECK_HIEGHT )
					{
						npc.Destroy()
					}
				}
			}
		}
		if( mapName == "mp_complex3" )
		{
			foreach( entity npc in GetNPCArray() )
			{
				// neither spawning from droppod nor hotdropping
				if( !IsValid( npc.GetParent() ) && !npc.e.isHotDropping )
				{
					if( npc.GetOrigin().z >= COMPLEX_CHECK_HEIGHT )
					{
						npc.Destroy()
					}
				}
			}
		}
		WaitFrame()
	}
}

//////////////////////////////
///// HACK FUNCTIONS END /////
//////////////////////////////



////////////////////////////////
///// SPAWNPOINT FUNCTIONS /////
////////////////////////////////

void function RateSpawnpointsPilot_FW( int checkClass, array<entity> spawnpoints, int team, entity player )
{
	array<entity> startSpawns = SpawnPoints_GetPilotStart( team )
	RateSpawnpoints_FW( startSpawns, checkClass, spawnpoints, team, player )
}

void function RateSpawnpointsTitan_FW( int checkClass, array<entity> spawnpoints, int team, entity player )
{
	array<entity> startSpawns = SpawnPoints_GetTitanStart( team )
	RateSpawnpoints_FW( startSpawns, checkClass, spawnpoints, team, player )
}

void function RateSpawnpoints_FW( array<entity> startSpawns, int checkClass, array<entity> spawnpoints, int team, entity player )
{
	if ( HasSwitchedSides() )
		team = GetOtherTeam( team )

	// average out startspawn positions
	vector averageFriendlySpawns
	foreach ( entity spawnpoint in startSpawns )
		averageFriendlySpawns += spawnpoint.GetOrigin()

	averageFriendlySpawns /= startSpawns.len()

	entity friendlyTerritory
	foreach ( entity territory in file.fwTerritories )
	{
		if ( team == territory.GetTeam() )
		{
			friendlyTerritory = territory
			break
		}
	}

	vector ratingPos
	if ( IsValid( friendlyTerritory ) )
		ratingPos = friendlyTerritory.GetOrigin()
	else
		ratingPos = averageFriendlySpawns

	foreach ( entity spawnpoint in spawnpoints )
	{
		// idk about magic number here really
		float rating = 1.0 - ( Distance2D( spawnpoint.GetOrigin(), ratingPos ) / 1000.0 )
		spawnpoint.CalculateRating( checkClass, player.GetTeam(), rating, rating )
	}
}

////////////////////////////////////
///// SPAWNPOINT FUNCTIONS END /////
////////////////////////////////////



//////////////////////////////
///// CALLBACK FUNCTIONS /////
//////////////////////////////

void function OnFWGamePrematch()
{
	InitFWScoreEvents()
	FW_createHarvester()
	InitFWCampSites()
	InitCampSpawnerLevel()
}

void function OnFWGamePlaying()
{
	startFWHarvester()
	FWAreaThreatLevelThink()
	StartFWCampThink()
	InitTurretSettings()
	FWPlayerObjectiveState()

	HACK_ForceDestroyNPCs()
}

void function OnFWPlayerConnected( entity player )
{
	InitFWPlayers( player )
}

void function OnFWPlayerClassChanged( entity player )
{
    // give player a friendly highlight
    Highlight_SetFriendlyHighlight( player, "fw_friendly" )
}

void function OnFWPlayerKilled( entity victim, entity attacker, var damageInfo )
{
	HandleFWPlayerKilledScoreEvent( victim, attacker )
}

void function OnFWPilotBecomesTitan( entity player, entity titan )
{
	// objective stuff
	SetTitanObjective( player, titan )
}

void function OnFWTitanBecomesPilot( entity player, entity titan )
{
	// objective stuff
	SetPilotObjective( player, titan )
}

//////////////////////////////////
///// CALLBACK FUNCTIONS END /////
//////////////////////////////////


/////////////////////////////////
///// SCORE EVENT FUNCTIONS /////
/////////////////////////////////

void function InitFWScoreEvents()
{
	// common scoreEvents
	ScoreEvent_SetEarnMeterValues( "KillHeavyTurret", 0.0, 0.20 ) // can only adds to titan's in this mode

	// fw special: save for later use of scoreEvents

	// combat
	ScoreEvent_SetEarnMeterValues( "FortWarAssault", 0.0, 0.05, 0.0 ) // titans don't earn
	ScoreEvent_SetEarnMeterValues( "FortWarDefense", 0.0, 0.05, 0.0 ) // titans don't earn
	ScoreEvent_SetEarnMeterValues( "FortWarPerimeterDefense", 0.0, 0.05 ) // unused
	ScoreEvent_SetEarnMeterValues( "FortWarSiege", 0.0, 0.05 ) // unused
	ScoreEvent_SetEarnMeterValues( "FortWarSnipe", 0.0, 0.05 ) // unused

	// constructions
	ScoreEvent_SetEarnMeterValues( "FortWarBaseConstruction", 0.0, 0.15 )
	ScoreEvent_SetEarnMeterValues( "FortWarForwardConstruction", 0.0, 0.15 )
	ScoreEvent_SetEarnMeterValues( "FortWarInvasiveConstruction", 0.0, 0.25 ) // unused
	ScoreEvent_SetEarnMeterValues( "FortWarResourceDenial", 0.0, 0.05 ) // unused
	ScoreEvent_SetEarnMeterValues( "FortWarSecuringGatheredResources", 0.0, 0.05 ) // unused

	// tower
	ScoreEvent_SetEarnMeterValues( "FortWarTowerDamage", 0.0, 0.10, 0.0 ) // using the const FW_HARVESTER_DAMAGE_SEGMENT, titans don't earn
	ScoreEvent_SetEarnMeterValues( "FortWarTowerDefense", 0.0, 0.10, 0.0 ) // titans don't earn
	ScoreEvent_SetEarnMeterValues( "FortWarShieldDestroyed", 0.0, 0.15 )

	// turrets
	ScoreEvent_SetEarnMeterValues( "FortWarTeamTurretControlBonus_One", 0.0, 0.15, 0.5 ) // give more meter if no turret left
	ScoreEvent_SetEarnMeterValues( "FortWarTeamTurretControlBonus_Two", 0.0, 0.15, 0.5 )
	ScoreEvent_SetEarnMeterValues( "FortWarTeamTurretControlBonus_Three", 0.0, 0.10, 0.5 )
	ScoreEvent_SetEarnMeterValues( "FortWarTeamTurretControlBonus_Four", 0.0, 0.10, 0.5 ) // give less meter if controlled most turrets
	ScoreEvent_SetEarnMeterValues( "FortWarTeamTurretControlBonus_Five", 0.0, 0.05, 0.5 )
	ScoreEvent_SetEarnMeterValues( "FortWarTeamTurretControlBonus_Six", 0.0, 0.05, 0.5 )
}

// consider this means victim recently damaged harvester
const float TOWER_DEFENSE_REQURED_TIME = 10.0

void function HandleFWPlayerKilledScoreEvent( entity victim, entity attacker )
{
	// this function only handles player's kills
	if( !attacker.IsPlayer() )
		return
    
	// suicide don't get scores
	if( attacker == victim )
		return

	int attackerTeam = attacker.GetTeam()
	int victimTeam = victim.GetTeam()

	string scoreEvent = ""
	int secondaryScore = 0
	entity attackerHarvester = FW_GetTeamHarvesterProp( attackerTeam )

	if( FW_IsPlayerInEnemyTerritory( victim ) ) // victim is in enemy territory
	{
		scoreEvent = "FortWarDefense" // enemy earn score from defense
		secondaryScore = POINTVALUE_FW_DEFENSE
	}

	if( FW_IsPlayerInFriendlyTerritory( victim ) ) // victim is in friendly territory
	{
		scoreEvent = "FortWarAssault" // enemy earn score from assault
		secondaryScore = POINTVALUE_FW_ASSAULT
	}

	if( victim in file.playerDamageHarvester ) // victim has damaged the harvester this life
	{    
		float damageTime = file.playerDamageHarvester[ victim ].recentDamageTime

		// is victim recently damaged havester?
		if( damageTime + TOWER_DEFENSE_REQURED_TIME >= Time() )
		{
			scoreEvent = "FortWarTowerDefense" // you defend the tower!
			secondaryScore = POINTVALUE_FW_TOWER_DEFENSE
		}

	}

	if( scoreEvent != "" )
	{
		AddPlayerScore( attacker, scoreEvent, victim )
		attacker.AddToPlayerGameStat( PGS_DEFENSE_SCORE, secondaryScore )
	}
}

/////////////////////////////////////
///// SCORE EVENT FUNCTIONS END /////
/////////////////////////////////////



//////////////////////////////////////
///// FACTION DIALOGUE FUNCTIONS /////
//////////////////////////////////////

const float FW_TERRYTORY_DIALOGUE_DEBOUNCE = 5.0

// WORKING IN PROGRESS
bool function TryFWTerritoryDialogue( entity territory, entity player )
{
	bool thisTimeIsTitan = player.IsTitan()
	int terrTeam = territory.GetTeam()
	int enemyTeam = GetOtherTeam( terrTeam )
	bool sameTeam = terrTeam == player.GetTeam()
	bool isInDebounce = file.teamTerrLastConnectTime[ terrTeam ] + FW_TERRYTORY_DIALOGUE_DEBOUNCE >= Time()

	// the territory trigger will only save players and titans
	array<entity> allEntsInside = GetAllEntitiesInTrigger( territory )
	allEntsInside.removebyvalue( null ) // since we're using a fake trigger, need to check this
	array<entity> friendliesInside // this means territory's friendly team
	array<entity> enemiesInside // this means territory's enemy team
	array<entity> enemyTitansInside
	foreach( entity ent in allEntsInside )
	{
		if( !IsValid( ent ) ) // since we're using a fake trigger, need to check this
			continue
		if( ent.GetTeam() == terrTeam )
			friendliesInside.append( ent )
	}
	foreach( entity ent in allEntsInside )
	{
		if( !IsValid( ent ) ) // since we're using a fake trigger, need to check this
			continue
		if( ent.GetTeam() != terrTeam )
			enemiesInside.append( ent )
	}
	foreach( entity enemy in enemiesInside )
	{
		if( !IsValid( enemy ) ) // since we're using a fake trigger, need to check this
			continue
		if( enemy.IsTitan() )
			enemyTitansInside.append( enemy )
	}

	print( "enemy in territory: " + string( enemiesInside.len() ) )
	print( "friendly in territory: " + string( friendliesInside.len() ) )

	print( "sameTeam: " + string( sameTeam ) )
	print( "isInDebounce: " + string( isInDebounce ) )
	print( "thisTimeIsTitan: " + string( thisTimeIsTitan ) )

	if( enemiesInside.len() > 3 || friendliesInside.len() > 1 ) // already have some players triggered dialogue
		return false

	// notify player enemy's behaves
	if( !sameTeam ) // player is not the same team as territory
	{
		// consider this means all enemies has left friendly territory, should use a debounce
		if( enemiesInside.len() == 0 && !isInDebounce )
		{
			PlayFactionDialogueToTeam( "fortwar_terEnemyExpelled", terrTeam )
			return true
		}
		// has more than 3 titans inside including new one, ignores debounce
		else if( enemyTitansInside.len() >= 3 && thisTimeIsTitan )
		{
			PlayFactionDialogueToTeam( "fortwar_terPresentEnemyTitans", terrTeam )
			return true
		}
		// only the player inside terrytory
		else if( enemyTitansInside.len() == 1 )
		{
			// entered territory as titan, ignores debounce
			if( thisTimeIsTitan )
			{
				PlayFactionDialogueToTeam( "fortwar_terEnteredEnemyPilot", terrTeam )
				return true
			}
			// entered territory as pilot
			else if( !isInDebounce )
			{
				PlayFactionDialogueToTeam( "fortwar_terEnteredEnemyPilot", terrTeam )
				return true
			}
		}

		// notify player friendly's behaves
		// consider this means all friendlies has left enemy territory
		if( friendliesInside.len() == 0 && !sameTeam && !isInDebounce )
		{
			PlayFactionDialogueToTeam( "fortwar_terFriendlyExpelled", terrTeam )
			return true
		}
	}

	return false
}

//////////////////////////////////////////
///// FACTION DIALOGUE FUNCTIONS END /////
//////////////////////////////////////////



/////////////////////////////////////////
///// GAMEMODE INITIALIZE FUNCTIONS /////
/////////////////////////////////////////

void function LoadEntities()
{
	// info_target
	foreach ( entity info_target in GetEntArrayByClass_Expensive( "info_target" ) )
	{
		if( info_target.HasKey( "editorclass" ) )
		{
			switch( info_target.kv.editorclass )
			{
				case "info_fw_team_tower":
					if ( info_target.GetTeam() == TEAM_IMC )
					{
						file.harvesterImc_info = info_target
						//print("fw_tower tracker spawned")
					}
					if ( info_target.GetTeam() == TEAM_MILITIA )
					{
						file.harvesterMlt_info = info_target
						//print("fw_tower tracker spawned")
					}
					break
				case "info_fw_camp":
					file.camps.append( info_target )
					//InitCampTracker( info_target )
					//print("fw_camp spawned")
					break
				case "info_fw_turret_site":
					string idString = expect string(info_target.kv.turretId)
					int id = int( info_target.kv.turretId )
					//print("info_fw_turret_siteID : " + idString )

					// set this for replace function to find
					TurretSiteStruct turretsite
					file.turretsites.append( turretsite )

					turretsite.site = info_target

					// create turret, spawn with no team and set it after game starts
					entity turret = CreateNPC( "npc_turret_mega", TEAM_UNASSIGNED, info_target.GetOrigin(), info_target.GetAngles() )
					SetSpawnOption_AISettings( turret, "npc_turret_mega_fortwar" )
					DispatchSpawn( turret )

					turretsite.turret = turret

					// init turret settings
					turret.s.minimapstate <- null               // entity, for saving turret's minimap handler
					turret.s.baseTurret <- false                // bool, is this turret from base
					turret.s.turretflagid <- ""                 // string, turret's id like "1", "2", "3"
					turret.s.lastDamagedTime <- 0.0             // float, for showing turret underattack icons
					turret.s.relatedBatteryPort <- null         // entity, corssfile

					// minimap icons holder
					entity minimapstate = CreateEntity( "prop_script" )
					minimapstate.SetValueForModelKey( info_target.GetModelName() ) // these info must have model to work
					minimapstate.Hide() // hide the model! it will still work on minimaps
					minimapstate.SetOrigin( info_target.GetOrigin() )
					minimapstate.SetAngles( info_target.GetAngles() )
					//SetTeam( minimapstate, info_target.GetTeam() ) // setTeam() for icons is done in TurretStateWatcher()
					minimapstate.kv.solid = SOLID_VPHYSICS
					DispatchSpawn( minimapstate )
					// show on minimaps
					minimapstate.Minimap_AlwaysShow( TEAM_IMC, null )
					minimapstate.Minimap_AlwaysShow( TEAM_MILITIA, null )
					minimapstate.Minimap_SetCustomState( eMinimapObject_prop_script.FW_BUILDSITE_SHIELDED )

					turretsite.minimapstate = minimapstate
					turret.s.minimapstate = minimapstate

					break
			}
		}
	}

	// script_ref
	foreach ( entity script_ref in GetEntArrayByClass_Expensive( "script_ref" ) )
	{
		if( script_ref.HasKey( "editorclass" ) )
		{
			switch( script_ref.kv.editorclass )
			{
				case "info_fw_foundation_plate":
					entity prop = CreatePropScript( script_ref.GetModelName(), script_ref.GetOrigin(), script_ref.GetAngles(), 6 )
					break
				case "info_fw_battery_port":
					entity batteryPort = CreatePropScript( script_ref.GetModelName(), script_ref.GetOrigin(), script_ref.GetAngles(), 6 )
					FW_InitBatteryPort(batteryPort)

					break
			}
		}
	}

	// trigger_multiple
	foreach ( entity trigger_multiple in GetEntArrayByClass_Expensive( "trigger_multiple" ) )
	{
		if( trigger_multiple.HasKey( "editorclass" ) )
		{
			switch( trigger_multiple.kv.editorclass )
			{
				case "trigger_fw_territory":
				SetupFWTerritoryTrigger( trigger_multiple )
				break
			}
		}
	}

	// maybe for tick_spawning reapers?
	ValidateAndFinalizePendingStationaryPositions()
}

void function InitCampSpawnerLevel() // can edit this to make more spawns, alertLevel icons supports max to lv3( 0,1,2 )
{
	// lv1 spawns: grunts
	CampSpawnStruct campSpawnLv1
	campSpawnLv1.spawnContent = "npc_soldier"
	campSpawnLv1.maxSpawnCount = FW_GRUNT_MAX_DEPLOYED
	campSpawnLv1.countPerSpawn = 4 // how many npcs to deploy per spawn, for droppods most be 4
	campSpawnLv1.killsToEscalate = FW_GRUNT_COUNT / 2

	file.fwNpcLevel[0] <- campSpawnLv1

	// lv2 spawns: spectres
	CampSpawnStruct campSpawnLv2
	campSpawnLv2.spawnContent = "npc_spectre"
	campSpawnLv2.maxSpawnCount = FW_SPECTRE_MAX_DEPLOYED
	campSpawnLv2.countPerSpawn = 4 // how many npcs to deploy per spawn, for droppods most be 4
	campSpawnLv2.killsToEscalate = FW_SPECTRE_COUNT / 2

	file.fwNpcLevel[1] <- campSpawnLv2

	// lv3 spawns: reapers
	CampSpawnStruct campSpawnLv3
	campSpawnLv3.spawnContent = "npc_super_spectre"
	campSpawnLv3.maxSpawnCount = FW_REAPER_MAX_DEPLOYED
	campSpawnLv3.countPerSpawn = 1 // how many npcs to deploy per spawn
	campSpawnLv3.killsToEscalate = FW_REAPER_COUNT / 2 // only 1 kill needed to spawn the boss?

	file.fwNpcLevel[2] <- campSpawnLv3
}

/////////////////////////////////////////////
///// GAMEMODE INITIALIZE FUNCTIONS END /////
/////////////////////////////////////////////



///////////////////////////////////////
///// PLAYER INITIALIZE FUNCTIONS /////
///////////////////////////////////////

void function InitFWPlayers( entity player )
{
	HarvesterDamageStruct emptyStruct
	file.playerDamageHarvester[ player ] <- emptyStruct

	// objective stuff
	player.s.notifiedTitanfall <- false

	// notification stuff
	player.s.lastTurretNotifyTime <- 0.0
}

///////////////////////////////////////////
///// PLAYER INITIALIZE FUNCTIONS END /////
///////////////////////////////////////////



/////////////////////////////
///// POWERUP FUNCTIONS /////
/////////////////////////////

void function FWAddPowerUpIcon( entity powerup )
{
	powerup.Minimap_SetAlignUpright( true )
	powerup.Minimap_SetZOrder( MINIMAP_Z_OBJECT )
	powerup.Minimap_SetClampToEdge( false )
	powerup.Minimap_AlwaysShow( TEAM_MILITIA, null )
	powerup.Minimap_AlwaysShow( TEAM_IMC, null )
}

/////////////////////////////////
///// POWERUP FUNCTIONS END /////
/////////////////////////////////



/////////////////////////////
///// AICAMPS FUNCTIONS /////
/////////////////////////////

void function InitFWCampSites()
{
	// init here
	foreach( entity info_target in file.camps )
	{
		InitCampTracker( info_target )
	}

	// camps don't have a id, set them manually
	foreach( int index, CampSiteStruct campsite in file.fwCampSites )
	{
		entity campInfo = campsite.camp
		float radius = float( campInfo.kv.radius )

		// get droppod spawns
		foreach ( entity spawnpoint in SpawnPoints_GetDropPod() )
			if ( Distance( campInfo.GetOrigin(), spawnpoint.GetOrigin() ) < radius )
				campsite.validDropPodSpawns.append( spawnpoint )

		// get titan spawns
		foreach ( entity spawnpoint in SpawnPoints_GetTitan() )
			if ( Distance( campInfo.GetOrigin(), spawnpoint.GetOrigin() ) < radius )
				campsite.validTitanSpawns.append( spawnpoint )

		if ( index == 0 )
		{
			campsite.campId = "A"
			SetGlobalNetInt( "fwCampAlertA", 0 )
			SetGlobalNetFloat( "fwCampStressA", 0.0 ) // start from empty
			SetLocationTrackerID( campsite.tracker, 0 )
			file.trackedCampNPCSpawns["A"] <- {}
			continue
		}
		if ( index == 1 )
		{
			campsite.campId = "B"
			SetGlobalNetInt( "fwCampAlertB", 0 )
			SetGlobalNetFloat( "fwCampStressB", 0.0 ) // start from empty
			SetLocationTrackerID( campsite.tracker, 1 )
			file.trackedCampNPCSpawns["B"] <- {}
			continue
		}
		if ( index == 2 )
		{
			campsite.campId = "C"
			SetGlobalNetInt( "fwCampAlertC", 0 )
			SetGlobalNetFloat( "fwCampStressC", 0.0 ) // start from empty
			SetLocationTrackerID( campsite.tracker, 2 )
			file.trackedCampNPCSpawns["C"] <- {}
			continue
		}
	}
}

void function InitCampTracker( entity camp )
{
	//print("InitCampTracker")
	CampSiteStruct campsite
	campsite.camp = camp
	file.fwCampSites.append( campsite )

	entity placementHelper = CreateEntity( "info_placement_helper" )
	placementHelper.SetOrigin( camp.GetOrigin() ) // tracker needs a owner to display
	campsite.info = placementHelper
	DispatchSpawn( placementHelper )

	float radius = float( camp.kv.radius ) // radius to show up icon and spawn ais

	entity tracker = GetAvailableCampLocationTracker()
	tracker.SetOwner( placementHelper )
	campsite.tracker = tracker
	SetLocationTrackerRadius( tracker, radius )
	DispatchSpawn( tracker )
}

void function StartFWCampThink()
{
	foreach( CampSiteStruct camp in file.fwCampSites )
	{
		//print( "has " + string( file.fwCampSites.len() ) + " camps in total" )
		//print( "campId is " + camp.campId )
		thread FWAiCampThink( camp )
	}
}

// this is not using respawn's remaining code!
void function FWAiCampThink( CampSiteStruct campsite )
{
	string campId = campsite.campId
	string alertVarName = "fwCampAlert" + campId
	string stressVarName = "fwCampStress" + campId


	bool firstSpawn = true
	while( GamePlayingOrSuddenDeath() )
	{
		wait WAVE_STATE_TRANSITION_TIME

		int alertLevel = GetGlobalNetInt( alertVarName )
		//print( "campsite" + campId + ".ignoredSinceLastClean: " + string( campsite.ignoredSinceLastClean ) )
		if( campsite.ignoredSinceLastClean >= FW_CAMP_IGNORE_NEEDED && alertLevel > 0 ) // has been ignored many times, level > 0
			alertLevel = 0 // reset level
		else if( !firstSpawn ) // not the first spawn!
			alertLevel += 1 // level up

		if( alertLevel >= FW_MAX_LEVELS - 1 ) // reached max level?
			alertLevel = FW_MAX_LEVELS - 1 // stay

		// update netVars, don't know how client update these, sometimes they can't catch up
		SetGlobalNetInt( alertVarName, alertLevel )
		SetGlobalNetFloat( stressVarName, 1.0 ) // refill

		// under attack, clean this
		campsite.ignoredSinceLastClean = 0

		CampSpawnStruct curSpawnStruct = file.fwNpcLevel[alertLevel]
		string npcToSpawn = curSpawnStruct.spawnContent
		int maxSpawnCount = curSpawnStruct.maxSpawnCount
		int countPerSpawn = curSpawnStruct.countPerSpawn
		int killsToEscalate = curSpawnStruct.killsToEscalate

		// for this time's loop
		file.trackedCampNPCSpawns[campId] = {}
		int killsNeeded = killsToEscalate
		int lastNpcLeft
		while( true )
		{
			WaitFrame()

			//print( alertVarName + " : " + string( GetGlobalNetInt( alertVarName ) ) )
			//print( stressVarName + " : " + string( GetGlobalNetFloat( stressVarName ) ) )
			//print( "campsite" + campId + ".ignoredSinceLastClean: " + string( campsite.ignoredSinceLastClean ) )

			if( !( npcToSpawn in file.trackedCampNPCSpawns[campId] ) ) // init it
				file.trackedCampNPCSpawns[campId][npcToSpawn] <- 0

			int npcsLeft = file.trackedCampNPCSpawns[campId][npcToSpawn]
			killsNeeded -= lastNpcLeft - npcsLeft

			if( killsNeeded <= 0 ) // check if needs more kills
			{
				SetGlobalNetFloat( stressVarName, 0.0 ) // empty
				AddIgnoredCountToOtherCamps( campsite )
				break
			}

			// update stress bar
			float campStressLeft = float( killsNeeded ) / float( killsToEscalate )
			SetGlobalNetFloat( stressVarName, campStressLeft )
			//print( "campStressLeft: " + string( campStressLeft ) )

			if( maxSpawnCount - npcsLeft >= countPerSpawn && killsNeeded >= countPerSpawn ) // keep spawning
			{
				// spawn functions, for fw we only spawn one kind of enemy each time
				// light units
				if( npcToSpawn == "npc_soldier"
				|| npcToSpawn == "npc_spectre"
				|| npcToSpawn == "npc_stalker" )
					thread FW_SpawnDroppodSquad( campsite, npcToSpawn )

				// reapers
				if( npcToSpawn == "npc_super_spectre" )
					thread FW_SpawnReaper( campsite )

				file.trackedCampNPCSpawns[campId][npcToSpawn] += countPerSpawn

				// titans?
				//else if( npcToSpawn == "npc_titan" )
				//{
				//	file.trackedCampNPCSpawns[campId][npcToSpawn] += 4
				//}
			}

			lastNpcLeft = file.trackedCampNPCSpawns[campId][npcToSpawn]
		}

		// first loop ends
		firstSpawn = false
	}
}

void function AddIgnoredCountToOtherCamps( CampSiteStruct senderCamp )
{
	foreach( CampSiteStruct camp in file.fwCampSites )
	{
		//print( "senderCampId is: " + senderCamp.campId )
		//print( "curCampId is " + camp.campId )
		if( camp.campId != senderCamp.campId ) // other camps
		{
			camp.ignoredSinceLastClean += 1
		}
	}
}

// functions from at
void function FW_SpawnDroppodSquad( CampSiteStruct campsite, string aiType )
{
	entity spawnpoint
	if ( campsite.validDropPodSpawns.len() == 0 )
		spawnpoint = campsite.tracker // no spawnPoints valid, use camp itself to spawn
	else
		spawnpoint = campsite.validDropPodSpawns.getrandom()

	// add variation to spawns
	wait RandomFloat( 1.0 )

	AiGameModes_SpawnDropPod( spawnpoint.GetOrigin(), spawnpoint.GetAngles(), FW_AI_TEAM, aiType, void function( array<entity> guys ) : ( campsite, aiType )
	{
		FW_HandleSquadSpawn( guys, campsite, aiType )
	})
}

void function FW_HandleSquadSpawn( array<entity> guys, CampSiteStruct campsite, string aiType )
{
	foreach ( entity guy in guys )
	{
		guy.EnableNPCFlag( NPC_ALLOW_PATROL | NPC_ALLOW_HAND_SIGNALS | NPC_ALLOW_FLEE ) // NPC_ALLOW_INVESTIGATE is not allowed
		guy.SetScriptName( FW_NPC_SCRIPTNAME ) // well no need
		// show on minimap to let players kill them
		guy.Minimap_AlwaysShow( TEAM_MILITIA, null )
		guy.Minimap_AlwaysShow( TEAM_IMC, null )

		// untrack them on death
		thread FW_WaitToUntrackNPC( guy, campsite.campId, aiType )
	}
	// at least don't let them running around
	thread FW_ForceAssaultInCamp( guys, campsite.camp )
}

void function FW_SpawnReaper( CampSiteStruct campsite )
{
	entity spawnpoint
	if ( campsite.validDropPodSpawns.len() == 0 )
		spawnpoint = campsite.tracker // no spawnPoints valid, use camp itself to spawn
	else
		spawnpoint = campsite.validDropPodSpawns.getrandom()

	// add variation to spawns
	wait RandomFloat( 1.0 )

	AiGameModes_SpawnReaper( spawnpoint.GetOrigin(), spawnpoint.GetAngles(), FW_AI_TEAM, "npc_super_spectre_aitdm",void function( entity reaper ) : ( campsite )
	{
		reaper.SetScriptName( FW_NPC_SCRIPTNAME ) // no neet rn
		// show on minimap to let players kill them
		reaper.Minimap_AlwaysShow( TEAM_MILITIA, null )
		reaper.Minimap_AlwaysShow( TEAM_IMC, null )

		// at least don't let them running around
		thread FW_ForceAssaultInCamp( [reaper], campsite.camp )
		// untrack them on death
		thread FW_WaitToUntrackNPC( reaper, campsite.campId, "npc_super_spectre" )
	})
}

// maybe this will make them stay around the camp
void function FW_ForceAssaultInCamp( array<entity> guys, entity camp )
{
	while( true )
	{
		bool oneGuyValid = false
		foreach( entity guy in guys )
		{
			if( IsValid( guy ) )
			{
				guy.AssaultPoint( camp.GetOrigin() )
				guy.AssaultSetGoalRadius( float( camp.kv.radius ) ) // the camp's radius
				guy.AssaultSetFightRadius( 0 )
				oneGuyValid = true
			}
		}
		if( !oneGuyValid ) // no guys left
			return

		wait RandomFloatRange( 10, 15 ) // make randomness
	}
}

void function FW_WaitToUntrackNPC( entity guy, string campId, string aiType )
{
	guy.WaitSignal( "OnDeath", "OnDestroy" )
	if( aiType in file.trackedCampNPCSpawns[ campId ] ) // maybe escalated?
		file.trackedCampNPCSpawns[ campId ][ aiType ]--
}

/////////////////////////////////
///// AICAMPS FUNCTIONS END /////
/////////////////////////////////



///////////////////////////////
///// TERRITORY FUNCTIONS /////
///////////////////////////////

void function SetupFWTerritoryTrigger( entity trigger )
{
	//print("trigger_fw_territory detected")
	file.fwTerritories.append( trigger )
	trigger.ConnectOutput( "OnStartTouch", EntityEnterFWTrig )
	trigger.ConnectOutput( "OnEndTouch", EntityLeaveFWTrig )

	// respawn didn't leave a key for trigger's team, let's set it manually.
	if( Distance( trigger.GetOrigin(), file.harvesterMlt_info.GetOrigin() ) > Distance( trigger.GetOrigin(), file.harvesterImc_info.GetOrigin() ) )
		SetTeam( trigger, TEAM_IMC )
	else
		SetTeam( trigger, TEAM_MILITIA )

	// init
	file.teamTerrLastConnectTime[ trigger.GetTeam() ] <- 0.0

	thread FWTerritoryTriggerThink( trigger )
}

// since we're using a trigger_multiple, needs this to remove invalid keys
void function FWTerritoryTriggerThink( entity trigger )
{
	trigger.EndSignal( "OnDestroy" )

	while( true )
	{
		if( null in trigger.e.scriptTriggerData.entities )
			delete trigger.e.scriptTriggerData.entities[ null ]
		WaitFrame()
	}
}

void function EntityEnterFWTrig( entity trigger, entity ent, entity caller, var value )
{
	if( !IsValid( ent ) ) // post-spawns
		return
	if( !ent.IsPlayer() && !ent.IsTitan() ) // no neet to add props and grunts i guess
		return
	// functions that trigger_multiple missing
	if( IsValid( ent ) )
	{
		ScriptTriggerAddEntity( trigger, ent )
		thread ScriptTriggerPlayerDisconnectThink( trigger, ent )
		//TryFWTerritoryDialogue( trigger, ent ) // WIP
		file.teamTerrLastConnectTime[ trigger.GetTeam() ] = Time()
	}

	if( !IsValid(ent) )
		return
	if ( ent.IsPlayer() ) // notifications for player
	{
		MessageToPlayer( ent, eEventNotifications.Clear ) // clean up last message
		bool sameTeam = ent.GetTeam() == trigger.GetTeam()
		if ( sameTeam )
		{
			Remote_CallFunction_NonReplay( ent , "ServerCallback_FW_NotifyEnterFriendlyArea" )
			ent.SetPlayerNetInt( "indicatorId", 1 ) // 1 means "FRIENDLY TERRITORY"
		}
		else
		{
			Remote_CallFunction_NonReplay( ent , "ServerCallback_FW_NotifyEnterEnemyArea" )
			ent.SetPlayerNetInt( "indicatorId", 2 ) // 2 means "ENEMY TERRITORY"
		}
	}
}

void function EntityLeaveFWTrig( entity trigger, entity ent, entity caller, var value )
{
	if( !IsValid( ent ) ) // post-spawns
		return
	if( !ent.IsPlayer() && !ent.IsTitan() ) // no neet to add props and grunts i guess
		return
	// functions that trigger_multiple missing
	 if( IsValid( ent ) )
	{
		if( ent in trigger.e.scriptTriggerData.entities ) // need to check this!
		{
			ScriptTriggerRemoveEntity( trigger, ent )
			//TryFWTerritoryDialogue( trigger, ent ) // WIP
			file.teamTerrLastConnectTime[ trigger.GetTeam() ] = Time()
		}
	}

	if( !IsValid(ent) )
		return
	if ( ent.IsPlayer() ) // notifications for player
	{
		MessageToPlayer( ent, eEventNotifications.Clear ) // clean up
		bool sameTeam = ent.GetTeam() == trigger.GetTeam()
		if ( sameTeam )
			Remote_CallFunction_NonReplay( ent , "ServerCallback_FW_NotifyExitFriendlyArea" )
		else
			Remote_CallFunction_NonReplay( ent , "ServerCallback_FW_NotifyExitEnemyArea" )
		ent.SetPlayerNetInt( "indicatorId", 4 ) // 4 means "NO MAN'S LAND"
	}
}

// globlized!
bool function FW_IsPlayerInFriendlyTerritory( entity player )
{
	foreach( entity trigger in file.fwTerritories )
	{
		if( trigger.GetTeam() == player.GetTeam() ) // is it friendly one?
		{
			if( GetAllEntitiesInTrigger( trigger ).contains( player ) ) // is player inside?
				return true
		}
	}
	return false // can't find the player
}

// globlized!
bool function FW_IsPlayerInEnemyTerritory( entity player )
{
	foreach( entity trigger in file.fwTerritories )
	{
		if( trigger.GetTeam() != player.GetTeam() ) // is it enemy one?
		{
			 if( GetAllEntitiesInTrigger( trigger ).contains( player ) ) // is player inside?
				return true
		}
	}
	return false // can't find the player
}

///////////////////////////////////
///// TERRITORY FUNCTIONS END /////
///////////////////////////////////



////////////////////////////////
///// TITANSPAWN FUNCTIONS /////
////////////////////////////////

// territory trigger don't have a kv.radius, let's use a const
// 2800 will pretty much get harvester's near titan startpoints
const float FW_SPAWNPOINT_SEARCH_RADIUS = 2800.0


Point function FW_ReCalculateTitanReplacementPoint( Point basePoint, entity player )
{
	int team = player.GetTeam()
	// find team's harvester
	entity teamHarvester = FW_GetTeamHarvesterProp( team )

	if ( !IsValid( teamHarvester ) ) // team's havester has been destroyed!
        return basePoint // return given value
		
	if( Distance2D( basePoint.origin, teamHarvester.GetOrigin() ) <= FW_SPAWNPOINT_SEARCH_RADIUS ) // close enough!
		return basePoint // this origin is good enough

	// if not close enough to base, re-calculate
	array<entity> fortWarPoints = FW_GetTitanSpawnPointsForTeam( team )
	entity validPoint = GetClosest( fortWarPoints, basePoint.origin )
	basePoint.origin = validPoint.GetOrigin()
	return basePoint
}

bool function FW_RequestTitanAllowed( entity player, array< string > args )
{
	if( !FW_IsPlayerInFriendlyTerritory( player ) ) // is player in friendly base?
	{
		PlayFactionDialogueToPlayer( "tw_territoryNag", player ) // notify player
		TryPlayTitanfallNegativeSoundToPlayer( player )
		int objectiveID = 101 // which means "#FW_OBJECTIVE_TITANFALL"
		Remote_CallFunction_NonReplay( player, "ServerCallback_FW_SetObjective", objectiveID ) 
		return false
	}
	return true
}

bool function TryPlayTitanfallNegativeSoundToPlayer( entity player )
{
	if( !( "lastNegativeSound" in player.s ) )
		player.s.lastNegativeSound <- 0.0 // float
	if( player.s.lastNegativeSound + 1.0 > Time() ) // in sound cooldown
		return false

	// use a sound to notify player they can't titanfall here
	EmitSoundOnEntityOnlyToPlayer( player, player, "titan_dryfire" )
	player.s.lastNegativeSound = Time()
	
	return true
}

array<entity> function FW_GetTitanSpawnPointsForTeam( int team )
{
	array<entity> validSpawnPoints
	// find team's harvester
	entity teamHarvester = FW_GetTeamHarvesterProp( team )

	array<entity> allPoints
	// same as _replacement_titans_drop.gnut does
	allPoints.extend( GetEntArrayByClass_Expensive( "info_spawnpoint_titan" ) )
	allPoints.extend( GetEntArrayByClass_Expensive( "info_spawnpoint_titan_start" ) )
	allPoints.extend( GetEntArrayByClass_Expensive( "info_replacement_titan_spawn" ) )

	// get valid points from all points
	foreach( entity point in allPoints )
	{
		if( Distance2D( point.GetOrigin(), teamHarvester.GetOrigin() ) <= FW_SPAWNPOINT_SEARCH_RADIUS )
			validSpawnPoints.append( point )
	}

	return validSpawnPoints
}

// "Respawn as Titan" don't follow the rateSpawnPoints, fix it manually
entity function FW_ForcedTitanStartPoint( entity player, entity basePoint )
{
	int team = player.GetTeam()
	array<entity> startPoints = SpawnPoints_GetTitanStart( team )
	entity validPoint = startPoints[ RandomInt( startPoints.len() ) ] // choose a random( maybe not safe ) start point
	return validPoint
}

////////////////////////////////////
///// TITANSPAWN FUNCTIONS END /////
////////////////////////////////////



/////////////////////////////////
///// THREATLEVEL FUNCTIONS /////
/////////////////////////////////

void function FWAreaThreatLevelThink()
{
	thread FWAreaThreatLevelThink_Threaded()
}

void function FWAreaThreatLevelThink_Threaded()
{
	entity imcTerritory
	entity mltTerritory
	foreach( entity territory in file.fwTerritories )
	{
		if( territory.GetTeam() == TEAM_IMC )
			imcTerritory = territory
		else
			mltTerritory = territory
	}

	float lastWarningTime // for debounce
	bool warnImcTitanApproach
	bool warnMltTitanApproach
	bool warnImcTitanInArea
	bool warnMltTitanInArea

	while( GamePlayingOrSuddenDeath() )
	{
		//print( " imc threat level is: " + string( GetGlobalNetInt( "imcTowerThreatLevel" ) ) )
		//print( " mlt threat level is: " + string( GetGlobalNetInt( "milTowerThreatLevel" ) ) )
		float imcLastDamage = fw_harvesterImc.lastDamage
		float mltLastDamage = fw_harvesterMlt.lastDamage
		bool imcShieldDown = fw_harvesterImc.harvesterShieldDown
		bool mltShieldDown = fw_harvesterMlt.harvesterShieldDown

		// imc threatLevel
		if( imcLastDamage + FW_HARVESTER_DAMAGED_DEBOUNCE >= Time() && imcShieldDown )
			SetGlobalNetInt( "imcTowerThreatLevel", 3 ) // 3 will show a "harvester being damaged" warning to player
		else if( warnImcTitanInArea )
			SetGlobalNetInt( "imcTowerThreatLevel", 2 ) // 2 will show a "titan in area" warning to player
		else if( warnImcTitanApproach )
			SetGlobalNetInt( "imcTowerThreatLevel", 1 ) // 1 will show a "titan approach" waning to player
		else
			SetGlobalNetInt( "imcTowerThreatLevel", 0 ) // 0 will hide all warnings

		// militia threatLevel
		if( mltLastDamage + FW_HARVESTER_DAMAGED_DEBOUNCE >= Time() && mltShieldDown )
			SetGlobalNetInt( "milTowerThreatLevel", 3 ) // 3 will show a "harvester being damaged" warning to player
		else if( warnMltTitanInArea )
			SetGlobalNetInt( "milTowerThreatLevel", 2 ) // 2 will show a "titan in area" warning to player
		else if( warnMltTitanApproach )
			SetGlobalNetInt( "milTowerThreatLevel", 1 ) // 1 will show a "titan approach" waning to player
		else
			SetGlobalNetInt( "milTowerThreatLevel", 0 ) // 0 will hide all warnings


		// clean it here
		warnImcTitanInArea = false
		warnMltTitanInArea = false
		warnImcTitanApproach = false
		warnMltTitanApproach = false

		// get valid titans
		array<entity> allTitans = GetNPCArrayByClass( "npc_titan" )
		array<entity> allPlayers = GetPlayerArray()
		foreach( entity player in allPlayers )
		{
			if( IsAlive( player ) && player.IsTitan() )
			{
				allTitans.append( player )
			}
		}

		// check threats
		array<entity> imcEntArray = GetAllEntitiesInTrigger( imcTerritory )
		array<entity> mltEntArray = GetAllEntitiesInTrigger( mltTerritory )
		imcEntArray.removebyvalue( null ) // since we're using a fake trigger, need to check this
		mltEntArray.removebyvalue( null )
		foreach( entity ent in imcEntArray )
		{
			//print( ent )
			if( !IsValid( ent ) ) // since we're using a fake trigger, need to check this
				continue
			if( ent.IsPlayer() || ent.IsNPC() )
			{
				if( ent.IsTitan() && ent.GetTeam() != TEAM_IMC )
					warnImcTitanInArea = true
			}
		}
		foreach( entity ent in mltEntArray )
		{
			//print( ent )
			if( !IsValid( ent ) ) // since we're using a fake trigger, need to check this
				continue
			if( ent.IsPlayer() || ent.IsNPC() )
			{
				if( ent.IsTitan() && ent.GetTeam() != TEAM_MILITIA )
					warnMltTitanInArea = true
			}
		}

		foreach( entity titan in allTitans )
		{
			if( !imcEntArray.contains( titan )
			&& !mltEntArray.contains( titan )
			&& titan.GetTeam() != TEAM_IMC
			&& !titan.e.isHotDropping )
				warnImcTitanApproach = true // this titan must be in natural space

			if( !mltEntArray.contains( titan )
			&& !imcEntArray.contains( titan )
			&& titan.GetTeam() != TEAM_MILITIA
			&& !titan.e.isHotDropping )
				warnMltTitanApproach = true // this titan must be in natural space
		}

		WaitFrame()
	}
}

/////////////////////////////////////
///// THREATLEVEL FUNCTIONS END /////
/////////////////////////////////////



////////////////////////////
///// TURRET FUNCTIONS /////
////////////////////////////

void function OnFWTurretSpawned( entity turret )
{
	turret.EnableTurret() // always enabled
	SetDefaultMPEnemyHighlight( turret ) // for sonar highlights to work
	AddEntityCallback_OnDamaged( turret, OnMegaTurretDamaged )
	thread FWTurretHighlightThink( turret )
}

// this will clear turret's highlight upon their death, for notifying players to fix them
void function FWTurretHighlightThink( entity turret )
{
	turret.EndSignal( "OnDestroy" )
	WaitFrame() // wait a frame for other turret spawn options to set up
	Highlight_SetFriendlyHighlight( turret, "fw_friendly" ) // initialize the highlight, they will show upon player's next respawn

	turret.WaitSignal( "OnDeath" )
	Highlight_ClearFriendlyHighlight( turret )
}

// for battery_port, replace the turret with new one
entity function FW_ReplaceMegaTurret( entity perviousTurret )
{
	if( !IsValid( perviousTurret ) ) // previous turret not exist!
		return

	entity turret = CreateNPC( "npc_turret_mega", perviousTurret.GetTeam(), perviousTurret.GetOrigin(), perviousTurret.GetAngles() )
	SetSpawnOption_AISettings( turret, "npc_turret_mega_fortwar" )
	DispatchSpawn( turret )

	// apply settings to new turret, must up on date
	turret.s.baseTurret <- perviousTurret.s.baseTurret
	turret.s.minimapstate <- perviousTurret.s.minimapstate
	turret.s.turretflagid <- perviousTurret.s.turretflagid
	turret.s.lastDamagedTime <- perviousTurret.s.lastDamagedTime
	turret.s.relatedBatteryPort <- perviousTurret.s.relatedBatteryPort

	int maxHealth = perviousTurret.GetMaxHealth()
	int maxShield = perviousTurret.GetShieldHealthMax()
	turret.SetMaxHealth( maxHealth )
	turret.SetHealth( maxHealth )
	turret.SetShieldHealth( maxShield )
	turret.SetShieldHealthMax( maxShield )

	// update turretSiteStruct
	foreach( TurretSiteStruct turretsite in file.turretsites )
	{
		if( turretsite.turret == perviousTurret )
		{
			turretsite.turret = turret // only changed this
		}
	}

	perviousTurret.Destroy() // destroy previous one

	return turret
}

// avoid notifications overrides itself
const float TURRET_NOTIFICATION_DEBOUNCE = 10.0

void function OnMegaTurretDamaged( entity turret, var damageInfo )
{
	int damageSourceID = DamageInfo_GetDamageSourceIdentifier( damageInfo )
	entity attacker = DamageInfo_GetAttacker( damageInfo )
	float damageAmount = DamageInfo_GetDamage( damageInfo )
	int scriptType = DamageInfo_GetCustomDamageType( damageInfo )
	int turretTeam = turret.GetTeam()

	if ( !damageSourceID && !damageAmount && !attacker )
		return

	if( turret.GetShieldHealth() - damageAmount <= 0 && scriptType != damageTypes.rodeoBatteryRemoval ) // this shot breaks shield
	{
		if ( !attacker.IsTitan() && !IsSuperSpectre( attacker ) )
		{
			if( attacker.IsPlayer() && attacker.GetTeam() != turret.GetTeam() ) // good to have
			{
				// avoid notifications overrides itself
				if( attacker.s.lastTurretNotifyTime + TURRET_NOTIFICATION_DEBOUNCE < Time() )
				{
					MessageToPlayer( attacker, eEventNotifications.Clear ) // clean up last message
					MessageToPlayer( attacker, eEventNotifications.TurretTitanDamageOnly )
					attacker.s.lastTurretNotifyTime = Time()
				}
			}
			DamageInfo_SetDamage( damageInfo, turret.GetShieldHealth() ) // destroy shields
			return
		}
	}

	// successfully damaged turret
	turret.s.lastDamagedTime = Time()

	if ( damageSourceID == eDamageSourceId.mp_titanweapon_heat_shield ||
	damageSourceID == eDamageSourceId.mp_titanweapon_meteor_thermite ||
	damageSourceID == eDamageSourceId.mp_titanweapon_flame_wall ||
	damageSourceID == eDamageSourceId.mp_titanability_slow_trap ||
	damageSourceID == eDamageSourceId.mp_titancore_flame_wave_secondary
	) // scorch's thermite damages
		DamageInfo_SetDamage( damageInfo, DamageInfo_GetDamage( damageInfo )/2 ) // nerf scorch

	// faction dialogue
	damageAmount = DamageInfo_GetDamage( damageInfo )
	if( turret.GetHealth() - damageAmount <= 0 ) // turret killed this shot
	{
		if( GamePlayingOrSuddenDeath() )
			PlayFactionDialogueToTeam( "fortwar_turretDestroyedFriendly", turretTeam )
	}
}

void function InitTurretSettings()
{
	foreach( TurretSiteStruct turretSite in file.turretsites )
	{
		entity turret = turretSite.turret
		entity minimapstate = turretSite.minimapstate
		int teamNum = turretSite.site.GetTeam()
		int id = int( string( turretSite.site.kv.turretId ) )
		string idString = string( id + 1 )
		int team = int( string( turretSite.site.kv.teamnumber ) )

		int stateFlag = 1 // netural

		// spawn with teamNumber?
		if( team == TEAM_IMC || team == TEAM_MILITIA )
			turret.s.baseTurret = true

		//SetTeam( minimapstate, team ) // setTeam() for icons is done in TurretStateWatcher()
		SetTeam( turret, team )

		//print( "Try to set globatNetEnt: " + "turretSite" + idString )

		turret.s.turretflagid = idString
		turretSite.turretflagid = idString

		thread TurretStateWatcher( turretSite )
	}
}

// about networkvar "turretStateFlags" value
// 1 means destoryed/netural
// 2 means imc turret
// 4 means mlt turret
// 10 means shielded imc turret
// 13 means shielded mlt turret
// 16 means destoryed/netural being attacked
// 18 means imc turret being attacked
// 20 means mlt turret being attacked
// 26 means shielded imc turret being attacked
// 28 means shielded mlt turret being attacked

// unsure:
// 24 means destroyed imc turret being attacked?
// 40 means destroyed imc turret?
// 48 means destroyed mlt turret being attacked?

const int TURRET_DESTROYED_FLAG = 1
const int TURRET_NATURAL_FLAG = 1
const int TURRET_IMC_FLAG = 2
const int TURRET_MLT_FLAG = 4
const int TURRET_SHIELDED_IMC_FLAG = 10
const int TURRET_SHIELDED_MLT_FLAG = 13

const int TURRET_UNDERATTACK_NATURAL_FLAG = 16
const int TURRET_UNDERATTACK_IMC_FLAG = 18
const int TURRET_UNDERATTACK_MLT_FLAG = 20
// natural turret noramlly can't get shielded
const int TURRET_SHIELDED_UNDERATTACK_IMC_FLAG = 26
const int TURRET_SHIELDED_UNDERATTACK_MLT_FLAG = 28

void function TurretStateWatcher( TurretSiteStruct turretSite )
{
	entity mapIcon = turretSite.minimapstate
	entity turret = turretSite.turret
	entity batteryPort = expect entity( turret.s.relatedBatteryPort )

	int turretHealth = GetCurrentPlaylistVarInt( "fw_turret_health", FW_DEFAULT_TURRET_HEALTH )
	int turretShield = GetCurrentPlaylistVarInt( "fw_turret_shield", FW_DEFAULT_TURRET_SHIELD )
	turret.SetMaxHealth( turretHealth )
	turret.SetHealth( turretHealth )
	turret.SetShieldHealthMax( turretShield )

	string idString = turretSite.turretflagid
	string siteVarName = "turretSite" + idString
	string stateVarName = "turretStateFlags" + idString

	// battery overlay icons holder
	entity overlayState = CreateEntity( "prop_script" )
	overlayState.SetValueForModelKey( $"models/communication/flag_base.mdl" ) // requires a model to show overlays
	overlayState.Hide() // this can still show players overlay icons
	overlayState.SetOrigin( batteryPort.GetOrigin() ) // tracking batteryPort's positions
	overlayState.SetAngles( batteryPort.GetAngles() )
	overlayState.kv.solid = SOLID_VPHYSICS
	DispatchSpawn( overlayState )

	svGlobal.levelEnt.EndSignal( "CleanUpEntitiesForRoundEnd" ) // end dialogues is good
	mapIcon.EndSignal( "OnDestroy" ) // mapIcon should be valid all time, tracking it
	batteryPort.EndSignal( "OnDestroy" ) // also track this
	overlayState.EndSignal( "OnDestroy" )

	SetGlobalNetEnt( siteVarName, overlayState ) // tracking batteryPort's positions and team
	SetGlobalNetInt( stateVarName, TURRET_NATURAL_FLAG ) // init for all turrets

	int lastFrameTeam
	bool lastFrameIsAlive

	while( true )
	{
		WaitFrame() // start of the loop

		turret = turretSite.turret // need to keep updating, for sometimes it being replaced

		if( !IsValid( turret ) ) // replacing turret this frame
			continue // skip the loop once

		bool isBaseTurret = expect bool( turret.s.baseTurret )
		int turretTeam = turret.GetTeam()
		bool turretAlive = IsAlive( turret )

		bool changedTeamThisFrame = lastFrameTeam != turretTeam // turret has changed team?
		bool killedThisFrame = lastFrameIsAlive != turretAlive // turret has no health left?

		if( !turretAlive ) // turret down, waiting to be repaired
		{
			if( !isBaseTurret ) // never reset base turret's team
			{
				SetTeam( turret, TEAM_UNASSIGNED )
				SetTeam( mapIcon, TEAM_UNASSIGNED )
				SetTeam( batteryPort, TEAM_UNASSIGNED )
				SetTeam( overlayState, TEAM_UNASSIGNED )
				batteryPort.SetUsableByGroup( "pilot" ) // show hints to any pilot
			}
			SetGlobalNetInt( stateVarName, TURRET_DESTROYED_FLAG )
			continue
		}

		// wrong dialogue, it will say "The turret you requested is on the way"
		//if( changedTeamThisFrame ) // has been hacked!
		//    PlayFactionDialogueToTeam( "fortwar_turretDeployFriendly", turretTeam )

		int iconTeam = turretTeam == TEAM_BOTH ? TEAM_UNASSIGNED : turretTeam // specific check
		SetTeam( mapIcon, iconTeam ) // update icon's team
		SetTeam( batteryPort, turretTeam ) // update batteryPort's team
		SetTeam( overlayState, iconTeam ) // update overlayEnt's team

		if( turretTeam != TEAM_BOTH && turretTeam != TEAM_UNASSIGNED ) // not a natural turret nor dead
			batteryPort.SetUsableByGroup( "friendlies pilot" ) // only show hint to friendlies

		float lastDamagedTime = expect float( turret.s.lastDamagedTime )
		int stateFlag = TURRET_NATURAL_FLAG

		// imc states
		if( iconTeam == TEAM_IMC )
		{
			if( lastDamagedTime + FW_TURRET_DAMAGED_DEBOUNCE >= Time() ) // recent underattack
			{
				if( turret.GetShieldHealth() > 0 ) // has shields
					stateFlag = TURRET_SHIELDED_UNDERATTACK_IMC_FLAG
				else
					stateFlag = TURRET_UNDERATTACK_IMC_FLAG

				// these dialogue have 30s debounce inside
				if( isBaseTurret )
					PlayFactionDialogueToTeam( "fortwar_baseTurretsUnderAttack", TEAM_IMC )
				else
					PlayFactionDialogueToTeam( "fortwar_awayTurretsUnderAttack", TEAM_IMC )
			}
			else if( turret.GetShieldHealth() > 0 ) // has shields left
				stateFlag = TURRET_SHIELDED_IMC_FLAG
			else
				stateFlag = TURRET_IMC_FLAG
		}

		// mlt states
		if( iconTeam == TEAM_MILITIA )
		{
			if( lastDamagedTime + FW_TURRET_DAMAGED_DEBOUNCE >= Time() ) // recent underattack
			{
				if( turret.GetShieldHealth() > 0 ) // has shields
					stateFlag = TURRET_SHIELDED_UNDERATTACK_MLT_FLAG
				else
					stateFlag = TURRET_UNDERATTACK_MLT_FLAG

				// these dialogue have 30s debounce inside
				if( isBaseTurret )
					PlayFactionDialogueToTeam( "fortwar_baseTurretsUnderAttack", TEAM_MILITIA )
				else
					PlayFactionDialogueToTeam( "fortwar_awayTurretsUnderAttack", TEAM_MILITIA )
			}
			else if( turret.GetShieldHealth() > 0 ) // has shields left
				stateFlag = TURRET_SHIELDED_MLT_FLAG
			else
				stateFlag = TURRET_MLT_FLAG
		}

		// natural states
		if( iconTeam == TEAM_UNASSIGNED )
		{
			if( lastDamagedTime + FW_TURRET_DAMAGED_DEBOUNCE >= Time() ) // recent underattack
				stateFlag = TURRET_UNDERATTACK_NATURAL_FLAG
			else
				stateFlag = TURRET_NATURAL_FLAG
		}

		SetGlobalNetInt( stateVarName, stateFlag )

		// update these
		lastFrameTeam = turretTeam
		lastFrameIsAlive = turretAlive

		WaitFrame()
	}
}

////////////////////////////////
///// TURRET FUNCTIONS END /////
////////////////////////////////



///////////////////////////////
///// HARVESTER FUNCTIONS /////
///////////////////////////////

void function startFWHarvester()
{
	thread HarvesterThink(fw_harvesterImc)
	thread HarvesterAlarm(fw_harvesterImc)
	thread UpdateHarvesterHealth( TEAM_IMC )

	thread HarvesterThink(fw_harvesterMlt)
	thread HarvesterAlarm(fw_harvesterMlt)
	thread UpdateHarvesterHealth( TEAM_MILITIA )
}

entity function FW_GetTeamHarvesterProp( int team )
{
	if( team == TEAM_IMC )
		return fw_harvesterImc.harvester
	else if( team == TEAM_MILITIA )
		return fw_harvesterMlt.harvester

	unreachable // crash the game
}

void function FW_createHarvester()
{
	// imc havester spawn
	fw_harvesterImc = SpawnHarvester( file.harvesterImc_info.GetOrigin(), file.harvesterImc_info.GetAngles(), GetCurrentPlaylistVarInt( "fw_harvester_health", FW_DEFAULT_HARVESTER_HEALTH ), GetCurrentPlaylistVarInt( "fw_harvester_shield", FW_DEFAULT_HARVESTER_SHIELD ), TEAM_IMC )
	fw_harvesterImc.harvester.Minimap_SetAlignUpright( true )
	fw_harvesterImc.harvester.Minimap_AlwaysShow( TEAM_IMC, null )
	fw_harvesterImc.harvester.Minimap_AlwaysShow( TEAM_MILITIA, null )
	fw_harvesterImc.harvester.Minimap_SetHeightTracking( true )
	fw_harvesterImc.harvester.Minimap_SetZOrder( MINIMAP_Z_OBJECT )
	fw_harvesterImc.harvester.Minimap_SetCustomState( eMinimapObject_prop_script.FD_HARVESTER )
	AddEntityCallback_OnDamaged( fw_harvesterImc.harvester, OnHarvesterDamaged )
	AddEntityCallback_OnPostDamaged( fw_harvesterImc.harvester, OnHarvesterPostDamaged )
	
	// imc havester settings
	// don't set this, or sonar pulse will try to find it and failed to set highlight
	//fw_harvesterMlt.harvester.SetScriptName("fw_team_tower")
	file.harvesters.append(fw_harvesterImc)
	entity trackerImc = GetAvailableBaseLocationTracker()
	trackerImc.SetOwner( fw_harvesterImc.harvester )
	DispatchSpawn( trackerImc )
	SetLocationTrackerRadius( trackerImc, 1 ) // whole map

	// scores starts from 100, TeamScore means harvester health; TeamScore2 means shield bar
	GameRules_SetTeamScore( TEAM_MILITIA , 100 )
	GameRules_SetTeamScore2( TEAM_MILITIA , 100 )


	// mlt havester spawn
	fw_harvesterMlt = SpawnHarvester( file.harvesterMlt_info.GetOrigin(), file.harvesterMlt_info.GetAngles(), GetCurrentPlaylistVarInt( "fw_harvester_health", FW_DEFAULT_HARVESTER_HEALTH ), GetCurrentPlaylistVarInt( "fw_harvester_shield", FW_DEFAULT_HARVESTER_SHIELD ), TEAM_MILITIA )
	fw_harvesterMlt.harvester.Minimap_SetAlignUpright( true )
	fw_harvesterMlt.harvester.Minimap_AlwaysShow( TEAM_IMC, null )
	fw_harvesterMlt.harvester.Minimap_AlwaysShow( TEAM_MILITIA, null )
	fw_harvesterMlt.harvester.Minimap_SetHeightTracking( true )
	fw_harvesterMlt.harvester.Minimap_SetZOrder( MINIMAP_Z_OBJECT )
	fw_harvesterMlt.harvester.Minimap_SetCustomState( eMinimapObject_prop_script.FD_HARVESTER )
	AddEntityCallback_OnDamaged( fw_harvesterMlt.harvester, OnHarvesterDamaged )
	AddEntityCallback_OnPostDamaged( fw_harvesterMlt.harvester, OnHarvesterPostDamaged )

	// mlt havester settings
	// don't set this, or sonar pulse will try to find it and failed to set highlight
	//fw_harvesterImc.harvester.SetScriptName("fw_team_tower")
	file.harvesters.append(fw_harvesterMlt)
	entity trackerMlt = GetAvailableBaseLocationTracker()
	trackerMlt.SetOwner( fw_harvesterMlt.harvester )
	DispatchSpawn( trackerMlt )
	SetLocationTrackerRadius( trackerMlt, 1 ) // whole map

	// scores starts from 100, TeamScore means harvester health; TeamScore2 means shield bar
	GameRules_SetTeamScore( TEAM_IMC , 100 )
	GameRules_SetTeamScore2( TEAM_IMC , 100 )
}

// this function can't handle specific damageSourceID, such as plasma railgun, but is the best to scale both shield and health damage
void function OnHarvesterDamaged( entity harvester, var damageInfo )
{
	if ( !IsValid( harvester ) )
		return

	int friendlyTeam = harvester.GetTeam()
	int enemyTeam = GetOtherTeam( friendlyTeam )
	int damageSourceID = DamageInfo_GetDamageSourceIdentifier( damageInfo )
		
	HarvesterStruct harvesterstruct // current harveter's struct
	if( friendlyTeam == TEAM_MILITIA )
		harvesterstruct = fw_harvesterMlt
	if( friendlyTeam == TEAM_IMC )
		harvesterstruct = fw_harvesterImc

	float damageAmount = DamageInfo_GetDamage( damageInfo )
	if((harvester.GetShieldHealth()-damageAmount)<0)
	{
		if( !harvesterstruct.harvesterShieldDown )
		{
			PlayFactionDialogueToTeam( "fortwar_baseShieldDownFriendly", friendlyTeam )
			PlayFactionDialogueToTeam( "fortwar_baseShieldDownEnemy", enemyTeam )
			harvesterstruct.harvesterShieldDown = true // prevent shield dialogues from repeating
		}
	}

	// always reset harvester's recharge delay
	harvesterstruct.lastDamage = Time()

	
	// done damage adjustments here, since harvester prop's health is setting manually through damageAmount
	if ( damageSourceID == eDamageSourceId.mp_titancore_laser_cannon )
		DamageInfo_SetDamage( damageInfo, DamageInfo_GetDamage( damageInfo ) / 50 ) // laser core shreds super well for some reason

    // plasma railgun can always do no-charge shots and deal same damage
    if ( damageSourceID == eDamageSourceId.mp_titanweapon_sniper ) // nerf northstar
    {
		DamageInfo_SetDamage( damageInfo, DamageInfo_GetDamage( damageInfo ) / 3 )
		entity inflictor = DamageInfo_GetInflictor( damageInfo )
		if( IsValid( inflictor ) && inflictor.IsProjectile() )
		{
			inflictor.s.extraDamagePerBullet = expect int( inflictor.s.extraDamagePerBullet ) / 3
		}
	}

    // leadwall have high pilot damage so works really well aginst harvester
    if ( damageSourceID == eDamageSourceId.mp_titanweapon_leadwall ) // nerf ronin
        DamageInfo_SetDamage( damageInfo, DamageInfo_GetDamage( damageInfo ) / 2 )

    // missiles mostly have high pilot damage so works really well aginst harvester
	if ( damageSourceID == eDamageSourceId.mp_titanweapon_salvo_rockets ||
		damageSourceID == eDamageSourceId.mp_titanweapon_shoulder_rockets ||
        damageSourceID == eDamageSourceId.mp_titancore_salvo_core
	) // titan missiles
		DamageInfo_SetDamage( damageInfo, DamageInfo_GetDamage( damageInfo ) / 3 )

	if ( damageSourceID == eDamageSourceId.mp_titanweapon_sticky_40mm ) // 40mm trakcer cannon
		DamageInfo_SetDamage( damageInfo, DamageInfo_GetDamage( damageInfo ) / 2 )

    if ( damageSourceID == eDamageSourceId.mp_titanweapon_flightcore_rockets ) // flight core shreds well
        DamageInfo_SetDamage( damageInfo, DamageInfo_GetDamage( damageInfo ) / 5 ) 

    // cluster missle is very effective against non-moving targets
    if ( damageSourceID == eDamageSourceId.mp_titanweapon_dumbfire_rockets ) // cluster missile shreds super well
        DamageInfo_SetDamage( damageInfo, DamageInfo_GetDamage( damageInfo ) / 10 ) 

    // scorch's thermites is very effective against non-moving targets
	if ( damageSourceID == eDamageSourceId.mp_titanweapon_heat_shield || 
        damageSourceID == eDamageSourceId.mp_titanweapon_meteor_thermite ||
		damageSourceID == eDamageSourceId.mp_titanweapon_flame_wall ||
		damageSourceID == eDamageSourceId.mp_titanability_slow_trap ||
		damageSourceID == eDamageSourceId.mp_titancore_flame_wave_secondary
	) // scorch's thermite damages, nerf scorch
		DamageInfo_SetDamage( damageInfo, DamageInfo_GetDamage( damageInfo ) / 5 )

}
void function OnHarvesterPostDamaged( entity harvester, var damageInfo )
{
	if ( !IsValid( harvester ) )
		return

	int friendlyTeam = harvester.GetTeam()
	int enemyTeam = GetOtherTeam( friendlyTeam )

	GameRules_SetTeamScore( friendlyTeam , 1.0 * GetHealthFrac( harvester ) * 100 )

	int damageSourceID = DamageInfo_GetDamageSourceIdentifier( damageInfo )
	entity attacker = DamageInfo_GetAttacker( damageInfo )
	int scriptType = DamageInfo_GetCustomDamageType( damageInfo )
	float damageAmount = DamageInfo_GetDamage( damageInfo )

	if(damageAmount == 0)
		return

	if ( !damageSourceID && !damageAmount && !attacker ) // actually not dealing any damage?
		return
	
	// prevent player from sniping the harvester cross-map
	if ( attacker.IsPlayer() && !FW_IsPlayerInEnemyTerritory( attacker ) )
	{
		Remote_CallFunction_NonReplay( attacker , "ServerCallback_FW_NotifyNeedsEnterEnemyArea" )
		DamageInfo_SetDamage( damageInfo, 0 )
		DamageInfo_SetCustomDamageType( damageInfo, scriptType | DF_NO_INDICATOR ) // hide the hitmarker
		return // these damage won't do anything to the harvester
	}

	HarvesterStruct harvesterstruct // current harveter's struct
	if( friendlyTeam == TEAM_MILITIA )
		harvesterstruct = fw_harvesterMlt
	if( friendlyTeam == TEAM_IMC )
		harvesterstruct = fw_harvesterImc


	damageAmount = DamageInfo_GetDamage( damageInfo ) // get damageAmount again after all damage adjustments

	if ( !attacker.IsTitan() )
	{
		if( attacker.IsPlayer() )
			Remote_CallFunction_NonReplay( attacker , "ServerCallback_FW_NotifyTitanRequired" )
		DamageInfo_SetDamage( damageInfo, harvester.GetShieldHealth() )
		damageAmount = 0 // never damage haveter's prop
	}

	if( !harvesterstruct.harvesterShieldDown )
	{
		PlayFactionDialogueToTeam( "fortwar_baseShieldDownFriendly", friendlyTeam )
		PlayFactionDialogueToTeam( "fortwar_baseShieldDownEnemy", enemyTeam )
		harvesterstruct.harvesterShieldDown = true // prevent shield dialogues from repeating
	}

	harvesterstruct.harvesterDamageTaken = harvesterstruct.harvesterDamageTaken + damageAmount // track damage for wave recaps
	float newHealth = harvester.GetHealth() - damageAmount
	float oldhealthpercent = ( ( harvester.GetHealth().tofloat() / harvester.GetMaxHealth() ) * 100 )
	float healthpercent = ( ( newHealth / harvester.GetMaxHealth() ) * 100 )

	if (healthpercent <= 75 && oldhealthpercent > 75) // we don't want the dialogue to keep saying "Harvester is below 75% health" everytime they take additional damage
	{
		PlayFactionDialogueToTeam( "fortwar_baseDmgFriendly75", friendlyTeam )
		PlayFactionDialogueToTeam( "fortwar_baseDmgEnemy75", enemyTeam )
	}

	if (healthpercent <= 50 && oldhealthpercent > 50)
	{
		PlayFactionDialogueToTeam( "fortwar_baseDmgFriendly50", friendlyTeam )
		PlayFactionDialogueToTeam( "fortwar_baseDmgEnemy50", enemyTeam )
	}

	if (healthpercent <= 25 && oldhealthpercent > 25)
	{
		PlayFactionDialogueToTeam( "fortwar_baseDmgFriendly25", friendlyTeam )
		PlayFactionDialogueToTeam( "fortwar_baseDmgEnemy25", enemyTeam )
	}

	if( newHealth <= 0 )
	{
		EmitSoundAtPosition(TEAM_UNASSIGNED,harvesterstruct.harvester.GetOrigin(),"coop_generator_destroyed")
		newHealth = 0
		harvesterstruct.rings.Destroy()
		harvesterstruct.harvester.Dissolve( ENTITY_DISSOLVE_CORE, Vector( 0, 0, 0 ), 500 )
	}

	harvester.SetHealth( newHealth )
	harvesterstruct.havesterWasDamaged = true

	if ( attacker.IsPlayer() )
	{
        // dialogue for enemy attackers
        if( !harvesterstruct.harvesterShieldDown )
            PlayFactionDialogueToTeam( "fortwar_baseEnemyAllyAttacking", enemyTeam )

		attacker.NotifyDidDamage( harvester, DamageInfo_GetHitBox( damageInfo ), DamageInfo_GetDamagePosition( damageInfo ), DamageInfo_GetCustomDamageType( damageInfo ), DamageInfo_GetDamage( damageInfo ), DamageInfo_GetDamageFlags( damageInfo ), DamageInfo_GetHitGroup( damageInfo ), DamageInfo_GetWeapon( damageInfo ), DamageInfo_GetDistFromAttackOrigin( damageInfo ) )

        // get newest damage for adding score!
        int scoreDamage = int( DamageInfo_GetDamage( damageInfo ) )
        // score events
        attacker.AddToPlayerGameStat( PGS_ASSAULT_SCORE, scoreDamage )

        // add to player structs
        file.playerDamageHarvester[ attacker ].recentDamageTime = Time()
        file.playerDamageHarvester[ attacker ].storedDamage += scoreDamage

        // enough to earn score?
        if( file.playerDamageHarvester[ attacker ].storedDamage >= FW_HARVESTER_DAMAGE_SEGMENT )
        {
            AddPlayerScore( attacker, "FortWarTowerDamage", attacker )
            attacker.AddToPlayerGameStat( PGS_DEFENSE_SCORE, POINTVALUE_FW_TOWER_DAMAGE )
            file.playerDamageHarvester[ attacker ].storedDamage -= FW_HARVESTER_DAMAGE_SEGMENT // reset stored damage
        }
	}


	// harvester down!
    if ( harvester.GetHealth() == 0 )
    {
		// force deciding winner
        SetWinner( enemyTeam )
        //PlayFactionDialogueToTeam( "scoring_wonMercy", enemyTeam )
        //PlayFactionDialogueToTeam( "fortwar_matchLoss", friendlyTeam )
        GameRules_SetTeamScore2( friendlyTeam, 0 ) // force set score2 to 0( shield bar will empty )
        GameRules_SetTeamScore( friendlyTeam, 0 ) // force set score to 0( health 0% )
    }
}

void function HarvesterThink( HarvesterStruct fw_harvester )
{
	entity harvester = fw_harvester.harvester


	EmitSoundOnEntity( harvester, "coop_generator_startup" )

	float lastTime = Time()
	wait 4
	int lastShieldHealth = harvester.GetShieldHealth()
	generateBeamFX( fw_harvester )
	generateShieldFX( fw_harvester )

	EmitSoundOnEntity( harvester, "coop_generator_ambient_healthy" )

	bool isRegening = false // stops the regenning sound to keep stacking on top of each other

	while ( IsAlive( harvester ) )
	{
		float currentTime = Time()
		float deltaTime = currentTime -lastTime

		if ( IsValid( fw_harvester.particleShield ) )
		{
			vector shieldColor = GetShieldTriLerpColor( 1.0 - ( harvester.GetShieldHealth().tofloat() / harvester.GetShieldHealthMax().tofloat() ) )
			EffectSetControlPointVector( fw_harvester.particleShield, 1, shieldColor )
		}

		if( IsValid( fw_harvester.particleBeam ) )
		{
			vector beamColor = GetShieldTriLerpColor( 1.0 - ( harvester.GetHealth().tofloat() / harvester.GetMaxHealth().tofloat() ) )
			EffectSetControlPointVector( fw_harvester.particleBeam, 1, beamColor )
		}

		if ( fw_harvester.harvester.GetShieldHealth() == 0 )
			if( IsValid( fw_harvester.particleShield ) )
				fw_harvester.particleShield.Destroy()

		if ( ( ( currentTime-fw_harvester.lastDamage ) >= GetCurrentPlaylistVarFloat( "fw_harvester_regen_delay", FW_DEFAULT_HARVESTER_REGEN_DELAY ) ) && ( harvester.GetShieldHealth() < harvester.GetShieldHealthMax() ) )
		{
			if( !IsValid( fw_harvester.particleShield ) )
				generateShieldFX( fw_harvester )

			if( harvester.GetShieldHealth() == 0 )
				EmitSoundOnEntity( harvester, "coop_generator_shieldrecharge_start" )

			if (!isRegening)
			{
				EmitSoundOnEntity( harvester, "coop_generator_shieldrecharge_resume" )
				fw_harvester.harvesterShieldDown = false
				isRegening = true
			}

			float newShieldHealth = ( harvester.GetShieldHealthMax() / GetCurrentPlaylistVarFloat( "fw_harvester_regen_time", FW_DEFAULT_HARVESTER_REGEN_TIME ) * deltaTime ) + harvester.GetShieldHealth()

			// shield full
			if ( newShieldHealth >= harvester.GetShieldHealthMax() )
			{
				StopSoundOnEntity( harvester, "coop_generator_shieldrecharge_resume" )
				harvester.SetShieldHealth( harvester.GetShieldHealthMax() )
				EmitSoundOnEntity( harvester, "coop_generator_shieldrecharge_end" )
				PlayFactionDialogueToTeam( "fortwar_baseShieldUpFriendly", harvester.GetTeam() )
				isRegening = false
			}
			else
			{
				harvester.SetShieldHealth( newShieldHealth )
			}
		}
		else if ( ( ( currentTime-fw_harvester.lastDamage ) < GENERATOR_SHIELD_REGEN_DELAY ) && ( harvester.GetShieldHealth() < harvester.GetShieldHealthMax() ) )
		{
			isRegening = false
		}

		if ( ( lastShieldHealth > 0 ) && ( harvester.GetShieldHealth() == 0 ) )
		{
			EmitSoundOnEntity( harvester, "TitanWar_Harvester_ShieldDown" ) // add this
			EmitSoundOnEntity( harvester, "coop_generator_shielddown" )
		}

		lastShieldHealth = harvester.GetShieldHealth()
		lastTime = currentTime
		WaitFrame()
	}
}

void function HarvesterAlarm( HarvesterStruct fw_harvester )
{
	while( IsAlive( fw_harvester.harvester ) )
	{
		if( fw_harvester.harvester.GetShieldHealth() == 0 )
		{
			wait EmitSoundOnEntity( fw_harvester.harvester, "coop_generator_underattack_alarm" )
		}
		else
		{
			WaitFrame()
		}
	}
}

void function UpdateHarvesterHealth( int team )
{
	entity harvester
	if( team == TEAM_MILITIA )
		harvester = fw_harvesterMlt.harvester
	if( team == TEAM_IMC )
		harvester = fw_harvesterImc.harvester

	while( true )
	{
		if( IsValid(harvester) )
		{
			GameRules_SetTeamScore2( team, 1.0 * harvester.GetShieldHealth() / harvester.GetShieldHealthMax() * 100 )
			WaitFrame()
		}
		else // harvester down
		{
			int winnerTeam = GetOtherTeam(team)
			SetWinner( winnerTeam )
			//PlayFactionDialogueToTeam( "scoring_wonMercy", winnerTeam )
			//PlayFactionDialogueToTeam( "fortwar_matchLoss", team )
			GameRules_SetTeamScore2( team, 0 ) // force set score2 to 0( shield bar will empty )
			GameRules_SetTeamScore( team, 0 ) // force set score to 0( health 0% )
			break
		}
	}
}

///////////////////////////////////
///// HARVESTER FUNCTIONS END /////
///////////////////////////////////



//////////////////////////////////////
///// PLAYER OBJECTIVE FUNCTIONS /////
//////////////////////////////////////

const int APPLY_BATTERY_TEXT_INDEX = 96 // notify player to use batteries on turrets
const int EARN_TITAN_TEXT_INDEX = 100 // notify player to earn titans
const int CALL_IN_TITAN_TEXT_INDEX = 101 // notify player to call in titans in territory
const int EMBARK_TITAN_TEXT_INDEX = 102 // notify player to embark titans
const int ATTACK_HARVESTER_TEXT_INDEX = 103 // notify player to attack harvester

void function FWPlayerObjectiveState()
{
	thread FWPlayerObjectiveState_Threaded()
}

void function FWPlayerObjectiveState_Threaded()
{
	while( GamePlayingOrSuddenDeath() )
	{
		foreach( player in GetPlayerArray() )
		{
			entity petTitan = player.GetPetTitan()
			entity titanSoul
			if( IsValid( petTitan ) )
				titanSoul = petTitan.GetTitanSoul()

			if ( IsValid( GetBatteryOnBack( player ) ) )
				player.SetPlayerNetInt( "gameInfoStatusText", APPLY_BATTERY_TEXT_INDEX ) 
			else if ( IsTitanAvailable( player ) )
			{
				if( !player.s.notifiedTitanfall ) // first notification, also do a objective announcement
				{
					SetObjective( player, CALL_IN_TITAN_TEXT_INDEX )
					player.s.notifiedTitanfall = true
				}
				else
					player.SetPlayerNetInt( "gameInfoStatusText", CALL_IN_TITAN_TEXT_INDEX ) 
			}
			else if ( IsValid( petTitan ) )
				player.SetPlayerNetInt( "gameInfoStatusText", EMBARK_TITAN_TEXT_INDEX )
			else if ( IsAlive( player ) && !player.IsTitan() )
				player.SetPlayerNetInt( "gameInfoStatusText", EARN_TITAN_TEXT_INDEX )
			else if( !IsValid( titanSoul ) ) // titan died or player first embarked
				player.s.notifiedTitanfall = false

			if ( !IsAlive( player ) ) // don't show objetive for dying players
				player.SetPlayerNetInt( "gameInfoStatusText", -1 )
		}
		WaitFrame()
	}

	// game entered other state, clean this
	foreach( player in GetPlayerArray() )
	{
		player.SetPlayerNetInt( "gameInfoStatusText", -1 )
	}
}

void function SetObjective( entity player, int stringid )
{
	Remote_CallFunction_NonReplay( player, "ServerCallback_FW_SetObjective", stringid )
	player.SetPlayerNetInt( "gameInfoStatusText", stringid )
}

void function SetTitanObjective( entity player, entity titan )
{
	SetObjective( player, ATTACK_HARVESTER_TEXT_INDEX )
}

void function SetPilotObjective( entity player, entity titan )
{
	if( titan.GetTitanSoul().IsEjecting() ) // this time titan is ejecting
	{
		SetObjective( player, EARN_TITAN_TEXT_INDEX )
		player.s.notifiedTitanfall = false
	}
	else
		player.SetPlayerNetInt( "gameInfoStatusText", EMBARK_TITAN_TEXT_INDEX )
}

//////////////////////////////////////////
///// PLAYER OBJECTIVE FUNCTIONS END /////
//////////////////////////////////////////



/////////////////////////////////
///// BatteryPort Functions /////
/////////////////////////////////

void function FW_InitBatteryPort( entity batteryPort )
{
	batteryPort.kv.fadedist = 10000 // try not to fade
	InitTurretBatteryPort( batteryPort )

	batteryPort.s.relatedTurret <- null             // entity, for saving batteryPort's nearest turret

	entity turret = GetNearestMegaTurret( batteryPort ) // consider this is the port's related turret
	
	bool isBaseTurret = expect bool( turret.s.baseTurret )
	SetTeam( batteryPort, turret.GetTeam() )
	batteryPort.s.relatedTurret = turret
	batteryPort.s.isUsable <- FW_IsBatteryPortUsable
	batteryPort.s.useBattery <- FW_UseBattery
	if( isBaseTurret ) // this is a base turret!
	{
		batteryPort.s.hackAvaliable = false
		batteryPort.SetUsableByGroup( "friendlies pilot" ) // only show hint to friendlies
	} // it can never be hacked!
	
	turret.s.relatedBatteryPort = batteryPort // do it here
}

function FW_IsBatteryPortUsable( batteryPortvar, playervar ) //actually bool function( entity, entity )
{	
	entity batteryPort = expect entity( batteryPortvar )
	entity player = expect entity( playervar )
	entity turret = expect entity( batteryPort.s.relatedTurret )
    if( !IsValid( turret ) ) // turret has been destroyed!
        return false

	// get turret's settings, decide behavior
	bool validTeam = turret.GetTeam() == player.GetTeam() || turret.GetTeam() == TEAM_BOTH || turret.GetTeam() == TEAM_UNASSIGNED
    bool isBaseTurret = expect bool( turret.s.baseTurret )
    // is this port able to be hacked
    bool portHackAvaliable = expect bool( batteryPort.s.hackAvaliable )

    // player has a battery, team valid or able to hack && not a base turret
    return ( PlayerHasBattery( player ) && ( validTeam || ( portHackAvaliable && !isBaseTurret ) ) )
}

function FW_UseBattery( batteryPortvar, playervar ) //actually void function( entity, entity )
{
	entity batteryPort = expect entity( batteryPortvar )
	entity player = expect entity( playervar )
	// change turret settings
    entity turret = expect entity( batteryPort.s.relatedTurret ) // consider this is the port's related turret

    int playerTeam = player.GetTeam()
    bool turretReplaced = false
    bool sameTeam = turret.GetTeam() == player.GetTeam()

    if( !IsAlive( turret ) ) // turret has been killed!
    {
        turret = FW_ReplaceMegaTurret( turret )
        if( !IsValid( turret ) ) // replace failed!
            return
        batteryPort.s.relatedTurret = turret
        turretReplaced = true // if turret has been replaced, mostly reset team!
    }

    bool teamChanged = false
    bool isBaseTurret = expect bool( turret.s.baseTurret )
    if( ( !sameTeam || turretReplaced ) && !isBaseTurret ) // is there a need to change team?
    {
        SetTeam( turret, playerTeam )
        teamChanged = true
    }

    // restore turret health
    int newHealth = int ( min( turret.GetMaxHealth(), turret.GetHealth() + ( turret.GetMaxHealth() * GetCurrentPlaylistVarFloat( "fw_turret_fixed_health", TURRET_FIXED_HEALTH_PERCENTAGE ) ) ) )
    if( turretReplaced || teamChanged ) // replaced/hacked turret will spawn with 50% health
        newHealth = int ( turret.GetMaxHealth() * GetCurrentPlaylistVarFloat( "fw_turret_hacked_health", TURRET_HACKED_HEALTH_PERCENTAGE ) )
    // restore turret shield
    int newShield = int ( min( turret.GetShieldHealthMax(), turret.GetShieldHealth() + ( turret.GetShieldHealth() * GetCurrentPlaylistVarFloat( "fw_turret_fixed_shield", TURRET_FIXED_SHIELD_PERCENTAGE ) ) ) )
    if( turretReplaced || teamChanged ) // replaced/hacked turret will spawn with 50% shield
        newShield = int ( turret.GetShieldHealthMax() * GetCurrentPlaylistVarFloat( "fw_turret_hacked_shield", TURRET_HACKED_SHIELD_PERCENTAGE ) )
    // only do team score event if turret's shields down, encourage players to hack more turrets
    bool additionalScore = turret.GetShieldHealth() <= 0
    // this can be too much powerful
    turret.SetHealth( newHealth )
    turret.SetShieldHealth( newShield )

    // score event
    string scoreEvent = "FortWarForwardConstruction"
    int secondaryScore = POINTVALUE_FW_FORWARD_CONSTRUCTION
    if( isBaseTurret ) // this is a base turret
    {
        scoreEvent = "FortWarBaseConstruction"
        secondaryScore = POINTVALUE_FW_BASE_CONSTRUCTION
    }
    AddPlayerScore( player, scoreEvent, player ) // player themself gets more meter
    player.AddToPlayerGameStat( PGS_DEFENSE_SCORE, secondaryScore )

    // only do team score event if turret's shields down
    if( additionalScore )
    {
        // get turrets alive, for adding scores
        string teamTurretCount = GetTeamAliveTurretCount_ReturnString( playerTeam )
        foreach( entity friendly in GetPlayerArrayOfTeam( playerTeam ) )
            AddPlayerScore( friendly, "FortWarTeamTurretControlBonus_" + teamTurretCount, friendly )

        PlayFactionDialogueToTeam( "fortwar_turretShieldedByFriendlyPilot", playerTeam )
    }

}

// get nearest turret, consider it belongs to the port
entity function GetNearestMegaTurret( entity ent )
{
    array<entity> allTurrets = GetNPCArrayByClass( "npc_turret_mega" )
    entity turret = GetClosest( allTurrets, ent.GetOrigin() )
    return turret
}

// this will get english name of the count, since the "FortWarTeamTurretControlBonus_" score event uses it
string function GetTeamAliveTurretCount_ReturnString( int team )
{
    int turretCount
    foreach( entity turret in GetNPCArrayByClass( "npc_turret_mega" ) )
    {
        if( turret.GetTeam() == team && IsAlive( turret ) )
            turretCount += 1
    }

    switch( turretCount )
    {
        case 1:
            return "One"
        case 2:
            return "Two"
        case 3:
            return "Three"
        case 4:
            return "Four"
        case 5:
            return "Five"
        case 6:
            return "Six"
    }

    return ""
}

/////////////////////////////////////
///// BatteryPort Functions End /////
/////////////////////////////////////