]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/target_music.qc
Merge remote-tracking branch 'origin/Mario/arena_nuke'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / target_music.qc
1 float music_disabled;
2 entity music_default;
3 entity music_target;
4 entity music_trigger;
5 // FIXME also control bgmvolume here, to not require a target_music for the default track.
6
7 .float state;
8 .float lastvol;
9
10 void TargetMusic_Advance()
11 {
12         // run AFTER all the thinks!
13         entity best, e;
14         float vol, vol0;
15         best = music_default;
16         if(music_target && time < music_target.lifetime)
17                 best = music_target;
18         if(music_trigger)
19                 best = music_trigger;
20         for(e = world; (e = findfloat(e, enttype, ENT_CLIENT_TRIGGER_MUSIC)); ) if(e.noise)
21         {
22                 vol0 = e.lastvol;
23                 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
24                 {
25                         vol0 = -1;
26                 }
27                 if(e == best)
28                 {
29                         // increase volume
30                         if(e.fade_time > 0)
31                                 e.state = bound(0, e.state + frametime / e.fade_time, 1);
32                         else
33                                 e.state = 1;
34                 }
35                 else
36                 {
37                         // decrease volume
38                         if(e.fade_rate > 0)
39                                 e.state = bound(0, e.state - frametime / e.fade_rate, 1);
40                         else
41                                 e.state = 0;
42                 }
43                 vol = e.state * e.volume * autocvar_bgmvolume;
44                 if(vol != vol0)
45                 {
46                         if(vol0 < 0)
47                                 sound(e, CH_BGM_SINGLE, e.noise, vol, ATTEN_NONE); // restart
48                         else
49                                 sound(e, CH_BGM_SINGLE, "", vol, ATTEN_NONE);
50                         e.lastvol = vol;
51                 }
52         }
53         music_trigger = world;
54
55         if(best)
56                 bgmtime = getsoundtime(best, CH_BGM_SINGLE);
57         else
58                 bgmtime = gettime(GETTIME_CDTRACK);
59 }
60
61 void Net_TargetMusic()
62 {
63         float vol, fai, fao, tim, id;
64         string noi;
65         entity e;
66
67         id = ReadShort();
68         vol = ReadByte() / 255.0;
69         fai = ReadByte() / 16.0;
70         fao = ReadByte() / 16.0;
71         tim = ReadByte();
72         noi = ReadString();
73
74         for(e = world; (e = findfloat(e, enttype, ENT_CLIENT_TRIGGER_MUSIC)); )
75         {
76                 if(e.count == id)
77                         break;
78         }
79         if(!e)
80         {
81                 e = spawn();
82                 e.enttype = ENT_CLIENT_TRIGGER_MUSIC;
83                 e.count = id;
84         }
85         if(e.noise != noi)
86         {
87                 if(e.noise)
88                         strunzone(e.noise);
89                 e.noise = strzone(noi);
90                 precache_sound(e.noise);
91                 sound(e, CH_BGM_SINGLE, e.noise, 0, ATTEN_NONE);
92                 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
93                 {
94                         print(sprintf(_("Cannot initialize sound %s\n"), e.noise));
95                         strunzone(e.noise);
96                         e.noise = string_null;
97                 }
98         }
99         e.volume = vol;
100         e.fade_time = fai;
101         e.fade_rate = fao;
102         if(vol > 0)
103         {
104                 if(tim == 0)
105                 {
106                         music_default = e;
107                         if(!music_disabled)
108                         {
109                                 e.state = 2;
110                                 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
111                                 localcmd("cd stop\n"); // just in case
112                                 music_disabled = 1;
113                         }
114                 }
115                 else
116                 {
117                         music_target = e;
118                         e.lifetime = time + tim;
119                 }
120         }
121 }
122
123 void Ent_TriggerMusic_Think()
124 {
125         if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, self, world))
126         {
127                 music_trigger = self;
128         }
129         self.nextthink = time;
130 }
131
132 void Ent_TriggerMusic_Remove()
133 {
134         if(self.noise)
135                 strunzone(self.noise);
136         self.noise = string_null;
137 }
138
139 void Ent_ReadTriggerMusic()
140 {
141         float f;
142         string s;
143         f = ReadByte();
144         if(f & 4)
145         {
146                 self.origin_x = ReadCoord();
147                 self.origin_y = ReadCoord();
148                 self.origin_z = ReadCoord();
149         }
150         if(f & 1)
151         {
152                 self.modelindex = ReadShort();
153                 if(self.modelindex)
154                 {
155                         self.mins_x = ReadCoord();
156                         self.mins_y = ReadCoord();
157                         self.mins_z = ReadCoord();
158                         self.maxs_x = ReadCoord();
159                         self.maxs_y = ReadCoord();
160                         self.maxs_z = ReadCoord();
161                 }
162                 else
163                 {
164                         self.mins    = '0 0 0';
165                         self.maxs_x = ReadCoord();
166                         self.maxs_y = ReadCoord();
167                         self.maxs_z = ReadCoord();
168                 }
169
170                 self.volume = ReadByte() / 255.0;
171                 self.fade_time = ReadByte() / 16.0;
172                 self.fade_rate = ReadByte() / 16.0;
173                 s = self.noise;
174                 if(self.noise)
175                         strunzone(self.noise);
176                 self.noise = strzone(ReadString());
177                 if(self.noise != s)
178                 {
179                         precache_sound(self.noise);
180                         sound(self, CH_BGM_SINGLE, self.noise, 0, ATTEN_NONE);
181                         if(getsoundtime(self, CH_BGM_SINGLE) < 0)
182                         {
183                                 print(sprintf(_("Cannot initialize sound %s\n"), self.noise));
184                                 strunzone(self.noise);
185                                 self.noise = string_null;
186                         }
187                 }
188         }
189
190         setorigin(self, self.origin);
191         setsize(self, self.mins, self.maxs);
192         self.cnt = 1;
193         self.think = Ent_TriggerMusic_Think;
194         self.nextthink = time;
195 }