X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/c823052456548bc19a37e35a0bfae944004ef049..cbb5549e12c8edfd8b38b642b7b25c1fb8912c9b:/cube.c diff --git a/cube.c b/cube.c index d9c0cad..d5221e6 100644 --- a/cube.c +++ b/cube.c @@ -6,11 +6,13 @@ #include #include #include +#include #include #include "puzzles.h" const char *const game_name = "Cube"; +const char *const game_winhelp_topic = "games.cube"; const int game_can_configure = TRUE; #define MAXVERTICES 20 @@ -160,7 +162,7 @@ enum { enum { LEFT, RIGHT, UP, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT }; #define GRID_SCALE 48.0F -#define ROLLTIME 0.1F +#define ROLLTIME 0.13F #define SQ(x) ( (x) * (x) ) @@ -276,6 +278,37 @@ game_params *dup_params(game_params *params) return ret; } +game_params *decode_params(char const *string) +{ + game_params *ret = default_params(); + + switch (*string) { + case 't': ret->solid = TETRAHEDRON; string++; break; + case 'c': ret->solid = CUBE; string++; break; + case 'o': ret->solid = OCTAHEDRON; string++; break; + case 'i': ret->solid = ICOSAHEDRON; string++; break; + default: break; + } + ret->d1 = ret->d2 = atoi(string); + while (*string && isdigit(*string)) string++; + if (*string == 'x') { + string++; + ret->d2 = atoi(string); + } + + return ret; +} + +char *encode_params(game_params *params) +{ + char data[256]; + + assert(params->solid >= 0 && params->solid < 4); + sprintf(data, "%c%dx%d", "tcoi"[params->solid], params->d1, params->d2); + + return dupstr(data); +} + static void enum_grid_squares(game_params *params, void (*callback)(void *, struct grid_square *), void *ctx) @@ -285,8 +318,8 @@ static void enum_grid_squares(game_params *params, if (solid->order == 4) { int x, y; - for (x = 0; x < params->d1; x++) - for (y = 0; y < params->d2; y++) { + for (y = 0; y < params->d2; y++) + for (x = 0; x < params->d1; x++) { struct grid_square sq; sq.x = (float)x; @@ -450,24 +483,24 @@ config_item *game_configure(game_params *params) char buf[80]; ret[0].name = "Type of solid"; - ret[0].type = CHOICES; + ret[0].type = C_CHOICES; ret[0].sval = ":Tetrahedron:Cube:Octahedron:Icosahedron"; ret[0].ival = params->solid; ret[1].name = "Width / top"; - ret[1].type = STRING; + ret[1].type = C_STRING; sprintf(buf, "%d", params->d1); ret[1].sval = dupstr(buf); ret[1].ival = 0; ret[2].name = "Height / bottom"; - ret[2].type = STRING; + ret[2].type = C_STRING; sprintf(buf, "%d", params->d2); ret[2].sval = dupstr(buf); ret[2].ival = 0; ret[3].name = NULL; - ret[3].type = ENDCFG; + ret[3].type = C_END; ret[3].sval = NULL; ret[3].ival = 0; @@ -560,7 +593,7 @@ static void classify_grid_square_callback(void *ctx, struct grid_square *sq) data->squareindex++; } -char *new_game_seed(game_params *params) +char *new_game_seed(game_params *params, random_state *rs) { struct grid_data data; int i, j, k, m, area, facesperclass; @@ -605,7 +638,7 @@ char *new_game_seed(game_params *params) for (i = 0; i < data.nclasses; i++) { for (j = 0; j < facesperclass; j++) { - int n = rand_upto(data.nsquares[i]); + int n = random_upto(rs, data.nsquares[i]); assert(!flags[data.gridptrs[i][n]]); flags[data.gridptrs[i][n]] = TRUE; @@ -653,7 +686,7 @@ char *new_game_seed(game_params *params) /* * Choose a non-blue square for the polyhedron. */ - sprintf(p, ":%d", data.gridptrs[0][rand_upto(m)]); + sprintf(p, ",%d", data.gridptrs[0][random_upto(rs, m)]); sfree(data.gridptrs[0]); sfree(flags); @@ -809,6 +842,34 @@ static struct solid *transform_poly(const struct solid *solid, int flip, return ret; } +char *validate_seed(game_params *params, char *seed) +{ + int area = grid_area(params->d1, params->d2, solids[params->solid]->order); + int i, j; + + i = (area + 3) / 4; + for (j = 0; j < i; j++) { + int c = seed[j]; + if (c >= '0' && c <= '9') continue; + if (c >= 'A' && c <= 'F') continue; + if (c >= 'a' && c <= 'f') continue; + return "Not enough hex digits at start of string"; + /* NB if seed[j]=='\0' that will also be caught here, so we're safe */ + } + + if (seed[i] != ',') + return "Expected ',' after hex digits"; + + i++; + do { + if (seed[i] < '0' || seed[i] > '9') + return "Expected decimal integer after ','"; + i++; + } while (seed[i]); + + return NULL; +} + game_state *new_game(game_params *params, char *seed) { game_state *state = snew(game_state); @@ -855,7 +916,7 @@ game_state *new_game(game_params *params, char *seed) j = 8; } - if (*p == ':') + if (*p == ',') p++; state->current = atoi(p); @@ -922,7 +983,16 @@ void free_game(game_state *state) sfree(state); } -game_state *make_move(game_state *from, int x, int y, int button) +game_ui *new_ui(game_state *state) +{ + return NULL; +} + +void free_ui(game_ui *ui) +{ +} + +game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button) { int direction; int pkey[2], skey[2], dkey[2]; @@ -1281,7 +1351,8 @@ void game_free_drawstate(game_drawstate *ds) } void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, - game_state *state, float animtime, float flashtime) + game_state *state, int dir, game_ui *ui, + float animtime, float flashtime) { int i, j; struct bbox bb = find_bbox(&state->params); @@ -1295,13 +1366,15 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, draw_rect(fe, 0, 0, (int)((bb.r-bb.l+2.0F) * GRID_SCALE), (int)((bb.d-bb.u+2.0F) * GRID_SCALE), COL_BACKGROUND); - if (oldstate && oldstate->movecount > state->movecount) { + if (dir < 0) { game_state *t; /* * This is an Undo. So reverse the order of the states, and * run the roll timer backwards. */ + assert(oldstate); + t = oldstate; oldstate = state; state = t; @@ -1439,12 +1512,12 @@ void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, } } -float game_anim_length(game_state *oldstate, game_state *newstate) +float game_anim_length(game_state *oldstate, game_state *newstate, int dir) { return ROLLTIME; } -float game_flash_length(game_state *oldstate, game_state *newstate) +float game_flash_length(game_state *oldstate, game_state *newstate, int dir) { return 0.0F; }