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