Stop the analysis pass in Loopy's redraw routine from being
[sgt/puzzles] / pegs.c
diff --git a/pegs.c b/pegs.c
index 8672513..3dac5fc 100644 (file)
--- a/pegs.c
+++ b/pegs.c
 #define GRID_PEG  1
 #define GRID_OBST 2
 
+#define GRID_CURSOR 10
+#define GRID_JUMPING 20
+
 enum {
     COL_BACKGROUND,
     COL_HIGHLIGHT,
     COL_LOWLIGHT,
     COL_PEG,
+    COL_CURSOR,
     NCOLOURS
 };
 
@@ -42,6 +46,8 @@ static char const *const pegs_titletypes[] = { TYPELIST(TITLE) };
 static char const *const pegs_lowertypes[] = { TYPELIST(LOWER) };
 #define TYPECONFIG TYPELIST(CONFIG)
 
+#define FLASH_FRAME 0.13F
+
 struct game_params {
     int w, h;
     int type;
@@ -49,6 +55,7 @@ struct game_params {
 
 struct game_state {
     int w, h;
+    int completed;
     unsigned char *grid;
 };
 
@@ -175,9 +182,9 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
-    if (params->w <= 3 || params->h <= 3)
+    if (full && (params->w <= 3 || params->h <= 3))
        return "Width and height must both be greater than three";
 
     /*
@@ -186,7 +193,7 @@ static char *validate_params(game_params *params)
      * soluble. For the moment, therefore, I'm going to disallow
      * them at any size other than the standard one.
      */
-    if (params->type == TYPE_CROSS || params->type == TYPE_OCTAGON) {
+    if (full && (params->type == TYPE_CROSS || params->type == TYPE_OCTAGON)) {
        if (params->w != 7 || params->h != 7)
            return "This board type is only supported at 7x7";
     }
@@ -486,7 +493,9 @@ static void pegs_generate(unsigned char *grid, int w, int h, random_state *rs)
        printf("insufficient extent; trying again\n");
 #endif
     }
+#ifdef GENERATION_DIAGNOSTICS
     fflush(stdout);
+#endif
 }
 
 /* ----------------------------------------------------------------------
@@ -525,9 +534,7 @@ static char *new_game_desc(game_params *params, random_state *rs,
                  case TYPE_OCTAGON:
                    cx = abs(x - w/2);
                    cy = abs(y - h/2);
-                   if (cx == 0 && cy == 0)
-                       v = GRID_HOLE;
-                   else if (cx + cy > 1 + max(w,h)/2)
+                   if (cx + cy > 1 + max(w,h)/2)
                        v = GRID_OBST;
                    else
                        v = GRID_PEG;
@@ -535,6 +542,107 @@ static char *new_game_desc(game_params *params, random_state *rs,
                }
                grid[y*w+x] = v;
            }
+
+       if (params->type == TYPE_OCTAGON) {
+           /*
+            * The octagonal (European) solitaire layout is
+            * actually _insoluble_ with the starting hole at the
+            * centre. Here's a proof:
+            * 
+            * Colour the squares of the board diagonally in
+            * stripes of three different colours, which I'll call
+            * A, B and C. So the board looks like this:
+            * 
+            *     A B C
+            *   A B C A B
+            * A B C A B C A
+            * B C A B C A B
+            * C A B C A B C
+            *   B C A B C
+            *     A B C
+            * 
+            * Suppose we keep running track of the number of pegs
+            * occuping each colour of square. This colouring has
+            * the property that any valid move whatsoever changes
+            * all three of those counts by one (two of them go
+            * down and one goes up), which means that the _parity_
+            * of every count flips on every move.
+            * 
+            * If the centre square starts off unoccupied, then
+            * there are twelve pegs on each colour and all three
+            * counts start off even; therefore, after 35 moves all
+            * three counts would have to be odd, which isn't
+            * possible if there's only one peg left. []
+            * 
+            * This proof works just as well if the starting hole
+            * is _any_ of the thirteen positions labelled B. Also,
+            * we can stripe the board in the opposite direction
+            * and rule out any square labelled B in that colouring
+            * as well. This leaves:
+            * 
+            *     Y n Y
+            *   n n Y n n
+            * Y n n Y n n Y
+            * n Y Y n Y Y n
+            * Y n n Y n n Y
+            *   n n Y n n
+            *     Y n Y
+            * 
+            * where the ns are squares we've proved insoluble, and
+            * the Ys are the ones remaining.
+            * 
+            * That doesn't prove all those starting positions to
+            * be soluble, of course; they're merely the ones we
+            * _haven't_ proved to be impossible. Nevertheless, it
+            * turns out that they are all soluble, so when the
+            * user requests an Octagon board the simplest thing is
+            * to pick one of these at random.
+            * 
+            * Rather than picking equiprobably from those twelve
+            * positions, we'll pick equiprobably from the three
+            * equivalence classes
+            */
+           switch (random_upto(rs, 3)) {
+             case 0:
+               /* Remove a random corner piece. */
+               {
+                   int dx, dy;
+
+                   dx = random_upto(rs, 2) * 2 - 1;   /* +1 or -1 */
+                   dy = random_upto(rs, 2) * 2 - 1;   /* +1 or -1 */
+                   if (random_upto(rs, 2))
+                       dy *= 3;
+                   else
+                       dx *= 3;
+                   grid[(3+dy)*w+(3+dx)] = GRID_HOLE;
+               }
+               break;
+             case 1:
+               /* Remove a random piece two from the centre. */
+               {
+                   int dx, dy;
+                   dx = 2 * (random_upto(rs, 2) * 2 - 1);
+                   if (random_upto(rs, 2))
+                       dy = 0;
+                   else
+                       dy = dx, dx = 0;
+                   grid[(3+dy)*w+(3+dx)] = GRID_HOLE;
+               }
+               break;
+             default /* case 2 */:
+               /* Remove a random piece one from the centre. */
+               {
+                   int dx, dy;
+                   dx = random_upto(rs, 2) * 2 - 1;
+                   if (random_upto(rs, 2))
+                       dy = 0;
+                   else
+                       dy = dx, dx = 0;
+                   grid[(3+dy)*w+(3+dx)] = GRID_HOLE;
+               }
+               break;
+           }
+       }
     }
 
     /*
@@ -564,7 +672,7 @@ static char *validate_desc(game_params *params, char *desc)
     return NULL;
 }
 
-static game_state *new_game(midend_data *me, game_params *params, char *desc)
+static game_state *new_game(midend *me, game_params *params, char *desc)
 {
     int w = params->w, h = params->h;
     game_state *state = snew(game_state);
@@ -572,6 +680,7 @@ static game_state *new_game(midend_data *me, game_params *params, char *desc)
 
     state->w = w;
     state->h = h;
+    state->completed = 0;
     state->grid = snewn(w*h, unsigned char);
     for (i = 0; i < w*h; i++)
        state->grid[i] = (desc[i] == 'P' ? GRID_PEG :
@@ -587,6 +696,7 @@ static game_state *dup_game(game_state *state)
 
     ret->w = state->w;
     ret->h = state->h;
+    ret->completed = state->completed;
     ret->grid = snewn(w*h, unsigned char);
     memcpy(ret->grid, state->grid, w*h);
 
@@ -605,6 +715,11 @@ static char *solve_game(game_state *state, game_state *currstate,
     return NULL;
 }
 
+static int game_can_format_as_text_now(game_params *params)
+{
+    return TRUE;
+}
+
 static char *game_text_format(game_state *state)
 {
     int w = state->w, h = state->h;
@@ -628,14 +743,30 @@ struct game_ui {
     int dragging;                     /* boolean: is a drag in progress? */
     int sx, sy;                               /* grid coords of drag start cell */
     int dx, dy;                               /* pixel coords of current drag posn */
