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