1 #include "globalsound.qh"
3 #include "../../animdecide.qh"
6 #include "../../../server/cl_player.qh"
9 REGISTER_NET_TEMP(globalsound)
10 REGISTER_NET_TEMP(playersound)
14 * @param from the source entity, its position is sent
15 * @param gs the global sound def
16 * @param r a random number in 0..1
18 void globalsound(int channel, entity from, entity gs, float r, int chan, float vol, float atten)
20 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
21 WriteHeader(channel, globalsound);
22 WriteByte(channel, gs.m_id);
23 WriteByte(channel, r * 255);
24 WriteByte(channel, etof(from));
25 WriteByte(channel, fabs(chan));
26 WriteByte(channel, floor(vol * 255));
27 WriteByte(channel, floor(atten * 64));
28 vector o = from.origin + 0.5 * (from.mins + from.maxs);
29 WriteCoord(channel, o.x);
30 WriteCoord(channel, o.y);
31 WriteCoord(channel, o.z);
35 * @param from the source entity, its position is sent
36 * @param ps the player sound def
37 * @param r a random number in 0..1
39 void playersound(int channel, entity from, entity ps, float r, int chan, float vol, float atten)
41 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
42 WriteHeader(channel, playersound);
43 WriteByte(channel, ps.m_id);
44 WriteByte(channel, r * 255);
45 WriteByte(channel, etof(from));
46 WriteByte(channel, fabs(chan));
47 WriteByte(channel, floor(vol * 255));
48 WriteByte(channel, floor(atten * 64));
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 string GlobalSound_sample(string pair, float r);
60 NET_HANDLE(globalsound, bool isnew)
62 entity gs = GlobalSounds_from(ReadByte());
63 float r = ReadByte() / 255;
64 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
66 int chan = ReadByte();
67 float vol = ReadByte() / 255;
68 float atten = ReadByte() / 64;
73 if (who == player_currententnum)
75 // client knows better, play at current position to unlag
76 entity e = findfloat(world, entnum, who);
77 sound7(e, chan, sample, vol, atten, 0, 0);
81 entity e = new(globalsound);
83 sound8(e, o, chan, sample, vol, atten, 0, 0);
84 remove(e); // debug with: e.think = SUB_Remove; e.nextthink = time + 1;
89 NET_HANDLE(playersound, bool isnew)
92 ps = PlayerSounds_from(ReadByte());
93 float r = ReadByte() / 255;
95 entity e = findfloat(world, entnum, autocvar_cl_forceplayermodels ? player_currententnum : who);
96 string s = e.(ps.m_playersoundfld); // TODO: populate this field
97 string sample = GlobalSound_sample(s, r);
98 int chan = ReadByte();
99 float vol = ReadByte() / 255;
100 float atten = ReadByte() / 64;
105 if (who == player_currententnum)
107 // client knows better, play at current position to unlag
108 sound7(e, chan, sample, vol, atten, 0, 0);
112 entity e = new(playersound);
114 sound8(e, o, chan, sample, vol, atten, 0, 0);
115 remove(e); // debug with: e.think = SUB_Remove; e.nextthink = time + 1;
122 string GlobalSound_sample(string pair, float r)
126 string s = cdr(pair);
130 string sample = car(pair);
131 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1)); // randomization
132 else sample = sprintf("%s.wav", sample);
136 void PrecacheGlobalSound(string sample)
140 string s = cdr(sample);
144 sample = car(sample);
147 for (int i = 1; i <= n; ++i)
148 precache_sound(sprintf("%s%d.wav", sample, i));
152 precache_sound(sprintf("%s.wav", sample));
158 entity GetVoiceMessage(string type)
160 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, LAMBDA(return it));
164 entity GetPlayerSound(string type)
166 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, LAMBDA(return it));
170 .string _GetPlayerSoundSampleField(string type, bool voice)
172 GetPlayerSoundSampleField_notFound = false;
173 entity e = voice ? GetVoiceMessage(type) : GetPlayerSound(type);
174 if (e) return e.m_playersoundfld;
175 GetPlayerSoundSampleField_notFound = true;
176 return playersound_taunt.m_playersoundfld;
179 .string GetVoiceMessageSampleField(string type)
181 return _GetPlayerSoundSampleField(type, true);
184 .string GetPlayerSoundSampleField(string type)
186 return _GetPlayerSoundSampleField(type, false);
189 string allvoicesamples;
191 void PrecachePlayerSounds(string f)
193 int fh = fopen(f, FILE_READ);
196 LOG_WARNINGF("Player sound file not found: %s\n", f);
199 for (string s; (s = fgets(fh)); )
201 int n = tokenize_console(s);
204 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
207 string file = argv(1);
208 string variants = argv(2);
209 PrecacheGlobalSound(strcat(file, " ", variants));
213 if (!allvoicesamples)
215 FOREACH(PlayerSounds, it.instanceOfVoiceMessage, LAMBDA(
216 allvoicesamples = strcat(allvoicesamples, " ", it.m_playersoundstr)
218 allvoicesamples = strzone(substring(allvoicesamples, 1, -1));
222 void ClearPlayerSounds(entity this)
224 FOREACH(PlayerSounds, true, LAMBDA(
225 .string fld = it.m_playersoundfld;
228 strunzone(this.(fld));
229 this.(fld) = string_null;
234 bool LoadPlayerSounds(entity this, string f, bool strict)
236 int fh = fopen(f, FILE_READ);
239 if (strict) LOG_WARNINGF("Player sound file not found: %s\n", f);
242 for (string s; (s = fgets(fh)); )
244 int n = tokenize_console(s);
247 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
250 string key = argv(0);
251 var.string field = GetPlayerSoundSampleField(key);
252 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
253 if (GetPlayerSoundSampleField_notFound)
255 LOG_TRACEF("Invalid sound info field: %s\n", key);
258 string file = argv(1);
259 string variants = argv(2);
260 if (this.(field)) strunzone(this.(field));
261 this.(field) = strzone(strcat(file, " ", variants));
267 .int modelindex_for_playersound;
268 .int skin_for_playersound;
270 void UpdatePlayerSounds(entity this)
272 if (this.modelindex == this.modelindex_for_playersound && this.skin == this.skin_for_playersound) return;
273 this.modelindex_for_playersound = this.modelindex;
274 this.skin_for_playersound = this.skin;
275 ClearPlayerSounds(this);
276 LoadPlayerSounds(this, "sound/player/default.sounds", true);
277 if (autocvar_g_debug_defaultsounds) return;
278 if (!LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false))
279 LoadPlayerSounds(this, get_model_datafilename(
285 void _GlobalSound(entity gs, entity ps, string sample, int chan, int voicetype, bool fake)
288 if (gs == NULL && ps == NULL && sample == "") return;
290 if (ps) // TODO: remove
292 sample = this.(ps.m_playersoundfld);
295 if (sample != "") sample = GlobalSound_sample(sample, r);
298 case VOICETYPE_LASTATTACKER_ONLY:
299 case VOICETYPE_LASTATTACKER:
303 if (!this.pusher) break;
304 msg_entity = this.pusher;
305 if (IS_REAL_CLIENT(msg_entity))
307 float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
308 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten);
309 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten);
310 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten);
313 if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
315 if (IS_REAL_CLIENT(msg_entity))
317 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
318 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
319 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
323 case VOICETYPE_TEAMRADIO:
328 float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
329 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
330 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
331 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
335 if (fake) { msg_entity = this; X(); }
338 FOR_EACH_REALCLIENT(msg_entity)
340 if (!teamplay || msg_entity.team == this.team) X();
346 case VOICETYPE_AUTOTAUNT:
347 case VOICETYPE_TAUNT:
349 if (voicetype == VOICETYPE_AUTOTAUNT) if (!sv_autotaunt) { break; }else {}
350 else if (IS_PLAYER(this) && this.deadflag == DEAD_NO) animdecide_setaction(this, ANIMACTION_TAUNT,
352 if (!sv_taunt) break;
353 if (autocvar_sv_gentle) break;
355 if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
359 if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < msg_entity.cvar_cl_autotaunt) \
361 float atten = (msg_entity.cvar_cl_voice_directional >= 1) \
362 ? bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, \
365 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
366 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
367 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
378 FOR_EACH_REALCLIENT(msg_entity)
386 case VOICETYPE_PLAYERSOUND:
391 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
392 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
393 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NORM);
397 if (gs) globalsound(MSG_ALL, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
398 else if (ps) playersound(MSG_ALL, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
399 else _sound(this, chan, sample, VOL_BASE, ATTEN_NORM);
405 backtrace("Invalid voice type!");