]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/cl_player.qc
Merge remote-tracking branch 'origin/master' into samual/weapons
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / cl_player.qc
1 .entity pusher;
2 .float pushltime;
3 .float istypefrag;
4
5 .float CopyBody_nextthink;
6 .void(void) CopyBody_think;
7 void CopyBody_Think(void)
8 {
9         if(self.CopyBody_nextthink && time > self.CopyBody_nextthink)
10         {
11                 self.CopyBody_think();
12                 if(wasfreed(self))
13                         return;
14                 self.CopyBody_nextthink = self.nextthink;
15                 self.CopyBody_think = self.think;
16                 self.think = CopyBody_Think;
17         }
18         CSQCMODEL_AUTOUPDATE();
19         self.nextthink = time;
20 }
21 void CopyBody(float keepvelocity)
22 {
23         entity oldself;
24         if (self.effects & EF_NODRAW)
25                 return;
26         oldself = self;
27         self = spawn();
28         self.enemy = oldself;
29         self.lip = oldself.lip;
30         self.colormap = oldself.colormap;
31         self.iscreature = oldself.iscreature;
32         self.teleportable = oldself.teleportable;
33         self.damagedbycontents = oldself.damagedbycontents;
34         self.angles = oldself.angles;
35         self.v_angle = oldself.v_angle;
36         self.avelocity = oldself.avelocity;
37         self.classname = "body";
38         self.damageforcescale = oldself.damageforcescale;
39         self.effects = oldself.effects;
40         self.glowmod = oldself.glowmod;
41         self.event_damage = oldself.event_damage;
42         self.anim_state = oldself.anim_state;
43         self.anim_time = oldself.anim_time;
44         self.anim_lower_action = oldself.anim_lower_action;
45         self.anim_lower_time = oldself.anim_lower_time;
46         self.anim_upper_action = oldself.anim_upper_action;
47         self.anim_upper_time = oldself.anim_upper_time;
48         self.anim_implicit_state = oldself.anim_implicit_state;
49         self.anim_implicit_time = oldself.anim_implicit_time;
50         self.anim_lower_implicit_action = oldself.anim_lower_implicit_action;
51         self.anim_lower_implicit_time = oldself.anim_lower_implicit_time;
52         self.anim_upper_implicit_action = oldself.anim_upper_implicit_action;
53         self.anim_upper_implicit_time = oldself.anim_upper_implicit_time;
54         self.dphitcontentsmask = oldself.dphitcontentsmask;
55         self.death_time = oldself.death_time;
56         self.pain_finished = oldself.pain_finished;
57         self.health = oldself.health;
58         self.armorvalue = oldself.armorvalue;
59         self.armortype = oldself.armortype;
60         self.model = oldself.model;
61         self.modelindex = oldself.modelindex;
62         self.skin = oldself.skin;
63         self.species = oldself.species;
64         self.movetype = oldself.movetype;
65         self.solid = oldself.solid;
66         self.ballistics_density = oldself.ballistics_density;
67         self.takedamage = oldself.takedamage;
68         self.customizeentityforclient = oldself.customizeentityforclient;
69         self.uncustomizeentityforclient = oldself.uncustomizeentityforclient;
70         self.uncustomizeentityforclient_set = oldself.uncustomizeentityforclient_set;
71         if (keepvelocity == 1)
72                 self.velocity = oldself.velocity;
73         self.oldvelocity = self.velocity;
74         self.alpha = oldself.alpha;
75         self.fade_time = oldself.fade_time;
76         self.fade_rate = oldself.fade_rate;
77         //self.weapon = oldself.weapon;
78         setorigin(self, oldself.origin);
79         setsize(self, oldself.mins, oldself.maxs);
80         self.prevorigin = oldself.origin;
81         self.reset = SUB_Remove;
82
83         Drag_MoveDrag(oldself, self);
84
85         if(self.colormap <= maxclients && self.colormap > 0)
86                 self.colormap = 1024 + oldself.clientcolors;
87
88         CSQCMODEL_AUTOINIT();
89         self.CopyBody_nextthink = oldself.nextthink;
90         self.CopyBody_think = oldself.think;
91         self.nextthink = time;
92         self.think = CopyBody_Think;
93         // "bake" the current animation frame for clones (they don't get clientside animation)
94         animdecide_setframes(self, FALSE, frame, frame1time, frame2, frame2time);
95
96         self = oldself;
97 }
98
99 float player_getspecies()
100 {
101         float s;
102         get_model_parameters(self.model, self.skin);
103         s = get_model_parameters_species;
104         get_model_parameters(string_null, 0);
105         if(s < 0)
106                 return SPECIES_HUMAN;
107         return s;
108 }
109
110 void player_setupanimsformodel()
111 {
112         // load animation info
113         animdecide_init(self);
114         animdecide_setstate(self, 0, FALSE);
115 }
116
117 void player_anim (void)
118 {
119         float deadbits = (self.anim_state & (ANIMSTATE_DEAD1 | ANIMSTATE_DEAD2));
120         if(self.deadflag && !deadbits)
121                 if(random() < 0.5)
122                         deadbits = ANIMSTATE_DEAD1;
123                 else
124                         deadbits = ANIMSTATE_DEAD2;
125         float animbits = deadbits;
126         if(self.freezetag_frozen)
127                 animbits |= ANIMSTATE_FROZEN;
128         if(self.crouch)
129                 animbits |= ANIMSTATE_DUCK;
130         animdecide_setstate(self, animbits, FALSE);
131         animdecide_setimplicitstate(self, (self.flags & FL_ONGROUND));
132
133         if (self.weaponentity)
134         {
135                 updateanim(self.weaponentity);
136                 if (!self.weaponentity.animstate_override)
137                         setanim(self.weaponentity, self.weaponentity.anim_idle, TRUE, FALSE, FALSE);
138         }
139 }
140
141 void PlayerCorpseDamage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
142 {
143         float take, save;
144         vector v;
145         Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
146
147         // damage resistance (ignore most of the damage from a bullet or similar)
148         damage = max(damage - 5, 1);
149
150         v = healtharmor_applydamage(self.armorvalue, autocvar_g_balance_armor_blockpercent, deathtype, damage);
151         take = v_x;
152         save = v_y;
153
154         if(sound_allowed(MSG_BROADCAST, attacker))
155         {
156                 if (save > 10)
157                         sound (self, CH_SHOTS, "misc/armorimpact.wav", VOL_BASE, ATTEN_NORM);
158                 else if (take > 30)
159                         sound (self, CH_SHOTS, "misc/bodyimpact2.wav", VOL_BASE, ATTEN_NORM);
160                 else if (take > 10)
161                         sound (self, CH_SHOTS, "misc/bodyimpact1.wav", VOL_BASE, ATTEN_NORM);
162         }
163
164         if (take > 50)
165                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
166         if (take > 100)
167                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
168
169         if (!(self.flags & FL_GODMODE))
170         {
171                 self.armorvalue = self.armorvalue - save;
172                 self.health = self.health - take;
173                 // pause regeneration for 5 seconds
174                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
175         }
176         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
177         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
178         self.dmg_inflictor = inflictor;
179
180         if (self.health <= -autocvar_sv_gibhealth && self.alpha >= 0)
181         {
182                 // don't use any animations as a gib
183                 self.frame = 0;
184                 // view just above the floor
185                 self.view_ofs = '0 0 4';
186
187                 Violence_GibSplash(self, 1, 1, attacker);
188                 self.alpha = -1;
189                 self.solid = SOLID_NOT; // restore later
190                 self.takedamage = DAMAGE_NO; // restore later
191                 self.damagedbycontents = FALSE;
192         }
193 }
194
195 // g_<gametype>_str:
196 // If 0, default is used.
197 // If <0, 0 is used.
198 // Otherwise, g_str (default value) is used.
199 // For consistency, negative values there are mapped to zero too.
200 #define GAMETYPE_DEFAULTED_SETTING(str) \
201         ((gametype_setting_tmp = cvar(strcat("g_", GetGametype(), "_" #str))), \
202          (gametype_setting_tmp < 0) ? 0 : \
203          (gametype_setting_tmp == 0) ? max(0, autocvar_g_##str) : \
204          gametype_setting_tmp)
205
206
207 void calculate_player_respawn_time()
208 {
209         float gametype_setting_tmp;
210         float sdelay_max = GAMETYPE_DEFAULTED_SETTING(respawn_delay_max);
211         float sdelay_small = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small);
212         float sdelay_large = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large);
213         float sdelay_small_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_small_count);
214         float sdelay_large_count = GAMETYPE_DEFAULTED_SETTING(respawn_delay_large_count);
215         float waves = GAMETYPE_DEFAULTED_SETTING(respawn_waves);
216
217         float pcount = 1;  // Include myself whether or not team is already set right and I'm a "player".
218         entity pl;
219         if (teamplay)
220         {
221                 FOR_EACH_PLAYER(pl)
222                         if (pl != self)
223                                 if (pl.team == self.team)
224                                         ++pcount;
225                 if (sdelay_small_count == 0)
226                         sdelay_small_count = 1;
227                 if (sdelay_large_count == 0)
228                         sdelay_large_count = 1;
229         }
230         else
231         {
232                 FOR_EACH_PLAYER(pl)
233                         if (pl != self)
234                                 ++pcount;
235                 if (sdelay_small_count == 0)
236                 {
237                         if (g_cts)
238                         {
239                                 // Players play independently. No point in requiring enemies.
240                                 sdelay_small_count = 1;
241                         }
242                         else
243                         {
244                                 // Players play AGAINST each other. Enemies required.
245                                 sdelay_small_count = 2;
246                         }
247                 }
248                 if (sdelay_large_count == 0)
249                 {
250                         if (g_cts)
251                         {
252                                 // Players play independently. No point in requiring enemies.
253                                 sdelay_large_count = 1;
254                         }
255                         else
256                         {
257                                 // Players play AGAINST each other. Enemies required.
258                                 sdelay_large_count = 2;
259                         }
260                 }
261         }
262
263         float sdelay;
264
265         if (pcount <= sdelay_small_count)
266                 sdelay = sdelay_small;
267         else if (pcount >= sdelay_large_count)
268                 sdelay = sdelay_large;
269         else  // NOTE: this case implies sdelay_large_count > sdelay_small_count.
270                 sdelay = sdelay_small + (sdelay_large - sdelay_small) * (pcount - sdelay_small_count) / (sdelay_large_count - sdelay_small_count);
271
272         if(waves)
273                 self.respawn_time = ceil((time + sdelay) / waves) * waves;
274         else
275                 self.respawn_time = time + sdelay;
276
277         if(sdelay < sdelay_max)
278                 self.respawn_time_max = time + sdelay_max;
279         else
280                 self.respawn_time_max = self.respawn_time;
281
282         if((sdelay + waves >= 5.0) && (self.respawn_time - time > 1.75))
283                 self.respawn_countdown = 10; // first number to count down from is 10
284         else
285                 self.respawn_countdown = -1; // do not count down
286
287         if(g_cts || autocvar_g_forced_respawn)
288                 self.respawn_flags = self.respawn_flags | RESPAWN_FORCE;
289 }
290
291 void ClientKill_Now_TeamChange();
292
293 void PlayerDamage (entity inflictor, entity attacker, float damage, float deathtype, vector hitloc, vector force)
294 {
295         float take, save, dh, da, j;
296         vector v;
297         float valid_damage_for_weaponstats;
298         float excess;
299
300         dh = max(self.health, 0);
301         da = max(self.armorvalue, 0);
302
303         if(!DEATH_ISSPECIAL(deathtype))
304         {
305                 damage *= sqrt(bound(1.0, self.cvar_cl_handicap, 100.0));
306                 if(self != attacker)
307                         damage /= sqrt(bound(1.0, attacker.cvar_cl_handicap, 100.0));
308         }
309
310         if(DEATH_ISWEAPON(deathtype, WEP_TUBA))
311         {
312                 // tuba causes blood to come out of the ears
313                 vector ear1, ear2;
314                 vector d;
315                 float f;
316                 ear1 = self.origin;
317                 ear1_z += 0.125 * self.view_ofs_z + 0.875 * self.maxs_z; // 7/8
318                 ear2 = ear1;
319                 makevectors(self.angles);
320                 ear1 += v_right * -10;
321                 ear2 += v_right * +10;
322                 d = inflictor.origin - self.origin;
323                 if (d)
324                         f = (d * v_right) / vlen(d); // this is cos of angle of d and v_right!
325                 else
326                         f = 0;  // Assum ecenter.
327                 force = v_right * vlen(force);
328                 Violence_GibSplash_At(ear1, force * -1, 2, bound(0, damage, 25) / 2 * (0.5 - 0.5 * f), self, attacker);
329                 Violence_GibSplash_At(ear2, force,      2, bound(0, damage, 25) / 2 * (0.5 + 0.5 * f), self, attacker);
330                 if(f > 0)
331                 {
332                         hitloc = ear1;
333                         force = force * -1;
334                 }
335                 else
336                 {
337                         hitloc = ear2;
338                         // force is already good
339                 }
340         }
341         else
342                 Violence_GibSplash_At(hitloc, force, 2, bound(0, damage, 200) / 16, self, attacker);
343
344
345         v = healtharmor_applydamage(self.armorvalue, autocvar_g_balance_armor_blockpercent, deathtype, damage);
346         take = v_x;
347         save = v_y;
348
349         if(attacker == self)
350         {
351                 // don't reset pushltime for self damage as it may be an attempt to
352                 // escape a lava pit or similar
353                 //self.pushltime = 0;
354                 self.istypefrag = 0;
355         }
356         else if(IS_PLAYER(attacker))
357         {
358                 self.pusher = attacker;
359                 self.pushltime = time + autocvar_g_maxpushtime;
360                 self.istypefrag = self.BUTTON_CHAT;
361         }
362         else if(time < self.pushltime)
363         {
364                 attacker = self.pusher;
365                 self.pushltime = max(self.pushltime, time + 0.6);
366         }
367         else
368         {
369                 self.pushltime = 0;
370                 self.istypefrag = 0;
371         }
372
373         frag_inflictor = inflictor;
374         frag_attacker = attacker;
375         frag_target = self;
376         frag_damage = damage;
377         damage_take = take;
378         damage_save = save;
379         damage_force = force;
380         MUTATOR_CALLHOOK(PlayerDamage_SplitHealthArmor);
381         take = bound(0, damage_take, self.health);
382         save = bound(0, damage_save, self.armorvalue);
383         excess = max(0, damage - take - save);
384
385         if(sound_allowed(MSG_BROADCAST, attacker))
386         {
387                 if (save > 10)
388                         sound (self, CH_SHOTS, "misc/armorimpact.wav", VOL_BASE, ATTEN_NORM);
389                 else if (take > 30)
390                         sound (self, CH_SHOTS, "misc/bodyimpact2.wav", VOL_BASE, ATTEN_NORM);
391                 else if (take > 10)
392                         sound (self, CH_SHOTS, "misc/bodyimpact1.wav", VOL_BASE, ATTEN_NORM); // FIXME possibly remove them?
393         }
394
395         if (take > 50)
396                 Violence_GibSplash_At(hitloc, force * -0.1, 3, 1, self, attacker);
397         if (take > 100)
398                 Violence_GibSplash_At(hitloc, force * -0.2, 3, 1, self, attacker);
399
400         if (time >= self.spawnshieldtime)
401         {
402                 if (!(self.flags & FL_GODMODE))
403                 {
404                         self.armorvalue = self.armorvalue - save;
405                         self.health = self.health - take;
406                         // pause regeneration for 5 seconds
407                         if(take)
408                 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_health_regen);
409
410                         if (time > self.pain_finished)          //Don't switch pain sequences like crazy
411                         {
412                                 self.pain_finished = time + 0.5;        //Supajoe
413
414                                 if(autocvar_sv_gentle < 1) {
415                                         if(self.classname != "body") // pain anim is BORKED on our ZYMs, FIXME remove this once we have good models
416                                         {
417                                                 if (!self.animstate_override)
418                                                 {
419                                                         if (random() > 0.5)
420                                                                 animdecide_setaction(self, ANIMACTION_PAIN1, TRUE);
421                                                         else
422                                                                 animdecide_setaction(self, ANIMACTION_PAIN2, TRUE);
423                                                 }
424                                         }
425
426                                         if(sound_allowed(MSG_BROADCAST, attacker))
427                                         if(!DEATH_ISWEAPON(deathtype, WEP_BLASTER) || attacker != self || self.health < 2 * WEP_CVAR_PRI(blaster, damage) * autocvar_g_balance_selfdamagepercent + 1) // WEAPONTODO: create separate limit for pain notification with laser
428                                         if(self.health > 1)
429                                         // exclude pain sounds for laserjumps as long as you aren't REALLY low on health and would die of the next two
430                                         {
431                                                 if(deathtype == DEATH_FALL)
432                                                         PlayerSound(playersound_fall, CH_PAIN, VOICETYPE_PLAYERSOUND);
433                                                 else if(self.health > 75) // TODO make a "gentle" version?
434                                                         PlayerSound(playersound_pain100, CH_PAIN, VOICETYPE_PLAYERSOUND);
435                                                 else if(self.health > 50)
436                                                         PlayerSound(playersound_pain75, CH_PAIN, VOICETYPE_PLAYERSOUND);
437                                                 else if(self.health > 25)
438                                                         PlayerSound(playersound_pain50, CH_PAIN, VOICETYPE_PLAYERSOUND);
439                                                 else
440                                                         PlayerSound(playersound_pain25, CH_PAIN, VOICETYPE_PLAYERSOUND);
441                                         }
442                                 }
443
444                                 // throw off bot aim temporarily
445                                 float shake;
446                                 shake = damage * 5 / (bound(0,skill,100) + 1);
447                                 self.v_angle_x = self.v_angle_x + (random() * 2 - 1) * shake;
448                                 self.v_angle_y = self.v_angle_y + (random() * 2 - 1) * shake;
449                         }
450                 }
451                 else
452                         self.max_armorvalue += (save + take);
453         }
454         self.dmg_save = self.dmg_save + save;//max(save - 10, 0);
455         self.dmg_take = self.dmg_take + take;//max(take - 10, 0);
456         self.dmg_inflictor = inflictor;
457
458         float abot, vbot, awep;
459         abot = (IS_BOT_CLIENT(attacker));
460         vbot = (IS_BOT_CLIENT(self));
461
462         valid_damage_for_weaponstats = 0;
463         awep = 0;
464
465         if(vbot || IS_REAL_CLIENT(self))
466         if(abot || IS_REAL_CLIENT(attacker))
467         if(attacker && self != attacker)
468         if(DIFF_TEAM(self, attacker))
469         {
470                 if(DEATH_ISSPECIAL(deathtype))
471                         awep = attacker.weapon;
472                 else
473                         awep = DEATH_WEAPONOF(deathtype);
474                 valid_damage_for_weaponstats = 1;
475         }
476
477         if(valid_damage_for_weaponstats)
478         {
479                 dh = dh - max(self.health, 0);
480                 da = da - max(self.armorvalue, 0);
481                 WeaponStats_LogDamage(awep, abot, self.weapon, vbot, dh + da);
482         }
483
484         if (self.health < 1)
485         {
486                 float defer_ClientKill_Now_TeamChange;
487                 defer_ClientKill_Now_TeamChange = FALSE;
488
489                 if(self.alivetime)
490                 {
491                         PlayerStats_Event(self, PLAYERSTATS_ALIVETIME, time - self.alivetime);
492                         self.alivetime = 0;
493                 }
494
495                 if(valid_damage_for_weaponstats)
496                         WeaponStats_LogKill(awep, abot, self.weapon, vbot);
497
498                 if(autocvar_sv_gentle < 1) // TODO make a "gentle" version?
499                 if(sound_allowed(MSG_BROADCAST, attacker))
500                 {
501                         if(deathtype == DEATH_DROWN)
502                                 PlayerSound(playersound_drown, CH_PAIN, VOICETYPE_PLAYERSOUND);
503                         else
504                                 PlayerSound(playersound_death, CH_PAIN, VOICETYPE_PLAYERSOUND);
505                 }
506
507                 // get rid of kill indicator
508                 if(self.killindicator)
509                 {
510                         remove(self.killindicator);
511                         self.killindicator = world;
512                         if(self.killindicator_teamchange)
513                                 defer_ClientKill_Now_TeamChange = TRUE;
514
515                         if(self.classname == "body")
516                         if(deathtype == DEATH_KILL)
517                         {
518                                 // for the lemmings fans, a small harmless explosion
519                                 pointparticles(particleeffectnum("rocket_explode"), self.origin, '0 0 0', 1);
520                         }
521                 }
522
523                 // print an obituary message
524                 Obituary (attacker, inflictor, self, deathtype);
525                 race_PreDie();
526
527         // increment frag counter for used weapon type
528         float w;
529         w = DEATH_WEAPONOF(deathtype);
530         if(WEP_VALID(w))
531         if(accuracy_isgooddamage(attacker, self))
532         attacker.accuracy.(accuracy_frags[w-1]) += 1;
533
534                 frag_attacker = attacker;
535                 frag_inflictor = inflictor;
536                 frag_target = self;
537                 frag_deathtype = deathtype;
538                 MUTATOR_CALLHOOK(PlayerDies);
539
540                 WEP_ACTION(self.weapon, WR_PLAYERDEATH);
541
542                 RemoveGrapplingHook(self);
543
544                 Portal_ClearAllLater(self);
545
546                 if(IS_REAL_CLIENT(self))
547                 {
548                         self.fixangle = TRUE;
549                         //msg_entity = self;
550                         //WriteByte (MSG_ONE, SVC_SETANGLE);
551                         //WriteAngle (MSG_ONE, self.v_angle_x);
552                         //WriteAngle (MSG_ONE, self.v_angle_y);
553                         //WriteAngle (MSG_ONE, 80);
554                 }
555
556                 if(defer_ClientKill_Now_TeamChange)
557                         ClientKill_Now_TeamChange(); // can turn player into spectator
558
559                 // player could have been miraculously resuscitated ;)
560                 // e.g. players in freezetag get frozen, they don't really die
561                 if(self.health >= 1 || !IS_PLAYER(self))
562                         return;
563
564                 // when we get here, player actually dies
565
566                 // clear waypoints
567                 WaypointSprite_PlayerDead();
568                 // throw a weapon
569                 SpawnThrownWeapon (self.origin + (self.mins + self.maxs) * 0.5, self.switchweapon);
570
571                 // become fully visible
572                 self.alpha = default_player_alpha;
573                 // make the corpse upright (not tilted)
574                 self.angles_x = 0;
575                 self.angles_z = 0;
576                 // don't spin
577                 self.avelocity = '0 0 0';
578                 // view from the floor
579                 self.view_ofs = '0 0 -8';
580                 // toss the corpse
581                 self.movetype = MOVETYPE_TOSS;
582                 // shootable corpse
583                 self.solid = SOLID_CORPSE;
584                 self.ballistics_density = autocvar_g_ballistics_density_corpse;
585                 // don't stick to the floor
586                 self.flags &= ~FL_ONGROUND;
587                 // dying animation
588                 self.deadflag = DEAD_DYING;
589                 // when to allow respawn
590                 calculate_player_respawn_time();
591
592                 self.death_time = time;
593                 if (random() < 0.5)
594                         animdecide_setstate(self, self.anim_state | ANIMSTATE_DEAD1, TRUE);
595                 else
596                         animdecide_setstate(self, self.anim_state | ANIMSTATE_DEAD2, TRUE);
597                 if (self.maxs_z > 5)
598                 {
599                         self.maxs_z = 5;
600                         setsize(self, self.mins, self.maxs);
601                 }
602                 // set damage function to corpse damage
603                 self.event_damage = PlayerCorpseDamage;
604                 // call the corpse damage function just in case it wants to gib
605                 self.event_damage(inflictor, attacker, excess, deathtype, hitloc, force);
606                 // set up to fade out later
607                 SUB_SetFade (self, time + 6 + random (), 1);
608
609                 if(autocvar_sv_gentle > 0 || autocvar_ekg) {
610                         // remove corpse
611                         PlayerCorpseDamage (inflictor, attacker, autocvar_sv_gibhealth+1.0, deathtype, hitloc, force);
612                 }
613
614                 // reset fields the weapons may use just in case
615                 for (j = WEP_FIRST; j <= WEP_LAST; ++j)
616                 {
617                         WEP_ACTION(j, WR_RESETPLAYER);
618                         ATTACK_FINISHED_FOR(self, j) = 0;
619                 }
620         }
621 }
622
623 .float muted; // to be used by prvm_edictset server playernumber muted 1
624 float Say(entity source, float teamsay, entity privatesay, string msgin, float floodcontrol)
625 // message "": do not say, just test flood control
626 // return value:
627 //   1 = accept
628 //   0 = reject
629 //  -1 = fake accept
630 {
631         string msgstr, colorstr, cmsgstr, namestr, fullmsgstr, sourcemsgstr, fullcmsgstr, sourcecmsgstr;
632         float flood;
633         var .float flood_field;
634         entity head;
635         float ret;
636         string privatemsgprefix = string_null; float privatemsgprefixlen = 0;
637
638         if(!teamsay && !privatesay)
639                 if(substring(msgin, 0, 1) == " ")
640                         msgin = substring(msgin, 1, strlen(msgin) - 1); // work around DP say bug (say_team does not have this!)
641
642         msgin = formatmessage(msgin);
643
644         if (!IS_PLAYER(source))
645                 colorstr = "^0"; // black for spectators
646         else if(teamplay)
647                 colorstr = Team_ColorCode(source.team);
648         else
649         {
650                 colorstr = "";
651                 teamsay = FALSE;
652         }
653
654         if(intermission_running)
655                 teamsay = FALSE;
656
657         if(msgin != "")
658                 msgin = trigger_magicear_processmessage_forallears(source, teamsay, privatesay, msgin);
659
660         /*
661          * using bprint solves this... me stupid
662         // how can we prevent the message from appearing in a listen server?
663         // for now, just give "say" back and only handle say_team
664         if(!teamsay)
665         {
666                 clientcommand(self, strcat("say ", msgin));
667                 return;
668         }
669         */
670
671         if(autocvar_g_chat_teamcolors)
672                 namestr = playername(source);
673         else
674                 namestr = source.netname;
675
676         if(msgin != "")
677         {
678                 if(privatesay)
679                 {
680                         msgstr = strcat("\{1}\{13}* ^3", namestr, "^3 tells you: ^7");
681                         privatemsgprefixlen = strlen(msgstr);
682                         msgstr = strcat(msgstr, msgin);
683                         cmsgstr = strcat(colorstr, "^3", namestr, "^3 tells you:\n^7", msgin);
684                         if(autocvar_g_chat_teamcolors)
685                                 privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", playername(privatesay), ": ^7");
686                         else
687                                 privatemsgprefix = strcat("\{1}\{13}* ^3You tell ", privatesay.netname, ": ^7");
688                 }
689                 else if(teamsay)
690                 {
691                         msgstr = strcat("\{1}\{13}", colorstr, "(^3", namestr, colorstr, ") ^7", msgin);
692                         cmsgstr = strcat(colorstr, "(^3", namestr, colorstr, ")\n^7", msgin);
693                 }
694                 else
695                 {
696                         msgstr = strcat("\{1}", namestr, "^7: ", msgin);
697                         cmsgstr = "";
698                 }
699                 msgstr = strcat(strreplace("\n", " ", msgstr), "\n"); // newlines only are good for centerprint
700         }
701         else
702         {
703                 msgstr = cmsgstr = "";
704         }
705
706         fullmsgstr = msgstr;
707         fullcmsgstr = cmsgstr;
708
709         // FLOOD CONTROL
710         flood = 0;
711         flood_field = floodcontrol_chat;
712         if(floodcontrol)
713         {
714                 float flood_spl;
715                 float flood_burst;
716                 float flood_lmax;
717                 float lines;
718                 if(privatesay)
719                 {
720                         flood_spl = autocvar_g_chat_flood_spl_tell;
721                         flood_burst = autocvar_g_chat_flood_burst_tell;
722                         flood_lmax = autocvar_g_chat_flood_lmax_tell;
723                         flood_field = floodcontrol_chattell;
724                 }
725                 else if(teamsay)
726                 {
727                         flood_spl = autocvar_g_chat_flood_spl_team;
728                         flood_burst = autocvar_g_chat_flood_burst_team;
729                         flood_lmax = autocvar_g_chat_flood_lmax_team;
730                         flood_field = floodcontrol_chatteam;
731                 }
732                 else
733                 {
734                         flood_spl = autocvar_g_chat_flood_spl;
735                         flood_burst = autocvar_g_chat_flood_burst;
736                         flood_lmax = autocvar_g_chat_flood_lmax;
737                         flood_field = floodcontrol_chat;
738                 }
739                 flood_burst = max(0, flood_burst - 1);
740                 // to match explanation in default.cfg, a value of 3 must allow three-line bursts and not four!
741
742                 // do flood control for the default line size
743                 if(msgstr != "")
744                 {
745                         getWrappedLine_remaining = msgstr;
746                         msgstr = "";
747                         lines = 0;
748                         while(getWrappedLine_remaining && (!flood_lmax || lines <= flood_lmax))
749                         {
750                                 msgstr = strcat(msgstr, " ", getWrappedLineLen(82.4289758859709, strlennocol)); // perl averagewidth.pl < gfx/vera-sans.width
751                                 ++lines;
752                         }
753                         msgstr = substring(msgstr, 1, strlen(msgstr) - 1);
754
755                         if(getWrappedLine_remaining != "")
756                         {
757                                 msgstr = strcat(msgstr, "\n");
758                                 flood = 2;
759                         }
760
761                         if(time >= source.flood_field)
762                         {
763                                 source.flood_field = max(time - flood_burst * flood_spl, source.flood_field) + lines * flood_spl;
764                         }
765                         else
766                         {
767                                 flood = 1;
768                                 msgstr = fullmsgstr;
769                         }
770                 }
771                 else
772                 {
773                         if(time >= source.flood_field)
774                                 source.flood_field = max(time - flood_burst * flood_spl, source.flood_field) + flood_spl;
775                         else
776                                 flood = 1;
777                 }
778
779                 if (timeout_status == TIMEOUT_ACTIVE) // when game is paused, no flood protection
780                         source.flood_field = flood = 0;
781         }
782
783         if(flood == 2) // cannot happen for empty msgstr
784         {
785                 if(autocvar_g_chat_flood_notify_flooder)
786                 {
787                         sourcemsgstr = strcat(msgstr, "\n^3FLOOD CONTROL: ^7message too long, trimmed\n");
788                         sourcecmsgstr = "";
789                 }
790                 else
791                 {
792                         sourcemsgstr = fullmsgstr;
793                         sourcecmsgstr = fullcmsgstr;
794                 }
795                 cmsgstr = "";
796         }
797         else
798         {
799                 sourcemsgstr = msgstr;
800                 sourcecmsgstr = cmsgstr;
801         }
802
803         if(!privatesay)
804         if (!IS_PLAYER(source))
805         {
806                 if (!intermission_running)
807                         if(teamsay || (autocvar_g_chat_nospectators == 1) || (autocvar_g_chat_nospectators == 2 && !(warmup_stage || gameover)))
808                                 teamsay = -1; // spectators
809         }
810
811         if(flood)
812                 print("NOTE: ", playername(source), "^7 is flooding.\n");
813
814         // build sourcemsgstr by cutting off a prefix and replacing it by the other one
815         if(privatesay)
816                 sourcemsgstr = strcat(privatemsgprefix, substring(sourcemsgstr, privatemsgprefixlen, -1));
817
818         if(source.muted)
819         {
820                 // always fake the message
821                 ret = -1;
822         }
823         else if(flood == 1)
824         {
825                 if(autocvar_g_chat_flood_notify_flooder)
826                 {
827                         sprint(source, strcat("^3FLOOD CONTROL: ^7wait ^1", ftos(source.flood_field - time), "^3 seconds\n"));
828                         ret = 0;
829                 }
830                 else
831                         ret = -1;
832         }
833         else
834         {
835                 ret = 1;
836         }
837
838         if(sourcemsgstr != "" && ret != 0)
839         {
840                 if(ret < 0) // faked message, because the player is muted
841                 {
842                         sprint(source, sourcemsgstr);
843                         if(sourcecmsgstr != "" && !privatesay)
844                                 centerprint(source, sourcecmsgstr);
845                 }
846                 else if(privatesay) // private message, between 2 people only
847                 {
848                         sprint(source, sourcemsgstr);
849                         sprint(privatesay, msgstr);
850                         if (!autocvar_g_chat_tellprivacy) { dedicated_print(msgstr); } // send to server console too if "tellprivacy" is disabled
851                         if(cmsgstr != "")
852                                 centerprint(privatesay, cmsgstr);
853                 }
854                 else if(teamsay > 0) // team message, only sent to team mates
855                 {
856                         sprint(source, sourcemsgstr);
857                         dedicated_print(msgstr); // send to server console too
858                         if(sourcecmsgstr != "")
859                                 centerprint(source, sourcecmsgstr);
860                         FOR_EACH_REALPLAYER(head) if(head.team == source.team)
861                                 if(head != source)
862                                 {
863                                         sprint(head, msgstr);
864                                         if(cmsgstr != "")
865                                                 centerprint(head, cmsgstr);
866                                 }
867                 }
868                 else if(teamsay < 0) // spectator message, only sent to spectators
869                 {
870                         sprint(source, sourcemsgstr);
871                         dedicated_print(msgstr); // send to server console too
872                         FOR_EACH_REALCLIENT(head) if (!IS_PLAYER(head))
873                                 if(head != source)
874                                         sprint(head, msgstr);
875                 }
876                 else if(sourcemsgstr != msgstr) // trimmed/server fixed message, sent to all players
877                 {
878                         sprint(source, sourcemsgstr);
879                         dedicated_print(msgstr); // send to server console too
880                         FOR_EACH_REALCLIENT(head)
881                                 if(head != source)
882                                         sprint(head, msgstr);
883                 }
884                 else
885                         bprint(msgstr); // entirely normal message, sent to all players -- bprint sends to server console too.
886         }
887
888         return ret;
889 }
890
891 float GetVoiceMessageVoiceType(string type)
892 {
893         if(type == "taunt")
894                 return VOICETYPE_TAUNT;
895         if(type == "teamshoot")
896                 return VOICETYPE_LASTATTACKER;
897         return VOICETYPE_TEAMRADIO;
898 }
899
900 string allvoicesamples;
901 .string GetVoiceMessageSampleField(string type)
902 {
903         GetPlayerSoundSampleField_notFound = 0;
904         switch(type)
905         {
906 #define _VOICEMSG(m) case #m: return playersound_##m;
907                 ALLVOICEMSGS
908 #undef _VOICEMSG
909         }
910         GetPlayerSoundSampleField_notFound = 1;
911         return playersound_taunt;
912 }
913
914 .string GetPlayerSoundSampleField(string type)
915 {
916         GetPlayerSoundSampleField_notFound = 0;
917         switch(type)
918         {
919 #define _VOICEMSG(m) case #m: return playersound_##m;
920                 ALLPLAYERSOUNDS
921 #undef _VOICEMSG
922         }
923         GetPlayerSoundSampleField_notFound = 1;
924         return playersound_taunt;
925 }
926
927 void PrecacheGlobalSound(string samplestring)
928 {
929         float n, i;
930         tokenize_console(samplestring);
931         n = stof(argv(1));
932         if(n > 0)
933         {
934                 for(i = 1; i <= n; ++i)
935                         precache_sound(strcat(argv(0), ftos(i), ".wav"));
936         }
937         else
938         {
939                 precache_sound(strcat(argv(0), ".wav"));
940         }
941 }
942
943 void PrecachePlayerSounds(string f)
944 {
945         float fh;
946         string s;
947         fh = fopen(f, FILE_READ);
948         if(fh < 0)
949                 return;
950         while((s = fgets(fh)))
951         {
952                 if(tokenize_console(s) != 3)
953                 {
954                         dprint("Invalid sound info line: ", s, "\n");
955                         continue;
956                 }
957                 PrecacheGlobalSound(strcat(argv(1), " ", argv(2)));
958         }
959         fclose(fh);
960
961         if (!allvoicesamples)
962         {
963 #define _VOICEMSG(m) allvoicesamples = strcat(allvoicesamples, " ", #m);
964                 ALLVOICEMSGS
965 #undef _VOICEMSG
966                 allvoicesamples = strzone(substring(allvoicesamples, 1, strlen(allvoicesamples) - 1));
967         }
968 }
969
970 void ClearPlayerSounds()
971 {
972 #define _VOICEMSG(m) if(self.playersound_##m) { strunzone(self.playersound_##m); self.playersound_##m = string_null; }
973         ALLPLAYERSOUNDS
974         ALLVOICEMSGS
975 #undef _VOICEMSG
976 }
977
978 float LoadPlayerSounds(string f, float first)
979 {
980         float fh;
981         string s;
982         var .string field;
983         fh = fopen(f, FILE_READ);
984         if(fh < 0)
985         {
986                 dprint("Player sound file not found: ", f, "\n");
987                 return 0;
988         }
989         while((s = fgets(fh)))
990         {
991                 if(tokenize_console(s) != 3)
992                         continue;
993                 field = GetPlayerSoundSampleField(argv(0));
994                 if(GetPlayerSoundSampleField_notFound)
995                         field = GetVoiceMessageSampleField(argv(0));
996                 if(GetPlayerSoundSampleField_notFound)
997                         continue;
998                 if(self.field)
999                         strunzone(self.field);
1000                 self.field = strzone(strcat(argv(1), " ", argv(2)));
1001         }
1002         fclose(fh);
1003         return 1;
1004 }
1005
1006 .float modelindex_for_playersound;
1007 .float skin_for_playersound;
1008 void UpdatePlayerSounds()
1009 {
1010         if(self.modelindex == self.modelindex_for_playersound)
1011         if(self.skin == self.skin_for_playersound)
1012                 return;
1013         self.modelindex_for_playersound = self.modelindex;
1014         self.skin_for_playersound = self.skin;
1015         ClearPlayerSounds();
1016         LoadPlayerSounds("sound/player/default.sounds", 1);
1017         if(!autocvar_g_debug_defaultsounds)
1018                 if(!LoadPlayerSounds(get_model_datafilename(self.model, self.skin, "sounds"), 0))
1019                         LoadPlayerSounds(get_model_datafilename(self.model, 0, "sounds"), 0);
1020 }
1021
1022 void FakeGlobalSound(string sample, float chan, float voicetype)
1023 {
1024         float n;
1025         float tauntrand;
1026
1027         if(sample == "")
1028                 return;
1029
1030         tokenize_console(sample);
1031         n = stof(argv(1));
1032         if(n > 0)
1033                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
1034         else
1035                 sample = strcat(argv(0), ".wav"); // randomization
1036
1037         switch(voicetype)
1038         {
1039                 case VOICETYPE_LASTATTACKER_ONLY:
1040                         break;
1041                 case VOICETYPE_LASTATTACKER:
1042                         if(self.pusher)
1043                         {
1044                                 msg_entity = self;
1045                                 if(IS_REAL_CLIENT(msg_entity))
1046                                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NONE);
1047                         }
1048                         break;
1049                 case VOICETYPE_TEAMRADIO:
1050                         msg_entity = self;
1051                         if(msg_entity.cvar_cl_voice_directional == 1)
1052                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1053                         else
1054                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1055                         break;
1056                 case VOICETYPE_AUTOTAUNT:
1057                         if(!sv_autotaunt)
1058                                 break;
1059                         if(!sv_taunt)
1060                                 break;
1061                         if(autocvar_sv_gentle)
1062                                 break;
1063                         tauntrand = random();
1064                         msg_entity = self;
1065                         if (tauntrand < msg_entity.cvar_cl_autotaunt)
1066                         {
1067                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1068                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1069                                 else
1070                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1071                         }
1072                         break;
1073                 case VOICETYPE_TAUNT:
1074                         if(IS_PLAYER(self))
1075                                 if(self.deadflag == DEAD_NO)
1076                                         animdecide_setaction(self, ANIMACTION_TAUNT, TRUE);
1077                         if(!sv_taunt)
1078                                 break;
1079                         if(autocvar_sv_gentle)
1080                                 break;
1081                         msg_entity = self;
1082                         if (msg_entity.cvar_cl_voice_directional >= 1)
1083                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1084                         else
1085                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1086                         break;
1087                 case VOICETYPE_PLAYERSOUND:
1088                         msg_entity = self;
1089                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NORM);
1090                         break;
1091                 default:
1092                         backtrace("Invalid voice type!");
1093                         break;
1094         }
1095 }
1096
1097 void GlobalSound(string sample, float chan, float voicetype)
1098 {
1099         float n;
1100         float tauntrand;
1101
1102         if(sample == "")
1103                 return;
1104
1105         tokenize_console(sample);
1106         n = stof(argv(1));
1107         if(n > 0)
1108                 sample = strcat(argv(0), ftos(floor(random() * n + 1)), ".wav"); // randomization
1109         else
1110                 sample = strcat(argv(0), ".wav"); // randomization
1111
1112         switch(voicetype)
1113         {
1114                 case VOICETYPE_LASTATTACKER_ONLY:
1115                         if(self.pusher)
1116                         {
1117                                 msg_entity = self.pusher;
1118                                 if(IS_REAL_CLIENT(msg_entity))
1119                                 {
1120                                         if(msg_entity.cvar_cl_voice_directional == 1)
1121                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1122                                         else
1123                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1124                                 }
1125                         }
1126                         break;
1127                 case VOICETYPE_LASTATTACKER:
1128                         if(self.pusher)
1129                         {
1130                                 msg_entity = self.pusher;
1131                                 if(IS_REAL_CLIENT(msg_entity))
1132                                 {
1133                                         if(msg_entity.cvar_cl_voice_directional == 1)
1134                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1135                                         else
1136                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1137                                 }
1138                                 msg_entity = self;
1139                                 if(IS_REAL_CLIENT(msg_entity))
1140                                         soundto(MSG_ONE, self, chan, sample, VOL_BASE, ATTEN_NONE);
1141                         }
1142                         break;
1143                 case VOICETYPE_TEAMRADIO:
1144                         FOR_EACH_REALCLIENT(msg_entity)
1145                                 if(!teamplay || msg_entity.team == self.team)
1146                                 {
1147                                         if(msg_entity.cvar_cl_voice_directional == 1)
1148                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_MIN);
1149                                         else
1150                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1151                                 }
1152                         break;
1153                 case VOICETYPE_AUTOTAUNT:
1154                         if(!sv_autotaunt)
1155                                 break;
1156                         if(!sv_taunt)
1157                                 break;
1158                         if(autocvar_sv_gentle)
1159                                 break;
1160                         tauntrand = random();
1161                         FOR_EACH_REALCLIENT(msg_entity)
1162                                 if (tauntrand < msg_entity.cvar_cl_autotaunt)
1163                                 {
1164                                         if (msg_entity.cvar_cl_voice_directional >= 1)
1165                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1166                                         else
1167                                                 soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1168                                 }
1169                         break;
1170                 case VOICETYPE_TAUNT:
1171                         if(IS_PLAYER(self))
1172                                 if(self.deadflag == DEAD_NO)
1173                                         animdecide_setaction(self, ANIMACTION_TAUNT, TRUE);
1174                         if(!sv_taunt)
1175                                 break;
1176                         if(autocvar_sv_gentle)
1177                                 break;
1178                         FOR_EACH_REALCLIENT(msg_entity)
1179                         {
1180                                 if (msg_entity.cvar_cl_voice_directional >= 1)
1181                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, ATTEN_MAX));
1182                                 else
1183                                         soundto(MSG_ONE, self, chan, sample, VOL_BASEVOICE, ATTEN_NONE);
1184                         }
1185                         break;
1186                 case VOICETYPE_PLAYERSOUND:
1187                         sound(self, chan, sample, VOL_BASE, ATTEN_NORM);
1188                         break;
1189                 default:
1190                         backtrace("Invalid voice type!");
1191                         break;
1192         }
1193 }
1194
1195 void PlayerSound(.string samplefield, float chan, float voicetype)
1196 {
1197         GlobalSound(self.samplefield, chan, voicetype);
1198 }
1199
1200 void VoiceMessage(string type, string msg)
1201 {
1202         var .string sample;
1203         float voicetype, ownteam;
1204         float flood;
1205         sample = GetVoiceMessageSampleField(type);
1206
1207         if(GetPlayerSoundSampleField_notFound)
1208         {
1209                 sprint(self, strcat("Invalid voice. Use one of: ", allvoicesamples, "\n"));
1210                 return;
1211         }
1212
1213         voicetype = GetVoiceMessageVoiceType(type);
1214         ownteam = (voicetype == VOICETYPE_TEAMRADIO);
1215
1216         flood = Say(self, ownteam, world, msg, 1);
1217
1218         if (flood > 0)
1219                 GlobalSound(self.sample, CH_VOICE, voicetype);
1220         else if (flood < 0)
1221                 FakeGlobalSound(self.sample, CH_VOICE, voicetype);
1222 }
1223
1224 void MoveToTeam(entity client, float team_colour, float type)
1225 {
1226         float lockteams_backup;
1227
1228         lockteams_backup = lockteams;  // backup any team lock
1229
1230         lockteams = 0;  // disable locked teams
1231
1232         TeamchangeFrags(client);  // move the players frags
1233         SetPlayerColors(client, team_colour - 1);  // set the players colour
1234         Damage(client, client, client, 100000, DEATH_AUTOTEAMCHANGE, client.origin, '0 0 0');  // kill the player
1235
1236         lockteams = lockteams_backup;  // restore the team lock
1237
1238         LogTeamchange(client.playerid, client.team, type);
1239 }