3 #include "lib/float.qh"
5 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
7 if (weight == 0) return;
8 if (mean == 0) e.(a) *= (value ** weight);
9 else e.(a) += (value ** mean) * weight;
13 float mean_evaluate(entity e, .float a, .float c, float mean)
15 if (e.(c) == 0) return 0;
16 if (mean == 0) return (e.(a) ** (1.0 / e.(c)));
17 else return ((e.(a) / e.(c)) ** (1.0 / mean));
20 #define MEAN_ACCUMULATE(s, prefix, v, w) mean_accumulate(s, prefix##_accumulator, prefix##_count, prefix##_mean, v, w)
21 #define MEAN_EVALUATE(s, prefix) mean_evaluate(s, prefix##_accumulator, prefix##_count, prefix##_mean)
22 #define MEAN_DECLARE(prefix, m) float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
24 /** Returns a random number between -1.0 and 1.0 */
25 #define crandom() (2 * (random() - 0.5))
30 Angc used for animations
35 float angc(float a1, float a2)
53 float fsnap(float val, float fsize)
55 return rint(val / fsize) * fsize;
58 vector vsnap(vector point, float fsize)
62 vret.x = rint(point.x / fsize) * fsize;
63 vret.y = rint(point.y / fsize) * fsize;
64 vret.z = ceil(point.z / fsize) * fsize;
69 vector lerpv(float t0, vector v0, float t1, vector v1, float t)
71 return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
74 vector bezier_quadratic_getpoint(vector a, vector b, vector c, float t)
76 return (c - 2 * b + a) * (t * t)
81 vector bezier_quadratic_getderivative(vector a, vector b, vector c, float t)
83 return (c - 2 * b + a) * (2 * t)
87 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float spd)
89 return (((startspeedfactor + endspeedfactor - 2
90 ) * spd - 2 * startspeedfactor - endspeedfactor + 3
91 ) * spd + startspeedfactor
95 bool cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor)
97 if (startspeedfactor < 0 || endspeedfactor < 0) return false;
100 // if this is the case, the possible zeros of the first derivative are outside
102 We can calculate this condition as condition
107 // better, see below:
108 if (startspeedfactor <= 3 && endspeedfactor <= 3) return true;
110 // if this is the case, the first derivative has no zeros at all
111 float se = startspeedfactor + endspeedfactor;
112 float s_e = startspeedfactor - endspeedfactor;
113 if (3 * (se - 4) * (se - 4) + s_e * s_e <= 12) // an ellipse
116 // Now let s <= 3, s <= 3, s+e >= 3 (triangle) then we get se <= 6 (top right corner).
117 // we also get s_e <= 6 - se
118 // 3 * (se - 4)^2 + (6 - se)^2
119 // is quadratic, has value 12 at 3 and 6, and value < 12 in between.
120 // Therefore, above "better" check works!
137 inflection point is always at (2s + e - 3) / (3s + 3e - 6).
139 s + e - 2 == 0: no inflection
142 0 < inflection < 1 if:
143 0 < 2s + e - 3 < 3s + 3e - 6
144 2s + e > 3 and 2e + s > 3
147 0 < inflection < 1 if:
148 0 > 2s + e - 3 > 3s + 3e - 6
149 2s + e < 3 and 2e + s < 3
151 Therefore: there is an inflection point iff:
152 e outside (3 - s)/2 .. 3 - s*2
154 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)
158 /** continuous function mapping all reals into -1..1 */
159 float float2range11(float f)
161 return f / (fabs(f) + 1);
164 /** continuous function mapping all reals into 0..1 */
165 float float2range01(float f)
167 return 0.5 + 0.5 * float2range11(f);
170 float median(float a, float b, float c)
172 return (a < c) ? bound(a, b, c) : bound(c, b, a);
175 float almost_equals(float a, float b)
177 float eps = (max(a, -a) + max(b, -b)) * 0.001;
178 return a - b < eps && b - a < eps;
181 float almost_equals_eps(float a, float b, float times_eps)
183 float eps = max(fabs(a), fabs(b)) * FLOAT_EPSILON * times_eps;
184 return a - b < eps && b - a < eps;
187 float almost_in_bounds(float a, float b, float c)
189 float eps = (max(a, -a) + max(c, -c)) * 0.001;
190 if (a > c) eps = -eps;
191 return b == median(a - eps, b, c + eps);
194 float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d)
196 if (halflifedist > 0) return (0.5 ** ((bound(mindist, d, maxdist) - mindist) / halflifedist));
197 else if (halflifedist < 0) return (0.5 ** ((bound(mindist, d, maxdist) - maxdist) / halflifedist));
201 float power2of(float e)
206 float log2of(float e)
208 // NOTE: generated code
212 if (e > 4194304) return 23;
214 if (e > 2097152) return 22;
217 if (e > 524288) return 20;
219 if (e > 262144) return 19;
223 if (e > 65536) return 17;
225 if (e > 32768) return 16;
228 if (e > 8192) return 14;
230 if (e > 4096) return 13;
235 if (e > 1024) return 11;
237 if (e > 512) return 10;
240 if (e > 128) return 8;
242 if (e > 64) return 7;
246 if (e > 16) return 5;
257 /** ax^2 + bx + c = 0 */
258 vector solve_quadratic(float a, float b, float c)
274 // actually, every number solves the equation!
281 D = b * b - 4 * a * c;
285 if (a > 0) // put the smaller solution first
287 v.x = ((-b) - D) / (2 * a);
288 v.y = ((-b) + D) / (2 * a);
292 v.x = (-b + D) / (2 * a);
293 v.y = (-b - D) / (2 * a);
299 // complex solutions!
302 if (a > 0) v.y = D / (2 * a);
303 else v.y = -D / (2 * a);