Include LCG state in game saves.
authorEric S. Raymond <esr@thyrsus.com>
Wed, 14 Jun 2017 17:00:28 +0000 (13:00 -0400)
committerEric S. Raymond <esr@thyrsus.com>
Wed, 14 Jun 2017 17:00:28 +0000 (13:00 -0400)
advent.h
main.c
misc.c
notes.adoc

index ee8e3faa9808e33b831669291ddd986fcfb65147..4717fe7b3134a2df50ce3b58d897e44e2a69d520 100644 (file)
--- a/advent.h
+++ b/advent.h
 #define INTRANSITIVE   -1              /* illegal object number */
 #define SPECIALBASE    300             /* base umber of special rooms */
 
 #define INTRANSITIVE   -1              /* illegal object number */
 #define SPECIALBASE    300             /* base umber of special rooms */
 
-typedef struct lcg_state
-{
-  unsigned long a, c, m, x;
-} lcg_state;
-
 typedef long token_t;  /* word token - someday this will be char[TOKLEN+1] */
 typedef long vocab_t;  /* index into a vocabulary array */
 
 struct game_t {
 typedef long token_t;  /* word token - someday this will be char[TOKLEN+1] */
 typedef long vocab_t;  /* index into a vocabulary array */
 
 struct game_t {
+    unsigned long lcg_a, lcg_c, lcg_m, lcg_x;
     long abbnum;
     long blklin;
     long bonus;
     long abbnum;
     long blklin;
     long bonus;
@@ -81,7 +77,6 @@ extern const char ascii_to_advent[];
 extern const char advent_to_ascii[];
 extern FILE *logfp;
 extern bool oldstyle, editline, prompt;
 extern const char advent_to_ascii[];
 extern FILE *logfp;
 extern bool oldstyle, editline, prompt;
-extern lcg_state lcgstate;
 
 /* b is not needed for POSIX but harmless */
 #define READ_MODE "rb"
 
 /* b is not needed for POSIX but harmless */
 #define READ_MODE "rb"
diff --git a/main.c b/main.c
index 90f60033d883790734dc94c6bebe51107540eb63..6ba8897825bd286f32d392202c17d876f99348d3 100644 (file)
--- a/main.c
+++ b/main.c
@@ -46,7 +46,6 @@ FILE  *logfp;
 bool oldstyle = false;
 bool editline = true;
 bool prompt = true;
 bool oldstyle = false;
 bool editline = true;
 bool prompt = true;
-lcg_state lcgstate;
 
 extern void initialise();
 extern void score(long);
 
 extern void initialise();
 extern void score(long);
@@ -114,9 +113,9 @@ int main(int argc, char *argv[])
 
     /* Initialize our LCG PRNG with parameters tested against
      * Knuth vol. 2. by the original authors */
 
     /* Initialize our LCG PRNG with parameters tested against
      * Knuth vol. 2. by the original authors */
-    lcgstate.a = 1093;
-    lcgstate.c = 221587;
-    lcgstate.m = 1048576;
+    game.lcg_a = 1093;
+    game.lcg_c = 221587;
+    game.lcg_m = 1048576;
     srand(time(NULL));
     long seedval = (long)rand();
     set_seed(seedval);
     srand(time(NULL));
     long seedval = (long)rand();
     set_seed(seedval);
diff --git a/misc.c b/misc.c
index 6f33146c2020ef74badb4ef85b8e89998fe334d7..6ca1f876ac21a2b9e2acfaecbc09c8222acf6117 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -460,21 +460,21 @@ bool TSTBIT(long mask, int bit)
 void set_seed(long seedval)
 /* Set the LCG seed */
 {
 void set_seed(long seedval)
 /* Set the LCG seed */
 {
-    lcgstate.x = (unsigned long) seedval % lcgstate.m;
+    game.lcg_x = (unsigned long) seedval % game.lcg_m;
 }
 
 unsigned long get_next_lcg_value(void)
 /* Return the LCG's current value, and then iterate it. */
 {
 }
 
 unsigned long get_next_lcg_value(void)
 /* Return the LCG's current value, and then iterate it. */
 {
-    unsigned long old_x = lcgstate.x;
-    lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
+    unsigned long old_x = game.lcg_x;
+    game.lcg_x = (game.lcg_a * game.lcg_x + game.lcg_c) % game.lcg_m;
     return old_x;
 }
 
 long randrange(long range)
 /* Return a random integer from [0, range). */
 {
     return old_x;
 }
 
 long randrange(long range)
 /* Return a random integer from [0, range). */
 {
-    return range * get_next_lcg_value() / lcgstate.m;
+    return range * get_next_lcg_value() / game.lcg_m;
 }
 
 long RNDVOC(long second, long force)
 }
 
 long RNDVOC(long second, long force)
index 07602ec40b50a2101a02b842bee959c33c36d42f..475ca94fa7aea635737685bbb3137f4349f203b7 100644 (file)
@@ -111,7 +111,6 @@ ways:
   We don't need whatever minor performance gains this might collect,
   and the choice to refrain will make forward translation into future
   languages easier.
   We don't need whatever minor performance gains this might collect,
   and the choice to refrain will make forward translation into future
   languages easier.
-
 * There are a few gotos left that resist restructuring; all of these
   are in the principal command interpreter function implementing its
   state machine.
 * There are a few gotos left that resist restructuring; all of these
   are in the principal command interpreter function implementing its
   state machine.