summaryrefslogtreecommitdiff
path: root/src/game/g_cmds.c
blob: c1f6c62fd1979ba62c4cff55be545bca144e6fee (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
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
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.F
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"

//rotax
char marking_h[ 9 ] = "^4[H] ^7";
char marking_a[ 9 ] = "^1[A] ^7";
char marking_s[ 9 ] = "^3[S] ^7";
char marking_temp[100];

//rotax
char * marking(gentity_t *ent)
{
  switch( ent->client->pers.teamSelection)
  {
    default:
    case PTE_NONE:
      return marking_s;
      break;

    case PTE_ALIENS:
      return marking_a;
      break;

    case PTE_HUMANS:
      return marking_h;
  }
}

//rotax
char * marking_vote(gentity_t *ent)
{ 
  Com_sprintf(marking_temp,
      sizeof(marking_temp), "%s%s^7 asks:", marking(ent), ent->client->pers.netname);
  
  return marking_temp;
}

/*
==================
G_SanitiseString

Remove case and control characters from a player name
==================
*/
void G_SanitiseString( char *in, char *out, int len )
{
  qboolean skip = qtrue;
  int spaces = 0;

  while( *in && len > 0 )
  {
    // strip leading white space
    if( *in == ' ' )
    {
      if( skip )
      {
        in++;
        continue;
      }
      spaces++;
    }
    else
    {
      spaces = 0;
      skip = qfalse;
    }
    
    if( Q_IsColorString( in ) )
    {
      in += 2;    // skip color code
      continue;
    }

    if( *in < 32 )
    {
      in++;
      continue;
    }

    *out++ = tolower( *in++ );
    len--;
  }
  out -= spaces; 
  *out = 0;
}

/*
==================
G_ClientNumberFromString

Returns a player number for either a number or name string
Returns -1 if invalid
==================
*/
int G_ClientNumberFromString( gentity_t *to, char *s )
{
  gclient_t *cl;
  int       idnum;
  char      s2[ MAX_STRING_CHARS ];
  char      n2[ MAX_STRING_CHARS ];

  // numeric values are just slot numbers
  if( s[ 0 ] >= '0' && s[ 0 ] <= '9' )
  {
    idnum = atoi( s );

    if( idnum < 0 || idnum >= level.maxclients )
      return -1;

    cl = &level.clients[ idnum ];

    if( cl->pers.connected == CON_DISCONNECTED )
      return -1;

    return idnum;
  }

  // check for a name match
  G_SanitiseString( s, s2, sizeof( s2 ) );

  for( idnum = 0, cl = level.clients; idnum < level.maxclients; idnum++, cl++ )
  {
    if( cl->pers.connected == CON_DISCONNECTED )
      continue;

    G_SanitiseString( cl->pers.netname, n2, sizeof( n2 ) );

    if( !strcmp( n2, s2 ) )
      return idnum;
  }

  return -1;
}


/*
==================
G_MatchOnePlayer

This is a companion function to G_ClientNumbersFromString()

returns qtrue if the int array plist only has one client id, false otherwise
In the case of false, err will be populated with an error message.
==================
*/
qboolean G_MatchOnePlayer( int *plist, char *err, int len )
{
  gclient_t *cl;
  int *p;
  char line[ MAX_NAME_LENGTH + 10 ] = {""};

  err[ 0 ] = '\0';
  if( plist[ 0 ] == -1 )
  {
    Q_strcat( err, len, "no connected player by that name or slot #" );
    return qfalse;
  }
  if( plist[ 1 ] != -1 )
  {
    Q_strcat( err, len, "more than one player name matches. "
            "be more specific or use the slot #:\n" );
    for( p = plist; *p != -1; p++ )
    {
      cl = &level.clients[ *p ];
      if( cl->pers.connected == CON_CONNECTED )
      {
        Com_sprintf( line, sizeof( line ), "%2i - %s^7\n",
          *p, cl->pers.netname );
        if( strlen( err ) + strlen( line ) > len )
          break;
        Q_strcat( err, len, line );
      }
    }
    return qfalse;
  }
  return qtrue;
}

/*
==================
G_ClientNumbersFromString

Sets plist to an array of integers that represent client numbers that have
names that are a partial match for s.

Returns number of matching clientids up to MAX_CLIENTS.
==================
*/
int G_ClientNumbersFromString( char *s, int *plist)
{
  gclient_t *p;
  int i, found = 0;
  char n2[ MAX_NAME_LENGTH ] = {""};
  char s2[ MAX_NAME_LENGTH ] = {""};
  int max = MAX_CLIENTS;

  // if a number is provided, it might be a slot #
  for( i = 0; s[ i ] && isdigit( s[ i ] ); i++ );
  if( !s[ i ] )
  {
    i = atoi( s );
    if( i >= 0 && i < level.maxclients )
    {
      p = &level.clients[ i ];
      if( p->pers.connected != CON_DISCONNECTED )
      {
        *plist = i;
        return 1;
      }
    }
    // we must assume that if only a number is provided, it is a clientNum
    *plist = -1;
    return 0;
  }

  // now look for name matches
  G_SanitiseString( s, s2, sizeof( s2 ) );
  if( strlen( s2 ) < 1 )
    return 0;
  for( i = 0; i < level.maxclients && found <= max; i++ )
  {
    p = &level.clients[ i ];
    if( p->pers.connected == CON_DISCONNECTED )
    {
      continue;
    }
    G_SanitiseString( p->pers.netname, n2, sizeof( n2 ) );
    if( strstr( n2, s2 ) )
    {
      *plist++ = i;
      found++;
    }
  }
  *plist = -1;
  return found;
}

/*
==================
ScoreboardMessage

==================
*/
void ScoreboardMessage( gentity_t *ent )
{
  char      entry[ 1024 ];
  char      string[ 1400 ];
  int       stringlength;
  int       i, j;
  gclient_t *cl;
  int       numSorted;
  weapon_t  weapon = WP_NONE;
  upgrade_t upgrade = UP_NONE;

  // send the latest information on all clients
  string[ 0 ] = 0;
  stringlength = 0;

  numSorted = level.numConnectedClients;

  for( i = 0; i < numSorted; i++ )
  {
    int   ping;

    cl = &level.clients[ level.sortedClients[ i ] ];

    if( cl->pers.connected == CON_CONNECTING )
      ping = -1;
    else if( cl->sess.spectatorState == SPECTATOR_FOLLOW )
      ping = cl->pers.ping < 999 ? cl->pers.ping : 999;
    else
      ping = cl->ps.ping < 999 ? cl->ps.ping : 999;

    //If (loop) client is a spectator, they have nothing, so indicate such. 
    //Only send the client requesting the scoreboard the weapon/upgrades information for members of their team. If they are not on a team, send it all.
    if( cl->sess.sessionTeam != TEAM_SPECTATOR && 
      (ent->client->pers.teamSelection == PTE_NONE || cl->pers.teamSelection == ent->client->pers.teamSelection ) )
    {
      weapon = cl->ps.weapon;

      if( BG_InventoryContainsUpgrade( UP_BATTLESUIT, cl->ps.stats ) )
        upgrade = UP_BATTLESUIT;
      else if( BG_InventoryContainsUpgrade( UP_JETPACK, cl->ps.stats ) )
        upgrade = UP_JETPACK;
      else if( BG_InventoryContainsUpgrade( UP_BATTPACK, cl->ps.stats ) )
        upgrade = UP_BATTPACK;
      else if( BG_InventoryContainsUpgrade( UP_HELMET, cl->ps.stats ) )
        upgrade = UP_HELMET;
      else if( BG_InventoryContainsUpgrade( UP_LIGHTARMOUR, cl->ps.stats ) )
        upgrade = UP_LIGHTARMOUR;
      else
        upgrade = UP_NONE;
    }
    else
    {
      weapon = WP_NONE;
      upgrade = UP_NONE;
    }

    Com_sprintf( entry, sizeof( entry ),
      " %d %d %d %d %d %d", level.sortedClients[ i ], cl->pers.score, ping, 
      ( level.time - cl->pers.enterTime ) / 60000, weapon, upgrade );

    j = strlen( entry );

    if( stringlength + j > 1024 )
      break;

    strcpy( string + stringlength, entry );
    stringlength += j;
  }

  trap_SendServerCommand( ent-g_entities, va( "scores %i %i %i%s", i,
    level.alienKills, level.humanKills, string ) );
}


/*
==================
ConcatArgs
==================
*/
char *ConcatArgs( int start )
{
  int         i, c, tlen;
  static char line[ MAX_STRING_CHARS ];
  int         len;
  char        arg[ MAX_STRING_CHARS ];

  len = 0;
  c = trap_Argc( );

  for( i = start; i < c; i++ )
  {
    trap_Argv( i, arg, sizeof( arg ) );
    tlen = strlen( arg );

    if( len + tlen >= MAX_STRING_CHARS - 1 )
      break;

    memcpy( line + len, arg, tlen );
    len += tlen;

    if( len == MAX_STRING_CHARS - 1 )
      break;

    if( i != c - 1 )
    {
      line[ len ] = ' ';
      len++;
    }
  }

  line[ len ] = 0;

  return line;
}

/*
==================
G_Flood_Limited

Determine whether a user is flood limited, and adjust their flood demerits
==================
*/

qboolean G_Flood_Limited( gentity_t *ent )
{
  int millisSinceLastCommand;
  int maximumDemerits;

  // This shouldn't be called if g_floodMinTime isn't set, but handle it anyway.
  if( !g_floodMinTime.integer )
    return qfalse;
  
  if( level.paused ) //Doesn't work when game is paused, so disable
    return qfalse;
  
  // Do not limit admins with no censor/flood flag
  if( G_admin_permission( ent, ADMF_NOCENSORFLOOD ) )
   return qfalse;
  
  millisSinceLastCommand = level.time - ent->client->pers.lastFloodTime;
  if( millisSinceLastCommand < g_floodMinTime.integer )
    ent->client->pers.floodDemerits += ( g_floodMinTime.integer - millisSinceLastCommand );
  else
  {
    ent->client->pers.floodDemerits -= ( millisSinceLastCommand - g_floodMinTime.integer );
    if( ent->client->pers.floodDemerits < 0 )
      ent->client->pers.floodDemerits = 0;
  }

  ent->client->pers.lastFloodTime = level.time;

  // If g_floodMaxDemerits == 0, then we go against g_floodMinTime^2.
  
  if( !g_floodMaxDemerits.integer )
     maximumDemerits = g_floodMinTime.integer * g_floodMinTime.integer / 1000;
  else
     maximumDemerits = g_floodMaxDemerits.integer;

  if( ent->client->pers.floodDemerits > maximumDemerits )
     return qtrue;

  return qfalse;
}
  
/*
==================
Cmd_Give_f

Give items to a client
==================
*/
void Cmd_Give_f( gentity_t *ent )
{
  char      *name;
  qboolean  give_all = qfalse;

  name = ConcatArgs( 1 );
  if( Q_stricmp( name, "all" ) == 0 )
    give_all = qtrue;

  if( give_all || Q_stricmp( name, "health" ) == 0 )
  {
    if(!g_devmapNoGod.integer)
    {
     ent->health = ent->client->ps.stats[ STAT_MAX_HEALTH ];
     BG_AddUpgradeToInventory( UP_MEDKIT, ent->client->ps.stats );
    }
  }

  if( give_all || Q_stricmpn( name, "funds", 5 ) == 0 )
  {
    int credits = give_all ? HUMAN_MAX_CREDITS : atoi( name + 6 );
    G_AddCreditToClient( ent->client, credits, qtrue );
  }

  if( give_all || Q_stricmp( name, "stamina" ) == 0 )
    ent->client->ps.stats[ STAT_STAMINA ] = MAX_STAMINA;

  if( Q_stricmp( name, "poison" ) == 0 )
  {
    ent->client->ps.stats[ STAT_STATE ] |= SS_BOOSTED;
    ent->client->lastBoostedTime = level.time;
  }

  if( give_all || Q_stricmp( name, "ammo" ) == 0 )
  {
    int maxAmmo, maxClips;
    gclient_t *client = ent->client;

    if( client->ps.weapon != WP_ALEVEL3_UPG &&
        BG_FindInfinteAmmoForWeapon( client->ps.weapon ) )
      return;

    BG_FindAmmoForWeapon( client->ps.weapon, &maxAmmo, &maxClips );

    if( BG_FindUsesEnergyForWeapon( client->ps.weapon ) &&
        BG_InventoryContainsUpgrade( UP_BATTPACK, client->ps.stats ) )
      maxAmmo = (int)( (float)maxAmmo * BATTPACK_MODIFIER );

    BG_PackAmmoArray( client->ps.weapon, client->ps.ammo, client->ps.powerups, maxAmmo, maxClips );
  }
}


/*
==================
Cmd_God_f

Sets client to godmode

argv(0) god
==================
*/
void Cmd_God_f( gentity_t *ent )
{
  char  *msg;

 if( !g_devmapNoGod.integer )
 {
  ent->flags ^= FL_GODMODE;

  if( !( ent->flags & FL_GODMODE ) )
    msg = "godmode OFF\n";
  else
    msg = "godmode ON\n";
 }
 else
 {
  msg = "Godmode has been disabled.\n";
 }

  trap_SendServerCommand( ent - g_entities, va( "print \"%s\"", msg ) );
}


/*
==================
Cmd_Notarget_f

Sets client to notarget

argv(0) notarget
==================
*/
void Cmd_Notarget_f( gentity_t *ent )
{
  char  *msg;

 if( !g_devmapNoGod.integer )
 {
  ent->flags ^= FL_NOTARGET;

  if( !( ent->flags & FL_NOTARGET ) )
    msg = "notarget OFF\n";
  else
    msg = "notarget ON\n";
 }
 else
 {
  msg = "Godmode has been disabled.\n";
 }

  trap_SendServerCommand( ent - g_entities, va( "print \"%s\"", msg ) );
}


/*
==================
Cmd_Noclip_f

argv(0) noclip
==================
*/
void Cmd_Noclip_f( gentity_t *ent )
{
  char  *msg;

 if( !g_devmapNoGod.integer )
 {
  if( ent->client->noclip )
    msg = "noclip OFF\n";
  else
    msg = "noclip ON\n";

  ent->client->noclip = !ent->client->noclip;
 } 
 else
 {
  msg = "Godmode has been disabled.\n";
 }

  trap_SendServerCommand( ent - g_entities, va( "print \"%s\"", msg ) );
}


/*
==================
Cmd_LevelShot_f

This is just to help generate the level pictures
for the menus.  It goes to the intermission immediately
and sends over a command to the client to resize the view,
hide the scoreboard, and take a special screenshot
==================
*/
void Cmd_LevelShot_f( gentity_t *ent )
{
  BeginIntermission( );
  trap_SendServerCommand( ent - g_entities, "clientLevelShot" );
}

/*
=================
Cmd_Kill_f
=================
*/
void Cmd_Kill_f( gentity_t *ent )
{
  if( ent->client->ps.stats[ STAT_STATE ] & SS_INFESTING )
    return;

  if( ent->client->ps.stats[ STAT_STATE ] & SS_HOVELING )
  {
    trap_SendServerCommand( ent-g_entities, "print \"Leave the hovel first (use your destroy key)\n\"" );
    return;
  }

  if( g_cheats.integer )
  {
    ent->flags &= ~FL_GODMODE;
    ent->client->ps.stats[ STAT_HEALTH ] = ent->health = 0;
    player_die( ent, ent, ent, 100000, MOD_SUICIDE );
  }
  else
  {
    if( ent->suicideTime == 0 )
    {
      trap_SendServerCommand( ent-g_entities, "print \"You will suicide in 20 seconds\n\"" );
      ent->suicideTime = level.time + 20000;
    }
    else if( ent->suicideTime > level.time )
    {
      trap_SendServerCommand( ent-g_entities, "print \"Suicide canceled\n\"" );
      ent->suicideTime = 0;
    }
  }
}

/*
==================
G_LeaveTeam
==================
*/
void G_LeaveTeam( gentity_t *self )
{
  pTeam_t   team = self->client->pers.teamSelection;
  gentity_t *ent;
  int       i;

  if( team == PTE_ALIENS )
    G_RemoveFromSpawnQueue( &level.alienSpawnQueue, self->client->ps.clientNum );
  else if( team == PTE_HUMANS )
    G_RemoveFromSpawnQueue( &level.humanSpawnQueue, self->client->ps.clientNum );
  else
  {
    if( self->client->sess.spectatorState == SPECTATOR_FOLLOW )
    {
      G_StopFollowing( self );
    }
    return;
  }
  
  // Cancel pending suicides
  self->suicideTime = 0;

  // stop any following clients
  G_StopFromFollowing( self );

  for( i = 0; i < level.num_entities; i++ )
  {
    ent = &g_entities[ i ];
    if( !ent->inuse )
      continue;

    // clean up projectiles
    if( ent->s.eType == ET_MISSILE && ent->r.ownerNum == self->s.number )
      G_FreeEntity( ent );
    if( ent->client && ent->client->pers.connected == CON_CONNECTED )
    {
      // cure poison
      if( ent->client->ps.stats[ STAT_STATE ] & SS_POISONCLOUDED &&
          ent->client->lastPoisonCloudedClient == self )
        ent->client->ps.stats[ STAT_STATE ] &= ~SS_POISONCLOUDED;
      if( ent->client->ps.stats[ STAT_STATE ] & SS_POISONED &&
          ent->client->lastPoisonClient == self )
        ent->client->ps.stats[ STAT_STATE ] &= ~SS_POISONED;
    }
  }
}

/*
=================
G_ChangeTeam
=================
*/
void G_ChangeTeam( gentity_t *ent, pTeam_t newTeam )
{
  pTeam_t oldTeam = ent->client->pers.teamSelection;
  qboolean isFixingImbalance=qfalse;
 
  if( oldTeam == newTeam )
    return;

  G_LeaveTeam( ent );
  ent->client->pers.teamSelection = newTeam;

  // G_LeaveTeam() calls G_StopFollowing() which sets spec mode to free. 
  // Undo that in this case, or else people can freespec while in the spawn queue on their new team
  if( newTeam != PTE_NONE )
  {
    ent->client->sess.spectatorState = SPECTATOR_LOCKED;
  }
  
  
  if ( ( level.numAlienClients - level.numHumanClients > 2 && oldTeam==PTE_ALIENS && newTeam == PTE_HUMANS && level.numHumanSpawns>0 ) ||
       ( level.numHumanClients - level.numAlienClients > 2 && oldTeam==PTE_HUMANS && newTeam == PTE_ALIENS  && level.numAlienSpawns>0 ) ) 
  {
    isFixingImbalance=qtrue;
  }

  // under certain circumstances, clients can keep their kills and credits
  // when switching teams
  if( G_admin_permission( ent, ADMF_TEAMCHANGEFREE ) ||
    ( g_teamImbalanceWarnings.integer && isFixingImbalance ) ||
    ( ( oldTeam == PTE_HUMANS || oldTeam == PTE_ALIENS )
    && ( level.time - ent->client->pers.teamChangeTime ) > 60000 ) )
  {
    if( oldTeam == PTE_ALIENS )
      ent->client->pers.credit *= (float)FREEKILL_HUMAN / FREEKILL_ALIEN;
    else if( newTeam == PTE_ALIENS )
      ent->client->pers.credit *= (float)FREEKILL_ALIEN / FREEKILL_HUMAN;
  }
  else
  {
    ent->client->pers.credit = 0;
    ent->client->pers.score = 0;
  }
  
  ent->client->ps.persistant[ PERS_KILLED ] = 0;
  ent->client->pers.statscounters.kills = 0;
  ent->client->pers.statscounters.structskilled = 0;
  ent->client->pers.statscounters.assists = 0;
  ent->client->pers.statscounters.repairspoisons = 0;
  ent->client->pers.statscounters.headshots = 0;
  ent->client->pers.statscounters.hits = 0;
  ent->client->pers.statscounters.hitslocational = 0;
  ent->client->pers.statscounters.deaths = 0;
  ent->client->pers.statscounters.feeds = 0;
  ent->client->pers.statscounters.suicides = 0;
  ent->client->pers.statscounters.teamkills = 0;
  ent->client->pers.statscounters.dmgdone = 0;
  ent->client->pers.statscounters.structdmgdone = 0;
  ent->client->pers.statscounters.ffdmgdone = 0;
  ent->client->pers.statscounters.structsbuilt = 0;
  ent->client->pers.statscounters.timealive = 0;
  ent->client->pers.statscounters.timeinbase = 0;
  ent->client->pers.statscounters.dretchbasytime = 0;
  ent->client->pers.statscounters.jetpackusewallwalkusetime = 0;

  if( G_admin_permission( ent, ADMF_DBUILDER ) )
  {
    if( !ent->client->pers.designatedBuilder )
    {
      ent->client->pers.designatedBuilder = qtrue;
      trap_SendServerCommand( ent-g_entities, 
        "print \"Your designation has been restored\n\"" );
    }
  }
  else if( ent->client->pers.designatedBuilder )
  {
    ent->client->pers.designatedBuilder = qfalse;
    trap_SendServerCommand( ent-g_entities, 
     "print \"You have lost designation due to teamchange\n\"" );
  }

  ent->client->pers.classSelection = PCL_NONE;
  ClientSpawn( ent, NULL, NULL, NULL );

  ent->client->pers.joinedATeam = qtrue;
  ent->client->pers.teamChangeTime = level.time;

  //update ClientInfo
  ClientUserinfoChanged( ent->client->ps.clientNum );
  G_CheckDBProtection( );
}

/*
=================
Cmd_Team_f
=================
*/
void Cmd_Team_f( gentity_t *ent )
{
  pTeam_t team;
  pTeam_t oldteam = ent->client->pers.teamSelection;
  char    s[ MAX_TOKEN_CHARS ];
  char buf[ MAX_STRING_CHARS ];
  qboolean force = G_admin_permission(ent, ADMF_FORCETEAMCHANGE);
  int     aliens = level.numAlienClients;
  int     humans = level.numHumanClients;

  //ROTAXfun
  char    safe[ MAX_TOKEN_CHARS ];

  // stop team join spam
  if( level.time - ent->client->pers.teamChangeTime < 1000 )
    return;

  if( oldteam == PTE_ALIENS )
    aliens--;
  else if( oldteam == PTE_HUMANS )
    humans--;

  // do warm up
  if( g_doWarmup.integer && g_warmupMode.integer == 1 &&
      level.time - level.startTime < g_warmup.integer * 1000 )
  {
    trap_SendServerCommand( ent - g_entities, va( "print \"team: you can't join"
      " a team during warm up (%d seconds remaining)\n\"",
      g_warmup.integer - ( level.time - level.startTime ) / 1000 ) );
    return;
  }

  trap_Argv( 1, s, sizeof( s ) );
  
  //ROTAXfun
  trap_Argv( 2, safe, sizeof( safe ) );
  if( !Q_stricmp( safe, "safe" ) )
    force = 0;

  if( !strlen( s ) )
  {
    trap_SendServerCommand( ent-g_entities, va("print \"team: %i\n\"",
      oldteam ) );
    return;
  }

  if( Q_stricmp( s, "spectate" ) ){
    if(G_admin_level(ent)<g_minLevelToJoinTeam.integer){
        trap_SendServerCommand( ent-g_entities,"print \"Sorry, but your admin level is only permitted to spectate.\n\"" ); 
        return;
    }
  }
  
  if( !Q_stricmp( s, "spectate" ) )
    team = PTE_NONE;
  else if( level.demoState == DS_PLAYBACK )//rotax
  {
    trap_SendServerCommand( ent-g_entities, "print \"You cannot join a team "
      "while a demo is being played\n\"" );
    return;
  }
  else if( !force && ent->client->pers.teamSelection == PTE_NONE &&
           g_maxGameClients.integer && level.numPlayingClients >=
           g_maxGameClients.integer )
  {
    trap_SendServerCommand( ent - g_entities, va( "print \"The maximum number "
      "of playing clients has been reached (g_maxGameClients = %i)\n\"",
      g_maxGameClients.integer ) );
    return;
  }
  else if( !Q_stricmp( s, "aliens" ) )
  {
    if( g_forceAutoSelect.integer && !G_admin_permission(ent, ADMF_FORCETEAMCHANGE) )
    {
      trap_SendServerCommand( ent-g_entities, "print \"You can only join teams using autoselect\n\"" );
      return;
    }

    if( level.alienTeamLocked && !force )
    {
      trap_SendServerCommand( ent-g_entities,
        va( "print \"Alien team has been ^1LOCKED\n\"" ) );
      return; 
    }
    else if( level.humanTeamLocked )
    {
      // if only one team has been locked, let people join the other
      // regardless of balance
      force = qtrue;
    }

    if( !force && g_teamForceBalance.integer && aliens > humans )
    {
      G_TriggerMenu( ent - g_entities, MN_A_TEAMFULL );
      return;
    }
    

    team = PTE_ALIENS;
  }
  else if( !Q_stricmp( s, "humans" ) )
  {
    if( g_forceAutoSelect.integer && !G_admin_permission(ent, ADMF_FORCETEAMCHANGE) )
    {
      trap_SendServerCommand( ent-g_entities, "print \"You can only join teams using autoselect\n\"" );
      return;
    }

    if( level.humanTeamLocked && !force )
    {
      trap_SendServerCommand( ent-g_entities,
        va( "print \"Human team has been ^1LOCKED\n\"" ) );
      return; 
    }
    else if( level.alienTeamLocked )
    {
      // if only one team has been locked, let people join the other
      // regardless of balance
      force = qtrue;
    }

    if( !force && g_teamForceBalance.integer && humans > aliens )
    {
      G_TriggerMenu( ent - g_entities, MN_H_TEAMFULL );
      return;
    }

    team = PTE_HUMANS;
  }
  else if( !Q_stricmp( s, "auto" ) )
  {
    if( level.humanTeamLocked && level.alienTeamLocked )
      team = PTE_NONE;
    else if( humans > aliens )
      team = PTE_ALIENS;
    else if( humans < aliens )
      team = PTE_HUMANS;
    else
      team = PTE_ALIENS + ( rand( ) % 2 );

    if( team == PTE_ALIENS && level.alienTeamLocked )
      team = PTE_HUMANS;
    else if( team == PTE_HUMANS && level.humanTeamLocked )
      team = PTE_ALIENS;
  }
  else
  {
    trap_SendServerCommand( ent-g_entities, va( "print \"Unknown team: %s\n\"", s ) );
    return;
  }

  // stop team join spam
  if( oldteam == team )
    return;

  //guard against build timer exploit
  if( oldteam != PTE_NONE && ent->client->sess.sessionTeam != TEAM_SPECTATOR &&
     ( ent->client->ps.stats[ STAT_PCLASS ] == PCL_ALIEN_BUILDER0 ||
       ent->client->ps.stats[ STAT_PCLASS ] == PCL_ALIEN_BUILDER0_UPG ||
       BG_InventoryContainsWeapon( WP_HBUILD, ent->client->ps.stats ) ||
       BG_InventoryContainsWeapon( WP_HBUILD2, ent->client->ps.stats ) ) &&
      ent->client->ps.stats[ STAT_MISC ] > 0 )
  {
    trap_SendServerCommand( ent-g_entities,
        va( "print \"You cannot change teams until build timer expires\n\"" ) );
    return;
  }

   if (team != PTE_NONE)
   {
     char  namebuff[32];
 
     Q_strncpyz (namebuff, ent->client->pers.netname, sizeof(namebuff));
     Q_CleanStr (namebuff);
 
     if (!namebuff[0] || !Q_stricmp (namebuff, "UnnamedPlayer"))
     {
       trap_SendServerCommand( ent-g_entities, va( "print \"Please set your player name before joining a team. Press ESC and use the Options / Game menu  or use /name in the console\n\"") );
       return;
     }
   }
 

  G_ChangeTeam( ent, team );
   
   

   if( team == PTE_ALIENS ) {
     if ( oldteam == PTE_HUMANS )
       Com_sprintf( buf, sizeof( buf ), "%s^7 abandoned ^4humans^7 and joined the ^1aliens^7.", ent->client->pers.netname );//rotax
     else
       Com_sprintf( buf, sizeof( buf ), "%s^7 joined the ^1aliens^7.", ent->client->pers.netname );//rotax
   }
   else if( team == PTE_HUMANS ) {
     if ( oldteam == PTE_ALIENS )
       Com_sprintf( buf, sizeof( buf ), "%s^7 abandoned the ^1aliens^7 and joined the ^4humans^7.", ent->client->pers.netname );//rotax
     else
       Com_sprintf( buf, sizeof( buf ), "%s^7 joined the ^4humans^7.", ent->client->pers.netname );//rotax
   }
   else if( team == PTE_NONE ) {
     if ( oldteam == PTE_HUMANS )
       Com_sprintf( buf, sizeof( buf ), "%s^7 left the ^4humans^7.", ent->client->pers.netname );//rotax
     else
       Com_sprintf( buf, sizeof( buf ), "%s^7 left the ^1aliens^7.", ent->client->pers.netname );//rotax
   }
   trap_SendServerCommand( -1, va( "print \"%s\n\"", buf ) );
   G_LogOnlyPrintf("ClientTeam: %s\n",buf);
   
   //ROTAXfun
   ent->client->pers.creditsRewardTime = level.time;
}


/*
==================
G_Say
==================
*/
static void G_SayTo( gentity_t *ent, gentity_t *other, int mode, int color, const char *name, const char *message, const char *prefix )
{
  qboolean ignore = qfalse;
  qboolean specAllChat = qfalse;
  
  //rotaxfun
  int clientNum;

  if( !other )
    return;

  if( !other->inuse )
    return;

  if( !other->client )
    return;

  if( other->client->pers.connected != CON_CONNECTED )
    return;

  if( ( mode == SAY_TEAM || mode == SAY_ACTION_T ) && !OnSameTeam( ent, other ) )
  {
    if( other->client->pers.teamSelection != PTE_NONE )
      return;

    specAllChat = G_admin_permission( other, ADMF_SPEC_ALLCHAT );
    if( !specAllChat )
      return;

    // specs with ADMF_SPEC_ALLCHAT flag can see team chat
  }
  
  if( mode == SAY_ADMINS && !G_admin_permission( other, ADMF_ADMINCHAT) )
     return;

  if( BG_ClientListTest( &other->client->sess.ignoreList, ent-g_entities ) )
    ignore = qtrue;
  
  //rotaxfun
//  trap_SendServerCommand( other-g_entities, va( "%s \"%s%s%s%c%c%s\"",
//    ( mode == SAY_TEAM || mode == SAY_ACTION_T ) ? "tchat" : "chat",
//    ( ignore ) ? "[skipnotify]" : "",
//    ( specAllChat ) ? prefix : "",
//    name, Q_COLOR_ESCAPE, color, message ) );

  if (ent->client->pers.teamSelection == PTE_NONE)//pokud specuju, nezobrazim tento text v bublinach
    clientNum = -2;
  else
    clientNum = ent->client->ps.clientNum; 

  trap_SendServerCommand( other-g_entities, va( "%s %i %i \"%s%s%s\"\"%c%c%s\"",
    ( mode == SAY_TEAM || mode == SAY_ACTION_T ) ? "tchat" : "chat",
    clientNum,
    ent->client->pers.teamSelection,//kvuli adminum aby ve specu videli barevne tymove bubliny, jinak by nesly rozlisit (potrebuju neco, co prozradi pravej tym i kdyz nekoho followuju)
    ( ignore ) ? "[skipnotify]" : "", 
    ( specAllChat ) ? prefix : "",
    name, 
    Q_COLOR_ESCAPE, color, message )
     );

  //rotaxfun
  //update ClientInfo
  ClientUserinfoChanged( ent->client->ps.clientNum );
//trap_SendServerCommand( -1, va( "print \"%i\n\"", ent->client->ps.clientNum));
}

#define EC    "\x19"

void G_Say( gentity_t *ent, gentity_t *target, int mode, const char *chatText )
{
  int         j;
  gentity_t   *other;
  int         color;
  char  prefix[ 9 ];//rotax
  char        name[ 64 ];
  // don't let text be too long for malicious reasons
  char        text[ MAX_SAY_TEXT ];
  char        location[ 64 ];

  // Bail if the text is blank.
  if( ! chatText[0] )
     return;

  // Flood limit.  If they're talking too fast, determine that and return.
  if( g_floodMinTime.integer )
    if ( G_Flood_Limited( ent ) )
    {
      trap_SendServerCommand( ent-g_entities, "print \"Your chat is flood-limited; wait before chatting again\n\"" );
      return;
    }

  //rotax
  if (g_chatTeamPrefix.integer)
    Com_sprintf( prefix, sizeof( prefix ), marking(ent));
  else
    Com_sprintf( prefix, sizeof( prefix ), "");
  
  /*rotax 
  if (g_chatTeamPrefix.integer && ent && ent->client )
  {
    switch( ent->client->pers.teamSelection)
    {
      default:
      case PTE_NONE:
        prefix = "[S] ";
        break;

      case PTE_ALIENS:
        prefix = "[A] ";
        break;

      case PTE_HUMANS:
        prefix = "[H] ";
    }
  }
  else
    prefix = "";*/

  switch( mode )
  {
    default:
    case SAY_ALL:
      G_LogPrintf( "say: %s^7: %s^7\n", ent->client->pers.netname, chatText );
      Com_sprintf( name, sizeof( name ), "%s%s%c%c: ", prefix,
                   ent->client->pers.netname, Q_COLOR_ESCAPE, COLOR_WHITE );//rotaxfun bylo "%s%s%c%c"EC"
      color = COLOR_GREEN;
      G_DemoCommand( DC_SERVER_COMMAND, va( "chat \"%s^2%s\"", name, chatText ) );//rotax
      break;

    case SAY_TEAM:
      G_LogPrintf( "sayteam: %s%s^7: %s^7\n", prefix, ent->client->pers.netname, chatText );
      if( Team_GetLocationMsg( ent, location, sizeof( location ) ) )
        Com_sprintf( name, sizeof( name ), "(%s%c%c) (%s): ",
          ent->client->pers.netname, Q_COLOR_ESCAPE, COLOR_WHITE, location );//rotaxfun bylo EC"(%s%c%c"EC") (%s)"EC"
      else
        Com_sprintf( name, sizeof( name ), "(%s%c%c): ",
          ent->client->pers.netname, Q_COLOR_ESCAPE, COLOR_WHITE );//rotaxfun bylo EC"(%s%c%c"EC")"EC": "
      
      if( ent->client->pers.teamSelection == PTE_NONE )
        color = COLOR_YELLOW;
      else
        color = COLOR_CYAN;
      G_DemoCommand( DC_SERVER_COMMAND, va( "tchat \"%s^%c%s\"", name, color, chatText ) );//rotax
      break;

    case SAY_TELL:
      if( target && OnSameTeam( target, ent ) &&
          Team_GetLocationMsg( ent, location, sizeof( location ) ) )
        Com_sprintf( name, sizeof( name ), "[%s%c%c] (%s): ",
          ent->client->pers.netname, Q_COLOR_ESCAPE, COLOR_WHITE, location );//rotaxfun bylo EC"[%s%c%c"EC"] (%s)"EC": "
      else
        Com_sprintf( name, sizeof( name ), "[%s%c%c]: ",
          ent->client->pers.netname, Q_COLOR_ESCAPE, COLOR_WHITE );//rotaxfun bylo EC"[%s%c%c"EC"]"EC": "
      color = COLOR_MAGENTA;
      break;
      
    case SAY_ACTION:
      G_LogPrintf( "action: %s^7: %s^7\n", ent->client->pers.netname, chatText );
      Com_sprintf( name, sizeof( name ), "^2%s^7%s%s%c%c ", g_actionPrefix.string, prefix,
                   ent->client->pers.netname, Q_COLOR_ESCAPE, COLOR_WHITE );//rotaxfun bylo "^2%s^7%s%s%c%c"EC" "
      color = COLOR_WHITE;
      G_DemoCommand( DC_SERVER_COMMAND, va( "chat \"%s%s\"", name, chatText ) );//rotax
      break;

    case SAY_ACTION_T:
      G_LogPrintf( "actionteam: %s%s^7: %s^7\n", prefix, ent->client->pers.netname, chatText );
      if( Team_GetLocationMsg( ent, location, sizeof( location ) ) )
        Com_sprintf( name, sizeof( name ), "^5%s^7%s%c%c(%s) ", g_actionPrefix.string, 
          ent->client->pers.netname, Q_COLOR_ESCAPE, COLOR_WHITE, location );//rotaxfun bylo EC"^5%s^7%s%c%c"EC"(%s)"EC" "
      else
        Com_sprintf( name, sizeof( name ), "^5%s^7%s%c%c ", g_actionPrefix.string, "^5%s^7%s%c%c ",
          ent->client->pers.netname, Q_COLOR_ESCAPE, COLOR_WHITE );//rotaxfun bylo EC"^5%s^7%s%c%c"EC""EC" "
      color = COLOR_WHITE;
      G_DemoCommand( DC_SERVER_COMMAND, va( "tchat \"%s%s\"", name, chatText ) );//rotax
      break;
      
      case SAY_ADMINS:
        if( G_admin_permission( ent, ADMF_ADMINCHAT ) ) //Differentiate between inter-admin chatter and user-admin alerts
        {
         G_LogPrintf( "say_admins: [ADMIN]%s^7: %s^7\n", ( ent ) ? ent->client->pers.netname : "console", chatText );
         Com_sprintf( name, sizeof( name ), "%s[ADMIN]%s%c%c: ", prefix,
                    ( ent ) ? ent->client->pers.netname : "console", Q_COLOR_ESCAPE, COLOR_WHITE );//rotaxfun bylo "%s[ADMIN]%s%c%c"EC": "
         color = COLOR_MAGENTA;
        }
        else
        {
          G_LogPrintf( "say_admins: [PLAYER]%s^7: %s^7\n", ent->client->pers.netname, chatText );
          Com_sprintf( name, sizeof( name ), "%s[PLAYER]%s%c%c: ", prefix,
            ent->client->pers.netname, Q_COLOR_ESCAPE, COLOR_WHITE );//rotaxfun bylo "%s[PLAYER]%s%c%c"EC": "
          color = COLOR_MAGENTA;
        }
        break;
  }
  
  if( mode!=SAY_TEAM && ent && ent->client && ent->client->pers.teamSelection == PTE_NONE && G_admin_level(ent)<g_minLevelToSpecMM1.integer )
  {
    trap_SendServerCommand( ent-g_entities,va( "print \"Sorry, but your admin level may only use teamchat while spectating.\n\"") ); 
    return;
  }

  Com_sprintf( text, sizeof( text ), "%s^7", chatText );

  if( target )
  {
    G_SayTo( ent, target, mode, color, name, text, prefix );
    return;
  }
  


  // Ugly hax: if adminsayfilter is off, do the SAY first to prevent text from going out of order
  if( !g_adminSayFilter.integer )
  {
    // send it to all the apropriate clients
    for( j = 0; j < level.maxclients; j++ )
    {
      other = &g_entities[ j ];
      G_SayTo( ent, other, mode, color, name, text, prefix );
    }
  }
   
   if( g_adminParseSay.integer && ( mode== SAY_ALL || mode == SAY_TEAM ) )
   {
     if( G_admin_cmd_check ( ent, qtrue ) && g_adminSayFilter.integer ) 
     {
       return;
     }
   }

  // if it's on, do it here, where it won't happen if it was an admin command
  if( g_adminSayFilter.integer )
  {
    // send it to all the apropriate clients
    for( j = 0; j < level.maxclients; j++ )
    {
      other = &g_entities[ j ];
      G_SayTo( ent, other, mode, color, name, text, prefix );
    }
  }
  

}

static void Cmd_SayArea_f( gentity_t *ent )
{
  int    entityList[ MAX_GENTITIES ];
  int    num, i;
  int    color = COLOR_BLUE;
  const char  *prefix;
  vec3_t range = { HELMET_RANGE, HELMET_RANGE, HELMET_RANGE };
  vec3_t mins, maxs;
  char   *msg = ConcatArgs( 1 );
  char   name[ 64 ];
  
   if( g_floodMinTime.integer )
   if ( G_Flood_Limited( ent ) )
   {
    trap_SendServerCommand( ent-g_entities, "print \"Your chat is flood-limited; wait before chatting again\n\"" );
    return;
   }
  
  if (g_chatTeamPrefix.integer)
  {
    switch( ent->client->pers.teamSelection)
    {
      default:
      case PTE_NONE:
        prefix = "[S] ";
        break;

      case PTE_ALIENS:
        prefix = "[A] ";
        break;

      case PTE_HUMANS:
        prefix = "[H] ";
    }
  }
  else
    prefix = "";

  G_LogPrintf( "sayarea: %s%s^7: %s\n", prefix, ent->client->pers.netname, msg );
  Com_sprintf( name, sizeof( name ), EC"<%s%c%c"EC"> ",
    ent->client->pers.netname, Q_COLOR_ESCAPE, COLOR_WHITE );

  VectorAdd( ent->s.origin, range, maxs );
  VectorSubtract( ent->s.origin, range, mins );

  num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES );
  for( i = 0; i < num; i++ )
    G_SayTo( ent, &g_entities[ entityList[ i ] ], SAY_TEAM, color, name, msg, prefix );
  
  //Send to ADMF_SPEC_ALLCHAT candidates
  for( i = 0; i < level.maxclients; i++ )
  {
    if( (&g_entities[ i ])->client->pers.teamSelection == PTE_NONE  &&
        G_admin_permission( &g_entities[ i ], ADMF_SPEC_ALLCHAT ) )
    {
      G_SayTo( ent, &g_entities[ i ], SAY_TEAM, color, name, msg, prefix );   
    }
  }
}


