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