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