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