1 #include "globalsound.qh"
3 #include <common/ent_cs.qh>
6 #include <common/animdecide.qh>
9 #include <server/cl_player.qh>
12 REGISTER_NET_TEMP(globalsound)
13 REGISTER_NET_TEMP(playersound)
15 string GlobalSound_sample(string pair, float r);
18 /** Use new sound handling. TODO: use when sounds play correctly on clients */
19 bool autocvar_g_debug_globalsounds = false;
21 * @param from the source entity, its position is sent
22 * @param gs the global sound def
23 * @param r a random number in 0..1
25 void globalsound(int channel, entity from, entity gs, float r, int chan, float vol, float atten)
27 assert(IS_PLAYER(from), eprint(from));
28 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
29 if (!autocvar_g_debug_globalsounds) {
30 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
33 soundto(channel, from, chan, sample, vol, atten);
36 _sound(from, chan, sample, vol, atten);
41 WriteHeader(channel, globalsound);
42 WriteByte(channel, gs.m_id);
43 WriteByte(channel, r * 255);
44 WriteByte(channel, etof(from));
45 WriteByte(channel, chan);
46 WriteByte(channel, floor(vol * 255));
47 WriteByte(channel, floor(atten * 64));
48 entcs_force_origin(from);
49 vector o = from.origin + 0.5 * (from.mins + from.maxs);
50 WriteCoord(channel, o.x);
51 WriteCoord(channel, o.y);
52 WriteCoord(channel, o.z);
56 * @param from the source entity, its position is sent
57 * @param ps the player sound def
58 * @param r a random number in 0..1
60 void playersound(int channel, entity from, entity ps, float r, int chan, float vol, float atten)
62 assert(IS_PLAYER(from), eprint(from));
63 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
64 if (!autocvar_g_debug_globalsounds) {
65 UpdatePlayerSounds(from);
66 string s = from.(ps.m_playersoundfld);
67 string sample = GlobalSound_sample(s, r);
70 soundto(channel, from, chan, sample, vol, atten);
73 _sound(from, chan, sample, vol, atten);
78 WriteHeader(channel, playersound);
79 WriteByte(channel, ps.m_id);
80 WriteByte(channel, r * 255);
81 WriteByte(channel, etof(from));
82 WriteByte(channel, chan);
83 WriteByte(channel, floor(vol * 255));
84 WriteByte(channel, floor(atten * 64));
85 entcs_force_origin(from);
86 vector o = from.origin + 0.5 * (from.mins + from.maxs);
87 WriteCoord(channel, o.x);
88 WriteCoord(channel, o.y);
89 WriteCoord(channel, o.z);
95 NET_HANDLE(globalsound, bool isnew)
97 entity gs = GlobalSounds_from(ReadByte());
98 float r = ReadByte() / 255;
99 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
100 int who = ReadByte();
101 entity e = entcs_receiver(who - 1);
102 int chan = ReadSByte();
103 float vol = ReadByte() / 255;
104 float atten = ReadByte() / 64;
109 // TODO: is this really what we want to be doing? Footsteps that follow the player at head height?
110 if (who == player_currententnum) e = findfloat(world, entnum, who); // play at camera position for full volume
111 else if (e) e.origin = o;
114 sound7(e, chan, sample, vol, atten, 0, 0);
119 LOG_WARNINGF("Missing entcs data for player %d\n", who);
120 sound8(e, o, chan, sample, vol, atten, 0, 0);
125 NET_HANDLE(playersound, bool isnew)
127 entity ps = PlayerSounds_from(ReadByte());
128 float r = ReadByte() / 255;
129 int who = ReadByte();
130 entity e = entcs_receiver(who - 1);
131 UpdatePlayerSounds(e);
132 string s = e.(ps.m_playersoundfld);
133 string sample = GlobalSound_sample(s, r);
134 int chan = ReadSByte();
135 float vol = ReadByte() / 255;
136 float atten = ReadByte() / 64;
141 if (who == player_currententnum) e = findfloat(world, entnum, who); // play at camera position for full volume
142 else if (e) e.origin = o;
145 // TODO: for non-visible players, origin should probably continue to be updated as long as the sound is playing
146 sound7(e, chan, sample, vol, atten, 0, 0);
151 LOG_WARNINGF("Missing entcs data for player %d\n", who);
152 sound8(e, o, chan, sample, vol, atten, 0, 0);
159 string GlobalSound_sample(string pair, float r)
163 string s = cdr(pair);
167 string sample = car(pair);
168 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1)); // randomization
169 else sample = sprintf("%s.wav", sample);
173 void PrecacheGlobalSound(string sample)
177 string s = cdr(sample);
181 sample = car(sample);
184 for (int i = 1; i <= n; ++i)
185 precache_sound(sprintf("%s%d.wav", sample, i));
189 precache_sound(sprintf("%s.wav", sample));
193 entity GetVoiceMessage(string type)
195 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, return it);
199 entity GetPlayerSound(string type)
201 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, return it);
205 string allvoicesamples;
206 STATIC_INIT(allvoicesamples)
208 FOREACH(PlayerSounds, it.instanceOfVoiceMessage, allvoicesamples = strcat(allvoicesamples, " ", it.m_playersoundstr));
209 allvoicesamples = strzone(substring(allvoicesamples, 1, -1));
212 .string _GetPlayerSoundSampleField(string type, bool voice)
214 GetPlayerSoundSampleField_notFound = false;
215 entity e = voice ? GetVoiceMessage(type) : GetPlayerSound(type);
216 if (e) return e.m_playersoundfld;
217 GetPlayerSoundSampleField_notFound = true;
218 return playersound_taunt.m_playersoundfld;
221 .string GetVoiceMessageSampleField(string type)
223 return _GetPlayerSoundSampleField(type, true);
226 void PrecachePlayerSounds(string f)
228 int fh = fopen(f, FILE_READ);
231 LOG_WARNINGF("Player sound file not found: %s\n", f);
234 for (string s; (s = fgets(fh)); )
236 int n = tokenize_console(s);
239 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
242 string file = argv(1);
243 string variants = argv(2);
244 PrecacheGlobalSound(strcat(file, " ", variants));
251 .string GetPlayerSoundSampleField(string type)
253 return _GetPlayerSoundSampleField(type, false);
256 void ClearPlayerSounds(entity this)
258 FOREACH(PlayerSounds, true, {
259 .string fld = it.m_playersoundfld;
262 strunzone(this.(fld));
263 this.(fld) = string_null;
268 bool LoadPlayerSounds(entity this, string f, bool strict)
270 int fh = fopen(f, FILE_READ);
273 if (strict) LOG_WARNINGF("Player sound file not found: %s\n", f);
276 for (string s; (s = fgets(fh)); )
278 int n = tokenize_console(s);
281 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
284 string key = argv(0);
285 var.string field = GetPlayerSoundSampleField(key);
286 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
287 if (GetPlayerSoundSampleField_notFound)
289 LOG_TRACEF("Invalid sound info field: %s\n", key);
292 string file = argv(1);
293 string variants = argv(2);
294 if (this.(field)) strunzone(this.(field));
295 this.(field) = strzone(strcat(file, " ", variants));
301 .string model_for_playersound;
302 .int skin_for_playersound;
304 bool autocvar_g_debug_defaultsounds;
306 void UpdatePlayerSounds(entity this)
308 if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
309 if (this.model_for_playersound) strunzone(this.model_for_playersound);
310 this.model_for_playersound = strzone(this.model);
311 this.skin_for_playersound = this.skin;
312 ClearPlayerSounds(this);
313 LoadPlayerSounds(this, "sound/player/default.sounds", true);
314 if (this.model == "null" || autocvar_g_debug_defaultsounds) return;
315 if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
316 LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
323 void _GlobalSound(entity this, entity gs, entity ps, string sample, int chan, int voicetype, bool fake)
325 if (gs == NULL && ps == NULL && sample == "") return;
326 if(this.classname == "body") return;
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 = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
341 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten);
342 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten);
343 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten);
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);
351 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
352 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
356 case VOICETYPE_TEAMRADIO:
361 float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
362 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
363 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
364 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
367 if (fake) { msg_entity = this; X(); }
370 FOREACH_CLIENT(IS_REAL_CLIENT(it) && (!teamplay || msg_entity.team == this.team), {
378 case VOICETYPE_AUTOTAUNT:
379 case VOICETYPE_TAUNT:
381 if (voicetype == VOICETYPE_AUTOTAUNT) if (!sv_autotaunt) { break; }else {}
382 else if (IS_PLAYER(this) && !IS_DEAD(this)) animdecide_setaction(this, ANIMACTION_TAUNT,
384 if (!sv_taunt) break;
385 if (autocvar_sv_gentle) break;
387 if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
391 if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < msg_entity.cvar_cl_autotaunt) \
393 float atten = (msg_entity.cvar_cl_voice_directional >= 1) \
394 ? bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, \
397 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
398 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
399 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
409 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
417 case VOICETYPE_PLAYERSOUND:
422 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
423 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
424 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NORM);
428 if (gs) globalsound(MSG_ALL, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
429 else if (ps) playersound(MSG_ALL, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
430 else _sound(this, chan, sample, VOL_BASE, ATTEN_NORM);
436 backtrace("Invalid voice type!");