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