File: g_mover.c

package info (click to toggle)
iortcw 1.51.c%2Bdfsg1-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 25,304 kB
  • sloc: ansic: 457,326; cpp: 6,507; makefile: 4,737; sh: 1,292; asm: 1,176; xml: 31
file content (4590 lines) | stat: -rw-r--r-- 123,615 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
/*
===========================================================================

Return to Castle Wolfenstein multiplayer GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company. 

This file is part of the Return to Castle Wolfenstein multiplayer GPL Source Code (“RTCW MP Source Code”).  

RTCW MP Source Code 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 3 of the License, or
(at your option) any later version.

RTCW MP Source Code 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 RTCW MP Source Code.  If not, see <http://www.gnu.org/licenses/>.

In addition, the RTCW MP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW MP Source Code.  If not, please request a copy in writing from id Software at the address below.

If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.

===========================================================================
*/


/*
 * name:		g_mover.c
 *
 * desc:
 *
*/

#include "g_local.h"

char *hintStrings[] = {
	"",                  // HINT_NONE
	"HINT_NONE",     // actually HINT_FORCENONE, but since this is being specified in the ent, the designer actually means HINT_FORCENONE
	"HINT_PLAYER",
	"HINT_ACTIVATE",
	"HINT_DOOR",
	"HINT_DOOR_ROTATING",
	"HINT_DOOR_LOCKED",
	"HINT_DOOR_ROTATING_LOCKED",
	"HINT_MG42",
	"HINT_BREAKABLE",
	"HINT_BREAKABLE_BIG",
	"HINT_CHAIR",
	"HINT_ALARM",
	"HINT_HEALTH",
	"HINT_TREASURE",
	"HINT_KNIFE",
	"HINT_LADDER",
	"HINT_BUTTON",
	"HINT_WATER",
	"HINT_CAUTION",
	"HINT_DANGER",
	"HINT_SECRET",
	"HINT_QUESTION",
	"HINT_EXCLAMATION",
	"HINT_CLIPBOARD",
	"HINT_WEAPON",
	"HINT_AMMO",
	"HINT_ARMOR",
	"HINT_POWERUP",
	"HINT_HOLDABLE",
	"HINT_INVENTORY",
	"HINT_SCENARIC",
	"HINT_EXIT",
	"HINT_PLYR_FRIEND",
	"HINT_PLYR_NEUTRAL",
	"HINT_PLYR_ENEMY",
	"HINT_PLYR_UNKNOWN",
	"HINT_BUILD",                // DHM - Nerve
	"HINT_DISARM",               // DHM - Nerve
	"HINT_REVIVE",               // DHM - Nerve
	"HINT_DYNAMITE",         // DHM - Nerve


	"",                  // HINT_BAD_USER
};

/*
===============================================================================

PUSHMOVE

===============================================================================
*/

void Reached_Train( gentity_t *ent );
void Think_BeginMoving( gentity_t *ent );
void Use_Func_Rotate( gentity_t * ent, gentity_t * other, gentity_t * activator );
void Blocked_Door( gentity_t *ent, gentity_t *other );
void Blocked_DoorRotate( gentity_t *ent, gentity_t *other );

#define PUSH_STACK_DEPTH 3
int pushedStackDepth = 0;

typedef struct {
	gentity_t   *ent;
	vec3_t origin;
	vec3_t angles;
	float deltayaw;
} pushed_t;
pushed_t pushed[MAX_GENTITIES * PUSH_STACK_DEPTH], *pushed_p;       // Arnout *PUSH_STACK_DEPTH to prevent overflows


/*
============
G_TestEntityPosition

============
*/
gentity_t *G_TestEntityPosition( gentity_t *ent ) {
	trace_t tr;
	int mask;

	if ( ent->clipmask ) {
//		if ( ent->r.contents == CONTENTS_CORPSE && ent->health <= 0 ) {	// Arnout: players waiting to be revived are important
//		if ( ent->r.contents == CONTENTS_CORPSE ) {
		// corpse aren't important
		//G_Damage( ent, NULL, NULL, NULL, NULL, 99999, 0, MOD_CRUSH );
//			return NULL;
//		} else {
		mask = ent->clipmask;
//		}
	} else {
		mask = MASK_SOLID;
	}
	if ( ent->client ) {
		trap_TraceCapsule( &tr, ent->client->ps.origin, ent->r.mins, ent->r.maxs, ent->client->ps.origin, ent->s.number, mask );
	} else if ( ent->s.eType == ET_CORPSE ) {
		vec3_t pos;
		VectorCopy( ent->s.pos.trBase, pos );
		pos[2] += 4; // move up a bit - corpses normally got their origin slightly in the ground
		trap_Trace( &tr, pos, ent->r.mins, ent->r.maxs, pos, ent->s.number, mask );
		// don't crush corpses against players
//		if( tr.startsolid && g_entities[ tr.entityNum ].client )
//			return NULL;
	} else if ( ent->s.eType == ET_MISSILE ) {
		trap_Trace( &tr, ent->s.pos.trBase, ent->r.mins, ent->r.maxs, ent->s.pos.trBase, ent->r.ownerNum, mask );
	} else {
		trap_Trace( &tr, ent->s.pos.trBase, ent->r.mins, ent->r.maxs, ent->s.pos.trBase, ent->s.number, mask );
	}

	if ( tr.startsolid ) {
		return &g_entities[ tr.entityNum ];
	}

	return NULL;
}

/*
============
G_TestEntityDropToFloor

============
*/
void G_TestEntityDropToFloor( gentity_t *ent, float maxdrop ) {
	trace_t tr;
	int mask;
	vec3_t endpos;

	if ( ent->clipmask ) {
		mask = ent->clipmask;
	} else {
		mask = MASK_SOLID;
	}
	if ( ent->client ) {
		VectorCopy( ent->client->ps.origin, endpos );
	} else {
		VectorCopy( ent->s.pos.trBase, endpos );
	}

	endpos[2] -= maxdrop;
	if ( ent->client ) {
		trap_TraceCapsule( &tr, ent->client->ps.origin, ent->r.mins, ent->r.maxs, endpos, ent->s.number, mask );
	} else {
		trap_Trace( &tr, ent->s.pos.trBase, ent->r.mins, ent->r.maxs, endpos, ent->s.number, mask );
	}

	VectorCopy( tr.endpos, ent->s.pos.trBase );
	if ( ent->client ) {
		VectorCopy( tr.endpos, ent->client->ps.origin );
	}
}

/*
============
G_TestEntityMoveTowardsPos

============
*/
void G_TestEntityMoveTowardsPos( gentity_t *ent, vec3_t pos ) {
	trace_t tr;
	int mask;

	if ( ent->clipmask ) {
		mask = ent->clipmask;
	} else {
		mask = MASK_SOLID;
	}
	if ( ent->client ) {
		trap_TraceCapsule( &tr, ent->client->ps.origin, ent->r.mins, ent->r.maxs, pos, ent->s.number, mask );
	} else {
		trap_Trace( &tr, ent->s.pos.trBase, ent->r.mins, ent->r.maxs, pos, ent->s.number, mask );
	}

	VectorCopy( tr.endpos, ent->s.pos.trBase );
	if ( ent->client ) {
		VectorCopy( tr.endpos, ent->client->ps.origin );
	}
}

/*
================
G_CreateRotationMatrix
================
*/
void G_CreateRotationMatrix( const vec3_t angles, vec3_t matrix[3] ) {
	AngleVectors( angles, matrix[0], matrix[1], matrix[2] );
	VectorInverse( matrix[1] );
}

/*
================
G_TransposeMatrix
================
*/
void G_TransposeMatrix( const vec3_t matrix[3], vec3_t transpose[3] ) {
	int i, j;
	for ( i = 0; i < 3; i++ ) {
		for ( j = 0; j < 3; j++ ) {
			transpose[i][j] = matrix[j][i];
		}
	}
}

/*
================
G_RotatePoint
================
*/
void G_RotatePoint( vec3_t point, const vec3_t matrix[3] ) {
	vec3_t tvec;

	VectorCopy( point, tvec );
	point[0] = DotProduct( matrix[0], tvec );
	point[1] = DotProduct( matrix[1], tvec );
	point[2] = DotProduct( matrix[2], tvec );
}


/*
==================
G_TryPushingEntity

Returns qfalse if the move is blocked
==================
*/
qboolean G_TryPushingEntity( gentity_t *check, gentity_t *pusher, vec3_t move, vec3_t amove ) {
	vec3_t org, org2, move2;
	gentity_t   *block;
	vec3_t matrix[3], transpose[3];
	float x, fx, y, fy, z, fz;
#define JITTER_INC  4
#define JITTER_MAX  ( check->r.maxs[0] / 2.0 )

	// EF_MOVER_STOP will just stop when contacting another entity
	// instead of pushing it, but entities can still ride on top of it
	if ( ( pusher->s.eFlags & EF_MOVER_STOP ) &&
		 check->s.groundEntityNum != pusher->s.number ) {
		//pusher->s.eFlags |= EF_MOVER_BLOCKED;
		return qfalse;
	}

	// save off the old position
	if ( pushed_p > &pushed[MAX_GENTITIES * PUSH_STACK_DEPTH] ) {
		G_Error( "pushed_p > &pushed[MAX_GENTITIES*PUSH_STACK_DEPTH]" );
	}
	pushed_p->ent = check;
	VectorCopy( check->s.pos.trBase, pushed_p->origin );
	VectorCopy( check->s.apos.trBase, pushed_p->angles );
	if ( check->client ) {
		pushed_p->deltayaw = check->client->ps.delta_angles[YAW];
		VectorCopy( check->client->ps.origin, pushed_p->origin );
	}
	pushed_p++;

	// try moving the contacted entity
	VectorAdd( check->s.pos.trBase, move, check->s.pos.trBase );
	if ( check->client ) {
		// make sure the client's view rotates when on a rotating mover
		// RF, this is done client-side now
		check->client->ps.delta_angles[YAW] += ANGLE2SHORT( amove[YAW] );
		//
		// RF, AI's need their ideal angle adjusted instead
		if ( check->aiCharacter ) {
			AICast_AdjustIdealYawForMover( check->s.number, ANGLE2SHORT( amove[YAW] ) );
		}
	}

	// figure movement due to the pusher's amove
	G_CreateRotationMatrix( amove, transpose );
	G_TransposeMatrix( (const vec3_t *)transpose, matrix );
	VectorSubtract( check->s.pos.trBase, pusher->r.currentOrigin, org );
	if ( check->client ) {
		VectorSubtract( check->client->ps.origin, pusher->r.currentOrigin, org );
	}
	VectorCopy( org, org2 );
	G_RotatePoint( org2, (const vec3_t *)matrix );
	VectorSubtract( org2, org, move2 );
	VectorAdd( check->s.pos.trBase, move2, check->s.pos.trBase );
	if ( check->client ) {
		VectorAdd( check->client->ps.origin, move, check->client->ps.origin );
		VectorAdd( check->client->ps.origin, move2, check->client->ps.origin );
	}

	// may have pushed them off an edge
	if ( check->s.groundEntityNum != pusher->s.number ) {
		check->s.groundEntityNum = ENTITYNUM_NONE;
	}

	block = G_TestEntityPosition( check );
	if ( !block ) {
		// pushed ok
		if ( check->client ) {
			VectorCopy( check->client->ps.origin, check->r.currentOrigin );
		} else {
			VectorCopy( check->s.pos.trBase, check->r.currentOrigin );
		}
		return qtrue;
	}

// TESTTEST
	// Arnout, if blocking entity is a player, try to move this player first.
	if ( block->client ) {
		pushedStackDepth++;
		if ( pushedStackDepth < PUSH_STACK_DEPTH && G_TryPushingEntity( block, pusher, move, amove ) ) {
			// pushed ok
			if ( check->client ) {
				VectorCopy( check->client->ps.origin, check->r.currentOrigin );
			} else {
				VectorCopy( check->s.pos.trBase, check->r.currentOrigin );
			}
			return qtrue;
		}
		pushedStackDepth--;
	}
// TESTTEST

	// RF, if still not valid, move them around to see if we can find a good spot
	if ( JITTER_MAX > JITTER_INC ) {
		VectorCopy( check->s.pos.trBase, org );
		if ( check->client ) {
			VectorCopy( check->client->ps.origin, org );
		}
		for ( z = 0; z < JITTER_MAX; z += JITTER_INC )
			for ( fz = -z; fz <= z; fz += 2 * z ) {
				for ( x = JITTER_INC; x < JITTER_MAX; x += JITTER_INC )
					for ( fx = -x; fx <= x; fx += 2 * x ) {
						for ( y = JITTER_INC; y < JITTER_MAX; y += JITTER_INC )
							for ( fy = -y; fy <= y; fy += 2 * y ) {
								VectorSet( move2, fx, fy, fz );
								VectorAdd( org, move2, org2 );
								VectorCopy( org2, check->s.pos.trBase );
								if ( check->client ) {
									VectorCopy( org2, check->client->ps.origin );
								}

								//
								// do the test
								block = G_TestEntityPosition( check );
								if ( !block ) {
									// pushed ok
									if ( check->client ) {
										VectorCopy( check->client->ps.origin, check->r.currentOrigin );
									} else {
										VectorCopy( check->s.pos.trBase, check->r.currentOrigin );
									}
									return qtrue;
								}
							}
					}
				if ( !fz ) {
					break;
				}
			}
		// didn't work, so set the position back
		VectorCopy( org, check->s.pos.trBase );
		if ( check->client ) {
			VectorCopy( org, check->client->ps.origin );
		}
	}

	// if it is ok to leave in the old position, do it
	// this is only relevent for riding entities, not pushed
	// Sliding trapdoors can cause this.
	VectorCopy( ( pushed_p - 1 )->origin, check->s.pos.trBase );
	if ( check->client ) {
		VectorCopy( ( pushed_p - 1 )->origin, check->client->ps.origin );
	}
	VectorCopy( ( pushed_p - 1 )->angles, check->s.apos.trBase );
	block = G_TestEntityPosition( check );
	if ( !block ) {
		check->s.groundEntityNum = ENTITYNUM_NONE;
		pushed_p--;
		return qtrue;
	}
	// blocked
	return qfalse;
}


/*
============
G_MoverPush

Objects need to be moved back on a failed push,
otherwise riders would continue to slide.
If qfalse is returned, *obstacle will be the blocking entity
============
*/
qboolean G_MoverPush( gentity_t *pusher, vec3_t move, vec3_t amove, gentity_t **obstacle ) {
	int i, e;
	gentity_t   *check;
	vec3_t mins, maxs;
	pushed_t    *p;
	int entityList[MAX_GENTITIES];
	int moveList[MAX_GENTITIES];
	int listedEntities, moveEntities;
	vec3_t totalMins, totalMaxs;

	*obstacle = NULL;


	// mins/maxs are the bounds at the destination
	// totalMins / totalMaxs are the bounds for the entire move
	if ( pusher->r.currentAngles[0] || pusher->r.currentAngles[1] || pusher->r.currentAngles[2]
		 || amove[0] || amove[1] || amove[2] ) {
		float radius;

		radius = RadiusFromBounds( pusher->r.mins, pusher->r.maxs );
		for ( i = 0; i < 3; i++ ) {
			mins[i] = pusher->r.currentOrigin[i] - radius + move[i];
			maxs[i] = pusher->r.currentOrigin[i] + radius + move[i];
			totalMins[i] = pusher->r.currentOrigin[i] - radius;
			totalMaxs[i] = pusher->r.currentOrigin[i] + radius;
		}
	} else {
		for ( i = 0 ; i < 3 ; i++ ) {
			mins[i] = pusher->r.absmin[i] + move[i];
			maxs[i] = pusher->r.absmax[i] + move[i];
		}

		VectorCopy( pusher->r.absmin, totalMins );
		VectorCopy( pusher->r.absmax, totalMaxs );
	}
	for ( i = 0; i < 3; i++ ) {
		if ( move[i] > 0 ) {
			totalMaxs[i] += move[i];
		} else {
			totalMins[i] += move[i];
		}
	}

	// unlink the pusher so we don't get it in the entityList
	trap_UnlinkEntity( pusher );

	listedEntities = trap_EntitiesInBox( totalMins, totalMaxs, entityList, MAX_GENTITIES );

	// move the pusher to its final position
	VectorAdd( pusher->r.currentOrigin, move, pusher->r.currentOrigin );
	VectorAdd( pusher->r.currentAngles, amove, pusher->r.currentAngles );
	trap_LinkEntity( pusher );

	moveEntities = 0;
	// see if any solid entities are inside the final position
	for ( e = 0 ; e < listedEntities ; e++ ) {
		check = &g_entities[ entityList[ e ] ];

		if ( check->s.eType == ET_ALARMBOX ) {
			continue;
		}

		if ( check->isProp && check->s.eType == ET_PROP ) {
			continue;
		}

		// only push items and players
		if ( check->s.eType != ET_MISSILE && check->s.eType != ET_ITEM && check->s.eType != ET_PLAYER && !check->physicsObject ) {
			continue;
		}

		if ( check->s.eType == ET_ITEM && check->item->giType == IT_CLIPBOARD && ( check->spawnflags & 1 ) ) {
			continue;
		}

		//if ( check->s.eType == ET_MISSILE && VectorLength( check->s.pos.trDelta ) ) {
		//	continue;	// it's moving
		//}

		// if the entity is standing on the pusher, it will definitely be moved
		if ( check->s.groundEntityNum != pusher->s.number ) {
			// see if the ent needs to be tested
			if ( check->r.absmin[0] >= maxs[0]
				 || check->r.absmin[1] >= maxs[1]
				 || check->r.absmin[2] >= maxs[2]
				 || check->r.absmax[0] <= mins[0]
				 || check->r.absmax[1] <= mins[1]
				 || check->r.absmax[2] <= mins[2] ) {
				continue;
			}
			// see if the ent's bbox is inside the pusher's final position
			// this does allow a fast moving object to pass through a thin entity...
			if ( G_TestEntityPosition( check ) != pusher ) {
				continue;
			}
		}

		moveList[moveEntities++] = entityList[e];
	}

	// unlink all to be moved entities so they cannot get stuck in each other
	for ( e = 0; e < moveEntities; e++ ) {
		check = &g_entities[ moveList[e] ];

		trap_UnlinkEntity( check );
	}

	for ( e = 0; e < moveEntities; e++ ) {
		check = &g_entities[ moveList[e] ];

		// the entity needs to be pushed
		pushedStackDepth = 0;   // Arnout: new push, reset stack depth
		if ( G_TryPushingEntity( check, pusher, move, amove ) ) {
			// link it in now so nothing else tries to clip into us
			trap_LinkEntity( check );
			continue;
		}

		// the move was blocked an entity

		// bobbing entities are instant-kill and never get blocked
		if ( pusher->s.pos.trType == TR_SINE || pusher->s.apos.trType == TR_SINE ) {
			G_Damage( check, pusher, pusher, NULL, NULL, 99999, 0, MOD_CRUSH );
			continue;
		}


		// save off the obstacle so we can call the block function (crush, etc)
		*obstacle = check;

		// move back any entities we already moved
		// go backwards, so if the same entity was pushed
		// twice, it goes back to the original position
		for ( p = pushed_p - 1 ; p >= pushed ; p-- ) {
			VectorCopy( p->origin, p->ent->s.pos.trBase );
			VectorCopy( p->angles, p->ent->s.apos.trBase );
			if ( p->ent->client ) {
				p->ent->client->ps.delta_angles[YAW] = p->deltayaw;
				VectorCopy( p->origin, p->ent->client->ps.origin );
			}
		}
		// link all entities at their original position
		for ( e = 0; e < moveEntities; e++ ) {
			check = &g_entities[ moveList[e] ];

			trap_LinkEntity( check );
		}
		// movement failed
		return qfalse;
	}
	// link all entities at their final position
	for ( e = 0; e < moveEntities; e++ ) {
		check = &g_entities[ moveList[e] ];

		trap_LinkEntity( check );
	}
	// movement was successfull
	return qtrue;
}


