8ce2b6ed3a4a8b7ea0fd09ddf75bf9b241b03847
[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         #ifdef SVQC
8                 #include <server/player.qh>
9         #endif
10
11         REGISTER_NET_TEMP(globalsound)
12         REGISTER_NET_TEMP(playersound)
13
14         #ifdef SVQC
15                 /**
16                  * @param from the source entity, its position is sent
17                  * @param gs the global sound def
18                  * @param r a random number in 0..1
19                  */
20                 void globalsound(int channel, entity from, entity gs, float r, int chan, float _vol, float _atten)
21                 {
22                         //assert(IS_PLAYER(from), eprint(from));
23                         if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
24                         if (!autocvar_g_debug_globalsounds) {
25                                 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
26                                 switch (channel) {
27                                         case MSG_ONE:
28                                                 soundto(channel, from, chan, sample, _vol, _atten);
29                                                 break;
30                                         case MSG_ALL:
31                                                 _sound(from, chan, sample, _vol, _atten);
32                                                 break;
33                                 }
34                                 return;
35                         }
36                         WriteHeader(channel, globalsound);
37                         WriteByte(channel, gs.m_id);
38                         WriteByte(channel, r * 255);
39                         WriteByte(channel, etof(from));
40                         WriteByte(channel, chan);
41                         WriteByte(channel, floor(_vol * 255));
42                         WriteByte(channel, floor(_atten * 64));
43                         entcs_force_origin(from);
44                         vector o = from.origin + 0.5 * (from.mins + from.maxs);
45                         WriteVector(channel, o);
46                 }
47
48                 /**
49                 * @param from the source entity, its position is sent
50                 * @param ps the player sound def
51                 * @param r a random number in 0..1
52                 */
53                 void playersound(int channel, entity from, entity ps, float r, int chan, float _vol, float _atten)
54                 {
55                         //assert(IS_PLAYER(from), eprint(from));
56                         if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
57                         if (!autocvar_g_debug_globalsounds) {
58                                 //UpdatePlayerSounds(from);
59                                 string s = from.(ps.m_playersoundfld);
60                                 string sample = GlobalSound_sample(s, r);
61                                 switch (channel) {
62                                         case MSG_ONE:
63                                                 soundto(channel, from, chan, sample, _vol, _atten);
64                                                 break;
65                                         case MSG_ALL:
66                                                 _sound(from, chan, sample, _vol, _atten);
67                                                 break;
68                                 }
69                                 return;
70                         }
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         void PrecacheGlobalSound(string sample)
159         {
160                 int n;
161                 {
162                         string s = cdr(sample);
163                         if (s) n = stof(s);
164                         else n = 0;
165                 }
166                 sample = car(sample);
167                 if (n > 0)
168                 {
169                         for (int i = 1; i <= n; ++i)
170                                 precache_sound(sprintf("%s%d.wav", sample, i));
171                 }
172                 else
173                 {
174                         precache_sound(sprintf("%s.wav", sample));
175                 }
176         }
177
178         entity GetVoiceMessage(string type)
179         {
180                 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, return it);
181                 return NULL;
182         }
183
184         entity GetPlayerSound(string type)
185         {
186                 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, return it);
187                 return NULL;
188         }
189
190         .string _GetPlayerSoundSampleField(string type, bool voice)
191         {
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;
197         }
198
199         .string GetVoiceMessageSampleField(string type)
200         {
201                 return _GetPlayerSoundSampleField(type, true);
202         }
203
204         void PrecachePlayerSounds(string f)
205         {
206                 int fh = fopen(f, FILE_READ);
207                 if (fh < 0)
208                 {
209                         LOG_WARNF("Player sound file not found: %s", f);
210                         return;
211                 }
212                 for (string s; (s = fgets(fh)); )
213                 {
214                         int n = tokenize_console(s);
215                         if (n != 3)
216                         {
217                                 if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
218                                 continue;
219                         }
220                         string file = argv(1);
221                         string variants = argv(2);
222                         PrecacheGlobalSound(strcat(file, " ", variants));
223                 }
224                 fclose(fh);
225         }
226
227         //#ifdef CSQC
228
229                 .string GetPlayerSoundSampleField(string type)
230                 {
231                         return _GetPlayerSoundSampleField(type, false);
232                 }
233
234                 void ClearPlayerSounds(entity this)
235                 {
236                         FOREACH(PlayerSounds, true, {
237                                 .string fld = it.m_playersoundfld;
238                                 if (this.(fld))
239                                 {
240                                         strfree(this.(fld));
241                                 }
242                         });
243                 }
244
245                 bool LoadPlayerSounds(entity this, string f, bool strict)
246                 {
247                         int fh = fopen(f, FILE_READ);
248                         if (fh < 0)
249                         {
250                                 if (strict) LOG_WARNF("Player sound file not found: %s", f);
251                                 return false;
252                         }
253                         for (string s; (s = fgets(fh)); )
254                         {
255                                 int n = tokenize_console(s);
256                                 if (n != 3)
257                                 {
258                                         if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
259                                         continue;
260                                 }
261                                 string key = argv(0);
262                                 var.string field = GetPlayerSoundSampleField(key);
263                                 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
264                                 if (GetPlayerSoundSampleField_notFound)
265                                 {
266                                         LOG_TRACEF("Invalid sound info field in player sound file '%s': %s", f, key);
267                                         continue;
268                                 }
269                                 string file = argv(1);
270                                 string variants = argv(2);
271                                 strcpy(this.(field), strcat(file, " ", variants));
272                         }
273                         fclose(fh);
274                         return true;
275                 }
276
277                 .string model_for_playersound;
278                 .int skin_for_playersound;
279
280                 bool autocvar_g_debug_defaultsounds;
281
282                 void UpdatePlayerSounds(entity this)
283                 {
284                         if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
285                         strcpy(this.model_for_playersound, 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"
290                         #ifdef SVQC
291                                 && autocvar_g_debug_globalsounds
292                         #endif
293                         ) return;
294                         if (autocvar_g_debug_defaultsounds) return;
295                         if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
296                         LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
297                 }
298
299         //#endif
300
301         #ifdef SVQC
302
303                 void _GlobalSound(entity this, entity gs, entity ps, string sample, int chan, float vol, int voicetype, bool fake)
304                 {
305                         if (gs == NULL && ps == NULL && sample == "") return;
306                         if(this.classname == "body") return;
307                         float r = random();
308                         if (sample != "") sample = GlobalSound_sample(sample, r);
309                         switch (voicetype)
310                         {
311                                 case VOICETYPE_LASTATTACKER_ONLY:
312                                 case VOICETYPE_LASTATTACKER:
313                                 {
314                                         if (!fake)
315                                         {
316                                                 if (!this.pusher) break;
317                                                 msg_entity = this.pusher;
318                                                 if (IS_REAL_CLIENT(msg_entity))
319                                                 {
320                                                         float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
321                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten);
322                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten);
323                                                         else soundto(MSG_ONE, this, chan, sample, vol, atten);
324                                                 }
325                                         }
326                                         if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
327                                         msg_entity = this;
328                                         if (IS_REAL_CLIENT(msg_entity))
329                                         {
330                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
331                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
332                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
333                                         }
334                                         break;
335                                 }
336                                 case VOICETYPE_TEAMRADIO:
337                                 {
338                                         #define X() \
339                                                 MACRO_BEGIN \
340                                                         float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
341                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten); \
342                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten); \
343                                                         else soundto(MSG_ONE, this, chan, sample, vol, atten); \
344                                                 MACRO_END
345
346                                         if (fake) { msg_entity = this; X(); }
347                                         else
348                                         {
349                                                 FOREACH_CLIENT(IS_REAL_CLIENT(it) && SAME_TEAM(it, this), {
350                                                         msg_entity = it;
351                                                         X();
352                                                 });
353                                         }
354                                         #undef X
355                                         break;
356                                 }
357                                 case VOICETYPE_AUTOTAUNT:
358                                 case VOICETYPE_TAUNT:
359                                 {
360                                         if (voicetype == VOICETYPE_AUTOTAUNT)
361                                         {
362                                                 if (!sv_autotaunt) break;
363                                         }
364                                         else if (IS_PLAYER(this) && !IS_DEAD(this))
365                                                 animdecide_setaction(this, ANIMACTION_TAUNT, true);
366
367                                         if (!sv_taunt) break;
368                                         if (autocvar_sv_gentle) break;
369                                         float tauntrand = 0;
370                                         if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
371
372                                         #define X() \
373                                                 MACRO_BEGIN \
374                                                         if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < CS(msg_entity).cvar_cl_autotaunt) \
375                                                         { \
376                                                                 float atten = (CS(msg_entity).cvar_cl_voice_directional >= 1) \
377                                                                         ? bound(ATTEN_MIN, CS(msg_entity).cvar_cl_voice_directional_taunt_attenuation, \
378                                                                         ATTEN_MAX) \
379                                                                         : ATTEN_NONE; \
380                                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten); \
381                                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten); \
382                                                                 else soundto(MSG_ONE, this, chan, sample, vol, atten); \
383                                                         } \
384                                                 MACRO_END
385                                         if (fake)
386                                         {
387                                                 msg_entity = this;
388                                                 X();
389                                         }
390                                         else
391                                         {
392                                                 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
393                                                         msg_entity = it;
394                                                         X();
395                                                 });
396                                         }
397                                         #undef X
398                                         break;
399                                 }
400                                 case VOICETYPE_PLAYERSOUND:
401                                 {
402                                         msg_entity = this;
403                                         if (fake)
404                                         {
405                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, ATTEN_NORM);
406                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, ATTEN_NORM);
407                                                 else soundto(MSG_ONE, this, chan, sample, vol, ATTEN_NORM);
408                                         }
409                                         else
410                                         {
411                                                 if (gs) globalsound(MSG_ALL, this, gs, r, chan, vol, ATTEN_NORM);
412                                                 else if (ps) playersound(MSG_ALL, this, ps, r, chan, vol, ATTEN_NORM);
413                                                 else _sound(this, chan, sample, vol, ATTEN_NORM);
414                                         }
415                                         break;
416                                 }
417                                 default:
418                                 {
419                                         backtrace("Invalid voice type!");
420                                         break;
421                                 }
422                         }
423                 }
424
425         #endif