Fix various departures from C found by `gcc -ansi -pedantic'. I
[sgt/puzzles] / twiddle.c
1 /*
2 * twiddle.c: Puzzle involving rearranging a grid of squares by
3 * rotating subsquares. Adapted and generalised from a
4 * door-unlocking puzzle in Metroid Prime 2 (the one in the Main
5 * Gyro Chamber).
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <ctype.h>
13 #include <math.h>
14
15 #include "puzzles.h"
16
17 #define TILE_SIZE 48
18 #define BORDER (TILE_SIZE / 2)
19 #define HIGHLIGHT_WIDTH (TILE_SIZE / 20)
20 #define COORD(x) ( (x) * TILE_SIZE + BORDER )
21 #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
22
23 #define ANIM_PER_RADIUS_UNIT 0.13F
24 #define FLASH_FRAME 0.13F
25
26 enum {
27 COL_BACKGROUND,
28 COL_TEXT,
29 COL_HIGHLIGHT,
30 COL_HIGHLIGHT_GENTLE,
31 COL_LOWLIGHT,
32 COL_LOWLIGHT_GENTLE,
33 NCOLOURS
34 };
35
36 struct game_params {
37 int w, h, n;
38 int rowsonly;
39 int orientable;
40 int movetarget;
41 };
42
43 struct game_state {
44 int w, h, n;
45 int orientable;
46 int *grid;
47 int completed;
48 int just_used_solve; /* used to suppress undo animation */
49 int used_solve; /* used to suppress completion flash */
50 int movecount, movetarget;
51 int lastx, lasty, lastr; /* coordinates of last rotation */
52 };
53
54 static game_params *default_params(void)
55 {
56 game_params *ret = snew(game_params);
57
58 ret->w = ret->h = 3;
59 ret->n = 2;
60 ret->rowsonly = ret->orientable = FALSE;
61 ret->movetarget = 0;
62
63 return ret;
64 }
65
66
67 static void free_params(game_params *params)
68 {
69 sfree(params);
70 }
71
72 static game_params *dup_params(game_params *params)
73 {
74 game_params *ret = snew(game_params);
75 *ret = *params; /* structure copy */
76 return ret;
77 }
78
79 static int game_fetch_preset(int i, char **name, game_params **params)
80 {
81 static struct {
82 char *title;
83 game_params params;
84 } presets[] = {
85 { "3x3 rows only", { 3, 3, 2, TRUE, FALSE } },
86 { "3x3 normal", { 3, 3, 2, FALSE, FALSE } },
87 { "3x3 orientable", { 3, 3, 2, FALSE, TRUE } },
88 { "4x4 normal", { 4, 4, 2, FALSE } },
89 { "4x4 orientable", { 4, 4, 2, FALSE, TRUE } },
90 { "4x4 radius 3", { 4, 4, 3, FALSE } },
91 { "5x5 radius 3", { 5, 5, 3, FALSE } },
92 { "6x6 radius 4", { 6, 6, 4, FALSE } },
93 };
94
95 if (i < 0 || i >= lenof(presets))
96 return FALSE;
97
98 *name = dupstr(presets[i].title);
99 *params = dup_params(&presets[i].params);
100
101 return TRUE;
102 }
103
104 static void decode_params(game_params *ret, char const *string)
105 {
106 ret->w = ret->h = atoi(string);
107 ret->n = 2;
108 ret->rowsonly = ret->orientable = FALSE;
109 ret->movetarget = 0;
110 while (*string && isdigit(*string)) string++;
111 if (*string == 'x') {
112 string++;
113 ret->h = atoi(string);
114 while (*string && isdigit(*string)) string++;
115 }
116 if (*string == 'n') {
117 string++;
118 ret->n = atoi(string);
119 while (*string && isdigit(*string)) string++;
120 }
121 while (*string) {
122 if (*string == 'r') {
123 ret->rowsonly = TRUE;
124 } else if (*string == 'o') {
125 ret->orientable = TRUE;
126 } else if (*string == 'm') {
127 string++;
128 ret->movetarget = atoi(string);
129 while (string[1] && isdigit(string[1])) string++;
130 }
131 string++;
132 }
133 }
134
135 static char *encode_params(game_params *params, int full)
136 {
137 char buf[256];
138 sprintf(buf, "%dx%dn%d%s%s", params->w, params->h, params->n,
139 params->rowsonly ? "r" : "",
140 params->orientable ? "o" : "");
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);
145 return dupstr(buf);
146 }
147
148 static config_item *game_configure(game_params *params)
149 {
150 config_item *ret;
151 char buf[80];
152
153 ret = snewn(7, config_item);
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
178 ret[4].name = "Orientation matters";
179 ret[4].type = C_BOOLEAN;
180 ret[4].sval = NULL;
181 ret[4].ival = params->orientable;
182
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);
187 ret[5].ival = 0;
188
189 ret[6].name = NULL;
190 ret[6].type = C_END;
191 ret[6].sval = NULL;
192 ret[6].ival = 0;
193
194 return ret;
195 }
196
197 static 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;
205 ret->orientable = cfg[4].ival;
206 ret->movetarget = atoi(cfg[5].sval);
207
208 return ret;
209 }
210
211 static char *validate_params(game_params *params)
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 */
230 static void do_rotate(int *grid, int w, int h, int n, int orientable,
231 int x, int y, int dir)
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];
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;
269
270 for (k = 0; k < 4; k++)
271 g[k] = grid[p[k]];
272
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 }
279 }
280 }
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 }
291 }
292
293 static int grid_complete(int *grid, int wh, int orientable)
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;
300 if (orientable) {
301 for (i = 0; i < wh; i++)
302 if (grid[i] & 3)
303 ok = FALSE;
304 }
305 return ok;
306 }
307
308 static char *new_game_desc(game_params *params, random_state *rs,
309 game_aux_info **aux, int interactive)
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++)
323 grid[i] = ((params->rowsonly ? i/w : i) + 1) * 4;
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 */
332 total_moves = params->movetarget;
333 if (!total_moves)
334 /* Add a random move to avoid parity issues. */
335 total_moves = w*h*n*n*2 + random_upto(rs, 2);
336
337 do {
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;
346
347 for (i = 0; i < total_moves; i++) {
348 int x, y, r, oldtotal, newtotal, dx, dy;
349
350 do {
351 x = random_upto(rs, w - n + 1);
352 y = random_upto(rs, h - n + 1);
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;
365 } while (abs(newtotal) < abs(oldtotal) || abs(newtotal) > 2);
366
367 do_rotate(grid, w, h, n, params->orientable, x, y, r);
368
369 /*
370 * Log the rotation we've just performed at this point,
371 * for inversion detection in the next move.
372 *
373 * Also zero a section of the prevmoves array, because
374 * any rotation area which _overlaps_ this one is now
375 * entirely safe to perform further moves in.
376 *
377 * Two rotation areas overlap if their top left
378 * coordinates differ by strictly less than n in both
379 * directions
380 */
381 prevmoves[y*rw+x] += r;
382 for (dy = -n+1; dy <= n-1; dy++) {
383 if (y + dy < 0 || y + dy >= rh)
384 continue;
385 for (dx = -n+1; dx <= n-1; dx++) {
386 if (x + dx < 0 || x + dx >= rw)
387 continue;
388 if (dx == 0 && dy == 0)
389 continue;
390 prevmoves[(y+dy)*rw+(x+dx)] = 0;
391 }
392 }
393 }
394
395 sfree(prevmoves);
396
397 } while (grid_complete(grid, wh, params->orientable));
398
399 /*
400 * Now construct the game description, by describing the grid
401 * as a simple sequence of integers. They're comma-separated,
402 * unless the puzzle is orientable in which case they're
403 * separated by orientation letters `u', `d', `l' and `r'.
404 */
405 ret = NULL;
406 retlen = 0;
407 for (i = 0; i < wh; i++) {
408 char buf[80];
409 int k;
410
411 k = sprintf(buf, "%d%c", grid[i] / 4,
412 (char)(params->orientable ? "uldr"[grid[i] & 3] : ','));
413
414 ret = sresize(ret, retlen + k + 1, char);
415 strcpy(ret + retlen, buf);
416 retlen += k;
417 }
418 if (!params->orientable)
419 ret[retlen-1] = '\0'; /* delete last comma */
420
421 sfree(grid);
422 return ret;
423 }
424
425 static void game_free_aux_info(game_aux_info *aux)
426 {
427 assert(!"Shouldn't happen");
428 }
429
430 static char *validate_desc(game_params *params, char *desc)
431 {
432 char *p, *err;
433 int w = params->w, h = params->h, wh = w*h;
434 int i;
435
436 p = desc;
437 err = NULL;
438
439 for (i = 0; i < wh; i++) {
440 if (*p < '0' || *p > '9')
441 return "Not enough numbers in string";
442 while (*p >= '0' && *p <= '9')
443 p++;
444 if (!params->orientable && i < wh-1) {
445 if (*p != ',')
446 return "Expected comma after number";
447 } else if (params->orientable && i < wh) {
448 if (*p != 'l' && *p != 'r' && *p != 'u' && *p != 'd')
449 return "Expected orientation letter after number";
450 } else if (i == wh-1 && *p) {
451 return "Excess junk at end of string";
452 }
453
454 if (*p) p++; /* eat comma */
455 }
456
457 return NULL;
458 }
459
460 static game_state *new_game(midend_data *me, game_params *params, char *desc)
461 {
462 game_state *state = snew(game_state);
463 int w = params->w, h = params->h, n = params->n, wh = w*h;
464 int i;
465 char *p;
466
467 state->w = w;
468 state->h = h;
469 state->n = n;
470 state->orientable = params->orientable;
471 state->completed = 0;
472 state->used_solve = state->just_used_solve = FALSE;
473 state->movecount = 0;
474 state->movetarget = params->movetarget;
475 state->lastx = state->lasty = state->lastr = -1;
476
477 state->grid = snewn(wh, int);
478
479 p = desc;
480
481 for (i = 0; i < wh; i++) {
482 state->grid[i] = 4 * atoi(p);
483 while (*p >= '0' && *p <= '9')
484 p++;
485 if (*p) {
486 if (params->orientable) {
487 switch (*p) {
488 case 'l': state->grid[i] |= 1; break;
489 case 'd': state->grid[i] |= 2; break;
490 case 'r': state->grid[i] |= 3; break;
491 }
492 }
493 p++;
494 }
495 }
496
497 return state;
498 }
499
500 static game_state *dup_game(game_state *state)
501 {
502 game_state *ret = snew(game_state);
503
504 ret->w = state->w;
505 ret->h = state->h;
506 ret->n = state->n;
507 ret->orientable = state->orientable;
508 ret->completed = state->completed;
509 ret->movecount = state->movecount;
510 ret->movetarget = state->movetarget;
511 ret->lastx = state->lastx;
512 ret->lasty = state->lasty;
513 ret->lastr = state->lastr;
514 ret->used_solve = state->used_solve;
515 ret->just_used_solve = state->just_used_solve;
516
517 ret->grid = snewn(ret->w * ret->h, int);
518 memcpy(ret->grid, state->grid, ret->w * ret->h * sizeof(int));
519
520 return ret;
521 }
522
523 static void free_game(game_state *state)
524 {
525 sfree(state->grid);
526 sfree(state);
527 }
528
529 static int compare_int(const void *av, const void *bv)
530 {
531 const int *a = (const int *)av;
532 const int *b = (const int *)bv;
533 if (*a < *b)
534 return -1;
535 else if (*a > *b)
536 return +1;
537 else
538 return 0;
539 }
540
541 static game_state *solve_game(game_state *state, game_aux_info *aux,
542 char **error)
543 {
544 game_state *ret = dup_game(state);
545 int i;
546
547 /*
548 * Simply replace the grid with a solved one. For this game,
549 * this isn't a useful operation for actually telling the user
550 * what they should have done, but it is useful for
551 * conveniently being able to get hold of a clean state from
552 * which to practise manoeuvres.
553 */
554 qsort(ret->grid, ret->w*ret->h, sizeof(int), compare_int);
555 for (i = 0; i < ret->w*ret->h; i++)
556 ret->grid[i] &= ~3;
557 ret->used_solve = ret->just_used_solve = TRUE;
558 ret->completed = ret->movecount = 1;
559
560 return ret;
561 }
562
563 static char *game_text_format(game_state *state)
564 {
565 char *ret, *p, buf[80];
566 int i, x, y, col, o, maxlen;
567
568 /*
569 * First work out how many characters we need to display each
570 * number. We're pretty flexible on grid contents here, so we
571 * have to scan the entire grid.
572 */
573 col = 0;
574 for (i = 0; i < state->w * state->h; i++) {
575 x = sprintf(buf, "%d", state->grid[i] / 4);
576 if (col < x) col = x;
577 }
578 o = (state->orientable ? 1 : 0);
579
580 /*
581 * Now we know the exact total size of the grid we're going to
582 * produce: it's got h rows, each containing w lots of col+o,
583 * w-1 spaces and a trailing newline.
584 */
585 maxlen = state->h * state->w * (col+o+1);
586
587 ret = snewn(maxlen+1, char);
588 p = ret;
589
590 for (y = 0; y < state->h; y++) {
591 for (x = 0; x < state->w; x++) {
592 int v = state->grid[state->w*y+x];
593 sprintf(buf, "%*d", col, v/4);
594 memcpy(p, buf, col);
595 p += col;
596 if (o)
597 *p++ = "^<v>"[v & 3];
598 if (x+1 == state->w)
599 *p++ = '\n';
600 else
601 *p++ = ' ';
602 }
603 }
604
605 assert(p - ret == maxlen);
606 *p = '\0';
607 return ret;
608 }
609
610 static game_ui *new_ui(game_state *state)
611 {
612 return NULL;
613 }
614
615 static void free_ui(game_ui *ui)
616 {
617 }
618
619 static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
620 int x, int y, int button)
621 {
622 int w = from->w, h = from->h, n = from->n, wh = w*h;
623 game_state *ret;
624 int dir;
625
626 button = button & (~MOD_MASK | MOD_NUM_KEYPAD);
627
628 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
629 /*
630 * Determine the coordinates of the click. We offset by n-1
631 * half-blocks so that the user must click at the centre of
632 * a rotation region rather than at the corner.
633 */
634 x -= (n-1) * TILE_SIZE / 2;
635 y -= (n-1) * TILE_SIZE / 2;
636 x = FROMCOORD(x);
637 y = FROMCOORD(y);
638 dir = (button == LEFT_BUTTON ? 1 : -1);
639 if (x < 0 || x > w-n || y < 0 || y > h-n)
640 return NULL;
641 } else if (button == 'a' || button == 'A' || button==MOD_NUM_KEYPAD+'7') {
642 x = y = 0;
643 dir = (button == 'A' ? -1 : +1);
644 } else if (button == 'b' || button == 'B' || button==MOD_NUM_KEYPAD+'9') {
645 x = w-n;
646 y = 0;
647 dir = (button == 'B' ? -1 : +1);
648 } else if (button == 'c' || button == 'C' || button==MOD_NUM_KEYPAD+'1') {
649 x = 0;
650 y = h-n;
651 dir = (button == 'C' ? -1 : +1);
652 } else if (button == 'd' || button == 'D' || button==MOD_NUM_KEYPAD+'3') {
653 x = w-n;
654 y = h-n;
655 dir = (button == 'D' ? -1 : +1);
656 } else if (button==MOD_NUM_KEYPAD+'8' && (w-n) % 2 == 0) {
657 x = (w-n) / 2;
658 y = 0;
659 dir = +1;
660 } else if (button==MOD_NUM_KEYPAD+'2' && (w-n) % 2 == 0) {
661 x = (w-n) / 2;
662 y = h-n;
663 dir = +1;
664 } else if (button==MOD_NUM_KEYPAD+'4' && (h-n) % 2 == 0) {
665 x = 0;
666 y = (h-n) / 2;
667 dir = +1;
668 } else if (button==MOD_NUM_KEYPAD+'6' && (h-n) % 2 == 0) {
669 x = w-n;
670 y = (h-n) / 2;
671 dir = +1;
672 } else if (button==MOD_NUM_KEYPAD+'5' && (w-n) % 2 == 0 && (h-n) % 2 == 0){
673 x = (w-n) / 2;
674 y = (h-n) / 2;
675 dir = +1;
676 } else {
677 return NULL; /* no move to be made */
678 }
679
680 /*
681 * This is a valid move. Make it.
682 */
683 ret = dup_game(from);
684 ret->just_used_solve = FALSE; /* zero this in a hurry */
685 ret->movecount++;
686 do_rotate(ret->grid, w, h, n, ret->orientable, x, y, dir);
687 ret->lastx = x;
688 ret->lasty = y;
689 ret->lastr = dir;
690
691 /*
692 * See if the game has been completed. To do this we simply
693 * test that the grid contents are in increasing order.
694 */
695 if (!ret->completed && grid_complete(ret->grid, wh, ret->orientable))
696 ret->completed = ret->movecount;
697 return ret;
698 }
699
700 /* ----------------------------------------------------------------------
701 * Drawing routines.
702 */
703
704 struct game_drawstate {
705 int started;
706 int w, h, bgcolour;
707 int *grid;
708 };
709
710 static void game_size(game_params *params, int *x, int *y)
711 {
712 *x = TILE_SIZE * params->w + 2 * BORDER;
713 *y = TILE_SIZE * params->h + 2 * BORDER;
714 }
715
716 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
717 {
718 float *ret = snewn(3 * NCOLOURS, float);
719 int i;
720 float max;
721
722 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
723
724 /*
725 * Drop the background colour so that the highlight is
726 * noticeably brighter than it while still being under 1.
727 */
728 max = ret[COL_BACKGROUND*3];
729 for (i = 1; i < 3; i++)
730 if (ret[COL_BACKGROUND*3+i] > max)
731 max = ret[COL_BACKGROUND*3+i];
732 if (max * 1.2F > 1.0F) {
733 for (i = 0; i < 3; i++)
734 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
735 }
736
737 for (i = 0; i < 3; i++) {
738 ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F;
739 ret[COL_HIGHLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.1F;
740 ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F;
741 ret[COL_LOWLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
742 ret[COL_TEXT * 3 + i] = 0.0;
743 }
744
745 *ncolours = NCOLOURS;
746 return ret;
747 }
748
749 static game_drawstate *game_new_drawstate(game_state *state)
750 {
751 struct game_drawstate *ds = snew(struct game_drawstate);
752 int i;
753
754 ds->started = FALSE;
755 ds->w = state->w;
756 ds->h = state->h;
757 ds->bgcolour = COL_BACKGROUND;
758 ds->grid = snewn(ds->w*ds->h, int);
759 for (i = 0; i < ds->w*ds->h; i++)
760 ds->grid[i] = -1;
761
762 return ds;
763 }
764
765 static void game_free_drawstate(game_drawstate *ds)
766 {
767 sfree(ds->grid);
768 sfree(ds);
769 }
770
771 struct rotation {
772 int cx, cy, cw, ch; /* clip region */
773 int ox, oy; /* rotation origin */
774 float c, s; /* cos and sin of rotation angle */
775 int lc, rc, tc, bc; /* colours of tile edges */
776 };
777
778 static void rotate(int *xy, struct rotation *rot)
779 {
780 if (rot) {
781 float xf = xy[0] - rot->ox, yf = xy[1] - rot->oy;
782 float xf2, yf2;
783
784 xf2 = rot->c * xf + rot->s * yf;
785 yf2 = - rot->s * xf + rot->c * yf;
786
787 xy[0] = xf2 + rot->ox + 0.5; /* round to nearest */
788 xy[1] = yf2 + rot->oy + 0.5; /* round to nearest */
789 }
790 }
791
792 static void draw_tile(frontend *fe, game_state *state, int x, int y,
793 int tile, int flash_colour, struct rotation *rot)
794 {
795 int coords[8];
796 char str[40];
797
798 /*
799 * If we've been passed a rotation region but we're drawing a
800 * tile which is outside it, we must draw it normally. This can
801 * occur if we're cleaning up after a completion flash while a
802 * new move is also being made.
803 */
804 if (rot && (x < rot->cx || y < rot->cy ||
805 x >= rot->cx+rot->cw || y >= rot->cy+rot->ch))
806 rot = NULL;
807
808 if (rot)
809 clip(fe, rot->cx, rot->cy, rot->cw, rot->ch);
810
811 /*
812 * We must draw each side of the tile's highlight separately,
813 * because in some cases (during rotation) they will all need
814 * to be different colours.
815 */
816
817 /* The centre point is common to all sides. */
818 coords[4] = x + TILE_SIZE / 2;
819 coords[5] = y + TILE_SIZE / 2;
820 rotate(coords+4, rot);
821
822 /* Right side. */
823 coords[0] = x + TILE_SIZE - 1;
824 coords[1] = y + TILE_SIZE - 1;
825 rotate(coords+0, rot);
826 coords[2] = x + TILE_SIZE - 1;
827 coords[3] = y;
828 rotate(coords+2, rot);
829 draw_polygon(fe, coords, 3, TRUE, rot ? rot->rc : COL_LOWLIGHT);
830 draw_polygon(fe, coords, 3, FALSE, rot ? rot->rc : COL_LOWLIGHT);
831
832 /* Bottom side. */
833 coords[2] = x;
834 coords[3] = y + TILE_SIZE - 1;
835 rotate(coords+2, rot);
836 draw_polygon(fe, coords, 3, TRUE, rot ? rot->bc : COL_LOWLIGHT);
837 draw_polygon(fe, coords, 3, FALSE, rot ? rot->bc : COL_LOWLIGHT);
838
839 /* Left side. */
840 coords[0] = x;
841 coords[1] = y;
842 rotate(coords+0, rot);
843 draw_polygon(fe, coords, 3, TRUE, rot ? rot->lc : COL_HIGHLIGHT);
844 draw_polygon(fe, coords, 3, FALSE, rot ? rot->lc : COL_HIGHLIGHT);
845
846 /* Top side. */
847 coords[2] = x + TILE_SIZE - 1;
848 coords[3] = y;
849 rotate(coords+2, rot);
850 draw_polygon(fe, coords, 3, TRUE, rot ? rot->tc : COL_HIGHLIGHT);
851 draw_polygon(fe, coords, 3, FALSE, rot ? rot->tc : COL_HIGHLIGHT);
852
853 /*
854 * Now the main blank area in the centre of the tile.
855 */
856 if (rot) {
857 coords[0] = x + HIGHLIGHT_WIDTH;
858 coords[1] = y + HIGHLIGHT_WIDTH;
859 rotate(coords+0, rot);
860 coords[2] = x + HIGHLIGHT_WIDTH;
861 coords[3] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
862 rotate(coords+2, rot);
863 coords[4] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
864 coords[5] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
865 rotate(coords+4, rot);
866 coords[6] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH;
867 coords[7] = y + HIGHLIGHT_WIDTH;
868 rotate(coords+6, rot);
869 draw_polygon(fe, coords, 4, TRUE, flash_colour);
870 draw_polygon(fe, coords, 4, FALSE, flash_colour);
871 } else {
872 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
873 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
874 flash_colour);
875 }
876
877 /*
878 * Next, the triangles for orientation.
879 */
880 if (state->orientable) {
881 int xdx, xdy, ydx, ydy;
882 int cx, cy, displ, displ2;
883 switch (tile & 3) {
884 case 0:
885 xdx = 1, xdy = 0;
886 ydx = 0, ydy = 1;
887 break;
888 case 1:
889 xdx = 0, xdy = -1;
890 ydx = 1, ydy = 0;
891 break;
892 case 2:
893 xdx = -1, xdy = 0;
894 ydx = 0, ydy = -1;
895 break;
896 default /* case 3 */:
897 xdx = 0, xdy = 1;
898 ydx = -1, ydy = 0;
899 break;
900 }
901
902 cx = x + TILE_SIZE / 2;
903 cy = y + TILE_SIZE / 2;
904 displ = TILE_SIZE / 2 - HIGHLIGHT_WIDTH - 2;
905 displ2 = TILE_SIZE / 3 - HIGHLIGHT_WIDTH;
906
907 coords[0] = cx - displ * xdx + displ2 * ydx;
908 coords[1] = cy - displ * xdy + displ2 * ydy;
909 rotate(coords+0, rot);
910 coords[2] = cx + displ * xdx + displ2 * ydx;
911 coords[3] = cy + displ * xdy + displ2 * ydy;
912 rotate(coords+2, rot);
913 coords[4] = cx - displ * ydx;
914 coords[5] = cy - displ * ydy;
915 rotate(coords+4, rot);
916 draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT_GENTLE);
917 draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT_GENTLE);
918 }
919
920 coords[0] = x + TILE_SIZE/2;
921 coords[1] = y + TILE_SIZE/2;
922 rotate(coords+0, rot);
923 sprintf(str, "%d", tile / 4);
924 draw_text(fe, coords[0], coords[1],
925 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
926 COL_TEXT, str);
927
928 if (rot)
929 unclip(fe);
930
931 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
932 }
933
934 static int highlight_colour(float angle)
935 {
936 int colours[32] = {
937 COL_LOWLIGHT,
938 COL_LOWLIGHT_GENTLE,
939 COL_LOWLIGHT_GENTLE,
940 COL_LOWLIGHT_GENTLE,
941 COL_HIGHLIGHT_GENTLE,
942 COL_HIGHLIGHT_GENTLE,
943 COL_HIGHLIGHT_GENTLE,
944 COL_HIGHLIGHT,
945 COL_HIGHLIGHT,
946 COL_HIGHLIGHT,
947 COL_HIGHLIGHT,
948 COL_HIGHLIGHT,
949 COL_HIGHLIGHT,
950 COL_HIGHLIGHT,
951 COL_HIGHLIGHT,
952 COL_HIGHLIGHT,
953 COL_HIGHLIGHT,
954 COL_HIGHLIGHT_GENTLE,
955 COL_HIGHLIGHT_GENTLE,
956 COL_HIGHLIGHT_GENTLE,
957 COL_LOWLIGHT_GENTLE,
958 COL_LOWLIGHT_GENTLE,
959 COL_LOWLIGHT_GENTLE,
960 COL_LOWLIGHT,
961 COL_LOWLIGHT,
962 COL_LOWLIGHT,
963 COL_LOWLIGHT,
964 COL_LOWLIGHT,
965 COL_LOWLIGHT,
966 COL_LOWLIGHT,
967 COL_LOWLIGHT,
968 COL_LOWLIGHT,
969 };
970
971 return colours[(int)((angle + 2*PI) / (PI/16)) & 31];
972 }
973
974 static float game_anim_length(game_state *oldstate, game_state *newstate,
975 int dir, game_ui *ui)
976 {
977 if ((dir > 0 && newstate->just_used_solve) ||
978 (dir < 0 && oldstate->just_used_solve))
979 return 0.0F;
980 else
981 return ANIM_PER_RADIUS_UNIT * sqrt(newstate->n-1);
982 }
983
984 static float game_flash_length(game_state *oldstate, game_state *newstate,
985 int dir, game_ui *ui)
986 {
987 if (!oldstate->completed && newstate->completed &&
988 !oldstate->used_solve && !newstate->used_solve)
989 return 2 * FLASH_FRAME;
990 else
991 return 0.0F;
992 }
993
994 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
995 game_state *state, int dir, game_ui *ui,
996 float animtime, float flashtime)
997 {
998 int i, bgcolour;
999 struct rotation srot, *rot;
1000 int lastx = -1, lasty = -1, lastr = -1;
1001
1002 if (flashtime > 0) {
1003 int frame = (int)(flashtime / FLASH_FRAME);
1004 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
1005 } else
1006 bgcolour = COL_BACKGROUND;
1007
1008 if (!ds->started) {
1009 int coords[10];
1010
1011 draw_rect(fe, 0, 0,
1012 TILE_SIZE * state->w + 2 * BORDER,
1013 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
1014 draw_update(fe, 0, 0,
1015 TILE_SIZE * state->w + 2 * BORDER,
1016 TILE_SIZE * state->h + 2 * BORDER);
1017
1018 /*
1019 * Recessed area containing the whole puzzle.
1020 */
1021 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
1022 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
1023 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
1024 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
1025 coords[4] = coords[2] - TILE_SIZE;
1026 coords[5] = coords[3] + TILE_SIZE;
1027 coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
1028 coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
1029 coords[6] = coords[8] + TILE_SIZE;
1030 coords[7] = coords[9] - TILE_SIZE;
1031 draw_polygon(fe, coords, 5, TRUE, COL_HIGHLIGHT);
1032 draw_polygon(fe, coords, 5, FALSE, COL_HIGHLIGHT);
1033
1034 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
1035 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
1036 draw_polygon(fe, coords, 5, TRUE, COL_LOWLIGHT);
1037 draw_polygon(fe, coords, 5, FALSE, COL_LOWLIGHT);
1038
1039 ds->started = TRUE;
1040 }
1041
1042 /*
1043 * If we're drawing any rotated tiles, sort out the rotation
1044 * parameters, and also zap the rotation region to the
1045 * background colour before doing anything else.
1046 */
1047 if (oldstate) {
1048 float angle;
1049 float anim_max = game_anim_length(oldstate, state, dir, ui);
1050
1051 if (dir > 0) {
1052 lastx = state->lastx;
1053 lasty = state->lasty;
1054 lastr = state->lastr;
1055 } else {
1056 lastx = oldstate->lastx;
1057 lasty = oldstate->lasty;
1058 lastr = -oldstate->lastr;
1059 }
1060
1061 rot = &srot;
1062 rot->cx = COORD(lastx);
1063 rot->cy = COORD(lasty);
1064 rot->cw = rot->ch = TILE_SIZE * state->n;
1065 rot->ox = rot->cx + rot->cw/2;
1066 rot->oy = rot->cy + rot->ch/2;
1067 angle = (-PI/2 * lastr) * (1.0 - animtime / anim_max);
1068 rot->c = cos(angle);
1069 rot->s = sin(angle);
1070
1071 /*
1072 * Sort out the colours of the various sides of the tile.
1073 */
1074 rot->lc = highlight_colour(PI + angle);
1075 rot->rc = highlight_colour(angle);
1076 rot->tc = highlight_colour(PI/2 + angle);
1077 rot->bc = highlight_colour(-PI/2 + angle);
1078
1079 draw_rect(fe, rot->cx, rot->cy, rot->cw, rot->ch, bgcolour);
1080 } else
1081 rot = NULL;
1082
1083 /*
1084 * Now draw each tile.
1085 */
1086 for (i = 0; i < state->w * state->h; i++) {
1087 int t;
1088 int tx = i % state->w, ty = i / state->w;
1089
1090 /*
1091 * Figure out what should be displayed at this location.
1092 * Usually it will be state->grid[i], unless we're in the
1093 * middle of animating an actual rotation and this cell is
1094 * within the rotation region, in which case we set -1
1095 * (always display).
1096 */
1097 if (oldstate && lastx >= 0 && lasty >= 0 &&
1098 tx >= lastx && tx < lastx + state->n &&
1099 ty >= lasty && ty < lasty + state->n)
1100 t = -1;
1101 else
1102 t = state->grid[i];
1103
1104 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
1105 ds->grid[i] != t || ds->grid[i] == -1 || t == -1) {
1106 int x = COORD(tx), y = COORD(ty);
1107
1108 draw_tile(fe, state, x, y, state->grid[i], bgcolour, rot);
1109 ds->grid[i] = t;
1110 }
1111 }
1112 ds->bgcolour = bgcolour;
1113
1114 /*
1115 * Update the status bar.
1116 */
1117 {
1118 char statusbuf[256];
1119
1120 /*
1121 * Don't show the new status until we're also showing the
1122 * new _state_ - after the game animation is complete.
1123 */
1124 if (oldstate)
1125 state = oldstate;
1126
1127 if (state->used_solve)
1128 sprintf(statusbuf, "Moves since auto-solve: %d",
1129 state->movecount - state->completed);
1130 else {
1131 sprintf(statusbuf, "%sMoves: %d",
1132 (state->completed ? "COMPLETED! " : ""),
1133 (state->completed ? state->completed : state->movecount));
1134 if (state->movetarget)
1135 sprintf(statusbuf+strlen(statusbuf), " (target %d)",
1136 state->movetarget);
1137 }
1138
1139 status_bar(fe, statusbuf);
1140 }
1141 }
1142
1143 static int game_wants_statusbar(void)
1144 {
1145 return TRUE;
1146 }
1147
1148 static int game_timing_state(game_state *state)
1149 {
1150 return TRUE;
1151 }
1152
1153 #ifdef COMBINED
1154 #define thegame twiddle
1155 #endif
1156
1157 const struct game thegame = {
1158 "Twiddle", "games.twiddle",
1159 default_params,
1160 game_fetch_preset,
1161 decode_params,
1162 encode_params,
1163 free_params,
1164 dup_params,
1165 TRUE, game_configure, custom_params,
1166 validate_params,
1167 new_game_desc,
1168 game_free_aux_info,
1169 validate_desc,
1170 new_game,
1171 dup_game,
1172 free_game,
1173 TRUE, solve_game,
1174 TRUE, game_text_format,
1175 new_ui,
1176 free_ui,
1177 make_move,
1178 game_size,
1179 game_colours,
1180 game_new_drawstate,
1181 game_free_drawstate,
1182 game_redraw,
1183 game_anim_length,
1184 game_flash_length,
1185 game_wants_statusbar,
1186 FALSE, game_timing_state,
1187 0, /* mouse_priorities */
1188 };