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