GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
random.h File Reference

Common functions to handle RNG manipulation. Using this interface has three goals: More...

#include <tonc.h>

Go to the source code of this file.

Data Structures

struct  RngInfo
 Information to track and restore RNG state. More...
 

Enumerations

enum  RngSequence {
  RNG_SEQ_CARD_SHUFFLE , RNG_SEQ_BLIND , RNG_SEQ_SHOP_ITEMS , RNG_SEQ_SKIP_TAGS ,
  RNG_SEQ_JOKER_MISPRINT , RNG_SEQ_JOKER_RESERVED_PARKING , RNG_SEQ_JOKER_BUSINESS_CARD , RNG_SEQ_MISC ,
  RNG_SEQ_MAX
}
 Keys to different independent RNG sequences. More...
 

Functions

void rng_init (void)
 Starts counting CPU cycles, which will be used by rng_shuffle_seed to generate a more random seed. To be called once on game start.
 
void rng_update (void)
 Accumulates CPU cycles for RNG seed generation, call exaclty once per frame.
 
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_BASE36 for compatibility with the Seed Input screen used to choose a seed for seeded runs.
 
void rng_shuffle_seed (void)
 Sets a new randomized seed for the RNG using a source of entropy.
 
u32 rng_get_u32 (enum RngSequence key)
 Get the next "randomly" generated number in the sequence corresponding to the given type.
 
void rng_restore (RngInfo info)
 Restore RNG info struct in the GameVariables. Sets the seed and restores the state for each independent sequence.
 

Detailed Description

Common functions to handle RNG manipulation. Using this interface has three goals:

  1. Make RNG repeatable so that people can share seeds and saves, and end up with the same run
  2. Prevent the players from engaging in save-scumming since the pseudo-RNG is deterministic
  3. Accomodate for base-36 seeds, so that we can freely chose one with alpha-numeric characters in the seed screen input

Definition in file random.h.

Enumeration Type Documentation

◆ RngSequence

Keys to different independent RNG sequences.

Definition at line 18 of file random.h.

Function Documentation

◆ rng_get_u32()

u32 rng_get_u32 ( enum RngSequence  key)

Get the next "randomly" generated number in the sequence corresponding to the given type.

Parameters
keykey to the RNG sequence we need to pull a random number from
Returns
u32

Definition at line 56 of file random.c.

◆ rng_init()

void rng_init ( void  )

Starts counting CPU cycles, which will be used by rng_shuffle_seed to generate a more random seed. To be called once on game start.

Definition at line 16 of file random.c.

◆ rng_restore()

void rng_restore ( RngInfo  info)

Restore RNG info struct in the GameVariables. Sets the seed and restores the state for each independent sequence.

Parameters
infoRngInfo struct applied
Note
Consistency of the RNG after reloading the RngInfo struct has yet to be tested properly

Definition at line 77 of file random.c.

◆ rng_set_seed()

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_BASE36 for compatibility with the Seed Input screen used to choose a seed for seeded runs.

Parameters
seedthe new RNG seed

Definition at line 33 of file random.c.

◆ rng_shuffle_seed()

void rng_shuffle_seed ( void  )

Sets a new randomized seed for the RNG using a source of entropy.

This needs to be called at least once before generating anything random but not on the very first cycle because it uses a CPU timer for entropy.

This would also be used to generate the new seed when starting a new non-player-seeded run

Note
rng_init needs to have been called, so that the CPU profiling can provide entropy.
See also
rng_init

Definition at line 27 of file random.c.

◆ rng_update()

void rng_update ( void  )

Accumulates CPU cycles for RNG seed generation, call exaclty once per frame.

Definition at line 22 of file random.c.