From ddb0df85b315cf60c529d0566254beb2c42cab2b Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Wed, 21 Jun 2017 09:41:01 -0400 Subject: [PATCH] YAMLify section 11 (hints). --- adventure.yaml | 60 +++++++++++++++++++++++++++++++++ dungeon.c | 19 ++--------- main.c | 10 +++--- newdungeon.py | 91 +++++++++++++++++++++++++++++++++++--------------- score.c | 2 +- 5 files changed, 132 insertions(+), 50 deletions(-) diff --git a/adventure.yaml b/adventure.yaml index ec9ce29..c5987d7 100644 --- a/adventure.yaml +++ b/adventure.yaml @@ -33,6 +33,13 @@ # If the inventory desription begins with "*" the object is dungeon # furniture that cannot be taken or carried. # +# hints: Each item contains a hint number (add 10 to get cond bit), +# the number of turns he must be at the right loc(s) before +# triggering the hint, the points deducted for taking the hint, the +# message number (section 6) of the question, and the message +# number of the hint. These values are stashed in the "hints" +# array. + locations: !!omap - LOC_NOWHERE: description: @@ -1547,3 +1554,56 @@ obituaries: yes_response: 'Okay, now where did I put my orange smoke?.... >POOF!<\nEverything disappears in a dense cloud of orange smoke.' - query: 'Now you''ve really done it! I''m out of orange smoke! You don''t expect\nme to do a decent reincarnation without any orange smoke, do you?' yes_response: 'Okay, if you''re so smart, do it yourself! I''m leaving!' + +# FIXME: Hint texts shouldn't be in arbitrary_messages, but inlined here +hints: +- number: 1 + turns: 4 + penalty: 2 + question: ENTRY_QUERY + hint: HARD_GRATE +- number: 2 + turns: 5 + penalty: 2 + question: BIRD_QUERY + hint: SKITTISH_BIRD +- number: 3 + turns: 8 + penalty: 2 + question: SNAKE_QUERY + hint: SNAKE_HINT +- number: 4 + turns: 75 + penalty: 4 + question: MAZE_HELP + hint: DROP_THINGS +- number: 5 + turns: 25 + penalty: 5 + question: PLOVER_QUERY + hint: EXPLORE_HINT +- number: 6 + turns: 20 + penalty: 3 + question: HELP_LEAVING + hint: NOGO_WEST +- number: 7 + turns: 8 + penalty: 2 + question: WONDERING_QUERY + hint: ADVANCED_SECTION +- number: 8 + turns: 25 + penalty: 2 + question: FOREST_QUERY + hint: GO_EAST +- number: 9 + turns: 10 + penalty: 4 + question: OGRE_QUERY + hint: OGRE_CLUE +- number: 10 + turns: 1 + penalty: 4 + question: MISSING_ONE + hint: NO_LOCATE diff --git a/dungeon.c b/dungeon.c index dccda9e..a39e413 100644 --- a/dungeon.c +++ b/dungeon.c @@ -51,8 +51,6 @@ long ATAB[TABSIZ + 1]; long PLAC[NOBJECTS + 1]; long FIXD[NOBJECTS + 1]; long ACTSPK[VRBSIZ + 1]; -long HINTS[HNTSIZ + 1][HINTLEN]; - static bool is_set(long var, long position) { @@ -325,7 +323,8 @@ static void read_hints(FILE* database) if (K <= 0 || K > HNTSIZ) BUG(TOO_MANY_HINTS); for (int I = 1; I <= 4; I++) { - HINTS[K][I] = GETNUM(NULL); + /* consume - actual arrqy-building now done in YAML. */ + GETNUM(NULL); } /* end loop */ HNTMAX = (HNTMAX > K ? HNTMAX : K); } @@ -468,19 +467,6 @@ static void write_1d(FILE* header_file, long array[], long dim, const char* varn fprintf(header_file, "\n});\n"); } -static void write_hints(FILE* header_file, long matrix[][HINTLEN], long dim1, long dim2, const char* varname) -{ - fprintf(header_file, "LOCATION long %s[][%ld] INITIALIZE(= {\n", varname, dim2); - for (int i = 0; i < dim1; ++i) { - fprintf(header_file, " {"); - for (int j = 0; j < dim2; ++j) { - fprintf(header_file, "%ld, ", matrix[i][j]); - } - fprintf(header_file, "},\n"); - } - fprintf(header_file, "});\n"); -} - static void write_file(FILE* header_file) { fprintf(header_file, "#ifndef DATABASE_H\n"); @@ -516,7 +502,6 @@ static void write_file(FILE* header_file) write_1d(header_file, PLAC, NOBJECTS + 1, "PLAC"); write_1d(header_file, FIXD, NOBJECTS + 1, "FIXD"); write_1d(header_file, ACTSPK, VRBSIZ + 1, "ACTSPK"); - write_hints(header_file, HINTS, HNTSIZ + 1, 5, "HINTS"); fprintf(header_file, "#undef LOCATION\n"); fprintf(header_file, "#undef INITIALIZE\n"); diff --git a/main.c b/main.c index 79572bb..00a4d89 100644 --- a/main.c +++ b/main.c @@ -197,7 +197,7 @@ static void checkhints(void) ++game.hintlc[hint]; /* Come here if he's been long enough at required loc(s) for some * unused hint. */ - if (game.hintlc[hint] >= HINTS[hint][1]) { + if (game.hintlc[hint] >= hints[hint-1].turns) { int i; switch (hint - 1) { @@ -263,13 +263,13 @@ static void checkhints(void) /* Fall through to hint display */ game.hintlc[hint] = 0; - if (!YES(arbitrary_messages[HINTS[hint][3]], arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN])) + if (!YES(hints[hint-1].question, arbitrary_messages[NO_MESSAGE], arbitrary_messages[OK_MAN])) return; - SETPRM(1, HINTS[hint][2], HINTS[hint][2]); + SETPRM(1, hints[hint-1].penalty, hints[hint-1].penalty); RSPEAK(HINT_COST); - game.hinted[hint] = YES(arbitrary_messages[WANT_HINT], arbitrary_messages[HINTS[hint][4]], arbitrary_messages[OK_MAN]); + game.hinted[hint] = YES(arbitrary_messages[WANT_HINT], hints[hint-1].hint, arbitrary_messages[OK_MAN]); if (game.hinted[hint] && game.limit > WARNTIME) - game.limit += WARNTIME * HINTS[hint][2]; + game.limit += WARNTIME * hints[hint-1].penalty; } } } diff --git a/newdungeon.py b/newdungeon.py index f8ef957..136d941 100755 --- a/newdungeon.py +++ b/newdungeon.py @@ -40,12 +40,21 @@ typedef struct {{ const char* message; }} class_t; +typedef struct {{ + const int number; + const int turns; + const int penalty; + const char* question; + const char* hint; +}} hint_t; + extern location_t locations[]; extern object_description_t object_descriptions[]; extern const char* arbitrary_messages[]; extern const class_t classes[]; extern turn_threshold_t turn_thresholds[]; extern obituary_t obituaries[]; +extern hint_t hints[]; extern size_t CLSSES; extern int maximum_deaths; @@ -90,6 +99,10 @@ obituary_t obituaries[] = {{ {} }}; +hint_t hints[] = {{ +{} +}}; + size_t CLSSES = {}; int maximum_deaths = {}; int turn_threshold_count = {}; @@ -205,30 +218,54 @@ def get_obituaries(obit): obit_str = obit_str[:-1] # trim trailing newline return obit_str -with open(yaml_name, "r") as f: - db = yaml.load(f) - -h = h_template.format( - get_refs(db["arbitrary_messages"]), - get_refs(db["locations"]), - get_refs(db["object_descriptions"]), -) - -c = c_template.format( - h_name, - get_arbitrary_messages(db["arbitrary_messages"]), - get_class_messages(db["classes"]), - get_turn_thresholds(db["turn_thresholds"]), - get_locations(db["locations"]), - get_object_descriptions(db["object_descriptions"]), - get_obituaries(db["obituaries"]), - len(db["classes"]), - len(db["obituaries"]), - len(db["turn_thresholds"]), -) - -with open(h_name, "w") as hf: - hf.write(h) - -with open(c_name, "w") as cf: - cf.write(c) +def get_hints(hnt, arb): + template = """ {{ + .number = {}, + .penalty = {}, + .turns = {}, + .question = {}, + .hint = {}, + }}, +""" + hnt_str = "" + md = dict(arb) + for item in hnt: + number = item["number"] + penalty = item["penalty"] + turns = item["turns"] + question = make_c_string(md[item["question"]]) + hint = make_c_string(md[item["hint"]]) + hnt_str += template.format(number, penalty, turns, question, hint) + hnt_str = hnt_str[:-1] # trim trailing newline + return hnt_str + + +if __name__ == "__main__": + with open(yaml_name, "r") as f: + db = yaml.load(f) + + h = h_template.format( + get_refs(db["arbitrary_messages"]), + get_refs(db["locations"]), + get_refs(db["object_descriptions"]), + ) + + c = c_template.format( + h_name, + get_arbitrary_messages(db["arbitrary_messages"]), + get_class_messages(db["classes"]), + get_turn_thresholds(db["turn_thresholds"]), + get_locations(db["locations"]), + get_object_descriptions(db["object_descriptions"]), + get_obituaries(db["obituaries"]), + get_hints(db["hints"], db["arbitrary_messages"]), + len(db["classes"]), + len(db["obituaries"]), + len(db["turn_thresholds"]), + ) + + with open(h_name, "w") as hf: + hf.write(h) + + with open(c_name, "w") as cf: + cf.write(c) diff --git a/score.c b/score.c index f3fc807..8f91e44 100644 --- a/score.c +++ b/score.c @@ -89,7 +89,7 @@ long score(enum termination mode) /* Deduct for hints/turns/saves. Hints < 4 are special; see database desc. */ for (long i = 1; i <= HNTMAX; i++) { if (game.hinted[i]) - score = score - HINTS[i][2]; + score = score - hints[i-1].penalty; } if (game.novice) score -= 5; -- 2.31.1