2 #include "../dpdefs/csprogsdefs.qh"
4 #include "../common/triggers/triggers.qh"
5 #include "../common/util.qh"
6 #include "autocvars.qh"
7 #include "bgmscript.qh"
13 #define CONSTANT_SPEED_DECAY
16 float bgmscriptbufsize;
17 float bgmscriptbufloaded;
20 .float bgmscriptline0;
21 .float bgmscriptvolume;
23 .float bgmscriptstate;
24 .float bgmscriptstatetime;
26 float GetAttackDecaySustainAmplitude(float a, float d, float s, float t)
29 // attack: from 0 to 1, in time a for a full length
30 // decay: from 1 to s, in time d
42 return ((t - a) / d) * (s - 1) + 1;
47 float GetReleaseAmplitude(float d, float s, float r, float t)
49 float decayval, releaseval;
57 releaseval = s * (1 - t / r);
65 // value is s at time 0
67 decayval = ((t + d) / d) * (s - 1) + 1;
68 return max(decayval, releaseval);
74 float GetAttackTime(float a, float amp)
79 float GetReleaseTime(float d, float s, float r, float amp)
81 float decaytime, releasetime;
86 // if amp > s, we may be in the attack or in the prolonged decay curve
87 releasetime = (1 - amp / s) * r;
91 if(s == 1) // gracefully handle division by zero here
95 // value is s at time 0
97 decaytime = (amp - 1) / (s - 1) * d - d;
98 return max(decaytime, releasetime);
104 void BGMScript_Init()
108 bgmscriptbuf = bgmscriptbufsize = 0;
109 bgmscriptbufloaded = 1;
110 s = strcat("maps/", mi_shortname, ".bgs");
111 fh = fopen(s, FILE_READ);
114 bgmscriptbuf = buf_create();
115 while((s = fgets(fh)))
117 bufstr_set(bgmscriptbuf, bgmscriptbufsize, s);
123 void BGMScript_InitEntity(entity e)
127 if(e.bgmscript != "")
129 if(!bgmscriptbufloaded)
134 m = strcat(e.bgmscript, " ");
137 e.bgmscriptline0 = -1;
138 for(i = 0; i < bgmscriptbufsize; ++i)
140 if(substring(bufstr_get(bgmscriptbuf, i), 0, l) == m)
143 e.bgmscriptline = e.bgmscriptline0 = i;
144 if(i >= bgmscriptbufsize)
146 printf("ERROR: bgmscript does not define %s\n", e.bgmscript);
147 strunzone(e.bgmscript);
148 e.bgmscript = string_null;
153 float GetCurrentAmplitude(entity e, float trel)
156 return GetAttackDecaySustainAmplitude(e.bgmscriptattack, e.bgmscriptdecay, e.bgmscriptsustain, trel) * e.bgmscriptvolume;
159 #ifdef CONSTANT_SPEED_DECAY
160 return GetReleaseAmplitude(e.bgmscriptdecay, e.bgmscriptsustain * e.bgmscriptvolume, e.bgmscriptrelease, trel);
162 return GetReleaseAmplitude(e.bgmscriptdecay, e.bgmscriptsustain, e.bgmscriptrelease, trel) * e.bgmscriptvolume;
167 float GetTimeForAmplitude(entity e, float amp)
170 return GetAttackTime(e.bgmscriptattack, amp / e.bgmscriptvolume);
173 #ifdef CONSTANT_SPEED_DECAY
174 return GetReleaseTime(e.bgmscriptdecay, e.bgmscriptsustain * e.bgmscriptvolume, e.bgmscriptrelease, amp);
176 return GetReleaseTime(e.bgmscriptdecay, e.bgmscriptsustain, e.bgmscriptrelease, amp / e.bgmscriptvolume);
181 float BGMScript(entity e)
185 if(e.bgmscript == "")
188 if(autocvar_bgmvolume <= 0)
191 e.just_toggled = false;
196 if(bgmtime < e.bgmscripttime)
198 amp = GetCurrentAmplitude(e, e.bgmscripttime - e.bgmscriptstatetime + drawframetime);
200 e.bgmscriptline = e.bgmscriptline0;
201 e.bgmscripttime = bgmtime;
203 // treat this as a stop event for all notes, to prevent sticking keys
204 e.bgmscriptstate = false;
205 e.bgmscriptvolume = 1;
206 e.bgmscriptstatetime = bgmtime - GetTimeForAmplitude(e, amp);
209 // find the CURRENT line
212 tokenize_console(bufstr_get(bgmscriptbuf, e.bgmscriptline));
213 if(stof(argv(1)) >= bgmtime || argv(0) != e.bgmscript)
215 e.bgmscripttime = bgmtime;
216 return GetCurrentAmplitude(e, bgmtime - e.bgmscriptstatetime);
218 else if(bgmtime >= stof(argv(1)))
220 e.bgmscriptline += 1;
221 e.bgmscripttime = stof(argv(1));
223 amp = GetCurrentAmplitude(e, e.bgmscripttime - e.bgmscriptstatetime);
225 // time code reached!
229 e.just_toggled = e.bgmscriptstate = true;
230 e.bgmscriptvolume = vel;
233 e.just_toggled = e.bgmscriptstate = false;
235 e.bgmscriptstatetime = e.bgmscripttime - GetTimeForAmplitude(e, amp);