Merge branch 'master' into Mario/showspecs
[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         int id = ReadShort();
57         float vol = ReadByte() / 255.0;
58         float fai = ReadByte() / 16.0;
59         float fao = ReadByte() / 16.0;
60         float tim = ReadByte();
61         string noi = ReadString();
62
63         entity e;
64         for(e = world; (e = findfloat(e, enttype, ENT_CLIENT_TRIGGER_MUSIC)); )
65         {
66                 if(e.count == id)
67                         break;
68         }
69         if(!e)
70         {
71                 e = spawn();
72                 e.enttype = ENT_CLIENT_TRIGGER_MUSIC;
73                 e.count = id;
74         }
75         if(e.noise != noi)
76         {
77                 if(e.noise)
78                         strunzone(e.noise);
79                 e.noise = strzone(noi);
80                 precache_sound(e.noise);
81                 sound(e, CH_BGM_SINGLE, e.noise, 0, ATTEN_NONE);
82                 if(getsoundtime(e, CH_BGM_SINGLE) < 0)
83                 {
84                         dprintf("Cannot initialize sound %s\n", e.noise);
85                         strunzone(e.noise);
86                         e.noise = string_null;
87                 }
88         }
89         e.volume = vol;
90         e.fade_time = fai;
91         e.fade_rate = fao;
92         if(vol > 0)
93         {
94                 if(tim == 0)
95                 {
96                         music_default = e;
97                         if(!music_disabled)
98                         {
99                                 e.state = 2;
100                                 cvar_settemp("music_playlist_index", "-1"); // don't use playlists
101                                 localcmd("cd stop\n"); // just in case
102                                 music_disabled = 1;
103                         }
104                 }
105                 else
106                 {
107                         music_target = e;
108                         e.lifetime = time + tim;
109                 }
110         }
111 }
112
113 void Ent_TriggerMusic_Think()
114 {
115         if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, self, world))
116         {
117                 music_trigger = self;
118         }
119         self.nextthink = time;
120 }
121
122 void Ent_TriggerMusic_Remove()
123 {
124         if(self.noise)
125                 strunzone(self.noise);
126         self.noise = string_null;
127 }
128
129 void Ent_ReadTriggerMusic()
130 {
131         int f = ReadByte();
132         if(f & 4)
133         {
134                 self.origin_x = ReadCoord();
135                 self.origin_y = ReadCoord();
136                 self.origin_z = ReadCoord();
137         }
138         if(f & 1)
139         {
140                 self.modelindex = ReadShort();
141                 if(self.modelindex)
142                 {
143                         self.mins_x = ReadCoord();
144                         self.mins_y = ReadCoord();
145                         self.mins_z = ReadCoord();
146                         self.maxs_x = ReadCoord();
147                         self.maxs_y = ReadCoord();
148                         self.maxs_z = ReadCoord();
149                 }
150                 else
151                 {
152                         self.mins    = '0 0 0';
153                         self.maxs_x = ReadCoord();
154                         self.maxs_y = ReadCoord();
155                         self.maxs_z = ReadCoord();
156                 }
157
158                 self.volume = ReadByte() / 255.0;
159                 self.fade_time = ReadByte() / 16.0;
160                 self.fade_rate = ReadByte() / 16.0;
161                 string s = self.noise;
162                 if(self.noise)
163                         strunzone(self.noise);
164                 self.noise = strzone(ReadString());
165                 if(self.noise != s)
166                 {
167                         precache_sound(self.noise);
168                         sound(self, CH_BGM_SINGLE, self.noise, 0, ATTEN_NONE);
169                         if(getsoundtime(self, CH_BGM_SINGLE) < 0)
170                         {
171                                 dprintf("Cannot initialize sound %s\n", self.noise);
172                                 strunzone(self.noise);
173                                 self.noise = string_null;
174                         }
175                 }
176         }
177
178         setorigin(self, self.origin);
179         setsize(self, self.mins, self.maxs);
180         self.cnt = 1;
181         self.think = Ent_TriggerMusic_Think;
182         self.nextthink = time;
183 }