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