/*
==================
Cmd_Say_f
==================
*/
static void Cmd_Say_f( gentity_t *ent )
{
  char    *p;
  char    *args;
  int     mode = SAY_ALL;
  int     skipargs = 0;

  args = G_SayConcatArgs( 0 );
  if( Q_stricmpn( args, "say_team ", 9 ) == 0 )
    mode = SAY_TEAM;
  if( Q_stricmpn( args, "say_admins ", 11 ) == 0 || Q_stricmpn( args, "a ", 2 ) == 0)
    mode = SAY_ADMINS;

  // support parsing /m out of say text since some people have a hard
  // time figuring out what the console is.
  if( !Q_stricmpn( args, "say /m ", 7 ) ||
      !Q_stricmpn( args, "say_team /m ", 12 ) || 
      !Q_stricmpn( args, "say /mt ", 8 ) || 
      !Q_stricmpn( args, "say_team /mt ", 13 ) )
  {
    G_PrivateMessage( ent );
    return;
  }
  
   
   if( !Q_stricmpn( args, "say /a ", 7) ||
       !Q_stricmpn( args, "say_team /a ", 12) ||
       !Q_stricmpn( args, "say /say_admins ", 16) ||
       !Q_stricmpn( args, "say_team /say_admins ", 21) )
   {
       mode = SAY_ADMINS;
       skipargs=1;
   }
   
   if( mode == SAY_ADMINS)  
   if(!G_admin_permission( ent, ADMF_ADMINCHAT ) )
   {
     if( !g_publicSayadmins.integer )
     {
      ADMP( "Sorry, but public use of say_admins has been disabled.\n" );
      return;
     }
     else
     {
       ADMP( "Your message has been sent to any available admins and to the server logs.\n" );
     }
   }
   

  if(!Q_stricmpn( args, "say /me ", 8 ) )
  {
   if( g_actionPrefix.string[0] ) 
   { 
    mode = SAY_ACTION;
    skipargs=1;
   } else return;
  }
  else if(!Q_stricmpn( args, "say_team /me ", 13 ) )
  {
   if( g_actionPrefix.string[0] ) 
   { 
    mode = SAY_ACTION_T;
    skipargs=1;
   } else return;
  }
  else if( !Q_stricmpn( args, "me ", 3 ) )
  {
   if( g_actionPrefix.string[0] ) 
   { 
    mode = SAY_ACTION;
   } else return;
  }
  else if( !Q_stricmpn( args, "me_team ", 8 ) )
  {
   if( g_actionPrefix.string[0] ) 
   { 
    mode = SAY_ACTION_T;
   } else return;
  }


  if( g_allowShare.integer )
  {
    args = G_SayConcatArgs(0);
    if( !Q_stricmpn( args, "say /share", 10 ) ||
      !Q_stricmpn( args, "say_team /share", 15 ) )
    {
      Cmd_Share_f( ent );
      return;
    }
   if( !Q_stricmpn( args, "say /donate", 11 ) ||
      !Q_stricmpn( args, "say_team /donate", 16 ) )
    {
      Cmd_Donate_f( ent );
      return;
    }
  }


  if( trap_Argc( ) < 2 )
    return;

  p = G_SayConcatArgs( 1 + skipargs );

  G_Say( ent, NULL, mode, p );
}

/*
==================
Cmd_Tell_f
==================
*/
static void Cmd_Tell_f( gentity_t *ent )
{
  int     targetNum;
  gentity_t *target;
  char    *p;
  char    arg[MAX_TOKEN_CHARS];

  if( trap_Argc( ) < 2 )
    return;

  trap_Argv( 1, arg, sizeof( arg ) );
  targetNum = atoi( arg );

  if( targetNum < 0 || targetNum >= level.maxclients )
    return;

  target = &g_entities[ targetNum ];
  if( !target || !target->inuse || !target->client )
    return;

  p = ConcatArgs( 2 );

  G_LogPrintf( "tell: %s to %s: %s\n", ent->client->pers.netname, target->client->pers.netname, p );
  G_Say( ent, target, SAY_TELL, p );
  // don't tell to the player self if it was already directed to this player
  // also don't send the chat back to a bot
  if( ent != target )
    G_Say( ent, ent, SAY_TELL, p );
}

