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