/*
=================
G_MoverTeam
=================
*/
void G_MoverTeam( gentity_t *ent ) {
	vec3_t move, amove;
	gentity_t   *part, *obstacle;
	vec3_t origin, angles;

	obstacle = NULL;

	// make sure all team slaves can move before commiting
	// any moves or calling any think functions
	// if the move is blocked, all moved objects will be backed out
	pushed_p = pushed;
	for ( part = ent ; part ; part = part->teamchain ) {
		// get current position
		BG_EvaluateTrajectory( &part->s.pos, level.time, origin );
		BG_EvaluateTrajectory( &part->s.apos, level.time, angles );
		VectorSubtract( origin, part->r.currentOrigin, move );
		VectorSubtract( angles, part->r.currentAngles, amove );

		if ( part->s.eFlags == EF_MOVER_STOP ) {
			part->s.eFlags &= ~EF_MOVER_BLOCKED;
		}

		if ( part->s.eType == ET_BAT && part->model && !G_MoverPush( part, move, amove, &obstacle ) ) {
			break;
		} else if ( !G_MoverPush( part, move, amove, &obstacle ) )    {
			break;  // move was blocked
		}
	}

	if ( part ) {
		// go back to the previous position
		for ( part = ent ; part ; part = part->teamchain ) {
			part->s.pos.trTime += level.time - level.previousTime;
			part->s.apos.trTime += level.time - level.previousTime;
			BG_EvaluateTrajectory( &part->s.pos, level.time, part->r.currentOrigin );
			BG_EvaluateTrajectory( &part->s.apos, level.time, part->r.currentAngles );
			trap_LinkEntity( part );
		}

		// if the pusher has a "blocked" function, call it
		if ( ent->blocked ) {
			ent->blocked( ent, obstacle );
		}
		return;
	}

	// the move succeeded
	for ( part = ent ; part ; part = part->teamchain ) {
		// call the reached function if time is at or past end point

		// opening/closing sliding door
		if ( part->s.pos.trType == TR_LINEAR_STOP ) {
			if ( level.time >= part->s.pos.trTime + part->s.pos.trDuration ) {
				if ( part->reached ) {
					part->reached( part );
				}
			}
		}
//----(SA)	removed
		// opening or closing rotating door
		else if ( part->s.apos.trType == TR_LINEAR_STOP ) {
			if ( level.time >= part->s.apos.trTime + part->s.apos.trDuration ) {
				if ( part->reached ) {
					part->reached( part );
				}
			}
		}
	}
}

/*
================
G_RunMover

================
*/
void G_RunMover( gentity_t *ent ) {
	// if not a team captain, don't do anything, because
	// the captain will handle everything
	if ( ent->flags & FL_TEAMSLAVE ) {
		// FIXME
		// hack to fix problem of tram car slaves being linked
		// after being unlinked in G_FindTeams
		if ( ent->r.linked && !Q_stricmp( ent->classname, "func_tramcar" ) ) {
			trap_UnlinkEntity( ent );
		}
		// Sigh... need to figure out why re links in
		else if ( ent->r.linked && !Q_stricmp( ent->classname, "func_rotating" ) ) {
			trap_UnlinkEntity( ent );
		}
		return;
	}

	// if stationary at one of the positions, don't move anything
	if ( ent->s.pos.trType != TR_STATIONARY || ent->s.apos.trType != TR_STATIONARY ) {
		G_MoverTeam( ent );
	}

	// check think function
	G_RunThink( ent );
}

/*
============================================================================

GENERAL MOVERS

Doors, plats, and buttons are all binary (two position) movers
Pos1 is "at rest", pos2 is "activated"
============================================================================
*/

/*
===============
SetMoverState
===============
*/
void SetMoverState( gentity_t *ent, moverState_t moverState, int time ) {
	vec3_t delta;
	float f;
	qboolean kicked = qfalse, soft = qfalse;

	kicked = (qboolean)( ent->flags & FL_KICKACTIVATE );
	soft = (qboolean)( ent->flags & FL_SOFTACTIVATE );    //----(SA)	added

	ent->moverState     = moverState;
	ent->s.pos.trTime   = time;
	ent->s.apos.trTime  = time;
	switch ( moverState ) {
	case MOVER_POS1:
		VectorCopy( ent->pos1, ent->s.pos.trBase );
		ent->s.pos.trType = TR_STATIONARY;
		ent->active = qfalse;
		break;
	case MOVER_POS2:
		VectorCopy( ent->pos2, ent->s.pos.trBase );
		ent->s.pos.trType = TR_STATIONARY;
		break;

		// JOSEPH 1-26-00
	case MOVER_POS3:
		VectorCopy( ent->pos3, ent->s.pos.trBase );
		ent->s.pos.trType = TR_STATIONARY;
		break;

	case MOVER_2TO3:
		VectorCopy( ent->pos2, ent->s.pos.trBase );
		VectorSubtract( ent->pos3, ent->pos2, delta );
		f = 1000.0 / ent->s.pos.trDuration;
		VectorScale( delta, f, ent->s.pos.trDelta );
		ent->s.pos.trType = TR_LINEAR_STOP;
		break;
	case MOVER_3TO2:
		VectorCopy( ent->pos3, ent->s.pos.trBase );
		VectorSubtract( ent->pos2, ent->pos3, delta );
		f = 1000.0 / ent->s.pos.trDuration;
		VectorScale( delta, f, ent->s.pos.trDelta );
		ent->s.pos.trType = TR_LINEAR_STOP;
		break;
		// END JOSEPH

	case MOVER_1TO2:        // opening
		VectorCopy( ent->pos1, ent->s.pos.trBase );
		VectorSubtract( ent->pos2, ent->pos1, delta );
//----(SA)	numerous changes start here
		ent->s.pos.trDuration = ent->gDuration;
		f = 1000.0 / ent->s.pos.trDuration;
		VectorScale( delta, f, ent->s.pos.trDelta );
		ent->s.pos.trType = TR_LINEAR_STOP;
		break;
	case MOVER_2TO1:        // closing
		VectorCopy( ent->pos2, ent->s.pos.trBase );
		VectorSubtract( ent->pos1, ent->pos2, delta );
		if ( ent->closespeed ) {                        //----(SA)	handle doors with different close speeds
			ent->s.pos.trDuration = ent->gDurationBack;
			f = 1000.0 / ent->gDurationBack;
		} else {
			ent->s.pos.trDuration = ent->gDuration;
			f = 1000.0 / ent->s.pos.trDuration;
		}
		VectorScale( delta, f, ent->s.pos.trDelta );
		ent->s.pos.trType = TR_LINEAR_STOP;
		break;


	case MOVER_POS1ROTATE:      // at close
		VectorCopy( ent->r.currentAngles, ent->s.apos.trBase );
		ent->s.apos.trType = TR_STATIONARY;
		break;
	case MOVER_POS2ROTATE:      // at open
		VectorCopy( ent->r.currentAngles, ent->s.apos.trBase );
		ent->s.apos.trType = TR_STATIONARY;
		break;
	case MOVER_1TO2ROTATE:      // opening
		VectorClear( ent->s.apos.trBase );              // set base to start position {0,0,0}

		if ( kicked ) {
			f = 2000.0 / ent->gDuration;        // double speed when kicked open
			ent->s.apos.trDuration = ent->gDuration / 2.0;
		} else if ( soft ) {
			f = 500.0 / ent->gDuration;         // 1/2 speed when soft opened
			ent->s.apos.trDuration = ent->gDuration * 2;
		} else {
			f = 1000.0 / ent->gDuration;
//				ent->s.apos.trDuration = ent->gDurationBack;	// (SA) durationback?
			ent->s.apos.trDuration = ent->gDuration;
		}
		VectorScale( ent->rotate, f * ent->angle, ent->s.apos.trDelta );
		ent->s.apos.trType = TR_LINEAR_STOP;
		break;
	case MOVER_2TO1ROTATE:      // closing
		VectorScale( ent->rotate, ent->angle, ent->s.apos.trBase );     // set base to end position
		// (kicked closes same as normally opened)
		// (soft closes at 1/2 speed)
		f = 1000.0 / ent->gDuration;
		ent->s.apos.trDuration = ent->gDuration;
		if ( soft ) {
			ent->s.apos.trDuration *= 2;
			f *= 0.5f;
		}
		VectorScale( ent->s.apos.trBase, -f, ent->s.apos.trDelta );
		ent->s.apos.trType = TR_LINEAR_STOP;
		ent->active = qfalse;
		break;


	}
	BG_EvaluateTrajectory( &ent->s.pos, level.time, ent->r.currentOrigin );
//	if (!(ent->r.svFlags & SVF_NOCLIENT) || (ent->r.contents))	// RF, added this for bats, but this is safe for all movers, since if they aren't solid, and aren't visible to the client, they don't need to be linked
//		trap_LinkEntity( ent );

	if ( !( ent->r.svFlags & SVF_NOCLIENT ) || ( ent->r.contents ) ) {    // RF, added this for bats, but this is safe for all movers, since if they aren't solid, and aren't visible to the client, they don't need to be linked
		trap_LinkEntity( ent );
		// if this entity is blocking AAS, then update it
		if ( ent->AASblocking && ent->s.pos.trType == TR_STATIONARY ) {
			// reset old blocking areas
			G_SetAASBlockingEntity( ent, qfalse );
			// set new areas
			G_SetAASBlockingEntity( ent, qtrue );
		}
	}
}

/*
================
MatchTeam

All entities in a mover team will move from pos1 to pos2
in the same amount of time
================
*/
void MatchTeam( gentity_t *teamLeader, int moverState, int time ) {
	gentity_t       *slave;

	for ( slave = teamLeader ; slave ; slave = slave->teamchain ) {

		// pass along flags for how door was activated
		if ( teamLeader->flags & FL_KICKACTIVATE ) {
			slave->flags |= FL_KICKACTIVATE;
		}
		if ( teamLeader->flags & FL_SOFTACTIVATE ) {
			slave->flags |= FL_SOFTACTIVATE;
		}

		SetMoverState( slave, moverState, time );
	}
}

/*
MatchTeamReverseAngleOnSlaves

the activator was blocking the door so reverse its direction
*/
void MatchTeamReverseAngleOnSlaves( gentity_t *teamLeader, int moverState, int time ) {
	gentity_t       *slave;

	for ( slave = teamLeader ; slave ; slave = slave->teamchain ) {
		// reverse open dir for teamLeader and all slaves
		slave->angle *= -1;

		// pass along flags for how door was activated
		if ( teamLeader->flags & FL_KICKACTIVATE ) {
			slave->flags |= FL_KICKACTIVATE;
		}
		if ( teamLeader->flags & FL_SOFTACTIVATE ) {
			slave->flags |= FL_SOFTACTIVATE;
		}

		SetMoverState( slave, moverState, time );
	}
}

/*
================
ReturnToPos1
================
*/
void ReturnToPos1( gentity_t *ent ) {
	MatchTeam( ent, MOVER_2TO1, level.time );

	// play starting sound
	G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );

	ent->s.loopSound = 0;
	// set looping sound
	ent->s.loopSound = ent->sound3to2;

}

// JOSEPH 1-26-00
/*
================
ReturnToPos2
================
*/
void ReturnToPos2( gentity_t *ent ) {
	MatchTeam( ent, MOVER_3TO2, level.time );

	ent->s.loopSound = 0;
	// looping sound
	ent->s.loopSound = ent->soundLoop;

	// starting sound
	G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound3to2 );
}

/*
================
GotoPos3
================
*/
void GotoPos3( gentity_t *ent ) {
	MatchTeam( ent, MOVER_2TO3, level.time );

	ent->s.loopSound = 0;
	// looping sound
	ent->s.loopSound = ent->soundLoop;

	// starting sound
	G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to3 );
}
// END JOSEPH

/*
================
ReturnToPos1Rotate
	closing
================
*/
void ReturnToPos1Rotate( gentity_t *ent ) {
	qboolean inPVS = qfalse;
	gentity_t   *player;

	MatchTeam( ent, MOVER_2TO1ROTATE, level.time );

	player = AICast_FindEntityForName( "player" );

	if ( player ) {
		inPVS = trap_InPVS( player->r.currentOrigin, ent->r.currentOrigin );
	}

	// play starting sound
	if ( inPVS ) {
		if ( ent->flags & FL_SOFTACTIVATE ) {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundSoftclose );
		} else {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
		}
	}

	ent->s.loopSound = ent->sound3to2;
}

/*
================
Reached_BinaryMover
================
*/
void Reached_BinaryMover( gentity_t *ent ) {

	// stop the looping sound
	ent->s.loopSound = 0;
//	ent->s.loopSound = ent->soundLoop;

	if ( ent->moverState == MOVER_1TO2 ) {
		// reached pos2
		SetMoverState( ent, MOVER_POS2, level.time );

		// play sound
		if ( ent->flags & FL_SOFTACTIVATE ) {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundSoftendo );
		} else {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos2 );
		}

		// fire targets
		if ( !ent->activator ) {
			ent->activator = ent;
		}

		G_UseTargets( ent, ent->activator );

		if ( ent->flags & FL_TOGGLE ) {
			ent->think = ReturnToPos1;
			ent->nextthink = 0;
			return;
		}

		// JOSEPH 1-27-00
		// return to pos1 after a delay
		if ( ent->wait != -1000 ) {
			ent->think = ReturnToPos1;
			ent->nextthink = level.time + ent->wait;
		}
		// END JOSEPH
	} else if ( ent->moverState == MOVER_2TO1 ) {
		// reached pos1
		SetMoverState( ent, MOVER_POS1, level.time );

		// play sound
		if ( ent->flags & FL_SOFTACTIVATE ) {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundSoftendc );
		} else {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos1 );
		}

		// close areaportals
		if ( ent->teammaster == ent || !ent->teammaster ) {
			trap_AdjustAreaPortalState( ent, qfalse );
		}
	} else if ( ent->moverState == MOVER_1TO2ROTATE )   {
		// reached pos2
		SetMoverState( ent, MOVER_POS2ROTATE, level.time );

		// play sound
		if ( ent->flags & FL_KICKACTIVATE ) {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundKickedEnd );
		} else if ( ent->flags & FL_SOFTACTIVATE ) {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundSoftendo );
		} else {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos2 );
		}

		// fire targets
		if ( !ent->activator ) {
			ent->activator = ent;
		}
		G_UseTargets( ent, ent->activator );

		if ( ent->flags & FL_TOGGLE ) {
			ent->think = ReturnToPos1Rotate;
			ent->nextthink = 0;
			return;
		}

		// return to pos1 after a delay
		ent->think = ReturnToPos1Rotate;
		ent->nextthink = level.time + ent->wait;

	} else if ( ent->moverState == MOVER_2TO1ROTATE )   {
		// reached pos1
		SetMoverState( ent, MOVER_POS1ROTATE, level.time );

		// to stop sound from being requested if not in pvs anoying bug
		{
			qboolean inPVS = qfalse;
			gentity_t *player;

			player = AICast_FindEntityForName( "player" );

			if ( player ) {
				inPVS = trap_InPVS( player->r.currentOrigin, ent->r.currentOrigin );
			}

			// play sound
			if ( inPVS ) {
				if ( ent->flags & FL_SOFTACTIVATE ) {
					G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundSoftendc );
				} else {
					G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos1 );
				}
			}
		}

		// clear the 'soft' flag
		ent->flags &= ~FL_SOFTACTIVATE; //----(SA)	added

		// close areaportals
		if ( ent->teammaster == ent || !ent->teammaster ) {
			trap_AdjustAreaPortalState( ent, qfalse );
		}
	} else {
		G_Error( "Reached_BinaryMover: bad moverState" );
	}

//	ent->flags &= ~(FL_KICKACTIVATE|FL_SOFTACTIVATE);	// (SA) it was not opened normally.  Clear this so it thinks it's closed normally
	ent->flags &= ~FL_KICKACTIVATE; // (SA) it was not opened normally.  Clear this so it thinks it's closed normally

}

