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