Files
balatro-gba-chinese-jocker-…/source/random.c
T
MeirGavishandCopilot Autofix powered by AI 7a5a4a1276 Updated all static variables to have the s_ prefix and constants to upper case (#591)
* Updated all static variables to have the s_ prefix and constants to upper case

* Renamed missed sneaky functions in round.c (+ clang-format...)

* Renamed a few more missed constants

* Updated rng_update() documentation

* Apply suggestions from Copilot's code review

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Added missed suggestion

* Updated rng_update() comment

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-07-16 20:21:18 +03:00

52 lines
963 B
C

#include "random.h"
#include "game_variables.h"
#include "util.h"
#include <stdlib.h>
#include <tonc.h>
// Accumulate timer 1 into a bigger variable so we can generate more diverse seeds
static u32 s_timer_acc = 0;
// Timers usage docs: https://gbadev.net/tonc/timers.html
void rng_init(void)
{
REG_TM1D = 0;
REG_TM1CNT = TM_FREQ_1 | TM_ENABLE; // using timer with x1 prescale
}
void rng_update(void)
{
s_timer_acc += (u32)REG_TM1D;
}
void rng_set_seed(u32 seed)
{
g_game_vars.rng_info.seed = seed % (MAX_BASE36 + 1);
g_game_vars.rng_info.step = 0;
srand(g_game_vars.rng_info.seed);
}
void rng_shuffle_seed(void)
{
rng_set_seed(s_timer_acc);
}
u32 rng_get_u32(void)
{
g_game_vars.rng_info.step++;
return rand();
}
void rng_restore(RngInfo info)
{
g_game_vars.rng_info = info;
srand(g_game_vars.rng_info.seed);
for (u32 i = 0; i < g_game_vars.rng_info.step; i++)
{
(void)rng_get_u32();
}
}