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