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)
22 // when triggered, the music is overridden for activator until lifetime (or forever, if lifetime is 0)
23 // when targetname is not set, THIS ONE is default
24 void target_music_sendto(entity this, int to, bool is)
26 WriteHeader(to, TE_CSQC_TARGET_MUSIC);
27 WriteShort(to, etof(this));
28 WriteByte(to, this.volume * 255.0 * is);
29 WriteByte(to, this.fade_time * 16.0);
30 WriteByte(to, this.fade_rate * 16.0);
31 WriteByte(to, this.lifetime);
32 WriteString(to, this.noise);
34 void target_music_reset(entity this)
36 if (this.targetname == "") target_music_sendto(this, MSG_ALL, 1);
38 void target_music_kill()
40 FOREACH_ENTITY_CLASS("target_music", true, {
42 if (it.targetname == "")
43 target_music_sendto(it, MSG_ALL, 1);
45 target_music_sendto(it, MSG_ALL, 0);
48 void target_music_use(entity this, entity actor, entity trigger)
52 if(IS_REAL_CLIENT(actor))
55 target_music_sendto(this, MSG_ONE, 1);
57 FOREACH_CLIENT(IS_SPEC(it) && it.enemy == actor, {
59 target_music_sendto(this, MSG_ONE, 1);
62 spawnfunc(target_music)
64 this.use = target_music_use;
65 this.reset = target_music_reset;
68 if(this.targetname == "")
69 target_music_sendto(this, MSG_INIT, 1);
71 target_music_sendto(this, MSG_INIT, 0);
73 void TargetMusic_RestoreGame()
75 FOREACH_ENTITY_CLASS("target_music", true,
77 if(it.targetname == "")
78 target_music_sendto(it, MSG_INIT, 1);
80 target_music_sendto(it, MSG_INIT, 0);
90 // when triggered, it is disabled/enabled for everyone
91 bool trigger_music_SendEntity(entity this, entity to, float sf)
93 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_MUSIC);
97 WriteByte(MSG_ENTITY, sf);
100 WriteCoord(MSG_ENTITY, this.origin.x);
101 WriteCoord(MSG_ENTITY, this.origin.y);
102 WriteCoord(MSG_ENTITY, this.origin.z);
106 if(this.model != "null")
108 WriteShort(MSG_ENTITY, this.modelindex);
109 WriteCoord(MSG_ENTITY, this.mins.x);
110 WriteCoord(MSG_ENTITY, this.mins.y);
111 WriteCoord(MSG_ENTITY, this.mins.z);
112 WriteCoord(MSG_ENTITY, this.maxs.x);
113 WriteCoord(MSG_ENTITY, this.maxs.y);
114 WriteCoord(MSG_ENTITY, this.maxs.z);
118 WriteShort(MSG_ENTITY, 0);
119 WriteCoord(MSG_ENTITY, this.maxs.x);
120 WriteCoord(MSG_ENTITY, this.maxs.y);
121 WriteCoord(MSG_ENTITY, this.maxs.z);
123 WriteByte(MSG_ENTITY, this.volume * 255.0);
124 WriteByte(MSG_ENTITY, this.fade_time * 16.0);
125 WriteByte(MSG_ENTITY, this.fade_rate * 16.0);
126 WriteString(MSG_ENTITY, this.noise);
130 void trigger_music_reset(entity this)
132 this.cnt = !(this.spawnflags & 1);
133 this.SendFlags |= 0x80;
135 void trigger_music_use(entity this, entity actor, entity trigger)
137 this.cnt = !this.cnt;
138 this.SendFlags |= 0x80;
140 spawnfunc(trigger_music)
142 if(this.model != "") _setmodel(this, this.model);
143 if(!this.volume) this.volume = 1;
146 setorigin(this, this.origin + this.mins);
147 setsize(this, '0 0 0', this.maxs - this.mins);
149 trigger_music_reset(this);
151 this.use = trigger_music_use;
152 this.reset = trigger_music_reset;
154 Net_LinkEntity(this, false, 0, trigger_music_SendEntity);
158 entity TargetMusic_list;
159 STATIC_INIT(TargetMusic_list)
161 TargetMusic_list = LL_NEW();
164 void TargetMusic_Advance()
166 // run AFTER all the thinks!
167 entity best = music_default;
168 if (music_target && time < music_target.lifetime) best = music_target;
169 if (music_trigger) best = music_trigger;
170 LL_EACH(TargetMusic_list, it.noise, {
171 const float vol0 = (getsoundtime(it, CH_BGM_SINGLE) >= 0) ? it.lastvol : -1;
175 it.state = (it.fade_time > 0) ? bound(0, it.state + frametime / it.fade_time, 1) : 1;
180 it.state = (it.fade_rate > 0) ? bound(0, it.state - frametime / it.fade_rate, 1) : 0;
182 const float vol = it.state * it.volume * autocvar_bgmvolume;
186 _sound(it, CH_BGM_SINGLE, it.noise, vol, ATTEN_NONE); // restart
188 _sound(it, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
192 music_trigger = NULL;
193 bgmtime = (best) ? getsoundtime(best, CH_BGM_SINGLE) : gettime(GETTIME_CDTRACK);
196 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
202 void Net_TargetMusic()
204 const int id = ReadShort();
205 const float vol = ReadByte() / 255.0;
206 const float fai = ReadByte() / 16.0;
207 const float fao = ReadByte() / 16.0;
208 const float tim = ReadByte();
209 const string noi = ReadString();
212 LL_EACH(TargetMusic_list, it.count == id, { e = it; break; });
215 LL_PUSH(TargetMusic_list, e = new_pure(TargetMusic));
222 e.noise = strzone(noi);
223 precache_sound(e.noise);
224 _sound(e, CH_BGM_SINGLE, e.noise, 0, ATTEN_NONE);
225 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
227 LOG_TRACEF("Cannot initialize sound %s", e.noise);
229 e.noise = string_null;
243 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
244 localcmd("cd stop\n"); // just in case
251 e.lifetime = time + tim;
256 void Ent_TriggerMusic_Think(entity this)
258 if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, this, NULL))
260 music_trigger = this;
262 this.nextthink = time;
265 void Ent_TriggerMusic_Remove(entity this)
268 strunzone(this.noise);
269 this.noise = string_null;
272 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
277 this.origin_x = ReadCoord();
278 this.origin_y = ReadCoord();
279 this.origin_z = ReadCoord();
283 this.modelindex = ReadShort();
286 this.mins_x = ReadCoord();
287 this.mins_y = ReadCoord();
288 this.mins_z = ReadCoord();
289 this.maxs_x = ReadCoord();
290 this.maxs_y = ReadCoord();
291 this.maxs_z = ReadCoord();
296 this.maxs_x = ReadCoord();
297 this.maxs_y = ReadCoord();
298 this.maxs_z = ReadCoord();
301 this.volume = ReadByte() / 255.0;
302 this.fade_time = ReadByte() / 16.0;
303 this.fade_rate = ReadByte() / 16.0;
304 string s = this.noise;
306 strunzone(this.noise);
307 this.noise = strzone(ReadString());
310 precache_sound(this.noise);
311 _sound(this, CH_BGM_SINGLE, this.noise, 0, ATTEN_NONE);
312 if(getsoundtime(this, CH_BGM_SINGLE) < 0)
314 LOG_TRACEF("Cannot initialize sound %s", this.noise);
315 strunzone(this.noise);
316 this.noise = string_null;
321 setorigin(this, this.origin);
322 setsize(this, this.mins, this.maxs);
324 setthink(this, Ent_TriggerMusic_Think);
325 this.nextthink = time;