]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_player.qc
Make it work
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_player.qc
1 .entity pusher;
2 .float pushltime;
3 .float istypefrag;
4
5 .float CopyBody_nextthink;
6 .void(void) CopyBody_think;
7 void CopyBody_Think(void)
8 {
9         if(self.CopyBody_nextthink && time > self.CopyBody_nextthink)
10         {
11                 self.CopyBody_think();
12                 if(wasfreed(self))
13                         return;
14                 self.CopyBody_nextthink = self.nextthink;
15                 self.CopyBody_think = self.think;
16                 self.think = CopyBody_Think;
17         }
18         CSQCMODEL_AUTOUPDATE();
19         self.nextthink = time;
20 }
21 void CopyBody(float keepvelocity)
22 {
23         entity oldself;
24         if (self.effects & EF_NODRAW)
25                 return;
26         oldself = self;
27         self = spawn();
28         self.enemy = oldself;
29         self.lip = oldself.lip;
30         self.colormap = oldself.colormap;
31         self.iscreature = oldself.iscreature;
32         self.teleportable = oldself.teleportable;
33         self.damagedbycontents = oldself.damagedbycontents;
34         self.angles = oldself.angles;
35         self.v_angle = oldself.v_angle;
36         self.avelocity = oldself.avelocity;
37         self.classname = "body";
38         self.damageforcescale = oldself.damageforcescale;
39         self.effects = oldself.effects;
40         self.glowmod = oldself.glowmod;
41         self.event_damage = oldself.event_damage;
42         self.anim_state = oldself.anim_state;
43         self.anim_time = oldself.anim_time;
44         self.anim_lower_action = oldself.anim_lower_action;
45         self.anim_lower_time = oldself.anim_lower_time;
46         self.anim_upper_action = oldself.anim_upper_action;
47         self.anim_upper_time = oldself.anim_upper_time;
48         self.anim_implicit_state = oldself.anim_implicit_state;
49         self.anim_implicit_time = oldself.anim_implicit_time;
50         self.anim_lower_implicit_action = oldself.anim_lower_implicit_action;
51         self.anim_lower_implicit_time = oldself.anim_lower_implicit_time;
52         self.anim_upper_implicit_action = oldself.anim_upper_implicit_action;
53         self.anim_upper_implicit_time = oldself.anim_upper_implicit_time;
54         self.dphitcontentsmask = oldself.dphitcontentsmask;
55         self.death_time = oldself.death_time;
56         self.pain_finished = oldself.pain_finished;
57         self.health = oldself.health;
58         self.armorvalue = oldself.armorvalue;
59         self.armortype = oldself.armortype;
60         self.model = oldself.model;
61         self.modelindex = oldself.modelindex;
62         self.skin = oldself.skin;
63         self.species = oldself.species;
64         self.movetype = oldself.movetype;
65         self.solid = oldself.solid;
66         self.ballistics_density = oldself.ballistics_density;
67         self.takedamage = oldself.takedamage;
68         self.customizeentityforclient = oldself.customizeentityforclient;
69         self.uncustomizeentityforclient = oldself.uncustomizeentityforclient;
70         self.uncustomizeentityforclient_set = oldself.uncustomizeentityforclient_set;
71         if (keepvelocity == 1)
72                 self.velocity = oldself.velocity;
73         self.oldvelocity = self.velocity;
74         self.alpha = oldself.alpha;
75         self.fade_time = oldself.fade_time;
76         self.fade_rate = oldself.fade_rate;
77         //self.weapon = oldself.weapon;
78         setorigin(self, oldself.origin);
79         setsize(self, oldself.mins, oldself.maxs);
80         self.prevorigin = oldself.origin;
81         self.reset = SUB_Remove;
82
83         Drag_MoveDrag(oldself, self);
84
85         if(self.colormap <= maxclients && self.colormap > 0)
86                 self.colormap = 1024 + oldself.clientcolors;
87
88         CSQCMODEL_AUTOINIT();
89         self.CopyBody_nextthink = oldself.nextthink;
90         self.CopyBody_think = oldself.think;
91         self.nextthink = time;
92         self.think = CopyBody_Think;
93         // "bake" the current animation frame for clones (they don't get clientside animation)
94         animdecide_setframes(self, FALSE, frame, frame1time, frame2, frame2time);
95
96         self = oldself;
97 }
98
99 float player_getspecies()
100 {
101         float s;
102         get_model_parameters(self.model, self.skin);
103         s = get_model_parameters_species;
104         get_model_parameters(string_null, 0);
105         if(s < 0)
106                 return SPECIES_HUMAN;
107         return s;
108 }
109
110 void player_setupanimsformodel()
111 {
112         // load animation info
113         animdecide_init(self);
114         animdecide_setstate(self, 0, FALSE);
115 }
116
117 void player_anim (void)
118 {
119         float deadbits = (self.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
120         if(self.deadflag) {
121                 if (!deadbits) {
122                         // Decide on which death animation to use.
123                         if(random() < 0.5)
124                                 deadbits = ANIMSTATE_DEAD1;
125                         else
126                                 deadbits = ANIMSTATE_DEAD2;
127                 }
128         } else {
129                 // Clear a previous death animation.
130                 deadbits = 0;
131         }
132         float animbits = deadbits;
133         if(self.frozen)
134                 animbits |= ANIMSTATE_FROZEN;
135         if(self.crouch)
136                 animbits |= ANIMSTATE_DUCK;
137         animdecide_setstate(self, animbits, FALSE);
138         animdecide_setimplicitstate(self, (self.flags & FL_ONGROUND));
139
140         if (self.weaponentity)
141         {
142                 updateanim(self.weaponentity);
143                 if (!self.weaponentity.animstate_override)
144                         setanim(self.weaponentity, self.weaponentity.anim_idle, TRUE, FALSE, FALSE);
145         }
146 }
147
148 void PlayerCorpseDamage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
149 {
150         float take, save;
151         vector v;
152         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
153
154         // damage resistance (ignore most of the damage from a bullet or similar)
155         damage = max(damage - 5, 1);
156
157         v = healtharmor_applydamage(self.armorvalue, autocvar_g_balance_armor_blockpercent, deathtype, damage);
158         take = v_x;
159         save = v_y;
160
161         if(sound_allowed(MSG_BROADCAST, attacker))
162         {
163                 if (save > 10)
164                         sound (self, CH_SHOTS, "misc/armorimpact.wav", VOL_BASE, ATTEN_NORM);
165                 else if (take > 30)
166                         sound (self, CH_SHOTS, "misc/bodyimpact2.wav", VOL_BASE, ATTEN_NORM);
167                 else if (take > 10)
168                         sound (self, CH_SHOTS, "misc/bodyimpact1.wav", VOL_BASE, ATTEN_NORM);
169         }
170
171         if (take > 50)
172                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
173         if (take > 100)
174                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
175
176         if (!(self.flags & FL_GODMODE))
177         {
178                 self.armorvalue = self.armorvalue - save;
179                 self.health = self.health - take;
180                 // pause regeneration for 5 seconds
181                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
182         }
183         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
184         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
185         self.dmg_inflictor = inflictor;
186
187         if (self.health <= -autocvar_sv_gibhealth && self.alpha >= 0)
188         {
189                 // don't use any animations as a gib
190                 self.frame = 0;
191                 // view just above the floor
192                 self.view_ofs = '0 0 4';
193
194                 Violence_GibSplash(self, 1, 1, attacker);
195                 self.alpha = -1;
196                 self.solid = SOLID_NOT; // restore later
197                 self.takedamage = DAMAGE_NO; // restore later
198                 self.damagedbycontents = FALSE;
199         }
200 }
201
202 // g_<gametype>_str:
203 // If 0, default is used.
204 // If <0, 0 is used.
205 // Otherwise, g_str (default value) is used.
206 // For consistency, negative values there are mapped to zero too.
207 #define GAMETYPE_DEFAULTED_SETTING(str) \
208         ((gametype_setting_tmp = cvar(strcat("g_", GetGametype(), "_" #str))), \
209          (gametype_setting_tmp < 0) ? 0 : \
210          (gametype_setting_tmp == 0) ? max(0, autocvar_g_##str) : \
211          gametype_setting_tmp)
212
213
214 void calculate_player_respawn_time()
215 {
216         if(g_ca)
217                 return;
218
219         float gametype_setting_tmp;
220         float sdelay_max = GAMETYPE_DEFAULTED_SETTING(respawn_delay_max);
221         float sdelay_small = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small);
222         float sdelay_large = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large);
223         float sdelay_small_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small_count);
224         float sdelay_large_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large_count);
225         float waves = GAMETYPE_DEFAULTED_SETTING(respawn_waves);
226
227         float pcount = 1;  // Include myself whether or not team is already set right and I'm a "player".
228         entity pl;
229         if (teamplay)
230         {
231                 FOR_EACH_PLAYER(pl)
232                         if (pl != self)
233                                 if (pl.team == self.team)
234                                         ++pcount;
235                 if (sdelay_small_count == 0)
236                         sdelay_small_count = 1;
237                 if (sdelay_large_count == 0)
238                         sdelay_large_count = 1;
239         }
240         else
241         {
242                 FOR_EACH_PLAYER(pl)
243                         if (pl != self)
244                                 ++pcount;
245                 if (sdelay_small_count == 0)
246                 {
247                         if (g_cts)
248                         {
249                                 // Players play independently. No point in requiring enemies.
250                                 sdelay_small_count = 1;
251                         }
252                         else
253                         {
254                                 // Players play AGAINST each other. Enemies required.
255                                 sdelay_small_count = 2;
256                         }
257                 }
258                 if (sdelay_large_count == 0)
259                 {
260                         if (g_cts)
261                         {
262                                 // Players play independently. No point in requiring enemies.
263                                 sdelay_large_count = 1;
264                         }
265                         else
266                         {
267                                 // Players play AGAINST each other. Enemies required.
268                                 sdelay_large_count = 2;
269                         }
270                 }
271         }
272
273         float sdelay;
274
275         if (pcount <= sdelay_small_count)
276                 sdelay = sdelay_small;
277         else if (pcount >= sdelay_large_count)
278                 sdelay = sdelay_large;
279         else  // NOTE: this case implies sdelay_large_count > sdelay_small_count.
280                 sdelay = sdelay_small + (sdelay_large - sdelay_small) * (pcount - sdelay_small_count) / (sdelay_large_count - sdelay_small_count);
281
282         if(waves)
283                 self.respawn_time = ceil((time + sdelay) / waves) * waves;
284         else
285                 self.respawn_time = time + sdelay;
286
287         if(sdelay < sdelay_max)
288                 self.respawn_time_max = time + sdelay_max;
289         else
290                 self.respawn_time_max = self.respawn_time;
291
292         if((sdelay + waves >= 5.0) && (self.respawn_time - time > 1.75))
293                 self.respawn_countdown = 10; // first number to count down from is 10
294         else
295                 self.respawn_countdown = -1; // do not count down
296
297         if(autocvar_g_forced_respawn)
298                 self.respawn_flags = self.respawn_flags | RESPAWN_FORCE;
299 }
300
301 void ClientKill_Now_TeamChange();
302
303 void PlayerDamage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
304 {
305         float take, save, dh, da, j;
306         vector v;
307         float valid_damage_for_weaponstats;
308         float excess;
309
310         dh = max(self.health, 0);
311         da = max(self.armorvalue, 0);
312
313         if(!DEATH_ISSPECIAL(deathtype))
314         {
315                 damage *= sqrt(bound(1.0, self.cvar_cl_handicap, 100.0));
316                 if(self != attacker)
317                         damage /= sqrt(bound(1.0, attacker.cvar_cl_handicap, 100.0));
318         }
319
320         if(DEATH_ISWEAPON(deathtype, WEP_TUBA))
321         {
322                 // tuba causes blood to come out of the ears
323                 vector ear1, ear2;
324                 vector d;
325                 float f;
326                 ear1 = self.origin;
327                 ear1_z += 0.125 * self.view_ofs_z + 0.875 * self.maxs_z; // 7/8
328                 ear2 = ear1;
329                 makevectors(self.angles);
330                 ear1 += v_right * -10;
331                 ear2 += v_right * +10;
332                 d = inflictor.origin - self.origin;
333                 if (d)
334                         f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
335                 else
336                         f = 0;  // Assum ecenter.
337                 force = v_right * vlen(force);
338                 Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), self, attacker);
339                 Violence_GibSplash_At(ear2, force,      2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), self, attacker);
340                 if(f > 0)
341                 {
342                         hitloc = ear1;
343                         force = force * -1;
344                 }
345                 else
346                 {
347                         hitloc = ear2;
348                         // force is already good
349                 }
350         }
351         else
352                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
353
354
355         v = healtharmor_applydamage(self.armorvalue, autocvar_g_balance_armor_blockpercent, deathtype, damage);
356         take = v_x;
357         save = v_y;
358
359         if(attacker == self)
360         {
361                 // don't reset pushltime for self damage as it may be an attempt to
362                 // escape a lava pit or similar
363                 //self.pushltime = 0;
364                 self.istypefrag = 0;
365         }
366         else if(IS_PLAYER(attacker))
367         {
368                 self.pusher = attacker;
369                 self.pushltime = time + autocvar_g_maxpushtime;
370                 self.istypefrag = self.BUTTON_CHAT;
371         }
372         else if(time < self.pushltime)
373         {
374                 attacker = self.pusher;
375                 self.pushltime = max(self.pushltime, time + 0.6);
376         }
377         else
378         {
379                 self.pushltime = 0;
380                 self.istypefrag = 0;
381         }
382
383         frag_inflictor = inflictor;
384         frag_attacker = attacker;
385         frag_target = self;
386         frag_damage = damage;
387         damage_take = take;
388         damage_save = save;
389         damage_force = force;
390         MUTATOR_CALLHOOK(PlayerDamage_SplitHealthArmor);
391         take = bound(0, damage_take, self.health);
392         save = bound(0, damage_save, self.armorvalue);
393         excess = max(0, damage - take - save);
394
395         if(sound_allowed(MSG_BROADCAST, attacker))
396         {
397                 if (save > 10)
398                         sound (self, CH_SHOTS, "misc/armorimpact.wav", VOL_BASE, ATTEN_NORM);
399                 else if (take > 30)
400                         sound (self, CH_SHOTS, "misc/bodyimpact2.wav", VOL_BASE, ATTEN_NORM);
401                 else if (take > 10)
402                         sound (self, CH_SHOTS, "misc/bodyimpact1.wav", VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
403         }
404
405         if (take > 50)
406                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
407         if (take > 100)
408                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
409
410         if (time >= self.spawnshieldtime)
411         {
412                 if (!(self.flags & FL_GODMODE))
413                 {
414                         self.armorvalue = self.armorvalue - save;
415                         self.health = self.health - take;
416                         // pause regeneration for 5 seconds
417                         if(take)
418                                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
419
420                         if (time > self.pain_finished)          //Don't switch pain sequences like crazy
421                         {
422                                 self.pain_finished = time + 0.5;        //Supajoe
423
424                                 if(autocvar_sv_gentle < 1) {
425                                         if(self.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
426                                         {
427                                                 if (!self.animstate_override)
428                                                 {
429                                                         if (random() > 0.5)
430                                                                 animdecide_setaction(self, ANIMACTION_PAIN1, TRUE);
431                                                         else
432                                                                 animdecide_setaction(self, ANIMACTION_PAIN2, TRUE);
433                                                 }
434                                         }
435
436                                         if(sound_allowed(MSG_BROADCAST, attacker))
437                                         if((self.health < 2 * WEP_CVAR_PRI(blaster, damage) * autocvar_g_balance_selfdamagepercent + 1) || !((get_weaponinfo(DEATH_WEAPONOF(deathtype))).spawnflags & WEP_FLAG_CANCLIMB) || attacker != self) // WEAPONTODO: create separate limit for pain notification with laser
438                                         if(self.health > 1)
439                                         // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
440                                         {
441                                                 if(deathtype == DEATH_FALL)
442                                                         PlayerSound(playersound_fall, CH_PAIN, VOICETYPE_PLAYERSOUND);
443                                                 else if(self.health > 75) // TODO make a "gentle" version?
444                                                         PlayerSound(playersound_pain100, CH_PAIN, VOICETYPE_PLAYERSOUND);
445                                                 else if(self.health > 50)
446                                                         PlayerSound(playersound_pain75, CH_PAIN, VOICETYPE_PLAYERSOUND);
447                                                 else if(self.health > 25)
448                                                         PlayerSound(playersound_pain50, CH_PAIN, VOICETYPE_PLAYERSOUND);
449                                                 else
450                                                         PlayerSound(playersound_pain25, CH_PAIN, VOICETYPE_PLAYERSOUND);
451                                         }
452                                 }
453                         }
454
455                         // throw off bot aim temporarily
456                         float shake;
457                         if(IS_BOT_CLIENT(self) && self.health >= 1)
458                         {
459                                 shake = damage * 5 / (bound(0,skill,100) + 1);
460                                 self.v_angle_x = self.v_angle_x + (random() * 2 - 1) * shake;
461                                 self.v_angle_y = self.v_angle_y + (random() * 2 - 1) * shake;
462                                 self.v_angle_x = bound(-90, self.v_angle_x, 90);
463                         }
464                 }
465                 else
466                         self.max_armorvalue += (save + take);
467         }
468         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
469         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
470         self.dmg_inflictor = inflictor;
471
472         float abot, vbot, awep;
473         abot = (IS_BOT_CLIENT(attacker));
474         vbot = (IS_BOT_CLIENT(self));
475
476         valid_damage_for_weaponstats = 0;
477         awep = 0;
478
479         if(vbot || IS_REAL_CLIENT(self))
480         if(abot || IS_REAL_CLIENT(attacker))
481         if(attacker && self != attacker)
482         if(DIFF_TEAM(self, attacker))
483         {
484                 if(DEATH_ISSPECIAL(deathtype))
485                         awep = attacker.weapon;
486                 else
487                         awep = DEATH_WEAPONOF(deathtype);
488                 valid_damage_for_weaponstats = 1;
489         }
490
491         if(valid_damage_for_weaponstats)
492         {
493                 dh = dh - max(self.health, 0);
494                 da = da - max(self.armorvalue, 0);
495                 WeaponStats_LogDamage(awep, abot, self.weapon, vbot, dh + da);
496         }
497
498         if (self.health < 1)
499         {
500                 float defer_ClientKill_Now_TeamChange;
501                 defer_ClientKill_Now_TeamChange = FALSE;
502
503                 if(self.alivetime)
504                 {
505                         PS_GR_P_ADDVAL(self, PLAYERSTATS_ALIVETIME, time - self.alivetime);
506                         self.alivetime = 0;
507                 }
508
509                 if(valid_damage_for_weaponstats)
510                         WeaponStats_LogKill(awep, abot, self.weapon, vbot);
511
512                 if(autocvar_sv_gentle < 1) // TODO make a "gentle" version?
513                 if(sound_allowed(MSG_BROADCAST, attacker))
514                 {
515                         if(deathtype == DEATH_DROWN)
516                                 PlayerSound(playersound_drown, CH_PAIN, VOICETYPE_PLAYERSOUND);
517                         else
518                                 PlayerSound(playersound_death, CH_PAIN, VOICETYPE_PLAYERSOUND);
519                 }
520
521                 // get rid of kill indicator
522                 if(self.killindicator)
523                 {
524                         remove(self.killindicator);
525                         self.killindicator = world;
526                         if(self.killindicator_teamchange)
527                                 defer_ClientKill_Now_TeamChange = TRUE;
528
529                         if(self.classname == "body")
530                         if(deathtype == DEATH_KILL)
531                         {
532                                 // for the lemmings fans, a small harmless explosion
533                                 pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
534                         }
535                 }
536
537                 // print an obituary message
538                 Obituary (attacker, inflictor, self, deathtype);
539
540         // increment frag counter for used weapon type
541         float w;
542         w = DEATH_WEAPONOF(deathtype);
543         if(WEP_VALID(w))
544         if(accuracy_isgooddamage(attacker, self))
545         attacker.accuracy.(accuracy_frags[w-1]) += 1;
546
547                 frag_attacker = attacker;
548                 frag_inflictor = inflictor;
549                 frag_target = self;
550                 frag_damage = excess;
551                 frag_deathtype = deathtype;
552                 MUTATOR_CALLHOOK(PlayerDies);
553                 excess = frag_damage;
554
555                 WEP_ACTION(self.weapon, WR_PLAYERDEATH);
556
557                 RemoveGrapplingHook(self);
558
559                 Portal_ClearAllLater(self);
560
561                 self.fixangle = TRUE;
562
563                 if(defer_ClientKill_Now_TeamChange)
564                         ClientKill_Now_TeamChange(); // can turn player into spectator
565
566                 // player could have been miraculously resuscitated ;)
567                 // e.g. players in freezetag get frozen, they don't really die
568                 if(self.health >= 1 || !IS_PLAYER(self))
569                         return;
570
571                 // when we get here, player actually dies
572
573                 Unfreeze(self); // remove any icy remains
574                 self.health = 0; // Unfreeze resets health, so we need to set it back
575
576                 // clear waypoints
577                 WaypointSprite_PlayerDead();
578                 // throw a weapon
579                 SpawnThrownWeapon (self.origin + (self.mins + self.maxs) * 0.5, self.switchweapon);
580
581                 // become fully visible
582                 self.alpha = default_player_alpha;
583                 // make the corpse upright (not tilted)
584                 self.angles_x = 0;
585                 self.angles_z = 0;
586                 // don't spin
587                 self.avelocity = '0 0 0';
588                 // view from the floor
589                 self.view_ofs = '0 0 -8';
590                 // toss the corpse
591                 self.movetype = MOVETYPE_TOSS;
592                 // shootable corpse
593                 self.solid = SOLID_CORPSE;
594                 self.ballistics_density = autocvar_g_ballistics_density_corpse;
595                 // don't stick to the floor
596                 self.flags &= ~FL_ONGROUND;
597                 // dying animation
598                 self.deadflag = DEAD_DYING;
599
600                 // when to allow respawn
601                 calculate_player_respawn_time();
602
603                 self.death_time = time;
604                 if (random() < 0.5)
605                         animdecide_setstate(self, self.anim_state | ANIMSTATE_DEAD1, TRUE);
606                 else
607                         animdecide_setstate(self, self.anim_state | ANIMSTATE_DEAD2, TRUE);
608                 if (self.maxs_z > 5)
609                 {
610                         self.maxs_z = 5;
611                         setsize(self, self.mins, self.maxs);
612                 }
613                 // set damage function to corpse damage
614                 self.event_damage = PlayerCorpseDamage;
615                 // call the corpse damage function just in case it wants to gib
616                 self.event_damage(inflictor, attacker, excess, deathtype, hitloc, force);
617                 // set up to fade out later
618                 SUB_SetFade (self, time + 6 + random (), 1);
619
620                 if(autocvar_sv_gentle > 0 || autocvar_ekg) {
621                         // remove corpse
622                         PlayerCorpseDamage (inflictor, attacker, autocvar_sv_gibhealth+1.0, deathtype, hitloc, force);
623                 }
624
625                 // reset fields the weapons may use just in case
626                 for (j = WEP_FIRST; j <= WEP_LAST; ++j)
627                 {
628                         WEP_ACTION(j, WR_RESETPLAYER);
629                         ATTACK_FINISHED_FOR(self, j) = 0;
630                 }
631         }
632 }
633
634 .float muted; // to be used by prvm_edictset server playernumber muted 1
635 float Say(entity source, float teamsay, entity privatesay, string msgin, float floodcontrol)
636 // message "": do not say, just test flood control
637 // return value:
638 //   1 = accept
639 //   0 = reject
640 //  -1 = fake accept
641 {
642         string msgstr, colorstr, cmsgstr, namestr, fullmsgstr, sourcemsgstr, fullcmsgstr, sourcecmsgstr, colorprefix;
643         float flood;
644         var .float flood_field;
645         entity head;
646         float ret;
647         string privatemsgprefix = string_null; float privatemsgprefixlen = 0;
648
649         if(!teamsay && !privatesay)
650                 if(substring(msgin, 0, 1) == " ")
651                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
652
653         msgin = formatmessage(msgin);
654
655         if (!IS_PLAYER(source))
656                 colorstr = "^0"; // black for spectators
657         else if(teamplay)
658                 colorstr = Team_ColorCode(source.team);
659         else
660         {
661                 colorstr = "";
662                 teamsay = FALSE;
663         }
664
665         if(intermission_running)
666                 teamsay = FALSE;
667
668         if(msgin != "")
669                 msgin = trigger_magicear_processmessage_forallears(source, teamsay, privatesay, msgin);
670
671         /*
672          * using bprint solves this... me stupid
673         // how can we prevent the message from appearing in a listen server?
674         // for now, just give "say" back and only handle say_team
675         if(!teamsay)
676         {
677                 clientcommand(self, strcat("say ", msgin));
678                 return;
679         }
680         */
681
682         if(autocvar_g_chat_teamcolors)
683                 namestr = playername(source);
684         else
685                 namestr = source.netname;
686
687         if(strdecolorize(namestr) == namestr)
688                 colorprefix = "^3";
689         else
690                 colorprefix = "^7";
691
692         if(msgin != "")
693         {
694                 if(privatesay)
695                 {
696                         msgstr = strcat("\{1}\{13}* ", colorprefix, namestr, "^3 tells you: ^7");
697                         privatemsgprefixlen = strlen(msgstr);
698                         msgstr = strcat(msgstr, msgin);
699                         cmsgstr = strcat(colorstr, colorprefix, namestr, "^3 tells you:\n^7", msgin);
700                         if(autocvar_g_chat_teamcolors)
701                                 privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", playername(privatesay), ": ^7");
702                         else
703                                 privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", privatesay.netname, ": ^7");
704                 }
705                 else if(teamsay)
706                 {
707                         msgstr = strcat("\{1}\{13}", colorstr, "(", colorprefix, namestr, colorstr, ") ^7", msgin);
708                         cmsgstr = strcat(colorstr, "(", colorprefix, namestr, colorstr, ")\n^7", msgin);
709                 }
710                 else
711                 {
712                         msgstr = strcat("\{1}", colorprefix, namestr, "^7: ", msgin);
713                         cmsgstr = "";
714                 }
715                 msgstr = strcat(strreplace("\n", " ", msgstr), "\n"); // newlines only are good for centerprint
716         }
717         else
718         {
719                 msgstr = cmsgstr = "";
720         }
721
722         fullmsgstr = msgstr;
723         fullcmsgstr = cmsgstr;
724
725         // FLOOD CONTROL
726         flood = 0;
727         flood_field = floodcontrol_chat;
728         if(floodcontrol)
729         {
730                 float flood_spl;
731                 float flood_burst;
732                 float flood_lmax;
733                 float lines;
734                 if(privatesay)
735                 {
736                         flood_spl = autocvar_g_chat_flood_spl_tell;
737                         flood_burst = autocvar_g_chat_flood_burst_tell;
738                         flood_lmax = autocvar_g_chat_flood_lmax_tell;
739                         flood_field = floodcontrol_chattell;
740                 }
741                 else if(teamsay)
742                 {
743                         flood_spl = autocvar_g_chat_flood_spl_team;
744                         flood_burst = autocvar_g_chat_flood_burst_team;
745                         flood_lmax = autocvar_g_chat_flood_lmax_team;
746                         flood_field = floodcontrol_chatteam;
747                 }
748                 else
749                 {
750                         flood_spl = autocvar_g_chat_flood_spl;
751                         flood_burst = autocvar_g_chat_flood_burst;
752                         flood_lmax = autocvar_g_chat_flood_lmax;
753                         flood_field = floodcontrol_chat;
754                 }
755                 flood_burst = max(0, flood_burst - 1);
756                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
757
758                 // do flood control for the default line size
759                 if(msgstr != "")
760                 {
761                         getWrappedLine_remaining = msgstr;
762                         msgstr = "";
763                         lines = 0;
764                         while(getWrappedLine_remaining && (!flood_lmax || lines <= flood_lmax))
765                         {
766                                 msgstr = strcat(msgstr, " ", getWrappedLineLen(82.4289758859709, strlennocol)); // perl averagewidth.pl < gfx/vera-sans.width
767                                 ++lines;
768                         }
769                         msgstr = substring(msgstr, 1, strlen(msgstr) - 1);
770
771                         if(getWrappedLine_remaining != "")
772                         {
773                                 msgstr = strcat(msgstr, "\n");
774                                 flood = 2;
775                         }
776
777                         if(time >= source.flood_field)
778                         {
779                                 source.flood_field = max(time - flood_burst * flood_spl, source.flood_field) + lines * flood_spl;
780                         }
781                         else
782                         {
783                                 flood = 1;
784                                 msgstr = fullmsgstr;
785                         }
786                 }
787                 else
788                 {
789                         if(time >= source.flood_field)
790                                 source.flood_field = max(time - flood_burst * flood_spl, source.flood_field) + flood_spl;
791                         else
792                                 flood = 1;
793                 }
794
795                 if (timeout_status == TIMEOUT_ACTIVE) // when game is paused, no flood protection
796                         source.flood_field = flood = 0;
797         }
798
799         if(flood == 2) // cannot happen for empty msgstr
800         {
801                 if(autocvar_g_chat_flood_notify_flooder)
802                 {
803                         sourcemsgstr = strcat(msgstr, "\n^3FLOOD CONTROL: ^7message too long, trimmed\n");
804                         sourcecmsgstr = "";
805                 }
806                 else
807                 {
808                         sourcemsgstr = fullmsgstr;
809                         sourcecmsgstr = fullcmsgstr;
810                 }
811                 cmsgstr = "";
812         }
813         else
814         {
815                 sourcemsgstr = msgstr;
816                 sourcecmsgstr = cmsgstr;
817         }
818
819         if(!privatesay)
820         if (!IS_PLAYER(source))
821         {
822                 if (!intermission_running)
823                         if(teamsay || (autocvar_g_chat_nospectators == 1) || (autocvar_g_chat_nospectators == 2 && !(warmup_stage || gameover)))
824                                 teamsay = -1; // spectators
825         }
826
827         if(flood)
828                 print("NOTE: ", playername(source), "^7 is flooding.\n");
829
830         // build sourcemsgstr by cutting off a prefix and replacing it by the other one
831         if(privatesay)
832                 sourcemsgstr = strcat(privatemsgprefix, substring(sourcemsgstr, privatemsgprefixlen, -1));
833
834         if(source.muted)
835         {
836                 // always fake the message
837                 ret = -1;
838         }
839         else if(flood == 1)
840         {
841                 if(autocvar_g_chat_flood_notify_flooder)
842                 {
843                         sprint(source, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(source.flood_field - time), "^3 seconds\n"));
844                         ret = 0;
845                 }
846                 else
847                         ret = -1;
848         }
849         else
850         {
851                 ret = 1;
852         }
853
854         if(sourcemsgstr != "" && ret != 0)
855         {
856                 if(ret < 0) // faked message, because the player is muted
857                 {
858                         sprint(source, sourcemsgstr);
859                         if(sourcecmsgstr != "" && !privatesay)
860                                 centerprint(source, sourcecmsgstr);
861                 }
862                 else if(privatesay) // private message, between 2 people only
863                 {
864                         sprint(source, sourcemsgstr);
865                         sprint(privatesay, msgstr);
866                         if (!autocvar_g_chat_tellprivacy) { dedicated_print(msgstr); } // send to server console too if "tellprivacy" is disabled
867                         if(cmsgstr != "")
868                                 centerprint(privatesay, cmsgstr);
869                 }
870                 else if(teamsay > 0) // team message, only sent to team mates
871                 {
872                         sprint(source, sourcemsgstr);
873                         dedicated_print(msgstr); // send to server console too
874                         if(sourcecmsgstr != "")
875                                 centerprint(source, sourcecmsgstr);
876                         FOR_EACH_REALPLAYER(head) if(head.team == source.team)
877                                 if(head != source)
878                                 {
879                                         sprint(head, msgstr);
880                                         if(cmsgstr != "")
881                                                 centerprint(head, cmsgstr);
882                                 }
883                 }
884                 else if(teamsay < 0) // spectator message, only sent to spectators
885                 {
886                         sprint(source, sourcemsgstr);
887                         dedicated_print(msgstr); // send to server console too
888                         FOR_EACH_REALCLIENT(head) if (!IS_PLAYER(head))
889                                 if(head != source)
890                                         sprint(head, msgstr);
891                 }
892                 else if(sourcemsgstr != msgstr) // trimmed/server fixed message, sent to all players
893                 {
894                         sprint(source, sourcemsgstr);
895                         dedicated_print(msgstr); // send to server console too
896                         FOR_EACH_REALCLIENT(head)
897                                 if(head != source)
898                                         sprint(head, msgstr);
899                 }
900                 else
901                         bprint(msgstr); // entirely normal message, sent to all players -- bprint sends to server console too.
902         }
903
904         return ret;
905 }
906
907 float GetVoiceMessageVoiceType(string type)
908 {
909         if(type == "taunt")
910                 return VOICETYPE_TAUNT;
911         if(type == "teamshoot")
912                 return VOICETYPE_LASTATTACKER;
913         return VOICETYPE_TEAMRADIO;
914 }
915
916 string allvoicesamples;
917 .string GetVoiceMessageSampleField(string type)
918 {
919         GetPlayerSoundSampleField_notFound = 0;
920         switch(type)
921         {
922 #define _VOICEMSG(m) case #m: return playersound_##m;
923                 ALLVOICEMSGS
924 #undef _VOICEMSG
925         }
926         GetPlayerSoundSampleField_notFound = 1;
927         return playersound_taunt;
928 }
929
930 .string GetPlayerSoundSampleField(string type)
931 {
932         GetPlayerSoundSampleField_notFound = 0;
933         switch(type)
934         {
935 #define _VOICEMSG(m) case #m: return playersound_##m;
936                 ALLPLAYERSOUNDS
937 #undef _VOICEMSG
938         }
939         GetPlayerSoundSampleField_notFound = 1;
940         return playersound_taunt;
941 }
942
943 void PrecacheGlobalSound(string samplestring)
944 {
945         float n, i;
946         tokenize_console(samplestring);
947         n = stof(argv(1));
948         if(n > 0)
949         {
950                 for(i = 1; i <= n; ++i)
951                         precache_sound(strcat(argv(0), ftos(i), ".wav"));
952         }
953         else
954         {
955                 precache_sound(strcat(argv(0), ".wav"));
956         }
957 }
958
959 void PrecachePlayerSounds(string f)
960 {
961         float fh;
962         string s;
963         fh = fopen(f, FILE_READ);
964         if(fh < 0)
965                 return;
966         while((s = fgets(fh)))
967         {
968                 if(tokenize_console(s) != 3)
969                 {
970                         dprint("Invalid sound info line: ", s, "\n");
971                         continue;
972                 }
973                 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
974         }
975         fclose(fh);
976
977         if (!allvoicesamples)
978         {
979 #define _VOICEMSG(m) allvoicesamples = strcat(allvoicesamples, " ", #m);
980                 ALLVOICEMSGS
981 #undef _VOICEMSG
982                 allvoicesamples = strzone(substring(allvoicesamples, 1, strlen(allvoicesamples) - 1));
983         }
984 }
985
986 void ClearPlayerSounds()
987 {
988 #define _VOICEMSG(m) if(self.playersound_##m) { strunzone(self.playersound_##m); self.playersound_##m = string_null; }
989         ALLPLAYERSOUNDS
990         ALLVOICEMSGS
991 #undef _VOICEMSG
992 }
993
994 float LoadPlayerSounds(string f, float first)
995 {
996         float fh;
997         string s;
998         var .string field;
999         fh = fopen(f, FILE_READ);
1000         if(fh < 0)
1001         {
1002                 dprint("Player sound file not found: ", f, "\n");
1003                 return 0;
1004         }
1005         while((s = fgets(fh)))
1006         {
1007                 if(tokenize_console(s) != 3)
1008                         continue;
1009                 field = GetPlayerSoundSampleField(argv(0));
1010                 if(GetPlayerSoundSampleField_notFound)
1011                         field = GetVoiceMessageSampleField(argv(0));
1012                 if(GetPlayerSoundSampleField_notFound)
1013                         continue;
1014                 if(self.field)
1015                         strunzone(self.field);
1016                 self.field = strzone(strcat(argv(1), " ", argv(2)));
1017         }
1018         fclose(fh);
1019         return 1;
1020 }
1021
1022 .float modelindex_for_playersound;
1023 .float skin_for_playersound;
1024 void UpdatePlayerSounds()
1025 {
1026         if(self.modelindex == self.modelindex_for_playersound)
1027         if(self.skin == self.skin_for_playersound)
1028                 return;
1029         self.modelindex_for_playersound = self.modelindex;
1030         self.skin_for_playersound = self.skin;
1031         ClearPlayerSounds();
1032         LoadPlayerSounds("sound/player/default.sounds", 1);
1033         if(!autocvar_g_debug_defaultsounds)
1034                 if(!LoadPlayerSounds(get_model_datafilename(self.model, self.skin, "sounds"), 0))
1035                         LoadPlayerSounds(get_model_datafilename(self.model, 0, "sounds"), 0);
1036 }
1037
1038 void FakeGlobalSound(string sample, float chan, float voicetype)
1039 {
1040         float n;
1041         float tauntrand;
1042
1043         if(sample == "")
1044                 return;
1045
1046         tokenize_console(sample);
1047         n = stof(argv(1));
1048         if(n > 0)
1049                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
1050         else
1051                 sample = strcat(argv(0), ".wav"); // randomization
1052
1053         switch(voicetype)
1054         {
1055                 case VOICETYPE_LASTATTACKER_ONLY:
1056                         break;
1057                 case VOICETYPE_LASTATTACKER:
1058                         if(self.pusher)
1059                         {
1060                                 msg_entity = self;
1061                                 if(IS_REAL_CLIENT(msg_entity))
1062                                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NONE);
1063                         }
1064                         break;
1065                 case VOICETYPE_TEAMRADIO:
1066                         msg_entity = self;
1067                         if(msg_entity.cvar_cl_voice_directional == 1)
1068                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1069                         else
1070                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1071                         break;
1072                 case VOICETYPE_AUTOTAUNT:
1073                         if(!sv_autotaunt)
1074                                 break;
1075                         if(!sv_taunt)
1076                                 break;
1077                         if(autocvar_sv_gentle)
1078                                 break;
1079                         tauntrand = random();
1080                         msg_entity = self;
1081                         if (tauntrand < msg_entity.cvar_cl_autotaunt)
1082                         {
1083                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1084                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1085                                 else
1086                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1087                         }
1088                         break;
1089                 case VOICETYPE_TAUNT:
1090                         if(IS_PLAYER(self))
1091                                 if(self.deadflag == DEAD_NO)
1092                                         animdecide_setaction(self, ANIMACTION_TAUNT, TRUE);
1093                         if(!sv_taunt)
1094                                 break;
1095                         if(autocvar_sv_gentle)
1096                                 break;
1097                         msg_entity = self;
1098                         if (msg_entity.cvar_cl_voice_directional >= 1)
1099                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1100                         else
1101                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1102                         break;
1103                 case VOICETYPE_PLAYERSOUND:
1104                         msg_entity = self;
1105                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NORM);
1106                         break;
1107                 default:
1108                         backtrace("Invalid voice type!");
1109                         break;
1110         }
1111 }
1112
1113 void GlobalSound(string sample, float chan, float voicetype)
1114 {
1115         float n;
1116         float tauntrand;
1117
1118         if(sample == "")
1119                 return;
1120
1121         tokenize_console(sample);
1122         n = stof(argv(1));
1123         if(n > 0)
1124                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
1125         else
1126                 sample = strcat(argv(0), ".wav"); // randomization
1127
1128         switch(voicetype)
1129         {
1130                 case VOICETYPE_LASTATTACKER_ONLY:
1131                         if(self.pusher)
1132                         {
1133                                 msg_entity = self.pusher;
1134                                 if(IS_REAL_CLIENT(msg_entity))
1135                                 {
1136                                         if(msg_entity.cvar_cl_voice_directional == 1)
1137                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1138                                         else
1139                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1140                                 }
1141                         }
1142                         break;
1143                 case VOICETYPE_LASTATTACKER:
1144                         if(self.pusher)
1145                         {
1146                                 msg_entity = self.pusher;
1147                                 if(IS_REAL_CLIENT(msg_entity))
1148                                 {
1149                                         if(msg_entity.cvar_cl_voice_directional == 1)
1150                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1151                                         else
1152                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1153                                 }
1154                                 msg_entity = self;
1155                                 if(IS_REAL_CLIENT(msg_entity))
1156                                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NONE);
1157                         }
1158                         break;
1159                 case VOICETYPE_TEAMRADIO:
1160                         FOR_EACH_REALCLIENT(msg_entity)
1161                                 if(!teamplay || msg_entity.team == self.team)
1162                                 {
1163                                         if(msg_entity.cvar_cl_voice_directional == 1)
1164                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1165                                         else
1166                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1167                                 }
1168                         break;
1169                 case VOICETYPE_AUTOTAUNT:
1170                         if(!sv_autotaunt)
1171                                 break;
1172                         if(!sv_taunt)
1173                                 break;
1174                         if(autocvar_sv_gentle)
1175                                 break;
1176                         tauntrand = random();
1177                         FOR_EACH_REALCLIENT(msg_entity)
1178                                 if (tauntrand < msg_entity.cvar_cl_autotaunt)
1179                                 {
1180                                         if (msg_entity.cvar_cl_voice_directional >= 1)
1181                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1182                                         else
1183                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1184                                 }
1185                         break;
1186                 case VOICETYPE_TAUNT:
1187                         if(IS_PLAYER(self))
1188                                 if(self.deadflag == DEAD_NO)
1189                                         animdecide_setaction(self, ANIMACTION_TAUNT, TRUE);
1190                         if(!sv_taunt)
1191                                 break;
1192                         if(autocvar_sv_gentle)
1193                                 break;
1194                         FOR_EACH_REALCLIENT(msg_entity)
1195                         {
1196                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1197                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1198                                 else
1199                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1200                         }
1201                         break;
1202                 case VOICETYPE_PLAYERSOUND:
1203                         sound(self, chan, sample, VOL_BASE, ATTEN_NORM);
1204                         break;
1205                 default:
1206                         backtrace("Invalid voice type!");
1207                         break;
1208         }
1209 }
1210
1211 void PlayerSound(.string samplefield, float chan, float voicetype)
1212 {
1213         GlobalSound(self.samplefield, chan, voicetype);
1214 }
1215
1216 void VoiceMessage(string type, string msg)
1217 {
1218         var .string sample;
1219         float voicetype, ownteam;
1220         float flood;
1221         sample = GetVoiceMessageSampleField(type);
1222
1223         if(GetPlayerSoundSampleField_notFound)
1224         {
1225                 sprint(self, strcat("Invalid voice. Use one of: ", allvoicesamples, "\n"));
1226                 return;
1227         }
1228
1229         voicetype = GetVoiceMessageVoiceType(type);
1230         ownteam = (voicetype == VOICETYPE_TEAMRADIO);
1231
1232         flood = Say(self, ownteam, world, msg, 1);
1233
1234         if(IS_SPEC(self) || IS_OBSERVER(self) || flood < 0)
1235                 FakeGlobalSound(self.sample, CH_VOICE, voicetype);
1236         else if (flood > 0)
1237                 GlobalSound(self.sample, CH_VOICE, voicetype);
1238 }
1239
1240 void MoveToTeam(entity client, float team_colour, float type)
1241 {
1242         float lockteams_backup;
1243
1244         lockteams_backup = lockteams;  // backup any team lock
1245
1246         lockteams = 0;  // disable locked teams
1247
1248         TeamchangeFrags(client);  // move the players frags
1249         SetPlayerColors(client, team_colour - 1);  // set the players colour
1250         Damage(client, client, client, 100000, DEATH_AUTOTEAMCHANGE, client.origin, '0 0 0');  // kill the player
1251
1252         lockteams = lockteams_backup;  // restore the team lock
1253
1254         LogTeamchange(client.playerid, client.team, type);
1255 }