+
+int midend_wants_statusbar(midend_data *me)
+{
+ return me->ourgame->wants_statusbar();
+}
+
+config_item *midend_get_config(midend_data *me, int which, char **wintitle)
+{
+ char *titlebuf, *parstr;
+ config_item *ret;
+
+ titlebuf = snewn(40 + strlen(me->ourgame->name), char);
+
+ switch (which) {
+ case CFG_SETTINGS:
+ sprintf(titlebuf, "%s configuration", me->ourgame->name);
+ *wintitle = dupstr(titlebuf);
+ return me->ourgame->configure(me->params);
+ case CFG_SEED:
+ 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;
+ if (which == CFG_SEED)
+ ret[0].name = "Game random seed";
+ else
+ ret[0].name = "Game ID";
+ ret[0].ival = 0;
+ /*
+ * 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, 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;
+ ret[1].name = ret[1].sval = NULL;
+ ret[1].ival = 0;
+
+ return ret;
+ }
+
+ assert(!"We shouldn't be here");
+ return NULL;
+}
+
+static char *midend_game_id_int(midend_data *me, char *id, int defmode)
+{
+ char *error, *par, *desc, *seed;
+
+ seed = strchr(id, '#');
+ desc = strchr(id, ':');
+
+ 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 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 `defmode', we take
+ * it to be either parameters, seed or description.
+ */
+ if (defmode == DEF_SEED) {
+ seed = id;
+ par = desc = NULL;
+ } else if (defmode == DEF_DESC) {
+ desc = id;
+ par = seed = NULL;
+ } else {
+ par = id;
+ seed = desc = NULL;
+ }
+ }
+
+ if (par) {
+ 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(tmpparams);
+ return error;
+ }
+ if (me->tmpparams)
+ me->ourgame->free_params(me->tmpparams);
+ me->tmpparams = 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);
+ } else {
+ me->ourgame->free_params(me->params);
+ me->params = me->ourgame->dup_params(tmpparams);
+ }
+ }
+
+ if (desc) {
+ error = me->ourgame->validate_desc(me->params, desc);
+ if (error)
+ return error;
+
+ sfree(me->desc);
+ 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) {
+ sfree(me->seedstr);
+ 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;
+ game_params *params;
+
+ switch (which) {
+ case CFG_SETTINGS:
+ params = me->ourgame->custom_params(cfg);
+ error = me->ourgame->validate_params(params);
+
+ if (error) {
+ me->ourgame->free_params(params);
+ return error;
+ }
+
+ me->ourgame->free_params(me->params);
+ me->params = params;
+ break;
+
+ case CFG_SEED:
+ case CFG_DESC:
+ error = midend_game_id_int(me, cfg[0].sval,
+ (which == CFG_SEED ? DEF_SEED : DEF_DESC));
+ if (error)
+ return error;
+ break;
+ }
+
+ return NULL;
+}
+
+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].state);
+ else
+ return NULL;
+}
+
+char *midend_solve(midend_data *me)
+{
+ game_state *s;
+ char *msg;
+
+ if (!me->ourgame->can_solve)
+ return "This game does not support the Solve operation";
+
+ if (me->statepos < 1)
+ 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].state, me->aux_info, &msg);
+ if (!s)
+ return msg;
+
+ /*
+ * 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].state);
+ ensure(me);
+ 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);
+ midend_redraw(me);
+ activate_timer(me->frontend);
+ return NULL;
+}