1 #include "globalsound.qh"
3 #include "../../animdecide.qh"
6 #include "../../../server/cl_player.qh"
9 REGISTER_NET_TEMP(globalsound)
13 * @param from the source entity, its position is sent
14 * @param gs the global sound def
15 * @param r a random number in 0..1
17 void globalsound(int channel, entity from, entity gs, float r, int chan, float vol, float atten)
19 if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
20 WriteHeader(channel, globalsound);
21 WriteByte(channel, gs.m_id);
22 WriteByte(channel, r * 255);
23 WriteByte(channel, etof(from));
24 WriteByte(channel, fabs(chan));
25 WriteByte(channel, floor(vol * 255));
26 WriteByte(channel, floor(atten * 64));
27 vector o = from.origin + 0.5 * (from.mins + from.maxs);
28 WriteCoord(channel, o.x);
29 WriteCoord(channel, o.y);
30 WriteCoord(channel, o.z);
34 string GlobalSound_sample(string pair, float r);
38 NET_HANDLE(globalsound, bool isnew)
40 entity gs = GlobalSounds_from(ReadByte());
41 float r = ReadByte() / 255;
42 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
44 int chan = ReadByte();
45 float vol = ReadByte() / 255;
46 float atten = ReadByte() / 64;
51 if (who == player_localnum + 1)
53 // client knows better, play at current position to unlag
54 entity e = findfloat(world, entnum, who);
55 sound7(e, chan, sample, vol, atten, 0, 0);
59 entity e = new(globalsound);
61 sound8(e, o, chan, sample, vol, atten, 0, 0);
62 remove(e); // debug with: e.think = SUB_Remove; e.nextthink = time + 1;
69 string GlobalSound_sample(string pair, float r)
77 string sample = car(pair);
78 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1)); // randomization
79 else sample = sprintf("%s.wav", sample);
83 void PrecacheGlobalSound(string sample)
87 string s = cdr(sample);
94 for (int i = 1; i <= n; ++i)
95 precache_sound(sprintf("%s%d.wav", sample, i));
99 precache_sound(sprintf("%s.wav", sample));
105 int GetVoiceMessageVoiceType(string type)
107 if (type == "taunt") return VOICETYPE_TAUNT;
108 if (type == "teamshoot") return VOICETYPE_LASTATTACKER;
109 return VOICETYPE_TEAMRADIO;
112 .string GetVoiceMessageSampleField(string type)
114 GetPlayerSoundSampleField_notFound = false;
117 #define X(m) case #m: return playersound_##m;
121 GetPlayerSoundSampleField_notFound = true;
122 return playersound_taunt;
125 .string GetPlayerSoundSampleField(string type)
127 GetPlayerSoundSampleField_notFound = false;
130 #define X(m) case #m: return playersound_##m;
134 GetPlayerSoundSampleField_notFound = true;
135 return playersound_taunt;
138 string allvoicesamples;
140 void PrecachePlayerSounds(string f)
142 int fh = fopen(f, FILE_READ);
145 LOG_WARNINGF("Player sound file not found: %s\n", f);
148 for (string s; (s = fgets(fh)); )
150 int n = tokenize_console(s);
153 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
156 string file = argv(1);
157 string variants = argv(2);
158 PrecacheGlobalSound(strcat(file, " ", variants));
162 if (!allvoicesamples)
164 #define X(m) allvoicesamples = strcat(allvoicesamples, " ", #m);
167 allvoicesamples = strzone(substring(allvoicesamples, 1, -1));
171 void ClearPlayerSounds(entity this)
174 if (this.playersound_##m) \
176 strunzone(this.playersound_##m); \
177 this.playersound_##m = string_null; \
184 bool LoadPlayerSounds(string f, bool strict)
187 int fh = fopen(f, FILE_READ);
190 if (strict) LOG_WARNINGF("Player sound file not found: %s\n", f);
193 for (string s; (s = fgets(fh)); )
195 int n = tokenize_console(s);
198 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
201 string key = argv(0);
202 var.string field = GetPlayerSoundSampleField(key);
203 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
204 if (GetPlayerSoundSampleField_notFound)
206 LOG_TRACEF("Invalid sound info field: %s\n", key);
209 string file = argv(1);
210 string variants = argv(2);
211 if (self.(field)) strunzone(self.(field));
212 self.(field) = strzone(strcat(file, " ", variants));
218 .int modelindex_for_playersound;
219 .int skin_for_playersound;
221 void UpdatePlayerSounds(entity this)
223 if (this.modelindex == this.modelindex_for_playersound && this.skin == this.skin_for_playersound) return;
224 this.modelindex_for_playersound = this.modelindex;
225 this.skin_for_playersound = this.skin;
226 ClearPlayerSounds(this);
227 LoadPlayerSounds("sound/player/default.sounds", true);
228 if (autocvar_g_debug_defaultsounds) return;
229 if (!LoadPlayerSounds(get_model_datafilename(this.model, this.skin, "sounds"), false))
230 LoadPlayerSounds(get_model_datafilename(
236 void _GlobalSound(entity gs, string sample, int chan, int voicetype, bool fake)
239 if (gs == NULL && sample == "") return;
241 if (sample != "") sample = GlobalSound_sample(sample, r);
244 case VOICETYPE_LASTATTACKER_ONLY:
245 case VOICETYPE_LASTATTACKER:
249 if (!this.pusher) break;
250 msg_entity = this.pusher;
251 if (IS_REAL_CLIENT(msg_entity))
253 float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
254 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten);
255 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten);
258 if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
260 if (IS_REAL_CLIENT(msg_entity))
262 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
263 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
267 case VOICETYPE_TEAMRADIO:
272 float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
273 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
274 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
278 if (fake) { msg_entity = this; X(); }
281 FOR_EACH_REALCLIENT(msg_entity)
283 if (!teamplay || msg_entity.team == this.team) X();
289 case VOICETYPE_AUTOTAUNT:
290 case VOICETYPE_TAUNT:
292 if (voicetype == VOICETYPE_AUTOTAUNT) if (!sv_autotaunt) { break; }else {}
293 else if (IS_PLAYER(this) && this.deadflag == DEAD_NO) animdecide_setaction(this, ANIMACTION_TAUNT,
295 if (!sv_taunt) break;
296 if (autocvar_sv_gentle) break;
298 if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
302 if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < msg_entity.cvar_cl_autotaunt) \
304 float atten = (msg_entity.cvar_cl_voice_directional >= 1) \
305 ? bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, \
308 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
309 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
320 FOR_EACH_REALCLIENT(msg_entity)
328 case VOICETYPE_PLAYERSOUND:
333 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
334 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NORM);
338 if (gs) globalsound(MSG_ALL, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
339 else _sound(this, chan, sample, VOL_BASE, ATTEN_NORM);
345 backtrace("Invalid voice type!");
351 void PlayerSound(.string samplefield, int chan, float voicetype)
354 _GlobalSound(NULL, this.(samplefield), chan, voicetype, false);
357 void VoiceMessage(string type, string msg)
360 var.string sample = GetVoiceMessageSampleField(type);
361 if (GetPlayerSoundSampleField_notFound)
363 sprint(this, sprintf("Invalid voice. Use one of: %s\n", allvoicesamples));
366 int voicetype = GetVoiceMessageVoiceType(type);
367 bool ownteam = (voicetype == VOICETYPE_TEAMRADIO);
368 int flood = Say(this, ownteam, world, msg, true);
370 if (IS_SPEC(this) || IS_OBSERVER(this) || flood < 0) fake = true;
371 else if (flood > 0) fake = false;
373 _GlobalSound(NULL, this.(sample), CH_VOICE, voicetype, fake);