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