]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_client.qc
fix viewzoom for observers
[divverent/nexuiz.git] / data / qcsrc / server / cl_client.qc
1 void info_player_survivor (void)
2 {
3         info_player_deathmatch();
4 }
5
6 void info_player_start (void)
7 {
8         info_player_deathmatch();
9 }
10
11 void info_player_deathmatch (void)
12 {
13         self.classname = "info_player_deathmatch";
14         relocate_spawnpoint();
15 }
16
17 void() spawnpoint_use =
18 {
19         if(teams_matter)
20                 self.team = activator.team;
21 };
22
23 float spawn_allbad;
24 float spawn_allgood;
25 entity Spawn_FilterOutBadSpots(entity firstspot, entity playerlist, float mindist, float teamcheck)
26 {
27         local entity spot, player, nextspot, previousspot, newfirstspot;
28         local float pcount;
29         spot = firstspot;
30         newfirstspot = world;
31         previousspot = world;
32         spawn_allgood = TRUE;
33         spawn_allbad = TRUE;
34         while (spot)
35         {
36                 nextspot = spot.chain;
37                 // count team mismatches as bad spots
38
39                 local float spotactive;
40                 spotactive = 1;
41
42                 // filter out spots for assault
43                 if(spot.target != "") {
44                         local entity ent;
45                         ent = find(world, targetname, spot.target);
46                         while(ent) {
47                                 if(ent.classname == "target_objective")
48                                         if(ent.health < 0 || ent.health >= ASSAULT_VALUE_INACTIVE)
49                                                 spotactive = 0;
50                                 ent = find(ent, targetname, spot.target);
51                         }
52                 }
53
54                 if (spot.team == teamcheck && spotactive > 0)
55                 {
56                         pcount = 0;
57                         player = playerlist;
58                         while (player)
59                         {
60                                 if (player != self)
61                                 if (vlen(player.origin - spot.origin) < mindist)
62                                         pcount = pcount + 1;
63                                 player = player.chain;
64                         }
65                         if (!pcount)
66                         {
67                                 spawn_allbad = FALSE;
68                                 if (newfirstspot)
69                                         previousspot.chain = spot;
70                                 else
71                                         newfirstspot = spot;
72                                 previousspot = spot;
73                                 spot.chain = world;
74                         }
75                         else
76                                 spawn_allgood = FALSE;
77                 }
78                 spot = nextspot;
79         }
80         // if we couldn't find ANY good points, return the original list
81         if (!newfirstspot)
82                 newfirstspot = firstspot;
83         return newfirstspot;
84 }
85
86 entity Spawn_RandomPoint(entity firstspot)
87 {
88         local entity spot;
89         local float numspots;
90         // count number of spots
91         numspots = 0;
92         spot = firstspot;
93         while (spot)
94         {
95                 numspots = numspots + 1;
96                 spot = spot.chain;
97         }
98         // pick a random one
99         numspots = numspots * random();
100         spot = firstspot;
101         while (spot.chain && numspots >= 1)
102         {
103                 numspots = numspots - 1;
104                 spot = spot.chain;
105         }
106         return spot;
107 }
108
109 entity Spawn_FurthestPoint(entity firstspot, entity playerlist)
110 {
111         local entity best, spot, player;
112         local float bestrating, rating;
113         best = world;
114         bestrating = -1000000;
115         spot = firstspot;
116         while (spot)
117         {
118                 rating = 1000000000;
119                 player = playerlist;
120                 while (player)
121                 {
122                         if (player != self)
123                                 rating = min(rating, vlen(player.origin - spot.origin));
124                         player = player.chain;
125                 }
126                 rating = rating + random() * 16;
127                 if (bestrating < rating)
128                 {
129                         best = spot;
130                         bestrating = rating;
131                 }
132                 spot = spot.chain;
133         }
134         return best;
135 }
136
137 /*
138 =============
139 SelectSpawnPoint
140
141 Finds a point to respawn
142 =============
143 */
144 entity SelectSpawnPoint (float anypoint)
145 {
146         local float teamcheck;
147         local entity spot, firstspot, playerlist;
148
149         spot = find (world, classname, "testplayerstart");
150         if (spot)
151                 return spot;
152
153         teamcheck = 0;
154
155         if(!anypoint && (g_ctf || g_assault || g_onslaught))
156                 teamcheck = self.team;
157
158         // get the list of players
159         playerlist = findchain(classname, "player");
160         // get the entire list of spots
161         firstspot = findchain(classname, "info_player_deathmatch");
162         // filter out the bad ones
163         // (note this returns the original list if none survived)
164         firstspot = Spawn_FilterOutBadSpots(firstspot, playerlist, 100, teamcheck);
165
166         // there is 50/50 chance of choosing a random spot or the furthest spot
167         // (this means that roughly every other spawn will be furthest, so you
168         // usually won't get fragged at spawn twice in a row)
169         if (arena_roundbased)
170         {
171                 firstspot = Spawn_FilterOutBadSpots(firstspot, playerlist, 800, teamcheck);
172                 spot = Spawn_RandomPoint(firstspot);
173         }
174         else if (random() > 0.5 || spawn_allbad || spawn_allgood)
175                 spot = Spawn_RandomPoint(firstspot);
176         else
177                 spot = Spawn_FurthestPoint(firstspot, playerlist);
178
179         if (!spot)
180         {
181                 if(cvar("spawn_debug"))
182                         GotoNextMap();
183                 else
184                         return world;
185         }
186
187         return spot;
188 }
189
190 /*
191 =============
192 CheckPlayerModel
193
194 Checks if the argument string can be a valid playermodel.
195 Returns a valid one in doubt.
196 =============
197 */
198 string FallbackPlayerModel = "models/player/marine.zym";
199 string CheckPlayerModel(string plyermodel) {
200         if(strlen(plyermodel) < 4)
201                 return FallbackPlayerModel;
202         if( substring(plyermodel,0,14) != "models/player/")
203                 return FallbackPlayerModel;
204         else if(cvar("sv_servermodelsonly"))
205         {
206                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".zym")
207                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".dpm")
208                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".md3")
209                 if(substring(plyermodel,strlen(plyermodel)-4,4) != ".psk")
210                         return FallbackPlayerModel;
211                 if(!fexists(plyermodel))
212                         return FallbackPlayerModel;
213         }
214         return plyermodel;
215 }
216
217 /*
218 =============
219 Client_customizeentityforclient
220
221 LOD reduction
222 =============
223 */
224 float Client_customizeentityforclient()
225 {
226 #ifdef ALLOW_VARIABLE_LOD
227         // self: me
228         // other: the player viewing me
229         float distance;
230         float f;
231
232         if(self.flags & FL_NOTARGET) // we don't need LOD for spectators
233                 return TRUE;
234
235         if(other.cvar_cl_playerdetailreduction <= 0)
236         {
237                 if(other.cvar_cl_playerdetailreduction <= -2)
238                         self.modelindex = self.modelindex_lod2;
239                 else if(other.cvar_cl_playerdetailreduction <= -1)
240                         self.modelindex = self.modelindex_lod1;
241                 else
242                         self.modelindex = self.modelindex_lod0;
243         }
244         else
245         {
246                 distance = vlen(self.origin - other.origin);
247                 f = (distance + 100.0) * other.cvar_cl_playerdetailreduction;
248                 if(f > 10000)
249                         self.modelindex = self.modelindex_lod2;
250                 else if(f > 5000)
251                         self.modelindex = self.modelindex_lod1;
252                 else
253                         self.modelindex = self.modelindex_lod0;
254         }
255 #endif
256
257         return TRUE;
258 }
259
260 void UpdatePlayerSounds();
261 void setmodel_lod(entity e, string modelname)
262 {
263 #ifdef ALLOW_VARIABLE_LOD
264         string s;
265
266         // FIXME: this only supports 3-letter extensions
267         s = strcat(substring(modelname, 0, strlen(modelname) - 4), "_1", substring(modelname, 0, strlen(modelname) - 4));
268         if(fexists(s))
269         {
270                 precache_model(s);
271                 setmodel(e, s); // players have high precision
272                 self.modelindex_lod1 = self.modelindex;
273         }
274         else
275                 self.modelindex_lod1 = -1;
276
277         s = strcat(substring(modelname, 0, strlen(modelname) - 4), "_2", substring(modelname, 0, strlen(modelname) - 4));
278         if(fexists(s))
279         {
280                 precache_model(s);
281                 setmodel(e, s); // players have high precision
282                 self.modelindex_lod2 = self.modelindex;
283         }
284         else
285                 self.modelindex_lod2 = -1;
286
287         precache_model(modelname);
288         setmodel(e, modelname); // players have high precision
289         self.modelindex_lod0 = self.modelindex;
290
291         if(self.modelindex_lod1 < 0)
292                 self.modelindex_lod1 = self.modelindex;
293
294         if(self.modelindex_lod2 < 0)
295                 self.modelindex_lod2 = self.modelindex;
296 #else
297         precache_model(modelname);
298         setmodel(e, modelname); // players have high precision
299 #endif
300         player_setupanimsformodel();
301         UpdatePlayerSounds();
302 }
303
304 /*
305 =============
306 PutObserverInServer
307
308 putting a client as observer in the server
309 =============
310 */
311 void PutObserverInServer (void)
312 {
313         entity  spot;
314         spot = SelectSpawnPoint (TRUE);
315         if(!spot)
316                 error("No spawnpoints for observers?!?\n");
317         RemoveGrapplingHook(self); // Wazat's Grappling Hook
318
319         if(clienttype(self) == CLIENTTYPE_REAL)
320         {
321                 msg_entity = self;
322                 WriteByte(MSG_ONE, SVC_SETVIEW);
323                 WriteEntity(MSG_ONE, self);
324         }
325
326         DropAllRunes(self);
327         kh_Key_DropAll(self, TRUE);
328
329         if(self.flagcarried)
330                 DropFlag(self.flagcarried);
331
332         WaypointSprite_PlayerDead();
333
334         DistributeFragsAmongTeam(self, self.team, 1);
335
336         if(self.frags <= 0 && self.frags > -666 && g_lms && self.killcount != -666)
337                 bprint ("^4", self.netname, "^4 has no more lives left\n");
338         else if(self.killcount != -666)
339                 bprint ("^4", self.netname, "^4 is spectating now\n");
340
341         self.classname = "observer";
342         self.health = -666;
343         self.takedamage = DAMAGE_NO;
344         self.solid = SOLID_NOT;
345         self.movetype = MOVETYPE_NOCLIP;
346         self.flags = FL_CLIENT | FL_NOTARGET;
347         self.armorvalue = 666;
348         self.effects = 0;
349         self.armorvalue = cvar("g_balance_armor_start");
350         self.pauserotarmor_finished = 0;
351         self.pauserothealth_finished = 0;
352         self.pauseregen_finished = 0;
353         self.damageforcescale = 0;
354         self.death_time = 0;
355         self.dead_frame = 0;
356         self.deaths = 0;
357         self.alpha = 0;
358         self.scale = 0;
359         self.fade_time = 0;
360         self.pain_frame = 0;
361         self.pain_finished = 0;
362         self.strength_finished = 0;
363         self.invincible_finished = 0;
364         self.pushltime = 0;
365         self.think = SUB_Null;
366         self.nextthink = 0;
367         self.hook_time = 0;
368         self.runes = 0;
369         self.deadflag = DEAD_NO;
370         self.angles = spot.angles;
371         self.angles_z = 0;
372         self.fixangle = TRUE;
373         self.crouch = FALSE;
374
375         self.view_ofs = PL_VIEW_OFS;
376         setorigin (self, spot.origin);
377         setsize (self, '0 0 0', '0 0 0');
378         self.oldorigin = self.origin;
379         self.items = 0;
380         self.model = "";
381         self.modelindex = 0;
382         self.weapon = 0;
383         self.weaponmodel = "";
384         self.weaponentity = world;
385         self.killcount = -666;
386         self.velocity = '0 0 0';
387         self.avelocity = '0 0 0';
388         self.punchangle = '0 0 0';
389         self.punchvector = '0 0 0';
390         self.oldvelocity = self.velocity;
391         self.customizeentityforclient = Client_customizeentityforclient;
392         self.viewzoom = 1;
393
394         if(g_arena)
395         {
396                 if(self.frags != -2)
397                 {
398                         Spawnqueue_Insert(self);
399                 }
400                 else
401                 {
402                         Spawnqueue_Unmark(self);
403                         Spawnqueue_Remove(self);
404                 }
405         }
406         else if(!g_lms)
407                 self.frags = -666;
408 }
409
410 float RestrictSkin(float s)
411 {
412         if(!teams_matter)
413                 return s;
414         if(s == 6)
415                 return 6;
416         return mod(s, 3);
417 }
418
419 void FixPlayermodel()
420 {
421         local string defaultmodel;
422         local float defaultskin;
423         local vector m1, m2;
424
425         if(cvar("sv_defaultcharacter") == 1) {
426                 defaultmodel = "";
427                 defaultskin = 0;
428
429                 if(teams_matter)
430                 {
431                         defaultmodel = cvar_string(strcat("sv_defaultplayermodel_", Team_ColorNameLowerCase(self.team)));
432                         defaultskin = cvar(strcat("sv_defaultplayerskin_", Team_ColorNameLowerCase(self.team)));
433                 }
434
435                 if(defaultmodel == "")
436                 {
437                         defaultmodel = cvar_string("sv_defaultplayermodel");
438                         defaultskin = cvar("sv_defaultplayerskin");
439                 }
440
441                 if (defaultmodel != self.model)
442                 {
443                         m1 = self.mins;
444                         m2 = self.maxs;
445                         setmodel_lod (self, defaultmodel);
446                         setsize (self, m1, m2);
447                 }
448
449                 self.skin = defaultskin;
450         } else {
451                 if (self.playermodel != self.model)
452                 {
453                         self.playermodel = CheckPlayerModel(self.playermodel);
454                         m1 = self.mins;
455                         m2 = self.maxs;
456                         setmodel_lod (self, self.playermodel);
457                         setsize (self, m1, m2);
458                 }
459
460                 self.skin = RestrictSkin(stof(self.playerskin));
461         }
462
463         if(!teams_matter)
464                 if(strlen(cvar_string("sv_defaultplayercolors")))
465                         if(self.clientcolors != cvar("sv_defaultplayercolors"))
466                                 setcolor(self, cvar("sv_defaultplayercolors"));
467 }
468
469 /*
470 =============
471 PutClientInServer
472
473 Called when a client spawns in the server
474 =============
475 */
476 void PutClientInServer (void)
477 {
478         if(clienttype(self) == CLIENTTYPE_BOT)
479         {
480                 self.classname = "player";
481         }
482         else if(clienttype(self) == CLIENTTYPE_REAL)
483         {
484                 msg_entity = self;
485                 WriteByte(MSG_ONE, SVC_SETVIEW);
486                 WriteEntity(MSG_ONE, self);
487         }
488
489         // player is dead and becomes observer
490         if(g_lms && self.frags < 1)
491                 self.classname = "observer";
492
493         if(g_arena)
494         if(!self.spawned)
495                 self.classname = "observer";
496
497         if(self.classname == "player") {
498                 entity  spot;
499
500                 spot = SelectSpawnPoint (FALSE);
501                 if(!spot)
502                 {
503                         centerprint(self, "Sorry, no spawnpoints available!\nHope your team can fix it...");
504                         return; // spawn failed
505                 }
506
507                 RemoveGrapplingHook(self); // Wazat's Grappling Hook
508
509                 self.classname = "player";
510                 self.iscreature = TRUE;
511                 self.movetype = MOVETYPE_WALK;
512                 self.solid = SOLID_SLIDEBOX;
513                 self.flags = FL_CLIENT;
514                 self.takedamage = DAMAGE_AIM;
515                 if(g_minstagib)
516                         self.effects = EF_FULLBRIGHT;
517                 else
518                         self.effects = 0;
519                 self.air_finished = time + 12;
520                 self.dmg = 2;
521
522                 self.ammo_shells = start_ammo_shells;
523                 self.ammo_nails = start_ammo_nails;
524                 self.ammo_rockets = start_ammo_rockets;
525                 self.ammo_cells = start_ammo_cells;
526                 self.health = start_health;
527                 self.armorvalue = start_armorvalue;
528                 self.items = start_items;
529                 self.switchweapon = start_switchweapon;
530                 self.weapon = 0;
531                 self.jump_interval = time;
532
533                 self.spawnshieldtime = time + cvar("g_spawnshieldtime");
534                 self.pauserotarmor_finished = time + cvar("g_balance_pause_armor_rot_spawn");
535                 self.pauserothealth_finished = time + cvar("g_balance_pause_health_rot_spawn");
536                 self.pauseregen_finished = time + cvar("g_balance_pause_health_regen_spawn");
537                 self.damageforcescale = 2;
538                 self.death_time = 0;
539                 self.dead_frame = 0;
540                 self.alpha = 0;
541                 self.scale = 0;
542                 self.fade_time = 0;
543                 self.pain_frame = 0;
544                 self.pain_finished = 0;
545                 self.strength_finished = 0;
546                 self.invincible_finished = 0;
547                 self.pushltime = 0;
548                 //self.speed_finished = 0;
549                 //self.slowmo_finished = 0;
550                 // players have no think function
551                 self.think = SUB_Null;
552                 self.nextthink = 0;
553                 self.hook_time = 0;
554
555                 self.runes = 0;
556
557                 self.deadflag = DEAD_NO;
558
559                 self.angles = spot.angles;
560
561                 self.angles_z = 0; // never spawn tilted even if the spot says to
562                 self.fixangle = TRUE; // turn this way immediately
563                 self.velocity = '0 0 0';
564                 self.avelocity = '0 0 0';
565                 self.punchangle = '0 0 0';
566                 self.punchvector = '0 0 0';
567                 self.oldvelocity = self.velocity;
568
569                 self.viewzoom = 0.6;
570                 self.has_zoomed = 0;
571
572                 self.customizeentityforclient = Client_customizeentityforclient;
573
574                 self.model = "";
575                 FixPlayermodel();
576
577                 self.crouch = FALSE;
578                 self.view_ofs = PL_VIEW_OFS;
579                 setsize (self, PL_MIN, PL_MAX);
580                 self.spawnorigin = spot.origin;
581                 setorigin (self, spot.origin + '0 0 1' * (1 - self.mins_z - 24));
582                 // don't reset back to last position, even if new position is stuck in solid
583                 self.oldorigin = self.origin;
584
585                 if(g_arena)
586                 {
587                         Spawnqueue_Remove(self);
588                         Spawnqueue_Mark(self);
589                 }
590
591                 self.event_damage = PlayerDamage;
592
593                 self.bot_attack = TRUE;
594
595                 self.statdraintime = time + 5;
596                 self.button0 = self.button1 = self.button2 = self.button3 = 0;
597
598                 if(self.killcount == -666) {
599                         self.killcount = 0;
600                         if(!g_arena)
601                         if(!g_lms)
602                                 self.frags = 0;
603                 }
604
605                 self.cnt = WEP_LASER;
606                 self.nixnex_lastchange_id = -1;
607
608                 CL_SpawnWeaponentity();
609                 self.alpha = default_player_alpha;
610                 self.colormod = '1 1 1' * cvar("g_player_brightness");
611                 self.exteriorweaponentity.alpha = default_weapon_alpha;
612
613                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval")*2;
614                 self.lms_traveled_distance = 0;
615                 self.speedrunning = FALSE;
616
617                 if(cvar("spawn_debug"))
618                 {
619                         sprint(self, strcat("spawnpoint origin:  ", vtos(spot.origin), "\n"));
620                         remove(spot);   // usefull for checking if there are spawnpoints, that let drop through the floor
621                 }
622
623                 //stuffcmd(self, "chase_active 0");
624                 //stuffcmd(self, "set viewsize $tmpviewsize \n");
625
626                 if (cvar("g_spawnsound"))
627                         sound (self, CHAN_AUTO, "misc/spawn.wav", 1, ATTN_NORM);
628
629                 if(g_assault) {
630                         if(self.team == assault_attacker_team)
631                                 centerprint(self, "You are attacking!\n");
632                         else
633                                 centerprint(self, "You are defending!\n");
634                 }
635
636         } else if(self.classname == "observer") {
637                 PutObserverInServer ();
638         }
639 }
640
641 /*
642 =============
643 SetNewParms
644 =============
645 */
646 void SetNewParms (void)
647 {
648
649 }
650
651 /*
652 =============
653 SetChangeParms
654 =============
655 */
656 void SetChangeParms (void)
657 {
658
659 }
660
661 /*
662 =============
663 ClientKill
664
665 Called when a client types 'kill' in the console
666 =============
667 */
668 void ClientKill (void)
669 {
670         Damage(self, self, self, 100000, DEATH_KILL, self.origin, '0 0 0');
671 }
672
673 void FixClientCvars(entity e)
674 {
675         float t;
676         // send prediction settings to the client
677         stuffcmd(e, "\nin_bindmap 0 0\n");
678         stuffcmd(e, strcat("cl_gravity ", ftos(cvar("sv_gravity")), "\n"));
679         stuffcmd(e, strcat("cl_movement_accelerate ", ftos(cvar("sv_accelerate")), "\n"));
680         stuffcmd(e, strcat("cl_movement_friction ", ftos(cvar("sv_friction")), "\n"));
681         stuffcmd(e, strcat("cl_movement_maxspeed ", ftos(cvar("sv_maxspeed")), "\n"));
682         stuffcmd(e, strcat("cl_movement_airaccelerate ", ftos(cvar("sv_airaccelerate")), "\n"));
683         stuffcmd(e, strcat("cl_movement_maxairspeed ", ftos(cvar("sv_maxairspeed")), "\n"));
684         stuffcmd(e, strcat("cl_movement_stopspeed ", ftos(cvar("sv_stopspeed")), "\n"));
685         stuffcmd(e, strcat("cl_movement_jumpvelocity ", ftos(cvar("sv_jumpvelocity")), "\n"));
686         stuffcmd(e, strcat("cl_movement_stepheight ", ftos(cvar("sv_stepheight")), "\n"));
687         stuffcmd(e, strcat("set cl_movement_friction_on_land ", ftos(cvar("sv_friction_on_land")), "\n"));
688         stuffcmd(e, strcat("set cl_movement_airaccel_qw ", ftos(cvar("sv_airaccel_qw")), "\n"));
689         stuffcmd(e, strcat("set cl_movement_airaccel_sideways_friction ", ftos(cvar("sv_airaccel_sideways_friction")), "\n"));
690         stuffcmd(e, "cl_movement_edgefriction 1\n");
691
692         // notify about available teams
693         if(teamplay)
694         {
695                 CheckAllowedTeams(e);
696                 t = 0; if(c1 >= 0) t |= 1; if(c2 >= 0) t |= 2; if(c3 >= 0) t |= 4; if(c4 >= 0) t |= 8;
697                 stuffcmd(e, strcat("set _teams_available ", ftos(t), "\n"));
698         }
699         else
700                 stuffcmd(e, "set _teams_available 0\n");
701 }
702
703 /*
704 =============
705 ClientConnect
706
707 Called when a client connects to the server
708 =============
709 */
710 string ColoredTeamName(float t);
711 //void dom_player_join_team(entity pl);
712 void ClientConnect (void)
713 {
714         local string s;
715         float wep;
716
717         if(Ban_IsClientBanned(self))
718         {
719                 s = strcat("^1NOTE:^7 banned client ", self.netaddress, " just tried to enter\n");
720                 dropclient(self);
721                 bprint(s);
722                 return;
723         }
724
725         self.classname = "player_joining";
726         self.flags = self.flags | FL_CLIENT;
727         self.version_nagtime = time + 10 + random() * 10;
728
729         if(player_count<0)
730         {
731                 dprint("BUG player count is lower than zero, this cannot happen!\n");
732                 player_count = 0;
733         }
734
735         bot_clientconnect();
736
737         //if(g_domination)
738         //      dom_player_join_team(self);
739
740         //JoinBestTeam(self, FALSE);
741
742         if((cvar("sv_spectate") == 1 && !g_lms) || cvar("g_campaign")) {
743                 self.classname = "observer";
744         } else {
745                 self.classname = "player";
746                 campaign_bots_may_start = 1;
747         }
748
749         self.playerid = (playerid_last = playerid_last + 1);
750         if(cvar("sv_eventlog"))
751         {
752                 if(clienttype(self) == CLIENTTYPE_REAL)
753                         s = "player";
754                 else
755                         s = "bot";
756                 GameLogEcho(strcat(":join:", ftos(self.playerid), ":", s, ":", self.netname), TRUE);
757                 s = strcat(":team:", ftos(self.playerid), ":");
758                 s = strcat(s, ftos(self.team));
759                 GameLogEcho(s, FALSE);
760         }
761
762         //stuffcmd(self, "set tmpviewsize $viewsize \n");
763
764         bprint ("^4",self.netname);
765         bprint ("^4 connected");
766
767         if(g_domination || g_ctf)
768         {
769                 bprint(" and joined the ");
770                 bprint(ColoredTeamName(self.team));
771         }
772
773         bprint("\n");
774
775         self.welcomemessage_time = 0;
776
777         stuffcmd(self, strcat("exec maps/", mapname, ".cfg\n"));
778         // TODO: is this being used for anything else than cd tracks?
779         // Remember: SVC_CDTRACK exists. Maybe it should be used.
780
781         FixClientCvars(self);
782
783         // waypoint sprites
784         WaypointSprite_InitClient(self);
785
786         // Wazat's grappling hook
787         SetGrappleHookBindings();
788
789         // get autoswitch state from player when he toggles it
790         stuffcmd(self, "alias autoswitch \"set cl_autoswitch $1 ; cmd autoswitch $1\"\n");
791
792         // get version info from player
793         stuffcmd(self, "cmd clientversion $gameversion\n");
794
795         // send all weapon info strings
796         stuffcmd(self, "register_bestweapon clear\n"); // clear the Quake stuff
797         wep = WEP_FIRST;
798         while (wep <= WEP_LAST)
799         {
800                 weapon_action(wep, WR_REGISTER);
801                 wep = wep + 1;
802         }
803
804         // get other cvars from player
805         GetCvars(0);
806
807         // set cvar for team scoreboard
808         if (teams_matter)
809         {
810                 local float t;
811                 t = cvar("teamplay");
812                 // 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
813                 stuffcmd(self, strcat("set teamplay ", ftos(t), "\n"));
814         }
815         else
816                 stuffcmd(self, "set teamplay 0\n");
817
818         if(g_lms)
819         {
820                 self.frags = LMS_NewPlayerLives();
821                 if(!self.frags)
822                         self.frags = -666;
823         }
824         else if(g_arena)
825         {
826                 self.classname = "observer";
827                 Spawnqueue_Insert(self);
828         }
829
830         bot_relinkplayerlist();
831
832         self.jointime = time;
833 }
834
835 /*
836 =============
837 ClientDisconnect
838
839 Called when a client disconnects from the server
840 =============
841 */
842 void(entity e) DropFlag;
843 .entity chatbubbleentity;
844 .entity teambubbleentity;
845 void ClientDisconnect (void)
846 {
847         float save;
848         if(cvar("sv_eventlog"))
849                 GameLogEcho(strcat(":part:", ftos(self.playerid)), FALSE);
850         bprint ("^4",self.netname);
851         bprint ("^4 disconnected\n");
852
853         if (self.chatbubbleentity)
854         {
855                 remove (self.chatbubbleentity);
856                 self.chatbubbleentity = world;
857         }
858
859         if (self.teambubbleentity)
860         {
861                 remove (self.teambubbleentity);
862                 self.teambubbleentity = world;
863         }
864
865         WaypointSprite_PlayerGone();
866
867         DropAllRunes(self);
868         kh_Key_DropAll(self, TRUE);
869
870         if(self.flagcarried)
871                 DropFlag(self.flagcarried);
872
873         DistributeFragsAmongTeam(self, self.team, 1);
874
875         save = self.flags;
876         self.flags = self.flags - (self.flags & FL_CLIENT);
877         bot_relinkplayerlist();
878         self.flags = save;
879
880         // remove laserdot
881         if(self.weaponentity)
882                 if(self.weaponentity.lasertarget)
883                         remove(self.weaponentity.lasertarget);
884
885         if(g_arena)
886         {
887                 Spawnqueue_Unmark(self);
888                 Spawnqueue_Remove(self);
889         }
890
891         // free cvars
892         GetCvars(-1);
893 }
894
895 .float buttonchat;
896 void() ChatBubbleThink =
897 {
898         self.nextthink = time;
899         if (!self.owner.modelindex || self.owner.chatbubbleentity != self)
900         {
901                 self.owner.chatbubbleentity = world;
902                 remove(self);
903                 return;
904         }
905         setorigin(self, self.owner.origin + '0 0 15' + self.owner.maxs_z * '0 0 1');
906         if (self.owner.buttonchat && !self.owner.deadflag)
907                 self.model = self.mdl;
908         else
909                 self.model = "";
910 };
911
912 void() UpdateChatBubble =
913 {
914         if (!self.modelindex)
915                 return;
916         // spawn a chatbubble entity if needed
917         if (!self.chatbubbleentity)
918         {
919                 self.chatbubbleentity = spawn();
920                 self.chatbubbleentity.owner = self;
921                 self.chatbubbleentity.exteriormodeltoclient = self;
922                 self.chatbubbleentity.think = ChatBubbleThink;
923                 self.chatbubbleentity.nextthink = time;
924                 setmodel(self.chatbubbleentity, "models/misc/chatbubble.spr"); // precision set below
925                 setorigin(self.chatbubbleentity, self.origin + '0 0 15' + self.maxs_z * '0 0 1');
926                 self.chatbubbleentity.mdl = self.chatbubbleentity.model;
927                 self.chatbubbleentity.model = "";
928                 self.chatbubbleentity.effects = EF_LOWPRECISION;
929         }
930 }
931
932
933 void() TeamBubbleThink =
934 {
935         self.nextthink = time;
936         if (!self.owner.modelindex || self.owner.teambubbleentity != self)
937         {
938                 self.owner.teambubbleentity = world;
939                 remove(self);
940                 return;
941         }
942 //      setorigin(self, self.owner.origin + '0 0 15' + self.owner.maxs_z * '0 0 1');  // bandwidth hog. setattachment does this now
943         if (self.owner.buttonchat || self.owner.deadflag)
944                 self.model = "";
945         else
946                 self.model = self.mdl;
947
948 };
949
950 float() TeamBubble_customizeentityforclient
951 {
952         return (self.owner != other && self.owner.team == other.team && other.killcount > -666);
953 }
954
955 void() UpdateTeamBubble =
956 {
957         if (!self.modelindex || !cvar("teamplay"))
958                 return;
959         // spawn a teambubble entity if needed
960         if (!self.teambubbleentity && cvar("teamplay"))
961         {
962                 self.teambubbleentity = spawn();
963                 self.teambubbleentity.owner = self;
964                 self.teambubbleentity.exteriormodeltoclient = self;
965                 self.teambubbleentity.think = TeamBubbleThink;
966                 self.teambubbleentity.nextthink = time;
967                 setmodel(self.teambubbleentity, "models/misc/teambubble.spr"); // precision set below
968 //              setorigin(self.teambubbleentity, self.origin + '0 0 15' + self.maxs_z * '0 0 1');
969                 setorigin(self.teambubbleentity, self.teambubbleentity.origin + '0 0 15' + self.maxs_z * '0 0 1');
970                 setattachment(self.teambubbleentity, self, "");  // sticks to moving player better, also conserves bandwidth
971                 self.teambubbleentity.mdl = self.teambubbleentity.model;
972                 self.teambubbleentity.model = self.teambubbleentity.mdl;
973                 self.teambubbleentity.customizeentityforclient = TeamBubble_customizeentityforclient;
974                 self.teambubbleentity.effects = EF_LOWPRECISION;
975         }
976 }
977
978 // LordHavoc: this hack will be removed when proper _pants/_shirt layers are
979 // added to the model skins
980 /*void() UpdateColorModHack =
981 {
982         local float c;
983         c = self.clientcolors & 15;
984         // LordHavoc: only bothering to support white, green, red, yellow, blue
985              if (teamplay == 0) self.colormod = '0 0 0';
986         else if (c ==  0) self.colormod = '1.00 1.00 1.00';
987         else if (c ==  3) self.colormod = '0.10 1.73 0.10';
988         else if (c ==  4) self.colormod = '1.73 0.10 0.10';
989         else if (c == 12) self.colormod = '1.22 1.22 0.10';
990         else if (c == 13) self.colormod = '0.10 0.10 1.73';
991         else self.colormod = '1 1 1';
992 };*/
993
994 void respawn(void)
995 {
996         CopyBody(1);
997         self.effects |= EF_NODRAW; // prevent another CopyBody
998         PutClientInServer();
999 }
1000
1001 void player_powerups (void)
1002 {
1003         if (g_minstagib)
1004         {
1005                 if (self.items & IT_STRENGTH)
1006                 {
1007                         if (time > self.strength_finished)
1008                         {
1009                                 if (g_minstagib_invis_alpha > 0)
1010                                 {
1011                                         self.alpha = default_player_alpha;
1012                                         self.exteriorweaponentity.alpha = default_weapon_alpha;
1013                                         self.effects = self.effects | EF_FULLBRIGHT;
1014                                 }
1015                                 else
1016                                 {
1017                                         self.effects -= self.effects & EF_NODRAW;
1018                                 }
1019                                 self.items = self.items - (self.items & IT_STRENGTH);
1020                                 sprint(self, "^3Invisibility has worn off\n");
1021                         }
1022                 }
1023                 else
1024                 {
1025                         if (time < self.strength_finished)
1026                         {
1027                                 if (g_minstagib_invis_alpha > 0)
1028                                 {
1029                                         self.alpha = g_minstagib_invis_alpha;
1030                                         self.exteriorweaponentity.alpha = g_minstagib_invis_alpha;
1031                                         self.effects -= self.effects & EF_FULLBRIGHT;
1032                                 }
1033                                 else
1034                                 {
1035                                         self.effects = self.effects | EF_NODRAW;
1036                                 }
1037                                 self.items = self.items | IT_STRENGTH;
1038                                 sprint(self, "^3You are invisible\n");
1039                         }
1040                 }
1041
1042                 if (self.items & IT_INVINCIBLE)
1043                 {
1044                         if (time > self.invincible_finished)
1045                         {
1046                                 self.items = self.items - (self.items & IT_INVINCIBLE);
1047                                 sprint(self, "^3Speed has worn off\n");
1048                         }
1049                 }
1050                 else
1051                 {
1052                         if (time < self.invincible_finished)
1053                         {
1054                                 self.items = self.items | IT_INVINCIBLE;
1055                                 sprint(self, "^3You are on speed\n");
1056                         }
1057                 }
1058                 return;
1059         }
1060
1061         self.effects = self.effects - (self.effects & (EF_RED | EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT));
1062         if (self.items & IT_STRENGTH)
1063         {
1064                 self.effects = self.effects | (EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT);
1065                 if (time > self.strength_finished)
1066                 {
1067                         self.items = self.items - (self.items & IT_STRENGTH);
1068                         sprint(self, "^3Strength has worn off\n");
1069                 }
1070         }
1071         else
1072         {
1073                 if (time < self.strength_finished)
1074                 {
1075                         self.items = self.items | IT_STRENGTH;
1076                         sprint(self, "^3Strength infuses your weapons with devastating power\n");
1077                 }
1078         }
1079         if (self.items & IT_INVINCIBLE)
1080         {
1081                 self.effects = self.effects | (EF_RED | EF_ADDITIVE | EF_FULLBRIGHT);
1082                 if (time > self.invincible_finished)
1083                 {
1084                         self.items = self.items - (self.items & IT_INVINCIBLE);
1085                         sprint(self, "^3Shield has worn off\n");
1086                 }
1087         }
1088         else
1089         {
1090                 if (time < self.invincible_finished)
1091                 {
1092                         self.items = self.items | IT_INVINCIBLE;
1093                         sprint(self, "^3Shield surrounds you\n");
1094                 }
1095         }
1096
1097         if (cvar("g_fullbrightplayers"))
1098                 self.effects = self.effects | EF_FULLBRIGHT;
1099
1100         // midair gamemode: damage only while in the air
1101         // if in midair mode, being on ground grants temporary invulnerability
1102         // (this is so that multishot weapon don't clear the ground flag on the
1103         // first damage in the frame, leaving the player vulnerable to the
1104         // remaining hits in the same frame)
1105         if (self.flags & FL_ONGROUND)
1106         if (g_midair)
1107                 self.spawnshieldtime = max(self.spawnshieldtime, time + cvar("g_midair_shieldtime"));
1108
1109         if (time < self.spawnshieldtime)
1110                 self.effects = self.effects | (EF_ADDITIVE | EF_FULLBRIGHT);
1111 }
1112
1113 float CalcRegen(float current, float stable, float regenfactor)
1114 {
1115         if(current > stable)
1116                 return current;
1117         else if(current > stable - 0.25) // when close enough, "snap"
1118                 return stable;
1119         else
1120                 return min(stable, current + (stable - current) * regenfactor * frametime);
1121 }
1122
1123 void player_regen (void)
1124 {
1125         float maxh, maxa, limith, limita, max_mod, regen_mod, rot_mod, limit_mod;
1126         maxh = cvar("g_balance_health_stable");
1127         maxa = cvar("g_balance_armor_stable");
1128         limith = cvar("g_balance_health_limit");
1129         limita = cvar("g_balance_armor_limit");
1130
1131         if (g_minstagib || (g_lms && !cvar("g_lms_regenerate")))
1132                 return;
1133
1134         max_mod = regen_mod = rot_mod = limit_mod = 1;
1135
1136         if (self.runes & RUNE_REGEN)
1137         {
1138                 if (self.runes & CURSE_VENOM) // do we have both rune/curse?
1139                 {
1140                         regen_mod = cvar("g_balance_rune_regen_combo_regenrate");
1141                         max_mod = cvar("g_balance_rune_regen_combo_hpmod");
1142                         limit_mod = cvar("g_balance_rune_regen_combo_limitmod");
1143                 }
1144                 else
1145                 {
1146                         regen_mod = cvar("g_balance_rune_regen_regenrate");
1147                         max_mod = cvar("g_balance_rune_regen_hpmod");
1148                         limit_mod = cvar("g_balance_rune_regen_limitmod");
1149                 }
1150         }
1151         else if (self.runes & CURSE_VENOM)
1152         {
1153                 max_mod = cvar("g_balance_curse_venom_hpmod");
1154                 if (self.runes & RUNE_REGEN) // do we have both rune/curse?
1155                         rot_mod = cvar("g_balance_rune_regen_combo_rotrate");
1156                 else
1157                         rot_mod = cvar("g_balance_curse_venom_rotrate");
1158                 limit_mod = cvar("g_balance_curse_venom_limitmod");
1159                 //if (!self.runes & RUNE_REGEN)
1160                 //      rot_mod = cvar("g_balance_curse_venom_rotrate");
1161         }
1162         maxh = maxh * max_mod;
1163         //maxa = maxa * max_mod;
1164         limith = limith * limit_mod;
1165         limita = limita * limit_mod;
1166
1167         if (self.armorvalue > maxa)
1168         {
1169                 if (time > self.pauserotarmor_finished)
1170                 {
1171                         self.armorvalue = max(maxa, self.armorvalue + (maxa - self.armorvalue) * cvar("g_balance_armor_rot") * frametime);
1172                         self.armorvalue = max(maxa, self.armorvalue - cvar("g_balance_armor_rotlinear") * frametime);
1173                 }
1174         }
1175         else if (self.armorvalue < maxa)
1176         {
1177                 if (time > self.pauseregen_finished)
1178                 {
1179                         self.armorvalue = CalcRegen(self.armorvalue, maxa, cvar("g_balance_armor_regen"));
1180                         self.armorvalue = min(maxa, self.armorvalue + cvar("g_balance_armor_regenlinear") * frametime);
1181                 }
1182         }
1183         if (self.health > maxh)
1184         {
1185                 if (time > self.pauserothealth_finished)
1186                 {
1187                         self.health = max(maxh, self.health + (maxh - self.health) * rot_mod*cvar("g_balance_health_rot") * frametime);
1188                         self.health = max(maxh, self.health - rot_mod*cvar("g_balance_health_rotlinear") * frametime);
1189                 }
1190         }
1191         else if (self.health < maxh)
1192         {
1193                 if (time > self.pauseregen_finished)
1194                 {
1195                         self.health = CalcRegen(self.health, maxh, regen_mod * cvar("g_balance_health_regen"));
1196                         self.health = min(maxh, self.health + regen_mod*cvar("g_balance_health_regenlinear") * frametime);
1197                 }
1198         }
1199
1200         if (self.health > limith)
1201                 self.health = limith;
1202         if (self.armorvalue > limita)
1203                 self.armorvalue = limita;
1204
1205         // if player rotted to death...  die!
1206         if(self.health < 1)
1207                 self.event_damage(self, self, 1, DEATH_ROT, self.origin, '0 0 0');
1208 }
1209
1210 /*
1211 ======================
1212 spectate mode routines
1213 ======================
1214 */
1215 void SpectateCopy(entity spectatee) {
1216         self.armortype = spectatee.armortype;
1217         self.armorvalue = spectatee.armorvalue;
1218         self.currentammo = spectatee.currentammo;
1219         self.effects = spectatee.effects;
1220         self.health = spectatee.health;
1221         self.impulse = 0;
1222         self.items = spectatee.items;
1223         self.punchangle = spectatee.punchangle;
1224         self.view_ofs = spectatee.view_ofs;
1225         self.v_angle = spectatee.v_angle;
1226         self.viewzoom = spectatee.viewzoom;
1227         self.velocity = spectatee.velocity;
1228         self.dmg_take = spectatee.dmg_take;
1229         self.dmg_save = spectatee.dmg_save;
1230         self.dmg_inflictor = spectatee.dmg_inflictor;
1231         self.angles = spectatee.v_angle;
1232         self.fixangle = TRUE;
1233         setorigin(self, spectatee.origin);
1234         setsize(self, spectatee.mins, spectatee.maxs);
1235 }
1236
1237 void SpectateUpdate() {
1238         if(!self.enemy)
1239                         PutObserverInServer();
1240
1241         if (self != self.enemy) {
1242                 if(self.enemy.flags & FL_NOTARGET)
1243                         PutObserverInServer();
1244                 SpectateCopy(self.enemy);
1245                 //msg_entity = self;
1246                 //WriteByte(MSG_ONE, SVC_SETANGLE);
1247                 //WriteAngle(MSG_ONE, self.enemy.v_angle_x);
1248                 //WriteAngle(MSG_ONE, self.enemy.v_angle_y);
1249                 //WriteAngle(MSG_ONE, self.enemy.v_angle_z);
1250         }
1251 }
1252
1253 float SpectateNext() {
1254         other = find(self.enemy, classname, "player");
1255         if (!other) {
1256                 other = find(other, classname, "player");
1257         }
1258         if (other) {
1259                 self.enemy = other;
1260         }
1261         if(self.enemy.classname == "player") {
1262                 msg_entity = self;
1263                 WriteByte(MSG_ONE, SVC_SETVIEW);
1264                 WriteEntity(MSG_ONE, self.enemy);
1265                 //stuffcmd(self, "set viewsize $tmpviewsize \n");
1266                 SpectateUpdate();
1267                 return 1;
1268         } else {
1269                 return 0;
1270         }
1271 }
1272
1273 /*
1274 =============
1275 ShowRespawnCountdown()
1276
1277 Update a respawn countdown display.
1278 =============
1279 */
1280 void ShowRespawnCountdown()
1281 {
1282         float number;
1283         if(self.deadflag == DEAD_NO) // just respawned?
1284                 return;
1285         else
1286         {
1287                 number = ceil(self.death_time - time);
1288                 if(number <= 0)
1289                         return;
1290                 if(number <= self.respawn_countdown)
1291                 {
1292                         self.respawn_countdown = number - 1;
1293                         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
1294                                 play2(self, strcat("announcer/robotic/", ftos(number), "announcer/.ogg"));
1295                 }
1296         }
1297 }
1298
1299 void LeaveSpectatorMode()
1300 {
1301         if(!cvar("teamplay") || cvar("g_campaign") || cvar("g_balance_teams")) {
1302                 self.classname = "player";
1303                 if(cvar("g_campaign") || cvar("g_balance_teams"))
1304                         JoinBestTeam(self, 0);
1305                 if(cvar("g_campaign"))
1306                         campaign_bots_may_start = 1;
1307                 PutClientInServer();
1308                 if(!(self.flags & FL_NOTARGET))
1309                         bprint ("^4", self.netname, "^4 is playing now\n");
1310                 centerprint(self,"");
1311                 return;
1312         } else {
1313                 stuffcmd(self,"menu_showteamselect\n");
1314                 return;
1315         }
1316 }
1317
1318 /*
1319 =============
1320 PlayerPreThink
1321
1322 Called every frame for each client before the physics are run
1323 =============
1324 */
1325 void() ctf_setstatus;
1326 .float vote_nagtime;
1327 void PlayerPreThink (void)
1328 {
1329         // version nagging
1330         if(self.version_nagtime)
1331                 if(self.cvar_g_nexuizversion)
1332                         if(time > self.version_nagtime)
1333                         {
1334                                 if(strstr(self.cvar_g_nexuizversion, "svn", 0) < 0)
1335                                         if(self.cvar_g_nexuizversion != cvar_string("g_nexuizversion"))
1336                                         {
1337                                                 dprint("^1NOTE^7 to ", self.netname, "^7 - the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "\n");
1338                                                 sprint(self, strcat("\{1}^1NOTE: ^7the server is running ^3Nexuiz ", cvar_string("g_nexuizversion"), "^7, you have ^3Nexuiz ", self.cvar_g_nexuizversion, "\n"));
1339                                         }
1340                                 self.version_nagtime = 0;
1341                         }
1342
1343         // vote nagging
1344         if(self.cvar_scr_centertime)
1345                 if(time > self.vote_nagtime)
1346                 {
1347                         VoteNag();
1348                         self.vote_nagtime = time + self.cvar_scr_centertime * 0.6;
1349                 }
1350
1351         // GOD MODE info
1352         if(!(self.flags & FL_GODMODE)) if(self.max_armorvalue)
1353         {
1354                 sprint(self, strcat("godmode saved you ", ftos(self.max_armorvalue), " units of damage, cheater!\n"));
1355                 self.max_armorvalue = 0;
1356         }
1357
1358         if(self.classname == "player") {
1359 //              if(self.netname == "Wazat")
1360 //                      bprint(self.classname, "\n");
1361
1362                 CheckRules_Player();
1363
1364                 if(self.button7)
1365                         PrintWelcomeMessage(self);
1366
1367                 if(g_lms || !cvar("sv_spectate"))
1368                 if((time - self.jointime) <= cvar("welcome_message_time"))
1369                         PrintWelcomeMessage(self);
1370
1371                 if (intermission_running)
1372                 {
1373                         IntermissionThink ();   // otherwise a button could be missed between
1374                         return;                                 // the think tics
1375                 }
1376
1377                 if(self.teleport_time)
1378                 if(time > self.teleport_time)
1379                 {
1380                         self.teleport_time = 0;
1381                         self.effects = self.effects - (self.effects & EF_NODRAW);
1382                         if(self.weaponentity)
1383                                 self.weaponentity.flags = self.weaponentity.flags - (self.weaponentity.flags & EF_NODRAW);
1384                 }
1385
1386                 Nixnex_GiveCurrentWeapon();
1387
1388                 if(frametime > 0) // don't do this in cl_movement frames, just in server ticks
1389                         UpdateSelectedPlayer();
1390
1391                 if (self.deadflag != DEAD_NO)
1392                 {
1393                         float button_pressed, force_respawn;
1394                         player_anim();
1395                         button_pressed = (self.button0 || self.button2 || self.button3 || self.button6 || self.buttonuse);
1396                         force_respawn = (g_lms || cvar("g_forced_respawn"));
1397                         if (self.deadflag == DEAD_DYING)
1398                         {
1399                                 if(force_respawn)
1400                                         self.deadflag = DEAD_RESPAWNING;
1401                                 else if(!button_pressed)
1402                                         self.deadflag = DEAD_DEAD;
1403                         }
1404                         else if (self.deadflag == DEAD_DEAD)
1405                         {
1406                                 if(button_pressed)
1407                                         self.deadflag = DEAD_RESPAWNABLE;
1408                         }
1409                         else if (self.deadflag == DEAD_RESPAWNABLE)
1410                         {
1411                                 if(!button_pressed)
1412                                         self.deadflag = DEAD_RESPAWNING;
1413                         }
1414                         else if (self.deadflag == DEAD_RESPAWNING)
1415                         {
1416                                 if(time > self.death_time)
1417                                 {
1418                                         self.death_time = time + 1; // only retry once a second
1419                                         respawn();
1420                                 }
1421                         }
1422                         ShowRespawnCountdown();
1423                         return;
1424                 }
1425
1426                 if(g_lms && !self.deadflag && cvar("g_lms_campcheck_interval"))
1427                 {
1428                         vector dist;
1429
1430                         // calculate player movement (in 2 dimensions only, so jumping on one spot doesn't count as movement)
1431                         dist = self.oldorigin - self.origin;
1432                         dist_z = 0;
1433                         self.lms_traveled_distance += fabs(vlen(dist));
1434
1435                         if((cvar("g_campaign") && !campaign_bots_may_start) || (time < restart_countdown))
1436                         {
1437                                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval")*2;
1438                                 self.lms_traveled_distance = 0;
1439                         }
1440
1441                         if(time > self.lms_nextcheck)
1442                         {
1443                                 //sprint(self, "distance: ", ftos(self.lms_traveled_distance), "\n");
1444                                 if(self.lms_traveled_distance < cvar("g_lms_campcheck_distance"))
1445                                 {
1446                                         centerprint(self, cvar_string("g_lms_campcheck_message"));
1447                                         // FIXME KadaverJack: gibbing player here causes playermodel to bounce around, instead of eye.md3
1448                                         // I wasn't able to find out WHY that happens, so I put a workaround in place that shall prevent players from being gibbed :(
1449                                         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');
1450                                 }
1451                                 self.lms_nextcheck = time + cvar("g_lms_campcheck_interval");
1452                                 self.lms_traveled_distance = 0;
1453                         }
1454                 }
1455
1456                 if (self.button5 && !self.hook.state)
1457                 {
1458                         if (!self.crouch)
1459                         {
1460                                 self.crouch = TRUE;
1461                                 self.view_ofs = PL_CROUCH_VIEW_OFS;
1462                                 setsize (self, PL_CROUCH_MIN, PL_CROUCH_MAX);
1463                                 player_setanim(self.anim_duck, FALSE, TRUE, TRUE);
1464                         }
1465                 }
1466                 else
1467                 {
1468                         if (self.crouch)
1469                         {
1470                                 tracebox(self.origin, PL_MIN, PL_MAX, self.origin, FALSE, self);
1471                                 if (!trace_startsolid)
1472                                 {
1473                                         self.crouch = FALSE;
1474                                         self.view_ofs = PL_VIEW_OFS;
1475                                         setsize (self, PL_MIN, PL_MAX);
1476                                 }
1477                         }
1478                 }
1479
1480                 FixPlayermodel();
1481
1482                 GrapplingHookFrame();
1483
1484                 W_WeaponFrame();
1485
1486                 {
1487                         float zoomfactor, zoomspeed, zoomdir;
1488                         zoomfactor = self.cvar_cl_zoomfactor;
1489                         if(zoomfactor < 1 || zoomfactor > 16)
1490                                 zoomfactor = 2.5;
1491                         zoomspeed = self.cvar_cl_zoomspeed;
1492                         if(zoomspeed >= 0) // < 0 is instant zoom
1493                                 if(zoomspeed < 0.5 || zoomspeed > 16)
1494                                         zoomspeed = 3.5;
1495
1496                         zoomdir = self.button4;
1497                         if(self.button3)
1498                                 if(self.weapon == WEP_NEX)
1499                                         if(!g_minstagib)
1500                                                 zoomdir = 1;
1501
1502                         if(zoomdir)
1503                                 self.has_zoomed = 1;
1504
1505                         if(self.has_zoomed)
1506                         {
1507                                 if(zoomspeed <= 0) // instant zoom
1508                                 {
1509                                         if(zoomdir)
1510                                                 self.viewzoom = 1 / zoomfactor;
1511                                         else
1512                                                 self.viewzoom = 1;
1513                                 }
1514                                 else
1515                                 {
1516                                         // geometric zoom would be:
1517                                         //   self.viewzoom = bound(1 / zoomfactor, self.viewzoom * pow(zoomfactor, (zoomdir ? -1 : 1) * frametime * zoomspeed), 1);
1518                                         // however, testing showed that arithmetic/harmonic zoom works better
1519                                         if(zoomdir)
1520                                                 // self.viewzoom = 1 / bound(1, 1 / self.viewzoom + (zoomdir ? 1 : -1) * frametime * zoomspeed * (zoomfactor - 1), zoomfactor);
1521                                                 // zoom in = arithmetic: 1x, 2x, 3x, 4x, ..., 8x
1522                                                 self.viewzoom = 1 / bound(1, 1 / self.viewzoom + frametime * zoomspeed * (zoomfactor - 1), zoomfactor);
1523                                         else
1524                                                 // self.viewzoom = bound(1 / zoomfactor, self.viewzoom + (zoomdir ? -1 : 1) * frametime * zoomspeed * (1 - 1 / zoomfactor), 1);
1525                                                 // zoom out = harmonic: 8/1x, 8/2x, 8/3x, 8/4x, ..., 8/8x
1526                                                 self.viewzoom = bound(1 / zoomfactor, self.viewzoom + frametime * zoomspeed * (1 - 1 / zoomfactor), 1);
1527                                 }
1528                         }
1529                         else
1530                                 self.viewzoom = min(1, self.viewzoom + frametime); // spawn zoom-in
1531                 }
1532
1533                 player_powerups();
1534                 player_regen();
1535                 player_anim();
1536
1537                 if (g_minstagib)
1538                         minstagib_ammocheck();
1539
1540                 ctf_setstatus();
1541                 kh_setstatus();
1542
1543                 //self.angles_y=self.v_angle_y + 90;   // temp
1544
1545                 //if (TetrisPreFrame()) return;
1546         } else if(gameover) {
1547                 if (intermission_running)
1548                         IntermissionThink ();   // otherwise a button could be missed between
1549                 return;
1550         } else if(self.classname == "observer") {
1551
1552                 if (self.flags & FL_JUMPRELEASED) {
1553                         if (self.button2 && self.version == cvar("gameversion")) {
1554                                 self.welcomemessage_time = 0;
1555                                 self.flags = self.flags - FL_JUMPRELEASED;
1556                                 LeaveSpectatorMode();
1557                                 return;
1558                         } else if(self.button0 && self.version == cvar("gameversion")) {
1559                                 self.welcomemessage_time = 0;
1560                                 self.flags = self.flags - FL_JUMPRELEASED;
1561                                 if(SpectateNext() == 1) {
1562                                         self.classname = "spectator";
1563                                 }
1564                         }
1565                 } else {
1566                         if (!(self.button0 || self.button2)) {
1567                                 self.flags = self.flags | FL_JUMPRELEASED;
1568                         }
1569                 }
1570                 PrintWelcomeMessage(self);
1571         } else if(self.classname == "spectator") {
1572                 if (self.flags & FL_JUMPRELEASED) {
1573                         if (self.button2 && self.version == cvar("gameversion")) {
1574                                 self.welcomemessage_time = 0;
1575                                 self.flags = self.flags - FL_JUMPRELEASED;
1576                                 LeaveSpectatorMode();
1577                                 return;
1578                         } else if(self.button0) {
1579                                 self.welcomemessage_time = 0;
1580                                 self.flags = self.flags - FL_JUMPRELEASED;
1581                                 if(SpectateNext() == 1) {
1582                                         self.classname = "spectator";
1583                                 } else {
1584                                         self.classname = "observer";
1585                                         PutClientInServer();
1586                                 }
1587                         } else if (self.button3) {
1588                                 self.welcomemessage_time = 0;
1589                                 self.flags = self.flags - FL_JUMPRELEASED;
1590                                 self.classname = "observer";
1591                                 PutClientInServer();
1592                         } else {
1593                                 SpectateUpdate();
1594                         }
1595                 } else {
1596                         if (!(self.button0 || self.button3)) {
1597                                 self.flags = self.flags | FL_JUMPRELEASED;
1598                         }
1599                 }
1600                 PrintWelcomeMessage(self);
1601                 self.flags = self.flags | FL_CLIENT | FL_NOTARGET;
1602         }
1603 }
1604
1605
1606 /*
1607 =============
1608 PlayerPostThink
1609
1610 Called every frame for each client after the physics are run
1611 =============
1612 */
1613 void PlayerPostThink (void)
1614 {
1615         // Savage: Check for nameless players
1616         if (strlen(self.netname) < 1) {
1617                 self.netname = "Player";
1618                 stuffcmd(self, "seta _cl_name Player\n");
1619         }
1620
1621         if(self.classname == "player") {
1622                 CheckRules_Player();
1623                 UpdateChatBubble();
1624                 UpdateTeamBubble();
1625                 if (self.impulse)
1626                         ImpulseCommands();
1627                 if (intermission_running)
1628                         return;         // intermission or finale
1629
1630                 //PrintWelcomeMessage(self);
1631                 //if (TetrisPostFrame()) return;
1632         
1633                 // restart countdown
1634                 if(time < restart_countdown)
1635                 {
1636                         string s;
1637                         float sec;
1638
1639                         sec = ceil(restart_countdown-time);
1640                         s = strcat(NEWLINES, "^1Game starts in ", ftos(sec), " seconds");
1641                         centerprint(self, s);
1642                         self.movetype = MOVETYPE_NONE;          
1643                         self.velocity = '0 0 0';
1644                         self.avelocity = '0 0 0';
1645                         self.movement = '0 0 0';
1646                 }
1647                 else if(self.movetype == MOVETYPE_NONE)
1648                 {
1649                         self.movetype = MOVETYPE_WALK;
1650                         centerprint(self, "\n");
1651                 }
1652         } else if (self.classname == "observer") {
1653                 //do nothing
1654         } else if (self.classname == "spectator") {
1655                 //do nothing
1656         }
1657
1658         Arena_Warmup();
1659
1660         if(frametime)
1661                 antilag_record(self);
1662 }