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