]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/target/music.qc
Make ReadVector/WriteVector more commonly used
[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                 WriteVector(MSG_ENTITY, this.origin);
107         }
108         if(sf & 1)
109         {
110                 if(this.model != "null")
111                 {
112                         WriteShort(MSG_ENTITY, this.modelindex);
113                         WriteVector(MSG_ENTITY, this.mins);
114                         WriteVector(MSG_ENTITY, this.maxs);
115                 }
116                 else
117                 {
118                         WriteShort(MSG_ENTITY, 0);
119                         WriteVector(MSG_ENTITY, this.maxs);
120                 }
121                 WriteByte(MSG_ENTITY, this.volume * 255.0);
122                 WriteByte(MSG_ENTITY, this.fade_time * 16.0);
123                 WriteByte(MSG_ENTITY, this.fade_rate * 16.0);
124                 WriteString(MSG_ENTITY, this.noise);
125         }
126         return 1;
127 }
128 void trigger_music_reset(entity this)
129 {
130         this.cnt = !(this.spawnflags & 1);
131         this.SendFlags |= 0x80;
132 }
133 void trigger_music_use(entity this, entity actor, entity trigger)
134 {
135         this.cnt = !this.cnt;
136         this.SendFlags |= 0x80;
137 }
138 spawnfunc(trigger_music)
139 {
140         if(this.model != "") _setmodel(this, this.model);
141         if(!this.volume) this.volume = 1;
142         if(!this.modelindex)
143         {
144                 setorigin(this, this.origin + this.mins);
145                 setsize(this, '0 0 0', this.maxs - this.mins);
146         }
147         trigger_music_reset(this);
148
149         this.use = trigger_music_use;
150         this.reset = trigger_music_reset;
151
152         Net_LinkEntity(this, false, 0, trigger_music_SendEntity);
153 }
154 #elif defined(CSQC)
155
156 entity TargetMusic_list;
157 STATIC_INIT(TargetMusic_list)
158 {
159         TargetMusic_list = LL_NEW();
160 }
161
162 void TargetMusic_Advance()
163 {
164         // run AFTER all the thinks!
165         entity best = music_default;
166         if (music_target && time < music_target.lifetime) best = music_target;
167         if (music_trigger) best = music_trigger;
168         LL_EACH(TargetMusic_list, it.noise, {
169                 const float vol0 = (getsoundtime(it, CH_BGM_SINGLE) >= 0) ? it.lastvol : -1;
170                 if (it == best)
171                 {
172                         // increase volume
173                         it.state = (it.fade_time > 0) ? bound(0, it.state + frametime / it.fade_time, 1) : 1;
174                 }
175                 else
176                 {
177                         // decrease volume
178                         it.state = (it.fade_rate > 0) ? bound(0, it.state - frametime / it.fade_rate, 1) : 0;
179                 }
180                 const float vol = it.state * it.volume * autocvar_bgmvolume;
181                 if (vol != vol0)
182                 {
183                         if(vol0 < 0)
184                                 _sound(it, CH_BGM_SINGLE, it.noise, vol, ATTEN_NONE); // restart
185                         else
186                                 _sound(it, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
187                         it.lastvol = vol;
188                 }
189         });
190         music_trigger = NULL;
191         bgmtime = (best) ? getsoundtime(best, CH_BGM_SINGLE) : gettime(GETTIME_CDTRACK);
192 }
193
194 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
195 {
196         Net_TargetMusic();
197         return true;
198 }
199
200 void Net_TargetMusic()
201 {
202         const int id = ReadShort();
203         const float vol = ReadByte() / 255.0;
204         const float fai = ReadByte() / 16.0;
205         const float fao = ReadByte() / 16.0;
206         const float tim = ReadByte();
207         const string noi = ReadString();
208
209         entity e = NULL;
210         LL_EACH(TargetMusic_list, it.count == id, { e = it; break; });
211         if (!e)
212         {
213                 LL_PUSH(TargetMusic_list, e = new_pure(TargetMusic));
214                 e.count = id;
215         }
216         if(e.noise != noi)
217         {
218                 if(e.noise)
219                         strunzone(e.noise);
220                 e.noise = strzone(noi);
221                 precache_sound(e.noise);
222                 _sound(e, CH_BGM_SINGLE, e.noise, 0, ATTEN_NONE);
223                 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
224                 {
225                         LOG_TRACEF("Cannot initialize sound %s", e.noise);
226                         strunzone(e.noise);
227                         e.noise = string_null;
228                 }
229         }
230         e.volume = vol;
231         e.fade_time = fai;
232         e.fade_rate = fao;
233         if(vol > 0)
234         {
235                 if(tim == 0)
236                 {
237                         music_default = e;
238                         if(!music_disabled)
239                         {
240                                 e.state = 2;
241                                 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
242                                 localcmd("cd stop\n"); // just in case
243                                 music_disabled = 1;
244                         }
245                 }
246                 else
247                 {
248                         music_target = e;
249                         e.lifetime = time + tim;
250                 }
251         }
252 }
253
254 void Ent_TriggerMusic_Think(entity this)
255 {
256         if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, this, NULL))
257         {
258                 music_trigger = this;
259         }
260         this.nextthink = time;
261 }
262
263 void Ent_TriggerMusic_Remove(entity this)
264 {
265         if(this.noise)
266                 strunzone(this.noise);
267         this.noise = string_null;
268 }
269
270 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
271 {
272         int f = ReadByte();
273         if(f & 4)
274         {
275                 this.origin = ReadVector();
276         }
277         if(f & 1)
278         {
279                 this.modelindex = ReadShort();
280                 if(this.modelindex)
281                 {
282                         this.mins = ReadVector();
283                         this.maxs = ReadVector();
284                 }
285                 else
286                 {
287                         this.mins    = '0 0 0';
288                         this.maxs = ReadVector();
289                 }
290
291                 this.volume = ReadByte() / 255.0;
292                 this.fade_time = ReadByte() / 16.0;
293                 this.fade_rate = ReadByte() / 16.0;
294                 string s = this.noise;
295                 if(this.noise)
296                         strunzone(this.noise);
297                 this.noise = strzone(ReadString());
298                 if(this.noise != s)
299                 {
300                         precache_sound(this.noise);
301                         _sound(this, CH_BGM_SINGLE, this.noise, 0, ATTEN_NONE);
302                         if(getsoundtime(this, CH_BGM_SINGLE) < 0)
303                         {
304                                 LOG_TRACEF("Cannot initialize sound %s", this.noise);
305                                 strunzone(this.noise);
306                                 this.noise = string_null;
307                         }
308                 }
309         }
310
311         setorigin(this, this.origin);
312         setsize(this, this.mins, this.maxs);
313         this.cnt = 1;
314         setthink(this, Ent_TriggerMusic_Think);
315         this.nextthink = time;
316         return true;
317 }
318
319 #endif