/*
==================
Cmd_Where_f
==================
*/
void Cmd_Where_f( gentity_t *ent )
{
  trap_SendServerCommand( ent-g_entities, va( "print \"%s\n\"", vtos( ent->s.origin ) ) );
}

/*
==================
Cmd_CallVote_f
==================
*/
void Cmd_CallVote_f( gentity_t *ent )
{
  int   i;
  char  arg1[ MAX_STRING_TOKENS ];
  char  arg2[ MAX_STRING_TOKENS ];
  int   clientNum = -1;
  char  name[ MAX_NETNAME ];
  char *arg1plus;
  char *arg2plus;
  char nullstring[] = "";
  char  message[ MAX_STRING_CHARS ];
  char targetname[ MAX_NAME_LENGTH] = "";
  char reason[ MAX_STRING_CHARS ] = "";
  char *ptr = NULL;
  gentity_t *arma_ent;//ROTAX;

  arg1plus = G_SayConcatArgs( 1 );
  arg2plus = G_SayConcatArgs( 2 );

  if( !g_allowVote.integer )
  {
    trap_SendServerCommand( ent-g_entities, "print \"Voting not allowed here\n\"" );
    return;
  }
  
  // Flood limit.  If they're talking too fast, determine that and return.
  if( g_floodMinTime.integer )
    if ( G_Flood_Limited( ent ) )
    {
      trap_SendServerCommand( ent-g_entities, "print \"Your /callvote attempt is flood-limited; wait before chatting again\n\"" );
      return;
    }

  if( g_voteMinTime.integer
    && ent->client->pers.firstConnect 
    && level.time - ent->client->pers.enterTime < g_voteMinTime.integer * 1000
    && !G_admin_permission( ent, ADMF_NO_VOTE_LIMIT ) 
    && (level.numPlayingClients > 0 && level.numConnectedClients>1) )
  {
    trap_SendServerCommand( ent-g_entities, va(
      "print \"You must wait %d seconds after connecting before calling a vote\n\"",
      g_voteMinTime.integer ) );
    return;
  }

  if( level.voteTime )
  {
    trap_SendServerCommand( ent-g_entities, "print \"A vote is already in progress\n\"" );
    return;
  }

  if( g_voteLimit.integer > 0
    && ent->client->pers.voteCount >= g_voteLimit.integer
    && !G_admin_permission( ent, ADMF_NO_VOTE_LIMIT ) )
  {
    trap_SendServerCommand( ent-g_entities, va(
      "print \"You have already called the maximum number of votes (%d)\n\"",
      g_voteLimit.integer ) );
    return;
  }
  
  if( ent->client->pers.muted )
  {
    trap_SendServerCommand( ent - g_entities,
      "print \"You are muted and cannot call votes\n\"" );
    return;
  }

  // make sure it is a valid command to vote on
  trap_Argv( 1, arg1, sizeof( arg1 ) );
  trap_Argv( 2, arg2, sizeof( arg2 ) );

  if( strchr( arg1, ';' ) || strchr( arg2, ';' ) )
  {
    trap_SendServerCommand( ent-g_entities, "print \"Invalid vote string\n\"" );
    return;
  }

  // if there is still a vote to be executed
  if( level.voteExecuteTime )
  {
    if( !Q_stricmp( level.voteString, "map_restart" ) )
    {
      G_admin_maplog_result( "r" );
    }
    else if( !Q_stricmpn( level.voteString, "map", 3 ) )
    {
      G_admin_maplog_result( "m" );
    }

    level.voteExecuteTime = 0;
    trap_SendConsoleCommand( EXEC_APPEND, va( "%s\n", level.voteString ) );
  }
  
  level.votePassThreshold=50;
  
  ptr = strstr(arg1plus, " -");
  if( ptr )
  {
    *ptr = '\0';
    ptr+=2; 

    if( *ptr == 'r' || *ptr=='R' )
    {
      ptr++;
      while( *ptr == ' ' )
        ptr++;
      strcpy(reason, ptr);
    }
    else
    {
      trap_SendServerCommand( ent-g_entities, "print \"callvote: Warning: invalid argument specified \n\"" );
    }
  }

  // detect clientNum for partial name match votes
  if( !Q_stricmp( arg1, "kick" ) ||
    !Q_stricmp( arg1, "mute" ) ||
    !Q_stricmp( arg1, "unmute" ) )
  {
    int clientNums[ MAX_CLIENTS ] = { -1 };
    int numMatches=0;
    char err[ MAX_STRING_CHARS ] = "";
    
    Q_strncpyz(targetname, arg2plus, sizeof(targetname));
    ptr = strstr(targetname, " -");
    if( ptr )
      *ptr = '\0';
    
    if( g_requireVoteReasons.integer && !G_admin_permission( ent, ADMF_UNACCOUNTABLE ) && !Q_stricmp( arg1, "kick" ) && reason[ 0 ]=='\0' )
    {
       trap_SendServerCommand( ent-g_entities, "print \"callvote: You must specify a reason. Use /callvote kick [player] -r [reason] \n\"" );
       return;
    }
    
    if( !targetname[ 0 ] )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callvote: no target\n\"" );
      return;
    }

    numMatches = G_ClientNumbersFromString( targetname, clientNums );
    if( numMatches == 1 )
    {
      // there was only one partial name match
      clientNum = clientNums[ 0 ]; 
    }
    else
    {
      // look for an exact name match (sets clientNum to -1 if it fails) 
      clientNum = G_ClientNumberFromString( ent, targetname );
    }
    
    if( clientNum==-1  && numMatches > 1 ) 
    {
      G_MatchOnePlayer( clientNums, err, sizeof( err ) );
      ADMP( va( "^3callvote: ^7%s\n", err ) );
      return;
    }
    
    if( clientNum != -1 &&
      level.clients[ clientNum ].pers.connected == CON_DISCONNECTED )
    {
      clientNum = -1;
    }

    if( clientNum != -1 )
    {
      Q_strncpyz( name, level.clients[ clientNum ].pers.netname,
        sizeof( name ) );
      Q_CleanStr( name );
      if ( G_admin_permission ( &g_entities[ clientNum ], ADMF_IMMUNITY ) )
      {
    char reasonprint[ MAX_STRING_CHARS ] = "";

    if( reason[ 0 ] != '\0' )
      Com_sprintf(reasonprint, sizeof(reasonprint), "With reason: %s", reason);

        Com_sprintf( message, sizeof( message ), "%s^7 attempted /callvote %s %s on immune admin %s^7 %s^7",
          ent->client->pers.netname, arg1, targetname, g_entities[ clientNum ].client->pers.netname, reasonprint );
      }
    }
    else
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callvote: invalid player\n\"" );
      return;
    }
  }
 
  if( !Q_stricmp( arg1, "kick" ) )
  {
    if( G_admin_permission( &g_entities[ clientNum ], ADMF_IMMUNITY ) )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callvote: admin is immune from vote kick\n\"" );
      G_AdminsPrintf("%s\n",message);
      return;
    }

    // use ip in case this player disconnects before the vote ends
    Com_sprintf( level.voteString, sizeof( level.voteString ),
      "!ban %s \"%s\" vote kick", level.clients[ clientNum ].pers.ip,
      g_adminTempBan.string );
    if ( reason[0]!='\0' )
      Q_strcat( level.voteString, sizeof( level.voteDisplayString ), va( "(%s^7)", reason ) );
    Com_sprintf( level.voteDisplayString, sizeof( level.voteDisplayString ),
      "Kick player \'%s\'", name );
  }
  else if( !Q_stricmp( arg1, "mute" ) )
  {
    if( level.clients[ clientNum ].pers.muted )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callvote: player is already muted\n\"" );
      return;
    }

    if( G_admin_permission( &g_entities[ clientNum ], ADMF_IMMUNITY ) )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callvote: admin is immune from vote mute\n\"" );
      G_AdminsPrintf("%s\n",message);
      return;
    }
    Com_sprintf( level.voteString, sizeof( level.voteString ),
      "!mute %i", clientNum );
    Com_sprintf( level.voteDisplayString, sizeof( level.voteDisplayString ),
      "Mute player \'%s\'", name );
  }
  else if( !Q_stricmp( arg1, "unmute" ) )
  {
    if( !level.clients[ clientNum ].pers.muted )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callvote: player is not currently muted\n\"" );
      return;
    }
    Com_sprintf( level.voteString, sizeof( level.voteString ),
      "!unmute %i", clientNum );
    Com_sprintf( level.voteDisplayString, sizeof( level.voteDisplayString ),
      "Un-Mute player \'%s\'", name );
  }
  else if( !Q_stricmp( arg1, "map_restart" ) )
  {
    if( g_mapvoteMaxTime.integer 
      && (( level.time - level.startTime ) >= g_mapvoteMaxTime.integer * 1000 )
      && !G_admin_permission( ent, ADMF_NO_VOTE_LIMIT ) 
      && (level.numPlayingClients > 0 && level.numConnectedClients>1) )
    {
       trap_SendServerCommand( ent-g_entities, va(
         "print \"You cannot call for a restart after %d seconds\n\"",
         g_mapvoteMaxTime.integer ) );
       return;
    }
    Com_sprintf( level.voteString, sizeof( level.voteString ), "%s", arg1 );
    Com_sprintf( level.voteDisplayString,
        sizeof( level.voteDisplayString ), "Restart current map" );
    level.votePassThreshold = g_mapVotesPercent.integer;
  }
  else if( !Q_stricmp( arg1, "map" ) )
  {
    if( g_mapvoteMaxTime.integer 
      && (( level.time - level.startTime ) >= g_mapvoteMaxTime.integer * 1000 )
      && !G_admin_permission( ent, ADMF_NO_VOTE_LIMIT ) 
      && (level.numPlayingClients > 0 && level.numConnectedClients>1) )
    {
       trap_SendServerCommand( ent-g_entities, va(
         "print \"You cannot call for a mapchange after %d seconds\n\"",
         g_mapvoteMaxTime.integer ) );
       return;
    }
  
    if( !trap_FS_FOpenFile( va( "maps/%s.bsp", arg2 ), NULL, FS_READ ) )
    {
      trap_SendServerCommand( ent - g_entities, va( "print \"callvote: "
        "'maps/%s.bsp' could not be found on the server\n\"", arg2 ) );
      return;
    }
    Com_sprintf( level.voteString, sizeof( level.voteString ), "%s %s", arg1, arg2 );
    Com_sprintf( level.voteDisplayString,
        sizeof( level.voteDisplayString ), "Change to map '%s'", arg2 );
    level.votePassThreshold = g_mapVotesPercent.integer;
  }
  else if( !Q_stricmp( arg1, "nextmap" ) )
  {
    if( G_MapExists( g_nextMap.string ) )
    {
      trap_SendServerCommand( ent - g_entities, va( "print \"callvote: "
        "the next map is already set to '%s^7'\n\"", g_nextMap.string ) );
      return;
    }

    if( !trap_FS_FOpenFile( va( "maps/%s.bsp", arg2 ), NULL, FS_READ ) )
    {
      trap_SendServerCommand( ent - g_entities, va( "print \"callvote: "
        "'maps/%s^7.bsp' could not be found on the server\n\"", arg2 ) );
      return;
    }
    Com_sprintf( level.voteString, sizeof( level.voteString ),
      "set g_nextMap %s", arg2 );
    Com_sprintf( level.voteDisplayString,
        sizeof( level.voteDisplayString ), "Set the ^1next^7 map to '%s^7'", arg2 );//ROTAX
    level.votePassThreshold = g_mapVotesPercent.integer;
  }
  else if( !Q_stricmp( arg1, "draw" ) )
  {
    Com_sprintf( level.voteString, sizeof( level.voteString ), "evacuation" );
    Com_sprintf( level.voteDisplayString, sizeof( level.voteDisplayString ),
        "End match in a draw" );
    level.votePassThreshold = g_mapVotesPercent.integer;
  }
   else if( !Q_stricmp( arg1, "poll" ) )
    {
      if( arg2plus[ 0 ] == '\0' )
      {
        trap_SendServerCommand( ent-g_entities, "print \"callvote: You forgot to specify what people should vote on.\n\"" );
        return;
      }
      Com_sprintf( level.voteString, sizeof( level.voteString ), nullstring);
      Com_sprintf( level.voteDisplayString,
          sizeof( level.voteDisplayString ), "[Poll] \'%s^7'", arg2plus );//ROTAX added ^7
   }
   else if( !Q_stricmp( arg1, "sudden_death" ) ||
     !Q_stricmp( arg1, "suddendeath" ) ||
     !Q_stricmp( arg1, "sd" ))//rotax pridano sd
   {
     if(!g_suddenDeathVotePercent.integer)
     {
       trap_SendServerCommand( ent-g_entities, "print \"Sudden Death votes have been disabled\n\"" );
       return;
     } 
     else if( g_suddenDeath.integer ) 
     {
      trap_SendServerCommand( ent - g_entities, va( "print \"callvote: Sudden Death has already begun\n\"") );
      return;
     }
     else if( G_TimeTilSuddenDeath() <= g_suddenDeathVoteDelay.integer * 1000 )
     {
      trap_SendServerCommand( ent - g_entities, va( "print \"callvote: Sudden Death is already immenent\n\"") );
      return;
     }
    else 
     {
       level.votePassThreshold = g_suddenDeathVotePercent.integer;
       Com_sprintf( level.voteString, sizeof( level.voteString ), "suddendeath" );
       Com_sprintf( level.voteDisplayString,
           sizeof( level.voteDisplayString ), "Begin sudden death" );

       if( g_suddenDeathVoteDelay.integer )
         Q_strcat( level.voteDisplayString, sizeof( level.voteDisplayString ), va( " in %d seconds", g_suddenDeathVoteDelay.integer ) );

     }
   }
   else if( !Q_stricmp( arg1, "armageddon" ) ||
     !Q_stricmp( arg1, "arma" ))//ROTAX
   {
     if(!g_rotaxARMAGEDDON.integer)
     {
       trap_SendServerCommand( ent-g_entities, "print \"callvote: Armageddon is disabled.\n\"" );
       return;
     } 	  
     else if(armageddon == 1)
     {
       trap_SendServerCommand( ent-g_entities, "print \"callvote: Armageddon is right NOW!\n\"" );
       return;
     }
     else if (( level.time - level.startTime ) < 1200000 && !G_admin_permission( ent, ADMF_CAN_PERM_BAN ) )
     {
       trap_SendServerCommand( ent-g_entities, "print \"callvote: Armageddon can be called 20 minutes after start.\n\"" );
       return;
     }
     else 
     {
      //projdu vsechny entity
      int je_co_nicit = 0;
      arma_ent = &g_entities[ 0 ];

      for ( i = 1, arma_ent = g_entities + i; i < level.num_entities; i++, arma_ent++ )
      {
        if( arma_ent->s.eType != ET_BUILDABLE )
          continue;

        //pokud zije alespon jedna budova, krome spawnu atd., tak ok
        if( ((arma_ent->s.modelindex != BA_A_OVERMIND && arma_ent->s.modelindex != BA_H_ARMOURY && arma_ent->s.modelindex != BA_H_MEDISTAT && arma_ent->s.modelindex != BA_H_REACTOR && arma_ent->s.modelindex != BA_H_REPEATER)) && arma_ent->health > 0 )
        {
          je_co_nicit = 1;
          break;
        }
      }

      //pokud je co nicit
      if (je_co_nicit == 1)
      {
        Com_sprintf( level.voteString, sizeof( level.voteString ), "armageddon" );
        Com_sprintf( level.voteDisplayString,
            sizeof( level.voteDisplayString ), "Summon armageddon?" );
        level.votePassThreshold = g_mapVotesPercent.integer;
      }
      else
      {
        trap_SendServerCommand( ent-g_entities, "print \"callvote: Everything is already destroyed...\n\"" );
        return;
      }
     }
   }
   else if( !Q_stricmp( arg1, "delay" ) )//ROTAXfun
   {
     if( g_suddenDeath.integer ) 
     {
      trap_SendServerCommand( ent - g_entities, va( "print \"callvote: Sudden Death has already begun\n\"") );
      return;
     }
     else if( G_TimeTilSuddenDeath() <= g_suddenDeathVoteDelay.integer * 1000 )
     {
      trap_SendServerCommand( ent - g_entities, va( "print \"callvote: Sudden Death is already immenent\n\"") );
      return;
     }
     else 
     {
       level.votePassThreshold = g_suddenDeathVotePercent.integer;
       Com_sprintf( level.voteString, sizeof( level.voteString ), "delay" );
       Com_sprintf( level.voteDisplayString,
           sizeof( level.voteDisplayString ), "Delay Sudden Death by 10 minutes" );
     }
   }
  else
  {
    trap_SendServerCommand( ent-g_entities, "print \"Invalid vote string\n\"" );
    trap_SendServerCommand( ent-g_entities, "print \"Valid vote commands are: "
      "map, map_restart, nextmap, kick, mute, unmute, poll, sudden_death (sd), armageddon (arma), delay.\n" );//rotax
    return;
  }
  
  if( level.votePassThreshold!=50 )
  {
    Q_strcat( level.voteDisplayString, sizeof( level.voteDisplayString ), va( " (Needs > %d percent)", level.votePassThreshold ) );
  }
  else if(!Q_stricmp( level.voteString, "armageddon" ))//ROTAX
  {
    Q_strcat( level.voteDisplayString, sizeof( level.voteDisplayString ), " (Needs > 70 percent)" );
  }

  if ( reason[0]!='\0' )
    Q_strcat( level.voteDisplayString, sizeof( level.voteDisplayString ), va( " Reason: '%s^7'", reason ) );
  
  //rotax
  //trap_SendServerCommand( -1, va( "print \"%s" S_COLOR_WHITE
  //       " called a vote: %s\n\"", ent->client->pers.netname, level.voteDisplayString ) );
  Com_sprintf( level.voteDisplayString, sizeof( level.voteDisplayString ),
      va ("%s %s", marking_vote(ent), level.voteDisplayString ) );

  trap_SendServerCommand( -1, va( "print \"^2____VOTE____^7 %s\n\"", level.voteDisplayString ) );
  
  G_LogPrintf("Vote: %s^7 called a vote: %s^7\n", ent->client->pers.netname, level.voteDisplayString );
  
  //rotax
  //Q_strcat( level.voteDisplayString, sizeof( level.voteDisplayString ), va( " Called by: '%s^7'", ent->client->pers.netname ) );

  ent->client->pers.voteCount++;

  // start the voting, the caller autoamtically votes yes
  level.voteTime = level.time;
  level.voteNo = 0;

  for( i = 0 ; i < level.maxclients ; i++ )
    level.clients[i].ps.eFlags &= ~EF_VOTED;

  if( !Q_stricmp( arg1, "poll" ) )
  {
    level.voteYes = 0;
  }
  else
  {
   level.voteYes = 1;
   ent->client->ps.eFlags |= EF_VOTED;
  }

  trap_SetConfigstring( CS_VOTE_TIME, va( "%i", level.voteTime ) );
  trap_SetConfigstring( CS_VOTE_STRING, level.voteDisplayString );
  trap_SetConfigstring( CS_VOTE_YES, va( "%i", level.voteYes ) );
  trap_SetConfigstring( CS_VOTE_NO, va( "%i", level.voteNo ) );
}

//ROTAX
int G_FindConfigstringIndexROTAX( char *name, int start, int max )
{
  int   i;
  char  s[ MAX_STRING_CHARS ];
  char  wtf[1024];
  
  Q_strncpyz( wtf, name, sizeof( wtf ) );

  if( !wtf || !wtf[ 0 ] )
    return 0;

  for( i = 1; i < max; i++ )
  {
    trap_GetConfigstring( start + i, s, sizeof( s ) );
    if( !s[ 0 ] )
      break;
      
      //trap_SendServerCommand( -1, va("print \"((%s))\n\"", name) );
      //Why the hell variable "name" not work??? Try uncomment line above and in game type "/playsound something".
      //It will print one correct line and next lines are only mess (should be same). Don't know why. I was spend 4 hours with this
      //and then I resolved it trough "wtf" variable.
      
    if( !strcmp( s, wtf ) )
      return i;
  }

  return 0;
}

//ROTAX
//taunt
//seznam dostupnych zvuku
void Cmd_Playsound_f( gentity_t *ent )
{
  char msg[ 64 ];
  char map[ MAX_STRING_CHARS ];//rotax

  int         index = 0;

  if( g_floodMinTime.integer )
    if ( G_Flood_Limited( ent ) )
    {
      trap_SendServerCommand( ent-g_entities, "print \"Playsound is flood-limited; wait before talking again\n\"" );
      return;
    }

  trap_Argv( 1, msg, sizeof( msg ) );
  trap_Cvar_VariableStringBuffer( "mapname", map, sizeof( map ) );

  index = G_FindConfigstringIndexROTAX( va("playsound/%s/%s.wav", map, msg), CS_SOUNDS, MAX_SOUNDS );

  if ( index > 0 )
  {    
    /*
    if (g_chatTeamPrefix.integer)
      Com_sprintf( prefix, sizeof( prefix ), marking(ent));
    else
      Com_sprintf( prefix, sizeof( prefix ), "");*/

    G_LogPrintf( "playsound: %s^7: %s^7\n", ent->client->pers.netname, msg );/*
    Com_sprintf( name2, sizeof( name2 ), "^1(PS)^7 %s%s%c%c"EC": ", prefix,
                 ent->client->pers.netname, Q_COLOR_ESCAPE, COLOR_WHITE );
    color = COLOR_GREEN;*/

    //Com_sprintf( text, sizeof( text ), "%s^7", msg );

    // send it to all the apropriate clients
    /*
    for( j = 0; j < level.maxclients; j++ )
    {
      other = &g_entities[ j ];
      G_SayTo( ent, other, SAY_ALL, color, name2, msg, prefix );
    }*/
    
    //BG_AddPredictableEventToPlayerstate( EV_PAIN, G_SoundIndex( va("playsound/%s/%s.wav", map, msg) ), &ent->client->ps );
    //G_AddPredictableEvent( ent, EV_PAIN, index );
    //G_AddPredictableEvent( ent, EV_GENERAL_SOUND, index );
    //G_AddPredictableEvent( ent, EV_GLOBAL_SOUND, G_SoundIndex( va("playsound/%s/%s.wav", map, msg) ) );
    G_AddEvent( ent, EV_GENERAL_SOUND, index );
  }
  else
    trap_SendServerCommand( ent-g_entities, "print \"Playsound: requested sound not found\n\"" );
}

