X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/9dce977f1715615afd2e28b67df78eb39ad9a175..HEAD:/galaxies.c diff --git a/galaxies.c b/galaxies.c index 8ac8399..db89700 100644 --- a/galaxies.c +++ b/galaxies.c @@ -81,6 +81,7 @@ enum { COL_GRID, COL_EDGE, COL_ARROW, + COL_CURSOR, NCOLOURS }; @@ -335,6 +336,11 @@ static struct space *sp2dot(game_state *state, int x, int y) #define IS_VERTICAL_EDGE(x) ((x % 2) == 0) +static int game_can_format_as_text_now(game_params *params) +{ + return TRUE; +} + static char *game_text_format(game_state *state) { int maxlen = (state->sx+1)*state->sy, x, y; @@ -349,15 +355,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. */ @@ -607,85 +615,8 @@ 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]. */ +/* 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) { int movelen = 0, movesize = 256, x, y, len; @@ -1299,6 +1230,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, @@ -1307,7 +1239,7 @@ static char *new_game_desc(game_params *params, random_state *rs, 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); @@ -1334,7 +1266,8 @@ generate: for (i = 0; i < state->sx*state->sy; i++) if (state->grid[i].type == s_tile) outline_tile_fordot(state, &state->grid[i], TRUE); - assert(check_complete(state, FALSE)); + cc = check_complete(state, NULL, NULL); + assert(cc); copy = dup_game(state); clear_game(copy, 0); @@ -1517,6 +1450,7 @@ generate: state = copy2; } } + sfree(posns); } #endif @@ -2258,7 +2192,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; @@ -2266,7 +2200,7 @@ 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 @@ -2326,12 +2260,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) { game_ui *ui = snew(game_ui); ui->dragging = FALSE; + ui->cur_x = ui->cur_y = 1; + ui->cur_visible = 0; return ui; } @@ -2369,6 +2306,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; @@ -2380,6 +2319,9 @@ struct game_drawstate { int dragging, dragx, dragy; int *colour_scratch; + + int cx, cy, cur_visible; + blitter *cur_bl; }; #define CORNER_TOLERANCE 0.15F @@ -2427,7 +2369,7 @@ 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, +static char *interpret_move(game_state *state, game_ui *ui, const game_drawstate *ds, int x, int y, int button) { char buf[80]; @@ -2462,7 +2404,7 @@ 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, +static char *interpret_move(game_state *state, game_ui *ui, const game_drawstate *ds, int x, int y, int button) { /* UI operations (play mode): @@ -2477,10 +2419,12 @@ 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; + buf[0] = '\0'; + if (button == 'H' || button == 'h') { char *ret; game_state *tmp = dup_game(state); @@ -2491,6 +2435,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); @@ -2505,6 +2450,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); @@ -2517,7 +2464,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) { @@ -2538,7 +2485,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); @@ -2581,9 +2528,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... @@ -2610,13 +2554,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(game_state *state, int *dsf, int *colours) { int w = state->w, h = state->h; int x, y, i, ret; @@ -2695,10 +2689,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 @@ -2883,7 +2883,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; @@ -2929,6 +2929,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->cur_bl); + ds->cur_bl = blitter_new(dr, TILE_SIZE, TILE_SIZE); } static float *game_colours(frontend *fe, int *ncolours) @@ -2974,10 +2977,13 @@ 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; } @@ -3003,11 +3009,16 @@ static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) 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->bl) blitter_free(dr, ds->bl); sfree(ds->dx); @@ -3027,7 +3038,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 @@ -3037,7 +3049,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; @@ -3047,9 +3059,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, @@ -3076,10 +3088,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. @@ -3144,6 +3163,12 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, draw_update(dr, ds->dragx, ds->dragy, 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); @@ -3154,7 +3179,7 @@ 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++) { @@ -3237,6 +3262,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. */ @@ -3250,6 +3284,33 @@ 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; @@ -3257,7 +3318,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, 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); + SCOORD(ui->doty) - ui->dy, COL_ARROW); } #ifdef EDITOR { @@ -3287,6 +3348,11 @@ static float game_flash_length(game_state *oldstate, game_state *newstate, return 0.0F; } +static int game_status(game_state *state) +{ + return state->completed ? +1 : 0; +} + static int game_timing_state(game_state *state, game_ui *ui) { return TRUE; @@ -3319,16 +3385,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. @@ -3492,7 +3558,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, @@ -3507,6 +3573,7 @@ const struct game thegame = { game_redraw, game_anim_length, game_flash_length, + game_status, #ifdef EDITOR FALSE, FALSE, NULL, NULL, TRUE, /* wants_statusbar */