chiark / gitweb /
changelog: document last change
[sgt-puzzles.git] / galaxies.c
index 77ef597a1b63ece6af0816f3fde0d19c1148d0f2..f4f75c629c7889b506b48c7c36f087e939003a37 100644 (file)
@@ -148,6 +148,14 @@ struct game_state {
                            or -1 if stale. */
 };
 
+static int check_complete(const game_state *state, int *dsf, int *colours);
+static int solver_state(game_state *state, int maxdiff);
+static int solver_obvious(game_state *state);
+static int solver_obvious_dot(game_state *state, space *dot);
+static space *space_opposite_dot(const game_state *state, const space *sp,
+                                 const space *dot);
+static space *tile_opposite(const game_state *state, const space *sp);
+
 /* ----------------------------------------------------------
  * Game parameters and presets
  */
@@ -194,7 +202,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 */
@@ -221,7 +229,7 @@ static void decode_params(game_params *params, char const *string)
     }
 }
 
-static char *encode_params(game_params *params, int full)
+static char *encode_params(const game_params *params, int full)
 {
     char str[80];
     sprintf(str, "%dx%d", params->w, params->h);
@@ -230,7 +238,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;
     char buf[80];
@@ -262,7 +270,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);
 
@@ -273,7 +281,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 (params->w < 3 || params->h < 3)
         return "Width and height must both be at least 3";
@@ -302,7 +310,7 @@ static void remove_dot(space *space) {
     space->flags &= ~F_DOT;
 }
 
