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