5 #include <common/constants.qh>
6 #include <server/constants.qh>
7 #include <server/defs.qh>
10 REGISTER_NET_TEMP(TE_CSQC_TARGET_MUSIC)
11 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_MUSIC)
15 IntrusiveList g_targetmusic_list;
16 STATIC_INIT(g_targetmusic_list) { g_targetmusic_list = IL_NEW(); }
25 // when triggered, the music is overridden for activator until lifetime (or forever, if lifetime is 0)
26 // when targetname is not set, THIS ONE is default
27 void target_music_sendto(entity this, int to, bool is)
29 WriteHeader(to, TE_CSQC_TARGET_MUSIC);
30 WriteShort(to, etof(this));
31 WriteByte(to, this.volume * 255.0 * is);
32 WriteByte(to, this.fade_time * 16.0);
33 WriteByte(to, this.fade_rate * 16.0);
34 WriteByte(to, this.lifetime);
35 WriteString(to, this.noise);
37 void target_music_reset(entity this)
39 if (this.targetname == "") target_music_sendto(this, MSG_ALL, 1);
41 void target_music_kill()
43 IL_EACH(g_targetmusic_list, true,
46 if (it.targetname == "")
47 target_music_sendto(it, MSG_ALL, 1);
49 target_music_sendto(it, MSG_ALL, 0);
52 void target_music_use(entity this, entity actor, entity trigger)
56 if(IS_REAL_CLIENT(actor))
59 target_music_sendto(this, MSG_ONE, 1);
61 FOREACH_CLIENT(IS_SPEC(it) && it.enemy == actor, {
63 target_music_sendto(this, MSG_ONE, 1);
66 spawnfunc(target_music)
68 this.use = target_music_use;
69 this.reset = target_music_reset;
72 IL_PUSH(g_targetmusic_list, this);
73 if(this.targetname == "")
74 target_music_sendto(this, MSG_INIT, 1);
76 target_music_sendto(this, MSG_INIT, 0);
78 void TargetMusic_RestoreGame()
80 IL_EACH(g_targetmusic_list, true,
82 if(it.targetname == "")
83 target_music_sendto(it, MSG_INIT, 1);
85 target_music_sendto(it, MSG_INIT, 0);
95 // when triggered, it is disabled/enabled for everyone
96 bool trigger_music_SendEntity(entity this, entity to, float sf)
98 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_MUSIC);
102 WriteByte(MSG_ENTITY, sf);
105 WriteCoord(MSG_ENTITY, this.origin.x);
106 WriteCoord(MSG_ENTITY, this.origin.y);
107 WriteCoord(MSG_ENTITY, this.origin.z);
111 if(this.model != "null")
113 WriteShort(MSG_ENTITY, this.modelindex);
114 WriteCoord(MSG_ENTITY, this.mins.x);
115 WriteCoord(MSG_ENTITY, this.mins.y);
116 WriteCoord(MSG_ENTITY, this.mins.z);
117 WriteCoord(MSG_ENTITY, this.maxs.x);
118 WriteCoord(MSG_ENTITY, this.maxs.y);
119 WriteCoord(MSG_ENTITY, this.maxs.z);
123 WriteShort(MSG_ENTITY, 0);
124 WriteCoord(MSG_ENTITY, this.maxs.x);
125 WriteCoord(MSG_ENTITY, this.maxs.y);
126 WriteCoord(MSG_ENTITY, this.maxs.z);
128 WriteByte(MSG_ENTITY, this.volume * 255.0);
129 WriteByte(MSG_ENTITY, this.fade_time * 16.0);
130 WriteByte(MSG_ENTITY, this.fade_rate * 16.0);
131 WriteString(MSG_ENTITY, this.noise);
135 void trigger_music_reset(entity this)
137 this.cnt = !(this.spawnflags & 1);
138 this.SendFlags |= 0x80;
140 void trigger_music_use(entity this, entity actor, entity trigger)
142 this.cnt = !this.cnt;
143 this.SendFlags |= 0x80;
145 spawnfunc(trigger_music)
147 if(this.model != "") _setmodel(this, this.model);
148 if(!this.volume) this.volume = 1;
151 setorigin(this, this.origin + this.mins);
152 setsize(this, '0 0 0', this.maxs - this.mins);
154 trigger_music_reset(this);
156 this.use = trigger_music_use;
157 this.reset = trigger_music_reset;
159 Net_LinkEntity(this, false, 0, trigger_music_SendEntity);
163 entity TargetMusic_list;
164 STATIC_INIT(TargetMusic_list)
166 TargetMusic_list = LL_NEW();
169 void TargetMusic_Advance()
171 // run AFTER all the thinks!
172 entity best = music_default;
173 if (music_target && time < music_target.lifetime) best = music_target;
174 if (music_trigger) best = music_trigger;
175 LL_EACH(TargetMusic_list, it.noise, {
176 const float vol0 = (getsoundtime(it, CH_BGM_SINGLE) >= 0) ? it.lastvol : -1;
180 it.state = (it.fade_time > 0) ? bound(0, it.state + frametime / it.fade_time, 1) : 1;
185 it.state = (it.fade_rate > 0) ? bound(0, it.state - frametime / it.fade_rate, 1) : 0;
187 const float vol = it.state * it.volume * autocvar_bgmvolume;
191 _sound(it, CH_BGM_SINGLE, it.noise, vol, ATTEN_NONE); // restart
193 _sound(it, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
197 music_trigger = NULL;
198 bgmtime = (best) ? getsoundtime(best, CH_BGM_SINGLE) : gettime(GETTIME_CDTRACK);
201 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
207 void Net_TargetMusic()
209 const int id = ReadShort();
210 const float vol = ReadByte() / 255.0;
211 const float fai = ReadByte() / 16.0;
212 const float fao = ReadByte() / 16.0;
213 const float tim = ReadByte();
214 const string noi = ReadString();
217 LL_EACH(TargetMusic_list, it.count == id, { e = it; break; });
220 LL_PUSH(TargetMusic_list, e = new_pure(TargetMusic));
227 e.noise = strzone(noi);
228 precache_sound(e.noise);
229 _sound(e, CH_BGM_SINGLE, e.noise, 0, ATTEN_NONE);
230 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
232 LOG_TRACEF("Cannot initialize sound %s", e.noise);
234 e.noise = string_null;
248 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
249 localcmd("cd stop\n"); // just in case
256 e.lifetime = time + tim;
261 void Ent_TriggerMusic_Think(entity this)
263 if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, this, NULL))
265 music_trigger = this;
267 this.nextthink = time;
270 void Ent_TriggerMusic_Remove(entity this)
273 strunzone(this.noise);
274 this.noise = string_null;
277 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
282 this.origin_x = ReadCoord();
283 this.origin_y = ReadCoord();
284 this.origin_z = ReadCoord();
288 this.modelindex = ReadShort();
291 this.mins_x = ReadCoord();
292 this.mins_y = ReadCoord();
293 this.mins_z = ReadCoord();
294 this.maxs_x = ReadCoord();
295 this.maxs_y = ReadCoord();
296 this.maxs_z = ReadCoord();
301 this.maxs_x = ReadCoord();
302 this.maxs_y = ReadCoord();
303 this.maxs_z = ReadCoord();
306 this.volume = ReadByte() / 255.0;
307 this.fade_time = ReadByte() / 16.0;
308 this.fade_rate = ReadByte() / 16.0;
309 string s = this.noise;
311 strunzone(this.noise);
312 this.noise = strzone(ReadString());
315 precache_sound(this.noise);
316 _sound(this, CH_BGM_SINGLE, this.noise, 0, ATTEN_NONE);
317 if(getsoundtime(this, CH_BGM_SINGLE) < 0)
319 LOG_TRACEF("Cannot initialize sound %s", this.noise);
320 strunzone(this.noise);
321 this.noise = string_null;
326 setorigin(this, this.origin);
327 setsize(this, this.mins, this.maxs);
329 setthink(this, Ent_TriggerMusic_Think);
330 this.nextthink = time;