chiark / gitweb /
Tents: mark squares as non-tents with {Shift,Control}-cursor keys.
[sgt-puzzles.git] / pegs.c
diff --git a/pegs.c b/pegs.c
index 560bf0ae318e1f9b648cac3523ee182e649dc1bd..1902e164b9f19c248567b33c34729679cdec0b31 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
 };
 
@@ -98,7 +102,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 */
@@ -125,7 +129,7 @@ static void decode_params(game_params *params, char const *string)
            params->type = i;
 }
 
-static char *encode_params(game_params *params, int full)
+static char *encode_params(const game_params *params, int full)
 {
     char str[80];
 
@@ -137,7 +141,7 @@ static char *encode_params(game_params *params, int full)
     return dupstr(str);
 }
 
-static config_item *game_configure(game_params *params)
+static config_item *game_configure(const game_params *params)
 {
     config_item *ret = snewn(4, config_item);
     char buf[80];
@@ -167,7 +171,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);
 
@@ -178,7 +182,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)
 {
     if (full && (params->w <= 3 || params->h <= 3))
        return "Width and height must both be greater than three";
@@ -499,7 +503,7 @@ static void pegs_generate(unsigned char *grid, int w, int h, random_state *rs)
  * it as part of the puzzle.
  */
 
-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;
@@ -656,7 +660,7 @@ static char *new_game_desc(game_params *params, random_state *rs,
     return ret;
 }
 
-static char *validate_desc(game_params *params, char *desc)
+static char *validate_desc(const game_params *params, const char *desc)
 {
     int len = params->w * params->h;
 
@@ -668,7 +672,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;
     game_state *state = snew(game_state);
@@ -685,7 +690,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->w, h = state->h;
     game_state *ret = snew(game_state);
@@ -705,13 +710,18 @@ static void free_game(game_state *state)
     sfree(state);
 }
 
-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)
 {
     return NULL;
 }
 
-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->w, h = state->h;
     int x, y;
@@ -734,14 +744,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)
+static game_ui *new_ui(const 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;
 }
@@ -751,17 +777,17 @@ 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)
 {
     /*
      * Cancel a drag, in case the source square has become
@@ -789,10 +815,12 @@ struct game_drawstate {
     int bgcolour;
 };
 
-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->w, h = state->h;
+    char buf[80];
 
     if (button == LEFT_BUTTON) {
        int tx, ty;
@@ -819,6 +847,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) {
@@ -829,7 +858,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;
 
        /*
@@ -859,11 +887,64 @@ 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;
 }
 
-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->w, h = state->h;
     int sx, sy, tx, ty;
@@ -918,8 +999,8 @@ 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)
 {
     /* Ick: fake up `ds->tilesize' for macro expansion purposes */
     struct { int tilesize; } ads, *ds = &ads;
@@ -930,7 +1011,7 @@ static void game_compute_size(game_params *params, int tilesize,
 }
 
 static void game_set_size(drawing *dr, game_drawstate *ds,
-                         game_params *params, int tilesize)
+                          const game_params *params, int tilesize)
 {
     ds->tilesize = tilesize;
 
@@ -950,11 +1031,15 @@ static float *game_colours(frontend *fe, int *ncolours)
     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(drawing *dr, game_state *state)
+static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
 {
     int w = state->w, h = state->h;
     struct game_drawstate *ds = snew(struct game_drawstate);
@@ -988,24 +1073,39 @@ static void game_free_drawstate(drawing *dr, game_drawstate *ds)
 static void draw_tile(drawing *dr, game_drawstate *ds,
                      int x, int y, int v, int bgcolour)
 {
+    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) {
+        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,
-                   COL_LOWLIGHT, COL_LOWLIGHT);
+                    bg, bg);
     } else if (v == GRID_PEG) {
+        bg = (cursor || jumping) ? COL_CURSOR : COL_PEG;
        draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/3,
-                   COL_PEG, COL_PEG);
+                   bg, bg);
+        bg = (!cursor || jumping) ? COL_PEG : COL_CURSOR;
+        draw_circle(dr, x+TILESIZE/2, y+TILESIZE/2, TILESIZE/4,
+                    bg, bg);
     }
 
     draw_update(dr, x, 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->w, h = state->h;
     int x, y;
@@ -1132,10 +1232,15 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
             */
            if (ui->dragging && ui->sx == x && ui->sy == y && v == GRID_PEG)
                v = GRID_HOLE;
+
+            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;
            }
        }
 
@@ -1153,14 +1258,14 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
     ds->bgcolour = bgcolour;
 }
 
-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)
         return 2 * FLASH_FRAME;
@@ -1168,16 +1273,25 @@ 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)
+{
+    /*
+     * 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(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)
 {
 }
 
-static void game_print(drawing *dr, game_state *state, int tilesize)
+static void game_print(drawing *dr, const game_state *state, int tilesize)
 {
 }
 
@@ -1186,7 +1300,7 @@ static void game_print(drawing *dr, game_state *state, int tilesize)
 #endif
 
 const struct game thegame = {
-    "Pegs", "games.pegs",
+    "Pegs", "games.pegs", "pegs",
     default_params,
     game_fetch_preset,
     decode_params,
@@ -1201,7 +1315,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,
@@ -1216,8 +1330,11 @@ const struct game thegame = {
     game_redraw,
     game_anim_length,
     game_flash_length,
+    game_status,
     FALSE, FALSE, game_print_size, game_print,
     FALSE,                            /* wants_statusbar */
     FALSE, game_timing_state,
     0,                                /* flags */
 };
+
+/* vim: set shiftwidth=4 tabstop=8: */