Move PRNG initialization to simplify cheat.c

This commit is contained in:
Eric S. Raymond 2017-07-01 08:59:45 -04:00
parent 0a87fc9f78
commit 065caace64
4 changed files with 15 additions and 20 deletions

View file

@ -210,7 +210,7 @@ extern int savefile(FILE *, long);
extern int suspend(void); extern int suspend(void);
extern int resume(void); extern int resume(void);
extern int restore(FILE *); extern int restore(FILE *);
extern void initialise(void); extern long initialise(void);
extern int action(struct command_t *command); extern int action(struct command_t *command);
/* Alas, declaring this static confuses the coverage analyzer */ /* Alas, declaring this static confuses the coverage analyzer */

View file

@ -83,13 +83,6 @@ int main(int argc, char *argv[])
FILE *fp = NULL; FILE *fp = NULL;
game.lcg_a = 1093;
game.lcg_c = 221587;
game.lcg_m = 1048576;
srand(time(NULL));
long seedval = (long)rand();
set_seed(seedval);
/* Initialize game variables */ /* Initialize game variables */
initialise(); initialise();

14
init.c
View file

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h>
#include "advent.h" #include "advent.h"
@ -9,11 +10,20 @@
* Initialisation * Initialisation
*/ */
void initialise(void) long initialise(void)
{ {
if (oldstyle) if (oldstyle)
printf("Initialising...\n"); printf("Initialising...\n");
/* Initialize our LCG PRNG with parameters tested against
* Knuth vol. 2. by the original authors */
game.lcg_a = 1093;
game.lcg_c = 221587;
game.lcg_m = 1048576;
srand(time(NULL));
long seedval = (long)rand();
set_seed(seedval);
for (int i = 1; i <= NOBJECTS; i++) { for (int i = 1; i <= NOBJECTS; i++) {
game.place[i] = LOC_NOWHERE; game.place[i] = LOC_NOWHERE;
} }
@ -57,4 +67,6 @@ void initialise(void)
} }
} }
game.conds = setbit(11); game.conds = setbit(11);
return seedval;
} }

12
main.c
View file

@ -19,7 +19,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <getopt.h> #include <getopt.h>
#include <signal.h> #include <signal.h>
#include <time.h>
#include <string.h> #include <string.h>
#include "advent.h" #include "advent.h"
#include "linenoise/linenoise.h" #include "linenoise/linenoise.h"
@ -138,17 +137,8 @@ int main(int argc, char *argv[])
linenoiseHistorySetMaxLen(350); linenoiseHistorySetMaxLen(350);
/* Initialize our LCG PRNG with parameters tested against
* Knuth vol. 2. by the original authors */
game.lcg_a = 1093;
game.lcg_c = 221587;
game.lcg_m = 1048576;
srand(time(NULL));
long seedval = (long)rand();
set_seed(seedval);
/* Initialize game variables */ /* Initialize game variables */
initialise(); long seedval = initialise();
/* Start-up, dwarf stuff */ /* Start-up, dwarf stuff */
make_zzword(game.zzword); make_zzword(game.zzword);