Subtle UI change to Mines. Although I mostly find the unified left-
[sgt/puzzles] / samegame.c
CommitLineData
6bbab0fe 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
31enum {
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) */
39struct 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). */
f1359c5e 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 */
d951510d 52#define TILE_IMPOSSIBLE 0x2000 /* used in drawstate */
6bbab0fe 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
64static int npoints(game_params *params, int nsel)
65{
66 int sdiff = nsel - params->scoresub;
67 return (sdiff > 0) ? sdiff * sdiff : 0;
68}
69
70struct game_state {
71 struct game_params params;
72 int n;
73 int *tiles; /* colour only */
74 int score;
75 int complete, impossible;
76};
77
78static 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
88static 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
96static 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
114static void free_params(game_params *params)
115{
116 sfree(params);
117}
118
119static 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
126static 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
153static 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
162static 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
200static 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
3ff276f2 212static char *validate_params(game_params *params, int full)
6bbab0fe 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
242static char *new_game_desc(game_params *params, random_state *rs,
c566778e 243 char **aux, int interactive)
6bbab0fe 244{
245 char *ret;
246 int n, i, j, c, retlen, *tiles;
247
6bbab0fe 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
6bbab0fe 285static char *validate_desc(game_params *params, char *desc)
286{
287 int area = params->w * params->h, i;
288 char *p = desc;
289
290 for (i = 0; i < area; i++) {
291 char *q = p;
292 int n;
293
294 if (!isdigit(*p))
295 return "Not enough numbers in string";
296 while (isdigit(*p)) p++;
297
298 if (i < area-1 && *p != ',')
299 return "Expected comma after number";
300 else if (i == area-1 && *p)
301 return "Excess junk at end of string";
302
303 n = atoi(q);
304 if (n < 0 || n > params->ncols)
305 return "Colour out of range";
306
307 if (*p) p++; /* eat comma */
308 }
309 return NULL;
310}
311
312static game_state *new_game(midend_data *me, game_params *params, char *desc)
313{
314 game_state *state = snew(game_state);
315 char *p = desc;
316 int i;
317
318 state->params = *params; /* struct copy */
319 state->n = state->params.w * state->params.h;
320 state->tiles = snewn(state->n, int);
321
322 for (i = 0; i < state->n; i++) {
323 assert(*p);
324 state->tiles[i] = atoi(p);
325 while (*p && *p != ',')
326 p++;
327 if (*p) p++; /* eat comma */
328 }
329 state->complete = state->impossible = 0;
330 state->score = 0;
331
332 return state;
333}
334
335static game_state *dup_game(game_state *state)
336{
337 game_state *ret = snew(game_state);
338
339 *ret = *state; /* structure copy, except... */
340
341 ret->tiles = snewn(state->n, int);
342 memcpy(ret->tiles, state->tiles, state->n * sizeof(int));
343
344 return ret;
345}
346
347static void free_game(game_state *state)
348{
349 sfree(state->tiles);
350 sfree(state);
351}
352
df11cd4e 353static char *solve_game(game_state *state, game_state *currstate,
c566778e 354 char *aux, char **error)
6bbab0fe 355{
356 return NULL;
357}
358
359static char *game_text_format(game_state *state)
360{
361 char *ret, *p;
362 int x, y, maxlen;
363
364 maxlen = state->params.h * (state->params.w + 1);
365 ret = snewn(maxlen+1, char);
366 p = ret;
367
368 for (y = 0; y < state->params.h; y++) {
369 for (x = 0; x < state->params.w; x++) {
370 int t = TILE(state,x,y);
371 if (t <= 0) *p++ = ' ';
372 else if (t < 10) *p++ = '0'+t;
373 else *p++ = 'a'+(t-10);
374 }
375 *p++ = '\n';
376 }
377 assert(p - ret == maxlen);
378 *p = '\0';
379 return ret;
380}
381
382struct game_ui {
383 struct game_params params;
384 int *tiles; /* selected-ness only */
385 int nselected;
f1359c5e 386 int xsel, ysel, displaysel;
6bbab0fe 387};
388
389static game_ui *new_ui(game_state *state)
390{
391 game_ui *ui = snew(game_ui);
392
393 ui->params = state->params; /* structure copy */
394 ui->tiles = snewn(state->n, int);
395 memset(ui->tiles, 0, state->n*sizeof(int));
396 ui->nselected = 0;
397
f1359c5e 398 ui->xsel = ui->ysel = ui->displaysel = 0;
399
6bbab0fe 400 return ui;
401}
402
403static void free_ui(game_ui *ui)
404{
405 sfree(ui->tiles);
406 sfree(ui);
407}
408
844f605f 409static char *encode_ui(game_ui *ui)
ae8290c6 410{
411 return NULL;
412}
413
844f605f 414static void decode_ui(game_ui *ui, char *encoding)
ae8290c6 415{
416}
417
6bbab0fe 418static void sel_clear(game_ui *ui, game_state *state)
419{
420 int i;
421
422 for (i = 0; i < state->n; i++)
423 ui->tiles[i] &= ~TILE_SELECTED;
424 ui->nselected = 0;
6bbab0fe 425}
426
427
428static void game_changed_state(game_ui *ui, game_state *oldstate,
429 game_state *newstate)
430{
431 sel_clear(ui, newstate);
432}
433
df11cd4e 434static char *sel_movedesc(game_ui *ui, game_state *state)
6bbab0fe 435{
df11cd4e 436 int i;
437 char *ret, *sep, buf[80];
438 int retlen, retsize;
6bbab0fe 439
df11cd4e 440 retsize = 256;
441 ret = snewn(retsize, char);
442 retlen = 0;
443 ret[retlen++] = 'M';
444 sep = "";
6bbab0fe 445
446 for (i = 0; i < state->n; i++) {
447 if (ui->tiles[i] & TILE_SELECTED) {
df11cd4e 448 sprintf(buf, "%s%d", sep, i);
449 sep = ",";
450 if (retlen + strlen(buf) >= retsize) {
451 retsize = retlen + strlen(buf) + 256;
452 ret = sresize(ret, retsize, char);
453 }
454 strcpy(ret + retlen, buf);
455 retlen += strlen(buf);
456
6bbab0fe 457 ui->tiles[i] &= ~TILE_SELECTED;
458 }
459 }
460 ui->nselected = 0;
df11cd4e 461
462 assert(retlen < retsize);
463 ret[retlen++] = '\0';
464 return sresize(ret, retlen, char);
6bbab0fe 465}
466
467static void sel_expand(game_ui *ui, game_state *state, int tx, int ty)
468{
469 int ns = 1, nadded, x, y, c;
470
471 TILE(ui,tx,ty) |= TILE_SELECTED;
6bbab0fe 472 do {
473 nadded = 0;
474
475 for (x = 0; x < state->params.w; x++) {
476 for (y = 0; y < state->params.h; y++) {
477 if (x == tx && y == ty) continue;
478 if (ISSEL(ui,x,y)) continue;
479
480 c = COL(state,x,y);
481 if ((x > 0) &&
482 ISSEL(ui,x-1,y) && COL(state,x-1,y) == c) {
483 TILE(ui,x,y) |= TILE_SELECTED;
484 nadded++;
485 continue;
486 }
487
488 if ((x+1 < state->params.w) &&
489 ISSEL(ui,x+1,y) && COL(state,x+1,y) == c) {
490 TILE(ui,x,y) |= TILE_SELECTED;
491 nadded++;
492 continue;
493 }
494
495 if ((y > 0) &&
496 ISSEL(ui,x,y-1) && COL(state,x,y-1) == c) {
497 TILE(ui,x,y) |= TILE_SELECTED;
498 nadded++;
499 continue;
500 }
501
502 if ((y+1 < state->params.h) &&
503 ISSEL(ui,x,y+1) && COL(state,x,y+1) == c) {
504 TILE(ui,x,y) |= TILE_SELECTED;
505 nadded++;
506 continue;
507 }
508 }
509 }
510 ns += nadded;
6bbab0fe 511 } while (nadded > 0);
512
513 if (ns > 1) {
514 ui->nselected = ns;
515 } else {
516 sel_clear(ui, state);
517 }
518}
519
520static int sg_emptycol(game_state *ret, int x)
521{
522 int y;
523 for (y = 0; y < ret->params.h; y++) {
524 if (COL(ret,x,y)) return 0;
525 }
526 return 1;
527}
528
529
530static void sg_snuggle(game_state *ret)
531{
532 int x,y, ndone;
533
534 /* make all unsupported tiles fall down. */
535 do {
536 ndone = 0;
537 for (x = 0; x < ret->params.w; x++) {
538 for (y = ret->params.h-1; y > 0; y--) {
539 if (COL(ret,x,y) != 0) continue;
540 if (COL(ret,x,y-1) != 0) {
541 SWAPTILE(ret,x,y,x,y-1);
542 ndone++;
543 }
544 }
545 }
546 } while (ndone);
547
548 /* shuffle all columns as far left as they can go. */
549 do {
550 ndone = 0;
551 for (x = 0; x < ret->params.w-1; x++) {
552 if (sg_emptycol(ret,x) && !sg_emptycol(ret,x+1)) {
6bbab0fe 553 ndone++;
554 for (y = 0; y < ret->params.h; y++) {
555 SWAPTILE(ret,x,y,x+1,y);
556 }
557 }
558 }
559 } while (ndone);
560}
561
562static void sg_check(game_state *ret)
563{
564 int x,y, complete = 1, impossible = 1;
565
566 for (x = 0; x < ret->params.w; x++) {
567 for (y = 0; y < ret->params.h; y++) {
568 if (COL(ret,x,y) == 0)
569 continue;
570 complete = 0;
571 if (x+1 < ret->params.w) {
572 if (COL(ret,x,y) == COL(ret,x+1,y))
573 impossible = 0;
574 }
575 if (y+1 < ret->params.h) {
576 if (COL(ret,x,y) == COL(ret,x,y+1))
577 impossible = 0;
578 }
579 }
580 }
581 ret->complete = complete;
582 ret->impossible = impossible;
583}
584
585struct game_drawstate {
586 int started, bgcolour;
587 int tileinner, tilegap;
588 int *tiles; /* contains colour and SELECTED. */
589};
590
df11cd4e 591static game_state *execute_move(game_state *from, char *move);
592
593static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
594 int x, int y, int button)
6bbab0fe 595{
596 int tx, ty;
df11cd4e 597 char *ret = "";
6bbab0fe 598
f1359c5e 599 ui->displaysel = 0;
600
601 if (button == RIGHT_BUTTON || button == LEFT_BUTTON) {
602 tx = FROMCOORD(x); ty= FROMCOORD(y);
603 } else if (button == CURSOR_UP || button == CURSOR_DOWN ||
604 button == CURSOR_LEFT || button == CURSOR_RIGHT) {
605 int dx = 0, dy = 0;
606 ui->displaysel = 1;
607 dx = (button == CURSOR_LEFT) ? -1 : ((button == CURSOR_RIGHT) ? +1 : 0);
608 dy = (button == CURSOR_DOWN) ? +1 : ((button == CURSOR_UP) ? -1 : 0);
df11cd4e 609 ui->xsel = (ui->xsel + state->params.w + dx) % state->params.w;
610 ui->ysel = (ui->ysel + state->params.h + dy) % state->params.h;
f1359c5e 611 return ret;
612 } else if (button == CURSOR_SELECT || button == ' ' || button == '\r' ||
613 button == '\n') {
614 ui->displaysel = 1;
615 tx = ui->xsel;
616 ty = ui->ysel;
f1359c5e 617 } else
6bbab0fe 618 return NULL;
619
df11cd4e 620 if (tx < 0 || tx >= state->params.w || ty < 0 || ty >= state->params.h)
6bbab0fe 621 return NULL;
df11cd4e 622 if (COL(state, tx, ty) == 0) return NULL;
6bbab0fe 623
624 if (ISSEL(ui,tx,ty)) {
625 if (button == RIGHT_BUTTON)
df11cd4e 626 sel_clear(ui, state);
6bbab0fe 627 else {
df11cd4e 628 game_state *tmp;
629
630 ret = sel_movedesc(ui, state);
631
632 /*
633 * Unfortunately, we must check for completeness or
634 * impossibility now, in order to update the game_ui;
635 * and we can't do that without constructing the new
636 * grid. Sigh.
637 */
638 tmp = execute_move(state, ret);
639 if (tmp->complete || tmp->impossible)
640 ui->displaysel = 0;
641 free_game(tmp);
6bbab0fe 642 }
643 } else {
df11cd4e 644 sel_clear(ui, state); /* might be no-op */
645 sel_expand(ui, state, tx, ty);
6bbab0fe 646 }
647
648 return ret;
649}
650
df11cd4e 651static game_state *execute_move(game_state *from, char *move)
652{
653 int i, n;
654 game_state *ret;
655
656 if (move[0] == 'M') {
657 ret = dup_game(from);
658
659 n = 0;
660 move++;
661
662 while (*move) {
663 i = atoi(move);
664 if (i < 0 || i >= ret->n) {
665 free_game(ret);
666 return NULL;
667 }
668 n++;
669 ret->tiles[i] = 0;
670
671 while (*move && isdigit((unsigned char)*move)) move++;
672 if (*move == ',') move++;
673 }
674
675 ret->score += npoints(&ret->params, n);
676
677 sg_snuggle(ret); /* shifts blanks down and to the left */
678 sg_check(ret); /* checks for completeness or impossibility */
679
680 return ret;
681 } else
682 return NULL; /* couldn't parse move string */
683}
684
6bbab0fe 685/* ----------------------------------------------------------------------
686 * Drawing routines.
687 */
688
1f3ee4ee 689static void game_set_size(game_drawstate *ds, game_params *params,
690 int tilesize)
691{
6bbab0fe 692 ds->tilegap = 2;
1f3ee4ee 693 ds->tileinner = tilesize - ds->tilegap;
694}
6bbab0fe 695
1f3ee4ee 696static void game_compute_size(game_params *params, int tilesize,
697 int *x, int *y)
698{
699 /* Ick: fake up tile size variables for macro expansion purposes */
700 game_drawstate ads, *ds = &ads;
701 game_set_size(ds, params, tilesize);
6bbab0fe 702
703 *x = TILE_SIZE * params->w + 2 * BORDER - TILE_GAP;
704 *y = TILE_SIZE * params->h + 2 * BORDER - TILE_GAP;
705}
706
707static float *game_colours(frontend *fe, game_state *state, int *ncolours)
708{
709 float *ret = snewn(3 * NCOLOURS, float);
710
711 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
712
713 ret[COL_1 * 3 + 0] = 0.0F;
714 ret[COL_1 * 3 + 1] = 0.0F;
715 ret[COL_1 * 3 + 2] = 1.0F;
716
717 ret[COL_2 * 3 + 0] = 0.0F;
718 ret[COL_2 * 3 + 1] = 0.5F;
719 ret[COL_2 * 3 + 2] = 0.0F;
720
721 ret[COL_3 * 3 + 0] = 1.0F;
722 ret[COL_3 * 3 + 1] = 0.0F;
723 ret[COL_3 * 3 + 2] = 0.0F;
724
367cfc41 725 ret[COL_4 * 3 + 0] = 1.0F;
726 ret[COL_4 * 3 + 1] = 1.0F;
727 ret[COL_4 * 3 + 2] = 0.0F;
6bbab0fe 728
367cfc41 729 ret[COL_5 * 3 + 0] = 1.0F;
730 ret[COL_5 * 3 + 1] = 0.0F;
731 ret[COL_5 * 3 + 2] = 1.0F;
6bbab0fe 732
367cfc41 733 ret[COL_6 * 3 + 0] = 0.0F;
734 ret[COL_6 * 3 + 1] = 1.0F;
735 ret[COL_6 * 3 + 2] = 1.0F;
6bbab0fe 736
367cfc41 737 ret[COL_7 * 3 + 0] = 0.5F;
738 ret[COL_7 * 3 + 1] = 0.5F;
739 ret[COL_7 * 3 + 2] = 1.0F;
6bbab0fe 740
367cfc41 741 ret[COL_8 * 3 + 0] = 0.5F;
742 ret[COL_8 * 3 + 1] = 1.0F;
743 ret[COL_8 * 3 + 2] = 0.5F;
6bbab0fe 744
367cfc41 745 ret[COL_9 * 3 + 0] = 1.0F;
746 ret[COL_9 * 3 + 1] = 0.5F;
747 ret[COL_9 * 3 + 2] = 0.5F;
6bbab0fe 748
749 ret[COL_IMPOSSIBLE * 3 + 0] = 0.0F;
750 ret[COL_IMPOSSIBLE * 3 + 1] = 0.0F;
751 ret[COL_IMPOSSIBLE * 3 + 2] = 0.0F;
752
753 ret[COL_SEL * 3 + 0] = 1.0F;
754 ret[COL_SEL * 3 + 1] = 1.0F;
755 ret[COL_SEL * 3 + 2] = 1.0F;
756
757 ret[COL_HIGHLIGHT * 3 + 0] = 1.0F;
758 ret[COL_HIGHLIGHT * 3 + 1] = 1.0F;
759 ret[COL_HIGHLIGHT * 3 + 2] = 1.0F;
760
761 ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
762 ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
763 ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
764
765 *ncolours = NCOLOURS;
766 return ret;
767}
768
769static game_drawstate *game_new_drawstate(game_state *state)
770{
771 struct game_drawstate *ds = snew(struct game_drawstate);
772 int i;
773
774 ds->started = 0;
775 ds->tileinner = ds->tilegap = 0; /* not decided yet */
776 ds->tiles = snewn(state->n, int);
777 for (i = 0; i < state->n; i++)
778 ds->tiles[i] = -1;
779
780 return ds;
781}
782
783static void game_free_drawstate(game_drawstate *ds)
784{
785 sfree(ds->tiles);
786 sfree(ds);
787}
788
789/* Drawing routing for the tile at (x,y) is responsible for drawing
790 * itself and the gaps to its right and below. If we're the same colour
791 * as the tile to our right, then we fill in the gap; ditto below, and if
792 * both then we fill the teeny tiny square in the corner as well.
793 */
794
795static void tile_redraw(frontend *fe, game_drawstate *ds,
796 int x, int y, int dright, int dbelow,
d951510d 797 int tile, int bgcolour)
6bbab0fe 798{
799 int outer = bgcolour, inner = outer, col = tile & TILE_COLMASK;
800
801 if (col) {
d951510d 802 if (tile & TILE_IMPOSSIBLE) {
6bbab0fe 803 outer = col;
804 inner = COL_IMPOSSIBLE;
805 } else if (tile & TILE_SELECTED) {
806 outer = COL_SEL;
807 inner = col;
808 } else {
809 outer = inner = col;
810 }
811 }
812 draw_rect(fe, COORD(x), COORD(y), TILE_INNER, TILE_INNER, outer);
813 draw_rect(fe, COORD(x)+TILE_INNER/4, COORD(y)+TILE_INNER/4,
814 TILE_INNER/2, TILE_INNER/2, inner);
815
816 if (dright)
817 draw_rect(fe, COORD(x)+TILE_INNER, COORD(y), TILE_GAP, TILE_INNER,
818 (tile & TILE_JOINRIGHT) ? outer : bgcolour);
819 if (dbelow)
820 draw_rect(fe, COORD(x), COORD(y)+TILE_INNER, TILE_INNER, TILE_GAP,
821 (tile & TILE_JOINDOWN) ? outer : bgcolour);
822 if (dright && dbelow)
823 draw_rect(fe, COORD(x)+TILE_INNER, COORD(y)+TILE_INNER, TILE_GAP, TILE_GAP,
824 (tile & TILE_JOINDIAG) ? outer : bgcolour);
825
f1359c5e 826 if (tile & TILE_HASSEL) {
827 int sx = COORD(x)+2, sy = COORD(y)+2, ssz = TILE_INNER-5;
828 int scol = (outer == COL_SEL) ? COL_LOWLIGHT : COL_HIGHLIGHT;
829 draw_line(fe, sx, sy, sx+ssz, sy, scol);
830 draw_line(fe, sx+ssz, sy, sx+ssz, sy+ssz, scol);
831 draw_line(fe, sx+ssz, sy+ssz, sx, sy+ssz, scol);
832 draw_line(fe, sx, sy+ssz, sx, sy, scol);
833 }
834
6bbab0fe 835 draw_update(fe, COORD(x), COORD(y), TILE_SIZE, TILE_SIZE);
836}
837
838static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
839 game_state *state, int dir, game_ui *ui,
840 float animtime, float flashtime)
841{
842 int bgcolour, x, y;
843
6bbab0fe 844 /* This was entirely cloned from fifteen.c; it should probably be
845 * moved into some generic 'draw-recessed-rectangle' utility fn. */
846 if (!ds->started) {
847 int coords[10];
848
849 draw_rect(fe, 0, 0,
850 TILE_SIZE * state->params.w + 2 * BORDER,
851 TILE_SIZE * state->params.h + 2 * BORDER, COL_BACKGROUND);
852 draw_update(fe, 0, 0,
853 TILE_SIZE * state->params.w + 2 * BORDER,
854 TILE_SIZE * state->params.h + 2 * BORDER);
855
856 /*
857 * Recessed area containing the whole puzzle.
858 */
859 coords[0] = COORD(state->params.w) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
860 coords[1] = COORD(state->params.h) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
861 coords[2] = COORD(state->params.w) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
862 coords[3] = COORD(0) - HIGHLIGHT_WIDTH;
863 coords[4] = coords[2] - TILE_SIZE;
864 coords[5] = coords[3] + TILE_SIZE;
865 coords[8] = COORD(0) - HIGHLIGHT_WIDTH;
866 coords[9] = COORD(state->params.h) + HIGHLIGHT_WIDTH - 1 - TILE_GAP;
867 coords[6] = coords[8] + TILE_SIZE;
868 coords[7] = coords[9] - TILE_SIZE;
28b5987d 869 draw_polygon(fe, coords, 5, COL_HIGHLIGHT, COL_HIGHLIGHT);
6bbab0fe 870
871 coords[1] = COORD(0) - HIGHLIGHT_WIDTH;
872 coords[0] = COORD(0) - HIGHLIGHT_WIDTH;
28b5987d 873 draw_polygon(fe, coords, 5, COL_LOWLIGHT, COL_LOWLIGHT);
6bbab0fe 874
875 ds->started = 1;
876 }
877
878 if (flashtime > 0.0) {
879 int frame = (int)(flashtime / FLASH_FRAME);
880 bgcolour = (frame % 2 ? COL_LOWLIGHT : COL_HIGHLIGHT);
881 } else
882 bgcolour = COL_BACKGROUND;
883
884 for (x = 0; x < state->params.w; x++) {
885 for (y = 0; y < state->params.h; y++) {
886 int i = (state->params.w * y) + x;
887 int col = COL(state,x,y), tile = col;
888 int dright = (x+1 < state->params.w);
889 int dbelow = (y+1 < state->params.h);
890
891 tile |= ISSEL(ui,x,y);
d951510d 892 if (state->impossible)
893 tile |= TILE_IMPOSSIBLE;
6bbab0fe 894 if (dright && COL(state,x+1,y) == col)
895 tile |= TILE_JOINRIGHT;
896 if (dbelow && COL(state,x,y+1) == col)
897 tile |= TILE_JOINDOWN;
898 if ((tile & TILE_JOINRIGHT) && (tile & TILE_JOINDOWN) &&
899 COL(state,x+1,y+1) == col)
900 tile |= TILE_JOINDIAG;
901
f1359c5e 902 if (ui->displaysel && ui->xsel == x && ui->ysel == y)
903 tile |= TILE_HASSEL;
904
6bbab0fe 905 /* For now we're never expecting oldstate at all (because we have
906 * no animation); when we do we might well want to be looking
907 * at the tile colours from oldstate, not state. */
908 if ((oldstate && COL(oldstate,x,y) != col) ||
909 (flashtime > 0.0) ||
910 (ds->bgcolour != bgcolour) ||
911 (tile != ds->tiles[i])) {
d951510d 912 tile_redraw(fe, ds, x, y, dright, dbelow, tile, bgcolour);
6bbab0fe 913 ds->tiles[i] = tile;
914 }
915 }
916 }
917 ds->bgcolour = bgcolour;
918
919 {
920 char status[255], score[80];
921
922 sprintf(score, "Score: %d", state->score);
923
924 if (state->complete)
925 sprintf(status, "COMPLETE! %s", score);
926 else if (state->impossible)
927 sprintf(status, "Cannot move! %s", score);
928 else if (ui->nselected)
929 sprintf(status, "%s Selected: %d (%d)",
930 score, ui->nselected, npoints(&state->params, ui->nselected));
931 else
932 sprintf(status, "%s", score);
933 status_bar(fe, status);
934 }
935}
936
937static float game_anim_length(game_state *oldstate, game_state *newstate,
938 int dir, game_ui *ui)
939{
940 return 0.0F;
941}
942
943static float game_flash_length(game_state *oldstate, game_state *newstate,
944 int dir, game_ui *ui)
945{
946 if ((!oldstate->complete && newstate->complete) ||
947 (!oldstate->impossible && newstate->impossible))
948 return 2 * FLASH_FRAME;
949 else
950 return 0.0F;
951}
952
953static int game_wants_statusbar(void)
954{
955 return TRUE;
956}
957
958static int game_timing_state(game_state *state)
959{
960 return TRUE;
961}
962
963#ifdef COMBINED
964#define thegame samegame
965#endif
966
967const struct game thegame = {
f3cc3e50 968 "Same Game", "games.samegame",
6bbab0fe 969 default_params,
970 game_fetch_preset,
971 decode_params,
972 encode_params,
973 free_params,
974 dup_params,
975 TRUE, game_configure, custom_params,
976 validate_params,
977 new_game_desc,
6bbab0fe 978 validate_desc,
979 new_game,
980 dup_game,
981 free_game,
982 FALSE, solve_game,
983 TRUE, game_text_format,
984 new_ui,
985 free_ui,
ae8290c6 986 encode_ui,
987 decode_ui,
6bbab0fe 988 game_changed_state,
df11cd4e 989 interpret_move,
990 execute_move,
1f3ee4ee 991 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
6bbab0fe 992 game_colours,
993 game_new_drawstate,
994 game_free_drawstate,
995 game_redraw,
996 game_anim_length,
997 game_flash_length,
998 game_wants_statusbar,
999 FALSE, game_timing_state,
1000 0, /* mouse_priorities */
1001};