summaryrefslogtreecommitdiff
path: root/src/game/g_mover.c
blob: 8f006a3ee597a5d48d0745c752556003ff6dc7d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
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
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2000-2006 Tim Angus

This file is part of Tremulous.

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

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

You should have received a copy of the GNU General Public License
along with Tremulous; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

#include "g_local.h"



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

PUSHMOVE

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

void MatchTeam( gentity_t *teamLeader, int moverState, int time );

typedef struct
{
  gentity_t *ent;
  vec3_t  origin;
  vec3_t  angles;
  float deltayaw;
} pushed_t;

pushed_t  pushed[ MAX_GENTITIES ], *pushed_p;


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

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

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

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

  return NULL;
}

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

/*
================
G_TransposeMatrix
================
*/
void G_TransposeMatrix( 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, 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    matrix[ 3 ], transpose[ 3 ];
  vec3_t    org, org2, move2;
  gentity_t *block;

  // 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 )
    return qfalse;

  //don't try to move buildables unless standing on a mover
  if( check->s.eType == ET_BUILDABLE &&
      check->s.groundEntityNum != pusher->s.number )
    return qfalse;

  // save off the old position
  if( pushed_p > &pushed[ MAX_GENTITIES ] )
    G_Error( "pushed_p > &pushed[MAX_GENTITIES]" );

  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
  // figure movement due to the pusher's amove
  G_CreateRotationMatrix( amove, transpose );
  G_TransposeMatrix( transpose, matrix );

  if( check->client )
    VectorSubtract( check->client->ps.origin, pusher->r.currentOrigin, org );
  else
    VectorSubtract( check->s.pos.trBase, pusher->r.currentOrigin, org );

  VectorCopy( org, org2 );
  G_RotatePoint( org2, matrix );
  VectorSubtract( org2, org, move2 );
  // add movement
  VectorAdd( check->s.pos.trBase, move, check->s.pos.trBase );
  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 );
    // make sure the client's view rotates when on a rotating mover
    check->client->ps.delta_angles[ YAW ] += ANGLE2SHORT( amove[ YAW ] );
  }

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

  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 );

    trap_LinkEntity( check );
    return qtrue;
  }

  // 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 = -1;
    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       listedEntities;
  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 ] + move[ i ] - radius;
      maxs[ i ] = pusher->r.currentOrigin[ i ] + move[ i ] + radius;
      totalMins[ i ] = mins[ i ] - move[ i ];
      totalMaxs[ i ] = maxs[ i ] - move[ i ];
    }
  }
  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 it's final position
  VectorAdd( pusher->r.currentOrigin, move, pusher->r.currentOrigin );
  VectorAdd( pusher->r.currentAngles, amove, pusher->r.currentAngles );
  trap_LinkEntity( pusher );

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

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

    // 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 ) )
        continue;
    }

    // the entity needs to be pushed
    if( G_TryPushingEntity( check, pusher, move, amove ) )
      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 );
      }

      trap_LinkEntity( p->ent );
    }

    return qfalse;
  }

  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( !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
    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 );
      }
    }
    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 )
    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 ) &&
      ent->moverState < MODEL_POS1 ) //yuck yuck hack
    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;

  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;
      break;

    case MOVER_POS2:
      VectorCopy( ent->pos2, ent->s.pos.trBase );
      ent->s.pos.trType = TR_STATIONARY;
      break;

    case MOVER_1TO2:
      VectorCopy( ent->pos1, ent->s.pos.trBase );
      VectorSubtract( ent->pos2, ent->pos1, 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_2TO1:
      VectorCopy( ent->pos2, ent->s.pos.trBase );
      VectorSubtract( ent->pos1, 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 ROTATOR_POS1:
      VectorCopy( ent->pos1, ent->s.apos.trBase );
      ent->s.apos.trType = TR_STATIONARY;
      break;

    case ROTATOR_POS2:
      VectorCopy( ent->pos2, ent->s.apos.trBase );
      ent->s.apos.trType = TR_STATIONARY;
      break;

    case ROTATOR_1TO2:
      VectorCopy( ent->pos1, ent->s.apos.trBase );
      VectorSubtract( ent->pos2, ent->pos1, delta );
      f = 1000.0 / ent->s.apos.trDuration;
      VectorScale( delta, f, ent->s.apos.trDelta );
      ent->s.apos.trType = TR_LINEAR_STOP;
      break;

    case ROTATOR_2TO1:
      VectorCopy( ent->pos2, ent->s.apos.trBase );
      VectorSubtract( ent->pos1, ent->pos2, delta );
      f = 1000.0 / ent->s.apos.trDuration;
      VectorScale( delta, f, ent->s.apos.trDelta );
      ent->s.apos.trType = TR_LINEAR_STOP;
      break;

    case MODEL_POS1:
      break;

    case MODEL_POS2:
      break;

    default:
      break;
  }

  if( moverState >= MOVER_POS1 && moverState <= MOVER_2TO1 )
    BG_EvaluateTrajectory( &ent->s.pos, level.time, ent->r.currentOrigin );

  if( moverState >= ROTATOR_POS1 && moverState <= ROTATOR_2TO1 )
    BG_EvaluateTrajectory( &ent->s.apos, level.time, ent->r.currentAngles );

  trap_LinkEntity( ent );
}

