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