]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/target/music.qc
Merge branch 'master' into Mario/entcs
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / target / music.qc
1 #include "music.qh"
2 #if defined(CSQC)
3 #elif defined(MENUQC)
4 #elif defined(SVQC)
5     #include <common/constants.qh>
6     #include <server/constants.qh>
7     #include <server/defs.qh>
8 #endif
9
10 REGISTER_NET_TEMP(TE_CSQC_TARGET_MUSIC)
11 REGISTER_NET_LINKED(ENT_CLIENT_TRIGGER_MUSIC)
12
13 #ifdef SVQC
14
15 // values:
16 //   volume
17 //   noise
18 //   targetname
19 //   lifetime
20 //   fade_time
21 //   fade_rate
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)
25 {
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);
33 }
34 void target_music_reset(entity this)
35 {
36         if (this.targetname == "") target_music_sendto(this, MSG_ALL, 1);
37 }
38 void target_music_kill()
39 {
40         FOREACH_ENTITY_CLASS("target_music", true, {
41                 it.volume = 0;
42         if (it.targetname == "")
43             target_music_sendto(it, MSG_ALL, 1);
44         else
45             target_music_sendto(it, MSG_ALL, 0);
46         });
47 }
48 void target_music_use(entity this, entity actor, entity trigger)
49 {
50         if(!actor)
51                 return;
52         if(IS_REAL_CLIENT(actor))
53         {
54                 msg_entity = actor;
55                 target_music_sendto(this, MSG_ONE, 1);
56         }
57         FOREACH_CLIENT(IS_SPEC(it) && it.enemy == actor, {
58                 msg_entity = it;
59                 target_music_sendto(this, MSG_ONE, 1);
60         });
61 }
62 spawnfunc(target_music)
63 {
64         this.use = target_music_use;
65         this.reset = target_music_reset;
66         if(!this.volume)
67                 this.volume = 1;
68         if(this.targetname == "")
69                 target_music_sendto(this, MSG_INIT, 1);
70         else
71                 target_music_sendto(this, MSG_INIT, 0);
72 }
73 void TargetMusic_RestoreGame()
74 {
75         FOREACH_ENTITY_CLASS("target_music", true,
76         {
77                 if(it.targetname == "")
78                         target_music_sendto(it, MSG_INIT, 1);
79                 else
80                         target_music_sendto(it, MSG_INIT, 0);
81         });
82 }
83 // values:
84 //   volume
85 //   noise
86 //   targetname
87 //   fade_time
88 // spawnflags:
89 //   1 = START_OFF
90 // when triggered, it is disabled/enabled for everyone
91 bool trigger_music_SendEntity(entity this, entity to, float sf)
92 {
93         WriteHeader(MSG_ENTITY, ENT_CLIENT_TRIGGER_MUSIC);
94         sf &= ~0x80;
95         if(this.cnt)
96                 sf |= 0x80;
97         WriteByte(MSG_ENTITY, sf);
98         if(sf & 4)
99         {
100                 WriteCoord(MSG_ENTITY, this.origin.x);
101                 WriteCoord(MSG_ENTITY, this.origin.y);
102                 WriteCoord(MSG_ENTITY, this.origin.z);
103         }
104         if(sf & 1)
105         {
106                 if(this.model != "null")
107                 {
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);
115                 }
116                 else
117                 {
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);
122                 }
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);
127         }
128         return 1;
129 }
130 void trigger_music_reset(entity this)
131 {
132         this.cnt = !(this.spawnflags & 1);
133         this.SendFlags |= 0x80;
134 }
135 void trigger_music_use(entity this, entity actor, entity trigger)
136 {
137         this.cnt = !this.cnt;
138         this.SendFlags |= 0x80;
139 }
140 spawnfunc(trigger_music)
141 {
142         if(this.model != "") _setmodel(this, this.model);
143         if(!this.volume) this.volume = 1;
144         if(!this.modelindex)
145         {
146                 setorigin(this, this.origin + this.mins);
147                 setsize(this, '0 0 0', this.maxs - this.mins);
148         }
149         trigger_music_reset(this);
150
151         this.use = trigger_music_use;
152         this.reset = trigger_music_reset;
153
154         Net_LinkEntity(this, false, 0, trigger_music_SendEntity);
155 }
156 #elif defined(CSQC)
157
158 entity TargetMusic_list;
159 STATIC_INIT(TargetMusic_list)
160 {
161         TargetMusic_list = LL_NEW();
162 }
163
164 void TargetMusic_Advance()
165 {
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;
172                 if (it == best)
173                 {
174                         // increase volume
175                         it.state = (it.fade_time > 0) ? bound(0, it.state + frametime / it.fade_time, 1) : 1;
176                 }
177                 else
178                 {
179                         // decrease volume
180                         it.state = (it.fade_rate > 0) ? bound(0, it.state - frametime / it.fade_rate, 1) : 0;
181                 }
182                 const float vol = it.state * it.volume * autocvar_bgmvolume;
183                 if (vol != vol0)
184                 {
185                         if(vol0 < 0)
186                                 _sound(it, CH_BGM_SINGLE, it.noise, vol, ATTEN_NONE); // restart
187                         else
188                                 _sound(it, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
189                         it.lastvol = vol;
190                 }
191         });
192         music_trigger = NULL;
193         bgmtime = (best) ? getsoundtime(best, CH_BGM_SINGLE) : gettime(GETTIME_CDTRACK);
194 }
195
196 NET_HANDLE(TE_CSQC_TARGET_MUSIC, bool isNew)
197 {
198         Net_TargetMusic();
199         return true;
200 }
201
202 void Net_TargetMusic()
203 {
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();
210
211         entity e = NULL;
212         LL_EACH(TargetMusic_list, it.count == id, { e = it; break; });
213         if (!e)
214         {
215                 LL_PUSH(TargetMusic_list, e = new_pure(TargetMusic));
216                 e.count = id;
217         }
218         if(e.noise != noi)
219         {
220                 if(e.noise)
221                         strunzone(e.noise);
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)
226                 {
227                         LOG_TRACEF("Cannot initialize sound %s", e.noise);
228                         strunzone(e.noise);
229                         e.noise = string_null;
230                 }
231         }
232         e.volume = vol;
233         e.fade_time = fai;
234         e.fade_rate = fao;
235         if(vol > 0)
236         {
237                 if(tim == 0)
238                 {
239                         music_default = e;
240                         if(!music_disabled)
241                         {
242                                 e.state = 2;
243                                 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
244                                 localcmd("cd stop\n"); // just in case
245                                 music_disabled = 1;
246                         }
247                 }
248                 else
249                 {
250                         music_target = e;
251                         e.lifetime = time + tim;
252                 }
253         }
254 }
255
256 void Ent_TriggerMusic_Think(entity this)
257 {
258         if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, this, NULL))
259         {
260                 music_trigger = this;
261         }
262         this.nextthink = time;
263 }
264
265 void Ent_TriggerMusic_Remove(entity this)
266 {
267         if(this.noise)
268                 strunzone(this.noise);
269         this.noise = string_null;
270 }
271
272 NET_HANDLE(ENT_CLIENT_TRIGGER_MUSIC, bool isnew)
273 {
274         int f = ReadByte();
275         if(f & 4)
276         {
277                 this.origin_x = ReadCoord();
278                 this.origin_y = ReadCoord();
279                 this.origin_z = ReadCoord();
280         }
281         if(f & 1)
282         {
283                 this.modelindex = ReadShort();
284                 if(this.modelindex)
285                 {
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();
292                 }
293                 else
294                 {
295                         this.mins    = '0 0 0';
296                         this.maxs_x = ReadCoord();
297                         this.maxs_y = ReadCoord();
298                         this.maxs_z = ReadCoord();
299                 }
300
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;
305                 if(this.noise)
306                         strunzone(this.noise);
307                 this.noise = strzone(ReadString());
308                 if(this.noise != s)
309                 {
310                         precache_sound(this.noise);
311                         _sound(this, CH_BGM_SINGLE, this.noise, 0, ATTEN_NONE);
312                         if(getsoundtime(this, CH_BGM_SINGLE) < 0)
313                         {
314                                 LOG_TRACEF("Cannot initialize sound %s", this.noise);
315                                 strunzone(this.noise);
316                                 this.noise = string_null;
317                         }
318                 }
319         }
320
321         setorigin(this, this.origin);
322         setsize(this, this.mins, this.maxs);
323         this.cnt = 1;
324         setthink(this, Ent_TriggerMusic_Think);
325         this.nextthink = time;
326         return true;
327 }
328
329 #endif