3 #include "lib/float.qh"
6 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
8 if (weight == 0) return;
9 if (mean == 0) e.(a) *= (value ** weight);
10 else e.(a) += (value ** mean) * weight;
15 float mean_evaluate(entity e, .float a, .float c, float mean)
17 if (e.(c) == 0) return 0;
18 if (mean == 0) return (e.(a) ** (1.0 / e.(c)));
19 else return ((e.(a) / e.(c)) ** (1.0 / mean));
22 #define MEAN_ACCUMULATE(s, prefix, v, w) mean_accumulate(s, prefix##_accumulator, prefix##_count, prefix##_mean, v, w)
23 #define MEAN_EVALUATE(s, prefix) mean_evaluate(s, prefix##_accumulator, prefix##_count, prefix##_mean)
24 #define MEAN_DECLARE(prefix, m) float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
26 /** Returns a random number between -1.0 and 1.0 */
27 #define crandom() (2 * (random() - 0.5))
32 Angc used for animations
38 float angc(float a1, float a2)
57 float fsnap(float val, float fsize)
59 return rint(val / fsize) * fsize;
63 vector vsnap(vector point, float fsize)
67 vret.x = rint(point.x / fsize) * fsize;
68 vret.y = rint(point.y / fsize) * fsize;
69 vret.z = ceil(point.z / fsize) * fsize;
75 float lerpratio(float f0, float f1, float ratio)
77 return f0 * (1 - ratio) + f1 * ratio;
81 float lerp(float t0, float f0, float t1, float f1, float t)
83 return lerpratio(f0, f1, (t - t0) / (t1 - t0));
87 float lerp3ratio(float f0, float f1, float f2, float ratio)
90 return ratio < mid ? lerpratio(f0, f1, ratio / mid) : ratio > mid ? lerpratio(f1, f2, (ratio - mid) / mid) : f1;
95 vector lerpvratio(vector f0, vector f1, float ratio)
97 return f0 * (1 - ratio) + f1 * ratio;
101 vector lerpv3ratio(vector f0, vector f1, vector f2, float ratio)
104 return ratio < mid ? lerpvratio(f0, f1, ratio / mid) : ratio > mid ? lerpvratio(f1, f2, (ratio - mid) / mid) : f1;
108 vector lerpv(float t0, vector v0, float t1, vector v1, float t)
110 return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
114 vector bezier_quadratic_getpoint(vector a, vector b, vector c, float t)
116 return (c - 2 * b + a) * (t * t)
122 vector bezier_quadratic_getderivative(vector a, vector b, vector c, float t)
124 return (c - 2 * b + a) * (2 * t)
129 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float spd)
131 return (((startspeedfactor + endspeedfactor - 2
132 ) * spd - 2 * startspeedfactor - endspeedfactor + 3
133 ) * spd + startspeedfactor
138 bool cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor)
140 if (startspeedfactor < 0 || endspeedfactor < 0) return false;
143 // if this is the case, the possible zeros of the first derivative are outside
145 We can calculate this condition as condition
150 // better, see below:
151 if (startspeedfactor <= 3 && endspeedfactor <= 3) return true;
153 // if this is the case, the first derivative has no zeros at all
154 float se = startspeedfactor + endspeedfactor;
155 float s_e = startspeedfactor - endspeedfactor;
156 if (3 * (se - 4) * (se - 4) + s_e * s_e <= 12) // an ellipse
159 // Now let s <= 3, s <= 3, s+e >= 3 (triangle) then we get se <= 6 (top right corner).
160 // we also get s_e <= 6 - se
161 // 3 * (se - 4)^2 + (6 - se)^2
162 // is quadratic, has value 12 at 3 and 6, and value < 12 in between.
163 // Therefore, above "better" check works!
180 inflection point is always at (2s + e - 3) / (3s + 3e - 6).
182 s + e - 2 == 0: no inflection
185 0 < inflection < 1 if:
186 0 < 2s + e - 3 < 3s + 3e - 6
187 2s + e > 3 and 2e + s > 3
190 0 < inflection < 1 if:
191 0 > 2s + e - 3 > 3s + 3e - 6
192 2s + e < 3 and 2e + s < 3
194 Therefore: there is an inflection point iff:
195 e outside (3 - s)/2 .. 3 - s*2
197 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)
201 /** continuous function mapping all reals into -1..1 */
203 float float2range11(float f)
205 return f / (fabs(f) + 1);
208 /** continuous function mapping all reals into 0..1 */
210 float float2range01(float f)
212 return 0.5 + 0.5 * float2range11(f);
216 float median(float a, float b, float c)
218 return (a < c) ? bound(a, b, c) : bound(c, b, a);
222 float almost_equals(float a, float b)
224 float eps = (max(a, -a) + max(b, -b)) * 0.001;
225 return a - b < eps && b - a < eps;
229 float almost_equals_eps(float a, float b, float times_eps)
231 float eps = max(fabs(a), fabs(b)) * FLOAT_EPSILON * times_eps;
232 return a - b < eps && b - a < eps;
236 float almost_in_bounds(float a, float b, float c)
238 float eps = (max(a, -a) + max(c, -c)) * 0.001;
239 if (a > c) eps = -eps;
240 return b == median(a - eps, b, c + eps);
244 float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d)
246 if (halflifedist > 0) return (0.5 ** ((bound(mindist, d, maxdist) - mindist) / halflifedist));
247 else if (halflifedist < 0) return (0.5 ** ((bound(mindist, d, maxdist) - maxdist) / halflifedist));
251 #define power2of(e) (2 ** e)
254 float log2of(float e)
256 // NOTE: generated code
260 if (e > 4194304) return 23;
262 if (e > 2097152) return 22;
265 if (e > 524288) return 20;
267 if (e > 262144) return 19;
271 if (e > 65536) return 17;
273 if (e > 32768) return 16;
276 if (e > 8192) return 14;
278 if (e > 4096) return 13;
283 if (e > 1024) return 11;
285 if (e > 512) return 10;
288 if (e > 128) return 8;
290 if (e > 64) return 7;
294 if (e > 16) return 5;
305 /** ax^2 + bx + c = 0 */
307 vector solve_quadratic(float a, float b, float c)
323 // actually, every number solves the equation!
330 D = b * b - 4 * a * c;
334 if (a > 0) // put the smaller solution first
336 v.x = ((-b) - D) / (2 * a);
337 v.y = ((-b) + D) / (2 * a);
341 v.x = (-b + D) / (2 * a);
342 v.y = (-b - D) / (2 * a);
348 // complex solutions!
351 if (a > 0) v.y = D / (2 * a);
352 else v.y = -D / (2 * a);
359 /// Maps values between the src and dest range: src_min to dest_min, src_max to dest_max, values between them
360 /// to the corresponding values between and extrapolates for values outside the range.
362 /// src_min and src_max must not be the same or division by zero occurs.
364 /// dest_max can be smaller than dest_min if you want the resulting range to be inverted, all values can be negative.
366 float map_ranges(float value, float src_min, float src_max, float dest_min, float dest_max) {
367 float src_diff = src_max - src_min;
368 float dest_diff = dest_max - dest_min;
369 float ratio = (value - src_min) / src_diff;
370 return dest_min + dest_diff * ratio;
373 /// Same as `map_ranges` except that values outside the source range are clamped to min or max.
375 float map_bound_ranges(float value, float src_min, float src_max, float dest_min, float dest_max) {
376 if (value <= src_min) return dest_min;
377 if (value >= src_max) return dest_max;
378 return map_ranges(value, src_min, src_max, dest_min, dest_max);