aboutsummaryrefslogtreecommitdiffstats
path: root/src/jake2/game/PlayerClient.java
blob: 80cdbe877a1f56cc4d2718b051945faab98d9a31 (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
/*
 * Copyright (C) 1997-2001 Id Software, Inc.
 * 
 * This program 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.
 * 
 * This program 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
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 */

// Created on 28.12.2003 by RST.
// $Id: PlayerClient.java,v 1.7 2005-02-06 19:10:48 salomo Exp $
package jake2.game;

import jake2.Defines;
import jake2.util.Lib;
import jake2.util.Math3D;

public class PlayerClient {

    /*
     * QUAKED info_player_start (1 0 0) (-16 -16 -24) (16 16 32) The normal
     * starting point for a level.
     */
    public static void SP_info_player_start(edict_t self) {
        if (GameBase.coop.value == 0)
            return;
        if (Lib.Q_stricmp(GameBase.level.mapname, "security") == 0) {
            // invoke one of our gross, ugly, disgusting hacks
            self.think = PlayerClientAdapters.SP_CreateCoopSpots;
            self.nextthink = GameBase.level.time + Defines.FRAMETIME;
        }
    }

    /*
     * QUAKED info_player_deathmatch (1 0 1) (-16 -16 -24) (16 16 32) potential
     * spawning position for deathmatch games
     */
    public static void SP_info_player_deathmatch(edict_t self) {
        if (0 == GameBase.deathmatch.value) {
            GameUtil.G_FreeEdict(self);
            return;
        }
        GameMisc.SP_misc_teleporter_dest.think(self);
    }

    /*
     * QUAKED info_player_coop (1 0 1) (-16 -16 -24) (16 16 32) potential
     * spawning position for coop games
     */

    public static void SP_info_player_coop(edict_t self) {
        if (0 == GameBase.coop.value) {
            GameUtil.G_FreeEdict(self);
            return;
        }

        if ((Lib.Q_stricmp(GameBase.level.mapname, "jail2") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "jail4") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "mine1") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "mine2") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "mine3") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "mine4") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "lab") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "boss1") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "fact3") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "biggun") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "space") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "command") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "power2") == 0)
                || (Lib.Q_stricmp(GameBase.level.mapname, "strike") == 0)) {
            // invoke one of our gross, ugly, disgusting hacks
            self.think = PlayerClientAdapters.SP_FixCoopSpots;
            self.nextthink = GameBase.level.time + Defines.FRAMETIME;
        }
    }

    /*
     * QUAKED info_player_intermission (1 0 1) (-16 -16 -24) (16 16 32) The
     * deathmatch intermission point will be at one of these Use 'angles'
     * instead of 'angle', so you can set pitch or roll as well as yaw. 'pitch
     * yaw roll'
     */
    public static void SP_info_player_intermission() {
    }

    public static void ClientObituary(edict_t self, edict_t inflictor,
            edict_t attacker) {
        int mod;
        String message;
        String message2;
        boolean ff;

        if (GameBase.coop.value != 0 && attacker.client != null)
            GameBase.meansOfDeath |= Defines.MOD_FRIENDLY_FIRE;

        if (GameBase.deathmatch.value != 0 || GameBase.coop.value != 0) {
            ff = (GameBase.meansOfDeath & Defines.MOD_FRIENDLY_FIRE) != 0;
            mod = GameBase.meansOfDeath & ~Defines.MOD_FRIENDLY_FIRE;
            message = null;
            message2 = "";

            switch (mod) {
            case Defines.MOD_SUICIDE:
                message = "suicides";
                break;
            case Defines.MOD_FALLING:
                message = "cratered";
                break;
            case Defines.MOD_CRUSH:
                message = "was squished";
                break;
            case Defines.MOD_WATER:
                message = "sank like a rock";
                break;
            case Defines.MOD_SLIME:
                message = "melted";
                break;
            case Defines.MOD_LAVA:
                message = "does a back flip into the lava";
                break;
            case Defines.MOD_EXPLOSIVE:
            case Defines.MOD_BARREL:
                message = "blew up";
                break;
            case Defines.MOD_EXIT:
                message = "found a way out";
                break;
            case Defines.MOD_TARGET_LASER:
                message = "saw the light";
                break;
            case Defines.MOD_TARGET_BLASTER:
                message = "got blasted";
                break;
            case Defines.MOD_BOMB:
            case Defines.MOD_SPLASH:
            case Defines.MOD_TRIGGER_HURT:
                message = "was in the wrong place";
                break;
            }
            if (attacker == self) {
                switch (mod) {
                case Defines.MOD_HELD_GRENADE:
                    message = "tried to put the pin back in";
                    break;
                case Defines.MOD_HG_SPLASH:
                case Defines.MOD_G_SPLASH:
                    if (GameAI.IsNeutral(self))
                        message = "tripped on its own grenade";
                    else if (GameAI.IsFemale(self))
                        message = "tripped on her own grenade";
                    else
                        message = "tripped on his own grenade";
                    break;
                case Defines.MOD_R_SPLASH:
                    if (GameAI.IsNeutral(self))
                        message = "blew itself up";
                    else if (GameAI.IsFemale(self))
                        message = "blew herself up";
                    else
                        message = "blew himself up";
                    break;
                case Defines.MOD_BFG_BLAST:
                    message = "should have used a smaller gun";
                    break;
                default:
                    if (GameAI.IsNeutral(self))
                        message = "killed itself";
                    else if (GameAI.IsFemale(self))
                        message = "killed herself";
                    else
                        message = "killed himself";
                    break;
                }
            }
            if (message != null) {
                GameBase.gi.bprintf(Defines.PRINT_MEDIUM,
                        self.client.pers.netname + " " + message + ".\n");
                if (GameBase.deathmatch.value != 0)
                    self.client.resp.score--;
                self.enemy = null;
                return;
            }

            self.enemy = attacker;
            if (attacker != null && attacker.client != null) {
                switch (mod) {
                case Defines.MOD_BLASTER:
                    message = "was blasted by";
                    break;
                case Defines.MOD_SHOTGUN:
                    message = "was gunned down by";
                    break;
                case Defines.MOD_SSHOTGUN:
                    message = "was blown away by";
                    message2 = "'s super shotgun";
                    break;
                case Defines.MOD_MACHINEGUN:
                    message = "was machinegunned by";
                    break;
                case Defines.MOD_CHAINGUN:
                    message = "was cut in half by";
                    message2 = "'s chaingun";
                    break;
                case Defines.MOD_GRENADE:
                    message = "was popped by";
                    message2 = "'s grenade";
                    break;
                case Defines.MOD_G_SPLASH:
                    message = "was shredded by";
                    message2 = "'s shrapnel";
                    break;
                case Defines.MOD_ROCKET:
                    message = "ate";
                    message2 = "'s rocket";
                    break;
                case Defines.MOD_R_SPLASH:
                    message = "almost dodged";
                    message2 = "'s rocket";
                    break;
                case Defines.MOD_HYPERBLASTER:
                    message = "was melted by";
                    message2 = "'s hyperblaster";
                    break;
                case Defines.MOD_RAILGUN:
                    message = "was railed by";
                    break;
                case Defines.MOD_BFG_LASER:
                    message = "saw the pretty lights from";
                    message2 = "'s BFG";
                    break;
                case Defines.MOD_BFG_BLAST:
                    message = "was disintegrated by";
                    message2 = "'s BFG blast";
                    break;
                case Defines.MOD_BFG_EFFECT:
                    message = "couldn't hide from";
                    message2 = "'s BFG";
                    break;
                case Defines.MOD_HANDGRENADE:
                    message = "caught";
                    message2 = "'s handgrenade";
                    break;
                case Defines.MOD_HG_SPLASH:
                    message = "didn't see";
                    message2 = "'s handgrenade";
                    break;
                case Defines.MOD_HELD_GRENADE:
                    message = "feels";
                    message2 = "'s pain";
                    break;
                case Defines.MOD_TELEFRAG:
                    message = "tried to invade";
                    message2 = "'s personal space";
                    break;
                }
                if (message != null) {
                    GameBase.gi.bprintf(Defines.PRINT_MEDIUM,
                            self.client.pers.netname + " " + message + " "
                                    + attacker.client.pers.netname + " "
                                    + message2 + "\n");
                    if (GameBase.deathmatch.value != 0) {
                        if (ff)
                            attacker.client.resp.score--;
                        else
                            attacker.client.resp.score++;
                    }
                    return;
                }
            }
        }

        GameBase.gi.bprintf(Defines.PRINT_MEDIUM, self.client.pers.netname
                + " died.\n");
        if (GameBase.deathmatch.value != 0)
            self.client.resp.score--;
    }

    /*
     * ================== player_die ==================
     */

    //=======================================================================
    /*
     * ============== InitClientPersistant
     * 
     * This is only called when the game first initializes in single player, but
     * is called after each death and level change in deathmatch ==============
     */
    public static void InitClientPersistant(gclient_t client) {
        gitem_t item;

        client.pers = new client_persistant_t();

        item = GameUtil.FindItem("Blaster");
        client.pers.selected_item = GameUtil.ITEM_INDEX(item);
        client.pers.inventory[client.pers.selected_item] = 1;

        /*
         * Give shotgun. item = FindItem("Shotgun"); client.pers.selected_item =
         * ITEM_INDEX(item); client.pers.inventory[client.pers.selected_item] =
         * 1;
         */

        client.pers.weapon = item;

        client.pers.health = 100;
        client.pers.max_health = 100;

        client.pers.max_bullets = 200;
        client.pers.max_shells = 100;
        client.pers.max_rockets = 50;
        client.pers.max_grenades = 50;
        client.pers.max_cells = 200;
        client.pers.max_slugs = 50;

        client.pers.connected = true;
    }

    public static void InitClientResp(gclient_t client) {
        //memset(& client.resp, 0, sizeof(client.resp));
        client.resp.clear(); //  ok.
        client.resp.enterframe = GameBase.level.framenum;
        client.resp.coop_respawn.set(client.pers);
    }

    /*
     * ================== SaveClientData
     * 
     * Some information that should be persistant, like health, is still stored
     * in the edict structure, so it needs to be mirrored out to the client
     * structure before all the edicts are wiped. ==================
     */
    public static void SaveClientData() {
        int i;
        edict_t ent;

        for (i = 0; i < GameBase.game.maxclients; i++) {
            ent = GameBase.g_edicts[1 + i];
            if (!ent.inuse)
                continue;

            GameBase.game.clients[i].pers.health = ent.health;
            GameBase.game.clients[i].pers.max_health = ent.max_health;
            GameBase.game.clients[i].pers.savedFlags = (ent.flags & (Defines.FL_GODMODE
                    | Defines.FL_NOTARGET | Defines.FL_POWER_ARMOR));

            if (GameBase.coop.value != 0)
                GameBase.game.clients[i].pers.score = ent.client.resp.score;
        }
    }

    public static void FetchClientEntData(edict_t ent) {
        ent.health = ent.client.pers.health;
        ent.max_health = ent.client.pers.max_health;
        ent.flags |= ent.client.pers.savedFlags;
        if (GameBase.coop.value != 0)
            ent.client.resp.score = ent.client.pers.score;
    }

    /*
     * ================ PlayersRangeFromSpot
     * 
     * Returns the distance to the nearest player from the given spot
     * ================
     */
    static float PlayersRangeFromSpot(edict_t spot) {
        edict_t player;
        float bestplayerdistance;
        float[] v = { 0, 0, 0 };
        int n;
        float playerdistance;

        bestplayerdistance = 9999999;

        for (n = 1; n <= GameBase.maxclients.value; n++) {
            player = GameBase.g_edicts[n];

            if (!player.inuse)
                continue;

            if (player.health <= 0)
                continue;

            Math3D.VectorSubtract(spot.s.origin, player.s.origin, v);
            playerdistance = Math3D.VectorLength(v);

            if (playerdistance < bestplayerdistance)
                bestplayerdistance = playerdistance;
        }

        return bestplayerdistance;
    }

    /*
     * ================ SelectRandomDeathmatchSpawnPoint
     * 
     * go to a random point, but NOT the two points closest to other players
     * ================
     */
    public static edict_t SelectRandomDeathmatchSpawnPoint() {
        edict_t spot, spot1, spot2;
        int count = 0;
        int selection;
        float range, range1, range2;

        spot = null;
        range1 = range2 = 99999;
        spot1 = spot2 = null;

        EdictIterator es = null;

        while ((es = GameBase.G_Find(es, GameBase.findByClass,
                "info_player_deathmatch")) != null) {
            spot = es.o;
            count++;
            range = PlayersRangeFromSpot(spot);
            if (range < range1) {
                range1 = range;
                spot1 = spot;
            } else if (range < range2) {
                range2 = range;
                spot2 = spot;
            }
        }

        if (count == 0)
            return null;

        if (count <= 2) {
            spot1 = spot2 = null;
        } else
            count -= 2;

        selection = Lib.rand() % count;

        spot = null;
        es = null;
        do {
            es = GameBase.G_Find(es, GameBase.findByClass,
                    "info_player_deathmatch");
            spot = es.o;
            if (spot == spot1 || spot == spot2)
                selection++;
        } while (selection-- > 0);

        return spot;
    }

    /*
     * ================ SelectFarthestDeathmatchSpawnPoint
     * 
     * ================
     */
    static edict_t SelectFarthestDeathmatchSpawnPoint() {
        edict_t bestspot;
        float bestdistance, bestplayerdistance;
        edict_t spot;

        spot = null;
        bestspot = null;
        bestdistance = 0;

        EdictIterator es = null;
        while ((es = GameBase.G_Find(es, GameBase.findByClass,
                "info_player_deathmatch")).o != null) {
            spot = es.o;
            bestplayerdistance = PlayersRangeFromSpot(spot);

            if (bestplayerdistance > bestdistance) {
                bestspot = spot;
                bestdistance = bestplayerdistance;
            }
        }

        if (bestspot != null) {
            return bestspot;
        }

        // if there is a player just spawned on each and every start spot
        // we have no choice to turn one into a telefrag meltdown
        spot = GameBase.G_Find(null, GameBase.findByClass,
                "info_player_deathmatch").o;

        return spot;
    }

    public static edict_t SelectDeathmatchSpawnPoint() {
        if (0 != ((int) (GameBase.dmflags.value) & Defines.DF_SPAWN_FARTHEST))
            return SelectFarthestDeathmatchSpawnPoint();
        else
            return SelectRandomDeathmatchSpawnPoint();
    }

    public static edict_t SelectCoopSpawnPoint(edict_t ent) {
        int index;
        edict_t spot = null;
        String target;

        //index = ent.client - game.clients;
        index = ent.client.index;

        // player 0 starts in normal player spawn point
        if (index == 0)
            return null;

        spot = null;
        EdictIterator es = null;

        // assume there are four coop spots at each spawnpoint
        while (true) {

            es = GameBase.G_Find(es, GameBase.findByClass,
                    "info_player_coop");
                    
            // nullpointer exi fixed (RST).
            if (es == null)
                return null;
            
            spot = es.o;
                
            if (spot == null)
                return null; // we didn't have enough...

            target = spot.targetname;
            if (target == null)
                target = "";
            if (Lib.Q_stricmp(GameBase.game.spawnpoint, target) == 0) { // this
                                                                        // is a
                                                                        // coop
                                                                        // spawn
                                                                        // point
                                                                        // for
                                                                        // one
                                                                        // of
                                                                        // the
                                                                        // clients
                                                                        // here
                index--;
                if (0 == index)
                    return spot; // this is it
            }
        }

    }

    /*
     * =========== SelectSpawnPoint
     * 
     * Chooses a player start, deathmatch start, coop start, etc ============
     */
    public static void SelectSpawnPoint(edict_t ent, float[] origin,
            float[] angles) {
        edict_t spot = null;

        if (GameBase.deathmatch.value != 0)
            spot = SelectDeathmatchSpawnPoint();
        else if (GameBase.coop.value != 0)
            spot = SelectCoopSpawnPoint(ent);

        EdictIterator es = null;
        // find a single player start spot
        if (null == spot) {
            while ((es = GameBase.G_Find(es, GameBase.findByClass,
                    "info_player_start")) != null) {
                spot = es.o;

                if (GameBase.game.spawnpoint.length() == 0
                        && spot.targetname == null)
                    break;

                if (GameBase.game.spawnpoint.length() == 0
                        || spot.targetname == null)
                    continue;

                if (Lib.Q_stricmp(GameBase.game.spawnpoint, spot.targetname) == 0)
                    break;
            }

            if (null == spot) {
                if (GameBase.game.spawnpoint.length() == 0) { // there wasn't a
                                                              // spawnpoint
                                                              // without a
                                                              // target, so use
                                                              // any
                    spot = (es = GameBase.G_Find(es, GameBase.findByClass,
                            "info_player_start")).o;
                }
                if (null == spot)
                    GameBase.gi.error("Couldn't find spawn point "
                            + GameBase.game.spawnpoint + "\n");
            }
        }

        Math3D.VectorCopy(spot.s.origin, origin);
        origin[2] += 9;
        Math3D.VectorCopy(spot.s.angles, angles);
    }

    //======================================================================

    public static void InitBodyQue() {
        int i;
        edict_t ent;

        GameBase.level.body_que = 0;
        for (i = 0; i < Defines.BODY_QUEUE_SIZE; i++) {
            ent = GameUtil.G_Spawn();
            ent.classname = "bodyque";
        }
    }

    public static void CopyToBodyQue(edict_t ent) {
        edict_t body;

        // grab a body que and cycle to the next one
        int i = (int) GameBase.maxclients.value + GameBase.level.body_que + 1;
        body = GameBase.g_edicts[i];
        GameBase.level.body_que = (GameBase.level.body_que + 1)
                % Defines.BODY_QUEUE_SIZE;

        // FIXME: send an effect on the removed body

        GameBase.gi.unlinkentity(ent);

        GameBase.gi.unlinkentity(body);
        body.s = ent.s.getClone();

        body.s.number = body.index;

        body.svflags = ent.svflags;
        Math3D.VectorCopy(ent.mins, body.mins);
        Math3D.VectorCopy(ent.maxs, body.maxs);
        Math3D.VectorCopy(ent.absmin, body.absmin);
        Math3D.VectorCopy(ent.absmax, body.absmax);
        Math3D.VectorCopy(ent.size, body.size);
        body.solid = ent.solid;
        body.clipmask = ent.clipmask;
        body.owner = ent.owner;
        body.movetype = ent.movetype;

        body.die = PlayerClientAdapters.body_die;
        body.takedamage = Defines.DAMAGE_YES;

        GameBase.gi.linkentity(body);
    }

    public static void respawn(edict_t self) {
        if (GameBase.deathmatch.value != 0 || GameBase.coop.value != 0) {
            // spectator's don't leave bodies
            if (self.movetype != Defines.MOVETYPE_NOCLIP)
                CopyToBodyQue(self);
            self.svflags &= ~Defines.SVF_NOCLIENT;
            PutClientInServer(self);

            // add a teleportation effect
            self.s.event = Defines.EV_PLAYER_TELEPORT;

            // hold in place briefly
            self.client.ps.pmove.pm_flags = pmove_t.PMF_TIME_TELEPORT;
            self.client.ps.pmove.pm_time = 14;

            self.client.respawn_time = GameBase.level.time;

            return;
        }

        // restart the entire server
        GameBase.gi.AddCommandString("menu_loadgame\n");
    }

    private static boolean passwdOK(String i1, String i2) {
        if (i1.length() != 0 && !i1.equals("none") && !i1.equals(i2))
            return false;
        return true;
    }

    /*
     * only called when pers.spectator changes note that resp.spectator should
     * be the opposite of pers.spectator here
     */
    public static void spectator_respawn(edict_t ent) {
        int i, numspec;

        // if the user wants to become a spectator, make sure he doesn't
        // exceed max_spectators

        if (ent.client.pers.spectator) {
            String value = Info.Info_ValueForKey(ent.client.pers.userinfo,
                    "spectator");

            if (!passwdOK(GameBase.spectator_password.string, value)) {
                GameBase.gi.cprintf(ent, Defines.PRINT_HIGH,
                        "Spectator password incorrect.\n");
                ent.client.pers.spectator = false;
                GameBase.gi.WriteByte(Defines.svc_stufftext);
                GameBase.gi.WriteString("spectator 0\n");
                GameBase.gi.unicast(ent, true);
                return;
            }

            // count spectators
            for (i = 1, numspec = 0; i <= GameBase.maxclients.value; i++)
                if (GameBase.g_edicts[i].inuse
                        && GameBase.g_edicts[i].client.pers.spectator)
                    numspec++;

            if (numspec >= GameBase.maxspectators.value) {
                GameBase.gi.cprintf(ent, Defines.PRINT_HIGH,
                        "Server spectator limit is full.");
                ent.client.pers.spectator = false;
                // reset his spectator var
                GameBase.gi.WriteByte(Defines.svc_stufftext);
                GameBase.gi.WriteString("spectator 0\n");
                GameBase.gi.unicast(ent, true);
                return;
            }
        } else {
            // he was a spectator and wants to join the game
            // he must have the right password
            String value = Info.Info_ValueForKey(ent.client.pers.userinfo,
                    "password");
            if (!passwdOK(GameBase.spectator_password.string, value)) {
                GameBase.gi.cprintf(ent, Defines.PRINT_HIGH,
                        "Password incorrect.\n");
                ent.client.pers.spectator = true;
                GameBase.gi.WriteByte(Defines.svc_stufftext);
                GameBase.gi.WriteString("spectator 1\n");
                GameBase.gi.unicast(ent, true);
                return;
            }
        }

        // clear client on respawn
        ent.client.resp.score = ent.client.pers.score = 0;

        ent.svflags &= ~Defines.SVF_NOCLIENT;
        PutClientInServer(ent);

        // add a teleportation effect
        if (!ent.client.pers.spectator) {
            // send effect
            GameBase.gi.WriteByte(Defines.svc_muzzleflash);
            //gi.WriteShort(ent - g_edicts);
            GameBase.gi.WriteShort(ent.index);

            GameBase.gi.WriteByte(Defines.MZ_LOGIN);
            GameBase.gi.multicast(ent.s.origin, Defines.MULTICAST_PVS);

            // hold in place briefly
            ent.client.ps.pmove.pm_flags = pmove_t.PMF_TIME_TELEPORT;
            ent.client.ps.pmove.pm_time = 14;
        }

        ent.client.respawn_time = GameBase.level.time;

        if (ent.client.pers.spectator)
            GameBase.gi.bprintf(Defines.PRINT_HIGH, ent.client.pers.netname
                    + " has moved to the sidelines\n");
        else
            GameBase.gi.bprintf(Defines.PRINT_HIGH, ent.client.pers.netname
                    + " joined the game\n");
    }

    //==============================================================

    /*
     * =========== PutClientInServer
     * 
     * Called when a player connects to a server or respawns in a deathmatch.
     * ============
     */
    public static void PutClientInServer(edict_t ent) {
        float[] mins = { -16, -16, -24 };
        float[] maxs = { 16, 16, 32 };
        int index;
        float[] spawn_origin = { 0, 0, 0 }, spawn_angles = { 0, 0, 0 };
        gclient_t client;
        int i;
        client_persistant_t saved = new client_persistant_t();
        client_respawn_t resp = new client_respawn_t();

        // find a spawn point
        // do it before setting health back up, so farthest
        // ranging doesn't count this client
        SelectSpawnPoint(ent, spawn_origin, spawn_angles);

        index = ent.index - 1;
        client = ent.client;

        // deathmatch wipes most client data every spawn
        if (GameBase.deathmatch.value != 0) {           

            resp.set(client.resp);
            String userinfo = client.pers.userinfo;
            InitClientPersistant(client);
            
            userinfo = ClientUserinfoChanged(ent, userinfo);
            
        } else if (GameBase.coop.value != 0) {

            resp.set(client.resp);

            String userinfo = client.pers.userinfo;

            resp.coop_respawn.game_helpchanged = client.pers.game_helpchanged;
            resp.coop_respawn.helpchanged = client.pers.helpchanged;
            client.pers.set(resp.coop_respawn);
            userinfo = ClientUserinfoChanged(ent, userinfo);
            if (resp.score > client.pers.score)
                client.pers.score = resp.score;
        } else {
            resp.clear();
        }

        // clear everything but the persistant data
        saved.set(client.pers);
        client.clear();
        client.pers.set(saved);
        if (client.pers.health <= 0)
            InitClientPersistant(client);

        client.resp.set(resp);

        // copy some data from the client to the entity
        FetchClientEntData(ent);

        // clear entity values
        ent.groundentity = null;
        ent.client = GameBase.game.clients[index];
        ent.takedamage = Defines.DAMAGE_AIM;
        ent.movetype = Defines.MOVETYPE_WALK;
        ent.viewheight = 22;
        ent.inuse = true;
        ent.classname = "player";
        ent.mass = 200;
        ent.solid = Defines.SOLID_BBOX;
        ent.deadflag = Defines.DEAD_NO;
        ent.air_finished = GameBase.level.time + 12;
        ent.clipmask = Defines.MASK_PLAYERSOLID;
        ent.model = "players/male/tris.md2";
        ent.pain = PlayerClientAdapters.player_pain;
        ent.die = GameAI.player_die;
        ent.waterlevel = 0;
        ent.watertype = 0;
        ent.flags &= ~Defines.FL_NO_KNOCKBACK;
        ent.svflags &= ~Defines.SVF_DEADMONSTER;

        Math3D.VectorCopy(mins, ent.mins);
        Math3D.VectorCopy(maxs, ent.maxs);
        Math3D.VectorClear(ent.velocity);

        // clear playerstate values
        ent.client.ps.clear();     

        client.ps.pmove.origin[0] = (short) (spawn_origin[0] * 8);
        client.ps.pmove.origin[1] = (short) (spawn_origin[1] * 8);
        client.ps.pmove.origin[2] = (short) (spawn_origin[2] * 8);

        if (GameBase.deathmatch.value != 0
                && 0 != ((int) GameBase.dmflags.value & Defines.DF_FIXED_FOV)) {
            client.ps.fov = 90;
        } else {
            client.ps.fov = Lib.atoi(Info.Info_ValueForKey(
                    client.pers.userinfo, "fov"));
            if (client.ps.fov < 1)
                client.ps.fov = 90;
            else if (client.ps.fov > 160)
                client.ps.fov = 160;
        }

        client.ps.gunindex = GameBase.gi
                .modelindex(client.pers.weapon.view_model);

        // clear entity state values
        ent.s.effects = 0;
        ent.s.modelindex = 255; // will use the skin specified model
        ent.s.modelindex2 = 255; // custom gun model
        // sknum is player num and weapon number
        // weapon number will be added in changeweapon
        ent.s.skinnum = ent.index - 1;

        ent.s.frame = 0;
        Math3D.VectorCopy(spawn_origin, ent.s.origin);
        ent.s.origin[2] += 1; // make sure off ground
        Math3D.VectorCopy(ent.s.origin, ent.s.old_origin);

        // set the delta angle
        for (i = 0; i < 3; i++) {
            client.ps.pmove.delta_angles[i] = (short) Math3D
                    .ANGLE2SHORT(spawn_angles[i] - client.resp.cmd_angles[i]);
        }

        ent.s.angles[Defines.PITCH] = 0;
        ent.s.angles[Defines.YAW] = spawn_angles[Defines.YAW];
        ent.s.angles[Defines.ROLL] = 0;
        Math3D.VectorCopy(ent.s.angles, client.ps.viewangles);
        Math3D.VectorCopy(ent.s.angles, client.v_angle);

        // spawn a spectator
        if (client.pers.spectator) {
            client.chase_target = null;

            client.resp.spectator = true;

            ent.movetype = Defines.MOVETYPE_NOCLIP;
            ent.solid = Defines.SOLID_NOT;
            ent.svflags |= Defines.SVF_NOCLIENT;
            ent.client.ps.gunindex = 0;
            GameBase.gi.linkentity(ent);
            return;
        } else
            client.resp.spectator = false;

        if (!GameUtil.KillBox(ent)) { // could't spawn in?
        }

        GameBase.gi.linkentity(ent);

        // force the current weapon up
        client.newweapon = client.pers.weapon;
        GamePWeapon.ChangeWeapon(ent);
    }

    /*
     * ===================== ClientBeginDeathmatch
     * 
     * A client has just connected to the server in deathmatch mode, so clear
     * everything out before starting them. =====================
     */
    public static void ClientBeginDeathmatch(edict_t ent) {
        GameUtil.G_InitEdict(ent, ent.index);

        InitClientResp(ent.client);

        // locate ent at a spawn point
        PutClientInServer(ent);

        if (GameBase.level.intermissiontime != 0) {
            PlayerHud.MoveClientToIntermission(ent);
        } else {
            // send effect
            GameBase.gi.WriteByte(Defines.svc_muzzleflash);
            //gi.WriteShort(ent - g_edicts);
            GameBase.gi.WriteShort(ent.index);
            GameBase.gi.WriteByte(Defines.MZ_LOGIN);
            GameBase.gi.multicast(ent.s.origin, Defines.MULTICAST_PVS);
        }

        GameBase.gi.bprintf(Defines.PRINT_HIGH, ent.client.pers.netname
                + " entered the game\n");

        // make sure all view stuff is valid
        PlayerView.ClientEndServerFrame(ent);
    }

    /*
     * =========== ClientBegin
     * 
     * called when a client has finished connecting, and is ready to be placed
     * into the game. This will happen every level load. ============
     */
    public static void ClientBegin(edict_t ent) {
        int i;

        //ent.client = game.clients + (ent - g_edicts - 1);
        ent.client = GameBase.game.clients[ent.index - 1];

        if (GameBase.deathmatch.value != 0) {
            ClientBeginDeathmatch(ent);
            return;
        }

        // if there is already a body waiting for us (a loadgame), just
        // take it, otherwise spawn one from scratch
        if (ent.inuse == true) {
            // the client has cleared the client side viewangles upon
            // connecting to the server, which is different than the
            // state when the game is saved, so we need to compensate
            // with deltaangles
            for (i = 0; i < 3; i++)
                ent.client.ps.pmove.delta_angles[i] = (short) Math3D
                        .ANGLE2SHORT(ent.client.ps.viewangles[i]);
        } else {
            // a spawn point will completely reinitialize the entity
            // except for the persistant data that was initialized at
            // ClientConnect() time
            GameUtil.G_InitEdict(ent, ent.index);
            ent.classname = "player";
            InitClientResp(ent.client);
            PutClientInServer(ent);
        }

        if (GameBase.level.intermissiontime != 0) {
            PlayerHud.MoveClientToIntermission(ent);
        } else {
            // send effect if in a multiplayer game
            if (GameBase.game.maxclients > 1) {
                GameBase.gi.WriteByte(Defines.svc_muzzleflash);
                GameBase.gi.WriteShort(ent.index);
                GameBase.gi.WriteByte(Defines.MZ_LOGIN);
                GameBase.gi.multicast(ent.s.origin, Defines.MULTICAST_PVS);

                GameBase.gi.bprintf(Defines.PRINT_HIGH, ent.client.pers.netname
                        + " entered the game\n");
            }
        }

        // make sure all view stuff is valid
        PlayerView.ClientEndServerFrame(ent);
    }

    /*
     * =========== ClientUserInfoChanged
     * 
     * called whenever the player updates a userinfo variable.
     * 
     * The game can override any of the settings in place (forcing skins or
     * names, etc) before copying it off. ============
     */
    public static String ClientUserinfoChanged(edict_t ent, String userinfo) {
        String s;
        int playernum;

        // check for malformed or illegal info strings
        if (!Info.Info_Validate(userinfo)) {
            return "\\name\\badinfo\\skin\\male/grunt";
        }

        // set name
        s = Info.Info_ValueForKey(userinfo, "name");

        ent.client.pers.netname = s;

        // set spectator
        s = Info.Info_ValueForKey(userinfo, "spectator");
        // spectators are only supported in deathmatch
        if (GameBase.deathmatch.value != 0 && !s.equals("0"))
            ent.client.pers.spectator = true;
        else
            ent.client.pers.spectator = false;

        // set skin
        s = Info.Info_ValueForKey(userinfo, "skin");

        playernum = ent.index - 1;

        // combine name and skin into a configstring
        GameBase.gi.configstring(Defines.CS_PLAYERSKINS + playernum,
                ent.client.pers.netname + "\\" + s);

        // fov
        if (GameBase.deathmatch.value != 0
                && 0 != ((int) GameBase.dmflags.value & Defines.DF_FIXED_FOV)) {
            ent.client.ps.fov = 90;
        } else {
            ent.client.ps.fov = Lib
                    .atoi(Info.Info_ValueForKey(userinfo, "fov"));
            if (ent.client.ps.fov < 1)
                ent.client.ps.fov = 90;
            else if (ent.client.ps.fov > 160)
                ent.client.ps.fov = 160;
        }

        // handedness
        s = Info.Info_ValueForKey(userinfo, "hand");
        if (s.length() > 0) {
            ent.client.pers.hand = Lib.atoi(s);
        }

        // save off the userinfo in case we want to check something later
        ent.client.pers.userinfo = userinfo;

        return userinfo;
    }

    /*
     * =========== ClientConnect
     * 
     * Called when a player begins connecting to the server. The game can refuse
     * entrance to a client by returning false. If the client is allowed, the
     * connection process will continue and eventually get to ClientBegin()
     * Changing levels will NOT cause this to be called again, but loadgames
     * will. ============
     */
    public static boolean ClientConnect(edict_t ent, String userinfo) {
        String value;

        // check to see if they are on the banned IP list
        value = Info.Info_ValueForKey(userinfo, "ip");
        if (GameSVCmds.SV_FilterPacket(value)) {
            userinfo = Info.Info_SetValueForKey1(userinfo, "rejmsg", "Banned.");
            return false;
        }

        // check for a spectator
        value = Info.Info_ValueForKey(userinfo, "spectator");
        if (GameBase.deathmatch.value != 0 && value.length() != 0
                && 0 != Lib.strcmp(value, "0")) {
            int i, numspec;

            if (!passwdOK(GameBase.spectator_password.string, value)) {
                userinfo = Info.Info_SetValueForKey1(userinfo, "rejmsg",
                        "Spectator password required or incorrect.");
                return false;
            }

            // count spectators
            for (i = numspec = 0; i < GameBase.maxclients.value; i++)
                if (GameBase.g_edicts[i + 1].inuse
                        && GameBase.g_edicts[i + 1].client.pers.spectator)
                    numspec++;

            if (numspec >= GameBase.maxspectators.value) {
                userinfo = Info.Info_SetValueForKey1(userinfo, "rejmsg",
                        "Server spectator limit is full.");
                return false;
            }
        } else {
            // check for a password
            value = Info.Info_ValueForKey(userinfo, "password");
            if (!passwdOK(GameBase.spectator_password.string, value)) {
                userinfo = Info.Info_SetValueForKey1(userinfo, "rejmsg",
                        "Password required or incorrect.");
                return false;
            }
        }

        // they can connect
        ent.client = GameBase.game.clients[ent.index - 1];

        // if there is already a body waiting for us (a loadgame), just
        // take it, otherwise spawn one from scratch
        if (ent.inuse == false) {
            // clear the respawning variables
            InitClientResp(ent.client);
            if (!GameBase.game.autosaved || null == ent.client.pers.weapon)
                InitClientPersistant(ent.client);
        }

        userinfo = ClientUserinfoChanged(ent, userinfo);

        if (GameBase.game.maxclients > 1)
            GameBase.gi.dprintf(ent.client.pers.netname + " connected\n");

        ent.svflags = 0; // make sure we start with known default
        ent.client.pers.connected = true;
        return true;
    }

    /*
     * =========== ClientDisconnect
     * 
     * Called when a player drops from the server. Will not be called between
     * levels. ============
     */
    public static void ClientDisconnect(edict_t ent) {
        int playernum;

        if (ent.client == null)
            return;

        GameBase.gi.bprintf(Defines.PRINT_HIGH, ent.client.pers.netname
                + " disconnected\n");

        // send effect
        GameBase.gi.WriteByte(Defines.svc_muzzleflash);
        GameBase.gi.WriteShort(ent.index);
        GameBase.gi.WriteByte(Defines.MZ_LOGOUT);
        GameBase.gi.multicast(ent.s.origin, Defines.MULTICAST_PVS);

        GameBase.gi.unlinkentity(ent);
        ent.s.modelindex = 0;
        ent.solid = Defines.SOLID_NOT;
        ent.inuse = false;
        ent.classname = "disconnected";
        ent.client.pers.connected = false;

        playernum = ent.index - 1;
        GameBase.gi.configstring(Defines.CS_PLAYERSKINS + playernum, "");
    }

    /*
     * static int CheckBlock(, int c) { int v, i; v = 0; for (i = 0; i < c; i++)
     * v += ((byte *) b)[i]; return v; }
     * 
     * public static void PrintPmove(pmove_t * pm) { unsigned c1, c2;
     * 
     * c1 = CheckBlock(& pm.s, sizeof(pm.s)); c2 = CheckBlock(& pm.cmd,
     * sizeof(pm.cmd)); Com_Printf("sv %3i:%i %i\n", pm.cmd.impulse, c1, c2); }
     */

    /*
     * ============== ClientThink
     * 
     * This will be called once for each client frame, which will usually be a
     * couple times for each server frame. ==============
     */
    public static void ClientThink(edict_t ent, usercmd_t ucmd) {
        gclient_t client;
        edict_t other;
        int i, j;
        pmove_t pm = null;

        GameBase.level.current_entity = ent;
        client = ent.client;

        if (GameBase.level.intermissiontime != 0) {
            client.ps.pmove.pm_type = Defines.PM_FREEZE;
            // can exit intermission after five seconds
            if (GameBase.level.time > GameBase.level.intermissiontime + 5.0f
                    && 0 != (ucmd.buttons & Defines.BUTTON_ANY))
                GameBase.level.exitintermission = true;
            return;
        }

        PlayerClientAdapters.pm_passent = ent;

        if (ent.client.chase_target != null) {

            client.resp.cmd_angles[0] = Math3D.SHORT2ANGLE(ucmd.angles[0]);
            client.resp.cmd_angles[1] = Math3D.SHORT2ANGLE(ucmd.angles[1]);
            client.resp.cmd_angles[2] = Math3D.SHORT2ANGLE(ucmd.angles[2]);

        } else {

            // set up for pmove
            //memset(& pm, 0, sizeof(pm));
            pm = new pmove_t();

            if (ent.movetype == Defines.MOVETYPE_NOCLIP)
                client.ps.pmove.pm_type = Defines.PM_SPECTATOR;
            else if (ent.s.modelindex != 255)
                client.ps.pmove.pm_type = Defines.PM_GIB;
            else if (ent.deadflag != 0)
                client.ps.pmove.pm_type = Defines.PM_DEAD;
            else
                client.ps.pmove.pm_type = Defines.PM_NORMAL;

            client.ps.pmove.gravity = (short) GameBase.sv_gravity.value;
            pm.s.set(client.ps.pmove);

            for (i = 0; i < 3; i++) {
                pm.s.origin[i] = (short) (ent.s.origin[i] * 8);
                pm.s.velocity[i] = (short) (ent.velocity[i] * 8);
            }

            if (client.old_pmove.equals(pm.s)) {
                pm.snapinitial = true;
                //		gi.dprintf ("pmove changed!\n");
            }

            // this should be a copy
            pm.cmd.set(ucmd);

            pm.trace = PlayerClientAdapters.PM_trace; // adds default parms
            pm.pointcontents = GameBase.gi.pointcontents;

            // perform a pmove
            GameBase.gi.Pmove(pm);

            // save results of pmove
            client.ps.pmove.set(pm.s);
            client.old_pmove.set(pm.s);

            for (i = 0; i < 3; i++) {
                ent.s.origin[i] = pm.s.origin[i] * 0.125f;
                ent.velocity[i] = pm.s.velocity[i] * 0.125f;
            }

            Math3D.VectorCopy(pm.mins, ent.mins);
            Math3D.VectorCopy(pm.maxs, ent.maxs);

            client.resp.cmd_angles[0] = Math3D.SHORT2ANGLE(ucmd.angles[0]);
            client.resp.cmd_angles[1] = Math3D.SHORT2ANGLE(ucmd.angles[1]);
            client.resp.cmd_angles[2] = Math3D.SHORT2ANGLE(ucmd.angles[2]);

            if (ent.groundentity != null && null == pm.groundentity
                    && (pm.cmd.upmove >= 10) && (pm.waterlevel == 0)) {
                GameBase.gi.sound(ent, Defines.CHAN_VOICE, GameBase.gi
                        .soundindex("*jump1.wav"), 1, Defines.ATTN_NORM, 0);
                GameWeapon.PlayerNoise(ent, ent.s.origin, Defines.PNOISE_SELF);
            }

            ent.viewheight = (int) pm.viewheight;
            ent.waterlevel = (int) pm.waterlevel;
            ent.watertype = pm.watertype;
            ent.groundentity = pm.groundentity;
            if (pm.groundentity != null)
                ent.groundentity_linkcount = pm.groundentity.linkcount;

            if (ent.deadflag != 0) {
                client.ps.viewangles[Defines.ROLL] = 40;
                client.ps.viewangles[Defines.PITCH] = -15;
                client.ps.viewangles[Defines.YAW] = client.killer_yaw;
            } else {
                Math3D.VectorCopy(pm.viewangles, client.v_angle);
                Math3D.VectorCopy(pm.viewangles, client.ps.viewangles);
            }

            GameBase.gi.linkentity(ent);

            if (ent.movetype != Defines.MOVETYPE_NOCLIP)
                GameBase.G_TouchTriggers(ent);

            // touch other objects
            for (i = 0; i < pm.numtouch; i++) {
                other = pm.touchents[i];
                for (j = 0; j < i; j++)
                    if (pm.touchents[j] == other)
                        break;
                if (j != i)
                    continue; // duplicated
                if (other.touch == null)
                    continue;
                other.touch.touch(other, ent, GameBase.dummyplane, null);
            }

        }

        client.oldbuttons = client.buttons;
        client.buttons = ucmd.buttons;
        client.latched_buttons |= client.buttons & ~client.oldbuttons;

        // save light level the player is standing on for
        // monster sighting AI
        ent.light_level = ucmd.lightlevel;

        // fire weapon from final position if needed
        if ((client.latched_buttons & Defines.BUTTON_ATTACK) != 0) {
            if (client.resp.spectator) {

                client.latched_buttons = 0;

                if (client.chase_target != null) {
                    client.chase_target = null;
                    client.ps.pmove.pm_flags &= ~pmove_t.PMF_NO_PREDICTION;
                } else
                    GameAI.GetChaseTarget(ent);

            } else if (!client.weapon_thunk) {
                client.weapon_thunk = true;
                GamePWeapon.Think_Weapon(ent);
            }
        }

        if (client.resp.spectator) {
            if (ucmd.upmove >= 10) {
                if (0 == (client.ps.pmove.pm_flags & pmove_t.PMF_JUMP_HELD)) {
                    client.ps.pmove.pm_flags |= pmove_t.PMF_JUMP_HELD;
                    if (client.chase_target != null)
                        GameAI.ChaseNext(ent);
                    else
                        GameAI.GetChaseTarget(ent);
                }
            } else
                client.ps.pmove.pm_flags &= ~pmove_t.PMF_JUMP_HELD;
        }

        // update chase cam if being followed
        for (i = 1; i <= GameBase.maxclients.value; i++) {
            other = GameBase.g_edicts[i];
            if (other.inuse && other.client.chase_target == ent)
                GameAI.UpdateChaseCam(other);
        }
    }

    /*
     * ============== ClientBeginServerFrame
     * 
     * This will be called once for each server frame, before running any other
     * entities in the world. ==============
     */
    public static void ClientBeginServerFrame(edict_t ent) {
        gclient_t client;
        int buttonMask;

        if (GameBase.level.intermissiontime != 0)
            return;

        client = ent.client;

        if (GameBase.deathmatch.value != 0
                && client.pers.spectator != client.resp.spectator
                && (GameBase.level.time - client.respawn_time) >= 5) {
            spectator_respawn(ent);
            return;
        }

        // run weapon animations if it hasn't been done by a ucmd_t
        if (!client.weapon_thunk && !client.resp.spectator)
            GamePWeapon.Think_Weapon(ent);
        else
            client.weapon_thunk = false;

        if (ent.deadflag != 0) {
            // wait for any button just going down
            if (GameBase.level.time > client.respawn_time) {
                // in deathmatch, only wait for attack button
                if (GameBase.deathmatch.value != 0)
                    buttonMask = Defines.BUTTON_ATTACK;
                else
                    buttonMask = -1;

                if ((client.latched_buttons & buttonMask) != 0
                        || (GameBase.deathmatch.value != 0 && 0 != ((int) GameBase.dmflags.value & Defines.DF_FORCE_RESPAWN))) {
                    respawn(ent);
                    client.latched_buttons = 0;
                }
            }
            return;
        }

        // add player trail so monsters can follow
        if (GameBase.deathmatch.value != 0)
            if (!GameUtil.visible(ent, PlayerTrail.LastSpot()))
                PlayerTrail.Add(ent.s.old_origin);

        client.latched_buttons = 0;
    }
}