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