Initialize dwarf locations in YAML, not C.

Minimizes magic numbers.
This commit is contained in:
Eric S. Raymond 2024-01-02 10:32:23 -05:00
parent 1f9f39d789
commit b247359d7f
5 changed files with 28 additions and 8 deletions

View file

@ -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: |-

15
init.c
View file

@ -11,6 +11,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <assert.h>
#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;
}

View file

@ -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:

View file

@ -53,4 +53,7 @@ const travelop_t travel[] = {{
const char *ignore = "{ignore}";
/* Dwarf starting locations */
const int dwarflocs[NDWARFLOCS] = {{{dwarflocs}}};
/* end */

View file

@ -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;