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