X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=map.c;h=f1af38ba5edaa0d80af92cee5160079b92ad5a0e;hb=db313b3948d27244dd7c34c2609c66d6204d8931;hp=ee3c35b40cfbe3e191afbf99f0d8e0b171f11d9c;hpb=f2ff444fca4da3f63fe0473fc660bb3a5429c2bc;p=sgt-puzzles.git diff --git a/map.c b/map.c index ee3c35b..f1af38b 100644 --- a/map.c +++ b/map.c @@ -7,8 +7,6 @@ * * - clue marking * - better four-colouring algorithm? - * - can we make the pencil marks look nicer? - * - ability to drag a set of pencil marks? */ #include @@ -102,8 +100,13 @@ static game_params *default_params(void) { game_params *ret = snew(game_params); +#ifdef PORTRAIT_SCREEN + ret->w = 16; + ret->h = 18; +#else ret->w = 20; ret->h = 15; +#endif ret->n = 30; ret->diff = DIFF_NORMAL; @@ -111,12 +114,21 @@ static game_params *default_params(void) } static const struct game_params map_presets[] = { +#ifdef PORTRAIT_SCREEN + {16, 18, 30, DIFF_EASY}, + {16, 18, 30, DIFF_NORMAL}, + {16, 18, 30, DIFF_HARD}, + {16, 18, 30, DIFF_RECURSE}, + {25, 30, 75, DIFF_NORMAL}, + {25, 30, 75, DIFF_HARD}, +#else {20, 15, 30, DIFF_EASY}, {20, 15, 30, DIFF_NORMAL}, {20, 15, 30, DIFF_HARD}, {20, 15, 30, DIFF_RECURSE}, {30, 25, 75, DIFF_NORMAL}, {30, 25, 75, DIFF_HARD}, +#endif }; static int game_fetch_preset(int i, char **name, game_params **params) @@ -143,7 +155,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 */ @@ -180,7 +192,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 ret[400]; @@ -191,7 +203,7 @@ static char *encode_params(game_params *params, int full) return dupstr(ret); } -static config_item *game_configure(game_params *params) +static config_item *game_configure(const game_params *params) { config_item *ret; char buf[80]; @@ -229,7 +241,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); @@ -241,7 +253,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 < 2 || params->h < 2) return "Width and height must be at least two"; @@ -804,14 +816,17 @@ static void fourcolour(int *graph, int n, int ngraph, int *colouring, struct solver_scratch { unsigned char *possible; /* bitmap of colours for each region */ + int *graph; + int n; + int ngraph; + int *bfsqueue; int *bfscolour; #ifdef SOLVER_DIAGNOSTICS int *bfsprev; #endif - int n; - int ngraph; + int depth; }; @@ -870,15 +885,22 @@ static int place_colour(struct solver_scratch *sc, int *graph = sc->graph, n = sc->n, ngraph = sc->ngraph; int j, k; - if (!(sc->possible[index] & (1 << colour))) + if (!(sc->possible[index] & (1 << colour))) { +#ifdef SOLVER_DIAGNOSTICS + if (verbose) + printf("%*scannot place %c in region %d\n", 2*sc->depth, "", + colnames[colour], index); +#endif return FALSE; /* can't do it */ + } sc->possible[index] = 1 << colour; colouring[index] = colour; #ifdef SOLVER_DIAGNOSTICS if (verbose) - printf("%s %c in region %d\n", verb, colnames[colour], index); + printf("%*s%s %c in region %d\n", 2*sc->depth, "", + verb, colnames[colour], index); #endif /* @@ -889,7 +911,8 @@ static int place_colour(struct solver_scratch *sc, k = graph[j] - index*n; #ifdef SOLVER_DIAGNOSTICS if (verbose && (sc->possible[k] & (1 << colour))) - printf(" ruling out %c in region %d\n", colnames[colour], k); + printf("%*s ruling out %c in region %d\n", 2*sc->depth, "", + colnames[colour], k); #endif sc->possible[k] &= ~(1 << colour); } @@ -925,24 +948,32 @@ static int map_solver(struct solver_scratch *sc, { int i; - /* - * Initialise scratch space. - */ - for (i = 0; i < n; i++) - sc->possible[i] = (1 << FOUR) - 1; + if (sc->depth == 0) { + /* + * Initialise scratch space. + */ + for (i = 0; i < n; i++) + sc->possible[i] = (1 << FOUR) - 1; - /* - * Place clues. - */ - for (i = 0; i < n; i++) - if (colouring[i] >= 0) { - if (!place_colour(sc, colouring, i, colouring[i] + /* + * Place clues. + */ + for (i = 0; i < n; i++) + if (colouring[i] >= 0) { + if (!place_colour(sc, colouring, i, colouring[i] #ifdef SOLVER_DIAGNOSTICS - , "initial clue:" + , "initial clue:" #endif - )) - return 0; /* the clues aren't even consistent! */ - } + )) { +#ifdef SOLVER_DIAGNOSTICS + if (verbose) + printf("%*sinitial clue set is inconsistent\n", + 2*sc->depth, ""); +#endif + return 0; /* the clues aren't even consistent! */ + } + } + } /* * Now repeatedly loop until we find nothing further to do. @@ -960,21 +991,35 @@ static int map_solver(struct solver_scratch *sc, for (i = 0; i < n; i++) if (colouring[i] < 0) { int p = sc->possible[i]; - if (p == 0) + if (p == 0) { +#ifdef SOLVER_DIAGNOSTICS + if (verbose) + printf("%*sregion %d has no possible colours left\n", + 2*sc->depth, "", i); +#endif return 0; /* puzzle is inconsistent */ + } if ((p & (p-1)) == 0) { /* p is a power of two */ - int c; + int c, ret; for (c = 0; c < FOUR; c++) if (p == (1 << c)) break; assert(c < FOUR); - if (!place_colour(sc, colouring, i, c + ret = place_colour(sc, colouring, i, c #ifdef SOLVER_DIAGNOSTICS - , "placing" + , "placing" #endif - )) - return 0; /* found puzzle to be inconsistent */ + ); + /* + * place_colour() can only fail if colour c was not + * even a _possibility_ for region i, and we're + * pretty sure it was because we checked before + * calling place_colour(). So we can safely assert + * here rather than having to return a nice + * friendly error code. + */ + assert(ret); done_something = TRUE; } } @@ -1041,11 +1086,12 @@ static int map_solver(struct solver_scratch *sc, if (verbose) { char buf[80]; if (!started) - printf("adjacent regions %d,%d share colours %s\n", - j1, j2, colourset(buf, v)); + printf("%*sadjacent regions %d,%d share colours" + " %s\n", 2*sc->depth, "", j1, j2, + colourset(buf, v)); started = TRUE; - printf(" ruling out %s in region %d\n", - colourset(buf, sc->possible[k] & v), k); + printf("%*s ruling out %s in region %d\n",2*sc->depth, + "", colourset(buf, sc->possible[k] & v), k); } #endif sc->possible[k] &= ~v; @@ -1176,13 +1222,15 @@ static int map_solver(struct solver_scratch *sc, char buf[80], *sep = ""; int r; - printf("forcing chain, colour %s, ", + printf("%*sforcing chain, colour %s, ", + 2*sc->depth, "", colourset(buf, origc)); for (r = j; r != -1; r = sc->bfsprev[r]) { printf("%s%d", sep, r); sep = "-"; } - printf("\n ruling out %s in region %d\n", + printf("\n%*s ruling out %s in region" + " %d\n", 2*sc->depth, "", colourset(buf, origc), k); } #endif @@ -1206,14 +1254,25 @@ static int map_solver(struct solver_scratch *sc, for (i = 0; i < n; i++) if (colouring[i] < 0) break; - if (i == n) + if (i == n) { +#ifdef SOLVER_DIAGNOSTICS + if (verbose) + printf("%*sone solution found\n", 2*sc->depth, ""); +#endif return 1; /* success! */ + } /* * If recursion is not permissible, we now give up. */ - if (difficulty < DIFF_RECURSE) + if (difficulty < DIFF_RECURSE) { +#ifdef SOLVER_DIAGNOSTICS + if (verbose) + printf("%*sunable to proceed further without recursion\n", + 2*sc->depth, ""); +#endif return 2; /* unable to complete */ + } /* * Now we've got to do something recursive. So first hunt for a @@ -1247,6 +1306,11 @@ static int map_solver(struct solver_scratch *sc, assert(best >= 0); /* or we'd be solved already */ +#ifdef SOLVER_DIAGNOSTICS + if (verbose) + printf("%*srecursing on region %d\n", 2*sc->depth, "", best); +#endif + /* * Now iterate over the possible colours for this region. */ @@ -1262,11 +1326,27 @@ static int map_solver(struct solver_scratch *sc, if (!(sc->possible[best] & (1 << i))) continue; + memcpy(rsc->possible, sc->possible, n); memcpy(subcolouring, origcolouring, n * sizeof(int)); - subcolouring[best] = i; + + place_colour(rsc, subcolouring, best, i +#ifdef SOLVER_DIAGNOSTICS + , "trying" +#endif + ); + subret = map_solver(rsc, graph, n, ngraph, subcolouring, difficulty); +#ifdef SOLVER_DIAGNOSTICS + if (verbose) { + printf("%*sretracting %c in region %d; found %s\n", + 2*sc->depth, "", colnames[i], best, + subret == 0 ? "no solutions" : + subret == 1 ? "one solution" : "multiple solutions"); + } +#endif + /* * If this possibility turned up more than one valid * solution, or if it turned up one and we already had @@ -1293,9 +1373,18 @@ static int map_solver(struct solver_scratch *sc, */ } + sfree(origcolouring); sfree(subcolouring); free_scratch(rsc); +#ifdef SOLVER_DIAGNOSTICS + if (verbose && sc->depth == 0) { + printf("%*s%s found\n", + 2*sc->depth, "", + ret == 0 ? "no solutions" : + ret == 1 ? "one solution" : "multiple solutions"); + } +#endif return ret; } } @@ -1304,7 +1393,7 @@ static int map_solver(struct solver_scratch *sc, * Game generation main function. */ -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) { struct solver_scratch *sc = NULL; @@ -1615,14 +1704,14 @@ static char *new_game_desc(game_params *params, random_state *rs, return ret; } -static char *parse_edge_list(game_params *params, char **desc, int *map) +static char *parse_edge_list(const game_params *params, const char **desc, + int *map) { int w = params->w, h = params->h, wh = w*h, n = params->n; int i, k, pos, state; - char *p = *desc; + const char *p = *desc; - for (i = 0; i < wh; i++) - map[wh+i] = i; + dsf_init(map+wh, wh); pos = -1; state = 0; @@ -1692,7 +1781,7 @@ static char *parse_edge_list(game_params *params, char **desc, int *map) return NULL; } -static char *validate_desc(game_params *params, char *desc) +static char *validate_desc(const game_params *params, const char *desc) { int w = params->w, h = params->h, wh = w*h, n = params->n; int area; @@ -1701,9 +1790,9 @@ static char *validate_desc(game_params *params, char *desc) map = snewn(2*wh, int); ret = parse_edge_list(params, &desc, map); + sfree(map); if (ret) return ret; - sfree(map); if (*desc != ',') return "Expected comma before clue list"; @@ -1727,11 +1816,12 @@ static char *validate_desc(game_params *params, char *desc) return NULL; } -static game_state *new_game(midend *me, game_params *params, char *desc) +static game_state *new_game(midend *me, const game_params *params, + const char *desc) { int w = params->w, h = params->h, wh = w*h, n = params->n; int i, pos; - char *p; + const char *p; game_state *state = snew(game_state); state->p = *params; @@ -1794,7 +1884,7 @@ static game_state *new_game(midend *me, game_params *params, char *desc) * outlines by the judicious use of diagonally divided squares. */ { - random_state *rs = random_init(desc, strlen(desc)); + random_state *rs = random_new(desc, strlen(desc)); int *squares = snewn(wh, int); int done_something; @@ -1863,7 +1953,7 @@ static game_state *new_game(midend *me, game_params *params, char *desc) for (i = 0; i < state->map->ngraph + n; i++) { bestx[i] = besty[i] = -1; - best[i] = 2*(w+h)+1; + best[i] = (float)(2*(w+h)+1); ax[i] = ay[i] = 0.0F; an[i] = 0; } @@ -2003,7 +2093,7 @@ static game_state *new_game(midend *me, game_params *params, char *desc) */ ax[gindex] += ex[i]; ay[gindex] += ey[i]; - an[gindex] += 1.0F; + an[gindex] += 1; } else { /* * In pass 1, work out whether this @@ -2015,7 +2105,7 @@ static game_state *new_game(midend *me, game_params *params, char *desc) assert(an[gindex] > 0); dx = ex[i] - ax[gindex]; dy = ey[i] - ay[gindex]; - d = sqrt(dx*dx + dy*dy); + d = (float)sqrt(dx*dx + dy*dy); if (d < best[gindex]) { best[gindex] = d; bestx[gindex] = ex[i]; @@ -2066,7 +2156,7 @@ static game_state *new_game(midend *me, game_params *params, char *desc) return state; } -static game_state *dup_game(game_state *state) +static game_state *dup_game(const game_state *state) { game_state *ret = snew(game_state); @@ -2095,12 +2185,13 @@ static void free_game(game_state *state) sfree(state->map->regiony); sfree(state->map); } + sfree(state->pencil); sfree(state->colouring); sfree(state); } -static char *solve_game(game_state *state, game_state *currstate, - char *aux, char **error) +static char *solve_game(const game_state *state, const game_state *currstate, + const char *aux, char **error) { if (!aux) { /* @@ -2159,23 +2250,42 @@ static char *solve_game(game_state *state, game_state *currstate, return dupstr(aux); } -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) { return NULL; } struct game_ui { - int drag_colour; /* -1 means no drag active */ + /* + * drag_colour: + * + * - -2 means no drag currently active. + * - >=0 means we're dragging a solid colour. + * - -1 means we're dragging a blank space, and drag_pencil + * might or might not add some pencil-mark stipples to that. + */ + int drag_colour; + int drag_pencil; int dragx, dragy; int show_numbers; + + int cur_x, cur_y, cur_visible, cur_moved, cur_lastmove; }; -static game_ui *new_ui(game_state *state) +static game_ui *new_ui(const game_state *state) { game_ui *ui = snew(game_ui); ui->dragx = ui->dragy = -1; ui->drag_colour = -2; + ui->drag_pencil = 0; ui->show_numbers = FALSE; + ui->cur_x = ui->cur_y = ui->cur_visible = ui->cur_moved = 0; + ui->cur_lastmove = 0; return ui; } @@ -2184,17 +2294,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) { } @@ -2221,8 +2331,21 @@ struct game_drawstate { #define COORD(x) ( (x) * TILESIZE + BORDER ) #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 ) -static int region_from_coords(game_state *state, game_drawstate *ds, - int x, int y) + /* + * EPSILON_FOO are epsilons added to absolute cursor position by + * cursor movement, such that in pathological cases (e.g. a very + * small diamond-shaped area) it's relatively easy to select the + * region you wanted. + */ + +#define EPSILON_X(button) (((button) == CURSOR_RIGHT) ? +1 : \ + ((button) == CURSOR_LEFT) ? -1 : 0) +#define EPSILON_Y(button) (((button) == CURSOR_DOWN) ? +1 : \ + ((button) == CURSOR_UP) ? -1 : 0) + + +static int region_from_coords(const game_state *state, + const game_drawstate *ds, int x, int y) { int w = state->p.w, h = state->p.h, wh = w*h /*, n = state->p.n */; int tx = FROMCOORD(x), ty = FROMCOORD(y); @@ -2240,10 +2363,12 @@ static int region_from_coords(game_state *state, game_drawstate *ds, return state->map->map[quadrant * wh + ty*w+tx]; } -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]; + char *bufp, buf[256]; + int alt_button; /* * Enable or disable numeric labels on regions. @@ -2253,15 +2378,58 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, return ""; } + if (IS_CURSOR_MOVE(button)) { + move_cursor(button, &ui->cur_x, &ui->cur_y, state->p.w, state->p.h, 0); + ui->cur_visible = 1; + ui->cur_moved = 1; + ui->cur_lastmove = button; + ui->dragx = COORD(ui->cur_x) + TILESIZE/2 + EPSILON_X(button); + ui->dragy = COORD(ui->cur_y) + TILESIZE/2 + EPSILON_Y(button); + return ""; + } + if (IS_CURSOR_SELECT(button)) { + if (!ui->cur_visible) { + ui->dragx = COORD(ui->cur_x) + TILESIZE/2 + EPSILON_X(ui->cur_lastmove); + ui->dragy = COORD(ui->cur_y) + TILESIZE/2 + EPSILON_Y(ui->cur_lastmove); + ui->cur_visible = 1; + return ""; + } + if (ui->drag_colour == -2) { /* not currently cursor-dragging, start. */ + int r = region_from_coords(state, ds, ui->dragx, ui->dragy); + if (r >= 0) { + ui->drag_colour = state->colouring[r]; + ui->drag_pencil = (ui->drag_colour >= 0) ? 0 : state->pencil[r]; + } else { + ui->drag_colour = -1; + ui->drag_pencil = 0; + } + ui->cur_moved = 0; + return ""; + } else { /* currently cursor-dragging; drop the colour in the new region. */ + x = COORD(ui->cur_x) + TILESIZE/2 + EPSILON_X(ui->cur_lastmove); + y = COORD(ui->cur_y) + TILESIZE/2 + EPSILON_Y(ui->cur_lastmove); + alt_button = (button == CURSOR_SELECT2) ? 1 : 0; + /* Double-select removes current colour. */ + if (!ui->cur_moved) ui->drag_colour = -1; + goto drag_dropped; + } + } + if (button == LEFT_BUTTON || button == RIGHT_BUTTON) { int r = region_from_coords(state, ds, x, y); - if (r >= 0) + if (r >= 0) { ui->drag_colour = state->colouring[r]; - else + ui->drag_pencil = state->pencil[r]; + if (ui->drag_colour >= 0) + ui->drag_pencil = 0; /* should be already, but double-check */ + } else { ui->drag_colour = -1; + ui->drag_pencil = 0; + } ui->dragx = x; ui->dragy = y; + ui->cur_visible = 0; return ""; } @@ -2274,14 +2442,23 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, if ((button == LEFT_RELEASE || button == RIGHT_RELEASE) && ui->drag_colour > -2) { + alt_button = (button == RIGHT_RELEASE) ? 1 : 0; + goto drag_dropped; + } + + return NULL; + +drag_dropped: + { int r = region_from_coords(state, ds, x, y); int c = ui->drag_colour; + int p = ui->drag_pencil; + int oldp; /* * Cancel the drag, whatever happens. */ ui->drag_colour = -2; - ui->dragx = ui->dragy = -1; if (r < 0) return ""; /* drag into border; do nothing else */ @@ -2289,21 +2466,41 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, if (state->map->immutable[r]) return ""; /* can't change this region */ - if (state->colouring[r] == c) + if (state->colouring[r] == c && state->pencil[r] == p) return ""; /* don't _need_ to change this region */ - if (button == RIGHT_RELEASE && state->colouring[r] >= 0) - return ""; /* can't pencil on a coloured region */ + if (alt_button) { + if (state->colouring[r] >= 0) { + /* Can't pencil on a coloured region */ + return ""; + } else if (c >= 0) { + /* Right-dragging from colour to blank toggles one pencil */ + p = state->pencil[r] ^ (1 << c); + c = -1; + } + /* Otherwise, right-dragging from blank to blank is equivalent + * to left-dragging. */ + } - sprintf(buf, "%s%c:%d", (button == RIGHT_RELEASE ? "p" : ""), - (int)(c < 0 ? 'C' : '0' + c), r); - return dupstr(buf); - } + bufp = buf; + oldp = state->pencil[r]; + if (c != state->colouring[r]) { + bufp += sprintf(bufp, ";%c:%d", (int)(c < 0 ? 'C' : '0' + c), r); + if (c >= 0) + oldp = 0; + } + if (p != oldp) { + int i; + for (i = 0; i < FOUR; i++) + if ((oldp ^ p) & (1 << i)) + bufp += sprintf(bufp, ";p%c:%d", (int)('0' + i), r); + } - return NULL; + return dupstr(buf+1); /* ignore first semicolon */ + } } -static game_state *execute_move(game_state *state, char *move) +static game_state *execute_move(const game_state *state, const char *move) { int n = state->p.n; game_state *ret = dup_game(state); @@ -2384,8 +2581,8 @@ static game_state *execute_move(game_state *state, char *move) * Drawing routines. */ -static void game_compute_size(game_params *params, int tilesize, - int *x, int *y) +static void game_compute_size(const game_params *params, int tilesize, + int *x, int *y) { /* Ick: fake up `ds->tilesize' for macro expansion purposes */ struct { int tilesize; } ads, *ds = &ads; @@ -2396,26 +2593,33 @@ static void game_compute_size(game_params *params, int tilesize, } static void game_set_size(drawing *dr, game_drawstate *ds, - game_params *params, int tilesize) + const game_params *params, int tilesize) { ds->tilesize = tilesize; - if (ds->bl) - blitter_free(dr, ds->bl); + assert(!ds->bl); /* set_size is never called twice */ ds->bl = blitter_new(dr, TILESIZE+3, TILESIZE+3); } const float map_colours[FOUR][3] = { +#ifdef VIVID_COLOURS + /* Use more vivid colours (e.g. on the Pocket PC) */ + {0.75F, 0.25F, 0.25F}, + {0.3F, 0.7F, 0.3F}, + {0.3F, 0.3F, 0.7F}, + {0.85F, 0.85F, 0.1F}, +#else {0.7F, 0.5F, 0.4F}, {0.8F, 0.7F, 0.4F}, {0.5F, 0.6F, 0.4F}, {0.55F, 0.45F, 0.35F}, +#endif }; const int map_hatching[FOUR] = { HATCH_VERT, HATCH_SLASH, HATCH_HORIZ, HATCH_BACKSLASH }; -static float *game_colours(frontend *fe, game_state *state, int *ncolours) +static float *game_colours(frontend *fe, int *ncolours) { float *ret = snewn(3 * NCOLOURS, float); @@ -2442,7 +2646,7 @@ static float *game_colours(frontend *fe, game_state *state, 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; @@ -2501,12 +2705,12 @@ static void draw_error(drawing *dr, game_drawstate *ds, int x, int y) } static void draw_square(drawing *dr, game_drawstate *ds, - game_params *params, struct map *map, - int x, int y, int v) + const game_params *params, struct map *map, + int x, int y, unsigned long v) { int w = params->w, h = params->h, wh = w*h; - int tv, bv, xo, yo, errs, pencil, i, j, oldj; - int show_numbers; + int tv, bv, xo, yo, i, j, oldj; + unsigned long errs, pencil, show_numbers; errs = v & ERR_MASK; v &= ~ERR_MASK; @@ -2559,7 +2763,9 @@ static void draw_square(drawing *dr, game_drawstate *ds, xo < 2 ? LE : RE); ee = map->map[e * wh + y*w+x]; - c = (yo & 1) * 2 + (xo & 1); + if (xo != (yo * 2 + 1) % 5) + continue; + c = yo; if (!(pencil & ((ee == te ? PENCIL_T_BASE : PENCIL_B_BASE) << c))) continue; @@ -2571,9 +2777,9 @@ static void draw_square(drawing *dr, game_drawstate *ds, (map->map[TE * wh + y*w+x] != map->map[RE * wh + y*w+x])) continue; /* avoid BL-TR diagonal line */ - draw_rect(dr, COORD(x) + (5*xo+1)*TILESIZE/20, - COORD(y) + (5*yo+1)*TILESIZE/20, - 4*TILESIZE/20, 4*TILESIZE/20, COL_0 + c); + draw_circle(dr, COORD(x) + (xo+1)*TILESIZE/5, + COORD(y) + (yo+1)*TILESIZE/5, + TILESIZE/7, COL_0 + c, COL_0 + c); } /* @@ -2628,9 +2834,10 @@ static void draw_square(drawing *dr, game_drawstate *ds, draw_update(dr, COORD(x), COORD(y), TILESIZE, TILESIZE); } -static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, - game_state *state, int dir, game_ui *ui, - float animtime, float flashtime) +static void game_redraw(drawing *dr, game_drawstate *ds, + const game_state *oldstate, const game_state *state, + int dir, const game_ui *ui, + float animtime, float flashtime) { int w = state->p.w, h = state->p.h, wh = w*h, n = state->p.n; int x, y, i; @@ -2675,7 +2882,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, for (x = 0; x < w; x++) { int tv = state->colouring[state->map->map[TE * wh + y*w+x]]; int bv = state->colouring[state->map->map[BE * wh + y*w+x]]; - int v; + unsigned long v; if (tv < 0) tv = FOUR; @@ -2758,7 +2965,7 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, */ for (y = 0; y < h; y++) for (x = 0; x < w; x++) { - int v = ds->todraw[y*w+x]; + unsigned long v = ds->todraw[y*w+x]; if (ds->drawn[y*w+x] != v) { draw_square(dr, ds, &state->p, state->map, x, y, v); ds->drawn[y*w+x] = v; @@ -2768,26 +2975,44 @@ static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, /* * Draw the dragged colour blob if any. */ - if (ui->drag_colour > -2) { + if ((ui->drag_colour > -2) || ui->cur_visible) { + int bg, iscur = 0; + if (ui->drag_colour >= 0) + bg = COL_0 + ui->drag_colour; + else if (ui->drag_colour == -1) { + bg = COL_BACKGROUND; + } else { + int r = region_from_coords(state, ds, ui->dragx, ui->dragy); + int c = (r < 0) ? -1 : state->colouring[r]; + assert(ui->cur_visible); + /*bg = COL_GRID;*/ + bg = (c < 0) ? COL_BACKGROUND : COL_0 + c; + iscur = 1; + } + ds->dragx = ui->dragx - TILESIZE/2 - 2; ds->dragy = ui->dragy - TILESIZE/2 - 2; blitter_save(dr, ds->bl, ds->dragx, ds->dragy); - draw_circle(dr, ui->dragx, ui->dragy, TILESIZE/2, - (ui->drag_colour < 0 ? COL_BACKGROUND : - COL_0 + ui->drag_colour), COL_GRID); + draw_circle(dr, ui->dragx, ui->dragy, + iscur ? TILESIZE/4 : TILESIZE/2, bg, COL_GRID); + for (i = 0; i < FOUR; i++) + if (ui->drag_pencil & (1 << i)) + draw_circle(dr, ui->dragx + ((i*4+2)%10-3) * TILESIZE/10, + ui->dragy + (i*2-3) * TILESIZE/10, + TILESIZE/8, COL_0 + i, COL_0 + i); draw_update(dr, ds->dragx, ds->dragy, TILESIZE + 3, TILESIZE + 3); ds->drag_visible = TRUE; } } -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 && !oldstate->cheated && !newstate->cheated) { @@ -2797,24 +3022,24 @@ static float game_flash_length(game_state *oldstate, game_state *newstate, flash_type = atoi(env); else flash_type = 0; - flash_length = (flash_type == 1 ? 0.50 : 0.30); + flash_length = (flash_type == 1 ? 0.50F : 0.30F); } return flash_length; } else return 0.0F; } -static int game_wants_statusbar(void) +static int game_status(const game_state *state) { - return FALSE; + 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; } -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; @@ -2824,11 +3049,11 @@ static void game_print_size(game_params *params, float *x, float *y) * given tile size and then scale. */ game_compute_size(params, 400, &pw, &ph); - *x = pw / 100.0; - *y = ph / 100.0; + *x = pw / 100.0F; + *y = ph / 100.0F; } -static void game_print(drawing *dr, game_state *state, int tilesize) +static void game_print(drawing *dr, const game_state *state, int tilesize) { int w = state->p.w, h = state->p.h, wh = w*h, n = state->p.n; int ink, c[FOUR], i; @@ -2837,12 +3062,14 @@ static void game_print(drawing *dr, game_state *state, int tilesize) /* Ick: fake up `ds->tilesize' for macro expansion purposes */ struct { int tilesize; } ads, *ds = &ads; + /* We can't call game_set_size() here because we don't want a blitter */ ads.tilesize = tilesize; ink = print_mono_colour(dr, 0); for (i = 0; i < FOUR; i++) - c[i] = print_rgb_colour(dr, map_hatching[i], map_colours[i][0], - map_colours[i][1], map_colours[i][2]); + c[i] = print_rgb_hatched_colour(dr, map_colours[i][0], + map_colours[i][1], map_colours[i][2], + map_hatching[i]); coordsize = 0; coords = NULL; @@ -2970,9 +3197,9 @@ static void game_print(drawing *dr, game_state *state, int tilesize) #endif const struct game thegame = { - "Map", "games.map", + "Map", "games.map", "map", default_params, - game_fetch_preset, + game_fetch_preset, NULL, decode_params, encode_params, free_params, @@ -2985,7 +3212,7 @@ const struct game thegame = { dup_game, free_game, TRUE, solve_game, - FALSE, game_text_format, + FALSE, game_can_format_as_text_now, game_text_format, new_ui, free_ui, encode_ui, @@ -3000,53 +3227,15 @@ const struct game thegame = { game_redraw, game_anim_length, game_flash_length, + game_status, TRUE, TRUE, game_print_size, game_print, - game_wants_statusbar, + FALSE, /* wants_statusbar */ FALSE, game_timing_state, - 0, /* mouse_priorities */ + 0, /* flags */ }; #ifdef STANDALONE_SOLVER -#include - -void frontend_default_colour(frontend *fe, float *output) {} -void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize, - int align, int colour, char *text) {} -void draw_rect(drawing *dr, int x, int y, int w, int h, int colour) {} -void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour) {} -void draw_polygon(drawing *dr, int *coords, int npoints, - int fillcolour, int outlinecolour) {} -void draw_circle(drawing *dr, int cx, int cy, int radius, - int fillcolour, int outlinecolour) {} -void clip(drawing *dr, int x, int y, int w, int h) {} -void unclip(drawing *dr) {} -void start_draw(drawing *dr) {} -void draw_update(drawing *dr, int x, int y, int w, int h) {} -void end_draw(drawing *dr) {} -blitter *blitter_new(drawing *dr, int w, int h) {return NULL;} -void blitter_free(drawing *dr, blitter *bl) {} -void blitter_save(drawing *dr, blitter *bl, int x, int y) {} -void blitter_load(drawing *dr, blitter *bl, int x, int y) {} -int print_mono_colour(drawing *dr, int grey) { return 0; } -int print_rgb_colour(drawing *dr, int hatch, float r, float g, float b) -{ return 0; } -void print_line_width(drawing *dr, int width) {} - -void fatal(char *fmt, ...) -{ - va_list ap; - - fprintf(stderr, "fatal error: "); - - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - - fprintf(stderr, "\n"); - exit(1); -} - int main(int argc, char **argv) { game_params *p; @@ -3147,3 +3336,5 @@ int main(int argc, char **argv) } #endif + +/* vim: set shiftwidth=4 tabstop=8: */