Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
math.qh
Go to the documentation of this file.
1#pragma once
2
3#include "lib/float.qh"
4
6void mean_accumulate(entity e, .float a, .float c, float mean, float value, float weight)
7{
8 if (weight == 0)
9 return;
10 if (mean == 0)
11 e.(a) *= (value ** weight);
12 else
13 e.(a) += (value ** mean) * weight;
14 e.(c) += weight;
15}
16
18float mean_evaluate(entity e, .float a, .float c, float mean)
19{
20 if (e.(c) == 0)
21 return 0;
22 if (mean == 0)
23 return (e.(a) ** (1.0 / e.(c)));
24 return ((e.(a) / e.(c)) ** (1.0 / mean));
25}
26
27#define MEAN_ACCUMULATE(s, prefix, v, w) mean_accumulate(s, prefix##_accumulator, prefix##_count, prefix##_mean, v, w)
28#define MEAN_EVALUATE(s, prefix) mean_evaluate(s, prefix##_accumulator, prefix##_count, prefix##_mean)
29#define MEAN_DECLARE(prefix, m) float prefix##_mean = m; .float prefix##_count, prefix##_accumulator
30
31
32/*
33==================
34Angc used for animations
35==================
36*/
37
38
40float angc(float a1, float a2)
41{
42 while (a1 > 180)
43 a1 -= 360;
44 while (a1 < -179)
45 a1 += 360;
46 while (a2 > 180)
47 a2 -= 360;
48 while (a2 < -179)
49 a2 += 360;
50 float a = a1 - a2;
51 while (a > 180)
52 a -= 360;
53 while (a < -179)
54 a += 360;
55 return a;
56}
57
59float fsnap(float val, float fsize)
60{
61 return rint(val / fsize) * fsize;
62}
63
65vector vsnap(vector point, float fsize)
66{
67 vector vret;
68 vret.x = rint(point.x / fsize) * fsize;
69 vret.y = rint(point.y / fsize) * fsize;
70 vret.z = ceil(point.z / fsize) * fsize;
71 return vret;
72}
73
75float lerpratio(float f0, float f1, float ratio)
76{
77 return f0 * (1 - ratio) + f1 * ratio;
78}
79
81float lerp(float t0, float f0, float t1, float f1, float t)
82{
83 return lerpratio(f0, f1, (t - t0) / (t1 - t0));
84}
85
87float lerp3ratio(float f0, float f1, float f2, float ratio)
88{
89 float mid = 0.5;
90 return ratio < mid ? lerpratio(f0, f1, ratio / mid) : ratio > mid ? lerpratio(f1, f2, (ratio - mid) / mid) : f1;
91}
92
93
95vector lerpvratio(vector f0, vector f1, float ratio)
96{
97 return f0 * (1 - ratio) + f1 * ratio;
98}
99
102{
103 float mid = 0.5;
104 return ratio < mid ? lerpvratio(f0, f1, ratio / mid) : ratio > mid ? lerpvratio(f1, f2, (ratio - mid) / mid) : f1;
105}
106
108vector lerpv(float t0, vector v0, float t1, vector v1, float t)
109{
110 return v0 + (v1 - v0) * ((t - t0) / (t1 - t0));
111}
112
115{
116 return (c - 2 * b + a) * (t * t)
117 + (b - a) * (2 * t)
118 + a;
119}
120
123{
124 return (c - 2 * b + a) * (2 * t)
125 + (b - a) * 2;
126}
127
129float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float spd)
130{
131 return (((startspeedfactor + endspeedfactor - 2
132 ) * spd - 2 * startspeedfactor - endspeedfactor + 3
133 ) * spd + startspeedfactor
134 ) * spd;
135}
136
138bool cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor)
139{
140 if (startspeedfactor < 0 || endspeedfactor < 0)
141 return false;
142
143 /*
144 // if this is the case, the possible zeros of the first derivative are outside
145 // 0..1
146 We can calculate this condition as condition
147 if (se <= 3)
148 return true;
149 */
150
151 // better, see below:
152 if (startspeedfactor <= 3 && endspeedfactor <= 3)
153 return true;
154
155 // if this is the case, the first derivative has no zeros at all
156 float se = startspeedfactor + endspeedfactor;
157 float s_e = startspeedfactor - endspeedfactor;
158 if (3 * (se - 4) * (se - 4) + s_e * s_e <= 12) // an ellipse
159 return true;
160
161 // Now let s <= 3, s <= 3, s+e >= 3 (triangle) then we get se <= 6 (top right corner).
162 // we also get s_e <= 6 - se
163 // 3 * (se - 4)^2 + (6 - se)^2
164 // is quadratic, has value 12 at 3 and 6, and value < 12 in between.
165 // Therefore, above "better" check works!
166
167 return false;
168
169 // known good cases:
170 // (0, [0..3])
171 // (0.5, [0..3.8])
172 // (1, [0..4])
173 // (1.5, [0..3.9])
174 // (2, [0..3.7])
175 // (2.5, [0..3.4])
176 // (3, [0..3])
177 // (3.5, [0.2..2.3])
178 // (4, 1)
179
180 /*
181 On another note:
182 inflection point is always at (2s + e - 3) / (3s + 3e - 6).
183
184 s + e - 2 == 0: no inflection
185
186 s + e > 2:
187 0 < inflection < 1 if:
188 0 < 2s + e - 3 < 3s + 3e - 6
189 2s + e > 3 and 2e + s > 3
190
191 s + e < 2:
192 0 < inflection < 1 if:
193 0 > 2s + e - 3 > 3s + 3e - 6
194 2s + e < 3 and 2e + s < 3
195
196 Therefore: there is an inflection point iff:
197 e outside (3 - s)/2 .. 3 - s*2
198
199 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)
200 */
201}
202
205float float2range11(float f)
206{
207 return f / (fabs(f) + 1);
208}
209
212float float2range01(float f)
213{
214 return 0.5 + 0.5 * float2range11(f);
215}
216
218float median(float a, float b, float c)
219{
220 return (a < c) ? bound(a, b, c) : bound(c, b, a);
221}
222
224float almost_equals(float a, float b)
225{
226 float eps = (max(a, -a) + max(b, -b)) * 0.001;
227 return a - b < eps && b - a < eps;
228}
229
231float almost_equals_eps(float a, float b, float times_eps)
232{
233 float eps = max(fabs(a), fabs(b)) * FLOAT_EPSILON * times_eps;
234 return a - b < eps && b - a < eps;
235}
236
238float almost_in_bounds(float a, float b, float c)
239{
240 float eps = (max(a, -a) + max(c, -c)) * 0.001;
241 if (a > c)
242 eps = -eps;
243 return b == median(a - eps, b, c + eps);
244}
245
247float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d)
248{
249 if (halflifedist > 0)
250 return (0.5 ** ((bound(mindist, d, maxdist) - mindist) / halflifedist));
251 else if (halflifedist < 0)
252 return (0.5 ** ((bound(mindist, d, maxdist) - maxdist) / halflifedist));
253 return 1;
254}
255
256#define power2of(e) (2 ** e)
257
259float log2of(float e)
260{
261 // NOTE: generated code
262 if (e > 2048)
263 if (e > 131072)
264 if (e > 1048576)
265 if (e > 4194304) return 23;
266 else
267 if (e > 2097152) return 22;
268 else return 21;
269 else
270 if (e > 524288) return 20;
271 else
272 if (e > 262144) return 19;
273 else return 18;
274 else
275 if (e > 16384)
276 if (e > 65536) return 17;
277 else
278 if (e > 32768) return 16;
279 else return 15;
280 else
281 if (e > 8192) return 14;
282 else
283 if (e > 4096) return 13;
284 else return 12;
285 else
286 if (e > 32)
287 if (e > 256)
288 if (e > 1024) return 11;
289 else
290 if (e > 512) return 10;
291 else return 9;
292 else
293 if (e > 128) return 8;
294 else
295 if (e > 64) return 7;
296 else return 6;
297 else
298 if (e > 4)
299 if (e > 16) return 5;
300 else
301 if (e > 8) return 4;
302 else return 3;
303 else
304 if (e > 2) return 2;
305 else
306 if (e > 1) return 1;
307 else return 0;
308}
309
312vector solve_quadratic(float a, float b, float c)
313{
314 vector v;
315 if (a == 0)
316 {
317 if (b != 0)
318 {
319 v.x = v.y = -c / b;
320 v.z = 1;
321 }
322 else if (c == 0)
323 v.z = 1; // actually, every number solves the equation!
324 else
325 v.z = 0;
326 v.x = 0;
327 v.y = 0;
328 }
329 else
330 {
331 float D = b * b - 4 * a * c; // discriminant
332 if (D >= 0)
333 {
334 D = sqrt(D);
335 if (a > 0) // put the smaller solution first
336 {
337 v.x = ((-b) - D) / (2 * a);
338 v.y = ((-b) + D) / (2 * a);
339 }
340 else
341 {
342 v.x = (-b + D) / (2 * a);
343 v.y = (-b - D) / (2 * a);
344 }
345 v.z = 1;
346 }
347 else
348 {
349 // complex solutions!
350 D = sqrt(-D);
351 v.x = -b / (2 * a);
352 if (a > 0)
353 v.y = D / (2 * a);
354 else
355 v.y = -D / (2 * a);
356 v.z = 0;
357 }
358 }
359 return v;
360}
361
367float map_ranges(float value, float src_min, float src_max, float dest_min, float dest_max)
368{
369 float src_diff = src_max - src_min;
370 float dest_diff = dest_max - dest_min;
371 float ratio = (value - src_min) / src_diff;
372 return dest_min + dest_diff * ratio;
373}
374
377float map_bound_ranges(float value, float src_min, float src_max, float dest_min, float dest_max)
378{
379 if (value <= src_min) return dest_min;
380 if (value >= src_max) return dest_max;
381 return map_ranges(value, src_min, src_max, dest_min, dest_max);
382}
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
const float FLOAT_EPSILON
Definition float.qh:4
#define ERASEABLE
Definition _all.inc:37
ERASEABLE float mean_evaluate(entity e,.float a,.float c, float mean)
Definition math.qh:18
ERASEABLE float ExponentialFalloff(float mindist, float maxdist, float halflifedist, float d)
Definition math.qh:247
ERASEABLE float lerpratio(float f0, float f1, float ratio)
Definition math.qh:75
ERASEABLE void mean_accumulate(entity e,.float a,.float c, float mean, float value, float weight)
Definition math.qh:6
ERASEABLE float cubic_speedfunc(float startspeedfactor, float endspeedfactor, float spd)
Definition math.qh:129
ERASEABLE float lerp3ratio(float f0, float f1, float f2, float ratio)
Definition math.qh:87
ERASEABLE float log2of(float e)
Definition math.qh:259
ERASEABLE float map_ranges(float value, float src_min, float src_max, float dest_min, float dest_max)
Maps values between the src and dest range: src_min to dest_min, src_max to dest_max,...
Definition math.qh:367
ERASEABLE float angc(float a1, float a2)
Definition math.qh:40
ERASEABLE float float2range11(float f)
Continuous function mapping all reals into -1..1.
Definition math.qh:205
ERASEABLE vector lerpv3ratio(vector f0, vector f1, vector f2, float ratio)
Definition math.qh:101
ERASEABLE vector vsnap(vector point, float fsize)
Definition math.qh:65
ERASEABLE vector lerpv(float t0, vector v0, float t1, vector v1, float t)
Definition math.qh:108
ERASEABLE float fsnap(float val, float fsize)
Definition math.qh:59
ERASEABLE vector solve_quadratic(float a, float b, float c)
ax^2 + bx + c = 0
Definition math.qh:312
ERASEABLE float float2range01(float f)
Continuous function mapping all reals into 0..1.
Definition math.qh:212
ERASEABLE vector bezier_quadratic_getderivative(vector a, vector b, vector c, float t)
Definition math.qh:122
ERASEABLE float almost_equals(float a, float b)
Definition math.qh:224
ERASEABLE vector bezier_quadratic_getpoint(vector a, vector b, vector c, float t)
Definition math.qh:114
ERASEABLE float median(float a, float b, float c)
Definition math.qh:218
ERASEABLE float map_bound_ranges(float value, float src_min, float src_max, float dest_min, float dest_max)
Same as map_ranges except that values outside the source range are clamped to min or max.
Definition math.qh:377
ERASEABLE bool cubic_speedfunc_is_sane(float startspeedfactor, float endspeedfactor)
Definition math.qh:138
ERASEABLE float almost_in_bounds(float a, float b, float c)
Definition math.qh:238
ERASEABLE vector lerpvratio(vector f0, vector f1, float ratio)
Definition math.qh:95
ERASEABLE float lerp(float t0, float f0, float t1, float f1, float t)
Definition math.qh:81
ERASEABLE float almost_equals_eps(float a, float b, float times_eps)
Definition math.qh:231
float ceil(float f)
float bound(float min, float value, float max)
float sqrt(float f)
float rint(float f)
float fabs(float f)
float max(float f,...)
spree_inf s1 s2 s3loc s2 spree_inf s1 s2 s3loc s2 spree_inf s1 s2 s3loc s2 s1 s2loc s1 s2loc s1 s2loc s1 s2loc s1 s2loc s1 s2loc s1 s2 f1points f2
Definition all.inc:369
f1
Definition all.inc:566
vector
Definition self.qh:96