1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
|
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2000-2006 Tim Angus
This file is part of Tremulous.
Tremulous is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Tremulous 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Tremulous; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "g_local.h"
/*
================
G_SetBuildableAnim
Triggers an animation client side
================
*/
void G_SetBuildableAnim( gentity_t *ent, buildableAnimNumber_t anim, qboolean force )
{
int localAnim = anim | ( ent->s.legsAnim & ANIM_TOGGLEBIT );
if( force )
localAnim |= ANIM_FORCEBIT;
// don't flip the togglebit more than once per frame
if( ent->animTime != level.time )
{
ent->animTime = level.time;
localAnim ^= ANIM_TOGGLEBIT;
}
ent->s.legsAnim = localAnim;
}
/*
================
G_SetIdleBuildableAnim
Set the animation to use whilst no other animations are running
================
*/
void G_SetIdleBuildableAnim( gentity_t *ent, buildableAnimNumber_t anim )
{
ent->s.torsoAnim = anim;
}
/*
===============
G_CheckSpawnPoint
Check if a spawn at a specified point is valid
===============
*/
gentity_t *G_CheckSpawnPoint( int spawnNum, vec3_t origin, vec3_t normal,
buildable_t spawn, vec3_t spawnOrigin )
{
float displacement;
vec3_t mins, maxs;
vec3_t cmins, cmaxs;
vec3_t localOrigin;
trace_t tr;
BG_BuildableBoundingBox( spawn, mins, maxs );
if( spawn == BA_A_SPAWN )
{
VectorSet( cmins, -MAX_ALIEN_BBOX, -MAX_ALIEN_BBOX, -MAX_ALIEN_BBOX );
VectorSet( cmaxs, MAX_ALIEN_BBOX, MAX_ALIEN_BBOX, MAX_ALIEN_BBOX );
displacement = ( maxs[ 2 ] + MAX_ALIEN_BBOX ) * M_ROOT3;
VectorMA( origin, displacement, normal, localOrigin );
}
else if( spawn == BA_H_SPAWN )
{
BG_ClassBoundingBox( PCL_HUMAN, cmins, cmaxs, NULL, NULL, NULL );
VectorCopy( origin, localOrigin );
localOrigin[ 2 ] += maxs[ 2 ] + fabs( cmins[ 2 ] ) + 1.0f;
}
else
return NULL;
trap_Trace( &tr, origin, NULL, NULL, localOrigin, spawnNum, MASK_SHOT );
if( tr.entityNum != ENTITYNUM_NONE )
return &g_entities[ tr.entityNum ];
trap_Trace( &tr, localOrigin, cmins, cmaxs, localOrigin, -1, MASK_PLAYERSOLID );
if( tr.entityNum != ENTITYNUM_NONE )
return &g_entities[ tr.entityNum ];
if( spawnOrigin != NULL )
VectorCopy( localOrigin, spawnOrigin );
return NULL;
}
#define POWER_REFRESH_TIME 2000
/*
================
G_FindPower
attempt to find power for self, return qtrue if successful
================
*/
qboolean G_FindPower( gentity_t *self )
{
int i, j;
gentity_t *ent, *ent2;
gentity_t *closestPower = NULL;
int distance = 0;
int minDistance = REPEATER_BASESIZE + 1;
vec3_t temp_v;
if( self->buildableTeam != TEAM_HUMANS )
return qfalse;
// Reactor is always powered
if( self->s.modelindex == BA_H_REACTOR )
{
self->parentNode = self;
return qtrue;
}
// Handle repeaters
if( self->s.modelindex == BA_H_REPEATER )
{
self->parentNode = G_Reactor( );
return self->parentNode != NULL;
}
// Reset parent
self->parentNode = NULL;
// Iterate through entities
for( i = MAX_CLIENTS, ent = g_entities + i; i < level.num_entities; i++, ent++ )
{
if( ent->s.eType != ET_BUILDABLE )
continue;
// If entity is a power item calculate the distance to it
if( ( ent->s.modelindex == BA_H_REACTOR || ent->s.modelindex == BA_H_REPEATER ) &&
ent->spawned && ent->powered && ent->health > 0 )
{
VectorSubtract( self->s.origin, ent->s.origin, temp_v );
distance = VectorLength( temp_v );
// Always prefer a reactor if there is one in range
if( ent->s.modelindex == BA_H_REACTOR && distance <= REACTOR_BASESIZE )
{
// Only power as much BP as the reactor can hold
if( self->s.modelindex != BA_NONE )
{
int buildPoints = g_humanBuildPoints.integer;
// Scan the buildables in the reactor zone
for( j = MAX_CLIENTS, ent2 = g_entities + j; j < level.num_entities; j++, ent2++ )
{
gentity_t *powerEntity;
if( ent2->s.eType != ET_BUILDABLE )
continue;
if( ent2 == self )
continue;
powerEntity = ent2->parentNode;
if( powerEntity && powerEntity->s.modelindex == BA_H_REACTOR && ( powerEntity == ent ) )
{
buildPoints -= BG_Buildable( ent2->s.modelindex )->buildPoints;
}
}
buildPoints -= level.humanBuildPointQueue;
buildPoints -= BG_Buildable( self->s.modelindex )->buildPoints;
if( buildPoints >= 0 )
{
self->parentNode = ent;
return qtrue;
}
else
{
// a buildable can still be built if it shares BP from two zones
// TODO: handle combined power zones here
}
}
// Dummy buildables don't need to look for zones
else
{
self->parentNode = ent;
return qtrue;
}
}
else if( distance < minDistance )
{
// It's a repeater, so check that enough BP will be available to power
// the buildable but only if self is a real buildable
if( self->s.modelindex != BA_NONE )
{
int buildPoints = g_humanRepeaterBuildPoints.integer;
// Scan the buildables in the repeater zone
for( j = MAX_CLIENTS, ent2 = g_entities + j; j < level.num_entities; j++, ent2++ )
{
gentity_t *powerEntity;
if( ent2->s.eType != ET_BUILDABLE )
continue;
if( ent2 == self )
continue;
powerEntity = ent2->parentNode;
if( powerEntity && powerEntity->s.modelindex == BA_H_REPEATER && ( powerEntity == ent ) )
{
buildPoints -= BG_Buildable( ent2->s.modelindex )->buildPoints;
}
}
if( self->usesZone && level.powerZones[ ent->zone ].active )
buildPoints -= level.powerZones[ ent->zone ].queuedBuildPoints;
buildPoints -= BG_Buildable( self->s.modelindex )->buildPoints;
if( buildPoints >= 0 )
{
closestPower = ent;
minDistance = distance;
}
else
{
// a buildable can still be built if it shares BP from two zones
// TODO: handle combined power zones here
}
}
else
{
// Dummy buildables don't need to look for zones
closestPower = ent;
minDistance = distance;
}
}
}
}
// If there were no power items nearby give up
if( closestPower )
{
self->parentNode = closestPower;
return qtrue;
}
else
return qfalse;
}
/*
================
G_PowerEntityForPoint
Simple wrapper to G_FindPower to find the entity providing
power for the specified point
================
*/
gentity_t *G_PowerEntityForPoint( const vec3_t origin )
{
gentity_t dummy;
dummy.parentNode = NULL;
dummy.buildableTeam = TEAM_HUMANS;
dummy.s.modelindex = BA_NONE;
VectorCopy( origin, dummy.s.origin );
if( G_FindPower( &dummy ) )
return dummy.parentNode;
else
return NULL;
}
/*
================
G_PowerEntityForEntity
Simple wrapper to G_FindPower to find the entity providing
power for the specified entity
================
*/
gentity_t *G_PowerEntityForEntity( gentity_t *ent )
{
if( G_FindPower( ent ) )
return ent->parentNode;
return NULL;
}
/*
================
G_IsPowered
Check if a location has power, returning the entity type
that is providing it
================
*/
buildable_t G_IsPowered( vec3_t origin )
{
gentity_t *ent = G_PowerEntityForPoint( origin );
if( ent )
return ent->s.modelindex;
else
return BA_NONE;
}
/*
==================
G_GetBuildPoints
Get the number of build points from a position
==================
*/
int G_GetBuildPoints( const vec3_t pos, team_t team, int extraDistance )
{
if( level.suddenDeath )
{
return 0;
}
else if( team == TEAM_ALIENS )
{
return level.alienBuildPoints;
}
else if( team == TEAM_HUMANS )
{
gentity_t *powerPoint = G_PowerEntityForPoint( pos );
if( powerPoint && powerPoint->s.modelindex == BA_H_REACTOR )
return level.humanBuildPoints;
if( powerPoint && powerPoint->s.modelindex == BA_H_REPEATER &&
powerPoint->usesZone && level.powerZones[ powerPoint->zone ].active )
{
return level.powerZones[ powerPoint->zone ].totalBuildPoints -
level.powerZones[ powerPoint->zone ].queuedBuildPoints;
}
// Return the BP of the main zone by default
return level.humanBuildPoints;
}
return 0;
// TODO: handle combined power zones in G_FindPower. Until then, use the closest zone and prefer the reactor
#if 0
int i;
gentity_t *ent;
int distance = 0;
vec3_t temp_v;
int buildPoints = 0;
qboolean zoneFound = qfalse;
if( level.suddenDeath )
{
buildPoints = 0;
}
else if( team == TEAM_HUMANS )
{
// Iterate through entities
for( i = MAX_CLIENTS, ent = g_entities + i; i < level.num_entities; i++, ent++ )
{
ent = &g_entities[ i ];
VectorSubtract( pos, ent->s.origin, temp_v );
distance = VectorLength( temp_v );
if( ent->s.modelindex == BA_H_REACTOR && distance <= REACTOR_BASESIZE + extraDistance )
{
// Reactor is in range
zoneFound = qtrue;
buildPoints += level.humanBuildPoints;
}
else if( ent->s.modelindex == BA_H_REPEATER && distance <= REPEATER_BASESIZE + extraDistance )
{
if( ent->usesZone && level.powerZones[ent->zone].active )
{
zone_t *zone = &level.powerZones[ent->zone];
zoneFound = qtrue;
buildPoints += zone->totalBuildPoints - zone->queuedBuildPoints;
}
}
}
}
else if( team == TEAM_ALIENS )
{
buildPoints = level.alienBuildPoints;
}
if( buildPoints < 0 )
buildPoints = 0;
if( !zoneFound )
buildPoints = level.humanBuildPoints; // if the player isn't in a zone, show the number of BP of the main zone
return buildPoints;
#endif
}
/*
==================
G_InPowerZone
See if a buildable is inside of another power zone
(This doesn't check if power zones overlap)
==================
*/
qboolean G_InPowerZone( gentity_t *self )
{
int i;
gentity_t *ent;
int distance;
vec3_t temp_v;
for( i = MAX_CLIENTS, ent = g_entities + i; i < level.num_entities; i++, ent++ )
{
if( ent->s.eType != ET_BUILDABLE )
continue;
if( ent == self )
continue;
if( !ent->spawned )
continue;
if( ent->health <= 0 )
continue;
// if entity is a power item calculate the distance to it
if( ( ent->s.modelindex == BA_H_REACTOR || ent->s.modelindex == BA_H_REPEATER ) &&
ent->spawned && ent->powered )
{
VectorSubtract( self->s.origin, ent->s.origin, temp_v );
distance = VectorLength( temp_v );
if( ent->s.modelindex == BA_H_REACTOR && distance <= REACTOR_BASESIZE )
return qtrue;
else if( ent->s.modelindex == BA_H_REPEATER && distance <= REPEATER_BASESIZE )
return qtrue;
}
}
return qfalse;
}
/*
================
G_FindDCC
attempt to find a controlling DCC for self, return number found
================
*/
int G_FindDCC( gentity_t *self )
{
int i;
gentity_t *ent;
int distance = 0;
vec3_t temp_v;
int foundDCC = 0;
if( self->buildableTeam != TEAM_HUMANS )
return 0;
//iterate through entities
for( i = MAX_CLIENTS, ent = g_entities + i; i < level.num_entities; i++, ent++ )
{
if( ent->s.eType != ET_BUILDABLE )
continue;
//if entity is a dcc calculate the distance to it
if( ent->s.modelindex == BA_H_DCC && ent->spawned )
{
VectorSubtract( self->s.origin, ent->s.origin, temp_v );
distance = VectorLength( temp_v );
if( distance < DC_RANGE && ent->powered )
{
foundDCC++;
}
}
}
return foundDCC;
}
/*
================
G_IsDCCBuilt
See if any powered DCC exists
================
*/
qboolean G_IsDCCBuilt( void )
{
int i;
gentity_t *ent;
for( i = MAX_CLIENTS, ent = g_entities + i; i < level.num_entities; i++, ent++ )
{
if( ent->s.eType != ET_BUILDABLE )
continue;
if( ent->s.modelindex != BA_H_DCC )
continue;
if( !ent->spawned )
continue;
if( ent->health <= 0 )
continue;
return qtrue;
}
return qfalse;
}
/*
================
G_Reactor
G_Overmind
Since there's only one of these and we quite often want to find them, cache the
results, but check them for validity each time
The code here will break if more than one reactor or overmind is allowed, even
if one of them is dead/unspawned
================
*/
static gentity_t *G_FindBuildable( buildable_t buildable );
gentity_t *G_Reactor( void )
{
static gentity_t *rc;
// If cache becomes invalid renew it
if( !rc || rc->s.eType != ET_BUILDABLE || rc->s.modelindex == BA_H_REACTOR )
rc = G_FindBuildable( BA_H_REACTOR );
// If we found it and it's alive, return it
if( rc && rc->spawned && rc->health > 0 )
return rc;
return NULL;
}
gentity_t *G_Overmind( void )
{
static gentity_t *om;
// If cache becomes invalid renew it
if( !om || om->s.eType != ET_BUILDABLE || om->s.modelindex == BA_A_OVERMIND )
om = G_FindBuildable( BA_A_OVERMIND );
// If we found it and it's alive, return it
if( om && om->spawned && om->health > 0 )
return om;
return NULL;
}
/*
================
G_FindCreep
attempt to find creep for self, return qtrue if successful
================
*/
qboolean G_FindCreep( gentity_t *self )
{
int i;
gentity_t *ent;
gentity_t *closestSpawn = NULL;
int distance = 0;
int minDistance = 10000;
vec3_t temp_v;
//don't check for creep if flying through the air
if( self->s.groundEntityNum == -1 )
return qtrue;
//if self does not have a parentNode or it's parentNode is invalid find a new one
if( self->client || self->parentNode == NULL || !self->parentNode->inuse ||
self->parentNode->health <= 0 )
{
for( i = MAX_CLIENTS, ent = g_entities + i; i < level.num_entities; i++, ent++ )
{
if( ent->s.eType != ET_BUILDABLE )
continue;
if( ( ent->s.modelindex == BA_A_SPAWN ||
ent->s.modelindex == BA_A_OVERMIND ) &&
ent->spawned && ent->health > 0 )
{
VectorSubtract( self->s.origin, ent->s.origin, temp_v );
distance = VectorLength( temp_v );
if( distance < minDistance )
{
closestSpawn = ent;
minDistance = distance;
}
}
}
if( minDistance <= CREEP_BASESIZE )
{
if( !self->client )
self->parentNode = closestSpawn;
return qtrue;
}
else
return qfalse;
}
if( self->client )
return qfalse;
//if we haven't returned by now then we must already have a valid parent
return qtrue;
}
/*
================
G_IsCreepHere
simple wrapper to G_FindCreep to check if a location has creep
================
*/
qboolean G_IsCreepHere( vec3_t origin )
{
gentity_t dummy;
memset( &dummy, 0, sizeof( gentity_t ) );
dummy.parentNode = NULL;
dummy.s.modelindex = BA_NONE;
VectorCopy( origin, dummy.s.origin );
return G_FindCreep( &dummy );
}
/*
================
G_CreepSlow
Set any nearby humans' SS_CREEPSLOWED flag
================
*/
static void G_CreepSlow( gentity_t *self )
{
int entityList[ MAX_GENTITIES ];
vec3_t range;
vec3_t mins, maxs;
int i, num;
gentity_t *enemy;
buildable_t buildable = self->s.modelindex;
float creepSize = (float)BG_Buildable( buildable )->creepSize;
VectorSet( range, creepSize, creepSize, creepSize );
VectorAdd( self->s.origin, range, maxs );
VectorSubtract( self->s.origin, range, mins );
//find humans
num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES );
for( i = 0; i < num; i++ )
{
enemy = &g_entities[ entityList[ i ] ];
if( enemy->client && enemy->client->ps.stats[ STAT_TEAM ] == TEAM_HUMANS &&
enemy->client->ps.groundEntityNum != ENTITYNUM_NONE )
{
enemy->client->ps.stats[ STAT_STATE ] |= SS_CREEPSLOWED;
enemy->client->lastCreepSlowTime = level.time;
}
}
}
/*
================
nullDieFunction
hack to prevent compilers complaining about function pointer -> NULL conversion
================
*/
static void nullDieFunction( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int mod )
{
}
//==================================================================================
/*
================
AGeneric_CreepRecede
Called when an alien buildable dies
================
*/
void AGeneric_CreepRecede( gentity_t *self )
{
//if the creep just died begin the recession
if( !( self->s.eFlags & EF_DEAD ) )
{
self->s.eFlags |= EF_DEAD;
G_QueueBuildPoints( self );
G_AddEvent( self, EV_BUILD_DESTROY, 0 );
if( self->spawned )
self->s.time = -level.time;
else
self->s.time = -( level.time -
(int)( (float)CREEP_SCALEDOWN_TIME *
( 1.0f - ( (float)( level.time - self->buildTime ) /
(float)BG_Buildable( self->s.modelindex )->buildTime ) ) ) );
}
//creep is still receeding
if( ( self->timestamp + 10000 ) > level.time )
self->nextthink = level.time + 500;
else //creep has died
G_FreeEntity( self );
}
/*
================
AGeneric_Blast
Called when an Alien buildable explodes after dead state
================
*/
void AGeneric_Blast( gentity_t *self )
{
vec3_t dir;
VectorCopy( self->s.origin2, dir );
//do a bit of radius damage
G_SelectiveRadiusDamage( self->s.pos.trBase, self, self->splashDamage,
self->splashRadius, self, self->splashMethodOfDeath,
TEAM_ALIENS );
//pretty events and item cleanup
self->s.eFlags |= EF_NODRAW; //don't draw the model once it's destroyed
G_AddEvent( self, EV_ALIEN_BUILDABLE_EXPLOSION, DirToByte( dir ) );
self->timestamp = level.time;
self->think = AGeneric_CreepRecede;
self->nextthink = level.time + 500;
self->r.contents = 0; //stop collisions...
trap_LinkEntity( self ); //...requires a relink
}
/*
================
AGeneric_Die
Called when an Alien buildable is killed and enters a brief dead state prior to
exploding.
================
*/
void AGeneric_Die( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int mod )
{
G_RewardAttackers( self );
G_SetBuildableAnim( self, BANIM_DESTROY1, qtrue );
G_SetIdleBuildableAnim( self, BANIM_DESTROYED );
self->die = nullDieFunction;
self->think = AGeneric_Blast;
self->s.eFlags &= ~EF_FIRING; //prevent any firing effects
self->powered = qfalse;
if( self->spawned )
self->nextthink = level.time + 5000;
else
self->nextthink = level.time; //blast immediately
G_LogDestruction( self, attacker, mod );
}
/*
================
AGeneric_CreepCheck
Tests for creep and kills the buildable if there is none
================
*/
void AGeneric_CreepCheck( gentity_t *self )
{
gentity_t *spawn;
spawn = self->parentNode;
if( !G_FindCreep( self ) )
{
if( spawn && self->killedBy != ENTITYNUM_NONE )
G_Damage( self, NULL, g_entities + self->killedBy, NULL, NULL,
self->health, 0, MOD_NOCREEP );
else
G_Damage( self, NULL, NULL, NULL, NULL, self->health, 0, MOD_NOCREEP );
return;
}
G_CreepSlow( self );
}
/*
================
AGeneric_Think
A generic think function for Alien buildables
================
*/
void AGeneric_Think( gentity_t *self )
{
self->powered = G_Overmind( ) != NULL;
self->nextthink = level.time + BG_Buildable( self->s.modelindex )->nextthink;
AGeneric_CreepCheck( self );
}
/*
================
AGeneric_Pain
A generic pain function for Alien buildables
================
*/
void AGeneric_Pain( gentity_t *self, gentity_t *attacker, int damage )
{
if( self->health <= 0 )
return;
// Alien buildables only have the first pain animation defined
G_SetBuildableAnim( self, BANIM_PAIN1, qfalse );
}
//==================================================================================
/*
================
ASpawn_Die
Called when an alien spawn dies
================
*/
void ASpawn_Die( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int mod )
{
int i;
AGeneric_Die( self, inflictor, attacker, damage, mod );
// All supported structures that no longer have creep will have been killed
// by whoever killed this structure
for( i = MAX_CLIENTS; i < level.num_entities; i++ )
{
gentity_t *ent = g_entities + i;
if( !ent->inuse || ent->health <= 0 || ent->s.eType != ET_BUILDABLE ||
ent->parentNode != self )
continue;
ent->killedBy = attacker - g_entities;
}
}
/*
================
ASpawn_Think
think function for Alien Spawn
================
*/
void ASpawn_Think( gentity_t *self )
{
gentity_t *ent;
if( self->spawned )
{
//only suicide if at rest
if( self->s.groundEntityNum )
{
if( ( ent = G_CheckSpawnPoint( self->s.number, self->s.origin,
self->s.origin2, BA_A_SPAWN, NULL ) ) != NULL )
{
// If the thing blocking the spawn is a buildable, kill it.
// If it's part of the map, kill self.
if( ent->s.eType == ET_BUILDABLE )
{
G_Damage( ent, NULL, NULL, NULL, NULL, 10000, 0, MOD_SUICIDE );
G_SetBuildableAnim( self, BANIM_SPAWN1, qtrue );
}
else if( ent->s.number == ENTITYNUM_WORLD || ent->s.eType == ET_MOVER )
{
G_Damage( self, NULL, NULL, NULL, NULL, 10000, 0, MOD_SUICIDE );
return;
}
if( ent->s.eType == ET_CORPSE )
G_FreeEntity( ent ); //quietly remove
}
}
}
G_CreepSlow( self );
self->nextthink = level.time + BG_Buildable( self->s.modelindex )->nextthink;
}
//==================================================================================
#define OVERMIND_ATTACK_PERIOD 10000
#define OVERMIND_DYING_PERIOD 5000
#define OVERMIND_SPAWNS_PERIOD 30000
/*
================
AOvermind_Think
Think function for Alien Overmind
================
*/
void AOvermind_Think( gentity_t *self )
{
vec3_t range = { OVERMIND_ATTACK_RANGE, OVERMIND_ATTACK_RANGE, OVERMIND_ATTACK_RANGE };
vec3_t mins, maxs;
int i;
VectorAdd( self->s.origin, range, maxs );
VectorSubtract( self->s.origin, range, mins );
if( self->spawned && ( self->health > 0 ) )
{
//do some damage
if( G_SelectiveRadiusDamage( self->s.pos.trBase, self, self->splashDamage,
self->splashRadius, self, MOD_OVERMIND, TEAM_ALIENS ) )
{
self->timestamp = level.time;
G_SetBuildableAnim( self, BANIM_ATTACK1, qfalse );
}
// just in case an egg finishes building after we tell overmind to stfu
if( level.numAlienSpawns > 0 )
level.overmindMuted = qfalse;
// shut up during intermission
if( level.intermissiontime )
level.overmindMuted = qtrue;
//low on spawns
if( !level.overmindMuted && level.numAlienSpawns <= 0 &&
level.time > self->overmindSpawnsTimer )
{
qboolean haveBuilder = qfalse;
gentity_t *builder;
self->overmindSpawnsTimer = level.time + OVERMIND_SPAWNS_PERIOD;
G_BroadcastEvent( EV_OVERMIND_SPAWNS, 0 );
for( i = 0; i < level.numConnectedClients; i++ )
{
builder = &g_entities[ level.sortedClients[ i ] ];
if( builder->health > 0 &&
( builder->client->pers.classSelection == PCL_ALIEN_BUILDER0 ||
builder->client->pers.classSelection == PCL_ALIEN_BUILDER0_UPG ) )
{
haveBuilder = qtrue;
break;
}
}
// aliens now know they have no eggs, but they're screwed, so stfu
if( !haveBuilder || G_TimeTilSuddenDeath( ) <= 0 )
level.overmindMuted = qtrue;
}
//overmind dying
if( self->health < ( OVERMIND_HEALTH / 10.0f ) && level.time > self->overmindDyingTimer )
{
self->overmindDyingTimer = level.time + OVERMIND_DYING_PERIOD;
G_BroadcastEvent( EV_OVERMIND_DYING, 0 );
}
//overmind under attack
if( self->health < self->lastHealth && level.time > self->overmindAttackTimer )
{
self->overmindAttackTimer = level.time + OVERMIND_ATTACK_PERIOD;
G_BroadcastEvent( EV_OVERMIND_ATTACK, 0 );
}
self->lastHealth = self->health;
}
else
self->overmindSpawnsTimer = level.time + OVERMIND_SPAWNS_PERIOD;
G_CreepSlow( self );
self->nextthink = level.time + BG_Buildable( self->s.modelindex )->nextthink;
}
//==================================================================================
/*
================
ABarricade_Pain
Barricade pain animation depends on shrunk state
================
*/
void ABarricade_Pain( gentity_t *self, gentity_t *attacker, int damage )
{
if( self->health <= 0 )
return;
if( !self->shrunkTime )
G_SetBuildableAnim( self, BANIM_PAIN1, qfalse );
else
G_SetBuildableAnim( self, BANIM_PAIN2, qfalse );
}
/*
================
ABarricade_Shrink
Set shrink state for a barricade. When unshrinking, checks to make sure there
is enough room.
================
*/
void ABarricade_Shrink( gentity_t *self, qboolean shrink )
{
if ( !self->spawned || self->health <= 0 )
shrink = qtrue;
if ( shrink && self->shrunkTime )
{
int anim;
// We need to make sure that the animation has been set to shrunk mode
// because we start out shrunk but with the construct animation when built
self->shrunkTime = level.time;
anim = self->s.torsoAnim & ~( ANIM_FORCEBIT | ANIM_TOGGLEBIT );
if ( self->spawned && self->health > 0 && anim != BANIM_DESTROYED )
{
G_SetIdleBuildableAnim( self, BANIM_DESTROYED );
G_SetBuildableAnim( self, BANIM_ATTACK1, qtrue );
}
return;
}
if ( !shrink && ( !self->shrunkTime ||
level.time < self->shrunkTime + BARRICADE_SHRINKTIMEOUT ) )
return;
BG_BuildableBoundingBox( BA_A_BARRICADE, self->r.mins, self->r.maxs );
if ( shrink )
{
self->r.maxs[ 2 ] = (int)( self->r.maxs[ 2 ] * BARRICADE_SHRINKPROP );
self->shrunkTime = level.time;
// shrink animation, the destroy animation is used
if ( self->spawned && self->health > 0 )
{
G_SetBuildableAnim( self, BANIM_ATTACK1, qtrue );
G_SetIdleBuildableAnim( self, BANIM_DESTROYED );
}
}
else
{
trace_t tr;
int anim;
trap_Trace( &tr, self->s.origin, self->r.mins, self->r.maxs,
self->s.origin, self->s.number, MASK_PLAYERSOLID );
if ( tr.startsolid || tr.fraction < 1.0f )
{
self->r.maxs[ 2 ] = (int)( self->r.maxs[ 2 ] * BARRICADE_SHRINKPROP );
return;
}
self->shrunkTime = 0;
// unshrink animation, IDLE2 has been hijacked for this
anim = self->s.legsAnim & ~( ANIM_FORCEBIT | ANIM_TOGGLEBIT );
if ( self->spawned && self->health > 0 &&
anim != BANIM_CONSTRUCT1 && anim != BANIM_CONSTRUCT2 )
{
G_SetIdleBuildableAnim( self, BG_Buildable( BA_A_BARRICADE )->idleAnim );
G_SetBuildableAnim( self, BANIM_ATTACK2, qtrue );
}
}
// a change in size requires a relink
if ( self->spawned )
trap_LinkEntity( self );
}
/*
================
ABarricade_Die
Called when an alien barricade dies
================
*/
void ABarricade_Die( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int mod )
{
AGeneric_Die( self, inflictor, attacker, damage, mod );
ABarricade_Shrink( self, qtrue );
}
/*
================
ABarricade_Think
Think function for Alien Barricade
================
*/
void ABarricade_Think( gentity_t *self )
{
AGeneric_Think( self );
// Shrink if unpowered
ABarricade_Shrink( self, !self->powered );
}
/*
================
ABarricade_Touch
Barricades shrink when they are come into contact with an Alien that can
pass through
================
*/
void ABarricade_Touch( gentity_t *self, gentity_t *other, trace_t *trace )
{
gclient_t *client = other->client;
int client_z, min_z;
if( !client || client->pers.teamSelection != TEAM_ALIENS )
return;
// Client must be high enough to pass over. Note that STEPSIZE (18) is
// hardcoded here because we don't include bg_local.h!
client_z = other->s.origin[ 2 ] + other->r.mins[ 2 ];
min_z = self->s.origin[ 2 ] - 18 +
(int)( self->r.maxs[ 2 ] * BARRICADE_SHRINKPROP );
if( client_z < min_z )
return;
ABarricade_Shrink( self, qtrue );
}
//==================================================================================
/*
================
AAcidTube_Think
Think function for Alien Acid Tube
================
*/
void AAcidTube_Think( gentity_t *self )
{
int entityList[ MAX_GENTITIES ];
vec3_t range = { ACIDTUBE_RANGE, ACIDTUBE_RANGE, ACIDTUBE_RANGE };
vec3_t mins, maxs;
int i, num;
gentity_t *enemy;
AGeneric_Think( self );
VectorAdd( self->s.origin, range, maxs );
VectorSubtract( self->s.origin, range, mins );
// attack nearby humans
if( self->spawned && self->health > 0 && self->powered )
{
num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES );
for( i = 0; i < num; i++ )
{
enemy = &g_entities[ entityList[ i ] ];
if( !G_Visible( self, enemy, CONTENTS_SOLID ) )
continue;
if( enemy->client && enemy->client->ps.stats[ STAT_TEAM ] == TEAM_HUMANS )
{
// start the attack animation
if( level.time >= self->timestamp + ACIDTUBE_REPEAT_ANIM )
{
self->timestamp = level.time;
G_SetBuildableAnim( self, BANIM_ATTACK1, qfalse );
G_AddEvent( self, EV_ALIEN_ACIDTUBE, DirToByte( self->s.origin2 ) );
}
G_SelectiveRadiusDamage( self->s.pos.trBase, self, ACIDTUBE_DAMAGE,
ACIDTUBE_RANGE, self, MOD_ATUBE, TEAM_ALIENS );
self->nextthink = level.time + ACIDTUBE_REPEAT;
return;
}
}
}
}
//==================================================================================
/*
================
AHive_CheckTarget
Returns true and fires the hive missile if the target is valid
================
*/
static qboolean AHive_CheckTarget( gentity_t *self, gentity_t *enemy )
{
trace_t trace;
vec3_t tip_origin, dirToTarget;
// Check if this is a valid target
if( enemy->health <= 0 || !enemy->client ||
enemy->client->ps.stats[ STAT_TEAM ] != TEAM_HUMANS )
return qfalse;
// Check if the tip of the hive can see the target
VectorMA( self->s.pos.trBase, self->r.maxs[ 2 ], self->s.origin2,
tip_origin );
trap_Trace( &trace, tip_origin, NULL, NULL, enemy->s.pos.trBase,
self->s.number, MASK_SHOT );
if( trace.fraction < 1.0f && trace.entityNum != enemy->s.number )
return qfalse;
self->active = qtrue;
self->target_ent = enemy;
self->timestamp = level.time + HIVE_REPEAT;
VectorSubtract( enemy->s.pos.trBase, self->s.pos.trBase, dirToTarget );
VectorNormalize( dirToTarget );
vectoangles( dirToTarget, self->turretAim );
// Fire at target
FireWeapon( self );
G_SetBuildableAnim( self, BANIM_ATTACK1, qfalse );
return qtrue;
}
/*
================
AHive_Think
Think function for Alien Hive
================
*/
void AHive_Think( gentity_t *self )
{
AGeneric_Think( self );
// Hive missile hasn't returned in HIVE_REPEAT seconds, forget about it
if( self->timestamp < level.time )
self->active = qfalse;
// Find a target to attack
if( self->spawned && !self->active && self->powered )
{
int i, num, entityList[ MAX_GENTITIES ];
vec3_t mins, maxs,
range = { HIVE_SENSE_RANGE, HIVE_SENSE_RANGE, HIVE_SENSE_RANGE };
VectorAdd( self->s.origin, range, maxs );
VectorSubtract( self->s.origin, range, mins );
num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES );
for( i = 0; i < num; i++ )
{
if( AHive_CheckTarget( self, g_entities + entityList[ i ] ) )
return;
}
}
}
/*
================
AHive_Pain
pain function for Alien Hive
================
*/
void AHive_Pain( gentity_t *self, gentity_t *attacker, int damage )
{
if( self->spawned && self->powered && !self->active )
AHive_CheckTarget( self, attacker );
G_SetBuildableAnim( self, BANIM_PAIN1, qfalse );
}
//==================================================================================
#define HOVEL_TRACE_DEPTH 128.0f
/*
================
AHovel_Blocked
Is this hovel entrance blocked?
================
*/
qboolean AHovel_Blocked( gentity_t *hovel, gentity_t *player, qboolean provideExit )
{
vec3_t forward, normal, origin, start, end, angles, hovelMaxs;
vec3_t mins, maxs;
float displacement;
trace_t tr;
BG_BuildableBoundingBox( BA_A_HOVEL, NULL, hovelMaxs );
BG_ClassBoundingBox( player->client->ps.stats[ STAT_CLASS ],
mins, maxs, NULL, NULL, NULL );
VectorCopy( hovel->s.origin2, normal );
AngleVectors( hovel->s.angles, forward, NULL, NULL );
VectorInverse( forward );
displacement = VectorMaxComponent( maxs ) * M_ROOT3 +
VectorMaxComponent( hovelMaxs ) * M_ROOT3 + 1.0f;
VectorMA( hovel->s.origin, displacement, forward, origin );
VectorCopy( hovel->s.origin, start );
VectorCopy( origin, end );
// see if there's something between the hovel and its exit
// (eg built right up against a wall)
trap_Trace( &tr, start, NULL, NULL, end, player->s.number, MASK_PLAYERSOLID );
if( tr.fraction < 1.0f )
return qtrue;
vectoangles( forward, angles );
VectorMA( origin, HOVEL_TRACE_DEPTH, normal, start );
//compute a place up in the air to start the real trace
trap_Trace( &tr, origin, mins, maxs, start, player->s.number, MASK_PLAYERSOLID );
VectorMA( origin, ( HOVEL_TRACE_DEPTH * tr.fraction ) - 1.0f, normal, start );
VectorMA( origin, -HOVEL_TRACE_DEPTH, normal, end );
trap_Trace( &tr, start, mins, maxs, end, player->s.number, MASK_PLAYERSOLID );
VectorCopy( tr.endpos, origin );
trap_Trace( &tr, origin, mins, maxs, origin, player->s.number, MASK_PLAYERSOLID );
if( tr.fraction < 1.0f )
return qtrue;
if( provideExit )
{
G_SetOrigin( player, origin );
VectorCopy( origin, player->client->ps.origin );
// nudge
VectorMA( normal, 200.0f, forward, player->client->ps.velocity );
G_SetClientViewAngle( player, angles );
}
return qfalse;
}
/*
================
APropHovel_Blocked
Wrapper to test a hovel placement for validity
================
*/
static qboolean APropHovel_Blocked( vec3_t origin, vec3_t angles, vec3_t normal,
gentity_t *player )
{
gentity_t hovel;
VectorCopy( origin, hovel.s.origin );
VectorCopy( angles, hovel.s.angles );
VectorCopy( normal, hovel.s.origin2 );
return AHovel_Blocked( &hovel, player, qfalse );
}
/*
================
AHovel_Use
Called when an alien uses a hovel
================
*/
void AHovel_Use( gentity_t *self, gentity_t *other, gentity_t *activator )
{
vec3_t hovelOrigin, hovelAngles, inverseNormal;
if( self->spawned && self->powered )
{
if( self->active )
{
//this hovel is in use
G_TriggerMenu( activator->client->ps.clientNum, MN_A_HOVEL_OCCUPIED );
}
else if( ( ( activator->client->ps.stats[ STAT_CLASS ] == PCL_ALIEN_BUILDER0 ) ||
( activator->client->ps.stats[ STAT_CLASS ] == PCL_ALIEN_BUILDER0_UPG ) ) &&
activator->health > 0 && self->health > 0 )
{
if( AHovel_Blocked( self, activator, qfalse ) )
{
//you can get in, but you can't get out
G_TriggerMenu( activator->client->ps.clientNum, MN_A_HOVEL_BLOCKED );
return;
}
self->active = qtrue;
G_SetBuildableAnim( self, BANIM_ATTACK1, qfalse );
//prevent lerping
activator->client->ps.eFlags ^= EF_TELEPORT_BIT;
activator->client->ps.eFlags |= EF_NODRAW;
G_UnlaggedClear( activator );
// Cancel pending suicides
activator->suicideTime = 0;
activator->client->ps.stats[ STAT_STATE ] |= SS_HOVELING;
activator->client->hovel = self;
self->builder = activator;
VectorCopy( self->s.pos.trBase, hovelOrigin );
VectorMA( hovelOrigin, 128.0f, self->s.origin2, hovelOrigin );
VectorCopy( self->s.origin2, inverseNormal );
VectorInverse( inverseNormal );
vectoangles( inverseNormal, hovelAngles );
VectorCopy( activator->s.pos.trBase, activator->client->hovelOrigin );
G_SetOrigin( activator, hovelOrigin );
VectorCopy( hovelOrigin, activator->client->ps.origin );
G_SetClientViewAngle( activator, hovelAngles );
}
}
}
/*
================
AHovel_Think
Think for alien hovel
================
*/
void AHovel_Think( gentity_t *self )
{
AGeneric_Think( self );
if( self->spawned )
{
if( self->active )
G_SetIdleBuildableAnim( self, BANIM_IDLE2 );
else
G_SetIdleBuildableAnim( self, BANIM_IDLE1 );
}
}
/*
================
AHovel_Die
Die for alien hovel
================
*/
void AHovel_Die( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int mod )
{
//if the hovel is occupied free the occupant
if( self->active )
{
gentity_t *builder = self->builder;
vec3_t newOrigin;
vec3_t newAngles;
VectorCopy( self->s.angles, newAngles );
newAngles[ ROLL ] = 0;
VectorCopy( self->s.origin, newOrigin );
VectorMA( newOrigin, 1.0f, self->s.origin2, newOrigin );
//prevent lerping
builder->client->ps.eFlags ^= EF_TELEPORT_BIT;
builder->client->ps.eFlags &= ~EF_NODRAW;
G_UnlaggedClear( builder );
G_SetOrigin( builder, newOrigin );
VectorCopy( newOrigin, builder->client->ps.origin );
G_SetClientViewAngle( builder, newAngles );
//client leaves hovel
builder->client->ps.stats[ STAT_STATE ] &= ~SS_HOVELING;
}
AGeneric_Die( self, inflictor, attacker, damage, mod );
self->nextthink = level.time + 100;
}
//==================================================================================
/*
================
ABooster_Touch
Called when an alien touches a booster
================
*/
void ABooster_Touch( gentity_t *self, gentity_t *other, trace_t *trace )
{
gclient_t *client = other->client;
if( !self->spawned || !self->powered || self->health <= 0 )
return;
if( !client )
return;
if( client->ps.stats[ STAT_TEAM ] == TEAM_HUMANS )
return;
client->ps.stats[ STAT_STATE ] |= SS_BOOSTED;
client->boostedTime = level.time;
}
//==================================================================================
#define TRAPPER_ACCURACY 10 // lower is better
/*
================
ATrapper_FireOnEnemy
Used by ATrapper_Think to fire at enemy
================
*/
void ATrapper_FireOnEnemy( gentity_t *self, int firespeed, float range )
{
gentity_t *enemy = self->enemy;
vec3_t dirToTarget;
vec3_t halfAcceleration, thirdJerk;
float distanceToTarget = BG_Buildable( self->s.modelindex )->turretRange;
int lowMsec = 0;
int highMsec = (int)( (
( ( distanceToTarget * LOCKBLOB_SPEED ) +
( distanceToTarget * BG_Class( enemy->client->ps.stats[ STAT_CLASS ] )->speed ) ) /
( LOCKBLOB_SPEED * LOCKBLOB_SPEED ) ) * 1000.0f );
VectorScale( enemy->acceleration, 1.0f / 2.0f, halfAcceleration );
VectorScale( enemy->jerk, 1.0f / 3.0f, thirdJerk );
// highMsec and lowMsec can only move toward
// one another, so the loop must terminate
while( highMsec - lowMsec > TRAPPER_ACCURACY )
{
int partitionMsec = ( highMsec + lowMsec ) / 2;
float time = (float)partitionMsec / 1000.0f;
float projectileDistance = LOCKBLOB_SPEED * time;
VectorMA( enemy->s.pos.trBase, time, enemy->s.pos.trDelta, dirToTarget );
VectorMA( dirToTarget, time * time, halfAcceleration, dirToTarget );
VectorMA( dirToTarget, time * time * time, thirdJerk, dirToTarget );
VectorSubtract( dirToTarget, self->s.pos.trBase, dirToTarget );
distanceToTarget = VectorLength( dirToTarget );
if( projectileDistance < distanceToTarget )
lowMsec = partitionMsec;
else if( projectileDistance > distanceToTarget )
highMsec = partitionMsec;
else if( projectileDistance == distanceToTarget )
break; // unlikely to happen
}
VectorNormalize( dirToTarget );
vectoangles( dirToTarget, self->turretAim );
//fire at target
FireWeapon( self );
G_SetBuildableAnim( self, BANIM_ATTACK1, qfalse );
self->count = level.time + firespeed;
}
/*
================
ATrapper_CheckTarget
Used by ATrapper_Think to check enemies for validity
================
*/
qboolean ATrapper_CheckTarget( gentity_t *self, gentity_t *target, int range )
{
vec3_t distance;
trace_t trace;
if( !target ) // Do we have a target?
return qfalse;
if( !target->inuse ) // Does the target still exist?
return qfalse;
if( target == self ) // is the target us?
return qfalse;
if( !target->client ) // is the target a bot or player?
return qfalse;
if( target->client->ps.stats[ STAT_TEAM ] == TEAM_ALIENS ) // one of us?
return qfalse;
if( target->client->sess.spectatorState != SPECTATOR_NOT ) // is the target alive?
return qfalse;
if( target->health <= 0 ) // is the target still alive?
return qfalse;
if( target->client->ps.stats[ STAT_STATE ] & SS_BLOBLOCKED ) // locked?
return qfalse;
VectorSubtract( target->r.currentOrigin, self->r.currentOrigin, distance );
if( VectorLength( distance ) > range ) // is the target within range?
return qfalse;
//only allow a narrow field of "vision"
VectorNormalize( distance ); //is now direction of target
if( DotProduct( distance, self->s.origin2 ) < LOCKBLOB_DOT )
return qfalse;
trap_Trace( &trace, self->s.pos.trBase, NULL, NULL, target->s.pos.trBase, self->s.number, MASK_SHOT );
if ( trace.contents & CONTENTS_SOLID ) // can we see the target?
return qfalse;
return qtrue;
}
/*
================
ATrapper_FindEnemy
Used by ATrapper_Think to locate enemy gentities
================
*/
void ATrapper_FindEnemy( gentity_t *ent, int range )
{
gentity_t *target;
//iterate through entities
for( target = g_entities; target < &g_entities[ level.num_entities ]; target++ )
{
//if target is not valid keep searching
if( !ATrapper_CheckTarget( ent, target, range ) )
continue;
//we found a target
ent->enemy = target;
return;
}
//couldn't find a target
ent->enemy = NULL;
}
/*
================
ATrapper_Think
think function for Alien Defense
================
*/
void ATrapper_Think( gentity_t *self )
{
int range = BG_Buildable( self->s.modelindex )->turretRange;
int firespeed = BG_Buildable( self->s.modelindex )->turretFireSpeed;
AGeneric_Think( self );
if( self->spawned && self->powered )
{
//if the current target is not valid find a new one
if( !ATrapper_CheckTarget( self, self->enemy, range ) )
ATrapper_FindEnemy( self, range );
//if a new target cannot be found don't do anything
if( !self->enemy )
return;
//if we are pointing at our target and we can fire shoot it
if( self->count < level.time )
ATrapper_FireOnEnemy( self, firespeed, range );
}
}
//==================================================================================
/*
================
G_SuicideIfNoPower
Destroy human structures that have been unpowered too long
================
*/
static void G_SuicideIfNoPower( gentity_t *self )
{
if( self->buildableTeam != TEAM_HUMANS )
return;
if( G_Reactor( ) && !self->parentNode )
{
// If no parent for x seconds then disappear
if( self->count < 0 )
self->count = level.time;
else if( self->count > 0 && ( ( level.time - self->count ) > HUMAN_BUILDABLE_INACTIVE_TIME ) )
G_Damage( self, NULL, NULL, NULL, NULL, self->health, 0, MOD_SUICIDE );
}
else
{
self->count = -1;
}
}
void HSpawn_Blast( gentity_t *ent );
void HSpawn_Disappear( gentity_t *ent );
/*
================
HRepeater_Die
Called when a repeater dies
================
*/
static void HRepeater_Die( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int mod )
{
G_RewardAttackers( self );
G_SetBuildableAnim( self, BANIM_DESTROY1, qtrue );
G_SetIdleBuildableAnim( self, BANIM_DESTROYED );
self->die = nullDieFunction;
self->powered = qfalse; //free up power
self->s.eFlags &= ~EF_FIRING; //prevent any firing effects
if( self->spawned )
{
self->think = HSpawn_Blast;
self->nextthink = level.time + HUMAN_DETONATION_DELAY;
}
else
{
self->think = HSpawn_Disappear;
self->nextthink = level.time; //blast immediately
}
G_LogDestruction( self, attacker, mod );
if( self->usesZone )
{
zone_t *zone = &level.powerZones[self->zone];
zone->active = qfalse;
self->usesZone = qfalse;
}
}
/*
================
HRepeater_Think
Think for human power repeater
================
*/
void HRepeater_Think( gentity_t *self )
{
int i;
qboolean reactor = qfalse;
gentity_t *ent;
zone_t *zone;
if( self->spawned )
{
// Iterate through entities
for ( i = 1, ent = g_entities + i; i < level.num_entities; i++, ent++ )
{
if( ent->s.eType != ET_BUILDABLE )
continue;
if( ent->s.modelindex == BA_H_REACTOR && ent->spawned )
reactor = qtrue;
}
}
if( G_InPowerZone( self ) )
{
// if the repeater is inside of another power zone then disappear
G_Damage( self, NULL, NULL, NULL, NULL, self->health, 0, MOD_SUICIDE );
}
self->powered = reactor;
// Initialise the zone once the repeater has spawned
if( self->spawned && ( !self->usesZone || !level.powerZones[ self->zone ].active ) )
{
// See if a free zone exists
for( i = 0; i < g_humanRepeaterMaxZones.integer; i++ )
{
zone = &level.powerZones[ i ];
if( !zone->active )
{
// Initialise the BP queue with all BP queued
zone->queuedBuildPoints = zone->totalBuildPoints = g_humanRepeaterBuildPoints.integer;
zone->nextQueueTime = level.time;
zone->active = qtrue;
self->zone = zone - level.powerZones;
self->usesZone = qtrue;
break;
}
}
}
self->nextthink = level.time + POWER_REFRESH_TIME;
}
/*
================
HRepeater_Use
Use for human power repeater
================
*/
void HRepeater_Use( gentity_t *self, gentity_t *other, gentity_t *activator )
{
if( self->health <= 0 || !self->spawned )
return;
if( other && other->client )
G_GiveClientMaxAmmo( other, qtrue );
}
/*
================
HReactor_Think
Think function for Human Reactor
================
*/
void HReactor_Think( gentity_t *self )
{
int entityList[ MAX_GENTITIES ];
vec3_t range = { REACTOR_ATTACK_RANGE,
REACTOR_ATTACK_RANGE,
REACTOR_ATTACK_RANGE };
vec3_t dccrange = { REACTOR_ATTACK_DCC_RANGE,
REACTOR_ATTACK_DCC_RANGE,
REACTOR_ATTACK_DCC_RANGE };
vec3_t mins, maxs;
int i, num;
gentity_t *enemy, *tent;
if( self->dcc )
{
VectorAdd( self->s.origin, dccrange, maxs );
VectorSubtract( self->s.origin, dccrange, mins );
}
else
{
VectorAdd( self->s.origin, range, maxs );
VectorSubtract( self->s.origin, range, mins );
}
if( self->spawned && ( self->health > 0 ) )
{
qboolean fired = qfalse;
// Creates a tesla trail for every target
num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES );
for( i = 0; i < num; i++ )
{
enemy = &g_entities[ entityList[ i ] ];
if( !enemy->client ||
enemy->client->ps.stats[ STAT_TEAM ] != TEAM_ALIENS )
continue;
tent = G_TempEntity( enemy->s.pos.trBase, EV_TESLATRAIL );
tent->s.generic1 = self->s.number; //src
tent->s.clientNum = enemy->s.number; //dest
VectorCopy( self->s.pos.trBase, tent->s.origin2 );
fired = qtrue;
}
// Actual damage is done by radius
if( fired )
{
self->timestamp = level.time;
if( self->dcc )
G_SelectiveRadiusDamage( self->s.pos.trBase, self,
REACTOR_ATTACK_DCC_DAMAGE,
REACTOR_ATTACK_DCC_RANGE, self,
MOD_REACTOR, TEAM_HUMANS );
else
G_SelectiveRadiusDamage( self->s.pos.trBase, self,
REACTOR_ATTACK_DAMAGE,
REACTOR_ATTACK_RANGE, self,
MOD_REACTOR, TEAM_HUMANS );
}
}
if( self->dcc )
self->nextthink = level.time + REACTOR_ATTACK_DCC_REPEAT;
else
self->nextthink = level.time + REACTOR_ATTACK_REPEAT;
}
//==================================================================================
/*
================
HArmoury_Activate
Called when a human activates an Armoury
================
*/
void HArmoury_Activate( gentity_t *self, gentity_t *other, gentity_t *activator )
{
if( self->spawned )
{
//only humans can activate this
if( activator->client->ps.stats[ STAT_TEAM ] != TEAM_HUMANS )
return;
//if this is powered then call the armoury menu
if( self->powered )
G_TriggerMenu( activator->client->ps.clientNum, MN_H_ARMOURY );
else
G_TriggerMenu( activator->client->ps.clientNum, MN_H_NOTPOWERED );
}
}
/*
================
HArmoury_Think
Think for armoury
================
*/
void HArmoury_Think( gentity_t *self )
{
//make sure we have power
self->nextthink = level.time + POWER_REFRESH_TIME;
self->powered = G_FindPower( self );
G_SuicideIfNoPower( self );
}
//==================================================================================
/*
================
HDCC_Think
Think for dcc
================
*/
void HDCC_Think( gentity_t *self )
{
//make sure we have power
self->nextthink = level.time + POWER_REFRESH_TIME;
self->powered = G_FindPower( self );
G_SuicideIfNoPower( self );
}
//==================================================================================
void HSpawn_Die( gentity_t *self, gentity_t *inflictor, gentity_t *attacker,
int damage, int mod );
/*
================
HMedistat_Die
Die function for Human Medistation
================
*/
void HMedistat_Die( gentity_t *self, gentity_t *inflictor,
gentity_t *attacker, int damage, int mod )
{
//clear target's healing flag
if( self->enemy && self->enemy->client )
self->enemy->client->ps.stats[ STAT_STATE ] &= ~SS_HEALING_ACTIVE;
HSpawn_Die( self, inflictor, attacker, damage, mod );
}
/*
================
HMedistat_Think
think function for Human Medistation
================
*/
void HMedistat_Think( gentity_t *self )
{
int entityList[ MAX_GENTITIES ];
vec3_t mins, maxs;
int i, num;
gentity_t *player;
qboolean occupied = qfalse;
self->nextthink = level.time + BG_Buildable( self->s.modelindex )->nextthink;
G_SuicideIfNoPower( self );
//clear target's healing flag
if( self->enemy && self->enemy->client )
self->enemy->client->ps.stats[ STAT_STATE ] &= ~SS_HEALING_ACTIVE;
//make sure we have power
if( !( self->powered = G_FindPower( self ) ) )
{
if( self->active )
{
G_SetBuildableAnim( self, BANIM_CONSTRUCT2, qtrue );
G_SetIdleBuildableAnim( self, BANIM_IDLE1 );
self->active = qfalse;
self->enemy = NULL;
}
self->nextthink = level.time + POWER_REFRESH_TIME;
return;
}
if( self->spawned )
{
VectorAdd( self->s.origin, self->r.maxs, maxs );
VectorAdd( self->s.origin, self->r.mins, mins );
mins[ 2 ] += fabs( self->r.mins[ 2 ] ) + self->r.maxs[ 2 ];
maxs[ 2 ] += 60; //player height
//if active use the healing idle
if( self->active )
G_SetIdleBuildableAnim( self, BANIM_IDLE2 );
//check if a previous occupier is still here
num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES );
for( i = 0; i < num; i++ )
{
player = &g_entities[ entityList[ i ] ];
//remove poison from everyone, not just the healed player
if( player->client && player->client->ps.stats[ STAT_STATE ] & SS_POISONED )
player->client->ps.stats[ STAT_STATE ] &= ~SS_POISONED;
if( self->enemy == player && player->client &&
player->client->ps.stats[ STAT_TEAM ] == TEAM_HUMANS &&
player->health < player->client->ps.stats[ STAT_MAX_HEALTH ] &&
PM_Live( player->client->ps.pm_type ) )
{
occupied = qtrue;
player->client->ps.stats[ STAT_STATE ] |= SS_HEALING_ACTIVE;
}
}
if( !occupied )
{
self->enemy = NULL;
//look for something to heal
for( i = 0; i < num; i++ )
{
player = &g_entities[ entityList[ i ] ];
if( player->client && player->client->ps.stats[ STAT_TEAM ] == TEAM_HUMANS )
{
if( ( player->health < player->client->ps.stats[ STAT_MAX_HEALTH ] ||
player->client->ps.stats[ STAT_STAMINA ] < MAX_STAMINA ) &&
PM_Live( player->client->ps.pm_type ) )
{
self->enemy = player;
//start the heal anim
if( !self->active )
{
G_SetBuildableAnim( self, BANIM_ATTACK1, qfalse );
self->active = qtrue;
player->client->ps.stats[ STAT_STATE ] |= SS_HEALING_ACTIVE;
}
}
else if( !BG_InventoryContainsUpgrade( UP_MEDKIT, player->client->ps.stats ) )
BG_AddUpgradeToInventory( UP_MEDKIT, player->client->ps.stats );
}
}
}
//nothing left to heal so go back to idling
if( !self->enemy && self->active )
{
G_SetBuildableAnim( self, BANIM_CONSTRUCT2, qtrue );
G_SetIdleBuildableAnim( self, BANIM_IDLE1 );
self->active = qfalse;
}
else if( self->enemy && self->enemy->client ) //heal!
{
if( self->enemy->client->ps.stats[ STAT_STAMINA ] < MAX_STAMINA )
self->enemy->client->ps.stats[ STAT_STAMINA ] += STAMINA_MEDISTAT_RESTORE;
if( self->enemy->client->ps.stats[ STAT_STAMINA ] > MAX_STAMINA )
self->enemy->client->ps.stats[ STAT_STAMINA ] = MAX_STAMINA;
self->enemy->health++;
//if they're completely healed, give them a medkit
if( self->enemy->health >= self->enemy->client->ps.stats[ STAT_MAX_HEALTH ] )
{
self->enemy->health = self->enemy->client->ps.stats[ STAT_MAX_HEALTH ];
if( !BG_InventoryContainsUpgrade( UP_MEDKIT, self->enemy->client->ps.stats ) )
BG_AddUpgradeToInventory( UP_MEDKIT, self->enemy->client->ps.stats );
}
}
}
}
//==================================================================================
/*
================
HMGTurret_CheckTarget
Used by HMGTurret_Think to check enemies for validity
================
*/
qboolean HMGTurret_CheckTarget( gentity_t *self, gentity_t *target,
qboolean los_check )
{
trace_t tr;
vec3_t dir, end;
if( !target || target->health <= 0 || !target->client ||
target->client->pers.teamSelection != TEAM_ALIENS ||
( target->client->ps.stats[ STAT_STATE ] & SS_HOVELING ) )
return qfalse;
if( !los_check )
return qtrue;
// Accept target if we can line-trace to it
VectorSubtract( target->s.pos.trBase, self->s.pos.trBase, dir );
VectorNormalize( dir );
VectorMA( self->s.pos.trBase, MGTURRET_RANGE, dir, end );
trap_Trace( &tr, self->s.pos.trBase, NULL, NULL, end,
self->s.number, MASK_SHOT );
return tr.entityNum == target - g_entities;
}
/*
================
HMGTurret_TrackEnemy
Used by HMGTurret_Think to track enemy location
================
*/
qboolean HMGTurret_TrackEnemy( gentity_t *self )
{
vec3_t dirToTarget, dttAdjusted, angleToTarget, angularDiff, xNormal;
vec3_t refNormal = { 0.0f, 0.0f, 1.0f };
float temp, rotAngle, angularSpeed;
angularSpeed = self->lev1Grabbed ? MGTURRET_ANGULARSPEED_GRAB :
MGTURRET_ANGULARSPEED;
VectorSubtract( self->enemy->s.pos.trBase, self->s.pos.trBase, dirToTarget );
VectorNormalize( dirToTarget );
CrossProduct( self->s.origin2, refNormal, xNormal );
VectorNormalize( xNormal );
rotAngle = RAD2DEG( acos( DotProduct( self->s.origin2, refNormal ) ) );
RotatePointAroundVector( dttAdjusted, xNormal, dirToTarget, rotAngle );
vectoangles( dttAdjusted, angleToTarget );
angularDiff[ PITCH ] = AngleSubtract( self->s.angles2[ PITCH ], angleToTarget[ PITCH ] );
angularDiff[ YAW ] = AngleSubtract( self->s.angles2[ YAW ], angleToTarget[ YAW ] );
//if not pointing at our target then move accordingly
if( angularDiff[ PITCH ] < 0 && angularDiff[ PITCH ] < (-angularSpeed) )
self->s.angles2[ PITCH ] += angularSpeed;
else if( angularDiff[ PITCH ] > 0 && angularDiff[ PITCH ] > angularSpeed )
self->s.angles2[ PITCH ] -= angularSpeed;
else
self->s.angles2[ PITCH ] = angleToTarget[ PITCH ];
//disallow vertical movement past a certain limit
temp = fabs( self->s.angles2[ PITCH ] );
if( temp > 180 )
temp -= 360;
if( temp < -MGTURRET_VERTICALCAP )
self->s.angles2[ PITCH ] = (-360) + MGTURRET_VERTICALCAP;
//if not pointing at our target then move accordingly
if( angularDiff[ YAW ] < 0 && angularDiff[ YAW ] < ( -angularSpeed ) )
self->s.angles2[ YAW ] += angularSpeed;
else if( angularDiff[ YAW ] > 0 && angularDiff[ YAW ] > angularSpeed )
self->s.angles2[ YAW ] -= angularSpeed;
else
self->s.angles2[ YAW ] = angleToTarget[ YAW ];
AngleVectors( self->s.angles2, dttAdjusted, NULL, NULL );
RotatePointAroundVector( dirToTarget, xNormal, dttAdjusted, -rotAngle );
vectoangles( dirToTarget, self->turretAim );
//fire if target is within accuracy
return ( abs( angularDiff[ YAW ] ) - angularSpeed <=
MGTURRET_ACCURACY_TO_FIRE ) &&
( abs( angularDiff[ PITCH ] ) - angularSpeed <=
MGTURRET_ACCURACY_TO_FIRE );
}
/*
================
HMGTurret_FindEnemy
Used by HMGTurret_Think to locate enemy gentities
================
*/
void HMGTurret_FindEnemy( gentity_t *self )
{
int entityList[ MAX_GENTITIES ];
vec3_t range;
vec3_t mins, maxs;
int i, num;
gentity_t *target;
if( self->enemy )
self->enemy->targeted = NULL;
self->enemy = NULL;
// Look for targets in a box around the turret
VectorSet( range, MGTURRET_RANGE, MGTURRET_RANGE, MGTURRET_RANGE );
VectorAdd( self->s.origin, range, maxs );
VectorSubtract( self->s.origin, range, mins );
num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES );
for( i = 0; i < num; i++ )
{
target = &g_entities[ entityList[ i ] ];
if( !HMGTurret_CheckTarget( self, target, qtrue ) )
continue;
self->enemy = target;
self->enemy->targeted = self;
return;
}
}
/*
================
HMGTurret_Think
Think function for MG turret
================
*/
void HMGTurret_Think( gentity_t *self )
{
self->nextthink = level.time +
BG_Buildable( self->s.modelindex )->nextthink;
// Turn off client side muzzle flashes
self->s.eFlags &= ~EF_FIRING;
G_SuicideIfNoPower( self );
// If not powered or spawned don't do anything
if( !( self->powered = G_FindPower( self ) ) )
{
self->nextthink = level.time + POWER_REFRESH_TIME;
return;
}
if( !self->spawned )
return;
// If the current target is not valid find a new enemy
if( !HMGTurret_CheckTarget( self, self->enemy, qtrue ) )
{
self->active = qfalse;
self->turretSpinupTime = -1;
HMGTurret_FindEnemy( self );
}
if( !self->enemy )
return;
// Track until we can hit the target
if( !HMGTurret_TrackEnemy( self ) )
{
self->active = qfalse;
self->turretSpinupTime = -1;
return;
}
// Update spin state
if( !self->active && self->timestamp < level.time )
{
self->active = qtrue;
self->turretSpinupTime = level.time + MGTURRET_SPINUP_TIME;
G_AddEvent( self, EV_MGTURRET_SPINUP, 0 );
}
// Not firing or haven't spun up yet
if( !self->active || self->turretSpinupTime > level.time )
return;
// Fire repeat delay
if( self->timestamp > level.time )
return;
FireWeapon( self );
self->s.eFlags |= EF_FIRING;
self->timestamp = level.time + BG_Buildable( self->s.modelindex )->turretFireSpeed;
G_AddEvent( self, EV_FIRE_WEAPON, 0 );
G_SetBuildableAnim( self, BANIM_ATTACK1, qfalse );
}
//==================================================================================
/*
================
HTeslaGen_Think
Think function for Tesla Generator
================
*/
void HTeslaGen_Think( gentity_t *self )
{
self->nextthink = level.time + BG_Buildable( self->s.modelindex )->nextthink;
G_SuicideIfNoPower( self );
//if not powered don't do anything and check again for power next think
if( !( self->powered = G_FindPower( self ) ) )
{
self->s.eFlags &= ~EF_FIRING;
self->nextthink = level.time + POWER_REFRESH_TIME;
return;
}
if( self->spawned && self->count < level.time )
{
vec3_t range, mins, maxs;
int entityList[ MAX_GENTITIES ], i, num;
// Communicates firing state to client
self->s.eFlags &= ~EF_FIRING;
VectorSet( range, TESLAGEN_RANGE, TESLAGEN_RANGE, TESLAGEN_RANGE );
VectorAdd( self->s.origin, range, maxs );
VectorSubtract( self->s.origin, range, mins );
// Attack nearby Aliens
num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES );
for( i = 0; i < num; i++ )
{
self->enemy = &g_entities[ entityList[ i ] ];
if( self->enemy->client && self->enemy->health > 0 &&
self->enemy->client->ps.stats[ STAT_TEAM ] == TEAM_ALIENS &&
Distance( self->enemy->s.pos.trBase,
self->s.pos.trBase ) <= TESLAGEN_RANGE )
FireWeapon( self );
}
self->enemy = NULL;
if( self->s.eFlags & EF_FIRING )
{
G_AddEvent( self, EV_FIRE_WEAPON, 0 );
//doesn't really need an anim
//G_SetBuildableAnim( self, BANIM_ATTACK1, qfalse );
self->count = level.time + TESLAGEN_REPEAT;
}
}
}
//==================================================================================
/*
================
HSpawn_Disappear
Called when a human spawn is destroyed before it is spawned
think function
================
*/
void HSpawn_Disappear( gentity_t *self )
{
self->s.eFlags |= EF_NODRAW; //don't draw the model once its destroyed
self->timestamp = level.time;
G_QueueBuildPoints( self );
G_FreeEntity( self );
}
/*
================
HSpawn_blast
Called when a human spawn explodes
think function
================
*/
void HSpawn_Blast( gentity_t *self )
{
vec3_t dir;
// we don't have a valid direction, so just point straight up
dir[ 0 ] = dir[ 1 ] = 0;
dir[ 2 ] = 1;
self->timestamp = level.time;
//do some radius damage
G_RadiusDamage( self->s.pos.trBase, self, self->splashDamage,
self->splashRadius, self, self->splashMethodOfDeath );
// begin freeing build points
G_QueueBuildPoints( self );
// turn into an explosion
self->s.eType = ET_EVENTS + EV_HUMAN_BUILDABLE_EXPLOSION;
self->freeAfterEvent = qtrue;
G_AddEvent( self, EV_HUMAN_BUILDABLE_EXPLOSION, DirToByte( dir ) );
}
/*
================
HSpawn_die
Called when a human spawn dies
================
*/
void HSpawn_Die( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int mod )
{
G_RewardAttackers( self );
G_SetBuildableAnim( self, BANIM_DESTROY1, qtrue );
G_SetIdleBuildableAnim( self, BANIM_DESTROYED );
self->die = nullDieFunction;
self->powered = qfalse; //free up power
self->s.eFlags &= ~EF_FIRING; //prevent any firing effects
if( self->spawned )
{
self->think = HSpawn_Blast;
self->nextthink = level.time + HUMAN_DETONATION_DELAY;
}
else
{
self->think = HSpawn_Disappear;
self->nextthink = level.time; //blast immediately
}
G_LogDestruction( self, attacker, mod );
}
/*
================
HSpawn_Think
Think for human spawn
================
*/
void HSpawn_Think( gentity_t *self )
{
gentity_t *ent;
// spawns work without power
self->powered = qtrue;
G_SuicideIfNoPower( self );
// set parentNode
G_FindPower( self );
if( self->spawned )
{
//only suicide if at rest
if( self->s.groundEntityNum )
{
if( ( ent = G_CheckSpawnPoint( self->s.number, self->s.origin,
self->s.origin2, BA_H_SPAWN, NULL ) ) != NULL )
{
// If the thing blocking the spawn is a buildable, kill it.
// If it's part of the map, kill self.
if( ent->s.eType == ET_BUILDABLE )
{
G_Damage( ent, NULL, NULL, NULL, NULL, self->health, 0, MOD_SUICIDE );
G_SetBuildableAnim( self, BANIM_SPAWN1, qtrue );
}
else if( ent->s.number == ENTITYNUM_WORLD || ent->s.eType == ET_MOVER )
{
G_Damage( self, NULL, NULL, NULL, NULL, self->health, 0, MOD_SUICIDE );
return;
}
if( ent->s.eType == ET_CORPSE )
G_FreeEntity( ent ); //quietly remove
}
}
}
self->nextthink = level.time + BG_Buildable( self->s.modelindex )->nextthink;
}
//==================================================================================
/*
============
G_QueueBuildPoints
============
*/
void G_QueueBuildPoints( gentity_t *self )
{
gentity_t *killer = NULL;
gentity_t *powerEntity;
if( self->killedBy != ENTITYNUM_NONE )
killer = &g_entities[ self->killedBy ];
if( killer && killer->client &&
killer->client->ps.stats[ STAT_TEAM ] == self->buildableTeam )
{
// Don't take away build points if killed by a teammate
// deconstructing an egg/overmind (MOD_NOCREEP)
return;
}
switch( self->buildableTeam )
{
default:
case TEAM_NONE:
return;
case TEAM_ALIENS:
if( !level.alienBuildPointQueue )
level.alienNextQueueTime = level.time + g_alienBuildQueueTime.integer;
level.alienBuildPointQueue +=
BG_Buildable( self->s.modelindex )->buildPoints;
break;
case TEAM_HUMANS:
powerEntity = G_PowerEntityForEntity( self );
if( powerEntity )
{
int nqt;
switch( powerEntity->s.modelindex )
{
case BA_H_REACTOR:
nqt = G_NextQueueTime( level.humanBuildPointQueue,
g_humanBuildPoints.integer,
g_humanBuildQueueTime.integer );
if( !level.humanBuildPointQueue ||
level.time + nqt < level.humanNextQueueTime )
level.humanNextQueueTime = level.time + nqt;
level.humanBuildPointQueue +=
BG_Buildable( self->s.modelindex )->buildPoints;
break;
case BA_H_REPEATER:
if( powerEntity->usesZone &&
level.powerZones[ powerEntity->zone ].active )
{
zone_t *zone = &level.powerZones[ powerEntity->zone ];
nqt = G_NextQueueTime( zone->queuedBuildPoints,
zone->totalBuildPoints,
g_humanRepeaterBuildQueueTime.integer );
if( !zone->queuedBuildPoints ||
level.time + nqt < zone->nextQueueTime )
zone->nextQueueTime = level.time + nqt;
zone->queuedBuildPoints +=
BG_Buildable( self->s.modelindex )->buildPoints;
}
break;
default:
break;
}
}
else
{
if( !level.humanBuildPointQueue )
level.humanNextQueueTime = level.time + g_humanBuildQueueTime.integer;
level.humanBuildPointQueue +=
BG_Buildable( self->s.modelindex )->buildPoints;
}
}
}
/*
============
G_NextQueueTime
============
*/
int G_NextQueueTime( int queuedBP, int totalBP, int queueBaseRate )
{
float fractionQueued;
if( totalBP == 0 )
return 0;
fractionQueued = queuedBP / (float)totalBP;
return ( 1.0f - fractionQueued ) * queueBaseRate;
}
/*
============
G_BuildableTouchTriggers
Find all trigger entities that a buildable touches.
============
*/
void G_BuildableTouchTriggers( gentity_t *ent )
{
int i, num;
int touch[ MAX_GENTITIES ];
gentity_t *hit;
trace_t trace;
vec3_t mins, maxs;
vec3_t bmins, bmaxs;
static vec3_t range = { 10, 10, 10 };
// dead buildables don't activate triggers!
if( ent->health <= 0 )
return;
BG_BuildableBoundingBox( ent->s.modelindex, bmins, bmaxs );
VectorAdd( ent->s.origin, bmins, mins );
VectorAdd( ent->s.origin, bmaxs, maxs );
VectorSubtract( mins, range, mins );
VectorAdd( maxs, range, maxs );
num = trap_EntitiesInBox( mins, maxs, touch, MAX_GENTITIES );
VectorAdd( ent->s.origin, bmins, mins );
VectorAdd( ent->s.origin, bmaxs, maxs );
for( i = 0; i < num; i++ )
{
hit = &g_entities[ touch[ i ] ];
if( !hit->touch )
continue;
if( !( hit->r.contents & CONTENTS_TRIGGER ) )
continue;
//ignore buildables not yet spawned
if( !ent->spawned )
continue;
if( !trap_EntityContact( mins, maxs, hit ) )
continue;
memset( &trace, 0, sizeof( trace ) );
if( hit->touch )
hit->touch( hit, ent, &trace );
}
}
/*
===============
G_BuildableThink
General think function for buildables
===============
*/
void G_BuildableThink( gentity_t *ent, int msec )
{
int bHealth = BG_Buildable( ent->s.modelindex )->health;
int bRegen = BG_Buildable( ent->s.modelindex )->regenRate;
int bTime = BG_Buildable( ent->s.modelindex )->buildTime;
//toggle spawned flag for buildables
if( !ent->spawned && ent->health > 0 )
{
if( ent->buildTime + bTime < level.time )
ent->spawned = qtrue;
}
// Timer actions
ent->time1000 += msec;
if( ent->time1000 >= 1000 )
{
ent->time1000 -= 1000;
if( !ent->spawned && ent->health > 0 )
ent->health += (int)( ceil( (float)bHealth / (float)( bTime * 0.001 ) ) );
else if( ent->health > 0 && ent->health < bHealth )
{
if( ent->buildableTeam == TEAM_ALIENS && bRegen &&
( ent->lastDamageTime + ALIEN_REGEN_DAMAGE_TIME ) < level.time )
{
ent->health += bRegen;
}
else if( ent->buildableTeam == TEAM_HUMANS && ent->dcc &&
( ent->lastDamageTime + HUMAN_REGEN_DAMAGE_TIME ) < level.time )
{
ent->health += DC_HEALRATE * ent->dcc;
}
}
if( ent->health >= bHealth )
{
int i;
ent->health = bHealth;
for( i = 0; i < MAX_CLIENTS; i++ )
ent->credits[ i ] = 0;
}
}
if( ent->lev1Grabbed && ent->lev1GrabTime + LEVEL1_GRAB_TIME < level.time )
ent->lev1Grabbed = qfalse;
if( ent->clientSpawnTime > 0 )
ent->clientSpawnTime -= msec;
if( ent->clientSpawnTime < 0 )
ent->clientSpawnTime = 0;
// Pack health
ent->dcc = ( ent->buildableTeam != TEAM_HUMANS ) ? 0 : G_FindDCC( ent );
if( ent->health > 0 )
{
ent->s.generic1 = (int)( ( ent->health + bHealth / B_HEALTH_MASK - 1 ) *
B_HEALTH_MASK / bHealth );
}
else
ent->s.generic1 = 0;
// Set flags
ent->s.eFlags &= ~( EF_B_POWERED | EF_B_SPAWNED | EF_B_MARKED );
if( ent->powered )
ent->s.eFlags |= EF_B_POWERED;
if( ent->spawned )
ent->s.eFlags |= EF_B_SPAWNED;
if( ent->deconstruct )
ent->s.eFlags |= EF_B_MARKED;
//check if this buildable is touching any triggers
G_BuildableTouchTriggers( ent );
//fall back on normal physics routines
G_Physics( ent, msec );
}
/*
===============
G_BuildableRange
Check whether a point is within some range of a type of buildable
===============
*/
qboolean G_BuildableRange( vec3_t origin, float r, buildable_t buildable )
{
int entityList[ MAX_GENTITIES ];
vec3_t range;
vec3_t mins, maxs;
int i, num;
gentity_t *ent;
VectorSet( range, r, r, r );
VectorAdd( origin, range, maxs );
VectorSubtract( origin, range, mins );
num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES );
for( i = 0; i < num; i++ )
{
ent = &g_entities[ entityList[ i ] ];
if( ent->s.eType != ET_BUILDABLE )
continue;
if( ent->buildableTeam == TEAM_HUMANS && !ent->powered )
continue;
if( ent->s.modelindex == buildable && ent->spawned )
return qtrue;
}
return qfalse;
}
/*
================
G_FindBuildable
Finds a buildable of the specified type
================
*/
static gentity_t *G_FindBuildable( buildable_t buildable )
{
int i;
gentity_t *ent;
for( i = MAX_CLIENTS, ent = g_entities + i;
i < level.num_entities; i++, ent++ )
{
if( ent->s.eType != ET_BUILDABLE )
continue;
if( ent->s.modelindex == buildable )
return ent;
}
return NULL;
}
/*
===============
G_BuildablesIntersect
Test if two buildables intersect each other
===============
*/
static qboolean G_BuildablesIntersect( buildable_t a, vec3_t originA,
buildable_t b, vec3_t originB )
{
vec3_t minsA, maxsA;
vec3_t minsB, maxsB;
BG_BuildableBoundingBox( a, minsA, maxsA );
VectorAdd( minsA, originA, minsA );
VectorAdd( maxsA, originA, maxsA );
BG_BuildableBoundingBox( b, minsB, maxsB );
VectorAdd( minsB, originB, minsB );
VectorAdd( maxsB, originB, maxsB );
return BoundsIntersect( minsA, maxsA, minsB, maxsB );
}
/*
===============
G_CompareBuildablesForRemoval
qsort comparison function for a buildable removal list
===============
*/
static buildable_t cmpBuildable;
static vec3_t cmpOrigin;
static int G_CompareBuildablesForRemoval( const void *a, const void *b )
{
int precedence[ ] =
{
BA_NONE,
BA_A_BARRICADE,
BA_A_ACIDTUBE,
BA_A_TRAPPER,
BA_A_HIVE,
BA_A_BOOSTER,
BA_A_HOVEL,
BA_A_SPAWN,
BA_A_OVERMIND,
BA_H_MGTURRET,
BA_H_TESLAGEN,
BA_H_DCC,
BA_H_MEDISTAT,
BA_H_ARMOURY,
BA_H_SPAWN,
BA_H_REPEATER,
BA_H_REACTOR
};
gentity_t *buildableA, *buildableB;
int i;
int aPrecedence = 0, bPrecedence = 0;
qboolean aMatches = qfalse, bMatches = qfalse;
buildableA = *(gentity_t **)a;
buildableB = *(gentity_t **)b;
// Prefer the one that collides with the thing we're building
aMatches = G_BuildablesIntersect( cmpBuildable, cmpOrigin,
buildableA->s.modelindex, buildableA->s.origin );
bMatches = G_BuildablesIntersect( cmpBuildable, cmpOrigin,
buildableB->s.modelindex, buildableB->s.origin );
if( aMatches && !bMatches )
return -1;
else if( !aMatches && bMatches )
return 1;
// If one matches the thing we're building, prefer it
aMatches = ( buildableA->s.modelindex == cmpBuildable );
bMatches = ( buildableB->s.modelindex == cmpBuildable );
if( aMatches && !bMatches )
return -1;
else if( !aMatches && bMatches )
return 1;
// They're the same type
if( buildableA->s.modelindex == buildableB->s.modelindex )
{
gentity_t *powerEntity = G_PowerEntityForPoint( cmpOrigin );
// Prefer the entity that is providing power for this point
aMatches = ( powerEntity == buildableA );
bMatches = ( powerEntity == buildableB );
if( aMatches && !bMatches )
return -1;
else if( !aMatches && bMatches )
return 1;
// Pick the one marked earliest
return buildableA->deconstructTime - buildableB->deconstructTime;
}
// Resort to preference list
for( i = 0; i < sizeof( precedence ) / sizeof( precedence[ 0 ] ); i++ )
{
if( buildableA->s.modelindex == precedence[ i ] )
aPrecedence = i;
if( buildableB->s.modelindex == precedence[ i ] )
bPrecedence = i;
}
return aPrecedence - bPrecedence;
}
/*
===============
G_FreeMarkedBuildables
Free up build points for a team by deconstructing marked buildables
===============
*/
void G_FreeMarkedBuildables( gentity_t *deconner )
{
int i;
gentity_t *ent;
if( !g_markDeconstruct.integer )
return; // Not enabled, can't deconstruct anything
for( i = 0; i < level.numBuildablesForRemoval; i++ )
{
ent = level.markedBuildables[ i ];
G_Damage( ent, NULL, deconner, NULL, NULL, ent->health, 0, MOD_DECONSTRUCT );
G_FreeEntity( ent );
}
}
/*
===============
G_SufficientBPAvailable
Determine if enough build points can be released for the buildable
and list the buildables that must be destroyed if this is the case
===============
*/
static itemBuildError_t G_SufficientBPAvailable( buildable_t buildable,
vec3_t origin )
{
int i;
int numBuildables = 0;
int pointsYielded = 0;
gentity_t *ent;
team_t team = BG_Buildable( buildable )->team;
int buildPoints = BG_Buildable( buildable )->buildPoints;
int remainingBP, remainingSpawns;
qboolean collision = qfalse;
int collisionCount = 0;
qboolean repeaterInRange = qfalse;
int repeaterInRangeCount = 0;
itemBuildError_t bpError;
buildable_t spawn;
buildable_t core;
int spawnCount = 0;
level.numBuildablesForRemoval = 0;
if( team == TEAM_ALIENS )
{
remainingBP = G_GetBuildPoints( origin, team, 0 );
remainingSpawns = level.numAlienSpawns;
bpError = IBE_NOALIENBP;
spawn = BA_A_SPAWN;
core = BA_A_OVERMIND;
}
else if( team == TEAM_HUMANS )
{
if( buildable == BA_H_REACTOR || buildable == BA_H_REPEATER )
remainingBP = level.humanBuildPoints;
else
remainingBP = G_GetBuildPoints( origin, team, 0 );
remainingSpawns = level.numHumanSpawns;
bpError = IBE_NOHUMANBP;
spawn = BA_H_SPAWN;
core = BA_H_REACTOR;
}
else
{
Com_Error( ERR_FATAL, "team is %d\n", team );
return IBE_NONE;
}
// Simple non-marking case
if( !g_markDeconstruct.integer )
{
if( remainingBP - buildPoints < 0 )
return bpError;
// Check for buildable<->buildable collisions
for( i = MAX_CLIENTS, ent = g_entities + i; i < level.num_entities; i++, ent++ )
{
if( ent->s.eType != ET_BUILDABLE )
continue;
if( G_BuildablesIntersect( buildable, origin, ent->s.modelindex, ent->s.origin ) )
return IBE_NOROOM;
}
return IBE_NONE;
}
// Set buildPoints to the number extra that are required
buildPoints -= remainingBP;
// Build a list of buildable entities
for( i = MAX_CLIENTS, ent = g_entities + i; i < level.num_entities; i++, ent++ )
{
if( ent->s.eType != ET_BUILDABLE )
continue;
collision = G_BuildablesIntersect( buildable, origin, ent->s.modelindex, ent->s.origin );
if( collision )
{
// Don't allow replacements at all
if( g_markDeconstruct.integer == 1 )
return IBE_NOROOM;
// Only allow replacements of the same type
if( g_markDeconstruct.integer == 2 && ent->s.modelindex != buildable )
return IBE_NOROOM;
// Any other setting means anything goes
collisionCount++;
}
// Check if this is a repeater and it's in range
if( buildable == BA_H_REPEATER &&
buildable == ent->s.modelindex &&
Distance( ent->s.origin, origin ) < REPEATER_BASESIZE )
{
repeaterInRange = qtrue;
repeaterInRangeCount++;
}
else
repeaterInRange = qfalse;
// Don't allow marked buildables to be replaced in another zone,
// unless the marked buildable isn't in a zone (and thus unpowered)
if( team == TEAM_HUMANS &&
buildable != BA_H_REACTOR &&
buildable != BA_H_REPEATER &&
G_PowerEntityForPoint( ent->s.origin ) != G_PowerEntityForPoint( origin ) )
continue;
if( !ent->inuse )
continue;
if( ent->health <= 0 )
continue;
if( ent->buildableTeam != team )
continue;
// Don't allow destruction of hovel with granger inside
if( ent->s.modelindex == BA_A_HOVEL && ent->active )
continue;
// Explicitly disallow replacement of the core buildable with anything
// other than the core buildable
if( ent->s.modelindex == core && buildable != core )
continue;
// Don't allow a power source to be replaced by a dependant
if( team == TEAM_HUMANS &&
G_PowerEntityForPoint( origin ) != ent &&
buildable != BA_H_REPEATER &&
buildable != core )
continue;
if( ent->deconstruct )
{
level.markedBuildables[ numBuildables++ ] = ent;
// Buildables that are marked here will always end up at the front of the
// removal list, so just incrementing numBuildablesForRemoval is sufficient
if( collision || repeaterInRange )
{
// Collided with something, so we definitely have to remove it or
// it's a repeater that intersects the new repeater's power area,
// so it must be removed
if( collision )
collisionCount--;
if( repeaterInRange )
repeaterInRangeCount--;
pointsYielded += BG_Buildable( ent->s.modelindex )->buildPoints;
level.numBuildablesForRemoval++;
}
else if( BG_Buildable( ent->s.modelindex )->uniqueTest &&
ent->s.modelindex == buildable )
{
// If it's a unique buildable, it must be replaced by the same type
pointsYielded += BG_Buildable( ent->s.modelindex )->buildPoints;
level.numBuildablesForRemoval++;
}
}
}
// We still need build points, but have no candidates for removal
if( buildPoints > 0 && numBuildables == 0 )
return bpError;
// Collided with something we can't remove
if( collisionCount > 0 )
return IBE_NOROOM;
// There are one or more repeaters we can't remove
if( repeaterInRangeCount > 0 )
return IBE_RPTPOWERHERE;
// Sort the list
cmpBuildable = buildable;
VectorCopy( origin, cmpOrigin );
qsort( level.markedBuildables, numBuildables, sizeof( level.markedBuildables[ 0 ] ),
G_CompareBuildablesForRemoval );
// Determine if there are enough markees to yield the required BP
for( ; pointsYielded < buildPoints && level.numBuildablesForRemoval < numBuildables;
level.numBuildablesForRemoval++ )
{
ent = level.markedBuildables[ level.numBuildablesForRemoval ];
pointsYielded += BG_Buildable( ent->s.modelindex )->buildPoints;
}
for( i = 0; i < level.numBuildablesForRemoval; i++ )
{
if( level.markedBuildables[ i ]->s.modelindex == spawn )
spawnCount++;
}
// Make sure we're not removing the last spawn
if( !g_cheats.integer && remainingSpawns > 0 && ( remainingSpawns - spawnCount ) < 1 )
return IBE_LASTSPAWN;
// Not enough points yielded
if( pointsYielded < buildPoints )
return bpError;
else
return IBE_NONE;
}
/*
================
G_SetBuildableLinkState
Links or unlinks all the buildable entities
================
*/
static void G_SetBuildableLinkState( qboolean link )
{
int i;
gentity_t *ent;
for ( i = 1, ent = g_entities + i; i < level.num_entities; i++, ent++ )
{
if( ent->s.eType != ET_BUILDABLE )
continue;
if( link )
trap_LinkEntity( ent );
else
trap_UnlinkEntity( ent );
}
}
static void G_SetBuildableMarkedLinkState( qboolean link )
{
int i;
gentity_t *ent;
for( i = 0; i < level.numBuildablesForRemoval; i++ )
{
ent = level.markedBuildables[ i ];
if( link )
trap_LinkEntity( ent );
else
trap_UnlinkEntity( ent );
}
}
/*
================
G_CanBuild
Checks to see if a buildable can be built
================
*/
itemBuildError_t G_CanBuild( gentity_t *ent, buildable_t buildable, int distance, vec3_t origin )
{
vec3_t angles;
vec3_t entity_origin, normal;
vec3_t mins, maxs;
trace_t tr1, tr2, tr3;
itemBuildError_t reason = IBE_NONE, tempReason;
gentity_t *tempent;
float minNormal;
qboolean invert;
int contents;
playerState_t *ps = &ent->client->ps;
int buildPoints;
// Stop all buildables from interacting with traces
G_SetBuildableLinkState( qfalse );
BG_BuildableBoundingBox( buildable, mins, maxs );
BG_PositionBuildableRelativeToPlayer( ps, mins, maxs, trap_Trace, entity_origin, angles, &tr1 );
trap_Trace( &tr2, entity_origin, mins, maxs, entity_origin, ent->s.number, MASK_PLAYERSOLID );
trap_Trace( &tr3, ps->origin, NULL, NULL, entity_origin, ent->s.number, MASK_PLAYERSOLID );
VectorCopy( entity_origin, origin );
VectorCopy( tr1.plane.normal, normal );
minNormal = BG_Buildable( buildable )->minNormal;
invert = BG_Buildable( buildable )->invertNormal;
//can we build at this angle?
if( !( normal[ 2 ] >= minNormal || ( invert && normal[ 2 ] <= -minNormal ) ) )
reason = IBE_NORMAL;
if( tr1.entityNum != ENTITYNUM_WORLD )
reason = IBE_NORMAL;
contents = trap_PointContents( entity_origin, -1 );
buildPoints = BG_Buildable( buildable )->buildPoints;
if( ( tempReason = G_SufficientBPAvailable( buildable, origin ) ) != IBE_NONE )
reason = tempReason;
if( ent->client->ps.stats[ STAT_TEAM ] == TEAM_ALIENS )
{
//alien criteria
// Check there is an Overmind
if( buildable != BA_A_OVERMIND )
{
if( !G_Overmind( ) )
reason = IBE_NOOVERMIND;
}
//check there is creep near by for building on
if( BG_Buildable( buildable )->creepTest )
{
if( !G_IsCreepHere( entity_origin ) )
reason = IBE_NOCREEP;
}
if( buildable == BA_A_HOVEL )
{
vec3_t builderMins, builderMaxs;
//this assumes the adv builder is the biggest thing that'll use the hovel
BG_ClassBoundingBox( PCL_ALIEN_BUILDER0_UPG, builderMins, builderMaxs, NULL, NULL, NULL );
if( APropHovel_Blocked( origin, angles, normal, ent ) )
reason = IBE_HOVELEXIT;
}
// Check permission to build here
if( tr1.surfaceFlags & SURF_NOALIENBUILD || contents & CONTENTS_NOALIENBUILD )
reason = IBE_PERMISSION;
}
else if( ent->client->ps.stats[ STAT_TEAM ] == TEAM_HUMANS )
{
//human criteria
// Check for power
if( G_IsPowered( entity_origin ) == BA_NONE )
{
//tell player to build a repeater to provide power
if( buildable != BA_H_REACTOR && buildable != BA_H_REPEATER )
reason = IBE_NOPOWERHERE;
}
//this buildable requires a DCC
if( BG_Buildable( buildable )->dccTest && !G_IsDCCBuilt( ) )
reason = IBE_NODCC;
//check that there is a parent reactor when building a repeater
if( buildable == BA_H_REPEATER )
{
tempent = G_FindBuildable( BA_H_REACTOR );
if( tempent == NULL ) // No reactor
reason = IBE_RPTNOREAC;
else if( g_markDeconstruct.integer && G_IsPowered( entity_origin ) == BA_H_REACTOR )
reason = IBE_RPTPOWERHERE;
else if( !g_markDeconstruct.integer && G_IsPowered( entity_origin ) )
reason = IBE_RPTPOWERHERE;
}
// Check permission to build here
if( tr1.surfaceFlags & SURF_NOHUMANBUILD || contents & CONTENTS_NOHUMANBUILD )
reason = IBE_PERMISSION;
}
// Check permission to build here
if( tr1.surfaceFlags & SURF_NOBUILD || contents & CONTENTS_NOBUILD )
reason = IBE_PERMISSION;
// Can we only have one of these?
if( BG_Buildable( buildable )->uniqueTest )
{
tempent = G_FindBuildable( buildable );
if( tempent && !tempent->deconstruct && !( tempent->s.eFlags & EF_DEAD ) )
{
switch( buildable )
{
case BA_A_OVERMIND:
reason = IBE_ONEOVERMIND;
break;
case BA_A_HOVEL:
reason = IBE_ONEHOVEL;
break;
case BA_H_REACTOR:
reason = IBE_ONEREACTOR;
break;
default:
Com_Error( ERR_FATAL, "No reason for denying build of %d\n", buildable );
break;
}
}
}
// Relink buildables
G_SetBuildableLinkState( qtrue );
//check there is enough room to spawn from (presuming this is a spawn)
if( reason == IBE_NONE )
{
G_SetBuildableMarkedLinkState( qfalse );
if( G_CheckSpawnPoint( ENTITYNUM_NONE, origin, normal, buildable, NULL ) != NULL )
reason = IBE_NORMAL;
G_SetBuildableMarkedLinkState( qtrue );
}
//this item does not fit here
if( reason == IBE_NONE && ( tr2.fraction < 1.0f || tr3.fraction < 1.0f ) )
reason = IBE_NOROOM;
if( reason != IBE_NONE )
level.numBuildablesForRemoval = 0;
return reason;
}
/*
================
G_Build
Spawns a buildable
================
*/
static gentity_t *G_Build( gentity_t *builder, buildable_t buildable, vec3_t origin, vec3_t angles )
{
gentity_t *built;
vec3_t normal;
// Free existing buildables
G_FreeMarkedBuildables( builder );
// Spawn the buildable
built = G_Spawn();
built->s.eType = ET_BUILDABLE;
built->killedBy = ENTITYNUM_NONE;
built->classname = BG_Buildable( buildable )->entityName;
built->s.modelindex = buildable;
built->buildableTeam = built->s.modelindex2 = BG_Buildable( buildable )->team;
BG_BuildableBoundingBox( buildable, built->r.mins, built->r.maxs );
// detect the buildable's normal vector
if( !builder->client )
{
// initial base layout created by server
if( builder->s.origin2[ 0 ] ||
builder->s.origin2[ 1 ] ||
builder->s.origin2[ 2 ] )
{
VectorCopy( builder->s.origin2, normal );
}
else if( BG_Buildable( buildable )->traj == TR_BUOYANCY )
VectorSet( normal, 0.0f, 0.0f, -1.0f );
else
VectorSet( normal, 0.0f, 0.0f, 1.0f );
}
else
{
// in-game building by a player
BG_GetClientNormal( &builder->client->ps, normal );
}
// when building the initial layout, spawn the entity slightly off its
// target surface so that it can be "dropped" onto it
if( !builder->client )
VectorMA( origin, 1.0f, normal, origin );
built->health = 1;
built->splashDamage = BG_Buildable( buildable )->splashDamage;
built->splashRadius = BG_Buildable( buildable )->splashRadius;
built->splashMethodOfDeath = BG_Buildable( buildable )->meansOfDeath;
built->nextthink = BG_Buildable( buildable )->nextthink;
built->takedamage = qtrue;
built->spawned = qfalse;
built->buildTime = built->s.time = level.time;
// build instantly in cheat mode
if( builder->client && g_cheats.integer )
{
built->health = BG_Buildable( buildable )->health;
built->buildTime = built->s.time =
level.time - BG_Buildable( buildable )->buildTime;
}
//things that vary for each buildable that aren't in the dbase
switch( buildable )
{
case BA_A_SPAWN:
built->die = ASpawn_Die;
built->think = ASpawn_Think;
built->pain = AGeneric_Pain;
break;
case BA_A_BARRICADE:
built->die = ABarricade_Die;
built->think = ABarricade_Think;
built->pain = ABarricade_Pain;
built->touch = ABarricade_Touch;
built->shrunkTime = 0;
ABarricade_Shrink( built, qtrue );
break;
case BA_A_BOOSTER:
built->die = AGeneric_Die;
built->think = AGeneric_Think;
built->pain = AGeneric_Pain;
built->touch = ABooster_Touch;
break;
case BA_A_ACIDTUBE:
built->die = AGeneric_Die;
built->think = AAcidTube_Think;
built->pain = AGeneric_Pain;
break;
case BA_A_HIVE:
built->die = AGeneric_Die;
built->think = AHive_Think;
built->pain = AHive_Pain;
break;
case BA_A_TRAPPER:
built->die = AGeneric_Die;
built->think = ATrapper_Think;
built->pain = AGeneric_Pain;
break;
case BA_A_OVERMIND:
built->die = ASpawn_Die;
built->think = AOvermind_Think;
built->pain = AGeneric_Pain;
break;
case BA_A_HOVEL:
built->die = AHovel_Die;
built->use = AHovel_Use;
built->think = AHovel_Think;
built->pain = AGeneric_Pain;
break;
case BA_H_SPAWN:
built->die = HSpawn_Die;
built->think = HSpawn_Think;
break;
case BA_H_MGTURRET:
built->die = HSpawn_Die;
built->think = HMGTurret_Think;
break;
case BA_H_TESLAGEN:
built->die = HSpawn_Die;
built->think = HTeslaGen_Think;
break;
case BA_H_ARMOURY:
built->think = HArmoury_Think;
built->die = HSpawn_Die;
built->use = HArmoury_Activate;
break;
case BA_H_DCC:
built->think = HDCC_Think;
built->die = HSpawn_Die;
break;
case BA_H_MEDISTAT:
built->think = HMedistat_Think;
built->die = HMedistat_Die;
break;
case BA_H_REACTOR:
built->think = HReactor_Think;
built->die = HSpawn_Die;
built->use = HRepeater_Use;
built->powered = built->active = qtrue;
break;
case BA_H_REPEATER:
built->think = HRepeater_Think;
built->die = HRepeater_Die;
built->use = HRepeater_Use;
built->count = -1;
break;
default:
//erk
break;
}
built->s.number = built - g_entities;
built->r.contents = CONTENTS_BODY;
built->clipmask = MASK_PLAYERSOLID;
built->enemy = NULL;
built->s.weapon = BG_Buildable( buildable )->turretProjType;
if( builder->client )
built->builtBy = builder->client->ps.clientNum;
else
built->builtBy = -1;
G_SetOrigin( built, origin );
// gently nudge the buildable onto the surface :)
VectorScale( normal, -50.0f, built->s.pos.trDelta );
// set turret angles
VectorCopy( builder->s.angles2, built->s.angles2 );
VectorCopy( angles, built->s.angles );
built->s.angles[ PITCH ] = 0.0f;
built->s.angles2[ YAW ] = angles[ YAW ];
built->s.pos.trType = BG_Buildable( buildable )->traj;
built->s.pos.trTime = level.time;
built->physicsBounce = BG_Buildable( buildable )->bounce;
built->s.groundEntityNum = -1;
built->s.generic1 = (int)( ( (float)built->health /
(float)BG_Buildable( buildable )->health ) * B_HEALTH_MASK );
if( built->s.generic1 < 0 )
built->s.generic1 = 0;
if( BG_Buildable( buildable )->team == TEAM_ALIENS )
{
built->powered = qtrue;
built->s.eFlags |= EF_B_POWERED;
}
else if( ( built->powered = G_FindPower( built ) ) )
built->s.eFlags |= EF_B_POWERED;
built->s.eFlags &= ~EF_B_SPAWNED;
VectorCopy( normal, built->s.origin2 );
G_AddEvent( built, EV_BUILD_CONSTRUCT, 0 );
G_SetIdleBuildableAnim( built, BG_Buildable( buildable )->idleAnim );
if( built->builtBy >= 0 )
G_SetBuildableAnim( built, BANIM_CONSTRUCT1, qtrue );
trap_LinkEntity( built );
return built;
}
/*
=================
G_BuildIfValid
=================
*/
qboolean G_BuildIfValid( gentity_t *ent, buildable_t buildable )
{
float dist;
vec3_t origin;
dist = BG_Class( ent->client->ps.stats[ STAT_CLASS ] )->buildDist;
switch( G_CanBuild( ent, buildable, dist, origin ) )
{
case IBE_NONE:
G_Build( ent, buildable, origin, ent->s.apos.trBase );
return qtrue;
case IBE_NOALIENBP:
G_TriggerMenu( ent->client->ps.clientNum, MN_A_NOBP );
return qfalse;
case IBE_NOOVERMIND:
G_TriggerMenu( ent->client->ps.clientNum, MN_A_NOOVMND );
return qfalse;
case IBE_NOCREEP:
G_TriggerMenu( ent->client->ps.clientNum, MN_A_NOCREEP );
return qfalse;
case IBE_ONEOVERMIND:
G_TriggerMenu( ent->client->ps.clientNum, MN_A_ONEOVERMIND );
return qfalse;
case IBE_ONEHOVEL:
G_TriggerMenu( ent->client->ps.clientNum, MN_A_ONEHOVEL );
return qfalse;
case IBE_HOVELEXIT:
G_TriggerMenu( ent->client->ps.clientNum, MN_A_HOVEL_EXIT );
return qfalse;
case IBE_NORMAL:
G_TriggerMenu( ent->client->ps.clientNum, MN_B_NORMAL );
return qfalse;
case IBE_PERMISSION:
G_TriggerMenu( ent->client->ps.clientNum, MN_B_NORMAL );
return qfalse;
case IBE_ONEREACTOR:
G_TriggerMenu( ent->client->ps.clientNum, MN_H_ONEREACTOR );
return qfalse;
case IBE_NOPOWERHERE:
G_TriggerMenu( ent->client->ps.clientNum, MN_H_NOPOWERHERE );
return qfalse;
case IBE_NOROOM:
G_TriggerMenu( ent->client->ps.clientNum, MN_B_NOROOM );
return qfalse;
case IBE_NOHUMANBP:
G_TriggerMenu( ent->client->ps.clientNum, MN_H_NOBP);
return qfalse;
case IBE_NODCC:
G_TriggerMenu( ent->client->ps.clientNum, MN_H_NODCC );
return qfalse;
case IBE_RPTPOWERHERE:
G_TriggerMenu( ent->client->ps.clientNum, MN_H_RPTPOWERHERE );
return qfalse;
case IBE_LASTSPAWN:
G_TriggerMenu( ent->client->ps.clientNum, MN_B_LASTSPAWN );
return qfalse;
default:
break;
}
return qfalse;
}
/*
================
G_FinishSpawningBuildable
Traces down to find where an item should rest, instead of letting them
free fall from their spawn points
================
*/
static void G_FinishSpawningBuildable( gentity_t *ent )
{
trace_t tr;
vec3_t dest;
gentity_t *built;
buildable_t buildable = ent->s.modelindex;
built = G_Build( ent, buildable, ent->s.pos.trBase, ent->s.angles );
G_FreeEntity( ent );
built->takedamage = qtrue;
built->spawned = qtrue; //map entities are already spawned
built->health = BG_Buildable( buildable )->health;
built->s.eFlags |= EF_B_SPAWNED;
// drop towards normal surface
VectorScale( built->s.origin2, -4096.0f, dest );
VectorAdd( dest, built->s.origin, dest );
trap_Trace( &tr, built->s.origin, built->r.mins, built->r.maxs, dest, built->s.number, built->clipmask );
if( tr.startsolid )
{
G_Printf( S_COLOR_YELLOW "G_FinishSpawningBuildable: %s startsolid at %s\n",
built->classname, vtos( built->s.origin ) );
G_FreeEntity( built );
return;
}
//point items in the correct direction
VectorCopy( tr.plane.normal, built->s.origin2 );
// allow to ride movers
built->s.groundEntityNum = tr.entityNum;
G_SetOrigin( built, tr.endpos );
trap_LinkEntity( built );
}
/*
============
G_SpawnBuildable
Sets the clipping size and plants the object on the floor.
Items can't be immediately dropped to floor, because they might
be on an entity that hasn't spawned yet.
============
*/
void G_SpawnBuildable( gentity_t *ent, buildable_t buildable )
{
ent->s.modelindex = buildable;
// some movers spawn on the second frame, so delay item
// spawns until the third frame so they can ride trains
ent->nextthink = level.time + FRAMETIME * 2;
ent->think = G_FinishSpawningBuildable;
}
/*
============
G_LayoutSave
============
*/
void G_LayoutSave( char *name )
{
char map[ MAX_QPATH ];
char fileName[ MAX_OSPATH ];
fileHandle_t f;
int len;
int i;
gentity_t *ent;
char *s;
trap_Cvar_VariableStringBuffer( "mapname", map, sizeof( map ) );
if( !map[ 0 ] )
{
G_Printf( "LayoutSave( ): no map is loaded\n" );
return;
}
Com_sprintf( fileName, sizeof( fileName ), "layouts/%s/%s.dat", map, name );
len = trap_FS_FOpenFile( fileName, &f, FS_WRITE );
if( len < 0 )
{
G_Printf( "layoutsave: could not open %s\n", fileName );
return;
}
G_Printf( "layoutsave: saving layout to %s\n", fileName );
for( i = MAX_CLIENTS; i < level.num_entities; i++ )
{
ent = &level.gentities[ i ];
if( ent->s.eType != ET_BUILDABLE )
continue;
s = va( "%i %f %f %f %f %f %f %f %f %f %f %f %f\n",
ent->s.modelindex,
ent->s.pos.trBase[ 0 ],
ent->s.pos.trBase[ 1 ],
ent->s.pos.trBase[ 2 ],
ent->s.angles[ 0 ],
ent->s.angles[ 1 ],
ent->s.angles[ 2 ],
ent->s.origin2[ 0 ],
ent->s.origin2[ 1 ],
ent->s.origin2[ 2 ],
ent->s.angles2[ 0 ],
ent->s.angles2[ 1 ],
ent->s.angles2[ 2 ] );
trap_FS_Write( s, strlen( s ), f );
}
trap_FS_FCloseFile( f );
}
/*
============
G_LayoutList
============
*/
int G_LayoutList( const char *map, char *list, int len )
{
// up to 128 single character layout names could fit in layouts
char fileList[ ( MAX_CVAR_VALUE_STRING / 2 ) * 5 ] = {""};
char layouts[ MAX_CVAR_VALUE_STRING ] = {""};
int numFiles, i, fileLen = 0, listLen;
int count = 0;
char *filePtr;
Q_strcat( layouts, sizeof( layouts ), "*BUILTIN* " );
numFiles = trap_FS_GetFileList( va( "layouts/%s", map ), ".dat",
fileList, sizeof( fileList ) );
filePtr = fileList;
for( i = 0; i < numFiles; i++, filePtr += fileLen + 1 )
{
fileLen = strlen( filePtr );
listLen = strlen( layouts );
if( fileLen < 5 )
continue;
// list is full, stop trying to add to it
if( ( listLen + fileLen ) >= sizeof( layouts ) )
break;
Q_strcat( layouts, sizeof( layouts ), filePtr );
listLen = strlen( layouts );
// strip extension and add space delimiter
layouts[ listLen - 4 ] = ' ';
layouts[ listLen - 3 ] = '\0';
count++;
}
if( count != numFiles )
{
G_Printf( S_COLOR_YELLOW "WARNING: layout list was truncated to %d "
"layouts, but %d layout files exist in layouts/%s/.\n",
count, numFiles, map );
}
Q_strncpyz( list, layouts, len );
return count + 1;
}
/*
============
G_LayoutSelect
set level.layout based on g_layouts or g_layoutAuto
============
*/
void G_LayoutSelect( void )
{
char fileName[ MAX_OSPATH ];
char layouts[ MAX_CVAR_VALUE_STRING ];
char layouts2[ MAX_CVAR_VALUE_STRING ];
char *l;
char map[ MAX_QPATH ];
char *s;
int cnt = 0;
int layoutNum;
Q_strncpyz( layouts, g_layouts.string, sizeof( layouts ) );
trap_Cvar_VariableStringBuffer( "mapname", map, sizeof( map ) );
// one time use cvar
trap_Cvar_Set( "g_layouts", "" );
// pick an included layout at random if no list has been provided
if( !layouts[ 0 ] && g_layoutAuto.integer )
{
G_LayoutList( map, layouts, sizeof( layouts ) );
}
if( !layouts[ 0 ] )
return;
Q_strncpyz( layouts2, layouts, sizeof( layouts2 ) );
l = &layouts2[ 0 ];
layouts[ 0 ] = '\0';
while( 1 )
{
s = COM_ParseExt( &l, qfalse );
if( !*s )
break;
if( !Q_stricmp( s, "*BUILTIN*" ) )
{
Q_strcat( layouts, sizeof( layouts ), s );
Q_strcat( layouts, sizeof( layouts ), " " );
cnt++;
continue;
}
Com_sprintf( fileName, sizeof( fileName ), "layouts/%s/%s.dat", map, s );
if( trap_FS_FOpenFile( fileName, NULL, FS_READ ) > 0 )
{
Q_strcat( layouts, sizeof( layouts ), s );
Q_strcat( layouts, sizeof( layouts ), " " );
cnt++;
}
else
G_Printf( S_COLOR_YELLOW "WARNING: layout \"%s\" does not exist\n", s );
}
if( !cnt )
{
G_Printf( S_COLOR_RED "ERROR: none of the specified layouts could be "
"found, using map default\n" );
return;
}
layoutNum = ( rand( ) % cnt ) + 1;
cnt = 0;
Q_strncpyz( layouts2, layouts, sizeof( layouts2 ) );
l = &layouts2[ 0 ];
while( 1 )
{
s = COM_ParseExt( &l, qfalse );
if( !*s )
break;
Q_strncpyz( level.layout, s, sizeof( level.layout ) );
cnt++;
if( cnt >= layoutNum )
break;
}
G_Printf( "using layout \"%s\" from list (%s)\n", level.layout, layouts );
}
/*
============
G_LayoutBuildItem
============
*/
static void G_LayoutBuildItem( buildable_t buildable, vec3_t origin,
vec3_t angles, vec3_t origin2, vec3_t angles2 )
{
gentity_t *builder;
builder = G_Spawn( );
builder->client = 0;
VectorCopy( origin, builder->s.pos.trBase );
VectorCopy( angles, builder->s.angles );
VectorCopy( origin2, builder->s.origin2 );
VectorCopy( angles2, builder->s.angles2 );
G_SpawnBuildable( builder, buildable );
}
/*
============
G_LayoutLoad
load the layout .dat file indicated by level.layout and spawn buildables
as if a builder was creating them
============
*/
void G_LayoutLoad( void )
{
fileHandle_t f;
int len;
char *layout, *layoutHead;
char map[ MAX_QPATH ];
int buildable = BA_NONE;
vec3_t origin = { 0.0f, 0.0f, 0.0f };
vec3_t angles = { 0.0f, 0.0f, 0.0f };
vec3_t origin2 = { 0.0f, 0.0f, 0.0f };
vec3_t angles2 = { 0.0f, 0.0f, 0.0f };
char line[ MAX_STRING_CHARS ];
int i = 0;
if( !level.layout[ 0 ] || !Q_stricmp( level.layout, "*BUILTIN*" ) )
return;
trap_Cvar_VariableStringBuffer( "mapname", map, sizeof( map ) );
len = trap_FS_FOpenFile( va( "layouts/%s/%s.dat", map, level.layout ),
&f, FS_READ );
if( len < 0 )
{
G_Printf( "ERROR: layout %s could not be opened\n", level.layout );
return;
}
layoutHead = layout = BG_Alloc( len + 1 );
trap_FS_Read( layout, len, f );
layout[ len ] = '\0';
trap_FS_FCloseFile( f );
while( *layout )
{
if( i >= sizeof( line ) - 1 )
{
G_Printf( S_COLOR_RED "ERROR: line overflow in %s before \"%s\"\n",
va( "layouts/%s/%s.dat", map, level.layout ), line );
break;
}
line[ i++ ] = *layout;
line[ i ] = '\0';
if( *layout == '\n' )
{
i = 0;
sscanf( line, "%d %f %f %f %f %f %f %f %f %f %f %f %f\n",
&buildable,
&origin[ 0 ], &origin[ 1 ], &origin[ 2 ],
&angles[ 0 ], &angles[ 1 ], &angles[ 2 ],
&origin2[ 0 ], &origin2[ 1 ], &origin2[ 2 ],
&angles2[ 0 ], &angles2[ 1 ], &angles2[ 2 ] );
if( buildable > BA_NONE && buildable < BA_NUM_BUILDABLES )
G_LayoutBuildItem( buildable, origin, angles, origin2, angles2 );
else
G_Printf( S_COLOR_YELLOW "WARNING: bad buildable number (%d) in "
" layout. skipping\n", buildable );
}
layout++;
}
BG_Free( layoutHead );
}
/*
============
G_BaseSelfDestruct
============
*/
void G_BaseSelfDestruct( team_t team )
{
int i;
gentity_t *ent;
for( i = MAX_CLIENTS; i < level.num_entities; i++ )
{
ent = &level.gentities[ i ];
if( ent->health <= 0 )
continue;
if( ent->s.eType != ET_BUILDABLE )
continue;
if( ent->buildableTeam != team )
continue;
G_Damage( ent, NULL, NULL, NULL, NULL, 10000, 0, MOD_SUICIDE );
}
}
|