From 4993be4c086b2a81dbf5af5a2c687cf2f0c93d21 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Wed, 14 Jun 2017 13:00:28 -0400 Subject: [PATCH] Include LCG state in game saves. --- advent.h | 7 +------ main.c | 7 +++---- misc.c | 8 ++++---- notes.adoc | 1 - 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/advent.h b/advent.h index ee8e3fa..4717fe7 100644 --- a/advent.h +++ b/advent.h @@ -14,15 +14,11 @@ #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 { + unsigned long lcg_a, lcg_c, lcg_m, lcg_x; 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 lcg_state lcgstate; /* b is not needed for POSIX but harmless */ #define READ_MODE "rb" diff --git a/main.c b/main.c index 90f6003..6ba8897 100644 --- a/main.c +++ b/main.c @@ -46,7 +46,6 @@ FILE *logfp; bool oldstyle = false; bool editline = true; bool prompt = true; -lcg_state lcgstate; 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 */ - 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); diff --git a/misc.c b/misc.c index 6f33146..6ca1f87 100644 --- 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 */ { - 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 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 range * get_next_lcg_value() / lcgstate.m; + return range * get_next_lcg_value() / game.lcg_m; } long RNDVOC(long second, long force) diff --git a/notes.adoc b/notes.adoc index 07602ec..475ca94 100644 --- a/notes.adoc +++ b/notes.adoc @@ -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. - * There are a few gotos left that resist restructuring; all of these are in the principal command interpreter function implementing its state machine. -- 2.31.1