Refactoring from James H: the highlight and lowlight colour setup
[sgt/puzzles] / fifteen.c
CommitLineData
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
28enum {
29 COL_BACKGROUND,
30 COL_TEXT,
31 COL_HIGHLIGHT,
32 COL_LOWLIGHT,
33 NCOLOURS
34};
35
36struct game_params {
37 int w, h;
38};
39
40struct 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 50static 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 59static int game_fetch_preset(int i, char **name, game_params **params)
4efb3868 60{
61 return FALSE;
62}
63
be8d5aa1 64static void free_params(game_params *params)
4efb3868 65{
66 sfree(params);
67}
68
be8d5aa1 69static 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 76static 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 86static 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 95static 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 122static 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
3ff276f2 132static char *validate_params(game_params *params, int full)
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 140static 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 154static char *new_game_desc(game_params *params, random_state *rs,
c566778e 155 char **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
1185e3c5 270static char *validate_desc(game_params *params, char *desc)
5928817c 271{
272 char *p, *err;
273 int i, area;
274 int *used;
275
276 area = params->w * params->h;
1185e3c5 277 p = desc;
5928817c 278 err = NULL;
279
280 used = snewn(area, int);
281 for (i = 0; i < area; i++)
282 used[i] = FALSE;
283
284 for (i = 0; i < area; i++) {
285 char *q = p;
286 int n;
287
288 if (*p < '0' || *p > '9') {
289 err = "Not enough numbers in string";
290 goto leave;
291 }
292 while (*p >= '0' && *p <= '9')
293 p++;
294 if (i < area-1 && *p != ',') {
295 err = "Expected comma after number";
296 goto leave;
297 }
298 else if (i == area-1 && *p) {
299 err = "Excess junk at end of string";
300 goto leave;
301 }
302 n = atoi(q);
303 if (n < 0 || n >= area) {
304 err = "Number out of range";
305 goto leave;
306 }
307 if (used[n]) {
308 err = "Number used twice";
309 goto leave;
310 }
311 used[n] = TRUE;
312
313 if (*p) p++; /* eat comma */
314 }
315
316 leave:
317 sfree(used);
318 return err;
319}
320
c380832d 321static game_state *new_game(midend_data *me, game_params *params, char *desc)
4efb3868 322{
323 game_state *state = snew(game_state);
324 int i;
325 char *p;
326
327 state->w = params->w;
328 state->h = params->h;
329 state->n = params->w * params->h;
330 state->tiles = snewn(state->n, int);
331
332 state->gap_pos = 0;
1185e3c5 333 p = desc;
4efb3868 334 i = 0;
335 for (i = 0; i < state->n; i++) {
336 assert(*p);
337 state->tiles[i] = atoi(p);
338 if (state->tiles[i] == 0)
339 state->gap_pos = i;
340 while (*p && *p != ',')
341 p++;
342 if (*p) p++; /* eat comma */
343 }
344 assert(!*p);
345 assert(state->tiles[state->gap_pos] == 0);
346
fd1a1a2b 347 state->completed = state->movecount = 0;
2ac6d24e 348 state->used_solve = state->just_used_solve = FALSE;
4efb3868 349
350 return state;
351}
352
be8d5aa1 353static game_state *dup_game(game_state *state)
4efb3868 354{
355 game_state *ret = snew(game_state);
356
357 ret->w = state->w;
358 ret->h = state->h;
359 ret->n = state->n;
360 ret->tiles = snewn(state->w * state->h, int);
361 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
362 ret->gap_pos = state->gap_pos;
363 ret->completed = state->completed;
fd1a1a2b 364 ret->movecount = state->movecount;
2ac6d24e 365 ret->used_solve = state->used_solve;
366 ret->just_used_solve = state->just_used_solve;
4efb3868 367
368 return ret;
369}
370
be8d5aa1 371static void free_game(game_state *state)
4efb3868 372{
ab53eb64 373 sfree(state->tiles);
4efb3868 374 sfree(state);
375}
376
df11cd4e 377static char *solve_game(game_state *state, game_state *currstate,
c566778e 378 char *aux, char **error)
2ac6d24e 379{
df11cd4e 380 return dupstr("S");
2ac6d24e 381}
382
9b4b03d3 383static char *game_text_format(game_state *state)
384{
af52394e 385 char *ret, *p, buf[80];
386 int x, y, col, maxlen;
387
388 /*
389 * First work out how many characters we need to display each
390 * number.
391 */
392 col = sprintf(buf, "%d", state->n-1);
393
394 /*
395 * Now we know the exact total size of the grid we're going to
396 * produce: it's got h rows, each containing w lots of col, w-1
397 * spaces and a trailing newline.
398 */
399 maxlen = state->h * state->w * (col+1);
400
48a10826 401 ret = snewn(maxlen+1, char);
af52394e 402 p = ret;
403
404 for (y = 0; y < state->h; y++) {
405 for (x = 0; x < state->w; x++) {
406 int v = state->tiles[state->w*y+x];
407 if (v == 0)
408 sprintf(buf, "%*s", col, "");
409 else
410 sprintf(buf, "%*d", col, v);
411 memcpy(p, buf, col);
412 p += col;
413 if (x+1 == state->w)
414 *p++ = '\n';
415 else
416 *p++ = ' ';
417 }
418 }
419
420 assert(p - ret == maxlen);
421 *p = '\0';
422 return ret;
9b4b03d3 423}
424
be8d5aa1 425static game_ui *new_ui(game_state *state)
74a4e547 426{
427 return NULL;
428}
429
be8d5aa1 430static void free_ui(game_ui *ui)
74a4e547 431{
432}
433
844f605f 434static char *encode_ui(game_ui *ui)
ae8290c6 435{
436 return NULL;
437}
438
844f605f 439static void decode_ui(game_ui *ui, char *encoding)
ae8290c6 440{
441}
442
07dfb697 443static void game_changed_state(game_ui *ui, game_state *oldstate,
444 game_state *newstate)
445{
446}
447
1e3e152d 448struct game_drawstate {
449 int started;
450 int w, h, bgcolour;
451 int *tiles;
452 int tilesize;
453};
454
df11cd4e 455static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
456 int x, int y, int button)
457{
458 int gx, gy, dx, dy;
459 char buf[80];
4efb3868 460
f0ee053c 461 button &= ~MOD_MASK;
462
df11cd4e 463 gx = X(state, state->gap_pos);
464 gy = Y(state, state->gap_pos);
4efb3868 465
466 if (button == CURSOR_RIGHT && gx > 0)
467 dx = gx - 1, dy = gy;
df11cd4e 468 else if (button == CURSOR_LEFT && gx < state->w-1)
4efb3868 469 dx = gx + 1, dy = gy;
470 else if (button == CURSOR_DOWN && gy > 0)
471 dy = gy - 1, dx = gx;
df11cd4e 472 else if (button == CURSOR_UP && gy < state->h-1)
4efb3868 473 dy = gy + 1, dx = gx;
474 else if (button == LEFT_BUTTON) {
475 dx = FROMCOORD(x);
476 dy = FROMCOORD(y);
df11cd4e 477 if (dx < 0 || dx >= state->w || dy < 0 || dy >= state->h)
4efb3868 478 return NULL; /* out of bounds */
479 /*
480 * Any click location should be equal to the gap location
481 * in _precisely_ one coordinate.
482 */
483 if ((dx == gx && dy == gy) || (dx != gx && dy != gy))
484 return NULL;
485 } else
486 return NULL; /* no move */
487
df11cd4e 488 sprintf(buf, "M%d,%d", dx, dy);
489 return dupstr(buf);
490}
491
492static game_state *execute_move(game_state *from, char *move)
493{
494 int gx, gy, dx, dy, ux, uy, up, p;
495 game_state *ret;
496
497 if (!strcmp(move, "S")) {
498 int i;
499
500 ret = dup_game(from);
501
502 /*
503 * Simply replace the grid with a solved one. For this game,
504 * this isn't a useful operation for actually telling the user
505 * what they should have done, but it is useful for
506 * conveniently being able to get hold of a clean state from
507 * which to practise manoeuvres.
508 */
509 for (i = 0; i < ret->n; i++)
510 ret->tiles[i] = (i+1) % ret->n;
511 ret->gap_pos = ret->n-1;
512 ret->used_solve = ret->just_used_solve = TRUE;
513 ret->completed = ret->movecount = 1;
514
515 return ret;
516 }
517
518 gx = X(from, from->gap_pos);
519 gy = Y(from, from->gap_pos);
520
521 if (move[0] != 'M' ||
522 sscanf(move+1, "%d,%d", &dx, &dy) != 2 ||
523 (dx == gx && dy == gy) || (dx != gx && dy != gy) ||
524 dx < 0 || dx >= from->w || dy < 0 || dy >= from->h)
525 return NULL;
526
4efb3868 527 /*
528 * Find the unit displacement from the original gap
529 * position towards this one.
530 */
531 ux = (dx < gx ? -1 : dx > gx ? +1 : 0);
532 uy = (dy < gy ? -1 : dy > gy ? +1 : 0);
533 up = C(from, ux, uy);
534
535 ret = dup_game(from);
2ac6d24e 536 ret->just_used_solve = FALSE; /* zero this in a hurry */
4efb3868 537
538 ret->gap_pos = C(from, dx, dy);
539 assert(ret->gap_pos >= 0 && ret->gap_pos < ret->n);
540
541 ret->tiles[ret->gap_pos] = 0;
542
543 for (p = from->gap_pos; p != ret->gap_pos; p += up) {
544 assert(p >= 0 && p < from->n);
545 ret->tiles[p] = from->tiles[p + up];
fd1a1a2b 546 ret->movecount++;
4efb3868 547 }
548
549 /*
550 * See if the game has been completed.
551 */
552 if (!ret->completed) {
fd1a1a2b 553 ret->completed = ret->movecount;
4efb3868 554 for (p = 0; p < ret->n; p++)
555 if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0))
fd1a1a2b 556 ret->completed = 0;
4efb3868 557 }
558
559 return ret;
560}
561
562/* ----------------------------------------------------------------------
563 * Drawing routines.
564 */
565
1f3ee4ee 566static void game_compute_size(game_params *params, int tilesize,
567 int *x, int *y)
4efb3868 568{
1f3ee4ee 569 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
570 struct { int tilesize; } ads, *ds = &ads;
571 ads.tilesize = tilesize;
1e3e152d 572
4efb3868 573 *x = TILE_SIZE * params->w + 2 * BORDER;
574 *y = TILE_SIZE * params->h + 2 * BORDER;
575}
576
1f3ee4ee 577static void game_set_size(game_drawstate *ds, game_params *params,
578 int tilesize)
579{
580 ds->tilesize = tilesize;
581}
582
be8d5aa1 583static float *game_colours(frontend *fe, game_state *state, int *ncolours)
4efb3868 584{
585 float *ret = snewn(3 * NCOLOURS, float);
586 int i;
4efb3868 587
937a9eff 588 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
4efb3868 589
937a9eff 590 for (i = 0; i < 3; i++)
4efb3868 591 ret[COL_TEXT * 3 + i] = 0.0;
4efb3868 592
593 *ncolours = NCOLOURS;
594 return ret;
595}
596
be8d5aa1 597static game_drawstate *game_new_drawstate(game_state *state)
4efb3868 598{
599 struct game_drawstate *ds = snew(struct game_drawstate);
600 int i;
601
602 ds->started = FALSE;
603 ds->w = state->w;
604 ds->h = state->h;
605 ds->bgcolour = COL_BACKGROUND;
606 ds->tiles = snewn(ds->w*ds->h, int);
1e3e152d 607 ds->tilesize = 0; /* haven't decided yet */
4efb3868 608 for (i = 0; i < ds->w*ds->h; i++)
609 ds->tiles[i] = -1;
610
611 return ds;
612}
613
be8d5aa1 614static void game_free_drawstate(game_drawstate *ds)
4efb3868 615{
616 sfree(ds->tiles);
617 sfree(ds);
618}
619
1e3e152d 620static void draw_tile(frontend *fe, game_drawstate *ds, game_state *state,
621 int x, int y, int tile, int flash_colour)
4efb3868 622{
623 if (tile == 0) {
624 draw_rect(fe, x, y, TILE_SIZE, TILE_SIZE,
625 flash_colour);
626 } else {
627 int coords[6];
628 char str[40];
629
630 coords[0] = x + TILE_SIZE - 1;
631 coords[1] = y + TILE_SIZE - 1;
632 coords[2] = x + TILE_SIZE - 1;
633 coords[3] = y;
634 coords[4] = x;
635 coords[5] = y + TILE_SIZE - 1;
28b5987d 636 draw_polygon(fe, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT);
4efb3868 637
638 coords[0] = x;
639 coords[1] = y;
28b5987d 640 draw_polygon(fe, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
4efb3868 641
642 draw_rect(fe, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
643 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
644 flash_colour);
645
646 sprintf(str, "%d", tile);
647 draw_text(fe, x + TILE_SIZE/2, y + TILE_SIZE/2,
648 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
649 COL_TEXT, str);
650 }
651 draw_update(fe, x, y, TILE_SIZE, TILE_SIZE);
652}
653
be8d5aa1 654static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
c822de4a 655 game_state *state, int dir, game_ui *ui,
74a4e547 656 float animtime, float flashtime)
4efb3868 657{
658 int i, pass, bgcolour;
659
660 if (flashtime > 0) {
661 int frame = (int)(flashtime / FLASH_FRAME);
662 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
663 } else
664 bgcolour = COL_BACKGROUND;
665
666 if (!ds->started) {
19f24306 667 int coords[10];
4efb3868 668
669 draw_rect(fe, 0, 0,
670 TILE_SIZE * state->w + 2 * BORDER,
671 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
672 draw_update(fe, 0, 0,
673 TILE_SIZE * state->w + 2 * BORDER,
674 TILE_SIZE * state->h + 2 * BORDER);
675
676 /*
677 * Recessed area containing the whole puzzle.
678 */
679 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
680 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
681 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
682 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
19f24306 683 coords[4] = coords[2] - TILE_SIZE;
684 coords[5] = coords[3] + TILE_SIZE;
685 coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
686 coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
687 coords[6] = coords[8] + TILE_SIZE;
688 coords[7] = coords[9] - TILE_SIZE;
28b5987d 689 draw_polygon(fe, coords, 5, COL_HIGHLIGHT, COL_HIGHLIGHT);
4efb3868 690
691 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
692 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
28b5987d 693 draw_polygon(fe, coords, 5, COL_LOWLIGHT, COL_LOWLIGHT);
4efb3868 694
695 ds->started = TRUE;
696 }
697
698 /*
699 * Now draw each tile. We do this in two passes to make
700 * animation easy.
701 */
702 for (pass = 0; pass < 2; pass++) {
703 for (i = 0; i < state->n; i++) {
704 int t, t0;
705 /*
706 * Figure out what should be displayed at this
707 * location. It's either a simple tile, or it's a
708 * transition between two tiles (in which case we say
709 * -1 because it must always be drawn).
710 */
711
712 if (oldstate && oldstate->tiles[i] != state->tiles[i])
713 t = -1;
714 else
715 t = state->tiles[i];
716
717 t0 = t;
718
719 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
720 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
721 int x, y;
722
723 /*
724 * Figure out what to _actually_ draw, and where to
725 * draw it.
726 */
727 if (t == -1) {
728 int x0, y0, x1, y1;
729 int j;
730
731 /*
732 * On the first pass, just blank the tile.
733 */
734 if (pass == 0) {
735 x = COORD(X(state, i));
736 y = COORD(Y(state, i));
737 t = 0;
738 } else {
739 float c;
740
741 t = state->tiles[i];
742
743 /*
744 * Don't bother moving the gap; just don't
745 * draw it.
746 */
747 if (t == 0)
748 continue;
749
750 /*
751 * Find the coordinates of this tile in the old and
752 * new states.
753 */
754 x1 = COORD(X(state, i));
755 y1 = COORD(Y(state, i));
756 for (j = 0; j < oldstate->n; j++)
757 if (oldstate->tiles[j] == state->tiles[i])
758 break;
759 assert(j < oldstate->n);
760 x0 = COORD(X(state, j));
761 y0 = COORD(Y(state, j));
762
763 c = (animtime / ANIM_TIME);
764 if (c < 0.0F) c = 0.0F;
765 if (c > 1.0F) c = 1.0F;
766
767 x = x0 + (int)(c * (x1 - x0));
768 y = y0 + (int)(c * (y1 - y0));
769 }
770
771 } else {
772 if (pass == 0)
773 continue;
774 x = COORD(X(state, i));
775 y = COORD(Y(state, i));
776 }
777
1e3e152d 778 draw_tile(fe, ds, state, x, y, t, bgcolour);
4efb3868 779 }
780 ds->tiles[i] = t0;
781 }
782 }
783 ds->bgcolour = bgcolour;
fd1a1a2b 784
785 /*
786 * Update the status bar.
787 */
788 {
789 char statusbuf[256];
790
d108c342 791 /*
792 * Don't show the new status until we're also showing the
793 * new _state_ - after the game animation is complete.
794 */
795 if (oldstate)
796 state = oldstate;
797
2ac6d24e 798 if (state->used_solve)
799 sprintf(statusbuf, "Moves since auto-solve: %d",
800 state->movecount - state->completed);
801 else
802 sprintf(statusbuf, "%sMoves: %d",
803 (state->completed ? "COMPLETED! " : ""),
804 (state->completed ? state->completed : state->movecount));
fd1a1a2b 805
806 status_bar(fe, statusbuf);
807 }
4efb3868 808}
809
be8d5aa1 810static float game_anim_length(game_state *oldstate,
e3f21163 811 game_state *newstate, int dir, game_ui *ui)
4efb3868 812{
2ac6d24e 813 if ((dir > 0 && newstate->just_used_solve) ||
814 (dir < 0 && oldstate->just_used_solve))
815 return 0.0F;
816 else
817 return ANIM_TIME;
4efb3868 818}
819
be8d5aa1 820static float game_flash_length(game_state *oldstate,
e3f21163 821 game_state *newstate, int dir, game_ui *ui)
4efb3868 822{
2ac6d24e 823 if (!oldstate->completed && newstate->completed &&
824 !oldstate->used_solve && !newstate->used_solve)
4efb3868 825 return 2 * FLASH_FRAME;
826 else
827 return 0.0F;
828}
fd1a1a2b 829
be8d5aa1 830static int game_wants_statusbar(void)
fd1a1a2b 831{
832 return TRUE;
833}
be8d5aa1 834
48dcdd62 835static int game_timing_state(game_state *state)
836{
837 return TRUE;
838}
839
be8d5aa1 840#ifdef COMBINED
841#define thegame fifteen
842#endif
843
844const struct game thegame = {
1d228b10 845 "Fifteen", "games.fifteen",
be8d5aa1 846 default_params,
847 game_fetch_preset,
848 decode_params,
849 encode_params,
850 free_params,
851 dup_params,
1d228b10 852 TRUE, game_configure, custom_params,
be8d5aa1 853 validate_params,
1185e3c5 854 new_game_desc,
1185e3c5 855 validate_desc,
be8d5aa1 856 new_game,
857 dup_game,
858 free_game,
2ac6d24e 859 TRUE, solve_game,
af52394e 860 TRUE, game_text_format,
be8d5aa1 861 new_ui,
862 free_ui,
ae8290c6 863 encode_ui,
864 decode_ui,
07dfb697 865 game_changed_state,
df11cd4e 866 interpret_move,
867 execute_move,
1f3ee4ee 868 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
be8d5aa1 869 game_colours,
870 game_new_drawstate,
871 game_free_drawstate,
872 game_redraw,
873 game_anim_length,
874 game_flash_length,
875 game_wants_statusbar,
48dcdd62 876 FALSE, game_timing_state,
93b1da3d 877 0, /* mouse_priorities */
be8d5aa1 878};