chiark / gitweb /
Add 'const' to the game_params arguments in validate_desc and
[sgt-puzzles.git] / lightup.c
1 /*
2  * lightup.c: Implementation of the Nikoli game 'Light Up'.
3  *
4  * Possible future solver enhancements:
5  *
6  *  - In a situation where two clues are diagonally adjacent, you can
7  *    deduce bounds on the number of lights shared between them. For
8  *    instance, suppose a 3 clue is diagonally adjacent to a 1 clue:
9  *    of the two squares adjacent to both clues, at least one must be
10  *    a light (or the 3 would be unsatisfiable) and yet at most one
11  *    must be a light (or the 1 would be overcommitted), so in fact
12  *    _exactly_ one must be a light, and hence the other two squares
13  *    adjacent to the 3 must also be lights and the other two adjacent
14  *    to the 1 must not. Likewise if the 3 is replaced with a 2 but
15  *    one of its other two squares is known not to be a light, and so
16  *    on.
17  *
18  *  - In a situation where two clues are orthogonally separated (not
19  *    necessarily directly adjacent), you may be able to deduce
20  *    something about the squares that align with each other. For
21  *    instance, suppose two clues are vertically adjacent. Consider
22  *    the pair of squares A,B horizontally adjacent to the top clue,
23  *    and the pair C,D horizontally adjacent to the bottom clue.
24  *    Assuming no intervening obstacles, A and C align with each other
25  *    and hence at most one of them can be a light, and B and D
26  *    likewise, so we must have at most two lights between the four
27  *    squares. So if the clues indicate that there are at _least_ two
28  *    lights in those four squares because the top clue requires at
29  *    least one of AB to be a light and the bottom one requires at
30  *    least one of CD, then we can in fact deduce that there are
31  *    _exactly_ two lights between the four squares, and fill in the
32  *    other squares adjacent to each clue accordingly. For instance,
33  *    if both clues are 3s, then we instantly deduce that all four of
34  *    the squares _vertically_ adjacent to the two clues must be
35  *    lights. (For that to happen, of course, there'd also have to be
36  *    a black square in between the clues, so the two inner lights
37  *    don't light each other.)
38  *
39  *  - I haven't thought it through carefully, but there's always the
40  *    possibility that both of the above deductions are special cases
41  *    of some more general pattern which can be made computationally
42  *    feasible...
43  */
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <assert.h>
49 #include <ctype.h>
50 #include <math.h>
51
52 #include "puzzles.h"
53
54 /*
55  * In standalone solver mode, `verbose' is a variable which can be
56  * set by command-line option; in debugging mode it's simply always
57  * true.
58  */
59 #if defined STANDALONE_SOLVER
60 #define SOLVER_DIAGNOSTICS
61 int verbose = 0;
62 #undef debug
63 #define debug(x) printf x
64 #elif defined SOLVER_DIAGNOSTICS
65 #define verbose 2
66 #endif
67
68 /* --- Constants, structure definitions, etc. --- */
69
70 #define PREFERRED_TILE_SIZE 32
71 #define TILE_SIZE       (ds->tilesize)
72 #define BORDER          (TILE_SIZE / 2)
73 #define TILE_RADIUS     (ds->crad)
74
75 #define COORD(x)  ( (x) * TILE_SIZE + BORDER )
76 #define FROMCOORD(x)  ( ((x) - BORDER + TILE_SIZE) / TILE_SIZE - 1 )
77
78 #define FLASH_TIME 0.30F
79
80 enum {
81     COL_BACKGROUND,
82     COL_GRID,
83     COL_BLACK,                         /* black */
84     COL_LIGHT,                         /* white */
85     COL_LIT,                           /* yellow */
86     COL_ERROR,                         /* red */
87     COL_CURSOR,
88     NCOLOURS
89 };
90
91 enum { SYMM_NONE, SYMM_REF2, SYMM_ROT2, SYMM_REF4, SYMM_ROT4, SYMM_MAX };
92
93 #define DIFFCOUNT 2
94
95 struct game_params {
96     int w, h;
97     int blackpc;        /* %age of black squares */
98     int symm;
99     int difficulty;     /* 0 to DIFFCOUNT */
100 };
101
102 #define F_BLACK         1
103
104 /* flags for black squares */
105 #define F_NUMBERED      2       /* it has a number attached */
106 #define F_NUMBERUSED    4       /* this number was useful for solving */
107
108 /* flags for non-black squares */
109 #define F_IMPOSSIBLE    8       /* can't put a light here */
110 #define F_LIGHT         16
111
112 #define F_MARK          32
113
114 struct game_state {
115     int w, h, nlights;
116     int *lights;        /* For black squares, (optionally) the number
117                            of surrounding lights. For non-black squares,
118                            the number of times it's lit. size h*w*/
119     unsigned int *flags;        /* size h*w */
120     int completed, used_solve;
121 };
122
123 #define GRID(gs,grid,x,y) (gs->grid[(y)*((gs)->w) + (x)])
124
125 /* A ll_data holds information about which lights would be lit by
126  * a particular grid location's light (or conversely, which locations
127  * could light a specific other location). */
128 /* most things should consider this struct opaque. */
129 typedef struct {
130     int ox,oy;
131     int minx, maxx, miny, maxy;
132     int include_origin;
133 } ll_data;
134
135 /* Macro that executes 'block' once per light in lld, including
136  * the origin if include_origin is specified. 'block' can use
137  * lx and ly as the coords. */
138 #define FOREACHLIT(lld,block) do {                              \
139   int lx,ly;                                                    \
140   ly = (lld)->oy;                                               \
141   for (lx = (lld)->minx; lx <= (lld)->maxx; lx++) {             \
142     if (lx == (lld)->ox) continue;                              \
143     block                                                       \
144   }                                                             \
145   lx = (lld)->ox;                                               \
146   for (ly = (lld)->miny; ly <= (lld)->maxy; ly++) {             \
147     if (!(lld)->include_origin && ly == (lld)->oy) continue;    \
148     block                                                       \
149   }                                                             \
150 } while(0)
151
152
153 typedef struct {
154     struct { int x, y; unsigned int f; } points[4];
155     int npoints;
156 } surrounds;
157
158 /* Fills in (doesn't allocate) a surrounds structure with the grid locations
159  * around a given square, taking account of the edges. */
160 static void get_surrounds(game_state *state, int ox, int oy, surrounds *s)
161 {
162     assert(ox >= 0 && ox < state->w && oy >= 0 && oy < state->h);
163     s->npoints = 0;
164 #define ADDPOINT(cond,nx,ny) do {\
165     if (cond) { \
166         s->points[s->npoints].x = (nx); \
167         s->points[s->npoints].y = (ny); \
168         s->points[s->npoints].f = 0; \
169         s->npoints++; \
170     } } while(0)
171     ADDPOINT(ox > 0,            ox-1, oy);
172     ADDPOINT(ox < (state->w-1), ox+1, oy);
173     ADDPOINT(oy > 0,            ox,   oy-1);
174     ADDPOINT(oy < (state->h-1), ox,   oy+1);
175 }
176
177 /* --- Game parameter functions --- */
178
179 #define DEFAULT_PRESET 0
180
181 const struct game_params lightup_presets[] = {
182     { 7, 7, 20, SYMM_ROT4, 0 },
183     { 7, 7, 20, SYMM_ROT4, 1 },
184     { 7, 7, 20, SYMM_ROT4, 2 },
185     { 10, 10, 20, SYMM_ROT2, 0 },
186     { 10, 10, 20, SYMM_ROT2, 1 },
187 #ifdef SLOW_SYSTEM
188     { 12, 12, 20, SYMM_ROT2, 0 },
189     { 12, 12, 20, SYMM_ROT2, 1 },
190 #else
191     { 10, 10, 20, SYMM_ROT2, 2 },
192     { 14, 14, 20, SYMM_ROT2, 0 },
193     { 14, 14, 20, SYMM_ROT2, 1 },
194     { 14, 14, 20, SYMM_ROT2, 2 }
195 #endif
196 };
197
198 static game_params *default_params(void)
199 {
200     game_params *ret = snew(game_params);
201     *ret = lightup_presets[DEFAULT_PRESET];
202
203     return ret;
204 }
205
206 static int game_fetch_preset(int i, char **name, game_params **params)
207 {
208     game_params *ret;
209     char buf[80];
210
211     if (i < 0 || i >= lenof(lightup_presets))
212         return FALSE;
213
214     ret = default_params();
215     *ret = lightup_presets[i];
216     *params = ret;
217
218     sprintf(buf, "%dx%d %s",
219             ret->w, ret->h,
220             ret->difficulty == 2 ? "hard" :
221             ret->difficulty == 1 ? "tricky" : "easy");
222     *name = dupstr(buf);
223
224     return TRUE;
225 }
226
227 static void free_params(game_params *params)
228 {
229     sfree(params);
230 }
231
232 static game_params *dup_params(game_params *params)
233 {
234     game_params *ret = snew(game_params);
235     *ret = *params;                    /* structure copy */
236     return ret;
237 }
238
239 #define EATNUM(x) do { \
240     (x) = atoi(string); \
241     while (*string && isdigit((unsigned char)*string)) string++; \
242 } while(0)
243
244 static void decode_params(game_params *params, char const *string)
245 {
246     EATNUM(params->w);
247     if (*string == 'x') {
248         string++;
249         EATNUM(params->h);
250     }
251     if (*string == 'b') {
252         string++;
253         EATNUM(params->blackpc);
254     }
255     if (*string == 's') {
256         string++;
257         EATNUM(params->symm);
258     } else {
259         /* cope with user input such as '18x10' by ensuring symmetry
260          * is not selected by default to be incompatible with dimensions */
261         if (params->symm == SYMM_ROT4 && params->w != params->h)
262             params->symm = SYMM_ROT2;
263     }
264     params->difficulty = 0;
265     /* cope with old params */
266     if (*string == 'r') {
267         params->difficulty = 2;
268         string++;
269     }
270     if (*string == 'd') {
271         string++;
272         EATNUM(params->difficulty);
273     }
274 }
275
276 static char *encode_params(game_params *params, int full)
277 {
278     char buf[80];
279
280     if (full) {
281         sprintf(buf, "%dx%db%ds%dd%d",
282                 params->w, params->h, params->blackpc,
283                 params->symm,
284                 params->difficulty);
285     } else {
286         sprintf(buf, "%dx%d", params->w, params->h);
287     }
288     return dupstr(buf);
289 }
290
291 static config_item *game_configure(game_params *params)
292 {
293     config_item *ret;
294     char buf[80];
295
296     ret = snewn(6, config_item);
297
298     ret[0].name = "Width";
299     ret[0].type = C_STRING;
300     sprintf(buf, "%d", params->w);
301     ret[0].sval = dupstr(buf);
302     ret[0].ival = 0;
303
304     ret[1].name = "Height";
305     ret[1].type = C_STRING;
306     sprintf(buf, "%d", params->h);
307     ret[1].sval = dupstr(buf);
308     ret[1].ival = 0;
309
310     ret[2].name = "%age of black squares";
311     ret[2].type = C_STRING;
312     sprintf(buf, "%d", params->blackpc);
313     ret[2].sval = dupstr(buf);
314     ret[2].ival = 0;
315
316     ret[3].name = "Symmetry";
317     ret[3].type = C_CHOICES;
318     ret[3].sval = ":None"
319                   ":2-way mirror:2-way rotational"
320                   ":4-way mirror:4-way rotational";
321     ret[3].ival = params->symm;
322
323     ret[4].name = "Difficulty";
324     ret[4].type = C_CHOICES;
325     ret[4].sval = ":Easy:Tricky:Hard";
326     ret[4].ival = params->difficulty;
327
328     ret[5].name = NULL;
329     ret[5].type = C_END;
330     ret[5].sval = NULL;
331     ret[5].ival = 0;
332
333     return ret;
334 }
335
336 static game_params *custom_params(config_item *cfg)
337 {
338     game_params *ret = snew(game_params);
339
340     ret->w =       atoi(cfg[0].sval);
341     ret->h =       atoi(cfg[1].sval);
342     ret->blackpc = atoi(cfg[2].sval);
343     ret->symm =    cfg[3].ival;
344     ret->difficulty = cfg[4].ival;
345
346     return ret;
347 }
348
349 static char *validate_params(game_params *params, int full)
350 {
351     if (params->w < 2 || params->h < 2)
352         return "Width and height must be at least 2";
353     if (full) {
354         if (params->blackpc < 5 || params->blackpc > 100)
355             return "Percentage of black squares must be between 5% and 100%";
356         if (params->w != params->h) {
357             if (params->symm == SYMM_ROT4)
358                 return "4-fold symmetry is only available with square grids";
359         }
360         if (params->symm < 0 || params->symm >= SYMM_MAX)
361             return "Unknown symmetry type";
362         if (params->difficulty < 0 || params->difficulty > DIFFCOUNT)
363             return "Unknown difficulty level";
364     }
365     return NULL;
366 }
367
368 /* --- Game state construction/freeing helper functions --- */
369
370 static game_state *new_state(const game_params *params)
371 {
372     game_state *ret = snew(game_state);
373
374     ret->w = params->w;
375     ret->h = params->h;
376     ret->lights = snewn(ret->w * ret->h, int);
377     ret->nlights = 0;
378     memset(ret->lights, 0, ret->w * ret->h * sizeof(int));
379     ret->flags = snewn(ret->w * ret->h, unsigned int);
380     memset(ret->flags, 0, ret->w * ret->h * sizeof(unsigned int));
381     ret->completed = ret->used_solve = 0;
382     return ret;
383 }
384
385 static game_state *dup_game(game_state *state)
386 {
387     game_state *ret = snew(game_state);
388
389     ret->w = state->w;
390     ret->h = state->h;
391
392     ret->lights = snewn(ret->w * ret->h, int);
393     memcpy(ret->lights, state->lights, ret->w * ret->h * sizeof(int));
394     ret->nlights = state->nlights;
395
396     ret->flags = snewn(ret->w * ret->h, unsigned int);
397     memcpy(ret->flags, state->flags, ret->w * ret->h * sizeof(unsigned int));
398
399     ret->completed = state->completed;
400     ret->used_solve = state->used_solve;
401
402     return ret;
403 }
404
405 static void free_game(game_state *state)
406 {
407     sfree(state->lights);
408     sfree(state->flags);
409     sfree(state);
410 }
411
412 static void debug_state(game_state *state)
413 {
414     int x, y;
415     char c = '?';
416
417     for (y = 0; y < state->h; y++) {
418         for (x = 0; x < state->w; x++) {
419             c = '.';
420             if (GRID(state, flags, x, y) & F_BLACK) {
421                 if (GRID(state, flags, x, y) & F_NUMBERED)
422                     c = GRID(state, lights, x, y) + '0';
423                 else
424                     c = '#';
425             } else {
426                 if (GRID(state, flags, x, y) & F_LIGHT)
427                     c = 'O';
428                 else if (GRID(state, flags, x, y) & F_IMPOSSIBLE)
429                     c = 'X';
430             }
431             debug(("%c", (int)c));
432         }
433         debug(("     "));
434         for (x = 0; x < state->w; x++) {
435             if (GRID(state, flags, x, y) & F_BLACK)
436                 c = '#';
437             else {
438                 c = (GRID(state, flags, x, y) & F_LIGHT) ? 'A' : 'a';
439                 c += GRID(state, lights, x, y);
440             }
441             debug(("%c", (int)c));
442         }
443         debug(("\n"));
444     }
445 }
446
447 /* --- Game completion test routines. --- */
448
449 /* These are split up because occasionally functions are only
450  * interested in one particular aspect. */
451
452 /* Returns non-zero if all grid spaces are lit. */
453 static int grid_lit(game_state *state)
454 {
455     int x, y;
456
457     for (x = 0; x < state->w; x++) {
458         for (y = 0; y < state->h; y++) {
459             if (GRID(state,flags,x,y) & F_BLACK) continue;
460             if (GRID(state,lights,x,y) == 0)
461                 return 0;
462         }
463     }
464     return 1;
465 }
466
467 /* Returns non-zero if any lights are lit by other lights. */
468 static int grid_overlap(game_state *state)
469 {
470     int x, y;
471
472     for (x = 0; x < state->w; x++) {
473         for (y = 0; y < state->h; y++) {
474             if (!(GRID(state, flags, x, y) & F_LIGHT)) continue;
475             if (GRID(state, lights, x, y) > 1)
476                 return 1;
477         }
478     }
479     return 0;
480 }
481
482 static int number_wrong(game_state *state, int x, int y)
483 {
484     surrounds s;
485     int i, n, empty, lights = GRID(state, lights, x, y);
486
487     /*
488      * This function computes the display hint for a number: we
489      * turn the number red if it is definitely wrong. This means
490      * that either
491      * 
492      *  (a) it has too many lights around it, or
493      *  (b) it would have too few lights around it even if all the
494      *      plausible squares (not black, lit or F_IMPOSSIBLE) were
495      *      filled with lights.
496      */
497
498     assert(GRID(state, flags, x, y) & F_NUMBERED);
499     get_surrounds(state, x, y, &s);
500
501     empty = n = 0;
502     for (i = 0; i < s.npoints; i++) {
503         if (GRID(state,flags,s.points[i].x,s.points[i].y) & F_LIGHT) {
504             n++;
505             continue;
506         }
507         if (GRID(state,flags,s.points[i].x,s.points[i].y) & F_BLACK)
508             continue;
509         if (GRID(state,flags,s.points[i].x,s.points[i].y) & F_IMPOSSIBLE)
510             continue;
511         if (GRID(state,lights,s.points[i].x,s.points[i].y))
512             continue;
513         empty++;
514     }
515     return (n > lights || (n + empty < lights));
516 }
517
518 static int number_correct(game_state *state, int x, int y)
519 {
520     surrounds s;
521     int n = 0, i, lights = GRID(state, lights, x, y);
522
523     assert(GRID(state, flags, x, y) & F_NUMBERED);
524     get_surrounds(state, x, y, &s);
525     for (i = 0; i < s.npoints; i++) {
526         if (GRID(state,flags,s.points[i].x,s.points[i].y) & F_LIGHT)
527             n++;
528     }
529     return (n == lights) ? 1 : 0;
530 }
531
532 /* Returns non-zero if any numbers add up incorrectly. */
533 static int grid_addsup(game_state *state)
534 {
535     int x, y;
536
537     for (x = 0; x < state->w; x++) {
538         for (y = 0; y < state->h; y++) {
539             if (!(GRID(state, flags, x, y) & F_NUMBERED)) continue;
540             if (!number_correct(state, x, y)) return 0;
541         }
542     }
543     return 1;
544 }
545
546 static int grid_correct(game_state *state)
547 {
548     if (grid_lit(state) &&
549         !grid_overlap(state) &&
550         grid_addsup(state)) return 1;
551     return 0;
552 }
553
554 /* --- Board initial setup (blacks, lights, numbers) --- */
555
556 static void clean_board(game_state *state, int leave_blacks)
557 {
558     int x,y;
559     for (x = 0; x < state->w; x++) {
560         for (y = 0; y < state->h; y++) {
561             if (leave_blacks)
562                 GRID(state, flags, x, y) &= F_BLACK;
563             else
564                 GRID(state, flags, x, y) = 0;
565             GRID(state, lights, x, y) = 0;
566         }
567     }
568     state->nlights = 0;
569 }
570
571 static void set_blacks(game_state *state, const game_params *params,
572                        random_state *rs)
573 {
574     int x, y, degree = 0, rotate = 0, nblack;
575     int rh, rw, i;
576     int wodd = (state->w % 2) ? 1 : 0;
577     int hodd = (state->h % 2) ? 1 : 0;
578     int xs[4], ys[4];
579
580     switch (params->symm) {
581     case SYMM_NONE: degree = 1; rotate = 0; break;
582     case SYMM_ROT2: degree = 2; rotate = 1; break;
583     case SYMM_REF2: degree = 2; rotate = 0; break;
584     case SYMM_ROT4: degree = 4; rotate = 1; break;
585     case SYMM_REF4: degree = 4; rotate = 0; break;
586     default: assert(!"Unknown symmetry type");
587     }
588     if (params->symm == SYMM_ROT4 && (state->h != state->w))
589         assert(!"4-fold symmetry unavailable without square grid");
590
591     if (degree == 4) {
592         rw = state->w/2;
593         rh = state->h/2;
594         if (!rotate) rw += wodd; /* ... but see below. */
595         rh += hodd;
596     } else if (degree == 2) {
597         rw = state->w;
598         rh = state->h/2;
599         rh += hodd;
600     } else {
601         rw = state->w;
602         rh = state->h;
603     }
604
605     /* clear, then randomise, required region. */
606     clean_board(state, 0);
607     nblack = (rw * rh * params->blackpc) / 100;
608     for (i = 0; i < nblack; i++) {
609         do {
610             x = random_upto(rs,rw);
611             y = random_upto(rs,rh);
612         } while (GRID(state,flags,x,y) & F_BLACK);
613         GRID(state, flags, x, y) |= F_BLACK;
614     }
615
616     /* Copy required region. */
617     if (params->symm == SYMM_NONE) return;
618
619     for (x = 0; x < rw; x++) {
620         for (y = 0; y < rh; y++) {
621             if (degree == 4) {
622                 xs[0] = x;
623                 ys[0] = y;
624                 xs[1] = state->w - 1 - (rotate ? y : x);
625                 ys[1] = rotate ? x : y;
626                 xs[2] = rotate ? (state->w - 1 - x) : x;
627                 ys[2] = state->h - 1 - y;
628                 xs[3] = rotate ? y : (state->w - 1 - x);
629                 ys[3] = state->h - 1 - (rotate ? x : y);
630             } else {
631                 xs[0] = x;
632                 ys[0] = y;
633                 xs[1] = rotate ? (state->w - 1 - x) : x;
634                 ys[1] = state->h - 1 - y;
635             }
636             for (i = 1; i < degree; i++) {
637                 GRID(state, flags, xs[i], ys[i]) =
638                     GRID(state, flags, xs[0], ys[0]);
639             }
640         }
641     }
642     /* SYMM_ROT4 misses the middle square above; fix that here. */
643     if (degree == 4 && rotate && wodd &&
644         (random_upto(rs,100) <= (unsigned int)params->blackpc))
645         GRID(state,flags,
646              state->w/2 + wodd - 1, state->h/2 + hodd - 1) |= F_BLACK;
647
648 #ifdef SOLVER_DIAGNOSTICS
649     if (verbose) debug_state(state);
650 #endif
651 }
652
653 /* Fills in (does not allocate) a ll_data with all the tiles that would
654  * be illuminated by a light at point (ox,oy). If origin=1 then the
655  * origin is included in this list. */
656 static void list_lights(game_state *state, int ox, int oy, int origin,
657                         ll_data *lld)
658 {
659     int x,y;
660
661     lld->ox = lld->minx = lld->maxx = ox;
662     lld->oy = lld->miny = lld->maxy = oy;
663     lld->include_origin = origin;
664
665     y = oy;
666     for (x = ox-1; x >= 0; x--) {
667         if (GRID(state, flags, x, y) & F_BLACK) break;
668         if (x < lld->minx) lld->minx = x;
669     }
670     for (x = ox+1; x < state->w; x++) {
671         if (GRID(state, flags, x, y) & F_BLACK) break;
672         if (x > lld->maxx) lld->maxx = x;
673     }
674
675     x = ox;
676     for (y = oy-1; y >= 0; y--) {
677         if (GRID(state, flags, x, y) & F_BLACK) break;
678         if (y < lld->miny) lld->miny = y;
679     }
680     for (y = oy+1; y < state->h; y++) {
681         if (GRID(state, flags, x, y) & F_BLACK) break;
682         if (y > lld->maxy) lld->maxy = y;
683     }
684 }
685
686 /* Makes sure a light is the given state, editing the lights table to suit the
687  * new state if necessary. */
688 static void set_light(game_state *state, int ox, int oy, int on)
689 {
690     ll_data lld;
691     int diff = 0;
692
693     assert(!(GRID(state,flags,ox,oy) & F_BLACK));
694
695     if (!on && GRID(state,flags,ox,oy) & F_LIGHT) {
696         diff = -1;
697         GRID(state,flags,ox,oy) &= ~F_LIGHT;
698         state->nlights--;
699     } else if (on && !(GRID(state,flags,ox,oy) & F_LIGHT)) {
700         diff = 1;
701         GRID(state,flags,ox,oy) |= F_LIGHT;
702         state->nlights++;
703     }
704
705     if (diff != 0) {
706         list_lights(state,ox,oy,1,&lld);
707         FOREACHLIT(&lld, GRID(state,lights,lx,ly) += diff; );
708     }
709 }
710
711 /* Returns 1 if removing a light at (x,y) would cause a square to go dark. */
712 static int check_dark(game_state *state, int x, int y)
713 {
714     ll_data lld;
715
716     list_lights(state, x, y, 1, &lld);
717     FOREACHLIT(&lld, if (GRID(state,lights,lx,ly) == 1) { return 1; } );
718     return 0;
719 }
720
721 /* Sets up an initial random correct position (i.e. every
722  * space lit, and no lights lit by other lights) by filling the
723  * grid with lights and then removing lights one by one at random. */
724 static void place_lights(game_state *state, random_state *rs)
725 {
726     int i, x, y, n, *numindices, wh = state->w*state->h;
727     ll_data lld;
728
729     numindices = snewn(wh, int);
730     for (i = 0; i < wh; i++) numindices[i] = i;
731     shuffle(numindices, wh, sizeof(*numindices), rs);
732
733     /* Place a light on all grid squares without lights. */
734     for (x = 0; x < state->w; x++) {
735         for (y = 0; y < state->h; y++) {
736             GRID(state, flags, x, y) &= ~F_MARK; /* we use this later. */
737             if (GRID(state, flags, x, y) & F_BLACK) continue;
738             set_light(state, x, y, 1);
739         }
740     }
741
742     for (i = 0; i < wh; i++) {
743         y = numindices[i] / state->w;
744         x = numindices[i] % state->w;
745         if (!(GRID(state, flags, x, y) & F_LIGHT)) continue;
746         if (GRID(state, flags, x, y) & F_MARK) continue;
747         list_lights(state, x, y, 0, &lld);
748
749         /* If we're not lighting any lights ourself, don't remove anything. */
750         n = 0;
751         FOREACHLIT(&lld, if (GRID(state,flags,lx,ly) & F_LIGHT) { n += 1; } );
752         if (n == 0) continue; /* [1] */
753
754         /* Check whether removing lights we're lighting would cause anything
755          * to go dark. */
756         n = 0;
757         FOREACHLIT(&lld, if (GRID(state,flags,lx,ly) & F_LIGHT) { n += check_dark(state,lx,ly); } );
758         if (n == 0) {
759             /* No, it wouldn't, so we can remove them all. */
760             FOREACHLIT(&lld, set_light(state,lx,ly, 0); );
761             GRID(state,flags,x,y) |= F_MARK;
762         }
763
764         if (!grid_overlap(state)) {
765             sfree(numindices);
766             return; /* we're done. */
767         }
768         assert(grid_lit(state));
769     }
770     /* could get here if the line at [1] continue'd out of the loop. */
771     if (grid_overlap(state)) {
772         debug_state(state);
773         assert(!"place_lights failed to resolve overlapping lights!");
774     }
775     sfree(numindices);
776 }
777
778 /* Fills in all black squares with numbers of adjacent lights. */
779 static void place_numbers(game_state *state)
780 {
781     int x, y, i, n;
782     surrounds s;
783
784     for (x = 0; x < state->w; x++) {
785         for (y = 0; y < state->h; y++) {
786             if (!(GRID(state,flags,x,y) & F_BLACK)) continue;
787             get_surrounds(state, x, y, &s);
788             n = 0;
789             for (i = 0; i < s.npoints; i++) {
790                 if (GRID(state,flags,s.points[i].x, s.points[i].y) & F_LIGHT)
791                     n++;
792             }
793             GRID(state,flags,x,y) |= F_NUMBERED;
794             GRID(state,lights,x,y) = n;
795         }
796     }
797 }
798
799 /* --- Actual solver, with helper subroutines. --- */
800
801 static void tsl_callback(game_state *state,
802                          int lx, int ly, int *x, int *y, int *n)
803 {
804     if (GRID(state,flags,lx,ly) & F_IMPOSSIBLE) return;
805     if (GRID(state,lights,lx,ly) > 0) return;
806     *x = lx; *y = ly; (*n)++;
807 }
808
809 static int try_solve_light(game_state *state, int ox, int oy,
810                            unsigned int flags, int lights)
811 {
812     ll_data lld;
813     int sx = 0, sy = 0, n = 0;
814
815     if (lights > 0) return 0;
816     if (flags & F_BLACK) return 0;
817
818     /* We have an unlit square; count how many ways there are left to
819      * place a light that lights us (including this square); if only
820      * one, we must put a light there. Squares that could light us
821      * are, of course, the same as the squares we would light... */
822     list_lights(state, ox, oy, 1, &lld);
823     FOREACHLIT(&lld, { tsl_callback(state, lx, ly, &sx, &sy, &n); });
824     if (n == 1) {
825         set_light(state, sx, sy, 1);
826 #ifdef SOLVER_DIAGNOSTICS
827         debug(("(%d,%d) can only be lit from (%d,%d); setting to LIGHT\n",
828                 ox,oy,sx,sy));
829         if (verbose) debug_state(state);
830 #endif
831         return 1;
832     }
833
834     return 0;
835 }
836
837 static int could_place_light(unsigned int flags, int lights)
838 {
839     if (flags & (F_BLACK | F_IMPOSSIBLE)) return 0;
840     return (lights > 0) ? 0 : 1;
841 }
842
843 static int could_place_light_xy(game_state *state, int x, int y)
844 {
845     int lights = GRID(state,lights,x,y);
846     unsigned int flags = GRID(state,flags,x,y);
847     return (could_place_light(flags, lights)) ? 1 : 0;
848 }
849
850 /* For a given number square, determine whether we have enough info
851  * to unambiguously place its lights. */
852 static int try_solve_number(game_state *state, int nx, int ny,
853                             unsigned int nflags, int nlights)
854 {
855     surrounds s;
856     int x, y, nl, ns, i, ret = 0, lights;
857     unsigned int flags;
858
859     if (!(nflags & F_NUMBERED)) return 0;
860     nl = nlights;
861     get_surrounds(state,nx,ny,&s);
862     ns = s.npoints;
863
864     /* nl is no. of lights we need to place, ns is no. of spaces we
865      * have to place them in. Try and narrow these down, and mark
866      * points we can ignore later. */
867     for (i = 0; i < s.npoints; i++) {
868         x = s.points[i].x; y = s.points[i].y;
869         flags = GRID(state,flags,x,y);
870         lights = GRID(state,lights,x,y);
871         if (flags & F_LIGHT) {
872             /* light here already; one less light for one less place. */
873             nl--; ns--;
874             s.points[i].f |= F_MARK;
875         } else if (!could_place_light(flags, lights)) {
876             ns--;
877             s.points[i].f |= F_MARK;
878         }
879     }
880     if (ns == 0) return 0; /* nowhere to put anything. */
881     if (nl == 0) {
882         /* we have placed all lights we need to around here; all remaining
883          * surrounds are therefore IMPOSSIBLE. */
884         GRID(state,flags,nx,ny) |= F_NUMBERUSED;
885         for (i = 0; i < s.npoints; i++) {
886             if (!(s.points[i].f & F_MARK)) {
887                 GRID(state,flags,s.points[i].x,s.points[i].y) |= F_IMPOSSIBLE;
888                 ret = 1;
889             }
890         }
891 #ifdef SOLVER_DIAGNOSTICS
892         printf("Clue at (%d,%d) full; setting unlit to IMPOSSIBLE.\n",
893                nx,ny);
894         if (verbose) debug_state(state);
895 #endif
896     } else if (nl == ns) {
897         /* we have as many lights to place as spaces; fill them all. */
898         GRID(state,flags,nx,ny) |= F_NUMBERUSED;
899         for (i = 0; i < s.npoints; i++) {
900             if (!(s.points[i].f & F_MARK)) {
901                 set_light(state, s.points[i].x,s.points[i].y, 1);
902                 ret = 1;
903             }
904         }
905 #ifdef SOLVER_DIAGNOSTICS
906         printf("Clue at (%d,%d) trivial; setting unlit to LIGHT.\n",
907                nx,ny);
908         if (verbose) debug_state(state);
909 #endif
910     }
911     return ret;
912 }
913
914 struct setscratch {
915     int x, y;
916     int n;
917 };
918
919 #define SCRATCHSZ (state->w+state->h)
920
921 /* New solver algorithm: overlapping sets can add IMPOSSIBLE flags.
922  * Algorithm thanks to Simon:
923  *
924  * (a) Any square where you can place a light has a set of squares
925  *     which would become non-lights as a result. (This includes
926  *     squares lit by the first square, and can also include squares
927  *     adjacent to the same clue square if the new light is the last
928  *     one around that clue.) Call this MAKESDARK(x,y) with (x,y) being
929  *     the square you place a light.
930
931  * (b) Any unlit square has a set of squares on which you could place
932  *     a light to illuminate it. (Possibly including itself, of
933  *     course.) This set of squares has the property that _at least
934  *     one_ of them must contain a light. Sets of this type also arise
935  *     from clue squares. Call this MAKESLIGHT(x,y), again with (x,y)
936  *     the square you would place a light.
937
938  * (c) If there exists (dx,dy) and (lx,ly) such that MAKESDARK(dx,dy) is
939  *     a superset of MAKESLIGHT(lx,ly), this implies that placing a light at
940  *     (dx,dy) would either leave no remaining way to illuminate a certain
941  *     square, or would leave no remaining way to fulfill a certain clue
942  *     (at lx,ly). In either case, a light can be ruled out at that position.
943  *
944  * So, we construct all possible MAKESLIGHT sets, both from unlit squares
945  * and clue squares, and then we look for plausible MAKESDARK sets that include
946  * our (lx,ly) to see if we can find a (dx,dy) to rule out. By the time we have
947  * constructed the MAKESLIGHT set we don't care about (lx,ly), just the set
948  * members.
949  *
950  * Once we have such a set, Simon came up with a Cunning Plan to find
951  * the most sensible MAKESDARK candidate:
952  *
953  * (a) for each square S in your set X, find all the squares which _would_
954  *     rule it out. That means any square which would light S, plus
955  *     any square adjacent to the same clue square as S (provided
956  *     that clue square has only one remaining light to be placed).
957  *     It's not hard to make this list. Don't do anything with this
958  *     data at the moment except _count_ the squares.
959
960  * (b) Find the square S_min in the original set which has the
961  *     _smallest_ number of other squares which would rule it out.
962
963  * (c) Find all the squares that rule out S_min (it's probably
964  *     better to recompute this than to have stored it during step
965  *     (a), since the CPU requirement is modest but the storage
966  *     cost would get ugly.) For each of these squares, see if it
967  *     rules out everything else in the set X. Any which does can
968  *     be marked as not-a-light.
969  *
970  */
971
972 typedef void (*trl_cb)(game_state *state, int dx, int dy,
973                        struct setscratch *scratch, int n, void *ctx);
974
975 static void try_rule_out(game_state *state, int x, int y,
976                          struct setscratch *scratch, int n,
977                          trl_cb cb, void *ctx);
978
979 static void trl_callback_search(game_state *state, int dx, int dy,
980                        struct setscratch *scratch, int n, void *ignored)
981 {
982     int i;
983
984 #ifdef SOLVER_DIAGNOSTICS
985     if (verbose) debug(("discount cb: light at (%d,%d)\n", dx, dy));
986 #endif
987
988     for (i = 0; i < n; i++) {
989         if (dx == scratch[i].x && dy == scratch[i].y) {
990             scratch[i].n = 1;
991             return;
992         }
993     }
994 }
995
996 static void trl_callback_discount(game_state *state, int dx, int dy,
997                        struct setscratch *scratch, int n, void *ctx)
998 {
999     int *didsth = (int *)ctx;
1000     int i;
1001
1002     if (GRID(state,flags,dx,dy) & F_IMPOSSIBLE) {
1003 #ifdef SOLVER_DIAGNOSTICS
1004         debug(("Square at (%d,%d) already impossible.\n", dx,dy));
1005 #endif
1006         return;
1007     }
1008
1009     /* Check whether a light at (dx,dy) rules out everything
1010      * in scratch, and mark (dx,dy) as IMPOSSIBLE if it does.
1011      * We can use try_rule_out for this as well, as the set of
1012      * squares which would rule out (x,y) is the same as the
1013      * set of squares which (x,y) would rule out. */
1014
1015 #ifdef SOLVER_DIAGNOSTICS
1016     if (verbose) debug(("Checking whether light at (%d,%d) rules out everything in scratch.\n", dx, dy));
1017 #endif
1018
1019     for (i = 0; i < n; i++)
1020         scratch[i].n = 0;
1021     try_rule_out(state, dx, dy, scratch, n, trl_callback_search, NULL);
1022     for (i = 0; i < n; i++) {
1023         if (scratch[i].n == 0) return;
1024     }
1025     /* The light ruled out everything in scratch. Yay. */
1026     GRID(state,flags,dx,dy) |= F_IMPOSSIBLE;
1027 #ifdef SOLVER_DIAGNOSTICS
1028     debug(("Set reduction discounted square at (%d,%d):\n", dx,dy));
1029     if (verbose) debug_state(state);
1030 #endif
1031
1032     *didsth = 1;
1033 }
1034
1035 static void trl_callback_incn(game_state *state, int dx, int dy,
1036                        struct setscratch *scratch, int n, void *ctx)
1037 {
1038     struct setscratch *s = (struct setscratch *)ctx;
1039     s->n++;
1040 }
1041
1042 static void try_rule_out(game_state *state, int x, int y,
1043                          struct setscratch *scratch, int n,
1044                          trl_cb cb, void *ctx)
1045 {
1046     /* XXX Find all the squares which would rule out (x,y); anything
1047      * that would light it as well as squares adjacent to same clues
1048      * as X assuming that clue only has one remaining light.
1049      * Call the callback with each square. */
1050     ll_data lld;
1051     surrounds s, ss;
1052     int i, j, curr_lights, tot_lights;
1053
1054     /* Find all squares that would rule out a light at (x,y) and call trl_cb
1055      * with them: anything that would light (x,y)... */
1056
1057     list_lights(state, x, y, 0, &lld);
1058     FOREACHLIT(&lld, { if (could_place_light_xy(state, lx, ly)) { cb(state, lx, ly, scratch, n, ctx); } });
1059
1060     /* ... as well as any empty space (that isn't x,y) next to any clue square
1061      * next to (x,y) that only has one light left to place. */
1062
1063     get_surrounds(state, x, y, &s);
1064     for (i = 0; i < s.npoints; i++) {
1065         if (!(GRID(state,flags,s.points[i].x,s.points[i].y) & F_NUMBERED))
1066             continue;
1067         /* we have an adjacent clue square; find /its/ surrounds
1068          * and count the remaining lights it needs. */
1069         get_surrounds(state,s.points[i].x,s.points[i].y,&ss);
1070         curr_lights = 0;
1071         for (j = 0; j < ss.npoints; j++) {
1072             if (GRID(state,flags,ss.points[j].x,ss.points[j].y) & F_LIGHT)
1073                 curr_lights++;
1074         }
1075         tot_lights = GRID(state, lights, s.points[i].x, s.points[i].y);
1076         /* We have a clue with tot_lights to fill, and curr_lights currently
1077          * around it. If adding a light at (x,y) fills up the clue (i.e.
1078          * curr_lights + 1 = tot_lights) then we need to discount all other
1079          * unlit squares around the clue. */
1080         if ((curr_lights + 1) == tot_lights) {
1081             for (j = 0; j < ss.npoints; j++) {
1082                 int lx = ss.points[j].x, ly = ss.points[j].y;
1083                 if (lx == x && ly == y) continue;
1084                 if (could_place_light_xy(state, lx, ly))
1085                     cb(state, lx, ly, scratch, n, ctx);
1086             }
1087         }
1088     }
1089 }
1090
1091 #ifdef SOLVER_DIAGNOSTICS
1092 static void debug_scratch(const char *msg, struct setscratch *scratch, int n)
1093 {
1094     int i;
1095     debug(("%s scratch (%d elements):\n", msg, n));
1096     for (i = 0; i < n; i++) {
1097         debug(("  (%d,%d) n%d\n", scratch[i].x, scratch[i].y, scratch[i].n));
1098     }
1099 }
1100 #endif
1101
1102 static int discount_set(game_state *state,
1103                         struct setscratch *scratch, int n)
1104 {
1105     int i, besti, bestn, didsth = 0;
1106
1107 #ifdef SOLVER_DIAGNOSTICS
1108     if (verbose > 1) debug_scratch("discount_set", scratch, n);
1109 #endif
1110     if (n == 0) return 0;
1111
1112     for (i = 0; i < n; i++) {
1113         try_rule_out(state, scratch[i].x, scratch[i].y, scratch, n,
1114                      trl_callback_incn, (void*)&(scratch[i]));
1115     }
1116 #ifdef SOLVER_DIAGNOSTICS
1117     if (verbose > 1) debug_scratch("discount_set after count", scratch, n);
1118 #endif
1119
1120     besti = -1; bestn = SCRATCHSZ;
1121     for (i = 0; i < n; i++) {
1122         if (scratch[i].n < bestn) {
1123             bestn = scratch[i].n;
1124             besti = i;
1125         }
1126     }
1127 #ifdef SOLVER_DIAGNOSTICS
1128     if (verbose > 1) debug(("best square (%d,%d) with n%d.\n",
1129            scratch[besti].x, scratch[besti].y, scratch[besti].n));
1130 #endif
1131     try_rule_out(state, scratch[besti].x, scratch[besti].y, scratch, n,
1132                  trl_callback_discount, (void*)&didsth);
1133 #ifdef SOLVER_DIAGNOSTICS
1134     if (didsth) debug((" [from square (%d,%d)]\n",
1135                        scratch[besti].x, scratch[besti].y));
1136 #endif
1137
1138     return didsth;
1139 }
1140
1141 static void discount_clear(game_state *state, struct setscratch *scratch, int *n)
1142 {
1143     *n = 0;
1144     memset(scratch, 0, SCRATCHSZ * sizeof(struct setscratch));
1145 }
1146
1147 static void unlit_cb(game_state *state, int lx, int ly,
1148                      struct setscratch *scratch, int *n)
1149 {
1150     if (could_place_light_xy(state, lx, ly)) {
1151         scratch[*n].x = lx; scratch[*n].y = ly; (*n)++;
1152     }
1153 }
1154
1155 /* Construct a MAKESLIGHT set from an unlit square. */
1156 static int discount_unlit(game_state *state, int x, int y,
1157                           struct setscratch *scratch)
1158 {
1159     ll_data lld;
1160     int n, didsth;
1161
1162 #ifdef SOLVER_DIAGNOSTICS
1163     if (verbose) debug(("Trying to discount for unlit square at (%d,%d).\n", x, y));
1164     if (verbose > 1) debug_state(state);
1165 #endif
1166
1167     discount_clear(state, scratch, &n);
1168
1169     list_lights(state, x, y, 1, &lld);
1170     FOREACHLIT(&lld, { unlit_cb(state, lx, ly, scratch, &n); });
1171     didsth = discount_set(state, scratch, n);
1172 #ifdef SOLVER_DIAGNOSTICS
1173     if (didsth) debug(("  [from unlit square at (%d,%d)].\n", x, y));
1174 #endif
1175     return didsth;
1176
1177 }
1178
1179 /* Construct a series of MAKESLIGHT sets from a clue square.
1180  *  for a clue square with N remaining spaces that must contain M lights, every
1181  *  subset of size N-M+1 of those N spaces forms such a set.
1182  */
1183
1184 static int discount_clue(game_state *state, int x, int y,
1185                           struct setscratch *scratch)
1186 {
1187     int slen, m = GRID(state, lights, x, y), n, i, didsth = 0, lights;
1188     unsigned int flags;
1189     surrounds s, sempty;
1190     combi_ctx *combi;
1191
1192     if (m == 0) return 0;
1193
1194 #ifdef SOLVER_DIAGNOSTICS
1195     if (verbose) debug(("Trying to discount for sets at clue (%d,%d).\n", x, y));
1196     if (verbose > 1) debug_state(state);
1197 #endif
1198
1199     /* m is no. of lights still to place; starts off at the clue value
1200      * and decreases when we find a light already down.
1201      * n is no. of spaces left; starts off at 0 and goes up when we find
1202      * a plausible space. */
1203
1204     get_surrounds(state, x, y, &s);
1205     memset(&sempty, 0, sizeof(surrounds));
1206     for (i = 0; i < s.npoints; i++) {
1207         int lx = s.points[i].x, ly = s.points[i].y;
1208         flags = GRID(state,flags,lx,ly);
1209         lights = GRID(state,lights,lx,ly);
1210
1211         if (flags & F_LIGHT) m--;
1212
1213         if (could_place_light(flags, lights)) {
1214             sempty.points[sempty.npoints].x = lx;
1215             sempty.points[sempty.npoints].y = ly;
1216             sempty.npoints++;
1217         }
1218     }
1219     n = sempty.npoints; /* sempty is now a surrounds of only blank squares. */
1220     if (n == 0) return 0; /* clue is full already. */
1221
1222     if (m < 0 || m > n) return 0; /* become impossible. */
1223
1224     combi = new_combi(n - m + 1, n);
1225     while (next_combi(combi)) {
1226         discount_clear(state, scratch, &slen);
1227         for (i = 0; i < combi->r; i++) {
1228             scratch[slen].x = sempty.points[combi->a[i]].x;
1229             scratch[slen].y = sempty.points[combi->a[i]].y;
1230             slen++;
1231         }
1232         if (discount_set(state, scratch, slen)) didsth = 1;
1233     }
1234     free_combi(combi);
1235 #ifdef SOLVER_DIAGNOSTICS
1236     if (didsth) debug(("  [from clue at (%d,%d)].\n", x, y));
1237 #endif
1238     return didsth;
1239 }
1240
1241 #define F_SOLVE_FORCEUNIQUE     1
1242 #define F_SOLVE_DISCOUNTSETS    2
1243 #define F_SOLVE_ALLOWRECURSE    4
1244
1245 static unsigned int flags_from_difficulty(int difficulty)
1246 {
1247     unsigned int sflags = F_SOLVE_FORCEUNIQUE;
1248     assert(difficulty <= DIFFCOUNT);
1249     if (difficulty >= 1) sflags |= F_SOLVE_DISCOUNTSETS;
1250     if (difficulty >= 2) sflags |= F_SOLVE_ALLOWRECURSE;
1251     return sflags;
1252 }
1253
1254 #define MAXRECURSE 5
1255
1256 static int solve_sub(game_state *state,
1257                      unsigned int solve_flags, int depth,
1258                      int *maxdepth)
1259 {
1260     unsigned int flags;
1261     int x, y, didstuff, ncanplace, lights;
1262     int bestx, besty, n, bestn, copy_soluble, self_soluble, ret, maxrecurse = 0;
1263     game_state *scopy;
1264     ll_data lld;
1265     struct setscratch *sscratch = NULL;
1266
1267 #ifdef SOLVER_DIAGNOSTICS
1268     printf("solve_sub: depth = %d\n", depth);
1269 #endif
1270     if (maxdepth && *maxdepth < depth) *maxdepth = depth;
1271     if (solve_flags & F_SOLVE_ALLOWRECURSE) maxrecurse = MAXRECURSE;
1272
1273     while (1) {
1274         if (grid_overlap(state)) {
1275             /* Our own solver, from scratch, should never cause this to happen
1276              * (assuming a soluble grid). However, if we're trying to solve
1277              * from a half-completed *incorrect* grid this might occur; we
1278              * just return the 'no solutions' code in this case. */
1279             ret = 0; goto done;
1280         }
1281
1282         if (grid_correct(state)) { ret = 1; goto done; }
1283
1284         ncanplace = 0;
1285         didstuff = 0;
1286         /* These 2 loops, and the functions they call, are the critical loops
1287          * for timing; any optimisations should look here first. */
1288         for (x = 0; x < state->w; x++) {
1289             for (y = 0; y < state->h; y++) {
1290                 flags = GRID(state,flags,x,y);
1291                 lights = GRID(state,lights,x,y);
1292                 ncanplace += could_place_light(flags, lights);
1293
1294                 if (try_solve_light(state, x, y, flags, lights)) didstuff = 1;
1295                 if (try_solve_number(state, x, y, flags, lights)) didstuff = 1;
1296             }
1297         }
1298         if (didstuff) continue;
1299         if (!ncanplace) {
1300             /* nowhere to put a light, puzzle is unsoluble. */
1301             ret = 0; goto done;
1302         }
1303
1304         if (solve_flags & F_SOLVE_DISCOUNTSETS) {
1305             if (!sscratch) sscratch = snewn(SCRATCHSZ, struct setscratch);
1306             /* Try a more cunning (and more involved) way... more details above. */
1307             for (x = 0; x < state->w; x++) {
1308                 for (y = 0; y < state->h; y++) {
1309                     flags = GRID(state,flags,x,y);
1310                     lights = GRID(state,lights,x,y);
1311
1312                     if (!(flags & F_BLACK) && lights == 0) {
1313                         if (discount_unlit(state, x, y, sscratch)) {
1314                             didstuff = 1;
1315                             goto reduction_success;
1316                         }
1317                     } else if (flags & F_NUMBERED) {
1318                         if (discount_clue(state, x, y, sscratch)) {
1319                             didstuff = 1;
1320                             goto reduction_success;
1321                         }
1322                     }
1323                 }
1324             }
1325         }
1326 reduction_success:
1327         if (didstuff) continue;
1328
1329         /* We now have to make a guess; we have places to put lights but
1330          * no definite idea about where they can go. */
1331         if (depth >= maxrecurse) {
1332             /* mustn't delve any deeper. */
1333             ret = -1; goto done;
1334         }
1335         /* Of all the squares that we could place a light, pick the one
1336          * that would light the most currently unlit squares. */
1337         /* This heuristic was just plucked from the air; there may well be
1338          * a more efficient way of choosing a square to flip to minimise
1339          * recursion. */
1340         bestn = 0;
1341         bestx = besty = -1; /* suyb */
1342         for (x = 0; x < state->w; x++) {
1343             for (y = 0; y < state->h; y++) {
1344                 flags = GRID(state,flags,x,y);
1345                 lights = GRID(state,lights,x,y);
1346                 if (!could_place_light(flags, lights)) continue;
1347
1348                 n = 0;
1349                 list_lights(state, x, y, 1, &lld);
1350                 FOREACHLIT(&lld, { if (GRID(state,lights,lx,ly) == 0) n++; });
1351                 if (n > bestn) {
1352                     bestn = n; bestx = x; besty = y;
1353                 }
1354             }
1355         }
1356         assert(bestn > 0);
1357         assert(bestx >= 0 && besty >= 0);
1358
1359         /* Now we've chosen a plausible (x,y), try to solve it once as 'lit'
1360          * and once as 'impossible'; we need to make one copy to do this. */
1361
1362         scopy = dup_game(state);
1363 #ifdef SOLVER_DIAGNOSTICS
1364         debug(("Recursing #1: trying (%d,%d) as IMPOSSIBLE\n", bestx, besty));
1365 #endif
1366         GRID(state,flags,bestx,besty) |= F_IMPOSSIBLE;
1367         self_soluble = solve_sub(state, solve_flags,  depth+1, maxdepth);
1368
1369         if (!(solve_flags & F_SOLVE_FORCEUNIQUE) && self_soluble > 0) {
1370             /* we didn't care about finding all solutions, and we just
1371              * found one; return with it immediately. */
1372             free_game(scopy);
1373             ret = self_soluble;
1374             goto done;
1375         }
1376
1377 #ifdef SOLVER_DIAGNOSTICS
1378         debug(("Recursing #2: trying (%d,%d) as LIGHT\n", bestx, besty));
1379 #endif
1380         set_light(scopy, bestx, besty, 1);
1381         copy_soluble = solve_sub(scopy, solve_flags, depth+1, maxdepth);
1382
1383         /* If we wanted a unique solution but we hit our recursion limit
1384          * (on either branch) then we have to assume we didn't find possible
1385          * extra solutions, and return 'not soluble'. */
1386         if ((solve_flags & F_SOLVE_FORCEUNIQUE) &&
1387             ((copy_soluble < 0) || (self_soluble < 0))) {
1388             ret = -1;
1389         /* Make sure that whether or not it was self or copy (or both) that
1390          * were soluble, that we return a solved state in self. */
1391         } else if (copy_soluble <= 0) {
1392             /* copy wasn't soluble; keep self state and return that result. */
1393             ret = self_soluble;
1394         } else if (self_soluble <= 0) {
1395             /* copy solved and we didn't, so copy in copy's (now solved)
1396              * flags and light state. */
1397             memcpy(state->lights, scopy->lights,
1398                    scopy->w * scopy->h * sizeof(int));
1399             memcpy(state->flags, scopy->flags,
1400                    scopy->w * scopy->h * sizeof(unsigned int));
1401             ret = copy_soluble;
1402         } else {
1403             ret = copy_soluble + self_soluble;
1404         }
1405         free_game(scopy);
1406         goto done;
1407     }
1408 done:
1409     if (sscratch) sfree(sscratch);
1410 #ifdef SOLVER_DIAGNOSTICS
1411     if (ret < 0)
1412         debug(("solve_sub: depth = %d returning, ran out of recursion.\n",
1413                depth));
1414     else
1415         debug(("solve_sub: depth = %d returning, %d solutions.\n",
1416                depth, ret));
1417 #endif
1418     return ret;
1419 }
1420
1421 /* Fills in the (possibly partially-complete) game_state as far as it can,
1422  * returning the number of possible solutions. If it returns >0 then the
1423  * game_state will be in a solved state, but you won't know which one. */
1424 static int dosolve(game_state *state, int solve_flags, int *maxdepth)
1425 {
1426     int x, y, nsol;
1427
1428     for (x = 0; x < state->w; x++) {
1429         for (y = 0; y < state->h; y++) {
1430             GRID(state,flags,x,y) &= ~F_NUMBERUSED;
1431         }
1432     }
1433     nsol = solve_sub(state, solve_flags, 0, maxdepth);
1434     return nsol;
1435 }
1436
1437 static int strip_unused_nums(game_state *state)
1438 {
1439     int x,y,n=0;
1440     for (x = 0; x < state->w; x++) {
1441         for (y = 0; y < state->h; y++) {
1442             if ((GRID(state,flags,x,y) & F_NUMBERED) &&
1443                 !(GRID(state,flags,x,y) & F_NUMBERUSED)) {
1444                 GRID(state,flags,x,y) &= ~F_NUMBERED;
1445                 GRID(state,lights,x,y) = 0;
1446                 n++;
1447             }
1448         }
1449     }
1450     debug(("Stripped %d unused numbers.\n", n));
1451     return n;
1452 }
1453
1454 static void unplace_lights(game_state *state)
1455 {
1456     int x,y;
1457     for (x = 0; x < state->w; x++) {
1458         for (y = 0; y < state->h; y++) {
1459             if (GRID(state,flags,x,y) & F_LIGHT)
1460                 set_light(state,x,y,0);
1461             GRID(state,flags,x,y) &= ~F_IMPOSSIBLE;
1462             GRID(state,flags,x,y) &= ~F_NUMBERUSED;
1463         }
1464     }
1465 }
1466
1467 static int puzzle_is_good(game_state *state, int difficulty)
1468 {
1469     int nsol, mdepth = 0;
1470     unsigned int sflags = flags_from_difficulty(difficulty);
1471
1472     unplace_lights(state);
1473
1474 #ifdef SOLVER_DIAGNOSTICS
1475     debug(("Trying to solve with difficulty %d (0x%x):\n",
1476            difficulty, sflags));
1477     if (verbose) debug_state(state);
1478 #endif
1479
1480     nsol = dosolve(state, sflags, &mdepth);
1481     /* if we wanted an easy puzzle, make sure we didn't need recursion. */
1482     if (!(sflags & F_SOLVE_ALLOWRECURSE) && mdepth > 0) {
1483         debug(("Ignoring recursive puzzle.\n"));
1484         return 0;
1485     }
1486
1487     debug(("%d solutions found.\n", nsol));
1488     if (nsol <= 0) return 0;
1489     if (nsol > 1) return 0;
1490     return 1;
1491 }
1492
1493 /* --- New game creation and user input code. --- */
1494
1495 /* The basic algorithm here is to generate the most complex grid possible
1496  * while honouring two restrictions:
1497  *
1498  *  * we require a unique solution, and
1499  *  * either we require solubility with no recursion (!params->recurse)
1500  *  * or we require some recursion. (params->recurse).
1501  *
1502  * The solver helpfully keeps track of the numbers it needed to use to
1503  * get its solution, so we use that to remove an initial set of numbers
1504  * and check we still satsify our requirements (on uniqueness and
1505  * non-recursiveness, if applicable; we don't check explicit recursiveness
1506  * until the end).
1507  *
1508  * Then we try to remove all numbers in a random order, and see if we
1509  * still satisfy requirements (putting them back if we didn't).
1510  *
1511  * Removing numbers will always, in general terms, make a puzzle require
1512  * more recursion but it may also mean a puzzle becomes non-unique.
1513  *
1514  * Once we're done, if we wanted a recursive puzzle but the most difficult
1515  * puzzle we could come up with was non-recursive, we give up and try a new
1516  * grid. */
1517
1518 #define MAX_GRIDGEN_TRIES 20
1519
1520 static char *new_game_desc(const game_params *params_in, random_state *rs,
1521                            char **aux, int interactive)
1522 {
1523     game_params params_copy = *params_in; /* structure copy */
1524     game_params *params = &params_copy;
1525     game_state *news = new_state(params), *copys;
1526     int i, j, run, x, y, wh = params->w*params->h, num;
1527     char *ret, *p;
1528     int *numindices;
1529
1530     /* Construct a shuffled list of grid positions; we only
1531      * do this once, because if it gets used more than once it'll
1532      * be on a different grid layout. */
1533     numindices = snewn(wh, int);
1534     for (j = 0; j < wh; j++) numindices[j] = j;
1535     shuffle(numindices, wh, sizeof(*numindices), rs);
1536
1537     while (1) {
1538         for (i = 0; i < MAX_GRIDGEN_TRIES; i++) {
1539             set_blacks(news, params, rs); /* also cleans board. */
1540
1541             /* set up lights and then the numbers, and remove the lights */
1542             place_lights(news, rs);
1543             debug(("Generating initial grid.\n"));
1544             place_numbers(news);
1545             if (!puzzle_is_good(news, params->difficulty)) continue;
1546
1547             /* Take a copy, remove numbers we didn't use and check there's
1548              * still a unique solution; if so, use the copy subsequently. */
1549             copys = dup_game(news);
1550             strip_unused_nums(copys);
1551             if (!puzzle_is_good(copys, params->difficulty)) {
1552                 debug(("Stripped grid is not good, reverting.\n"));
1553                 free_game(copys);
1554             } else {
1555                 free_game(news);
1556                 news = copys;
1557             }
1558
1559             /* Go through grid removing numbers at random one-by-one and
1560              * trying to solve again; if it ceases to be good put the number back. */
1561             for (j = 0; j < wh; j++) {
1562                 y = numindices[j] / params->w;
1563                 x = numindices[j] % params->w;
1564                 if (!(GRID(news, flags, x, y) & F_NUMBERED)) continue;
1565                 num = GRID(news, lights, x, y);
1566                 GRID(news, lights, x, y) = 0;
1567                 GRID(news, flags, x, y) &= ~F_NUMBERED;
1568                 if (!puzzle_is_good(news, params->difficulty)) {
1569                     GRID(news, lights, x, y) = num;
1570                     GRID(news, flags, x, y) |= F_NUMBERED;
1571                 } else
1572                     debug(("Removed (%d,%d) still soluble.\n", x, y));
1573             }
1574             if (params->difficulty > 0) {
1575                 /* Was the maximally-difficult puzzle difficult enough?
1576                  * Check we can't solve it with a more simplistic solver. */
1577                 if (puzzle_is_good(news, params->difficulty-1)) {
1578                     debug(("Maximally-hard puzzle still not hard enough, skipping.\n"));
1579                     continue;
1580                 }
1581             }
1582
1583             goto goodpuzzle;
1584         }
1585         /* Couldn't generate a good puzzle in however many goes. Ramp up the
1586          * %age of black squares (if we didn't already have lots; in which case
1587          * why couldn't we generate a puzzle?) and try again. */
1588         if (params->blackpc < 90) params->blackpc += 5;
1589         debug(("New black layout %d%%.\n", params->blackpc));
1590     }
1591 goodpuzzle:
1592     /* Game is encoded as a long string one character per square;
1593      * 'S' is a space
1594      * 'B' is a black square with no number
1595      * '0', '1', '2', '3', '4' is a black square with a number. */
1596     ret = snewn((params->w * params->h) + 1, char);
1597     p = ret;
1598     run = 0;
1599     for (y = 0; y < params->h; y++) {
1600         for (x = 0; x < params->w; x++) {
1601             if (GRID(news,flags,x,y) & F_BLACK) {
1602                 if (run) {
1603                     *p++ = ('a'-1) + run;
1604                     run = 0;
1605                 }
1606                 if (GRID(news,flags,x,y) & F_NUMBERED)
1607                     *p++ = '0' + GRID(news,lights,x,y);
1608                 else
1609                     *p++ = 'B';
1610             } else {
1611                 if (run == 26) {
1612                     *p++ = ('a'-1) + run;
1613                     run = 0;
1614                 }
1615                 run++;
1616             }
1617         }
1618     }
1619     if (run) {
1620         *p++ = ('a'-1) + run;
1621         run = 0;
1622     }
1623     *p = '\0';
1624     assert(p - ret <= params->w * params->h);
1625     free_game(news);
1626     sfree(numindices);
1627
1628     return ret;
1629 }
1630
1631 static char *validate_desc(const game_params *params, char *desc)
1632 {
1633     int i;
1634     for (i = 0; i < params->w*params->h; i++) {
1635         if (*desc >= '0' && *desc <= '4')
1636             /* OK */;
1637         else if (*desc == 'B')
1638             /* OK */;
1639         else if (*desc >= 'a' && *desc <= 'z')
1640             i += *desc - 'a';          /* and the i++ will add another one */
1641         else if (!*desc)
1642             return "Game description shorter than expected";
1643         else
1644             return "Game description contained unexpected character";
1645         desc++;
1646     }
1647     if (*desc || i > params->w*params->h)
1648         return "Game description longer than expected";
1649
1650     return NULL;
1651 }
1652
1653 static game_state *new_game(midend *me, game_params *params, char *desc)
1654 {
1655     game_state *ret = new_state(params);
1656     int x,y;
1657     int run = 0;
1658
1659     for (y = 0; y < params->h; y++) {
1660         for (x = 0; x < params->w; x++) {
1661             char c = '\0';
1662
1663             if (run == 0) {
1664                 c = *desc++;
1665                 assert(c != 'S');
1666                 if (c >= 'a' && c <= 'z')
1667                     run = c - 'a' + 1;
1668             }
1669
1670             if (run > 0) {
1671                 c = 'S';
1672                 run--;
1673             }
1674
1675             switch (c) {
1676               case '0': case '1': case '2': case '3': case '4':
1677                 GRID(ret,flags,x,y) |= F_NUMBERED;
1678                 GRID(ret,lights,x,y) = (c - '0');
1679                 /* run-on... */
1680
1681               case 'B':
1682                 GRID(ret,flags,x,y) |= F_BLACK;
1683                 break;
1684
1685               case 'S':
1686                 /* empty square */
1687                 break;
1688
1689               default:
1690                 assert(!"Malformed desc.");
1691                 break;
1692             }
1693         }
1694     }
1695     if (*desc) assert(!"Over-long desc.");
1696
1697     return ret;
1698 }
1699
1700 static char *solve_game(game_state *state, game_state *currstate,
1701                         char *aux, char **error)
1702 {
1703     game_state *solved;
1704     char *move = NULL, buf[80];
1705     int movelen, movesize, x, y, len;
1706     unsigned int oldflags, solvedflags, sflags;
1707
1708     /* We don't care here about non-unique puzzles; if the
1709      * user entered one themself then I doubt they care. */
1710
1711     sflags = F_SOLVE_ALLOWRECURSE | F_SOLVE_DISCOUNTSETS;
1712
1713     /* Try and solve from where we are now (for non-unique
1714      * puzzles this may produce a different answer). */
1715     solved = dup_game(currstate);
1716     if (dosolve(solved, sflags, NULL) > 0) goto solved;
1717     free_game(solved);
1718
1719     /* That didn't work; try solving from the clean puzzle. */
1720     solved = dup_game(state);
1721     if (dosolve(solved, sflags, NULL) > 0) goto solved;
1722     *error = "Unable to find a solution to this puzzle.";
1723     goto done;
1724
1725 solved:
1726     movesize = 256;
1727     move = snewn(movesize, char);
1728     movelen = 0;
1729     move[movelen++] = 'S';
1730     move[movelen] = '\0';
1731     for (x = 0; x < currstate->w; x++) {
1732         for (y = 0; y < currstate->h; y++) {
1733             len = 0;
1734             oldflags = GRID(currstate, flags, x, y);
1735             solvedflags = GRID(solved, flags, x, y);
1736             if ((oldflags & F_LIGHT) != (solvedflags & F_LIGHT))
1737                 len = sprintf(buf, ";L%d,%d", x, y);
1738             else if ((oldflags & F_IMPOSSIBLE) != (solvedflags & F_IMPOSSIBLE))
1739                 len = sprintf(buf, ";I%d,%d", x, y);
1740             if (len) {
1741                 if (movelen + len >= movesize) {
1742                     movesize = movelen + len + 256;
1743                     move = sresize(move, movesize, char);
1744                 }
1745                 strcpy(move + movelen, buf);
1746                 movelen += len;
1747             }
1748         }
1749     }
1750
1751 done:
1752     free_game(solved);
1753     return move;
1754 }
1755
1756 static int game_can_format_as_text_now(game_params *params)
1757 {
1758     return TRUE;
1759 }
1760
1761 /* 'borrowed' from slant.c, mainly. I could have printed it one
1762  * character per cell (like debug_state) but that comes out tiny.
1763  * 'L' is used for 'light here' because 'O' looks too much like '0'
1764  * (black square with no surrounding lights). */
1765 static char *game_text_format(game_state *state)
1766 {
1767     int w = state->w, h = state->h, W = w+1, H = h+1;
1768     int x, y, len, lights;
1769     unsigned int flags;
1770     char *ret, *p;
1771
1772     len = (h+H) * (w+W+1) + 1;
1773     ret = snewn(len, char);
1774     p = ret;
1775
1776     for (y = 0; y < H; y++) {
1777         for (x = 0; x < W; x++) {
1778             *p++ = '+';
1779             if (x < w)
1780                 *p++ = '-';
1781         }
1782         *p++ = '\n';
1783         if (y < h) {
1784             for (x = 0; x < W; x++) {
1785                 *p++ = '|';
1786                 if (x < w) {
1787                     /* actual interesting bit. */
1788                     flags = GRID(state, flags, x, y);
1789                     lights = GRID(state, lights, x, y);
1790                     if (flags & F_BLACK) {
1791                         if (flags & F_NUMBERED)
1792                             *p++ = '0' + lights;
1793                         else
1794                             *p++ = '#';
1795                     } else {
1796                         if (flags & F_LIGHT)
1797                             *p++ = 'L';
1798                         else if (flags & F_IMPOSSIBLE)
1799                             *p++ = 'x';
1800                         else if (lights > 0)
1801                             *p++ = '.';
1802                         else
1803                             *p++ = ' ';
1804                     }
1805                 }
1806             }
1807             *p++ = '\n';
1808         }
1809     }
1810     *p++ = '\0';
1811
1812     assert(p - ret == len);
1813     return ret;
1814 }
1815
1816 struct game_ui {
1817     int cur_x, cur_y, cur_visible;
1818 };
1819
1820 static game_ui *new_ui(game_state *state)
1821 {
1822     game_ui *ui = snew(game_ui);
1823     ui->cur_x = ui->cur_y = ui->cur_visible = 0;
1824     return ui;
1825 }
1826
1827 static void free_ui(game_ui *ui)
1828 {
1829     sfree(ui);
1830 }
1831
1832 static char *encode_ui(game_ui *ui)
1833 {
1834     /* nothing to encode. */
1835     return NULL;
1836 }
1837
1838 static void decode_ui(game_ui *ui, char *encoding)
1839 {
1840     /* nothing to decode. */
1841 }
1842
1843 static void game_changed_state(game_ui *ui, game_state *oldstate,
1844                                game_state *newstate)
1845 {
1846     if (newstate->completed)
1847         ui->cur_visible = 0;
1848 }
1849
1850 #define DF_BLACK        1       /* black square */
1851 #define DF_NUMBERED     2       /* black square with number */
1852 #define DF_LIT          4       /* display (white) square lit up */
1853 #define DF_LIGHT        8       /* display light in square */
1854 #define DF_OVERLAP      16      /* display light as overlapped */
1855 #define DF_CURSOR       32      /* display cursor */
1856 #define DF_NUMBERWRONG  64      /* display black numbered square as error. */
1857 #define DF_FLASH        128     /* background flash is on. */
1858 #define DF_IMPOSSIBLE   256     /* display non-light little square */
1859
1860 struct game_drawstate {
1861     int tilesize, crad;
1862     int w, h;
1863     unsigned int *flags;         /* width * height */
1864     int started;
1865 };
1866
1867
1868 /* Believe it or not, this empty = "" hack is needed to get around a bug in
1869  * the prc-tools gcc when optimisation is turned on; before, it produced:
1870     lightup-sect.c: In function `interpret_move':
1871     lightup-sect.c:1416: internal error--unrecognizable insn:
1872     (insn 582 580 583 (set (reg:SI 134)
1873             (pc)) -1 (nil)
1874         (nil))
1875  */
1876 static char *interpret_move(game_state *state, game_ui *ui, const game_drawstate *ds,
1877                             int x, int y, int button)
1878 {
1879     enum { NONE, FLIP_LIGHT, FLIP_IMPOSSIBLE } action = NONE;
1880     int cx = -1, cy = -1;
1881     unsigned int flags;
1882     char buf[80], *nullret = NULL, *empty = "", c;
1883
1884     if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
1885         if (ui->cur_visible)
1886             nullret = empty;
1887         ui->cur_visible = 0;
1888         cx = FROMCOORD(x);
1889         cy = FROMCOORD(y);
1890         action = (button == LEFT_BUTTON) ? FLIP_LIGHT : FLIP_IMPOSSIBLE;
1891     } else if (IS_CURSOR_SELECT(button) ||
1892                button == 'i' || button == 'I' ||
1893                button == ' ' || button == '\r' || button == '\n') {
1894         if (ui->cur_visible) {
1895             /* Only allow cursor-effect operations if the cursor is visible
1896              * (otherwise you have no idea which square it might be affecting) */
1897             cx = ui->cur_x;
1898             cy = ui->cur_y;
1899             action = (button == 'i' || button == 'I' || button == CURSOR_SELECT2) ?
1900                 FLIP_IMPOSSIBLE : FLIP_LIGHT;
1901         }
1902         ui->cur_visible = 1;
1903     } else if (IS_CURSOR_MOVE(button)) {
1904         move_cursor(button, &ui->cur_x, &ui->cur_y, state->w, state->h, 0);
1905         ui->cur_visible = 1;
1906         nullret = empty;
1907     } else
1908         return NULL;
1909
1910     switch (action) {
1911     case FLIP_LIGHT:
1912     case FLIP_IMPOSSIBLE:
1913         if (cx < 0 || cy < 0 || cx >= state->w || cy >= state->h)
1914             return nullret;
1915         flags = GRID(state, flags, cx, cy);
1916         if (flags & F_BLACK)
1917             return nullret;
1918         if (action == FLIP_LIGHT) {
1919 #ifdef STYLUS_BASED
1920             if (flags & F_IMPOSSIBLE || flags & F_LIGHT) c = 'I'; else c = 'L';
1921 #else
1922             if (flags & F_IMPOSSIBLE) return nullret;
1923             c = 'L';
1924 #endif
1925         } else {
1926 #ifdef STYLUS_BASED
1927             if (flags & F_IMPOSSIBLE || flags & F_LIGHT) c = 'L'; else c = 'I';
1928 #else
1929             if (flags & F_LIGHT) return nullret;
1930             c = 'I';
1931 #endif
1932         }
1933         sprintf(buf, "%c%d,%d", (int)c, cx, cy);
1934         break;
1935
1936     case NONE:
1937         return nullret;
1938
1939     default:
1940         assert(!"Shouldn't get here!");
1941     }
1942     return dupstr(buf);
1943 }
1944
1945 static game_state *execute_move(game_state *state, char *move)
1946 {
1947     game_state *ret = dup_game(state);
1948     int x, y, n, flags;
1949     char c;
1950
1951     if (!*move) goto badmove;
1952
1953     while (*move) {
1954         c = *move;
1955         if (c == 'S') {
1956             ret->used_solve = TRUE;
1957             move++;
1958         } else if (c == 'L' || c == 'I') {
1959             move++;
1960             if (sscanf(move, "%d,%d%n", &x, &y, &n) != 2 ||
1961                 x < 0 || y < 0 || x >= ret->w || y >= ret->h)
1962                 goto badmove;
1963
1964             flags = GRID(ret, flags, x, y);
1965             if (flags & F_BLACK) goto badmove;
1966
1967             /* LIGHT and IMPOSSIBLE are mutually exclusive. */
1968             if (c == 'L') {
1969                 GRID(ret, flags, x, y) &= ~F_IMPOSSIBLE;
1970                 set_light(ret, x, y, (flags & F_LIGHT) ? 0 : 1);
1971             } else {
1972                 set_light(ret, x, y, 0);
1973                 GRID(ret, flags, x, y) ^= F_IMPOSSIBLE;
1974             }
1975             move += n;
1976         } else goto badmove;
1977
1978         if (*move == ';')
1979             move++;
1980         else if (*move) goto badmove;
1981     }
1982     if (grid_correct(ret)) ret->completed = 1;
1983     return ret;
1984
1985 badmove:
1986     free_game(ret);
1987     return NULL;
1988 }
1989
1990 /* ----------------------------------------------------------------------
1991  * Drawing routines.
1992  */
1993
1994 /* XXX entirely cloned from fifteen.c; separate out? */
1995 static void game_compute_size(game_params *params, int tilesize,
1996                               int *x, int *y)
1997 {
1998     /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1999     struct { int tilesize; } ads, *ds = &ads;
2000     ads.tilesize = tilesize;
2001
2002     *x = TILE_SIZE * params->w + 2 * BORDER;
2003     *y = TILE_SIZE * params->h + 2 * BORDER;
2004 }
2005
2006 static void game_set_size(drawing *dr, game_drawstate *ds,
2007                           game_params *params, int tilesize)
2008 {
2009     ds->tilesize = tilesize;
2010     ds->crad = 3*(tilesize-1)/8;
2011 }
2012
2013 static float *game_colours(frontend *fe, int *ncolours)
2014 {
2015     float *ret = snewn(3 * NCOLOURS, float);
2016     int i;
2017
2018     frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
2019
2020     for (i = 0; i < 3; i++) {
2021         ret[COL_BLACK * 3 + i] = 0.0F;
2022         ret[COL_LIGHT * 3 + i] = 1.0F;
2023         ret[COL_CURSOR * 3 + i] = ret[COL_BACKGROUND * 3 + i] / 2.0F;
2024         ret[COL_GRID * 3 + i] = ret[COL_BACKGROUND * 3 + i] / 1.5F;
2025
2026     }
2027
2028     ret[COL_ERROR * 3 + 0] = 1.0F;
2029     ret[COL_ERROR * 3 + 1] = 0.25F;
2030     ret[COL_ERROR * 3 + 2] = 0.25F;
2031
2032     ret[COL_LIT * 3 + 0] = 1.0F;
2033     ret[COL_LIT * 3 + 1] = 1.0F;
2034     ret[COL_LIT * 3 + 2] = 0.0F;
2035
2036     *ncolours = NCOLOURS;
2037     return ret;
2038 }
2039
2040 static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
2041 {
2042     struct game_drawstate *ds = snew(struct game_drawstate);
2043     int i;
2044
2045     ds->tilesize = ds->crad = 0;
2046     ds->w = state->w; ds->h = state->h;
2047
2048     ds->flags = snewn(ds->w*ds->h, unsigned int);
2049     for (i = 0; i < ds->w*ds->h; i++)
2050         ds->flags[i] = -1;
2051
2052     ds->started = 0;
2053
2054     return ds;
2055 }
2056
2057 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
2058 {
2059     sfree(ds->flags);
2060     sfree(ds);
2061 }
2062
2063 /* At some stage we should put these into a real options struct.
2064  * Note that tile_redraw has no #ifdeffery; it relies on tile_flags not
2065  * to put those flags in. */
2066 #define HINT_LIGHTS
2067 #define HINT_OVERLAPS
2068 #define HINT_NUMBERS
2069
2070 static unsigned int tile_flags(game_drawstate *ds, game_state *state, game_ui *ui,
2071                                int x, int y, int flashing)
2072 {
2073     unsigned int flags = GRID(state, flags, x, y);
2074     int lights = GRID(state, lights, x, y);
2075     unsigned int ret = 0;
2076
2077     if (flashing) ret |= DF_FLASH;
2078     if (ui && ui->cur_visible && x == ui->cur_x && y == ui->cur_y)
2079         ret |= DF_CURSOR;
2080
2081     if (flags & F_BLACK) {
2082         ret |= DF_BLACK;
2083         if (flags & F_NUMBERED) {
2084 #ifdef HINT_NUMBERS
2085             if (number_wrong(state, x, y))
2086                 ret |= DF_NUMBERWRONG;
2087 #endif
2088             ret |= DF_NUMBERED;
2089         }
2090     } else {
2091 #ifdef HINT_LIGHTS
2092         if (lights > 0) ret |= DF_LIT;
2093 #endif
2094         if (flags & F_LIGHT) {
2095             ret |= DF_LIGHT;
2096 #ifdef HINT_OVERLAPS
2097             if (lights > 1) ret |= DF_OVERLAP;
2098 #endif
2099         }
2100         if (flags & F_IMPOSSIBLE) ret |= DF_IMPOSSIBLE;
2101     }
2102     return ret;
2103 }
2104
2105 static void tile_redraw(drawing *dr, game_drawstate *ds, game_state *state,
2106                         int x, int y)
2107 {
2108     unsigned int ds_flags = GRID(ds, flags, x, y);
2109     int dx = COORD(x), dy = COORD(y);
2110     int lit = (ds_flags & DF_FLASH) ? COL_GRID : COL_LIT;
2111
2112     if (ds_flags & DF_BLACK) {
2113         draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_BLACK);
2114         if (ds_flags & DF_NUMBERED) {
2115             int ccol = (ds_flags & DF_NUMBERWRONG) ? COL_ERROR : COL_LIGHT;
2116             char str[32];
2117
2118             /* We know that this won't change over the course of the game
2119              * so it's OK to ignore this when calculating whether or not
2120              * to redraw the tile. */
2121             sprintf(str, "%d", GRID(state, lights, x, y));
2122             draw_text(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
2123                       FONT_VARIABLE, TILE_SIZE*3/5,
2124                       ALIGN_VCENTRE | ALIGN_HCENTRE, ccol, str);
2125         }
2126     } else {
2127         draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE,
2128                   (ds_flags & DF_LIT) ? lit : COL_BACKGROUND);
2129         draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
2130         if (ds_flags & DF_LIGHT) {
2131             int lcol = (ds_flags & DF_OVERLAP) ? COL_ERROR : COL_LIGHT;
2132             draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, TILE_RADIUS,
2133                         lcol, COL_BLACK);
2134         } else if ((ds_flags & DF_IMPOSSIBLE)) {
2135             static int draw_blobs_when_lit = -1;
2136             if (draw_blobs_when_lit < 0) {
2137                 char *env = getenv("LIGHTUP_LIT_BLOBS");
2138                 draw_blobs_when_lit = (!env || (env[0] == 'y' ||
2139                                                 env[0] == 'Y'));
2140             }
2141             if (!(ds_flags & DF_LIT) || draw_blobs_when_lit) {
2142                 int rlen = TILE_SIZE / 4;
2143                 draw_rect(dr, dx + TILE_SIZE/2 - rlen/2,
2144                           dy + TILE_SIZE/2 - rlen/2,
2145                           rlen, rlen, COL_BLACK);
2146             }
2147         }
2148     }
2149
2150     if (ds_flags & DF_CURSOR) {
2151         int coff = TILE_SIZE/8;
2152         draw_rect_outline(dr, dx + coff, dy + coff,
2153                           TILE_SIZE - coff*2, TILE_SIZE - coff*2, COL_CURSOR);
2154     }
2155
2156     draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
2157 }
2158
2159 static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
2160                         game_state *state, int dir, game_ui *ui,
2161                         float animtime, float flashtime)
2162 {
2163     int flashing = FALSE;
2164     int x,y;
2165
2166     if (flashtime) flashing = (int)(flashtime * 3 / FLASH_TIME) != 1;
2167
2168     if (!ds->started) {
2169         draw_rect(dr, 0, 0,
2170                   TILE_SIZE * ds->w + 2 * BORDER,
2171                   TILE_SIZE * ds->h + 2 * BORDER, COL_BACKGROUND);
2172
2173         draw_rect_outline(dr, COORD(0)-1, COORD(0)-1,
2174                           TILE_SIZE * ds->w + 2,
2175                           TILE_SIZE * ds->h + 2,
2176                           COL_GRID);
2177
2178         draw_update(dr, 0, 0,
2179                     TILE_SIZE * ds->w + 2 * BORDER,
2180                     TILE_SIZE * ds->h + 2 * BORDER);
2181         ds->started = 1;
2182     }
2183
2184     for (x = 0; x < ds->w; x++) {
2185         for (y = 0; y < ds->h; y++) {
2186             unsigned int ds_flags = tile_flags(ds, state, ui, x, y, flashing);
2187             if (ds_flags != GRID(ds, flags, x, y)) {
2188                 GRID(ds, flags, x, y) = ds_flags;
2189                 tile_redraw(dr, ds, state, x, y);
2190             }
2191         }
2192     }
2193 }
2194
2195 static float game_anim_length(game_state *oldstate, game_state *newstate,
2196                               int dir, game_ui *ui)
2197 {
2198     return 0.0F;
2199 }
2200
2201 static float game_flash_length(game_state *oldstate, game_state *newstate,
2202                                int dir, game_ui *ui)
2203 {
2204     if (!oldstate->completed && newstate->completed &&
2205         !oldstate->used_solve && !newstate->used_solve)
2206         return FLASH_TIME;
2207     return 0.0F;
2208 }
2209
2210 static int game_status(game_state *state)
2211 {
2212     return state->completed ? +1 : 0;
2213 }
2214
2215 static int game_timing_state(game_state *state, game_ui *ui)
2216 {
2217     return TRUE;
2218 }
2219
2220 static void game_print_size(game_params *params, float *x, float *y)
2221 {
2222     int pw, ph;
2223
2224     /*
2225      * I'll use 6mm squares by default.
2226      */
2227     game_compute_size(params, 600, &pw, &ph);
2228     *x = pw / 100.0F;
2229     *y = ph / 100.0F;
2230 }
2231
2232 static void game_print(drawing *dr, game_state *state, int tilesize)
2233 {
2234     int w = state->w, h = state->h;
2235     int ink = print_mono_colour(dr, 0);
2236     int paper = print_mono_colour(dr, 1);
2237     int x, y;
2238
2239     /* Ick: fake up `ds->tilesize' for macro expansion purposes */
2240     game_drawstate ads, *ds = &ads;
2241     game_set_size(dr, ds, NULL, tilesize);
2242
2243     /*
2244      * Border.
2245      */
2246     print_line_width(dr, TILE_SIZE / 16);
2247     draw_rect_outline(dr, COORD(0), COORD(0),
2248                       TILE_SIZE * w, TILE_SIZE * h, ink);
2249
2250     /*
2251      * Grid.
2252      */
2253     print_line_width(dr, TILE_SIZE / 24);
2254     for (x = 1; x < w; x++)
2255         draw_line(dr, COORD(x), COORD(0), COORD(x), COORD(h), ink);
2256     for (y = 1; y < h; y++)
2257         draw_line(dr, COORD(0), COORD(y), COORD(w), COORD(y), ink);
2258
2259     /*
2260      * Grid contents.
2261      */
2262     for (y = 0; y < h; y++)
2263         for (x = 0; x < w; x++) {
2264             unsigned int ds_flags = tile_flags(ds, state, NULL, x, y, FALSE);
2265             int dx = COORD(x), dy = COORD(y);
2266             if (ds_flags & DF_BLACK) {
2267                 draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, ink);
2268                 if (ds_flags & DF_NUMBERED) {
2269                     char str[32];
2270                     sprintf(str, "%d", GRID(state, lights, x, y));
2271                     draw_text(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
2272                               FONT_VARIABLE, TILE_SIZE*3/5,
2273                               ALIGN_VCENTRE | ALIGN_HCENTRE, paper, str);
2274                 }
2275             } else if (ds_flags & DF_LIGHT) {
2276                 draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
2277                             TILE_RADIUS, -1, ink);
2278             }
2279         }
2280 }
2281
2282 #ifdef COMBINED
2283 #define thegame lightup
2284 #endif
2285
2286 const struct game thegame = {
2287     "Light Up", "games.lightup", "lightup",
2288     default_params,
2289     game_fetch_preset,
2290     decode_params,
2291     encode_params,
2292     free_params,
2293     dup_params,
2294     TRUE, game_configure, custom_params,
2295     validate_params,
2296     new_game_desc,
2297     validate_desc,
2298     new_game,
2299     dup_game,
2300     free_game,
2301     TRUE, solve_game,
2302     TRUE, game_can_format_as_text_now, game_text_format,
2303     new_ui,
2304     free_ui,
2305     encode_ui,
2306     decode_ui,
2307     game_changed_state,
2308     interpret_move,
2309     execute_move,
2310     PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
2311     game_colours,
2312     game_new_drawstate,
2313     game_free_drawstate,
2314     game_redraw,
2315     game_anim_length,
2316     game_flash_length,
2317     game_status,
2318     TRUE, FALSE, game_print_size, game_print,
2319     FALSE,                             /* wants_statusbar */
2320     FALSE, game_timing_state,
2321     0,                                 /* flags */
2322 };
2323
2324 #ifdef STANDALONE_SOLVER
2325
2326 int main(int argc, char **argv)
2327 {
2328     game_params *p;
2329     game_state *s;
2330     char *id = NULL, *desc, *err, *result;
2331     int nsol, diff, really_verbose = 0;
2332     unsigned int sflags;
2333
2334     while (--argc > 0) {
2335         char *p = *++argv;
2336         if (!strcmp(p, "-v")) {
2337             really_verbose++;
2338         } else if (*p == '-') {
2339             fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
2340             return 1;
2341         } else {
2342             id = p;
2343         }
2344     }
2345
2346     if (!id) {
2347         fprintf(stderr, "usage: %s [-v] <game_id>\n", argv[0]);
2348         return 1;
2349     }
2350
2351     desc = strchr(id, ':');
2352     if (!desc) {
2353         fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
2354         return 1;
2355     }
2356     *desc++ = '\0';
2357
2358     p = default_params();
2359     decode_params(p, id);
2360     err = validate_desc(p, desc);
2361     if (err) {
2362         fprintf(stderr, "%s: %s\n", argv[0], err);
2363         return 1;
2364     }
2365     s = new_game(NULL, p, desc);
2366
2367     /* Run the solvers easiest to hardest until we find one that
2368      * can solve our puzzle. If it's soluble we know that the
2369      * hardest (recursive) solver will always find the solution. */
2370     nsol = sflags = 0;
2371     for (diff = 0; diff <= DIFFCOUNT; diff++) {
2372         printf("\nSolving with difficulty %d.\n", diff);
2373         sflags = flags_from_difficulty(diff);
2374         unplace_lights(s);
2375         nsol = dosolve(s, sflags, NULL);
2376         if (nsol == 1) break;
2377     }
2378
2379     printf("\n");
2380     if (nsol == 0) {
2381         printf("Puzzle has no solution.\n");
2382     } else if (nsol < 0) {
2383         printf("Unable to find a unique solution.\n");
2384     } else if (nsol > 1) {
2385         printf("Puzzle has multiple solutions.\n");
2386     } else {
2387         verbose = really_verbose;
2388         unplace_lights(s);
2389         printf("Puzzle has difficulty %d: solving...\n", diff);
2390         dosolve(s, sflags, NULL); /* sflags from last successful solve */
2391         result = game_text_format(s);
2392         printf("%s", result);
2393         sfree(result);
2394     }
2395
2396     return 0;
2397 }
2398
2399 #endif
2400
2401 /* vim: set shiftwidth=4 tabstop=8: */