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