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