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 | |
27 | enum { |
28 | COL_BACKGROUND, |
29 | COL_TEXT, |
30 | COL_HIGHLIGHT, |
31 | COL_HIGHLIGHT_GENTLE, |
32 | COL_LOWLIGHT, |
33 | COL_LOWLIGHT_GENTLE, |
34 | NCOLOURS |
35 | }; |
36 | |
37 | struct game_params { |
38 | int w, h, n; |
39 | int rowsonly; |
a3631c72 |
40 | int orientable; |
81875211 |
41 | int movetarget; |
9038fd11 |
42 | }; |
43 | |
44 | struct 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 | |
55 | static 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 | |
68 | static void free_params(game_params *params) |
69 | { |
70 | sfree(params); |
71 | } |
72 | |
73 | static 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 | |
80 | static 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 |
105 | static 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 |
136 | static 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 | |
149 | static 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 | |
198 | static 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 | |
212 | static 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 |
231 | static 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 |
294 | static 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 |
309 | static char *new_game_desc(game_params *params, random_state *rs, |
c566778e |
310 | char **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 | |
1185e3c5 |
433 | static char *validate_desc(game_params *params, char *desc) |
9038fd11 |
434 | { |
435 | char *p, *err; |
436 | int w = params->w, h = params->h, wh = w*h; |
437 | int i; |
438 | |
1185e3c5 |
439 | p = desc; |
9038fd11 |
440 | err = NULL; |
441 | |
442 | for (i = 0; i < wh; i++) { |
30861651 |
443 | if (*p < '0' || *p > '9') |
9038fd11 |
444 | return "Not enough numbers in string"; |
9038fd11 |
445 | while (*p >= '0' && *p <= '9') |
446 | p++; |
30861651 |
447 | if (!params->orientable && i < wh-1) { |
448 | if (*p != ',') |
449 | return "Expected comma after number"; |
450 | } else if (params->orientable && i < wh) { |
451 | if (*p != 'l' && *p != 'r' && *p != 'u' && *p != 'd') |
452 | return "Expected orientation letter after number"; |
453 | } else if (i == wh-1 && *p) { |
9038fd11 |
454 | return "Excess junk at end of string"; |
455 | } |
456 | |
457 | if (*p) p++; /* eat comma */ |
458 | } |
459 | |
460 | return NULL; |
461 | } |
462 | |
c380832d |
463 | static game_state *new_game(midend_data *me, game_params *params, char *desc) |
9038fd11 |
464 | { |
465 | game_state *state = snew(game_state); |
466 | int w = params->w, h = params->h, n = params->n, wh = w*h; |
467 | int i; |
468 | char *p; |
469 | |
470 | state->w = w; |
471 | state->h = h; |
472 | state->n = n; |
a3631c72 |
473 | state->orientable = params->orientable; |
9038fd11 |
474 | state->completed = 0; |
2ac6d24e |
475 | state->used_solve = state->just_used_solve = FALSE; |
9038fd11 |
476 | state->movecount = 0; |
81875211 |
477 | state->movetarget = params->movetarget; |
9038fd11 |
478 | state->lastx = state->lasty = state->lastr = -1; |
479 | |
480 | state->grid = snewn(wh, int); |
481 | |
1185e3c5 |
482 | p = desc; |
9038fd11 |
483 | |
484 | for (i = 0; i < wh; i++) { |
30861651 |
485 | state->grid[i] = 4 * atoi(p); |
9038fd11 |
486 | while (*p >= '0' && *p <= '9') |
487 | p++; |
30861651 |
488 | if (*p) { |
489 | if (params->orientable) { |
490 | switch (*p) { |
491 | case 'l': state->grid[i] |= 1; break; |
492 | case 'd': state->grid[i] |= 2; break; |
493 | case 'r': state->grid[i] |= 3; break; |
494 | } |
495 | } |
496 | p++; |
497 | } |
9038fd11 |
498 | } |
499 | |
500 | return state; |
501 | } |
502 | |
503 | static game_state *dup_game(game_state *state) |
504 | { |
505 | game_state *ret = snew(game_state); |
506 | |
507 | ret->w = state->w; |
508 | ret->h = state->h; |
509 | ret->n = state->n; |
a3631c72 |
510 | ret->orientable = state->orientable; |
9038fd11 |
511 | ret->completed = state->completed; |
512 | ret->movecount = state->movecount; |
81875211 |
513 | ret->movetarget = state->movetarget; |
9038fd11 |
514 | ret->lastx = state->lastx; |
515 | ret->lasty = state->lasty; |
516 | ret->lastr = state->lastr; |
2ac6d24e |
517 | ret->used_solve = state->used_solve; |
518 | ret->just_used_solve = state->just_used_solve; |
9038fd11 |
519 | |
520 | ret->grid = snewn(ret->w * ret->h, int); |
521 | memcpy(ret->grid, state->grid, ret->w * ret->h * sizeof(int)); |
522 | |
523 | return ret; |
524 | } |
525 | |
526 | static void free_game(game_state *state) |
527 | { |
528 | sfree(state->grid); |
529 | sfree(state); |
530 | } |
531 | |
2ac6d24e |
532 | static int compare_int(const void *av, const void *bv) |
533 | { |
534 | const int *a = (const int *)av; |
535 | const int *b = (const int *)bv; |
536 | if (*a < *b) |
537 | return -1; |
538 | else if (*a > *b) |
539 | return +1; |
540 | else |
541 | return 0; |
542 | } |
543 | |
df11cd4e |
544 | static char *solve_game(game_state *state, game_state *currstate, |
c566778e |
545 | char *aux, char **error) |
2ac6d24e |
546 | { |
df11cd4e |
547 | return dupstr("S"); |
2ac6d24e |
548 | } |
549 | |
9b4b03d3 |
550 | static char *game_text_format(game_state *state) |
551 | { |
af52394e |
552 | char *ret, *p, buf[80]; |
553 | int i, x, y, col, o, maxlen; |
554 | |
555 | /* |
556 | * First work out how many characters we need to display each |
557 | * number. We're pretty flexible on grid contents here, so we |
558 | * have to scan the entire grid. |
559 | */ |
560 | col = 0; |
561 | for (i = 0; i < state->w * state->h; i++) { |
562 | x = sprintf(buf, "%d", state->grid[i] / 4); |
563 | if (col < x) col = x; |
564 | } |
565 | o = (state->orientable ? 1 : 0); |
566 | |
567 | /* |
568 | * Now we know the exact total size of the grid we're going to |
569 | * produce: it's got h rows, each containing w lots of col+o, |
570 | * w-1 spaces and a trailing newline. |
571 | */ |
572 | maxlen = state->h * state->w * (col+o+1); |
573 | |
48a10826 |
574 | ret = snewn(maxlen+1, char); |
af52394e |
575 | p = ret; |
576 | |
577 | for (y = 0; y < state->h; y++) { |
578 | for (x = 0; x < state->w; x++) { |
579 | int v = state->grid[state->w*y+x]; |
580 | sprintf(buf, "%*d", col, v/4); |
581 | memcpy(p, buf, col); |
582 | p += col; |
583 | if (o) |
584 | *p++ = "^<v>"[v & 3]; |
585 | if (x+1 == state->w) |
586 | *p++ = '\n'; |
587 | else |
588 | *p++ = ' '; |
589 | } |
590 | } |
591 | |
592 | assert(p - ret == maxlen); |
593 | *p = '\0'; |
594 | return ret; |
9b4b03d3 |
595 | } |
596 | |
9038fd11 |
597 | static game_ui *new_ui(game_state *state) |
598 | { |
599 | return NULL; |
600 | } |
601 | |
602 | static void free_ui(game_ui *ui) |
603 | { |
604 | } |
605 | |
844f605f |
606 | static char *encode_ui(game_ui *ui) |
ae8290c6 |
607 | { |
608 | return NULL; |
609 | } |
610 | |
844f605f |
611 | static void decode_ui(game_ui *ui, char *encoding) |
ae8290c6 |
612 | { |
613 | } |
614 | |
07dfb697 |
615 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
616 | game_state *newstate) |
617 | { |
618 | } |
619 | |
1e3e152d |
620 | struct game_drawstate { |
621 | int started; |
622 | int w, h, bgcolour; |
623 | int *grid; |
624 | int tilesize; |
625 | }; |
626 | |
df11cd4e |
627 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
628 | int x, int y, int button) |
9038fd11 |
629 | { |
df11cd4e |
630 | int w = state->w, h = state->h, n = state->n /* , wh = w*h */; |
631 | char buf[80]; |
9038fd11 |
632 | int dir; |
633 | |
f0ee053c |
634 | button = button & (~MOD_MASK | MOD_NUM_KEYPAD); |
635 | |
9038fd11 |
636 | if (button == LEFT_BUTTON || button == RIGHT_BUTTON) { |
637 | /* |
638 | * Determine the coordinates of the click. We offset by n-1 |
639 | * half-blocks so that the user must click at the centre of |
640 | * a rotation region rather than at the corner. |
641 | */ |
642 | x -= (n-1) * TILE_SIZE / 2; |
643 | y -= (n-1) * TILE_SIZE / 2; |
644 | x = FROMCOORD(x); |
645 | y = FROMCOORD(y); |
a96666e4 |
646 | dir = (button == LEFT_BUTTON ? 1 : -1); |
647 | if (x < 0 || x > w-n || y < 0 || y > h-n) |
9038fd11 |
648 | return NULL; |
a96666e4 |
649 | } else if (button == 'a' || button == 'A' || button==MOD_NUM_KEYPAD+'7') { |
650 | x = y = 0; |
651 | dir = (button == 'A' ? -1 : +1); |
652 | } else if (button == 'b' || button == 'B' || button==MOD_NUM_KEYPAD+'9') { |
653 | x = w-n; |
654 | y = 0; |
655 | dir = (button == 'B' ? -1 : +1); |
656 | } else if (button == 'c' || button == 'C' || button==MOD_NUM_KEYPAD+'1') { |
657 | x = 0; |
658 | y = h-n; |
659 | dir = (button == 'C' ? -1 : +1); |
660 | } else if (button == 'd' || button == 'D' || button==MOD_NUM_KEYPAD+'3') { |
661 | x = w-n; |
662 | y = h-n; |
663 | dir = (button == 'D' ? -1 : +1); |
664 | } else if (button==MOD_NUM_KEYPAD+'8' && (w-n) % 2 == 0) { |
665 | x = (w-n) / 2; |
666 | y = 0; |
667 | dir = +1; |
668 | } else if (button==MOD_NUM_KEYPAD+'2' && (w-n) % 2 == 0) { |
669 | x = (w-n) / 2; |
670 | y = h-n; |
671 | dir = +1; |
672 | } else if (button==MOD_NUM_KEYPAD+'4' && (h-n) % 2 == 0) { |
673 | x = 0; |
674 | y = (h-n) / 2; |
675 | dir = +1; |
676 | } else if (button==MOD_NUM_KEYPAD+'6' && (h-n) % 2 == 0) { |
677 | x = w-n; |
678 | y = (h-n) / 2; |
679 | dir = +1; |
680 | } else if (button==MOD_NUM_KEYPAD+'5' && (w-n) % 2 == 0 && (h-n) % 2 == 0){ |
681 | x = (w-n) / 2; |
682 | y = (h-n) / 2; |
683 | dir = +1; |
684 | } else { |
685 | return NULL; /* no move to be made */ |
686 | } |
9038fd11 |
687 | |
a96666e4 |
688 | /* |
df11cd4e |
689 | * If we reach here, we have a valid move. |
a96666e4 |
690 | */ |
df11cd4e |
691 | sprintf(buf, "M%d,%d,%d", x, y, dir); |
692 | return dupstr(buf); |
693 | } |
694 | |
695 | static game_state *execute_move(game_state *from, char *move) |
696 | { |
697 | game_state *ret; |
698 | int w = from->w, h = from->h, n = from->n, wh = w*h; |
699 | int x, y, dir; |
700 | |
701 | if (!strcmp(move, "S")) { |
702 | int i; |
703 | ret = dup_game(from); |
704 | |
705 | /* |
706 | * Simply replace the grid with a solved one. For this game, |
707 | * this isn't a useful operation for actually telling the user |
708 | * what they should have done, but it is useful for |
709 | * conveniently being able to get hold of a clean state from |
710 | * which to practise manoeuvres. |
711 | */ |
712 | qsort(ret->grid, ret->w*ret->h, sizeof(int), compare_int); |
713 | for (i = 0; i < ret->w*ret->h; i++) |
714 | ret->grid[i] &= ~3; |
715 | ret->used_solve = ret->just_used_solve = TRUE; |
716 | ret->completed = ret->movecount = 1; |
717 | |
718 | return ret; |
719 | } |
720 | |
721 | if (move[0] != 'M' || |
722 | sscanf(move+1, "%d,%d,%d", &x, &y, &dir) != 3 || |
723 | x < 0 || y < 0 || x > from->w - n || y > from->h - n) |
724 | return NULL; /* can't parse this move string */ |
725 | |
a96666e4 |
726 | ret = dup_game(from); |
727 | ret->just_used_solve = FALSE; /* zero this in a hurry */ |
728 | ret->movecount++; |
729 | do_rotate(ret->grid, w, h, n, ret->orientable, x, y, dir); |
730 | ret->lastx = x; |
731 | ret->lasty = y; |
732 | ret->lastr = dir; |
9038fd11 |
733 | |
a96666e4 |
734 | /* |
735 | * See if the game has been completed. To do this we simply |
736 | * test that the grid contents are in increasing order. |
737 | */ |
738 | if (!ret->completed && grid_complete(ret->grid, wh, ret->orientable)) |
739 | ret->completed = ret->movecount; |
740 | return ret; |
9038fd11 |
741 | } |
742 | |
743 | /* ---------------------------------------------------------------------- |
744 | * Drawing routines. |
745 | */ |
746 | |
1e3e152d |
747 | static void game_size(game_params *params, game_drawstate *ds, |
748 | int *x, int *y, int expand) |
9038fd11 |
749 | { |
871bf294 |
750 | double tsx, tsy, ts; |
1e3e152d |
751 | /* |
752 | * Each window dimension equals the tile size times one more |
753 | * than the grid dimension (the border is half the width of the |
754 | * tiles). |
755 | */ |
871bf294 |
756 | tsx = (double)*x / ((double)params->w + 1.0); |
757 | tsy = (double)*y / ((double)params->h + 1.0); |
1e3e152d |
758 | ts = min(tsx, tsy); |
759 | if (expand) |
871bf294 |
760 | ds->tilesize = (int)(ts + 0.5); |
1e3e152d |
761 | else |
871bf294 |
762 | ds->tilesize = min((int)ts, PREFERRED_TILE_SIZE); |
1e3e152d |
763 | |
9038fd11 |
764 | *x = TILE_SIZE * params->w + 2 * BORDER; |
765 | *y = TILE_SIZE * params->h + 2 * BORDER; |
766 | } |
767 | |
768 | static float *game_colours(frontend *fe, game_state *state, int *ncolours) |
769 | { |
770 | float *ret = snewn(3 * NCOLOURS, float); |
771 | int i; |
772 | float max; |
773 | |
774 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
775 | |
776 | /* |
777 | * Drop the background colour so that the highlight is |
778 | * noticeably brighter than it while still being under 1. |
779 | */ |
780 | max = ret[COL_BACKGROUND*3]; |
781 | for (i = 1; i < 3; i++) |
782 | if (ret[COL_BACKGROUND*3+i] > max) |
783 | max = ret[COL_BACKGROUND*3+i]; |
784 | if (max * 1.2F > 1.0F) { |
785 | for (i = 0; i < 3; i++) |
786 | ret[COL_BACKGROUND*3+i] /= (max * 1.2F); |
787 | } |
788 | |
789 | for (i = 0; i < 3; i++) { |
790 | ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F; |
791 | ret[COL_HIGHLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.1F; |
792 | ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F; |
793 | ret[COL_LOWLIGHT_GENTLE * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F; |
794 | ret[COL_TEXT * 3 + i] = 0.0; |
795 | } |
796 | |
797 | *ncolours = NCOLOURS; |
798 | return ret; |
799 | } |
800 | |
801 | static game_drawstate *game_new_drawstate(game_state *state) |
802 | { |
803 | struct game_drawstate *ds = snew(struct game_drawstate); |
804 | int i; |
805 | |
806 | ds->started = FALSE; |
807 | ds->w = state->w; |
808 | ds->h = state->h; |
809 | ds->bgcolour = COL_BACKGROUND; |
810 | ds->grid = snewn(ds->w*ds->h, int); |
1e3e152d |
811 | ds->tilesize = 0; /* haven't decided yet */ |
9038fd11 |
812 | for (i = 0; i < ds->w*ds->h; i++) |
813 | ds->grid[i] = -1; |
814 | |
815 | return ds; |
816 | } |
817 | |
818 | static void game_free_drawstate(game_drawstate *ds) |
819 | { |
ab53eb64 |
820 | sfree(ds->grid); |
9038fd11 |
821 | sfree(ds); |
822 | } |
823 | |
824 | struct rotation { |
825 | int cx, cy, cw, ch; /* clip region */ |
826 | int ox, oy; /* rotation origin */ |
827 | float c, s; /* cos and sin of rotation angle */ |
828 | int lc, rc, tc, bc; /* colours of tile edges */ |
829 | }; |
830 | |
831 | static void rotate(int *xy, struct rotation *rot) |
832 | { |
833 | if (rot) { |
834 | float xf = xy[0] - rot->ox, yf = xy[1] - rot->oy; |
835 | float xf2, yf2; |
836 | |
837 | xf2 = rot->c * xf + rot->s * yf; |
838 | yf2 = - rot->s * xf + rot->c * yf; |
839 | |
840 | xy[0] = xf2 + rot->ox + 0.5; /* round to nearest */ |
841 | xy[1] = yf2 + rot->oy + 0.5; /* round to nearest */ |
842 | } |
843 | } |
844 | |
1e3e152d |
845 | static void draw_tile(frontend *fe, game_drawstate *ds, game_state *state, |
846 | int x, int y, int tile, int flash_colour, |
847 | struct rotation *rot) |
9038fd11 |
848 | { |
849 | int coords[8]; |
850 | char str[40]; |
851 | |
0551ee6b |
852 | /* |
853 | * If we've been passed a rotation region but we're drawing a |
854 | * tile which is outside it, we must draw it normally. This can |
855 | * occur if we're cleaning up after a completion flash while a |
856 | * new move is also being made. |
857 | */ |
858 | if (rot && (x < rot->cx || y < rot->cy || |
daaccee8 |
859 | x >= rot->cx+rot->cw || y >= rot->cy+rot->ch)) |
0551ee6b |
860 | rot = NULL; |
861 | |
9038fd11 |
862 | if (rot) |
863 | clip(fe, rot->cx, rot->cy, rot->cw, rot->ch); |
864 | |
865 | /* |
866 | * We must draw each side of the tile's highlight separately, |
867 | * because in some cases (during rotation) they will all need |
868 | * to be different colours. |
869 | */ |
870 | |
871 | /* The centre point is common to all sides. */ |
872 | coords[4] = x + TILE_SIZE / 2; |
873 | coords[5] = y + TILE_SIZE / 2; |
874 | rotate(coords+4, rot); |
875 | |
876 | /* Right side. */ |
877 | coords[0] = x + TILE_SIZE - 1; |
878 | coords[1] = y + TILE_SIZE - 1; |
879 | rotate(coords+0, rot); |
880 | coords[2] = x + TILE_SIZE - 1; |
881 | coords[3] = y; |
882 | rotate(coords+2, rot); |
883 | draw_polygon(fe, coords, 3, TRUE, rot ? rot->rc : COL_LOWLIGHT); |
884 | draw_polygon(fe, coords, 3, FALSE, rot ? rot->rc : COL_LOWLIGHT); |
885 | |
886 | /* Bottom side. */ |
887 | coords[2] = x; |
888 | coords[3] = y + TILE_SIZE - 1; |
889 | rotate(coords+2, rot); |
890 | draw_polygon(fe, coords, 3, TRUE, rot ? rot->bc : COL_LOWLIGHT); |
891 | draw_polygon(fe, coords, 3, FALSE, rot ? rot->bc : COL_LOWLIGHT); |
892 | |
893 | /* Left side. */ |
894 | coords[0] = x; |
895 | coords[1] = y; |
896 | rotate(coords+0, rot); |
897 | draw_polygon(fe, coords, 3, TRUE, rot ? rot->lc : COL_HIGHLIGHT); |
898 | draw_polygon(fe, coords, 3, FALSE, rot ? rot->lc : COL_HIGHLIGHT); |
899 | |
900 | /* Top side. */ |
901 | coords[2] = x + TILE_SIZE - 1; |
902 | coords[3] = y; |
903 | rotate(coords+2, rot); |
904 | draw_polygon(fe, coords, 3, TRUE, rot ? rot->tc : COL_HIGHLIGHT); |
905 | draw_polygon(fe, coords, 3, FALSE, rot ? rot->tc : COL_HIGHLIGHT); |
906 | |
a3631c72 |
907 | /* |
908 | * Now the main blank area in the centre of the tile. |
909 | */ |
9038fd11 |
910 | if (rot) { |
911 | coords[0] = x + HIGHLIGHT_WIDTH; |
912 | coords[1] = y + HIGHLIGHT_WIDTH; |
913 | rotate(coords+0, rot); |
914 | coords[2] = x + HIGHLIGHT_WIDTH; |
915 | coords[3] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH; |
916 | rotate(coords+2, rot); |
917 | coords[4] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH; |
918 | coords[5] = y + TILE_SIZE - 1 - HIGHLIGHT_WIDTH; |
919 | rotate(coords+4, rot); |
920 | coords[6] = x + TILE_SIZE - 1 - HIGHLIGHT_WIDTH; |
921 | coords[7] = y + HIGHLIGHT_WIDTH; |
922 | rotate(coords+6, rot); |
923 | draw_polygon(fe, coords, 4, TRUE, flash_colour); |
924 | draw_polygon(fe, coords, 4, FALSE, flash_colour); |
925 | } else { |
926 | draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH, |
927 | TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH, |
928 | flash_colour); |
929 | } |
930 | |
a3631c72 |
931 | /* |
4b24f582 |
932 | * Next, the triangles for orientation. |
a3631c72 |
933 | */ |
934 | if (state->orientable) { |
d50832a3 |
935 | int xdx, xdy, ydx, ydy; |
936 | int cx, cy, displ, displ2; |
a3631c72 |
937 | switch (tile & 3) { |
938 | case 0: |
d50832a3 |
939 | xdx = 1, xdy = 0; |
940 | ydx = 0, ydy = 1; |
a3631c72 |
941 | break; |
942 | case 1: |
d50832a3 |
943 | xdx = 0, xdy = -1; |
944 | ydx = 1, ydy = 0; |
a3631c72 |
945 | break; |
946 | case 2: |
d50832a3 |
947 | xdx = -1, xdy = 0; |
948 | ydx = 0, ydy = -1; |
a3631c72 |
949 | break; |
950 | default /* case 3 */: |
d50832a3 |
951 | xdx = 0, xdy = 1; |
952 | ydx = -1, ydy = 0; |
a3631c72 |
953 | break; |
954 | } |
955 | |
d50832a3 |
956 | cx = x + TILE_SIZE / 2; |
957 | cy = y + TILE_SIZE / 2; |
958 | displ = TILE_SIZE / 2 - HIGHLIGHT_WIDTH - 2; |
959 | displ2 = TILE_SIZE / 3 - HIGHLIGHT_WIDTH; |
a3631c72 |
960 | |
30861651 |
961 | coords[0] = cx - displ * xdx + displ2 * ydx; |
962 | coords[1] = cy - displ * xdy + displ2 * ydy; |
a3631c72 |
963 | rotate(coords+0, rot); |
30861651 |
964 | coords[2] = cx + displ * xdx + displ2 * ydx; |
965 | coords[3] = cy + displ * xdy + displ2 * ydy; |
a3631c72 |
966 | rotate(coords+2, rot); |
30861651 |
967 | coords[4] = cx - displ * ydx; |
968 | coords[5] = cy - displ * ydy; |
a3631c72 |
969 | rotate(coords+4, rot); |
d50832a3 |
970 | draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT_GENTLE); |
971 | draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT_GENTLE); |
a3631c72 |
972 | } |
973 | |
9038fd11 |
974 | coords[0] = x + TILE_SIZE/2; |
975 | coords[1] = y + TILE_SIZE/2; |
976 | rotate(coords+0, rot); |
a3631c72 |
977 | sprintf(str, "%d", tile / 4); |
9038fd11 |
978 | draw_text(fe, coords[0], coords[1], |
979 | FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE, |
980 | COL_TEXT, str); |
981 | |
982 | if (rot) |
983 | unclip(fe); |
984 | |
985 | draw_update(fe, x, y, TILE_SIZE, TILE_SIZE); |
986 | } |
987 | |
988 | static int highlight_colour(float angle) |
989 | { |
990 | int colours[32] = { |
991 | COL_LOWLIGHT, |
992 | COL_LOWLIGHT_GENTLE, |
993 | COL_LOWLIGHT_GENTLE, |
994 | COL_LOWLIGHT_GENTLE, |
995 | COL_HIGHLIGHT_GENTLE, |
996 | COL_HIGHLIGHT_GENTLE, |
997 | COL_HIGHLIGHT_GENTLE, |
998 | COL_HIGHLIGHT, |
999 | COL_HIGHLIGHT, |
1000 | COL_HIGHLIGHT, |
1001 | COL_HIGHLIGHT, |
1002 | COL_HIGHLIGHT, |
1003 | COL_HIGHLIGHT, |
1004 | COL_HIGHLIGHT, |
1005 | COL_HIGHLIGHT, |
1006 | COL_HIGHLIGHT, |
1007 | COL_HIGHLIGHT, |
1008 | COL_HIGHLIGHT_GENTLE, |
1009 | COL_HIGHLIGHT_GENTLE, |
1010 | COL_HIGHLIGHT_GENTLE, |
1011 | COL_LOWLIGHT_GENTLE, |
1012 | COL_LOWLIGHT_GENTLE, |
1013 | COL_LOWLIGHT_GENTLE, |
1014 | COL_LOWLIGHT, |
1015 | COL_LOWLIGHT, |
1016 | COL_LOWLIGHT, |
1017 | COL_LOWLIGHT, |
1018 | COL_LOWLIGHT, |
1019 | COL_LOWLIGHT, |
1020 | COL_LOWLIGHT, |
1021 | COL_LOWLIGHT, |
1022 | COL_LOWLIGHT, |
1023 | }; |
1024 | |
1025 | return colours[(int)((angle + 2*PI) / (PI/16)) & 31]; |
1026 | } |
1027 | |
1028 | static float game_anim_length(game_state *oldstate, game_state *newstate, |
e3f21163 |
1029 | int dir, game_ui *ui) |
9038fd11 |
1030 | { |
2ac6d24e |
1031 | if ((dir > 0 && newstate->just_used_solve) || |
1032 | (dir < 0 && oldstate->just_used_solve)) |
1033 | return 0.0F; |
1034 | else |
1035 | return ANIM_PER_RADIUS_UNIT * sqrt(newstate->n-1); |
9038fd11 |
1036 | } |
1037 | |
1038 | static float game_flash_length(game_state *oldstate, game_state *newstate, |
e3f21163 |
1039 | int dir, game_ui *ui) |
9038fd11 |
1040 | { |
2ac6d24e |
1041 | if (!oldstate->completed && newstate->completed && |
1042 | !oldstate->used_solve && !newstate->used_solve) |
9038fd11 |
1043 | return 2 * FLASH_FRAME; |
1044 | else |
1045 | return 0.0F; |
1046 | } |
1047 | |
1048 | static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, |
1049 | game_state *state, int dir, game_ui *ui, |
1050 | float animtime, float flashtime) |
1051 | { |
1052 | int i, bgcolour; |
1053 | struct rotation srot, *rot; |
1054 | int lastx = -1, lasty = -1, lastr = -1; |
1055 | |
1056 | if (flashtime > 0) { |
1057 | int frame = (int)(flashtime / FLASH_FRAME); |
1058 | bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT); |
1059 | } else |
1060 | bgcolour = COL_BACKGROUND; |
1061 | |
1062 | if (!ds->started) { |
19f24306 |
1063 | int coords[10]; |
9038fd11 |
1064 | |
1065 | draw_rect(fe, 0, 0, |
1066 | TILE_SIZE * state->w + 2 * BORDER, |
1067 | TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND); |
1068 | draw_update(fe, 0, 0, |
1069 | TILE_SIZE * state->w + 2 * BORDER, |
1070 | TILE_SIZE * state->h + 2 * BORDER); |
1071 | |
1072 | /* |
1073 | * Recessed area containing the whole puzzle. |
1074 | */ |
1075 | coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1; |
1076 | coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1; |
1077 | coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1; |
1078 | coords[3] = COORD(0) - HIGHLIGHT_WIDTH; |
19f24306 |
1079 | coords[4] = coords[2] - TILE_SIZE; |
1080 | coords[5] = coords[3] + TILE_SIZE; |
1081 | coords[8] = COORD(0) - HIGHLIGHT_WIDTH; |
1082 | coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1; |
1083 | coords[6] = coords[8] + TILE_SIZE; |
1084 | coords[7] = coords[9] - TILE_SIZE; |
1085 | draw_polygon(fe, coords, 5, TRUE, COL_HIGHLIGHT); |
1086 | draw_polygon(fe, coords, 5, FALSE, COL_HIGHLIGHT); |
9038fd11 |
1087 | |
1088 | coords[1] = COORD(0) - HIGHLIGHT_WIDTH; |
1089 | coords[0] = COORD(0) - HIGHLIGHT_WIDTH; |
19f24306 |
1090 | draw_polygon(fe, coords, 5, TRUE, COL_LOWLIGHT); |
1091 | draw_polygon(fe, coords, 5, FALSE, COL_LOWLIGHT); |
9038fd11 |
1092 | |
1093 | ds->started = TRUE; |
1094 | } |
1095 | |
1096 | /* |
1097 | * If we're drawing any rotated tiles, sort out the rotation |
1098 | * parameters, and also zap the rotation region to the |
1099 | * background colour before doing anything else. |
1100 | */ |
1101 | if (oldstate) { |
1102 | float angle; |
e3f21163 |
1103 | float anim_max = game_anim_length(oldstate, state, dir, ui); |
9038fd11 |
1104 | |
1105 | if (dir > 0) { |
1106 | lastx = state->lastx; |
1107 | lasty = state->lasty; |
1108 | lastr = state->lastr; |
1109 | } else { |
1110 | lastx = oldstate->lastx; |
1111 | lasty = oldstate->lasty; |
1112 | lastr = -oldstate->lastr; |
1113 | } |
1114 | |
1115 | rot = &srot; |
1116 | rot->cx = COORD(lastx); |
1117 | rot->cy = COORD(lasty); |
1118 | rot->cw = rot->ch = TILE_SIZE * state->n; |
1119 | rot->ox = rot->cx + rot->cw/2; |
1120 | rot->oy = rot->cy + rot->ch/2; |
1121 | angle = (-PI/2 * lastr) * (1.0 - animtime / anim_max); |
1122 | rot->c = cos(angle); |
1123 | rot->s = sin(angle); |
1124 | |
1125 | /* |
1126 | * Sort out the colours of the various sides of the tile. |
1127 | */ |
1128 | rot->lc = highlight_colour(PI + angle); |
1129 | rot->rc = highlight_colour(angle); |
1130 | rot->tc = highlight_colour(PI/2 + angle); |
1131 | rot->bc = highlight_colour(-PI/2 + angle); |
1132 | |
1133 | draw_rect(fe, rot->cx, rot->cy, rot->cw, rot->ch, bgcolour); |
1134 | } else |
1135 | rot = NULL; |
1136 | |
1137 | /* |
1138 | * Now draw each tile. |
1139 | */ |
1140 | for (i = 0; i < state->w * state->h; i++) { |
1141 | int t; |
1142 | int tx = i % state->w, ty = i / state->w; |
1143 | |
1144 | /* |
1145 | * Figure out what should be displayed at this location. |
1146 | * Usually it will be state->grid[i], unless we're in the |
1147 | * middle of animating an actual rotation and this cell is |
1148 | * within the rotation region, in which case we set -1 |
1149 | * (always display). |
1150 | */ |
1151 | if (oldstate && lastx >= 0 && lasty >= 0 && |
1152 | tx >= lastx && tx < lastx + state->n && |
1153 | ty >= lasty && ty < lasty + state->n) |
1154 | t = -1; |
1155 | else |
1156 | t = state->grid[i]; |
1157 | |
1158 | if (ds->bgcolour != bgcolour || /* always redraw when flashing */ |
1159 | ds->grid[i] != t || ds->grid[i] == -1 || t == -1) { |
1160 | int x = COORD(tx), y = COORD(ty); |
1161 | |
1e3e152d |
1162 | draw_tile(fe, ds, state, x, y, state->grid[i], bgcolour, rot); |
9038fd11 |
1163 | ds->grid[i] = t; |
1164 | } |
1165 | } |
1166 | ds->bgcolour = bgcolour; |
1167 | |
1168 | /* |
1169 | * Update the status bar. |
1170 | */ |
1171 | { |
1172 | char statusbuf[256]; |
1173 | |
1174 | /* |
1175 | * Don't show the new status until we're also showing the |
1176 | * new _state_ - after the game animation is complete. |
1177 | */ |
1178 | if (oldstate) |
1179 | state = oldstate; |
1180 | |
2ac6d24e |
1181 | if (state->used_solve) |
1182 | sprintf(statusbuf, "Moves since auto-solve: %d", |
1183 | state->movecount - state->completed); |
81875211 |
1184 | else { |
2ac6d24e |
1185 | sprintf(statusbuf, "%sMoves: %d", |
1186 | (state->completed ? "COMPLETED! " : ""), |
1187 | (state->completed ? state->completed : state->movecount)); |
81875211 |
1188 | if (state->movetarget) |
1189 | sprintf(statusbuf+strlen(statusbuf), " (target %d)", |
1190 | state->movetarget); |
1191 | } |
9038fd11 |
1192 | |
1193 | status_bar(fe, statusbuf); |
1194 | } |
1195 | } |
1196 | |
1197 | static int game_wants_statusbar(void) |
1198 | { |
1199 | return TRUE; |
1200 | } |
1201 | |
48dcdd62 |
1202 | static int game_timing_state(game_state *state) |
1203 | { |
1204 | return TRUE; |
1205 | } |
1206 | |
9038fd11 |
1207 | #ifdef COMBINED |
1208 | #define thegame twiddle |
1209 | #endif |
1210 | |
1211 | const struct game thegame = { |
1d228b10 |
1212 | "Twiddle", "games.twiddle", |
9038fd11 |
1213 | default_params, |
1214 | game_fetch_preset, |
1215 | decode_params, |
1216 | encode_params, |
1217 | free_params, |
1218 | dup_params, |
1d228b10 |
1219 | TRUE, game_configure, custom_params, |
9038fd11 |
1220 | validate_params, |
1185e3c5 |
1221 | new_game_desc, |
1185e3c5 |
1222 | validate_desc, |
9038fd11 |
1223 | new_game, |
1224 | dup_game, |
1225 | free_game, |
2ac6d24e |
1226 | TRUE, solve_game, |
af52394e |
1227 | TRUE, game_text_format, |
9038fd11 |
1228 | new_ui, |
1229 | free_ui, |
ae8290c6 |
1230 | encode_ui, |
1231 | decode_ui, |
07dfb697 |
1232 | game_changed_state, |
df11cd4e |
1233 | interpret_move, |
1234 | execute_move, |
9038fd11 |
1235 | game_size, |
1236 | game_colours, |
1237 | game_new_drawstate, |
1238 | game_free_drawstate, |
1239 | game_redraw, |
1240 | game_anim_length, |
1241 | game_flash_length, |
1242 | game_wants_statusbar, |
48dcdd62 |
1243 | FALSE, game_timing_state, |
93b1da3d |
1244 | 0, /* mouse_priorities */ |
9038fd11 |
1245 | }; |