4efb3868 |
1 | /* |
2 | * sixteen.c: `16-puzzle', a sliding-tiles jigsaw which differs |
3 | * from the 15-puzzle in that you toroidally rotate a row or column |
4 | * at a time. |
5 | */ |
6 | |
7 | #include <stdio.h> |
8 | #include <stdlib.h> |
9 | #include <string.h> |
10 | #include <assert.h> |
b0e26073 |
11 | #include <ctype.h> |
4efb3868 |
12 | #include <math.h> |
13 | |
14 | #include "puzzles.h" |
15 | |
4efb3868 |
16 | #define TILE_SIZE 48 |
17 | #define BORDER TILE_SIZE /* big border to fill with arrows */ |
18 | #define HIGHLIGHT_WIDTH (TILE_SIZE / 20) |
19 | #define COORD(x) ( (x) * TILE_SIZE + BORDER ) |
20 | #define FROMCOORD(x) ( ((x) - BORDER + 2*TILE_SIZE) / TILE_SIZE - 2 ) |
21 | |
8c1fd974 |
22 | #define ANIM_TIME 0.13F |
23 | #define FLASH_FRAME 0.13F |
4efb3868 |
24 | |
25 | #define X(state, i) ( (i) % (state)->w ) |
26 | #define Y(state, i) ( (i) / (state)->w ) |
27 | #define C(state, x, y) ( (y) * (state)->w + (x) ) |
28 | |
29 | enum { |
30 | COL_BACKGROUND, |
31 | COL_TEXT, |
32 | COL_HIGHLIGHT, |
33 | COL_LOWLIGHT, |
34 | NCOLOURS |
35 | }; |
36 | |
37 | struct game_params { |
38 | int w, h; |
81875211 |
39 | int movetarget; |
4efb3868 |
40 | }; |
41 | |
42 | struct game_state { |
43 | int w, h, n; |
44 | int *tiles; |
45 | int completed; |
2ac6d24e |
46 | int just_used_solve; /* used to suppress undo animation */ |
47 | int used_solve; /* used to suppress completion flash */ |
81875211 |
48 | int movecount, movetarget; |
c8230524 |
49 | int last_movement_sense; |
4efb3868 |
50 | }; |
51 | |
be8d5aa1 |
52 | static game_params *default_params(void) |
4efb3868 |
53 | { |
54 | game_params *ret = snew(game_params); |
55 | |
56 | ret->w = ret->h = 4; |
81875211 |
57 | ret->movetarget = 0; |
4efb3868 |
58 | |
59 | return ret; |
60 | } |
61 | |
be8d5aa1 |
62 | static int game_fetch_preset(int i, char **name, game_params **params) |
4efb3868 |
63 | { |
64 | game_params *ret; |
65 | int w, h; |
66 | char buf[80]; |
67 | |
68 | switch (i) { |
69 | case 0: w = 3, h = 3; break; |
70 | case 1: w = 4, h = 3; break; |
71 | case 2: w = 4, h = 4; break; |
72 | case 3: w = 5, h = 4; break; |
73 | case 4: w = 5, h = 5; break; |
74 | default: return FALSE; |
75 | } |
76 | |
77 | sprintf(buf, "%dx%d", w, h); |
78 | *name = dupstr(buf); |
79 | *params = ret = snew(game_params); |
80 | ret->w = w; |
81 | ret->h = h; |
81875211 |
82 | ret->movetarget = 0; |
4efb3868 |
83 | return TRUE; |
84 | } |
85 | |
be8d5aa1 |
86 | static void free_params(game_params *params) |
4efb3868 |
87 | { |
88 | sfree(params); |
89 | } |
90 | |
be8d5aa1 |
91 | static game_params *dup_params(game_params *params) |
4efb3868 |
92 | { |
93 | game_params *ret = snew(game_params); |
94 | *ret = *params; /* structure copy */ |
95 | return ret; |
96 | } |
97 | |
1185e3c5 |
98 | static void decode_params(game_params *ret, char const *string) |
b0e26073 |
99 | { |
b0e26073 |
100 | ret->w = ret->h = atoi(string); |
1185e3c5 |
101 | ret->movetarget = 0; |
b0e26073 |
102 | while (*string && isdigit(*string)) string++; |
103 | if (*string == 'x') { |
104 | string++; |
105 | ret->h = atoi(string); |
81875211 |
106 | while (*string && isdigit((unsigned char)*string)) |
107 | string++; |
108 | } |
109 | if (*string == 'm') { |
110 | string++; |
111 | ret->movetarget = atoi(string); |
112 | while (*string && isdigit((unsigned char)*string)) |
113 | string++; |
b0e26073 |
114 | } |
b0e26073 |
115 | } |
116 | |
1185e3c5 |
117 | static char *encode_params(game_params *params, int full) |
b0e26073 |
118 | { |
119 | char data[256]; |
120 | |
121 | sprintf(data, "%dx%d", params->w, params->h); |
1185e3c5 |
122 | /* Shuffle limit is part of the limited parameters, because we have to |
123 | * supply the target move count. */ |
124 | if (params->movetarget) |
125 | sprintf(data + strlen(data), "m%d", params->movetarget); |
b0e26073 |
126 | |
127 | return dupstr(data); |
128 | } |
129 | |
be8d5aa1 |
130 | static config_item *game_configure(game_params *params) |
c8230524 |
131 | { |
132 | config_item *ret; |
133 | char buf[80]; |
134 | |
81875211 |
135 | ret = snewn(4, config_item); |
c8230524 |
136 | |
137 | ret[0].name = "Width"; |
95709966 |
138 | ret[0].type = C_STRING; |
c8230524 |
139 | sprintf(buf, "%d", params->w); |
140 | ret[0].sval = dupstr(buf); |
141 | ret[0].ival = 0; |
142 | |
143 | ret[1].name = "Height"; |
95709966 |
144 | ret[1].type = C_STRING; |
c8230524 |
145 | sprintf(buf, "%d", params->h); |
146 | ret[1].sval = dupstr(buf); |
147 | ret[1].ival = 0; |
148 | |
81875211 |
149 | ret[2].name = "Number of shuffling moves"; |
150 | ret[2].type = C_STRING; |
151 | sprintf(buf, "%d", params->movetarget); |
152 | ret[2].sval = dupstr(buf); |
c8230524 |
153 | ret[2].ival = 0; |
154 | |
81875211 |
155 | ret[3].name = NULL; |
156 | ret[3].type = C_END; |
157 | ret[3].sval = NULL; |
158 | ret[3].ival = 0; |
159 | |
c8230524 |
160 | return ret; |
161 | } |
162 | |
be8d5aa1 |
163 | static game_params *custom_params(config_item *cfg) |
c8230524 |
164 | { |
165 | game_params *ret = snew(game_params); |
166 | |
167 | ret->w = atoi(cfg[0].sval); |
168 | ret->h = atoi(cfg[1].sval); |
81875211 |
169 | ret->movetarget = atoi(cfg[2].sval); |
c8230524 |
170 | |
171 | return ret; |
172 | } |
173 | |
be8d5aa1 |
174 | static char *validate_params(game_params *params) |
c8230524 |
175 | { |
176 | if (params->w < 2 && params->h < 2) |
177 | return "Width and height must both be at least two"; |
178 | |
179 | return NULL; |
180 | } |
181 | |
be8d5aa1 |
182 | static int perm_parity(int *perm, int n) |
4efb3868 |
183 | { |
184 | int i, j, ret; |
185 | |
186 | ret = 0; |
187 | |
188 | for (i = 0; i < n-1; i++) |
189 | for (j = i+1; j < n; j++) |
190 | if (perm[i] > perm[j]) |
191 | ret = !ret; |
192 | |
193 | return ret; |
194 | } |
195 | |
1185e3c5 |
196 | static char *new_game_desc(game_params *params, random_state *rs, |
6aa6af4c |
197 | game_aux_info **aux, int interactive) |
4efb3868 |
198 | { |
199 | int stop, n, i, x; |
200 | int x1, x2, p1, p2; |
201 | int *tiles, *used; |
202 | char *ret; |
203 | int retlen; |
204 | |
205 | n = params->w * params->h; |
206 | |
207 | tiles = snewn(n, int); |
4efb3868 |
208 | |
81875211 |
209 | if (params->movetarget) { |
210 | int prevstart = -1, prevoffset = -1, prevdirection = 0, nrepeats = 0; |
4efb3868 |
211 | |
81875211 |
212 | /* |
213 | * Shuffle the old-fashioned way, by making a series of |
214 | * single moves on the grid. |
215 | */ |
4efb3868 |
216 | |
81875211 |
217 | for (i = 0; i < n; i++) |
218 | tiles[i] = i; |
219 | |
220 | for (i = 0; i < params->movetarget; i++) { |
221 | int start, offset, len, direction; |
222 | int j, tmp; |
223 | |
224 | /* |
225 | * Choose a move to make. We can choose from any row |
226 | * or any column. |
227 | */ |
228 | while (1) { |
229 | j = random_upto(rs, params->w + params->h); |
230 | |
231 | if (j < params->w) { |
232 | /* Column. */ |
233 | start = j; |
234 | offset = params->w; |
235 | len = params->h; |
236 | } else { |
237 | /* Row. */ |
238 | start = (j - params->w) * params->w; |
239 | offset = 1; |
240 | len = params->w; |
241 | } |
4efb3868 |
242 | |
81875211 |
243 | direction = -1 + 2 * random_upto(rs, 2); |
4efb3868 |
244 | |
81875211 |
245 | /* |
246 | * To at least _try_ to avoid boring cases, check that |
247 | * this move doesn't directly undo the previous one, or |
248 | * repeat it so many times as to turn it into fewer |
249 | * moves. |
250 | */ |
251 | if (start == prevstart && offset == prevoffset) { |
252 | if (direction == -prevdirection) |
253 | continue; /* inverse of previous move */ |
254 | else if (2 * (nrepeats+1) > len) |
255 | continue; /* previous move repeated too often */ |
256 | } |
4efb3868 |
257 | |
81875211 |
258 | /* If we didn't `continue', we've found an OK move to make. */ |
259 | break; |
260 | } |
4efb3868 |
261 | |
81875211 |
262 | /* |
263 | * Now save the move into the `prev' variables. |
264 | */ |
265 | if (start == prevstart && offset == prevoffset) { |
266 | nrepeats++; |
267 | } else { |
268 | prevstart = start; |
269 | prevoffset = offset; |
270 | prevdirection = direction; |
271 | nrepeats = 1; |
272 | } |
4efb3868 |
273 | |
81875211 |
274 | /* |
275 | * And make it. |
276 | */ |
277 | if (direction < 0) { |
278 | start += (len-1) * offset; |
279 | offset = -offset; |
280 | } |
281 | tmp = tiles[start]; |
282 | for (j = 0; j+1 < len; j++) |
283 | tiles[start + j*offset] = tiles[start + (j+1)*offset]; |
284 | tiles[start + (len-1) * offset] = tmp; |
285 | } |
286 | |
287 | } else { |
288 | |
289 | used = snewn(n, int); |
290 | |
291 | for (i = 0; i < n; i++) { |
292 | tiles[i] = -1; |
293 | used[i] = FALSE; |
294 | } |
295 | |
296 | /* |
297 | * If both dimensions are odd, there is a parity |
298 | * constraint. |
299 | */ |
300 | if (params->w & params->h & 1) |
301 | stop = 2; |
302 | else |
303 | stop = 0; |
304 | |
305 | /* |
306 | * Place everything except (possibly) the last two tiles. |
307 | */ |
308 | for (x = 0, i = n; i > stop; i--) { |
309 | int k = i > 1 ? random_upto(rs, i) : 0; |
310 | int j; |
311 | |
312 | for (j = 0; j < n; j++) |
313 | if (!used[j] && (k-- == 0)) |
314 | break; |
315 | |
316 | assert(j < n && !used[j]); |
317 | used[j] = TRUE; |
318 | |
319 | while (tiles[x] >= 0) |
320 | x++; |
321 | assert(x < n); |
322 | tiles[x] = j; |
323 | } |
324 | |
325 | if (stop) { |
326 | /* |
327 | * Find the last two locations, and the last two |
328 | * pieces. |
329 | */ |
330 | while (tiles[x] >= 0) |
331 | x++; |
332 | assert(x < n); |
333 | x1 = x; |
334 | x++; |
335 | while (tiles[x] >= 0) |
336 | x++; |
337 | assert(x < n); |
338 | x2 = x; |
339 | |
340 | for (i = 0; i < n; i++) |
341 | if (!used[i]) |
342 | break; |
343 | p1 = i; |
344 | for (i = p1+1; i < n; i++) |
345 | if (!used[i]) |
346 | break; |
347 | p2 = i; |
348 | |
349 | /* |
350 | * Try the last two tiles one way round. If that fails, |
351 | * swap them. |
352 | */ |
353 | tiles[x1] = p1; |
354 | tiles[x2] = p2; |
355 | if (perm_parity(tiles, n) != 0) { |
356 | tiles[x1] = p2; |
357 | tiles[x2] = p1; |
358 | assert(perm_parity(tiles, n) == 0); |
359 | } |
360 | } |
361 | |
362 | sfree(used); |
4efb3868 |
363 | } |
364 | |
365 | /* |
1185e3c5 |
366 | * Now construct the game description, by describing the tile |
367 | * array as a simple sequence of comma-separated integers. |
4efb3868 |
368 | */ |
369 | ret = NULL; |
370 | retlen = 0; |
371 | for (i = 0; i < n; i++) { |
372 | char buf[80]; |
373 | int k; |
374 | |
375 | k = sprintf(buf, "%d,", tiles[i]+1); |
376 | |
377 | ret = sresize(ret, retlen + k + 1, char); |
378 | strcpy(ret + retlen, buf); |
379 | retlen += k; |
380 | } |
381 | ret[retlen-1] = '\0'; /* delete last comma */ |
382 | |
383 | sfree(tiles); |
4efb3868 |
384 | |
385 | return ret; |
386 | } |
387 | |
2ac6d24e |
388 | static void game_free_aux_info(game_aux_info *aux) |
6f2d8d7c |
389 | { |
390 | assert(!"Shouldn't happen"); |
391 | } |
392 | |
5928817c |
393 | |
1185e3c5 |
394 | static char *validate_desc(game_params *params, char *desc) |
5928817c |
395 | { |
396 | char *p, *err; |
397 | int i, area; |
398 | int *used; |
399 | |
400 | area = params->w * params->h; |
1185e3c5 |
401 | p = desc; |
5928817c |
402 | err = NULL; |
403 | |
404 | used = snewn(area, int); |
405 | for (i = 0; i < area; i++) |
406 | used[i] = FALSE; |
407 | |
408 | for (i = 0; i < area; i++) { |
409 | char *q = p; |
410 | int n; |
411 | |
412 | if (*p < '0' || *p > '9') { |
413 | err = "Not enough numbers in string"; |
414 | goto leave; |
415 | } |
416 | while (*p >= '0' && *p <= '9') |
417 | p++; |
418 | if (i < area-1 && *p != ',') { |
419 | err = "Expected comma after number"; |
420 | goto leave; |
421 | } |
422 | else if (i == area-1 && *p) { |
423 | err = "Excess junk at end of string"; |
424 | goto leave; |
425 | } |
426 | n = atoi(q); |
427 | if (n < 1 || n > area) { |
428 | err = "Number out of range"; |
429 | goto leave; |
430 | } |
431 | if (used[n-1]) { |
432 | err = "Number used twice"; |
433 | goto leave; |
434 | } |
435 | used[n-1] = TRUE; |
436 | |
437 | if (*p) p++; /* eat comma */ |
438 | } |
439 | |
440 | leave: |
441 | sfree(used); |
442 | return err; |
443 | } |
444 | |
c380832d |
445 | static game_state *new_game(midend_data *me, game_params *params, char *desc) |
4efb3868 |
446 | { |
447 | game_state *state = snew(game_state); |
448 | int i; |
449 | char *p; |
450 | |
451 | state->w = params->w; |
452 | state->h = params->h; |
453 | state->n = params->w * params->h; |
454 | state->tiles = snewn(state->n, int); |
455 | |
1185e3c5 |
456 | p = desc; |
4efb3868 |
457 | i = 0; |
458 | for (i = 0; i < state->n; i++) { |
459 | assert(*p); |
460 | state->tiles[i] = atoi(p); |
461 | while (*p && *p != ',') |
462 | p++; |
463 | if (*p) p++; /* eat comma */ |
464 | } |
465 | assert(!*p); |
466 | |
fd1a1a2b |
467 | state->completed = state->movecount = 0; |
81875211 |
468 | state->movetarget = params->movetarget; |
2ac6d24e |
469 | state->used_solve = state->just_used_solve = FALSE; |
c8230524 |
470 | state->last_movement_sense = 0; |
4efb3868 |
471 | |
472 | return state; |
473 | } |
474 | |
be8d5aa1 |
475 | static game_state *dup_game(game_state *state) |
4efb3868 |
476 | { |
477 | game_state *ret = snew(game_state); |
478 | |
479 | ret->w = state->w; |
480 | ret->h = state->h; |
481 | ret->n = state->n; |
482 | ret->tiles = snewn(state->w * state->h, int); |
483 | memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int)); |
484 | ret->completed = state->completed; |
fd1a1a2b |
485 | ret->movecount = state->movecount; |
81875211 |
486 | ret->movetarget = state->movetarget; |
2ac6d24e |
487 | ret->used_solve = state->used_solve; |
488 | ret->just_used_solve = state->just_used_solve; |
c8230524 |
489 | ret->last_movement_sense = state->last_movement_sense; |
4efb3868 |
490 | |
491 | return ret; |
492 | } |
493 | |
be8d5aa1 |
494 | static void free_game(game_state *state) |
4efb3868 |
495 | { |
496 | sfree(state); |
497 | } |
498 | |
2ac6d24e |
499 | static game_state *solve_game(game_state *state, game_aux_info *aux, |
500 | char **error) |
501 | { |
502 | game_state *ret = dup_game(state); |
503 | int i; |
504 | |
505 | /* |
506 | * Simply replace the grid with a solved one. For this game, |
507 | * this isn't a useful operation for actually telling the user |
508 | * what they should have done, but it is useful for |
509 | * conveniently being able to get hold of a clean state from |
510 | * which to practise manoeuvres. |
511 | */ |
512 | for (i = 0; i < ret->n; i++) |
513 | ret->tiles[i] = i+1; |
514 | ret->used_solve = ret->just_used_solve = TRUE; |
e73ca44d |
515 | ret->completed = ret->movecount = 1; |
2ac6d24e |
516 | |
517 | return ret; |
518 | } |
519 | |
9b4b03d3 |
520 | static char *game_text_format(game_state *state) |
521 | { |
af52394e |
522 | char *ret, *p, buf[80]; |
523 | int x, y, col, maxlen; |
524 | |
525 | /* |
526 | * First work out how many characters we need to display each |
527 | * number. |
528 | */ |
529 | col = sprintf(buf, "%d", state->n); |
530 | |
531 | /* |
532 | * Now we know the exact total size of the grid we're going to |
533 | * produce: it's got h rows, each containing w lots of col, w-1 |
534 | * spaces and a trailing newline. |
535 | */ |
536 | maxlen = state->h * state->w * (col+1); |
537 | |
48a10826 |
538 | ret = snewn(maxlen+1, char); |
af52394e |
539 | p = ret; |
540 | |
541 | for (y = 0; y < state->h; y++) { |
542 | for (x = 0; x < state->w; x++) { |
543 | int v = state->tiles[state->w*y+x]; |
544 | sprintf(buf, "%*d", col, v); |
545 | memcpy(p, buf, col); |
546 | p += col; |
547 | if (x+1 == state->w) |
548 | *p++ = '\n'; |
549 | else |
550 | *p++ = ' '; |
551 | } |
552 | } |
553 | |
554 | assert(p - ret == maxlen); |
555 | *p = '\0'; |
556 | return ret; |
9b4b03d3 |
557 | } |
558 | |
be8d5aa1 |
559 | static game_ui *new_ui(game_state *state) |
74a4e547 |
560 | { |
561 | return NULL; |
562 | } |
563 | |
be8d5aa1 |
564 | static void free_ui(game_ui *ui) |
74a4e547 |
565 | { |
566 | } |
567 | |
be8d5aa1 |
568 | static game_state *make_move(game_state *from, game_ui *ui, |
569 | int x, int y, int button) |
4efb3868 |
570 | { |
571 | int cx, cy; |
572 | int dx, dy, tx, ty, n; |
573 | game_state *ret; |
574 | |
f0ee053c |
575 | button &= ~MOD_MASK; |
e91825f8 |
576 | if (button != LEFT_BUTTON && button != RIGHT_BUTTON) |
4efb3868 |
577 | return NULL; |
578 | |
579 | cx = FROMCOORD(x); |
580 | cy = FROMCOORD(y); |
581 | if (cx == -1 && cy >= 0 && cy < from->h) |
582 | n = from->w, dx = +1, dy = 0; |
583 | else if (cx == from->w && cy >= 0 && cy < from->h) |
584 | n = from->w, dx = -1, dy = 0; |
585 | else if (cy == -1 && cx >= 0 && cx < from->w) |
586 | n = from->h, dy = +1, dx = 0; |
587 | else if (cy == from->h && cx >= 0 && cx < from->w) |
588 | n = from->h, dy = -1, dx = 0; |
589 | else |
590 | return NULL; /* invalid click location */ |
591 | |
e91825f8 |
592 | /* reverse direction if right hand button is pressed */ |
593 | if (button == RIGHT_BUTTON) |
594 | { |
595 | dx = -dx; if (dx) cx = from->w - 1 - cx; |
596 | dy = -dy; if (dy) cy = from->h - 1 - cy; |
597 | } |
598 | |
4efb3868 |
599 | ret = dup_game(from); |
2ac6d24e |
600 | ret->just_used_solve = FALSE; /* zero this in a hurry */ |
4efb3868 |
601 | |
602 | do { |
603 | cx += dx; |
604 | cy += dy; |
605 | tx = (cx + dx + from->w) % from->w; |
606 | ty = (cy + dy + from->h) % from->h; |
607 | ret->tiles[C(ret, cx, cy)] = from->tiles[C(from, tx, ty)]; |
608 | } while (--n > 0); |
609 | |
fd1a1a2b |
610 | ret->movecount++; |
611 | |
c8230524 |
612 | ret->last_movement_sense = -(dx+dy); |
613 | |
4efb3868 |
614 | /* |
615 | * See if the game has been completed. |
616 | */ |
617 | if (!ret->completed) { |
fd1a1a2b |
618 | ret->completed = ret->movecount; |
4efb3868 |
619 | for (n = 0; n < ret->n; n++) |
620 | if (ret->tiles[n] != n+1) |
621 | ret->completed = FALSE; |
622 | } |
623 | |
624 | return ret; |
625 | } |
626 | |
627 | /* ---------------------------------------------------------------------- |
628 | * Drawing routines. |
629 | */ |
630 | |
631 | struct game_drawstate { |
632 | int started; |
633 | int w, h, bgcolour; |
634 | int *tiles; |
635 | }; |
636 | |
be8d5aa1 |
637 | static void game_size(game_params *params, int *x, int *y) |
4efb3868 |
638 | { |
639 | *x = TILE_SIZE * params->w + 2 * BORDER; |
640 | *y = TILE_SIZE * params->h + 2 * BORDER; |
641 | } |
642 | |
be8d5aa1 |
643 | static float *game_colours(frontend *fe, game_state *state, int *ncolours) |
4efb3868 |
644 | { |
645 | float *ret = snewn(3 * NCOLOURS, float); |
646 | int i; |
647 | float max; |
648 | |
649 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
650 | |
651 | /* |
652 | * Drop the background colour so that the highlight is |
653 | * noticeably brighter than it while still being under 1. |
654 | */ |
655 | max = ret[COL_BACKGROUND*3]; |
656 | for (i = 1; i < 3; i++) |
657 | if (ret[COL_BACKGROUND*3+i] > max) |
658 | max = ret[COL_BACKGROUND*3+i]; |
659 | if (max * 1.2F > 1.0F) { |
660 | for (i = 0; i < 3; i++) |
661 | ret[COL_BACKGROUND*3+i] /= (max * 1.2F); |
662 | } |
663 | |
664 | for (i = 0; i < 3; i++) { |
665 | ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F; |
666 | ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F; |
667 | ret[COL_TEXT * 3 + i] = 0.0; |
668 | } |
669 | |
670 | *ncolours = NCOLOURS; |
671 | return ret; |
672 | } |
673 | |
be8d5aa1 |
674 | static game_drawstate *game_new_drawstate(game_state *state) |
4efb3868 |
675 | { |
676 | struct game_drawstate *ds = snew(struct game_drawstate); |
677 | int i; |
678 | |
679 | ds->started = FALSE; |
680 | ds->w = state->w; |
681 | ds->h = state->h; |
682 | ds->bgcolour = COL_BACKGROUND; |
683 | ds->tiles = snewn(ds->w*ds->h, int); |
684 | for (i = 0; i < ds->w*ds->h; i++) |
685 | ds->tiles[i] = -1; |
686 | |
687 | return ds; |
688 | } |
689 | |
be8d5aa1 |
690 | static void game_free_drawstate(game_drawstate *ds) |
4efb3868 |
691 | { |
692 | sfree(ds->tiles); |
693 | sfree(ds); |
694 | } |
695 | |
696 | static void draw_tile(frontend *fe, game_state *state, int x, int y, |
697 | int tile, int flash_colour) |
698 | { |
699 | if (tile == 0) { |
700 | draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE, |
701 | flash_colour); |
702 | } else { |
703 | int coords[6]; |
704 | char str[40]; |
705 | |
706 | coords[0] = x + TILE_SIZE - 1; |
707 | coords[1] = y + TILE_SIZE - 1; |
708 | coords[2] = x + TILE_SIZE - 1; |
709 | coords[3] = y; |
710 | coords[4] = x; |
711 | coords[5] = y + TILE_SIZE - 1; |
712 | draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT); |
713 | draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT); |
714 | |
715 | coords[0] = x; |
716 | coords[1] = y; |
717 | draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT); |
718 | draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT); |
719 | |
720 | draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH, |
721 | TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH, |
722 | flash_colour); |
723 | |
724 | sprintf(str, "%d", tile); |
725 | draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2, |
726 | FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE, |
727 | COL_TEXT, str); |
728 | } |
729 | draw_update(fe, x, y, TILE_SIZE, TILE_SIZE); |
730 | } |
731 | |
732 | static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy) |
733 | { |
734 | int coords[14]; |
735 | int ydy = -xdx, ydx = xdy; |
736 | |
737 | #define POINT(n, xx, yy) ( \ |
738 | coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \ |
739 | coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy) |
740 | |
741 | POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */ |
742 | POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */ |
743 | POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */ |
744 | POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */ |
745 | POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */ |
746 | POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */ |
747 | POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */ |
748 | |
749 | draw_polygon(fe, coords, 7, TRUE, COL_LOWLIGHT); |
750 | draw_polygon(fe, coords, 7, FALSE, COL_TEXT); |
751 | } |
752 | |
be8d5aa1 |
753 | static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, |
c822de4a |
754 | game_state *state, int dir, game_ui *ui, |
74a4e547 |
755 | float animtime, float flashtime) |
4efb3868 |
756 | { |
b443c381 |
757 | int i, bgcolour; |
4efb3868 |
758 | |
759 | if (flashtime > 0) { |
760 | int frame = (int)(flashtime / FLASH_FRAME); |
761 | bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT); |
762 | } else |
763 | bgcolour = COL_BACKGROUND; |
764 | |
765 | if (!ds->started) { |
766 | int coords[6]; |
767 | |
768 | draw_rect(fe, 0, 0, |
769 | TILE_SIZE * state->w + 2 * BORDER, |
770 | TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND); |
771 | draw_update(fe, 0, 0, |
772 | TILE_SIZE * state->w + 2 * BORDER, |
773 | TILE_SIZE * state->h + 2 * BORDER); |
774 | |
775 | /* |
776 | * Recessed area containing the whole puzzle. |
777 | */ |
778 | coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1; |
779 | coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1; |
780 | coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1; |
781 | coords[3] = COORD(0) - HIGHLIGHT_WIDTH; |
782 | coords[4] = COORD(0) - HIGHLIGHT_WIDTH; |
783 | coords[5] = COORD(state->h) + HIGHLIGHT_WIDTH - 1; |
784 | draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT); |
785 | draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT); |
786 | |
787 | coords[1] = COORD(0) - HIGHLIGHT_WIDTH; |
788 | coords[0] = COORD(0) - HIGHLIGHT_WIDTH; |
789 | draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT); |
790 | draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT); |
791 | |
792 | /* |
793 | * Arrows for making moves. |
794 | */ |
795 | for (i = 0; i < state->w; i++) { |
796 | draw_arrow(fe, COORD(i), COORD(0), +1, 0); |
797 | draw_arrow(fe, COORD(i+1), COORD(state->h), -1, 0); |
798 | } |
799 | for (i = 0; i < state->h; i++) { |
800 | draw_arrow(fe, COORD(state->w), COORD(i), 0, +1); |
801 | draw_arrow(fe, COORD(0), COORD(i+1), 0, -1); |
802 | } |
803 | |
804 | ds->started = TRUE; |
805 | } |
806 | |
807 | /* |
b443c381 |
808 | * Now draw each tile. |
4efb3868 |
809 | */ |
810 | |
811 | clip(fe, COORD(0), COORD(0), TILE_SIZE*state->w, TILE_SIZE*state->h); |
812 | |
b443c381 |
813 | for (i = 0; i < state->n; i++) { |
814 | int t, t0; |
815 | /* |
816 | * Figure out what should be displayed at this |
817 | * location. It's either a simple tile, or it's a |
818 | * transition between two tiles (in which case we say |
819 | * -1 because it must always be drawn). |
820 | */ |
821 | |
822 | if (oldstate && oldstate->tiles[i] != state->tiles[i]) |
823 | t = -1; |
824 | else |
825 | t = state->tiles[i]; |
826 | |
827 | t0 = t; |
828 | |
829 | if (ds->bgcolour != bgcolour || /* always redraw when flashing */ |
830 | ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) { |
831 | int x, y, x2, y2; |
832 | |
833 | /* |
834 | * Figure out what to _actually_ draw, and where to |
835 | * draw it. |
836 | */ |
837 | if (t == -1) { |
838 | int x0, y0, x1, y1, dx, dy; |
839 | int j; |
840 | float c; |
841 | int sense; |
842 | |
5b5c6b12 |
843 | if (dir < 0) { |
844 | assert(oldstate); |
b443c381 |
845 | sense = -oldstate->last_movement_sense; |
5b5c6b12 |
846 | } else { |
b443c381 |
847 | sense = state->last_movement_sense; |
5b5c6b12 |
848 | } |
b443c381 |
849 | |
850 | t = state->tiles[i]; |
851 | |
852 | /* |
853 | * FIXME: must be prepared to draw a double |
854 | * tile in some situations. |
855 | */ |
856 | |
857 | /* |
858 | * Find the coordinates of this tile in the old and |
859 | * new states. |
860 | */ |
861 | x1 = COORD(X(state, i)); |
862 | y1 = COORD(Y(state, i)); |
863 | for (j = 0; j < oldstate->n; j++) |
864 | if (oldstate->tiles[j] == state->tiles[i]) |
865 | break; |
866 | assert(j < oldstate->n); |
867 | x0 = COORD(X(state, j)); |
868 | y0 = COORD(Y(state, j)); |
869 | |
870 | dx = (x1 - x0); |
871 | if (dx != 0 && |
872 | dx != TILE_SIZE * sense) { |
873 | dx = (dx < 0 ? dx + TILE_SIZE * state->w : |
874 | dx - TILE_SIZE * state->w); |
875 | assert(abs(dx) == TILE_SIZE); |
876 | } |
877 | dy = (y1 - y0); |
878 | if (dy != 0 && |
879 | dy != TILE_SIZE * sense) { |
880 | dy = (dy < 0 ? dy + TILE_SIZE * state->h : |
881 | dy - TILE_SIZE * state->h); |
882 | assert(abs(dy) == TILE_SIZE); |
883 | } |
884 | |
885 | c = (animtime / ANIM_TIME); |
886 | if (c < 0.0F) c = 0.0F; |
887 | if (c > 1.0F) c = 1.0F; |
888 | |
889 | x = x0 + (int)(c * dx); |
890 | y = y0 + (int)(c * dy); |
891 | x2 = x1 - dx + (int)(c * dx); |
892 | y2 = y1 - dy + (int)(c * dy); |
893 | } else { |
894 | x = COORD(X(state, i)); |
895 | y = COORD(Y(state, i)); |
896 | x2 = y2 = -1; |
897 | } |
898 | |
899 | draw_tile(fe, state, x, y, t, bgcolour); |
900 | if (x2 != -1 || y2 != -1) |
901 | draw_tile(fe, state, x2, y2, t, bgcolour); |
902 | } |
903 | ds->tiles[i] = t0; |
4efb3868 |
904 | } |
905 | |
906 | unclip(fe); |
907 | |
908 | ds->bgcolour = bgcolour; |
fd1a1a2b |
909 | |
910 | /* |
911 | * Update the status bar. |
912 | */ |
913 | { |
914 | char statusbuf[256]; |
915 | |
d108c342 |
916 | /* |
917 | * Don't show the new status until we're also showing the |
918 | * new _state_ - after the game animation is complete. |
919 | */ |
920 | if (oldstate) |
921 | state = oldstate; |
922 | |
2ac6d24e |
923 | if (state->used_solve) |
924 | sprintf(statusbuf, "Moves since auto-solve: %d", |
925 | state->movecount - state->completed); |
81875211 |
926 | else { |
2ac6d24e |
927 | sprintf(statusbuf, "%sMoves: %d", |
928 | (state->completed ? "COMPLETED! " : ""), |
929 | (state->completed ? state->completed : state->movecount)); |
81875211 |
930 | if (state->movetarget) |
931 | sprintf(statusbuf+strlen(statusbuf), " (target %d)", |
932 | state->movetarget); |
933 | } |
fd1a1a2b |
934 | |
935 | status_bar(fe, statusbuf); |
936 | } |
4efb3868 |
937 | } |
938 | |
be8d5aa1 |
939 | static float game_anim_length(game_state *oldstate, |
e3f21163 |
940 | game_state *newstate, int dir, game_ui *ui) |
4efb3868 |
941 | { |
2ac6d24e |
942 | if ((dir > 0 && newstate->just_used_solve) || |
943 | (dir < 0 && oldstate->just_used_solve)) |
944 | return 0.0F; |
945 | else |
946 | return ANIM_TIME; |
4efb3868 |
947 | } |
948 | |
be8d5aa1 |
949 | static float game_flash_length(game_state *oldstate, |
e3f21163 |
950 | game_state *newstate, int dir, game_ui *ui) |
4efb3868 |
951 | { |
2ac6d24e |
952 | if (!oldstate->completed && newstate->completed && |
953 | !oldstate->used_solve && !newstate->used_solve) |
4efb3868 |
954 | return 2 * FLASH_FRAME; |
955 | else |
956 | return 0.0F; |
957 | } |
fd1a1a2b |
958 | |
be8d5aa1 |
959 | static int game_wants_statusbar(void) |
fd1a1a2b |
960 | { |
961 | return TRUE; |
962 | } |
be8d5aa1 |
963 | |
48dcdd62 |
964 | static int game_timing_state(game_state *state) |
965 | { |
966 | return TRUE; |
967 | } |
968 | |
19ef4855 |
969 | #ifdef COMBINED |
970 | #define thegame sixteen |
971 | #endif |
972 | |
be8d5aa1 |
973 | const struct game thegame = { |
1d228b10 |
974 | "Sixteen", "games.sixteen", |
be8d5aa1 |
975 | default_params, |
976 | game_fetch_preset, |
977 | decode_params, |
978 | encode_params, |
979 | free_params, |
980 | dup_params, |
1d228b10 |
981 | TRUE, game_configure, custom_params, |
be8d5aa1 |
982 | validate_params, |
1185e3c5 |
983 | new_game_desc, |
6f2d8d7c |
984 | game_free_aux_info, |
1185e3c5 |
985 | validate_desc, |
be8d5aa1 |
986 | new_game, |
987 | dup_game, |
988 | free_game, |
2ac6d24e |
989 | TRUE, solve_game, |
af52394e |
990 | TRUE, game_text_format, |
be8d5aa1 |
991 | new_ui, |
992 | free_ui, |
993 | make_move, |
994 | game_size, |
995 | game_colours, |
996 | game_new_drawstate, |
997 | game_free_drawstate, |
998 | game_redraw, |
999 | game_anim_length, |
1000 | game_flash_length, |
1001 | game_wants_statusbar, |
48dcdd62 |
1002 | FALSE, game_timing_state, |
be8d5aa1 |
1003 | }; |