+    int cur_x, cur_y, cur_visible, cur_jumping;
 };
 
 static game_ui *new_ui(game_state *state)
 {
     game_ui *ui = snew(game_ui);
+    int x, y, v;
 
     ui->sx = ui->sy = ui->dx = ui->dy = 0;
     ui->dragging = FALSE;
+    ui->cur_visible = ui->cur_jumping = 0;
+
+    /* make sure we start the cursor somewhere on the grid. */
+    for (x = 0; x < state->w; x++) {
+        for (y = 0; y < state->h; y++) {
+            v = state->grid[y*state->w+x];
+            if (v == GRID_PEG || v == GRID_HOLE) {
+                ui->cur_x = x; ui->cur_y = y;
+                goto found;
+            }
+        }
+    }
+    assert(!"new_ui found nowhere for cursor");
+found:
 
     return ui;
 }
@@ -680,12 +811,14 @@ struct game_drawstate {
     int w, h;
     unsigned char *grid;
     int started;
+    int bgcolour;
 };
 
-static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
+static char *interpret_move(game_state *state, game_ui *ui, const game_drawstate *ds,
                            int x, int y, int button)
 {
     int w = state->w, h = state->h;
+    char buf[80];
 
     if (button == LEFT_BUTTON) {
        int tx, ty;
@@ -712,6 +845,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
            ui->sy = ty;
            ui->dx = x;
            ui->dy = y;
+            ui->cur_visible = ui->cur_jumping = 0;
            return "";                 /* ui modified */
        }
     } else if (button == LEFT_DRAG && ui->dragging) {
@@ -722,7 +856,6 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
        ui->dy = y;
        return "";                     /* ui modified */
     } else if (button == LEFT_RELEASE && ui->dragging) {
-       char buf[80];
        int tx, ty, dx, dy;
 
        /*
@@ -752,7 +885,60 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
         */
        sprintf(buf, "%d,%d-%d,%d", ui->sx, ui->sy, tx, ty);
        return dupstr(buf);
+    } else if (IS_CURSOR_MOVE(button)) {
+        if (!ui->cur_jumping) {
+            /* Not jumping; move cursor as usual, making sure we don't
+             * leave the gameboard (which may be an irregular shape) */
+            int cx = ui->cur_x, cy = ui->cur_y;
+            move_cursor(button, &cx, &cy, w, h, 0);
+            ui->cur_visible = 1;
+            if (state->grid[cy*w+cx] == GRID_HOLE ||
+                state->grid[cy*w+cx] == GRID_PEG) {
+                ui->cur_x = cx;
+                ui->cur_y = cy;
+            }
+            return "";
+        } else {
+            int dx, dy, mx, my, jx, jy;
+
+            /* We're jumping; if the requested direction has a hole, and
+             * there's a peg in the way, */
+            assert(state->grid[ui->cur_y*w+ui->cur_x] == GRID_PEG);
+            dx = (button == CURSOR_RIGHT) ? 1 : (button == CURSOR_LEFT) ? -1 : 0;
+            dy = (button == CURSOR_DOWN) ? 1 : (button == CURSOR_UP) ? -1 : 0;
+
+            mx = ui->cur_x+dx; my = ui->cur_y+dy;
+            jx = mx+dx; jy = my+dy;
+
+            ui->cur_jumping = 0; /* reset, whatever. */
+            if (jx >= 0 && jy >= 0 && jx < w && jy < h &&
+                state->grid[my*w+mx] == GRID_PEG &&
+                state->grid[jy*w+jx] == GRID_HOLE) {
+                /* Move cursor to the jumped-to location (this felt more
+                 * natural while playtesting) */
+                sprintf(buf, "%d,%d-%d,%d", ui->cur_x, ui->cur_y, jx, jy);
+                ui->cur_x = jx; ui->cur_y = jy;
+                return dupstr(buf);
+            }
+            return "";
+        }
+    } else if (IS_CURSOR_SELECT(button)) {
+        if (!ui->cur_visible) {
+            ui->cur_visible = 1;
+            return "";
+        }
+        if (ui->cur_jumping) {
+            ui->cur_jumping = 0;
+            return "";
+        }
+        if (state->grid[ui->cur_y*w+ui->cur_x] == GRID_PEG) {
+            /* cursor is on peg: next arrow-move wil jump. */
+            ui->cur_jumping = 1;
+            return "";
+        }
+        return NULL;
     }
