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