]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_player.qc
fix a bug in flood control
[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 .float floodcontrol_chattell;
671 void Say(entity source, float teamsay, entity privatesay, string msgin, float floodcontrol)
672 {
673         string msgstr, colorstr, cmsgstr, namestr, fullmsgstr, sourcemsgstr, fullcmsgstr, sourcecmsgstr, privatemsgprefix;
674         float flood, privatemsgprefixlen;
675         entity head;
676
677         if(Ban_MaybeEnforceBan(source))
678                 return;
679         
680         if(!teamsay && !privatesay)
681                 if(substring(msgin, 0, 1) == " ")
682                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
683
684         msgin = formatmessage(msgin);
685
686         if(msgin == "")
687                 return;
688
689         if(source.classname != "player")
690                 colorstr = "^0"; // black for spectators
691         else if(teams_matter)
692                 colorstr = Team_ColorCode(source.team);
693         else
694                 teamsay = FALSE;
695
696         if(intermission_running)
697                 teamsay = FALSE;
698
699         /*
700          * using bprint solves this... me stupid
701         // how can we prevent the message from appearing in a listen server?
702         // for now, just give "say" back and only handle say_team
703         if(!teamsay)
704         {
705                 clientcommand(self, strcat("say ", msgin));
706                 return;
707         }
708         */
709
710         if(cvar("g_chat_teamcolors"))
711                 namestr = playername(source);
712         else
713                 namestr = source.netname;
714
715         if(privatesay)
716         {
717                 msgstr = strcat("\{1}\{13}* ^3", namestr, "^3 tells you: ^7");
718                 privatemsgprefixlen = strlen(msgstr);
719                 msgstr = strcat(msgstr, msgin);
720                 cmsgstr = strcat(colorstr, "^3", namestr, "^3 tells you:\n^7", msgin);
721                 if(cvar("g_chat_teamcolors"))
722                         privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", playername(privatesay), ": ^7");
723                 else
724                         privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", privatesay.netname, ": ^7");
725         }
726         else if(teamsay)
727         {
728                 msgstr = strcat("\{1}\{13}", colorstr, "(^3", namestr, colorstr, ") ^7", msgin);
729                 cmsgstr = strcat(colorstr, "(^3", namestr, colorstr, ")\n^7", msgin);
730         }
731         else
732         {
733                 msgstr = strcat("\{1}", namestr, "^7: ", msgin);
734                 cmsgstr = "";
735         }
736         
737         msgstr = strcat(strreplace("\n", " ", msgstr), "\n"); // newlines only are good for centerprint
738         fullmsgstr = msgstr;
739         fullcmsgstr = cmsgstr;
740
741         // FLOOD CONTROL
742         flood = 0;
743         if(floodcontrol)
744         {
745                 float flood_spl;
746                 float flood_burst;
747                 float flood_lmax;
748                 var .float flood_field;
749                 float lines;
750                 if(privatesay)
751                 {
752                         flood_spl = cvar("g_chat_flood_spl_tell");
753                         flood_burst = cvar("g_chat_flood_burst_tell");
754                         flood_lmax = cvar("g_chat_flood_lmax_tell");
755                         flood_field = floodcontrol_chattell;
756                 }
757                 else if(teamsay)
758                 {
759                         flood_spl = cvar("g_chat_flood_spl_team");
760                         flood_burst = cvar("g_chat_flood_burst_team");
761                         flood_lmax = cvar("g_chat_flood_lmax_team");
762                         flood_field = floodcontrol_chatteam;
763                 }
764                 else
765                 {
766                         flood_spl = cvar("g_chat_flood_spl");
767                         flood_burst = cvar("g_chat_flood_burst");
768                         flood_lmax = cvar("g_chat_flood_lmax");
769                         flood_field = floodcontrol_chat;
770                 }
771                 flood_burst = max(0, flood_burst - 1);
772                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
773
774                 // do flood control for the default line size
775                 getWrappedLine_remaining = msgstr;
776                 msgstr = "";
777                 lines = 0;
778                 while(getWrappedLine_remaining && (!flood_lmax || lines <= flood_lmax))
779                 {
780                         msgstr = strcat(msgstr, " ", getWrappedLine(82.4289758859709, strlennocol)); // perl averagewidth.pl < gfx/vera-sans.width
781                         ++lines;
782                 }
783                 msgstr = substring(msgstr, 1, strlen(msgstr) - 1);
784
785                 if(getWrappedLine_remaining != "")
786                 {
787                         msgstr = strcat(msgstr, "\n");
788                         flood = 2;
789                 }
790
791                 if(time >= source.flood_field)
792                 {
793                         source.flood_field = max(time - flood_burst * flood_spl, source.flood_field) + lines * flood_spl;
794                 }
795                 else
796                 {
797                         flood = 1;
798                         msgstr = fullmsgstr;
799                 }
800         }
801
802         if(flood == 2)
803         {
804                 if(cvar("g_chat_flood_notify_flooder"))
805                 {
806                         sourcemsgstr = strcat(msgstr, "\n^3FLOOD CONTROL: ^7message too long, trimmed\n");
807                         sourcecmsgstr = "";
808                 }
809                 else
810                 {
811                         sourcemsgstr = fullmsgstr;
812                         sourcecmsgstr = fullcmsgstr;
813                 }
814                 cmsgstr = "";
815         }
816         else
817         {
818                 sourcemsgstr = msgstr;
819                 sourcecmsgstr = cmsgstr;
820         }
821
822         if(!privatesay)
823         if(source.classname != "player")
824         {
825                 if(teamsay || (cvar("g_chat_nospectators") == 1) || (cvar("g_chat_nospectators") == 2 && !inWarmupStage))
826                         teamsay = -1; // spectators
827         }
828
829         if(flood)
830                 print("NOTE: ", playername(source), "^7 is flooding.\n");
831         
832         // build sourcemsgstr by cutting off a prefix and replacing it by the other one
833         if(privatesay)
834                 sourcemsgstr = strcat(privatemsgprefix, substring(sourcemsgstr, privatemsgprefixlen, -1));
835
836         if(flood == 1)
837         {
838                 if(cvar("g_chat_flood_notify_flooder"))
839                         sprint(source, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(source.flood_field - time), "^3 seconds\n"));
840                 else
841                         sprint(source, sourcemsgstr);
842         }
843         else if(privatesay)
844         {
845                 sprint(source, sourcemsgstr);
846                 sprint(privatesay, msgstr);
847                 if(cmsgstr != "")
848                         centerprint(privatesay, cmsgstr);
849         }
850         else if(teamsay > 0)
851         {
852                 sprint(source, sourcemsgstr);
853                 if(sourcecmsgstr != "")
854                         centerprint(source, sourcecmsgstr);
855                 FOR_EACH_REALPLAYER(head) if(head.team == source.team)
856                         if(head != source)
857                         {
858                                 sprint(head, msgstr);
859                                 if(cmsgstr != "")
860                                         centerprint(head, cmsgstr);
861                         }
862         }
863         else if(teamsay < 0)
864         {
865                 sprint(source, sourcemsgstr);
866                 FOR_EACH_REALCLIENT(head) if(head.classname != "player")
867                         if(head != source)
868                                 sprint(head, msgstr);
869         }
870         else if(sourcemsgstr != msgstr)
871         {
872                 sprint(source, sourcemsgstr);
873                 FOR_EACH_REALCLIENT(head)
874                         if(head != source)
875                                 sprint(head, msgstr);
876         }
877         else
878                 bprint(msgstr);
879 }
880
881 float GetVoiceMessageVoiceType(string type)
882 {
883         if(type == "taunt")
884                 return VOICETYPE_TAUNT;
885         if(type == "teamshoot")
886                 return VOICETYPE_LASTATTACKER;
887         return VOICETYPE_TEAMRADIO;
888 }
889
890 string allvoicesamples;
891 float GetPlayerSoundSampleField_notFound;
892 float GetPlayerSoundSampleField_fixed;
893 .string GetVoiceMessageSampleField(string type)
894 {
895         GetPlayerSoundSampleField_notFound = 0;
896         GetPlayerSoundSampleField_fixed = 0;
897         switch(type)
898         {
899 #define _VOICEMSG(m) case #m: return playersound_##m;
900                 ALLVOICEMSGS
901 #undef _VOICEMSG
902         }
903         GetPlayerSoundSampleField_notFound = 1;
904         return playersound_taunt;
905 }
906
907 .string GetPlayerSoundSampleField(string type)
908 {
909         GetPlayerSoundSampleField_notFound = 0;
910         GetPlayerSoundSampleField_fixed = 0;
911         switch(type)
912         {
913 #define _VOICEMSG(m) case #m: return playersound_##m;
914                 ALLPLAYERSOUNDS
915 #undef _VOICEMSG
916         }
917         GetPlayerSoundSampleField_notFound = 1;
918         return playersound_taunt;
919 }
920
921 void PrecacheGlobalSound(string samplestring)
922 {
923         float n, i;
924         tokenize_console(samplestring);
925         n = stof(argv(1));
926         if(n > 0)
927         {
928                 for(i = 1; i <= n; ++i)
929                         precache_sound(strcat(argv(0), ftos(i), ".wav"));
930         }
931         else
932         {
933                 precache_sound(strcat(argv(0), ".wav"));
934         }
935 }
936
937 void PrecachePlayerSounds(string f)
938 {
939         float fh;
940         string s;
941         fh = fopen(f, FILE_READ);
942         if(fh < 0)
943                 return;
944         while((s = fgets(fh)))
945         {
946                 if(tokenize_console(s) != 3)
947                 {
948                         dprint("Invalid sound info line: ", s, "\n");
949                         continue;
950                 }
951                 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
952         }
953         fclose(fh);
954
955         if not(allvoicesamples)
956         {
957 #define _VOICEMSG(m) allvoicesamples = strcat(allvoicesamples, " ", #m);
958                 ALLVOICEMSGS
959 #undef _VOICEMSG
960                 allvoicesamples = strzone(substring(allvoicesamples, 1, strlen(allvoicesamples) - 1));
961         }
962 }
963
964 void ClearPlayerSounds()
965 {
966 #define _VOICEMSG(m) if(self.playersound_##m) { strunzone(self.playersound_##m); self.playersound_##m = string_null; }
967         ALLPLAYERSOUNDS
968         ALLVOICEMSGS
969 #undef _VOICEMSG
970 }
971
972 void LoadPlayerSounds(string f, float first)
973 {
974         float fh;
975         string s;
976         var .string field;
977         fh = fopen(f, FILE_READ);
978         if(fh < 0)
979                 return;
980         while((s = fgets(fh)))
981         {
982                 if(tokenize_console(s) != 3)
983                         continue;
984                 field = GetPlayerSoundSampleField(argv(0));
985                 if(GetPlayerSoundSampleField_notFound)
986                         field = GetVoiceMessageSampleField(argv(0));
987                 if(GetPlayerSoundSampleField_notFound)
988                         continue;
989                 if(GetPlayerSoundSampleField_fixed)
990                         if not(first)
991                                 continue;
992                 if(self.field)
993                         strunzone(self.field);
994                 self.field = strzone(strcat(argv(1), " ", argv(2)));
995         }
996         fclose(fh);
997 }
998
999 .float modelindex_for_playersound;
1000 void UpdatePlayerSounds()
1001 {
1002         if(self.modelindex == self.modelindex_for_playersound)
1003                 return;
1004         self.modelindex_for_playersound = self.modelindex;
1005         ClearPlayerSounds();
1006         LoadPlayerSounds("sound/player/default.sounds", 1);
1007         LoadPlayerSounds(strcat(self.model, ".sounds"), 0);
1008 }
1009
1010 void GlobalSound(string sample, float chan, float voicetype)
1011 {
1012         float n;
1013         float tauntrand;
1014
1015         if(sample == "")
1016                 return;
1017
1018         tokenize_console(sample);
1019         n = stof(argv(1));
1020         if(n > 0)
1021                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
1022         else
1023                 sample = strcat(argv(0), ".wav"); // randomization
1024         
1025         switch(voicetype)
1026         {
1027                 case VOICETYPE_LASTATTACKER_ONLY:
1028                         if(self.pusher)
1029                                 if(self.pusher.team == self.team)
1030                                 {
1031                                         msg_entity = self.pusher;
1032                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
1033                                         {
1034                                                 if(msg_entity.cvar_cl_voice_directional == 1)
1035                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
1036                                                 else
1037                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1038                                         }
1039                                 }
1040                         break;
1041                 case VOICETYPE_LASTATTACKER:
1042                         if(self.pusher)
1043                                 if(self.pusher.team == self.team)
1044                                 {
1045                                         msg_entity = self.pusher;
1046                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
1047                                         {
1048                                                 if(msg_entity.cvar_cl_voice_directional == 1)
1049                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
1050                                                 else
1051                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1052                                         }
1053                                         msg_entity = self;
1054                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
1055                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTN_NONE);
1056                                 }
1057                         break;
1058                 case VOICETYPE_TEAMRADIO:
1059                         FOR_EACH_REALCLIENT(msg_entity)
1060                                 if(!teams_matter || msg_entity.team == self.team)
1061                                 {
1062                                         if(msg_entity.cvar_cl_voice_directional == 1)
1063                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
1064                                         else
1065                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1066                                 }
1067                         break;
1068                 case VOICETYPE_AUTOTAUNT:
1069                         tauntrand = random();
1070                         FOR_EACH_REALCLIENT(msg_entity)
1071                                 if (tauntrand < msg_entity.cvar_cl_autotaunt)
1072                                 {
1073                                         if (msg_entity.cvar_cl_voice_directional >= 1)
1074                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTN_MAX));
1075                                         else
1076                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1077                                 }
1078                         break;
1079                 case VOICETYPE_TAUNT:
1080                         FOR_EACH_REALCLIENT(msg_entity)
1081                         {
1082                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1083                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTN_MAX));
1084                                 else
1085                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1086                         }
1087                 case VOICETYPE_PLAYERSOUND:
1088                         sound(self, chan, sample, VOL_BASE, ATTN_NORM);
1089                         break;
1090                 default:
1091                         backtrace("Invalid voice type!");
1092                         break;
1093         }
1094 }
1095
1096 void PlayerSound(.string samplefield, float chan, float voicetype)
1097 {
1098         string sample;
1099         sample = self.samplefield;
1100         GlobalSound(sample, chan, voicetype);
1101 }
1102
1103 .float floodcontrol_voice;
1104 .float floodcontrol_voiceteam;
1105 void VoiceMessage(string type, string msg)
1106 {
1107         var .string sample;
1108         var float voicetype, ownteam;
1109         sample = GetVoiceMessageSampleField(type);
1110
1111         if(GetPlayerSoundSampleField_notFound)
1112         {
1113                 sprint(self, strcat("Invalid voice. Use one of: ", allvoicesamples, "\n"));
1114                 return;
1115         }
1116
1117         voicetype = GetVoiceMessageVoiceType(type);
1118         ownteam = (voicetype == VOICETYPE_TEAMRADIO);
1119
1120         float flood;
1121         float flood_spv;
1122         var .float flood_field;
1123
1124         flood = 0;
1125         if(ownteam)
1126         {
1127                 flood_spv = cvar("g_voice_flood_spv_team");
1128                 flood_field = floodcontrol_voiceteam;
1129         }
1130         else
1131         {
1132                 flood_spv = cvar("g_voice_flood_spv");
1133                 flood_field = floodcontrol_voice;
1134         }
1135
1136         if(time >= self.flood_field)
1137                 self.flood_field = max(time, self.flood_field) + flood_spv;
1138         else
1139                 flood = 1;
1140
1141         if (msg != "")
1142                 Say(self, ownteam, world, msg, 0);
1143
1144         if (!flood)
1145                 PlayerSound(sample, CHAN_VOICE, voicetype);
1146 }