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