chiark / gitweb /
James Harvey has contributed an implementation of `Same Game', also
[sgt-puzzles.git] / samegame.c
1 /*
2  * 'same game' -- try to remove all the coloured squares by
3  *                selecting regions of contiguous colours.
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <ctype.h>
11 #include <math.h>
12
13 #include "puzzles.h"
14
15 #define TILE_INNER (ds->tileinner)
16 #define TILE_GAP (ds->tilegap)
17 #define TILE_SIZE (TILE_INNER + TILE_GAP)
18 #define PREFERRED_TILE_SIZE 32
19 #define BORDER (TILE_SIZE / 2)
20 #define HIGHLIGHT_WIDTH 2
21
22 #define FLASH_FRAME 0.13F
23
24 #define COORD(x)  ( (x) * TILE_SIZE + BORDER )
25 #define FROMCOORD(x)  ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
26
27 #define X(state, i) ( (i) % (state)->params.w )
28 #define Y(state, i) ( (i) / (state)->params.w )
29 #define C(state, x, y) ( (y) * (state)->w + (x) )
30
31 enum {
32     COL_BACKGROUND,
33     COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8, COL_9,
34     COL_IMPOSSIBLE, COL_SEL, COL_HIGHLIGHT, COL_LOWLIGHT,
35     NCOLOURS
36 };
37
38 /* scoresub is 1 or 2 (for (n-1)^2 or (n-2)^2) */
39 struct game_params {
40     int w, h, ncols, scoresub;
41 };
42
43 /* These flags must be unique across all uses; in the game_state,
44  * the game_ui, and the drawstate (as they all get combined in the
45  * drawstate). */
46 #define TILE_COLMASK    0x0ff
47 #define TILE_SELECTED   0x100 /* used in ui and drawstate */
48 #define TILE_JOINRIGHT  0x200 /* used in drawstate */
49 #define TILE_JOINDOWN   0x400 /* used in drawstate */
50 #define TILE_JOINDIAG   0x800 /* used in drawstate */
51
52 #define TILE(gs,x,y) ((gs)->tiles[(gs)->params.w*(y)+(x)])
53 #define COL(gs,x,y) (TILE(gs,x,y) & TILE_COLMASK)
54 #define ISSEL(gs,x,y) (TILE(gs,x,y) & TILE_SELECTED)
55
56 #define SWAPTILE(gs,x1,y1,x2,y2) do {   \
57     int t = TILE(gs,x1,y1);               \
58     TILE(gs,x1,y1) = TILE(gs,x2,y2);      \
59     TILE(gs,x2,y2) = t;                   \
60 } while (0)
61
62 static int npoints(game_params *params, int nsel)
63 {
64     int sdiff = nsel - params->scoresub;
65     return (sdiff > 0) ? sdiff * sdiff : 0;
66 }
67
68 struct game_state {
69     struct game_params params;
70     int n;
71     int *tiles; /* colour only */
72     int score;
73     int complete, impossible;
74 };
75
76 static game_params *default_params(void)
77 {
78     game_params *ret = snew(game_params);
79     ret->w = 5;
80     ret->h = 5;
81     ret->ncols = 3;
82     ret->scoresub = 2;
83     return ret;
84 }
85
86 static const struct game_params samegame_presets[] = {
87     { 5, 5, 3, 2 },
88     { 10, 5, 3, 2 },
89     { 15, 10, 3, 2 },
90     { 15, 10, 4, 2 },
91     { 20, 15, 4, 2 }
92 };
93
94 static int game_fetch_preset(int i, char **name, game_params **params)
95 {
96     game_params *ret;
97     char str[80];
98
99     if (i < 0 || i >= lenof(samegame_presets))
100         return FALSE;
101
102     ret = snew(game_params);
103     *ret = samegame_presets[i];
104
105     sprintf(str, "%dx%d, %d colours", ret->w, ret->h, ret->ncols);
106
107     *name = dupstr(str);
108     *params = ret;
109     return TRUE;
110 }
111
112 static void free_params(game_params *params)
113 {
114     sfree(params);
115 }
116
117 static game_params *dup_params(game_params *params)
118 {
119     game_params *ret = snew(game_params);
120     *ret = *params;                    /* structure copy */
121     return ret;
122 }
123
124 static void decode_params(game_params *params, char const *string)
125 {
126     char const *p = string;
127
128     params->w = atoi(p);
129     while (*p && isdigit((unsigned char)*p)) p++;
130     if (*p == 'x') {
131         p++;
132         params->h = atoi(p);
133         while (*p && isdigit((unsigned char)*p)) p++;
134     } else {
135         params->h = params->w;
136     }
137     if (*p++ == 'c') {
138         params->ncols = atoi(p);
139         while (*p && isdigit((unsigned char)*p)) p++;
140     } else {
141         params->ncols = 3;
142     }
143     if (*p++ == 's') {
144         params->scoresub = atoi(p);
145         while (*p && isdigit((unsigned char)*p)) p++;
146     } else {
147         params->scoresub = 2;
148     }
149 }
150
151 static char *encode_params(game_params *params, int full)
152 {
153     char ret[80];
154
155     sprintf(ret, "%dx%dc%ds%d",
156             params->w, params->h, params->ncols, params->scoresub);
157     return dupstr(ret);
158 }
159
160 static config_item *game_configure(game_params *params)
161 {
162     config_item *ret;
163     char buf[80];
164
165     ret = snewn(5, config_item);
166
167     ret[0].name = "Width";
168     ret[0].type = C_STRING;
169     sprintf(buf, "%d", params->w);
170     ret[0].sval = dupstr(buf);
171     ret[0].ival = 0;
172
173     ret[1].name = "Height";
174     ret[1].type = C_STRING;
175     sprintf(buf, "%d", params->h);
176     ret[1].sval = dupstr(buf);
177     ret[1].ival = 0;
178
179     ret[2].name = "No. of colours";
180     ret[2].type = C_STRING;
181     sprintf(buf, "%d", params->ncols);
182     ret[2].sval = dupstr(buf);
183     ret[2].ival = 0;
184
185     ret[3].name = "Scoring system";
186     ret[3].type = C_CHOICES;
187     ret[3].sval = ":(n-1)^2:(n-2)^2";
188     ret[3].ival = params->scoresub-1;
189
190     ret[4].name = NULL;
191     ret[4].type = C_END;
192     ret[4].sval = NULL;
193     ret[4].ival = 0;
194
195     return ret;
196 }
197
198 static game_params *custom_params(config_item *cfg)
199 {
200     game_params *ret = snew(game_params);
201
202     ret->w = atoi(cfg[0].sval);
203     ret->h = atoi(cfg[1].sval);
204     ret->ncols = atoi(cfg[2].sval);
205     ret->scoresub = cfg[3].ival + 1;
206
207     return ret;
208 }
209
210 static char *validate_params(game_params *params)
211 {
212     if (params->w < 1 || params->h < 1)
213         return "Width and height must both be positive";
214     if (params->ncols < 2)
215         return "It's too easy with only one colour...";
216     if (params->ncols > 9)
217         return "Maximum of 9 colours";
218
219     /* ...and we must make sure we can generate at least 2 squares
220      * of each colour so it's theoretically soluble. */
221     if ((params->w * params->h) < (params->ncols * 2))
222         return "Too many colours makes given grid size impossible";
223
224     if ((params->scoresub < 1) || (params->scoresub > 2))
225         return "Scoring system not recognised";
226
227     return NULL;
228 }
229
230 /* Currently this is a very very dumb game-generation engine; it
231  * just picks randomly from the tile space. I had a look at a few
232  * other same game implementations, and none of them attempt to do
233  * anything to try and make sure the grid started off with a nice
234  * set of large blocks.
235  *
236  * It does at least make sure that there are >= 2 of each colour
237  * present at the start.
238  */
239
240 static char *new_game_desc(game_params *params, random_state *rs,
241                            game_aux_info **aux, int interactive)
242 {
243     char *ret;
244     int n, i, j, c, retlen, *tiles;
245
246     debug(("new_game_desc: %dx%d, %d colours",
247            params->w, params->h, params->ncols));
248     n = params->w * params->h;
249     tiles = snewn(n, int);
250     memset(tiles, 0, n*sizeof(int));
251
252     /* randomly place two of each colour */
253     for (c = 0; c < params->ncols; c++) {
254         for (j = 0; j < 2; j++) {
255             do {
256                 i = (int)random_upto(rs, n);
257             } while (tiles[i] != 0);
258             tiles[i] = c+1;
259         }
260     }
261
262     /* fill in the rest randomly */
263     for (i = 0; i < n; i++) {
264         if (tiles[i] == 0)
265             tiles[i] = (int)random_upto(rs, params->ncols)+1;
266     }
267
268     ret = NULL;
269     retlen = 0;
270     for (i = 0; i < n; i++) {
271         char buf[80];
272         int k;
273
274         k = sprintf(buf, "%d,", tiles[i]);
275         ret = sresize(ret, retlen + k + 1, char);
276         strcpy(ret + retlen, buf);
277         retlen += k;
278     }
279     ret[retlen-1] = '\0'; /* delete last comma */
280
281     sfree(tiles);
282     return ret;
283 }
284
285 static void game_free_aux_info(game_aux_info *aux)
286 {
287     assert(!"Shouldn't happen");
288 }
289
290 static char *validate_desc(game_params *params, char *desc)
291 {
292     int area = params->w * params->h, i;
293     char *p = desc;
294
295     for (i = 0; i < area; i++) {
296         char *q = p;
297         int n;
298
299         if (!isdigit(*p))
300             return "Not enough numbers in string";
301         while (isdigit(*p)) p++;
302
303         if (i < area-1 && *p != ',')
304             return "Expected comma after number";
305         else if (i == area-1 && *p)
306             return "Excess junk at end of string";
307
308         n = atoi(q);
309         if (n < 0 || n > params->ncols)
310             return "Colour out of range";
311
312         if (*p) p++; /* eat comma */
313     }
314     return NULL;
315 }
316
317 static game_state *new_game(midend_data *me, game_params *params, char *desc)
318 {
319     game_state *state = snew(game_state);
320     char *p = desc;
321     int i;
322
323     state->params = *params; /* struct copy */
324     state->n = state->params.w * state->params.h;
325     state->tiles = snewn(state->n, int);
326
327     for (i = 0; i < state->n; i++) {
328         assert(*p);
329         state->tiles[i] = atoi(p);
330         while (*p && *p != ',')
331             p++;
332         if (*p) p++;                   /* eat comma */
333     }
334     state->complete = state->impossible = 0;
335     state->score = 0;
336
337     return state;
338 }
339
340 static game_state *dup_game(game_state *state)
341 {
342     game_state *ret = snew(game_state);
343
344     *ret = *state; /* structure copy, except... */
345
346     ret->tiles = snewn(state->n, int);
347     memcpy(ret->tiles, state->tiles, state->n * sizeof(int));
348
349     return ret;
350 }
351
352 static void free_game(game_state *state)
353 {
354     sfree(state->tiles);
355     sfree(state);
356 }
357
358 static game_state *solve_game(game_state *state, game_aux_info *aux,
359                               char **error)
360 {
361     return NULL;
362 }
363
364 static char *game_text_format(game_state *state)
365 {
366     char *ret, *p;
367     int x, y, maxlen;
368
369     maxlen = state->params.h * (state->params.w + 1);
370     ret = snewn(maxlen+1, char);
371     p = ret;
372
373     for (y = 0; y < state->params.h; y++) {
374         for (x = 0; x < state->params.w; x++) {
375             int t = TILE(state,x,y);
376             if (t <= 0)      *p++ = ' ';
377             else if (t < 10) *p++ = '0'+t;
378             else             *p++ = 'a'+(t-10);
379         }
380         *p++ = '\n';
381     }
382     assert(p - ret == maxlen);
383     *p = '\0';
384     return ret;
385 }
386
387 struct game_ui {
388     struct game_params params;
389     int *tiles; /* selected-ness only */
390     int nselected;
391 };
392
393 static game_ui *new_ui(game_state *state)
394 {
395     game_ui *ui = snew(game_ui);
396
397     ui->params = state->params; /* structure copy */
398     ui->tiles = snewn(state->n, int);
399     memset(ui->tiles, 0, state->n*sizeof(int));
400     ui->nselected = 0;
401
402     return ui;
403 }
404
405 static void free_ui(game_ui *ui)
406 {
407     sfree(ui->tiles);
408     sfree(ui);
409 }
410
411 static void sel_clear(game_ui *ui, game_state *state)
412 {
413     int i;
414
415     for (i = 0; i < state->n; i++)
416         ui->tiles[i] &= ~TILE_SELECTED;
417     ui->nselected = 0;
418     debug(("sel_clear"));
419 }
420
421
422 static void game_changed_state(game_ui *ui, game_state *oldstate,
423                                game_state *newstate)
424 {
425     sel_clear(ui, newstate);
426 }
427
428 static void sel_remove(game_ui *ui, game_state *state)
429 {
430     int i, nremoved = 0;
431
432     state->score += npoints(&state->params, ui->nselected);
433
434     for (i = 0; i < state->n; i++) {
435         if (ui->tiles[i] & TILE_SELECTED) {
436             nremoved++;
437             state->tiles[i] = 0;
438             ui->tiles[i] &= ~TILE_SELECTED;
439         }
440     }
441     ui->nselected = 0;
442     debug(("sel_remove: removed %d selected tiles", nremoved));
443 }
444
445 static void sel_expand(game_ui *ui, game_state *state, int tx, int ty)
446 {
447     int ns = 1, nadded, x, y, c;
448
449     TILE(ui,tx,ty) |= TILE_SELECTED;
450     debug(("sel_expand, selected initial tile"));
451     do {
452         nadded = 0;
453
454         for (x = 0; x < state->params.w; x++) {
455             for (y = 0; y < state->params.h; y++) {
456                 if (x == tx && y == ty) continue;
457                 if (ISSEL(ui,x,y)) continue;
458
459                 c = COL(state,x,y);
460                 if ((x > 0) &&
461                     ISSEL(ui,x-1,y) && COL(state,x-1,y) == c) {
462                     TILE(ui,x,y) |= TILE_SELECTED;
463                     nadded++;
464                     continue;
465                 }
466
467                 if ((x+1 < state->params.w) &&
468                     ISSEL(ui,x+1,y) && COL(state,x+1,y) == c) {
469                     TILE(ui,x,y) |= TILE_SELECTED;
470                     nadded++;
471                     continue;
472                 }
473
474                 if ((y > 0) &&
475                     ISSEL(ui,x,y-1) && COL(state,x,y-1) == c) {
476                     TILE(ui,x,y) |= TILE_SELECTED;
477                     nadded++;
478                     continue;
479                 }
480
481                 if ((y+1 < state->params.h) &&
482                     ISSEL(ui,x,y+1) && COL(state,x,y+1) == c) {
483                     TILE(ui,x,y) |= TILE_SELECTED;
484                     nadded++;
485                     continue;
486                 }
487             }
488         }
489         ns += nadded;
490         debug(("sel_expand, new pass, selected %d more tiles", nadded));
491     } while (nadded > 0);
492
493     if (ns > 1) {
494         ui->nselected = ns;
495     } else {
496         sel_clear(ui, state);
497     }
498 }
499
500 static int sg_emptycol(game_state *ret, int x)
501 {
502     int y;
503     for (y = 0; y < ret->params.h; y++) {
504         if (COL(ret,x,y)) return 0;
505     }
506     return 1;
507 }
508
509
510 static void sg_snuggle(game_state *ret)
511 {
512     int x,y, ndone;
513
514     /* make all unsupported tiles fall down. */
515     do {
516         ndone = 0;
517         for (x = 0; x < ret->params.w; x++) {
518             for (y = ret->params.h-1; y > 0; y--) {
519                 if (COL(ret,x,y) != 0) continue;
520                 if (COL(ret,x,y-1) != 0) {
521                     SWAPTILE(ret,x,y,x,y-1);
522                     ndone++;
523                 }
524             }
525         }
526     } while (ndone);
527
528     /* shuffle all columns as far left as they can go. */
529     do {
530         ndone = 0;
531         for (x = 0; x < ret->params.w-1; x++) {
532             if (sg_emptycol(ret,x) && !sg_emptycol(ret,x+1)) {
533                 debug(("column %d is empty, shuffling from %d", x, x+1));
534                 ndone++;
535                 for (y = 0; y < ret->params.h; y++) {
536                     SWAPTILE(ret,x,y,x+1,y);
537                 }
538             }
539         }
540     } while (ndone);
541 }
542
543 static void sg_check(game_state *ret)
544 {
545     int x,y, complete = 1, impossible = 1;
546
547     for (x = 0; x < ret->params.w; x++) {
548         for (y = 0; y < ret->params.h; y++) {
549             if (COL(ret,x,y) == 0)
550                 continue;
551             complete = 0;
552             if (x+1 < ret->params.w) {
553                 if (COL(ret,x,y) == COL(ret,x+1,y))
554                     impossible = 0;
555             }
556             if (y+1 < ret->params.h) {
557                 if (COL(ret,x,y) == COL(ret,x,y+1))
558                     impossible = 0;
559             }
560         }
561     }
562     ret->complete = complete;
563     ret->impossible = impossible;
564 }
565
566 struct game_drawstate {
567     int started, bgcolour;
568     int tileinner, tilegap;
569     int *tiles; /* contains colour and SELECTED. */
570 };
571
572 static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
573                              int x, int y, int button)
574 {
575     int tx, ty;
576     game_state *ret = from;
577
578     if (button != RIGHT_BUTTON && button != LEFT_BUTTON)
579         return NULL;
580
581     tx = FROMCOORD(x); ty= FROMCOORD(y);
582     if (tx < 0 || tx >= from->params.w || ty < 0 || ty >= from->params.h)
583         return NULL;
584     if (COL(from, tx, ty) == 0) return NULL;
585
586     if (ISSEL(ui,tx,ty)) {
587         if (button == RIGHT_BUTTON)
588             sel_clear(ui, from);
589         else {
590             /* this is the actual move. */
591             ret = dup_game(from);
592             sel_remove(ui, ret);
593             sg_snuggle(ret); /* shifts blanks down and to the left */
594             sg_check(ret);   /* checks for completeness or impossibility */
595         }
596     } else {
597         sel_clear(ui, from); /* might be no-op */
598         sel_expand(ui, from, tx, ty);
599     }
600
601     return ret;
602 }
603
604 /* ----------------------------------------------------------------------
605  * Drawing routines.
606  */
607
608 static void game_size(game_params *params, game_drawstate *ds, int *x, int *y,
609                       int expand)
610 {
611     int tsx, tsy, ts;
612
613     /*
614      * We could choose the tile gap dynamically as well if we
615      * wanted to; for example, at low tile sizes it might be
616      * sensible to leave it out completely. However, for the moment
617      * and for the sake of simplicity I'm just going to fix it at
618      * 2.
619      */
620     ds->tilegap = 2;
621
622     /*
623      * Each window dimension equals the tile size (inner plus gap)
624      * times the grid dimension, plus another tile size (border is
625      * half the width of a tile), minus one tile gap.
626      * 
627      * We must cast to unsigned before adding to *x and *y, since
628      * they might be INT_MAX!
629      */
630     tsx = (unsigned)(*x + ds->tilegap) / (params->w + 1);
631     tsy = (unsigned)(*y + ds->tilegap) / (params->h + 1);
632
633     ts = min(tsx, tsy);
634     if (expand)
635         ds->tileinner = ts - ds->tilegap;
636     else
637         ds->tileinner = min(ts, PREFERRED_TILE_SIZE) - ds->tilegap;
638
639     *x = TILE_SIZE * params->w + 2 * BORDER - TILE_GAP;
640     *y = TILE_SIZE * params->h + 2 * BORDER - TILE_GAP;
641 }
642
643 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
644 {
645     float *ret = snewn(3 * NCOLOURS, float);
646
647     frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
648
649     ret[COL_1 * 3 + 0] = 0.0F;
650     ret[COL_1 * 3 + 1] = 0.0F;
651     ret[COL_1 * 3 + 2] = 1.0F;
652
653     ret[COL_2 * 3 + 0] = 0.0F;
654     ret[COL_2 * 3 + 1] = 0.5F;
655     ret[COL_2 * 3 + 2] = 0.0F;
656
657     ret[COL_3 * 3 + 0] = 1.0F;
658     ret[COL_3 * 3 + 1] = 0.0F;
659     ret[COL_3 * 3 + 2] = 0.0F;
660
661     ret[COL_4 * 3 + 0] = 0.5F;
662     ret[COL_4 * 3 + 1] = 0.5F;
663     ret[COL_4 * 3 + 2] = 1.0F;
664
665     ret[COL_5 * 3 + 0] = 0.5F;
666     ret[COL_5 * 3 + 1] = 1.0F;
667     ret[COL_5 * 3 + 2] = 0.5F;
668
669     ret[COL_6 * 3 + 0] = 1.0F;
670     ret[COL_6 * 3 + 1] = 0.5F;
671     ret[COL_6 * 3 + 2] = 0.5F;
672
673     ret[COL_7 * 3 + 0] = 1.0F;
674     ret[COL_7 * 3 + 1] = 1.0F;
675     ret[COL_7 * 3 + 2] = 0.0F;
676
677     ret[COL_8 * 3 + 0] = 1.0F;
678     ret[COL_8 * 3 + 1] = 0.0F;
679     ret[COL_8 * 3 + 2] = 1.0F;
680
681     ret[COL_9 * 3 + 0] = 0.0F;
682     ret[COL_9 * 3 + 1] = 1.0F;
683     ret[COL_9 * 3 + 2] = 1.0F;
684
685     ret[COL_IMPOSSIBLE * 3 + 0] = 0.0F;
686     ret[COL_IMPOSSIBLE * 3 + 1] = 0.0F;
687     ret[COL_IMPOSSIBLE * 3 + 2] = 0.0F;
688
689     ret[COL_SEL * 3 + 0] = 1.0F;
690     ret[COL_SEL * 3 + 1] = 1.0F;
691     ret[COL_SEL * 3 + 2] = 1.0F;
692
693     ret[COL_HIGHLIGHT * 3 + 0] = 1.0F;
694     ret[COL_HIGHLIGHT * 3 + 1] = 1.0F;
695     ret[COL_HIGHLIGHT * 3 + 2] = 1.0F;
696
697     ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
698     ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
699     ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
700
701     *ncolours = NCOLOURS;
702     return ret;
703 }
704
705 static game_drawstate *game_new_drawstate(game_state *state)
706 {
707     struct game_drawstate *ds = snew(struct game_drawstate);
708     int i;
709
710     ds->started = 0;
711     ds->tileinner = ds->tilegap = 0;   /* not decided yet */
712     ds->tiles = snewn(state->n, int);
713     for (i = 0; i < state->n; i++)
714         ds->tiles[i] = -1;
715
716     return ds;
717 }
718
719 static void game_free_drawstate(game_drawstate *ds)
720 {
721     sfree(ds->tiles);
722     sfree(ds);
723 }
724
725 /* Drawing routing for the tile at (x,y) is responsible for drawing
726  * itself and the gaps to its right and below. If we're the same colour
727  * as the tile to our right, then we fill in the gap; ditto below, and if
728  * both then we fill the teeny tiny square in the corner as well.
729  */
730
731 static void tile_redraw(frontend *fe, game_drawstate *ds,
732                         int x, int y, int dright, int dbelow,
733                         int tile, game_state *state, int bgcolour)
734 {
735     int outer = bgcolour, inner = outer, col = tile & TILE_COLMASK;
736
737     if (col) {
738         if (state->impossible) {
739             outer = col;
740             inner = COL_IMPOSSIBLE;
741         } else if (tile & TILE_SELECTED) {
742             outer = COL_SEL;
743             inner = col;
744         } else {
745             outer = inner = col;
746         }
747     }
748     draw_rect(fe, COORD(x), COORD(y), TILE_INNER, TILE_INNER, outer);
749     draw_rect(fe, COORD(x)+TILE_INNER/4, COORD(y)+TILE_INNER/4,
750               TILE_INNER/2, TILE_INNER/2, inner);
751
752     if (dright)
753         draw_rect(fe, COORD(x)+TILE_INNER, COORD(y), TILE_GAP, TILE_INNER,
754                   (tile & TILE_JOINRIGHT) ? outer : bgcolour);
755     if (dbelow)
756         draw_rect(fe, COORD(x), COORD(y)+TILE_INNER, TILE_INNER, TILE_GAP,
757                   (tile & TILE_JOINDOWN) ? outer : bgcolour);
758     if (dright && dbelow)
759         draw_rect(fe, COORD(x)+TILE_INNER, COORD(y)+TILE_INNER, TILE_GAP, TILE_GAP,
760                   (tile & TILE_JOINDIAG) ? outer : bgcolour);
761
762     draw_update(fe, COORD(x), COORD(y), TILE_SIZE, TILE_SIZE);
763 }
764
765 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
766                         game_state *state, int dir, game_ui *ui,
767                         float animtime, float flashtime)
768 {
769     int bgcolour, x, y;
770
771     debug(("samegame redraw: dir %d, oldstate 0x%lx, animtime %f, flashtime %f",
772            dir, oldstate, animtime, flashtime));
773
774     /* This was entirely cloned from fifteen.c; it should probably be
775      * moved into some generic 'draw-recessed-rectangle' utility fn. */
776     if (!ds->started) {
777         int coords[10];
778
779         draw_rect(fe, 0, 0,
780                   TILE_SIZE * state->params.w + 2 * BORDER,
781                   TILE_SIZE * state->params.h + 2 * BORDER, COL_BACKGROUND);
782         draw_update(fe, 0, 0,
783                     TILE_SIZE * state->params.w + 2 * BORDER,
784                     TILE_SIZE * state->params.h + 2 * BORDER);
785
786         /*
787          * Recessed area containing the whole puzzle.
788          */
789         coords[0] = COORD(state->params.w) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
790         coords[1] = COORD(state->params.h) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
791         coords[2] = COORD(state->params.w) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
792         coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
793         coords[4] = coords[2] - TILE_SIZE;
794         coords[5] = coords[3] + TILE_SIZE;
795         coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
796         coords[9] = COORD(state->params.h) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
797         coords[6] = coords[8] + TILE_SIZE;
798         coords[7] = coords[9] - TILE_SIZE;
799         draw_polygon(fe, coords, 5, TRUE, COL_HIGHLIGHT);
800         draw_polygon(fe, coords, 5, FALSE, COL_HIGHLIGHT);
801
802         coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
803         coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
804         draw_polygon(fe, coords, 5, TRUE, COL_LOWLIGHT);
805         draw_polygon(fe, coords, 5, FALSE, COL_LOWLIGHT);
806
807         ds->started = 1;
808     }
809
810     if (flashtime > 0.0) {
811         int frame = (int)(flashtime / FLASH_FRAME);
812         bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
813     } else
814         bgcolour = COL_BACKGROUND;
815
816     for (x = 0; x < state->params.w; x++) {
817         for (y = 0; y < state->params.h; y++) {
818             int i = (state->params.w * y) + x;
819             int col = COL(state,x,y), tile = col;
820             int dright = (x+1 < state->params.w);
821             int dbelow = (y+1 < state->params.h);
822
823             tile |= ISSEL(ui,x,y);
824             if (dright && COL(state,x+1,y) == col)
825                 tile |= TILE_JOINRIGHT;
826             if (dbelow && COL(state,x,y+1) == col)
827                 tile |= TILE_JOINDOWN;
828             if ((tile & TILE_JOINRIGHT) && (tile & TILE_JOINDOWN) &&
829                 COL(state,x+1,y+1) == col)
830                 tile |= TILE_JOINDIAG;
831
832             /* For now we're never expecting oldstate at all (because we have
833              * no animation); when we do we might well want to be looking
834              * at the tile colours from oldstate, not state. */
835             if ((oldstate && COL(oldstate,x,y) != col) ||
836                 (flashtime > 0.0) ||
837                 (ds->bgcolour != bgcolour) ||
838                 (tile != ds->tiles[i])) {
839                 tile_redraw(fe, ds, x, y, dright, dbelow,
840                             tile, state, bgcolour);
841                 ds->tiles[i] = tile;
842             }
843         }
844     }
845     ds->bgcolour = bgcolour;
846
847     {
848         char status[255], score[80];
849
850         sprintf(score, "Score: %d", state->score);
851
852         if (state->complete)
853             sprintf(status, "COMPLETE! %s", score);
854         else if (state->impossible)
855             sprintf(status, "Cannot move! %s", score);
856         else if (ui->nselected)
857             sprintf(status, "%s  Selected: %d (%d)",
858                     score, ui->nselected, npoints(&state->params, ui->nselected));
859         else
860             sprintf(status, "%s", score);
861         status_bar(fe, status);
862     }
863 }
864
865 static float game_anim_length(game_state *oldstate, game_state *newstate,
866                               int dir, game_ui *ui)
867 {
868     return 0.0F;
869 }
870
871 static float game_flash_length(game_state *oldstate, game_state *newstate,
872                                int dir, game_ui *ui)
873 {
874     if ((!oldstate->complete && newstate->complete) ||
875         (!oldstate->impossible && newstate->impossible))
876         return 2 * FLASH_FRAME;
877     else
878         return 0.0F;
879 }
880
881 static int game_wants_statusbar(void)
882 {
883     return TRUE;
884 }
885
886 static int game_timing_state(game_state *state)
887 {
888     return TRUE;
889 }
890
891 #ifdef COMBINED
892 #define thegame samegame
893 #endif
894
895 const struct game thegame = {
896     "Same Game", NULL,
897     default_params,
898     game_fetch_preset,
899     decode_params,
900     encode_params,
901     free_params,
902     dup_params,
903     TRUE, game_configure, custom_params,
904     validate_params,
905     new_game_desc,
906     game_free_aux_info,
907     validate_desc,
908     new_game,
909     dup_game,
910     free_game,
911     FALSE, solve_game,
912     TRUE, game_text_format,
913     new_ui,
914     free_ui,
915     game_changed_state,
916     make_move,
917     game_size,
918     game_colours,
919     game_new_drawstate,
920     game_free_drawstate,
921     game_redraw,
922     game_anim_length,
923     game_flash_length,
924     game_wants_statusbar,
925     FALSE, game_timing_state,
926     0,                                 /* mouse_priorities */
927 };