/*
================
IsBinaryMoverBlocked
================
*/
qboolean IsBinaryMoverBlocked( gentity_t *ent, gentity_t *other, gentity_t *activator ) {

	vec3_t dir, angles;
	vec3_t pos;
	vec3_t vec;
	float dot;
	vec3_t forward;
	qboolean is_relay = qfalse;

	if ( Q_stricmp( ent->classname, "func_door_rotating" ) == 0 ) {
		if ( ent->spawnflags & 32 ) {
			return qfalse;
		}

		//----(SA)	only check for blockage by players
		if ( !activator ) {
			if ( Q_stricmp( other->classname, "target_relay" ) == 0 ) {
				is_relay = qtrue;
			} else {
				return qfalse;
			}
		}
		//----(SA)	end

		VectorAdd( ent->r.absmin, ent->r.absmax, pos );
		VectorScale( pos, 0.5, pos );

		VectorSubtract( pos, ent->s.origin, dir );
		vectoangles( dir, angles );

		if ( ent->rotate[YAW] ) {
			angles[YAW] += ent->angle;
		} else if ( ent->rotate[PITCH] ) {
			angles[PITCH] += ent->angle;
		} else if ( ent->rotate[ROLL] ) {
			angles[ROLL] += ent->angle;
		}

		AngleVectors( angles, forward, NULL, NULL );
		// VectorSubtract (other->r.currentOrigin, pos, vec);

		if ( is_relay ) {
			VectorSubtract( other->r.currentOrigin, pos, vec );
		} else {
			VectorSubtract( activator->r.currentOrigin, pos, vec );
		}

		VectorNormalize( vec );
		dot = DotProduct( vec, forward );

		if ( dot >= 0 ) {
			return qtrue;
		} else {
			return qfalse;
		}

	}

	return qfalse;

}

// JOSEPH 1-26-00
/*
================
Reached_TrinaryMover
================
*/
void Reached_TrinaryMover( gentity_t *ent ) {

	// stop the looping sound
	ent->s.loopSound = ent->soundLoop;

	if ( ent->moverState == MOVER_1TO2 ) {
		// reached pos2
		SetMoverState( ent, MOVER_POS2, level.time );

		// goto pos 3
		ent->think = GotoPos3;
		ent->nextthink = level.time + 1000; //FRAMETIME;

		// play sound
		G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos2 );
	} else if ( ent->moverState == MOVER_2TO1 ) {
		// reached pos1
		SetMoverState( ent, MOVER_POS1, level.time );

		// play sound
		G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos1 );

		// close areaportals
		if ( ent->teammaster == ent || !ent->teammaster ) {
			trap_AdjustAreaPortalState( ent, qfalse );
		}
	} else if ( ent->moverState == MOVER_2TO3 )   {
		// reached pos3
		SetMoverState( ent, MOVER_POS3, level.time );

		// play sound
		G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos3 );

		// return to pos2 after a delay
		if ( ent->wait != -1000 ) {
			ent->think = ReturnToPos2;
			ent->nextthink = level.time + ent->wait;
		}

		// fire targets
		if ( !ent->activator ) {
			ent->activator = ent;
		}
		G_UseTargets( ent, ent->activator );
	} else if ( ent->moverState == MOVER_3TO2 )   {
		// reached pos2
		SetMoverState( ent, MOVER_POS2, level.time );

		// return to pos1
		ent->think = ReturnToPos1;
		ent->nextthink = level.time + 1000; //FRAMETIME;

		// play sound
		G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos3 );
	} else {
		G_Error( "Reached_BinaryMover: bad moverState" );
	}
}
// END JOSEPH

// JOSEPH 1-26-00
/*
================
Use_TrinaryMover
================
*/
void Use_TrinaryMover( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
	int total;
	int partial;
	qboolean isblocked = qfalse;

	isblocked = IsBinaryMoverBlocked( ent, other, activator );

	if ( isblocked ) {
		MatchTeamReverseAngleOnSlaves( ent, MOVER_1TO2ROTATE, level.time + 50 );

		// starting sound
		G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );

		// looping sound
		ent->s.loopSound = ent->soundLoop;

		// open areaportal
		if ( ent->teammaster == ent || !ent->teammaster ) {
			trap_AdjustAreaPortalState( ent, qtrue );
		}
		return;
	}

	// only the master should be used
	if ( ent->flags & FL_TEAMSLAVE ) {
		Use_TrinaryMover( ent->teammaster, other, activator );
		return;
	}

	ent->activator = activator;

	if ( ent->moverState == MOVER_POS1 ) {

		// start moving 50 msec later, becase if this was player
		// triggered, level.time hasn't been advanced yet
		MatchTeam( ent, MOVER_1TO2, level.time + 50 );

		// starting sound
		G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );

		// looping sound
		ent->s.loopSound = ent->soundLoop;

		// open areaportal
		if ( ent->teammaster == ent || !ent->teammaster ) {
			trap_AdjustAreaPortalState( ent, qtrue );
		}
		return;
	}

	if ( ent->moverState == MOVER_POS2 ) {

		// start moving 50 msec later, becase if this was player
		// triggered, level.time hasn't been advanced yet
		MatchTeam( ent, MOVER_2TO3, level.time + 50 );

		// starting sound
		G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to3 );

		// looping sound
		ent->s.loopSound = ent->soundLoop;

		return;
	}

	// if all the way up, just delay before coming down
	if ( ent->moverState == MOVER_POS3 ) {
		if ( ent->wait != -1000 ) {
			ent->nextthink = level.time + ent->wait;
		}
		return;
	}

	// only partway down before reversing
	if ( ent->moverState == MOVER_2TO1 ) {
		total = ent->s.pos.trDuration;
		partial = level.time - ent->s.time;
		if ( partial > total ) {
			partial = total;
		}

		MatchTeam( ent, MOVER_1TO2, level.time - ( total - partial ) );

		G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
		return;
	}

	if ( ent->moverState == MOVER_3TO2 ) {
		total = ent->s.pos.trDuration;
		partial = level.time - ent->s.time;
		if ( partial > total ) {
			partial = total;
		}

		MatchTeam( ent, MOVER_2TO3, level.time - ( total - partial ) );

		G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to3 );
		return;
	}

	// only partway up before reversing
	if ( ent->moverState == MOVER_1TO2 ) {
		total = ent->s.pos.trDuration;
		partial = level.time - ent->s.time;
		if ( partial > total ) {
			partial = total;
		}

		MatchTeam( ent, MOVER_2TO1, level.time - ( total - partial ) );

		if ( ent->flags & FL_SOFTACTIVATE ) {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundSoftclose );
		} else {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
		}
		return;
	}

	if ( ent->moverState == MOVER_2TO3 ) {
		total = ent->s.pos.trDuration;
		partial = level.time - ent->s.time;
		if ( partial > total ) {
			partial = total;
		}

		MatchTeam( ent, MOVER_3TO2, level.time - ( total - partial ) );

		G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound3to2 );
		return;
	}
}
// END JOSEPH

/*
================
Use_BinaryMover
================
*/
void Use_BinaryMover( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
//	int		total;
//	int		partial;
	qboolean isblocked = qfalse;
	qboolean nosound = qfalse;

	if ( level.time <= 4000 ) { // hack.  don't play door sounds if in the first /four/ seconds of game (FIXME: TODO: THIS IS STILL A HACK)
		nosound = qtrue;
	}

	// only the master should be used
	if ( ent->flags & FL_TEAMSLAVE ) {

		// pass along flags for how door was activated
		if ( ent->flags & FL_KICKACTIVATE ) {
			ent->teammaster->flags |= FL_KICKACTIVATE;
		}
		if ( ent->flags & FL_SOFTACTIVATE ) {
			ent->teammaster->flags |= FL_SOFTACTIVATE;
		}

		Use_BinaryMover( ent->teammaster, other, activator );
		return;
	}

	// only check for blocking when opening, otherwise the door has no choice
	if ( ent->moverState == MOVER_POS1 || ent->moverState == MOVER_POS1ROTATE ) {
		isblocked = IsBinaryMoverBlocked( ent, other, activator );
	}


	if ( isblocked ) {
		// start moving 50 msec later, becase if this was player
		// triggered, level.time hasn't been advanced yet
		// ent->angle *= -1;
		// MatchTeam( ent, MOVER_1TO2ROTATE, level.time + 50 );
		MatchTeamReverseAngleOnSlaves( ent, MOVER_1TO2ROTATE, level.time + 50 );

		// starting sound
		if ( !nosound ) {
			if ( ent->flags & FL_KICKACTIVATE ) {  // kicked
				if ( activator ) {
					AICast_AudibleEvent( activator->s.number, ent->s.origin, HEAR_RANGE_DOOR_OPEN );    // "someone kicked open a door near me!"
				}
				G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundKicked );
			} else if ( ent->flags & FL_SOFTACTIVATE ) {
				G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundSoftopen );
			} else {
				if ( activator ) {
					AICast_AudibleEvent( activator->s.number, ent->s.origin, HEAR_RANGE_DOOR_KICKOPEN );    // "someone kicked open a door near me!"
				}
				G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
			}
		}

		ent->s.loopSound = 0;

		// looping sound
		if ( !nosound ) {
			ent->s.loopSound = ent->sound2to3;
		} else if ( !nosound ) {
			ent->s.loopSound = ent->soundLoop;
		}

		// open areaportal
		if ( ent->teammaster == ent || !ent->teammaster ) {
			trap_AdjustAreaPortalState( ent, qtrue );
		}
		return;
	}

	ent->activator = activator;

	// Rafael
	if ( ent->nextTrain && ent->nextTrain->wait == -1 && ent->nextTrain->count == 1 ) {
		ent->nextTrain->count = 0;
		return;
	}

	if ( ent->moverState == MOVER_POS1 ) {

		// start moving 50 msec later, becase if this was player
		// triggered, level.time hasn't been advanced yet
		MatchTeam( ent, MOVER_1TO2, level.time + 50 );

		// play starting sound
		if ( !nosound ) {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
		}

		ent->s.loopSound = 0;

		// set looping sound
		if ( !nosound ) {
			ent->s.loopSound = ent->sound2to3;
		}

		// open areaportal
		if ( ent->teammaster == ent || !ent->teammaster ) {
			trap_AdjustAreaPortalState( ent, qtrue );
		}
		return;
	}

	if ( ent->moverState == MOVER_POS1ROTATE ) {

		// start moving 50 msec later, becase if this was player
		// triggered, level.time hasn't been advanced yet
		MatchTeam( ent, MOVER_1TO2ROTATE, level.time + 50 );

		// play starting sound
		if ( !nosound ) {
			if ( ent->flags & FL_KICKACTIVATE ) {  // kicked
				if ( activator ) {
					AICast_AudibleEvent( activator->s.number, ent->s.origin, HEAR_RANGE_DOOR_KICKOPEN );    // "someone kicked open a door near me!"
				}
				G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundKicked );
			} else if ( ent->flags & FL_SOFTACTIVATE ) {
				G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundSoftopen );
			} else {
				if ( activator ) {
					AICast_AudibleEvent( activator->s.number, ent->s.origin, HEAR_RANGE_DOOR_OPEN );    // "someone opened a door near me!"
				}
				G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
			}
		}

		ent->s.loopSound = 0;
		// set looping sound
		if ( !nosound ) {
			ent->s.loopSound = ent->sound2to3;
		}

		// open areaportal
		if ( ent->teammaster == ent || !ent->teammaster ) {
			trap_AdjustAreaPortalState( ent, qtrue );
		}
		return;
	}

	// if all the way up, just delay before coming down
	// JOSEPH 1-27-00
	if ( ent->moverState == MOVER_POS2 ) {
		if ( ent->flags & FL_TOGGLE ) {
			ent->nextthink = level.time + 50;
			return;
		}

		if ( ent->wait != -1000 ) {
			ent->nextthink = level.time + ent->wait;
		}
		return;
	}
	// END JOSEPH

	// if all the way up, just delay before coming down
	if ( ent->moverState == MOVER_POS2ROTATE ) {
		if ( ent->flags & FL_TOGGLE ) {
			ent->nextthink = level.time + 50;   // do it *now* for toggles
		} else {
			ent->nextthink = level.time + ent->wait;
		}
		return;
	}

	// only partway down before reversing
	if ( ent->moverState == MOVER_2TO1 ) {
		Blocked_Door( ent, NULL );

		if ( !nosound ) {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
		}
		return;
	}

	// only partway up before reversing
	if ( ent->moverState == MOVER_1TO2 ) {
		Blocked_Door( ent, NULL );

		if ( !nosound ) {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
		}
		return;
	}

	// only partway closed before reversing
	if ( ent->moverState == MOVER_2TO1ROTATE ) {
		Blocked_DoorRotate( ent, NULL );

		if ( !nosound ) {
			G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
		}
		return;
	}

	// only partway open before reversing
	if ( ent->moverState == MOVER_1TO2ROTATE ) {
		Blocked_DoorRotate( ent, NULL );

		if ( !nosound ) {
			if ( ent->flags & FL_SOFTACTIVATE ) {
				G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundSoftclose );
			} else {
				G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
			}
		}
		return;
	}
}



/*
================
InitMover

"pos1", "pos2", and "speed" should be set before calling,
so the movement delta can be calculated
================
*/
void InitMover( gentity_t *ent ) {
	vec3_t move;
	float distance;
	float light;
	vec3_t color;
	qboolean lightSet, colorSet;
	char        *sound;

	// if the "model2" key is set, use a seperate model
	// for drawing, but clip against the brushes
	if ( ent->model2 ) {
		ent->s.modelindex2 = G_ModelIndex( ent->model2 );
	}

	// if the "loopsound" key is set, use a constant looping sound when moving
	if ( G_SpawnString( "noise", "100", &sound ) ) {
		ent->s.loopSound = G_SoundIndex( sound );
	}

	// if the "color" or "light" keys are set, setup constantLight
	lightSet = G_SpawnFloat( "light", "100", &light );
	colorSet = G_SpawnVector( "color", "1 1 1", color );
	if ( lightSet || colorSet ) {
		int r, g, b, i;

		r = color[0] * 255;
		if ( r > 255 ) {
			r = 255;
		}
		g = color[1] * 255;
		if ( g > 255 ) {
			g = 255;
		}
		b = color[2] * 255;
		if ( b > 255 ) {
			b = 255;
		}
		i = light / 4;
		if ( i > 255 ) {
			i = 255;
		}
		ent->s.constantLight = r | ( g << 8 ) | ( b << 16 ) | ( i << 24 );
	}

	// JOSEPH 1-26-00
	if ( !Q_stricmp( ent->classname,"func_secret" ) ) {
		ent->use = Use_TrinaryMover;
		ent->reached = Reached_TrinaryMover;
	} else if ( !Q_stricmp( ent->classname, "func_rotating" ) )       {
		ent->use = Use_Func_Rotate;
		ent->reached = 0; // rotating can never reach
	} else
	{
		ent->use = Use_BinaryMover;
		ent->reached = Reached_BinaryMover;
	}
	// END JOSEPH

	ent->moverState = MOVER_POS1;
	ent->r.svFlags = SVF_USE_CURRENT_ORIGIN;
	ent->s.eType = ET_MOVER;

	VectorCopy( ent->pos1, ent->r.currentOrigin );
	trap_LinkEntity( ent );

	ent->s.pos.trType = TR_STATIONARY;
	VectorCopy( ent->pos1, ent->s.pos.trBase );

	// calculate time to reach second position from speed
	VectorSubtract( ent->pos2, ent->pos1, move );
	distance = VectorLength( move );
	if ( !ent->speed ) {
		ent->speed = 100;
	}

//----(SA)	changes
	// open time based on speed
//	VectorScale( move, ent->speed, ent->s.pos.trDelta );
	VectorScale( move, ent->speed, ent->gDelta );
	ent->s.pos.trDuration = distance * 1000 / ent->speed;
	if ( ent->s.pos.trDuration <= 0 ) {
		ent->s.pos.trDuration = 1;
	}
	ent->gDurationBack = ent->gDuration = ent->s.pos.trDuration;

	// close time based on speed
	if ( ent->closespeed ) {
		VectorScale( move, ent->closespeed, ent->gDelta );
		ent->gDurationBack = distance * 1000 / ent->closespeed;
		if ( ent->gDurationBack <= 0 ) {
			ent->gDurationBack = 1;
//----(SA) end
		}
	}
}

/*
================
InitMoverRotate

"pos1", "pos2", and "speed" should be set before calling,
so the movement delta can be calculated
================
*/
void InitMoverRotate( gentity_t *ent ) {
	vec3_t move;
	float light;
	vec3_t color;
	qboolean lightSet, colorSet;

	// if the "model2" key is set, use a seperate model
	// for drawing, but clip against the brushes
	if ( ent->model2 ) {
		ent->s.modelindex2 = G_ModelIndex( ent->model2 );
	}

	// if the "color" or "light" keys are set, setup constantLight
	lightSet = G_SpawnFloat( "light", "100", &light );
	colorSet = G_SpawnVector( "color", "1 1 1", color );
	if ( lightSet || colorSet ) {
		int r, g, b, i;

		r = color[0] * 255;
		if ( r > 255 ) {
			r = 255;
		}
		g = color[1] * 255;
		if ( g > 255 ) {
			g = 255;
		}
		b = color[2] * 255;
		if ( b > 255 ) {
			b = 255;
		}
		i = light / 4;
		if ( i > 255 ) {
			i = 255;
		}
		ent->s.constantLight = r | ( g << 8 ) | ( b << 16 ) | ( i << 24 );
	}


	ent->use = Use_BinaryMover;

	if ( !( ent->spawnflags & 64 ) ) { // STAYOPEN
		ent->reached = Reached_BinaryMover;
	}

	ent->moverState = MOVER_POS1ROTATE;
	ent->r.svFlags = SVF_USE_CURRENT_ORIGIN;
	ent->s.eType = ET_MOVER;
	VectorCopy( ent->s.origin, ent->s.pos.trBase );
	VectorCopy( ent->pos1, ent->r.currentOrigin );
	trap_LinkEntity( ent );

	ent->s.pos.trType = TR_STATIONARY;
	VectorCopy( ent->pos1, ent->s.pos.trBase );

	// calculate time to reach second position from speed
	VectorSubtract( ent->pos2, ent->pos1, move );
	VectorLength( move );
	if ( !ent->speed ) {
		ent->speed = 100;
	}

	VectorScale( move, ent->speed, ent->s.pos.trDelta );

	ent->s.apos.trDuration = ent->speed;
	if ( ent->s.apos.trDuration <= 0 ) {
		ent->s.apos.trDuration = 1;
	}

	ent->gDuration = ent->gDurationBack = ent->s.apos.trDuration;   // (SA) store 'real' durations so doors can be opened/closed at different speeds
}