/*
================
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 )
    SetMoverState( slave, moverState, time );
}



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

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

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


/*
================
ReturnToApos1
================
*/
void ReturnToApos1( gentity_t *ent )
{
  MatchTeam( ent, ROTATOR_2TO1, level.time );

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

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


/*
================
Think_ClosedModelDoor
================
*/
void Think_ClosedModelDoor( gentity_t *ent )
{
  // play sound
  if( ent->soundPos1 )
    G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos1 );

  // close areaportals
  if( ent->teammaster == ent || !ent->teammaster )
    trap_AdjustAreaPortalState( ent, qfalse );

  ent->moverState = MODEL_POS1;
}


/*
================
Think_CloseModelDoor
================
*/
void Think_CloseModelDoor( gentity_t *ent )
{
  int       entityList[ MAX_GENTITIES ];
  int       numEntities, i;
  gentity_t *clipBrush = ent->clipBrush;
  gentity_t *check;
  qboolean  canClose = qtrue;

  numEntities = trap_EntitiesInBox( clipBrush->r.absmin, clipBrush->r.absmax, entityList, MAX_GENTITIES );

  //set brush solid
  trap_LinkEntity( ent->clipBrush );

  //see if any solid entities are inside the door
  for( i = 0; i < numEntities; i++ )
  {
    check = &g_entities[ entityList[ i ] ];

    //only test items and players
    if( check->s.eType != ET_ITEM && check->s.eType != ET_BUILDABLE &&
        check->s.eType != ET_CORPSE && check->s.eType != ET_PLAYER &&
        !check->physicsObject )
      continue;

    //test is this entity collides with this door
    if( G_TestEntityPosition( check ) )
      canClose = qfalse;
  }

  //something is blocking this door
  if( !canClose )
  {
    //set brush non-solid
    trap_UnlinkEntity( ent->clipBrush );

    ent->nextthink = level.time + ent->wait;
    return;
  }

  //toggle door state
  ent->s.legsAnim = qfalse;

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

  ent->moverState = MODEL_2TO1;

  ent->think = Think_ClosedModelDoor;
  ent->nextthink = level.time + ent->speed;
}


/*
================
Think_OpenModelDoor
================
*/
void Think_OpenModelDoor( gentity_t *ent )
{
  //set brush non-solid
  trap_UnlinkEntity( ent->clipBrush );

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

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

  ent->moverState = MODEL_POS2;

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

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

  G_UseTargets( ent, ent->activator );
}


