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 WriteCoord(MSG_ENTITY, this.origin.x);
107 WriteCoord(MSG_ENTITY, this.origin.y);
108 WriteCoord(MSG_ENTITY, this.origin.z);
112 if(this.model != "null")
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);
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);
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);
136 void trigger_music_reset(entity this)
138 this.cnt = !(this.spawnflags & 1);
139 this.SendFlags |= 0x80;
141 void trigger_music_use(entity this, entity actor, entity trigger)
143 this.cnt = !this.cnt;
144 this.SendFlags |= 0x80;
146 spawnfunc(trigger_music)
148 if(this.model != "") _setmodel(this, this.model);
149 if(!this.volume) this.volume = 1;
152 setorigin(this, this.origin + this.mins);
153 setsize(this, '0 0 0', this.maxs - this.mins);
155 trigger_music_reset(this);
157 this.use = trigger_music_use;
158 this.reset = trigger_music_reset;
160 Net_LinkEntity(this, false, 0, trigger_music_SendEntity);
164 entity TargetMusic_list;
165 STATIC_INIT(TargetMusic_list)
167 TargetMusic_list = LL_NEW();
170 void TargetMusic_Advance()
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;
181 it.state = (it.fade_time > 0) ? bound(0, it.state + frametime / it.fade_time, 1) : 1;
186 it.state = (it.fade_rate > 0) ? bound(0, it.state - frametime / it.fade_rate, 1) : 0;
188 const float vol = it.state * it.volume * autocvar_bgmvolume;
192 _sound(it, CH_BGM_SINGLE, it.noise, vol, ATTEN_NONE); // restart
194 _sound(it, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
198 music_trigger = NULL;
199 bgmtime = (best) ? getsoundtime(best, CH_BGM_SINGLE) : gettime(GETTIME_CDTRACK);
202 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
208 void Net_TargetMusic()
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();
218 LL_EACH(TargetMusic_list, it.count == id, { e = it; break; });
221 LL_PUSH(TargetMusic_list, e = new_pure(TargetMusic));
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)
233 LOG_TRACEF("Cannot initialize sound %s", e.noise);
235 e.noise = string_null;
249 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
250 localcmd("cd stop\n"); // just in case
257 e.lifetime = time + tim;
262 void Ent_TriggerMusic_Think(entity this)
264 if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, this, NULL))
266 music_trigger = this;
268 this.nextthink = time;
271 void Ent_TriggerMusic_Remove(entity this)
274 strunzone(this.noise);
275 this.noise = string_null;
278 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
283 this.origin_x = ReadCoord();
284 this.origin_y = ReadCoord();
285 this.origin_z = ReadCoord();
289 this.modelindex = ReadShort();
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();
302 this.maxs_x = ReadCoord();
303 this.maxs_y = ReadCoord();
304 this.maxs_z = ReadCoord();
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;
312 strunzone(this.noise);
313 this.noise = strzone(ReadString());
316 precache_sound(this.noise);
317 _sound(this, CH_BGM_SINGLE, this.noise, 0, ATTEN_NONE);
318 if(getsoundtime(this, CH_BGM_SINGLE) < 0)
320 LOG_TRACEF("Cannot initialize sound %s", this.noise);
321 strunzone(this.noise);
322 this.noise = string_null;
327 setorigin(this, this.origin);
328 setsize(this, this.mins, this.maxs);
330 setthink(this, Ent_TriggerMusic_Think);
331 this.nextthink = time;