Merge remote-tracking branch 'origin/divVerent/allow-override-item-model'
[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                                 localcmd("cd stop\n"); // just in case
110                                 music_disabled = 1;
111                         }
112                 }
113                 else
114                 {
115                         music_target = e;
116                         e.lifetime = time + tim;
117                 }
118         }
119 }
120
121 void Ent_TriggerMusic_Think()
122 {
123         if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, self, world))
124         {
125                 music_trigger = self;
126         }
127         self.nextthink = time;
128 }
129
130 void Ent_TriggerMusic_Remove()
131 {
132         if(self.noise)
133                 strunzone(self.noise);
134         self.noise = string_null;
135 }
136
137 void Ent_ReadTriggerMusic()
138 {
139         float f;
140         string s;
141         f = ReadByte();
142         if(f & 4)
143         {
144                 self.origin_x = ReadCoord();
145                 self.origin_y = ReadCoord();
146                 self.origin_z = ReadCoord();
147         }
148         if(f & 1)
149         {
150                 self.modelindex = ReadShort();
151                 if(self.modelindex)
152                 {
153                         self.mins_x = ReadCoord();
154                         self.mins_y = ReadCoord();
155                         self.mins_z = ReadCoord();
156                         self.maxs_x = ReadCoord();
157                         self.maxs_y = ReadCoord();
158                         self.maxs_z = ReadCoord();
159                 }
160                 else
161                 {
162                         self.mins    = '0 0 0';
163                         self.maxs_x = ReadCoord();
164                         self.maxs_y = ReadCoord();
165                         self.maxs_z = ReadCoord();
166                 }
167
168                 self.volume = ReadByte() / 255.0;
169                 self.fade_time = ReadByte() / 16.0;
170                 self.fade_rate = ReadByte() / 16.0;
171                 s = self.noise;
172                 if(self.noise)
173                         strunzone(self.noise);
174                 self.noise = strzone(ReadString());
175                 if(self.noise != s)
176                 {
177                         precache_sound(self.noise);
178                         sound(self, CH_BGM_SINGLE, self.noise, 0, ATTN_NONE);
179                         if(getsoundtime(self, CH_BGM_SINGLE) < 0)
180                         {
181                                 print(sprintf(_("Cannot initialize sound %s\n"), self.noise));
182                                 strunzone(self.noise);
183                                 self.noise = string_null;
184                         }
185                 }
186         }
187
188         setorigin(self, self.origin);
189         setsize(self, self.mins, self.maxs);
190         self.cnt = 1;
191         self.think = Ent_TriggerMusic_Think;
192         self.nextthink = time;
193 }