]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/globalsound.qc
Merge branch 'master' into terencehill/glowmod_color_fix
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / effects / qc / globalsound.qc
1 #include "globalsound.qh"
2
3 #include <common/ent_cs.qh>
4
5         #include <common/animdecide.qh>
6
7         REGISTER_NET_TEMP(globalsound)
8         REGISTER_NET_TEMP(playersound)
9
10         #ifdef SVQC
11                 /**
12                  * @param from the source entity, its position is sent
13                  * @param gs the global sound def
14                  * @param r a random number in 0..1
15                  */
16                 void globalsound(int channel, entity from, entity gs, float r, int chan, float _vol, float _atten, float _pitch)
17                 {
18                         //assert(IS_PLAYER(from), eprint(from));
19                         if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
20                         if (!autocvar_g_debug_globalsounds) {
21                                 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
22                                 switch (channel) {
23                                         case MSG_ONE:
24                                                 soundto(channel, from, chan, sample, _vol, _atten, _pitch);
25                                                 break;
26                                         case MSG_ALL:
27                                                 if(sound_allowed(MSG_BROADCAST, from))
28                                                         sound7(from, chan, sample, _vol, _atten, _pitch, 0);
29                                                 break;
30                                 }
31                                 return;
32                         }
33                         // FIXME: pitch not implemented
34                         WriteHeader(channel, globalsound);
35                         WriteByte(channel, gs.m_id);
36                         WriteByte(channel, r * 255);
37                         WriteByte(channel, etof(from));
38                         WriteByte(channel, chan);
39                         WriteByte(channel, floor(_vol * 255));
40                         WriteByte(channel, floor(_atten * 64));
41                         entcs_force_origin(from);
42                         vector o = from.origin + 0.5 * (from.mins + from.maxs);
43                         WriteVector(channel, o);
44                 }
45
46                 /**
47                 * @param from the source entity, its position is sent
48                 * @param ps the player sound def
49                 * @param r a random number in 0..1
50                 */
51                 void playersound(int channel, entity from, entity ps, float r, int chan, float _vol, float _atten, float _pitch)
52                 {
53                         //assert(IS_PLAYER(from), eprint(from));
54                         if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
55                         if (!autocvar_g_debug_globalsounds) {
56                                 //UpdatePlayerSounds(from);
57                                 string s = from.(ps.m_playersoundfld);
58                                 string sample = GlobalSound_sample(s, r);
59                                 switch (channel) {
60                                         case MSG_ONE:
61                                                 soundto(channel, from, chan, sample, _vol, _atten, _pitch);
62                                                 break;
63                                         case MSG_ALL:
64                                                 if(sound_allowed(MSG_BROADCAST, from))
65                                                         sound7(from, chan, sample, _vol, _atten, _pitch, 0);
66                                                 break;
67                                 }
68                                 return;
69                         }
70                         // FIXME: pitch not implemented
71                         WriteHeader(channel, playersound);
72                         WriteByte(channel, ps.m_id);
73                         WriteByte(channel, r * 255);
74                         WriteByte(channel, etof(from));
75                         WriteByte(channel, chan);
76                         WriteByte(channel, floor(_vol * 255));
77                         WriteByte(channel, floor(_atten * 64));
78                         entcs_force_origin(from);
79                         vector o = from.origin + 0.5 * (from.mins + from.maxs);
80                         WriteVector(channel, o);
81                 }
82         #endif
83
84         #ifdef CSQC
85
86                 NET_HANDLE(globalsound, bool isnew)
87                 {
88                         entity gs = REGISTRY_GET(GlobalSounds, ReadByte());
89                         float r = ReadByte() / 255;
90                         string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
91                         int who = ReadByte();
92                         entity e = entcs_receiver(who - 1);
93                         int chan = ReadSByte();
94                         float vol = ReadByte() / 255;
95                         float atten = ReadByte() / 64;
96                         vector o = ReadVector();
97                         // TODO: is this really what we want to be doing? Footsteps that follow the player at head height?
98                         if (who == player_currententnum) e = findfloat(NULL, entnum, who);  // play at camera position for full volume
99                         else if (e) e.origin = o;
100                         if (e)
101                         {
102                                 sound7(e, chan, sample, vol, atten, 0, 0);
103                         }
104                         else
105                         {
106                                 // Can this happen?
107                                 LOG_WARNF("Missing entcs data for player %d", who);
108                                 sound8(e, o, chan, sample, vol, atten, 0, 0);
109                         }
110                         return true;
111                 }
112
113                 NET_HANDLE(playersound, bool isnew)
114                 {
115                         entity ps = REGISTRY_GET(PlayerSounds, ReadByte());
116                         float r = ReadByte() / 255;
117                         int who = ReadByte();
118                         entity e = entcs_receiver(who - 1);
119                         UpdatePlayerSounds(e);
120                         string s = e.(ps.m_playersoundfld);
121                         string sample = GlobalSound_sample(s, r);
122                         int chan = ReadSByte();
123                         float vol = ReadByte() / 255;
124                         float atten = ReadByte() / 64;
125                         vector o = ReadVector();
126                         if (who == player_currententnum) e = findfloat(NULL, entnum, who);  // play at camera position for full volume
127                         else if (e) e.origin = o;
128                         if (e)
129                         {
130                                 // TODO: for non-visible players, origin should probably continue to be updated as long as the sound is playing
131                                 sound7(e, chan, sample, vol, atten, 0, 0);
132                         }
133                         else
134                         {
135                                 // Can this happen?
136                                 LOG_WARNF("Missing entcs data for player %d", who);
137                                 sound8(e, o, chan, sample, vol, atten, 0, 0);
138                         }
139                         return true;
140                 }
141
142         #endif
143
144         string GlobalSound_sample(string pair, float r)
145         {
146                 int n;
147                 {
148                         string s = cdr(pair);
149                         if (s) n = stof(s);
150                         else n = 0;
151                 }
152                 string sample = car(pair);
153                 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1));  // randomization
154                 else sample = sprintf("%s.wav", sample);
155                 return sample;
156         }
157
158         float GlobalSound_pitch(float _pitch)
159         {
160                 // customizable gradient function that crosses (0,a), (c,1) and asymptotically approaches b
161                 float a = 1.5; // max pitch
162                 float b = 0.75; // min pitch
163                 float c = 100; // standard pitch (scale * 100)
164                 float d = _pitch;
165                 float pitch_shift = (b*d*(a-1) + a*c*(1-b)) / (d*(a-1) + c*(1-b));
166
167                 return pitch_shift * 100;
168         }
169
170         void PrecacheGlobalSound(string sample)
171         {
172                 int n;
173                 {
174                         string s = cdr(sample);
175                         if (s) n = stof(s);
176                         else n = 0;
177                 }
178                 sample = car(sample);
179                 if (n > 0)
180                 {
181                         for (int i = 1; i <= n; ++i)
182                                 precache_sound(sprintf("%s%d.wav", sample, i));
183                 }
184                 else
185                 {
186                         precache_sound(sprintf("%s.wav", sample));
187                 }
188         }
189
190         entity GetVoiceMessage(string type)
191         {
192                 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, return it);
193                 return NULL;
194         }
195
196         entity GetPlayerSound(string type)
197         {
198                 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, return it);
199                 return NULL;
200         }
201
202         .string _GetPlayerSoundSampleField(string type, bool voice)
203         {
204                 GetPlayerSoundSampleField_notFound = false;
205                 entity e = voice ? GetVoiceMessage(type) : GetPlayerSound(type);
206                 if (e) return e.m_playersoundfld;
207                 GetPlayerSoundSampleField_notFound = true;
208                 return playersound_taunt.m_playersoundfld;
209         }
210
211         .string GetVoiceMessageSampleField(string type)
212         {
213                 return _GetPlayerSoundSampleField(type, true);
214         }
215
216         void PrecachePlayerSounds(string f)
217         {
218                 int fh = fopen(f, FILE_READ);
219                 if (fh < 0)
220                 {
221                         LOG_WARNF("Player sound file not found: %s", f);
222                         return;
223                 }
224                 for (string s; (s = fgets(fh)); )
225                 {
226                         int n = tokenize_console(s);
227                         if (n != 3)
228                         {
229                                 if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
230                                 continue;
231                         }
232                         string file = argv(1);
233                         string variants = argv(2);
234                         PrecacheGlobalSound(strcat(file, " ", variants));
235                 }
236                 fclose(fh);
237         }
238
239         //#ifdef CSQC
240
241                 .string GetPlayerSoundSampleField(string type)
242                 {
243                         return _GetPlayerSoundSampleField(type, false);
244                 }
245
246                 void ClearPlayerSounds(entity this)
247                 {
248                         FOREACH(PlayerSounds, true, {
249                                 .string fld = it.m_playersoundfld;
250                                 if (this.(fld))
251                                 {
252                                         strfree(this.(fld));
253                                 }
254                         });
255                 }
256
257                 bool LoadPlayerSounds(entity this, string f, bool strict)
258                 {
259                         int fh = fopen(f, FILE_READ);
260                         if (fh < 0)
261                         {
262                                 if (strict) LOG_WARNF("Player sound file not found: %s", f);
263                                 return false;
264                         }
265                         for (string s; (s = fgets(fh)); )
266                         {
267                                 int n = tokenize_console(s);
268                                 if (n != 3)
269                                 {
270                                         if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
271                                         continue;
272                                 }
273                                 string key = argv(0);
274                                 var.string field = GetPlayerSoundSampleField(key);
275                                 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
276                                 if (GetPlayerSoundSampleField_notFound)
277                                 {
278                                         LOG_TRACEF("Invalid sound info field in player sound file '%s': %s", f, key);
279                                         continue;
280                                 }
281                                 string file = argv(1);
282                                 string variants = argv(2);
283                                 strcpy(this.(field), strcat(file, " ", variants));
284                         }
285                         fclose(fh);
286                         return true;
287                 }
288
289                 .string model_for_playersound;
290                 .int skin_for_playersound;
291
292                 bool autocvar_g_debug_defaultsounds;
293
294                 void UpdatePlayerSounds(entity this)
295                 {
296                         if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
297                         strcpy(this.model_for_playersound, this.model);
298                         this.skin_for_playersound = this.skin;
299                         ClearPlayerSounds(this);
300                         LoadPlayerSounds(this, "sound/player/default.sounds", true);
301                         if (this.model == "null"
302                         #ifdef SVQC
303                                 && autocvar_g_debug_globalsounds
304                         #endif
305                         ) return;
306                         if (autocvar_g_debug_defaultsounds) return;
307                         if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
308                         LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
309                 }
310
311         //#endif
312
313         #ifdef SVQC
314
315                 void _GlobalSound(entity this, entity gs, entity ps, string sample, int chan, float vol, int voicetype, bool fake)
316                 {
317                         if (gs == NULL && ps == NULL && sample == "") return;
318                         if(this.classname == "body") return;
319                         float r = random();
320                         float myscale = ((this.scale) ? this.scale : 1); // safety net
321                         float thepitch = ((myscale == 1) ? 0 : GlobalSound_pitch(myscale * 100));
322                         if (sample != "") sample = GlobalSound_sample(sample, r);
323                         switch (voicetype)
324                         {
325                                 case VOICETYPE_LASTATTACKER_ONLY:
326                                 case VOICETYPE_LASTATTACKER:
327                                 {
328                                         if (!fake)
329                                         {
330                                                 if (!this.pusher) break;
331                                                 msg_entity = this.pusher;
332                                                 if (IS_REAL_CLIENT(msg_entity))
333                                                 {
334                                                         float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
335                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten, thepitch);
336                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten, thepitch);
337                                                         else soundto(MSG_ONE, this, chan, sample, vol, atten, thepitch);
338                                                 }
339                                         }
340                                         if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
341                                         msg_entity = this;
342                                         if (IS_REAL_CLIENT(msg_entity))
343                                         {
344                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE, thepitch);
345                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE, thepitch);
346                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE, thepitch);
347                                         }
348                                         break;
349                                 }
350                                 case VOICETYPE_TEAMRADIO:
351                                 {
352                                         #define X() \
353                                                 MACRO_BEGIN \
354                                                         float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
355                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten, thepitch); \
356                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten, thepitch); \
357                                                         else soundto(MSG_ONE, this, chan, sample, vol, atten, thepitch); \
358                                                 MACRO_END
359
360                                         if (fake) { msg_entity = this; X(); }
361                                         else
362                                         {
363                                                 FOREACH_CLIENT(IS_REAL_CLIENT(it) && SAME_TEAM(it, this), {
364                                                         msg_entity = it;
365                                                         X();
366                                                 });
367                                         }
368                                         #undef X
369                                         break;
370                                 }
371                                 case VOICETYPE_AUTOTAUNT:
372                                 case VOICETYPE_TAUNT:
373                                 {
374                                         if (voicetype == VOICETYPE_AUTOTAUNT)
375                                         {
376                                                 if (!autocvar_sv_autotaunt) break;
377                                         }
378                                         else if (IS_PLAYER(this) && !IS_DEAD(this))
379                                                 animdecide_setaction(this, ANIMACTION_TAUNT, true);
380
381                                         if (!autocvar_sv_taunt) break;
382                                         if (autocvar_sv_gentle) break;
383                                         float tauntrand = 0;
384                                         if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
385
386                                         #define X() \
387                                                 MACRO_BEGIN \
388                                                         if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < CS(msg_entity).cvar_cl_autotaunt) \
389                                                         { \
390                                                                 float atten = (CS(msg_entity).cvar_cl_voice_directional >= 1) \
391                                                                         ? bound(ATTEN_MIN, CS(msg_entity).cvar_cl_voice_directional_taunt_attenuation, \
392                                                                         ATTEN_MAX) \
393                                                                         : ATTEN_NONE; \
394                                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten, thepitch); \
395                                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten, thepitch); \
396                                                                 else soundto(MSG_ONE, this, chan, sample, vol, atten, thepitch); \
397                                                         } \
398                                                 MACRO_END
399                                         if (fake)
400                                         {
401                                                 msg_entity = this;
402                                                 X();
403                                         }
404                                         else
405                                         {
406                                                 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
407                                                         msg_entity = it;
408                                                         X();
409                                                 });
410                                         }
411                                         #undef X
412                                         break;
413                                 }
414                                 case VOICETYPE_PLAYERSOUND:
415                                 {
416                                         msg_entity = this;
417                                         if (fake)
418                                         {
419                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, ATTEN_NORM, thepitch);
420                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, ATTEN_NORM, thepitch);
421                                                 else soundto(MSG_ONE, this, chan, sample, vol, ATTEN_NORM, thepitch);
422                                         }
423                                         else
424                                         {
425                                                 if (gs) globalsound(MSG_ALL, this, gs, r, chan, vol, ATTEN_NORM, thepitch);
426                                                 else if (ps) playersound(MSG_ALL, this, ps, r, chan, vol, ATTEN_NORM, thepitch);
427                                                 else if (sound_allowed(MSG_BROADCAST, this)) sound7(this, chan, sample, vol, ATTEN_NORM, thepitch, 0);
428                                         }
429                                         break;
430                                 }
431                                 default:
432                                 {
433                                         backtrace("Invalid voice type!");
434                                         break;
435                                 }
436                         }
437                 }
438
439         #endif