Move per-puzzle ad-hoc getenv preferences into game_ui.

Environment variables that set specific settings of particular
puzzles, such as SLANT_SWAP_BUTTONS, LIGHTUP_LIT_BLOBS and
LOOPY_AUTOFOLLOW, now all affect the game behaviour via fields in
game_ui instead of being looked up by getenv in the individual
functions that need to know them.

The purpose of this refactoring is to put those config fields in a
place where other more user-friendly configuration systems will also
be able to access them, once I introduce one. However, for the moment,
there's no functional change: I haven't _yet_ changed how the user
sets those options. They're still set by environment variables alone.
All I'm changing here is where the settings are stored inside the
code, and exactly when they're read out of the environment to put into
the game_ui.

Specifically, the getenvs now happen during new_ui(). Or rather, in
all the puzzles I've changed here, they happen in a subroutine
legacy_prefs_override() called from within new_ui(), after it's set up
the default values for the settings, and then gives the environment a
chance to override them. Or rather, legacy_prefs_override() only
actually calls getenv the first time, and after that, it's cached the
answers it got.

In order to make the override functions less wordy, I've altered the
prototype of getenv_bool so that it returns an int rather than a bool,
and takes its default return value in the same form. That way you can
set the default to something other than 0 or 1, and find out whether a
value was present at all.

This commit only touches environment configuration specific to an
individual puzzle. The midend also has some standard environment-based
config options that apply to all puzzles, such as colour scheme and
default presets and preset-menu extension. I haven't handled those
yet.
This commit is contained in:
Simon Tatham
2023-04-21 15:41:18 +01:00
parent a4fca3286f
commit 0d1a1f08ba
11 changed files with 402 additions and 195 deletions

View File

@ -1833,13 +1833,37 @@ static char *game_text_format(const game_state *state)
struct game_ui {
int cur_x, cur_y;
bool cur_visible;
/*
* User preference: when a square contains both a black blob for
* 'user is convinced this isn't a light' and a yellow highlight
* for 'this square is lit by a light', both of which rule out it
* being a light, should we still bother to show the blob?
*/
bool draw_blobs_when_lit;
};
static void legacy_prefs_override(struct game_ui *ui_out)
{
static bool initialised = false;
static int draw_blobs_when_lit = -1;
if (!initialised) {
initialised = true;
draw_blobs_when_lit = getenv_bool("LIGHTUP_LIT_BLOBS", -1);
}
if (draw_blobs_when_lit != -1)
ui_out->draw_blobs_when_lit = draw_blobs_when_lit;
}
static game_ui *new_ui(const game_state *state)
{
game_ui *ui = snew(game_ui);
ui->cur_x = ui->cur_y = 0;
ui->cur_visible = getenv_bool("PUZZLES_SHOW_CURSOR", false);
ui->draw_blobs_when_lit = true;
legacy_prefs_override(ui);
return ui;
}
@ -2130,7 +2154,7 @@ static unsigned int tile_flags(game_drawstate *ds, const game_state *state,
return ret;
}
static void tile_redraw(drawing *dr, game_drawstate *ds,
static void tile_redraw(drawing *dr, game_drawstate *ds, const game_ui *ui,
const game_state *state, int x, int y)
{
unsigned int ds_flags = GRID(ds, flags, x, y);
@ -2160,10 +2184,7 @@ static void tile_redraw(drawing *dr, game_drawstate *ds,
draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, TILE_RADIUS,
lcol, COL_BLACK);
} else if ((ds_flags & DF_IMPOSSIBLE)) {
static int draw_blobs_when_lit = -1;
if (draw_blobs_when_lit < 0)
draw_blobs_when_lit = getenv_bool("LIGHTUP_LIT_BLOBS", true);
if (!(ds_flags & DF_LIT) || draw_blobs_when_lit) {
if (!(ds_flags & DF_LIT) || ui->draw_blobs_when_lit) {
int rlen = TILE_SIZE / 4;
draw_rect(dr, dx + TILE_SIZE/2 - rlen/2,
dy + TILE_SIZE/2 - rlen/2,
@ -2208,7 +2229,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds,
unsigned int ds_flags = tile_flags(ds, state, ui, x, y, flashing);
if (ds_flags != GRID(ds, flags, x, y)) {
GRID(ds, flags, x, y) = ds_flags;
tile_redraw(dr, ds, state, x, y);
tile_redraw(dr, ds, ui, state, x, y);
}
}
}