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