720a8fb7 |
1 | /* |
2 | * net.c: Net game. |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <string.h> |
8 | #include <assert.h> |
b0e26073 |
9 | #include <ctype.h> |
2ef96bd6 |
10 | #include <math.h> |
720a8fb7 |
11 | |
12 | #include "puzzles.h" |
13 | #include "tree234.h" |
14 | |
2ef96bd6 |
15 | #define MATMUL(xr,yr,m,x,y) do { \ |
16 | float rx, ry, xx = (x), yy = (y), *mat = (m); \ |
17 | rx = mat[0] * xx + mat[2] * yy; \ |
18 | ry = mat[1] * xx + mat[3] * yy; \ |
19 | (xr) = rx; (yr) = ry; \ |
20 | } while (0) |
21 | |
22 | /* Direction and other bitfields */ |
720a8fb7 |
23 | #define R 0x01 |
24 | #define U 0x02 |
25 | #define L 0x04 |
26 | #define D 0x08 |
27 | #define LOCKED 0x10 |
2ef96bd6 |
28 | #define ACTIVE 0x20 |
720a8fb7 |
29 | |
30 | /* Rotations: Anticlockwise, Clockwise, Flip, general rotate */ |
31 | #define A(x) ( (((x) & 0x07) << 1) | (((x) & 0x08) >> 3) ) |
32 | #define C(x) ( (((x) & 0x0E) >> 1) | (((x) & 0x01) << 3) ) |
33 | #define F(x) ( (((x) & 0x0C) >> 2) | (((x) & 0x03) << 2) ) |
34 | #define ROT(x, n) ( ((n)&3) == 0 ? (x) : \ |
35 | ((n)&3) == 1 ? A(x) : \ |
36 | ((n)&3) == 2 ? F(x) : C(x) ) |
37 | |
38 | /* X and Y displacements */ |
39 | #define X(x) ( (x) == R ? +1 : (x) == L ? -1 : 0 ) |
40 | #define Y(x) ( (x) == D ? +1 : (x) == U ? -1 : 0 ) |
41 | |
42 | /* Bit count */ |
43 | #define COUNT(x) ( (((x) & 0x08) >> 3) + (((x) & 0x04) >> 2) + \ |
44 | (((x) & 0x02) >> 1) + ((x) & 0x01) ) |
45 | |
46 | #define TILE_SIZE 32 |
47 | #define TILE_BORDER 1 |
48 | #define WINDOW_OFFSET 16 |
49 | |
8c1fd974 |
50 | #define ROTATE_TIME 0.13F |
51 | #define FLASH_FRAME 0.07F |
2ef96bd6 |
52 | |
f0ee053c |
53 | /* Transform physical coords to game coords using game_drawstate ds */ |
54 | #define GX(x) (((x) + ds->org_x) % ds->width) |
55 | #define GY(y) (((y) + ds->org_y) % ds->height) |
56 | /* ...and game coords to physical coords */ |
57 | #define RX(x) (((x) + ds->width - ds->org_x) % ds->width) |
58 | #define RY(y) (((y) + ds->height - ds->org_y) % ds->height) |
59 | |
2ef96bd6 |
60 | enum { |
61 | COL_BACKGROUND, |
62 | COL_LOCKED, |
63 | COL_BORDER, |
64 | COL_WIRE, |
65 | COL_ENDPOINT, |
66 | COL_POWERED, |
67 | COL_BARRIER, |
68 | NCOLOURS |
69 | }; |
70 | |
720a8fb7 |
71 | struct game_params { |
72 | int width; |
73 | int height; |
74 | int wrapping; |
c0edd11f |
75 | int unique; |
720a8fb7 |
76 | float barrier_probability; |
77 | }; |
78 | |
1185e3c5 |
79 | struct game_aux_info { |
2ac6d24e |
80 | int width, height; |
2ac6d24e |
81 | unsigned char *tiles; |
82 | }; |
83 | |
720a8fb7 |
84 | struct game_state { |
f0ee053c |
85 | int width, height, wrapping, completed; |
1185e3c5 |
86 | int last_rotate_x, last_rotate_y, last_rotate_dir; |
2ac6d24e |
87 | int used_solve, just_used_solve; |
720a8fb7 |
88 | unsigned char *tiles; |
89 | unsigned char *barriers; |
90 | }; |
91 | |
c0edd11f |
92 | #define OFFSETWH(x2,y2,x1,y1,dir,width,height) \ |
93 | ( (x2) = ((x1) + width + X((dir))) % width, \ |
94 | (y2) = ((y1) + height + Y((dir))) % height) |
95 | |
720a8fb7 |
96 | #define OFFSET(x2,y2,x1,y1,dir,state) \ |
c0edd11f |
97 | OFFSETWH(x2,y2,x1,y1,dir,(state)->width,(state)->height) |
720a8fb7 |
98 | |
99 | #define index(state, a, x, y) ( a[(y) * (state)->width + (x)] ) |
100 | #define tile(state, x, y) index(state, (state)->tiles, x, y) |
101 | #define barrier(state, x, y) index(state, (state)->barriers, x, y) |
102 | |
103 | struct xyd { |
104 | int x, y, direction; |
105 | }; |
106 | |
c0edd11f |
107 | static int xyd_cmp(const void *av, const void *bv) { |
108 | const struct xyd *a = (const struct xyd *)av; |
109 | const struct xyd *b = (const struct xyd *)bv; |
720a8fb7 |
110 | if (a->x < b->x) |
111 | return -1; |
112 | if (a->x > b->x) |
113 | return +1; |
114 | if (a->y < b->y) |
115 | return -1; |
116 | if (a->y > b->y) |
117 | return +1; |
118 | if (a->direction < b->direction) |
119 | return -1; |
120 | if (a->direction > b->direction) |
121 | return +1; |
122 | return 0; |
23e8c9fd |
123 | } |
720a8fb7 |
124 | |
c0edd11f |
125 | static int xyd_cmp_nc(void *av, void *bv) { return xyd_cmp(av, bv); } |
126 | |
720a8fb7 |
127 | static struct xyd *new_xyd(int x, int y, int direction) |
128 | { |
129 | struct xyd *xyd = snew(struct xyd); |
130 | xyd->x = x; |
131 | xyd->y = y; |
132 | xyd->direction = direction; |
133 | return xyd; |
134 | } |
135 | |
136 | /* ---------------------------------------------------------------------- |
7f77ea24 |
137 | * Manage game parameters. |
138 | */ |
be8d5aa1 |
139 | static game_params *default_params(void) |
7f77ea24 |
140 | { |
141 | game_params *ret = snew(game_params); |
142 | |
eb2ad6f1 |
143 | ret->width = 5; |
144 | ret->height = 5; |
145 | ret->wrapping = FALSE; |
c0edd11f |
146 | ret->unique = TRUE; |
eb2ad6f1 |
147 | ret->barrier_probability = 0.0; |
7f77ea24 |
148 | |
149 | return ret; |
150 | } |
151 | |
ab53eb64 |
152 | static const struct game_params net_presets[] = { |
153 | {5, 5, FALSE, TRUE, 0.0}, |
154 | {7, 7, FALSE, TRUE, 0.0}, |
155 | {9, 9, FALSE, TRUE, 0.0}, |
156 | {11, 11, FALSE, TRUE, 0.0}, |
157 | {13, 11, FALSE, TRUE, 0.0}, |
158 | {5, 5, TRUE, TRUE, 0.0}, |
159 | {7, 7, TRUE, TRUE, 0.0}, |
160 | {9, 9, TRUE, TRUE, 0.0}, |
161 | {11, 11, TRUE, TRUE, 0.0}, |
162 | {13, 11, TRUE, TRUE, 0.0}, |
163 | }; |
164 | |
be8d5aa1 |
165 | static int game_fetch_preset(int i, char **name, game_params **params) |
eb2ad6f1 |
166 | { |
167 | game_params *ret; |
168 | char str[80]; |
ab53eb64 |
169 | |
170 | if (i < 0 || i >= lenof(net_presets)) |
eb2ad6f1 |
171 | return FALSE; |
172 | |
173 | ret = snew(game_params); |
ab53eb64 |
174 | *ret = net_presets[i]; |
eb2ad6f1 |
175 | |
176 | sprintf(str, "%dx%d%s", ret->width, ret->height, |
177 | ret->wrapping ? " wrapping" : ""); |
178 | |
179 | *name = dupstr(str); |
180 | *params = ret; |
181 | return TRUE; |
182 | } |
183 | |
be8d5aa1 |
184 | static void free_params(game_params *params) |
7f77ea24 |
185 | { |
186 | sfree(params); |
187 | } |
188 | |
be8d5aa1 |
189 | static game_params *dup_params(game_params *params) |
eb2ad6f1 |
190 | { |
191 | game_params *ret = snew(game_params); |
192 | *ret = *params; /* structure copy */ |
193 | return ret; |
194 | } |
195 | |
1185e3c5 |
196 | static void decode_params(game_params *ret, char const *string) |
b0e26073 |
197 | { |
b0e26073 |
198 | char const *p = string; |
199 | |
200 | ret->width = atoi(p); |
40fde884 |
201 | while (*p && isdigit((unsigned char)*p)) p++; |
b0e26073 |
202 | if (*p == 'x') { |
203 | p++; |
204 | ret->height = atoi(p); |
40fde884 |
205 | while (*p && isdigit((unsigned char)*p)) p++; |
b0e26073 |
206 | } else { |
207 | ret->height = ret->width; |
208 | } |
c0edd11f |
209 | |
210 | while (*p) { |
211 | if (*p == 'w') { |
212 | p++; |
213 | ret->wrapping = TRUE; |
214 | } else if (*p == 'b') { |
215 | p++; |
216 | ret->barrier_probability = atof(p); |
40fde884 |
217 | while (*p && (*p == '.' || isdigit((unsigned char)*p))) p++; |
c0edd11f |
218 | } else if (*p == 'a') { |
219 | p++; |
220 | ret->unique = FALSE; |
40fde884 |
221 | } else |
222 | p++; /* skip any other gunk */ |
c0edd11f |
223 | } |
b0e26073 |
224 | } |
225 | |
1185e3c5 |
226 | static char *encode_params(game_params *params, int full) |
b0e26073 |
227 | { |
228 | char ret[400]; |
229 | int len; |
230 | |
231 | len = sprintf(ret, "%dx%d", params->width, params->height); |
232 | if (params->wrapping) |
233 | ret[len++] = 'w'; |
1185e3c5 |
234 | if (full && params->barrier_probability) |
b0e26073 |
235 | len += sprintf(ret+len, "b%g", params->barrier_probability); |
40fde884 |
236 | if (full && !params->unique) |
c0edd11f |
237 | ret[len++] = 'a'; |
b0e26073 |
238 | assert(len < lenof(ret)); |
239 | ret[len] = '\0'; |
240 | |
241 | return dupstr(ret); |
242 | } |
243 | |
be8d5aa1 |
244 | static config_item *game_configure(game_params *params) |
c8230524 |
245 | { |
246 | config_item *ret; |
247 | char buf[80]; |
248 | |
c0edd11f |
249 | ret = snewn(6, config_item); |
c8230524 |
250 | |
251 | ret[0].name = "Width"; |
95709966 |
252 | ret[0].type = C_STRING; |
c8230524 |
253 | sprintf(buf, "%d", params->width); |
254 | ret[0].sval = dupstr(buf); |
255 | ret[0].ival = 0; |
256 | |
257 | ret[1].name = "Height"; |
95709966 |
258 | ret[1].type = C_STRING; |
c8230524 |
259 | sprintf(buf, "%d", params->height); |
260 | ret[1].sval = dupstr(buf); |
261 | ret[1].ival = 0; |
262 | |
263 | ret[2].name = "Walls wrap around"; |
95709966 |
264 | ret[2].type = C_BOOLEAN; |
c8230524 |
265 | ret[2].sval = NULL; |
266 | ret[2].ival = params->wrapping; |
267 | |
268 | ret[3].name = "Barrier probability"; |
95709966 |
269 | ret[3].type = C_STRING; |
c8230524 |
270 | sprintf(buf, "%g", params->barrier_probability); |
271 | ret[3].sval = dupstr(buf); |
272 | ret[3].ival = 0; |
273 | |
c0edd11f |
274 | ret[4].name = "Ensure unique solution"; |
275 | ret[4].type = C_BOOLEAN; |
c8230524 |
276 | ret[4].sval = NULL; |
c0edd11f |
277 | ret[4].ival = params->unique; |
278 | |
279 | ret[5].name = NULL; |
280 | ret[5].type = C_END; |
281 | ret[5].sval = NULL; |
282 | ret[5].ival = 0; |
c8230524 |
283 | |
284 | return ret; |
285 | } |
286 | |
be8d5aa1 |
287 | static game_params *custom_params(config_item *cfg) |
c8230524 |
288 | { |
289 | game_params *ret = snew(game_params); |
290 | |
291 | ret->width = atoi(cfg[0].sval); |
292 | ret->height = atoi(cfg[1].sval); |
293 | ret->wrapping = cfg[2].ival; |
95709966 |
294 | ret->barrier_probability = (float)atof(cfg[3].sval); |
c0edd11f |
295 | ret->unique = cfg[4].ival; |
c8230524 |
296 | |
297 | return ret; |
298 | } |
299 | |
be8d5aa1 |
300 | static char *validate_params(game_params *params) |
c8230524 |
301 | { |
ab53eb64 |
302 | if (params->width <= 0 || params->height <= 0) |
c8230524 |
303 | return "Width and height must both be greater than zero"; |
c8230524 |
304 | if (params->width <= 1 && params->height <= 1) |
305 | return "At least one of width and height must be greater than one"; |
306 | if (params->barrier_probability < 0) |
307 | return "Barrier probability may not be negative"; |
308 | if (params->barrier_probability > 1) |
309 | return "Barrier probability may not be greater than 1"; |
e7c352f5 |
310 | |
311 | /* |
312 | * Specifying either grid dimension as 2 in a wrapping puzzle |
313 | * makes it actually impossible to ensure a unique puzzle |
314 | * solution. |
315 | * |
316 | * Proof: |
317 | * |
318 | * Without loss of generality, let us assume the puzzle _width_ |
319 | * is 2, so we can conveniently discuss rows without having to |
320 | * say `rows/columns' all the time. (The height may be 2 as |
321 | * well, but that doesn't matter.) |
322 | * |
323 | * In each row, there are two edges between tiles: the inner |
324 | * edge (running down the centre of the grid) and the outer |
325 | * edge (the identified left and right edges of the grid). |
326 | * |
327 | * Lemma: In any valid 2xn puzzle there must be at least one |
328 | * row in which _exactly one_ of the inner edge and outer edge |
329 | * is connected. |
330 | * |
331 | * Proof: No row can have _both_ inner and outer edges |
332 | * connected, because this would yield a loop. So the only |
333 | * other way to falsify the lemma is for every row to have |
334 | * _neither_ the inner nor outer edge connected. But this |
335 | * means there is no connection at all between the left and |
336 | * right columns of the puzzle, so there are two disjoint |
337 | * subgraphs, which is also disallowed. [] |
338 | * |
339 | * Given such a row, it is always possible to make the |
340 | * disconnected edge connected and the connected edge |
341 | * disconnected without changing the state of any other edge. |
342 | * (This is easily seen by case analysis on the various tiles: |
343 | * left-pointing and right-pointing endpoints can be exchanged, |
344 | * likewise T-pieces, and a corner piece can select its |
345 | * horizontal connectivity independently of its vertical.) This |
346 | * yields a distinct valid solution. |
347 | * |
348 | * Thus, for _every_ row in which exactly one of the inner and |
349 | * outer edge is connected, there are two valid states for that |
350 | * row, and hence the total number of solutions of the puzzle |
351 | * is at least 2^(number of such rows), and in particular is at |
352 | * least 2 since there must be at least one such row. [] |
353 | */ |
354 | if (params->unique && params->wrapping && |
355 | (params->width == 2 || params->height == 2)) |
356 | return "No wrapping puzzle with a width or height of 2 can have" |
357 | " a unique solution"; |
358 | |
c8230524 |
359 | return NULL; |
360 | } |
361 | |
7f77ea24 |
362 | /* ---------------------------------------------------------------------- |
c0edd11f |
363 | * Solver used to assure solution uniqueness during generation. |
364 | */ |
365 | |
366 | /* |
367 | * Test cases I used while debugging all this were |
368 | * |
369 | * ./net --generate 1 13x11w#12300 |
370 | * which expands under the non-unique grid generation rules to |
371 | * 13x11w:5eaade1bd222664436d5e2965c12656b1129dd825219e3274d558d5eb2dab5da18898e571d5a2987be79746bd95726c597447d6da96188c513add829da7681da954db113d3cd244 |
372 | * and has two ambiguous areas. |
373 | * |
374 | * An even better one is |
375 | * 13x11w#507896411361192 |
376 | * which expands to |
377 | * 13x11w:b7125b1aec598eb31bd58d82572bc11494e5dee4e8db2bdd29b88d41a16bdd996d2996ddec8c83741a1e8674e78328ba71737b8894a9271b1cd1399453d1952e43951d9b712822e |
378 | * and has an ambiguous area _and_ a situation where loop avoidance |
379 | * is a necessary deductive technique. |
380 | * |
381 | * Then there's |
382 | * 48x25w#820543338195187 |
383 | * becoming |
384 | * 48x25w:255989d14cdd185deaa753a93821a12edc1ab97943ac127e2685d7b8b3c48861b2192416139212b316eddd35de43714ebc7628d753db32e596284d9ec52c5a7dc1b4c811a655117d16dc28921b2b4161352cab1d89d18bc836b8b891d55ea4622a1251861b5bc9a8aa3e5bcd745c95229ca6c3b5e21d5832d397e917325793d7eb442dc351b2db2a52ba8e1651642275842d8871d5534aabc6d5b741aaa2d48ed2a7dbbb3151ddb49d5b9a7ed1ab98ee75d613d656dbba347bc514c84556b43a9bc65a3256ead792488b862a9d2a8a39b4255a4949ed7dbd79443292521265896b4399c95ede89d7c8c797a6a57791a849adea489359a158aa12e5dacce862b8333b7ebea7d344d1a3c53198864b73a9dedde7b663abb1b539e1e8853b1b7edb14a2a17ebaae4dbe63598a2e7e9a2dbdad415bc1d8cb88cbab5a8c82925732cd282e641ea3bd7d2c6e776de9117a26be86deb7c82c89524b122cb9397cd1acd2284e744ea62b9279bae85479ababe315c3ac29c431333395b24e6a1e3c43a2da42d4dce84aadd5b154aea555eaddcbd6e527d228c19388d9b424d94214555a7edbdeebe569d4a56dc51a86bd9963e377bb74752bd5eaa5761ba545e297b62a1bda46ab4aee423ad6c661311783cc18786d4289236563cb4a75ec67d481c14814994464cd1b87396dee63e5ab6e952cc584baa1d4c47cb557ec84dbb63d487c8728118673a166846dd3a4ebc23d6cb9c5827d96b4556e91899db32b517eda815ae271a8911bd745447121dc8d321557bc2a435ebec1bbac35b1a291669451174e6aa2218a4a9c5a6ca31ebc45d84e3a82c121e9ced7d55e9a |
385 | * which has a spot (far right) where slightly more complex loop |
386 | * avoidance is required. |
387 | */ |
388 | |
389 | static int dsf_canonify(int *dsf, int val) |
390 | { |
391 | int v2 = val; |
392 | |
393 | while (dsf[val] != val) |
394 | val = dsf[val]; |
395 | |
396 | while (v2 != val) { |
397 | int tmp = dsf[v2]; |
398 | dsf[v2] = val; |
399 | v2 = tmp; |
400 | } |
401 | |
402 | return val; |
403 | } |
404 | |
405 | static void dsf_merge(int *dsf, int v1, int v2) |
406 | { |
407 | v1 = dsf_canonify(dsf, v1); |
408 | v2 = dsf_canonify(dsf, v2); |
409 | dsf[v2] = v1; |
410 | } |
411 | |
412 | struct todo { |
413 | unsigned char *marked; |
414 | int *buffer; |
415 | int buflen; |
416 | int head, tail; |
417 | }; |
418 | |
419 | static struct todo *todo_new(int maxsize) |
420 | { |
421 | struct todo *todo = snew(struct todo); |
422 | todo->marked = snewn(maxsize, unsigned char); |
423 | memset(todo->marked, 0, maxsize); |
424 | todo->buflen = maxsize + 1; |
425 | todo->buffer = snewn(todo->buflen, int); |
426 | todo->head = todo->tail = 0; |
427 | return todo; |
428 | } |
429 | |
430 | static void todo_free(struct todo *todo) |
431 | { |
432 | sfree(todo->marked); |
433 | sfree(todo->buffer); |
434 | sfree(todo); |
435 | } |
436 | |
437 | static void todo_add(struct todo *todo, int index) |
438 | { |
439 | if (todo->marked[index]) |
440 | return; /* already on the list */ |
441 | todo->marked[index] = TRUE; |
442 | todo->buffer[todo->tail++] = index; |
443 | if (todo->tail == todo->buflen) |
444 | todo->tail = 0; |
445 | } |
446 | |
447 | static int todo_get(struct todo *todo) { |
448 | int ret; |
449 | |
450 | if (todo->head == todo->tail) |
451 | return -1; /* list is empty */ |
452 | ret = todo->buffer[todo->head++]; |
453 | if (todo->head == todo->buflen) |
454 | todo->head = 0; |
455 | todo->marked[ret] = FALSE; |
456 | |
457 | return ret; |
458 | } |
459 | |
84942c65 |
460 | static int net_solver(int w, int h, unsigned char *tiles, |
461 | unsigned char *barriers, int wrapping) |
c0edd11f |
462 | { |
463 | unsigned char *tilestate; |
464 | unsigned char *edgestate; |
465 | int *deadends; |
466 | int *equivalence; |
467 | struct todo *todo; |
468 | int i, j, x, y; |
469 | int area; |
470 | int done_something; |
471 | |
472 | /* |
473 | * Set up the solver's data structures. |
474 | */ |
475 | |
476 | /* |
477 | * tilestate stores the possible orientations of each tile. |
478 | * There are up to four of these, so we'll index the array in |
479 | * fours. tilestate[(y * w + x) * 4] and its three successive |
480 | * members give the possible orientations, clearing to 255 from |
481 | * the end as things are ruled out. |
482 | * |
483 | * In this loop we also count up the area of the grid (which is |
484 | * not _necessarily_ equal to w*h, because there might be one |
485 | * or more blank squares present. This will never happen in a |
486 | * grid generated _by_ this program, but it's worth keeping the |
487 | * solver as general as possible.) |
488 | */ |
489 | tilestate = snewn(w * h * 4, unsigned char); |
490 | area = 0; |
491 | for (i = 0; i < w*h; i++) { |
492 | tilestate[i * 4] = tiles[i] & 0xF; |
493 | for (j = 1; j < 4; j++) { |
494 | if (tilestate[i * 4 + j - 1] == 255 || |
495 | A(tilestate[i * 4 + j - 1]) == tilestate[i * 4]) |
496 | tilestate[i * 4 + j] = 255; |
497 | else |
498 | tilestate[i * 4 + j] = A(tilestate[i * 4 + j - 1]); |
499 | } |
500 | if (tiles[i] != 0) |
501 | area++; |
502 | } |
503 | |
504 | /* |
505 | * edgestate stores the known state of each edge. It is 0 for |
506 | * unknown, 1 for open (connected) and 2 for closed (not |
507 | * connected). |
508 | * |
509 | * In principle we need only worry about each edge once each, |
510 | * but in fact it's easier to track each edge twice so that we |
511 | * can reference it from either side conveniently. Also I'm |
512 | * going to allocate _five_ bytes per tile, rather than the |
513 | * obvious four, so that I can index edgestate[(y*w+x) * 5 + d] |
514 | * where d is 1,2,4,8 and they never overlap. |
515 | */ |
516 | edgestate = snewn((w * h - 1) * 5 + 9, unsigned char); |
517 | memset(edgestate, 0, (w * h - 1) * 5 + 9); |
518 | |
519 | /* |
520 | * deadends tracks which edges have dead ends on them. It is |
521 | * indexed by tile and direction: deadends[(y*w+x) * 5 + d] |
522 | * tells you whether heading out of tile (x,y) in direction d |
523 | * can reach a limited amount of the grid. Values are area+1 |
524 | * (no dead end known) or less than that (can reach _at most_ |
525 | * this many other tiles by heading this way out of this tile). |
526 | */ |
527 | deadends = snewn((w * h - 1) * 5 + 9, int); |
528 | for (i = 0; i < (w * h - 1) * 5 + 9; i++) |
529 | deadends[i] = area+1; |
530 | |
531 | /* |
532 | * equivalence tracks which sets of tiles are known to be |
533 | * connected to one another, so we can avoid creating loops by |
534 | * linking together tiles which are already linked through |
535 | * another route. |
536 | * |
537 | * This is a disjoint set forest structure: equivalence[i] |
538 | * contains the index of another member of the equivalence |
539 | * class containing i, or contains i itself for precisely one |
540 | * member in each such class. To find a representative member |
541 | * of the equivalence class containing i, you keep replacing i |
542 | * with equivalence[i] until it stops changing; then you go |
543 | * _back_ along the same path and point everything on it |
544 | * directly at the representative member so as to speed up |
545 | * future searches. Then you test equivalence between tiles by |
546 | * finding the representative of each tile and seeing if |
547 | * they're the same; and you create new equivalence (merge |
548 | * classes) by finding the representative of each tile and |
549 | * setting equivalence[one]=the_other. |
550 | */ |
551 | equivalence = snewn(w * h, int); |
552 | for (i = 0; i < w*h; i++) |
553 | equivalence[i] = i; /* initially all distinct */ |
554 | |
555 | /* |
556 | * On a non-wrapping grid, we instantly know that all the edges |
557 | * round the edge are closed. |
558 | */ |
559 | if (!wrapping) { |
560 | for (i = 0; i < w; i++) { |
561 | edgestate[i * 5 + 2] = edgestate[((h-1) * w + i) * 5 + 8] = 2; |
562 | } |
563 | for (i = 0; i < h; i++) { |
564 | edgestate[(i * w + w-1) * 5 + 1] = edgestate[(i * w) * 5 + 4] = 2; |
565 | } |
566 | } |
567 | |
568 | /* |
84942c65 |
569 | * If we have barriers available, we can mark those edges as |
570 | * closed too. |
571 | */ |
572 | if (barriers) { |
573 | for (y = 0; y < h; y++) for (x = 0; x < w; x++) { |
574 | int d; |
575 | for (d = 1; d <= 8; d += d) { |
576 | if (barriers[y*w+x] & d) { |
577 | int x2, y2; |
578 | /* |
579 | * In principle the barrier list should already |
580 | * contain each barrier from each side, but |
581 | * let's not take chances with our internal |
582 | * consistency. |
583 | */ |
584 | OFFSETWH(x2, y2, x, y, d, w, h); |
585 | edgestate[(y*w+x) * 5 + d] = 2; |
586 | edgestate[(y2*w+x2) * 5 + F(d)] = 2; |
587 | } |
588 | } |
589 | } |
590 | } |
591 | |
592 | /* |
c0edd11f |
593 | * Since most deductions made by this solver are local (the |
594 | * exception is loop avoidance, where joining two tiles |
595 | * together on one side of the grid can theoretically permit a |
596 | * fresh deduction on the other), we can address the scaling |
597 | * problem inherent in iterating repeatedly over the entire |
598 | * grid by instead working with a to-do list. |
599 | */ |
600 | todo = todo_new(w * h); |
601 | |
602 | /* |
603 | * Main deductive loop. |
604 | */ |
605 | done_something = TRUE; /* prevent instant termination! */ |
606 | while (1) { |
607 | int index; |
608 | |
609 | /* |
610 | * Take a tile index off the todo list and process it. |
611 | */ |
612 | index = todo_get(todo); |
613 | if (index == -1) { |
614 | /* |
615 | * If we have run out of immediate things to do, we |
616 | * have no choice but to scan the whole grid for |
617 | * longer-range things we've missed. Hence, I now add |
618 | * every square on the grid back on to the to-do list. |
619 | * I also set `done_something' to FALSE at this point; |
620 | * if we later come back here and find it still FALSE, |
621 | * we will know we've scanned the entire grid without |
622 | * finding anything new to do, and we can terminate. |
623 | */ |
624 | if (!done_something) |
625 | break; |
626 | for (i = 0; i < w*h; i++) |
627 | todo_add(todo, i); |
628 | done_something = FALSE; |
629 | |
630 | index = todo_get(todo); |
631 | } |
632 | |
633 | y = index / w; |
634 | x = index % w; |
635 | { |
636 | int d, ourclass = dsf_canonify(equivalence, y*w+x); |
637 | int deadendmax[9]; |
638 | |
639 | deadendmax[1] = deadendmax[2] = deadendmax[4] = deadendmax[8] = 0; |
640 | |
641 | for (i = j = 0; i < 4 && tilestate[(y*w+x) * 4 + i] != 255; i++) { |
642 | int valid; |
643 | int nnondeadends, nondeadends[4], deadendtotal; |
644 | int nequiv, equiv[5]; |
645 | int val = tilestate[(y*w+x) * 4 + i]; |
646 | |
647 | valid = TRUE; |
648 | nnondeadends = deadendtotal = 0; |
649 | equiv[0] = ourclass; |
650 | nequiv = 1; |
651 | for (d = 1; d <= 8; d += d) { |
652 | /* |
653 | * Immediately rule out this orientation if it |
654 | * conflicts with any known edge. |
655 | */ |
656 | if ((edgestate[(y*w+x) * 5 + d] == 1 && !(val & d)) || |
657 | (edgestate[(y*w+x) * 5 + d] == 2 && (val & d))) |
658 | valid = FALSE; |
659 | |
660 | if (val & d) { |
661 | /* |
662 | * Count up the dead-end statistics. |
663 | */ |
664 | if (deadends[(y*w+x) * 5 + d] <= area) { |
665 | deadendtotal += deadends[(y*w+x) * 5 + d]; |
666 | } else { |
667 | nondeadends[nnondeadends++] = d; |
668 | } |
669 | |
670 | /* |
671 | * Ensure we aren't linking to any tiles, |
672 | * through edges not already known to be |
673 | * open, which create a loop. |
674 | */ |
675 | if (edgestate[(y*w+x) * 5 + d] == 0) { |
676 | int c, k, x2, y2; |
677 | |
678 | OFFSETWH(x2, y2, x, y, d, w, h); |
679 | c = dsf_canonify(equivalence, y2*w+x2); |
680 | for (k = 0; k < nequiv; k++) |
681 | if (c == equiv[k]) |
682 | break; |
683 | if (k == nequiv) |
684 | equiv[nequiv++] = c; |
685 | else |
686 | valid = FALSE; |
687 | } |
688 | } |
689 | } |
690 | |
691 | if (nnondeadends == 0) { |
692 | /* |
693 | * If this orientation links together dead-ends |
694 | * with a total area of less than the entire |
695 | * grid, it is invalid. |
696 | * |
697 | * (We add 1 to deadendtotal because of the |
698 | * tile itself, of course; one tile linking |
699 | * dead ends of size 2 and 3 forms a subnetwork |
700 | * with a total area of 6, not 5.) |
701 | */ |
9535138a |
702 | if (deadendtotal > 0 && deadendtotal+1 < area) |
c0edd11f |
703 | valid = FALSE; |
704 | } else if (nnondeadends == 1) { |
705 | /* |
706 | * If this orientation links together one or |
707 | * more dead-ends with precisely one |
708 | * non-dead-end, then we may have to mark that |
709 | * non-dead-end as a dead end going the other |
710 | * way. However, it depends on whether all |
711 | * other orientations share the same property. |
712 | */ |
713 | deadendtotal++; |
714 | if (deadendmax[nondeadends[0]] < deadendtotal) |
715 | deadendmax[nondeadends[0]] = deadendtotal; |
716 | } else { |
717 | /* |
718 | * If this orientation links together two or |
719 | * more non-dead-ends, then we can rule out the |
720 | * possibility of putting in new dead-end |
721 | * markings in those directions. |
722 | */ |
723 | int k; |
724 | for (k = 0; k < nnondeadends; k++) |
725 | deadendmax[nondeadends[k]] = area+1; |
726 | } |
727 | |
728 | if (valid) |
729 | tilestate[(y*w+x) * 4 + j++] = val; |
730 | #ifdef SOLVER_DIAGNOSTICS |
731 | else |
732 | printf("ruling out orientation %x at %d,%d\n", val, x, y); |
733 | #endif |
734 | } |
735 | |
736 | assert(j > 0); /* we can't lose _all_ possibilities! */ |
737 | |
738 | if (j < i) { |
c0edd11f |
739 | done_something = TRUE; |
740 | |
741 | /* |
742 | * We have ruled out at least one tile orientation. |
743 | * Make sure the rest are blanked. |
744 | */ |
745 | while (j < 4) |
746 | tilestate[(y*w+x) * 4 + j++] = 255; |
3af1c093 |
747 | } |
c0edd11f |
748 | |
3af1c093 |
749 | /* |
750 | * Now go through the tile orientations again and see |
751 | * if we've deduced anything new about any edges. |
752 | */ |
753 | { |
754 | int a, o; |
c0edd11f |
755 | a = 0xF; o = 0; |
3af1c093 |
756 | |
c0edd11f |
757 | for (i = 0; i < 4 && tilestate[(y*w+x) * 4 + i] != 255; i++) { |
758 | a &= tilestate[(y*w+x) * 4 + i]; |
759 | o |= tilestate[(y*w+x) * 4 + i]; |
760 | } |
761 | for (d = 1; d <= 8; d += d) |
762 | if (edgestate[(y*w+x) * 5 + d] == 0) { |
763 | int x2, y2, d2; |
764 | OFFSETWH(x2, y2, x, y, d, w, h); |
765 | d2 = F(d); |
766 | if (a & d) { |
767 | /* This edge is open in all orientations. */ |
768 | #ifdef SOLVER_DIAGNOSTICS |
769 | printf("marking edge %d,%d:%d open\n", x, y, d); |
770 | #endif |
771 | edgestate[(y*w+x) * 5 + d] = 1; |
772 | edgestate[(y2*w+x2) * 5 + d2] = 1; |
773 | dsf_merge(equivalence, y*w+x, y2*w+x2); |
774 | done_something = TRUE; |
775 | todo_add(todo, y2*w+x2); |
776 | } else if (!(o & d)) { |
777 | /* This edge is closed in all orientations. */ |
778 | #ifdef SOLVER_DIAGNOSTICS |
779 | printf("marking edge %d,%d:%d closed\n", x, y, d); |
780 | #endif |
781 | edgestate[(y*w+x) * 5 + d] = 2; |
782 | edgestate[(y2*w+x2) * 5 + d2] = 2; |
783 | done_something = TRUE; |
784 | todo_add(todo, y2*w+x2); |
785 | } |
786 | } |
787 | |
788 | } |
789 | |
790 | /* |
791 | * Now check the dead-end markers and see if any of |
792 | * them has lowered from the real ones. |
793 | */ |
794 | for (d = 1; d <= 8; d += d) { |
795 | int x2, y2, d2; |
796 | OFFSETWH(x2, y2, x, y, d, w, h); |
797 | d2 = F(d); |
798 | if (deadendmax[d] > 0 && |
799 | deadends[(y2*w+x2) * 5 + d2] > deadendmax[d]) { |
800 | #ifdef SOLVER_DIAGNOSTICS |
801 | printf("setting dead end value %d,%d:%d to %d\n", |
802 | x2, y2, d2, deadendmax[d]); |
803 | #endif |
804 | deadends[(y2*w+x2) * 5 + d2] = deadendmax[d]; |
805 | done_something = TRUE; |
806 | todo_add(todo, y2*w+x2); |
807 | } |
808 | } |
809 | |
810 | } |
811 | } |
812 | |
813 | /* |
814 | * Mark all completely determined tiles as locked. |
815 | */ |
816 | j = TRUE; |
817 | for (i = 0; i < w*h; i++) { |
818 | if (tilestate[i * 4 + 1] == 255) { |
819 | assert(tilestate[i * 4 + 0] != 255); |
820 | tiles[i] = tilestate[i * 4] | LOCKED; |
821 | } else { |
822 | tiles[i] &= ~LOCKED; |
823 | j = FALSE; |
824 | } |
825 | } |
826 | |
827 | /* |
828 | * Free up working space. |
829 | */ |
830 | todo_free(todo); |
831 | sfree(tilestate); |
832 | sfree(edgestate); |
833 | sfree(deadends); |
834 | sfree(equivalence); |
835 | |
836 | return j; |
837 | } |
838 | |
839 | /* ---------------------------------------------------------------------- |
1185e3c5 |
840 | * Randomly select a new game description. |
720a8fb7 |
841 | */ |
842 | |
c0edd11f |
843 | /* |
844 | * Function to randomly perturb an ambiguous section in a grid, to |
845 | * attempt to ensure unique solvability. |
846 | */ |
847 | static void perturb(int w, int h, unsigned char *tiles, int wrapping, |
848 | random_state *rs, int startx, int starty, int startd) |
849 | { |
850 | struct xyd *perimeter, *perim2, *loop[2], looppos[2]; |
851 | int nperim, perimsize, nloop[2], loopsize[2]; |
852 | int x, y, d, i; |
853 | |
854 | /* |
855 | * We know that the tile at (startx,starty) is part of an |
856 | * ambiguous section, and we also know that its neighbour in |
857 | * direction startd is fully specified. We begin by tracing all |
858 | * the way round the ambiguous area. |
859 | */ |
860 | nperim = perimsize = 0; |
861 | perimeter = NULL; |
862 | x = startx; |
863 | y = starty; |
864 | d = startd; |
865 | #ifdef PERTURB_DIAGNOSTICS |
866 | printf("perturb %d,%d:%d\n", x, y, d); |
867 | #endif |
868 | do { |
869 | int x2, y2, d2; |
870 | |
871 | if (nperim >= perimsize) { |
872 | perimsize = perimsize * 3 / 2 + 32; |
873 | perimeter = sresize(perimeter, perimsize, struct xyd); |
874 | } |
875 | perimeter[nperim].x = x; |
876 | perimeter[nperim].y = y; |
877 | perimeter[nperim].direction = d; |
878 | nperim++; |
879 | #ifdef PERTURB_DIAGNOSTICS |
880 | printf("perimeter: %d,%d:%d\n", x, y, d); |
881 | #endif |
882 | |
883 | /* |
884 | * First, see if we can simply turn left from where we are |
885 | * and find another locked square. |
886 | */ |
887 | d2 = A(d); |
888 | OFFSETWH(x2, y2, x, y, d2, w, h); |
889 | if ((!wrapping && (abs(x2-x) > 1 || abs(y2-y) > 1)) || |
890 | (tiles[y2*w+x2] & LOCKED)) { |
891 | d = d2; |
892 | } else { |
893 | /* |
894 | * Failing that, step left into the new square and look |
895 | * in front of us. |
896 | */ |
897 | x = x2; |
898 | y = y2; |
899 | OFFSETWH(x2, y2, x, y, d, w, h); |
900 | if ((wrapping || (abs(x2-x) <= 1 && abs(y2-y) <= 1)) && |
901 | !(tiles[y2*w+x2] & LOCKED)) { |
902 | /* |
903 | * And failing _that_, we're going to have to step |
904 | * forward into _that_ square and look right at the |
905 | * same locked square as we started with. |
906 | */ |
907 | x = x2; |
908 | y = y2; |
909 | d = C(d); |
910 | } |
911 | } |
912 | |
913 | } while (x != startx || y != starty || d != startd); |
914 | |
915 | /* |
916 | * Our technique for perturbing this ambiguous area is to |
917 | * search round its edge for a join we can make: that is, an |
918 | * edge on the perimeter which is (a) not currently connected, |
919 | * and (b) connecting it would not yield a full cross on either |
920 | * side. Then we make that join, search round the network to |
921 | * find the loop thus constructed, and sever the loop at a |
922 | * randomly selected other point. |
923 | */ |
924 | perim2 = snewn(nperim, struct xyd); |
925 | memcpy(perim2, perimeter, nperim * sizeof(struct xyd)); |
926 | /* Shuffle the perimeter, so as to search it without directional bias. */ |
927 | for (i = nperim; --i ;) { |
928 | int j = random_upto(rs, i+1); |
929 | struct xyd t; |
930 | |
931 | t = perim2[j]; |
932 | perim2[j] = perim2[i]; |
933 | perim2[i] = t; |
934 | } |
935 | for (i = 0; i < nperim; i++) { |
936 | int x2, y2; |
937 | |
938 | x = perim2[i].x; |
939 | y = perim2[i].y; |
940 | d = perim2[i].direction; |
941 | |
942 | OFFSETWH(x2, y2, x, y, d, w, h); |
943 | if (!wrapping && (abs(x2-x) > 1 || abs(y2-y) > 1)) |
944 | continue; /* can't link across non-wrapping border */ |
945 | if (tiles[y*w+x] & d) |
946 | continue; /* already linked in this direction! */ |
947 | if (((tiles[y*w+x] | d) & 15) == 15) |
948 | continue; /* can't turn this tile into a cross */ |
949 | if (((tiles[y2*w+x2] | F(d)) & 15) == 15) |
950 | continue; /* can't turn other tile into a cross */ |
951 | |
952 | /* |
953 | * We've found the point at which we're going to make a new |
954 | * link. |
955 | */ |
956 | #ifdef PERTURB_DIAGNOSTICS |
957 | printf("linking %d,%d:%d\n", x, y, d); |
958 | #endif |
959 | tiles[y*w+x] |= d; |
960 | tiles[y2*w+x2] |= F(d); |
961 | |
962 | break; |
963 | } |
ab53eb64 |
964 | sfree(perim2); |
c0edd11f |
965 | |
966 | if (i == nperim) |
967 | return; /* nothing we can do! */ |
968 | |
969 | /* |
970 | * Now we've constructed a new link, we need to find the entire |
971 | * loop of which it is a part. |
972 | * |
973 | * In principle, this involves doing a complete search round |
974 | * the network. However, I anticipate that in the vast majority |
975 | * of cases the loop will be quite small, so what I'm going to |
976 | * do is make _two_ searches round the network in parallel, one |
977 | * keeping its metaphorical hand on the left-hand wall while |
978 | * the other keeps its hand on the right. As soon as one of |
979 | * them gets back to its starting point, I abandon the other. |
980 | */ |
981 | for (i = 0; i < 2; i++) { |
982 | loopsize[i] = nloop[i] = 0; |
983 | loop[i] = NULL; |
984 | looppos[i].x = x; |
985 | looppos[i].y = y; |
986 | looppos[i].direction = d; |
987 | } |
988 | while (1) { |
989 | for (i = 0; i < 2; i++) { |
990 | int x2, y2, j; |
991 | |
992 | x = looppos[i].x; |
993 | y = looppos[i].y; |
994 | d = looppos[i].direction; |
995 | |
996 | OFFSETWH(x2, y2, x, y, d, w, h); |
997 | |
998 | /* |
999 | * Add this path segment to the loop, unless it exactly |
1000 | * reverses the previous one on the loop in which case |
1001 | * we take it away again. |
1002 | */ |
1003 | #ifdef PERTURB_DIAGNOSTICS |
1004 | printf("looppos[%d] = %d,%d:%d\n", i, x, y, d); |
1005 | #endif |
1006 | if (nloop[i] > 0 && |
1007 | loop[i][nloop[i]-1].x == x2 && |
1008 | loop[i][nloop[i]-1].y == y2 && |
1009 | loop[i][nloop[i]-1].direction == F(d)) { |
1010 | #ifdef PERTURB_DIAGNOSTICS |
1011 | printf("removing path segment %d,%d:%d from loop[%d]\n", |
1012 | x2, y2, F(d), i); |
1013 | #endif |
1014 | nloop[i]--; |
1015 | } else { |
1016 | if (nloop[i] >= loopsize[i]) { |
1017 | loopsize[i] = loopsize[i] * 3 / 2 + 32; |
1018 | loop[i] = sresize(loop[i], loopsize[i], struct xyd); |
1019 | } |
1020 | #ifdef PERTURB_DIAGNOSTICS |
1021 | printf("adding path segment %d,%d:%d to loop[%d]\n", |
1022 | x, y, d, i); |
1023 | #endif |
1024 | loop[i][nloop[i]++] = looppos[i]; |
1025 | } |
1026 | |
1027 | #ifdef PERTURB_DIAGNOSTICS |
1028 | printf("tile at new location is %x\n", tiles[y2*w+x2] & 0xF); |
1029 | #endif |
1030 | d = F(d); |
1031 | for (j = 0; j < 4; j++) { |
1032 | if (i == 0) |
1033 | d = A(d); |
1034 | else |
1035 | d = C(d); |
1036 | #ifdef PERTURB_DIAGNOSTICS |
1037 | printf("trying dir %d\n", d); |
1038 | #endif |
1039 | if (tiles[y2*w+x2] & d) { |
1040 | looppos[i].x = x2; |
1041 | looppos[i].y = y2; |
1042 | looppos[i].direction = d; |
1043 | break; |
1044 | } |
1045 | } |
1046 | |
1047 | assert(j < 4); |
1048 | assert(nloop[i] > 0); |
1049 | |
1050 | if (looppos[i].x == loop[i][0].x && |
1051 | looppos[i].y == loop[i][0].y && |
1052 | looppos[i].direction == loop[i][0].direction) { |
1053 | #ifdef PERTURB_DIAGNOSTICS |
1054 | printf("loop %d finished tracking\n", i); |
1055 | #endif |
1056 | |
1057 | /* |
1058 | * Having found our loop, we now sever it at a |
1059 | * randomly chosen point - absolutely any will do - |
1060 | * which is not the one we joined it at to begin |
1061 | * with. Conveniently, the one we joined it at is |
1062 | * loop[i][0], so we just avoid that one. |
1063 | */ |
1064 | j = random_upto(rs, nloop[i]-1) + 1; |
1065 | x = loop[i][j].x; |
1066 | y = loop[i][j].y; |
1067 | d = loop[i][j].direction; |
1068 | OFFSETWH(x2, y2, x, y, d, w, h); |
1069 | tiles[y*w+x] &= ~d; |
1070 | tiles[y2*w+x2] &= ~F(d); |
1071 | |
1072 | break; |
1073 | } |
1074 | } |
1075 | if (i < 2) |
1076 | break; |
1077 | } |
1078 | sfree(loop[0]); |
1079 | sfree(loop[1]); |
1080 | |
1081 | /* |
1082 | * Finally, we must mark the entire disputed section as locked, |
1083 | * to prevent the perturb function being called on it multiple |
1084 | * times. |
1085 | * |
1086 | * To do this, we _sort_ the perimeter of the area. The |
1087 | * existing xyd_cmp function will arrange things into columns |
1088 | * for us, in such a way that each column has the edges in |
1089 | * vertical order. Then we can work down each column and fill |
1090 | * in all the squares between an up edge and a down edge. |
1091 | */ |
1092 | qsort(perimeter, nperim, sizeof(struct xyd), xyd_cmp); |
1093 | x = y = -1; |
1094 | for (i = 0; i <= nperim; i++) { |
1095 | if (i == nperim || perimeter[i].x > x) { |
1096 | /* |
1097 | * Fill in everything from the last Up edge to the |
1098 | * bottom of the grid, if necessary. |
1099 | */ |
1100 | if (x != -1) { |
1101 | while (y < h) { |
1102 | #ifdef PERTURB_DIAGNOSTICS |
1103 | printf("resolved: locking tile %d,%d\n", x, y); |
1104 | #endif |
1105 | tiles[y * w + x] |= LOCKED; |
1106 | y++; |
1107 | } |
1108 | x = y = -1; |
1109 | } |
1110 | |
1111 | if (i == nperim) |
1112 | break; |
1113 | |
1114 | x = perimeter[i].x; |
1115 | y = 0; |
1116 | } |
1117 | |
1118 | if (perimeter[i].direction == U) { |
1119 | x = perimeter[i].x; |
1120 | y = perimeter[i].y; |
1121 | } else if (perimeter[i].direction == D) { |
1122 | /* |
1123 | * Fill in everything from the last Up edge to here. |
1124 | */ |
1125 | assert(x == perimeter[i].x && y <= perimeter[i].y); |
1126 | while (y <= perimeter[i].y) { |
1127 | #ifdef PERTURB_DIAGNOSTICS |
1128 | printf("resolved: locking tile %d,%d\n", x, y); |
1129 | #endif |
1130 | tiles[y * w + x] |= LOCKED; |
1131 | y++; |
1132 | } |
1133 | x = y = -1; |
1134 | } |
1135 | } |
1136 | |
1137 | sfree(perimeter); |
1138 | } |
1139 | |
1185e3c5 |
1140 | static char *new_game_desc(game_params *params, random_state *rs, |
6aa6af4c |
1141 | game_aux_info **aux, int interactive) |
720a8fb7 |
1142 | { |
1185e3c5 |
1143 | tree234 *possibilities, *barriertree; |
1144 | int w, h, x, y, cx, cy, nbarriers; |
1145 | unsigned char *tiles, *barriers; |
1146 | char *desc, *p; |
6f2d8d7c |
1147 | |
1185e3c5 |
1148 | w = params->width; |
1149 | h = params->height; |
720a8fb7 |
1150 | |
c0edd11f |
1151 | cx = w / 2; |
1152 | cy = h / 2; |
1153 | |
1185e3c5 |
1154 | tiles = snewn(w * h, unsigned char); |
1185e3c5 |
1155 | barriers = snewn(w * h, unsigned char); |
720a8fb7 |
1156 | |
c0edd11f |
1157 | begin_generation: |
1158 | |
1159 | memset(tiles, 0, w * h); |
1160 | memset(barriers, 0, w * h); |
720a8fb7 |
1161 | |
1162 | /* |
1163 | * Construct the unshuffled grid. |
1164 | * |
1165 | * To do this, we simply start at the centre point, repeatedly |
1166 | * choose a random possibility out of the available ways to |
1167 | * extend a used square into an unused one, and do it. After |
1168 | * extending the third line out of a square, we remove the |
1169 | * fourth from the possibilities list to avoid any full-cross |
1170 | * squares (which would make the game too easy because they |
1171 | * only have one orientation). |
1172 | * |
1173 | * The slightly worrying thing is the avoidance of full-cross |
1174 | * squares. Can this cause our unsophisticated construction |
1175 | * algorithm to paint itself into a corner, by getting into a |
1176 | * situation where there are some unreached squares and the |
1177 | * only way to reach any of them is to extend a T-piece into a |
1178 | * full cross? |
1179 | * |
1180 | * Answer: no it can't, and here's a proof. |
1181 | * |
1182 | * Any contiguous group of such unreachable squares must be |
1183 | * surrounded on _all_ sides by T-pieces pointing away from the |
1184 | * group. (If not, then there is a square which can be extended |
1185 | * into one of the `unreachable' ones, and so it wasn't |
1186 | * unreachable after all.) In particular, this implies that |
1187 | * each contiguous group of unreachable squares must be |
1188 | * rectangular in shape (any deviation from that yields a |
1189 | * non-T-piece next to an `unreachable' square). |
1190 | * |
1191 | * So we have a rectangle of unreachable squares, with T-pieces |
1192 | * forming a solid border around the rectangle. The corners of |
1193 | * that border must be connected (since every tile connects all |
1194 | * the lines arriving in it), and therefore the border must |
1195 | * form a closed loop around the rectangle. |
1196 | * |
1197 | * But this can't have happened in the first place, since we |
1198 | * _know_ we've avoided creating closed loops! Hence, no such |
1199 | * situation can ever arise, and the naive grid construction |
1200 | * algorithm will guaranteeably result in a complete grid |
1201 | * containing no unreached squares, no full crosses _and_ no |
1202 | * closed loops. [] |
1203 | */ |
c0edd11f |
1204 | possibilities = newtree234(xyd_cmp_nc); |
ecadce0d |
1205 | |
1185e3c5 |
1206 | if (cx+1 < w) |
1207 | add234(possibilities, new_xyd(cx, cy, R)); |
1208 | if (cy-1 >= 0) |
1209 | add234(possibilities, new_xyd(cx, cy, U)); |
1210 | if (cx-1 >= 0) |
1211 | add234(possibilities, new_xyd(cx, cy, L)); |
1212 | if (cy+1 < h) |
1213 | add234(possibilities, new_xyd(cx, cy, D)); |
720a8fb7 |
1214 | |
1215 | while (count234(possibilities) > 0) { |
1216 | int i; |
1217 | struct xyd *xyd; |
1218 | int x1, y1, d1, x2, y2, d2, d; |
1219 | |
1220 | /* |
1221 | * Extract a randomly chosen possibility from the list. |
1222 | */ |
1223 | i = random_upto(rs, count234(possibilities)); |
1224 | xyd = delpos234(possibilities, i); |
1225 | x1 = xyd->x; |
1226 | y1 = xyd->y; |
1227 | d1 = xyd->direction; |
1228 | sfree(xyd); |
1229 | |
1185e3c5 |
1230 | OFFSET(x2, y2, x1, y1, d1, params); |
720a8fb7 |
1231 | d2 = F(d1); |
1232 | #ifdef DEBUG |
1233 | printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n", |
1234 | x1, y1, "0RU3L567D9abcdef"[d1], x2, y2, "0RU3L567D9abcdef"[d2]); |
1235 | #endif |
1236 | |
1237 | /* |
1238 | * Make the connection. (We should be moving to an as yet |
1239 | * unused tile.) |
1240 | */ |
1185e3c5 |
1241 | index(params, tiles, x1, y1) |= d1; |
1242 | assert(index(params, tiles, x2, y2) == 0); |
1243 | index(params, tiles, x2, y2) |= d2; |
720a8fb7 |
1244 | |
1245 | /* |
1246 | * If we have created a T-piece, remove its last |
1247 | * possibility. |
1248 | */ |
1185e3c5 |
1249 | if (COUNT(index(params, tiles, x1, y1)) == 3) { |
720a8fb7 |
1250 | struct xyd xyd1, *xydp; |
1251 | |
1252 | xyd1.x = x1; |
1253 | xyd1.y = y1; |
1185e3c5 |
1254 | xyd1.direction = 0x0F ^ index(params, tiles, x1, y1); |
720a8fb7 |
1255 | |
1256 | xydp = find234(possibilities, &xyd1, NULL); |
1257 | |
1258 | if (xydp) { |
1259 | #ifdef DEBUG |
1260 | printf("T-piece; removing (%d,%d,%c)\n", |
1261 | xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]); |
1262 | #endif |
1263 | del234(possibilities, xydp); |
1264 | sfree(xydp); |
1265 | } |
1266 | } |
1267 | |
1268 | /* |
1269 | * Remove all other possibilities that were pointing at the |
1270 | * tile we've just moved into. |
1271 | */ |
1272 | for (d = 1; d < 0x10; d <<= 1) { |
1273 | int x3, y3, d3; |
1274 | struct xyd xyd1, *xydp; |
1275 | |
1185e3c5 |
1276 | OFFSET(x3, y3, x2, y2, d, params); |
720a8fb7 |
1277 | d3 = F(d); |
1278 | |
1279 | xyd1.x = x3; |
1280 | xyd1.y = y3; |
1281 | xyd1.direction = d3; |
1282 | |
1283 | xydp = find234(possibilities, &xyd1, NULL); |
1284 | |
1285 | if (xydp) { |
1286 | #ifdef DEBUG |
1287 | printf("Loop avoidance; removing (%d,%d,%c)\n", |
1288 | xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]); |
1289 | #endif |
1290 | del234(possibilities, xydp); |
1291 | sfree(xydp); |
1292 | } |
1293 | } |
1294 | |
1295 | /* |
1296 | * Add new possibilities to the list for moving _out_ of |
1297 | * the tile we have just moved into. |
1298 | */ |
1299 | for (d = 1; d < 0x10; d <<= 1) { |
1300 | int x3, y3; |
1301 | |
1302 | if (d == d2) |
1303 | continue; /* we've got this one already */ |
1304 | |
1185e3c5 |
1305 | if (!params->wrapping) { |
720a8fb7 |
1306 | if (d == U && y2 == 0) |
1307 | continue; |
1185e3c5 |
1308 | if (d == D && y2 == h-1) |
720a8fb7 |
1309 | continue; |
1310 | if (d == L && x2 == 0) |
1311 | continue; |
1185e3c5 |
1312 | if (d == R && x2 == w-1) |
720a8fb7 |
1313 | continue; |
1314 | } |
1315 | |
1185e3c5 |
1316 | OFFSET(x3, y3, x2, y2, d, params); |
720a8fb7 |
1317 | |
1185e3c5 |
1318 | if (index(params, tiles, x3, y3)) |
720a8fb7 |
1319 | continue; /* this would create a loop */ |
1320 | |
1321 | #ifdef DEBUG |
1322 | printf("New frontier; adding (%d,%d,%c)\n", |
1323 | x2, y2, "0RU3L567D9abcdef"[d]); |
1324 | #endif |
1325 | add234(possibilities, new_xyd(x2, y2, d)); |
1326 | } |
1327 | } |
1328 | /* Having done that, we should have no possibilities remaining. */ |
1329 | assert(count234(possibilities) == 0); |
1330 | freetree234(possibilities); |
1331 | |
c0edd11f |
1332 | if (params->unique) { |
1333 | int prevn = -1; |
1334 | |
1335 | /* |
1336 | * Run the solver to check unique solubility. |
1337 | */ |
84942c65 |
1338 | while (!net_solver(w, h, tiles, NULL, params->wrapping)) { |
c0edd11f |
1339 | int n = 0; |
1340 | |
1341 | /* |
1342 | * We expect (in most cases) that most of the grid will |
1343 | * be uniquely specified already, and the remaining |
1344 | * ambiguous sections will be small and separate. So |
1345 | * our strategy is to find each individual such |
1346 | * section, and perform a perturbation on the network |
1347 | * in that area. |
1348 | */ |
1349 | for (y = 0; y < h; y++) for (x = 0; x < w; x++) { |
1350 | if (x+1 < w && ((tiles[y*w+x] ^ tiles[y*w+x+1]) & LOCKED)) { |
1351 | n++; |
1352 | if (tiles[y*w+x] & LOCKED) |
1353 | perturb(w, h, tiles, params->wrapping, rs, x+1, y, L); |
1354 | else |
1355 | perturb(w, h, tiles, params->wrapping, rs, x, y, R); |
1356 | } |
1357 | if (y+1 < h && ((tiles[y*w+x] ^ tiles[(y+1)*w+x]) & LOCKED)) { |
1358 | n++; |
1359 | if (tiles[y*w+x] & LOCKED) |
1360 | perturb(w, h, tiles, params->wrapping, rs, x, y+1, U); |
1361 | else |
1362 | perturb(w, h, tiles, params->wrapping, rs, x, y, D); |
1363 | } |
1364 | } |
1365 | |
1366 | /* |
1367 | * Now n counts the number of ambiguous sections we |
1368 | * have fiddled with. If we haven't managed to decrease |
1369 | * it from the last time we ran the solver, give up and |
1370 | * regenerate the entire grid. |
1371 | */ |
1372 | if (prevn != -1 && prevn <= n) |
1373 | goto begin_generation; /* (sorry) */ |
1374 | |
1375 | prevn = n; |
1376 | } |
1377 | |
1378 | /* |
1379 | * The solver will have left a lot of LOCKED bits lying |
1380 | * around in the tiles array. Remove them. |
1381 | */ |
1382 | for (x = 0; x < w*h; x++) |
1383 | tiles[x] &= ~LOCKED; |
1384 | } |
1385 | |
720a8fb7 |
1386 | /* |
1387 | * Now compute a list of the possible barrier locations. |
1388 | */ |
c0edd11f |
1389 | barriertree = newtree234(xyd_cmp_nc); |
1185e3c5 |
1390 | for (y = 0; y < h; y++) { |
1391 | for (x = 0; x < w; x++) { |
1392 | |
1393 | if (!(index(params, tiles, x, y) & R) && |
1394 | (params->wrapping || x < w-1)) |
1395 | add234(barriertree, new_xyd(x, y, R)); |
1396 | if (!(index(params, tiles, x, y) & D) && |
1397 | (params->wrapping || y < h-1)) |
1398 | add234(barriertree, new_xyd(x, y, D)); |
720a8fb7 |
1399 | } |
1400 | } |
1401 | |
1402 | /* |
1185e3c5 |
1403 | * Save the unshuffled grid in an aux_info. |
2ac6d24e |
1404 | */ |
1405 | { |
1185e3c5 |
1406 | game_aux_info *solution; |
2ac6d24e |
1407 | |
1185e3c5 |
1408 | solution = snew(game_aux_info); |
1409 | solution->width = w; |
1410 | solution->height = h; |
1411 | solution->tiles = snewn(w * h, unsigned char); |
1412 | memcpy(solution->tiles, tiles, w * h); |
2ac6d24e |
1413 | |
1185e3c5 |
1414 | *aux = solution; |
2ac6d24e |
1415 | } |
1416 | |
1417 | /* |
720a8fb7 |
1418 | * Now shuffle the grid. |
1419 | */ |
1185e3c5 |
1420 | for (y = 0; y < h; y++) { |
1421 | for (x = 0; x < w; x++) { |
1422 | int orig = index(params, tiles, x, y); |
720a8fb7 |
1423 | int rot = random_upto(rs, 4); |
1185e3c5 |
1424 | index(params, tiles, x, y) = ROT(orig, rot); |
720a8fb7 |
1425 | } |
1426 | } |
1427 | |
1428 | /* |
1429 | * And now choose barrier locations. (We carefully do this |
1430 | * _after_ shuffling, so that changing the barrier rate in the |
1185e3c5 |
1431 | * params while keeping the random seed the same will give the |
720a8fb7 |
1432 | * same shuffled grid and _only_ change the barrier locations. |
1433 | * Also the way we choose barrier locations, by repeatedly |
1434 | * choosing one possibility from the list until we have enough, |
1435 | * is designed to ensure that raising the barrier rate while |
1436 | * keeping the seed the same will provide a superset of the |
1437 | * previous barrier set - i.e. if you ask for 10 barriers, and |
1438 | * then decide that's still too hard and ask for 20, you'll get |
1439 | * the original 10 plus 10 more, rather than getting 20 new |
1440 | * ones and the chance of remembering your first 10.) |
1441 | */ |
1185e3c5 |
1442 | nbarriers = (int)(params->barrier_probability * count234(barriertree)); |
1443 | assert(nbarriers >= 0 && nbarriers <= count234(barriertree)); |
720a8fb7 |
1444 | |
1445 | while (nbarriers > 0) { |
1446 | int i; |
1447 | struct xyd *xyd; |
1448 | int x1, y1, d1, x2, y2, d2; |
1449 | |
1450 | /* |
1451 | * Extract a randomly chosen barrier from the list. |
1452 | */ |
1185e3c5 |
1453 | i = random_upto(rs, count234(barriertree)); |
1454 | xyd = delpos234(barriertree, i); |
720a8fb7 |
1455 | |
1456 | assert(xyd != NULL); |
1457 | |
1458 | x1 = xyd->x; |
1459 | y1 = xyd->y; |
1460 | d1 = xyd->direction; |
1461 | sfree(xyd); |
1462 | |
1185e3c5 |
1463 | OFFSET(x2, y2, x1, y1, d1, params); |
720a8fb7 |
1464 | d2 = F(d1); |
1465 | |
1185e3c5 |
1466 | index(params, barriers, x1, y1) |= d1; |
1467 | index(params, barriers, x2, y2) |= d2; |
720a8fb7 |
1468 | |
1469 | nbarriers--; |
1470 | } |
1471 | |
1472 | /* |
1473 | * Clean up the rest of the barrier list. |
1474 | */ |
1475 | { |
1476 | struct xyd *xyd; |
1477 | |
1185e3c5 |
1478 | while ( (xyd = delpos234(barriertree, 0)) != NULL) |
720a8fb7 |
1479 | sfree(xyd); |
1480 | |
1185e3c5 |
1481 | freetree234(barriertree); |
1482 | } |
1483 | |
1484 | /* |
1485 | * Finally, encode the grid into a string game description. |
1486 | * |
1487 | * My syntax is extremely simple: each square is encoded as a |
1488 | * hex digit in which bit 0 means a connection on the right, |
1489 | * bit 1 means up, bit 2 left and bit 3 down. (i.e. the same |
1490 | * encoding as used internally). Each digit is followed by |
1491 | * optional barrier indicators: `v' means a vertical barrier to |
1492 | * the right of it, and `h' means a horizontal barrier below |
1493 | * it. |
1494 | */ |
1495 | desc = snewn(w * h * 3 + 1, char); |
1496 | p = desc; |
1497 | for (y = 0; y < h; y++) { |
1498 | for (x = 0; x < w; x++) { |
1499 | *p++ = "0123456789abcdef"[index(params, tiles, x, y)]; |
1500 | if ((params->wrapping || x < w-1) && |
1501 | (index(params, barriers, x, y) & R)) |
1502 | *p++ = 'v'; |
1503 | if ((params->wrapping || y < h-1) && |
1504 | (index(params, barriers, x, y) & D)) |
1505 | *p++ = 'h'; |
1506 | } |
1507 | } |
1508 | assert(p - desc <= w*h*3); |
366d045b |
1509 | *p = '\0'; |
1185e3c5 |
1510 | |
1511 | sfree(tiles); |
1512 | sfree(barriers); |
1513 | |
1514 | return desc; |
1515 | } |
1516 | |
1517 | static void game_free_aux_info(game_aux_info *aux) |
1518 | { |
1519 | sfree(aux->tiles); |
1520 | sfree(aux); |
1521 | } |
1522 | |
1523 | static char *validate_desc(game_params *params, char *desc) |
1524 | { |
1525 | int w = params->width, h = params->height; |
1526 | int i; |
1527 | |
1528 | for (i = 0; i < w*h; i++) { |
1529 | if (*desc >= '0' && *desc <= '9') |
1530 | /* OK */; |
1531 | else if (*desc >= 'a' && *desc <= 'f') |
1532 | /* OK */; |
1533 | else if (*desc >= 'A' && *desc <= 'F') |
1534 | /* OK */; |
1535 | else if (!*desc) |
1536 | return "Game description shorter than expected"; |
1537 | else |
1538 | return "Game description contained unexpected character"; |
1539 | desc++; |
1540 | while (*desc == 'h' || *desc == 'v') |
1541 | desc++; |
1542 | } |
1543 | if (*desc) |
1544 | return "Game description longer than expected"; |
1545 | |
1546 | return NULL; |
1547 | } |
1548 | |
1549 | /* ---------------------------------------------------------------------- |
1550 | * Construct an initial game state, given a description and parameters. |
1551 | */ |
1552 | |
c380832d |
1553 | static game_state *new_game(midend_data *me, game_params *params, char *desc) |
1185e3c5 |
1554 | { |
1555 | game_state *state; |
1556 | int w, h, x, y; |
1557 | |
1558 | assert(params->width > 0 && params->height > 0); |
1559 | assert(params->width > 1 || params->height > 1); |
1560 | |
1561 | /* |
1562 | * Create a blank game state. |
1563 | */ |
1564 | state = snew(game_state); |
1565 | w = state->width = params->width; |
1566 | h = state->height = params->height; |
1185e3c5 |
1567 | state->wrapping = params->wrapping; |
1568 | state->last_rotate_dir = state->last_rotate_x = state->last_rotate_y = 0; |
1569 | state->completed = state->used_solve = state->just_used_solve = FALSE; |
1570 | state->tiles = snewn(state->width * state->height, unsigned char); |
1571 | memset(state->tiles, 0, state->width * state->height); |
1572 | state->barriers = snewn(state->width * state->height, unsigned char); |
1573 | memset(state->barriers, 0, state->width * state->height); |
1574 | |
1575 | /* |
1576 | * Parse the game description into the grid. |
1577 | */ |
1578 | for (y = 0; y < h; y++) { |
1579 | for (x = 0; x < w; x++) { |
1580 | if (*desc >= '0' && *desc <= '9') |
1581 | tile(state, x, y) = *desc - '0'; |
1582 | else if (*desc >= 'a' && *desc <= 'f') |
1583 | tile(state, x, y) = *desc - 'a' + 10; |
1584 | else if (*desc >= 'A' && *desc <= 'F') |
1585 | tile(state, x, y) = *desc - 'A' + 10; |
1586 | if (*desc) |
1587 | desc++; |
1588 | while (*desc == 'h' || *desc == 'v') { |
1589 | int x2, y2, d1, d2; |
1590 | if (*desc == 'v') |
1591 | d1 = R; |
1592 | else |
1593 | d1 = D; |
1594 | |
1595 | OFFSET(x2, y2, x, y, d1, state); |
1596 | d2 = F(d1); |
1597 | |
1598 | barrier(state, x, y) |= d1; |
1599 | barrier(state, x2, y2) |= d2; |
1600 | |
1601 | desc++; |
1602 | } |
1603 | } |
1604 | } |
1605 | |
1606 | /* |
1607 | * Set up border barriers if this is a non-wrapping game. |
1608 | */ |
1609 | if (!state->wrapping) { |
1610 | for (x = 0; x < state->width; x++) { |
1611 | barrier(state, x, 0) |= U; |
1612 | barrier(state, x, state->height-1) |= D; |
1613 | } |
1614 | for (y = 0; y < state->height; y++) { |
1615 | barrier(state, 0, y) |= L; |
1616 | barrier(state, state->width-1, y) |= R; |
1617 | } |
f0ee053c |
1618 | } else { |
1619 | /* |
1620 | * We check whether this is de-facto a non-wrapping game |
1621 | * despite the parameters, in case we were passed the |
1622 | * description of a non-wrapping game. This is so that we |
1623 | * can change some aspects of the UI behaviour. |
1624 | */ |
1625 | state->wrapping = FALSE; |
1626 | for (x = 0; x < state->width; x++) |
1627 | if (!(barrier(state, x, 0) & U) || |
1628 | !(barrier(state, x, state->height-1) & D)) |
1629 | state->wrapping = TRUE; |
1630 | for (y = 0; y < state->width; y++) |
1631 | if (!(barrier(state, 0, y) & L) || |
1632 | !(barrier(state, state->width-1, y) & R)) |
1633 | state->wrapping = TRUE; |
720a8fb7 |
1634 | } |
1635 | |
720a8fb7 |
1636 | return state; |
1637 | } |
1638 | |
be8d5aa1 |
1639 | static game_state *dup_game(game_state *state) |
720a8fb7 |
1640 | { |
1641 | game_state *ret; |
1642 | |
1643 | ret = snew(game_state); |
1644 | ret->width = state->width; |
1645 | ret->height = state->height; |
1646 | ret->wrapping = state->wrapping; |
1647 | ret->completed = state->completed; |
2ac6d24e |
1648 | ret->used_solve = state->used_solve; |
1649 | ret->just_used_solve = state->just_used_solve; |
2ef96bd6 |
1650 | ret->last_rotate_dir = state->last_rotate_dir; |
1185e3c5 |
1651 | ret->last_rotate_x = state->last_rotate_x; |
1652 | ret->last_rotate_y = state->last_rotate_y; |
720a8fb7 |
1653 | ret->tiles = snewn(state->width * state->height, unsigned char); |
1654 | memcpy(ret->tiles, state->tiles, state->width * state->height); |
1655 | ret->barriers = snewn(state->width * state->height, unsigned char); |
1656 | memcpy(ret->barriers, state->barriers, state->width * state->height); |
1657 | |
1658 | return ret; |
1659 | } |
1660 | |
be8d5aa1 |
1661 | static void free_game(game_state *state) |
720a8fb7 |
1662 | { |
1663 | sfree(state->tiles); |
1664 | sfree(state->barriers); |
1665 | sfree(state); |
1666 | } |
1667 | |
2ac6d24e |
1668 | static game_state *solve_game(game_state *state, game_aux_info *aux, |
1669 | char **error) |
1670 | { |
1671 | game_state *ret; |
1672 | |
1185e3c5 |
1673 | if (!aux) { |
c0edd11f |
1674 | /* |
1675 | * Run the internal solver on the provided grid. This might |
1676 | * not yield a complete solution. |
1677 | */ |
1678 | ret = dup_game(state); |
84942c65 |
1679 | net_solver(ret->width, ret->height, ret->tiles, |
1680 | ret->barriers, ret->wrapping); |
c0edd11f |
1681 | } else { |
1682 | assert(aux->width == state->width); |
1683 | assert(aux->height == state->height); |
1684 | ret = dup_game(state); |
1685 | memcpy(ret->tiles, aux->tiles, ret->width * ret->height); |
1686 | ret->used_solve = ret->just_used_solve = TRUE; |
1687 | ret->completed = TRUE; |
2ac6d24e |
1688 | } |
1689 | |
2ac6d24e |
1690 | return ret; |
1691 | } |
1692 | |
9b4b03d3 |
1693 | static char *game_text_format(game_state *state) |
1694 | { |
1695 | return NULL; |
1696 | } |
1697 | |
720a8fb7 |
1698 | /* ---------------------------------------------------------------------- |
1699 | * Utility routine. |
1700 | */ |
1701 | |
1702 | /* |
1703 | * Compute which squares are reachable from the centre square, as a |
1704 | * quick visual aid to determining how close the game is to |
1705 | * completion. This is also a simple way to tell if the game _is_ |
1706 | * completed - just call this function and see whether every square |
1707 | * is marked active. |
1708 | */ |
f0ee053c |
1709 | static unsigned char *compute_active(game_state *state, int cx, int cy) |
720a8fb7 |
1710 | { |
1711 | unsigned char *active; |
1712 | tree234 *todo; |
1713 | struct xyd *xyd; |
1714 | |
1715 | active = snewn(state->width * state->height, unsigned char); |
1716 | memset(active, 0, state->width * state->height); |
1717 | |
1718 | /* |
1719 | * We only store (x,y) pairs in todo, but it's easier to reuse |
1720 | * xyd_cmp and just store direction 0 every time. |
1721 | */ |
c0edd11f |
1722 | todo = newtree234(xyd_cmp_nc); |
f0ee053c |
1723 | index(state, active, cx, cy) = ACTIVE; |
1724 | add234(todo, new_xyd(cx, cy, 0)); |
720a8fb7 |
1725 | |
1726 | while ( (xyd = delpos234(todo, 0)) != NULL) { |
1727 | int x1, y1, d1, x2, y2, d2; |
1728 | |
1729 | x1 = xyd->x; |
1730 | y1 = xyd->y; |
1731 | sfree(xyd); |
1732 | |
1733 | for (d1 = 1; d1 < 0x10; d1 <<= 1) { |
1734 | OFFSET(x2, y2, x1, y1, d1, state); |
1735 | d2 = F(d1); |
1736 | |
1737 | /* |
1738 | * If the next tile in this direction is connected to |
1739 | * us, and there isn't a barrier in the way, and it |
1740 | * isn't already marked active, then mark it active and |
1741 | * add it to the to-examine list. |
1742 | */ |
1743 | if ((tile(state, x1, y1) & d1) && |
1744 | (tile(state, x2, y2) & d2) && |
1745 | !(barrier(state, x1, y1) & d1) && |
1746 | !index(state, active, x2, y2)) { |
2ef96bd6 |
1747 | index(state, active, x2, y2) = ACTIVE; |
720a8fb7 |
1748 | add234(todo, new_xyd(x2, y2, 0)); |
1749 | } |
1750 | } |
1751 | } |
1752 | /* Now we expect the todo list to have shrunk to zero size. */ |
1753 | assert(count234(todo) == 0); |
1754 | freetree234(todo); |
1755 | |
1756 | return active; |
1757 | } |
1758 | |
66164171 |
1759 | struct game_ui { |
f0ee053c |
1760 | int org_x, org_y; /* origin */ |
1761 | int cx, cy; /* source tile (game coordinates) */ |
66164171 |
1762 | int cur_x, cur_y; |
1763 | int cur_visible; |
cbb5549e |
1764 | random_state *rs; /* used for jumbling */ |
66164171 |
1765 | }; |
1766 | |
be8d5aa1 |
1767 | static game_ui *new_ui(game_state *state) |
74a4e547 |
1768 | { |
cbb5549e |
1769 | void *seed; |
1770 | int seedsize; |
66164171 |
1771 | game_ui *ui = snew(game_ui); |
f0ee053c |
1772 | ui->org_x = ui->org_y = 0; |
1773 | ui->cur_x = ui->cx = state->width / 2; |
1774 | ui->cur_y = ui->cy = state->height / 2; |
66164171 |
1775 | ui->cur_visible = FALSE; |
cbb5549e |
1776 | get_random_seed(&seed, &seedsize); |
1777 | ui->rs = random_init(seed, seedsize); |
1778 | sfree(seed); |
66164171 |
1779 | |
1780 | return ui; |
74a4e547 |
1781 | } |
1782 | |
be8d5aa1 |
1783 | static void free_ui(game_ui *ui) |
74a4e547 |
1784 | { |
cbb5549e |
1785 | random_free(ui->rs); |
66164171 |
1786 | sfree(ui); |
74a4e547 |
1787 | } |
1788 | |
720a8fb7 |
1789 | /* ---------------------------------------------------------------------- |
1790 | * Process a move. |
1791 | */ |
be8d5aa1 |
1792 | static game_state *make_move(game_state *state, game_ui *ui, |
c0361acd |
1793 | game_drawstate *ds, int x, int y, int button) { |
66164171 |
1794 | game_state *ret, *nullret; |
720a8fb7 |
1795 | int tx, ty, orig; |
f0ee053c |
1796 | int shift = button & MOD_SHFT, ctrl = button & MOD_CTRL; |
720a8fb7 |
1797 | |
f0ee053c |
1798 | button &= ~MOD_MASK; |
66164171 |
1799 | nullret = NULL; |
720a8fb7 |
1800 | |
66164171 |
1801 | if (button == LEFT_BUTTON || |
1802 | button == MIDDLE_BUTTON || |
1803 | button == RIGHT_BUTTON) { |
1804 | |
1805 | if (ui->cur_visible) { |
1806 | ui->cur_visible = FALSE; |
1807 | nullret = state; |
1808 | } |
1809 | |
1810 | /* |
1811 | * The button must have been clicked on a valid tile. |
1812 | */ |
1813 | x -= WINDOW_OFFSET + TILE_BORDER; |
1814 | y -= WINDOW_OFFSET + TILE_BORDER; |
1815 | if (x < 0 || y < 0) |
1816 | return nullret; |
1817 | tx = x / TILE_SIZE; |
1818 | ty = y / TILE_SIZE; |
1819 | if (tx >= state->width || ty >= state->height) |
1820 | return nullret; |
f0ee053c |
1821 | /* Transform from physical to game coords */ |
1822 | tx = (tx + ui->org_x) % state->width; |
1823 | ty = (ty + ui->org_y) % state->height; |
66164171 |
1824 | if (x % TILE_SIZE >= TILE_SIZE - TILE_BORDER || |
1825 | y % TILE_SIZE >= TILE_SIZE - TILE_BORDER) |
1826 | return nullret; |
1827 | } else if (button == CURSOR_UP || button == CURSOR_DOWN || |
1828 | button == CURSOR_RIGHT || button == CURSOR_LEFT) { |
f0ee053c |
1829 | int dir; |
1830 | switch (button) { |
1831 | case CURSOR_UP: dir = U; break; |
1832 | case CURSOR_DOWN: dir = D; break; |
1833 | case CURSOR_LEFT: dir = L; break; |
1834 | case CURSOR_RIGHT: dir = R; break; |
1835 | default: return nullret; |
1836 | } |
1837 | if (shift) { |
1838 | /* |
1839 | * Move origin. |
1840 | */ |
1841 | if (state->wrapping) { |
1842 | OFFSET(ui->org_x, ui->org_y, ui->org_x, ui->org_y, dir, state); |
1843 | } else return nullret; /* disallowed for non-wrapping grids */ |
1844 | } |
1845 | if (ctrl) { |
1846 | /* |
1847 | * Change source tile. |
1848 | */ |
1849 | OFFSET(ui->cx, ui->cy, ui->cx, ui->cy, dir, state); |
1850 | } |
1851 | if (!shift && !ctrl) { |
1852 | /* |
1853 | * Move keyboard cursor. |
1854 | */ |
1855 | OFFSET(ui->cur_x, ui->cur_y, ui->cur_x, ui->cur_y, dir, state); |
1856 | ui->cur_visible = TRUE; |
1857 | } |
1858 | return state; /* UI activity has occurred */ |
66164171 |
1859 | } else if (button == 'a' || button == 's' || button == 'd' || |
1860 | button == 'A' || button == 'S' || button == 'D') { |
1861 | tx = ui->cur_x; |
1862 | ty = ui->cur_y; |
1863 | if (button == 'a' || button == 'A') |
1864 | button = LEFT_BUTTON; |
1865 | else if (button == 's' || button == 'S') |
1866 | button = MIDDLE_BUTTON; |
1867 | else if (button == 'd' || button == 'D') |
1868 | button = RIGHT_BUTTON; |
0671fa51 |
1869 | ui->cur_visible = TRUE; |
cbb5549e |
1870 | } else if (button == 'j' || button == 'J') { |
1871 | /* XXX should we have some mouse control for this? */ |
1872 | button = 'J'; /* canonify */ |
1873 | tx = ty = -1; /* shut gcc up :( */ |
66164171 |
1874 | } else |
1875 | return nullret; |
720a8fb7 |
1876 | |
1877 | /* |
1878 | * The middle button locks or unlocks a tile. (A locked tile |
1879 | * cannot be turned, and is visually marked as being locked. |
1880 | * This is a convenience for the player, so that once they are |
1881 | * sure which way round a tile goes, they can lock it and thus |
1882 | * avoid forgetting later on that they'd already done that one; |
1883 | * and the locking also prevents them turning the tile by |
1884 | * accident. If they change their mind, another middle click |
1885 | * unlocks it.) |
1886 | */ |
1887 | if (button == MIDDLE_BUTTON) { |
cbb5549e |
1888 | |
720a8fb7 |
1889 | ret = dup_game(state); |
2ac6d24e |
1890 | ret->just_used_solve = FALSE; |
720a8fb7 |
1891 | tile(ret, tx, ty) ^= LOCKED; |
1185e3c5 |
1892 | ret->last_rotate_dir = ret->last_rotate_x = ret->last_rotate_y = 0; |
720a8fb7 |
1893 | return ret; |
720a8fb7 |
1894 | |
cbb5549e |
1895 | } else if (button == LEFT_BUTTON || button == RIGHT_BUTTON) { |
720a8fb7 |
1896 | |
cbb5549e |
1897 | /* |
1898 | * The left and right buttons have no effect if clicked on a |
1899 | * locked tile. |
1900 | */ |
1901 | if (tile(state, tx, ty) & LOCKED) |
1902 | return nullret; |
1903 | |
1904 | /* |
1905 | * Otherwise, turn the tile one way or the other. Left button |
1906 | * turns anticlockwise; right button turns clockwise. |
1907 | */ |
1908 | ret = dup_game(state); |
2ac6d24e |
1909 | ret->just_used_solve = FALSE; |
cbb5549e |
1910 | orig = tile(ret, tx, ty); |
1911 | if (button == LEFT_BUTTON) { |
1912 | tile(ret, tx, ty) = A(orig); |
1913 | ret->last_rotate_dir = +1; |
1914 | } else { |
1915 | tile(ret, tx, ty) = C(orig); |
1916 | ret->last_rotate_dir = -1; |
1917 | } |
1185e3c5 |
1918 | ret->last_rotate_x = tx; |
1919 | ret->last_rotate_y = ty; |
cbb5549e |
1920 | |
1921 | } else if (button == 'J') { |
1922 | |
1923 | /* |
1924 | * Jumble all unlocked tiles to random orientations. |
1925 | */ |
1926 | int jx, jy; |
1927 | ret = dup_game(state); |
2ac6d24e |
1928 | ret->just_used_solve = FALSE; |
cbb5549e |
1929 | for (jy = 0; jy < ret->height; jy++) { |
1930 | for (jx = 0; jx < ret->width; jx++) { |
1931 | if (!(tile(ret, jx, jy) & LOCKED)) { |
1932 | int rot = random_upto(ui->rs, 4); |
1933 | orig = tile(ret, jx, jy); |
1934 | tile(ret, jx, jy) = ROT(orig, rot); |
1935 | } |
1936 | } |
1937 | } |
1938 | ret->last_rotate_dir = 0; /* suppress animation */ |
1185e3c5 |
1939 | ret->last_rotate_x = ret->last_rotate_y = 0; |
cbb5549e |
1940 | |
ab53eb64 |
1941 | } else { |
1942 | ret = NULL; /* placate optimisers which don't understand assert(0) */ |
1943 | assert(0); |
1944 | } |
720a8fb7 |
1945 | |
1946 | /* |
1947 | * Check whether the game has been completed. |
1948 | */ |
1949 | { |
f0ee053c |
1950 | unsigned char *active = compute_active(ret, ui->cx, ui->cy); |
720a8fb7 |
1951 | int x1, y1; |
1952 | int complete = TRUE; |
1953 | |
1954 | for (x1 = 0; x1 < ret->width; x1++) |
1955 | for (y1 = 0; y1 < ret->height; y1++) |
1185e3c5 |
1956 | if ((tile(ret, x1, y1) & 0xF) && !index(ret, active, x1, y1)) { |
720a8fb7 |
1957 | complete = FALSE; |
1958 | goto break_label; /* break out of two loops at once */ |
1959 | } |
1960 | break_label: |
1961 | |
1962 | sfree(active); |
1963 | |
1964 | if (complete) |
1965 | ret->completed = TRUE; |
1966 | } |
1967 | |
1968 | return ret; |
1969 | } |
1970 | |
1971 | /* ---------------------------------------------------------------------- |
1972 | * Routines for drawing the game position on the screen. |
1973 | */ |
1974 | |
2ef96bd6 |
1975 | struct game_drawstate { |
1976 | int started; |
1977 | int width, height; |
f0ee053c |
1978 | int org_x, org_y; |
2ef96bd6 |
1979 | unsigned char *visible; |
1980 | }; |
1981 | |
be8d5aa1 |
1982 | static game_drawstate *game_new_drawstate(game_state *state) |
2ef96bd6 |
1983 | { |
1984 | game_drawstate *ds = snew(game_drawstate); |
1985 | |
1986 | ds->started = FALSE; |
1987 | ds->width = state->width; |
1988 | ds->height = state->height; |
f0ee053c |
1989 | ds->org_x = ds->org_y = -1; |
2ef96bd6 |
1990 | ds->visible = snewn(state->width * state->height, unsigned char); |
1991 | memset(ds->visible, 0xFF, state->width * state->height); |
1992 | |
1993 | return ds; |
1994 | } |
1995 | |
be8d5aa1 |
1996 | static void game_free_drawstate(game_drawstate *ds) |
2ef96bd6 |
1997 | { |
1998 | sfree(ds->visible); |
1999 | sfree(ds); |
2000 | } |
2001 | |
be8d5aa1 |
2002 | static void game_size(game_params *params, int *x, int *y) |
7f77ea24 |
2003 | { |
2004 | *x = WINDOW_OFFSET * 2 + TILE_SIZE * params->width + TILE_BORDER; |
2005 | *y = WINDOW_OFFSET * 2 + TILE_SIZE * params->height + TILE_BORDER; |
2006 | } |
2007 | |
be8d5aa1 |
2008 | static float *game_colours(frontend *fe, game_state *state, int *ncolours) |
2ef96bd6 |
2009 | { |
2010 | float *ret; |
83680571 |
2011 | |
2ef96bd6 |
2012 | ret = snewn(NCOLOURS * 3, float); |
2013 | *ncolours = NCOLOURS; |
720a8fb7 |
2014 | |
2ef96bd6 |
2015 | /* |
2016 | * Basic background colour is whatever the front end thinks is |
2017 | * a sensible default. |
2018 | */ |
2019 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
2020 | |
2021 | /* |
2022 | * Wires are black. |
2023 | */ |
03f856c4 |
2024 | ret[COL_WIRE * 3 + 0] = 0.0F; |
2025 | ret[COL_WIRE * 3 + 1] = 0.0F; |
2026 | ret[COL_WIRE * 3 + 2] = 0.0F; |
2ef96bd6 |
2027 | |
2028 | /* |
2029 | * Powered wires and powered endpoints are cyan. |
2030 | */ |
03f856c4 |
2031 | ret[COL_POWERED * 3 + 0] = 0.0F; |
2032 | ret[COL_POWERED * 3 + 1] = 1.0F; |
2033 | ret[COL_POWERED * 3 + 2] = 1.0F; |
2ef96bd6 |
2034 | |
2035 | /* |
2036 | * Barriers are red. |
2037 | */ |
03f856c4 |
2038 | ret[COL_BARRIER * 3 + 0] = 1.0F; |
2039 | ret[COL_BARRIER * 3 + 1] = 0.0F; |
2040 | ret[COL_BARRIER * 3 + 2] = 0.0F; |
2ef96bd6 |
2041 | |
2042 | /* |
2043 | * Unpowered endpoints are blue. |
2044 | */ |
03f856c4 |
2045 | ret[COL_ENDPOINT * 3 + 0] = 0.0F; |
2046 | ret[COL_ENDPOINT * 3 + 1] = 0.0F; |
2047 | ret[COL_ENDPOINT * 3 + 2] = 1.0F; |
2ef96bd6 |
2048 | |
2049 | /* |
2050 | * Tile borders are a darker grey than the background. |
2051 | */ |
03f856c4 |
2052 | ret[COL_BORDER * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0]; |
2053 | ret[COL_BORDER * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1]; |
2054 | ret[COL_BORDER * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2]; |
2ef96bd6 |
2055 | |
2056 | /* |
2057 | * Locked tiles are a grey in between those two. |
2058 | */ |
03f856c4 |
2059 | ret[COL_LOCKED * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0]; |
2060 | ret[COL_LOCKED * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1]; |
2061 | ret[COL_LOCKED * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2]; |
2ef96bd6 |
2062 | |
2063 | return ret; |
2064 | } |
2065 | |
2066 | static void draw_thick_line(frontend *fe, int x1, int y1, int x2, int y2, |
2067 | int colour) |
720a8fb7 |
2068 | { |
2ef96bd6 |
2069 | draw_line(fe, x1-1, y1, x2-1, y2, COL_WIRE); |
2070 | draw_line(fe, x1+1, y1, x2+1, y2, COL_WIRE); |
2071 | draw_line(fe, x1, y1-1, x2, y2-1, COL_WIRE); |
2072 | draw_line(fe, x1, y1+1, x2, y2+1, COL_WIRE); |
2073 | draw_line(fe, x1, y1, x2, y2, colour); |
2074 | } |
720a8fb7 |
2075 | |
2ef96bd6 |
2076 | static void draw_rect_coords(frontend *fe, int x1, int y1, int x2, int y2, |
2077 | int colour) |
2078 | { |
2079 | int mx = (x1 < x2 ? x1 : x2); |
2080 | int my = (y1 < y2 ? y1 : y2); |
2081 | int dx = (x2 + x1 - 2*mx + 1); |
2082 | int dy = (y2 + y1 - 2*my + 1); |
720a8fb7 |
2083 | |
2ef96bd6 |
2084 | draw_rect(fe, mx, my, dx, dy, colour); |
2085 | } |
720a8fb7 |
2086 | |
f0ee053c |
2087 | /* |
2088 | * draw_barrier_corner() and draw_barrier() are passed physical coords |
2089 | */ |
6c333866 |
2090 | static void draw_barrier_corner(frontend *fe, int x, int y, int dx, int dy, |
2091 | int phase) |
2ef96bd6 |
2092 | { |
2093 | int bx = WINDOW_OFFSET + TILE_SIZE * x; |
2094 | int by = WINDOW_OFFSET + TILE_SIZE * y; |
6c333866 |
2095 | int x1, y1; |
2ef96bd6 |
2096 | |
2ef96bd6 |
2097 | x1 = (dx > 0 ? TILE_SIZE+TILE_BORDER-1 : 0); |
2098 | y1 = (dy > 0 ? TILE_SIZE+TILE_BORDER-1 : 0); |
2099 | |
2100 | if (phase == 0) { |
6c333866 |
2101 | draw_rect_coords(fe, bx+x1+dx, by+y1, |
2ef96bd6 |
2102 | bx+x1-TILE_BORDER*dx, by+y1-(TILE_BORDER-1)*dy, |
6c333866 |
2103 | COL_WIRE); |
2104 | draw_rect_coords(fe, bx+x1, by+y1+dy, |
2ef96bd6 |
2105 | bx+x1-(TILE_BORDER-1)*dx, by+y1-TILE_BORDER*dy, |
6c333866 |
2106 | COL_WIRE); |
2ef96bd6 |
2107 | } else { |
2108 | draw_rect_coords(fe, bx+x1, by+y1, |
2109 | bx+x1-(TILE_BORDER-1)*dx, by+y1-(TILE_BORDER-1)*dy, |
6c333866 |
2110 | COL_BARRIER); |
720a8fb7 |
2111 | } |
2ef96bd6 |
2112 | } |
2113 | |
6c333866 |
2114 | static void draw_barrier(frontend *fe, int x, int y, int dir, int phase) |
2ef96bd6 |
2115 | { |
2116 | int bx = WINDOW_OFFSET + TILE_SIZE * x; |
2117 | int by = WINDOW_OFFSET + TILE_SIZE * y; |
2118 | int x1, y1, w, h; |
2119 | |
2120 | x1 = (X(dir) > 0 ? TILE_SIZE : X(dir) == 0 ? TILE_BORDER : 0); |
2121 | y1 = (Y(dir) > 0 ? TILE_SIZE : Y(dir) == 0 ? TILE_BORDER : 0); |
2122 | w = (X(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER); |
2123 | h = (Y(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER); |
2124 | |
2125 | if (phase == 0) { |
6c333866 |
2126 | draw_rect(fe, bx+x1-X(dir), by+y1-Y(dir), w, h, COL_WIRE); |
2ef96bd6 |
2127 | } else { |
6c333866 |
2128 | draw_rect(fe, bx+x1, by+y1, w, h, COL_BARRIER); |
2ef96bd6 |
2129 | } |
2130 | } |
720a8fb7 |
2131 | |
f0ee053c |
2132 | /* |
2133 | * draw_tile() is passed physical coordinates |
2134 | */ |
2135 | static void draw_tile(frontend *fe, game_state *state, game_drawstate *ds, |
2136 | int x, int y, int tile, int src, float angle, int cursor) |
2ef96bd6 |
2137 | { |
2138 | int bx = WINDOW_OFFSET + TILE_SIZE * x; |
2139 | int by = WINDOW_OFFSET + TILE_SIZE * y; |
2140 | float matrix[4]; |
2141 | float cx, cy, ex, ey, tx, ty; |
2142 | int dir, col, phase; |
720a8fb7 |
2143 | |
2ef96bd6 |
2144 | /* |
2145 | * When we draw a single tile, we must draw everything up to |
2146 | * and including the borders around the tile. This means that |
2147 | * if the neighbouring tiles have connections to those borders, |
2148 | * we must draw those connections on the borders themselves. |
2ef96bd6 |
2149 | */ |
2150 | |
6c333866 |
2151 | clip(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER); |
2152 | |
2ef96bd6 |
2153 | /* |
2154 | * So. First blank the tile out completely: draw a big |
2155 | * rectangle in border colour, and a smaller rectangle in |
2156 | * background colour to fill it in. |
2157 | */ |
2158 | draw_rect(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER, |
2159 | COL_BORDER); |
2160 | draw_rect(fe, bx+TILE_BORDER, by+TILE_BORDER, |
2161 | TILE_SIZE-TILE_BORDER, TILE_SIZE-TILE_BORDER, |
2162 | tile & LOCKED ? COL_LOCKED : COL_BACKGROUND); |
2163 | |
2164 | /* |
66164171 |
2165 | * Draw an inset outline rectangle as a cursor, in whichever of |
2166 | * COL_LOCKED and COL_BACKGROUND we aren't currently drawing |
2167 | * in. |
2168 | */ |
2169 | if (cursor) { |
2170 | draw_line(fe, bx+TILE_SIZE/8, by+TILE_SIZE/8, |
2171 | bx+TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8, |
2172 | tile & LOCKED ? COL_BACKGROUND : COL_LOCKED); |
2173 | draw_line(fe, bx+TILE_SIZE/8, by+TILE_SIZE/8, |
2174 | bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE/8, |
2175 | tile & LOCKED ? COL_BACKGROUND : COL_LOCKED); |
2176 | draw_line(fe, bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE/8, |
2177 | bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8, |
2178 | tile & LOCKED ? COL_BACKGROUND : COL_LOCKED); |
2179 | draw_line(fe, bx+TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8, |
2180 | bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8, |
2181 | tile & LOCKED ? COL_BACKGROUND : COL_LOCKED); |
2182 | } |
2183 | |
2184 | /* |
2ef96bd6 |
2185 | * Set up the rotation matrix. |
2186 | */ |
03f856c4 |
2187 | matrix[0] = (float)cos(angle * PI / 180.0); |
2188 | matrix[1] = (float)-sin(angle * PI / 180.0); |
2189 | matrix[2] = (float)sin(angle * PI / 180.0); |
2190 | matrix[3] = (float)cos(angle * PI / 180.0); |
2ef96bd6 |
2191 | |
2192 | /* |
2193 | * Draw the wires. |
2194 | */ |
03f856c4 |
2195 | cx = cy = TILE_BORDER + (TILE_SIZE-TILE_BORDER) / 2.0F - 0.5F; |
2ef96bd6 |
2196 | col = (tile & ACTIVE ? COL_POWERED : COL_WIRE); |
2197 | for (dir = 1; dir < 0x10; dir <<= 1) { |
2198 | if (tile & dir) { |
03f856c4 |
2199 | ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir); |
2200 | ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir); |
2ef96bd6 |
2201 | MATMUL(tx, ty, matrix, ex, ey); |
03f856c4 |
2202 | draw_thick_line(fe, bx+(int)cx, by+(int)cy, |
2203 | bx+(int)(cx+tx), by+(int)(cy+ty), |
2ef96bd6 |
2204 | COL_WIRE); |
2205 | } |
2206 | } |
2207 | for (dir = 1; dir < 0x10; dir <<= 1) { |
2208 | if (tile & dir) { |
03f856c4 |
2209 | ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir); |
2210 | ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir); |
2ef96bd6 |
2211 | MATMUL(tx, ty, matrix, ex, ey); |
03f856c4 |
2212 | draw_line(fe, bx+(int)cx, by+(int)cy, |
2213 | bx+(int)(cx+tx), by+(int)(cy+ty), col); |
2ef96bd6 |
2214 | } |
2215 | } |
2216 | |
2217 | /* |
2218 | * Draw the box in the middle. We do this in blue if the tile |
2219 | * is an unpowered endpoint, in cyan if the tile is a powered |
2220 | * endpoint, in black if the tile is the centrepiece, and |
2221 | * otherwise not at all. |
2222 | */ |
2223 | col = -1; |
f0ee053c |
2224 | if (src) |
2ef96bd6 |
2225 | col = COL_WIRE; |
2226 | else if (COUNT(tile) == 1) { |
2227 | col = (tile & ACTIVE ? COL_POWERED : COL_ENDPOINT); |
2228 | } |
2229 | if (col >= 0) { |
2230 | int i, points[8]; |
2231 | |
2232 | points[0] = +1; points[1] = +1; |
2233 | points[2] = +1; points[3] = -1; |
2234 | points[4] = -1; points[5] = -1; |
2235 | points[6] = -1; points[7] = +1; |
2236 | |
2237 | for (i = 0; i < 8; i += 2) { |
03f856c4 |
2238 | ex = (TILE_SIZE * 0.24F) * points[i]; |
2239 | ey = (TILE_SIZE * 0.24F) * points[i+1]; |
2ef96bd6 |
2240 | MATMUL(tx, ty, matrix, ex, ey); |
03f856c4 |
2241 | points[i] = bx+(int)(cx+tx); |
2242 | points[i+1] = by+(int)(cy+ty); |
2ef96bd6 |
2243 | } |
2244 | |
2245 | draw_polygon(fe, points, 4, TRUE, col); |
2246 | draw_polygon(fe, points, 4, FALSE, COL_WIRE); |
2247 | } |
2248 | |
2249 | /* |
2250 | * Draw the points on the border if other tiles are connected |
2251 | * to us. |
2252 | */ |
2253 | for (dir = 1; dir < 0x10; dir <<= 1) { |
2254 | int dx, dy, px, py, lx, ly, vx, vy, ox, oy; |
2255 | |
2256 | dx = X(dir); |
2257 | dy = Y(dir); |
2258 | |
2259 | ox = x + dx; |
2260 | oy = y + dy; |
2261 | |
2262 | if (ox < 0 || ox >= state->width || oy < 0 || oy >= state->height) |
2263 | continue; |
2264 | |
f0ee053c |
2265 | if (!(tile(state, GX(ox), GY(oy)) & F(dir))) |
2ef96bd6 |
2266 | continue; |
2267 | |
03f856c4 |
2268 | px = bx + (int)(dx>0 ? TILE_SIZE + TILE_BORDER - 1 : dx<0 ? 0 : cx); |
2269 | py = by + (int)(dy>0 ? TILE_SIZE + TILE_BORDER - 1 : dy<0 ? 0 : cy); |
2ef96bd6 |
2270 | lx = dx * (TILE_BORDER-1); |
2271 | ly = dy * (TILE_BORDER-1); |
2272 | vx = (dy ? 1 : 0); |
2273 | vy = (dx ? 1 : 0); |
2274 | |
2275 | if (angle == 0.0 && (tile & dir)) { |
2276 | /* |
2277 | * If we are fully connected to the other tile, we must |
2278 | * draw right across the tile border. (We can use our |
2279 | * own ACTIVE state to determine what colour to do this |
2280 | * in: if we are fully connected to the other tile then |
2281 | * the two ACTIVE states will be the same.) |
2282 | */ |
2283 | draw_rect_coords(fe, px-vx, py-vy, px+lx+vx, py+ly+vy, COL_WIRE); |
2284 | draw_rect_coords(fe, px, py, px+lx, py+ly, |
2285 | (tile & ACTIVE) ? COL_POWERED : COL_WIRE); |
2286 | } else { |
2287 | /* |
2288 | * The other tile extends into our border, but isn't |
2289 | * actually connected to us. Just draw a single black |
2290 | * dot. |
2291 | */ |
2292 | draw_rect_coords(fe, px, py, px, py, COL_WIRE); |
2293 | } |
2294 | } |
2295 | |
2296 | /* |
2297 | * Draw barrier corners, and then barriers. |
2298 | */ |
2299 | for (phase = 0; phase < 2; phase++) { |
6c333866 |
2300 | for (dir = 1; dir < 0x10; dir <<= 1) { |
2301 | int x1, y1, corner = FALSE; |
2302 | /* |
2303 | * If at least one barrier terminates at the corner |
2304 | * between dir and A(dir), draw a barrier corner. |
2305 | */ |
2306 | if (barrier(state, GX(x), GY(y)) & (dir | A(dir))) { |
2307 | corner = TRUE; |
2308 | } else { |
2309 | /* |
2310 | * Only count barriers terminating at this corner |
2311 | * if they're physically next to the corner. (That |
2312 | * is, if they've wrapped round from the far side |
2313 | * of the screen, they don't count.) |
2314 | */ |
2315 | x1 = x + X(dir); |
2316 | y1 = y + Y(dir); |
2317 | if (x1 >= 0 && x1 < state->width && |
2318 | y1 >= 0 && y1 < state->height && |
2319 | (barrier(state, GX(x1), GY(y1)) & A(dir))) { |
2320 | corner = TRUE; |
2321 | } else { |
2322 | x1 = x + X(A(dir)); |
2323 | y1 = y + Y(A(dir)); |
2324 | if (x1 >= 0 && x1 < state->width && |
2325 | y1 >= 0 && y1 < state->height && |
2326 | (barrier(state, GX(x1), GY(y1)) & dir)) |
2327 | corner = TRUE; |
2328 | } |
2329 | } |
2330 | |
2331 | if (corner) { |
2332 | /* |
2333 | * At least one barrier terminates here. Draw a |
2334 | * corner. |
2335 | */ |
2336 | draw_barrier_corner(fe, x, y, |
2337 | X(dir)+X(A(dir)), Y(dir)+Y(A(dir)), |
2338 | phase); |
2339 | } |
2340 | } |
2341 | |
2ef96bd6 |
2342 | for (dir = 1; dir < 0x10; dir <<= 1) |
f0ee053c |
2343 | if (barrier(state, GX(x), GY(y)) & dir) |
6c333866 |
2344 | draw_barrier(fe, x, y, dir, phase); |
2ef96bd6 |
2345 | } |
2346 | |
6c333866 |
2347 | unclip(fe); |
2348 | |
2ef96bd6 |
2349 | draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER); |
720a8fb7 |
2350 | } |
2351 | |
be8d5aa1 |
2352 | static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, |
c822de4a |
2353 | game_state *state, int dir, game_ui *ui, float t, float ft) |
2ef96bd6 |
2354 | { |
f0ee053c |
2355 | int x, y, tx, ty, frame, last_rotate_dir, moved_origin = FALSE; |
2ef96bd6 |
2356 | unsigned char *active; |
2357 | float angle = 0.0; |
2358 | |
2359 | /* |
6c333866 |
2360 | * Clear the screen, and draw the exterior barrier lines, if |
2361 | * this is our first call or if the origin has changed. |
2ef96bd6 |
2362 | */ |
6c333866 |
2363 | if (!ds->started || ui->org_x != ds->org_x || ui->org_y != ds->org_y) { |
2364 | int phase; |
2365 | |
2ef96bd6 |
2366 | ds->started = TRUE; |
2367 | |
2368 | draw_rect(fe, 0, 0, |
2369 | WINDOW_OFFSET * 2 + TILE_SIZE * state->width + TILE_BORDER, |
2370 | WINDOW_OFFSET * 2 + TILE_SIZE * state->height + TILE_BORDER, |
2371 | COL_BACKGROUND); |
f0ee053c |
2372 | |
f0ee053c |
2373 | ds->org_x = ui->org_x; |
2374 | ds->org_y = ui->org_y; |
2375 | moved_origin = TRUE; |
2376 | |
2ef96bd6 |
2377 | draw_update(fe, 0, 0, |
2378 | WINDOW_OFFSET*2 + TILE_SIZE*state->width + TILE_BORDER, |
2379 | WINDOW_OFFSET*2 + TILE_SIZE*state->height + TILE_BORDER); |
6c333866 |
2380 | |
2ef96bd6 |
2381 | for (phase = 0; phase < 2; phase++) { |
2382 | |
2383 | for (x = 0; x < ds->width; x++) { |
6c333866 |
2384 | if (x+1 < ds->width) { |
2385 | if (barrier(state, GX(x), GY(0)) & R) |
2386 | draw_barrier_corner(fe, x, -1, +1, +1, phase); |
2387 | if (barrier(state, GX(x), GY(ds->height-1)) & R) |
2388 | draw_barrier_corner(fe, x, ds->height, +1, -1, phase); |
2389 | } |
2390 | if (barrier(state, GX(x), GY(0)) & U) { |
2391 | draw_barrier_corner(fe, x, -1, -1, +1, phase); |
2392 | draw_barrier_corner(fe, x, -1, +1, +1, phase); |
2393 | draw_barrier(fe, x, -1, D, phase); |
2394 | } |
2395 | if (barrier(state, GX(x), GY(ds->height-1)) & D) { |
2396 | draw_barrier_corner(fe, x, ds->height, -1, -1, phase); |
2397 | draw_barrier_corner(fe, x, ds->height, +1, -1, phase); |
2398 | draw_barrier(fe, x, ds->height, U, phase); |
2399 | } |
2ef96bd6 |
2400 | } |
2401 | |
2402 | for (y = 0; y < ds->height; y++) { |
6c333866 |
2403 | if (y+1 < ds->height) { |
2404 | if (barrier(state, GX(0), GY(y)) & D) |
2405 | draw_barrier_corner(fe, -1, y, +1, +1, phase); |
2406 | if (barrier(state, GX(ds->width-1), GY(y)) & D) |
2407 | draw_barrier_corner(fe, ds->width, y, -1, +1, phase); |
2408 | } |
2409 | if (barrier(state, GX(0), GY(y)) & L) { |
2410 | draw_barrier_corner(fe, -1, y, +1, -1, phase); |
2411 | draw_barrier_corner(fe, -1, y, +1, +1, phase); |
2412 | draw_barrier(fe, -1, y, R, phase); |
2413 | } |
2414 | if (barrier(state, GX(ds->width-1), GY(y)) & R) { |
2415 | draw_barrier_corner(fe, ds->width, y, -1, -1, phase); |
2416 | draw_barrier_corner(fe, ds->width, y, -1, +1, phase); |
2417 | draw_barrier(fe, ds->width, y, L, phase); |
2418 | } |
2ef96bd6 |
2419 | } |
2420 | } |
2421 | } |
2422 | |
2423 | tx = ty = -1; |
cbb5549e |
2424 | last_rotate_dir = dir==-1 ? oldstate->last_rotate_dir : |
2425 | state->last_rotate_dir; |
2426 | if (oldstate && (t < ROTATE_TIME) && last_rotate_dir) { |
2ef96bd6 |
2427 | /* |
1185e3c5 |
2428 | * We're animating a single tile rotation. Find the turning |
2429 | * tile. |
2ef96bd6 |
2430 | */ |
1185e3c5 |
2431 | tx = (dir==-1 ? oldstate->last_rotate_x : state->last_rotate_x); |
2432 | ty = (dir==-1 ? oldstate->last_rotate_y : state->last_rotate_y); |
2433 | angle = last_rotate_dir * dir * 90.0F * (t / ROTATE_TIME); |
2434 | state = oldstate; |
87ed82be |
2435 | } |
1185e3c5 |
2436 | |
87ed82be |
2437 | frame = -1; |
2438 | if (ft > 0) { |
2ef96bd6 |
2439 | /* |
2440 | * We're animating a completion flash. Find which frame |
2441 | * we're at. |
2442 | */ |
87ed82be |
2443 | frame = (int)(ft / FLASH_FRAME); |
2ef96bd6 |
2444 | } |
2445 | |
2446 | /* |
2447 | * Draw any tile which differs from the way it was last drawn. |
2448 | */ |
f0ee053c |
2449 | active = compute_active(state, ui->cx, ui->cy); |
2ef96bd6 |
2450 | |
2451 | for (x = 0; x < ds->width; x++) |
2452 | for (y = 0; y < ds->height; y++) { |
f0ee053c |
2453 | unsigned char c = tile(state, GX(x), GY(y)) | |
2454 | index(state, active, GX(x), GY(y)); |
2455 | int is_src = GX(x) == ui->cx && GY(y) == ui->cy; |
2456 | int is_anim = GX(x) == tx && GY(y) == ty; |
2457 | int is_cursor = ui->cur_visible && |
2458 | GX(x) == ui->cur_x && GY(y) == ui->cur_y; |
2ef96bd6 |
2459 | |
2460 | /* |
2461 | * In a completion flash, we adjust the LOCKED bit |
2462 | * depending on our distance from the centre point and |
2463 | * the frame number. |
2464 | */ |
2465 | if (frame >= 0) { |
f0ee053c |
2466 | int rcx = RX(ui->cx), rcy = RY(ui->cy); |
2ef96bd6 |
2467 | int xdist, ydist, dist; |
f0ee053c |
2468 | xdist = (x < rcx ? rcx - x : x - rcx); |
2469 | ydist = (y < rcy ? rcy - y : y - rcy); |
2ef96bd6 |
2470 | dist = (xdist > ydist ? xdist : ydist); |
2471 | |
2472 | if (frame >= dist && frame < dist+4) { |
2473 | int lock = (frame - dist) & 1; |
2474 | lock = lock ? LOCKED : 0; |
2475 | c = (c &~ LOCKED) | lock; |
2476 | } |
2477 | } |
2478 | |
f0ee053c |
2479 | if (moved_origin || |
2480 | index(state, ds->visible, x, y) != c || |
2ef96bd6 |
2481 | index(state, ds->visible, x, y) == 0xFF || |
f0ee053c |
2482 | is_src || is_anim || is_cursor) { |
2483 | draw_tile(fe, state, ds, x, y, c, |
2484 | is_src, (is_anim ? angle : 0.0F), is_cursor); |
2485 | if (is_src || is_anim || is_cursor) |
2ef96bd6 |
2486 | index(state, ds->visible, x, y) = 0xFF; |
2487 | else |
2488 | index(state, ds->visible, x, y) = c; |
2489 | } |
2490 | } |
2491 | |
fd1a1a2b |
2492 | /* |
2493 | * Update the status bar. |
2494 | */ |
2495 | { |
2496 | char statusbuf[256]; |
1185e3c5 |
2497 | int i, n, n2, a; |
fd1a1a2b |
2498 | |
2499 | n = state->width * state->height; |
1185e3c5 |
2500 | for (i = a = n2 = 0; i < n; i++) { |
fd1a1a2b |
2501 | if (active[i]) |
2502 | a++; |
1185e3c5 |
2503 | if (state->tiles[i] & 0xF) |
2504 | n2++; |
2505 | } |
fd1a1a2b |
2506 | |
2507 | sprintf(statusbuf, "%sActive: %d/%d", |
2ac6d24e |
2508 | (state->used_solve ? "Auto-solved. " : |
1185e3c5 |
2509 | state->completed ? "COMPLETED! " : ""), a, n2); |
fd1a1a2b |
2510 | |
2511 | status_bar(fe, statusbuf); |
2512 | } |
2513 | |
2ef96bd6 |
2514 | sfree(active); |
2515 | } |
2516 | |
be8d5aa1 |
2517 | static float game_anim_length(game_state *oldstate, |
e3f21163 |
2518 | game_state *newstate, int dir, game_ui *ui) |
2ef96bd6 |
2519 | { |
1185e3c5 |
2520 | int last_rotate_dir; |
2ef96bd6 |
2521 | |
2522 | /* |
2ac6d24e |
2523 | * Don't animate an auto-solve move. |
2524 | */ |
2525 | if ((dir > 0 && newstate->just_used_solve) || |
2526 | (dir < 0 && oldstate->just_used_solve)) |
2527 | return 0.0F; |
2528 | |
2529 | /* |
cbb5549e |
2530 | * Don't animate if last_rotate_dir is zero. |
2ef96bd6 |
2531 | */ |
cbb5549e |
2532 | last_rotate_dir = dir==-1 ? oldstate->last_rotate_dir : |
2533 | newstate->last_rotate_dir; |
1185e3c5 |
2534 | if (last_rotate_dir) |
2535 | return ROTATE_TIME; |
2ef96bd6 |
2536 | |
87ed82be |
2537 | return 0.0F; |
2538 | } |
2539 | |
be8d5aa1 |
2540 | static float game_flash_length(game_state *oldstate, |
e3f21163 |
2541 | game_state *newstate, int dir, game_ui *ui) |
87ed82be |
2542 | { |
2ef96bd6 |
2543 | /* |
87ed82be |
2544 | * If the game has just been completed, we display a completion |
2545 | * flash. |
2ef96bd6 |
2546 | */ |
2ac6d24e |
2547 | if (!oldstate->completed && newstate->completed && |
2548 | !oldstate->used_solve && !newstate->used_solve) { |
f0ee053c |
2549 | int size = 0; |
2550 | if (size < newstate->width) |
2551 | size = newstate->width; |
2552 | if (size < newstate->height) |
2553 | size = newstate->height; |
87ed82be |
2554 | return FLASH_FRAME * (size+4); |
2ef96bd6 |
2555 | } |
2556 | |
87ed82be |
2557 | return 0.0F; |
2ef96bd6 |
2558 | } |
fd1a1a2b |
2559 | |
be8d5aa1 |
2560 | static int game_wants_statusbar(void) |
fd1a1a2b |
2561 | { |
2562 | return TRUE; |
2563 | } |
be8d5aa1 |
2564 | |
48dcdd62 |
2565 | static int game_timing_state(game_state *state) |
2566 | { |
2567 | return TRUE; |
2568 | } |
2569 | |
be8d5aa1 |
2570 | #ifdef COMBINED |
2571 | #define thegame net |
2572 | #endif |
2573 | |
2574 | const struct game thegame = { |
1d228b10 |
2575 | "Net", "games.net", |
be8d5aa1 |
2576 | default_params, |
2577 | game_fetch_preset, |
2578 | decode_params, |
2579 | encode_params, |
2580 | free_params, |
2581 | dup_params, |
1d228b10 |
2582 | TRUE, game_configure, custom_params, |
be8d5aa1 |
2583 | validate_params, |
1185e3c5 |
2584 | new_game_desc, |
6f2d8d7c |
2585 | game_free_aux_info, |
1185e3c5 |
2586 | validate_desc, |
be8d5aa1 |
2587 | new_game, |
2588 | dup_game, |
2589 | free_game, |
2ac6d24e |
2590 | TRUE, solve_game, |
9b4b03d3 |
2591 | FALSE, game_text_format, |
be8d5aa1 |
2592 | new_ui, |
2593 | free_ui, |
2594 | make_move, |
2595 | game_size, |
2596 | game_colours, |
2597 | game_new_drawstate, |
2598 | game_free_drawstate, |
2599 | game_redraw, |
2600 | game_anim_length, |
2601 | game_flash_length, |
2602 | game_wants_statusbar, |
48dcdd62 |
2603 | FALSE, game_timing_state, |
93b1da3d |
2604 | 0, /* mouse_priorities */ |
be8d5aa1 |
2605 | }; |