]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/cl_player.qc
prevent bugs with dragging a player while he dies
[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
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                 Portal_ClearAllLater(self);
433                 // clear waypoints
434                 WaypointSprite_PlayerDead();
435                 // make the corpse upright (not tilted)
436                 self.angles_x = 0;
437                 self.angles_z = 0;
438                 // don't spin
439                 self.avelocity = '0 0 0';
440                 // view from the floor
441                 self.view_ofs = '0 0 -8';
442                 // toss the corpse
443                 self.movetype = MOVETYPE_TOSS;
444                 // shootable corpse
445                 self.solid = SOLID_CORPSE;
446                 // don't stick to the floor
447                 self.flags &~= FL_ONGROUND;
448                 // dying animation
449                 self.deadflag = DEAD_DYING;
450                 // when to allow respawn
451                 sdelay = 0;
452                 waves = 0;
453                 if(cvar("g_respawn_mapsettings"))
454                 {
455                         sdelay = cvar("g_respawn_mapsettings_delay");
456                         waves = cvar("g_respawn_mapsettings_waves");
457                 }
458                 if(!sdelay)
459                         sdelay = cvar(strcat("g_", GetGametype(), "_respawn_delay"));
460                 if(!sdelay)
461                         sdelay = cvar("g_respawn_delay");
462                 if(!waves)
463                         waves = cvar(strcat("g_", GetGametype(), "_respawn_waves"));
464                 if(!waves)
465                         waves = cvar("g_respawn_waves");
466                 if(waves)
467                         self.death_time = ceil((time + sdelay) / waves) * waves;
468                 else
469                         self.death_time = time + sdelay;
470                 if((sdelay + waves >= 5.0) && (self.death_time - time > 1.75))
471                         self.respawn_countdown = 10; // first number to count down from is 10
472                 else
473                         self.respawn_countdown = -1; // do not count down
474                 if (random() < 0.5)
475                 {
476                         setanim(self, self.anim_die1, FALSE, TRUE, TRUE);
477                         self.dead_frame = self.anim_dead1_x;
478                 }
479                 else
480                 {
481                         setanim(self, self.anim_die2, FALSE, TRUE, TRUE);
482                         self.dead_frame = self.anim_dead2_x;
483                 }
484                 // set damage function to corpse damage
485                 self.event_damage = PlayerCorpseDamage;
486                 // call the corpse damage function just in case it wants to gib
487                 self.event_damage(inflictor, attacker, 0, deathtype, hitloc, force);
488                 // set up to fade out later
489                 SUB_SetFade (self, time + 12 + random () * 4, 1);
490
491                 // remove laserdot
492                 if(self.weaponentity)
493                         if(self.weaponentity.lasertarget)
494                                 remove(self.weaponentity.lasertarget);
495
496                 if(clienttype(self) == CLIENTTYPE_REAL)
497                 {
498                         self.fixangle = TRUE;
499                         //msg_entity = self;
500                         //WriteByte (MSG_ONE, SVC_SETANGLE);
501                         //WriteAngle (MSG_ONE, self.v_angle_x);
502                         //WriteAngle (MSG_ONE, self.v_angle_y);
503                         //WriteAngle (MSG_ONE, 80);
504                 }
505
506                 if(g_arena)
507                         Spawnqueue_Unmark(self);
508
509                 if(defer_ClientKill_Now_TeamChange)
510                         ClientKill_Now_TeamChange();
511
512                 if(sv_gentle > 0) {
513                         // remove corpse
514                         PlayerCorpseDamage (inflictor, attacker, 100.0, deathtype, hitloc, force);
515                 }
516         }
517 }
518
519 float UpdateSelectedPlayer_countvalue(float v)
520 {
521         return max(0, (v - 1.0) / 0.5);
522 }
523
524 // returns: -2 if no hit, otherwise cos of the angle
525 // uses the global v_angle
526 float UpdateSelectedPlayer_canSee(entity p, float mincosangle, float maxdist)
527 {
528         vector so, d;
529         float c;
530
531         if(p == self)
532                 return -2;
533
534         if(p.deadflag)
535                 return -2;
536
537         so = self.origin + self.view_ofs;
538         d = p.origin - so;
539
540         // misaimed?
541         if(dist_point_line(d, '0 0 0', v_forward) > maxdist)
542                 return -2;
543
544         // now find the cos of the angle...
545         c = normalize(d) * v_forward;
546
547         if(c <= mincosangle)
548                 return -2;
549
550         // not visible in any way? forget it
551         if(!checkpvs(so, p))
552                 return -2;
553
554         traceline(so, p.origin, MOVE_NOMONSTERS, self);
555         if(trace_fraction < 1)
556                 return -2;
557
558         return c;
559 }
560
561 void ClearSelectedPlayer()
562 {
563         if(self.selected_player)
564         {
565                 centerprint_expire(self, CENTERPRIO_POINT);
566                 self.selected_player = world;
567                 self.selected_player_display_needs_update = FALSE;
568         }
569 }
570
571 void UpdateSelectedPlayer()
572 {
573         entity selected;
574         float selected_score;
575         selected = world;
576         selected_score = 0.95; // 18 degrees
577
578         if(!cvar("sv_allow_shownames"))
579                 return;
580
581         if(clienttype(self) != CLIENTTYPE_REAL)
582                 return;
583
584         if(self.cvar_cl_shownames == 0)
585                 return;
586
587         if(self.cvar_cl_shownames == 1 && !teams_matter)
588                 return;
589
590         makevectors(self.v_angle); // sets v_forward
591
592         // 1. cursor trace is always right
593         if(self.cursor_trace_ent && self.cursor_trace_ent.classname == "player" && !self.cursor_trace_ent.deadflag)
594         {
595                 selected = self.cursor_trace_ent;
596         }
597         else
598         {
599                 // 2. if we don't have a cursor trace, find the player which is least
600                 //    mis-aimed at
601                 entity p;
602                 FOR_EACH_PLAYER(p)
603                 {
604                         float c;
605                         c = UpdateSelectedPlayer_canSee(p, selected_score, 100); // 100 = 2.5 meters
606                         if(c >= -1)
607                         {
608                                 selected = p;
609                                 selected_score = c;
610                         }
611                 }
612         }
613
614         if(selected)
615         {
616                 self.selected_player_display_timeout = time + self.cvar_scr_centertime;
617         }
618         else
619         {
620                 if(time < self.selected_player_display_timeout)
621                         if(UpdateSelectedPlayer_canSee(self.selected_player, 0.7, 200) >= -1) // 5 meters, 45 degrees
622                                 selected = self.selected_player;
623         }
624
625         if(selected)
626         {
627                 if(selected == self.selected_player)
628                 {
629                         float save;
630                         save = UpdateSelectedPlayer_countvalue(self.selected_player_count);
631                         self.selected_player_count = self.selected_player_count + frametime;
632                         if(save != UpdateSelectedPlayer_countvalue(self.selected_player_count))
633                         {
634                                 string namestr, healthstr;
635                                 namestr = playername(selected);
636                                 if(teams_matter)
637                                 {
638                                         healthstr = ftos(floor(selected.health));
639                                         if(self.team == selected.team)
640                                         {
641                                                 namestr = strcat(namestr, " (", healthstr, "%)");
642                                                 self.selected_player_display_needs_update = TRUE;
643                                         }
644                                 }
645                                 centerprint_atprio(self, CENTERPRIO_POINT, namestr);
646                         }
647                 }
648                 else
649                 {
650                         ClearSelectedPlayer();
651                         self.selected_player = selected;
652                         self.selected_player_time = time;
653                         self.selected_player_count = 0;
654                         self.selected_player_display_needs_update = FALSE;
655                 }
656         }
657         else
658         {
659                 ClearSelectedPlayer();
660         }
661
662         if(self.selected_player)
663                 self.last_selected_player = self.selected_player;
664 }
665
666 .float floodcontrol_chat;
667 .float floodcontrol_chatteam;
668 void Say(entity source, float teamsay, string msgin, float floodcontrol)
669 {
670         string msgstr, colorstr, cmsgstr, namestr, fullmsgstr, sourcemsgstr, fullcmsgstr, sourcecmsgstr;
671         float flood;
672         entity head;
673
674         if(Ban_MaybeEnforceBan(source))
675                 return;
676
677         if(!teamsay)
678                 if(substring(msgin, 0, 1) == " ")
679                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
680
681         msgin = formatmessage(msgin);
682
683         if(msgin == "")
684                 return;
685
686         if(source.classname != "player")
687                 colorstr = "^0"; // black for spectators
688         else if(teams_matter)
689                 colorstr = Team_ColorCode(source.team);
690         else
691                 teamsay = FALSE;
692
693         if(intermission_running)
694                 teamsay = FALSE;
695
696         /*
697          * using bprint solves this... me stupid
698         // how can we prevent the message from appearing in a listen server?
699         // for now, just give "say" back and only handle say_team
700         if(!teamsay)
701         {
702                 clientcommand(self, strcat("say ", msgin));
703                 return;
704         }
705         */
706
707         if(cvar("g_chat_teamcolors"))
708                 namestr = playername(source);
709         else
710                 namestr = source.netname;
711         if(teamsay)
712         {
713                 msgstr = strcat("\{1}\{13}", colorstr, "(^3", namestr, colorstr, ") ^7", msgin);
714                 cmsgstr = strcat(colorstr, "(^3", namestr, colorstr, ")\n^7", msgin);
715         }
716         else
717         {
718                 msgstr = strcat("\{1}", namestr, "^7: ", msgin);
719                 cmsgstr = "";
720         }
721         
722         msgstr = strcat(strreplace("\n", " ", msgstr), "\n"); // newlines only are good for centerprint
723         fullmsgstr = msgstr;
724         fullcmsgstr = cmsgstr;
725
726         // FLOOD CONTROL
727         flood = 0;
728         if(floodcontrol)
729         {
730                 float flood_spl;
731                 float flood_burst;
732                 float flood_lmax;
733                 var .float flood_field;
734                 float lines;
735                 if(teamsay)
736                 {
737                         flood_spl = cvar("g_chat_flood_spl_team");
738                         flood_burst = cvar("g_chat_flood_burst_team");
739                         flood_lmax = cvar("g_chat_flood_lmax_team");
740                         flood_field = floodcontrol_chatteam;
741                 }
742                 else
743                 {
744                         flood_spl = cvar("g_chat_flood_spl");
745                         flood_burst = cvar("g_chat_flood_burst");
746                         flood_lmax = cvar("g_chat_flood_lmax");
747                         flood_field = floodcontrol_chat;
748                 }
749                 flood_burst = max(0, flood_burst - 1);
750                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
751
752                 // do flood control for the default line size
753                 getWrappedLine_remaining = msgstr;
754                 msgstr = "";
755                 lines = 0;
756                 while(getWrappedLine_remaining && (!flood_lmax || lines <= flood_lmax))
757                 {
758                         msgstr = strcat(msgstr, " ", getWrappedLine(82.4289758859709, strlennocol)); // perl averagewidth.pl < gfx/vera-sans.width
759                         ++lines;
760                 }
761                 msgstr = substring(msgstr, 1, strlen(msgstr) - 1);
762
763                 if(getWrappedLine_remaining != "")
764                         flood = 2;
765
766                 if(time >= self.flood_field)
767                         self.flood_field = max(time - flood_burst * flood_spl, self.flood_field) + lines * flood_spl;
768                 else
769                         flood = 1;
770         }
771
772         if(flood == 2)
773         {
774                 if(cvar("g_chat_flood_notify_flooder"))
775                 {
776                         sourcemsgstr = strcat(msgstr, "\n^3FLOOD CONTROL: ^7message too long, trimmed\n");
777                         sourcecmsgstr = "";
778                 }
779                 else
780                 {
781                         sourcemsgstr = fullmsgstr;
782                         sourcecmsgstr = fullcmsgstr;
783                 }
784                 cmsgstr = "";
785         }
786         else
787         {
788                 sourcemsgstr = msgstr;
789                 sourcecmsgstr = cmsgstr;
790         }
791
792         if(source.classname != "player")
793         {
794                 if(teamsay || (cvar("g_chat_nospectators") == 1) || (cvar("g_chat_nospectators") == 2 && !inWarmupStage))
795                         teamsay = -1; // spectators
796         }
797
798         if(flood)
799                 print("NOTE: ", playername(self), "^7 is flooding.\n");
800
801         if(flood == 1)
802         {
803                 if(cvar("g_chat_flood_notify_flooder"))
804                         sprint(self, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(self.flood_field - time), "^3 seconds\n"));
805                 else
806                         sprint(self, fullmsgstr);
807         }
808         else if(teamsay > 0)
809         {
810                 sprint(source, sourcemsgstr);
811                 if(sourcecmsgstr != "")
812                         centerprint(source, sourcecmsgstr);
813                 FOR_EACH_REALPLAYER(head) if(head.team == source.team)
814                         if(head != source)
815                         {
816                                 sprint(head, msgstr);
817                                 if(cmsgstr != "")
818                                         centerprint(head, cmsgstr);
819                         }
820         }
821         else if(teamsay < 0)
822         {
823                 sprint(source, sourcemsgstr);
824                 FOR_EACH_REALCLIENT(head) if(head.classname != "player")
825                         if(head != source)
826                                 sprint(head, msgstr);
827         }
828         else if(sourcemsgstr != msgstr)
829         {
830                 sprint(source, sourcemsgstr);
831                 FOR_EACH_REALCLIENT(head)
832                         if(head != source)
833                                 sprint(head, msgstr);
834         }
835         else
836                 bprint(msgstr);
837 }
838
839 float GetVoiceMessageVoiceType(string type)
840 {
841         if(type == "taunt")
842                 return VOICETYPE_TAUNT;
843         if(type == "teamshoot")
844                 return VOICETYPE_LASTATTACKER;
845         return VOICETYPE_TEAMRADIO;
846 }
847
848 string allvoicesamples;
849 float GetPlayerSoundSampleField_notFound;
850 float GetPlayerSoundSampleField_fixed;
851 .string GetVoiceMessageSampleField(string type)
852 {
853         GetPlayerSoundSampleField_notFound = 0;
854         GetPlayerSoundSampleField_fixed = 0;
855         switch(type)
856         {
857 #define _VOICEMSG(m) case #m: return playersound_##m;
858                 ALLVOICEMSGS
859 #undef _VOICEMSG
860         }
861         GetPlayerSoundSampleField_notFound = 1;
862         return playersound_taunt;
863 }
864
865 .string GetPlayerSoundSampleField(string type)
866 {
867         GetPlayerSoundSampleField_notFound = 0;
868         GetPlayerSoundSampleField_fixed = 0;
869         switch(type)
870         {
871 #define _VOICEMSG(m) case #m: return playersound_##m;
872                 ALLPLAYERSOUNDS
873 #undef _VOICEMSG
874         }
875         GetPlayerSoundSampleField_notFound = 1;
876         return playersound_taunt;
877 }
878
879 void PrecacheGlobalSound(string samplestring)
880 {
881         float n, i;
882         tokenize_sane(samplestring);
883         n = stof(argv(1));
884         if(n > 0)
885         {
886                 for(i = 1; i <= n; ++i)
887                         precache_sound(strcat(argv(0), ftos(i), ".wav"));
888         }
889         else
890         {
891                 precache_sound(strcat(argv(0), ".wav"));
892         }
893 }
894
895 void PrecachePlayerSounds(string f)
896 {
897         float fh;
898         string s;
899         fh = fopen(f, FILE_READ);
900         if(fh < 0)
901                 return;
902         while((s = fgets(fh)))
903         {
904                 if(tokenize_sane(s) != 3)
905                 {
906                         dprint("Invalid sound info line: ", s, "\n");
907                         continue;
908                 }
909                 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
910         }
911         fclose(fh);
912
913         if not(allvoicesamples)
914         {
915 #define _VOICEMSG(m) allvoicesamples = strcat(allvoicesamples, " ", #m);
916                 ALLVOICEMSGS
917 #undef _VOICEMSG
918                 allvoicesamples = strzone(substring(allvoicesamples, 1, strlen(allvoicesamples) - 1));
919         }
920 }
921
922 void ClearPlayerSounds()
923 {
924 #define _VOICEMSG(m) if(self.playersound_##m) { strunzone(self.playersound_##m); self.playersound_##m = string_null; }
925         ALLPLAYERSOUNDS
926         ALLVOICEMSGS
927 #undef _VOICEMSG
928 }
929
930 void LoadPlayerSounds(string f, float first)
931 {
932         float fh;
933         string s;
934         var .string field;
935         fh = fopen(f, FILE_READ);
936         if(fh < 0)
937                 return;
938         while((s = fgets(fh)))
939         {
940                 if(tokenize_sane(s) != 3)
941                         continue;
942                 field = GetPlayerSoundSampleField(argv(0));
943                 if(GetPlayerSoundSampleField_notFound)
944                         field = GetVoiceMessageSampleField(argv(0));
945                 if(GetPlayerSoundSampleField_notFound)
946                         continue;
947                 if(GetPlayerSoundSampleField_fixed)
948                         if not(first)
949                                 continue;
950                 if(self.field)
951                         strunzone(self.field);
952                 self.field = strzone(strcat(argv(1), " ", argv(2)));
953         }
954         fclose(fh);
955 }
956
957 .float modelindex_for_playersound;
958 void UpdatePlayerSounds()
959 {
960         if(self.modelindex == self.modelindex_for_playersound)
961                 return;
962         self.modelindex_for_playersound = self.modelindex;
963         ClearPlayerSounds();
964         LoadPlayerSounds("sound/player/default.sounds", 1);
965         LoadPlayerSounds(strcat(self.model, ".sounds"), 0);
966 }
967
968 void GlobalSound(string sample, float chan, float voicetype)
969 {
970         float n;
971         float tauntrand;
972
973         if(sample == "")
974                 return;
975
976         tokenize_sane(sample);
977         n = stof(argv(1));
978         if(n > 0)
979                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
980         else
981                 sample = strcat(argv(0), ".wav"); // randomization
982         
983         switch(voicetype)
984         {
985                 case VOICETYPE_LASTATTACKER_ONLY:
986                         if(self.pusher)
987                                 if(self.pusher.team == self.team)
988                                 {
989                                         msg_entity = self.pusher;
990                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
991                                         {
992                                                 if(msg_entity.cvar_cl_voice_directional == 1)
993                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
994                                                 else
995                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
996                                         }
997                                 }
998                         break;
999                 case VOICETYPE_LASTATTACKER:
1000                         if(self.pusher)
1001                                 if(self.pusher.team == self.team)
1002                                 {
1003                                         msg_entity = self.pusher;
1004                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
1005                                         {
1006                                                 if(msg_entity.cvar_cl_voice_directional == 1)
1007                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
1008                                                 else
1009                                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1010                                         }
1011                                         msg_entity = self;
1012                                         if(clienttype(msg_entity) == CLIENTTYPE_REAL)
1013                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTN_NONE);
1014                                 }
1015                         break;
1016                 case VOICETYPE_TEAMRADIO:
1017                         FOR_EACH_REALCLIENT(msg_entity)
1018                                 if(!teams_matter || msg_entity.team == self.team)
1019                                 {
1020                                         if(msg_entity.cvar_cl_voice_directional == 1)
1021                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_MIN);
1022                                         else
1023                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1024                                 }
1025                         break;
1026                 case VOICETYPE_AUTOTAUNT:
1027                         tauntrand = random();
1028                         FOR_EACH_REALCLIENT(msg_entity)
1029                                 if (tauntrand < msg_entity.cvar_cl_autotaunt)
1030                                 {
1031                                         if (msg_entity.cvar_cl_voice_directional >= 1)
1032                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTN_MAX));
1033                                         else
1034                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1035                                 }
1036                         break;
1037                 case VOICETYPE_TAUNT:
1038                         FOR_EACH_REALCLIENT(msg_entity)
1039                         {
1040                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1041                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTN_MAX));
1042                                 else
1043                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTN_NONE);
1044                         }
1045                 case VOICETYPE_PLAYERSOUND:
1046                         sound(self, chan, sample, VOL_BASE, ATTN_NORM);
1047                         break;
1048                 default:
1049                         backtrace("Invalid voice type!");
1050                         break;
1051         }
1052 }
1053
1054 void PlayerSound(.string samplefield, float chan, float voicetype)
1055 {
1056         string sample;
1057         sample = self.samplefield;
1058         GlobalSound(sample, chan, voicetype);
1059 }
1060
1061 .float floodcontrol_voice;
1062 .float floodcontrol_voiceteam;
1063 void VoiceMessage(string type, string msg)
1064 {
1065         var .string sample;
1066         var float voicetype, ownteam;
1067         sample = GetVoiceMessageSampleField(type);
1068
1069         if(GetPlayerSoundSampleField_notFound)
1070         {
1071                 sprint(self, strcat("Invalid voice. Use one of: ", allvoicesamples, "\n"));
1072                 return;
1073         }
1074
1075         voicetype = GetVoiceMessageVoiceType(type);
1076         ownteam = (voicetype == VOICETYPE_TEAMRADIO);
1077
1078         float flood;
1079         float flood_spv;
1080         var .float flood_field;
1081
1082         flood = 0;
1083         if(ownteam)
1084         {
1085                 flood_spv = cvar("g_voice_flood_spv_team");
1086                 flood_field = floodcontrol_voiceteam;
1087         }
1088         else
1089         {
1090                 flood_spv = cvar("g_voice_flood_spv");
1091                 flood_field = floodcontrol_voice;
1092         }
1093
1094         if(time >= self.flood_field)
1095                 self.flood_field = max(time, self.flood_field) + flood_spv;
1096         else
1097                 flood = 1;
1098
1099         if (msg != "")
1100                 Say(self, ownteam, msg, 0);
1101
1102         if (!flood)
1103                 PlayerSound(sample, CHAN_VOICE, voicetype);
1104 }