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 vector o = from.origin + 0.5 * (from.mins + from.maxs);
32 WriteCoord(channel, o.x);
33 WriteCoord(channel, o.y);
34 WriteCoord(channel, o.z);
38 * @param from the source entity, its position is sent
39 * @param ps the player sound def
40 * @param r a random number in 0..1
42 void playersound(int channel, entity from, entity ps, float r, int chan, float vol, float atten)
44 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
45 WriteHeader(channel, playersound);
46 WriteByte(channel, ps.m_id);
47 WriteByte(channel, r * 255);
48 WriteByte(channel, etof(from));
49 WriteByte(channel, fabs(chan));
50 WriteByte(channel, floor(vol * 255));
51 WriteByte(channel, floor(atten * 64));
52 entcs_force_origin(from);
53 vector o = from.origin + 0.5 * (from.mins + from.maxs);
54 WriteCoord(channel, o.x);
55 WriteCoord(channel, o.y);
56 WriteCoord(channel, o.z);
60 string GlobalSound_sample(string pair, float r);
64 NET_HANDLE(globalsound, bool isnew)
66 entity gs = GlobalSounds_from(ReadByte());
67 float r = ReadByte() / 255;
68 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
70 entity e = entcs_receiver[who - 1];
71 int chan = ReadByte();
72 float vol = ReadByte() / 255;
73 float atten = ReadByte() / 64;
78 // TODO: is this really what we want to be doing? Footsteps that follow the player at head height?
79 if (who == player_currententnum) e = findfloat(world, entnum, who); // play at camera position for full volume
80 else if (e) e.origin = o;
83 sound7(e, chan, sample, vol, atten, 0, 0);
87 entity e = new(globalsound);
89 sound8(e, o, chan, sample, vol, atten, 0, 0);
90 remove(e); // debug with: e.think = SUB_Remove; e.nextthink = time + 1;
95 NET_HANDLE(playersound, bool isnew)
97 entity ps = PlayerSounds_from(ReadByte());
98 float r = ReadByte() / 255;
100 entity e = entcs_receiver[who - 1];
101 UpdatePlayerSounds(e);
102 string s = e.(ps.m_playersoundfld);
103 string sample = GlobalSound_sample(s, r);
104 int chan = ReadByte();
105 float vol = ReadByte() / 255;
106 float atten = ReadByte() / 64;
111 if (who == player_currententnum) e = findfloat(world, entnum, who); // play at camera position for full volume
112 else if (e) e.origin = o;
115 // TODO: for non-visible players, origin should probably continue to be updated as long as the sound is playing
116 sound7(e, chan, sample, vol, atten, 0, 0);
120 LOG_WARNINGF("Missing entcs data for player %d\n", who);
122 entity e = new(playersound);
124 sound8(e, o, chan, sample, vol, atten, 0, 0);
125 remove(e); // debug with: e.think = SUB_Remove; e.nextthink = time + 1;
132 string GlobalSound_sample(string pair, float r)
136 string s = cdr(pair);
140 string sample = car(pair);
141 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1)); // randomization
142 else sample = sprintf("%s.wav", sample);
146 void PrecacheGlobalSound(string sample)
150 string s = cdr(sample);
154 sample = car(sample);
157 for (int i = 1; i <= n; ++i)
158 precache_sound(sprintf("%s%d.wav", sample, i));
162 precache_sound(sprintf("%s.wav", sample));
166 entity GetVoiceMessage(string type)
168 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, LAMBDA(return it));
172 entity GetPlayerSound(string type)
174 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, LAMBDA(return it));
178 string allvoicesamples;
179 STATIC_INIT(allvoicesamples)
181 FOREACH(PlayerSounds, it.instanceOfVoiceMessage, LAMBDA(
182 allvoicesamples = strcat(allvoicesamples, " ", it.m_playersoundstr)
184 allvoicesamples = strzone(substring(allvoicesamples, 1, -1));
187 .string _GetPlayerSoundSampleField(string type, bool voice)
189 GetPlayerSoundSampleField_notFound = false;
190 entity e = voice ? GetVoiceMessage(type) : GetPlayerSound(type);
191 if (e) return e.m_playersoundfld;
192 GetPlayerSoundSampleField_notFound = true;
193 return playersound_taunt.m_playersoundfld;
196 .string GetVoiceMessageSampleField(string type)
198 return _GetPlayerSoundSampleField(type, true);
201 void PrecachePlayerSounds(string f)
203 int fh = fopen(f, FILE_READ);
206 LOG_WARNINGF("Player sound file not found: %s\n", f);
209 for (string s; (s = fgets(fh)); )
211 int n = tokenize_console(s);
214 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
217 string file = argv(1);
218 string variants = argv(2);
219 PrecacheGlobalSound(strcat(file, " ", variants));
226 .string GetPlayerSoundSampleField(string type)
228 return _GetPlayerSoundSampleField(type, false);
231 void ClearPlayerSounds(entity this)
233 FOREACH(PlayerSounds, true, LAMBDA(
234 .string fld = it.m_playersoundfld;
237 strunzone(this.(fld));
238 this.(fld) = string_null;
243 bool LoadPlayerSounds(entity this, string f, bool strict)
245 int fh = fopen(f, FILE_READ);
248 if (strict) LOG_WARNINGF("Player sound file not found: %s\n", f);
251 for (string s; (s = fgets(fh)); )
253 int n = tokenize_console(s);
256 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
259 string key = argv(0);
260 var.string field = GetPlayerSoundSampleField(key);
261 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
262 if (GetPlayerSoundSampleField_notFound)
264 LOG_TRACEF("Invalid sound info field: %s\n", key);
267 string file = argv(1);
268 string variants = argv(2);
269 if (this.(field)) strunzone(this.(field));
270 this.(field) = strzone(strcat(file, " ", variants));
276 .string model_for_playersound;
277 .int skin_for_playersound;
279 bool autocvar_g_debug_defaultsounds;
281 void UpdatePlayerSounds(entity this)
283 if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
284 if (this.model_for_playersound) strunzone(this.model_for_playersound);
285 this.model_for_playersound = strzone(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" || autocvar_g_debug_defaultsounds) return;
290 if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
291 LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
298 void _GlobalSound(entity gs, entity ps, string sample, int chan, int voicetype, bool fake)
301 if (gs == NULL && ps == NULL && sample == "") return;
303 if (sample != "") sample = GlobalSound_sample(sample, r);
306 case VOICETYPE_LASTATTACKER_ONLY:
307 case VOICETYPE_LASTATTACKER:
311 if (!this.pusher) break;
312 msg_entity = this.pusher;
313 if (IS_REAL_CLIENT(msg_entity))
315 float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
316 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten);
317 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten);
318 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten);
321 if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
323 if (IS_REAL_CLIENT(msg_entity))
325 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
326 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
327 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
331 case VOICETYPE_TEAMRADIO:
336 float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
337 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
338 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
339 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
343 if (fake) { msg_entity = this; X(); }
346 FOR_EACH_REALCLIENT(msg_entity)
348 if (!teamplay || msg_entity.team == this.team) X();
354 case VOICETYPE_AUTOTAUNT:
355 case VOICETYPE_TAUNT:
357 if (voicetype == VOICETYPE_AUTOTAUNT) if (!sv_autotaunt) { break; }else {}
358 else if (IS_PLAYER(this) && this.deadflag == DEAD_NO) animdecide_setaction(this, ANIMACTION_TAUNT,
360 if (!sv_taunt) break;
361 if (autocvar_sv_gentle) break;
363 if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
367 if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < msg_entity.cvar_cl_autotaunt) \
369 float atten = (msg_entity.cvar_cl_voice_directional >= 1) \
370 ? bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, \
373 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
374 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
375 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
386 FOR_EACH_REALCLIENT(msg_entity)
394 case VOICETYPE_PLAYERSOUND:
399 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
400 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
401 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NORM);
405 if (gs) globalsound(MSG_ALL, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
406 else if (ps) playersound(MSG_ALL, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
407 else _sound(this, chan, sample, VOL_BASE, ATTEN_NORM);
413 backtrace("Invalid voice type!");