4 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
9 e.(a) *= pow(value, weight);
11 e.(a) += pow(value, mean) * weight;
15 float mean_evaluate(entity e, .float a, .float c, float mean)
20 return pow(e.(a), 1.0 / e.(c));
22 return pow(e.(a) / e.(c), 1.0 / mean);
25 #define MEAN_ACCUMULATE(prefix,v,w) mean_accumulate(self,prefix##_accumulator,prefix##_count,prefix##_mean,v,w)
26 #define MEAN_EVALUATE(prefix) mean_evaluate(self,prefix##_accumulator,prefix##_count,prefix##_mean)
27 #define MEAN_DECLARE(prefix,m) float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
33 Returns a random number between -1.0 and 1.0
38 return 2 * (random() - 0.5);
44 Angc used for animations
49 float angc(float a1, float a2)
51 while (a1 > 180) a1 -= 360;
52 while (a1 < -179) a1 += 360;
53 while (a2 > 180) a2 -= 360;
54 while (a2 < -179) a2 += 360;
56 while (a > 180) a -= 360;
57 while (a < -179) a += 360;
61 float fsnap(float val,float fsize)
63 return rint(val / fsize) * fsize;
66 vector vsnap(vector point,float fsize)
70 vret.x = rint(point.x / fsize) * fsize;
71 vret.y = rint(point.y / fsize) * fsize;
72 vret.z = ceil(point.z / fsize) * fsize;
77 vector lerpv(float t0, vector v0, float t1, vector v1, float t)
79 return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
82 vector bezier_quadratic_getpoint(vector a, vector b, vector c, float t)
85 (c - 2 * b + a) * (t * t) +
90 vector bezier_quadratic_getderivative(vector a, vector b, vector c, float t)
93 (c - 2 * b + a) * (2 * t) +
97 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float x)
100 ((( startspeedfactor + endspeedfactor - 2
101 ) * x - 2 * startspeedfactor - endspeedfactor + 3
102 ) * x + startspeedfactor
106 bool cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor)
108 if (startspeedfactor < 0 || endspeedfactor < 0)
112 // if this is the case, the possible zeros of the first derivative are outside
114 We can calculate this condition as condition
119 // better, see below:
120 if (startspeedfactor <= 3 && endspeedfactor <= 3)
123 // if this is the case, the first derivative has no zeros at all
124 float se = startspeedfactor + endspeedfactor;
125 float s_e = startspeedfactor - endspeedfactor;
126 if (3 * (se - 4) * (se - 4) + s_e * s_e <= 12) // an ellipse
129 // Now let s <= 3, s <= 3, s+e >= 3 (triangle) then we get se <= 6 (top right corner).
130 // we also get s_e <= 6 - se
131 // 3 * (se - 4)^2 + (6 - se)^2
132 // is quadratic, has value 12 at 3 and 6, and value < 12 in between.
133 // Therefore, above "better" check works!
150 inflection point is always at (2s + e - 3) / (3s + 3e - 6).
152 s + e - 2 == 0: no inflection
155 0 < inflection < 1 if:
156 0 < 2s + e - 3 < 3s + 3e - 6
157 2s + e > 3 and 2e + s > 3
160 0 < inflection < 1 if:
161 0 > 2s + e - 3 > 3s + 3e - 6
162 2s + e < 3 and 2e + s < 3
164 Therefore: there is an inflection point iff:
165 e outside (3 - s)/2 .. 3 - s*2
167 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)
171 /** continuous function mapping all reals into -1..1 */
172 float float2range11(float f)
174 return f / (fabs(f) + 1);
177 /** continuous function mapping all reals into 0..1 */
178 float float2range01(float f)
180 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);
188 float almost_equals(float a, float b)
190 float eps = (max(a, -a) + max(b, -b)) * 0.001;
191 return a - b < eps && b - a < eps;
194 float almost_in_bounds(float a, float b, float c)
196 float eps = (max(a, -a) + max(c, -c)) * 0.001;
199 return b == median(a - eps, b, c + eps);
202 float power2of(float e)
207 float log2of(float x)
209 // NOTE: generated code