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