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