support for announcer/male/impressive and announcer/male/headshot ;)
[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.movetype = oldself.movetype;
49         self.nextthink = oldself.nextthink;
50         self.skin = oldself.skin;
51         self.solid = oldself.solid;
52         self.takedamage = oldself.takedamage;
53         self.think = oldself.think;
54         self.gibrandom = oldself.gibrandom;
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.oldorigin = oldself.origin;
65         self = oldself;
66 }
67
68 float animparseerror;
69 vector animparseline(float animfile)
70 {
71         local string line;
72         local float c;
73         local vector anim;
74         if (animfile < 0)
75                 return '0 1 2';
76         line = fgets(animfile);
77         c = tokenize(line);
78         if (c != 3)
79         {
80                 animparseerror = TRUE;
81                 return '0 1 2';
82         }
83         anim_x = stof(argv(0));
84         anim_y = stof(argv(1));
85         anim_z = stof(argv(2));
86         // don't allow completely bogus values
87         if (anim_x < 0 || anim_y < 1 || anim_z < 0.001)
88                 anim = '0 1 2';
89         return anim;
90 };
91
92 void player_setupanimsformodel()
93 {
94         local string animfilename;
95         local float animfile;
96         // defaults for legacy .zym models without animinfo files
97         self.anim_die1 = '0 1 0.5'; // 2 seconds
98         self.anim_die2 = '1 1 0.5'; // 2 seconds
99         self.anim_draw = '2 1 3'; // TODO: analyze models and set framerate
100         self.anim_duck = '3 1 100'; // this anim seems bogus in most models, so make it play VERY briefly!
101         self.anim_duckwalk = '4 1 1';
102         self.anim_duckjump = '5 1 100'; // zym anims keep playing until changed, so this only has to start the anim, landing will end it
103         self.anim_duckidle = '6 1 1';
104         self.anim_idle = '7 1 1';
105         self.anim_jump = '8 1 100'; // zym anims keep playing until changed, so this only has to start the anim, landing will end it
106         self.anim_pain1 = '9 1 2'; // 0.5 seconds
107         self.anim_pain2 = '10 1 2'; // 0.5 seconds
108         self.anim_shoot = '11 1 5'; // TODO: analyze models and set framerate
109         self.anim_taunt = '12 1 1'; // FIXME?  there is no code using this anim
110         self.anim_run = '13 1 1';
111         self.anim_runbackwards = '14 1 1';
112         self.anim_strafeleft = '15 1 1';
113         self.anim_straferight = '16 1 1';
114         self.anim_dead1 = '17 1 1';
115         self.anim_dead2 = '18 1 1';
116         self.anim_forwardright = '19 1 1';
117         self.anim_forwardleft = '20 1 1';
118         self.anim_backright = '21 1 1';
119         self.anim_backleft  = '22 1 1';
120         animparseerror = FALSE;
121         animfilename = strcat(self.model, ".animinfo");
122         animfile = fopen(animfilename, FILE_READ);
123         if (animfile >= 0)
124         {
125                 self.anim_die1         = animparseline(animfile);
126                 self.anim_die2         = animparseline(animfile);
127                 self.anim_draw         = animparseline(animfile);
128                 self.anim_duck         = animparseline(animfile);
129                 self.anim_duckwalk     = animparseline(animfile);
130                 self.anim_duckjump     = animparseline(animfile);
131                 self.anim_duckidle     = animparseline(animfile);
132                 self.anim_idle         = animparseline(animfile);
133                 self.anim_jump         = animparseline(animfile);
134                 self.anim_pain1        = animparseline(animfile);
135                 self.anim_pain2        = animparseline(animfile);
136                 self.anim_shoot        = animparseline(animfile);
137                 self.anim_taunt        = animparseline(animfile);
138                 self.anim_run          = animparseline(animfile);
139                 self.anim_runbackwards = animparseline(animfile);
140                 self.anim_strafeleft   = animparseline(animfile);
141                 self.anim_straferight  = animparseline(animfile);
142                 self.anim_forwardright = animparseline(animfile);
143                 self.anim_forwardleft  = animparseline(animfile);
144                 self.anim_backright    = animparseline(animfile);
145                 self.anim_backleft     = animparseline(animfile);
146                 fclose(animfile);
147
148                 // derived anims
149                 self.anim_dead1 = '0 1 1' + '1 0 0' * (self.anim_die1_x + self.anim_die1_y - 1);
150                 self.anim_dead2 = '0 1 1' + '1 0 0' * (self.anim_die2_x + self.anim_die2_y - 1);
151
152                 if (animparseerror)
153                         print("Parse error in ", animfilename, ", some player animations are broken\n");
154         }
155         else
156                 dprint("File ", animfilename, " not found, assuming legacy .zym model animation timings\n");
157         // reset animstate now
158         player_setanim(self.anim_idle, TRUE, FALSE, TRUE);
159 };
160
161 void player_setanim(vector anim, float looping, float override, float restart)
162 {
163         if (!restart)
164         if (anim_x == self.animstate_startframe)
165         if (anim_y == self.animstate_numframes)
166         if (anim_z == self.animstate_framerate)
167                 return;
168         self.animstate_startframe = anim_x;
169         self.animstate_numframes = anim_y;
170         self.animstate_framerate = anim_z;
171         self.animstate_starttime = time;
172         self.animstate_endtime = time + self.animstate_numframes / self.animstate_framerate;
173         self.animstate_looping = looping;
174         self.animstate_override = override;
175         self.frame = self.animstate_startframe;
176 };
177
178 void player_updateframe()
179 {
180         if (time >= self.animstate_endtime)
181         {
182                 if (self.animstate_looping)
183                 {
184                         self.animstate_starttime = self.animstate_endtime;
185                         self.animstate_endtime = self.animstate_endtime + self.animstate_numframes / self.animstate_framerate;
186                 }
187                 self.animstate_override = FALSE;
188         }
189         self.frame = self.animstate_startframe + bound(0, (time - self.animstate_starttime) * self.animstate_framerate, self.animstate_numframes - 1);
190 };
191
192 void player_anim (void)
193 {
194         player_updateframe();
195
196         if (self.deadflag != DEAD_NO)
197         {
198                 if (time > self.animstate_endtime)
199                 {
200                         if (self.maxs_z > 5)
201                         {
202                                 self.maxs_z = 5;
203                                 setsize(self, self.mins, self.maxs);
204                         }
205                         self.frame = self.dead_frame;
206                 }
207                 return;
208         }
209
210         if (!self.animstate_override)
211         {
212                 if (!(self.flags & FL_ONGROUND))
213                 {
214                         if (self.crouch)
215                                 player_setanim(self.anim_duckjump, FALSE, TRUE, FALSE);
216                         else
217                                 player_setanim(self.anim_jump, FALSE, TRUE, FALSE);
218                 }
219                 else if (self.crouch)
220                 {
221                         if (self.movement_x * self.movement_x + self.movement_y * self.movement_y > 20)
222                                 player_setanim(self.anim_duckwalk, TRUE, FALSE, FALSE);
223                         else
224                                 player_setanim(self.anim_duckidle, TRUE, FALSE, FALSE);
225                 }
226                 else if ((self.movement_x * self.movement_x + self.movement_y * self.movement_y) > 20)
227                 {
228                         if (self.movement_x > 0 && self.movement_y == 0)
229                                 player_setanim(self.anim_run, TRUE, FALSE, FALSE);
230                         else if (self.movement_x < 0 && self.movement_y == 0)
231                                 player_setanim(self.anim_runbackwards, TRUE, FALSE, FALSE);
232                         else if (self.movement_x == 0 && self.movement_y > 0)
233                                 player_setanim(self.anim_straferight, TRUE, FALSE, FALSE);
234                         else if (self.movement_x == 0 && self.movement_y < 0)
235                                 player_setanim(self.anim_strafeleft, TRUE, FALSE, FALSE);
236                         else if (self.movement_x > 0 && self.movement_y > 0)
237                                 player_setanim(self.anim_forwardright, TRUE, FALSE, FALSE);
238                         else if (self.movement_x > 0 && self.movement_y < 0)
239                                 player_setanim(self.anim_forwardleft, TRUE, FALSE, FALSE);
240                         else if (self.movement_x < 0 && self.movement_y > 0)
241                                 player_setanim(self.anim_backright, TRUE, FALSE, FALSE);
242                         else if (self.movement_x < 0 && self.movement_y < 0)
243                                 player_setanim(self.anim_backleft, TRUE, FALSE, FALSE);
244                         else
245                                 player_setanim(self.anim_run, TRUE, FALSE, FALSE);
246                 }
247                 else
248                         player_setanim(self.anim_idle, TRUE, FALSE, FALSE);
249         }
250 }
251 //End change by Supajoe on 11:44 PM EST 11/16/03 (Subject: Player animations)
252
253 void SpawnThrownWeapon (vector org, float w)
254 {
255         W_ThrowWeapon(randomvec() * 100 + '0 0 200', org - self.origin, FALSE);
256 }
257
258 void PlayerCorpseDamage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
259 {
260         local float take, save;
261         if(sv_gentle < 1)       
262                 pointparticles(particleeffectnum("blood"), hitloc, force, bound(0, damage, 200));
263         // damage resistance (ignore most of the damage from a bullet or similar)
264         damage = max(damage - 5, 1);
265
266         save = bound(0, damage * cvar("g_balance_armor_blockpercent"), self.armorvalue);
267         take = bound(0, damage - save, damage);
268
269         if (save > 10)
270                 sound (self, CHAN_PROJECTILE, "misc/armorimpact.wav", VOL_BASE, ATTN_NORM);
271         else if (take > 30)
272                 sound (self, CHAN_PROJECTILE, "misc/bodyimpact2.wav", VOL_BASE, ATTN_NORM);
273         else if (take > 10)
274                 sound (self, CHAN_PROJECTILE, "misc/bodyimpact1.wav", VOL_BASE, ATTN_NORM);
275
276         if(sv_gentle < 1) {     
277                 if (take > 50)
278                         TossGib (world, "models/gibs/chunk.mdl", hitloc, force * -0.1,1);
279                 if (take > 100)
280                         TossGib (world, "models/gibs/chunk.mdl", hitloc, force * -0.2,1);
281         }
282
283         if (!(self.flags & FL_GODMODE))
284         {
285                 self.armorvalue = self.armorvalue - save;
286                 self.health = self.health - take;
287                 // pause regeneration for 5 seconds
288                 self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_health_regen"));
289         }
290         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
291         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
292         self.dmg_inflictor = inflictor;
293
294         if (self.health <= -50)
295         {
296                 // don't use any animations as a gib
297                 self.frame = 0;
298                 self.dead_frame = 0;
299                 // view just above the floor
300                 self.view_ofs = '0 0 4';
301
302                 // make a juicy mess
303                 local float multiplier;
304                 multiplier = 1;
305                 if (cvar("ekg"))
306                         multiplier = 5;
307
308
309                 // make a meaty mess
310                 TossGib (self, "models/gibs/eye.md3", self.origin + self.view_ofs, self.velocity + randomvec() * 150,0);
311                 
312                 if(sv_gentle < 1) {
313                         te_bloodshower (self.origin + self.mins, self.origin + self.maxs, 1200 * multiplier, 1000);
314                         TossGib (world, "models/gibs/bloodyskull.md3", self.origin + self.view_ofs, self.velocity,0);
315
316                         local float c;
317                         c = 0;
318
319                         while (c < multiplier)
320                         {
321                                 c = c + 1;
322                                 //TossGib (world, "models/gibs/gib1.md3", self.origin, self.velocity + randomvec() * 450,0);
323                                 //TossGib (world, "models/gibs/gib2.md3", self.origin, self.velocity + randomvec() * 450,0);
324                                 //TossGib (world, "models/gibs/gib3.md3", self.origin, self.velocity + randomvec() * 450,0);
325                                 //TossGib (world, "models/gibs/gib4.md3", self.origin, self.velocity + randomvec() * 450,0);
326                                 //TossGib (world, "models/gibs/gib5.md3", self.origin, self.velocity + randomvec() * 450,0);
327                                 //TossGib (world, "models/gibs/gib6.md3", self.origin, self.velocity + randomvec() * 450,0);
328
329                                 TossGib (world, "models/gibs/arm.md3", self.origin + self.view_ofs, self.velocity + randomvec() * (random() * 120 + 90),0);
330                                 TossGib (world, "models/gibs/arm.md3", self.origin + self.view_ofs, self.velocity + randomvec() * (random() * 120 + 90),0);
331                                 //TossGib (world, "models/gibs/arm.md3", self.origin + self.view_ofs, self.velocity + randomvec() * 150,0);
332                                 //TossGib (world, "models/gibs/arm.md3", self.origin + self.view_ofs, self.velocity + randomvec() * 150,0);
333                                 TossGib (world, "models/gibs/chest.md3", self.origin + self.view_ofs * 0.5, self.velocity + randomvec() * (random() * 120 + 80),0);
334                                 //TossGib (world, "models/gibs/smallchest.md3", self.origin + self.view_ofs, self.velocity + randomvec() * 150,0);
335                                 TossGib (world, "models/gibs/smallchest.md3", self.origin + self.view_ofs, self.velocity + randomvec() * (random() * 120 + 80),0);
336                                 TossGib (world, "models/gibs/leg1.md3", self.origin + self.view_ofs * -0.2, self.velocity + randomvec() * (random() * 120 + 85),0);
337                                 TossGib (world, "models/gibs/leg2.md3", self.origin + self.view_ofs * -0.4, self.velocity + randomvec() * (random() * 120 + 85),0);
338                                 //TossGib (world, "models/gibs/leg1.md3", self.origin + self.view_ofs * -0.2, self.velocity + randomvec() * 150,0);
339                                 //TossGib (world, "models/gibs/leg2.md3", self.origin + self.view_ofs * -0.4, self.velocity + randomvec() * 150,0);
340
341                                 // these splat on impact
342                                 TossGib (world, "models/gibs/chunk.mdl", self.origin, self.velocity + randomvec() * 450,1);
343                                 TossGib (world, "models/gibs/chunk.mdl", self.origin, self.velocity + randomvec() * 450,1);
344                                 TossGib (world, "models/gibs/chunk.mdl", self.origin, self.velocity + randomvec() * 450,1);
345                                 TossGib (world, "models/gibs/chunk.mdl", self.origin, self.velocity + randomvec() * 450,1);
346                         }
347                         sound (self, CHAN_PLAYER, "misc/gib.wav", VOL_BASE, ATTN_NORM);
348                 }
349         }
350 }
351
352 void ClientKill_Now_TeamChange();
353
354 void PlayerDamage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
355 {
356         local float take, save, waves, sdelay;
357
358         if(self.hook)
359                 if(self.hook.aiment)
360                         if(self.hook.aiment == attacker)
361                                 RemoveGrapplingHook(self); // STOP THAT, you parasite!
362
363         damage = damage * bound(1.0, self.cvar_cl_handicap, 100.0);
364         if(sv_gentle > 0) {
365                 pointparticles(particleeffectnum("damage_hit"), hitloc, force, bound(0, damage, 200));
366         } else {
367                 pointparticles(particleeffectnum("blood"), hitloc, force, bound(0, damage, 200));
368         }
369
370         if(g_arena)
371         if(numspawned < 2)
372                 return;
373
374         if (!g_minstagib)
375         {
376                 save = bound(0, damage * cvar("g_balance_armor_blockpercent"), self.armorvalue);
377                 take = bound(0, damage - save, damage);
378         }
379         else
380         {
381                 save = 0;
382                 take = damage;
383         }
384
385         if (save > 10)
386                 sound (self, CHAN_PROJECTILE, "misc/armorimpact.wav", VOL_BASE, ATTN_NORM);
387         else if (take > 30)
388                 sound (self, CHAN_PROJECTILE, "misc/bodyimpact2.wav", VOL_BASE, ATTN_NORM);
389         else if (take > 10)
390                 sound (self, CHAN_PROJECTILE, "misc/bodyimpact1.wav", VOL_BASE, ATTN_NORM); // FIXME possibly remove them?
391
392         if (take > 50)
393                 TossGib (world, "models/gibs/chunk.mdl", hitloc, force * -0.1,1);
394         if (take > 100)
395                 TossGib (world, "models/gibs/chunk.mdl", hitloc, force * -0.2,1);
396
397         if (time > self.spawnshieldtime)
398         {
399                 if (!(self.flags & FL_GODMODE))
400                 {
401                         self.armorvalue = self.armorvalue - save;
402                         self.health = self.health - take;
403                         // pause regeneration for 5 seconds
404                         self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_health_regen"));
405
406                         if (time > self.pain_finished)          //Don't switch pain sequences like crazy
407                         {
408                                 self.pain_finished = time + 0.5;        //Supajoe
409
410                                 if(sv_gentle < 1) {             
411                                         if (random() > 0.5)
412                                                 player_setanim(self.anim_pain1, FALSE, TRUE, TRUE);
413                                         else
414                                                 player_setanim(self.anim_pain2, FALSE, TRUE, TRUE);
415
416                                         if(deathtype >= DEATH_SPECIAL_START || deathtype & DEATH_WEAPONMASK != WEP_LASER || attacker != self || self.health < 2 * cvar("g_balance_laser_primary_damage") * cvar("g_balance_selfdamagepercent") + 1)
417                                         // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
418                                         {
419                                                 if(self.health > 75) // TODO make a "gentle" version?
420                                                         PlayerSound(playersound_pain100, CHAN_PAIN, 0);
421                                                 else if(self.health > 50)
422                                                         PlayerSound(playersound_pain75, CHAN_PAIN, 0);
423                                                 else if(self.health > 25)
424                                                         PlayerSound(playersound_pain50, CHAN_PAIN, 0);
425                                                 else if(self.health > 1)
426                                                         PlayerSound(playersound_pain25, CHAN_PAIN, 0);
427                                         }
428                                 }
429
430                                 // throw off bot aim temporarily
431                                 local float shake;
432                                 shake = damage * 5 / (bound(0,skill,100) + 1);
433                                 self.v_angle_x = self.v_angle_x + (random() * 2 - 1) * shake;
434                                 self.v_angle_y = self.v_angle_y + (random() * 2 - 1) * shake;
435                         }
436                 }
437                 else
438                         self.max_armorvalue += (save + take);
439         }
440         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
441         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
442         self.dmg_inflictor = inflictor;
443
444         if(attacker == self)
445         {
446                 // don't reset pushltime for self damage as it may be an attempt to
447                 // escape a lava pit or similar
448                 //self.pushltime = 0;
449         }
450         else if(attacker.classname == "player" || attacker.classname == "gib")
451         {
452                 self.pusher = attacker;
453                 self.pushltime = time + cvar("g_maxpushtime");
454         }
455         else if(time < self.pushltime)
456         {
457                 attacker = self.pusher;
458                 self.pushltime = max(self.pushltime, time + 0.6);
459         }
460         else
461                 self.pushltime = 0;
462
463         if (self.health < 1)
464         {
465                 float defer_ClientKill_Now_TeamChange;
466                 defer_ClientKill_Now_TeamChange = FALSE;
467
468                 if(sv_gentle < 1) // TODO make a "gentle" version?
469                 {
470                         if(deathtype == DEATH_DROWN)
471                                 PlayerSound(playersound_drown, CHAN_PAIN, 0);
472                         else
473                                 PlayerSound(playersound_death, CHAN_PAIN, 0);
474                 }
475
476                 // get rid of kill indicator
477                 if(self.killindicator)
478                 {
479                         remove(self.killindicator);
480                         self.killindicator = world;
481                         if(self.killindicator_teamchange)
482                                 defer_ClientKill_Now_TeamChange = TRUE;
483
484                         if(self.classname == "body")
485                         if(deathtype == DEATH_KILL)
486                         {
487                                 // for the lemmings fans, a small harmless explosion
488                                 pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
489                         }
490                 }
491
492                 // become fully visible
493                 self.alpha = 1;
494                 // clear selected player display
495                 ClearSelectedPlayer();
496                 // throw a weapon
497                 SpawnThrownWeapon (self.origin + (self.mins + self.maxs) * 0.5, self.switchweapon);
498                 // print an obituary message
499                 Obituary (attacker, inflictor, self, deathtype);
500                 race_PreDie();
501                 DropAllRunes(self);
502                 if(self == attacker)
503                         kh_Key_DropAll(self, TRUE);
504                 else if(attacker.classname == "player" || attacker.classname == "gib")
505                         kh_Key_DropAll(self, FALSE);
506                 else
507                         kh_Key_DropAll(self, TRUE);
508                 if(self.flagcarried)
509                 {
510                         if(attacker.classname != "player" && attacker.classname != "gib")
511                                 DropFlag(self.flagcarried, self, attacker); // penalty for flag loss by suicide
512                         else if(attacker.team == self.team)
513                                 DropFlag(self.flagcarried, attacker, attacker); // penalty for flag loss by suicide/teamkill
514                         else
515                                 DropFlag(self.flagcarried, world, attacker);
516                 }
517                 Portal_ClearAllLater(self);
518                 // clear waypoints
519                 WaypointSprite_PlayerDead();
520                 // make the corpse upright (not tilted)
521                 self.angles_x = 0;
522                 self.angles_z = 0;
523                 // don't spin
524                 self.avelocity = '0 0 0';
525                 // view from the floor
526                 self.view_ofs = '0 0 -8';
527                 // toss the corpse
528                 self.movetype = MOVETYPE_TOSS;
529                 // shootable corpse
530                 self.solid = SOLID_CORPSE;
531                 // don't stick to the floor
532                 self.flags = self.flags - (self.flags & FL_ONGROUND);
533                 // dying animation
534                 self.deadflag = DEAD_DYING;
535                 // when to allow respawn
536                 sdelay = 0;
537                 waves = 0;
538                 if(cvar("g_respawn_mapsettings"))
539                 {
540                         sdelay = cvar("g_respawn_mapsettings_delay");
541                         waves = cvar("g_respawn_mapsettings_waves");
542                 }
543                 if(!sdelay)
544                         sdelay = cvar(strcat("g_", GetGametype(), "_respawn_delay"));
545                 if(!sdelay)
546                         sdelay = cvar("g_respawn_delay");
547                 if(!waves)
548                         waves = cvar(strcat("g_", GetGametype(), "_respawn_waves"));
549                 if(!waves)
550                         waves = cvar("g_respawn_waves");
551                 if(waves)
552                         self.death_time = ceil((time + sdelay) / waves) * waves;
553                 else
554                         self.death_time = time + sdelay;
555                 if((sdelay + waves >= 5.0) && (self.death_time - time > 1.75))
556                         self.respawn_countdown = 10; // first number to count down from is 10
557                 else
558                         self.respawn_countdown = -1; // do not count down
559                 if (random() < 0.5)
560                 {
561                         player_setanim(self.anim_die1, FALSE, TRUE, TRUE);
562                         self.dead_frame = self.anim_dead1_x;
563                 }
564                 else
565                 {
566                         player_setanim(self.anim_die2, FALSE, TRUE, TRUE);
567                         self.dead_frame = self.anim_dead2_x;
568                 }
569                 // set damage function to corpse damage
570                 self.event_damage = PlayerCorpseDamage;
571                 // call the corpse damage function just in case it wants to gib
572                 self.event_damage(inflictor, attacker, 0, deathtype, hitloc, force);
573                 // set up to fade out later
574                 SUB_SetFade (self, time + 12 + random () * 4, 1);
575
576                 // remove laserdot
577                 if(self.weaponentity)
578                         if(self.weaponentity.lasertarget)
579                                 remove(self.weaponentity.lasertarget);
580
581                 if(clienttype(self) == CLIENTTYPE_REAL)
582                 {
583                         self.fixangle = TRUE;
584                         //msg_entity = self;
585                         //WriteByte (MSG_ONE, SVC_SETANGLE);
586                         //WriteAngle (MSG_ONE, self.v_angle_x);
587                         //WriteAngle (MSG_ONE, self.v_angle_y);
588                         //WriteAngle (MSG_ONE, 80);
589                 }
590
591                 if(g_arena)
592                         Spawnqueue_Unmark(self);
593
594                 if(defer_ClientKill_Now_TeamChange)
595                         ClientKill_Now_TeamChange();
596
597                 if(sv_gentle > 0) {
598                         // remove corpse
599                         PlayerCorpseDamage (inflictor, attacker, 100.0, deathtype, hitloc, force);
600                         pointparticles(particleeffectnum("damage_dissolve"), self.origin, force, 1);
601                 }
602         }
603 }
604
605 float UpdateSelectedPlayer_countvalue(float v)
606 {
607         return max(0, (v - 1.0) / 0.5);
608 }
609
610 // returns: -2 if no hit, otherwise cos of the angle
611 // uses the global v_angle
612 float UpdateSelectedPlayer_canSee(entity p, float mincosangle, float maxdist)
613 {
614         vector so, d;
615         float c;
616
617         if(p == self)
618                 return -2;
619
620         if(p.deadflag)
621                 return -2;
622
623         so = self.origin + self.view_ofs;
624         d = p.origin - so;
625
626         // misaimed?
627         if(dist_point_line(d, '0 0 0', v_forward) > maxdist)
628                 return -2;
629
630         // now find the cos of the angle...
631         c = normalize(d) * v_forward;
632
633         if(c <= mincosangle)
634                 return -2;
635
636         traceline(so, p.origin, MOVE_NOMONSTERS, self);
637         if(trace_fraction < 1)
638                 return -2;
639
640         return c;
641 }
642
643 void ClearSelectedPlayer()
644 {
645         if(self.selected_player)
646         {
647                 centerprint_expire(self, CENTERPRIO_POINT);
648                 self.selected_player = world;
649                 self.selected_player_display_needs_update = FALSE;
650         }
651 }
652
653 void UpdateSelectedPlayer()
654 {
655         entity selected;
656         float selected_score;
657         selected = world;
658         selected_score = 0.95; // 18 degrees
659
660         if(!cvar("sv_allow_shownames"))
661                 return;
662
663         if(clienttype(self) != CLIENTTYPE_REAL)
664                 return;
665
666         if(self.cvar_cl_shownames == 0)
667                 return;
668
669         if(self.cvar_cl_shownames == 1 && !teams_matter)
670                 return;
671
672         makevectors(self.v_angle); // sets v_forward
673
674         // 1. cursor trace is always right
675         if(self.cursor_trace_ent && self.cursor_trace_ent.classname == "player" && !self.cursor_trace_ent.deadflag)
676         {
677                 selected = self.cursor_trace_ent;
678         }
679         else
680         {
681                 // 2. if we don't have a cursor trace, find the player which is least
682                 //    mis-aimed at
683                 entity p;
684                 FOR_EACH_PLAYER(p)
685                 {
686                         float c;
687                         c = UpdateSelectedPlayer_canSee(p, selected_score, 100); // 100 = 2.5 meters
688                         if(c >= -1)
689                         {
690                                 selected = p;
691                                 selected_score = c;
692                         }
693                 }
694         }
695
696         if(selected)
697         {
698                 self.selected_player_display_timeout = time + self.cvar_scr_centertime;
699         }
700         else
701         {
702                 if(time < self.selected_player_display_timeout)
703                         if(UpdateSelectedPlayer_canSee(self.selected_player, 0.7, 200) >= -1) // 5 meters, 45 degrees
704                                 selected = self.selected_player;
705         }
706
707         if(selected)
708         {
709                 if(selected == self.selected_player)
710                 {
711                         float save;
712                         save = UpdateSelectedPlayer_countvalue(self.selected_player_count);
713                         self.selected_player_count = self.selected_player_count + frametime;
714                         if(save != UpdateSelectedPlayer_countvalue(self.selected_player_count))
715                         {
716                                 string namestr, healthstr;
717                                 namestr = playername(selected);
718                                 if(teams_matter)
719                                 {
720                                         healthstr = ftos(floor(selected.health));
721                                         if(self.team == selected.team)
722                                         {
723                                                 namestr = strcat(namestr, " (", healthstr, "%)");
724                                                 self.selected_player_display_needs_update = TRUE;
725                                         }
726                                 }
727                                 centerprint_atprio(self, CENTERPRIO_POINT, namestr);
728                         }
729                 }
730                 else
731                 {
732                         ClearSelectedPlayer();
733                         self.selected_player = selected;
734                         self.selected_player_time = time;
735                         self.selected_player_count = 0;
736                         self.selected_player_display_needs_update = FALSE;
737                 }
738         }
739         else
740         {
741                 ClearSelectedPlayer();
742         }
743
744         if(self.selected_player)
745                 self.last_selected_player = self.selected_player;
746 }
747
748 float GetVoiceMessageTeamsayType(string type)
749 {
750         if(type == "taunt")
751                 return 0;
752         if(type == "teamshoot")
753                 return 2;
754         return 1;
755 }
756
757 string allvoicesamples = "attack attackinfive coverme defend freelance incoming meet needhelp seenflag taunt teamshoot";
758 float GetPlayerSoundSampleField_notFound;
759 float GetPlayerSoundSampleField_fixed;
760 .string GetVoiceMessageSampleField(string type)
761 {
762         GetPlayerSoundSampleField_notFound = 0;
763         GetPlayerSoundSampleField_fixed = 0;
764         switch(type)
765         {
766                 case "attack":       return playersound_attack;
767                 case "attackinfive": return playersound_attackinfive;
768                 case "coverme":      return playersound_coverme;
769                 case "defend":       return playersound_defend;
770                 case "freelance":    return playersound_freelance;
771                 case "incoming":     return playersound_incoming;
772                 case "meet":         return playersound_meet;
773                 case "needhelp":     return playersound_needhelp;
774                 case "seenflag":     return playersound_seenflag;
775                 case "taunt":        return playersound_taunt;
776                 case "teamshoot":    return playersound_teamshoot;
777         }
778         GetPlayerSoundSampleField_notFound = 1;
779         return playersound_taunt;
780 }
781
782 .string GetPlayerSoundSampleField(string type)
783 {
784         GetPlayerSoundSampleField_notFound = 0;
785         GetPlayerSoundSampleField_fixed = 0;
786         switch(type)
787         {
788                 case "death":        return playersound_death;
789                 case "drown":        return playersound_drown;
790                 case "falling":      return playersound_falling;
791                 case "gasp":         return playersound_gasp;
792                 case "jump":         return playersound_jump;
793                 case "pain25":       return playersound_pain25;
794                 case "pain50":       return playersound_pain50;
795                 case "pain75":       return playersound_pain75;
796                 case "pain100":      return playersound_pain100;
797         }
798         GetPlayerSoundSampleField_notFound = 1;
799         return playersound_taunt;
800 }
801
802 void PrecacheGlobalSound(string samplestring)
803 {
804         float n, i;
805         tokenize(samplestring);
806         n = stof(argv(1));
807         if(n > 0)
808         {
809                 for(i = 1; i <= n; ++i)
810                         precache_sound(strcat(argv(0), ftos(i), ".wav"));
811         }
812         else
813         {
814                 precache_sound(strcat(argv(0), ".wav"));
815         }
816 }
817
818 void PrecachePlayerSounds(string f)
819 {
820         float fh;
821         string s;
822         fh = fopen(f, FILE_READ);
823         if(fh < 0)
824                 return;
825         while((s = fgets(fh)))
826         {
827                 if(tokenize(s) != 3)
828                 {
829                         dprint("Invalid sound info line: ", s, "\n");
830                         continue;
831                 }
832                 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
833         }
834         fclose(fh);
835 }
836
837 void ClearPlayerSounds()
838 {
839         if(self.playersound_attack      ) { strunzone(self.playersound_attack      ); self.playersound_attack = string_null; }
840         if(self.playersound_attackinfive) { strunzone(self.playersound_attackinfive); self.playersound_attackinfive = string_null; }
841         if(self.playersound_coverme     ) { strunzone(self.playersound_coverme     ); self.playersound_coverme = string_null; }
842         if(self.playersound_defend      ) { strunzone(self.playersound_defend      ); self.playersound_defend = string_null; }
843         if(self.playersound_freelance   ) { strunzone(self.playersound_freelance   ); self.playersound_freelance = string_null; }
844         if(self.playersound_incoming    ) { strunzone(self.playersound_incoming    ); self.playersound_incoming = string_null; }
845         if(self.playersound_meet        ) { strunzone(self.playersound_meet        ); self.playersound_meet = string_null; }
846         if(self.playersound_needhelp    ) { strunzone(self.playersound_needhelp    ); self.playersound_needhelp = string_null; }
847         if(self.playersound_seenflag    ) { strunzone(self.playersound_seenflag    ); self.playersound_seenflag = string_null; }
848         if(self.playersound_taunt       ) { strunzone(self.playersound_taunt       ); self.playersound_taunt = string_null; }
849         if(self.playersound_teamshoot   ) { strunzone(self.playersound_teamshoot   ); self.playersound_teamshoot = string_null; }
850         if(self.playersound_death       ) { strunzone(self.playersound_death       ); self.playersound_death = string_null; }
851         if(self.playersound_drown       ) { strunzone(self.playersound_drown       ); self.playersound_drown = string_null; }
852         if(self.playersound_falling     ) { strunzone(self.playersound_falling     ); self.playersound_falling = string_null; }
853         if(self.playersound_gasp        ) { strunzone(self.playersound_gasp        ); self.playersound_gasp = string_null; }
854         if(self.playersound_jump        ) { strunzone(self.playersound_jump        ); self.playersound_jump = string_null; }
855         if(self.playersound_pain25      ) { strunzone(self.playersound_pain25      ); self.playersound_pain25 = string_null; }
856         if(self.playersound_pain50      ) { strunzone(self.playersound_pain50      ); self.playersound_pain50 = string_null; }
857         if(self.playersound_pain75      ) { strunzone(self.playersound_pain75      ); self.playersound_pain75 = string_null; }
858         if(self.playersound_pain100     ) { strunzone(self.playersound_pain100     ); self.playersound_pain100 = string_null; }
859 }
860
861 void LoadPlayerSounds(string f, float first)
862 {
863         float fh;
864         string s;
865         var .string field;
866         fh = fopen(f, FILE_READ);
867         if(fh < 0)
868                 return;
869         while((s = fgets(fh)))
870         {
871                 if(tokenize(s) != 3)
872                         continue;
873                 field = GetPlayerSoundSampleField(argv(0));
874                 if(GetPlayerSoundSampleField_notFound)
875                         field = GetVoiceMessageSampleField(argv(0));
876                 if(GetPlayerSoundSampleField_notFound)
877                         continue;
878                 if(GetPlayerSoundSampleField_fixed)
879                         if not(first)
880                                 continue;
881                 if(self.field)
882                         strunzone(self.field);
883                 self.field = strzone(strcat(argv(1), " ", argv(2)));
884         }
885         fclose(fh);
886 }
887
888 .float modelindex_for_playersound;
889 void UpdatePlayerSounds()
890 {
891         if(self.modelindex == self.modelindex_for_playersound)
892                 return;
893         self.modelindex_for_playersound = self.modelindex;
894         ClearPlayerSounds();
895         LoadPlayerSounds("sound/player/default.sounds", 1);
896         LoadPlayerSounds(strcat(self.model, ".sounds"), 0);
897 }
898
899 void GlobalSound(string sample, float chan, float teamsay)
900 {
901         float n;
902
903         if(sample == "")
904                 return;
905
906         tokenize(sample);
907         n = stof(argv(1));
908         if(n > 0)
909                 sample = strcat(argv(0), ftos(ceil(random() * n)), ".wav"); // randomization
910         else
911                 sample = strcat(argv(0), ".wav"); // randomization
912
913         if(teamsay == 2) // only to last attacker
914         {
915                 if(teams_matter)
916                         if(self.pusher)
917                                 if(self.pusher.team == self.team)
918                                 {
919                                         msg_entity = self.pusher;
920                                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTN_NONE);
921                                         msg_entity = self;
922                                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTN_NONE);
923                                 }
924         }
925         else if(teamsay == 0) // to everyone
926         {
927                 // broadcast the sound, but it's directional
928                 sound(self, chan, sample, VOL_BASE, ATTN_NORM);
929         }
930         else if(teamsay == 1) // to the same team
931         {
932                 if(teams_matter)
933                 {
934                         entity e;
935                         FOR_EACH_REALCLIENT(e)
936                                 if(!teams_matter || e.team == self.team)
937                                 {
938                                         msg_entity = e;
939                                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTN_NONE);
940                                 }
941                 }
942                 else
943                 {
944                         sound(self, chan, sample, VOL_BASE, ATTN_NONE);
945                 }
946         }
947 }
948
949 void PlayerSound(.string samplefield, float chan, float teamsay)
950 {
951         string sample;
952         sample = self.samplefield;
953         GlobalSound(sample, chan, teamsay);
954 }
955
956 void VoiceMessage(string type)
957 {
958         var .string sample;
959         var float teamsay;
960         sample = GetVoiceMessageSampleField(type);
961
962         if(GetPlayerSoundSampleField_notFound)
963         {
964                 sprint(self, strcat("Invalid voice. Use one of: ", allvoicesamples, "\n"));
965                 return;
966         }
967
968         teamsay = GetVoiceMessageTeamsayType(type);
969
970         PlayerSound(sample, CHAN_VOICE, teamsay);
971 }