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