]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/target/music.qc
Merge branch 'master' into terencehill/tooltips_cleanup
[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 "../../constants.qh"
5     #include "../../../server/constants.qh"
6     #include "../../../server/defs.qh"
7 #endif
8
9 #ifdef SVQC
10
11 // values:
12 //   volume
13 //   noise
14 //   targetname
15 //   lifetime
16 //   fade_time
17 //   fade_rate
18 // when triggered, the music is overridden for activator until lifetime (or forever, if lifetime is 0)
19 // when targetname is not set, THIS ONE is default
20 void target_music_sendto(float to, float is)
21 {SELFPARAM();
22         WriteByte(to, SVC_TEMPENTITY);
23         WriteByte(to, TE_CSQC_TARGET_MUSIC);
24         WriteShort(to, num_for_edict(self));
25         WriteByte(to, self.volume * 255.0 * is);
26         WriteByte(to, self.fade_time * 16.0);
27         WriteByte(to, self.fade_rate * 16.0);
28         WriteByte(to, self.lifetime);
29         WriteString(to, self.noise);
30 }
31 void target_music_reset()
32 {SELFPARAM();
33         if(self.targetname == "")
34                 target_music_sendto(MSG_ALL, 1);
35 }
36 void target_music_use()
37 {
38         if(!activator)
39                 return;
40         if(IS_REAL_CLIENT(activator))
41         {
42                 msg_entity = activator;
43                 target_music_sendto(MSG_ONE, 1);
44         }
45         entity head;
46         FOR_EACH_SPEC(head) if(head.enemy == activator) { msg_entity = head; target_music_sendto(MSG_ONE, 1); }
47 }
48 spawnfunc(target_music)
49 {
50         self.use = target_music_use;
51         self.reset = target_music_reset;
52         if(!self.volume)
53                 self.volume = 1;
54         if(self.targetname == "")
55                 target_music_sendto(MSG_INIT, 1);
56         else
57                 target_music_sendto(MSG_INIT, 0);
58 }
59 void TargetMusic_RestoreGame()
60 {SELFPARAM();
61         for(entity e = world; (e = find(e, classname, "target_music")); )
62         {
63                 setself(e);
64                 if(self.targetname == "")
65                         target_music_sendto(MSG_INIT, 1);
66                 else
67                         target_music_sendto(MSG_INIT, 0);
68         }
69 }
70 // values:
71 //   volume
72 //   noise
73 //   targetname
74 //   fade_time
75 // spawnflags:
76 //   1 = START_OFF
77 // when triggered, it is disabled/enabled for everyone
78 bool trigger_music_SendEntity(entity this, entity to, float sf)
79 {
80         WriteByte(MSG_ENTITY, ENT_CLIENT_TRIGGER_MUSIC);
81         sf &= ~0x80;
82         if(self.cnt)
83                 sf |= 0x80;
84         WriteByte(MSG_ENTITY, sf);
85         if(sf & 4)
86         {
87                 WriteCoord(MSG_ENTITY, self.origin.x);
88                 WriteCoord(MSG_ENTITY, self.origin.y);
89                 WriteCoord(MSG_ENTITY, self.origin.z);
90         }
91         if(sf & 1)
92         {
93                 if(self.model != "null")
94                 {
95                         WriteShort(MSG_ENTITY, self.modelindex);
96                         WriteCoord(MSG_ENTITY, self.mins.x);
97                         WriteCoord(MSG_ENTITY, self.mins.y);
98                         WriteCoord(MSG_ENTITY, self.mins.z);
99                         WriteCoord(MSG_ENTITY, self.maxs.x);
100                         WriteCoord(MSG_ENTITY, self.maxs.y);
101                         WriteCoord(MSG_ENTITY, self.maxs.z);
102                 }
103                 else
104                 {
105                         WriteShort(MSG_ENTITY, 0);
106                         WriteCoord(MSG_ENTITY, self.maxs.x);
107                         WriteCoord(MSG_ENTITY, self.maxs.y);
108                         WriteCoord(MSG_ENTITY, self.maxs.z);
109                 }
110                 WriteByte(MSG_ENTITY, self.volume * 255.0);
111                 WriteByte(MSG_ENTITY, self.fade_time * 16.0);
112                 WriteByte(MSG_ENTITY, self.fade_rate * 16.0);
113                 WriteString(MSG_ENTITY, self.noise);
114         }
115         return 1;
116 }
117 void trigger_music_reset()
118 {SELFPARAM();
119         self.cnt = !(self.spawnflags & 1);
120         self.SendFlags |= 0x80;
121 }
122 void trigger_music_use()
123 {SELFPARAM();
124         self.cnt = !self.cnt;
125         self.SendFlags |= 0x80;
126 }
127 spawnfunc(trigger_music)
128 {
129         if(self.model != "")
130                 _setmodel(self, self.model);
131         if(!self.volume)
132                 self.volume = 1;
133         if(!self.modelindex)
134         {
135                 setorigin(self, self.origin + self.mins);
136                 setsize(self, '0 0 0', self.maxs - self.mins);
137         }
138         trigger_music_reset();
139
140         self.use = trigger_music_use;
141         self.reset = trigger_music_reset;
142
143         Net_LinkEntity(self, false, 0, trigger_music_SendEntity);
144 }
145 #elif defined(CSQC)
146
147 void TargetMusic_Advance()
148 {
149         // run AFTER all the thinks!
150         entity best, e;
151         float vol, vol0;
152         best = music_default;
153         if(music_target && time < music_target.lifetime)
154                 best = music_target;
155         if(music_trigger)
156                 best = music_trigger;
157         for(e = world; (e = findfloat(e, enttype, ENT_CLIENT_TRIGGER_MUSIC)); ) if(e.noise)
158         {
159                 vol0 = e.lastvol;
160                 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
161                 {
162                         vol0 = -1;
163                 }
164                 if(e == best)
165                 {
166                         // increase volume
167                         if(e.fade_time > 0)
168                                 e.state = bound(0, e.state + frametime / e.fade_time, 1);
169                         else
170                                 e.state = 1;
171                 }
172                 else
173                 {
174                         // decrease volume
175                         if(e.fade_rate > 0)
176                                 e.state = bound(0, e.state - frametime / e.fade_rate, 1);
177                         else
178                                 e.state = 0;
179                 }
180                 vol = e.state * e.volume * autocvar_bgmvolume;
181                 if(vol != vol0)
182                 {
183                         if(vol0 < 0)
184                                 _sound(e, CH_BGM_SINGLE, e.noise, vol, ATTEN_NONE); // restart
185                         else
186                                 _sound(e, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
187                         e.lastvol = vol;
188                 }
189         }
190         music_trigger = world;
191
192         if(best)
193                 bgmtime = getsoundtime(best, CH_BGM_SINGLE);
194         else
195                 bgmtime = gettime(GETTIME_CDTRACK);
196 }
197
198 void Net_TargetMusic()
199 {
200         int id = ReadShort();
201         float vol = ReadByte() / 255.0;
202         float fai = ReadByte() / 16.0;
203         float fao = ReadByte() / 16.0;
204         float tim = ReadByte();
205         string noi = ReadString();
206
207         entity e;
208         for(e = world; (e = findfloat(e, enttype, ENT_CLIENT_TRIGGER_MUSIC)); )
209         {
210                 if(e.count == id)
211                         break;
212         }
213         if(!e)
214         {
215                 e = spawn();
216                 e.enttype = ENT_CLIENT_TRIGGER_MUSIC;
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 void Ent_ReadTriggerMusic()
274 {SELFPARAM();
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 }
328
329 #endif