From: Eric S. Raymond Date: Thu, 28 Feb 2019 10:58:16 +0000 (-0500) Subject: The FORTRAN-derived longs were semantically ints. Make it so. X-Git-Tag: 1.8~17 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=81af59974bcc0c619437cb83064d7c18e27614ec;p=open-adventure.git The FORTRAN-derived longs were semantically ints. Make it so. Note. because we used fixed-length declrations in the right places, this shouldn't break saves. (Besides being right, this will someday make a port to Go a touch easier, if we decide to do that.) --- diff --git a/advent.h b/advent.h index 4808daf..a1fc454 100644 --- a/advent.h +++ b/advent.h @@ -119,15 +119,15 @@ typedef enum { GO_DWARFWAKE, } phase_codes_t; -typedef long vocab_t; // index into a vocabulary array */ -typedef long verb_t; // index into an actions array */ -typedef long obj_t; // index into the object array */ -typedef long loc_t; // index into the locations array */ -typedef long turn_t; // turn counter or threshold */ +typedef int vocab_t; // index into a vocabulary array */ +typedef int verb_t; // index into an actions array */ +typedef int obj_t; // index into the object array */ +typedef int loc_t; // index into the locations array */ +typedef int turn_t; // turn counter or threshold */ struct game_t { int32_t lcg_x; - long abbnum; // How often to print long descriptions + int abbnum; // How often to print int descriptions score_t bonus; // What kind of finishing bonus we are getting loc_t chloc; // pirate chest location loc_t chloc2; // pirate chest alternate location @@ -141,8 +141,8 @@ struct game_t { 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 + int conds; // min value for cond[loc] if loc has any hints + int detail; // level of detail in descriptions /* dflag controls the level of activation of dwarves: * 0 No dwarf stuff yet (wait until reaches Hall Of Mists) @@ -150,15 +150,15 @@ struct game_t { * 2 Met first dwarf, others start moving, no knives thrown yet * 3 A knife has been thrown (first set always misses) * 3+ Dwarves are mad (increases their accuracy) */ - long dflag; + int dflag; - long dkill; // dwarves killed - long dtotal; // total dwarves (including pirate) in loc - long foobar; // progress in saying "FEE FIE FOE FOO". - long holdng; // number of objects being carried - long igo; // # uses of "go" instead of a direction - long iwest; // # times he's said "west" instead of "w" - long knfloc; // knife location; 0 if none, -1 after caveat + int dkill; // dwarves killed + int dtotal; // total dwarves (including pirate) in loc + int foobar; // progress in saying "FEE FIE FOE FOO". + int holdng; // number of objects being carried + int igo; // # uses of "go" instead of a direction + int iwest; // # times he's said "west" instead of "w" + int knfloc; // knife location; 0 if none, -1 after caveat turn_t limit; // lifetime of lamp loc_t loc; // where player is now loc_t newloc; // where player is going @@ -166,24 +166,24 @@ struct game_t { loc_t oldloc; // where player was loc_t oldlc2; // where player was two moves ago obj_t oldobj; // last object player handled - long saved; // point penalty for saves - long tally; // count of treasures gained - long thresh; // current threshold for endgame scoring tier + int saved; // point penalty for saves + int tally; // count of treasures gained + int 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) char zzword[TOKLEN + 1]; // randomly generated magic word from bird - 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 + int abbrev[NLOCATIONS + 1]; // has location been seen? + int atloc[NLOCATIONS + 1]; // head of object linked list per location + int dseen[NDWARVES + 1]; // true if dwarf has seen him loc_t dloc[NDWARVES + 1]; // location of dwarves, initially hard-wired in loc_t odloc[NDWARVES + 1]; // prior loc of each dwarf, initially garbage loc_t fixed[NOBJECTS + 1]; // fixed location of object (if not IS_FREE) - obj_t link[NOBJECTS * 2 + 1]; // object-list links + obj_t link[NOBJECTS * 2 + 1];// object-list links loc_t place[NOBJECTS + 1]; // location of object - long hinted[NHINTS]; // hinted[i] = true iff hint i has been used. - long hintlc[NHINTS]; // hintlc[i] = how long at LOC with cond bit i - long prop[NOBJECTS + 1]; // object state array */ + int hinted[NHINTS]; // hinted[i] = true iff hint i has been used. + int hintlc[NHINTS]; // hintlc[i] = how int at LOC with cond bit i + int prop[NOBJECTS + 1]; // object state array */ }; /* @@ -226,21 +226,21 @@ extern bool silent_yes(void); extern bool yes(const char*, const char*, const char*); extern void juggle(obj_t); extern void move(obj_t, loc_t); -extern loc_t put(obj_t, long, long); +extern loc_t put(obj_t, int, int); extern void carry(obj_t, loc_t); extern void drop(obj_t, loc_t); extern int atdwrf(loc_t); -extern long setbit(int); -extern bool tstbit(long, int); +extern int setbit(int); +extern bool tstbit(int, int); extern void set_seed(int32_t); extern int32_t randrange(int32_t); -extern long score(enum termination); +extern int score(enum termination); extern void terminate(enum termination) __attribute__((noreturn)); extern int savefile(FILE *, int32_t); extern int suspend(void); extern int resume(void); extern int restore(FILE *); -extern long initialise(void); +extern int initialise(void); extern phase_codes_t action(command_t); extern void state_change(obj_t, int); extern bool is_valid(struct game_t); diff --git a/cheat.c b/cheat.c index 008fb9c..0eee14b 100644 --- a/cheat.c +++ b/cheat.c @@ -43,19 +43,19 @@ int main(int argc, char *argv[]) switch (ch) { case 'd': game.numdie = (turn_t)atoi(optarg); - printf("cheat: game.numdie = %ld\n", game.numdie); + printf("cheat: game.numdie = %d\n", game.numdie); break; case 'l': game.limit = (turn_t)atoi(optarg); - printf("cheat: game.limit = %ld\n", game.limit); + printf("cheat: game.limit = %d\n", game.limit); break; case 's': - game.saved = (long)atoi(optarg); - printf("cheat: game.saved = %ld\n", game.saved); + game.saved = (int)atoi(optarg); + printf("cheat: game.saved = %d\n", game.saved); break; case 't': game.turns = (turn_t)atoi(optarg); - printf("cheat: game.turns = %ld\n", game.turns); + printf("cheat: game.turns = %d\n", game.turns); break; case 'v': version = atoi(optarg); diff --git a/init.c b/init.c index 4ff52a8..a4a77aa 100644 --- a/init.c +++ b/init.c @@ -43,13 +43,13 @@ struct game_t game = { .foobar = WORD_EMPTY, }; -long initialise(void) +int initialise(void) { if (settings.oldstyle) printf("Initialising...\n"); srand(time(NULL)); - long seedval = (long)rand(); + int seedval = (int)rand(); set_seed(seedval); for (int i = 1; i <= NOBJECTS; i++) { diff --git a/main.c b/main.c index 5bf7d95..2840f43 100644 --- a/main.c +++ b/main.c @@ -96,7 +96,7 @@ int main(int argc, char *argv[]) } /* Initialize game variables */ - long seedval = initialise(); + int seedval = initialise(); #ifndef ADVENT_NOSAVE if (!rfp) { @@ -113,7 +113,7 @@ int main(int argc, char *argv[]) #endif if (settings.logfp) - fprintf(settings.logfp, "seed %ld\n", seedval); + fprintf(settings.logfp, "seed %d\n", seedval); /* interpret commands until EOF or interrupt */ for (;;) { @@ -129,7 +129,7 @@ int main(int argc, char *argv[]) terminate(quitgame); } -/* Check if this loc is eligible for any hints. If been here long +/* Check if this loc is eligible for any hints. If been here int * enough, display. Ignore "HINTS" < 4 (special stuff, see database * notes). */ static void checkhints(void) @@ -141,7 +141,7 @@ static void checkhints(void) if (!CNDBIT(game.loc, hint + 1 + COND_HBASE)) game.hintlc[hint] = -1; ++game.hintlc[hint]; - /* Come here if he's been long enough at required loc(s) for some + /* Come here if he's been int enough at required loc(s) for some * unused hint. */ if (game.hintlc[hint] >= hints[hint].turns) { int i; @@ -653,8 +653,8 @@ static void playermove(int motion) for (;;) { /* L12 loop */ for (;;) { enum condtype_t condtype = travel[travel_entry].condtype; - long condarg1 = travel[travel_entry].condarg1; - long condarg2 = travel[travel_entry].condarg2; + int condarg1 = travel[travel_entry].condarg1; + int condarg2 = travel[travel_entry].condarg2; if (condtype < cond_not) { /* YAML N and [pct N] conditionals */ if (condtype == cond_goto || condtype == cond_pct) { diff --git a/misc.c b/misc.c index 3633928..f2d6742 100644 --- a/misc.c +++ b/misc.c @@ -483,7 +483,7 @@ static void tokenize(char* raw, command_t *cmd) /* Bound prefix on the %s would be needed to prevent buffer * overflow. but we shortstop this more simply by making each - * raw-input buffer as long as the entire input buffer. */ + * raw-input buffer as int as the entire input buffer. */ sscanf(raw, "%s%s", cmd->word[0].raw, cmd->word[1].raw); /* (ESR) In oldstyle mode, simulate the uppercasing and truncating @@ -544,7 +544,7 @@ bool get_command_input(command_t *command) const char *types[] = {"NO_WORD_TYPE", "MOTION", "OBJECT", "ACTION", "NUMERIC"}; /* needs to stay synced with enum speechpart */ const char *roles[] = {"unknown", "intransitive", "transitive"}; - printf("Command: role = %s type1 = %s, id1 = %ld, type2 = %s, id2 = %ld\n", + printf("Command: role = %s type1 = %s, id1 = %d, type2 = %s, id2 = %d\n", roles[command->part], types[command->word[0].type], command->word[0].id, @@ -597,7 +597,7 @@ void move(obj_t object, loc_t where) drop(object, where); } -loc_t put(obj_t object, loc_t where, long pval) +loc_t put(obj_t object, loc_t where, int pval) /* put() is the same as move(), except it returns a value used to set up the * negated game.prop values for the repository objects. */ { @@ -610,7 +610,7 @@ void carry(obj_t object, loc_t where) * location. Incr holdng unless it was already being toted. If object>NOBJECTS * (moving "fixed" second loc), don't change game.place or game.holdng. */ { - long temp; + int temp; if (object <= NOBJECTS) { if (game.place[object] == CARRIED) @@ -667,7 +667,7 @@ int atdwrf(loc_t where) if (game.dflag < 2) return at; at = -1; - for (long i = 1; i <= NDWARVES - 1; i++) { + for (int i = 1; i <= NDWARVES - 1; i++) { if (game.dloc[i] == where) return i; if (game.dloc[i] != 0) @@ -679,13 +679,13 @@ int atdwrf(loc_t where) /* Utility routines (setbit, tstbit, set_seed, get_next_lcg_value, * randrange) */ -long setbit(int bit) +int setbit(int bit) /* Returns 2**bit for use in constructing bit-masks. */ { return (1L << bit); } -bool tstbit(long mask, int bit) +bool tstbit(int mask, int bit) /* Returns true if the specified bit is set in the mask. */ { return (mask & (1 << bit)) != 0; diff --git a/saveresume.c b/saveresume.c index 6e73017..eab902a 100644 --- a/saveresume.c +++ b/saveresume.c @@ -23,7 +23,7 @@ /* * If you change the first three members, the resume function may not properly * reject saves from older versions. Yes, this glues us to a hardware- - * dependent length of long. Later members can change, but bump the version + * dependent length of int. Later members can change, but bump the version * when you do that. */ struct save_t { @@ -190,7 +190,7 @@ bool is_valid(struct game_t valgame) } /* Recalculate tally, throw the towel if in disagreement */ - long temp_tally = 0; + int temp_tally = 0; for (int treasure = 1; treasure <= NOBJECTS; treasure++) { if (objects[treasure].is_treasure) { if (valgame.prop[treasure] == STATE_NOTFOUND) { diff --git a/score.c b/score.c index 368b1df..275a358 100644 --- a/score.c +++ b/score.c @@ -11,7 +11,7 @@ static int mxscor; /* ugh..the price for having score() not exit. */ -long score(enum termination mode) +int score(enum termination mode) /* mode is 'scoregame' if scoring, 'quitgame' if quitting, 'endgame' if died * or won */ { @@ -116,14 +116,14 @@ long score(enum termination mode) void terminate(enum termination mode) /* End of game. Let's tell him all about it. */ { - long points = score(mode); + int points = score(mode); if (points + game.trnluz + 1 >= mxscor && game.trnluz != 0) rspeak(TOOK_LONG); if (points + game.saved + 1 >= mxscor && game.saved != 0) rspeak(WITHOUT_SUSPENDS); rspeak(TOTAL_SCORE, points, mxscor, game.turns, game.turns); - for (int i = 1; i <= (long)NCLASSES; i++) { + for (int i = 1; i <= (int)NCLASSES; i++) { if (classes[i].threshold >= points) { speak(classes[i].message); i = classes[i].threshold + 1 - points;