Merge branch 'master' into TimePath/csqc_viewmodels
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_player.qc
1 #include "cl_player.qh"
2
3 #include "bot/bot.qh"
4 #include "cheats.qh"
5 #include "g_damage.qh"
6 #include "g_subs.qh"
7 #include "miscfunctions.qh"
8 #include "portals.qh"
9 #include "teamplay.qh"
10 #include "weapons/throwing.qh"
11 #include "command/common.qh"
12 #include "../common/anim.qh"
13 #include "../common/animdecide.qh"
14 #include "../common/csqcmodel_settings.qh"
15 #include "../common/deathtypes/all.qh"
16 #include "../common/triggers/subs.qh"
17 #include "../common/playerstats.qh"
18 #include "../lib/csqcmodel/sv_model.qh"
19
20 #include "../common/minigames/sv_minigames.qh"
21
22 #include "../common/mutators/mutator/waypoints/waypointsprites.qh"
23 #include "../common/triggers/include.qh"
24
25 #include "weapons/weaponstats.qh"
26
27 #include "../common/animdecide.qh"
28
29 void Drop_Special_Items(entity player)
30 {
31         // called when the player has become stuck or frozen
32         // so objective items aren't stuck with the player
33
34         MUTATOR_CALLHOOK(DropSpecialItems, player);
35 }
36
37 void CopyBody_Think()
38 {SELFPARAM();
39         if(self.CopyBody_nextthink && time > self.CopyBody_nextthink)
40         {
41                 self.CopyBody_think();
42                 if(wasfreed(self))
43                         return;
44                 self.CopyBody_nextthink = self.nextthink;
45                 self.CopyBody_think = self.think;
46                 self.think = CopyBody_Think;
47         }
48         CSQCMODEL_AUTOUPDATE(self);
49         self.nextthink = time;
50 }
51 void CopyBody(float keepvelocity)
52 {SELFPARAM();
53         if (self.effects & EF_NODRAW)
54                 return;
55         setself(new(body));
56         self.enemy = this;
57         self.lip = this.lip;
58         self.colormap = this.colormap;
59         self.iscreature = this.iscreature;
60         self.teleportable = this.teleportable;
61         self.damagedbycontents = this.damagedbycontents;
62         self.angles = this.angles;
63         self.v_angle = this.v_angle;
64         self.avelocity = this.avelocity;
65         self.damageforcescale = this.damageforcescale;
66         self.effects = this.effects;
67         self.glowmod = this.glowmod;
68         self.event_damage = this.event_damage;
69         self.anim_state = this.anim_state;
70         self.anim_time = this.anim_time;
71         self.anim_lower_action = this.anim_lower_action;
72         self.anim_lower_time = this.anim_lower_time;
73         self.anim_upper_action = this.anim_upper_action;
74         self.anim_upper_time = this.anim_upper_time;
75         self.anim_implicit_state = this.anim_implicit_state;
76         self.anim_implicit_time = this.anim_implicit_time;
77         self.anim_lower_implicit_action = this.anim_lower_implicit_action;
78         self.anim_lower_implicit_time = this.anim_lower_implicit_time;
79         self.anim_upper_implicit_action = this.anim_upper_implicit_action;
80         self.anim_upper_implicit_time = this.anim_upper_implicit_time;
81         self.dphitcontentsmask = this.dphitcontentsmask;
82         self.death_time = this.death_time;
83         self.pain_finished = this.pain_finished;
84         self.health = this.health;
85         self.armorvalue = this.armorvalue;
86         self.armortype = this.armortype;
87         self.model = this.model;
88         self.modelindex = this.modelindex;
89         self.skin = this.skin;
90         self.species = this.species;
91         self.movetype = this.movetype;
92         self.solid = this.solid;
93         self.ballistics_density = this.ballistics_density;
94         self.takedamage = this.takedamage;
95         self.customizeentityforclient = this.customizeentityforclient;
96         self.uncustomizeentityforclient = this.uncustomizeentityforclient;
97         self.uncustomizeentityforclient_set = this.uncustomizeentityforclient_set;
98         if (keepvelocity == 1)
99                 self.velocity = this.velocity;
100         self.oldvelocity = self.velocity;
101         self.alpha = this.alpha;
102         self.fade_time = this.fade_time;
103         self.fade_rate = this.fade_rate;
104         //self.weapon = this.weapon;
105         setorigin(self, this.origin);
106         setsize(self, this.mins, this.maxs);
107         self.prevorigin = this.origin;
108         self.reset = SUB_Remove;
109
110         Drag_MoveDrag(this, self);
111
112         if(self.colormap <= maxclients && self.colormap > 0)
113                 self.colormap = 1024 + this.clientcolors;
114
115         CSQCMODEL_AUTOINIT(self);
116         self.CopyBody_nextthink = this.nextthink;
117         self.CopyBody_think = this.think;
118         self.nextthink = time;
119         self.think = CopyBody_Think;
120         // "bake" the current animation frame for clones (they don't get clientside animation)
121         animdecide_load_if_needed(self);
122         animdecide_setframes(self, false, frame, frame1time, frame2, frame2time);
123
124         setself(this);
125 }
126
127 void player_setupanimsformodel()
128 {SELFPARAM();
129         // load animation info
130         animdecide_load_if_needed(self);
131         animdecide_setstate(self, 0, false);
132 }
133
134 void player_anim ()
135 {SELFPARAM();
136         int deadbits = (self.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
137         if(self.deadflag) {
138                 if (!deadbits) {
139                         // Decide on which death animation to use.
140                         if(random() < 0.5)
141                                 deadbits = ANIMSTATE_DEAD1;
142                         else
143                                 deadbits = ANIMSTATE_DEAD2;
144                 }
145         } else {
146                 // Clear a previous death animation.
147                 deadbits = 0;
148         }
149         int animbits = deadbits;
150         if(self.frozen)
151                 animbits |= ANIMSTATE_FROZEN;
152         if(self.movetype == MOVETYPE_FOLLOW)
153                 animbits |= ANIMSTATE_FOLLOW;
154         if(self.crouch)
155                 animbits |= ANIMSTATE_DUCK;
156         animdecide_setstate(self, animbits, false);
157         animdecide_setimplicitstate(self, (self.flags & FL_ONGROUND));
158 }
159
160 void PlayerCorpseDamage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
161 {SELFPARAM();
162         float take, save;
163         vector v;
164         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
165
166         // damage resistance (ignore most of the damage from a bullet or similar)
167         damage = max(damage - 5, 1);
168
169         v = healtharmor_applydamage(self.armorvalue, autocvar_g_balance_armor_blockpercent, deathtype, damage);
170         take = v.x;
171         save = v.y;
172
173         if(sound_allowed(MSG_BROADCAST, attacker))
174         {
175                 if (save > 10)
176                         sound (self, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
177                 else if (take > 30)
178                         sound (self, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
179                 else if (take > 10)
180                         sound (self, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM);
181         }
182
183         if (take > 50)
184                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
185         if (take > 100)
186                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
187
188         self.armorvalue = self.armorvalue - save;
189         self.health = self.health - take;
190         // pause regeneration for 5 seconds
191         self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
192
193         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
194         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
195         self.dmg_inflictor = inflictor;
196
197         if (self.health <= -autocvar_sv_gibhealth && self.alpha >= 0)
198         {
199                 // don't use any animations as a gib
200                 self.frame = 0;
201                 // view just above the floor
202                 self.view_ofs = '0 0 4';
203
204                 Violence_GibSplash(self, 1, 1, attacker);
205                 self.alpha = -1;
206                 self.solid = SOLID_NOT; // restore later
207                 self.takedamage = DAMAGE_NO; // restore later
208                 self.damagedbycontents = false;
209         }
210 }
211
212 void calculate_player_respawn_time()
213 {SELFPARAM();
214         if(g_ca)
215                 return;
216
217         float gametype_setting_tmp;
218         float sdelay_max = GAMETYPE_DEFAULTED_SETTING(respawn_delay_max);
219         float sdelay_small = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small);
220         float sdelay_large = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large);
221         float sdelay_small_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small_count);
222         float sdelay_large_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large_count);
223         float waves = GAMETYPE_DEFAULTED_SETTING(respawn_waves);
224
225         float pcount = 1;  // Include myself whether or not team is already set right and I'm a "player".
226         entity pl;
227         if (teamplay)
228         {
229                 FOR_EACH_PLAYER(pl)
230                         if (pl != self)
231                                 if (pl.team == self.team)
232                                         ++pcount;
233                 if (sdelay_small_count == 0)
234                         sdelay_small_count = 1;
235                 if (sdelay_large_count == 0)
236                         sdelay_large_count = 1;
237         }
238         else
239         {
240                 FOR_EACH_PLAYER(pl)
241                         if (pl != self)
242                                 ++pcount;
243                 if (sdelay_small_count == 0)
244                 {
245                         if (g_cts)
246                         {
247                                 // Players play independently. No point in requiring enemies.
248                                 sdelay_small_count = 1;
249                         }
250                         else
251                         {
252                                 // Players play AGAINST each other. Enemies required.
253                                 sdelay_small_count = 2;
254                         }
255                 }
256                 if (sdelay_large_count == 0)
257                 {
258                         if (g_cts)
259                         {
260                                 // Players play independently. No point in requiring enemies.
261                                 sdelay_large_count = 1;
262                         }
263                         else
264                         {
265                                 // Players play AGAINST each other. Enemies required.
266                                 sdelay_large_count = 2;
267                         }
268                 }
269         }
270
271         float sdelay;
272
273         if (pcount <= sdelay_small_count)
274                 sdelay = sdelay_small;
275         else if (pcount >= sdelay_large_count)
276                 sdelay = sdelay_large;
277         else  // NOTE: this case implies sdelay_large_count > sdelay_small_count.
278                 sdelay = sdelay_small + (sdelay_large - sdelay_small) * (pcount - sdelay_small_count) / (sdelay_large_count - sdelay_small_count);
279
280         if(waves)
281                 self.respawn_time = ceil((time + sdelay) / waves) * waves;
282         else
283                 self.respawn_time = time + sdelay;
284
285         if(sdelay < sdelay_max)
286                 self.respawn_time_max = time + sdelay_max;
287         else
288                 self.respawn_time_max = self.respawn_time;
289
290         if((sdelay + waves >= 5.0) && (self.respawn_time - time > 1.75))
291                 self.respawn_countdown = 10; // first number to count down from is 10
292         else
293                 self.respawn_countdown = -1; // do not count down
294
295         if(autocvar_g_forced_respawn)
296                 self.respawn_flags = self.respawn_flags | RESPAWN_FORCE;
297 }
298
299 void PlayerDamage (entity inflictor, entity attacker, float damage, int deathtype, vector hitloc, vector force)
300 {SELFPARAM();
301         float take, save, dh, da;
302         int j;
303         vector v;
304         float valid_damage_for_weaponstats;
305         float excess;
306
307         dh = max(self.health, 0);
308         da = max(self.armorvalue, 0);
309
310         if(!DEATH_ISSPECIAL(deathtype))
311         {
312                 damage *= sqrt(bound(1.0, self.cvar_cl_handicap, 100.0));
313                 if(self != attacker)
314                         damage /= sqrt(bound(1.0, attacker.cvar_cl_handicap, 100.0));
315         }
316
317         if(DEATH_ISWEAPON(deathtype, WEP_TUBA))
318         {
319                 // tuba causes blood to come out of the ears
320                 vector ear1, ear2;
321                 vector d;
322                 float f;
323                 ear1 = self.origin;
324                 ear1_z += 0.125 * self.view_ofs.z + 0.875 * self.maxs.z; // 7/8
325                 ear2 = ear1;
326                 makevectors(self.angles);
327                 ear1 += v_right * -10;
328                 ear2 += v_right * +10;
329                 d = inflictor.origin - self.origin;
330                 if (d)
331                         f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
332                 else
333                         f = 0;  // Assum ecenter.
334                 force = v_right * vlen(force);
335                 Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), self, attacker);
336                 Violence_GibSplash_At(ear2, force,      2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), self, attacker);
337                 if(f > 0)
338                 {
339                         hitloc = ear1;
340                         force = force * -1;
341                 }
342                 else
343                 {
344                         hitloc = ear2;
345                         // force is already good
346                 }
347         }
348         else
349                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
350
351
352         v = healtharmor_applydamage(self.armorvalue, autocvar_g_balance_armor_blockpercent, deathtype, damage);
353         take = v.x;
354         save = v.y;
355
356         if(attacker == self)
357         {
358                 // don't reset pushltime for self damage as it may be an attempt to
359                 // escape a lava pit or similar
360                 //self.pushltime = 0;
361                 self.istypefrag = 0;
362         }
363         else if(IS_PLAYER(attacker))
364         {
365                 self.pusher = attacker;
366                 self.pushltime = time + autocvar_g_maxpushtime;
367                 self.istypefrag = self.BUTTON_CHAT;
368         }
369         else if(time < self.pushltime)
370         {
371                 attacker = self.pusher;
372                 self.pushltime = max(self.pushltime, time + 0.6);
373         }
374         else
375         {
376                 self.pushltime = 0;
377                 self.istypefrag = 0;
378         }
379
380         frag_damage = damage;
381         MUTATOR_CALLHOOK(PlayerDamage_SplitHealthArmor, inflictor, attacker, self, force, take, save);
382         take = bound(0, damage_take, self.health);
383         save = bound(0, damage_save, self.armorvalue);
384         excess = max(0, damage - take - save);
385
386         if(sound_allowed(MSG_BROADCAST, attacker))
387         {
388                 if (save > 10)
389                         sound (self, CH_SHOTS, SND_ARMORIMPACT, VOL_BASE, ATTEN_NORM);
390                 else if (take > 30)
391                         sound (self, CH_SHOTS, SND_BODYIMPACT2, VOL_BASE, ATTEN_NORM);
392                 else if (take > 10)
393                         sound (self, CH_SHOTS, SND_BODYIMPACT1, VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
394         }
395
396         if (take > 50)
397                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
398         if (take > 100)
399                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
400
401         if (time >= self.spawnshieldtime)
402         {
403                 if (!(self.flags & FL_GODMODE))
404                 {
405                         self.armorvalue = self.armorvalue - save;
406                         self.health = self.health - take;
407                         // pause regeneration for 5 seconds
408                         if(take)
409                                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
410
411                         if (time > self.pain_finished)          //Don't switch pain sequences like crazy
412                         {
413                                 self.pain_finished = time + 0.5;        //Supajoe
414
415                                 if(autocvar_sv_gentle < 1) {
416                                         if(self.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
417                                         {
418                                                 if (!self.animstate_override)
419                                                 {
420                                                         if (random() > 0.5)
421                                                                 animdecide_setaction(self, ANIMACTION_PAIN1, true);
422                                                         else
423                                                                 animdecide_setaction(self, ANIMACTION_PAIN2, true);
424                                                 }
425                                         }
426
427                                         if(sound_allowed(MSG_BROADCAST, attacker))
428                                         if((self.health < 2 * WEP_CVAR_PRI(blaster, damage) * autocvar_g_balance_selfdamagepercent + 1) || !(DEATH_WEAPONOF(deathtype).spawnflags & WEP_FLAG_CANCLIMB) || attacker != self) // WEAPONTODO: create separate limit for pain notification with laser
429                                         if(self.health > 1)
430                                         // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
431                                         {
432                                                 if(deathtype == DEATH_FALL.m_id)
433                                                         PlayerSound(playersound_fall, CH_PAIN, VOICETYPE_PLAYERSOUND);
434                                                 else if(self.health > 75) // TODO make a "gentle" version?
435                                                         PlayerSound(playersound_pain100, CH_PAIN, VOICETYPE_PLAYERSOUND);
436                                                 else if(self.health > 50)
437                                                         PlayerSound(playersound_pain75, CH_PAIN, VOICETYPE_PLAYERSOUND);
438                                                 else if(self.health > 25)
439                                                         PlayerSound(playersound_pain50, CH_PAIN, VOICETYPE_PLAYERSOUND);
440                                                 else
441                                                         PlayerSound(playersound_pain25, CH_PAIN, VOICETYPE_PLAYERSOUND);
442                                         }
443                                 }
444                         }
445
446                         // throw off bot aim temporarily
447                         float shake;
448                         if(IS_BOT_CLIENT(self) && self.health >= 1)
449                         {
450                                 shake = damage * 5 / (bound(0,skill,100) + 1);
451                                 self.v_angle_x = self.v_angle.x + (random() * 2 - 1) * shake;
452                                 self.v_angle_y = self.v_angle.y + (random() * 2 - 1) * shake;
453                                 self.v_angle_x = bound(-90, self.v_angle.x, 90);
454                         }
455                 }
456                 else
457                         self.max_armorvalue += (save + take);
458         }
459         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
460         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
461         self.dmg_inflictor = inflictor;
462
463         if (self != attacker) {
464                 float realdmg = damage - excess;
465                 if (IS_PLAYER(attacker)) {
466                         PlayerScore_Add(attacker, SP_DMG, realdmg);
467                 }
468                 if (IS_PLAYER(self)) {
469                         PlayerScore_Add(self, SP_DMGTAKEN, realdmg);
470                 }
471         }
472
473         bool abot = (IS_BOT_CLIENT(attacker));
474         bool vbot = (IS_BOT_CLIENT(self));
475
476         valid_damage_for_weaponstats = 0;
477         Weapon awep = WEP_Null;
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 = get_weaponinfo(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.m_id, abot, self.weapon, vbot, dh + da);
496                 MUTATOR_CALLHOOK(PlayerDamaged, attacker, self, dh, da, hitloc, deathtype);
497         }
498
499         if (self.health < 1)
500         {
501                 float defer_ClientKill_Now_TeamChange;
502                 defer_ClientKill_Now_TeamChange = false;
503
504                 if(self.alivetime)
505                 {
506                         PS_GR_P_ADDVAL(self, PLAYERSTATS_ALIVETIME, time - self.alivetime);
507                         self.alivetime = 0;
508                 }
509
510                 if(valid_damage_for_weaponstats)
511                         WeaponStats_LogKill(awep.m_id, abot, self.weapon, vbot);
512
513                 if(autocvar_sv_gentle < 1) // TODO make a "gentle" version?
514                 if(sound_allowed(MSG_BROADCAST, attacker))
515                 {
516                         if(deathtype == DEATH_DROWN.m_id)
517                                 PlayerSound(playersound_drown, CH_PAIN, VOICETYPE_PLAYERSOUND);
518                         else
519                                 PlayerSound(playersound_death, CH_PAIN, VOICETYPE_PLAYERSOUND);
520                 }
521
522                 // get rid of kill indicator
523                 if(self.killindicator)
524                 {
525                         remove(self.killindicator);
526                         self.killindicator = world;
527                         if(self.killindicator_teamchange)
528                                 defer_ClientKill_Now_TeamChange = true;
529
530                         if(self.classname == "body")
531                         if(deathtype == DEATH_KILL.m_id)
532                         {
533                                 // for the lemmings fans, a small harmless explosion
534                                 Send_Effect(EFFECT_ROCKET_EXPLODE, self.origin, '0 0 0', 1);
535                         }
536                 }
537
538                 // print an obituary message
539                 if(self.classname != "body")
540                         Obituary (attacker, inflictor, self, deathtype);
541
542         // increment frag counter for used weapon type
543         Weapon w = DEATH_WEAPONOF(deathtype);
544         if(w != WEP_Null)
545         if(accuracy_isgooddamage(attacker, self))
546         attacker.accuracy.(accuracy_frags[w.m_id-1]) += 1;
547
548                 MUTATOR_CALLHOOK(PlayerDies, inflictor, attacker, self, deathtype);
549                 excess = frag_damage;
550
551                 Weapon wep = get_weaponinfo(self.weapon);
552                 wep.wr_playerdeath(wep);
553
554                 RemoveGrapplingHook(self);
555
556                 Portal_ClearAllLater(self);
557
558                 self.fixangle = true;
559
560                 if(defer_ClientKill_Now_TeamChange)
561                         ClientKill_Now_TeamChange(); // can turn player into spectator
562
563                 // player could have been miraculously resuscitated ;)
564                 // e.g. players in freezetag get frozen, they don't really die
565                 if(self.health >= 1 || !(IS_PLAYER(self) || self.classname == "body"))
566                         return;
567
568                 // when we get here, player actually dies
569
570                 Unfreeze(self); // remove any icy remains
571                 self.health = 0; // Unfreeze resets health, so we need to set it back
572
573                 // clear waypoints
574                 WaypointSprite_PlayerDead();
575                 // throw a weapon
576                 SpawnThrownWeapon (self.origin + (self.mins + self.maxs) * 0.5, self.switchweapon);
577
578                 // become fully visible
579                 self.alpha = default_player_alpha;
580                 // make the corpse upright (not tilted)
581                 self.angles_x = 0;
582                 self.angles_z = 0;
583                 // don't spin
584                 self.avelocity = '0 0 0';
585                 // view from the floor
586                 self.view_ofs = '0 0 -8';
587                 // toss the corpse
588                 self.movetype = MOVETYPE_TOSS;
589                 // shootable corpse
590                 self.solid = SOLID_CORPSE;
591                 self.ballistics_density = autocvar_g_ballistics_density_corpse;
592                 // don't stick to the floor
593                 self.flags &= ~FL_ONGROUND;
594                 // dying animation
595                 self.deadflag = DEAD_DYING;
596
597                 // when to allow respawn
598                 calculate_player_respawn_time();
599
600                 self.death_time = time;
601                 if (random() < 0.5)
602                         animdecide_setstate(self, self.anim_state | ANIMSTATE_DEAD1, true);
603                 else
604                         animdecide_setstate(self, self.anim_state | ANIMSTATE_DEAD2, true);
605                 if (self.maxs.z > 5)
606                 {
607                         self.maxs_z = 5;
608                         setsize(self, self.mins, self.maxs);
609                 }
610                 // set damage function to corpse damage
611                 self.event_damage = PlayerCorpseDamage;
612                 // call the corpse damage function just in case it wants to gib
613                 self.event_damage(inflictor, attacker, excess, deathtype, hitloc, force);
614
615                 // set up to fade out later
616                 SUB_SetFade (self, time + 6 + random (), 1);
617                 // reset body think wrapper broken by SUB_SetFade
618                 if(self.classname == "body" && self.think != CopyBody_Think) {
619                         self.CopyBody_think = self.think;
620                         self.CopyBody_nextthink = self.nextthink;
621                         self.think = CopyBody_Think;
622                         self.nextthink = time;
623                 }
624
625                 if(autocvar_sv_gentle > 0 || autocvar_ekg || self.classname == "body") {
626                         // remove corpse
627                         // clones don't run any animation code any more, so we must gib them when they die :(
628                         PlayerCorpseDamage (inflictor, attacker, autocvar_sv_gibhealth+1.0, deathtype, hitloc, force);
629                 }
630
631                 // reset fields the weapons may use just in case
632                 for (j = WEP_FIRST; j <= WEP_LAST; ++j)
633                 {
634                         Weapon w = get_weaponinfo(j);
635                         w.wr_resetplayer(w);
636                         for (int slot = 0; slot < MAX_WEAPONSLOTS; ++slot)
637                         {
638                                 ATTACK_FINISHED_FOR(self, j, slot) = 0;
639                         }
640                 }
641         }
642 }
643
644 void MoveToTeam(entity client, int team_colour, int type)
645 {
646         int lockteams_backup = lockteams;  // backup any team lock
647         lockteams = 0;  // disable locked teams
648         TeamchangeFrags(client);  // move the players frags
649         SetPlayerColors(client, team_colour - 1);  // set the players colour
650         Damage(client, client, client, 100000, DEATH_AUTOTEAMCHANGE.m_id, client.origin, '0 0 0');  // kill the player
651         lockteams = lockteams_backup;  // restore the team lock
652         LogTeamchange(client.playerid, client.team, type);
653 }
654
655 /**
656  * message "": do not say, just test flood control
657  * return value:
658  *   1 = accept
659  *   0 = reject
660  *  -1 = fake accept
661  */
662 int Say(entity source, float teamsay, entity privatesay, string msgin, bool floodcontrol)
663 {
664         string msgstr, colorstr, cmsgstr, namestr, fullmsgstr, sourcemsgstr, fullcmsgstr, sourcecmsgstr, colorprefix;
665         float flood;
666         var .float flood_field;
667         entity head;
668         float ret;
669         string privatemsgprefix = string_null; float privatemsgprefixlen = 0;
670
671         if(!teamsay && !privatesay)
672                 if(substring(msgin, 0, 1) == " ")
673                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
674
675         msgin = formatmessage(msgin);
676
677         if (!IS_PLAYER(source))
678                 colorstr = "^0"; // black for spectators
679         else if(teamplay)
680                 colorstr = Team_ColorCode(source.team);
681         else
682         {
683                 colorstr = "";
684                 teamsay = false;
685         }
686
687         if(intermission_running)
688                 teamsay = false;
689
690         if(msgin != "")
691                 msgin = trigger_magicear_processmessage_forallears(source, teamsay, privatesay, msgin);
692
693         /*
694          * using bprint solves this... me stupid
695         // how can we prevent the message from appearing in a listen server?
696         // for now, just give "say" back and only handle say_team
697         if(!teamsay)
698         {
699                 clientcommand(self, strcat("say ", msgin));
700                 return;
701         }
702         */
703
704         if(autocvar_g_chat_teamcolors)
705                 namestr = playername(source);
706         else
707                 namestr = source.netname;
708
709         if(strdecolorize(namestr) == namestr)
710                 colorprefix = "^3";
711         else
712                 colorprefix = "^7";
713
714         if(msgin != "")
715         {
716                 if(privatesay)
717                 {
718                         msgstr = strcat("\{1}\{13}* ", colorprefix, namestr, "^3 tells you: ^7");
719                         privatemsgprefixlen = strlen(msgstr);
720                         msgstr = strcat(msgstr, msgin);
721                         cmsgstr = strcat(colorstr, colorprefix, namestr, "^3 tells you:\n^7", msgin);
722                         if(autocvar_g_chat_teamcolors)
723                                 privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", playername(privatesay), ": ^7");
724                         else
725                                 privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", privatesay.netname, ": ^7");
726                 }
727                 else if(teamsay)
728                 {
729                         if(strstrofs(msgin, "/me", 0) >= 0)
730                         {
731                                 //msgin = strreplace("/me", "", msgin);
732                                 //msgin = substring(msgin, 3, strlen(msgin));
733                                 msgin = strreplace("/me", strcat(colorstr, "(", colorprefix, namestr, colorstr, ")^7"), msgin);
734                                 msgstr = strcat("\{1}\{13}^4* ", "^7", msgin);
735                         }
736                         else
737                                 msgstr = strcat("\{1}\{13}", colorstr, "(", colorprefix, namestr, colorstr, ") ^7", msgin);
738                         cmsgstr = strcat(colorstr, "(", colorprefix, namestr, colorstr, ")\n^7", msgin);
739                 }
740                 else
741                 {
742                         if(strstrofs(msgin, "/me", 0) >= 0)
743                         {
744                                 //msgin = strreplace("/me", "", msgin);
745                                 //msgin = substring(msgin, 3, strlen(msgin));
746                                 msgin = strreplace("/me", strcat(colorprefix, namestr), msgin);
747                                 msgstr = strcat("\{1}^4* ", "^7", msgin);
748                         }
749                         else
750                                 msgstr = strcat("\{1}", colorprefix, namestr, "^7: ", msgin);
751                         cmsgstr = "";
752                 }
753                 msgstr = strcat(strreplace("\n", " ", msgstr), "\n"); // newlines only are good for centerprint
754         }
755         else
756         {
757                 msgstr = cmsgstr = "";
758         }
759
760         fullmsgstr = msgstr;
761         fullcmsgstr = cmsgstr;
762
763         // FLOOD CONTROL
764         flood = 0;
765         flood_field = floodcontrol_chat;
766         if(floodcontrol)
767         {
768                 float flood_spl;
769                 float flood_burst;
770                 float flood_lmax;
771                 float lines;
772                 if(privatesay)
773                 {
774                         flood_spl = autocvar_g_chat_flood_spl_tell;
775                         flood_burst = autocvar_g_chat_flood_burst_tell;
776                         flood_lmax = autocvar_g_chat_flood_lmax_tell;
777                         flood_field = floodcontrol_chattell;
778                 }
779                 else if(teamsay)
780                 {
781                         flood_spl = autocvar_g_chat_flood_spl_team;
782                         flood_burst = autocvar_g_chat_flood_burst_team;
783                         flood_lmax = autocvar_g_chat_flood_lmax_team;
784                         flood_field = floodcontrol_chatteam;
785                 }
786                 else
787                 {
788                         flood_spl = autocvar_g_chat_flood_spl;
789                         flood_burst = autocvar_g_chat_flood_burst;
790                         flood_lmax = autocvar_g_chat_flood_lmax;
791                         flood_field = floodcontrol_chat;
792                 }
793                 flood_burst = max(0, flood_burst - 1);
794                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
795
796                 // do flood control for the default line size
797                 if(msgstr != "")
798                 {
799                         getWrappedLine_remaining = msgstr;
800                         msgstr = "";
801                         lines = 0;
802                         while(getWrappedLine_remaining && (!flood_lmax || lines <= flood_lmax))
803                         {
804                                 msgstr = strcat(msgstr, " ", getWrappedLineLen(82.4289758859709, strlennocol)); // perl averagewidth.pl < gfx/vera-sans.width
805                                 ++lines;
806                         }
807                         msgstr = substring(msgstr, 1, strlen(msgstr) - 1);
808
809                         if(getWrappedLine_remaining != "")
810                         {
811                                 msgstr = strcat(msgstr, "\n");
812                                 flood = 2;
813                         }
814
815                         if (time >= source.(flood_field))
816                         {
817                                 source.(flood_field) = max(time - flood_burst * flood_spl, source.(flood_field)) + lines * flood_spl;
818                         }
819                         else
820                         {
821                                 flood = 1;
822                                 msgstr = fullmsgstr;
823                         }
824                 }
825                 else
826                 {
827                         if (time >= source.(flood_field))
828                                 source.(flood_field) = max(time - flood_burst * flood_spl, source.(flood_field)) + flood_spl;
829                         else
830                                 flood = 1;
831                 }
832
833                 if (timeout_status == TIMEOUT_ACTIVE) // when game is paused, no flood protection
834                         source.(flood_field) = flood = 0;
835         }
836
837         if(flood == 2) // cannot happen for empty msgstr
838         {
839                 if(autocvar_g_chat_flood_notify_flooder)
840                 {
841                         sourcemsgstr = strcat(msgstr, "\n^3FLOOD CONTROL: ^7message too long, trimmed\n");
842                         sourcecmsgstr = "";
843                 }
844                 else
845                 {
846                         sourcemsgstr = fullmsgstr;
847                         sourcecmsgstr = fullcmsgstr;
848                 }
849                 cmsgstr = "";
850         }
851         else
852         {
853                 sourcemsgstr = msgstr;
854                 sourcecmsgstr = cmsgstr;
855         }
856
857         if(!privatesay)
858         if (!IS_PLAYER(source))
859         {
860                 if (!intermission_running)
861                         if(teamsay || (autocvar_g_chat_nospectators == 1) || (autocvar_g_chat_nospectators == 2 && !(warmup_stage || gameover)))
862                                 teamsay = -1; // spectators
863         }
864
865         if(flood)
866                 LOG_INFO("NOTE: ", playername(source), "^7 is flooding.\n");
867
868         // build sourcemsgstr by cutting off a prefix and replacing it by the other one
869         if(privatesay)
870                 sourcemsgstr = strcat(privatemsgprefix, substring(sourcemsgstr, privatemsgprefixlen, -1));
871
872         if(source.muted)
873         {
874                 // always fake the message
875                 ret = -1;
876         }
877         else if(flood == 1)
878         {
879                 if (autocvar_g_chat_flood_notify_flooder)
880                 {
881                         sprint(source, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(source.(flood_field) - time), "^3 seconds\n"));
882                         ret = 0;
883                 }
884                 else
885                         ret = -1;
886         }
887         else
888         {
889                 ret = 1;
890         }
891
892         if(sourcemsgstr != "" && ret != 0)
893         {
894                 if(ret < 0) // faked message, because the player is muted
895                 {
896                         sprint(source, sourcemsgstr);
897                         if(sourcecmsgstr != "" && !privatesay)
898                                 centerprint(source, sourcecmsgstr);
899                 }
900                 else if(privatesay) // private message, between 2 people only
901                 {
902                         sprint(source, sourcemsgstr);
903                         sprint(privatesay, msgstr);
904                         if (!autocvar_g_chat_tellprivacy) { dedicated_print(msgstr); } // send to server console too if "tellprivacy" is disabled
905                         if(cmsgstr != "")
906                                 centerprint(privatesay, cmsgstr);
907                 }
908                 else if ( teamsay && source.active_minigame )
909                 {
910                         sprint(source, sourcemsgstr);
911                         dedicated_print(msgstr); // send to server console too
912                         FOR_EACH_REALCLIENT(head)
913                                 if(head != source)
914                                 if(head.active_minigame == source.active_minigame)
915                                         sprint(head, msgstr);
916                 }
917                 else if(teamsay > 0) // team message, only sent to team mates
918                 {
919                         sprint(source, sourcemsgstr);
920                         dedicated_print(msgstr); // send to server console too
921                         if(sourcecmsgstr != "")
922                                 centerprint(source, sourcecmsgstr);
923                         FOR_EACH_REALPLAYER(head) if(head.team == source.team)
924                                 if(head != source)
925                                 {
926                                         sprint(head, msgstr);
927                                         if(cmsgstr != "")
928                                                 centerprint(head, cmsgstr);
929                                 }
930                 }
931                 else if(teamsay < 0) // spectator message, only sent to spectators
932                 {
933                         sprint(source, sourcemsgstr);
934                         dedicated_print(msgstr); // send to server console too
935                         FOR_EACH_REALCLIENT(head) if (!IS_PLAYER(head))
936                                 if(head != source)
937                                         sprint(head, msgstr);
938                 }
939                 else if(sourcemsgstr != msgstr) // trimmed/server fixed message, sent to all players
940                 {
941                         sprint(source, sourcemsgstr);
942                         dedicated_print(msgstr); // send to server console too
943                         FOR_EACH_REALCLIENT(head)
944                                 if(head != source)
945                                         sprint(head, msgstr);
946                 }
947                 else
948                         bprint(msgstr); // entirely normal message, sent to all players -- bprint sends to server console too.
949         }
950
951         return ret;
952 }