]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/globalsound.qc
Merge branch 'martin-t/cfgs' into 'master'
[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 = GlobalSounds_from(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 = PlayerSounds_from(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                                         strunzone(this.(fld));
241                                         this.(fld) = string_null;
242                                 }
243                         });
244                 }
245
246                 bool LoadPlayerSounds(entity this, string f, bool strict)
247                 {
248                         int fh = fopen(f, FILE_READ);
249                         if (fh < 0)
250                         {
251                                 if (strict) LOG_WARNF("Player sound file not found: %s", f);
252                                 return false;
253                         }
254                         for (string s; (s = fgets(fh)); )
255                         {
256                                 int n = tokenize_console(s);
257                                 if (n != 3)
258                                 {
259                                         if (n != 0) LOG_WARNF("Invalid sound info line: %s", s);
260                                         continue;
261                                 }
262                                 string key = argv(0);
263                                 var.string field = GetPlayerSoundSampleField(key);
264                                 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
265                                 if (GetPlayerSoundSampleField_notFound)
266                                 {
267                                         LOG_TRACEF("Invalid sound info field: %s", key);
268                                         continue;
269                                 }
270                                 string file = argv(1);
271                                 string variants = argv(2);
272                                 if (this.(field)) strunzone(this.(field));
273                                 this.(field) = strzone(strcat(file, " ", variants));
274                         }
275                         fclose(fh);
276                         return true;
277                 }
278
279                 .string model_for_playersound;
280                 .int skin_for_playersound;
281
282                 bool autocvar_g_debug_defaultsounds;
283
284                 void UpdatePlayerSounds(entity this)
285                 {
286                         if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
287                         if (this.model_for_playersound) strunzone(this.model_for_playersound);
288                         this.model_for_playersound = strzone(this.model);
289                         this.skin_for_playersound = this.skin;
290                         ClearPlayerSounds(this);
291                         LoadPlayerSounds(this, "sound/player/default.sounds", true);
292                         if (this.model == "null"
293                         #ifdef SVQC
294                             && autocvar_g_debug_globalsounds
295                         #endif
296                          ) return;
297                         if (autocvar_g_debug_defaultsounds) return;
298                         if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
299                         LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
300                 }
301
302         //#endif
303
304         #ifdef SVQC
305
306                 void _GlobalSound(entity this, entity gs, entity ps, string sample, int chan, float vol, int voicetype, bool fake)
307                 {
308                         if (gs == NULL && ps == NULL && sample == "") return;
309                         if(this.classname == "body") return;
310                         float r = random();
311                         if (sample != "") sample = GlobalSound_sample(sample, r);
312                         switch (voicetype)
313                         {
314                                 case VOICETYPE_LASTATTACKER_ONLY:
315                                 case VOICETYPE_LASTATTACKER:
316                                 {
317                                         if (!fake)
318                                         {
319                                                 if (!this.pusher) break;
320                                                 msg_entity = this.pusher;
321                                                 if (IS_REAL_CLIENT(msg_entity))
322                                                 {
323                                                         float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
324                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten);
325                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten);
326                                                         else soundto(MSG_ONE, this, chan, sample, vol, atten);
327                                                 }
328                                         }
329                                         if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
330                                         msg_entity = this;
331                                         if (IS_REAL_CLIENT(msg_entity))
332                                         {
333                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
334                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
335                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
336                                         }
337                                         break;
338                                 }
339                                 case VOICETYPE_TEAMRADIO:
340                                 {
341                                         #define X() \
342                                                 MACRO_BEGIN \
343                                                 { \
344                                                         float atten = (CS(msg_entity).cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
345                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, vol, atten); \
346                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, vol, atten); \
347                                                         else soundto(MSG_ONE, this, chan, sample, vol, atten); \
348                                                 } MACRO_END
349
350                                         if (fake) { msg_entity = this; X(); }
351                                         else
352                                         {
353                                                 FOREACH_CLIENT(IS_REAL_CLIENT(it) && SAME_TEAM(it, this), {
354                                                         msg_entity = it;
355                                                         X();
356                                                 });
357                                         }
358                 #undef X
359                                         break;
360                                 }
361                                 case VOICETYPE_AUTOTAUNT:
362                                 case VOICETYPE_TAUNT:
363                                 {
364                                         if (voicetype == VOICETYPE_AUTOTAUNT) if (!sv_autotaunt) { break; }else {}
365                                         else if (IS_PLAYER(this) && !IS_DEAD(this)) animdecide_setaction(this, ANIMACTION_TAUNT,
366                                                         true);
367                                         if (!sv_taunt) break;
368                                         if (autocvar_sv_gentle) break;
369                                         float tauntrand = 0;
370                                         if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
371                                         #define X() \
372                                                 MACRO_BEGIN \
373                                                 { \
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