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