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