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