]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_client.qc
Suppress mutator blocked weapon warnings
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_client.qc
1 #include "cl_client.qh"
2
3 #include "anticheat.qh"
4 #include "cl_impulse.qh"
5 #include "cl_player.qh"
6 #include "ipban.qh"
7 #include "miscfunctions.qh"
8 #include "portals.qh"
9 #include "teamplay.qh"
10 #include "playerdemo.qh"
11 #include "spawnpoints.qh"
12 #include "g_damage.qh"
13 #include "g_hook.qh"
14 #include "command/common.qh"
15 #include "cheats.qh"
16 #include "g_world.qh"
17 #include "race.qh"
18 #include "antilag.qh"
19 #include "campaign.qh"
20 #include "command/common.qh"
21
22 #include "bot/bot.qh"
23 #include "bot/navigation.qh"
24
25 #include "../common/ent_cs.qh"
26 #include "../common/state.qh"
27
28 #include "../common/triggers/teleporters.qh"
29
30 #include "../common/vehicles/all.qh"
31
32 #include "weapons/hitplot.qh"
33 #include "weapons/weaponsystem.qh"
34
35 #include "../common/net_notice.qh"
36 #include "../common/physics/player.qh"
37
38 #include "../common/items/all.qc"
39
40 #include "../common/mutators/mutator/waypoints/all.qh"
41
42 #include "../common/triggers/subs.qh"
43 #include "../common/triggers/triggers.qh"
44 #include "../common/triggers/trigger/secret.qh"
45
46 #include "../common/minigames/sv_minigames.qh"
47
48 #include "../common/items/inventory.qh"
49
50 #include "../common/monsters/sv_monsters.qh"
51
52 #include "../lib/warpzone/server.qh"
53
54
55 void send_CSQC_teamnagger() {
56         WriteHeader(MSG_BROADCAST, TE_CSQC_TEAMNAGGER);
57 }
58
59 bool ClientData_Send(entity this, entity to, int sf)
60 {
61         assert(to == this.owner, return false);
62
63         entity e = to;
64         if (IS_SPEC(e)) e = e.enemy;
65
66         sf = 0;
67         if (e.race_completed)       sf |= 1; // forced scoreboard
68         if (to.spectatee_status)    sf |= 2; // spectator ent number follows
69         if (e.zoomstate)            sf |= 4; // zoomed
70         if (e.porto_v_angle_held)   sf |= 8; // angles held
71
72         WriteHeader(MSG_ENTITY, ENT_CLIENT_CLIENTDATA);
73         WriteByte(MSG_ENTITY, sf);
74
75         if (sf & 2)
76         {
77                 WriteByte(MSG_ENTITY, to.spectatee_status);
78         }
79         if (sf & 8)
80         {
81                 WriteAngle(MSG_ENTITY, e.v_angle.x);
82                 WriteAngle(MSG_ENTITY, e.v_angle.y);
83         }
84         return true;
85 }
86
87 void ClientData_Attach(entity this)
88 {
89         Net_LinkEntity(this.clientdata = new(clientdata), false, 0, ClientData_Send);
90         make_pure(this.clientdata);
91         self.clientdata.drawonlytoclient = this;
92         self.clientdata.owner = this;
93 }
94
95 void ClientData_Detach(entity this)
96 {
97         remove(this.clientdata);
98         self.clientdata = NULL;
99 }
100
101 void ClientData_Touch(entity e)
102 {
103         e.clientdata.SendFlags = 1;
104
105         // make it spectatable
106         FOREACH_CLIENT(IS_REAL_CLIENT(it) && it != e && IS_SPEC(it) && it.enemy == e, LAMBDA(it.clientdata.SendFlags = 1));
107 }
108
109 .string netname_previous;
110
111 void SetSpectatee(entity player, entity spectatee);
112
113
114 /*
115 =============
116 CheckPlayerModel
117
118 Checks if the argument string can be a valid playermodel.
119 Returns a valid one in doubt.
120 =============
121 */
122 string FallbackPlayerModel;
123 string CheckPlayerModel(string plyermodel) {
124         if(FallbackPlayerModel != cvar_defstring("_cl_playermodel"))
125         {
126                 // note: we cannot summon Don Strunzone here, some player may
127                 // still have the model string set. In case anyone manages how
128                 // to change a cvar default, we'll have a small leak here.
129                 FallbackPlayerModel = strzone(cvar_defstring("_cl_playermodel"));
130         }
131         // only in right path
132         if( substring(plyermodel,0,14) != "models/player/")
133                 return FallbackPlayerModel;
134         // only good file extensions
135         if(substring(plyermodel,-4,4) != ".zym")
136         if(substring(plyermodel,-4,4) != ".dpm")
137         if(substring(plyermodel,-4,4) != ".iqm")
138         if(substring(plyermodel,-4,4) != ".md3")
139         if(substring(plyermodel,-4,4) != ".psk")
140                 return FallbackPlayerModel;
141         // forbid the LOD models
142         if(substring(plyermodel, -9,5) == "_lod1")
143                 return FallbackPlayerModel;
144         if(substring(plyermodel, -9,5) == "_lod2")
145                 return FallbackPlayerModel;
146         if(plyermodel != strtolower(plyermodel))
147                 return FallbackPlayerModel;
148         // also, restrict to server models
149         if(autocvar_sv_servermodelsonly)
150         {
151                 if(!fexists(plyermodel))
152                         return FallbackPlayerModel;
153         }
154         return plyermodel;
155 }
156
157 void setplayermodel(entity e, string modelname)
158 {
159         precache_model(modelname);
160         _setmodel(e, modelname);
161         player_setupanimsformodel();
162 }
163
164 /*
165 =============
166 PutObserverInServer
167
168 putting a client as observer in the server
169 =============
170 */
171 void FixPlayermodel(entity player);
172 void PutObserverInServer()
173 {
174         SELFPARAM();
175         PlayerState_detach(this);
176         entity  spot;
177     self.hud = HUD_NORMAL;
178
179         if(IS_PLAYER(self)) { Send_Effect(EFFECT_SPAWN_NEUTRAL, self.origin, '0 0 0', 1); }
180
181         spot = SelectSpawnPoint (true);
182         if(!spot)
183                 error("No spawnpoints for observers?!?\n");
184         RemoveGrapplingHook(self); // Wazat's Grappling Hook
185
186         if(IS_REAL_CLIENT(self))
187         {
188                 msg_entity = self;
189                 WriteByte(MSG_ONE, SVC_SETVIEW);
190                 WriteEntity(MSG_ONE, self);
191         }
192
193         self.frags = FRAGS_SPECTATOR;
194         self.bot_attack = false;
195
196         bool mutator_returnvalue = MUTATOR_CALLHOOK(MakePlayerObserver);
197
198         Portal_ClearAll(self);
199
200         Unfreeze(self);
201
202         if(self.alivetime)
203         {
204                 if(!warmup_stage)
205                         PS_GR_P_ADDVAL(self, PLAYERSTATS_ALIVETIME, time - self.alivetime);
206                 self.alivetime = 0;
207         }
208
209         if(self.vehicle)
210                 vehicles_exit(VHEF_RELEASE);
211
212         WaypointSprite_PlayerDead(self);
213
214         if(!mutator_returnvalue)  // mutator prevents resetting teams
215                 self.team = -1;  // move this as it is needed to log the player spectating in eventlog
216
217         if(self.killcount != FRAGS_SPECTATOR)
218         {
219                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_QUIT_SPECTATE, self.netname);
220                 if(!intermission_running)
221                 if(autocvar_g_chat_nospectators == 1 || (!(warmup_stage || gameover) && autocvar_g_chat_nospectators == 2))
222                         Send_Notification(NOTIF_ONE_ONLY, self, MSG_INFO, INFO_CHAT_NOSPECTATORS);
223
224                 if(self.just_joined == false) {
225                         LogTeamchange(self.playerid, -1, 4);
226                 } else
227                         self.just_joined = false;
228         }
229
230         PlayerScore_Clear(self); // clear scores when needed
231
232         accuracy_resend(self);
233
234         self.spectatortime = time;
235
236         self.classname = STR_OBSERVER;
237         self.iscreature = false;
238         self.teleportable = TELEPORT_SIMPLE;
239         self.damagedbycontents = false;
240         self.health = FRAGS_SPECTATOR;
241         self.takedamage = DAMAGE_NO;
242         self.solid = SOLID_NOT;
243         self.movetype = MOVETYPE_FLY_WORLDONLY; // user preference is controlled by playerprethink
244         self.flags = FL_CLIENT | FL_NOTARGET;
245         self.armorvalue = 666;
246         self.effects = 0;
247         self.armorvalue = autocvar_g_balance_armor_start;
248         self.pauserotarmor_finished = 0;
249         self.pauserothealth_finished = 0;
250         self.pauseregen_finished = 0;
251         self.damageforcescale = 0;
252         self.death_time = 0;
253         self.respawn_flags = 0;
254         self.respawn_time = 0;
255         self.stat_respawn_time = 0;
256         self.alpha = 0;
257         self.scale = 0;
258         self.fade_time = 0;
259         self.pain_frame = 0;
260         self.pain_finished = 0;
261         self.strength_finished = 0;
262         self.invincible_finished = 0;
263         self.superweapons_finished = 0;
264         self.pushltime = 0;
265         self.istypefrag = 0;
266         self.think = func_null;
267         self.nextthink = 0;
268         self.hook_time = 0;
269         self.deadflag = DEAD_NO;
270         self.angles = spot.angles;
271         self.angles_z = 0;
272         self.fixangle = true;
273         self.crouch = false;
274         self.revival_time = 0;
275
276         setorigin (self, (spot.origin + STAT(PL_VIEW_OFS, NULL))); // offset it so that the spectator spawns higher off the ground, looks better this way
277         self.prevorigin = self.origin;
278         self.items = 0;
279         self.weapons = '0 0 0';
280         self.model = "";
281         FixPlayermodel(self);
282         setmodel(self, MDL_Null);
283         self.drawonlytoclient = self;
284
285         setsize (self, STAT(PL_CROUCH_MIN, NULL), STAT(PL_CROUCH_MAX, NULL)); // give the spectator some space between walls for MOVETYPE_FLY_WORLDONLY
286         self.view_ofs = '0 0 0'; // so that your view doesn't go into the ceiling with MOVETYPE_FLY_WORLDONLY, previously "PL_VIEW_OFS"
287
288         self.weaponname = "";
289         self.weaponmodel = "";
290         for (int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
291         {
292                 self.weaponentities[slot] = NULL;
293         }
294         self.exteriorweaponentity = world;
295         self.killcount = FRAGS_SPECTATOR;
296         self.velocity = '0 0 0';
297         self.avelocity = '0 0 0';
298         self.punchangle = '0 0 0';
299         self.punchvector = '0 0 0';
300         self.oldvelocity = self.velocity;
301         self.fire_endtime = -1;
302         self.event_damage = func_null;
303 }
304
305 int player_getspecies(entity this)
306 {
307         get_model_parameters(this.model, this.skin);
308         int s = get_model_parameters_species;
309         get_model_parameters(string_null, 0);
310         if (s < 0) return SPECIES_HUMAN;
311         return s;
312 }
313
314 .float model_randomizer;
315 void FixPlayermodel(entity player)
316 {
317         string defaultmodel = "";
318         int defaultskin = 0;
319         if(autocvar_sv_defaultcharacter)
320         {
321                 if(teamplay)
322                 {
323                         string s = Static_Team_ColorName_Lower(player.team);
324                         if (s != "neutral")
325                         {
326                                 defaultmodel = cvar_string(strcat("sv_defaultplayermodel_", s));
327                                 defaultskin = cvar(strcat("sv_defaultplayerskin_", s));
328                         }
329                 }
330
331                 if(defaultmodel == "")
332                 {
333                         defaultmodel = autocvar_sv_defaultplayermodel;
334                         defaultskin = autocvar_sv_defaultplayerskin;
335                 }
336
337                 int n = tokenize_console(defaultmodel);
338                 if(n > 0)
339                 {
340                         defaultmodel = argv(floor(n * player.model_randomizer));
341                         // However, do NOT randomize if the player-selected model is in the list.
342                         for (int i = 0; i < n; ++i)
343                                 if ((argv(i) == player.playermodel && defaultskin == stof(player.playerskin)) || argv(i) == strcat(player.playermodel, ":", player.playerskin))
344                                         defaultmodel = argv(i);
345                 }
346
347                 int i = strstrofs(defaultmodel, ":", 0);
348                 if(i >= 0)
349                 {
350                         defaultskin = stof(substring(defaultmodel, i+1, -1));
351                         defaultmodel = substring(defaultmodel, 0, i);
352                 }
353         }
354         MUTATOR_CALLHOOK(FixPlayermodel, defaultmodel, defaultskin);
355         defaultmodel = ret_string;
356         defaultskin = ret_int;
357
358         bool chmdl = false;
359         int oldskin;
360         if(defaultmodel != "")
361         {
362                 if (defaultmodel != player.model)
363                 {
364                         vector m1 = player.mins;
365                         vector m2 = player.maxs;
366                         setplayermodel (player, defaultmodel);
367                         setsize (player, m1, m2);
368                         chmdl = true;
369                 }
370
371                 oldskin = player.skin;
372                 player.skin = defaultskin;
373         } else {
374                 if (player.playermodel != player.model || player.playermodel == "")
375                 {
376                         player.playermodel = CheckPlayerModel(player.playermodel); // this is never "", so no endless loop
377                         vector m1 = player.mins;
378                         vector m2 = player.maxs;
379                         setplayermodel (player, player.playermodel);
380                         setsize (player, m1, m2);
381                         chmdl = true;
382                 }
383
384                 oldskin = player.skin;
385                 player.skin = stof(player.playerskin);
386         }
387
388         if(chmdl || oldskin != player.skin) // model or skin has changed
389         {
390                 player.species = player_getspecies(player); // update species
391         }
392
393         if(!teamplay)
394                 if(strlen(autocvar_sv_defaultplayercolors))
395                         if(player.clientcolors != stof(autocvar_sv_defaultplayercolors))
396                                 setcolor(player, stof(autocvar_sv_defaultplayercolors));
397 }
398
399
400 /** Called when a client spawns in the server */
401 void PutClientInServer()
402 {
403         SELFPARAM();
404         if (IS_BOT_CLIENT(this)) {
405                 this.classname = STR_PLAYER;
406         } else if (IS_REAL_CLIENT(this)) {
407                 msg_entity = this;
408                 WriteByte(MSG_ONE, SVC_SETVIEW);
409                 WriteEntity(MSG_ONE, this);
410         }
411         if (gameover) {
412                 this.classname = STR_OBSERVER;
413         }
414
415         SetSpectatee(this, NULL);
416
417         // reset player keys
418         this.itemkeys = 0;
419
420         MUTATOR_CALLHOOK(PutClientInServer, this);
421
422         if (IS_OBSERVER(this)) {
423                 PutObserverInServer();
424         } else if (IS_PLAYER(this)) {
425                 PlayerState_attach(this);
426                 accuracy_resend(this);
427
428                 if (this.team < 0)
429                         JoinBestTeam(this, false, true);
430
431                 entity spot = SelectSpawnPoint(false);
432                 if (!spot) {
433                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_JOIN_NOSPAWNS);
434                         return; // spawn failed
435                 }
436
437                 this.classname = STR_PLAYER;
438                 this.wasplayer = true;
439                 this.iscreature = true;
440                 this.teleportable = TELEPORT_NORMAL;
441                 this.damagedbycontents = true;
442                 this.movetype = MOVETYPE_WALK;
443                 this.solid = SOLID_SLIDEBOX;
444                 this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
445                 if (autocvar_g_playerclip_collisions)
446                         this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
447                 if (IS_BOT_CLIENT(this) && autocvar_g_botclip_collisions)
448                         this.dphitcontentsmask |= DPCONTENTS_BOTCLIP;
449                 this.frags = FRAGS_PLAYER;
450                 if (INDEPENDENT_PLAYERS) MAKE_INDEPENDENT_PLAYER(this);
451                 this.flags = FL_CLIENT | FL_PICKUPITEMS;
452                 if (autocvar__notarget)
453                         this.flags |= FL_NOTARGET;
454                 this.takedamage = DAMAGE_AIM;
455                 this.effects = EF_TELEPORT_BIT | EF_RESTARTANIM_BIT;
456                 this.dmg = 2; // WTF
457
458                 if (warmup_stage) {
459                         this.ammo_shells = warmup_start_ammo_shells;
460                         this.ammo_nails = warmup_start_ammo_nails;
461                         this.ammo_rockets = warmup_start_ammo_rockets;
462                         this.ammo_cells = warmup_start_ammo_cells;
463                         this.ammo_plasma = warmup_start_ammo_plasma;
464                         this.ammo_fuel = warmup_start_ammo_fuel;
465                         this.health = warmup_start_health;
466                         this.armorvalue = warmup_start_armorvalue;
467                         this.weapons = WARMUP_START_WEAPONS;
468                 } else {
469                         this.ammo_shells = start_ammo_shells;
470                         this.ammo_nails = start_ammo_nails;
471                         this.ammo_rockets = start_ammo_rockets;
472                         this.ammo_cells = start_ammo_cells;
473                         this.ammo_plasma = start_ammo_plasma;
474                         this.ammo_fuel = start_ammo_fuel;
475                         this.health = start_health;
476                         this.armorvalue = start_armorvalue;
477                         this.weapons = start_weapons;
478                 }
479
480                 this.superweapons_finished = (this.weapons & WEPSET_SUPERWEAPONS) ? time + autocvar_g_balance_superweapons_time : 0;
481
482                 this.items = start_items;
483
484                 this.spawnshieldtime = time + autocvar_g_spawnshieldtime;
485                 this.pauserotarmor_finished = time + autocvar_g_balance_pause_armor_rot_spawn;
486                 this.pauserothealth_finished = time + autocvar_g_balance_pause_health_rot_spawn;
487                 this.pauserotfuel_finished = time + autocvar_g_balance_pause_fuel_rot_spawn;
488                 this.pauseregen_finished = time + autocvar_g_balance_pause_health_regen_spawn;
489                 // extend the pause of rotting if client was reset at the beginning of the countdown
490                 if (!autocvar_sv_ready_restart_after_countdown && time < game_starttime) { // TODO why is this cvar NOTted?
491                         float f = game_starttime - time;
492                         this.spawnshieldtime += f;
493                         this.pauserotarmor_finished += f;
494                         this.pauserothealth_finished += f;
495                         this.pauseregen_finished += f;
496                 }
497                 this.damageforcescale = 2;
498                 this.death_time = 0;
499                 this.respawn_flags = 0;
500                 this.respawn_time = 0;
501                 this.stat_respawn_time = 0;
502                 this.scale = autocvar_sv_player_scale;
503                 this.fade_time = 0;
504                 this.pain_frame = 0;
505                 this.pain_finished = 0;
506                 this.pushltime = 0;
507                 this.think = func_null; // players have no think function
508                 this.nextthink = 0;
509                 this.dmg_team = 0;
510                 this.ballistics_density = autocvar_g_ballistics_density_player;
511
512                 this.deadflag = DEAD_NO;
513
514                 this.angles = spot.angles;
515                 this.angles_z = 0; // never spawn tilted even if the spot says to
516                 if (IS_BOT_CLIENT(this))
517                         this.v_angle = this.angles;
518                 this.fixangle = true; // turn this way immediately
519                 this.oldvelocity = this.velocity = '0 0 0';
520                 this.avelocity = '0 0 0';
521                 this.punchangle = '0 0 0';
522                 this.punchvector = '0 0 0';
523
524                 this.strength_finished = 0;
525                 this.invincible_finished = 0;
526                 this.fire_endtime = -1;
527                 this.revival_time = 0;
528                 this.air_finished = time + 12;
529
530                 entity spawnevent = new(spawnevent);
531                 make_pure(spawnevent);
532                 spawnevent.owner = this;
533                 Net_LinkEntity(spawnevent, false, 0.5, SpawnEvent_Send);
534
535                 // Cut off any still running player sounds.
536                 stopsound(this, CH_PLAYER_SINGLE);
537
538                 this.model = "";
539                 FixPlayermodel(this);
540                 this.drawonlytoclient = NULL;
541
542                 this.crouch = false;
543                 this.view_ofs = STAT(PL_VIEW_OFS, NULL);
544                 setsize(this, STAT(PL_MIN, NULL), STAT(PL_MAX, NULL));
545                 this.spawnorigin = spot.origin;
546                 setorigin(this, spot.origin + '0 0 1' * (1 - this.mins.z - 24));
547                 // don't reset back to last position, even if new position is stuck in solid
548                 this.oldorigin = this.origin;
549                 this.prevorigin = this.origin;
550                 this.lastteleporttime = time; // prevent insane speeds due to changing origin
551         this.hud = HUD_NORMAL;
552
553                 this.event_damage = PlayerDamage;
554
555                 this.bot_attack = true;
556                 this.monster_attack = true;
557
558                 PHYS_INPUT_BUTTON_ATCK(this) = PHYS_INPUT_BUTTON_JUMP(this) = PHYS_INPUT_BUTTON_ATCK2(this) = false;
559
560                 if (this.killcount == FRAGS_SPECTATOR) {
561                         PlayerScore_Clear(this);
562                         this.killcount = 0;
563                 }
564
565                 for (int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
566                 {
567                         CL_SpawnWeaponentity(this, weaponentities[slot]);
568                 }
569                 this.alpha = default_player_alpha;
570                 this.colormod = '1 1 1' * autocvar_g_player_brightness;
571                 this.exteriorweaponentity.alpha = default_weapon_alpha;
572
573                 this.speedrunning = false;
574
575                 target_voicescript_clear(this);
576
577                 // reset fields the weapons may use
578                 FOREACH(Weapons, true, LAMBDA(
579                         it.wr_resetplayer(it);
580                         // reload all reloadable weapons
581                         if (it.spawnflags & WEP_FLAG_RELOADABLE) {
582                                 this.weapon_load[it.m_id] = it.reloading_ammo;
583                         }
584                 ));
585
586                 {
587                         string s = spot.target;
588                         spot.target = string_null;
589                         WITH(entity, activator, this, LAMBDA(
590                                 WITH(entity, self, spot, SUB_UseTargets())
591                         ));
592                         spot.target = s;
593                 }
594
595                 Unfreeze(this);
596
597                 MUTATOR_CALLHOOK(PlayerSpawn, spot);
598
599                 if (autocvar_spawn_debug)
600                 {
601                         sprint(this, strcat("spawnpoint origin:  ", vtos(spot.origin), "\n"));
602                         remove(spot); // usefull for checking if there are spawnpoints, that let drop through the floor
603                 }
604
605                 PS(this).m_switchweapon = w_getbestweapon(this);
606                 this.cnt = -1; // W_LastWeapon will not complain
607                 PS(this).m_weapon = WEP_Null;
608                 this.weaponname = "";
609                 PS(this).m_switchingweapon = WEP_Null;
610
611                 if (!warmup_stage && !this.alivetime)
612                         this.alivetime = time;
613
614                 antilag_clear(this);
615         }
616 }
617
618 void ClientInit_misc();
619
620 .float ebouncefactor, ebouncestop; // electro's values
621 // TODO do we need all these fields, or should we stop autodetecting runtime
622 // changes and just have a console command to update this?
623 bool ClientInit_SendEntity(entity this, entity to, int sf)
624 {
625         WriteHeader(MSG_ENTITY, _ENT_CLIENT_INIT);
626         return = true;
627         msg_entity = to;
628         // MSG_INIT replacement
629         // TODO: make easier to use
630         Registry_send_all();
631         W_PROP_reload(MSG_ONE, to);
632         ClientInit_misc();
633         MUTATOR_CALLHOOK(Ent_Init);
634 }
635 void ClientInit_misc()
636 {
637         int channel = MSG_ONE;
638         WriteHeader(channel, ENT_CLIENT_INIT);
639         WriteByte(channel, g_nexball_meter_period * 32);
640         WriteInt24_t(channel, compressShotOrigin(hook_shotorigin[0]));
641         WriteInt24_t(channel, compressShotOrigin(hook_shotorigin[1]));
642         WriteInt24_t(channel, compressShotOrigin(hook_shotorigin[2]));
643         WriteInt24_t(channel, compressShotOrigin(hook_shotorigin[3]));
644         WriteInt24_t(channel, compressShotOrigin(arc_shotorigin[0]));
645         WriteInt24_t(channel, compressShotOrigin(arc_shotorigin[1]));
646         WriteInt24_t(channel, compressShotOrigin(arc_shotorigin[2]));
647         WriteInt24_t(channel, compressShotOrigin(arc_shotorigin[3]));
648
649         if(sv_foginterval && world.fog != "")
650                 WriteString(channel, world.fog);
651         else
652                 WriteString(channel, "");
653         WriteByte(channel, self.count * 255.0); // g_balance_armor_blockpercent
654         WriteByte(channel, serverflags); // client has to know if it should zoom or not
655         WriteCoord(channel, autocvar_g_trueaim_minrange);
656 }
657
658 void ClientInit_CheckUpdate()
659 {SELFPARAM();
660         self.nextthink = time;
661         if(self.count != autocvar_g_balance_armor_blockpercent)
662         {
663                 self.count = autocvar_g_balance_armor_blockpercent;
664                 self.SendFlags |= 1;
665         }
666 }
667
668 void ClientInit_Spawn()
669 {SELFPARAM();
670
671         entity e = new(clientinit);
672         make_pure(e);
673         e.think = ClientInit_CheckUpdate;
674         Net_LinkEntity(e, false, 0, ClientInit_SendEntity);
675
676         WITH(entity, self, e, ClientInit_CheckUpdate());
677 }
678
679 /*
680 =============
681 SetNewParms
682 =============
683 */
684 void SetNewParms ()
685 {
686         // initialize parms for a new player
687         parm1 = -(86400 * 366);
688
689         MUTATOR_CALLHOOK(SetNewParms);
690 }
691
692 /*
693 =============
694 SetChangeParms
695 =============
696 */
697 void SetChangeParms ()
698 {SELFPARAM();
699         // save parms for level change
700         parm1 = self.parm_idlesince - time;
701
702         MUTATOR_CALLHOOK(SetChangeParms);
703 }
704
705 /*
706 =============
707 DecodeLevelParms
708 =============
709 */
710 void DecodeLevelParms(entity this)
711 {
712         // load parms
713         this.parm_idlesince = parm1;
714         if (this.parm_idlesince == -(86400 * 366))
715                 this.parm_idlesince = time;
716
717         // whatever happens, allow 60 seconds of idling directly after connect for map loading
718         this.parm_idlesince = max(this.parm_idlesince, time - sv_maxidle + 60);
719
720         MUTATOR_CALLHOOK(DecodeLevelParms);
721 }
722
723 /*
724 =============
725 ClientKill
726
727 Called when a client types 'kill' in the console
728 =============
729 */
730
731 .float clientkill_nexttime;
732 void ClientKill_Now_TeamChange(entity this)
733 {
734         if(this.killindicator_teamchange == -1)
735         {
736                 JoinBestTeam( this, false, true );
737         }
738         else if(this.killindicator_teamchange == -2)
739         {
740                 if(blockSpectators)
741                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_SPECTATE_WARNING, autocvar_g_maxplayers_spectator_blocktime);
742                 WITH(entity, self, this, PutObserverInServer());
743         }
744         else
745                 WITH(entity, self, this, SV_ChangeTeam(this.killindicator_teamchange - 1));
746         this.killindicator_teamchange = 0;
747 }
748
749 void ClientKill_Now()
750 {SELFPARAM();
751         if(self.vehicle)
752         {
753             vehicles_exit(VHEF_RELEASE);
754             if(!self.killindicator_teamchange)
755             {
756             self.vehicle_health = -1;
757             Damage(self, self, self, 1 , DEATH_KILL.m_id, self.origin, '0 0 0');
758             }
759         }
760
761         if(self.killindicator && !wasfreed(self.killindicator))
762                 remove(self.killindicator);
763
764         self.killindicator = world;
765
766         if(self.killindicator_teamchange)
767                 ClientKill_Now_TeamChange(self);
768
769         if(IS_PLAYER(self))
770                 Damage(self, self, self, 100000, DEATH_KILL.m_id, self.origin, '0 0 0');
771
772         // now I am sure the player IS dead
773 }
774 void KillIndicator_Think()
775 {SELFPARAM();
776         if (gameover)
777         {
778                 self.owner.killindicator = world;
779                 remove(self);
780                 return;
781         }
782
783         if (self.owner.alpha < 0 && !self.owner.vehicle)
784         {
785                 self.owner.killindicator = world;
786                 remove(self);
787                 return;
788         }
789
790         if(self.cnt <= 0)
791         {
792                 WITH(entity, self, self.owner, ClientKill_Now());
793                 return;
794         }
795     else if(g_cts && self.health == 1) // health == 1 means that it's silent
796     {
797         self.nextthink = time + 1;
798         self.cnt -= 1;
799     }
800         else
801         {
802                 if(self.cnt <= 10)
803                         setmodel(self, MDL_NUM(self.cnt));
804                 if(IS_REAL_CLIENT(self.owner))
805                 {
806                         if(self.cnt <= 10)
807                                 { Send_Notification(NOTIF_ONE, self.owner, MSG_ANNCE, Announcer_PickNumber(CNT_KILL, self.cnt)); }
808                 }
809                 self.nextthink = time + 1;
810                 self.cnt -= 1;
811         }
812 }
813
814 float clientkilltime;
815 void ClientKill_TeamChange (float targetteam) // 0 = don't change, -1 = auto, -2 = spec
816 {SELFPARAM();
817         float killtime;
818         float starttime;
819         entity e;
820
821         if (gameover)
822                 return;
823
824         killtime = autocvar_g_balance_kill_delay;
825
826         if(g_race_qualifying || g_cts)
827                 killtime = 0;
828
829     if(MUTATOR_CALLHOOK(ClientKill, self, killtime))
830         return;
831
832         self.killindicator_teamchange = targetteam;
833
834     if(!self.killindicator)
835         {
836                 if(!IS_DEAD(self))
837                 {
838                         killtime = max(killtime, self.clientkill_nexttime - time);
839                         self.clientkill_nexttime = time + killtime + autocvar_g_balance_kill_antispam;
840                 }
841
842                 if(killtime <= 0 || !IS_PLAYER(self) || IS_DEAD(self))
843                 {
844                         ClientKill_Now();
845                 }
846                 else
847                 {
848                         starttime = max(time, clientkilltime);
849
850                         self.killindicator = spawn();
851                         self.killindicator.owner = self;
852                         self.killindicator.scale = 0.5;
853                         setattachment(self.killindicator, self, "");
854                         setorigin(self.killindicator, '0 0 52');
855                         self.killindicator.think = KillIndicator_Think;
856                         self.killindicator.nextthink = starttime + (self.lip) * 0.05;
857                         clientkilltime = max(clientkilltime, self.killindicator.nextthink + 0.05);
858                         self.killindicator.cnt = ceil(killtime);
859                         self.killindicator.count = bound(0, ceil(killtime), 10);
860                         //sprint(self, strcat("^1You'll be dead in ", ftos(self.killindicator.cnt), " seconds\n"));
861
862                         for(e = world; (e = find(e, classname, "body")) != world; )
863                         {
864                                 if(e.enemy != self)
865                                         continue;
866                                 e.killindicator = spawn();
867                                 e.killindicator.owner = e;
868                                 e.killindicator.scale = 0.5;
869                                 setattachment(e.killindicator, e, "");
870                                 setorigin(e.killindicator, '0 0 52');
871                                 e.killindicator.think = KillIndicator_Think;
872                                 e.killindicator.nextthink = starttime + (e.lip) * 0.05;
873                                 clientkilltime = max(clientkilltime, e.killindicator.nextthink + 0.05);
874                                 e.killindicator.cnt = ceil(killtime);
875                         }
876                         self.lip = 0;
877                 }
878         }
879         if(self.killindicator)
880         {
881                 if(targetteam == 0) // just die
882                 {
883                         self.killindicator.colormod = '0 0 0';
884                         if(IS_REAL_CLIENT(self))
885                         if(self.killindicator.cnt > 0)
886                                 Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_TEAMCHANGE_SUICIDE, self.killindicator.cnt);
887                 }
888                 else if(targetteam == -1) // auto
889                 {
890                         self.killindicator.colormod = '0 1 0';
891                         if(IS_REAL_CLIENT(self))
892                         if(self.killindicator.cnt > 0)
893                                 Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_TEAMCHANGE_AUTO, self.killindicator.cnt);
894                 }
895                 else if(targetteam == -2) // spectate
896                 {
897                         self.killindicator.colormod = '0.5 0.5 0.5';
898                         if(IS_REAL_CLIENT(self))
899                         if(self.killindicator.cnt > 0)
900                                 Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_TEAMCHANGE_SPECTATE, self.killindicator.cnt);
901                 }
902                 else
903                 {
904                         self.killindicator.colormod = Team_ColorRGB(targetteam);
905                         if(IS_REAL_CLIENT(self))
906                         if(self.killindicator.cnt > 0)
907                                 Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, APP_TEAM_NUM_4(targetteam, CENTER_TEAMCHANGE_), self.killindicator.cnt);
908                 }
909         }
910
911 }
912
913 void ClientKill ()
914 {SELFPARAM();
915         if(gameover) return;
916         if(self.player_blocked) return;
917         if(STAT(FROZEN, self)) return;
918
919         ClientKill_TeamChange(0);
920 }
921
922 void FixClientCvars(entity e)
923 {
924         // send prediction settings to the client
925         stuffcmd(e, "\nin_bindmap 0 0\n");
926         if(autocvar_g_antilag == 3) // client side hitscan
927                 stuffcmd(e, "cl_cmd settemp cl_prydoncursor_notrace 0\n");
928         if(autocvar_sv_gentle)
929                 stuffcmd(e, "cl_cmd settemp cl_gentle 1\n");
930
931         MUTATOR_CALLHOOK(FixClientCvars, e);
932 }
933
934 float PlayerInIDList(entity p, string idlist)
935 {
936         float n, i;
937         string s;
938
939         // NOTE: we do NOT check crypto_idfp_signed here, an unsigned ID is fine too for this
940         if (!p.crypto_idfp)
941                 return 0;
942
943         // this function allows abbreviated player IDs too!
944         n = tokenize_console(idlist);
945         for(i = 0; i < n; ++i)
946         {
947                 s = argv(i);
948                 if(s == substring(p.crypto_idfp, 0, strlen(s)))
949                         return 1;
950         }
951
952         return 0;
953 }
954
955 #ifdef DP_EXT_PRECONNECT
956 /*
957 =============
958 ClientPreConnect
959
960 Called once (not at each match start) when a client begins a connection to the server
961 =============
962 */
963 void ClientPreConnect ()
964 {SELFPARAM();
965         if(autocvar_sv_eventlog)
966         {
967                 GameLogEcho(sprintf(":connect:%d:%d:%s",
968                         self.playerid,
969                         etof(self),
970                         ((IS_REAL_CLIENT(self)) ? self.netaddress : "bot")
971                 ));
972         }
973 }
974 #endif
975
976 /**
977 =============
978 ClientConnect
979
980 Called when a client connects to the server
981 =============
982 */
983 void ClientConnect()
984 {
985         SELFPARAM();
986         if (Ban_MaybeEnforceBanOnce(this)) return;
987         assert(!IS_CLIENT(this), return);
988         assert(player_count >= 0, player_count = 0);
989         this.classname = "player_joining";
990         this.flags = FL_CLIENT;
991
992 #ifdef WATERMARK
993         Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_WATERMARK, WATERMARK);
994 #endif
995         this.version_nagtime = time + 10 + random() * 10;
996
997         ClientState_attach(this);
998
999         // identify the right forced team
1000         if (autocvar_g_campaign)
1001         {
1002                 if (IS_REAL_CLIENT(this)) // only players, not bots
1003                 {
1004                         switch (autocvar_g_campaign_forceteam)
1005                         {
1006                                 case 1: this.team_forced = NUM_TEAM_1; break;
1007                                 case 2: this.team_forced = NUM_TEAM_2; break;
1008                                 case 3: this.team_forced = NUM_TEAM_3; break;
1009                                 case 4: this.team_forced = NUM_TEAM_4; break;
1010                                 default: this.team_forced = 0;
1011                         }
1012                 }
1013         }
1014         else if (PlayerInIDList(this, autocvar_g_forced_team_red))    this.team_forced = NUM_TEAM_1;
1015         else if (PlayerInIDList(this, autocvar_g_forced_team_blue))   this.team_forced = NUM_TEAM_2;
1016         else if (PlayerInIDList(this, autocvar_g_forced_team_yellow)) this.team_forced = NUM_TEAM_3;
1017         else if (PlayerInIDList(this, autocvar_g_forced_team_pink))   this.team_forced = NUM_TEAM_4;
1018         else switch (autocvar_g_forced_team_otherwise)
1019         {
1020                 default: this.team_forced = 0; break;
1021                 case "red": this.team_forced = NUM_TEAM_1; break;
1022                 case "blue": this.team_forced = NUM_TEAM_2; break;
1023                 case "yellow": this.team_forced = NUM_TEAM_3; break;
1024                 case "pink": this.team_forced = NUM_TEAM_4; break;
1025                 case "spectate":
1026                 case "spectator":
1027                         this.team_forced = -1;
1028                         break;
1029         }
1030         if (!teamplay && this.team_forced > 0) this.team_forced = 0;
1031
1032         JoinBestTeam(this, false, false); // if the team number is valid, keep it
1033
1034         if (autocvar_sv_spectate || autocvar_g_campaign || this.team_forced < 0) {
1035                 this.classname = STR_OBSERVER;
1036         } else {
1037                 if (!teamplay || autocvar_g_balance_teams)
1038                 {
1039                         this.classname = STR_PLAYER;
1040                         campaign_bots_may_start = 1;
1041                 }
1042                 else
1043                 {
1044                         this.classname = STR_OBSERVER; // do it anyway
1045                 }
1046         }
1047
1048         this.playerid = ++playerid_last;
1049
1050         PlayerStats_GameReport_AddEvent(sprintf("kills-%d", this.playerid));
1051
1052         // always track bots, don't ask for cl_allow_uidtracking
1053     if (IS_BOT_CLIENT(this)) PlayerStats_GameReport_AddPlayer(this);
1054
1055         if (autocvar_sv_eventlog)
1056                 GameLogEcho(strcat(":join:", ftos(this.playerid), ":", ftos(etof(this)), ":", ((IS_REAL_CLIENT(this)) ? this.netaddress : "bot"), ":", this.netname));
1057
1058         LogTeamchange(this.playerid, this.team, 1);
1059
1060         this.just_joined = true;  // stop spamming the eventlog with additional lines when the client connects
1061
1062         this.netname_previous = strzone(this.netname);
1063
1064         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, ((teamplay && IS_PLAYER(this)) ? APP_TEAM_ENT_4(this, INFO_JOIN_CONNECT_TEAM_) : INFO_JOIN_CONNECT), this.netname);
1065
1066         stuffcmd(this, clientstuff, "\n");
1067         stuffcmd(this, "cl_particles_reloadeffects\n"); // TODO do we still need this?
1068
1069         FixClientCvars(this);
1070
1071         // get version info from player
1072         stuffcmd(this, "cmd clientversion $gameversion\n");
1073
1074         // notify about available teams
1075         if (teamplay)
1076         {
1077                 CheckAllowedTeams(this);
1078                 int t = 0;
1079                 if (c1 >= 0) t |= BIT(0);
1080                 if (c2 >= 0) t |= BIT(1);
1081                 if (c3 >= 0) t |= BIT(2);
1082                 if (c4 >= 0) t |= BIT(3);
1083                 stuffcmd(this, sprintf("set _teams_available %d\n", t));
1084         }
1085         else
1086         {
1087                 stuffcmd(this, "set _teams_available 0\n");
1088         }
1089
1090         bot_relinkplayerlist();
1091
1092         this.spectatortime = time;
1093         if (blockSpectators)
1094         {
1095                 Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_SPECTATE_WARNING, autocvar_g_maxplayers_spectator_blocktime);
1096         }
1097
1098         this.jointime = time;
1099         this.allowed_timeouts = autocvar_sv_timeout_number;
1100
1101         if (IS_REAL_CLIENT(this))
1102         {
1103                 if (!autocvar_g_campaign)
1104                 {
1105                         this.motd_actived_time = -1;
1106                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_MOTD, getwelcomemessage());
1107                 }
1108
1109                 if (g_weaponarena_weapons == WEPSET(TUBA))
1110                         stuffcmd(this, "cl_cmd settemp chase_active 1\n");
1111         }
1112
1113         if (!sv_foginterval && world.fog != "")
1114                 stuffcmd(this, strcat("\nfog ", world.fog, "\nr_fog_exp2 0\nr_drawfog 1\n"));
1115
1116         if (autocvar_sv_teamnagger && !(autocvar_bot_vs_human && (c3==-1 && c4==-1)))
1117                 if (!g_ca && !g_cts && !g_race) // teamnagger is currently bad for ca, race & cts
1118                         send_CSQC_teamnagger();
1119
1120         CSQCMODEL_AUTOINIT(this);
1121
1122         this.model_randomizer = random();
1123
1124         if (IS_REAL_CLIENT(this))
1125                 sv_notice_join(this);
1126
1127         FOREACH_ENTITY_FLOAT(init_for_player_needed, true, {
1128                 WITH(entity, self, it, it.init_for_player(it));
1129         });
1130
1131         MUTATOR_CALLHOOK(ClientConnect, this);
1132 }
1133 /*
1134 =============
1135 ClientDisconnect
1136
1137 Called when a client disconnects from the server
1138 =============
1139 */
1140 .entity chatbubbleentity;
1141 void ReadyCount();
1142 void ClientDisconnect()
1143 {
1144         SELFPARAM();
1145         assert(IS_CLIENT(this), return);
1146
1147         PlayerStats_GameReport_FinalizePlayer(this);
1148         if (this.vehicle) vehicles_exit(VHEF_RELEASE);
1149         if (this.active_minigame) part_minigame(this);
1150         if (IS_PLAYER(this)) Send_Effect(EFFECT_SPAWN_NEUTRAL, this.origin, '0 0 0', 1);
1151
1152         if (autocvar_sv_eventlog)
1153                 GameLogEcho(strcat(":part:", ftos(this.playerid)));
1154
1155         Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_QUIT_DISCONNECT, this.netname);
1156
1157     MUTATOR_CALLHOOK(ClientDisconnect);
1158
1159         ClientState_detach(this);
1160
1161         Portal_ClearAll(self);
1162
1163         Unfreeze(self);
1164
1165         RemoveGrapplingHook(self);
1166
1167         // Here, everything has been done that requires this player to be a client.
1168
1169         self.flags &= ~FL_CLIENT;
1170
1171         if (this.chatbubbleentity) remove(this.chatbubbleentity);
1172         if (this.killindicator) remove(this.killindicator);
1173
1174         WaypointSprite_PlayerGone();
1175
1176         bot_relinkplayerlist();
1177
1178         if (self.netname_previous) strunzone(self.netname_previous);
1179         if (self.clientstatus) strunzone(self.clientstatus);
1180         if (self.weaponorder_byimpulse) strunzone(self.weaponorder_byimpulse);
1181         if (self.personal) remove(self.personal);
1182
1183         this.playerid = 0;
1184         ReadyCount();
1185         if (vote_called && IS_REAL_CLIENT(this)) VoteCount(false);
1186 }
1187
1188 void ChatBubbleThink()
1189 {SELFPARAM();
1190         self.nextthink = time;
1191         if ((self.owner.alpha < 0) || self.owner.chatbubbleentity != self)
1192         {
1193                 if(self.owner) // but why can that ever be world?
1194                         self.owner.chatbubbleentity = world;
1195                 remove(self);
1196                 return;
1197         }
1198
1199         self.mdl = "";
1200
1201         if ( !IS_DEAD(self.owner) && IS_PLAYER(self.owner) )
1202         {
1203                 if ( self.owner.active_minigame )
1204                         self.mdl = "models/sprites/minigame_busy.iqm";
1205                 else if (PHYS_INPUT_BUTTON_CHAT(self.owner))
1206                         self.mdl = "models/misc/chatbubble.spr";
1207         }
1208
1209         if ( self.model != self.mdl )
1210                 _setmodel(self, self.mdl);
1211
1212 }
1213
1214 void UpdateChatBubble()
1215 {SELFPARAM();
1216         if (self.alpha < 0)
1217                 return;
1218         // spawn a chatbubble entity if needed
1219         if (!self.chatbubbleentity)
1220         {
1221                 self.chatbubbleentity = new(chatbubbleentity);
1222                 self.chatbubbleentity.owner = self;
1223                 self.chatbubbleentity.exteriormodeltoclient = self;
1224                 self.chatbubbleentity.think = ChatBubbleThink;
1225                 self.chatbubbleentity.nextthink = time;
1226                 setmodel(self.chatbubbleentity, MDL_CHAT); // precision set below
1227                 //setorigin(self.chatbubbleentity, self.origin + '0 0 15' + self.maxs_z * '0 0 1');
1228                 setorigin(self.chatbubbleentity, '0 0 15' + self.maxs_z * '0 0 1');
1229                 setattachment(self.chatbubbleentity, self, "");  // sticks to moving player better, also conserves bandwidth
1230                 self.chatbubbleentity.mdl = self.chatbubbleentity.model;
1231                 //self.chatbubbleentity.model = "";
1232                 self.chatbubbleentity.effects = EF_LOWPRECISION;
1233         }
1234 }
1235
1236
1237 // LordHavoc: this hack will be removed when proper _pants/_shirt layers are
1238 // added to the model skins
1239 /*void UpdateColorModHack()
1240 {
1241         float c;
1242         c = self.clientcolors & 15;
1243         // LordHavoc: only bothering to support white, green, red, yellow, blue
1244              if (!teamplay) self.colormod = '0 0 0';
1245         else if (c ==  0) self.colormod = '1.00 1.00 1.00';
1246         else if (c ==  3) self.colormod = '0.10 1.73 0.10';
1247         else if (c ==  4) self.colormod = '1.73 0.10 0.10';
1248         else if (c == 12) self.colormod = '1.22 1.22 0.10';
1249         else if (c == 13) self.colormod = '0.10 0.10 1.73';
1250         else self.colormod = '1 1 1';
1251 }*/
1252
1253 void respawn()
1254 {SELFPARAM();
1255         if(self.alpha >= 0 && autocvar_g_respawn_ghosts)
1256         {
1257                 self.solid = SOLID_NOT;
1258                 self.takedamage = DAMAGE_NO;
1259                 self.movetype = MOVETYPE_FLY;
1260                 self.velocity = '0 0 1' * autocvar_g_respawn_ghosts_speed;
1261                 self.avelocity = randomvec() * autocvar_g_respawn_ghosts_speed * 3 - randomvec() * autocvar_g_respawn_ghosts_speed * 3;
1262                 self.effects |= CSQCMODEL_EF_RESPAWNGHOST;
1263                 Send_Effect(EFFECT_RESPAWN_GHOST, self.origin, '0 0 0', 1);
1264                 if(autocvar_g_respawn_ghosts_maxtime)
1265                         SUB_SetFade (self, time + autocvar_g_respawn_ghosts_maxtime / 2 + random () * (autocvar_g_respawn_ghosts_maxtime - autocvar_g_respawn_ghosts_maxtime / 2), 1.5);
1266         }
1267
1268         CopyBody(self, 1);
1269
1270         self.effects |= EF_NODRAW; // prevent another CopyBody
1271         PutClientInServer();
1272 }
1273
1274 void play_countdown(float finished, string samp)
1275 {SELFPARAM();
1276         if(IS_REAL_CLIENT(self))
1277                 if(floor(finished - time - frametime) != floor(finished - time))
1278                         if(finished - time < 6)
1279                                 _sound (self, CH_INFO, samp, VOL_BASE, ATTEN_NORM);
1280 }
1281
1282 void player_powerups ()
1283 {SELFPARAM();
1284         // add a way to see what the items were BEFORE all of these checks for the mutator hook
1285         int items_prev = self.items;
1286
1287         if((self.items & IT_USING_JETPACK) && !IS_DEAD(self) && !gameover)
1288                 self.modelflags |= MF_ROCKET;
1289         else
1290                 self.modelflags &= ~MF_ROCKET;
1291
1292         self.effects &= ~(EF_RED | EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT | EF_FLAME | EF_NODEPTHTEST);
1293
1294         if((self.alpha < 0 || IS_DEAD(self)) && !self.vehicle) // don't apply the flags if the player is gibbed
1295                 return;
1296
1297         Fire_ApplyDamage(self);
1298         Fire_ApplyEffect(self);
1299
1300         if (!g_instagib)
1301         {
1302                 if (self.items & ITEM_Strength.m_itemid)
1303                 {
1304                         play_countdown(self.strength_finished, SND(POWEROFF));
1305                         self.effects = self.effects | (EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT);
1306                         if (time > self.strength_finished)
1307                         {
1308                                 self.items = self.items - (self.items & ITEM_Strength.m_itemid);
1309                                 //Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_POWERDOWN_STRENGTH, self.netname);
1310                                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_POWERDOWN_STRENGTH);
1311                         }
1312                 }
1313                 else
1314                 {
1315                         if (time < self.strength_finished)
1316                         {
1317                                 self.items = self.items | ITEM_Strength.m_itemid;
1318                                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_POWERUP_STRENGTH, self.netname);
1319                                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_POWERUP_STRENGTH);
1320                         }
1321                 }
1322                 if (self.items & ITEM_Shield.m_itemid)
1323                 {
1324                         play_countdown(self.invincible_finished, SND(POWEROFF));
1325                         self.effects = self.effects | (EF_RED | EF_ADDITIVE | EF_FULLBRIGHT);
1326                         if (time > self.invincible_finished)
1327                         {
1328                                 self.items = self.items - (self.items & ITEM_Shield.m_itemid);
1329                                 //Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_POWERDOWN_SHIELD, self.netname);
1330                                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_POWERDOWN_SHIELD);
1331                         }
1332                 }
1333                 else
1334                 {
1335                         if (time < self.invincible_finished)
1336                         {
1337                                 self.items = self.items | ITEM_Shield.m_itemid;
1338                                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_POWERUP_SHIELD, self.netname);
1339                                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_POWERUP_SHIELD);
1340                         }
1341                 }
1342                 if (self.items & IT_SUPERWEAPON)
1343                 {
1344                         if (!(self.weapons & WEPSET_SUPERWEAPONS))
1345                         {
1346                                 self.superweapons_finished = 0;
1347                                 self.items = self.items - (self.items & IT_SUPERWEAPON);
1348                                 //Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_SUPERWEAPON_LOST, self.netname);
1349                                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_SUPERWEAPON_LOST);
1350                         }
1351                         else if (self.items & IT_UNLIMITED_SUPERWEAPONS)
1352                         {
1353                                 // don't let them run out
1354                         }
1355                         else
1356                         {
1357                                 play_countdown(self.superweapons_finished, SND(POWEROFF));
1358                                 if (time > self.superweapons_finished)
1359                                 {
1360                                         self.items = self.items - (self.items & IT_SUPERWEAPON);
1361                                         self.weapons &= ~WEPSET_SUPERWEAPONS;
1362                                         //Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_SUPERWEAPON_BROKEN, self.netname);
1363                                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_SUPERWEAPON_BROKEN);
1364                                 }
1365                         }
1366                 }
1367                 else if(self.weapons & WEPSET_SUPERWEAPONS)
1368                 {
1369                         if (time < self.superweapons_finished || (self.items & IT_UNLIMITED_SUPERWEAPONS))
1370                         {
1371                                 self.items = self.items | IT_SUPERWEAPON;
1372                                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_SUPERWEAPON_PICKUP, self.netname);
1373                                 Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_SUPERWEAPON_PICKUP);
1374                         }
1375                         else
1376                         {
1377                                 self.superweapons_finished = 0;
1378                                 self.weapons &= ~WEPSET_SUPERWEAPONS;
1379                         }
1380                 }
1381                 else
1382                 {
1383                         self.superweapons_finished = 0;
1384                 }
1385         }
1386
1387         if(autocvar_g_nodepthtestplayers)
1388                 self.effects = self.effects | EF_NODEPTHTEST;
1389
1390         if(autocvar_g_fullbrightplayers)
1391                 self.effects = self.effects | EF_FULLBRIGHT;
1392
1393         if (time >= game_starttime)
1394         if (time < self.spawnshieldtime)
1395                 self.effects = self.effects | (EF_ADDITIVE | EF_FULLBRIGHT);
1396
1397         MUTATOR_CALLHOOK(PlayerPowerups, self, items_prev);
1398 }
1399
1400 float CalcRegen(float current, float stable, float regenfactor, float regenframetime)
1401 {
1402         if(current > stable)
1403                 return current;
1404         else if(current > stable - 0.25) // when close enough, "snap"
1405                 return stable;
1406         else
1407                 return min(stable, current + (stable - current) * regenfactor * regenframetime);
1408 }
1409
1410 float CalcRot(float current, float stable, float rotfactor, float rotframetime)
1411 {
1412         if(current < stable)
1413                 return current;
1414         else if(current < stable + 0.25) // when close enough, "snap"
1415                 return stable;
1416         else
1417                 return max(stable, current + (stable - current) * rotfactor * rotframetime);
1418 }
1419
1420 float CalcRotRegen(float current, float regenstable, float regenfactor, float regenlinear, float regenframetime, float rotstable, float rotfactor, float rotlinear, float rotframetime, float limit)
1421 {
1422         if(current > rotstable)
1423         {
1424                 if(rotframetime > 0)
1425                 {
1426                         current = CalcRot(current, rotstable, rotfactor, rotframetime);
1427                         current = max(rotstable, current - rotlinear * rotframetime);
1428                 }
1429         }
1430         else if(current < regenstable)
1431         {
1432                 if(regenframetime > 0)
1433                 {
1434                         current = CalcRegen(current, regenstable, regenfactor, regenframetime);
1435                         current = min(regenstable, current + regenlinear * regenframetime);
1436                 }
1437         }
1438
1439         if(current > limit)
1440                 current = limit;
1441
1442         return current;
1443 }
1444
1445 void player_regen ()
1446 {SELFPARAM();
1447         float max_mod, regen_mod, rot_mod, limit_mod;
1448         max_mod = regen_mod = rot_mod = limit_mod = 1;
1449         regen_mod_max = max_mod;
1450         regen_mod_regen = regen_mod;
1451         regen_mod_rot = rot_mod;
1452         regen_mod_limit = limit_mod;
1453
1454         regen_health = autocvar_g_balance_health_regen;
1455         regen_health_linear = autocvar_g_balance_health_regenlinear;
1456         regen_health_rot = autocvar_g_balance_health_rot;
1457         regen_health_rotlinear = autocvar_g_balance_health_rotlinear;
1458         regen_health_stable = autocvar_g_balance_health_regenstable;
1459         regen_health_rotstable = autocvar_g_balance_health_rotstable;
1460         if(!MUTATOR_CALLHOOK(PlayerRegen))
1461         if(!STAT(FROZEN, self))
1462         {
1463                 float mina, maxa, limith, limita;
1464                 maxa = autocvar_g_balance_armor_rotstable;
1465                 mina = autocvar_g_balance_armor_regenstable;
1466                 limith = autocvar_g_balance_health_limit;
1467                 limita = autocvar_g_balance_armor_limit;
1468
1469                 max_mod = regen_mod_max;
1470                 regen_mod = regen_mod_regen;
1471                 rot_mod = regen_mod_rot;
1472                 limit_mod = regen_mod_limit;
1473
1474                 regen_health_rotstable = regen_health_rotstable * max_mod;
1475                 regen_health_stable = regen_health_stable * max_mod;
1476                 limith = limith * limit_mod;
1477                 limita = limita * limit_mod;
1478
1479                 self.armorvalue = CalcRotRegen(self.armorvalue, mina, autocvar_g_balance_armor_regen, autocvar_g_balance_armor_regenlinear, regen_mod * frametime * (time > self.pauseregen_finished), maxa, autocvar_g_balance_armor_rot, autocvar_g_balance_armor_rotlinear, rot_mod * frametime * (time > self.pauserotarmor_finished), limita);
1480                 self.health = CalcRotRegen(self.health, regen_health_stable, regen_health, regen_health_linear, regen_mod * frametime * (time > self.pauseregen_finished), regen_health_rotstable, regen_health_rot, regen_health_rotlinear, rot_mod * frametime * (time > self.pauserothealth_finished), limith);
1481         }
1482
1483         // if player rotted to death...  die!
1484         // check this outside above checks, as player may still be able to rot to death
1485         if(self.health < 1)
1486         {
1487                 if(self.vehicle)
1488                         vehicles_exit(VHEF_RELEASE);
1489                 if(self.event_damage)
1490                         self.event_damage(self, self, self, 1, DEATH_ROT.m_id, self.origin, '0 0 0');
1491         }
1492
1493         if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
1494         {
1495                 float minf, maxf, limitf;
1496
1497                 maxf = autocvar_g_balance_fuel_rotstable;
1498                 minf = autocvar_g_balance_fuel_regenstable;
1499                 limitf = autocvar_g_balance_fuel_limit;
1500
1501                 self.ammo_fuel = CalcRotRegen(self.ammo_fuel, minf, autocvar_g_balance_fuel_regen, autocvar_g_balance_fuel_regenlinear, frametime * (time > self.pauseregen_finished) * ((self.items & ITEM_JetpackRegen.m_itemid) != 0), maxf, autocvar_g_balance_fuel_rot, autocvar_g_balance_fuel_rotlinear, frametime * (time > self.pauserotfuel_finished), limitf);
1502         }
1503 }
1504
1505 float zoomstate_set;
1506 void SetZoomState(float z)
1507 {SELFPARAM();
1508         if(z != self.zoomstate)
1509         {
1510                 self.zoomstate = z;
1511                 ClientData_Touch(self);
1512         }
1513         zoomstate_set = 1;
1514 }
1515
1516 void GetPressedKeys()
1517 {
1518         SELFPARAM();
1519         MUTATOR_CALLHOOK(GetPressedKeys);
1520         int keys = this.pressedkeys;
1521         keys = BITSET(keys, KEY_FORWARD,        this.movement.x > 0);
1522         keys = BITSET(keys, KEY_BACKWARD,       this.movement.x < 0);
1523         keys = BITSET(keys, KEY_RIGHT,          this.movement.y > 0);
1524         keys = BITSET(keys, KEY_LEFT,           this.movement.y < 0);
1525
1526         keys = BITSET(keys, KEY_JUMP,           PHYS_INPUT_BUTTON_JUMP(this));
1527         keys = BITSET(keys, KEY_CROUCH,         PHYS_INPUT_BUTTON_CROUCH(this));
1528         keys = BITSET(keys, KEY_ATCK,           PHYS_INPUT_BUTTON_ATCK(this));
1529         keys = BITSET(keys, KEY_ATCK2,          PHYS_INPUT_BUTTON_ATCK2(this));
1530         this.pressedkeys = keys;
1531 }
1532
1533 /*
1534 ======================
1535 spectate mode routines
1536 ======================
1537 */
1538
1539 void SpectateCopy(entity this, entity spectatee)
1540 {
1541         MUTATOR_CALLHOOK(SpectateCopy, spectatee, self);
1542         self.armortype = spectatee.armortype;
1543         self.armorvalue = spectatee.armorvalue;
1544         self.ammo_cells = spectatee.ammo_cells;
1545         self.ammo_plasma = spectatee.ammo_plasma;
1546         self.ammo_shells = spectatee.ammo_shells;
1547         self.ammo_nails = spectatee.ammo_nails;
1548         self.ammo_rockets = spectatee.ammo_rockets;
1549         self.ammo_fuel = spectatee.ammo_fuel;
1550         self.clip_load = spectatee.clip_load;
1551         self.clip_size = spectatee.clip_size;
1552         self.effects = spectatee.effects & EFMASK_CHEAP; // eat performance
1553         self.health = spectatee.health;
1554         self.impulse = 0;
1555         self.items = spectatee.items;
1556         self.last_pickup = spectatee.last_pickup;
1557         self.hit_time = spectatee.hit_time;
1558         self.strength_finished = spectatee.strength_finished;
1559         self.invincible_finished = spectatee.invincible_finished;
1560         self.pressedkeys = spectatee.pressedkeys;
1561         self.weapons = spectatee.weapons;
1562         PS(self).m_switchweapon = PS(spectatee).m_switchweapon;
1563         PS(self).m_switchingweapon = PS(spectatee).m_switchingweapon;
1564         PS(self).m_weapon = PS(spectatee).m_weapon;
1565         self.vortex_charge = spectatee.vortex_charge;
1566         self.vortex_chargepool_ammo = spectatee.vortex_chargepool_ammo;
1567         self.hagar_load = spectatee.hagar_load;
1568         self.arc_heat_percent = spectatee.arc_heat_percent;
1569         self.minelayer_mines = spectatee.minelayer_mines;
1570         self.punchangle = spectatee.punchangle;
1571         self.view_ofs = spectatee.view_ofs;
1572         self.velocity = spectatee.velocity;
1573         self.dmg_take = spectatee.dmg_take;
1574         self.dmg_save = spectatee.dmg_save;
1575         self.dmg_inflictor = spectatee.dmg_inflictor;
1576         self.v_angle = spectatee.v_angle;
1577         self.angles = spectatee.v_angle;
1578         STAT(FROZEN, self) = STAT(FROZEN, spectatee);
1579         self.revive_progress = spectatee.revive_progress;
1580         if(!PHYS_INPUT_BUTTON_USE(self))
1581                 self.fixangle = true;
1582         setorigin(self, spectatee.origin);
1583         setsize(self, spectatee.mins, spectatee.maxs);
1584         SetZoomState(spectatee.zoomstate);
1585
1586     anticheat_spectatecopy(spectatee);
1587         self.hud = spectatee.hud;
1588         if(spectatee.vehicle)
1589     {
1590         self.fixangle = false;
1591         //self.velocity = spectatee.vehicle.velocity;
1592         self.vehicle_health = spectatee.vehicle_health;
1593         self.vehicle_shield = spectatee.vehicle_shield;
1594         self.vehicle_energy = spectatee.vehicle_energy;
1595         self.vehicle_ammo1 = spectatee.vehicle_ammo1;
1596         self.vehicle_ammo2 = spectatee.vehicle_ammo2;
1597         self.vehicle_reload1 = spectatee.vehicle_reload1;
1598         self.vehicle_reload2 = spectatee.vehicle_reload2;
1599
1600         msg_entity = self;
1601
1602         WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1603             WriteAngle(MSG_ONE,  spectatee.v_angle.x);
1604             WriteAngle(MSG_ONE,  spectatee.v_angle.y);
1605             WriteAngle(MSG_ONE,  spectatee.v_angle.z);
1606
1607         //WriteByte (MSG_ONE, SVC_SETVIEW);
1608         //    WriteEntity(MSG_ONE, self);
1609         //makevectors(spectatee.v_angle);
1610         //setorigin(self, spectatee.origin - v_forward * 400 + v_up * 300);*/
1611     }
1612 }
1613
1614 bool SpectateUpdate()
1615 {SELFPARAM();
1616         if(!self.enemy)
1617             return false;
1618
1619         if(!IS_PLAYER(self.enemy) || self == self.enemy)
1620         {
1621                 SetSpectatee(self, NULL);
1622                 return false;
1623         }
1624
1625         SpectateCopy(this, this.enemy);
1626
1627         return true;
1628 }
1629
1630 bool SpectateSet()
1631 {SELFPARAM();
1632         if(!IS_PLAYER(self.enemy))
1633                 return false;
1634
1635         msg_entity = self;
1636         WriteByte(MSG_ONE, SVC_SETVIEW);
1637         WriteEntity(MSG_ONE, self.enemy);
1638         self.movetype = MOVETYPE_NONE;
1639         accuracy_resend(self);
1640
1641         if(!SpectateUpdate())
1642                 PutObserverInServer();
1643
1644         return true;
1645 }
1646
1647 void SetSpectatee(entity player, entity spectatee)
1648 {
1649         entity old_spectatee = player.enemy;
1650
1651         player.enemy = spectatee;
1652
1653         // WEAPONTODO
1654         // these are required to fix the spectator bug with arc
1655         if(old_spectatee && old_spectatee.arc_beam) { old_spectatee.arc_beam.SendFlags |= ARC_SF_SETTINGS; }
1656         if(player.enemy && player.enemy.arc_beam) { player.enemy.arc_beam.SendFlags |= ARC_SF_SETTINGS; }
1657 }
1658
1659 bool Spectate(entity pl)
1660 {SELFPARAM();
1661         if(MUTATOR_CALLHOOK(SpectateSet, self, pl))
1662                 return false;
1663         pl = spec_player;
1664
1665         SetSpectatee(self, pl);
1666         return SpectateSet();
1667 }
1668
1669 bool SpectateNext()
1670 {SELFPARAM();
1671         other = find(self.enemy, classname, STR_PLAYER);
1672
1673         if (MUTATOR_CALLHOOK(SpectateNext, self, other))
1674                 other = spec_player;
1675         else if (!other)
1676                 other = find(other, classname, STR_PLAYER);
1677
1678         if(other) { SetSpectatee(self, other); }
1679
1680         return SpectateSet();
1681 }
1682
1683 bool SpectatePrev()
1684 {SELFPARAM();
1685         // NOTE: chain order is from the highest to the lower entnum (unlike find)
1686         other = findchain(classname, STR_PLAYER);
1687         if (!other) // no player
1688                 return false;
1689
1690         entity first = other;
1691         // skip players until current spectated player
1692         if(self.enemy)
1693         while(other && other != self.enemy)
1694                 other = other.chain;
1695
1696         switch (MUTATOR_CALLHOOK(SpectatePrev, self, other, first))
1697         {
1698                 case MUT_SPECPREV_FOUND:
1699                     other = spec_player;
1700                     break;
1701                 case MUT_SPECPREV_RETURN:
1702                     other = spec_player;
1703                     return true;
1704                 case MUT_SPECPREV_CONTINUE:
1705                 default:
1706                 {
1707                         if(other.chain)
1708                                 other = other.chain;
1709                         else
1710                                 other = first;
1711                         break;
1712                 }
1713         }
1714
1715         SetSpectatee(self, other);
1716         return SpectateSet();
1717 }
1718
1719 /*
1720 =============
1721 ShowRespawnCountdown()
1722
1723 Update a respawn countdown display.
1724 =============
1725 */
1726 void ShowRespawnCountdown()
1727 {SELFPARAM();
1728         float number;
1729         if(!IS_DEAD(self)) // just respawned?
1730                 return;
1731         else
1732         {
1733                 number = ceil(self.respawn_time - time);
1734                 if(number <= 0)
1735                         return;
1736                 if(number <= self.respawn_countdown)
1737                 {
1738                         self.respawn_countdown = number - 1;
1739                         if(ceil(self.respawn_time - (time + 0.5)) == number) // only say it if it is the same number even in 0.5s; to prevent overlapping sounds
1740                                 { Send_Notification(NOTIF_ONE, self, MSG_ANNCE, Announcer_PickNumber(CNT_RESPAWN, number)); }
1741                 }
1742         }
1743 }
1744
1745 void LeaveSpectatorMode()
1746 {SELFPARAM();
1747         if(self.caplayer)
1748                 return;
1749         if(nJoinAllowed(self, self))
1750         {
1751                 if(!teamplay || autocvar_g_campaign || autocvar_g_balance_teams || (self.wasplayer && autocvar_g_changeteam_banned) || self.team_forced > 0)
1752                 {
1753                         self.classname = STR_PLAYER;
1754
1755                         if(autocvar_g_campaign || autocvar_g_balance_teams)
1756                                 { JoinBestTeam(self, false, true); }
1757
1758                         if(autocvar_g_campaign)
1759                                 { campaign_bots_may_start = 1; }
1760
1761                         Kill_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER_CPID, CPID_PREVENT_JOIN);
1762
1763                         PutClientInServer();
1764
1765                         if(IS_PLAYER(self)) { Send_Notification(NOTIF_ALL, world, MSG_INFO, ((teamplay && this.team != -1) ? APP_TEAM_ENT_4(this, INFO_JOIN_PLAY_TEAM_) : INFO_JOIN_PLAY), self.netname); }
1766                 }
1767                 else
1768                         stuffcmd(self, "menu_showteamselect\n");
1769         }
1770         else
1771         {
1772                 // Player may not join because g_maxplayers is set
1773                 Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_JOIN_PREVENT);
1774         }
1775 }
1776
1777 /**
1778  * Determines whether the player is allowed to join. This depends on cvar
1779  * g_maxplayers, if it isn't used this function always return true, otherwise
1780  * it checks whether the number of currently playing players exceeds g_maxplayers.
1781  * @return int number of free slots for players, 0 if none
1782  */
1783 bool nJoinAllowed(entity this, entity ignore)
1784 {
1785         if(!ignore)
1786         // this is called that way when checking if anyone may be able to join (to build qcstatus)
1787         // so report 0 free slots if restricted
1788         {
1789                 if(autocvar_g_forced_team_otherwise == "spectate")
1790                         return false;
1791                 if(autocvar_g_forced_team_otherwise == "spectator")
1792                         return false;
1793         }
1794
1795         if(this.team_forced < 0)
1796                 return false; // forced spectators can never join
1797
1798         // TODO simplify this
1799         int totalClients = 0;
1800         int currentlyPlaying = 0;
1801         FOREACH_CLIENT(true, LAMBDA(
1802                 if(it != ignore)
1803                         ++totalClients;
1804                 if(IS_REAL_CLIENT(it))
1805                 if(IS_PLAYER(it) || it.caplayer)
1806                         ++currentlyPlaying;
1807         ));
1808
1809         if (!autocvar_g_maxplayers)
1810                 return maxclients - totalClients;
1811
1812         if(currentlyPlaying < autocvar_g_maxplayers)
1813                 return min(maxclients - totalClients, autocvar_g_maxplayers - currentlyPlaying);
1814
1815         return false;
1816 }
1817
1818 /**
1819  * Checks whether the client is an observer or spectator, if so, he will get kicked after
1820  * g_maxplayers_spectator_blocktime seconds
1821  */
1822 void checkSpectatorBlock()
1823 {SELFPARAM();
1824         if(IS_SPEC(self) || IS_OBSERVER(self))
1825         if(!self.caplayer)
1826         if(IS_REAL_CLIENT(self))
1827         {
1828                 if( time > (self.spectatortime + autocvar_g_maxplayers_spectator_blocktime) ) {
1829                         Send_Notification(NOTIF_ONE_ONLY, self, MSG_INFO, INFO_QUIT_KICK_SPECTATING);
1830                         dropclient(self);
1831                 }
1832         }
1833 }
1834
1835 void PrintWelcomeMessage()
1836 {SELFPARAM();
1837         if(self.motd_actived_time == 0)
1838         {
1839                 if (autocvar_g_campaign) {
1840                         if ((IS_PLAYER(self) && PHYS_INPUT_BUTTON_INFO(self)) || (!IS_PLAYER(self))) {
1841                                 self.motd_actived_time = time;
1842                                 Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_MOTD, campaign_message);
1843                         }
1844                 } else {
1845                         if (PHYS_INPUT_BUTTON_INFO(self)) {
1846                                 self.motd_actived_time = time;
1847                                 Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_MOTD, getwelcomemessage());
1848                         }
1849                 }
1850         }
1851         else if(self.motd_actived_time > 0) // showing MOTD or campaign message
1852         {
1853                 if (autocvar_g_campaign) {
1854                         if (PHYS_INPUT_BUTTON_INFO(self))
1855                                 self.motd_actived_time = time;
1856                         else if ((time - self.motd_actived_time > 2) && IS_PLAYER(self)) { // hide it some seconds after BUTTON_INFO has been released
1857                                 self.motd_actived_time = 0;
1858                                 Kill_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER_CPID, CPID_MOTD);
1859                         }
1860                 } else {
1861                         if (PHYS_INPUT_BUTTON_INFO(self))
1862                                 self.motd_actived_time = time;
1863                         else if (time - self.motd_actived_time > 2) { // hide it some seconds after BUTTON_INFO has been released
1864                                 self.motd_actived_time = 0;
1865                                 Kill_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER_CPID, CPID_MOTD);
1866                         }
1867                 }
1868         }
1869         else //if(self.motd_actived_time < 0) // just connected, motd is active
1870         {
1871                 if(PHYS_INPUT_BUTTON_INFO(self)) // BUTTON_INFO hides initial MOTD
1872                         self.motd_actived_time = -2; // wait until BUTTON_INFO gets released
1873                 else if(self.motd_actived_time == -2 || IS_PLAYER(self) || IS_SPEC(self))
1874                 {
1875                         // instanctly hide MOTD
1876                         self.motd_actived_time = 0;
1877                         Kill_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER_CPID, CPID_MOTD);
1878                 }
1879         }
1880 }
1881
1882 void ObserverThink()
1883 {SELFPARAM();
1884         if ( self.impulse )
1885         {
1886                 MinigameImpulse(self, self.impulse);
1887                 self.impulse = 0;
1888         }
1889         float prefered_movetype;
1890         if (self.flags & FL_JUMPRELEASED) {
1891                 if (PHYS_INPUT_BUTTON_JUMP(self) && !self.version_mismatch) {
1892                         self.flags &= ~FL_JUMPRELEASED;
1893                         self.flags |= FL_SPAWNING;
1894                 } else if(PHYS_INPUT_BUTTON_ATCK(self) && !self.version_mismatch) {
1895                         self.flags &= ~FL_JUMPRELEASED;
1896                         if(SpectateNext()) {
1897                                 self.classname = STR_SPECTATOR;
1898                         }
1899                 } else {
1900                         prefered_movetype = ((!PHYS_INPUT_BUTTON_USE(self) ? self.cvar_cl_clippedspectating : !self.cvar_cl_clippedspectating) ? MOVETYPE_FLY_WORLDONLY : MOVETYPE_NOCLIP);
1901                         if (self.movetype != prefered_movetype)
1902                                 self.movetype = prefered_movetype;
1903                 }
1904         } else {
1905                 if (!(PHYS_INPUT_BUTTON_ATCK(self) || PHYS_INPUT_BUTTON_JUMP(self))) {
1906                         self.flags |= FL_JUMPRELEASED;
1907                         if(self.flags & FL_SPAWNING)
1908                         {
1909                                 self.flags &= ~FL_SPAWNING;
1910                                 LeaveSpectatorMode();
1911                                 return;
1912                         }
1913                 }
1914         }
1915 }
1916
1917 void SpectatorThink()
1918 {SELFPARAM();
1919         if ( self.impulse )
1920         {
1921                 if(MinigameImpulse(self, self.impulse))
1922                         self.impulse = 0;
1923         }
1924         if (self.flags & FL_JUMPRELEASED) {
1925                 if (PHYS_INPUT_BUTTON_JUMP(self) && !self.version_mismatch) {
1926                         self.flags &= ~FL_JUMPRELEASED;
1927                         self.flags |= FL_SPAWNING;
1928                 } else if(PHYS_INPUT_BUTTON_ATCK(self) || self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209)) {
1929                         self.flags &= ~FL_JUMPRELEASED;
1930                         if(SpectateNext()) {
1931                                 self.classname = STR_SPECTATOR;
1932                         } else {
1933                                 self.classname = STR_OBSERVER;
1934                                 PutClientInServer();
1935                         }
1936                         self.impulse = 0;
1937                 } else if(self.impulse == 12 || self.impulse == 16  || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229)) {
1938                         self.flags &= ~FL_JUMPRELEASED;
1939                         if(SpectatePrev()) {
1940                                 self.classname = STR_SPECTATOR;
1941                         } else {
1942                                 self.classname = STR_OBSERVER;
1943                                 PutClientInServer();
1944                         }
1945                         self.impulse = 0;
1946                 } else if (PHYS_INPUT_BUTTON_ATCK2(self)) {
1947                         self.flags &= ~FL_JUMPRELEASED;
1948                         self.classname = STR_OBSERVER;
1949                         PutClientInServer();
1950                 } else {
1951                         if(!SpectateUpdate())
1952                                 PutObserverInServer();
1953                 }
1954         } else {
1955                 if (!(PHYS_INPUT_BUTTON_ATCK(self) || PHYS_INPUT_BUTTON_ATCK2(self))) {
1956                         self.flags |= FL_JUMPRELEASED;
1957                         if(self.flags & FL_SPAWNING)
1958                         {
1959                                 self.flags &= ~FL_SPAWNING;
1960                                 LeaveSpectatorMode();
1961                                 return;
1962                         }
1963                 }
1964                 if(!SpectateUpdate())
1965                         PutObserverInServer();
1966         }
1967
1968         self.flags |= FL_CLIENT | FL_NOTARGET;
1969 }
1970
1971 void vehicles_enter (entity pl, entity veh);
1972 void PlayerUseKey()
1973 {SELFPARAM();
1974         if (!IS_PLAYER(self))
1975                 return;
1976
1977         if(self.vehicle)
1978         {
1979                 if(!gameover)
1980                 {
1981                         vehicles_exit(VHEF_NORMAL);
1982                         return;
1983                 }
1984         }
1985         else if(autocvar_g_vehicles_enter)
1986         {
1987                 if(!STAT(FROZEN, self))
1988                 if(!IS_DEAD(self))
1989                 if(!gameover)
1990                 {
1991                         entity head, closest_target = world;
1992                         head = WarpZone_FindRadius(self.origin, autocvar_g_vehicles_enter_radius, true);
1993
1994                         while(head) // find the closest acceptable target to enter
1995                         {
1996                                 if(head.vehicle_flags & VHF_ISVEHICLE)
1997                                 if(!IS_DEAD(head))
1998                                 if(!head.owner || ((head.vehicle_flags & VHF_MULTISLOT) && SAME_TEAM(head.owner, self)))
1999                                 if(head.takedamage != DAMAGE_NO)
2000                                 {
2001                                         if(closest_target)
2002                                         {
2003                                                 if(vlen(self.origin - head.origin) < vlen(self.origin - closest_target.origin))
2004                                                 { closest_target = head; }
2005                                         }
2006                                         else { closest_target = head; }
2007                                 }
2008
2009                                 head = head.chain;
2010                         }
2011
2012                         if(closest_target) { vehicles_enter(self, closest_target); return; }
2013                 }
2014         }
2015
2016         // a use key was pressed; call handlers
2017         MUTATOR_CALLHOOK(PlayerUseKey);
2018 }
2019
2020
2021 /*
2022 =============
2023 PlayerPreThink
2024
2025 Called every frame for each client before the physics are run
2026 =============
2027 */
2028 .float usekeypressed;
2029 void() nexball_setstatus;
2030 .float last_vehiclecheck;
2031 .int items_added;
2032 void PlayerPreThink ()
2033 {SELFPARAM();
2034         WarpZone_PlayerPhysics_FixVAngle();
2035
2036         self.stat_game_starttime = game_starttime;
2037         self.stat_round_starttime = round_starttime;
2038         self.stat_allow_oldvortexbeam = autocvar_g_allow_oldvortexbeam;
2039         self.stat_leadlimit = autocvar_leadlimit;
2040
2041         self.weaponsinmap = weaponsInMap;
2042
2043         if(frametime)
2044         {
2045                 // physics frames: update anticheat stuff
2046                 anticheat_prethink();
2047         }
2048
2049         if(blockSpectators && frametime)
2050                 // WORKAROUND: only use dropclient in server frames (frametime set). Never use it in cl_movement frames (frametime zero).
2051                 checkSpectatorBlock();
2052
2053         zoomstate_set = 0;
2054
2055         // Savage: Check for nameless players
2056         if (isInvisibleString(self.netname)) {
2057                 string new_name = strzone(strcat("Player@", ftos(self.playerid)));
2058                 if(autocvar_sv_eventlog)
2059                         GameLogEcho(strcat(":name:", ftos(self.playerid), ":", new_name));
2060                 if(self.netname_previous)
2061                         strunzone(self.netname_previous);
2062                 self.netname_previous = strzone(new_name);
2063                 self.netname = self.netname_previous;
2064                 // stuffcmd(self, strcat("name ", self.netname, "\n"));
2065         } else if(self.netname_previous != self.netname) {
2066                 if(autocvar_sv_eventlog)
2067                         GameLogEcho(strcat(":name:", ftos(self.playerid), ":", self.netname));
2068                 if(self.netname_previous)
2069                         strunzone(self.netname_previous);
2070                 self.netname_previous = strzone(self.netname);
2071         }
2072
2073         // version nagging
2074         if(self.version_nagtime)
2075                 if(self.cvar_g_xonoticversion)
2076                         if(time > self.version_nagtime)
2077                         {
2078                                 // don't notify git users
2079                                 if(strstr(self.cvar_g_xonoticversion, "git", 0) < 0 && strstr(self.cvar_g_xonoticversion, "autobuild", 0) < 0)
2080                                 {
2081                                         if(strstr(autocvar_g_xonoticversion, "git", 0) >= 0 || strstr(autocvar_g_xonoticversion, "autobuild", 0) >= 0)
2082                                         {
2083                                                 // notify release users if connecting to git
2084                                                 LOG_TRACE("^1NOTE^7 to ", self.netname, "^7 - the server is running ^3Xonotic ", autocvar_g_xonoticversion, " (beta)^7, you have ^3Xonotic ", self.cvar_g_xonoticversion, "^1\n");
2085                                                 Send_Notification(NOTIF_ONE_ONLY, self, MSG_INFO, INFO_VERSION_BETA, autocvar_g_xonoticversion, self.cvar_g_xonoticversion);
2086                                         }
2087                                         else
2088                                         {
2089                                                 float r;
2090                                                 r = vercmp(self.cvar_g_xonoticversion, autocvar_g_xonoticversion);
2091                                                 if(r < 0)
2092                                                 {
2093                                                         // give users new version
2094                                                         LOG_TRACE("^1NOTE^7 to ", self.netname, "^7 - ^3Xonotic ", autocvar_g_xonoticversion, "^7 is out, and you still have ^3Xonotic ", self.cvar_g_xonoticversion, "^1 - get the update from ^4http://www.xonotic.org/^1!\n");
2095                                                         Send_Notification(NOTIF_ONE_ONLY, self, MSG_INFO, INFO_VERSION_OUTDATED, autocvar_g_xonoticversion, self.cvar_g_xonoticversion);
2096                                                 }
2097                                                 else if(r > 0)
2098                                                 {
2099                                                         // notify users about old server version
2100                                                         LOG_INFO("^1NOTE^7 to ", self.netname, "^7 - the server is running ^3Xonotic ", autocvar_g_xonoticversion, "^7, you have ^3Xonotic ", self.cvar_g_xonoticversion, "^1\n");
2101                                                         Send_Notification(NOTIF_ONE_ONLY, self, MSG_INFO, INFO_VERSION_OLD, autocvar_g_xonoticversion, self.cvar_g_xonoticversion);
2102                                                 }
2103                                         }
2104                                 }
2105                                 self.version_nagtime = 0;
2106                         }
2107
2108         // GOD MODE info
2109         if(!(self.flags & FL_GODMODE)) if(self.max_armorvalue)
2110         {
2111                 Send_Notification(NOTIF_ONE_ONLY, self, MSG_INFO, INFO_GODMODE_OFF, self.max_armorvalue);
2112                 self.max_armorvalue = 0;
2113         }
2114
2115         if(STAT(FROZEN, self) == 2)
2116         {
2117                 self.revive_progress = bound(0, self.revive_progress + frametime * self.revive_speed, 1);
2118                 self.health = max(1, self.revive_progress * start_health);
2119                 self.iceblock.alpha = bound(0.2, 1 - self.revive_progress, 1);
2120
2121                 if(self.revive_progress >= 1)
2122                         Unfreeze(self);
2123         }
2124         else if(STAT(FROZEN, self) == 3)
2125         {
2126                 self.revive_progress = bound(0, self.revive_progress - frametime * self.revive_speed, 1);
2127                 self.health = max(0, autocvar_g_nades_ice_health + (start_health-autocvar_g_nades_ice_health) * self.revive_progress );
2128
2129                 if(self.health < 1)
2130                 {
2131                         if(self.vehicle)
2132                                 vehicles_exit(VHEF_RELEASE);
2133                         self.event_damage(self, self, self.frozen_by, 1, DEATH_NADE_ICE_FREEZE.m_id, self.origin, '0 0 0');
2134                 }
2135                 else if ( self.revive_progress <= 0 )
2136                         Unfreeze(self);
2137         }
2138
2139         MUTATOR_CALLHOOK(PlayerPreThink);
2140
2141         if(autocvar_g_vehicles_enter)
2142         if(time > self.last_vehiclecheck)
2143         if(IS_PLAYER(self))
2144         if(!gameover)
2145         if(!STAT(FROZEN, self))
2146         if(!self.vehicle)
2147         if(!IS_DEAD(self))
2148         {
2149                 entity veh;
2150                 for(veh = world; (veh = findflags(veh, vehicle_flags, VHF_ISVEHICLE)); )
2151                 if(vlen(veh.origin - self.origin) < autocvar_g_vehicles_enter_radius)
2152                 if(!IS_DEAD(veh))
2153                 if(veh.takedamage != DAMAGE_NO)
2154                 if((veh.vehicle_flags & VHF_MULTISLOT) && SAME_TEAM(veh.owner, self))
2155                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_VEHICLE_ENTER_GUNNER);
2156                 else if(!veh.owner)
2157                 if(!veh.team || SAME_TEAM(self, veh))
2158                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_VEHICLE_ENTER);
2159                 else if(autocvar_g_vehicles_steal)
2160                         Send_Notification(NOTIF_ONE, self, MSG_CENTER, CENTER_VEHICLE_ENTER_STEAL);
2161
2162                 self.last_vehiclecheck = time + 1;
2163         }
2164
2165         if(!self.cvar_cl_newusekeysupported) // FIXME remove this - it was a stupid idea to begin with, we can JUST use the button
2166         {
2167                 if(PHYS_INPUT_BUTTON_USE(self) && !self.usekeypressed)
2168                         PlayerUseKey();
2169                 self.usekeypressed = PHYS_INPUT_BUTTON_USE(self);
2170         }
2171
2172         if(IS_REAL_CLIENT(self))
2173                 PrintWelcomeMessage();
2174
2175         if(IS_PLAYER(self))
2176         {
2177
2178                 CheckRules_Player();
2179
2180                 if (intermission_running)
2181                 {
2182                         IntermissionThink ();   // otherwise a button could be missed between
2183                         return;                                 // the think tics
2184                 }
2185
2186                 //don't allow the player to turn around while game is paused!
2187                 if(timeout_status == TIMEOUT_ACTIVE) {
2188                         // FIXME turn this into CSQC stuff
2189                         self.v_angle = self.lastV_angle;
2190                         self.angles = self.lastV_angle;
2191                         self.fixangle = true;
2192                 }
2193
2194                 if(frametime)
2195                 {
2196                         player_powerups();
2197                 }
2198
2199                 if (IS_DEAD(self))
2200                 {
2201                         if(self.personal && g_race_qualifying)
2202                         {
2203                                 if(time > self.respawn_time)
2204                                 {
2205                                         self.respawn_time = time + 1; // only retry once a second
2206                                         self.stat_respawn_time = self.respawn_time;
2207                                         respawn();
2208                                         self.impulse = 141;
2209                                 }
2210                         }
2211                         else
2212                         {
2213                                 float button_pressed;
2214                                 if(frametime)
2215                                         player_anim();
2216                                 button_pressed = (PHYS_INPUT_BUTTON_ATCK(self) || PHYS_INPUT_BUTTON_JUMP(self) || PHYS_INPUT_BUTTON_ATCK2(self) || PHYS_INPUT_BUTTON_HOOK(self) || PHYS_INPUT_BUTTON_USE(self));
2217
2218                                 if (self.deadflag == DEAD_DYING)
2219                                 {
2220                                         if((self.respawn_flags & RESPAWN_FORCE) && !autocvar_g_respawn_delay_max)
2221                                                 self.deadflag = DEAD_RESPAWNING;
2222                                         else if(!button_pressed)
2223                                                 self.deadflag = DEAD_DEAD;
2224                                 }
2225                                 else if (self.deadflag == DEAD_DEAD)
2226                                 {
2227                                         if(button_pressed)
2228                                                 self.deadflag = DEAD_RESPAWNABLE;
2229                                         else if(time >= self.respawn_time_max && (self.respawn_flags & RESPAWN_FORCE))
2230                                                 self.deadflag = DEAD_RESPAWNING;
2231                                 }
2232                                 else if (self.deadflag == DEAD_RESPAWNABLE)
2233                                 {
2234                                         if(!button_pressed)
2235                                                 self.deadflag = DEAD_RESPAWNING;
2236                                 }
2237                                 else if (self.deadflag == DEAD_RESPAWNING)
2238                                 {
2239                                         if(time > self.respawn_time)
2240                                         {
2241                                                 self.respawn_time = time + 1; // only retry once a second
2242                                                 self.respawn_time_max = self.respawn_time;
2243                                                 respawn();
2244                                         }
2245                                 }
2246
2247                                 ShowRespawnCountdown();
2248
2249                                 if(self.respawn_flags & RESPAWN_SILENT)
2250                                         self.stat_respawn_time = 0;
2251                                 else if((self.respawn_flags & RESPAWN_FORCE) && autocvar_g_respawn_delay_max)
2252                                         self.stat_respawn_time = self.respawn_time_max;
2253                                 else
2254                                         self.stat_respawn_time = self.respawn_time;
2255                         }
2256
2257                         // if respawning, invert stat_respawn_time to indicate this, the client translates it
2258                         if(self.deadflag == DEAD_RESPAWNING && self.stat_respawn_time > 0)
2259                                 self.stat_respawn_time *= -1;
2260
2261                         return;
2262                 }
2263
2264                 self.prevorigin = self.origin;
2265
2266                 float do_crouch = PHYS_INPUT_BUTTON_CROUCH(self);
2267                 if(self.hook.state)
2268                         do_crouch = 0;
2269                 if(self.vehicle)
2270                         do_crouch = 0;
2271                 if(STAT(FROZEN, self))
2272                         do_crouch = 0;
2273
2274                 // WEAPONTODO: THIS SHIT NEEDS TO GO EVENTUALLY
2275                 // It cannot be predicted by the engine!
2276                 .entity weaponentity = weaponentities[0]; // TODO: unhardcode
2277                 if((PS(self).m_weapon == WEP_SHOCKWAVE || PS(self).m_weapon == WEP_SHOTGUN) && self.(weaponentity).wframe == WFRAME_FIRE2 && time < self.(weaponentity).weapon_nextthink)
2278                         do_crouch = 0;
2279
2280                 if (do_crouch)
2281                 {
2282                         if (!self.crouch)
2283                         {
2284                                 self.crouch = true;
2285                                 self.view_ofs = STAT(PL_CROUCH_VIEW_OFS, self);
2286                                 setsize (self, STAT(PL_CROUCH_MIN, self), STAT(PL_CROUCH_MAX, self));
2287                                 // setanim(self, self.anim_duck, false, true, true); // this anim is BROKEN anyway
2288                         }
2289                 }
2290                 else
2291                 {
2292                         if (self.crouch)
2293                         {
2294                                 tracebox(self.origin, STAT(PL_MIN, self), STAT(PL_MAX, self), self.origin, false, self);
2295                                 if (!trace_startsolid)
2296                                 {
2297                                         self.crouch = false;
2298                                         self.view_ofs = STAT(PL_VIEW_OFS, self);
2299                                         setsize (self, STAT(PL_MIN, self), STAT(PL_MAX, self));
2300                                 }
2301                         }
2302                 }
2303
2304                 FixPlayermodel(self);
2305
2306                 // LordHavoc: allow firing on move frames (sub-ticrate), this gives better timing on slow servers
2307                 //if(frametime)
2308                 {
2309                         self.items &= ~self.items_added;
2310
2311                         W_WeaponFrame(self);
2312
2313                         self.items_added = 0;
2314                         if(self.items & ITEM_Jetpack.m_itemid)
2315                                 if(self.items & ITEM_JetpackRegen.m_itemid || self.ammo_fuel >= 0.01)
2316                                         self.items_added |= IT_FUEL;
2317
2318                         self.items |= self.items_added;
2319                 }
2320
2321                 player_regen();
2322
2323                 // WEAPONTODO: Add a weapon request for this
2324                 // rot vortex charge to the charge limit
2325                 if(WEP_CVAR(vortex, charge_rot_rate) && self.vortex_charge > WEP_CVAR(vortex, charge_limit) && self.vortex_charge_rottime < time)
2326                         self.vortex_charge = bound(WEP_CVAR(vortex, charge_limit), self.vortex_charge - WEP_CVAR(vortex, charge_rot_rate) * frametime / W_TICSPERFRAME, 1);
2327
2328                 if(frametime)
2329                         player_anim();
2330
2331                 // secret status
2332                 secrets_setstatus();
2333
2334                 // monsters status
2335                 monsters_setstatus(self);
2336
2337                 self.dmg_team = max(0, self.dmg_team - autocvar_g_teamdamage_resetspeed * frametime);
2338
2339                 //self.angles_y=self.v_angle_y + 90;   // temp
2340         } else if(gameover) {
2341                 if (intermission_running)
2342                         IntermissionThink ();   // otherwise a button could be missed between
2343                 return;
2344         } else if(IS_OBSERVER(self)) {
2345                 ObserverThink();
2346         } else if(IS_SPEC(self)) {
2347                 SpectatorThink();
2348         }
2349
2350         // WEAPONTODO: Add weapon request for this
2351         if(!zoomstate_set)
2352                 SetZoomState(
2353                         PHYS_INPUT_BUTTON_ZOOM(self)
2354                         || PHYS_INPUT_BUTTON_ZOOMSCRIPT(self)
2355                         || (PHYS_INPUT_BUTTON_ATCK2(self) && PS(self).m_weapon == WEP_VORTEX)
2356                         || (PHYS_INPUT_BUTTON_ATCK2(self) && PS(self).m_weapon == WEP_RIFLE && WEP_CVAR(rifle, secondary) == 0)
2357                 ); // WEAPONTODO
2358
2359         float oldspectatee_status;
2360         oldspectatee_status = self.spectatee_status;
2361         if(IS_SPEC(self))
2362                 self.spectatee_status = etof(self.enemy);
2363         else if(IS_OBSERVER(self))
2364                 self.spectatee_status = etof(self);
2365         else
2366                 self.spectatee_status = 0;
2367         if(self.spectatee_status != oldspectatee_status)
2368         {
2369                 ClientData_Touch(self);
2370                 if(g_race || g_cts)
2371                         race_InitSpectator();
2372         }
2373
2374         if(self.teamkill_soundtime)
2375         if(time > self.teamkill_soundtime)
2376         {
2377                 self.teamkill_soundtime = 0;
2378
2379                 entity e = self.teamkill_soundsource;
2380                 entity oldpusher = e.pusher;
2381                 e.pusher = this;
2382                 PlayerSound(e, playersound_teamshoot, CH_VOICE, VOICETYPE_LASTATTACKER_ONLY);
2383                 e.pusher = oldpusher;
2384         }
2385
2386         if(self.taunt_soundtime)
2387         if(time > self.taunt_soundtime)
2388         {
2389                 self.taunt_soundtime = 0;
2390                 PlayerSound(self, playersound_taunt, CH_VOICE, VOICETYPE_AUTOTAUNT);
2391         }
2392
2393         target_voicescript_next(self);
2394
2395         // WEAPONTODO: Move into weaponsystem somehow
2396         // if a player goes unarmed after holding a loaded weapon, empty his clip size and remove the crosshair ammo ring
2397         if (PS(self).m_weapon == WEP_Null)
2398                 self.clip_load = self.clip_size = 0;
2399 }
2400
2401 void DrownPlayer(entity this)
2402 {
2403         if(IS_DEAD(this))
2404                 return;
2405
2406         if (this.waterlevel != WATERLEVEL_SUBMERGED)
2407         {
2408                 if(this.air_finished < time)
2409                         PlayerSound(this, playersound_gasp, CH_PLAYER, VOICETYPE_PLAYERSOUND);
2410                 this.air_finished = time + autocvar_g_balance_contents_drowndelay;
2411                 this.dmg = 2;
2412         }
2413         else if (this.air_finished < time)
2414         {       // drown!
2415                 if (this.pain_finished < time)
2416                 {
2417                         Damage (this, world, world, autocvar_g_balance_contents_playerdamage_drowning * autocvar_g_balance_contents_damagerate, DEATH_DROWN.m_id, this.origin, '0 0 0');
2418                         this.pain_finished = time + 0.5;
2419                 }
2420         }
2421 }
2422
2423 /*
2424 =============
2425 PlayerPostThink
2426
2427 Called every frame for each client after the physics are run
2428 =============
2429 */
2430 .float idlekick_lasttimeleft;
2431 void PlayerPostThink ()
2432 {SELFPARAM();
2433         if(sv_maxidle > 0 && frametime) // WORKAROUND: only use dropclient in server frames (frametime set). Never use it in cl_movement frames (frametime zero).
2434         if(IS_REAL_CLIENT(self))
2435         if(IS_PLAYER(self) || sv_maxidle_spectatorsareidle)
2436         {
2437                 if (time - self.parm_idlesince < 1) // instead of (time == self.parm_idlesince) to support sv_maxidle <= 10
2438                 {
2439                         if(self.idlekick_lasttimeleft)
2440                         {
2441                                 self.idlekick_lasttimeleft = 0;
2442                                 Kill_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER_CPID, CPID_IDLING);
2443                         }
2444                 }
2445                 else
2446                 {
2447                         float timeleft;
2448                         timeleft = ceil(sv_maxidle - (time - self.parm_idlesince));
2449                         if(timeleft == min(10, sv_maxidle - 1)) // - 1 to support sv_maxidle <= 10
2450                         {
2451                                 if(!self.idlekick_lasttimeleft)
2452                                         Send_Notification(NOTIF_ONE_ONLY, self, MSG_CENTER, CENTER_DISCONNECT_IDLING, timeleft);
2453                         }
2454                         if(timeleft <= 0)
2455                         {
2456                                 Send_Notification(NOTIF_ALL, world, MSG_INFO, INFO_QUIT_KICK_IDLING, self.netname);
2457                                 dropclient(self);
2458                                 return;
2459                         }
2460                         else if(timeleft <= 10)
2461                         {
2462                                 if(timeleft != self.idlekick_lasttimeleft)
2463                                         { Send_Notification(NOTIF_ONE, self, MSG_ANNCE, Announcer_PickNumber(CNT_IDLE, timeleft)); }
2464                                 self.idlekick_lasttimeleft = timeleft;
2465                         }
2466                 }
2467         }
2468
2469         CheatFrame();
2470
2471         //CheckPlayerJump();
2472
2473         if(IS_PLAYER(self)) {
2474                 DrownPlayer(self);
2475                 CheckRules_Player();
2476                 UpdateChatBubble();
2477                 if (self.impulse)
2478                         ImpulseCommands(self);
2479                 if (intermission_running)
2480                         return;         // intermission or finale
2481                 GetPressedKeys();
2482         }
2483
2484         /*
2485         float i;
2486         for(i = 0; i < 1000; ++i)
2487         {
2488                 vector end;
2489                 end = self.origin + '0 0 1024' + 512 * randomvec();
2490                 tracebox(self.origin, self.mins, self.maxs, end, MOVE_NORMAL, self);
2491                 if(trace_fraction < 1)
2492                 if(!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOIMPACT))
2493                 {
2494                         print("I HIT SOLID: ", vtos(self.origin), " -> ", vtos(end), "\n");
2495                         break;
2496                 }
2497         }
2498         */
2499
2500         if(self.waypointsprite_attachedforcarrier)
2501                 WaypointSprite_UpdateHealth(self.waypointsprite_attachedforcarrier, '1 0 0' * healtharmor_maxdamage(self.health, self.armorvalue, autocvar_g_balance_armor_blockpercent, DEATH_WEAPON.m_id));
2502
2503         playerdemo_write();
2504
2505         CSQCMODEL_AUTOUPDATE(self);
2506 }