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