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