OSDN Git Service

upgrade to 3.6.2
[jnethack/source.git] / src / rnd.c
index c3627e8..8f92e42 100644 (file)
--- a/src/rnd.c
+++ b/src/rnd.c
@@ -4,6 +4,76 @@
 
 #include "hack.h"
 
+#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 = 1 };
+
+static struct rnglist_t rnglist[] = {
+    { rn2, FALSE, { 0 } },                      /* CORE */
+    { rn2_on_display_rng, FALSE, { 0 } },       /* DISP */
+};
+
+int
+whichrng(fn)
+int FDECL((*fn), (int));
+{
+    int i;
+
+    for (i = 0; i < SIZE(rnglist); ++i)
+        if (rnglist[i].fn == fn)
+            return i;
+    return -1;
+}
+
+void
+init_isaac64(seed, fn)
+unsigned long seed;
+int FDECL((*fn), (int));
+{
+    unsigned char new_rng_state[sizeof seed];
+    unsigned i;
+    int 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(&rnglist[rngindx].rng_state, new_rng_state,
+                 (int) sizeof seed);
+}
+
+static int
+RND(int x)
+{
+    return (isaac64_next_uint64(&rnglist[CORE].rng_state) % x);
+}
+
+/* 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);
@@ -16,6 +86,15 @@ extern int NDECL(rand);
 #define RND(x) ((int) ((Rand() >> 3) % (x)))
 #endif
 #endif /* LINT */
+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