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 */
}
}
-static char *encode_params(game_params *params, int full)
+static char *encode_params(const game_params *params, int full)
{
char ret[400];
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];
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);
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";
*/
}
+ sfree(origcolouring);
sfree(subcolouring);
free_scratch(rsc);
* 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;
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;
dsf_init(map+wh, wh);
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;
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";
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;
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;
}
*/
ax[gindex] += ex[i];
ay[gindex] += ey[i];
- an[gindex] += 1.0F;
+ an[gindex] += 1;
} else {
/*
* In pass 1, work out whether this
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];
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);
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) {
/*
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;
}
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;
}
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)
{
}
#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);
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 *bufp, buf[256];
+ int alt_button;
/*
* Enable or disable numeric labels on regions.
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);
}
ui->dragx = x;
ui->dragy = y;
+ ui->cur_visible = 0;
return "";
}
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;
* Cancel the drag, whatever happens.
*/
ui->drag_colour = -2;
- ui->dragx = ui->dragy = -1;
if (r < 0)
return ""; /* drag into border; do nothing else */
if (state->colouring[r] == c && state->pencil[r] == p)
return ""; /* don't _need_ to change this region */
- if (button == RIGHT_RELEASE) {
+ if (alt_button) {
if (state->colouring[r] >= 0) {
/* Can't pencil on a coloured region */
return "";
return dupstr(buf+1); /* ignore first semicolon */
}
-
- return NULL;
}
-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);
* 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;
}
static void game_set_size(drawing *dr, game_drawstate *ds,
- game_params *params, int tilesize)
+ const game_params *params, int tilesize)
{
ds->tilesize = tilesize;
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;
}
static void draw_square(drawing *dr, game_drawstate *ds,
- game_params *params, struct map *map,
+ const game_params *params, struct map *map,
int x, int y, unsigned long v)
{
int w = params->w, h = params->h, wh = w*h;
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;
/*
* 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,
}
}
-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) {
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_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;
}
-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;
* 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;
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;
const struct game thegame = {
"Map", "games.map", "map",
default_params,
- game_fetch_preset,
+ game_fetch_preset, NULL,
decode_params,
encode_params,
free_params,
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,
game_redraw,
game_anim_length,
game_flash_length,
+ game_status,
TRUE, TRUE, game_print_size, game_print,
FALSE, /* wants_statusbar */
FALSE, game_timing_state,
}
#endif
+
+/* vim: set shiftwidth=4 tabstop=8: */