Include LCG state in game saves.

This commit is contained in:
Eric S. Raymond 2017-06-14 13:00:28 -04:00
parent fdd72c6417
commit 4993be4c08
4 changed files with 8 additions and 15 deletions

View file

@ -14,15 +14,11 @@
#define INTRANSITIVE -1 /* illegal object number */ #define INTRANSITIVE -1 /* illegal object number */
#define SPECIALBASE 300 /* base umber of special rooms */ #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 token_t; /* word token - someday this will be char[TOKLEN+1] */
typedef long vocab_t; /* index into a vocabulary array */ typedef long vocab_t; /* index into a vocabulary array */
struct game_t { struct game_t {
unsigned long lcg_a, lcg_c, lcg_m, lcg_x;
long abbnum; long abbnum;
long blklin; long blklin;
long bonus; long bonus;
@ -81,7 +77,6 @@ extern const char ascii_to_advent[];
extern const char advent_to_ascii[]; extern const char advent_to_ascii[];
extern FILE *logfp; extern FILE *logfp;
extern bool oldstyle, editline, prompt; extern bool oldstyle, editline, prompt;
extern lcg_state lcgstate;
/* b is not needed for POSIX but harmless */ /* b is not needed for POSIX but harmless */
#define READ_MODE "rb" #define READ_MODE "rb"

7
main.c
View file

@ -46,7 +46,6 @@ FILE *logfp;
bool oldstyle = false; bool oldstyle = false;
bool editline = true; bool editline = true;
bool prompt = true; bool prompt = true;
lcg_state lcgstate;
extern void initialise(); extern void initialise();
extern void score(long); extern void score(long);
@ -114,9 +113,9 @@ int main(int argc, char *argv[])
/* Initialize our LCG PRNG with parameters tested against /* Initialize our LCG PRNG with parameters tested against
* Knuth vol. 2. by the original authors */ * Knuth vol. 2. by the original authors */
lcgstate.a = 1093; game.lcg_a = 1093;
lcgstate.c = 221587; game.lcg_c = 221587;
lcgstate.m = 1048576; game.lcg_m = 1048576;
srand(time(NULL)); srand(time(NULL));
long seedval = (long)rand(); long seedval = (long)rand();
set_seed(seedval); set_seed(seedval);

8
misc.c
View file

@ -460,21 +460,21 @@ bool TSTBIT(long mask, int bit)
void set_seed(long seedval) void set_seed(long seedval)
/* Set the LCG seed */ /* 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) unsigned long get_next_lcg_value(void)
/* Return the LCG's current value, and then iterate it. */ /* Return the LCG's current value, and then iterate it. */
{ {
unsigned long old_x = lcgstate.x; unsigned long old_x = game.lcg_x;
lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m; game.lcg_x = (game.lcg_a * game.lcg_x + game.lcg_c) % game.lcg_m;
return old_x; return old_x;
} }
long randrange(long range) long randrange(long range)
/* Return a random integer from [0, 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)

View file

@ -111,7 +111,6 @@ ways:
We don't need whatever minor performance gains this might collect, We don't need whatever minor performance gains this might collect,
and the choice to refrain will make forward translation into future and the choice to refrain will make forward translation into future
languages easier. languages easier.
* There are a few gotos left that resist restructuring; all of these * There are a few gotos left that resist restructuring; all of these
are in the principal command interpreter function implementing its are in the principal command interpreter function implementing its
state machine. state machine.