e7c63b02 |
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 | { |
06fb836f |
1015 | int x, y, ox, oy, nx = 0, ny = 0, loop = 0; |
e7c63b02 |
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 | for (i = 0; i < wh; i++) |
1068 | dsf[i] = i; |
1069 | |
1070 | /* For each island, find connected islands right or down |
1071 | * and merge the dsf for the island squares as well as the |
1072 | * bridge squares. */ |
1073 | for (x = 0; x < state->w; x++) { |
1074 | for (y = 0; y < state->h; y++) { |
1075 | GRID(state,x,y) &= ~(G_SWEEP|G_WARN); /* for group_full. */ |
1076 | |
1077 | is = INDEX(state, gridi, x, y); |
1078 | if (!is) continue; |
1079 | d1 = DINDEX(x,y); |
1080 | for (i = 0; i < is->adj.npoints; i++) { |
1081 | /* only want right/down */ |
1082 | if (is->adj.points[i].dx == -1 || |
1083 | is->adj.points[i].dy == -1) continue; |
1084 | |
1085 | is_join = island_find_connection(is, i); |
1086 | if (!is_join) continue; |
1087 | |
1088 | d2 = DINDEX(is_join->x, is_join->y); |
1089 | if (dsf_canonify(dsf,d1) == dsf_canonify(dsf,d2)) { |
1090 | ; /* we have a loop. See comment in map_hasloops. */ |
1091 | /* However, we still want to merge all squares joining |
1092 | * this side-that-makes-a-loop. */ |
1093 | } |
1094 | /* merge all squares between island 1 and island 2. */ |
1095 | for (x2 = x; x2 <= is_join->x; x2++) { |
1096 | for (y2 = y; y2 <= is_join->y; y2++) { |
1097 | d2 = DINDEX(x2,y2); |
1098 | if (d1 != d2) dsf_merge(dsf,d1,d2); |
1099 | } |
1100 | } |
1101 | } |
1102 | } |
1103 | } |
1104 | } |
1105 | |
1106 | static int map_group_check(game_state *state, int canon, int warn, |
1107 | int *nislands_r) |
1108 | { |
1109 | int *dsf = state->solver->dsf, nislands = 0; |
1110 | int x, y, i, allfull = 1; |
1111 | struct island *is; |
1112 | |
1113 | for (i = 0; i < state->n_islands; i++) { |
1114 | is = &state->islands[i]; |
1115 | if (dsf_canonify(dsf, DINDEX(is->x,is->y)) != canon) continue; |
1116 | |
1117 | GRID(state, is->x, is->y) |= G_SWEEP; |
1118 | nislands++; |
1119 | if (island_countbridges(is) != is->count) |
1120 | allfull = 0; |
1121 | } |
1122 | if (warn && allfull && nislands != state->n_islands) { |
1123 | /* we're full and this island group isn't the whole set. |
1124 | * Mark all squares with this dsf canon as ERR. */ |
1125 | for (x = 0; x < state->w; x++) { |
1126 | for (y = 0; y < state->h; y++) { |
1127 | if (dsf_canonify(dsf, DINDEX(x,y)) == canon) { |
1128 | GRID(state,x,y) |= G_WARN; |
1129 | } |
1130 | } |
1131 | } |
1132 | |
1133 | } |
1134 | if (nislands_r) *nislands_r = nislands; |
1135 | return allfull; |
1136 | } |
1137 | |
1138 | static int map_group_full(game_state *state, int *ngroups_r) |
1139 | { |
1140 | int *dsf = state->solver->dsf, ngroups = 0; |
1141 | int i, anyfull = 0; |
1142 | struct island *is; |
1143 | |
1144 | /* NB this assumes map_group (or sth else) has cleared G_SWEEP. */ |
1145 | |
1146 | for (i = 0; i < state->n_islands; i++) { |
1147 | is = &state->islands[i]; |
1148 | if (GRID(state,is->x,is->y) & G_SWEEP) continue; |
1149 | |
1150 | ngroups++; |
1151 | if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)), |
1152 | 1, NULL)) |
1153 | anyfull = 1; |
1154 | } |
1155 | |
1156 | *ngroups_r = ngroups; |
1157 | return anyfull; |
1158 | } |
1159 | |
1160 | static int map_check(game_state *state) |
1161 | { |
1162 | int ngroups; |
1163 | |
1164 | /* Check for loops, if necessary. */ |
1165 | if (!state->allowloops) { |
1166 | if (map_hasloops(state, 1)) |
1167 | return 0; |
1168 | } |
1169 | |
1170 | /* Place islands into island groups and check for early |
1171 | * satisfied-groups. */ |
1172 | map_group(state); /* clears WARN and SWEEP */ |
1173 | if (map_group_full(state, &ngroups)) { |
1174 | if (ngroups == 1) return 1; |
1175 | } |
1176 | return 0; |
1177 | } |
1178 | |
1179 | static void map_clear(game_state *state) |
1180 | { |
1181 | int x, y; |
1182 | |
1183 | for (x = 0; x < state->w; x++) { |
1184 | for (y = 0; y < state->h; y++) { |
1185 | /* clear most flags; might want to be slightly more careful here. */ |
1186 | GRID(state,x,y) &= G_ISLAND; |
1187 | } |
1188 | } |
1189 | } |
1190 | |
1191 | static void solve_join(struct island *is, int direction, int n, int is_max) |
1192 | { |
1193 | struct island *is_orth; |
1194 | int d1, d2, *dsf = is->state->solver->dsf; |
1195 | game_state *state = is->state; /* for DINDEX */ |
1196 | |
1197 | is_orth = INDEX(is->state, gridi, |
1198 | ISLAND_ORTHX(is, direction), |
1199 | ISLAND_ORTHY(is, direction)); |
1200 | assert(is_orth); |
1201 | /*debug(("...joining (%d,%d) to (%d,%d) with %d bridge(s).\n", |
1202 | is->x, is->y, is_orth->x, is_orth->y, n));*/ |
1203 | island_join(is, is_orth, n, is_max); |
1204 | |
1205 | if (n > 0 && !is_max) { |
1206 | d1 = DINDEX(is->x, is->y); |
1207 | d2 = DINDEX(is_orth->x, is_orth->y); |
1208 | if (dsf_canonify(dsf, d1) != dsf_canonify(dsf, d2)) |
1209 | dsf_merge(dsf, d1, d2); |
1210 | } |
1211 | } |
1212 | |
1213 | static int solve_fillone(struct island *is) |
1214 | { |
1215 | int i, nadded = 0; |
1216 | |
1217 | debug(("solve_fillone for island (%d,%d).\n", is->x, is->y)); |
1218 | |
1219 | for (i = 0; i < is->adj.npoints; i++) { |
1220 | if (island_isadj(is, i)) { |
1221 | if (island_hasbridge(is, i)) { |
1222 | /* already attached; do nothing. */; |
1223 | } else { |
1224 | solve_join(is, i, 1, 0); |
1225 | nadded++; |
1226 | } |
1227 | } |
1228 | } |
1229 | return nadded; |
1230 | } |
1231 | |
1232 | static int solve_fill(struct island *is) |
1233 | { |
1234 | /* for each unmarked adjacent, make sure we convert every possible bridge |
1235 | * to a real one, and then work out the possibles afresh. */ |
1236 | int i, nnew, ncurr, nadded = 0, missing; |
1237 | |
1238 | debug(("solve_fill for island (%d,%d).\n", is->x, is->y)); |
1239 | |
1240 | missing = is->count - island_countbridges(is); |
1241 | if (missing < 0) return 0; |
1242 | |
1243 | /* very like island_countspaces. */ |
1244 | for (i = 0; i < is->adj.npoints; i++) { |
1245 | nnew = island_adjspace(is, 1, missing, i); |
1246 | if (nnew) { |
1247 | ncurr = GRIDCOUNT(is->state, |
1248 | is->adj.points[i].x, is->adj.points[i].y, |
1249 | is->adj.points[i].dx ? G_LINEH : G_LINEV); |
1250 | |
1251 | solve_join(is, i, nnew + ncurr, 0); |
1252 | nadded += nnew; |
1253 | } |
1254 | } |
1255 | return nadded; |
1256 | } |
1257 | |
1258 | static int solve_island_stage1(struct island *is, int *didsth_r) |
1259 | { |
1260 | int bridges = island_countbridges(is); |
1261 | int nspaces = island_countspaces(is, 1); |
1262 | int nadj = island_countadj(is); |
1263 | int didsth = 0; |
1264 | |
1265 | assert(didsth_r); |
1266 | |
1267 | /*debug(("island at (%d,%d) filled %d/%d (%d spc) nadj %d\n", |
1268 | is->x, is->y, bridges, is->count, nspaces, nadj));*/ |
1269 | if (bridges > is->count) { |
1270 | /* We only ever add bridges when we're sure they fit, or that's |
1271 | * the only place they can go. If we've added bridges such that |
1272 | * another island has become wrong, the puzzle must not have had |
1273 | * a solution. */ |
1274 | debug(("...island at (%d,%d) is overpopulated!\n", is->x, is->y)); |
1275 | return 0; |
1276 | } else if (bridges == is->count) { |
1277 | /* This island is full. Make sure it's marked (and update |
1278 | * possibles if we did). */ |
1279 | if (!(GRID(is->state, is->x, is->y) & G_MARK)) { |
1280 | debug(("...marking island (%d,%d) as full.\n", is->x, is->y)); |
1281 | island_togglemark(is); |
1282 | didsth = 1; |
1283 | } |
1284 | } else if (GRID(is->state, is->x, is->y) & G_MARK) { |
1285 | debug(("...island (%d,%d) is marked but unfinished!\n", |
1286 | is->x, is->y)); |
1287 | return 0; /* island has been marked unfinished; no solution from here. */ |
1288 | } else { |
1289 | /* This is the interesting bit; we try and fill in more information |
1290 | * about this island. */ |
1291 | if (is->count == bridges + nspaces) { |
1292 | if (solve_fill(is) > 0) didsth = 1; |
1293 | } else if (is->count > ((nadj-1) * is->state->maxb)) { |
1294 | /* must have at least one bridge in each possible direction. */ |
1295 | if (solve_fillone(is) > 0) didsth = 1; |
1296 | } |
1297 | } |
1298 | if (didsth) { |
1299 | map_update_possibles(is->state); |
1300 | *didsth_r = 1; |
1301 | } |
1302 | return 1; |
1303 | } |
1304 | |
1305 | /* returns non-zero if a new line here would cause a loop. */ |
1306 | static int solve_island_checkloop(struct island *is, int direction) |
1307 | { |
1308 | struct island *is_orth; |
1309 | int *dsf = is->state->solver->dsf, d1, d2; |
1310 | game_state *state = is->state; |
1311 | |
1312 | if (is->state->allowloops) return 0; /* don't care anyway */ |
1313 | if (island_hasbridge(is, direction)) return 0; /* already has a bridge */ |
1314 | if (island_isadj(is, direction) == 0) return 0; /* no adj island */ |
1315 | |
1316 | is_orth = INDEX(is->state, gridi, |
1317 | ISLAND_ORTHX(is,direction), |
1318 | ISLAND_ORTHY(is,direction)); |
1319 | if (!is_orth) return 0; |
1320 | |
1321 | d1 = DINDEX(is->x, is->y); |
1322 | d2 = DINDEX(is_orth->x, is_orth->y); |
1323 | if (dsf_canonify(dsf, d1) == dsf_canonify(dsf, d2)) { |
1324 | /* two islands are connected already; don't join them. */ |
1325 | return 1; |
1326 | } |
1327 | return 0; |
1328 | } |
1329 | |
1330 | static int solve_island_stage2(struct island *is, int *didsth_r) |
1331 | { |
1332 | int added = 0, removed = 0, navail = 0, nadj, i; |
1333 | |
1334 | assert(didsth_r); |
1335 | |
1336 | for (i = 0; i < is->adj.npoints; i++) { |
1337 | if (solve_island_checkloop(is, i)) { |
1338 | debug(("removing possible loop at (%d,%d) direction %d.\n", |
1339 | is->x, is->y, i)); |
1340 | solve_join(is, i, -1, 0); |
1341 | map_update_possibles(is->state); |
1342 | removed = 1; |
1343 | } else { |
1344 | navail += island_isadj(is, i); |
1345 | /*debug(("stage2: navail for (%d,%d) direction (%d,%d) is %d.\n", |
1346 | is->x, is->y, |
1347 | is->adj.points[i].dx, is->adj.points[i].dy, |
1348 | island_isadj(is, i)));*/ |
1349 | } |
1350 | } |
1351 | |
1352 | /*debug(("island at (%d,%d) navail %d: checking...\n", is->x, is->y, navail));*/ |
1353 | |
1354 | for (i = 0; i < is->adj.npoints; i++) { |
1355 | if (!island_hasbridge(is, i)) { |
1356 | nadj = island_isadj(is, i); |
1357 | if (nadj > 0 && (navail - nadj) < is->count) { |
1358 | /* we couldn't now complete the island without at |
1359 | * least one bridge here; put it in. */ |
1360 | /*debug(("nadj %d, navail %d, is->count %d.\n", |
1361 | nadj, navail, is->count));*/ |
1362 | debug(("island at (%d,%d) direction (%d,%d) must have 1 bridge\n", |
1363 | is->x, is->y, |
1364 | is->adj.points[i].dx, is->adj.points[i].dy)); |
1365 | solve_join(is, i, 1, 0); |
1366 | added = 1; |
1367 | /*debug_state(is->state); |
1368 | debug_possibles(is->state);*/ |
1369 | } |
1370 | } |
1371 | } |
1372 | if (added) map_update_possibles(is->state); |
1373 | if (added || removed) *didsth_r = 1; |
1374 | return 1; |
1375 | } |
1376 | |
1377 | static int solve_island_subgroup(struct island *is, int direction, int n) |
1378 | { |
1379 | struct island *is_join; |
1380 | int nislands, *dsf = is->state->solver->dsf; |
1381 | game_state *state = is->state; |
1382 | |
1383 | debug(("..checking subgroups.\n")); |
1384 | |
1385 | /* if is isn't full, return 0. */ |
1386 | if (n < is->count) { |
1387 | debug(("...orig island (%d,%d) not full.\n", is->x, is->y)); |
1388 | return 0; |
1389 | } |
1390 | |
1391 | is_join = INDEX(state, gridi, |
1392 | ISLAND_ORTHX(is, direction), |
1393 | ISLAND_ORTHY(is, direction)); |
1394 | assert(is_join); |
1395 | |
1396 | /* if is_join isn't full, return 0. */ |
1397 | if (island_countbridges(is_join) < is_join->count) { |
1398 | debug(("...dest island (%d,%d) not full.\n", is_join->x, is_join->y)); |
1399 | return 0; |
1400 | } |
1401 | |
1402 | /* Check group membership for is->dsf; if it's full return 1. */ |
1403 | if (map_group_check(state, dsf_canonify(dsf, DINDEX(is->x,is->y)), |
1404 | 0, &nislands)) { |
1405 | if (nislands < state->n_islands) { |
1406 | /* we have a full subgroup that isn't the whole set. |
1407 | * This isn't allowed. */ |
1408 | debug(("island at (%d,%d) makes full subgroup, disallowing.\n", |
1409 | is->x, is->y, n)); |
1410 | return 1; |
1411 | } else { |
1412 | debug(("...has finished puzzle.\n")); |
1413 | } |
1414 | } |
1415 | return 0; |
1416 | } |
1417 | |
1418 | static int solve_island_impossible(game_state *state) |
1419 | { |
1420 | struct island *is; |
1421 | int i; |
1422 | |
1423 | /* If any islands are impossible, return 1. */ |
1424 | for (i = 0; i < state->n_islands; i++) { |
1425 | is = &state->islands[i]; |
1426 | if (island_impossible(is, 0)) { |
1427 | debug(("island at (%d,%d) has become impossible, disallowing.\n", |
1428 | is->x, is->y)); |
1429 | return 1; |
1430 | } |
1431 | } |
1432 | return 0; |
1433 | } |
1434 | |
1435 | /* Bear in mind that this function is really rather inefficient. */ |
1436 | static int solve_island_stage3(struct island *is, int *didsth_r) |
1437 | { |
1438 | int i, n, x, y, missing, spc, curr, maxb, didsth = 0; |
1439 | int wh = is->state->w * is->state->h; |
1440 | struct solver_state *ss = is->state->solver; |
1441 | |
1442 | assert(didsth_r); |
1443 | |
1444 | missing = is->count - island_countbridges(is); |
1445 | if (missing <= 0) return 1; |
1446 | |
1447 | for (i = 0; i < is->adj.npoints; i++) { |
1448 | /* We only do right- or down-pointing bridges. */ |
1449 | if (is->adj.points[i].dx == -1 || |
1450 | is->adj.points[i].dy == -1) continue; |
1451 | |
1452 | x = is->adj.points[i].x; |
1453 | y = is->adj.points[i].y; |
1454 | spc = island_adjspace(is, 1, missing, i); |
1455 | if (spc == 0) continue; |
1456 | |
1457 | curr = GRIDCOUNT(is->state, x, y, |
1458 | is->adj.points[i].dx ? G_LINEH : G_LINEV); |
1459 | debug(("island at (%d,%d) s3, trying %d - %d bridges.\n", |
1460 | is->x, is->y, curr+1, curr+spc)); |
1461 | |
1462 | /* Now we know that this island could have more bridges, |
1463 | * to bring the total from curr+1 to curr+spc. */ |
1464 | maxb = -1; |
1465 | /* We have to squirrel the dsf away and restore it afterwards; |
1466 | * it is additive only, and can't be removed from. */ |
1467 | memcpy(ss->tmpdsf, ss->dsf, wh*sizeof(int)); |
1468 | for (n = curr+1; n <= curr+spc; n++) { |
1469 | solve_join(is, i, n, 0); |
1470 | map_update_possibles(is->state); |
1471 | |
1472 | if (solve_island_subgroup(is, i, n) || |
1473 | solve_island_impossible(is->state)) { |
1474 | maxb = n-1; |
1475 | debug(("island at (%d,%d) d(%d,%d) new max of %d bridges:\n", |
1476 | is->x, is->y, |
1477 | is->adj.points[i].dx, is->adj.points[i].dy, |
1478 | maxb)); |
1479 | break; |
1480 | } |
1481 | } |
1482 | solve_join(is, i, curr, 0); /* put back to before. */ |
1483 | memcpy(ss->dsf, ss->tmpdsf, wh*sizeof(int)); |
1484 | |
1485 | if (maxb != -1) { |
1486 | /*debug_state(is->state);*/ |
1487 | if (maxb == 0) { |
1488 | debug(("...adding NOLINE.\n")); |
1489 | solve_join(is, i, -1, 0); /* we can't have any bridges here. */ |
1490 | didsth = 1; |
1491 | } else { |
1492 | debug(("...setting maximum\n")); |
1493 | solve_join(is, i, maxb, 1); |
1494 | } |
1495 | } |
1496 | map_update_possibles(is->state); |
1497 | } |
1498 | if (didsth) *didsth_r = didsth; |
1499 | return 1; |
1500 | } |
1501 | |
1502 | #define CONTINUE_IF_FULL do { \ |
1503 | if (GRID(state, is->x, is->y) & G_MARK) { \ |
1504 | /* island full, don't try fixing it */ \ |
1505 | continue; \ |
1506 | } } while(0) |
1507 | |
1508 | static int solve_sub(game_state *state, int difficulty, int depth) |
1509 | { |
1510 | struct island *is; |
1511 | int i, didsth; |
1512 | |
1513 | while (1) { |
1514 | didsth = 0; |
1515 | |
1516 | /* First island iteration: things we can work out by looking at |
1517 | * properties of the island as a whole. */ |
1518 | for (i = 0; i < state->n_islands; i++) { |
1519 | is = &state->islands[i]; |
1520 | if (!solve_island_stage1(is, &didsth)) return 0; |
1521 | } |
1522 | if (didsth) continue; |
1523 | else if (difficulty < 1) break; |
1524 | |
1525 | /* Second island iteration: thing we can work out by looking at |
1526 | * properties of individual island connections. */ |
1527 | for (i = 0; i < state->n_islands; i++) { |
1528 | is = &state->islands[i]; |
1529 | CONTINUE_IF_FULL; |
1530 | if (!solve_island_stage2(is, &didsth)) return 0; |
1531 | } |
1532 | if (didsth) continue; |
1533 | else if (difficulty < 2) break; |
1534 | |
1535 | /* Third island iteration: things we can only work out by looking |
1536 | * at groups of islands. */ |
1537 | for (i = 0; i < state->n_islands; i++) { |
1538 | is = &state->islands[i]; |
1539 | if (!solve_island_stage3(is, &didsth)) return 0; |
1540 | } |
1541 | if (didsth) continue; |
1542 | else if (difficulty < 3) break; |
1543 | |
1544 | /* If we can be bothered, write a recursive solver to finish here. */ |
1545 | break; |
1546 | } |
1547 | if (map_check(state)) return 1; /* solved it */ |
1548 | return 0; |
1549 | } |
1550 | |
1551 | static void solve_for_hint(game_state *state) |
1552 | { |
1553 | map_group(state); |
1554 | solve_sub(state, 10, 0); |
1555 | } |
1556 | |
1557 | static int solve_from_scratch(game_state *state, int difficulty) |
1558 | { |
1559 | map_clear(state); |
1560 | map_group(state); |
1561 | map_update_possibles(state); |
1562 | return solve_sub(state, difficulty, 0); |
1563 | } |
1564 | |
1565 | /* --- New game functions --- */ |
1566 | |
1567 | static game_state *new_state(game_params *params) |
1568 | { |
1569 | game_state *ret = snew(game_state); |
1570 | int wh = params->w * params->h, i; |
1571 | |
1572 | ret->w = params->w; |
1573 | ret->h = params->h; |
1574 | ret->allowloops = params->allowloops; |
1575 | ret->maxb = params->maxb; |
1576 | ret->params = *params; |
1577 | |
1578 | ret->grid = snewn(wh, grid_type); |
1579 | memset(ret->grid, 0, GRIDSZ(ret)); |
1580 | ret->scratch = snewn(wh, grid_type); |
1581 | memset(ret->scratch, 0, GRIDSZ(ret)); |
1582 | |
1583 | ret->wha = snewn(wh*N_WH_ARRAYS, char); |
1584 | memset(ret->wha, 0, wh*N_WH_ARRAYS*sizeof(char)); |
1585 | |
1586 | ret->possv = ret->wha; |
1587 | ret->possh = ret->wha + wh; |
1588 | ret->lines = ret->wha + wh*2; |
1589 | ret->maxv = ret->wha + wh*3; |
1590 | ret->maxh = ret->wha + wh*4; |
1591 | |
1592 | memset(ret->maxv, ret->maxb, wh*sizeof(char)); |
1593 | memset(ret->maxh, ret->maxb, wh*sizeof(char)); |
1594 | |
1595 | ret->islands = NULL; |
1596 | ret->n_islands = 0; |
1597 | ret->n_islands_alloc = 0; |
1598 | |
1599 | ret->gridi = snewn(wh, struct island *); |
1600 | for (i = 0; i < wh; i++) ret->gridi[i] = NULL; |
1601 | |
1602 | ret->solved = ret->completed = 0; |
1603 | |
1604 | ret->solver = snew(struct solver_state); |
1605 | ret->solver->dsf = snewn(wh, int); |
1606 | ret->solver->tmpdsf = snewn(wh, int); |
1607 | for (i = 0; i < wh; i++) ret->solver->dsf[i] = i; |
1608 | |
1609 | ret->solver->refcount = 1; |
1610 | |
1611 | return ret; |
1612 | } |
1613 | |
1614 | static game_state *dup_game(game_state *state) |
1615 | { |
1616 | game_state *ret = snew(game_state); |
1617 | int wh = state->w*state->h; |
1618 | |
1619 | ret->w = state->w; |
1620 | ret->h = state->h; |
1621 | ret->allowloops = state->allowloops; |
1622 | ret->maxb = state->maxb; |
1623 | ret->params = state->params; |
1624 | |
1625 | ret->grid = snewn(wh, grid_type); |
1626 | memcpy(ret->grid, state->grid, GRIDSZ(ret)); |
1627 | ret->scratch = snewn(wh, grid_type); |
1628 | memcpy(ret->scratch, state->scratch, GRIDSZ(ret)); |
1629 | |
1630 | ret->wha = snewn(wh*N_WH_ARRAYS, char); |
1631 | memcpy(ret->wha, state->wha, wh*N_WH_ARRAYS*sizeof(char)); |
1632 | |
1633 | ret->possv = ret->wha; |
1634 | ret->possh = ret->wha + wh; |
1635 | ret->lines = ret->wha + wh*2; |
1636 | ret->maxv = ret->wha + wh*3; |
1637 | ret->maxh = ret->wha + wh*4; |
1638 | |
1639 | ret->islands = snewn(state->n_islands, struct island); |
1640 | memcpy(ret->islands, state->islands, state->n_islands * sizeof(struct island)); |
1641 | ret->n_islands = ret->n_islands_alloc = state->n_islands; |
1642 | |
1643 | ret->gridi = snewn(wh, struct island *); |
1644 | fixup_islands_for_realloc(ret); |
1645 | |
1646 | ret->solved = state->solved; |
1647 | ret->completed = state->completed; |
1648 | |
1649 | ret->solver = state->solver; |
1650 | ret->solver->refcount++; |
1651 | |
1652 | return ret; |
1653 | } |
1654 | |
1655 | static void free_game(game_state *state) |
1656 | { |
1657 | if (--state->solver->refcount <= 0) { |
1658 | sfree(state->solver->dsf); |
1659 | sfree(state->solver->tmpdsf); |
1660 | sfree(state->solver); |
1661 | } |
1662 | |
1663 | sfree(state->islands); |
1664 | sfree(state->gridi); |
1665 | |
1666 | sfree(state->wha); |
1667 | |
1668 | sfree(state->scratch); |
1669 | sfree(state->grid); |
1670 | sfree(state); |
1671 | } |
1672 | |
1673 | #define MAX_NEWISLAND_TRIES 50 |
1674 | |
1675 | #define ORDER(a,b) do { if (a < b) { int tmp=a; int a=b; int b=tmp; } } while(0) |
1676 | |
1677 | static char *new_game_desc(game_params *params, random_state *rs, |
1678 | char **aux, int interactive) |
1679 | { |
1680 | game_state *tobuild = NULL; |
1681 | int i, j, wh = params->w * params->h, x, y, dx, dy; |
1682 | int minx, miny, maxx, maxy, joinx, joiny, newx, newy, diffx, diffy; |
1683 | int ni_req = max((params->islands * wh) / 100, 2), ni_curr, ni_bad; |
1684 | struct island *is, *is2; |
1685 | char *ret; |
1686 | unsigned int echeck; |
1687 | |
1688 | /* pick a first island position randomly. */ |
1689 | generate: |
1690 | if (tobuild) free_game(tobuild); |
1691 | tobuild = new_state(params); |
1692 | |
1693 | x = random_upto(rs, params->w); |
1694 | y = random_upto(rs, params->h); |
1695 | island_add(tobuild, x, y, 0); |
1696 | ni_curr = 1; |
1697 | ni_bad = 0; |
1698 | debug(("Created initial island at (%d,%d).\n", x, y)); |
1699 | |
1700 | while (ni_curr < ni_req) { |
1701 | /* Pick a random island to try and extend from. */ |
1702 | i = random_upto(rs, tobuild->n_islands); |
1703 | is = &tobuild->islands[i]; |
1704 | |
1705 | /* Pick a random direction to extend in. */ |
1706 | j = random_upto(rs, is->adj.npoints); |
1707 | dx = is->adj.points[j].x - is->x; |
1708 | dy = is->adj.points[j].y - is->y; |
1709 | |
1710 | /* Find out limits of where we could put a new island. */ |
1711 | joinx = joiny = -1; |
1712 | minx = is->x + 2*dx; miny = is->y + 2*dy; /* closest is 2 units away. */ |
1713 | x = is->x+dx; y = is->y+dy; |
1714 | if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) { |
1715 | /* already a line next to the island, continue. */ |
1716 | goto bad; |
1717 | } |
1718 | while (1) { |
1719 | if (x < 0 || x >= params->w || y < 0 || y >= params->h) { |
1720 | /* got past the edge; put a possible at the island |
1721 | * and exit. */ |
1722 | maxx = x-dx; maxy = y-dy; |
1723 | goto foundmax; |
1724 | } |
1725 | if (GRID(tobuild,x,y) & G_ISLAND) { |
1726 | /* could join up to an existing island... */ |
1727 | joinx = x; joiny = y; |
1728 | /* ... or make a new one 2 spaces away. */ |
1729 | maxx = x - 2*dx; maxy = y - 2*dy; |
1730 | goto foundmax; |
1731 | } else if (GRID(tobuild,x,y) & (G_LINEV|G_LINEH)) { |
1732 | /* could make a new one 1 space away from the line. */ |
1733 | maxx = x - dx; maxy = y - dy; |
1734 | goto foundmax; |
1735 | } |
1736 | x += dx; y += dy; |
1737 | } |
1738 | |
1739 | foundmax: |
1740 | debug(("Island at (%d,%d) with d(%d,%d) has new positions " |
1741 | "(%d,%d) -> (%d,%d), join (%d,%d).\n", |
1742 | is->x, is->y, dx, dy, minx, miny, maxx, maxy, joinx, joiny)); |
1743 | /* Now we know where we could either put a new island |
1744 | * (between min and max), or (if loops are allowed) could join on |
1745 | * to an existing island (at join). */ |
1746 | if (params->allowloops && joinx != -1 && joiny != -1) { |
1747 | if (random_upto(rs, 100) < (unsigned long)params->expansion) { |
1748 | is2 = INDEX(tobuild, gridi, joinx, joiny); |
1749 | debug(("Joining island at (%d,%d) to (%d,%d).\n", |
1750 | is->x, is->y, is2->x, is2->y)); |
1751 | goto join; |
1752 | } |
1753 | } |
1754 | diffx = (maxx - minx) * dx; |
1755 | diffy = (maxy - miny) * dy; |
1756 | if (diffx < 0 || diffy < 0) goto bad; |
1757 | if (random_upto(rs,100) < (unsigned long)params->expansion) { |
1758 | newx = maxx; newy = maxy; |
1759 | debug(("Creating new island at (%d,%d) (expanded).\n", newx, newy)); |
1760 | } else { |
1761 | newx = minx + random_upto(rs,diffx+1)*dx; |
1762 | newy = miny + random_upto(rs,diffy+1)*dy; |
1763 | debug(("Creating new island at (%d,%d).\n", newx, newy)); |
1764 | } |
1765 | /* check we're not next to island in the other orthogonal direction. */ |
1766 | if ((INGRID(tobuild,newx+dy,newy+dx) && (GRID(tobuild,newx+dy,newy+dx) & G_ISLAND)) || |
1767 | (INGRID(tobuild,newx-dy,newy-dx) && (GRID(tobuild,newx-dy,newy-dx) & G_ISLAND))) { |
1768 | debug(("New location is adjacent to island, skipping.\n")); |
1769 | goto bad; |
1770 | } |
1771 | is2 = island_add(tobuild, newx, newy, 0); |
1772 | /* Must get is again at this point; the array might have |
1773 | * been realloced by island_add... */ |
1774 | is = &tobuild->islands[i]; /* ...but order will not change. */ |
1775 | |
1776 | ni_curr++; ni_bad = 0; |
1777 | join: |
1778 | island_join(is, is2, random_upto(rs, tobuild->maxb)+1, 0); |
1779 | debug_state(tobuild); |
1780 | continue; |
1781 | |
1782 | bad: |
1783 | ni_bad++; |
1784 | if (ni_bad > MAX_NEWISLAND_TRIES) { |
1785 | debug(("Unable to create any new islands after %d tries; " |
1786 | "created %d [%d%%] (instead of %d [%d%%] requested).\n", |
1787 | MAX_NEWISLAND_TRIES, |
1788 | ni_curr, ni_curr * 100 / wh, |
1789 | ni_req, ni_req * 100 / wh)); |
1790 | goto generated; |
1791 | } |
1792 | } |
1793 | |
1794 | generated: |
1795 | if (ni_curr == 1) { |
1796 | debug(("Only generated one island (!), retrying.\n")); |
1797 | goto generate; |
1798 | } |
1799 | /* Check we have at least one island on each extremity of the grid. */ |
1800 | echeck = 0; |
1801 | for (x = 0; x < params->w; x++) { |
1802 | if (INDEX(tobuild, gridi, x, 0)) echeck |= 1; |
50082dba |
1803 | if (INDEX(tobuild, gridi, x, params->h-1)) echeck |= 2; |
e7c63b02 |
1804 | } |
1805 | for (y = 0; y < params->h; y++) { |
1806 | if (INDEX(tobuild, gridi, 0, y)) echeck |= 4; |
50082dba |
1807 | if (INDEX(tobuild, gridi, params->w-1, y)) echeck |= 8; |
e7c63b02 |
1808 | } |
1809 | if (echeck != 15) { |
1810 | debug(("Generated grid doesn't fill to sides, retrying.\n")); |
1811 | goto generate; |
1812 | } |
1813 | |
1814 | map_count(tobuild); |
1815 | map_find_orthogonal(tobuild); |
1816 | |
1817 | if (params->difficulty > 0) { |
1818 | if (solve_from_scratch(tobuild, params->difficulty-1) > 0) { |
1819 | debug(("Grid is solvable at difficulty %d (too easy); retrying.\n", |
1820 | params->difficulty-1)); |
1821 | goto generate; |
1822 | } |
1823 | } |
1824 | |
1825 | if (solve_from_scratch(tobuild, params->difficulty) == 0) { |
1826 | debug(("Grid not solvable at difficulty %d, (too hard); retrying.\n", |
1827 | params->difficulty)); |
1828 | goto generate; |
1829 | } |
1830 | |
1831 | /* ... tobuild is now solved. We rely on this making the diff for aux. */ |
1832 | debug_state(tobuild); |
1833 | ret = encode_game(tobuild); |
1834 | { |
1835 | game_state *clean = dup_game(tobuild); |
1836 | map_clear(clean); |
1837 | map_update_possibles(clean); |
1838 | *aux = game_state_diff(clean, tobuild); |
1839 | free_game(clean); |
1840 | } |
1841 | free_game(tobuild); |
1842 | |
1843 | return ret; |
1844 | } |
1845 | |
1846 | static char *validate_desc(game_params *params, char *desc) |
1847 | { |
1848 | int i, wh = params->w * params->h; |
1849 | |
1850 | for (i = 0; i < wh; i++) { |
1851 | if (*desc >= '1' && *desc <= '9') |
1852 | /* OK */; |
1853 | else if (*desc >= 'a' && *desc <= 'z') |
1854 | i += *desc - 'a'; /* plus the i++ */ |
1855 | else if (*desc >= 'A' && *desc <= 'G') |
1856 | /* OK */; |
1857 | else if (*desc == 'V' || *desc == 'W' || |
1858 | *desc == 'X' || *desc == 'Y' || |
1859 | *desc == 'H' || *desc == 'I' || |
1860 | *desc == 'J' || *desc == 'K') |
1861 | /* OK */; |
1862 | else if (!*desc) |
1863 | return "Game description shorter than expected"; |
1864 | else |
1865 | return "Game description containers unexpected character"; |
1866 | desc++; |
1867 | } |
1868 | if (*desc || i > wh) |
1869 | return "Game description longer than expected"; |
1870 | |
1871 | return NULL; |
1872 | } |
1873 | |
1874 | static game_state *new_game_sub(game_params *params, char *desc) |
1875 | { |
1876 | game_state *state = new_state(params); |
1877 | int x, y, run = 0; |
1878 | |
1879 | debug(("new_game[_sub]: desc = '%s'.\n", desc)); |
1880 | |
1881 | for (y = 0; y < params->h; y++) { |
1882 | for (x = 0; x < params->w; x++) { |
1883 | char c = '\0'; |
1884 | |
1885 | if (run == 0) { |
1886 | c = *desc++; |
1887 | assert(c != 'S'); |
1888 | if (c >= 'a' && c <= 'z') |
1889 | run = c - 'a' + 1; |
1890 | } |
1891 | |
1892 | if (run > 0) { |
1893 | c = 'S'; |
1894 | run--; |
1895 | } |
1896 | |
1897 | switch (c) { |
1898 | case '1': case '2': case '3': case '4': |
1899 | case '5': case '6': case '7': case '8': case '9': |
1900 | island_add(state, x, y, (c - '0')); |
1901 | break; |
1902 | |
1903 | case 'A': case 'B': case 'C': case 'D': |
1904 | case 'E': case 'F': case 'G': |
1905 | island_add(state, x, y, (c - 'A') + 10); |
1906 | break; |
1907 | |
1908 | case 'S': |
1909 | /* empty square */ |
1910 | break; |
1911 | |
1912 | default: |
1913 | assert(!"Malformed desc."); |
1914 | break; |
1915 | } |
1916 | } |
1917 | } |
1918 | if (*desc) assert(!"Over-long desc."); |
1919 | |
1920 | map_find_orthogonal(state); |
1921 | map_update_possibles(state); |
1922 | |
1923 | return state; |
1924 | } |
1925 | |
1926 | static game_state *new_game(midend *me, game_params *params, char *desc) |
1927 | { |
1928 | return new_game_sub(params, desc); |
1929 | } |
1930 | |
1931 | struct game_ui { |
1932 | int dragx_src, dragy_src; /* source; -1 means no drag */ |
1933 | int dragx_dst, dragy_dst; /* src's closest orth island. */ |
1934 | grid_type todraw; |
1935 | int dragging, drag_is_noline, nlines; |
1936 | }; |
1937 | |
1938 | static char *ui_cancel_drag(game_ui *ui) |
1939 | { |
1940 | ui->dragx_src = ui->dragy_src = -1; |
1941 | ui->dragx_dst = ui->dragy_dst = -1; |
1942 | ui->dragging = 0; |
1943 | return ""; |
1944 | } |
1945 | |
1946 | static game_ui *new_ui(game_state *state) |
1947 | { |
1948 | game_ui *ui = snew(game_ui); |
1949 | ui_cancel_drag(ui); |
1950 | return ui; |
1951 | } |
1952 | |
1953 | static void free_ui(game_ui *ui) |
1954 | { |
1955 | sfree(ui); |
1956 | } |
1957 | |
1958 | static char *encode_ui(game_ui *ui) |
1959 | { |
1960 | return NULL; |
1961 | } |
1962 | |
1963 | static void decode_ui(game_ui *ui, char *encoding) |
1964 | { |
1965 | } |
1966 | |
1967 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
1968 | game_state *newstate) |
1969 | { |
1970 | } |
1971 | |
1972 | struct game_drawstate { |
1973 | int tilesize; |
1974 | int w, h; |
1975 | grid_type *grid; |
1976 | int *lv, *lh; |
1977 | int started, dragging; |
1978 | }; |
1979 | |
1980 | static char *update_drag_dst(game_state *state, game_ui *ui, game_drawstate *ds, |
1981 | int nx, int ny) |
1982 | { |
1983 | int ox, oy, dx, dy, i, currl, maxb; |
1984 | struct island *is; |
1985 | grid_type gtype, ntype, mtype, curr; |
1986 | |
1987 | if (ui->dragx_src == -1 || ui->dragy_src == -1) return NULL; |
1988 | |
1989 | ui->dragx_dst = -1; |
1990 | ui->dragy_dst = -1; |
1991 | |
1992 | /* work out which of the four directions we're closest to... */ |
1993 | ox = COORD(ui->dragx_src) + TILE_SIZE/2; |
1994 | oy = COORD(ui->dragy_src) + TILE_SIZE/2; |
1995 | |
1996 | if (abs(nx-ox) < abs(ny-oy)) { |
1997 | dx = 0; |
1998 | dy = (ny-oy) < 0 ? -1 : 1; |
1999 | gtype = G_LINEV; ntype = G_NOLINEV; mtype = G_MARKV; |
2000 | maxb = INDEX(state, maxv, ui->dragx_src+dx, ui->dragy_src+dy); |
2001 | } else { |
2002 | dy = 0; |
2003 | dx = (nx-ox) < 0 ? -1 : 1; |
2004 | gtype = G_LINEH; ntype = G_NOLINEH; mtype = G_MARKH; |
2005 | maxb = INDEX(state, maxh, ui->dragx_src+dx, ui->dragy_src+dy); |
2006 | } |
2007 | if (ui->drag_is_noline) { |
2008 | ui->todraw = ntype; |
2009 | } else { |
2010 | curr = GRID(state, ui->dragx_src+dx, ui->dragy_src+dy); |
2011 | currl = INDEX(state, lines, ui->dragx_src+dx, ui->dragy_src+dy); |
2012 | |
2013 | if (curr & gtype) { |
2014 | if (currl == maxb) { |
2015 | ui->todraw = 0; |
2016 | ui->nlines = 0; |
2017 | } else { |
2018 | ui->todraw = gtype; |
2019 | ui->nlines = currl + 1; |
2020 | } |
2021 | } else { |
2022 | ui->todraw = gtype; |
2023 | ui->nlines = 1; |
2024 | } |
2025 | } |
2026 | |
2027 | /* ... and see if there's an island off in that direction. */ |
2028 | is = INDEX(state, gridi, ui->dragx_src, ui->dragy_src); |
2029 | for (i = 0; i < is->adj.npoints; i++) { |
2030 | if (is->adj.points[i].off == 0) continue; |
2031 | curr = GRID(state, is->x+dx, is->y+dy); |
2032 | if (curr & mtype) continue; /* don't allow changes to marked lines. */ |
2033 | if (ui->drag_is_noline) { |
2034 | if (curr & gtype) continue; /* no no-line where already a line */ |
2035 | } else { |
2036 | if (POSSIBLES(state, dx, is->x+dx, is->y+dy) == 0) continue; /* no line if !possible. */ |
2037 | if (curr & ntype) continue; /* can't have a bridge where there's a no-line. */ |
2038 | } |
2039 | |
2040 | if (is->adj.points[i].dx == dx && |
2041 | is->adj.points[i].dy == dy) { |
2042 | ui->dragx_dst = ISLAND_ORTHX(is,i); |
2043 | ui->dragy_dst = ISLAND_ORTHY(is,i); |
2044 | } |
2045 | } |
2046 | /*debug(("update_drag src (%d,%d) d(%d,%d) dst (%d,%d)\n", |
2047 | ui->dragx_src, ui->dragy_src, dx, dy, |
2048 | ui->dragx_dst, ui->dragy_dst));*/ |
2049 | return ""; |
2050 | } |
2051 | |
2052 | static char *finish_drag(game_state *state, game_ui *ui) |
2053 | { |
2054 | char buf[80]; |
2055 | |
2056 | if (ui->dragx_src == -1 || ui->dragy_src == -1) |
2057 | return NULL; |
2058 | if (ui->dragx_dst == -1 || ui->dragy_dst == -1) |
2059 | return ui_cancel_drag(ui); |
2060 | |
2061 | if (ui->drag_is_noline) { |
2062 | sprintf(buf, "N%d,%d,%d,%d", |
2063 | ui->dragx_src, ui->dragy_src, |
2064 | ui->dragx_dst, ui->dragy_dst); |
2065 | } else { |
2066 | sprintf(buf, "L%d,%d,%d,%d,%d", |
2067 | ui->dragx_src, ui->dragy_src, |
2068 | ui->dragx_dst, ui->dragy_dst, ui->nlines); |
2069 | } |
2070 | |
2071 | ui_cancel_drag(ui); |
2072 | |
2073 | return dupstr(buf); |
2074 | } |
2075 | |
2076 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
2077 | int x, int y, int button) |
2078 | { |
2079 | int gx = FROMCOORD(x), gy = FROMCOORD(y); |
2080 | char buf[80], *ret; |
2081 | grid_type ggrid = INGRID(state,gx,gy) ? GRID(state,gx,gy) : 0; |
2082 | |
2083 | if (button == LEFT_BUTTON || button == RIGHT_BUTTON) { |
2084 | if (!INGRID(state, gx, gy)) return NULL; |
2085 | if ((ggrid & G_ISLAND) && !(ggrid & G_MARK)) { |
2086 | ui->dragx_src = gx; |
2087 | ui->dragy_src = gy; |
2088 | return ""; |
2089 | } else |
2090 | return ui_cancel_drag(ui); |
2091 | } else if (button == LEFT_DRAG || button == RIGHT_DRAG) { |
2092 | if (gx != ui->dragx_src || gy != ui->dragy_src) { |
2093 | ui->dragging = 1; |
2094 | ui->drag_is_noline = (button == RIGHT_DRAG) ? 1 : 0; |
2095 | return update_drag_dst(state, ui, ds, x, y); |
2096 | } else { |
2097 | /* cancel a drag when we go back to the starting point */ |
2098 | ui->dragx_dst = -1; |
2099 | ui->dragy_dst = -1; |
2100 | return ""; |
2101 | } |
2102 | } else if (button == LEFT_RELEASE || button == RIGHT_RELEASE) { |
2103 | if (ui->dragging) { |
2104 | return finish_drag(state, ui); |
2105 | } else { |
2106 | ui_cancel_drag(ui); |
2107 | if (!INGRID(state, gx, gy)) return NULL; |
2108 | if (!(GRID(state, gx, gy) & G_ISLAND)) return NULL; |
2109 | sprintf(buf, "M%d,%d", gx, gy); |
2110 | return dupstr(buf); |
2111 | } |
2112 | } else if (button == 'h' || button == 'H') { |
2113 | game_state *solved = dup_game(state); |
2114 | solve_for_hint(solved); |
2115 | ret = game_state_diff(state, solved); |
2116 | free_game(solved); |
2117 | return ret; |
2118 | } |
2119 | |
2120 | return NULL; |
2121 | } |
2122 | |
2123 | static game_state *execute_move(game_state *state, char *move) |
2124 | { |
2125 | game_state *ret = dup_game(state); |
2126 | int x1, y1, x2, y2, nl, n; |
2127 | struct island *is1, *is2; |
2128 | char c; |
2129 | |
2130 | debug(("execute_move: %s\n", move)); |
2131 | |
2132 | if (!*move) goto badmove; |
2133 | while (*move) { |
2134 | c = *move++; |
2135 | if (c == 'S') { |
2136 | ret->solved = TRUE; |
2137 | n = 0; |
2138 | } else if (c == 'L') { |
2139 | if (sscanf(move, "%d,%d,%d,%d,%d%n", |
2140 | &x1, &y1, &x2, &y2, &nl, &n) != 5) |
2141 | goto badmove; |
2142 | is1 = INDEX(ret, gridi, x1, y1); |
2143 | is2 = INDEX(ret, gridi, x2, y2); |
2144 | if (!is1 || !is2) goto badmove; |
2145 | if (nl < 0 || nl > state->maxb) goto badmove; |
2146 | island_join(is1, is2, nl, 0); |
2147 | } else if (c == 'N') { |
2148 | if (sscanf(move, "%d,%d,%d,%d%n", |
2149 | &x1, &y1, &x2, &y2, &n) != 4) |
2150 | goto badmove; |
2151 | is1 = INDEX(ret, gridi, x1, y1); |
2152 | is2 = INDEX(ret, gridi, x2, y2); |
2153 | if (!is1 || !is2) goto badmove; |
2154 | island_join(is1, is2, -1, 0); |
2155 | } else if (c == 'M') { |
2156 | if (sscanf(move, "%d,%d%n", |
2157 | &x1, &y1, &n) != 2) |
2158 | goto badmove; |
2159 | is1 = INDEX(ret, gridi, x1, y1); |
2160 | if (!is1) goto badmove; |
2161 | island_togglemark(is1); |
2162 | } else |
2163 | goto badmove; |
2164 | |
2165 | move += n; |
2166 | if (*move == ';') |
2167 | move++; |
2168 | else if (*move) goto badmove; |
2169 | } |
2170 | |
2171 | map_update_possibles(ret); |
2172 | if (map_check(ret)) { |
2173 | debug(("Game completed.\n")); |
2174 | ret->completed = 1; |
2175 | } |
2176 | return ret; |
2177 | |
2178 | badmove: |
2179 | debug(("%s: unrecognised move.\n", move)); |
2180 | free_game(ret); |
2181 | return NULL; |
2182 | } |
2183 | |
2184 | static char *solve_game(game_state *state, game_state *currstate, |
2185 | char *aux, char **error) |
2186 | { |
2187 | char *ret; |
2188 | game_state *solved; |
2189 | |
2190 | if (aux) { |
2191 | debug(("solve_game: aux = %s\n", aux)); |
2192 | solved = execute_move(state, aux); |
2193 | if (!solved) { |
2194 | *error = "Generated aux string is not a valid move (!)."; |
2195 | return NULL; |
2196 | } |
2197 | } else { |
2198 | solved = dup_game(state); |
2199 | /* solve with max strength... */ |
2200 | if (solve_from_scratch(solved, 10) == 0) { |
2201 | free_game(solved); |
2202 | *error = "Game does not have a (non-recursive) solution."; |
2203 | return NULL; |
2204 | } |
2205 | } |
2206 | ret = game_state_diff(currstate, solved); |
2207 | free_game(solved); |
2208 | debug(("solve_game: ret = %s\n", ret)); |
2209 | return ret; |
2210 | } |
2211 | |
2212 | /* ---------------------------------------------------------------------- |
2213 | * Drawing routines. |
2214 | */ |
2215 | |
2216 | static void game_compute_size(game_params *params, int tilesize, |
2217 | int *x, int *y) |
2218 | { |
2219 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
2220 | struct { int tilesize; } ads, *ds = &ads; |
2221 | ads.tilesize = tilesize; |
2222 | |
2223 | *x = TILE_SIZE * params->w + 2 * BORDER; |
2224 | *y = TILE_SIZE * params->h + 2 * BORDER; |
2225 | } |
2226 | |
2227 | static void game_set_size(drawing *dr, game_drawstate *ds, |
2228 | game_params *params, int tilesize) |
2229 | { |
2230 | ds->tilesize = tilesize; |
2231 | } |
2232 | |
8266f3fc |
2233 | static float *game_colours(frontend *fe, int *ncolours) |
e7c63b02 |
2234 | { |
2235 | float *ret = snewn(3 * NCOLOURS, float); |
2236 | int i; |
2237 | |
2238 | game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT); |
2239 | |
2240 | for (i = 0; i < 3; i++) { |
2241 | ret[COL_FOREGROUND * 3 + i] = 0.0F; |
2242 | ret[COL_HINT * 3 + i] = ret[COL_LOWLIGHT * 3 + i]; |
2243 | ret[COL_GRID * 3 + i] = |
2244 | (ret[COL_HINT * 3 + i] + ret[COL_BACKGROUND * 3 + i]) * 0.5F; |
2245 | ret[COL_MARK * 3 + i] = ret[COL_HIGHLIGHT * 3 + i]; |
2246 | } |
2247 | ret[COL_WARNING * 3 + 0] = 1.0F; |
2248 | ret[COL_WARNING * 3 + 1] = 0.25F; |
2249 | ret[COL_WARNING * 3 + 2] = 0.25F; |
2250 | |
2251 | ret[COL_SELECTED * 3 + 0] = 0.25F; |
2252 | ret[COL_SELECTED * 3 + 1] = 1.00F; |
2253 | ret[COL_SELECTED * 3 + 2] = 0.25F; |
2254 | |
2255 | *ncolours = NCOLOURS; |
2256 | return ret; |
2257 | } |
2258 | |
2259 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
2260 | { |
2261 | struct game_drawstate *ds = snew(struct game_drawstate); |
2262 | int wh = state->w*state->h; |
2263 | |
2264 | ds->tilesize = 0; |
2265 | ds->w = state->w; |
2266 | ds->h = state->h; |
2267 | ds->started = 0; |
2268 | ds->grid = snewn(wh, grid_type); |
2269 | memset(ds->grid, -1, wh*sizeof(grid_type)); |
2270 | ds->lv = snewn(wh, int); |
2271 | ds->lh = snewn(wh, int); |
2272 | memset(ds->lv, 0, wh*sizeof(int)); |
2273 | memset(ds->lh, 0, wh*sizeof(int)); |
2274 | |
2275 | return ds; |
2276 | } |
2277 | |
2278 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
2279 | { |
2280 | sfree(ds->lv); |
2281 | sfree(ds->lh); |
2282 | sfree(ds->grid); |
2283 | sfree(ds); |
2284 | } |
2285 | |
2286 | #define LINE_WIDTH (TILE_SIZE/8) |
2287 | #define TS8(x) (((x)*TILE_SIZE)/8) |
2288 | |
2289 | #define OFFSET(thing) ((TILE_SIZE/2) - ((thing)/2)) |
2290 | |
5b0ab052 |
2291 | static void lines_vert(drawing *dr, game_drawstate *ds, |
2292 | int ox, int oy, int lv, int col, grid_type v) |
e7c63b02 |
2293 | { |
5b0ab052 |
2294 | int lw = LINE_WIDTH, gw = LINE_WIDTH, bw, i, loff; |
2295 | while ((bw = lw * lv + gw * (lv+1)) > TILE_SIZE) |
2296 | gw--; |
2297 | loff = OFFSET(bw); |
e7c63b02 |
2298 | if (v & G_MARKV) |
5b0ab052 |
2299 | draw_rect(dr, ox + loff, oy, bw, TILE_SIZE, COL_MARK); |
2300 | for (i = 0; i < lv; i++, loff += lw + gw) |
2301 | draw_rect(dr, ox + loff + gw, oy, lw, TILE_SIZE, col); |
e7c63b02 |
2302 | } |
2303 | |
5b0ab052 |
2304 | static void lines_horiz(drawing *dr, game_drawstate *ds, |
2305 | int ox, int oy, int lh, int col, grid_type v) |
e7c63b02 |
2306 | { |
5b0ab052 |
2307 | int lw = LINE_WIDTH, gw = LINE_WIDTH, bw, i, loff; |
2308 | while ((bw = lw * lh + gw * (lh+1)) > TILE_SIZE) |
2309 | gw--; |
2310 | loff = OFFSET(bw); |
e7c63b02 |
2311 | if (v & G_MARKH) |
5b0ab052 |
2312 | draw_rect(dr, ox, oy + loff, TILE_SIZE, bw, COL_MARK); |
2313 | for (i = 0; i < lh; i++, loff += lw + gw) |
2314 | draw_rect(dr, ox, oy + loff + gw, TILE_SIZE, lw, col); |
e7c63b02 |
2315 | } |
2316 | |
2317 | static void line_cross(drawing *dr, game_drawstate *ds, |
2318 | int ox, int oy, int col, grid_type v) |
2319 | { |
2320 | int off = TS8(2); |
2321 | draw_line(dr, ox, oy, ox+off, oy+off, col); |
2322 | draw_line(dr, ox+off, oy, ox, oy+off, col); |
2323 | } |
2324 | |
2325 | static void lines_lvlh(game_state *state, int x, int y, grid_type v, |
2326 | int *lv_r, int *lh_r) |
2327 | { |
2328 | int lh = 0, lv = 0; |
2329 | |
2330 | if (v & G_LINEV) lv = INDEX(state,lines,x,y); |
2331 | if (v & G_LINEH) lh = INDEX(state,lines,x,y); |
2332 | |
2333 | #ifdef DRAW_HINTS |
2334 | if (INDEX(state, possv, x, y) && !lv) { |
2335 | lv = INDEX(state, possv, x, y); |
2336 | } |
2337 | if (INDEX(state, possh, x, y) && !lh) { |
2338 | lh = INDEX(state, possh, x, y); |
2339 | } |
2340 | #endif |
2341 | /*debug(("lvlh: (%d,%d) v 0x%x lv %d lh %d.\n", x, y, v, lv, lh));*/ |
2342 | *lv_r = lv; *lh_r = lh; |
2343 | } |
2344 | |
2345 | static void dsf_debug_draw(drawing *dr, |
2346 | game_state *state, game_drawstate *ds, |
2347 | int x, int y) |
2348 | { |
2349 | #ifdef DRAW_DSF |
2350 | int ts = TILE_SIZE/2; |
2351 | int ox = COORD(x) + ts/2, oy = COORD(y) + ts/2; |
2352 | char str[10]; |
2353 | |
2354 | sprintf(str, "%d", dsf_canonify(state->solver->dsf, DINDEX(x,y))); |
2355 | draw_text(dr, ox, oy, FONT_VARIABLE, ts, |
2356 | ALIGN_VCENTRE | ALIGN_HCENTRE, COL_WARNING, str); |
2357 | #endif |
2358 | } |
2359 | |
2360 | static void lines_redraw(drawing *dr, |
2361 | game_state *state, game_drawstate *ds, game_ui *ui, |
2362 | int x, int y, grid_type v, int lv, int lh) |
2363 | { |
5b0ab052 |
2364 | int ox = COORD(x), oy = COORD(y); |
e7c63b02 |
2365 | int vcol = (v & G_FLASH) ? COL_HIGHLIGHT : |
2366 | (v & G_WARN) ? COL_WARNING : COL_FOREGROUND, hcol = vcol; |
2367 | grid_type todraw = v & G_NOLINE; |
2368 | |
2369 | if (v & G_ISSEL) { |
2370 | if (ui->todraw & G_FLAGSH) hcol = COL_SELECTED; |
2371 | if (ui->todraw & G_FLAGSV) vcol = COL_SELECTED; |
2372 | todraw |= ui->todraw; |
2373 | } |
2374 | |
2375 | draw_rect(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND); |
2376 | |
2377 | #ifdef DRAW_HINTS |
2378 | if (INDEX(state, possv, x, y) && !(v & G_LINEV)) |
2379 | vcol = COL_HINT; |
2380 | if (INDEX(state, possh, x, y) && !(v & G_LINEH)) |
2381 | hcol = COL_HINT; |
2382 | #endif |
2383 | #ifdef DRAW_GRID |
2384 | draw_rect_outline(dr, ox, oy, TILE_SIZE, TILE_SIZE, COL_GRID); |
2385 | #endif |
2386 | |
2387 | if (todraw & G_NOLINEV) { |
2388 | line_cross(dr, ds, ox + TS8(3), oy + TS8(1), vcol, todraw); |
2389 | line_cross(dr, ds, ox + TS8(3), oy + TS8(5), vcol, todraw); |
2390 | } |
2391 | if (todraw & G_NOLINEH) { |
2392 | line_cross(dr, ds, ox + TS8(1), oy + TS8(3), hcol, todraw); |
2393 | line_cross(dr, ds, ox + TS8(5), oy + TS8(3), hcol, todraw); |
2394 | } |
5b0ab052 |
2395 | if (lv) |
2396 | lines_vert(dr, ds, ox, oy, lv, vcol, v); |
2397 | if (lh) |
2398 | lines_horiz(dr, ds, ox, oy, lh, hcol, v); |
e7c63b02 |
2399 | |
2400 | dsf_debug_draw(dr, state, ds, x, y); |
2401 | draw_update(dr, ox, oy, TILE_SIZE, TILE_SIZE); |
2402 | } |
2403 | |
e5ab926f |
2404 | #define ISLAND_RADIUS ((TILE_SIZE*12)/20) |
e7c63b02 |
2405 | #define ISLAND_NUMSIZE(is) \ |
e5ab926f |
2406 | (((is)->count < 10) ? (TILE_SIZE*7)/10 : (TILE_SIZE*5)/10) |
e7c63b02 |
2407 | |
2408 | static void island_redraw(drawing *dr, |
2409 | game_state *state, game_drawstate *ds, |
2410 | struct island *is, grid_type v) |
2411 | { |
2412 | /* These overlap the edges of their squares, which is why they're drawn later. |
2413 | * We know they can't overlap each other because they're not allowed within 2 |
2414 | * squares of each other. */ |
2415 | int half = TILE_SIZE/2; |
2416 | int ox = COORD(is->x) + half, oy = COORD(is->y) + half; |
2417 | int orad = ISLAND_RADIUS, irad = orad - LINE_WIDTH; |
2418 | int updatesz = orad*2+1; |
2419 | int tcol = (v & G_FLASH) ? COL_HIGHLIGHT : |
2420 | (v & G_WARN) ? COL_WARNING : COL_FOREGROUND; |
2421 | int col = (v & G_ISSEL) ? COL_SELECTED : tcol; |
2422 | int bg = (v & G_MARK) ? COL_MARK : COL_BACKGROUND; |
2423 | char str[10]; |
2424 | |
2425 | #ifdef DRAW_GRID |
2426 | draw_rect_outline(dr, COORD(is->x), COORD(is->y), |
2427 | TILE_SIZE, TILE_SIZE, COL_GRID); |
2428 | #endif |
2429 | |
2430 | /* draw a thick circle */ |
2431 | draw_circle(dr, ox, oy, orad, col, col); |
2432 | draw_circle(dr, ox, oy, irad, bg, bg); |
2433 | |
2434 | sprintf(str, "%d", is->count); |
2435 | draw_text(dr, ox, oy, FONT_VARIABLE, ISLAND_NUMSIZE(is), |
2436 | ALIGN_VCENTRE | ALIGN_HCENTRE, tcol, str); |
2437 | |
2438 | dsf_debug_draw(dr, state, ds, is->x, is->y); |
2439 | draw_update(dr, ox - orad, oy - orad, updatesz, updatesz); |
2440 | } |
2441 | |
2442 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
2443 | game_state *state, int dir, game_ui *ui, |
2444 | float animtime, float flashtime) |
2445 | { |
2446 | int x, y, force = 0, i, j, redraw, lv, lh; |
2447 | grid_type v, dsv, flash = 0; |
2448 | struct island *is, *is_drag_src = NULL, *is_drag_dst = NULL; |
2449 | |
2450 | if (flashtime) { |
2451 | int f = (int)(flashtime * 5 / FLASH_TIME); |
2452 | if (f == 1 || f == 3) flash = G_FLASH; |
2453 | } |
2454 | |
2455 | /* Clear screen, if required. */ |
2456 | if (!ds->started) { |
2457 | draw_rect(dr, 0, 0, |
2458 | TILE_SIZE * ds->w + 2 * BORDER, |
2459 | TILE_SIZE * ds->h + 2 * BORDER, COL_BACKGROUND); |
2460 | #ifdef DRAW_GRID |
2461 | draw_rect_outline(dr, |
2462 | COORD(0)-1, COORD(0)-1, |
2463 | TILE_SIZE * ds->w + 2, TILE_SIZE * ds->h + 2, |
2464 | COL_GRID); |
2465 | #endif |
2466 | draw_update(dr, 0, 0, |
2467 | TILE_SIZE * ds->w + 2 * BORDER, |
2468 | TILE_SIZE * ds->h + 2 * BORDER); |
2469 | ds->started = 1; |
2470 | force = 1; |
2471 | } |
2472 | |
2473 | if (ui->dragx_src != -1 && ui->dragy_src != -1) { |
2474 | ds->dragging = 1; |
2475 | is_drag_src = INDEX(state, gridi, ui->dragx_src, ui->dragy_src); |
2476 | assert(is_drag_src); |
2477 | if (ui->dragx_dst != -1 && ui->dragy_dst != -1) { |
2478 | is_drag_dst = INDEX(state, gridi, ui->dragx_dst, ui->dragy_dst); |
2479 | assert(is_drag_dst); |
2480 | } |
2481 | } else |
2482 | ds->dragging = 0; |
2483 | |
2484 | /* Draw all lines (and hints, if we want), but *not* islands. */ |
2485 | for (x = 0; x < ds->w; x++) { |
2486 | for (y = 0; y < ds->h; y++) { |
2487 | v = GRID(state, x, y) | flash; |
2488 | dsv = GRID(ds,x,y) & ~G_REDRAW; |
2489 | |
2490 | if (v & G_ISLAND) continue; |
2491 | |
2492 | if (is_drag_dst) { |
2493 | if (WITHIN(x,is_drag_src->x, is_drag_dst->x) && |
2494 | WITHIN(y,is_drag_src->y, is_drag_dst->y)) |
2495 | v |= G_ISSEL; |
2496 | } |
2497 | lines_lvlh(state, x, y, v, &lv, &lh); |
2498 | |
2499 | if (v != dsv || |
2500 | lv != INDEX(ds,lv,x,y) || |
2501 | lh != INDEX(ds,lh,x,y) || |
2502 | force) { |
2503 | GRID(ds, x, y) = v | G_REDRAW; |
2504 | INDEX(ds,lv,x,y) = lv; |
2505 | INDEX(ds,lh,x,y) = lh; |
2506 | lines_redraw(dr, state, ds, ui, x, y, v, lv, lh); |
2507 | } else |
2508 | GRID(ds,x,y) &= ~G_REDRAW; |
2509 | } |
2510 | } |
2511 | |
2512 | /* Draw islands. */ |
2513 | for (i = 0; i < state->n_islands; i++) { |
2514 | is = &state->islands[i]; |
2515 | v = GRID(state, is->x, is->y) | flash; |
2516 | |
2517 | redraw = 0; |
2518 | for (j = 0; j < is->adj.npoints; j++) { |
2519 | if (GRID(ds,is->adj.points[j].x,is->adj.points[j].y) & G_REDRAW) { |
2520 | redraw = 1; |
2521 | } |
2522 | } |
2523 | |
2524 | if (is_drag_src) { |
2525 | if (is == is_drag_src) |
2526 | v |= G_ISSEL; |
2527 | else if (is_drag_dst && is == is_drag_dst) |
2528 | v |= G_ISSEL; |
2529 | } |
2530 | |
2531 | if (island_impossible(is, v & G_MARK)) v |= G_WARN; |
2532 | |
2533 | if ((v != GRID(ds, is->x, is->y)) || force || redraw) { |
2534 | GRID(ds,is->x,is->y) = v; |
2535 | island_redraw(dr, state, ds, is, v); |
2536 | } |
2537 | } |
2538 | } |
2539 | |
2540 | static float game_anim_length(game_state *oldstate, game_state *newstate, |
2541 | int dir, game_ui *ui) |
2542 | { |
2543 | return 0.0F; |
2544 | } |
2545 | |
2546 | static float game_flash_length(game_state *oldstate, game_state *newstate, |
2547 | int dir, game_ui *ui) |
2548 | { |
2549 | if (!oldstate->completed && newstate->completed && |
2550 | !oldstate->solved && !newstate->solved) |
2551 | return FLASH_TIME; |
2552 | |
2553 | return 0.0F; |
2554 | } |
2555 | |
e7c63b02 |
2556 | static int game_timing_state(game_state *state, game_ui *ui) |
2557 | { |
2558 | return TRUE; |
2559 | } |
2560 | |
2561 | static void game_print_size(game_params *params, float *x, float *y) |
2562 | { |
2563 | int pw, ph; |
2564 | |
2565 | /* 10mm squares by default. */ |
2566 | game_compute_size(params, 1000, &pw, &ph); |
2567 | *x = pw / 100.0; |
2568 | *y = ph / 100.0; |
2569 | } |
2570 | |
2571 | static void game_print(drawing *dr, game_state *state, int ts) |
2572 | { |
2573 | int ink = print_mono_colour(dr, 0); |
2574 | int paper = print_mono_colour(dr, 1); |
2575 | int x, y, cx, cy, i, nl; |
2576 | int loff = ts/8; |
2577 | grid_type grid; |
2578 | |
2579 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
2580 | game_drawstate ads, *ds = &ads; |
2581 | ads.tilesize = ts; |
2582 | |
2583 | /* I don't think this wants a border. */ |
2584 | |
2585 | /* Bridges */ |
2586 | print_line_width(dr, ts / 12); |
2587 | for (x = 0; x < state->w; x++) { |
2588 | for (y = 0; y < state->h; y++) { |
2589 | cx = COORD(x); cy = COORD(y); |
2590 | grid = GRID(state,x,y); |
2591 | nl = INDEX(state,lines,x,y); |
2592 | |
2593 | if (grid & G_ISLAND) continue; |
2594 | if (grid & G_LINEV) { |
2595 | if (nl > 1) { |
2596 | draw_line(dr, cx+ts/2-loff, cy, cx+ts/2-loff, cy+ts, ink); |
2597 | draw_line(dr, cx+ts/2+loff, cy, cx+ts/2+loff, cy+ts, ink); |
2598 | } else { |
2599 | draw_line(dr, cx+ts/2, cy, cx+ts/2, cy+ts, ink); |
2600 | } |
2601 | } |
2602 | if (grid & G_LINEH) { |
2603 | if (nl > 1) { |
2604 | draw_line(dr, cx, cy+ts/2-loff, cx+ts, cy+ts/2-loff, ink); |
2605 | draw_line(dr, cx, cy+ts/2+loff, cx+ts, cy+ts/2+loff, ink); |
2606 | } else { |
2607 | draw_line(dr, cx, cy+ts/2, cx+ts, cy+ts/2, ink); |
2608 | } |
2609 | } |
2610 | } |
2611 | } |
2612 | |
2613 | /* Islands */ |
2614 | for (i = 0; i < state->n_islands; i++) { |
2615 | char str[10]; |
2616 | struct island *is = &state->islands[i]; |
2617 | grid = GRID(state, is->x, is->y); |
2618 | cx = COORD(is->x) + ts/2; |
2619 | cy = COORD(is->y) + ts/2; |
2620 | |
2621 | draw_circle(dr, cx, cy, ISLAND_RADIUS, paper, ink); |
2622 | |
2623 | sprintf(str, "%d", is->count); |
2624 | draw_text(dr, cx, cy, FONT_VARIABLE, ISLAND_NUMSIZE(is), |
2625 | ALIGN_VCENTRE | ALIGN_HCENTRE, ink, str); |
2626 | } |
2627 | } |
2628 | |
2629 | #ifdef COMBINED |
2630 | #define thegame bridges |
2631 | #endif |
2632 | |
2633 | const struct game thegame = { |
2634 | "Bridges", "games.bridges", |
2635 | default_params, |
2636 | game_fetch_preset, |
2637 | decode_params, |
2638 | encode_params, |
2639 | free_params, |
2640 | dup_params, |
2641 | TRUE, game_configure, custom_params, |
2642 | validate_params, |
2643 | new_game_desc, |
2644 | validate_desc, |
2645 | new_game, |
2646 | dup_game, |
2647 | free_game, |
2648 | TRUE, solve_game, |
2649 | TRUE, game_text_format, |
2650 | new_ui, |
2651 | free_ui, |
2652 | encode_ui, |
2653 | decode_ui, |
2654 | game_changed_state, |
2655 | interpret_move, |
2656 | execute_move, |
2657 | PREFERRED_TILE_SIZE, game_compute_size, game_set_size, |
2658 | game_colours, |
2659 | game_new_drawstate, |
2660 | game_free_drawstate, |
2661 | game_redraw, |
2662 | game_anim_length, |
2663 | game_flash_length, |
2664 | TRUE, FALSE, game_print_size, game_print, |
ac9f41c4 |
2665 | FALSE, /* wants_statusbar */ |
e7c63b02 |
2666 | FALSE, game_timing_state, |
2705d374 |
2667 | 0, /* flags */ |
e7c63b02 |
2668 | }; |
2669 | |
2670 | /* vim: set shiftwidth=4 tabstop=8: */ |