/*
==================
Cmd_Vote_f
==================
*/
void Cmd_Vote_f( gentity_t *ent )
{
  char msg[ 64 ];

  if( !level.voteTime )
  { 
    if( ent->client->pers.teamSelection != PTE_NONE )
    {
      // If there is a teamvote going on but no global vote, forward this vote on as a teamvote
      // (ugly hack for 1.1 cgames + noobs who can't figure out how to use any command that isn't bound by default)
      int     cs_offset = 0;
      if( ent->client->pers.teamSelection == PTE_ALIENS )
        cs_offset = 1;
    
      if( level.teamVoteTime[ cs_offset ] )
      {
         if( !(ent->client->ps.eFlags & EF_TEAMVOTED ) )
        {
          Cmd_TeamVote_f(ent); 
          return;
        }
      }
    }
    trap_SendServerCommand( ent-g_entities, "print \"No vote in progress\n\"" );
    return;
  }

  if( ent->client->ps.eFlags & EF_VOTED )
  {
    trap_SendServerCommand( ent-g_entities, "print \"Vote already cast\n\"" );
    return;
  }

  //ROTAXfun
  //trap_SendServerCommand( ent-g_entities, "print \"Vote cast\n\"" );

  ent->client->ps.eFlags |= EF_VOTED;

  trap_Argv( 1, msg, sizeof( msg ) );

  if( msg[ 0 ] == 'y' || msg[ 1 ] == 'Y' || msg[ 1 ] == '1' )
  {
    level.voteYes++;
    trap_SetConfigstring( CS_VOTE_YES, va( "%i", level.voteYes ) );
    
    //ROTAXfun
    trap_SendServerCommand( -1, va("print \"^7Vote: %s%s ^7voted ^2YES^7\n\"", marking(ent), ent->client->pers.netname ) );
    G_LogPrintf("Vote: %s%s ^7voted YES\n", marking(ent), ent->client->pers.netname );
  }
  else
  {
    level.voteNo++;
    trap_SetConfigstring( CS_VOTE_NO, va( "%i", level.voteNo ) );
    
    //ROTAXfun
    trap_SendServerCommand( -1, va("print \"^7Vote: %s%s ^7voted ^1NO^7\n\"", marking(ent), ent->client->pers.netname ) );
    G_LogPrintf("Vote: %s%s ^7voted NO\n", marking(ent), ent->client->pers.netname );
  }

  // a majority will be determined in G_CheckVote, which will also account
  // for players entering or leaving
}

/*
==================
Cmd_CallTeamVote_f
==================
*/
void Cmd_CallTeamVote_f( gentity_t *ent )
{
  int   i, team, cs_offset = 0;
  char  arg1[ MAX_STRING_TOKENS ];
  char  arg2[ MAX_STRING_TOKENS ];
  int   clientNum = -1;
  char  name[ MAX_NETNAME ];
  char nullstring[] = "";
  char  message[ MAX_STRING_CHARS ];
  char targetname[ MAX_NAME_LENGTH] = "";
  char reason[ MAX_STRING_CHARS ] = "";
  char *arg1plus;
  char *arg2plus;
  char *ptr = NULL;
  int numVoters = 0;

  arg1plus = G_SayConcatArgs( 1 );
  arg2plus = G_SayConcatArgs( 2 );
  
  team = ent->client->pers.teamSelection;

  if( team == PTE_ALIENS )
    cs_offset = 1;

  if(team==PTE_ALIENS)
    numVoters = level.numAlienClients;
  else if(team==PTE_HUMANS)
    numVoters = level.numHumanClients;

  if( !g_allowVote.integer )
  {
    trap_SendServerCommand( ent-g_entities, "print \"Voting not allowed here\n\"" );
    return;
  }

  if( level.teamVoteTime[ cs_offset ] )
  {
    trap_SendServerCommand( ent-g_entities, "print \"A team vote is already in progress\n\"" );
    return;
  }

  if( g_voteLimit.integer > 0
    && ent->client->pers.voteCount >= g_voteLimit.integer 
    && !G_admin_permission( ent, ADMF_NO_VOTE_LIMIT ) )
  {
    trap_SendServerCommand( ent-g_entities, va(
      "print \"You have already called the maximum number of votes (%d)\n\"",
      g_voteLimit.integer ) );
    return;
  }
  
  if( ent->client->pers.muted )
  {
    trap_SendServerCommand( ent - g_entities,
      "print \"You are muted and cannot call teamvotes\n\"" );
    return;
  }

  if( g_voteMinTime.integer
    && ent->client->pers.firstConnect 
    && level.time - ent->client->pers.enterTime < g_voteMinTime.integer * 1000
    && !G_admin_permission( ent, ADMF_NO_VOTE_LIMIT ) 
    && (level.numPlayingClients > 0 && level.numConnectedClients>1) )
  {
    trap_SendServerCommand( ent-g_entities, va(
      "print \"You must wait %d seconds after connecting before calling a vote\n\"",
      g_voteMinTime.integer ) );
    return;
  }

  // make sure it is a valid command to vote on
  trap_Argv( 1, arg1, sizeof( arg1 ) );
  trap_Argv( 2, arg2, sizeof( arg2 ) );

  if( strchr( arg1, ';' ) || strchr( arg2, ';' ) )
  {
    trap_SendServerCommand( ent-g_entities, "print \"Invalid team vote string\n\"" );
    return;
  }
  
  ptr = strstr(arg1plus, " -");
  if( ptr )
  {
    *ptr = '\0';
    ptr+=2; 

    if( *ptr == 'r' || *ptr=='R' )
    {
      ptr++;
      while( *ptr == ' ' )
        ptr++;
      strcpy(reason, ptr);
    }
    else
    {
      trap_SendServerCommand( ent-g_entities, "print \"callteamvote: Warning: invalid argument specified \n\"" );
    }
  }
  
  // detect clientNum for partial name match votes
  if( !Q_stricmp( arg1, "kick" ) ||
    !Q_stricmp( arg1, "denybuild" ) ||
    !Q_stricmp( arg1, "allowbuild" ) || 
    !Q_stricmp( arg1, "designate" ) || 
    !Q_stricmp( arg1, "undesignate" ) )
  {
    int clientNums[ MAX_CLIENTS ] = { -1 };
    int numMatches=0;
    char err[ MAX_STRING_CHARS ];
    
    Q_strncpyz(targetname, arg2plus, sizeof(targetname));
    ptr = strstr(targetname, " -");
    if( ptr )
      *ptr = '\0';
    
    if( g_requireVoteReasons.integer && !G_admin_permission( ent, ADMF_UNACCOUNTABLE ) && !Q_stricmp( arg1, "kick" ) && reason[ 0 ]=='\0' )
    {
       trap_SendServerCommand( ent-g_entities, "print \"callvote: You must specify a reason. Use /callteamvote kick [player] -r [reason] \n\"" );
       return;
    }
    

    if( !arg2[ 0 ] )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callteamvote: no target\n\"" );
      return;
    }

    numMatches = G_ClientNumbersFromString( targetname, clientNums );
    if( numMatches == 1 )
    {
      // there was only one partial name match
      clientNum = clientNums[ 0 ]; 
    }
    else
    {
      // look for an exact name match (sets clientNum to -1 if it fails) 
      clientNum = G_ClientNumberFromString( ent, targetname );
    }
    
    if( clientNum==-1  && numMatches > 1 ) 
    {
      G_MatchOnePlayer( clientNums, err, sizeof( err ) );
      ADMP( va( "^3callteamvote: ^7%s\n", err ) );
      return;
    }

    // make sure this player is on the same team
    if( clientNum != -1 && level.clients[ clientNum ].pers.teamSelection !=
      team )
    {
      clientNum = -1;
    }
      
    if( clientNum != -1 &&
      level.clients[ clientNum ].pers.connected == CON_DISCONNECTED )
    {
      clientNum = -1;
    }

    if( clientNum != -1 )
    {
      Q_strncpyz( name, level.clients[ clientNum ].pers.netname,
        sizeof( name ) );
      Q_CleanStr( name );
      if( G_admin_permission( &g_entities[ clientNum ], ADMF_IMMUNITY ) )
      {
       char reasonprint[ MAX_STRING_CHARS ] = "";
       if( reason[ 0 ] != '\0' )
        Com_sprintf(reasonprint, sizeof(reasonprint), "With reason: %s", reason);

        Com_sprintf( message, sizeof( message ), "%s^7 attempted /callteamvote %s %s on immune admin %s^7 %s^7",
          ent->client->pers.netname, arg1, arg2, g_entities[ clientNum ].client->pers.netname, reasonprint );
      }
    }
    else
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callteamvote: invalid player\n\"" );
      return;
    }
  }

  if( !Q_stricmp( arg1, "kick" ) )
  {
    if( G_admin_permission( &g_entities[ clientNum ], ADMF_IMMUNITY ) )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callteamvote: admin is immune from vote kick\n\"" );
      G_AdminsPrintf("%s\n",message);
      return;
    }


    // use ip in case this player disconnects before the vote ends
    Com_sprintf( level.teamVoteString[ cs_offset ],
      sizeof( level.teamVoteString[ cs_offset ] ),
      "!ban %s \"%s\" team vote kick", level.clients[ clientNum ].pers.ip,
      g_adminTempBan.string );
    Com_sprintf( level.teamVoteDisplayString[ cs_offset ],
        sizeof( level.teamVoteDisplayString[ cs_offset ] ),
        "Kick player '%s'", name );
  }
  else if( !Q_stricmp( arg1, "denybuild" ) )
  {
    if( level.clients[ clientNum ].pers.denyBuild )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callteamvote: player already lost building rights\n\"" );
      return;
    }

    if( G_admin_permission( &g_entities[ clientNum ], ADMF_IMMUNITY ) )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callteamvote: admin is immune from denybuild\n\"" );
      G_AdminsPrintf("%s\n",message);
      return;
    }

    Com_sprintf( level.teamVoteString[ cs_offset ],
      sizeof( level.teamVoteString[ cs_offset ] ), "!denybuild %i", clientNum );
    Com_sprintf( level.teamVoteDisplayString[ cs_offset ],
        sizeof( level.teamVoteDisplayString[ cs_offset ] ),
        "Take away building rights from '%s'", name );
  }
  else if( !Q_stricmp( arg1, "allowbuild" ) )
  {
    if( !level.clients[ clientNum ].pers.denyBuild )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callteamvote: player already has building rights\n\"" );
      return;
    }

    Com_sprintf( level.teamVoteString[ cs_offset ],
      sizeof( level.teamVoteString[ cs_offset ] ), "!allowbuild %i", clientNum );
    Com_sprintf( level.teamVoteDisplayString[ cs_offset ],
        sizeof( level.teamVoteDisplayString[ cs_offset ] ),
        "Allow '%s' to build", name );
  }
  else if( !Q_stricmp( arg1, "designate" ) )
  {
    if( !g_designateVotes.integer )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callteamvote: Designate votes have been disabled.\n\"" );
      return;
    }

    if( level.clients[ clientNum ].pers.designatedBuilder )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callteamvote: player is already a designated builder\n\"" );
      return;
    }
    Com_sprintf( level.teamVoteString[ cs_offset ],
      sizeof( level.teamVoteString[ cs_offset ] ), "!designate %i", clientNum );
    Com_sprintf( level.teamVoteDisplayString[ cs_offset ],
        sizeof( level.teamVoteDisplayString[ cs_offset ] ),
        "Make '%s' a designated builder", name );
  }
  else if( !Q_stricmp( arg1, "undesignate" ) )
  {

    if( !g_designateVotes.integer )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callteamvote: Designate votes have been disabled.\n\"" );
      return;
    }

    if( !level.clients[ clientNum ].pers.designatedBuilder )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callteamvote: player is not currently a designated builder\n\"" );
      return;
    }
    Com_sprintf( level.teamVoteString[ cs_offset ],
      sizeof( level.teamVoteString[ cs_offset ] ), "!undesignate %i", clientNum );
    Com_sprintf( level.teamVoteDisplayString[ cs_offset ],
        sizeof( level.teamVoteDisplayString[ cs_offset ] ),
        "Remove designated builder status from '%s'", name );
  }
  else if( !Q_stricmp( arg1, "admitdefeat" ) )
  {
    //rotax
    trap_SendServerCommand( ent-g_entities,
      "print \"callteamvote: Admit Defeat is for noobs\n\"" );
    return;
    
    if( numVoters <=1 )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"callteamvote: You cannot admitdefeat by yourself. Use /callvote draw.\n\"" );
      return;
    }

    Com_sprintf( level.teamVoteString[ cs_offset ],
      sizeof( level.teamVoteString[ cs_offset ] ), "admitdefeat %i", team );
    Com_sprintf( level.teamVoteDisplayString[ cs_offset ],
        sizeof( level.teamVoteDisplayString[ cs_offset ] ),
        "Admit Defeat" );
  }
   else if( !Q_stricmp( arg1, "poll" ) )
   {
     if( arg2plus[ 0 ] == '\0' )
     {
       trap_SendServerCommand( ent-g_entities, "print \"callteamvote: You forgot to specify what people should vote on.\n\"" );
       return;
     }
     Com_sprintf( level.teamVoteString[ cs_offset ], sizeof( level.teamVoteString[ cs_offset ] ), nullstring );
     Com_sprintf( level.teamVoteDisplayString[ cs_offset ],
         sizeof( level.voteDisplayString ), "[Poll] \'%s\'", arg2plus );
   }
  else
  {
    trap_SendServerCommand( ent-g_entities, "print \"Invalid vote string\n\"" );
    trap_SendServerCommand( ent-g_entities,
       "print \"Valid team vote commands are: "
       "kick, denybuild, allowbuild, poll, designate and undesignate.\n\"" );//rotax
    return;
  }
  ent->client->pers.voteCount++;
  
  if ( reason[0]!='\0' )
    Q_strcat( level.teamVoteDisplayString[ cs_offset ], sizeof( level.teamVoteDisplayString[ cs_offset ] ), va( " Reason: '%s'^7", reason ) );

  for( i = 0 ; i < level.maxclients ; i++ )
  {
    if( level.clients[ i ].pers.connected == CON_DISCONNECTED )
      continue;

    if( level.clients[ i ].ps.stats[ STAT_PTEAM ] == team )
    {
      //rotax
      //trap_SendServerCommand( i, va("print \"%s " S_COLOR_WHITE
      //      "called a team vote: %s^7 \n\"", ent->client->pers.netname, level.teamVoteDisplayString[ cs_offset ] ) );
      trap_SendServerCommand( i, va("print \"" S_COLOR_WHITE
            "^2____TEAMVOTE____^7 %s %s^7 \n\"", marking_vote(ent), level.teamVoteDisplayString[ cs_offset ] ) );
    }
    else if( G_admin_permission( &g_entities[ i ], ADMF_ADMINCHAT ) && 
             ( ( !Q_stricmp( arg1, "kick" ) || !Q_stricmp( arg1, "denybuild" ) ) || 
             level.clients[ i ].pers.teamSelection == PTE_NONE ) )
    {
      //rotax
      //trap_SendServerCommand( i, va("print \"^6[Admins]^7 %s " S_COLOR_WHITE
      //      "called a team vote: %s^7 \n\"", ent->client->pers.netname, level.teamVoteDisplayString[ cs_offset ] ) );
      trap_SendServerCommand( i, va("print \"" S_COLOR_WHITE
            "^2____TEAMVOTE____^7 ^6[Admins]^7 %s %s^7 \n\"", marking_vote(ent), level.teamVoteDisplayString[ cs_offset ] ) );
    }
  }
  
  if(team==PTE_ALIENS)
    G_LogPrintf("Teamvote: %s^7 called a teamvote (aliens): %s^7\n", ent->client->pers.netname, level.teamVoteDisplayString[ cs_offset ] );
  else if(team==PTE_HUMANS)
    G_LogPrintf("Teamvote: %s^7 called a teamvote (humans): %s^7\n", ent->client->pers.netname, level.teamVoteDisplayString[ cs_offset ] );
  
  Q_strcat( level.teamVoteDisplayString[ cs_offset ], sizeof( level.teamVoteDisplayString[ cs_offset ] ), va( " Called by: '%s^7'", ent->client->pers.netname ) );

  // start the voting, the caller autoamtically votes yes
  level.teamVoteTime[ cs_offset ] = level.time;
  level.teamVoteNo[ cs_offset ] = 0;

  for( i = 0 ; i < level.maxclients ; i++ )
  {
    if( level.clients[ i ].ps.stats[ STAT_PTEAM ] == team )
      level.clients[ i ].ps.eFlags &= ~EF_TEAMVOTED;
  }

  if( !Q_stricmp( arg1, "poll" ) )
  {
    level.teamVoteYes[ cs_offset ] = 0;
  }
  else
  {
   level.teamVoteYes[ cs_offset ] = 1;
   ent->client->ps.eFlags |= EF_TEAMVOTED;
  }

  trap_SetConfigstring( CS_TEAMVOTE_TIME + cs_offset, va( "%i", level.teamVoteTime[ cs_offset ] ) );
  trap_SetConfigstring( CS_TEAMVOTE_STRING + cs_offset, level.teamVoteDisplayString[ cs_offset ] );
  trap_SetConfigstring( CS_TEAMVOTE_YES + cs_offset, va( "%i", level.teamVoteYes[ cs_offset ] ) );
  trap_SetConfigstring( CS_TEAMVOTE_NO + cs_offset, va( "%i", level.teamVoteNo[ cs_offset ] ) );
}


/*
==================
Cmd_TeamVote_f
==================
*/
void Cmd_TeamVote_f( gentity_t *ent )
{
  int     cs_offset = 0;
  char    msg[ 64 ];

  if( ent->client->pers.teamSelection == PTE_ALIENS )
    cs_offset = 1;

  if( !level.teamVoteTime[ cs_offset ] )
  {
    trap_SendServerCommand( ent-g_entities, "print \"No team vote in progress\n\"" );
    return;
  }

  if( ent->client->ps.eFlags & EF_TEAMVOTED )
  {
    trap_SendServerCommand( ent-g_entities, "print \"Team vote already cast\n\"" );
    return;
  }

  //ROTAXfun
  //trap_SendServerCommand( ent-g_entities, "print \"Team vote cast\n\"" );

  ent->client->ps.eFlags |= EF_TEAMVOTED;

  trap_Argv( 1, msg, sizeof( msg ) );

  if( msg[ 0 ] == 'y' || msg[ 1 ] == 'Y' || msg[ 1 ] == '1' )
  {
    level.teamVoteYes[ cs_offset ]++;
    trap_SetConfigstring( CS_TEAMVOTE_YES + cs_offset, va( "%i", level.teamVoteYes[ cs_offset ] ) );
    
    //ROTAXfun
    G_LogPrintf("Vote: %s%s ^5voted YES\n", marking(ent), ent->client->pers.netname );
    G_TeamCommand( ent->client->pers.teamSelection, va( "tchat -1 \"^5Vote: %s%s ^5voted ^2YES^5\"", marking(ent), ent->client->pers.netname ) );
  }
  else
  {
    level.teamVoteNo[ cs_offset ]++;
    trap_SetConfigstring( CS_TEAMVOTE_NO + cs_offset, va( "%i", level.teamVoteNo[ cs_offset ] ) );
    
    //ROTAXfun
    G_LogPrintf("Vote: %s%s ^5voted NO\n", marking(ent), ent->client->pers.netname );
    G_TeamCommand( ent->client->pers.teamSelection, va( "tchat -1 \"^5Vote: %s%s ^7voted ^2NO^5\"", marking(ent), ent->client->pers.netname ) );
  }

  // a majority will be determined in TeamCheckVote, which will also account
  // for players entering or leaving
}


/*
=================
Cmd_SetViewpos_f
=================
*/
void Cmd_SetViewpos_f( gentity_t *ent )
{
  vec3_t  origin, angles;
  char    buffer[ MAX_TOKEN_CHARS ];
  int     i;

  if( trap_Argc( ) != 5 )
  {
    trap_SendServerCommand( ent-g_entities, va( "print \"usage: setviewpos x y z yaw\n\"" ) );
    return;
  }

  VectorClear( angles );

  for( i = 0 ; i < 3 ; i++ )
  {
    trap_Argv( i + 1, buffer, sizeof( buffer ) );
    origin[ i ] = atof( buffer );
  }

  trap_Argv( 4, buffer, sizeof( buffer ) );
  angles[ YAW ] = atof( buffer );

  TeleportPlayer( ent, origin, angles );
}

#define AS_OVER_RT3         ((ALIENSENSE_RANGE*0.5f)/M_ROOT3)

static qboolean G_RoomForClassChange( gentity_t *ent, pClass_t class,
  vec3_t newOrigin )
{
  vec3_t    fromMins, fromMaxs;
  vec3_t    toMins, toMaxs;
  vec3_t    temp;
  trace_t   tr;
  float     nudgeHeight;
  float     maxHorizGrowth;
  pClass_t  oldClass = ent->client->ps.stats[ STAT_PCLASS ];

  BG_FindBBoxForClass( oldClass, fromMins, fromMaxs, NULL, NULL, NULL );
  BG_FindBBoxForClass( class, toMins, toMaxs, NULL, NULL, NULL );

  VectorCopy( ent->s.origin, newOrigin );

  // find max x/y diff
  maxHorizGrowth = toMaxs[ 0 ] - fromMaxs[ 0 ];
  if( toMaxs[ 1 ] - fromMaxs[ 1 ] > maxHorizGrowth )
    maxHorizGrowth = toMaxs[ 1 ] - fromMaxs[ 1 ];
  if( toMins[ 0 ] - fromMins[ 0 ] > -maxHorizGrowth )
    maxHorizGrowth = -( toMins[ 0 ] - fromMins[ 0 ] );
  if( toMins[ 1 ] - fromMins[ 1 ] > -maxHorizGrowth )
    maxHorizGrowth = -( toMins[ 1 ] - fromMins[ 1 ] );

  if( maxHorizGrowth > 0.0f )
  {
    // test by moving the player up the max required on a 60 degree slope
    nudgeHeight = maxHorizGrowth * 2.0f;
  }
  else
  {
    // player is shrinking, so there's no need to nudge them upwards
    nudgeHeight = 0.0f;
  }

  // find what the new origin would be on a level surface
  newOrigin[ 2 ] += fabs( toMins[ 2 ] ) - fabs( fromMins[ 2 ] );

  //compute a place up in the air to start the real trace
  VectorCopy( newOrigin, temp );
  temp[ 2 ] += nudgeHeight;
  trap_Trace( &tr, newOrigin, toMins, toMaxs, temp, ent->s.number, MASK_PLAYERSOLID );

  //trace down to the ground so that we can evolve on slopes
  VectorCopy( newOrigin, temp );
  temp[ 2 ] += ( nudgeHeight * tr.fraction );
  trap_Trace( &tr, temp, toMins, toMaxs, newOrigin, ent->s.number, MASK_PLAYERSOLID );
  VectorCopy( tr.endpos, newOrigin );

  //make REALLY sure
  trap_Trace( &tr, newOrigin, toMins, toMaxs, newOrigin,
    ent->s.number, MASK_PLAYERSOLID );

  //check there is room to evolve
  if( !tr.startsolid && tr.fraction == 1.0f )
    return qtrue;
  else
    return qfalse;
}

