]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_player.qc
potential fix for clones
[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         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
286
287         // damage resistance (ignore most of the damage from a bullet or similar)
288         damage = max(damage - 5, 1);
289
290         save = bound(0, damage * cvar("g_balance_armor_blockpercent"), self.armorvalue);
291         take = bound(0, damage - save, damage);
292
293         if(sound_allowed(MSG_BROADCAST, attacker))
294         {
295                 if (save > 10)
296                         sound (self, CHAN_PROJECTILE, "misc/armorimpact.wav", VOL_BASE, ATTN_NORM);
297                 else if (take > 30)
298                         sound (self, CHAN_PROJECTILE, "misc/bodyimpact2.wav", VOL_BASE, ATTN_NORM);
299                 else if (take > 10)
300                         sound (self, CHAN_PROJECTILE, "misc/bodyimpact1.wav", VOL_BASE, ATTN_NORM);
301         }
302
303         if (take > 50)
304                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
305         if (take > 100)
306                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
307
308         if (!(self.flags & FL_GODMODE))
309         {
310                 self.armorvalue = self.armorvalue - save;
311                 self.health = self.health - take;
312                 // pause regeneration for 5 seconds
313                 self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_health_regen"));
314         }
315         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
316         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
317         self.dmg_inflictor = inflictor;
318
319         if (self.health <= -75 && self.modelindex != 0)
320         {
321                 // don't use any animations as a gib
322                 self.frame = 0;
323                 self.dead_frame = 0;
324                 // view just above the floor
325                 self.view_ofs = '0 0 4';
326
327                 Violence_GibSplash(self, 1, 1, attacker);
328                 self.modelindex = 0; // restore later
329                 self.solid = SOLID_NOT; // restore later
330         }
331 }
332
333 void ClientKill_Now_TeamChange();
334
335 void PlayerDamage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
336 {
337         local float take, save, waves, sdelay;
338
339         if(!DEATH_ISSPECIAL(deathtype))
340         {
341                 damage *= sqrt(bound(1.0, self.cvar_cl_handicap, 100.0));
342                 if(self != attacker)
343                         damage /= sqrt(bound(1.0, attacker.cvar_cl_handicap, 100.0));
344         }
345
346         if(DEATH_ISWEAPON(deathtype, WEP_TUBA))
347         {
348                 // tuba causes blood to come out of the ears
349                 vector ear1, ear2;
350                 vector d;
351                 float f;
352                 ear1 = self.origin;
353                 ear1_z += 0.125 * self.view_ofs_z + 0.875 * self.maxs_z; // 7/8
354                 ear2 = ear1;
355                 makevectors(self.angles);
356                 ear1 += v_right * -10;
357                 ear2 += v_right * +10;
358                 d = inflictor.origin - self.origin;
359                 f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
360                 force = v_right * vlen(force);
361                 Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), self, attacker);
362                 Violence_GibSplash_At(ear2, force,      2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), self, attacker);
363                 if(f > 0)
364                 {
365                         hitloc = ear1;
366                         force = force * -1;
367                 }
368                 else
369                 {
370                         hitloc = ear2;
371                         // force is already good
372                 }
373         }
374         else
375                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
376
377         if(g_arena)
378         if(numspawned < 2)
379                 return;
380
381         if (!g_minstagib)
382         {
383                 save = bound(0, damage * cvar("g_balance_armor_blockpercent"), self.armorvalue);
384                 take = bound(0, damage - save, damage);
385         }
386         else
387         {
388                 save = 0;
389                 take = damage;
390         }
391
392         if(sound_allowed(MSG_BROADCAST, attacker))
393         {
394                 if (save > 10)
395                         sound (self, CHAN_PROJECTILE, "misc/armorimpact.wav", VOL_BASE, ATTN_NORM);
396                 else if (take > 30)
397                         sound (self, CHAN_PROJECTILE, "misc/bodyimpact2.wav", VOL_BASE, ATTN_NORM);
398                 else if (take > 10)
399                         sound (self, CHAN_PROJECTILE, "misc/bodyimpact1.wav", VOL_BASE, ATTN_NORM); // FIXME possibly remove them?
400         }
401
402         if (take > 50)
403                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
404         if (take > 100)
405                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
406
407         if (time > self.spawnshieldtime)
408         {
409                 if (!(self.flags & FL_GODMODE))
410                 {
411                         self.armorvalue = self.armorvalue - save;
412                         self.health = self.health - take;
413                         // pause regeneration for 5 seconds
414                         self.pauseregen_finished = max(self.pauseregen_finished, time + cvar("g_balance_pause_health_regen"));
415
416                         if (time > self.pain_finished)          //Don't switch pain sequences like crazy
417                         {
418                                 self.pain_finished = time + 0.5;        //Supajoe
419
420                                 if(sv_gentle < 1) {             
421                                         if(self.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
422                                         {
423                                                 if (random() > 0.5)
424                                                         setanim(self, self.anim_pain1, FALSE, TRUE, TRUE);
425                                                 else
426                                                         setanim(self, self.anim_pain2, FALSE, TRUE, TRUE);
427                                         }
428
429                                         if(sound_allowed(MSG_BROADCAST, attacker))
430                                         if(!DEATH_ISWEAPON(deathtype, WEP_LASER) || attacker != self || self.health < 2 * cvar("g_balance_laser_primary_damage") * cvar("g_balance_selfdamagepercent") + 1)
431                                         // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
432                                         {
433                                                 if(self.health > 75) // TODO make a "gentle" version?
434                                                         PlayerSound(playersound_pain100, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
435                                                 else if(self.health > 50)
436                                                         PlayerSound(playersound_pain75, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
437                                                 else if(self.health > 25)
438                                                         PlayerSound(playersound_pain50, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
439                                                 else if(self.health > 1)
440                                                         PlayerSound(playersound_pain25, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
441                                         }
442                                 }
443
444                                 // throw off bot aim temporarily
445                                 local float shake;
446                                 shake = damage * 5 / (bound(0,skill,100) + 1);
447                                 self.v_angle_x = self.v_angle_x + (random() * 2 - 1) * shake;
448                                 self.v_angle_y = self.v_angle_y + (random() * 2 - 1) * shake;
449                         }
450                 }
451                 else
452                         self.max_armorvalue += (save + take);
453         }
454         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
455         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
456         self.dmg_inflictor = inflictor;
457
458         if(attacker == self)
459         {
460                 // don't reset pushltime for self damage as it may be an attempt to
461                 // escape a lava pit or similar
462                 //self.pushltime = 0;
463         }
464         else if(attacker.classname == "player" || attacker.classname == "gib")
465         {
466                 self.pusher = attacker;
467                 self.pushltime = time + cvar("g_maxpushtime");
468         }
469         else if(time < self.pushltime)
470         {
471                 attacker = self.pusher;
472                 self.pushltime = max(self.pushltime, time + 0.6);
473         }
474         else
475                 self.pushltime = 0;
476
477         if (self.health < 1)
478         {
479                 float defer_ClientKill_Now_TeamChange;
480                 defer_ClientKill_Now_TeamChange = FALSE;
481
482                 if(sv_gentle < 1) // TODO make a "gentle" version?
483                 if(sound_allowed(MSG_BROADCAST, attacker))
484                 {
485                         if(deathtype == DEATH_DROWN)
486                                 PlayerSound(playersound_drown, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
487                         else
488                                 PlayerSound(playersound_death, CHAN_PAIN, VOICETYPE_PLAYERSOUND);
489                 }
490
491                 // get rid of kill indicator
492                 if(self.killindicator)
493                 {
494                         remove(self.killindicator);
495                         self.killindicator = world;
496                         if(self.killindicator_teamchange)
497                                 defer_ClientKill_Now_TeamChange = TRUE;
498
499                         if(self.classname == "body")
500                         if(deathtype == DEATH_KILL)
501                         {
502                                 // for the lemmings fans, a small harmless explosion
503                                 pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
504                         }
505                 }
506
507                 // become fully visible
508                 self.alpha = 1;
509                 // clear selected player display
510                 ClearSelectedPlayer();
511                 // throw a weapon
512                 SpawnThrownWeapon (self.origin + (self.mins + self.maxs) * 0.5, self.switchweapon);
513                 // print an obituary message
514                 Obituary (attacker, inflictor, self, deathtype);
515                 race_PreDie();
516                 DropAllRunes(self);
517                 if(self == attacker)
518                         kh_Key_DropAll(self, TRUE);
519                 else if(attacker.classname == "player" || attacker.classname == "gib")
520                         kh_Key_DropAll(self, FALSE);
521                 else
522                         kh_Key_DropAll(self, TRUE);
523                 if(self.flagcarried)
524                 {
525                         if(attacker.classname != "player" && attacker.classname != "gib")
526                                 DropFlag(self.flagcarried, self, attacker); // penalty for flag loss by suicide
527                         else if(attacker.team == self.team)
528                                 DropFlag(self.flagcarried, attacker, attacker); // penalty for flag loss by suicide/teamkill
529                         else
530                                 DropFlag(self.flagcarried, world, attacker);
531                 }
532                 if(self.ballcarried)
533                         DropBall(self.ballcarried, self.origin, self.velocity);
534                 Portal_ClearAllLater(self);
535                 // clear waypoints
536                 WaypointSprite_PlayerDead();
537                 // make the corpse upright (not tilted)
538                 self.angles_x = 0;
539                 self.angles_z = 0;
540                 // don't spin
541                 self.avelocity = '0 0 0';
542                 // view from the floor
543                 self.view_ofs = '0 0 -8';
544                 // toss the corpse
545                 self.movetype = MOVETYPE_TOSS;
546                 // shootable corpse
547                 self.solid = SOLID_CORPSE;
548                 // don't stick to the floor
549                 self.flags &~= FL_ONGROUND;
550                 // dying animation
551                 self.deadflag = DEAD_DYING;
552                 // when to allow respawn
553                 sdelay = 0;
554                 waves = 0;
555                 if(cvar("g_respawn_mapsettings"))
556                 {
557                         sdelay = cvar("g_respawn_mapsettings_delay");
558                         waves = cvar("g_respawn_mapsettings_waves");
559                 }
560                 if(!sdelay)
561                         sdelay = cvar(strcat("g_", GetGametype(), "_respawn_delay"));
562                 if(!sdelay)
563                         sdelay = cvar("g_respawn_delay");
564                 if(!waves)
565                         waves = cvar(strcat("g_", GetGametype(), "_respawn_waves"));
566                 if(!waves)
567                         waves = cvar("g_respawn_waves");
568                 if(waves)
569                         self.death_time = ceil((time + sdelay) / waves) * waves;
570                 else
571                         self.death_time = time + sdelay;
572                 if((sdelay + waves >= 5.0) && (self.death_time - time > 1.75))
573                         self.respawn_countdown = 10; // first number to count down from is 10
574                 else
575                         self.respawn_countdown = -1; // do not count down
576                 if (random() < 0.5)
577                 {
578                         setanim(self, self.anim_die1, FALSE, TRUE, TRUE);
579                         self.dead_frame = self.anim_dead1_x;
580                 }
581                 else
582                 {
583                         setanim(self, self.anim_die2, FALSE, TRUE, TRUE);
584                         self.dead_frame = self.anim_dead2_x;
585                 }
586                 // set damage function to corpse damage
587                 self.event_damage = PlayerCorpseDamage;
588                 // call the corpse damage function just in case it wants to gib
589                 self.event_damage(inflictor, attacker, 0, deathtype, hitloc, force);
590                 // set up to fade out later
591                 SUB_SetFade (self, time + 12 + random () * 4, 1);
592
593                 // remove laserdot
594                 if(self.weaponentity)
595                         if(self.weaponentity.lasertarget)
596                                 remove(self.weaponentity.lasertarget);
597
598                 if(clienttype(self) == CLIENTTYPE_REAL)
599                 {
600                         self.fixangle = TRUE;
601                         //msg_entity = self;
602                         //WriteByte (MSG_ONE, SVC_SETANGLE);
603                         //WriteAngle (MSG_ONE, self.v_angle_x);
604                         //WriteAngle (MSG_ONE, self.v_angle_y);
605                         //WriteAngle (MSG_ONE, 80);
606                 }
607
608                 if(g_arena)
609                         Spawnqueue_Unmark(self);
610
611                 if(defer_ClientKill_Now_TeamChange)
612                         ClientKill_Now_TeamChange();
613
614                 if(sv_gentle > 0) {
615                         // remove corpse
616                         PlayerCorpseDamage (inflictor, attacker, 100.0, deathtype, hitloc, force);
617                 }
618         }
619 }
620
621 float UpdateSelectedPlayer_countvalue(float v)
622 {
623         return max(0, (v - 1.0) / 0.5);
624 }
625
626 // returns: -2 if no hit, otherwise cos of the angle
627 // uses the global v_angle
628 float UpdateSelectedPlayer_canSee(entity p, float mincosangle, float maxdist)
629 {
630         vector so, d;
631         float c;
632
633         if(p == self)
634                 return -2;
635
636         if(p.deadflag)
637                 return -2;
638
639         so = self.origin + self.view_ofs;
640         d = p.origin - so;
641
642         // misaimed?
643         if(dist_point_line(d, '0 0 0', v_forward) > maxdist)
644                 return -2;
645
646         // now find the cos of the angle...
647         c = normalize(d) * v_forward;
648
649         if(c <= mincosangle)
650                 return -2;
651
652         // not visible in any way? forget it
653         if(!checkpvs(so, p))
654                 return -2;
655
656         traceline(so, p.origin, MOVE_NOMONSTERS, self);
657         if(trace_fraction < 1)
658                 return -2;
659
660         return c;
661 }
662
663 void ClearSelectedPlayer()
664 {
665         if(self.selected_player)
666         {
667                 centerprint_expire(self, CENTERPRIO_POINT);
668                 self.selected_player = world;
669                 self.selected_player_display_needs_update = FALSE;
670         }
671 }
672
673 void UpdateSelectedPlayer()
674 {
675         entity selected;
676         float selected_score;
677         selected = world;
678         selected_score = 0.95; // 18 degrees
679
680         if(!cvar("sv_allow_shownames"))
681                 return;
682
683         if(clienttype(self) != CLIENTTYPE_REAL)
684                 return;
685
686         if(self.cvar_cl_shownames == 0)
687                 return;
688
689         if(self.cvar_cl_shownames == 1 && !teams_matter)
690                 return;
691
692         makevectors(self.v_angle); // sets v_forward
693
694         // 1. cursor trace is always right
695         if(self.cursor_trace_ent && self.cursor_trace_ent.classname == "player" && !self.cursor_trace_ent.deadflag)
696         {
697                 selected = self.cursor_trace_ent;
698         }
699         else
700         {
701                 // 2. if we don't have a cursor trace, find the player which is least
702                 //    mis-aimed at
703                 entity p;
704                 FOR_EACH_PLAYER(p)
705                 {
706                         float c;
707                         c = UpdateSelectedPlayer_canSee(p, selected_score, 100); // 100 = 2.5 meters
708                         if(c >= -1)
709                         {
710                                 selected = p;
711                                 selected_score = c;
712                         }
713                 }
714         }
715
716         if(selected)
717         {
718                 self.selected_player_display_timeout = time + self.cvar_scr_centertime;
719         }
720         else
721         {
722                 if(time < self.selected_player_display_timeout)
723                         if(UpdateSelectedPlayer_canSee(self.selected_player, 0.7, 200) >= -1) // 5 meters, 45 degrees
724                                 selected = self.selected_player;
725         }
726
727         if(selected)
728         {
729                 if(selected == self.selected_player)
730                 {
731                         float save;
732                         save = UpdateSelectedPlayer_countvalue(self.selected_player_count);
733                         self.selected_player_count = self.selected_player_count + frametime;
734                         if(save != UpdateSelectedPlayer_countvalue(self.selected_player_count))
735                         {
736                                 string namestr, healthstr;
737                                 namestr = playername(selected);
738                                 if(teams_matter)
739                                 {
740                                         healthstr = ftos(floor(selected.health));
741                                         if(self.team == selected.team)
742                                         {
743                                                 namestr = strcat(namestr, " (", healthstr, "%)");
744                                                 self.selected_player_display_needs_update = TRUE;
745                                         }
746                                 }
747                                 centerprint_atprio(self, CENTERPRIO_POINT, namestr);
748                         }
749                 }
750                 else
751                 {
752                         ClearSelectedPlayer();
753                         self.selected_player = selected;
754                         self.selected_player_time = time;
755                         self.selected_player_count = 0;
756                         self.selected_player_display_needs_update = FALSE;
757                 }
758         }
759         else
760         {
761                 ClearSelectedPlayer();
762         }
763
764         if(self.selected_player)
765                 self.last_selected_player = self.selected_player;
766 }
767
768 .float muted; // to be used by prvm_edictset server playernumber muted 1
769 void Say(entity source, float teamsay, entity privatesay, string msgin, float floodcontrol)
770 {
771         string msgstr, colorstr, cmsgstr, namestr, fullmsgstr, sourcemsgstr, fullcmsgstr, sourcecmsgstr, privatemsgprefix;
772         float flood, privatemsgprefixlen;
773         entity head;
774
775         if(Ban_MaybeEnforceBan(source))
776                 return;
777         
778         if(!teamsay && !privatesay)
779                 if(substring(msgin, 0, 1) == " ")
780                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
781
782         msgin = formatmessage(msgin);
783
784         if(msgin == "")
785                 return;
786
787         if(source.classname != "player")
788                 colorstr = "^0"; // black for spectators
789         else if(teams_matter)
790                 colorstr = Team_ColorCode(source.team);
791         else
792                 teamsay = FALSE;
793
794         if(intermission_running)
795                 teamsay = FALSE;
796
797         /*
798          * using bprint solves this... me stupid
799         // how can we prevent the message from appearing in a listen server?
800         // for now, just give "say" back and only handle say_team
801         if(!teamsay)
802         {
803                 clientcommand(self, strcat("say ", msgin));
804                 return;
805         }
806         */
807
808         if(cvar("g_chat_teamcolors"))
809                 namestr = playername(source);
810         else
811                 namestr = source.netname;
812
813         if(privatesay)
814         {
815                 msgstr = strcat("\{1}\{13}* ^3", namestr, "^3 tells you: ^7");
816                 privatemsgprefixlen = strlen(msgstr);
817                 msgstr = strcat(msgstr, msgin);
818                 cmsgstr = strcat(colorstr, "^3", namestr, "^3 tells you:\n^7", msgin);
819                 if(cvar("g_chat_teamcolors"))
820                         privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", playername(privatesay), ": ^7");
821                 else
822                         privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", privatesay.netname, ": ^7");
823         }
824         else if(teamsay)
825         {
826                 msgstr = strcat("\{1}\{13}", colorstr, "(^3", namestr, colorstr, ") ^7", msgin);
827                 cmsgstr = strcat(colorstr, "(^3", namestr, colorstr, ")\n^7", msgin);
828         }
829         else
830         {
831                 msgstr = strcat("\{1}", namestr, "^7: ", msgin);
832                 cmsgstr = "";
833         }
834         
835         msgstr = strcat(strreplace("\n", " ", msgstr), "\n"); // newlines only are good for centerprint
836         fullmsgstr = msgstr;
837         fullcmsgstr = cmsgstr;
838
839         // FLOOD CONTROL
840         flood = 0;
841         if(floodcontrol)
842         {
843                 float flood_spl;
844                 float flood_burst;
845                 float flood_lmax;
846                 var .float flood_field;
847                 float lines;
848                 if(privatesay)
849                 {
850                         flood_spl = cvar("g_chat_flood_spl_tell");
851                         flood_burst = cvar("g_chat_flood_burst_tell");
852                         flood_lmax = cvar("g_chat_flood_lmax_tell");
853                         flood_field = floodcontrol_chattell;
854                 }
855                 else if(teamsay)
856                 {
857                         flood_spl = cvar("g_chat_flood_spl_team");
858                         flood_burst = cvar("g_chat_flood_burst_team");
859                         flood_lmax = cvar("g_chat_flood_lmax_team");
860                         flood_field = floodcontrol_chatteam;
861                 }
862                 else
863                 {
864                         flood_spl = cvar("g_chat_flood_spl");
865                         flood_burst = cvar("g_chat_flood_burst");
866                         flood_lmax = cvar("g_chat_flood_lmax");
867                         flood_field = floodcontrol_chat;
868                 }
869                 flood_burst = max(0, flood_burst - 1);
870                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
871
872                 // do flood control for the default line size
873                 getWrappedLine_remaining = msgstr;
874                 msgstr = "";
875                 lines = 0;
876                 while(getWrappedLine_remaining && (!flood_lmax || lines <= flood_lmax))
877                 {
878                         msgstr = strcat(msgstr, " ", getWrappedLine(82.4289758859709, strlennocol)); // perl averagewidth.pl < gfx/vera-sans.width
879                         ++lines;
880                 }
881                 msgstr = substring(msgstr, 1, strlen(msgstr) - 1);
882
883                 if(getWrappedLine_remaining != "")
884                 {
885                         msgstr = strcat(msgstr, "\n");
886                         flood = 2;
887                 }
888
889                 if(time >= source.flood_field)
890                 {
891                         source.flood_field = max(time - flood_burst * flood_spl, source.flood_field) + lines * flood_spl;
892                 }
893                 else
894                 {
895                         flood = 1;
896                         msgstr = fullmsgstr;
897                 }
898         }
899         
900         if (timeoutStatus == 2) //when game is paused, no flood protection
901                 source.flood_field = flood = 0;
902
903         if(flood == 2)
904         {
905                 if(cvar("g_chat_flood_notify_flooder"))
906                 {
907                         sourcemsgstr = strcat(msgstr, "\n^3FLOOD CONTROL: ^7message too long, trimmed\n");
908                         sourcecmsgstr = "";
909                 }
910                 else
911                 {
912                         sourcemsgstr = fullmsgstr;
913                         sourcecmsgstr = fullcmsgstr;
914                 }
915                 cmsgstr = "";
916         }
917         else
918         {
919                 sourcemsgstr = msgstr;
920                 sourcecmsgstr = cmsgstr;
921         }
922
923         if(!privatesay)
924         if(source.classname != "player")
925         {
926                 if(teamsay || (cvar("g_chat_nospectators") == 1) || (cvar("g_chat_nospectators") == 2 && !inWarmupStage))
927                         teamsay = -1; // spectators
928         }
929
930         if(flood)
931                 print("NOTE: ", playername(source), "^7 is flooding.\n");
932         
933         // build sourcemsgstr by cutting off a prefix and replacing it by the other one
934         if(privatesay)
935                 sourcemsgstr = strcat(privatemsgprefix, substring(sourcemsgstr, privatemsgprefixlen, -1));
936
937         if(source.muted)
938         {
939                 // always fake the message
940                 sprint(source, sourcemsgstr);
941                 if(cmsgstr != "" && !privatesay)
942                         centerprint(source, sourcecmsgstr);
943         }
944         else if(flood == 1)
945         {
946                 if(cvar("g_chat_flood_notify_flooder"))
947                         sprint(source, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(source.flood_field - time), "^3 seconds\n"));
948                 else
949                 {
950                         sprint(source, sourcemsgstr);
951                         if(cmsgstr != "" && !privatesay)
952                                 centerprint(source, sourcecmsgstr);
953                 }
954         }
955         else if(privatesay)
956         {
957                 sprint(source, sourcemsgstr);
958                 sprint(privatesay, msgstr);
959                 if(cmsgstr != "")
960                         centerprint(privatesay, cmsgstr);
961         }
962         else if(teamsay > 0)
963         {
964                 sprint(source, sourcemsgstr);
965                 if(sourcecmsgstr != "")
966                         centerprint(source, sourcecmsgstr);
967                 FOR_EACH_REALPLAYER(head) if(head.team == source.team)
968                         if(head != source)
969                         {
970                                 sprint(head, msgstr);
971                                 if(cmsgstr != "")
972                                         centerprint(head, cmsgstr);
973                         }
974         }
975         else if(teamsay < 0)
976         {
977                 sprint(source, sourcemsgstr);
978                 FOR_EACH_REALCLIENT(head) if(head.classname != "player")
979                         if(head != source)
980                                 sprint(head, msgstr);
981         }
982         else if(sourcemsgstr != msgstr)
983         {
984                 sprint(source, sourcemsgstr);
985                 FOR_EACH_REALCLIENT(head)
986                         if(head != source)
987                                 sprint(head, msgstr);
988         }
989         else
990                 bprint(msgstr);
991 }
992
993 float GetVoiceMessageVoiceType(string type)
994 {
995         if(type == "taunt")
996                 return VOICETYPE_TAUNT;
997         if(type == "teamshoot")
998                 return VOICETYPE_LASTATTACKER;
999         return VOICETYPE_TEAMRADIO;
1000 }
1001
1002 string allvoicesamples;
1003 float GetPlayerSoundSampleField_notFound;
1004 float GetPlayerSoundSampleField_fixed;
1005 .string GetVoiceMessageSampleField(string type)
1006 {
1007         GetPlayerSoundSampleField_notFound = 0;
1008         GetPlayerSoundSampleField_fixed = 0;
1009         switch(type)
1010         {
1011 #define _VOICEMSG(m) case #m: return playersound_##m;
1012                 ALLVOICEMSGS
1013 #undef _VOICEMSG
1014         }
1015         GetPlayerSoundSampleField_notFound = 1;
1016         return playersound_taunt;
1017 }
1018
1019 .string GetPlayerSoundSampleField(string type)
1020 {
1021         GetPlayerSoundSampleField_notFound = 0;
1022         GetPlayerSoundSampleField_fixed = 0;
1023         switch(type)
1024         {
1025 #define _VOICEMSG(m) case #m: return playersound_##m;
1026                 ALLPLAYERSOUNDS
1027 #undef _VOICEMSG
1028         }
1029         GetPlayerSoundSampleField_notFound = 1;
1030         return playersound_taunt;
1031 }
1032
1033 void PrecacheGlobalSound(string samplestring)
1034 {
1035         float n, i;
1036         tokenize_console(samplestring);
1037         n = stof(argv(1));
1038         if(n > 0)
1039         {
1040                 for(i = 1; i <= n; ++i)
1041                         precache_sound(strcat(argv(0), ftos(i), ".wav"));
1042         }
1043         else
1044         {
1045                 precache_sound(strcat(argv(0), ".wav"));
1046         }
1047 }
1048
1049 void PrecachePlayerSounds(string f)
1050 {
1051         float fh;
1052         string s;
1053         fh = fopen(f, FILE_READ);
1054         if(fh < 0)
1055                 return;
1056         while((s = fgets(fh)))
1057         {
1058                 if(tokenize_console(s) != 3)
1059                 {
1060                         dprint("Invalid sound info line: ", s, "\n");
1061                         continue;
1062                 }
1063                 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
1064         }
1065         fclose(fh);
1066
1067         if not(allvoicesamples)
1068         {
1069 #define _VOICEMSG(m) allvoicesamples = strcat(allvoicesamples, " ", #m);
1070                 ALLVOICEMSGS
1071 #undef _VOICEMSG
1072                 allvoicesamples = strzone(substring(allvoicesamples, 1, strlen(allvoicesamples) - 1));
1073         }
1074 }
1075
1076 void ClearPlayerSounds()
1077 {
1078 #define _VOICEMSG(m) if(self.playersound_##m) { strunzone(self.playersound_##m); self.playersound_##m = string_null; }
1079         ALLPLAYERSOUNDS
1080         ALLVOICEMSGS
1081 #undef _VOICEMSG
1082 }
1083
1084 void LoadPlayerSounds(string f, float first)
1085 {
1086         float fh;
1087         string s;
1088         var .string field;
1089         fh = fopen(f, FILE_READ);
1090         if(fh < 0)
1091                 return;
1092         while((s = fgets(fh)))
1093         {
1094                 if(tokenize_console(s) != 3)
1095                         continue;
1096                 field = GetPlayerSoundSampleField(argv(0));
1097                 if(GetPlayerSoundSampleField_notFound)
1098                         field = GetVoiceMessageSampleField(argv(0));
1099                 if(GetPlayerSoundSampleField_notFound)
1100                         continue;
1101                 if(GetPlayerSoundSampleField_fixed)
1102                         if not(first)
1103                                 continue;
1104                 if(self.field)
1105                         strunzone(self.field);
1106                 self.field = strzone(strcat(argv(1), " ", argv(2)));
1107         }
1108         fclose(fh);
1109 }
1110
1111 .float modelindex_for_playersound;
1112 void UpdatePlayerSounds()
1113 {
1114         if(self.modelindex == self.modelindex_for_playersound)
1115                 return;
1116         self.modelindex_for_playersound = self.modelindex;
1117         ClearPlayerSounds();
1118         LoadPlayerSounds("sound/player/default.sounds", 1);
1119         LoadPlayerSounds(strcat(self.model, ".sounds"), 0);
1120 }
1121
1122 void GlobalSound(string sample, float chan, float voicetype)
1123 {
1124         float n;
1125         float tauntrand;
1126
1127         if(sample == "")
1128                 return;
1129
1130         tokenize_console(sample);
1131         n = stof(argv(1));
1132         if(n > 0)
1133                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
1134         else
1135                 sample = strcat(argv(0), ".wav"); // randomization
1136         
1137         switch(voicetype)
1138         {
1139                 case VOICETYPE_LASTATTACKER_ONLY:
1140                         if(self.pusher)
1141                                 if(self.pusher.team == self.team)
1142                                 {
1143                                         msg_entity = self.pusher;
1144                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
1145                                         {
1146                                                 if(msg_entity.cvar_cl_voice_directional == 1)
1147                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
1148                                                 else
1149                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1150                                         }
1151                                 }
1152                         break;
1153                 case VOICETYPE_LASTATTACKER:
1154                         if(self.pusher)
1155                                 if(self.pusher.team == self.team)
1156                                 {
1157                                         msg_entity = self.pusher;
1158                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
1159                                         {
1160                                                 if(msg_entity.cvar_cl_voice_directional == 1)
1161                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
1162                                                 else
1163                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1164                                         }
1165                                         msg_entity = self;
1166                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
1167                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTN_NONE);
1168                                 }
1169                         break;
1170                 case VOICETYPE_TEAMRADIO:
1171                         FOR_EACH_REALCLIENT(msg_entity)
1172                                 if(!teams_matter || msg_entity.team == self.team)
1173                                 {
1174                                         if(msg_entity.cvar_cl_voice_directional == 1)
1175                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
1176                                         else
1177                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1178                                 }
1179                         break;
1180                 case VOICETYPE_AUTOTAUNT:
1181                         tauntrand = random();
1182                         FOR_EACH_REALCLIENT(msg_entity)
1183                                 if (tauntrand < msg_entity.cvar_cl_autotaunt)
1184                                 {
1185                                         if (msg_entity.cvar_cl_voice_directional >= 1)
1186                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTN_MAX));
1187                                         else
1188                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1189                                 }
1190                         break;
1191                 case VOICETYPE_TAUNT:
1192                         FOR_EACH_REALCLIENT(msg_entity)
1193                         {
1194                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1195                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTN_MAX));
1196                                 else
1197                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1198                         }
1199                 case VOICETYPE_PLAYERSOUND:
1200                         sound(self, chan, sample, VOL_BASE, ATTN_NORM);
1201                         break;
1202                 default:
1203                         backtrace("Invalid voice type!");
1204                         break;
1205         }
1206 }
1207
1208 void PlayerSound(.string samplefield, float chan, float voicetype)
1209 {
1210         string sample;
1211         sample = self.samplefield;
1212         GlobalSound(sample, chan, voicetype);
1213 }
1214
1215 void VoiceMessage(string type, string msg)
1216 {
1217         var .string sample;
1218         var float voicetype, ownteam;
1219         sample = GetVoiceMessageSampleField(type);
1220
1221         if(GetPlayerSoundSampleField_notFound)
1222         {
1223                 sprint(self, strcat("Invalid voice. Use one of: ", allvoicesamples, "\n"));
1224                 return;
1225         }
1226
1227         voicetype = GetVoiceMessageVoiceType(type);
1228         ownteam = (voicetype == VOICETYPE_TEAMRADIO);
1229
1230         float flood;
1231         float flood_spv;
1232         var .float flood_field;
1233
1234         flood = 0;
1235         if(ownteam)
1236         {
1237                 flood_spv = cvar("g_voice_flood_spv_team");
1238                 flood_field = floodcontrol_voiceteam;
1239         }
1240         else
1241         {
1242                 flood_spv = cvar("g_voice_flood_spv");
1243                 flood_field = floodcontrol_voice;
1244         }
1245
1246         if(time >= self.flood_field)
1247                 self.flood_field = max(time, self.flood_field) + flood_spv;
1248         else
1249                 flood = 1;
1250                 
1251         if (timeoutStatus == 2) //when game is paused, no flood protection
1252                 self.flood_field = flood = 0;
1253
1254         if (msg != "")
1255                 Say(self, ownteam, world, msg, 0);
1256
1257         if (!flood)
1258                 PlayerSound(sample, CHAN_VOICE, voicetype);
1259 }