X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/f0ee053c2a4a214a0e77b22462d0c42806d0462b..81ff8295651d75cdd07ff6fbb5a52d8aebe84136:/net.c diff --git a/net.c b/net.c index 59effe6..8bfd899 100644 --- a/net.c +++ b/net.c @@ -12,8 +12,6 @@ #include "puzzles.h" #include "tree234.h" -#define PI 3.141592653589793238462643383279502884197169399 - #define MATMUL(xr,yr,m,x,y) do { \ float rx, ry, xx = (x), yy = (y), *mat = (m); \ rx = mat[0] * xx + mat[2] * yy; \ @@ -28,11 +26,6 @@ #define D 0x08 #define LOCKED 0x10 #define ACTIVE 0x20 -/* Corner flags go in the barriers array */ -#define RU 0x10 -#define UL 0x20 -#define LD 0x40 -#define DR 0x80 /* Rotations: Anticlockwise, Clockwise, Flip, general rotate */ #define A(x) ( (((x) & 0x07) << 1) | (((x) & 0x08) >> 3) ) @@ -50,7 +43,8 @@ #define COUNT(x) ( (((x) & 0x08) >> 3) + (((x) & 0x04) >> 2) + \ (((x) & 0x02) >> 1) + ((x) & 0x01) ) -#define TILE_SIZE 32 +#define PREFERRED_TILE_SIZE 32 +#define TILE_SIZE (ds->tilesize) #define TILE_BORDER 1 #define WINDOW_OFFSET 16 @@ -127,7 +121,7 @@ static int xyd_cmp(const void *av, const void *bv) { if (a->direction > b->direction) return +1; return 0; -}; +} static int xyd_cmp_nc(void *av, void *bv) { return xyd_cmp(av, bv); } @@ -156,32 +150,29 @@ static game_params *default_params(void) return ret; } +static const struct game_params net_presets[] = { + {5, 5, FALSE, TRUE, 0.0}, + {7, 7, FALSE, TRUE, 0.0}, + {9, 9, FALSE, TRUE, 0.0}, + {11, 11, FALSE, TRUE, 0.0}, + {13, 11, FALSE, TRUE, 0.0}, + {5, 5, TRUE, TRUE, 0.0}, + {7, 7, TRUE, TRUE, 0.0}, + {9, 9, TRUE, TRUE, 0.0}, + {11, 11, TRUE, TRUE, 0.0}, + {13, 11, TRUE, TRUE, 0.0}, +}; + static int game_fetch_preset(int i, char **name, game_params **params) { game_params *ret; char str[80]; - static const struct { int x, y, wrap; } values[] = { - {5, 5, FALSE}, - {7, 7, FALSE}, - {9, 9, FALSE}, - {11, 11, FALSE}, - {13, 11, FALSE}, - {5, 5, TRUE}, - {7, 7, TRUE}, - {9, 9, TRUE}, - {11, 11, TRUE}, - {13, 11, TRUE}, - }; - - if (i < 0 || i >= lenof(values)) + + if (i < 0 || i >= lenof(net_presets)) return FALSE; ret = snew(game_params); - ret->width = values[i].x; - ret->height = values[i].y; - ret->wrapping = values[i].wrap; - ret->unique = TRUE; - ret->barrier_probability = 0.0; + *ret = net_presets[i]; sprintf(str, "%dx%d%s", ret->width, ret->height, ret->wrapping ? " wrapping" : ""); @@ -309,12 +300,8 @@ static game_params *custom_params(config_item *cfg) static char *validate_params(game_params *params) { - if (params->width <= 0 && params->height <= 0) + if (params->width <= 0 || params->height <= 0) return "Width and height must both be greater than zero"; - if (params->width <= 0) - return "Width must be greater than zero"; - if (params->height <= 0) - return "Height must be greater than zero"; if (params->width <= 1 && params->height <= 1) return "At least one of width and height must be greater than one"; if (params->barrier_probability < 0) @@ -975,6 +962,7 @@ static void perturb(int w, int h, unsigned char *tiles, int wrapping, break; } + sfree(perim2); if (i == nperim) return; /* nothing we can do! */ @@ -1151,7 +1139,7 @@ static void perturb(int w, int h, unsigned char *tiles, int wrapping, } static char *new_game_desc(game_params *params, random_state *rs, - game_aux_info **aux) + game_aux_info **aux, int interactive) { tree234 *possibilities, *barriertree; int w, h, x, y, cx, cy, nbarriers; @@ -1242,7 +1230,7 @@ static char *new_game_desc(game_params *params, random_state *rs, OFFSET(x2, y2, x1, y1, d1, params); d2 = F(d1); -#ifdef DEBUG +#ifdef GENERATION_DIAGNOSTICS printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n", x1, y1, "0RU3L567D9abcdef"[d1], x2, y2, "0RU3L567D9abcdef"[d2]); #endif @@ -1269,7 +1257,7 @@ static char *new_game_desc(game_params *params, random_state *rs, xydp = find234(possibilities, &xyd1, NULL); if (xydp) { -#ifdef DEBUG +#ifdef GENERATION_DIAGNOSTICS printf("T-piece; removing (%d,%d,%c)\n", xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]); #endif @@ -1296,7 +1284,7 @@ static char *new_game_desc(game_params *params, random_state *rs, xydp = find234(possibilities, &xyd1, NULL); if (xydp) { -#ifdef DEBUG +#ifdef GENERATION_DIAGNOSTICS printf("Loop avoidance; removing (%d,%d,%c)\n", xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]); #endif @@ -1331,7 +1319,7 @@ static char *new_game_desc(game_params *params, random_state *rs, if (index(params, tiles, x3, y3)) continue; /* this would create a loop */ -#ifdef DEBUG +#ifdef GENERATION_DIAGNOSTICS printf("New frontier; adding (%d,%d,%c)\n", x2, y2, "0RU3L567D9abcdef"[d]); #endif @@ -1563,7 +1551,7 @@ static char *validate_desc(game_params *params, char *desc) * Construct an initial game state, given a description and parameters. */ -static game_state *new_game(game_params *params, char *desc) +static game_state *new_game(midend_data *me, game_params *params, char *desc) { game_state *state; int w, h, x, y; @@ -1646,44 +1634,6 @@ static game_state *new_game(game_params *params, char *desc) state->wrapping = TRUE; } - /* - * Set up the barrier corner flags, for drawing barriers - * prettily when they meet. - */ - for (y = 0; y < state->height; y++) { - for (x = 0; x < state->width; x++) { - int dir; - - for (dir = 1; dir < 0x10; dir <<= 1) { - int dir2 = A(dir); - int x1, y1, x2, y2, x3, y3; - int corner = FALSE; - - if (!(barrier(state, x, y) & dir)) - continue; - - if (barrier(state, x, y) & dir2) - corner = TRUE; - - OFFSET(x1, y1, x, y, dir, state); - if (barrier(state, x1, y1) & dir2) - corner = TRUE; - - OFFSET(x2, y2, x, y, dir2, state); - if (barrier(state, x2, y2) & dir) - corner = TRUE; - - if (corner) { - barrier(state, x, y) |= (dir << 4); - barrier(state, x1, y1) |= (A(dir) << 4); - barrier(state, x2, y2) |= (C(dir) << 4); - OFFSET(x3, y3, x1, y1, dir2, state); - barrier(state, x3, y3) |= (F(dir) << 4); - } - } - } - } - return state; } @@ -1716,8 +1666,8 @@ static void free_game(game_state *state) sfree(state); } -static game_state *solve_game(game_state *state, game_aux_info *aux, - char **error) +static game_state *solve_game(game_state *state, game_state *currstate, + game_aux_info *aux, char **error) { game_state *ret; @@ -1837,12 +1787,24 @@ static void free_ui(game_ui *ui) sfree(ui); } +static void game_changed_state(game_ui *ui, game_state *oldstate, + game_state *newstate) +{ +} + +struct game_drawstate { + int started; + int width, height; + int org_x, org_y; + int tilesize; + unsigned char *visible; +}; + /* ---------------------------------------------------------------------- * Process a move. */ static game_state *make_move(game_state *state, game_ui *ui, - int x, int y, int button) -{ + game_drawstate *ds, int x, int y, int button) { game_state *ret, *nullret; int tx, ty, orig; int shift = button & MOD_SHFT, ctrl = button & MOD_CTRL; @@ -1909,10 +1871,11 @@ static game_state *make_move(game_state *state, game_ui *ui, } return state; /* UI activity has occurred */ } else if (button == 'a' || button == 's' || button == 'd' || - button == 'A' || button == 'S' || button == 'D') { + button == 'A' || button == 'S' || button == 'D' || + button == CURSOR_SELECT) { tx = ui->cur_x; ty = ui->cur_y; - if (button == 'a' || button == 'A') + if (button == 'a' || button == 'A' || button == CURSOR_SELECT) button = LEFT_BUTTON; else if (button == 's' || button == 'S') button = MIDDLE_BUTTON; @@ -1990,7 +1953,10 @@ static game_state *make_move(game_state *state, game_ui *ui, ret->last_rotate_dir = 0; /* suppress animation */ ret->last_rotate_x = ret->last_rotate_y = 0; - } else assert(0); + } else { + ret = NULL; /* placate optimisers which don't understand assert(0) */ + assert(0); + } /* * Check whether the game has been completed. @@ -2021,13 +1987,6 @@ static game_state *make_move(game_state *state, game_ui *ui, * Routines for drawing the game position on the screen. */ -struct game_drawstate { - int started; - int width, height; - int org_x, org_y; - unsigned char *visible; -}; - static game_drawstate *game_new_drawstate(game_state *state) { game_drawstate *ds = snew(game_drawstate); @@ -2037,6 +1996,7 @@ static game_drawstate *game_new_drawstate(game_state *state) ds->height = state->height; ds->org_x = ds->org_y = -1; ds->visible = snewn(state->width * state->height, unsigned char); + ds->tilesize = 0; /* undecided yet */ memset(ds->visible, 0xFF, state->width * state->height); return ds; @@ -2048,8 +2008,23 @@ static void game_free_drawstate(game_drawstate *ds) sfree(ds); } -static void game_size(game_params *params, int *x, int *y) +static void game_size(game_params *params, game_drawstate *ds, int *x, int *y, + int expand) { + int tsx, tsy, ts; + /* + * Each window dimension equals the tile size times the grid + * dimension, plus TILE_BORDER, plus twice WINDOW_OFFSET. + */ + tsx = (*x - 2*WINDOW_OFFSET - TILE_BORDER) / params->width; + tsy = (*y - 2*WINDOW_OFFSET - TILE_BORDER) / params->height; + ts = min(tsx, tsy); + + if (expand) + ds->tilesize = ts; + else + ds->tilesize = min(ts, PREFERRED_TILE_SIZE); + *x = WINDOW_OFFSET * 2 + TILE_SIZE * params->width + TILE_BORDER; *y = WINDOW_OFFSET * 2 + TILE_SIZE * params->height + TILE_BORDER; } @@ -2136,37 +2111,32 @@ static void draw_rect_coords(frontend *fe, int x1, int y1, int x2, int y2, /* * draw_barrier_corner() and draw_barrier() are passed physical coords */ -static void draw_barrier_corner(frontend *fe, int x, int y, int dir, int phase, - int barrier) +static void draw_barrier_corner(frontend *fe, game_drawstate *ds, + int x, int y, int dx, int dy, int phase) { int bx = WINDOW_OFFSET + TILE_SIZE * x; int by = WINDOW_OFFSET + TILE_SIZE * y; - int x1, y1, dx, dy, dir2; - - dir >>= 4; + int x1, y1; - dir2 = A(dir); - dx = X(dir) + X(dir2); - dy = Y(dir) + Y(dir2); x1 = (dx > 0 ? TILE_SIZE+TILE_BORDER-1 : 0); y1 = (dy > 0 ? TILE_SIZE+TILE_BORDER-1 : 0); if (phase == 0) { - draw_rect_coords(fe, bx+x1, by+y1, + draw_rect_coords(fe, bx+x1+dx, by+y1, bx+x1-TILE_BORDER*dx, by+y1-(TILE_BORDER-1)*dy, - barrier ? COL_WIRE : COL_BACKGROUND); - draw_rect_coords(fe, bx+x1, by+y1, + COL_WIRE); + draw_rect_coords(fe, bx+x1, by+y1+dy, bx+x1-(TILE_BORDER-1)*dx, by+y1-TILE_BORDER*dy, - barrier ? COL_WIRE : COL_BACKGROUND); + COL_WIRE); } else { draw_rect_coords(fe, bx+x1, by+y1, bx+x1-(TILE_BORDER-1)*dx, by+y1-(TILE_BORDER-1)*dy, - barrier ? COL_BARRIER : COL_BORDER); + COL_BARRIER); } } -static void draw_barrier(frontend *fe, int x, int y, int dir, int phase, - int barrier) +static void draw_barrier(frontend *fe, game_drawstate *ds, + int x, int y, int dir, int phase) { int bx = WINDOW_OFFSET + TILE_SIZE * x; int by = WINDOW_OFFSET + TILE_SIZE * y; @@ -2178,11 +2148,9 @@ static void draw_barrier(frontend *fe, int x, int y, int dir, int phase, h = (Y(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER); if (phase == 0) { - draw_rect(fe, bx+x1-X(dir), by+y1-Y(dir), w, h, - barrier ? COL_WIRE : COL_BACKGROUND); + draw_rect(fe, bx+x1-X(dir), by+y1-Y(dir), w, h, COL_WIRE); } else { - draw_rect(fe, bx+x1, by+y1, w, h, - barrier ? COL_BARRIER : COL_BORDER); + draw_rect(fe, bx+x1, by+y1, w, h, COL_BARRIER); } } @@ -2203,17 +2171,10 @@ static void draw_tile(frontend *fe, game_state *state, game_drawstate *ds, * and including the borders around the tile. This means that * if the neighbouring tiles have connections to those borders, * we must draw those connections on the borders themselves. - * - * This would be terribly fiddly if we ever had to draw a tile - * while its neighbour was in mid-rotate, because we'd have to - * arrange to _know_ that the neighbour was being rotated and - * hence had an anomalous effect on the redraw of this tile. - * Fortunately, the drawing algorithm avoids ever calling us in - * this circumstance: we're either drawing lots of straight - * tiles at game start or after a move is complete, or we're - * repeatedly drawing only the rotating tile. So no problem. */ + clip(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER); + /* * So. First blank the tile out completely: draw a big * rectangle in border colour, and a smaller rectangle in @@ -2361,14 +2322,55 @@ static void draw_tile(frontend *fe, game_state *state, game_drawstate *ds, * Draw barrier corners, and then barriers. */ for (phase = 0; phase < 2; phase++) { - for (dir = 1; dir < 0x10; dir <<= 1) - if (barrier(state, GX(x), GY(y)) & (dir << 4)) - draw_barrier_corner(fe, x, y, dir << 4, phase, TRUE); + for (dir = 1; dir < 0x10; dir <<= 1) { + int x1, y1, corner = FALSE; + /* + * If at least one barrier terminates at the corner + * between dir and A(dir), draw a barrier corner. + */ + if (barrier(state, GX(x), GY(y)) & (dir | A(dir))) { + corner = TRUE; + } else { + /* + * Only count barriers terminating at this corner + * if they're physically next to the corner. (That + * is, if they've wrapped round from the far side + * of the screen, they don't count.) + */ + x1 = x + X(dir); + y1 = y + Y(dir); + if (x1 >= 0 && x1 < state->width && + y1 >= 0 && y1 < state->height && + (barrier(state, GX(x1), GY(y1)) & A(dir))) { + corner = TRUE; + } else { + x1 = x + X(A(dir)); + y1 = y + Y(A(dir)); + if (x1 >= 0 && x1 < state->width && + y1 >= 0 && y1 < state->height && + (barrier(state, GX(x1), GY(y1)) & dir)) + corner = TRUE; + } + } + + if (corner) { + /* + * At least one barrier terminates here. Draw a + * corner. + */ + draw_barrier_corner(fe, ds, x, y, + X(dir)+X(A(dir)), Y(dir)+Y(A(dir)), + phase); + } + } + for (dir = 1; dir < 0x10; dir <<= 1) if (barrier(state, GX(x), GY(y)) & dir) - draw_barrier(fe, x, y, dir, phase, TRUE); + draw_barrier(fe, ds, x, y, dir, phase); } + unclip(fe); + draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER); } @@ -2380,9 +2382,12 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, float angle = 0.0; /* - * Clear the screen if this is our first call. + * Clear the screen, and draw the exterior barrier lines, if + * this is our first call or if the origin has changed. */ - if (!ds->started) { + if (!ds->started || ui->org_x != ds->org_x || ui->org_y != ds->org_y) { + int phase; + ds->started = TRUE; draw_rect(fe, 0, 0, @@ -2390,15 +2395,6 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, WINDOW_OFFSET * 2 + TILE_SIZE * state->height + TILE_BORDER, COL_BACKGROUND); - } - - /* - * If the origin has changed, we need to redraw the exterior - * barrier lines. - */ - if (ui->org_x != ds->org_x || ui->org_y != ds->org_y) { - int phase; - ds->org_x = ui->org_x; ds->org_y = ui->org_y; moved_origin = TRUE; @@ -2406,29 +2402,45 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, draw_update(fe, 0, 0, WINDOW_OFFSET*2 + TILE_SIZE*state->width + TILE_BORDER, WINDOW_OFFSET*2 + TILE_SIZE*state->height + TILE_BORDER); - + for (phase = 0; phase < 2; phase++) { for (x = 0; x < ds->width; x++) { - int ub = barrier(state, GX(x), GY(0)); - int db = barrier(state, GX(x), GY(ds->height-1)); - draw_barrier_corner(fe, x, -1, LD, phase, ub & UL); - draw_barrier_corner(fe, x, -1, DR, phase, ub & RU); - draw_barrier(fe, x, -1, D, phase, ub & U); - draw_barrier_corner(fe, x, ds->height, RU, phase, db & DR); - draw_barrier_corner(fe, x, ds->height, UL, phase, db & LD); - draw_barrier(fe, x, ds->height, U, phase, db & D); + if (x+1 < ds->width) { + if (barrier(state, GX(x), GY(0)) & R) + draw_barrier_corner(fe, ds, x, -1, +1, +1, phase); + if (barrier(state, GX(x), GY(ds->height-1)) & R) + draw_barrier_corner(fe, ds, x, ds->height, +1, -1, phase); + } + if (barrier(state, GX(x), GY(0)) & U) { + draw_barrier_corner(fe, ds, x, -1, -1, +1, phase); + draw_barrier_corner(fe, ds, x, -1, +1, +1, phase); + draw_barrier(fe, ds, x, -1, D, phase); + } + if (barrier(state, GX(x), GY(ds->height-1)) & D) { + draw_barrier_corner(fe, ds, x, ds->height, -1, -1, phase); + draw_barrier_corner(fe, ds, x, ds->height, +1, -1, phase); + draw_barrier(fe, ds, x, ds->height, U, phase); + } } for (y = 0; y < ds->height; y++) { - int lb = barrier(state, GX(0), GY(y)); - int rb = barrier(state, GX(ds->width-1), GY(y)); - draw_barrier_corner(fe, -1, y, RU, phase, lb & UL); - draw_barrier_corner(fe, -1, y, DR, phase, lb & LD); - draw_barrier(fe, -1, y, R, phase, lb & L); - draw_barrier_corner(fe, ds->width, y, UL, phase, rb & RU); - draw_barrier_corner(fe, ds->width, y, LD, phase, rb & DR); - draw_barrier(fe, ds->width, y, L, phase, rb & R); + if (y+1 < ds->height) { + if (barrier(state, GX(0), GY(y)) & D) + draw_barrier_corner(fe, ds, -1, y, +1, +1, phase); + if (barrier(state, GX(ds->width-1), GY(y)) & D) + draw_barrier_corner(fe, ds, ds->width, y, -1, +1, phase); + } + if (barrier(state, GX(0), GY(y)) & L) { + draw_barrier_corner(fe, ds, -1, y, +1, -1, phase); + draw_barrier_corner(fe, ds, -1, y, +1, +1, phase); + draw_barrier(fe, ds, -1, y, R, phase); + } + if (barrier(state, GX(ds->width-1), GY(y)) & R) { + draw_barrier_corner(fe, ds, ds->width, y, -1, -1, phase); + draw_barrier_corner(fe, ds, ds->width, y, -1, +1, phase); + draw_barrier(fe, ds, ds->width, y, L, phase); + } } } } @@ -2528,7 +2540,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, } static float game_anim_length(game_state *oldstate, - game_state *newstate, int dir) + game_state *newstate, int dir, game_ui *ui) { int last_rotate_dir; @@ -2551,7 +2563,7 @@ static float game_anim_length(game_state *oldstate, } static float game_flash_length(game_state *oldstate, - game_state *newstate, int dir) + game_state *newstate, int dir, game_ui *ui) { /* * If the game has just been completed, we display a completion @@ -2575,6 +2587,11 @@ static int game_wants_statusbar(void) return TRUE; } +static int game_timing_state(game_state *state) +{ + return TRUE; +} + #ifdef COMBINED #define thegame net #endif @@ -2599,6 +2616,7 @@ const struct game thegame = { FALSE, game_text_format, new_ui, free_ui, + game_changed_state, make_move, game_size, game_colours, @@ -2608,4 +2626,6 @@ const struct game thegame = { game_anim_length, game_flash_length, game_wants_statusbar, + FALSE, game_timing_state, + 0, /* mouse_priorities */ };