X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=galaxies.c;h=f4f75c629c7889b506b48c7c36f087e939003a37;hb=a0a581c8b5422bf0c5ed3fde6aa25811e4eb89fc;hp=4a6c60170886c5a8abf8e17461171798227c7061;hpb=774395d37366e52113c434173295db682d7d37e9;p=sgt-puzzles.git diff --git a/galaxies.c b/galaxies.c index 4a6c601..f4f75c6 100644 --- a/galaxies.c +++ b/galaxies.c @@ -152,6 +152,9 @@ 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 @@ -316,6 +319,21 @@ static void remove_assoc(const game_state *state, space *tile) { } } +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); @@ -332,6 +350,35 @@ static void add_assoc(const game_state *state, space *tile, space *dot) { tile->x, tile->y, dot->x, dot->y, dot->nassoc));*/ } +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) { space *sp = &SPACE(state, x, y); @@ -508,7 +555,8 @@ static int edges_into_vertex(game_state *state, } #endif -static space *space_opposite_dot(game_state *state, space *sp, space *dot) +static space *space_opposite_dot(const game_state *state, const space *sp, + const space *dot) { int dx, dy, tx, ty; space *sp2; @@ -524,7 +572,7 @@ static space *space_opposite_dot(game_state *state, space *sp, space *dot) return sp2; } -static space *tile_opposite(game_state *state, space *sp) +static space *tile_opposite(const game_state *state, const space *sp) { space *dot; @@ -1253,6 +1301,8 @@ generate: game_update_dots(state); + if (state->ndots == 1) goto generate; + #ifdef DEBUGGING { char *tmp = encode_game(state); @@ -2310,6 +2360,7 @@ struct game_drawstate { unsigned long *grid; int *dx, *dy; blitter *bl; + blitter *blmirror; int dragging, dragx, dragy; @@ -2542,7 +2593,7 @@ static char *interpret_move(const game_state *state, game_ui *ui, 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); } @@ -2789,6 +2840,7 @@ 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); space *sp, *dot; + int currently_solving = FALSE; debug(("%s\n", move)); @@ -2835,7 +2887,11 @@ static game_state *execute_move(const game_state *state, const 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; @@ -2860,7 +2916,11 @@ static game_state *execute_move(const game_state *state, const char *move) space *dot = &SPACE(ret, sp->dotx, sp->doty); if (dot->flags & F_DOT_HOLD) continue; } - add_assoc(ret, 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; @@ -2872,6 +2932,7 @@ static game_state *execute_move(const game_state *state, const char *move) } else if (c == 'S') { move++; ret->used_solve = 1; + currently_solving = TRUE; } else goto badmove; @@ -2927,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); } @@ -3001,6 +3065,7 @@ static game_drawstate *game_new_drawstate(drawing *dr, const 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; @@ -3017,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); @@ -3142,6 +3208,16 @@ static void draw_square(drawing *dr, game_drawstate *ds, int x, int y, draw_update(dr, lx, ly, TILE_SIZE, TILE_SIZE); } +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, @@ -3149,6 +3225,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, { int w = ds->w, h = ds->h; int x, y, flashing = FALSE; + int oppx, oppy; if (flashtime > 0) { int frame = (int)(flashtime / FLASH_TIME); @@ -3157,8 +3234,15 @@ static void game_redraw(drawing *dr, game_drawstate *ds, 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) { @@ -3183,7 +3267,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, for (x = 0; x < w; x++) { unsigned long flags = 0; int ddx = 0, ddy = 0; - space *sp; + space *sp, *opp; int dx, dy; /* @@ -3221,6 +3305,11 @@ static void game_redraw(drawing *dr, game_drawstate *ds, * 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); @@ -3236,7 +3325,9 @@ static void game_redraw(drawing *dr, game_drawstate *ds, */ 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); @@ -3313,10 +3404,13 @@ static void game_redraw(drawing *dr, game_drawstate *ds, 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 { @@ -3539,7 +3633,7 @@ static void game_print(drawing *dr, const 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,