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 // when triggered, it is disabled/enabled for everyone
103 bool trigger_music_SendEntity(entity this, entity to, int sendflags)
105 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_MUSIC);
109 WriteByte(MSG_ENTITY, sendflags);
110 if(sendflags & SF_MUSIC_ORIGIN)
112 WriteVector(MSG_ENTITY, this.origin);
114 if(sendflags & SF_TRIGGER_INIT)
116 if(this.model != "null")
118 WriteShort(MSG_ENTITY, this.modelindex);
119 WriteVector(MSG_ENTITY, this.mins);
120 WriteVector(MSG_ENTITY, this.maxs);
124 WriteShort(MSG_ENTITY, 0);
125 WriteVector(MSG_ENTITY, this.maxs);
127 WriteByte(MSG_ENTITY, this.volume * 255.0);
128 WriteByte(MSG_ENTITY, this.fade_time * 16.0);
129 WriteByte(MSG_ENTITY, this.fade_rate * 16.0);
130 WriteString(MSG_ENTITY, this.noise);
134 void trigger_music_reset(entity this)
136 this.cnt = !(this.spawnflags & START_DISABLED);
137 this.SendFlags |= 0x80;
139 void trigger_music_use(entity this, entity actor, entity trigger)
141 this.cnt = !this.cnt;
142 this.SendFlags |= 0x80;
144 spawnfunc(trigger_music)
148 _setmodel(this, this.model);
156 setorigin(this, this.origin + this.mins);
157 setsize(this, '0 0 0', this.maxs - this.mins);
159 trigger_music_reset(this);
161 this.use = trigger_music_use;
162 this.reset = trigger_music_reset;
164 Net_LinkEntity(this, false, 0, trigger_music_SendEntity);
168 entity TargetMusic_list;
169 STATIC_INIT(TargetMusic_list)
171 TargetMusic_list = LL_NEW();
174 void TargetMusic_Advance()
176 // run AFTER all the thinks!
177 entity best = music_default;
178 if (music_target && time < music_target.lifetime)
184 best = music_trigger;
186 LL_EACH(TargetMusic_list, it.noise, {
187 const float vol0 = (getsoundtime(it, CH_BGM_SINGLE) >= 0) ? it.lastvol : -1;
191 it.state = (it.fade_time > 0) ? bound(0, it.state + frametime / it.fade_time, 1) : 1;
196 it.state = (it.fade_rate > 0) ? bound(0, it.state - frametime / it.fade_rate, 1) : 0;
198 const float vol = it.state * it.volume * autocvar_bgmvolume;
202 sound7(it, CH_BGM_SINGLE, it.noise, vol, ATTEN_NONE, 0, BIT(4)); // restart
204 sound7(it, CH_BGM_SINGLE, "", vol, ATTEN_NONE, 0, BIT(4));
208 music_trigger = NULL;
209 bgmtime = (best) ? getsoundtime(best, CH_BGM_SINGLE) : gettime(GETTIME_CDTRACK);
212 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
218 void Net_TargetMusic()
220 const int id = ReadShort();
221 const float vol = ReadByte() / 255.0;
222 const float fai = ReadByte() / 16.0;
223 const float fao = ReadByte() / 16.0;
224 const float tim = ReadByte();
225 const string noi = ReadString();
228 LL_EACH(TargetMusic_list, it.count == id, { e = it; break; });
231 LL_PUSH(TargetMusic_list, e = new_pure(TargetMusic));
238 e.noise = strzone(noi);
239 precache_sound(e.noise);
240 _sound(e, CH_BGM_SINGLE, e.noise, 0, ATTEN_NONE);
241 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
243 LOG_TRACEF("Cannot initialize sound %s", e.noise);
245 e.noise = string_null;
259 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
260 localcmd("cd stop\n"); // just in case
267 e.lifetime = time + tim;
272 void Ent_TriggerMusic_Think(entity this)
274 vector org = (csqcplayer) ? csqcplayer.origin : view_origin;
275 if(WarpZoneLib_BoxTouchesBrush(org + STAT(PL_MIN), org + STAT(PL_MAX), this, NULL))
277 music_trigger = this;
281 void Ent_TriggerMusic_Remove(entity this)
284 strunzone(this.noise);
285 this.noise = string_null;
288 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
290 int sendflags = ReadByte();
291 if(sendflags & SF_MUSIC_ORIGIN)
293 this.origin = ReadVector();
295 if(sendflags & SF_TRIGGER_INIT)
297 this.modelindex = ReadShort();
300 this.mins = ReadVector();
301 this.maxs = ReadVector();
306 this.maxs = ReadVector();
309 this.volume = ReadByte() / 255.0;
310 this.fade_time = ReadByte() / 16.0;
311 this.fade_rate = ReadByte() / 16.0;
312 string s = this.noise;
314 strunzone(this.noise);
315 this.noise = strzone(ReadString());
318 precache_sound(this.noise);
319 sound7(this, CH_BGM_SINGLE, this.noise, 0, ATTEN_NONE, 0, BIT(4));
320 if(getsoundtime(this, CH_BGM_SINGLE) < 0)
322 LOG_WARNF("Cannot initialize sound %s", this.noise);
323 strunzone(this.noise);
324 this.noise = string_null;
329 setorigin(this, this.origin);
330 setsize(this, this.mins, this.maxs);
332 this.draw = Ent_TriggerMusic_Think;
335 LL_PUSH(TargetMusic_list, this);
336 IL_PUSH(g_drawables, this);