]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_player.qc
Fix auxiliary crosshairs
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_player.qc
1 #include "cl_player.qh"
2 #include "_all.qh"
3
4 #include "bot/bot.qh"
5 #include "cheats.qh"
6 #include "g_damage.qh"
7 #include "g_subs.qh"
8 #include "g_violence.qh"
9 #include "miscfunctions.qh"
10 #include "portals.qh"
11 #include "teamplay.qh"
12 #include "waypointsprites.qh"
13 #include "weapons/throwing.qh"
14 #include "command/common.qh"
15 #include "../common/animdecide.qh"
16 #include "../common/csqcmodel_settings.qh"
17 #include "../common/deathtypes.qh"
18 #include "../common/triggers/subs.qh"
19 #include "../common/playerstats.qh"
20 #include "../csqcmodellib/sv_model.qh"
21
22 #include "weapons/weaponstats.qh"
23
24 #include "../common/animdecide.qh"
25
26 void CopyBody_Think(void)
27 {
28         if(self.CopyBody_nextthink && time > self.CopyBody_nextthink)
29         {
30                 self.CopyBody_think();
31                 if(wasfreed(self))
32                         return;
33                 self.CopyBody_nextthink = self.nextthink;
34                 self.CopyBody_think = self.think;
35                 self.think = CopyBody_Think;
36         }
37         CSQCMODEL_AUTOUPDATE();
38         self.nextthink = time;
39 }
40 void CopyBody(float keepvelocity)
41 {
42         entity oldself;
43         if (self.effects & EF_NODRAW)
44                 return;
45         oldself = self;
46         self = spawn();
47         self.enemy = oldself;
48         self.lip = oldself.lip;
49         self.colormap = oldself.colormap;
50         self.iscreature = oldself.iscreature;
51         self.teleportable = oldself.teleportable;
52         self.damagedbycontents = oldself.damagedbycontents;
53         self.angles = oldself.angles;
54         self.v_angle = oldself.v_angle;
55         self.avelocity = oldself.avelocity;
56         self.classname = "body";
57         self.damageforcescale = oldself.damageforcescale;
58         self.effects = oldself.effects;
59         self.glowmod = oldself.glowmod;
60         self.event_damage = oldself.event_damage;
61         self.anim_state = oldself.anim_state;
62         self.anim_time = oldself.anim_time;
63         self.anim_lower_action = oldself.anim_lower_action;
64         self.anim_lower_time = oldself.anim_lower_time;
65         self.anim_upper_action = oldself.anim_upper_action;
66         self.anim_upper_time = oldself.anim_upper_time;
67         self.anim_implicit_state = oldself.anim_implicit_state;
68         self.anim_implicit_time = oldself.anim_implicit_time;
69         self.anim_lower_implicit_action = oldself.anim_lower_implicit_action;
70         self.anim_lower_implicit_time = oldself.anim_lower_implicit_time;
71         self.anim_upper_implicit_action = oldself.anim_upper_implicit_action;
72         self.anim_upper_implicit_time = oldself.anim_upper_implicit_time;
73         self.dphitcontentsmask = oldself.dphitcontentsmask;
74         self.death_time = oldself.death_time;
75         self.pain_finished = oldself.pain_finished;
76         self.health = oldself.health;
77         self.armorvalue = oldself.armorvalue;
78         self.armortype = oldself.armortype;
79         self.model = oldself.model;
80         self.modelindex = oldself.modelindex;
81         self.skin = oldself.skin;
82         self.species = oldself.species;
83         self.movetype = oldself.movetype;
84         self.solid = oldself.solid;
85         self.ballistics_density = oldself.ballistics_density;
86         self.takedamage = oldself.takedamage;
87         self.customizeentityforclient = oldself.customizeentityforclient;
88         self.uncustomizeentityforclient = oldself.uncustomizeentityforclient;
89         self.uncustomizeentityforclient_set = oldself.uncustomizeentityforclient_set;
90         if (keepvelocity == 1)
91                 self.velocity = oldself.velocity;
92         self.oldvelocity = self.velocity;
93         self.alpha = oldself.alpha;
94         self.fade_time = oldself.fade_time;
95         self.fade_rate = oldself.fade_rate;
96         //self.weapon = oldself.weapon;
97         setorigin(self, oldself.origin);
98         setsize(self, oldself.mins, oldself.maxs);
99         self.prevorigin = oldself.origin;
100         self.reset = SUB_Remove;
101
102         Drag_MoveDrag(oldself, self);
103
104         if(self.colormap <= maxclients && self.colormap > 0)
105                 self.colormap = 1024 + oldself.clientcolors;
106
107         CSQCMODEL_AUTOINIT();
108         self.CopyBody_nextthink = oldself.nextthink;
109         self.CopyBody_think = oldself.think;
110         self.nextthink = time;
111         self.think = CopyBody_Think;
112         // "bake" the current animation frame for clones (they don't get clientside animation)
113         animdecide_load_if_needed(self);
114         animdecide_setframes(self, false, frame, frame1time, frame2, frame2time);
115
116         self = oldself;
117 }
118
119 float player_getspecies()
120 {
121         float s;
122         get_model_parameters(self.model, self.skin);
123         s = get_model_parameters_species;
124         get_model_parameters(string_null, 0);
125         if(s < 0)
126                 return SPECIES_HUMAN;
127         return s;
128 }
129
130 void player_setupanimsformodel()
131 {
132         // load animation info
133         animdecide_load_if_needed(self);
134         animdecide_setstate(self, 0, false);
135 }
136
137 void player_anim (void)
138 {
139         int deadbits = (self.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
140         if(self.deadflag) {
141                 if (!deadbits) {
142                         // Decide on which death animation to use.
143                         if(random() < 0.5)
144                                 deadbits = ANIMSTATE_DEAD1;
145                         else
146                                 deadbits = ANIMSTATE_DEAD2;
147                 }
148         } else {
149                 // Clear a previous death animation.
150                 deadbits = 0;
151         }
152         int animbits = deadbits;
153         if(self.frozen)
154                 animbits |= ANIMSTATE_FROZEN;
155         if(self.crouch)
156                 animbits |= ANIMSTATE_DUCK;
157         animdecide_setstate(self, animbits, false);
158         animdecide_setimplicitstate(self, (self.flags & FL_ONGROUND));
159
160         if (self.weaponentity)
161         {
162                 updateanim(self.weaponentity);
163                 if (!self.weaponentity.animstate_override)
164                         setanim(self.weaponentity, self.weaponentity.anim_idle, true, false, false);
165         }
166 }
167
168 void PlayerCorpseDamage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
169 {
170         float take, save;
171         vector v;
172         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
173
174         // damage resistance (ignore most of the damage from a bullet or similar)
175         damage = max(damage - 5, 1);
176
177         v = healtharmor_applydamage(self.armorvalue, autocvar_g_balance_armor_blockpercent, deathtype, damage);
178         take = v.x;
179         save = v.y;
180
181         if(sound_allowed(MSG_BROADCAST, attacker))
182         {
183                 if (save > 10)
184                         sound (self, CH_SHOTS, "misc/armorimpact.wav", VOL_BASE, ATTEN_NORM);
185                 else if (take > 30)
186                         sound (self, CH_SHOTS, "misc/bodyimpact2.wav", VOL_BASE, ATTEN_NORM);
187                 else if (take > 10)
188                         sound (self, CH_SHOTS, "misc/bodyimpact1.wav", VOL_BASE, ATTEN_NORM);
189         }
190
191         if (take > 50)
192                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
193         if (take > 100)
194                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
195
196         self.armorvalue = self.armorvalue - save;
197         self.health = self.health - take;
198         // pause regeneration for 5 seconds
199         self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
200
201         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
202         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
203         self.dmg_inflictor = inflictor;
204
205         if (self.health <= -autocvar_sv_gibhealth && self.alpha >= 0)
206         {
207                 // don't use any animations as a gib
208                 self.frame = 0;
209                 // view just above the floor
210                 self.view_ofs = '0 0 4';
211
212                 Violence_GibSplash(self, 1, 1, attacker);
213                 self.alpha = -1;
214                 self.solid = SOLID_NOT; // restore later
215                 self.takedamage = DAMAGE_NO; // restore later
216                 self.damagedbycontents = false;
217         }
218 }
219
220 void calculate_player_respawn_time()
221 {
222         if(g_ca)
223                 return;
224
225         float gametype_setting_tmp;
226         float sdelay_max = GAMETYPE_DEFAULTED_SETTING(respawn_delay_max);
227         float sdelay_small = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small);
228         float sdelay_large = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large);
229         float sdelay_small_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small_count);
230         float sdelay_large_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large_count);
231         float waves = GAMETYPE_DEFAULTED_SETTING(respawn_waves);
232
233         float pcount = 1;  // Include myself whether or not team is already set right and I'm a "player".
234         entity pl;
235         if (teamplay)
236         {
237                 FOR_EACH_PLAYER(pl)
238                         if (pl != self)
239                                 if (pl.team == self.team)
240                                         ++pcount;
241                 if (sdelay_small_count == 0)
242                         sdelay_small_count = 1;
243                 if (sdelay_large_count == 0)
244                         sdelay_large_count = 1;
245         }
246         else
247         {
248                 FOR_EACH_PLAYER(pl)
249                         if (pl != self)
250                                 ++pcount;
251                 if (sdelay_small_count == 0)
252                 {
253                         if (g_cts)
254                         {
255                                 // Players play independently. No point in requiring enemies.
256                                 sdelay_small_count = 1;
257                         }
258                         else
259                         {
260                                 // Players play AGAINST each other. Enemies required.
261                                 sdelay_small_count = 2;
262                         }
263                 }
264                 if (sdelay_large_count == 0)
265                 {
266                         if (g_cts)
267                         {
268                                 // Players play independently. No point in requiring enemies.
269                                 sdelay_large_count = 1;
270                         }
271                         else
272                         {
273                                 // Players play AGAINST each other. Enemies required.
274                                 sdelay_large_count = 2;
275                         }
276                 }
277         }
278
279         float sdelay;
280
281         if (pcount <= sdelay_small_count)
282                 sdelay = sdelay_small;
283         else if (pcount >= sdelay_large_count)
284                 sdelay = sdelay_large;
285         else  // NOTE: this case implies sdelay_large_count > sdelay_small_count.
286                 sdelay = sdelay_small + (sdelay_large - sdelay_small) * (pcount - sdelay_small_count) / (sdelay_large_count - sdelay_small_count);
287
288         if(waves)
289                 self.respawn_time = ceil((time + sdelay) / waves) * waves;
290         else
291                 self.respawn_time = time + sdelay;
292
293         if(sdelay < sdelay_max)
294                 self.respawn_time_max = time + sdelay_max;
295         else
296                 self.respawn_time_max = self.respawn_time;
297
298         if((sdelay + waves >= 5.0) && (self.respawn_time - time > 1.75))
299                 self.respawn_countdown = 10; // first number to count down from is 10
300         else
301                 self.respawn_countdown = -1; // do not count down
302
303         if(autocvar_g_forced_respawn)
304                 self.respawn_flags = self.respawn_flags | RESPAWN_FORCE;
305 }
306
307 void PlayerDamage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
308 {
309         float take, save, dh, da;
310         int j;
311         vector v;
312         float valid_damage_for_weaponstats;
313         float excess;
314
315         dh = max(self.health, 0);
316         da = max(self.armorvalue, 0);
317
318         if(!DEATH_ISSPECIAL(deathtype))
319         {
320                 damage *= sqrt(bound(1.0, self.cvar_cl_handicap, 100.0));
321                 if(self != attacker)
322                         damage /= sqrt(bound(1.0, attacker.cvar_cl_handicap, 100.0));
323         }
324
325         if(DEATH_ISWEAPON(deathtype, WEP_TUBA.m_id))
326         {
327                 // tuba causes blood to come out of the ears
328                 vector ear1, ear2;
329                 vector d;
330                 float f;
331                 ear1 = self.origin;
332                 ear1_z += 0.125 * self.view_ofs.z + 0.875 * self.maxs.z; // 7/8
333                 ear2 = ear1;
334                 makevectors(self.angles);
335                 ear1 += v_right * -10;
336                 ear2 += v_right * +10;
337                 d = inflictor.origin - self.origin;
338                 if (d)
339                         f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
340                 else
341                         f = 0;  // Assum ecenter.
342                 force = v_right * vlen(force);
343                 Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), self, attacker);
344                 Violence_GibSplash_At(ear2, force,      2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), self, attacker);
345                 if(f > 0)
346                 {
347                         hitloc = ear1;
348                         force = force * -1;
349                 }
350                 else
351                 {
352                         hitloc = ear2;
353                         // force is already good
354                 }
355         }
356         else
357                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
358
359
360         v = healtharmor_applydamage(self.armorvalue, autocvar_g_balance_armor_blockpercent, deathtype, damage);
361         take = v.x;
362         save = v.y;
363
364         if(attacker == self)
365         {
366                 // don't reset pushltime for self damage as it may be an attempt to
367                 // escape a lava pit or similar
368                 //self.pushltime = 0;
369                 self.istypefrag = 0;
370         }
371         else if(IS_PLAYER(attacker))
372         {
373                 self.pusher = attacker;
374                 self.pushltime = time + autocvar_g_maxpushtime;
375                 self.istypefrag = self.BUTTON_CHAT;
376         }
377         else if(time < self.pushltime)
378         {
379                 attacker = self.pusher;
380                 self.pushltime = max(self.pushltime, time + 0.6);
381         }
382         else
383         {
384                 self.pushltime = 0;
385                 self.istypefrag = 0;
386         }
387
388         frag_damage = damage;
389         MUTATOR_CALLHOOK(PlayerDamage_SplitHealthArmor, inflictor, attacker, self, force, take, save);
390         take = bound(0, damage_take, self.health);
391         save = bound(0, damage_save, self.armorvalue);
392         excess = max(0, damage - take - save);
393
394         if(sound_allowed(MSG_BROADCAST, attacker))
395         {
396                 if (save > 10)
397                         sound (self, CH_SHOTS, "misc/armorimpact.wav", VOL_BASE, ATTEN_NORM);
398                 else if (take > 30)
399                         sound (self, CH_SHOTS, "misc/bodyimpact2.wav", VOL_BASE, ATTEN_NORM);
400                 else if (take > 10)
401                         sound (self, CH_SHOTS, "misc/bodyimpact1.wav", VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
402         }
403
404         if (take > 50)
405                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
406         if (take > 100)
407                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
408
409         if (time >= self.spawnshieldtime)
410         {
411                 if (!(self.flags & FL_GODMODE))
412                 {
413                         self.armorvalue = self.armorvalue - save;
414                         self.health = self.health - take;
415                         // pause regeneration for 5 seconds
416                         if(take)
417                                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
418
419                         if (time > self.pain_finished)          //Don't switch pain sequences like crazy
420                         {
421                                 self.pain_finished = time + 0.5;        //Supajoe
422
423                                 if(autocvar_sv_gentle < 1) {
424                                         if(self.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
425                                         {
426                                                 if (!self.animstate_override)
427                                                 {
428                                                         if (random() > 0.5)
429                                                                 animdecide_setaction(self, ANIMACTION_PAIN1, true);
430                                                         else
431                                                                 animdecide_setaction(self, ANIMACTION_PAIN2, true);
432                                                 }
433                                         }
434
435                                         if(sound_allowed(MSG_BROADCAST, attacker))
436                                         if((self.health < 2 * WEP_CVAR_PRI(blaster, damage) * autocvar_g_balance_selfdamagepercent + 1) || !((get_weaponinfo(DEATH_WEAPONOF(deathtype))).spawnflags & WEP_FLAG_CANCLIMB) || attacker != self) // WEAPONTODO: create separate limit for pain notification with laser
437                                         if(self.health > 1)
438                                         // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
439                                         {
440                                                 if(deathtype == DEATH_FALL)
441                                                         PlayerSound(playersound_fall, CH_PAIN, VOICETYPE_PLAYERSOUND);
442                                                 else if(self.health > 75) // TODO make a "gentle" version?
443                                                         PlayerSound(playersound_pain100, CH_PAIN, VOICETYPE_PLAYERSOUND);
444                                                 else if(self.health > 50)
445                                                         PlayerSound(playersound_pain75, CH_PAIN, VOICETYPE_PLAYERSOUND);
446                                                 else if(self.health > 25)
447                                                         PlayerSound(playersound_pain50, CH_PAIN, VOICETYPE_PLAYERSOUND);
448                                                 else
449                                                         PlayerSound(playersound_pain25, CH_PAIN, VOICETYPE_PLAYERSOUND);
450                                         }
451                                 }
452                         }
453
454                         // throw off bot aim temporarily
455                         float shake;
456                         if(IS_BOT_CLIENT(self) && self.health >= 1)
457                         {
458                                 shake = damage * 5 / (bound(0,skill,100) + 1);
459                                 self.v_angle_x = self.v_angle.x + (random() * 2 - 1) * shake;
460                                 self.v_angle_y = self.v_angle.y + (random() * 2 - 1) * shake;
461                                 self.v_angle_x = bound(-90, self.v_angle.x, 90);
462                         }
463                 }
464                 else
465                         self.max_armorvalue += (save + take);
466         }
467         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
468         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
469         self.dmg_inflictor = inflictor;
470
471         float abot, vbot, awep;
472         abot = (IS_BOT_CLIENT(attacker));
473         vbot = (IS_BOT_CLIENT(self));
474
475         valid_damage_for_weaponstats = 0;
476         awep = 0;
477
478         if(vbot || IS_REAL_CLIENT(self))
479         if(abot || IS_REAL_CLIENT(attacker))
480         if(attacker && self != attacker)
481         if(DIFF_TEAM(self, attacker))
482         {
483                 if(DEATH_ISSPECIAL(deathtype))
484                         awep = attacker.weapon;
485                 else
486                         awep = DEATH_WEAPONOF(deathtype);
487                 valid_damage_for_weaponstats = 1;
488         }
489
490         if(valid_damage_for_weaponstats)
491         {
492                 dh = dh - max(self.health, 0);
493                 da = da - max(self.armorvalue, 0);
494                 WeaponStats_LogDamage(awep, abot, self.weapon, vbot, dh + da);
495         }
496
497         if (self.health < 1)
498         {
499                 float defer_ClientKill_Now_TeamChange;
500                 defer_ClientKill_Now_TeamChange = false;
501
502                 if(self.alivetime)
503                 {
504                         PS_GR_P_ADDVAL(self, PLAYERSTATS_ALIVETIME, time - self.alivetime);
505                         self.alivetime = 0;
506                 }
507
508                 if(valid_damage_for_weaponstats)
509                         WeaponStats_LogKill(awep, abot, self.weapon, vbot);
510
511                 if(autocvar_sv_gentle < 1) // TODO make a "gentle" version?
512                 if(sound_allowed(MSG_BROADCAST, attacker))
513                 {
514                         if(deathtype == DEATH_DROWN)
515                                 PlayerSound(playersound_drown, CH_PAIN, VOICETYPE_PLAYERSOUND);
516                         else
517                                 PlayerSound(playersound_death, CH_PAIN, VOICETYPE_PLAYERSOUND);
518                 }
519
520                 // get rid of kill indicator
521                 if(self.killindicator)
522                 {
523                         remove(self.killindicator);
524                         self.killindicator = world;
525                         if(self.killindicator_teamchange)
526                                 defer_ClientKill_Now_TeamChange = true;
527
528                         if(self.classname == "body")
529                         if(deathtype == DEATH_KILL)
530                         {
531                                 // for the lemmings fans, a small harmless explosion
532                                 Send_Effect("rocket_explode", self.origin, '0 0 0', 1);
533                         }
534                 }
535
536                 // print an obituary message
537                 if(self.classname != "body")
538                         Obituary (attacker, inflictor, self, deathtype);
539
540         // increment frag counter for used weapon type
541         int w = DEATH_WEAPONOF(deathtype);
542         if(WEP_VALID(w))
543         if(accuracy_isgooddamage(attacker, self))
544         attacker.accuracy.(accuracy_frags[w-1]) += 1;
545
546                 MUTATOR_CALLHOOK(PlayerDies, inflictor, attacker, self, deathtype);
547
548                 WEP_ACTION(self.weapon, WR_PLAYERDEATH);
549
550                 RemoveGrapplingHook(self);
551
552                 Portal_ClearAllLater(self);
553
554                 self.fixangle = true;
555
556                 if(defer_ClientKill_Now_TeamChange)
557                         ClientKill_Now_TeamChange(); // can turn player into spectator
558
559                 // player could have been miraculously resuscitated ;)
560                 // e.g. players in freezetag get frozen, they don't really die
561                 if(self.health >= 1 || !(IS_PLAYER(self) || self.classname == "body"))
562                         return;
563
564                 // when we get here, player actually dies
565
566                 Unfreeze(self); // remove any icy remains
567                 self.health = 0; // Unfreeze resets health, so we need to set it back
568
569                 // clear waypoints
570                 WaypointSprite_PlayerDead();
571                 // throw a weapon
572                 SpawnThrownWeapon (self.origin + (self.mins + self.maxs) * 0.5, self.switchweapon);
573
574                 // become fully visible
575                 self.alpha = default_player_alpha;
576                 // make the corpse upright (not tilted)
577                 self.angles_x = 0;
578                 self.angles_z = 0;
579                 // don't spin
580                 self.avelocity = '0 0 0';
581                 // view from the floor
582                 self.view_ofs = '0 0 -8';
583                 // toss the corpse
584                 self.movetype = MOVETYPE_TOSS;
585                 // shootable corpse
586                 self.solid = SOLID_CORPSE;
587                 self.ballistics_density = autocvar_g_ballistics_density_corpse;
588                 // don't stick to the floor
589                 self.flags &= ~FL_ONGROUND;
590                 // dying animation
591                 self.deadflag = DEAD_DYING;
592
593                 // when to allow respawn
594                 calculate_player_respawn_time();
595
596                 self.death_time = time;
597                 if (random() < 0.5)
598                         animdecide_setstate(self, self.anim_state | ANIMSTATE_DEAD1, true);
599                 else
600                         animdecide_setstate(self, self.anim_state | ANIMSTATE_DEAD2, true);
601                 if (self.maxs.z > 5)
602                 {
603                         self.maxs_z = 5;
604                         setsize(self, self.mins, self.maxs);
605                 }
606                 // set damage function to corpse damage
607                 self.event_damage = PlayerCorpseDamage;
608                 // call the corpse damage function just in case it wants to gib
609                 self.event_damage(inflictor, attacker, excess, deathtype, hitloc, force);
610
611                 // set up to fade out later
612                 SUB_SetFade (self, time + 6 + random (), 1);
613                 // reset body think wrapper broken by SUB_SetFade
614                 if(self.classname == "body" && self.think != CopyBody_Think) {
615                         self.CopyBody_think = self.think;
616                         self.CopyBody_nextthink = self.nextthink;
617                         self.think = CopyBody_Think;
618                         self.nextthink = time;
619                 }
620
621                 if(autocvar_sv_gentle > 0 || autocvar_ekg || self.classname == "body") {
622                         // remove corpse
623                         // clones don't run any animation code any more, so we must gib them when they die :(
624                         PlayerCorpseDamage (inflictor, attacker, autocvar_sv_gibhealth+1.0, deathtype, hitloc, force);
625                 }
626
627                 // reset fields the weapons may use just in case
628                 for (j = WEP_FIRST; j <= WEP_LAST; ++j)
629                 {
630                         WEP_ACTION(j, WR_RESETPLAYER);
631                         ATTACK_FINISHED_FOR(self, j) = 0;
632                 }
633         }
634 }
635
636 float Say(entity source, float teamsay, entity privatesay, string msgin, float floodcontrol)
637 // message "": do not say, just test flood control
638 // return value:
639 //   1 = accept
640 //   0 = reject
641 //  -1 = fake accept
642 {
643         string msgstr, colorstr, cmsgstr, namestr, fullmsgstr, sourcemsgstr, fullcmsgstr, sourcecmsgstr, colorprefix;
644         float flood;
645         var .float flood_field;
646         entity head;
647         float ret;
648         string privatemsgprefix = string_null; float privatemsgprefixlen = 0;
649
650         if(!teamsay && !privatesay)
651                 if(substring(msgin, 0, 1) == " ")
652                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
653
654         msgin = formatmessage(msgin);
655
656         if (!IS_PLAYER(source))
657                 colorstr = "^0"; // black for spectators
658         else if(teamplay)
659                 colorstr = Team_ColorCode(source.team);
660         else
661         {
662                 colorstr = "";
663                 teamsay = false;
664         }
665
666         if(intermission_running)
667                 teamsay = false;
668
669         if(msgin != "")
670                 msgin = trigger_magicear_processmessage_forallears(source, teamsay, privatesay, msgin);
671
672         /*
673          * using bprint solves this... me stupid
674         // how can we prevent the message from appearing in a listen server?
675         // for now, just give "say" back and only handle say_team
676         if(!teamsay)
677         {
678                 clientcommand(self, strcat("say ", msgin));
679                 return;
680         }
681         */
682
683         if(autocvar_g_chat_teamcolors)
684                 namestr = playername(source);
685         else
686                 namestr = source.netname;
687
688         if(strdecolorize(namestr) == namestr)
689                 colorprefix = "^3";
690         else
691                 colorprefix = "^7";
692
693         if(msgin != "")
694         {
695                 if(privatesay)
696                 {
697                         msgstr = strcat("\{1}\{13}* ", colorprefix, namestr, "^3 tells you: ^7");
698                         privatemsgprefixlen = strlen(msgstr);
699                         msgstr = strcat(msgstr, msgin);
700                         cmsgstr = strcat(colorstr, colorprefix, namestr, "^3 tells you:\n^7", msgin);
701                         if(autocvar_g_chat_teamcolors)
702                                 privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", playername(privatesay), ": ^7");
703                         else
704                                 privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", privatesay.netname, ": ^7");
705                 }
706                 else if(teamsay)
707                 {
708                         msgstr = strcat("\{1}\{13}", colorstr, "(", colorprefix, namestr, colorstr, ") ^7", msgin);
709                         cmsgstr = strcat(colorstr, "(", colorprefix, namestr, colorstr, ")\n^7", msgin);
710                 }
711                 else
712                 {
713                         msgstr = strcat("\{1}", colorprefix, namestr, "^7: ", msgin);
714                         cmsgstr = "";
715                 }
716                 msgstr = strcat(strreplace("\n", " ", msgstr), "\n"); // newlines only are good for centerprint
717         }
718         else
719         {
720                 msgstr = cmsgstr = "";
721         }
722
723         fullmsgstr = msgstr;
724         fullcmsgstr = cmsgstr;
725
726         // FLOOD CONTROL
727         flood = 0;
728         flood_field = floodcontrol_chat;
729         if(floodcontrol)
730         {
731                 float flood_spl;
732                 float flood_burst;
733                 float flood_lmax;
734                 float lines;
735                 if(privatesay)
736                 {
737                         flood_spl = autocvar_g_chat_flood_spl_tell;
738                         flood_burst = autocvar_g_chat_flood_burst_tell;
739                         flood_lmax = autocvar_g_chat_flood_lmax_tell;
740                         flood_field = floodcontrol_chattell;
741                 }
742                 else if(teamsay)
743                 {
744                         flood_spl = autocvar_g_chat_flood_spl_team;
745                         flood_burst = autocvar_g_chat_flood_burst_team;
746                         flood_lmax = autocvar_g_chat_flood_lmax_team;
747                         flood_field = floodcontrol_chatteam;
748                 }
749                 else
750                 {
751                         flood_spl = autocvar_g_chat_flood_spl;
752                         flood_burst = autocvar_g_chat_flood_burst;
753                         flood_lmax = autocvar_g_chat_flood_lmax;
754                         flood_field = floodcontrol_chat;
755                 }
756                 flood_burst = max(0, flood_burst - 1);
757                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
758
759                 // do flood control for the default line size
760                 if(msgstr != "")
761                 {
762                         getWrappedLine_remaining = msgstr;
763                         msgstr = "";
764                         lines = 0;
765                         while(getWrappedLine_remaining && (!flood_lmax || lines <= flood_lmax))
766                         {
767                                 msgstr = strcat(msgstr, " ", getWrappedLineLen(82.4289758859709, strlennocol)); // perl averagewidth.pl < gfx/vera-sans.width
768                                 ++lines;
769                         }
770                         msgstr = substring(msgstr, 1, strlen(msgstr) - 1);
771
772                         if(getWrappedLine_remaining != "")
773                         {
774                                 msgstr = strcat(msgstr, "\n");
775                                 flood = 2;
776                         }
777
778                         if (time >= source.(flood_field))
779                         {
780                                 source.(flood_field) = max(time - flood_burst * flood_spl, source.(flood_field)) + lines * flood_spl;
781                         }
782                         else
783                         {
784                                 flood = 1;
785                                 msgstr = fullmsgstr;
786                         }
787                 }
788                 else
789                 {
790                         if (time >= source.(flood_field))
791                                 source.(flood_field) = max(time - flood_burst * flood_spl, source.(flood_field)) + flood_spl;
792                         else
793                                 flood = 1;
794                 }
795
796                 if (timeout_status == TIMEOUT_ACTIVE) // when game is paused, no flood protection
797                         source.(flood_field) = flood = 0;
798         }
799
800         if(flood == 2) // cannot happen for empty msgstr
801         {
802                 if(autocvar_g_chat_flood_notify_flooder)
803                 {
804                         sourcemsgstr = strcat(msgstr, "\n^3FLOOD CONTROL: ^7message too long, trimmed\n");
805                         sourcecmsgstr = "";
806                 }
807                 else
808                 {
809                         sourcemsgstr = fullmsgstr;
810                         sourcecmsgstr = fullcmsgstr;
811                 }
812                 cmsgstr = "";
813         }
814         else
815         {
816                 sourcemsgstr = msgstr;
817                 sourcecmsgstr = cmsgstr;
818         }
819
820         if(!privatesay)
821         if (!IS_PLAYER(source))
822         {
823                 if (!intermission_running)
824                         if(teamsay || (autocvar_g_chat_nospectators == 1) || (autocvar_g_chat_nospectators == 2 && !(warmup_stage || gameover)))
825                                 teamsay = -1; // spectators
826         }
827
828         if(flood)
829                 print("NOTE: ", playername(source), "^7 is flooding.\n");
830
831         // build sourcemsgstr by cutting off a prefix and replacing it by the other one
832         if(privatesay)
833                 sourcemsgstr = strcat(privatemsgprefix, substring(sourcemsgstr, privatemsgprefixlen, -1));
834
835         if(source.muted)
836         {
837                 // always fake the message
838                 ret = -1;
839         }
840         else if(flood == 1)
841         {
842                 if (autocvar_g_chat_flood_notify_flooder)
843                 {
844                         sprint(source, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(source.(flood_field) - time), "^3 seconds\n"));
845                         ret = 0;
846                 }
847                 else
848                         ret = -1;
849         }
850         else
851         {
852                 ret = 1;
853         }
854
855         if(sourcemsgstr != "" && ret != 0)
856         {
857                 if(ret < 0) // faked message, because the player is muted
858                 {
859                         sprint(source, sourcemsgstr);
860                         if(sourcecmsgstr != "" && !privatesay)
861                                 centerprint(source, sourcecmsgstr);
862                 }
863                 else if(privatesay) // private message, between 2 people only
864                 {
865                         sprint(source, sourcemsgstr);
866                         sprint(privatesay, msgstr);
867                         if (!autocvar_g_chat_tellprivacy) { dedicated_print(msgstr); } // send to server console too if "tellprivacy" is disabled
868                         if(cmsgstr != "")
869                                 centerprint(privatesay, cmsgstr);
870                 }
871                 else if(teamsay > 0) // team message, only sent to team mates
872                 {
873                         sprint(source, sourcemsgstr);
874                         dedicated_print(msgstr); // send to server console too
875                         if(sourcecmsgstr != "")
876                                 centerprint(source, sourcecmsgstr);
877                         FOR_EACH_REALPLAYER(head) if(head.team == source.team)
878                                 if(head != source)
879                                 {
880                                         sprint(head, msgstr);
881                                         if(cmsgstr != "")
882                                                 centerprint(head, cmsgstr);
883                                 }
884                 }
885                 else if(teamsay < 0) // spectator message, only sent to spectators
886                 {
887                         sprint(source, sourcemsgstr);
888                         dedicated_print(msgstr); // send to server console too
889                         FOR_EACH_REALCLIENT(head) if (!IS_PLAYER(head))
890                                 if(head != source)
891                                         sprint(head, msgstr);
892                 }
893                 else if(sourcemsgstr != msgstr) // trimmed/server fixed message, sent to all players
894                 {
895                         sprint(source, sourcemsgstr);
896                         dedicated_print(msgstr); // send to server console too
897                         FOR_EACH_REALCLIENT(head)
898                                 if(head != source)
899                                         sprint(head, msgstr);
900                 }
901                 else
902                         bprint(msgstr); // entirely normal message, sent to all players -- bprint sends to server console too.
903         }
904
905         return ret;
906 }
907
908 float GetVoiceMessageVoiceType(string type)
909 {
910         if(type == "taunt")
911                 return VOICETYPE_TAUNT;
912         if(type == "teamshoot")
913                 return VOICETYPE_LASTATTACKER;
914         return VOICETYPE_TEAMRADIO;
915 }
916
917 .string GetVoiceMessageSampleField(string type)
918 {
919         GetPlayerSoundSampleField_notFound = 0;
920         switch(type)
921         {
922 #define _VOICEMSG(m) case #m: return playersound_##m;
923                 ALLVOICEMSGS
924 #undef _VOICEMSG
925         }
926         GetPlayerSoundSampleField_notFound = 1;
927         return playersound_taunt;
928 }
929
930 .string GetPlayerSoundSampleField(string type)
931 {
932         GetPlayerSoundSampleField_notFound = 0;
933         switch(type)
934         {
935 #define _VOICEMSG(m) case #m: return playersound_##m;
936                 ALLPLAYERSOUNDS
937 #undef _VOICEMSG
938         }
939         GetPlayerSoundSampleField_notFound = 1;
940         return playersound_taunt;
941 }
942
943 void PrecacheGlobalSound(string samplestring)
944 {
945         float n, i;
946         tokenize_console(samplestring);
947         n = stof(argv(1));
948         if(n > 0)
949         {
950                 for(i = 1; i <= n; ++i)
951                         precache_sound(strcat(argv(0), ftos(i), ".wav"));
952         }
953         else
954         {
955                 precache_sound(strcat(argv(0), ".wav"));
956         }
957 }
958
959 void PrecachePlayerSounds(string f)
960 {
961         float fh;
962         string s;
963         fh = fopen(f, FILE_READ);
964         if(fh < 0)
965                 return;
966         while((s = fgets(fh)))
967         {
968                 if(tokenize_console(s) != 3)
969                 {
970                         dprint("Invalid sound info line: ", s, "\n");
971                         continue;
972                 }
973                 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
974         }
975         fclose(fh);
976
977         if (!allvoicesamples)
978         {
979 #define _VOICEMSG(m) allvoicesamples = strcat(allvoicesamples, " ", #m);
980                 ALLVOICEMSGS
981 #undef _VOICEMSG
982                 allvoicesamples = strzone(substring(allvoicesamples, 1, strlen(allvoicesamples) - 1));
983         }
984 }
985
986 void ClearPlayerSounds()
987 {
988 #define _VOICEMSG(m) if(self.playersound_##m) { strunzone(self.playersound_##m); self.playersound_##m = string_null; }
989         ALLPLAYERSOUNDS
990         ALLVOICEMSGS
991 #undef _VOICEMSG
992 }
993
994 float LoadPlayerSounds(string f, float first)
995 {
996         float fh;
997         string s;
998         var .string field;
999         fh = fopen(f, FILE_READ);
1000         if(fh < 0)
1001         {
1002                 dprint("Player sound file not found: ", f, "\n");
1003                 return 0;
1004         }
1005         while((s = fgets(fh)))
1006         {
1007                 if(tokenize_console(s) != 3)
1008                         continue;
1009                 field = GetPlayerSoundSampleField(argv(0));
1010                 if(GetPlayerSoundSampleField_notFound)
1011                         field = GetVoiceMessageSampleField(argv(0));
1012                 if(GetPlayerSoundSampleField_notFound)
1013                         continue;
1014                 if (self.(field))
1015                         strunzone(self.(field));
1016                 self.(field) = strzone(strcat(argv(1), " ", argv(2)));
1017         }
1018         fclose(fh);
1019         return 1;
1020 }
1021
1022 void UpdatePlayerSounds()
1023 {
1024         if(self.modelindex == self.modelindex_for_playersound)
1025         if(self.skin == self.skin_for_playersound)
1026                 return;
1027         self.modelindex_for_playersound = self.modelindex;
1028         self.skin_for_playersound = self.skin;
1029         ClearPlayerSounds();
1030         LoadPlayerSounds("sound/player/default.sounds", 1);
1031         if(!autocvar_g_debug_defaultsounds)
1032                 if(!LoadPlayerSounds(get_model_datafilename(self.model, self.skin, "sounds"), 0))
1033                         LoadPlayerSounds(get_model_datafilename(self.model, 0, "sounds"), 0);
1034 }
1035
1036 void FakeGlobalSound(string sample, float chan, float voicetype)
1037 {
1038         float n;
1039         float tauntrand;
1040
1041         if(sample == "")
1042                 return;
1043
1044         tokenize_console(sample);
1045         n = stof(argv(1));
1046         if(n > 0)
1047                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
1048         else
1049                 sample = strcat(argv(0), ".wav"); // randomization
1050
1051         switch(voicetype)
1052         {
1053                 case VOICETYPE_LASTATTACKER_ONLY:
1054                         break;
1055                 case VOICETYPE_LASTATTACKER:
1056                         if(self.pusher)
1057                         {
1058                                 msg_entity = self;
1059                                 if(IS_REAL_CLIENT(msg_entity))
1060                                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NONE);
1061                         }
1062                         break;
1063                 case VOICETYPE_TEAMRADIO:
1064                         msg_entity = self;
1065                         if(msg_entity.cvar_cl_voice_directional == 1)
1066                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1067                         else
1068                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1069                         break;
1070                 case VOICETYPE_AUTOTAUNT:
1071                         if(!sv_autotaunt)
1072                                 break;
1073                         if(!sv_taunt)
1074                                 break;
1075                         if(autocvar_sv_gentle)
1076                                 break;
1077                         tauntrand = random();
1078                         msg_entity = self;
1079                         if (tauntrand < msg_entity.cvar_cl_autotaunt)
1080                         {
1081                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1082                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1083                                 else
1084                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1085                         }
1086                         break;
1087                 case VOICETYPE_TAUNT:
1088                         if(IS_PLAYER(self))
1089                                 if(self.deadflag == DEAD_NO)
1090                                         animdecide_setaction(self, ANIMACTION_TAUNT, true);
1091                         if(!sv_taunt)
1092                                 break;
1093                         if(autocvar_sv_gentle)
1094                                 break;
1095                         msg_entity = self;
1096                         if (msg_entity.cvar_cl_voice_directional >= 1)
1097                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1098                         else
1099                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1100                         break;
1101                 case VOICETYPE_PLAYERSOUND:
1102                         msg_entity = self;
1103                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NORM);
1104                         break;
1105                 default:
1106                         backtrace("Invalid voice type!");
1107                         break;
1108         }
1109 }
1110
1111 void GlobalSound(string sample, float chan, float voicetype)
1112 {
1113         float n;
1114         float tauntrand;
1115
1116         if(sample == "")
1117                 return;
1118
1119         tokenize_console(sample);
1120         n = stof(argv(1));
1121         if(n > 0)
1122                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
1123         else
1124                 sample = strcat(argv(0), ".wav"); // randomization
1125
1126         switch(voicetype)
1127         {
1128                 case VOICETYPE_LASTATTACKER_ONLY:
1129                         if(self.pusher)
1130                         {
1131                                 msg_entity = self.pusher;
1132                                 if(IS_REAL_CLIENT(msg_entity))
1133                                 {
1134                                         if(msg_entity.cvar_cl_voice_directional == 1)
1135                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1136                                         else
1137                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1138                                 }
1139                         }
1140                         break;
1141                 case VOICETYPE_LASTATTACKER:
1142                         if(self.pusher)
1143                         {
1144                                 msg_entity = self.pusher;
1145                                 if(IS_REAL_CLIENT(msg_entity))
1146                                 {
1147                                         if(msg_entity.cvar_cl_voice_directional == 1)
1148                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1149                                         else
1150                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1151                                 }
1152                                 msg_entity = self;
1153                                 if(IS_REAL_CLIENT(msg_entity))
1154                                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NONE);
1155                         }
1156                         break;
1157                 case VOICETYPE_TEAMRADIO:
1158                         FOR_EACH_REALCLIENT(msg_entity)
1159                                 if(!teamplay || msg_entity.team == self.team)
1160                                 {
1161                                         if(msg_entity.cvar_cl_voice_directional == 1)
1162                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1163                                         else
1164                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1165                                 }
1166                         break;
1167                 case VOICETYPE_AUTOTAUNT:
1168                         if(!sv_autotaunt)
1169                                 break;
1170                         if(!sv_taunt)
1171                                 break;
1172                         if(autocvar_sv_gentle)
1173                                 break;
1174                         tauntrand = random();
1175                         FOR_EACH_REALCLIENT(msg_entity)
1176                                 if (tauntrand < msg_entity.cvar_cl_autotaunt)
1177                                 {
1178                                         if (msg_entity.cvar_cl_voice_directional >= 1)
1179                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1180                                         else
1181                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1182                                 }
1183                         break;
1184                 case VOICETYPE_TAUNT:
1185                         if(IS_PLAYER(self))
1186                                 if(self.deadflag == DEAD_NO)
1187                                         animdecide_setaction(self, ANIMACTION_TAUNT, true);
1188                         if(!sv_taunt)
1189                                 break;
1190                         if(autocvar_sv_gentle)
1191                                 break;
1192                         FOR_EACH_REALCLIENT(msg_entity)
1193                         {
1194                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1195                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1196                                 else
1197                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1198                         }
1199                         break;
1200                 case VOICETYPE_PLAYERSOUND:
1201                         sound(self, chan, sample, VOL_BASE, ATTEN_NORM);
1202                         break;
1203                 default:
1204                         backtrace("Invalid voice type!");
1205                         break;
1206         }
1207 }
1208
1209 void PlayerSound(.string samplefield, float chan, float voicetype)
1210 {
1211         GlobalSound(self.(samplefield), chan, voicetype);
1212 }
1213
1214 void VoiceMessage(string type, string msg)
1215 {
1216         var .string sample;
1217         float voicetype, ownteam;
1218         float flood;
1219         sample = GetVoiceMessageSampleField(type);
1220
1221         if(GetPlayerSoundSampleField_notFound)
1222         {
1223                 sprint(self, strcat("Invalid voice. Use one of: ", allvoicesamples, "\n"));
1224                 return;
1225         }
1226
1227         voicetype = GetVoiceMessageVoiceType(type);
1228         ownteam = (voicetype == VOICETYPE_TEAMRADIO);
1229
1230         flood = Say(self, ownteam, world, msg, 1);
1231
1232         if (IS_SPEC(self) || IS_OBSERVER(self) || flood < 0)
1233                 FakeGlobalSound(self.(sample), CH_VOICE, voicetype);
1234         else if (flood > 0)
1235                 GlobalSound(self.(sample), CH_VOICE, voicetype);
1236 }
1237
1238 void MoveToTeam(entity client, float team_colour, float type)
1239 {
1240         float lockteams_backup;
1241
1242         lockteams_backup = lockteams;  // backup any team lock
1243
1244         lockteams = 0;  // disable locked teams
1245
1246         TeamchangeFrags(client);  // move the players frags
1247         SetPlayerColors(client, team_colour - 1);  // set the players colour
1248         Damage(client, client, client, 100000, DEATH_AUTOTEAMCHANGE, client.origin, '0 0 0');  // kill the player
1249
1250         lockteams = lockteams_backup;  // restore the team lock
1251
1252         LogTeamchange(client.playerid, client.team, type);
1253 }