From b247359d7f5422fe0c5c1f78d05da01c7d311f9e Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Tue, 2 Jan 2024 10:32:23 -0500 Subject: [PATCH] Initialize dwarf locations in YAML, not C. Minimizes magic numbers. --- adventure.yaml | 12 ++++++++++++ init.c | 15 +++++++-------- make_dungeon.py | 2 ++ templates/dungeon.c.tpl | 3 +++ templates/dungeon.h.tpl | 4 ++++ 5 files changed, 28 insertions(+), 8 deletions(-) diff --git a/adventure.yaml b/adventure.yaml index 3c0e566..2069759 100644 --- a/adventure.yaml +++ b/adventure.yaml @@ -2835,6 +2835,18 @@ locations: !!omap {verbs: [], action: [goto, LOC_Y2]}, ] +# Starting locations of dwarves. +# Sixth dwarf is special (the pirate). He always starts at his +# chest's eventual location inside the maze. +dwarflocs: [ + LOC_KINGHALL, + LOC_WESTBANK, + LOC_Y2, + LOC_ALIKE3, + LOC_COMPLEX, + LOC_MAZEEND12, +] + arbitrary_messages: !!omap - NO_MESSAGE: !!null - CAVE_NEARBY: |- diff --git a/init.c b/init.c index 9104a59..aabd1cb 100644 --- a/init.c +++ b/init.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "advent.h" @@ -21,17 +22,10 @@ struct settings_t settings = { }; struct game_t game = { - .dwarves[1].loc = LOC_KINGHALL, - .dwarves[2].loc = LOC_WESTBANK, - .dwarves[3].loc = LOC_Y2, - .dwarves[4].loc = LOC_ALIKE3, - .dwarves[5].loc = LOC_COMPLEX, - - /* Sixth dwarf is special (the pirate). He always starts at his + /* Last dwarf is special (the pirate). He always starts at his * chest's eventual location inside the maze. This loc is saved * in chloc for ref. The dead end in the other maze has its * loc stored in chloc2. */ - .dwarves[6].loc = LOC_MAZEEND12, .chloc = LOC_MAZEEND12, .chloc2 = LOC_DEADEND13, .abbnum = 5, @@ -52,6 +46,11 @@ int initialise(void) int seedval = (int)rand(); set_seed(seedval); + assert(NDWARVES == NDWARFLOCS); + for (int i = 1; i <= NDWARFLOCS; i++) { + game.dwarves[i].loc = dwarflocs[i-1]; + } + for (int i = 1; i <= NOBJECTS; i++) { game.objects[i].place = LOC_NOWHERE; } diff --git a/make_dungeon.py b/make_dungeon.py index 8ab149e..0b8bc53 100755 --- a/make_dungeon.py +++ b/make_dungeon.py @@ -626,6 +626,7 @@ if __name__ == "__main__": tkeys=bigdump(tkey), travel=get_travel(travel), ignore=ignore, + dwarflocs=", ".join(db["dwarflocs"]) + ",", ) # 0-origin index of birds's last song. Bird should @@ -650,6 +651,7 @@ if __name__ == "__main__": motions=get_refs(db["motions"]), actions=get_refs(db["actions"]), state_definitions=statedefines, + ndwarflocs=str(len(db["dwarflocs"])), ) with open(H_NAME, "w", encoding="ascii", errors="surrogateescape") as hf: diff --git a/templates/dungeon.c.tpl b/templates/dungeon.c.tpl index 4a7011a..a192adb 100644 --- a/templates/dungeon.c.tpl +++ b/templates/dungeon.c.tpl @@ -53,4 +53,7 @@ const travelop_t travel[] = {{ const char *ignore = "{ignore}"; +/* Dwarf starting locations */ +const int dwarflocs[NDWARFLOCS] = {{{dwarflocs}}}; + /* end */ diff --git a/templates/dungeon.h.tpl b/templates/dungeon.h.tpl index 0a9c1c5..2a26917 100644 --- a/templates/dungeon.h.tpl +++ b/templates/dungeon.h.tpl @@ -35,6 +35,10 @@ SPDX-License-Identifier: BSD-2-Clause #define COND_HOGRE 20 /* Trying to deal with ogre */ #define COND_HJADE 21 /* Found all treasures except jade */ +/* Count of dwarf starting locations */ +#define NDWARFLOCS {ndwarflocs} +extern const int dwarflocs[NDWARFLOCS]; + typedef struct {{ const char** strs; const int n; -- 2.31.1