]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_player.qc
Merge branch 'master' into Melanosuchus/minigames
[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 "../common/minigames/sv_minigames.qh"
7
8 #include "weapons/weaponstats.qh"
9
10 void CopyBody_Think(void)
11 {
12         if(self.CopyBody_nextthink && time > self.CopyBody_nextthink)
13         {
14                 self.CopyBody_think();
15                 if(wasfreed(self))
16                         return;
17                 self.CopyBody_nextthink = self.nextthink;
18                 self.CopyBody_think = self.think;
19                 self.think = CopyBody_Think;
20         }
21         CSQCMODEL_AUTOUPDATE();
22         self.nextthink = time;
23 }
24 void CopyBody(float keepvelocity)
25 {
26         entity oldself;
27         if (self.effects & EF_NODRAW)
28                 return;
29         oldself = self;
30         self = spawn();
31         self.enemy = oldself;
32         self.lip = oldself.lip;
33         self.colormap = oldself.colormap;
34         self.iscreature = oldself.iscreature;
35         self.teleportable = oldself.teleportable;
36         self.damagedbycontents = oldself.damagedbycontents;
37         self.angles = oldself.angles;
38         self.v_angle = oldself.v_angle;
39         self.avelocity = oldself.avelocity;
40         self.classname = "body";
41         self.damageforcescale = oldself.damageforcescale;
42         self.effects = oldself.effects;
43         self.glowmod = oldself.glowmod;
44         self.event_damage = oldself.event_damage;
45         self.anim_state = oldself.anim_state;
46         self.anim_time = oldself.anim_time;
47         self.anim_lower_action = oldself.anim_lower_action;
48         self.anim_lower_time = oldself.anim_lower_time;
49         self.anim_upper_action = oldself.anim_upper_action;
50         self.anim_upper_time = oldself.anim_upper_time;
51         self.anim_implicit_state = oldself.anim_implicit_state;
52         self.anim_implicit_time = oldself.anim_implicit_time;
53         self.anim_lower_implicit_action = oldself.anim_lower_implicit_action;
54         self.anim_lower_implicit_time = oldself.anim_lower_implicit_time;
55         self.anim_upper_implicit_action = oldself.anim_upper_implicit_action;
56         self.anim_upper_implicit_time = oldself.anim_upper_implicit_time;
57         self.dphitcontentsmask = oldself.dphitcontentsmask;
58         self.death_time = oldself.death_time;
59         self.pain_finished = oldself.pain_finished;
60         self.health = oldself.health;
61         self.armorvalue = oldself.armorvalue;
62         self.armortype = oldself.armortype;
63         self.model = oldself.model;
64         self.modelindex = oldself.modelindex;
65         self.skin = oldself.skin;
66         self.species = oldself.species;
67         self.movetype = oldself.movetype;
68         self.solid = oldself.solid;
69         self.ballistics_density = oldself.ballistics_density;
70         self.takedamage = oldself.takedamage;
71         self.customizeentityforclient = oldself.customizeentityforclient;
72         self.uncustomizeentityforclient = oldself.uncustomizeentityforclient;
73         self.uncustomizeentityforclient_set = oldself.uncustomizeentityforclient_set;
74         if (keepvelocity == 1)
75                 self.velocity = oldself.velocity;
76         self.oldvelocity = self.velocity;
77         self.alpha = oldself.alpha;
78         self.fade_time = oldself.fade_time;
79         self.fade_rate = oldself.fade_rate;
80         //self.weapon = oldself.weapon;
81         setorigin(self, oldself.origin);
82         setsize(self, oldself.mins, oldself.maxs);
83         self.prevorigin = oldself.origin;
84         self.reset = SUB_Remove;
85
86         Drag_MoveDrag(oldself, self);
87
88         if(self.colormap <= maxclients && self.colormap > 0)
89                 self.colormap = 1024 + oldself.clientcolors;
90
91         CSQCMODEL_AUTOINIT();
92         self.CopyBody_nextthink = oldself.nextthink;
93         self.CopyBody_think = oldself.think;
94         self.nextthink = time;
95         self.think = CopyBody_Think;
96         // "bake" the current animation frame for clones (they don't get clientside animation)
97         animdecide_load_if_needed(self);
98         animdecide_setframes(self, false, frame, frame1time, frame2, frame2time);
99
100         self = oldself;
101 }
102
103 float player_getspecies()
104 {
105         float s;
106         get_model_parameters(self.model, self.skin);
107         s = get_model_parameters_species;
108         get_model_parameters(string_null, 0);
109         if(s < 0)
110                 return SPECIES_HUMAN;
111         return s;
112 }
113
114 void player_setupanimsformodel()
115 {
116         // load animation info
117         animdecide_load_if_needed(self);
118         animdecide_setstate(self, 0, false);
119 }
120
121 void player_anim (void)
122 {
123         int deadbits = (self.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
124         if(self.deadflag) {
125                 if (!deadbits) {
126                         // Decide on which death animation to use.
127                         if(random() < 0.5)
128                                 deadbits = ANIMSTATE_DEAD1;
129                         else
130                                 deadbits = ANIMSTATE_DEAD2;
131                 }
132         } else {
133                 // Clear a previous death animation.
134                 deadbits = 0;
135         }
136         int animbits = deadbits;
137         if(self.frozen)
138                 animbits |= ANIMSTATE_FROZEN;
139         if(self.crouch)
140                 animbits |= ANIMSTATE_DUCK;
141         animdecide_setstate(self, animbits, false);
142         animdecide_setimplicitstate(self, (self.flags & FL_ONGROUND));
143
144         if (self.weaponentity)
145         {
146                 updateanim(self.weaponentity);
147                 if (!self.weaponentity.animstate_override)
148                         setanim(self.weaponentity, self.weaponentity.anim_idle, true, false, false);
149         }
150 }
151
152 void PlayerCorpseDamage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
153 {
154         float take, save;
155         vector v;
156         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
157
158         // damage resistance (ignore most of the damage from a bullet or similar)
159         damage = max(damage - 5, 1);
160
161         v = healtharmor_applydamage(self.armorvalue, autocvar_g_balance_armor_blockpercent, deathtype, damage);
162         take = v.x;
163         save = v.y;
164
165         if(sound_allowed(MSG_BROADCAST, attacker))
166         {
167                 if (save > 10)
168                         sound (self, CH_SHOTS, "misc/armorimpact.wav", VOL_BASE, ATTEN_NORM);
169                 else if (take > 30)
170                         sound (self, CH_SHOTS, "misc/bodyimpact2.wav", VOL_BASE, ATTEN_NORM);
171                 else if (take > 10)
172                         sound (self, CH_SHOTS, "misc/bodyimpact1.wav", VOL_BASE, ATTEN_NORM);
173         }
174
175         if (take > 50)
176                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
177         if (take > 100)
178                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
179
180         self.armorvalue = self.armorvalue - save;
181         self.health = self.health - take;
182         // pause regeneration for 5 seconds
183         self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
184
185         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
186         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
187         self.dmg_inflictor = inflictor;
188
189         if (self.health <= -autocvar_sv_gibhealth && self.alpha >= 0)
190         {
191                 // don't use any animations as a gib
192                 self.frame = 0;
193                 // view just above the floor
194                 self.view_ofs = '0 0 4';
195
196                 Violence_GibSplash(self, 1, 1, attacker);
197                 self.alpha = -1;
198                 self.solid = SOLID_NOT; // restore later
199                 self.takedamage = DAMAGE_NO; // restore later
200                 self.damagedbycontents = false;
201         }
202 }
203
204 void calculate_player_respawn_time()
205 {
206         if(g_ca)
207                 return;
208
209         float gametype_setting_tmp;
210         float sdelay_max = GAMETYPE_DEFAULTED_SETTING(respawn_delay_max);
211         float sdelay_small = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small);
212         float sdelay_large = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large);
213         float sdelay_small_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small_count);
214         float sdelay_large_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large_count);
215         float waves = GAMETYPE_DEFAULTED_SETTING(respawn_waves);
216
217         float pcount = 1;  // Include myself whether or not team is already set right and I'm a "player".
218         entity pl;
219         if (teamplay)
220         {
221                 FOR_EACH_PLAYER(pl)
222                         if (pl != self)
223                                 if (pl.team == self.team)
224                                         ++pcount;
225                 if (sdelay_small_count == 0)
226                         sdelay_small_count = 1;
227                 if (sdelay_large_count == 0)
228                         sdelay_large_count = 1;
229         }
230         else
231         {
232                 FOR_EACH_PLAYER(pl)
233                         if (pl != self)
234                                 ++pcount;
235                 if (sdelay_small_count == 0)
236                 {
237                         if (g_cts)
238                         {
239                                 // Players play independently. No point in requiring enemies.
240                                 sdelay_small_count = 1;
241                         }
242                         else
243                         {
244                                 // Players play AGAINST each other. Enemies required.
245                                 sdelay_small_count = 2;
246                         }
247                 }
248                 if (sdelay_large_count == 0)
249                 {
250                         if (g_cts)
251                         {
252                                 // Players play independently. No point in requiring enemies.
253                                 sdelay_large_count = 1;
254                         }
255                         else
256                         {
257                                 // Players play AGAINST each other. Enemies required.
258                                 sdelay_large_count = 2;
259                         }
260                 }
261         }
262
263         float sdelay;
264
265         if (pcount <= sdelay_small_count)
266                 sdelay = sdelay_small;
267         else if (pcount >= sdelay_large_count)
268                 sdelay = sdelay_large;
269         else  // NOTE: this case implies sdelay_large_count > sdelay_small_count.
270                 sdelay = sdelay_small + (sdelay_large - sdelay_small) * (pcount - sdelay_small_count) / (sdelay_large_count - sdelay_small_count);
271
272         if(waves)
273                 self.respawn_time = ceil((time + sdelay) / waves) * waves;
274         else
275                 self.respawn_time = time + sdelay;
276
277         if(sdelay < sdelay_max)
278                 self.respawn_time_max = time + sdelay_max;
279         else
280                 self.respawn_time_max = self.respawn_time;
281
282         if((sdelay + waves >= 5.0) && (self.respawn_time - time > 1.75))
283                 self.respawn_countdown = 10; // first number to count down from is 10
284         else
285                 self.respawn_countdown = -1; // do not count down
286
287         if(autocvar_g_forced_respawn)
288                 self.respawn_flags = self.respawn_flags | RESPAWN_FORCE;
289 }
290
291 void PlayerDamage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
292 {
293         float take, save, dh, da;
294         int j;
295         vector v;
296         float valid_damage_for_weaponstats;
297         float excess;
298
299         dh = max(self.health, 0);
300         da = max(self.armorvalue, 0);
301
302         if(!DEATH_ISSPECIAL(deathtype))
303         {
304                 damage *= sqrt(bound(1.0, self.cvar_cl_handicap, 100.0));
305                 if(self != attacker)
306                         damage /= sqrt(bound(1.0, attacker.cvar_cl_handicap, 100.0));
307         }
308
309         if(DEATH_ISWEAPON(deathtype, WEP_TUBA))
310         {
311                 // tuba causes blood to come out of the ears
312                 vector ear1, ear2;
313                 vector d;
314                 float f;
315                 ear1 = self.origin;
316                 ear1_z += 0.125 * self.view_ofs.z + 0.875 * self.maxs.z; // 7/8
317                 ear2 = ear1;
318                 makevectors(self.angles);
319                 ear1 += v_right * -10;
320                 ear2 += v_right * +10;
321                 d = inflictor.origin - self.origin;
322                 if (d)
323                         f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
324                 else
325                         f = 0;  // Assum ecenter.
326                 force = v_right * vlen(force);
327                 Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), self, attacker);
328                 Violence_GibSplash_At(ear2, force,      2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), self, attacker);
329                 if(f > 0)
330                 {
331                         hitloc = ear1;
332                         force = force * -1;
333                 }
334                 else
335                 {
336                         hitloc = ear2;
337                         // force is already good
338                 }
339         }
340         else
341                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
342
343
344         v = healtharmor_applydamage(self.armorvalue, autocvar_g_balance_armor_blockpercent, deathtype, damage);
345         take = v.x;
346         save = v.y;
347
348         if(attacker == self)
349         {
350                 // don't reset pushltime for self damage as it may be an attempt to
351                 // escape a lava pit or similar
352                 //self.pushltime = 0;
353                 self.istypefrag = 0;
354         }
355         else if(IS_PLAYER(attacker))
356         {
357                 self.pusher = attacker;
358                 self.pushltime = time + autocvar_g_maxpushtime;
359                 self.istypefrag = self.BUTTON_CHAT;
360         }
361         else if(time < self.pushltime)
362         {
363                 attacker = self.pusher;
364                 self.pushltime = max(self.pushltime, time + 0.6);
365         }
366         else
367         {
368                 self.pushltime = 0;
369                 self.istypefrag = 0;
370         }
371
372         frag_inflictor = inflictor;
373         frag_attacker = attacker;
374         frag_target = self;
375         frag_damage = damage;
376         damage_take = take;
377         damage_save = save;
378         damage_force = force;
379         MUTATOR_CALLHOOK(PlayerDamage_SplitHealthArmor);
380         take = bound(0, damage_take, self.health);
381         save = bound(0, damage_save, self.armorvalue);
382         excess = max(0, damage - take - save);
383
384         if(sound_allowed(MSG_BROADCAST, attacker))
385         {
386                 if (save > 10)
387                         sound (self, CH_SHOTS, "misc/armorimpact.wav", VOL_BASE, ATTEN_NORM);
388                 else if (take > 30)
389                         sound (self, CH_SHOTS, "misc/bodyimpact2.wav", VOL_BASE, ATTEN_NORM);
390                 else if (take > 10)
391                         sound (self, CH_SHOTS, "misc/bodyimpact1.wav", VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
392         }
393
394         if (take > 50)
395                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
396         if (take > 100)
397                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
398
399         if (time >= self.spawnshieldtime)
400         {
401                 if (!(self.flags & FL_GODMODE))
402                 {
403                         self.armorvalue = self.armorvalue - save;
404                         self.health = self.health - take;
405                         // pause regeneration for 5 seconds
406                         if(take)
407                                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
408
409                         if (time > self.pain_finished)          //Don't switch pain sequences like crazy
410                         {
411                                 self.pain_finished = time + 0.5;        //Supajoe
412
413                                 if(autocvar_sv_gentle < 1) {
414                                         if(self.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
415                                         {
416                                                 if (!self.animstate_override)
417                                                 {
418                                                         if (random() > 0.5)
419                                                                 animdecide_setaction(self, ANIMACTION_PAIN1, true);
420                                                         else
421                                                                 animdecide_setaction(self, ANIMACTION_PAIN2, true);
422                                                 }
423                                         }
424
425                                         if(sound_allowed(MSG_BROADCAST, attacker))
426                                         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
427                                         if(self.health > 1)
428                                         // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
429                                         {
430                                                 if(deathtype == DEATH_FALL)
431                                                         PlayerSound(playersound_fall, CH_PAIN, VOICETYPE_PLAYERSOUND);
432                                                 else if(self.health > 75) // TODO make a "gentle" version?
433                                                         PlayerSound(playersound_pain100, CH_PAIN, VOICETYPE_PLAYERSOUND);
434                                                 else if(self.health > 50)
435                                                         PlayerSound(playersound_pain75, CH_PAIN, VOICETYPE_PLAYERSOUND);
436                                                 else if(self.health > 25)
437                                                         PlayerSound(playersound_pain50, CH_PAIN, VOICETYPE_PLAYERSOUND);
438                                                 else
439                                                         PlayerSound(playersound_pain25, CH_PAIN, VOICETYPE_PLAYERSOUND);
440                                         }
441                                 }
442                         }
443
444                         // throw off bot aim temporarily
445                         float shake;
446                         if(IS_BOT_CLIENT(self) && self.health >= 1)
447                         {
448                                 shake = damage * 5 / (bound(0,skill,100) + 1);
449                                 self.v_angle_x = self.v_angle.x + (random() * 2 - 1) * shake;
450                                 self.v_angle_y = self.v_angle.y + (random() * 2 - 1) * shake;
451                                 self.v_angle_x = bound(-90, self.v_angle.x, 90);
452                         }
453                 }
454                 else
455                         self.max_armorvalue += (save + take);
456         }
457         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
458         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
459         self.dmg_inflictor = inflictor;
460
461         float abot, vbot, awep;
462         abot = (IS_BOT_CLIENT(attacker));
463         vbot = (IS_BOT_CLIENT(self));
464
465         valid_damage_for_weaponstats = 0;
466         awep = 0;
467
468         if(vbot || IS_REAL_CLIENT(self))
469         if(abot || IS_REAL_CLIENT(attacker))
470         if(attacker && self != attacker)
471         if(DIFF_TEAM(self, attacker))
472         {
473                 if(DEATH_ISSPECIAL(deathtype))
474                         awep = attacker.weapon;
475                 else
476                         awep = DEATH_WEAPONOF(deathtype);
477                 valid_damage_for_weaponstats = 1;
478         }
479
480         if(valid_damage_for_weaponstats)
481         {
482                 dh = dh - max(self.health, 0);
483                 da = da - max(self.armorvalue, 0);
484                 WeaponStats_LogDamage(awep, abot, self.weapon, vbot, dh + da);
485         }
486
487         if (self.health < 1)
488         {
489                 float defer_ClientKill_Now_TeamChange;
490                 defer_ClientKill_Now_TeamChange = false;
491
492                 if(self.alivetime)
493                 {
494                         PS_GR_P_ADDVAL(self, PLAYERSTATS_ALIVETIME, time - self.alivetime);
495                         self.alivetime = 0;
496                 }
497
498                 if(valid_damage_for_weaponstats)
499                         WeaponStats_LogKill(awep, abot, self.weapon, vbot);
500
501                 if(autocvar_sv_gentle < 1) // TODO make a "gentle" version?
502                 if(sound_allowed(MSG_BROADCAST, attacker))
503                 {
504                         if(deathtype == DEATH_DROWN)
505                                 PlayerSound(playersound_drown, CH_PAIN, VOICETYPE_PLAYERSOUND);
506                         else
507                                 PlayerSound(playersound_death, CH_PAIN, VOICETYPE_PLAYERSOUND);
508                 }
509
510                 // get rid of kill indicator
511                 if(self.killindicator)
512                 {
513                         remove(self.killindicator);
514                         self.killindicator = world;
515                         if(self.killindicator_teamchange)
516                                 defer_ClientKill_Now_TeamChange = true;
517
518                         if(self.classname == "body")
519                         if(deathtype == DEATH_KILL)
520                         {
521                                 // for the lemmings fans, a small harmless explosion
522                                 pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
523                         }
524                 }
525
526                 // print an obituary message
527                 if(self.classname != "body")
528                         Obituary (attacker, inflictor, self, deathtype);
529
530         // increment frag counter for used weapon type
531         int w = DEATH_WEAPONOF(deathtype);
532         if(WEP_VALID(w))
533         if(accuracy_isgooddamage(attacker, self))
534         attacker.accuracy.(accuracy_frags[w-1]) += 1;
535
536                 frag_attacker = attacker;
537                 frag_inflictor = inflictor;
538                 frag_target = self;
539                 frag_deathtype = deathtype;
540                 MUTATOR_CALLHOOK(PlayerDies);
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 && source.active_minigame )
866                 {
867                         sprint(source, sourcemsgstr);
868                         dedicated_print(msgstr); // send to server console too
869                         FOR_EACH_REALCLIENT(head) 
870                                 if(head != source)
871                                 if(head.active_minigame == source.active_minigame)
872                                         sprint(head, msgstr);
873                 }
874                 else if(teamsay > 0) // team message, only sent to team mates
875                 {
876                         sprint(source, sourcemsgstr);
877                         dedicated_print(msgstr); // send to server console too
878                         if(sourcecmsgstr != "")
879                                 centerprint(source, sourcecmsgstr);
880                         FOR_EACH_REALPLAYER(head) if(head.team == source.team)
881                                 if(head != source)
882                                 {
883                                         sprint(head, msgstr);
884                                         if(cmsgstr != "")
885                                                 centerprint(head, cmsgstr);
886                                 }
887                 }
888                 else if(teamsay < 0) // spectator message, only sent to spectators
889                 {
890                         sprint(source, sourcemsgstr);
891                         dedicated_print(msgstr); // send to server console too
892                         FOR_EACH_REALCLIENT(head) if (!IS_PLAYER(head))
893                                 if(head != source)
894                                         sprint(head, msgstr);
895                 }
896                 else if(sourcemsgstr != msgstr) // trimmed/server fixed message, sent to all players
897                 {
898                         sprint(source, sourcemsgstr);
899                         dedicated_print(msgstr); // send to server console too
900                         FOR_EACH_REALCLIENT(head)
901                                 if(head != source)
902                                         sprint(head, msgstr);
903                 }
904                 else
905                         bprint(msgstr); // entirely normal message, sent to all players -- bprint sends to server console too.
906         }
907
908         return ret;
909 }
910
911 float GetVoiceMessageVoiceType(string type)
912 {
913         if(type == "taunt")
914                 return VOICETYPE_TAUNT;
915         if(type == "teamshoot")
916                 return VOICETYPE_LASTATTACKER;
917         return VOICETYPE_TEAMRADIO;
918 }
919
920 .string GetVoiceMessageSampleField(string type)
921 {
922         GetPlayerSoundSampleField_notFound = 0;
923         switch(type)
924         {
925 #define _VOICEMSG(m) case #m: return playersound_##m;
926                 ALLVOICEMSGS
927 #undef _VOICEMSG
928         }
929         GetPlayerSoundSampleField_notFound = 1;
930         return playersound_taunt;
931 }
932
933 .string GetPlayerSoundSampleField(string type)
934 {
935         GetPlayerSoundSampleField_notFound = 0;
936         switch(type)
937         {
938 #define _VOICEMSG(m) case #m: return playersound_##m;
939                 ALLPLAYERSOUNDS
940 #undef _VOICEMSG
941         }
942         GetPlayerSoundSampleField_notFound = 1;
943         return playersound_taunt;
944 }
945
946 void PrecacheGlobalSound(string samplestring)
947 {
948         float n, i;
949         tokenize_console(samplestring);
950         n = stof(argv(1));
951         if(n > 0)
952         {
953                 for(i = 1; i <= n; ++i)
954                         precache_sound(strcat(argv(0), ftos(i), ".wav"));
955         }
956         else
957         {
958                 precache_sound(strcat(argv(0), ".wav"));
959         }
960 }
961
962 void PrecachePlayerSounds(string f)
963 {
964         float fh;
965         string s;
966         fh = fopen(f, FILE_READ);
967         if(fh < 0)
968                 return;
969         while((s = fgets(fh)))
970         {
971                 if(tokenize_console(s) != 3)
972                 {
973                         dprint("Invalid sound info line: ", s, "\n");
974                         continue;
975                 }
976                 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
977         }
978         fclose(fh);
979
980         if (!allvoicesamples)
981         {
982 #define _VOICEMSG(m) allvoicesamples = strcat(allvoicesamples, " ", #m);
983                 ALLVOICEMSGS
984 #undef _VOICEMSG
985                 allvoicesamples = strzone(substring(allvoicesamples, 1, strlen(allvoicesamples) - 1));
986         }
987 }
988
989 void ClearPlayerSounds()
990 {
991 #define _VOICEMSG(m) if(self.playersound_##m) { strunzone(self.playersound_##m); self.playersound_##m = string_null; }
992         ALLPLAYERSOUNDS
993         ALLVOICEMSGS
994 #undef _VOICEMSG
995 }
996
997 float LoadPlayerSounds(string f, float first)
998 {
999         float fh;
1000         string s;
1001         var .string field;
1002         fh = fopen(f, FILE_READ);
1003         if(fh < 0)
1004         {
1005                 dprint("Player sound file not found: ", f, "\n");
1006                 return 0;
1007         }
1008         while((s = fgets(fh)))
1009         {
1010                 if(tokenize_console(s) != 3)
1011                         continue;
1012                 field = GetPlayerSoundSampleField(argv(0));
1013                 if(GetPlayerSoundSampleField_notFound)
1014                         field = GetVoiceMessageSampleField(argv(0));
1015                 if(GetPlayerSoundSampleField_notFound)
1016                         continue;
1017                 if(self.field)
1018                         strunzone(self.field);
1019                 self.field = strzone(strcat(argv(1), " ", argv(2)));
1020         }
1021         fclose(fh);
1022         return 1;
1023 }
1024
1025 void UpdatePlayerSounds()
1026 {
1027         if(self.modelindex == self.modelindex_for_playersound)
1028         if(self.skin == self.skin_for_playersound)
1029                 return;
1030         self.modelindex_for_playersound = self.modelindex;
1031         self.skin_for_playersound = self.skin;
1032         ClearPlayerSounds();
1033         LoadPlayerSounds("sound/player/default.sounds", 1);
1034         if(!autocvar_g_debug_defaultsounds)
1035                 if(!LoadPlayerSounds(get_model_datafilename(self.model, self.skin, "sounds"), 0))
1036                         LoadPlayerSounds(get_model_datafilename(self.model, 0, "sounds"), 0);
1037 }
1038
1039 void FakeGlobalSound(string sample, float chan, float voicetype)
1040 {
1041         float n;
1042         float tauntrand;
1043
1044         if(sample == "")
1045                 return;
1046
1047         tokenize_console(sample);
1048         n = stof(argv(1));
1049         if(n > 0)
1050                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
1051         else
1052                 sample = strcat(argv(0), ".wav"); // randomization
1053
1054         switch(voicetype)
1055         {
1056                 case VOICETYPE_LASTATTACKER_ONLY:
1057                         break;
1058                 case VOICETYPE_LASTATTACKER:
1059                         if(self.pusher)
1060                         {
1061                                 msg_entity = self;
1062                                 if(IS_REAL_CLIENT(msg_entity))
1063                                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NONE);
1064                         }
1065                         break;
1066                 case VOICETYPE_TEAMRADIO:
1067                         msg_entity = self;
1068                         if(msg_entity.cvar_cl_voice_directional == 1)
1069                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1070                         else
1071                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1072                         break;
1073                 case VOICETYPE_AUTOTAUNT:
1074                         if(!sv_autotaunt)
1075                                 break;
1076                         if(!sv_taunt)
1077                                 break;
1078                         if(autocvar_sv_gentle)
1079                                 break;
1080                         tauntrand = random();
1081                         msg_entity = self;
1082                         if (tauntrand < msg_entity.cvar_cl_autotaunt)
1083                         {
1084                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1085                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1086                                 else
1087                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1088                         }
1089                         break;
1090                 case VOICETYPE_TAUNT:
1091                         if(IS_PLAYER(self))
1092                                 if(self.deadflag == DEAD_NO)
1093                                         animdecide_setaction(self, ANIMACTION_TAUNT, true);
1094                         if(!sv_taunt)
1095                                 break;
1096                         if(autocvar_sv_gentle)
1097                                 break;
1098                         msg_entity = self;
1099                         if (msg_entity.cvar_cl_voice_directional >= 1)
1100                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1101                         else
1102                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1103                         break;
1104                 case VOICETYPE_PLAYERSOUND:
1105                         msg_entity = self;
1106                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NORM);
1107                         break;
1108                 default:
1109                         backtrace("Invalid voice type!");
1110                         break;
1111         }
1112 }
1113
1114 void GlobalSound(string sample, float chan, float voicetype)
1115 {
1116         float n;
1117         float tauntrand;
1118
1119         if(sample == "")
1120                 return;
1121
1122         tokenize_console(sample);
1123         n = stof(argv(1));
1124         if(n > 0)
1125                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
1126         else
1127                 sample = strcat(argv(0), ".wav"); // randomization
1128
1129         switch(voicetype)
1130         {
1131                 case VOICETYPE_LASTATTACKER_ONLY:
1132                         if(self.pusher)
1133                         {
1134                                 msg_entity = self.pusher;
1135                                 if(IS_REAL_CLIENT(msg_entity))
1136                                 {
1137                                         if(msg_entity.cvar_cl_voice_directional == 1)
1138                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1139                                         else
1140                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1141                                 }
1142                         }
1143                         break;
1144                 case VOICETYPE_LASTATTACKER:
1145                         if(self.pusher)
1146                         {
1147                                 msg_entity = self.pusher;
1148                                 if(IS_REAL_CLIENT(msg_entity))
1149                                 {
1150                                         if(msg_entity.cvar_cl_voice_directional == 1)
1151                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1152                                         else
1153                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1154                                 }
1155                                 msg_entity = self;
1156                                 if(IS_REAL_CLIENT(msg_entity))
1157                                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NONE);
1158                         }
1159                         break;
1160                 case VOICETYPE_TEAMRADIO:
1161                         FOR_EACH_REALCLIENT(msg_entity)
1162                                 if(!teamplay || msg_entity.team == self.team)
1163                                 {
1164                                         if(msg_entity.cvar_cl_voice_directional == 1)
1165                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1166                                         else
1167                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1168                                 }
1169                         break;
1170                 case VOICETYPE_AUTOTAUNT:
1171                         if(!sv_autotaunt)
1172                                 break;
1173                         if(!sv_taunt)
1174                                 break;
1175                         if(autocvar_sv_gentle)
1176                                 break;
1177                         tauntrand = random();
1178                         FOR_EACH_REALCLIENT(msg_entity)
1179                                 if (tauntrand < msg_entity.cvar_cl_autotaunt)
1180                                 {
1181                                         if (msg_entity.cvar_cl_voice_directional >= 1)
1182                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1183                                         else
1184                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1185                                 }
1186                         break;
1187                 case VOICETYPE_TAUNT:
1188                         if(IS_PLAYER(self))
1189                                 if(self.deadflag == DEAD_NO)
1190                                         animdecide_setaction(self, ANIMACTION_TAUNT, true);
1191                         if(!sv_taunt)
1192                                 break;
1193                         if(autocvar_sv_gentle)
1194                                 break;
1195                         FOR_EACH_REALCLIENT(msg_entity)
1196                         {
1197                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1198                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1199                                 else
1200                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1201                         }
1202                         break;
1203                 case VOICETYPE_PLAYERSOUND:
1204                         sound(self, chan, sample, VOL_BASE, ATTEN_NORM);
1205                         break;
1206                 default:
1207                         backtrace("Invalid voice type!");
1208                         break;
1209         }
1210 }
1211
1212 void PlayerSound(.string samplefield, float chan, float voicetype)
1213 {
1214         GlobalSound(self.samplefield, chan, voicetype);
1215 }
1216
1217 void VoiceMessage(string type, string msg)
1218 {
1219         var .string sample;
1220         float voicetype, ownteam;
1221         float flood;
1222         sample = GetVoiceMessageSampleField(type);
1223
1224         if(GetPlayerSoundSampleField_notFound)
1225         {
1226                 sprint(self, strcat("Invalid voice. Use one of: ", allvoicesamples, "\n"));
1227                 return;
1228         }
1229
1230         voicetype = GetVoiceMessageVoiceType(type);
1231         ownteam = (voicetype == VOICETYPE_TEAMRADIO);
1232
1233         flood = Say(self, ownteam, world, msg, 1);
1234
1235         if(IS_SPEC(self) || IS_OBSERVER(self) || flood < 0)
1236                 FakeGlobalSound(self.sample, CH_VOICE, voicetype);
1237         else if (flood > 0)
1238                 GlobalSound(self.sample, CH_VOICE, voicetype);
1239 }
1240
1241 void MoveToTeam(entity client, float team_colour, float type)
1242 {
1243         float lockteams_backup;
1244
1245         lockteams_backup = lockteams;  // backup any team lock
1246
1247         lockteams = 0;  // disable locked teams
1248
1249         TeamchangeFrags(client);  // move the players frags
1250         SetPlayerColors(client, team_colour - 1);  // set the players colour
1251         Damage(client, client, client, 100000, DEATH_AUTOTEAMCHANGE, client.origin, '0 0 0');  // kill the player
1252
1253         lockteams = lockteams_backup;  // restore the team lock
1254
1255         LogTeamchange(client.playerid, client.team, type);
1256 }