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