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