-static void remove_assoc(game_state *state, space *tile) {
+static void remove_assoc(const game_state *state, space *tile) {
     if (tile->flags & F_TILE_ASSOC) {
         SPACE(state, tile->dotx, tile->doty).nassoc--;
         tile->flags &= ~F_TILE_ASSOC;
@@ -311,7 +319,22 @@ static void remove_assoc(game_state *state, space *tile) {
     }
 }
 
-static void add_assoc(game_state *state, space *tile, space *dot) {
+static void remove_assoc_with_opposite(game_state *state, space *tile) {
+    space *opposite;
+
+    if (!(tile->flags & F_TILE_ASSOC)) {
+        return;
+    }
+
+    opposite = tile_opposite(state, tile);
+    remove_assoc(state, tile);
+
+    if (opposite != NULL && opposite != tile) {
+        remove_assoc(state, opposite);
+    }
+}
+
+static void add_assoc(const game_state *state, space *tile, space *dot) {
     remove_assoc(state, tile);
 
 #ifdef STANDALONE_PICTURE_GENERATOR
@@ -327,21 +350,50 @@ static void add_assoc(game_state *state, space *tile, space *dot) {
            tile->x, tile->y, dot->x, dot->y, dot->nassoc));*/
 }
 
-static struct space *sp2dot(game_state *state, int x, int y)
+static void add_assoc_with_opposite(game_state *state, space *tile, space *dot) {
+    int *colors;
+    space *opposite = space_opposite_dot(state, tile, dot);
+
+    if (opposite == NULL) {
+        return;
+    }
+    if (opposite->flags & F_DOT) {
+        return;
+    }
+
+    colors = snewn(state->w * state->h, int);
+    check_complete(state, NULL, colors);
+    if (colors[(tile->y - 1)/2 * state->w + (tile->x - 1)/2]) {
+        sfree(colors);
+        return;
+    }
+    if (colors[(opposite->y - 1)/2 * state->w + (opposite->x - 1)/2]) {
+        sfree(colors);
+        return;
+    }
+
+    sfree(colors);
+    remove_assoc_with_opposite(state, tile);
+    add_assoc(state, tile, dot);
+    remove_assoc_with_opposite(state, opposite);
+    add_assoc(state, opposite, dot);
+}
+
+static space *sp2dot(const game_state *state, int x, int y)
 {
-    struct space *sp = &SPACE(state, x, y);
+    space *sp = &SPACE(state, x, y);
     if (!(sp->flags & F_TILE_ASSOC)) return NULL;
     return &SPACE(state, sp->dotx, sp->doty);
 }
 
 #define IS_VERTICAL_EDGE(x) ((x % 2) == 0)
 
-static int game_can_format_as_text_now(game_params *params)
+static int game_can_format_as_text_now(const game_params *params)
 {
     return TRUE;
 }
 
-static char *game_text_format(game_state *state)
+static char *game_text_format(const game_state *state)
 {
     int maxlen = (state->sx+1)*state->sy, x, y;
     char *ret, *p;
@@ -398,7 +450,7 @@ static char *game_text_format(game_state *state)
     return ret;
 }
 
-static void dbg_state(game_state *state)
+static void dbg_state(const game_state *state)
 {
 #ifdef DEBUGGING
     char *temp = game_text_format(state);
@@ -465,7 +517,7 @@ static int foreach_vertex(game_state *state, space_cb cb, unsigned int f,
 static int is_same_assoc(game_state *state,
                          int x1, int y1, int x2, int y2)
 {
-    struct space *s1, *s2;
+    space *s1, *s2;
 
     if (!INGRID(state, x1, y1) || !INGRID(state, x2, y2))
         return 0;
@@ -503,8 +555,8 @@ static int edges_into_vertex(game_state *state,
 }
 #endif
 
-static struct space *space_opposite_dot(struct game_state *state,
-                                        struct space *sp, struct space *dot)
+static space *space_opposite_dot(const game_state *state, const space *sp,
+                                 const space *dot)
 {
     int dx, dy, tx, ty;
     space *sp2;
@@ -520,9 +572,9 @@ static struct space *space_opposite_dot(struct game_state *state,
     return sp2;
 }
 
-static struct space *tile_opposite(struct game_state *state, struct space *sp)
+static space *tile_opposite(const game_state *state, const space *sp)
 {
-    struct space *dot;
+    space *dot;
 
     assert(sp->flags & F_TILE_ASSOC);
     dot = &SPACE(state, sp->dotx, sp->doty);
@@ -540,8 +592,7 @@ static int dotfortile(game_state *state, space *tile, space *dot)
     return 1;
 }
 
-static void adjacencies(struct game_state *state, struct space *sp,
-                        struct space **a1s, struct space **a2s)
+static void adjacencies(game_state *state, space *sp, space **a1s, space **a2s)
 {
     int dxs[4] = {-1, 1, 0, 0}, dys[4] = {0, 0, -1, 1};
     int n, x, y;
@@ -569,7 +620,7 @@ static void adjacencies(struct game_state *state, struct space *sp,
 
 static int outline_tile_fordot(game_state *state, space *tile, int mark)
 {
-    struct space *tadj[4], *eadj[4];
+    space *tadj[4], *eadj[4];
     int i, didsth = 0, edge, same;
 
     assert(tile->type == s_tile);
@@ -599,8 +650,7 @@ static int outline_tile_fordot(game_state *state, space *tile, int mark)
     return didsth;
 }
 
-static void tiles_from_edge(struct game_state *state,
-                            struct space *sp, struct space **ts)
+static void tiles_from_edge(game_state *state, space *sp, space **ts)
 {
     int xs[2], ys[2];
 
@@ -617,7 +667,8 @@ static void tiles_from_edge(struct game_state *state,
 
 /* Returns a move string for use by 'solve', including the initial
  * 'S' if issolve is true. */
-static char *diff_game(game_state *src, game_state *dest, int issolve)
+static char *diff_game(const game_state *src, const game_state *dest,
+                       int issolve)
 {
     int movelen = 0, movesize = 256, x, y, len;
     char *move = snewn(movesize, char), buf[80], *sep = "";
@@ -756,13 +807,13 @@ static game_state *blank_game(int w, int h)
 
     state->sx = (w*2)+1;
     state->sy = (h*2)+1;
-    state->grid = snewn(state->sx * state->sy, struct space);
+    state->grid = snewn(state->sx * state->sy, space);
     state->completed = state->used_solve = 0;
 
     for (x = 0; x < state->sx; x++) {
         for (y = 0; y < state->sy; y++) {
-            struct space *sp = &SPACE(state, x, y);
-            memset(sp, 0, sizeof(struct space));
+            space *sp = &SPACE(state, x, y);
+            memset(sp, 0, sizeof(space));
             sp->x = x;
             sp->y = y;
             if ((x % 2) == 0 && (y % 2) == 0)
@@ -819,7 +870,7 @@ static void clear_game(game_state *state, int cleardots)
     if (cleardots) game_update_dots(state);
 }
 
-static game_state *dup_game(game_state *state)
+static game_state *dup_game(const game_state *state)
 {
     game_state *ret = blank_game(state->w, state->h);
 
@@ -827,7 +878,7 @@ static game_state *dup_game(game_state *state)
     ret->used_solve = state->used_solve;
 
     memcpy(ret->grid, state->grid,
-           ret->sx*ret->sy*sizeof(struct space));
+           ret->sx*ret->sy*sizeof(space));
 
     game_update_dots(ret);
 
@@ -1166,8 +1217,6 @@ int maxtries;
 #define MAXTRIES 50
 #endif
 
-static int solver_obvious_dot(game_state *state,space *dot);
-
 #define GP_DOTS   1
 
 static void generate_pass(game_state *state, random_state *rs, int *scratch,
@@ -1230,10 +1279,7 @@ static void generate_pass(game_state *state, random_state *rs, int *scratch,
     dbg_state(state);
 }
 
-static int check_complete(game_state *state, int *dsf, int *colours);
-static int solver_state(game_state *state, int maxdiff);
-
-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)
 {
     game_state *state = blank_game(params->w, params->h), *copy;
@@ -1255,6 +1301,8 @@ generate:
 
     game_update_dots(state);
 
+    if (state->ndots == 1) goto generate;
+
 #ifdef DEBUGGING
     {
         char *tmp = encode_game(state);
@@ -1466,8 +1514,6 @@ generate:
     return desc;
 }
 
-static int solver_obvious(game_state *state);
-
 static int dots_too_close(game_state *state)
 {
     /* Quick-and-dirty check, using half the solver:
@@ -1480,7 +1526,7 @@ static int dots_too_close(game_state *state)
     return (ret == -1) ? 1 : 0;
 }
 
-static game_state *load_game(game_params *params, char *desc,
+static game_state *load_game(const game_params *params, const char *desc,
                              char **why_r)
 {
     game_state *state = blank_game(params->w, params->h);
@@ -1528,7 +1574,7 @@ fail:
     return NULL;
 }
 
-static char *validate_desc(game_params *params, char *desc)
+static char *validate_desc(const game_params *params, const char *desc)
 {
     char *why = NULL;
     game_state *dummy = load_game(params, desc, &why);
@@ -1540,7 +1586,8 @@ static char *validate_desc(game_params *params, char *desc)
     return why;
 }
 
-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)
 {
     game_state *state = load_game(params, desc, NULL);
     if (!state) {
@@ -1749,7 +1796,7 @@ static int solver_lines_opposite_cb(game_state *state, space *edge, void *ctx)
 static int solver_spaces_oneposs_cb(game_state *state, space *tile, void *ctx)
 {
     int n, eset, ret;
-    struct space *edgeadj[4], *tileadj[4];
+    space *edgeadj[4], *tileadj[4];
     int dotx, doty;
 
     assert(tile->type == s_tile);
@@ -2056,8 +2103,6 @@ static int solver_recurse_cb(game_state *state, space *tile, void *ctx)
     return 0;
 }
 
-static int solver_state(game_state *state, int maxdiff);
-
 #define MAXRECURSE 5
 
 static int solver_recurse(game_state *state, int maxdiff)
@@ -2089,11 +2134,11 @@ static int solver_recurse(game_state *state, int maxdiff)
     solver_recurse_depth++;
 #endif
 
-    ingrid = snewn(gsz, struct space);
-    memcpy(ingrid, state->grid, gsz * sizeof(struct space));
+    ingrid = snewn(gsz, space);
+    memcpy(ingrid, state->grid, gsz * sizeof(space));
 
     for (n = 0; n < state->ndots; n++) {
-        memcpy(state->grid, ingrid, gsz * sizeof(struct space));
+        memcpy(state->grid, ingrid, gsz * sizeof(space));
 
         if (!dotfortile(state, rctx.best, state->dots[n])) continue;
 
@@ -2107,8 +2152,8 @@ static int solver_recurse(game_state *state, int maxdiff)
         if (diff == DIFF_IMPOSSIBLE && ret != DIFF_IMPOSSIBLE) {
             /* we found our first solved grid; copy it away. */
             assert(!outgrid);
-            outgrid = snewn(gsz, struct space);
-            memcpy(outgrid, state->grid, gsz * sizeof(struct space));
+            outgrid = snewn(gsz, space);
+            memcpy(outgrid, state->grid, gsz * sizeof(space));
         }
         /* reset cell back to unassociated. */
         bestopp = tile_opposite(state, rctx.best);
@@ -2140,7 +2185,7 @@ static int solver_recurse(game_state *state, int maxdiff)
 
     if (outgrid) {
         /* we found (at least one) soln; copy it back to state */
-        memcpy(state->grid, outgrid, gsz * sizeof(struct space));
+        memcpy(state->grid, outgrid, gsz * sizeof(space));
         sfree(outgrid);
     }
     sfree(ingrid);
@@ -2212,8 +2257,8 @@ got_result:
 }
 
 #ifndef EDITOR
-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)
 {
     game_state *tosolve;
     char *ret;
@@ -2263,7 +2308,7 @@ struct game_ui {
     int cur_x, cur_y, cur_visible;
 };
 
-static game_ui *new_ui(game_state *state)
+static game_ui *new_ui(const game_state *state)
 {
     game_ui *ui = snew(game_ui);
     ui->dragging = FALSE;
@@ -2277,17 +2322,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)
 {
 }
 
@@ -2315,6 +2360,7 @@ struct game_drawstate {
     unsigned long *grid;
     int *dx, *dy;
     blitter *bl;
+    blitter *blmirror;
 
     int dragging, dragx, dragy;
 
@@ -2369,12 +2415,13 @@ static void coord_round_to_edge(float x, float y, int *xr, int *yr)
 #endif
 
 #ifdef EDITOR
-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)
 {
     char buf[80];
     int px, py;
-    struct space *sp;
+    space *sp;
 
     px = 2*FROMCOORD((float)x) + 0.5;
     py = 2*FROMCOORD((float)y) + 0.5;
@@ -2404,8 +2451,9 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
     return NULL;
 }
 #else
-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)
 {
     /* UI operations (play mode):
      *
@@ -2421,7 +2469,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
     char buf[80];
     const char *sep = "";
     int px, py;
-    struct space *sp, *dot;
+    space *sp, *dot;
 
     buf[0] = '\0';
 
@@ -2545,7 +2593,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
         if (INUI(state, px, py)) {
             sp = &SPACE(state, px, py);
 
-            if (!(sp->flags & F_DOT) && !(sp->flags & F_TILE_ASSOC))
+            if (!(sp->flags & F_DOT))
                sprintf(buf + strlen(buf), "%sA%d,%d,%d,%d",
                        sep, px, py, ui->dotx, ui->doty);
        }
@@ -2610,7 +2658,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
 }
 #endif
 
-static int check_complete(game_state *state, int *dsf, int *colours)
+static int check_complete(const game_state *state, int *dsf, int *colours)
 {
     int w = state->w, h = state->h;
     int x, y, i, ret;
@@ -2787,11 +2835,12 @@ static int check_complete(game_state *state, int *dsf, int *colours)
     return ret;
 }
 
-static game_state *execute_move(game_state *state, char *move)
+static game_state *execute_move(const game_state *state, const char *move)
 {
     int x, y, ax, ay, n, dx, dy;
     game_state *ret = dup_game(state);
-    struct space *sp, *dot;
+    space *sp, *dot;
+    int currently_solving = FALSE;
 
     debug(("%s\n", move));
 
@@ -2804,7 +2853,7 @@ static game_state *execute_move(game_state *state, char *move)
             ) {
             move++;
             if (sscanf(move, "%d,%d%n", &x, &y, &n) != 2 ||
-                !INUI(state, x, y))
+                !INUI(ret, x, y))
                 goto badmove;
 
             sp = &SPACE(ret, x, y);
@@ -2812,14 +2861,14 @@ static game_state *execute_move(game_state *state, char *move)
             if (c == 'D' || c == 'd') {
                 unsigned int currf, newf, maskf;
 
-                if (!dot_is_possible(state, sp, 1)) goto badmove;
+                if (!dot_is_possible(ret, sp, 1)) goto badmove;
 
                 newf = F_DOT | (c == 'd' ? F_DOT_BLACK : 0);
                 currf = GRID(ret, grid, x, y).flags;
                 maskf = F_DOT | F_DOT_BLACK;
                 /* if we clicked 'white dot':
                  *   white --> empty, empty --> white, black --> white.
-                 * if we clicker 'black dot':
+                 * if we clicked 'black dot':
                  *   black --> empty, empty --> black, white --> black.
                  */
                 if (currf & maskf) {
@@ -2838,7 +2887,11 @@ static game_state *execute_move(game_state *state, char *move)
             } else if (c == 'U') {
                 if (sp->type != s_tile || !(sp->flags & F_TILE_ASSOC))
                     goto badmove;
-                remove_assoc(ret, sp);
+                /* The solver doesn't assume we'll mirror things */
+                if (currently_solving)
+                    remove_assoc(ret, sp);
+                else
+                    remove_assoc_with_opposite(ret, sp);
             } else if (c == 'M') {
                 if (!(sp->flags & F_DOT)) goto badmove;
                 sp->flags ^= F_DOT_HOLD;
@@ -2847,8 +2900,8 @@ static game_state *execute_move(game_state *state, char *move)
         } else if (c == 'A' || c == 'a') {
             move++;
             if (sscanf(move, "%d,%d,%d,%d%n", &x, &y, &ax, &ay, &n) != 4 ||
-                x < 1 || y < 1 || x >= (state->sx-1) || y >= (state->sy-1) ||
-                ax < 1 || ay < 1 || ax >= (state->sx-1) || ay >= (state->sy-1))
+                x < 1 || y < 1 || x >= (ret->sx-1) || y >= (ret->sy-1) ||
+                ax < 1 || ay < 1 || ax >= (ret->sx-1) || ay >= (ret->sy-1))
                 goto badmove;
 
             dot = &GRID(ret, grid, ax, ay);
@@ -2860,10 +2913,14 @@ static game_state *execute_move(game_state *state, char *move)
                     sp = &GRID(ret, grid, x+dx, y+dy);
                     if (sp->type != s_tile) continue;
                     if (sp->flags & F_TILE_ASSOC) {
-                        space *dot = &SPACE(state, sp->dotx, sp->doty);
+                        space *dot = &SPACE(ret, sp->dotx, sp->doty);
                         if (dot->flags & F_DOT_HOLD) continue;
                     }
-                    add_assoc(state, sp, dot);
+                    /* The solver doesn't assume we'll mirror things */
+                    if (currently_solving)
+                        add_assoc(ret, sp, dot);
+                    else
+                        add_assoc_with_opposite(ret, sp, dot);
                 }
             }
             move += n;
@@ -2875,6 +2932,7 @@ static game_state *execute_move(game_state *state, char *move)
         } else if (c == 'S') {
             move++;
            ret->used_solve = 1;
+            currently_solving = TRUE;
         } else
             goto badmove;
 
@@ -2907,8 +2965,8 @@ badmove:
  * we may want to drag from them, for example.
  */
 
-static void game_compute_size(game_params *params, int sz,
-                             int *x, int *y)
+static void game_compute_size(const game_params *params, int sz,
+                              int *x, int *y)
 {
     struct { int tilesize, w, h; } ads, *ds = &ads;
 
@@ -2921,7 +2979,7 @@ static void game_compute_size(game_params *params, int sz,
 }
 
 static void game_set_size(drawing *dr, game_drawstate *ds,
-                         game_params *params, int sz)
+                          const game_params *params, int sz)
 {
     ds->tilesize = sz;
 
@@ -2930,6 +2988,9 @@ static void game_set_size(drawing *dr, game_drawstate *ds,
     assert(!ds->bl);
     ds->bl = blitter_new(dr, TILE_SIZE, TILE_SIZE);
 
+    assert(!ds->blmirror);
+    ds->blmirror = blitter_new(dr, TILE_SIZE, TILE_SIZE);
+
     assert(!ds->cur_bl);
     ds->cur_bl = blitter_new(dr, TILE_SIZE, TILE_SIZE);
 }
@@ -2977,10 +3038,10 @@ static float *game_colours(frontend *fe, int *ncolours)
     /* tinge the edit background to bluey */
     ret[COL_BACKGROUND * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.8F;
     ret[COL_BACKGROUND * 3 + 1] = ret[COL_BACKGROUND * 3 + 0] * 0.8F;
-    ret[COL_BACKGROUND * 3 + 2] = max(ret[COL_BACKGROUND * 3 + 0] * 1.4F, 1.0F);
+    ret[COL_BACKGROUND * 3 + 2] = min(ret[COL_BACKGROUND * 3 + 0] * 1.4F, 1.0F);
 #endif
 
-    ret[COL_CURSOR * 3 + 0] = max(ret[COL_BACKGROUND * 3 + 0] * 1.4F, 1.0F);
+    ret[COL_CURSOR * 3 + 0] = min(ret[COL_BACKGROUND * 3 + 0] * 1.4F, 1.0F);
     ret[COL_CURSOR * 3 + 1] = ret[COL_BACKGROUND * 3 + 0] * 0.8F;
     ret[COL_CURSOR * 3 + 2] = ret[COL_BACKGROUND * 3 + 0] * 0.8F;
 
@@ -2988,7 +3049,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)
 {
     struct game_drawstate *ds = snew(struct game_drawstate);
     int i;
@@ -3004,6 +3065,7 @@ static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
     ds->dy = snewn(ds->w*ds->h, int);
 
     ds->bl = NULL;
+    ds->blmirror = NULL;
     ds->dragging = FALSE;
     ds->dragx = ds->dragy = 0;
 
@@ -3020,6 +3082,7 @@ static void game_free_drawstate(drawing *dr, game_drawstate *ds)
 {
     if (ds->cur_bl) blitter_free(dr, ds->cur_bl);
     sfree(ds->colour_scratch);
+    if (ds->blmirror) blitter_free(dr, ds->blmirror);
     if (ds->bl) blitter_free(dr, ds->bl);
     sfree(ds->dx);
     sfree(ds->dy);
@@ -3145,12 +3208,24 @@ static void draw_square(drawing *dr, game_drawstate *ds, int x, int y,
     draw_update(dr, lx, ly, TILE_SIZE, TILE_SIZE);
 }
 
-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 calculate_opposite_point(const game_ui *ui,
+                                     const game_drawstate *ds, const int x,
+                                     const int y, int *oppositex,
+                                     int *oppositey)
+{
+    /* oppositex - dotx = dotx - x <=> oppositex = 2 * dotx - x */
+    *oppositex = 2 * SCOORD(ui->dotx) - x;
+    *oppositey = 2 * SCOORD(ui->doty) - y;
+}
+
+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 = ds->w, h = ds->h;
     int x, y, flashing = FALSE;
+    int oppx, oppy;
 
     if (flashtime > 0) {
         int frame = (int)(flashtime / FLASH_TIME);
@@ -3159,8 +3234,15 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
 
     if (ds->dragging) {
         assert(ds->bl);
+        assert(ds->blmirror);
+        calculate_opposite_point(ui, ds, ds->dragx + TILE_SIZE/2,
+                                 ds->dragy + TILE_SIZE/2, &oppx, &oppy);
+        oppx -= TILE_SIZE/2;
+        oppy -= TILE_SIZE/2;
         blitter_load(dr, ds->bl, ds->dragx, ds->dragy);
         draw_update(dr, ds->dragx, ds->dragy, TILE_SIZE, TILE_SIZE);
+        blitter_load(dr, ds->blmirror, oppx, oppy);
+        draw_update(dr, oppx, oppy, TILE_SIZE, TILE_SIZE);
         ds->dragging = FALSE;
     }
     if (ds->cur_visible) {
@@ -3185,7 +3267,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
         for (x = 0; x < w; x++) {
             unsigned long flags = 0;
             int ddx = 0, ddy = 0;
-            space *sp;
+            space *sp, *opp;
             int dx, dy;
 
             /*
@@ -3223,6 +3305,11 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
              * everything goes briefly back to background colour.
              */
             sp = &SPACE(state, x*2+1, y*2+1);
+            if (sp->flags & F_TILE_ASSOC) {
+                opp = tile_opposite(state, sp);
+            } else {
+                opp = NULL;
+            }
             if (ds->colour_scratch[y*w+x] && !flashing) {
                 flags |= (ds->colour_scratch[y*w+x] == 2 ?
                           DRAW_BLACK : DRAW_WHITE);
@@ -3238,7 +3325,9 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
              */
             if ((sp->flags & F_TILE_ASSOC) && !ds->colour_scratch[y*w+x]) {
                if (ui->dragging && ui->srcx == x*2+1 && ui->srcy == y*2+1) {
-                   /* don't do it */
+                    /* tile is the source, don't do it */
+                } else if (ui->dragging && opp && ui->srcx == opp->x && ui->srcy == opp->y) {
+                    /* opposite tile is the source, don't do it */
                } else if (sp->doty != y*2+1 || sp->dotx != x*2+1) {
                     flags |= DRAW_ARROW;
                     ddy = sp->doty - (y*2+1);
@@ -3315,10 +3404,13 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
         ds->dragging = TRUE;
         ds->dragx = ui->dx - TILE_SIZE/2;
         ds->dragy = ui->dy - TILE_SIZE/2;
+        calculate_opposite_point(ui, ds, ui->dx, ui->dy, &oppx, &oppy);
         blitter_save(dr, ds->bl, ds->dragx, ds->dragy);
-        draw_arrow(dr, ds, ui->dx, ui->dy,
-                   SCOORD(ui->dotx) - ui->dx,
+        blitter_save(dr, ds->blmirror, oppx - TILE_SIZE/2, oppy - TILE_SIZE/2);
+        draw_arrow(dr, ds, ui->dx, ui->dy, SCOORD(ui->dotx) - ui->dx,
                    SCOORD(ui->doty) - ui->dy, COL_ARROW);
+        draw_arrow(dr, ds, oppx, oppy, SCOORD(ui->dotx) - oppx,
+                   SCOORD(ui->doty) - oppy, COL_ARROW);
     }
 #ifdef EDITOR
     {
@@ -3332,14 +3424,14 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
 #endif
 }
 
-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) &&
         !(newstate->used_solve))
@@ -3348,18 +3440,18 @@ static float game_flash_length(game_state *oldstate, game_state *newstate,
         return 0.0F;
 }
 
-static int game_is_solved(game_state *state)
+static int game_status(const game_state *state)
 {
-    return state->completed;
+    return state->completed ? +1 : 0;
 }
 
-static int game_timing_state(game_state *state, game_ui *ui)
+static int game_timing_state(const game_state *state, game_ui *ui)
 {
     return TRUE;
 }
 
 #ifndef EDITOR
-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;
 
@@ -3372,7 +3464,7 @@ static void game_print_size(game_params *params, float *x, float *y)
    *y = ph / 100.0F;
 }
 
-static void game_print(drawing *dr, game_state *state, int sz)
+static void game_print(drawing *dr, const game_state *state, int sz)
 {
     int w = state->w, h = state->h;
     int white, black, blackish;
@@ -3541,7 +3633,7 @@ static void game_print(drawing *dr, game_state *state, int sz)
 const struct game thegame = {
     "Galaxies", "games.galaxies", "galaxies",
     default_params,
-    game_fetch_preset,
+    game_fetch_preset, NULL,
     decode_params,
     encode_params,
     free_params,
@@ -3573,7 +3665,7 @@ const struct game thegame = {
     game_redraw,
     game_anim_length,
     game_flash_length,
-    game_is_solved,
+    game_status,
 #ifdef EDITOR
     FALSE, FALSE, NULL, NULL,
     TRUE,                              /* wants_statusbar */