/*
=================
Cmd_Class_f
=================
*/
void Cmd_Class_f( gentity_t *ent )
{
  char      s[ MAX_TOKEN_CHARS ];
  int       clientNum;
  int       i;
  vec3_t    infestOrigin;
  pClass_t  currentClass = ent->client->pers.classSelection;
  pClass_t  newClass;
  int       numLevels;
  int       entityList[ MAX_GENTITIES ];
  vec3_t    range = { AS_OVER_RT3, AS_OVER_RT3, AS_OVER_RT3 };
  vec3_t    mins, maxs;
  int       num;
  gentity_t *other;


  clientNum = ent->client - level.clients;
  trap_Argv( 1, s, sizeof( s ) );
  newClass = BG_FindClassNumForName( s );

  if( ent->client->sess.sessionTeam == TEAM_SPECTATOR )
  {
    if( ent->client->sess.spectatorState == SPECTATOR_FOLLOW )
      G_StopFollowing( ent );

    if( ent->client->pers.teamSelection == PTE_ALIENS )
    {
      if( newClass != PCL_ALIEN_BUILDER0 &&
          newClass != PCL_ALIEN_BUILDER0_UPG &&
          newClass != PCL_ALIEN_LEVEL0 )
      {
        trap_SendServerCommand( ent-g_entities,
          va( "print \"You cannot spawn with class %s\n\"", s ) );
        return;
      } 
      
      if( !BG_ClassIsAllowed( newClass ) )
      {
        trap_SendServerCommand( ent-g_entities,
          va( "print \"Class %s is not allowed\n\"", s ) );
        return;
      }
      
      if( !BG_FindStagesForClass( newClass, g_alienStage.integer ) )
      {
        trap_SendServerCommand( ent-g_entities,
          va( "print \"Class %s not allowed at stage %d\n\"",
              s, g_alienStage.integer ) );
        return;
      }
      
      if( ent->client->pers.denyBuild && ( newClass==PCL_ALIEN_BUILDER0 || newClass==PCL_ALIEN_BUILDER0_UPG ) )
      {
        trap_SendServerCommand( ent-g_entities, "print \"Your building rights have been revoked\n\"" );
        return;
      }

      // spawn from an egg
      if( G_PushSpawnQueue( &level.alienSpawnQueue, clientNum ) )
      {
        ent->client->pers.classSelection = newClass;
        ent->client->ps.stats[ STAT_PCLASS ] = newClass;
      }
    }
    else if( ent->client->pers.teamSelection == PTE_HUMANS )
    {
      //set the item to spawn with
      if( !Q_stricmp( s, BG_FindNameForWeapon( WP_MACHINEGUN ) ) &&
          BG_WeaponIsAllowed( WP_MACHINEGUN ) )
      {
        ent->client->pers.humanItemSelection = WP_MACHINEGUN;
      }
      else if( !Q_stricmp( s, BG_FindNameForWeapon( WP_HBUILD ) ) &&
               BG_WeaponIsAllowed( WP_HBUILD ) )
      {
        ent->client->pers.humanItemSelection = WP_HBUILD;
      }
      else if( !Q_stricmp( s, BG_FindNameForWeapon( WP_HBUILD2 ) ) &&
               BG_WeaponIsAllowed( WP_HBUILD2 ) &&
               BG_FindStagesForWeapon( WP_HBUILD2, g_humanStage.integer ) )
      {
        ent->client->pers.humanItemSelection = WP_HBUILD2;
      }
      else
      {
        trap_SendServerCommand( ent-g_entities,
          "print \"Unknown starting item\n\"" );
        return;
      }
      // spawn from a telenode
      G_LogOnlyPrintf("ClientTeamClass: %i human %s\n", clientNum, s);
      if( G_PushSpawnQueue( &level.humanSpawnQueue, clientNum ) )
      {
        ent->client->pers.classSelection = PCL_HUMAN;
        ent->client->ps.stats[ STAT_PCLASS ] = PCL_HUMAN;
      }
    }
    return;
  }

  if( ent->health <= 0 )
    return;

  if( ent->client->pers.teamSelection == PTE_ALIENS &&
      !( ent->client->ps.stats[ STAT_STATE ] & SS_INFESTING ) &&
      !( ent->client->ps.stats[ STAT_STATE ] & SS_HOVELING ) )
  {
    if( newClass == PCL_NONE )
    {
      trap_SendServerCommand( ent-g_entities, "print \"Unknown class\n\"" );
      return;
    }

    //if we are not currently spectating, we are attempting evolution
    if( ent->client->pers.classSelection != PCL_NONE )
    {
      if( ( ent->client->ps.stats[ STAT_STATE ] & SS_WALLCLIMBING ) ||
          ( ent->client->ps.stats[ STAT_STATE ] & SS_WALLCLIMBINGCEILING ) )
      {
        trap_SendServerCommand( ent-g_entities,
          "print \"You cannot evolve while wallwalking\n\"" );
        return;
      }

      //check there are no humans nearby
      VectorAdd( ent->client->ps.origin, range, maxs );
      VectorSubtract( ent->client->ps.origin, range, mins );

      num = trap_EntitiesInBox( mins, maxs, entityList, MAX_GENTITIES );
      for( i = 0; i < num; i++ )
      {
        other = &g_entities[ entityList[ i ] ];

        if( ( other->client && other->client->ps.stats[ STAT_PTEAM ] == PTE_HUMANS ) ||
            ( other->s.eType == ET_BUILDABLE && other->biteam == BIT_HUMANS ) )
        {
          G_TriggerMenu( clientNum, MN_A_TOOCLOSE );
          return;
        }
      }

      if( !level.overmindPresent )
      {
        G_TriggerMenu( clientNum, MN_A_NOOVMND_EVOLVE );
        return;
      }

      //guard against selling the HBUILD weapons exploit
       if( ent->client->sess.sessionTeam != TEAM_SPECTATOR &&
           ( currentClass == PCL_ALIEN_BUILDER0 ||
            currentClass == PCL_ALIEN_BUILDER0_UPG ) &&
          ent->client->ps.stats[ STAT_MISC ] > 0 )
      {
        trap_SendServerCommand( ent-g_entities,
            va( "print \"You cannot evolve until build timer expires\n\"" ) );
        return;
      }

      numLevels = BG_ClassCanEvolveFromTo( currentClass,
                                           newClass,
                                           (short)ent->client->ps.persistant[ PERS_CREDIT ], 0 );

      if( G_RoomForClassChange( ent, newClass, infestOrigin ) )
      {
        //...check we can evolve to that class
        if( numLevels >= 0 &&
            BG_FindStagesForClass( newClass, g_alienStage.integer ) &&
            BG_ClassIsAllowed( newClass ) )
        {
          G_LogOnlyPrintf("ClientTeamClass: %i alien %s\n", clientNum, s);

          ent->client->pers.evolveHealthFraction = (float)ent->client->ps.stats[ STAT_HEALTH ] /
            (float)BG_FindHealthForClass( currentClass );

          if( ent->client->pers.evolveHealthFraction < 0.0f )
            ent->client->pers.evolveHealthFraction = 0.0f;
          else if( ent->client->pers.evolveHealthFraction > 1.0f )
            ent->client->pers.evolveHealthFraction = 1.0f;

          //remove credit
          G_AddCreditToClient( ent->client, -(short)numLevels, qtrue );
          ent->client->pers.classSelection = newClass;
          ClientUserinfoChanged( clientNum );
          VectorCopy( infestOrigin, ent->s.pos.trBase );
          ClientSpawn( ent, ent, ent->s.pos.trBase, ent->s.apos.trBase );
          return;
        }
        else
        {
          trap_SendServerCommand( ent-g_entities,
               "print \"You cannot evolve from your current class\n\"" );
          return;
        }
      }
      else
      {
        G_TriggerMenu( clientNum, MN_A_NOEROOM );
        return;
      }
    }
   else if( ent->client->pers.teamSelection == PTE_HUMANS )
   {
    //humans cannot use this command whilst alive
    if( ent->client->pers.classSelection != PCL_NONE )
    {
      trap_SendServerCommand( ent-g_entities, va( "print \"You must be dead to use the class command\n\"" ) );
      return;
    }

    ent->client->pers.classSelection =
      ent->client->ps.stats[ STAT_PCLASS ] = PCL_HUMAN;

    //set the item to spawn with
    if( !Q_stricmp( s, BG_FindNameForWeapon( WP_MACHINEGUN ) ) && BG_WeaponIsAllowed( WP_MACHINEGUN ) )
      ent->client->pers.humanItemSelection = WP_MACHINEGUN;
    else if( !Q_stricmp( s, BG_FindNameForWeapon( WP_HBUILD ) ) && BG_WeaponIsAllowed( WP_HBUILD ) )
      ent->client->pers.humanItemSelection = WP_HBUILD;
    else if( !Q_stricmp( s, BG_FindNameForWeapon( WP_HBUILD2 ) ) && BG_WeaponIsAllowed( WP_HBUILD2 ) &&
        BG_FindStagesForWeapon( WP_HBUILD2, g_humanStage.integer ) )
      ent->client->pers.humanItemSelection = WP_HBUILD2;
    else
    {
      ent->client->pers.classSelection = PCL_NONE;
      trap_SendServerCommand( ent-g_entities, va( "print \"Unknown starting item\n\"" ) );
      return;
    }

    G_LogOnlyPrintf("ClientTeamClass: %i human %s\n", clientNum, s);

    G_PushSpawnQueue( &level.humanSpawnQueue, clientNum );
  }
 }
}

/*
=================
DBCommand

Send command to all designated builders of selected team
=================
*/
void DBCommand( pTeam_t team, const char *text )
{
  int i;
  gentity_t *ent;

  for( i = 0, ent = g_entities + i; i < level.maxclients; i++, ent++ )
  {
    if( !ent->client || ( ent->client->pers.connected != CON_CONNECTED ) ||
        ( ent->client->pers.teamSelection != team ) ||
        !ent->client->pers.designatedBuilder )
      continue;

    trap_SendServerCommand( i, text );
  }
}

/*
=================
Cmd_Destroy_f
=================
*/
void Cmd_Destroy_f( gentity_t *ent )
{
  vec3_t      forward, end;
  trace_t     tr;
  gentity_t   *traceEnt;
  char        cmd[ 12 ];
  qboolean    deconstruct = qtrue;

  if( ent->client->pers.denyBuild )
  {
    trap_SendServerCommand( ent-g_entities,
      "print \"Your building rights have been revoked\n\"" );
    return;
  }

  trap_Argv( 0, cmd, sizeof( cmd ) );
  if( Q_stricmp( cmd, "destroy" ) == 0 )
    deconstruct = qfalse;

  if( ent->client->ps.stats[ STAT_STATE ] & SS_HOVELING )
  {
    if( ( ent->client->hovel->s.eFlags & EF_DBUILDER ) &&
      !ent->client->pers.designatedBuilder )
    {
      trap_SendServerCommand( ent-g_entities, 
        "print \"This structure is protected by designated builder\n\"" );
      DBCommand( ent->client->pers.teamSelection,
        va( "print \"%s^3 has attempted to decon a protected structure!\n\"",
          ent->client->pers.netname ) );
      return;
    }
    G_Damage( ent->client->hovel, ent, ent, forward, ent->s.origin, 
      10000, 0, MOD_SUICIDE );
  }

  if( !( ent->client->ps.stats[ STAT_STATE ] & SS_INFESTING ) )
  {
    AngleVectors( ent->client->ps.viewangles, forward, NULL, NULL );
    VectorMA( ent->client->ps.origin, 100, forward, end );

    trap_Trace( &tr, ent->client->ps.origin, NULL, NULL, end, ent->s.number, MASK_PLAYERSOLID );
    traceEnt = &g_entities[ tr.entityNum ];

    if( tr.fraction < 1.0f &&
        ( traceEnt->s.eType == ET_BUILDABLE ) &&
        ( traceEnt->biteam == ent->client->pers.teamSelection ) &&
        ( ( ent->client->ps.weapon >= WP_ABUILD ) &&
          ( ent->client->ps.weapon <= WP_HBUILD ) ) )
    {
      // Cancel deconstruction
      if( g_markDeconstruct.integer && traceEnt->deconstruct )
      {
        traceEnt->deconstruct = qfalse;
        return;
      }
      if( ( traceEnt->s.eFlags & EF_DBUILDER ) &&
        !ent->client->pers.designatedBuilder )
      {
        trap_SendServerCommand( ent-g_entities, 
          "print \"This structure is protected by designated builder\n\"" );
        DBCommand( ent->client->pers.teamSelection,
          va( "print \"%s^3 has attempted to decon a protected structure!\n\"",
            ent->client->pers.netname ) );
        return;
      }
 

      // Prevent destruction of the last spawn
      if( !g_markDeconstruct.integer && !g_cheats.integer )
      {
        if( ent->client->pers.teamSelection == PTE_ALIENS &&
            traceEnt->s.modelindex == BA_A_SPAWN )
        {
          if( level.numAlienSpawns <= 1 )
          {
            //ROTAX
            trap_SendServerCommand( ent-g_entities,
              "print \"Last spawn\n\"" );
            return;
          }
        }
        else if( ent->client->pers.teamSelection == PTE_HUMANS &&
                 traceEnt->s.modelindex == BA_H_SPAWN )
        {
          if( level.numHumanSpawns <= 1 )
          {
            //ROTAX
            trap_SendServerCommand( ent-g_entities,
              "print \"Last spawn\n\"" );
            return;
          }
        }
      }

      // Don't allow destruction of hovel with granger inside
      if( traceEnt->s.modelindex == BA_A_HOVEL && traceEnt->active )
        return;

/* rotax 
      // Don't allow destruction of buildables that cannot be rebuilt
      if(g_suddenDeath.integer && traceEnt->health > 0 &&
          ( ( g_suddenDeathMode.integer == SDMODE_SELECTIVE &&
              !BG_FindReplaceableTestForBuildable( traceEnt->s.modelindex ) ) ||
            ( g_suddenDeathMode.integer == SDMODE_BP &&
              BG_FindBuildPointsForBuildable( traceEnt->s.modelindex ) ) ||
            g_suddenDeathMode.integer == SDMODE_NO_BUILD ) )
      {
        trap_SendServerCommand( ent-g_entities,
          "print \"During Sudden Death you can only decon buildings that "
          "can be rebuilt\n\"" );
        return;
      }
*/
      if(g_suddenDeath.integer)//rotax
      {
        /* - vraceno
        if (traceEnt->s.modelindex == BA_H_REACTOR)
        {
          //pocet rc
          int pocet_rc = 0;
          int i = 0;
          gentity_t *check_ent;
          check_ent = &g_entities[ 0 ];
          for ( i = 1, check_ent = g_entities + i; i < level.num_entities; i++, check_ent++ )
          {
            if( check_ent->s.eType != ET_BUILDABLE )
              continue;

            if( check_ent->s.modelindex == BA_H_REACTOR && check_ent->health > 0 )
            {
              pocet_rc++;
            }
          }
          
          if (pocet_rc == 1)
          {
            trap_SendServerCommand( ent-g_entities,
              "print \"You can't deconstruct last Reactor in sudden death\n\"" );
            return;
          }
        }
        else if (traceEnt->s.modelindex == BA_A_OVERMIND)
        {
          //pocet om
          int pocet_om = 0;
          int i = 0;
          gentity_t *check_ent;
          check_ent = &g_entities[ 0 ];
          for ( i = 1, check_ent = g_entities + i; i < level.num_entities; i++, check_ent++ )
          {
            if( check_ent->s.eType != ET_BUILDABLE )
              continue;

            if( check_ent->s.modelindex == BA_A_OVERMIND && check_ent->health > 0 )
            {
              pocet_om++;
            }
          }
          
          if (pocet_om == 1)
          {
            trap_SendServerCommand( ent-g_entities,
              "print \"You can't deconstruct last Overmind in sudden death\n\"" );
            return;
          }
        }
        else*/ 
        
        if (traceEnt->health > 0)
        {
          trap_SendServerCommand( ent-g_entities,
            "print \"You can't deconstruct buildings in sudden death\n\"" );
          return;
        }
      }
    
      if(g_friendlyFire.integer == 0 || g_friendlyBuildableFire.integer == 0)//rotax pokud je ffboff, neme deconovat
      {
        trap_SendServerCommand( ent-g_entities,
          "print \"Deconstructing is off.\n\"" );
        return;
      }

      //rotax pokud nema jmeno, nemuze deconovat
      if (g_newbieNamePrefix.string[ 0 ] && !Q_stricmpn( ent->client->pers.netname, g_newbieNamePrefix.string, strlen(g_newbieNamePrefix.string)))
      {
        trap_SendServerCommand( ent-g_entities,
          "print \"Take a nick before trying to build. Type: /name your_nick\n\"" );
        return;
      }

      if( ent->client->ps.stats[ STAT_MISC ] > 0 )
      {
        G_AddEvent( ent, EV_BUILD_DELAY, ent->client->ps.clientNum );
        return;
      }

      if( traceEnt->health > 0 || g_deconDead.integer )
      {
        if( g_markDeconstruct.integer )
        {
          traceEnt->deconstruct     = qtrue; // Mark buildable for deconstruction
          traceEnt->deconstructTime = level.time;
        }
        else
        {
          if( traceEnt->health > 0 )
          {
            buildHistory_t *new;

            new = G_Alloc( sizeof( buildHistory_t ) );
            new->ID = ( ++level.lastBuildID > 1000 ) 
                ? ( level.lastBuildID = 1 ) : level.lastBuildID;
            new->ent = ent;
            new->name[ 0 ] = 0;
            new->buildable = traceEnt->s.modelindex;
            VectorCopy( traceEnt->s.pos.trBase, new->origin );
            VectorCopy( traceEnt->s.angles, new->angles );
            VectorCopy( traceEnt->s.origin2, new->origin2 );
            VectorCopy( traceEnt->s.angles2, new->angles2 );
            new->fate = BF_DECONNED;
            new->next = NULL;
            new->marked = NULL;
            G_LogBuild( new );

            G_TeamCommand( ent->client->pers.teamSelection,
              va( "print \"%s ^3DECONSTRUCTED^7 by %s^7\n\"",
                BG_FindHumanNameForBuildable( traceEnt->s.modelindex ), 
                ent->client->pers.netname ) );

            G_LogPrintf( "Decon: %i %i 0: %s^7 deconstructed %s\n",
              ent->client->ps.clientNum,
              traceEnt->s.modelindex,
              ent->client->pers.netname, 
              BG_FindNameForBuildable( traceEnt->s.modelindex ) );
              
            //ROTAXfun
            AddScore( ent, -2 );
        }

          if( !deconstruct )
            G_Damage( traceEnt, ent, ent, forward, tr.endpos, 10000, 0, MOD_SUICIDE );
          else
            G_FreeEntity( traceEnt );

          if( !g_cheats.integer )
            ent->client->ps.stats[ STAT_MISC ] +=
              BG_FindBuildDelayForWeapon( ent->s.weapon ) >> 2;
        }
      }
    }
  }
}


/*
=================
Cmd_ActivateItem_f

Activate an item
=================
*/
void Cmd_ActivateItem_f( gentity_t *ent )
{
  char  s[ MAX_TOKEN_CHARS ];
  int   upgrade, weapon;

  trap_Argv( 1, s, sizeof( s ) );
  upgrade = BG_FindUpgradeNumForName( s );
  weapon = BG_FindWeaponNumForName( s );

  if( upgrade != UP_NONE && BG_InventoryContainsUpgrade( upgrade, ent->client->ps.stats ) )
    BG_ActivateUpgrade( upgrade, ent->client->ps.stats );
  else if( weapon != WP_NONE && BG_InventoryContainsWeapon( weapon, ent->client->ps.stats ) )
  {
    //ROTAX
    if ( !(ent->client->ps.weapon == WP_LUCIFER_CANNON && ent->client->ps.stats[ STAT_MISC ] > 0) )
      G_ForceWeaponChange( ent, weapon );
  }
  else
    trap_SendServerCommand( ent-g_entities, va( "print \"You don't have the %s\n\"", s ) );
}


/*
=================
Cmd_DeActivateItem_f

Deactivate an item
=================
*/
void Cmd_DeActivateItem_f( gentity_t *ent )
{
  char  s[ MAX_TOKEN_CHARS ];
  int   upgrade;

  trap_Argv( 1, s, sizeof( s ) );
  upgrade = BG_FindUpgradeNumForName( s );

  if( BG_InventoryContainsUpgrade( upgrade, ent->client->ps.stats ) )
    BG_DeactivateUpgrade( upgrade, ent->client->ps.stats );
  else
    trap_SendServerCommand( ent-g_entities, va( "print \"You don't have the %s\n\"", s ) );
}


/*
=================
Cmd_ToggleItem_f
=================
*/
void Cmd_ToggleItem_f( gentity_t *ent )
{
  char  s[ MAX_TOKEN_CHARS ];
  int   upgrade, weapon, i;

  trap_Argv( 1, s, sizeof( s ) );
  upgrade = BG_FindUpgradeNumForName( s );
  weapon = BG_FindWeaponNumForName( s );

  if( weapon != WP_NONE )
  {
    //special case to allow switching between
    //the blaster and the primary weapon

    if( ent->client->ps.weapon != WP_BLASTER )
      weapon = WP_BLASTER;
    else
    {
      //find a held weapon which isn't the blaster
      for( i = WP_NONE + 1; i < WP_NUM_WEAPONS; i++ )
      {
        if( i == WP_BLASTER )
          continue;

        if( BG_InventoryContainsWeapon( i, ent->client->ps.stats ) )
        {
          weapon = i;
          break;
        }
      }

      if( i == WP_NUM_WEAPONS )
        weapon = WP_BLASTER;
    }

    //ROTAX
    if ( !(ent->client->ps.weapon == WP_LUCIFER_CANNON && ent->client->ps.stats[ STAT_MISC ] > 0) )
      G_ForceWeaponChange( ent, weapon );
  }
  else if( BG_InventoryContainsUpgrade( upgrade, ent->client->ps.stats ) )
  {
    if( BG_UpgradeIsActive( upgrade, ent->client->ps.stats ) )
      BG_DeactivateUpgrade( upgrade, ent->client->ps.stats );
    else
      BG_ActivateUpgrade( upgrade, ent->client->ps.stats );
  }
  else
    trap_SendServerCommand( ent-g_entities, va( "print \"You don't have the %s\n\"", s ) );
}

