4 #include <server/utils.qh>
6 bool autocvar_bot_sound_monopoly;
9 bool sound_allowed(int to, entity e)
11 if(!e) return true; // save on a few checks
14 if (e.classname == "body") e = e.enemy;
15 else if (e.realowner && e.realowner != e) e = e.realowner;
16 else if (e.owner && e.owner != e) e = e.owner;
19 // sounds to self may always pass
20 if (to == MSG_ONE && e == msg_entity) return true;
21 // sounds by players can be removed
22 if (autocvar_bot_sound_monopoly && IS_REAL_CLIENT(e)) return false;
23 // anything else may pass
27 /** hack: string precache_sound(string s) = #19; */
28 int precache_sound_index(string s) = #19;
30 const int SVC_SOUND = 6;
31 const int SVC_STOPSOUND = 16;
33 const int SND_VOLUME = BIT(0);
34 const int SND_ATTENUATION = BIT(1);
35 const int SND_LARGEENTITY = BIT(3);
36 const int SND_LARGESOUND = BIT(4);
37 const int SND_SPEEDUSHORT4000 = BIT(5);
39 void soundtoat(int to, entity e, vector o, int chan, string samp, float vol, float attenu, float _pitch)
41 if (!sound_allowed(to, e)) return;
43 int idx = precache_sound_index(samp);
44 attenu = floor(attenu * 64);
45 vol = floor(vol * 255);
47 int speed4000 = floor((_pitch * 0.01) * 4000 + 0.5);
48 if (vol != 255) sflags |= SND_VOLUME;
49 if (attenu != 64) sflags |= SND_ATTENUATION;
50 if (entno >= 8192 || chan < 0 || chan > 7) sflags |= SND_LARGEENTITY;
51 if (idx >= 256) sflags |= SND_LARGESOUND;
52 if (speed4000 && speed4000 != 4000) sflags |= SND_SPEEDUSHORT4000;
53 WriteByte(to, SVC_SOUND);
54 WriteByte(to, sflags);
55 if (sflags & SND_VOLUME) WriteByte(to, vol);
56 if (sflags & SND_ATTENUATION) WriteByte(to, attenu);
57 if (sflags & SND_SPEEDUSHORT4000) WriteShort(to, speed4000);
58 if (sflags & SND_LARGEENTITY)
60 WriteShort(to, entno);
65 WriteShort(to, (entno << 3) | chan);
67 if (sflags & SND_LARGESOUND) WriteShort(to, idx);
68 else WriteByte(to, idx);
74 void soundto(int _dest, entity e, int chan, string samp, float vol, float _atten, float _pitch)
76 if (!sound_allowed(_dest, e)) return;
77 vector o = e.origin + 0.5 * (e.mins + e.maxs);
78 soundtoat(_dest, e, o, chan, samp, vol, _atten, _pitch);
80 void soundat(entity e, vector o, int chan, string samp, float vol, float _atten)
82 soundtoat(((chan & 8) ? MSG_ALL : MSG_BROADCAST), e, o, chan, samp, vol, _atten, 0);
84 void stopsoundto(int _dest, entity e, int chan)
86 if (!sound_allowed(_dest, e)) return;
88 if (entno >= 8192 || chan < 0 || chan > 7)
90 int idx = precache_sound_index(SND(Null));
91 int sflags = SND_LARGEENTITY;
92 if (idx >= 256) sflags |= SND_LARGESOUND;
93 WriteByte(_dest, SVC_SOUND);
94 WriteByte(_dest, sflags);
95 WriteShort(_dest, entno);
96 WriteByte(_dest, chan);
97 if (sflags & SND_LARGESOUND) WriteShort(_dest, idx);
98 else WriteByte(_dest, idx);
99 WriteCoord(_dest, e.origin.x);
100 WriteCoord(_dest, e.origin.y);
101 WriteCoord(_dest, e.origin.z);
105 WriteByte(_dest, SVC_STOPSOUND);
106 WriteShort(_dest, entno * 8 + chan);
109 void stopsound(entity e, int chan)
111 if (!sound_allowed(MSG_BROADCAST, e)) return;
112 stopsoundto(MSG_BROADCAST, e, chan); // unreliable, gets there fast
113 stopsoundto(MSG_ALL, e, chan); // in case of packet loss
116 void play2(entity e, string filename)
119 soundtoat(MSG_ONE, NULL, '0 0 0', CH_INFO, filename, VOL_BASE, ATTEN_NONE, 0);
123 /** use this one if you might be causing spam (e.g. from touch functions that might get called more than once per frame) */
124 float spamsound(entity e, int chan, Sound samp, float vol, float _atten)
126 if (!sound_allowed(MSG_BROADCAST, e)) return false;
127 if (time > e.spamtime)
130 sound(e, chan, samp, vol, _atten);
136 void play2team(float t, string filename)
138 if (autocvar_bot_sound_monopoly) return;
139 FOREACH_CLIENT(IS_PLAYER(it) && IS_REAL_CLIENT(it) && it.team == t, play2(it, filename));
142 void play2all(string samp)
144 if (autocvar_bot_sound_monopoly) return;
145 _sound(NULL, CH_INFO, samp, VOL_BASE, ATTEN_NONE);