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