/*
=================
Cmd_Buy_f
=================
*/
void Cmd_Buy_f( gentity_t *ent )
{
  char      s[ MAX_TOKEN_CHARS ];
  int       i;
  int       weapon, upgrade, numItems = 0;
  int       maxAmmo, maxClips;
  qboolean  buyingEnergyAmmo = qfalse;
  qboolean  hasEnergyWeapon = qfalse;

  for( i = UP_NONE; i < UP_NUM_UPGRADES; i++ )
  {
    if( BG_InventoryContainsUpgrade( i, ent->client->ps.stats ) )
      numItems++;
  }

  for( i = WP_NONE; i < WP_NUM_WEAPONS; i++ )
  {
    if( BG_InventoryContainsWeapon( i, ent->client->ps.stats ) )
    {
      if( BG_FindUsesEnergyForWeapon( i ) )
        hasEnergyWeapon = qtrue;
      numItems++;
    }
  }

  trap_Argv( 1, s, sizeof( s ) );

  weapon = BG_FindWeaponNumForName( s );
  upgrade = BG_FindUpgradeNumForName( s );

  //special case to keep norf happy
  if( weapon == WP_NONE && upgrade == UP_AMMO )
  {
    buyingEnergyAmmo = hasEnergyWeapon;
  }

  if( buyingEnergyAmmo )
  {
    //no armoury nearby
    if( !G_BuildableRange( ent->client->ps.origin, 100, BA_H_REACTOR ) &&
        !G_BuildableRange( ent->client->ps.origin, 100, BA_H_REPEATER ) &&
        !G_BuildableRange( ent->client->ps.origin, 100, BA_H_ARMOURY ) )
    {
      trap_SendServerCommand( ent-g_entities, va(
        "print \"You must be near a reactor, repeater or armoury\n\"" ) );
      return;
    }
  }
  else
  {
    //no armoury nearby
    if( !G_BuildableRange( ent->client->ps.origin, 100, BA_H_ARMOURY ) )
    {
      trap_SendServerCommand( ent-g_entities, va( "print \"You must be near a powered armoury\n\"" ) );
      return;
    }
  }

  if( weapon != WP_NONE )
  {
    //already got this?
    if( BG_InventoryContainsWeapon( weapon, ent->client->ps.stats ) )
    {
      G_TriggerMenu( ent->client->ps.clientNum, MN_H_ITEMHELD );
      return;
    }

    //can afford this?
    if( BG_FindPriceForWeapon( weapon ) > (short)ent->client->ps.persistant[ PERS_CREDIT ] )
    {
      G_TriggerMenu( ent->client->ps.clientNum, MN_H_NOFUNDS );
      return;
    }

    //have space to carry this?
    if( BG_FindSlotsForWeapon( weapon ) & ent->client->ps.stats[ STAT_SLOTS ] )
    {
      G_TriggerMenu( ent->client->ps.clientNum, MN_H_NOSLOTS );
      return;
    }

    if( BG_FindTeamForWeapon( weapon ) != WUT_HUMANS )
    {
      //shouldn't need a fancy dialog
      trap_SendServerCommand( ent-g_entities, va( "print \"You can't buy alien items\n\"" ) );
      return;
    }

    //are we /allowed/ to buy this?
    if( !BG_FindPurchasableForWeapon( weapon ) )
    {
      trap_SendServerCommand( ent-g_entities, va( "print \"You can't buy this item\n\"" ) );
      return;
    }

    //are we /allowed/ to buy this?
    if( !BG_FindStagesForWeapon( weapon, g_humanStage.integer ) || !BG_WeaponIsAllowed( weapon ) )
    {
      trap_SendServerCommand( ent-g_entities, va( "print \"You can't buy this item\n\"" ) );
      return;
    }

    //add to inventory
    BG_AddWeaponToInventory( weapon, ent->client->ps.stats );
    BG_FindAmmoForWeapon( weapon, &maxAmmo, &maxClips );

    if( BG_FindUsesEnergyForWeapon( weapon ) &&
        BG_InventoryContainsUpgrade( UP_BATTPACK, ent->client->ps.stats ) )
      maxAmmo = (int)( (float)maxAmmo * BATTPACK_MODIFIER );

    BG_PackAmmoArray( weapon, ent->client->ps.ammo, ent->client->ps.powerups,
                      maxAmmo, maxClips );

    G_ForceWeaponChange( ent, weapon );

    //set build delay/pounce etc to 0
    ent->client->ps.stats[ STAT_MISC ] = 0;

    //subtract from funds
    G_AddCreditToClient( ent->client, -(short)BG_FindPriceForWeapon( weapon ), qfalse );
  }
  else if( upgrade != UP_NONE )
  {
    //already got this?
    if( BG_InventoryContainsUpgrade( upgrade, ent->client->ps.stats ) )
    {
      G_TriggerMenu( ent->client->ps.clientNum, MN_H_ITEMHELD );
      return;
    }

    //can afford this?
    if( BG_FindPriceForUpgrade( upgrade ) > (short)ent->client->ps.persistant[ PERS_CREDIT ] )
    {
      G_TriggerMenu( ent->client->ps.clientNum, MN_H_NOFUNDS );
      return;
    }

    //have space to carry this?
    if( BG_FindSlotsForUpgrade( upgrade ) & ent->client->ps.stats[ STAT_SLOTS ] )
    {
      G_TriggerMenu( ent->client->ps.clientNum, MN_H_NOSLOTS );
      return;
    }

    if( BG_FindTeamForUpgrade( upgrade ) != WUT_HUMANS )
    {
      //shouldn't need a fancy dialog
      trap_SendServerCommand( ent-g_entities, va( "print \"You can't buy alien items\n\"" ) );
      return;
    }

    //are we /allowed/ to buy this?
    if( !BG_FindPurchasableForUpgrade( upgrade ) )
    {
      trap_SendServerCommand( ent-g_entities, va( "print \"You can't buy this item\n\"" ) );
      return;
    }

    //are we /allowed/ to buy this?
    if( !BG_FindStagesForUpgrade( upgrade, g_humanStage.integer ) || !BG_UpgradeIsAllowed( upgrade ) )
    {
      trap_SendServerCommand( ent-g_entities, va( "print \"You can't buy this item\n\"" ) );
      return;
    }

    if( upgrade == UP_BATTLESUIT && ent->client->ps.pm_flags & PMF_DUCKED )
    {
      trap_SendServerCommand( ent-g_entities,
        va( "print \"You can't buy this item while crouching\n\"" ) );
      return;
    }

    if( upgrade == UP_AMMO )
      G_GiveClientMaxAmmo( ent, buyingEnergyAmmo );
    else
    {
      //add to inventory
      BG_AddUpgradeToInventory( upgrade, ent->client->ps.stats );
    }

    if( upgrade == UP_BATTPACK )
      G_GiveClientMaxAmmo( ent, qtrue );

    //subtract from funds
    G_AddCreditToClient( ent->client, -(short)BG_FindPriceForUpgrade( upgrade ), qfalse );
  }
  else
  {
    trap_SendServerCommand( ent-g_entities, va( "print \"Unknown item\n\"" ) );
  }

  if( trap_Argc( ) >= 2 )
  {
    trap_Argv( 2, s, sizeof( s ) );

    //retrigger the armoury menu
    if( !Q_stricmp( s, "retrigger" ) )
      ent->client->retriggerArmouryMenu = level.framenum + RAM_FRAMES;
  }

  //update ClientInfo
  ClientUserinfoChanged( ent->client->ps.clientNum );
}


/*
=================
Cmd_Sell_f
=================
*/
void Cmd_Sell_f( gentity_t *ent )
{
  char      s[ MAX_TOKEN_CHARS ];
  int       i;
  int       weapon, upgrade;

  trap_Argv( 1, s, sizeof( s ) );

  //no armoury nearby
  if( !G_BuildableRange( ent->client->ps.origin, 100, BA_H_ARMOURY ) )
  {
    trap_SendServerCommand( ent-g_entities, va( "print \"You must be near a powered armoury\n\"" ) );
    return;
  }

  weapon = BG_FindWeaponNumForName( s );
  upgrade = BG_FindUpgradeNumForName( s );

  if( weapon != WP_NONE )
  {
    //are we /allowed/ to sell this?
    if( !BG_FindPurchasableForWeapon( weapon ) )
    {
      trap_SendServerCommand( ent-g_entities, va( "print \"You can't sell this weapon\n\"" ) );
      return;
    }

    //remove weapon if carried
    if( BG_InventoryContainsWeapon( weapon, ent->client->ps.stats ) )
    {
      //guard against selling the HBUILD weapons exploit
      if( ( weapon == WP_HBUILD || weapon == WP_HBUILD2 ) &&
          ent->client->ps.stats[ STAT_MISC ] > 0 )
      {
        trap_SendServerCommand( ent-g_entities, va( "print \"Cannot sell until build timer expires\n\"" ) );
        return;
      }

      BG_RemoveWeaponFromInventory( weapon, ent->client->ps.stats );

      //add to funds
      G_AddCreditToClient( ent->client, (short)BG_FindPriceForWeapon( weapon ), qfalse );
    }

    //if we have this weapon selected, force a new selection
    if( weapon == ent->client->ps.weapon )
      G_ForceWeaponChange( ent, WP_NONE );
  }
  else if( upgrade != UP_NONE )
  {
    //are we /allowed/ to sell this?
    if( !BG_FindPurchasableForUpgrade( upgrade ) )
    {
      trap_SendServerCommand( ent-g_entities, va( "print \"You can't sell this item\n\"" ) );
      return;
    }
    //remove upgrade if carried
    if( BG_InventoryContainsUpgrade( upgrade, ent->client->ps.stats ) )
    {
      BG_RemoveUpgradeFromInventory( upgrade, ent->client->ps.stats );

      if( upgrade == UP_BATTPACK )
        G_GiveClientMaxAmmo( ent, qtrue );

      //add to funds
      G_AddCreditToClient( ent->client, (short)BG_FindPriceForUpgrade( upgrade ), qfalse );
    }
  }
  else if( !Q_stricmp( s, "weapons" ) )
  {
    for( i = WP_NONE + 1; i < WP_NUM_WEAPONS; i++ )
    {
      //guard against selling the HBUILD weapons exploit
      if( ( i == WP_HBUILD || i == WP_HBUILD2 ) &&
          ent->client->ps.stats[ STAT_MISC ] > 0 )
      {
        trap_SendServerCommand( ent-g_entities, va( "print \"Cannot sell until build timer expires\n\"" ) );
        continue;
      }

      if( BG_InventoryContainsWeapon( i, ent->client->ps.stats ) &&
          BG_FindPurchasableForWeapon( i ) )
      {
        BG_RemoveWeaponFromInventory( i, ent->client->ps.stats );

        //add to funds
        G_AddCreditToClient( ent->client, (short)BG_FindPriceForWeapon( i ), qfalse );
      }

      //if we have this weapon selected, force a new selection
      if( i == ent->client->ps.weapon )
        G_ForceWeaponChange( ent, WP_NONE );
    }
  }
  else if( !Q_stricmp( s, "upgrades" ) )
  {
    for( i = UP_NONE + 1; i < UP_NUM_UPGRADES; i++ )
    {
      //remove upgrade if carried
      if( BG_InventoryContainsUpgrade( i, ent->client->ps.stats ) &&
          BG_FindPurchasableForUpgrade( i ) )
      {
        BG_RemoveUpgradeFromInventory( i, ent->client->ps.stats );

        if( i == UP_BATTPACK )
        {
          int j;

          //remove energy
          for( j = WP_NONE; j < WP_NUM_WEAPONS; j++ )
          {
            if( BG_InventoryContainsWeapon( j, ent->client->ps.stats ) &&
                BG_FindUsesEnergyForWeapon( j ) &&
                !BG_FindInfinteAmmoForWeapon( j ) )
            {
              BG_PackAmmoArray( j, ent->client->ps.ammo, ent->client->ps.powerups, 0, 0 );
            }
          }
        }

        //add to funds
        G_AddCreditToClient( ent->client, (short)BG_FindPriceForUpgrade( i ), qfalse );
      }
    }
  }
  else
    trap_SendServerCommand( ent-g_entities, va( "print \"Unknown item\n\"" ) );

  if( trap_Argc( ) >= 2 )
  {
    trap_Argv( 2, s, sizeof( s ) );

    //retrigger the armoury menu
    if( !Q_stricmp( s, "retrigger" ) )
      ent->client->retriggerArmouryMenu = level.framenum + RAM_FRAMES;
  }

  //update ClientInfo
  ClientUserinfoChanged( ent->client->ps.clientNum );
}


/*
=================
Cmd_Build_f
=================
*/
void Cmd_Build_f( gentity_t *ent )
{
  char          s[ MAX_TOKEN_CHARS ];
  buildable_t   buildable;
  float         dist;
  vec3_t        origin;
  pTeam_t       team;

  if( ent->client->pers.denyBuild )
  {
    trap_SendServerCommand( ent-g_entities,
      "print \"Your building rights have been revoked\n\"" );
    return;
  }

  trap_Argv( 1, s, sizeof( s ) );

  buildable = BG_FindBuildNumForName( s );


  if( g_suddenDeath.integer)
  {
    if( g_suddenDeathMode.integer == SDMODE_SELECTIVE )
    {
      if( !BG_FindReplaceableTestForBuildable( buildable ) )
      {
        trap_SendServerCommand( ent-g_entities,
          "print \"This building type cannot be rebuilt during Sudden Death\n\"" );
        return;
      }
      if( G_BuildingExists( buildable ) )
      {
        trap_SendServerCommand( ent-g_entities,
          "print \"You can only rebuild one of each type of rebuildable building during Sudden Death.\n\"" );
        return;
      }
    }
    else if( g_suddenDeathMode.integer == SDMODE_NO_BUILD )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"Building is not allowed during Sudden Death\n\"" );
      return;
    }
  }

  team = ent->client->ps.stats[ STAT_PTEAM ];

  if( buildable != BA_NONE &&
      ( ( 1 << ent->client->ps.weapon ) & BG_FindBuildWeaponForBuildable( buildable ) ) &&
      !( ent->client->ps.stats[ STAT_STATE ] & SS_INFESTING ) &&
      !( ent->client->ps.stats[ STAT_STATE ] & SS_HOVELING ) &&
      BG_BuildableIsAllowed( buildable ) &&
      ( ( team == PTE_ALIENS && BG_FindStagesForBuildable( buildable, g_alienStage.integer ) ) ||
        ( team == PTE_HUMANS && BG_FindStagesForBuildable( buildable, g_humanStage.integer ) ) ) )
  {
    dist = BG_FindBuildDistForClass( ent->client->ps.stats[ STAT_PCLASS ] );

    //these are the errors displayed when the builder first selects something to use
    switch( G_CanBuild( ent, buildable, dist, origin ) )
    {
      case IBE_NONE:
      case IBE_TNODEWARN:
      case IBE_RPTWARN:
      case IBE_RPTWARN2:
      case IBE_SPWNWARN:
      case IBE_NOROOM:
      case IBE_NORMAL:
      case IBE_HOVELEXIT:
        ent->client->ps.stats[ STAT_BUILDABLE ] = ( buildable | SB_VALID_TOGGLEBIT );
        break;

      case IBE_NOASSERT:
        G_TriggerMenu( ent->client->ps.clientNum, MN_A_NOASSERT );
        break;

      case IBE_NOOVERMIND:
        G_TriggerMenu( ent->client->ps.clientNum, MN_A_NOOVMND );
        break;

      /*ROTAXfun !TADY CHYB9 TEN HOVEL, proto to nic nehlasi kdyz chci v sd stavet hovel (normalne) - vraceno
      case IBE_OVERMIND_AFTER_SD://ROTAXfun
        trap_SendServerCommand( ent-g_entities,
          "print \"You can build only one Overmind in sudden death\n\"" );
        break;*/
        
      case IBE_OVERMIND:
        G_TriggerMenu( ent->client->ps.clientNum, MN_A_OVERMIND );
        break;

      case IBE_HOVEL_AFTER_SD://ROTAXfun
        trap_SendServerCommand( ent-g_entities,
          "print \"You can build only one Hovel in sudden death\n\"" );
        break;

      case IBE_REACTOR:
        G_TriggerMenu( ent->client->ps.clientNum, MN_H_REACTOR );
        break;
        
      /*ROTAXfun vraceno
      case IBE_RC_AFTER_SD://ROTAXfun
        trap_SendServerCommand( ent-g_entities,
          "print \"You can build only one Reactor in sudden death\n\"" );
        break; */

      case IBE_REPEATER:
        G_TriggerMenu( ent->client->ps.clientNum, MN_H_REPEATER );
        break;

      case IBE_NOPOWER:
        G_TriggerMenu( ent->client->ps.clientNum, MN_H_NOPOWER );
        break;

      case IBE_NOCREEP:
        G_TriggerMenu( ent->client->ps.clientNum, MN_A_NOCREEP );
        break;

      case IBE_NODCC:
        G_TriggerMenu( ent->client->ps.clientNum, MN_H_NODCC );
        break;

      default:
        break;
    }
  }
  else
    trap_SendServerCommand( ent-g_entities, va( "print \"Cannot build this item\n\"" ) );
}


/*
=================
Cmd_Boost_f
=================
*/
void Cmd_Boost_f( gentity_t *ent )
{
  if( BG_InventoryContainsUpgrade( UP_JETPACK, ent->client->ps.stats ) &&
      BG_UpgradeIsActive( UP_JETPACK, ent->client->ps.stats ) )
    return;

  if( ent->client->pers.cmd.buttons & BUTTON_WALKING )
    return;

  if( ( ent->client->ps.stats[ STAT_PTEAM ] == PTE_HUMANS ) &&
      ( ent->client->ps.stats[ STAT_STAMINA ] > 0 ) )
    ent->client->ps.stats[ STAT_STATE ] |= SS_SPEEDBOOST;
}

/*
=================
Cmd_Protect_f
=================
*/
void Cmd_Protect_f( gentity_t *ent )
{
  vec3_t      forward, end;
  trace_t     tr;
  gentity_t   *traceEnt;

  if( !ent->client->pers.designatedBuilder )
  {
    trap_SendServerCommand( ent-g_entities, "print \"Only designated"
        " builders can toggle structure protection.\n\"" );
    return;
  }

  AngleVectors( ent->client->ps.viewangles, forward, NULL, NULL );
  VectorMA( ent->client->ps.origin, 100, forward, end );

  trap_Trace( &tr, ent->client->ps.origin, NULL, NULL, end, ent->s.number,
    MASK_PLAYERSOLID );
  traceEnt = &g_entities[ tr.entityNum ];

  if( tr.fraction < 1.0f && ( traceEnt->s.eType == ET_BUILDABLE ) &&
      ( traceEnt->biteam == ent->client->pers.teamSelection ) )
  {
    if( traceEnt->s.eFlags & EF_DBUILDER )
    {
      trap_SendServerCommand( ent-g_entities,
        "print \"Structure protection removed\n\"" );
      traceEnt->s.eFlags &= ~EF_DBUILDER;
    }
    else
    {
      trap_SendServerCommand( ent-g_entities, 
        "print \"Structure protection applied\n\"" );
      traceEnt->s.eFlags |= EF_DBUILDER;
    }
  }
}

 /*
 =================
 Cmd_Resign_f
 =================
 */
 void Cmd_Resign_f( gentity_t *ent )
 {
   if( !ent->client->pers.designatedBuilder )
   {
     trap_SendServerCommand( ent-g_entities,
       "print \"You are not a designated builder\n\"" );
     return;
   }
 
   ent->client->pers.designatedBuilder = qfalse;
   trap_SendServerCommand( -1, va(
     "print \"%s" S_COLOR_WHITE " has resigned\n\"",
     ent->client->pers.netname ) );
   G_CheckDBProtection( );
 }
 
 

/*
=================
Cmd_Reload_f
=================
*/
void Cmd_Reload_f( gentity_t *ent )
{
  if( ( ent->client->ps.weapon >= WP_ABUILD ) &&
    ( ent->client->ps.weapon <= WP_HBUILD ) )
  {
    Cmd_Protect_f( ent );
  }
  else if( ent->client->ps.weaponstate != WEAPON_RELOADING )
    ent->client->ps.pm_flags |= PMF_WEAPON_RELOAD;
}


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

   if(!ent) return;


   if( !level.intermissiontime && ent->client->pers.statscounters.timeLastViewed && (level.time - ent->client->pers.statscounters.timeLastViewed) <60000 ) 
   {   
     ADMP( "You may only check your stats once per minute and during intermission.\n");
     return;
   }
   
   if( !g_myStats.integer )
   {
    ADMP( "myStats has been disabled\n");
    return;
   }
   
   ADMP( G_statsString( &ent->client->pers.statscounters, &ent->client->pers.teamSelection ) );
   ent->client->pers.statscounters.timeLastViewed = level.time;
  
  return;
}

char *G_statsString( statsCounters_t *sc, pTeam_t *pt )
{
  char *s;
  
  int percentNearBase=0;
  int percentJetpackWallwalk=0;
  int percentHeadshots=0;
  double avgTimeAlive=0;
  int avgTimeAliveMins = 0;
  int avgTimeAliveSecs = 0;

  if( sc->timealive )
   percentNearBase = (int)(100 *  (float) sc->timeinbase / ((float) (sc->timealive ) ) );

  if( sc->timealive && sc->deaths )
  {
    avgTimeAlive = sc->timealive / sc->deaths;
  }

  avgTimeAliveMins = (int) (avgTimeAlive / 60.0f);
  avgTimeAliveSecs = (int) (avgTimeAlive - (60.0f * avgTimeAliveMins));
  
  if( *pt == PTE_ALIENS )
  {
    if( sc->dretchbasytime > 0 )
     percentJetpackWallwalk = (int)(100 *  (float) sc->jetpackusewallwalkusetime / ((float) ( sc->dretchbasytime) ) );
    
    if( sc->hitslocational )
      percentHeadshots = (int)(100 * (float) sc->headshots / ((float) (sc->hitslocational) ) );
    
    s = va( "^3Kills:^7 %3i ^3StructKills:^7 %3i ^3Assists:^7 %3i^7 ^3Poisons:^7 %3i ^3Headshots:^7 %3i (%3i)\n^3Deaths:^7 %3i ^3Feeds:^7 %3i ^3Suicides:^7 %3i ^3TKs:^7 %3i ^3Avg Lifespan:^7 %4d:%02d\n^3Damage to:^7 ^3Enemies:^7 %5i ^3Structs:^7 %5i ^3Friendlies:^7 %3i \n^3Structs Built:^7 %3i ^3Time Near Base:^7 %3i ^3Time wallwalking:^7 %3i\n",
     sc->kills,
     sc->structskilled,
     sc->assists,
     sc->repairspoisons,
     sc->headshots,
     percentHeadshots,
     sc->deaths,
     sc->feeds,
     sc->suicides,
     sc->teamkills,
     avgTimeAliveMins,
     avgTimeAliveSecs,
     sc->dmgdone,
     sc->structdmgdone,
     sc->ffdmgdone,
     sc->structsbuilt,
     percentNearBase,
     percentJetpackWallwalk
         );
  }
  else if( *pt == PTE_HUMANS )
  {
    if( sc->timealive )
     percentJetpackWallwalk = (int)(100 *  (float) sc->jetpackusewallwalkusetime / ((float) ( sc->timealive ) ) );
    s = va( "^3Kills:^7 %3i ^3StructKills:^7 %3i ^3Assists:^7 %3i \n^3Deaths:^7 %3i ^3Feeds:^7 %3i ^3Suicides:^7 %3i ^3TKs:^7 %3i ^3Avg Lifespan:^7 %4d:%02d\n^3Damage to:^7 ^3Enemies:^7 %5i ^3Structs:^7 %5i ^3Friendlies:^7 %3i \n^3Structs Built:^7 %3i ^3Repairs:^7 %4i ^3Time Near Base:^7 %3i ^3Time Jetpacking:^7 %3i\n",
     sc->kills,
     sc->structskilled,
     sc->assists,
     sc->deaths,
     sc->feeds,
     sc->suicides,
     sc->teamkills,
     avgTimeAliveMins,
     avgTimeAliveSecs,
     sc->dmgdone,
     sc->structdmgdone,
     sc->ffdmgdone,
     sc->structsbuilt,
     sc->repairspoisons,
     percentNearBase,
     percentJetpackWallwalk
         );
  }
  else s="No stats available\n";

  return s;
}



