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