]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/globalsound.qc
GlobalSound: clientside voice selection
[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, autocvar_cl_forceplayermodels ? player_currententnum : who);
96                         UpdatePlayerSounds(e);
97                         string s = e.(ps.m_playersoundfld);
98                         string sample = GlobalSound_sample(s, r);
99                         int chan = ReadByte();
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                         if (who == player_currententnum)
107                         {
108                                 // client knows better, play at current position to unlag
109                                 sound7(e, chan, sample, vol, atten, 0, 0);
110                         }
111                         else
112                         {
113                                 entity e = new(playersound);
114                                 e.origin = o;
115                                 sound8(e, o, chan, sample, vol, atten, 0, 0);
116                                 remove(e);  // debug with: e.think = SUB_Remove; e.nextthink = time + 1;
117                         }
118                         return true;
119                 }
120
121         #endif
122
123         string GlobalSound_sample(string pair, float r)
124         {
125                 int n;
126                 {
127                         string s = cdr(pair);
128                         if (s) n = stof(s);
129                         else n = 0;
130                 }
131                 string sample = car(pair);
132                 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1));  // randomization
133                 else sample = sprintf("%s.wav", sample);
134                 return sample;
135         }
136
137         void PrecacheGlobalSound(string sample)
138         {
139                 int n;
140                 {
141                         string s = cdr(sample);
142                         if (s) n = stof(s);
143                         else n = 0;
144                 }
145                 sample = car(sample);
146                 if (n > 0)
147                 {
148                         for (int i = 1; i <= n; ++i)
149                                 precache_sound(sprintf("%s%d.wav", sample, i));
150                 }
151                 else
152                 {
153                         precache_sound(sprintf("%s.wav", sample));
154                 }
155         }
156
157         entity GetVoiceMessage(string type)
158         {
159                 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, LAMBDA(return it));
160                 return NULL;
161         }
162
163         entity GetPlayerSound(string type)
164         {
165                 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, LAMBDA(return it));
166                 return NULL;
167         }
168
169         string allvoicesamples;
170         STATIC_INIT(allvoicesamples)
171         {
172                 FOREACH(PlayerSounds, it.instanceOfVoiceMessage, LAMBDA(
173                         allvoicesamples = strcat(allvoicesamples, " ", it.m_playersoundstr)
174                                                                            ));
175                 allvoicesamples = strzone(substring(allvoicesamples, 1, -1));
176         }
177
178         .string _GetPlayerSoundSampleField(string type, bool voice)
179         {
180                 GetPlayerSoundSampleField_notFound = false;
181                 entity e = voice ? GetVoiceMessage(type) : GetPlayerSound(type);
182                 if (e) return e.m_playersoundfld;
183                 GetPlayerSoundSampleField_notFound = true;
184                 return playersound_taunt.m_playersoundfld;
185         }
186
187         .string GetVoiceMessageSampleField(string type)
188         {
189                 return _GetPlayerSoundSampleField(type, true);
190         }
191
192         void PrecachePlayerSounds(string f)
193         {
194                 int fh = fopen(f, FILE_READ);
195                 if (fh < 0)
196                 {
197                         LOG_WARNINGF("Player sound file not found: %s\n", f);
198                         return;
199                 }
200                 for (string s; (s = fgets(fh)); )
201                 {
202                         int n = tokenize_console(s);
203                         if (n != 3)
204                         {
205                                 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
206                                 continue;
207                         }
208                         string file = argv(1);
209                         string variants = argv(2);
210                         PrecacheGlobalSound(strcat(file, " ", variants));
211                 }
212                 fclose(fh);
213         }
214
215         #ifdef CSQC
216
217                 .string GetPlayerSoundSampleField(string type)
218                 {
219                         return _GetPlayerSoundSampleField(type, false);
220                 }
221
222                 void ClearPlayerSounds(entity this)
223                 {
224                         FOREACH(PlayerSounds, true, LAMBDA(
225                                 .string fld = it.m_playersoundfld;
226                                 if (this.(fld))
227         {
228                 strunzone(this.(fld));
229                 this.(fld) = string_null;
230         }
231                                                               ));
232                 }
233
234                 bool LoadPlayerSounds(entity this, string f, bool strict)
235                 {
236                         int fh = fopen(f, FILE_READ);
237                         if (fh < 0)
238                         {
239                                 if (strict) LOG_WARNINGF("Player sound file not found: %s\n", f);
240                                 return false;
241                         }
242                         for (string s; (s = fgets(fh)); )
243                         {
244                                 int n = tokenize_console(s);
245                                 if (n != 3)
246                                 {
247                                         if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
248                                         continue;
249                                 }
250                                 string key = argv(0);
251                                 var.string field = GetPlayerSoundSampleField(key);
252                                 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
253                                 if (GetPlayerSoundSampleField_notFound)
254                                 {
255                                         LOG_TRACEF("Invalid sound info field: %s\n", key);
256                                         continue;
257                                 }
258                                 string file = argv(1);
259                                 string variants = argv(2);
260                                 if (this.(field)) strunzone(this.(field));
261                                 this.(field) = strzone(strcat(file, " ", variants));
262                         }
263                         fclose(fh);
264                         return true;
265                 }
266
267                 .int modelindex_for_playersound;
268                 .int skin_for_playersound;
269
270                 bool autocvar_g_debug_defaultsounds;
271
272                 void UpdatePlayerSounds(entity this)
273                 {
274                         if (this.modelindex == this.modelindex_for_playersound && this.skin == this.skin_for_playersound) return;
275                         this.modelindex_for_playersound = this.modelindex;
276                         this.skin_for_playersound = this.skin;
277                         ClearPlayerSounds(this);
278                         LoadPlayerSounds(this, "sound/player/default.sounds", true);
279                         if (this.model == "null" || autocvar_g_debug_defaultsounds) return;
280                         if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
281                         LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
282                 }
283
284         #endif
285
286         #ifdef SVQC
287
288                 void _GlobalSound(entity gs, entity ps, string sample, int chan, int voicetype, bool fake)
289                 {
290                         SELFPARAM();
291                         if (gs == NULL && ps == NULL && sample == "") return;
292                         float r = random();
293                         if (sample != "") sample = GlobalSound_sample(sample, r);
294                         switch (voicetype)
295                         {
296                                 case VOICETYPE_LASTATTACKER_ONLY:
297                                 case VOICETYPE_LASTATTACKER:
298                                 {
299                                         if (!fake)
300                                         {
301                                                 if (!this.pusher) break;
302                                                 msg_entity = this.pusher;
303                                                 if (IS_REAL_CLIENT(msg_entity))
304                                                 {
305                                                         float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
306                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten);
307                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten);
308                                                         else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten);
309                                                 }
310                                         }
311                                         if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
312                                         msg_entity = this;
313                                         if (IS_REAL_CLIENT(msg_entity))
314                                         {
315                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
316                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
317                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
318                                         }
319                                         break;
320                                 }
321                                 case VOICETYPE_TEAMRADIO:
322                                 {
323                                         #define X() \
324                                                 do \
325                                                 { \
326                                                         float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
327                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
328                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
329                                                         else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
330                                                 } \
331                                                 while (0)
332
333                                         if (fake) { msg_entity = this; X(); }
334                                         else
335                                         {
336                                                 FOR_EACH_REALCLIENT(msg_entity)
337                                                 {
338                                                         if (!teamplay || msg_entity.team == this.team) X();
339                                                 }
340                                         }
341                 #undef X
342                                         break;
343                                 }
344                                 case VOICETYPE_AUTOTAUNT:
345                                 case VOICETYPE_TAUNT:
346                                 {
347                                         if (voicetype == VOICETYPE_AUTOTAUNT) if (!sv_autotaunt) { break; }else {}
348                                         else if (IS_PLAYER(this) && this.deadflag == DEAD_NO) animdecide_setaction(this, ANIMACTION_TAUNT,
349                                                         true);
350                                         if (!sv_taunt) break;
351                                         if (autocvar_sv_gentle) break;
352                                         float tauntrand = 0;
353                                         if (voicetype == VOICETYPE_AUTOTAUNT) tauntrand = random();
354                                         #define X() \
355                                                 do \
356                                                 { \
357                                                         if (voicetype != VOICETYPE_AUTOTAUNT || tauntrand < msg_entity.cvar_cl_autotaunt) \
358                                                         { \
359                                                                 float atten = (msg_entity.cvar_cl_voice_directional >= 1) \
360                                                                     ? bound(ATTEN_MIN, msg_entity.cvar_cl_voice_directional_taunt_attenuation, \
361                                                                         ATTEN_MAX) \
362                                                                         : ATTEN_NONE; \
363                                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
364                                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
365                                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
366                                                         } \
367                                                 } \
368                                                 while (0)
369                                         if (fake)
370                                         {
371                                                 msg_entity = this;
372                                                 X();
373                                         }
374                                         else
375                                         {
376                                                 FOR_EACH_REALCLIENT(msg_entity)
377                                                 {
378                                                         X();
379                                                 }
380                                         }
381                 #undef X
382                                         break;
383                                 }
384                                 case VOICETYPE_PLAYERSOUND:
385                                 {
386                                         msg_entity = this;
387                                         if (fake)
388                                         {
389                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
390                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
391                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NORM);
392                                         }
393                                         else
394                                         {
395                                                 if (gs) globalsound(MSG_ALL, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
396                                                 else if (ps) playersound(MSG_ALL, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
397                                                 else _sound(this, chan, sample, VOL_BASE, ATTEN_NORM);
398                                         }
399                                         break;
400                                 }
401                                 default:
402                                 {
403                                         backtrace("Invalid voice type!");
404                                         break;
405                                 }
406                         }
407                 }
408
409         #endif
410 #endif