Rogue diagnostic!
[sgt/puzzles] / midend.c
index b6b1720..e545841 100644 (file)
--- a/midend.c
+++ b/midend.c
@@ -25,7 +25,29 @@ struct midend_data {
     random_state *random;
     const game *ourgame;
 
-    char *desc, *seedstr;
+    /*
+     * `desc' and `privdesc' deserve a comment.
+     * 
+     * `desc' is the game description as presented to the user when
+     * they ask for Game -> Specific. `privdesc', if non-NULL, is a
+     * different game description used to reconstruct the initial
+     * game_state when de-serialising. If privdesc is NULL, `desc'
+     * is used for both.
+     * 
+     * For almost all games, `privdesc' is NULL and never used. The
+     * exception (as usual) is Mines: the initial game state has no
+     * squares open at all, but after the first click `desc' is
+     * rewritten to describe a game state with an initial click and
+     * thus a bunch of squares open. If we used that desc to
+     * serialise and deserialise, then the initial game state after
+     * deserialisation would look unlike the initial game state
+     * beforehand, and worse still execute_move() might fail on the
+     * attempted first click. So `privdesc' is also used in this
+     * case, to provide a game description describing the same
+     * fixed mine layout _but_ no initial click. (These game IDs
+     * may also be typed directly into Mines if you like.)
+     */
+    char *desc, *privdesc, *seedstr;
     game_aux_info *aux_info;
     enum { GOT_SEED, GOT_DESC, GOT_NOTHING } genmode;
     int nstates, statesize, statepos;
@@ -48,6 +70,8 @@ struct midend_data {
     char *laststatus;
 
     int pressed_mouse_button;
+
+    int winwidth, winheight;
 };
 
 #define ensure(me) do { \
@@ -73,7 +97,7 @@ midend_data *midend_new(frontend *fe, const game *ourgame)
     me->states = NULL;
     me->params = ourgame->default_params();
     me->curparams = NULL;
-    me->desc = NULL;
+    me->desc = me->privdesc = NULL;
     me->seedstr = NULL;
     me->aux_info = NULL;
     me->genmode = GOT_NOTHING;
@@ -90,6 +114,7 @@ midend_data *midend_new(frontend *fe, const game *ourgame)
     me->laststatus = NULL;
     me->timing = FALSE;
     me->elapsed = 0.0F;
+    me->winwidth = me->winheight = 0;
 
     sfree(randseed);
 
@@ -134,9 +159,11 @@ void midend_free(midend_data *me)
     sfree(me);
 }
 
-void midend_size(midend_data *me, int *x, int *y)
+void midend_size(midend_data *me, int *x, int *y, int expand)
 {
-    me->ourgame->size(me->params, x, y);
+    me->ourgame->size(me->params, me->drawstate, x, y, expand);
+    me->winwidth = *x;
+    me->winheight = *y;
 }
 
 void midend_set_params(midend_data *me, game_params *params)
@@ -155,11 +182,18 @@ static void midend_set_timer(midend_data *me)
        deactivate_timer(me->frontend);
 }
 
+static void midend_size_new_drawstate(midend_data *me)
+{
+    me->ourgame->size(me->params, me->drawstate, &me->winwidth, &me->winheight,
+                      TRUE);
+}
+
 void midend_force_redraw(midend_data *me)
 {
     if (me->drawstate)
         me->ourgame->free_drawstate(me->drawstate);
     me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
+    midend_size_new_drawstate(me);
     midend_redraw(me);
 }
 
@@ -200,6 +234,7 @@ void midend_new_game(midend_data *me)
         }
 
        sfree(me->desc);
+       sfree(me->privdesc);
        if (me->aux_info)
            me->ourgame->free_aux_info(me->aux_info);
        me->aux_info = NULL;
@@ -207,6 +242,7 @@ void midend_new_game(midend_data *me)
         rs = random_init(me->seedstr, strlen(me->seedstr));
         me->desc = me->ourgame->new_desc(me->curparams, rs,
                                         &me->aux_info, TRUE);
+       me->privdesc = NULL;
         random_free(rs);
     }
 
@@ -217,6 +253,7 @@ void midend_new_game(midend_data *me)
     me->nstates++;
     me->statepos = 1;
     me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
+    midend_size_new_drawstate(me);
     me->elapsed = 0.0F;
     midend_set_timer(me);
     if (me->ui)
