]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_client.qc
SP_LMS_RANK) // allow writing to this field in intermission as it is needed for newly...
[divverent/nexuiz.git] / data / qcsrc / server / cl_client.qc
1 .float spectatee_status;
2 .float zoomstate;
3 .float bloodloss_timer;
4
5 .entity clientdata;
6 float ClientData_Send(entity to, float sf)
7 {
8         if(to != self.owner)
9         {
10                 error("wtf");
11                 return FALSE;
12         }
13
14         entity e;
15
16         e = to;
17         if(to.classname == "spectator")
18                 e = to.enemy;
19
20         sf = 0;
21
22         if(e.race_completed)
23                 sf |= 1; // forced scoreboard
24         if(to.spectatee_status)
25                 sf |= 2; // spectator ent number follows
26         if(e.zoomstate)
27                 sf |= 4; // zoomed
28         if(e.porto_v_angle_held)
29                 sf |= 8; // angles held
30         
31         WriteByte(MSG_ENTITY, ENT_CLIENT_CLIENTDATA);
32         WriteByte(MSG_ENTITY, sf);
33
34         if(sf & 2)
35                 WriteByte(MSG_ENTITY, to.spectatee_status);
36         
37         if(sf & 8)
38         {
39                 WriteAngle(MSG_ENTITY, e.v_angle_x);
40                 WriteAngle(MSG_ENTITY, e.v_angle_y);
41         }
42
43         return TRUE;
44 }
45
46 void ClientData_Attach()
47 {
48         Net_LinkEntity(self.clientdata = spawn(), FALSE, 0, ClientData_Send);
49         self.clientdata.drawonlytoclient = self;
50         self.clientdata.owner = self;
51 }
52
53 void ClientData_Detach()
54 {
55         remove(self.clientdata);
56         self.clientdata = world;
57 }
58
59 void ClientData_Touch(entity e)
60 {
61         e.clientdata.SendFlags = 1;
62
63         // make it spectatable
64         entity e2;
65         FOR_EACH_REALCLIENT(e2)
66         {
67                 if(e2 != e)
68                         if(e2.classname == "spectator")
69                                 if(e2.enemy == e)
70                                         e2.clientdata.SendFlags = 1;
71         }
72 }
73
74
75 #define SPAWNPOINT_SCORE frags
76
77 .float wantswelcomemessage;
78 .string netname_previous;
79
80 void spawnfunc_info_player_survivor (void)
81 {
82         spawnfunc_info_player_deathmatch();
83 }
84
85 void spawnfunc_info_player_start (void)
86 {
87         spawnfunc_info_player_deathmatch();
88 }
89
90 void spawnfunc_info_player_deathmatch (void)
91 {
92         self.classname = "info_player_deathmatch";
93         relocate_spawnpoint();
94 }
95
96 void spawnpoint_use()
97 {
98         if(teams_matter)
99         if(have_team_spawns)
100         {
101                 self.team = activator.team;
102                 some_spawn_has_been_used = 1;
103         }
104 };
105
106 // Returns:
107 //   -1 if a spawn can't be used
108 //   otherwise, a weight of the spawnpoint
109 float Spawn_Score(entity spot, entity playerlist, float teamcheck)
110 {
111         float shortest, thisdist;
112         entity player;
113
114         // filter out spots for the wrong team
115         if(teamcheck)
116         if(spot.team != teamcheck)
117                 return -1;
118
119         if(race_spawns)
120                 if(spot.target == "")
121                         return -1;
122
123         // filter out spots for assault
124         if(spot.target != "") {
125                 local entity ent;
126                 float good, found;
127                 ent = find(world, targetname, spot.target);
128                 while(ent) {
129                         found = 1;
130                         if(ent.classname == "target_objective")
131                         {
132                                 if(ent.health < 0 || ent.health >= ASSAULT_VALUE_INACTIVE)
133                                         return -1;
134                                 good = 1;
135                         }
136                         else if(ent.classname == "trigger_race_checkpoint")
137                         {
138                                 if(self.classname == "player") // spectators may spawn everywhere
139                                 {
140                                         if(g_race_qualifying)
141                                         {
142                                                 // spawn at first
143                                                 if(ent.race_checkpoint != 0)
144                                                         return -1;
145                                                 if(spot.race_place != race_lowest_place_spawn)
146                                                         return -1;
147                                         }
148                                         else
149                                         {
150                                                 if(ent.race_checkpoint != race_PreviousCheckpoint(self.race_checkpoint))
151                                                         return -1;
152                                                 float pl;
153                                                 pl = self.race_place;
154                                                 if(pl > race_highest_place_spawn)
155                                                         pl = 0;
156                                                 if(spot.race_place != pl)
157                                                         return -1;
158                                         }
159                                 }
160                                 good = 1;
161                         }
162                         else
163                         {
164                         }
165                         ent = find(ent, targetname, spot.target);
166                 }
167
168                 if(found && !good)
169                         return -1;
170         }
171
172         player = playerlist;
173         shortest = vlen(world.maxs - world.mins);
174         for(player = playerlist; player; player = player.chain)
175                 if (player != self)
176                 {
177                         thisdist = vlen(player.origin - spot.origin);
178                         if (thisdist < shortest)
179                                 shortest = thisdist;
180                 }
181         return shortest;
182 }
183
184 float spawn_allbad;
185 float spawn_allgood;
186 entity Spawn_FilterOutBadSpots(entity firstspot, entity playerlist, float mindist, float teamcheck)
187 {
188         local entity spot, spotlist, spotlistend;
189         spawn_allgood = TRUE;
190         spawn_allbad = TRUE;
191
192         spotlist = world;
193         spotlistend = world;
194
195         for(spot = firstspot; spot; spot = spot.chain)
196         {
197                 spot.SPAWNPOINT_SCORE = Spawn_Score(spot, playerlist, teamcheck);
198
199                 if(cvar("spawn_debugview"))
200                 {
201                         setmodel(spot, "models/runematch/rune.mdl");
202                         if(spot.SPAWNPOINT_SCORE < mindist)
203                         {
204                                 spot.colormod = '1 0 0';
205                                 spot.scale = 1;
206                         }
207                         else
208                         {
209                                 spot.colormod = '0 1 0';
210                                 spot.scale = spot.SPAWNPOINT_SCORE / mindist;
211                         }
212                 }
213
214                 if(spot.SPAWNPOINT_SCORE >= 0) // spawning allowed here
215                 {
216                         if(spot.SPAWNPOINT_SCORE < mindist)
217                         {
218                                 // too short distance
219                                 spawn_allgood = FALSE;
220                         }
221                         else 
222                         {
223                                 // perfect
224                                 spawn_allbad = FALSE;
225
226                                 if(spotlistend)
227                                         spotlistend.chain = spot;
228                                 spotlistend = spot;
229                                 if(!spotlist)
230                                         spotlist = spot;
231
232                                 /*
233                                 if(teamcheck)
234                                 if(spot.team != teamcheck)
235                                         error("invalid spawn added");
236
237                                 print("added ", etos(spot), "\n");
238                                 */
239                         }
240                 }
241         }
242         if(spotlistend)
243                 spotlistend.chain = world;
244
245         /*
246                 entity e;
247                 if(teamcheck)
248                         for(e = spotlist; e; e = e.chain)
249                         {
250                                 print("seen ", etos(e), "\n");
251                                 if(e.team != teamcheck)
252                                         error("invalid spawn found");
253                         }
254         */
255
256         return spotlist;
257 }
258
259 entity Spawn_WeightedPoint(entity firstspot, float lower, float upper, float exponent)
260 {
261         // weight of a point: bound(lower, mindisttoplayer, upper)^exponent
262         // multiplied by spot.cnt (useful if you distribute many spawnpoints in a small area)
263         local entity spot;
264
265         RandomSelection_Init();
266         for(spot = firstspot; spot; spot = spot.chain)
267                 RandomSelection_Add(spot, 0, pow(bound(lower, spot.SPAWNPOINT_SCORE, upper), exponent) * spot.cnt, spot.SPAWNPOINT_SCORE >= lower);
268
269         return RandomSelection_chosen_ent;
270 }
271
272 /*
273 =============
274 SelectSpawnPoint
275
276 Finds a point to respawn
277 =============
278 */
279 entity SelectSpawnPoint (float anypoint)
280 {
281         local float teamcheck;
282         local entity firstspot_new;
283         local entity spot, firstspot, playerlist;
284
285         spot = find (world, classname, "testplayerstart");
286         if (spot)
287                 return spot;
288
289         teamcheck = 0;
290
291         if(!anypoint && have_team_spawns)
292                 teamcheck = self.team;
293
294         // get the list of players
295         playerlist = findchain(classname, "player");
296         // get the entire list of spots
297         firstspot = findchain(classname, "info_player_deathmatch");
298         // filter out the bad ones
299         // (note this returns the original list if none survived)
300         firstspot_new = Spawn_FilterOutBadSpots(firstspot, playerlist, 100, teamcheck);
301         if(!firstspot_new)
302                 firstspot_new = Spawn_FilterOutBadSpots(firstspot, playerlist, -1, teamcheck);
303         firstspot = firstspot_new;
304
305         // there is 50/50 chance of choosing a random spot or the furthest spot
306         // (this means that roughly every other spawn will be furthest, so you
307         // usually won't get fragged at spawn twice in a row)
308         if (arena_roundbased)
309         {
310                 firstspot_new = Spawn_FilterOutBadSpots(firstspot, playerlist, 800, teamcheck);
311                 if(firstspot_new)
312                         firstspot = firstspot_new;
313                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
314         }
315         else if (random() > cvar("g_spawn_furthest"))
316                 spot = Spawn_WeightedPoint(firstspot, 1, 1, 1);
317         else
318                 spot = Spawn_WeightedPoint(firstspot, 1, 5000, 5); // chooses a far far away spawnpoint
319
320         if(cvar("spawn_debugview"))
321         {
322                 print("spot mindistance: ", ftos(spot.SPAWNPOINT_SCORE), "\n");
323
324                 entity e;
325                 if(teamcheck)
326                         for(e = firstspot; e; e = e.chain)
327                                 if(e.team != teamcheck)
328                                         error("invalid spawn found");
329         }
330
331         if (!spot)
332         {
333                 if(cvar("spawn_debug"))
334                         GotoNextMap();
335                 else
336                 {
337                         if(some_spawn_has_been_used)
338                                 return world; // team can't spawn any more, because of actions of other team
339                         else
340                                 error("Cannot find a spawn point - please fix the map!");
341                 }
342         }
343
344         return spot;
345 }
346
347 /*
348 =============
349 CheckPlayerModel
350
351 Checks if the argument string can be a valid playermodel.
352 Returns a valid one in doubt.
353 =============
354 */
355 string FallbackPlayerModel = "models/player/marine.zym";
356 string CheckPlayerModel(string plyermodel) {
357         if(strlen(plyermodel) < 4)
358                 return FallbackPlayerModel;
359         if( substring(plyermodel,0,14) != "models/player/")
360                 return FallbackPlayerModel;
361         else if(cvar("sv_servermodelsonly"))
362         {
363                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".zym")
364                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".dpm")
365                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".md3")
366                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".psk")
367                         return FallbackPlayerModel;
368                 if(plyermodel != strtolower(plyermodel))
369                         return FallbackPlayerModel;
370                 if(!fexists(plyermodel))
371                         return FallbackPlayerModel;
372         }
373         return plyermodel;
374 }
375
376 /*
377 =============
378 Client_customizeentityforclient
379
380 LOD reduction
381 =============
382 */
383 float Client_customizeentityforclient()
384 {
385 #ifdef ALLOW_VARIABLE_LOD
386         // self: me
387         // other: the player viewing me
388         float distance;
389         float f;
390
391         if(self.flags & FL_NOTARGET) // we don't need LOD for spectators
392                 return TRUE;
393
394         if(other.cvar_cl_playerdetailreduction <= 0)
395         {
396                 if(other.cvar_cl_playerdetailreduction <= -2)
397                         self.modelindex = self.modelindex_lod2;
398                 else if(other.cvar_cl_playerdetailreduction <= -1)
399                         self.modelindex = self.modelindex_lod1;
400                 else
401                         self.modelindex = self.modelindex_lod0;
402         }
403         else
404         {
405                 distance = vlen(self.origin - other.origin);
406                 f = (distance + 100.0) * other.cvar_cl_playerdetailreduction;
407                 if(f > 10000)
408                         self.modelindex = self.modelindex_lod2;
409                 else if(f > 5000)
410                         self.modelindex = self.modelindex_lod1;
411                 else
412                         self.modelindex = self.modelindex_lod0;
413         }
414 #endif
415
416         return TRUE;
417 }
418
419 void UpdatePlayerSounds();
420 void setmodel_lod(entity e, string modelname)
421 {
422 #ifdef ALLOW_VARIABLE_LOD
423         string s;
424
425         // FIXME: this only supports 3-letter extensions
426         s = strcat(substring(modelname, 0, strlen(modelname) - 4), "_1", substring(modelname, 0, strlen(modelname) - 4));
427         if(fexists(s))
428         {
429                 precache_model(s);
430                 setmodel(e, s); // players have high precision
431                 self.modelindex_lod1 = self.modelindex;
432         }
433         else
434                 self.modelindex_lod1 = -1;
435
436         s = strcat(substring(modelname, 0, strlen(modelname) - 4), "_2", substring(modelname, 0, strlen(modelname) - 4));
437         if(fexists(s))
438         {
439                 precache_model(s);
440                 setmodel(e, s); // players have high precision
441                 self.modelindex_lod2 = self.modelindex;
442         }
443         else
444                 self.modelindex_lod2 = -1;
445
446         precache_model(modelname);
447         setmodel(e, modelname); // players have high precision
448         self.modelindex_lod0 = self.modelindex;
449
450         if(self.modelindex_lod1 < 0)
451                 self.modelindex_lod1 = self.modelindex;
452
453         if(self.modelindex_lod2 < 0)
454                 self.modelindex_lod2 = self.modelindex;
455 #else
456         precache_model(modelname);
457         setmodel(e, modelname); // players have high precision
458 #endif
459         player_setupanimsformodel();
460         UpdatePlayerSounds();
461 }
462
463 /*
464 =============
465 PutObserverInServer
466
467 putting a client as observer in the server
468 =============
469 */
470 void PutObserverInServer (void)
471 {
472         entity  spot;
473
474         race_PreSpawnObserver();
475
476         spot = SelectSpawnPoint (TRUE);
477         if(!spot)
478                 error("No spawnpoints for observers?!?\n");
479         RemoveGrapplingHook(self); // Wazat's Grappling Hook
480
481         if(clienttype(self) == CLIENTTYPE_REAL)
482         {
483                 msg_entity = self;
484                 WriteByte(MSG_ONE, SVC_SETVIEW);
485                 WriteEntity(MSG_ONE, self);
486         }
487
488         DropAllRunes(self);
489         kh_Key_DropAll(self, TRUE);
490
491         Portal_ClearAll(self);
492
493         if(self.flagcarried)
494                 DropFlag(self.flagcarried, world, world);
495
496         WaypointSprite_PlayerDead();
497         
498         if(self.killcount != -666)
499         {
500                 if(g_lms)
501                 {
502                         if(PlayerScore_Add(self, SP_LMS_RANK, 0) > 0)
503                                 bprint ("^4", self.netname, "^4 has no more lives left\n");
504                         else
505                                 bprint ("^4", self.netname, "^4 is spectating now\n"); // TODO turn this into a proper forfeit?
506                 }
507                 else
508                         bprint ("^4", self.netname, "^4 is spectating now\n");
509         }
510
511         PlayerScore_Clear(self); // clear scores when needed
512
513         self.spectatortime = time;
514         
515         self.classname = "observer";
516         self.iscreature = FALSE;
517         self.health = -666;
518         self.takedamage = DAMAGE_NO;
519         self.solid = SOLID_NOT;
520         self.movetype = MOVETYPE_NOCLIP;
521         self.flags = FL_CLIENT | FL_NOTARGET;
522         self.armorvalue = 666;
523         self.effects = 0;
524         self.armorvalue = cvar("g_balance_armor_start");
525         self.pauserotarmor_finished = 0;
526         self.pauserothealth_finished = 0;
527         self.pauseregen_finished = 0;
528         self.damageforcescale = 0;
529         self.death_time = 0;
530         self.dead_frame = 0;
531         self.alpha = 0;
532         self.scale = 0;
533         self.fade_time = 0;
534         self.pain_frame = 0;
535         self.pain_finished = 0;
536         self.strength_finished = 0;
537         self.invincible_finished = 0;
538         self.pushltime = 0;
539         self.think = SUB_Null;
540         self.nextthink = 0;
541         self.hook_time = 0;
542         self.runes = 0;
543         self.deadflag = DEAD_NO;
544         self.angles = spot.angles;
545         self.angles_z = 0;
546         self.fixangle = TRUE;
547         self.crouch = FALSE;
548
549         self.view_ofs = PL_VIEW_OFS;
550         setorigin (self, spot.origin);
551         setsize (self, '0 0 0', '0 0 0');
552         self.oldorigin = self.origin;
553         self.items = 0;
554         self.weapons = 0;
555         self.model = "";
556         self.modelindex = 0;
557         self.weapon = 0;
558         self.weaponmodel = "";
559         self.weaponentity = world;
560         self.killcount = -666;
561         self.velocity = '0 0 0';
562         self.avelocity = '0 0 0';
563         self.punchangle = '0 0 0';
564         self.punchvector = '0 0 0';
565         self.oldvelocity = self.velocity;
566         self.customizeentityforclient = Client_customizeentityforclient;
567         self.wantswelcomemessage = 1;
568
569         if(g_arena)
570         {
571                 if(self.version_mismatch)
572                 {
573                         Spawnqueue_Unmark(self);
574                         Spawnqueue_Remove(self);
575                 }
576                 else
577                 {
578                         Spawnqueue_Insert(self);
579                 }
580         }
581         else if(g_lms)
582         {
583                 // Only if the player cannot play at all
584                 if(PlayerScore_Add(self, SP_LMS_RANK, 0) == 666)
585                         self.frags = FRAGS_SPECTATOR;
586                 else
587                         self.frags = FRAGS_LMS_LOSER;
588         }
589         else
590                 self.frags = FRAGS_SPECTATOR;
591 }
592
593 float RestrictSkin(float s)
594 {
595         if(!teams_matter)
596                 return s;
597         if(s == 6)
598                 return 6;
599         return mod(s, 3);
600 }
601
602 void FixPlayermodel()
603 {
604         local string defaultmodel;
605         local float defaultskin;
606         local vector m1, m2;
607
608         defaultmodel = "";
609
610         if(cvar("sv_defaultcharacter") == 1) {
611                 defaultskin = 0;
612
613                 if(teams_matter)
614                 {
615                         defaultmodel = cvar_string(strcat("sv_defaultplayermodel_", Team_ColorNameLowerCase(self.team)));
616                         defaultskin = cvar(strcat("sv_defaultplayerskin_", Team_ColorNameLowerCase(self.team)));
617                 }
618
619                 if(defaultmodel == "")
620                 {
621                         defaultmodel = cvar_string("sv_defaultplayermodel");
622                         defaultskin = cvar("sv_defaultplayerskin");
623                 }
624         }
625
626         if(self.modelindex == 0)
627         {
628                 if(self.model != "")
629                         bprint("\{1}^1Player ", self.netname, "^1 has a zero modelindex, trying to fix...\n");
630                 self.model = ""; // force the != checks to return true
631         }
632
633         if(defaultmodel != "")
634         {
635                 if (defaultmodel != self.model)
636                 {
637                         m1 = self.mins;
638                         m2 = self.maxs;
639                         setmodel_lod (self, defaultmodel);
640                         setsize (self, m1, m2);
641                 }
642
643                 self.skin = defaultskin;
644         } else {
645                 if (self.playermodel != self.model)
646                 {
647                         self.playermodel = CheckPlayerModel(self.playermodel);
648                         m1 = self.mins;
649                         m2 = self.maxs;
650                         setmodel_lod (self, self.playermodel);
651                         setsize (self, m1, m2);
652                 }
653
654                 self.skin = RestrictSkin(stof(self.playerskin));
655         }
656
657         if(!teams_matter)
658                 if(strlen(cvar_string("sv_defaultplayercolors")))
659                         if(self.clientcolors != cvar("sv_defaultplayercolors"))
660                                 setcolor(self, cvar("sv_defaultplayercolors"));
661 }
662
663 void PlayerTouchExplode(entity p1, entity p2)
664 {
665         vector org;
666         org = (p1.origin + p2.origin) * 0.5;
667         org_z += (p1.mins_z + p2.mins_z) * 0.5;
668
669         te_explosion(org);
670
671         entity e;
672         e = spawn();
673         setorigin(e, org);
674         RadiusDamage(e, world, g_touchexplode_damage, g_touchexplode_edgedamage, g_touchexplode_radius, world, g_touchexplode_force, DEATH_TOUCHEXPLODE, world);
675         remove(e);
676 }
677
678 /*
679 =============
680 PutClientInServer
681
682 Called when a client spawns in the server
683 =============
684 */
685 //void() ctf_playerchanged;
686 void PutClientInServer (void)
687 {
688         if(clienttype(self) == CLIENTTYPE_BOT)
689         {
690                 self.classname = "player";
691         }
692         else if(clienttype(self) == CLIENTTYPE_REAL)
693         {
694                 msg_entity = self;
695                 WriteByte(MSG_ONE, SVC_SETVIEW);
696                 WriteEntity(MSG_ONE, self);
697         }
698
699         // player is dead and becomes observer
700         // FIXME fix LMS scoring for new system
701         if(g_lms)
702         {
703                 if(PlayerScore_Add(self, SP_LMS_RANK, 0) > 0)
704                         self.classname = "observer";
705         }
706
707         if(g_arena)
708         if(!self.spawned)
709                 self.classname = "observer";
710
711         if(self.classname == "player") {
712                 entity spot, oldself;
713
714                 race_PreSpawn();
715
716                 spot = SelectSpawnPoint (FALSE);
717                 if(!spot)
718                 {
719                         centerprint(self, "Sorry, no spawnpoints available!\nHope your team can fix it...");
720                         return; // spawn failed
721                 }
722
723                 RemoveGrapplingHook(self); // Wazat's Grappling Hook
724                 self.campingrifle_bulletcounter = 0;
725
726                 self.classname = "player";
727                 self.iscreature = TRUE;
728                 self.movetype = MOVETYPE_WALK;
729                 self.solid = SOLID_SLIDEBOX;
730                 self.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_PLAYERCLIP;
731                 self.frags = FRAGS_PLAYER;
732                 if(independent_players)
733                         MAKE_INDEPENDENT_PLAYER(self);
734                 self.flags = FL_CLIENT;
735                 self.takedamage = DAMAGE_AIM;
736                 if(g_minstagib)
737                         self.effects = EF_FULLBRIGHT;
738                 else
739                         self.effects = 0;
740                 self.air_finished = time + 12;
741                 self.dmg = 2;
742
743                 if(inWarmupStage)
744                 {
745                         self.ammo_shells = warmup_start_ammo_shells;
746                         self.ammo_nails = warmup_start_ammo_nails;
747                         self.ammo_rockets = warmup_start_ammo_rockets;
748                         self.ammo_cells = warmup_start_ammo_cells;
749                         self.health = warmup_start_health;
750                         self.armorvalue = warmup_start_armorvalue;
751                         self.weapons = warmup_start_weapons;
752                 }
753                 else
754                 {
755                         self.ammo_shells = start_ammo_shells;
756                         self.ammo_nails = start_ammo_nails;
757                         self.ammo_rockets = start_ammo_rockets;
758                         self.ammo_cells = start_ammo_cells;
759                         self.health = start_health;
760                         self.armorvalue = start_armorvalue;
761                         self.weapons = start_weapons;
762                 }
763                 self.items = start_items;
764                 self.switchweapon = w_getbestweapon(self);
765                 self.cnt = self.switchweapon;
766                 self.weapon = 0;
767                 self.jump_interval = time;
768
769                 self.spawnshieldtime = time + cvar("g_spawnshieldtime");
770                 self.pauserotarmor_finished = time + cvar("g_balance_pause_armor_rot_spawn");
771                 self.pauserothealth_finished = time + cvar("g_balance_pause_health_rot_spawn");
772                 self.pauseregen_finished = time + cvar("g_balance_pause_health_regen_spawn");
773                 //extend the pause of rotting if client was reset at the beginning of the countdown
774                 if(!cvar("sv_ready_restart_after_countdown") && time < game_starttime) { // TODO why is this cvar NOTted?
775                         self.spawnshieldtime += game_starttime - time;
776                         self.pauserotarmor_finished += game_starttime - time;
777                         self.pauserothealth_finished += game_starttime - time;
778                         self.pauseregen_finished += game_starttime - time;
779                 }
780                 self.damageforcescale = 2;
781                 self.death_time = 0;
782                 self.dead_frame = 0;
783                 self.alpha = 0;
784                 self.scale = 0;
785                 self.fade_time = 0;
786                 self.pain_frame = 0;
787                 self.pain_finished = 0;
788                 self.strength_finished = 0;
789                 self.invincible_finished = 0;
790                 self.pushltime = 0;
791                 //self.speed_finished = 0;
792                 //self.slowmo_finished = 0;
793                 // players have no think function
794                 self.think = SUB_Null;
795                 self.nextthink = 0;
796                 self.hook_time = 0;
797                 self.dmg_team = 0;
798
799                 self.runes = 0;
800
801                 self.deadflag = DEAD_NO;
802
803                 self.angles = spot.angles;
804
805                 self.angles_z = 0; // never spawn tilted even if the spot says to
806                 self.fixangle = TRUE; // turn this way immediately
807                 self.velocity = '0 0 0';
808                 self.avelocity = '0 0 0';
809                 self.punchangle = '0 0 0';
810                 self.punchvector = '0 0 0';
811                 self.oldvelocity = self.velocity;
812
813                 msg_entity = self;
814                 WRITESPECTATABLE_MSG_ONE({
815                         WriteByte(MSG_ONE, SVC_TEMPENTITY);
816                         WriteByte(MSG_ONE, TE_CSQC_SPAWN);
817                 });
818
819                 self.customizeentityforclient = Client_customizeentityforclient;
820
821                 self.model = "";
822                 FixPlayermodel();
823
824                 self.crouch = FALSE;
825                 self.view_ofs = PL_VIEW_OFS;
826                 setsize (self, PL_MIN, PL_MAX);
827                 self.spawnorigin = spot.origin;
828                 setorigin (self, spot.origin + '0 0 1' * (1 - self.mins_z - 24));
829                 // don't reset back to last position, even if new position is stuck in solid
830                 self.oldorigin = self.origin;
831
832                 if(g_arena)
833                 {
834                         Spawnqueue_Remove(self);
835                         Spawnqueue_Mark(self);
836                 }
837
838                 self.event_damage = PlayerDamage;
839
840                 self.bot_attack = TRUE;
841
842                 self.statdraintime = time + 5;
843                 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = 0;
844
845                 if(self.killcount == -666) {
846                         PlayerScore_Clear(self);
847                         self.killcount = 0;
848                 }
849
850                 self.cnt = WEP_LASER;
851                 self.nixnex_lastchange_id = -1;
852
853                 CL_SpawnWeaponentity();
854                 self.alpha = default_player_alpha;
855                 self.colormod = '1 1 1' * cvar("g_player_brightness");
856                 self.exteriorweaponentity.alpha = default_weapon_alpha;
857
858                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval")*2;
859                 self.lms_traveled_distance = 0;
860                 self.speedrunning = FALSE;
861
862                 race_PostSpawn(spot);
863
864                 if(cvar("spawn_debug"))
865                 {
866                         sprint(self, strcat("spawnpoint origin:  ", vtos(spot.origin), "\n"));
867                         remove(spot);   // usefull for checking if there are spawnpoints, that let drop through the floor
868                 }
869
870                 //stuffcmd(self, "chase_active 0");
871                 //stuffcmd(self, "set viewsize $tmpviewsize \n");
872
873                 if (cvar("g_spawnsound"))
874                         sound (self, CHAN_TRIGGER, "misc/spawn.wav", VOL_BASE, ATTN_NORM);
875
876                 if(g_assault) {
877                         if(self.team == assault_attacker_team)
878                                 centerprint(self, "You are attacking!");
879                         else
880                                 centerprint(self, "You are defending!");
881                 }
882
883                 target_voicescript_clear(self);
884
885                 oldself = self;
886                 self = spot;
887                         activator = oldself;
888                                 SUB_UseTargets();
889                         activator = world;
890                 self = oldself;
891
892         } else if(self.classname == "observer") {
893                 PutObserverInServer ();
894         }
895
896         //if(g_ctf)
897         //      ctf_playerchanged();
898 }
899
900 float ClientInit_SendEntity(entity to, float sf)
901 {
902         float i;
903         WriteByte(MSG_ENTITY, ENT_CLIENT_INIT);
904         WriteShort(MSG_ENTITY, CSQC_REVISION);
905         for(i = 1; i <= 24; ++i)
906                 WriteByte(MSG_ENTITY, (get_weaponinfo(i)).impulse + 1);
907         WriteCoord(MSG_ENTITY, hook_shotorigin_x);
908         WriteCoord(MSG_ENTITY, hook_shotorigin_y);
909         WriteCoord(MSG_ENTITY, hook_shotorigin_z);
910
911         if(sv_foginterval && world.fog != "")
912                 WriteString(MSG_ENTITY, world.fog);
913         else
914                 WriteString(MSG_ENTITY, "");
915
916         return TRUE;
917 }
918
919 void ClientInit_Spawn()
920 {
921         Net_LinkEntity(spawn(), FALSE, 0, ClientInit_SendEntity);
922 }
923
924 /*
925 =============
926 SetNewParms
927 =============
928 */
929 void SetNewParms (void)
930 {
931         // initialize parms for a new player
932         parm1 = -(86400 * 366);
933 }
934
935 /*
936 =============
937 SetChangeParms
938 =============
939 */
940 void SetChangeParms (void)
941 {
942         // save parms for level change
943         parm1 = self.parm_idlesince - time;
944 }
945
946 /*
947 =============
948 DecodeLevelParms
949 =============
950 */
951 void DecodeLevelParms (void)
952 {
953         // load parms
954         self.parm_idlesince = parm1;
955         if(self.parm_idlesince == -(86400 * 366))
956                 self.parm_idlesince = time;
957
958         // whatever happens, allow 60 seconds of idling directly after connect for map loading
959         self.parm_idlesince = max(self.parm_idlesince, time - sv_maxidle + 60);
960 }
961
962 /*
963 =============
964 ClientKill
965
966 Called when a client types 'kill' in the console
967 =============
968 */
969
970 void ClientKill_Now_TeamChange()
971 {
972         if(self.killindicator_teamchange == -1)
973         {
974                 self.team = -1;
975                 JoinBestTeam( self, FALSE, FALSE );
976         }
977         else
978         {
979                 SV_ChangeTeam(self.killindicator_teamchange - 1);
980         }
981 }
982
983 void ClientKill_Now()
984 {
985         if(self.killindicator_teamchange)
986                 ClientKill_Now_TeamChange();
987
988         // in any case:
989         Damage(self, self, self, 100000, DEATH_KILL, self.origin, '0 0 0');
990
991         if(self.killindicator)
992         {
993                 dprint("Cleaned up after a leaked kill indicator.\n");
994                 remove(self.killindicator);
995                 self.killindicator = world;
996         }
997 }
998 void KillIndicator_Think()
999 {
1000         if (!self.owner.modelindex)
1001         {
1002                 self.owner.killindicator = world;
1003                 remove(self);
1004                 return;
1005         }
1006
1007         if(self.cnt <= 0)
1008         {
1009                 self = self.owner;
1010                 ClientKill_Now(); // no oldself needed
1011                 return;
1012         }
1013         else
1014         {
1015                 if(self.cnt <= 10)
1016                         setmodel(self, strcat("models/sprites/", ftos(self.cnt), ".spr32"));
1017                 if(clienttype(self.owner) == CLIENTTYPE_REAL)
1018                 {
1019                         if(self.cnt <= 10)
1020                                 announce(self.owner, strcat("announcer/robotic/", ftos(self.cnt), ".wav"));
1021                         if(self.owner.killindicator_teamchange)
1022                         {
1023                                 if(self.owner.killindicator_teamchange == -1)
1024                                         centerprint(self.owner, strcat("Changing team in ", ftos(self.cnt), " seconds"));
1025                                 else
1026                                         centerprint(self.owner, strcat("Changing to ", ColoredTeamName(self.owner.killindicator_teamchange), " in ", ftos(self.cnt), " seconds"));
1027                         }
1028                         else
1029                                 centerprint(self.owner, strcat("^1Suicide in ", ftos(self.cnt), " seconds"));
1030                 }
1031                 self.nextthink = time + 1;
1032                 self.cnt -= 1;
1033         }
1034 }
1035
1036 void ClientKill_TeamChange (float targetteam) // 0 = don't change, -1 = auto
1037 {
1038         float killtime;
1039         entity e;
1040         killtime = cvar("g_balance_kill_delay");
1041
1042         self.killindicator_teamchange = targetteam;
1043
1044         if(!self.killindicator)
1045         {
1046                 if(killtime <= 0 || !self.modelindex || self.deadflag != DEAD_NO)
1047                 {
1048                         ClientKill_Now();
1049                 }
1050                 else
1051                 {
1052                         self.killindicator = spawn();
1053                         self.killindicator.owner = self;
1054                         self.killindicator.scale = 0.5;
1055                         setattachment(self.killindicator, self, "");
1056                         setorigin(self.killindicator, '0 0 52');
1057                         self.killindicator.think = KillIndicator_Think;
1058                         self.killindicator.nextthink = time + (self.lip) * 0.05;
1059                         self.killindicator.cnt = ceil(killtime);
1060                         self.killindicator.count = bound(0, ceil(killtime), 10);
1061                         sprint(self, strcat("^1You'll be dead in ", ftos(self.killindicator.cnt), " seconds\n"));
1062
1063                         for(e = world; (e = find(e, classname, "body")) != world; )
1064                         {
1065                                 if(e.enemy != self)
1066                                         continue;
1067                                 e.killindicator = spawn();
1068                                 e.killindicator.owner = e;
1069                                 e.killindicator.scale = 0.5;
1070                                 setattachment(e.killindicator, e, "");
1071                                 setorigin(e.killindicator, '0 0 52');
1072                                 e.killindicator.think = KillIndicator_Think;
1073                                 e.killindicator.nextthink = time + (e.lip) * 0.05;
1074                                 e.killindicator.cnt = ceil(killtime);
1075                         }
1076                         self.lip = 0;
1077                 }
1078         }
1079         if(self.killindicator)
1080         {
1081                 if(targetteam)
1082                         self.killindicator.colormod = TeamColor(targetteam);
1083                 else
1084                         self.killindicator.colormod = '0 0 0';
1085         }
1086 }
1087
1088 void ClientKill (void)
1089 {
1090         ClientKill_TeamChange(0);
1091 }
1092
1093 void DoTeamChange(float destteam)
1094 {
1095         float t, c0;
1096         if(!cvar("teamplay"))
1097         {
1098                 if(destteam >= 0)
1099                         SetPlayerColors(self, destteam);
1100                 return;
1101         }
1102         if(self.classname == "player")
1103         if(destteam == -1)
1104         {
1105                 CheckAllowedTeams(self);
1106                 t = FindSmallestTeam(self, TRUE);
1107                 switch(self.team)
1108                 {
1109                         case COLOR_TEAM1: c0 = c1; break;
1110                         case COLOR_TEAM2: c0 = c2; break;
1111                         case COLOR_TEAM3: c0 = c3; break;
1112                         case COLOR_TEAM4: c0 = c4; break;
1113                         default:          c0 = 999;
1114                 }
1115                 switch(t)
1116                 {
1117                         case 1:
1118                                 if(c0 > c1)
1119                                         destteam = COLOR_TEAM1;
1120                                 break;
1121                         case 2:
1122                                 if(c0 > c2)
1123                                         destteam = COLOR_TEAM2;
1124                                 break;
1125                         case 3:
1126                                 if(c0 > c3)
1127                                         destteam = COLOR_TEAM3;
1128                                 break;
1129                         case 4:
1130                                 if(c0 > c4)
1131                                         destteam = COLOR_TEAM4;
1132                                 break;
1133                 }
1134                 if(destteam == -1)
1135                         return;
1136         }
1137         if(destteam == self.team && !self.killindicator)
1138                 return;
1139         ClientKill_TeamChange(destteam);
1140 }
1141
1142 void FixClientCvars(entity e)
1143 {
1144         // send prediction settings to the client
1145         stuffcmd(e, "\nin_bindmap 0 0\n");
1146         /*
1147          * we no longer need to stuff this. Remove this comment block if you feel 
1148          * 2.3 and higher (or was it 2.2.3?) don't need these any more
1149         stuffcmd(e, strcat("cl_gravity ", ftos(cvar("sv_gravity")), "\n"));
1150         stuffcmd(e, strcat("cl_movement_accelerate ", ftos(cvar("sv_accelerate")), "\n"));
1151         stuffcmd(e, strcat("cl_movement_friction ", ftos(cvar("sv_friction")), "\n"));
1152         stuffcmd(e, strcat("cl_movement_maxspeed ", ftos(cvar("sv_maxspeed")), "\n"));
1153         stuffcmd(e, strcat("cl_movement_airaccelerate ", ftos(cvar("sv_airaccelerate")), "\n"));
1154         stuffcmd(e, strcat("cl_movement_maxairspeed ", ftos(cvar("sv_maxairspeed")), "\n"));
1155         stuffcmd(e, strcat("cl_movement_stopspeed ", ftos(cvar("sv_stopspeed")), "\n"));
1156         stuffcmd(e, strcat("cl_movement_jumpvelocity ", ftos(cvar("sv_jumpvelocity")), "\n"));
1157         stuffcmd(e, strcat("cl_movement_stepheight ", ftos(cvar("sv_stepheight")), "\n"));
1158         stuffcmd(e, strcat("set cl_movement_friction_on_land ", ftos(cvar("sv_friction_on_land")), "\n"));
1159         stuffcmd(e, strcat("set cl_movement_airaccel_qw ", ftos(cvar("sv_airaccel_qw")), "\n"));
1160         stuffcmd(e, strcat("set cl_movement_airaccel_sideways_friction ", ftos(cvar("sv_airaccel_sideways_friction")), "\n"));
1161         stuffcmd(e, "cl_movement_edgefriction 1\n");
1162          */
1163 }
1164
1165 /*
1166 =============
1167 ClientConnect
1168
1169 Called when a client connects to the server
1170 =============
1171 */
1172 //void ctf_clientconnect();
1173 string ColoredTeamName(float t);
1174 void DecodeLevelParms (void);
1175 //void dom_player_join_team(entity pl);
1176 void ClientConnect (void)
1177 {
1178         local string s;
1179
1180         if(self.flags & FL_CLIENT)
1181         {
1182                 print("Warning: ClientConnect, but already connected!\n");
1183                 return;
1184         }
1185
1186         if(Ban_MaybeEnforceBan(self))
1187                 return;
1188
1189         DecodeLevelParms();
1190
1191         self.classname = "player_joining";
1192
1193         self.flags = FL_CLIENT;
1194         self.version_nagtime = time + 10 + random() * 10;
1195
1196         if(player_count<0)
1197         {
1198                 dprint("BUG player count is lower than zero, this cannot happen!\n");
1199                 player_count = 0;
1200         }
1201
1202         PlayerScore_Attach(self);
1203         ClientData_Attach();
1204
1205         bot_clientconnect();
1206
1207         race_PreSpawnObserver();
1208
1209         //if(g_domination)
1210         //      dom_player_join_team(self);
1211
1212         //JoinBestTeam(self, FALSE, FALSE);
1213         //
1214         if((cvar("sv_spectate") == 1 && !g_lms) || cvar("g_campaign")) {
1215                 self.classname = "observer";
1216         } else {
1217                 self.classname = "player";
1218                 campaign_bots_may_start = 1;
1219         }
1220
1221         self.playerid = (playerid_last = playerid_last + 1);
1222         if(cvar("sv_eventlog"))
1223         {
1224                 if(clienttype(self) == CLIENTTYPE_REAL)
1225                         GameLogEcho(strcat(":join:", ftos(self.playerid), ":", ftos(num_for_edict(self)), ":", self.netaddress, ":", self.netname));
1226                 else
1227                         GameLogEcho(strcat(":join:", ftos(self.playerid), ":", ftos(num_for_edict(self)), ":bot:", self.netname));
1228                 s = strcat(":team:", ftos(self.playerid), ":");
1229                 s = strcat(s, ftos(self.team));
1230                 GameLogEcho(s);
1231         }
1232         self.netname_previous = strzone(self.netname);
1233
1234         //stuffcmd(self, "set tmpviewsize $viewsize \n");
1235
1236         bprint ("^4",self.netname);
1237         bprint ("^4 connected");
1238
1239         if(g_domination || g_ctf)
1240         {
1241                 bprint(" and joined the ");
1242                 bprint(ColoredTeamName(self.team));
1243         }
1244
1245         bprint("\n");
1246
1247         self.welcomemessage_time = 0;
1248
1249         stuffcmd(self, strcat(clientstuff, "\n"));
1250         stuffcmd(self, strcat("exec maps/", mapname, ".cfg\n"));
1251         stuffcmd(self, "cl_particles_reloadeffects\n");
1252
1253         FixClientCvars(self);
1254
1255         // spawnfunc_waypoint sprites
1256         WaypointSprite_InitClient(self);
1257
1258         // Wazat's grappling hook
1259         SetGrappleHookBindings();
1260
1261         // get autoswitch state from player when he toggles it
1262         stuffcmd(self, "alias autoswitch \"set cl_autoswitch $1 ; cmd autoswitch $1\"\n"); // default.cfg-ed in 2.4.1
1263
1264         // get version info from player
1265         stuffcmd(self, "cmd clientversion $gameversion\n");
1266
1267         // get other cvars from player
1268         GetCvars(0);
1269
1270         // set cvar for team scoreboard
1271         if (teams_matter)
1272         {
1273                 local float t;
1274                 t = cvar("teamplay");
1275                 // we have to stuff the correct teamplay value because if this is a listen server, this changes the teamplay mode of the server itself, which is bad
1276                 stuffcmd(self, strcat("set teamplay ", ftos(t), "\n"));
1277         }
1278         else
1279                 stuffcmd(self, "set teamplay 0\n");
1280
1281         // notify about available teams
1282         if(teamplay)
1283         {
1284                 CheckAllowedTeams(self);
1285                 t = 0; if(c1 >= 0) t |= 1; if(c2 >= 0) t |= 2; if(c3 >= 0) t |= 4; if(c4 >= 0) t |= 8;
1286                 stuffcmd(self, strcat("set _teams_available ", ftos(t), "\n"));
1287         }
1288         else
1289                 stuffcmd(self, "set _teams_available 0\n");
1290
1291         stuffcmd(self, strcat("set gametype ", ftos(game), "\n"));
1292
1293         if(g_arena)
1294         {
1295                 self.classname = "observer";
1296                 Spawnqueue_Insert(self);
1297         }
1298         /*else if(g_ctf)
1299         {
1300                 ctf_clientconnect();
1301         }*/
1302
1303         if(teams_matter)
1304                 attach_entcs();
1305
1306         bot_relinkplayerlist();
1307
1308         self.spectatortime = time;
1309         if(blockSpectators)
1310         {
1311                 sprint(self, strcat("^7You have to become a player within the next ", ftos(cvar("g_maxplayers_spectator_blocktime")), " seconds, otherwise you will be kicked, because spectators aren't allowed at this time!\n"));
1312         }
1313
1314         self.jointime = time;
1315         self.allowedTimeouts = cvar("sv_timeout_number");
1316
1317         if(clienttype(self) == CLIENTTYPE_REAL)
1318         {
1319                 sprint(self, strcat("nexuiz-csqc protocol ", ftos(CSQC_REVISION), "\n"));
1320
1321                 if(cvar("g_bugrigs"))
1322                         stuffcmd(self, "cl_cmd settemp chase_active 1\n");
1323         }
1324
1325         if(g_lms)
1326         {
1327                 if(PlayerScore_Add(self, SP_LMS_LIVES, LMS_NewPlayerLives()) <= 0)
1328                 {
1329                         PlayerScore_Add(self, SP_LMS_RANK, 666);
1330                         self.frags = FRAGS_SPECTATOR;
1331                 }
1332         }
1333
1334         if(!sv_foginterval && world.fog != "")
1335                 stuffcmd(self, strcat("\nfog ", world.fog, "\nr_fog_exp2 0\nr_drawfog 1\n"));
1336 }
1337
1338 /*
1339 =============
1340 ClientDisconnect
1341
1342 Called when a client disconnects from the server
1343 =============
1344 */
1345 .entity chatbubbleentity;
1346 .entity teambubbleentity;
1347 void ReadyCount();
1348 void ClientDisconnect (void)
1349 {
1350         if not(self.flags & FL_CLIENT)
1351         {
1352                 print("Warning: ClientDisconnect without ClientConnect\n");
1353                 return;
1354         }
1355
1356         bot_clientdisconnect();
1357
1358         if(teams_matter)
1359                 detach_entcs();
1360         
1361         if(cvar("sv_eventlog"))
1362                 GameLogEcho(strcat(":part:", ftos(self.playerid)));
1363         bprint ("^4",self.netname);
1364         bprint ("^4 disconnected\n");
1365
1366         DropAllRunes(self);
1367         kh_Key_DropAll(self, TRUE);
1368
1369         Portal_ClearAll(self);
1370
1371         if(self.flagcarried)
1372                 DropFlag(self.flagcarried, world, world);
1373
1374         // Here, everything has been done that requires this player to be a client.
1375
1376         self.flags &~= FL_CLIENT;
1377
1378         if (self.chatbubbleentity)
1379                 remove (self.chatbubbleentity);
1380
1381         if (self.teambubbleentity)
1382                 remove (self.teambubbleentity);
1383
1384         if (self.killindicator)
1385                 remove (self.killindicator);
1386
1387         WaypointSprite_PlayerGone();
1388
1389         bot_relinkplayerlist();
1390
1391         // remove laserdot
1392         if(self.weaponentity)
1393                 if(self.weaponentity.lasertarget)
1394                         remove(self.weaponentity.lasertarget);
1395
1396         if(g_arena)
1397         {
1398                 Spawnqueue_Unmark(self);
1399                 Spawnqueue_Remove(self);
1400         }
1401
1402         ClientData_Detach();
1403         PlayerScore_Detach(self);
1404
1405         if(self.netname_previous)
1406                 strunzone(self.netname_previous);
1407
1408         ClearPlayerSounds();
1409
1410         self.playerid = 0;
1411         ReadyCount();
1412
1413         // free cvars
1414         GetCvars(-1);
1415 }
1416
1417 .float BUTTON_CHAT;
1418 void ChatBubbleThink()
1419 {
1420         self.nextthink = time;
1421         if (!self.owner.modelindex || self.owner.chatbubbleentity != self)
1422         {
1423                 if(self.owner) // but why can that ever be world?
1424                         self.owner.chatbubbleentity = world;
1425                 remove(self);
1426                 return;
1427         }
1428         if (self.owner.BUTTON_CHAT && !self.owner.deadflag)
1429                 self.model = self.mdl;
1430         else
1431                 self.model = "";
1432 };
1433
1434 void UpdateChatBubble()
1435 {
1436         if (!self.modelindex)
1437                 return;
1438         // spawn a chatbubble entity if needed
1439         if (!self.chatbubbleentity)
1440         {
1441                 self.chatbubbleentity = spawn();
1442                 self.chatbubbleentity.owner = self;
1443                 self.chatbubbleentity.exteriormodeltoclient = self;
1444                 self.chatbubbleentity.think = ChatBubbleThink;
1445                 self.chatbubbleentity.nextthink = time;
1446                 setmodel(self.chatbubbleentity, "models/misc/chatbubble.spr"); // precision set below
1447                 //setorigin(self.chatbubbleentity, self.origin + '0 0 15' + self.maxs_z * '0 0 1');
1448                 setorigin(self.chatbubbleentity, '0 0 15' + self.maxs_z * '0 0 1');
1449                 setattachment(self.chatbubbleentity, self, "");  // sticks to moving player better, also conserves bandwidth
1450                 self.chatbubbleentity.mdl = self.chatbubbleentity.model;
1451                 self.chatbubbleentity.model = "";
1452                 self.chatbubbleentity.effects = EF_LOWPRECISION;
1453         }
1454 }
1455
1456
1457 void TeamBubbleThink()
1458 {
1459         self.nextthink = time;
1460         if (!self.owner.modelindex || self.owner.teambubbleentity != self)
1461         {
1462                 if(self.owner) // but why can that ever be world?
1463                         self.owner.teambubbleentity = world;
1464                 remove(self);
1465                 return;
1466         }
1467 //      setorigin(self, self.owner.origin + '0 0 15' + self.owner.maxs_z * '0 0 1');  // bandwidth hog. setattachment does this now
1468         if (self.owner.BUTTON_CHAT || self.owner.deadflag || self.owner.killindicator)
1469                 self.model = "";
1470         else
1471                 self.model = self.mdl;
1472
1473 };
1474
1475 float TeamBubble_customizeentityforclient()
1476 {
1477         return (self.owner != other && self.owner.team == other.team && other.killcount > -666);
1478 }
1479
1480 void UpdateTeamBubble()
1481 {
1482         if (!self.modelindex || !cvar("teamplay"))
1483                 return;
1484         // spawn a teambubble entity if needed
1485         if (!self.teambubbleentity && cvar("teamplay"))
1486         {
1487                 self.teambubbleentity = spawn();
1488                 self.teambubbleentity.owner = self;
1489                 self.teambubbleentity.exteriormodeltoclient = self;
1490                 self.teambubbleentity.think = TeamBubbleThink;
1491                 self.teambubbleentity.nextthink = time;
1492                 setmodel(self.teambubbleentity, "models/misc/teambubble.spr"); // precision set below
1493 //              setorigin(self.teambubbleentity, self.origin + '0 0 15' + self.maxs_z * '0 0 1');
1494                 setorigin(self.teambubbleentity, '0 0 15' + self.maxs_z * '0 0 1');
1495                 setattachment(self.teambubbleentity, self, "");  // sticks to moving player better, also conserves bandwidth
1496                 self.teambubbleentity.mdl = self.teambubbleentity.model;
1497                 self.teambubbleentity.model = self.teambubbleentity.mdl;
1498                 self.teambubbleentity.customizeentityforclient = TeamBubble_customizeentityforclient;
1499                 self.teambubbleentity.effects = EF_LOWPRECISION;
1500         }
1501 }
1502
1503 // LordHavoc: this hack will be removed when proper _pants/_shirt layers are
1504 // added to the model skins
1505 /*void UpdateColorModHack()
1506 {
1507         local float c;
1508         c = self.clientcolors & 15;
1509         // LordHavoc: only bothering to support white, green, red, yellow, blue
1510              if (teamplay == 0) self.colormod = '0 0 0';
1511         else if (c ==  0) self.colormod = '1.00 1.00 1.00';
1512         else if (c ==  3) self.colormod = '0.10 1.73 0.10';
1513         else if (c ==  4) self.colormod = '1.73 0.10 0.10';
1514         else if (c == 12) self.colormod = '1.22 1.22 0.10';
1515         else if (c == 13) self.colormod = '0.10 0.10 1.73';
1516         else self.colormod = '1 1 1';
1517 };*/
1518
1519 void respawn(void)
1520 {
1521         CopyBody(1);
1522         self.effects |= EF_NODRAW; // prevent another CopyBody
1523         PutClientInServer();
1524 }
1525
1526 /**
1527  * When sv_timeout is used this function returs strings like
1528  * "Timeout begins in 2 seconds!\n" or "Timeout ends in 23 seconds!\n".
1529  * Called by centerprint functions
1530  * @param addOneSecond boolean, set to 1 if the welcome-message centerprint asks for the text
1531  */
1532 string getTimeoutText(float addOneSecond) {
1533         if (!cvar("sv_timeout") || !timeoutStatus)
1534                 return "";
1535
1536         local string retStr;
1537         if (timeoutStatus == 1) {
1538                 if (addOneSecond == 1) {
1539                         retStr = strcat("Timeout begins in ", ftos(remainingLeadTime + 1), " seconds!\n");
1540                 }
1541                 else {
1542                         retStr = strcat("Timeout begins in ", ftos(remainingLeadTime), " seconds!\n");
1543                 }
1544                 return retStr;
1545         }
1546         else if (timeoutStatus == 2) {
1547                 if (addOneSecond) {
1548                         retStr = strcat("Timeout ends in ", ftos(remainingTimeoutTime + 1), " seconds!\n");
1549                         //don't show messages like "Timeout ends in 0 seconds"...
1550                         if ((remainingTimeoutTime + 1) > 0)
1551                                 return retStr;
1552                         else
1553                                 return "";
1554                 }
1555                 else {
1556                         retStr = strcat("Timeout ends in ", ftos(remainingTimeoutTime), " seconds!\n");
1557                         //don't show messages like "Timeout ends in 0 seconds"...
1558                         if (remainingTimeoutTime > 0)
1559                                 return retStr;
1560                         else
1561                                 return "";
1562                 }
1563         }
1564         else return "";
1565 }
1566
1567 void player_powerups (void)
1568 {
1569         if (g_minstagib)
1570         {
1571                 if (self.items & IT_STRENGTH)
1572                 {
1573                         if (time > self.strength_finished)
1574                         {
1575                                 self.alpha = default_player_alpha;
1576                                 self.exteriorweaponentity.alpha = default_weapon_alpha;
1577                                 self.items &~= IT_STRENGTH;
1578                                 sprint(self, "^3Invisibility has worn off\n");
1579                         }
1580                 }
1581                 else
1582                 {
1583                         if (time < self.strength_finished)
1584                         {
1585                                 self.alpha = g_minstagib_invis_alpha;
1586                                 self.exteriorweaponentity.alpha = g_minstagib_invis_alpha;
1587                                 self.items |= IT_STRENGTH;
1588                                 sprint(self, "^3You are invisible\n");
1589                         }
1590                 }
1591
1592                 if (self.items & IT_INVINCIBLE)
1593                 {
1594                         if (time > self.invincible_finished)
1595                         {
1596                                 self.items = self.items - (self.items & IT_INVINCIBLE);
1597                                 sprint(self, "^3Speed has worn off\n");
1598                         }
1599                 }
1600                 else
1601                 {
1602                         if (time < self.invincible_finished)
1603                         {
1604                                 self.items = self.items | IT_INVINCIBLE;
1605                                 sprint(self, "^3You are on speed\n");
1606                         }
1607                 }
1608                 return;
1609         }
1610
1611         self.effects = self.effects - (self.effects & (EF_RED | EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT));
1612         if (self.items & IT_STRENGTH)
1613         {
1614                 self.effects = self.effects | (EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT);
1615                 if (time > self.strength_finished)
1616                 {
1617                         self.items = self.items - (self.items & IT_STRENGTH);
1618                         sprint(self, "^3Strength has worn off\n");
1619                 }
1620         }
1621         else
1622         {
1623                 if (time < self.strength_finished)
1624                 {
1625                         self.items = self.items | IT_STRENGTH;
1626                         sprint(self, "^3Strength infuses your weapons with devastating power\n");
1627                 }
1628         }
1629         if (self.items & IT_INVINCIBLE)
1630         {
1631                 self.effects = self.effects | (EF_RED | EF_ADDITIVE | EF_FULLBRIGHT);
1632                 if (time > self.invincible_finished)
1633                 {
1634                         self.items = self.items - (self.items & IT_INVINCIBLE);
1635                         sprint(self, "^3Shield has worn off\n");
1636                 }
1637         }
1638         else
1639         {
1640                 if (time < self.invincible_finished)
1641                 {
1642                         self.items = self.items | IT_INVINCIBLE;
1643                         sprint(self, "^3Shield surrounds you\n");
1644                 }
1645         }
1646
1647         if (cvar("g_fullbrightplayers"))
1648                 self.effects = self.effects | EF_FULLBRIGHT;
1649
1650         // midair gamemode: damage only while in the air
1651         // if in midair mode, being on ground grants temporary invulnerability
1652         // (this is so that multishot weapon don't clear the ground flag on the
1653         // first damage in the frame, leaving the player vulnerable to the
1654         // remaining hits in the same frame)
1655         if (self.flags & FL_ONGROUND)
1656         if (g_midair)
1657                 self.spawnshieldtime = max(self.spawnshieldtime, time + cvar("g_midair_shieldtime"));
1658
1659         if (time >= game_starttime)
1660         if (time < self.spawnshieldtime)
1661                 self.effects = self.effects | (EF_ADDITIVE | EF_FULLBRIGHT);
1662 }
1663
1664 float CalcRegen(float current, float stable, float regenfactor)
1665 {
1666         if(current > stable)
1667                 return current;
1668         else if(current > stable - 0.25) // when close enough, "snap"
1669                 return stable;
1670         else
1671                 return min(stable, current + (stable - current) * regenfactor * frametime);
1672 }
1673
1674 void player_regen (void)
1675 {
1676         float maxh, maxa, limith, limita, max_mod, regen_mod, rot_mod, limit_mod;
1677         maxh = cvar("g_balance_health_stable");
1678         maxa = cvar("g_balance_armor_stable");
1679         limith = cvar("g_balance_health_limit");
1680         limita = cvar("g_balance_armor_limit");
1681
1682         if (g_minstagib || (g_lms && !cvar("g_lms_regenerate")))
1683                 return;
1684
1685         max_mod = regen_mod = rot_mod = limit_mod = 1;
1686
1687         if (self.runes & RUNE_REGEN)
1688         {
1689                 if (self.runes & CURSE_VENOM) // do we have both rune/curse?
1690                 {
1691                         regen_mod = cvar("g_balance_rune_regen_combo_regenrate");
1692                         max_mod = cvar("g_balance_rune_regen_combo_hpmod");
1693                         limit_mod = cvar("g_balance_rune_regen_combo_limitmod");
1694                 }
1695                 else
1696                 {
1697                         regen_mod = cvar("g_balance_rune_regen_regenrate");
1698                         max_mod = cvar("g_balance_rune_regen_hpmod");
1699                         limit_mod = cvar("g_balance_rune_regen_limitmod");
1700                 }
1701         }
1702         else if (self.runes & CURSE_VENOM)
1703         {
1704                 max_mod = cvar("g_balance_curse_venom_hpmod");
1705                 if (self.runes & RUNE_REGEN) // do we have both rune/curse?
1706                         rot_mod = cvar("g_balance_rune_regen_combo_rotrate");
1707                 else
1708                         rot_mod = cvar("g_balance_curse_venom_rotrate");
1709                 limit_mod = cvar("g_balance_curse_venom_limitmod");
1710                 //if (!self.runes & RUNE_REGEN)
1711                 //      rot_mod = cvar("g_balance_curse_venom_rotrate");
1712         }
1713         maxh = maxh * max_mod;
1714         //maxa = maxa * max_mod;
1715         limith = limith * limit_mod;
1716         limita = limita * limit_mod;
1717
1718         if (self.armorvalue > maxa)
1719         {
1720                 if (time > self.pauserotarmor_finished)
1721                 {
1722                         self.armorvalue = max(maxa, self.armorvalue + (maxa - self.armorvalue) * cvar("g_balance_armor_rot") * frametime);
1723                         self.armorvalue = max(maxa, self.armorvalue - cvar("g_balance_armor_rotlinear") * frametime);
1724                 }
1725         }
1726         else if (self.armorvalue < maxa)
1727         {
1728                 if (time > self.pauseregen_finished)
1729                 {
1730                         self.armorvalue = CalcRegen(self.armorvalue, maxa, cvar("g_balance_armor_regen"));
1731                         self.armorvalue = min(maxa, self.armorvalue + cvar("g_balance_armor_regenlinear") * frametime);
1732                 }
1733         }
1734         if (self.health > maxh)
1735         {
1736                 if (time > self.pauserothealth_finished)
1737                 {
1738                         self.health = max(maxh, self.health + (maxh - self.health) * rot_mod*cvar("g_balance_health_rot") * frametime);
1739                         self.health = max(maxh, self.health - rot_mod*cvar("g_balance_health_rotlinear") * frametime);
1740                 }
1741         }
1742         else if (self.health < maxh)
1743         {
1744                 if (time > self.pauseregen_finished)
1745                 {
1746                         self.health = CalcRegen(self.health, maxh, regen_mod * cvar("g_balance_health_regen"));
1747                         self.health = min(maxh, self.health + regen_mod*cvar("g_balance_health_regenlinear") * frametime);
1748                 }
1749         }
1750
1751         if (self.health > limith)
1752                 self.health = limith;
1753         if (self.armorvalue > limita)
1754                 self.armorvalue = limita;
1755
1756         // if player rotted to death...  die!
1757         if(self.health < 1)
1758                 self.event_damage(self, self, 1, DEATH_ROT, self.origin, '0 0 0');
1759 }
1760
1761 float zoomstate_set;
1762 void SetZoomState(float z)
1763 {
1764         if(z != self.zoomstate)
1765                 ClientData_Touch(self);
1766         zoomstate_set = 1;
1767 }
1768
1769 void GetPressedKeys(void) {
1770         if (self.movement_x > 0) // get if movement keys are pressed
1771         {       // forward key pressed
1772                 self.pressedkeys |= KEY_FORWARD;
1773                 self.pressedkeys &~= KEY_BACKWARD;
1774         }
1775         else if (self.movement_x < 0)
1776         {       // backward key pressed
1777                 self.pressedkeys |= KEY_BACKWARD;
1778                 self.pressedkeys &~= KEY_FORWARD;
1779         }
1780         else
1781         {       // no x input
1782                 self.pressedkeys &~= KEY_FORWARD;
1783                 self.pressedkeys &~= KEY_BACKWARD;
1784         }
1785         
1786         if (self.movement_y > 0)
1787         {       // right key pressed
1788                 self.pressedkeys |= KEY_RIGHT;
1789                 self.pressedkeys &~= KEY_LEFT;
1790         }
1791         else if (self.movement_y < 0)
1792         {       // left key pressed
1793                 self.pressedkeys |= KEY_LEFT;
1794                 self.pressedkeys &~= KEY_RIGHT;
1795         }
1796         else
1797         {       // no y input
1798                 self.pressedkeys &~= KEY_RIGHT;
1799                 self.pressedkeys &~= KEY_LEFT;
1800         }
1801         
1802         if (self.BUTTON_JUMP) // get if jump and crouch keys are pressed
1803                 self.pressedkeys |= KEY_JUMP;
1804         else
1805                 self.pressedkeys &~= KEY_JUMP;
1806         if (self.BUTTON_CROUCH)
1807                 self.pressedkeys |= KEY_CROUCH;
1808         else
1809                 self.pressedkeys &~= KEY_CROUCH;
1810 }
1811
1812 /*
1813 ======================
1814 spectate mode routines
1815 ======================
1816 */
1817 void SpectateCopy(entity spectatee) {
1818         self.armortype = spectatee.armortype;
1819         self.armorvalue = spectatee.armorvalue;
1820         self.currentammo = spectatee.currentammo;
1821         self.effects = spectatee.effects & EFMASK_CHEAP; // eat performance
1822         self.health = spectatee.health;
1823         self.impulse = 0;
1824         self.items = spectatee.items;
1825         self.strength_finished = spectatee.strength_finished;
1826         self.invincible_finished = spectatee.invincible_finished;
1827         self.pressedkeys = spectatee.pressedkeys;
1828         self.weapons = spectatee.weapons;
1829         self.switchweapon = spectatee.switchweapon;
1830         self.weapon = spectatee.weapon;
1831         self.punchangle = spectatee.punchangle;
1832         self.view_ofs = spectatee.view_ofs;
1833         self.v_angle = spectatee.v_angle;
1834         self.velocity = spectatee.velocity;
1835         self.dmg_take = spectatee.dmg_take;
1836         self.dmg_save = spectatee.dmg_save;
1837         self.dmg_inflictor = spectatee.dmg_inflictor;
1838         self.angles = spectatee.v_angle;
1839         self.fixangle = TRUE;
1840         setorigin(self, spectatee.origin);
1841         setsize(self, spectatee.mins, spectatee.maxs);
1842         SetZoomState(spectatee.zoomstate);
1843 }
1844
1845 float SpectateUpdate() {
1846         if(!self.enemy)
1847                 return 0;
1848
1849         if (self == self.enemy)
1850                 return 0;
1851         
1852         if(self.enemy.flags & FL_NOTARGET)
1853                 return 0;
1854
1855         SpectateCopy(self.enemy);
1856
1857         return 1;
1858 }
1859
1860 float SpectateNext() {
1861         other = find(self.enemy, classname, "player");
1862         if (!other) {
1863                 other = find(other, classname, "player");
1864         }
1865         if (other) {
1866                 self.enemy = other;
1867         }
1868         if(self.enemy.classname == "player") {
1869                 msg_entity = self;
1870                 WriteByte(MSG_ONE, SVC_SETVIEW);
1871                 WriteEntity(MSG_ONE, self.enemy);
1872                 self.wantswelcomemessage = 1;
1873                 //stuffcmd(self, "set viewsize $tmpviewsize \n");
1874                 if(!SpectateUpdate())
1875                         PutObserverInServer();
1876                 return 1;
1877         } else {
1878                 return 0;
1879         }
1880 }
1881
1882 /*
1883 =============
1884 ShowRespawnCountdown()
1885
1886 Update a respawn countdown display.
1887 =============
1888 */
1889 void ShowRespawnCountdown()
1890 {
1891         float number;
1892         if(self.deadflag == DEAD_NO) // just respawned?
1893                 return;
1894         else
1895         {
1896                 number = ceil(self.death_time - time);
1897                 if(number <= 0)
1898                         return;
1899                 if(number <= self.respawn_countdown)
1900                 {
1901                         self.respawn_countdown = number - 1;
1902                         if(ceil(self.death_time - (time + 0.5)) == number) // only say it if it is the same number even in 0.5s; to prevent overlapping sounds
1903                                 announce(self, strcat("announcer/robotic/", ftos(number), ".wav"));
1904                 }
1905         }
1906 }
1907
1908 void LeaveSpectatorMode()
1909 {
1910         if(isJoinAllowed()) {
1911                 if(!cvar("teamplay") || cvar("g_campaign") || cvar("g_balance_teams")) {
1912                         self.classname = "player";
1913                         if(cvar("g_campaign") || cvar("g_balance_teams") || cvar("g_balance_teams_force"))
1914                                 JoinBestTeam(self, FALSE, TRUE);
1915                         if(cvar("g_campaign"))
1916                                 campaign_bots_may_start = 1;
1917                         PutClientInServer();
1918                         if(!(self.flags & FL_NOTARGET))
1919                                 bprint ("^4", self.netname, "^4 is playing now\n");
1920                         centerprint(self,"");
1921                         return;
1922                 } else {
1923                         stuffcmd(self,"menu_showteamselect\n");
1924                         return;
1925                 }
1926         }
1927         else {
1928                 //player may not join because of g_maxplayers is set
1929                 centerprint_atprio(self, CENTERPRIO_MAPVOTE, PREVENT_JOIN_TEXT);
1930         }
1931 }
1932
1933 /**
1934  * Determines whether the player is allowed to join. This depends on cvar
1935  * g_maxplayers, if it isn't used this function always return TRUE, otherwise
1936  * it checks whether the number of currently playing players exceeds g_maxplayers.
1937  * @return bool TRUE if the player is allowed to join, false otherwise
1938  */
1939 float isJoinAllowed() {
1940         if (!cvar("g_maxplayers"))
1941                 return TRUE;
1942
1943         local entity e;
1944         local float currentlyPlaying;
1945         FOR_EACH_REALPLAYER(e) {
1946                 if(e.classname == "player")
1947                         currentlyPlaying += 1;
1948         }
1949         if(currentlyPlaying < cvar("g_maxplayers"))
1950                 return TRUE;
1951
1952         return FALSE;
1953 }
1954
1955 /**
1956  * Checks whether the client is an observer or spectator, if so, he will get kicked after
1957  * g_maxplayers_spectator_blocktime seconds
1958  */
1959 void checkSpectatorBlock() {
1960         if(self.classname == "spectator" || self.classname == "observer") {
1961                 if( time > (self.spectatortime + cvar("g_maxplayers_spectator_blocktime")) ) {
1962                         sprint(self, "^7You were kicked from the server because you are spectator and spectators aren't allowed at the moment.\n");
1963                         dropclient(self);
1964                 }
1965         }
1966 }
1967
1968 float vercmp_recursive(string v1, string v2)
1969 {
1970         float dot1, dot2;
1971         string s1, s2;
1972         float r;
1973
1974         dot1 = strstrofs(v1, ".", 0);
1975         dot2 = strstrofs(v2, ".", 0);
1976         if(dot1 == -1)
1977                 s1 = v1;
1978         else
1979                 s1 = substring(v1, 0, dot1);
1980         if(dot2 == -1)
1981                 s2 = v2;
1982         else
1983                 s2 = substring(v2, 0, dot2);
1984
1985         r = stof(s1) - stof(s2);
1986         if(r != 0)
1987                 return r;
1988
1989         r = strcasecmp(s1, s2);
1990         if(r != 0)
1991                 return r;
1992
1993         if(dot1 == -1)
1994                 if(dot2 == -1)
1995                         return 0;
1996                 else
1997                         return -1;
1998         else
1999                 if(dot2 == -1)
2000                         return 1;
2001                 else
2002                         return vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
2003 }
2004
2005 float vercmp(string v1, string v2)
2006 {
2007         if(strcasecmp(v1, v2) == 0) // early out check
2008                 return 0;
2009         return vercmp_recursive(v1, v2);
2010 }
2011
2012 void ObserverThink()
2013 {
2014         if (self.flags & FL_JUMPRELEASED) {
2015                 if (self.BUTTON_JUMP && !self.version_mismatch) {
2016                         self.welcomemessage_time = 0;
2017                         self.flags &~= FL_JUMPRELEASED;
2018                         LeaveSpectatorMode();
2019                         return;
2020                 } else if(self.BUTTON_ATCK && !self.version_mismatch) {
2021                         self.welcomemessage_time = 0;
2022                         self.flags &~= FL_JUMPRELEASED;
2023                         if(SpectateNext() == 1) {
2024                                 self.classname = "spectator";
2025                         }
2026                 }
2027         } else {
2028                 if (!(self.BUTTON_ATCK || self.BUTTON_JUMP)) {
2029                         self.flags |= FL_JUMPRELEASED;
2030                 }
2031         }
2032         if(self.BUTTON_ZOOM)
2033                 self.wantswelcomemessage = 0;
2034         if(self.wantswelcomemessage)
2035                 PrintWelcomeMessage(self);
2036 }
2037
2038 void SpectatorThink()
2039 {
2040         if (self.flags & FL_JUMPRELEASED) {
2041                 if (self.BUTTON_JUMP && !self.version_mismatch) {
2042                         self.welcomemessage_time = 0;
2043                         self.flags &~= FL_JUMPRELEASED;
2044                         LeaveSpectatorMode();
2045                         return;
2046                 } else if(self.BUTTON_ATCK) {
2047                         self.welcomemessage_time = 0;
2048                         self.flags &~= FL_JUMPRELEASED;
2049                         if(SpectateNext() == 1) {
2050                                 self.classname = "spectator";
2051                         } else {
2052                                 self.classname = "observer";
2053                                 PutClientInServer();
2054                         }
2055                 } else if (self.BUTTON_ATCK2) {
2056                         self.welcomemessage_time = 0;
2057                         self.flags &~= FL_JUMPRELEASED;
2058                         self.classname = "observer";
2059                         PutClientInServer();
2060                 } else {
2061                         if(!SpectateUpdate())
2062                                 PutObserverInServer();
2063                 }
2064         } else {
2065                 if (!(self.BUTTON_ATCK || self.BUTTON_ATCK2)) {
2066                         self.flags |= FL_JUMPRELEASED;
2067                 }
2068         }
2069         if(self.BUTTON_ZOOM)
2070                 self.wantswelcomemessage = 0;
2071         if(self.wantswelcomemessage)
2072                 PrintWelcomeMessage(self);
2073         self.flags |= FL_CLIENT | FL_NOTARGET;
2074 }
2075
2076 .float touchexplode_time;
2077
2078 /*
2079 =============
2080 PlayerPreThink
2081
2082 Called every frame for each client before the physics are run
2083 =============
2084 */
2085 void() ctf_setstatus;
2086 void PlayerPreThink (void)
2087 {
2088         self.stat_sys_ticrate = cvar("sys_ticrate");
2089         self.stat_game_starttime = game_starttime;
2090
2091         if(blockSpectators && frametime)
2092                 // WORKAROUND: only use dropclient in server frames (frametime set). Never use it in cl_movement frames (frametime zero).
2093                 checkSpectatorBlock();
2094         
2095         zoomstate_set = 0;
2096
2097         if(self.netname_previous != self.netname)
2098         {
2099                 if(cvar("sv_eventlog"))
2100                         GameLogEcho(strcat(":name:", ftos(self.playerid), ":", self.netname));
2101                 if(self.netname_previous)
2102                         strunzone(self.netname_previous);
2103                 self.netname_previous = strzone(self.netname);
2104         }
2105
2106         // version nagging
2107         if(self.version_nagtime)
2108                 if(self.cvar_g_nexuizversion)
2109                         if(time > self.version_nagtime)
2110                         {
2111                                 if(strstr(self.cvar_g_nexuizversion, "svn", 0) < 0)
2112                                 {
2113                                         if(strstr(cvar_string("g_nexuizversion"), "svn", 0) >= 0)
2114                                         {
2115                                                 dprint("^1NOTE^7 to ", self.netname, "^7 - the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), " (beta)^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1\n");
2116                                                 sprint(self, strcat("\{1}^1NOTE: ^7the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), " (beta)^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1\n"));
2117                                         }
2118                                         else
2119                                         {
2120                                                 float r;
2121                                                 r = vercmp(self.cvar_g_nexuizversion, cvar_string("g_nexuizversion"));
2122                                                 if(r < 0)
2123                                                 {
2124                                                         dprint("^1NOTE^7 to ", self.netname, "^7 - ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7 is out, and you still have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1 - get the update from ^4http://www.nexuiz.com/^1!\n");
2125                                                         sprint(self, strcat("\{1}^1NOTE: ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7 is out, and you still have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1 - get the update from ^4http://www.nexuiz.com/^1!\n"));
2126                                                 }
2127                                                 else if(r > 0)
2128                                                 {
2129                                                         dprint("^1NOTE^7 to ", self.netname, "^7 - the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1\n");
2130                                                         sprint(self, strcat("\{1}^1NOTE: ^7the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "^1\n"));
2131                                                 }
2132                                         }
2133                                 }
2134                                 self.version_nagtime = 0;
2135                         }
2136
2137         // GOD MODE info
2138         if(!(self.flags & FL_GODMODE)) if(self.max_armorvalue)
2139         {
2140                 sprint(self, strcat("godmode saved you ", ftos(self.max_armorvalue), " units of damage, cheater!\n"));
2141                 self.max_armorvalue = 0;
2142         }
2143
2144         if(frametime)
2145                 antilag_record(self);
2146
2147         if(self.classname == "player") {
2148 //              if(self.netname == "Wazat")
2149 //                      bprint(self.classname, "\n");
2150
2151                 CheckRules_Player();
2152
2153                 if(self.BUTTON_INFO)
2154                         PrintWelcomeMessage(self);
2155
2156                 if(g_lms || !cvar("sv_spectate"))
2157                 if((time - self.jointime) <= cvar("welcome_message_time"))
2158                         PrintWelcomeMessage(self);
2159
2160                 if (intermission_running)
2161                 {
2162                         IntermissionThink ();   // otherwise a button could be missed between
2163                         return;                                 // the think tics
2164                 }
2165
2166                 if(self.teleport_time)
2167                 if(time > self.teleport_time)
2168                 {
2169                         self.teleport_time = 0;
2170                         self.effects = self.effects - (self.effects & EF_NODRAW);
2171                 }
2172
2173                 Nixnex_GiveCurrentWeapon();
2174
2175                 if(frametime > 0) // don't do this in cl_movement frames, just in server ticks
2176                         UpdateSelectedPlayer();
2177
2178                 //don't allow the player to turn around while game is paused!
2179                 if(timeoutStatus == 2) {
2180                         self.v_angle = self.lastV_angle;
2181                         self.angles = self.lastV_angle;
2182                         self.fixangle = TRUE;
2183                 }
2184
2185                 if (self.deadflag != DEAD_NO)
2186                 {
2187                         float button_pressed, force_respawn;
2188                         player_anim();
2189                         button_pressed = (self.BUTTON_ATCK || self.BUTTON_JUMP || self.BUTTON_ATCK2 || self.BUTTON_HOOK || self.BUTTON_USE);
2190                         force_respawn = (g_lms || cvar("g_forced_respawn"));
2191                         if (self.deadflag == DEAD_DYING)
2192                         {
2193                                 if(force_respawn)
2194                                         self.deadflag = DEAD_RESPAWNING;
2195                                 else if(!button_pressed)
2196                                         self.deadflag = DEAD_DEAD;
2197                         }
2198                         else if (self.deadflag == DEAD_DEAD)
2199                         {
2200                                 if(button_pressed)
2201                                         self.deadflag = DEAD_RESPAWNABLE;
2202                         }
2203                         else if (self.deadflag == DEAD_RESPAWNABLE)
2204                         {
2205                                 if(!button_pressed)
2206                                         self.deadflag = DEAD_RESPAWNING;
2207                         }
2208                         else if (self.deadflag == DEAD_RESPAWNING)
2209                         {
2210                                 if(time > self.death_time)
2211                                 {
2212                                         self.death_time = time + 1; // only retry once a second
2213                                         respawn();
2214                                 }
2215                         }
2216                         ShowRespawnCountdown();
2217                         return;
2218                 }
2219
2220                 if(g_touchexplode)
2221                 if(time > self.touchexplode_time)
2222                 if(self.classname == "player")
2223                 if(self.deadflag == DEAD_NO)
2224                 if not(IS_INDEPENDENT_PLAYER(self))
2225                 FOR_EACH_PLAYER(other) if(self != other)
2226                 {
2227                         if(time > other.touchexplode_time)
2228                         if(other.classname == "player")
2229                         if(other.deadflag == DEAD_NO)
2230                         if not(IS_INDEPENDENT_PLAYER(other))
2231                         if(boxesoverlap(self.absmin, self.absmax, other.absmin, other.absmax))
2232                         {
2233                                 PlayerTouchExplode(self, other);
2234                                 self.touchexplode_time = other.touchexplode_time = time + 0.2;
2235                         }
2236                 }
2237
2238                 if(g_lms && !self.deadflag && cvar("g_lms_campcheck_interval"))
2239                 {
2240                         vector dist;
2241
2242                         // calculate player movement (in 2 dimensions only, so jumping on one spot doesn't count as movement)
2243                         dist = self.oldorigin - self.origin;
2244                         dist_z = 0;
2245                         self.lms_traveled_distance += fabs(vlen(dist));
2246
2247                         if((cvar("g_campaign") && !campaign_bots_may_start) || (time < game_starttime))
2248                         {
2249                                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval")*2;
2250                                 self.lms_traveled_distance = 0;
2251                         }
2252
2253                         if(time > self.lms_nextcheck)
2254                         {
2255                                 //sprint(self, "distance: ", ftos(self.lms_traveled_distance), "\n");
2256                                 if(self.lms_traveled_distance < cvar("g_lms_campcheck_distance"))
2257                                 {
2258                                         centerprint(self, cvar_string("g_lms_campcheck_message"));
2259                                         // FIXME KadaverJack: gibbing player here causes playermodel to bounce around, instead of eye.md3
2260                                         // I wasn't able to find out WHY that happens, so I put a workaround in place that shall prevent players from being gibbed :(
2261                                         Damage(self, self, self, bound(0, cvar("g_lms_campcheck_damage"), self.health + self.armorvalue * cvar("g_balance_armor_blockpercent") + 5), DEATH_CAMP, self.origin, '0 0 0');
2262                                 }
2263                                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval");
2264                                 self.lms_traveled_distance = 0;
2265                         }
2266                 }
2267
2268                 self.oldorigin = self.origin;
2269
2270                 if ((self.BUTTON_CROUCH && !self.hook.state) || self.health <= g_bloodloss)
2271                 {
2272                         if (!self.crouch)
2273                         {
2274                                 self.crouch = TRUE;
2275                                 self.view_ofs = PL_CROUCH_VIEW_OFS;
2276                                 setsize (self, PL_CROUCH_MIN, PL_CROUCH_MAX);
2277                                 player_setanim(self.anim_duck, FALSE, TRUE, TRUE);
2278                         }
2279                 }
2280                 else
2281                 {
2282                         if (self.crouch)
2283                         {
2284                                 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, FALSE, self);
2285                                 if (!trace_startsolid)
2286                                 {
2287                                         self.crouch = FALSE;
2288                                         self.view_ofs = PL_VIEW_OFS;
2289                                         setsize (self, PL_MIN, PL_MAX);
2290                                 }
2291                         }
2292                 }
2293                 
2294                 if(self.health <= g_bloodloss && self.deadflag == DEAD_NO)
2295                 {
2296                         if(self.bloodloss_timer < time)
2297                         {
2298                                 self.event_damage(self, self, 1, DEATH_ROT, self.origin, '0 0 0');
2299                                 self.bloodloss_timer = time + 0.5 + random() * 0.5;
2300                         }
2301                 }
2302
2303                 FixPlayermodel();
2304
2305                 GrapplingHookFrame();
2306
2307                 W_WeaponFrame();
2308
2309                 player_powerups();
2310                 player_regen();
2311                 player_anim();
2312
2313                 if (g_minstagib)
2314                         minstagib_ammocheck();
2315
2316                 ctf_setstatus();
2317                 kh_setstatus();
2318
2319                 self.dmg_team = max(0, self.dmg_team - cvar("g_teamdamage_resetspeed") * frametime);
2320
2321                 //self.angles_y=self.v_angle_y + 90;   // temp
2322
2323                 //if (TetrisPreFrame()) return;
2324         } else if(gameover) {
2325                 if (intermission_running)
2326                         IntermissionThink ();   // otherwise a button could be missed between
2327                 return;
2328         } else if(self.classname == "observer") {
2329                 ObserverThink();
2330         } else if(self.classname == "spectator") {
2331                 SpectatorThink();
2332         }
2333
2334         if(!zoomstate_set)
2335                 SetZoomState(self.BUTTON_ZOOM || (self.BUTTON_ATCK2 && self.weapon == WEP_NEX));
2336
2337         float oldspectatee_status;
2338         oldspectatee_status = self.spectatee_status;
2339         if(self.classname == "spectator")
2340                 self.spectatee_status = num_for_edict(self.enemy);
2341         else if(self.classname == "observer")
2342                 self.spectatee_status = num_for_edict(self);
2343         else
2344                 self.spectatee_status = 0;
2345         if(self.spectatee_status != oldspectatee_status)
2346         {
2347                 ClientData_Touch(self);
2348                 if(g_race)
2349                         race_InitSpectator();
2350         }
2351
2352         if(self.teamkill_soundtime)
2353         if(time > self.teamkill_soundtime)
2354         {
2355                 self.teamkill_soundtime = 0;
2356
2357                 entity oldpusher, oldself;
2358
2359                 oldself = self; self = self.teamkill_soundsource;
2360                 oldpusher = self.pusher; self.pusher = oldself;
2361
2362                 PlayerSound(playersound_teamshoot, CHAN_VOICE, VOICETYPE_LASTATTACKER_ONLY);
2363
2364                 self.pusher = oldpusher;
2365                 self = oldself;
2366         }
2367
2368         if(self.taunt_soundtime)
2369         if(time > self.taunt_soundtime)
2370         {
2371                 self.taunt_soundtime = 0;
2372                 PlayerSound(playersound_taunt, CHAN_VOICE, VOICETYPE_AUTOTAUNT);
2373         }
2374
2375         target_voicescript_next(self);
2376 }
2377
2378
2379 /*
2380 =============
2381 PlayerPostThink
2382
2383 Called every frame for each client after the physics are run
2384 =============
2385 */
2386 .float idlekick_lasttimeleft;
2387 .float race_penalty;
2388 .float race_penalty_nagged;
2389 .float race_penalty_nagtime;
2390 void PlayerPostThink (void)
2391 {
2392         // Savage: Check for nameless players
2393         if (strlen(self.netname) < 1) {
2394                 self.netname = "Player";
2395                 stuffcmd(self, "seta _cl_name Player\n");
2396         }
2397
2398         if(sv_maxidle && frametime)
2399         {
2400                 // WORKAROUND: only use dropclient in server frames (frametime set). Never use it in cl_movement frames (frametime zero).
2401                 float timeleft;
2402                 timeleft = ceil(sv_maxidle - (time - self.parm_idlesince));
2403                 if(timeleft <= 0)
2404                 {
2405                         bprint("^3", self.netname, "^3 was kicked for idling.\n");
2406                         announce(self, "announcer/robotic/terminated.wav");
2407                         dropclient(self);
2408                         return;
2409                 }
2410                 else if(timeleft <= 10)
2411                 {
2412                         if(timeleft != self.idlekick_lasttimeleft)
2413                         {
2414                                 centerprint_atprio(self, CENTERPRIO_IDLEKICK, strcat("^3Stop idling!\n^3Disconnecting in ", ftos(timeleft), "..."));
2415                                 announce(self, strcat("announcer/robotic/", ftos(timeleft), ".wav"));
2416                         }
2417                 }
2418                 else
2419                 {
2420                         centerprint_expire(self, CENTERPRIO_IDLEKICK);
2421                 }
2422                 self.idlekick_lasttimeleft = timeleft;
2423         }
2424
2425         if(self.classname == "player") {
2426                 CheckRules_Player();
2427                 UpdateChatBubble();
2428                 UpdateTeamBubble();
2429                 if (self.impulse)
2430                         ImpulseCommands();
2431                 if (intermission_running)
2432                         return;         // intermission or finale
2433
2434                 //PrintWelcomeMessage(self);
2435                 //if (TetrisPostFrame()) return;
2436
2437                 // restart countdown
2438                         if(time < game_starttime) {
2439                                 if (!cvar("sv_ready_restart_after_countdown"))
2440                                 {
2441                                         if(self.movement != '0 0 0' && g_race && !g_race_qualifying)
2442                                         {
2443                                                 if(time < game_starttime - 2)
2444                                                 {
2445                                                         if(!self.race_penalty_nagged)
2446                                                         {
2447                                                                 // TODO better notification for this!
2448                                                                 self.race_penalty_nagtime = 0;
2449                                                                 self.race_penalty_nagged = 1;
2450                                                         }
2451                                                 }
2452                                                 else if(!self.race_penalty)
2453                                                 {
2454                                                         self.race_penalty_nagtime = 0;
2455                                                         self.race_penalty = time + 5;
2456                                                 }
2457                                         }
2458                                         if(time > self.race_penalty_nagtime)
2459                                         {
2460                                                 if(self.race_penalty > time)
2461                                                 {
2462                                                         centerprint_atprio(self, CENTERPRIO_IDLEKICK, "^1FIVE SECONDS PENALTY.");
2463                                                 }
2464                                                 else if(self.race_penalty_nagged && time < game_starttime - 2)
2465                                                 {
2466                                                         centerprint_atprio(self, CENTERPRIO_IDLEKICK, "^1DO NOT MOVE DURING THE COUNTDOWN.");
2467                                                 }
2468                                                 self.race_penalty_nagtime = time + self.cvar_scr_centertime * 0.6;
2469                                         }
2470                                         self.movetype = MOVETYPE_NONE;          
2471                                         self.velocity = '0 0 0';
2472                                         self.avelocity = '0 0 0';
2473                                         self.movement = '0 0 0';
2474                                 }
2475                         }
2476                         else if (time < self.race_penalty)
2477                         {
2478                                 self.movetype = MOVETYPE_NONE;          
2479                                 self.velocity = '0 0 0';
2480                                 self.avelocity = '0 0 0';
2481                                 self.movement = '0 0 0';
2482                         }
2483                         else
2484                         {
2485                                 //allow the player to move again if sv_ready_restart_after_countdown is not used and countdown is over
2486                                 if (!cvar("sv_ready_restart_after_countdown"))
2487                                 {
2488                                         if(self.movetype == MOVETYPE_NONE)
2489                                         {
2490                                                 self.movetype = MOVETYPE_WALK;
2491                                         }
2492                                         self.race_penalty = 0;
2493                                         self.race_penalty_nagged = 0;
2494                                 }
2495                         }
2496                 GetPressedKeys();
2497         } else if (self.classname == "observer") {
2498                 //do nothing
2499         } else if (self.classname == "spectator") {
2500                 //do nothing
2501         }
2502
2503         /*
2504         float i;
2505         for(i = 0; i < 1000; ++i)
2506         {
2507                 vector end;
2508                 end = self.origin + '0 0 1024' + 512 * randomvec();
2509                 tracebox(self.origin, self.mins, self.maxs, end, MOVE_NORMAL, self);
2510                 if(trace_fraction < 1)
2511                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
2512                 {
2513                         print("I HIT SOLID: ", vtos(self.origin), " -> ", vtos(end), "\n");
2514                         break;
2515                 }
2516         }
2517         */
2518
2519         Arena_Warmup();
2520
2521         //pointparticles(particleeffectnum("machinegun_impact"), self.origin + self.view_ofs + '0 0 7', '0 0 0', 1);
2522 }