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