]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/client.qc
Merge branch 'master' into bones_was_here/q3compat
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / client.qc
1 #include "client.qh"
2
3 #include <common/csqcmodel_settings.qh>
4 #include <common/deathtypes/all.qh>
5 #include <common/effects/all.qh>
6 #include <common/effects/qc/globalsound.qh>
7 #include <common/ent_cs.qh>
8 #include <common/gamemodes/_mod.qh>
9 #include <common/gamemodes/gamemode/nexball/sv_nexball.qh>
10 #include <common/items/_mod.qh>
11 #include <common/items/inventory.qh>
12 #include <common/mapobjects/func/conveyor.qh>
13 #include <common/mapobjects/func/ladder.qh>
14 #include <common/mapobjects/subs.qh>
15 #include <common/mapobjects/target/spawnpoint.qh>
16 #include <common/mapobjects/teleporters.qh>
17 #include <common/mapobjects/trigger/counter.qh>
18 #include <common/mapobjects/trigger/secret.qh>
19 #include <common/mapobjects/trigger/swamp.qh>
20 #include <common/mapobjects/triggers.qh>
21 #include <common/minigames/sv_minigames.qh>
22 #include <common/monsters/sv_monsters.qh>
23 #include <common/mutators/mutator/instagib/sv_instagib.qh>
24 #include <common/mutators/mutator/nades/nades.qh>
25 #include <common/mutators/mutator/overkill/oknex.qh>
26 #include <common/mutators/mutator/waypoints/all.qh>
27 #include <common/net_linked.qh>
28 #include <common/net_notice.qh>
29 #include <common/notifications/all.qh>
30 #include <common/physics/player.qh>
31 #include <common/playerstats.qh>
32 #include <common/state.qh>
33 #include <common/stats.qh>
34 #include <common/vehicles/all.qh>
35 #include <common/vehicles/sv_vehicles.qh>
36 #include <common/viewloc.qh>
37 #include <common/weapons/_all.qh>
38 #include <common/weapons/weapon/vortex.qh>
39 #include <common/wepent.qh>
40 #include <lib/csqcmodel/sv_model.qh>
41 #include <lib/warpzone/common.qh>
42 #include <lib/warpzone/server.qh>
43 #include <server/anticheat.qh>
44 #include <server/antilag.qh>
45 #include <server/bot/api.qh>
46 #include <server/bot/default/cvars.qh>
47 #include <server/campaign.qh>
48 #include <server/chat.qh>
49 #include <server/cheats.qh>
50 #include <server/clientkill.qh>
51 #include <server/command/common.qh>
52 #include <server/command/common.qh>
53 #include <server/command/vote.qh>
54 #include <server/compat/quake3.qh>
55 #include <server/damage.qh>
56 #include <server/gamelog.qh>
57 #include <server/handicap.qh>
58 #include <server/hook.qh>
59 #include <server/impulse.qh>
60 #include <server/intermission.qh>
61 #include <server/ipban.qh>
62 #include <server/main.qh>
63 #include <server/mutators/_mod.qh>
64 #include <server/player.qh>
65 #include <server/portals.qh>
66 #include <server/race.qh>
67 #include <server/resources.qh>
68 #include <server/scores.qh>
69 #include <server/scores_rules.qh>
70 #include <server/spawnpoints.qh>
71 #include <server/teamplay.qh>
72 #include <server/weapons/accuracy.qh>
73 #include <server/weapons/common.qh>
74 #include <server/weapons/hitplot.qh>
75 #include <server/weapons/selection.qh>
76 #include <server/weapons/tracing.qh>
77 #include <server/weapons/weaponsystem.qh>
78 #include <server/world.qh>
79
80 STATIC_METHOD(Client, Add, void(Client this, int _team))
81 {
82     ClientConnect(this);
83     TRANSMUTE(Player, this);
84     this.frame = 12; // 7
85     this.team = _team;
86     PutClientInServer(this);
87 }
88
89 STATIC_METHOD(Client, Remove, void(Client this))
90 {
91     TRANSMUTE(Observer, this);
92     PutClientInServer(this);
93     ClientDisconnect(this);
94 }
95
96 void send_CSQC_teamnagger() {
97         WriteHeader(MSG_BROADCAST, TE_CSQC_TEAMNAGGER);
98 }
99
100 int CountSpectators(entity player, entity to)
101 {
102         if(!player) { return 0; } // not sure how, but best to be safe
103
104         int spec_count = 0;
105
106         FOREACH_CLIENT(IS_REAL_CLIENT(it) && IS_SPEC(it) && it != to && it.enemy == player,
107         {
108                 spec_count++;
109         });
110
111         return spec_count;
112 }
113
114 void WriteSpectators(entity player, entity to)
115 {
116         if(!player) { return; } // not sure how, but best to be safe
117
118         int spec_count = 0;
119         FOREACH_CLIENT(IS_REAL_CLIENT(it) && IS_SPEC(it) && it != to && it.enemy == player,
120         {
121                 if(spec_count >= MAX_SPECTATORS)
122                         break;
123                 WriteByte(MSG_ENTITY, num_for_edict(it));
124                 ++spec_count;
125         });
126 }
127
128 bool ClientData_Send(entity this, entity to, int sf)
129 {
130         assert(to == this.owner, return false);
131
132         entity e = to;
133         if (IS_SPEC(e)) e = e.enemy;
134
135         sf = 0;
136         if (CS(e).race_completed)       sf |= BIT(0); // forced scoreboard
137         if (CS(to).spectatee_status)    sf |= BIT(1); // spectator ent number follows
138         if (CS(e).zoomstate)            sf |= BIT(2); // zoomed
139         if (autocvar_sv_showspectators) sf |= BIT(4); // show spectators
140
141         WriteHeader(MSG_ENTITY, ENT_CLIENT_CLIENTDATA);
142         WriteByte(MSG_ENTITY, sf);
143
144         if (sf & BIT(1))
145                 WriteByte(MSG_ENTITY, CS(to).spectatee_status);
146
147         if(sf & BIT(4))
148         {
149                 float specs = CountSpectators(e, to);
150                 WriteByte(MSG_ENTITY, specs);
151                 WriteSpectators(e, to);
152         }
153
154         return true;
155 }
156
157 void ClientData_Attach(entity this)
158 {
159         Net_LinkEntity(CS(this).clientdata = new_pure(clientdata), false, 0, ClientData_Send);
160         CS(this).clientdata.drawonlytoclient = this;
161         CS(this).clientdata.owner = this;
162 }
163
164 void ClientData_Detach(entity this)
165 {
166         delete(CS(this).clientdata);
167         CS(this).clientdata = NULL;
168 }
169
170 void ClientData_Touch(entity e)
171 {
172         entity cd = CS(e).clientdata;
173         if (cd) { cd.SendFlags = 1; }
174
175         // make it spectatable
176         FOREACH_CLIENT(IS_REAL_CLIENT(it) && it != e && IS_SPEC(it) && it.enemy == e,
177         {
178                 entity cd = CS(it).clientdata;
179                 if (cd) { cd.SendFlags = 1; }
180         });
181 }
182
183
184 /*
185 =============
186 CheckPlayerModel
187
188 Checks if the argument string can be a valid playermodel.
189 Returns a valid one in doubt.
190 =============
191 */
192 string FallbackPlayerModel;
193 string CheckPlayerModel(string plyermodel) {
194         if(FallbackPlayerModel != cvar_defstring("_cl_playermodel"))
195         {
196                 // note: we cannot summon Don Strunzone here, some player may
197                 // still have the model string set. In case anyone manages how
198                 // to change a cvar default, we'll have a small leak here.
199                 FallbackPlayerModel = strzone(cvar_defstring("_cl_playermodel"));
200         }
201         // only in right path
202         if(substring(plyermodel, 0, 14) != "models/player/")
203                 return FallbackPlayerModel;
204         // only good file extensions
205         if(substring(plyermodel, -4, 4) != ".iqm"
206                 && substring(plyermodel, -4, 4) != ".zym"
207                 && substring(plyermodel, -4, 4) != ".dpm"
208                 && substring(plyermodel, -4, 4) != ".md3"
209                 && substring(plyermodel, -4, 4) != ".psk")
210         {
211                 return FallbackPlayerModel;
212         }
213         // forbid the LOD models
214         if(substring(plyermodel, -9, 5) == "_lod1" || substring(plyermodel, -9, 5) == "_lod2")
215                 return FallbackPlayerModel;
216         if(plyermodel != strtolower(plyermodel))
217                 return FallbackPlayerModel;
218         // also, restrict to server models
219         if(autocvar_sv_servermodelsonly)
220         {
221                 if(!fexists(plyermodel))
222                         return FallbackPlayerModel;
223         }
224         return plyermodel;
225 }
226
227 void setplayermodel(entity e, string modelname)
228 {
229         precache_model(modelname);
230         _setmodel(e, modelname);
231         player_setupanimsformodel(e);
232         if(!autocvar_g_debug_globalsounds)
233                 UpdatePlayerSounds(e);
234 }
235
236 /** putting a client as observer in the server */
237 void PutObserverInServer(entity this)
238 {
239         bool mutator_returnvalue = MUTATOR_CALLHOOK(MakePlayerObserver, this);
240         PlayerState_detach(this);
241
242         if (IS_PLAYER(this))
243         {
244                 if(GetResource(this, RES_HEALTH) >= 1)
245                 {
246                         // despawn effect
247                         Send_Effect(EFFECT_SPAWN_NEUTRAL, this.origin, '0 0 0', 1);
248                 }
249
250                 // was a player, recount votes and ready status
251                 if(IS_REAL_CLIENT(this))
252                 {
253                         if (vote_called) { VoteCount(false); }
254                         ReadyCount();
255                 }
256                 entcs_update_players(this);
257         }
258
259         entity spot = SelectSpawnPoint(this, true);
260         if (!spot) LOG_FATAL("No spawnpoints for observers?!?");
261         this.angles = vec2(spot.angles);
262         this.fixangle = true;
263         // offset it so that the spectator spawns higher off the ground, looks better this way
264         setorigin(this, spot.origin + STAT(PL_VIEW_OFS, this));
265         if (IS_REAL_CLIENT(this))
266         {
267                 msg_entity = this;
268                 WriteByte(MSG_ONE, SVC_SETVIEW);
269                 WriteEntity(MSG_ONE, this);
270         }
271         // give the spectator some space between walls for MOVETYPE_FLY_WORLDONLY
272         // so that your view doesn't go into the ceiling with MOVETYPE_FLY_WORLDONLY, previously "PL_VIEW_OFS"
273         if(!autocvar_g_debug_globalsounds)
274         {
275                 // needed for player sounds
276                 this.model = "";
277                 FixPlayermodel(this);
278         }
279         setmodel(this, MDL_Null);
280         setsize(this, STAT(PL_CROUCH_MIN, this), STAT(PL_CROUCH_MAX, this));
281         this.view_ofs = '0 0 0';
282
283         RemoveGrapplingHooks(this);
284         Portal_ClearAll(this);
285         Unfreeze(this, false);
286         SetSpectatee(this, NULL);
287
288         if (this.alivetime)
289         {
290                 if (!warmup_stage)
291                         PlayerStats_GameReport_Event_Player(this, PLAYERSTATS_ALIVETIME, time - this.alivetime);
292                 this.alivetime = 0;
293         }
294
295         if (this.vehicle) vehicles_exit(this.vehicle, VHEF_RELEASE);
296
297         WaypointSprite_PlayerDead(this);
298
299         if (CS(this).killcount != FRAGS_SPECTATOR && !game_stopped && CHAT_NOSPECTATORS())
300                 Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_CHAT_NOSPECTATORS);
301
302         accuracy_resend(this);
303
304         CS(this).spectatortime = time;
305         if(this.bot_attack)
306                 IL_REMOVE(g_bot_targets, this);
307         this.bot_attack = false;
308         if(this.monster_attack)
309                 IL_REMOVE(g_monster_targets, this);
310         this.monster_attack = false;
311         STAT(HUD, this) = HUD_NORMAL;
312         TRANSMUTE(Observer, this);
313         this.iscreature = false;
314         this.teleportable = TELEPORT_SIMPLE;
315         if(this.damagedbycontents)
316                 IL_REMOVE(g_damagedbycontents, this);
317         this.damagedbycontents = false;
318         SetResourceExplicit(this, RES_HEALTH, FRAGS_SPECTATOR);
319         SetSpectatee_status(this, etof(this));
320         this.takedamage = DAMAGE_NO;
321         this.solid = SOLID_NOT;
322         set_movetype(this, MOVETYPE_FLY_WORLDONLY); // user preference is controlled by playerprethink
323         this.flags = FL_CLIENT | FL_NOTARGET;
324         this.effects = 0;
325         SetResourceExplicit(this, RES_ARMOR, autocvar_g_balance_armor_start); // was 666?!
326         this.pauserotarmor_finished = 0;
327         this.pauserothealth_finished = 0;
328         this.pauseregen_finished = 0;
329         this.damageforcescale = 0;
330         this.death_time = 0;
331         this.respawn_flags = 0;
332         this.respawn_time = 0;
333         STAT(RESPAWN_TIME, this) = 0;
334         this.alpha = 0;
335         this.scale = 0;
336         this.fade_time = 0;
337         this.pain_finished = 0;
338         STAT(STRENGTH_FINISHED, this) = 0;
339         STAT(INVINCIBLE_FINISHED, this) = 0;
340         STAT(SUPERWEAPONS_FINISHED, this) = 0;
341         STAT(AIR_FINISHED, this) = 0;
342         //this.dphitcontentsmask = 0;
343         this.dphitcontentsmask = DPCONTENTS_SOLID;
344         if (autocvar_g_playerclip_collisions)
345                 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
346         this.pushltime = 0;
347         this.istypefrag = 0;
348         setthink(this, func_null);
349         this.nextthink = 0;
350         this.deadflag = DEAD_NO;
351         UNSET_DUCKED(this);
352         STAT(REVIVE_PROGRESS, this) = 0;
353         this.revival_time = 0;
354         this.draggable = drag_undraggable;
355
356         this.items = 0;
357         STAT(WEAPONS, this) = '0 0 0';
358         this.drawonlytoclient = this;
359
360         this.viewloc = NULL;
361
362         //this.spawnpoint_targ = NULL; // keep it so they can return to where they were?
363
364         this.weaponmodel = "";
365         for (int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
366         {
367                 this.weaponentities[slot] = NULL;
368         }
369         this.exteriorweaponentity = NULL;
370         CS(this).killcount = FRAGS_SPECTATOR;
371         this.velocity = '0 0 0';
372         this.avelocity = '0 0 0';
373         this.punchangle = '0 0 0';
374         this.punchvector = '0 0 0';
375         this.oldvelocity = this.velocity;
376         this.fire_endtime = -1;
377         this.event_damage = func_null;
378         this.event_heal = func_null;
379
380         for(int slot = 0; slot < MAX_AXH; ++slot)
381         {
382                 entity axh = this.(AuxiliaryXhair[slot]);
383                 this.(AuxiliaryXhair[slot]) = NULL;
384
385                 if(axh.owner == this && axh != NULL && !wasfreed(axh))
386                         delete(axh);
387         }
388
389         if (mutator_returnvalue)
390         {
391                 // mutator prevents resetting teams+score
392         }
393         else
394         {
395                 SetPlayerTeam(this, -1, TEAM_CHANGE_SPECTATOR);
396                 this.frags = FRAGS_SPECTATOR;
397         }
398         if (CS(this).just_joined)
399                 CS(this).just_joined = false;
400 }
401
402 int player_getspecies(entity this)
403 {
404         get_model_parameters(this.model, this.skin);
405         int s = get_model_parameters_species;
406         get_model_parameters(string_null, 0);
407         if (s < 0) return SPECIES_HUMAN;
408         return s;
409 }
410
411 .float model_randomizer;
412 void FixPlayermodel(entity player)
413 {
414         string defaultmodel = "";
415         int defaultskin = 0;
416         if(autocvar_sv_defaultcharacter)
417         {
418                 if(teamplay)
419                 {
420                         switch(player.team)
421                         {
422                                 case NUM_TEAM_1: defaultmodel = autocvar_sv_defaultplayermodel_red; defaultskin = autocvar_sv_defaultplayerskin_red; break;
423                                 case NUM_TEAM_2: defaultmodel = autocvar_sv_defaultplayermodel_blue; defaultskin = autocvar_sv_defaultplayerskin_blue; break;
424                                 case NUM_TEAM_3: defaultmodel = autocvar_sv_defaultplayermodel_yellow; defaultskin = autocvar_sv_defaultplayerskin_yellow; break;
425                                 case NUM_TEAM_4: defaultmodel = autocvar_sv_defaultplayermodel_pink; defaultskin = autocvar_sv_defaultplayerskin_pink; break;
426                         }
427                 }
428
429                 if(defaultmodel == "")
430                 {
431                         defaultmodel = autocvar_sv_defaultplayermodel;
432                         defaultskin = autocvar_sv_defaultplayerskin;
433                 }
434
435                 int n = tokenize_console(defaultmodel);
436                 if(n > 0)
437                 {
438                         defaultmodel = argv(floor(n * CS(player).model_randomizer));
439                         // However, do NOT randomize if the player-selected model is in the list.
440                         for (int i = 0; i < n; ++i)
441                                 if ((argv(i) == player.playermodel && defaultskin == stof(player.playerskin)) || argv(i) == strcat(player.playermodel, ":", player.playerskin))
442                                         defaultmodel = argv(i);
443                 }
444
445                 int i = strstrofs(defaultmodel, ":", 0);
446                 if(i >= 0)
447                 {
448                         defaultskin = stof(substring(defaultmodel, i+1, -1));
449                         defaultmodel = substring(defaultmodel, 0, i);
450                 }
451         }
452         if(autocvar_sv_defaultcharacterskin && !defaultskin)
453         {
454                 if(teamplay)
455                 {
456                         switch(player.team)
457                         {
458                                 case NUM_TEAM_1: defaultskin = autocvar_sv_defaultplayerskin_red; break;
459                                 case NUM_TEAM_2: defaultskin = autocvar_sv_defaultplayerskin_blue; break;
460                                 case NUM_TEAM_3: defaultskin = autocvar_sv_defaultplayerskin_yellow; break;
461                                 case NUM_TEAM_4: defaultskin = autocvar_sv_defaultplayerskin_pink; break;
462                         }
463                 }
464
465                 if(!defaultskin)
466                         defaultskin = autocvar_sv_defaultplayerskin;
467         }
468
469         MUTATOR_CALLHOOK(FixPlayermodel, defaultmodel, defaultskin, player);
470         defaultmodel = M_ARGV(0, string);
471         defaultskin = M_ARGV(1, int);
472
473         bool chmdl = false;
474         int oldskin;
475         if(defaultmodel != "")
476         {
477                 if (defaultmodel != player.model)
478                 {
479                         vector m1 = player.mins;
480                         vector m2 = player.maxs;
481                         setplayermodel (player, defaultmodel);
482                         setsize (player, m1, m2);
483                         chmdl = true;
484                 }
485
486                 oldskin = player.skin;
487                 player.skin = defaultskin;
488         } else {
489                 if (player.playermodel != player.model || player.playermodel == "")
490                 {
491                         player.playermodel = CheckPlayerModel(player.playermodel); // this is never "", so no endless loop
492                         vector m1 = player.mins;
493                         vector m2 = player.maxs;
494                         setplayermodel (player, player.playermodel);
495                         setsize (player, m1, m2);
496                         chmdl = true;
497                 }
498
499                 if(!autocvar_sv_defaultcharacterskin)
500                 {
501                         oldskin = player.skin;
502                         player.skin = stof(player.playerskin);
503                 }
504                 else
505                 {
506                         oldskin = player.skin;
507                         player.skin = defaultskin;
508                 }
509         }
510
511         if(chmdl || oldskin != player.skin) // model or skin has changed
512         {
513                 player.species = player_getspecies(player); // update species
514                 if(!autocvar_g_debug_globalsounds)
515                         UpdatePlayerSounds(player); // update skin sounds
516         }
517
518         if(!teamplay)
519                 if(strlen(autocvar_sv_defaultplayercolors))
520                         if(player.clientcolors != stof(autocvar_sv_defaultplayercolors))
521                                 setcolor(player, stof(autocvar_sv_defaultplayercolors));
522 }
523
524 void PutPlayerInServer(entity this)
525 {
526         if (this.vehicle) vehicles_exit(this.vehicle, VHEF_RELEASE);
527
528         PlayerState_attach(this);
529         accuracy_resend(this);
530
531         if (this.team < 0)
532                 TeamBalance_JoinBestTeam(this);
533
534         entity spot = SelectSpawnPoint(this, false);
535         if (!spot) {
536                 Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_JOIN_NOSPAWNS);
537                 return; // spawn failed
538         }
539
540         TRANSMUTE(Player, this);
541
542         CS(this).wasplayer = true;
543         this.iscreature = true;
544         this.teleportable = TELEPORT_NORMAL;
545         if(!this.damagedbycontents)
546                 IL_PUSH(g_damagedbycontents, this);
547         this.damagedbycontents = true;
548         set_movetype(this, MOVETYPE_WALK);
549         this.solid = SOLID_SLIDEBOX;
550         this.dphitcontentsmask = DPCONTENTS_BODY | DPCONTENTS_SOLID;
551         if (autocvar_g_playerclip_collisions)
552                 this.dphitcontentsmask |= DPCONTENTS_PLAYERCLIP;
553         if (IS_BOT_CLIENT(this) && autocvar_g_botclip_collisions)
554                 this.dphitcontentsmask |= DPCONTENTS_BOTCLIP;
555         this.frags = FRAGS_PLAYER;
556         if (INDEPENDENT_PLAYERS) MAKE_INDEPENDENT_PLAYER(this);
557         this.flags = FL_CLIENT | FL_PICKUPITEMS;
558         if (autocvar__notarget)
559                 this.flags |= FL_NOTARGET;
560         this.takedamage = DAMAGE_AIM;
561         this.effects = EF_TELEPORT_BIT | EF_RESTARTANIM_BIT;
562
563         if (warmup_stage) {
564                 SetResource(this, RES_SHELLS, warmup_start_ammo_shells);
565                 SetResource(this, RES_BULLETS, warmup_start_ammo_nails);
566                 SetResource(this, RES_ROCKETS, warmup_start_ammo_rockets);
567                 SetResource(this, RES_CELLS, warmup_start_ammo_cells);
568                 SetResource(this, RES_PLASMA, warmup_start_ammo_plasma);
569                 SetResource(this, RES_FUEL, warmup_start_ammo_fuel);
570                 SetResource(this, RES_HEALTH, warmup_start_health);
571                 SetResource(this, RES_ARMOR, warmup_start_armorvalue);
572                 STAT(WEAPONS, this) = WARMUP_START_WEAPONS;
573         } else {
574                 SetResource(this, RES_SHELLS, start_ammo_shells);
575                 SetResource(this, RES_BULLETS, start_ammo_nails);
576                 SetResource(this, RES_ROCKETS, start_ammo_rockets);
577                 SetResource(this, RES_CELLS, start_ammo_cells);
578                 SetResource(this, RES_PLASMA, start_ammo_plasma);
579                 SetResource(this, RES_FUEL, start_ammo_fuel);
580                 SetResource(this, RES_HEALTH, start_health);
581                 SetResource(this, RES_ARMOR, start_armorvalue);
582                 STAT(WEAPONS, this) = start_weapons;
583                 if (MUTATOR_CALLHOOK(ForbidRandomStartWeapons, this) == false)
584                 {
585                         GiveRandomWeapons(this, random_start_weapons_count,
586                                 autocvar_g_random_start_weapons, random_start_ammo);
587                 }
588         }
589         SetSpectatee_status(this, 0);
590
591         PS(this).dual_weapons = '0 0 0';
592
593         STAT(SUPERWEAPONS_FINISHED, this) = (STAT(WEAPONS, this) & WEPSET_SUPERWEAPONS) ? time + autocvar_g_balance_superweapons_time : 0;
594
595         this.items = start_items;
596
597         this.spawnshieldtime = time + autocvar_g_spawnshieldtime;
598         this.pauserotarmor_finished = time + autocvar_g_balance_pause_armor_rot_spawn;
599         this.pauserothealth_finished = time + autocvar_g_balance_pause_health_rot_spawn;
600         this.pauserotfuel_finished = time + autocvar_g_balance_pause_fuel_rot_spawn;
601         this.pauseregen_finished = time + autocvar_g_balance_pause_health_regen_spawn;
602         if (!sv_ready_restart_after_countdown && time < game_starttime)
603         {
604                 float f = game_starttime - time;
605                 this.spawnshieldtime += f;
606                 this.pauserotarmor_finished += f;
607                 this.pauserothealth_finished += f;
608                 this.pauseregen_finished += f;
609         }
610
611         this.damageforcescale = autocvar_g_player_damageforcescale;
612         this.death_time = 0;
613         this.respawn_flags = 0;
614         this.respawn_time = 0;
615         STAT(RESPAWN_TIME, this) = 0;
616         this.scale = ((q3compat && autocvar_sv_q3compat_changehitbox) ? 0.9 : autocvar_sv_player_scale);
617         this.fade_time = 0;
618         this.pain_finished = 0;
619         this.pushltime = 0;
620         setthink(this, func_null); // players have no think function
621         this.nextthink = 0;
622         this.dmg_team = 0;
623         PS(this).ballistics_density = autocvar_g_ballistics_density_player;
624
625         this.deadflag = DEAD_NO;
626
627         this.angles = spot.angles;
628         this.angles_z = 0; // never spawn tilted even if the spot says to
629         if (IS_BOT_CLIENT(this))
630         {
631                 this.v_angle = this.angles;
632                 bot_aim_reset(this);
633         }
634         this.fixangle = true; // turn this way immediately
635         this.oldvelocity = this.velocity = '0 0 0';
636         this.avelocity = '0 0 0';
637         this.punchangle = '0 0 0';
638         this.punchvector = '0 0 0';
639
640         STAT(STRENGTH_FINISHED, this) = 0;
641         STAT(INVINCIBLE_FINISHED, this) = 0;
642         this.fire_endtime = -1;
643         STAT(REVIVE_PROGRESS, this) = 0;
644         this.revival_time = 0;
645
646         // TODO: we can't set these in the PlayerSpawn hook since the target code is called before it!
647         STAT(BUFFS, this) = 0;
648         STAT(BUFF_TIME, this) = 0;
649
650         STAT(AIR_FINISHED, this) = 0;
651         this.waterlevel = WATERLEVEL_NONE;
652         this.watertype = CONTENT_EMPTY;
653
654         entity spawnevent = new_pure(spawnevent);
655         spawnevent.owner = this;
656         Net_LinkEntity(spawnevent, false, 0.5, SpawnEvent_Send);
657
658         // Cut off any still running player sounds.
659         stopsound(this, CH_PLAYER_SINGLE);
660
661         this.model = "";
662         FixPlayermodel(this);
663         this.drawonlytoclient = NULL;
664
665         this.viewloc = NULL;
666
667         for(int slot = 0; slot < MAX_AXH; ++slot)
668         {
669                 entity axh = this.(AuxiliaryXhair[slot]);
670                 this.(AuxiliaryXhair[slot]) = NULL;
671
672                 if(axh.owner == this && axh != NULL && !wasfreed(axh))
673                         delete(axh);
674         }
675
676         this.spawnpoint_targ = NULL;
677
678         UNSET_DUCKED(this);
679         this.view_ofs = STAT(PL_VIEW_OFS, this);
680         setsize(this, STAT(PL_MIN, this), STAT(PL_MAX, this));
681         this.spawnorigin = spot.origin;
682         setorigin(this, spot.origin + '0 0 1' * (1 - this.mins.z - 24));
683         // don't reset back to last position, even if new position is stuck in solid
684         this.oldorigin = this.origin;
685         if(this.conveyor)
686                 IL_REMOVE(g_conveyed, this);
687         this.conveyor = NULL; // prevent conveyors at the previous location from moving a freshly spawned player
688         if(this.swampslug)
689                 IL_REMOVE(g_swamped, this);
690         this.swampslug = NULL;
691         this.swamp_interval = 0;
692         if(this.ladder_entity)
693                 IL_REMOVE(g_ladderents, this);
694         this.ladder_entity = NULL;
695         IL_EACH(g_counters, it.realowner == this,
696         {
697                 delete(it);
698         });
699         STAT(HUD, this) = HUD_NORMAL;
700
701         this.event_damage = PlayerDamage;
702         this.event_heal = PlayerHeal;
703
704         this.draggable = func_null;
705
706         if(!this.bot_attack)
707                 IL_PUSH(g_bot_targets, this);
708         this.bot_attack = true;
709         if(!this.monster_attack)
710                 IL_PUSH(g_monster_targets, this);
711         this.monster_attack = true;
712         navigation_dynamicgoal_init(this, false);
713
714         PHYS_INPUT_BUTTON_ATCK(this) = PHYS_INPUT_BUTTON_JUMP(this) = PHYS_INPUT_BUTTON_ATCK2(this) = false;
715
716         // player was spectator
717         if (CS(this).killcount == FRAGS_SPECTATOR) {
718                 PlayerScore_Clear(this);
719                 CS(this).killcount = 0;
720                 CS(this).startplaytime = time;
721         }
722
723         for (int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
724         {
725                 .entity weaponentity = weaponentities[slot];
726                 CL_SpawnWeaponentity(this, weaponentity);
727         }
728         this.alpha = default_player_alpha;
729         this.colormod = '1 1 1' * autocvar_g_player_brightness;
730         this.exteriorweaponentity.alpha = default_weapon_alpha;
731
732         this.speedrunning = false;
733
734         this.counter_cnt = 0;
735         this.fragsfilter_cnt = 0;
736
737         target_voicescript_clear(this);
738
739         // reset fields the weapons may use
740         FOREACH(Weapons, true, {
741                 it.wr_resetplayer(it, this);
742                         // reload all reloadable weapons
743                 if (it.spawnflags & WEP_FLAG_RELOADABLE) {
744                         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
745                         {
746                                 .entity weaponentity = weaponentities[slot];
747                                 this.(weaponentity).weapon_load[it.m_id] = it.reloading_ammo;
748                         }
749                 }
750         });
751
752         {
753                 string s = spot.target;
754                 if(g_assault || g_race) // TODO: make targeting work in assault & race without this hack
755                         spot.target = string_null;
756                 SUB_UseTargets(spot, this, NULL);
757                 if(g_assault || g_race)
758                         spot.target = s;
759         }
760
761         Unfreeze(this, false);
762
763         MUTATOR_CALLHOOK(PlayerSpawn, this, spot);
764
765         if (autocvar_spawn_debug)
766         {
767                 sprint(this, strcat("spawnpoint origin:  ", vtos(spot.origin), "\n"));
768                 delete(spot); // usefull for checking if there are spawnpoints, that let drop through the floor
769         }
770
771         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
772         {
773                 .entity weaponentity = weaponentities[slot];
774                 if(slot == 0 || autocvar_g_weaponswitch_debug == 1)
775                         this.(weaponentity).m_switchweapon = w_getbestweapon(this, weaponentity);
776                 else
777                         this.(weaponentity).m_switchweapon = WEP_Null;
778                 this.(weaponentity).m_weapon = WEP_Null;
779                 this.(weaponentity).weaponname = "";
780                 this.(weaponentity).m_switchingweapon = WEP_Null;
781                 this.(weaponentity).cnt = -1;
782         }
783
784         MUTATOR_CALLHOOK(PlayerWeaponSelect, this);
785
786         if (CS(this).impulse) ImpulseCommands(this);
787
788         W_ResetGunAlign(this, CS_CVAR(this).cvar_cl_gunalign);
789         for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
790         {
791                 .entity weaponentity = weaponentities[slot];
792                 W_WeaponFrame(this, weaponentity);
793         }
794
795         if (!warmup_stage && !this.alivetime)
796                 this.alivetime = time;
797
798         antilag_clear(this, CS(this));
799 }
800
801 /** Called when a client spawns in the server */
802 void PutClientInServer(entity this)
803 {
804         if (IS_BOT_CLIENT(this)) {
805                 TRANSMUTE(Player, this);
806         } else if (IS_REAL_CLIENT(this)) {
807                 msg_entity = this;
808                 WriteByte(MSG_ONE, SVC_SETVIEW);
809                 WriteEntity(MSG_ONE, this);
810         }
811         if (game_stopped)
812                 TRANSMUTE(Observer, this);
813
814         SetSpectatee(this, NULL);
815
816         // reset player keys
817         if(PS(this))
818                 PS(this).itemkeys = 0;
819
820         MUTATOR_CALLHOOK(PutClientInServer, this);
821
822         if (IS_OBSERVER(this)) {
823                 PutObserverInServer(this);
824         } else if (IS_PLAYER(this)) {
825                 PutPlayerInServer(this);
826         }
827 }
828
829 // TODO do we need all these fields, or should we stop autodetecting runtime
830 // changes and just have a console command to update this?
831 bool ClientInit_SendEntity(entity this, entity to, int sf)
832 {
833         WriteHeader(MSG_ENTITY, _ENT_CLIENT_INIT);
834         return = true;
835         msg_entity = to;
836         // MSG_INIT replacement
837         // TODO: make easier to use
838         Registry_send_all();
839         W_PROP_reload(MSG_ONE, to);
840         ClientInit_misc(this);
841         MUTATOR_CALLHOOK(Ent_Init);
842 }
843 void ClientInit_misc(entity this)
844 {
845         int channel = MSG_ONE;
846         WriteHeader(channel, ENT_CLIENT_INIT);
847         WriteByte(channel, g_nexball_meter_period * 32);
848         WriteInt24_t(channel, compressShotOrigin(hook_shotorigin[0]));
849         WriteInt24_t(channel, compressShotOrigin(hook_shotorigin[1]));
850         WriteInt24_t(channel, compressShotOrigin(hook_shotorigin[2]));
851         WriteInt24_t(channel, compressShotOrigin(hook_shotorigin[3]));
852         WriteInt24_t(channel, compressShotOrigin(arc_shotorigin[0]));
853         WriteInt24_t(channel, compressShotOrigin(arc_shotorigin[1]));
854         WriteInt24_t(channel, compressShotOrigin(arc_shotorigin[2]));
855         WriteInt24_t(channel, compressShotOrigin(arc_shotorigin[3]));
856
857         if(autocvar_sv_foginterval && world.fog != "")
858                 WriteString(channel, world.fog);
859         else
860                 WriteString(channel, "");
861         WriteByte(channel, this.count * 255.0); // g_balance_armor_blockpercent
862         WriteByte(channel, this.cnt * 255.0); // g_balance_damagepush_speedfactor
863         WriteByte(channel, serverflags);
864         WriteCoord(channel, autocvar_g_trueaim_minrange);
865 }
866
867 void ClientInit_CheckUpdate(entity this)
868 {
869         this.nextthink = time;
870         if(this.count != autocvar_g_balance_armor_blockpercent)
871         {
872                 this.count = autocvar_g_balance_armor_blockpercent;
873                 this.SendFlags |= 1;
874         }
875         if(this.cnt != autocvar_g_balance_damagepush_speedfactor)
876         {
877                 this.cnt = autocvar_g_balance_damagepush_speedfactor;
878                 this.SendFlags |= 1;
879         }
880 }
881
882 void ClientInit_Spawn()
883 {
884         entity e = new_pure(clientinit);
885         setthink(e, ClientInit_CheckUpdate);
886         Net_LinkEntity(e, false, 0, ClientInit_SendEntity);
887
888         ClientInit_CheckUpdate(e);
889 }
890
891 /*
892 =============
893 SetNewParms
894 =============
895 */
896 void SetNewParms ()
897 {
898         // initialize parms for a new player
899         parm1 = -(86400 * 366);
900
901         MUTATOR_CALLHOOK(SetNewParms);
902 }
903
904 /*
905 =============
906 SetChangeParms
907 =============
908 */
909 void SetChangeParms (entity this)
910 {
911         // save parms for level change
912         parm1 = CS(this).parm_idlesince - time;
913
914         MUTATOR_CALLHOOK(SetChangeParms);
915 }
916
917 /*
918 =============
919 DecodeLevelParms
920 =============
921 */
922 void DecodeLevelParms(entity this)
923 {
924         // load parms
925         CS(this).parm_idlesince = parm1;
926         if (CS(this).parm_idlesince == -(86400 * 366))
927                 CS(this).parm_idlesince = time;
928
929         // whatever happens, allow 60 seconds of idling directly after connect for map loading
930         CS(this).parm_idlesince = max(CS(this).parm_idlesince, time - autocvar_sv_maxidle + 60);
931
932         MUTATOR_CALLHOOK(DecodeLevelParms);
933 }
934
935 void FixClientCvars(entity e)
936 {
937         // send prediction settings to the client
938         stuffcmd(e, "\nin_bindmap 0 0\n");
939         if(autocvar_g_antilag == 3) // client side hitscan
940                 stuffcmd(e, "cl_cmd settemp cl_prydoncursor_notrace 0\n");
941         if(autocvar_sv_gentle)
942                 stuffcmd(e, "cl_cmd settemp cl_gentle 1\n");
943
944         stuffcmd(e, sprintf("\ncl_jumpspeedcap_min \"%s\"\n", autocvar_sv_jumpspeedcap_min));
945         stuffcmd(e, sprintf("\ncl_jumpspeedcap_max \"%s\"\n", autocvar_sv_jumpspeedcap_max));
946
947         stuffcmd(e, sprintf("\ncl_shootfromfixedorigin \"%s\"\n", autocvar_g_shootfromfixedorigin));
948
949         MUTATOR_CALLHOOK(FixClientCvars, e);
950 }
951
952 bool findinlist_abbrev(string tofind, string list)
953 {
954         if(list == "" || tofind == "")
955                 return false; // empty list or search, just return
956
957         // this function allows abbreviated strings!
958         FOREACH_WORD(list, it == substring(tofind, 0, strlen(it)),
959         {
960                 return true;
961         });
962
963         return false;
964 }
965
966 bool PlayerInIPList(entity p, string iplist)
967 {
968         // some safety checks (never allow local?)
969         if(p.netaddress == "local" || p.netaddress == "" || !IS_REAL_CLIENT(p))
970                 return false;
971
972         return findinlist_abbrev(p.netaddress, iplist);
973 }
974
975 bool PlayerInIDList(entity p, string idlist)
976 {
977         // NOTE: we do NOT check crypto_idfp_signed here, an unsigned ID is fine too for this
978         if(!p.crypto_idfp)
979                 return false;
980
981         return findinlist_abbrev(p.crypto_idfp, idlist);
982 }
983
984 bool PlayerInList(entity player, string list)
985 {
986         return boolean(PlayerInIDList(player, list) || PlayerInIPList(player, list));
987 }
988
989 #ifdef DP_EXT_PRECONNECT
990 /*
991 =============
992 ClientPreConnect
993
994 Called once (not at each match start) when a client begins a connection to the server
995 =============
996 */
997 void ClientPreConnect(entity this)
998 {
999         if(autocvar_sv_eventlog)
1000         {
1001                 GameLogEcho(sprintf(":connect:%d:%d:%s",
1002                         this.playerid,
1003                         etof(this),
1004                         ((IS_REAL_CLIENT(this)) ? this.netaddress : "bot")
1005                 ));
1006         }
1007 }
1008 #endif
1009
1010 string GetClientVersionMessage(entity this)
1011 {
1012         if (CS(this).version_mismatch) {
1013                 if(CS(this).version < autocvar_gameversion) {
1014                         return strcat("This is Xonotic ", autocvar_g_xonoticversion,
1015                                 "\n^3Your client version is outdated.\n\n\n### YOU WON'T BE ABLE TO PLAY ON THIS SERVER ###\n\n\nPlease update!!!^8");
1016                 } else {
1017                         return strcat("This is Xonotic ", autocvar_g_xonoticversion,
1018                                 "\n^3This server is using an outdated Xonotic version.\n\n\n ### THIS SERVER IS INCOMPATIBLE AND THUS YOU CANNOT JOIN ###.^8");
1019                 }
1020         } else {
1021                 return strcat("Welcome to Xonotic ", autocvar_g_xonoticversion);
1022         }
1023 }
1024
1025 string getwelcomemessage(entity this)
1026 {
1027         MUTATOR_CALLHOOK(BuildMutatorsPrettyString, "");
1028         string modifications = M_ARGV(0, string);
1029
1030         if(g_weaponarena)
1031         {
1032                 if(g_weaponarena_random)
1033                         modifications = strcat(modifications, ", ", ftos(g_weaponarena_random), " of ", g_weaponarena_list, " Arena");
1034                 else
1035                         modifications = strcat(modifications, ", ", g_weaponarena_list, " Arena");
1036         }
1037         else if(cvar("g_balance_blaster_weaponstartoverride") == 0)
1038                 modifications = strcat(modifications, ", No start weapons");
1039         if(cvar("sv_gravity") < stof(cvar_defstring("sv_gravity")))
1040                 modifications = strcat(modifications, ", Low gravity");
1041         if(g_weapon_stay && !g_cts)
1042                 modifications = strcat(modifications, ", Weapons stay");
1043         if(autocvar_g_jetpack)
1044                 modifications = strcat(modifications, ", Jet pack");
1045         if(autocvar_g_powerups == 0)
1046                 modifications = strcat(modifications, ", No powerups");
1047         if(autocvar_g_powerups > 0)
1048                 modifications = strcat(modifications, ", Powerups");
1049         modifications = substring(modifications, 2, strlen(modifications) - 2);
1050
1051         string versionmessage = GetClientVersionMessage(this);
1052         string s = strcat(versionmessage, "^8\n^8\nhost is ^9", autocvar_hostname, "^8\n");
1053
1054         s = strcat(s, "^8\nmatch type is ^1", gamemode_name, "^8\n");
1055
1056         if(modifications != "")
1057                 s = strcat(s, "^8\nactive modifications: ^3", modifications, "^8\n");
1058
1059         if(cache_lastmutatormsg != autocvar_g_mutatormsg)
1060         {
1061                 strcpy(cache_lastmutatormsg, autocvar_g_mutatormsg);
1062                 strcpy(cache_mutatormsg, cache_lastmutatormsg);
1063         }
1064
1065         if (cache_mutatormsg != "") {
1066                 s = strcat(s, "\n\n^8special gameplay tips: ^7", cache_mutatormsg);
1067         }
1068
1069         string mutator_msg = "";
1070         MUTATOR_CALLHOOK(BuildGameplayTipsString, mutator_msg);
1071         mutator_msg = M_ARGV(0, string);
1072
1073         s = strcat(s, mutator_msg); // trust that the mutator will do proper formatting
1074
1075         string motd = autocvar_sv_motd;
1076         if (motd != "") {
1077                 s = strcat(s, "\n\n^8MOTD: ^7", strreplace("\\n", "\n", motd));
1078         }
1079         return s;
1080 }
1081
1082 bool autocvar_sv_qcphysics = true; // TODO this is for testing - remove when qcphysics work
1083
1084 /**
1085 =============
1086 ClientConnect
1087
1088 Called when a client connects to the server
1089 =============
1090 */
1091 void ClientConnect(entity this)
1092 {
1093         if (Ban_MaybeEnforceBanOnce(this)) return;
1094         assert(!IS_CLIENT(this), return);
1095         this.flags |= FL_CLIENT;
1096         assert(player_count >= 0, player_count = 0);
1097
1098         TRANSMUTE(Client, this);
1099         CS(this).version_nagtime = time + 10 + random() * 10;
1100
1101         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_JOIN_CONNECT, this.netname);
1102
1103         bot_clientconnect(this);
1104
1105         Player_DetermineForcedTeam(this);
1106
1107         TRANSMUTE(Observer, this);
1108
1109         PlayerStats_GameReport_AddEvent(sprintf("kills-%d", this.playerid));
1110
1111         // always track bots, don't ask for cl_allow_uidtracking
1112         if (IS_BOT_CLIENT(this))
1113                 PlayerStats_GameReport_AddPlayer(this);
1114         else
1115                 CS(this).allowed_timeouts = autocvar_sv_timeout_number;
1116
1117         if (autocvar_sv_eventlog)
1118                 GameLogEcho(strcat(":join:", ftos(this.playerid), ":", ftos(etof(this)), ":", ((IS_REAL_CLIENT(this)) ? GameLog_ProcessIP(this.netaddress) : "bot"), ":", playername(this.netname, this.team, false)));
1119
1120         CS(this).just_joined = true;  // stop spamming the eventlog with additional lines when the client connects
1121
1122         stuffcmd(this, clientstuff, "\n");
1123         stuffcmd(this, "cl_particles_reloadeffects\n"); // TODO do we still need this?
1124
1125         FixClientCvars(this);
1126
1127         // get version info from player
1128         stuffcmd(this, "cmd clientversion $gameversion\n");
1129
1130         // notify about available teams
1131         if (teamplay)
1132         {
1133                 entity balance = TeamBalance_CheckAllowedTeams(this);
1134                 int t = TeamBalance_GetAllowedTeams(balance);
1135                 TeamBalance_Destroy(balance);
1136                 stuffcmd(this, sprintf("set _teams_available %d\n", t));
1137         }
1138         else
1139         {
1140                 stuffcmd(this, "set _teams_available 0\n");
1141         }
1142
1143         bot_relinkplayerlist();
1144
1145         CS(this).spectatortime = time;
1146         if (blockSpectators)
1147         {
1148                 Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_SPECTATE_WARNING, autocvar_g_maxplayers_spectator_blocktime);
1149         }
1150
1151         CS(this).jointime = time;
1152
1153         if (IS_REAL_CLIENT(this))
1154         {
1155                 if (g_weaponarena_weapons == WEPSET(TUBA))
1156                         stuffcmd(this, "cl_cmd settemp chase_active 1\n");
1157         }
1158
1159         if (!autocvar_sv_foginterval && world.fog != "")
1160                 stuffcmd(this, strcat("\nfog ", world.fog, "\nr_fog_exp2 0\nr_drawfog 1\n"));
1161
1162         if (autocvar_sv_teamnagger && !(autocvar_bot_vs_human && AvailableTeams() == 2))
1163                 if(!MUTATOR_CALLHOOK(HideTeamNagger, this))
1164                         send_CSQC_teamnagger();
1165
1166         CSQCMODEL_AUTOINIT(this);
1167
1168         CS(this).model_randomizer = random();
1169
1170         if (IS_REAL_CLIENT(this))
1171                 sv_notice_join(this);
1172
1173         this.move_qcphysics = autocvar_sv_qcphysics;
1174
1175         // update physics stats (players can spawn before physics runs)
1176         Physics_UpdateStats(this);
1177
1178         IL_EACH(g_initforplayer, it.init_for_player, {
1179                 it.init_for_player(it, this);
1180         });
1181
1182         Handicap_Initialize(this);
1183
1184         MUTATOR_CALLHOOK(ClientConnect, this);
1185
1186         if (IS_REAL_CLIENT(this))
1187         {
1188                 if (!autocvar_g_campaign && !IS_PLAYER(this))
1189                 {
1190                         CS(this).motd_actived_time = -1;
1191                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_MOTD, getwelcomemessage(this));
1192                 }
1193         }
1194 }
1195 /*
1196 =============
1197 ClientDisconnect
1198
1199 Called when a client disconnects from the server
1200 =============
1201 */
1202 .entity chatbubbleentity;
1203 void player_powerups_remove_all(entity this);
1204
1205 void ClientDisconnect(entity this)
1206 {
1207         assert(IS_CLIENT(this), return);
1208
1209         PlayerStats_GameReport_FinalizePlayer(this);
1210         if (this.vehicle) vehicles_exit(this.vehicle, VHEF_RELEASE);
1211         if (CS(this).active_minigame) part_minigame(this);
1212         if (IS_PLAYER(this)) Send_Effect(EFFECT_SPAWN_NEUTRAL, this.origin, '0 0 0', 1);
1213
1214         if (autocvar_sv_eventlog)
1215                 GameLogEcho(strcat(":part:", ftos(this.playerid)));
1216
1217         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_DISCONNECT, this.netname);
1218
1219         if(IS_SPEC(this))
1220                 SetSpectatee(this, NULL);
1221
1222         MUTATOR_CALLHOOK(ClientDisconnect, this);
1223
1224         strfree(CS(this).netname_previous); // needs to be before the CS entity is removed!
1225         strfree(CS_CVAR(this).weaponorder_byimpulse);
1226         ClientState_detach(this);
1227
1228         Portal_ClearAll(this);
1229
1230         Unfreeze(this, false);
1231
1232         RemoveGrapplingHooks(this);
1233
1234         // Here, everything has been done that requires this player to be a client.
1235
1236         this.flags &= ~FL_CLIENT;
1237
1238         if (this.chatbubbleentity) delete(this.chatbubbleentity);
1239         if (this.killindicator) delete(this.killindicator);
1240
1241         IL_EACH(g_counters, it.realowner == this,
1242         {
1243                 delete(it);
1244         });
1245
1246         WaypointSprite_PlayerGone(this);
1247
1248         bot_relinkplayerlist();
1249
1250         strfree(this.clientstatus);
1251         if (this.personal) delete(this.personal);
1252
1253         this.playerid = 0;
1254         ReadyCount();
1255         if (vote_called && IS_REAL_CLIENT(this)) VoteCount(false);
1256
1257         player_powerups_remove_all(this); // stop powerup sound
1258
1259         ONREMOVE(this);
1260 }
1261
1262 void ChatBubbleThink(entity this)
1263 {
1264         this.nextthink = time;
1265         if ((this.owner.alpha < 0) || this.owner.chatbubbleentity != this)
1266         {
1267                 if(this.owner) // but why can that ever be NULL?
1268                         this.owner.chatbubbleentity = NULL;
1269                 delete(this);
1270                 return;
1271         }
1272
1273         this.mdl = "";
1274
1275         if ( !IS_DEAD(this.owner) && IS_PLAYER(this.owner) )
1276         {
1277                 if ( CS(this.owner).active_minigame && PHYS_INPUT_BUTTON_MINIGAME(this.owner) )
1278                         this.mdl = "models/sprites/minigame_busy.iqm";
1279                 else if (PHYS_INPUT_BUTTON_CHAT(this.owner))
1280                         this.mdl = "models/misc/chatbubble.spr";
1281         }
1282
1283         if ( this.model != this.mdl )
1284                 _setmodel(this, this.mdl);
1285
1286 }
1287
1288 void UpdateChatBubble(entity this)
1289 {
1290         if (this.alpha < 0)
1291                 return;
1292         // spawn a chatbubble entity if needed
1293         if (!this.chatbubbleentity)
1294         {
1295                 this.chatbubbleentity = new(chatbubbleentity);
1296                 this.chatbubbleentity.owner = this;
1297                 this.chatbubbleentity.exteriormodeltoclient = this;
1298                 setthink(this.chatbubbleentity, ChatBubbleThink);
1299                 this.chatbubbleentity.nextthink = time;
1300                 setmodel(this.chatbubbleentity, MDL_CHAT); // precision set below
1301                 //setorigin(this.chatbubbleentity, this.origin + '0 0 15' + this.maxs_z * '0 0 1');
1302                 setorigin(this.chatbubbleentity, '0 0 15' + this.maxs_z * '0 0 1');
1303                 setattachment(this.chatbubbleentity, this, "");  // sticks to moving player better, also conserves bandwidth
1304                 this.chatbubbleentity.mdl = this.chatbubbleentity.model;
1305                 //this.chatbubbleentity.model = "";
1306                 this.chatbubbleentity.effects = EF_LOWPRECISION;
1307         }
1308 }
1309
1310 void calculate_player_respawn_time(entity this)
1311 {
1312         if(MUTATOR_CALLHOOK(CalculateRespawnTime, this))
1313                 return;
1314
1315         float gametype_setting_tmp;
1316         float sdelay_max = GAMETYPE_DEFAULTED_SETTING(respawn_delay_max);
1317         float sdelay_small = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small);
1318         float sdelay_large = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large);
1319         float sdelay_small_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small_count);
1320         float sdelay_large_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large_count);
1321         float waves = GAMETYPE_DEFAULTED_SETTING(respawn_waves);
1322
1323         float pcount = 1;  // Include myself whether or not team is already set right and I'm a "player".
1324         if (teamplay)
1325         {
1326                 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
1327                         if(it.team == this.team)
1328                                 ++pcount;
1329                 });
1330                 if (sdelay_small_count == 0)
1331                         sdelay_small_count = 1;
1332                 if (sdelay_large_count == 0)
1333                         sdelay_large_count = 1;
1334         }
1335         else
1336         {
1337                 FOREACH_CLIENT(IS_PLAYER(it) && it != this, {
1338                         ++pcount;
1339                 });
1340                 if (sdelay_small_count == 0)
1341                 {
1342                         if (IS_INDEPENDENT_PLAYER(this))
1343                         {
1344                                 // Players play independently. No point in requiring enemies.
1345                                 sdelay_small_count = 1;
1346                         }
1347                         else
1348                         {
1349                                 // Players play AGAINST each other. Enemies required.
1350                                 sdelay_small_count = 2;
1351                         }
1352                 }
1353                 if (sdelay_large_count == 0)
1354                 {
1355                         if (IS_INDEPENDENT_PLAYER(this))
1356                         {
1357                                 // Players play independently. No point in requiring enemies.
1358                                 sdelay_large_count = 1;
1359                         }
1360                         else
1361                         {
1362                                 // Players play AGAINST each other. Enemies required.
1363                                 sdelay_large_count = 2;
1364                         }
1365                 }
1366         }
1367
1368         float sdelay;
1369
1370         if (pcount <= sdelay_small_count)
1371                 sdelay = sdelay_small;
1372         else if (pcount >= sdelay_large_count)
1373                 sdelay = sdelay_large;
1374         else  // NOTE: this case implies sdelay_large_count > sdelay_small_count.
1375                 sdelay = sdelay_small + (sdelay_large - sdelay_small) * (pcount - sdelay_small_count) / (sdelay_large_count - sdelay_small_count);
1376
1377         if(waves)
1378                 this.respawn_time = ceil((time + sdelay) / waves) * waves;
1379         else
1380                 this.respawn_time = time + sdelay;
1381
1382         if(sdelay < sdelay_max)
1383                 this.respawn_time_max = time + sdelay_max;
1384         else
1385                 this.respawn_time_max = this.respawn_time;
1386
1387         if((sdelay + waves >= 5.0) && (this.respawn_time - time > 1.75))
1388                 this.respawn_countdown = 10; // first number to count down from is 10
1389         else
1390                 this.respawn_countdown = -1; // do not count down
1391
1392         if(autocvar_g_forced_respawn)
1393                 this.respawn_flags = this.respawn_flags | RESPAWN_FORCE;
1394 }
1395
1396 // LordHavoc: this hack will be removed when proper _pants/_shirt layers are
1397 // added to the model skins
1398 /*void UpdateColorModHack()
1399 {
1400         float c;
1401         c = this.clientcolors & 15;
1402         // LordHavoc: only bothering to support white, green, red, yellow, blue
1403              if (!teamplay) this.colormod = '0 0 0';
1404         else if (c ==  0) this.colormod = '1.00 1.00 1.00';
1405         else if (c ==  3) this.colormod = '0.10 1.73 0.10';
1406         else if (c ==  4) this.colormod = '1.73 0.10 0.10';
1407         else if (c == 12) this.colormod = '1.22 1.22 0.10';
1408         else if (c == 13) this.colormod = '0.10 0.10 1.73';
1409         else this.colormod = '1 1 1';
1410 }*/
1411
1412 void respawn(entity this)
1413 {
1414         bool damagedbycontents_prev = this.damagedbycontents;
1415         if(this.alpha >= 0)
1416         {
1417                 if(autocvar_g_respawn_ghosts)
1418                 {
1419                         this.solid = SOLID_NOT;
1420                         this.takedamage = DAMAGE_NO;
1421                         this.damagedbycontents = false;
1422                         set_movetype(this, MOVETYPE_FLY);
1423                         this.velocity = '0 0 1' * autocvar_g_respawn_ghosts_speed;
1424                         this.avelocity = randomvec() * autocvar_g_respawn_ghosts_speed * 3 - randomvec() * autocvar_g_respawn_ghosts_speed * 3;
1425                         this.effects |= CSQCMODEL_EF_RESPAWNGHOST;
1426                         this.alpha = min(this.alpha, autocvar_g_respawn_ghosts_alpha);
1427                         Send_Effect(EFFECT_RESPAWN_GHOST, this.origin, '0 0 0', 1);
1428                         if(autocvar_g_respawn_ghosts_time > 0)
1429                                 SUB_SetFade(this, time + autocvar_g_respawn_ghosts_time, autocvar_g_respawn_ghosts_fadetime);
1430                 }
1431                 else
1432                         SUB_SetFade (this, time, 1); // fade out the corpse immediately
1433         }
1434
1435         CopyBody(this, 1);
1436         this.damagedbycontents = damagedbycontents_prev;
1437
1438         this.effects |= EF_NODRAW; // prevent another CopyBody
1439         PutClientInServer(this);
1440 }
1441
1442 void play_countdown(entity this, float finished, Sound samp)
1443 {
1444         TC(Sound, samp);
1445         if(IS_REAL_CLIENT(this))
1446                 if(floor(finished - time - frametime) != floor(finished - time))
1447                         if(finished - time < 6)
1448                                 sound (this, CH_INFO, samp, VOL_BASE, ATTEN_NORM);
1449 }
1450
1451 void player_powerups_remove_all(entity this)
1452 {
1453         if (this.items & (ITEM_Strength.m_itemid | ITEM_Shield.m_itemid | IT_SUPERWEAPON))
1454         {
1455                 // don't play the poweroff sound when the game restarts or the player disconnects
1456                 if (time > game_starttime + 1 && IS_CLIENT(this))
1457                         sound(this, CH_INFO, SND_POWEROFF, VOL_BASE, ATTEN_NORM);
1458                 stopsound(this, CH_TRIGGER_SINGLE); // get rid of the pickup sound
1459                 this.items &= ~ITEM_Strength.m_itemid;
1460                 this.items &= ~ITEM_Shield.m_itemid;
1461                 this.items -= (this.items & IT_SUPERWEAPON);
1462         }
1463 }
1464
1465 void player_powerups(entity this)
1466 {
1467         if((this.items & IT_USING_JETPACK) && !IS_DEAD(this) && !game_stopped)
1468                 this.modelflags |= MF_ROCKET;
1469         else
1470                 this.modelflags &= ~MF_ROCKET;
1471
1472         this.effects &= ~(EF_RED | EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT | EF_FLAME | EF_NODEPTHTEST);
1473
1474         if (IS_DEAD(this))
1475                 player_powerups_remove_all(this);
1476
1477         if((this.alpha < 0 || IS_DEAD(this)) && !this.vehicle) // don't apply the flags if the player is gibbed
1478                 return;
1479
1480         // add a way to see what the items were BEFORE all of these checks for the mutator hook
1481         int items_prev = this.items;
1482
1483         Fire_ApplyDamage(this);
1484         Fire_ApplyEffect(this);
1485
1486         if (!MUTATOR_IS_ENABLED(mutator_instagib))
1487         {
1488                 if (this.items & ITEM_Strength.m_itemid)
1489                 {
1490                         play_countdown(this, STAT(STRENGTH_FINISHED, this), SND_POWEROFF);
1491                         this.effects = this.effects | (EF_BLUE | EF_ADDITIVE | EF_FULLBRIGHT);
1492                         if (time > STAT(STRENGTH_FINISHED, this))
1493                         {
1494                                 this.items = this.items - (this.items & ITEM_Strength.m_itemid);
1495                                 //Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_POWERDOWN_STRENGTH, this.netname);
1496                                 Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_POWERDOWN_STRENGTH);
1497                         }
1498                 }
1499                 else
1500                 {
1501                         if (time < STAT(STRENGTH_FINISHED, this))
1502                         {
1503                                 this.items = this.items | ITEM_Strength.m_itemid;
1504                                 if(!g_cts)
1505                                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_POWERUP_STRENGTH, this.netname);
1506                                 Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_POWERUP_STRENGTH);
1507                         }
1508                 }
1509                 if (this.items & ITEM_Shield.m_itemid)
1510                 {
1511                         play_countdown(this, STAT(INVINCIBLE_FINISHED, this), SND_POWEROFF);
1512                         this.effects = this.effects | (EF_RED | EF_ADDITIVE | EF_FULLBRIGHT);
1513                         if (time > STAT(INVINCIBLE_FINISHED, this))
1514                         {
1515                                 this.items = this.items - (this.items & ITEM_Shield.m_itemid);
1516                                 //Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_POWERDOWN_SHIELD, this.netname);
1517                                 Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_POWERDOWN_SHIELD);
1518                         }
1519                 }
1520                 else
1521                 {
1522                         if (time < STAT(INVINCIBLE_FINISHED, this))
1523                         {
1524                                 this.items = this.items | ITEM_Shield.m_itemid;
1525                                 if(!g_cts)
1526                                         Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_POWERUP_SHIELD, this.netname);
1527                                 Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_POWERUP_SHIELD);
1528                         }
1529                 }
1530                 if (this.items & IT_SUPERWEAPON)
1531                 {
1532                         if (!(STAT(WEAPONS, this) & WEPSET_SUPERWEAPONS))
1533                         {
1534                                 STAT(SUPERWEAPONS_FINISHED, this) = 0;
1535                                 this.items = this.items - (this.items & IT_SUPERWEAPON);
1536                                 //Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_SUPERWEAPON_LOST, this.netname);
1537                                 Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_SUPERWEAPON_LOST);
1538                         }
1539                         else if (this.items & IT_UNLIMITED_SUPERWEAPONS)
1540                         {
1541                                 // don't let them run out
1542                         }
1543                         else
1544                         {
1545                                 play_countdown(this, STAT(SUPERWEAPONS_FINISHED, this), SND_POWEROFF);
1546                                 if (time > STAT(SUPERWEAPONS_FINISHED, this))
1547                                 {
1548                                         this.items = this.items - (this.items & IT_SUPERWEAPON);
1549                                         STAT(WEAPONS, this) &= ~WEPSET_SUPERWEAPONS;
1550                                         //Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_SUPERWEAPON_BROKEN, this.netname);
1551                                         Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_SUPERWEAPON_BROKEN);
1552                                 }
1553                         }
1554                 }
1555                 else if(STAT(WEAPONS, this) & WEPSET_SUPERWEAPONS)
1556                 {
1557                         if (time < STAT(SUPERWEAPONS_FINISHED, this) || (this.items & IT_UNLIMITED_SUPERWEAPONS))
1558                         {
1559                                 this.items = this.items | IT_SUPERWEAPON;
1560                                 if(!(this.items & IT_UNLIMITED_SUPERWEAPONS))
1561                                 {
1562                                         if(!g_cts)
1563                                                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_SUPERWEAPON_PICKUP, this.netname);
1564                                         Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_SUPERWEAPON_PICKUP);
1565                                 }
1566                         }
1567                         else
1568                         {
1569                                 STAT(SUPERWEAPONS_FINISHED, this) = 0;
1570                                 STAT(WEAPONS, this) &= ~WEPSET_SUPERWEAPONS;
1571                         }
1572                 }
1573                 else
1574                 {
1575                         STAT(SUPERWEAPONS_FINISHED, this) = 0;
1576                 }
1577         }
1578
1579         if(autocvar_g_nodepthtestplayers)
1580                 this.effects = this.effects | EF_NODEPTHTEST;
1581
1582         if(autocvar_g_fullbrightplayers)
1583                 this.effects = this.effects | EF_FULLBRIGHT;
1584
1585         if (time >= game_starttime)
1586         if (time < this.spawnshieldtime)
1587                 this.effects = this.effects | (EF_ADDITIVE | EF_FULLBRIGHT);
1588
1589         MUTATOR_CALLHOOK(PlayerPowerups, this, items_prev);
1590 }
1591
1592 float CalcRegen(float current, float stable, float regenfactor, float regenframetime)
1593 {
1594         if(current > stable)
1595                 return current;
1596         else if(current > stable - 0.25) // when close enough, "snap"
1597                 return stable;
1598         else
1599                 return min(stable, current + (stable - current) * regenfactor * regenframetime);
1600 }
1601
1602 float CalcRot(float current, float stable, float rotfactor, float rotframetime)
1603 {
1604         if(current < stable)
1605                 return current;
1606         else if(current < stable + 0.25) // when close enough, "snap"
1607                 return stable;
1608         else
1609                 return max(stable, current + (stable - current) * rotfactor * rotframetime);
1610 }
1611
1612 void RotRegen(entity this, int res, float regenstable, float regenfactor, float regenlinear, float regenframetime, float rotstable, float rotfactor, float rotlinear, float rotframetime, float limit_mod)
1613 {
1614         float old = GetResource(this, res);
1615         float current = old;
1616         if(current > rotstable)
1617         {
1618                 if(rotframetime > 0)
1619                 {
1620                         current = CalcRot(current, rotstable, rotfactor, rotframetime);
1621                         current = max(rotstable, current - rotlinear * rotframetime);
1622                 }
1623         }
1624         else if(current < regenstable)
1625         {
1626                 if(regenframetime > 0)
1627                 {
1628                         current = CalcRegen(current, regenstable, regenfactor, regenframetime);
1629                         current = min(regenstable, current + regenlinear * regenframetime);
1630                 }
1631         }
1632
1633         float limit = GetResourceLimit(this, res) * limit_mod;
1634         if(current > limit)
1635                 current = limit;
1636
1637         if (current != old)
1638                 SetResource(this, res, current);
1639 }
1640
1641 void player_regen(entity this)
1642 {
1643         float max_mod, regen_mod, rot_mod, limit_mod;
1644         max_mod = regen_mod = rot_mod = limit_mod = 1;
1645
1646         float regen_health = autocvar_g_balance_health_regen;
1647         float regen_health_linear = autocvar_g_balance_health_regenlinear;
1648         float regen_health_rot = autocvar_g_balance_health_rot;
1649         float regen_health_rotlinear = autocvar_g_balance_health_rotlinear;
1650         float regen_health_stable = autocvar_g_balance_health_regenstable;
1651         float regen_health_rotstable = autocvar_g_balance_health_rotstable;
1652         bool mutator_returnvalue = MUTATOR_CALLHOOK(PlayerRegen, this, max_mod, regen_mod, rot_mod, limit_mod, regen_health, regen_health_linear, regen_health_rot,
1653                 regen_health_rotlinear, regen_health_stable, regen_health_rotstable);
1654         max_mod = M_ARGV(1, float);
1655         regen_mod = M_ARGV(2, float);
1656         rot_mod = M_ARGV(3, float);
1657         limit_mod = M_ARGV(4, float);
1658         regen_health = M_ARGV(5, float);
1659         regen_health_linear = M_ARGV(6, float);
1660         regen_health_rot = M_ARGV(7, float);
1661         regen_health_rotlinear = M_ARGV(8, float);
1662         regen_health_stable = M_ARGV(9, float);
1663         regen_health_rotstable = M_ARGV(10, float);
1664
1665         if(!mutator_returnvalue)
1666         if(!STAT(FROZEN, this))
1667         {
1668                 float maxa = autocvar_g_balance_armor_rotstable;
1669                 float mina = autocvar_g_balance_armor_regenstable;
1670
1671                 RotRegen(this, RES_ARMOR, mina, autocvar_g_balance_armor_regen, autocvar_g_balance_armor_regenlinear,
1672                         regen_mod * frametime * (time > this.pauseregen_finished), maxa, autocvar_g_balance_armor_rot, autocvar_g_balance_armor_rotlinear,
1673                         rot_mod * frametime * (time > this.pauserotarmor_finished), limit_mod);
1674
1675                 RotRegen(this, RES_HEALTH, regen_health_stable * max_mod, regen_health, regen_health_linear,
1676                         regen_mod * frametime * (time > this.pauseregen_finished), regen_health_rotstable * max_mod, regen_health_rot, regen_health_rotlinear,
1677                         rot_mod * frametime * (time > this.pauserothealth_finished), limit_mod);
1678         }
1679
1680         // if player rotted to death...  die!
1681         // check this outside above checks, as player may still be able to rot to death
1682         if(GetResource(this, RES_HEALTH) < 1)
1683         {
1684                 if(this.vehicle)
1685                         vehicles_exit(this.vehicle, VHEF_RELEASE);
1686                 if(this.event_damage)
1687                         this.event_damage(this, this, this, 1, DEATH_ROT.m_id, DMG_NOWEP, this.origin, '0 0 0');
1688         }
1689
1690         if (!(this.items & IT_UNLIMITED_AMMO))
1691         {
1692                 float maxf = autocvar_g_balance_fuel_rotstable;
1693                 float minf = autocvar_g_balance_fuel_regenstable;
1694
1695                 RotRegen(this, RES_FUEL, minf, autocvar_g_balance_fuel_regen, autocvar_g_balance_fuel_regenlinear,
1696                         frametime * (time > this.pauseregen_finished) * ((this.items & ITEM_JetpackRegen.m_itemid) != 0),
1697                         maxf, autocvar_g_balance_fuel_rot, autocvar_g_balance_fuel_rotlinear, frametime * (time > this.pauserotfuel_finished), 1);
1698         }
1699 }
1700
1701 bool zoomstate_set;
1702 void SetZoomState(entity this, float newzoom)
1703 {
1704         if(newzoom != CS(this).zoomstate)
1705         {
1706                 CS(this).zoomstate = newzoom;
1707                 ClientData_Touch(this);
1708         }
1709         zoomstate_set = true;
1710 }
1711
1712 void GetPressedKeys(entity this)
1713 {
1714         MUTATOR_CALLHOOK(GetPressedKeys, this);
1715         if (game_stopped)
1716         {
1717                 CS(this).pressedkeys = 0;
1718                 STAT(PRESSED_KEYS, this) = 0;
1719                 return;
1720         }
1721
1722         // NOTE: GetPressedKeys and PM_dodging_GetPressedKeys use similar code
1723         int keys = STAT(PRESSED_KEYS, this);
1724         keys = BITSET(keys, KEY_FORWARD,        CS(this).movement.x > 0);
1725         keys = BITSET(keys, KEY_BACKWARD,       CS(this).movement.x < 0);
1726         keys = BITSET(keys, KEY_RIGHT,          CS(this).movement.y > 0);
1727         keys = BITSET(keys, KEY_LEFT,           CS(this).movement.y < 0);
1728
1729         keys = BITSET(keys, KEY_JUMP,           PHYS_INPUT_BUTTON_JUMP(this));
1730         keys = BITSET(keys, KEY_CROUCH,         IS_DUCKED(this)); // workaround: player can't un-crouch until their path is clear, so we keep the button held here
1731         keys = BITSET(keys, KEY_ATCK,           PHYS_INPUT_BUTTON_ATCK(this));
1732         keys = BITSET(keys, KEY_ATCK2,          PHYS_INPUT_BUTTON_ATCK2(this));
1733         CS(this).pressedkeys = keys; // store for other users
1734
1735         STAT(PRESSED_KEYS, this) = keys;
1736 }
1737
1738 /*
1739 ======================
1740 spectate mode routines
1741 ======================
1742 */
1743
1744 void SpectateCopy(entity this, entity spectatee)
1745 {
1746         TC(Client, this); TC(Client, spectatee);
1747
1748         MUTATOR_CALLHOOK(SpectateCopy, spectatee, this);
1749         PS(this) = PS(spectatee);
1750         this.armortype = spectatee.armortype;
1751         SetResourceExplicit(this, RES_ARMOR, GetResource(spectatee, RES_ARMOR));
1752         SetResourceExplicit(this, RES_CELLS, GetResource(spectatee, RES_CELLS));
1753         SetResourceExplicit(this, RES_PLASMA, GetResource(spectatee, RES_PLASMA));
1754         SetResourceExplicit(this, RES_SHELLS, GetResource(spectatee, RES_SHELLS));
1755         SetResourceExplicit(this, RES_BULLETS, GetResource(spectatee, RES_BULLETS));
1756         SetResourceExplicit(this, RES_ROCKETS, GetResource(spectatee, RES_ROCKETS));
1757         SetResourceExplicit(this, RES_FUEL, GetResource(spectatee, RES_FUEL));
1758         this.effects = spectatee.effects & EFMASK_CHEAP; // eat performance
1759         SetResourceExplicit(this, RES_HEALTH, GetResource(spectatee, RES_HEALTH));
1760         CS(this).impulse = 0;
1761         this.disableclientprediction = 1; // no need to run prediction on a spectator
1762         this.items = spectatee.items;
1763         STAT(LAST_PICKUP, this) = STAT(LAST_PICKUP, spectatee);
1764         STAT(HIT_TIME, this) = STAT(HIT_TIME, spectatee);
1765         STAT(STRENGTH_FINISHED, this) = STAT(STRENGTH_FINISHED, spectatee);
1766         STAT(INVINCIBLE_FINISHED, this) = STAT(INVINCIBLE_FINISHED, spectatee);
1767         STAT(SUPERWEAPONS_FINISHED, this) = STAT(SUPERWEAPONS_FINISHED, spectatee);
1768         STAT(AIR_FINISHED, this) = STAT(AIR_FINISHED, spectatee);
1769         STAT(PRESSED_KEYS, this) = STAT(PRESSED_KEYS, spectatee);
1770         STAT(WEAPONS, this) = STAT(WEAPONS, spectatee);
1771         this.punchangle = spectatee.punchangle;
1772         this.view_ofs = spectatee.view_ofs;
1773         this.velocity = spectatee.velocity;
1774         this.dmg_take = spectatee.dmg_take;
1775         this.dmg_save = spectatee.dmg_save;
1776         this.dmg_inflictor = spectatee.dmg_inflictor;
1777         this.v_angle = spectatee.v_angle;
1778         this.angles = spectatee.v_angle;
1779         STAT(FROZEN, this) = STAT(FROZEN, spectatee);
1780         STAT(REVIVE_PROGRESS, this) = STAT(REVIVE_PROGRESS, spectatee);
1781         this.viewloc = spectatee.viewloc;
1782         if(!PHYS_INPUT_BUTTON_USE(this) && STAT(CAMERA_SPECTATOR, this) != 2)
1783                 this.fixangle = true;
1784         setorigin(this, spectatee.origin);
1785         setsize(this, spectatee.mins, spectatee.maxs);
1786         SetZoomState(this, CS(spectatee).zoomstate);
1787
1788     anticheat_spectatecopy(this, spectatee);
1789         STAT(HUD, this) = STAT(HUD, spectatee);
1790         if(spectatee.vehicle)
1791     {
1792         this.angles = spectatee.v_angle;
1793
1794         //this.fixangle = false;
1795         //this.velocity = spectatee.vehicle.velocity;
1796         this.vehicle_health = spectatee.vehicle_health;
1797         this.vehicle_shield = spectatee.vehicle_shield;
1798         this.vehicle_energy = spectatee.vehicle_energy;
1799         this.vehicle_ammo1 = spectatee.vehicle_ammo1;
1800         this.vehicle_ammo2 = spectatee.vehicle_ammo2;
1801         this.vehicle_reload1 = spectatee.vehicle_reload1;
1802         this.vehicle_reload2 = spectatee.vehicle_reload2;
1803
1804         //msg_entity = this;
1805
1806        // WriteByte (MSG_ONE, SVC_SETVIEWANGLES);
1807             //WriteAngle(MSG_ONE,  spectatee.v_angle.x);
1808            // WriteAngle(MSG_ONE,  spectatee.v_angle.y);
1809            // WriteAngle(MSG_ONE,  spectatee.v_angle.z);
1810
1811         //WriteByte (MSG_ONE, SVC_SETVIEW);
1812         //    WriteEntity(MSG_ONE, this);
1813         //makevectors(spectatee.v_angle);
1814         //setorigin(this, spectatee.origin - v_forward * 400 + v_up * 300);*/
1815     }
1816 }
1817
1818 bool SpectateUpdate(entity this)
1819 {
1820         if(!this.enemy)
1821                 return false;
1822
1823         if(!IS_PLAYER(this.enemy) || this == this.enemy)
1824         {
1825                 SetSpectatee(this, NULL);
1826                 return false;
1827         }
1828
1829         SpectateCopy(this, this.enemy);
1830
1831         return true;
1832 }
1833
1834 bool SpectateSet(entity this)
1835 {
1836         if(!IS_PLAYER(this.enemy))
1837                 return false;
1838
1839         ClientData_Touch(this.enemy);
1840
1841         msg_entity = this;
1842         WriteByte(MSG_ONE, SVC_SETVIEW);
1843         WriteEntity(MSG_ONE, this.enemy);
1844         set_movetype(this, MOVETYPE_NONE);
1845         accuracy_resend(this);
1846
1847         if(!SpectateUpdate(this))
1848                 PutObserverInServer(this);
1849
1850         return true;
1851 }
1852
1853 void SetSpectatee_status(entity this, int spectatee_num)
1854 {
1855         int oldspectatee_status = CS(this).spectatee_status;
1856         CS(this).spectatee_status = spectatee_num;
1857
1858         if (CS(this).spectatee_status != oldspectatee_status)
1859         {
1860                 if (STAT(PRESSED_KEYS, this))
1861                 {
1862                         CS(this).pressedkeys = 0;
1863                         STAT(PRESSED_KEYS, this) = 0;
1864                 }
1865                 ClientData_Touch(this);
1866                 if (g_race || g_cts) race_InitSpectator();
1867         }
1868 }
1869
1870 void SetSpectatee(entity this, entity spectatee)
1871 {
1872         if(IS_BOT_CLIENT(this))
1873                 return; // bots abuse .enemy, this code is useless to them
1874
1875         entity old_spectatee = this.enemy;
1876
1877         this.enemy = spectatee;
1878
1879         // WEAPONTODO
1880         // these are required to fix the spectator bug with arc
1881         if(old_spectatee)
1882         {
1883                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1884                 {
1885                         .entity weaponentity = weaponentities[slot];
1886                         if(old_spectatee.(weaponentity).arc_beam)
1887                                 old_spectatee.(weaponentity).arc_beam.SendFlags |= ARC_SF_SETTINGS;
1888                 }
1889         }
1890         if(this.enemy)
1891         {
1892                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
1893                 {
1894                         .entity weaponentity = weaponentities[slot];
1895                         if(this.enemy.(weaponentity).arc_beam)
1896                                 this.enemy.(weaponentity).arc_beam.SendFlags |= ARC_SF_SETTINGS;
1897                 }
1898         }
1899
1900         if (this.enemy)
1901                 SetSpectatee_status(this, etof(this.enemy));
1902
1903         // needed to update spectator list
1904         if(old_spectatee) { ClientData_Touch(old_spectatee); }
1905 }
1906
1907 bool Spectate(entity this, entity pl)
1908 {
1909         if(MUTATOR_CALLHOOK(SpectateSet, this, pl))
1910                 return false;
1911         pl = M_ARGV(1, entity);
1912
1913         SetSpectatee(this, pl);
1914         return SpectateSet(this);
1915 }
1916
1917 bool SpectateNext(entity this)
1918 {
1919         entity ent = find(this.enemy, classname, STR_PLAYER);
1920
1921         if (MUTATOR_CALLHOOK(SpectateNext, this, ent))
1922                 ent = M_ARGV(1, entity);
1923         else if (!ent)
1924                 ent = find(ent, classname, STR_PLAYER);
1925
1926         if(ent) { SetSpectatee(this, ent); }
1927
1928         return SpectateSet(this);
1929 }
1930
1931 bool SpectatePrev(entity this)
1932 {
1933         // NOTE: chain order is from the highest to the lower entnum (unlike find)
1934         entity ent = findchain(classname, STR_PLAYER);
1935         if (!ent) // no player
1936                 return false;
1937
1938         entity first = ent;
1939         // skip players until current spectated player
1940         if(this.enemy)
1941         while(ent && ent != this.enemy)
1942                 ent = ent.chain;
1943
1944         switch (MUTATOR_CALLHOOK(SpectatePrev, this, ent, first))
1945         {
1946                 case MUT_SPECPREV_FOUND:
1947                         ent = M_ARGV(1, entity);
1948                         break;
1949                 case MUT_SPECPREV_RETURN:
1950                         return true;
1951                 case MUT_SPECPREV_CONTINUE:
1952                 default:
1953                 {
1954                         if(ent.chain)
1955                                 ent = ent.chain;
1956                         else
1957                                 ent = first;
1958                         break;
1959                 }
1960         }
1961
1962         SetSpectatee(this, ent);
1963         return SpectateSet(this);
1964 }
1965
1966 /*
1967 =============
1968 ShowRespawnCountdown()
1969
1970 Update a respawn countdown display.
1971 =============
1972 */
1973 void ShowRespawnCountdown(entity this)
1974 {
1975         float number;
1976         if(!IS_DEAD(this)) // just respawned?
1977                 return;
1978         else
1979         {
1980                 number = ceil(this.respawn_time - time);
1981                 if(number <= 0)
1982                         return;
1983                 if(number <= this.respawn_countdown)
1984                 {
1985                         this.respawn_countdown = number - 1;
1986                         if(ceil(this.respawn_time - (time + 0.5)) == number) // only say it if it is the same number even in 0.5s; to prevent overlapping sounds
1987                                 { Send_Notification(NOTIF_ONE, this, MSG_ANNCE, Announcer_PickNumber(CNT_RESPAWN, number)); }
1988                 }
1989         }
1990 }
1991
1992 .bool team_selected;
1993 bool ShowTeamSelection(entity this)
1994 {
1995         if (!teamplay || autocvar_g_campaign || autocvar_g_balance_teams || this.team_selected || (CS(this).wasplayer && autocvar_g_changeteam_banned) || Player_HasRealForcedTeam(this))
1996                 return false;
1997         stuffcmd(this, "menu_showteamselect\n");
1998         return true;
1999 }
2000 void Join(entity this)
2001 {
2002         TRANSMUTE(Player, this);
2003
2004         if(!this.team_selected)
2005         if(autocvar_g_campaign || autocvar_g_balance_teams)
2006                 TeamBalance_JoinBestTeam(this);
2007
2008         if(autocvar_g_campaign)
2009                 campaign_bots_may_start = true;
2010
2011         Kill_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CPID_PREVENT_JOIN);
2012
2013         PutClientInServer(this);
2014
2015         if(IS_PLAYER(this))
2016         if(teamplay && this.team != -1)
2017         {
2018         }
2019         else
2020                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_JOIN_PLAY, this.netname);
2021         this.team_selected = false;
2022 }
2023
2024 int GetPlayerLimit()
2025 {
2026         if(g_duel)
2027                 return 2; // TODO: this workaround is needed since the mutator hook from duel can't be activated before the gametype is loaded (e.g. switching modes via gametype vote screen)
2028         int player_limit = autocvar_g_maxplayers;
2029         MUTATOR_CALLHOOK(GetPlayerLimit, player_limit);
2030         player_limit = M_ARGV(0, int);
2031         return player_limit;
2032 }
2033
2034 /**
2035  * Determines whether the player is allowed to join. This depends on cvar
2036  * g_maxplayers, if it isn't used this function always return true, otherwise
2037  * it checks whether the number of currently playing players exceeds g_maxplayers.
2038  * @return int number of free slots for players, 0 if none
2039  */
2040 int nJoinAllowed(entity this, entity ignore)
2041 {
2042         if(!ignore)
2043         // this is called that way when checking if anyone may be able to join (to build qcstatus)
2044         // so report 0 free slots if restricted
2045         {
2046                 if(autocvar_g_forced_team_otherwise == "spectate")
2047                         return 0;
2048                 if(autocvar_g_forced_team_otherwise == "spectator")
2049                         return 0;
2050         }
2051
2052         if(this && (Player_GetForcedTeamIndex(this) == TEAM_FORCE_SPECTATOR))
2053                 return 0; // forced spectators can never join
2054
2055         // TODO simplify this
2056         int totalClients = 0;
2057         int currentlyPlaying = 0;
2058         FOREACH_CLIENT(true, {
2059                 if(it != ignore)
2060                         ++totalClients;
2061                 if(IS_REAL_CLIENT(it))
2062                 if(IS_PLAYER(it) || it.caplayer)
2063                         ++currentlyPlaying;
2064         });
2065
2066         int player_limit = GetPlayerLimit();
2067
2068         int free_slots = 0;
2069         if (!player_limit)
2070                 free_slots = maxclients - totalClients;
2071         else if(player_limit > 0 && currentlyPlaying < player_limit)
2072                 free_slots = min(maxclients - totalClients, player_limit - currentlyPlaying);
2073
2074         static float msg_time = 0;
2075         if(this && !this.caplayer && ignore && !free_slots && time > msg_time)
2076         {
2077                 Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_JOIN_PREVENT);
2078                 msg_time = time + 0.5;
2079         }
2080
2081         return free_slots;
2082 }
2083
2084 /**
2085  * Checks whether the client is an observer or spectator, if so, he will get kicked after
2086  * g_maxplayers_spectator_blocktime seconds
2087  */
2088 void checkSpectatorBlock(entity this)
2089 {
2090         if(IS_SPEC(this) || IS_OBSERVER(this))
2091         if(!this.caplayer)
2092         if(IS_REAL_CLIENT(this))
2093         {
2094                 if( time > (CS(this).spectatortime + autocvar_g_maxplayers_spectator_blocktime) ) {
2095                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_QUIT_KICK_SPECTATING);
2096                         dropclient(this);
2097                 }
2098         }
2099 }
2100
2101 void PrintWelcomeMessage(entity this)
2102 {
2103         if(CS(this).motd_actived_time == 0)
2104         {
2105                 if (autocvar_g_campaign) {
2106                         if ((IS_PLAYER(this) && PHYS_INPUT_BUTTON_INFO(this)) || (!IS_PLAYER(this))) {
2107                                 CS(this).motd_actived_time = time;
2108                                 Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_CAMPAIGN_MESSAGE, Campaign_GetMessage(), Campaign_GetLevelNum());
2109                         }
2110                 } else {
2111                         if (PHYS_INPUT_BUTTON_INFO(this)) {
2112                                 CS(this).motd_actived_time = time;
2113                                 Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_MOTD, getwelcomemessage(this));
2114                         }
2115                 }
2116         }
2117         else if(CS(this).motd_actived_time > 0) // showing MOTD or campaign message
2118         {
2119                 if (autocvar_g_campaign) {
2120                         if (PHYS_INPUT_BUTTON_INFO(this))
2121                                 CS(this).motd_actived_time = time;
2122                         else if ((time - CS(this).motd_actived_time > 2) && IS_PLAYER(this)) { // hide it some seconds after BUTTON_INFO has been released
2123                                 CS(this).motd_actived_time = 0;
2124                                 Kill_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CPID_CAMPAIGN_MESSAGE);
2125                         }
2126                 } else {
2127                         if (PHYS_INPUT_BUTTON_INFO(this))
2128                                 CS(this).motd_actived_time = time;
2129                         else if (time - CS(this).motd_actived_time > 2) { // hide it some seconds after BUTTON_INFO has been released
2130                                 CS(this).motd_actived_time = 0;
2131                                 Kill_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CPID_MOTD);
2132                         }
2133                 }
2134         }
2135         else //if(CS(this).motd_actived_time < 0) // just connected, motd is active
2136         {
2137                 if(PHYS_INPUT_BUTTON_INFO(this)) // BUTTON_INFO hides initial MOTD
2138                         CS(this).motd_actived_time = -2; // wait until BUTTON_INFO gets released
2139                 else if (CS(this).motd_actived_time == -2)
2140                 {
2141                         // instantly hide MOTD
2142                         CS(this).motd_actived_time = 0;
2143                         if (autocvar_g_campaign)
2144                                 Kill_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CPID_CAMPAIGN_MESSAGE);
2145                         else
2146                                 Kill_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CPID_MOTD);
2147                 }
2148                 else if (IS_PLAYER(this) || IS_SPEC(this))
2149                 {
2150                         // FIXME occasionally for some reason MOTD never goes away
2151                         // delay MOTD removal a little bit in the hope it fixes this bug
2152                         if (CS(this).motd_actived_time == -1) // MOTD marked to fade away as soon as client becomes player or spectator
2153                                 CS(this).motd_actived_time = -(5 + floor(random() * 10)); // add small delay
2154                         else //if (CS(this).motd_actived_time < -2)
2155                                 CS(this).motd_actived_time++;
2156                 }
2157         }
2158 }
2159
2160 bool joinAllowed(entity this)
2161 {
2162         if (CS(this).version_mismatch) return false;
2163         if (time < CS(this).jointime + MIN_SPEC_TIME) return false;
2164         if (!nJoinAllowed(this, this)) return false;
2165         if (teamplay && lockteams) return false;
2166         if (MUTATOR_CALLHOOK(ForbidSpawn, this)) return false;
2167         if (ShowTeamSelection(this)) return false;
2168         return true;
2169 }
2170
2171 .string shootfromfixedorigin;
2172 .bool dualwielding_prev;
2173 bool PlayerThink(entity this)
2174 {
2175         if (game_stopped || intermission_running) {
2176                 this.modelflags &= ~MF_ROCKET;
2177                 if(intermission_running)
2178                         IntermissionThink(this);
2179                 return false;
2180         }
2181
2182         if (timeout_status == TIMEOUT_ACTIVE) {
2183                 // don't allow the player to turn around while game is paused
2184                 // FIXME turn this into CSQC stuff
2185                 this.v_angle = this.lastV_angle;
2186                 this.angles = this.lastV_angle;
2187                 this.fixangle = true;
2188         }
2189
2190         if (frametime) player_powerups(this);
2191
2192         if (IS_DEAD(this)) {
2193                 if (this.personal && g_race_qualifying) {
2194                         if (time > this.respawn_time) {
2195                                 STAT(RESPAWN_TIME, this) = this.respawn_time = time + 1; // only retry once a second
2196                                 respawn(this);
2197                                 CS(this).impulse = CHIMPULSE_SPEEDRUN.impulse;
2198                         }
2199                 } else {
2200                         if (frametime) player_anim(this);
2201
2202                         if (this.respawn_flags & RESPAWN_DENY)
2203                         {
2204                                 STAT(RESPAWN_TIME, this) = 0;
2205                                 return false;
2206                         }
2207
2208                         bool button_pressed = (PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_JUMP(this) || PHYS_INPUT_BUTTON_ATCK2(this) || PHYS_INPUT_BUTTON_HOOK(this) || PHYS_INPUT_BUTTON_USE(this));
2209
2210                         switch(this.deadflag)
2211                         {
2212                                 case DEAD_DYING:
2213                                 {
2214                                         if ((this.respawn_flags & RESPAWN_FORCE) && !(this.respawn_time < this.respawn_time_max))
2215                                                 this.deadflag = DEAD_RESPAWNING;
2216                                         else if (!button_pressed || (time >= this.respawn_time_max && (this.respawn_flags & RESPAWN_FORCE)))
2217                                                 this.deadflag = DEAD_DEAD;
2218                                         break;
2219                                 }
2220                                 case DEAD_DEAD:
2221                                 {
2222                                         if (button_pressed)
2223                                                 this.deadflag = DEAD_RESPAWNABLE;
2224                                         else if (time >= this.respawn_time_max && (this.respawn_flags & RESPAWN_FORCE))
2225                                                 this.deadflag = DEAD_RESPAWNING;
2226                                         break;
2227                                 }
2228                                 case DEAD_RESPAWNABLE:
2229                                 {
2230                                         if (!button_pressed || (this.respawn_flags & RESPAWN_FORCE))
2231                                                 this.deadflag = DEAD_RESPAWNING;
2232                                         break;
2233                                 }
2234                                 case DEAD_RESPAWNING:
2235                                 {
2236                                         if (time > this.respawn_time)
2237                                         {
2238                                                 this.respawn_time = time + 1; // only retry once a second
2239                                                 this.respawn_time_max = this.respawn_time;
2240                                                 respawn(this);
2241                                         }
2242                                         break;
2243                                 }
2244                         }
2245
2246                         ShowRespawnCountdown(this);
2247
2248                         if (this.respawn_flags & RESPAWN_SILENT)
2249                                 STAT(RESPAWN_TIME, this) = 0;
2250                         else if ((this.respawn_flags & RESPAWN_FORCE) && this.respawn_time < this.respawn_time_max)
2251                         {
2252                                 if (time < this.respawn_time)
2253                                         STAT(RESPAWN_TIME, this) = this.respawn_time;
2254                                 else if (this.deadflag != DEAD_RESPAWNING)
2255                                         STAT(RESPAWN_TIME, this) = -this.respawn_time_max;
2256                         }
2257                         else
2258                                 STAT(RESPAWN_TIME, this) = this.respawn_time;
2259                 }
2260
2261                 // if respawning, invert stat_respawn_time to indicate this, the client translates it
2262                 if (this.deadflag == DEAD_RESPAWNING && STAT(RESPAWN_TIME, this) > 0)
2263                         STAT(RESPAWN_TIME, this) *= -1;
2264
2265                 return false;
2266         }
2267
2268         FixPlayermodel(this);
2269
2270         if (this.shootfromfixedorigin != autocvar_g_shootfromfixedorigin) {
2271                 this.shootfromfixedorigin = autocvar_g_shootfromfixedorigin;
2272                 stuffcmd(this, sprintf("\ncl_shootfromfixedorigin \"%s\"\n", autocvar_g_shootfromfixedorigin));
2273         }
2274
2275         // reset gun alignment when dual wielding status changes
2276         // to ensure guns are always aligned right and left
2277         bool dualwielding = W_DualWielding(this);
2278         if(this.dualwielding_prev != dualwielding)
2279         {
2280                 W_ResetGunAlign(this, CS_CVAR(this).cvar_cl_gunalign);
2281                 this.dualwielding_prev = dualwielding;
2282         }
2283
2284         // LordHavoc: allow firing on move frames (sub-ticrate), this gives better timing on slow servers
2285         //if(frametime)
2286         {
2287                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
2288                 {
2289                         .entity weaponentity = weaponentities[slot];
2290                         if(WEP_CVAR(vortex, charge_always))
2291                                 W_Vortex_Charge(this, weaponentity, frametime);
2292                         W_WeaponFrame(this, weaponentity);
2293                 }
2294         }
2295
2296         if (frametime)
2297         {
2298                 // WEAPONTODO: Add a weapon request for this
2299                 // rot vortex charge to the charge limit
2300                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
2301                 {
2302                         .entity weaponentity = weaponentities[slot];
2303                         if (WEP_CVAR(vortex, charge_rot_rate) && this.(weaponentity).vortex_charge > WEP_CVAR(vortex, charge_limit) && this.(weaponentity).vortex_charge_rottime < time)
2304                                 this.(weaponentity).vortex_charge = bound(WEP_CVAR(vortex, charge_limit), this.(weaponentity).vortex_charge - WEP_CVAR(vortex, charge_rot_rate) * frametime / W_TICSPERFRAME, 1);
2305                 }
2306
2307                 player_regen(this);
2308                 player_anim(this);
2309                 this.dmg_team = max(0, this.dmg_team - autocvar_g_teamdamage_resetspeed * frametime);
2310         }
2311
2312         monsters_setstatus(this);
2313
2314         return true;
2315 }
2316
2317 .bool would_spectate;
2318 // merged SpectatorThink and ObserverThink (old names are here so you can grep for them)
2319 void ObserverOrSpectatorThink(entity this)
2320 {
2321         bool is_spec = IS_SPEC(this);
2322         if ( CS(this).impulse )
2323         {
2324                 int r = MinigameImpulse(this, CS(this).impulse);
2325                 if (!is_spec || r)
2326                         CS(this).impulse = 0;
2327
2328                 if (is_spec && CS(this).impulse == IMP_weapon_drop.impulse)
2329                 {
2330                         STAT(CAMERA_SPECTATOR, this) = (STAT(CAMERA_SPECTATOR, this) + 1) % 3;
2331                         CS(this).impulse = 0;
2332                         return;
2333                 }
2334         }
2335
2336         if (this.flags & FL_JUMPRELEASED) {
2337                 if (PHYS_INPUT_BUTTON_JUMP(this) && (joinAllowed(this) || time < CS(this).jointime + MIN_SPEC_TIME)) {
2338                         this.flags &= ~FL_JUMPRELEASED;
2339                         this.flags |= FL_SPAWNING;
2340                 } else if((is_spec && (PHYS_INPUT_BUTTON_ATCK(this) || CS(this).impulse == 10 || CS(this).impulse == 15 || CS(this).impulse == 18 || (CS(this).impulse >= 200 && CS(this).impulse <= 209)))
2341                         || (!is_spec && ((PHYS_INPUT_BUTTON_ATCK(this) && !CS(this).version_mismatch) || this.would_spectate))) {
2342                         this.flags &= ~FL_JUMPRELEASED;
2343                         if(SpectateNext(this)) {
2344                                 TRANSMUTE(Spectator, this);
2345                         } else if (is_spec) {
2346                                 TRANSMUTE(Observer, this);
2347                                 PutClientInServer(this);
2348                         }
2349                         if (is_spec)
2350                                 CS(this).impulse = 0;
2351                 } else if (is_spec) {
2352                         if(CS(this).impulse == 12 || CS(this).impulse == 16  || CS(this).impulse == 19 || (CS(this).impulse >= 220 && CS(this).impulse <= 229)) {
2353                                 this.flags &= ~FL_JUMPRELEASED;
2354                                 if(SpectatePrev(this)) {
2355                                         TRANSMUTE(Spectator, this);
2356                                 } else {
2357                                         TRANSMUTE(Observer, this);
2358                                         PutClientInServer(this);
2359                                 }
2360                                 CS(this).impulse = 0;
2361                         } else if(PHYS_INPUT_BUTTON_ATCK2(this)) {
2362                                 this.would_spectate = false;
2363                                 this.flags &= ~FL_JUMPRELEASED;
2364                                 TRANSMUTE(Observer, this);
2365                                 PutClientInServer(this);
2366                         } else if(!SpectateUpdate(this) && !SpectateNext(this)) {
2367                                 PutObserverInServer(this);
2368                                 this.would_spectate = true;
2369                         }
2370                 }
2371                 else {
2372                         int preferred_movetype = ((!PHYS_INPUT_BUTTON_USE(this) ? CS_CVAR(this).cvar_cl_clippedspectating : !CS_CVAR(this).cvar_cl_clippedspectating) ? MOVETYPE_FLY_WORLDONLY : MOVETYPE_NOCLIP);
2373                         set_movetype(this, preferred_movetype);
2374                 }
2375         } else { // jump pressed
2376                 if ((is_spec && !(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_ATCK2(this)))
2377                         || (!is_spec && !(PHYS_INPUT_BUTTON_ATCK(this) || PHYS_INPUT_BUTTON_JUMP(this)))) {
2378                         this.flags |= FL_JUMPRELEASED;
2379                         if(this.flags & FL_SPAWNING)
2380                         {
2381                                 this.flags &= ~FL_SPAWNING;
2382                                 if(joinAllowed(this))
2383                                         Join(this);
2384                                 else if(time < CS(this).jointime + MIN_SPEC_TIME)
2385                                         CS(this).autojoin_checked = -1;
2386                                 return;
2387                         }
2388                 }
2389                 if(is_spec && !SpectateUpdate(this))
2390                         PutObserverInServer(this);
2391         }
2392         if (is_spec)
2393                 this.flags |= FL_CLIENT | FL_NOTARGET;
2394 }
2395
2396 void PlayerUseKey(entity this)
2397 {
2398         if (!IS_PLAYER(this))
2399                 return;
2400
2401         if(this.vehicle)
2402         {
2403                 if(!game_stopped)
2404                 {
2405                         vehicles_exit(this.vehicle, VHEF_NORMAL);
2406                         return;
2407                 }
2408         }
2409         else if(autocvar_g_vehicles_enter)
2410         {
2411                 if(!game_stopped && !STAT(FROZEN, this) && !IS_DEAD(this) && !IS_INDEPENDENT_PLAYER(this))
2412                 {
2413                         entity head, closest_target = NULL;
2414                         head = WarpZone_FindRadius(this.origin, autocvar_g_vehicles_enter_radius, true);
2415
2416                         while(head) // find the closest acceptable target to enter
2417                         {
2418                                 if(IS_VEHICLE(head) && !IS_DEAD(head) && head.takedamage != DAMAGE_NO)
2419                                 if(!head.owner || ((head.vehicle_flags & VHF_MULTISLOT) && SAME_TEAM(head.owner, this)))
2420                                 {
2421                                         if(closest_target)
2422                                         {
2423                                                 if(vlen2(this.origin - head.origin) < vlen2(this.origin - closest_target.origin))
2424                                                 { closest_target = head; }
2425                                         }
2426                                         else { closest_target = head; }
2427                                 }
2428
2429                                 head = head.chain;
2430                         }
2431
2432                         if(closest_target) { vehicles_enter(this, closest_target); return; }
2433                 }
2434         }
2435
2436         // a use key was pressed; call handlers
2437         MUTATOR_CALLHOOK(PlayerUseKey, this);
2438 }
2439
2440
2441 /*
2442 =============
2443 PlayerPreThink
2444
2445 Called every frame for each client before the physics are run
2446 =============
2447 */
2448 .float last_vehiclecheck;
2449 void PlayerPreThink (entity this)
2450 {
2451         STAT(GUNALIGN, this) = CS_CVAR(this).cvar_cl_gunalign; // TODO
2452         STAT(MOVEVARS_CL_TRACK_CANJUMP, this) = CS_CVAR(this).cvar_cl_movement_track_canjump;
2453
2454         WarpZone_PlayerPhysics_FixVAngle(this);
2455
2456         if (frametime) {
2457                 // physics frames: update anticheat stuff
2458                 anticheat_prethink(this);
2459         }
2460
2461         if (blockSpectators && frametime) {
2462                 // WORKAROUND: only use dropclient in server frames (frametime set).
2463                 // Never use it in cl_movement frames (frametime zero).
2464                 checkSpectatorBlock(this);
2465         }
2466
2467         zoomstate_set = false;
2468
2469         // Check for nameless players
2470         if (this.netname == "" || this.netname != CS(this).netname_previous)
2471         {
2472                 bool assume_unchanged = (CS(this).netname_previous == "");
2473                 if (autocvar_sv_name_maxlength > 0 && strlennocol(this.netname) > autocvar_sv_name_maxlength)
2474                 {
2475                         int new_length = textLengthUpToLength(this.netname, autocvar_sv_name_maxlength, strlennocol);
2476                         this.netname = strzone(strcat(substring(this.netname, 0, new_length), "^7"));
2477                         sprint(this, sprintf("Warning: your name is longer than %d characters, it has been truncated.\n", autocvar_sv_name_maxlength));
2478                         assume_unchanged = false;
2479                         // stuffcmd(this, strcat("name ", this.netname, "\n")); // maybe?
2480                 }
2481                 if (isInvisibleString(this.netname))
2482                 {
2483                         this.netname = strzone(sprintf("Player#%d", this.playerid));
2484                         sprint(this, "Warning: invisible names are not allowed.\n");
2485                         assume_unchanged = false;
2486                         // stuffcmd(this, strcat("name ", this.netname, "\n")); // maybe?
2487                 }
2488                 if (!assume_unchanged && autocvar_sv_eventlog)
2489                         GameLogEcho(strcat(":name:", ftos(this.playerid), ":", playername(this.netname, this.team, false)));
2490                 strcpy(CS(this).netname_previous, this.netname);
2491         }
2492
2493         // version nagging
2494         if (CS(this).version_nagtime && CS_CVAR(this).cvar_g_xonoticversion && time > CS(this).version_nagtime) {
2495         CS(this).version_nagtime = 0;
2496         if (strstrofs(CS_CVAR(this).cvar_g_xonoticversion, "git", 0) >= 0 || strstrofs(CS_CVAR(this).cvar_g_xonoticversion, "autobuild", 0) >= 0) {
2497             // git client
2498         } else if (strstrofs(autocvar_g_xonoticversion, "git", 0) >= 0 || strstrofs(autocvar_g_xonoticversion, "autobuild", 0) >= 0) {
2499             // git server
2500             Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_VERSION_BETA, autocvar_g_xonoticversion, CS_CVAR(this).cvar_g_xonoticversion);
2501         } else {
2502             int r = vercmp(CS_CVAR(this).cvar_g_xonoticversion, autocvar_g_xonoticversion);
2503             if (r < 0) { // old client
2504                 Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_VERSION_OUTDATED, autocvar_g_xonoticversion, CS_CVAR(this).cvar_g_xonoticversion);
2505             } else if (r > 0) { // old server
2506                 Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_VERSION_OLD, autocvar_g_xonoticversion, CS_CVAR(this).cvar_g_xonoticversion);
2507             }
2508         }
2509     }
2510
2511         // GOD MODE info
2512         if (!(this.flags & FL_GODMODE) && this.max_armorvalue)
2513         {
2514                 Send_Notification(NOTIF_ONE_ONLY, this, MSG_INFO, INFO_GODMODE_OFF, this.max_armorvalue);
2515                 this.max_armorvalue = 0;
2516         }
2517
2518         if (frametime && IS_PLAYER(this) && time >= game_starttime)
2519         {
2520                 if (STAT(FROZEN, this) == FROZEN_TEMP_REVIVING)
2521                 {
2522                         STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) + frametime * this.revive_speed, 1);
2523                         SetResourceExplicit(this, RES_HEALTH, max(1, STAT(REVIVE_PROGRESS, this) * start_health));
2524                         if (this.iceblock)
2525                                 this.iceblock.alpha = bound(0.2, 1 - STAT(REVIVE_PROGRESS, this), 1);
2526
2527                         if (STAT(REVIVE_PROGRESS, this) >= 1)
2528                                 Unfreeze(this, false);
2529                 }
2530                 else if (STAT(FROZEN, this) == FROZEN_TEMP_DYING)
2531                 {
2532                         STAT(REVIVE_PROGRESS, this) = bound(0, STAT(REVIVE_PROGRESS, this) - frametime * this.revive_speed, 1);
2533                         SetResourceExplicit(this, RES_HEALTH, max(0, autocvar_g_nades_ice_health + (start_health-autocvar_g_nades_ice_health) * STAT(REVIVE_PROGRESS, this)));
2534
2535                         if (GetResource(this, RES_HEALTH) < 1)
2536                         {
2537                                 if (this.vehicle)
2538                                         vehicles_exit(this.vehicle, VHEF_RELEASE);
2539                                 if(this.event_damage)
2540                                         this.event_damage(this, this, this.frozen_by, 1, DEATH_NADE_ICE_FREEZE.m_id, DMG_NOWEP, this.origin, '0 0 0');
2541                         }
2542                         else if (STAT(REVIVE_PROGRESS, this) <= 0)
2543                                 Unfreeze(this, false);
2544                 }
2545         }
2546
2547         MUTATOR_CALLHOOK(PlayerPreThink, this);
2548
2549         if(autocvar_g_vehicles_enter && (time > this.last_vehiclecheck) && !game_stopped && !this.vehicle)
2550         if(IS_PLAYER(this) && !STAT(FROZEN, this) && !IS_DEAD(this) && !IS_INDEPENDENT_PLAYER(this))
2551         {
2552                 FOREACH_ENTITY_RADIUS(this.origin, autocvar_g_vehicles_enter_radius, IS_VEHICLE(it) && !IS_DEAD(it) && it.takedamage != DAMAGE_NO,
2553                 {
2554                         if(!it.owner)
2555                         {
2556                                 if(!it.team || SAME_TEAM(this, it))
2557                                         Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_VEHICLE_ENTER);
2558                                 else if(autocvar_g_vehicles_steal)
2559                                         Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_VEHICLE_ENTER_STEAL);
2560                         }
2561                         else if((it.vehicle_flags & VHF_MULTISLOT) && SAME_TEAM(it.owner, this))
2562                         {
2563                                 Send_Notification(NOTIF_ONE, this, MSG_CENTER, CENTER_VEHICLE_ENTER_GUNNER);
2564                         }
2565                 });
2566
2567                 this.last_vehiclecheck = time + 1;
2568         }
2569
2570         if(!CS_CVAR(this).cvar_cl_newusekeysupported) // FIXME remove this - it was a stupid idea to begin with, we can JUST use the button
2571         {
2572                 if(PHYS_INPUT_BUTTON_USE(this) && !CS(this).usekeypressed)
2573                         PlayerUseKey(this);
2574                 CS(this).usekeypressed = PHYS_INPUT_BUTTON_USE(this);
2575         }
2576
2577         if (IS_REAL_CLIENT(this))
2578                 PrintWelcomeMessage(this);
2579
2580         if (IS_PLAYER(this)) {
2581                 if (IS_REAL_CLIENT(this) && time < CS(this).jointime + MIN_SPEC_TIME)
2582                         error("Client can't be spawned as player on connection!");
2583                 if(!PlayerThink(this))
2584                         return;
2585         }
2586         else if (game_stopped || intermission_running) {
2587                 if(intermission_running)
2588                         IntermissionThink(this);
2589                 return;
2590         }
2591         else if (IS_REAL_CLIENT(this) && CS(this).autojoin_checked <= 0 && time >= CS(this).jointime + MIN_SPEC_TIME)
2592         {
2593                 bool early_join_requested = (CS(this).autojoin_checked < 0);
2594                 CS(this).autojoin_checked = 1;
2595                 // don't do this in ClientConnect
2596                 // many things can go wrong if a client is spawned as player on connection
2597                 if (early_join_requested || MUTATOR_CALLHOOK(AutoJoinOnConnection, this)
2598                         || (!(autocvar_sv_spectate || autocvar_g_campaign || (Player_GetForcedTeamIndex(this) == TEAM_FORCE_SPECTATOR))
2599                                 && (!teamplay || autocvar_g_balance_teams)))
2600                 {
2601                         campaign_bots_may_start = true;
2602                         if(joinAllowed(this))
2603                                 Join(this);
2604                         return;
2605                 }
2606         }
2607         else if (IS_OBSERVER(this) || IS_SPEC(this)) {
2608                 ObserverOrSpectatorThink(this);
2609         }
2610
2611         // WEAPONTODO: Add weapon request for this
2612         if (!zoomstate_set) {
2613                 bool wep_zoomed = false;
2614                 for(int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
2615                 {
2616                         .entity weaponentity = weaponentities[slot];
2617                         Weapon thiswep = this.(weaponentity).m_weapon;
2618                         if(thiswep != WEP_Null && thiswep.wr_zoom)
2619                                 wep_zoomed += thiswep.wr_zoom(thiswep, this);
2620                 }
2621                 SetZoomState(this, PHYS_INPUT_BUTTON_ZOOM(this) || PHYS_INPUT_BUTTON_ZOOMSCRIPT(this) || wep_zoomed);
2622         }
2623
2624         if (CS(this).teamkill_soundtime && time > CS(this).teamkill_soundtime)
2625         {
2626                 CS(this).teamkill_soundtime = 0;
2627
2628                 entity e = CS(this).teamkill_soundsource;
2629                 entity oldpusher = e.pusher;
2630                 e.pusher = this;
2631                 PlayerSound(e, playersound_teamshoot, CH_VOICE, VOL_BASEVOICE, VOICETYPE_LASTATTACKER_ONLY);
2632                 e.pusher = oldpusher;
2633         }
2634
2635         if (CS(this).taunt_soundtime && time > CS(this).taunt_soundtime) {
2636                 CS(this).taunt_soundtime = 0;
2637                 PlayerSound(this, playersound_taunt, CH_VOICE, VOL_BASEVOICE, VOICETYPE_AUTOTAUNT);
2638         }
2639
2640         target_voicescript_next(this);
2641 }
2642
2643 void DrownPlayer(entity this)
2644 {
2645         if(IS_DEAD(this) || game_stopped || time < game_starttime || this.vehicle
2646                 || STAT(FROZEN, this) || this.watertype != CONTENT_WATER)
2647         {
2648                 STAT(AIR_FINISHED, this) = 0;
2649                 return;
2650         }
2651
2652         if (this.waterlevel != WATERLEVEL_SUBMERGED)
2653         {
2654                 if(STAT(AIR_FINISHED, this) && STAT(AIR_FINISHED, this) < time)
2655                         PlayerSound(this, playersound_gasp, CH_PLAYER, VOL_BASE, VOICETYPE_PLAYERSOUND);
2656                 STAT(AIR_FINISHED, this) = 0;
2657         }
2658         else
2659         {
2660                 if (!STAT(AIR_FINISHED, this))
2661                         STAT(AIR_FINISHED, this) = time + autocvar_g_balance_contents_drowndelay;
2662                 if (STAT(AIR_FINISHED, this) < time)
2663                 {       // drown!
2664                         if (this.pain_finished < time)
2665                         {
2666                                 Damage (this, NULL, NULL, autocvar_g_balance_contents_playerdamage_drowning * autocvar_g_balance_contents_damagerate, DEATH_DROWN.m_id, DMG_NOWEP, this.origin, '0 0 0');
2667                                 this.pain_finished = time + 0.5;
2668                         }
2669                 }
2670         }
2671 }
2672
2673 .bool move_qcphysics;
2674
2675 void Player_Physics(entity this)
2676 {
2677         this.movetype = (this.move_qcphysics) ? MOVETYPE_QCPLAYER : this.move_movetype;
2678
2679         if(!this.move_qcphysics)
2680                 return;
2681
2682         if(!frametime && !CS(this).pm_frametime)
2683                 return;
2684
2685         Movetype_Physics_NoMatchTicrate(this, CS(this).pm_frametime, true);
2686
2687         CS(this).pm_frametime = 0;
2688 }
2689
2690 /*
2691 =============
2692 PlayerPostThink
2693
2694 Called every frame for each client after the physics are run
2695 =============
2696 */
2697 void PlayerPostThink (entity this)
2698 {
2699         Player_Physics(this);
2700
2701         if (autocvar_sv_maxidle > 0)
2702         if (frametime) // WORKAROUND: only use dropclient in server frames (frametime set). Never use it in cl_movement frames (frametime zero).
2703         if (IS_REAL_CLIENT(this))
2704         if (IS_PLAYER(this) || autocvar_sv_maxidle_spectatorsareidle)
2705         {
2706                 int totalClients = 0;
2707                 if(autocvar_sv_maxidle_slots > 0)
2708                 {
2709                         FOREACH_CLIENT(IS_REAL_CLIENT(it) || autocvar_sv_maxidle_slots_countbots,
2710                         {
2711                                 ++totalClients;
2712                         });
2713                 }
2714
2715                 if (autocvar_sv_maxidle_slots > 0 && (maxclients - totalClients) > autocvar_sv_maxidle_slots)
2716                 { /* do nothing */ }
2717                 else if (time - CS(this).parm_idlesince < 1) // instead of (time == this.parm_idlesince) to support sv_maxidle <= 10
2718                 {
2719                         if (CS(this).idlekick_lasttimeleft)
2720                         {
2721                                 CS(this).idlekick_lasttimeleft = 0;
2722                                 Kill_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CPID_IDLING);
2723                         }
2724                 }
2725                 else
2726                 {
2727                         float timeleft = ceil(autocvar_sv_maxidle - (time - CS(this).parm_idlesince));
2728                         if (timeleft == min(10, autocvar_sv_maxidle - 1)) { // - 1 to support sv_maxidle <= 10
2729                                 if (!CS(this).idlekick_lasttimeleft)
2730                                         Send_Notification(NOTIF_ONE_ONLY, this, MSG_CENTER, CENTER_DISCONNECT_IDLING, timeleft);
2731                         }
2732                         if (timeleft <= 0) {
2733                                 Send_Notification(NOTIF_ALL, NULL, MSG_INFO, INFO_QUIT_KICK_IDLING, this.netname);
2734                                 dropclient(this);
2735                                 return;
2736                         }
2737                         else if (timeleft <= 10) {
2738                                 if (timeleft != CS(this).idlekick_lasttimeleft)
2739                                         Send_Notification(NOTIF_ONE, this, MSG_ANNCE, Announcer_PickNumber(CNT_IDLE, timeleft));
2740                                 CS(this).idlekick_lasttimeleft = timeleft;
2741                         }
2742                 }
2743         }
2744
2745         CheatFrame(this);
2746
2747         if (game_stopped)
2748         {
2749                 this.solid = SOLID_NOT;
2750                 this.takedamage = DAMAGE_NO;
2751                 set_movetype(this, MOVETYPE_NONE);
2752                 CS(this).teamkill_complain = 0;
2753                 CS(this).teamkill_soundtime = 0;
2754                 CS(this).teamkill_soundsource = NULL;
2755         }
2756
2757         if (IS_PLAYER(this)) {
2758                 if(this.death_time == time && IS_DEAD(this))
2759                 {
2760                         // player's bbox gets resized now, instead of in the damage event that killed the player,
2761                         // once all the damage events of this frame have been processed with normal size
2762                         this.maxs.z = 5;
2763                         setsize(this, this.mins, this.maxs);
2764                 }
2765                 DrownPlayer(this);
2766                 UpdateChatBubble(this);
2767                 if (CS(this).impulse) ImpulseCommands(this);
2768                 GetPressedKeys(this);
2769                 if (game_stopped)
2770                 {
2771                         CSQCMODEL_AUTOUPDATE(this);
2772                         return;
2773                 }
2774         }
2775         else if (IS_OBSERVER(this) && STAT(PRESSED_KEYS, this))
2776         {
2777                 CS(this).pressedkeys = 0;
2778                 STAT(PRESSED_KEYS, this) = 0;
2779         }
2780
2781         if (this.waypointsprite_attachedforcarrier) {
2782                 float hp = healtharmor_maxdamage(GetResource(this, RES_HEALTH), GetResource(this, RES_ARMOR), autocvar_g_balance_armor_blockpercent, DEATH_WEAPON.m_id).x;
2783                 WaypointSprite_UpdateHealth(this.waypointsprite_attachedforcarrier, hp);
2784         }
2785
2786         CSQCMODEL_AUTOUPDATE(this);
2787 }
2788
2789 // hack to copy the button fields from the client entity to the Client State
2790 void PM_UpdateButtons(entity this, entity store)
2791 {
2792         if(this.impulse)
2793                 store.impulse = this.impulse;
2794         this.impulse = 0;
2795
2796         bool typing = this.buttonchat || this.button12;
2797
2798         store.button0 = (typing) ? 0 : this.button0;
2799         //button1?!
2800         store.button2 = (typing) ? 0 : this.button2;
2801         store.button3 = (typing) ? 0 : this.button3;
2802         store.button4 = this.button4;
2803         store.button5 = (typing) ? 0 : this.button5;
2804         store.button6 = this.button6;
2805         store.button7 = this.button7;
2806         store.button8 = this.button8;
2807         store.button9 = this.button9;
2808         store.button10 = this.button10;
2809         store.button11 = this.button11;
2810         store.button12 = this.button12;
2811         store.button13 = this.button13;
2812         store.button14 = this.button14;
2813         store.button15 = this.button15;
2814         store.button16 = this.button16;
2815         store.buttonuse = this.buttonuse;
2816         store.buttonchat = this.buttonchat;
2817
2818         store.cursor_active = this.cursor_active;
2819         store.cursor_screen = this.cursor_screen;
2820         store.cursor_trace_start = this.cursor_trace_start;
2821         store.cursor_trace_endpos = this.cursor_trace_endpos;
2822         store.cursor_trace_ent = this.cursor_trace_ent;
2823
2824         store.ping = this.ping;
2825         store.ping_packetloss = this.ping_packetloss;
2826         store.ping_movementloss = this.ping_movementloss;
2827
2828         store.v_angle = this.v_angle;
2829         store.movement = this.movement;
2830 }
2831
2832 NET_HANDLE(fpsreport, bool)
2833 {
2834         int fps = ReadShort();
2835         PlayerScore_Set(sender, SP_FPS, fps);
2836         return true;
2837 }