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