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