@@ -228,6 +265,10 @@ void midend_new_game(midend_data *me)
 static int midend_undo(midend_data *me)
 {
     if (me->statepos > 1) {
+        if (me->ui)
+            me->ourgame->changed_state(me->ui,
+                                       me->states[me->statepos-1].state,
+                                       me->states[me->statepos-2].state);
        me->statepos--;
         me->dir = -1;
         return 1;
@@ -238,6 +279,10 @@ static int midend_undo(midend_data *me)
 static int midend_redo(midend_data *me)
 {
     if (me->statepos < me->nstates) {
+        if (me->ui)
+            me->ourgame->changed_state(me->ui,
+                                       me->states[me->statepos-1].state,
+                                       me->states[me->statepos].state);
        me->statepos++;
         me->dir = +1;
         return 1;
@@ -296,7 +341,14 @@ void midend_restart_game(midend_data *me)
     if (me->statepos == 1)
         return;                        /* no point doing anything at all! */
 
-    s = me->ourgame->dup_game(me->states[0].state);
+    /*
+     * During restart, we reconstruct the game from the (public)
+     * game description rather than from states[0], because that
+     * way Mines gets slightly more sensible behaviour (restart
+     * goes to _after_ the first click so you don't have to
+     * remember where you clicked).
+     */
+    s = me->ourgame->new_game(me, me->params, me->desc);
 
     /*
      * Now enter the restarted state as the next move.
@@ -308,6 +360,10 @@ void midend_restart_game(midend_data *me)
     me->states[me->nstates].state = s;
     me->states[me->nstates].special = TRUE;   /* we just restarted */
     me->statepos = ++me->nstates;
+    if (me->ui)
+        me->ourgame->changed_state(me->ui,
+                                   me->states[me->statepos-2].state,
+                                   me->states[me->statepos-1].state);
     me->anim_time = 0.0;
     midend_finish_move(me);
     midend_redraw(me);
@@ -342,9 +398,22 @@ static int midend_really_process_key(midend_data *me, int x, int y, int button)
        ret = 0;
        goto done;
     } else {
-        game_state *s =
-            me->ourgame->make_move(me->states[me->statepos-1].state,
-                                   me->ui, me->drawstate, x, y, button);
+        game_state *s;
+       char *movestr;
+       
+       movestr =
+            me->ourgame->interpret_move(me->states[me->statepos-1].state,
+                                       me->ui, me->drawstate, x, y, button);
+       if (!movestr)
+           s = NULL;
+       else if (!*movestr)
+           s = me->states[me->statepos-1].state;
+       else {
+           s = me->ourgame->execute_move(me->states[me->statepos-1].state,
+                                         movestr);
+           assert(s != NULL);
+           sfree(movestr);
+       }
 
         if (s == me->states[me->statepos-1].state) {
             /*
@@ -699,10 +768,12 @@ int midend_wants_statusbar(midend_data *me)
     return me->ourgame->wants_statusbar();
 }
 
-void midend_supersede_game_desc(midend_data *me, char *desc)
+void midend_supersede_game_desc(midend_data *me, char *desc, char *privdesc)
 {
     sfree(me->desc);
+    sfree(me->privdesc);
     me->desc = dupstr(desc);
+    me->privdesc = privdesc ? dupstr(privdesc) : NULL;
 }
 
 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
@@ -842,7 +913,8 @@ static char *midend_game_id_int(midend_data *me, char *id, int defmode)
     }
 
     sfree(me->desc);
-    me->desc = NULL;
+    sfree(me->privdesc);
+    me->desc = me->privdesc = NULL;
     sfree(me->seedstr);
     me->seedstr = NULL;
 
@@ -913,7 +985,7 @@ char *midend_text_format(midend_data *me)
 char *midend_solve(midend_data *me)
 {
     game_state *s;
-    char *msg;
+    char *msg, *movestr;
 
     if (!me->ourgame->can_solve)
        return "This game does not support the Solve operation";
@@ -922,9 +994,14 @@ char *midend_solve(midend_data *me)
        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)
+    movestr = me->ourgame->solve(me->states[0].state,
+                                me->states[me->statepos-1].state,
+                                me->aux_info, &msg);
+    if (!movestr)
        return msg;
+    s = me->ourgame->execute_move(me->states[me->statepos-1].state, movestr);
+    assert(s);
+    sfree(movestr);
 
     /*
      * Now enter the solved state as the next move.
@@ -936,6 +1013,10 @@ char *midend_solve(midend_data *me)
     me->states[me->nstates].state = s;
     me->states[me->nstates].special = TRUE;   /* created using solve */
     me->statepos = ++me->nstates;
+    if (me->ui)
+        me->ourgame->changed_state(me->ui,
+                                   me->states[me->statepos-2].state,
+                                   me->states[me->statepos-1].state);
     me->anim_time = 0.0;
     midend_finish_move(me);
     midend_redraw(me);