Put some of important parameters into fixed width signed integers
authorNHOrus <jy6x2b32pie9@yahoo.com>
Sun, 10 Sep 2017 21:13:55 +0000 (00:13 +0300)
committerNHOrus <jy6x2b32pie9@yahoo.com>
Sun, 10 Sep 2017 21:58:13 +0000 (00:58 +0300)
Massaged test files. Fixes issue #41

actions.c
advent.h
init.c
misc.c
saveresume.c
tests/illformed.chk
tests/resumefail2.chk

index 3808f9b119b8ea836eafac3308a7774c73d5106e..e41c7bf5913b3918c84787af649e55ae6103e8fc 100644 (file)
--- a/actions.c
+++ b/actions.c
@@ -1250,7 +1250,7 @@ static int wake(verb_t verb, obj_t obj)
 static int seed(verb_t verb, const char *arg)
 /* Set seed */
 {
 static int seed(verb_t verb, const char *arg)
 /* Set seed */
 {
-    int seed = atoi(arg);
+    int32_t seed = strtol(arg, NULL, 10);
     speak(actions[verb].message, seed);
     set_seed(seed);
     --game.turns;
     speak(actions[verb].message, seed);
     set_seed(seed);
     --game.turns;
index 17ae719d51231dc82430eb6eaec53278e0173123..788d65934e8b88cea5019e40136927f5ed261456 100644 (file)
--- a/advent.h
+++ b/advent.h
@@ -2,9 +2,16 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <stdarg.h>
+#include <inttypes.h>
 
 #include "dungeon.h"
 
 
 #include "dungeon.h"
 
+/* LCG PRNG parameters tested against
+ * Knuth vol. 2. by the original authors */
+#define LCG_A 1093
+#define LCG_C 221587
+#define LCG_M 1048576
+
 #define LINESIZE       1024
 #define TOKLEN         5          // № sigificant characters in a token */
 #define NDWARVES       6          // number of dwarves
 #define LINESIZE       1024
 #define TOKLEN         5          // № sigificant characters in a token */
 #define NDWARVES       6          // number of dwarves
@@ -112,7 +119,7 @@ typedef long loc_t;    // index into the locations array */
 typedef long turn_t;   // turn counter or threshold */
 
 struct game_t {
 typedef long turn_t;   // turn counter or threshold */
 
 struct game_t {
-    unsigned long lcg_a, lcg_c, lcg_m, lcg_x;
+    int64_t lcg_x;
     long abbnum;                 // How often to print long descriptions
     score_t bonus;               // What kind of finishing bonus we are getting
     loc_t chloc;                 // pirate chest location
     long abbnum;                 // How often to print long descriptions
     score_t bonus;               // What kind of finishing bonus we are getting
     loc_t chloc;                 // pirate chest location
@@ -122,6 +129,11 @@ struct game_t {
     bool clshnt;                 // has player read the clue in the endgame?
     bool closed;                 // whether we're all the way closed
     bool closng;                 // whether it's closing time yet
     bool clshnt;                 // has player read the clue in the endgame?
     bool closed;                 // whether we're all the way closed
     bool closng;                 // whether it's closing time yet
+    bool lmwarn;                 // has player been warned about lamp going dim?
+    bool novice;                 // asked for instructions at start-up?
+    bool panic;                  // has player found out he's trapped?
+    bool wzdark;                 // whether the loc he's leaving was dark
+    bool blooded;                // has player drunk of dragon's blood?
     long conds;                  // min value for cond[loc] if loc has any hints
     long detail;                 // level of detail in descriptions
 
     long conds;                  // min value for cond[loc] if loc has any hints
     long detail;                 // level of detail in descriptions
 
@@ -141,24 +153,19 @@ struct game_t {
     long iwest;                  // # times he's said "west" instead of "w"
     long knfloc;                 // knife location; 0 if none, -1 after caveat
     turn_t limit;                // lifetime of lamp
     long iwest;                  // # times he's said "west" instead of "w"
     long knfloc;                 // knife location; 0 if none, -1 after caveat
     turn_t limit;                // lifetime of lamp
-    bool lmwarn;                 // has player been warned about lamp going dim?
     loc_t loc;                   // where player is now
     loc_t newloc;                // where player is going
     loc_t loc;                   // where player is now
     loc_t newloc;                // where player is going
-    bool novice;                 // asked for instructions at start-up?
     turn_t numdie;               // number of times killed so far
     loc_t oldloc;                // where player was
     loc_t oldlc2;                // where player was two moves ago
     obj_t oldobj;                // last object player handled
     turn_t numdie;               // number of times killed so far
     loc_t oldloc;                // where player was
     loc_t oldlc2;                // where player was two moves ago
     obj_t oldobj;                // last object player handled
-    bool panic;                  // has player found out he's trapped?
     long saved;                  // point penalty for saves
     long tally;                  // count of treasures gained
     long thresh;                 // current threshold for endgame scoring tier
     turn_t trndex;               // FIXME: not used, remove on next format bump
     turn_t trnluz;               // # points lost so far due to turns used
     turn_t turns;                // counts commands given (ignores yes/no)
     long saved;                  // point penalty for saves
     long tally;                  // count of treasures gained
     long thresh;                 // current threshold for endgame scoring tier
     turn_t trndex;               // FIXME: not used, remove on next format bump
     turn_t trnluz;               // # points lost so far due to turns used
     turn_t turns;                // counts commands given (ignores yes/no)
-    bool wzdark;                 // whether the loc he's leaving was dark
     char zzword[TOKLEN + 1];     // randomly generated magic word from bird
     char zzword[TOKLEN + 1];     // randomly generated magic word from bird
-    bool blooded;                // has player drunk of dragon's blood?
     long abbrev[NLOCATIONS + 1]; // has location been seen?
     long atloc[NLOCATIONS + 1];  // head of object linked list per location
     long dseen[NDWARVES + 1];    // true if dwarf has seen him
     long abbrev[NLOCATIONS + 1]; // has location been seen?
     long atloc[NLOCATIONS + 1];  // head of object linked list per location
     long dseen[NDWARVES + 1];    // true if dwarf has seen him
@@ -214,11 +221,11 @@ extern void drop(obj_t, loc_t);
 extern int atdwrf(loc_t);
 extern long setbit(int);
 extern bool tstbit(long, int);
 extern int atdwrf(loc_t);
 extern long setbit(int);
 extern bool tstbit(long, int);
-extern void set_seed(long);
-extern long randrange(long);
+extern void set_seed(int32_t);
+extern int32_t randrange(int32_t);
 extern long score(enum termination);
 extern void terminate(enum termination) __attribute__((noreturn));
 extern long score(enum termination);
 extern void terminate(enum termination) __attribute__((noreturn));
-extern int savefile(FILE *, long);
+extern int savefile(FILE *, int32_t);
 extern int suspend(void);
 extern int resume(void);
 extern int restore(FILE *);
 extern int suspend(void);
 extern int resume(void);
 extern int restore(FILE *);
diff --git a/init.c b/init.c
index be5c3fb271c0da846c96422c6c5e2f4ba6574894..fbe44106e30b3b3148b7bc548ce27d5cc5ca6255 100644 (file)
--- a/init.c
+++ b/init.c
@@ -37,12 +37,6 @@ struct game_t game = {
     .loc     = LOC_START,
     .limit   = GAMELIMIT,
     .foobar  = WORD_EMPTY,
     .loc     = LOC_START,
     .limit   = GAMELIMIT,
     .foobar  = WORD_EMPTY,
-
-    /* Initialize our LCG PRNG with parameters tested against
-     * Knuth vol. 2. by the original authors */
-    .lcg_a = 1093,
-    .lcg_c = 221587,
-    .lcg_m = 1048576,
 };
 
 long initialise(void)
 };
 
 long initialise(void)
diff --git a/misc.c b/misc.c
index 03681d0b07389b6eaf7af1cbd5f6eb2da7ec62b2..302e61cc641d4faeac7b176839ddd2aa05cac9a1 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -6,6 +6,7 @@
 #include <sys/time.h>
 #include <ctype.h>
 #include <editline/readline.h>
 #include <sys/time.h>
 #include <ctype.h>
 #include <editline/readline.h>
+#include <inttypes.h>
 
 #include "advent.h"
 #include "dungeon.h"
 
 #include "advent.h"
 #include "dungeon.h"
@@ -66,8 +67,8 @@ static void vspeak(const char* msg, bool blank, va_list ap)
             i++;
             // Integer specifier.
             if (msg[i] == 'd') {
             i++;
             // Integer specifier.
             if (msg[i] == 'd') {
-                long arg = va_arg(ap, long);
-                int ret = snprintf(renderp, size, "%ld", arg);
+                int32_t arg = va_arg(ap, int32_t);
+                int ret = snprintf(renderp, size, "%" PRId32, arg);
                 if (ret < size) {
                     renderp += ret;
                     size -= ret;
                 if (ret < size) {
                     renderp += ret;
                     size -= ret;
@@ -642,10 +643,10 @@ bool tstbit(long mask, int bit)
     return (mask & (1 << bit)) != 0;
 }
 
     return (mask & (1 << bit)) != 0;
 }
 
-void set_seed(long seedval)
+void set_seed(int32_t seedval)
 /* Set the LCG seed */
 {
 /* Set the LCG seed */
 {
-    game.lcg_x = (unsigned long) seedval % game.lcg_m;
+    game.lcg_x = (uint32_t) seedval % LCG_M;
 
     // once seed is set, we need to generate the Z`ZZZ word
     for (int i = 0; i < 5; ++i) {
 
     // once seed is set, we need to generate the Z`ZZZ word
     for (int i = 0; i < 5; ++i) {
@@ -655,18 +656,18 @@ void set_seed(long seedval)
     game.zzword[5] = '\0';
 }
 
     game.zzword[5] = '\0';
 }
 
-static unsigned long get_next_lcg_value(void)
+static int32_t 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 = game.lcg_x;
-    game.lcg_x = (game.lcg_a * game.lcg_x + game.lcg_c) % game.lcg_m;
+    int32_t old_x = game.lcg_x;
+    game.lcg_x = (LCG_A * game.lcg_x + LCG_C) % LCG_M;
     return old_x;
 }
 
     return old_x;
 }
 
-long randrange(long range)
+int32_t randrange(int32_t range)
 /* Return a random integer from [0, range). */
 {
 /* Return a random integer from [0, range). */
 {
-    return range * get_next_lcg_value() / game.lcg_m;
+    return range * get_next_lcg_value() / LCG_M;
 }
 
 // LCOV_EXCL_START
 }
 
 // LCOV_EXCL_START
index cc000d8acf2ffbd8d291e6c70b321313246de612..f1d52d458423cedd5e01e7bff1dcfc4c45288eff 100644 (file)
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <editline/readline.h>
 #include <time.h>
 #include <string.h>
 #include <editline/readline.h>
 #include <time.h>
+#include <inttypes.h>
 
 #include "advent.h"
 #include "dungeon.h"
 
 #include "advent.h"
 #include "dungeon.h"
@@ -11,7 +12,7 @@
  * see the history.adoc file in the source distribution for discussion.
  */
 
  * see the history.adoc file in the source distribution for discussion.
  */
 
-#define VRSION 27      /* bump on save format change */
+#define VRSION 28      /* bump on save format change */
 
 /*
  * If you change the first three members, the resume function may not properly
 
 /*
  * If you change the first three members, the resume function may not properly
  * when you do that.
  */
 struct save_t {
  * when you do that.
  */
 struct save_t {
-    long savetime;
-    long mode;         /* not used, must be present for version detection */
-    long version;
+    int64_t savetime;
+    int32_t mode;              /* not used, must be present for version detection */
+    int32_t version;
     struct game_t game;
 };
 struct save_t save;
 
 #define IGNORE(r) do{if (r){}}while(0)
 
     struct game_t game;
 };
 struct save_t save;
 
 #define IGNORE(r) do{if (r){}}while(0)
 
-int savefile(FILE *fp, long version)
+int savefile(FILE *fp, int32_t version)
 /* Save game to file. No input or output from user. */
 {
     save.savetime = time(NULL);
 /* Save game to file. No input or output from user. */
 {
     save.savetime = time(NULL);
@@ -139,15 +140,9 @@ bool is_valid(struct game_t* valgame)
         return false;
     }
 
         return false;
     }
 
-    /* Prevent RNG substitution. Why we are saving PRNG parameters? */
-
-    if (valgame->lcg_a != game.lcg_a || valgame->lcg_c != game.lcg_c || valgame->lcg_m != game.lcg_m) {
-        return false;
-    }
-
     /* Check for RNG overflow. Truncate */
     /* Check for RNG overflow. Truncate */
-    if (valgame->lcg_x >= game.lcg_m) {
-        valgame->lcg_x %= game.lcg_m;
+    if (valgame->lcg_x >= LCG_M) {
+        valgame->lcg_x %= LCG_M;
     }
 
     /*  Bounds check for locations */
     }
 
     /*  Bounds check for locations */
@@ -241,4 +236,4 @@ bool is_valid(struct game_t* valgame)
     return true;
 }
 
     return true;
 }
 
-/* end */
\ No newline at end of file
+/* end */
index 20f18c979563268a6c47064bc7ade99698b470ad..3c763afed36d7a6ce538368ae505dfab3d3e2b00 100644 (file)
@@ -669,7 +669,7 @@ The grate is locked.
 
 > seed -123
 
 
 > seed -123
 
-Seed set to 4294967173
+Seed set to -123
 
 You're outside grate.
 
 
 You're outside grate.
 
index cdda771e755426de84a5b437978b6a5e3a5430d1..a34e001024ed9dc7c4effdfab3e152c96956c36d 100644 (file)
@@ -11,7 +11,7 @@ down a gully.
 Can't open file y, try again.
 
 I'm sorry, but that Adventure was begun using Version -133.-7 of the
 Can't open file y, try again.
 
 I'm sorry, but that Adventure was begun using Version -133.-7 of the
-save file format, and this program uses Version 2.7.  You must find an instance
+save file format, and this program uses Version 2.8.  You must find an instance
 using that other version in order to resume that Adventure.
 
 You're in front of building.
 using that other version in order to resume that Adventure.
 
 You're in front of building.