]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/globalsound.qc
Merge branch 'master' into terencehill/accuracy_shotgun
[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         string GlobalSound_sample(string pair, float r);
15
16         #ifdef SVQC
17                 /**
18                  * @param from the source entity, its position is sent
19                  * @param gs the global sound def
20                  * @param r a random number in 0..1
21                  */
22                 void globalsound(int channel, entity from, entity gs, float r, int chan, float _vol, float _atten)
23                 {
24                         //assert(IS_PLAYER(from), eprint(from));
25                         if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
26                         if (!autocvar_g_debug_globalsounds) {
27                                 string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
28                                 switch (channel) {
29                                         case MSG_ONE:
30                                                 soundto(channel, from, chan, sample, _vol, _atten);
31                                                 break;
32                                         case MSG_ALL:
33                                                 _sound(from, chan, sample, _vol, _atten);
34                                                 break;
35                                 }
36                                 return;
37                         }
38                         WriteHeader(channel, globalsound);
39                         WriteByte(channel, gs.m_id);
40                         WriteByte(channel, r * 255);
41                         WriteByte(channel, etof(from));
42                         WriteByte(channel, chan);
43                         WriteByte(channel, floor(_vol * 255));
44                         WriteByte(channel, floor(_atten * 64));
45                         entcs_force_origin(from);
46                         vector o = from.origin + 0.5 * (from.mins + from.maxs);
47                         WriteCoord(channel, o.x);
48                         WriteCoord(channel, o.y);
49                         WriteCoord(channel, o.z);
50                 }
51
52                 /**
53                 * @param from the source entity, its position is sent
54                 * @param ps the player sound def
55                 * @param r a random number in 0..1
56                 */
57                 void playersound(int channel, entity from, entity ps, float r, int chan, float _vol, float _atten)
58                 {
59                         //assert(IS_PLAYER(from), eprint(from));
60                         if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
61                         if (!autocvar_g_debug_globalsounds) {
62                                 //UpdatePlayerSounds(from);
63                                 string s = from.(ps.m_playersoundfld);
64                                 string sample = GlobalSound_sample(s, r);
65                                 switch (channel) {
66                                         case MSG_ONE:
67                                                 soundto(channel, from, chan, sample, _vol, _atten);
68                                                 break;
69                                         case MSG_ALL:
70                                                 _sound(from, chan, sample, _vol, _atten);
71                                                 break;
72                                 }
73                                 return;
74                         }
75                         WriteHeader(channel, playersound);
76                         WriteByte(channel, ps.m_id);
77                         WriteByte(channel, r * 255);
78                         WriteByte(channel, etof(from));
79                         WriteByte(channel, chan);
80                         WriteByte(channel, floor(_vol * 255));
81                         WriteByte(channel, floor(_atten * 64));
82                         entcs_force_origin(from);
83                         vector o = from.origin + 0.5 * (from.mins + from.maxs);
84                         WriteCoord(channel, o.x);
85                         WriteCoord(channel, o.y);
86                         WriteCoord(channel, o.z);
87                 }
88         #endif
89
90         #ifdef CSQC
91
92                 NET_HANDLE(globalsound, bool isnew)
93                 {
94                         entity gs = GlobalSounds_from(ReadByte());
95                         float r = ReadByte() / 255;
96                         string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
97                         int who = ReadByte();
98                         entity e = entcs_receiver(who - 1);
99                         int chan = ReadSByte();
100                         float vol = ReadByte() / 255;
101                         float atten = ReadByte() / 64;
102                         vector o;
103                         o.x = ReadCoord();
104                         o.y = ReadCoord();
105                         o.z = ReadCoord();
106                         // TODO: is this really what we want to be doing? Footsteps that follow the player at head height?
107                         if (who == player_currententnum) e = findfloat(NULL, entnum, who);  // play at camera position for full volume
108                         else if (e) e.origin = o;
109                         if (e)
110                         {
111                                 sound7(e, chan, sample, vol, atten, 0, 0);
112                         }
113                         else
114                         {
115                                 // Can this happen?
116                                 LOG_WARNF("Missing entcs data for player %d", who);
117                                 sound8(e, o, chan, sample, vol, atten, 0, 0);
118                         }
119                         return true;
120                 }
121
122                 NET_HANDLE(playersound, bool isnew)
123                 {
124                         entity ps = PlayerSounds_from(ReadByte());
125                         float r = ReadByte() / 255;
126                         int who = ReadByte();
127                         entity e = entcs_receiver(who - 1);
128                         UpdatePlayerSounds(e);
129                         string s = e.(ps.m_playersoundfld);
130                         string sample = GlobalSound_sample(s, r);
131                         int chan = ReadSByte();
132                         float vol = ReadByte() / 255;
133                         float atten = ReadByte() / 64;
134                         vector o;
135                         o.x = ReadCoord();
136                         o.y = ReadCoord();
137                         o.z = ReadCoord();
138                         if (who == player_currententnum) e = findfloat(NULL, entnum, who);  // play at camera position for full volume
139                         else if (e) e.origin = o;
140                         if (e)
141                         {
142                                 // TODO: for non-visible players, origin should probably continue to be updated as long as the sound is playing
143                                 sound7(e, chan, sample, vol, atten, 0, 0);
144                         }
145                         else
146                         {
147                                 // Can this happen?
148                                 LOG_WARNF("Missing entcs data for player %d", who);
149                                 sound8(e, o, chan, sample, vol, atten, 0, 0);
150                         }
151                         return true;
152                 }
153
154         #endif
155
156         string GlobalSound_sample(string pair, float r)
157         {
158                 int n;
159                 {
160                         string s = cdr(pair);
161                         if (s) n = stof(s);
162                         else n = 0;
163                 }
164                 string sample = car(pair);
165                 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1));  // randomization
166                 else sample = sprintf("%s.wav", sample);
167                 return sample;
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                                         strunzone(this.(fld));
253                                         this.(fld) = string_null;
254                                 }
255                         });
256                 }
257
258                 bool LoadPlayerSounds(entity this, string f, bool strict)
259                 {
260                         int fh = fopen(f, FILE_READ);
261                         if (fh < 0)
262                         {
263                                 if (strict) LOG_WARNF("Player sound file not found: %s", f);
264                                 return false;
265                         }
266                         for (string s; (s = fgets(fh)); )
267                         {
268                                 int n = tokenize_console(s);
269                                 if (n != 3)
270                                 {
271                                         if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
272                                         continue;
273                                 }
274                                 string key = argv(0);
275                                 var.string field = GetPlayerSoundSampleField(key);
276                                 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
277                                 if (GetPlayerSoundSampleField_notFound)
278                                 {
279                                         LOG_TRACEF("Invalid sound info field: %s", key);
280                                         continue;
281                                 }
282                                 string file = argv(1);
283                                 string variants = argv(2);
284                                 if (this.(field)) strunzone(this.(field));
285                                 this.(field) = strzone(strcat(file, " ", variants));
286                         }
287                         fclose(fh);
288                         return true;
289                 }
290
291                 .string model_for_playersound;
292                 .int skin_for_playersound;
293
294                 bool autocvar_g_debug_defaultsounds;
295
296                 void UpdatePlayerSounds(entity this)
297                 {
298                         if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
299                         if (this.model_for_playersound) strunzone(this.model_for_playersound);
300                         this.model_for_playersound = strzone(this.model);
301                         this.skin_for_playersound = this.skin;
302                         ClearPlayerSounds(this);
303                         LoadPlayerSounds(this, "sound/player/default.sounds", true);
304                         if (this.model == "null"
305                         #ifdef SVQC
306                             && autocvar_g_debug_globalsounds
307                         #endif
308                          ) return;
309                         if (autocvar_g_debug_defaultsounds) return;
310                         if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
311                         LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
312                 }
313
314         //#endif
315
316         #ifdef SVQC
317
318                 void _GlobalSound(entity this, entity gs, entity ps, string sample, int chan, float vol, int voicetype, bool fake)
319                 {
320                         if (gs == NULL && ps == NULL && sample == "") return;
321                         if(this.classname == "body") return;
322                         float r = random();
323                         if (sample != "") sample = GlobalSound_sample(sample, r);
324                         switch (voicetype)
325                         {
326                                 case VOICETYPE_LASTATTACKER_ONLY:
327                                 case VOICETYPE_LASTATTACKER:
328                                 {
329                                         if (!fake)
330                                         {
331                                                 if (!this.pusher) break;
332                                                 msg_entity = this.pusher;
333                                                 if (IS_REAL_CLIENT(msg_entity))
334                                                 {
335                                                         float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
336                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten);
337                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten);
338                                                         else soundto(MSG_ONE, this, chan, sample, vol, atten);
339                                                 }
340                                         }
341                                         if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
342                                         msg_entity = this;
343                                         if (IS_REAL_CLIENT(msg_entity))
344                                         {
345                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
346                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
347                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
348                                         }
349                                         break;
350                                 }
351                                 case VOICETYPE_TEAMRADIO:
352                                 {
353                                         #define X() \
354                                                 MACRO_BEGIN \
355                                                 { \
356                                                         float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
357                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten); \
358                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten); \
359                                                         else soundto(MSG_ONE, this, chan, sample, vol, atten); \
360                                                 } MACRO_END
361
362                                         if (fake) { msg_entity = this; X(); }
363                                         else
364                                         {
365                                                 FOREACH_CLIENT(IS_REAL_CLIENT(it) && SAME_TEAM(it, this), {
366                                                         msg_entity = it;
367                                                         X();
368                                                 });
369                                         }
370                 #undef X
371                                         break;
372                                 }
373                                 case VOICETYPE_AUTOTAUNT:
374                                 case VOICETYPE_TAUNT:
375                                 {
376                                         if (voicetype == VOICETYPE_AUTOTAUNT) if (!sv_autotaunt) { break; }else {}
377                                         else if (IS_PLAYER(this) && !IS_DEAD(this)) animdecide_setaction(this, ANIMACTION_TAUNT,
378                                                         true);
379                                         if (!sv_taunt) break;
380                                         if (autocvar_sv_gentle) break;
381                                         float tauntrand = 0;
382                                         if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
383                                         #define X() \
384                                                 MACRO_BEGIN \
385                                                 { \
386                                                         if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < CS(msg_entity).cvar_cl_autotaunt) \
387                                                         { \
388                                                                 float atten = (CS(msg_entity).cvar_cl_voice_directional >= 1) \
389                                                                     ? bound(ATTEN_MIN, CS(msg_entity).cvar_cl_voice_directional_taunt_attenuation, \
390                                                                         ATTEN_MAX) \
391                                                                         : ATTEN_NONE; \
392                                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten); \
393                                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten); \
394                                                                 else soundto(MSG_ONE, this, chan, sample, vol, atten); \
395                                                         } \
396                                                 } MACRO_END
397                                         if (fake)
398                                         {
399                                                 msg_entity = this;
400                                                 X();
401                                         }
402                                         else
403                                         {
404                                                 FOREACH_CLIENT(IS_REAL_CLIENT(it), {
405                                                         msg_entity = it;
406                                                         X();
407                                                 });
408                                         }
409                 #undef X
410                                         break;
411                                 }
412                                 case VOICETYPE_PLAYERSOUND:
413                                 {
414                                         msg_entity = this;
415                                         if (fake)
416                                         {
417                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, ATTEN_NORM);
418                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, ATTEN_NORM);
419                                                 else soundto(MSG_ONE, this, chan, sample, vol, ATTEN_NORM);
420                                         }
421                                         else
422                                         {
423                                                 if (gs) globalsound(MSG_ALL, this, gs, r, chan, vol, ATTEN_NORM);
424                                                 else if (ps) playersound(MSG_ALL, this, ps, r, chan, vol, ATTEN_NORM);
425                                                 else _sound(this, chan, sample, vol, ATTEN_NORM);
426                                         }
427                                         break;
428                                 }
429                                 default:
430                                 {
431                                         backtrace("Invalid voice type!");
432                                         break;
433                                 }
434                         }
435                 }
436
437         #endif