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