include the rnd.c bits

This commit is contained in:
nhmall
2019-01-28 19:54:18 -05:00
parent 457e4b68aa
commit 54062e0ad6

View File

@@ -7,28 +7,71 @@
#ifdef USE_ISAAC64
#include "isaac64.h"
#if 0
static isaac64_ctx rng_state;
#endif
struct rnglist_t {
int FDECL((*fn), (int));
boolean init;
isaac64_ctx rng_state;
};
enum {CORE = 0, DISP};
static struct rnglist_t rnglist[] = {
{rn2, FALSE, {0}}, /* CORE */
{rn2_on_display_rng, FALSE, {0}}, /* DISP */
};
int
whichrng(fn)
int (*fn)(int);
{
int i;
for (i = 0; i < SIZE(rnglist); ++i)
if (rnglist[i].fn == fn)
return i;
return -1;
}
void
init_isaac64(unsigned long seed)
init_isaac64(seed, fn)
unsigned long seed;
int FDECL((*fn),(int));
{
unsigned char new_rng_state[sizeof(seed)];
int i;
int i, rngindx = whichrng(fn);
if (rngindx < 0)
panic("Bad rng function passed to init_isaac64().");
for (i=0; i<sizeof(seed); i++) {
new_rng_state[i]= (unsigned char)(seed & 0xFF);
seed >>= 8;
}
isaac64_init(&rng_state, new_rng_state, sizeof(seed));
isaac64_init(&rnglist[rngindx].rng_state, new_rng_state, sizeof(seed));
}
static int
RND(int x)
{
return (isaac64_next_uint64(&rng_state) % x);
return (isaac64_next_uint64(&rnglist[CORE].rng_state) % x);
}
#else
/* 0 <= rn2(x) < x, but on a different sequence from the "main" rn2;
used in cases where the answer doesn't affect gameplay and we don't
want to give users easy control over the main RNG sequence. */
int
rn2_on_display_rng(x)
register int x;
{
return (isaac64_next_uint64(&rnglist[DISP].rng_state) % x);
}
#else /* USE_ISAAC64 */
/* "Rand()"s definition is determined by [OS]conf.h */
#if defined(LINT) && defined(UNIX) /* rand() is long... */
extern int NDECL(rand);
@@ -41,7 +84,15 @@ extern int NDECL(rand);
#define RND(x) ((int) ((Rand() >> 3) % (x)))
#endif
#endif /* LINT */
#endif
int
rn2_on_display_rng(x)
register int x;
{
static unsigned seed = 1;
seed *= 2739110765;
return (int)((seed >> 16) % (unsigned)x);
}
#endif /* USE_ISAAC64 */
/* 0 <= rn2(x) < x */
int
@@ -60,20 +111,6 @@ register int x;
#endif
}
/* 0 <= rn2(x) < x, but on a different sequence from the "main" rn2;
used in cases where the answer doesn't affect gameplay and we don't
want to give users easy control over the main RNG sequence. This is
an intentionally low-quality RNG to discourage its use for anything
gameplay-affecting; please use a better RNG in that case. */
int
rn2_on_display_rng(x)
register int x;
{
static unsigned seed = 1;
seed *= 2739110765;
return (int)((seed >> 16) % (unsigned)x);
}
/* 0 <= rnl(x) < x; sometimes subtracting Luck;
good luck approaches 0, bad luck approaches (x-1) */
int