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