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) { g_targetmusic_list = IL_NEW(); }
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)
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);
38 void target_music_reset(entity this)
40 if (this.targetname == "") target_music_sendto(this, MSG_ALL, 1);
42 void target_music_kill()
44 IL_EACH(g_targetmusic_list, true,
47 if (it.targetname == "")
48 target_music_sendto(it, MSG_ALL, 1);
50 target_music_sendto(it, MSG_ALL, 0);
53 void target_music_use(entity this, entity actor, entity trigger)
57 if(IS_REAL_CLIENT(actor))
60 target_music_sendto(this, MSG_ONE, 1);
62 FOREACH_CLIENT(IS_SPEC(it) && it.enemy == actor, {
64 target_music_sendto(this, MSG_ONE, 1);
67 spawnfunc(target_music)
69 this.use = target_music_use;
70 this.reset = target_music_reset;
73 IL_PUSH(g_targetmusic_list, this);
74 if(this.targetname == "")
75 target_music_sendto(this, MSG_INIT, 1);
77 target_music_sendto(this, MSG_INIT, 0);
79 void TargetMusic_RestoreGame()
81 IL_EACH(g_targetmusic_list, true,
83 if(it.targetname == "")
84 target_music_sendto(it, MSG_INIT, 1);
86 target_music_sendto(it, MSG_INIT, 0);
96 // when triggered, it is disabled/enabled for everyone
97 bool trigger_music_SendEntity(entity this, entity to, float sf)
99 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_MUSIC);
103 WriteByte(MSG_ENTITY, sf);
106 WriteVector(MSG_ENTITY, this.origin);
110 if(this.model != "null")
112 WriteShort(MSG_ENTITY, this.modelindex);
113 WriteVector(MSG_ENTITY, this.mins);
114 WriteVector(MSG_ENTITY, this.maxs);
118 WriteShort(MSG_ENTITY, 0);
119 WriteVector(MSG_ENTITY, this.maxs);
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);
128 void trigger_music_reset(entity this)
130 this.cnt = !(this.spawnflags & 1);
131 this.SendFlags |= 0x80;
133 void trigger_music_use(entity this, entity actor, entity trigger)
135 this.cnt = !this.cnt;
136 this.SendFlags |= 0x80;
138 spawnfunc(trigger_music)
140 if(this.model != "") _setmodel(this, this.model);
141 if(!this.volume) this.volume = 1;
144 setorigin(this, this.origin + this.mins);
145 setsize(this, '0 0 0', this.maxs - this.mins);
147 trigger_music_reset(this);
149 this.use = trigger_music_use;
150 this.reset = trigger_music_reset;
152 Net_LinkEntity(this, false, 0, trigger_music_SendEntity);
156 entity TargetMusic_list;
157 STATIC_INIT(TargetMusic_list)
159 TargetMusic_list = LL_NEW();
162 void TargetMusic_Advance()
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;
173 it.state = (it.fade_time > 0) ? bound(0, it.state + frametime / it.fade_time, 1) : 1;
178 it.state = (it.fade_rate > 0) ? bound(0, it.state - frametime / it.fade_rate, 1) : 0;
180 const float vol = it.state * it.volume * autocvar_bgmvolume;
184 _sound(it, CH_BGM_SINGLE, it.noise, vol, ATTEN_NONE); // restart
186 _sound(it, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
190 music_trigger = NULL;
191 bgmtime = (best) ? getsoundtime(best, CH_BGM_SINGLE) : gettime(GETTIME_CDTRACK);
194 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
200 void Net_TargetMusic()
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();
210 LL_EACH(TargetMusic_list, it.count == id, { e = it; break; });
213 LL_PUSH(TargetMusic_list, e = new_pure(TargetMusic));
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)
225 LOG_TRACEF("Cannot initialize sound %s", e.noise);
227 e.noise = string_null;
241 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
242 localcmd("cd stop\n"); // just in case
249 e.lifetime = time + tim;
254 void Ent_TriggerMusic_Think(entity this)
256 if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, this, NULL))
258 music_trigger = this;
260 this.nextthink = time;
263 void Ent_TriggerMusic_Remove(entity this)
266 strunzone(this.noise);
267 this.noise = string_null;
270 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
275 this.origin = ReadVector();
279 this.modelindex = ReadShort();
282 this.mins = ReadVector();
283 this.maxs = ReadVector();
288 this.maxs = ReadVector();
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;
296 strunzone(this.noise);
297 this.noise = strzone(ReadString());
300 precache_sound(this.noise);
301 _sound(this, CH_BGM_SINGLE, this.noise, 0, ATTEN_NONE);
302 if(getsoundtime(this, CH_BGM_SINGLE) < 0)
304 LOG_TRACEF("Cannot initialize sound %s", this.noise);
305 strunzone(this.noise);
306 this.noise = string_null;
311 setorigin(this, this.origin);
312 setsize(this, this.mins, this.maxs);
314 setthink(this, Ent_TriggerMusic_Think);
315 this.nextthink = time;