c2014bf95ba0bbe515ef14cf5b591a7fc8314887
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / target / music.qc
1 #if defined(CSQC)
2 #elif defined(MENUQC)
3 #elif defined(SVQC)
4     #include "../../constants.qh"
5     #include "../../../server/constants.qh"
6     #include "../../../server/defs.qh"
7 #endif
8
9 #ifdef SVQC
10
11 // values:
12 //   volume
13 //   noise
14 //   targetname
15 //   lifetime
16 //   fade_time
17 //   fade_rate
18 // when triggered, the music is overridden for activator until lifetime (or forever, if lifetime is 0)
19 // when targetname is not set, THIS ONE is default
20 void target_music_sendto(float to, float is)
21 {SELFPARAM();
22         WriteHeader(to, TE_CSQC_TARGET_MUSIC);
23         WriteShort(to, num_for_edict(self));
24         WriteByte(to, self.volume * 255.0 * is);
25         WriteByte(to, self.fade_time * 16.0);
26         WriteByte(to, self.fade_rate * 16.0);
27         WriteByte(to, self.lifetime);
28         WriteString(to, self.noise);
29 }
30 void target_music_reset()
31 {SELFPARAM();
32         if(self.targetname == "")
33                 target_music_sendto(MSG_ALL, 1);
34 }
35 void target_music_kill()
36 {
37         for(self = world; (self = find(self, classname, "target_music")); )
38         {
39                 self.volume = 0;
40                 if(self.targetname == "")
41                         target_music_sendto(MSG_ALL, 1);
42                 else
43                         target_music_sendto(MSG_ALL, 0);
44         }
45 }
46 void target_music_use()
47 {
48         if(!activator)
49                 return;
50         if(IS_REAL_CLIENT(activator))
51         {
52                 msg_entity = activator;
53                 target_music_sendto(MSG_ONE, 1);
54         }
55         entity head;
56         FOR_EACH_SPEC(head) if(head.enemy == activator) { msg_entity = head; target_music_sendto(MSG_ONE, 1); }
57 }
58 spawnfunc(target_music)
59 {
60         self.use = target_music_use;
61         self.reset = target_music_reset;
62         if(!self.volume)
63                 self.volume = 1;
64         if(self.targetname == "")
65                 target_music_sendto(MSG_INIT, 1);
66         else
67                 target_music_sendto(MSG_INIT, 0);
68 }
69 void TargetMusic_RestoreGame()
70 {SELFPARAM();
71         for(entity e = world; (e = find(e, classname, "target_music")); )
72         {
73                 setself(e);
74                 if(self.targetname == "")
75                         target_music_sendto(MSG_INIT, 1);
76                 else
77                         target_music_sendto(MSG_INIT, 0);
78         }
79 }
80 // values:
81 //   volume
82 //   noise
83 //   targetname
84 //   fade_time
85 // spawnflags:
86 //   1 = START_OFF
87 // when triggered, it is disabled/enabled for everyone
88 bool trigger_music_SendEntity(entity this, entity to, float sf)
89 {
90         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_MUSIC);
91         sf &= ~0x80;
92         if(self.cnt)
93                 sf |= 0x80;
94         WriteByte(MSG_ENTITY, sf);
95         if(sf & 4)
96         {
97                 WriteCoord(MSG_ENTITY, self.origin.x);
98                 WriteCoord(MSG_ENTITY, self.origin.y);
99                 WriteCoord(MSG_ENTITY, self.origin.z);
100         }
101         if(sf & 1)
102         {
103                 if(self.model != "null")
104                 {
105                         WriteShort(MSG_ENTITY, self.modelindex);
106                         WriteCoord(MSG_ENTITY, self.mins.x);
107                         WriteCoord(MSG_ENTITY, self.mins.y);
108                         WriteCoord(MSG_ENTITY, self.mins.z);
109                         WriteCoord(MSG_ENTITY, self.maxs.x);
110                         WriteCoord(MSG_ENTITY, self.maxs.y);
111                         WriteCoord(MSG_ENTITY, self.maxs.z);
112                 }
113                 else
114                 {
115                         WriteShort(MSG_ENTITY, 0);
116                         WriteCoord(MSG_ENTITY, self.maxs.x);
117                         WriteCoord(MSG_ENTITY, self.maxs.y);
118                         WriteCoord(MSG_ENTITY, self.maxs.z);
119                 }
120                 WriteByte(MSG_ENTITY, self.volume * 255.0);
121                 WriteByte(MSG_ENTITY, self.fade_time * 16.0);
122                 WriteByte(MSG_ENTITY, self.fade_rate * 16.0);
123                 WriteString(MSG_ENTITY, self.noise);
124         }
125         return 1;
126 }
127 void trigger_music_reset()
128 {SELFPARAM();
129         self.cnt = !(self.spawnflags & 1);
130         self.SendFlags |= 0x80;
131 }
132 void trigger_music_use()
133 {SELFPARAM();
134         self.cnt = !self.cnt;
135         self.SendFlags |= 0x80;
136 }
137 spawnfunc(trigger_music)
138 {
139         if(self.model != "")
140                 _setmodel(self, self.model);
141         if(!self.volume)
142                 self.volume = 1;
143         if(!self.modelindex)
144         {
145                 setorigin(self, self.origin + self.mins);
146                 setsize(self, '0 0 0', self.maxs - self.mins);
147         }
148         trigger_music_reset();
149
150         self.use = trigger_music_use;
151         self.reset = trigger_music_reset;
152
153         Net_LinkEntity(self, false, 0, trigger_music_SendEntity);
154 }
155 #elif defined(CSQC)
156
157 void TargetMusic_Advance()
158 {
159         // run AFTER all the thinks!
160         entity best, e;
161         float vol, vol0;
162         best = music_default;
163         if(music_target && time < music_target.lifetime)
164                 best = music_target;
165         if(music_trigger)
166                 best = music_trigger;
167         for(e = world; (e = findfloat(e, enttype, NET_ENT_CLIENT_TRIGGER_MUSIC.m_id)); ) if(e.noise)
168         {
169                 vol0 = e.lastvol;
170                 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
171                 {
172                         vol0 = -1;
173                 }
174                 if(e == best)
175                 {
176                         // increase volume
177                         if(e.fade_time > 0)
178                                 e.state = bound(0, e.state + frametime / e.fade_time, 1);
179                         else
180                                 e.state = 1;
181                 }
182                 else
183                 {
184                         // decrease volume
185                         if(e.fade_rate > 0)
186                                 e.state = bound(0, e.state - frametime / e.fade_rate, 1);
187                         else
188                                 e.state = 0;
189                 }
190                 vol = e.state * e.volume * autocvar_bgmvolume;
191                 if(vol != vol0)
192                 {
193                         if(vol0 < 0)
194                                 _sound(e, CH_BGM_SINGLE, e.noise, vol, ATTEN_NONE); // restart
195                         else
196                                 _sound(e, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
197                         e.lastvol = vol;
198                 }
199         }
200         music_trigger = world;
201
202         if(best)
203                 bgmtime = getsoundtime(best, CH_BGM_SINGLE);
204         else
205                 bgmtime = gettime(GETTIME_CDTRACK);
206 }
207
208 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
209 {
210         Net_TargetMusic();
211         return true;
212 }
213
214 void Net_TargetMusic()
215 {
216         int id = ReadShort();
217         float vol = ReadByte() / 255.0;
218         float fai = ReadByte() / 16.0;
219         float fao = ReadByte() / 16.0;
220         float tim = ReadByte();
221         string noi = ReadString();
222
223         entity e;
224         for(e = world; (e = findfloat(e, enttype, NET_ENT_CLIENT_TRIGGER_MUSIC.m_id)); )
225         {
226                 if(e.count == id)
227                         break;
228         }
229         if(!e)
230         {
231                 e = spawn();
232                 e.enttype = NET_ENT_CLIENT_TRIGGER_MUSIC.m_id;
233                 e.count = id;
234         }
235         if(e.noise != noi)
236         {
237                 if(e.noise)
238                         strunzone(e.noise);
239                 e.noise = strzone(noi);
240                 precache_sound(e.noise);
241                 _sound(e, CH_BGM_SINGLE, e.noise, 0, ATTEN_NONE);
242                 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
243                 {
244                         LOG_TRACEF("Cannot initialize sound %s\n", e.noise);
245                         strunzone(e.noise);
246                         e.noise = string_null;
247                 }
248         }
249         e.volume = vol;
250         e.fade_time = fai;
251         e.fade_rate = fao;
252         if(vol > 0)
253         {
254                 if(tim == 0)
255                 {
256                         music_default = e;
257                         if(!music_disabled)
258                         {
259                                 e.state = 2;
260                                 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
261                                 localcmd("cd stop\n"); // just in case
262                                 music_disabled = 1;
263                         }
264                 }
265                 else
266                 {
267                         music_target = e;
268                         e.lifetime = time + tim;
269                 }
270         }
271 }
272
273 void Ent_TriggerMusic_Think()
274 {SELFPARAM();
275         if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, self, world))
276         {
277                 music_trigger = self;
278         }
279         self.nextthink = time;
280 }
281
282 void Ent_TriggerMusic_Remove()
283 {SELFPARAM();
284         if(self.noise)
285                 strunzone(self.noise);
286         self.noise = string_null;
287 }
288
289 void Ent_ReadTriggerMusic()
290 {SELFPARAM();
291         int f = ReadByte();
292         if(f & 4)
293         {
294                 self.origin_x = ReadCoord();
295                 self.origin_y = ReadCoord();
296                 self.origin_z = ReadCoord();
297         }
298         if(f & 1)
299         {
300                 self.modelindex = ReadShort();
301                 if(self.modelindex)
302                 {
303                         self.mins_x = ReadCoord();
304                         self.mins_y = ReadCoord();
305                         self.mins_z = ReadCoord();
306                         self.maxs_x = ReadCoord();
307                         self.maxs_y = ReadCoord();
308                         self.maxs_z = ReadCoord();
309                 }
310                 else
311                 {
312                         self.mins    = '0 0 0';
313                         self.maxs_x = ReadCoord();
314                         self.maxs_y = ReadCoord();
315                         self.maxs_z = ReadCoord();
316                 }
317
318                 self.volume = ReadByte() / 255.0;
319                 self.fade_time = ReadByte() / 16.0;
320                 self.fade_rate = ReadByte() / 16.0;
321                 string s = self.noise;
322                 if(self.noise)
323                         strunzone(self.noise);
324                 self.noise = strzone(ReadString());
325                 if(self.noise != s)
326                 {
327                         precache_sound(self.noise);
328                         _sound(self, CH_BGM_SINGLE, self.noise, 0, ATTEN_NONE);
329                         if(getsoundtime(self, CH_BGM_SINGLE) < 0)
330                         {
331                                 LOG_TRACEF("Cannot initialize sound %s\n", self.noise);
332                                 strunzone(self.noise);
333                                 self.noise = string_null;
334                         }
335                 }
336         }
337
338         setorigin(self, self.origin);
339         setsize(self, self.mins, self.maxs);
340         self.cnt = 1;
341         self.think = Ent_TriggerMusic_Think;
342         self.nextthink = time;
343 }
344
345 #endif