/*
================
Reached_BinaryMover
================
*/
void Reached_BinaryMover( 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 );

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

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

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

    G_UseTargets( ent, ent->activator );
  }
  else if( ent->moverState == MOVER_2TO1 )
  {
    // reached pos1
    SetMoverState( ent, MOVER_POS1, level.time );

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

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

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

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

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

    G_UseTargets( ent, ent->activator );
  }
  else if( ent->moverState == ROTATOR_2TO1 )
  {
    // reached pos1
    SetMoverState( ent, ROTATOR_POS1, level.time );

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

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


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

  // if this is a non-client-usable door return
  if( ent->targetname && other && other->client )
    return;

  // only the master should be used
  if( ent->flags & FL_TEAMSLAVE )
  {
    Use_BinaryMover( 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
    if( ent->sound1to2 )
      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 );
  }
  else if( ent->moverState == MOVER_POS2 )
  {
    // if all the way up, just delay before coming down
    ent->nextthink = level.time + ent->wait;
  }
  else if( ent->moverState == MOVER_2TO1 )
  {
    // only partway down before reversing
    total = ent->s.pos.trDuration;
    partial = level.time - ent->s.pos.trTime;

    if( partial > total )
      partial = total;

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

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

    if( partial > total )
      partial = total;

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

    if( ent->sound2to1 )
      G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
  }
  else if( ent->moverState == ROTATOR_POS1 )
  {
    // start moving 50 msec later, becase if this was player
    // triggered, level.time hasn't been advanced yet
    MatchTeam( ent, ROTATOR_1TO2, level.time + 50 );

    // starting sound
    if( ent->sound1to2 )
      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 );
  }
  else if( ent->moverState == ROTATOR_POS2 )
  {
    // if all the way up, just delay before coming down
    ent->nextthink = level.time + ent->wait;
  }
  else if( ent->moverState == ROTATOR_2TO1 )
  {
    // only partway down before reversing
    total = ent->s.apos.trDuration;
    partial = level.time - ent->s.apos.trTime;

    if( partial > total )
      partial = total;

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

    if( ent->sound1to2 )
      G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
  }
  else if( ent->moverState == ROTATOR_1TO2 )
  {
    // only partway up before reversing
    total = ent->s.apos.trDuration;
    partial = level.time - ent->s.apos.trTime;

    if( partial > total )
      partial = total;

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

    if( ent->sound2to1 )
      G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
  }
  else if( ent->moverState == MODEL_POS1 )
  {
    //toggle door state
    ent->s.legsAnim = qtrue;

    ent->think = Think_OpenModelDoor;
    ent->nextthink = level.time + ent->speed;

    // starting sound
    if( ent->sound1to2 )
      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 );

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



/*
================
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 );
  }


  ent->use = Use_BinaryMover;
  ent->reached = Reached_BinaryMover;

  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;

  VectorScale( move, ent->speed, ent->s.pos.trDelta );
  ent->s.pos.trDuration = distance * 1000 / ent->speed;

  if( ent->s.pos.trDuration <= 0 )
    ent->s.pos.trDuration = 1;
}


/*
================
InitRotator

"pos1", "pos2", and "speed" should be set before calling,
so the movement delta can be calculated
================
*/
void InitRotator( gentity_t *ent )
{
  vec3_t    move;
  float     angle;
  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 );
  }


  ent->use = Use_BinaryMover;
  ent->reached = Reached_BinaryMover;

  ent->moverState = ROTATOR_POS1;
  ent->r.svFlags = SVF_USE_CURRENT_ORIGIN;
  ent->s.eType = ET_MOVER;
  VectorCopy( ent->pos1, ent->r.currentAngles );
  trap_LinkEntity( ent );

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

  // calculate time to reach second position from speed
  VectorSubtract( ent->pos2, ent->pos1, move );
  angle = VectorLength( move );

  if( !ent->speed )
    ent->speed = 120;

  VectorScale( move, ent->speed, ent->s.apos.trDelta );
  ent->s.apos.trDuration = angle * 1000 / ent->speed;

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


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

DOOR

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

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

/*
================
Blocked_Door
================
*/
void Blocked_Door( gentity_t *ent, gentity_t *other )
{
  // remove anything other than a client or buildable
  if( !other->client && other->s.eType != ET_BUILDABLE )
  {
    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 );
}

/*
================
Touch_DoorTriggerSpectator
================
*/
static void Touch_DoorTriggerSpectator( gentity_t *ent, gentity_t *other, trace_t *trace )
{
  int     i, axis;
  vec3_t  origin, dir, angles;

  axis = ent->count;
  VectorClear( dir );

  if( fabs( other->s.origin[ axis ] - ent->r.absmax[ axis ] ) <
      fabs( other->s.origin[ axis ] - ent->r.absmin[ axis ] ) )
  {
    origin[ axis ] = ent->r.absmin[ axis ] - 20;
    dir[ axis ] = -1;
  }
  else
  {
    origin[ axis ] = ent->r.absmax[ axis ] + 20;
    dir[ axis ] = 1;
  }

  for( i = 0; i < 3; i++ )
  {
    if( i == axis )
      continue;

    origin[ i ] = ( ent->r.absmin[ i ] + ent->r.absmax[ i ] ) * 0.5;
  }

  vectoangles( dir, angles );
  TeleportPlayer( other, origin, angles );
}