/*
=================
G_StopFromFollowing

stops any other clients from following this one
called when a player leaves a team or dies
=================
*/
void G_StopFromFollowing( gentity_t *ent )
{
  int i;

  for( i = 0; i < level.maxclients; i++ )
  {
    if( level.clients[ i ].sess.spectatorState == SPECTATOR_FOLLOW &&
        level.clients[ i ].sess.spectatorClient == ent-g_entities )
    {
      if( !G_FollowNewClient( &g_entities[ i ], 1 ) )
        G_StopFollowing( &g_entities[ i ] );
    }
  }
}

/*
=================
G_StopFollowing

If the client being followed leaves the game, or you just want to drop
to free floating spectator mode
=================
*/
void G_StopFollowing( gentity_t *ent )
{
  ent->client->ps.persistant[ PERS_TEAM ] = TEAM_SPECTATOR;
  ent->client->sess.sessionTeam = TEAM_SPECTATOR;
  ent->client->ps.stats[ STAT_PTEAM ] = ent->client->pers.teamSelection;

  if( ent->client->pers.teamSelection == PTE_NONE )
  {
    ent->client->sess.spectatorState = SPECTATOR_FREE;
    ent->client->ps.pm_type = PM_SPECTATOR;
    ent->client->ps.stats[ STAT_HEALTH ] = 100; // hacky server-side fix to prevent cgame from viewlocking a freespec
  }
  else
  {
    vec3_t   spawn_origin, spawn_angles;

    ent->client->sess.spectatorState = SPECTATOR_LOCKED;
    if( ent->client->pers.teamSelection == PTE_ALIENS )
      G_SelectAlienLockSpawnPoint( spawn_origin, spawn_angles );
    else if( ent->client->pers.teamSelection == PTE_HUMANS )
      G_SelectHumanLockSpawnPoint( spawn_origin, spawn_angles );
    G_SetOrigin( ent, spawn_origin );
    VectorCopy( spawn_origin, ent->client->ps.origin );
    G_SetClientViewAngle( ent, spawn_angles );
  }
  ent->client->sess.spectatorClient = -1;
  ent->client->ps.pm_flags &= ~PMF_FOLLOW;

  // Prevent spawning with bsuit in rare case
  if( BG_InventoryContainsUpgrade( UP_BATTLESUIT, ent->client->ps.stats ) )
    BG_RemoveUpgradeFromInventory( UP_BATTLESUIT, ent->client->ps.stats );

  ent->client->ps.stats[ STAT_STATE ] &= ~SS_WALLCLIMBING;
  ent->client->ps.stats[ STAT_STATE ] &= ~SS_WALLCLIMBINGCEILING;
  ent->client->ps.eFlags &= ~EF_WALLCLIMB;
  ent->client->ps.viewangles[ PITCH ] = 0.0f;

  ent->client->ps.clientNum = ent - g_entities;

  CalculateRanks( );
}

/*
=================
G_FollowNewClient

This was a really nice, elegant function. Then I fucked it up.
=================
*/
qboolean G_FollowNewClient( gentity_t *ent, int dir )
{
  int       clientnum = ent->client->sess.spectatorClient;
  int       original = clientnum;
  qboolean  selectAny = qfalse;

  if( dir > 1 )
    dir = 1;
  else if( dir < -1 )
    dir = -1;
  else if( dir == 0 )
    return qtrue;

  if( ent->client->sess.sessionTeam != TEAM_SPECTATOR )
    return qfalse;

  // select any if no target exists
  if( clientnum < 0 || clientnum >= level.maxclients )
  {
    clientnum = original = 0;
    selectAny = qtrue;
  }

  do
  {
    clientnum += dir;

    if( clientnum >= level.maxclients )
      clientnum = 0;

    if( clientnum < 0 )
      clientnum = level.maxclients - 1;

    // avoid selecting existing follow target
    if( clientnum == original && !selectAny )
      continue; //effectively break;

    // can't follow self
    if( &level.clients[ clientnum ] == ent->client )
      continue;

    // can only follow connected clients
    if( level.clients[ clientnum ].pers.connected != CON_CONNECTED &&
        !level.clients[ clientnum ].pers.demoClient )//rotax
      continue;

    // can't follow another spectator
     if( level.clients[ clientnum ].pers.teamSelection == PTE_NONE &&
        !level.clients[ clientnum ].pers.demoClient )//rotax
       continue;
     
      // can only follow teammates when dead and on a team
     if( ent->client->pers.teamSelection != PTE_NONE && 
         ( level.clients[ clientnum ].pers.teamSelection != 
           ent->client->pers.teamSelection ) )
       continue;
     
     // cannot follow a teammate who is following you
     if( level.clients[ clientnum ].sess.spectatorState == SPECTATOR_FOLLOW && 
         ( level.clients[ clientnum ].sess.spectatorClient == ent->s.number ) )
       continue;

    // this is good, we can use it
    ent->client->sess.spectatorClient = clientnum;
    ent->client->sess.spectatorState = SPECTATOR_FOLLOW;
    return qtrue;

  } while( clientnum != original );

  return qfalse;
}

/*
=================
G_ToggleFollow
=================
*/
void G_ToggleFollow( gentity_t *ent )
{
  if( ent->client->sess.spectatorState == SPECTATOR_FOLLOW )
    G_StopFollowing( ent );
  else
    G_FollowNewClient( ent, 1 );
}

/*
=================
Cmd_Follow_f
=================
*/
void Cmd_Follow_f( gentity_t *ent )
{
  int   i;
  int   pids[ MAX_CLIENTS ];
  char  arg[ MAX_TOKEN_CHARS ];

  if( ent->client->sess.sessionTeam != TEAM_SPECTATOR )
  {
    trap_SendServerCommand( ent - g_entities, "print \"follow: You cannot follow unless you are dead or on the spectators.\n\"" );
    return;
  }

  if( trap_Argc( ) != 2 )
  {
    G_ToggleFollow( ent );
  }
  else
  {
    trap_Argv( 1, arg, sizeof( arg ) );
    if( G_ClientNumbersFromString( arg, pids ) == 1 )
    {
      i = pids[ 0 ];
    }
    else
    {
      i = G_ClientNumberFromString( ent, arg );

      if( i == -1 )
      {
        trap_SendServerCommand( ent - g_entities,
          "print \"follow: invalid player\n\"" );
        return;
      }
    }

    // can't follow self
    if( &level.clients[ i ] == ent->client )
    {
      trap_SendServerCommand( ent - g_entities, "print \"follow: You cannot follow yourself.\n\"" );
      return;
    }

    // can't follow another spectator
    if( level.clients[ i ].pers.teamSelection == PTE_NONE)
    {
      trap_SendServerCommand( ent - g_entities, "print \"follow: You cannot follow another spectator.\n\"" );
      return;
    }

    // can only follow teammates when dead and on a team
    if( ent->client->pers.teamSelection != PTE_NONE && 
        ( level.clients[ i ].pers.teamSelection != 
          ent->client->pers.teamSelection ) )
    {
      trap_SendServerCommand( ent - g_entities, "print \"follow: You can only follow teammates, and only when you are dead.\n\"" );
      return;
    }

    ent->client->sess.spectatorState = SPECTATOR_FOLLOW;
    ent->client->sess.spectatorClient = i;
  }
}

/*
=================
Cmd_FollowCycle_f
=================
*/
void Cmd_FollowCycle_f( gentity_t *ent )
{
  char args[ 11 ];
  int  dir = 1;

  trap_Argv( 0, args, sizeof( args ) );
  if( Q_stricmp( args, "followprev" ) == 0 )
    dir = -1;

  // won't work unless spectating
   if( ent->client->sess.sessionTeam != TEAM_SPECTATOR )
     return;
   if( ent->client->sess.spectatorState == SPECTATOR_NOT )
     return;
  G_FollowNewClient( ent, dir );
}

/*
=================
Cmd_PTRCVerify_f

Check a PTR code is valid
=================
*/
void Cmd_PTRCVerify_f( gentity_t *ent )
{
  connectionRecord_t  *connection;
  char                s[ MAX_TOKEN_CHARS ] = { 0 };
  int                 code;

  if( ent->client->pers.connection )
    return;

  trap_Argv( 1, s, sizeof( s ) );

  if( !strlen( s ) )
    return;

  code = atoi( s );

  connection = G_FindConnectionForCode( code );
  //rotax - zakaz automatickeho pripojeni do teamu po odpojeni/kicku kdyz je nektery team zamknut.
  //if( connection && connection->clientNum == -1 )
  if( (connection && connection->clientNum == -1) && (!level.humanTeamLocked && !level.alienTeamLocked) )
  {
    // valid code
    if( connection->clientTeam != PTE_NONE )
      trap_SendServerCommand( ent->client->ps.clientNum, "ptrcconfirm" );

    // restore mapping
    ent->client->pers.connection = connection;
    connection->clientNum = ent->client->ps.clientNum;
  }
  else
  {
    // invalid code -- generate a new one
    connection = G_GenerateNewConnection( ent->client );

    if( connection )
    {
      trap_SendServerCommand( ent->client->ps.clientNum,
        va( "ptrcissue %d", connection->ptrCode ) );
    }
  }
}

/*
=================
Cmd_PTRCRestore_f

Restore against a PTR code
=================
*/
void Cmd_PTRCRestore_f( gentity_t *ent )
{
  char                s[ MAX_TOKEN_CHARS ] = { 0 };
  int                 code;
  connectionRecord_t  *connection;

  if( ent->client->pers.joinedATeam )
  {
    trap_SendServerCommand( ent - g_entities,
      "print \"You cannot use a PTR code after joining a team\n\"" );
    return;
  }

  trap_Argv( 1, s, sizeof( s ) );

  if( !strlen( s ) )
    return;

  code = atoi( s );

  connection = ent->client->pers.connection;
  if( connection && connection->ptrCode == code )
  {
    // set the correct team
    G_ChangeTeam( ent, connection->clientTeam );

    // set the correct credit
    ent->client->ps.persistant[ PERS_CREDIT ] = 0;
    G_AddCreditToClient( ent->client, connection->clientCredit, qtrue );
  }
  else
  {
    trap_SendServerCommand( ent - g_entities,
      va( "print \"\"%d\" is not a valid PTR code\n\"", code ) );
  }
}

static void Cmd_Ignore_f( gentity_t *ent )
{
  int pids[ MAX_CLIENTS ];
  char name[ MAX_NAME_LENGTH ];
  char cmd[ 9 ];
  int matches = 0;
  int i;
  qboolean ignore = qfalse;

  trap_Argv( 0, cmd, sizeof( cmd ) );
  if( Q_stricmp( cmd, "ignore" ) == 0 )
    ignore = qtrue;

  if( trap_Argc() < 2 )
  {
    trap_SendServerCommand( ent-g_entities, va( "print \"[skipnotify]"
      "%s: usage \\%s [clientNum | partial name match]\n\"", cmd, cmd ) );
    return;
  }

  Q_strncpyz( name, ConcatArgs( 1 ), sizeof( name ) );
  matches = G_ClientNumbersFromString( name, pids ); 
  if( matches < 1 )
  {
    trap_SendServerCommand( ent-g_entities, va( "print \"[skipnotify]"
      "%s: no clients match the name '%s'\n\"", cmd, name ) );
    return;
  }

  for( i = 0; i < matches; i++ )
  {
    if( ignore )
    {
      if( !BG_ClientListTest( &ent->client->sess.ignoreList, pids[ i ] ) )
      {
        BG_ClientListAdd( &ent->client->sess.ignoreList, pids[ i ] );
        ClientUserinfoChanged( ent->client->ps.clientNum );
        trap_SendServerCommand( ent-g_entities, va( "print \"[skipnotify]"
          "ignore: added %s^7 to your ignore list\n\"",
          level.clients[ pids[ i ] ].pers.netname ) );
      }
      else
      {
        trap_SendServerCommand( ent-g_entities, va( "print \"[skipnotify]"
          "ignore: %s^7 is already on your ignore list\n\"",
          level.clients[ pids[ i ] ].pers.netname ) );
      }
    }
    else
    {
      if( BG_ClientListTest( &ent->client->sess.ignoreList, pids[ i ] ) )
      {
        BG_ClientListRemove( &ent->client->sess.ignoreList, pids[ i ] );
        ClientUserinfoChanged( ent->client->ps.clientNum );
        trap_SendServerCommand( ent-g_entities, va( "print \"[skipnotify]"
          "unignore: removed %s^7 from your ignore list\n\"",
          level.clients[ pids[ i ] ].pers.netname ) );
      }
      else
      {
        trap_SendServerCommand( ent-g_entities, va( "print \"[skipnotify]"
          "unignore: %s^7 is not on your ignore list\n\"",
          level.clients[ pids[ i ] ].pers.netname ) );
      }
    }
  }
}

 /*
 =================
 Cmd_Share_f
 =================
 */
 void Cmd_Share_f( gentity_t *ent )
 {
   int   i, clientNum = 0, creds = 0, skipargs = 0;
   int   clientNums[ MAX_CLIENTS ] = { -1 };
   char  cmd[ 12 ];
   char  arg1[ MAX_STRING_TOKENS ];
   char  arg2[ MAX_STRING_TOKENS ];
   pTeam_t team;
   
   if( !ent || !ent->client || ( ent->client->pers.teamSelection == PTE_NONE ) )
   {
     return;
   }
   
   if( !g_allowShare.integer )
   {
     trap_SendServerCommand( ent-g_entities, "print \"Share has been disabled.\n\"" );
     return;
   }
   
   if( g_floodMinTime.integer )
   if ( G_Flood_Limited( ent ) )
   {
    trap_SendServerCommand( ent-g_entities, "print \"Your chat is flood-limited; wait before chatting again\n\"" );
    return;
   }
 
   team = ent->client->pers.teamSelection;
 
   G_SayArgv( 0, cmd, sizeof( cmd ) );
   if( !Q_stricmp( cmd, "say" ) || !Q_stricmp( cmd, "say_team" ) )
   {
     skipargs = 1;
     G_SayArgv( 1, cmd, sizeof( cmd ) );
   }
 
   // target player name is in arg1
   G_SayArgv( 1+skipargs, arg1, sizeof( arg1 ) );
   // amount to be shared is in arg2
   G_SayArgv( 2+skipargs, arg2, sizeof( arg2 ) );
 
   if( arg1[0] && !strchr( arg1, ';' ) && Q_stricmp( arg1, "target_in_aim" ) )
   {
     //check arg1 is a number
     for( i = 0; arg1[ i ]; i++ )
     {
       if( arg1[ i ] < '0' || arg1[ i ] > '9' )
       {
         clientNum = -1;
         break;
       }
     }
 
     if( clientNum >= 0 )
     {
       clientNum = atoi( arg1 );
     }
     else if( G_ClientNumbersFromString( arg1, clientNums ) == 1 )
     {
       // there was one partial name match
       clientNum = clientNums[ 0 ]; 
     }
     else
     {
       // look for an exact name match before bailing out
       clientNum = G_ClientNumberFromString( ent, arg1 );
       if( clientNum == -1 )
       {
         trap_SendServerCommand( ent-g_entities,
           "print \"share: invalid player name specified.\n\"" );
         return;
       }
     }
   }
   else // arg1 not set
   {
     vec3_t      forward, end;
     trace_t     tr;
     gentity_t   *traceEnt;
 
     
     // trace a teammate
     AngleVectors( ent->client->ps.viewangles, forward, NULL, NULL );
     VectorMA( ent->client->ps.origin, 8192 * 16, forward, end );
 
     trap_Trace( &tr, ent->client->ps.origin, NULL, NULL, end, ent->s.number, MASK_PLAYERSOLID );
     traceEnt = &g_entities[ tr.entityNum ];
 
     if( tr.fraction < 1.0f && traceEnt->client &&
       ( traceEnt->client->pers.teamSelection == team ) )
     {
       clientNum = traceEnt - g_entities;
     }
     else
     {
       trap_SendServerCommand( ent-g_entities,
         va( "print \"share: aim at a teammate to share %s.\n\"",
         ( team == PTE_HUMANS ) ? "credits" : "evolvepoints" ) );
       return;
     }
   }
 
   // verify target player team
   if( ( clientNum < 0 ) || ( clientNum >= level.maxclients ) ||
       ( level.clients[ clientNum ].pers.teamSelection != team ) )
   {
     trap_SendServerCommand( ent-g_entities,
       "print \"share: not a valid player of your team.\n\"" );
     return;
   }
 
   if( !arg2[0] || strchr( arg2, ';' ) )
   {
     // default credit count
     if( team == PTE_HUMANS )
     {
       creds = FREEKILL_HUMAN;
     }
     else if( team == PTE_ALIENS )
     {
       creds = FREEKILL_ALIEN;
     }
   }
   else
   {
     //check arg2 is a number
     for( i = 0; arg2[ i ]; i++ )
     {
       if( arg2[ i ] < '0' || arg2[ i ] > '9' )
       {
         trap_SendServerCommand( ent-g_entities,
           "print \"usage: share [name|slot#] [amount]\n\"" );
         return;
       }
     }
 
     // credit count from parameter
     creds = atoi( arg2 );
   }
 
   // player specified "0" to transfer
   if( creds <= 0 )
   {
     trap_SendServerCommand( ent-g_entities,
       "print \"Ooh, you are a generous one, indeed!\n\"" );
     return;
   }
 
   // transfer only credits the player really has
   if( creds > ent->client->pers.credit )
   {
     creds = ent->client->pers.credit;
   }
 
   // player has no credits
   if( creds <= 0 )
   {
     trap_SendServerCommand( ent-g_entities,
       "print \"Earn some first, lazy gal!\n\"" );
     return;
   }
 
   // allow transfers only up to the credit/evo limit
   if( ( team == PTE_HUMANS ) && 
       ( creds > HUMAN_MAX_CREDITS - level.clients[ clientNum ].pers.credit ) )
   {
     creds = HUMAN_MAX_CREDITS - level.clients[ clientNum ].pers.credit;
   }
   else if( ( team == PTE_ALIENS ) && 
       ( creds > ALIEN_MAX_KILLS - level.clients[ clientNum ].pers.credit ) )
   {
     creds = ALIEN_MAX_KILLS - level.clients[ clientNum ].pers.credit;
   }
 
   // target cannot take any more credits
   if( creds <= 0 )
   {
     trap_SendServerCommand( ent-g_entities,
       va( "print \"share: player cannot receive any more %s.\n\"",
         ( team == PTE_HUMANS ) ? "credits" : "evolvepoints" ) );
     return;
   }
 
   // transfer credits
   G_AddCreditToClient( ent->client, -creds, qfalse );
   trap_SendServerCommand( ent-g_entities,
     va( "print \"share: transferred %d %s to %s^7.\n\"", creds,
       ( team == PTE_HUMANS ) ? "credits" : "evolvepoints",
       level.clients[ clientNum ].pers.netname ) );
   G_AddCreditToClient( &(level.clients[ clientNum ]), creds, qtrue );
   trap_SendServerCommand( clientNum,
     va( "print \"You have received %d %s from %s^7.\n\"", creds,
       ( team == PTE_HUMANS ) ? "credits" : "evolvepoints",
       ent->client->pers.netname ) );
 
   G_LogPrintf( "Share: %i %i %i %d: %s^7 transferred %d%s to %s^7\n",
     ent->client->ps.clientNum,
     clientNum,
     team,
     creds,
     ent->client->pers.netname,
     creds,
     ( team == PTE_HUMANS ) ? "c" : "e",
     level.clients[ clientNum ].pers.netname );
 }
 
 /*
 =================
 Cmd_Donate_f
 
 Alms for the poor
 =================
 */
 void Cmd_Donate_f( gentity_t *ent ) {
   char s[ MAX_TOKEN_CHARS ] = "", *type = "evo(s)";
   int i, value, divisor, portion, new_credits, total=0,
     max = ALIEN_MAX_KILLS, *amounts;
   qboolean donated = qtrue;
 
   if( !ent->client ) return;
 
   if( !g_allowShare.integer )
   {
     trap_SendServerCommand( ent-g_entities, "print \"Donate has been disabled.\n\"" );
     return;
   }
   
  if( g_floodMinTime.integer )
   if ( G_Flood_Limited( ent ) )
   {
    trap_SendServerCommand( ent-g_entities, "print \"Your chat is flood-limited; wait before chatting again\n\"" );
    return;
   }
 
 
   if( ent->client->pers.teamSelection == PTE_ALIENS )
     divisor = level.numAlienClients-1;
   else if( ent->client->pers.teamSelection == PTE_HUMANS ) {
     divisor = level.numHumanClients-1;
     max = HUMAN_MAX_CREDITS;
     type = "credit(s)";
   } else {
     trap_SendServerCommand( ent-g_entities,
       va( "print \"donate: spectators cannot be so gracious\n\"" ) );
     return;
   }
 
   if( divisor < 1 ) {
     trap_SendServerCommand( ent-g_entities,
       "print \"donate: get yourself some teammates first\n\"" );
     return;
   }
 
   trap_Argv( 1, s, sizeof( s ) );
   value = atoi(s);
   if( value <= 0 ) {
     trap_SendServerCommand( ent-g_entities,
       "print \"donate: very funny\n\"" );
     return;
   }
   if( value > ent->client->pers.credit)
     value = ent->client->pers.credit;
 
   // allocate memory for distribution amounts
   amounts = G_Alloc( level.maxclients * sizeof( int ) );
   for( i = 0; i < level.maxclients; i++ ) amounts[ i ] = 0;
 
   // determine donation amounts for each client
   total = value;
   while( donated && value ) {
     donated = qfalse;
     portion = value / divisor;
     if( portion < 1 ) portion = 1;
     for( i = 0; i < level.maxclients; i++ )
       if( level.clients[ i ].pers.connected == CON_CONNECTED &&
            ent->client != level.clients + i &&
            level.clients[ i ].pers.teamSelection ==
            ent->client->pers.teamSelection ) {
         new_credits = level.clients[ i ].pers.credit + portion;
         amounts[ i ] = portion;
         if( new_credits > max ) {
           amounts[ i ] -= new_credits - max;
           new_credits = max;
         }
         if( amounts[ i ] ) {
           G_AddCreditToClient( &(level.clients[ i ]), amounts[ i ], qtrue );
           donated = qtrue;
           value -= amounts[ i ];
           if( value < portion ) break;
         }
       }
   }
 
   // transfer funds
   G_AddCreditToClient( ent->client, value - total, qtrue );
   for( i = 0; i < level.maxclients; i++ )
     if( amounts[ i ] ) {
       trap_SendServerCommand( i,
         va( "print \"%s^7 donated %d %s to you, don't forget to say 'thank you'!\n\"",
         ent->client->pers.netname, amounts[ i ], type ) );
     }
 
   G_Free( amounts );
 
   trap_SendServerCommand( ent-g_entities,
     va( "print \"Donated %d %s to the cause.\n\"",
     total-value, type ) );
 }
 

