3 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
5 if (weight == 0) return;
6 if (mean == 0) e.(a) *= pow(value, weight);
7 else e.(a) += pow(value, mean) * weight;
11 float mean_evaluate(entity e, .float a, .float c, float mean)
13 if (e.(c) == 0) return 0;
14 if (mean == 0) return pow(e.(a), 1.0 / e.(c));
15 else return pow(e.(a) / e.(c), 1.0 / mean);
18 #define MEAN_ACCUMULATE(s, prefix, v, w) mean_accumulate(s, prefix##_accumulator, prefix##_count, prefix##_mean, v, w)
19 #define MEAN_EVALUATE(s, prefix) mean_evaluate(s, prefix##_accumulator, prefix##_count, prefix##_mean)
20 #define MEAN_DECLARE(prefix, m) float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
22 /** Returns a random number between -1.0 and 1.0 */
23 #define crandom() (2 * (random() - 0.5))
28 Angc used for animations
33 float angc(float a1, float a2)
51 float fsnap(float val, float fsize)
53 return rint(val / fsize) * fsize;
56 vector vsnap(vector point, float fsize)
60 vret.x = rint(point.x / fsize) * fsize;
61 vret.y = rint(point.y / fsize) * fsize;
62 vret.z = ceil(point.z / fsize) * fsize;
67 vector lerpv(float t0, vector v0, float t1, vector v1, float t)
69 return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
72 vector bezier_quadratic_getpoint(vector a, vector b, vector c, float t)
74 return (c - 2 * b + a) * (t * t)
79 vector bezier_quadratic_getderivative(vector a, vector b, vector c, float t)
81 return (c - 2 * b + a) * (2 * t)
85 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float spd)
87 return (((startspeedfactor + endspeedfactor - 2
88 ) * spd - 2 * startspeedfactor - endspeedfactor + 3
89 ) * spd + startspeedfactor
93 bool cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor)
95 if (startspeedfactor < 0 || endspeedfactor < 0) return false;
98 // if this is the case, the possible zeros of the first derivative are outside
100 We can calculate this condition as condition
105 // better, see below:
106 if (startspeedfactor <= 3 && endspeedfactor <= 3) return true;
108 // if this is the case, the first derivative has no zeros at all
109 float se = startspeedfactor + endspeedfactor;
110 float s_e = startspeedfactor - endspeedfactor;
111 if (3 * (se - 4) * (se - 4) + s_e * s_e <= 12) // an ellipse
114 // Now let s <= 3, s <= 3, s+e >= 3 (triangle) then we get se <= 6 (top right corner).
115 // we also get s_e <= 6 - se
116 // 3 * (se - 4)^2 + (6 - se)^2
117 // is quadratic, has value 12 at 3 and 6, and value < 12 in between.
118 // Therefore, above "better" check works!
135 inflection point is always at (2s + e - 3) / (3s + 3e - 6).
137 s + e - 2 == 0: no inflection
140 0 < inflection < 1 if:
141 0 < 2s + e - 3 < 3s + 3e - 6
142 2s + e > 3 and 2e + s > 3
145 0 < inflection < 1 if:
146 0 > 2s + e - 3 > 3s + 3e - 6
147 2s + e < 3 and 2e + s < 3
149 Therefore: there is an inflection point iff:
150 e outside (3 - s)/2 .. 3 - s*2
152 in other words, if (s,e) in triangle (1,1)(0,3)(0,1.5) or in triangle (1,1)(3,0)(1.5,0)
156 /** continuous function mapping all reals into -1..1 */
157 float float2range11(float f)
159 return f / (fabs(f) + 1);
162 /** continuous function mapping all reals into 0..1 */
163 float float2range01(float f)
165 return 0.5 + 0.5 * float2range11(f);
168 float median(float a, float b, float c)
170 return (a < c) ? bound(a, b, c) : bound(c, b, a);
173 float almost_equals(float a, float b)
175 float eps = (max(a, -a) + max(b, -b)) * 0.001;
176 return a - b < eps && b - a < eps;
179 float almost_in_bounds(float a, float b, float c)
181 float eps = (max(a, -a) + max(c, -c)) * 0.001;
182 if (a > c) eps = -eps;
183 return b == median(a - eps, b, c + eps);
186 float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d)
188 if (halflifedist > 0) return pow(0.5, (bound(mindist, d, maxdist) - mindist) / halflifedist);
189 else if (halflifedist < 0) return pow(0.5, (bound(mindist, d, maxdist) - maxdist) / halflifedist);
193 float power2of(float e)
198 float log2of(float e)
200 // NOTE: generated code
204 if (e > 4194304) return 23;
206 if (e > 2097152) return 22;
209 if (e > 524288) return 20;
211 if (e > 262144) return 19;
215 if (e > 65536) return 17;
217 if (e > 32768) return 16;
220 if (e > 8192) return 14;
222 if (e > 4096) return 13;
227 if (e > 1024) return 11;
229 if (e > 512) return 10;
232 if (e > 128) return 8;
234 if (e > 64) return 7;
238 if (e > 16) return 5;
249 /** ax^2 + bx + c = 0 */
250 vector solve_quadratic(float a, float b, float c)
266 // actually, every number solves the equation!
273 D = b * b - 4 * a * c;
277 if (a > 0) // put the smaller solution first
279 v.x = ((-b) - D) / (2 * a);
280 v.y = ((-b) + D) / (2 * a);
284 v.x = (-b + D) / (2 * a);
285 v.y = (-b - D) / (2 * a);
291 // complex solutions!
294 if (a > 0) v.y = D / (2 * a);
295 else v.y = -D / (2 * a);