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