chiark / gitweb /
changelog: document last change
[sgt-puzzles.git] / slant.c
diff --git a/slant.c b/slant.c
index 6c540bad4f04233081e922c814a76df6bf9e326b..e45a2ea851c46f0fdd31641578f1b9ff0b19361c 100644 (file)
--- a/slant.c
+++ b/slant.c
@@ -39,6 +39,8 @@ enum {
     COL_SLANT1,
     COL_SLANT2,
     COL_ERROR,
+    COL_CURSOR,
+    COL_FILLEDSQUARE,
     NCOLOURS
 };
 
@@ -83,7 +85,6 @@ typedef struct game_clues {
 
 #define ERR_VERTEX 1
 #define ERR_SQUARE 2
-#define ERR_SQUARE_TMP 4
 
 struct game_state {
     struct game_params p;
@@ -136,7 +137,7 @@ static void free_params(game_params *params)
     sfree(params);
 }
 
-static game_params *dup_params(game_params *params)
+static game_params *dup_params(const game_params *params)
 {
     game_params *ret = snew(game_params);
     *ret = *params;                   /* structure copy */
@@ -162,7 +163,7 @@ static void decode_params(game_params *ret, char const *string)
     }
 }
 
-static char *encode_params(game_params *params, int full)
+static char *encode_params(const game_params *params, int full)
 {
     char data[256];
 
@@ -173,7 +174,7 @@ static char *encode_params(game_params *params, int full)
     return dupstr(data);
 }
 
-static config_item *game_configure(game_params *params)
+static config_item *game_configure(const game_params *params)
 {
     config_item *ret;
     char buf[80];
@@ -205,7 +206,7 @@ static config_item *game_configure(game_params *params)
     return ret;
 }
 
