1 #include "globalsound.qh"
3 #include "../common/ent_cs.qh"
6 #include "../../animdecide.qh"
9 #include "../../../server/cl_player.qh"
12 REGISTER_NET_TEMP(globalsound)
13 REGISTER_NET_TEMP(playersound)
17 * @param from the source entity, its position is sent
18 * @param gs the global sound def
19 * @param r a random number in 0..1
21 void globalsound(int channel, entity from, entity gs, float r, int chan, float vol, float atten)
23 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
24 WriteHeader(channel, globalsound);
25 WriteByte(channel, gs.m_id);
26 WriteByte(channel, r * 255);
27 WriteByte(channel, etof(from));
28 WriteByte(channel, fabs(chan));
29 WriteByte(channel, floor(vol * 255));
30 WriteByte(channel, floor(atten * 64));
31 entcs_force_origin(from);
32 vector o = from.origin + 0.5 * (from.mins + from.maxs);
33 WriteCoord(channel, o.x);
34 WriteCoord(channel, o.y);
35 WriteCoord(channel, o.z);
39 * @param from the source entity, its position is sent
40 * @param ps the player sound def
41 * @param r a random number in 0..1
43 void playersound(int channel, entity from, entity ps, float r, int chan, float vol, float atten)
45 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
46 WriteHeader(channel, playersound);
47 WriteByte(channel, ps.m_id);
48 WriteByte(channel, r * 255);
49 WriteByte(channel, etof(from));
50 WriteByte(channel, fabs(chan));
51 WriteByte(channel, floor(vol * 255));
52 WriteByte(channel, floor(atten * 64));
53 entcs_force_origin(from);
54 vector o = from.origin + 0.5 * (from.mins + from.maxs);
55 WriteCoord(channel, o.x);
56 WriteCoord(channel, o.y);
57 WriteCoord(channel, o.z);
61 string GlobalSound_sample(string pair, float r);
65 NET_HANDLE(globalsound, bool isnew)
67 entity gs = GlobalSounds_from(ReadByte());
68 float r = ReadByte() / 255;
69 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
71 entity e = entcs_receiver(who - 1);
72 int chan = ReadByte();
73 float vol = ReadByte() / 255;
74 float atten = ReadByte() / 64;
79 // TODO: is this really what we want to be doing? Footsteps that follow the player at head height?
80 if (who == player_currententnum) e = findfloat(world, entnum, who); // play at camera position for full volume
81 else if (e) e.origin = o;
84 sound7(e, chan, sample, vol, atten, 0, 0);
88 LOG_WARNINGF("Missing entcs data for player %d\n", who);
90 entity e = new(globalsound);
92 sound8(e, o, chan, sample, vol, atten, 0, 0);
93 remove(e); // debug with: e.think = SUB_Remove; e.nextthink = time + 1;
98 NET_HANDLE(playersound, bool isnew)
100 entity ps = PlayerSounds_from(ReadByte());
101 float r = ReadByte() / 255;
102 int who = ReadByte();
103 entity e = entcs_receiver(who - 1);
104 UpdatePlayerSounds(e);
105 string s = e.(ps.m_playersoundfld);
106 string sample = GlobalSound_sample(s, r);
107 int chan = ReadByte();
108 float vol = ReadByte() / 255;
109 float atten = ReadByte() / 64;
114 if (who == player_currententnum) e = findfloat(world, entnum, who); // play at camera position for full volume
115 else if (e) e.origin = o;
118 // TODO: for non-visible players, origin should probably continue to be updated as long as the sound is playing
119 sound7(e, chan, sample, vol, atten, 0, 0);
123 LOG_WARNINGF("Missing entcs data for player %d\n", who);
125 entity e = new(playersound);
127 sound8(e, o, chan, sample, vol, atten, 0, 0);
128 remove(e); // debug with: e.think = SUB_Remove; e.nextthink = time + 1;
135 string GlobalSound_sample(string pair, float r)
139 string s = cdr(pair);
143 string sample = car(pair);
144 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1)); // randomization
145 else sample = sprintf("%s.wav", sample);
149 void PrecacheGlobalSound(string sample)
153 string s = cdr(sample);
157 sample = car(sample);
160 for (int i = 1; i <= n; ++i)
161 precache_sound(sprintf("%s%d.wav", sample, i));
165 precache_sound(sprintf("%s.wav", sample));
169 entity GetVoiceMessage(string type)
171 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, LAMBDA(return it));
175 entity GetPlayerSound(string type)
177 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, LAMBDA(return it));
181 string allvoicesamples;
182 STATIC_INIT(allvoicesamples)
184 FOREACH(PlayerSounds, it.instanceOfVoiceMessage, LAMBDA(
185 allvoicesamples = strcat(allvoicesamples, " ", it.m_playersoundstr)
187 allvoicesamples = strzone(substring(allvoicesamples, 1, -1));
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_WARNINGF("Player sound file not found: %s\n", f);
212 for (string s; (s = fgets(fh)); )
214 int n = tokenize_console(s);
217 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", 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, LAMBDA(
237 .string fld = it.m_playersoundfld;
240 strunzone(this.(fld));
241 this.(fld) = string_null;
246 bool LoadPlayerSounds(entity this, string f, bool strict)
248 int fh = fopen(f, FILE_READ);
251 if (strict) LOG_WARNINGF("Player sound file not found: %s\n", f);
254 for (string s; (s = fgets(fh)); )
256 int n = tokenize_console(s);
259 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
262 string key = argv(0);
263 var.string field = GetPlayerSoundSampleField(key);
264 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
265 if (GetPlayerSoundSampleField_notFound)
267 LOG_TRACEF("Invalid sound info field: %s\n", key);
270 string file = argv(1);
271 string variants = argv(2);
272 if (this.(field)) strunzone(this.(field));
273 this.(field) = strzone(strcat(file, " ", variants));
279 .string model_for_playersound;
280 .int skin_for_playersound;
282 bool autocvar_g_debug_defaultsounds;
284 void UpdatePlayerSounds(entity this)
286 if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
287 if (this.model_for_playersound) strunzone(this.model_for_playersound);
288 this.model_for_playersound = strzone(this.model);
289 this.skin_for_playersound = this.skin;
290 ClearPlayerSounds(this);
291 LoadPlayerSounds(this, "sound/player/default.sounds", true);
292 if (this.model == "null" || autocvar_g_debug_defaultsounds) return;
293 if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
294 LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
301 void _GlobalSound(entity gs, entity ps, string sample, int chan, int voicetype, bool fake)
304 if (gs == NULL && ps == NULL && sample == "") return;
306 if (sample != "") sample = GlobalSound_sample(sample, r);
309 case VOICETYPE_LASTATTACKER_ONLY:
310 case VOICETYPE_LASTATTACKER:
314 if (!this.pusher) break;
315 msg_entity = this.pusher;
316 if (IS_REAL_CLIENT(msg_entity))
318 float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
319 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten);
320 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten);
321 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten);
324 if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
326 if (IS_REAL_CLIENT(msg_entity))
328 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
329 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
330 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
334 case VOICETYPE_TEAMRADIO:
339 float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
340 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
341 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
342 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
346 if (fake) { msg_entity = this; X(); }
349 FOR_EACH_REALCLIENT(msg_entity)
351 if (!teamplay || msg_entity.team == this.team) X();
357 case VOICETYPE_AUTOTAUNT:
358 case VOICETYPE_TAUNT:
360 if (voicetype == VOICETYPE_AUTOTAUNT) if (!sv_autotaunt) { break; }else {}
361 else if (IS_PLAYER(this) && this.deadflag == DEAD_NO) animdecide_setaction(this, ANIMACTION_TAUNT,
363 if (!sv_taunt) break;
364 if (autocvar_sv_gentle) break;
366 if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
370 if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < msg_entity.cvar_cl_autotaunt) \
372 float atten = (msg_entity.cvar_cl_voice_directional >= 1) \
373 ? bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, \
376 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
377 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
378 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
389 FOR_EACH_REALCLIENT(msg_entity)
397 case VOICETYPE_PLAYERSOUND:
402 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
403 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
404 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NORM);
408 if (gs) globalsound(MSG_ALL, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
409 else if (ps) playersound(MSG_ALL, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
410 else _sound(this, chan, sample, VOL_BASE, ATTEN_NORM);
416 backtrace("Invalid voice type!");