Skip to content

Commit

Permalink
test: add (crude) RNG debugging option
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilyV99 committed Aug 20, 2023
1 parent 8729251 commit 97acebb
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
1 change: 1 addition & 0 deletions modules/zelda/ZeldaCore.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ src/user_object.cpp
src/ffc.cpp
src/slopes.cpp
src/zc/saves.cpp
src/zc/db_rng.cpp

## End of Zelda Core module
)
17 changes: 9 additions & 8 deletions src/base/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ zc_randgen* zc_get_default_rand()
return &default_rng;
}

int32_t zc_oldrand(zc_randgen* rng)
{
// RAND_MAX can't be used because it is platform dependent, and we need
// reproducible randomness. 0x7fff is the value MSVC uses.
return zc_rand(0x7fff, 0, rng);
}

int32_t zc_rand(zc_randgen* rng)
{
if(!rng) rng = &default_rng;
Expand All @@ -38,4 +31,12 @@ void zc_srand(int32_t seedval, zc_randgen* rng)
{
if(!rng) rng = &default_rng;
rng->seed(seedval);
}
}

int32_t zc_oldrand(zc_randgen* rng)
{
// RAND_MAX can't be used because it is platform dependent, and we need
// reproducible randomness. 0x7fff is the value MSVC uses.
return zc_rand(0x7fff, 0, rng);
}

20 changes: 19 additions & 1 deletion src/base/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@
#include "base/zdefs.h"
#include <random>

typedef std::mt19937 zc_randgen;
#ifdef IS_PLAYER
#define DEBUG_RAND false
#else
#define DEBUG_RAND false //Can only work when IS_PLAYER
#endif

typedef std::mt19937 zc_randgen;
zc_randgen* zc_get_default_rand();

#if !DEBUG_RAND
int32_t zc_oldrand(zc_randgen* rng=NULL);
int32_t zc_rand(zc_randgen* rng=NULL);
int32_t zc_rand(int32_t upper, int32_t lower=0, zc_randgen* rng=NULL);
void zc_srand(int32_t seedval, zc_randgen* rng=NULL);
#else

#define zc_oldrand(...) db_oldrand(__FILE__,__LINE__,__VA_ARGS__)
int32_t db_oldrand(char const* f, int l,zc_randgen* rng=NULL);
#define zc_rand(...) db_rand(__FILE__,__LINE__,__VA_ARGS__)
int32_t db_rand(char const* f, int l,zc_randgen* rng=NULL);
int32_t db_rand(char const* f, int l,int32_t upper,int32_t lower=0,zc_randgen* rng=NULL);
#define zc_srand(...) db_srand(__FILE__,__LINE__,__VA_ARGS__)
void db_srand(char const* f, int l, int32_t seedval, zc_randgen* rng=NULL);

#endif

#endif

41 changes: 41 additions & 0 deletions src/zc/db_rng.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "base/random.h"

#if DEBUG_RAND
#include "zc/replay.h"
#include <fmt/format.h>

static void db_print(char const* fname, int l, std::string const& extra)
{
if(FILE* f = fopen("__db_rand.txt","a"))
{
char replstr[128] = {0};
char buf[512] = {0};
if(replay_is_replaying()) sprintf(replstr, "%06d ", replay_get_frame());
sprintf(buf, "%s %s%s LINE %d\n", extra.c_str(), replstr, fname, l);
fwrite(buf, 1, strlen(buf), f);
fclose(f);
}
}

int32_t db_oldrand(char const* fname, int l, zc_randgen* rng)
{
if(!rng) db_print(fname,l,"OLD");
return zc_oldrand(rng);
}
int32_t db_rand(char const* fname, int l,zc_randgen* rng)
{
if(!rng) db_print(fname,l,"NEW");
return zc_rand(rng);
}
int32_t db_rand(char const* fname, int l,int32_t upper,int32_t lower,zc_randgen* rng)
{
if(!rng) db_print(fname,l,fmt::format("NEW {}-{}",lower,upper));
return zc_rand(upper,lower,rng);
}
void db_srand(char const* fname, int l, int32_t seedval, zc_randgen* rng)
{
if(!rng) db_print(fname,l,fmt::format("SEED {}",seedval));
zc_srand(seedval,rng);
}
#endif

0 comments on commit 97acebb

Please sign in to comment.