]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_player.qc
a reset facility for weapon properties... should help keep code clean
[divverent/nexuiz.git] / data / qcsrc / server / cl_player.qc
1 float weaponstats_buffer;
2
3 void WeaponStats_Init()
4 {
5         if(cvar_string("sv_weaponstats_killfile") != "" || cvar_string("sv_weaponstats_damagefile") != "")
6                 weaponstats_buffer = buf_create();
7         else
8                 weaponstats_buffer = -1;
9 }
10
11 #define WEAPONSTATS_GETINDEX(awep,vwep) ((vwep) + (awep) * (WEP_LAST - WEP_FIRST + 1) - (WEP_FIRST + WEP_FIRST * (WEP_LAST - WEP_FIRST + 1)))
12
13 void WeaponStats_Shutdown()
14 {
15         float i, j, idx, f;
16         float fh;
17         string prefix;
18         if(weaponstats_buffer < 0)
19                 return;
20         prefix = strcat(cvar_string("hostname"), "\t", GetGametype(), "_", GetMapname(), "\t");
21         if(cvar_string("sv_weaponstats_killfile") != "")
22         {
23                 fh = fopen(cvar_string("sv_weaponstats_killfile"), FILE_APPEND);
24                 if(fh >= 0)
25                 {
26                         fputs(fh, "#begin killfile\n");
27                         fputs(fh, strcat("#date ", strftime(TRUE, "%a %b %e %H:%M:%S %Z %Y"), "\n"));
28                         fputs(fh, strcat("#config ", ftos(crc16(FALSE, cvar_changes)), "\n"));
29                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
30                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
31                                 {
32                                         idx = WEAPONSTATS_GETINDEX(i, j);
33                                         f = stov(bufstr_get(weaponstats_buffer, idx)) * '0 1 0';
34                                         if(f != 0)
35                                                 fputs(fh, strcat(prefix, ftos(i), "\t", ftos(j), "\t", ftos(f), "\n"));
36                                 }
37                         fputs(fh, "#end\n\n");
38                         fclose(fh);
39                         print("Weapon kill stats written\n");
40                 }
41         }
42         if(cvar_string("sv_weaponstats_damagefile") != "")
43         {
44                 fh = fopen(cvar_string("sv_weaponstats_damagefile"), FILE_APPEND);
45                 if(fh >= 0)
46                 {
47                         fputs(fh, "#begin damagefile\n");
48                         fputs(fh, strcat("#date ", strftime(TRUE, "%a %b %e %H:%M:%S %Z %Y"), "\n"));
49                         fputs(fh, strcat("#config ", ftos(crc16(FALSE, cvar_changes)), "\n"));
50                         for(i = WEP_FIRST; i <= WEP_LAST; ++i)
51                                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
52                                 {
53                                         idx = WEAPONSTATS_GETINDEX(i, j);
54                                         f = stov(bufstr_get(weaponstats_buffer, idx)) * '1 0 0';
55                                         if(f != 0)
56                                                 fputs(fh, strcat(prefix, ftos(i), "\t", ftos(j), "\t", ftos(f), "\n"));
57                                 }
58                         fputs(fh, "#end\n\n");
59                         fclose(fh);
60                         print("Weapon damage stats written\n");
61                 }
62         }
63         buf_del(weaponstats_buffer);
64         weaponstats_buffer = -1;
65 }
66
67 void WeaponStats_LogItem(float awep, float vwep, vector item)
68 {
69         float idx;
70         if(weaponstats_buffer < 0)
71                 return;
72         if(awep < WEP_FIRST || vwep < WEP_FIRST)
73                 return;
74         if(awep > WEP_LAST || vwep > WEP_LAST)
75                 return;
76         idx = WEAPONSTATS_GETINDEX(awep,vwep);
77         bufstr_set(weaponstats_buffer, idx, vtos(stov(bufstr_get(weaponstats_buffer, idx)) + item));
78 }
79 void WeaponStats_LogDamage(float awep, float vwep, float damage)
80 {
81         if(damage < 0)
82                 error("negative damage?");
83         WeaponStats_LogItem(awep, vwep, '1 0 0' * damage);
84 }
85 void WeaponStats_LogKill(float awep, float vwep)
86 {
87         WeaponStats_LogItem(awep, vwep, '0 1 0');
88 }
89
90 // changes by LordHavoc on 03/29/04 and 03/30/04 at Vermeulen's request
91 // merged player_run and player_stand to player_anim
92 // added death animations to player_anim
93 // can now spawn thrown weapons from anywhere, not just from players
94 // thrown weapons now fade out after 20 seconds
95 // created PlayerGib function
96 // PlayerDie no longer uses hitloc or damage
97 // PlayerDie now supports dying animations as well as gibbing
98 // cleaned up PlayerDie a lot
99 // added CopyBody
100
101 .entity pusher;
102 .float pushltime;
103
104 void CopyBody(float keepvelocity)
105 {
106         local entity oldself;
107         if (self.effects & EF_NODRAW)
108                 return;
109         oldself = self;
110         self = spawn();
111         self.enemy = oldself;
112         self.lip = oldself.lip;
113         self.colormap = oldself.colormap;
114         self.iscreature = oldself.iscreature;
115         self.angles = oldself.angles;
116         self.avelocity = oldself.avelocity;
117         self.classname = "body";
118         self.damageforcescale = oldself.damageforcescale;
119         self.effects = oldself.effects;
120         self.event_damage = oldself.event_damage;
121         self.animstate_startframe = oldself.animstate_startframe;
122         self.animstate_numframes = oldself.animstate_numframes;
123         self.animstate_framerate = oldself.animstate_framerate;
124         self.animstate_starttime = oldself.animstate_starttime;
125         self.animstate_endtime = oldself.animstate_endtime;
126         self.animstate_override = oldself.animstate_override;
127         self.animstate_looping = oldself.animstate_looping;
128         self.frame = oldself.frame;
129         self.dead_frame = oldself.dead_frame;
130         self.pain_finished = oldself.pain_finished;
131         self.health = oldself.health;
132         self.armorvalue = oldself.armorvalue;
133         self.armortype = oldself.armortype;
134         self.model = oldself.model;
135         self.modelindex = oldself.modelindex;
136         self.modelindex_lod0 = oldself.modelindex_lod0;
137         self.modelindex_lod0_from_nexuiz = oldself.modelindex_lod0_from_nexuiz;
138         self.modelindex_lod1 = oldself.modelindex_lod1;
139         self.modelindex_lod2 = oldself.modelindex_lod2;
140         self.skinindex = oldself.skinindex;
141         self.species = oldself.species;
142         self.movetype = oldself.movetype;
143         self.nextthink = oldself.nextthink;
144         self.solid = oldself.solid;
145         self.takedamage = oldself.takedamage;
146         self.think = oldself.think;
147         self.customizeentityforclient = oldself.customizeentityforclient;
148         self.uncustomizeentityforclient = oldself.uncustomizeentityforclient;
149         self.uncustomizeentityforclient_set = oldself.uncustomizeentityforclient_set;
150         if (keepvelocity == 1)
151                 self.velocity = oldself.velocity;
152         self.oldvelocity = self.velocity;
153         self.fade_time = oldself.fade_time;
154         self.fade_rate = oldself.fade_rate;
155         //self.weapon = oldself.weapon;
156         setorigin(self, oldself.origin);
157         setsize(self, oldself.mins, oldself.maxs);
158         self.prevorigin = oldself.origin;
159         self.reset = SUB_Remove;
160
161         Drag_MoveDrag(oldself, self);
162
163         self = oldself;
164 }
165
166 float player_getspecies()
167 {
168         local float glob, i, j, fh, len, s, sk;
169         local string fn, l;
170
171         s = -1;
172
173         glob = search_begin("models/player/*.txt", TRUE, TRUE);
174         if(glob < 0)
175                 return s;
176         for(j = 0; j <= 1; ++j)
177         {
178                 for(i = 0; i < search_getsize(glob); ++i)
179                 {
180                         fn = search_getfilename(glob, i);
181                         fh = fopen(fn, FILE_READ);
182                         if(fh < 0)
183                                 continue;
184                         fgets(fh); fgets(fh);
185                         sk = stof(fgets(fh));
186                         if(sk == (j ? 0 : self.skinindex)) // 2nd pass skips the skin test
187                         if(fgets(fh) == self.model)
188                         {
189                                 l = fgets(fh);
190                                 len = tokenize_console(l);
191                                 if (len != 2)
192                                         goto nospecies;
193                                 if (argv(0) != "species")
194                                         goto nospecies;
195                                 switch(argv(1))
196                                 {
197                                         case "human":       s = SPECIES_HUMAN;       break;
198                                         case "alien":       s = SPECIES_ALIEN;       break;
199                                         case "robot_shiny": s = SPECIES_ROBOT_SHINY; break;
200                                         case "robot_rusty": s = SPECIES_ROBOT_RUSTY; break;
201                                         case "robot_solid": s = SPECIES_ROBOT_SOLID; break;
202                                         case "animal":      s = SPECIES_ANIMAL;      break;
203                                         case "reserved":    s = SPECIES_RESERVED;    break;
204                                 }
205                         }
206 :nospecies
207                         fclose(fh);
208                 }
209                 if (s >= 0)
210                         break;
211         }
212         search_end(glob);
213
214         if (s < 0)
215                 s = SPECIES_HUMAN;
216
217         return s;
218 }
219
220 void player_setupanimsformodel()
221 {
222         local string animfilename;
223         local float animfile;
224         // defaults for legacy .zym models without animinfo files
225         self.anim_die1 = '0 1 0.5'; // 2 seconds
226         self.anim_die2 = '1 1 0.5'; // 2 seconds
227         self.anim_draw = '2 1 3'; // TODO: analyze models and set framerate
228         self.anim_duck = '3 1 100'; // this anim seems bogus in most models, so make it play VERY briefly!
229         self.anim_duckwalk = '4 1 1';
230         self.anim_duckjump = '5 1 100'; // zym anims keep playing until changed, so this only has to start the anim, landing will end it
231         self.anim_duckidle = '6 1 1';
232         self.anim_idle = '7 1 1';
233         self.anim_jump = '8 1 100'; // zym anims keep playing until changed, so this only has to start the anim, landing will end it
234         self.anim_pain1 = '9 1 2'; // 0.5 seconds
235         self.anim_pain2 = '10 1 2'; // 0.5 seconds
236         self.anim_shoot = '11 1 5'; // TODO: analyze models and set framerate
237         self.anim_taunt = '12 1 0.33'; // FIXME?  there is no code using this anim
238         self.anim_run = '13 1 1';
239         self.anim_runbackwards = '14 1 1';
240         self.anim_strafeleft = '15 1 1';
241         self.anim_straferight = '16 1 1';
242         self.anim_dead1 = '17 1 1';
243         self.anim_dead2 = '18 1 1';
244         self.anim_forwardright = '19 1 1';
245         self.anim_forwardleft = '20 1 1';
246         self.anim_backright = '21 1 1';
247         self.anim_backleft  = '22 1 1';
248         animparseerror = FALSE;
249         animfilename = strcat(self.model, ".animinfo");
250         animfile = fopen(animfilename, FILE_READ);
251         if (animfile >= 0)
252         {
253                 self.anim_die1         = animparseline(animfile);
254                 self.anim_die2         = animparseline(animfile);
255                 self.anim_draw         = animparseline(animfile);
256                 self.anim_duck         = animparseline(animfile);
257                 self.anim_duckwalk     = animparseline(animfile);
258                 self.anim_duckjump     = animparseline(animfile);
259                 self.anim_duckidle     = animparseline(animfile);
260                 self.anim_idle         = animparseline(animfile);
261                 self.anim_jump         = animparseline(animfile);
262                 self.anim_pain1        = animparseline(animfile);
263                 self.anim_pain2        = animparseline(animfile);
264                 self.anim_shoot        = animparseline(animfile);
265                 self.anim_taunt        = animparseline(animfile);
266                 self.anim_run          = animparseline(animfile);
267                 self.anim_runbackwards = animparseline(animfile);
268                 self.anim_strafeleft   = animparseline(animfile);
269                 self.anim_straferight  = animparseline(animfile);
270                 self.anim_forwardright = animparseline(animfile);
271                 self.anim_forwardleft  = animparseline(animfile);
272                 self.anim_backright    = animparseline(animfile);
273                 self.anim_backleft     = animparseline(animfile);
274                 fclose(animfile);
275
276                 // derived anims
277                 self.anim_dead1 = '0 1 1' + '1 0 0' * (self.anim_die1_x + self.anim_die1_y - 1);
278                 self.anim_dead2 = '0 1 1' + '1 0 0' * (self.anim_die2_x + self.anim_die2_y - 1);
279
280                 if (animparseerror)
281                         print("Parse error in ", animfilename, ", some player animations are broken\n");
282         }
283         else
284                 dprint("File ", animfilename, " not found, assuming legacy .zym model animation timings\n");
285         // reset animstate now
286         setanim(self, self.anim_idle, TRUE, FALSE, TRUE);
287 };
288
289 void player_anim (void)
290 {
291         updateanim(self);
292         if (self.weaponentity)
293                 updateanim(self.weaponentity);
294
295         if (self.deadflag != DEAD_NO)
296         {
297                 if (time > self.animstate_endtime)
298                 {
299                         if (self.maxs_z > 5)
300                         {
301                                 self.maxs_z = 5;
302                                 setsize(self, self.mins, self.maxs);
303                         }
304                         self.frame = self.dead_frame;
305                 }
306                 return;
307         }
308
309         if (!self.animstate_override)
310         {
311                 if (!(self.flags & FL_ONGROUND))
312                 {
313                         if (self.crouch)
314                                 setanim(self, self.anim_duckjump, FALSE, TRUE, self.restart_jump);
315                         else
316                                 setanim(self, self.anim_jump, FALSE, TRUE, self.restart_jump);
317                         self.restart_jump = FALSE;
318                 }
319                 else if (self.crouch)
320                 {
321                         if (self.movement_x * self.movement_x + self.movement_y * self.movement_y > 20)
322                                 setanim(self, self.anim_duckwalk, TRUE, FALSE, FALSE);
323                         else
324                                 setanim(self, self.anim_duckidle, TRUE, FALSE, FALSE);
325                 }
326                 else if ((self.movement_x * self.movement_x + self.movement_y * self.movement_y) > 20)
327                 {
328                         if (self.movement_x > 0 && self.movement_y == 0)
329                                 setanim(self, self.anim_run, TRUE, FALSE, FALSE);
330                         else if (self.movement_x < 0 && self.movement_y == 0)
331                                 setanim(self, self.anim_runbackwards, TRUE, FALSE, FALSE);
332                         else if (self.movement_x == 0 && self.movement_y > 0)
333                                 setanim(self, self.anim_straferight, TRUE, FALSE, FALSE);
334                         else if (self.movement_x == 0 && self.movement_y < 0)
335                                 setanim(self, self.anim_strafeleft, TRUE, FALSE, FALSE);
336                         else if (self.movement_x > 0 && self.movement_y > 0)
337                                 setanim(self, self.anim_forwardright, TRUE, FALSE, FALSE);
338                         else if (self.movement_x > 0 && self.movement_y < 0)
339                                 setanim(self, self.anim_forwardleft, TRUE, FALSE, FALSE);
340                         else if (self.movement_x < 0 && self.movement_y > 0)
341                                 setanim(self, self.anim_backright, TRUE, FALSE, FALSE);
342                         else if (self.movement_x < 0 && self.movement_y < 0)
343                                 setanim(self, self.anim_backleft, TRUE, FALSE, FALSE);
344                         else
345                                 setanim(self, self.anim_run, TRUE, FALSE, FALSE);
346                 }
347                 else
348                         setanim(self, self.anim_idle, TRUE, FALSE, FALSE);
349         }
350
351         if (self.weaponentity)
352         if (!self.weaponentity.animstate_override)
353                 setanim(self.weaponentity, self.weaponentity.anim_idle, TRUE, FALSE, FALSE);
354 }
355
356 void SpawnThrownWeapon (vector org, float w)
357 {
358         if(g_minstagib)
359         if(self.ammo_cells <= 0)
360                 return;
361
362         if(g_pinata)
363         {
364                 float j;
365                 for(j = WEP_FIRST; j <= WEP_LAST; ++j)
366                 {
367                         if(self.weapons & W_WeaponBit(j))
368                                 if(W_IsWeaponThrowable(j))
369                                         W_ThrowNewWeapon(self, j, FALSE, self.origin, randomvec() * 175 + '0 0 325');
370                 }
371         }
372         else
373                 W_ThrowWeapon(randomvec() * 125 + '0 0 200', org - self.origin, FALSE);
374 }
375
376 void PlayerCorpseDamage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
377 {
378         local float take, save;
379         vector v;
380         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
381
382         // damage resistance (ignore most of the damage from a bullet or similar)
383         damage = max(damage - 5, 1);
384
385         v = healtharmor_applydamage(self.armorvalue, cvar("g_balance_armor_blockpercent"), damage);
386         take = v_x;
387         save = v_y;
388
389         if(sound_allowed(MSG_BROADCAST, attacker))
390         {
391                 if (save > 10)
392                         sound (self, CHAN_PROJECTILE, "misc/armorimpact.wav", VOL_BASE, ATTN_NORM);
393                 else if (take > 30)
394                         sound (self, CHAN_PROJECTILE, "misc/bodyimpact2.wav", VOL_BASE, ATTN_NORM);
395                 else if (take > 10)
396                         sound (self, CHAN_PROJECTILE, "misc/bodyimpact1.wav", VOL_BASE, ATTN_NORM);
397         }
398
399         if (take > 50)
400                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
401         if (take > 100)
402                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
403
404         if (!(self.flags & FL_GODMODE))
405         {
406                 self.armorvalue = self.armorvalue - save;
407                 self.health = self.health - take;
408                 // pause regeneration for 5 seconds
409                 self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_health_regen"));
410         }
411         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
412         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
413         self.dmg_inflictor = inflictor;
414
415         if (self.health <= -75 && self.modelindex != 0)
416         {
417                 // don't use any animations as a gib
418                 self.frame = 0;
419                 self.dead_frame = 0;
420                 // view just above the floor
421                 self.view_ofs = '0 0 4';
422
423                 Violence_GibSplash(self, 1, 1, attacker);
424                 self.modelindex = 0; // restore later
425                 self.solid = SOLID_NOT; // restore later
426         }
427 }
428
429 void ClientKill_Now_TeamChange();
430
431 void PlayerDamage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
432 {
433         local float take, save, waves, sdelay, dh, da, j;
434         vector v;
435         float valid_damage_for_weaponstats;
436
437         dh = max(self.health, 0);
438         da = max(self.armorvalue, 0);
439
440         if(!DEATH_ISSPECIAL(deathtype))
441         {
442                 damage *= sqrt(bound(1.0, self.cvar_cl_handicap, 100.0));
443                 if(self != attacker)
444                         damage /= sqrt(bound(1.0, attacker.cvar_cl_handicap, 100.0));
445         }
446
447         if(DEATH_ISWEAPON(deathtype, WEP_TUBA))
448         {
449                 // tuba causes blood to come out of the ears
450                 vector ear1, ear2;
451                 vector d;
452                 float f;
453                 ear1 = self.origin;
454                 ear1_z += 0.125 * self.view_ofs_z + 0.875 * self.maxs_z; // 7/8
455                 ear2 = ear1;
456                 makevectors(self.angles);
457                 ear1 += v_right * -10;
458                 ear2 += v_right * +10;
459                 d = inflictor.origin - self.origin;
460                 f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
461                 force = v_right * vlen(force);
462                 Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), self, attacker);
463                 Violence_GibSplash_At(ear2, force,      2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), self, attacker);
464                 if(f > 0)
465                 {
466                         hitloc = ear1;
467                         force = force * -1;
468                 }
469                 else
470                 {
471                         hitloc = ear2;
472                         // force is already good
473                 }
474         }
475         else
476                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
477
478         if(g_arena)
479         if(numspawned < 2)
480                 return;
481
482         if (!g_minstagib)
483         {
484                 v = healtharmor_applydamage(self.armorvalue, cvar("g_balance_armor_blockpercent"), damage);
485                 take = v_x;
486                 save = v_y;
487         }
488         else
489         {
490                 save = 0;
491                 take = damage;
492         }
493
494         if(sound_allowed(MSG_BROADCAST, attacker))
495         {
496                 if (save > 10)
497                         sound (self, CHAN_PROJECTILE, "misc/armorimpact.wav", VOL_BASE, ATTN_NORM);
498                 else if (take > 30)
499                         sound (self, CHAN_PROJECTILE, "misc/bodyimpact2.wav", VOL_BASE, ATTN_NORM);
500                 else if (take > 10)
501                         sound (self, CHAN_PROJECTILE, "misc/bodyimpact1.wav", VOL_BASE, ATTN_NORM); // FIXME possibly remove them?
502         }
503
504         if (take > 50)
505                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
506         if (take > 100)
507                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
508
509         if (time > self.spawnshieldtime)
510         {
511                 if (!(self.flags & FL_GODMODE))
512                 {
513                         self.armorvalue = self.armorvalue - save;
514                         self.health = self.health - take;
515                         // pause regeneration for 5 seconds
516                         self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_health_regen"));
517
518                         if (time > self.pain_finished)          //Don't switch pain sequences like crazy
519                         {
520                                 self.pain_finished = time + 0.5;        //Supajoe
521
522                                 if(sv_gentle < 1) {
523                                         if(self.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
524                                         {
525                                                 if (random() > 0.5)
526                                                         setanim(self, self.anim_pain1, FALSE, TRUE, TRUE);
527                                                 else
528                                                         setanim(self, self.anim_pain2, FALSE, TRUE, TRUE);
529                                         }
530
531                                         if(sound_allowed(MSG_BROADCAST, attacker))
532                                         if(!DEATH_ISWEAPON(deathtype, WEP_LASER) || attacker != self || self.health < 2 * cvar("g_balance_laser_primary_damage") * cvar("g_balance_selfdamagepercent") + 1)
533                                         // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
534                                         {
535                                                 if(self.health > 75) // TODO make a "gentle" version?
536                                                         PlayerSound(playersound_pain100, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
537                                                 else if(self.health > 50)
538                                                         PlayerSound(playersound_pain75, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
539                                                 else if(self.health > 25)
540                                                         PlayerSound(playersound_pain50, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
541                                                 else if(self.health > 1)
542                                                         PlayerSound(playersound_pain25, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
543                                         }
544                                 }
545
546                                 // throw off bot aim temporarily
547                                 local float shake;
548                                 shake = damage * 5 / (bound(0,skill,100) + 1);
549                                 self.v_angle_x = self.v_angle_x + (random() * 2 - 1) * shake;
550                                 self.v_angle_y = self.v_angle_y + (random() * 2 - 1) * shake;
551                         }
552                 }
553                 else
554                         self.max_armorvalue += (save + take);
555         }
556         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
557         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
558         self.dmg_inflictor = inflictor;
559
560         if(attacker == self)
561         {
562                 // don't reset pushltime for self damage as it may be an attempt to
563                 // escape a lava pit or similar
564                 //self.pushltime = 0;
565         }
566         else if(attacker.classname == "player" || attacker.classname == "gib")
567         {
568                 self.pusher = attacker;
569                 self.pushltime = time + cvar("g_maxpushtime");
570         }
571         else if(time < self.pushltime)
572         {
573                 attacker = self.pusher;
574                 self.pushltime = max(self.pushltime, time + 0.6);
575         }
576         else
577                 self.pushltime = 0;
578
579         valid_damage_for_weaponstats = 0;
580         if(clienttype(self) == CLIENTTYPE_REAL)
581         if(clienttype(attacker) == CLIENTTYPE_REAL)
582         if(self != attacker)
583         if(!DEATH_ISSPECIAL(deathtype))
584         if(IsDifferentTeam(self, attacker))
585                 valid_damage_for_weaponstats = 1;
586         
587         if(valid_damage_for_weaponstats)
588         {
589                 dh = dh - max(self.health, 0);
590                 da = da - max(self.armorvalue, 0);
591                 WeaponStats_LogDamage(DEATH_WEAPONOF(deathtype), self.weapon, dh + da);
592         }
593
594         if (self.health < 1)
595         {
596                 float defer_ClientKill_Now_TeamChange;
597                 defer_ClientKill_Now_TeamChange = FALSE;
598
599                 if(valid_damage_for_weaponstats)
600                         WeaponStats_LogKill(DEATH_WEAPONOF(deathtype), self.weapon);
601
602                 if(sv_gentle < 1) // TODO make a "gentle" version?
603                 if(sound_allowed(MSG_BROADCAST, attacker))
604                 {
605                         if(deathtype == DEATH_DROWN)
606                                 PlayerSound(playersound_drown, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
607                         else
608                                 PlayerSound(playersound_death, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
609                 }
610
611                 // get rid of kill indicator
612                 if(self.killindicator)
613                 {
614                         remove(self.killindicator);
615                         self.killindicator = world;
616                         if(self.killindicator_teamchange)
617                                 defer_ClientKill_Now_TeamChange = TRUE;
618
619                         if(self.classname == "body")
620                         if(deathtype == DEATH_KILL)
621                         {
622                                 // for the lemmings fans, a small harmless explosion
623                                 pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
624                         }
625                 }
626
627                 // become fully visible
628                 self.alpha = 1;
629                 // clear selected player display
630                 ClearSelectedPlayer();
631                 // throw a weapon
632                 SpawnThrownWeapon (self.origin + (self.mins + self.maxs) * 0.5, self.switchweapon);
633                 // print an obituary message
634                 Obituary (attacker, inflictor, self, deathtype);
635                 race_PreDie();
636                 DropAllRunes(self);
637                 if(self == attacker)
638                         kh_Key_DropAll(self, TRUE);
639                 else if(attacker.classname == "player" || attacker.classname == "gib")
640                         kh_Key_DropAll(self, FALSE);
641                 else
642                         kh_Key_DropAll(self, TRUE);
643                 if(self.flagcarried)
644                 {
645                         if(attacker.classname != "player" && attacker.classname != "gib")
646                                 DropFlag(self.flagcarried, self, attacker); // penalty for flag loss by suicide
647                         else if(attacker.team == self.team)
648                                 DropFlag(self.flagcarried, attacker, attacker); // penalty for flag loss by suicide/teamkill
649                         else
650                                 DropFlag(self.flagcarried, world, attacker);
651                 }
652                 if(self.ballcarried)
653                         DropBall(self.ballcarried, self.origin, self.velocity);
654                 Portal_ClearAllLater(self);
655                 // clear waypoints
656                 WaypointSprite_PlayerDead();
657                 // make the corpse upright (not tilted)
658                 self.angles_x = 0;
659                 self.angles_z = 0;
660                 // don't spin
661                 self.avelocity = '0 0 0';
662                 // view from the floor
663                 self.view_ofs = '0 0 -8';
664                 // toss the corpse
665                 self.movetype = MOVETYPE_TOSS;
666                 // shootable corpse
667                 self.solid = SOLID_CORPSE;
668                 // don't stick to the floor
669                 self.flags &~= FL_ONGROUND;
670                 // dying animation
671                 self.deadflag = DEAD_DYING;
672                 // when to allow respawn
673                 sdelay = 0;
674                 waves = 0;
675                 if(cvar("g_respawn_mapsettings"))
676                 {
677                         sdelay = cvar("g_respawn_mapsettings_delay");
678                         waves = cvar("g_respawn_mapsettings_waves");
679                 }
680                 if(!sdelay)
681                         sdelay = cvar(strcat("g_", GetGametype(), "_respawn_delay"));
682                 if(!sdelay)
683                         sdelay = cvar("g_respawn_delay");
684                 if(!waves)
685                         waves = cvar(strcat("g_", GetGametype(), "_respawn_waves"));
686                 if(!waves)
687                         waves = cvar("g_respawn_waves");
688                 if(waves)
689                         self.death_time = ceil((time + sdelay) / waves) * waves;
690                 else
691                         self.death_time = time + sdelay;
692                 if((sdelay + waves >= 5.0) && (self.death_time - time > 1.75))
693                         self.respawn_countdown = 10; // first number to count down from is 10
694                 else
695                         self.respawn_countdown = -1; // do not count down
696                 if (random() < 0.5)
697                 {
698                         setanim(self, self.anim_die1, FALSE, TRUE, TRUE);
699                         self.dead_frame = self.anim_dead1_x;
700                 }
701                 else
702                 {
703                         setanim(self, self.anim_die2, FALSE, TRUE, TRUE);
704                         self.dead_frame = self.anim_dead2_x;
705                 }
706                 // set damage function to corpse damage
707                 self.event_damage = PlayerCorpseDamage;
708                 // call the corpse damage function just in case it wants to gib
709                 self.event_damage(inflictor, attacker, 0, deathtype, hitloc, force);
710                 // set up to fade out later
711                 SUB_SetFade (self, time + 12 + random () * 4, 1);
712
713                 // remove laserdot
714                 if(self.weaponentity)
715                         if(self.weaponentity.lasertarget)
716                                 remove(self.weaponentity.lasertarget);
717
718                 if(clienttype(self) == CLIENTTYPE_REAL)
719                 {
720                         self.fixangle = TRUE;
721                         //msg_entity = self;
722                         //WriteByte (MSG_ONE, SVC_SETANGLE);
723                         //WriteAngle (MSG_ONE, self.v_angle_x);
724                         //WriteAngle (MSG_ONE, self.v_angle_y);
725                         //WriteAngle (MSG_ONE, 80);
726                 }
727
728                 if(g_arena)
729                         Spawnqueue_Unmark(self);
730
731                 if(defer_ClientKill_Now_TeamChange)
732                         ClientKill_Now_TeamChange();
733
734                 if(sv_gentle > 0) {
735                         // remove corpse
736                         PlayerCorpseDamage (inflictor, attacker, 100.0, deathtype, hitloc, force);
737                 }
738
739                 // reset fields the weapons may use just in case
740         for (j = WEP_FIRST; j <= WEP_LAST; ++j)
741             weapon_action(j, WR_RESETPLAYER);
742         }
743 }
744
745 float UpdateSelectedPlayer_countvalue(float v)
746 {
747         return max(0, (v - 1.0) / 0.5);
748 }
749
750 // returns: -2 if no hit, otherwise cos of the angle
751 // uses the global v_angle
752 float UpdateSelectedPlayer_canSee(entity p, float mincosangle, float maxdist)
753 {
754         vector so, d;
755         float c;
756
757         if(p == self)
758                 return -2;
759
760         if(p.deadflag)
761                 return -2;
762
763         so = self.origin + self.view_ofs;
764         d = p.origin - so;
765
766         // misaimed?
767         if(dist_point_line(d, '0 0 0', v_forward) > maxdist)
768                 return -2;
769
770         // now find the cos of the angle...
771         c = normalize(d) * v_forward;
772
773         if(c <= mincosangle)
774                 return -2;
775
776         // not visible in any way? forget it
777         if(!checkpvs(so, p))
778                 return -2;
779
780         traceline(so, p.origin, MOVE_NOMONSTERS, self);
781         if(trace_fraction < 1)
782                 return -2;
783
784         return c;
785 }
786
787 void ClearSelectedPlayer()
788 {
789         if(self.selected_player)
790         {
791                 centerprint_expire(self, CENTERPRIO_POINT);
792                 self.selected_player = world;
793                 self.selected_player_display_needs_update = FALSE;
794         }
795 }
796
797 void UpdateSelectedPlayer()
798 {
799         entity selected;
800         float selected_score;
801         selected = world;
802         selected_score = 0.95; // 18 degrees
803
804         if(!cvar("sv_allow_shownames"))
805                 return;
806
807         if(clienttype(self) != CLIENTTYPE_REAL)
808                 return;
809
810         if(self.cvar_cl_shownames == 0)
811                 return;
812
813         if(self.cvar_cl_shownames == 1 && !teams_matter)
814                 return;
815
816         makevectors(self.v_angle); // sets v_forward
817
818         // 1. cursor trace is always right
819         if(self.cursor_trace_ent && self.cursor_trace_ent.classname == "player" && !self.cursor_trace_ent.deadflag)
820         {
821                 selected = self.cursor_trace_ent;
822         }
823         else
824         {
825                 // 2. if we don't have a cursor trace, find the player which is least
826                 //    mis-aimed at
827                 entity p;
828                 FOR_EACH_PLAYER(p)
829                 {
830                         float c;
831                         c = UpdateSelectedPlayer_canSee(p, selected_score, 100); // 100 = 2.5 meters
832                         if(c >= -1)
833                         {
834                                 selected = p;
835                                 selected_score = c;
836                         }
837                 }
838         }
839
840         if(selected)
841         {
842                 self.selected_player_display_timeout = time + self.cvar_scr_centertime;
843         }
844         else
845         {
846                 if(time < self.selected_player_display_timeout)
847                         if(UpdateSelectedPlayer_canSee(self.selected_player, 0.7, 200) >= -1) // 5 meters, 45 degrees
848                                 selected = self.selected_player;
849         }
850
851         if(selected)
852         {
853                 if(selected == self.selected_player)
854                 {
855                         float save;
856                         save = UpdateSelectedPlayer_countvalue(self.selected_player_count);
857                         self.selected_player_count = self.selected_player_count + frametime;
858                         if(save != UpdateSelectedPlayer_countvalue(self.selected_player_count))
859                         {
860                                 string namestr, healthstr;
861                                 namestr = playername(selected);
862                                 if(teams_matter)
863                                 {
864                                         healthstr = ftos(floor(selected.health));
865                                         if(self.team == selected.team)
866                                         {
867                                                 namestr = strcat(namestr, " (", healthstr, "%)");
868                                                 self.selected_player_display_needs_update = TRUE;
869                                         }
870                                 }
871                                 centerprint_atprio(self, CENTERPRIO_POINT, namestr);
872                         }
873                 }
874                 else
875                 {
876                         ClearSelectedPlayer();
877                         self.selected_player = selected;
878                         self.selected_player_time = time;
879                         self.selected_player_count = 0;
880                         self.selected_player_display_needs_update = FALSE;
881                 }
882         }
883         else
884         {
885                 ClearSelectedPlayer();
886         }
887
888         if(self.selected_player)
889                 self.last_selected_player = self.selected_player;
890 }
891
892 .float muted; // to be used by prvm_edictset server playernumber muted 1
893 void Say(entity source, float teamsay, entity privatesay, string msgin, float floodcontrol)
894 {
895         string msgstr, colorstr, cmsgstr, namestr, fullmsgstr, sourcemsgstr, fullcmsgstr, sourcecmsgstr, privatemsgprefix;
896         float flood, privatemsgprefixlen;
897         entity head;
898
899         if(Ban_MaybeEnforceBan(source))
900                 return;
901
902         if(!teamsay && !privatesay)
903                 if(substring(msgin, 0, 1) == " ")
904                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
905
906         msgin = formatmessage(msgin);
907
908         if(msgin == "")
909                 return;
910
911         if(source.classname != "player")
912                 colorstr = "^0"; // black for spectators
913         else if(teams_matter)
914                 colorstr = Team_ColorCode(source.team);
915         else
916                 teamsay = FALSE;
917
918         if(intermission_running)
919                 teamsay = FALSE;
920
921         msgin = trigger_magicear_processmessage_forallears(source, teamsay, privatesay, msgin);
922         if(msgin == "")
923                 return;
924
925         /*
926          * using bprint solves this... me stupid
927         // how can we prevent the message from appearing in a listen server?
928         // for now, just give "say" back and only handle say_team
929         if(!teamsay)
930         {
931                 clientcommand(self, strcat("say ", msgin));
932                 return;
933         }
934         */
935
936         if(cvar("g_chat_teamcolors"))
937                 namestr = playername(source);
938         else
939                 namestr = source.netname;
940
941         if(privatesay)
942         {
943                 msgstr = strcat("\{1}\{13}* ^3", namestr, "^3 tells you: ^7");
944                 privatemsgprefixlen = strlen(msgstr);
945                 msgstr = strcat(msgstr, msgin);
946                 cmsgstr = strcat(colorstr, "^3", namestr, "^3 tells you:\n^7", msgin);
947                 if(cvar("g_chat_teamcolors"))
948                         privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", playername(privatesay), ": ^7");
949                 else
950                         privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", privatesay.netname, ": ^7");
951         }
952         else if(teamsay)
953         {
954                 msgstr = strcat("\{1}\{13}", colorstr, "(^3", namestr, colorstr, ") ^7", msgin);
955                 cmsgstr = strcat(colorstr, "(^3", namestr, colorstr, ")\n^7", msgin);
956         }
957         else
958         {
959                 msgstr = strcat("\{1}", namestr, "^7: ", msgin);
960                 cmsgstr = "";
961         }
962
963         msgstr = strcat(strreplace("\n", " ", msgstr), "\n"); // newlines only are good for centerprint
964         fullmsgstr = msgstr;
965         fullcmsgstr = cmsgstr;
966
967         // FLOOD CONTROL
968         flood = 0;
969         if(floodcontrol)
970         {
971                 float flood_spl;
972                 float flood_burst;
973                 float flood_lmax;
974                 var .float flood_field;
975                 float lines;
976                 if(privatesay)
977                 {
978                         flood_spl = cvar("g_chat_flood_spl_tell");
979                         flood_burst = cvar("g_chat_flood_burst_tell");
980                         flood_lmax = cvar("g_chat_flood_lmax_tell");
981                         flood_field = floodcontrol_chattell;
982                 }
983                 else if(teamsay)
984                 {
985                         flood_spl = cvar("g_chat_flood_spl_team");
986                         flood_burst = cvar("g_chat_flood_burst_team");
987                         flood_lmax = cvar("g_chat_flood_lmax_team");
988                         flood_field = floodcontrol_chatteam;
989                 }
990                 else
991                 {
992                         flood_spl = cvar("g_chat_flood_spl");
993                         flood_burst = cvar("g_chat_flood_burst");
994                         flood_lmax = cvar("g_chat_flood_lmax");
995                         flood_field = floodcontrol_chat;
996                 }
997                 flood_burst = max(0, flood_burst - 1);
998                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
999
1000                 // do flood control for the default line size
1001                 getWrappedLine_remaining = msgstr;
1002                 msgstr = "";
1003                 lines = 0;
1004                 while(getWrappedLine_remaining && (!flood_lmax || lines <= flood_lmax))
1005                 {
1006                         msgstr = strcat(msgstr, " ", getWrappedLine(82.4289758859709, strlennocol)); // perl averagewidth.pl < gfx/vera-sans.width
1007                         ++lines;
1008                 }
1009                 msgstr = substring(msgstr, 1, strlen(msgstr) - 1);
1010
1011                 if(getWrappedLine_remaining != "")
1012                 {
1013                         msgstr = strcat(msgstr, "\n");
1014                         flood = 2;
1015                 }
1016
1017                 if(time >= source.flood_field)
1018                 {
1019                         source.flood_field = max(time - flood_burst * flood_spl, source.flood_field) + lines * flood_spl;
1020                 }
1021                 else
1022                 {
1023                         flood = 1;
1024                         msgstr = fullmsgstr;
1025                 }
1026         }
1027
1028         if (timeoutStatus == 2) //when game is paused, no flood protection
1029                 source.flood_field = flood = 0;
1030
1031         if(flood == 2)
1032         {
1033                 if(cvar("g_chat_flood_notify_flooder"))
1034                 {
1035                         sourcemsgstr = strcat(msgstr, "\n^3FLOOD CONTROL: ^7message too long, trimmed\n");
1036                         sourcecmsgstr = "";
1037                 }
1038                 else
1039                 {
1040                         sourcemsgstr = fullmsgstr;
1041                         sourcecmsgstr = fullcmsgstr;
1042                 }
1043                 cmsgstr = "";
1044         }
1045         else
1046         {
1047                 sourcemsgstr = msgstr;
1048                 sourcecmsgstr = cmsgstr;
1049         }
1050
1051         if(!privatesay)
1052         if(source.classname != "player")
1053         {
1054                 if(teamsay || (cvar("g_chat_nospectators") == 1) || (cvar("g_chat_nospectators") == 2 && !inWarmupStage))
1055                         teamsay = -1; // spectators
1056         }
1057
1058         if(flood)
1059                 print("NOTE: ", playername(source), "^7 is flooding.\n");
1060
1061         // build sourcemsgstr by cutting off a prefix and replacing it by the other one
1062         if(privatesay)
1063                 sourcemsgstr = strcat(privatemsgprefix, substring(sourcemsgstr, privatemsgprefixlen, -1));
1064
1065         if(source.muted)
1066         {
1067                 // always fake the message
1068                 sprint(source, sourcemsgstr);
1069                 if(cmsgstr != "" && !privatesay)
1070                         centerprint(source, sourcecmsgstr);
1071         }
1072         else if(flood == 1)
1073         {
1074                 if(cvar("g_chat_flood_notify_flooder"))
1075                         sprint(source, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(source.flood_field - time), "^3 seconds\n"));
1076                 else
1077                 {
1078                         sprint(source, sourcemsgstr);
1079                         if(cmsgstr != "" && !privatesay)
1080                                 centerprint(source, sourcecmsgstr);
1081                 }
1082         }
1083         else if(privatesay)
1084         {
1085                 sprint(source, sourcemsgstr);
1086                 sprint(privatesay, msgstr);
1087                 if(cmsgstr != "")
1088                         centerprint(privatesay, cmsgstr);
1089         }
1090         else if(teamsay > 0)
1091         {
1092                 sprint(source, sourcemsgstr);
1093                 if(sourcecmsgstr != "")
1094                         centerprint(source, sourcecmsgstr);
1095                 FOR_EACH_REALPLAYER(head) if(head.team == source.team)
1096                         if(head != source)
1097                         {
1098                                 sprint(head, msgstr);
1099                                 if(cmsgstr != "")
1100                                         centerprint(head, cmsgstr);
1101                         }
1102         }
1103         else if(teamsay < 0)
1104         {
1105                 sprint(source, sourcemsgstr);
1106                 FOR_EACH_REALCLIENT(head) if(head.classname != "player")
1107                         if(head != source)
1108                                 sprint(head, msgstr);
1109         }
1110         else if(sourcemsgstr != msgstr)
1111         {
1112                 sprint(source, sourcemsgstr);
1113                 FOR_EACH_REALCLIENT(head)
1114                         if(head != source)
1115                                 sprint(head, msgstr);
1116         }
1117         else
1118                 bprint(msgstr);
1119 }
1120
1121 float GetVoiceMessageVoiceType(string type)
1122 {
1123         if(type == "taunt")
1124                 return VOICETYPE_TAUNT;
1125         if(type == "teamshoot")
1126                 return VOICETYPE_LASTATTACKER;
1127         return VOICETYPE_TEAMRADIO;
1128 }
1129
1130 string allvoicesamples;
1131 float GetPlayerSoundSampleField_notFound;
1132 float GetPlayerSoundSampleField_fixed;
1133 .string GetVoiceMessageSampleField(string type)
1134 {
1135         GetPlayerSoundSampleField_notFound = 0;
1136         GetPlayerSoundSampleField_fixed = 0;
1137         switch(type)
1138         {
1139 #define _VOICEMSG(m) case #m: return playersound_##m;
1140                 ALLVOICEMSGS
1141 #undef _VOICEMSG
1142         }
1143         GetPlayerSoundSampleField_notFound = 1;
1144         return playersound_taunt;
1145 }
1146
1147 .string GetPlayerSoundSampleField(string type)
1148 {
1149         GetPlayerSoundSampleField_notFound = 0;
1150         GetPlayerSoundSampleField_fixed = 0;
1151         switch(type)
1152         {
1153 #define _VOICEMSG(m) case #m: return playersound_##m;
1154                 ALLPLAYERSOUNDS
1155 #undef _VOICEMSG
1156         }
1157         GetPlayerSoundSampleField_notFound = 1;
1158         return playersound_taunt;
1159 }
1160
1161 void PrecacheGlobalSound(string samplestring)
1162 {
1163         float n, i;
1164         tokenize_console(samplestring);
1165         n = stof(argv(1));
1166         if(n > 0)
1167         {
1168                 for(i = 1; i <= n; ++i)
1169                         precache_sound(strcat(argv(0), ftos(i), ".wav"));
1170         }
1171         else
1172         {
1173                 precache_sound(strcat(argv(0), ".wav"));
1174         }
1175 }
1176
1177 void PrecachePlayerSounds(string f)
1178 {
1179         float fh;
1180         string s;
1181         fh = fopen(f, FILE_READ);
1182         if(fh < 0)
1183                 return;
1184         while((s = fgets(fh)))
1185         {
1186                 if(tokenize_console(s) != 3)
1187                 {
1188                         dprint("Invalid sound info line: ", s, "\n");
1189                         continue;
1190                 }
1191                 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
1192         }
1193         fclose(fh);
1194
1195         if not(allvoicesamples)
1196         {
1197 #define _VOICEMSG(m) allvoicesamples = strcat(allvoicesamples, " ", #m);
1198                 ALLVOICEMSGS
1199 #undef _VOICEMSG
1200                 allvoicesamples = strzone(substring(allvoicesamples, 1, strlen(allvoicesamples) - 1));
1201         }
1202 }
1203
1204 void ClearPlayerSounds()
1205 {
1206 #define _VOICEMSG(m) if(self.playersound_##m) { strunzone(self.playersound_##m); self.playersound_##m = string_null; }
1207         ALLPLAYERSOUNDS
1208         ALLVOICEMSGS
1209 #undef _VOICEMSG
1210 }
1211
1212 void LoadPlayerSounds(string f, float first)
1213 {
1214         float fh;
1215         string s;
1216         var .string field;
1217         fh = fopen(f, FILE_READ);
1218         if(fh < 0)
1219                 return;
1220         while((s = fgets(fh)))
1221         {
1222                 if(tokenize_console(s) != 3)
1223                         continue;
1224                 field = GetPlayerSoundSampleField(argv(0));
1225                 if(GetPlayerSoundSampleField_notFound)
1226                         field = GetVoiceMessageSampleField(argv(0));
1227                 if(GetPlayerSoundSampleField_notFound)
1228                         continue;
1229                 if(GetPlayerSoundSampleField_fixed)
1230                         if not(first)
1231                                 continue;
1232                 if(self.field)
1233                         strunzone(self.field);
1234                 self.field = strzone(strcat(argv(1), " ", argv(2)));
1235         }
1236         fclose(fh);
1237 }
1238
1239 .float modelindex_for_playersound;
1240 void UpdatePlayerSounds()
1241 {
1242         if(self.modelindex == self.modelindex_for_playersound)
1243                 return;
1244         self.modelindex_for_playersound = self.modelindex;
1245         ClearPlayerSounds();
1246         LoadPlayerSounds("sound/player/default.sounds", 1);
1247         LoadPlayerSounds(strcat(self.model, ".sounds"), 0);
1248 }
1249
1250 void GlobalSound(string sample, float chan, float voicetype)
1251 {
1252         float n;
1253         float tauntrand;
1254
1255         if(sample == "")
1256                 return;
1257
1258         tokenize_console(sample);
1259         n = stof(argv(1));
1260         if(n > 0)
1261                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
1262         else
1263                 sample = strcat(argv(0), ".wav"); // randomization
1264
1265         switch(voicetype)
1266         {
1267                 case VOICETYPE_LASTATTACKER_ONLY:
1268                         if(self.pusher)
1269                                 if(self.pusher.team == self.team)
1270                                 {
1271                                         msg_entity = self.pusher;
1272                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
1273                                         {
1274                                                 if(msg_entity.cvar_cl_voice_directional == 1)
1275                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
1276                                                 else
1277                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1278                                         }
1279                                 }
1280                         break;
1281                 case VOICETYPE_LASTATTACKER:
1282                         if(self.pusher)
1283                                 if(self.pusher.team == self.team)
1284                                 {
1285                                         msg_entity = self.pusher;
1286                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
1287                                         {
1288                                                 if(msg_entity.cvar_cl_voice_directional == 1)
1289                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
1290                                                 else
1291                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1292                                         }
1293                                         msg_entity = self;
1294                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
1295                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTN_NONE);
1296                                 }
1297                         break;
1298                 case VOICETYPE_TEAMRADIO:
1299                         FOR_EACH_REALCLIENT(msg_entity)
1300                                 if(!teams_matter || msg_entity.team == self.team)
1301                                 {
1302                                         if(msg_entity.cvar_cl_voice_directional == 1)
1303                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
1304                                         else
1305                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1306                                 }
1307                         break;
1308                 case VOICETYPE_AUTOTAUNT:
1309                         if(!sv_autotaunt)
1310                                 break;
1311                         if(!sv_taunt)
1312                                 break;
1313                         if(sv_gentle)
1314                                 break;
1315                         tauntrand = random();
1316                         FOR_EACH_REALCLIENT(msg_entity)
1317                                 if (tauntrand < msg_entity.cvar_cl_autotaunt)
1318                                 {
1319                                         if (msg_entity.cvar_cl_voice_directional >= 1)
1320                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTN_MAX));
1321                                         else
1322                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1323                                 }
1324                         break;
1325                 case VOICETYPE_TAUNT:
1326                         if(self.classname == "player")
1327                                 if(self.deadflag == DEAD_NO)
1328                                         setanim(self, self.anim_taunt, FALSE, TRUE, TRUE);
1329                         if(!sv_taunt)
1330                                 break;
1331                         if(sv_gentle)
1332                                 break;
1333                         FOR_EACH_REALCLIENT(msg_entity)
1334                         {
1335                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1336                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTN_MAX));
1337                                 else
1338                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1339                         }
1340                 case VOICETYPE_PLAYERSOUND:
1341                         sound(self, chan, sample, VOL_BASE, ATTN_NORM);
1342                         break;
1343                 default:
1344                         backtrace("Invalid voice type!");
1345                         break;
1346         }
1347 }
1348
1349 void PlayerSound(.string samplefield, float chan, float voicetype)
1350 {
1351         string sample;
1352         sample = self.samplefield;
1353         GlobalSound(sample, chan, voicetype);
1354 }
1355
1356 void VoiceMessage(string type, string msg)
1357 {
1358         var .string sample;
1359         var float voicetype, ownteam;
1360         sample = GetVoiceMessageSampleField(type);
1361
1362         if(GetPlayerSoundSampleField_notFound)
1363         {
1364                 sprint(self, strcat("Invalid voice. Use one of: ", allvoicesamples, "\n"));
1365                 return;
1366         }
1367
1368         voicetype = GetVoiceMessageVoiceType(type);
1369         ownteam = (voicetype == VOICETYPE_TEAMRADIO);
1370
1371         float flood;
1372         float flood_spv;
1373         var .float flood_field;
1374
1375         flood = 0;
1376         if(ownteam)
1377         {
1378                 flood_spv = cvar("g_voice_flood_spv_team");
1379                 flood_field = floodcontrol_voiceteam;
1380         }
1381         else
1382         {
1383                 flood_spv = cvar("g_voice_flood_spv");
1384                 flood_field = floodcontrol_voice;
1385         }
1386
1387         if(time >= self.flood_field)
1388                 self.flood_field = max(time, self.flood_field) + flood_spv;
1389         else
1390                 flood = 1;
1391
1392         if (timeoutStatus == 2) //when game is paused, no flood protection
1393                 self.flood_field = flood = 0;
1394
1395         if (msg != "")
1396                 Say(self, ownteam, world, msg, 0);
1397
1398         if (!flood)
1399                 PlayerSound(sample, CHAN_VOICE, voicetype);
1400 }