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