commands_t cmds[ ] = {
  // normal commands
  { "team", 0, Cmd_Team_f },
  { "vote", 0, Cmd_Vote_f },
  { "ignore", 0, Cmd_Ignore_f },
  { "unignore", 0, Cmd_Ignore_f },

  // communication commands
  { "tell", CMD_MESSAGE, Cmd_Tell_f },
  { "callvote", CMD_MESSAGE, Cmd_CallVote_f },
  { "callteamvote", CMD_MESSAGE|CMD_TEAM, Cmd_CallTeamVote_f },
  { "say_area", CMD_MESSAGE|CMD_TEAM, Cmd_SayArea_f },
  // can be used even during intermission
  { "say", CMD_MESSAGE|CMD_INTERMISSION, Cmd_Say_f },
  { "say_team", CMD_MESSAGE|CMD_INTERMISSION, Cmd_Say_f },
  { "say_admins", CMD_MESSAGE|CMD_INTERMISSION, Cmd_Say_f },
  { "a", CMD_MESSAGE|CMD_INTERMISSION, Cmd_Say_f },
  { "m", CMD_MESSAGE|CMD_INTERMISSION, G_PrivateMessage },
  { "mt", CMD_MESSAGE|CMD_INTERMISSION, G_PrivateMessage },
  { "me", CMD_MESSAGE|CMD_INTERMISSION, Cmd_Say_f },
  { "me_team", CMD_MESSAGE|CMD_INTERMISSION, Cmd_Say_f },

  { "score", CMD_INTERMISSION, ScoreboardMessage },
  { "mystats", CMD_TEAM|CMD_INTERMISSION, Cmd_MyStats_f },

  // cheats
  { "give", CMD_CHEAT|CMD_TEAM|CMD_LIVING, Cmd_Give_f },
  { "god", CMD_CHEAT|CMD_TEAM|CMD_LIVING, Cmd_God_f },
  { "notarget", CMD_CHEAT|CMD_TEAM|CMD_LIVING, Cmd_Notarget_f },
  { "noclip", CMD_CHEAT|CMD_TEAM|CMD_LIVING, Cmd_Noclip_f },
  { "levelshot", CMD_CHEAT, Cmd_LevelShot_f },
  { "setviewpos", CMD_CHEAT, Cmd_SetViewpos_f },
  { "destroy", CMD_CHEAT|CMD_TEAM|CMD_LIVING, Cmd_Destroy_f },

  { "kill", CMD_TEAM|CMD_LIVING, Cmd_Kill_f },

  // game commands
  { "ptrcverify", CMD_NOTEAM, Cmd_PTRCVerify_f },
  { "ptrcrestore", CMD_NOTEAM, Cmd_PTRCRestore_f },

  { "follow", 0, Cmd_Follow_f },
  { "follownext", 0, Cmd_FollowCycle_f },
  { "followprev", 0, Cmd_FollowCycle_f },

  { "where", CMD_TEAM, Cmd_Where_f },
  { "teamvote", CMD_TEAM, Cmd_TeamVote_f },
  { "class", CMD_TEAM, Cmd_Class_f },

  { "build", CMD_TEAM|CMD_LIVING, Cmd_Build_f },
  { "deconstruct", CMD_TEAM|CMD_LIVING, Cmd_Destroy_f },

  { "buy", CMD_HUMAN|CMD_LIVING, Cmd_Buy_f },
  { "sell", CMD_HUMAN|CMD_LIVING, Cmd_Sell_f },
  { "itemact", CMD_HUMAN|CMD_LIVING, Cmd_ActivateItem_f },
  { "itemdeact", CMD_HUMAN|CMD_LIVING, Cmd_DeActivateItem_f },
  { "itemtoggle", CMD_HUMAN|CMD_LIVING, Cmd_ToggleItem_f },
  { "reload", CMD_HUMAN|CMD_LIVING, Cmd_Reload_f },
  { "boost", 0, Cmd_Boost_f },
  { "share", CMD_TEAM, Cmd_Share_f },
  { "donate", CMD_TEAM, Cmd_Donate_f },
  { "protect", CMD_TEAM|CMD_LIVING, Cmd_Protect_f },
  { "resign", CMD_TEAM, Cmd_Resign_f },
  { "builder", 0, Cmd_Builder_f },
  //ROTAX
  { "playsound", CMD_TEAM|CMD_LIVING, Cmd_Playsound_f }
};
static int numCmds = sizeof( cmds ) / sizeof( cmds[ 0 ] );

/*
=================
ClientCommand
=================
*/
void ClientCommand( int clientNum )
{
  gentity_t *ent;
  char      cmd[ MAX_TOKEN_CHARS ];
  int       i;

  ent = g_entities + clientNum;
  if( !ent->client )
    return;   // not fully in game yet

  trap_Argv( 0, cmd, sizeof( cmd ) );

  for( i = 0; i < numCmds; i++ )
  {
    if( Q_stricmp( cmd, cmds[ i ].cmdName ) == 0 )
      break;
  }

  if( i == numCmds )
  {
    if( !G_admin_cmd_check( ent, qfalse ) )
      trap_SendServerCommand( clientNum,
        va( "print \"Unknown command %s\n\"", cmd ) );
    return;
  }

  // do tests here to reduce the amount of repeated code

  if( !( cmds[ i ].cmdFlags & CMD_INTERMISSION ) && ( level.intermissiontime || level.paused ) )
    return;

  if( cmds[ i ].cmdFlags & CMD_CHEAT && !g_cheats.integer )
  {
    trap_SendServerCommand( clientNum,
      "print \"Cheats are not enabled on this server\n\"" );
    return;
  }

  if( cmds[ i ].cmdFlags & CMD_MESSAGE && ent->client->pers.muted )
  {
    trap_SendServerCommand( clientNum,
      "print \"You are muted and cannot use message commands.\n\"" );
    return;
  }

  if( cmds[ i ].cmdFlags & CMD_TEAM &&
      ent->client->pers.teamSelection == PTE_NONE )
  {
    trap_SendServerCommand( clientNum, "print \"Join a team first\n\"" );
    return;
  }

  if( cmds[ i ].cmdFlags & CMD_NOTEAM &&
      ent->client->pers.teamSelection != PTE_NONE )
  {
    trap_SendServerCommand( clientNum,
      "print \"Cannot use this command when on a team\n\"" );
    return;
  }

  if( cmds[ i ].cmdFlags & CMD_ALIEN &&
      ent->client->pers.teamSelection != PTE_ALIENS )
  {
    trap_SendServerCommand( clientNum,
      "print \"Must be alien to use this command\n\"" );
    return;
  }

  if( cmds[ i ].cmdFlags & CMD_HUMAN &&
      ent->client->pers.teamSelection != PTE_HUMANS )
  {
    trap_SendServerCommand( clientNum,
      "print \"Must be human to use this command\n\"" );
    return;
  }

  if( cmds[ i ].cmdFlags & CMD_LIVING &&
    ( ent->client->ps.stats[ STAT_HEALTH ] <= 0 ||
      ent->client->sess.sessionTeam == TEAM_SPECTATOR ) )
  {
    trap_SendServerCommand( clientNum,
      "print \"Must be living to use this command\n\"" );
    return;
  }

  cmds[ i ].cmdHandler( ent );
}

int G_SayArgc( void )
{
  int c = 0;
  char *s;

  s = ConcatArgs( 0 );
  while( 1 )
  {
    while( *s == ' ' )
      s++;
    if( !*s )
      break;
    c++;
    while( *s && *s != ' ' )
      s++;
  }
  return c;
}

qboolean G_SayArgv( int n, char *buffer, int bufferLength )
{
  int bc = 0;
  int c = 0;
  char *s;

  if( bufferLength < 1 )
    return qfalse;
  if( n < 0 )
    return qfalse;
  s = ConcatArgs( 0 );
  while( c < n )
  {
    while( *s == ' ' )
      s++;
    if( !*s )
      break;
    c++;
    while( *s && *s != ' ' )
      s++;
  }
  if( c < n )
    return qfalse;
  while( *s == ' ' )
    s++;
  if( !*s )
    return qfalse;
  //memccpy( buffer, s, ' ', bufferLength );
  while( bc < bufferLength - 1 && *s && *s != ' ' )
    buffer[ bc++ ] = *s++;
  buffer[ bc ] = 0;
  return qtrue;
}

char *G_SayConcatArgs( int start )
{
  char *s;
  int c = 0;

  s = ConcatArgs( 0 );
  while( c < start )
  {
    while( *s == ' ' )
      s++;
    if( !*s )
      break;
    c++;
    while( *s && *s != ' ' )
      s++;
  }
  while( *s == ' ' )
    s++;
    
  //ROTAX
  fixLineSpaces(s,s);
    
  return s;
}

void G_DecolorString( char *in, char *out )
{   
  while( *in ) {
    if( *in == 27 || *in == '^' ) {
      in++;
      if( *in )
        in++;
      continue;
    }
    *out++ = *in++;
  }
  *out = '\0';
}

void G_ParseEscapedString( char *buffer )
{
  int i = 0;
  int j = 0;

  while( buffer[i] )
  {
    if(!buffer[i]) break;

    if(buffer[i] == '\\')
    {
      if(buffer[i + 1] == '\\')
        buffer[j] = buffer[++i];
      else if(buffer[i + 1] == 'n')
      {
        buffer[j] = '\n';
        i++;
      }
      else
        buffer[j] = buffer[i];
    }
    else
      buffer[j] = buffer[i];

    i++;
    j++;
  }
  buffer[j] = 0;
}

void G_WordWrap( char *buffer, int maxwidth )
{
  char out[ MAX_STRING_CHARS ];
  int i = 0;
  int j = 0;
  int k;
  int linecount = 0;
  int currentcolor = 7;

  while ( buffer[ j ]!='\0' )
  {
     if( i == ( MAX_STRING_CHARS - 1 ) )
       break;

     //If it's the start of a new line, copy over the color code,
     //but not if we already did it, or if the text at the start of the next line is also a color code
     if( linecount == 0 && i>2 && out[ i-2 ] != Q_COLOR_ESCAPE && out[ i-1 ] != Q_COLOR_ESCAPE )
     {
       out[ i ] = Q_COLOR_ESCAPE;
       out[ i + 1 ] = '0' + currentcolor; 
       i+=2;
       continue;
     }

     if( linecount < maxwidth )
     {
       out[ i ] = buffer[ j ];
       if( out[ i ] == '\n' ) 
       {
         linecount = 0;
       }
       else if( Q_IsColorString( &buffer[j] ) )
       {
         currentcolor = buffer[j+1] - '0';
       }
       else
         linecount++;
       
       //If we're at a space and getting close to a line break, look ahead and make sure that there isn't already a \n or a closer space coming. If not, break here.
      if( out[ i ] == ' ' && linecount >= (maxwidth - 10 ) ) 
      {
        qboolean foundbreak = qfalse;
        for( k = i+1; k < maxwidth; k++ )
        {
          if( !buffer[ k ] )
            continue;
          if( buffer[ k ] == '\n' || buffer[ k ] == ' ' )
            foundbreak = qtrue;
        }
        if( !foundbreak )
        {
          out [ i ] = '\n';
          linecount = 0;
        }
      }
       
      i++;
      j++;
     }
     else
     {
       out[ i ] = '\n';
       i++;
       linecount = 0;
     }
  }
  out[ i ] = '\0';


  strcpy( buffer, out );
}

void G_PrivateMessage( gentity_t *ent )
{
  int pids[ MAX_CLIENTS ];
  int ignoreids[ MAX_CLIENTS ];
  char name[ MAX_NAME_LENGTH ];
  char cmd[ 12 ];
  char str[ MAX_STRING_CHARS ];
  char *msg;
  char color;
  int pcount, matches, ignored = 0;
  int i;
  int skipargs = 0;
  qboolean teamonly = qfalse;
  gentity_t *tmpent;

  if( !g_privateMessages.integer && ent )
  {
    ADMP( "Sorry, but private messages have been disabled\n" );
    return;
  }
  
  if( g_floodMinTime.integer )
   if ( G_Flood_Limited( ent ) )
   {
    trap_SendServerCommand( ent-g_entities, "print \"Your chat is flood-limited; wait before chatting again\n\"" );
    return;
   }

  G_SayArgv( 0, cmd, sizeof( cmd ) );
  if( !Q_stricmp( cmd, "say" ) || !Q_stricmp( cmd, "say_team" ) )
  {
    skipargs = 1;
    G_SayArgv( 1, cmd, sizeof( cmd ) );
  }
  if( G_SayArgc( ) < 3+skipargs )
  {
    ADMP( va( "usage: %s [name|slot#] [message]\n", cmd ) );
    return;
  }

  if( !Q_stricmp( cmd, "mt" ) || !Q_stricmp( cmd, "/mt" ) )
    teamonly = qtrue;

  G_SayArgv( 1+skipargs, name, sizeof( name ) );
  msg = G_SayConcatArgs( 2+skipargs );
  pcount = G_ClientNumbersFromString( name, pids );

  if( ent )
  {
    int count = 0;

    for( i=0; i < pcount; i++ )
    {
      tmpent = &g_entities[ pids[ i ] ];

      if( teamonly && !OnSameTeam( ent, tmpent ) )
        continue;

      if( BG_ClientListTest( &tmpent->client->sess.ignoreList,
        ent-g_entities ) )
      {
        ignoreids[ ignored++ ] = pids[ i ];
        continue;
      }

      pids[ count ] = pids[ i ];
      count++;
    }
    matches = count;
  }
  else
  {
    matches = pcount;
  }

  color = teamonly ? COLOR_CYAN : COLOR_YELLOW;

  if( !Q_stricmp( name, "console" ) )
  {
    ADMP( va( "^%cPrivate message: ^7%s\n", color, msg ) );
    ADMP( va( "^%csent to Console.\n", color ) );

    G_LogPrintf( "privmsg: %s^7: Console: ^6%s^7\n",
      ( ent ) ? ent->client->pers.netname : "Console", msg );

    return;
  }

  Q_strncpyz( str,
    va( "^%csent to %i player%s: ^7", color, matches,
      ( matches == 1 ) ? "" : "s" ),
    sizeof( str ) );

  for( i=0; i < matches; i++ )
  {
    tmpent = &g_entities[ pids[ i ] ];

    if( i > 0 )
      Q_strcat( str, sizeof( str ), "^7, " );
    Q_strcat( str, sizeof( str ), tmpent->client->pers.netname );
    trap_SendServerCommand( pids[ i ], va(
      "chat -1 \"%s^%c -> ^7%s^7: (%d recipients): ^%c%s^7\" %i",//rotaxfun -1
      ( ent ) ? ent->client->pers.netname : "console",
      color,
      name,
      matches,
      color,
      msg,
      ent ? ent-g_entities : -1 ) );

    trap_SendServerCommand( pids[ i ], va( 
      "cp \"^%cprivate message from ^7%s^7\"", color,
      ( ent ) ? ent->client->pers.netname : "console" ) );
  }

  if( !matches )
    ADMP( va( "^3No player matching ^7\'%s^7\' ^3to send message to.\n",
      name ) );
  else
  {
    if( ent )
      ADMP( va( "^%cPrivate message: ^7%s\n", color, msg ) );

    ADMP( va( "%s\n", str ) );

    G_LogPrintf( "%s: %s^7: %s^7: %s\n",
      ( teamonly ) ? "tprivmsg" : "privmsg",
      ( ent ) ? ent->client->pers.netname : "console",
      name, msg );
  }

  if( ignored )
  {
    Q_strncpyz( str, va( "^%cignored by %i player%s: ^7", color, ignored,
      ( ignored == 1 ) ? "" : "s" ), sizeof( str ) );
    for( i=0; i < ignored; i++ )
    {
      tmpent = &g_entities[ ignoreids[ i ] ];
      if( i > 0 )
        Q_strcat( str, sizeof( str ), "^7, " );
      Q_strcat( str, sizeof( str ), tmpent->client->pers.netname );
    }
    ADMP( va( "%s\n", str ) );
  }
}

 /*
 =================
 Cmd_Builder_f
 =================
 */
 void Cmd_Builder_f( gentity_t *ent )
 {
   vec3_t      forward, right, up;
   vec3_t      start, end;
   trace_t     tr;
   gentity_t   *traceEnt;
   char bdnumbchr[21];
 
   AngleVectors( ent->client->ps.viewangles, forward, right, up );
   if( ent->client->pers.teamSelection != PTE_NONE )
     CalcMuzzlePoint( ent, forward, right, up, start );
   else
     VectorCopy( ent->client->ps.origin, start );
   VectorMA( start, 1000, forward, end );
 
   trap_Trace( &tr, start, NULL, NULL, end, ent->s.number, MASK_PLAYERSOLID );
   traceEnt = &g_entities[ tr.entityNum ];
 
   Com_sprintf( bdnumbchr, sizeof(bdnumbchr), "%i", traceEnt->bdnumb );
 
   if( tr.fraction < 1.0f && ( traceEnt->s.eType == ET_BUILDABLE ) )
   {
     if( G_admin_permission( ent, 'U' ) ) {
      trap_SendServerCommand( ent-g_entities, va(
        "print \"^5/builder:^7 ^3Building:^7 %s ^3Built By:^7 %s^7 ^3Buildlog Number:^7 %s^7\n\"",
        BG_FindHumanNameForBuildable( traceEnt->s.modelindex ),
        (traceEnt->bdnumb != -1) ? G_FindBuildLogName( traceEnt->bdnumb ) : "<world>",
        (traceEnt->bdnumb != -1) ? bdnumbchr : "none" ) );
     }
     else
     {
      trap_SendServerCommand( ent-g_entities, va(
        "print \"^5/builder:^7 ^3Building:^7 %s ^3Built By:^7 %s^7\n\"",
         BG_FindHumanNameForBuildable( traceEnt->s.modelindex ),     
        (traceEnt->bdnumb != -1) ? G_FindBuildLogName( traceEnt->bdnumb ) : "<world>" ) );
     }
   }
   else
   {
      trap_SendServerCommand( ent-g_entities, "print \"^5/builder:^7 No structure found in your crosshair. Please face a structure and try again.\n\"" );
   }
 }

void G_CP( gentity_t *ent )
 { 
   int i;
   char buffer[MAX_STRING_CHARS];
   char prefixes[MAX_STRING_CHARS] = "";
   char wrappedtext[ MAX_STRING_CHARS ] = "";
   char *ptr;
   char *text;
   qboolean sendAliens = qtrue;
   qboolean sendHumans = qtrue;
   qboolean sendSpecs = qtrue;
   Q_strncpyz( buffer, ConcatArgs( 1 ), sizeof( buffer ) );
   G_ParseEscapedString( buffer );

   if( strstr( buffer, "!cp" ) )
   {
     ptr = buffer;
     while( *ptr != '!' )
       ptr++;
     ptr+=4;
     
     Q_strncpyz( buffer, ptr, sizeof(buffer) );
   }

   text = buffer;

   ptr = buffer;
   while( *ptr == ' ' )
     ptr++;
   if( *ptr == '-' )
   {
      sendAliens = qfalse;
      sendHumans = qfalse;
      sendSpecs = qfalse;
      Q_strcat( prefixes, sizeof( prefixes ), " " );
      ptr++;

      while( *ptr != ' ' )
      {
        if( *ptr == 'a' || *ptr == 'A' )
        {
          sendAliens = qtrue;
          Q_strcat( prefixes, sizeof( prefixes ), "[A]" );
        }
        if( *ptr == 'h' || *ptr == 'H' )
        {
          sendHumans = qtrue;
          Q_strcat( prefixes, sizeof( prefixes ), "[H]" );
        }
        if( *ptr == 's' || *ptr == 'S' )
        {
          sendSpecs = qtrue;
          Q_strcat( prefixes, sizeof( prefixes ), "[S]" );
        }
        ptr++;
      }
      text = ptr+1;
   }
  
  strcpy( wrappedtext, text );
  G_WordWrap( wrappedtext, 50 );

  for( i = 0; i < level.maxclients; i++ )
  {
    if( level.clients[ i ].pers.connected == CON_DISCONNECTED )
      continue;

    if( ( !sendAliens && level.clients[ i ].pers.teamSelection == PTE_ALIENS ) ||
         ( !sendHumans && level.clients[ i ].pers.teamSelection == PTE_HUMANS ) ||
         ( !sendSpecs && level.clients[ i ].pers.teamSelection == PTE_NONE ) )
    {
      if( G_admin_permission( &g_entities[ i ], ADMF_ADMINCHAT ) )
      {
        trap_SendServerCommand( i, va("print \"^6[Admins]^7 CP to other team%s: %s \n\"", prefixes, text ) );
      }
      continue;
    }

      trap_SendServerCommand( i, va( "cp \"%s\"", wrappedtext ) );
      trap_SendServerCommand( i, va( "print \"%s^7 CP%s: %s\n\"", ( ent ? G_admin_adminPrintName( ent ) : "console" ), prefixes, text ) );
    }

     G_Printf( "cp: %s\n", ConcatArgs( 1 ) );
 }