X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=galaxies.c;h=53d6fab2db1bf519229805cdfdca4db102edc21e;hb=3234912f921916a1b8da164fd61dc75579358577;hp=e1bc5b58cfd32034afbe152511b0eca96a5d5e16;hpb=db743e54e8a453491edbe02be6640f75154d5363;p=sgt-puzzles.git diff --git a/galaxies.c b/galaxies.c index e1bc5b5..53d6fab 100644 --- a/galaxies.c +++ b/galaxies.c @@ -53,6 +53,25 @@ int solver_show_working; #define solvep(x) do { if (solver_show_working) { printf x; } } while(0) #endif +#ifdef STANDALONE_PICTURE_GENERATOR +/* + * Dirty hack to enable the generator to construct a game ID which + * solves to a specified black-and-white bitmap. We define a global + * variable here which gives the desired colour of each square, and + * we arrange that the grid generator never merges squares of + * different colours. + * + * The bitmap as stored here is a simple int array (at these sizes + * it isn't worth doing fiddly bit-packing). picture[y*w+x] is 1 + * iff the pixel at (x,y) is intended to be black. + * + * (It might be nice to be able to specify some pixels as + * don't-care, to give the generator more leeway. But that might be + * fiddly.) + */ +static int *picture; +#endif + enum { COL_BACKGROUND, COL_WHITEBG, @@ -62,6 +81,7 @@ enum { COL_GRID, COL_EDGE, COL_ARROW, + COL_CURSOR, NCOLOURS }; @@ -128,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 */ @@ -174,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 */ @@ -201,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); @@ -210,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]; @@ -242,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); @@ -253,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"; @@ -282,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; @@ -291,9 +319,29 @@ 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 + if (picture) + assert(!picture[(tile->y/2) * state->w + (tile->x/2)] == + !(dot->flags & F_DOT_BLACK)); +#endif tile->flags |= F_TILE_ASSOC; tile->dotx = dot->x; tile->doty = dot->y; @@ -302,16 +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 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 maxlen = (state->sx+1)*state->sy, x, y; char *ret, *p; @@ -325,15 +407,17 @@ static char *game_text_format(game_state *state) sp = &SPACE(state, x, y); if (sp->flags & F_DOT) *p++ = 'o'; +#if 0 else if (sp->flags & (F_REACHABLE|F_MULTIPLE|F_MARK)) *p++ = (sp->flags & F_MULTIPLE) ? 'M' : (sp->flags & F_REACHABLE) ? 'R' : 'X'; +#endif else { switch (sp->type) { case s_tile: if (sp->flags & F_TILE_ASSOC) { space *dot = sp2dot(state, sp->x, sp->y); - if (dot->flags & F_DOT) + if (dot && dot->flags & F_DOT) *p++ = (dot->flags & F_DOT_BLACK) ? 'B' : 'W'; else *p++ = '?'; /* association with not-a-dot. */ @@ -366,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); @@ -433,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; @@ -471,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; @@ -488,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); @@ -508,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; @@ -537,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); @@ -567,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]; @@ -583,86 +665,10 @@ static void tiles_from_edge(struct game_state *state, ts[1] = INGRID(state, xs[1], ys[1]) ? &SPACE(state, xs[1], ys[1]) : NULL; } - /* Check all tiles are associated with something, and all shapes - * are the correct symmetry (i.e. all tiles have a matching tile - * the opposite direction from the dot) */ -static int cccb_assoc(game_state *state, space *tile, void *unused) -{ - assert(tile->type == s_tile); - - if (!(tile->flags & F_TILE_ASSOC)) return -1; - return 0; -} - -struct dgs_ctx { - space *dot; - int ndots; -}; - -static int dgs_cb_check(game_state *state, space *tile, void *vctx) -{ - struct dgs_ctx *ctx = (struct dgs_ctx *)vctx; - space *opp; - - if (!(tile->flags & F_TILE_ASSOC)) return 0; - if (tile->dotx != ctx->dot->x || - tile->doty != ctx->dot->y) return 0; - - ctx->ndots += 1; - - /* Check this tile has an opposite associated with same dot. */ - opp = tile_opposite(state, tile); - if (!opp || !(opp->flags & F_TILE_ASSOC)) return -1; - if (opp->dotx != tile->dotx || opp->doty != tile->doty) return -1; - - /* Check its edges are correct */ - if (outline_tile_fordot(state, tile, 0) == 1) - return -1; /* there was some fixing required, we're wrong. */ - - return 0; -} - -static int dot_good_shape(game_state *state, space *dot, int mark) -{ - struct dgs_ctx ctx; - - ctx.dot = dot; - ctx.ndots = 0; - - if (mark) dot->flags &= ~F_GOOD; - - if (foreach_tile(state, dgs_cb_check, 0, &ctx) == -1) - return 0; - if (ctx.ndots == 0) return 0; /* no dots assoc. with tile. */ - - if (mark) { - debug(("marking dot %d,%d good tile.\n", dot->x, dot->y)); - dot->flags |= F_GOOD; - } - return 1; -} - -static int check_complete(game_state *state, int mark_errors) -{ - int i, complete = 1; - - /* Are all tiles associated? */ - if (foreach_tile(state, cccb_assoc, 0, NULL) == -1) - complete = 0; - - /* Check all dots are associated, and their tiles are well-formed. */ - for (i = 0; i < state->ndots; i++) { - if (!dot_good_shape(state, state->dots[i], mark_errors)) - complete = 0; - } - - /*if (complete == 1) printf("Complete!\n");*/ - return complete; -} - -/* Returns a move string for use by 'solve'; if you don't want the - * initial 'S;' use ret[2]. */ -static char *diff_game(game_state *src, game_state *dest, int issolve) +/* Returns a move string for use by 'solve', including the initial + * 'S' if issolve is true. */ +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 = ""; @@ -728,6 +734,9 @@ static int dot_is_possible(game_state *state, space *sp, int allow_assoc) { int bx = 0, by = 0, dx, dy; space *adj; +#ifdef STANDALONE_PICTURE_GENERATOR + int col = -1; +#endif switch (sp->type) { case s_tile: @@ -749,8 +758,24 @@ static int dot_is_possible(game_state *state, space *sp, int allow_assoc) adj = &SPACE(state, sp->x+dx, sp->y+dy); - if (!allow_assoc && (adj->flags & F_TILE_ASSOC)) - return 0; +#ifdef STANDALONE_PICTURE_GENERATOR + /* + * Check that all the squares we're looking at have the + * same colour. + */ + if (picture) { + if (adj->type == s_tile) { + int c = picture[(adj->y / 2) * state->w + (adj->x / 2)]; + if (col < 0) + col = c; + if (c != col) + return 0; /* colour mismatch */ + } + } +#endif + + if (!allow_assoc && (adj->flags & F_TILE_ASSOC)) + return 0; if (dx != 0 || dy != 0) { /* Other than our own square, no dots nearby. */ @@ -782,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) @@ -845,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); @@ -853,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); @@ -948,6 +973,19 @@ static int movedot_cb(game_state *state, space *tile, void *vctx) newopp->doty != md->olddot->y) return -1; /* associated, but wrong dot. */ } +#ifdef STANDALONE_PICTURE_GENERATOR + if (picture) { + /* + * Reject if either tile and the dot don't match in colour. + */ + if (!(picture[(tile->y/2) * state->w + (tile->x/2)]) ^ + !(md->newdot->flags & F_DOT_BLACK)) + return -1; + if (!(picture[(newopp->y/2) * state->w + (newopp->x/2)]) ^ + !(md->newdot->flags & F_DOT_BLACK)) + return -1; + } +#endif break; case MD_MOVE: @@ -983,12 +1021,36 @@ static int dot_expand_or_move(game_state *state, space *dot, toadd[i]->x, toadd[i]->y)); assert(dot->flags & F_DOT); +#ifdef STANDALONE_PICTURE_GENERATOR + if (picture) { + /* + * Reject the expansion totally if any of the new tiles are + * the wrong colour. + */ + for (i = 0; i < nadd; i++) { + if (!(picture[(toadd[i]->y/2) * state->w + (toadd[i]->x/2)]) ^ + !(dot->flags & F_DOT_BLACK)) + return 0; + } + } +#endif + /* First off, could we just expand the current dot's tile to cover * the space(s) passed in and their opposites? */ for (i = 0; i < nadd; i++) { tileopp = space_opposite_dot(state, toadd[i], dot); if (!tileopp) goto noexpand; if (tileopp->flags & F_TILE_ASSOC) goto noexpand; +#ifdef STANDALONE_PICTURE_GENERATOR + if (picture) { + /* + * The opposite tiles have to be the right colour as well. + */ + if (!(picture[(tileopp->y/2) * state->w + (tileopp->x/2)]) ^ + !(dot->flags & F_DOT_BLACK)) + goto noexpand; + } +#endif } /* OK, all spaces have valid empty opposites: associate spaces and * opposites with our dot. */ @@ -1006,7 +1068,7 @@ static int dot_expand_or_move(game_state *state, space *dot, noexpand: /* Otherwise, try to move dot so as to encompass given spaces: */ - /* first, alculate the 'centre of gravity' of the new dot. */ + /* first, calculate the 'centre of gravity' of the new dot. */ nnew = dot->nassoc + nadd; /* number of tiles assoc. with new dot. */ cx = dot->x * dot->nassoc; cy = dot->y * dot->nassoc; @@ -1046,6 +1108,13 @@ noexpand: dot->x, dot->y)); return 0; } +#ifdef STANDALONE_PICTURE_GENERATOR + if (picture) { + if (!(picture[(tileopp->y/2) * state->w + (tileopp->x/2)]) ^ + !(dot->flags & F_DOT_BLACK)) + return 0; + } +#endif } /* If we've got here, we're ok. First, associate all of 'toadd' @@ -1060,8 +1129,16 @@ noexpand: /* Finally, move the dot and fix up all the old associations. */ debug(("Moving dot at %d,%d to %d,%d\n", dot->x, dot->y, md.newdot->x, md.newdot->y)); - remove_dot(dot); - add_dot(md.newdot); + { +#ifdef STANDALONE_PICTURE_GENERATOR + int f = dot->flags & F_DOT_BLACK; +#endif + remove_dot(dot); + add_dot(md.newdot); +#ifdef STANDALONE_PICTURE_GENERATOR + md.newdot->flags |= f; +#endif + } md.op = MD_MOVE; ret = foreach_tile(state, movedot_cb, 0, &md); @@ -1140,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, @@ -1188,6 +1263,12 @@ static void generate_pass(game_state *state, random_state *rs, int *scratch, * if we can, and add one if so. */ if (dot_is_possible(state, sp, 0)) { add_dot(sp); +#ifdef STANDALONE_PICTURE_GENERATOR + if (picture) { + if (picture[(sp->y/2) * state->w + (sp->x/2)]) + sp->flags |= F_DOT_BLACK; + } +#endif ret = solver_obvious_dot(state, sp); assert(ret != -1); debug(("Added dot (and obvious associations) at %d,%d\n", @@ -1198,15 +1279,13 @@ static void generate_pass(game_state *state, random_state *rs, int *scratch, dbg_state(state); } -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; char *desc; int *scratch, sz = state->sx*state->sy, i; - int diff, ntries = 0; + int diff, ntries = 0, cc; /* Random list of squares to try and process, one-by-one. */ scratch = snewn(sz, int); @@ -1222,6 +1301,8 @@ generate: game_update_dots(state); + if (state->ndots == 1) goto generate; + #ifdef DEBUGGING { char *tmp = encode_game(state); @@ -1230,6 +1311,12 @@ generate: } #endif + for (i = 0; i < state->sx*state->sy; i++) + if (state->grid[i].type == s_tile) + outline_tile_fordot(state, &state->grid[i], TRUE); + cc = check_complete(state, NULL, NULL); + assert(cc); + copy = dup_game(state); clear_game(copy, 0); dbg_state(copy); @@ -1247,6 +1334,174 @@ generate: ntries < MAXTRIES) goto generate; } +#ifdef STANDALONE_PICTURE_GENERATOR + /* + * Postprocessing pass to prevent excessive numbers of adjacent + * singletons. Iterate over all edges in random shuffled order; + * for each edge that separates two regions, investigate + * whether removing that edge and merging the regions would + * still yield a valid and soluble puzzle. (The two regions + * must also be the same colour, of course.) If so, do it. + * + * This postprocessing pass is slow (due to repeated solver + * invocations), and seems to be unnecessary during normal + * unconstrained game generation. However, when generating a + * game under colour constraints, excessive singletons seem to + * turn up more often, so it's worth doing this. + */ + { + int *posns, nposns; + int i, j, newdiff; + game_state *copy2; + + nposns = params->w * (params->h+1) + params->h * (params->w+1); + posns = snewn(nposns, int); + for (i = j = 0; i < state->sx*state->sy; i++) + if (state->grid[i].type == s_edge) + posns[j++] = i; + assert(j == nposns); + + shuffle(posns, nposns, sizeof(*posns), rs); + + for (i = 0; i < nposns; i++) { + int x, y, x0, y0, x1, y1, cx, cy, cn, cx0, cy0, cx1, cy1, tx, ty; + space *s0, *s1, *ts, *d0, *d1, *dn; + int ok; + + /* Coordinates of edge space */ + x = posns[i] % state->sx; + y = posns[i] / state->sx; + + /* Coordinates of square spaces on either side of edge */ + x0 = ((x+1) & ~1) - 1; /* round down to next odd number */ + y0 = ((y+1) & ~1) - 1; + x1 = 2*x-x0; /* and reflect about x to get x1 */ + y1 = 2*y-y0; + + if (!INGRID(state, x0, y0) || !INGRID(state, x1, y1)) + continue; /* outermost edge of grid */ + s0 = &SPACE(state, x0, y0); + s1 = &SPACE(state, x1, y1); + assert(s0->type == s_tile && s1->type == s_tile); + + if (s0->dotx == s1->dotx && s0->doty == s1->doty) + continue; /* tiles _already_ owned by same dot */ + + d0 = &SPACE(state, s0->dotx, s0->doty); + d1 = &SPACE(state, s1->dotx, s1->doty); + + if ((d0->flags ^ d1->flags) & F_DOT_BLACK) + continue; /* different colours: cannot merge */ + + /* + * Work out where the centre of gravity of the new + * region would be. + */ + cx = d0->nassoc * d0->x + d1->nassoc * d1->x; + cy = d0->nassoc * d0->y + d1->nassoc * d1->y; + cn = d0->nassoc + d1->nassoc; + if (cx % cn || cy % cn) + continue; /* CoG not at integer coordinates */ + cx /= cn; + cy /= cn; + assert(INUI(state, cx, cy)); + + /* + * Ensure that the CoG would actually be _in_ the new + * region, by verifying that all its surrounding tiles + * belong to one or other of our two dots. + */ + cx0 = ((cx+1) & ~1) - 1; /* round down to next odd number */ + cy0 = ((cy+1) & ~1) - 1; + cx1 = 2*cx-cx0; /* and reflect about cx to get cx1 */ + cy1 = 2*cy-cy0; + ok = TRUE; + for (ty = cy0; ty <= cy1; ty += 2) + for (tx = cx0; tx <= cx1; tx += 2) { + ts = &SPACE(state, tx, ty); + assert(ts->type == s_tile); + if ((ts->dotx != d0->x || ts->doty != d0->y) && + (ts->dotx != d1->x || ts->doty != d1->y)) + ok = FALSE; + } + if (!ok) + continue; + + /* + * Verify that for every tile in either source region, + * that tile's image in the new CoG is also in one of + * the two source regions. + */ + for (ty = 1; ty < state->sy; ty += 2) { + for (tx = 1; tx < state->sx; tx += 2) { + int tx1, ty1; + + ts = &SPACE(state, tx, ty); + assert(ts->type == s_tile); + if ((ts->dotx != d0->x || ts->doty != d0->y) && + (ts->dotx != d1->x || ts->doty != d1->y)) + continue; /* not part of these tiles anyway */ + tx1 = 2*cx-tx; + ty1 = 2*cy-ty; + if (!INGRID(state, tx1, ty1)) { + ok = FALSE; + break; + } + ts = &SPACE(state, cx+cx-tx, cy+cy-ty); + if ((ts->dotx != d0->x || ts->doty != d0->y) && + (ts->dotx != d1->x || ts->doty != d1->y)) { + ok = FALSE; + break; + } + } + if (!ok) + break; + } + if (!ok) + continue; + + /* + * Now we're clear to attempt the merge. We take a copy + * of the game state first, so we can revert it easily + * if the resulting puzzle turns out to have become + * insoluble. + */ + copy2 = dup_game(state); + + remove_dot(d0); + remove_dot(d1); + dn = &SPACE(state, cx, cy); + add_dot(dn); + dn->flags |= (d0->flags & F_DOT_BLACK); + for (ty = 1; ty < state->sy; ty += 2) { + for (tx = 1; tx < state->sx; tx += 2) { + ts = &SPACE(state, tx, ty); + assert(ts->type == s_tile); + if ((ts->dotx != d0->x || ts->doty != d0->y) && + (ts->dotx != d1->x || ts->doty != d1->y)) + continue; /* not part of these tiles anyway */ + add_assoc(state, ts, dn); + } + } + + copy = dup_game(state); + clear_game(copy, 0); + dbg_state(copy); + newdiff = solver_state(copy, params->diff); + free_game(copy); + if (diff == newdiff) { + /* Still just as soluble. Let the merge stand. */ + free_game(copy2); + } else { + /* Became insoluble. Revert. */ + free_game(state); + state = copy2; + } + } + sfree(posns); + } +#endif + desc = encode_game(state); #ifndef STANDALONE_SOLVER debug(("new_game_desc generated: \n")); @@ -1259,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: @@ -1273,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); @@ -1321,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); @@ -1333,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) { @@ -1542,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); @@ -1849,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) @@ -1882,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; @@ -1900,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); @@ -1933,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); @@ -1945,6 +2197,13 @@ static int solver_state(game_state *state, int maxdiff) solver_ctx *sctx = new_solver(state); int ret, diff = DIFF_NORMAL; +#ifdef STANDALONE_PICTURE_GENERATOR + /* hack, hack: set picture to NULL during solving so that add_assoc + * won't complain when we attempt recursive guessing and guess wrong */ + int *savepic = picture; + picture = NULL; +#endif + ret = solver_obvious(state); if (ret < 0) { diff = DIFF_IMPOSSIBLE; @@ -1978,7 +2237,7 @@ cont: break; } - if (check_complete(state, 0)) goto got_result; + if (check_complete(state, NULL, NULL)) goto got_result; diff = (maxdiff >= DIFF_UNREASONABLE) ? solver_recurse(state, maxdiff) : DIFF_UNFINISHED; @@ -1986,16 +2245,20 @@ cont: got_result: free_solver(sctx); #ifndef STANDALONE_SOLVER - debug(("solver_state ends:\n")); + debug(("solver_state ends, diff %s:\n", galaxies_diffnames[diff])); dbg_state(state); #endif +#ifdef STANDALONE_PICTURE_GENERATOR + picture = savepic; +#endif + return diff; } #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; @@ -2042,12 +2305,15 @@ struct game_ui { int dx, dy; /* pixel coords of drag pos. */ int dotx, doty; /* grid coords of dot we're dragging from. */ int srcx, srcy; /* grid coords of drag start */ + 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; + ui->cur_x = ui->cur_y = 1; + ui->cur_visible = 0; return ui; } @@ -2056,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) { } @@ -2085,6 +2351,8 @@ static void game_changed_state(game_ui *ui, game_state *oldstate, #define DRAW_WIDTH (BORDER * 2 + ds->w * TILE_SIZE) #define DRAW_HEIGHT (BORDER * 2 + ds->h * TILE_SIZE) +#define CURSOR_SIZE DOT_SIZE + struct game_drawstate { int started; int w, h; @@ -2092,10 +2360,14 @@ struct game_drawstate { unsigned long *grid; int *dx, *dy; blitter *bl; + blitter *blmirror; int dragging, dragx, dragy; int *colour_scratch; + + int cx, cy, cur_visible; + blitter *cur_bl; }; #define CORNER_TOLERANCE 0.15F @@ -2143,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; @@ -2178,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): * @@ -2193,9 +2467,11 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, * Add or remove dot (left-click) */ char buf[80]; - const char *sep; + const char *sep = ""; int px, py; - struct space *sp, *dot; + space *sp, *dot; + + buf[0] = '\0'; if (button == 'H' || button == 'h') { char *ret; @@ -2207,6 +2483,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, } if (button == LEFT_BUTTON) { + ui->cur_visible = 0; coord_round_to_edge(FROMCOORD((float)x), FROMCOORD((float)y), &px, &py); @@ -2221,6 +2498,8 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, } else if (button == RIGHT_BUTTON) { int px1, py1; + ui->cur_visible = 0; + px = (int)(2*FROMCOORD((float)x) + 0.5); py = (int)(2*FROMCOORD((float)y) + 0.5); @@ -2233,7 +2512,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, for (py1 = py-1; py1 <= py+1; py1++) for (px1 = px-1; px1 <= px+1; px1++) { if (px1 >= 0 && px1 < state->sx && - py1 >= 0 && py1 < state->sx && + py1 >= 0 && py1 < state->sy && x >= SCOORD(px1-1) && x < SCOORD(px1+1) && y >= SCOORD(py1-1) && y < SCOORD(py1+1) && SPACE(state, px1, py1).flags & F_DOT) { @@ -2254,7 +2533,7 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, if (!dot) { px = 2*FROMCOORD(x+TILE_SIZE) - 1; py = 2*FROMCOORD(y+TILE_SIZE) - 1; - if (px >= 0 && px < state->sx && py >= 0 && py < state->sx) { + if (px >= 0 && px < state->sx && py >= 0 && py < state->sy) { sp = &SPACE(state, px, py); if (sp->flags & F_TILE_ASSOC) { dot = &SPACE(state, sp->dotx, sp->doty); @@ -2297,9 +2576,6 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, if (px == ui->srcx && py == ui->srcy) return ""; - sep = ""; - buf[0] = '\0'; - /* * Otherwise, we remove the arrow from its starting * square if we didn't start from a dot... @@ -2317,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); } @@ -2326,13 +2602,63 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, return dupstr(buf); else return ""; + } else if (IS_CURSOR_MOVE(button)) { + move_cursor(button, &ui->cur_x, &ui->cur_y, state->sx-1, state->sy-1, 0); + if (ui->cur_x < 1) ui->cur_x = 1; + if (ui->cur_y < 1) ui->cur_y = 1; + ui->cur_visible = 1; + if (ui->dragging) { + ui->dx = SCOORD(ui->cur_x); + ui->dy = SCOORD(ui->cur_y); + } + return ""; + } else if (IS_CURSOR_SELECT(button)) { + if (!ui->cur_visible) { + ui->cur_visible = 1; + return ""; + } + sp = &SPACE(state, ui->cur_x, ui->cur_y); + if (ui->dragging) { + ui->dragging = FALSE; + + if ((ui->srcx != ui->dotx || ui->srcy != ui->doty) && + SPACE(state, ui->srcx, ui->srcy).flags & F_TILE_ASSOC) { + sprintf(buf, "%sU%d,%d", sep, ui->srcx, ui->srcy); + sep = ";"; + } + if (sp->type == s_tile && !(sp->flags & F_DOT) && !(sp->flags & F_TILE_ASSOC)) { + sprintf(buf + strlen(buf), "%sA%d,%d,%d,%d", + sep, ui->cur_x, ui->cur_y, ui->dotx, ui->doty); + } + return dupstr(buf); + } else if (sp->flags & F_DOT) { + ui->dragging = TRUE; + ui->dx = SCOORD(ui->cur_x); + ui->dy = SCOORD(ui->cur_y); + ui->dotx = ui->srcx = ui->cur_x; + ui->doty = ui->srcy = ui->cur_y; + return ""; + } else if (sp->flags & F_TILE_ASSOC) { + assert(sp->type == s_tile); + ui->dragging = TRUE; + ui->dx = SCOORD(ui->cur_x); + ui->dy = SCOORD(ui->cur_y); + ui->dotx = sp->dotx; + ui->doty = sp->doty; + ui->srcx = ui->cur_x; + ui->srcy = ui->cur_y; + return ""; + } else if (sp->type == s_edge) { + sprintf(buf, "E%d,%d", ui->cur_x, ui->cur_y); + return dupstr(buf); + } } return NULL; } #endif -static int check_complete_in_play(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; @@ -2411,10 +2737,16 @@ static int check_complete_in_play(game_state *state, int *dsf, int *colours) */ for (i = 0; i < w*h; i++) if (sqdata[i].valid) { - sqdata[i].cx = sqdata[i].minx + sqdata[i].maxx + 1; - sqdata[i].cy = sqdata[i].miny + sqdata[i].maxy + 1; + int cx, cy; + cx = sqdata[i].cx = sqdata[i].minx + sqdata[i].maxx + 1; + cy = sqdata[i].cy = sqdata[i].miny + sqdata[i].maxy + 1; if (!(SPACE(state, sqdata[i].cx, sqdata[i].cy).flags & F_DOT)) sqdata[i].valid = FALSE; /* no dot at centre of symmetry */ + if (dsf_canonify(dsf, (cy-1)/2*w+(cx-1)/2) != i || + dsf_canonify(dsf, (cy)/2*w+(cx-1)/2) != i || + dsf_canonify(dsf, (cy-1)/2*w+(cx)/2) != i || + dsf_canonify(dsf, (cy)/2*w+(cx)/2) != i) + sqdata[i].valid = FALSE; /* dot at cx,cy isn't ours */ if (SPACE(state, sqdata[i].cx, sqdata[i].cy).flags & F_DOT_BLACK) sqdata[i].colour = 2; else @@ -2503,11 +2835,12 @@ static int check_complete_in_play(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)); @@ -2520,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); @@ -2528,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) { @@ -2554,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; @@ -2563,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); @@ -2576,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; @@ -2591,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; @@ -2599,7 +2941,7 @@ static game_state *execute_move(game_state *state, char *move) else if (*move) goto badmove; } - if (check_complete_in_play(ret, NULL, NULL)) + if (check_complete(ret, NULL, NULL)) ret->completed = 1; return ret; @@ -2623,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; @@ -2637,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; @@ -2645,6 +2987,12 @@ 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); } static float *game_colours(frontend *fe, int *ncolours) @@ -2690,15 +3038,18 @@ 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] = ret[COL_BACKGROUND * 3 + 0] * 1.4F; - if (ret[COL_BACKGROUND * 3 + 2] > 1.0F) ret[COL_BACKGROUND * 3 + 2] = 1.0F; + ret[COL_BACKGROUND * 3 + 2] = min(ret[COL_BACKGROUND * 3 + 0] * 1.4F, 1.0F); #endif + 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; + *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) { struct game_drawstate *ds = snew(struct game_drawstate); int i; @@ -2714,17 +3065,24 @@ 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; ds->colour_scratch = snewn(ds->w * ds->h, int); + ds->cur_bl = NULL; + ds->cx = ds->cy = 0; + ds->cur_visible = 0; + return ds; } 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); @@ -2743,7 +3101,8 @@ static void game_free_drawstate(drawing *dr, game_drawstate *ds) #define DRAW_WHITE 0x0100 #define DRAW_BLACK 0x0200 #define DRAW_ARROW 0x0400 -#define DOT_SHIFT_C 11 +#define DRAW_CURSOR 0x0800 +#define DOT_SHIFT_C 12 #define DOT_SHIFT_M 2 #define DOT_WHITE 1UL #define DOT_BLACK 2UL @@ -2753,7 +3112,7 @@ static void game_free_drawstate(drawing *dr, game_drawstate *ds) * (ddx,ddy). (I.e. pointing at the point (cx+ddx, cy+ddy). */ static void draw_arrow(drawing *dr, game_drawstate *ds, - int cx, int cy, int ddx, int ddy) + int cx, int cy, int ddx, int ddy, int col) { float vlen = (float)sqrt(ddx*ddx+ddy*ddy); float xdx = ddx/vlen, xdy = ddy/vlen; @@ -2763,9 +3122,9 @@ static void draw_arrow(drawing *dr, game_drawstate *ds, int adx = (int)((ydx-xdx)*TILE_SIZE/8), ady = (int)((ydy-xdy)*TILE_SIZE/8); int adx2 = (int)((-ydx-xdx)*TILE_SIZE/8), ady2 = (int)((-ydy-xdy)*TILE_SIZE/8); - draw_line(dr, e1x, e1y, e2x, e2y, COL_ARROW); - draw_line(dr, e1x, e1y, e1x+adx, e1y+ady, COL_ARROW); - draw_line(dr, e1x, e1y, e1x+adx2, e1y+ady2, COL_ARROW); + draw_line(dr, e1x, e1y, e2x, e2y, col); + draw_line(dr, e1x, e1y, e1x+adx, e1y+ady, col); + draw_line(dr, e1x, e1y, e1x+adx2, e1y+ady2, col); } static void draw_square(drawing *dr, game_drawstate *ds, int x, int y, @@ -2792,10 +3151,17 @@ static void draw_square(drawing *dr, game_drawstate *ds, int x, int y, draw_rect(dr, lx, ly, TILE_SIZE, 1, gridcol); /* - * Draw the arrow. + * Draw the arrow, if present, or the cursor, if here. */ if (flags & DRAW_ARROW) - draw_arrow(dr, ds, lx + TILE_SIZE/2, ly + TILE_SIZE/2, ddx, ddy); + draw_arrow(dr, ds, lx + TILE_SIZE/2, ly + TILE_SIZE/2, ddx, ddy, + (flags & DRAW_CURSOR) ? COL_CURSOR : COL_ARROW); + else if (flags & DRAW_CURSOR) + draw_rect_outline(dr, + lx + TILE_SIZE/2 - CURSOR_SIZE, + ly + TILE_SIZE/2 - CURSOR_SIZE, + 2*CURSOR_SIZE+1, 2*CURSOR_SIZE+1, + COL_CURSOR); /* * Draw the edges. @@ -2842,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); @@ -2856,10 +3234,23 @@ 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) { + assert(ds->cur_bl); + blitter_load(dr, ds->cur_bl, ds->cx, ds->cy); + draw_update(dr, ds->cx, ds->cy, CURSOR_SIZE*2+1, CURSOR_SIZE*2+1); + ds->cur_visible = FALSE; + } if (!ds->started) { draw_rect(dr, 0, 0, DRAW_WIDTH, DRAW_HEIGHT, COL_BACKGROUND); @@ -2870,13 +3261,13 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, ds->started = TRUE; } - check_complete_in_play(state, NULL, ds->colour_scratch); + check_complete(state, NULL, ds->colour_scratch); for (y = 0; y < h; y++) for (x = 0; x < w; x++) { unsigned long flags = 0; int ddx = 0, ddy = 0; - space *sp; + space *sp, *opp; int dx, dy; /* @@ -2914,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); @@ -2929,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); @@ -2952,6 +3350,15 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, } } + /* + * Now work out if we have to draw a cursor for this square; + * cursors-on-lines are taken care of below. + */ + if (ui->cur_visible && + ui->cur_x == x*2+1 && ui->cur_y == y*2+1 && + !(SPACE(state, x*2+1, y*2+1).flags & F_DOT)) + flags |= DRAW_CURSOR; + /* * Now we have everything we're going to need. Draw the * square. @@ -2966,14 +3373,44 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, } } + /* + * Draw a cursor. This secondary blitter is much less invasive than trying + * to fix up all of the rest of the code with sufficient flags to be able to + * display this sensibly. + */ + if (ui->cur_visible) { + space *sp = &SPACE(state, ui->cur_x, ui->cur_y); + ds->cur_visible = TRUE; + ds->cx = SCOORD(ui->cur_x) - CURSOR_SIZE; + ds->cy = SCOORD(ui->cur_y) - CURSOR_SIZE; + blitter_save(dr, ds->cur_bl, ds->cx, ds->cy); + if (sp->flags & F_DOT) { + /* draw a red dot (over the top of whatever would be there already) */ + draw_circle(dr, SCOORD(ui->cur_x), SCOORD(ui->cur_y), DOT_SIZE, + COL_CURSOR, COL_BLACKDOT); + } else if (sp->type != s_tile) { + /* draw an edge/vertex square; tile cursors are dealt with above. */ + int dx = (ui->cur_x % 2) ? CURSOR_SIZE : CURSOR_SIZE/3; + int dy = (ui->cur_y % 2) ? CURSOR_SIZE : CURSOR_SIZE/3; + int x1 = SCOORD(ui->cur_x)-dx, y1 = SCOORD(ui->cur_y)-dy; + int xs = dx*2+1, ys = dy*2+1; + + draw_rect(dr, x1, y1, xs, ys, COL_CURSOR); + } + draw_update(dr, ds->cx, ds->cy, CURSOR_SIZE*2+1, CURSOR_SIZE*2+1); + } + if (ui->dragging) { 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, - SCOORD(ui->doty) - ui->dy); + 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 { @@ -2987,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)) @@ -3003,13 +3440,18 @@ 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; } #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; @@ -3022,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; @@ -3035,16 +3477,16 @@ static void game_print(drawing *dr, game_state *state, int sz) game_drawstate ads, *ds = &ads; ds->tilesize = sz; - white = print_grey_colour(dr, HATCH_CLEAR, 1.0F); - black = print_grey_colour(dr, HATCH_SOLID, 0.0F); - blackish = print_grey_colour(dr, HATCH_X, 0.5F); + white = print_mono_colour(dr, 1); + black = print_mono_colour(dr, 0); + blackish = print_hatched_colour(dr, HATCH_X); /* * Get the completion information. */ dsf = snewn(w * h, int); colours = snewn(w * h, int); - check_complete_in_play(state, dsf, colours); + check_complete(state, dsf, colours); /* * Draw the grid. @@ -3208,7 +3650,7 @@ const struct game thegame = { #else TRUE, solve_game, #endif - TRUE, game_text_format, + TRUE, game_can_format_as_text_now, game_text_format, new_ui, free_ui, encode_ui, @@ -3223,6 +3665,7 @@ const struct game thegame = { game_redraw, game_anim_length, game_flash_length, + game_status, #ifdef EDITOR FALSE, FALSE, NULL, NULL, TRUE, /* wants_statusbar */ @@ -3407,4 +3850,146 @@ int main(int argc, char **argv) #endif +#ifdef STANDALONE_PICTURE_GENERATOR + +/* + * Main program for the standalone picture generator. To use it, + * simply provide it with an XBM-format bitmap file (note XBM, not + * XPM) on standard input, and it will output a game ID in return. + * For example: + * + * $ ./galaxiespicture < badly-drawn-cat.xbm + * 11x11:eloMBLzFeEzLNMWifhaWYdDbixCymBbBMLoDdewGg + * + * If you want a puzzle with a non-standard difficulty level, pass + * a partial parameters string as a command-line argument (e.g. + * `./galaxiespicture du < foo.xbm', where `du' is the same suffix + * which if it appeared in a random-seed game ID would set the + * difficulty level to Unreasonable). However, be aware that if the + * generator fails to produce an adequately difficult puzzle too + * many times then it will give up and return an easier one (just + * as it does during normal GUI play). To be sure you really have + * the difficulty you asked for, use galaxiessolver to + * double-check. + * + * (Perhaps I ought to include an option to make this standalone + * generator carry on looping until it really does get the right + * difficulty. Hmmm.) + */ + +#include + +int main(int argc, char **argv) +{ + game_params *par; + char *params, *desc; + random_state *rs; + time_t seed = time(NULL); + char buf[4096]; + int i; + int x, y; + + par = default_params(); + if (argc > 1) + decode_params(par, argv[1]); /* get difficulty */ + par->w = par->h = -1; + + /* + * Now read an XBM file from standard input. This is simple and + * hacky and will do very little error detection, so don't feed + * it bogus data. + */ + picture = NULL; + x = y = 0; + while (fgets(buf, sizeof(buf), stdin)) { + buf[strcspn(buf, "\r\n")] = '\0'; + if (!strncmp(buf, "#define", 7)) { + /* + * Lines starting `#define' give the width and height. + */ + char *num = buf + strlen(buf); + char *symend; + + while (num > buf && isdigit((unsigned char)num[-1])) + num--; + symend = num; + while (symend > buf && isspace((unsigned char)symend[-1])) + symend--; + + if (symend-5 >= buf && !strncmp(symend-5, "width", 5)) + par->w = atoi(num); + else if (symend-6 >= buf && !strncmp(symend-6, "height", 6)) + par->h = atoi(num); + } else { + /* + * Otherwise, break the string up into words and take + * any word of the form `0x' plus hex digits to be a + * byte. + */ + char *p, *wordstart; + + if (!picture) { + if (par->w < 0 || par->h < 0) { + printf("failed to read width and height\n"); + return 1; + } + picture = snewn(par->w * par->h, int); + for (i = 0; i < par->w * par->h; i++) + picture[i] = -1; + } + + p = buf; + while (*p) { + while (*p && (*p == ',' || isspace((unsigned char)*p))) + p++; + wordstart = p; + while (*p && !(*p == ',' || *p == '}' || + isspace((unsigned char)*p))) + p++; + if (*p) + *p++ = '\0'; + + if (wordstart[0] == '0' && + (wordstart[1] == 'x' || wordstart[1] == 'X') && + !wordstart[2 + strspn(wordstart+2, + "0123456789abcdefABCDEF")]) { + unsigned long byte = strtoul(wordstart+2, NULL, 16); + for (i = 0; i < 8; i++) { + int bit = (byte >> i) & 1; + if (y < par->h && x < par->w) + picture[y * par->w + x] = bit; + x++; + } + + if (x >= par->w) { + x = 0; + y++; + } + } + } + } + } + + for (i = 0; i < par->w * par->h; i++) + if (picture[i] < 0) { + fprintf(stderr, "failed to read enough bitmap data\n"); + return 1; + } + + rs = random_new((void*)&seed, sizeof(time_t)); + + desc = new_game_desc(par, rs, NULL, FALSE); + params = encode_params(par, FALSE); + printf("%s:%s\n", params, desc); + + sfree(desc); + sfree(params); + free_params(par); + random_free(rs); + + return 0; +} + +#endif + /* vim: set shiftwidth=4 tabstop=8: */