From 457e4b68aac9c9cab9e7b750fc57aa028190c0e7 Mon Sep 17 00:00:00 2001 From: nhmall Date: Mon, 28 Jan 2019 19:43:55 -0500 Subject: [PATCH] merge Alex's dual rng proposal with the isaac64 rng code and adjust This is branched from Alex's hallu-rng-stability branch, with two build corrections (detect.c, zap.c), and merged with the isaac64 branch that we have ready to go. Alex's dual rng is supported by setting up the array of multiple isaac64 contexts. I stuck with Alex's approach of passing the rng function name around as the parameter (rng or rn2_on_display_rng) for the new additional parameter needed for set_random(), init_random(), reseed_random(), and init_isaac64(). --- include/extern.h | 6 +++--- src/detect.c | 4 ++-- src/do.c | 3 ++- src/hacklib.c | 32 +++++++++++++++++++++----------- src/mklev.c | 6 ++++-- src/options.c | 5 +++-- src/zap.c | 2 +- 7 files changed, 36 insertions(+), 22 deletions(-) diff --git a/include/extern.h b/include/extern.h index 7e48246f5..f67d09c85 100644 --- a/include/extern.h +++ b/include/extern.h @@ -925,8 +925,8 @@ E char *FDECL(strstri, (const char *, const char *)); #endif E boolean FDECL(fuzzymatch, (const char *, const char *, const char *, BOOLEAN_P)); -E void NDECL(init_random); -E void NDECL(reseed_random); +E void FDECL(init_random, (int FDECL((*fn), (int)))); +E void FDECL(reseed_random, (int FDECL((*fn), (int)))); E time_t NDECL(getnow); E int NDECL(getyear); #if 0 @@ -2105,7 +2105,7 @@ E void FDECL(genl_outrip, (winid, int, time_t)); /* ### rnd.c ### */ #ifdef USE_ISAAC64 -E void FDECL(init_isaac64, (unsigned long)); +E void FDECL(init_isaac64, (unsigned long, int FDECL((*fn), (int)))); E long NDECL(nhrand); #endif E int FDECL(rn2, (int)); diff --git a/src/detect.c b/src/detect.c index 545f3906e..13676f48a 100644 --- a/src/detect.c +++ b/src/detect.c @@ -869,10 +869,10 @@ int src_cursed; obj.ox = x; obj.oy = y; } - obj.otyp = !Hallucination ? GOLD_PIECE : random_object(); + obj.otyp = !Hallucination ? GOLD_PIECE : random_object(rn2_on_display_rng); obj.quan = (long) ((obj.otyp == GOLD_PIECE) ? rnd(10) : objects[obj.otyp].oc_merge ? rnd(2) : 1); - obj.corpsenm = random_monster(); /* if otyp == CORPSE */ + obj.corpsenm = random_monster(rn2); /* if otyp == CORPSE */ map_object(&obj, 1); } else if (trap) { map_trap(trap, 1); diff --git a/src/do.c b/src/do.c index ad2e3045d..7467356c7 100644 --- a/src/do.c +++ b/src/do.c @@ -1420,7 +1420,8 @@ boolean at_stairs, falling, portal; /* we'll reach here if running in wizard mode */ error("Cannot continue this game."); } - reseed_random(); + reseed_random(rn2); + reseed_random(rn2_on_display_rng); minit(); /* ZEROCOMP */ getlev(fd, hackpid, new_ledger, FALSE); (void) nhclose(fd); diff --git a/src/hacklib.c b/src/hacklib.c index 60f492b37..1fe5c8fd6 100644 --- a/src/hacklib.c +++ b/src/hacklib.c @@ -51,8 +51,8 @@ boolean fuzzymatch (const char *, const char *, const char *, boolean) void setrandom (void) - void init_random (void) - void reseed_random (void) + void init_random (fn) + void reseed_random (fn) time_t getnow (void) int getyear (void) char * yymmdd (time_t) @@ -851,12 +851,20 @@ extern struct tm *FDECL(localtime, (time_t *)); STATIC_DCL struct tm *NDECL(getlt); /* Sets the seed for the random number generator */ +#ifdef USE_ISAAC64 static void -set_random(unsigned long seed) +set_random(seed, fn) +unsigned long seed; +int FDECL((*fn), (int)); +{ + init_isaac64(seed, fn); +} +#else /* USE_ISAAC64 */ +static void +set_random(seed, fn) +unsigned long seed; +int FDECL((*fn),(int)); { -#ifdef USE_ISAAC64 - init_isaac64(seed); -#else /* the types are different enough here that sweeping the different * routine names into one via #defines is even more confusing */ @@ -877,8 +885,8 @@ set_random(unsigned long seed) # endif # endif # endif -#endif } +#endif /* USE_ISAAC64 */ /* An appropriate version of this must always be provided in port-specific code somewhere. It returns a number suitable @@ -890,19 +898,21 @@ extern unsigned long NDECL(sys_random_seed); * Only call once. */ void -init_random() +init_random(fn) +int FDECL((*fn),(int)); { - set_random(sys_random_seed()); + set_random(sys_random_seed(), fn); } /* Reshuffles the random number generator. */ void -reseed_random() +reseed_random(fn) +int FDECL((*fn),(int)); { /* only reseed if we are certain that the seed generation is unguessable * by the players. */ if (has_strong_rngseed) - init_random(); + init_random(fn); } time_t diff --git a/src/mklev.c b/src/mklev.c index 4cf5b9000..e2048aec8 100644 --- a/src/mklev.c +++ b/src/mklev.c @@ -982,7 +982,8 @@ mklev() struct mkroom *croom; int ridx; - reseed_random(); + reseed_random(rn2); + reseed_random(rn2_on_display_rng); init_mapseen(&u.uz); if (getbones()) @@ -1012,7 +1013,8 @@ mklev() for (ridx = 0; ridx < SIZE(rooms); ridx++) rooms[ridx].orig_rtype = rooms[ridx].rtype; - reseed_random(); + reseed_random(rn2); + reseed_random(rn2_on_display_rng); } void diff --git a/src/options.c b/src/options.c index 8fe27c8d4..77d6d6338 100644 --- a/src/options.c +++ b/src/options.c @@ -691,8 +691,9 @@ initoptions_init() /* set up the command parsing */ reset_commands(TRUE); /* init */ - /* initialize the random number generator */ - init_random(); + /* initialize the random number generator(s) */ + init_random(rn2); + init_random(rn2_on_display_rng); /* for detection of configfile options specified multiple times */ iflags.opt_booldup = iflags.opt_compdup = (int *) 0; diff --git a/src/zap.c b/src/zap.c index 95c5aaa2c..8c0219529 100644 --- a/src/zap.c +++ b/src/zap.c @@ -1488,7 +1488,7 @@ int id; /* now change it into something laid by the hero */ while (tryct--) { - mnum = can_be_hatched(random_monster()); + mnum = can_be_hatched(random_monster(rn2)); if (mnum != NON_PM && !dead_species(mnum, TRUE)) { otmp->spe = 1; /* laid by hero */ set_corpsenm(otmp, mnum); /* also sets hatch timer */ -- 2.40.0