Just for Gareth: a means of overriding individual game colour
[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;
81875211 42 int movetarget;
9038fd11 43};
44
45struct game_state {
46 int w, h, n;
a3631c72 47 int orientable;
9038fd11 48 int *grid;
49 int completed;
2ac6d24e 50 int just_used_solve; /* used to suppress undo animation */
51 int used_solve; /* used to suppress completion flash */
81875211 52 int movecount, movetarget;
9038fd11 53 int lastx, lasty, lastr; /* coordinates of last rotation */
54};
55
56static game_params *default_params(void)
57{
58 game_params *ret = snew(game_params);
59
60 ret->w = ret->h = 3;
61 ret->n = 2;
a3631c72 62 ret->rowsonly = ret->orientable = FALSE;
81875211 63 ret->movetarget = 0;
9038fd11 64
65 return ret;
66}
67
68
69static void free_params(game_params *params)
70{
71 sfree(params);
72}
73
74static 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
81static int game_fetch_preset(int i, char **name, game_params **params)
82{
83 static struct {
84 char *title;
85 game_params params;
86 } presets[] = {
a3631c72 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 } },
9038fd11 90 { "4x4 normal", { 4, 4, 2, FALSE } },
a3631c72 91 { "4x4 orientable", { 4, 4, 2, FALSE, TRUE } },
9038fd11 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
1185e3c5 106static void decode_params(game_params *ret, char const *string)
9038fd11 107{
9038fd11 108 ret->w = ret->h = atoi(string);
109 ret->n = 2;
a3631c72 110 ret->rowsonly = ret->orientable = FALSE;
81875211 111 ret->movetarget = 0;
9038fd11 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 }
a3631c72 123 while (*string) {
124 if (*string == 'r') {
125 ret->rowsonly = TRUE;
126 } else if (*string == 'o') {
127 ret->orientable = TRUE;
81875211 128 } else if (*string == 'm') {
129 string++;
130 ret->movetarget = atoi(string);
131 while (string[1] && isdigit(string[1])) string++;
a3631c72 132 }
9038fd11 133 string++;
9038fd11 134 }
9038fd11 135}
136
1185e3c5 137static char *encode_params(game_params *params, int full)
9038fd11 138{
139 char buf[256];
a3631c72 140 sprintf(buf, "%dx%dn%d%s%s", params->w, params->h, params->n,
141 params->rowsonly ? "r" : "",
142 params->orientable ? "o" : "");
1185e3c5 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);
9038fd11 147 return dupstr(buf);
148}
149
150static config_item *game_configure(game_params *params)
151{
152 config_item *ret;
153 char buf[80];
154
81875211 155 ret = snewn(7, config_item);
9038fd11 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
a3631c72 180 ret[4].name = "Orientation matters";
181 ret[4].type = C_BOOLEAN;
9038fd11 182 ret[4].sval = NULL;
a3631c72 183 ret[4].ival = params->orientable;
184
81875211 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);
a3631c72 189 ret[5].ival = 0;
9038fd11 190
81875211 191 ret[6].name = NULL;
192 ret[6].type = C_END;
193 ret[6].sval = NULL;
194 ret[6].ival = 0;
195
9038fd11 196 return ret;
197}
198
199static 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;
a3631c72 207 ret->orientable = cfg[4].ival;
81875211 208 ret->movetarget = atoi(cfg[5].sval);
9038fd11 209
210 return ret;
211}
212
213static 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 */
a3631c72 232static void do_rotate(int *grid, int w, int h, int n, int orientable,
233 int x, int y, int dir)
9038fd11 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
a3631c72 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 }
9038fd11 281 }
282 }
a3631c72 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 }
9038fd11 293}
294
a3631c72 295static int grid_complete(int *grid, int wh, int orientable)
9038fd11 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;
a3631c72 302 if (orientable) {
303 for (i = 0; i < wh; i++)
304 if (grid[i] & 3)
305 ok = FALSE;
306 }
9038fd11 307 return ok;
308}
309
1185e3c5 310static char *new_game_desc(game_params *params, random_state *rs,
6f2d8d7c 311 game_aux_info **aux)
9038fd11 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++)
a3631c72 325 grid[i] = ((params->rowsonly ? i/w : i) + 1) * 4;
9038fd11 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 */
81875211 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));
9038fd11 369
370 /*
1185e3c5 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'.
9038fd11 375 */
376 ret = NULL;
377 retlen = 0;
378 for (i = 0; i < wh; i++) {
379 char buf[80];
380 int k;
381
30861651 382 k = sprintf(buf, "%d%c", grid[i] / 4,
383 params->orientable ? "uldr"[grid[i] & 3] : ',');
9038fd11 384
385 ret = sresize(ret, retlen + k + 1, char);
386 strcpy(ret + retlen, buf);
387 retlen += k;
388 }
30861651 389 if (!params->orientable)
390 ret[retlen-1] = '\0'; /* delete last comma */
9038fd11 391
392 sfree(grid);
393 return ret;
394}
395
2ac6d24e 396static void game_free_aux_info(game_aux_info *aux)
6f2d8d7c 397{
398 assert(!"Shouldn't happen");
399}
400
1185e3c5 401static char *validate_desc(game_params *params, char *desc)
9038fd11 402{
403 char *p, *err;
404 int w = params->w, h = params->h, wh = w*h;
405 int i;
406
1185e3c5 407 p = desc;
9038fd11 408 err = NULL;
409
410 for (i = 0; i < wh; i++) {
30861651 411 if (*p < '0' || *p > '9')
9038fd11 412 return "Not enough numbers in string";
9038fd11 413 while (*p >= '0' && *p <= '9')
414 p++;
30861651 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) {
9038fd11 422 return "Excess junk at end of string";
423 }
424
425 if (*p) p++; /* eat comma */
426 }
427
428 return NULL;
429}
430
1185e3c5 431static game_state *new_game(game_params *params, char *desc)
9038fd11 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;
a3631c72 441 state->orientable = params->orientable;
9038fd11 442 state->completed = 0;
2ac6d24e 443 state->used_solve = state->just_used_solve = FALSE;
9038fd11 444 state->movecount = 0;
81875211 445 state->movetarget = params->movetarget;
9038fd11 446 state->lastx = state->lasty = state->lastr = -1;
447
448 state->grid = snewn(wh, int);
449
1185e3c5 450 p = desc;
9038fd11 451
452 for (i = 0; i < wh; i++) {
30861651 453 state->grid[i] = 4 * atoi(p);
9038fd11 454 while (*p >= '0' && *p <= '9')
455 p++;
30861651 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 }
9038fd11 466 }
467
468 return state;
469}
470
471static 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;
a3631c72 478 ret->orientable = state->orientable;
9038fd11 479 ret->completed = state->completed;
480 ret->movecount = state->movecount;
81875211 481 ret->movetarget = state->movetarget;
9038fd11 482 ret->lastx = state->lastx;
483 ret->lasty = state->lasty;
484 ret->lastr = state->lastr;
2ac6d24e 485 ret->used_solve = state->used_solve;
486 ret->just_used_solve = state->just_used_solve;
9038fd11 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
494static void free_game(game_state *state)
495{
496 sfree(state->grid);
497 sfree(state);
498}
499
2ac6d24e 500static 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
512static game_state *solve_game(game_state *state, game_aux_info *aux,
513 char **error)
514{
515 game_state *ret = dup_game(state);
ef059e59 516 int i;
2ac6d24e 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);
ef059e59 526 for (i = 0; i < ret->w*ret->h; i++)
527 ret->grid[i] &= ~3;
2ac6d24e 528 ret->used_solve = ret->just_used_solve = TRUE;
e73ca44d 529 ret->completed = ret->movecount = 1;
2ac6d24e 530
531 return ret;
532}
533
9b4b03d3 534static char *game_text_format(game_state *state)
535{
af52394e 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
48a10826 558 ret = snewn(maxlen+1, char);
af52394e 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;
9b4b03d3 579}
580
9038fd11 581static game_ui *new_ui(game_state *state)
582{
583 return NULL;
584}
585
586static void free_ui(game_ui *ui)
587{
588}
589
590static 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 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
598 /*
599 * Determine the coordinates of the click. We offset by n-1
600 * half-blocks so that the user must click at the centre of
601 * a rotation region rather than at the corner.
602 */
603 x -= (n-1) * TILE_SIZE / 2;
604 y -= (n-1) * TILE_SIZE / 2;
605 x = FROMCOORD(x);
606 y = FROMCOORD(y);
607 if (x < 0 || x > w-n || y < 0 || y > w-n)
608 return NULL;
609
610 /*
611 * This is a valid move. Make it.
612 */
613 ret = dup_game(from);
2ac6d24e 614 ret->just_used_solve = FALSE; /* zero this in a hurry */
9038fd11 615 ret->movecount++;
616 dir = (button == LEFT_BUTTON ? 1 : -1);
a3631c72 617 do_rotate(ret->grid, w, h, n, ret->orientable, x, y, dir);
9038fd11 618 ret->lastx = x;
619 ret->lasty = y;
620 ret->lastr = dir;
621
622 /*
623 * See if the game has been completed. To do this we simply
624 * test that the grid contents are in increasing order.
625 */
a3631c72 626 if (!ret->completed && grid_complete(ret->grid, wh, ret->orientable))
9038fd11 627 ret->completed = ret->movecount;
628 return ret;
629 }
630 return NULL;
631}
632
633/* ----------------------------------------------------------------------
634 * Drawing routines.
635 */
636
637struct game_drawstate {
638 int started;
639 int w, h, bgcolour;
640 int *grid;
641};
642
643static void game_size(game_params *params, int *x, int *y)
644{
645 *x = TILE_SIZE * params->w + 2 * BORDER;
646 *y = TILE_SIZE * params->h + 2 * BORDER;
647}
648
649static float *game_colours(frontend *fe, game_state *state, int *ncolours)
650{
651 float *ret = snewn(3 * NCOLOURS, float);
652 int i;
653 float max;
654
655 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
656
657 /*
658 * Drop the background colour so that the highlight is
659 * noticeably brighter than it while still being under 1.
660 */
661 max = ret[COL_BACKGROUND*3];
662 for (i = 1; i < 3; i++)
663 if (ret[COL_BACKGROUND*3+i] > max)
664 max = ret[COL_BACKGROUND*3+i];
665 if (max * 1.2F > 1.0F) {
666 for (i = 0; i < 3; i++)
667 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
668 }
669
670 for (i = 0; i < 3; i++) {
671 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
672 ret[COL_HIGHLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.1F;
673 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
674 ret[COL_LOWLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
675 ret[COL_TEXT * 3 + i] = 0.0;
676 }
677
678 *ncolours = NCOLOURS;
679 return ret;
680}
681
682static game_drawstate *game_new_drawstate(game_state *state)
683{
684 struct game_drawstate *ds = snew(struct game_drawstate);
685 int i;
686
687 ds->started = FALSE;
688 ds->w = state->w;
689 ds->h = state->h;
690 ds->bgcolour = COL_BACKGROUND;
691 ds->grid = snewn(ds->w*ds->h, int);
692 for (i = 0; i < ds->w*ds->h; i++)
693 ds->grid[i] = -1;
694
695 return ds;
696}
697
698static void game_free_drawstate(game_drawstate *ds)
699{
700 sfree(ds);
701}
702
703struct rotation {
704 int cx, cy, cw, ch; /* clip region */
705 int ox, oy; /* rotation origin */
706 float c, s; /* cos and sin of rotation angle */
707 int lc, rc, tc, bc; /* colours of tile edges */
708};
709
710static void rotate(int *xy, struct rotation *rot)
711{
712 if (rot) {
713 float xf = xy[0] - rot->ox, yf = xy[1] - rot->oy;
714 float xf2, yf2;
715
716 xf2 = rot->c * xf + rot->s * yf;
717 yf2 = - rot->s * xf + rot->c * yf;
718
719 xy[0] = xf2 + rot->ox + 0.5; /* round to nearest */
720 xy[1] = yf2 + rot->oy + 0.5; /* round to nearest */
721 }
722}
723
724static void draw_tile(frontend *fe, game_state *state, int x, int y,
725 int tile, int flash_colour, struct rotation *rot)
726{
727 int coords[8];
728 char str[40];
729
0551ee6b 730 /*
731 * If we've been passed a rotation region but we're drawing a
732 * tile which is outside it, we must draw it normally. This can
733 * occur if we're cleaning up after a completion flash while a
734 * new move is also being made.
735 */
736 if (rot && (x < rot->cx || y < rot->cy ||
daaccee8 737 x >= rot->cx+rot->cw || y >= rot->cy+rot->ch))
0551ee6b 738 rot = NULL;
739
9038fd11 740 if (rot)
741 clip(fe, rot->cx, rot->cy, rot->cw, rot->ch);
742
743 /*
744 * We must draw each side of the tile's highlight separately,
745 * because in some cases (during rotation) they will all need
746 * to be different colours.
747 */
748
749 /* The centre point is common to all sides. */
750 coords[4] = x + TILE_SIZE / 2;
751 coords[5] = y + TILE_SIZE / 2;
752 rotate(coords+4, rot);
753
754 /* Right side. */
755 coords[0] = x + TILE_SIZE - 1;
756 coords[1] = y + TILE_SIZE - 1;
757 rotate(coords+0, rot);
758 coords[2] = x + TILE_SIZE - 1;
759 coords[3] = y;
760 rotate(coords+2, rot);
761 draw_polygon(fe, coords, 3, TRUE, rot ? rot->rc : COL_LOWLIGHT);
762 draw_polygon(fe, coords, 3, FALSE, rot ? rot->rc : COL_LOWLIGHT);
763
764 /* Bottom side. */
765 coords[2] = x;
766 coords[3] = y + TILE_SIZE - 1;
767 rotate(coords+2, rot);
768 draw_polygon(fe, coords, 3, TRUE, rot ? rot->bc : COL_LOWLIGHT);
769 draw_polygon(fe, coords, 3, FALSE, rot ? rot->bc : COL_LOWLIGHT);
770
771 /* Left side. */
772 coords[0] = x;
773 coords[1] = y;
774 rotate(coords+0, rot);
775 draw_polygon(fe, coords, 3, TRUE, rot ? rot->lc : COL_HIGHLIGHT);
776 draw_polygon(fe, coords, 3, FALSE, rot ? rot->lc : COL_HIGHLIGHT);
777
778 /* Top side. */
779 coords[2] = x + TILE_SIZE - 1;
780 coords[3] = y;
781 rotate(coords+2, rot);
782 draw_polygon(fe, coords, 3, TRUE, rot ? rot->tc : COL_HIGHLIGHT);
783 draw_polygon(fe, coords, 3, FALSE, rot ? rot->tc : COL_HIGHLIGHT);
784
a3631c72 785 /*
786 * Now the main blank area in the centre of the tile.
787 */
9038fd11 788 if (rot) {
789 coords[0] = x + HIGHLIGHT_WIDTH;
790 coords[1] = y + HIGHLIGHT_WIDTH;
791 rotate(coords+0, rot);
792 coords[2] = x + HIGHLIGHT_WIDTH;
793 coords[3] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
794 rotate(coords+2, rot);
795 coords[4] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
796 coords[5] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
797 rotate(coords+4, rot);
798 coords[6] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
799 coords[7] = y + HIGHLIGHT_WIDTH;
800 rotate(coords+6, rot);
801 draw_polygon(fe, coords, 4, TRUE, flash_colour);
802 draw_polygon(fe, coords, 4, FALSE, flash_colour);
803 } else {
804 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
805 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
806 flash_colour);
807 }
808
a3631c72 809 /*
4b24f582 810 * Next, the triangles for orientation.
a3631c72 811 */
812 if (state->orientable) {
d50832a3 813 int xdx, xdy, ydx, ydy;
814 int cx, cy, displ, displ2;
a3631c72 815 switch (tile & 3) {
816 case 0:
d50832a3 817 xdx = 1, xdy = 0;
818 ydx = 0, ydy = 1;
a3631c72 819 break;
820 case 1:
d50832a3 821 xdx = 0, xdy = -1;
822 ydx = 1, ydy = 0;
a3631c72 823 break;
824 case 2:
d50832a3 825 xdx = -1, xdy = 0;
826 ydx = 0, ydy = -1;
a3631c72 827 break;
828 default /* case 3 */:
d50832a3 829 xdx = 0, xdy = 1;
830 ydx = -1, ydy = 0;
a3631c72 831 break;
832 }
833
d50832a3 834 cx = x + TILE_SIZE / 2;
835 cy = y + TILE_SIZE / 2;
836 displ = TILE_SIZE / 2 - HIGHLIGHT_WIDTH - 2;
837 displ2 = TILE_SIZE / 3 - HIGHLIGHT_WIDTH;
a3631c72 838
30861651 839 coords[0] = cx - displ * xdx + displ2 * ydx;
840 coords[1] = cy - displ * xdy + displ2 * ydy;
a3631c72 841 rotate(coords+0, rot);
30861651 842 coords[2] = cx + displ * xdx + displ2 * ydx;
843 coords[3] = cy + displ * xdy + displ2 * ydy;
a3631c72 844 rotate(coords+2, rot);
30861651 845 coords[4] = cx - displ * ydx;
846 coords[5] = cy - displ * ydy;
a3631c72 847 rotate(coords+4, rot);
d50832a3 848 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT_GENTLE);
849 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT_GENTLE);
a3631c72 850 }
851
9038fd11 852 coords[0] = x + TILE_SIZE/2;
853 coords[1] = y + TILE_SIZE/2;
854 rotate(coords+0, rot);
a3631c72 855 sprintf(str, "%d", tile / 4);
9038fd11 856 draw_text(fe, coords[0], coords[1],
857 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
858 COL_TEXT, str);
859
860 if (rot)
861 unclip(fe);
862
863 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
864}
865
866static int highlight_colour(float angle)
867{
868 int colours[32] = {
869 COL_LOWLIGHT,
870 COL_LOWLIGHT_GENTLE,
871 COL_LOWLIGHT_GENTLE,
872 COL_LOWLIGHT_GENTLE,
873 COL_HIGHLIGHT_GENTLE,
874 COL_HIGHLIGHT_GENTLE,
875 COL_HIGHLIGHT_GENTLE,
876 COL_HIGHLIGHT,
877 COL_HIGHLIGHT,
878 COL_HIGHLIGHT,
879 COL_HIGHLIGHT,
880 COL_HIGHLIGHT,
881 COL_HIGHLIGHT,
882 COL_HIGHLIGHT,
883 COL_HIGHLIGHT,
884 COL_HIGHLIGHT,
885 COL_HIGHLIGHT,
886 COL_HIGHLIGHT_GENTLE,
887 COL_HIGHLIGHT_GENTLE,
888 COL_HIGHLIGHT_GENTLE,
889 COL_LOWLIGHT_GENTLE,
890 COL_LOWLIGHT_GENTLE,
891 COL_LOWLIGHT_GENTLE,
892 COL_LOWLIGHT,
893 COL_LOWLIGHT,
894 COL_LOWLIGHT,
895 COL_LOWLIGHT,
896 COL_LOWLIGHT,
897 COL_LOWLIGHT,
898 COL_LOWLIGHT,
899 COL_LOWLIGHT,
900 COL_LOWLIGHT,
901 };
902
903 return colours[(int)((angle + 2*PI) / (PI/16)) & 31];
904}
905
906static float game_anim_length(game_state *oldstate, game_state *newstate,
907 int dir)
908{
2ac6d24e 909 if ((dir > 0 && newstate->just_used_solve) ||
910 (dir < 0 && oldstate->just_used_solve))
911 return 0.0F;
912 else
913 return ANIM_PER_RADIUS_UNIT * sqrt(newstate->n-1);
9038fd11 914}
915
916static float game_flash_length(game_state *oldstate, game_state *newstate,
917 int dir)
918{
2ac6d24e 919 if (!oldstate->completed && newstate->completed &&
920 !oldstate->used_solve && !newstate->used_solve)
9038fd11 921 return 2 * FLASH_FRAME;
922 else
923 return 0.0F;
924}
925
926static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
927 game_state *state, int dir, game_ui *ui,
928 float animtime, float flashtime)
929{
930 int i, bgcolour;
931 struct rotation srot, *rot;
932 int lastx = -1, lasty = -1, lastr = -1;
933
934 if (flashtime > 0) {
935 int frame = (int)(flashtime / FLASH_FRAME);
936 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
937 } else
938 bgcolour = COL_BACKGROUND;
939
940 if (!ds->started) {
941 int coords[6];
942
943 draw_rect(fe, 0, 0,
944 TILE_SIZE * state->w + 2 * BORDER,
945 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
946 draw_update(fe, 0, 0,
947 TILE_SIZE * state->w + 2 * BORDER,
948 TILE_SIZE * state->h + 2 * BORDER);
949
950 /*
951 * Recessed area containing the whole puzzle.
952 */
953 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
954 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
955 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
956 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
957 coords[4] = COORD(0) - HIGHLIGHT_WIDTH;
958 coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
959 draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT);
960 draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT);
961
962 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
963 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
964 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT);
965 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT);
966
967 ds->started = TRUE;
968 }
969
970 /*
971 * If we're drawing any rotated tiles, sort out the rotation
972 * parameters, and also zap the rotation region to the
973 * background colour before doing anything else.
974 */
975 if (oldstate) {
976 float angle;
977 float anim_max = game_anim_length(oldstate, state, dir);
978
979 if (dir > 0) {
980 lastx = state->lastx;
981 lasty = state->lasty;
982 lastr = state->lastr;
983 } else {
984 lastx = oldstate->lastx;
985 lasty = oldstate->lasty;
986 lastr = -oldstate->lastr;
987 }
988
989 rot = &srot;
990 rot->cx = COORD(lastx);
991 rot->cy = COORD(lasty);
992 rot->cw = rot->ch = TILE_SIZE * state->n;
993 rot->ox = rot->cx + rot->cw/2;
994 rot->oy = rot->cy + rot->ch/2;
995 angle = (-PI/2 * lastr) * (1.0 - animtime / anim_max);
996 rot->c = cos(angle);
997 rot->s = sin(angle);
998
999 /*
1000 * Sort out the colours of the various sides of the tile.
1001 */
1002 rot->lc = highlight_colour(PI + angle);
1003 rot->rc = highlight_colour(angle);
1004 rot->tc = highlight_colour(PI/2 + angle);
1005 rot->bc = highlight_colour(-PI/2 + angle);
1006
1007 draw_rect(fe, rot->cx, rot->cy, rot->cw, rot->ch, bgcolour);
1008 } else
1009 rot = NULL;
1010
1011 /*
1012 * Now draw each tile.
1013 */
1014 for (i = 0; i < state->w * state->h; i++) {
1015 int t;
1016 int tx = i % state->w, ty = i / state->w;
1017
1018 /*
1019 * Figure out what should be displayed at this location.
1020 * Usually it will be state->grid[i], unless we're in the
1021 * middle of animating an actual rotation and this cell is
1022 * within the rotation region, in which case we set -1
1023 * (always display).
1024 */
1025 if (oldstate && lastx >= 0 && lasty >= 0 &&
1026 tx >= lastx && tx < lastx + state->n &&
1027 ty >= lasty && ty < lasty + state->n)
1028 t = -1;
1029 else
1030 t = state->grid[i];
1031
1032 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
1033 ds->grid[i] != t || ds->grid[i] == -1 || t == -1) {
1034 int x = COORD(tx), y = COORD(ty);
1035
1036 draw_tile(fe, state, x, y, state->grid[i], bgcolour, rot);
1037 ds->grid[i] = t;
1038 }
1039 }
1040 ds->bgcolour = bgcolour;
1041
1042 /*
1043 * Update the status bar.
1044 */
1045 {
1046 char statusbuf[256];
1047
1048 /*
1049 * Don't show the new status until we're also showing the
1050 * new _state_ - after the game animation is complete.
1051 */
1052 if (oldstate)
1053 state = oldstate;
1054
2ac6d24e 1055 if (state->used_solve)
1056 sprintf(statusbuf, "Moves since auto-solve: %d",
1057 state->movecount - state->completed);
81875211 1058 else {
2ac6d24e 1059 sprintf(statusbuf, "%sMoves: %d",
1060 (state->completed ? "COMPLETED! " : ""),
1061 (state->completed ? state->completed : state->movecount));
81875211 1062 if (state->movetarget)
1063 sprintf(statusbuf+strlen(statusbuf), " (target %d)",
1064 state->movetarget);
1065 }
9038fd11 1066
1067 status_bar(fe, statusbuf);
1068 }
1069}
1070
1071static int game_wants_statusbar(void)
1072{
1073 return TRUE;
1074}
1075
1076#ifdef COMBINED
1077#define thegame twiddle
1078#endif
1079
1080const struct game thegame = {
1d228b10 1081 "Twiddle", "games.twiddle",
9038fd11 1082 default_params,
1083 game_fetch_preset,
1084 decode_params,
1085 encode_params,
1086 free_params,
1087 dup_params,
1d228b10 1088 TRUE, game_configure, custom_params,
9038fd11 1089 validate_params,
1185e3c5 1090 new_game_desc,
6f2d8d7c 1091 game_free_aux_info,
1185e3c5 1092 validate_desc,
9038fd11 1093 new_game,
1094 dup_game,
1095 free_game,
2ac6d24e 1096 TRUE, solve_game,
af52394e 1097 TRUE, game_text_format,
9038fd11 1098 new_ui,
1099 free_ui,
1100 make_move,
1101 game_size,
1102 game_colours,
1103 game_new_drawstate,
1104 game_free_drawstate,
1105 game_redraw,
1106 game_anim_length,
1107 game_flash_length,
1108 game_wants_statusbar,
1109};