GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
random.c
1#include "random.h"
2
3#include "game_variables.h"
4#include "util.h"
5
6#include <stdlib.h>
7#include <tonc.h>
8
9static void s_init_rng_states(void);
10static void s_xorshift32(u32* state);
11
12// Accumulate timer 1 into a bigger variable so we can generate more diverse seeds
13static u32 s_timer_acc = 0;
14
15// Timers usage docs: https://gbadev.net/tonc/timers.html
16void rng_init(void)
17{
18 REG_TM1D = 0;
19 REG_TM1CNT = TM_FREQ_1 | TM_ENABLE; // using timer with x1 prescale
20}
21
22void rng_update(void)
23{
24 s_timer_acc += (u32)REG_TM1D;
25}
26
28{
29 srand(s_timer_acc);
30 rng_set_seed(rand());
31}
32
33void rng_set_seed(u32 seed)
34{
35 // We store the seed to display it at the end of the run, but here it's only used to generate
36 // the independent rng sequences' initial states. We also avoid the seed 0 as the Xorshift32
37 // method used will stay stuck.
38 u32 capped_seed = seed % (MAX_BASE36 + 1);
39 g_game_vars.rng_info.seed = (capped_seed == 0) ? MAX_BASE36 : capped_seed;
40 s_init_rng_states();
41}
42
47static inline void s_init_rng_states(void)
48{
49 srand(g_game_vars.rng_info.seed);
50 for (enum RngSequence key = 0; key < RNG_SEQ_MAX; key++)
51 {
52 g_game_vars.rng_info.states[key] = rand();
53 }
54}
55
57{
58 s_xorshift32(&g_game_vars.rng_info.states[key]);
59 return g_game_vars.rng_info.states[key];
60}
61
70static inline void s_xorshift32(u32* state)
71{
72 *(state) ^= *(state) << 13;
73 *(state) ^= *(state) >> 17;
74 *(state) ^= *(state) << 5;
75}
76
78{
79 g_game_vars.rng_info = info;
80}
Game global game variables struct definition.
Common functions to handle RNG manipulation. Using this interface has three goals:
void rng_update(void)
Accumulates CPU cycles for RNG seed generation, call exaclty once per frame.
Definition random.c:22
void rng_shuffle_seed(void)
Sets a new randomized seed for the RNG using a source of entropy.
Definition random.c:27
u32 rng_get_u32(enum RngSequence key)
Get the next "randomly" generated number in the sequence corresponding to the given type.
Definition random.c:56
void rng_set_seed(u32 seed)
Set the rng seed to the chosen value, and reset the step counter to 0. The seed will be capped at MAX...
Definition random.c:33
RngSequence
Keys to different independent RNG sequences.
Definition random.h:19
void rng_restore(RngInfo info)
Restore RNG info struct in the GameVariables. Sets the seed and restores the state for each independe...
Definition random.c:77
void rng_init(void)
Starts counting CPU cycles, which will be used by rng_shuffle_seed to generate a more random seed....
Definition random.c:16
Information to track and restore RNG state.
Definition random.h:41
u32 seed
Definition random.h:43
u32 states[RNG_SEQ_MAX]
Definition random.h:45
Utilities relating around number string representation and protected arithmatic helper functions.
#define MAX_BASE36
Hex value of "ZZZZZZ" in base 36.
Definition util.h:27