/*
===============================================================================

DOOR

A use can be triggered either by a touch function, by being shot, or by being
targeted by another entity.

===============================================================================
*/

//---- (SA) don't remember what the max keys was supposed to be, so change here and nuke this comment if you know
#define MAX_DOOR_KEYS 16

/*
================
Blocked_Door
================
*/
void Blocked_Door( gentity_t *ent, gentity_t *other ) {
	gentity_t *slave;
	int time;

	// remove anything other than a client
	if ( other ) {
		if ( !other->client && other->s.eType != ET_CORPSE ) {
			// except CTF flags!!!!
			if ( other->s.eType == ET_ITEM && other->item->giType == IT_TEAM ) {
				Team_DroppedFlagThink( other );
				return;
			}
			G_TempEntity( other->s.origin, EV_ITEM_POP );
			G_FreeEntity( other );
			return;
		}

		if ( ent->damage ) {
			G_Damage( other, ent, ent, NULL, NULL, ent->damage, 0, MOD_CRUSH );
		}
	}

	if ( ent->spawnflags & 4 ) {
		return;     // crushers don't reverse
	}

	// reverse direction
//	Use_BinaryMover( ent, ent, other );
	for ( slave = ent ; slave ; slave = slave->teamchain )
	{
//		time = level.time - slave->s.pos.trTime;
		time = level.time - ( slave->s.pos.trDuration - ( level.time - slave->s.pos.trTime ) );

		if ( slave->moverState == MOVER_1TO2 ) {
			SetMoverState( slave, MOVER_2TO1, time );
		} else {
			SetMoverState( slave, MOVER_1TO2, time );
		}
		trap_LinkEntity( slave );
	}

}

/*
================
Touch_DoorTriggerSpectator
================
*/
static void Touch_DoorTriggerSpectator( gentity_t *ent, gentity_t *other, trace_t *trace ) {
	int axis;
	float doorMin, doorMax;
	vec3_t origin;

	axis = ent->count;
	// the constants below relate to constants in Think_SpawnNewDoorTrigger()
	doorMin = ent->r.absmin[axis] + 100;
	doorMax = ent->r.absmax[axis] - 100;

	VectorCopy(other->client->ps.origin, origin);

	if (origin[axis] < doorMin || origin[axis] > doorMax) return;

	if (fabs(origin[axis] - doorMax) < fabs(origin[axis] - doorMin)) {
		origin[axis] = doorMin - 10;
	} else {
		origin[axis] = doorMax + 10;
	}

	TeleportPlayer(other, origin, tv(10000000.0, 0, 0));
}

/*
================
Blocked_DoorRotate
================
*/

#define DOORPUSHBACK    16

void Blocked_DoorRotate( gentity_t *ent, gentity_t *other ) {

	gentity_t       *slave;
	int time;

	// remove anything other than a client
	if ( other ) {
		if ( !other->client && other->s.eType != ET_CORPSE ) {
			if ( other->s.eType == ET_ITEM && other->item->giType == IT_TEAM ) {
				Team_DroppedFlagThink( other );
				return;
			}
			G_TempEntity( other->s.origin, EV_ITEM_POP );
			G_FreeEntity( other );
			return;
		}

		if ( other->health <= 0 ) {
			G_Damage( other, ent, ent, NULL, NULL, 99999, 0, MOD_CRUSH );
		}

		if ( ent->damage ) {
			G_Damage( other, ent, ent, NULL, NULL, ent->damage, 0, MOD_CRUSH );
		}
	}

	for ( slave = ent ; slave ; slave = slave->teamchain )
	{
		// RF, trying to fix "stuck in door" bug
		time = level.time - ( slave->s.apos.trDuration - ( level.time - slave->s.apos.trTime ) );
		//time = level.time - slave->s.apos.trTime;

		if ( slave->moverState == MOVER_1TO2ROTATE ) {
			SetMoverState( slave, MOVER_2TO1ROTATE, time );
		} else
		{
			SetMoverState( slave, MOVER_1TO2ROTATE, time );
		}
		trap_LinkEntity( slave );
	}


}


/*
================
Touch_DoorTrigger
================
*/
void Touch_DoorTrigger( gentity_t *ent, gentity_t *other, trace_t *trace ) {
	if ( other->client && other->client->sess.sessionTeam == TEAM_SPECTATOR ) {
		// if the door is not open and not opening
		if ( ent->parent->moverState != MOVER_1TO2 &&
			 ent->parent->moverState != MOVER_POS2 ) {
			Touch_DoorTriggerSpectator( ent, other, trace );
		}
	} else if ( ent->parent->moverState != MOVER_1TO2 )   {
		Use_BinaryMover( ent->parent, ent, other );
	}
}

/*
======================
Think_SpawnNewDoorTrigger

All of the parts of a door have been spawned, so create
a trigger that encloses all of them
======================
*/
void Think_SpawnNewDoorTrigger( gentity_t *ent ) {
	gentity_t       *other;
	vec3_t mins, maxs;
	int i, best;

	if (!ent) {
		return;
	}

	// set all of the slaves as shootable
	for ( other = ent ; other ; other = other->teamchain ) {
		other->takedamage = qtrue;
	}

	// find the bounds of everything on the team
	VectorCopy( ent->r.absmin, mins );
	VectorCopy( ent->r.absmax, maxs );

	for ( other = ent->teamchain ; other ; other = other->teamchain ) {
		AddPointToBounds( other->r.absmin, mins, maxs );
		AddPointToBounds( other->r.absmax, mins, maxs );
	}

	// find the thinnest axis, which will be the one we expand
	best = 0;
	for ( i = 1 ; i < 3 ; i++ ) {
		if ( maxs[i] - mins[i] < maxs[best] - mins[best] ) {
			best = i;
		}
	}
	maxs[best] += 120;
	mins[best] -= 120;

	// create a trigger with this size
	other = G_Spawn();
	VectorCopy( mins, other->r.mins );
	VectorCopy( maxs, other->r.maxs );
	other->parent = ent;
	other->r.contents = CONTENTS_TRIGGER;
	other->touch = Touch_DoorTrigger;
	trap_LinkEntity( other );

	MatchTeam( ent, ent->moverState, level.time );
}

void Think_MatchTeam( gentity_t *ent ) {
	MatchTeam( ent, ent->moverState, level.time );
}


//----(SA) added

/*
==============
findNonAIBrushTargeter
	determine if there is an entity pointing at ent that is not a "trigger_aidoor"
	(used now for checking which key to set for a door)
==============
*/
qboolean findNonAIBrushTargeter( gentity_t *ent ) {
	gentity_t *targeter = NULL;

	if ( !( ent->targetname ) ) {
		return qfalse;
	}

	while ( ( targeter = G_Find( targeter, FOFS( target ), ent->targetname ) ) != NULL )
	{
		if ( strcmp( targeter->classname,"trigger_aidoor" ) &&
			 Q_stricmp( targeter->classname, "func_invisible_user" ) ) {
			return qtrue;
		}
	}

	return qfalse;
}


/*
==============
finishSpawningKeyedMover
==============
*/
void finishSpawningKeyedMover( gentity_t *ent ) {
	gentity_t       *slave;

	// all ents should be spawned, so it's okay to check for special door triggers now

//----(SA)	modified
	if ( ent->key == -2 ) {    // the key was not set in the spawn
		if ( ent->targetname && findNonAIBrushTargeter( ent ) ) {
			ent->key = -1;  // something is targeting this (other than a trigger_aidoor) so leave locked
		} else {
			ent->key = 0;
		}
	}
//----(SA)	end

	if ( ent->key ) {
		G_SetAASBlockingEntity( ent, qtrue );
	}

	ent->nextthink = level.time + FRAMETIME;

	if ( !( ent->flags & FL_TEAMSLAVE ) ) {
		if ( ent->targetname || ent->takedamage ) {  // non touch/shoot doors
			ent->think = Think_MatchTeam;
		}
// (SA) is this safe?  is ent->spawnflags & 8 consistant among all keyed ents?
		else if ( ( ent->spawnflags & 8 ) && ( strcmp( ent->classname, "func_door_rotating" ) ) ) {
			ent->think = Think_SpawnNewDoorTrigger;
		} else {
			ent->think = Think_MatchTeam;
		}

		// (SA) slaves have been marked as FL_TEAMSLAVE now, so they won't
		// finish their think on their own.  So set keys for teamed doors
		for ( slave = ent ; slave ; slave = slave->teamchain )
		{
			if ( slave == ent ) {
				continue;
			}

			slave->key = ent->key;

			if ( slave->key ) {
				G_SetAASBlockingEntity( slave, qtrue );
			}
		}
	}
}

//----(SA) end



/*
==============
Door_reverse_sounds
	The door has been marked as "START_OPEN" which means the open/closed
	positions have been swapped.
	This swaps the sounds around as well
==============
*/
void Door_reverse_sounds( gentity_t *ent ) {
	int stemp;

	stemp = ent->sound1to2;
	ent->sound1to2 = ent->sound2to1;
	ent->sound2to1 = stemp;

	stemp = ent->soundPos1;
	ent->soundPos1 = ent->soundPos2;
	ent->soundPos2 = stemp;

	stemp = ent->sound2to3;
	ent->sound2to3 = ent->sound3to2;
	ent->sound3to2 = stemp;


	stemp = ent->soundSoftopen;
	ent->soundSoftopen = ent->soundSoftclose;
	ent->soundSoftclose = stemp;

	stemp = ent->soundSoftendo;
	ent->soundSoftendo = ent->soundSoftendc;
	ent->soundSoftendc = stemp;

}


/*
==============
DoorSetSounds
	get sound indexes for the various door sounds
	(used by SP_func_door() and SP_func_door_rotating() )
==============
*/
void DoorSetSounds( gentity_t *ent, int doortype, qboolean isRotating ) {
	ent->sound1to2 = G_SoundIndex( va( "sound/movers/doors/door%i_open.wav", doortype ) );       // opening
	ent->soundPos2 = G_SoundIndex( va( "sound/movers/doors/door%i_endo.wav", doortype ) );       // open
	ent->sound2to1 = G_SoundIndex( va( "sound/movers/doors/door%i_close.wav", doortype ) );      // closing
	ent->soundPos1 = G_SoundIndex( va( "sound/movers/doors/door%i_endc.wav", doortype ) );       // closed
	ent->sound2to3 = G_SoundIndex( va( "sound/movers/doors/door%i_loopo.wav", doortype ) );      // loopopen
	ent->sound3to2 = G_SoundIndex( va( "sound/movers/doors/door%i_loopc.wav", doortype ) );      // loopclosed
	ent->soundPos3 = G_SoundIndex( va( "sound/movers/doors/door%i_locked.wav", doortype ) ); // locked


	ent->soundSoftopen  = G_SoundIndex( va( "sound/movers/doors/door%i_openq.wav", doortype ) ); // opening quietly
	ent->soundSoftendo  = G_SoundIndex( va( "sound/movers/doors/door%i_endoq.wav", doortype ) ); // open quietly
	ent->soundSoftclose = G_SoundIndex( va( "sound/movers/doors/door%i_closeq.wav", doortype ) );    // closing quietly
	ent->soundSoftendc  = G_SoundIndex( va( "sound/movers/doors/door%i_endcq.wav", doortype ) ); // closed quietly

	if ( isRotating ) {
		ent->soundKicked    = G_SoundIndex( va( "sound/movers/doors/door%i_kicked.wav", doortype ) );
		ent->soundKickedEnd = G_SoundIndex( va( "sound/movers/doors/door%i_kickedend.wav", doortype ) );
	}

}

//----(SA)	added
/*
==============
G_TryDoor
	seemed better to have this isolated.  this way i can get func_invisible_user's using the
	regular rules of doors.
==============
*/
void G_TryDoor( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
	qboolean walking = qfalse;

	walking = (qboolean)( ent->flags & FL_SOFTACTIVATE );


	if ( ( ent->s.apos.trType == TR_STATIONARY && ent->s.pos.trType == TR_STATIONARY ) ) {
		if ( ent->active == qfalse ) {
			if ( ent->key < 0 ) {  // door force locked
				if ( !walking && activator ) { // only send audible event if not trying to open slowly
					AICast_AudibleEvent( activator->s.clientNum, ent->s.origin, HEAR_RANGE_DOOR_LOCKED );   // "someone tried locked door near me!"
				}
				G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos3 );
				return;
			}

			if ( activator ) {
				if ( ent->key > 0 ) {  // door requires key
					gitem_t *item = BG_FindItemForKey( ent->key, 0 );
					if ( !( activator->client->ps.stats[STAT_KEYS] & ( 1 << item->giTag ) ) ) {
						if ( !walking ) {  // only send audible event if not trying to open slowly
							AICast_AudibleEvent( activator->s.clientNum, ent->s.origin, HEAR_RANGE_DOOR_LOCKED );   // "someone tried locked door near me!"
						}
						// player does not have key
						G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos3 );
						return;
					}
				}
			}


			if ( ent->teammaster && ent->team && ent != ent->teammaster ) {
				ent->teammaster->active = qtrue;
				if ( walking ) {
					ent->teammaster->flags |= FL_SOFTACTIVATE;      // no noise generated
				} else {
					if ( activator ) {
						AICast_AudibleEvent( activator->s.clientNum, ent->s.origin, HEAR_RANGE_DOOR_OPEN ); // "someone opened door near me!"
					}
				}

				Use_BinaryMover( ent->teammaster, activator, activator );
				G_UseTargets( ent->teammaster, activator );
			} else
			{
				ent->active = qtrue;
				if ( walking ) {
					ent->flags |= FL_SOFTACTIVATE;      // no noise
				} else {
					if ( activator ) {
						AICast_AudibleEvent( activator->s.clientNum, ent->s.origin, HEAR_RANGE_DOOR_OPEN ); // "someone opened door near me!"
					}
				}

				Use_BinaryMover( ent, activator, activator );
				G_UseTargets( ent, activator );
			}
		}
	}
}
//----(SA)	end


/*QUAKED func_door (0 .5 .8) ? START_OPEN TOGGLE CRUSHER TOUCH SHOOT-THRU
TOGGLE      wait in both the start and end states for a trigger event.
START_OPEN  the door to moves to its destination when spawned, and operate in reverse.  It is used to temporarily or permanently close off an area when triggered (not useful for touch or takedamage doors).
NOMONSTER   monsters will not trigger this door
SHOOT-THRU	Bullets don't stop when they hit the door.  Set "shoot_thru_scale" with bullet damage scale (see below)

"key"       -1 for locked, key number for which key opens, 0 for open.  default '0' unless door is targeted.  (trigger_aidoor entities targeting this door do /not/ affect the key status)
"model2"    .md3 model to also draw
"angle"	    determines the opening direction
"targetname" if set, no touch field will be spawned and a remote button or trigger field activates the door.
"speed"	    movement speed (100 default)
"closespeed" optional different movement speed for door closing
"wait"      wait before returning (3 default, -1 = never return)
"lip"       lip remaining at end of move (8 default)
"dmg"       damage to inflict when blocked (2 default)
"color"     constantLight color
"light"     constantLight radius
"health"    if set, the door must be shot open
"team"		team name.  other doors with same team name will open/close in syncronicity
"shoot_thru_scale"	Multiplier for how much damage bullets do that have passed through the door.  Effectively how much damage the door 'absorbs'.  0.0 - 1.0 (0.0 will turn SHOOT-THRU off, 1.0 is full damage)
"type"		use sounds based on construction of door:
	 0 - nosound (default)
	 1 - metal
	 2 - stone
	 3 - lab
	 4 - wood
	 5 - iron/jail
	 6 - portcullis
	 7 - wood (quiet)

SOUND NAMING INFO -
inside "sound/movers/doors/door<number>...
	_open.wav		// opening
	_endo.wav		// open
	_close.wav		// closing
	_endc.wav		// closed
	_loopo.wav		// opening loop
	_loopc.wav		// closing loop
	_locked.wav		// locked

	_openq.wav		// opening quietly
	_endoq.wav		// open quietly
	_closeq.wav		// closing quietly
	_endcq.wav		// closed quietly

and for rotating doors:
	_kicked.wav
	_kickedend.wav

*/
void SP_func_door( gentity_t *ent ) {
	vec3_t abs_movedir;
	float distance;
	vec3_t size;
	float lip;
	int key, doortype;

	G_SpawnInt( "type", "0", &doortype );

	if ( doortype ) { // /*why on earthy did this check for <=8?*/ && doortype <= 8)	// no doortype = silent
		DoorSetSounds( ent, doortype, qfalse );
	}

	ent->blocked = Blocked_Door;

	// default speed of 400
	if ( !ent->speed ) {
		ent->speed = 400;
	}

	// default wait of 2 seconds
	if ( !ent->wait ) {
		ent->wait = 2;
	}
	ent->wait *= 1000;

	//---- (SA) door keys

	if ( G_SpawnInt( "key", "", &key ) ) { // if door has a key entered, set it
		ent->key = key;
	} else {
		ent->key = -2;                  // otherwise, set the key when this ent finishes spawning

	}
	// if the key is invalid, set the key in the finishSpawning routine
	if ( ent->key > MAX_DOOR_KEYS || ent->key < -2 ) {
		G_Error( "invalid key number: %d in func_door_rotating\n", ent->key );
		ent->key = -2;
	}
	//---- (SA) end

	// default lip of 8 units
	G_SpawnFloat( "lip", "8", &lip );

	// default damage of 2 points
	G_SpawnInt( "dmg", "2", &ent->damage );

	// first position at start
	VectorCopy( ent->s.origin, ent->pos1 );

	// calculate second position
	trap_SetBrushModel( ent, ent->model );
	G_SetMovedir( ent->s.angles, ent->movedir );
	abs_movedir[0] = fabs( ent->movedir[0] );
	abs_movedir[1] = fabs( ent->movedir[1] );
	abs_movedir[2] = fabs( ent->movedir[2] );
	VectorSubtract( ent->r.maxs, ent->r.mins, size );
	distance = DotProduct( abs_movedir, size ) - lip;
	VectorMA( ent->pos1, distance, ent->movedir, ent->pos2 );

	if ( ent->spawnflags & 1 ) {    // START_OPEN - reverse position 1 and 2
		vec3_t temp;
		int tempi;

		VectorCopy( ent->pos2, temp );
		VectorCopy( ent->s.origin, ent->pos2 );
		VectorCopy( temp, ent->pos1 );

		// swap speeds if door has 'closespeed'
		if ( ent->closespeed ) {
			tempi = ent->speed;
			ent->speed = ent->closespeed;
			ent->closespeed = tempi;
		}

		// swap sounds
		Door_reverse_sounds( ent );
	}

	// TOGGLE
	if ( ent->spawnflags & 2 ) {
		ent->flags |= FL_TOGGLE;
	}

	InitMover( ent );

	ent->s.dmgFlags = HINT_DOOR;    // make it a door for cursorhints

	if ( !( ent->flags & FL_TEAMSLAVE ) ) {
		int health;

		G_SpawnInt( "health", "0", &health );
		if ( health ) {
			ent->takedamage = qtrue;
		}
	}

	ent->nextthink = level.time + FRAMETIME;
	ent->think = finishSpawningKeyedMover;
}

