]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/effects/qc/globalsound.qc
Fix a crash and a warning with clones
[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 <common/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 = ReadSByte();
75                         float vol = ReadByte() / 255;
76                         float atten = ReadByte() / 64;
77                         vector o;
78                         o.x = ReadCoord();
79                         o.y = ReadCoord();
80                         o.z = ReadCoord();
81                         // TODO: is this really what we want to be doing? Footsteps that follow the player at head height?
82                         if (who == player_currententnum) e = findfloat(world, entnum, who);  // play at camera position for full volume
83                         else if (e) e.origin = o;
84                         if (e)
85                         {
86                                 sound7(e, chan, sample, vol, atten, 0, 0);
87                         }
88                         else
89                         {
90                                 // Can this happen?
91                                 LOG_WARNINGF("Missing entcs data for player %d\n", who);
92                                 sound8(e, o, chan, sample, vol, atten, 0, 0);
93                         }
94                         return true;
95                 }
96
97                 NET_HANDLE(playersound, bool isnew)
98                 {
99                         entity ps = PlayerSounds_from(ReadByte());
100                         float r = ReadByte() / 255;
101                         int who = ReadByte();
102                         entity e = entcs_receiver(who - 1);
103                         UpdatePlayerSounds(e);
104                         string s = e.(ps.m_playersoundfld);
105                         string sample = GlobalSound_sample(s, r);
106                         int chan = ReadSByte();
107                         float vol = ReadByte() / 255;
108                         float atten = ReadByte() / 64;
109                         vector o;
110                         o.x = ReadCoord();
111                         o.y = ReadCoord();
112                         o.z = ReadCoord();
113                         if (who == player_currententnum) e = findfloat(world, entnum, who);  // play at camera position for full volume
114                         else if (e) e.origin = o;
115                         if (e)
116                         {
117                                 // TODO: for non-visible players, origin should probably continue to be updated as long as the sound is playing
118                                 sound7(e, chan, sample, vol, atten, 0, 0);
119                         }
120                         else
121                         {
122                                 // Can this happen?
123                                 LOG_WARNINGF("Missing entcs data for player %d\n", who);
124                                 sound8(e, o, chan, sample, vol, atten, 0, 0);
125                         }
126                         return true;
127                 }
128
129         #endif
130
131         string GlobalSound_sample(string pair, float r)
132         {
133                 int n;
134                 {
135                         string s = cdr(pair);
136                         if (s) n = stof(s);
137                         else n = 0;
138                 }
139                 string sample = car(pair);
140                 if (n > 0) sample = sprintf("%s%d.wav", sample, floor(r * n + 1));  // randomization
141                 else sample = sprintf("%s.wav", sample);
142                 return sample;
143         }
144
145         void PrecacheGlobalSound(string sample)
146         {
147                 int n;
148                 {
149                         string s = cdr(sample);
150                         if (s) n = stof(s);
151                         else n = 0;
152                 }
153                 sample = car(sample);
154                 if (n > 0)
155                 {
156                         for (int i = 1; i <= n; ++i)
157                                 precache_sound(sprintf("%s%d.wav", sample, i));
158                 }
159                 else
160                 {
161                         precache_sound(sprintf("%s.wav", sample));
162                 }
163         }
164
165         entity GetVoiceMessage(string type)
166         {
167                 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == true, LAMBDA(return it));
168                 return NULL;
169         }
170
171         entity GetPlayerSound(string type)
172         {
173                 FOREACH(PlayerSounds, it.m_playersoundstr == type && it.instanceOfVoiceMessage == false, LAMBDA(return it));
174                 return NULL;
175         }
176
177         string allvoicesamples;
178         STATIC_INIT(allvoicesamples)
179         {
180                 FOREACH(PlayerSounds, it.instanceOfVoiceMessage, LAMBDA(
181                         allvoicesamples = strcat(allvoicesamples, " ", it.m_playersoundstr)
182                                                                            ));
183                 allvoicesamples = strzone(substring(allvoicesamples, 1, -1));
184         }
185
186         .string _GetPlayerSoundSampleField(string type, bool voice)
187         {
188                 GetPlayerSoundSampleField_notFound = false;
189                 entity e = voice ? GetVoiceMessage(type) : GetPlayerSound(type);
190                 if (e) return e.m_playersoundfld;
191                 GetPlayerSoundSampleField_notFound = true;
192                 return playersound_taunt.m_playersoundfld;
193         }
194
195         .string GetVoiceMessageSampleField(string type)
196         {
197                 return _GetPlayerSoundSampleField(type, true);
198         }
199
200         void PrecachePlayerSounds(string f)
201         {
202                 int fh = fopen(f, FILE_READ);
203                 if (fh < 0)
204                 {
205                         LOG_WARNINGF("Player sound file not found: %s\n", f);
206                         return;
207                 }
208                 for (string s; (s = fgets(fh)); )
209                 {
210                         int n = tokenize_console(s);
211                         if (n != 3)
212                         {
213                                 if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
214                                 continue;
215                         }
216                         string file = argv(1);
217                         string variants = argv(2);
218                         PrecacheGlobalSound(strcat(file, " ", variants));
219                 }
220                 fclose(fh);
221         }
222
223         #ifdef CSQC
224
225                 .string GetPlayerSoundSampleField(string type)
226                 {
227                         return _GetPlayerSoundSampleField(type, false);
228                 }
229
230                 void ClearPlayerSounds(entity this)
231                 {
232                         FOREACH(PlayerSounds, true, LAMBDA(
233                                 .string fld = it.m_playersoundfld;
234                                 if (this.(fld))
235         {
236                 strunzone(this.(fld));
237                 this.(fld) = string_null;
238         }
239                                                               ));
240                 }
241
242                 bool LoadPlayerSounds(entity this, string f, bool strict)
243                 {
244                         int fh = fopen(f, FILE_READ);
245                         if (fh < 0)
246                         {
247                                 if (strict) LOG_WARNINGF("Player sound file not found: %s\n", f);
248                                 return false;
249                         }
250                         for (string s; (s = fgets(fh)); )
251                         {
252                                 int n = tokenize_console(s);
253                                 if (n != 3)
254                                 {
255                                         if (n != 0) LOG_WARNINGF("Invalid sound info line: %s\n", s);
256                                         continue;
257                                 }
258                                 string key = argv(0);
259                                 var.string field = GetPlayerSoundSampleField(key);
260                                 if (GetPlayerSoundSampleField_notFound) field = GetVoiceMessageSampleField(key);
261                                 if (GetPlayerSoundSampleField_notFound)
262                                 {
263                                         LOG_TRACEF("Invalid sound info field: %s\n", key);
264                                         continue;
265                                 }
266                                 string file = argv(1);
267                                 string variants = argv(2);
268                                 if (this.(field)) strunzone(this.(field));
269                                 this.(field) = strzone(strcat(file, " ", variants));
270                         }
271                         fclose(fh);
272                         return true;
273                 }
274
275                 .string model_for_playersound;
276                 .int skin_for_playersound;
277
278                 bool autocvar_g_debug_defaultsounds;
279
280                 void UpdatePlayerSounds(entity this)
281                 {
282                         if (this.model == this.model_for_playersound && this.skin == this.skin_for_playersound) return;
283                         if (this.model_for_playersound) strunzone(this.model_for_playersound);
284                         this.model_for_playersound = strzone(this.model);
285                         this.skin_for_playersound = this.skin;
286                         ClearPlayerSounds(this);
287                         LoadPlayerSounds(this, "sound/player/default.sounds", true);
288                         if (this.model == "null" || autocvar_g_debug_defaultsounds) return;
289                         if (LoadPlayerSounds(this, get_model_datafilename(this.model, this.skin, "sounds"), false)) return;
290                         LoadPlayerSounds(this, get_model_datafilename(this.model, 0, "sounds"), true);
291                 }
292
293         #endif
294
295         #ifdef SVQC
296
297                 bool autocvar_g_debug_globalsounds;
298
299                 void _GlobalSound(entity this, entity gs, entity ps, string sample, int chan, int voicetype, bool fake)
300                 {
301                         if (gs == NULL && ps == NULL && sample == "") return;
302                         if(this.classname == "body") return;
303                         float r = random();
304                         if (sample != "") sample = GlobalSound_sample(sample, r);
305                         switch (voicetype)
306                         {
307                                 case VOICETYPE_LASTATTACKER_ONLY:
308                                 case VOICETYPE_LASTATTACKER:
309                                 {
310                                         if (!fake)
311                                         {
312                                                 if (!this.pusher) break;
313                                                 msg_entity = this.pusher;
314                                                 if (IS_REAL_CLIENT(msg_entity))
315                                                 {
316                                                         float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE;
317                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten);
318                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten);
319                                                         else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten);
320                                                 }
321                                         }
322                                         if (voicetype == VOICETYPE_LASTATTACKER_ONLY) break;
323                                         msg_entity = this;
324                                         if (IS_REAL_CLIENT(msg_entity))
325                                         {
326                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NONE);
327                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NONE);
328                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NONE);
329                                         }
330                                         break;
331                                 }
332                                 case VOICETYPE_TEAMRADIO:
333                                 {
334                                         #define X() \
335                                                 MACRO_BEGIN \
336                                                 { \
337                                                         float atten = (msg_entity.cvar_cl_voice_directional == 1) ? ATTEN_MIN : ATTEN_NONE; \
338                                                         if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
339                                                         else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
340                                                         else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
341                                                 } MACRO_END
342
343                                         if (fake) { msg_entity = this; X(); }
344                                         else
345                                         {
346                                                 FOREACH_CLIENT(IS_REAL_CLIENT(it) && (!teamplay || msg_entity.team == this.team), LAMBDA(
347                                                         msg_entity = it;
348                                                         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) && !IS_DEAD(this)) 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                                                 MACRO_BEGIN \
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) \
374                                                                 { \
375                                                                         if(autocvar_g_debug_globalsounds) \
376                                                                                 globalsound(MSG_ONE, this, gs, r, chan, VOL_BASEVOICE, atten); \
377                                                                         else \
378                                                                                 soundto(MSG_ONE, this, chan, GlobalSound_sample(gs.m_globalsoundstr, r), VOL_BASE, ATTEN_NORM); \
379                                                                 } \
380                                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASEVOICE, atten); \
381                                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASEVOICE, atten); \
382                                                         } \
383                                                 } MACRO_END
384                                         if (fake)
385                                         {
386                                                 msg_entity = this;
387                                                 X();
388                                         }
389                                         else
390                                         {
391                                                 FOREACH_CLIENT(IS_REAL_CLIENT(it), LAMBDA(
392                                                         msg_entity = it;
393                                                         X();
394                                                 ));
395                                         }
396                 #undef X
397                                         break;
398                                 }
399                                 case VOICETYPE_PLAYERSOUND:
400                                 {
401                                         msg_entity = this;
402                                         if (fake)
403                                         {
404                                                 if (gs) globalsound(MSG_ONE, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
405                                                 else if (ps) playersound(MSG_ONE, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
406                                                 else soundto(MSG_ONE, this, chan, sample, VOL_BASE, ATTEN_NORM);
407                                         }
408                                         else
409                                         {
410                                                 if (gs)
411                                                 {
412                                                         if(autocvar_g_debug_globalsounds)
413                                                                 globalsound(MSG_ALL, this, gs, r, chan, VOL_BASE, ATTEN_NORM);
414                                                         else
415                                                                 _sound(this, chan, GlobalSound_sample(gs.m_globalsoundstr, r), VOL_BASE, ATTEN_NORM);
416                                                 }
417                                                 else if (ps) playersound(MSG_ALL, this, ps, r, chan, VOL_BASE, ATTEN_NORM);
418                                                 else _sound(this, chan, sample, VOL_BASE, ATTEN_NORM);
419                                         }
420                                         break;
421                                 }
422                                 default:
423                                 {
424                                         backtrace("Invalid voice type!");
425                                         break;
426                                 }
427                         }
428                 }
429
430         #endif
431 #endif