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)
16 * @param from the source entity, its position is sent
17 * @param gs the global sound def
18 * @param r a random number in 0..1
20 void globalsound(int channel, entity from, entity gs, float r, int chan, float _vol, float _atten)
22 //assert(IS_PLAYER(from), eprint(from));
23 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
24 if (!autocvar_g_debug_globalsounds) {
25 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
28 soundto(channel, from, chan, sample, _vol, _atten);
31 _sound(from, chan, sample, _vol, _atten);
36 WriteHeader(channel, globalsound);
37 WriteByte(channel, gs.m_id);
38 WriteByte(channel, r * 255);
39 WriteByte(channel, etof(from));
40 WriteByte(channel, chan);
41 WriteByte(channel, floor(_vol * 255));
42 WriteByte(channel, floor(_atten * 64));
43 entcs_force_origin(from);
44 vector o = from.origin + 0.5 * (from.mins + from.maxs);
45 WriteCoord(channel, o.x);
46 WriteCoord(channel, o.y);
47 WriteCoord(channel, o.z);
51 * @param from the source entity, its position is sent
52 * @param ps the player sound def
53 * @param r a random number in 0..1
55 void playersound(int channel, entity from, entity ps, float r, int chan, float _vol, float _atten)
57 //assert(IS_PLAYER(from), eprint(from));
58 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
59 if (!autocvar_g_debug_globalsounds) {
60 //UpdatePlayerSounds(from);
61 string s = from.(ps.m_playersoundfld);
62 string sample = GlobalSound_sample(s, r);
65 soundto(channel, from, chan, sample, _vol, _atten);
68 _sound(from, chan, sample, _vol, _atten);
73 WriteHeader(channel, playersound);
74 WriteByte(channel, ps.m_id);
75 WriteByte(channel, r * 255);
76 WriteByte(channel, etof(from));
77 WriteByte(channel, chan);
78 WriteByte(channel, floor(_vol * 255));
79 WriteByte(channel, floor(_atten * 64));
80 entcs_force_origin(from);
81 vector o = from.origin + 0.5 * (from.mins + from.maxs);
82 WriteCoord(channel, o.x);
83 WriteCoord(channel, o.y);
84 WriteCoord(channel, o.z);
90 NET_HANDLE(globalsound, bool isnew)
92 entity gs = GlobalSounds_from(ReadByte());
93 float r = ReadByte() / 255;
94 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
96 entity e = entcs_receiver(who - 1);
97 int chan = ReadSByte();
98 float vol = ReadByte() / 255;
99 float atten = ReadByte() / 64;
104 // TODO: is this really what we want to be doing? Footsteps that follow the player at head height?
105 if (who == player_currententnum) e = findfloat(NULL, entnum, who); // play at camera position for full volume
106 else if (e) e.origin = o;
109 sound7(e, chan, sample, vol, atten, 0, 0);
114 LOG_WARNF("Missing entcs data for player %d", who);
115 sound8(e, o, chan, sample, vol, atten, 0, 0);
120 NET_HANDLE(playersound, bool isnew)
122 entity ps = PlayerSounds_from(ReadByte());
123 float r = ReadByte() / 255;
124 int who = ReadByte();
125 entity e = entcs_receiver(who - 1);
126 UpdatePlayerSounds(e);
127 string s = e.(ps.m_playersoundfld);
128 string sample = GlobalSound_sample(s, r);
129 int chan = ReadSByte();
130 float vol = ReadByte() / 255;
131 float atten = ReadByte() / 64;
136 if (who == player_currententnum) e = findfloat(NULL, entnum, who); // play at camera position for full volume
137 else if (e) e.origin = o;
140 // TODO: for non-visible players, origin should probably continue to be updated as long as the sound is playing
141 sound7(e, chan, sample, vol, atten, 0, 0);
146 LOG_WARNF("Missing entcs data for player %d", who);
147 sound8(e, o, chan, sample, vol, atten, 0, 0);
154 string GlobalSound_sample(string pair, float r)
158 string s = cdr(pair);
162 string sample = car(pair);
163 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1)); // randomization
164 else sample = sprintf("%s.wav", sample);
168 void PrecacheGlobalSound(string sample)
172 string s = cdr(sample);
176 sample = car(sample);
179 for (int i = 1; i <= n; ++i)
180 precache_sound(sprintf("%s%d.wav", sample, i));
184 precache_sound(sprintf("%s.wav", sample));
188 entity GetVoiceMessage(string type)
190 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, return it);
194 entity GetPlayerSound(string type)
196 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, return it);
200 .string _GetPlayerSoundSampleField(string type, bool voice)
202 GetPlayerSoundSampleField_notFound = false;
203 entity e = voice ? GetVoiceMessage(type) : GetPlayerSound(type);
204 if (e) return e.m_playersoundfld;
205 GetPlayerSoundSampleField_notFound = true;
206 return playersound_taunt.m_playersoundfld;
209 .string GetVoiceMessageSampleField(string type)
211 return _GetPlayerSoundSampleField(type, true);
214 void PrecachePlayerSounds(string f)
216 int fh = fopen(f, FILE_READ);
219 LOG_WARNF("Player sound file not found: %s", f);
222 for (string s; (s = fgets(fh)); )
224 int n = tokenize_console(s);
227 if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
230 string file = argv(1);
231 string variants = argv(2);
232 PrecacheGlobalSound(strcat(file, " ", variants));
239 .string GetPlayerSoundSampleField(string type)
241 return _GetPlayerSoundSampleField(type, false);
244 void ClearPlayerSounds(entity this)
246 FOREACH(PlayerSounds, true, {
247 .string fld = it.m_playersoundfld;
250 strunzone(this.(fld));
251 this.(fld) = string_null;
256 bool LoadPlayerSounds(entity this, string f, bool strict)
258 int fh = fopen(f, FILE_READ);
261 if (strict) LOG_WARNF("Player sound file not found: %s", f);
264 for (string s; (s = fgets(fh)); )
266 int n = tokenize_console(s);
269 if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
272 string key = argv(0);
273 var.string field = GetPlayerSoundSampleField(key);
274 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
275 if (GetPlayerSoundSampleField_notFound)
277 LOG_TRACEF("Invalid sound info field: %s", key);
280 string file = argv(1);
281 string variants = argv(2);
282 if (this.(field)) strunzone(this.(field));
283 this.(field) = strzone(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 if (this.model_for_playersound) strunzone(this.model_for_playersound);
298 this.model_for_playersound = strzone(this.model);
299 this.skin_for_playersound = this.skin;
300 ClearPlayerSounds(this);
301 LoadPlayerSounds(this, "sound/player/default.sounds", true);
302 if (this.model == "null"
304 && autocvar_g_debug_globalsounds
307 if (autocvar_g_debug_defaultsounds) return;
308 if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
309 LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
316 void _GlobalSound(entity this, entity gs, entity ps, string sample, int chan, float vol, int voicetype, bool fake)
318 if (gs == NULL && ps == NULL && sample == "") return;
319 if(this.classname == "body") return;
321 if (sample != "") sample = GlobalSound_sample(sample, r);
324 case VOICETYPE_LASTATTACKER_ONLY:
325 case VOICETYPE_LASTATTACKER:
329 if (!this.pusher) break;
330 msg_entity = this.pusher;
331 if (IS_REAL_CLIENT(msg_entity))
333 float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
334 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten);
335 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten);
336 else soundto(MSG_ONE, this, chan, sample, vol, atten);
339 if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
341 if (IS_REAL_CLIENT(msg_entity))
343 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
344 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
345 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
349 case VOICETYPE_TEAMRADIO:
354 float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
355 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten); \
356 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten); \
357 else soundto(MSG_ONE, this, chan, sample, vol, atten); \
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) if (!sv_autotaunt) { break; }else {}
375 else if (IS_PLAYER(this) && !IS_DEAD(this)) animdecide_setaction(this, ANIMACTION_TAUNT,
377 if (!sv_taunt) break;
378 if (autocvar_sv_gentle) break;
380 if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
384 if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < CS(msg_entity).cvar_cl_autotaunt) \
386 float atten = (CS(msg_entity).cvar_cl_voice_directional >= 1) \
387 ? bound(ATTEN_MIN, CS(msg_entity).cvar_cl_voice_directional_taunt_attenuation, \
390 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten); \
391 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten); \
392 else soundto(MSG_ONE, this, chan, sample, vol, atten); \
402 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
410 case VOICETYPE_PLAYERSOUND:
415 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, ATTEN_NORM);
416 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, ATTEN_NORM);
417 else soundto(MSG_ONE, this, chan, sample, vol, ATTEN_NORM);
421 if (gs) globalsound(MSG_ALL, this, gs, r, chan, vol, ATTEN_NORM);
422 else if (ps) playersound(MSG_ALL, this, ps, r, chan, vol, ATTEN_NORM);
423 else _sound(this, chan, sample, vol, ATTEN_NORM);
429 backtrace("Invalid voice type!");