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