1 #include "globalsound.qh"
3 #include <common/ent_cs.qh>
5 #include <common/animdecide.qh>
7 REGISTER_NET_TEMP(globalsound)
8 REGISTER_NET_TEMP(playersound)
11 REPLICATE(cvar_cl_autotaunt, float, "cl_autotaunt");
12 REPLICATE(cvar_cl_voice_directional, int, "cl_voice_directional");
13 REPLICATE(cvar_cl_voice_directional_taunt_attenuation, float, "cl_voice_directional_taunt_attenuation");
18 * @param from the source entity, its position is sent
19 * @param gs the global sound def
20 * @param r a random number in 0..1
22 void globalsound(int channel, entity from, entity gs, float r, int chan, float _vol, float _atten, float _pitch)
24 //assert(IS_PLAYER(from), eprint(from));
25 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
26 if (!autocvar_g_debug_globalsounds) {
27 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
30 soundto(channel, from, chan, sample, _vol, _atten, _pitch);
33 if(sound_allowed(MSG_BROADCAST, from))
34 sound7(from, chan, sample, _vol, _atten, _pitch, 0);
39 // FIXME: pitch not implemented
40 WriteHeader(channel, globalsound);
41 WriteByte(channel, gs.m_id);
42 WriteByte(channel, r * 255);
43 WriteByte(channel, etof(from));
44 WriteByte(channel, chan);
45 WriteByte(channel, floor(_vol * 255));
46 WriteByte(channel, floor(_atten * 64));
47 entcs_force_origin(from);
48 vector o = from.origin + 0.5 * (from.mins + from.maxs);
49 WriteVector(channel, o);
53 * @param from the source entity, its position is sent
54 * @param ps the player sound def
55 * @param r a random number in 0..1
57 void playersound(int channel, entity from, entity ps, float r, int chan, float _vol, float _atten, float _pitch)
59 //assert(IS_PLAYER(from), eprint(from));
60 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
61 if (!autocvar_g_debug_globalsounds) {
62 //UpdatePlayerSounds(from);
63 string s = from.(ps.m_playersoundfld);
64 string sample = GlobalSound_sample(s, r);
67 soundto(channel, from, chan, sample, _vol, _atten, _pitch);
70 if(sound_allowed(MSG_BROADCAST, from))
71 sound7(from, chan, sample, _vol, _atten, _pitch, 0);
76 // FIXME: pitch not implemented
77 WriteHeader(channel, playersound);
78 WriteByte(channel, ps.m_id);
79 WriteByte(channel, r * 255);
80 WriteByte(channel, etof(from));
81 WriteByte(channel, chan);
82 WriteByte(channel, floor(_vol * 255));
83 WriteByte(channel, floor(_atten * 64));
84 entcs_force_origin(from);
85 vector o = from.origin + 0.5 * (from.mins + from.maxs);
86 WriteVector(channel, o);
92 NET_HANDLE(globalsound, bool isnew)
94 entity gs = REGISTRY_GET(GlobalSounds, ReadByte());
95 float r = ReadByte() / 255;
96 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
98 entity e = entcs_receiver(who - 1);
99 int chan = ReadSByte();
100 float vol = ReadByte() / 255;
101 float atten = ReadByte() / 64;
102 vector o = ReadVector();
103 // TODO: is this really what we want to be doing? Footsteps that follow the player at head height?
104 if (who == player_currententnum) e = findfloat(NULL, entnum, who); // play at camera position for full volume
105 else if (e) e.origin = o;
108 sound7(e, chan, sample, vol, atten, 0, 0);
113 LOG_WARNF("Missing entcs data for player %d", who);
114 sound8(e, o, chan, sample, vol, atten, 0, 0);
119 NET_HANDLE(playersound, bool isnew)
121 entity ps = REGISTRY_GET(PlayerSounds, ReadByte());
122 float r = ReadByte() / 255;
123 int who = ReadByte();
124 entity e = entcs_receiver(who - 1);
125 UpdatePlayerSounds(e);
126 string s = e.(ps.m_playersoundfld);
127 string sample = GlobalSound_sample(s, r);
128 int chan = ReadSByte();
129 float vol = ReadByte() / 255;
130 float atten = ReadByte() / 64;
131 vector o = ReadVector();
132 if (who == player_currententnum) e = findfloat(NULL, entnum, who); // play at camera position for full volume
133 else if (e) e.origin = o;
136 // TODO: for non-visible players, origin should probably continue to be updated as long as the sound is playing
137 sound7(e, chan, sample, vol, atten, 0, 0);
142 LOG_WARNF("Missing entcs data for player %d", who);
143 sound8(e, o, chan, sample, vol, atten, 0, 0);
150 string GlobalSound_sample(string pair, float r)
154 string s = cdr(pair);
158 string sample = car(pair);
159 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1)); // randomization
160 else sample = sprintf("%s.wav", sample);
164 float GlobalSound_pitch(float _pitch)
166 // customizable gradient function that crosses (0,a), (c,1) and asymptotically approaches b
167 float a = 1.5; // max pitch
168 float b = 0.75; // min pitch
169 float c = 100; // standard pitch (scale * 100)
171 float pitch_shift = (b*d*(a-1) + a*c*(1-b)) / (d*(a-1) + c*(1-b));
173 return pitch_shift * 100;
176 void PrecacheGlobalSound(string sample)
180 string s = cdr(sample);
184 sample = car(sample);
187 for (int i = 1; i <= n; ++i)
188 precache_sound(sprintf("%s%d.wav", sample, i));
192 precache_sound(sprintf("%s.wav", sample));
196 entity GetVoiceMessage(string type)
198 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, return it);
202 entity GetPlayerSound(string type)
204 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, return it);
208 .string _GetPlayerSoundSampleField(string type, bool voice)
210 GetPlayerSoundSampleField_notFound = false;
211 entity e = voice ? GetVoiceMessage(type) : GetPlayerSound(type);
212 if (e) return e.m_playersoundfld;
213 GetPlayerSoundSampleField_notFound = true;
214 return playersound_taunt.m_playersoundfld;
217 .string GetVoiceMessageSampleField(string type)
219 return _GetPlayerSoundSampleField(type, true);
222 void PrecachePlayerSounds(string f)
224 int fh = fopen(f, FILE_READ);
227 LOG_WARNF("Player sound file not found: %s", f);
230 for (string s; (s = fgets(fh)); )
232 int n = tokenize_console(s);
235 if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
238 string file = argv(1);
239 string variants = argv(2);
240 PrecacheGlobalSound(strcat(file, " ", variants));
247 .string GetPlayerSoundSampleField(string type)
249 return _GetPlayerSoundSampleField(type, false);
252 void ClearPlayerSounds(entity this)
254 FOREACH(PlayerSounds, true, {
255 .string fld = it.m_playersoundfld;
263 bool LoadPlayerSounds(entity this, string f, bool strict)
265 int fh = fopen(f, FILE_READ);
268 if (strict) LOG_WARNF("Player sound file not found: %s", f);
271 for (string s; (s = fgets(fh)); )
273 int n = tokenize_console(s);
276 if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
279 string key = argv(0);
280 var.string field = GetPlayerSoundSampleField(key);
281 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
282 if (GetPlayerSoundSampleField_notFound)
284 LOG_TRACEF("Invalid sound info field in player sound file '%s': %s", f, key);
287 string file = argv(1);
288 string variants = argv(2);
289 strcpy(this.(field), strcat(file, " ", variants));
295 .string model_for_playersound;
296 .int skin_for_playersound;
298 bool autocvar_g_debug_defaultsounds;
300 void UpdatePlayerSounds(entity this)
302 if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
303 strcpy(this.model_for_playersound, this.model);
304 this.skin_for_playersound = this.skin;
305 ClearPlayerSounds(this);
306 LoadPlayerSounds(this, "sound/player/default.sounds", true);
307 if (this.model == "null"
309 && autocvar_g_debug_globalsounds
312 if (autocvar_g_debug_defaultsounds) return;
313 if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
314 LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
321 void _GlobalSound(entity this, entity gs, entity ps, string sample, int chan, float vol, int voicetype, bool fake)
323 if (gs == NULL && ps == NULL && sample == "") return;
324 if(this.classname == "body") return;
326 float myscale = ((this.scale) ? this.scale : 1); // safety net
327 float thepitch = ((myscale == 1) ? 0 : GlobalSound_pitch(myscale * 100));
328 if (sample != "") sample = GlobalSound_sample(sample, r);
331 case VOICETYPE_LASTATTACKER_ONLY:
332 case VOICETYPE_LASTATTACKER:
336 if (!this.pusher) break;
337 msg_entity = this.pusher;
338 if (IS_REAL_CLIENT(msg_entity))
340 float atten = (CS_CVAR(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
341 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten, thepitch);
342 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten, thepitch);
343 else soundto(MSG_ONE, this, chan, sample, vol, atten, thepitch);
346 if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
348 if (IS_REAL_CLIENT(msg_entity))
350 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE, thepitch);
351 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE, thepitch);
352 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE, thepitch);
356 case VOICETYPE_TEAMRADIO:
360 float atten = (CS_CVAR(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
361 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten, thepitch); \
362 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten, thepitch); \
363 else soundto(MSG_ONE, this, chan, sample, vol, atten, thepitch); \
366 if (fake) { msg_entity = this; X(); }
369 FOREACH_CLIENT(IS_REAL_CLIENT(it) && SAME_TEAM(it, this), {
377 case VOICETYPE_AUTOTAUNT:
378 case VOICETYPE_TAUNT:
380 if (voicetype == VOICETYPE_AUTOTAUNT)
382 if (!autocvar_sv_autotaunt) break;
384 else if (IS_PLAYER(this) && !IS_DEAD(this))
385 animdecide_setaction(this, ANIMACTION_TAUNT, true);
387 if (!autocvar_sv_taunt) break;
388 if (autocvar_sv_gentle) break;
390 if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
394 if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < CS_CVAR(msg_entity).cvar_cl_autotaunt) \
396 float atten = (CS_CVAR(msg_entity).cvar_cl_voice_directional >= 1) \
397 ? bound(ATTEN_MIN, CS_CVAR(msg_entity).cvar_cl_voice_directional_taunt_attenuation, \
400 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten, thepitch); \
401 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten, thepitch); \
402 else soundto(MSG_ONE, this, chan, sample, vol, atten, thepitch); \
412 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
420 case VOICETYPE_PLAYERSOUND:
425 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, ATTEN_NORM, thepitch);
426 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, ATTEN_NORM, thepitch);
427 else soundto(MSG_ONE, this, chan, sample, vol, ATTEN_NORM, thepitch);
431 if (gs) globalsound(MSG_ALL, this, gs, r, chan, vol, ATTEN_NORM, thepitch);
432 else if (ps) playersound(MSG_ALL, this, ps, r, chan, vol, ATTEN_NORM, thepitch);
433 else if (sound_allowed(MSG_BROADCAST, this)) sound7(this, chan, sample, vol, ATTEN_NORM, thepitch, 0);
439 backtrace("Invalid voice type!");