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