+
     return NULL;
 }
 
@@ -762,7 +948,7 @@ static game_state *execute_move(game_state *state, char *move)
     int sx, sy, tx, ty;
     game_state *ret;
 
-    if (sscanf(move, "%d,%d-%d,%d", &sx, &sy, &tx, &ty)) {
+    if (sscanf(move, "%d,%d-%d,%d", &sx, &sy, &tx, &ty) == 4) {
        int mx, my, dx, dy;
 
        if (sx < 0 || sx >= w || sy < 0 || sy >= h)
@@ -787,6 +973,21 @@ static game_state *execute_move(game_state *state, char *move)
        ret->grid[my*w+mx] = GRID_HOLE;
        ret->grid[ty*w+tx] = GRID_PEG;
 
+        /*
+         * Opinion varies on whether getting to a single peg counts as
+         * completing the game, or whether that peg has to be at a
+         * specific location (central in the classic cross game, for
+         * instance). For now we take the former, rather lax position.
+         */
+        if (!ret->completed) {
+            int count = 0, i;
+            for (i = 0; i < w*h; i++)
+                if (ret->grid[i] == GRID_PEG)
+                    count++;
+            if (count == 1)
+                ret->completed = 1;
+        }
+
        return ret;
     }
     return NULL;
@@ -796,66 +997,47 @@ static game_state *execute_move(game_state *state, char *move)
  * Drawing routines.
  */
 
-static void game_size(game_params *params, game_drawstate *ds,
-                      int *x, int *y, int expand)
+static void game_compute_size(game_params *params, int tilesize,
+                             int *x, int *y)
 {
-    double tsx, tsy, ts;
-    /*
-     * Each window dimension equals the tile size times one more
-     * than the grid dimension (the border is half the width of the
-     * tiles).
-     */
-    tsx = (double)*x / ((double)params->w + 1.0);
-    tsy = (double)*y / ((double)params->h + 1.0);
-    ts = min(tsx, tsy);
-    if (expand)
-        ds->tilesize = (int)(ts + 0.5);
-    else
-        ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE);
+    /* Ick: fake up `ds->tilesize' for macro expansion purposes */
+    struct { int tilesize; } ads, *ds = &ads;
+    ads.tilesize = tilesize;
 
     *x = TILESIZE * params->w + 2 * BORDER;
     *y = TILESIZE * params->h + 2 * BORDER;
-
-    if (ds->drag_background)
-       blitter_free(ds->drag_background);
-    ds->drag_background = blitter_new(TILESIZE, TILESIZE);
 }
 