/*
================
manualDoorTriggerSpectator

This effectively creates a temporary door auto trigger so manually
triggers doors can be skipped by spectators
================
*/
static void manualDoorTriggerSpectator( gentity_t *door, gentity_t *player )
{
  gentity_t *other;
  gentity_t triggerHull;
  int       best, i;
  vec3_t    mins, maxs;

  //don't skip a door that is already open
  if( door->moverState == MOVER_1TO2 ||
      door->moverState == MOVER_POS2 ||
      door->moverState == ROTATOR_1TO2 ||
      door->moverState == ROTATOR_POS2 ||
      door->moverState == MODEL_1TO2 ||
      door->moverState == MODEL_POS2 )
    return;

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

  for( other = door->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 ] += 60;
  mins[ best ] -= 60;

  VectorCopy( mins, triggerHull.r.absmin );
  VectorCopy( maxs, triggerHull.r.absmax );
  triggerHull.count = best;

  Touch_DoorTriggerSpectator( &triggerHull, player, NULL );
}

/*
================
manualTriggerSpectator

Trip to skip the closest door targetted by trigger
================
*/
void manualTriggerSpectator( gentity_t *trigger, gentity_t *player )
{
  gentity_t *t = NULL;
  gentity_t *targets[ MAX_GENTITIES ];
  int       i = 0, j;
  float     minDistance = (float)INFINITE;

  //restrict this hack to trigger_multiple only for now
  if( strcmp( trigger->classname, "trigger_multiple" ) )
    return;

  if( !trigger->target )
    return;

  //create a list of door entities this trigger targets
  while( ( t = G_Find( t, FOFS( targetname ), trigger->target ) ) != NULL )
  {
    if( !strcmp( t->classname, "func_door" ) )
      targets[ i++ ] = t;
    else if( t == trigger )
      G_Printf( "WARNING: Entity used itself.\n" );

    if( !trigger->inuse )
    {
      G_Printf( "triggerity was removed while using targets\n" );
      return;
    }
  }

  //if more than 0 targets
  if( i > 0 )
  {
    gentity_t *closest = NULL;

    //pick the closest door
    for( j = 0; j < i; j++ )
    {
      float d = Distance( player->r.currentOrigin, targets[ j ]->r.currentOrigin );

      if( d < minDistance )
      {
        minDistance = d;
        closest = targets[ j ];
      }
    }

    //try and skip the door
    manualDoorTriggerSpectator( closest, player );
  }
}


