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 PI 3.141592653589793238462643383279502884197169399
25 #define ANIM_PER_RADIUS_UNIT 0.13F
26 #define FLASH_FRAME 0.13F
50 int lastx
, lasty
, lastr
; /* coordinates of last rotation */
53 static game_params
*default_params(void)
55 game_params
*ret
= snew(game_params
);
59 ret
->rowsonly
= ret
->orientable
= FALSE
;
65 static void free_params(game_params
*params
)
70 static game_params
*dup_params(game_params
*params
)
72 game_params
*ret
= snew(game_params
);
73 *ret
= *params
; /* structure copy */
77 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
83 { "3x3 rows only", { 3, 3, 2, TRUE
, FALSE
} },
84 { "3x3 normal", { 3, 3, 2, FALSE
, FALSE
} },
85 { "3x3 orientable", { 3, 3, 2, FALSE
, TRUE
} },
86 { "4x4 normal", { 4, 4, 2, FALSE
} },
87 { "4x4 orientable", { 4, 4, 2, FALSE
, TRUE
} },
88 { "4x4 radius 3", { 4, 4, 3, FALSE
} },
89 { "5x5 radius 3", { 5, 5, 3, FALSE
} },
90 { "6x6 radius 4", { 6, 6, 4, FALSE
} },
93 if (i
< 0 || i
>= lenof(presets
))
96 *name
= dupstr(presets
[i
].title
);
97 *params
= dup_params(&presets
[i
].params
);
102 static game_params
*decode_params(char const *string
)
104 game_params
*ret
= snew(game_params
);
106 ret
->w
= ret
->h
= atoi(string
);
108 ret
->rowsonly
= ret
->orientable
= FALSE
;
109 while (*string
&& isdigit(*string
)) string
++;
110 if (*string
== 'x') {
112 ret
->h
= atoi(string
);
113 while (*string
&& isdigit(*string
)) string
++;
115 if (*string
== 'n') {
117 ret
->n
= atoi(string
);
118 while (*string
&& isdigit(*string
)) string
++;
121 if (*string
== 'r') {
122 ret
->rowsonly
= TRUE
;
123 } else if (*string
== 'o') {
124 ret
->orientable
= TRUE
;
132 static char *encode_params(game_params
*params
)
135 sprintf(buf
, "%dx%dn%d%s%s", params
->w
, params
->h
, params
->n
,
136 params
->rowsonly ?
"r" : "",
137 params
->orientable ?
"o" : "");
141 static config_item
*game_configure(game_params
*params
)
146 ret
= snewn(6, config_item
);
148 ret
[0].name
= "Width";
149 ret
[0].type
= C_STRING
;
150 sprintf(buf
, "%d", params
->w
);
151 ret
[0].sval
= dupstr(buf
);
154 ret
[1].name
= "Height";
155 ret
[1].type
= C_STRING
;
156 sprintf(buf
, "%d", params
->h
);
157 ret
[1].sval
= dupstr(buf
);
160 ret
[2].name
= "Rotation radius";
161 ret
[2].type
= C_STRING
;
162 sprintf(buf
, "%d", params
->n
);
163 ret
[2].sval
= dupstr(buf
);
166 ret
[3].name
= "One number per row";
167 ret
[3].type
= C_BOOLEAN
;
169 ret
[3].ival
= params
->rowsonly
;
171 ret
[4].name
= "Orientation matters";
172 ret
[4].type
= C_BOOLEAN
;
174 ret
[4].ival
= params
->orientable
;
184 static game_params
*custom_params(config_item
*cfg
)
186 game_params
*ret
= snew(game_params
);
188 ret
->w
= atoi(cfg
[0].sval
);
189 ret
->h
= atoi(cfg
[1].sval
);
190 ret
->n
= atoi(cfg
[2].sval
);
191 ret
->rowsonly
= cfg
[3].ival
;
192 ret
->orientable
= cfg
[4].ival
;
197 static char *validate_params(game_params
*params
)
200 return "Rotation radius must be at least two";
201 if (params
->w
< params
->n
)
202 return "Width must be at least the rotation radius";
203 if (params
->h
< params
->n
)
204 return "Height must be at least the rotation radius";
209 * This function actually performs a rotation on a grid. The `x'
210 * and `y' coordinates passed in are the coordinates of the _top
211 * left corner_ of the rotated region. (Using the centre would have
212 * involved half-integers and been annoyingly fiddly. Clicking in
213 * the centre is good for a user interface, but too inconvenient to
216 static void do_rotate(int *grid
, int w
, int h
, int n
, int orientable
,
217 int x
, int y
, int dir
)
221 assert(x
>= 0 && x
+n
<= w
);
222 assert(y
>= 0 && y
+n
<= h
);
225 return; /* nothing to do */
227 grid
+= y
*w
+x
; /* translate region to top corner */
230 * If we were leaving the result of the rotation in a separate
231 * grid, the simple thing to do would be to loop over each
232 * square within the rotated region and assign it from its
233 * source square. However, to do it in place without taking
234 * O(n^2) memory, we need to be marginally more clever. What
235 * I'm going to do is loop over about one _quarter_ of the
236 * rotated region and permute each element within that quarter
237 * with its rotational coset.
239 * The size of the region I need to loop over is (n+1)/2 by
240 * n/2, which is an obvious exact quarter for even n and is a
241 * rectangle for odd n. (For odd n, this technique leaves out
242 * one element of the square, which is of course the central
243 * one that never moves anyway.)
245 for (i
= 0; i
< (n
+1)/2; i
++) {
246 for (j
= 0; j
< n
/2; j
++) {
256 for (k
= 0; k
< 4; k
++)
259 for (k
= 0; k
< 4; k
++) {
260 int v
= g
[(k
+dir
) & 3];
262 v
^= ((v
+dir
) ^ v
) & 3; /* alter orientation */
269 * Don't forget the orientation on the centre square, if n is
272 if (orientable
&& (n
& 1)) {
273 int v
= grid
[n
/2*(w
+1)];
274 v
^= ((v
+dir
) ^ v
) & 3; /* alter orientation */
279 static int grid_complete(int *grid
, int wh
, int orientable
)
283 for (i
= 1; i
< wh
; i
++)
284 if (grid
[i
] < grid
[i
-1])
287 for (i
= 0; i
< wh
; i
++)
294 static char *new_game_seed(game_params
*params
, random_state
*rs
)
297 int w
= params
->w
, h
= params
->h
, n
= params
->n
, wh
= w
*h
;
304 * Set up a solved grid.
306 grid
= snewn(wh
, int);
307 for (i
= 0; i
< wh
; i
++)
308 grid
[i
] = ((params
->rowsonly ? i
/w
: i
) + 1) * 4;
311 * Shuffle it. This game is complex enough that I don't feel up
312 * to analysing its full symmetry properties (particularly at
313 * n=4 and above!), so I'm going to do it the pedestrian way
314 * and simply shuffle the grid by making a long sequence of
315 * randomly chosen moves.
317 total_moves
= w
*h
*n
*n
*2;
318 for (i
= 0; i
< total_moves
; i
++) {
321 x
= random_upto(rs
, w
- n
+ 1);
322 y
= random_upto(rs
, h
- n
+ 1);
323 do_rotate(grid
, w
, h
, n
, params
->orientable
,
324 x
, y
, 1 + random_upto(rs
, 3));
327 * Optionally one more move in case the entire grid has
328 * happened to come out solved.
330 if (i
== total_moves
- 1 && grid_complete(grid
, wh
,
336 * Now construct the game seed, by describing the grid as a
337 * simple sequence of integers. They're comma-separated, unless
338 * the puzzle is orientable in which case they're separated by
339 * orientation letters `u', `d', `l' and `r'.
343 for (i
= 0; i
< wh
; i
++) {
347 k
= sprintf(buf
, "%d%c", grid
[i
] / 4,
348 params
->orientable ?
"uldr"[grid
[i
] & 3] : ',');
350 ret
= sresize(ret
, retlen
+ k
+ 1, char);
351 strcpy(ret
+ retlen
, buf
);
354 if (!params
->orientable
)
355 ret
[retlen
-1] = '\0'; /* delete last comma */
361 static char *validate_seed(game_params
*params
, char *seed
)
364 int w
= params
->w
, h
= params
->h
, wh
= w
*h
;
370 for (i
= 0; i
< wh
; i
++) {
371 if (*p
< '0' || *p
> '9')
372 return "Not enough numbers in string";
373 while (*p
>= '0' && *p
<= '9')
375 if (!params
->orientable
&& i
< wh
-1) {
377 return "Expected comma after number";
378 } else if (params
->orientable
&& i
< wh
) {
379 if (*p
!= 'l' && *p
!= 'r' && *p
!= 'u' && *p
!= 'd')
380 return "Expected orientation letter after number";
381 } else if (i
== wh
-1 && *p
) {
382 return "Excess junk at end of string";
385 if (*p
) p
++; /* eat comma */
391 static game_state
*new_game(game_params
*params
, char *seed
)
393 game_state
*state
= snew(game_state
);
394 int w
= params
->w
, h
= params
->h
, n
= params
->n
, wh
= w
*h
;
401 state
->orientable
= params
->orientable
;
402 state
->completed
= 0;
403 state
->movecount
= 0;
404 state
->lastx
= state
->lasty
= state
->lastr
= -1;
406 state
->grid
= snewn(wh
, int);
410 for (i
= 0; i
< wh
; i
++) {
411 state
->grid
[i
] = 4 * atoi(p
);
412 while (*p
>= '0' && *p
<= '9')
415 if (params
->orientable
) {
417 case 'l': state
->grid
[i
] |= 1; break;
418 case 'd': state
->grid
[i
] |= 2; break;
419 case 'r': state
->grid
[i
] |= 3; break;
429 static game_state
*dup_game(game_state
*state
)
431 game_state
*ret
= snew(game_state
);
436 ret
->orientable
= state
->orientable
;
437 ret
->completed
= state
->completed
;
438 ret
->movecount
= state
->movecount
;
439 ret
->lastx
= state
->lastx
;
440 ret
->lasty
= state
->lasty
;
441 ret
->lastr
= state
->lastr
;
443 ret
->grid
= snewn(ret
->w
* ret
->h
, int);
444 memcpy(ret
->grid
, state
->grid
, ret
->w
* ret
->h
* sizeof(int));
449 static void free_game(game_state
*state
)
455 static char *game_text_format(game_state
*state
)
457 char *ret
, *p
, buf
[80];
458 int i
, x
, y
, col
, o
, maxlen
;
461 * First work out how many characters we need to display each
462 * number. We're pretty flexible on grid contents here, so we
463 * have to scan the entire grid.
466 for (i
= 0; i
< state
->w
* state
->h
; i
++) {
467 x
= sprintf(buf
, "%d", state
->grid
[i
] / 4);
468 if (col
< x
) col
= x
;
470 o
= (state
->orientable ?
1 : 0);
473 * Now we know the exact total size of the grid we're going to
474 * produce: it's got h rows, each containing w lots of col+o,
475 * w-1 spaces and a trailing newline.
477 maxlen
= state
->h
* state
->w
* (col
+o
+1);
479 ret
= snewn(maxlen
, char);
482 for (y
= 0; y
< state
->h
; y
++) {
483 for (x
= 0; x
< state
->w
; x
++) {
484 int v
= state
->grid
[state
->w
*y
+x
];
485 sprintf(buf
, "%*d", col
, v
/4);
489 *p
++ = "^<v>"[v
& 3];
497 assert(p
- ret
== maxlen
);
502 static game_ui
*new_ui(game_state
*state
)
507 static void free_ui(game_ui
*ui
)
511 static game_state
*make_move(game_state
*from
, game_ui
*ui
, int x
, int y
,
514 int w
= from
->w
, h
= from
->h
, n
= from
->n
, wh
= w
*h
;
518 if (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
) {
520 * Determine the coordinates of the click. We offset by n-1
521 * half-blocks so that the user must click at the centre of
522 * a rotation region rather than at the corner.
524 x
-= (n
-1) * TILE_SIZE
/ 2;
525 y
-= (n
-1) * TILE_SIZE
/ 2;
528 if (x
< 0 || x
> w
-n
|| y
< 0 || y
> w
-n
)
532 * This is a valid move. Make it.
534 ret
= dup_game(from
);
536 dir
= (button
== LEFT_BUTTON ?
1 : -1);
537 do_rotate(ret
->grid
, w
, h
, n
, ret
->orientable
, x
, y
, dir
);
543 * See if the game has been completed. To do this we simply
544 * test that the grid contents are in increasing order.
546 if (!ret
->completed
&& grid_complete(ret
->grid
, wh
, ret
->orientable
))
547 ret
->completed
= ret
->movecount
;
553 /* ----------------------------------------------------------------------
557 struct game_drawstate
{
563 static void game_size(game_params
*params
, int *x
, int *y
)
565 *x
= TILE_SIZE
* params
->w
+ 2 * BORDER
;
566 *y
= TILE_SIZE
* params
->h
+ 2 * BORDER
;
569 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
571 float *ret
= snewn(3 * NCOLOURS
, float);
575 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
578 * Drop the background colour so that the highlight is
579 * noticeably brighter than it while still being under 1.
581 max
= ret
[COL_BACKGROUND
*3];
582 for (i
= 1; i
< 3; i
++)
583 if (ret
[COL_BACKGROUND
*3+i
] > max
)
584 max
= ret
[COL_BACKGROUND
*3+i
];
585 if (max
* 1.2F
> 1.0F
) {
586 for (i
= 0; i
< 3; i
++)
587 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
590 for (i
= 0; i
< 3; i
++) {
591 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
592 ret
[COL_HIGHLIGHT_GENTLE
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.1F
;
593 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
594 ret
[COL_LOWLIGHT_GENTLE
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.9F
;
595 ret
[COL_TEXT
* 3 + i
] = 0.0;
598 *ncolours
= NCOLOURS
;
602 static game_drawstate
*game_new_drawstate(game_state
*state
)
604 struct game_drawstate
*ds
= snew(struct game_drawstate
);
610 ds
->bgcolour
= COL_BACKGROUND
;
611 ds
->grid
= snewn(ds
->w
*ds
->h
, int);
612 for (i
= 0; i
< ds
->w
*ds
->h
; i
++)
618 static void game_free_drawstate(game_drawstate
*ds
)
624 int cx
, cy
, cw
, ch
; /* clip region */
625 int ox
, oy
; /* rotation origin */
626 float c
, s
; /* cos and sin of rotation angle */
627 int lc
, rc
, tc
, bc
; /* colours of tile edges */
630 static void rotate(int *xy
, struct rotation
*rot
)
633 float xf
= xy
[0] - rot
->ox
, yf
= xy
[1] - rot
->oy
;
636 xf2
= rot
->c
* xf
+ rot
->s
* yf
;
637 yf2
= - rot
->s
* xf
+ rot
->c
* yf
;
639 xy
[0] = xf2
+ rot
->ox
+ 0.5; /* round to nearest */
640 xy
[1] = yf2
+ rot
->oy
+ 0.5; /* round to nearest */
644 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
,
645 int tile
, int flash_colour
, struct rotation
*rot
)
651 clip(fe
, rot
->cx
, rot
->cy
, rot
->cw
, rot
->ch
);
654 * We must draw each side of the tile's highlight separately,
655 * because in some cases (during rotation) they will all need
656 * to be different colours.
659 /* The centre point is common to all sides. */
660 coords
[4] = x
+ TILE_SIZE
/ 2;
661 coords
[5] = y
+ TILE_SIZE
/ 2;
662 rotate(coords
+4, rot
);
665 coords
[0] = x
+ TILE_SIZE
- 1;
666 coords
[1] = y
+ TILE_SIZE
- 1;
667 rotate(coords
+0, rot
);
668 coords
[2] = x
+ TILE_SIZE
- 1;
670 rotate(coords
+2, rot
);
671 draw_polygon(fe
, coords
, 3, TRUE
, rot ? rot
->rc
: COL_LOWLIGHT
);
672 draw_polygon(fe
, coords
, 3, FALSE
, rot ? rot
->rc
: COL_LOWLIGHT
);
676 coords
[3] = y
+ TILE_SIZE
- 1;
677 rotate(coords
+2, rot
);
678 draw_polygon(fe
, coords
, 3, TRUE
, rot ? rot
->bc
: COL_LOWLIGHT
);
679 draw_polygon(fe
, coords
, 3, FALSE
, rot ? rot
->bc
: COL_LOWLIGHT
);
684 rotate(coords
+0, rot
);
685 draw_polygon(fe
, coords
, 3, TRUE
, rot ? rot
->lc
: COL_HIGHLIGHT
);
686 draw_polygon(fe
, coords
, 3, FALSE
, rot ? rot
->lc
: COL_HIGHLIGHT
);
689 coords
[2] = x
+ TILE_SIZE
- 1;
691 rotate(coords
+2, rot
);
692 draw_polygon(fe
, coords
, 3, TRUE
, rot ? rot
->tc
: COL_HIGHLIGHT
);
693 draw_polygon(fe
, coords
, 3, FALSE
, rot ? rot
->tc
: COL_HIGHLIGHT
);
696 * Now the main blank area in the centre of the tile.
699 coords
[0] = x
+ HIGHLIGHT_WIDTH
;
700 coords
[1] = y
+ HIGHLIGHT_WIDTH
;
701 rotate(coords
+0, rot
);
702 coords
[2] = x
+ HIGHLIGHT_WIDTH
;
703 coords
[3] = y
+ TILE_SIZE
- 1 - HIGHLIGHT_WIDTH
;
704 rotate(coords
+2, rot
);
705 coords
[4] = x
+ TILE_SIZE
- 1 - HIGHLIGHT_WIDTH
;
706 coords
[5] = y
+ TILE_SIZE
- 1 - HIGHLIGHT_WIDTH
;
707 rotate(coords
+4, rot
);
708 coords
[6] = x
+ TILE_SIZE
- 1 - HIGHLIGHT_WIDTH
;
709 coords
[7] = y
+ HIGHLIGHT_WIDTH
;
710 rotate(coords
+6, rot
);
711 draw_polygon(fe
, coords
, 4, TRUE
, flash_colour
);
712 draw_polygon(fe
, coords
, 4, FALSE
, flash_colour
);
714 draw_rect(fe
, x
+ HIGHLIGHT_WIDTH
, y
+ HIGHLIGHT_WIDTH
,
715 TILE_SIZE
- 2*HIGHLIGHT_WIDTH
, TILE_SIZE
- 2*HIGHLIGHT_WIDTH
,
720 * Next, the colour bars for orientation.
722 if (state
->orientable
) {
723 int xdx
, xdy
, ydx
, ydy
;
724 int cx
, cy
, displ
, displ2
;
738 default /* case 3 */:
744 cx
= x
+ TILE_SIZE
/ 2;
745 cy
= y
+ TILE_SIZE
/ 2;
746 displ
= TILE_SIZE
/ 2 - HIGHLIGHT_WIDTH
- 2;
747 displ2
= TILE_SIZE
/ 3 - HIGHLIGHT_WIDTH
;
749 coords
[0] = cx
- displ
* xdx
+ displ2
* ydx
;
750 coords
[1] = cy
- displ
* xdy
+ displ2
* ydy
;
751 rotate(coords
+0, rot
);
752 coords
[2] = cx
+ displ
* xdx
+ displ2
* ydx
;
753 coords
[3] = cy
+ displ
* xdy
+ displ2
* ydy
;
754 rotate(coords
+2, rot
);
755 coords
[4] = cx
- displ
* ydx
;
756 coords
[5] = cy
- displ
* ydy
;
757 rotate(coords
+4, rot
);
758 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT_GENTLE
);
759 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT_GENTLE
);
762 coords
[0] = x
+ TILE_SIZE
/2;
763 coords
[1] = y
+ TILE_SIZE
/2;
764 rotate(coords
+0, rot
);
765 sprintf(str
, "%d", tile
/ 4);
766 draw_text(fe
, coords
[0], coords
[1],
767 FONT_VARIABLE
, TILE_SIZE
/3, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
773 draw_update(fe
, x
, y
, TILE_SIZE
, TILE_SIZE
);
776 static int highlight_colour(float angle
)
783 COL_HIGHLIGHT_GENTLE
,
784 COL_HIGHLIGHT_GENTLE
,
785 COL_HIGHLIGHT_GENTLE
,
796 COL_HIGHLIGHT_GENTLE
,
797 COL_HIGHLIGHT_GENTLE
,
798 COL_HIGHLIGHT_GENTLE
,
813 return colours
[(int)((angle
+ 2*PI
) / (PI
/16)) & 31];
816 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
819 return ANIM_PER_RADIUS_UNIT
* sqrt(newstate
->n
-1);
822 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
825 if (!oldstate
->completed
&& newstate
->completed
)
826 return 2 * FLASH_FRAME
;
831 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
832 game_state
*state
, int dir
, game_ui
*ui
,
833 float animtime
, float flashtime
)
836 struct rotation srot
, *rot
;
837 int lastx
= -1, lasty
= -1, lastr
= -1;
840 int frame
= (int)(flashtime
/ FLASH_FRAME
);
841 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
843 bgcolour
= COL_BACKGROUND
;
849 TILE_SIZE
* state
->w
+ 2 * BORDER
,
850 TILE_SIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
851 draw_update(fe
, 0, 0,
852 TILE_SIZE
* state
->w
+ 2 * BORDER
,
853 TILE_SIZE
* state
->h
+ 2 * BORDER
);
856 * Recessed area containing the whole puzzle.
858 coords
[0] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
859 coords
[1] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
860 coords
[2] = COORD(state
->w
) + HIGHLIGHT_WIDTH
- 1;
861 coords
[3] = COORD(0) - HIGHLIGHT_WIDTH
;
862 coords
[4] = COORD(0) - HIGHLIGHT_WIDTH
;
863 coords
[5] = COORD(state
->h
) + HIGHLIGHT_WIDTH
- 1;
864 draw_polygon(fe
, coords
, 3, TRUE
, COL_HIGHLIGHT
);
865 draw_polygon(fe
, coords
, 3, FALSE
, COL_HIGHLIGHT
);
867 coords
[1] = COORD(0) - HIGHLIGHT_WIDTH
;
868 coords
[0] = COORD(0) - HIGHLIGHT_WIDTH
;
869 draw_polygon(fe
, coords
, 3, TRUE
, COL_LOWLIGHT
);
870 draw_polygon(fe
, coords
, 3, FALSE
, COL_LOWLIGHT
);
876 * If we're drawing any rotated tiles, sort out the rotation
877 * parameters, and also zap the rotation region to the
878 * background colour before doing anything else.
882 float anim_max
= game_anim_length(oldstate
, state
, dir
);
885 lastx
= state
->lastx
;
886 lasty
= state
->lasty
;
887 lastr
= state
->lastr
;
889 lastx
= oldstate
->lastx
;
890 lasty
= oldstate
->lasty
;
891 lastr
= -oldstate
->lastr
;
895 rot
->cx
= COORD(lastx
);
896 rot
->cy
= COORD(lasty
);
897 rot
->cw
= rot
->ch
= TILE_SIZE
* state
->n
;
898 rot
->ox
= rot
->cx
+ rot
->cw
/2;
899 rot
->oy
= rot
->cy
+ rot
->ch
/2;
900 angle
= (-PI
/2 * lastr
) * (1.0 - animtime
/ anim_max
);
905 * Sort out the colours of the various sides of the tile.
907 rot
->lc
= highlight_colour(PI
+ angle
);
908 rot
->rc
= highlight_colour(angle
);
909 rot
->tc
= highlight_colour(PI
/2 + angle
);
910 rot
->bc
= highlight_colour(-PI
/2 + angle
);
912 draw_rect(fe
, rot
->cx
, rot
->cy
, rot
->cw
, rot
->ch
, bgcolour
);
917 * Now draw each tile.
919 for (i
= 0; i
< state
->w
* state
->h
; i
++) {
921 int tx
= i
% state
->w
, ty
= i
/ state
->w
;
924 * Figure out what should be displayed at this location.
925 * Usually it will be state->grid[i], unless we're in the
926 * middle of animating an actual rotation and this cell is
927 * within the rotation region, in which case we set -1
930 if (oldstate
&& lastx
>= 0 && lasty
>= 0 &&
931 tx
>= lastx
&& tx
< lastx
+ state
->n
&&
932 ty
>= lasty
&& ty
< lasty
+ state
->n
)
937 if (ds
->bgcolour
!= bgcolour
|| /* always redraw when flashing */
938 ds
->grid
[i
] != t
|| ds
->grid
[i
] == -1 || t
== -1) {
939 int x
= COORD(tx
), y
= COORD(ty
);
941 draw_tile(fe
, state
, x
, y
, state
->grid
[i
], bgcolour
, rot
);
945 ds
->bgcolour
= bgcolour
;
948 * Update the status bar.
954 * Don't show the new status until we're also showing the
955 * new _state_ - after the game animation is complete.
960 sprintf(statusbuf
, "%sMoves: %d",
961 (state
->completed ?
"COMPLETED! " : ""),
962 (state
->completed ? state
->completed
: state
->movecount
));
964 status_bar(fe
, statusbuf
);
968 static int game_wants_statusbar(void)
974 #define thegame twiddle
977 const struct game thegame
= {
978 "Twiddle", "games.twiddle",
985 TRUE
, game_configure
, custom_params
,
992 TRUE
, game_text_format
,
1003 game_wants_statusbar
,