-
Notifications
You must be signed in to change notification settings - Fork 1
/
acsmath.acs
695 lines (568 loc) · 14 KB
/
acsmath.acs
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
@const int INT_MAX = 0x7fffffff;
@const int INT_MIN = 0x80000000;
@const fixed FIXED_MAX = fixed(0x7fffffff);
@const fixed FIXED_MIN = fixed(0x80000000);
@const int USHORT_MAX = 65535;
@const int SHORT_MAX = 32767;
@const int SHORT_MIN = -32768;
@const int UBYTE_MAX = 255;
@const int SBYTE_MAX = 127;
@const int SBYTE_MIN = -128;
@const fixed PI = 3.1415926535897932384626433832795;
@const fixed TAU = 6.2831853071795864769252867665590;
@const fixed SQRT_2 = 1.41421356237;
@const fixed MATH_E = 2.7182818284590452353602874713526624977572470937;
@const fixed LOG2_E = 1.44269504089;
@const fixed LOG2_10 = 3.32192809489;
// Generic functions.
function num min(num a, num b)
{
if (a < b)
return a;
return b;
}
function num max(num a, num b)
{
if (a > b)
return a;
return b;
}
function int clamp(num x, num a, num b)
{
if (x > b)
return b;
if (x < a)
return a;
return x;
}
function int sgn(num x)
{
if (x > 0)
return 1;
if (x < 0)
return -1;
return 0;
}
function int abs(num x)
{
if (x > 0)
return x;
return -x;
}
// From commonFuncs.h by Ijon Tichy.
function num middle(num x, num y, num z)
{
if ((x < z) && (y < z)) { return max(x, y); }
return max(min(x, y), z);
}
function int cmp(num a, num b)
{
if (a > b)
return 1;
if (a < b)
return -1;
return 0;
}
function num mod(num a, num b)
{
if (a < 0)
{
int rem = a % b;
if (rem != 0)
return b + (a % b);
return 0;
}
return a % b;
}
function raw cond(bool x, raw whentrue, raw whenfalse)
{
if (x)
return whentrue;
return whenfalse;
}
function raw condTrue(raw x, raw whentrue)
{
if (x)
return whentrue;
return x;
}
function raw condFalse(raw x, raw whenfalse)
{
if (x)
return x;
return whenfalse;
}
// Rounding.
function fixed fract(fixed x)
{
return x - trunc(x);
}
function fixed trunc(fixed x)
{
if (x > 0.0)
return floor(x);
return ceil(x);
}
/* ZDOOM DEFINES ITS OWN VERSIONS OF floor, ceil AND round THAT SILENTLY RETURN 0 IN ZANDRONUM, PLEASE REMOVE THESE FUNCTIONS FROM FROM zspecial.acs OR RENAME THE ACSUTILS OR ZDOOM FUNCTIONS TO SOMETHING ELSE. */ function fixed floor(fixed x)
{
return fixed(int(x) & 0xffff0000);
}
/* ZDOOM DEFINES ITS OWN VERSIONS OF floor, ceil AND round THAT SILENTLY RETURN 0 IN ZANDRONUM, PLEASE REMOVE THESE FUNCTIONS FROM FROM zspecial.acs OR RENAME THE ACSUTILS OR ZDOOM FUNCTIONS TO SOMETHING ELSE. */function fixed ceil(fixed x)
{
return fixed((int(x) - 1) & 0xffff0000) + 1.0;
}
/* ZDOOM DEFINES ITS OWN VERSIONS OF floor, ceil AND round THAT SILENTLY RETURN 0 IN ZANDRONUM, PLEASE REMOVE THESE FUNCTIONS FROM FROM zspecial.acs OR RENAME THE ACSUTILS OR ZDOOM FUNCTIONS TO SOMETHING ELSE. */ function fixed round(fixed x)
{
return fixed(int(x + 0.5) & 0xffff0000);
}
function int itrunc(fixed x)
{
if (x > 0.0)
return ifloor(x);
return iceil(x);
}
function int ifloor(fixed x)
{
return int(x)>>16;
}
function int iceil(fixed x)
{
return (int(x) - 1 >> 16) + 1;
}
function int iround(fixed x)
{
return int(x + 0.5) >> 16;
}
// Numerical algorithms.
function fixed AngleDiff(fixed a, fixed b)
{
a = mod(a, 1.0);
b = mod(b, 1.0);
fixed diff = b - a;
if (diff > 0.5)
return diff - 1.0;
else if (diff < -0.5)
return diff + 1.0;
return diff;
}
function int ipow(int x, int y)
{
int n = 1;
while (y-- > 0)
n *= x;
return n;
}
function fixed fpow(fixed x_, fixed y_)
{
raw x = x_;
raw y = y_;
raw n = 1.0;
if (y > 0)
{
while (y-- > 0)
n = FixedMul(n, x);
return n;
}
while (y++ < 0)
n = FixedDiv(n, x);
return n;
}
function fixed lerp(fixed a, fixed b, fixed alpha)
{
return FixedMul(a, 1.0 - alpha) + FixedMul(b, alpha);
}
// From commonFuncs.h by Ijon Tichy.
function num gcf(num a, num b)
{
int c;
while (1)
{
if (b == 0) { return a; }
c = a % b;
a = b;
b = c;
}
return -1;
}
// By TechnoDoomed1
// fixed IntDiv(int a, int b)
function fixed IntDiv (int a_, int b_) {
raw a = a_;
raw b = b_;
raw quotient = 0, current_fraction = 1.0;
// Only works when the ratio is less than 32767 = 2^15 - 1.
// Otherwise return 0.
if ((abs(a) / abs(b)) > 32767) {
return 0.0;
}
// Performs the same algorithm as hand division, but working with powers of 2 instead of 10.
// This is done until we reach the maximum allowed precision, which is 1 (=2^-16 in fixed point).
while (current_fraction > 1) {
quotient += (a / b) * current_fraction;
a = (a % b) * 2;
current_fraction /= 2;
}
return fixed(quotient);
}
// raw, raw swap(raw a, raw b)
function void swap(raw a, raw b)
{
r1 = b;
r2 = a;
}
// Bit math.
function int getbit(int x, int n)
{
return x & (1 << n);
}
function int clrbit(int p, int n)
{
return p & ~(1 << n);
}
function int setbit(int p, int n)
{
return p | ~(1 << n);
}
function int tglbit(int p, int n)
{
return p ^ (1 << n);
}
function bool notflag(int flags, int flag)
{
return !(flags & flag);
}
function bool randbool(void)
{
return bool(Random(0, 1));
}
function int randint(void)
{
return Random(INT_MIN, INT_MAX);
}
// From commonFuncs.h by Ijon Tichy.
function int randsign(void)
{
return 2 * Random(0, 1) - 1;
}
function raw RandomPick2(raw v0, raw v1)
{
if (Random(0, 1))
return v0;
return v1;
}
function raw RandomPick3(raw v0, raw v1, raw v2)
{
int x = Random(0, 2);
switch (x)
{
case 0: return v0;
case 1: return v1;
}
return v2;
}
function raw RandomPick4(raw v0, raw v1, raw v2, raw v3)
{
int x = Random(0, 3);
switch (x)
{
case 0: return v0;
case 1: return v1;
case 2: return v2;
}
return v3;
}
function raw RandomPick5(raw v0, raw v1, raw v2, raw v3, raw v4)
{
int x = Random(0, 4);
switch (x)
{
case 0: return v0;
case 1: return v1;
case 2: return v2;
case 3: return v3;
}
return v4;
}
function raw RandomPick6(raw v0, raw v1, raw v2, raw v3, raw v4, raw v5)
{
int x = Random(0, 5);
switch (x)
{
case 0: return v0;
case 1: return v1;
case 2: return v2;
case 3: return v3;
case 4: return v4;
}
return v5;
}
function raw RandomPick7(raw v0, raw v1, raw v2, raw v3, raw v4, raw v5, raw v6)
{
int x = Random(0, 6);
switch (x)
{
case 0: return v0;
case 1: return v1;
case 2: return v2;
case 3: return v3;
case 4: return v4;
case 5: return v5;
}
return v6;
}
function raw RandomPick8(raw v0, raw v1, raw v2, raw v3, raw v4, raw v5, raw v6, raw v7)
{
int x = Random(0, 7);
switch (x)
{
case 0: return v0;
case 1: return v1;
case 2: return v2;
case 3: return v3;
case 4: return v4;
case 5: return v5;
case 6: return v6;
}
return v7;
}
function int npo2(int v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
function int getNumDigits(int base, int number)
{
int digits = 0;
while (number)
{
digits++;
number /= base;
}
return digits;
}
function int flag2index(int x)
{
return getNumDigits(2, x) - 1;
}
// Logarithms.
// All logaritm functions written by TechnoDoomed1 unless stated otherwise
function fixed log2 (fixed x_) {
raw x = x_;
// We calculate the integral and decimal parts of the bit logarithm of x.
raw integer_part = 0, decimal_part = 0;
// The integral part is how many times we can divide by 2 until we reach a number in the range [1, 2).
// If the number is on the range (0, 1) then we multiply by 2 until we reach a number in the range [1, 2).
while (x < 1.0) {
-- integer_part;
x *= 2;
}
while (x >= 2.0) {
++ integer_part;
x /= 2;
}
// Then, we square the number each time to get the next relevant byte, until we reach max precision allowed.
// WHY? Because if 2^d = y, then (2^d)^2 = 2^(2d) = y^2, where d is the decimal part.
//-----------------------------------------------------------------------------------------------------------------
// Let's start with the fraction 1/2, and go downwards until we reach the max precision for a fixed-point number,
// which is 1 (since it occupies the right side of the byte, it really represents 2^-16).
raw current_fraction = 0.5;
while (current_fraction > 1) {
x = FixedMul(x, x);
if (x >= 2.0) {
decimal_part += current_fraction;
x /= 2;
}
current_fraction /= 2;
}
// We can finally return the number as the integral part (shifted 16 bytes to the left, to be on the corresponding
// integral part of the fixed-point number) plus the decimal part, which is the sum of all the fractions of 2 that
// correspond to the solution of 2^d = y.
return ((integer_part << 16) + decimal_part);
}
function fixed ilog2 (int x_) {
raw x = x_;
// We calculate the integral and decimal parts of the bit logarithm of x.
raw integer_part = 0, decimal_part = 0;
// The integral part is how many times we can divide by 2 until we reach a number lower than 2.
// We lose precision by not keeping the fractional part until that part fits perfectly in a fixed number variable.
while (x >= 32768) {
++ integer_part;
x /= 2;
}
x <<= 16;
while (x >= 2.0) {
++ integer_part;
x /= 2;
}
// Then, we square the number each time to get the next relevant byte, until we reach max precision allowed.
// WHY? Because if 2^d = y, then (2^d)^2 = 2^(2d) = y^2, where d is the decimal part.
//-----------------------------------------------------------------------------------------------------------------
// Let's start with the fraction 1/2, and go downwards until we reach the max precision for a fixed-point number,
// which is 1 (since it occupies the right side of the byte, it really represents 2^-16).
raw current_fraction = 0.5;
while (current_fraction > 1) {
x = FixedMul(x, x);
if (x >= 2.0) {
decimal_part += current_fraction;
x /= 2;
}
current_fraction /= 2;
}
// We can finally return the number as the integral part (shifted 16 bytes to the left, to be on the corresponding
// integral part of the fixed-point number) plus the decimal part, which is the sum of all the fractions of 2 that
// correspond to the solution of 2^d = y.
return ((integer_part << 16) + decimal_part);
}
function fixed ln (fixed x) {
// This calculates the natural logarithm of a number using the property that: ln(x) = log_2(x) / log_2(e)
// This is done because calculating the log_2 of a number is far easier, specially with fixed-point arithmetics.
return FixedDiv(log2(x), LOG2_E);
}
// By Korshun.
function fixed iln (int x) {
return FixedDiv(ilog2(x), LOG2_E);
}
// By Korshun.
function fixed log10 (fixed x) {
return FixedDiv(log2(x), LOG2_10);
}
// By Korshun.
function fixed ilog10 (int x) {
return FixedDiv(ilog2(x), LOG2_10);
}
function fixed logb (fixed x, fixed base) {
// This calculates the logarithm in any base > 1.0, by using the property that: log_b(x) = log_2(x) / log_2(b)
// Otherwise, returns 0.
if (base > 1.0)
return FixedDiv(log2(x), log2(base));
return 0.0;
}
// By Korshun.
function fixed ilogb (int x, fixed base) {
// This calculates the logarithm in any base > 1.0, by using the property that: log_b(x) = log_2(x) / log_2(b)
// Otherwise, returns 0.
if (base > 1.0)
return FixedDiv(ilog2(x), log2(base));
return 0.0;
}
// Trigonometry.
function fixed tan(ang x)
{
return FixedDiv(sin(x), cos(x));
}
function fixed cot(ang x)
{
return FixedDiv(cos(x), sin(x));
}
function fixed sec(ang x)
{
return FixedDiv(1.0, sin(x));
}
function fixed cosec(ang x)
{
return FixedDiv(1.0, cos(x));
}
function ang atan(fixed x)
{
return VectorAngle(1.0, x);
}
function ang asin(fixed x)
{
return atan(FixedDiv(x, FixedSqrt(1.0 - FixedMul(x, x))));
}
function ang acos(fixed x)
{
return ang(int(2) * int(atan(FixedSqrt(FixedDiv(1.0 - x, 1.0 + x)))));
}
function ang acot(fixed x)
{
return 0.25 - atan(x);
}
function ang asec(fixed x)
{
return acos(FixedDiv(1.0, x));
}
function ang acosec(fixed x)
{
return asin(FixedDiv(1.0, x));
}
// Vectors.
// fixed, fixed RotateVector(fixed x, fixed y, ang angle);
function void RotateVector(fixed x, fixed y, ang angle)
{
// Rotate around Z axis.
fixed s = sin(angle);
fixed c = cos(angle);
r1 = FixedMul(x, c) - FixedMul(y, s);
r2 = FixedMul(x, s) + FixedMul(y, c);
}
// fixed, fixed RotateVectorCS(fixed x, fixed y, fixed c, fixed s);
function void RotateVectorCS(fixed x, fixed y, fixed c, fixed s)
{
// Rotate around Z axis.
r1 = FixedMul(x, c) - FixedMul(y, s);
r2 = FixedMul(x, s) + FixedMul(y, c);
}
// fixed, fixed RotatePoint(fixed x, fixed y, fixed originX, fixed originY, ang angle)
function void RotatePoint(fixed x, fixed y, fixed originX, fixed originY, ang angle)
{
x -= originX;
y -= originY;
RotateVector(x, y, angle);
r1 += originX;
r2 += originY;
}
// ang, ang VectorToAngles(fixed x, fixed y, fixed z)
function void VectorToAngles(fixed x, fixed y, fixed z)
{
fixed xy = VectorLength(x, y);
r1 = VectorAngle(x, y);
r2 = VectorAngle(xy, z);
}
// fixed, fixed, fixed AnglesToVector(ang angle, ang pitch)
function void AnglesToVector(ang angle, ang pitch)
{
fixed cos_pitch = cos(pitch);
r1 = FixedMul(cos_pitch, cos(angle));
r2 = FixedMul(cos_pitch, sin(angle));
r3 = sin(pitch);
}
function fixed VectorLength3D(fixed x, fixed y, fixed z)
{
return VectorLength(VectorLength(x, y), z);
}
function fixed SqVectorLength(fixed x, fixed y)
{
return FixedMul(x, x) + FixedMul(y, y);
}
function fixed SqVectorLength3D(fixed x, fixed y, fixed z)
{
return FixedMul(x, x) + FixedMul(y, y) + FixedMul(z, z);
}
function fixed dot2(fixed x1, fixed y1, fixed x2, fixed y2)
{
return FixedMul(x1, x2) + FixedMul(y1, y2);
}
function fixed dot3(fixed x1, fixed y1, fixed z1, fixed x2, fixed y2, fixed z2)
{
return FixedMul(x1, x2) + FixedMul(y1, y2) + FixedMul(z1, z2);
}
function void normalize2d(fixed x, fixed y)
{
fixed l = VectorLength(x, y);
r1 = FixedDiv(x, l);
r2 = FixedDiv(y, l);
}
function void normalize3d(fixed x, fixed y, fixed z)
{
fixed l = VectorLength3D(x, y, z);
r1 = FixedDiv(x, l);
r2 = FixedDiv(y, l);
r3 = FixedDiv(z, l);
}