From: Eric S. Raymond Date: Mon, 3 Jul 2017 11:14:15 +0000 (-0400) Subject: Magic-number elimination. X-Git-Tag: takebird~70 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=eba8015059c1ad2d20e55be9ed69c22aec1871be;p=open-adventure.git Magic-number elimination. --- diff --git a/actions.c b/actions.c index 5de7cb0..9ddbb56 100644 --- a/actions.c +++ b/actions.c @@ -171,7 +171,7 @@ static int bigwords(token_t foo) * Look up foo in special section of vocab to determine which word we've got. * Last word zips the eggs back to the giant room (unless already there). */ { - char word[6]; + char word[TOKLEN+1]; packed_to_token(foo, word); int k = (int) get_special_vocab_id(word); int spk = NOTHING_HAPPENS; @@ -1043,7 +1043,7 @@ static int say(struct command_t *command) b = command->wd2x; command->wd1 = command->wd2; } - char word1[6]; + char word1[TOKLEN+1]; packed_to_token(command->wd1, word1); int wd = (int) get_vocab_id(word1); /* FIXME: magic numbers */ diff --git a/advent.h b/advent.h index 2ef6a59..0852440 100644 --- a/advent.h +++ b/advent.h @@ -6,6 +6,7 @@ #include "dungeon.h" #define LINESIZE 1024 +#define TOKLEN 5 // # sigificant character sin a token */ #define NDWARVES 6 // number of dwarves #define PIRATE NDWARVES // must be NDWARVES-1 when zero-origin #define DALTLC LOC_NUGGET // alternate dwarf location @@ -151,7 +152,7 @@ struct game_t { long trnluz; // # points lost so far due to number of turns used long turns; // how many commands he's given (ignores yes/no) bool wzdark; // whether the loc he's leaving was dark - char zzword[6]; // 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]; long atloc[NLOCATIONS + 1]; @@ -190,7 +191,7 @@ extern struct settings_t settings; extern char* xstrdup(const char* s); extern void* xmalloc(size_t size); extern void packed_to_token(long, char token[]); -extern long token_to_packed(const char token[6]); +extern long token_to_packed(const char token[TOKLEN+1]); extern void tokenize(char*, long tokens[4]); extern void vspeak(const char*, bool, va_list); extern bool wordeq(token_t, token_t); diff --git a/main.c b/main.c index 4f46c0a..3b48af3 100644 --- a/main.c +++ b/main.c @@ -1089,8 +1089,8 @@ L2607: } else lampcheck(); - char word1[6]; - char word2[6]; + char word1[TOKLEN+1]; + char word2[TOKLEN+1]; packed_to_token(command.wd1, word1); packed_to_token(command.wd2, word2); V1 = get_vocab_id(word1); diff --git a/misc.c b/misc.c index b1c50d1..6f80c9d 100644 --- a/misc.c +++ b/misc.c @@ -35,7 +35,7 @@ void* xmalloc(size_t size) return (ptr); } -void packed_to_token(long packed, char token[6]) +void packed_to_token(long packed, char token[TOKLEN+1]) { // The advent->ascii mapping. const char advent_to_ascii[] = { @@ -68,7 +68,7 @@ void packed_to_token(long packed, char token[6]) } } -long token_to_packed(const char token[6]) +long token_to_packed(const char token[TOKLEN+1]) { const char ascii_to_advent[] = { 63, 63, 63, 63, 63, 63, 63, 63, @@ -113,7 +113,7 @@ void tokenize(char* raw, long tokens[4]) int word_count = sscanf(raw, "%s%s", words[0], words[1]); // make space for substrings and zero it out - char chunk_data[][6] = { + char chunk_data[][TOKLEN+1] = { {"\0\0\0\0\0"}, {"\0\0\0\0\0"}, {"\0\0\0\0\0"}, @@ -679,7 +679,7 @@ long randrange(long range) return range * get_next_lcg_value() / game.lcg_m; } -void make_zzword(char zzword[6]) +void make_zzword(char zzword[TOKLEN+1]) { for (int i = 0; i < 5; ++i) { zzword[i] = 'A' + randrange(26);