// JOSEPH 1-26-00
/*QUAKED func_secret (0 .5 .8) ? REVERSE x CRUSHER TOUCH
TOGGLE      wait in both the start and end states for a trigger event.
START_OPEN  the door to moves to its destination when spawned, and operate in reverse.  It is used to temporarily or permanently close off an area when triggered (not useful for touch or takedamage doors).
NOMONSTER   monsters will not trigger this door

"key"       -1 for locked, key number for which key opens, 0 for open.  default '0' unless door is targeted.  (trigger_aidoor entities targeting this door do /not/ affect the key status)
"model2"    .md3 model to also draw
"angle"	    determines the opening direction
"targetname" if set, no touch field will be spawned and a remote button or trigger field activates the door.
"speed"	    movement speed (100 default)
"wait"      wait before returning (2 default, -1 = never return)
"lip"       lip remaining at end of move (8 default)
"dmg"       damage to inflict when blocked (2 default)
"color"     constantLight color
"light"     constantLight radius
"health"    if set, the door must be shot open
*/
void SP_func_secret( gentity_t *ent ) {
	vec3_t abs_movedir;
	vec3_t angles2;
	float distance;
	vec3_t size;
	float lip;
	int key;

	ent->sound1to2 = ent->sound2to1 = ent->sound2to3 = G_SoundIndex( "sound/movers/doors/dr1_strt.wav" );
	ent->soundPos1 = ent->soundPos3 = G_SoundIndex( "sound/movers/doors/dr1_end.wav" );

	ent->blocked = Blocked_Door;

	// default speed of 100
	if ( !ent->speed ) {
		ent->speed = 100;
	}

	// default wait of 2 seconds
	if ( !ent->wait ) {
		ent->wait = 2;
	}
	ent->wait *= 1000;

	//---- (SA) door keys

	if ( G_SpawnInt( "key", "", &key ) ) { // if door has a key entered, set it
		ent->key = key;
	} else {
		ent->key = -1;                  // otherwise, set the key when this ent finishes spawning

	}
	// if the key is invalid, set the key in the finishSpawning routine
	if ( ent->key > MAX_DOOR_KEYS || ent->key < -1 ) {
		G_Error( "invalid key number: %d in func_door_rotating\n", ent->key );
		ent->key = -1;
	}
	//---- (SA) end

	// default lip of 8 units
	G_SpawnFloat( "lip", "8", &lip );

	// default damage of 2 points
	G_SpawnInt( "dmg", "2", &ent->damage );

	// first position at start
	VectorCopy( ent->s.origin, ent->pos1 );

	VectorCopy( ent->s.angles, angles2 );

	if ( ent->spawnflags & 1 ) {
		angles2[1] -= 90;
	} else {
		angles2[1] += 90;
	}

	// calculate second position
	trap_SetBrushModel( ent, ent->model );
	G_SetMovedir( ent->s.angles, ent->movedir );
	abs_movedir[0] = fabs( ent->movedir[0] );
	abs_movedir[1] = fabs( ent->movedir[1] );
	abs_movedir[2] = fabs( ent->movedir[2] );
	VectorSubtract( ent->r.maxs, ent->r.mins, size );
	distance = DotProduct( abs_movedir, size ) - lip;
	VectorMA( ent->pos1, distance, ent->movedir, ent->pos2 );

	// calculate third position
	G_SetMovedir( angles2, ent->movedir );
	abs_movedir[0] = fabs( ent->movedir[0] );
	abs_movedir[1] = fabs( ent->movedir[1] );
	abs_movedir[2] = fabs( ent->movedir[2] );
	VectorSubtract( ent->r.maxs, ent->r.mins, size );
	distance = DotProduct( abs_movedir, size ) - lip;
	VectorMA( ent->pos2, distance, ent->movedir, ent->pos3 );

	// if "start_open", reverse position 1 and 3
	/*if ( ent->spawnflags & 1 ) {
		vec3_t	temp;

		VectorCopy( ent->pos3, temp );
		VectorCopy( ent->s.origin, ent->pos3 );
		VectorCopy( temp, ent->pos1 );
	}*/

	InitMover( ent );

	if ( !( ent->flags & FL_TEAMSLAVE ) ) {
		int health;

		G_SpawnInt( "health", "0", &health );
		if ( health ) {
			ent->takedamage = qtrue;
		}
	}

	ent->nextthink = level.time + FRAMETIME;
	ent->think = finishSpawningKeyedMover;
}
// END JOSEPH

/*
===============================================================================

PLAT

===============================================================================
*/

/*
==============
Touch_Plat

Don't allow decent if a living player is on it
===============
*/
void Touch_Plat( gentity_t *ent, gentity_t *other, trace_t *trace ) {
	if ( !other->client || other->client->ps.stats[STAT_HEALTH] <= 0 ) {
		return;
	}

	// delay return-to-pos1 by one second
	if ( ent->moverState == MOVER_POS2 ) {
		ent->nextthink = level.time + 1000;
	}
}

/*
==============
Touch_PlatCenterTrigger

If the plat is at the bottom position, start it going up
===============
*/
void Touch_PlatCenterTrigger( gentity_t *ent, gentity_t *other, trace_t *trace ) {
	if ( !other->client ) {
		return;
	}

	if ( ent->parent->moverState == MOVER_POS1 ) {
		Use_BinaryMover( ent->parent, ent, other );
	}
}


/*
================
SpawnPlatTrigger

Spawn a trigger in the middle of the plat's low position
Elevator cars require that the trigger extend through the entire low position,
not just sit on top of it.
================
*/
void SpawnPlatTrigger( gentity_t *ent ) {
	gentity_t   *trigger;
	vec3_t tmin, tmax;

	// the middle trigger will be a thin trigger just
	// above the starting position
	trigger = G_Spawn();
	trigger->touch = Touch_PlatCenterTrigger;
	trigger->r.contents = CONTENTS_TRIGGER;
	trigger->parent = ent;

	tmin[0] = ent->pos1[0] + ent->r.mins[0] + 33;
	tmin[1] = ent->pos1[1] + ent->r.mins[1] + 33;
	tmin[2] = ent->pos1[2] + ent->r.mins[2];

	tmax[0] = ent->pos1[0] + ent->r.maxs[0] - 33;
	tmax[1] = ent->pos1[1] + ent->r.maxs[1] - 33;
	tmax[2] = ent->pos1[2] + ent->r.maxs[2] + 8;

	if ( tmax[0] <= tmin[0] ) {
		tmin[0] = ent->pos1[0] + ( ent->r.mins[0] + ent->r.maxs[0] ) * 0.5;
		tmax[0] = tmin[0] + 1;
	}
	if ( tmax[1] <= tmin[1] ) {
		tmin[1] = ent->pos1[1] + ( ent->r.mins[1] + ent->r.maxs[1] ) * 0.5;
		tmax[1] = tmin[1] + 1;
	}

	VectorCopy( tmin, trigger->r.mins );
	VectorCopy( tmax, trigger->r.maxs );

	trap_LinkEntity( trigger );
}


/*QUAKED func_plat (0 .5 .8) ?
Plats are always drawn in the extended position so they will light correctly.

"lip"		default 8, protrusion above rest position
"height"	total height of movement, defaults to model height
"speed"		overrides default 200.
"dmg"		overrides default 2
"model2"	.md3 model to also draw
"color"		constantLight color
"light"		constantLight radius
*/
void SP_func_plat( gentity_t *ent ) {
	float lip, height;

	ent->sound1to2 = ent->sound2to1 = G_SoundIndex( "sound/movers/plats/pt1_strt.wav" );
	ent->soundPos1 = ent->soundPos2 = G_SoundIndex( "sound/movers/plats/pt1_end.wav" );

	VectorClear( ent->s.angles );

	G_SpawnFloat( "speed", "200", &ent->speed );
	G_SpawnInt( "dmg", "2", &ent->damage );
	G_SpawnFloat( "wait", "1", &ent->wait );
	G_SpawnFloat( "lip", "8", &lip );

	ent->wait = 1000;

	// create second position
	trap_SetBrushModel( ent, ent->model );

	if ( !G_SpawnFloat( "height", "0", &height ) ) {
		height = ( ent->r.maxs[2] - ent->r.mins[2] ) - lip;
	}

	// pos1 is the rest (bottom) position, pos2 is the top
	VectorCopy( ent->s.origin, ent->pos2 );
	VectorCopy( ent->pos2, ent->pos1 );
	ent->pos1[2] -= height;

	InitMover( ent );

	// touch function keeps the plat from returning while
	// a live player is standing on it
	ent->touch = Touch_Plat;

	ent->blocked = Blocked_Door;

	ent->parent = ent;  // so it can be treated as a door

	// spawn the trigger if one hasn't been custom made
	if ( !ent->targetname ) {
		SpawnPlatTrigger( ent );
	}
}


/*
===============================================================================

BUTTON

===============================================================================
*/

/*
==============
Touch_Button

===============
*/
void Touch_Button( gentity_t *ent, gentity_t *other, trace_t *trace ) {
	if ( !other->client ) {
		return;
	}

	if ( ent->moverState == MOVER_POS1 ) {
		Use_BinaryMover( ent, other, other );
	}
}


/*QUAKED func_button (0 .5 .8) ? x x x TOUCH x x STAYOPEN
When a button is touched, it moves some distance in the direction of its angle, triggers all of its targets, waits some time, then returns to its original position where it can be triggered again.

"model2"	.md3 model to also draw
"angle"		determines the opening direction
"target"	all entities with a matching targetname will be used
"speed"		override the default 40 speed
"wait"		override the default 1 second wait (-1 = never return)
"lip"		override the default 4 pixel lip remaining at end of move
"health"	if set, the button must be killed instead of touched
"color"		constantLight color
"light"		constantLight radius
*/
void SP_func_button( gentity_t *ent ) {
	vec3_t abs_movedir;
	float distance;
	vec3_t size;
	float lip;

	ent->sound1to2 = G_SoundIndex( "sound/movers/switches/butn2.wav" );

	if ( !ent->speed ) {
		ent->speed = 40;
	}

	if ( !ent->wait ) {
		ent->wait = 1;
	}
	ent->wait *= 1000;

	// first position
	VectorCopy( ent->s.origin, ent->pos1 );

	// calculate second position
	trap_SetBrushModel( ent, ent->model );

	G_SpawnFloat( "lip", "4", &lip );

	G_SetMovedir( ent->s.angles, ent->movedir );
	abs_movedir[0] = fabs( ent->movedir[0] );
	abs_movedir[1] = fabs( ent->movedir[1] );
	abs_movedir[2] = fabs( ent->movedir[2] );
	VectorSubtract( ent->r.maxs, ent->r.mins, size );
	distance = abs_movedir[0] * size[0] + abs_movedir[1] * size[1] + abs_movedir[2] * size[2] - lip;
	VectorMA( ent->pos1, distance, ent->movedir, ent->pos2 );

	if ( ent->health ) {
		// shootable button
		ent->takedamage = qtrue;
	} else if ( ent->spawnflags & 8 ) {
		// touchable button
		ent->touch = Touch_Button;
	}

	InitMover( ent );
}



/*
===============================================================================

TRAIN

===============================================================================
*/


#define TRAIN_START_ON      1
#define TRAIN_TOGGLE        2
#define TRAIN_BLOCK_STOPS   4

/*
===============
Think_BeginMoving

The wait time at a corner has completed, so start moving again
===============
*/
void Think_BeginMoving( gentity_t *ent ) {
	ent->s.pos.trTime = level.time;
	ent->s.pos.trType = TR_LINEAR_STOP;
}

/*
===============
Reached_Train
===============
*/
void Reached_Train( gentity_t *ent ) {
	gentity_t       *next;
	float speed;
	vec3_t move;
	float length;

	// copy the apropriate values
	next = ent->nextTrain;
	if ( !next || !next->nextTrain ) {
		return;     // just stop
	}

	// Rafael
	if ( next->wait == -1 && next->count ) {
		return;
	}

	// fire all other targets
	G_UseTargets( next, NULL );

	// set the new trajectory
	ent->nextTrain = next->nextTrain;

	if ( next->wait == -1 ) {
		next->count = 1;
	}

	VectorCopy( next->s.origin, ent->pos1 );
	VectorCopy( next->nextTrain->s.origin, ent->pos2 );

	// if the path_corner has a speed, use that
	if ( next->speed ) {
		speed = next->speed;
	} else {
		// otherwise use the train's speed
		speed = ent->speed;
	}
	if ( speed < 1 ) {
		speed = 1;
	}

	if ( !strcmp( ent->classname, "func_bats" ) && next->radius ) {
		ent->radius = next->radius;
	}

	// calculate duration
	VectorSubtract( ent->pos2, ent->pos1, move );
	length = VectorLength( move );

	ent->s.pos.trDuration = length * 1000 / speed;
	ent->gDuration = ent->s.pos.trDuration;

	// Tequila comment: Be sure to send to clients after any fast move case
	ent->r.svFlags &= ~SVF_NOCLIENT;

	// Tequila comment: Fast move case
	if(ent->s.pos.trDuration<1) {
		// Tequila comment: As trDuration is used later in a division, we need to avoid that case now
		// With null trDuration,
		// the calculated rocks bounding box becomes infinite and the engine think for a short time
		// any entity is riding that mover but not the world entity... In rare case, I found it
		// can also stuck every map entities after func_door are used.
		// The desired effect with very very big speed is to have instant move, so any not null duration
		// lower than a frame duration should be sufficient.
		// Afaik, the negative case don't have to be supported.
		ent->s.pos.trDuration=1;

		// Tequila comment: Don't send entity to clients so it becomes really invisible 
		ent->r.svFlags |= SVF_NOCLIENT;
	}

	// looping sound
	ent->s.loopSound = next->soundLoop;

	// start it going
	SetMoverState( ent, MOVER_1TO2, level.time );

	// if there is a "wait" value on the target, don't start moving yet
	if ( next->wait ) {
		ent->nextthink = level.time + next->wait * 1000;
		ent->think = Think_BeginMoving;
		ent->s.pos.trType = TR_STATIONARY;
	}
}


