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