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