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