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