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