-static float *game_colours(frontend *fe, game_state *state, int *ncolours)
+static void game_set_size(drawing *dr, game_drawstate *ds,
+                         game_params *params, int tilesize)
 {
-    float *ret = snewn(3 * NCOLOURS, float);
-    int i;
-    float max;
+    ds->tilesize = tilesize;
 
-    frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
+    assert(TILESIZE > 0);
 
-    /*
-     * Drop the background colour so that the highlight is
-     * noticeably brighter than it while still being under 1.
-     */
-    max = ret[COL_BACKGROUND*3];
-    for (i = 1; i < 3; i++)
-        if (ret[COL_BACKGROUND*3+i] > max)
-            max = ret[COL_BACKGROUND*3+i];
-    if (max * 1.2F > 1.0F) {
-        for (i = 0; i < 3; i++)
-            ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
-    }
+    assert(!ds->drag_background);      /* set_size is never called twice */
+    ds->drag_background = blitter_new(dr, TILESIZE, TILESIZE);
+}
 
-    for (i = 0; i < 3; i++) {
-        ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
-        ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
-    }
+static float *game_colours(frontend *fe, int *ncolours)
+{
+    float *ret = snewn(3 * NCOLOURS, float);
+
+    game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
 
     ret[COL_PEG * 3 + 0] = 0.0F;
     ret[COL_PEG * 3 + 1] = 0.0F;
     ret[COL_PEG * 3 + 2] = 1.0F;
 
+    ret[COL_CURSOR * 3 + 0] = 0.5F;
+    ret[COL_CURSOR * 3 + 1] = 0.5F;
+    ret[COL_CURSOR * 3 + 2] = 1.0F;
+
     *ncolours = NCOLOURS;
     return ret;
 }
 
-static game_drawstate *game_new_drawstate(game_state *state)
+static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
 {
     int w = state->w, h = state->h;
     struct game_drawstate *ds = snew(struct game_drawstate);
@@ -873,55 +1055,77 @@ static game_drawstate *game_new_drawstate(game_state *state)
     memset(ds->grid, 255, w*h);
 
     ds->started = FALSE;
+    ds->bgcolour = -1;
 
     return ds;
 }
 
-static void game_free_drawstate(game_drawstate *ds)
+static void game_free_drawstate(drawing *dr, game_drawstate *ds)
 {
     if (ds->drag_background)
-       blitter_free(ds->drag_background);
+       blitter_free(dr, ds->drag_background);
     sfree(ds->grid);
     sfree(ds);
 }
 
