X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/cbb5549e12c8edfd8b38b642b7b25c1fb8912c9b..28d0118c5fbdbbbb656f0ea6fb3b2e48e6aa73f8:/midend.c diff --git a/midend.c b/midend.c index 4636fa0..0c91491 100644 --- a/midend.c +++ b/midend.c @@ -11,36 +11,48 @@ #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; - int fresh_seed; + char *desc, *seedstr; + game_aux_info *aux_info; + 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, *tmpparams; + struct midend_state_entry *states; game_drawstate *drawstate; game_state *oldstate; game_ui *ui; float anim_time, anim_pos; float flash_time, flash_pos; int dir; + + int pressed_mouse_button; }; #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) -midend_data *midend_new(frontend *fe) +midend_data *midend_new(frontend *fe, const game *ourgame) { midend_data *me = snew(midend_data); void *randseed; @@ -49,12 +61,16 @@ midend_data *midend_new(frontend *fe) get_random_seed(&randseed, &randseedsize); me->frontend = fe; + me->ourgame = ourgame; me->random = random_init(randseed, randseedsize); me->nstates = me->statesize = me->statepos = 0; me->states = NULL; - me->params = default_params(); - me->seed = NULL; - me->fresh_seed = FALSE; + me->params = ourgame->default_params(); + me->tmpparams = NULL; + me->desc = NULL; + me->seedstr = NULL; + me->aux_info = NULL; + me->genmode = GOT_NOTHING; me->drawstate = NULL; me->oldstate = NULL; me->presets = NULL; @@ -64,6 +80,7 @@ midend_data *midend_new(frontend *fe) me->flash_time = me->flash_pos = 0.0F; me->dir = 0; me->ui = NULL; + me->pressed_mouse_button = 0; sfree(randseed); @@ -73,54 +90,85 @@ midend_data *midend_new(frontend *fe) void midend_free(midend_data *me) { sfree(me->states); - sfree(me->seed); - free_params(me->params); + 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->tmpparams) + me->ourgame->free_params(me->tmpparams); sfree(me); } void midend_size(midend_data *me, int *x, int *y) { - game_size(me->params, x, y); + me->ourgame->size(me->params, x, y); } void midend_set_params(midend_data *me, game_params *params) { - free_params(me->params); - me->params = dup_params(params); + me->ourgame->free_params(me->params); + me->params = me->ourgame->dup_params(params); } void midend_new_game(midend_data *me) { while (me->nstates > 0) - free_game(me->states[--me->nstates]); + me->ourgame->free_game(me->states[--me->nstates].state); if (me->drawstate) - game_free_drawstate(me->drawstate); + me->ourgame->free_drawstate(me->drawstate); assert(me->nstates == 0); - if (!me->fresh_seed) { - sfree(me->seed); - me->seed = new_game_seed(me->params, me->random); - } else - me->fresh_seed = FALSE; + 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); + } + + sfree(me->desc); + if (me->aux_info) + me->ourgame->free_aux_info(me->aux_info); + me->aux_info = NULL; + + rs = random_init(me->seedstr, strlen(me->seedstr)); + me->desc = me->ourgame->new_desc + (me->tmpparams ? me->tmpparams : me->params, rs, &me->aux_info); + random_free(rs); + + if (me->tmpparams) { + me->ourgame->free_params(me->tmpparams); + me->tmpparams = NULL; + } + } ensure(me); - me->states[me->nstates++] = 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 = game_new_drawstate(me->states[0]); + me->drawstate = me->ourgame->new_drawstate(me->states[0].state); if (me->ui) - free_ui(me->ui); - me->ui = new_ui(me->states[0]); -} - -void midend_restart_game(midend_data *me) -{ - while (me->nstates > 1) - free_game(me->states[--me->nstates]); - me->statepos = me->nstates; - free_ui(me->ui); - me->ui = new_ui(me->states[0]); + me->ourgame->free_ui(me->ui); + me->ui = me->ourgame->new_ui(me->states[0].state); + me->pressed_mouse_button = 0; } static int midend_undo(midend_data *me) @@ -147,11 +195,19 @@ static void midend_finish_move(midend_data *me) { float flashtime; - if (me->oldstate || me->statepos > 1) { - flashtime = game_flash_length(me->oldstate ? me->oldstate : - me->states[me->statepos-2], - me->states[me->statepos-1], - me->oldstate ? me->dir : +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].state, + me->states[me->statepos-1].state, + me->oldstate ? me->dir : +1); if (flashtime > 0) { me->flash_pos = 0.0F; me->flash_time = flashtime; @@ -159,7 +215,7 @@ static void midend_finish_move(midend_data *me) } if (me->oldstate) - free_game(me->oldstate); + me->ourgame->free_game(me->oldstate); me->oldstate = NULL; me->anim_pos = me->anim_time = 0; me->dir = 0; @@ -178,9 +234,37 @@ static void midend_stop_anim(midend_data *me) } } -int midend_process_key(midend_data *me, int x, int y, int button) +void midend_restart_game(midend_data *me) +{ + game_state *s; + + 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 = 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') { @@ -196,6 +280,8 @@ int midend_process_key(midend_data *me, int x, int y, int button) } 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') { @@ -203,13 +289,14 @@ int midend_process_key(midend_data *me, int x, int y, int button) if (!midend_redo(me)) return 1; } else if (button == 'q' || button == 'Q' || button == '\x11') { - free_game(oldstate); + me->ourgame->free_game(oldstate); return 0; } else { - game_state *s = 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 @@ -220,21 +307,31 @@ int midend_process_key(midend_data *me, int x, int y, int button) } else if (s) { midend_stop_anim(me); while (me->nstates > me->statepos) - 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 { - free_game(oldstate); + me->ourgame->free_game(oldstate); return 1; } } + if (!gotspecial) + special = me->states[me->statepos-1].special; + /* * See if this move requires an animation. */ - anim_time = game_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) { @@ -252,6 +349,104 @@ int midend_process_key(midend_data *me, int x, int y, int button) return 1; } +int midend_process_key(midend_data *me, int x, int y, int button) +{ + int ret = 1; + + /* + * Harmonise mouse drag and release messages. + * + * Some front ends might accidentally switch from sending, say, + * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a + * drag. (This can happen on the Mac, for example, since + * RIGHT_DRAG is usually done using Command+drag, and if the + * user accidentally releases Command half way through the drag + * then there will be trouble.) + * + * It would be an O(number of front ends) annoyance to fix this + * in the front ends, but an O(number of back ends) annoyance + * to have each game capable of dealing with it. Therefore, we + * fix it _here_ in the common midend code so that it only has + * to be done once. + * + * The possible ways in which things can go screwy in the front + * end are: + * + * - in a system containing multiple physical buttons button + * presses can inadvertently overlap. We can see ABab (caps + * meaning button-down and lowercase meaning button-up) when + * the user had semantically intended AaBb. + * + * - in a system where one button is simulated by means of a + * modifier key and another button, buttons can mutate + * between press and release (possibly during drag). So we + * can see Ab instead of Aa. + * + * Definite requirements are: + * + * - button _presses_ must never be invented or destroyed. If + * the user presses two buttons in succession, the button + * presses must be transferred to the backend unchanged. So + * if we see AaBb , that's fine; if we see ABab (the button + * presses inadvertently overlapped) we must somehow + * `correct' it to AaBb. + * + * - every mouse action must end up looking like a press, zero + * or more drags, then a release. This allows back ends to + * make the _assumption_ that incoming mouse data will be + * sane in this regard, and not worry about the details. + * + * So my policy will be: + * + * - treat any button-up as a button-up for the currently + * pressed button, or ignore it if there is no currently + * pressed button. + * + * - treat any drag as a drag for the currently pressed + * button, or ignore it if there is no currently pressed + * button. + * + * - if we see a button-down while another button is currently + * pressed, invent a button-up for the first one and then + * pass the button-down through as before. + * + */ + if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) { + if (me->pressed_mouse_button) { + if (IS_MOUSE_DRAG(button)) { + button = me->pressed_mouse_button + + (LEFT_DRAG - LEFT_BUTTON); + } else { + button = me->pressed_mouse_button + + (LEFT_RELEASE - LEFT_BUTTON); + } + } else + return ret; /* ignore it */ + } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) { + /* + * Fabricate a button-up for the previously pressed button. + */ + ret = ret && midend_really_process_key + (me, x, y, (me->pressed_mouse_button + + (LEFT_RELEASE - LEFT_BUTTON))); + } + + /* + * Now send on the event we originally received. + */ + ret = ret && midend_really_process_key(me, x, y, button); + + /* + * And update the currently pressed button. + */ + if (IS_MOUSE_RELEASE(button)) + me->pressed_mouse_button = 0; + else if (IS_MOUSE_DOWN(button)) + me->pressed_mouse_button = button; + + return ret; +} + void midend_redraw(midend_data *me) { if (me->statepos > 0 && me->drawstate) { @@ -259,13 +454,13 @@ void midend_redraw(midend_data *me) if (me->oldstate && me->anim_time > 0 && me->anim_pos < me->anim_time) { assert(me->dir != 0); - game_redraw(me->frontend, me->drawstate, me->oldstate, - me->states[me->statepos-1], me->dir, - me->ui, me->anim_pos, me->flash_pos); + me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate, + me->states[me->statepos-1].state, me->dir, + me->ui, me->anim_pos, me->flash_pos); } else { - game_redraw(me->frontend, me->drawstate, NULL, - me->states[me->statepos-1], +1 /*shrug*/, - me->ui, 0.0, me->flash_pos); + me->ourgame->redraw(me->frontend, me->drawstate, NULL, + me->states[me->statepos-1].state, +1 /*shrug*/, + me->ui, 0.0, me->flash_pos); } end_draw(me->frontend); } @@ -294,16 +489,19 @@ float *midend_colours(midend_data *me, int *ncolours) float *ret; if (me->nstates == 0) { - char *seed = new_game_seed(me->params, me->random); - state = new_game(me->params, seed); - sfree(seed); + game_aux_info *aux = NULL; + 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 = game_colours(me->frontend, state, ncolours); + ret = me->ourgame->colours(me->frontend, state, ncolours); if (me->nstates == 0) - free_game(state); + me->ourgame->free_game(state); return ret; } @@ -314,7 +512,7 @@ int midend_num_presets(midend_data *me) char *name; game_params *preset; - while (game_fetch_preset(me->npresets, &name, &preset)) { + while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) { if (me->presetsize <= me->npresets) { me->presetsize = me->npresets + 10; me->presets = sresize(me->presets, me->presetsize, @@ -342,7 +540,7 @@ void midend_fetch_preset(midend_data *me, int n, int midend_wants_statusbar(midend_data *me) { - return game_wants_statusbar(); + return me->ourgame->wants_statusbar(); } config_item *midend_get_config(midend_data *me, int which, char **wintitle) @@ -350,30 +548,52 @@ config_item *midend_get_config(midend_data *me, int which, char **wintitle) char *titlebuf, *parstr; config_item *ret; - titlebuf = snewn(40 + strlen(game_name), char); + titlebuf = snewn(40 + strlen(me->ourgame->name), char); switch (which) { case CFG_SETTINGS: - sprintf(titlebuf, "%s configuration", game_name); + sprintf(titlebuf, "%s configuration", me->ourgame->name); *wintitle = dupstr(titlebuf); - return game_configure(me->params); + return me->ourgame->configure(me->params); case CFG_SEED: - sprintf(titlebuf, "%s game selection", game_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 = 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->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; @@ -387,59 +607,103 @@ config_item *midend_get_config(midend_data *me, int which, char **wintitle) 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 = decode_params(par); - error = 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) { - free_params(params); + me->ourgame->free_params(tmpparams); return error; } - free_params(me->params); - me->params = params; + 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 (seed) { - error = validate_seed(me->params, seed); + 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; + 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; @@ -447,20 +711,22 @@ char *midend_set_config(midend_data *me, int which, config_item *cfg) switch (which) { case CFG_SETTINGS: - params = custom_params(cfg); - error = validate_params(params); + params = me->ourgame->custom_params(cfg); + error = me->ourgame->validate_params(params); if (error) { - free_params(params); + me->ourgame->free_params(params); return error; } - free_params(me->params); + me->ourgame->free_params(me->params); me->params = params; 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; @@ -468,3 +734,44 @@ char *midend_set_config(midend_data *me, int which, config_item *cfg) 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; +}