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