From bf2fa227f0786952ae4a632a1520bf1cf6663c98 Mon Sep 17 00:00:00 2001 From: NHOrus Date: Thu, 29 Jun 2017 20:11:53 +0300 Subject: [PATCH] Unshadowed k2 into k3 in main.c Reindented everything --- actions.c | 107 +++++++-------- cheat.c | 16 +-- init.c | 12 +- main.c | 141 ++++++++++--------- misc.c | 379 +++++++++++++++++++++++++-------------------------- saveresume.c | 10 +- score.c | 4 +- 7 files changed, 325 insertions(+), 344 deletions(-) diff --git a/actions.c b/actions.c index e01d2cf..07644ab 100644 --- a/actions.c +++ b/actions.c @@ -28,7 +28,7 @@ static int attack(struct command_t *command) if (AT(TROLL))obj = obj * NOBJECTS + TROLL; if (AT(OGRE))obj = obj * NOBJECTS + OGRE; if (HERE(BEAR) && game.prop[BEAR] == UNTAMED_BEAR) - obj = obj * NOBJECTS + BEAR; + obj = obj * NOBJECTS + BEAR; if (obj > NOBJECTS) return GO_UNKNOWN; if (obj == 0) { /* Can't attack bird or machine by throwing axe. */ @@ -50,8 +50,8 @@ static int attack(struct command_t *command) DESTROY(BIRD); spk = BIRD_DEAD; } else if (obj == VEND) { - state_change(VEND, - game.prop[VEND]==VEND_BLOCKS ? VEND_UNBLOCKS : VEND_BLOCKS); + state_change(VEND, + game.prop[VEND] == VEND_BLOCKS ? VEND_UNBLOCKS : VEND_BLOCKS); return GO_CLEAROBJ; } @@ -85,17 +85,14 @@ static int attack(struct command_t *command) * fixed), move rug there (not fixed), and move him there, * too. Then do a null motion to get new description. */ rspeak(BARE_HANDS_QUERY); - if(silent_yes()) - { - // FIXME: setting wd1 is a workaround for broken logic - command->wd1 = token_to_packed("Y"); - } - else - { - // FIXME: setting wd1 is a workaround for broken logic - command->wd1 = token_to_packed("N"); - return GO_CHECKFOO; - } + if (silent_yes()) { + // FIXME: setting wd1 is a workaround for broken logic + command->wd1 = token_to_packed("Y"); + } else { + // FIXME: setting wd1 is a workaround for broken logic + command->wd1 = token_to_packed("N"); + return GO_CHECKFOO; + } pspeak(DRAGON, look, 3); game.prop[DRAGON] = 1; game.prop[RUG] = 0; @@ -241,9 +238,9 @@ static int vcarry(token_t verb, token_t obj) spk = YOU_JOKING; if (obj == PLANT && game.prop[PLANT] <= 0)spk = DEEP_ROOTS; if (obj == BEAR && game.prop[BEAR] == SITTING_BEAR) - spk = BEAR_CHAINED; + spk = BEAR_CHAINED; if (obj == CHAIN && game.prop[BEAR] != UNTAMED_BEAR) - spk = STILL_LOCKED; + spk = STILL_LOCKED; if (obj == URN)spk = URN_NOBUDGE; if (obj == CAVITY)spk = DOUGHNUT_HOLES; if (obj == BLOOD)spk = FEW_DROPS; @@ -264,7 +261,7 @@ static int vcarry(token_t verb, token_t obj) return (fill(verb, BOTTLE)); else { if (game.prop[BOTTLE] != EMPTY_BOTTLE) - spk = BOTTLE_FULL; + spk = BOTTLE_FULL; if (!TOTING(BOTTLE))spk = NO_CONTAINER; rspeak(spk); return GO_CLEAROBJ; @@ -311,9 +308,9 @@ static int chain(token_t verb) if (verb != LOCK) { spk = CHAIN_UNLOCKED; if (game.prop[BEAR] == UNTAMED_BEAR) - spk = BEAR_BLOCKS; + spk = BEAR_BLOCKS; if (game.prop[CHAIN] == 0) - spk = ALREADY_UNLOCKED; + spk = ALREADY_UNLOCKED; if (spk != CHAIN_UNLOCKED) { rspeak(spk); return GO_CLEAROBJ; @@ -321,22 +318,22 @@ static int chain(token_t verb) game.prop[CHAIN] = 0; game.fixed[CHAIN] = 0; if (game.prop[BEAR] != BEAR_DEAD) - game.prop[BEAR] = CONTENTED_BEAR; - /* FIXME: Arithmetic on state numbers */ + game.prop[BEAR] = CONTENTED_BEAR; + /* FIXME: Arithmetic on state numbers */ game.fixed[BEAR] = 2 - game.prop[BEAR]; } else { spk = CHAIN_LOCKED; if (game.prop[CHAIN] != 0) - spk = ALREADY_LOCKED; + spk = ALREADY_LOCKED; if (game.loc != objects[CHAIN].plac) - spk = NO_LOCKSITE; + spk = NO_LOCKSITE; if (spk != CHAIN_LOCKED) { rspeak(spk); return GO_CLEAROBJ; } game.prop[CHAIN] = 2; if (TOTING(CHAIN)) - drop(CHAIN, game.loc); + drop(CHAIN, game.loc); game.fixed[CHAIN] = -1; } rspeak(spk); @@ -410,12 +407,12 @@ static int discard(token_t verb, token_t obj, bool just_do_it) if (obj == BOTTLE && k != 0) game.place[k] = LOC_NOWHERE; if (obj == CAGE && game.prop[BIRD] == BIRD_CAGED) - drop(BIRD, game.loc); + drop(BIRD, game.loc); drop(obj, game.loc); if (obj != BIRD) return GO_CLEAROBJ; game.prop[BIRD] = BIRD_UNCAGED; if (FOREST(game.loc)) - game.prop[BIRD] = BIRD_FOREST_UNCAGED; + game.prop[BIRD] = BIRD_FOREST_UNCAGED; return GO_CLEAROBJ; } @@ -472,7 +469,7 @@ static int extinguish(token_t verb, int obj) int spk = actions[verb].message; if (obj == INTRANSITIVE) { if (HERE(LAMP) && game.prop[LAMP] == LAMP_BRIGHT) - obj = LAMP; + obj = LAMP; if (HERE(URN) && game.prop[URN] == 2)obj = obj * NOBJECTS + URN; if (obj == INTRANSITIVE || obj == 0 || obj > NOBJECTS) return GO_UNKNOWN; } @@ -481,7 +478,7 @@ static int extinguish(token_t verb, int obj) game.prop[URN] = game.prop[URN] / 2; spk = URN_DARK; } else if (obj == LAMP) { - state_change(LAMP, LAMP_DARK); + state_change(LAMP, LAMP_DARK); spk = DARK(game.loc) ? PITCH_DARK : NO_MESSAGE; } else if (obj == DRAGON || obj == VOLCANO) spk = BEYOND_POWER; @@ -512,9 +509,9 @@ static int feed(token_t verb, token_t obj) } } else if (obj == BEAR) { if (game.prop[BEAR] == UNTAMED_BEAR) - spk = NOTHING_EDIBLE; + spk = NOTHING_EDIBLE; if (game.prop[BEAR] == BEAR_DEAD) - spk = RIDICULOUS_ATTEMPT; + spk = RIDICULOUS_ATTEMPT; if (HERE(FOOD)) { DESTROY(FOOD); game.prop[BEAR] = SITTING_BEAR; @@ -580,7 +577,7 @@ int fill(token_t verb, token_t obj) if (LIQUID() != 0) spk = BOTTLE_FULL; if (spk == BOTTLED_WATER) { - /* FIXME: Arithmetic on property values */ + /* FIXME: Arithmetic on property values */ game.prop[BOTTLE] = MOD(conditions[game.loc], 4) / 2 * 2; k = LIQUID(); if (TOTING(BOTTLE)) @@ -666,7 +663,7 @@ static int light(token_t verb, token_t obj) int spk = actions[verb].message; if (obj == INTRANSITIVE) { if (HERE(LAMP) && game.prop[LAMP] == LAMP_DARK && game.limit >= 0) - obj = LAMP; + obj = LAMP; if (HERE(URN) && game.prop[URN] == 1)obj = obj * NOBJECTS + URN; if (obj == INTRANSITIVE || obj == 0 || obj > NOBJECTS) return GO_UNKNOWN; } @@ -689,7 +686,7 @@ static int light(token_t verb, token_t obj) rspeak(spk); return GO_CLEAROBJ; } - state_change(LAMP, LAMP_BRIGHT); + state_change(LAMP, LAMP_BRIGHT); if (game.wzdark) return GO_TOP; else @@ -706,21 +703,21 @@ static int listen(void) if (k != SILENT) { rspeak(k); if (locations[game.loc].loud) - return GO_CLEAROBJ; - else - spk = NO_MESSAGE; + return GO_CLEAROBJ; + else + spk = NO_MESSAGE; } for (int i = 1; i <= NOBJECTS; i++) { if (!HERE(i) || objects[i].sounds[0] == NULL || game.prop[i] < 0) continue; - int mi = game.prop[i]; - if (i == BIRD) - mi += 3 * game.blooded; - long packed_zzword = token_to_packed(game.zzword); + int mi = game.prop[i]; + if (i == BIRD) + mi += 3 * game.blooded; + long packed_zzword = token_to_packed(game.zzword); pspeak(i, hear, mi, packed_zzword); spk = NO_MESSAGE; - /* FIXME: Magic number, sensitive to bird state logic */ - if (i == BIRD && game.prop[i] == 5) + /* FIXME: Magic number, sensitive to bird state logic */ + if (i == BIRD && game.prop[i] == 5) DESTROY(BIRD); } rspeak(spk); @@ -763,8 +760,8 @@ static int lock(token_t verb, token_t obj) if (!game.panic)game.clock2 = PANICTIME; game.panic = true; } else { - state_change(GRATE, (verb == LOCK) ? GRATE_CLOSED : GRATE_OPEN); - return GO_CLEAROBJ; + state_change(GRATE, (verb == LOCK) ? GRATE_CLOSED : GRATE_OPEN); + return GO_CLEAROBJ; } } } @@ -1005,7 +1002,7 @@ static int wave(token_t verb, token_t obj) } /* FIXME: Arithemetic on proprty values */ if (HERE(BIRD)) - spk = FREE_FLY + MOD(game.prop[BIRD], 2); + spk = FREE_FLY + MOD(game.prop[BIRD], 2); if (spk == FREE_FLY && game.loc == game.place[STEPS] && game.prop[JADE] < 0) { drop(JADE, game.loc); game.prop[JADE] = 0; @@ -1086,7 +1083,7 @@ int action(struct command_t *command) switch (command->part) { case intransitive: if (command->wd2 > 0 && command->verb != SAY) - return GO_WORD2; + return GO_WORD2; if (command->verb == SAY)command->obj = command->wd2; if (command->obj == 0 || command->obj == INTRANSITIVE) { /* Analyse an intransitive verb (ie, no object given yet). */ @@ -1150,7 +1147,7 @@ int action(struct command_t *command) case BRIEF: return brief(); case READ: - command->obj = INTRANSITIVE; + command->obj = INTRANSITIVE; return read(*command); case BREAK: return GO_UNKNOWN; @@ -1166,8 +1163,8 @@ int action(struct command_t *command) return listen(); case PART: return reservoir(); - default: - BUG(INTRANSITIVE_ACTION_VERB_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE + default: + BUG(INTRANSITIVE_ACTION_VERB_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE } } /* FALLTHRU */ @@ -1198,7 +1195,7 @@ int action(struct command_t *command) rspeak(spk); return GO_CLEAROBJ; } - case GO: { + case GO: { rspeak(spk); return GO_CLEAROBJ; } @@ -1213,7 +1210,7 @@ int action(struct command_t *command) case RUB: return rub(command->verb, command->obj); case THROW: - return throw(command); + return throw (command); case QUIT: { rspeak(spk); return GO_CLEAROBJ; @@ -1261,11 +1258,11 @@ int action(struct command_t *command) rspeak(spk); return GO_CLEAROBJ; } - case PART: + case PART: return reservoir(); - default: - BUG(TRANSITIVE_ACTION_VERB_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE - } + default: + BUG(TRANSITIVE_ACTION_VERB_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE + } case unknown: /* Unknown verb, couldn't deduce object - might need hint */ rspeak(WHAT_DO, command->wd1, command->wd1x); diff --git a/cheat.c b/cheat.c index d9da405..4643462 100644 --- a/cheat.c +++ b/cheat.c @@ -58,9 +58,8 @@ int main(int argc, char *argv[]) break; } } - - if (savefilename == NULL) - { + + if (savefilename == NULL) { fprintf(stderr, usage, argv[0]); fprintf(stderr, @@ -84,21 +83,20 @@ int main(int argc, char *argv[]) game.newloc = LOC_START; game.loc = LOC_START; game.limit = GAMELIMIT; - + // apply cheats game.numdie = numdie; game.saved = saved; - + fp = fopen(savefilename, WRITE_MODE); - if (fp == NULL) - { + if (fp == NULL) { fprintf(stderr, "Can't open file %s. Exiting.\n", savefilename); exit(-1); - } + } savefile(fp, version); - + printf("cheat: %s created.\n", savefilename); return 0; } diff --git a/init.c b/init.c index d22da5e..f434844 100644 --- a/init.c +++ b/init.c @@ -25,7 +25,7 @@ void initialise(void) if (!(locations[i].description.big == 0 || tkey[i] == 0)) { int k = tkey[i]; if (T_TERMINATE(travel[k])) - conditions[i] |= (1 << COND_FORCED); + conditions[i] |= (1 << COND_FORCED); } game.atloc[i] = 0; } @@ -56,11 +56,11 @@ void initialise(void) * not yet found, so we know when to close the cave. */ game.tally = 0; for (int treasure = 1; treasure <= NOBJECTS; treasure++) { - if (objects[treasure].is_treasure) { - if (objects[treasure].inventory != 0) - game.prop[treasure] = -1; - game.tally = game.tally - game.prop[treasure]; - } + if (objects[treasure].is_treasure) { + if (objects[treasure].inventory != 0) + game.prop[treasure] = -1; + game.tally = game.tally - game.prop[treasure]; + } } /* Clear the hint stuff. game.hintlc[i] is how long he's been at LOC diff --git a/main.c b/main.c index 7c6ba5f..5f803f6 100644 --- a/main.c +++ b/main.c @@ -170,7 +170,7 @@ static bool fallback_handler(char *buf) // autogenerated, so don't charge user time for it. --game.turns; // here we reconfigure any global game state that uses random numbers - make_zzword(game.zzword); + make_zzword(game.zzword); return true; } return false; @@ -283,8 +283,8 @@ static bool spotted_by_pirate(int i) int snarfed = 0; bool movechest = false, robplayer = false; for (int treasure = 1; treasure <= NOBJECTS; treasure++) { - if (!objects[treasure].is_treasure) - continue; + if (!objects[treasure].is_treasure) + continue; /* Pirate won't take pyramid from plover room or dark * room (too easy!). */ if (treasure == PYRAMID && (game.loc == objects[PYRAMID].plac || game.loc == objects[EMERALD].plac)) { @@ -318,9 +318,9 @@ static bool spotted_by_pirate(int i) } if (robplayer) { rspeak(PIRATE_POUNCES); - for (int treasure = 1; treasure <= NOBJECTS; treasure++) { - if (!objects[treasure].is_treasure) - continue; + for (int treasure = 1; treasure <= NOBJECTS; treasure++) { + if (!objects[treasure].is_treasure) + continue; if (!(treasure == PYRAMID && (game.loc == objects[PYRAMID].plac || game.loc == objects[EMERALD].plac))) { if (AT(treasure) && game.fixed[treasure] == 0) carry(treasure, game.loc); @@ -401,7 +401,7 @@ static bool dwarfmove(void) kk = tkey[game.dloc[i]]; if (kk != 0) do { - game.newloc = T_DESTINATION(travel[kk]); + game.newloc = T_DESTINATION(travel[kk]); /* Have we avoided a dwarf encounter? */ bool avoided = (SPECIAL(game.newloc) || !INDEEP(game.newloc) || @@ -417,7 +417,7 @@ static bool dwarfmove(void) } ++kk; } while - (!travel[kk - 1].stop); + (!travel[kk - 1].stop); tk[j] = game.odloc[i]; if (j >= 2) --j; @@ -551,8 +551,8 @@ static bool playermove(token_t verb, int motion) ++kk; /* go to next travel entry for this location */ continue; } - /* we've reached the end of travel entries for game.loc */ - kk = k2; + /* we've reached the end of travel entries for game.loc */ + kk = k2; if (kk == 0) { rspeak(NOT_CONNECTED); return true; @@ -619,43 +619,43 @@ static bool playermove(token_t verb, int motion) long cond = T_CONDITION(travel[kk]); long arg = MOD(cond, 100); if (!SPECIAL(cond)) { - /* YAML N and [pct N] conditionals */ + /* YAML N and [pct N] conditionals */ if (cond <= 100) { if (cond == 0 || PCT(cond)) break; /* else fall through */ } - /* YAML [with OBJ] clause */ + /* YAML [with OBJ] clause */ if (TOTING(arg) || (cond > 200 && AT(arg))) break; /* else fall through to check [not OBJ STATE] */ } else if (game.prop[arg] != cond / 100 - 3) break; - /* We arrive here on conditional failure. - * Skip to next non-matching destination */ - long k2 = kk; - do { - if (travel[k2].stop) + /* We arrive here on conditional failure. + * Skip to next non-matching destination */ + long k3 = kk; + do { + if (travel[k3].stop) BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE - ++k2; + ++k3; } while - (T_HIGH(travel[kk]) == T_HIGH(travel[k2])); - kk = k2; + (T_HIGH(travel[kk]) == T_HIGH(travel[k3])); + kk = k3; } - /* Found an eligible rule, now execute it */ + /* Found an eligible rule, now execute it */ game.newloc = T_DESTINATION(travel[kk]); if (!SPECIAL(game.newloc)) return true; - if (game.newloc > 500) { - /* Execute a speak rule */ - rspeak(L_SPEAK(game.newloc)); - game.newloc = game.loc; - return true; - } else { - game.newloc -= SPECIALBASE; + if (game.newloc > 500) { + /* Execute a speak rule */ + rspeak(L_SPEAK(game.newloc)); + game.newloc = game.loc; + return true; + } else { + game.newloc -= SPECIALBASE; switch (game.newloc) { case 1: /* Travel 301. Plover-alcove passage. Can carry only @@ -676,13 +676,13 @@ static bool playermove(token_t verb, int motion) * to get it out. Having dropped it, go back and * pretend he wasn't carrying it after all. */ drop(EMERALD, game.loc); - k2 = kk; + k2 = kk; do { if (travel[k2].stop) BUG(CONDITIONAL_TRAVEL_ENTRY_WITH_NO_ALTERATION); // LCOV_EXCL_LINE ++k2; } while - (T_HIGH(travel[kk]) == T_HIGH(travel[k2])); + (T_HIGH(travel[kk]) == T_HIGH(travel[k2])); kk = k2; continue; /* goto L12 */ case 3: @@ -696,7 +696,7 @@ static bool playermove(token_t verb, int motion) * entries check for game.prop(TROLL)=0.) Special * stuff for bear. */ if (game.prop[TROLL] == 1) { - pspeak(TROLL,look, 1); + pspeak(TROLL, look, 1); game.prop[TROLL] = 0; move(TROLL2, 0); move(TROLL2 + NOBJECTS, 0); @@ -719,8 +719,8 @@ static bool playermove(token_t verb, int motion) croak(); return true; } - default: - BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE + default: + BUG(SPECIAL_TRAVEL_500_GT_L_GT_300_EXCEEDS_GOTO_LIST); // LCOV_EXCL_LINE } } break; /* Leave L12 loop */ @@ -778,7 +778,7 @@ static bool closecheck(void) move(TROLL2 + NOBJECTS, objects[TROLL].fixd); juggle(CHASM); if (game.prop[BEAR] != BEAR_DEAD) - DESTROY(BEAR); + DESTROY(BEAR); game.prop[CHAIN] = 0; game.fixed[CHAIN] = 0; game.prop[AXE] = 0; @@ -871,7 +871,7 @@ static void lampcheck(void) int spk = GET_BATTERIES; if (game.place[BATTERY] == LOC_NOWHERE)spk = LAMP_DIM; if (game.prop[BATTERY] == DEAD_BATTERIES) - spk = MISSING_BATTERIES; + spk = MISSING_BATTERIES; rspeak(spk); } } @@ -1016,22 +1016,21 @@ L2600: game.knfloc = 0; /* This is where we get a new command from the user */ - char* input; - for (;;) { - input = get_input(); - if (input == NULL) - return(false); - if (word_count(input) > 2) - { - rspeak(TWO_WORDS); - continue; - } - if (strcmp(input, "") != 0) - break; - } - long tokens[4]; - tokenize(input, tokens); - command.wd1 = tokens[0]; + char* input; + for (;;) { + input = get_input(); + if (input == NULL) + return (false); + if (word_count(input) > 2) { + rspeak(TWO_WORDS); + continue; + } + if (strcmp(input, "") != 0) + break; + } + long tokens[4]; + tokenize(input, tokens); + command.wd1 = tokens[0]; command.wd1x = tokens[1]; command.wd2 = tokens[2]; command.wd2x = tokens[3]; @@ -1043,16 +1042,14 @@ L2607: game.foobar = (game.foobar > 0 ? -game.foobar : 0); ++game.turns; - /* If a turn threshold has been met, apply penalties and tell - * the player about it. */ - for (int i = 0; i < NTHRESHOLDS; ++i) - { - if (game.turns == turn_thresholds[i].threshold + 1) - { - game.trnluz += turn_thresholds[i].point_loss; - speak(turn_thresholds[i].message); - } - } + /* If a turn threshold has been met, apply penalties and tell + * the player about it. */ + for (int i = 0; i < NTHRESHOLDS; ++i) { + if (game.turns == turn_thresholds[i].threshold + 1) { + game.trnluz += turn_thresholds[i].point_loss; + speak(turn_thresholds[i].message); + } + } if (command.verb == SAY && command.wd2 > 0) command.verb = 0; @@ -1066,12 +1063,12 @@ L2607: } else lampcheck(); - char word1[6]; - char word2[6]; - packed_to_token(command.wd1, word1); - packed_to_token(command.wd2, word2); - V1 = get_vocab_id(word1); - V2 = get_vocab_id(word2); + char word1[6]; + char word2[6]; + packed_to_token(command.wd1, word1); + packed_to_token(command.wd2, word2); + V1 = get_vocab_id(word1); + V2 = get_vocab_id(word2); if (V1 == ENTER && (V2 == STREAM || V2 == 1000 + WATER)) { if (LIQLOC(game.loc) == WATER) { rspeak(FEET_WET); @@ -1089,10 +1086,10 @@ L2607: if (!((V1 != 1000 + WATER && V1 != 1000 + OIL) || (V2 != 1000 + PLANT && V2 != 1000 + DOOR))) { if (AT(V2 - 1000)) - command.wd2 = token_to_packed("POUR"); + command.wd2 = token_to_packed("POUR"); } if (V1 == 1000 + CAGE && V2 == 1000 + BIRD && HERE(CAGE) && HERE(BIRD)) - command.wd1 = token_to_packed("CATCH"); + command.wd1 = token_to_packed("CATCH"); } L2620: if (wordeq(command.wd1, token_to_packed("WEST"))) { @@ -1105,8 +1102,8 @@ L2620: rspeak(GO_UNNEEDED); } Lookup: - packed_to_token(command.wd1, word1); - defn = get_vocab_id(word1); + packed_to_token(command.wd1, word1); + defn = get_vocab_id(word1); if (defn == -1) { /* Gee, I don't understand. */ if (fallback_handler(input)) @@ -1172,7 +1169,7 @@ Laction: default: BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE } - linenoiseFree(input); + linenoiseFree(input); } } diff --git a/misc.c b/misc.c index ae43668..4f32246 100644 --- a/misc.c +++ b/misc.c @@ -11,47 +11,47 @@ #include "dungeon.h" const char new_advent_to_ascii[] = { - ' ', '!', '"', '#', '$', '%', '&', '\'', - '(', ')', '*', '+', ',', '-', '.', '/', - '0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', ':', ';', '<', '=', '>', '?', - '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', - 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', - 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', - 'X', 'Y', 'Z', '\0', '\0', '\0', '\0', '\0', + ' ', '!', '"', '#', '$', '%', '&', '\'', + '(', ')', '*', '+', ',', '-', '.', '/', + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', ':', ';', '<', '=', '>', '?', + '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', + 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', + 'X', 'Y', 'Z', '\0', '\0', '\0', '\0', '\0', }; const char new_ascii_to_advent[] = { - 63, 63, 63, 63, 63, 63, 63, 63, - 63, 63, 63, 63, 63, 63, 63, 63, - 63, 63, 63, 63, 63, 63, 63, 63, - 63, 63, 63, 63, 63, 63, 63, 63, - - 0, 1, 2, 3, 4, 5, 6, 7, - 8, 9, 10, 11, 12, 13, 14, 15, - 16, 17, 18, 19, 20, 21, 22, 23, - 24, 25, 26, 27, 28, 29, 30, 31, - 32, 33, 34, 35, 36, 37, 38, 39, - 40, 41, 42, 43, 44, 45, 46, 47, - 48, 49, 50, 51, 52, 53, 54, 55, - 56, 57, 58, 59, 60, 61, 62, 63, - - 63, 63, 63, 63, 63, 63, 63, 63, - 63, 63, 63, 63, 63, 63, 63, 63, - 63, 63, 63, 63, 63, 63, 63, 63, - 63, 63, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, + + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, + 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, + 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, + 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 58, 59, 60, 61, 62, 63, + + 63, 63, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, + 63, 63, 63, 63, 63, 63, 63, 63, }; char* xstrdup(const char* s) { - char* ptr = strdup(s); - if (ptr == NULL) { - // LCOV_EXCL_START - // exclude from coverage analysis because we can't simulate an out of memory error in testing - fprintf(stderr, "Out of memory!\n"); - exit(EXIT_FAILURE); - } - return(ptr); + char* ptr = strdup(s); + if (ptr == NULL) { + // LCOV_EXCL_START + // exclude from coverage analysis because we can't simulate an out of memory error in testing + fprintf(stderr, "Out of memory!\n"); + exit(EXIT_FAILURE); + } + return (ptr); } void* xmalloc(size_t size) @@ -62,7 +62,7 @@ void* xmalloc(size_t size) // exclude from coverage analysis because we can't simulate an out of memory error in testing fprintf(stderr, "Out of memory!\n"); exit(EXIT_FAILURE); - // LCOV_EXCL_STOP + // LCOV_EXCL_STOP } return (ptr); } @@ -71,7 +71,7 @@ void packed_to_token(long packed, char token[6]) { // Unpack and map back to ASCII. for (int i = 0; i < 5; ++i) { - char advent = (packed >> i * 6) & 63; + char advent = (packed >> i * 6) & 63; token[i] = new_advent_to_ascii[(int) advent]; } @@ -89,51 +89,50 @@ void packed_to_token(long packed, char token[6]) long token_to_packed(const char token[6]) { - size_t t_len = strlen(token); - long packed = 0; - for (size_t i = 0; i < t_len; ++i) - { - char mapped = new_ascii_to_advent[(int) token[i]]; - packed |= (mapped << (6 * i)); + size_t t_len = strlen(token); + long packed = 0; + for (size_t i = 0; i < t_len; ++i) { + char mapped = new_ascii_to_advent[(int) token[i]]; + packed |= (mapped << (6 * i)); } - return(packed); + return (packed); } void tokenize(char* raw, long tokens[4]) { - // set each token to 0 - for (int i = 0; i < 4; ++i) - tokens[i] = 0; - - // grab the first two words - char* words[2]; - words[0] = (char*) xmalloc(strlen(raw)); - words[1] = (char*) xmalloc(strlen(raw)); - int word_count = sscanf(raw, "%s%s", words[0], words[1]); - - // make space for substrings and zero it out - char chunk_data[][6] = { - {"\0\0\0\0\0"}, - {"\0\0\0\0\0"}, - {"\0\0\0\0\0"}, - {"\0\0\0\0\0"}, - }; - - // break the words into up to 4 5-char substrings - sscanf(words[0], "%5s%5s", chunk_data[0], chunk_data[1]); - if (word_count == 2) - sscanf(words[1], "%5s%5s", chunk_data[2], chunk_data[3]); - free(words[0]); - free(words[1]); - - // uppercase all the substrings - for (int i = 0; i < 4; ++i) - for (unsigned int j = 0; j < strlen(chunk_data[i]); ++j) - chunk_data[i][j] = (char) toupper(chunk_data[i][j]); - - // pack the substrings - for (int i = 0; i < 4; ++i) - tokens[i] = token_to_packed(chunk_data[i]); + // set each token to 0 + for (int i = 0; i < 4; ++i) + tokens[i] = 0; + + // grab the first two words + char* words[2]; + words[0] = (char*) xmalloc(strlen(raw)); + words[1] = (char*) xmalloc(strlen(raw)); + int word_count = sscanf(raw, "%s%s", words[0], words[1]); + + // make space for substrings and zero it out + char chunk_data[][6] = { + {"\0\0\0\0\0"}, + {"\0\0\0\0\0"}, + {"\0\0\0\0\0"}, + {"\0\0\0\0\0"}, + }; + + // break the words into up to 4 5-char substrings + sscanf(words[0], "%5s%5s", chunk_data[0], chunk_data[1]); + if (word_count == 2) + sscanf(words[1], "%5s%5s", chunk_data[2], chunk_data[3]); + free(words[0]); + free(words[1]); + + // uppercase all the substrings + for (int i = 0; i < 4; ++i) + for (unsigned int j = 0; j < strlen(chunk_data[i]); ++j) + chunk_data[i][j] = (char) toupper(chunk_data[i][j]); + + // pack the substrings + for (int i = 0; i < 4; ++i) + tokens[i] = token_to_packed(chunk_data[i]); } /* Hide the fact that wods are corrently packed longs */ @@ -184,8 +183,8 @@ void vspeak(const char* msg, va_list ap) size--; } else { long arg = va_arg(ap, long); - if (arg == -1) - arg = 0; + if (arg == -1) + arg = 0; i++; // Integer specifier. In order to accommodate the fact that PARMS can have both legitimate integers *and* packed tokens, stringify everything. Future work may eliminate the need for this. if (msg[i] == 'd') { @@ -256,19 +255,19 @@ void pspeak(vocab_t msg, enum speaktype mode, int skip, ...) switch (mode) { case touch: vspeak(objects[msg].inventory, ap); - break; - case look: + break; + case look: vspeak(objects[msg].descriptions[skip], ap); - break; + break; case hear: vspeak(objects[msg].sounds[skip], ap); - break; + break; case study: vspeak(objects[msg].texts[skip], ap); - break; + break; case change: vspeak(objects[msg].changes[skip], ap); - break; + break; } va_end(ap); } @@ -294,19 +293,18 @@ void echo_input(FILE* destination, char* input_prompt, char* input) int word_count(char* s) { - char* copy = xstrdup(s); - char delims[] = " \t"; - int count = 0; - char* word; + char* copy = xstrdup(s); + char delims[] = " \t"; + int count = 0; + char* word; - word = strtok(copy, delims); - while(word != NULL) - { - word = strtok(NULL, delims); - ++count; + word = strtok(copy, delims); + while (word != NULL) { + word = strtok(NULL, delims); + ++count; } - free(copy); - return(count); + free(copy); + return (count); } char* get_input() @@ -328,17 +326,17 @@ char* get_input() input = NULL; size_t n = 0; if (isatty(0)) - // LCOV_EXCL_START - // Should be unreachable in tests, as they will use a non-interactive shell. + // LCOV_EXCL_START + // Should be unreachable in tests, as they will use a non-interactive shell. printf("%s", input_prompt); - // LCOV_EXCL_STOP + // LCOV_EXCL_STOP ssize_t numread = getline(&input, &n, stdin); - if (numread == -1) // Got EOF; return with it. - return(NULL); + if (numread == -1) // Got EOF; return with it. + return (NULL); } if (input == NULL) // Got EOF; return with it. - return(input); + return (input); else if (input[0] == '#') // Ignore comments. continue; else // We have a 'normal' line; leave the loop. @@ -361,43 +359,43 @@ char* get_input() bool silent_yes() { - char* reply; - bool outcome; - - for (;;) { - reply = get_input(); - if (reply == NULL) { - // LCOV_EXCL_START - // Should be unreachable. Reply should never be NULL - linenoiseFree(reply); - exit(EXIT_SUCCESS); - // LCOV_EXCL_STOP - } + char* reply; + bool outcome; - char* firstword = (char*) xmalloc(strlen(reply)+1); - sscanf(reply, "%s", firstword); + for (;;) { + reply = get_input(); + if (reply == NULL) { + // LCOV_EXCL_START + // Should be unreachable. Reply should never be NULL + linenoiseFree(reply); + exit(EXIT_SUCCESS); + // LCOV_EXCL_STOP + } - for (int i = 0; i < (int)strlen(firstword); ++i) - firstword[i] = tolower(firstword[i]); + char* firstword = (char*) xmalloc(strlen(reply) + 1); + sscanf(reply, "%s", firstword); + + for (int i = 0; i < (int)strlen(firstword); ++i) + firstword[i] = tolower(firstword[i]); - int yes = strncmp("yes", firstword, sizeof("yes") - 1); - int y = strncmp("y", firstword, sizeof("y") - 1); - int no = strncmp("no", firstword, sizeof("no") - 1); - int n = strncmp("n", firstword, sizeof("n") - 1); + int yes = strncmp("yes", firstword, sizeof("yes") - 1); + int y = strncmp("y", firstword, sizeof("y") - 1); + int no = strncmp("no", firstword, sizeof("no") - 1); + int n = strncmp("n", firstword, sizeof("n") - 1); - free(firstword); + free(firstword); - if (yes == 0 || y == 0) { - outcome = true; - break; - } else if (no == 0 || n == 0) { - outcome = false; - break; - } else - rspeak(PLEASE_ANSWER); - } - linenoiseFree(reply); - return (outcome); + if (yes == 0 || y == 0) { + outcome = true; + break; + } else if (no == 0 || n == 0) { + outcome = false; + break; + } else + rspeak(PLEASE_ANSWER); + } + linenoiseFree(reply); + return (outcome); } @@ -417,10 +415,10 @@ bool yes(const char* question, const char* yes_response, const char* no_response // Should be unreachable. Reply should never be NULL linenoiseFree(reply); exit(EXIT_SUCCESS); - // LCOV_EXCL_STOP + // LCOV_EXCL_STOP } - char* firstword = (char*) xmalloc(strlen(reply)+1); + char* firstword = (char*) xmalloc(strlen(reply) + 1); sscanf(reply, "%s", firstword); for (int i = 0; i < (int)strlen(firstword); ++i) @@ -434,7 +432,7 @@ bool yes(const char* question, const char* yes_response, const char* no_response free(firstword); if (yes == 0 || y == 0) { - speak(yes_response); + speak(yes_response); outcome = true; break; } else if (no == 0 || n == 0) { @@ -453,89 +451,81 @@ bool yes(const char* question, const char* yes_response, const char* no_response int get_motion_vocab_id(const char* word) // Return the first motion number that has 'word' as one of its words. { - for (int i = 0; i < NMOTIONS; ++i) - { - for (int j = 0; j < motions[i].words.n; ++j) - { - if (strcasecmp(word, motions[i].words.strs[j]) == 0) - return(i); - } + for (int i = 0; i < NMOTIONS; ++i) { + for (int j = 0; j < motions[i].words.n; ++j) { + if (strcasecmp(word, motions[i].words.strs[j]) == 0) + return (i); + } } - // If execution reaches here, we didn't find the word. - return(WORD_NOT_FOUND); + // If execution reaches here, we didn't find the word. + return (WORD_NOT_FOUND); } int get_object_vocab_id(const char* word) // Return the first object number that has 'word' as one of its words. { - for (int i = 0; i < NOBJECTS + 1; ++i) // FIXME: the + 1 should go when 1-indexing for objects is removed - { - for (int j = 0; j < objects[i].words.n; ++j) - { - if (strcasecmp(word, objects[i].words.strs[j]) == 0) - return(i); - } + for (int i = 0; i < NOBJECTS + 1; ++i) { // FIXME: the + 1 should go when 1-indexing for objects is removed + for (int j = 0; j < objects[i].words.n; ++j) { + if (strcasecmp(word, objects[i].words.strs[j]) == 0) + return (i); + } } - // If execution reaches here, we didn't find the word. - return(WORD_NOT_FOUND); + // If execution reaches here, we didn't find the word. + return (WORD_NOT_FOUND); } int get_action_vocab_id(const char* word) // Return the first motion number that has 'word' as one of its words. { - for (int i = 0; i < NACTIONS; ++i) - { - for (int j = 0; j < actions[i].words.n; ++j) - { - if (strcasecmp(word, actions[i].words.strs[j]) == 0) - return(i); - } + for (int i = 0; i < NACTIONS; ++i) { + for (int j = 0; j < actions[i].words.n; ++j) { + if (strcasecmp(word, actions[i].words.strs[j]) == 0) + return (i); + } } - // If execution reaches here, we didn't find the word. - return(WORD_NOT_FOUND); + // If execution reaches here, we didn't find the word. + return (WORD_NOT_FOUND); } int get_special_vocab_id(const char* word) // Return the first special number that has 'word' as one of its words. { - for (int i = 0; i < NSPECIALS; ++i) - { - for (int j = 0; j < specials[i].words.n; ++j) - { - if (strcasecmp(word, specials[i].words.strs[j]) == 0) - return(i); - } + for (int i = 0; i < NSPECIALS; ++i) { + for (int j = 0; j < specials[i].words.n; ++j) { + if (strcasecmp(word, specials[i].words.strs[j]) == 0) + return (i); + } } - // If execution reaches here, we didn't find the word. - return(WORD_NOT_FOUND); + // If execution reaches here, we didn't find the word. + return (WORD_NOT_FOUND); } long get_vocab_id(const char* word) // Search the vocab categories in order for the supplied word. { - long ref_num; - - ref_num = get_motion_vocab_id(word); - if (ref_num != WORD_NOT_FOUND) - return(ref_num + 0); // FIXME: replace with a proper hash + long ref_num; - ref_num = get_object_vocab_id(word); - if (ref_num != WORD_NOT_FOUND) - return(ref_num + 1000); // FIXME: replace with a proper hash + ref_num = get_motion_vocab_id(word); + if (ref_num != WORD_NOT_FOUND) + return (ref_num + 0); // FIXME: replace with a proper hash - ref_num = get_action_vocab_id(word); - if (ref_num != WORD_NOT_FOUND) - return(ref_num + 2000); // FIXME: replace with a proper hash + ref_num = get_object_vocab_id(word); + if (ref_num != WORD_NOT_FOUND) + return (ref_num + 1000); // FIXME: replace with a proper hash - ref_num = get_special_vocab_id(word); - if (ref_num != WORD_NOT_FOUND) - return(ref_num + 3000); // FIXME: replace with a proper hash + ref_num = get_action_vocab_id(word); + if (ref_num != WORD_NOT_FOUND) + return (ref_num + 2000); // FIXME: replace with a proper hash - // Check for the reservoir magic word. - if (strcasecmp(word, game.zzword) == 0) - return(PART + 2000); // FIXME: replace with a proper hash + ref_num = get_special_vocab_id(word); + if (ref_num != WORD_NOT_FOUND) + return (ref_num + 3000); // FIXME: replace with a proper hash - return(WORD_NOT_FOUND); + // Check for the reservoir magic word. + if (strcasecmp(word, game.zzword) == 0) + return (PART + 2000); // FIXME: replace with a proper hash + + return (WORD_NOT_FOUND); } void juggle(long object) @@ -673,12 +663,11 @@ long randrange(long range) void make_zzword(char zzword[6]) { - for (int i = 0; i < 5; ++i) - { - zzword[i] = 'A' + randrange(26); + for (int i = 0; i < 5; ++i) { + zzword[i] = 'A' + randrange(26); } - zzword[1] = '\''; // force second char to apostrophe - zzword[5] = '\0'; + zzword[1] = '\''; // force second char to apostrophe + zzword[5] = '\0'; } void datime(long* d, long* t) diff --git a/saveresume.c b/saveresume.c index f13e3f3..cf5a696 100644 --- a/saveresume.c +++ b/saveresume.c @@ -27,19 +27,19 @@ struct save_t { struct save_t save; int savefile(FILE *fp, long version) - /* Save game to file. No input or output from user. */ +/* Save game to file. No input or output from user. */ { long i, k; datime(&i, &k); k = i + 650 * k; save.savetime = k; save.mode = -1; - + save.version = (version == 0) ? VRSION : version; - + memcpy(&save.game, &game, sizeof(struct game_t)); IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp)); - return(0); + return (0); } /* Suspend and resume */ @@ -119,7 +119,7 @@ int restore(FILE* fp) } else { memcpy(&game, &save.game, sizeof(struct game_t)); //game.zzword = rndvoc(3, game.zzword); - make_zzword(game.zzword); + make_zzword(game.zzword); } return GO_TOP; } diff --git a/score.c b/score.c index d707fb8..1e4e5c2 100644 --- a/score.c +++ b/score.c @@ -37,8 +37,8 @@ long score(enum termination mode) * Give the poor guy 2 points just for finding each treasure. */ mxscor = 0; for (int i = 1; i <= NOBJECTS; i++) { - if (!objects[i].is_treasure) - continue; + if (!objects[i].is_treasure) + continue; if (objects[i].inventory != 0) { long k = 12; if (i == CHEST)k = 14; -- 2.31.1