X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/df11cd4e43b66b17df44a1e933f5c71361dc13a4..fdb3b29aacf3f9d0bf16c816ba2ec959e6518bd3:/net.c diff --git a/net.c b/net.c index 6253568..4fada1c 100644 --- a/net.c +++ b/net.c @@ -77,11 +77,6 @@ struct game_params { float barrier_probability; }; -struct game_aux_info { - int width, height; - unsigned char *tiles; -}; - struct game_state { int width, height, wrapping, completed; int last_rotate_x, last_rotate_y, last_rotate_dir; @@ -298,7 +293,7 @@ static game_params *custom_params(config_item *cfg) return ret; } -static char *validate_params(game_params *params) +static char *validate_params(game_params *params, int full) { if (params->width <= 0 || params->height <= 0) return "Width and height must both be greater than zero"; @@ -352,7 +347,7 @@ static char *validate_params(game_params *params) * is at least 2^(number of such rows), and in particular is at * least 2 since there must be at least one such row. [] */ - if (params->unique && params->wrapping && + if (full && params->unique && params->wrapping && (params->width == 2 || params->height == 2)) return "No wrapping puzzle with a width or height of 2 can have" " a unique solution"; @@ -1139,7 +1134,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, int interactive) + char **aux, int interactive) { tree234 *possibilities, *barriertree; int w, h, x, y, cx, cy, nbarriers; @@ -1401,16 +1396,16 @@ static char *new_game_desc(game_params *params, random_state *rs, } /* - * Save the unshuffled grid in an aux_info. + * Save the unshuffled grid in aux. */ { - game_aux_info *solution; + char *solution; + int i; - solution = snew(game_aux_info); - solution->width = w; - solution->height = h; - solution->tiles = snewn(w * h, unsigned char); - memcpy(solution->tiles, tiles, w * h); + solution = snewn(w * h + 1, char); + for (i = 0; i < w * h; i++) + solution[i] = "0123456789abcdef"[tiles[i] & 0xF]; + solution[w*h] = '\0'; *aux = solution; } @@ -1515,12 +1510,6 @@ static char *new_game_desc(game_params *params, random_state *rs, return desc; } -static void game_free_aux_info(game_aux_info *aux) -{ - sfree(aux->tiles); - sfree(aux); -} - static char *validate_desc(game_params *params, char *desc) { int w = params->width, h = params->height; @@ -1667,27 +1656,34 @@ static void free_game(game_state *state) } static char *solve_game(game_state *state, game_state *currstate, - game_aux_info *aux, char **error) + char *aux, char **error) { unsigned char *tiles; char *ret; int retlen, retsize; int i; - int tiles_need_freeing; + + tiles = snewn(state->width * state->height, unsigned char); if (!aux) { /* * Run the internal solver on the provided grid. This might * not yield a complete solution. */ - tiles = snewn(state->width * state->height, unsigned char); memcpy(tiles, state->tiles, state->width * state->height); net_solver(state->width, state->height, tiles, state->barriers, state->wrapping); - tiles_need_freeing = TRUE; } else { - tiles = aux->tiles; - tiles_need_freeing = FALSE; + for (i = 0; i < state->width * state->height; i++) { + int c = aux[i]; + + if (c >= '0' && c <= '9') + tiles[i] = c - '0'; + else if (c >= 'a' && c <= 'f') + tiles[i] = c - 'a' + 10; + else if (c >= 'A' && c <= 'F') + tiles[i] = c - 'A' + 10; + } } /* @@ -1747,6 +1743,8 @@ static char *solve_game(game_state *state, game_state *currstate, ret[retlen] = '\0'; ret = sresize(ret, retlen+1, char); + sfree(tiles); + return ret; } @@ -1846,6 +1844,23 @@ static void free_ui(game_ui *ui) sfree(ui); } +static char *encode_ui(game_ui *ui) +{ + char buf[120]; + /* + * We preserve the origin and centre-point coordinates over a + * serialise. + */ + sprintf(buf, "O%d,%d;C%d,%d", ui->org_x, ui->org_y, ui->cx, ui->cy); + return dupstr(buf); +} + +static void decode_ui(game_ui *ui, char *encoding) +{ + sscanf(encoding, "O%d,%d;C%d,%d", + &ui->org_x, &ui->org_y, &ui->cx, &ui->cy); +} + static void game_changed_state(game_ui *ui, game_state *oldstate, game_state *newstate) { @@ -1866,11 +1881,16 @@ static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, int x, int y, int button) { char *nullret; - int tx, ty; + int tx = -1, ty = -1, dir = 0; int shift = button & MOD_SHFT, ctrl = button & MOD_CTRL; + enum { + NONE, ROTATE_LEFT, ROTATE_180, ROTATE_RIGHT, TOGGLE_LOCK, JUMBLE, + MOVE_ORIGIN, MOVE_SOURCE, MOVE_ORIGIN_AND_SOURCE, MOVE_CURSOR + } action; button &= ~MOD_MASK; nullret = NULL; + action = NONE; if (button == LEFT_BUTTON || button == MIDDLE_BUTTON || @@ -1898,9 +1918,11 @@ static char *interpret_move(game_state *state, game_ui *ui, if (x % TILE_SIZE >= TILE_SIZE - TILE_BORDER || y % TILE_SIZE >= TILE_SIZE - TILE_BORDER) return nullret; + + action = button == LEFT_BUTTON ? ROTATE_LEFT : + button == RIGHT_BUTTON ? ROTATE_RIGHT : TOGGLE_LOCK; } else if (button == CURSOR_UP || button == CURSOR_DOWN || button == CURSOR_RIGHT || button == CURSOR_LEFT) { - int dir; switch (button) { case CURSOR_UP: dir = U; break; case CURSOR_DOWN: dir = D; break; @@ -1908,44 +1930,28 @@ static char *interpret_move(game_state *state, game_ui *ui, case CURSOR_RIGHT: dir = R; break; default: return nullret; } - if (shift) { - /* - * Move origin. - */ - if (state->wrapping) { - OFFSET(ui->org_x, ui->org_y, ui->org_x, ui->org_y, dir, state); - } else return nullret; /* disallowed for non-wrapping grids */ - } - if (ctrl) { - /* - * Change source tile. - */ - OFFSET(ui->cx, ui->cy, ui->cx, ui->cy, dir, state); - } - if (!shift && !ctrl) { - /* - * Move keyboard cursor. - */ - OFFSET(ui->cur_x, ui->cur_y, ui->cur_x, ui->cur_y, dir, state); - ui->cur_visible = TRUE; - } - return ""; /* UI activity has occurred */ + if (shift && ctrl) action = MOVE_ORIGIN_AND_SOURCE; + else if (shift) action = MOVE_ORIGIN; + else if (ctrl) action = MOVE_SOURCE; + else action = MOVE_CURSOR; } else if (button == 'a' || button == 's' || button == 'd' || button == 'A' || button == 'S' || button == 'D' || + button == 'f' || button == 'F' || button == CURSOR_SELECT) { tx = ui->cur_x; ty = ui->cur_y; if (button == 'a' || button == 'A' || button == CURSOR_SELECT) - button = LEFT_BUTTON; + action = ROTATE_LEFT; else if (button == 's' || button == 'S') - button = MIDDLE_BUTTON; + action = TOGGLE_LOCK; else if (button == 'd' || button == 'D') - button = RIGHT_BUTTON; + action = ROTATE_RIGHT; + else if (button == 'f' || button == 'F') + action = ROTATE_180; ui->cur_visible = TRUE; } else if (button == 'j' || button == 'J') { /* XXX should we have some mouse control for this? */ - button = 'J'; /* canonify */ - tx = ty = -1; /* shut gcc up :( */ + action = JUMBLE; } else return nullret; @@ -1959,11 +1965,13 @@ static char *interpret_move(game_state *state, game_ui *ui, * accident. If they change their mind, another middle click * unlocks it.) */ - if (button == MIDDLE_BUTTON) { + if (action == TOGGLE_LOCK) { char buf[80]; sprintf(buf, "L%d,%d", tx, ty); return dupstr(buf); - } else if (button == LEFT_BUTTON || button == RIGHT_BUTTON) { + } else if (action == ROTATE_LEFT || action == ROTATE_RIGHT || + action == ROTATE_180) { + char buf[80]; /* * The left and right buttons have no effect if clicked on a @@ -1976,10 +1984,10 @@ static char *interpret_move(game_state *state, game_ui *ui, * Otherwise, turn the tile one way or the other. Left button * turns anticlockwise; right button turns clockwise. */ - char buf[80]; - sprintf(buf, "%c%d,%d", (button == LEFT_BUTTON ? 'A' : 'C'), tx, ty); + sprintf(buf, "%c%d,%d", (int)(action == ROTATE_LEFT ? 'A' : + action == ROTATE_RIGHT ? 'C' : 'F'), tx, ty); return dupstr(buf); - } else if (button == 'J') { + } else if (action == JUMBLE) { /* * Jumble all unlocked tiles to random orientations. */ @@ -2012,6 +2020,22 @@ static char *interpret_move(game_state *state, game_ui *ui, ret = sresize(ret, p - ret, char); return ret; + } else if (action == MOVE_ORIGIN || action == MOVE_SOURCE || + action == MOVE_ORIGIN_AND_SOURCE || action == MOVE_CURSOR) { + assert(dir != 0); + if (action == MOVE_ORIGIN || action == MOVE_ORIGIN_AND_SOURCE) { + if (state->wrapping) { + OFFSET(ui->org_x, ui->org_y, ui->org_x, ui->org_y, dir, state); + } else return nullret; /* disallowed for non-wrapping grids */ + } + if (action == MOVE_SOURCE || action == MOVE_ORIGIN_AND_SOURCE) { + OFFSET(ui->cx, ui->cy, ui->cx, ui->cy, dir, state); + } + if (action == MOVE_CURSOR) { + OFFSET(ui->cur_x, ui->cur_y, ui->cur_x, ui->cur_y, dir, state); + ui->cur_visible = TRUE; + } + return ""; } else { return NULL; } @@ -2051,10 +2075,8 @@ static game_state *execute_move(game_state *from, char *move) ret->last_rotate_dir = +1; } else if (move[0] == 'F') { tile(ret, tx, ty) = F(orig); - if (!noanim) { - free_game(ret); - return NULL; - } + if (!noanim) + ret->last_rotate_dir = +2; /* + for sake of argument */ } else if (move[0] == 'C') { tile(ret, tx, ty) = C(orig); if (!noanim) @@ -2131,25 +2153,17 @@ static void game_free_drawstate(game_drawstate *ds) sfree(ds); } -static void game_size(game_params *params, game_drawstate *ds, int *x, int *y, - int expand) +static void game_compute_size(game_params *params, int tilesize, + int *x, int *y) { - 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 + tilesize * params->width + TILE_BORDER; + *y = WINDOW_OFFSET * 2 + tilesize * params->height + TILE_BORDER; +} - *x = WINDOW_OFFSET * 2 + TILE_SIZE * params->width + TILE_BORDER; - *y = WINDOW_OFFSET * 2 + TILE_SIZE * params->height + TILE_BORDER; +static void game_set_size(game_drawstate *ds, game_params *params, + int tilesize) +{ + ds->tilesize = tilesize; } static float *game_colours(frontend *fe, game_state *state, int *ncolours) @@ -2390,8 +2404,7 @@ static void draw_tile(frontend *fe, game_state *state, game_drawstate *ds, points[i+1] = by+(int)(cy+ty); } - draw_polygon(fe, points, 4, TRUE, col); - draw_polygon(fe, points, 4, FALSE, COL_WIRE); + draw_polygon(fe, points, 4, col, COL_WIRE); } /* @@ -2730,7 +2743,6 @@ const struct game thegame = { TRUE, game_configure, custom_params, validate_params, new_game_desc, - game_free_aux_info, validate_desc, new_game, dup_game, @@ -2739,10 +2751,12 @@ const struct game thegame = { FALSE, game_text_format, new_ui, free_ui, + encode_ui, + decode_ui, game_changed_state, interpret_move, execute_move, - game_size, + PREFERRED_TILE_SIZE, game_compute_size, game_set_size, game_colours, game_new_drawstate, game_free_drawstate,