X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/df11cd4e43b66b17df44a1e933f5c71361dc13a4..59dae0db4e2110631e1c7f2ea5990f6b406aff6e:/net.c diff --git a/net.c b/net.c index 6253568..767ef31 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; @@ -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) { @@ -1964,6 +1979,7 @@ static char *interpret_move(game_state *state, game_ui *ui, sprintf(buf, "L%d,%d", tx, ty); return dupstr(buf); } else if (button == LEFT_BUTTON || button == RIGHT_BUTTON) { + char buf[80]; /* * The left and right buttons have no effect if clicked on a @@ -1976,7 +1992,6 @@ 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); return dupstr(buf); } else if (button == 'J') { @@ -2730,7 +2745,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,6 +2753,8 @@ const struct game thegame = { FALSE, game_text_format, new_ui, free_ui, + encode_ui, + decode_ui, game_changed_state, interpret_move, execute_move,