1 #include "globalsound.qh"
3 #include <common/ent_cs.qh>
5 #include <common/animdecide.qh>
8 #include <server/player.qh>
11 REGISTER_NET_TEMP(globalsound)
12 REGISTER_NET_TEMP(playersound)
14 string GlobalSound_sample(string pair, float r);
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)
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);
33 _sound(from, chan, sample, _vol, _atten);
38 WriteHeader(channel, globalsound);
39 WriteByte(channel, gs.m_id);
40 WriteByte(channel, r * 255);
41 WriteByte(channel, etof(from));
42 WriteByte(channel, chan);
43 WriteByte(channel, floor(_vol * 255));
44 WriteByte(channel, floor(_atten * 64));
45 entcs_force_origin(from);
46 vector o = from.origin + 0.5 * (from.mins + from.maxs);
47 WriteCoord(channel, o.x);
48 WriteCoord(channel, o.y);
49 WriteCoord(channel, o.z);
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)
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);
70 _sound(from, chan, sample, _vol, _atten);
75 WriteHeader(channel, playersound);
76 WriteByte(channel, ps.m_id);
77 WriteByte(channel, r * 255);
78 WriteByte(channel, etof(from));
79 WriteByte(channel, chan);
80 WriteByte(channel, floor(_vol * 255));
81 WriteByte(channel, floor(_atten * 64));
82 entcs_force_origin(from);
83 vector o = from.origin + 0.5 * (from.mins + from.maxs);
84 WriteCoord(channel, o.x);
85 WriteCoord(channel, o.y);
86 WriteCoord(channel, o.z);
92 NET_HANDLE(globalsound, bool isnew)
94 entity gs = GlobalSounds_from(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;
106 // TODO: is this really what we want to be doing? Footsteps that follow the player at head height?
107 if (who == player_currententnum) e = findfloat(NULL, entnum, who); // play at camera position for full volume
108 else if (e) e.origin = o;
111 sound7(e, chan, sample, vol, atten, 0, 0);
116 LOG_WARNF("Missing entcs data for player %d", who);
117 sound8(e, o, chan, sample, vol, atten, 0, 0);
122 NET_HANDLE(playersound, bool isnew)
124 entity ps = PlayerSounds_from(ReadByte());
125 float r = ReadByte() / 255;
126 int who = ReadByte();
127 entity e = entcs_receiver(who - 1);
128 UpdatePlayerSounds(e);
129 string s = e.(ps.m_playersoundfld);
130 string sample = GlobalSound_sample(s, r);
131 int chan = ReadSByte();
132 float vol = ReadByte() / 255;
133 float atten = ReadByte() / 64;
138 if (who == player_currententnum) e = findfloat(NULL, entnum, who); // play at camera position for full volume
139 else if (e) e.origin = o;
142 // TODO: for non-visible players, origin should probably continue to be updated as long as the sound is playing
143 sound7(e, chan, sample, vol, atten, 0, 0);
148 LOG_WARNF("Missing entcs data for player %d", who);
149 sound8(e, o, chan, sample, vol, atten, 0, 0);
156 string GlobalSound_sample(string pair, float r)
160 string s = cdr(pair);
164 string sample = car(pair);
165 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1)); // randomization
166 else sample = sprintf("%s.wav", sample);
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;
252 strunzone(this.(fld));
253 this.(fld) = string_null;
258 bool LoadPlayerSounds(entity this, string f, bool strict)
260 int fh = fopen(f, FILE_READ);
263 if (strict) LOG_WARNF("Player sound file not found: %s", f);
266 for (string s; (s = fgets(fh)); )
268 int n = tokenize_console(s);
271 if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
274 string key = argv(0);
275 var.string field = GetPlayerSoundSampleField(key);
276 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
277 if (GetPlayerSoundSampleField_notFound)
279 LOG_TRACEF("Invalid sound info field: %s", key);
282 string file = argv(1);
283 string variants = argv(2);
284 if (this.(field)) strunzone(this.(field));
285 this.(field) = strzone(strcat(file, " ", variants));
291 .string model_for_playersound;
292 .int skin_for_playersound;
294 bool autocvar_g_debug_defaultsounds;
296 void UpdatePlayerSounds(entity this)
298 if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
299 if (this.model_for_playersound) strunzone(this.model_for_playersound);
300 this.model_for_playersound = strzone(this.model);
301 this.skin_for_playersound = this.skin;
302 ClearPlayerSounds(this);
303 LoadPlayerSounds(this, "sound/player/default.sounds", true);
304 if (this.model == "null"
306 && autocvar_g_debug_globalsounds
309 if (autocvar_g_debug_defaultsounds) return;
310 if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
311 LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
318 void _GlobalSound(entity this, entity gs, entity ps, string sample, int chan, float vol, int voicetype, bool fake)
320 if (gs == NULL && ps == NULL && sample == "") return;
321 if(this.classname == "body") return;
323 if (sample != "") sample = GlobalSound_sample(sample, r);
326 case VOICETYPE_LASTATTACKER_ONLY:
327 case VOICETYPE_LASTATTACKER:
331 if (!this.pusher) break;
332 msg_entity = this.pusher;
333 if (IS_REAL_CLIENT(msg_entity))
335 float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
336 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten);
337 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten);
338 else soundto(MSG_ONE, this, chan, sample, vol, atten);
341 if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
343 if (IS_REAL_CLIENT(msg_entity))
345 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
346 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
347 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
351 case VOICETYPE_TEAMRADIO:
356 float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
357 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten); \
358 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten); \
359 else soundto(MSG_ONE, this, chan, sample, vol, atten); \
362 if (fake) { msg_entity = this; X(); }
365 FOREACH_CLIENT(IS_REAL_CLIENT(it) && SAME_TEAM(it, this), {
373 case VOICETYPE_AUTOTAUNT:
374 case VOICETYPE_TAUNT:
376 if (voicetype == VOICETYPE_AUTOTAUNT) if (!sv_autotaunt) { break; }else {}
377 else if (IS_PLAYER(this) && !IS_DEAD(this)) animdecide_setaction(this, ANIMACTION_TAUNT,
379 if (!sv_taunt) break;
380 if (autocvar_sv_gentle) break;
382 if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
386 if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < CS(msg_entity).cvar_cl_autotaunt) \
388 float atten = (CS(msg_entity).cvar_cl_voice_directional >= 1) \
389 ? bound(ATTEN_MIN, CS(msg_entity).cvar_cl_voice_directional_taunt_attenuation, \
392 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten); \
393 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten); \
394 else soundto(MSG_ONE, this, chan, sample, vol, atten); \
404 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
412 case VOICETYPE_PLAYERSOUND:
417 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, ATTEN_NORM);
418 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, ATTEN_NORM);
419 else soundto(MSG_ONE, this, chan, sample, vol, ATTEN_NORM);
423 if (gs) globalsound(MSG_ALL, this, gs, r, chan, vol, ATTEN_NORM);
424 else if (ps) playersound(MSG_ALL, this, ps, r, chan, vol, ATTEN_NORM);
425 else _sound(this, chan, sample, vol, ATTEN_NORM);
431 backtrace("Invalid voice type!");