]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/target/music.qc
Merge branch 'master' into terencehill/dynamic_hud
[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 <common/constants.qh>
5     #include <server/constants.qh>
6     #include <server/defs.qh>
7 #endif
8
9 REGISTER_NET_TEMP(TE_CSQC_TARGET_MUSIC)
10 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_MUSIC)
11
12 #ifdef SVQC
13
14 // values:
15 //   volume
16 //   noise
17 //   targetname
18 //   lifetime
19 //   fade_time
20 //   fade_rate
21 // when triggered, the music is overridden for activator until lifetime (or forever, if lifetime is 0)
22 // when targetname is not set, THIS ONE is default
23 void target_music_sendto(float to, float is)
24 {SELFPARAM();
25         WriteHeader(to, TE_CSQC_TARGET_MUSIC);
26         WriteShort(to, etof(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(entity this)
34 {
35         if (this.targetname == "") target_music_sendto(MSG_ALL, 1);
36 }
37 void target_music_kill()
38 {
39     SELFPARAM();
40         for(self = world; (self = find(self, classname, "target_music")); )
41         {
42                 self.volume = 0;
43                 if(self.targetname == "")
44                         target_music_sendto(MSG_ALL, 1);
45                 else
46                         target_music_sendto(MSG_ALL, 0);
47         }
48 }
49 void target_music_use()
50 {
51         if(!activator)
52                 return;
53         if(IS_REAL_CLIENT(activator))
54         {
55                 msg_entity = activator;
56                 target_music_sendto(MSG_ONE, 1);
57         }
58         FOREACH_CLIENT(IS_SPEC(it) && it.enemy == activator, {
59                 msg_entity = it;
60                 target_music_sendto(MSG_ONE, 1);
61         });
62 }
63 spawnfunc(target_music)
64 {
65         self.use = target_music_use;
66         self.reset = target_music_reset;
67         if(!self.volume)
68                 self.volume = 1;
69         if(self.targetname == "")
70                 target_music_sendto(MSG_INIT, 1);
71         else
72                 target_music_sendto(MSG_INIT, 0);
73 }
74 void TargetMusic_RestoreGame()
75 {SELFPARAM();
76         for(entity e = world; (e = find(e, classname, "target_music")); )
77         {
78                 setself(e);
79                 if(self.targetname == "")
80                         target_music_sendto(MSG_INIT, 1);
81                 else
82                         target_music_sendto(MSG_INIT, 0);
83         }
84 }
85 // values:
86 //   volume
87 //   noise
88 //   targetname
89 //   fade_time
90 // spawnflags:
91 //   1 = START_OFF
92 // when triggered, it is disabled/enabled for everyone
93 bool trigger_music_SendEntity(entity this, entity to, float sf)
94 {
95         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_MUSIC);
96         sf &= ~0x80;
97         if(self.cnt)
98                 sf |= 0x80;
99         WriteByte(MSG_ENTITY, sf);
100         if(sf & 4)
101         {
102                 WriteCoord(MSG_ENTITY, self.origin.x);
103                 WriteCoord(MSG_ENTITY, self.origin.y);
104                 WriteCoord(MSG_ENTITY, self.origin.z);
105         }
106         if(sf & 1)
107         {
108                 if(self.model != "null")
109                 {
110                         WriteShort(MSG_ENTITY, self.modelindex);
111                         WriteCoord(MSG_ENTITY, self.mins.x);
112                         WriteCoord(MSG_ENTITY, self.mins.y);
113                         WriteCoord(MSG_ENTITY, self.mins.z);
114                         WriteCoord(MSG_ENTITY, self.maxs.x);
115                         WriteCoord(MSG_ENTITY, self.maxs.y);
116                         WriteCoord(MSG_ENTITY, self.maxs.z);
117                 }
118                 else
119                 {
120                         WriteShort(MSG_ENTITY, 0);
121                         WriteCoord(MSG_ENTITY, self.maxs.x);
122                         WriteCoord(MSG_ENTITY, self.maxs.y);
123                         WriteCoord(MSG_ENTITY, self.maxs.z);
124                 }
125                 WriteByte(MSG_ENTITY, self.volume * 255.0);
126                 WriteByte(MSG_ENTITY, self.fade_time * 16.0);
127                 WriteByte(MSG_ENTITY, self.fade_rate * 16.0);
128                 WriteString(MSG_ENTITY, self.noise);
129         }
130         return 1;
131 }
132 void trigger_music_reset(entity this)
133 {
134         this.cnt = !(this.spawnflags & 1);
135         this.SendFlags |= 0x80;
136 }
137 void trigger_music_use()
138 {SELFPARAM();
139         self.cnt = !self.cnt;
140         self.SendFlags |= 0x80;
141 }
142 spawnfunc(trigger_music)
143 {
144         if(this.model != "") _setmodel(this, this.model);
145         if(!this.volume) this.volume = 1;
146         if(!this.modelindex)
147         {
148                 setorigin(this, this.origin + this.mins);
149                 setsize(this, '0 0 0', this.maxs - this.mins);
150         }
151         trigger_music_reset(this);
152
153         this.use = trigger_music_use;
154         this.reset = trigger_music_reset;
155
156         Net_LinkEntity(this, false, 0, trigger_music_SendEntity);
157 }
158 #elif defined(CSQC)
159
160 entity TargetMusic_list;
161 STATIC_INIT(TargetMusic_list)
162 {
163         TargetMusic_list = LL_NEW();
164 }
165
166 void TargetMusic_Advance()
167 {
168         // run AFTER all the thinks!
169         entity best = music_default;
170         if (music_target && time < music_target.lifetime) best = music_target;
171         if (music_trigger) best = music_trigger;
172         LL_EACH(TargetMusic_list, it.noise, {
173                 const float vol0 = (getsoundtime(it, CH_BGM_SINGLE) >= 0) ? it.lastvol : -1;
174                 if (it == best)
175                 {
176                         // increase volume
177                         it.state = (it.fade_time > 0) ? bound(0, it.state + frametime / it.fade_time, 1) : 1;
178                 }
179                 else
180                 {
181                         // decrease volume
182                         it.state = (it.fade_rate > 0) ? bound(0, it.state - frametime / it.fade_rate, 1) : 0;
183                 }
184                 const float vol = it.state * it.volume * autocvar_bgmvolume;
185                 if (vol != vol0)
186                 {
187                         if(vol0 < 0)
188                                 _sound(it, CH_BGM_SINGLE, it.noise, vol, ATTEN_NONE); // restart
189                         else
190                                 _sound(it, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
191                         it.lastvol = vol;
192                 }
193         });
194         music_trigger = world;
195         bgmtime = (best) ? getsoundtime(best, CH_BGM_SINGLE) : gettime(GETTIME_CDTRACK);
196 }
197
198 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
199 {
200         Net_TargetMusic();
201         return true;
202 }
203
204 void Net_TargetMusic()
205 {
206         const int id = ReadShort();
207         const float vol = ReadByte() / 255.0;
208         const float fai = ReadByte() / 16.0;
209         const float fao = ReadByte() / 16.0;
210         const float tim = ReadByte();
211         const string noi = ReadString();
212
213         entity e = NULL;
214         LL_EACH(TargetMusic_list, it.count == id, { e = it; break; });
215         if (!e)
216         {
217                 LL_PUSH(TargetMusic_list, e = new_pure(TargetMusic));
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                         LOG_TRACEF("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 {SELFPARAM();
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 {SELFPARAM();
269         if(self.noise)
270                 strunzone(self.noise);
271         self.noise = string_null;
272 }
273
274 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
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                                 LOG_TRACEF("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         return true;
329 }
330
331 #endif