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