Make indentation consistent. (Somehow I forgot to do this before I
[sgt/puzzles] / sixteen.c
CommitLineData
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
1e3e152d 16#define PREFERRED_TILE_SIZE 48
17#define TILE_SIZE (ds->tilesize)
18#define BORDER TILE_SIZE
4efb3868 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
8c1fd974 23#define ANIM_TIME 0.13F
24#define FLASH_FRAME 0.13F
4efb3868 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
30enum {
31 COL_BACKGROUND,
32 COL_TEXT,
33 COL_HIGHLIGHT,
34 COL_LOWLIGHT,
35 NCOLOURS
36};
37
38struct game_params {
39 int w, h;
81875211 40 int movetarget;
4efb3868 41};
42
43struct game_state {
44 int w, h, n;
45 int *tiles;
46 int completed;
2ac6d24e 47 int used_solve; /* used to suppress completion flash */
81875211 48 int movecount, movetarget;
c8230524 49 int last_movement_sense;
4efb3868 50};
51
be8d5aa1 52static 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 62static 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 86static void free_params(game_params *params)
4efb3868 87{
88 sfree(params);
89}
90
be8d5aa1 91static 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 98static void decode_params(game_params *ret, char const *string)
b0e26073 99{
b0e26073 100 ret->w = ret->h = atoi(string);
1185e3c5 101 ret->movetarget = 0;
89167dad 102 while (*string && isdigit((unsigned char)*string)) string++;
b0e26073 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 117static 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 130static 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 163static 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
3ff276f2 174static char *validate_params(game_params *params, int full)
c8230524 175{
ab53eb64 176 if (params->w < 2 || params->h < 2)
c8230524 177 return "Width and height must both be at least two";
178
179 return NULL;
180}
181
be8d5aa1 182static 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 196static char *new_game_desc(game_params *params, random_state *rs,
c566778e 197 char **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) {
060ba134 210 int prevoffset = -1;
211 int max = (params->w > params->h ? params->w : params->h);
212 int *prevmoves = snewn(max, int);
4efb3868 213
81875211 214 /*
215 * Shuffle the old-fashioned way, by making a series of
216 * single moves on the grid.
217 */
4efb3868 218
81875211 219 for (i = 0; i < n; i++)
220 tiles[i] = i;
221
222 for (i = 0; i < params->movetarget; i++) {
060ba134 223 int start, offset, len, direction, index;
81875211 224 int j, tmp;
225
226 /*
227 * Choose a move to make. We can choose from any row
228 * or any column.
229 */
230 while (1) {
231 j = random_upto(rs, params->w + params->h);
232
233 if (j < params->w) {
234 /* Column. */
060ba134 235 index = j;
81875211 236 start = j;
237 offset = params->w;
238 len = params->h;
239 } else {
240 /* Row. */
060ba134 241 index = j - params->w;
242 start = index * params->w;
81875211 243 offset = 1;
244 len = params->w;
245 }
4efb3868 246
81875211 247 direction = -1 + 2 * random_upto(rs, 2);
4efb3868 248
81875211 249 /*
060ba134 250 * To at least _try_ to avoid boring cases, check
251 * that this move doesn't directly undo a previous
252 * one, or repeat it so many times as to turn it
253 * into fewer moves in the opposite direction. (For
254 * example, in a row of length 4, we're allowed to
255 * move it the same way twice, but not three
256 * times.)
257 *
258 * We track this for each individual row/column,
259 * and clear all the counters as soon as a
260 * perpendicular move is made. This isn't perfect
261 * (it _can't_ guaranteeably be perfect - there
262 * will always come a move count beyond which a
263 * shorter solution will be possible than the one
264 * which constructed the position) but it should
265 * sort out all the obvious cases.
81875211 266 */
060ba134 267 if (offset == prevoffset) {
268 tmp = prevmoves[index] + direction;
269 if (abs(2*tmp) > len || abs(tmp) < abs(prevmoves[index]))
270 continue;
271 }
4efb3868 272
81875211 273 /* If we didn't `continue', we've found an OK move to make. */
060ba134 274 if (offset != prevoffset) {
275 int i;
276 for (i = 0; i < max; i++)
277 prevmoves[i] = 0;
278 prevoffset = offset;
279 }
280 prevmoves[index] += direction;
81875211 281 break;
282 }
4efb3868 283
81875211 284 /*
060ba134 285 * Make the move.
81875211 286 */
287 if (direction < 0) {
288 start += (len-1) * offset;
289 offset = -offset;
290 }
291 tmp = tiles[start];
292 for (j = 0; j+1 < len; j++)
293 tiles[start + j*offset] = tiles[start + (j+1)*offset];
294 tiles[start + (len-1) * offset] = tmp;
295 }
296
060ba134 297 sfree(prevmoves);
298
81875211 299 } else {
300
301 used = snewn(n, int);
302
303 for (i = 0; i < n; i++) {
304 tiles[i] = -1;
305 used[i] = FALSE;
306 }
307
308 /*
309 * If both dimensions are odd, there is a parity
310 * constraint.
311 */
312 if (params->w & params->h & 1)
313 stop = 2;
314 else
315 stop = 0;
316
317 /*
318 * Place everything except (possibly) the last two tiles.
319 */
320 for (x = 0, i = n; i > stop; i--) {
321 int k = i > 1 ? random_upto(rs, i) : 0;
322 int j;
323
324 for (j = 0; j < n; j++)
325 if (!used[j] && (k-- == 0))
326 break;
327
328 assert(j < n && !used[j]);
329 used[j] = TRUE;
330
331 while (tiles[x] >= 0)
332 x++;
333 assert(x < n);
334 tiles[x] = j;
335 }
336
337 if (stop) {
338 /*
339 * Find the last two locations, and the last two
340 * pieces.
341 */
342 while (tiles[x] >= 0)
343 x++;
344 assert(x < n);
345 x1 = x;
346 x++;
347 while (tiles[x] >= 0)
348 x++;
349 assert(x < n);
350 x2 = x;
351
352 for (i = 0; i < n; i++)
353 if (!used[i])
354 break;
355 p1 = i;
356 for (i = p1+1; i < n; i++)
357 if (!used[i])
358 break;
359 p2 = i;
360
361 /*
362 * Try the last two tiles one way round. If that fails,
363 * swap them.
364 */
365 tiles[x1] = p1;
366 tiles[x2] = p2;
367 if (perm_parity(tiles, n) != 0) {
368 tiles[x1] = p2;
369 tiles[x2] = p1;
370 assert(perm_parity(tiles, n) == 0);
371 }
372 }
373
374 sfree(used);
4efb3868 375 }
376
377 /*
1185e3c5 378 * Now construct the game description, by describing the tile
379 * array as a simple sequence of comma-separated integers.
4efb3868 380 */
381 ret = NULL;
382 retlen = 0;
383 for (i = 0; i < n; i++) {
384 char buf[80];
385 int k;
386
387 k = sprintf(buf, "%d,", tiles[i]+1);
388
389 ret = sresize(ret, retlen + k + 1, char);
390 strcpy(ret + retlen, buf);
391 retlen += k;
392 }
393 ret[retlen-1] = '\0'; /* delete last comma */
394
395 sfree(tiles);
4efb3868 396
397 return ret;
398}
399
5928817c 400
1185e3c5 401static char *validate_desc(game_params *params, char *desc)
5928817c 402{
403 char *p, *err;
404 int i, area;
405 int *used;
406
407 area = params->w * params->h;
1185e3c5 408 p = desc;
5928817c 409 err = NULL;
410
411 used = snewn(area, int);
412 for (i = 0; i < area; i++)
413 used[i] = FALSE;
414
415 for (i = 0; i < area; i++) {
416 char *q = p;
417 int n;
418
419 if (*p < '0' || *p > '9') {
420 err = "Not enough numbers in string";
421 goto leave;
422 }
423 while (*p >= '0' && *p <= '9')
424 p++;
425 if (i < area-1 && *p != ',') {
426 err = "Expected comma after number";
427 goto leave;
428 }
429 else if (i == area-1 && *p) {
430 err = "Excess junk at end of string";
431 goto leave;
432 }
433 n = atoi(q);
434 if (n < 1 || n > area) {
435 err = "Number out of range";
436 goto leave;
437 }
438 if (used[n-1]) {
439 err = "Number used twice";
440 goto leave;
441 }
442 used[n-1] = TRUE;
443
444 if (*p) p++; /* eat comma */
445 }
446
447 leave:
448 sfree(used);
449 return err;
450}
451
dafd6cf6 452static game_state *new_game(midend *me, game_params *params, char *desc)
4efb3868 453{
454 game_state *state = snew(game_state);
455 int i;
456 char *p;
457
458 state->w = params->w;
459 state->h = params->h;
460 state->n = params->w * params->h;
461 state->tiles = snewn(state->n, int);
462
1185e3c5 463 p = desc;
4efb3868 464 i = 0;
465 for (i = 0; i < state->n; i++) {
466 assert(*p);
467 state->tiles[i] = atoi(p);
468 while (*p && *p != ',')
469 p++;
470 if (*p) p++; /* eat comma */
471 }
472 assert(!*p);
473
fd1a1a2b 474 state->completed = state->movecount = 0;
81875211 475 state->movetarget = params->movetarget;
a440f184 476 state->used_solve = FALSE;
c8230524 477 state->last_movement_sense = 0;
4efb3868 478
479 return state;
480}
481
be8d5aa1 482static game_state *dup_game(game_state *state)
4efb3868 483{
484 game_state *ret = snew(game_state);
485
486 ret->w = state->w;
487 ret->h = state->h;
488 ret->n = state->n;
489 ret->tiles = snewn(state->w * state->h, int);
490 memcpy(ret->tiles, state->tiles, state->w * state->h * sizeof(int));
491 ret->completed = state->completed;
fd1a1a2b 492 ret->movecount = state->movecount;
81875211 493 ret->movetarget = state->movetarget;
2ac6d24e 494 ret->used_solve = state->used_solve;
c8230524 495 ret->last_movement_sense = state->last_movement_sense;
4efb3868 496
497 return ret;
498}
499
be8d5aa1 500static void free_game(game_state *state)
4efb3868 501{
ab53eb64 502 sfree(state->tiles);
4efb3868 503 sfree(state);
504}
505
df11cd4e 506static char *solve_game(game_state *state, game_state *currstate,
c566778e 507 char *aux, char **error)
2ac6d24e 508{
df11cd4e 509 return dupstr("S");
2ac6d24e 510}
511
fa3abef5 512static int game_can_format_as_text_now(game_params *params)
513{
514 return TRUE;
515}
516
9b4b03d3 517static char *game_text_format(game_state *state)
518{
af52394e 519 char *ret, *p, buf[80];
520 int x, y, col, maxlen;
521
522 /*
523 * First work out how many characters we need to display each
524 * number.
525 */
526 col = sprintf(buf, "%d", state->n);
527
528 /*
529 * Now we know the exact total size of the grid we're going to
530 * produce: it's got h rows, each containing w lots of col, w-1
531 * spaces and a trailing newline.
532 */
533 maxlen = state->h * state->w * (col+1);
534
48a10826 535 ret = snewn(maxlen+1, char);
af52394e 536 p = ret;
537
538 for (y = 0; y < state->h; y++) {
539 for (x = 0; x < state->w; x++) {
540 int v = state->tiles[state->w*y+x];
541 sprintf(buf, "%*d", col, v);
542 memcpy(p, buf, col);
543 p += col;
544 if (x+1 == state->w)
545 *p++ = '\n';
546 else
547 *p++ = ' ';
548 }
549 }
550
551 assert(p - ret == maxlen);
552 *p = '\0';
553 return ret;
9b4b03d3 554}
555
3e17893b 556struct game_ui {
557 int cur_x, cur_y;
558 int cur_visible;
559};
560
be8d5aa1 561static game_ui *new_ui(game_state *state)
74a4e547 562{
3e17893b 563 game_ui *ui = snew(game_ui);
564 ui->cur_x = 0;
565 ui->cur_y = -1;
566 ui->cur_visible = FALSE;
567
568 return ui;
74a4e547 569}
570
be8d5aa1 571static void free_ui(game_ui *ui)
74a4e547 572{
3e17893b 573 sfree(ui);
74a4e547 574}
575
844f605f 576static char *encode_ui(game_ui *ui)
ae8290c6 577{
578 return NULL;
579}
580
844f605f 581static void decode_ui(game_ui *ui, char *encoding)
ae8290c6 582{
583}
584
07dfb697 585static void game_changed_state(game_ui *ui, game_state *oldstate,
586 game_state *newstate)
587{
588}
589
1e3e152d 590struct game_drawstate {
591 int started;
592 int w, h, bgcolour;
593 int *tiles;
594 int tilesize;
3e17893b 595 int cur_x, cur_y;
1e3e152d 596};
597
e1f3c707 598static char *interpret_move(game_state *state, game_ui *ui, const game_drawstate *ds,
df11cd4e 599 int x, int y, int button)
600{
3e17893b 601 int cx = -1, cy = -1, dx, dy;
df11cd4e 602 char buf[80];
4efb3868 603
f0ee053c 604 button &= ~MOD_MASK;
4efb3868 605
3e17893b 606 if (IS_CURSOR_MOVE(button)) {
607 /* right/down rotates cursor clockwise,
608 * left/up rotates anticlockwise. */
609 int cpos, diff;
610 cpos = c2pos(state->w, state->h, ui->cur_x, ui->cur_y);
611 diff = c2diff(state->w, state->h, ui->cur_x, ui->cur_y, button);
612
613 cpos += diff;
614 pos2c(state->w, state->h, cpos, &ui->cur_x, &ui->cur_y);
615
616 ui->cur_visible = 1;
617 return "";
618 }
619
620 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
621 cx = FROMCOORD(x);
622 cy = FROMCOORD(y);
623 ui->cur_visible = 0;
624 } else if (IS_CURSOR_SELECT(button)) {
625 if (ui->cur_visible) {
626 cx = ui->cur_x;
627 cy = ui->cur_y;
628 } else {
629 ui->cur_visible = 1;
630 return "";
631 }
632 } else {
633 return NULL;
634 }
635
df11cd4e 636 if (cx == -1 && cy >= 0 && cy < state->h)
637 dx = -1, dy = 0;
638 else if (cx == state->w && cy >= 0 && cy < state->h)
639 dx = +1, dy = 0;
640 else if (cy == -1 && cx >= 0 && cx < state->w)
641 dy = -1, dx = 0;
642 else if (cy == state->h && cx >= 0 && cx < state->w)
643 dy = +1, dx = 0;
4efb3868 644 else
3e17893b 645 return ""; /* invalid click location */
4efb3868 646
e91825f8 647 /* reverse direction if right hand button is pressed */
3e17893b 648 if (button == RIGHT_BUTTON || button == CURSOR_SELECT2) {
df11cd4e 649 dx = -dx;
650 dy = -dy;
e91825f8 651 }
652
df11cd4e 653 if (dx)
654 sprintf(buf, "R%d,%d", cy, dx);
655 else
656 sprintf(buf, "C%d,%d", cx, dy);
657 return dupstr(buf);
658}
659
660static game_state *execute_move(game_state *from, char *move)
661{
662 int cx, cy, dx, dy;
663 int tx, ty, n;
664 game_state *ret;
665
666 if (!strcmp(move, "S")) {
667 int i;
668
669 ret = dup_game(from);
670
671 /*
672 * Simply replace the grid with a solved one. For this game,
673 * this isn't a useful operation for actually telling the user
674 * what they should have done, but it is useful for
675 * conveniently being able to get hold of a clean state from
676 * which to practise manoeuvres.
677 */
678 for (i = 0; i < ret->n; i++)
679 ret->tiles[i] = i+1;
a440f184 680 ret->used_solve = TRUE;
df11cd4e 681 ret->completed = ret->movecount = 1;
682
683 return ret;
684 }
685
686 if (move[0] == 'R' && sscanf(move+1, "%d,%d", &cy, &dx) == 2 &&
687 cy >= 0 && cy < from->h) {
688 cx = dy = 0;
689 n = from->w;
690 } else if (move[0] == 'C' && sscanf(move+1, "%d,%d", &cx, &dy) == 2 &&
691 cx >= 0 && cx < from->w) {
692 cy = dx = 0;
693 n = from->h;
694 } else
695 return NULL;
696
4efb3868 697 ret = dup_game(from);
698
699 do {
df11cd4e 700 tx = (cx - dx + from->w) % from->w;
701 ty = (cy - dy + from->h) % from->h;
4efb3868 702 ret->tiles[C(ret, cx, cy)] = from->tiles[C(from, tx, ty)];
df11cd4e 703 cx = tx;
704 cy = ty;
4efb3868 705 } while (--n > 0);
706
fd1a1a2b 707 ret->movecount++;
708
df11cd4e 709 ret->last_movement_sense = dx+dy;
c8230524 710
4efb3868 711 /*
712 * See if the game has been completed.
713 */
714 if (!ret->completed) {
fd1a1a2b 715 ret->completed = ret->movecount;
4efb3868 716 for (n = 0; n < ret->n; n++)
717 if (ret->tiles[n] != n+1)
718 ret->completed = FALSE;
719 }
720
721 return ret;
722}
723
724/* ----------------------------------------------------------------------
725 * Drawing routines.
726 */
727
1f3ee4ee 728static void game_compute_size(game_params *params, int tilesize,
729 int *x, int *y)
4efb3868 730{
1f3ee4ee 731 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
732 struct { int tilesize; } ads, *ds = &ads;
733 ads.tilesize = tilesize;
1e3e152d 734
4efb3868 735 *x = TILE_SIZE * params->w + 2 * BORDER;
736 *y = TILE_SIZE * params->h + 2 * BORDER;
737}
738
dafd6cf6 739static void game_set_size(drawing *dr, game_drawstate *ds,
740 game_params *params, int tilesize)
1f3ee4ee 741{
742 ds->tilesize = tilesize;
743}
744
8266f3fc 745static float *game_colours(frontend *fe, int *ncolours)
4efb3868 746{
747 float *ret = snewn(3 * NCOLOURS, float);
748 int i;
4efb3868 749
937a9eff 750 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
4efb3868 751
937a9eff 752 for (i = 0; i < 3; i++)
4efb3868 753 ret[COL_TEXT * 3 + i] = 0.0;
4efb3868 754
755 *ncolours = NCOLOURS;
756 return ret;
757}
758
dafd6cf6 759static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
4efb3868 760{
761 struct game_drawstate *ds = snew(struct game_drawstate);
762 int i;
763
764 ds->started = FALSE;
765 ds->w = state->w;
766 ds->h = state->h;
767 ds->bgcolour = COL_BACKGROUND;
768 ds->tiles = snewn(ds->w*ds->h, int);
1e3e152d 769 ds->tilesize = 0; /* haven't decided yet */
4efb3868 770 for (i = 0; i < ds->w*ds->h; i++)
771 ds->tiles[i] = -1;
3e17893b 772 ds->cur_x = ds->cur_y = -1;
4efb3868 773
774 return ds;
775}
776
dafd6cf6 777static void game_free_drawstate(drawing *dr, game_drawstate *ds)
4efb3868 778{
779 sfree(ds->tiles);
780 sfree(ds);
781}
782
dafd6cf6 783static void draw_tile(drawing *dr, game_drawstate *ds,
1e3e152d 784 game_state *state, int x, int y,
4efb3868 785 int tile, int flash_colour)
786{
787 if (tile == 0) {
dafd6cf6 788 draw_rect(dr, x, y, TILE_SIZE, TILE_SIZE,
4efb3868 789 flash_colour);
790 } else {
791 int coords[6];
792 char str[40];
793
794 coords[0] = x + TILE_SIZE - 1;
795 coords[1] = y + TILE_SIZE - 1;
796 coords[2] = x + TILE_SIZE - 1;
797 coords[3] = y;
798 coords[4] = x;
799 coords[5] = y + TILE_SIZE - 1;
dafd6cf6 800 draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT);
4efb3868 801
802 coords[0] = x;
803 coords[1] = y;
dafd6cf6 804 draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
4efb3868 805
dafd6cf6 806 draw_rect(dr, x + HIGHLIGHT_WIDTH, y + HIGHLIGHT_WIDTH,
4efb3868 807 TILE_SIZE - 2*HIGHLIGHT_WIDTH, TILE_SIZE - 2*HIGHLIGHT_WIDTH,
808 flash_colour);
809
810 sprintf(str, "%d", tile);
dafd6cf6 811 draw_text(dr, x + TILE_SIZE/2, y + TILE_SIZE/2,
4efb3868 812 FONT_VARIABLE, TILE_SIZE/3, ALIGN_VCENTRE | ALIGN_HCENTRE,
813 COL_TEXT, str);
814 }
dafd6cf6 815 draw_update(dr, x, y, TILE_SIZE, TILE_SIZE);
4efb3868 816}
817
dafd6cf6 818static void draw_arrow(drawing *dr, game_drawstate *ds,
3e17893b 819 int x, int y, int xdx, int xdy, int cur)
4efb3868 820{
821 int coords[14];
822 int ydy = -xdx, ydx = xdy;
823
824#define POINT(n, xx, yy) ( \
825 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
826 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
827
828 POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */
829 POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */
830 POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */
831 POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */
832 POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */
833 POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */
834 POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */
835
3e17893b 836 draw_polygon(dr, coords, 7, cur ? COL_HIGHLIGHT : COL_LOWLIGHT, COL_TEXT);
837}
838
839static void draw_arrow_for_cursor(drawing *dr, game_drawstate *ds,
840 int cur_x, int cur_y, int cur)
841{
842 if (cur_x == -1 && cur_y == -1)
843 return; /* 'no cursur here */
844 else if (cur_x == -1) /* LH column. */
845 draw_arrow(dr, ds, COORD(0), COORD(cur_y+1), 0, -1, cur);
846 else if (cur_x == ds->w) /* RH column */
847 draw_arrow(dr, ds, COORD(ds->w), COORD(cur_y), 0, +1, cur);
848 else if (cur_y == -1) /* Top row */
849 draw_arrow(dr, ds, COORD(cur_x), COORD(0), +1, 0, cur);
850 else if (cur_y == ds->h) /* Bottom row */
851 draw_arrow(dr, ds, COORD(cur_x+1), COORD(ds->h), -1, 0, cur);
852 else
853 assert(!"Invalid cursor position");
854
855 draw_update(dr, COORD(cur_x), COORD(cur_y),
856 TILE_SIZE, TILE_SIZE);
4efb3868 857}
858
dafd6cf6 859static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
c822de4a 860 game_state *state, int dir, game_ui *ui,
74a4e547 861 float animtime, float flashtime)
4efb3868 862{
b443c381 863 int i, bgcolour;
3e17893b 864 int cur_x = -1, cur_y = -1;
4efb3868 865
866 if (flashtime > 0) {
867 int frame = (int)(flashtime / FLASH_FRAME);
868 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
869 } else
870 bgcolour = COL_BACKGROUND;
871
872 if (!ds->started) {
19f24306 873 int coords[10];
4efb3868 874
dafd6cf6 875 draw_rect(dr, 0, 0,
4efb3868 876 TILE_SIZE * state->w + 2 * BORDER,
877 TILE_SIZE * state->h + 2 * BORDER, COL_BACKGROUND);
dafd6cf6 878 draw_update(dr, 0, 0,
4efb3868 879 TILE_SIZE * state->w + 2 * BORDER,
880 TILE_SIZE * state->h + 2 * BORDER);
881
882 /*
883 * Recessed area containing the whole puzzle.
884 */
885 coords[0] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
886 coords[1] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
887 coords[2] = COORD(state->w) + HIGHLIGHT_WIDTH - 1;
888 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
19f24306 889 coords[4] = coords[2] - TILE_SIZE;
890 coords[5] = coords[3] + TILE_SIZE;
891 coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
892 coords[9] = COORD(state->h) + HIGHLIGHT_WIDTH - 1;
893 coords[6] = coords[8] + TILE_SIZE;
894 coords[7] = coords[9] - TILE_SIZE;
dafd6cf6 895 draw_polygon(dr, coords, 5, COL_HIGHLIGHT, COL_HIGHLIGHT);
4efb3868 896
897 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
898 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
dafd6cf6 899 draw_polygon(dr, coords, 5, COL_LOWLIGHT, COL_LOWLIGHT);
4efb3868 900
901 /*
902 * Arrows for making moves.
903 */
904 for (i = 0; i < state->w; i++) {
3e17893b 905 draw_arrow(dr, ds, COORD(i), COORD(0), +1, 0, 0);
906 draw_arrow(dr, ds, COORD(i+1), COORD(state->h), -1, 0, 0);
4efb3868 907 }
908 for (i = 0; i < state->h; i++) {
3e17893b 909 draw_arrow(dr, ds, COORD(state->w), COORD(i), 0, +1, 0);
910 draw_arrow(dr, ds, COORD(0), COORD(i+1), 0, -1, 0);
4efb3868 911 }
912
913 ds->started = TRUE;
914 }
3e17893b 915 /*
916 * Cursor (highlighted arrow around edge)
917 */
918 if (ui->cur_visible) {
919 cur_x = ui->cur_x; cur_y = ui->cur_y;
920 }
921 if (cur_x != ds->cur_x || cur_y != ds->cur_y) {
922 /* Cursor has changed; redraw two (prev and curr) arrows. */
923 draw_arrow_for_cursor(dr, ds, cur_x, cur_y, 1);
924 draw_arrow_for_cursor(dr, ds, ds->cur_x, ds->cur_y, 0);
925 ds->cur_x = cur_x; ds->cur_y = cur_y;
926 }
4efb3868 927
928 /*
b443c381 929 * Now draw each tile.
4efb3868 930 */
931
dafd6cf6 932 clip(dr, COORD(0), COORD(0), TILE_SIZE*state->w, TILE_SIZE*state->h);
4efb3868 933
b443c381 934 for (i = 0; i < state->n; i++) {
935 int t, t0;
936 /*
937 * Figure out what should be displayed at this
938 * location. It's either a simple tile, or it's a
939 * transition between two tiles (in which case we say
940 * -1 because it must always be drawn).
941 */
942
943 if (oldstate && oldstate->tiles[i] != state->tiles[i])
944 t = -1;
945 else
946 t = state->tiles[i];
947
948 t0 = t;
949
950 if (ds->bgcolour != bgcolour || /* always redraw when flashing */
951 ds->tiles[i] != t || ds->tiles[i] == -1 || t == -1) {
952 int x, y, x2, y2;
953
954 /*
955 * Figure out what to _actually_ draw, and where to
956 * draw it.
957 */
958 if (t == -1) {
959 int x0, y0, x1, y1, dx, dy;
960 int j;
961 float c;
962 int sense;
963
5b5c6b12 964 if (dir < 0) {
965 assert(oldstate);
b443c381 966 sense = -oldstate->last_movement_sense;
5b5c6b12 967 } else {
b443c381 968 sense = state->last_movement_sense;
5b5c6b12 969 }
b443c381 970
971 t = state->tiles[i];
972
973 /*
974 * FIXME: must be prepared to draw a double
975 * tile in some situations.
976 */
977
978 /*
979 * Find the coordinates of this tile in the old and
980 * new states.
981 */
982 x1 = COORD(X(state, i));
983 y1 = COORD(Y(state, i));
984 for (j = 0; j < oldstate->n; j++)
985 if (oldstate->tiles[j] == state->tiles[i])
986 break;
987 assert(j < oldstate->n);
988 x0 = COORD(X(state, j));
989 y0 = COORD(Y(state, j));
990
991 dx = (x1 - x0);
992 if (dx != 0 &&
993 dx != TILE_SIZE * sense) {
994 dx = (dx < 0 ? dx + TILE_SIZE * state->w :
995 dx - TILE_SIZE * state->w);
996 assert(abs(dx) == TILE_SIZE);
997 }
998 dy = (y1 - y0);
999 if (dy != 0 &&
1000 dy != TILE_SIZE * sense) {
1001 dy = (dy < 0 ? dy + TILE_SIZE * state->h :
1002 dy - TILE_SIZE * state->h);
1003 assert(abs(dy) == TILE_SIZE);
1004 }
1005
1006 c = (animtime / ANIM_TIME);
1007 if (c < 0.0F) c = 0.0F;
1008 if (c > 1.0F) c = 1.0F;
1009
1010 x = x0 + (int)(c * dx);
1011 y = y0 + (int)(c * dy);
1012 x2 = x1 - dx + (int)(c * dx);
1013 y2 = y1 - dy + (int)(c * dy);
1014 } else {
1015 x = COORD(X(state, i));
1016 y = COORD(Y(state, i));
1017 x2 = y2 = -1;
1018 }
1019
dafd6cf6 1020 draw_tile(dr, ds, state, x, y, t, bgcolour);
b443c381 1021 if (x2 != -1 || y2 != -1)
dafd6cf6 1022 draw_tile(dr, ds, state, x2, y2, t, bgcolour);
b443c381 1023 }
1024 ds->tiles[i] = t0;
4efb3868 1025 }
1026
dafd6cf6 1027 unclip(dr);
4efb3868 1028
1029 ds->bgcolour = bgcolour;
fd1a1a2b 1030
1031 /*
1032 * Update the status bar.
1033 */
1034 {
1035 char statusbuf[256];
1036
d108c342 1037 /*
1038 * Don't show the new status until we're also showing the
1039 * new _state_ - after the game animation is complete.
1040 */
1041 if (oldstate)
1042 state = oldstate;
1043
2ac6d24e 1044 if (state->used_solve)
1045 sprintf(statusbuf, "Moves since auto-solve: %d",
1046 state->movecount - state->completed);
81875211 1047 else {
2ac6d24e 1048 sprintf(statusbuf, "%sMoves: %d",
1049 (state->completed ? "COMPLETED! " : ""),
1050 (state->completed ? state->completed : state->movecount));
81875211 1051 if (state->movetarget)
1052 sprintf(statusbuf+strlen(statusbuf), " (target %d)",
1053 state->movetarget);
1054 }
fd1a1a2b 1055
dafd6cf6 1056 status_bar(dr, statusbuf);
fd1a1a2b 1057 }
4efb3868 1058}
1059
be8d5aa1 1060static float game_anim_length(game_state *oldstate,
e3f21163 1061 game_state *newstate, int dir, game_ui *ui)
4efb3868 1062{
a440f184 1063 return ANIM_TIME;
4efb3868 1064}
1065
be8d5aa1 1066static float game_flash_length(game_state *oldstate,
e3f21163 1067 game_state *newstate, int dir, game_ui *ui)
4efb3868 1068{
2ac6d24e 1069 if (!oldstate->completed && newstate->completed &&
1070 !oldstate->used_solve && !newstate->used_solve)
4efb3868 1071 return 2 * FLASH_FRAME;
1072 else
1073 return 0.0F;
1074}
fd1a1a2b 1075
1cea529f 1076static int game_status(game_state *state)
4496362f 1077{
1cea529f 1078 return state->completed ? +1 : 0;
4496362f 1079}
1080
4d08de49 1081static int game_timing_state(game_state *state, game_ui *ui)
48dcdd62 1082{
1083 return TRUE;
1084}
1085
dafd6cf6 1086static void game_print_size(game_params *params, float *x, float *y)
1087{
1088}
1089
1090static void game_print(drawing *dr, game_state *state, int tilesize)
1091{
1092}
1093
19ef4855 1094#ifdef COMBINED
1095#define thegame sixteen
1096#endif
1097
be8d5aa1 1098const struct game thegame = {
750037d7 1099 "Sixteen", "games.sixteen", "sixteen",
be8d5aa1 1100 default_params,
1101 game_fetch_preset,
1102 decode_params,
1103 encode_params,
1104 free_params,
1105 dup_params,
1d228b10 1106 TRUE, game_configure, custom_params,
be8d5aa1 1107 validate_params,
1185e3c5 1108 new_game_desc,
1185e3c5 1109 validate_desc,
be8d5aa1 1110 new_game,
1111 dup_game,
1112 free_game,
2ac6d24e 1113 TRUE, solve_game,
fa3abef5 1114 TRUE, game_can_format_as_text_now, game_text_format,
be8d5aa1 1115 new_ui,
1116 free_ui,
ae8290c6 1117 encode_ui,
1118 decode_ui,
07dfb697 1119 game_changed_state,
df11cd4e 1120 interpret_move,
1121 execute_move,
1f3ee4ee 1122 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
be8d5aa1 1123 game_colours,
1124 game_new_drawstate,
1125 game_free_drawstate,
1126 game_redraw,
1127 game_anim_length,
1128 game_flash_length,
1cea529f 1129 game_status,
dafd6cf6 1130 FALSE, FALSE, game_print_size, game_print,
ac9f41c4 1131 TRUE, /* wants_statusbar */
48dcdd62 1132 FALSE, game_timing_state,
2705d374 1133 0, /* flags */
be8d5aa1 1134};
3e17893b 1135
1136/* vim: set shiftwidth=4 tabstop=8: */