/*
===============
Think_SetupTrainTargets

Link all the corners together
===============
*/
void Think_SetupTrainTargets( gentity_t *ent ) {
	gentity_t       *path, *next, *start;

	ent->nextTrain = G_Find( NULL, FOFS( targetname ), ent->target );
	if ( !ent->nextTrain ) {
		G_Printf( "func_train at %s with an unfound target\n",
				  vtos( ent->r.absmin ) );
		return;
	}

	start = NULL;

	if ( ent->s.eType == ET_BAT ) {
		for ( path = ent->nextTrain ; path != start ; path = next ) {

			if ( !start ) {
				start = path;
			}

			if ( !path->target ) {
				G_Printf( "Train corner at %s without a target\n",
						  vtos( path->s.origin ) );
				return;
			}

			// find a path_corner among the targets
			// there may also be other targets that get fired when the corner
			// is reached
			next = NULL;
			do {
				next = G_Find( next, FOFS( targetname ), path->target );
				if ( !next ) {
					G_Printf( "Train corner at %s without a target path_corner\n",
							  vtos( path->s.origin ) );
					return;
				}
			} while ( strcmp( next->classname, "path_corner" ) );

			path->nextTrain = next;
		}
	} else
	{
		for ( path = ent->nextTrain ; !path->nextTrain ; path = next ) {

			if ( !start ) {
				start = path;
			}

			if ( !path->target ) {
				G_Printf( "Train corner at %s without a target\n",
						  vtos( path->s.origin ) );
				return;
			}

			// find a path_corner among the targets
			// there may also be other targets that get fired when the corner
			// is reached
			next = NULL;
			do {
				next = G_Find( next, FOFS( targetname ), path->target );
				if ( !next ) {
					G_Printf( "Train corner at %s without a target path_corner\n",
							  vtos( path->s.origin ) );
					return;
				}
			} while ( strcmp( next->classname, "path_corner" ) );

			path->nextTrain = next;
		}
	}

	if ( !Q_stricmp( ent->classname, "func_train" ) && ent->spawnflags & 2 ) { // TOGGLE
		VectorCopy( ent->nextTrain->s.origin, ent->s.pos.trBase );
		VectorCopy( ent->nextTrain->s.origin, ent->r.currentOrigin );
		trap_LinkEntity( ent );
	} else if ( !Q_stricmp( ent->classname, "func_train_particles" ) && ent->spawnflags & 2 )       { // TOGGLE
		VectorCopy( ent->nextTrain->s.origin, ent->s.pos.trBase );
		VectorCopy( ent->nextTrain->s.origin, ent->r.currentOrigin );
		trap_LinkEntity( ent );
	} else if ( !Q_stricmp( ent->classname, "func_tramcar" ) && ent->spawnflags & 2 )       { // TOGGLE
		VectorCopy( ent->nextTrain->s.origin, ent->s.pos.trBase );
		VectorCopy( ent->nextTrain->s.origin, ent->r.currentOrigin );
		trap_LinkEntity( ent );
	} else if ( !Q_stricmp( ent->classname, "func_bat" ) )       {
		//VectorCopy (ent->nextTrain->s.origin, ent->s.pos.trBase);
		//VectorCopy (ent->nextTrain->s.origin, ent->r.currentOrigin);
		//trap_LinkEntity (ent);
		if ( ent->spawnflags & 1 ) {  // start on
			ent->use( ent, ent, ent );
		}
	} else if ( !Q_stricmp( ent->classname, "truck_cam" ) && ent->spawnflags & 2 )     { // TOGGLE
		VectorCopy( ent->nextTrain->s.origin, ent->s.pos.trBase );
		VectorCopy( ent->nextTrain->s.origin, ent->r.currentOrigin );
		trap_LinkEntity( ent );
	} else
	{
		if ( !Q_stricmp( ent->classname, "func_tramcar" ) ) {
			Reached_Tramcar( ent );
		} else if ( !Q_stricmp( ent->classname, "truck_cam" ) ) {
			Reached_Tramcar( ent );
		} else if ( !Q_stricmp( ent->classname, "camera_cam" ) ) {
			Reached_Tramcar( ent );
		} else {
			Reached_Train( ent );
		}
	}
}



/*QUAKED path_corner (.5 .3 0) (-8 -8 -8) (8 8 8) STOP END REVERSE
Train path corners.
Target: next path corner and other targets to fire
"speed" speed to move to the next corner
"wait" seconds to wait before behining move to next corner

"count2" used only in conjunction with the truck_cam to control playing of gear changes
*/
void SP_path_corner( gentity_t *self ) {
	if ( !self->targetname ) {
		G_Printf( "path_corner with no targetname at %s\n", vtos( self->s.origin ) );
		G_FreeEntity( self );
		return;
	}
	// path corners don't need to be linked in

	if ( self->wait == -1 ) {
		self->count = 1;
	}
}



/*QUAKED func_train (0 .5 .8) ? START_ON TOGGLE BLOCK_STOPS
A train is a mover that moves between path_corner target points.
Trains MUST HAVE AN ORIGIN BRUSH.
The train spawns at the first target it is pointing at.
"model2"	.md3 model to also draw
"speed"		default 100
"dmg"		default	2
"noise"		looping sound to play when the train is in motion
"target"	next path corner
"color"		constantLight color
"light"		constantLight radius
*/
void SP_func_train( gentity_t *self ) {
	VectorClear( self->s.angles );

	if ( self->spawnflags & TRAIN_BLOCK_STOPS ) {
		self->damage = 0;
		self->s.eFlags |= EF_MOVER_STOP;
	} else {
		if ( !self->damage ) {
			self->damage = 2;
		}
	}

	if ( !self->speed ) {
		self->speed = 100;
	}

	if ( !self->target ) {
		G_Printf( "func_train without a target at %s\n", vtos( self->r.absmin ) );
		G_FreeEntity( self );
		return;
	}

	trap_SetBrushModel( self, self->model );
	InitMover( self );

	self->reached = Reached_Train;

	// start trains on the second frame, to make sure their targets have had
	// a chance to spawn
	self->nextthink = level.time + FRAMETIME;
	self->think = Think_SetupTrainTargets;

	self->blocked = Blocked_Door;

}

// Rafael - bats
/*QUAKED func_train_particles ( 0.3 0.1 0.8) ? START_ON TOGGLE
health = default 16 bats
*/
void Func_train_particles_reached( gentity_t *self ) {
	gentity_t *tent;
	vec3_t vec, ang;
	vec3_t forward;

	Reached_Train( self );

	if ( self->nextTrain->wait == -1 && self->nextTrain->count ) {
		return;
	}

	if ( !self->count ) {
		tent = G_TempEntity( self->r.currentOrigin, EV_BATS );
		tent->s.time = self->speed;
		tent->s.density = self->health;
		VectorCopy( self->r.currentOrigin, tent->s.origin );
		VectorSubtract( self->nextTrain->s.origin, self->r.currentOrigin, vec );
		vectoangles( vec, ang );
		AngleVectors( ang, forward, NULL, NULL );
		VectorCopy( forward, tent->s.angles );
		self->count = 1;
	} else
	{
		tent = G_TempEntity( self->r.currentOrigin, EV_BATS_UPDATEPOSITION );
		tent->s.time = self->speed;
		VectorCopy( self->r.currentOrigin, tent->s.origin );
		VectorSubtract( self->nextTrain->s.origin, self->r.currentOrigin, vec );
		vectoangles( vec, ang );
		AngleVectors( ang, forward, NULL, NULL );
		VectorCopy( forward, tent->s.angles );
	}

	tent->s.frame = self->s.number;
	trap_LinkEntity( self );

}

void SP_func_train_particles( gentity_t *self ) {
	SP_func_train( self );
	self->reached = Func_train_particles_reached;
	self->blocked = 0;

	self->damage = 0;

	if ( !self->health ) {
		self->health = 16;
	}

	if ( !self->speed ) {
		self->speed = 50;
	}
}

// RF, bats v2.0
/*QUAKED func_bats ( 0.3 0.1 0.8) (-32 -32 -32) (32 32 32) START_ON TOGGLE
count = default 10 bats
radius = maximum distance from center of entity to place each bat (default=32)
speed = speed to travel to next waypoint (default=300)
*/
void FuncBatsReached( gentity_t *self ) {
	if ( !self->active ) {
		self->nextthink = -1;
		self->think = 0;
		return;
	}

	Reached_Train( self );

	// disable this to debug path
	self->r.svFlags |= SVF_NOCLIENT;
	self->r.contents = 0;

	if ( !self->nextTrain || !self->nextTrain->target ) {
		self->active = qfalse;   // remove the bats at next point
		return;
	}
}

// each bat calls this every server frame, so it moves towards it's ideal position
void BatMoveThink( gentity_t *bat ) {
	gentity_t *owner;
	vec3_t goalpos, vec;
	float speed, dist;
	int i;
	trace_t tr;

	owner = &g_entities[bat->r.ownerNum];
	if ( owner->active == qtrue && owner->inuse ) { // move towards the owner
		BG_EvaluateTrajectory( &owner->s.pos, level.time, goalpos );

		// randomize ther movedir as we go
		for ( i = 0; i < 3; i++ )
			bat->movedir[i] += crandom() * (float)owner->radius * 0.1;
		if ( VectorLength( bat->movedir ) > (float)owner->radius ) {
			VectorNormalize( bat->movedir );
			VectorScale( bat->movedir, (float)owner->radius, bat->movedir );
		}
		VectorAdd( goalpos, bat->movedir, goalpos );

		VectorSubtract( goalpos, bat->s.pos.trBase, vec );
		dist = VectorLength( vec );
		speed = dist / 48;
		VectorMA( bat->s.pos.trBase, 0.05 * speed, vec, bat->s.pos.trBase );
		bat->s.pos.trTime = level.time;
		VectorCopy( bat->s.pos.trBase, bat->r.currentOrigin );
		trap_LinkEntity( bat );

		// check for hurting someone
		if ( bat->damage < level.time ) {
			trap_Trace( &tr, bat->r.currentOrigin, NULL, NULL, bat->r.currentOrigin, bat->s.number, CONTENTS_BODY );
			if ( tr.startsolid && tr.entityNum < MAX_CLIENTS && !g_entities[tr.entityNum].aiCharacter ) {
				G_Damage( &g_entities[tr.entityNum], bat, bat, vec3_origin, bat->r.currentOrigin, 1 + rand() % 3, DAMAGE_NO_KNOCKBACK, MOD_BAT );

				// !! TODO: bat biting sound and view feedback

				// don't keep hurting them each time we think
				bat->damage = level.time + 1000;
			}
		}

	} else if ( owner->active || !owner->inuse ) {
		// owner has finished
		G_FreeEntity( bat );
		return;
	}
	bat->nextthink = level.time + 50;
}

void BatDie( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int meansOfDeath ) {
	G_AddEvent( self, EV_BATS_DEATH, 0 );
	self->think = G_FreeEntity;
	self->nextthink = level.time + 100;
}

void FuncBatsActivate( gentity_t *self, gentity_t * other, gentity_t * activator ) {
	int i;
	gentity_t *bat;
	vec3_t vec;

	if ( !self->active ) {
		self->active = qtrue;

		// spawn "count" bats
		for ( i = 0; i < self->count; i++ ) {
			bat = G_Spawn();
			bat->classname = "func_bat";
			bat->s.eType = ET_BAT;

			VectorSet( vec, crandom(), crandom(), crandom() );
			VectorNormalize( vec );
			VectorScale( vec, random() * (float)self->radius, bat->movedir );
			VectorAdd( self->s.pos.trBase, bat->movedir, bat->s.pos.trBase );
			bat->s.pos.trTime = level.time;
			VectorClear( bat->s.pos.trDelta );
			VectorCopy( bat->s.pos.trBase, bat->r.currentOrigin );

			bat->r.ownerNum = self->s.number;
			bat->r.contents = CONTENTS_CORPSE;
			bat->takedamage = qtrue;
			bat->health = 1;
			bat->pain = 0;
			bat->die = BatDie;
			VectorSet( bat->r.mins, -18, -18, -18 );
			VectorSet( bat->r.maxs,  18,  18,  18 );

			bat->speed = self->speed;
			bat->radius = self->radius;

			bat->think = BatMoveThink;
			bat->nextthink = level.time + 50;

			trap_LinkEntity( bat );
		}

		InitMover( self );  // start moving
		FuncBatsReached( self );
		self->reached = FuncBatsReached;
		self->blocked = 0;

		self->use = FuncBatsActivate;   // make sure this stays the same

	} else {    // second use kills bats
		self->active = 2;
	}
}

void SP_func_bats( gentity_t *self ) {
	if ( !self->count ) {
		self->count = 10;
	}

	if ( !self->radius ) {
		self->radius = 32;
	}

	if ( !self->speed ) {
		self->speed = 300;
	}

	// setup train waypoints
	self->active = qfalse;
	self->use = FuncBatsActivate;

	self->damage = 0;

	self->nextthink = level.time + FRAMETIME;
	self->think = Think_SetupTrainTargets;

	// disable this to debug path
	self->r.svFlags |= SVF_NOCLIENT;
	self->r.contents = 0;

	// TODO: make us shootable, but not "solid", so that if the player hits the group, at least one bat is killed
}

// JOSEPH 9-27-99
/*
===============
Think_BeginMoving_rotating

The wait time at a corner has completed, so start moving again
===============
*/
void Think_BeginMoving_rotating( gentity_t *ent ) {
	ent->s.pos.trTime = level.time;
	ent->s.pos.trType = TR_LINEAR_STOP;
}

/*
===============
Reached_Train_rotating
===============
*/
void Reached_Train_rotating( gentity_t *ent ) {
	gentity_t       *next;
	float speed;
	vec3_t move;
	float length;
	float frames;

	// copy the apropriate values
	next = ent->nextTrain;
	if ( !next || !next->nextTrain ) {
		return;     // just stop
	}

	// fire all other targets
	G_UseTargets( next, NULL );

	// set the new trajectory
	ent->nextTrain = next->nextTrain;
	VectorCopy( next->s.origin, ent->pos1 );
	VectorCopy( next->nextTrain->s.origin, ent->pos2 );

	// if the path_corner has a speed, use that
	if ( next->speed ) {
		speed = next->speed;
	} else {
		// otherwise use the train's speed
		speed = ent->speed;
	}
	if ( speed < 1 ) {
		speed = 1;
	}

	ent->rotate[0] = next->rotate[2];
	ent->rotate[1] = next->rotate[0];
	ent->rotate[2] = next->rotate[1];

	// calculate duration
	VectorSubtract( ent->pos2, ent->pos1, move );
	length = VectorLength( move );

	if ( next->duration ) {
		ent->s.pos.trDuration = ( next->duration * 1000 );
	} else {
		ent->s.pos.trDuration = length * 1000 / speed;
	}

	// Rotate the train
	frames = floor( ent->s.pos.trDuration / 100 );

	if ( !frames ) {
		frames = 0.001;
	}

	ent->s.apos.trType = TR_LINEAR;

	if ( ent->TargetFlag ) {
		VectorCopy( ent->TargetAngles, ent->r.currentAngles );
		VectorCopy( ent->r.currentAngles, ent->s.angles );
		VectorCopy( ent->s.angles, ent->s.apos.trBase );
		ent->TargetFlag = 0;
	}

	//G_Printf( "Train angles %s\n",
	//			vtos(ent->s.angles) );

	//G_Printf( "Add  X  Y  X %s\n",
	//			vtos(ent->rotate) );

	// X
	if ( ent->rotate[2] ) {
		ent->s.apos.trDelta[2] = ( ent->rotate[2] / frames ) * 10;
	} else {
		ent->s.apos.trDelta[2] = 0;
	}
	// Y
	if ( ent->rotate[0] ) {
		ent->s.apos.trDelta[0] = ( ent->rotate[0] / frames ) * 10;
	} else {
		ent->s.apos.trDelta[0] = 0;
	}
	// Z
	if ( ent->rotate[1] ) {
		ent->s.apos.trDelta[1] = ( ent->rotate[1] / frames ) * 10;
	} else {
		ent->s.apos.trDelta[1] = 0;
	}

	// looping sound
	ent->s.loopSound = next->soundLoop;

	ent->TargetFlag = 1;
	ent->TargetAngles[0] = ent->r.currentAngles[0] + ent->rotate[0];
	//ent->TargetAngles[0] = AngleNormalize360 (ent->TargetAngles[0]);
	ent->TargetAngles[1] = ent->r.currentAngles[1] + ent->rotate[1];
	//ent->TargetAngles[1] = AngleNormalize360 (ent->TargetAngles[1]);
	ent->TargetAngles[2] = ent->r.currentAngles[2] + ent->rotate[2];
	//ent->TargetAngles[2] = AngleNormalize360 (ent->TargetAngles[2]);

	// start it going
	SetMoverState( ent, MOVER_1TO2, level.time );

	// if there is a "wait" value on the target, don't start moving yet
	if ( next->wait ) {
		ent->nextthink = level.time + next->wait * 1000;
		ent->think = Think_BeginMoving_rotating;
		ent->s.pos.trType = TR_STATIONARY;
	}
}

/*
===============
Think_SetupTrainTargets_rotating

Link all the corners together
===============
*/
void Think_SetupTrainTargets_rotating( gentity_t *ent ) {
	gentity_t       *path, *next, *start;


	ent->nextTrain = G_Find( NULL, FOFS( targetname ), ent->target );
	if ( !ent->nextTrain ) {
		G_Printf( "func_train at %s with an unfound target\n",
				  vtos( ent->r.absmin ) );
		return;
	}

	VectorCopy( ent->s.angles, ent->s.apos.trBase );
	VectorCopy( ent->s.angles, ent->TargetAngles );
	ent->TargetFlag = 1;

	start = NULL;
	for ( path = ent->nextTrain ; path != start ; path = next ) {
		if ( !start ) {
			start = path;
		}

		if ( !path->target ) {
			G_Printf( "Train corner at %s without a target\n",
					  vtos( path->s.origin ) );
			return;
		}

		// find a path_corner among the targets
		// there may also be other targets that get fired when the corner
		// is reached
		next = NULL;
		do {
			next = G_Find( next, FOFS( targetname ), path->target );
			if ( !next ) {
				G_Printf( "Train corner at %s without a target path_corner\n",
						  vtos( path->s.origin ) );
				return;
			}
		} while ( strcmp( next->classname, "path_corner" ) );

		path->nextTrain = next;
	}

	// start the train moving from the first corner
	Reached_Train_rotating( ent );
}

/*QUAKED func_train_rotating (0 .5 .8) ? START_ON TOGGLE BLOCK_STOPS
A train is a mover that moves between path_corner target points.
This train can also rotate along the X Y Z
Trains MUST HAVE AN ORIGIN BRUSH.
The train spawns at the first target it is pointing at.

"model2"	.md3 model to also draw
"dmg"		default	2
"speed"		default 100
"noise"		looping sound to play when the train is in motion
"target"	next path corner
"color"		constantLight color
"light"		constantLight radius

On the path corner:
speed    departure speed from that corner
rotate   angle change for X Y Z to next corner
duration duration for angle change (overrides speed)
*/

void SP_func_train_rotating( gentity_t *self ) {
	VectorClear( self->s.angles );

	if ( self->spawnflags & TRAIN_BLOCK_STOPS ) {
		self->damage = 0;
	} else {
		if ( !self->damage ) {
			self->damage = 2;
		}
	}

	if ( !self->speed ) {
		self->speed = 100;
	}

	if ( !self->target ) {
		G_Printf( "func_train without a target at %s\n", vtos( self->r.absmin ) );
		G_FreeEntity( self );
		return;
	}

	trap_SetBrushModel( self, self->model );
	InitMover( self );

	self->reached = Reached_Train_rotating;

	// start trains on the second frame, to make sure their targets have had
	// a chance to spawn
	self->nextthink = level.time + FRAMETIME;
	self->think = Think_SetupTrainTargets_rotating;
}
// END JOSEPH

