2 * twiddle.c: Puzzle involving rearranging a grid of squares by
3 * rotating subsquares. Adapted and generalised from a
4 * door-unlocking puzzle in Metroid Prime 2 (the one in the Main
17 #define PREFERRED_TILE_SIZE 48
18 #define TILE_SIZE (ds->tilesize)
19 #define BORDER (TILE_SIZE / 2)
20 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
21 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
22 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
24 #define ANIM_PER_RADIUS_UNIT 0.13F
25 #define FLASH_FRAME 0.13F
49 int just_used_solve; /* used to suppress undo animation */
50 int used_solve; /* used to suppress completion flash */
51 int movecount, movetarget;
52 int lastx, lasty, lastr; /* coordinates of last rotation */
55 static game_params *default_params(void)
57 game_params *ret = snew(game_params);
61 ret->rowsonly = ret->orientable = FALSE;
68 static void free_params(game_params *params)
73 static game_params *dup_params(game_params *params)
75 game_params *ret = snew(game_params);
76 *ret = *params; /* structure copy */
80 static int game_fetch_preset(int i, char **name, game_params **params)
86 { "3x3 rows only", { 3, 3, 2, TRUE, FALSE } },
87 { "3x3 normal", { 3, 3, 2, FALSE, FALSE } },
88 { "3x3 orientable", { 3, 3, 2, FALSE, TRUE } },
89 { "4x4 normal", { 4, 4, 2, FALSE } },
90 { "4x4 orientable", { 4, 4, 2, FALSE, TRUE } },
91 { "4x4 radius 3", { 4, 4, 3, FALSE } },
92 { "5x5 radius 3", { 5, 5, 3, FALSE } },
93 { "6x6 radius 4", { 6, 6, 4, FALSE } },
96 if (i < 0 || i >= lenof(presets))
99 *name = dupstr(presets[i].title);
100 *params = dup_params(&presets[i].params);
105 static void decode_params(game_params *ret, char const *string)
107 ret->w = ret->h = atoi(string);
109 ret->rowsonly = ret->orientable = FALSE;
111 while (*string && isdigit(*string)) string++;
112 if (*string == 'x') {
114 ret->h = atoi(string);
115 while (*string && isdigit(*string)) string++;
117 if (*string == 'n') {
119 ret->n = atoi(string);
120 while (*string && isdigit(*string)) string++;
123 if (*string == 'r') {
124 ret->rowsonly = TRUE;
125 } else if (*string == 'o') {
126 ret->orientable = TRUE;
127 } else if (*string == 'm') {
129 ret->movetarget = atoi(string);
130 while (string[1] && isdigit(string[1])) string++;
136 static char *encode_params(game_params *params, int full)
139 sprintf(buf, "%dx%dn%d%s%s", params->w, params->h, params->n,
140 params->rowsonly ? "r" : "",
141 params->orientable ? "o" : "");
142 /* Shuffle limit is part of the limited parameters, because we have to
143 * supply the target move count. */
144 if (params->movetarget)
145 sprintf(buf + strlen(buf), "m%d", params->movetarget);
149 static config_item *game_configure(game_params *params)
154 ret = snewn(7, config_item);
156 ret[0].name = "Width";
157 ret[0].type = C_STRING;
158 sprintf(buf, "%d", params->w);
159 ret[0].sval = dupstr(buf);
162 ret[1].name = "Height";
163 ret[1].type = C_STRING;
164 sprintf(buf, "%d", params->h);
165 ret[1].sval = dupstr(buf);
168 ret[2].name = "Rotation radius";
169 ret[2].type = C_STRING;
170 sprintf(buf, "%d", params->n);
171 ret[2].sval = dupstr(buf);
174 ret[3].name = "One number per row";
175 ret[3].type = C_BOOLEAN;
177 ret[3].ival = params->rowsonly;
179 ret[4].name = "Orientation matters";
180 ret[4].type = C_BOOLEAN;
182 ret[4].ival = params->orientable;
184 ret[5].name = "Number of shuffling moves";
185 ret[5].type = C_STRING;
186 sprintf(buf, "%d", params->movetarget);
187 ret[5].sval = dupstr(buf);
198 static game_params *custom_params(config_item *cfg)
200 game_params *ret = snew(game_params);
202 ret->w = atoi(cfg[0].sval);
203 ret->h = atoi(cfg[1].sval);
204 ret->n = atoi(cfg[2].sval);
205 ret->rowsonly = cfg[3].ival;
206 ret->orientable = cfg[4].ival;
207 ret->movetarget = atoi(cfg[5].sval);
212 static char *validate_params(game_params *params)
215 return "Rotation radius must be at least two";
216 if (params->w < params->n)
217 return "Width must be at least the rotation radius";
218 if (params->h < params->n)
219 return "Height must be at least the rotation radius";
224 * This function actually performs a rotation on a grid. The `x'
225 * and `y' coordinates passed in are the coordinates of the _top
226 * left corner_ of the rotated region. (Using the centre would have
227 * involved half-integers and been annoyingly fiddly. Clicking in
228 * the centre is good for a user interface, but too inconvenient to
231 static void do_rotate(int *grid, int w, int h, int n, int orientable,
232 int x, int y, int dir)
236 assert(x >= 0 && x+n <= w);
237 assert(y >= 0 && y+n <= h);
240 return; /* nothing to do */
242 grid += y*w+x; /* translate region to top corner */
245 * If we were leaving the result of the rotation in a separate
246 * grid, the simple thing to do would be to loop over each
247 * square within the rotated region and assign it from its
248 * source square. However, to do it in place without taking
249 * O(n^2) memory, we need to be marginally more clever. What
250 * I'm going to do is loop over about one _quarter_ of the
251 * rotated region and permute each element within that quarter
252 * with its rotational coset.
254 * The size of the region I need to loop over is (n+1)/2 by
255 * n/2, which is an obvious exact quarter for even n and is a
256 * rectangle for odd n. (For odd n, this technique leaves out
257 * one element of the square, which is of course the central
258 * one that never moves anyway.)
260 for (i = 0; i < (n+1)/2; i++) {
261 for (j = 0; j < n/2; j++) {
268 p[2] = (n-j-1)*w+(n-i-1);
271 for (k = 0; k < 4; k++)
274 for (k = 0; k < 4; k++) {
275 int v = g[(k+dir) & 3];
277 v ^= ((v+dir) ^ v) & 3; /* alter orientation */
284 * Don't forget the orientation on the centre square, if n is
287 if (orientable && (n & 1)) {
288 int v = grid[n/2*(w+1)];
289 v ^= ((v+dir) ^ v) & 3; /* alter orientation */
294 static int grid_complete(int *grid, int wh, int orientable)
298 for (i = 1; i < wh; i++)
299 if (grid[i] < grid[i-1])
302 for (i = 0; i < wh; i++)
309 static char *new_game_desc(game_params *params, random_state *rs,
310 game_aux_info **aux, int interactive)
313 int w = params->w, h = params->h, n = params->n, wh = w*h;
320 * Set up a solved grid.
322 grid = snewn(wh, int);
323 for (i = 0; i < wh; i++)
324 grid[i] = ((params->rowsonly ? i/w : i) + 1) * 4;
327 * Shuffle it. This game is complex enough that I don't feel up
328 * to analysing its full symmetry properties (particularly at
329 * n=4 and above!), so I'm going to do it the pedestrian way
330 * and simply shuffle the grid by making a long sequence of
331 * randomly chosen moves.
333 total_moves = params->movetarget;
335 /* Add a random move to avoid parity issues. */
336 total_moves = w*h*n*n*2 + random_upto(rs, 2);
340 int rw, rh; /* w/h of rotation centre space */
344 prevmoves = snewn(rw * rh, int);
345 for (i = 0; i < rw * rh; i++)
348 for (i = 0; i < total_moves; i++) {
349 int x, y, r, oldtotal, newtotal, dx, dy;
352 x = random_upto(rs, w - n + 1);
353 y = random_upto(rs, h - n + 1);
354 r = 2 * random_upto(rs, 2) - 1;
357 * See if any previous rotations has happened at
358 * this point which nothing has overlapped since.
359 * If so, ensure we haven't either undone a
360 * previous move or repeated one so many times that
361 * it turns into fewer moves in the inverse
362 * direction (i.e. three identical rotations).
364 oldtotal = prevmoves[y*rw+x];
365 newtotal = oldtotal + r;
368 * Special case here for w==h==n, in which case
369 * there is actually no way to _avoid_ all moves
370 * repeating or undoing previous ones.
372 } while ((w != n || h != n) &&
373 (abs(newtotal) < abs(oldtotal) || abs(newtotal) > 2));
375 do_rotate(grid, w, h, n, params->orientable, x, y, r);
378 * Log the rotation we've just performed at this point,
379 * for inversion detection in the next move.
381 * Also zero a section of the prevmoves array, because
382 * any rotation area which _overlaps_ this one is now
383 * entirely safe to perform further moves in.
385 * Two rotation areas overlap if their top left
386 * coordinates differ by strictly less than n in both
389 prevmoves[y*rw+x] += r;
390 for (dy = -n+1; dy <= n-1; dy++) {
391 if (y + dy < 0 || y + dy >= rh)
393 for (dx = -n+1; dx <= n-1; dx++) {
394 if (x + dx < 0 || x + dx >= rw)
396 if (dx == 0 && dy == 0)
398 prevmoves[(y+dy)*rw+(x+dx)] = 0;
405 } while (grid_complete(grid, wh, params->orientable));
408 * Now construct the game description, by describing the grid
409 * as a simple sequence of integers. They're comma-separated,
410 * unless the puzzle is orientable in which case they're
411 * separated by orientation letters `u', `d', `l' and `r'.
415 for (i = 0; i < wh; i++) {
419 k = sprintf(buf, "%d%c", grid[i] / 4,
420 (char)(params->orientable ? "uldr"[grid[i] & 3] : ','));
422 ret = sresize(ret, retlen + k + 1, char);
423 strcpy(ret + retlen, buf);
426 if (!params->orientable)
427 ret[retlen-1] = '\0'; /* delete last comma */
433 static void game_free_aux_info(game_aux_info *aux)
435 assert(!"Shouldn't happen");
438 static char *validate_desc(game_params *params, char *desc)
441 int w = params->w, h = params->h, wh = w*h;
447 for (i = 0; i < wh; i++) {
448 if (*p < '0' || *p > '9')
449 return "Not enough numbers in string";
450 while (*p >= '0' && *p <= '9')
452 if (!params->orientable && i < wh-1) {
454 return "Expected comma after number";
455 } else if (params->orientable && i < wh) {
456 if (*p != 'l' && *p != 'r' && *p != 'u' && *p != 'd')
457 return "Expected orientation letter after number";
458 } else if (i == wh-1 && *p) {
459 return "Excess junk at end of string";
462 if (*p) p++; /* eat comma */
468 static game_state *new_game(midend_data *me, game_params *params, char *desc)
470 game_state *state = snew(game_state);
471 int w = params->w, h = params->h, n = params->n, wh = w*h;
478 state->orientable = params->orientable;
479 state->completed = 0;
480 state->used_solve = state->just_used_solve = FALSE;
481 state->movecount = 0;
482 state->movetarget = params->movetarget;
483 state->lastx = state->lasty = state->lastr = -1;
485 state->grid = snewn(wh, int);
489 for (i = 0; i < wh; i++) {
490 state->grid[i] = 4 * atoi(p);
491 while (*p >= '0' && *p <= '9')
494 if (params->orientable) {
496 case 'l': state->grid[i] |= 1; break;
497 case 'd': state->grid[i] |= 2; break;
498 case 'r': state->grid[i] |= 3; break;
508 static game_state *dup_game(game_state *state)
510 game_state *ret = snew(game_state);
515 ret->orientable = state->orientable;
516 ret->completed = state->completed;
517 ret->movecount = state->movecount;
518 ret->movetarget = state->movetarget;
519 ret->lastx = state->lastx;
520 ret->lasty = state->lasty;
521 ret->lastr = state->lastr;
522 ret->used_solve = state->used_solve;
523 ret->just_used_solve = state->just_used_solve;
525 ret->grid = snewn(ret->w * ret->h, int);
526 memcpy(ret->grid, state->grid, ret->w * ret->h * sizeof(int));
531 static void free_game(game_state *state)
537 static int compare_int(const void *av, const void *bv)
539 const int *a = (const int *)av;
540 const int *b = (const int *)bv;
549 static game_state *solve_game(game_state *state, game_state *currstate,
550 game_aux_info *aux, char **error)
552 game_state *ret = dup_game(state);
556 * Simply replace the grid with a solved one. For this game,
557 * this isn't a useful operation for actually telling the user
558 * what they should have done, but it is useful for
559 * conveniently being able to get hold of a clean state from
560 * which to practise manoeuvres.
562 qsort(ret->grid, ret->w*ret->h, sizeof(int), compare_int);
563 for (i = 0; i < ret->w*ret->h; i++)
565 ret->used_solve = ret->just_used_solve = TRUE;
566 ret->completed = ret->movecount = 1;
571 static char *game_text_format(game_state *state)
573 char *ret, *p, buf[80];
574 int i, x, y, col, o, maxlen;
577 * First work out how many characters we need to display each
578 * number. We're pretty flexible on grid contents here, so we
579 * have to scan the entire grid.
582 for (i = 0; i < state->w * state->h; i++) {
583 x = sprintf(buf, "%d", state->grid[i] / 4);
584 if (col < x) col = x;
586 o = (state->orientable ? 1 : 0);
589 * Now we know the exact total size of the grid we're going to
590 * produce: it's got h rows, each containing w lots of col+o,
591 * w-1 spaces and a trailing newline.
593 maxlen = state->h * state->w * (col+o+1);
595 ret = snewn(maxlen+1, char);
598 for (y = 0; y < state->h; y++) {
599 for (x = 0; x < state->w; x++) {
600 int v = state->grid[state->w*y+x];
601 sprintf(buf, "%*d", col, v/4);
605 *p++ = "^<v>"[v & 3];
613 assert(p - ret == maxlen);
618 static game_ui *new_ui(game_state *state)
623 static void free_ui(game_ui *ui)
627 static void game_changed_state(game_ui *ui, game_state *oldstate,
628 game_state *newstate)
632 struct game_drawstate {
639 static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
640 int x, int y, int button)
642 int w = from->w, h = from->h, n = from->n, wh = w*h;
646 button = button & (~MOD_MASK | MOD_NUM_KEYPAD);
648 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
650 * Determine the coordinates of the click. We offset by n-1
651 * half-blocks so that the user must click at the centre of
652 * a rotation region rather than at the corner.
654 x -= (n-1) * TILE_SIZE / 2;
655 y -= (n-1) * TILE_SIZE / 2;
658 dir = (button == LEFT_BUTTON ? 1 : -1);
659 if (x < 0 || x > w-n || y < 0 || y > h-n)
661 } else if (button == 'a' || button == 'A' || button==MOD_NUM_KEYPAD+'7') {
663 dir = (button == 'A' ? -1 : +1);
664 } else if (button == 'b' || button == 'B' || button==MOD_NUM_KEYPAD+'9') {
667 dir = (button == 'B' ? -1 : +1);
668 } else if (button == 'c' || button == 'C' || button==MOD_NUM_KEYPAD+'1') {
671 dir = (button == 'C' ? -1 : +1);
672 } else if (button == 'd' || button == 'D' || button==MOD_NUM_KEYPAD+'3') {
675 dir = (button == 'D' ? -1 : +1);
676 } else if (button==MOD_NUM_KEYPAD+'8' && (w-n) % 2 == 0) {
680 } else if (button==MOD_NUM_KEYPAD+'2' && (w-n) % 2 == 0) {
684 } else if (button==MOD_NUM_KEYPAD+'4' && (h-n) % 2 == 0) {
688 } else if (button==MOD_NUM_KEYPAD+'6' && (h-n) % 2 == 0) {
692 } else if (button==MOD_NUM_KEYPAD+'5' && (w-n) % 2 == 0 && (h-n) % 2 == 0){
697 return NULL; /* no move to be made */
701 * This is a valid move. Make it.
703 ret = dup_game(from);
704 ret->just_used_solve = FALSE; /* zero this in a hurry */
706 do_rotate(ret->grid, w, h, n, ret->orientable, x, y, dir);
712 * See if the game has been completed. To do this we simply
713 * test that the grid contents are in increasing order.
715 if (!ret->completed && grid_complete(ret->grid, wh, ret->orientable))
716 ret->completed = ret->movecount;
720 /* ----------------------------------------------------------------------
724 static void game_size(game_params *params, game_drawstate *ds,
725 int *x, int *y, int expand)
729 * Each window dimension equals the tile size times one more
730 * than the grid dimension (the border is half the width of the
733 tsx = *x / (params->w + 1);
734 tsy = *y / (params->h + 1);
739 ds->tilesize = min(ts, PREFERRED_TILE_SIZE);
741 *x = TILE_SIZE * params->w + 2 * BORDER;
742 *y = TILE_SIZE * params->h + 2 * BORDER;
745 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
747 float *ret = snewn(3 * NCOLOURS, float);
751 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
754 * Drop the background colour so that the highlight is
755 * noticeably brighter than it while still being under 1.
757 max = ret[COL_BACKGROUND*3];
758 for (i = 1; i < 3; i++)
759 if (ret[COL_BACKGROUND*3+i] > max)
760 max = ret[COL_BACKGROUND*3+i];
761 if (max * 1.2F > 1.0F) {
762 for (i = 0; i < 3; i++)
763 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
766 for (i = 0; i < 3; i++) {
767 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
768 ret[COL_HIGHLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.1F;
769 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
770 ret[COL_LOWLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
771 ret[COL_TEXT * 3 + i] = 0.0;
774 *ncolours = NCOLOURS;
778 static game_drawstate *game_new_drawstate(game_state *state)
780 struct game_drawstate *ds = snew(struct game_drawstate);
786 ds->bgcolour = COL_BACKGROUND;
787 ds->grid = snewn(ds->w*ds->h, int);
788 ds->tilesize = 0; /* haven't decided yet */
789 for (i = 0; i < ds->w*ds->h; i++)
795 static void game_free_drawstate(game_drawstate *ds)
802 int cx, cy, cw, ch; /* clip region */
803 int ox, oy; /* rotation origin */
804 float c, s; /* cos and sin of rotation angle */
805 int lc, rc, tc, bc; /* colours of tile edges */
808 static void rotate(int *xy, struct rotation *rot)
811 float xf = xy[0] - rot->ox, yf = xy[1] - rot->oy;
814 xf2 = rot->c * xf + rot->s * yf;
815 yf2 = - rot->s * xf + rot->c * yf;
817 xy[0] = xf2 + rot->ox + 0.5; /* round to nearest */
818 xy[1] = yf2 + rot->oy + 0.5; /* round to nearest */
822 static void draw_tile(frontend *fe, game_drawstate *ds, game_state *state,
823 int x, int y, int tile, int flash_colour,
824 struct rotation *rot)
830 * If we've been passed a rotation region but we're drawing a
831 * tile which is outside it, we must draw it normally. This can
832 * occur if we're cleaning up after a completion flash while a
833 * new move is also being made.
835 if (rot && (x < rot->cx || y < rot->cy ||
836 x >= rot->cx+rot->cw || y >= rot->cy+rot->ch))
840 clip(fe, rot->cx, rot->cy, rot->cw, rot->ch);
843 * We must draw each side of the tile's highlight separately,
844 * because in some cases (during rotation) they will all need
845 * to be different colours.
848 /* The centre point is common to all sides. */
849 coords[4] = x + TILE_SIZE / 2;
850 coords[5] = y + TILE_SIZE / 2;
851 rotate(coords+4, rot);
854 coords[0] = x + TILE_SIZE - 1;
855 coords[1] = y + TILE_SIZE - 1;
856 rotate(coords+0, rot);
857 coords[2] = x + TILE_SIZE - 1;
859 rotate(coords+2, rot);
860 draw_polygon(fe, coords, 3, TRUE, rot ? rot->rc : COL_LOWLIGHT);
861 draw_polygon(fe, coords, 3, FALSE, rot ? rot->rc : COL_LOWLIGHT);
865 coords[3] = y + TILE_SIZE - 1;
866 rotate(coords+2, rot);
867 draw_polygon(fe, coords, 3, TRUE, rot ? rot->bc : COL_LOWLIGHT);
868 draw_polygon(fe, coords, 3, FALSE, rot ? rot->bc : COL_LOWLIGHT);
873 rotate(coords+0, rot);
874 draw_polygon(fe, coords, 3, TRUE, rot ? rot->lc : COL_HIGHLIGHT);
875 draw_polygon(fe, coords, 3, FALSE, rot ? rot->lc : COL_HIGHLIGHT);
878 coords[2] = x + TILE_SIZE - 1;
880 rotate(coords+2, rot);
881 draw_polygon(fe, coords, 3, TRUE, rot ? rot->tc : COL_HIGHLIGHT);
882 draw_polygon(fe, coords, 3, FALSE, rot ? rot->tc : COL_HIGHLIGHT);
885 * Now the main blank area in the centre of the tile.
888 coords[0] = x + HIGHLIGHT_WIDTH;
889 coords[1] = y + HIGHLIGHT_WIDTH;
890 rotate(coords+0, rot);
891 coords[2] = x + HIGHLIGHT_WIDTH;
892 coords[3] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
893 rotate(coords+2, rot);
894 coords[4] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
895 coords[5] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
896 rotate(coords+4, rot);
897 coords[6] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
898 coords[7] = y + HIGHLIGHT_WIDTH;
899 rotate(coords+6, rot);
900 draw_polygon(fe, coords, 4, TRUE, flash_colour);
901 draw_polygon(fe, coords, 4, FALSE, flash_colour);
903 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
904 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
909 * Next, the triangles for orientation.
911 if (state->orientable) {
912 int xdx, xdy, ydx, ydy;
913 int cx, cy, displ, displ2;
927 default /* case 3 */:
933 cx = x + TILE_SIZE / 2;
934 cy = y + TILE_SIZE / 2;
935 displ = TILE_SIZE / 2 - HIGHLIGHT_WIDTH - 2;
936 displ2 = TILE_SIZE / 3 - HIGHLIGHT_WIDTH;
938 coords[0] = cx - displ * xdx + displ2 * ydx;
939 coords[1] = cy - displ * xdy + displ2 * ydy;
940 rotate(coords+0, rot);
941 coords[2] = cx + displ * xdx + displ2 * ydx;
942 coords[3] = cy + displ * xdy + displ2 * ydy;
943 rotate(coords+2, rot);
944 coords[4] = cx - displ * ydx;
945 coords[5] = cy - displ * ydy;
946 rotate(coords+4, rot);
947 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT_GENTLE);
948 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT_GENTLE);
951 coords[0] = x + TILE_SIZE/2;
952 coords[1] = y + TILE_SIZE/2;
953 rotate(coords+0, rot);
954 sprintf(str, "%d", tile / 4);
955 draw_text(fe, coords[0], coords[1],
956 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
962 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
965 static int highlight_colour(float angle)
972 COL_HIGHLIGHT_GENTLE,
973 COL_HIGHLIGHT_GENTLE,
974 COL_HIGHLIGHT_GENTLE,
985 COL_HIGHLIGHT_GENTLE,
986 COL_HIGHLIGHT_GENTLE,
987 COL_HIGHLIGHT_GENTLE,
1002 return colours[(int)((angle + 2*PI) / (PI/16)) & 31];
1005 static float game_anim_length(game_state *oldstate, game_state *newstate,
1006 int dir, game_ui *ui)
1008 if ((dir > 0 && newstate->just_used_solve) ||
1009 (dir < 0 && oldstate->just_used_solve))
1012 return ANIM_PER_RADIUS_UNIT * sqrt(newstate->n-1);
1015 static float game_flash_length(game_state *oldstate, game_state *newstate,
1016 int dir, game_ui *ui)
1018 if (!oldstate->completed && newstate->completed &&
1019 !oldstate->used_solve && !newstate->used_solve)
1020 return 2 * FLASH_FRAME;
1025 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1026 game_state *state, int dir, game_ui *ui,
1027 float animtime, float flashtime)
1030 struct rotation srot, *rot;
1031 int lastx = -1, lasty = -1, lastr = -1;
1033 if (flashtime > 0) {
1034 int frame = (int)(flashtime / FLASH_FRAME);
1035 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
1037 bgcolour = COL_BACKGROUND;
1043 TILE_SIZE * state->w + 2 * BORDER,
1044 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
1045 draw_update(fe, 0, 0,
1046 TILE_SIZE * state->w + 2 * BORDER,
1047 TILE_SIZE * state->h + 2 * BORDER);
1050 * Recessed area containing the whole puzzle.
1052 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
1053 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
1054 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
1055 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
1056 coords[4] = coords[2] - TILE_SIZE;
1057 coords[5] = coords[3] + TILE_SIZE;
1058 coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
1059 coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
1060 coords[6] = coords[8] + TILE_SIZE;
1061 coords[7] = coords[9] - TILE_SIZE;
1062 draw_polygon(fe, coords, 5, TRUE, COL_HIGHLIGHT);
1063 draw_polygon(fe, coords, 5, FALSE, COL_HIGHLIGHT);
1065 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
1066 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
1067 draw_polygon(fe, coords, 5, TRUE, COL_LOWLIGHT);
1068 draw_polygon(fe, coords, 5, FALSE, COL_LOWLIGHT);
1074 * If we're drawing any rotated tiles, sort out the rotation
1075 * parameters, and also zap the rotation region to the
1076 * background colour before doing anything else.
1080 float anim_max = game_anim_length(oldstate, state, dir, ui);
1083 lastx = state->lastx;
1084 lasty = state->lasty;
1085 lastr = state->lastr;
1087 lastx = oldstate->lastx;
1088 lasty = oldstate->lasty;
1089 lastr = -oldstate->lastr;
1093 rot->cx = COORD(lastx);
1094 rot->cy = COORD(lasty);
1095 rot->cw = rot->ch = TILE_SIZE * state->n;
1096 rot->ox = rot->cx + rot->cw/2;
1097 rot->oy = rot->cy + rot->ch/2;
1098 angle = (-PI/2 * lastr) * (1.0 - animtime / anim_max);
1099 rot->c = cos(angle);
1100 rot->s = sin(angle);
1103 * Sort out the colours of the various sides of the tile.
1105 rot->lc = highlight_colour(PI + angle);
1106 rot->rc = highlight_colour(angle);
1107 rot->tc = highlight_colour(PI/2 + angle);
1108 rot->bc = highlight_colour(-PI/2 + angle);
1110 draw_rect(fe, rot->cx, rot->cy, rot->cw, rot->ch, bgcolour);
1115 * Now draw each tile.
1117 for (i = 0; i < state->w * state->h; i++) {
1119 int tx = i % state->w, ty = i / state->w;
1122 * Figure out what should be displayed at this location.
1123 * Usually it will be state->grid[i], unless we're in the
1124 * middle of animating an actual rotation and this cell is
1125 * within the rotation region, in which case we set -1
1128 if (oldstate && lastx >= 0 && lasty >= 0 &&
1129 tx >= lastx && tx < lastx + state->n &&
1130 ty >= lasty && ty < lasty + state->n)
1135 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
1136 ds->grid[i] != t || ds->grid[i] == -1 || t == -1) {
1137 int x = COORD(tx), y = COORD(ty);
1139 draw_tile(fe, ds, state, x, y, state->grid[i], bgcolour, rot);
1143 ds->bgcolour = bgcolour;
1146 * Update the status bar.
1149 char statusbuf[256];
1152 * Don't show the new status until we're also showing the
1153 * new _state_ - after the game animation is complete.
1158 if (state->used_solve)
1159 sprintf(statusbuf, "Moves since auto-solve: %d",
1160 state->movecount - state->completed);
1162 sprintf(statusbuf, "%sMoves: %d",
1163 (state->completed ? "COMPLETED! " : ""),
1164 (state->completed ? state->completed : state->movecount));
1165 if (state->movetarget)
1166 sprintf(statusbuf+strlen(statusbuf), " (target %d)",
1170 status_bar(fe, statusbuf);
1174 static int game_wants_statusbar(void)
1179 static int game_timing_state(game_state *state)
1185 #define thegame twiddle
1188 const struct game thegame = {
1189 "Twiddle", "games.twiddle",
1196 TRUE, game_configure, custom_params,
1205 TRUE, game_text_format,
1213 game_free_drawstate,
1217 game_wants_statusbar,
1218 FALSE, game_timing_state,
1219 0, /* mouse_priorities */