/*
================
Touch_DoorTrigger
================
*/
void Touch_DoorTrigger( gentity_t *ent, gentity_t *other, trace_t *trace )
{
  //buildables don't trigger movers
  if( other->s.eType == ET_BUILDABLE )
    return;

  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 &&
        ent->parent->moverState != ROTATOR_1TO2 &&
        ent->parent->moverState != ROTATOR_POS2 )
      Touch_DoorTriggerSpectator( ent, other, trace );
  }
  else if( ent->parent->moverState != MOVER_1TO2 &&
           ent->parent->moverState != ROTATOR_1TO2 &&
           ent->parent->moverState != ROTATOR_2TO1 )
  {
    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;

  //TA: disable shootable doors
  // 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 ] += 60;
  mins[ best ] -= 60;

  // create a trigger with this size
  other = G_Spawn( );
  other->classname = "door_trigger";
  VectorCopy( mins, other->r.mins );
  VectorCopy( maxs, other->r.maxs );
  other->parent = ent;
  other->r.contents = CONTENTS_TRIGGER;
  other->touch = Touch_DoorTrigger;
  // remember the thinnest axis
  other->count = best;
  trap_LinkEntity( other );

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

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


/*QUAKED func_door (0 .5 .8) ? START_OPEN x CRUSHER
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

"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 (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
*/
void SP_func_door( gentity_t *ent )
{
  vec3_t  abs_movedir;
  float   distance;
  vec3_t  size;
  float   lip;
  char    *s;

  G_SpawnString( "sound2to1", "sound/movers/doors/dr1_strt.wav", &s );
  ent->sound2to1 = G_SoundIndex( s );
  G_SpawnString( "sound1to2", "sound/movers/doors/dr1_strt.wav", &s );
  ent->sound1to2 = G_SoundIndex( s );

  G_SpawnString( "soundPos2", "sound/movers/doors/dr1_end.wav", &s );
  ent->soundPos2 = G_SoundIndex( s );
  G_SpawnString( "soundPos1", "sound/movers/doors/dr1_end.wav", &s );
  ent->soundPos1 = G_SoundIndex( s );

  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;

  // 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 "start_open", reverse position 1 and 2
  if( ent->spawnflags & 1 )
  {
    vec3_t  temp;

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

  InitMover( ent );

  ent->nextthink = level.time + FRAMETIME;

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

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

    if( ent->targetname || health )
    {
      // non touch/shoot doors
      ent->think = Think_MatchTeam;
    }
    else
      ent->think = Think_SpawnNewDoorTrigger;
  }
}

/*QUAKED func_door_rotating (0 .5 .8) START_OPEN CRUSHER REVERSE TOGGLE X_AXIS Y_AXIS
 * This is the rotating door... just as the name suggests it's a door that rotates
 * START_OPEN the door to moves to its destination when spawned, and operate in reverse.
 * REVERSE    if you want the door to open in the other direction, use this switch.
 * TOGGLE   wait in both the start and end states for a trigger event.
 * X_AXIS   open on the X-axis instead of the Z-axis
 * Y_AXIS   open on the Y-axis instead of the Z-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
 *   "distance" how many degrees the door will open
 *   "speed"    how fast the door will open (degrees/second)
 *   "color"    constantLight color
 *   "light"    constantLight radius
 *   */

void SP_func_door_rotating( gentity_t *ent )
{
  char    *s;

  G_SpawnString( "sound2to1", "sound/movers/doors/dr1_strt.wav", &s );
  ent->sound2to1 = G_SoundIndex( s );
  G_SpawnString( "sound1to2", "sound/movers/doors/dr1_strt.wav", &s );
  ent->sound1to2 = G_SoundIndex( s );

  G_SpawnString( "soundPos2", "sound/movers/doors/dr1_end.wav", &s );
  ent->soundPos2 = G_SoundIndex( s );
  G_SpawnString( "soundPos1", "sound/movers/doors/dr1_end.wav", &s );
  ent->soundPos1 = G_SoundIndex( s );

  ent->blocked = Blocked_Door;

  //default speed of 120
  if( !ent->speed )
    ent->speed = 120;

  // if speed is negative, positize it and add reverse flag
  if( ent->speed < 0 )
  {
    ent->speed *= -1;
    ent->spawnflags |= 8;
  }

  // default of 2 seconds
  if( !ent->wait )
    ent->wait = 2;

  ent->wait *= 1000;

  // set the axis of rotation
  VectorClear( ent->movedir );
  VectorClear( ent->s.angles );

  if( ent->spawnflags & 32 )
    ent->movedir[ 2 ] = 1.0;
  else if( ent->spawnflags & 64 )
    ent->movedir[ 0 ] = 1.0;
  else
    ent->movedir[ 1 ] = 1.0;

  // reverse direction if necessary
  if( ent->spawnflags & 8 )
    VectorNegate ( ent->movedir, ent->movedir );

  // default distance of 90 degrees. This is something the mapper should not
  // leave out, so we'll tell him if he does.
  if( !ent->rotatorAngle )
  {
    G_Printf( "%s at %s with no rotatorAngle set.\n",
              ent->classname, vtos( ent->s.origin ) );

    ent->rotatorAngle = 90.0;
  }

  VectorCopy( ent->s.angles, ent->pos1 );
  trap_SetBrushModel( ent, ent->model );
  VectorMA( ent->pos1, ent->rotatorAngle, ent->movedir, ent->pos2 );

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

    VectorCopy( ent->pos2, temp );
    VectorCopy( ent->s.angles, ent->pos2 );
    VectorCopy( temp, ent->pos1 );
    VectorNegate( ent->movedir, ent->movedir );
  }

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

  InitRotator( ent );

  ent->nextthink = level.time + FRAMETIME;

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

    G_SpawnInt( "health", "0", &health );

    if( health )
      ent->takedamage = qtrue;

    if( ent->targetname || health )
    {
      // non touch/shoot doors
      ent->think = Think_MatchTeam;
    }
    else
      ent->think = Think_SpawnNewDoorTrigger;
  }
}

