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