-static void draw_tile(frontend *fe, game_drawstate *ds,
-                     int x, int y, int v, int erasebg)
+static void draw_tile(drawing *dr, game_drawstate *ds,
+                     int x, int y, int v, int bgcolour)
 {
-    if (erasebg) {
-       draw_rect(fe, x, y, TILESIZE, TILESIZE, COL_BACKGROUND);
+    int cursor = 0, jumping = 0, bg;
+
+    if (bgcolour >= 0) {
+       draw_rect(dr, x, y, TILESIZE, TILESIZE, bgcolour);
+    }
+    if (v >= GRID_JUMPING) {
+        jumping = 1; v -= GRID_JUMPING;
+    }
+    if (v >= GRID_CURSOR) {
+        cursor = 1; v -= GRID_CURSOR;
     }
 
     if (v == GRID_HOLE) {
-       draw_circle(fe, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/4,
-                   COL_LOWLIGHT, COL_LOWLIGHT);
+        bg = cursor ? COL_HIGHLIGHT : COL_LOWLIGHT;
+        assert(!jumping); /* can't jump from a hole! */
+       draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/4,
+                    bg, bg);
     } else if (v == GRID_PEG) {
-       draw_circle(fe, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/3,
-                   COL_PEG, COL_PEG);
+        bg = (cursor || jumping) ? COL_CURSOR : COL_PEG;
+       draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/3,
+                   bg, bg);
+        bg = (!cursor || jumping) ? COL_PEG : COL_CURSOR;
+        draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/4,
+                    bg, bg);
     }
 
-    draw_update(fe, x, y, TILESIZE, TILESIZE);
+    draw_update(dr, x, y, TILESIZE, TILESIZE);
 }
 
