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