Introduced a new function in every game which formats a game_state
[sgt/puzzles] / twiddle.c
CommitLineData
9038fd11 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
9038fd11 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
28enum {
29 COL_BACKGROUND,
30 COL_TEXT,
31 COL_HIGHLIGHT,
32 COL_HIGHLIGHT_GENTLE,
33 COL_LOWLIGHT,
34 COL_LOWLIGHT_GENTLE,
35 NCOLOURS
36};
37
38struct game_params {
39 int w, h, n;
40 int rowsonly;
a3631c72 41 int orientable;
9038fd11 42};
43
44struct game_state {
45 int w, h, n;
a3631c72 46 int orientable;
9038fd11 47 int *grid;
48 int completed;
49 int movecount;
50 int lastx, lasty, lastr; /* coordinates of last rotation */
51};
52
53static game_params *default_params(void)
54{
55 game_params *ret = snew(game_params);
56
57 ret->w = ret->h = 3;
58 ret->n = 2;
a3631c72 59 ret->rowsonly = ret->orientable = FALSE;
9038fd11 60
61 return ret;
62}
63
64
65static void free_params(game_params *params)
66{
67 sfree(params);
68}
69
70static game_params *dup_params(game_params *params)
71{
72 game_params *ret = snew(game_params);
73 *ret = *params; /* structure copy */
74 return ret;
75}
76
77static int game_fetch_preset(int i, char **name, game_params **params)
78{
79 static struct {
80 char *title;
81 game_params params;
82 } presets[] = {
a3631c72 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 } },
9038fd11 86 { "4x4 normal", { 4, 4, 2, FALSE } },
a3631c72 87 { "4x4 orientable", { 4, 4, 2, FALSE, TRUE } },
9038fd11 88 { "4x4 radius 3", { 4, 4, 3, FALSE } },
89 { "5x5 radius 3", { 5, 5, 3, FALSE } },
90 { "6x6 radius 4", { 6, 6, 4, FALSE } },
91 };
92
93 if (i < 0 || i >= lenof(presets))
94 return FALSE;
95
96 *name = dupstr(presets[i].title);
97 *params = dup_params(&presets[i].params);
98
99 return TRUE;
100}
101
102static game_params *decode_params(char const *string)
103{
104 game_params *ret = snew(game_params);
105
106 ret->w = ret->h = atoi(string);
107 ret->n = 2;
a3631c72 108 ret->rowsonly = ret->orientable = FALSE;
9038fd11 109 while (*string && isdigit(*string)) string++;
110 if (*string == 'x') {
111 string++;
112 ret->h = atoi(string);
113 while (*string && isdigit(*string)) string++;
114 }
115 if (*string == 'n') {
116 string++;
117 ret->n = atoi(string);
118 while (*string && isdigit(*string)) string++;
119 }
a3631c72 120 while (*string) {
121 if (*string == 'r') {
122 ret->rowsonly = TRUE;
123 } else if (*string == 'o') {
124 ret->orientable = TRUE;
125 }
9038fd11 126 string++;
9038fd11 127 }
128
129 return ret;
130}
131
132static char *encode_params(game_params *params)
133{
134 char buf[256];
a3631c72 135 sprintf(buf, "%dx%dn%d%s%s", params->w, params->h, params->n,
136 params->rowsonly ? "r" : "",
137 params->orientable ? "o" : "");
9038fd11 138 return dupstr(buf);
139}
140
141static config_item *game_configure(game_params *params)
142{
143 config_item *ret;
144 char buf[80];
145
a3631c72 146 ret = snewn(6, config_item);
9038fd11 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
a3631c72 171 ret[4].name = "Orientation matters";
172 ret[4].type = C_BOOLEAN;
9038fd11 173 ret[4].sval = NULL;
a3631c72 174 ret[4].ival = params->orientable;
175
176 ret[5].name = NULL;
177 ret[5].type = C_END;
178 ret[5].sval = NULL;
179 ret[5].ival = 0;
9038fd11 180
181 return ret;
182}
183
184static game_params *custom_params(config_item *cfg)
185{
186 game_params *ret = snew(game_params);
187
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;
a3631c72 192 ret->orientable = cfg[4].ival;
9038fd11 193
194 return ret;
195}
196
197static char *validate_params(game_params *params)
198{
199 if (params->n < 2)
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";
205 return NULL;
206}
207
208/*
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
214 * use internally.)
215 */
a3631c72 216static void do_rotate(int *grid, int w, int h, int n, int orientable,
217 int x, int y, int dir)
9038fd11 218{
219 int i, j;
220
221 assert(x >= 0 && x+n <= w);
222 assert(y >= 0 && y+n <= h);
223 dir &= 3;
224 if (dir == 0)
225 return; /* nothing to do */
226
227 grid += y*w+x; /* translate region to top corner */
228
229 /*
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.
238 *
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.)
244 */
245 for (i = 0; i < (n+1)/2; i++) {
246 for (j = 0; j < n/2; j++) {
247 int k;
248 int g[4];
249 int p[4] = {
250 j*w+i,
251 i*w+(n-j-1),
252 (n-j-1)*w+(n-i-1),
253 (n-i-1)*w+j
254 };
255
256 for (k = 0; k < 4; k++)
257 g[k] = grid[p[k]];
258
a3631c72 259 for (k = 0; k < 4; k++) {
260 int v = g[(k+dir) & 3];
261 if (orientable)
262 v ^= ((v+dir) ^ v) & 3; /* alter orientation */
263 grid[p[k]] = v;
264 }
9038fd11 265 }
266 }
a3631c72 267
268 /*
269 * Don't forget the orientation on the centre square, if n is
270 * odd.
271 */
272 if (orientable && (n & 1)) {
273 int v = grid[n/2*(w+1)];
274 v ^= ((v+dir) ^ v) & 3; /* alter orientation */
275 grid[n/2*(w+1)] = v;
276 }
9038fd11 277}
278
a3631c72 279static int grid_complete(int *grid, int wh, int orientable)
9038fd11 280{
281 int ok = TRUE;
282 int i;
283 for (i = 1; i < wh; i++)
284 if (grid[i] < grid[i-1])
285 ok = FALSE;
a3631c72 286 if (orientable) {
287 for (i = 0; i < wh; i++)
288 if (grid[i] & 3)
289 ok = FALSE;
290 }
9038fd11 291 return ok;
292}
293
294static char *new_game_seed(game_params *params, random_state *rs)
295{
296 int *grid;
297 int w = params->w, h = params->h, n = params->n, wh = w*h;
298 int i;
299 char *ret;
300 int retlen;
301 int total_moves;
302
303 /*
304 * Set up a solved grid.
305 */
306 grid = snewn(wh, int);
307 for (i = 0; i < wh; i++)
a3631c72 308 grid[i] = ((params->rowsonly ? i/w : i) + 1) * 4;
9038fd11 309
310 /*
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.
316 */
317 total_moves = w*h*n*n*2;
318 for (i = 0; i < total_moves; i++) {
319 int x, y;
320
321 x = random_upto(rs, w - n + 1);
322 y = random_upto(rs, h - n + 1);
a3631c72 323 do_rotate(grid, w, h, n, params->orientable,
324 x, y, 1 + random_upto(rs, 3));
9038fd11 325
326 /*
327 * Optionally one more move in case the entire grid has
328 * happened to come out solved.
329 */
a3631c72 330 if (i == total_moves - 1 && grid_complete(grid, wh,
331 params->orientable))
9038fd11 332 i--;
333 }
334
335 /*
336 * Now construct the game seed, by describing the grid as a
30861651 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'.
9038fd11 340 */
341 ret = NULL;
342 retlen = 0;
343 for (i = 0; i < wh; i++) {
344 char buf[80];
345 int k;
346
30861651 347 k = sprintf(buf, "%d%c", grid[i] / 4,
348 params->orientable ? "uldr"[grid[i] & 3] : ',');
9038fd11 349
350 ret = sresize(ret, retlen + k + 1, char);
351 strcpy(ret + retlen, buf);
352 retlen += k;
353 }
30861651 354 if (!params->orientable)
355 ret[retlen-1] = '\0'; /* delete last comma */
9038fd11 356
357 sfree(grid);
358 return ret;
359}
360
361static char *validate_seed(game_params *params, char *seed)
362{
363 char *p, *err;
364 int w = params->w, h = params->h, wh = w*h;
365 int i;
366
367 p = seed;
368 err = NULL;
369
370 for (i = 0; i < wh; i++) {
30861651 371 if (*p < '0' || *p > '9')
9038fd11 372 return "Not enough numbers in string";
9038fd11 373 while (*p >= '0' && *p <= '9')
374 p++;
30861651 375 if (!params->orientable && i < wh-1) {
376 if (*p != ',')
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) {
9038fd11 382 return "Excess junk at end of string";
383 }
384
385 if (*p) p++; /* eat comma */
386 }
387
388 return NULL;
389}
390
391static game_state *new_game(game_params *params, char *seed)
392{
393 game_state *state = snew(game_state);
394 int w = params->w, h = params->h, n = params->n, wh = w*h;
395 int i;
396 char *p;
397
398 state->w = w;
399 state->h = h;
400 state->n = n;
a3631c72 401 state->orientable = params->orientable;
9038fd11 402 state->completed = 0;
403 state->movecount = 0;
404 state->lastx = state->lasty = state->lastr = -1;
405
406 state->grid = snewn(wh, int);
407
408 p = seed;
409
410 for (i = 0; i < wh; i++) {
30861651 411 state->grid[i] = 4 * atoi(p);
9038fd11 412 while (*p >= '0' && *p <= '9')
413 p++;
30861651 414 if (*p) {
415 if (params->orientable) {
416 switch (*p) {
417 case 'l': state->grid[i] |= 1; break;
418 case 'd': state->grid[i] |= 2; break;
419 case 'r': state->grid[i] |= 3; break;
420 }
421 }
422 p++;
423 }
9038fd11 424 }
425
426 return state;
427}
428
429static game_state *dup_game(game_state *state)
430{
431 game_state *ret = snew(game_state);
432
433 ret->w = state->w;
434 ret->h = state->h;
435 ret->n = state->n;
a3631c72 436 ret->orientable = state->orientable;
9038fd11 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;
442
443 ret->grid = snewn(ret->w * ret->h, int);
444 memcpy(ret->grid, state->grid, ret->w * ret->h * sizeof(int));
445
446 return ret;
447}
448
449static void free_game(game_state *state)
450{
451 sfree(state->grid);
452 sfree(state);
453}
454
9b4b03d3 455static char *game_text_format(game_state *state)
456{
457 return NULL;
458}
459
9038fd11 460static game_ui *new_ui(game_state *state)
461{
462 return NULL;
463}
464
465static void free_ui(game_ui *ui)
466{
467}
468
469static game_state *make_move(game_state *from, game_ui *ui, int x, int y,
470 int button)
471{
472 int w = from->w, h = from->h, n = from->n, wh = w*h;
473 game_state *ret;
474 int dir;
475
476 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
477 /*
478 * Determine the coordinates of the click. We offset by n-1
479 * half-blocks so that the user must click at the centre of
480 * a rotation region rather than at the corner.
481 */
482 x -= (n-1) * TILE_SIZE / 2;
483 y -= (n-1) * TILE_SIZE / 2;
484 x = FROMCOORD(x);
485 y = FROMCOORD(y);
486 if (x < 0 || x > w-n || y < 0 || y > w-n)
487 return NULL;
488
489 /*
490 * This is a valid move. Make it.
491 */
492 ret = dup_game(from);
493 ret->movecount++;
494 dir = (button == LEFT_BUTTON ? 1 : -1);
a3631c72 495 do_rotate(ret->grid, w, h, n, ret->orientable, x, y, dir);
9038fd11 496 ret->lastx = x;
497 ret->lasty = y;
498 ret->lastr = dir;
499
500 /*
501 * See if the game has been completed. To do this we simply
502 * test that the grid contents are in increasing order.
503 */
a3631c72 504 if (!ret->completed && grid_complete(ret->grid, wh, ret->orientable))
9038fd11 505 ret->completed = ret->movecount;
506 return ret;
507 }
508 return NULL;
509}
510
511/* ----------------------------------------------------------------------
512 * Drawing routines.
513 */
514
515struct game_drawstate {
516 int started;
517 int w, h, bgcolour;
518 int *grid;
519};
520
521static void game_size(game_params *params, int *x, int *y)
522{
523 *x = TILE_SIZE * params->w + 2 * BORDER;
524 *y = TILE_SIZE * params->h + 2 * BORDER;
525}
526
527static float *game_colours(frontend *fe, game_state *state, int *ncolours)
528{
529 float *ret = snewn(3 * NCOLOURS, float);
530 int i;
531 float max;
532
533 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
534
535 /*
536 * Drop the background colour so that the highlight is
537 * noticeably brighter than it while still being under 1.
538 */
539 max = ret[COL_BACKGROUND*3];
540 for (i = 1; i < 3; i++)
541 if (ret[COL_BACKGROUND*3+i] > max)
542 max = ret[COL_BACKGROUND*3+i];
543 if (max * 1.2F > 1.0F) {
544 for (i = 0; i < 3; i++)
545 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
546 }
547
548 for (i = 0; i < 3; i++) {
549 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
550 ret[COL_HIGHLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.1F;
551 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
552 ret[COL_LOWLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
553 ret[COL_TEXT * 3 + i] = 0.0;
554 }
555
556 *ncolours = NCOLOURS;
557 return ret;
558}
559
560static game_drawstate *game_new_drawstate(game_state *state)
561{
562 struct game_drawstate *ds = snew(struct game_drawstate);
563 int i;
564
565 ds->started = FALSE;
566 ds->w = state->w;
567 ds->h = state->h;
568 ds->bgcolour = COL_BACKGROUND;
569 ds->grid = snewn(ds->w*ds->h, int);
570 for (i = 0; i < ds->w*ds->h; i++)
571 ds->grid[i] = -1;
572
573 return ds;
574}
575
576static void game_free_drawstate(game_drawstate *ds)
577{
578 sfree(ds);
579}
580
581struct rotation {
582 int cx, cy, cw, ch; /* clip region */
583 int ox, oy; /* rotation origin */
584 float c, s; /* cos and sin of rotation angle */
585 int lc, rc, tc, bc; /* colours of tile edges */
586};
587
588static void rotate(int *xy, struct rotation *rot)
589{
590 if (rot) {
591 float xf = xy[0] - rot->ox, yf = xy[1] - rot->oy;
592 float xf2, yf2;
593
594 xf2 = rot->c * xf + rot->s * yf;
595 yf2 = - rot->s * xf + rot->c * yf;
596
597 xy[0] = xf2 + rot->ox + 0.5; /* round to nearest */
598 xy[1] = yf2 + rot->oy + 0.5; /* round to nearest */
599 }
600}
601
602static void draw_tile(frontend *fe, game_state *state, int x, int y,
603 int tile, int flash_colour, struct rotation *rot)
604{
605 int coords[8];
606 char str[40];
607
608 if (rot)
609 clip(fe, rot->cx, rot->cy, rot->cw, rot->ch);
610
611 /*
612 * We must draw each side of the tile's highlight separately,
613 * because in some cases (during rotation) they will all need
614 * to be different colours.
615 */
616
617 /* The centre point is common to all sides. */
618 coords[4] = x + TILE_SIZE / 2;
619 coords[5] = y + TILE_SIZE / 2;
620 rotate(coords+4, rot);
621
622 /* Right side. */
623 coords[0] = x + TILE_SIZE - 1;
624 coords[1] = y + TILE_SIZE - 1;
625 rotate(coords+0, rot);
626 coords[2] = x + TILE_SIZE - 1;
627 coords[3] = y;
628 rotate(coords+2, rot);
629 draw_polygon(fe, coords, 3, TRUE, rot ? rot->rc : COL_LOWLIGHT);
630 draw_polygon(fe, coords, 3, FALSE, rot ? rot->rc : COL_LOWLIGHT);
631
632 /* Bottom side. */
633 coords[2] = x;
634 coords[3] = y + TILE_SIZE - 1;
635 rotate(coords+2, rot);
636 draw_polygon(fe, coords, 3, TRUE, rot ? rot->bc : COL_LOWLIGHT);
637 draw_polygon(fe, coords, 3, FALSE, rot ? rot->bc : COL_LOWLIGHT);
638
639 /* Left side. */
640 coords[0] = x;
641 coords[1] = y;
642 rotate(coords+0, rot);
643 draw_polygon(fe, coords, 3, TRUE, rot ? rot->lc : COL_HIGHLIGHT);
644 draw_polygon(fe, coords, 3, FALSE, rot ? rot->lc : COL_HIGHLIGHT);
645
646 /* Top side. */
647 coords[2] = x + TILE_SIZE - 1;
648 coords[3] = y;
649 rotate(coords+2, rot);
650 draw_polygon(fe, coords, 3, TRUE, rot ? rot->tc : COL_HIGHLIGHT);
651 draw_polygon(fe, coords, 3, FALSE, rot ? rot->tc : COL_HIGHLIGHT);
652
a3631c72 653 /*
654 * Now the main blank area in the centre of the tile.
655 */
9038fd11 656 if (rot) {
657 coords[0] = x + HIGHLIGHT_WIDTH;
658 coords[1] = y + HIGHLIGHT_WIDTH;
659 rotate(coords+0, rot);
660 coords[2] = x + HIGHLIGHT_WIDTH;
661 coords[3] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
662 rotate(coords+2, rot);
663 coords[4] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
664 coords[5] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
665 rotate(coords+4, rot);
666 coords[6] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
667 coords[7] = y + HIGHLIGHT_WIDTH;
668 rotate(coords+6, rot);
669 draw_polygon(fe, coords, 4, TRUE, flash_colour);
670 draw_polygon(fe, coords, 4, FALSE, flash_colour);
671 } else {
672 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
673 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
674 flash_colour);
675 }
676
a3631c72 677 /*
678 * Next, the colour bars for orientation.
679 */
680 if (state->orientable) {
d50832a3 681 int xdx, xdy, ydx, ydy;
682 int cx, cy, displ, displ2;
a3631c72 683 switch (tile & 3) {
684 case 0:
d50832a3 685 xdx = 1, xdy = 0;
686 ydx = 0, ydy = 1;
a3631c72 687 break;
688 case 1:
d50832a3 689 xdx = 0, xdy = -1;
690 ydx = 1, ydy = 0;
a3631c72 691 break;
692 case 2:
d50832a3 693 xdx = -1, xdy = 0;
694 ydx = 0, ydy = -1;
a3631c72 695 break;
696 default /* case 3 */:
d50832a3 697 xdx = 0, xdy = 1;
698 ydx = -1, ydy = 0;
a3631c72 699 break;
700 }
701
d50832a3 702 cx = x + TILE_SIZE / 2;
703 cy = y + TILE_SIZE / 2;
704 displ = TILE_SIZE / 2 - HIGHLIGHT_WIDTH - 2;
705 displ2 = TILE_SIZE / 3 - HIGHLIGHT_WIDTH;
a3631c72 706
30861651 707 coords[0] = cx - displ * xdx + displ2 * ydx;
708 coords[1] = cy - displ * xdy + displ2 * ydy;
a3631c72 709 rotate(coords+0, rot);
30861651 710 coords[2] = cx + displ * xdx + displ2 * ydx;
711 coords[3] = cy + displ * xdy + displ2 * ydy;
a3631c72 712 rotate(coords+2, rot);
30861651 713 coords[4] = cx - displ * ydx;
714 coords[5] = cy - displ * ydy;
a3631c72 715 rotate(coords+4, rot);
d50832a3 716 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT_GENTLE);
717 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT_GENTLE);
a3631c72 718 }
719
9038fd11 720 coords[0] = x + TILE_SIZE/2;
721 coords[1] = y + TILE_SIZE/2;
722 rotate(coords+0, rot);
a3631c72 723 sprintf(str, "%d", tile / 4);
9038fd11 724 draw_text(fe, coords[0], coords[1],
725 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
726 COL_TEXT, str);
727
728 if (rot)
729 unclip(fe);
730
731 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
732}
733
734static int highlight_colour(float angle)
735{
736 int colours[32] = {
737 COL_LOWLIGHT,
738 COL_LOWLIGHT_GENTLE,
739 COL_LOWLIGHT_GENTLE,
740 COL_LOWLIGHT_GENTLE,
741 COL_HIGHLIGHT_GENTLE,
742 COL_HIGHLIGHT_GENTLE,
743 COL_HIGHLIGHT_GENTLE,
744 COL_HIGHLIGHT,
745 COL_HIGHLIGHT,
746 COL_HIGHLIGHT,
747 COL_HIGHLIGHT,
748 COL_HIGHLIGHT,
749 COL_HIGHLIGHT,
750 COL_HIGHLIGHT,
751 COL_HIGHLIGHT,
752 COL_HIGHLIGHT,
753 COL_HIGHLIGHT,
754 COL_HIGHLIGHT_GENTLE,
755 COL_HIGHLIGHT_GENTLE,
756 COL_HIGHLIGHT_GENTLE,
757 COL_LOWLIGHT_GENTLE,
758 COL_LOWLIGHT_GENTLE,
759 COL_LOWLIGHT_GENTLE,
760 COL_LOWLIGHT,
761 COL_LOWLIGHT,
762 COL_LOWLIGHT,
763 COL_LOWLIGHT,
764 COL_LOWLIGHT,
765 COL_LOWLIGHT,
766 COL_LOWLIGHT,
767 COL_LOWLIGHT,
768 COL_LOWLIGHT,
769 };
770
771 return colours[(int)((angle + 2*PI) / (PI/16)) & 31];
772}
773
774static float game_anim_length(game_state *oldstate, game_state *newstate,
775 int dir)
776{
777 return ANIM_PER_RADIUS_UNIT * sqrt(newstate->n-1);
778}
779
780static float game_flash_length(game_state *oldstate, game_state *newstate,
781 int dir)
782{
783 if (!oldstate->completed && newstate->completed)
784 return 2 * FLASH_FRAME;
785 else
786 return 0.0F;
787}
788
789static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
790 game_state *state, int dir, game_ui *ui,
791 float animtime, float flashtime)
792{
793 int i, bgcolour;
794 struct rotation srot, *rot;
795 int lastx = -1, lasty = -1, lastr = -1;
796
797 if (flashtime > 0) {
798 int frame = (int)(flashtime / FLASH_FRAME);
799 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
800 } else
801 bgcolour = COL_BACKGROUND;
802
803 if (!ds->started) {
804 int coords[6];
805
806 draw_rect(fe, 0, 0,
807 TILE_SIZE * state->w + 2 * BORDER,
808 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
809 draw_update(fe, 0, 0,
810 TILE_SIZE * state->w + 2 * BORDER,
811 TILE_SIZE * state->h + 2 * BORDER);
812
813 /*
814 * Recessed area containing the whole puzzle.
815 */
816 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
817 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
818 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
819 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
820 coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
821 coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
822 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
823 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
824
825 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
826 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
827 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
828 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
829
830 ds->started = TRUE;
831 }
832
833 /*
834 * If we're drawing any rotated tiles, sort out the rotation
835 * parameters, and also zap the rotation region to the
836 * background colour before doing anything else.
837 */
838 if (oldstate) {
839 float angle;
840 float anim_max = game_anim_length(oldstate, state, dir);
841
842 if (dir > 0) {
843 lastx = state->lastx;
844 lasty = state->lasty;
845 lastr = state->lastr;
846 } else {
847 lastx = oldstate->lastx;
848 lasty = oldstate->lasty;
849 lastr = -oldstate->lastr;
850 }
851
852 rot = &srot;
853 rot->cx = COORD(lastx);
854 rot->cy = COORD(lasty);
855 rot->cw = rot->ch = TILE_SIZE * state->n;
856 rot->ox = rot->cx + rot->cw/2;
857 rot->oy = rot->cy + rot->ch/2;
858 angle = (-PI/2 * lastr) * (1.0 - animtime / anim_max);
859 rot->c = cos(angle);
860 rot->s = sin(angle);
861
862 /*
863 * Sort out the colours of the various sides of the tile.
864 */
865 rot->lc = highlight_colour(PI + angle);
866 rot->rc = highlight_colour(angle);
867 rot->tc = highlight_colour(PI/2 + angle);
868 rot->bc = highlight_colour(-PI/2 + angle);
869
870 draw_rect(fe, rot->cx, rot->cy, rot->cw, rot->ch, bgcolour);
871 } else
872 rot = NULL;
873
874 /*
875 * Now draw each tile.
876 */
877 for (i = 0; i < state->w * state->h; i++) {
878 int t;
879 int tx = i % state->w, ty = i / state->w;
880
881 /*
882 * Figure out what should be displayed at this location.
883 * Usually it will be state->grid[i], unless we're in the
884 * middle of animating an actual rotation and this cell is
885 * within the rotation region, in which case we set -1
886 * (always display).
887 */
888 if (oldstate && lastx >= 0 && lasty >= 0 &&
889 tx >= lastx && tx < lastx + state->n &&
890 ty >= lasty && ty < lasty + state->n)
891 t = -1;
892 else
893 t = state->grid[i];
894
895 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
896 ds->grid[i] != t || ds->grid[i] == -1 || t == -1) {
897 int x = COORD(tx), y = COORD(ty);
898
899 draw_tile(fe, state, x, y, state->grid[i], bgcolour, rot);
900 ds->grid[i] = t;
901 }
902 }
903 ds->bgcolour = bgcolour;
904
905 /*
906 * Update the status bar.
907 */
908 {
909 char statusbuf[256];
910
911 /*
912 * Don't show the new status until we're also showing the
913 * new _state_ - after the game animation is complete.
914 */
915 if (oldstate)
916 state = oldstate;
917
918 sprintf(statusbuf, "%sMoves: %d",
919 (state->completed ? "COMPLETED! " : ""),
920 (state->completed ? state->completed : state->movecount));
921
922 status_bar(fe, statusbuf);
923 }
924}
925
926static int game_wants_statusbar(void)
927{
928 return TRUE;
929}
930
931#ifdef COMBINED
932#define thegame twiddle
933#endif
934
935const struct game thegame = {
1d228b10 936 "Twiddle", "games.twiddle",
9038fd11 937 default_params,
938 game_fetch_preset,
939 decode_params,
940 encode_params,
941 free_params,
942 dup_params,
1d228b10 943 TRUE, game_configure, custom_params,
9038fd11 944 validate_params,
945 new_game_seed,
946 validate_seed,
947 new_game,
948 dup_game,
949 free_game,
9b4b03d3 950 FALSE, game_text_format,
9038fd11 951 new_ui,
952 free_ui,
953 make_move,
954 game_size,
955 game_colours,
956 game_new_drawstate,
957 game_free_drawstate,
958 game_redraw,
959 game_anim_length,
960 game_flash_length,
961 game_wants_statusbar,
962};