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