]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/math.qh
Disable cl_useenginerefdef by default so that the QC implementation is used
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / math.qh
1 #pragma once
2
3 #include "lib/float.qh"
4
5 ERASEABLE
6 void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
7 {
8         if (weight == 0) return;
9         if (mean == 0) e.(a) *= (value ** weight);
10         else e.(a) += (value ** mean) * weight;
11         e.(c) += weight;
12 }
13
14 ERASEABLE
15 float mean_evaluate(entity e, .float a, .float c, float mean)
16 {
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));
20 }
21
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
25
26 /** Returns a random number between -1.0 and 1.0 */
27 #define crandom() (2 * (random() - 0.5))
28
29
30 /*
31 ==================
32 Angc used for animations
33 ==================
34 */
35
36
37 ERASEABLE
38 float angc(float a1, float a2)
39 {
40         while (a1 > 180)
41                 a1 -= 360;
42         while (a1 < -179)
43                 a1 += 360;
44         while (a2 > 180)
45                 a2 -= 360;
46         while (a2 < -179)
47                 a2 += 360;
48         float a = a1 - a2;
49         while (a > 180)
50                 a -= 360;
51         while (a < -179)
52                 a += 360;
53         return a;
54 }
55
56 ERASEABLE
57 float fsnap(float val, float fsize)
58 {
59         return rint(val / fsize) * fsize;
60 }
61
62 ERASEABLE
63 vector vsnap(vector point, float fsize)
64 {
65         vector vret;
66
67         vret.x = rint(point.x / fsize) * fsize;
68         vret.y = rint(point.y / fsize) * fsize;
69         vret.z = ceil(point.z / fsize) * fsize;
70
71         return vret;
72 }
73
74 ERASEABLE
75 vector lerpv(float t0, vector v0, float t1, vector v1, float t)
76 {
77         return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
78 }
79
80 ERASEABLE
81 vector bezier_quadratic_getpoint(vector a, vector b, vector c, float t)
82 {
83         return (c - 2 * b + a) * (t * t)
84                + (b - a) * (2 * t)
85                + a;
86 }
87
88 ERASEABLE
89 vector bezier_quadratic_getderivative(vector a, vector b, vector c, float t)
90 {
91         return (c - 2 * b + a) * (2 * t)
92                + (b - a) * 2;
93 }
94
95 ERASEABLE
96 float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float spd)
97 {
98         return (((startspeedfactor + endspeedfactor - 2
99                  ) * spd - 2 * startspeedfactor - endspeedfactor + 3
100                 ) * spd + startspeedfactor
101                ) * spd;
102 }
103
104 ERASEABLE
105 bool cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor)
106 {
107         if (startspeedfactor < 0 || endspeedfactor < 0) return false;
108
109         /*
110         // if this is the case, the possible zeros of the first derivative are outside
111         // 0..1
112         We can calculate this condition as condition
113         if(se <= 3)
114             return true;
115         */
116
117         // better, see below:
118         if (startspeedfactor <= 3 && endspeedfactor <= 3) return true;
119
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
124                 return true;
125
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!
131
132         return false;
133
134         // known good cases:
135         // (0, [0..3])
136         // (0.5, [0..3.8])
137         // (1, [0..4])
138         // (1.5, [0..3.9])
139         // (2, [0..3.7])
140         // (2.5, [0..3.4])
141         // (3, [0..3])
142         // (3.5, [0.2..2.3])
143         // (4, 1)
144
145         /*
146            On another note:
147            inflection point is always at (2s + e - 3) / (3s + 3e - 6).
148
149            s + e - 2 == 0: no inflection
150
151            s + e > 2:
152            0 < inflection < 1 if:
153            0 < 2s + e - 3 < 3s + 3e - 6
154            2s + e > 3 and 2e + s > 3
155
156            s + e < 2:
157            0 < inflection < 1 if:
158            0 > 2s + e - 3 > 3s + 3e - 6
159            2s + e < 3 and 2e + s < 3
160
161            Therefore: there is an inflection point iff:
162            e outside (3 - s)/2 .. 3 - s*2
163
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)
165         */
166 }
167
168 /** continuous function mapping all reals into -1..1 */
169 ERASEABLE
170 float float2range11(float f)
171 {
172         return f / (fabs(f) + 1);
173 }
174
175 /** continuous function mapping all reals into 0..1 */
176 ERASEABLE
177 float float2range01(float f)
178 {
179         return 0.5 + 0.5 * float2range11(f);
180 }
181
182 ERASEABLE
183 float median(float a, float b, float c)
184 {
185         return (a < c) ? bound(a, b, c) : bound(c, b, a);
186 }
187
188 ERASEABLE
189 float almost_equals(float a, float b)
190 {
191         float eps = (max(a, -a) + max(b, -b)) * 0.001;
192         return a - b < eps && b - a < eps;
193 }
194
195 ERASEABLE
196 float almost_equals_eps(float a, float b, float times_eps)
197 {
198         float eps = max(fabs(a), fabs(b)) * FLOAT_EPSILON * times_eps;
199         return a - b < eps && b - a < eps;
200 }
201
202 ERASEABLE
203 float almost_in_bounds(float a, float b, float c)
204 {
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);
208 }
209
210 ERASEABLE
211 float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d)
212 {
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));
215         else return 1;
216 }
217
218 #define power2of(e) (2 ** e)
219
220 ERASEABLE
221 float log2of(float e)
222 {
223         // NOTE: generated code
224         if (e > 2048)
225                 if (e > 131072)
226                         if (e > 1048576)
227                                 if (e > 4194304) return 23;
228                                 else
229                                         if (e > 2097152) return 22;
230                                         else return 21;
231                         else
232                                 if (e > 524288) return 20;
233                                 else
234                                         if (e > 262144) return 19;
235                                         else return 18;
236                 else
237                         if (e > 16384)
238                                 if (e > 65536) return 17;
239                                 else
240                                         if (e > 32768) return 16;
241                                         else return 15;
242                         else
243                                 if (e > 8192) return 14;
244                                 else
245                                         if (e > 4096) return 13;
246                                         else return 12;
247         else
248                 if (e > 32)
249                         if (e > 256)
250                                 if (e > 1024) return 11;
251                                 else
252                                         if (e > 512) return 10;
253                                         else return 9;
254                         else
255                                 if (e > 128) return 8;
256                                 else
257                                         if (e > 64) return 7;
258                                         else return 6;
259                 else
260                         if (e > 4)
261                                 if (e > 16) return 5;
262                                 else
263                                         if (e > 8) return 4;
264                                         else return 3;
265                         else
266                                 if (e > 2) return 2;
267                                 else
268                                         if (e > 1) return 1;
269                                         else return 0;
270 }
271
272 /** ax^2 + bx + c = 0 */
273 ERASEABLE
274 vector solve_quadratic(float a, float b, float c)
275 {
276         vector v;
277         float D;
278         v = '0 0 0';
279         if (a == 0)
280         {
281                 if (b != 0)
282                 {
283                         v.x = v.y = -c / b;
284                         v.z = 1;
285                 }
286                 else
287                 {
288                         if (c == 0)
289                         {
290                                 // actually, every number solves the equation!
291                                 v.z = 1;
292                         }
293                 }
294         }
295         else
296         {
297                 D = b * b - 4 * a * c;
298                 if (D >= 0)
299                 {
300                         D = sqrt(D);
301                         if (a > 0)  // put the smaller solution first
302                         {
303                                 v.x = ((-b) - D) / (2 * a);
304                                 v.y = ((-b) + D) / (2 * a);
305                         }
306                         else
307                         {
308                                 v.x = (-b + D) / (2 * a);
309                                 v.y = (-b - D) / (2 * a);
310                         }
311                         v.z = 1;
312                 }
313                 else
314                 {
315                         // complex solutions!
316                         D = sqrt(-D);
317                         v.x = -b / (2 * a);
318                         if (a > 0) v.y =  D / (2 * a);
319                         else v.y = -D / (2 * a);
320                         v.z = 0;
321                 }
322         }
323         return v;
324 }
325
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 corresponding values between and extrapolates for values outside the range.
328 ///
329 /// src_min and src_max must not be the same or division by zero occurs.
330 ///
331 /// dest_max can be smaller than dest_min if you want the resulting range to be inverted, all values can be negative.
332 ERASEABLE
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;
338 }
339
340 /// Same as `map_ranges` except that values outside the source range are clamped to min or max.
341 ERASEABLE
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);
346 }