]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/target_music.qc
teamradar: fix clip fail
[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, CHAN_VOICE) < 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 * cvar("bgmvolume");
43                 if(vol != vol0)
44                 {
45                         if(vol0 < 0)
46                                 sound(e, CHAN_VOICE, e.noise, vol, ATTN_NONE); // restart
47                         else
48                                 sound(e, CHAN_VOICE, "", vol, ATTN_NONE);
49                         e.lastvol = vol;
50                 }
51         }
52         music_trigger = world;
53 }
54
55 void Net_TargetMusic()
56 {
57         float vol, fai, fao, tim, id;
58         string noi;
59         entity e;
60
61         id = ReadShort();
62         vol = ReadByte() / 255.0;
63         fai = ReadByte() / 16.0;
64         fao = ReadByte() / 16.0;
65         tim = ReadByte();
66         noi = ReadString();
67
68         for(e = world; (e = findfloat(e, enttype, ENT_CLIENT_TRIGGER_MUSIC)); )
69         {
70                 if(e.count == id)
71                         break;
72         }
73         if(!e)
74         {
75                 e = spawn();
76                 e.enttype = ENT_CLIENT_TRIGGER_MUSIC;
77                 e.count = id;
78         }
79         if(e.noise != noi)
80         {
81                 if(e.noise)
82                         strunzone(e.noise);
83                 e.noise = strzone(noi);
84                 precache_sound(e.noise);
85                 sound(e, CHAN_VOICE, e.noise, 0, ATTN_NONE);
86                 if(getsoundtime(e, CHAN_VOICE) < 0)
87                 {
88                         print("Cannot initialize sound ", e.noise, "\n");
89                         strunzone(e.noise);
90                         e.noise = string_null;
91                 }
92         }
93         e.volume = vol;
94         e.fade_time = fai;
95         e.fade_rate = fao;
96         if(vol > 0)
97         {
98                 if(tim == 0)
99                 {
100                         music_default = e;
101                         if(!music_disabled)
102                         {
103                                 e.state = 2;
104                                 localcmd("cd stop\n"); // just in case
105                                 music_disabled = 1;
106                         }
107                 }
108                 else
109                 {
110                         music_target = e;
111                         e.lifetime = time + tim;
112                 }
113         }
114 }
115
116 void Ent_TriggerMusic_Think()
117 {
118         if(WarpZoneLib_BoxTouchesBrush(view_origin, view_origin, self, world))
119         {
120                 music_trigger = self;
121         }
122         self.nextthink = time;
123 }
124
125 void Ent_TriggerMusic_Remove()
126 {
127         if(self.noise)
128                 strunzone(self.noise);
129         self.noise = string_null;
130 }
131
132 void Ent_ReadTriggerMusic()
133 {
134         float f;
135         string s;
136         f = ReadByte();
137         if(f & 4)
138         {
139                 self.origin_x = ReadCoord();
140                 self.origin_y = ReadCoord();
141                 self.origin_z = ReadCoord();
142         }
143         if(f & 1)
144         {
145                 self.modelindex = ReadShort();
146                 if(self.modelindex)
147                 {
148                         self.mins_x = ReadCoord();
149                         self.mins_y = ReadCoord();
150                         self.mins_z = ReadCoord();
151                         self.maxs_x = ReadCoord();
152                         self.maxs_y = ReadCoord();
153                         self.maxs_z = ReadCoord();
154                 }
155                 else
156                 {
157                         self.mins    = '0 0 0';
158                         self.maxs_x = ReadCoord();
159                         self.maxs_y = ReadCoord();
160                         self.maxs_z = ReadCoord();
161                 }
162
163                 self.volume = ReadByte() / 255.0;
164                 self.fade_time = ReadByte() / 16.0;
165                 self.fade_rate = ReadByte() / 16.0;
166                 s = self.noise;
167                 if(self.noise)
168                         strunzone(self.noise);
169                 self.noise = strzone(ReadString());
170                 if(self.noise != s)
171                 {
172                         precache_sound(self.noise);
173                         sound(self, CHAN_VOICE, self.noise, 0, ATTN_NONE);
174                         if(getsoundtime(self, CHAN_VOICE) < 0)
175                         {
176                                 print("Cannot initialize sound ", self.noise, "\n");
177                                 strunzone(self.noise);
178                                 self.noise = string_null;
179                         }
180                 }
181         }
182
183         setorigin(self, self.origin);
184         setsize(self, self.mins, self.maxs);
185         self.cnt = 1;
186         self.think = Ent_TriggerMusic_Think;
187         self.nextthink = time;
188 }