2024-02-17 01:04:22 +03:00
|
|
|
#include "sys.h"
|
2024-04-09 01:25:56 +03:00
|
|
|
#include "prevent_bss_reordering.h"
|
2023-10-19 20:53:47 +03:00
|
|
|
|
2023-11-24 22:11:20 +03:00
|
|
|
s32 sSeededRandSeed3;
|
|
|
|
s32 sRandSeed1;
|
|
|
|
s32 sRandSeed2;
|
|
|
|
s32 sRandSeed3;
|
|
|
|
s32 sSeededRandSeed1;
|
|
|
|
s32 sSeededRandSeed2;
|
2023-10-07 09:24:29 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
f32 Math_ModF(f32 value, f32 mod) {
|
|
|
|
return value - ((s32) (value / mod) * mod);
|
2023-10-07 09:17:19 +03:00
|
|
|
}
|
2023-10-06 15:57:30 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
void Rand_Init(void) {
|
2023-11-24 22:11:20 +03:00
|
|
|
sRandSeed1 = (s32) osGetTime() % 30000;
|
|
|
|
sRandSeed2 = (s32) osGetTime() % 30000;
|
|
|
|
sRandSeed3 = (s32) osGetTime() % 30000;
|
2023-10-07 09:28:35 +03:00
|
|
|
}
|
2023-10-06 15:57:30 +03:00
|
|
|
|
2023-10-27 08:11:13 +03:00
|
|
|
f32 Rand_ZeroOne(void) {
|
2023-11-24 22:11:20 +03:00
|
|
|
sRandSeed1 = (sRandSeed1 * 171) % 30269;
|
|
|
|
sRandSeed2 = (sRandSeed2 * 172) % 30307;
|
|
|
|
sRandSeed3 = (sRandSeed3 * 170) % 30323;
|
2023-10-23 23:02:01 +03:00
|
|
|
|
2023-11-24 22:11:20 +03:00
|
|
|
return fabsf(Math_ModF((sRandSeed1 / 30269.0f) + (sRandSeed2 / 30307.0f) + (sRandSeed3 / 30323.0f), 1.0f));
|
2023-10-23 23:02:01 +03:00
|
|
|
}
|
2023-10-06 15:57:30 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
void Rand_SetSeed(s32 seed1, s32 seed2, s32 seed3) {
|
2023-11-24 22:11:20 +03:00
|
|
|
sSeededRandSeed1 = seed1;
|
|
|
|
sSeededRandSeed2 = seed2;
|
|
|
|
sSeededRandSeed3 = seed3;
|
2023-10-07 09:17:19 +03:00
|
|
|
}
|
2023-10-06 15:57:30 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
f32 Rand_ZeroOneSeeded(void) {
|
2023-11-24 22:11:20 +03:00
|
|
|
sSeededRandSeed1 = (sSeededRandSeed1 * 171) % 30269;
|
|
|
|
sSeededRandSeed2 = (sSeededRandSeed2 * 172) % 30307;
|
|
|
|
sSeededRandSeed3 = (sSeededRandSeed3 * 170) % 30323;
|
2023-10-23 23:02:01 +03:00
|
|
|
|
2023-11-24 22:11:20 +03:00
|
|
|
return fabsf(
|
|
|
|
Math_ModF((sSeededRandSeed1 / 30269.0f) + (sSeededRandSeed2 / 30307.0f) + (sSeededRandSeed3 / 30323.0f), 1.0f));
|
2023-10-23 23:02:01 +03:00
|
|
|
}
|
2023-10-06 15:57:30 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
f32 Math_Atan2F(f32 y, f32 x) {
|
|
|
|
if ((y == 0.0f) && (x == 0.0f)) {
|
2023-10-07 10:29:27 +03:00
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
if (x == 0.0f) {
|
|
|
|
if (y < 0.0f) {
|
2023-10-19 20:53:47 +03:00
|
|
|
return -M_PI / 2.0f;
|
2023-10-07 10:29:27 +03:00
|
|
|
} else {
|
2023-10-19 20:53:47 +03:00
|
|
|
return M_PI / 2.0f;
|
2023-10-07 10:29:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
if (x < 0.0f) {
|
|
|
|
if (y < 0.0f) {
|
|
|
|
return -(M_PI - Math_FAtanF(fabs(y / x)));
|
2023-10-07 10:29:27 +03:00
|
|
|
} else {
|
2023-10-30 00:19:30 +03:00
|
|
|
return M_PI - Math_FAtanF(fabs(y / x));
|
2023-10-07 10:29:27 +03:00
|
|
|
}
|
|
|
|
} else {
|
2023-10-30 00:19:30 +03:00
|
|
|
return Math_FAtanF(y / x);
|
2023-10-07 10:29:27 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-06 15:57:30 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
f32 Math_Atan2F_XY(f32 x, f32 y) {
|
|
|
|
if ((x == 0.0f) && (y == 0.0f)) {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x == 0.0f) {
|
|
|
|
if (y < 0.0f) {
|
|
|
|
return -M_PI / 2.0f;
|
|
|
|
} else {
|
|
|
|
return M_PI / 2.0f;
|
|
|
|
}
|
|
|
|
}
|
2023-10-06 15:57:30 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
if (y == 0.0f) {
|
|
|
|
if (x > 0.0f) {
|
|
|
|
return 0.0f;
|
|
|
|
} else {
|
|
|
|
return M_PI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x < 0.0f) {
|
|
|
|
if (y < 0.0f) {
|
|
|
|
return -(M_PI - Math_FAtanF(fabs(x / y)));
|
|
|
|
} else {
|
|
|
|
return M_PI - Math_FAtanF(fabs(x / y));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Math_FAtanF(x / y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f32 Math_Atan2F_XYAlt(f32 x, f32 y) {
|
|
|
|
if ((x == 0.0f) && (y == 0.0f)) {
|
2023-10-07 09:58:07 +03:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2023-10-07 10:38:28 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
if (x == 0.0f) {
|
|
|
|
if (y < 0.0f) {
|
2023-10-19 20:53:47 +03:00
|
|
|
return -M_PI / 2.0f;
|
2023-10-07 09:58:07 +03:00
|
|
|
}
|
2023-10-19 20:53:47 +03:00
|
|
|
return M_PI / 2.0f;
|
2023-10-07 09:58:07 +03:00
|
|
|
}
|
2023-10-07 10:38:28 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
if (y == 0.0f) {
|
2023-10-07 09:58:07 +03:00
|
|
|
return 0.0f;
|
|
|
|
}
|
2023-10-30 00:19:30 +03:00
|
|
|
return -Math_FAtanF(x / y);
|
|
|
|
}
|
|
|
|
|
|
|
|
f32 Math_FactorialF(f32 n) {
|
|
|
|
f32 out = 1.0f;
|
|
|
|
s32 i;
|
|
|
|
|
|
|
|
for (i = (s32) n; i > 1; i--) {
|
|
|
|
out *= i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2023-10-07 09:58:07 +03:00
|
|
|
}
|
2023-10-06 15:57:30 +03:00
|
|
|
|
2023-11-08 01:32:09 +03:00
|
|
|
f32 D_800C45E0[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600 };
|
2023-10-30 00:19:30 +03:00
|
|
|
|
|
|
|
f32 Math_Factorial(s32 n) {
|
|
|
|
f32 out;
|
|
|
|
s32 i;
|
2023-10-06 15:57:30 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
if (n > 12) {
|
|
|
|
out = 1.0f;
|
|
|
|
|
|
|
|
for (i = n; i > 1; i--) {
|
|
|
|
out *= i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
out = D_800C45E0[n];
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2023-10-06 15:57:30 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
f32 Math_PowF(f32 base, s32 exp) {
|
|
|
|
f32 out = 1.0f;
|
2023-10-07 10:38:28 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
while (exp > 0) {
|
|
|
|
exp--;
|
|
|
|
out *= base;
|
2023-10-07 09:21:36 +03:00
|
|
|
}
|
2023-10-30 00:19:30 +03:00
|
|
|
return out;
|
2023-10-07 09:21:36 +03:00
|
|
|
}
|
2023-10-06 15:57:30 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
void Math_MinMax(s32* min, s32* max, s32 val1, s32 val2, s32 val3) {
|
|
|
|
if (val1 < val2) {
|
|
|
|
if (val2 < val3) {
|
|
|
|
*min = val1;
|
|
|
|
*max = val3;
|
2023-10-07 09:40:47 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-10-30 00:19:30 +03:00
|
|
|
*max = val2;
|
2023-10-07 10:38:28 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
if (val1 < val3) {
|
|
|
|
*min = val1;
|
2023-10-07 09:40:47 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-10-30 00:19:30 +03:00
|
|
|
*min = val3;
|
2023-10-07 09:40:47 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-10-07 10:38:28 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
if (val1 < val3) {
|
|
|
|
*min = val2;
|
|
|
|
*max = val3;
|
2023-10-07 09:40:47 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-10-30 00:19:30 +03:00
|
|
|
*max = val1;
|
2023-10-07 10:38:28 +03:00
|
|
|
|
2023-10-30 00:19:30 +03:00
|
|
|
if (val2 < val3) {
|
|
|
|
*min = val2;
|
2023-10-07 09:40:47 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-10-30 00:19:30 +03:00
|
|
|
*min = val3;
|
2023-10-07 09:40:47 +03:00
|
|
|
}
|