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