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
18 #define BORDER (TILE_SIZE / 2)
19 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
20 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
21 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
23 #define ANIM_PER_RADIUS_UNIT 0.13F
24 #define FLASH_FRAME 0.13F
48 int just_used_solve; /* used to suppress undo animation */
49 int used_solve; /* used to suppress completion flash */
50 int movecount, movetarget;
51 int lastx, lasty, lastr; /* coordinates of last rotation */
54 static game_params *default_params(void)
56 game_params *ret = snew(game_params);
60 ret->rowsonly = ret->orientable = FALSE;
67 static void free_params(game_params *params)
72 static game_params *dup_params(game_params *params)
74 game_params *ret = snew(game_params);
75 *ret = *params; /* structure copy */
79 static int game_fetch_preset(int i, char **name, game_params **params)
85 { "3x3 rows only", { 3, 3, 2, TRUE, FALSE } },
86 { "3x3 normal", { 3, 3, 2, FALSE, FALSE } },
87 { "3x3 orientable", { 3, 3, 2, FALSE, TRUE } },
88 { "4x4 normal", { 4, 4, 2, FALSE } },
89 { "4x4 orientable", { 4, 4, 2, FALSE, TRUE } },
90 { "4x4 radius 3", { 4, 4, 3, FALSE } },
91 { "5x5 radius 3", { 5, 5, 3, FALSE } },
92 { "6x6 radius 4", { 6, 6, 4, FALSE } },
95 if (i < 0 || i >= lenof(presets))
98 *name = dupstr(presets[i].title);
99 *params = dup_params(&presets[i].params);
104 static void decode_params(game_params *ret, char const *string)
106 ret->w = ret->h = atoi(string);
108 ret->rowsonly = ret->orientable = FALSE;
110 while (*string && isdigit(*string)) string++;
111 if (*string == 'x') {
113 ret->h = atoi(string);
114 while (*string && isdigit(*string)) string++;
116 if (*string == 'n') {
118 ret->n = atoi(string);
119 while (*string && isdigit(*string)) string++;
122 if (*string == 'r') {
123 ret->rowsonly = TRUE;
124 } else if (*string == 'o') {
125 ret->orientable = TRUE;
126 } else if (*string == 'm') {
128 ret->movetarget = atoi(string);
129 while (string[1] && isdigit(string[1])) string++;
135 static char *encode_params(game_params *params, int full)
138 sprintf(buf, "%dx%dn%d%s%s", params->w, params->h, params->n,
139 params->rowsonly ? "r" : "",
140 params->orientable ? "o" : "");
141 /* Shuffle limit is part of the limited parameters, because we have to
142 * supply the target move count. */
143 if (params->movetarget)
144 sprintf(buf + strlen(buf), "m%d", params->movetarget);
148 static config_item *game_configure(game_params *params)
153 ret = snewn(7, config_item);
155 ret[0].name = "Width";
156 ret[0].type = C_STRING;
157 sprintf(buf, "%d", params->w);
158 ret[0].sval = dupstr(buf);
161 ret[1].name = "Height";
162 ret[1].type = C_STRING;
163 sprintf(buf, "%d", params->h);
164 ret[1].sval = dupstr(buf);
167 ret[2].name = "Rotation radius";
168 ret[2].type = C_STRING;
169 sprintf(buf, "%d", params->n);
170 ret[2].sval = dupstr(buf);
173 ret[3].name = "One number per row";
174 ret[3].type = C_BOOLEAN;
176 ret[3].ival = params->rowsonly;
178 ret[4].name = "Orientation matters";
179 ret[4].type = C_BOOLEAN;
181 ret[4].ival = params->orientable;
183 ret[5].name = "Number of shuffling moves";
184 ret[5].type = C_STRING;
185 sprintf(buf, "%d", params->movetarget);
186 ret[5].sval = dupstr(buf);
197 static game_params *custom_params(config_item *cfg)
199 game_params *ret = snew(game_params);
201 ret->w = atoi(cfg[0].sval);
202 ret->h = atoi(cfg[1].sval);
203 ret->n = atoi(cfg[2].sval);
204 ret->rowsonly = cfg[3].ival;
205 ret->orientable = cfg[4].ival;
206 ret->movetarget = atoi(cfg[5].sval);
211 static char *validate_params(game_params *params)
214 return "Rotation radius must be at least two";
215 if (params->w < params->n)
216 return "Width must be at least the rotation radius";
217 if (params->h < params->n)
218 return "Height must be at least the rotation radius";
223 * This function actually performs a rotation on a grid. The `x'
224 * and `y' coordinates passed in are the coordinates of the _top
225 * left corner_ of the rotated region. (Using the centre would have
226 * involved half-integers and been annoyingly fiddly. Clicking in
227 * the centre is good for a user interface, but too inconvenient to
230 static void do_rotate(int *grid, int w, int h, int n, int orientable,
231 int x, int y, int dir)
235 assert(x >= 0 && x+n <= w);
236 assert(y >= 0 && y+n <= h);
239 return; /* nothing to do */
241 grid += y*w+x; /* translate region to top corner */
244 * If we were leaving the result of the rotation in a separate
245 * grid, the simple thing to do would be to loop over each
246 * square within the rotated region and assign it from its
247 * source square. However, to do it in place without taking
248 * O(n^2) memory, we need to be marginally more clever. What
249 * I'm going to do is loop over about one _quarter_ of the
250 * rotated region and permute each element within that quarter
251 * with its rotational coset.
253 * The size of the region I need to loop over is (n+1)/2 by
254 * n/2, which is an obvious exact quarter for even n and is a
255 * rectangle for odd n. (For odd n, this technique leaves out
256 * one element of the square, which is of course the central
257 * one that never moves anyway.)
259 for (i = 0; i < (n+1)/2; i++) {
260 for (j = 0; j < n/2; j++) {
267 p[2] = (n-j-1)*w+(n-i-1);
270 for (k = 0; k < 4; k++)
273 for (k = 0; k < 4; k++) {
274 int v = g[(k+dir) & 3];
276 v ^= ((v+dir) ^ v) & 3; /* alter orientation */
283 * Don't forget the orientation on the centre square, if n is
286 if (orientable && (n & 1)) {
287 int v = grid[n/2*(w+1)];
288 v ^= ((v+dir) ^ v) & 3; /* alter orientation */
293 static int grid_complete(int *grid, int wh, int orientable)
297 for (i = 1; i < wh; i++)
298 if (grid[i] < grid[i-1])
301 for (i = 0; i < wh; i++)
308 static char *new_game_desc(game_params *params, random_state *rs,
309 game_aux_info **aux, int interactive)
312 int w = params->w, h = params->h, n = params->n, wh = w*h;
319 * Set up a solved grid.
321 grid = snewn(wh, int);
322 for (i = 0; i < wh; i++)
323 grid[i] = ((params->rowsonly ? i/w : i) + 1) * 4;
326 * Shuffle it. This game is complex enough that I don't feel up
327 * to analysing its full symmetry properties (particularly at
328 * n=4 and above!), so I'm going to do it the pedestrian way
329 * and simply shuffle the grid by making a long sequence of
330 * randomly chosen moves.
332 total_moves = params->movetarget;
334 /* Add a random move to avoid parity issues. */
335 total_moves = w*h*n*n*2 + random_upto(rs, 2);
339 int rw, rh; /* w/h of rotation centre space */
343 prevmoves = snewn(rw * rh, int);
344 for (i = 0; i < rw * rh; i++)
347 for (i = 0; i < total_moves; i++) {
348 int x, y, r, oldtotal, newtotal, dx, dy;
351 x = random_upto(rs, w - n + 1);
352 y = random_upto(rs, h - n + 1);
353 r = 2 * random_upto(rs, 2) - 1;
356 * See if any previous rotations has happened at
357 * this point which nothing has overlapped since.
358 * If so, ensure we haven't either undone a
359 * previous move or repeated one so many times that
360 * it turns into fewer moves in the inverse
361 * direction (i.e. three identical rotations).
363 oldtotal = prevmoves[y*rw+x];
364 newtotal = oldtotal + r;
365 } while (abs(newtotal) < abs(oldtotal) || abs(newtotal) > 2);
367 do_rotate(grid, w, h, n, params->orientable, x, y, r);
370 * Log the rotation we've just performed at this point,
371 * for inversion detection in the next move.
373 * Also zero a section of the prevmoves array, because
374 * any rotation area which _overlaps_ this one is now
375 * entirely safe to perform further moves in.
377 * Two rotation areas overlap if their top left
378 * coordinates differ by strictly less than n in both
381 prevmoves[y*rw+x] += r;
382 for (dy = -n+1; dy <= n-1; dy++) {
383 if (y + dy < 0 || y + dy >= rh)
385 for (dx = -n+1; dx <= n-1; dx++) {
386 if (x + dx < 0 || x + dx >= rw)
388 if (dx == 0 && dy == 0)
390 prevmoves[(y+dy)*rw+(x+dx)] = 0;
397 } while (grid_complete(grid, wh, params->orientable));
400 * Now construct the game description, by describing the grid
401 * as a simple sequence of integers. They're comma-separated,
402 * unless the puzzle is orientable in which case they're
403 * separated by orientation letters `u', `d', `l' and `r'.
407 for (i = 0; i < wh; i++) {
411 k = sprintf(buf, "%d%c", grid[i] / 4,
412 (char)(params->orientable ? "uldr"[grid[i] & 3] : ','));
414 ret = sresize(ret, retlen + k + 1, char);
415 strcpy(ret + retlen, buf);
418 if (!params->orientable)
419 ret[retlen-1] = '\0'; /* delete last comma */
425 static void game_free_aux_info(game_aux_info *aux)
427 assert(!"Shouldn't happen");
430 static char *validate_desc(game_params *params, char *desc)
433 int w = params->w, h = params->h, wh = w*h;
439 for (i = 0; i < wh; i++) {
440 if (*p < '0' || *p > '9')
441 return "Not enough numbers in string";
442 while (*p >= '0' && *p <= '9')
444 if (!params->orientable && i < wh-1) {
446 return "Expected comma after number";
447 } else if (params->orientable && i < wh) {
448 if (*p != 'l' && *p != 'r' && *p != 'u' && *p != 'd')
449 return "Expected orientation letter after number";
450 } else if (i == wh-1 && *p) {
451 return "Excess junk at end of string";
454 if (*p) p++; /* eat comma */
460 static game_state *new_game(midend_data *me, game_params *params, char *desc)
462 game_state *state = snew(game_state);
463 int w = params->w, h = params->h, n = params->n, wh = w*h;
470 state->orientable = params->orientable;
471 state->completed = 0;
472 state->used_solve = state->just_used_solve = FALSE;
473 state->movecount = 0;
474 state->movetarget = params->movetarget;
475 state->lastx = state->lasty = state->lastr = -1;
477 state->grid = snewn(wh, int);
481 for (i = 0; i < wh; i++) {
482 state->grid[i] = 4 * atoi(p);
483 while (*p >= '0' && *p <= '9')
486 if (params->orientable) {
488 case 'l': state->grid[i] |= 1; break;
489 case 'd': state->grid[i] |= 2; break;
490 case 'r': state->grid[i] |= 3; break;
500 static game_state *dup_game(game_state *state)
502 game_state *ret = snew(game_state);
507 ret->orientable = state->orientable;
508 ret->completed = state->completed;
509 ret->movecount = state->movecount;
510 ret->movetarget = state->movetarget;
511 ret->lastx = state->lastx;
512 ret->lasty = state->lasty;
513 ret->lastr = state->lastr;
514 ret->used_solve = state->used_solve;
515 ret->just_used_solve = state->just_used_solve;
517 ret->grid = snewn(ret->w * ret->h, int);
518 memcpy(ret->grid, state->grid, ret->w * ret->h * sizeof(int));
523 static void free_game(game_state *state)
529 static int compare_int(const void *av, const void *bv)
531 const int *a = (const int *)av;
532 const int *b = (const int *)bv;
541 static game_state *solve_game(game_state *state, game_aux_info *aux,
544 game_state *ret = dup_game(state);
548 * Simply replace the grid with a solved one. For this game,
549 * this isn't a useful operation for actually telling the user
550 * what they should have done, but it is useful for
551 * conveniently being able to get hold of a clean state from
552 * which to practise manoeuvres.
554 qsort(ret->grid, ret->w*ret->h, sizeof(int), compare_int);
555 for (i = 0; i < ret->w*ret->h; i++)
557 ret->used_solve = ret->just_used_solve = TRUE;
558 ret->completed = ret->movecount = 1;
563 static char *game_text_format(game_state *state)
565 char *ret, *p, buf[80];
566 int i, x, y, col, o, maxlen;
569 * First work out how many characters we need to display each
570 * number. We're pretty flexible on grid contents here, so we
571 * have to scan the entire grid.
574 for (i = 0; i < state->w * state->h; i++) {
575 x = sprintf(buf, "%d", state->grid[i] / 4);
576 if (col < x) col = x;
578 o = (state->orientable ? 1 : 0);
581 * Now we know the exact total size of the grid we're going to
582 * produce: it's got h rows, each containing w lots of col+o,
583 * w-1 spaces and a trailing newline.
585 maxlen = state->h * state->w * (col+o+1);
587 ret = snewn(maxlen+1, char);
590 for (y = 0; y < state->h; y++) {
591 for (x = 0; x < state->w; x++) {
592 int v = state->grid[state->w*y+x];
593 sprintf(buf, "%*d", col, v/4);
597 *p++ = "^<v>"[v & 3];
605 assert(p - ret == maxlen);
610 static game_ui *new_ui(game_state *state)
615 static void free_ui(game_ui *ui)
619 static void game_changed_state(game_ui *ui, game_state *oldstate,
620 game_state *newstate)
624 static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
625 int x, int y, int button)
627 int w = from->w, h = from->h, n = from->n, wh = w*h;
631 button = button & (~MOD_MASK | MOD_NUM_KEYPAD);
633 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
635 * Determine the coordinates of the click. We offset by n-1
636 * half-blocks so that the user must click at the centre of
637 * a rotation region rather than at the corner.
639 x -= (n-1) * TILE_SIZE / 2;
640 y -= (n-1) * TILE_SIZE / 2;
643 dir = (button == LEFT_BUTTON ? 1 : -1);
644 if (x < 0 || x > w-n || y < 0 || y > h-n)
646 } else if (button == 'a' || button == 'A' || button==MOD_NUM_KEYPAD+'7') {
648 dir = (button == 'A' ? -1 : +1);
649 } else if (button == 'b' || button == 'B' || button==MOD_NUM_KEYPAD+'9') {
652 dir = (button == 'B' ? -1 : +1);
653 } else if (button == 'c' || button == 'C' || button==MOD_NUM_KEYPAD+'1') {
656 dir = (button == 'C' ? -1 : +1);
657 } else if (button == 'd' || button == 'D' || button==MOD_NUM_KEYPAD+'3') {
660 dir = (button == 'D' ? -1 : +1);
661 } else if (button==MOD_NUM_KEYPAD+'8' && (w-n) % 2 == 0) {
665 } else if (button==MOD_NUM_KEYPAD+'2' && (w-n) % 2 == 0) {
669 } else if (button==MOD_NUM_KEYPAD+'4' && (h-n) % 2 == 0) {
673 } else if (button==MOD_NUM_KEYPAD+'6' && (h-n) % 2 == 0) {
677 } else if (button==MOD_NUM_KEYPAD+'5' && (w-n) % 2 == 0 && (h-n) % 2 == 0){
682 return NULL; /* no move to be made */
686 * This is a valid move. Make it.
688 ret = dup_game(from);
689 ret->just_used_solve = FALSE; /* zero this in a hurry */
691 do_rotate(ret->grid, w, h, n, ret->orientable, x, y, dir);
697 * See if the game has been completed. To do this we simply
698 * test that the grid contents are in increasing order.
700 if (!ret->completed && grid_complete(ret->grid, wh, ret->orientable))
701 ret->completed = ret->movecount;
705 /* ----------------------------------------------------------------------
709 struct game_drawstate {
715 static void game_size(game_params *params, int *x, int *y)
717 *x = TILE_SIZE * params->w + 2 * BORDER;
718 *y = TILE_SIZE * params->h + 2 * BORDER;
721 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
723 float *ret = snewn(3 * NCOLOURS, float);
727 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
730 * Drop the background colour so that the highlight is
731 * noticeably brighter than it while still being under 1.
733 max = ret[COL_BACKGROUND*3];
734 for (i = 1; i < 3; i++)
735 if (ret[COL_BACKGROUND*3+i] > max)
736 max = ret[COL_BACKGROUND*3+i];
737 if (max * 1.2F > 1.0F) {
738 for (i = 0; i < 3; i++)
739 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
742 for (i = 0; i < 3; i++) {
743 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
744 ret[COL_HIGHLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.1F;
745 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
746 ret[COL_LOWLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
747 ret[COL_TEXT * 3 + i] = 0.0;
750 *ncolours = NCOLOURS;
754 static game_drawstate *game_new_drawstate(game_state *state)
756 struct game_drawstate *ds = snew(struct game_drawstate);
762 ds->bgcolour = COL_BACKGROUND;
763 ds->grid = snewn(ds->w*ds->h, int);
764 for (i = 0; i < ds->w*ds->h; i++)
770 static void game_free_drawstate(game_drawstate *ds)
777 int cx, cy, cw, ch; /* clip region */
778 int ox, oy; /* rotation origin */
779 float c, s; /* cos and sin of rotation angle */
780 int lc, rc, tc, bc; /* colours of tile edges */
783 static void rotate(int *xy, struct rotation *rot)
786 float xf = xy[0] - rot->ox, yf = xy[1] - rot->oy;
789 xf2 = rot->c * xf + rot->s * yf;
790 yf2 = - rot->s * xf + rot->c * yf;
792 xy[0] = xf2 + rot->ox + 0.5; /* round to nearest */
793 xy[1] = yf2 + rot->oy + 0.5; /* round to nearest */
797 static void draw_tile(frontend *fe, game_state *state, int x, int y,
798 int tile, int flash_colour, struct rotation *rot)
804 * If we've been passed a rotation region but we're drawing a
805 * tile which is outside it, we must draw it normally. This can
806 * occur if we're cleaning up after a completion flash while a
807 * new move is also being made.
809 if (rot && (x < rot->cx || y < rot->cy ||
810 x >= rot->cx+rot->cw || y >= rot->cy+rot->ch))
814 clip(fe, rot->cx, rot->cy, rot->cw, rot->ch);
817 * We must draw each side of the tile's highlight separately,
818 * because in some cases (during rotation) they will all need
819 * to be different colours.
822 /* The centre point is common to all sides. */
823 coords[4] = x + TILE_SIZE / 2;
824 coords[5] = y + TILE_SIZE / 2;
825 rotate(coords+4, rot);
828 coords[0] = x + TILE_SIZE - 1;
829 coords[1] = y + TILE_SIZE - 1;
830 rotate(coords+0, rot);
831 coords[2] = x + TILE_SIZE - 1;
833 rotate(coords+2, rot);
834 draw_polygon(fe, coords, 3, TRUE, rot ? rot->rc : COL_LOWLIGHT);
835 draw_polygon(fe, coords, 3, FALSE, rot ? rot->rc : COL_LOWLIGHT);
839 coords[3] = y + TILE_SIZE - 1;
840 rotate(coords+2, rot);
841 draw_polygon(fe, coords, 3, TRUE, rot ? rot->bc : COL_LOWLIGHT);
842 draw_polygon(fe, coords, 3, FALSE, rot ? rot->bc : COL_LOWLIGHT);
847 rotate(coords+0, rot);
848 draw_polygon(fe, coords, 3, TRUE, rot ? rot->lc : COL_HIGHLIGHT);
849 draw_polygon(fe, coords, 3, FALSE, rot ? rot->lc : COL_HIGHLIGHT);
852 coords[2] = x + TILE_SIZE - 1;
854 rotate(coords+2, rot);
855 draw_polygon(fe, coords, 3, TRUE, rot ? rot->tc : COL_HIGHLIGHT);
856 draw_polygon(fe, coords, 3, FALSE, rot ? rot->tc : COL_HIGHLIGHT);
859 * Now the main blank area in the centre of the tile.
862 coords[0] = x + HIGHLIGHT_WIDTH;
863 coords[1] = y + HIGHLIGHT_WIDTH;
864 rotate(coords+0, rot);
865 coords[2] = x + HIGHLIGHT_WIDTH;
866 coords[3] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
867 rotate(coords+2, rot);
868 coords[4] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
869 coords[5] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
870 rotate(coords+4, rot);
871 coords[6] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
872 coords[7] = y + HIGHLIGHT_WIDTH;
873 rotate(coords+6, rot);
874 draw_polygon(fe, coords, 4, TRUE, flash_colour);
875 draw_polygon(fe, coords, 4, FALSE, flash_colour);
877 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
878 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
883 * Next, the triangles for orientation.
885 if (state->orientable) {
886 int xdx, xdy, ydx, ydy;
887 int cx, cy, displ, displ2;
901 default /* case 3 */:
907 cx = x + TILE_SIZE / 2;
908 cy = y + TILE_SIZE / 2;
909 displ = TILE_SIZE / 2 - HIGHLIGHT_WIDTH - 2;
910 displ2 = TILE_SIZE / 3 - HIGHLIGHT_WIDTH;
912 coords[0] = cx - displ * xdx + displ2 * ydx;
913 coords[1] = cy - displ * xdy + displ2 * ydy;
914 rotate(coords+0, rot);
915 coords[2] = cx + displ * xdx + displ2 * ydx;
916 coords[3] = cy + displ * xdy + displ2 * ydy;
917 rotate(coords+2, rot);
918 coords[4] = cx - displ * ydx;
919 coords[5] = cy - displ * ydy;
920 rotate(coords+4, rot);
921 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT_GENTLE);
922 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT_GENTLE);
925 coords[0] = x + TILE_SIZE/2;
926 coords[1] = y + TILE_SIZE/2;
927 rotate(coords+0, rot);
928 sprintf(str, "%d", tile / 4);
929 draw_text(fe, coords[0], coords[1],
930 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
936 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
939 static int highlight_colour(float angle)
946 COL_HIGHLIGHT_GENTLE,
947 COL_HIGHLIGHT_GENTLE,
948 COL_HIGHLIGHT_GENTLE,
959 COL_HIGHLIGHT_GENTLE,
960 COL_HIGHLIGHT_GENTLE,
961 COL_HIGHLIGHT_GENTLE,
976 return colours[(int)((angle + 2*PI) / (PI/16)) & 31];
979 static float game_anim_length(game_state *oldstate, game_state *newstate,
980 int dir, game_ui *ui)
982 if ((dir > 0 && newstate->just_used_solve) ||
983 (dir < 0 && oldstate->just_used_solve))
986 return ANIM_PER_RADIUS_UNIT * sqrt(newstate->n-1);
989 static float game_flash_length(game_state *oldstate, game_state *newstate,
990 int dir, game_ui *ui)
992 if (!oldstate->completed && newstate->completed &&
993 !oldstate->used_solve && !newstate->used_solve)
994 return 2 * FLASH_FRAME;
999 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1000 game_state *state, int dir, game_ui *ui,
1001 float animtime, float flashtime)
1004 struct rotation srot, *rot;
1005 int lastx = -1, lasty = -1, lastr = -1;
1007 if (flashtime > 0) {
1008 int frame = (int)(flashtime / FLASH_FRAME);
1009 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
1011 bgcolour = COL_BACKGROUND;
1017 TILE_SIZE * state->w + 2 * BORDER,
1018 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
1019 draw_update(fe, 0, 0,
1020 TILE_SIZE * state->w + 2 * BORDER,
1021 TILE_SIZE * state->h + 2 * BORDER);
1024 * Recessed area containing the whole puzzle.
1026 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
1027 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
1028 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
1029 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
1030 coords[4] = coords[2] - TILE_SIZE;
1031 coords[5] = coords[3] + TILE_SIZE;
1032 coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
1033 coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
1034 coords[6] = coords[8] + TILE_SIZE;
1035 coords[7] = coords[9] - TILE_SIZE;
1036 draw_polygon(fe, coords, 5, TRUE, COL_HIGHLIGHT);
1037 draw_polygon(fe, coords, 5, FALSE, COL_HIGHLIGHT);
1039 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
1040 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
1041 draw_polygon(fe, coords, 5, TRUE, COL_LOWLIGHT);
1042 draw_polygon(fe, coords, 5, FALSE, COL_LOWLIGHT);
1048 * If we're drawing any rotated tiles, sort out the rotation
1049 * parameters, and also zap the rotation region to the
1050 * background colour before doing anything else.
1054 float anim_max = game_anim_length(oldstate, state, dir, ui);
1057 lastx = state->lastx;
1058 lasty = state->lasty;
1059 lastr = state->lastr;
1061 lastx = oldstate->lastx;
1062 lasty = oldstate->lasty;
1063 lastr = -oldstate->lastr;
1067 rot->cx = COORD(lastx);
1068 rot->cy = COORD(lasty);
1069 rot->cw = rot->ch = TILE_SIZE * state->n;
1070 rot->ox = rot->cx + rot->cw/2;
1071 rot->oy = rot->cy + rot->ch/2;
1072 angle = (-PI/2 * lastr) * (1.0 - animtime / anim_max);
1073 rot->c = cos(angle);
1074 rot->s = sin(angle);
1077 * Sort out the colours of the various sides of the tile.
1079 rot->lc = highlight_colour(PI + angle);
1080 rot->rc = highlight_colour(angle);
1081 rot->tc = highlight_colour(PI/2 + angle);
1082 rot->bc = highlight_colour(-PI/2 + angle);
1084 draw_rect(fe, rot->cx, rot->cy, rot->cw, rot->ch, bgcolour);
1089 * Now draw each tile.
1091 for (i = 0; i < state->w * state->h; i++) {
1093 int tx = i % state->w, ty = i / state->w;
1096 * Figure out what should be displayed at this location.
1097 * Usually it will be state->grid[i], unless we're in the
1098 * middle of animating an actual rotation and this cell is
1099 * within the rotation region, in which case we set -1
1102 if (oldstate && lastx >= 0 && lasty >= 0 &&
1103 tx >= lastx && tx < lastx + state->n &&
1104 ty >= lasty && ty < lasty + state->n)
1109 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
1110 ds->grid[i] != t || ds->grid[i] == -1 || t == -1) {
1111 int x = COORD(tx), y = COORD(ty);
1113 draw_tile(fe, state, x, y, state->grid[i], bgcolour, rot);
1117 ds->bgcolour = bgcolour;
1120 * Update the status bar.
1123 char statusbuf[256];
1126 * Don't show the new status until we're also showing the
1127 * new _state_ - after the game animation is complete.
1132 if (state->used_solve)
1133 sprintf(statusbuf, "Moves since auto-solve: %d",
1134 state->movecount - state->completed);
1136 sprintf(statusbuf, "%sMoves: %d",
1137 (state->completed ? "COMPLETED! " : ""),
1138 (state->completed ? state->completed : state->movecount));
1139 if (state->movetarget)
1140 sprintf(statusbuf+strlen(statusbuf), " (target %d)",
1144 status_bar(fe, statusbuf);
1148 static int game_wants_statusbar(void)
1153 static int game_timing_state(game_state *state)
1159 #define thegame twiddle
1162 const struct game thegame = {
1163 "Twiddle", "games.twiddle",
1170 TRUE, game_configure, custom_params,
1179 TRUE, game_text_format,
1187 game_free_drawstate,
1191 game_wants_statusbar,
1192 FALSE, game_timing_state,
1193 0, /* mouse_priorities */