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