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