chiark / gitweb /
Dariusz Olszewski's changes to support compiling for PocketPC. This
[sgt-puzzles.git] / bridges.c
1 /*
2  * bridges.c: Implementation of the Nikoli game 'Bridges'.
3  *
4  * Things still to do:
5  *
6  * * write a recursive solver?
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <assert.h>
13 #include <ctype.h>
14 #include <math.h>
15
16 #include "puzzles.h"
17
18 /* Turn this on for hints about which lines are considered possibilities. */
19 #undef DRAW_HINTS
20 #undef DRAW_GRID
21 #undef DRAW_DSF
22
23 /* --- structures for params, state, etc. --- */
24
25 #define MAX_BRIDGES     4
26
27 #define PREFERRED_TILE_SIZE 24
28 #define TILE_SIZE       (ds->tilesize)
29 #define BORDER          (TILE_SIZE / 2)
30
31 #define COORD(x)  ( (x) * TILE_SIZE + BORDER )
32 #define FROMCOORD(x)  ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
33
34 #define FLASH_TIME 0.50F
35
36 enum {
37     COL_BACKGROUND,
38     COL_FOREGROUND,
39     COL_HIGHLIGHT, COL_LOWLIGHT,
40     COL_SELECTED, COL_MARK,
41     COL_HINT, COL_GRID,
42     COL_WARNING,
43     NCOLOURS
44 };
45
46 struct game_params {
47     int w, h, maxb;
48     int islands, expansion;     /* %age of island squares, %age chance of expansion */
49     int allowloops, difficulty;
50 };
51
52 /* general flags used by all structs */
53 #define G_ISLAND        0x0001
54 #define G_LINEV         0x0002     /* contains a vert. line */
55 #define G_LINEH         0x0004     /* contains a horiz. line (mutex with LINEV) */
56 #define G_LINE          (G_LINEV|G_LINEH)
57 #define G_MARKV         0x0008
58 #define G_MARKH         0x0010
59 #define G_MARK          (G_MARKV|G_MARKH)
60 #define G_NOLINEV       0x0020
61 #define G_NOLINEH       0x0040
62 #define G_NOLINE        (G_NOLINEV|G_NOLINEH)
63
64 /* flags used by the drawstate */
65 #define G_ISSEL         0x0080
66 #define G_REDRAW        0x0100
67 #define G_FLASH         0x0200
68 #define G_WARN          0x0400
69
70 /* flags used by the solver etc. */
71 #define G_SWEEP         0x0800
72
73 #define G_FLAGSH        (G_LINEH|G_MARKH|G_NOLINEH)
74 #define G_FLAGSV        (G_LINEV|G_MARKV|G_NOLINEV)
75
76 typedef unsigned int grid_type; /* change me later if we invent > 16 bits of flags. */
77
78 struct solver_state {
79     int *dsf, *tmpdsf;
80     int refcount;
81 };
82
83 /* state->gridi is an optimisation; it stores the pointer to the island
84  * structs indexed by (x,y). It's not strictly necessary (we could use
85  * find234 instead), but Purify showed that board generation (mostly the solver)
86  * was spending 60% of its time in find234. */
87
88 struct surrounds { /* cloned from lightup.c */
89     struct { int x, y, dx, dy, off; } points[4];
90     int npoints, nislands;
91 };
92
93 struct island {
94   game_state *state;
95   int x, y, count;
96   struct surrounds adj;
97 };
98
99 struct game_state {
100     int w, h, completed, solved, allowloops, maxb;
101     grid_type *grid, *scratch;
102     struct island *islands;
103     int n_islands, n_islands_alloc;
104     game_params params; /* used by the aux solver. */
105 #define N_WH_ARRAYS 5
106     char *wha, *possv, *possh, *lines, *maxv, *maxh;
107     struct island **gridi;
108     struct solver_state *solver; /* refcounted */
109 };
110
111 #define GRIDSZ(s) ((s)->w * (s)->h * sizeof(grid_type))
112
113 #define INGRID(s,x,y) ((x) >= 0 && (x) < (s)->w && (y) >= 0 && (y) < (s)->h)
114
115 #define DINDEX(x,y) ((y)*state->w + (x))
116
117 #define INDEX(s,g,x,y) ((s)->g[(y)*((s)->w) + (x)])
118 #define IDX(s,g,i) ((s)->g[(i)])
119 #define GRID(s,x,y) INDEX(s,grid,x,y)
120 #define SCRATCH(s,x,y) INDEX(s,scratch,x,y)
121 #define POSSIBLES(s,dx,x,y) ((dx) ? (INDEX(s,possh,x,y)) : (INDEX(s,possv,x,y)))
122 #define MAXIMUM(s,dx,x,y) ((dx) ? (INDEX(s,maxh,x,y)) : (INDEX(s,maxv,x,y)))
123
124 #define GRIDCOUNT(s,x,y,f) ((GRID(s,x,y) & (f)) ? (INDEX(s,lines,x,y)) : 0)
125
126 #define WITHIN2(x,min,max) (((x) < (min)) ? 0 : (((x) > (max)) ? 0 : 1))
127 #define WITHIN(x,min,max) ((min) > (max) ? \
128                            WITHIN2(x,max,min) : WITHIN2(x,min,max))
129
130 /* --- island struct and tree support functions --- */
131
132 #define ISLAND_ORTH(is,j,f,df) \
133     (is->f + (is->adj.points[(j)].off*is->adj.points[(j)].df))
134
135 #define ISLAND_ORTHX(is,j) ISLAND_ORTH(is,j,x,dx)
136 #define ISLAND_ORTHY(is,j) ISLAND_ORTH(is,j,y,dy)
137
138 static void fixup_islands_for_realloc(game_state *state)
139 {
140     int i;
141
142     for (i = 0; i < state->w*state->h; i++) state->gridi[i] = NULL;
143     for (i = 0; i < state->n_islands; i++) {
144         struct island *is = &state->islands[i];
145         is->state = state;
146         INDEX(state, gridi, is->x, is->y) = is;
147     }
148 }
149
150 static char *game_text_format(game_state *state)
151 {
152     int x, y, len, nl;
153     char *ret, *p;
154     struct island *is;
155     grid_type grid;
156
157     len = (state->h) * (state->w+1) + 1;
158     ret = snewn(len, char);
159     p = ret;
160
161     for (y = 0; y < state->h; y++) {
162         for (x = 0; x < state->w; x++) {
163             grid = GRID(state,x,y);
164             nl = INDEX(state,lines,x,y);
165             is = INDEX(state, gridi, x, y);
166             if (is) {
167                 *p++ = '0' + is->count;
168             } else if (grid & G_LINEV) {
169                 *p++ = (nl > 1) ? '"' : (nl == 1) ? '|' : '!'; /* gaah, want a double-bar. */
170             } else if (grid & G_LINEH) {
171                 *p++ = (nl > 1) ? '=' : (nl == 1) ? '-' : '~';
172             } else {
173                 *p++ = '.';
174             }
175         }
176         *p++ = '\n';
177     }
178     *p++ = '\0';
179
180     assert(p - ret == len);
181     return ret;
182 }
183
184 static void debug_state(game_state *state)
185 {
186     char *textversion = game_text_format(state);
187     debug(("%s", textversion));
188     sfree(textversion);
189 }
190
191 /*static void debug_possibles(game_state *state)
192 {
193     int x, y;
194     debug(("possh followed by possv\n"));
195     for (y = 0; y < state->h; y++) {
196         for (x = 0; x < state->w; x++) {
197             debug(("%d", POSSIBLES(state, 1, x, y)));
198         }
199         debug((" "));
200         for (x = 0; x < state->w; x++) {
201             debug(("%d", POSSIBLES(state, 0, x, y)));
202         }
203         debug(("\n"));
204     }
205     debug(("\n"));
206         for (y = 0; y < state->h; y++) {
207         for (x = 0; x < state->w; x++) {
208             debug(("%d", MAXIMUM(state, 1, x, y)));
209         }
210         debug((" "));
211         for (x = 0; x < state->w; x++) {
212             debug(("%d", MAXIMUM(state, 0, x, y)));
213         }
214         debug(("\n"));
215     }
216     debug(("\n"));
217 }*/
218
219 static void island_set_surrounds(struct island *is)
220 {
221     assert(INGRID(is->state,is->x,is->y));
222     is->adj.npoints = is->adj.nislands = 0;
223 #define ADDPOINT(cond,ddx,ddy) do {\
224     if (cond) { \
225         is->adj.points[is->adj.npoints].x = is->x+(ddx); \
226         is->adj.points[is->adj.npoints].y = is->y+(ddy); \
227         is->adj.points[is->adj.npoints].dx = (ddx); \
228         is->adj.points[is->adj.npoints].dy = (ddy); \
229         is->adj.points[is->adj.npoints].off = 0; \
230         is->adj.npoints++; \
231     } } while(0)
232     ADDPOINT(is->x > 0,                -1,  0);
233     ADDPOINT(is->x < (is->state->w-1), +1,  0);
234     ADDPOINT(is->y > 0,                 0, -1);
235     ADDPOINT(is->y < (is->state->h-1),  0, +1);
236 }
237
238 static void island_find_orthogonal(struct island *is)
239 {
240     /* fills in the rest of the 'surrounds' structure, assuming
241      * all other islands are now in place. */
242     int i, x, y, dx, dy, off;
243
244     is->adj.nislands = 0;
245     for (i = 0; i < is->adj.npoints; i++) {
246         dx = is->adj.points[i].dx;
247         dy = is->adj.points[i].dy;
248         x = is->x + dx;
249         y = is->y + dy;
250         off = 1;
251         is->adj.points[i].off = 0;
252         while (INGRID(is->state, x, y)) {
253             if (GRID(is->state, x, y) & G_ISLAND) {
254                 is->adj.points[i].off = off;
255                 is->adj.nislands++;
256                 /*debug(("island (%d,%d) has orth is. %d*(%d,%d) away at (%d,%d).\n",
257                        is->x, is->y, off, dx, dy,
258                        ISLAND_ORTHX(is,i), ISLAND_ORTHY(is,i)));*/
259                 goto foundisland;
260             }
261             off++; x += dx; y += dy;
262         }
263 foundisland:
264         ;
265     }
266 }
267
268 static int island_hasbridge(struct island *is, int direction)
269 {
270     int x = is->adj.points[direction].x;
271     int y = is->adj.points[direction].y;
272     grid_type gline = is->adj.points[direction].dx ? G_LINEH : G_LINEV;
273
274     if (GRID(is->state, x, y) & gline) return 1;
275     return 0;
276 }
277
278 static struct island *island_find_connection(struct island *is, int adjpt)
279 {
280     struct island *is_r;
281
282     assert(adjpt < is->adj.npoints);
283     if (!is->adj.points[adjpt].off) return NULL;
284     if (!island_hasbridge(is, adjpt)) return NULL;
285
286     is_r = INDEX(is->state, gridi,
287                  ISLAND_ORTHX(is, adjpt), ISLAND_ORTHY(is, adjpt));
288     assert(is_r);
289
290     return is_r;
291 }
292
293 static struct island *island_add(game_state *state, int x, int y, int count)
294 {
295     struct island *is;
296     int realloced = 0;
297
298     assert(!(GRID(state,x,y) & G_ISLAND));
299     GRID(state,x,y) |= G_ISLAND;
300
301     state->n_islands++;
302     if (state->n_islands > state->n_islands_alloc) {
303         state->n_islands_alloc = state->n_islands * 2;
304         state->islands =
305             sresize(state->islands, state->n_islands_alloc, struct island);
306         realloced = 1;
307     }
308     is = &state->islands[state->n_islands-1];
309
310     memset(is, 0, sizeof(struct island));
311     is->state = state;
312     is->x = x;
313     is->y = y;
314     is->count = count;
315     island_set_surrounds(is);
316
317     if (realloced)
318         fixup_islands_for_realloc(state);
319     else
320         INDEX(state, gridi, x, y) = is;
321
322     return is;
323 }
324
325
326 /* n = -1 means 'flip NOLINE flags [and set line to 0].' */
327 static void island_join(struct island *i1, struct island *i2, int n, int is_max)
328 {
329     game_state *state = i1->state;
330     int s, e, x, y;
331
332     assert(i1->state == i2->state);
333     assert(n >= -1 && n <= i1->state->maxb);
334
335     if (i1->x == i2->x) {
336         x = i1->x;
337         if (i1->y < i2->y) {
338             s = i1->y+1; e = i2->y-1;
339         } else {
340             s = i2->y+1; e = i1->y-1;
341         }
342         for (y = s; y <= e; y++) {
343             if (is_max) {
344                 INDEX(state,maxv,x,y) = n;
345             } else {
346                 if (n < 0) {
347                     GRID(state,x,y) ^= G_NOLINEV;
348                 } else if (n == 0) {
349                     GRID(state,x,y) &= ~G_LINEV;
350                 } else {
351                     GRID(state,x,y) |= G_LINEV;
352                     INDEX(state,lines,x,y) = n;
353                 }
354             }
355         }
356     } else if (i1->y == i2->y) {
357         y = i1->y;
358         if (i1->x < i2->x) {
359             s = i1->x+1; e = i2->x-1;
360         } else {
361             s = i2->x+1; e = i1->x-1;
362         }
363         for (x = s; x <= e; x++) {
364             if (is_max) {
365                 INDEX(state,maxh,x,y) = n;
366             } else {
367                 if (n < 0) {
368                     GRID(state,x,y) ^= G_NOLINEH;
369                 } else if (n == 0) {
370                     GRID(state,x,y) &= ~G_LINEH;
371                 } else {
372                     GRID(state,x,y) |= G_LINEH;
373                     INDEX(state,lines,x,y) = n;
374                 }
375             }
376         }
377     } else {
378         assert(!"island_join: islands not orthogonal.");
379     }
380 }
381
382 /* Counts the number of bridges currently attached to the island. */
383 static int island_countbridges(struct island *is)
384 {
385     int i, c = 0;
386
387     for (i = 0; i < is->adj.npoints; i++) {
388         c += GRIDCOUNT(is->state,
389                        is->adj.points[i].x, is->adj.points[i].y,
390                        is->adj.points[i].dx ? G_LINEH : G_LINEV);
391     }
392     /*debug(("island count for (%d,%d) is %d.\n", is->x, is->y, c));*/
393     return c;
394 }
395
396 static int island_adjspace(struct island *is, int marks, int missing,
397                            int direction)
398 {
399     int x, y, poss, curr, dx;
400     grid_type gline, mline;
401
402     x = is->adj.points[direction].x;
403     y = is->adj.points[direction].y;
404     dx = is->adj.points[direction].dx;
405     gline = dx ? G_LINEH : G_LINEV;
406
407     if (marks) {
408         mline = dx ? G_MARKH : G_MARKV;
409         if (GRID(is->state,x,y) & mline) return 0;
410     }
411     poss = POSSIBLES(is->state, dx, x, y);
412     poss = min(poss, missing);
413
414     curr = GRIDCOUNT(is->state, x, y, gline);
415     poss = min(poss, MAXIMUM(is->state, dx, x, y) - curr);
416
417     return poss;
418 }
419
420 /* Counts the number of bridge spaces left around the island;
421  * expects the possibles to be up-to-date. */
422 static int island_countspaces(struct island *is, int marks)
423 {
424     int i, c = 0, missing;
425
426     missing = is->count - island_countbridges(is);
427     if (missing < 0) return 0;
428
429     for (i = 0; i < is->adj.npoints; i++) {
430         c += island_adjspace(is, marks, missing, i);
431     }
432     return c;
433 }
434
435 static int island_isadj(struct island *is, int direction)
436 {
437     int x, y;
438     grid_type gline, mline;
439
440     x = is->adj.points[direction].x;
441     y = is->adj.points[direction].y;
442
443     mline = is->adj.points[direction].dx ? G_MARKH : G_MARKV;
444     gline = is->adj.points[direction].dx ? G_LINEH : G_LINEV;
445     if (GRID(is->state, x, y) & mline) {
446         /* If we're marked (i.e. the thing to attach to is complete)
447          * only count an adjacency if we're already attached. */
448         return GRIDCOUNT(is->state, x, y, gline);
449     } else {
450         /* If we're unmarked, count possible adjacency iff it's
451          * flagged as POSSIBLE. */
452         return POSSIBLES(is->state, is->adj.points[direction].dx, x, y);
453     }
454     return 0;
455 }
456
457 /* Counts the no. of possible adjacent islands (including islands
458  * we're already connected to). */
459 static int island_countadj(struct island *is)
460 {
461     int i, nadj = 0;
462
463     for (i = 0; i < is->adj.npoints; i++) {
464         if (island_isadj(is, i)) nadj++;
465     }
466     return nadj;
467 }
468
469 static void island_togglemark(struct island *is)
470 {
471     int i, j, x, y, o;
472     struct island *is_loop;
473
474     /* mark the island... */
475     GRID(is->state, is->x, is->y) ^= G_MARK;
476
477     /* ...remove all marks on non-island squares... */
478     for (x = 0; x < is->state->w; x++) {
479         for (y = 0; y < is->state->h; y++) {
480             if (!(GRID(is->state, x, y) & G_ISLAND))
481                 GRID(is->state, x, y) &= ~G_MARK;
482         }
483     }
484
485     /* ...and add marks to squares around marked islands. */
486     for (i = 0; i < is->state->n_islands; i++) {
487         is_loop = &is->state->islands[i];
488         if (!(GRID(is_loop->state, is_loop->x, is_loop->y) & G_MARK))
489             continue;
490
491         for (j = 0; j < is_loop->adj.npoints; j++) {
492             /* if this direction takes us to another island, mark all
493              * squares between the two islands. */
494             if (!is_loop->adj.points[j].off) continue;
495             assert(is_loop->adj.points[j].off > 1);
496             for (o = 1; o < is_loop->adj.points[j].off; o++) {
497                 GRID(is_loop->state,
498                      is_loop->x + is_loop->adj.points[j].dx*o,
499                      is_loop->y + is_loop->adj.points[j].dy*o) |=
500                     is_loop->adj.points[j].dy ? G_MARKV : G_MARKH;
501             }
502         }
503     }
504 }
505
506 static int island_impossible(struct island *is, int strict)
507 {
508     int curr = island_countbridges(is), nspc = is->count - curr, nsurrspc;
509     int i, poss;
510     grid_type v;
511     struct island *is_orth;
512
513     if (nspc < 0) {
514         debug(("island at (%d,%d) impossible because full.\n", is->x, is->y));
515         return 1;        /* too many bridges */
516     } else if ((curr + island_countspaces(is, 0)) < is->count) {
517         debug(("island at (%d,%d) impossible because not enough spaces.\n", is->x, is->y));
518         return 1;        /* impossible to create enough bridges */
519     } else if (strict && curr < is->count) {
520         debug(("island at (%d,%d) impossible because locked.\n", is->x, is->y));
521         return 1;        /* not enough bridges and island is locked */
522     }
523
524     /* Count spaces in surrounding islands. */
525     nsurrspc = 0;
526     for (i = 0; i < is->adj.npoints; i++) {
527         int ifree, dx = is->adj.points[i].dx;
528
529         if (!is->adj.points[i].off) continue;
530         v = GRID(is->state, is->adj.points[i].x, is->adj.points[i].y);
531         poss = POSSIBLES(is->state, dx,
532                          is->adj.points[i].x, is->adj.points[i].y);
533         if (poss == 0) continue;
534         is_orth = INDEX(is->state, gridi,
535                         ISLAND_ORTHX(is,i), ISLAND_ORTHY(is,i));
536         assert(is_orth);
537
538         ifree = is_orth->count - island_countbridges(is_orth);
539         if (ifree > 0)
540             nsurrspc += min(ifree, MAXIMUM(is->state, dx,
541                                            is->adj.points[i].x, is->adj.points[i].y));
542     }
543     if (nsurrspc < nspc) {
544         debug(("island at (%d,%d) impossible: surr. islands %d spc, need %d.\n",
545                is->x, is->y, nsurrspc, nspc));
546         return 1;       /* not enough spaces around surrounding islands to fill this one. */
547     }
548
549     return 0;
550 }
551
552 /* --- Game parameter functions --- */
553
554 #define DEFAULT_PRESET 0
555
556 const struct game_params bridges_presets[] = {
557   { 7, 7, 2, 30, 10, 1, 0 },
558   { 7, 7, 2, 30, 10, 1, 1 },
559   { 7, 7, 2, 30, 10, 1, 2 },
560   { 10, 10, 2, 30, 10, 1, 0 },
561   { 10, 10, 2, 30, 10, 1, 1 },
562   { 10, 10, 2, 30, 10, 1, 2 },
563   { 15, 15, 2, 30, 10, 1, 0 },
564   { 15, 15, 2, 30, 10, 1, 1 },
565   { 15, 15, 2, 30, 10, 1, 2 },
566 };
567
568 static game_params *default_params(void)
569 {
570     game_params *ret = snew(game_params);
571     *ret = bridges_presets[DEFAULT_PRESET];
572
573     return ret;
574 }
575
576 static int game_fetch_preset(int i, char **name, game_params **params)
577 {
578     game_params *ret;
579     char buf[80];
580
581     if (i < 0 || i >= lenof(bridges_presets))
582         return FALSE;
583
584     ret = default_params();
585     *ret = bridges_presets[i];
586     *params = ret;
587
588     sprintf(buf, "%dx%d %s", ret->w, ret->h,
589             ret->difficulty == 0 ? "easy" :
590             ret->difficulty == 1 ? "medium" : "hard");
591     *name = dupstr(buf);
592
593     return TRUE;
594 }
595
596 static void free_params(game_params *params)
597 {
598     sfree(params);
599 }
600
601 static game_params *dup_params(game_params *params)
602 {
603     game_params *ret = snew(game_params);
604     *ret = *params;                    /* structure copy */
605     return ret;
606 }
607
608 #define EATNUM(x) do { \
609     (x) = atoi(string); \
610     while (*string && isdigit((unsigned char)*string)) string++; \
611 } while(0)
612
613 static void decode_params(game_params *params, char const *string)
614 {
615     EATNUM(params->w);
616     params->h = params->w;
617     if (*string == 'x') {
618         string++;
619         EATNUM(params->h);
620     }
621     if (*string == 'i') {
622         string++;
623         EATNUM(params->islands);
624     }
625     if (*string == 'e') {
626         string++;
627         EATNUM(params->expansion);
628     }
629     if (*string == 'm') {
630         string++;
631         EATNUM(params->maxb);
632     }
633     params->allowloops = 1;
634     if (*string == 'L') {
635         string++;
636         params->allowloops = 0;
637     }
638     if (*string == 'd') {
639         string++;
640         EATNUM(params->difficulty);
641     }
642 }
643
644 static char *encode_params(game_params *params, int full)
645 {
646     char buf[80];
647
648     if (full) {
649         sprintf(buf, "%dx%di%de%dm%d%sd%d",
650                 params->w, params->h, params->islands, params->expansion,
651                 params->maxb, params->allowloops ? "" : "L",
652                 params->difficulty);
653     } else {
654         sprintf(buf, "%dx%dm%d%s", params->w, params->h,
655                 params->maxb, params->allowloops ? "" : "L");
656     }
657     return dupstr(buf);
658 }
659
660 static config_item *game_configure(game_params *params)
661 {
662     config_item *ret;
663     char buf[80];
664
665     ret = snewn(8, config_item);
666
667     ret[0].name = "Width";
668     ret[0].type = C_STRING;
669     sprintf(buf, "%d", params->w);
670     ret[0].sval = dupstr(buf);
671     ret[0].ival = 0;
672
673     ret[1].name = "Height";
674     ret[1].type = C_STRING;
675     sprintf(buf, "%d", params->h);
676     ret[1].sval = dupstr(buf);
677     ret[1].ival = 0;
678
679     ret[2].name = "Difficulty";
680     ret[2].type = C_CHOICES;
681     ret[2].sval = ":Easy:Medium:Hard";
682     ret[2].ival = params->difficulty;
683
684     ret[3].name = "Allow loops";
685     ret[3].type = C_BOOLEAN;
686     ret[3].sval = NULL;
687     ret[3].ival = params->allowloops;
688
689     ret[4].name = "Max. bridges per direction";
690     ret[4].type = C_CHOICES;
691     ret[4].sval = ":1:2:3:4"; /* keep up-to-date with MAX_BRIDGES */
692     ret[4].ival = params->maxb - 1;
693
694     ret[5].name = "%age of island squares";
695     ret[5].type = C_CHOICES;
696     ret[5].sval = ":5%:10%:15%:20%:25%:30%";
697     ret[5].ival = (params->islands / 5)-1;
698
699     ret[6].name = "Expansion factor (%age)";
700     ret[6].type = C_CHOICES;
701     ret[6].sval = ":0%:10%:20%:30%:40%:50%:60%:70%:80%:90%:100%";
702     ret[6].ival = params->expansion / 10;
703
704     ret[7].name = NULL;
705     ret[7].type = C_END;
706     ret[7].sval = NULL;
707     ret[7].ival = 0;
708
709     return ret;
710 }
711
712 static game_params *custom_params(config_item *cfg)
713 {
714     game_params *ret = snew(game_params);
715
716     ret->w          = atoi(cfg[0].sval);
717     ret->h          = atoi(cfg[1].sval);
718     ret->difficulty = cfg[2].ival;
719     ret->allowloops = cfg[3].ival;
720     ret->maxb       = cfg[4].ival + 1;
721     ret->islands    = (cfg[5].ival + 1) * 5;
722     ret->expansion  = cfg[6].ival * 10;
723
724     return ret;
725 }
726
727 static char *validate_params(game_params *params, int full)
728 {
729     if (params->w < 3 || params->h < 3)
730         return "Width and height must be at least 3";
731     if (params->maxb < 1 || params->maxb > MAX_BRIDGES)
732         return "Too many bridges.";
733     if (full) {
734         if (params->islands <= 0 || params->islands > 30)
735             return "%age of island squares must be between 1% and 30%";
736         if (params->expansion < 0 || params->expansion > 100)
737             return "Expansion factor must be between 0 and 100";
738     }
739     return NULL;
740 }
741
742 /* --- Game encoding and differences --- */
743
744 static char *encode_game(game_state *state)
745 {
746     char *ret, *p;
747     int wh = state->w*state->h, run, x, y;
748     struct island *is;
749
750     ret = snewn(wh + 1, char);
751     p = ret;
752     run = 0;
753     for (y = 0; y < state->h; y++) {
754         for (x = 0; x < state->w; x++) {
755             is = INDEX(state, gridi, x, y);
756             if (is) {
757                 if (run) {
758                     *p++ = ('a'-1) + run;
759                     run = 0;
760                 }
761                 if (is->count < 10)
762                     *p++ = '0' + is->count;
763                 else
764                     *p++ = 'A' + (is->count - 10);
765             } else {
766                 if (run == 26) {
767                     *p++ = ('a'-1) + run;
768                     run = 0;
769                 }
770                 run++;
771             }
772         }
773     }
774     if (run) {
775         *p++ = ('a'-1) + run;
776         run = 0;
777     }
778     *p = '\0';
779     assert(p - ret <= wh);
780
781     return ret;
782 }
783
784 static char *game_state_diff(game_state *src, game_state *dest)
785 {
786     int movesize = 256, movelen = 0;
787     char *move = snewn(movesize, char), buf[80];
788     int i, d, x, y, len;
789     grid_type gline, nline;
790     struct island *is_s, *is_d, *is_orth;
791
792 #define APPEND do {                                     \
793     if (movelen + len >= movesize) {                    \
794         movesize = movelen + len + 256;                 \
795         move = sresize(move, movesize, char);           \
796     }                                                   \
797     strcpy(move + movelen, buf);                        \
798     movelen += len;                                     \
799 } while(0)
800
801     move[movelen++] = 'S';
802     move[movelen] = '\0';
803
804     assert(src->n_islands == dest->n_islands);
805
806     for (i = 0; i < src->n_islands; i++) {
807         is_s = &src->islands[i];
808         is_d = &dest->islands[i];
809         assert(is_s->x == is_d->x);
810         assert(is_s->y == is_d->y);
811         assert(is_s->adj.npoints == is_d->adj.npoints); /* more paranoia */
812
813         for (d = 0; d < is_s->adj.npoints; d++) {
814             if (is_s->adj.points[d].dx == -1 ||
815                 is_s->adj.points[d].dy == -1) continue;
816
817             x = is_s->adj.points[d].x;
818             y = is_s->adj.points[d].y;
819             gline = is_s->adj.points[d].dx ? G_LINEH : G_LINEV;
820             nline = is_s->adj.points[d].dx ? G_NOLINEH : G_NOLINEV;
821             is_orth = INDEX(dest, gridi,
822                             ISLAND_ORTHX(is_d, d), ISLAND_ORTHY(is_d, d));
823
824             if (GRIDCOUNT(src, x, y, gline) != GRIDCOUNT(dest, x, y, gline)) {
825                 assert(is_orth);
826                 len = sprintf(buf, ";L%d,%d,%d,%d,%d",
827                               is_s->x, is_s->y, is_orth->x, is_orth->y,
828                               GRIDCOUNT(dest, x, y, gline));
829                 APPEND;
830             }
831             if ((GRID(src,x,y) & nline) != (GRID(dest, x, y) & nline)) {
832                 assert(is_orth);
833                 len = sprintf(buf, ";N%d,%d,%d,%d",
834                               is_s->x, is_s->y, is_orth->x, is_orth->y);
835                 APPEND;
836             }
837         }
838         if ((GRID(src, is_s->x, is_s->y) & G_MARK) !=
839             (GRID(dest, is_d->x, is_d->y) & G_MARK)) {
840             len = sprintf(buf, ";M%d,%d", is_s->x, is_s->y);
841             APPEND;
842         }
843     }
844     return move;
845 }
846
847 /* --- Game setup and solving utilities --- */
848
849 /* This function is optimised; a Quantify showed that lots of grid-generation time
850  * (>50%) was spent in here. Hence the IDX() stuff. */
851
852 static void map_update_possibles(game_state *state)
853 {
854     int x, y, s, e, bl, i, np, maxb, w = state->w, idx;
855     struct island *is_s = NULL, *is_f = NULL;
856
857     /* Run down vertical stripes [un]setting possv... */
858     for (x = 0; x < state->w; x++) {
859         idx = x;
860         s = e = -1;
861         bl = 0;
862         /* Unset possible flags until we find an island. */
863         for (y = 0; y < state->h; y++) {
864             is_s = IDX(state, gridi, idx);
865             if (is_s) break;
866
867             IDX(state, possv, idx) = 0;
868             idx += w;
869         }
870         for (; y < state->h; y++) {
871             is_f = IDX(state, gridi, idx);
872             if (is_f) {
873                 assert(is_s);
874                 maxb = IDX(state, maxv, idx);
875                 np = min(maxb, min(is_s->count, is_f->count));
876
877                 if (s != -1) {
878                     for (i = s; i <= e; i++) {
879                         INDEX(state, possv, x, i) = bl ? 0 : np;
880                     }
881                 }
882                 s = y+1;
883                 bl = 0;
884                 is_s = is_f;
885             } else {
886                 e = y;
887                 if (IDX(state,grid,idx) & (G_LINEH|G_NOLINEV)) bl = 1;
888             }
889             idx += w;
890         }
891         if (s != -1) {
892             for (i = s; i <= e; i++)
893                 INDEX(state, possv, x, i) = 0;
894         }
895     }
896
897     /* ...and now do horizontal stripes [un]setting possh. */
898     /* can we lose this clone'n'hack? */
899     for (y = 0; y < state->h; y++) {
900         idx = y*w;
901         s = e = -1;
902         bl = 0;
903         for (x = 0; x < state->w; x++) {
904             is_s = IDX(state, gridi, idx);
905             if (is_s) break;
906
907             IDX(state, possh, idx) = 0;
908             idx += 1;
909         }
910         for (; x < state->w; x++) {
911             is_f = IDX(state, gridi, idx);
912             if (is_f) {
913                 assert(is_s);
914                 maxb = IDX(state, maxh, idx);
915                 np = min(maxb, min(is_s->count, is_f->count));
916
917                 if (s != -1) {
918                     for (i = s; i <= e; i++) {
919                         INDEX(state, possh, i, y) = bl ? 0 : np;
920                     }
921                 }
922                 s = x+1;
923                 bl = 0;
924                 is_s = is_f;
925             } else {
926                 e = x;
927                 if (IDX(state,grid,idx) & (G_LINEV|G_NOLINEH)) bl = 1;
928             }
929             idx += 1;
930         }
931         if (s != -1) {
932             for (i = s; i <= e; i++)
933                 INDEX(state, possh, i, y) = 0;
934         }
935     }
936 }
937
938 static void map_count(game_state *state)
939 {
940     int i, n, ax, ay;
941     grid_type flag, grid;
942     struct island *is;
943
944     for (i = 0; i < state->n_islands; i++) {
945         is = &state->islands[i];
946         is->count = 0;
947         for (n = 0; n < is->adj.npoints; n++) {
948             ax = is->adj.points[n].x;
949             ay = is->adj.points[n].y;
950             flag = (ax == is->x) ? G_LINEV : G_LINEH;
951             grid = GRID(state,ax,ay);
952             if (grid & flag) {
953                 is->count += INDEX(state,lines,ax,ay);
954             }
955         }
956     }
957 }
958
959 static void map_find_orthogonal(game_state *state)
960 {
961     int i;
962
963     for (i = 0; i < state->n_islands; i++) {
964         island_find_orthogonal(&state->islands[i]);
965     }
966 }
967
968 static int grid_degree(game_state *state, int x, int y, int *nx_r, int *ny_r)
969 {
970     grid_type grid = SCRATCH(state, x, y), gline = grid & G_LINE;
971     struct island *is;
972     int x1, y1, x2, y2, c = 0, i, nx, ny;
973
974     nx = ny = -1; /* placate optimiser */
975     is = INDEX(state, gridi, x, y);
976     if (is) {
977         for (i = 0; i < is->adj.npoints; i++) {
978             gline = is->adj.points[i].dx ? G_LINEH : G_LINEV;
979             if (SCRATCH(state,
980                         is->adj.points[i].x,
981                         is->adj.points[i].y) & gline) {
982                 nx = is->adj.points[i].x;
983                 ny = is->adj.points[i].y;
984                 c++;
985             }
986         }
987     } else if (gline) {
988         if (gline & G_LINEV) {
989             x1 = x2 = x;
990             y1 = y-1; y2 = y+1;
991         } else {
992             x1 = x-1; x2 = x+1;
993             y1 = y2 = y;
994         }
995         /* Non-island squares with edges in should never be pointing off the
996          * edge of the grid. */
997         assert(INGRID(state, x1, y1));
998         assert(INGRID(state, x2, y2));
999         if (SCRATCH(state, x1, y1) & (gline | G_ISLAND)) {
1000             nx = x1; ny = y1; c++;
1001         }
1002         if (SCRATCH(state, x2, y2) & (gline | G_ISLAND)) {
1003             nx = x2; ny = y2; c++;
1004         }
1005     }
1006     if (c == 1) {
1007         assert(nx != -1 && ny != -1); /* paranoia */
1008         *nx_r = nx; *ny_r = ny;
1009     }
1010     return c;
1011 }
1012
1013 static int map_hasloops(game_state *state, int mark)
1014 {
1015     int x, y, ox, oy, nx = 0, ny = 0, loop = 0;
1016
1017     memcpy(state->scratch, state->grid, GRIDSZ(state));
1018
1019     /* This algorithm is actually broken; if there are two loops connected
1020      * by bridges this will also highlight bridges. The correct algorithm
1021      * uses a dsf and a two-pass edge-detection algorithm (see check_correct
1022      * in slant.c); this is BALGE for now, especially since disallow-loops
1023      * is not the default for this puzzle. If we want to fix this later then
1024      * copy the alg in slant.c to the empty statement in map_group. */
1025
1026     /* Remove all 1-degree edges. */
1027     for (y = 0; y < state->h; y++) {
1028         for (x = 0; x < state->w; x++) {
1029             ox = x; oy = y;
1030             while (grid_degree(state, ox, oy, &nx, &ny) == 1) {
1031                 /*debug(("hasloops: removing 1-degree at (%d,%d).\n", ox, oy));*/
1032                 SCRATCH(state, ox, oy) &= ~(G_LINE|G_ISLAND);
1033                 ox = nx; oy = ny;
1034             }
1035         }
1036     }
1037     /* Mark any remaining edges as G_WARN, if required. */
1038     for (x = 0; x < state->w; x++) {
1039         for (y = 0; y < state->h; y++) {
1040             if (GRID(state,x,y) & G_ISLAND) continue;
1041
1042             if (SCRATCH(state, x, y) & G_LINE) {
1043                 if (mark) {
1044                     /*debug(("hasloops: marking loop square at (%d,%d).\n",
1045                            x, y));*/
1046                     GRID(state,x,y) |= G_WARN;
1047                     loop = 1;
1048                 } else
1049                     return 1; /* short-cut as soon as we find one */
1050             } else {
1051                 if (mark)
1052                     GRID(state,x,y) &= ~G_WARN;
1053             }
1054         }
1055     }
1056     return loop;
1057 }
1058
1059 static void map_group(game_state *state)
1060 {
1061     int i, wh = state->w*state->h, d1, d2;
1062     int x, y, x2, y2;
1063     int *dsf = state->solver->dsf;
1064     struct island *is, *is_join;
1065
1066     /* Initialise dsf. */
1067     dsf_init(dsf, wh);
1068
1069     /* For each island, find connected islands right or down
1070      * and merge the dsf for the island squares as well as the
1071      * bridge squares. */
1072     for (x = 0; x < state->w; x++) {
1073         for (y = 0; y < state->h; y++) {
1074             GRID(state,x,y) &= ~(G_SWEEP|G_WARN); /* for group_full. */
1075
1076             is = INDEX(state, gridi, x, y);
1077             if (!is) continue;
1078             d1 = DINDEX(x,y);
1079             for (i = 0; i < is->adj.npoints; i++) {
1080                 /* only want right/down */
1081                 if (is->adj.points[i].dx == -1 ||
1082                     is->adj.points[i].dy == -1) continue;
1083
1084                 is_join = island_find_connection(is, i);
1085                 if (!is_join) continue;
1086
1087                 d2 = DINDEX(is_join->x, is_join->y);
1088                 if (dsf_canonify(dsf,d1) == dsf_canonify(dsf,d2)) {
1089                     ; /* we have a loop. See comment in map_hasloops. */
1090                     /* However, we still want to merge all squares joining
1091                      * this side-that-makes-a-loop. */
1092                 }
1093                 /* merge all squares between island 1 and island 2. */
1094                 for (x2 = x; x2 <= is_join->x; x2++) {
1095                     for (y2 = y; y2 <= is_join->y; y2++) {
1096                         d2 = DINDEX(x2,y2);
1097                         if (d1 != d2) dsf_merge(dsf,d1,d2);
1098                     }
1099                 }
1100             }
1101         }
1102     }
1103 }
1104
1105 static int map_group_check(game_state *state, int canon, int warn,
1106                            int *nislands_r)
1107 {
1108     int *dsf = state->solver->dsf, nislands = 0;
1109     int x, y, i, allfull = 1;
1110     struct island *is;
1111
1112     for (i = 0; i < state->n_islands; i++) {
1113         is = &state->islands[i];
1114         if (dsf_canonify(dsf, DINDEX(is->x,is->y)) != canon) continue;
1115
1116         GRID(state, is->x, is->y) |= G_SWEEP;
1117         nislands++;
1118         if (island_countbridges(is) != is->count)
1119             allfull = 0;
1120     }
1121     if (warn && allfull && nislands != state->n_islands) {
1122         /* we're full and this island group isn't the whole set.
1123          * Mark all squares with this dsf canon as ERR. */
1124         for (x = 0; x < state->w; x++) {
1125             for (y = 0; y < state->h; y++) {
1126                 if (dsf_canonify(dsf, DINDEX(x,y)) == canon) {
1127                     GRID(state,x,y) |= G_WARN;
1128                 }
1129             }
1130         }
1131
1132     }
1133     if (nislands_r) *nislands_r = nislands;
1134     return allfull;
1135 }
1136
1137 static int map_group_full(game_state *state, int *ngroups_r)
1138 {
1139     int *dsf = state->solver->dsf, ngroups = 0;
1140     int i, anyfull = 0;
1141     struct island *is;
1142
1143     /* NB this assumes map_group (or sth else) has cleared G_SWEEP. */
1144
1145     for (i = 0; i < state->n_islands; i++) {
1146         is = &state->islands[i];
1147         if (GRID(state,is->x,is->y) & G_SWEEP) continue;
1148
1149         ngroups++;
1150         if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)),
1151                             1, NULL))
1152             anyfull = 1;
1153     }
1154
1155     *ngroups_r = ngroups;
1156     return anyfull;
1157 }
1158
1159 static int map_check(game_state *state)
1160 {
1161     int ngroups;
1162
1163     /* Check for loops, if necessary. */
1164     if (!state->allowloops) {
1165         if (map_hasloops(state, 1))
1166             return 0;
1167     }
1168
1169     /* Place islands into island groups and check for early
1170      * satisfied-groups. */
1171     map_group(state); /* clears WARN and SWEEP */
1172     if (map_group_full(state, &ngroups)) {
1173         if (ngroups == 1) return 1;
1174     }
1175     return 0;
1176 }
1177
1178 static void map_clear(game_state *state)
1179 {
1180     int x, y;
1181
1182     for (x = 0; x < state->w; x++) {
1183         for (y = 0; y < state->h; y++) {
1184             /* clear most flags; might want to be slightly more careful here. */
1185             GRID(state,x,y) &= G_ISLAND;
1186         }
1187     }
1188 }
1189
1190 static void solve_join(struct island *is, int direction, int n, int is_max)
1191 {
1192     struct island *is_orth;
1193     int d1, d2, *dsf = is->state->solver->dsf;
1194     game_state *state = is->state; /* for DINDEX */
1195
1196     is_orth = INDEX(is->state, gridi,
1197                     ISLAND_ORTHX(is, direction),
1198                     ISLAND_ORTHY(is, direction));
1199     assert(is_orth);
1200     /*debug(("...joining (%d,%d) to (%d,%d) with %d bridge(s).\n",
1201            is->x, is->y, is_orth->x, is_orth->y, n));*/
1202     island_join(is, is_orth, n, is_max);
1203
1204     if (n > 0 && !is_max) {
1205         d1 = DINDEX(is->x, is->y);
1206         d2 = DINDEX(is_orth->x, is_orth->y);
1207         if (dsf_canonify(dsf, d1) != dsf_canonify(dsf, d2))
1208             dsf_merge(dsf, d1, d2);
1209     }
1210 }
1211
1212 static int solve_fillone(struct island *is)
1213 {
1214     int i, nadded = 0;
1215
1216     debug(("solve_fillone for island (%d,%d).\n", is->x, is->y));
1217
1218     for (i = 0; i < is->adj.npoints; i++) {
1219         if (island_isadj(is, i)) {
1220             if (island_hasbridge(is, i)) {
1221                 /* already attached; do nothing. */;
1222             } else {
1223                 solve_join(is, i, 1, 0);
1224                 nadded++;
1225             }
1226         }
1227     }
1228     return nadded;
1229 }
1230
1231 static int solve_fill(struct island *is)
1232 {
1233     /* for each unmarked adjacent, make sure we convert every possible bridge
1234      * to a real one, and then work out the possibles afresh. */
1235     int i, nnew, ncurr, nadded = 0, missing;
1236
1237     debug(("solve_fill for island (%d,%d).\n", is->x, is->y));
1238
1239     missing = is->count - island_countbridges(is);
1240     if (missing < 0) return 0;
1241
1242     /* very like island_countspaces. */
1243     for (i = 0; i < is->adj.npoints; i++) {
1244         nnew = island_adjspace(is, 1, missing, i);
1245         if (nnew) {
1246             ncurr = GRIDCOUNT(is->state,
1247                               is->adj.points[i].x, is->adj.points[i].y,
1248                               is->adj.points[i].dx ? G_LINEH : G_LINEV);
1249
1250             solve_join(is, i, nnew + ncurr, 0);
1251             nadded += nnew;
1252         }
1253     }
1254     return nadded;
1255 }
1256
1257 static int solve_island_stage1(struct island *is, int *didsth_r)
1258 {
1259     int bridges = island_countbridges(is);
1260     int nspaces = island_countspaces(is, 1);
1261     int nadj = island_countadj(is);
1262     int didsth = 0;
1263
1264     assert(didsth_r);
1265
1266     /*debug(("island at (%d,%d) filled %d/%d (%d spc) nadj %d\n",
1267            is->x, is->y, bridges, is->count, nspaces, nadj));*/
1268     if (bridges > is->count) {
1269         /* We only ever add bridges when we're sure they fit, or that's
1270          * the only place they can go. If we've added bridges such that
1271          * another island has become wrong, the puzzle must not have had
1272          * a solution. */
1273         debug(("...island at (%d,%d) is overpopulated!\n", is->x, is->y));
1274         return 0;
1275     } else if (bridges == is->count) {
1276         /* This island is full. Make sure it's marked (and update
1277          * possibles if we did). */
1278         if (!(GRID(is->state, is->x, is->y) & G_MARK)) {
1279             debug(("...marking island (%d,%d) as full.\n", is->x, is->y));
1280             island_togglemark(is);
1281             didsth = 1;
1282         }
1283     } else if (GRID(is->state, is->x, is->y) & G_MARK) {
1284         debug(("...island (%d,%d) is marked but unfinished!\n",
1285                is->x, is->y));
1286         return 0; /* island has been marked unfinished; no solution from here. */
1287     } else {
1288         /* This is the interesting bit; we try and fill in more information
1289          * about this island. */
1290         if (is->count == bridges + nspaces) {
1291             if (solve_fill(is) > 0) didsth = 1;
1292         } else if (is->count > ((nadj-1) * is->state->maxb)) {
1293             /* must have at least one bridge in each possible direction. */
1294             if (solve_fillone(is) > 0) didsth = 1;
1295         }
1296     }
1297     if (didsth) {
1298         map_update_possibles(is->state);
1299         *didsth_r = 1;
1300     }
1301     return 1;
1302 }
1303
1304 /* returns non-zero if a new line here would cause a loop. */
1305 static int solve_island_checkloop(struct island *is, int direction)
1306 {
1307     struct island *is_orth;
1308     int *dsf = is->state->solver->dsf, d1, d2;
1309     game_state *state = is->state;
1310
1311     if (is->state->allowloops) return 0; /* don't care anyway */
1312     if (island_hasbridge(is, direction)) return 0; /* already has a bridge */
1313     if (island_isadj(is, direction) == 0) return 0; /* no adj island */
1314
1315     is_orth = INDEX(is->state, gridi,
1316                     ISLAND_ORTHX(is,direction),
1317                     ISLAND_ORTHY(is,direction));
1318     if (!is_orth) return 0;
1319
1320     d1 = DINDEX(is->x, is->y);
1321     d2 = DINDEX(is_orth->x, is_orth->y);
1322     if (dsf_canonify(dsf, d1) == dsf_canonify(dsf, d2)) {
1323         /* two islands are connected already; don't join them. */
1324         return 1;
1325     }
1326     return 0;
1327 }
1328
1329 static int solve_island_stage2(struct island *is, int *didsth_r)
1330 {
1331     int added = 0, removed = 0, navail = 0, nadj, i;
1332
1333     assert(didsth_r);
1334
1335     for (i = 0; i < is->adj.npoints; i++) {
1336         if (solve_island_checkloop(is, i)) {
1337             debug(("removing possible loop at (%d,%d) direction %d.\n",
1338                    is->x, is->y, i));
1339             solve_join(is, i, -1, 0);
1340             map_update_possibles(is->state);
1341             removed = 1;
1342         } else {
1343             navail += island_isadj(is, i);
1344             /*debug(("stage2: navail for (%d,%d) direction (%d,%d) is %d.\n",
1345                    is->x, is->y,
1346                    is->adj.points[i].dx, is->adj.points[i].dy,
1347                    island_isadj(is, i)));*/
1348         }
1349     }
1350
1351     /*debug(("island at (%d,%d) navail %d: checking...\n", is->x, is->y, navail));*/
1352
1353     for (i = 0; i < is->adj.npoints; i++) {
1354         if (!island_hasbridge(is, i)) {
1355             nadj = island_isadj(is, i);
1356             if (nadj > 0 && (navail - nadj) < is->count) {
1357                 /* we couldn't now complete the island without at
1358                  * least one bridge here; put it in. */
1359                 /*debug(("nadj %d, navail %d, is->count %d.\n",
1360                        nadj, navail, is->count));*/
1361                 debug(("island at (%d,%d) direction (%d,%d) must have 1 bridge\n",
1362                        is->x, is->y,
1363                        is->adj.points[i].dx, is->adj.points[i].dy));
1364                 solve_join(is, i, 1, 0);
1365                 added = 1;
1366                 /*debug_state(is->state);
1367                 debug_possibles(is->state);*/
1368             }
1369         }
1370     }
1371     if (added) map_update_possibles(is->state);
1372     if (added || removed) *didsth_r = 1;
1373     return 1;
1374 }
1375
1376 static int solve_island_subgroup(struct island *is, int direction, int n)
1377 {
1378     struct island *is_join;
1379     int nislands, *dsf = is->state->solver->dsf;
1380     game_state *state = is->state;
1381
1382     debug(("..checking subgroups.\n"));
1383
1384     /* if is isn't full, return 0. */
1385     if (n < is->count) {
1386         debug(("...orig island (%d,%d) not full.\n", is->x, is->y));
1387         return 0;
1388     }
1389
1390     is_join = INDEX(state, gridi,
1391                     ISLAND_ORTHX(is, direction),
1392                     ISLAND_ORTHY(is, direction));
1393     assert(is_join);
1394
1395     /* if is_join isn't full, return 0. */
1396     if (island_countbridges(is_join) < is_join->count) {
1397         debug(("...dest island (%d,%d) not full.\n", is_join->x, is_join->y));
1398         return 0;
1399     }
1400
1401     /* Check group membership for is->dsf; if it's full return 1. */
1402     if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)),
1403                         0, &nislands)) {
1404         if (nislands < state->n_islands) {
1405             /* we have a full subgroup that isn't the whole set.
1406              * This isn't allowed. */
1407             debug(("island at (%d,%d) makes full subgroup, disallowing.\n",
1408                    is->x, is->y, n));
1409             return 1;
1410         } else {
1411             debug(("...has finished puzzle.\n"));
1412         }
1413     }
1414     return 0;
1415 }
1416
1417 static int solve_island_impossible(game_state *state)
1418 {
1419     struct island *is;
1420     int i;
1421
1422     /* If any islands are impossible, return 1. */
1423     for (i = 0; i < state->n_islands; i++) {
1424         is = &state->islands[i];
1425         if (island_impossible(is, 0)) {
1426             debug(("island at (%d,%d) has become impossible, disallowing.\n",
1427                    is->x, is->y));
1428             return 1;
1429         }
1430     }
1431     return 0;
1432 }
1433
1434 /* Bear in mind that this function is really rather inefficient. */
1435 static int solve_island_stage3(struct island *is, int *didsth_r)
1436 {
1437     int i, n, x, y, missing, spc, curr, maxb, didsth = 0;
1438     int wh = is->state->w * is->state->h;
1439     struct solver_state *ss = is->state->solver;
1440
1441     assert(didsth_r);
1442
1443     missing = is->count - island_countbridges(is);
1444     if (missing <= 0) return 1;
1445
1446     for (i = 0; i < is->adj.npoints; i++) {
1447         /* We only do right- or down-pointing bridges. */
1448         if (is->adj.points[i].dx == -1 ||
1449             is->adj.points[i].dy == -1) continue;
1450
1451         x = is->adj.points[i].x;
1452         y = is->adj.points[i].y;
1453         spc = island_adjspace(is, 1, missing, i);
1454         if (spc == 0) continue;
1455
1456         curr = GRIDCOUNT(is->state, x, y,
1457                          is->adj.points[i].dx ? G_LINEH : G_LINEV);
1458         debug(("island at (%d,%d) s3, trying %d - %d bridges.\n",
1459                is->x, is->y, curr+1, curr+spc));
1460
1461         /* Now we know that this island could have more bridges,
1462          * to bring the total from curr+1 to curr+spc. */
1463         maxb = -1;
1464         /* We have to squirrel the dsf away and restore it afterwards;
1465          * it is additive only, and can't be removed from. */
1466         memcpy(ss->tmpdsf, ss->dsf, wh*sizeof(int));
1467         for (n = curr+1; n <= curr+spc; n++) {
1468             solve_join(is, i, n, 0);
1469             map_update_possibles(is->state);
1470
1471             if (solve_island_subgroup(is, i, n) ||
1472                 solve_island_impossible(is->state)) {
1473                 maxb = n-1;
1474                 debug(("island at (%d,%d) d(%d,%d) new max of %d bridges:\n",
1475                        is->x, is->y,
1476                        is->adj.points[i].dx, is->adj.points[i].dy,
1477                        maxb));
1478                 break;
1479             }
1480         }
1481         solve_join(is, i, curr, 0); /* put back to before. */
1482         memcpy(ss->dsf, ss->tmpdsf, wh*sizeof(int));
1483
1484         if (maxb != -1) {
1485             /*debug_state(is->state);*/
1486             if (maxb == 0) {
1487                 debug(("...adding NOLINE.\n"));
1488                 solve_join(is, i, -1, 0); /* we can't have any bridges here. */
1489                 didsth = 1;
1490             } else {
1491                 debug(("...setting maximum\n"));
1492                 solve_join(is, i, maxb, 1);
1493             }
1494         }
1495         map_update_possibles(is->state);
1496     }
1497     if (didsth) *didsth_r = didsth;
1498     return 1;
1499 }
1500
1501 #define CONTINUE_IF_FULL do {                           \
1502 if (GRID(state, is->x, is->y) & G_MARK) {            \
1503     /* island full, don't try fixing it */           \
1504     continue;                                        \
1505 } } while(0)
1506
1507 static int solve_sub(game_state *state, int difficulty, int depth)
1508 {
1509     struct island *is;
1510     int i, didsth;
1511
1512     while (1) {
1513         didsth = 0;
1514
1515         /* First island iteration: things we can work out by looking at
1516          * properties of the island as a whole. */
1517         for (i = 0; i < state->n_islands; i++) {
1518             is = &state->islands[i];
1519             if (!solve_island_stage1(is, &didsth)) return 0;
1520         }
1521         if (didsth) continue;
1522         else if (difficulty < 1) break;
1523
1524         /* Second island iteration: thing we can work out by looking at
1525          * properties of individual island connections. */
1526         for (i = 0; i < state->n_islands; i++) {
1527             is = &state->islands[i];
1528             CONTINUE_IF_FULL;
1529             if (!solve_island_stage2(is, &didsth)) return 0;
1530         }
1531         if (didsth) continue;
1532         else if (difficulty < 2) break;
1533
1534         /* Third island iteration: things we can only work out by looking
1535          * at groups of islands. */
1536         for (i = 0; i < state->n_islands; i++) {
1537             is = &state->islands[i];
1538             if (!solve_island_stage3(is, &didsth)) return 0;
1539         }
1540         if (didsth) continue;
1541         else if (difficulty < 3) break;
1542
1543         /* If we can be bothered, write a recursive solver to finish here. */
1544         break;
1545     }
1546     if (map_check(state)) return 1; /* solved it */
1547     return 0;
1548 }
1549
1550 static void solve_for_hint(game_state *state)
1551 {
1552     map_group(state);
1553     solve_sub(state, 10, 0);
1554 }
1555
1556 static int solve_from_scratch(game_state *state, int difficulty)
1557 {
1558     map_clear(state);
1559     map_group(state);
1560     map_update_possibles(state);
1561     return solve_sub(state, difficulty, 0);
1562 }
1563
1564 /* --- New game functions --- */
1565
1566 static game_state *new_state(game_params *params)
1567 {
1568     game_state *ret = snew(game_state);
1569     int wh = params->w * params->h, i;
1570
1571     ret->w = params->w;
1572     ret->h = params->h;
1573     ret->allowloops = params->allowloops;
1574     ret->maxb = params->maxb;
1575     ret->params = *params;
1576
1577     ret->grid = snewn(wh, grid_type);
1578     memset(ret->grid, 0, GRIDSZ(ret));
1579     ret->scratch = snewn(wh, grid_type);
1580     memset(ret->scratch, 0, GRIDSZ(ret));
1581
1582     ret->wha = snewn(wh*N_WH_ARRAYS, char);
1583     memset(ret->wha, 0, wh*N_WH_ARRAYS*sizeof(char));
1584
1585     ret->possv = ret->wha;
1586     ret->possh = ret->wha + wh;
1587     ret->lines = ret->wha + wh*2;
1588     ret->maxv = ret->wha + wh*3;
1589     ret->maxh = ret->wha + wh*4;
1590
1591     memset(ret->maxv, ret->maxb, wh*sizeof(char));
1592     memset(ret->maxh, ret->maxb, wh*sizeof(char));
1593
1594     ret->islands = NULL;
1595     ret->n_islands = 0;
1596     ret->n_islands_alloc = 0;
1597
1598     ret->gridi = snewn(wh, struct island *);
1599     for (i = 0; i < wh; i++) ret->gridi[i] = NULL;
1600
1601     ret->solved = ret->completed = 0;
1602
1603     ret->solver = snew(struct solver_state);
1604     ret->solver->dsf = snew_dsf(wh);
1605     ret->solver->tmpdsf = snewn(wh, int);
1606
1607     ret->solver->refcount = 1;
1608
1609     return ret;
1610 }
1611
1612 static game_state *dup_game(game_state *state)
1613 {
1614     game_state *ret = snew(game_state);
1615     int wh = state->w*state->h;
1616
1617     ret->w = state->w;
1618     ret->h = state->h;
1619     ret->allowloops = state->allowloops;
1620     ret->maxb = state->maxb;
1621     ret->params = state->params;
1622
1623     ret->grid = snewn(wh, grid_type);
1624     memcpy(ret->grid, state->grid, GRIDSZ(ret));
1625     ret->scratch = snewn(wh, grid_type);
1626     memcpy(ret->scratch, state->scratch, GRIDSZ(ret));
1627
1628     ret->wha = snewn(wh*N_WH_ARRAYS, char);
1629     memcpy(ret->wha, state->wha, wh*N_WH_ARRAYS*sizeof(char));
1630
1631     ret->possv = ret->wha;
1632     ret->possh = ret->wha + wh;
1633     ret->lines = ret->wha + wh*2;
1634     ret->maxv = ret->wha + wh*3;
1635     ret->maxh = ret->wha + wh*4;
1636
1637     ret->islands = snewn(state->n_islands, struct island);
1638     memcpy(ret->islands, state->islands, state->n_islands * sizeof(struct island));
1639     ret->n_islands = ret->n_islands_alloc = state->n_islands;
1640
1641     ret->gridi = snewn(wh, struct island *);
1642     fixup_islands_for_realloc(ret);
1643
1644     ret->solved = state->solved;
1645     ret->completed = state->completed;
1646
1647     ret->solver = state->solver;
1648     ret->solver->refcount++;
1649
1650     return ret;
1651 }
1652
1653 static void free_game(game_state *state)
1654 {
1655     if (--state->solver->refcount <= 0) {
1656         sfree(state->solver->dsf);
1657         sfree(state->solver->tmpdsf);
1658         sfree(state->solver);
1659     }
1660
1661     sfree(state->islands);
1662     sfree(state->gridi);
1663
1664     sfree(state->wha);
1665
1666     sfree(state->scratch);
1667     sfree(state->grid);
1668     sfree(state);
1669 }
1670
1671 #define MAX_NEWISLAND_TRIES     50
1672
1673 #define ORDER(a,b) do { if (a < b) { int tmp=a; int a=b; int b=tmp; } } while(0)
1674
1675 static char *new_game_desc(game_params *params, random_state *rs,
1676                            char **aux, int interactive)
1677 {
1678     game_state *tobuild  = NULL;
1679     int i, j, wh = params->w * params->h, x, y, dx, dy;
1680     int minx, miny, maxx, maxy, joinx, joiny, newx, newy, diffx, diffy;
1681     int ni_req = max((params->islands * wh) / 100, 2), ni_curr, ni_bad;
1682     struct island *is, *is2;
1683     char *ret;
1684     unsigned int echeck;
1685
1686     /* pick a first island position randomly. */
1687 generate:
1688     if (tobuild) free_game(tobuild);
1689     tobuild = new_state(params);
1690
1691     x = random_upto(rs, params->w);
1692     y = random_upto(rs, params->h);
1693     island_add(tobuild, x, y, 0);
1694     ni_curr = 1;
1695     ni_bad = 0;
1696     debug(("Created initial island at (%d,%d).\n", x, y));
1697
1698     while (ni_curr < ni_req) {
1699         /* Pick a random island to try and extend from. */
1700         i = random_upto(rs, tobuild->n_islands);
1701         is = &tobuild->islands[i];
1702
1703         /* Pick a random direction to extend in. */
1704         j = random_upto(rs, is->adj.npoints);
1705         dx = is->adj.points[j].x - is->x;
1706         dy = is->adj.points[j].y - is->y;
1707
1708         /* Find out limits of where we could put a new island. */
1709         joinx = joiny = -1;
1710         minx = is->x + 2*dx; miny = is->y + 2*dy; /* closest is 2 units away. */
1711         x = is->x+dx; y = is->y+dy;
1712         if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) {
1713             /* already a line next to the island, continue. */
1714             goto bad;
1715         }
1716         while (1) {
1717             if (x < 0 || x >= params->w || y < 0 || y >= params->h) {
1718                 /* got past the edge; put a possible at the island
1719                  * and exit. */
1720                 maxx = x-dx; maxy = y-dy;
1721                 goto foundmax;
1722             }
1723             if (GRID(tobuild,x,y) & G_ISLAND) {
1724                 /* could join up to an existing island... */
1725                 joinx = x; joiny = y;
1726                 /* ... or make a new one 2 spaces away. */
1727                 maxx = x - 2*dx; maxy = y - 2*dy;
1728                 goto foundmax;
1729             } else if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) {
1730                 /* could make a new one 1 space away from the line. */
1731                 maxx = x - dx; maxy = y - dy;
1732                 goto foundmax;
1733             }
1734             x += dx; y += dy;
1735         }
1736
1737 foundmax:
1738         debug(("Island at (%d,%d) with d(%d,%d) has new positions "
1739                "(%d,%d) -> (%d,%d), join (%d,%d).\n",
1740                is->x, is->y, dx, dy, minx, miny, maxx, maxy, joinx, joiny));
1741         /* Now we know where we could either put a new island
1742          * (between min and max), or (if loops are allowed) could join on
1743          * to an existing island (at join). */
1744         if (params->allowloops && joinx != -1 && joiny != -1) {
1745             if (random_upto(rs, 100) < (unsigned long)params->expansion) {
1746                 is2 = INDEX(tobuild, gridi, joinx, joiny);
1747                 debug(("Joining island at (%d,%d) to (%d,%d).\n",
1748                        is->x, is->y, is2->x, is2->y));
1749                 goto join;
1750             }
1751         }
1752         diffx = (maxx - minx) * dx;
1753         diffy = (maxy - miny) * dy;
1754         if (diffx < 0 || diffy < 0)  goto bad;
1755         if (random_upto(rs,100) < (unsigned long)params->expansion) {
1756             newx = maxx; newy = maxy;
1757             debug(("Creating new island at (%d,%d) (expanded).\n", newx, newy));
1758         } else {
1759             newx = minx + random_upto(rs,diffx+1)*dx;
1760             newy = miny + random_upto(rs,diffy+1)*dy;
1761             debug(("Creating new island at (%d,%d).\n", newx, newy));
1762         }
1763         /* check we're not next to island in the other orthogonal direction. */
1764         if ((INGRID(tobuild,newx+dy,newy+dx) && (GRID(tobuild,newx+dy,newy+dx) & G_ISLAND)) ||
1765             (INGRID(tobuild,newx-dy,newy-dx) && (GRID(tobuild,newx-dy,newy-dx) & G_ISLAND))) {
1766             debug(("New location is adjacent to island, skipping.\n"));
1767             goto bad;
1768         }
1769         is2 = island_add(tobuild, newx, newy, 0);
1770         /* Must get is again at this point; the array might have
1771          * been realloced by island_add... */
1772         is = &tobuild->islands[i]; /* ...but order will not change. */
1773
1774         ni_curr++; ni_bad = 0;
1775 join:
1776         island_join(is, is2, random_upto(rs, tobuild->maxb)+1, 0);
1777         debug_state(tobuild);
1778         continue;
1779
1780 bad:
1781         ni_bad++;
1782         if (ni_bad > MAX_NEWISLAND_TRIES) {
1783             debug(("Unable to create any new islands after %d tries; "
1784                    "created %d [%d%%] (instead of %d [%d%%] requested).\n",
1785                    MAX_NEWISLAND_TRIES,
1786                    ni_curr, ni_curr * 100 / wh,
1787                    ni_req, ni_req * 100 / wh));
1788             goto generated;
1789         }
1790     }
1791
1792 generated:
1793     if (ni_curr == 1) {
1794         debug(("Only generated one island (!), retrying.\n"));
1795         goto generate;
1796     }
1797     /* Check we have at least one island on each extremity of the grid. */
1798     echeck = 0;
1799     for (x = 0; x < params->w; x++) {
1800         if (INDEX(tobuild, gridi, x, 0))           echeck |= 1;
1801         if (INDEX(tobuild, gridi, x, params->h-1)) echeck |= 2;
1802     }
1803     for (y = 0; y < params->h; y++) {
1804         if (INDEX(tobuild, gridi, 0,           y)) echeck |= 4;
1805         if (INDEX(tobuild, gridi, params->w-1, y)) echeck |= 8;
1806     }
1807     if (echeck != 15) {
1808         debug(("Generated grid doesn't fill to sides, retrying.\n"));
1809         goto generate;
1810     }
1811
1812     map_count(tobuild);
1813     map_find_orthogonal(tobuild);
1814
1815     if (params->difficulty > 0) {
1816         if (solve_from_scratch(tobuild, params->difficulty-1) > 0) {
1817             debug(("Grid is solvable at difficulty %d (too easy); retrying.\n",
1818                    params->difficulty-1));
1819             goto generate;
1820         }
1821     }
1822
1823     if (solve_from_scratch(tobuild, params->difficulty) == 0) {
1824         debug(("Grid not solvable at difficulty %d, (too hard); retrying.\n",
1825                params->difficulty));
1826         goto generate;
1827     }
1828
1829     /* ... tobuild is now solved. We rely on this making the diff for aux. */
1830     debug_state(tobuild);
1831     ret = encode_game(tobuild);
1832     {
1833         game_state *clean = dup_game(tobuild);
1834         map_clear(clean);
1835         map_update_possibles(clean);
1836         *aux = game_state_diff(clean, tobuild);
1837         free_game(clean);
1838     }
1839     free_game(tobuild);
1840
1841     return ret;
1842 }
1843
1844 static char *validate_desc(game_params *params, char *desc)
1845 {
1846     int i, wh = params->w * params->h;
1847
1848     for (i = 0; i < wh; i++) {
1849         if (*desc >= '1' && *desc <= '9')
1850             /* OK */;
1851         else if (*desc >= 'a' && *desc <= 'z')
1852             i += *desc - 'a'; /* plus the i++ */
1853         else if (*desc >= 'A' && *desc <= 'G')
1854             /* OK */;
1855         else if (*desc == 'V' || *desc == 'W' ||
1856                  *desc == 'X' || *desc == 'Y' ||
1857                  *desc == 'H' || *desc == 'I' ||
1858                  *desc == 'J' || *desc == 'K')
1859             /* OK */;
1860         else if (!*desc)
1861             return "Game description shorter than expected";
1862         else
1863             return "Game description containers unexpected character";
1864         desc++;
1865     }
1866     if (*desc || i > wh)
1867         return "Game description longer than expected";
1868
1869     return NULL;
1870 }
1871
1872 static game_state *new_game_sub(game_params *params, char *desc)
1873 {
1874     game_state *state = new_state(params);
1875     int x, y, run = 0;
1876
1877     debug(("new_game[_sub]: desc = '%s'.\n", desc));
1878
1879     for (y = 0; y < params->h; y++) {
1880         for (x = 0; x < params->w; x++) {
1881             char c = '\0';
1882
1883             if (run == 0) {
1884                 c = *desc++;
1885                 assert(c != 'S');
1886                 if (c >= 'a' && c <= 'z')
1887                     run = c - 'a' + 1;
1888             }
1889
1890             if (run > 0) {
1891                 c = 'S';
1892                 run--;
1893             }
1894
1895             switch (c) {
1896             case '1': case '2': case '3': case '4':
1897             case '5': case '6': case '7': case '8': case '9':
1898                 island_add(state, x, y, (c - '0'));
1899                 break;
1900
1901             case 'A': case 'B': case 'C': case 'D':
1902             case 'E': case 'F': case 'G':
1903                 island_add(state, x, y, (c - 'A') + 10);
1904                 break;
1905
1906             case 'S':
1907                 /* empty square */
1908                 break;
1909
1910             default:
1911                 assert(!"Malformed desc.");
1912                 break;
1913             }
1914         }
1915     }
1916     if (*desc) assert(!"Over-long desc.");
1917
1918     map_find_orthogonal(state);
1919     map_update_possibles(state);
1920
1921     return state;
1922 }
1923
1924 static game_state *new_game(midend *me, game_params *params, char *desc)
1925 {
1926     return new_game_sub(params, desc);
1927 }
1928
1929 struct game_ui {
1930     int dragx_src, dragy_src;   /* source; -1 means no drag */
1931     int dragx_dst, dragy_dst;   /* src's closest orth island. */
1932     grid_type todraw;
1933     int dragging, drag_is_noline, nlines;
1934 };
1935
1936 static char *ui_cancel_drag(game_ui *ui)
1937 {
1938     ui->dragx_src = ui->dragy_src = -1;
1939     ui->dragx_dst = ui->dragy_dst = -1;
1940     ui->dragging = 0;
1941     return "";
1942 }
1943
1944 static game_ui *new_ui(game_state *state)
1945 {
1946     game_ui *ui = snew(game_ui);
1947     ui_cancel_drag(ui);
1948     return ui;
1949 }
1950
1951 static void free_ui(game_ui *ui)
1952 {
1953     sfree(ui);
1954 }
1955
1956 static char *encode_ui(game_ui *ui)
1957 {
1958     return NULL;
1959 }
1960
1961 static void decode_ui(game_ui *ui, char *encoding)
1962 {
1963 }
1964
1965 static void game_changed_state(game_ui *ui, game_state *oldstate,
1966                                game_state *newstate)
1967 {
1968 }
1969
1970 struct game_drawstate {
1971     int tilesize;
1972     int w, h;
1973     grid_type *grid;
1974     int *lv, *lh;
1975     int started, dragging;
1976 };
1977
1978 static char *update_drag_dst(game_state *state, game_ui *ui, game_drawstate *ds,
1979                             int nx, int ny)
1980 {
1981     int ox, oy, dx, dy, i, currl, maxb;
1982     struct island *is;
1983     grid_type gtype, ntype, mtype, curr;
1984
1985     if (ui->dragx_src == -1 || ui->dragy_src == -1) return NULL;
1986
1987     ui->dragx_dst = -1;
1988     ui->dragy_dst = -1;
1989
1990     /* work out which of the four directions we're closest to... */
1991     ox = COORD(ui->dragx_src) + TILE_SIZE/2;
1992     oy = COORD(ui->dragy_src) + TILE_SIZE/2;
1993
1994     if (abs(nx-ox) < abs(ny-oy)) {
1995         dx = 0;
1996         dy = (ny-oy) < 0 ? -1 : 1;
1997         gtype = G_LINEV; ntype = G_NOLINEV; mtype = G_MARKV;
1998         maxb = INDEX(state, maxv, ui->dragx_src+dx, ui->dragy_src+dy);
1999     } else {
2000         dy = 0;
2001         dx = (nx-ox) < 0 ? -1 : 1;
2002         gtype = G_LINEH; ntype = G_NOLINEH; mtype = G_MARKH;
2003         maxb = INDEX(state, maxh, ui->dragx_src+dx, ui->dragy_src+dy);
2004     }
2005     if (ui->drag_is_noline) {
2006         ui->todraw = ntype;
2007     } else {
2008         curr = GRID(state, ui->dragx_src+dx, ui->dragy_src+dy);
2009         currl = INDEX(state, lines, ui->dragx_src+dx, ui->dragy_src+dy);
2010
2011         if (curr & gtype) {
2012             if (currl == maxb) {
2013                 ui->todraw = 0;
2014                 ui->nlines = 0;
2015             } else {
2016                 ui->todraw = gtype;
2017                 ui->nlines = currl + 1;
2018             }
2019         } else {
2020             ui->todraw = gtype;
2021             ui->nlines = 1;
2022         }
2023     }
2024
2025     /* ... and see if there's an island off in that direction. */
2026     is = INDEX(state, gridi, ui->dragx_src, ui->dragy_src);
2027     for (i = 0; i < is->adj.npoints; i++) {
2028         if (is->adj.points[i].off == 0) continue;
2029         curr = GRID(state, is->x+dx, is->y+dy);
2030         if (curr & mtype) continue; /* don't allow changes to marked lines. */
2031         if (ui->drag_is_noline) {
2032             if (curr & gtype) continue; /* no no-line where already a line */
2033         } else {
2034             if (POSSIBLES(state, dx, is->x+dx, is->y+dy) == 0) continue; /* no line if !possible. */
2035             if (curr & ntype) continue; /* can't have a bridge where there's a no-line. */
2036         }
2037
2038         if (is->adj.points[i].dx == dx &&
2039             is->adj.points[i].dy == dy) {
2040             ui->dragx_dst = ISLAND_ORTHX(is,i);
2041             ui->dragy_dst = ISLAND_ORTHY(is,i);
2042         }
2043     }
2044     /*debug(("update_drag src (%d,%d) d(%d,%d) dst (%d,%d)\n",
2045            ui->dragx_src, ui->dragy_src, dx, dy,
2046            ui->dragx_dst, ui->dragy_dst));*/
2047     return "";
2048 }
2049
2050 static char *finish_drag(game_state *state, game_ui *ui)
2051 {
2052     char buf[80];
2053
2054     if (ui->dragx_src == -1 || ui->dragy_src == -1)
2055         return NULL;
2056     if (ui->dragx_dst == -1 || ui->dragy_dst == -1)
2057         return ui_cancel_drag(ui);
2058
2059     if (ui->drag_is_noline) {
2060         sprintf(buf, "N%d,%d,%d,%d",
2061                 ui->dragx_src, ui->dragy_src,
2062                 ui->dragx_dst, ui->dragy_dst);
2063     } else {
2064         sprintf(buf, "L%d,%d,%d,%d,%d",
2065                 ui->dragx_src, ui->dragy_src,
2066                 ui->dragx_dst, ui->dragy_dst, ui->nlines);
2067     }
2068
2069     ui_cancel_drag(ui);
2070
2071     return dupstr(buf);
2072 }
2073
2074 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
2075                             int x, int y, int button)
2076 {
2077     int gx = FROMCOORD(x), gy = FROMCOORD(y);
2078     char buf[80], *ret;
2079     grid_type ggrid = INGRID(state,gx,gy) ? GRID(state,gx,gy) : 0;
2080
2081     if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
2082         if (!INGRID(state, gx, gy)) return NULL;
2083         if ((ggrid & G_ISLAND) && !(ggrid & G_MARK)) {
2084             ui->dragx_src = gx;
2085             ui->dragy_src = gy;
2086             return "";
2087         } else
2088             return ui_cancel_drag(ui);
2089     } else if (button == LEFT_DRAG || button == RIGHT_DRAG) {
2090         if (gx != ui->dragx_src || gy != ui->dragy_src) {
2091             ui->dragging = 1;
2092             ui->drag_is_noline = (button == RIGHT_DRAG) ? 1 : 0;
2093             return update_drag_dst(state, ui, ds, x, y);
2094         } else {
2095             /* cancel a drag when we go back to the starting point */
2096             ui->dragx_dst = -1;
2097             ui->dragy_dst = -1;
2098             return "";
2099         }
2100     } else if (button == LEFT_RELEASE || button == RIGHT_RELEASE) {
2101         if (ui->dragging) {
2102             return finish_drag(state, ui);
2103         } else {
2104             ui_cancel_drag(ui);
2105             if (!INGRID(state, gx, gy)) return NULL;
2106             if (!(GRID(state, gx, gy) & G_ISLAND)) return NULL;
2107             sprintf(buf, "M%d,%d", gx, gy);
2108             return dupstr(buf);
2109         }
2110     } else if (button == 'h' || button == 'H') {
2111         game_state *solved = dup_game(state);
2112         solve_for_hint(solved);
2113         ret = game_state_diff(state, solved);
2114         free_game(solved);
2115         return ret;
2116     }
2117
2118     return NULL;
2119 }
2120
2121 static game_state *execute_move(game_state *state, char *move)
2122 {
2123     game_state *ret = dup_game(state);
2124     int x1, y1, x2, y2, nl, n;
2125     struct island *is1, *is2;
2126     char c;
2127
2128     debug(("execute_move: %s\n", move));
2129
2130     if (!*move) goto badmove;
2131     while (*move) {
2132         c = *move++;
2133         if (c == 'S') {
2134             ret->solved = TRUE;
2135             n = 0;
2136         } else if (c == 'L') {
2137             if (sscanf(move, "%d,%d,%d,%d,%d%n",
2138                        &x1, &y1, &x2, &y2, &nl, &n) != 5)
2139                 goto badmove;
2140             is1 = INDEX(ret, gridi, x1, y1);
2141             is2 = INDEX(ret, gridi, x2, y2);
2142             if (!is1 || !is2) goto badmove;
2143             if (nl < 0 || nl > state->maxb) goto badmove;
2144             island_join(is1, is2, nl, 0);
2145         } else if (c == 'N') {
2146             if (sscanf(move, "%d,%d,%d,%d%n",
2147                        &x1, &y1, &x2, &y2, &n) != 4)
2148                 goto badmove;
2149             is1 = INDEX(ret, gridi, x1, y1);
2150             is2 = INDEX(ret, gridi, x2, y2);
2151             if (!is1 || !is2) goto badmove;
2152             island_join(is1, is2, -1, 0);
2153         } else if (c == 'M') {
2154             if (sscanf(move, "%d,%d%n",
2155                        &x1, &y1, &n) != 2)
2156                 goto badmove;
2157             is1 = INDEX(ret, gridi, x1, y1);
2158             if (!is1) goto badmove;
2159             island_togglemark(is1);
2160         } else
2161             goto badmove;
2162
2163         move += n;
2164         if (*move == ';')
2165             move++;
2166         else if (*move) goto badmove;
2167     }
2168
2169     map_update_possibles(ret);
2170     if (map_check(ret)) {
2171         debug(("Game completed.\n"));
2172         ret->completed = 1;
2173     }
2174     return ret;
2175
2176 badmove:
2177     debug(("%s: unrecognised move.\n", move));
2178     free_game(ret);
2179     return NULL;
2180 }
2181
2182 static char *solve_game(game_state *state, game_state *currstate,
2183                         char *aux, char **error)
2184 {
2185     char *ret;
2186     game_state *solved;
2187
2188     if (aux) {
2189         debug(("solve_game: aux = %s\n", aux));
2190         solved = execute_move(state, aux);
2191         if (!solved) {
2192             *error = "Generated aux string is not a valid move (!).";
2193             return NULL;
2194         }
2195     } else {
2196         solved = dup_game(state);
2197         /* solve with max strength... */
2198         if (solve_from_scratch(solved, 10) == 0) {
2199             free_game(solved);
2200             *error = "Game does not have a (non-recursive) solution.";
2201             return NULL;
2202         }
2203     }
2204     ret = game_state_diff(currstate, solved);
2205     free_game(solved);
2206     debug(("solve_game: ret = %s\n", ret));
2207     return ret;
2208 }
2209
2210 /* ----------------------------------------------------------------------
2211  * Drawing routines.
2212  */
2213
2214 static void game_compute_size(game_params *params, int tilesize,
2215                               int *x, int *y)
2216 {
2217     /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2218     struct { int tilesize; } ads, *ds = &ads;
2219     ads.tilesize = tilesize;
2220
2221     *x = TILE_SIZE * params->w + 2 * BORDER;
2222     *y = TILE_SIZE * params->h + 2 * BORDER;
2223 }
2224
2225 static void game_set_size(drawing *dr, game_drawstate *ds,
2226                           game_params *params, int tilesize)
2227 {
2228     ds->tilesize = tilesize;
2229 }
2230
2231 static float *game_colours(frontend *fe, int *ncolours)
2232 {
2233     float *ret = snewn(3 * NCOLOURS, float);
2234     int i;
2235
2236     game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
2237
2238     for (i = 0; i < 3; i++) {
2239         ret[COL_FOREGROUND * 3 + i] = 0.0F;
2240         ret[COL_HINT * 3 + i] = ret[COL_LOWLIGHT * 3 + i];
2241         ret[COL_GRID * 3 + i] =
2242             (ret[COL_HINT * 3 + i] + ret[COL_BACKGROUND * 3 + i]) * 0.5F;
2243         ret[COL_MARK * 3 + i] = ret[COL_HIGHLIGHT * 3 + i];
2244     }
2245     ret[COL_WARNING * 3 + 0] = 1.0F;
2246     ret[COL_WARNING * 3 + 1] = 0.25F;
2247     ret[COL_WARNING * 3 + 2] = 0.25F;
2248
2249     ret[COL_SELECTED * 3 + 0] = 0.25F;
2250     ret[COL_SELECTED * 3 + 1] = 1.00F;
2251     ret[COL_SELECTED * 3 + 2] = 0.25F;
2252
2253     *ncolours = NCOLOURS;
2254     return ret;
2255 }
2256
2257 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
2258 {
2259     struct game_drawstate *ds = snew(struct game_drawstate);
2260     int wh = state->w*state->h;
2261
2262     ds->tilesize = 0;
2263     ds->w = state->w;
2264     ds->h = state->h;
2265     ds->started = 0;
2266     ds->grid = snewn(wh, grid_type);
2267     memset(ds->grid, -1, wh*sizeof(grid_type));
2268     ds->lv = snewn(wh, int);
2269     ds->lh = snewn(wh, int);
2270     memset(ds->lv, 0, wh*sizeof(int));
2271     memset(ds->lh, 0, wh*sizeof(int));
2272
2273     return ds;
2274 }
2275
2276 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
2277 {
2278     sfree(ds->lv);
2279     sfree(ds->lh);
2280     sfree(ds->grid);
2281     sfree(ds);
2282 }
2283
2284 #define LINE_WIDTH (TILE_SIZE/8)
2285 #define TS8(x) (((x)*TILE_SIZE)/8)
2286
2287 #define OFFSET(thing) ((TILE_SIZE/2) - ((thing)/2))
2288
2289 static void lines_vert(drawing *dr, game_drawstate *ds,
2290                        int ox, int oy, int lv, int col, grid_type v)
2291 {
2292     int lw = LINE_WIDTH, gw = LINE_WIDTH, bw, i, loff;
2293     while ((bw = lw * lv + gw * (lv+1)) > TILE_SIZE)
2294         gw--;
2295     loff = OFFSET(bw);
2296     if (v & G_MARKV)
2297         draw_rect(dr, ox + loff, oy, bw, TILE_SIZE, COL_MARK);
2298     for (i = 0; i < lv; i++, loff += lw + gw)
2299         draw_rect(dr, ox + loff + gw, oy, lw, TILE_SIZE, col);
2300 }
2301
2302 static void lines_horiz(drawing *dr, game_drawstate *ds,
2303                         int ox, int oy, int lh, int col, grid_type v)
2304 {
2305     int lw = LINE_WIDTH, gw = LINE_WIDTH, bw, i, loff;
2306     while ((bw = lw * lh + gw * (lh+1)) > TILE_SIZE)
2307         gw--;
2308     loff = OFFSET(bw);
2309     if (v & G_MARKH)
2310         draw_rect(dr, ox, oy + loff, TILE_SIZE, bw, COL_MARK);
2311     for (i = 0; i < lh; i++, loff += lw + gw)
2312         draw_rect(dr, ox, oy + loff + gw, TILE_SIZE, lw, col);
2313 }
2314
2315 static void line_cross(drawing *dr, game_drawstate *ds,
2316                       int ox, int oy, int col, grid_type v)
2317 {
2318     int off = TS8(2);
2319     draw_line(dr, ox,     oy, ox+off, oy+off, col);
2320     draw_line(dr, ox+off, oy, ox,     oy+off, col);
2321 }
2322
2323 static void lines_lvlh(game_state *state, int x, int y, grid_type v,
2324                        int *lv_r, int *lh_r)
2325 {
2326     int lh = 0, lv = 0;
2327
2328     if (v & G_LINEV) lv = INDEX(state,lines,x,y);
2329     if (v & G_LINEH) lh = INDEX(state,lines,x,y);
2330
2331 #ifdef DRAW_HINTS
2332     if (INDEX(state, possv, x, y) && !lv) {
2333         lv = INDEX(state, possv, x, y);
2334     }
2335     if (INDEX(state, possh, x, y) && !lh) {
2336         lh = INDEX(state, possh, x, y);
2337     }
2338 #endif
2339     /*debug(("lvlh: (%d,%d) v 0x%x lv %d lh %d.\n", x, y, v, lv, lh));*/
2340     *lv_r = lv; *lh_r = lh;
2341 }
2342
2343 static void dsf_debug_draw(drawing *dr,
2344                            game_state *state, game_drawstate *ds,
2345                            int x, int y)
2346 {
2347 #ifdef DRAW_DSF
2348     int ts = TILE_SIZE/2;
2349     int ox = COORD(x) + ts/2, oy = COORD(y) + ts/2;
2350     char str[10];
2351
2352     sprintf(str, "%d", dsf_canonify(state->solver->dsf, DINDEX(x,y)));
2353     draw_text(dr, ox, oy, FONT_VARIABLE, ts,
2354               ALIGN_VCENTRE | ALIGN_HCENTRE, COL_WARNING, str);
2355 #endif
2356 }
2357
2358 static void lines_redraw(drawing *dr,
2359                          game_state *state, game_drawstate *ds, game_ui *ui,
2360                          int x, int y, grid_type v, int lv, int lh)
2361 {
2362     int ox = COORD(x), oy = COORD(y);
2363     int vcol = (v & G_FLASH) ? COL_HIGHLIGHT :
2364         (v & G_WARN) ? COL_WARNING : COL_FOREGROUND, hcol = vcol;
2365     grid_type todraw = v & G_NOLINE;
2366
2367     if (v & G_ISSEL) {
2368         if (ui->todraw & G_FLAGSH) hcol = COL_SELECTED;
2369         if (ui->todraw & G_FLAGSV) vcol = COL_SELECTED;
2370         todraw |= ui->todraw;
2371     }
2372
2373     draw_rect(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
2374
2375 #ifdef DRAW_HINTS
2376     if (INDEX(state, possv, x, y) && !(v & G_LINEV))
2377         vcol = COL_HINT;
2378     if (INDEX(state, possh, x, y) && !(v & G_LINEH))
2379         hcol = COL_HINT;
2380 #endif
2381 #ifdef DRAW_GRID
2382     draw_rect_outline(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_GRID);
2383 #endif
2384
2385     if (todraw & G_NOLINEV) {
2386         line_cross(dr, ds, ox + TS8(3), oy + TS8(1), vcol, todraw);
2387         line_cross(dr, ds, ox + TS8(3), oy + TS8(5), vcol, todraw);
2388     }
2389     if (todraw & G_NOLINEH) {
2390         line_cross(dr, ds, ox + TS8(1), oy + TS8(3), hcol, todraw);
2391         line_cross(dr, ds, ox + TS8(5), oy + TS8(3), hcol, todraw);
2392     }
2393     if (lv)
2394         lines_vert(dr, ds, ox, oy, lv, vcol, v);
2395     if (lh)
2396         lines_horiz(dr, ds, ox, oy, lh, hcol, v);
2397
2398     dsf_debug_draw(dr, state, ds, x, y);
2399     draw_update(dr, ox, oy, TILE_SIZE, TILE_SIZE);
2400 }
2401
2402 #define ISLAND_RADIUS ((TILE_SIZE*12)/20)
2403 #define ISLAND_NUMSIZE(is) \
2404     (((is)->count < 10) ? (TILE_SIZE*7)/10 : (TILE_SIZE*5)/10)
2405
2406 static void island_redraw(drawing *dr,
2407                           game_state *state, game_drawstate *ds,
2408                           struct island *is, grid_type v)
2409 {
2410     /* These overlap the edges of their squares, which is why they're drawn later.
2411      * We know they can't overlap each other because they're not allowed within 2
2412      * squares of each other. */
2413     int half = TILE_SIZE/2;
2414     int ox = COORD(is->x) + half, oy = COORD(is->y) + half;
2415     int orad = ISLAND_RADIUS, irad = orad - LINE_WIDTH;
2416     int updatesz = orad*2+1;
2417     int tcol = (v & G_FLASH) ? COL_HIGHLIGHT :
2418               (v & G_WARN)  ? COL_WARNING : COL_FOREGROUND;
2419     int col = (v & G_ISSEL) ? COL_SELECTED : tcol;
2420     int bg = (v & G_MARK) ? COL_MARK : COL_BACKGROUND;
2421     char str[10];
2422
2423 #ifdef DRAW_GRID
2424     draw_rect_outline(dr, COORD(is->x), COORD(is->y),
2425                       TILE_SIZE, TILE_SIZE, COL_GRID);
2426 #endif
2427
2428     /* draw a thick circle */
2429     draw_circle(dr, ox, oy, orad, col, col);
2430     draw_circle(dr, ox, oy, irad, bg, bg);
2431
2432     sprintf(str, "%d", is->count);
2433     draw_text(dr, ox, oy, FONT_VARIABLE, ISLAND_NUMSIZE(is),
2434               ALIGN_VCENTRE | ALIGN_HCENTRE, tcol, str);
2435
2436     dsf_debug_draw(dr, state, ds, is->x, is->y);
2437     draw_update(dr, ox - orad, oy - orad, updatesz, updatesz);
2438 }
2439
2440 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
2441                         game_state *state, int dir, game_ui *ui,
2442                         float animtime, float flashtime)
2443 {
2444     int x, y, force = 0, i, j, redraw, lv, lh;
2445     grid_type v, dsv, flash = 0;
2446     struct island *is, *is_drag_src = NULL, *is_drag_dst = NULL;
2447
2448     if (flashtime) {
2449         int f = (int)(flashtime * 5 / FLASH_TIME);
2450         if (f == 1 || f == 3) flash = G_FLASH;
2451     }
2452
2453     /* Clear screen, if required. */
2454     if (!ds->started) {
2455         draw_rect(dr, 0, 0,
2456                   TILE_SIZE * ds->w + 2 * BORDER,
2457                   TILE_SIZE * ds->h + 2 * BORDER, COL_BACKGROUND);
2458 #ifdef DRAW_GRID
2459         draw_rect_outline(dr,
2460                           COORD(0)-1, COORD(0)-1,
2461                           TILE_SIZE * ds->w + 2, TILE_SIZE * ds->h + 2,
2462                           COL_GRID);
2463 #endif
2464         draw_update(dr, 0, 0,
2465                     TILE_SIZE * ds->w + 2 * BORDER,
2466                     TILE_SIZE * ds->h + 2 * BORDER);
2467         ds->started = 1;
2468         force = 1;
2469     }
2470
2471     if (ui->dragx_src != -1 && ui->dragy_src != -1) {
2472         ds->dragging = 1;
2473         is_drag_src = INDEX(state, gridi, ui->dragx_src, ui->dragy_src);
2474         assert(is_drag_src);
2475         if (ui->dragx_dst != -1 && ui->dragy_dst != -1) {
2476             is_drag_dst = INDEX(state, gridi, ui->dragx_dst, ui->dragy_dst);
2477             assert(is_drag_dst);
2478         }
2479     } else
2480         ds->dragging = 0;
2481
2482     /* Draw all lines (and hints, if we want), but *not* islands. */
2483     for (x = 0; x < ds->w; x++) {
2484         for (y = 0; y < ds->h; y++) {
2485             v = GRID(state, x, y) | flash;
2486             dsv = GRID(ds,x,y) & ~G_REDRAW;
2487
2488             if (v & G_ISLAND) continue;
2489
2490             if (is_drag_dst) {
2491                 if (WITHIN(x,is_drag_src->x, is_drag_dst->x) &&
2492                     WITHIN(y,is_drag_src->y, is_drag_dst->y))
2493                     v |= G_ISSEL;
2494             }
2495             lines_lvlh(state, x, y, v, &lv, &lh);
2496
2497             if (v != dsv ||
2498                 lv != INDEX(ds,lv,x,y) ||
2499                 lh != INDEX(ds,lh,x,y) ||
2500                 force) {
2501                 GRID(ds, x, y) = v | G_REDRAW;
2502                 INDEX(ds,lv,x,y) = lv;
2503                 INDEX(ds,lh,x,y) = lh;
2504                 lines_redraw(dr, state, ds, ui, x, y, v, lv, lh);
2505             } else
2506                 GRID(ds,x,y) &= ~G_REDRAW;
2507         }
2508     }
2509
2510     /* Draw islands. */
2511     for (i = 0; i < state->n_islands; i++) {
2512         is = &state->islands[i];
2513         v = GRID(state, is->x, is->y) | flash;
2514
2515         redraw = 0;
2516         for (j = 0; j < is->adj.npoints; j++) {
2517             if (GRID(ds,is->adj.points[j].x,is->adj.points[j].y) & G_REDRAW) {
2518                 redraw = 1;
2519             }
2520         }
2521
2522         if (is_drag_src) {
2523             if (is == is_drag_src)
2524                 v |= G_ISSEL;
2525             else if (is_drag_dst && is == is_drag_dst)
2526                 v |= G_ISSEL;
2527         }
2528
2529         if (island_impossible(is, v & G_MARK)) v |= G_WARN;
2530
2531         if ((v != GRID(ds, is->x, is->y)) || force || redraw) {
2532             GRID(ds,is->x,is->y) = v;
2533             island_redraw(dr, state, ds, is, v);
2534         }
2535     }
2536 }
2537
2538 static float game_anim_length(game_state *oldstate, game_state *newstate,
2539                               int dir, game_ui *ui)
2540 {
2541     return 0.0F;
2542 }
2543
2544 static float game_flash_length(game_state *oldstate, game_state *newstate,
2545                                int dir, game_ui *ui)
2546 {
2547     if (!oldstate->completed && newstate->completed &&
2548         !oldstate->solved && !newstate->solved)
2549         return FLASH_TIME;
2550
2551     return 0.0F;
2552 }
2553
2554 static int game_timing_state(game_state *state, game_ui *ui)
2555 {
2556     return TRUE;
2557 }
2558
2559 static void game_print_size(game_params *params, float *x, float *y)
2560 {
2561     int pw, ph;
2562
2563     /* 10mm squares by default. */
2564     game_compute_size(params, 1000, &pw, &ph);
2565     *x = pw / 100.0;
2566     *y = ph / 100.0;
2567 }
2568
2569 static void game_print(drawing *dr, game_state *state, int ts)
2570 {
2571     int ink = print_mono_colour(dr, 0);
2572     int paper = print_mono_colour(dr, 1);
2573     int x, y, cx, cy, i, nl;
2574     int loff = ts/8;
2575     grid_type grid;
2576
2577     /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2578     game_drawstate ads, *ds = &ads;
2579     ads.tilesize = ts;
2580
2581     /* I don't think this wants a border. */
2582
2583     /* Bridges */
2584     print_line_width(dr, ts / 12);
2585     for (x = 0; x < state->w; x++) {
2586         for (y = 0; y < state->h; y++) {
2587             cx = COORD(x); cy = COORD(y);
2588             grid = GRID(state,x,y);
2589             nl = INDEX(state,lines,x,y);
2590
2591             if (grid & G_ISLAND) continue;
2592             if (grid & G_LINEV) {
2593                 if (nl > 1) {
2594                     draw_line(dr, cx+ts/2-loff, cy, cx+ts/2-loff, cy+ts, ink);
2595                     draw_line(dr, cx+ts/2+loff, cy, cx+ts/2+loff, cy+ts, ink);
2596                 } else {
2597                     draw_line(dr, cx+ts/2,      cy, cx+ts/2,      cy+ts, ink);
2598                 }
2599             }
2600             if (grid & G_LINEH) {
2601                 if (nl > 1) {
2602                     draw_line(dr, cx, cy+ts/2-loff, cx+ts, cy+ts/2-loff, ink);
2603                     draw_line(dr, cx, cy+ts/2+loff, cx+ts, cy+ts/2+loff, ink);
2604                 } else {
2605                     draw_line(dr, cx, cy+ts/2,      cx+ts,      cy+ts/2, ink);
2606                 }
2607             }
2608         }
2609     }
2610
2611     /* Islands */
2612     for (i = 0; i < state->n_islands; i++) {
2613         char str[10];
2614         struct island *is = &state->islands[i];
2615         grid = GRID(state, is->x, is->y);
2616         cx = COORD(is->x) + ts/2;
2617         cy = COORD(is->y) + ts/2;
2618
2619         draw_circle(dr, cx, cy, ISLAND_RADIUS, paper, ink);
2620
2621         sprintf(str, "%d", is->count);
2622         draw_text(dr, cx, cy, FONT_VARIABLE, ISLAND_NUMSIZE(is),
2623                   ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str);
2624     }
2625 }
2626
2627 #ifdef COMBINED
2628 #define thegame bridges
2629 #endif
2630
2631 const struct game thegame = {
2632     "Bridges", "games.bridges", "bridges",
2633     default_params,
2634     game_fetch_preset,
2635     decode_params,
2636     encode_params,
2637     free_params,
2638     dup_params,
2639     TRUE, game_configure, custom_params,
2640     validate_params,
2641     new_game_desc,
2642     validate_desc,
2643     new_game,
2644     dup_game,
2645     free_game,
2646     TRUE, solve_game,
2647     TRUE, game_text_format,
2648     new_ui,
2649     free_ui,
2650     encode_ui,
2651     decode_ui,
2652     game_changed_state,
2653     interpret_move,
2654     execute_move,
2655     PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
2656     game_colours,
2657     game_new_drawstate,
2658     game_free_drawstate,
2659     game_redraw,
2660     game_anim_length,
2661     game_flash_length,
2662     TRUE, FALSE, game_print_size, game_print,
2663     FALSE,                             /* wants_statusbar */
2664     FALSE, game_timing_state,
2665     REQUIRE_RBUTTON,                   /* flags */
2666 };
2667
2668 /* vim: set shiftwidth=4 tabstop=8: */