/*QUAKED func_door_model (0 .5 .8) ? START_OPEN
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

"model2"  .md3 model to also draw
"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 (3 default, -1 = never return)
"color"   constantLight color
"light"   constantLight radius
"health"  if set, the door must be shot open
*/
void SP_func_door_model( gentity_t *ent )
{
  char      *s;
  float     light;
  vec3_t    color;
  qboolean  lightSet, colorSet;
  char      *sound;
  gentity_t *clipBrush;

  G_SpawnString( "sound2to1", "sound/movers/doors/dr1_strt.wav", &s );
  ent->sound2to1 = G_SoundIndex( s );
  G_SpawnString( "sound1to2", "sound/movers/doors/dr1_strt.wav", &s );
  ent->sound1to2 = G_SoundIndex( s );

  G_SpawnString( "soundPos2", "sound/movers/doors/dr1_end.wav", &s );
  ent->soundPos2 = G_SoundIndex( s );
  G_SpawnString( "soundPos1", "sound/movers/doors/dr1_end.wav", &s );
  ent->soundPos1 = G_SoundIndex( s );

  //default speed of 100ms
  if( !ent->speed )
    ent->speed = 200;

  //default wait of 2 seconds
  if( ent->wait <= 0 )
    ent->wait = 2;

  ent->wait *= 1000;

  //brush model
  clipBrush = ent->clipBrush = G_Spawn( );
  clipBrush->model = ent->model;
  trap_SetBrushModel( clipBrush, clipBrush->model );
  clipBrush->s.eType = ET_INVISIBLE;
  trap_LinkEntity( clipBrush );

  //copy the bounds back from the clipBrush so the
  //triggers can be made
  VectorCopy( clipBrush->r.absmin, ent->r.absmin );
  VectorCopy( clipBrush->r.absmax, ent->r.absmax );
  VectorCopy( clipBrush->r.mins, ent->r.mins );
  VectorCopy( clipBrush->r.maxs, ent->r.maxs );

  G_SpawnVector( "modelOrigin", "0 0 0", ent->s.origin );

  G_SpawnVector( "scale", "1 1 1", ent->s.origin2 );

  // if the "model2" key is set, use a seperate model
  // for drawing, but clip against the brushes
  if( !ent->model2 )
    G_Printf( S_COLOR_YELLOW "WARNING: func_door_model %d spawned with no model2 key\n", ent->s.number );
  else
    ent->s.modelindex = 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 );
  }

  ent->use = Use_BinaryMover;

  ent->moverState = MODEL_POS1;
  ent->s.eType = ET_MODELDOOR;
  VectorCopy( ent->s.origin, ent->s.pos.trBase );
  ent->s.pos.trType = TR_STATIONARY;
  ent->s.pos.trTime = 0;
  ent->s.pos.trDuration = 0;
  VectorClear( ent->s.pos.trDelta );
  VectorCopy( ent->s.angles, ent->s.apos.trBase );
  ent->s.apos.trType = TR_STATIONARY;
  ent->s.apos.trTime = 0;
  ent->s.apos.trDuration = 0;
  VectorClear( ent->s.apos.trDelta );

  ent->s.misc      = (int)ent->animation[ 0 ];                  //first frame
  ent->s.weapon    = abs( (int)ent->animation[ 1 ] );           //number of frames

  //must be at least one frame -- mapper has forgotten animation key
  if( ent->s.weapon == 0 )
    ent->s.weapon = 1;

  ent->s.torsoAnim = ent->s.weapon * ( 1000.0f / ent->speed );  //framerate

  trap_LinkEntity( ent );

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

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

    if( !( ent->targetname || health ) )
    {
      ent->nextthink = level.time + FRAMETIME;
      ent->think = Think_SpawnNewDoorTrigger;
    }
  }
}

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

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 )
{
  // DONT_WAIT
  if( ent->spawnflags & 1 )
    return;

  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->classname = "plat_trigger";
  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;
  char  *s;

  G_SpawnString( "sound2to1", "sound/movers/plats/pt1_strt.wav", &s );
  ent->sound2to1 = G_SoundIndex( s );
  G_SpawnString( "sound1to2", "sound/movers/plats/pt1_strt.wav", &s );
  ent->sound1to2 = G_SoundIndex( s );

  G_SpawnString( "soundPos2", "sound/movers/plats/pt1_end.wav", &s );
  ent->soundPos2 = G_SoundIndex( s );
  G_SpawnString( "soundPos1", "sound/movers/plats/pt1_end.wav", &s );
  ent->soundPos1 = G_SoundIndex( s );

  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) ?