-static game_params *custom_params(config_item *cfg)
+static game_params *custom_params(const config_item *cfg)
 {
     game_params *ret = snew(game_params);
 
@@ -216,7 +217,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params, int full)
+static char *validate_params(const game_params *params, int full)
 {
     /*
      * (At least at the time of writing this comment) The grid
@@ -468,15 +469,13 @@ static int slant_solve(int w, int h, const signed char *clues,
      * Establish a disjoint set forest for tracking connectedness
      * between grid points.
      */
-    for (i = 0; i < W*H; i++)
-       sc->connected[i] = i;          /* initially all distinct */
+    dsf_init(sc->connected, W*H);
 
     /*
      * Establish a disjoint set forest for tracking which squares
      * are known to slant in the same direction.
      */
-    for (i = 0; i < w*h; i++)
-       sc->equiv[i] = i;              /* initially all distinct */
+    dsf_init(sc->equiv, w*h);
 
     /*
      * Clear the slashval array.
@@ -1006,9 +1005,7 @@ static void slant_generate(int w, int h, signed char *soln, random_state *rs)
      * Establish a disjoint set forest for tracking connectedness
      * between grid points.
      */
-    connected = snewn(W*H, int);
-    for (i = 0; i < W*H; i++)
-       connected[i] = i;                      /* initially all distinct */
+    connected = snew_dsf(W*H);
 
     /*
      * Prepare a list of the squares in the grid, and fill them in
@@ -1066,7 +1063,7 @@ static void slant_generate(int w, int h, signed char *soln, random_state *rs)
     sfree(connected);
 }
 
-static char *new_game_desc(game_params *params, random_state *rs,
+static char *new_game_desc(const game_params *params, random_state *rs,
                           char **aux, int interactive)
 {
     int w = params->w, h = params->h, W = w+1, H = h+1;
@@ -1219,7 +1216,7 @@ static char *new_game_desc(game_params *params, random_state *rs,
     return desc;
 }
 
-static char *validate_desc(game_params *params, char *desc)
+static char *validate_desc(const game_params *params, const char *desc)
 {
     int w = params->w, h = params->h, W = w+1, H = h+1;
     int area = W*H;
@@ -1244,7 +1241,8 @@ static char *validate_desc(game_params *params, char *desc)
     return NULL;
 }
 
-static game_state *new_game(midend *me, game_params *params, char *desc)
+static game_state *new_game(midend *me, const game_params *params,
+                            const char *desc)
 {
     int w = params->w, h = params->h, W = w+1, H = h+1;
     game_state *state = snew(game_state);
@@ -1263,7 +1261,7 @@ static game_state *new_game(midend *me, game_params *params, char *desc)
     state->clues->h = h;
     state->clues->clues = snewn(W*H, signed char);
     state->clues->refcount = 1;
-    state->clues->tmpdsf = snewn(W*H, int);
+    state->clues->tmpdsf = snewn(W*H*2+W+H, int);
     memset(state->clues->clues, -1, W*H);
     while (*desc) {
         int n = *desc++;
@@ -1279,7 +1277,7 @@ static game_state *new_game(midend *me, game_params *params, char *desc)
     return state;
 }
 
-static game_state *dup_game(game_state *state)
+static game_state *dup_game(const game_state *state)
 {
     int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
     game_state *ret = snew(game_state);
@@ -1354,165 +1352,71 @@ static int vertex_degree(int w, int h, signed char *soln, int x, int y,
     return anti ? 4 - ret : ret;
 }
 
+struct slant_neighbour_ctx {
+    const game_state *state;
+    int i, n, neighbours[4];
+};
+static int slant_neighbour(int vertex, void *vctx)
+{
+    struct slant_neighbour_ctx *ctx = (struct slant_neighbour_ctx *)vctx;
+
+    if (vertex >= 0) {
+        int w = ctx->state->p.w, h = ctx->state->p.h, W = w+1;
+        int x = vertex % W, y = vertex / W;
+        ctx->n = ctx->i = 0;
+        if (x < w && y < h && ctx->state->soln[y*w+x] < 0)
+            ctx->neighbours[ctx->n++] = (y+1)*W+(x+1);
+        if (x > 0 && y > 0 && ctx->state->soln[(y-1)*w+(x-1)] < 0)
+            ctx->neighbours[ctx->n++] = (y-1)*W+(x-1);
+        if (x > 0 && y < h && ctx->state->soln[y*w+(x-1)] > 0)
+            ctx->neighbours[ctx->n++] = (y+1)*W+(x-1);
+        if (x < w && y > 0 && ctx->state->soln[(y-1)*w+x] > 0)
+            ctx->neighbours[ctx->n++] = (y-1)*W+(x+1);
+    }
+
+    if (ctx->i < ctx->n)
+        return ctx->neighbours[ctx->i++];
+    else
+        return -1;
+}
+
 static int check_completion(game_state *state)
 {
     int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
-    int i, x, y, err = FALSE;
-    int *dsf;
+    int x, y, err = FALSE;
 
     memset(state->errors, 0, W*H);
 
     /*
-     * To detect loops in the grid, we iterate through each edge
-     * building up a dsf of connected components, and raise the
-     * alarm whenever we find an edge that connects two
-     * already-connected vertices.
-     * 
-     * We use the `tmpdsf' scratch space in the shared clues
-     * structure, to avoid mallocing too often.
-     * 
-     * When we find such an edge, we then search around the grid to
-     * find the loop it is a part of, so that we can highlight it
-     * as an error for the user. We do this by the hand-on-one-wall
-     * technique: the search will follow branches off the inside of
-     * the loop, discover they're dead ends, and unhighlight them
-     * again when returning to the actual loop.
-     * 
-     * This technique guarantees that every loop it tracks will
-     * surround a disjoint area of the grid (since if an existing
-     * loop appears on the boundary of a new one, so that there are
-     * multiple possible paths that would come back to the starting
-     * point, it will pick the one that allows it to turn right
-     * most sharply and hence the one that does not re-surround the
-     * area of the previous one). Thus, the total time taken in
-     * searching round loops is linear in the grid area since every
-     * edge is visited at most twice.
+     * Detect and error-highlight loops in the grid.
      */
-    dsf = state->clues->tmpdsf;
-    for (i = 0; i < W*H; i++)
-        dsf[i] = i;                   /* initially all distinct */
-    for (y = 0; y < h; y++)
-        for (x = 0; x < w; x++) {
-            int i1, i2;
-
-            if (state->soln[y*w+x] == 0)
-                continue;
-            if (state->soln[y*w+x] < 0) {
-                i1 = y*W+x;
-                i2 = (y+1)*W+(x+1);
-            } else {
-                i1 = y*W+(x+1);
-                i2 = (y+1)*W+x;
-            }
-
-            /*
-             * Our edge connects i1 with i2. If they're already
-             * connected, flag an error. Otherwise, link them.
-             */
-            if (dsf_canonify(dsf, i1) == dsf_canonify(dsf, i2)) {
-               int x1, y1, x2, y2, dx, dy, dt, pass;
-
-               err = TRUE;
-
-               /*
-                * Now search around the boundary of the loop to
-                * highlight it.
-                * 
-                * We have to do this in two passes. The first
-                * time, we toggle ERR_SQUARE_TMP on each edge;
-                * this pass terminates with ERR_SQUARE_TMP set on
-                * exactly the loop edges. In the second pass, we
-                * trace round that loop again and turn
-                * ERR_SQUARE_TMP into ERR_SQUARE. We have to do
-                * this because otherwise we might cancel part of a
-                * loop highlighted in a previous iteration of the
-                * outer loop.
-                */
-
-               for (pass = 0; pass < 2; pass++) {
-
-                   x1 = i1 % W;
-                   y1 = i1 / W;
-                   x2 = i2 % W;
-                   y2 = i2 / W;
-
-                   do {
-                       /* Mark this edge. */
-                       if (pass == 0) {
-                           state->errors[min(y1,y2)*W+min(x1,x2)] ^=
-                               ERR_SQUARE_TMP;
-                       } else {
-                           state->errors[min(y1,y2)*W+min(x1,x2)] |=
-                               ERR_SQUARE;
-                           state->errors[min(y1,y2)*W+min(x1,x2)] &=
-                               ~ERR_SQUARE_TMP;
-                       }
-
-                       /*
-                        * Progress to the next edge by turning as
-                        * sharply right as possible. In fact we do
-                        * this by facing back along the edge and
-                        * turning _left_ until we see an edge we
-                        * can follow.
-                        */
-                       dx = x1 - x2;
-                       dy = y1 - y2;
-
-                       for (i = 0; i < 4; i++) {
-                           /*
-                            * Rotate (dx,dy) to the left.
-                            */
-                           dt = dx; dx = dy; dy = -dt;
-
-                           /*
-                            * See if (x2,y2) has an edge in direction
-                            * (dx,dy).
-                            */
-                           if (x2+dx < 0 || x2+dx >= W ||
-                               y2+dy < 0 || y2+dy >= H)
-                               continue;  /* off the side of the grid */
-                           /* In the second pass, ignore unmarked edges. */
-                           if (pass == 1 &&
-                               !(state->errors[(y2-(dy<0))*W+x2-(dx<0)] &
-                                 ERR_SQUARE_TMP))
-                               continue;
-                           if (state->soln[(y2-(dy<0))*w+x2-(dx<0)] ==
-                               (dx==dy ? -1 : +1))
-                               break;
-                       }
-
-                       /*
-                        * In pass 0, we expect to have found
-                        * _some_ edge we can follow, even if it
-                        * was found by rotating all the way round
-                        * and going back the way we came.
-                        * 
-                        * In pass 1, because we're removing the
-                        * mark on each edge that allows us to
-                        * follow it, we expect to find _no_ edge
-                        * we can follow when we've come all the
-                        * way round the loop.
-                        */
-                       if (pass == 1 && i == 4)
-                           break;
-                       assert(i < 4);
-
-                       /*
-                        * Set x1,y1 to x2,y2, and x2,y2 to be the
-                        * other end of the new edge.
-                        */
-                       x1 = x2;
-                       y1 = y2;
-                       x2 += dx;
-                       y2 += dy;
-                   } while (y2*W+x2 != i2);
-
-               }
-               
-           } else
-                dsf_merge(dsf, i1, i2);
+    {
+        struct findloopstate *fls = findloop_new_state(W*H);
+        struct slant_neighbour_ctx ctx;
+        ctx.state = state;
+
+        if (findloop_run(fls, W*H, slant_neighbour, &ctx))
+            err = TRUE;
+        for (y = 0; y < h; y++) {
+            for (x = 0; x < w; x++) {
+                int u, v;
+                if (state->soln[y*w+x] == 0) {
+                    continue;
+                } else if (state->soln[y*w+x] > 0) {
+                    u = y*W+(x+1);
+                    v = (y+1)*W+x;
+                } else {
+                    u = (y+1)*W+(x+1);
+                    v = y*W+x;
+                }
+                if (findloop_is_loop_edge(fls, u, v))
+                    state->errors[y*W+x] |= ERR_SQUARE;
+           }
         }
 
+        findloop_free_state(fls);
+    }
+
     /*
      * Now go through and check the degree of each clue vertex, and
      * mark it with ERR_VERTEX if it cannot be fulfilled.
@@ -1555,8 +1459,8 @@ static int check_completion(game_state *state)
     return TRUE;
 }
 
-static char *solve_game(game_state *state, game_state *currstate,
-                       char *aux, char **error)
+static char *solve_game(const game_state *state, const game_state *currstate,
+                        const char *aux, char **error)
 {
     int w = state->p.w, h = state->p.h;
     signed char *soln;
@@ -1620,7 +1524,12 @@ static char *solve_game(game_state *state, game_state *currstate,
     return move;
 }
 
-static char *game_text_format(game_state *state)
+static int game_can_format_as_text_now(const game_params *params)
+{
+    return TRUE;
+}
+
+static char *game_text_format(const game_state *state)
 {
     int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
     int x, y, len;
@@ -1662,26 +1571,33 @@ static char *game_text_format(game_state *state)
     return ret;
 }
 
-static game_ui *new_ui(game_state *state)
+struct game_ui {
+    int cur_x, cur_y, cur_visible;
+};
+
+static game_ui *new_ui(const game_state *state)
 {
-    return NULL;
+    game_ui *ui = snew(game_ui);
+    ui->cur_x = ui->cur_y = ui->cur_visible = 0;
+    return ui;
 }
 
 static void free_ui(game_ui *ui)
 {
+    sfree(ui);
 }
 
-static char *encode_ui(game_ui *ui)
+static char *encode_ui(const game_ui *ui)
 {
     return NULL;
 }
 
-static void decode_ui(game_ui *ui, char *encoding)
+static void decode_ui(game_ui *ui, const char *encoding)
 {
 }
 
-static void game_changed_state(game_ui *ui, game_state *oldstate,
-                               game_state *newstate)
+static void game_changed_state(game_ui *ui, const game_state *oldstate,
+                               const game_state *newstate)
 {
 }
 
@@ -1716,6 +1632,7 @@ static void game_changed_state(game_ui *ui, game_state *oldstate,
 #define ERR_TR    0x00008000L
 #define ERR_BL    0x00010000L
 #define ERR_BR    0x00020000L
+#define CURSOR    0x00040000L
 
 struct game_drawstate {
     int tilesize;
@@ -1724,15 +1641,16 @@ struct game_drawstate {
     long *todraw;
 };
 
-static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
-                           int x, int y, int button)
+static char *interpret_move(const game_state *state, game_ui *ui,
+                            const game_drawstate *ds,
+                            int x, int y, int button)
 {
     int w = state->p.w, h = state->p.h;
+    int v;
+    char buf[80];
+    enum { CLOCKWISE, ANTICLOCKWISE, NONE } action = NONE;
 
     if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
-        int v;
-        char buf[80];
-
        /*
         * This is an utterly awful hack which I should really sort out
         * by means of a proper configuration mechanism. One Slant
@@ -1755,13 +1673,35 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
                    button = LEFT_BUTTON;
            }
        }
+        action = (button == LEFT_BUTTON) ? CLOCKWISE : ANTICLOCKWISE;
 
         x = FROMCOORD(x);
         y = FROMCOORD(y);
         if (x < 0 || y < 0 || x >= w || y >= h)
             return NULL;
+        ui->cur_visible = 0;
+    } else if (IS_CURSOR_SELECT(button)) {
+        if (!ui->cur_visible) {
+            ui->cur_visible = 1;
+            return "";
+        }
+        x = ui->cur_x;
+        y = ui->cur_y;
+
+        action = (button == CURSOR_SELECT2) ? ANTICLOCKWISE : CLOCKWISE;
+    } else if (IS_CURSOR_MOVE(button)) {
+        move_cursor(button, &ui->cur_x, &ui->cur_y, w, h, 0);
+        ui->cur_visible = 1;
+        return "";
+    } else if (button == '\\' || button == '\b' || button == '/') {
+       int x = ui->cur_x, y = ui->cur_y;
+       if (button == ("\\" "\b" "/")[state->soln[y*w + x] + 1]) return NULL;
+       sprintf(buf, "%c%d,%d", button == '\b' ? 'C' : button, x, y);
+       return dupstr(buf);
+    }
 
-        if (button == LEFT_BUTTON) {
+    if (action != NONE) {
+        if (action == CLOCKWISE) {
             /*
              * Left-clicking cycles blank -> \ -> / -> blank.
              */
@@ -1784,7 +1724,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
     return NULL;
 }
 
-static game_state *execute_move(game_state *state, char *move)
+static game_state *execute_move(const game_state *state, const char *move)
 {
     int w = state->p.w, h = state->p.h;
     char c;
@@ -1831,18 +1771,19 @@ static game_state *execute_move(game_state *state, char *move)
  * Drawing routines.
  */
 
-static void game_compute_size(game_params *params, int tilesize,
-                             int *x, int *y)
+static void game_compute_size(const game_params *params, int tilesize,
+                              int *x, int *y)
 {
     /* fool the macros */
-    struct dummy { int tilesize; } dummy = { tilesize }, *ds = &dummy;
+    struct dummy { int tilesize; } dummy, *ds = &dummy;
+    dummy.tilesize = tilesize;
 
     *x = 2 * BORDER + params->w * TILESIZE + 1;
     *y = 2 * BORDER + params->h * TILESIZE + 1;
 }
 
 static void game_set_size(drawing *dr, game_drawstate *ds,
-                         game_params *params, int tilesize)
+                          const game_params *params, int tilesize)
 {
     ds->tilesize = tilesize;
 }
@@ -1851,7 +1792,8 @@ static float *game_colours(frontend *fe, int *ncolours)
 {
     float *ret = snewn(3 * NCOLOURS, float);
 
-    frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
+    /* CURSOR colour is a background highlight. */
+    game_mkhighlight(fe, ret, COL_BACKGROUND, COL_CURSOR, COL_FILLEDSQUARE);
 
     ret[COL_GRID * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.7F;
     ret[COL_GRID * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.7F;
@@ -1877,7 +1819,7 @@ static float *game_colours(frontend *fe, int *ncolours)
     return ret;
 }
 
-static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
+static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
 {
     int w = state->p.w, h = state->p.h;
     int i;
@@ -1910,7 +1852,7 @@ static void draw_clue(drawing *dr, game_drawstate *ds,
     if (v < 0)
        return;
 
-    p[0] = v + '0';
+    p[0] = (char)v + '0';
     p[1] = '\0';
     draw_circle(dr, COORD(x), COORD(y), CLUE_RADIUS,
                bg >= 0 ? bg : COL_BACKGROUND, ccol);
@@ -1929,7 +1871,10 @@ static void draw_tile(drawing *dr, game_drawstate *ds, game_clues *clues,
     clip(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
 
     draw_rect(dr, COORD(x), COORD(y), TILESIZE, TILESIZE,
-             (v & FLASH) ? COL_GRID : COL_BACKGROUND);
+             (v & FLASH) ? COL_GRID :
+              (v & CURSOR) ? COL_CURSOR :
+             (v & (BACKSLASH | FORWSLASH)) ? COL_FILLEDSQUARE :
+             COL_BACKGROUND);
 
     /*
      * Draw the grid lines.
@@ -2007,9 +1952,10 @@ static void draw_tile(drawing *dr, game_drawstate *ds, game_clues *clues,
     draw_update(dr, COORD(x), COORD(y), TILESIZE, TILESIZE);
 }
 
-static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
-                       game_state *state, int dir, game_ui *ui,
-                       float animtime, float flashtime)
+static void game_redraw(drawing *dr, game_drawstate *ds,
+                        const game_state *oldstate, const game_state *state,
+                        int dir, const game_ui *ui,
+                        float animtime, float flashtime)
 {
     int w = state->p.w, h = state->p.h, W = w+1, H = h+1;
     int x, y;
@@ -2068,6 +2014,8 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
                     ds->todraw[(y+2)*(w+2)+(x+1)] |= ERR_T_L | ERR_C_TL;
                 }
            }
+            if (ui->cur_visible && ui->cur_x == x && ui->cur_y == y)
+                ds->todraw[(y+1)*(w+2)+(x+1)] |= CURSOR;
        }
     }
 
@@ -2094,14 +2042,14 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
     }
 }
 
-static float game_anim_length(game_state *oldstate, game_state *newstate,
-                             int dir, game_ui *ui)
+static float game_anim_length(const game_state *oldstate,
+                              const game_state *newstate, int dir, game_ui *ui)
 {
     return 0.0F;
 }
 
-static float game_flash_length(game_state *oldstate, game_state *newstate,
-                              int dir, game_ui *ui)
+static float game_flash_length(const game_state *oldstate,
+                               const game_state *newstate, int dir, game_ui *ui)
 {
     if (!oldstate->completed && newstate->completed &&
        !oldstate->used_solve && !newstate->used_solve)
@@ -2110,12 +2058,17 @@ static float game_flash_length(game_state *oldstate, game_state *newstate,
     return 0.0F;
 }
 
-static int game_timing_state(game_state *state, game_ui *ui)
+static int game_status(const game_state *state)
+{
+    return state->completed ? +1 : 0;
+}
+
+static int game_timing_state(const game_state *state, game_ui *ui)
 {
     return TRUE;
 }
 
-static void game_print_size(game_params *params, float *x, float *y)
+static void game_print_size(const game_params *params, float *x, float *y)
 {
     int pw, ph;
 
@@ -2123,11 +2076,11 @@ static void game_print_size(game_params *params, float *x, float *y)
      * I'll use 6mm squares by default.
      */
     game_compute_size(params, 600, &pw, &ph);
-    *x = pw / 100.0;
-    *y = ph / 100.0;
+    *x = pw / 100.0F;
+    *y = ph / 100.0F;
 }
 
-static void game_print(drawing *dr, game_state *state, int tilesize)
+static void game_print(drawing *dr, const game_state *state, int tilesize)
 {
     int w = state->p.w, h = state->p.h, W = w+1;
     int ink = print_mono_colour(dr, 0);
@@ -2191,9 +2144,9 @@ static void game_print(drawing *dr, game_state *state, int tilesize)
 #endif
 
 const struct game thegame = {
-    "Slant", "games.slant",
+    "Slant", "games.slant", "slant",
     default_params,
-    game_fetch_preset,
+    game_fetch_preset, NULL,
     decode_params,
     encode_params,
     free_params,
@@ -2206,7 +2159,7 @@ const struct game thegame = {
     dup_game,
     free_game,
     TRUE, solve_game,
-    TRUE, game_text_format,
+    TRUE, game_can_format_as_text_now, game_text_format,
     new_ui,
     free_ui,
     encode_ui,
@@ -2221,6 +2174,7 @@ const struct game thegame = {
     game_redraw,
     game_anim_length,
     game_flash_length,
+    game_status,
     TRUE, FALSE, game_print_size, game_print,
     FALSE,                            /* wants_statusbar */
     FALSE, game_timing_state,
@@ -2316,3 +2270,5 @@ int main(int argc, char **argv)
 }
 
 #endif
+
+/* vim: set shiftwidth=4 tabstop=8: */