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