-static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
+static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
                        game_state *state, int dir, game_ui *ui,
                        float animtime, float flashtime)
 {
     int w = state->w, h = state->h;
     int x, y;
+    int bgcolour;
+
+    if (flashtime > 0) {
+        int frame = (int)(flashtime / FLASH_FRAME);
+        bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
+    } else
+        bgcolour = COL_BACKGROUND;
 
     /*
      * Erase the sprite currently being dragged, if any.
      */
     if (ds->dragging) {
        assert(ds->drag_background);
-        blitter_load(fe, ds->drag_background, ds->dragx, ds->dragy);
-        draw_update(fe, ds->dragx, ds->dragy, TILESIZE, TILESIZE);
+        blitter_load(dr, ds->drag_background, ds->dragx, ds->dragy);
+        draw_update(dr, ds->dragx, ds->dragy, TILESIZE, TILESIZE);
        ds->dragging = FALSE;
     }
 
     if (!ds->started) {
-       draw_rect(fe, 0, 0,
+       draw_rect(dr, 0, 0,
                  TILESIZE * state->w + 2 * BORDER,
                  TILESIZE * state->h + 2 * BORDER, COL_BACKGROUND);
 
@@ -942,10 +1146,10 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
                    coords[3] = COORD(y+1) + HIGHLIGHT_WIDTH - 1;
                    coords[4] = COORD(x) - HIGHLIGHT_WIDTH;
                    coords[5] = COORD(y) - HIGHLIGHT_WIDTH;
-                   draw_polygon(fe, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
+                   draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
                    coords[4] = COORD(x+1) + HIGHLIGHT_WIDTH - 1;
                    coords[5] = COORD(y+1) + HIGHLIGHT_WIDTH - 1;
-                   draw_polygon(fe, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT);
+                   draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT);
                }
        for (y = 0; y < h; y++)
            for (x = 0; x < w; x++)
@@ -954,11 +1158,11 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
                     * Second pass: draw everything but the two
                     * diagonal corners.
                     */
-                   draw_rect(fe, COORD(x) - HIGHLIGHT_WIDTH,
+                   draw_rect(dr, COORD(x) - HIGHLIGHT_WIDTH,
                              COORD(y) - HIGHLIGHT_WIDTH,
                              TILESIZE + HIGHLIGHT_WIDTH,
                              TILESIZE + HIGHLIGHT_WIDTH, COL_HIGHLIGHT);
-                   draw_rect(fe, COORD(x),
+                   draw_rect(dr, COORD(x),
                              COORD(y),
                              TILESIZE + HIGHLIGHT_WIDTH,
                              TILESIZE + HIGHLIGHT_WIDTH, COL_LOWLIGHT);
@@ -986,7 +1190,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
                            coords[5] = coords[3] - HIGHLIGHT_WIDTH * (dx-sn*dy);
                            coords[6] = coords[0] + HIGHLIGHT_WIDTH * (dy+sn*dx);
                            coords[7] = coords[1] + HIGHLIGHT_WIDTH * (dx+sn*dy);
-                           draw_polygon(fe, coords, 4, c, c);
+                           draw_polygon(dr, coords, 4, c, c);
                        }
                    }
                }
@@ -997,7 +1201,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
                     * Second pass: draw everything but the two
                     * diagonal corners.
                     */
-                   draw_rect(fe, COORD(x),
+                   draw_rect(dr, COORD(x),
                              COORD(y),
                              TILESIZE,
                              TILESIZE, COL_BACKGROUND);
@@ -1005,7 +1209,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
 
        ds->started = TRUE;
 
-       draw_update(fe, 0, 0,
+       draw_update(dr, 0, 0,
                    TILESIZE * state->w + 2 * BORDER,
                    TILESIZE * state->h + 2 * BORDER);
     }
@@ -1025,8 +1229,15 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
             */
            if (ui->dragging && ui->sx == x && ui->sy == y && v == GRID_PEG)
                v = GRID_HOLE;
-           if (v != ds->grid[y*w+x] && v != GRID_OBST) {
-               draw_tile(fe, ds, COORD(x), COORD(y), v, TRUE);
+
+            if (ui->cur_visible && ui->cur_x == x && ui->cur_y == y)
+                v += ui->cur_jumping ? GRID_JUMPING : GRID_CURSOR;
+
+           if (v != GRID_OBST &&
+                (bgcolour != ds->bgcolour || /* always redraw when flashing */
+                 v != ds->grid[y*w+x])) {
+               draw_tile(dr, ds, COORD(x), COORD(y), v, bgcolour);
+                ds->grid[y*w+x] = v;
            }
        }
 
@@ -1037,9 +1248,11 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
        ds->dragging = TRUE;
        ds->dragx = ui->dx - TILESIZE/2;
        ds->dragy = ui->dy - TILESIZE/2;
-       blitter_save(fe, ds->drag_background, ds->dragx, ds->dragy);
-       draw_tile(fe, ds, ds->dragx, ds->dragy, GRID_PEG, FALSE);
+       blitter_save(dr, ds->drag_background, ds->dragx, ds->dragy);
+       draw_tile(dr, ds, ds->dragx, ds->dragy, GRID_PEG, -1);
     }
+
+    ds->bgcolour = bgcolour;
 }
 
 static float game_anim_length(game_state *oldstate, game_state *newstate,
@@ -1051,25 +1264,40 @@ static float game_anim_length(game_state *oldstate, game_state *newstate,
 static float game_flash_length(game_state *oldstate, game_state *newstate,
                               int dir, game_ui *ui)
 {
-    return 0.0F;
+    if (!oldstate->completed && newstate->completed)
+        return 2 * FLASH_FRAME;
+    else
+        return 0.0F;
 }
 
-static int game_wants_statusbar(void)
+static int game_status(game_state *state)
 {
-    return FALSE;
+    /*
+     * Dead-end situations are assumed to be rescuable by Undo, so we
+     * don't bother to identify them and return -1.
+     */
+    return state->completed ? +1 : 0;
 }
 
-static int game_timing_state(game_state *state)
+static int game_timing_state(game_state *state, game_ui *ui)
 {
     return TRUE;
 }
 
+static void game_print_size(game_params *params, float *x, float *y)
+{
+}
+
+static void game_print(drawing *dr, game_state *state, int tilesize)
+{
+}
+
 #ifdef COMBINED
 #define thegame pegs
 #endif
 
 const struct game thegame = {
-    "Pegs", NULL,
+    "Pegs", "games.pegs", "pegs",
     default_params,
     game_fetch_preset,
     decode_params,
@@ -1084,7 +1312,7 @@ const struct game thegame = {
     dup_game,
     free_game,
     FALSE, solve_game,
-    TRUE, game_text_format,
+    TRUE, game_can_format_as_text_now, game_text_format,
     new_ui,
     free_ui,
     encode_ui,
@@ -1092,14 +1320,18 @@ const struct game thegame = {
     game_changed_state,
     interpret_move,
     execute_move,
-    game_size,
+    PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
     game_colours,
     game_new_drawstate,
     game_free_drawstate,
     game_redraw,
     game_anim_length,
     game_flash_length,
-    game_wants_statusbar,
+    game_status,
+    FALSE, FALSE, game_print_size, game_print,
+    FALSE,                            /* wants_statusbar */
     FALSE, game_timing_state,
-    0,                                /* mouse_priorities */
+    0,                                /* flags */
 };
+
+/* vim: set shiftwidth=4 tabstop=8: */