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