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