]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/globalsound.qc
Shownames: fade when moving to spectate
[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 "../../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         #ifdef SVQC
16                 /**
17                  * @param from the source entity, its position is sent
18                  * @param gs the global sound def
19                  * @param r a random number in 0..1
20                  */
21                 void globalsound(int channel, entity from, entity gs, float r, int chan, float vol, float atten)
22                 {
23                         if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
24                         WriteHeader(channel, globalsound);
25                         WriteByte(channel, gs.m_id);
26                         WriteByte(channel, r * 255);
27                         WriteByte(channel, etof(from));
28                         WriteByte(channel, fabs(chan));
29                         WriteByte(channel, floor(vol * 255));
30                         WriteByte(channel, floor(atten * 64));
31                         entcs_force_origin(from);
32                         vector o = from.origin + 0.5 * (from.mins + from.maxs);
33                         WriteCoord(channel, o.x);
34                         WriteCoord(channel, o.y);
35                         WriteCoord(channel, o.z);
36                 }
37
38                 /**
39                 * @param from the source entity, its position is sent
40                 * @param ps the player sound def
41                 * @param r a random number in 0..1
42                 */
43                 void playersound(int channel, entity from, entity ps, float r, int chan, float vol, float atten)
44                 {
45                         if (channel == MSG_ONE && !IS_REAL_CLIENT(msg_entity)) return;
46                         WriteHeader(channel, playersound);
47                         WriteByte(channel, ps.m_id);
48                         WriteByte(channel, r * 255);
49                         WriteByte(channel, etof(from));
50                         WriteByte(channel, fabs(chan));
51                         WriteByte(channel, floor(vol * 255));
52                         WriteByte(channel, floor(atten * 64));
53                         entcs_force_origin(from);
54                         vector o = from.origin + 0.5 * (from.mins + from.maxs);
55                         WriteCoord(channel, o.x);
56                         WriteCoord(channel, o.y);
57                         WriteCoord(channel, o.z);
58                 }
59         #endif
60
61         string GlobalSound_sample(string pair, float r);
62
63         #ifdef CSQC
64
65                 NET_HANDLE(globalsound, bool isnew)
66                 {
67                         entity gs = GlobalSounds_from(ReadByte());
68                         float r = ReadByte() / 255;
69                         string sample = GlobalSound_sample(gs.m_globalsoundstr, r);
70                         int who = ReadByte();
71                         entity e = entcs_receiver(who - 1);
72                         int chan = ReadByte();
73                         float vol = ReadByte() / 255;
74                         float atten = ReadByte() / 64;
75                         vector o;
76                         o.x = ReadCoord();
77                         o.y = ReadCoord();
78                         o.z = ReadCoord();
79                         // TODO: is this really what we want to be doing? Footsteps that follow the player at head height?
80                         if (who == player_currententnum) e = findfloat(world, entnum, who);  // play at camera position for full volume
81                         else if (e) e.origin = o;
82                         if (e)
83                         {
84                                 sound7(e, chan, sample, vol, atten, 0, 0);
85                         }
86                         else
87                         {
88                                 LOG_WARNINGF("Missing entcs data for player %d\n", who);
89                                 // Can this happen?
90                                 entity e = new(globalsound);
91                                 e.origin = o;
92                                 sound8(e, o, chan, sample, vol, atten, 0, 0);
93                                 remove(e);  // debug with: e.think = SUB_Remove; e.nextthink = time + 1;
94                         }
95                         return true;
96                 }
97
98                 NET_HANDLE(playersound, bool isnew)
99                 {
100                         entity ps = PlayerSounds_from(ReadByte());
101                         float r = ReadByte() / 255;
102                         int who = ReadByte();
103                         entity e = entcs_receiver(who - 1);
104                         UpdatePlayerSounds(e);
105                         string s = e.(ps.m_playersoundfld);
106                         string sample = GlobalSound_sample(s, r);
107                         int chan = ReadByte();
108                         float vol = ReadByte() / 255;
109                         float atten = ReadByte() / 64;
110                         vector o;
111                         o.x = ReadCoord();
112                         o.y = ReadCoord();
113                         o.z = ReadCoord();
114                         if (who == player_currententnum) e = findfloat(world, entnum, who);  // play at camera position for full volume
115                         else if (e) e.origin = o;
116                         if (e)
117                         {
118                                 // TODO: for non-visible players, origin should probably continue to be updated as long as the sound is playing
119                                 sound7(e, chan, sample, vol, atten, 0, 0);
120                         }
121                         else
122                         {
123                                 LOG_WARNINGF("Missing entcs data for player %d\n", who);
124                                 // Can this happen?
125                                 entity e = new(playersound);
126                                 e.origin = o;
127                                 sound8(e, o, chan, sample, vol, atten, 0, 0);
128                                 remove(e);  // debug with: e.think = SUB_Remove; e.nextthink = time + 1;
129                         }
130                         return true;
131                 }
132
133         #endif
134
135         string GlobalSound_sample(string pair, float r)
136         {
137                 int n;
138                 {
139                         string s = cdr(pair);
140                         if (s) n = stof(s);
141                         else n = 0;
142                 }
143                 string sample = car(pair);
144                 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1));  // randomization
145                 else sample = sprintf("%s.wav", sample);
146                 return sample;
147         }
148
149         void PrecacheGlobalSound(string sample)
150         {
151                 int n;
152                 {
153                         string s = cdr(sample);
154                         if (s) n = stof(s);
155                         else n = 0;
156                 }
157                 sample = car(sample);
158                 if (n > 0)
159                 {
160                         for (int i = 1; i <= n; ++i)
161                                 precache_sound(sprintf("%s%d.wav", sample, i));
162                 }
163                 else
164                 {
165                         precache_sound(sprintf("%s.wav", sample));
166                 }
167         }
168
169         entity GetVoiceMessage(string type)
170         {
171                 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, LAMBDA(return it));
172                 return NULL;
173         }
174
175         entity GetPlayerSound(string type)
176         {
177                 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, LAMBDA(return it));
178                 return NULL;
179         }
180
181         string allvoicesamples;
182         STATIC_INIT(allvoicesamples)
183         {
184                 FOREACH(PlayerSounds, it.instanceOfVoiceMessage, LAMBDA(
185                         allvoicesamples = strcat(allvoicesamples, " ", it.m_playersoundstr)
186                                                                            ));
187                 allvoicesamples = strzone(substring(allvoicesamples, 1, -1));
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_WARNINGF("Player sound file not found: %s\n", 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_WARNINGF("Invalid sound info line: %s\n", 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, LAMBDA(
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_WARNINGF("Player sound file not found: %s\n", 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_WARNINGF("Invalid sound info line: %s\n", 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\n", 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" || autocvar_g_debug_defaultsounds) return;
293                         if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
294                         LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
295                 }
296
297         #endif
298
299         #ifdef SVQC
300
301                 void _GlobalSound(entity gs, entity ps, string sample, int chan, int voicetype, bool fake)
302                 {
303                         SELFPARAM();
304                         if (gs == NULL && ps == NULL && sample == "") return;
305                         float r = random();
306                         if (sample != "") sample = GlobalSound_sample(sample, r);
307                         switch (voicetype)
308                         {
309                                 case VOICETYPE_LASTATTACKER_ONLY:
310                                 case VOICETYPE_LASTATTACKER:
311                                 {
312                                         if (!fake)
313                                         {
314                                                 if (!this.pusher) break;
315                                                 msg_entity = this.pusher;
316                                                 if (IS_REAL_CLIENT(msg_entity))
317                                                 {
318                                                         float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
319                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten);
320                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten);
321                                                         else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten);
322                                                 }
323                                         }
324                                         if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
325                                         msg_entity = this;
326                                         if (IS_REAL_CLIENT(msg_entity))
327                                         {
328                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
329                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
330                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
331                                         }
332                                         break;
333                                 }
334                                 case VOICETYPE_TEAMRADIO:
335                                 {
336                                         #define X() \
337                                                 do \
338                                                 { \
339                                                         float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
340                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
341                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
342                                                         else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
343                                                 } \
344                                                 while (0)
345
346                                         if (fake) { msg_entity = this; X(); }
347                                         else
348                                         {
349                                                 FOR_EACH_REALCLIENT(msg_entity)
350                                                 {
351                                                         if (!teamplay || msg_entity.team == this.team) X();
352                                                 }
353                                         }
354                 #undef X
355                                         break;
356                                 }
357                                 case VOICETYPE_AUTOTAUNT:
358                                 case VOICETYPE_TAUNT:
359                                 {
360                                         if (voicetype == VOICETYPE_AUTOTAUNT) if (!sv_autotaunt) { break; }else {}
361                                         else if (IS_PLAYER(this) && this.deadflag == DEAD_NO) animdecide_setaction(this, ANIMACTION_TAUNT,
362                                                         true);
363                                         if (!sv_taunt) break;
364                                         if (autocvar_sv_gentle) break;
365                                         float tauntrand = 0;
366                                         if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
367                                         #define X() \
368                                                 do \
369                                                 { \
370                                                         if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < msg_entity.cvar_cl_autotaunt) \
371                                                         { \
372                                                                 float atten = (msg_entity.cvar_cl_voice_directional >= 1) \
373                                                                     ? bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, \
374                                                                         ATTEN_MAX) \
375                                                                         : ATTEN_NONE; \
376                                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
377                                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
378                                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
379                                                         } \
380                                                 } \
381                                                 while (0)
382                                         if (fake)
383                                         {
384                                                 msg_entity = this;
385                                                 X();
386                                         }
387                                         else
388                                         {
389                                                 FOR_EACH_REALCLIENT(msg_entity)
390                                                 {
391                                                         X();
392                                                 }
393                                         }
394                 #undef X
395                                         break;
396                                 }
397                                 case VOICETYPE_PLAYERSOUND:
398                                 {
399                                         msg_entity = this;
400                                         if (fake)
401                                         {
402                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
403                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
404                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NORM);
405                                         }
406                                         else
407                                         {
408                                                 if (gs) globalsound(MSG_ALL, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
409                                                 else if (ps) playersound(MSG_ALL, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
410                                                 else _sound(this, chan, sample, VOL_BASE, ATTEN_NORM);
411                                         }
412                                         break;
413                                 }
414                                 default:
415                                 {
416                                         backtrace("Invalid voice type!");
417                                         break;
418                                 }
419                         }
420                 }
421
422         #endif
423 #endif