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