* midend.c: general middle fragment sitting between the
* platform-specific front end and game-specific back end.
* Maintains a move list, takes care of Undo and Redo commands, and
- * processes standard keystrokes for undo/redo/new/restart/quit.
+ * processes standard keystrokes for undo/redo/new/quit.
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
+#include <stdlib.h>
+#include <ctype.h>
#include "puzzles.h"
+enum { DEF_PARAMS, DEF_SEED, DEF_DESC }; /* for midend_game_id_int */
+
+struct midend_state_entry {
+ game_state *state;
+ int special; /* created by solve or restart */
+};
+
struct midend_data {
frontend *frontend;
random_state *random;
const game *ourgame;
- char *seed;
+ char *desc, *seedstr;
game_aux_info *aux_info;
- int fresh_seed;
+ enum { GOT_SEED, GOT_DESC, GOT_NOTHING } genmode;
int nstates, statesize, statepos;
game_params **presets;
char **preset_names;
int npresets, presetsize;
- game_params *params;
- game_state **states;
+ game_params *params, *curparams;
+ struct midend_state_entry *states;
game_drawstate *drawstate;
game_state *oldstate;
game_ui *ui;
#define ensure(me) do { \
if ((me)->nstates >= (me)->statesize) { \
(me)->statesize = (me)->nstates + 128; \
- (me)->states = sresize((me)->states, (me)->statesize, game_state *); \
+ (me)->states = sresize((me)->states, (me)->statesize, \
+ struct midend_state_entry); \
} \
} while (0)
me->nstates = me->statesize = me->statepos = 0;
me->states = NULL;
me->params = ourgame->default_params();
- me->seed = NULL;
+ me->curparams = NULL;
+ me->desc = NULL;
+ me->seedstr = NULL;
me->aux_info = NULL;
- me->fresh_seed = FALSE;
+ me->genmode = GOT_NOTHING;
me->drawstate = NULL;
me->oldstate = NULL;
me->presets = NULL;
void midend_free(midend_data *me)
{
sfree(me->states);
- sfree(me->seed);
+ sfree(me->desc);
+ sfree(me->seedstr);
+ random_free(me->random);
if (me->aux_info)
me->ourgame->free_aux_info(me->aux_info);
me->ourgame->free_params(me->params);
+ if (me->curparams)
+ me->ourgame->free_params(me->curparams);
sfree(me);
}
void midend_new_game(midend_data *me)
{
while (me->nstates > 0)
- me->ourgame->free_game(me->states[--me->nstates]);
+ me->ourgame->free_game(me->states[--me->nstates].state);
if (me->drawstate)
me->ourgame->free_drawstate(me->drawstate);
assert(me->nstates == 0);
- if (!me->fresh_seed) {
- sfree(me->seed);
+ if (me->genmode == GOT_DESC) {
+ me->genmode = GOT_NOTHING;
+ } else {
+ random_state *rs;
+
+ if (me->genmode == GOT_SEED) {
+ me->genmode = GOT_NOTHING;
+ } else {
+ /*
+ * Generate a new random seed. 15 digits comes to about
+ * 48 bits, which should be more than enough.
+ */
+ char newseed[16];
+ int i;
+ newseed[15] = '\0';
+ for (i = 0; i < 15; i++)
+ newseed[i] = '0' + random_upto(me->random, 10);
+ sfree(me->seedstr);
+ me->seedstr = dupstr(newseed);
+
+ if (me->curparams)
+ me->ourgame->free_params(me->curparams);
+ me->curparams = me->ourgame->dup_params(me->params);
+ }
+
+ sfree(me->desc);
if (me->aux_info)
me->ourgame->free_aux_info(me->aux_info);
me->aux_info = NULL;
- me->seed = me->ourgame->new_seed(me->params, me->random,
- &me->aux_info);
- } else
- me->fresh_seed = FALSE;
+
+ rs = random_init(me->seedstr, strlen(me->seedstr));
+ me->desc = me->ourgame->new_desc(me->curparams, rs, &me->aux_info);
+ random_free(rs);
+ }
ensure(me);
- me->states[me->nstates++] = me->ourgame->new_game(me->params, me->seed);
+ me->states[me->nstates].state = me->ourgame->new_game(me->params, me->desc);
+ me->states[me->nstates].special = TRUE;
+ me->nstates++;
me->statepos = 1;
- me->drawstate = me->ourgame->new_drawstate(me->states[0]);
+ me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
if (me->ui)
me->ourgame->free_ui(me->ui);
- me->ui = me->ourgame->new_ui(me->states[0]);
+ me->ui = me->ourgame->new_ui(me->states[0].state);
me->pressed_mouse_button = 0;
}
-void midend_restart_game(midend_data *me)
-{
- while (me->nstates > 1)
- me->ourgame->free_game(me->states[--me->nstates]);
- me->statepos = me->nstates;
- me->ourgame->free_ui(me->ui);
- me->ui = me->ourgame->new_ui(me->states[0]);
-}
-
static int midend_undo(midend_data *me)
{
if (me->statepos > 1) {
{
float flashtime;
- if (me->oldstate || me->statepos > 1) {
+ /*
+ * We do not flash if the later of the two states is special.
+ * This covers both forward Solve moves and backward (undone)
+ * Restart moves.
+ */
+ if ((me->oldstate || me->statepos > 1) &&
+ ((me->dir > 0 && !me->states[me->statepos-1].special) ||
+ (me->dir < 0 && me->statepos < me->nstates &&
+ !me->states[me->statepos].special))) {
flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
- me->states[me->statepos-2],
- me->states[me->statepos-1],
+ me->states[me->statepos-2].state,
+ me->states[me->statepos-1].state,
me->oldstate ? me->dir : +1);
if (flashtime > 0) {
me->flash_pos = 0.0F;
}
}
+void midend_restart_game(midend_data *me)
+{
+ game_state *s;
+
+ midend_stop_anim(me);
+
+ assert(me->statepos >= 1);
+ if (me->statepos == 1)
+ return; /* no point doing anything at all! */
+
+ s = me->ourgame->dup_game(me->states[0].state);
+
+ /*
+ * Now enter the restarted state as the next move.
+ */
+ midend_stop_anim(me);
+ while (me->nstates > me->statepos)
+ me->ourgame->free_game(me->states[--me->nstates].state);
+ ensure(me);
+ me->states[me->nstates].state = s;
+ me->states[me->nstates].special = TRUE; /* we just restarted */
+ me->statepos = ++me->nstates;
+ me->anim_time = 0.0;
+ midend_finish_move(me);
+ midend_redraw(me);
+ activate_timer(me->frontend);
+}
+
static int midend_really_process_key(midend_data *me, int x, int y, int button)
{
- game_state *oldstate = me->ourgame->dup_game(me->states[me->statepos - 1]);
+ game_state *oldstate =
+ me->ourgame->dup_game(me->states[me->statepos - 1].state);
+ int special = FALSE, gotspecial = FALSE;
float anim_time;
if (button == 'n' || button == 'N' || button == '\x0E') {
midend_new_game(me);
midend_redraw(me);
return 1; /* never animate */
- } else if (button == 'r' || button == 'R') {
- midend_stop_anim(me);
- midend_restart_game(me);
- midend_redraw(me);
- return 1; /* never animate */
} else if (button == 'u' || button == 'u' ||
button == '\x1A' || button == '\x1F') {
midend_stop_anim(me);
+ special = me->states[me->statepos-1].special;
+ gotspecial = TRUE;
if (!midend_undo(me))
return 1;
- } else if (button == '\x12') {
+ } else if (button == 'r' || button == 'R' ||
+ button == '\x12') {
midend_stop_anim(me);
if (!midend_redo(me))
return 1;
me->ourgame->free_game(oldstate);
return 0;
} else {
- game_state *s = me->ourgame->make_move(me->states[me->statepos-1],
- me->ui, x, y, button);
+ game_state *s =
+ me->ourgame->make_move(me->states[me->statepos-1].state,
+ me->ui, x, y, button);
- if (s == me->states[me->statepos-1]) {
+ if (s == me->states[me->statepos-1].state) {
/*
* make_move() is allowed to return its input state to
* indicate that although no move has been made, the UI
} else if (s) {
midend_stop_anim(me);
while (me->nstates > me->statepos)
- me->ourgame->free_game(me->states[--me->nstates]);
+ me->ourgame->free_game(me->states[--me->nstates].state);
ensure(me);
- me->states[me->nstates] = s;
+ me->states[me->nstates].state = s;
+ me->states[me->nstates].special = FALSE; /* normal move */
me->statepos = ++me->nstates;
me->dir = +1;
} else {
}
}
+ if (!gotspecial)
+ special = me->states[me->statepos-1].special;
+
/*
* See if this move requires an animation.
*/
- anim_time = me->ourgame->anim_length(oldstate, me->states[me->statepos-1],
- me->dir);
+ if (special) {
+ anim_time = 0;
+ } else {
+ anim_time = me->ourgame->anim_length(oldstate,
+ me->states[me->statepos-1].state,
+ me->dir);
+ }
me->oldstate = oldstate;
if (anim_time > 0) {
me->anim_pos < me->anim_time) {
assert(me->dir != 0);
me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
- me->states[me->statepos-1], me->dir,
+ me->states[me->statepos-1].state, me->dir,
me->ui, me->anim_pos, me->flash_pos);
} else {
me->ourgame->redraw(me->frontend, me->drawstate, NULL,
- me->states[me->statepos-1], +1 /*shrug*/,
+ me->states[me->statepos-1].state, +1 /*shrug*/,
me->ui, 0.0, me->flash_pos);
}
end_draw(me->frontend);
if (me->nstates == 0) {
game_aux_info *aux = NULL;
- char *seed = me->ourgame->new_seed(me->params, me->random, &aux);
- state = me->ourgame->new_game(me->params, seed);
- sfree(seed);
+ char *desc = me->ourgame->new_desc(me->params, me->random, &aux);
+ state = me->ourgame->new_game(me->params, desc);
+ sfree(desc);
if (aux)
me->ourgame->free_aux_info(aux);
} else
- state = me->states[0];
+ state = me->states[0].state;
ret = me->ourgame->colours(me->frontend, state, ncolours);
+ {
+ int i;
+
+ /*
+ * Allow environment-based overrides for the standard
+ * colours by defining variables along the lines of
+ * `NET_COLOUR_4=6000c0'.
+ */
+
+ for (i = 0; i < *ncolours; i++) {
+ char buf[80], *e;
+ unsigned int r, g, b;
+ int j;
+
+ sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
+ for (j = 0; buf[j]; j++)
+ buf[j] = toupper((unsigned char)buf[j]);
+ if ((e = getenv(buf)) != NULL &&
+ sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
+ ret[i*3 + 0] = r / 255.0;
+ ret[i*3 + 1] = g / 255.0;
+ ret[i*3 + 2] = b / 255.0;
+ }
+ }
+ }
+
if (me->nstates == 0)
me->ourgame->free_game(state);
}
}
+ {
+ /*
+ * Allow environment-based extensions to the preset list by
+ * defining a variable along the lines of `SOLO_PRESETS=2x3
+ * Advanced:2x3da'. Colon-separated list of items,
+ * alternating between textual titles in the menu and
+ * encoded parameter strings.
+ */
+ char buf[80], *e, *p;
+ int j;
+
+ sprintf(buf, "%s_PRESETS", me->ourgame->name);
+ for (j = 0; buf[j]; j++)
+ buf[j] = toupper((unsigned char)buf[j]);
+
+ if ((e = getenv(buf)) != NULL) {
+ p = e = dupstr(e);
+
+ while (*p) {
+ char *name, *val;
+ game_params *preset;
+
+ name = p;
+ while (*p && *p != ':') p++;
+ if (*p) *p++ = '\0';
+ val = p;
+ while (*p && *p != ':') p++;
+ if (*p) *p++ = '\0';
+
+ preset = me->ourgame->default_params();
+ me->ourgame->decode_params(preset, val);
+
+ if (me->ourgame->validate_params(preset)) {
+ /* Drop this one from the list. */
+ me->ourgame->free_params(preset);
+ continue;
+ }
+
+ if (me->presetsize <= me->npresets) {
+ me->presetsize = me->npresets + 10;
+ me->presets = sresize(me->presets, me->presetsize,
+ game_params *);
+ me->preset_names = sresize(me->preset_names,
+ me->presetsize, char *);
+ }
+
+ me->presets[me->npresets] = preset;
+ me->preset_names[me->npresets] = name;
+ me->npresets++;
+ }
+ }
+ }
+
return me->npresets;
}
*wintitle = dupstr(titlebuf);
return me->ourgame->configure(me->params);
case CFG_SEED:
- sprintf(titlebuf, "%s game selection", me->ourgame->name);
+ case CFG_DESC:
+ sprintf(titlebuf, "%s %s selection", me->ourgame->name,
+ which == CFG_SEED ? "random" : "game");
*wintitle = dupstr(titlebuf);
ret = snewn(2, config_item);
ret[0].type = C_STRING;
- ret[0].name = "Game ID";
+ if (which == CFG_SEED)
+ ret[0].name = "Game random seed";
+ else
+ ret[0].name = "Game ID";
ret[0].ival = 0;
/*
- * The text going in here will be a string encoding of the
- * parameters, plus a colon, plus the game seed. This is a
- * full game ID.
+ * For CFG_DESC the text going in here will be a string
+ * encoding of the restricted parameters, plus a colon,
+ * plus the game description. For CFG_SEED it will be the
+ * full parameters, plus a hash, plus the random seed data.
+ * Either of these is a valid full game ID (although only
+ * the former is likely to persist across many code
+ * changes).
*/
- parstr = me->ourgame->encode_params(me->params);
- ret[0].sval = snewn(strlen(parstr) + strlen(me->seed) + 2, char);
- sprintf(ret[0].sval, "%s:%s", parstr, me->seed);
+ parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
+ if (which == CFG_DESC) {
+ ret[0].sval = snewn(strlen(parstr) + strlen(me->desc) + 2, char);
+ sprintf(ret[0].sval, "%s:%s", parstr, me->desc);
+ } else if (me->seedstr) {
+ ret[0].sval = snewn(strlen(parstr) + strlen(me->seedstr) + 2, char);
+ sprintf(ret[0].sval, "%s#%s", parstr, me->seedstr);
+ } else {
+ /*
+ * If the current game was not randomly generated, the
+ * best we can do is to give a template for typing a
+ * new seed in.
+ */
+ ret[0].sval = snewn(strlen(parstr) + 2, char);
+ sprintf(ret[0].sval, "%s#", parstr);
+ }
sfree(parstr);
ret[1].type = C_END;
return NULL;
}
-char *midend_game_id(midend_data *me, char *id, int def_seed)
+static char *midend_game_id_int(midend_data *me, char *id, int defmode)
{
- char *error, *par, *seed;
- game_params *params;
+ char *error, *par, *desc, *seed;
- seed = strchr(id, ':');
+ seed = strchr(id, '#');
+ desc = strchr(id, ':');
- if (seed) {
+ if (desc && (!seed || desc < seed)) {
+ /*
+ * We have a colon separating parameters from game
+ * description. So `par' now points to the parameters
+ * string, and `desc' to the description string.
+ */
+ *desc++ = '\0';
+ par = id;
+ seed = NULL;
+ } else if (seed && (!desc || seed < desc)) {
/*
- * We have a colon separating parameters from game seed. So
- * `par' now points to the parameters string, and `seed' to
- * the seed string.
+ * We have a hash separating parameters from random seed.
+ * So `par' now points to the parameters string, and `seed'
+ * to the seed string.
*/
*seed++ = '\0';
par = id;
+ desc = NULL;
} else {
/*
- * We only have one string. Depending on `def_seed', we
- * take it to be either parameters or seed.
+ * We only have one string. Depending on `defmode', we take
+ * it to be either parameters, seed or description.
*/
- if (def_seed) {
+ if (defmode == DEF_SEED) {
seed = id;
- par = NULL;
+ par = desc = NULL;
+ } else if (defmode == DEF_DESC) {
+ desc = id;
+ par = seed = NULL;
} else {
- seed = NULL;
par = id;
+ seed = desc = NULL;
}
}
if (par) {
- params = me->ourgame->decode_params(par);
- error = me->ourgame->validate_params(params);
+ game_params *tmpparams;
+ tmpparams = me->ourgame->dup_params(me->params);
+ me->ourgame->decode_params(tmpparams, par);
+ error = me->ourgame->validate_params(tmpparams);
if (error) {
- me->ourgame->free_params(params);
+ me->ourgame->free_params(tmpparams);
return error;
}
- me->ourgame->free_params(me->params);
- me->params = params;
+ if (me->curparams)
+ me->ourgame->free_params(me->curparams);
+ me->curparams = tmpparams;
+
+ /*
+ * Now filter only the persistent parts of this state into
+ * the long-term params structure, unless we've _only_
+ * received a params string in which case the whole lot is
+ * persistent.
+ */
+ if (seed || desc) {
+ char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
+ me->ourgame->decode_params(me->params, tmpstr);
+ sfree(tmpstr);
+ } else {
+ me->ourgame->free_params(me->params);
+ me->params = me->ourgame->dup_params(tmpparams);
+ }
}
- if (seed) {
- error = me->ourgame->validate_seed(me->params, seed);
+ sfree(me->desc);
+ me->desc = NULL;
+ sfree(me->seedstr);
+ me->seedstr = NULL;
+
+ if (desc) {
+ error = me->ourgame->validate_desc(me->params, desc);
if (error)
return error;
- sfree(me->seed);
- me->seed = dupstr(seed);
- me->fresh_seed = TRUE;
+ me->desc = dupstr(desc);
+ me->genmode = GOT_DESC;
if (me->aux_info)
me->ourgame->free_aux_info(me->aux_info);
me->aux_info = NULL;
}
+ if (seed) {
+ me->seedstr = dupstr(seed);
+ me->genmode = GOT_SEED;
+ }
+
return NULL;
}
+char *midend_game_id(midend_data *me, char *id)
+{
+ return midend_game_id_int(me, id, DEF_PARAMS);
+}
+
char *midend_set_config(midend_data *me, int which, config_item *cfg)
{
char *error;
break;
case CFG_SEED:
- error = midend_game_id(me, cfg[0].sval, TRUE);
+ case CFG_DESC:
+ error = midend_game_id_int(me, cfg[0].sval,
+ (which == CFG_SEED ? DEF_SEED : DEF_DESC));
if (error)
return error;
break;
char *midend_text_format(midend_data *me)
{
if (me->ourgame->can_format_as_text && me->statepos > 0)
- return me->ourgame->text_format(me->states[me->statepos-1]);
+ return me->ourgame->text_format(me->states[me->statepos-1].state);
else
return NULL;
}
return "No game set up to solve"; /* _shouldn't_ happen! */
msg = "Solve operation failed"; /* game _should_ overwrite on error */
- s = me->ourgame->solve(me->states[0], me->aux_info, &msg);
+ s = me->ourgame->solve(me->states[0].state, me->aux_info, &msg);
if (!s)
return msg;
/*
- * Now enter the solved state as the next move.~|~
+ * Now enter the solved state as the next move.
*/
midend_stop_anim(me);
while (me->nstates > me->statepos)
- me->ourgame->free_game(me->states[--me->nstates]);
+ me->ourgame->free_game(me->states[--me->nstates].state);
ensure(me);
- me->states[me->nstates] = s;
+ me->states[me->nstates].state = s;
+ me->states[me->nstates].special = TRUE; /* created using solve */
me->statepos = ++me->nstates;
me->anim_time = 0.0;
midend_finish_move(me);