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(entity this)
35 if (this.targetname == "") target_music_sendto(MSG_ALL, 1);
37 void target_music_kill()
39 for(self = world; (self = find(self, classname, "target_music")); )
42 if(self.targetname == "")
43 target_music_sendto(MSG_ALL, 1);
45 target_music_sendto(MSG_ALL, 0);
48 void target_music_use()
52 if(IS_REAL_CLIENT(activator))
54 msg_entity = activator;
55 target_music_sendto(MSG_ONE, 1);
58 FOR_EACH_SPEC(head) if(head.enemy == activator) { msg_entity = head; target_music_sendto(MSG_ONE, 1); }
60 spawnfunc(target_music)
62 self.use = target_music_use;
63 self.reset = target_music_reset;
66 if(self.targetname == "")
67 target_music_sendto(MSG_INIT, 1);
69 target_music_sendto(MSG_INIT, 0);
71 void TargetMusic_RestoreGame()
73 for(entity e = world; (e = find(e, classname, "target_music")); )
76 if(self.targetname == "")
77 target_music_sendto(MSG_INIT, 1);
79 target_music_sendto(MSG_INIT, 0);
89 // when triggered, it is disabled/enabled for everyone
90 bool trigger_music_SendEntity(entity this, entity to, float sf)
92 WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_MUSIC);
96 WriteByte(MSG_ENTITY, sf);
99 WriteCoord(MSG_ENTITY, self.origin.x);
100 WriteCoord(MSG_ENTITY, self.origin.y);
101 WriteCoord(MSG_ENTITY, self.origin.z);
105 if(self.model != "null")
107 WriteShort(MSG_ENTITY, self.modelindex);
108 WriteCoord(MSG_ENTITY, self.mins.x);
109 WriteCoord(MSG_ENTITY, self.mins.y);
110 WriteCoord(MSG_ENTITY, self.mins.z);
111 WriteCoord(MSG_ENTITY, self.maxs.x);
112 WriteCoord(MSG_ENTITY, self.maxs.y);
113 WriteCoord(MSG_ENTITY, self.maxs.z);
117 WriteShort(MSG_ENTITY, 0);
118 WriteCoord(MSG_ENTITY, self.maxs.x);
119 WriteCoord(MSG_ENTITY, self.maxs.y);
120 WriteCoord(MSG_ENTITY, self.maxs.z);
122 WriteByte(MSG_ENTITY, self.volume * 255.0);
123 WriteByte(MSG_ENTITY, self.fade_time * 16.0);
124 WriteByte(MSG_ENTITY, self.fade_rate * 16.0);
125 WriteString(MSG_ENTITY, self.noise);
129 void trigger_music_reset(entity this)
131 this.cnt = !(this.spawnflags & 1);
132 this.SendFlags |= 0x80;
134 void trigger_music_use()
136 self.cnt = !self.cnt;
137 self.SendFlags |= 0x80;
139 spawnfunc(trigger_music)
141 if(this.model != "") _setmodel(this, this.model);
142 if(!this.volume) this.volume = 1;
145 setorigin(this, this.origin + this.mins);
146 setsize(this, '0 0 0', this.maxs - this.mins);
148 trigger_music_reset(this);
150 this.use = trigger_music_use;
151 this.reset = trigger_music_reset;
153 Net_LinkEntity(this, false, 0, trigger_music_SendEntity);
157 void TargetMusic_Advance()
159 // run AFTER all the thinks!
162 best = music_default;
163 if(music_target && time < music_target.lifetime)
166 best = music_trigger;
167 for(e = world; (e = findfloat(e, enttype, NET_ENT_CLIENT_TRIGGER_MUSIC.m_id)); ) if(e.noise)
170 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
178 e.state = bound(0, e.state + frametime / e.fade_time, 1);
186 e.state = bound(0, e.state - frametime / e.fade_rate, 1);
190 vol = e.state * e.volume * autocvar_bgmvolume;
194 _sound(e, CH_BGM_SINGLE, e.noise, vol, ATTEN_NONE); // restart
196 _sound(e, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
200 music_trigger = world;
203 bgmtime = getsoundtime(best, CH_BGM_SINGLE);
205 bgmtime = gettime(GETTIME_CDTRACK);
208 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
214 void Net_TargetMusic()
216 int id = ReadShort();
217 float vol = ReadByte() / 255.0;
218 float fai = ReadByte() / 16.0;
219 float fao = ReadByte() / 16.0;
220 float tim = ReadByte();
221 string noi = ReadString();
224 for(e = world; (e = findfloat(e, enttype, NET_ENT_CLIENT_TRIGGER_MUSIC.m_id)); )
232 e.enttype = NET_ENT_CLIENT_TRIGGER_MUSIC.m_id;
239 e.noise = strzone(noi);
240 precache_sound(e.noise);
241 _sound(e, CH_BGM_SINGLE, e.noise, 0, ATTEN_NONE);
242 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
244 LOG_TRACEF("Cannot initialize sound %s\n", e.noise);
246 e.noise = string_null;
260 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
261 localcmd("cd stop\n"); // just in case
268 e.lifetime = time + tim;
273 void Ent_TriggerMusic_Think()
275 if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, self, world))
277 music_trigger = self;
279 self.nextthink = time;
282 void Ent_TriggerMusic_Remove()
285 strunzone(self.noise);
286 self.noise = string_null;
289 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
294 self.origin_x = ReadCoord();
295 self.origin_y = ReadCoord();
296 self.origin_z = ReadCoord();
300 self.modelindex = ReadShort();
303 self.mins_x = ReadCoord();
304 self.mins_y = ReadCoord();
305 self.mins_z = ReadCoord();
306 self.maxs_x = ReadCoord();
307 self.maxs_y = ReadCoord();
308 self.maxs_z = ReadCoord();
313 self.maxs_x = ReadCoord();
314 self.maxs_y = ReadCoord();
315 self.maxs_z = ReadCoord();
318 self.volume = ReadByte() / 255.0;
319 self.fade_time = ReadByte() / 16.0;
320 self.fade_rate = ReadByte() / 16.0;
321 string s = self.noise;
323 strunzone(self.noise);
324 self.noise = strzone(ReadString());
327 precache_sound(self.noise);
328 _sound(self, CH_BGM_SINGLE, self.noise, 0, ATTEN_NONE);
329 if(getsoundtime(self, CH_BGM_SINGLE) < 0)
331 LOG_TRACEF("Cannot initialize sound %s\n", self.noise);
332 strunzone(self.noise);
333 self.noise = string_null;
338 setorigin(self, self.origin);
339 setsize(self, self.mins, self.maxs);
341 self.think = Ent_TriggerMusic_Think;
342 self.nextthink = time;