X-Git-Url: https://jxself.org/git/?p=open-adventure.git;a=blobdiff_plain;f=misc.c;h=80e4352b74b7b2ea8feee8a2d91798e50d61c9f7;hp=a98ce1a47fab0ab773a7a3e99614c3f4f8692591;hb=5cdaa301dcf6ececc1d2a9e340d8da698a9f55d8;hpb=23ae7edb10e50d2a4fbeb3c56101b7a0c62e5a3d diff --git a/misc.c b/misc.c index a98ce1a..80e4352 100644 --- a/misc.c +++ b/misc.c @@ -6,6 +6,7 @@ #include #include #include +#include #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') { - 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; @@ -388,22 +389,21 @@ static int get_action_vocab_id(const char* word) return (WORD_NOT_FOUND); } -static bool is_valid_int(const char *str) -/* Returns true if the string passed in is represents a valid integer, +static bool is_valid_int(const char *str) +/* Returns true if the string passed in is represents a valid integer, * that could then be parsed by atoi() */ { // Handle negative number if (*str == '-') ++str; - // Handle empty string or just "-". Should never reach this + // Handle empty string or just "-". Should never reach this // point, because this is only used with transitive verbs. if (!*str) return false; // LCOV_EXCL_LINE // Check for non-digit chars in the rest of the stirng. - while (*str) - { + while (*str) { if (!isdigit(*str)) return false; else @@ -464,9 +464,9 @@ static void get_vocab_metadata(command_word_t* word) return; } -static void tokenize(char* raw, struct command_t *cmd) +static void tokenize(char* raw, command_t *cmd) { - memset(cmd, '\0', sizeof(struct command_t)); + memset(cmd, '\0', sizeof(command_t)); /* Bound prefix on the %s would be needed to prevent buffer * overflow. but we shortstop this more simply by making each @@ -500,7 +500,7 @@ static void tokenize(char* raw, struct command_t *cmd) get_vocab_metadata(&(cmd->word[1])); } -bool get_command_input(struct command_t *command) +bool get_command_input(command_t *command) /* Get user input on stdin, parse and map to command */ { char inputbuf[LINESIZE]; @@ -643,11 +643,13 @@ bool tstbit(long mask, int bit) return (mask & (1 << bit)) != 0; } -void set_seed(long seedval) +void set_seed(int32_t seedval) /* Set the LCG seed */ { - game.lcg_x = (unsigned long) seedval % game.lcg_m; - + game.lcg_x = seedval % LCG_M; + if (game.lcg_x < 0) { + game.lcg_x = LCG_M + game.lcg_x; + } // once seed is set, we need to generate the Z`ZZZ word for (int i = 0; i < 5; ++i) { game.zzword[i] = 'A' + randrange(26); @@ -656,18 +658,18 @@ void set_seed(long seedval) 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. */ { - 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; } -long randrange(long range) +int32_t randrange(int32_t 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