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