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