chiark / gitweb /
First cut at a game timer. Yet another backend function which
[sgt-puzzles.git] / 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 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <ctype.h>
13 #include <math.h>
14
15 #include "puzzles.h"
16
17 #define TILE_SIZE 48
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 )
22
23 #define PI 3.141592653589793238462643383279502884197169399
24
25 #define ANIM_PER_RADIUS_UNIT 0.13F
26 #define FLASH_FRAME 0.13F
27
28 enum {
29     COL_BACKGROUND,
30     COL_TEXT,
31     COL_HIGHLIGHT,
32     COL_HIGHLIGHT_GENTLE,
33     COL_LOWLIGHT,
34     COL_LOWLIGHT_GENTLE,
35     NCOLOURS
36 };
37
38 struct game_params {
39     int w, h, n;
40     int rowsonly;
41     int orientable;
42     int movetarget;
43 };
44
45 struct game_state {
46     int w, h, n;
47     int orientable;
48     int *grid;
49     int completed;
50     int just_used_solve;               /* used to suppress undo animation */
51     int used_solve;                    /* used to suppress completion flash */
52     int movecount, movetarget;
53     int lastx, lasty, lastr;           /* coordinates of last rotation */
54 };
55
56 static game_params *default_params(void)
57 {
58     game_params *ret = snew(game_params);
59
60     ret->w = ret->h = 3;
61     ret->n = 2;
62     ret->rowsonly = ret->orientable = FALSE;
63     ret->movetarget = 0;
64
65     return ret;
66 }
67
68
69 static void free_params(game_params *params)
70 {
71     sfree(params);
72 }
73
74 static game_params *dup_params(game_params *params)
75 {
76     game_params *ret = snew(game_params);
77     *ret = *params;                    /* structure copy */
78     return ret;
79 }
80
81 static int game_fetch_preset(int i, char **name, game_params **params)
82 {
83     static struct {
84         char *title;
85         game_params params;
86     } presets[] = {
87         { "3x3 rows only", { 3, 3, 2, TRUE, FALSE } },
88         { "3x3 normal", { 3, 3, 2, FALSE, FALSE } },
89         { "3x3 orientable", { 3, 3, 2, FALSE, TRUE } },
90         { "4x4 normal", { 4, 4, 2, FALSE } },
91         { "4x4 orientable", { 4, 4, 2, FALSE, TRUE } },
92         { "4x4 radius 3", { 4, 4, 3, FALSE } },
93         { "5x5 radius 3", { 5, 5, 3, FALSE } },
94         { "6x6 radius 4", { 6, 6, 4, FALSE } },
95     };
96
97     if (i < 0 || i >= lenof(presets))
98         return FALSE;
99
100     *name = dupstr(presets[i].title);
101     *params = dup_params(&presets[i].params);
102
103     return TRUE;
104 }
105
106 static void decode_params(game_params *ret, char const *string)
107 {
108     ret->w = ret->h = atoi(string);
109     ret->n = 2;
110     ret->rowsonly = ret->orientable = FALSE;
111     ret->movetarget = 0;
112     while (*string && isdigit(*string)) string++;
113     if (*string == 'x') {
114         string++;
115         ret->h = atoi(string);
116         while (*string && isdigit(*string)) string++;
117     }
118     if (*string == 'n') {
119         string++;
120         ret->n = atoi(string);
121         while (*string && isdigit(*string)) string++;
122     }
123     while (*string) {
124         if (*string == 'r') {
125             ret->rowsonly = TRUE;
126         } else if (*string == 'o') {
127             ret->orientable = TRUE;
128         } else if (*string == 'm') {
129             string++;
130             ret->movetarget = atoi(string);
131             while (string[1] && isdigit(string[1])) string++;
132         }
133         string++;
134     }
135 }
136
137 static char *encode_params(game_params *params, int full)
138 {
139     char buf[256];
140     sprintf(buf, "%dx%dn%d%s%s", params->w, params->h, params->n,
141             params->rowsonly ? "r" : "",
142             params->orientable ? "o" : "");
143     /* Shuffle limit is part of the limited parameters, because we have to
144      * supply the target move count. */
145     if (params->movetarget)
146         sprintf(buf + strlen(buf), "m%d", params->movetarget);
147     return dupstr(buf);
148 }
149
150 static config_item *game_configure(game_params *params)
151 {
152     config_item *ret;
153     char buf[80];
154
155     ret = snewn(7, config_item);
156
157     ret[0].name = "Width";
158     ret[0].type = C_STRING;
159     sprintf(buf, "%d", params->w);
160     ret[0].sval = dupstr(buf);
161     ret[0].ival = 0;
162
163     ret[1].name = "Height";
164     ret[1].type = C_STRING;
165     sprintf(buf, "%d", params->h);
166     ret[1].sval = dupstr(buf);
167     ret[1].ival = 0;
168
169     ret[2].name = "Rotation radius";
170     ret[2].type = C_STRING;
171     sprintf(buf, "%d", params->n);
172     ret[2].sval = dupstr(buf);
173     ret[2].ival = 0;
174
175     ret[3].name = "One number per row";
176     ret[3].type = C_BOOLEAN;
177     ret[3].sval = NULL;
178     ret[3].ival = params->rowsonly;
179
180     ret[4].name = "Orientation matters";
181     ret[4].type = C_BOOLEAN;
182     ret[4].sval = NULL;
183     ret[4].ival = params->orientable;
184
185     ret[5].name = "Number of shuffling moves";
186     ret[5].type = C_STRING;
187     sprintf(buf, "%d", params->movetarget);
188     ret[5].sval = dupstr(buf);
189     ret[5].ival = 0;
190
191     ret[6].name = NULL;
192     ret[6].type = C_END;
193     ret[6].sval = NULL;
194     ret[6].ival = 0;
195
196     return ret;
197 }
198
199 static game_params *custom_params(config_item *cfg)
200 {
201     game_params *ret = snew(game_params);
202
203     ret->w = atoi(cfg[0].sval);
204     ret->h = atoi(cfg[1].sval);
205     ret->n = atoi(cfg[2].sval);
206     ret->rowsonly = cfg[3].ival;
207     ret->orientable = cfg[4].ival;
208     ret->movetarget = atoi(cfg[5].sval);
209
210     return ret;
211 }
212
213 static char *validate_params(game_params *params)
214 {
215     if (params->n < 2)
216         return "Rotation radius must be at least two";
217     if (params->w < params->n)
218         return "Width must be at least the rotation radius";
219     if (params->h < params->n)
220         return "Height must be at least the rotation radius";
221     return NULL;
222 }
223
224 /*
225  * This function actually performs a rotation on a grid. The `x'
226  * and `y' coordinates passed in are the coordinates of the _top
227  * left corner_ of the rotated region. (Using the centre would have
228  * involved half-integers and been annoyingly fiddly. Clicking in
229  * the centre is good for a user interface, but too inconvenient to
230  * use internally.)
231  */
232 static void do_rotate(int *grid, int w, int h, int n, int orientable,
233                       int x, int y, int dir)
234 {
235     int i, j;
236
237     assert(x >= 0 && x+n <= w);
238     assert(y >= 0 && y+n <= h);
239     dir &= 3;
240     if (dir == 0)
241         return;                        /* nothing to do */
242
243     grid += y*w+x;                     /* translate region to top corner */
244
245     /*
246      * If we were leaving the result of the rotation in a separate
247      * grid, the simple thing to do would be to loop over each
248      * square within the rotated region and assign it from its
249      * source square. However, to do it in place without taking
250      * O(n^2) memory, we need to be marginally more clever. What
251      * I'm going to do is loop over about one _quarter_ of the
252      * rotated region and permute each element within that quarter
253      * with its rotational coset.
254      * 
255      * The size of the region I need to loop over is (n+1)/2 by
256      * n/2, which is an obvious exact quarter for even n and is a
257      * rectangle for odd n. (For odd n, this technique leaves out
258      * one element of the square, which is of course the central
259      * one that never moves anyway.)
260      */
261     for (i = 0; i < (n+1)/2; i++) {
262         for (j = 0; j < n/2; j++) {
263             int k;
264             int g[4];
265             int p[4] = {
266                 j*w+i,
267                 i*w+(n-j-1),
268                 (n-j-1)*w+(n-i-1),
269                 (n-i-1)*w+j
270             };
271
272             for (k = 0; k < 4; k++)
273                 g[k] = grid[p[k]];
274
275             for (k = 0; k < 4; k++) {
276                 int v = g[(k+dir) & 3];
277                 if (orientable)
278                     v ^= ((v+dir) ^ v) & 3;  /* alter orientation */
279                 grid[p[k]] = v;
280             }
281         }
282     }
283
284     /*
285      * Don't forget the orientation on the centre square, if n is
286      * odd.
287      */
288     if (orientable && (n & 1)) {
289         int v = grid[n/2*(w+1)];
290         v ^= ((v+dir) ^ v) & 3;  /* alter orientation */
291         grid[n/2*(w+1)] = v;
292     }
293 }
294
295 static int grid_complete(int *grid, int wh, int orientable)
296 {
297     int ok = TRUE;
298     int i;
299     for (i = 1; i < wh; i++)
300         if (grid[i] < grid[i-1])
301             ok = FALSE;
302     if (orientable) {
303         for (i = 0; i < wh; i++)
304             if (grid[i] & 3)
305                 ok = FALSE;
306     }
307     return ok;
308 }
309
310 static char *new_game_desc(game_params *params, random_state *rs,
311                            game_aux_info **aux)
312 {
313     int *grid;
314     int w = params->w, h = params->h, n = params->n, wh = w*h;
315     int i;
316     char *ret;
317     int retlen;
318     int total_moves;
319
320     /*
321      * Set up a solved grid.
322      */
323     grid = snewn(wh, int);
324     for (i = 0; i < wh; i++)
325         grid[i] = ((params->rowsonly ? i/w : i) + 1) * 4;
326
327     /*
328      * Shuffle it. This game is complex enough that I don't feel up
329      * to analysing its full symmetry properties (particularly at
330      * n=4 and above!), so I'm going to do it the pedestrian way
331      * and simply shuffle the grid by making a long sequence of
332      * randomly chosen moves.
333      */
334     total_moves = params->movetarget;
335     if (!total_moves)
336         total_moves = w*h*n*n*2 + random_upto(rs, 2);
337
338     do {
339         int oldx = -1, oldy = -1, oldr = -1;
340
341         for (i = 0; i < total_moves; i++) {
342             int x, y, r;
343
344             do {
345                 x = random_upto(rs, w - n + 1);
346                 y = random_upto(rs, h - n + 1);
347                 r = 1 + 2 * random_upto(rs, 2);
348             } while (x == oldx && y == oldy && (oldr == 0 || r == oldr));
349
350             do_rotate(grid, w, h, n, params->orientable,
351                       x, y, r);
352
353             /*
354              * Prevent immediate reversal of a previous move, or
355              * execution of three consecutive identical moves
356              * adding up to a single inverse move. One exception is
357              * when we only _have_ one x,y setting.
358              */
359             if (w != n || h != n) {
360                 if (oldx == x && oldy == y)
361                     oldr = 0;          /* now avoid _any_ move in this x,y */
362                 else
363                     oldr = -r & 3;     /* only prohibit the exact inverse */
364                 oldx = x;
365                 oldy = y;
366             }
367         }
368     } while (grid_complete(grid, wh, params->orientable));
369
370     /*
371      * Now construct the game description, by describing the grid
372      * as a simple sequence of integers. They're comma-separated,
373      * unless the puzzle is orientable in which case they're
374      * separated by orientation letters `u', `d', `l' and `r'.
375      */
376     ret = NULL;
377     retlen = 0;
378     for (i = 0; i < wh; i++) {
379         char buf[80];
380         int k;
381
382         k = sprintf(buf, "%d%c", grid[i] / 4,
383                     params->orientable ? "uldr"[grid[i] & 3] : ',');
384
385         ret = sresize(ret, retlen + k + 1, char);
386         strcpy(ret + retlen, buf);
387         retlen += k;
388     }
389     if (!params->orientable)
390         ret[retlen-1] = '\0';          /* delete last comma */
391
392     sfree(grid);
393     return ret;
394 }
395
396 static void game_free_aux_info(game_aux_info *aux)
397 {
398     assert(!"Shouldn't happen");
399 }
400
401 static char *validate_desc(game_params *params, char *desc)
402 {
403     char *p, *err;
404     int w = params->w, h = params->h, wh = w*h;
405     int i;
406
407     p = desc;
408     err = NULL;
409
410     for (i = 0; i < wh; i++) {
411         if (*p < '0' || *p > '9')
412             return "Not enough numbers in string";
413         while (*p >= '0' && *p <= '9')
414             p++;
415         if (!params->orientable && i < wh-1) {
416             if (*p != ',')
417                 return "Expected comma after number";
418         } else if (params->orientable && i < wh) {
419             if (*p != 'l' && *p != 'r' && *p != 'u' && *p != 'd')
420                 return "Expected orientation letter after number";
421         } else if (i == wh-1 && *p) {
422             return "Excess junk at end of string";
423         }
424
425         if (*p) p++;                   /* eat comma */
426     }
427
428     return NULL;
429 }
430
431 static game_state *new_game(midend_data *me, game_params *params, char *desc)
432 {
433     game_state *state = snew(game_state);
434     int w = params->w, h = params->h, n = params->n, wh = w*h;
435     int i;
436     char *p;
437
438     state->w = w;
439     state->h = h;
440     state->n = n;
441     state->orientable = params->orientable;
442     state->completed = 0;
443     state->used_solve = state->just_used_solve = FALSE;
444     state->movecount = 0;
445     state->movetarget = params->movetarget;
446     state->lastx = state->lasty = state->lastr = -1;
447
448     state->grid = snewn(wh, int);
449
450     p = desc;
451
452     for (i = 0; i < wh; i++) {
453         state->grid[i] = 4 * atoi(p);
454         while (*p >= '0' && *p <= '9')
455             p++;
456         if (*p) {
457             if (params->orientable) {
458                 switch (*p) {
459                   case 'l': state->grid[i] |= 1; break;
460                   case 'd': state->grid[i] |= 2; break;
461                   case 'r': state->grid[i] |= 3; break;
462                 }
463             }
464             p++;
465         }
466     }
467
468     return state;
469 }
470
471 static game_state *dup_game(game_state *state)
472 {
473     game_state *ret = snew(game_state);
474
475     ret->w = state->w;
476     ret->h = state->h;
477     ret->n = state->n;
478     ret->orientable = state->orientable;
479     ret->completed = state->completed;
480     ret->movecount = state->movecount;
481     ret->movetarget = state->movetarget;
482     ret->lastx = state->lastx;
483     ret->lasty = state->lasty;
484     ret->lastr = state->lastr;
485     ret->used_solve = state->used_solve;
486     ret->just_used_solve = state->just_used_solve;
487
488     ret->grid = snewn(ret->w * ret->h, int);
489     memcpy(ret->grid, state->grid, ret->w * ret->h * sizeof(int));
490
491     return ret;
492 }
493
494 static void free_game(game_state *state)
495 {
496     sfree(state->grid);
497     sfree(state);
498 }
499
500 static int compare_int(const void *av, const void *bv)
501 {
502     const int *a = (const int *)av;
503     const int *b = (const int *)bv;
504     if (*a < *b)
505         return -1;
506     else if (*a > *b)
507         return +1;
508     else
509         return 0;
510 }
511
512 static game_state *solve_game(game_state *state, game_aux_info *aux,
513                               char **error)
514 {
515     game_state *ret = dup_game(state);
516     int i;
517
518     /*
519      * Simply replace the grid with a solved one. For this game,
520      * this isn't a useful operation for actually telling the user
521      * what they should have done, but it is useful for
522      * conveniently being able to get hold of a clean state from
523      * which to practise manoeuvres.
524      */
525     qsort(ret->grid, ret->w*ret->h, sizeof(int), compare_int);
526     for (i = 0; i < ret->w*ret->h; i++)
527         ret->grid[i] &= ~3;
528     ret->used_solve = ret->just_used_solve = TRUE;
529     ret->completed = ret->movecount = 1;
530
531     return ret;
532 }
533
534 static char *game_text_format(game_state *state)
535 {
536     char *ret, *p, buf[80];
537     int i, x, y, col, o, maxlen;
538
539     /*
540      * First work out how many characters we need to display each
541      * number. We're pretty flexible on grid contents here, so we
542      * have to scan the entire grid.
543      */
544     col = 0;
545     for (i = 0; i < state->w * state->h; i++) {
546         x = sprintf(buf, "%d", state->grid[i] / 4);
547         if (col < x) col = x;
548     }
549     o = (state->orientable ? 1 : 0);
550
551     /*
552      * Now we know the exact total size of the grid we're going to
553      * produce: it's got h rows, each containing w lots of col+o,
554      * w-1 spaces and a trailing newline.
555      */
556     maxlen = state->h * state->w * (col+o+1);
557
558     ret = snewn(maxlen+1, char);
559     p = ret;
560
561     for (y = 0; y < state->h; y++) {
562         for (x = 0; x < state->w; x++) {
563             int v = state->grid[state->w*y+x];
564             sprintf(buf, "%*d", col, v/4);
565             memcpy(p, buf, col);
566             p += col;
567             if (o)
568                 *p++ = "^<v>"[v & 3];
569             if (x+1 == state->w)
570                 *p++ = '\n';
571             else
572                 *p++ = ' ';
573         }
574     }
575
576     assert(p - ret == maxlen);
577     *p = '\0';
578     return ret;
579 }
580
581 static game_ui *new_ui(game_state *state)
582 {
583     return NULL;
584 }
585
586 static void free_ui(game_ui *ui)
587 {
588 }
589
590 static game_state *make_move(game_state *from, game_ui *ui, int x, int y,
591                              int button)
592 {
593     int w = from->w, h = from->h, n = from->n, wh = w*h;
594     game_state *ret;
595     int dir;
596
597     button = button & (~MOD_MASK | MOD_NUM_KEYPAD);
598
599     if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
600         /*
601          * Determine the coordinates of the click. We offset by n-1
602          * half-blocks so that the user must click at the centre of
603          * a rotation region rather than at the corner.
604          */
605         x -= (n-1) * TILE_SIZE / 2;
606         y -= (n-1) * TILE_SIZE / 2;
607         x = FROMCOORD(x);
608         y = FROMCOORD(y);
609         dir = (button == LEFT_BUTTON ? 1 : -1);
610         if (x < 0 || x > w-n || y < 0 || y > h-n)
611             return NULL;
612     } else if (button == 'a' || button == 'A' || button==MOD_NUM_KEYPAD+'7') {
613         x = y = 0;
614         dir = (button == 'A' ? -1 : +1);
615     } else if (button == 'b' || button == 'B' || button==MOD_NUM_KEYPAD+'9') {
616         x = w-n;
617         y = 0;
618         dir = (button == 'B' ? -1 : +1);
619     } else if (button == 'c' || button == 'C' || button==MOD_NUM_KEYPAD+'1') {
620         x = 0;
621         y = h-n;
622         dir = (button == 'C' ? -1 : +1);
623     } else if (button == 'd' || button == 'D' || button==MOD_NUM_KEYPAD+'3') {
624         x = w-n;
625         y = h-n;
626         dir = (button == 'D' ? -1 : +1);
627     } else if (button==MOD_NUM_KEYPAD+'8' && (w-n) % 2 == 0) {
628         x = (w-n) / 2;
629         y = 0;
630         dir = +1;
631     } else if (button==MOD_NUM_KEYPAD+'2' && (w-n) % 2 == 0) {
632         x = (w-n) / 2;
633         y = h-n;
634         dir = +1;
635     } else if (button==MOD_NUM_KEYPAD+'4' && (h-n) % 2 == 0) {
636         x = 0;
637         y = (h-n) / 2;
638         dir = +1;
639     } else if (button==MOD_NUM_KEYPAD+'6' && (h-n) % 2 == 0) {
640         x = w-n;
641         y = (h-n) / 2;
642         dir = +1;
643     } else if (button==MOD_NUM_KEYPAD+'5' && (w-n) % 2 == 0 && (h-n) % 2 == 0){
644         x = (w-n) / 2;
645         y = (h-n) / 2;
646         dir = +1;
647     } else {
648         return NULL;                   /* no move to be made */
649     }
650
651     /*
652      * This is a valid move. Make it.
653      */
654     ret = dup_game(from);
655     ret->just_used_solve = FALSE;  /* zero this in a hurry */
656     ret->movecount++;
657     do_rotate(ret->grid, w, h, n, ret->orientable, x, y, dir);
658     ret->lastx = x;
659     ret->lasty = y;
660     ret->lastr = dir;
661
662     /*
663      * See if the game has been completed. To do this we simply
664      * test that the grid contents are in increasing order.
665      */
666     if (!ret->completed && grid_complete(ret->grid, wh, ret->orientable))
667         ret->completed = ret->movecount;
668     return ret;
669 }
670
671 /* ----------------------------------------------------------------------
672  * Drawing routines.
673  */
674
675 struct game_drawstate {
676     int started;
677     int w, h, bgcolour;
678     int *grid;
679 };
680
681 static void game_size(game_params *params, int *x, int *y)
682 {
683     *x = TILE_SIZE * params->w + 2 * BORDER;
684     *y = TILE_SIZE * params->h + 2 * BORDER;
685 }
686
687 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
688 {
689     float *ret = snewn(3 * NCOLOURS, float);
690     int i;
691     float max;
692
693     frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
694
695     /*
696      * Drop the background colour so that the highlight is
697      * noticeably brighter than it while still being under 1.
698      */
699     max = ret[COL_BACKGROUND*3];
700     for (i = 1; i < 3; i++)
701         if (ret[COL_BACKGROUND*3+i] > max)
702             max = ret[COL_BACKGROUND*3+i];
703     if (max * 1.2F > 1.0F) {
704         for (i = 0; i < 3; i++)
705             ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
706     }
707
708     for (i = 0; i < 3; i++) {
709         ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
710         ret[COL_HIGHLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.1F;
711         ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
712         ret[COL_LOWLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
713         ret[COL_TEXT * 3 + i] = 0.0;
714     }
715
716     *ncolours = NCOLOURS;
717     return ret;
718 }
719
720 static game_drawstate *game_new_drawstate(game_state *state)
721 {
722     struct game_drawstate *ds = snew(struct game_drawstate);
723     int i;
724
725     ds->started = FALSE;
726     ds->w = state->w;
727     ds->h = state->h;
728     ds->bgcolour = COL_BACKGROUND;
729     ds->grid = snewn(ds->w*ds->h, int);
730     for (i = 0; i < ds->w*ds->h; i++)
731         ds->grid[i] = -1;
732
733     return ds;
734 }
735
736 static void game_free_drawstate(game_drawstate *ds)
737 {
738     sfree(ds);
739 }
740
741 struct rotation {
742     int cx, cy, cw, ch;                /* clip region */
743     int ox, oy;                        /* rotation origin */
744     float c, s;                        /* cos and sin of rotation angle */
745     int lc, rc, tc, bc;                /* colours of tile edges */
746 };
747
748 static void rotate(int *xy, struct rotation *rot)
749 {
750     if (rot) {
751         float xf = xy[0] - rot->ox, yf = xy[1] - rot->oy;
752         float xf2, yf2;
753
754         xf2 = rot->c * xf + rot->s * yf;
755         yf2 = - rot->s * xf + rot->c * yf;
756
757         xy[0] = xf2 + rot->ox + 0.5;   /* round to nearest */
758         xy[1] = yf2 + rot->oy + 0.5;   /* round to nearest */
759     }
760 }
761
762 static void draw_tile(frontend *fe, game_state *state, int x, int y,
763                       int tile, int flash_colour, struct rotation *rot)
764 {
765     int coords[8];
766     char str[40];
767
768     /*
769      * If we've been passed a rotation region but we're drawing a
770      * tile which is outside it, we must draw it normally. This can
771      * occur if we're cleaning up after a completion flash while a
772      * new move is also being made.
773      */
774     if (rot && (x < rot->cx || y < rot->cy ||
775                 x >= rot->cx+rot->cw || y >= rot->cy+rot->ch))
776         rot = NULL;
777
778     if (rot)
779         clip(fe, rot->cx, rot->cy, rot->cw, rot->ch);
780
781     /*
782      * We must draw each side of the tile's highlight separately,
783      * because in some cases (during rotation) they will all need
784      * to be different colours.
785      */
786
787     /* The centre point is common to all sides. */
788     coords[4] = x + TILE_SIZE / 2;
789     coords[5] = y + TILE_SIZE / 2;
790     rotate(coords+4, rot);
791
792     /* Right side. */
793     coords[0] = x + TILE_SIZE - 1;
794     coords[1] = y + TILE_SIZE - 1;
795     rotate(coords+0, rot);
796     coords[2] = x + TILE_SIZE - 1;
797     coords[3] = y;
798     rotate(coords+2, rot);
799     draw_polygon(fe, coords, 3, TRUE, rot ? rot->rc : COL_LOWLIGHT);
800     draw_polygon(fe, coords, 3, FALSE, rot ? rot->rc : COL_LOWLIGHT);
801
802     /* Bottom side. */
803     coords[2] = x;
804     coords[3] = y + TILE_SIZE - 1;
805     rotate(coords+2, rot);
806     draw_polygon(fe, coords, 3, TRUE, rot ? rot->bc : COL_LOWLIGHT);
807     draw_polygon(fe, coords, 3, FALSE, rot ? rot->bc : COL_LOWLIGHT);
808
809     /* Left side. */
810     coords[0] = x;
811     coords[1] = y;
812     rotate(coords+0, rot);
813     draw_polygon(fe, coords, 3, TRUE, rot ? rot->lc : COL_HIGHLIGHT);
814     draw_polygon(fe, coords, 3, FALSE, rot ? rot->lc : COL_HIGHLIGHT);
815
816     /* Top side. */
817     coords[2] = x + TILE_SIZE - 1;
818     coords[3] = y;
819     rotate(coords+2, rot);
820     draw_polygon(fe, coords, 3, TRUE, rot ? rot->tc : COL_HIGHLIGHT);
821     draw_polygon(fe, coords, 3, FALSE, rot ? rot->tc : COL_HIGHLIGHT);
822
823     /*
824      * Now the main blank area in the centre of the tile.
825      */
826     if (rot) {
827         coords[0] = x + HIGHLIGHT_WIDTH;
828         coords[1] = y + HIGHLIGHT_WIDTH;
829         rotate(coords+0, rot);
830         coords[2] = x + HIGHLIGHT_WIDTH;
831         coords[3] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
832         rotate(coords+2, rot);
833         coords[4] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
834         coords[5] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
835         rotate(coords+4, rot);
836         coords[6] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
837         coords[7] = y + HIGHLIGHT_WIDTH;
838         rotate(coords+6, rot);
839         draw_polygon(fe, coords, 4, TRUE, flash_colour);
840         draw_polygon(fe, coords, 4, FALSE, flash_colour);
841     } else {
842         draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
843                   TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
844                   flash_colour);
845     }
846
847     /*
848      * Next, the triangles for orientation.
849      */
850     if (state->orientable) {
851         int xdx, xdy, ydx, ydy;
852         int cx, cy, displ, displ2;
853         switch (tile & 3) {
854           case 0:
855             xdx = 1, xdy = 0;
856             ydx = 0, ydy = 1;
857             break;
858           case 1:
859             xdx = 0, xdy = -1;
860             ydx = 1, ydy = 0;
861             break;
862           case 2:
863             xdx = -1, xdy = 0;
864             ydx = 0, ydy = -1;
865             break;
866           default /* case 3 */:
867             xdx = 0, xdy = 1;
868             ydx = -1, ydy = 0;
869             break;
870         }
871
872         cx = x + TILE_SIZE / 2;
873         cy = y + TILE_SIZE / 2;
874         displ = TILE_SIZE / 2 - HIGHLIGHT_WIDTH - 2;
875         displ2 = TILE_SIZE / 3 - HIGHLIGHT_WIDTH;
876
877         coords[0] = cx - displ * xdx + displ2 * ydx;
878         coords[1] = cy - displ * xdy + displ2 * ydy;
879         rotate(coords+0, rot);
880         coords[2] = cx + displ * xdx + displ2 * ydx;
881         coords[3] = cy + displ * xdy + displ2 * ydy;
882         rotate(coords+2, rot);
883         coords[4] = cx - displ * ydx;
884         coords[5] = cy - displ * ydy;
885         rotate(coords+4, rot);
886         draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT_GENTLE);
887         draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT_GENTLE);
888     }
889
890     coords[0] = x + TILE_SIZE/2;
891     coords[1] = y + TILE_SIZE/2;
892     rotate(coords+0, rot);
893     sprintf(str, "%d", tile / 4);
894     draw_text(fe, coords[0], coords[1],
895               FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
896               COL_TEXT, str);
897
898     if (rot)
899         unclip(fe);
900
901     draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
902 }
903
904 static int highlight_colour(float angle)
905 {
906     int colours[32] = {
907         COL_LOWLIGHT,
908         COL_LOWLIGHT_GENTLE,
909         COL_LOWLIGHT_GENTLE,
910         COL_LOWLIGHT_GENTLE,
911         COL_HIGHLIGHT_GENTLE,
912         COL_HIGHLIGHT_GENTLE,
913         COL_HIGHLIGHT_GENTLE,
914         COL_HIGHLIGHT,
915         COL_HIGHLIGHT,
916         COL_HIGHLIGHT,
917         COL_HIGHLIGHT,
918         COL_HIGHLIGHT,
919         COL_HIGHLIGHT,
920         COL_HIGHLIGHT,
921         COL_HIGHLIGHT,
922         COL_HIGHLIGHT,
923         COL_HIGHLIGHT,
924         COL_HIGHLIGHT_GENTLE,
925         COL_HIGHLIGHT_GENTLE,
926         COL_HIGHLIGHT_GENTLE,
927         COL_LOWLIGHT_GENTLE,
928         COL_LOWLIGHT_GENTLE,
929         COL_LOWLIGHT_GENTLE,
930         COL_LOWLIGHT,
931         COL_LOWLIGHT,
932         COL_LOWLIGHT,
933         COL_LOWLIGHT,
934         COL_LOWLIGHT,
935         COL_LOWLIGHT,
936         COL_LOWLIGHT,
937         COL_LOWLIGHT,
938         COL_LOWLIGHT,
939     };
940
941     return colours[(int)((angle + 2*PI) / (PI/16)) & 31];
942 }
943
944 static float game_anim_length(game_state *oldstate, game_state *newstate,
945                               int dir, game_ui *ui)
946 {
947     if ((dir > 0 && newstate->just_used_solve) ||
948         (dir < 0 && oldstate->just_used_solve))
949         return 0.0F;
950     else
951         return ANIM_PER_RADIUS_UNIT * sqrt(newstate->n-1);
952 }
953
954 static float game_flash_length(game_state *oldstate, game_state *newstate,
955                                int dir, game_ui *ui)
956 {
957     if (!oldstate->completed && newstate->completed &&
958         !oldstate->used_solve && !newstate->used_solve)
959         return 2 * FLASH_FRAME;
960     else
961         return 0.0F;
962 }
963
964 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
965                         game_state *state, int dir, game_ui *ui,
966                         float animtime, float flashtime)
967 {
968     int i, bgcolour;
969     struct rotation srot, *rot;
970     int lastx = -1, lasty = -1, lastr = -1;
971
972     if (flashtime > 0) {
973         int frame = (int)(flashtime / FLASH_FRAME);
974         bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
975     } else
976         bgcolour = COL_BACKGROUND;
977
978     if (!ds->started) {
979         int coords[6];
980
981         draw_rect(fe, 0, 0,
982                   TILE_SIZE * state->w + 2 * BORDER,
983                   TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
984         draw_update(fe, 0, 0,
985                     TILE_SIZE * state->w + 2 * BORDER,
986                     TILE_SIZE * state->h + 2 * BORDER);
987
988         /*
989          * Recessed area containing the whole puzzle.
990          */
991         coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
992         coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
993         coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
994         coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
995         coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
996         coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
997         draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
998         draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
999
1000         coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
1001         coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
1002         draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
1003         draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
1004
1005         ds->started = TRUE;
1006     }
1007
1008     /*
1009      * If we're drawing any rotated tiles, sort out the rotation
1010      * parameters, and also zap the rotation region to the
1011      * background colour before doing anything else.
1012      */
1013     if (oldstate) {
1014         float angle;
1015         float anim_max = game_anim_length(oldstate, state, dir, ui);
1016
1017         if (dir > 0) {
1018             lastx = state->lastx;
1019             lasty = state->lasty;
1020             lastr = state->lastr;
1021         } else {
1022             lastx = oldstate->lastx;
1023             lasty = oldstate->lasty;
1024             lastr = -oldstate->lastr;
1025         }
1026
1027         rot = &srot;
1028         rot->cx = COORD(lastx);
1029         rot->cy = COORD(lasty);
1030         rot->cw = rot->ch = TILE_SIZE * state->n;
1031         rot->ox = rot->cx + rot->cw/2;
1032         rot->oy = rot->cy + rot->ch/2;
1033         angle = (-PI/2 * lastr) * (1.0 - animtime / anim_max);
1034         rot->c = cos(angle);
1035         rot->s = sin(angle);
1036
1037         /*
1038          * Sort out the colours of the various sides of the tile.
1039          */
1040         rot->lc = highlight_colour(PI + angle);
1041         rot->rc = highlight_colour(angle);
1042         rot->tc = highlight_colour(PI/2 + angle);
1043         rot->bc = highlight_colour(-PI/2 + angle);
1044
1045         draw_rect(fe, rot->cx, rot->cy, rot->cw, rot->ch, bgcolour);
1046     } else
1047         rot = NULL;
1048
1049     /*
1050      * Now draw each tile.
1051      */
1052     for (i = 0; i < state->w * state->h; i++) {
1053         int t;
1054         int tx = i % state->w, ty = i / state->w;
1055
1056         /*
1057          * Figure out what should be displayed at this location.
1058          * Usually it will be state->grid[i], unless we're in the
1059          * middle of animating an actual rotation and this cell is
1060          * within the rotation region, in which case we set -1
1061          * (always display).
1062          */
1063         if (oldstate && lastx >= 0 && lasty >= 0 &&
1064             tx >= lastx && tx < lastx + state->n &&
1065             ty >= lasty && ty < lasty + state->n)
1066             t = -1;
1067         else
1068             t = state->grid[i];
1069
1070         if (ds->bgcolour != bgcolour ||   /* always redraw when flashing */
1071             ds->grid[i] != t || ds->grid[i] == -1 || t == -1) {
1072             int x = COORD(tx), y = COORD(ty);
1073
1074             draw_tile(fe, state, x, y, state->grid[i], bgcolour, rot);
1075             ds->grid[i] = t;
1076         }
1077     }
1078     ds->bgcolour = bgcolour;
1079
1080     /*
1081      * Update the status bar.
1082      */
1083     {
1084         char statusbuf[256];
1085
1086         /*
1087          * Don't show the new status until we're also showing the
1088          * new _state_ - after the game animation is complete.
1089          */
1090         if (oldstate)
1091             state = oldstate;
1092
1093         if (state->used_solve)
1094             sprintf(statusbuf, "Moves since auto-solve: %d",
1095                     state->movecount - state->completed);
1096         else {
1097             sprintf(statusbuf, "%sMoves: %d",
1098                     (state->completed ? "COMPLETED! " : ""),
1099                     (state->completed ? state->completed : state->movecount));
1100             if (state->movetarget)
1101                 sprintf(statusbuf+strlen(statusbuf), " (target %d)",
1102                         state->movetarget);
1103         }
1104
1105         status_bar(fe, statusbuf);
1106     }
1107 }
1108
1109 static int game_wants_statusbar(void)
1110 {
1111     return TRUE;
1112 }
1113
1114 static int game_timing_state(game_state *state)
1115 {
1116     return TRUE;
1117 }
1118
1119 #ifdef COMBINED
1120 #define thegame twiddle
1121 #endif
1122
1123 const struct game thegame = {
1124     "Twiddle", "games.twiddle",
1125     default_params,
1126     game_fetch_preset,
1127     decode_params,
1128     encode_params,
1129     free_params,
1130     dup_params,
1131     TRUE, game_configure, custom_params,
1132     validate_params,
1133     new_game_desc,
1134     game_free_aux_info,
1135     validate_desc,
1136     new_game,
1137     dup_game,
1138     free_game,
1139     TRUE, solve_game,
1140     TRUE, game_text_format,
1141     new_ui,
1142     free_ui,
1143     make_move,
1144     game_size,
1145     game_colours,
1146     game_new_drawstate,
1147     game_free_drawstate,
1148     game_redraw,
1149     game_anim_length,
1150     game_flash_length,
1151     game_wants_statusbar,
1152     FALSE, game_timing_state,
1153 };