5 #include <common/constants.qh>
6 #include <common/net_linked.qh>
7 #include <server/constants.qh>
8 #include <server/defs.qh>
11 REGISTER_NET_TEMP(TE_CSQC_TARGET_MUSIC)
12 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_MUSIC)
16 IntrusiveList g_targetmusic_list;
17 STATIC_INIT(g_targetmusic_list)
19 g_targetmusic_list = IL_NEW();
29 // when triggered, the music is overridden for activator until lifetime (or forever, if lifetime is 0)
30 // when targetname is not set, THIS ONE is default
31 void target_music_sendto(entity this, int to, bool is)
33 WriteHeader(to, TE_CSQC_TARGET_MUSIC);
34 WriteShort(to, etof(this));
35 WriteByte(to, this.volume * 255.0 * is);
36 WriteByte(to, this.fade_time * 16.0);
37 WriteByte(to, this.fade_rate * 16.0);
38 WriteByte(to, this.lifetime);
39 WriteString(to, this.noise);
41 void target_music_reset(entity this)
43 if (this.targetname == "")
45 target_music_sendto(this, MSG_ALL, true);
48 void target_music_kill()
50 IL_EACH(g_targetmusic_list, true,
53 if (it.targetname == "")
54 target_music_sendto(it, MSG_ALL, true);
56 target_music_sendto(it, MSG_ALL, false);
59 void target_music_use(entity this, entity actor, entity trigger)
63 if(IS_REAL_CLIENT(actor))
66 target_music_sendto(this, MSG_ONE, true);
68 FOREACH_CLIENT(IS_SPEC(it) && it.enemy == actor, {
70 target_music_sendto(this, MSG_ONE, true);
73 spawnfunc(target_music)
75 this.use = target_music_use;
76 this.reset = target_music_reset;
79 IL_PUSH(g_targetmusic_list, this);
80 if(this.targetname == "")
81 target_music_sendto(this, MSG_INIT, true);
83 target_music_sendto(this, MSG_INIT, false);
85 void TargetMusic_RestoreGame()
87 IL_EACH(g_targetmusic_list, true,
89 if(it.targetname == "")
90 target_music_sendto(it, MSG_INIT, true);
92 target_music_sendto(it, MSG_INIT, false);
102 // can be disabled/enabled for everyone with relays
103 bool trigger_music_SendEntity(entity this, entity to, int sendflags)
105 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_MUSIC);
106 WriteByte(MSG_ENTITY, sendflags);
107 if(sendflags & SF_MUSIC_ORIGIN)
109 WriteVector(MSG_ENTITY, this.origin);
111 if(sendflags & SF_TRIGGER_INIT)
113 if(this.model != "null")
115 WriteShort(MSG_ENTITY, this.modelindex);
116 WriteVector(MSG_ENTITY, this.mins);
117 WriteVector(MSG_ENTITY, this.maxs);
121 WriteShort(MSG_ENTITY, 0);
122 WriteVector(MSG_ENTITY, this.maxs);
124 WriteByte(MSG_ENTITY, this.volume * 255.0);
125 WriteByte(MSG_ENTITY, this.fade_time * 16.0);
126 WriteByte(MSG_ENTITY, this.fade_rate * 16.0);
127 WriteString(MSG_ENTITY, this.noise);
129 if(sendflags & SF_TRIGGER_UPDATE)
131 WriteByte(MSG_ENTITY, this.active);
135 void trigger_music_reset(entity this)
137 if(this.spawnflags & START_DISABLED)
139 this.setactive(this, ACTIVE_NOT);
143 this.setactive(this, ACTIVE_ACTIVE);
147 spawnfunc(trigger_music)
151 _setmodel(this, this.model);
159 setorigin(this, this.origin + this.mins);
160 setsize(this, '0 0 0', this.maxs - this.mins);
163 this.setactive = generic_netlinked_setactive;
164 this.use = generic_netlinked_legacy_use; // backwards compatibility
165 this.reset = trigger_music_reset;
168 Net_LinkEntity(this, false, 0, trigger_music_SendEntity);
172 entity TargetMusic_list;
173 STATIC_INIT(TargetMusic_list)
175 TargetMusic_list = LL_NEW();
178 void TargetMusic_Advance()
180 // run AFTER all the thinks!
181 entity best = music_default;
182 if (music_target && time < music_target.lifetime)
188 best = music_trigger;
190 LL_EACH(TargetMusic_list, it.noise, {
191 const float vol0 = (getsoundtime(it, CH_BGM_SINGLE) >= 0) ? it.lastvol : -1;
195 it.state = (it.fade_time > 0) ? bound(0, it.state + frametime / it.fade_time, 1) : 1;
200 it.state = (it.fade_rate > 0) ? bound(0, it.state - frametime / it.fade_rate, 1) : 0;
202 const float vol = it.state * it.volume * autocvar_bgmvolume;
206 sound7(it, CH_BGM_SINGLE, it.noise, vol, ATTEN_NONE, 0, BIT(4)); // restart
208 sound7(it, CH_BGM_SINGLE, "", vol, ATTEN_NONE, 0, BIT(4));
212 music_trigger = NULL;
213 bgmtime = (best) ? getsoundtime(best, CH_BGM_SINGLE) : gettime(GETTIME_CDTRACK);
216 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
222 void Net_TargetMusic()
224 const int id = ReadShort();
225 const float vol = ReadByte() / 255.0;
226 const float fai = ReadByte() / 16.0;
227 const float fao = ReadByte() / 16.0;
228 const float tim = ReadByte();
229 const string noi = ReadString();
232 LL_EACH(TargetMusic_list, it.count == id, { e = it; break; });
235 LL_PUSH(TargetMusic_list, e = new_pure(TargetMusic));
240 strcpy(e.noise, noi);
241 precache_sound(e.noise);
242 _sound(e, CH_BGM_SINGLE, e.noise, 0, ATTEN_NONE);
243 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
245 LOG_TRACEF("Cannot initialize sound %s", e.noise);
260 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
261 localcmd("cd stop\n"); // just in case
268 e.lifetime = time + tim;
273 void Ent_TriggerMusic_Think(entity this)
275 if(this.active == ACTIVE_NOT)
279 vector org = (csqcplayer) ? csqcplayer.origin : view_origin;
280 if(WarpZoneLib_BoxTouchesBrush(org + STAT(PL_MIN), org + STAT(PL_MAX), this, NULL))
282 music_trigger = this;
286 void Ent_TriggerMusic_Remove(entity this)
291 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
293 int sendflags = ReadByte();
294 if(sendflags & SF_MUSIC_ORIGIN)
296 this.origin = ReadVector();
298 if(sendflags & SF_TRIGGER_INIT)
300 this.modelindex = ReadShort();
303 this.mins = ReadVector();
304 this.maxs = ReadVector();
309 this.maxs = ReadVector();
312 this.volume = ReadByte() / 255.0;
313 this.fade_time = ReadByte() / 16.0;
314 this.fade_rate = ReadByte() / 16.0;
315 string s = this.noise;
316 strcpy(this.noise, ReadString());
319 precache_sound(this.noise);
320 sound7(this, CH_BGM_SINGLE, this.noise, 0, ATTEN_NONE, 0, BIT(4));
321 if(getsoundtime(this, CH_BGM_SINGLE) < 0)
323 LOG_WARNF("Cannot initialize sound %s", this.noise);
328 if(sendflags & SF_TRIGGER_UPDATE)
330 this.active = ReadByte();
333 setorigin(this, this.origin);
334 setsize(this, this.mins, this.maxs);
335 this.draw = Ent_TriggerMusic_Think;
338 LL_PUSH(TargetMusic_list, this);
339 IL_PUSH(g_drawables, this);