/*
===============================================================================

STATIC

===============================================================================
*/

/*
==============
Use_Static
	toggle hide or show (including collisions) this entity
==============
*/
void Use_Static( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
	if ( ent->r.linked ) {
		trap_UnlinkEntity( ent );
		// DISABLED since func_static will carve up AAS anyway, so blocking makes no sense
		// RF, AAS areas are now free
		//if (ent->model)
		//	G_SetAASBlockingEntity( ent, qfalse );
	} else {
		trap_LinkEntity( ent );
		// DISABLED since func_static will carve up AAS anyway, so blocking makes no sense
		// RF, AAS areas are now occupied
		//if (ent->model)
		//	G_SetAASBlockingEntity( ent, qtrue );
	}
}

void Static_Pain( gentity_t *ent, gentity_t *attacker, int damage, vec3_t point ) {
	vec3_t temp;

	if ( ent->spawnflags & 4 ) {
		if ( level.time > ent->wait + ent->delay + rand() % 1000 + 500 ) {
			ent->wait = level.time;
		} else {
			return;
		}

		// TBD only venom mg42 rocket and grenade can inflict damage
		if ( attacker && attacker->client
			 && ( attacker->s.weapon == WP_VENOM
				  || attacker->s.weapon == WP_VENOM_FULL
				  || attacker->s.weapon == WP_GRENADE_LAUNCHER
				  || attacker->s.weapon == WP_ROCKET_LAUNCHER
				  || attacker->client->ps.persistant[PERS_HWEAPON_USE] ) ) {

			VectorCopy( ent->r.currentOrigin, temp );
			VectorCopy( ent->pos3, ent->r.currentOrigin );
			Spawn_Shard( ent, attacker, 3, ent->count );
			VectorCopy( temp, ent->r.currentOrigin );
		}
		return;
	}

	if ( level.time > ent->wait + ent->delay + rand() % 1000 + 500 ) {
		G_UseTargets( ent, NULL );
		ent->wait = level.time;
	}

}

void G_BlockThink( gentity_t *ent ) {
	if ( ent->r.linked ) {
		G_SetAASBlockingEntity( ent, qtrue );
	} else {
		G_SetAASBlockingEntity( ent, qfalse );
	}
}


/*QUAKED func_leaky (0 .5 .8) ?
"type" - leaks particles of this type

1:oil
2:water
3:steam


*/

void SP_func_leaky( gentity_t *ent ) {
	if ( ent->model2 ) {
		ent->s.modelindex2 = G_ModelIndex( ent->model2 );
	}
	trap_SetBrushModel( ent, ent->model );
	trap_LinkEntity( ent );
	ent->s.pos.trType = TR_STATIONARY;
	VectorCopy( ent->s.origin, ent->s.pos.trBase );
	VectorCopy( ent->s.origin, ent->r.currentOrigin );
}


/*QUAKED func_static (0 .5 .8) ? start_invis pain painEFX
A bmodel that just sits there, doing nothing.  Can be used for conditional walls and models.
"model2"	.md3 model to also draw
"color"		constantLight color
"light"		constantLight radius
"start_invis" will start the entity as non-existant
If targeted, it will toggle existance when triggered

pain will use its target

When using pain you will need to specify the delay time
value of 1 = 1 sec 2 = 2 sec so on...
default is 1 sec you can use decimals
example :
delay
1.27

painEFX will spawn a shards
example:
shard
4
will spawn rubble

shard default is 4

shard =
shard_glass = 0,
shard_wood = 1,
shard_metal = 2,
shard_ceramic = 3,
shard_pebbles = 4
*/
void SP_func_static( gentity_t *ent ) {
	if ( ent->model2 ) {
		ent->s.modelindex2 = G_ModelIndex( ent->model2 );
	}
	trap_SetBrushModel( ent, ent->model );
	InitMover( ent );
	VectorCopy( ent->s.origin, ent->s.pos.trBase );
	VectorCopy( ent->s.origin, ent->r.currentOrigin );
	ent->use = Use_Static;

	if ( ent->spawnflags & 1 ) {
		trap_UnlinkEntity( ent );
	}

	if ( !( ent->flags & FL_TEAMSLAVE ) ) {
		int health;

		G_SpawnInt( "health", "0", &health );
		if ( health ) {
			ent->takedamage = qtrue;
		}
	}

	if ( ent->spawnflags & 2 || ent->spawnflags & 4 ) {
		ent->pain = Static_Pain;

		if ( !ent->delay ) {
			ent->delay = 1000;
		} else {
			ent->delay *= 1000;
		}

		ent->takedamage = qtrue;

		ent->isProp = qtrue;

		ent->health = 9999;

		if ( !( ent->count ) ) {
			ent->count = 4;
		}
	}

	// DISABLED since func_static will carve up AAS anyway, so blocking makes no sense
	/*
	// RF, check for blocking AAS
	if ( ent->spawnflags & 1 ) {
		// RF, AAS areas are now occupied
		if (ent->model) {
			ent->think = G_BlockThink;
			ent->nextthink = level.time + FRAMETIME;
		}
	}
	*/
}


/*
===============================================================================

ROTATING

===============================================================================
*/


/*QUAKED func_rotating (0 .5 .8) ? START_ON STARTINVIS X_AXIS Y_AXIS
You need to have an origin brush as part of this entity.  The center of that brush will be
the point around which it is rotated. It will rotate around the Z axis by default.  You can
check either the X_AXIS or Y_AXIS box to change that.

"model2"	.md3 model to also draw
"speed"		determines how fast it moves; default value is 100.
"dmg"		damage to inflict when blocked (2 default)
"color"		constantLight color
"light"		constantLight radius
*/

void Use_Func_Rotate( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
	if ( ent->spawnflags & 4 ) {
		ent->s.apos.trDelta[2] = ent->speed;
	} else if ( ent->spawnflags & 8 )   {
		ent->s.apos.trDelta[0] = ent->speed;
	} else {
		ent->s.apos.trDelta[1] = ent->speed;
	}

	if ( ent->spawnflags & 2 ) {
		ent->flags &= ~FL_TEAMSLAVE;
	}

	trap_LinkEntity( ent );
}

void SP_func_rotating( gentity_t *ent ) {
	if ( !ent->speed ) {
		ent->speed = 100;
	}

	// set the axis of rotation
	ent->s.apos.trType = TR_LINEAR;

	if ( ent->spawnflags & 1 ) {
		if ( ent->spawnflags & 4 ) {
			ent->s.apos.trDelta[2] = ent->speed;
		} else if ( ent->spawnflags & 8 ) {
			ent->s.apos.trDelta[0] = ent->speed;
		} else {
			ent->s.apos.trDelta[1] = ent->speed;
		}
	}

	if ( !ent->damage ) {
		ent->damage = 2;
	}

	trap_SetBrushModel( ent, ent->model );
	InitMover( ent );

	VectorCopy( ent->s.origin, ent->s.pos.trBase );
	VectorCopy( ent->s.pos.trBase, ent->r.currentOrigin );
	VectorCopy( ent->s.apos.trBase, ent->r.currentAngles );

	if ( ent->spawnflags & 2 ) {
		ent->flags |= FL_TEAMSLAVE;
		trap_UnlinkEntity( ent );
	} else {
		trap_LinkEntity( ent );
	}

}


/*
===============================================================================

BOBBING

===============================================================================
*/


/*QUAKED func_bobbing (0 .5 .8) ? X_AXIS Y_AXIS
Normally bobs on the Z axis
"model2"	.md3 model to also draw
"height"	amplitude of bob (32 default)
"speed"		seconds to complete a bob cycle (4 default)
"phase"		the 0.0 to 1.0 offset in the cycle to start at
"dmg"		damage to inflict when blocked (2 default)
"color"		constantLight color
"light"		constantLight radius
*/
void SP_func_bobbing( gentity_t *ent ) {
	float height;
	float phase;

	G_SpawnFloat( "speed", "4", &ent->speed );
	G_SpawnFloat( "height", "32", &height );
	G_SpawnInt( "dmg", "2", &ent->damage );
	G_SpawnFloat( "phase", "0", &phase );

	trap_SetBrushModel( ent, ent->model );
	InitMover( ent );

	VectorCopy( ent->s.origin, ent->s.pos.trBase );
	VectorCopy( ent->s.origin, ent->r.currentOrigin );

	ent->s.pos.trDuration = ent->speed * 1000;
	ent->s.pos.trTime = ent->s.pos.trDuration * phase;
	ent->s.pos.trType = TR_SINE;

	// set the axis of bobbing
	if ( ent->spawnflags & 1 ) {
		ent->s.pos.trDelta[0] = height;
	} else if ( ent->spawnflags & 2 ) {
		ent->s.pos.trDelta[1] = height;
	} else {
		ent->s.pos.trDelta[2] = height;
	}
}

/*
===============================================================================

PENDULUM

===============================================================================
*/


/*QUAKED func_pendulum (0 .5 .8) ?
You need to have an origin brush as part of this entity.
Pendulums always swing north / south on unrotated models.  Add an angles field to the model to allow rotation in other directions.
Pendulum frequency is a physical constant based on the length of the beam and gravity.
"model2"	.md3 model to also draw
"speed"		the number of degrees each way the pendulum swings, (30 default)
"phase"		the 0.0 to 1.0 offset in the cycle to start at
"dmg"		damage to inflict when blocked (2 default)
"color"		constantLight color
"light"		constantLight radius
*/
void SP_func_pendulum( gentity_t *ent ) {
	float freq;
	float length;
	float phase;
	float speed;

	G_SpawnFloat( "speed", "30", &speed );
	G_SpawnInt( "dmg", "2", &ent->damage );
	G_SpawnFloat( "phase", "0", &phase );

	trap_SetBrushModel( ent, ent->model );

	// find pendulum length
	length = fabs( ent->r.mins[2] );
	if ( length < 8 ) {
		length = 8;
	}

	freq = 1 / ( M_PI * 2 ) * sqrt( g_gravity.value / ( 3 * length ) );

	ent->s.pos.trDuration = ( 1000 / freq );

	InitMover( ent );

	VectorCopy( ent->s.origin, ent->s.pos.trBase );
	VectorCopy( ent->s.origin, ent->r.currentOrigin );

	VectorCopy( ent->s.angles, ent->s.apos.trBase );

	ent->s.apos.trDuration = 1000 / freq;
	ent->s.apos.trTime = ent->s.apos.trDuration * phase;
	ent->s.apos.trType = TR_SINE;
	ent->s.apos.trDelta[2] = speed;
}

/*QUAKED func_door_rotating (0 .5 .8) ? - TOGGLE X_AXIS Y_AXIS REVERSE FORCE STAYOPEN TAKE_KEY
You need to have an origin brush as part of this entity.  The center of that brush will be
the point around which it is rotated. It will rotate around the Z axis by default.  You can
check either the X_AXIS or Y_AXIS box to change that (only one axis allowed. If both X and Y
are checked, the default of Z will be used).
FORCE		door opens even if blocked
TAKE_KEY	removes the key from the players inventory
SHOOT-THRU	Bullets don't stop when they hit the door.  Set "shoot_thru_scale" with bullet damage scale (see below)

"key"       -1 for locked, key number for which key opens, 0 for open.  default '0' unless door is targeted.  (trigger_aidoor entities targeting this door do /not/ affect the key status)
"model2"    .md3 model to also draw
"degrees"   determines how many degrees it will turn (90 default)
"speed"	    movement speed (100 default)
"closespeed" optional different movement speed for door closing
"time"      how many milliseconds it will take to open 1 sec = 1000
"dmg"       damage to inflict when blocked (2 default)
"color"     constantLight color
"light"     constantLight radius
"shoot_thru_scale"	Multiplier for how much damage bullets do that have passed through the door.  Effectively how much damage the door 'absorbs'.  0.0 - 1.0 (0.0 will turn SHOOT-THRU off, 1.0 is full damage)
"type"		use sounds based on construction of door:
	 0 - nosound (default)
	 1 - metal
	 2 - stone
	 3 - lab
	 4 - wood
	 5 - iron/jail
	 6 - portcullis
	 7 - wood (quiet)
"team"		team name.  other doors with same team name will open/close in syncronicity
*/




//
//
void SP_func_door_rotating( gentity_t *ent ) {
	int key, doortype;

	G_SpawnInt( "type", "0", &doortype );

	if ( doortype ) { // /*why on earthy did this check for <=8?*/ && doortype <= 8)	// no doortype = silent
		DoorSetSounds( ent, doortype, qtrue );
	}


	// set the duration
	if ( !ent->speed ) {
		ent->speed = 1000;
	}

	// degrees door will open
	if ( !ent->angle ) {
		ent->angle = 90;
	}

	// reverse direction
	if ( ent->spawnflags & 16 ) {
		ent->angle *= -1;
	}

	// TOGGLE
	if ( ent->spawnflags & 2 ) {
		ent->flags |= FL_TOGGLE;
	}

	//---- (SA) door keys

	if ( G_SpawnInt( "key", "", &key ) ) { // if door has a key entered, set it
		ent->key = key;
	} else {
		ent->key = -2;                  // otherwise, set the key when this ent finishes spawning

	}
	// if the key is invalid, set the key in the finishSpawning routine
	if ( ent->key > MAX_DOOR_KEYS || ent->key < -2 ) {
		G_Error( "invalid key number: %d in func_door_rotating\n", ent->key );
		ent->key = -2;
	}
	//---- (SA) end


	// set the rotation axis
	VectorClear( ent->rotate );
	if      ( ent->spawnflags & 4 ) {
		ent->rotate[2] = 1;
	} else if ( ent->spawnflags & 8 ) {
		ent->rotate[0] = 1;
	} else { ent->rotate[1] = 1;}

	if ( VectorLength( ent->rotate ) > 1 ) { // check that rotation is only set for one axis
		G_Error( "Too many axis marked in func_door_rotating entity.  Only choose one axis of rotation. (defaulting to standard door rotation)" );
		VectorClear( ent->rotate );
		ent->rotate[1] = 1;
	}

	if ( !ent->wait ) {
		ent->wait = 2;
	}
	ent->wait *= 1000;

	//if (!ent->damage) {
	//	ent->damage = 2;
	//}

	trap_SetBrushModel( ent, ent->model );

	InitMoverRotate( ent );

	ent->s.dmgFlags = HINT_DOOR_ROTATING;

	if ( !( ent->flags & FL_TEAMSLAVE ) ) {
		int health;

		G_SpawnInt( "health", "0", &health );
		if ( health ) {
			ent->takedamage = qtrue;
		}
	}

	ent->nextthink = level.time + FRAMETIME;
	ent->think = finishSpawningKeyedMover;

	VectorCopy( ent->s.origin, ent->s.pos.trBase );
	VectorCopy( ent->s.pos.trBase, ent->r.currentOrigin );
	VectorCopy( ent->s.apos.trBase, ent->r.currentAngles );

	ent->blocked = Blocked_DoorRotate;

	trap_LinkEntity( ent );
}




/*
===============================================================================

EFFECTS

  I'm keeping all this stuff in here just to avoid collisions with Raf right now in g_misc or g_props
  Will move.
===============================================================================
*/

/*
==============
target_effect
==============
*/
void target_effect( gentity_t *self, gentity_t *other, gentity_t *activator ) {
	gentity_t   *tent;

	tent = G_TempEntity( self->r.currentOrigin, EV_EFFECT );
	VectorCopy( self->r.currentOrigin, tent->s.origin );
	if ( self->spawnflags & 32 ) {
		tent->s.dl_intensity = 1;   // low grav
	} else {
		tent->s.dl_intensity = 0;
	}

	trap_SetConfigstring( CS_TARGETEFFECT, self->dl_shader );   //----(SA)	allow shader to be set from entity

	// (SA) this should match the values from func_explosive
	tent->s.frame = self->key;      // pass the type to the client ("glass", "wood", "metal", "gibs", "brick", "stone", "fabric", 0, 1, 2, 3, 4, 5, 6)

	tent->s.eventParm       = self->spawnflags;
	tent->s.density         = self->health;

	if ( self->damage ) {
		G_RadiusDamage( self->s.pos.trBase, self, self->damage, self->damage, self, MOD_EXPLOSIVE );
	}

	G_UseTargets( self, other );
}


/*QUAKED target_effect (0 .5 .8) (-6 -6 -6) (6 6 6) fire explode smoke rubble gore lowgrav debris
"mass" defaults to 15.  This determines how much debris is emitted when it explodes.  (number of pieces)
"dmg" defaults to 0.  damage radius blast when triggered
"type" - if 'rubble' is specified, this is the model type ("glass", "wood", "metal", "gibs", "brick", "rock", "fabric") default is "wood"
*/
void SP_target_effect( gentity_t *ent ) {
	int mass;
	char    *type;

	ent->use = target_effect;

	if ( G_SpawnInt( "mass", "15", &mass ) ) {
		ent->health = mass;
	} else {
		ent->health = 15;
	}

	// (SA) this should match the values from func_explosive
	if ( G_SpawnString( "type", "wood", &type ) ) {
		if ( !Q_stricmp( type,"wood" ) ) {
			ent->key = 0;
		} else if ( !Q_stricmp( type,"glass" ) ) {
			ent->key = 1;
		} else if ( !Q_stricmp( type,"metal" ) )                                                       {
			ent->key = 2;
		} else if ( !Q_stricmp( type,"gibs" ) )                                                                                                               {
			ent->key = 3;
		} else if ( !Q_stricmp( type,"brick" ) )                                                                                                                                                                      {
			ent->key = 4;
		} else if ( !Q_stricmp( type,"rock" ) )                                                                                                                                                                                                                              {
			ent->key = 5;
		} else if ( !Q_stricmp( type,"fabric" ) )                                                                                                                                                                                                                                                                                     {
			ent->key = 6;
		}
	} else {
		ent->key = 5;   // default to 'rock'
	}

}



