4 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
6 if (weight == 0) return;
7 if (mean == 0) e.(a) *= pow(value, weight);
8 else e.(a) += pow(value, mean) * weight;
12 float mean_evaluate(entity e, .float a, .float c, float mean)
14 if (e.(c) == 0) return 0;
15 if (mean == 0) return pow(e.(a), 1.0 / e.(c));
16 else return pow(e.(a) / e.(c), 1.0 / mean);
19 #define MEAN_ACCUMULATE(s, prefix, v, w) mean_accumulate(s, prefix##_accumulator, prefix##_count, prefix##_mean, v, w)
20 #define MEAN_EVALUATE(s, prefix) mean_evaluate(s, prefix##_accumulator, prefix##_count, prefix##_mean)
21 #define MEAN_DECLARE(prefix, m) float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
23 /** Returns a random number between -1.0 and 1.0 */
24 #define crandom() (2 * (random() - 0.5))
29 Angc used for animations
34 float angc(float a1, float a2)
52 float fsnap(float val, float fsize)
54 return rint(val / fsize) * fsize;
57 vector vsnap(vector point, float fsize)
61 vret.x = rint(point.x / fsize) * fsize;
62 vret.y = rint(point.y / fsize) * fsize;
63 vret.z = ceil(point.z / fsize) * fsize;
68 vector lerpv(float t0, vector v0, float t1, vector v1, float t)
70 return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
73 vector bezier_quadratic_getpoint(vector a, vector b, vector c, float t)
75 return (c - 2 * b + a) * (t * t)
80 vector bezier_quadratic_getderivative(vector a, vector b, vector c, float t)
82 return (c - 2 * b + a) * (2 * t)
86 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float x)
88 return (((startspeedfactor + endspeedfactor - 2
89 ) * x - 2 * startspeedfactor - endspeedfactor + 3
90 ) * x + startspeedfactor
94 bool cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor)
96 if (startspeedfactor < 0 || endspeedfactor < 0) return false;
99 // if this is the case, the possible zeros of the first derivative are outside
101 We can calculate this condition as condition
106 // better, see below:
107 if (startspeedfactor <= 3 && endspeedfactor <= 3) return true;
109 // if this is the case, the first derivative has no zeros at all
110 float se = startspeedfactor + endspeedfactor;
111 float s_e = startspeedfactor - endspeedfactor;
112 if (3 * (se - 4) * (se - 4) + s_e * s_e <= 12) // an ellipse
115 // Now let s <= 3, s <= 3, s+e >= 3 (triangle) then we get se <= 6 (top right corner).
116 // we also get s_e <= 6 - se
117 // 3 * (se - 4)^2 + (6 - se)^2
118 // is quadratic, has value 12 at 3 and 6, and value < 12 in between.
119 // Therefore, above "better" check works!
136 inflection point is always at (2s + e - 3) / (3s + 3e - 6).
138 s + e - 2 == 0: no inflection
141 0 < inflection < 1 if:
142 0 < 2s + e - 3 < 3s + 3e - 6
143 2s + e > 3 and 2e + s > 3
146 0 < inflection < 1 if:
147 0 > 2s + e - 3 > 3s + 3e - 6
148 2s + e < 3 and 2e + s < 3
150 Therefore: there is an inflection point iff:
151 e outside (3 - s)/2 .. 3 - s*2
153 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)
157 /** continuous function mapping all reals into -1..1 */
158 float float2range11(float f)
160 return f / (fabs(f) + 1);
163 /** continuous function mapping all reals into 0..1 */
164 float float2range01(float f)
166 return 0.5 + 0.5 * float2range11(f);
169 float median(float a, float b, float c)
171 return (a < c) ? bound(a, b, c) : bound(c, b, a);
174 float almost_equals(float a, float b)
176 float eps = (max(a, -a) + max(b, -b)) * 0.001;
177 return a - b < eps && b - a < eps;
180 float almost_in_bounds(float a, float b, float c)
182 float eps = (max(a, -a) + max(c, -c)) * 0.001;
183 if (a > c) eps = -eps;
184 return b == median(a - eps, b, c + eps);
187 float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d)
189 if (halflifedist > 0) return pow(0.5, (bound(mindist, d, maxdist) - mindist) / halflifedist);
190 else if (halflifedist < 0) return pow(0.5, (bound(mindist, d, maxdist) - maxdist) / halflifedist);
194 float power2of(float e)
199 float log2of(float x)
201 // NOTE: generated code
205 if (x > 4194304) return 23;
207 if (x > 2097152) return 22;
210 if (x > 524288) return 20;
212 if (x > 262144) return 19;
216 if (x > 65536) return 17;
218 if (x > 32768) return 16;
221 if (x > 8192) return 14;
223 if (x > 4096) return 13;
228 if (x > 1024) return 11;
230 if (x > 512) return 10;
233 if (x > 128) return 8;
235 if (x > 64) return 7;
239 if (x > 16) return 5;
250 /** ax^2 + bx + c = 0 */
251 vector solve_quadratic(float a, float b, float c)
267 // actually, every number solves the equation!
274 D = b * b - 4 * a * c;
278 if (a > 0) // put the smaller solution first
280 v.x = ((-b) - D) / (2 * a);
281 v.y = ((-b) + D) / (2 * a);
285 v.x = (-b + D) / (2 * a);
286 v.y = (-b - D) / (2 * a);
292 // complex solutions!
295 if (a > 0) v.y = D / (2 * a);
296 else v.y = -D / (2 * a);