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 vector lerpv(float t0, vector v0, float t1, vector v1, float t)
77 return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
81 vector bezier_quadratic_getpoint(vector a, vector b, vector c, float t)
83 return (c - 2 * b + a) * (t * t)
89 vector bezier_quadratic_getderivative(vector a, vector b, vector c, float t)
91 return (c - 2 * b + a) * (2 * t)
96 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float spd)
98 return (((startspeedfactor + endspeedfactor - 2
99 ) * spd - 2 * startspeedfactor - endspeedfactor + 3
100 ) * spd + startspeedfactor
105 bool cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor)
107 if (startspeedfactor < 0 || endspeedfactor < 0) return false;
110 // if this is the case, the possible zeros of the first derivative are outside
112 We can calculate this condition as condition
117 // better, see below:
118 if (startspeedfactor <= 3 && endspeedfactor <= 3) return true;
120 // if this is the case, the first derivative has no zeros at all
121 float se = startspeedfactor + endspeedfactor;
122 float s_e = startspeedfactor - endspeedfactor;
123 if (3 * (se - 4) * (se - 4) + s_e * s_e <= 12) // an ellipse
126 // Now let s <= 3, s <= 3, s+e >= 3 (triangle) then we get se <= 6 (top right corner).
127 // we also get s_e <= 6 - se
128 // 3 * (se - 4)^2 + (6 - se)^2
129 // is quadratic, has value 12 at 3 and 6, and value < 12 in between.
130 // Therefore, above "better" check works!
147 inflection point is always at (2s + e - 3) / (3s + 3e - 6).
149 s + e - 2 == 0: no inflection
152 0 < inflection < 1 if:
153 0 < 2s + e - 3 < 3s + 3e - 6
154 2s + e > 3 and 2e + s > 3
157 0 < inflection < 1 if:
158 0 > 2s + e - 3 > 3s + 3e - 6
159 2s + e < 3 and 2e + s < 3
161 Therefore: there is an inflection point iff:
162 e outside (3 - s)/2 .. 3 - s*2
164 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)
168 /** continuous function mapping all reals into -1..1 */
170 float float2range11(float f)
172 return f / (fabs(f) + 1);
175 /** continuous function mapping all reals into 0..1 */
177 float float2range01(float f)
179 return 0.5 + 0.5 * float2range11(f);
183 float median(float a, float b, float c)
185 return (a < c) ? bound(a, b, c) : bound(c, b, a);
189 float almost_equals(float a, float b)
191 float eps = (max(a, -a) + max(b, -b)) * 0.001;
192 return a - b < eps && b - a < eps;
196 float almost_equals_eps(float a, float b, float times_eps)
198 float eps = max(fabs(a), fabs(b)) * FLOAT_EPSILON * times_eps;
199 return a - b < eps && b - a < eps;
203 float almost_in_bounds(float a, float b, float c)
205 float eps = (max(a, -a) + max(c, -c)) * 0.001;
206 if (a > c) eps = -eps;
207 return b == median(a - eps, b, c + eps);
211 float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d)
213 if (halflifedist > 0) return (0.5 ** ((bound(mindist, d, maxdist) - mindist) / halflifedist));
214 else if (halflifedist < 0) return (0.5 ** ((bound(mindist, d, maxdist) - maxdist) / halflifedist));
218 #define power2of(e) (2 ** e)
221 float log2of(float e)
223 // NOTE: generated code
227 if (e > 4194304) return 23;
229 if (e > 2097152) return 22;
232 if (e > 524288) return 20;
234 if (e > 262144) return 19;
238 if (e > 65536) return 17;
240 if (e > 32768) return 16;
243 if (e > 8192) return 14;
245 if (e > 4096) return 13;
250 if (e > 1024) return 11;
252 if (e > 512) return 10;
255 if (e > 128) return 8;
257 if (e > 64) return 7;
261 if (e > 16) return 5;
272 /** ax^2 + bx + c = 0 */
274 vector solve_quadratic(float a, float b, float c)
290 // actually, every number solves the equation!
297 D = b * b - 4 * a * c;
301 if (a > 0) // put the smaller solution first
303 v.x = ((-b) - D) / (2 * a);
304 v.y = ((-b) + D) / (2 * a);
308 v.x = (-b + D) / (2 * a);
309 v.y = (-b - D) / (2 * a);
315 // complex solutions!
318 if (a > 0) v.y = D / (2 * a);
319 else v.y = -D / (2 * a);
326 /// Maps values between the src and dest range: src_min to dest_min, src_max to dest_max, values between them
327 /// to the curresponding values between and extrapolates for values outside the range.
329 /// src_min and src_max must not be the same or division by zero accurs.
331 /// dest_max can be smaller than dest_min if you want the resulting range to be inverted, all values can be negative.
333 float map_ranges(float value, float src_min, float src_max, float dest_min, float dest_max) {
334 float src_diff = src_max - src_min;
335 float dest_diff = dest_max - dest_min;
336 float ratio = (value - src_min) / src_diff;
337 return dest_min + dest_diff * ratio;
340 /// Same as `map_ranges` except that values outside the source range are clamped to min or max.
342 float map_bound_ranges(float value, float src_min, float src_max, float dest_min, float dest_max) {
343 if (value <= src_min) return dest_min;
344 if (value >= src_max) return dest_max;
345 return map_ranges(value, src_min, src_max, dest_min, dest_max);