/*
===============================================================================

EXPLOSIVE
  I'm keeping all this stuff in here just to avoid collisions with Raf right now in g_misc or g_props
  Will move.
===============================================================================
*/


/*
==============
ThrowDebris
==============
*/
void ThrowDebris( gentity_t *self, char *modelname, float speed, vec3_t origin ) {
	// probably use le->leType = LE_FRAGMENT like brass and gibs
}

/*
==============
BecomeExplosion
	nuke the original entity and create all the debris entities that need to be synced to clients
==============
*/
void BecomeExplosion( gentity_t *self ) {
	self->die   = 0;
	self->pain  = 0;
	self->touch = 0;
	self->use   = 0;
	self->nextthink = level.time + FRAMETIME;
	self->think = G_FreeEntity;

	G_FreeEntity( self );
}



/*
==============
func_explosive_explode
	NOTE: the 'damage' passed in is ignored completely
==============
*/
void func_explosive_explode( gentity_t *self, gentity_t *inflictor, gentity_t *attacker, int damage, int mod ) {
	vec3_t origin;
	vec3_t size;
	vec3_t dir = {0, 0, 1};
	gentity_t   *tent = 0;

	// RF, AAS areas are now free
	if ( !( self->spawnflags & 16 ) ) {
		G_SetAASBlockingEntity( self, qfalse );
	}

	self->takedamage = qfalse;          // don't allow anything try to hurt me now that i'm exploding

	self->think = BecomeExplosion;
	self->nextthink = level.time + FRAMETIME;

	VectorSubtract( self->r.absmax, self->r.absmin, size );
	VectorScale( size, 0.5, size );
	VectorAdd( self->r.absmin, size, origin );

	VectorCopy( origin, self->s.pos.trBase );

	G_UseTargets( self, attacker );

	self->s.density = self->count;      // pass the "mass" to the client
	self->s.weapon = self->duration;    // pass the "force lowgrav" to client
	self->s.frame = self->key;          // pass the type to the client ("glass", "wood", "metal", "gibs", "brick", "stone", "fabric", 0, 1, 2, 3, 4, 5, 6)

	if ( self->damage ) {
//		G_RadiusDamage(self->s.origin, self, self->damage, self->damage+40, self, MOD_EXPLOSIVE);
		G_RadiusDamage( self->s.pos.trBase, self, self->damage, self->damage + 40, self, MOD_EXPLOSIVE );
	}

	// find target, aim at that
	if ( self->target ) {

		// since the explosive might need to fire the target rather than
		// aim at it, only aim at 'info_notnull' ents
		while ( 1 )
		{
			tent = G_Find( tent, FOFS( targetname ), self->target );
			if ( !tent ) {
				break;
			}

			if ( !Q_stricmp( tent->classname, "info_notnull" ) ) {
				break;  // found an info_notnull
			}
		}

		if ( tent ) {
			VectorSubtract( tent->s.pos.trBase, self->s.pos.trBase, dir );
			VectorNormalize( dir );
		}
	}

	// if a valid target entity was not found, check for a specified 'angle' for the explosion direction
	if ( !tent ) {
		if ( self->s.angles[1] ) {
			// up
			if ( self->s.angles[1] == -1 ) {
				// it's 'up' by default
			}
			// down
			else if ( self->s.angles[1] == -2 ) {
				dir[2] = -1;
			}
			// yawed
			else
			{
				RotatePointAroundVector( dir, dir, tv( 1, 0, 0 ), self->s.angles[1] );
			}
		}
	}

	G_AddEvent( self, EV_EXPLODE, DirToByte( dir ) );

}

/*
==============
func_explosive_touch
==============
*/
void func_explosive_touch( gentity_t *self, gentity_t *other, trace_t *trace ) {
//	func_explosive_explode(self, self, other, self->health, 0);
	func_explosive_explode( self, self, other, self->damage, 0 );
}


/*
==============
func_explosive_use
==============
*/
void func_explosive_use( gentity_t *self, gentity_t *other, gentity_t *activator ) {
//	func_explosive_explode (self, self, other, self->health, 0);
	G_Script_ScriptEvent( self, "death", "" ); // JPW NERVE used to trigger script stuff for MP
	func_explosive_explode( self, self, other, self->damage, 0 );
}

/*
==============
func_explosive_alert
==============
*/
void func_explosive_alert( gentity_t *self ) {
	func_explosive_explode( self, self, self, self->damage, 0 );
}

/*
==============
func_explosive_spawn
==============
*/
void func_explosive_spawn( gentity_t *self, gentity_t *other, gentity_t *activator ) {
	trap_LinkEntity( self );
	self->use = func_explosive_use;
	// turn the brush to visible

	// RF, AAS areas are now occupied
	if ( !( self->spawnflags & 16 ) ) {
		G_SetAASBlockingEntity( self, qtrue );
	}
}





/*
==============
InitExplosive
==============
*/
void InitExplosive( gentity_t *ent ) {
	char        *damage;

	// if the "model2" key is set, use a seperate model
	// for drawing, but clip against the brushes
	if ( ent->model2 ) {
		ent->s.modelindex2 = G_ModelIndex( ent->model2 );
	}

	// pick it up if the level designer uses "damage" instead of "dmg"
	if ( G_SpawnString( "damage", "0", &damage ) ) {
		ent->damage = atoi( damage );
	}

	ent->s.eType = ET_EXPLOSIVE;
	trap_LinkEntity( ent );

	ent->think = G_BlockThink;
	ent->nextthink = level.time + FRAMETIME;
}


/*QUAKED func_explosive (0 .5 .8) ? START_INVIS TOUCHABLE USESHADER LOWGRAV NOBLOCKAAS EXPLO DYNOMITE
EXPLO only explosives can damage it rockets grendades etc
DYNOMITE only can be damaged by DY-NO-MITE!
Any brush that you want to explode or break apart.  If you want an explosion, set dmg and it will do a radius explosion of that amount at the center of the bursh.
TOUCHABLE means automatic use on player contact.
USESHADER will apply the shader used on the brush model to the debris.
LOWGRAV specifies that the debris will /always/ fall slowly
"item" - when it explodes, pop this item out with the debirs (use QUAKED name. ex: "item_health_small")
"dmg" - how much radius damage should be done, defaults to 0
"health" - defaults to 100.  If health is set to '0' the brush will not be shootable.
"targetname" - if set, no touch field will be spawned and a remote button or trigger field triggers the explosion.
"type" - type of debris ("glass", "wood", "metal", "gibs", "brick", "rock", "fabric") default is "wood"
"mass" - defaults to 75.  This determines how much debris is emitted when it explodes.  You get one large chunk per 100 of mass (up to 8) and one small chunk per 25 of mass (up to 16).  So 800 gives the most.
"noise" - sound to play when triggered.  The explosive will default to a sound that matches it's 'type'.  Use the sound name "nosound" (case in-sensitive) if you want it silent.
the default sounds are:
  "wood"	- "sound/world/boardbreak.wav"
  "glass"	- "sound/world/glassbreak.wav"
  "metal"	- "sound/world/metalbreak.wav"
  "gibs"	- "sound/player/gibsplit1.wav"
  "brick"	- "sound/world/brickfall.wav"
  "stone"	- "sound/world/stonefall.wav"
  "fabric"	- "sound/world/metalbreak.wav"	// (SA) temp
*/
/*
"fxdensity" size of explosion 1 - 100 (default is 10)
*/
void SP_func_explosive( gentity_t *ent ) {
	int health, mass, dam, i;
	char buffer[MAX_QPATH];
	char    *s;
	char    *type;
	char    *cursorhint;

	trap_SetBrushModel( ent, ent->model );
	InitExplosive( ent );

	if ( ent->spawnflags & 1 ) {  // start invis
		ent->use = func_explosive_spawn;
		trap_UnlinkEntity( ent );
	} else if ( ent->targetname )     {
		ent->use = func_explosive_use;
		ent->AIScript_AlertEntity = func_explosive_alert;
	}


	if ( ent->spawnflags & 2 ) {  // touchable
		ent->touch = func_explosive_touch;
	} else {
		ent->touch = 0;
	}

	if ( ( ent->spawnflags & 4 ) && ent->model && strlen( ent->model ) ) {   // use shader
		ent->s.eFlags |= EF_INHERITSHADER;
	}

	if ( ent->spawnflags & 8 ) {  // force lowgravity
		ent->duration = 1;
	}

	G_SpawnInt( "health", "100", &health );
	ent->health = health;

	G_SpawnInt( "dmg", "0", &dam );
	ent->damage = dam;

	if ( ent->health ) {
		ent->takedamage = qtrue;
	}

	if ( G_SpawnInt( "mass", "75", &mass ) ) {
		ent->count = mass;
	} else {
		ent->count = 75;
	}

	if ( G_SpawnString( "type", "wood", &type ) ) {
		if ( !Q_stricmp( type,"wood" ) ) {
			ent->key = 0;
		} else if ( !Q_stricmp( type,"glass" ) ) {
			ent->key = 1;
		} else if ( !Q_stricmp( type,"metal" ) )                                                       {
			ent->key = 2;
		} else if ( !Q_stricmp( type,"gibs" ) )                                                                                                               {
			ent->key = 3;
		} else if ( !Q_stricmp( type,"brick" ) )                                                                                                                                                                      {
			ent->key = 4;
		} else if ( !Q_stricmp( type,"rock" ) )                                                                                                                                                                                                                              {
			ent->key = 5;
		} else if ( !Q_stricmp( type,"fabric" ) )                                                                                                                                                                                                                                                                                     {
			ent->key = 6;
		}
	} else {
		ent->key = 0;
	}

	if ( G_SpawnString( "noise", "NOSOUND", &s ) ) {
		if ( Q_stricmp( s, "nosound" ) ) {
			Q_strncpyz( buffer, s, sizeof( buffer ) );
			ent->s.dl_intensity = G_SoundIndex( buffer );
		}
	} else {
//		ent->s.dl_intensity = 0;
		switch ( ent->key )
		{
		case 0:     // "wood"
			ent->s.dl_intensity = G_SoundIndex( "sound/world/boardbreak.wav" );
			break;
		case 1:     // "glass"
			ent->s.dl_intensity = G_SoundIndex( "sound/world/glassbreak.wav" );
			break;
		case 2:     // "metal"
			ent->s.dl_intensity = G_SoundIndex( "sound/world/metalbreak.wav" );
			break;
		case 3:     // "gibs"
			ent->s.dl_intensity = G_SoundIndex( "sound/player/gibsplit1.wav" );
			break;
		case 4:     // "brick"
			ent->s.dl_intensity = G_SoundIndex( "sound/world/brickfall.wav" );
			break;
		case 5:     // "stone"
			ent->s.dl_intensity = G_SoundIndex( "sound/world/stonefall.wav" );
			break;

		default:
			break;
		}
	}

//----(SA)	added

	ent->s.dmgFlags = 0;

	if ( G_SpawnString( "cursorhint", "0", &cursorhint ) ) {

		for ( i = 0; i < HINT_NUM_HINTS; i++ ) {
			if ( !Q_strcasecmp( cursorhint, hintStrings[i] ) ) {
				ent->s.dmgFlags = i;
			}
		}
	}
//----(SA)	end

	// (SA) shouldn't need this
//	ent->s.density = ent->count;	// pass the "mass" to the client


	ent->die = func_explosive_explode;
}

/*QUAKED func_invisible_user (.3 .5 .8) ? STARTOFF HAS_USER NO_OFF_NOISE NOT_KICKABLE
when activated will use its target
"delay" - time (in seconds) before it can be used again
"offnoise" - specifies an alternate sound
"cursorhint" - overrides the auto-location of targeted entity (list below)
Normally when a player 'activates' this entity, if the entity has been turned 'off' (by a scripted command) you will hear a sound to indicate that you cannot activate the user.
The sound defaults to "sound/movers/invis_user_off.wav"

NO_OFF_NOISE - no sound will play if the invis_user is used when 'off'
NOT_KICKABLE - kicking doesn't fire, only player activating

"cursorhint" cursor types: (probably more, ask sherman if you think the list is out of date)
they /don't/ need to be all uppercase
	HINT_NONE
	HINT_PLAYER
	HINT_ACTIVATE
	HINT_DOOR
	HINT_DOOR_ROTATING
	HINT_DOOR_LOCKED
	HINT_DOOR_ROTATING_LOCKED
	HINT_MG42
	HINT_BREAKABLE
	HINT_BREAKABLE_BIG
	HINT_CHAIR
	HINT_ALARM
	HINT_HEALTH
	HINT_TREASURE
	HINT_KNIFE
	HINT_LADDER
	HINT_BUTTON
	HINT_WATER
	HINT_CAUTION
	HINT_DANGER
	HINT_SECRET
	HINT_QUESTION
	HINT_EXCLAMATION
	HINT_CLIPBOARD
	HINT_WEAPON
	HINT_AMMO
	HINT_ARMOR
	HINT_POWERUP
	HINT_HOLDABLE
	HINT_INVENTORY
	HINT_SCENARIC
	HINT_EXIT
	HINT_PLYR_FRIEND
	HINT_PLYR_NEUTRAL
	HINT_PLYR_ENEMY
	HINT_PLYR_UNKNOWN
*/

void use_invisible_user( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
	gentity_t *player;

	if ( ent->wait < level.time ) {
		ent->wait = level.time + ent->delay;
	} else {
		return;
	}

	if ( !( other->client ) ) {
		if ( ent->spawnflags & 1 ) {
			ent->spawnflags &= ~1;
		} else
		{
			ent->spawnflags |= 1;
		}

		if ( ent->spawnflags & 2 && !( ent->spawnflags & 1 ) ) {
			if ( ent->aiName ) {
				player = AICast_FindEntityForName( "player" );
				if ( player ) {
					AICast_ScriptEvent( AICast_GetCastState( player->s.number ), "trigger", ent->target );
				}
			}

			G_UseTargets( ent, other );

			// G_Printf ("ent%s used by %s\n", ent->classname, other->classname);
		}

		return;
	}

	if ( other->client && ent->spawnflags & 1 ) {
		//----(SA)	play 'off' sound
		//----(SA)	I think this is where this goes.  Raf, let me know if it's wrong.  I need someone to tell me what a test map is for this (I'll ask Dan tomorrow)
		// not usable by player.  turned off.
		G_Sound( ent, ent->soundPos1 );
		return;
	}

	if ( ent->aiName ) {
		player = AICast_FindEntityForName( "player" );
		if ( player ) {
			AICast_ScriptEvent( AICast_GetCastState( player->s.number ), "trigger", ent->target );
		}
	}

	G_UseTargets( ent, other ); //----(SA)	how about this so the triggered targets have an 'activator' as well as an 'other'?
								//----(SA)	Please let me know if you forsee any problems with this.
}


void func_invisible_user( gentity_t *ent ) {
	int i;
	char    *sound;
	char    *cursorhint;

	VectorCopy( ent->s.origin, ent->pos1 );
	trap_SetBrushModel( ent, ent->model );

	// InitMover (ent);
	VectorCopy( ent->pos1, ent->r.currentOrigin );
	trap_LinkEntity( ent );

	ent->s.pos.trType = TR_STATIONARY;
	VectorCopy( ent->pos1, ent->s.pos.trBase );

	ent->r.contents = CONTENTS_TRIGGER;

	ent->r.svFlags = SVF_NOCLIENT;

	ent->delay *= 1000; // convert to ms

	ent->use = use_invisible_user;


//----(SA)	added
	if ( G_SpawnString( "cursorhint", "0", &cursorhint ) ) {

		for ( i = 0; i < HINT_NUM_HINTS; i++ ) {
			if ( !Q_strcasecmp( cursorhint, hintStrings[i] ) ) {
				ent->s.dmgFlags = i;
			}
		}
	}
//----(SA)	end


	if ( !( ent->spawnflags & 4 ) ) {    // !NO_OFF_NOISE
		if ( G_SpawnString( "offnoise", "0", &sound ) ) {
			ent->soundPos1 = G_SoundIndex( sound );
		} else {
			ent->soundPos1 = G_SoundIndex( "sound/movers/invis_user_off.wav" );
		}
	}


}

/*
==========
G_Activate

  Generic activation routine for doors
==========
*/
void G_Activate( gentity_t *ent, gentity_t *activator ) {
	if ( ( ent->s.apos.trType == TR_STATIONARY && ent->s.pos.trType == TR_STATIONARY )
		 && ent->active == qfalse ) {
		// trigger the ent if possible, if not, then we'll just wait at the marker until it opens, which could be never(!?)
		if ( ent->key < 0 ) {  // ent force locked
			return;
		}

		if ( ent->key > 0 ) {  // ent requires key
			gitem_t *item = BG_FindItemForKey( ent->key, 0 );
			if ( !( activator->client->ps.stats[STAT_KEYS] & ( 1 << item->giTag ) ) ) {
				return;
			}
		}

		if ( !Q_stricmp( ent->classname, "script_mover" ) ) { // RF, dont activate script_mover's
			if ( activator->aiName ) {
				G_Script_ScriptEvent( ent, "activate", activator->aiName );
			}
			return;
		}

		// hack fix for bigdoor1 on tram1_21

		if ( !( ent->teammaster ) ) {
			ent->active = qtrue;
			Use_BinaryMover( ent, activator, activator );
			G_UseTargets( ent->teammaster, activator );
			return;
		}

		if ( ent->team && ent != ent->teammaster ) {
			ent->teammaster->active = qtrue;
			Use_BinaryMover( ent->teammaster, activator, activator );
			G_UseTargets( ent->teammaster, activator );
		} else
		{
			ent->active = qtrue;
			Use_BinaryMover( ent, activator, activator );
			G_UseTargets( ent->teammaster, activator );
		}
	}
}