4efb3868 |
1 | /* |
2 | * fifteen.c: standard 15-puzzle. |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <string.h> |
8 | #include <assert.h> |
b0e26073 |
9 | #include <ctype.h> |
4efb3868 |
10 | #include <math.h> |
11 | |
12 | #include "puzzles.h" |
13 | |
1e3e152d |
14 | #define PREFERRED_TILE_SIZE 48 |
15 | #define TILE_SIZE (ds->tilesize) |
4efb3868 |
16 | #define BORDER (TILE_SIZE / 2) |
17 | #define HIGHLIGHT_WIDTH (TILE_SIZE / 20) |
18 | #define COORD(x) ( (x) * TILE_SIZE + BORDER ) |
19 | #define FROMCOORD(x) ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 ) |
20 | |
8c1fd974 |
21 | #define ANIM_TIME 0.13F |
22 | #define FLASH_FRAME 0.13F |
4efb3868 |
23 | |
24 | #define X(state, i) ( (i) % (state)->w ) |
25 | #define Y(state, i) ( (i) / (state)->w ) |
26 | #define C(state, x, y) ( (y) * (state)->w + (x) ) |
27 | |
28 | enum { |
29 | COL_BACKGROUND, |
30 | COL_TEXT, |
31 | COL_HIGHLIGHT, |
32 | COL_LOWLIGHT, |
33 | NCOLOURS |
34 | }; |
35 | |
36 | struct game_params { |
37 | int w, h; |
38 | }; |
39 | |
40 | struct game_state { |
41 | int w, h, n; |
42 | int *tiles; |
43 | int gap_pos; |
44 | int completed; |
2ac6d24e |
45 | int just_used_solve; /* used to suppress undo animation */ |
46 | int used_solve; /* used to suppress completion flash */ |
fd1a1a2b |
47 | int movecount; |
4efb3868 |
48 | }; |
49 | |
be8d5aa1 |
50 | static game_params *default_params(void) |
4efb3868 |
51 | { |
52 | game_params *ret = snew(game_params); |
53 | |
54 | ret->w = ret->h = 4; |
55 | |
56 | return ret; |
57 | } |
58 | |
be8d5aa1 |
59 | static int game_fetch_preset(int i, char **name, game_params **params) |
4efb3868 |
60 | { |
61 | return FALSE; |
62 | } |
63 | |
be8d5aa1 |
64 | static void free_params(game_params *params) |
4efb3868 |
65 | { |
66 | sfree(params); |
67 | } |
68 | |
be8d5aa1 |
69 | static game_params *dup_params(game_params *params) |
4efb3868 |
70 | { |
71 | game_params *ret = snew(game_params); |
72 | *ret = *params; /* structure copy */ |
73 | return ret; |
74 | } |
75 | |
1185e3c5 |
76 | static void decode_params(game_params *ret, char const *string) |
b0e26073 |
77 | { |
b0e26073 |
78 | ret->w = ret->h = atoi(string); |
79 | while (*string && isdigit(*string)) string++; |
80 | if (*string == 'x') { |
81 | string++; |
82 | ret->h = atoi(string); |
83 | } |
b0e26073 |
84 | } |
85 | |
1185e3c5 |
86 | static char *encode_params(game_params *params, int full) |
b0e26073 |
87 | { |
88 | char data[256]; |
89 | |
90 | sprintf(data, "%dx%d", params->w, params->h); |
91 | |
92 | return dupstr(data); |
93 | } |
94 | |
be8d5aa1 |
95 | static config_item *game_configure(game_params *params) |
c8230524 |
96 | { |
97 | config_item *ret; |
98 | char buf[80]; |
99 | |
100 | ret = snewn(3, config_item); |
101 | |
102 | ret[0].name = "Width"; |
95709966 |
103 | ret[0].type = C_STRING; |
c8230524 |
104 | sprintf(buf, "%d", params->w); |
105 | ret[0].sval = dupstr(buf); |
106 | ret[0].ival = 0; |
107 | |
108 | ret[1].name = "Height"; |
95709966 |
109 | ret[1].type = C_STRING; |
c8230524 |
110 | sprintf(buf, "%d", params->h); |
111 | ret[1].sval = dupstr(buf); |
112 | ret[1].ival = 0; |
113 | |
114 | ret[2].name = NULL; |
95709966 |
115 | ret[2].type = C_END; |
c8230524 |
116 | ret[2].sval = NULL; |
117 | ret[2].ival = 0; |
118 | |
119 | return ret; |
120 | } |
121 | |
be8d5aa1 |
122 | static game_params *custom_params(config_item *cfg) |
c8230524 |
123 | { |
124 | game_params *ret = snew(game_params); |
125 | |
126 | ret->w = atoi(cfg[0].sval); |
127 | ret->h = atoi(cfg[1].sval); |
128 | |
129 | return ret; |
130 | } |
131 | |
be8d5aa1 |
132 | static char *validate_params(game_params *params) |
c8230524 |
133 | { |
ab53eb64 |
134 | if (params->w < 2 || params->h < 2) |
c8230524 |
135 | return "Width and height must both be at least two"; |
136 | |
137 | return NULL; |
138 | } |
139 | |
be8d5aa1 |
140 | static int perm_parity(int *perm, int n) |
4efb3868 |
141 | { |
142 | int i, j, ret; |
143 | |
144 | ret = 0; |
145 | |
146 | for (i = 0; i < n-1; i++) |
147 | for (j = i+1; j < n; j++) |
148 | if (perm[i] > perm[j]) |
149 | ret = !ret; |
150 | |
151 | return ret; |
152 | } |
153 | |
1185e3c5 |
154 | static char *new_game_desc(game_params *params, random_state *rs, |
6aa6af4c |
155 | game_aux_info **aux, int interactive) |
4efb3868 |
156 | { |
157 | int gap, n, i, x; |
158 | int x1, x2, p1, p2, parity; |
159 | int *tiles, *used; |
160 | char *ret; |
161 | int retlen; |
162 | |
163 | n = params->w * params->h; |
164 | |
165 | tiles = snewn(n, int); |
166 | used = snewn(n, int); |
167 | |
168 | for (i = 0; i < n; i++) { |
169 | tiles[i] = -1; |
170 | used[i] = FALSE; |
171 | } |
172 | |
48d70ca9 |
173 | gap = random_upto(rs, n); |
4efb3868 |
174 | tiles[gap] = 0; |
175 | used[0] = TRUE; |
176 | |
177 | /* |
178 | * Place everything else except the last two tiles. |
179 | */ |
180 | for (x = 0, i = n-1; i > 2; i--) { |
48d70ca9 |
181 | int k = random_upto(rs, i); |
4efb3868 |
182 | int j; |
183 | |
184 | for (j = 0; j < n; j++) |
185 | if (!used[j] && (k-- == 0)) |
186 | break; |
187 | |
188 | assert(j < n && !used[j]); |
189 | used[j] = TRUE; |
190 | |
191 | while (tiles[x] >= 0) |
192 | x++; |
193 | assert(x < n); |
194 | tiles[x] = j; |
195 | } |
196 | |
197 | /* |
198 | * Find the last two locations, and the last two pieces. |
199 | */ |
200 | while (tiles[x] >= 0) |
201 | x++; |
202 | assert(x < n); |
203 | x1 = x; |
204 | x++; |
205 | while (tiles[x] >= 0) |
206 | x++; |
207 | assert(x < n); |
208 | x2 = x; |
209 | |
210 | for (i = 0; i < n; i++) |
211 | if (!used[i]) |
212 | break; |
213 | p1 = i; |
214 | for (i = p1+1; i < n; i++) |
215 | if (!used[i]) |
216 | break; |
217 | p2 = i; |
218 | |
219 | /* |
220 | * Determine the required parity of the overall permutation. |
221 | * This is the XOR of: |
222 | * |
d3a026ed |
223 | * - The chessboard parity ((x^y)&1) of the gap square. The |
224 | * bottom right counts as even. |
4efb3868 |
225 | * |
226 | * - The parity of n. (The target permutation is 1,...,n-1,0 |
227 | * rather than 0,...,n-1; this is a cyclic permutation of |
228 | * the starting point and hence is odd iff n is even.) |
229 | */ |
d3a026ed |
230 | parity = ((X(params, gap) - (params->w-1)) ^ |
231 | (Y(params, gap) - (params->h-1)) ^ |
232 | (n+1)) & 1; |
4efb3868 |
233 | |
234 | /* |
235 | * Try the last two tiles one way round. If that fails, swap |
236 | * them. |
237 | */ |
238 | tiles[x1] = p1; |
239 | tiles[x2] = p2; |
240 | if (perm_parity(tiles, n) != parity) { |
241 | tiles[x1] = p2; |
242 | tiles[x2] = p1; |
243 | assert(perm_parity(tiles, n) == parity); |
244 | } |
245 | |
246 | /* |
1185e3c5 |
247 | * Now construct the game description, by describing the tile |
248 | * array as a simple sequence of comma-separated integers. |
4efb3868 |
249 | */ |
250 | ret = NULL; |
251 | retlen = 0; |
252 | for (i = 0; i < n; i++) { |
253 | char buf[80]; |
254 | int k; |
255 | |
256 | k = sprintf(buf, "%d,", tiles[i]); |
257 | |
258 | ret = sresize(ret, retlen + k + 1, char); |
259 | strcpy(ret + retlen, buf); |
260 | retlen += k; |
261 | } |
262 | ret[retlen-1] = '\0'; /* delete last comma */ |
263 | |
264 | sfree(tiles); |
265 | sfree(used); |
266 | |
267 | return ret; |
268 | } |
269 | |
2ac6d24e |
270 | static void game_free_aux_info(game_aux_info *aux) |
6f2d8d7c |
271 | { |
272 | assert(!"Shouldn't happen"); |
273 | } |
274 | |
1185e3c5 |
275 | static char *validate_desc(game_params *params, char *desc) |
5928817c |
276 | { |
277 | char *p, *err; |
278 | int i, area; |
279 | int *used; |
280 | |
281 | area = params->w * params->h; |
1185e3c5 |
282 | p = desc; |
5928817c |
283 | err = NULL; |
284 | |
285 | used = snewn(area, int); |
286 | for (i = 0; i < area; i++) |
287 | used[i] = FALSE; |
288 | |
289 | for (i = 0; i < area; i++) { |
290 | char *q = p; |
291 | int n; |
292 | |
293 | if (*p < '0' || *p > '9') { |
294 | err = "Not enough numbers in string"; |
295 | goto leave; |
296 | } |
297 | while (*p >= '0' && *p <= '9') |
298 | p++; |
299 | if (i < area-1 && *p != ',') { |
300 | err = "Expected comma after number"; |
301 | goto leave; |
302 | } |
303 | else if (i == area-1 && *p) { |
304 | err = "Excess junk at end of string"; |
305 | goto leave; |
306 | } |
307 | n = atoi(q); |
308 | if (n < 0 || n >= area) { |
309 | err = "Number out of range"; |
310 | goto leave; |
311 | } |
312 | if (used[n]) { |
313 | err = "Number used twice"; |
314 | goto leave; |
315 | } |
316 | used[n] = TRUE; |
317 | |
318 | if (*p) p++; /* eat comma */ |
319 | } |
320 | |
321 | leave: |
322 | sfree(used); |
323 | return err; |
324 | } |
325 | |
c380832d |
326 | static game_state *new_game(midend_data *me, game_params *params, char *desc) |
4efb3868 |
327 | { |
328 | game_state *state = snew(game_state); |
329 | int i; |
330 | char *p; |
331 | |
332 | state->w = params->w; |
333 | state->h = params->h; |
334 | state->n = params->w * params->h; |
335 | state->tiles = snewn(state->n, int); |
336 | |
337 | state->gap_pos = 0; |
1185e3c5 |
338 | p = desc; |
4efb3868 |
339 | i = 0; |
340 | for (i = 0; i < state->n; i++) { |
341 | assert(*p); |
342 | state->tiles[i] = atoi(p); |
343 | if (state->tiles[i] == 0) |
344 | state->gap_pos = i; |
345 | while (*p && *p != ',') |
346 | p++; |
347 | if (*p) p++; /* eat comma */ |
348 | } |
349 | assert(!*p); |
350 | assert(state->tiles[state->gap_pos] == 0); |
351 | |
fd1a1a2b |
352 | state->completed = state->movecount = 0; |
2ac6d24e |
353 | state->used_solve = state->just_used_solve = FALSE; |
4efb3868 |
354 | |
355 | return state; |
356 | } |
357 | |
be8d5aa1 |
358 | static game_state *dup_game(game_state *state) |
4efb3868 |
359 | { |
360 | game_state *ret = snew(game_state); |
361 | |
362 | ret->w = state->w; |
363 | ret->h = state->h; |
364 | ret->n = state->n; |
365 | ret->tiles = snewn(state->w * state->h, int); |
366 | memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int)); |
367 | ret->gap_pos = state->gap_pos; |
368 | ret->completed = state->completed; |
fd1a1a2b |
369 | ret->movecount = state->movecount; |
2ac6d24e |
370 | ret->used_solve = state->used_solve; |
371 | ret->just_used_solve = state->just_used_solve; |
4efb3868 |
372 | |
373 | return ret; |
374 | } |
375 | |
be8d5aa1 |
376 | static void free_game(game_state *state) |
4efb3868 |
377 | { |
ab53eb64 |
378 | sfree(state->tiles); |
4efb3868 |
379 | sfree(state); |
380 | } |
381 | |
4a29930e |
382 | static game_state *solve_game(game_state *state, game_state *currstate, |
383 | game_aux_info *aux, char **error) |
2ac6d24e |
384 | { |
385 | game_state *ret = dup_game(state); |
386 | int i; |
387 | |
388 | /* |
389 | * Simply replace the grid with a solved one. For this game, |
390 | * this isn't a useful operation for actually telling the user |
391 | * what they should have done, but it is useful for |
392 | * conveniently being able to get hold of a clean state from |
393 | * which to practise manoeuvres. |
394 | */ |
395 | for (i = 0; i < ret->n; i++) |
396 | ret->tiles[i] = (i+1) % ret->n; |
397 | ret->gap_pos = ret->n-1; |
398 | ret->used_solve = ret->just_used_solve = TRUE; |
e73ca44d |
399 | ret->completed = ret->movecount = 1; |
2ac6d24e |
400 | |
401 | return ret; |
402 | } |
403 | |
9b4b03d3 |
404 | static char *game_text_format(game_state *state) |
405 | { |
af52394e |
406 | char *ret, *p, buf[80]; |
407 | int x, y, col, maxlen; |
408 | |
409 | /* |
410 | * First work out how many characters we need to display each |
411 | * number. |
412 | */ |
413 | col = sprintf(buf, "%d", state->n-1); |
414 | |
415 | /* |
416 | * Now we know the exact total size of the grid we're going to |
417 | * produce: it's got h rows, each containing w lots of col, w-1 |
418 | * spaces and a trailing newline. |
419 | */ |
420 | maxlen = state->h * state->w * (col+1); |
421 | |
48a10826 |
422 | ret = snewn(maxlen+1, char); |
af52394e |
423 | p = ret; |
424 | |
425 | for (y = 0; y < state->h; y++) { |
426 | for (x = 0; x < state->w; x++) { |
427 | int v = state->tiles[state->w*y+x]; |
428 | if (v == 0) |
429 | sprintf(buf, "%*s", col, ""); |
430 | else |
431 | sprintf(buf, "%*d", col, v); |
432 | memcpy(p, buf, col); |
433 | p += col; |
434 | if (x+1 == state->w) |
435 | *p++ = '\n'; |
436 | else |
437 | *p++ = ' '; |
438 | } |
439 | } |
440 | |
441 | assert(p - ret == maxlen); |
442 | *p = '\0'; |
443 | return ret; |
9b4b03d3 |
444 | } |
445 | |
be8d5aa1 |
446 | static game_ui *new_ui(game_state *state) |
74a4e547 |
447 | { |
448 | return NULL; |
449 | } |
450 | |
be8d5aa1 |
451 | static void free_ui(game_ui *ui) |
74a4e547 |
452 | { |
453 | } |
454 | |
07dfb697 |
455 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
456 | game_state *newstate) |
457 | { |
458 | } |
459 | |
1e3e152d |
460 | struct game_drawstate { |
461 | int started; |
462 | int w, h, bgcolour; |
463 | int *tiles; |
464 | int tilesize; |
465 | }; |
466 | |
c0361acd |
467 | static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds, |
468 | int x, int y, int button) { |
4efb3868 |
469 | int gx, gy, dx, dy, ux, uy, up, p; |
470 | game_state *ret; |
471 | |
f0ee053c |
472 | button &= ~MOD_MASK; |
473 | |
4efb3868 |
474 | gx = X(from, from->gap_pos); |
475 | gy = Y(from, from->gap_pos); |
476 | |
477 | if (button == CURSOR_RIGHT && gx > 0) |
478 | dx = gx - 1, dy = gy; |
479 | else if (button == CURSOR_LEFT && gx < from->w-1) |
480 | dx = gx + 1, dy = gy; |
481 | else if (button == CURSOR_DOWN && gy > 0) |
482 | dy = gy - 1, dx = gx; |
483 | else if (button == CURSOR_UP && gy < from->h-1) |
484 | dy = gy + 1, dx = gx; |
485 | else if (button == LEFT_BUTTON) { |
486 | dx = FROMCOORD(x); |
487 | dy = FROMCOORD(y); |
488 | if (dx < 0 || dx >= from->w || dy < 0 || dy >= from->h) |
489 | return NULL; /* out of bounds */ |
490 | /* |
491 | * Any click location should be equal to the gap location |
492 | * in _precisely_ one coordinate. |
493 | */ |
494 | if ((dx == gx && dy == gy) || (dx != gx && dy != gy)) |
495 | return NULL; |
496 | } else |
497 | return NULL; /* no move */ |
498 | |
499 | /* |
500 | * Find the unit displacement from the original gap |
501 | * position towards this one. |
502 | */ |
503 | ux = (dx < gx ? -1 : dx > gx ? +1 : 0); |
504 | uy = (dy < gy ? -1 : dy > gy ? +1 : 0); |
505 | up = C(from, ux, uy); |
506 | |
507 | ret = dup_game(from); |
2ac6d24e |
508 | ret->just_used_solve = FALSE; /* zero this in a hurry */ |
4efb3868 |
509 | |
510 | ret->gap_pos = C(from, dx, dy); |
511 | assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n); |
512 | |
513 | ret->tiles[ret->gap_pos] = 0; |
514 | |
515 | for (p = from->gap_pos; p != ret->gap_pos; p += up) { |
516 | assert(p >= 0 && p < from->n); |
517 | ret->tiles[p] = from->tiles[p + up]; |
fd1a1a2b |
518 | ret->movecount++; |
4efb3868 |
519 | } |
520 | |
521 | /* |
522 | * See if the game has been completed. |
523 | */ |
524 | if (!ret->completed) { |
fd1a1a2b |
525 | ret->completed = ret->movecount; |
4efb3868 |
526 | for (p = 0; p < ret->n; p++) |
527 | if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0)) |
fd1a1a2b |
528 | ret->completed = 0; |
4efb3868 |
529 | } |
530 | |
531 | return ret; |
532 | } |
533 | |
534 | /* ---------------------------------------------------------------------- |
535 | * Drawing routines. |
536 | */ |
537 | |
1e3e152d |
538 | static void game_size(game_params *params, game_drawstate *ds, |
539 | int *x, int *y, int expand) |
4efb3868 |
540 | { |
1e3e152d |
541 | int tsx, tsy, ts; |
542 | /* |
543 | * Each window dimension equals the tile size times one more |
544 | * than the grid dimension (the border is half the width of the |
545 | * tiles). |
546 | */ |
547 | tsx = *x / (params->w + 1); |
548 | tsy = *y / (params->h + 1); |
549 | ts = min(tsx, tsy); |
550 | if (expand) |
551 | ds->tilesize = ts; |
552 | else |
553 | ds->tilesize = min(ts, PREFERRED_TILE_SIZE); |
554 | |
4efb3868 |
555 | *x = TILE_SIZE * params->w + 2 * BORDER; |
556 | *y = TILE_SIZE * params->h + 2 * BORDER; |
557 | } |
558 | |
be8d5aa1 |
559 | static float *game_colours(frontend *fe, game_state *state, int *ncolours) |
4efb3868 |
560 | { |
561 | float *ret = snewn(3 * NCOLOURS, float); |
562 | int i; |
563 | float max; |
564 | |
565 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
566 | |
567 | /* |
568 | * Drop the background colour so that the highlight is |
569 | * noticeably brighter than it while still being under 1. |
570 | */ |
571 | max = ret[COL_BACKGROUND*3]; |
572 | for (i = 1; i < 3; i++) |
573 | if (ret[COL_BACKGROUND*3+i] > max) |
574 | max = ret[COL_BACKGROUND*3+i]; |
575 | if (max * 1.2F > 1.0F) { |
576 | for (i = 0; i < 3; i++) |
577 | ret[COL_BACKGROUND*3+i] /= (max * 1.2F); |
578 | } |
579 | |
580 | for (i = 0; i < 3; i++) { |
581 | ret[COL_HIGHLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 1.2F; |
582 | ret[COL_LOWLIGHT * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.8F; |
583 | ret[COL_TEXT * 3 + i] = 0.0; |
584 | } |
585 | |
586 | *ncolours = NCOLOURS; |
587 | return ret; |
588 | } |
589 | |
be8d5aa1 |
590 | static game_drawstate *game_new_drawstate(game_state *state) |
4efb3868 |
591 | { |
592 | struct game_drawstate *ds = snew(struct game_drawstate); |
593 | int i; |
594 | |
595 | ds->started = FALSE; |
596 | ds->w = state->w; |
597 | ds->h = state->h; |
598 | ds->bgcolour = COL_BACKGROUND; |
599 | ds->tiles = snewn(ds->w*ds->h, int); |
1e3e152d |
600 | ds->tilesize = 0; /* haven't decided yet */ |
4efb3868 |
601 | for (i = 0; i < ds->w*ds->h; i++) |
602 | ds->tiles[i] = -1; |
603 | |
604 | return ds; |
605 | } |
606 | |
be8d5aa1 |
607 | static void game_free_drawstate(game_drawstate *ds) |
4efb3868 |
608 | { |
609 | sfree(ds->tiles); |
610 | sfree(ds); |
611 | } |
612 | |
1e3e152d |
613 | static void draw_tile(frontend *fe, game_drawstate *ds, game_state *state, |
614 | int x, int y, int tile, int flash_colour) |
4efb3868 |
615 | { |
616 | if (tile == 0) { |
617 | draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE, |
618 | flash_colour); |
619 | } else { |
620 | int coords[6]; |
621 | char str[40]; |
622 | |
623 | coords[0] = x + TILE_SIZE - 1; |
624 | coords[1] = y + TILE_SIZE - 1; |
625 | coords[2] = x + TILE_SIZE - 1; |
626 | coords[3] = y; |
627 | coords[4] = x; |
628 | coords[5] = y + TILE_SIZE - 1; |
629 | draw_polygon(fe, coords, 3, TRUE, COL_LOWLIGHT); |
630 | draw_polygon(fe, coords, 3, FALSE, COL_LOWLIGHT); |
631 | |
632 | coords[0] = x; |
633 | coords[1] = y; |
634 | draw_polygon(fe, coords, 3, TRUE, COL_HIGHLIGHT); |
635 | draw_polygon(fe, coords, 3, FALSE, COL_HIGHLIGHT); |
636 | |
637 | draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH, |
638 | TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH, |
639 | flash_colour); |
640 | |
641 | sprintf(str, "%d", tile); |
642 | draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2, |
643 | FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE, |
644 | COL_TEXT, str); |
645 | } |
646 | draw_update(fe, x, y, TILE_SIZE, TILE_SIZE); |
647 | } |
648 | |
be8d5aa1 |
649 | static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, |
c822de4a |
650 | game_state *state, int dir, game_ui *ui, |
74a4e547 |
651 | float animtime, float flashtime) |
4efb3868 |
652 | { |
653 | int i, pass, bgcolour; |
654 | |
655 | if (flashtime > 0) { |
656 | int frame = (int)(flashtime / FLASH_FRAME); |
657 | bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT); |
658 | } else |
659 | bgcolour = COL_BACKGROUND; |
660 | |
661 | if (!ds->started) { |
19f24306 |
662 | int coords[10]; |
4efb3868 |
663 | |
664 | draw_rect(fe, 0, 0, |
665 | TILE_SIZE * state->w + 2 * BORDER, |
666 | TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND); |
667 | draw_update(fe, 0, 0, |
668 | TILE_SIZE * state->w + 2 * BORDER, |
669 | TILE_SIZE * state->h + 2 * BORDER); |
670 | |
671 | /* |
672 | * Recessed area containing the whole puzzle. |
673 | */ |
674 | coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1; |
675 | coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1; |
676 | coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1; |
677 | coords[3] = COORD(0) - HIGHLIGHT_WIDTH; |
19f24306 |
678 | coords[4] = coords[2] - TILE_SIZE; |
679 | coords[5] = coords[3] + TILE_SIZE; |
680 | coords[8] = COORD(0) - HIGHLIGHT_WIDTH; |
681 | coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1; |
682 | coords[6] = coords[8] + TILE_SIZE; |
683 | coords[7] = coords[9] - TILE_SIZE; |
684 | draw_polygon(fe, coords, 5, TRUE, COL_HIGHLIGHT); |
685 | draw_polygon(fe, coords, 5, FALSE, COL_HIGHLIGHT); |
4efb3868 |
686 | |
687 | coords[1] = COORD(0) - HIGHLIGHT_WIDTH; |
688 | coords[0] = COORD(0) - HIGHLIGHT_WIDTH; |
19f24306 |
689 | draw_polygon(fe, coords, 5, TRUE, COL_LOWLIGHT); |
690 | draw_polygon(fe, coords, 5, FALSE, COL_LOWLIGHT); |
4efb3868 |
691 | |
692 | ds->started = TRUE; |
693 | } |
694 | |
695 | /* |
696 | * Now draw each tile. We do this in two passes to make |
697 | * animation easy. |
698 | */ |
699 | for (pass = 0; pass < 2; pass++) { |
700 | for (i = 0; i < state->n; i++) { |
701 | int t, t0; |
702 | /* |
703 | * Figure out what should be displayed at this |
704 | * location. It's either a simple tile, or it's a |
705 | * transition between two tiles (in which case we say |
706 | * -1 because it must always be drawn). |
707 | */ |
708 | |
709 | if (oldstate && oldstate->tiles[i] != state->tiles[i]) |
710 | t = -1; |
711 | else |
712 | t = state->tiles[i]; |
713 | |
714 | t0 = t; |
715 | |
716 | if (ds->bgcolour != bgcolour || /* always redraw when flashing */ |
717 | ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) { |
718 | int x, y; |
719 | |
720 | /* |
721 | * Figure out what to _actually_ draw, and where to |
722 | * draw it. |
723 | */ |
724 | if (t == -1) { |
725 | int x0, y0, x1, y1; |
726 | int j; |
727 | |
728 | /* |
729 | * On the first pass, just blank the tile. |
730 | */ |
731 | if (pass == 0) { |
732 | x = COORD(X(state, i)); |
733 | y = COORD(Y(state, i)); |
734 | t = 0; |
735 | } else { |
736 | float c; |
737 | |
738 | t = state->tiles[i]; |
739 | |
740 | /* |
741 | * Don't bother moving the gap; just don't |
742 | * draw it. |
743 | */ |
744 | if (t == 0) |
745 | continue; |
746 | |
747 | /* |
748 | * Find the coordinates of this tile in the old and |
749 | * new states. |
750 | */ |
751 | x1 = COORD(X(state, i)); |
752 | y1 = COORD(Y(state, i)); |
753 | for (j = 0; j < oldstate->n; j++) |
754 | if (oldstate->tiles[j] == state->tiles[i]) |
755 | break; |
756 | assert(j < oldstate->n); |
757 | x0 = COORD(X(state, j)); |
758 | y0 = COORD(Y(state, j)); |
759 | |
760 | c = (animtime / ANIM_TIME); |
761 | if (c < 0.0F) c = 0.0F; |
762 | if (c > 1.0F) c = 1.0F; |
763 | |
764 | x = x0 + (int)(c * (x1 - x0)); |
765 | y = y0 + (int)(c * (y1 - y0)); |
766 | } |
767 | |
768 | } else { |
769 | if (pass == 0) |
770 | continue; |
771 | x = COORD(X(state, i)); |
772 | y = COORD(Y(state, i)); |
773 | } |
774 | |
1e3e152d |
775 | draw_tile(fe, ds, state, x, y, t, bgcolour); |
4efb3868 |
776 | } |
777 | ds->tiles[i] = t0; |
778 | } |
779 | } |
780 | ds->bgcolour = bgcolour; |
fd1a1a2b |
781 | |
782 | /* |
783 | * Update the status bar. |
784 | */ |
785 | { |
786 | char statusbuf[256]; |
787 | |
d108c342 |
788 | /* |
789 | * Don't show the new status until we're also showing the |
790 | * new _state_ - after the game animation is complete. |
791 | */ |
792 | if (oldstate) |
793 | state = oldstate; |
794 | |
2ac6d24e |
795 | if (state->used_solve) |
796 | sprintf(statusbuf, "Moves since auto-solve: %d", |
797 | state->movecount - state->completed); |
798 | else |
799 | sprintf(statusbuf, "%sMoves: %d", |
800 | (state->completed ? "COMPLETED! " : ""), |
801 | (state->completed ? state->completed : state->movecount)); |
fd1a1a2b |
802 | |
803 | status_bar(fe, statusbuf); |
804 | } |
4efb3868 |
805 | } |
806 | |
be8d5aa1 |
807 | static float game_anim_length(game_state *oldstate, |
e3f21163 |
808 | game_state *newstate, int dir, game_ui *ui) |
4efb3868 |
809 | { |
2ac6d24e |
810 | if ((dir > 0 && newstate->just_used_solve) || |
811 | (dir < 0 && oldstate->just_used_solve)) |
812 | return 0.0F; |
813 | else |
814 | return ANIM_TIME; |
4efb3868 |
815 | } |
816 | |
be8d5aa1 |
817 | static float game_flash_length(game_state *oldstate, |
e3f21163 |
818 | game_state *newstate, int dir, game_ui *ui) |
4efb3868 |
819 | { |
2ac6d24e |
820 | if (!oldstate->completed && newstate->completed && |
821 | !oldstate->used_solve && !newstate->used_solve) |
4efb3868 |
822 | return 2 * FLASH_FRAME; |
823 | else |
824 | return 0.0F; |
825 | } |
fd1a1a2b |
826 | |
be8d5aa1 |
827 | static int game_wants_statusbar(void) |
fd1a1a2b |
828 | { |
829 | return TRUE; |
830 | } |
be8d5aa1 |
831 | |
48dcdd62 |
832 | static int game_timing_state(game_state *state) |
833 | { |
834 | return TRUE; |
835 | } |
836 | |
be8d5aa1 |
837 | #ifdef COMBINED |
838 | #define thegame fifteen |
839 | #endif |
840 | |
841 | const struct game thegame = { |
1d228b10 |
842 | "Fifteen", "games.fifteen", |
be8d5aa1 |
843 | default_params, |
844 | game_fetch_preset, |
845 | decode_params, |
846 | encode_params, |
847 | free_params, |
848 | dup_params, |
1d228b10 |
849 | TRUE, game_configure, custom_params, |
be8d5aa1 |
850 | validate_params, |
1185e3c5 |
851 | new_game_desc, |
6f2d8d7c |
852 | game_free_aux_info, |
1185e3c5 |
853 | validate_desc, |
be8d5aa1 |
854 | new_game, |
855 | dup_game, |
856 | free_game, |
2ac6d24e |
857 | TRUE, solve_game, |
af52394e |
858 | TRUE, game_text_format, |
be8d5aa1 |
859 | new_ui, |
860 | free_ui, |
07dfb697 |
861 | game_changed_state, |
be8d5aa1 |
862 | make_move, |
863 | game_size, |
864 | game_colours, |
865 | game_new_drawstate, |
866 | game_free_drawstate, |
867 | game_redraw, |
868 | game_anim_length, |
869 | game_flash_length, |
870 | game_wants_statusbar, |
48dcdd62 |
871 | FALSE, game_timing_state, |
93b1da3d |
872 | 0, /* mouse_priorities */ |
be8d5aa1 |
873 | }; |