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)
12 * @param from the source entity, its position is sent
13 * @param gs the global sound def
14 * @param r a random number in 0..1
16 void globalsound(int channel, entity from, entity gs, float r, int chan, float _vol, float _atten, float _pitch)
18 //assert(IS_PLAYER(from), eprint(from));
19 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
20 if (!autocvar_g_debug_globalsounds) {
21 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
24 soundto(channel, from, chan, sample, _vol, _atten, _pitch);
27 if(sound_allowed(MSG_BROADCAST, from))
28 sound7(from, chan, sample, _vol, _atten, _pitch, 0);
33 // FIXME: pitch not implemented
34 WriteHeader(channel, globalsound);
35 WriteByte(channel, gs.m_id);
36 WriteByte(channel, r * 255);
37 WriteByte(channel, etof(from));
38 WriteByte(channel, chan);
39 WriteByte(channel, floor(_vol * 255));
40 WriteByte(channel, floor(_atten * 64));
41 entcs_force_origin(from);
42 vector o = from.origin + 0.5 * (from.mins + from.maxs);
43 WriteVector(channel, o);
47 * @param from the source entity, its position is sent
48 * @param ps the player sound def
49 * @param r a random number in 0..1
51 void playersound(int channel, entity from, entity ps, float r, int chan, float _vol, float _atten, float _pitch)
53 //assert(IS_PLAYER(from), eprint(from));
54 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
55 if (!autocvar_g_debug_globalsounds) {
56 //UpdatePlayerSounds(from);
57 string s = from.(ps.m_playersoundfld);
58 string sample = GlobalSound_sample(s, r);
61 soundto(channel, from, chan, sample, _vol, _atten, _pitch);
64 if(sound_allowed(MSG_BROADCAST, from))
65 sound7(from, chan, sample, _vol, _atten, _pitch, 0);
70 // FIXME: pitch not implemented
71 WriteHeader(channel, playersound);
72 WriteByte(channel, ps.m_id);
73 WriteByte(channel, r * 255);
74 WriteByte(channel, etof(from));
75 WriteByte(channel, chan);
76 WriteByte(channel, floor(_vol * 255));
77 WriteByte(channel, floor(_atten * 64));
78 entcs_force_origin(from);
79 vector o = from.origin + 0.5 * (from.mins + from.maxs);
80 WriteVector(channel, o);
86 NET_HANDLE(globalsound, bool isnew)
88 entity gs = REGISTRY_GET(GlobalSounds, ReadByte());
89 float r = ReadByte() / 255;
90 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
92 entity e = entcs_receiver(who - 1);
93 int chan = ReadSByte();
94 float vol = ReadByte() / 255;
95 float atten = ReadByte() / 64;
96 vector o = ReadVector();
97 // TODO: is this really what we want to be doing? Footsteps that follow the player at head height?
98 if (who == player_currententnum) e = findfloat(NULL, entnum, who); // play at camera position for full volume
99 else if (e) e.origin = o;
102 sound7(e, chan, sample, vol, atten, 0, 0);
107 LOG_WARNF("Missing entcs data for player %d", who);
108 sound8(e, o, chan, sample, vol, atten, 0, 0);
113 NET_HANDLE(playersound, bool isnew)
115 entity ps = REGISTRY_GET(PlayerSounds, ReadByte());
116 float r = ReadByte() / 255;
117 int who = ReadByte();
118 entity e = entcs_receiver(who - 1);
119 UpdatePlayerSounds(e);
120 string s = e.(ps.m_playersoundfld);
121 string sample = GlobalSound_sample(s, r);
122 int chan = ReadSByte();
123 float vol = ReadByte() / 255;
124 float atten = ReadByte() / 64;
125 vector o = ReadVector();
126 if (who == player_currententnum) e = findfloat(NULL, entnum, who); // play at camera position for full volume
127 else if (e) e.origin = o;
130 // TODO: for non-visible players, origin should probably continue to be updated as long as the sound is playing
131 sound7(e, chan, sample, vol, atten, 0, 0);
136 LOG_WARNF("Missing entcs data for player %d", who);
137 sound8(e, o, chan, sample, vol, atten, 0, 0);
144 string GlobalSound_sample(string pair, float r)
148 string s = cdr(pair);
152 string sample = car(pair);
153 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1)); // randomization
154 else sample = sprintf("%s.wav", sample);
158 float GlobalSound_pitch(float _pitch)
160 // customizable gradient function that crosses (0,a), (c,1) and asymptotically approaches b
161 float a = 1.5; // max pitch
162 float b = 0.75; // min pitch
163 float c = 100; // standard pitch (scale * 100)
165 float pitch_shift = (b*d*(a-1) + a*c*(1-b)) / (d*(a-1) + c*(1-b));
167 return pitch_shift * 100;
170 void PrecacheGlobalSound(string sample)
174 string s = cdr(sample);
178 sample = car(sample);
181 for (int i = 1; i <= n; ++i)
182 precache_sound(sprintf("%s%d.wav", sample, i));
186 precache_sound(sprintf("%s.wav", sample));
190 entity GetVoiceMessage(string type)
192 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, return it);
196 entity GetPlayerSound(string type)
198 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, return it);
202 .string _GetPlayerSoundSampleField(string type, bool voice)
204 GetPlayerSoundSampleField_notFound = false;
205 entity e = voice ? GetVoiceMessage(type) : GetPlayerSound(type);
206 if (e) return e.m_playersoundfld;
207 GetPlayerSoundSampleField_notFound = true;
208 return playersound_taunt.m_playersoundfld;
211 .string GetVoiceMessageSampleField(string type)
213 return _GetPlayerSoundSampleField(type, true);
216 void PrecachePlayerSounds(string f)
218 int fh = fopen(f, FILE_READ);
221 LOG_WARNF("Player sound file not found: %s", f);
224 for (string s; (s = fgets(fh)); )
226 int n = tokenize_console(s);
229 if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
232 string file = argv(1);
233 string variants = argv(2);
234 PrecacheGlobalSound(strcat(file, " ", variants));
241 .string GetPlayerSoundSampleField(string type)
243 return _GetPlayerSoundSampleField(type, false);
246 void ClearPlayerSounds(entity this)
248 FOREACH(PlayerSounds, true, {
249 .string fld = it.m_playersoundfld;
257 bool LoadPlayerSounds(entity this, string f, bool strict)
259 int fh = fopen(f, FILE_READ);
262 if (strict) LOG_WARNF("Player sound file not found: %s", f);
265 for (string s; (s = fgets(fh)); )
267 int n = tokenize_console(s);
270 if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
273 string key = argv(0);
274 var.string field = GetPlayerSoundSampleField(key);
275 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
276 if (GetPlayerSoundSampleField_notFound)
278 LOG_TRACEF("Invalid sound info field in player sound file '%s': %s", f, key);
281 string file = argv(1);
282 string variants = argv(2);
283 strcpy(this.(field), strcat(file, " ", variants));
289 .string model_for_playersound;
290 .int skin_for_playersound;
292 bool autocvar_g_debug_defaultsounds;
294 void UpdatePlayerSounds(entity this)
296 if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
297 strcpy(this.model_for_playersound, this.model);
298 this.skin_for_playersound = this.skin;
299 ClearPlayerSounds(this);
300 LoadPlayerSounds(this, "sound/player/default.sounds", true);
301 if (this.model == "null"
303 && autocvar_g_debug_globalsounds
306 if (autocvar_g_debug_defaultsounds) return;
307 if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
308 LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
315 void _GlobalSound(entity this, entity gs, entity ps, string sample, int chan, float vol, int voicetype, bool fake)
317 if (gs == NULL && ps == NULL && sample == "") return;
318 if(this.classname == "body") return;
320 float myscale = ((this.scale) ? this.scale : 1); // safety net
321 float thepitch = ((myscale == 1) ? 0 : GlobalSound_pitch(myscale * 100));
322 if (sample != "") sample = GlobalSound_sample(sample, r);
325 case VOICETYPE_LASTATTACKER_ONLY:
326 case VOICETYPE_LASTATTACKER:
330 if (!this.pusher) break;
331 msg_entity = this.pusher;
332 if (IS_REAL_CLIENT(msg_entity))
334 float atten = (CS_CVAR(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
335 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten, thepitch);
336 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten, thepitch);
337 else soundto(MSG_ONE, this, chan, sample, vol, atten, thepitch);
340 if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
342 if (IS_REAL_CLIENT(msg_entity))
344 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE, thepitch);
345 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE, thepitch);
346 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE, thepitch);
350 case VOICETYPE_TEAMRADIO:
354 float atten = (CS_CVAR(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
355 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten, thepitch); \
356 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten, thepitch); \
357 else soundto(MSG_ONE, this, chan, sample, vol, atten, thepitch); \
360 if (fake) { msg_entity = this; X(); }
363 FOREACH_CLIENT(IS_REAL_CLIENT(it) && SAME_TEAM(it, this), {
371 case VOICETYPE_AUTOTAUNT:
372 case VOICETYPE_TAUNT:
374 if (voicetype == VOICETYPE_AUTOTAUNT)
376 if (!autocvar_sv_autotaunt) break;
378 else if (IS_PLAYER(this) && !IS_DEAD(this))
379 animdecide_setaction(this, ANIMACTION_TAUNT, true);
381 if (!autocvar_sv_taunt) break;
382 if (autocvar_sv_gentle) break;
384 if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
388 if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < CS_CVAR(msg_entity).cvar_cl_autotaunt) \
390 float atten = (CS_CVAR(msg_entity).cvar_cl_voice_directional >= 1) \
391 ? bound(ATTEN_MIN, CS_CVAR(msg_entity).cvar_cl_voice_directional_taunt_attenuation, \
394 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten, thepitch); \
395 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten, thepitch); \
396 else soundto(MSG_ONE, this, chan, sample, vol, atten, thepitch); \
406 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
414 case VOICETYPE_PLAYERSOUND:
419 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, ATTEN_NORM, thepitch);
420 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, ATTEN_NORM, thepitch);
421 else soundto(MSG_ONE, this, chan, sample, vol, ATTEN_NORM, thepitch);
425 if (gs) globalsound(MSG_ALL, this, gs, r, chan, vol, ATTEN_NORM, thepitch);
426 else if (ps) playersound(MSG_ALL, this, ps, r, chan, vol, ATTEN_NORM, thepitch);
427 else if (sound_allowed(MSG_BROADCAST, this)) sound7(this, chan, sample, vol, ATTEN_NORM, thepitch, 0);
433 backtrace("Invalid voice type!");