4 #include <common/constants.qh>
5 #include <server/constants.qh>
6 #include <server/defs.qh>
9 REGISTER_NET_TEMP(TE_CSQC_TARGET_MUSIC)
10 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_MUSIC)
21 // when triggered, the music is overridden for activator until lifetime (or forever, if lifetime is 0)
22 // when targetname is not set, THIS ONE is default
23 void target_music_sendto(float to, float is)
25 WriteHeader(to, TE_CSQC_TARGET_MUSIC);
26 WriteShort(to, etof(self));
27 WriteByte(to, self.volume * 255.0 * is);
28 WriteByte(to, self.fade_time * 16.0);
29 WriteByte(to, self.fade_rate * 16.0);
30 WriteByte(to, self.lifetime);
31 WriteString(to, self.noise);
33 void target_music_reset(entity this)
35 if (this.targetname == "") target_music_sendto(MSG_ALL, 1);
37 void target_music_kill()
39 FOREACH_ENTITY_CLASS("target_music", true, {
41 if (it.targetname == "")
42 WITHSELF(it, target_music_sendto(MSG_ALL, 1));
44 WITHSELF(it, target_music_sendto(MSG_ALL, 0));
47 void target_music_use(entity this, entity actor, entity trigger)
51 if(IS_REAL_CLIENT(actor))
54 target_music_sendto(MSG_ONE, 1);
56 FOREACH_CLIENT(IS_SPEC(it) && it.enemy == actor, {
58 target_music_sendto(MSG_ONE, 1);
61 spawnfunc(target_music)
63 self.use = target_music_use;
64 self.reset = target_music_reset;
67 if(self.targetname == "")
68 target_music_sendto(MSG_INIT, 1);
70 target_music_sendto(MSG_INIT, 0);
72 void TargetMusic_RestoreGame()
74 for(entity e = world; (e = find(e, classname, "target_music")); )
77 if(self.targetname == "")
78 target_music_sendto(MSG_INIT, 1);
80 target_music_sendto(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, self.origin.x);
101 WriteCoord(MSG_ENTITY, self.origin.y);
102 WriteCoord(MSG_ENTITY, self.origin.z);
106 if(self.model != "null")
108 WriteShort(MSG_ENTITY, self.modelindex);
109 WriteCoord(MSG_ENTITY, self.mins.x);
110 WriteCoord(MSG_ENTITY, self.mins.y);
111 WriteCoord(MSG_ENTITY, self.mins.z);
112 WriteCoord(MSG_ENTITY, self.maxs.x);
113 WriteCoord(MSG_ENTITY, self.maxs.y);
114 WriteCoord(MSG_ENTITY, self.maxs.z);
118 WriteShort(MSG_ENTITY, 0);
119 WriteCoord(MSG_ENTITY, self.maxs.x);
120 WriteCoord(MSG_ENTITY, self.maxs.y);
121 WriteCoord(MSG_ENTITY, self.maxs.z);
123 WriteByte(MSG_ENTITY, self.volume * 255.0);
124 WriteByte(MSG_ENTITY, self.fade_time * 16.0);
125 WriteByte(MSG_ENTITY, self.fade_rate * 16.0);
126 WriteString(MSG_ENTITY, self.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 = world;
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\n", 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()
258 if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, self, world))
260 music_trigger = self;
262 self.nextthink = time;
265 void Ent_TriggerMusic_Remove()
268 strunzone(self.noise);
269 self.noise = string_null;
272 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
277 self.origin_x = ReadCoord();
278 self.origin_y = ReadCoord();
279 self.origin_z = ReadCoord();
283 self.modelindex = ReadShort();
286 self.mins_x = ReadCoord();
287 self.mins_y = ReadCoord();
288 self.mins_z = ReadCoord();
289 self.maxs_x = ReadCoord();
290 self.maxs_y = ReadCoord();
291 self.maxs_z = ReadCoord();
296 self.maxs_x = ReadCoord();
297 self.maxs_y = ReadCoord();
298 self.maxs_z = ReadCoord();
301 self.volume = ReadByte() / 255.0;
302 self.fade_time = ReadByte() / 16.0;
303 self.fade_rate = ReadByte() / 16.0;
304 string s = self.noise;
306 strunzone(self.noise);
307 self.noise = strzone(ReadString());
310 precache_sound(self.noise);
311 _sound(self, CH_BGM_SINGLE, self.noise, 0, ATTEN_NONE);
312 if(getsoundtime(self, CH_BGM_SINGLE) < 0)
314 LOG_TRACEF("Cannot initialize sound %s\n", self.noise);
315 strunzone(self.noise);
316 self.noise = string_null;
321 setorigin(self, self.origin);
322 setsize(self, self.mins, self.maxs);
324 self.think = Ent_TriggerMusic_Think;
325 self.nextthink = time;