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