4 #include "../../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, num_for_edict(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()
35 if(self.targetname == "")
36 target_music_sendto(MSG_ALL, 1);
38 void target_music_kill()
40 for(self = world; (self = find(self, classname, "target_music")); )
43 if(self.targetname == "")
44 target_music_sendto(MSG_ALL, 1);
46 target_music_sendto(MSG_ALL, 0);
49 void target_music_use()
53 if(IS_REAL_CLIENT(activator))
55 msg_entity = activator;
56 target_music_sendto(MSG_ONE, 1);
59 FOR_EACH_SPEC(head) if(head.enemy == activator) { msg_entity = head; 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()
132 self.cnt = !(self.spawnflags & 1);
133 self.SendFlags |= 0x80;
135 void trigger_music_use()
137 self.cnt = !self.cnt;
138 self.SendFlags |= 0x80;
140 spawnfunc(trigger_music)
143 _setmodel(self, self.model);
148 setorigin(self, self.origin + self.mins);
149 setsize(self, '0 0 0', self.maxs - self.mins);
151 trigger_music_reset();
153 self.use = trigger_music_use;
154 self.reset = trigger_music_reset;
156 Net_LinkEntity(self, false, 0, trigger_music_SendEntity);
160 void TargetMusic_Advance()
162 // run AFTER all the thinks!
165 best = music_default;
166 if(music_target && time < music_target.lifetime)
169 best = music_trigger;
170 for(e = world; (e = findfloat(e, enttype, NET_ENT_CLIENT_TRIGGER_MUSIC.m_id)); ) if(e.noise)
173 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
181 e.state = bound(0, e.state + frametime / e.fade_time, 1);
189 e.state = bound(0, e.state - frametime / e.fade_rate, 1);
193 vol = e.state * e.volume * autocvar_bgmvolume;
197 _sound(e, CH_BGM_SINGLE, e.noise, vol, ATTEN_NONE); // restart
199 _sound(e, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
203 music_trigger = world;
206 bgmtime = getsoundtime(best, CH_BGM_SINGLE);
208 bgmtime = gettime(GETTIME_CDTRACK);
211 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
217 void Net_TargetMusic()
219 int id = ReadShort();
220 float vol = ReadByte() / 255.0;
221 float fai = ReadByte() / 16.0;
222 float fao = ReadByte() / 16.0;
223 float tim = ReadByte();
224 string noi = ReadString();
227 for(e = world; (e = findfloat(e, enttype, NET_ENT_CLIENT_TRIGGER_MUSIC.m_id)); )
235 e.enttype = NET_ENT_CLIENT_TRIGGER_MUSIC.m_id;
242 e.noise = strzone(noi);
243 precache_sound(e.noise);
244 _sound(e, CH_BGM_SINGLE, e.noise, 0, ATTEN_NONE);
245 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
247 LOG_TRACEF("Cannot initialize sound %s\n", e.noise);
249 e.noise = string_null;
263 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
264 localcmd("cd stop\n"); // just in case
271 e.lifetime = time + tim;
276 void Ent_TriggerMusic_Think()
278 if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, self, world))
280 music_trigger = self;
282 self.nextthink = time;
285 void Ent_TriggerMusic_Remove()
288 strunzone(self.noise);
289 self.noise = string_null;
292 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
297 self.origin_x = ReadCoord();
298 self.origin_y = ReadCoord();
299 self.origin_z = ReadCoord();
303 self.modelindex = ReadShort();
306 self.mins_x = ReadCoord();
307 self.mins_y = ReadCoord();
308 self.mins_z = ReadCoord();
309 self.maxs_x = ReadCoord();
310 self.maxs_y = ReadCoord();
311 self.maxs_z = ReadCoord();
316 self.maxs_x = ReadCoord();
317 self.maxs_y = ReadCoord();
318 self.maxs_z = ReadCoord();
321 self.volume = ReadByte() / 255.0;
322 self.fade_time = ReadByte() / 16.0;
323 self.fade_rate = ReadByte() / 16.0;
324 string s = self.noise;
326 strunzone(self.noise);
327 self.noise = strzone(ReadString());
330 precache_sound(self.noise);
331 _sound(self, CH_BGM_SINGLE, self.noise, 0, ATTEN_NONE);
332 if(getsoundtime(self, CH_BGM_SINGLE) < 0)
334 LOG_TRACEF("Cannot initialize sound %s\n", self.noise);
335 strunzone(self.noise);
336 self.noise = string_null;
341 setorigin(self, self.origin);
342 setsize(self, self.mins, self.maxs);
344 self.think = Ent_TriggerMusic_Think;
345 self.nextthink = time;