chiark / gitweb /
Another function pair required for serialisation; these ones save
[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 #define TILE_IMPOSSIBLE 0x2000 /* used in drawstate */
53
54 #define TILE(gs,x,y) ((gs)->tiles[(gs)->params.w*(y)+(x)])
55 #define COL(gs,x,y) (TILE(gs,x,y) & TILE_COLMASK)
56 #define ISSEL(gs,x,y) (TILE(gs,x,y) & TILE_SELECTED)
57
58 #define SWAPTILE(gs,x1,y1,x2,y2) do {   \
59     int t = TILE(gs,x1,y1);               \
60     TILE(gs,x1,y1) = TILE(gs,x2,y2);      \
61     TILE(gs,x2,y2) = t;                   \
62 } while (0)
63
64 static int npoints(game_params *params, int nsel)
65 {
66     int sdiff = nsel - params->scoresub;
67     return (sdiff > 0) ? sdiff * sdiff : 0;
68 }
69
70 struct game_state {
71     struct game_params params;
72     int n;
73     int *tiles; /* colour only */
74     int score;
75     int complete, impossible;
76 };
77
78 static game_params *default_params(void)
79 {
80     game_params *ret = snew(game_params);
81     ret->w = 5;
82     ret->h = 5;
83     ret->ncols = 3;
84     ret->scoresub = 2;
85     return ret;
86 }
87
88 static const struct game_params samegame_presets[] = {
89     { 5, 5, 3, 2 },
90     { 10, 5, 3, 2 },
91     { 15, 10, 3, 2 },
92     { 15, 10, 4, 2 },
93     { 20, 15, 4, 2 }
94 };
95
96 static int game_fetch_preset(int i, char **name, game_params **params)
97 {
98     game_params *ret;
99     char str[80];
100
101     if (i < 0 || i >= lenof(samegame_presets))
102         return FALSE;
103
104     ret = snew(game_params);
105     *ret = samegame_presets[i];
106
107     sprintf(str, "%dx%d, %d colours", ret->w, ret->h, ret->ncols);
108
109     *name = dupstr(str);
110     *params = ret;
111     return TRUE;
112 }
113
114 static void free_params(game_params *params)
115 {
116     sfree(params);
117 }
118
119 static game_params *dup_params(game_params *params)
120 {
121     game_params *ret = snew(game_params);
122     *ret = *params;                    /* structure copy */
123     return ret;
124 }
125
126 static void decode_params(game_params *params, char const *string)
127 {
128     char const *p = string;
129
130     params->w = atoi(p);
131     while (*p && isdigit((unsigned char)*p)) p++;
132     if (*p == 'x') {
133         p++;
134         params->h = atoi(p);
135         while (*p && isdigit((unsigned char)*p)) p++;
136     } else {
137         params->h = params->w;
138     }
139     if (*p++ == 'c') {
140         params->ncols = atoi(p);
141         while (*p && isdigit((unsigned char)*p)) p++;
142     } else {
143         params->ncols = 3;
144     }
145     if (*p++ == 's') {
146         params->scoresub = atoi(p);
147         while (*p && isdigit((unsigned char)*p)) p++;
148     } else {
149         params->scoresub = 2;
150     }
151 }
152
153 static char *encode_params(game_params *params, int full)
154 {
155     char ret[80];
156
157     sprintf(ret, "%dx%dc%ds%d",
158             params->w, params->h, params->ncols, params->scoresub);
159     return dupstr(ret);
160 }
161
162 static config_item *game_configure(game_params *params)
163 {
164     config_item *ret;
165     char buf[80];
166
167     ret = snewn(5, config_item);
168
169     ret[0].name = "Width";
170     ret[0].type = C_STRING;
171     sprintf(buf, "%d", params->w);
172     ret[0].sval = dupstr(buf);
173     ret[0].ival = 0;
174
175     ret[1].name = "Height";
176     ret[1].type = C_STRING;
177     sprintf(buf, "%d", params->h);
178     ret[1].sval = dupstr(buf);
179     ret[1].ival = 0;
180
181     ret[2].name = "No. of colours";
182     ret[2].type = C_STRING;
183     sprintf(buf, "%d", params->ncols);
184     ret[2].sval = dupstr(buf);
185     ret[2].ival = 0;
186
187     ret[3].name = "Scoring system";
188     ret[3].type = C_CHOICES;
189     ret[3].sval = ":(n-1)^2:(n-2)^2";
190     ret[3].ival = params->scoresub-1;
191
192     ret[4].name = NULL;
193     ret[4].type = C_END;
194     ret[4].sval = NULL;
195     ret[4].ival = 0;
196
197     return ret;
198 }
199
200 static game_params *custom_params(config_item *cfg)
201 {
202     game_params *ret = snew(game_params);
203
204     ret->w = atoi(cfg[0].sval);
205     ret->h = atoi(cfg[1].sval);
206     ret->ncols = atoi(cfg[2].sval);
207     ret->scoresub = cfg[3].ival + 1;
208
209     return ret;
210 }
211
212 static char *validate_params(game_params *params)
213 {
214     if (params->w < 1 || params->h < 1)
215         return "Width and height must both be positive";
216     if (params->ncols < 2)
217         return "It's too easy with only one colour...";
218     if (params->ncols > 9)
219         return "Maximum of 9 colours";
220
221     /* ...and we must make sure we can generate at least 2 squares
222      * of each colour so it's theoretically soluble. */
223     if ((params->w * params->h) < (params->ncols * 2))
224         return "Too many colours makes given grid size impossible";
225
226     if ((params->scoresub < 1) || (params->scoresub > 2))
227         return "Scoring system not recognised";
228
229     return NULL;
230 }
231
232 /* Currently this is a very very dumb game-generation engine; it
233  * just picks randomly from the tile space. I had a look at a few
234  * other same game implementations, and none of them attempt to do
235  * anything to try and make sure the grid started off with a nice
236  * set of large blocks.
237  *
238  * It does at least make sure that there are >= 2 of each colour
239  * present at the start.
240  */
241
242 static char *new_game_desc(game_params *params, random_state *rs,
243                            game_aux_info **aux, int interactive)
244 {
245     char *ret;
246     int n, i, j, c, retlen, *tiles;
247
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 char *solve_game(game_state *state, game_state *currstate,
359                         game_aux_info *aux, 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     int xsel, ysel, displaysel;
392 };
393
394 static game_ui *new_ui(game_state *state)
395 {
396     game_ui *ui = snew(game_ui);
397
398     ui->params = state->params; /* structure copy */
399     ui->tiles = snewn(state->n, int);
400     memset(ui->tiles, 0, state->n*sizeof(int));
401     ui->nselected = 0;
402
403     ui->xsel = ui->ysel = ui->displaysel = 0;
404
405     return ui;
406 }
407
408 static void free_ui(game_ui *ui)
409 {
410     sfree(ui->tiles);
411     sfree(ui);
412 }
413
414 char *encode_ui(game_ui *ui)
415 {
416     return NULL;
417 }
418
419 void decode_ui(game_ui *ui, char *encoding)
420 {
421 }
422
423 static void sel_clear(game_ui *ui, game_state *state)
424 {
425     int i;
426
427     for (i = 0; i < state->n; i++)
428         ui->tiles[i] &= ~TILE_SELECTED;
429     ui->nselected = 0;
430 }
431
432
433 static void game_changed_state(game_ui *ui, game_state *oldstate,
434                                game_state *newstate)
435 {
436     sel_clear(ui, newstate);
437 }
438
439 static char *sel_movedesc(game_ui *ui, game_state *state)
440 {
441     int i;
442     char *ret, *sep, buf[80];
443     int retlen, retsize;
444
445     retsize = 256;
446     ret = snewn(retsize, char);
447     retlen = 0;
448     ret[retlen++] = 'M';
449     sep = "";
450
451     for (i = 0; i < state->n; i++) {
452         if (ui->tiles[i] & TILE_SELECTED) {
453             sprintf(buf, "%s%d", sep, i);
454             sep = ",";
455             if (retlen + strlen(buf) >= retsize) {
456                 retsize = retlen + strlen(buf) + 256;
457                 ret = sresize(ret, retsize, char);
458             }
459             strcpy(ret + retlen, buf);
460             retlen += strlen(buf);
461
462             ui->tiles[i] &= ~TILE_SELECTED;
463         }
464     }
465     ui->nselected = 0;
466
467     assert(retlen < retsize);
468     ret[retlen++] = '\0';
469     return sresize(ret, retlen, char);
470 }
471
472 static void sel_expand(game_ui *ui, game_state *state, int tx, int ty)
473 {
474     int ns = 1, nadded, x, y, c;
475
476     TILE(ui,tx,ty) |= TILE_SELECTED;
477     do {
478         nadded = 0;
479
480         for (x = 0; x < state->params.w; x++) {
481             for (y = 0; y < state->params.h; y++) {
482                 if (x == tx && y == ty) continue;
483                 if (ISSEL(ui,x,y)) continue;
484
485                 c = COL(state,x,y);
486                 if ((x > 0) &&
487                     ISSEL(ui,x-1,y) && COL(state,x-1,y) == c) {
488                     TILE(ui,x,y) |= TILE_SELECTED;
489                     nadded++;
490                     continue;
491                 }
492
493                 if ((x+1 < state->params.w) &&
494                     ISSEL(ui,x+1,y) && COL(state,x+1,y) == c) {
495                     TILE(ui,x,y) |= TILE_SELECTED;
496                     nadded++;
497                     continue;
498                 }
499
500                 if ((y > 0) &&
501                     ISSEL(ui,x,y-1) && COL(state,x,y-1) == c) {
502                     TILE(ui,x,y) |= TILE_SELECTED;
503                     nadded++;
504                     continue;
505                 }
506
507                 if ((y+1 < state->params.h) &&
508                     ISSEL(ui,x,y+1) && COL(state,x,y+1) == c) {
509                     TILE(ui,x,y) |= TILE_SELECTED;
510                     nadded++;
511                     continue;
512                 }
513             }
514         }
515         ns += nadded;
516     } while (nadded > 0);
517
518     if (ns > 1) {
519         ui->nselected = ns;
520     } else {
521         sel_clear(ui, state);
522     }
523 }
524
525 static int sg_emptycol(game_state *ret, int x)
526 {
527     int y;
528     for (y = 0; y < ret->params.h; y++) {
529         if (COL(ret,x,y)) return 0;
530     }
531     return 1;
532 }
533
534
535 static void sg_snuggle(game_state *ret)
536 {
537     int x,y, ndone;
538
539     /* make all unsupported tiles fall down. */
540     do {
541         ndone = 0;
542         for (x = 0; x < ret->params.w; x++) {
543             for (y = ret->params.h-1; y > 0; y--) {
544                 if (COL(ret,x,y) != 0) continue;
545                 if (COL(ret,x,y-1) != 0) {
546                     SWAPTILE(ret,x,y,x,y-1);
547                     ndone++;
548                 }
549             }
550         }
551     } while (ndone);
552
553     /* shuffle all columns as far left as they can go. */
554     do {
555         ndone = 0;
556         for (x = 0; x < ret->params.w-1; x++) {
557             if (sg_emptycol(ret,x) && !sg_emptycol(ret,x+1)) {
558                 ndone++;
559                 for (y = 0; y < ret->params.h; y++) {
560                     SWAPTILE(ret,x,y,x+1,y);
561                 }
562             }
563         }
564     } while (ndone);
565 }
566
567 static void sg_check(game_state *ret)
568 {
569     int x,y, complete = 1, impossible = 1;
570
571     for (x = 0; x < ret->params.w; x++) {
572         for (y = 0; y < ret->params.h; y++) {
573             if (COL(ret,x,y) == 0)
574                 continue;
575             complete = 0;
576             if (x+1 < ret->params.w) {
577                 if (COL(ret,x,y) == COL(ret,x+1,y))
578                     impossible = 0;
579             }
580             if (y+1 < ret->params.h) {
581                 if (COL(ret,x,y) == COL(ret,x,y+1))
582                     impossible = 0;
583             }
584         }
585     }
586     ret->complete = complete;
587     ret->impossible = impossible;
588 }
589
590 struct game_drawstate {
591     int started, bgcolour;
592     int tileinner, tilegap;
593     int *tiles; /* contains colour and SELECTED. */
594 };
595
596 static game_state *execute_move(game_state *from, char *move);
597
598 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
599                             int x, int y, int button)
600 {
601     int tx, ty;
602     char *ret = "";
603
604     ui->displaysel = 0;
605
606     if (button == RIGHT_BUTTON || button == LEFT_BUTTON) {
607         tx = FROMCOORD(x); ty= FROMCOORD(y);
608     } else if (button == CURSOR_UP || button == CURSOR_DOWN ||
609                button == CURSOR_LEFT || button == CURSOR_RIGHT) {
610         int dx = 0, dy = 0;
611         ui->displaysel = 1;
612         dx = (button == CURSOR_LEFT) ? -1 : ((button == CURSOR_RIGHT) ? +1 : 0);
613         dy = (button == CURSOR_DOWN) ? +1 : ((button == CURSOR_UP)    ? -1 : 0);
614         ui->xsel = (ui->xsel + state->params.w + dx) % state->params.w;
615         ui->ysel = (ui->ysel + state->params.h + dy) % state->params.h;
616         return ret;
617     } else if (button == CURSOR_SELECT || button == ' ' || button == '\r' ||
618                button == '\n') {
619         ui->displaysel = 1;
620         tx = ui->xsel;
621         ty = ui->ysel;
622     } else
623         return NULL;
624
625     if (tx < 0 || tx >= state->params.w || ty < 0 || ty >= state->params.h)
626         return NULL;
627     if (COL(state, tx, ty) == 0) return NULL;
628
629     if (ISSEL(ui,tx,ty)) {
630         if (button == RIGHT_BUTTON)
631             sel_clear(ui, state);
632         else {
633             game_state *tmp;
634
635             ret = sel_movedesc(ui, state);
636
637             /*
638              * Unfortunately, we must check for completeness or
639              * impossibility now, in order to update the game_ui;
640              * and we can't do that without constructing the new
641              * grid. Sigh.
642              */
643             tmp = execute_move(state, ret);
644             if (tmp->complete || tmp->impossible)
645                 ui->displaysel = 0;
646             free_game(tmp);
647         }
648     } else {
649         sel_clear(ui, state); /* might be no-op */
650         sel_expand(ui, state, tx, ty);
651     }
652
653     return ret;
654 }
655
656 static game_state *execute_move(game_state *from, char *move)
657 {
658     int i, n;
659     game_state *ret;
660
661     if (move[0] == 'M') {
662         ret = dup_game(from);
663
664         n = 0;
665         move++;
666
667         while (*move) {
668             i = atoi(move);
669             if (i < 0 || i >= ret->n) {
670                 free_game(ret);
671                 return NULL;
672             }
673             n++;
674             ret->tiles[i] = 0;
675
676             while (*move && isdigit((unsigned char)*move)) move++;
677             if (*move == ',') move++;
678         }
679
680         ret->score += npoints(&ret->params, n);
681
682         sg_snuggle(ret); /* shifts blanks down and to the left */
683         sg_check(ret);   /* checks for completeness or impossibility */
684
685         return ret;
686     } else
687         return NULL;                   /* couldn't parse move string */
688 }
689
690 /* ----------------------------------------------------------------------
691  * Drawing routines.
692  */
693
694 static void game_size(game_params *params, game_drawstate *ds, int *x, int *y,
695                       int expand)
696 {
697     int tsx, tsy, ts;
698
699     /*
700      * We could choose the tile gap dynamically as well if we
701      * wanted to; for example, at low tile sizes it might be
702      * sensible to leave it out completely. However, for the moment
703      * and for the sake of simplicity I'm just going to fix it at
704      * 2.
705      */
706     ds->tilegap = 2;
707
708     /*
709      * Each window dimension equals the tile size (inner plus gap)
710      * times the grid dimension, plus another tile size (border is
711      * half the width of a tile), minus one tile gap.
712      * 
713      * We must cast to unsigned before adding to *x and *y, since
714      * they might be INT_MAX!
715      */
716     tsx = (unsigned)(*x + ds->tilegap) / (params->w + 1);
717     tsy = (unsigned)(*y + ds->tilegap) / (params->h + 1);
718
719     ts = min(tsx, tsy);
720     if (expand)
721         ds->tileinner = ts - ds->tilegap;
722     else
723         ds->tileinner = min(ts, PREFERRED_TILE_SIZE) - ds->tilegap;
724
725     *x = TILE_SIZE * params->w + 2 * BORDER - TILE_GAP;
726     *y = TILE_SIZE * params->h + 2 * BORDER - TILE_GAP;
727 }
728
729 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
730 {
731     float *ret = snewn(3 * NCOLOURS, float);
732
733     frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
734
735     ret[COL_1 * 3 + 0] = 0.0F;
736     ret[COL_1 * 3 + 1] = 0.0F;
737     ret[COL_1 * 3 + 2] = 1.0F;
738
739     ret[COL_2 * 3 + 0] = 0.0F;
740     ret[COL_2 * 3 + 1] = 0.5F;
741     ret[COL_2 * 3 + 2] = 0.0F;
742
743     ret[COL_3 * 3 + 0] = 1.0F;
744     ret[COL_3 * 3 + 1] = 0.0F;
745     ret[COL_3 * 3 + 2] = 0.0F;
746
747     ret[COL_4 * 3 + 0] = 1.0F;
748     ret[COL_4 * 3 + 1] = 1.0F;
749     ret[COL_4 * 3 + 2] = 0.0F;
750
751     ret[COL_5 * 3 + 0] = 1.0F;
752     ret[COL_5 * 3 + 1] = 0.0F;
753     ret[COL_5 * 3 + 2] = 1.0F;
754
755     ret[COL_6 * 3 + 0] = 0.0F;
756     ret[COL_6 * 3 + 1] = 1.0F;
757     ret[COL_6 * 3 + 2] = 1.0F;
758
759     ret[COL_7 * 3 + 0] = 0.5F;
760     ret[COL_7 * 3 + 1] = 0.5F;
761     ret[COL_7 * 3 + 2] = 1.0F;
762
763     ret[COL_8 * 3 + 0] = 0.5F;
764     ret[COL_8 * 3 + 1] = 1.0F;
765     ret[COL_8 * 3 + 2] = 0.5F;
766
767     ret[COL_9 * 3 + 0] = 1.0F;
768     ret[COL_9 * 3 + 1] = 0.5F;
769     ret[COL_9 * 3 + 2] = 0.5F;
770
771     ret[COL_IMPOSSIBLE * 3 + 0] = 0.0F;
772     ret[COL_IMPOSSIBLE * 3 + 1] = 0.0F;
773     ret[COL_IMPOSSIBLE * 3 + 2] = 0.0F;
774
775     ret[COL_SEL * 3 + 0] = 1.0F;
776     ret[COL_SEL * 3 + 1] = 1.0F;
777     ret[COL_SEL * 3 + 2] = 1.0F;
778
779     ret[COL_HIGHLIGHT * 3 + 0] = 1.0F;
780     ret[COL_HIGHLIGHT * 3 + 1] = 1.0F;
781     ret[COL_HIGHLIGHT * 3 + 2] = 1.0F;
782
783     ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
784     ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
785     ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
786
787     *ncolours = NCOLOURS;
788     return ret;
789 }
790
791 static game_drawstate *game_new_drawstate(game_state *state)
792 {
793     struct game_drawstate *ds = snew(struct game_drawstate);
794     int i;
795
796     ds->started = 0;
797     ds->tileinner = ds->tilegap = 0;   /* not decided yet */
798     ds->tiles = snewn(state->n, int);
799     for (i = 0; i < state->n; i++)
800         ds->tiles[i] = -1;
801
802     return ds;
803 }
804
805 static void game_free_drawstate(game_drawstate *ds)
806 {
807     sfree(ds->tiles);
808     sfree(ds);
809 }
810
811 /* Drawing routing for the tile at (x,y) is responsible for drawing
812  * itself and the gaps to its right and below. If we're the same colour
813  * as the tile to our right, then we fill in the gap; ditto below, and if
814  * both then we fill the teeny tiny square in the corner as well.
815  */
816
817 static void tile_redraw(frontend *fe, game_drawstate *ds,
818                         int x, int y, int dright, int dbelow,
819                         int tile, int bgcolour)
820 {
821     int outer = bgcolour, inner = outer, col = tile & TILE_COLMASK;
822
823     if (col) {
824         if (tile & TILE_IMPOSSIBLE) {
825             outer = col;
826             inner = COL_IMPOSSIBLE;
827         } else if (tile & TILE_SELECTED) {
828             outer = COL_SEL;
829             inner = col;
830         } else {
831             outer = inner = col;
832         }
833     }
834     draw_rect(fe, COORD(x), COORD(y), TILE_INNER, TILE_INNER, outer);
835     draw_rect(fe, COORD(x)+TILE_INNER/4, COORD(y)+TILE_INNER/4,
836               TILE_INNER/2, TILE_INNER/2, inner);
837
838     if (dright)
839         draw_rect(fe, COORD(x)+TILE_INNER, COORD(y), TILE_GAP, TILE_INNER,
840                   (tile & TILE_JOINRIGHT) ? outer : bgcolour);
841     if (dbelow)
842         draw_rect(fe, COORD(x), COORD(y)+TILE_INNER, TILE_INNER, TILE_GAP,
843                   (tile & TILE_JOINDOWN) ? outer : bgcolour);
844     if (dright && dbelow)
845         draw_rect(fe, COORD(x)+TILE_INNER, COORD(y)+TILE_INNER, TILE_GAP, TILE_GAP,
846                   (tile & TILE_JOINDIAG) ? outer : bgcolour);
847
848     if (tile & TILE_HASSEL) {
849         int sx = COORD(x)+2, sy = COORD(y)+2, ssz = TILE_INNER-5;
850         int scol = (outer == COL_SEL) ? COL_LOWLIGHT : COL_HIGHLIGHT;
851         draw_line(fe, sx,     sy,     sx+ssz, sy,     scol);
852         draw_line(fe, sx+ssz, sy,     sx+ssz, sy+ssz, scol);
853         draw_line(fe, sx+ssz, sy+ssz, sx,     sy+ssz, scol);
854         draw_line(fe, sx,     sy+ssz, sx,     sy,     scol);
855     }
856
857     draw_update(fe, COORD(x), COORD(y), TILE_SIZE, TILE_SIZE);
858 }
859
860 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
861                         game_state *state, int dir, game_ui *ui,
862                         float animtime, float flashtime)
863 {
864     int bgcolour, x, y;
865
866     /* This was entirely cloned from fifteen.c; it should probably be
867      * moved into some generic 'draw-recessed-rectangle' utility fn. */
868     if (!ds->started) {
869         int coords[10];
870
871         draw_rect(fe, 0, 0,
872                   TILE_SIZE * state->params.w + 2 * BORDER,
873                   TILE_SIZE * state->params.h + 2 * BORDER, COL_BACKGROUND);
874         draw_update(fe, 0, 0,
875                     TILE_SIZE * state->params.w + 2 * BORDER,
876                     TILE_SIZE * state->params.h + 2 * BORDER);
877
878         /*
879          * Recessed area containing the whole puzzle.
880          */
881         coords[0] = COORD(state->params.w) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
882         coords[1] = COORD(state->params.h) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
883         coords[2] = COORD(state->params.w) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
884         coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
885         coords[4] = coords[2] - TILE_SIZE;
886         coords[5] = coords[3] + TILE_SIZE;
887         coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
888         coords[9] = COORD(state->params.h) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
889         coords[6] = coords[8] + TILE_SIZE;
890         coords[7] = coords[9] - TILE_SIZE;
891         draw_polygon(fe, coords, 5, TRUE, COL_HIGHLIGHT);
892         draw_polygon(fe, coords, 5, FALSE, COL_HIGHLIGHT);
893
894         coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
895         coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
896         draw_polygon(fe, coords, 5, TRUE, COL_LOWLIGHT);
897         draw_polygon(fe, coords, 5, FALSE, COL_LOWLIGHT);
898
899         ds->started = 1;
900     }
901
902     if (flashtime > 0.0) {
903         int frame = (int)(flashtime / FLASH_FRAME);
904         bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
905     } else
906         bgcolour = COL_BACKGROUND;
907
908     for (x = 0; x < state->params.w; x++) {
909         for (y = 0; y < state->params.h; y++) {
910             int i = (state->params.w * y) + x;
911             int col = COL(state,x,y), tile = col;
912             int dright = (x+1 < state->params.w);
913             int dbelow = (y+1 < state->params.h);
914
915             tile |= ISSEL(ui,x,y);
916             if (state->impossible)
917                 tile |= TILE_IMPOSSIBLE;
918             if (dright && COL(state,x+1,y) == col)
919                 tile |= TILE_JOINRIGHT;
920             if (dbelow && COL(state,x,y+1) == col)
921                 tile |= TILE_JOINDOWN;
922             if ((tile & TILE_JOINRIGHT) && (tile & TILE_JOINDOWN) &&
923                 COL(state,x+1,y+1) == col)
924                 tile |= TILE_JOINDIAG;
925
926             if (ui->displaysel && ui->xsel == x && ui->ysel == y)
927                 tile |= TILE_HASSEL;
928
929             /* For now we're never expecting oldstate at all (because we have
930              * no animation); when we do we might well want to be looking
931              * at the tile colours from oldstate, not state. */
932             if ((oldstate && COL(oldstate,x,y) != col) ||
933                 (flashtime > 0.0) ||
934                 (ds->bgcolour != bgcolour) ||
935                 (tile != ds->tiles[i])) {
936                 tile_redraw(fe, ds, x, y, dright, dbelow, tile, bgcolour);
937                 ds->tiles[i] = tile;
938             }
939         }
940     }
941     ds->bgcolour = bgcolour;
942
943     {
944         char status[255], score[80];
945
946         sprintf(score, "Score: %d", state->score);
947
948         if (state->complete)
949             sprintf(status, "COMPLETE! %s", score);
950         else if (state->impossible)
951             sprintf(status, "Cannot move! %s", score);
952         else if (ui->nselected)
953             sprintf(status, "%s  Selected: %d (%d)",
954                     score, ui->nselected, npoints(&state->params, ui->nselected));
955         else
956             sprintf(status, "%s", score);
957         status_bar(fe, status);
958     }
959 }
960
961 static float game_anim_length(game_state *oldstate, game_state *newstate,
962                               int dir, game_ui *ui)
963 {
964     return 0.0F;
965 }
966
967 static float game_flash_length(game_state *oldstate, game_state *newstate,
968                                int dir, game_ui *ui)
969 {
970     if ((!oldstate->complete && newstate->complete) ||
971         (!oldstate->impossible && newstate->impossible))
972         return 2 * FLASH_FRAME;
973     else
974         return 0.0F;
975 }
976
977 static int game_wants_statusbar(void)
978 {
979     return TRUE;
980 }
981
982 static int game_timing_state(game_state *state)
983 {
984     return TRUE;
985 }
986
987 #ifdef COMBINED
988 #define thegame samegame
989 #endif
990
991 const struct game thegame = {
992     "Same Game", "games.samegame",
993     default_params,
994     game_fetch_preset,
995     decode_params,
996     encode_params,
997     free_params,
998     dup_params,
999     TRUE, game_configure, custom_params,
1000     validate_params,
1001     new_game_desc,
1002     game_free_aux_info,
1003     validate_desc,
1004     new_game,
1005     dup_game,
1006     free_game,
1007     FALSE, solve_game,
1008     TRUE, game_text_format,
1009     new_ui,
1010     free_ui,
1011     encode_ui,
1012     decode_ui,
1013     game_changed_state,
1014     interpret_move,
1015     execute_move,
1016     game_size,
1017     game_colours,
1018     game_new_drawstate,
1019     game_free_drawstate,
1020     game_redraw,
1021     game_anim_length,
1022     game_flash_length,
1023     game_wants_statusbar,
1024     FALSE, game_timing_state,
1025     0,                                 /* mouse_priorities */
1026 };