When a button is touched, it moves some distance in the direction of it's angle, triggers all of it's targets, waits some time, then returns to it's 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;
  char    *s;

  G_SpawnString( "sound1to2", "sound/movers/switches/button1.wav", &s );
  ent->sound1to2 = G_SoundIndex( s );

  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
  {
    // touchable button
    ent->touch = Touch_Button;
  }

  InitMover( ent );
}



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

TRAIN

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


#define TRAIN_START_OFF   1
#define TRAIN_BLOCK_STOPS 2

/*
===============
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

  // 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->lastSpeed = speed;

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

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

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

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

  if( ent->spawnflags & TRAIN_START_OFF )
  {
    ent->s.pos.trType = TR_STATIONARY;
    return;
  }

  // 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;
  }
}

/*
================
Start_Train
================
*/
void Start_Train( gentity_t *ent, gentity_t *other, gentity_t *activator )
{
  vec3_t  move;

  //recalculate duration as the mover is highly
  //unlikely to be right on a path_corner
  VectorSubtract( ent->pos2, ent->pos1, move );
  ent->s.pos.trDuration = VectorLength( move ) * 1000 / ent->lastSpeed;
  SetMoverState( ent, MOVER_1TO2, level.time );

  ent->spawnflags &= ~TRAIN_START_OFF;
}

/*
================
Stop_Train
================
*/
void Stop_Train( gentity_t *ent, gentity_t *other, gentity_t *activator )
{
  vec3_t  origin;

  //get current origin
  BG_EvaluateTrajectory( &ent->s.pos, level.time, origin );
  VectorCopy( origin, ent->pos1 );
  SetMoverState( ent, MOVER_POS1, level.time );

  ent->spawnflags |= TRAIN_START_OFF;
}

/*
================
Use_Train
================
*/
void Use_Train( gentity_t *ent, gentity_t *other, gentity_t *activator )
{
  if( ent->spawnflags & TRAIN_START_OFF )
  {
    //train is currently not moving so start it
    Start_Train( ent, other, activator );
  }
  else
  {
    //train is moving so stop it
    Stop_Train( ent, other, activator );
  }
}

/*
===============
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;
  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( ent );
}



/*QUAKED path_corner (.5 .3 0) (-8 -8 -8) (8 8 8)
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
*/
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
}

/*
================
Blocked_Train
================
*/
void Blocked_Train( gentity_t *self, gentity_t *other )
{
  if( self->spawnflags & TRAIN_BLOCK_STOPS )
    Stop_Train( self, other, other );
  else
  {
    if( !other->client )
    {
      //whatever is blocking the train isn't a client

      //KILL!!1!!!
      G_Damage( other, self, self, NULL, NULL, 10000, 0, MOD_CRUSH );

      //buildables need to be handled differently since even when
      //dealth fatal amounts of damage they won't instantly become non-solid
      if( other->s.eType == ET_BUILDABLE && other->spawned )
      {
        vec3_t    dir;
        gentity_t *tent;

        if( other->biteam == BIT_ALIENS )
        {
          VectorCopy( other->s.origin2, dir );
          tent = G_TempEntity( other->s.origin, EV_ALIEN_BUILDABLE_EXPLOSION );
          tent->s.eventParm = DirToByte( dir );
        }
        else if( other->biteam == BIT_HUMANS )
        {
          VectorSet( dir, 0.0f, 0.0f, 1.0f );
          tent = G_TempEntity( other->s.origin, EV_HUMAN_BUILDABLE_EXPLOSION );
          tent->s.eventParm = DirToByte( dir );
        }
      }

      //if it's still around free it
      if( other )
        G_FreeEntity( other );

      return;
    }

    G_Damage( other, self, self, NULL, NULL, 10000, 0, MOD_CRUSH );
  }
}


/*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;
  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;
  self->use = Use_Train;
  self->blocked = Blocked_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;
}

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

STATIC

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


/*QUAKED func_static (0 .5 .8) ?
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
*/
void SP_func_static( gentity_t *ent )
{
  trap_SetBrushModel( ent, ent->model );
  InitMover( ent );
  VectorCopy( ent->s.origin, ent->s.pos.trBase );
  VectorCopy( ent->s.origin, ent->r.currentOrigin );
}


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

ROTATING

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


/*QUAKED func_rotating (0 .5 .8) ? START_ON - 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 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 & 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 );

  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;
}