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