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 WriteVector(channel, o);
49 * @param from the source entity, its position is sent
50 * @param ps the player sound def
51 * @param r a random number in 0..1
53 void playersound(int channel, entity from, entity ps, float r, int chan, float _vol, float _atten)
55 //assert(IS_PLAYER(from), eprint(from));
56 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
57 if (!autocvar_g_debug_globalsounds) {
58 //UpdatePlayerSounds(from);
59 string s = from.(ps.m_playersoundfld);
60 string sample = GlobalSound_sample(s, r);
63 soundto(channel, from, chan, sample, _vol, _atten);
66 _sound(from, chan, sample, _vol, _atten);
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 void PrecacheGlobalSound(string sample)
162 string s = cdr(sample);
166 sample = car(sample);
169 for (int i = 1; i <= n; ++i)
170 precache_sound(sprintf("%s%d.wav", sample, i));
174 precache_sound(sprintf("%s.wav", sample));
178 entity GetVoiceMessage(string type)
180 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, return it);
184 entity GetPlayerSound(string type)
186 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, return it);
190 .string _GetPlayerSoundSampleField(string type, bool voice)
192 GetPlayerSoundSampleField_notFound = false;
193 entity e = voice ? GetVoiceMessage(type) : GetPlayerSound(type);
194 if (e) return e.m_playersoundfld;
195 GetPlayerSoundSampleField_notFound = true;
196 return playersound_taunt.m_playersoundfld;
199 .string GetVoiceMessageSampleField(string type)
201 return _GetPlayerSoundSampleField(type, true);
204 void PrecachePlayerSounds(string f)
206 int fh = fopen(f, FILE_READ);
209 LOG_WARNF("Player sound file not found: %s", f);
212 for (string s; (s = fgets(fh)); )
214 int n = tokenize_console(s);
217 if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
220 string file = argv(1);
221 string variants = argv(2);
222 PrecacheGlobalSound(strcat(file, " ", variants));
229 .string GetPlayerSoundSampleField(string type)
231 return _GetPlayerSoundSampleField(type, false);
234 void ClearPlayerSounds(entity this)
236 FOREACH(PlayerSounds, true, {
237 .string fld = it.m_playersoundfld;
245 bool LoadPlayerSounds(entity this, string f, bool strict)
247 int fh = fopen(f, FILE_READ);
250 if (strict) LOG_WARNF("Player sound file not found: %s", f);
253 for (string s; (s = fgets(fh)); )
255 int n = tokenize_console(s);
258 if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
261 string key = argv(0);
262 var.string field = GetPlayerSoundSampleField(key);
263 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
264 if (GetPlayerSoundSampleField_notFound)
266 LOG_TRACEF("Invalid sound info field in player sound file '%s': %s", f, key);
269 string file = argv(1);
270 string variants = argv(2);
271 strcpy(this.(field), strcat(file, " ", variants));
277 .string model_for_playersound;
278 .int skin_for_playersound;
280 bool autocvar_g_debug_defaultsounds;
282 void UpdatePlayerSounds(entity this)
284 if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
285 strcpy(this.model_for_playersound, this.model);
286 this.skin_for_playersound = this.skin;
287 ClearPlayerSounds(this);
288 LoadPlayerSounds(this, "sound/player/default.sounds", true);
289 if (this.model == "null"
291 && autocvar_g_debug_globalsounds
294 if (autocvar_g_debug_defaultsounds) return;
295 if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
296 LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
303 void _GlobalSound(entity this, entity gs, entity ps, string sample, int chan, float vol, int voicetype, bool fake)
305 if (gs == NULL && ps == NULL && sample == "") return;
306 if(this.classname == "body") return;
308 if (sample != "") sample = GlobalSound_sample(sample, r);
311 case VOICETYPE_LASTATTACKER_ONLY:
312 case VOICETYPE_LASTATTACKER:
316 if (!this.pusher) break;
317 msg_entity = this.pusher;
318 if (IS_REAL_CLIENT(msg_entity))
320 float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
321 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten);
322 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten);
323 else soundto(MSG_ONE, this, chan, sample, vol, atten);
326 if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
328 if (IS_REAL_CLIENT(msg_entity))
330 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
331 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
332 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
336 case VOICETYPE_TEAMRADIO:
340 float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
341 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten); \
342 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten); \
343 else soundto(MSG_ONE, this, chan, sample, vol, atten); \
346 if (fake) { msg_entity = this; X(); }
349 FOREACH_CLIENT(IS_REAL_CLIENT(it) && SAME_TEAM(it, this), {
357 case VOICETYPE_AUTOTAUNT:
358 case VOICETYPE_TAUNT:
360 if (voicetype == VOICETYPE_AUTOTAUNT)
362 if (!sv_autotaunt) break;
364 else if (IS_PLAYER(this) && !IS_DEAD(this))
365 animdecide_setaction(this, ANIMACTION_TAUNT, true);
367 if (!sv_taunt) break;
368 if (autocvar_sv_gentle) break;
370 if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
374 if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < CS(msg_entity).cvar_cl_autotaunt) \
376 float atten = (CS(msg_entity).cvar_cl_voice_directional >= 1) \
377 ? bound(ATTEN_MIN, CS(msg_entity).cvar_cl_voice_directional_taunt_attenuation, \
380 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten); \
381 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten); \
382 else soundto(MSG_ONE, this, chan, sample, vol, atten); \
392 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
400 case VOICETYPE_PLAYERSOUND:
405 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, ATTEN_NORM);
406 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, ATTEN_NORM);
407 else soundto(MSG_ONE, this, chan, sample, vol, ATTEN_NORM);
411 if (gs) globalsound(MSG_ALL, this, gs, r, chan, vol, ATTEN_NORM);
412 else if (ps) playersound(MSG_ALL, this, ps, r, chan, vol, ATTEN_NORM);
413 else _sound(this, chan, sample, vol, ATTEN_NORM);
419 backtrace("Invalid voice type!");