forked from fabiolimamp/alkaline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.qc
386 lines (309 loc) · 7.36 KB
/
math.qc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* math.qc
*
* Author: Joshua Skelton joshua.skelton@gmail.com
*
* A collection of helpful math functions.
*/
float mathlib_accuracy;
float M_PI = 3.14159265;
// Forward declarations
float(float value, float minValue, float maxValue) clamp;
float(float a, float b) mod;
float(float x) sign;
float(float value, float minValue, float maxValue) wrap;
vector(float x, float y, float z) Vector =
{
local vector swiz;
swiz_x = x;
swiz_y = y;
swiz_z = z;
return swiz;
}
vector(float num) SinCos =
{
local vector angle, vf, vu, vr, result;
vf = v_forward;
vu = v_up;
vr = v_right;
angle = '0 1 0' * num;
makevectors(angle);
result = [v_forward_y, v_forward_x, v_forward_x / v_forward_y]; // technically SinCosTan
v_forward = vf;
v_up = vu;
v_right = vr;
return result;
}
float(float num) sin =
{
vector temp;
temp = SinCos(num);
return temp_x;
}
float(float num) cos =
{
vector temp;
temp = SinCos(num);
return temp_y;
}
float(float num) tan =
{
vector temp;
temp = SinCos(num);
return temp_x / temp_y;
}
float(float y, float x) atan2 =
{
local vector ang; //temporary used to calculate trig values
ang = '0 0 0';
ang_x = x;
ang_y = y;
return vectoyaw(ang);
}
vector(vector v) vabs =
{
return [fabs(v_x),fabs(v_y),fabs(v_z)];
}
vector(vector a, vector b) vec_scale =
{
return [a_x * b_x, a_y * b_y, a_z * b_z];
}
vector(vector a, vector b) cross_product = {
vector ret;
ret_x = a_y*b_z - a_z*b_y;
ret_y = a_z*b_x - a_x*b_z;
ret_z = a_x*b_y - a_y*b_x;
return ret;
}
/*
* clamp
*
* Limits the given value to the given range.
*
* value: A number
*
* minValue: The minimum value of the range
*
* maxValue: The maximum value of the range
*
* Returns: A number within the given range.
*/
float(float value, float minValue, float maxValue) clamp = {
if (value > maxValue) {
return maxValue;
}
else if (value < minValue) {
return minValue;
}
return value;
};
/*
* mod
*
* Returns the remainder after the division of a by n
*
* a: The dividend
*
* b: The divisor
*
* Returns: The remainder of a divided by n
*/
float(float a, float n) mod = {
return a - (n * floor(a / n));
};
/*
* sign
*
* Returns an indication of the sign of the given number.
*
* x: A number
*
* Returns: -1 if x < 0, 0 if x == 0, 1 if x > 0.
*/
float(float x) sign = {
if (x > 0) {
return 1;
}
else if (x < 0) {
return -1;
}
return 0;
};
/*
* wrap
*
* Limits the given value to the given range and will wrap the value to the
* the other end of the range if exceeded.
*
* value: A number
*
* minValue: The minimum value of the range
*
* maxValue: The maximum value of the range
*
* Returns: A number within the given range.
*/
float(float value, float minValue, float maxValue) wrap = {
local float range = maxValue - minValue;
return mod(value - minValue, range + 1) + minValue;
};
float(float a, float b) min = { if (a <= b) return a; return b; }
float(float a, float b) max = { if (a >= b) return a; return b; }
float(float a, float b) xor = { return (a | b) - (a & b); }
float(float a, float b) not = { return a - (a & b); }
//faster version of id's anglemod
float(float v) anglemod =
{
return v - floor(v/360) * 360;
}
float(float v) anglemod180 =
{
return v - floor((v+180)/360) * 360;
}
float(float anga, float angb) angledif =
{
float dif;
dif = fabs(anga - angb);
if (dif > 180)
dif = 360 - dif;
return dif;
}
vector(vector anga, vector angb) anglesdif =
{
vector dif;
dif = anga - angb;
if (dif_x > 180)
dif_x = 360 - dif_x;
if (dif_y > 180)
dif_y = 360 - dif_y;
if (dif_z > 180)
dif_z = 360 - dif_z;
return dif;
}
float(vector ang, vector base_ang, vector offset) isInAngle = {
if (angledif(ang_x, base_ang_x) > offset_x || angledif(ang_y, base_ang_y) > offset_y)
return FALSE;
else
return TRUE;
};
// mathlib_sqrt
float(float num) sqrt =
{
local float apr;
if(mathlib_accuracy <= 0)
mathlib_accuracy = 0.001; //this sets a level of accuracy, it's a global float
if (num < mathlib_accuracy)
return 0;
if (num>1)
apr = num;
else
apr = 1;
do
{
apr = (num + (apr * apr)) / (2 * apr);
}
while (fabs((apr * apr) - num) > (num * mathlib_accuracy));
return apr;
}
float(float a, float b, float mix) lerp =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
return (b * mix + a * ( 1 - mix ) );
}
vector(vector a, vector b, float mix) lerpVector =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
return (b * mix + a * ( 1 - mix ) );
}
// for a relaxing lerp: hermite lerp.
float(float a, float b, float mix) lerpHermite =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
local float h01;
h01 = mix * mix;
h01 *= 3 - 2 * mix;
return (b * h01 + a * ( 1 - h01 ) );
}
vector(vector a, vector b, float mix) lerpVectorHermite =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
local float h01;
h01 = mix * mix;
h01 *= 3 - 2 * mix;
return (b * h01 + a * ( 1 - h01 ) );
}
// even more smooth:
float(float a, float b, float mix) lerpCosine =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
local float cmx;
cmx = (1 - cos(mix * 180)) / 2;
return (b * cmx + a * ( 1 - cmx ) );
}
vector(vector a, vector b, float mix) lerpVectorCosine =
{
if (mix <= 0) return a;
if (mix >= 1) return b;
local float cmx;
cmx = (1 - cos(mix * 180)) / 2;
return (b * cmx + a * ( 1 - cmx ) );
}
float() crandom = {
return 2*(random() - 0.5);
};
//Supa's random offset function
vector(float offset_amt) RandomOffset =
{
local vector offset;
offset_x = crandom() * offset_amt;
offset_y = crandom() * offset_amt;
offset_z = crandom() * offset_amt;
return offset;
};
// normalizes an angle vector to the 0/+359 range
vector(vector ang) normalizeAngles = {
ang_x = ang_x - floor(ang_x/360) * 360;
ang_y = ang_y - floor(ang_y/360) * 360;
ang_z = ang_z - floor(ang_z/360) * 360;
/*
while (ang_x > 360)
ang_x = ang_x - 360;
while (ang_x < 0)
ang_x = ang_x + 360;
while (ang_y > 360)
ang_y = ang_y - 360;
while (ang_y < 0)
ang_y = ang_y + 360;
while (ang_z > 360)
ang_z = ang_z - 360;
while (ang_z < 0)
ang_z = ang_z + 360;
*/
return ang;
};
// normalizes an angle vector to the -180/+179 range
vector(vector ang) normalizeAngles180 = {
ang_x = ang_x - floor((ang_x+180)/360) * 360;
ang_y = ang_y - floor((ang_y+180)/360) * 360;
ang_z = ang_z - floor((ang_z+180)/360) * 360;
/*
while (ang_x > 180)
ang_x = ang_x - 360;
while (ang_x < -180)
ang_x = ang_x + 360;
while (ang_y > 180)
ang_y = ang_y - 360;
while (ang_y < -180)
ang_y = ang_y + 360;
while (ang_z > 180)
ang_z = ang_z - 360;
while (ang_z < -180)
ang_z = ang_z + 360;
*/
return ang;
};