Added an automatic `Solve' feature to most games. This is useful for
[sgt/puzzles] / midend.c
index a28f3db..084cd16 100644 (file)
--- a/midend.c
+++ b/midend.c
@@ -17,6 +17,7 @@ struct midend_data {
     const game *ourgame;
 
     char *seed;
+    game_aux_info *aux_info;
     int fresh_seed;
     int nstates, statesize, statepos;
 
@@ -32,6 +33,8 @@ struct midend_data {
     float anim_time, anim_pos;
     float flash_time, flash_pos;
     int dir;
+
+    int pressed_mouse_button;
 };
 
 #define ensure(me) do { \
@@ -56,6 +59,7 @@ midend_data *midend_new(frontend *fe, const game *ourgame)
     me->states = NULL;
     me->params = ourgame->default_params();
     me->seed = NULL;
+    me->aux_info = NULL;
     me->fresh_seed = FALSE;
     me->drawstate = NULL;
     me->oldstate = NULL;
@@ -66,6 +70,7 @@ midend_data *midend_new(frontend *fe, const game *ourgame)
     me->flash_time = me->flash_pos = 0.0F;
     me->dir = 0;
     me->ui = NULL;
+    me->pressed_mouse_button = 0;
 
     sfree(randseed);
 
@@ -76,6 +81,8 @@ void midend_free(midend_data *me)
 {
     sfree(me->states);
     sfree(me->seed);
+    if (me->aux_info)
+       me->ourgame->free_aux_info(me->aux_info);
     me->ourgame->free_params(me->params);
     sfree(me);
 }
@@ -103,7 +110,11 @@ void midend_new_game(midend_data *me)
 
     if (!me->fresh_seed) {
        sfree(me->seed);
-       me->seed = me->ourgame->new_seed(me->params, me->random);
+       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;
 
@@ -114,6 +125,7 @@ void midend_new_game(midend_data *me)
     if (me->ui)
         me->ourgame->free_ui(me->ui);
     me->ui = me->ourgame->new_ui(me->states[0]);
+    me->pressed_mouse_button = 0;
 }
 
 void midend_restart_game(midend_data *me)
@@ -180,7 +192,7 @@ static void midend_stop_anim(midend_data *me)
     }
 }
 
-int midend_process_key(midend_data *me, int x, int y, int button)
+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]);
     float anim_time;
@@ -255,6 +267,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) {
@@ -297,9 +407,12 @@ float *midend_colours(midend_data *me, int *ncolours)
     float *ret;
 
     if (me->nstates == 0) {
-        char *seed = me->ourgame->new_seed(me->params, me->random);
+       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);
+       if (aux)
+           me->ourgame->free_aux_info(aux);
     } else
         state = me->states[0];
 
@@ -438,6 +551,9 @@ char *midend_game_id(midend_data *me, char *id, int def_seed)
         sfree(me->seed);
         me->seed = dupstr(seed);
         me->fresh_seed = TRUE;
+       if (me->aux_info)
+           me->ourgame->free_aux_info(me->aux_info);
+       me->aux_info = NULL;
     }
 
     return NULL;
@@ -471,3 +587,43 @@ 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]);
+    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], 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]);
+    ensure(me);
+    me->states[me->nstates] = s;
+    me->statepos = ++me->nstates;
+    me->anim_time = 0.0;
+    midend_finish_move(me);
+    midend_redraw(me);
+    activate_timer(me->frontend);
+    return NULL;
+}