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

62
map.c
View File

@ -45,12 +45,6 @@ static bool verbose = false;
#define FIVE (FOUR+1)
#define SIX (FOUR+2)
/*
* Ghastly run-time configuration option, just for Gareth (again).
*/
static int flash_type = -1;
static float flash_length;
/*
* Difficulty levels. I do some macro ickery here to ensure that my
* enum and the various forms of my name list always match up.
@ -2292,8 +2286,37 @@ struct game_ui {
int cur_x, cur_y, cur_lastmove;
bool cur_visible, cur_moved;
/*
* User preference to enable alternative versions of the
* completion flash. Some users have found the colour-cycling
* default version to be a bit eye-twisting.
*/
enum {
FLASH_CYCLIC, /* cycle the four colours of the map */
FLASH_EACH_TO_WHITE, /* turn each colour white in turn */
FLASH_ALL_TO_WHITE /* flash the whole map to white in one go */
} flash_type;
};
static void legacy_prefs_override(struct game_ui *ui_out)
{
static bool initialised = false;
static int flash_type = -1;
if (!initialised) {
char *env;
initialised = true;
if ((env = getenv("MAP_ALTERNATIVE_FLASH")) != NULL)
flash_type = FLASH_EACH_TO_WHITE;
}
if (flash_type != -1)
ui_out->flash_type = flash_type;
}
static game_ui *new_ui(const game_state *state)
{
game_ui *ui = snew(game_ui);
@ -2305,6 +2328,8 @@ static game_ui *new_ui(const game_state *state)
ui->cur_visible = getenv_bool("PUZZLES_SHOW_CURSOR", false);
ui->cur_moved = false;
ui->cur_lastmove = 0;
ui->flash_type = FLASH_CYCLIC;
legacy_prefs_override(ui);
return ui;
}
@ -2882,6 +2907,11 @@ static void draw_square(drawing *dr, game_drawstate *ds,
draw_update(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
}
static float flash_length(const game_ui *ui)
{
return (ui->flash_type == FLASH_EACH_TO_WHITE ? 0.50F : 0.30F);
}
static void game_redraw(drawing *dr, game_drawstate *ds,
const game_state *oldstate, const game_state *state,
int dir, const game_ui *ui,
@ -2905,10 +2935,10 @@ static void game_redraw(drawing *dr, game_drawstate *ds,
}
if (flashtime) {
if (flash_type == 1)
flash = (int)(flashtime * FOUR / flash_length);
if (ui->flash_type == FLASH_EACH_TO_WHITE)
flash = (int)(flashtime * FOUR / flash_length(ui));
else
flash = 1 + (int)(flashtime * THREE / flash_length);
flash = 1 + (int)(flashtime * THREE / flash_length(ui));
} else
flash = -1;
@ -2927,12 +2957,12 @@ static void game_redraw(drawing *dr, game_drawstate *ds,
bv = FOUR;
if (flash >= 0) {
if (flash_type == 1) {
if (ui->flash_type == FLASH_EACH_TO_WHITE) {
if (tv == flash)
tv = FOUR;
if (bv == flash)
bv = FOUR;
} else if (flash_type == 2) {
} else if (ui->flash_type == FLASH_ALL_TO_WHITE) {
if (flash % 2)
tv = bv = FOUR;
} else {
@ -3062,15 +3092,7 @@ static float game_flash_length(const game_state *oldstate,
{
if (!oldstate->completed && newstate->completed &&
!oldstate->cheated && !newstate->cheated) {
if (flash_type < 0) {
char *env = getenv("MAP_ALTERNATIVE_FLASH");
if (env)
flash_type = atoi(env);
else
flash_type = 0;
flash_length = (flash_type == 1 ? 0.50F : 0.30F);
}
return flash_length;
return flash_length(ui);
} else
return 0.0F;
}