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 UpdatePlayerSounds(e);
97 string s = e.(ps.m_playersoundfld);
98 string sample = GlobalSound_sample(s, r);
99 int chan = ReadByte();
100 float vol = ReadByte() / 255;
101 float atten = ReadByte() / 64;
106 if (who == player_currententnum)
108 // client knows better, play at current position to unlag
109 sound7(e, chan, sample, vol, atten, 0, 0);
113 entity e = new(playersound);
115 sound8(e, o, chan, sample, vol, atten, 0, 0);
116 remove(e); // debug with: e.think = SUB_Remove; e.nextthink = time + 1;
123 string GlobalSound_sample(string pair, float r)
127 string s = cdr(pair);
131 string sample = car(pair);
132 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1)); // randomization
133 else sample = sprintf("%s.wav", sample);
137 void PrecacheGlobalSound(string sample)
141 string s = cdr(sample);
145 sample = car(sample);
148 for (int i = 1; i <= n; ++i)
149 precache_sound(sprintf("%s%d.wav", sample, i));
153 precache_sound(sprintf("%s.wav", sample));
157 entity GetVoiceMessage(string type)
159 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, LAMBDA(return it));
163 entity GetPlayerSound(string type)
165 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, LAMBDA(return it));
169 string allvoicesamples;
170 STATIC_INIT(allvoicesamples)
172 FOREACH(PlayerSounds, it.instanceOfVoiceMessage, LAMBDA(
173 allvoicesamples = strcat(allvoicesamples, " ", it.m_playersoundstr)
175 allvoicesamples = strzone(substring(allvoicesamples, 1, -1));
178 .string _GetPlayerSoundSampleField(string type, bool voice)
180 GetPlayerSoundSampleField_notFound = false;
181 entity e = voice ? GetVoiceMessage(type) : GetPlayerSound(type);
182 if (e) return e.m_playersoundfld;
183 GetPlayerSoundSampleField_notFound = true;
184 return playersound_taunt.m_playersoundfld;
187 .string GetVoiceMessageSampleField(string type)
189 return _GetPlayerSoundSampleField(type, true);
192 void PrecachePlayerSounds(string f)
194 int fh = fopen(f, FILE_READ);
197 LOG_WARNINGF("Player sound file not found: %s\n", f);
200 for (string s; (s = fgets(fh)); )
202 int n = tokenize_console(s);
205 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
208 string file = argv(1);
209 string variants = argv(2);
210 PrecacheGlobalSound(strcat(file, " ", variants));
217 .string GetPlayerSoundSampleField(string type)
219 return _GetPlayerSoundSampleField(type, false);
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 bool autocvar_g_debug_defaultsounds;
272 void UpdatePlayerSounds(entity this)
274 if (this.modelindex == this.modelindex_for_playersound && this.skin == this.skin_for_playersound) return;
275 this.modelindex_for_playersound = this.modelindex;
276 this.skin_for_playersound = this.skin;
277 ClearPlayerSounds(this);
278 LoadPlayerSounds(this, "sound/player/default.sounds", true);
279 if (this.model == "null" || autocvar_g_debug_defaultsounds) return;
280 if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
281 LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
288 void _GlobalSound(entity gs, entity ps, string sample, int chan, int voicetype, bool fake)
291 if (gs == NULL && ps == NULL && sample == "") return;
293 if (sample != "") sample = GlobalSound_sample(sample, r);
296 case VOICETYPE_LASTATTACKER_ONLY:
297 case VOICETYPE_LASTATTACKER:
301 if (!this.pusher) break;
302 msg_entity = this.pusher;
303 if (IS_REAL_CLIENT(msg_entity))
305 float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
306 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten);
307 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten);
308 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten);
311 if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
313 if (IS_REAL_CLIENT(msg_entity))
315 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
316 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
317 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
321 case VOICETYPE_TEAMRADIO:
326 float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
327 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
328 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
329 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
333 if (fake) { msg_entity = this; X(); }
336 FOR_EACH_REALCLIENT(msg_entity)
338 if (!teamplay || msg_entity.team == this.team) X();
344 case VOICETYPE_AUTOTAUNT:
345 case VOICETYPE_TAUNT:
347 if (voicetype == VOICETYPE_AUTOTAUNT) if (!sv_autotaunt) { break; }else {}
348 else if (IS_PLAYER(this) && this.deadflag == DEAD_NO) animdecide_setaction(this, ANIMACTION_TAUNT,
350 if (!sv_taunt) break;
351 if (autocvar_sv_gentle) break;
353 if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
357 if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < msg_entity.cvar_cl_autotaunt) \
359 float atten = (msg_entity.cvar_cl_voice_directional >= 1) \
360 ? bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, \
363 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
364 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
365 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
376 FOR_EACH_REALCLIENT(msg_entity)
384 case VOICETYPE_PLAYERSOUND:
389 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
390 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
391 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NORM);
395 if (gs) globalsound(MSG_ALL, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
396 else if (ps) playersound(MSG_ALL, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
397 else _sound(this, chan, sample, VOL_BASE, ATTEN_NORM);
403 backtrace("Invalid voice type!");