New puzzle: `twiddle', generalised from a random door-unlocking
[sgt/puzzles] / twiddle.c
1 /*
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
5 * Gyro Chamber).
6 */
7
8 /*
9 * Possibly TODO:
10 *
11 * - it's horribly tempting to give the pieces significant
12 * _orientations_, perhaps by drawing some sort of oriented
13 * polygonal figure beneath the number. (An arrow pointing
14 * upwards springs readily to mind.)
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <assert.h>
21 #include <ctype.h>
22 #include <math.h>
23
24 #include "puzzles.h"
25
26 #define TILE_SIZE 48
27 #define BORDER (TILE_SIZE / 2)
28 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
29 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
30 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
31
32 #define PI 3.141592653589793238462643383279502884197169399
33
34 #define ANIM_PER_RADIUS_UNIT 0.13F
35 #define FLASH_FRAME 0.13F
36
37 enum {
38 COL_BACKGROUND,
39 COL_TEXT,
40 COL_HIGHLIGHT,
41 COL_HIGHLIGHT_GENTLE,
42 COL_LOWLIGHT,
43 COL_LOWLIGHT_GENTLE,
44 NCOLOURS
45 };
46
47 struct game_params {
48 int w, h, n;
49 int rowsonly;
50 };
51
52 struct game_state {
53 int w, h, n;
54 int *grid;
55 int completed;
56 int movecount;
57 int lastx, lasty, lastr; /* coordinates of last rotation */
58 };
59
60 static game_params *default_params(void)
61 {
62 game_params *ret = snew(game_params);
63
64 ret->w = ret->h = 3;
65 ret->n = 2;
66 ret->rowsonly = FALSE;
67
68 return ret;
69 }
70
71
72 static void free_params(game_params *params)
73 {
74 sfree(params);
75 }
76
77 static game_params *dup_params(game_params *params)
78 {
79 game_params *ret = snew(game_params);
80 *ret = *params; /* structure copy */
81 return ret;
82 }
83
84 static int game_fetch_preset(int i, char **name, game_params **params)
85 {
86 static struct {
87 char *title;
88 game_params params;
89 } presets[] = {
90 { "3x3 rows only", { 3, 3, 2, TRUE } },
91 { "3x3 normal", { 3, 3, 2, FALSE } },
92 { "4x4 normal", { 4, 4, 2, FALSE } },
93 { "4x4 radius 3", { 4, 4, 3, FALSE } },
94 { "5x5 radius 3", { 5, 5, 3, FALSE } },
95 { "6x6 radius 4", { 6, 6, 4, FALSE } },
96 };
97
98 if (i < 0 || i >= lenof(presets))
99 return FALSE;
100
101 *name = dupstr(presets[i].title);
102 *params = dup_params(&presets[i].params);
103
104 return TRUE;
105 }
106
107 static game_params *decode_params(char const *string)
108 {
109 game_params *ret = snew(game_params);
110
111 ret->w = ret->h = atoi(string);
112 ret->n = 2;
113 ret->rowsonly = FALSE;
114 while (*string && isdigit(*string)) string++;
115 if (*string == 'x') {
116 string++;
117 ret->h = atoi(string);
118 while (*string && isdigit(*string)) string++;
119 }
120 if (*string == 'n') {
121 string++;
122 ret->n = atoi(string);
123 while (*string && isdigit(*string)) string++;
124 }
125 if (*string == 'r') {
126 string++;
127 ret->rowsonly = TRUE;
128 }
129
130 return ret;
131 }
132
133 static char *encode_params(game_params *params)
134 {
135 char buf[256];
136 sprintf(buf, "%dx%dn%d%s", params->w, params->h, params->n,
137 params->rowsonly ? "r" : "");
138 return dupstr(buf);
139 }
140
141 static config_item *game_configure(game_params *params)
142 {
143 config_item *ret;
144 char buf[80];
145
146 ret = snewn(4, config_item);
147
148 ret[0].name = "Width";
149 ret[0].type = C_STRING;
150 sprintf(buf, "%d", params->w);
151 ret[0].sval = dupstr(buf);
152 ret[0].ival = 0;
153
154 ret[1].name = "Height";
155 ret[1].type = C_STRING;
156 sprintf(buf, "%d", params->h);
157 ret[1].sval = dupstr(buf);
158 ret[1].ival = 0;
159
160 ret[2].name = "Rotation radius";
161 ret[2].type = C_STRING;
162 sprintf(buf, "%d", params->n);
163 ret[2].sval = dupstr(buf);
164 ret[2].ival = 0;
165
166 ret[3].name = "One number per row";
167 ret[3].type = C_BOOLEAN;
168 ret[3].sval = NULL;
169 ret[3].ival = params->rowsonly;
170
171 ret[4].name = NULL;
172 ret[4].type = C_END;
173 ret[4].sval = NULL;
174 ret[4].ival = 0;
175
176 return ret;
177 }
178
179 static game_params *custom_params(config_item *cfg)
180 {
181 game_params *ret = snew(game_params);
182
183 ret->w = atoi(cfg[0].sval);
184 ret->h = atoi(cfg[1].sval);
185 ret->n = atoi(cfg[2].sval);
186 ret->rowsonly = cfg[3].ival;
187
188 return ret;
189 }
190
191 static char *validate_params(game_params *params)
192 {
193 if (params->n < 2)
194 return "Rotation radius must be at least two";
195 if (params->w < params->n)
196 return "Width must be at least the rotation radius";
197 if (params->h < params->n)
198 return "Height must be at least the rotation radius";
199 return NULL;
200 }
201
202 /*
203 * This function actually performs a rotation on a grid. The `x'
204 * and `y' coordinates passed in are the coordinates of the _top
205 * left corner_ of the rotated region. (Using the centre would have
206 * involved half-integers and been annoyingly fiddly. Clicking in
207 * the centre is good for a user interface, but too inconvenient to
208 * use internally.)
209 */
210 static void do_rotate(int *grid, int w, int h, int n, int x, int y, int dir)
211 {
212 int i, j;
213
214 assert(x >= 0 && x+n <= w);
215 assert(y >= 0 && y+n <= h);
216 dir &= 3;
217 if (dir == 0)
218 return; /* nothing to do */
219
220 grid += y*w+x; /* translate region to top corner */
221
222 /*
223 * If we were leaving the result of the rotation in a separate
224 * grid, the simple thing to do would be to loop over each
225 * square within the rotated region and assign it from its
226 * source square. However, to do it in place without taking
227 * O(n^2) memory, we need to be marginally more clever. What
228 * I'm going to do is loop over about one _quarter_ of the
229 * rotated region and permute each element within that quarter
230 * with its rotational coset.
231 *
232 * The size of the region I need to loop over is (n+1)/2 by
233 * n/2, which is an obvious exact quarter for even n and is a
234 * rectangle for odd n. (For odd n, this technique leaves out
235 * one element of the square, which is of course the central
236 * one that never moves anyway.)
237 */
238 for (i = 0; i < (n+1)/2; i++) {
239 for (j = 0; j < n/2; j++) {
240 int k;
241 int g[4];
242 int p[4] = {
243 j*w+i,
244 i*w+(n-j-1),
245 (n-j-1)*w+(n-i-1),
246 (n-i-1)*w+j
247 };
248
249 for (k = 0; k < 4; k++)
250 g[k] = grid[p[k]];
251
252 for (k = 0; k < 4; k++)
253 grid[p[k]] = g[(k+dir) & 3];
254 }
255 }
256 }
257
258 static int grid_complete(int *grid, int wh)
259 {
260 int ok = TRUE;
261 int i;
262 for (i = 1; i < wh; i++)
263 if (grid[i] < grid[i-1])
264 ok = FALSE;
265 return ok;
266 }
267
268 static char *new_game_seed(game_params *params, random_state *rs)
269 {
270 int *grid;
271 int w = params->w, h = params->h, n = params->n, wh = w*h;
272 int i;
273 char *ret;
274 int retlen;
275 int total_moves;
276
277 /*
278 * Set up a solved grid.
279 */
280 grid = snewn(wh, int);
281 for (i = 0; i < wh; i++)
282 grid[i] = (params->rowsonly ? i/w : i) + 1;
283
284 /*
285 * Shuffle it. This game is complex enough that I don't feel up
286 * to analysing its full symmetry properties (particularly at
287 * n=4 and above!), so I'm going to do it the pedestrian way
288 * and simply shuffle the grid by making a long sequence of
289 * randomly chosen moves.
290 */
291 total_moves = w*h*n*n*2;
292 for (i = 0; i < total_moves; i++) {
293 int x, y;
294
295 x = random_upto(rs, w - n + 1);
296 y = random_upto(rs, h - n + 1);
297 do_rotate(grid, w, h, n, x, y, 1 + random_upto(rs, 3));
298
299 /*
300 * Optionally one more move in case the entire grid has
301 * happened to come out solved.
302 */
303 if (i == total_moves - 1 && grid_complete(grid, wh))
304 i--;
305 }
306
307 /*
308 * Now construct the game seed, by describing the grid as a
309 * simple sequence of comma-separated integers.
310 */
311 ret = NULL;
312 retlen = 0;
313 for (i = 0; i < wh; i++) {
314 char buf[80];
315 int k;
316
317 k = sprintf(buf, "%d,", grid[i]);
318
319 ret = sresize(ret, retlen + k + 1, char);
320 strcpy(ret + retlen, buf);
321 retlen += k;
322 }
323 ret[retlen-1] = '\0'; /* delete last comma */
324
325 sfree(grid);
326 return ret;
327 }
328
329 static char *validate_seed(game_params *params, char *seed)
330 {
331 char *p, *err;
332 int w = params->w, h = params->h, wh = w*h;
333 int i;
334
335 p = seed;
336 err = NULL;
337
338 for (i = 0; i < wh; i++) {
339 if (*p < '0' || *p > '9') {
340 return "Not enough numbers in string";
341 }
342 while (*p >= '0' && *p <= '9')
343 p++;
344 if (i < wh-1 && *p != ',') {
345 return "Expected comma after number";
346 }
347 else if (i == wh-1 && *p) {
348 return "Excess junk at end of string";
349 }
350
351 if (*p) p++; /* eat comma */
352 }
353
354 return NULL;
355 }
356
357 static game_state *new_game(game_params *params, char *seed)
358 {
359 game_state *state = snew(game_state);
360 int w = params->w, h = params->h, n = params->n, wh = w*h;
361 int i;
362 char *p;
363
364 state->w = w;
365 state->h = h;
366 state->n = n;
367 state->completed = 0;
368 state->movecount = 0;
369 state->lastx = state->lasty = state->lastr = -1;
370
371 state->grid = snewn(wh, int);
372
373 p = seed;
374
375 for (i = 0; i < wh; i++) {
376 state->grid[i] = atoi(p);
377 while (*p >= '0' && *p <= '9')
378 p++;
379
380 if (*p) p++; /* eat comma */
381 }
382
383 return state;
384 }
385
386 static game_state *dup_game(game_state *state)
387 {
388 game_state *ret = snew(game_state);
389
390 ret->w = state->w;
391 ret->h = state->h;
392 ret->n = state->n;
393 ret->completed = state->completed;
394 ret->movecount = state->movecount;
395 ret->lastx = state->lastx;
396 ret->lasty = state->lasty;
397 ret->lastr = state->lastr;
398
399 ret->grid = snewn(ret->w * ret->h, int);
400 memcpy(ret->grid, state->grid, ret->w * ret->h * sizeof(int));
401
402 return ret;
403 }
404
405 static void free_game(game_state *state)
406 {
407 sfree(state->grid);
408 sfree(state);
409 }
410
411 static game_ui *new_ui(game_state *state)
412 {
413 return NULL;
414 }
415
416 static void free_ui(game_ui *ui)
417 {
418 }
419
420 static game_state *make_move(game_state *from, game_ui *ui, int x, int y,
421 int button)
422 {
423 int w = from->w, h = from->h, n = from->n, wh = w*h;
424 game_state *ret;
425 int dir;
426
427 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
428 /*
429 * Determine the coordinates of the click. We offset by n-1
430 * half-blocks so that the user must click at the centre of
431 * a rotation region rather than at the corner.
432 */
433 x -= (n-1) * TILE_SIZE / 2;
434 y -= (n-1) * TILE_SIZE / 2;
435 x = FROMCOORD(x);
436 y = FROMCOORD(y);
437 if (x < 0 || x > w-n || y < 0 || y > w-n)
438 return NULL;
439
440 /*
441 * This is a valid move. Make it.
442 */
443 ret = dup_game(from);
444 ret->movecount++;
445 dir = (button == LEFT_BUTTON ? 1 : -1);
446 do_rotate(ret->grid, w, h, n, x, y, dir);
447 ret->lastx = x;
448 ret->lasty = y;
449 ret->lastr = dir;
450
451 /*
452 * See if the game has been completed. To do this we simply
453 * test that the grid contents are in increasing order.
454 */
455 if (!ret->completed && grid_complete(ret->grid, wh))
456 ret->completed = ret->movecount;
457 return ret;
458 }
459 return NULL;
460 }
461
462 /* ----------------------------------------------------------------------
463 * Drawing routines.
464 */
465
466 struct game_drawstate {
467 int started;
468 int w, h, bgcolour;
469 int *grid;
470 };
471
472 static void game_size(game_params *params, int *x, int *y)
473 {
474 *x = TILE_SIZE * params->w + 2 * BORDER;
475 *y = TILE_SIZE * params->h + 2 * BORDER;
476 }
477
478 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
479 {
480 float *ret = snewn(3 * NCOLOURS, float);
481 int i;
482 float max;
483
484 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
485
486 /*
487 * Drop the background colour so that the highlight is
488 * noticeably brighter than it while still being under 1.
489 */
490 max = ret[COL_BACKGROUND*3];
491 for (i = 1; i < 3; i++)
492 if (ret[COL_BACKGROUND*3+i] > max)
493 max = ret[COL_BACKGROUND*3+i];
494 if (max * 1.2F > 1.0F) {
495 for (i = 0; i < 3; i++)
496 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
497 }
498
499 for (i = 0; i < 3; i++) {
500 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
501 ret[COL_HIGHLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.1F;
502 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
503 ret[COL_LOWLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
504 ret[COL_TEXT * 3 + i] = 0.0;
505 }
506
507 *ncolours = NCOLOURS;
508 return ret;
509 }
510
511 static game_drawstate *game_new_drawstate(game_state *state)
512 {
513 struct game_drawstate *ds = snew(struct game_drawstate);
514 int i;
515
516 ds->started = FALSE;
517 ds->w = state->w;
518 ds->h = state->h;
519 ds->bgcolour = COL_BACKGROUND;
520 ds->grid = snewn(ds->w*ds->h, int);
521 for (i = 0; i < ds->w*ds->h; i++)
522 ds->grid[i] = -1;
523
524 return ds;
525 }
526
527 static void game_free_drawstate(game_drawstate *ds)
528 {
529 sfree(ds);
530 }
531
532 struct rotation {
533 int cx, cy, cw, ch; /* clip region */
534 int ox, oy; /* rotation origin */
535 float c, s; /* cos and sin of rotation angle */
536 int lc, rc, tc, bc; /* colours of tile edges */
537 };
538
539 static void rotate(int *xy, struct rotation *rot)
540 {
541 if (rot) {
542 float xf = xy[0] - rot->ox, yf = xy[1] - rot->oy;
543 float xf2, yf2;
544
545 xf2 = rot->c * xf + rot->s * yf;
546 yf2 = - rot->s * xf + rot->c * yf;
547
548 xy[0] = xf2 + rot->ox + 0.5; /* round to nearest */
549 xy[1] = yf2 + rot->oy + 0.5; /* round to nearest */
550 }
551 }
552
553 static void draw_tile(frontend *fe, game_state *state, int x, int y,
554 int tile, int flash_colour, struct rotation *rot)
555 {
556 int coords[8];
557 char str[40];
558
559 if (rot)
560 clip(fe, rot->cx, rot->cy, rot->cw, rot->ch);
561
562 /*
563 * We must draw each side of the tile's highlight separately,
564 * because in some cases (during rotation) they will all need
565 * to be different colours.
566 */
567
568 /* The centre point is common to all sides. */
569 coords[4] = x + TILE_SIZE / 2;
570 coords[5] = y + TILE_SIZE / 2;
571 rotate(coords+4, rot);
572
573 /* Right side. */
574 coords[0] = x + TILE_SIZE - 1;
575 coords[1] = y + TILE_SIZE - 1;
576 rotate(coords+0, rot);
577 coords[2] = x + TILE_SIZE - 1;
578 coords[3] = y;
579 rotate(coords+2, rot);
580 draw_polygon(fe, coords, 3, TRUE, rot ? rot->rc : COL_LOWLIGHT);
581 draw_polygon(fe, coords, 3, FALSE, rot ? rot->rc : COL_LOWLIGHT);
582
583 /* Bottom side. */
584 coords[2] = x;
585 coords[3] = y + TILE_SIZE - 1;
586 rotate(coords+2, rot);
587 draw_polygon(fe, coords, 3, TRUE, rot ? rot->bc : COL_LOWLIGHT);
588 draw_polygon(fe, coords, 3, FALSE, rot ? rot->bc : COL_LOWLIGHT);
589
590 /* Left side. */
591 coords[0] = x;
592 coords[1] = y;
593 rotate(coords+0, rot);
594 draw_polygon(fe, coords, 3, TRUE, rot ? rot->lc : COL_HIGHLIGHT);
595 draw_polygon(fe, coords, 3, FALSE, rot ? rot->lc : COL_HIGHLIGHT);
596
597 /* Top side. */
598 coords[2] = x + TILE_SIZE - 1;
599 coords[3] = y;
600 rotate(coords+2, rot);
601 draw_polygon(fe, coords, 3, TRUE, rot ? rot->tc : COL_HIGHLIGHT);
602 draw_polygon(fe, coords, 3, FALSE, rot ? rot->tc : COL_HIGHLIGHT);
603
604 if (rot) {
605 coords[0] = x + HIGHLIGHT_WIDTH;
606 coords[1] = y + HIGHLIGHT_WIDTH;
607 rotate(coords+0, rot);
608 coords[2] = x + HIGHLIGHT_WIDTH;
609 coords[3] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
610 rotate(coords+2, rot);
611 coords[4] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
612 coords[5] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
613 rotate(coords+4, rot);
614 coords[6] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
615 coords[7] = y + HIGHLIGHT_WIDTH;
616 rotate(coords+6, rot);
617 draw_polygon(fe, coords, 4, TRUE, flash_colour);
618 draw_polygon(fe, coords, 4, FALSE, flash_colour);
619 } else {
620 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
621 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
622 flash_colour);
623 }
624
625 coords[0] = x + TILE_SIZE/2;
626 coords[1] = y + TILE_SIZE/2;
627 rotate(coords+0, rot);
628 sprintf(str, "%d", tile);
629 draw_text(fe, coords[0], coords[1],
630 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
631 COL_TEXT, str);
632
633 if (rot)
634 unclip(fe);
635
636 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
637 }
638
639 static int highlight_colour(float angle)
640 {
641 int colours[32] = {
642 COL_LOWLIGHT,
643 COL_LOWLIGHT_GENTLE,
644 COL_LOWLIGHT_GENTLE,
645 COL_LOWLIGHT_GENTLE,
646 COL_HIGHLIGHT_GENTLE,
647 COL_HIGHLIGHT_GENTLE,
648 COL_HIGHLIGHT_GENTLE,
649 COL_HIGHLIGHT,
650 COL_HIGHLIGHT,
651 COL_HIGHLIGHT,
652 COL_HIGHLIGHT,
653 COL_HIGHLIGHT,
654 COL_HIGHLIGHT,
655 COL_HIGHLIGHT,
656 COL_HIGHLIGHT,
657 COL_HIGHLIGHT,
658 COL_HIGHLIGHT,
659 COL_HIGHLIGHT_GENTLE,
660 COL_HIGHLIGHT_GENTLE,
661 COL_HIGHLIGHT_GENTLE,
662 COL_LOWLIGHT_GENTLE,
663 COL_LOWLIGHT_GENTLE,
664 COL_LOWLIGHT_GENTLE,
665 COL_LOWLIGHT,
666 COL_LOWLIGHT,
667 COL_LOWLIGHT,
668 COL_LOWLIGHT,
669 COL_LOWLIGHT,
670 COL_LOWLIGHT,
671 COL_LOWLIGHT,
672 COL_LOWLIGHT,
673 COL_LOWLIGHT,
674 };
675
676 return colours[(int)((angle + 2*PI) / (PI/16)) & 31];
677 }
678
679 static float game_anim_length(game_state *oldstate, game_state *newstate,
680 int dir)
681 {
682 return ANIM_PER_RADIUS_UNIT * sqrt(newstate->n-1);
683 }
684
685 static float game_flash_length(game_state *oldstate, game_state *newstate,
686 int dir)
687 {
688 if (!oldstate->completed && newstate->completed)
689 return 2 * FLASH_FRAME;
690 else
691 return 0.0F;
692 }
693
694 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
695 game_state *state, int dir, game_ui *ui,
696 float animtime, float flashtime)
697 {
698 int i, bgcolour;
699 struct rotation srot, *rot;
700 int lastx = -1, lasty = -1, lastr = -1;
701
702 if (flashtime > 0) {
703 int frame = (int)(flashtime / FLASH_FRAME);
704 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
705 } else
706 bgcolour = COL_BACKGROUND;
707
708 if (!ds->started) {
709 int coords[6];
710
711 draw_rect(fe, 0, 0,
712 TILE_SIZE * state->w + 2 * BORDER,
713 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
714 draw_update(fe, 0, 0,
715 TILE_SIZE * state->w + 2 * BORDER,
716 TILE_SIZE * state->h + 2 * BORDER);
717
718 /*
719 * Recessed area containing the whole puzzle.
720 */
721 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
722 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
723 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
724 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
725 coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
726 coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
727 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
728 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
729
730 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
731 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
732 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
733 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
734
735 ds->started = TRUE;
736 }
737
738 /*
739 * If we're drawing any rotated tiles, sort out the rotation
740 * parameters, and also zap the rotation region to the
741 * background colour before doing anything else.
742 */
743 if (oldstate) {
744 float angle;
745 float anim_max = game_anim_length(oldstate, state, dir);
746
747 if (dir > 0) {
748 lastx = state->lastx;
749 lasty = state->lasty;
750 lastr = state->lastr;
751 } else {
752 lastx = oldstate->lastx;
753 lasty = oldstate->lasty;
754 lastr = -oldstate->lastr;
755 }
756
757 rot = &srot;
758 rot->cx = COORD(lastx);
759 rot->cy = COORD(lasty);
760 rot->cw = rot->ch = TILE_SIZE * state->n;
761 rot->ox = rot->cx + rot->cw/2;
762 rot->oy = rot->cy + rot->ch/2;
763 angle = (-PI/2 * lastr) * (1.0 - animtime / anim_max);
764 rot->c = cos(angle);
765 rot->s = sin(angle);
766
767 /*
768 * Sort out the colours of the various sides of the tile.
769 */
770 rot->lc = highlight_colour(PI + angle);
771 rot->rc = highlight_colour(angle);
772 rot->tc = highlight_colour(PI/2 + angle);
773 rot->bc = highlight_colour(-PI/2 + angle);
774
775 draw_rect(fe, rot->cx, rot->cy, rot->cw, rot->ch, bgcolour);
776 } else
777 rot = NULL;
778
779 /*
780 * Now draw each tile.
781 */
782 for (i = 0; i < state->w * state->h; i++) {
783 int t;
784 int tx = i % state->w, ty = i / state->w;
785
786 /*
787 * Figure out what should be displayed at this location.
788 * Usually it will be state->grid[i], unless we're in the
789 * middle of animating an actual rotation and this cell is
790 * within the rotation region, in which case we set -1
791 * (always display).
792 */
793 if (oldstate && lastx >= 0 && lasty >= 0 &&
794 tx >= lastx && tx < lastx + state->n &&
795 ty >= lasty && ty < lasty + state->n)
796 t = -1;
797 else
798 t = state->grid[i];
799
800 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
801 ds->grid[i] != t || ds->grid[i] == -1 || t == -1) {
802 int x = COORD(tx), y = COORD(ty);
803
804 draw_tile(fe, state, x, y, state->grid[i], bgcolour, rot);
805 ds->grid[i] = t;
806 }
807 }
808 ds->bgcolour = bgcolour;
809
810 /*
811 * Update the status bar.
812 */
813 {
814 char statusbuf[256];
815
816 /*
817 * Don't show the new status until we're also showing the
818 * new _state_ - after the game animation is complete.
819 */
820 if (oldstate)
821 state = oldstate;
822
823 sprintf(statusbuf, "%sMoves: %d",
824 (state->completed ? "COMPLETED! " : ""),
825 (state->completed ? state->completed : state->movecount));
826
827 status_bar(fe, statusbuf);
828 }
829 }
830
831 static int game_wants_statusbar(void)
832 {
833 return TRUE;
834 }
835
836 #ifdef COMBINED
837 #define thegame twiddle
838 #endif
839
840 const struct game thegame = {
841 "Twiddle", "games.twiddle", TRUE,
842 default_params,
843 game_fetch_preset,
844 decode_params,
845 encode_params,
846 free_params,
847 dup_params,
848 game_configure,
849 custom_params,
850 validate_params,
851 new_game_seed,
852 validate_seed,
853 new_game,
854 dup_game,
855 free_game,
856 new_ui,
857 free_ui,
858 make_move,
859 game_size,
860 game_colours,
861 game_new_drawstate,
862 game_free_drawstate,
863 game_redraw,
864 game_anim_length,
865 game_flash_length,
866 game_wants_statusbar,
867 };