File: bgmscript.qc

package info (click to toggle)
nexuiz-data 2.5.2-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,294,288 kB
  • sloc: ansic: 10,523; perl: 6,845; sh: 2,188; java: 1,417; xml: 969; lisp: 519; ruby: 136; makefile: 125
file content (229 lines) | stat: -rw-r--r-- 4,607 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#define CONSTANT_SPEED_DECAY

float bgmscriptbuf;
float bgmscriptbufsize;
float bgmscriptbufloaded;

.float bgmscriptline;
.float bgmscriptline0;
.float bgmscriptvolume;
.float bgmscripttime;
.float bgmscriptstate;
.float bgmscriptstatetime;

float GetAttackDecaySustainAmplitude(float a, float d, float s, float t)
{
	// phase:
	//   attack: from 0 to 1, in time a for a full length
	//   decay: from 1 to s, in time d
	//   sustain: s
	
	if(t < 0)
		return 0;
	
	if(a)
		if(t <= a)
			return t / a;

	if(d)
		if(t <= a + d)
			return ((t - a) / d) * (s - 1) + 1;

	return s;
}

float GetReleaseAmplitude(float d, float s, float r, float t)
{
	float decayval, releaseval;

	if(!r)
		return 0;
	
	if(t > r)
		return 0;
	
	releaseval = s * (1 - t / r);

	if(t < -d)
		return 1;

	if(t < 0 && t >= -d)
	{
		// pre-time decay
		// value is s at time  0
		//          1 at time -d
		decayval = ((t + d) / d) * (s - 1) + 1;
		return max(decayval, releaseval);
	}

	return releaseval;
}

float GetAttackTime(float a, float amp)
{
	return amp * a;
}

float GetReleaseTime(float d, float s, float r, float amp)
{
	float decaytime, releasetime;

	if(!s)
		return 0;
	
	// if amp > s, we may be in the attack or in the prolonged decay curve
	releasetime = (1 - amp / s) * r;

	if(amp > s)
	{
		if(s == 1) // gracefully handle division by zero here
			return 0;

		// pre-time decay
		// value is s at time  0
		//          1 at time -d
		decaytime = (amp - 1) / (s - 1) * d - d;
		return max(decaytime, releasetime);
	}

	return releasetime;
}

void BGMScript_Init()
{
	string s;
	float fh;
	bgmscriptbuf = bgmscriptbufsize = 0;
	bgmscriptbufloaded = 1;
	s = strcat("maps/", mi_shortname, ".bgs");
	fh = fopen(s, FILE_READ);
	if(fh < 0)
		return;
	bgmscriptbuf = buf_create();
	while((s = fgets(fh)))
	{
		bufstr_set(bgmscriptbuf, bgmscriptbufsize, s);
		++bgmscriptbufsize;
	}
	fclose(fh);
}

void BGMScript_InitEntity(entity e)
{
	float l;
	string m;
	if(e.bgmscript != "")
	{
		if(!bgmscriptbufloaded)
			BGMScript_Init();

		float i;

		m = strcat(e.bgmscript, " ");
		l = strlen(m);

		e.bgmscriptline0 = -1;
		for(i = 0; i < bgmscriptbufsize; ++i)
		{
			if(substring(bufstr_get(bgmscriptbuf, i), 0, l) == m)
				break;
		}
		e.bgmscriptline = e.bgmscriptline0 = i;
		if(i >= bgmscriptbufsize)
		{
			print("ERROR: bgmscript does not define ", e.bgmscript, "\n");
			e.bgmscript = "";
		}
	}
}

float GetCurrentAmplitude(entity e, float trel)
{
	if(e.bgmscriptstate)
		return GetAttackDecaySustainAmplitude(e.bgmscriptattack, e.bgmscriptdecay, e.bgmscriptsustain, trel) * e.bgmscriptvolume;
	else
	{
#ifdef CONSTANT_SPEED_DECAY
		return GetReleaseAmplitude(e.bgmscriptdecay, e.bgmscriptsustain * e.bgmscriptvolume, e.bgmscriptrelease, trel);
#else
		return GetReleaseAmplitude(e.bgmscriptdecay, e.bgmscriptsustain, e.bgmscriptrelease, trel) * e.bgmscriptvolume;
#endif
	}
}

float GetTimeForAmplitude(entity e, float amp)
{
	if(e.bgmscriptstate)
		return GetAttackTime(e.bgmscriptattack, amp / e.bgmscriptvolume);
	else
	{
#ifdef CONSTANT_SPEED_DECAY
		return GetReleaseTime(e.bgmscriptdecay, e.bgmscriptsustain * e.bgmscriptvolume, e.bgmscriptrelease, amp);
#else
		return GetReleaseTime(e.bgmscriptdecay, e.bgmscriptsustain, e.bgmscriptrelease, amp / e.bgmscriptvolume);
#endif
	}
}

float BGMScript(entity e)
{
	float t;
	float amp, vel;

	if(e.bgmscript == "")
		return 1;
	
	if(cvar("bgmvolume") <= 0)
		return -1;

	e.just_toggled = FALSE;

	t = gettime(GETTIME_CDTRACK);
	if(t < 0)
		return -1;

	if(t < e.bgmscripttime)
	{
		//print("reset ", e.bgmscript, "\n");
		amp = GetCurrentAmplitude(e, e.bgmscripttime - e.bgmscriptstatetime + drawframetime);

		e.bgmscriptline = e.bgmscriptline0;
		e.bgmscripttime = t;

		// treat this as a stop event for all notes, to prevent sticking keys
		e.bgmscriptstate = FALSE;
		e.bgmscriptvolume = 1;
		e.bgmscriptstatetime = t - GetTimeForAmplitude(e, amp);
	}

	// find the CURRENT line
	for(;;)
	{
		tokenize_console(bufstr_get(bgmscriptbuf, e.bgmscriptline));
		if(stof(argv(1)) >= t || argv(0) != e.bgmscript)
		{
			e.bgmscripttime = t;
			return GetCurrentAmplitude(e, t - e.bgmscriptstatetime);
		}
		else if(t >= stof(argv(1)))
		{
			e.bgmscriptline += 1;
			e.bgmscripttime = stof(argv(1));

			amp = GetCurrentAmplitude(e, e.bgmscripttime - e.bgmscriptstatetime);

			// time code reached!
			vel = stof(argv(2));
			if(vel > 0)
			{
				e.just_toggled = e.bgmscriptstate = TRUE;
				e.bgmscriptvolume = vel;
			}
			else
				e.just_toggled = e.bgmscriptstate = FALSE;

			e.bgmscriptstatetime = e.bgmscripttime - GetTimeForAmplitude(e, amp);
		}
	}
}