Files
balatro-gba-chinese-jocker-…/source/random.c
T
Geralt c2844a5d32 Refactor/RNG system (#476)
* Extract rng manipulation to its own file

* clang format

* Get CPU cycles since game start to use for RNG

* change a few things

* clang format

* Implement suggestions from @ricfehr3

* clang format

* Fix rebase issue

* clang format

* Create RngInfo struct to hold seed and step

* clang format

* Make a note about rng_restore system not having been tested properly yet

* Apply suggestions from @ricfehr3

Co-authored-by: Rickey <ric@rf3.xyz>

* clang format

---------

Co-authored-by: MathisMartin31 <mathis.martin31@gmail.com>
Co-authored-by: Rickey <ric@rf3.xyz>
2026-05-21 19:47:48 -07:00

51 lines
937 B
C

#include "random.h"
#include "game_variables.h"
#include <stdlib.h>
#include <tonc.h>
// Accumulate timer 1 into a bigger variable so we can generate more diverse seeds
static u32 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)
{
timer_acc += (u32)REG_TM1D;
}
void rng_set_seed(u32 seed)
{
g_game_vars.rng_info.seed = seed % (MAX_SEED + 1);
g_game_vars.rng_info.step = 0;
srand(g_game_vars.rng_info.seed);
}
void rng_shuffle_seed(void)
{
rng_set_seed(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();
}
}