]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/target/music.qc
Fix the use of self, activator and other globals in .use
[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         FOREACH_ENTITY_CLASS("target_music", true, {
40                 it.volume = 0;
41         if (it.targetname == "")
42             WITHSELF(it, target_music_sendto(MSG_ALL, 1));
43         else
44             WITHSELF(it, target_music_sendto(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(MSG_ONE, 1);
55         }
56         FOREACH_CLIENT(IS_SPEC(it) && it.enemy == actor, {
57                 msg_entity = it;
58                 target_music_sendto(MSG_ONE, 1);
59         });
60 }
61 spawnfunc(target_music)
62 {
63         self.use1 = target_music_use;
64         self.reset = target_music_reset;
65         if(!self.volume)
66                 self.volume = 1;
67         if(self.targetname == "")
68                 target_music_sendto(MSG_INIT, 1);
69         else
70                 target_music_sendto(MSG_INIT, 0);
71 }
72 void TargetMusic_RestoreGame()
73 {SELFPARAM();
74         for(entity e = world; (e = find(e, classname, "target_music")); )
75         {
76                 setself(e);
77                 if(self.targetname == "")
78                         target_music_sendto(MSG_INIT, 1);
79                 else
80                         target_music_sendto(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(self.cnt)
96                 sf |= 0x80;
97         WriteByte(MSG_ENTITY, sf);
98         if(sf & 4)
99         {
100                 WriteCoord(MSG_ENTITY, self.origin.x);
101                 WriteCoord(MSG_ENTITY, self.origin.y);
102                 WriteCoord(MSG_ENTITY, self.origin.z);
103         }
104         if(sf & 1)
105         {
106                 if(self.model != "null")
107                 {
108                         WriteShort(MSG_ENTITY, self.modelindex);
109                         WriteCoord(MSG_ENTITY, self.mins.x);
110                         WriteCoord(MSG_ENTITY, self.mins.y);
111                         WriteCoord(MSG_ENTITY, self.mins.z);
112                         WriteCoord(MSG_ENTITY, self.maxs.x);
113                         WriteCoord(MSG_ENTITY, self.maxs.y);
114                         WriteCoord(MSG_ENTITY, self.maxs.z);
115                 }
116                 else
117                 {
118                         WriteShort(MSG_ENTITY, 0);
119                         WriteCoord(MSG_ENTITY, self.maxs.x);
120                         WriteCoord(MSG_ENTITY, self.maxs.y);
121                         WriteCoord(MSG_ENTITY, self.maxs.z);
122                 }
123                 WriteByte(MSG_ENTITY, self.volume * 255.0);
124                 WriteByte(MSG_ENTITY, self.fade_time * 16.0);
125                 WriteByte(MSG_ENTITY, self.fade_rate * 16.0);
126                 WriteString(MSG_ENTITY, self.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.use1 = 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 = world;
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\n", 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()
257 {SELFPARAM();
258         if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, self, world))
259         {
260                 music_trigger = self;
261         }
262         self.nextthink = time;
263 }
264
265 void Ent_TriggerMusic_Remove()
266 {SELFPARAM();
267         if(self.noise)
268                 strunzone(self.noise);
269         self.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                 self.origin_x = ReadCoord();
278                 self.origin_y = ReadCoord();
279                 self.origin_z = ReadCoord();
280         }
281         if(f & 1)
282         {
283                 self.modelindex = ReadShort();
284                 if(self.modelindex)
285                 {
286                         self.mins_x = ReadCoord();
287                         self.mins_y = ReadCoord();
288                         self.mins_z = ReadCoord();
289                         self.maxs_x = ReadCoord();
290                         self.maxs_y = ReadCoord();
291                         self.maxs_z = ReadCoord();
292                 }
293                 else
294                 {
295                         self.mins    = '0 0 0';
296                         self.maxs_x = ReadCoord();
297                         self.maxs_y = ReadCoord();
298                         self.maxs_z = ReadCoord();
299                 }
300
301                 self.volume = ReadByte() / 255.0;
302                 self.fade_time = ReadByte() / 16.0;
303                 self.fade_rate = ReadByte() / 16.0;
304                 string s = self.noise;
305                 if(self.noise)
306                         strunzone(self.noise);
307                 self.noise = strzone(ReadString());
308                 if(self.noise != s)
309                 {
310                         precache_sound(self.noise);
311                         _sound(self, CH_BGM_SINGLE, self.noise, 0, ATTEN_NONE);
312                         if(getsoundtime(self, CH_BGM_SINGLE) < 0)
313                         {
314                                 LOG_TRACEF("Cannot initialize sound %s\n", self.noise);
315                                 strunzone(self.noise);
316                                 self.noise = string_null;
317                         }
318                 }
319         }
320
321         setorigin(self, self.origin);
322         setsize(self, self.mins, self.maxs);
323         self.cnt = 1;
324         self.think = Ent_TriggerMusic_Think;
325         self.nextthink = time;
326         return true;
327 }
328
329 #endif