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