7bed19e1 |
1 | /* |
2 | * netslide.c: cross between Net and Sixteen, courtesy of Richard |
3 | * Boulton. |
4 | */ |
5 | |
6 | #include <stdio.h> |
7 | #include <stdlib.h> |
8 | #include <string.h> |
9 | #include <assert.h> |
10 | #include <ctype.h> |
11 | #include <math.h> |
12 | |
13 | #include "puzzles.h" |
14 | #include "tree234.h" |
15 | |
7bed19e1 |
16 | #define MATMUL(xr,yr,m,x,y) do { \ |
17 | float rx, ry, xx = (x), yy = (y), *mat = (m); \ |
18 | rx = mat[0] * xx + mat[2] * yy; \ |
19 | ry = mat[1] * xx + mat[3] * yy; \ |
20 | (xr) = rx; (yr) = ry; \ |
21 | } while (0) |
22 | |
23 | /* Direction and other bitfields */ |
24 | #define R 0x01 |
25 | #define U 0x02 |
26 | #define L 0x04 |
27 | #define D 0x08 |
28 | #define FLASHING 0x10 |
29 | #define ACTIVE 0x20 |
30 | /* Corner flags go in the barriers array */ |
31 | #define RU 0x10 |
32 | #define UL 0x20 |
33 | #define LD 0x40 |
34 | #define DR 0x80 |
35 | |
36 | /* Get tile at given coordinate */ |
37 | #define T(state, x, y) ( (y) * (state)->width + (x) ) |
38 | |
39 | /* Rotations: Anticlockwise, Clockwise, Flip, general rotate */ |
40 | #define A(x) ( (((x) & 0x07) << 1) | (((x) & 0x08) >> 3) ) |
41 | #define C(x) ( (((x) & 0x0E) >> 1) | (((x) & 0x01) << 3) ) |
42 | #define F(x) ( (((x) & 0x0C) >> 2) | (((x) & 0x03) << 2) ) |
43 | #define ROT(x, n) ( ((n)&3) == 0 ? (x) : \ |
44 | ((n)&3) == 1 ? A(x) : \ |
45 | ((n)&3) == 2 ? F(x) : C(x) ) |
46 | |
47 | /* X and Y displacements */ |
48 | #define X(x) ( (x) == R ? +1 : (x) == L ? -1 : 0 ) |
49 | #define Y(x) ( (x) == D ? +1 : (x) == U ? -1 : 0 ) |
50 | |
51 | /* Bit count */ |
52 | #define COUNT(x) ( (((x) & 0x08) >> 3) + (((x) & 0x04) >> 2) + \ |
53 | (((x) & 0x02) >> 1) + ((x) & 0x01) ) |
54 | |
1e3e152d |
55 | #define PREFERRED_TILE_SIZE 48 |
56 | #define TILE_SIZE (ds->tilesize) |
7bed19e1 |
57 | #define BORDER TILE_SIZE |
58 | #define TILE_BORDER 1 |
59 | #define WINDOW_OFFSET 0 |
60 | |
61 | #define ANIM_TIME 0.13F |
62 | #define FLASH_FRAME 0.07F |
63 | |
64 | enum { |
65 | COL_BACKGROUND, |
66 | COL_FLASHING, |
67 | COL_BORDER, |
68 | COL_WIRE, |
69 | COL_ENDPOINT, |
70 | COL_POWERED, |
71 | COL_BARRIER, |
72 | COL_LOWLIGHT, |
73 | COL_TEXT, |
74 | NCOLOURS |
75 | }; |
76 | |
77 | struct game_params { |
78 | int width; |
79 | int height; |
80 | int wrapping; |
81 | float barrier_probability; |
aa27d493 |
82 | int movetarget; |
7bed19e1 |
83 | }; |
84 | |
85 | struct game_state { |
86 | int width, height, cx, cy, wrapping, completed; |
a440f184 |
87 | int used_solve; |
aa27d493 |
88 | int move_count, movetarget; |
7bed19e1 |
89 | |
90 | /* position (row or col number, starting at 0) of last move. */ |
91 | int last_move_row, last_move_col; |
92 | |
93 | /* direction of last move: +1 or -1 */ |
94 | int last_move_dir; |
95 | |
96 | unsigned char *tiles; |
97 | unsigned char *barriers; |
98 | }; |
99 | |
100 | #define OFFSET(x2,y2,x1,y1,dir,state) \ |
101 | ( (x2) = ((x1) + (state)->width + X((dir))) % (state)->width, \ |
102 | (y2) = ((y1) + (state)->height + Y((dir))) % (state)->height) |
103 | |
104 | #define index(state, a, x, y) ( a[(y) * (state)->width + (x)] ) |
105 | #define tile(state, x, y) index(state, (state)->tiles, x, y) |
106 | #define barrier(state, x, y) index(state, (state)->barriers, x, y) |
107 | |
108 | struct xyd { |
109 | int x, y, direction; |
110 | }; |
111 | |
112 | static int xyd_cmp(void *av, void *bv) { |
113 | struct xyd *a = (struct xyd *)av; |
114 | struct xyd *b = (struct xyd *)bv; |
115 | if (a->x < b->x) |
116 | return -1; |
117 | if (a->x > b->x) |
118 | return +1; |
119 | if (a->y < b->y) |
120 | return -1; |
121 | if (a->y > b->y) |
122 | return +1; |
123 | if (a->direction < b->direction) |
124 | return -1; |
125 | if (a->direction > b->direction) |
126 | return +1; |
127 | return 0; |
23e8c9fd |
128 | } |
7bed19e1 |
129 | |
130 | static struct xyd *new_xyd(int x, int y, int direction) |
131 | { |
132 | struct xyd *xyd = snew(struct xyd); |
133 | xyd->x = x; |
134 | xyd->y = y; |
135 | xyd->direction = direction; |
136 | return xyd; |
137 | } |
138 | |
be8d5aa1 |
139 | static void slide_col(game_state *state, int dir, int col); |
1185e3c5 |
140 | static void slide_col_int(int w, int h, unsigned char *tiles, int dir, int col); |
be8d5aa1 |
141 | static void slide_row(game_state *state, int dir, int row); |
1185e3c5 |
142 | static void slide_row_int(int w, int h, unsigned char *tiles, int dir, int row); |
7bed19e1 |
143 | |
144 | /* ---------------------------------------------------------------------- |
145 | * Manage game parameters. |
146 | */ |
be8d5aa1 |
147 | static game_params *default_params(void) |
7bed19e1 |
148 | { |
149 | game_params *ret = snew(game_params); |
150 | |
151 | ret->width = 3; |
152 | ret->height = 3; |
153 | ret->wrapping = FALSE; |
154 | ret->barrier_probability = 1.0; |
aa27d493 |
155 | ret->movetarget = 0; |
7bed19e1 |
156 | |
157 | return ret; |
158 | } |
159 | |
ab53eb64 |
160 | static const struct { int x, y, wrap, bprob; const char* desc; } |
161 | netslide_presets[] = { |
3e17893b |
162 | {3, 3, FALSE, 1, " easy"}, |
163 | {3, 3, FALSE, 0, " medium"}, |
164 | {3, 3, TRUE, 0, " hard"}, |
165 | {4, 4, FALSE, 1, " easy"}, |
166 | {4, 4, FALSE, 0, " medium"}, |
167 | {4, 4, TRUE, 0, " hard"}, |
168 | {5, 5, FALSE, 1, " easy"}, |
169 | {5, 5, FALSE, 0, " medium"}, |
170 | {5, 5, TRUE, 0, " hard"}, |
ab53eb64 |
171 | }; |
172 | |
be8d5aa1 |
173 | static int game_fetch_preset(int i, char **name, game_params **params) |
7bed19e1 |
174 | { |
175 | game_params *ret; |
176 | char str[80]; |
ab53eb64 |
177 | |
178 | if (i < 0 || i >= lenof(netslide_presets)) |
7bed19e1 |
179 | return FALSE; |
180 | |
181 | ret = snew(game_params); |
ab53eb64 |
182 | ret->width = netslide_presets[i].x; |
183 | ret->height = netslide_presets[i].y; |
184 | ret->wrapping = netslide_presets[i].wrap; |
3e17893b |
185 | ret->barrier_probability = (float)netslide_presets[i].bprob; |
aa27d493 |
186 | ret->movetarget = 0; |
7bed19e1 |
187 | |
ab53eb64 |
188 | sprintf(str, "%dx%d%s", ret->width, ret->height, netslide_presets[i].desc); |
7bed19e1 |
189 | |
190 | *name = dupstr(str); |
191 | *params = ret; |
192 | return TRUE; |
193 | } |
194 | |
be8d5aa1 |
195 | static void free_params(game_params *params) |
7bed19e1 |
196 | { |
197 | sfree(params); |
198 | } |
199 | |
be8d5aa1 |
200 | static game_params *dup_params(game_params *params) |
7bed19e1 |
201 | { |
202 | game_params *ret = snew(game_params); |
203 | *ret = *params; /* structure copy */ |
204 | return ret; |
205 | } |
206 | |
1185e3c5 |
207 | static void decode_params(game_params *ret, char const *string) |
7bed19e1 |
208 | { |
7bed19e1 |
209 | char const *p = string; |
210 | |
211 | ret->wrapping = FALSE; |
212 | ret->barrier_probability = 0.0; |
aa27d493 |
213 | ret->movetarget = 0; |
7bed19e1 |
214 | |
215 | ret->width = atoi(p); |
89167dad |
216 | while (*p && isdigit((unsigned char)*p)) p++; |
7bed19e1 |
217 | if (*p == 'x') { |
218 | p++; |
219 | ret->height = atoi(p); |
89167dad |
220 | while (*p && isdigit((unsigned char)*p)) p++; |
7bed19e1 |
221 | if ( (ret->wrapping = (*p == 'w')) != 0 ) |
222 | p++; |
aa27d493 |
223 | if (*p == 'b') { |
3e17893b |
224 | ret->barrier_probability = (float)atof(++p); |
89167dad |
225 | while (*p && (isdigit((unsigned char)*p) || *p == '.')) p++; |
aa27d493 |
226 | } |
227 | if (*p == 'm') { |
228 | ret->movetarget = atoi(++p); |
229 | } |
7bed19e1 |
230 | } else { |
231 | ret->height = ret->width; |
232 | } |
7bed19e1 |
233 | } |
234 | |
1185e3c5 |
235 | static char *encode_params(game_params *params, int full) |
7bed19e1 |
236 | { |
237 | char ret[400]; |
238 | int len; |
239 | |
240 | len = sprintf(ret, "%dx%d", params->width, params->height); |
241 | if (params->wrapping) |
242 | ret[len++] = 'w'; |
1185e3c5 |
243 | if (full && params->barrier_probability) |
7bed19e1 |
244 | len += sprintf(ret+len, "b%g", params->barrier_probability); |
aa27d493 |
245 | /* Shuffle limit is part of the limited parameters, because we have to |
246 | * provide the target move count. */ |
247 | if (params->movetarget) |
248 | len += sprintf(ret+len, "m%d", params->movetarget); |
7bed19e1 |
249 | assert(len < lenof(ret)); |
250 | ret[len] = '\0'; |
251 | |
252 | return dupstr(ret); |
253 | } |
254 | |
be8d5aa1 |
255 | static config_item *game_configure(game_params *params) |
7bed19e1 |
256 | { |
257 | config_item *ret; |
258 | char buf[80]; |
259 | |
aa27d493 |
260 | ret = snewn(6, config_item); |
7bed19e1 |
261 | |
262 | ret[0].name = "Width"; |
263 | ret[0].type = C_STRING; |
264 | sprintf(buf, "%d", params->width); |
265 | ret[0].sval = dupstr(buf); |
266 | ret[0].ival = 0; |
267 | |
268 | ret[1].name = "Height"; |
269 | ret[1].type = C_STRING; |
270 | sprintf(buf, "%d", params->height); |
271 | ret[1].sval = dupstr(buf); |
272 | ret[1].ival = 0; |
273 | |
274 | ret[2].name = "Walls wrap around"; |
275 | ret[2].type = C_BOOLEAN; |
276 | ret[2].sval = NULL; |
277 | ret[2].ival = params->wrapping; |
278 | |
279 | ret[3].name = "Barrier probability"; |
280 | ret[3].type = C_STRING; |
281 | sprintf(buf, "%g", params->barrier_probability); |
282 | ret[3].sval = dupstr(buf); |
283 | ret[3].ival = 0; |
284 | |
aa27d493 |
285 | ret[4].name = "Number of shuffling moves"; |
286 | ret[4].type = C_STRING; |
287 | sprintf(buf, "%d", params->movetarget); |
288 | ret[4].sval = dupstr(buf); |
7bed19e1 |
289 | ret[4].ival = 0; |
290 | |
aa27d493 |
291 | ret[5].name = NULL; |
292 | ret[5].type = C_END; |
293 | ret[5].sval = NULL; |
294 | ret[5].ival = 0; |
295 | |
7bed19e1 |
296 | return ret; |
297 | } |
298 | |
be8d5aa1 |
299 | static game_params *custom_params(config_item *cfg) |
7bed19e1 |
300 | { |
301 | game_params *ret = snew(game_params); |
302 | |
303 | ret->width = atoi(cfg[0].sval); |
304 | ret->height = atoi(cfg[1].sval); |
305 | ret->wrapping = cfg[2].ival; |
306 | ret->barrier_probability = (float)atof(cfg[3].sval); |
aa27d493 |
307 | ret->movetarget = atoi(cfg[4].sval); |
7bed19e1 |
308 | |
309 | return ret; |
310 | } |
311 | |
3ff276f2 |
312 | static char *validate_params(game_params *params, int full) |
7bed19e1 |
313 | { |
ab53eb64 |
314 | if (params->width <= 1 || params->height <= 1) |
7bed19e1 |
315 | return "Width and height must both be greater than one"; |
7bed19e1 |
316 | if (params->barrier_probability < 0) |
317 | return "Barrier probability may not be negative"; |
318 | if (params->barrier_probability > 1) |
319 | return "Barrier probability may not be greater than 1"; |
320 | return NULL; |
321 | } |
322 | |
323 | /* ---------------------------------------------------------------------- |
1185e3c5 |
324 | * Randomly select a new game description. |
7bed19e1 |
325 | */ |
326 | |
1185e3c5 |
327 | static char *new_game_desc(game_params *params, random_state *rs, |
c566778e |
328 | char **aux, int interactive) |
7bed19e1 |
329 | { |
1185e3c5 |
330 | tree234 *possibilities, *barriertree; |
331 | int w, h, x, y, cx, cy, nbarriers; |
332 | unsigned char *tiles, *barriers; |
333 | char *desc, *p; |
7bed19e1 |
334 | |
1185e3c5 |
335 | w = params->width; |
336 | h = params->height; |
6f2d8d7c |
337 | |
1185e3c5 |
338 | tiles = snewn(w * h, unsigned char); |
339 | memset(tiles, 0, w * h); |
340 | barriers = snewn(w * h, unsigned char); |
341 | memset(barriers, 0, w * h); |
7bed19e1 |
342 | |
1185e3c5 |
343 | cx = w / 2; |
344 | cy = h / 2; |
7bed19e1 |
345 | |
346 | /* |
347 | * Construct the unshuffled grid. |
348 | * |
349 | * To do this, we simply start at the centre point, repeatedly |
350 | * choose a random possibility out of the available ways to |
351 | * extend a used square into an unused one, and do it. After |
352 | * extending the third line out of a square, we remove the |
353 | * fourth from the possibilities list to avoid any full-cross |
354 | * squares (which would make the game too easy because they |
355 | * only have one orientation). |
356 | * |
357 | * The slightly worrying thing is the avoidance of full-cross |
358 | * squares. Can this cause our unsophisticated construction |
359 | * algorithm to paint itself into a corner, by getting into a |
360 | * situation where there are some unreached squares and the |
361 | * only way to reach any of them is to extend a T-piece into a |
362 | * full cross? |
363 | * |
364 | * Answer: no it can't, and here's a proof. |
365 | * |
366 | * Any contiguous group of such unreachable squares must be |
367 | * surrounded on _all_ sides by T-pieces pointing away from the |
368 | * group. (If not, then there is a square which can be extended |
369 | * into one of the `unreachable' ones, and so it wasn't |
370 | * unreachable after all.) In particular, this implies that |
371 | * each contiguous group of unreachable squares must be |
372 | * rectangular in shape (any deviation from that yields a |
373 | * non-T-piece next to an `unreachable' square). |
374 | * |
375 | * So we have a rectangle of unreachable squares, with T-pieces |
376 | * forming a solid border around the rectangle. The corners of |
377 | * that border must be connected (since every tile connects all |
378 | * the lines arriving in it), and therefore the border must |
379 | * form a closed loop around the rectangle. |
380 | * |
381 | * But this can't have happened in the first place, since we |
382 | * _know_ we've avoided creating closed loops! Hence, no such |
383 | * situation can ever arise, and the naive grid construction |
384 | * algorithm will guaranteeably result in a complete grid |
385 | * containing no unreached squares, no full crosses _and_ no |
386 | * closed loops. [] |
387 | */ |
388 | possibilities = newtree234(xyd_cmp); |
389 | |
1185e3c5 |
390 | if (cx+1 < w) |
391 | add234(possibilities, new_xyd(cx, cy, R)); |
392 | if (cy-1 >= 0) |
393 | add234(possibilities, new_xyd(cx, cy, U)); |
394 | if (cx-1 >= 0) |
395 | add234(possibilities, new_xyd(cx, cy, L)); |
396 | if (cy+1 < h) |
397 | add234(possibilities, new_xyd(cx, cy, D)); |
7bed19e1 |
398 | |
399 | while (count234(possibilities) > 0) { |
400 | int i; |
401 | struct xyd *xyd; |
402 | int x1, y1, d1, x2, y2, d2, d; |
403 | |
404 | /* |
405 | * Extract a randomly chosen possibility from the list. |
406 | */ |
407 | i = random_upto(rs, count234(possibilities)); |
408 | xyd = delpos234(possibilities, i); |
409 | x1 = xyd->x; |
410 | y1 = xyd->y; |
411 | d1 = xyd->direction; |
412 | sfree(xyd); |
413 | |
1185e3c5 |
414 | OFFSET(x2, y2, x1, y1, d1, params); |
7bed19e1 |
415 | d2 = F(d1); |
95854b53 |
416 | #ifdef GENERATION_DIAGNOSTICS |
7bed19e1 |
417 | printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n", |
418 | x1, y1, "0RU3L567D9abcdef"[d1], x2, y2, "0RU3L567D9abcdef"[d2]); |
419 | #endif |
420 | |
421 | /* |
422 | * Make the connection. (We should be moving to an as yet |
423 | * unused tile.) |
424 | */ |
1185e3c5 |
425 | index(params, tiles, x1, y1) |= d1; |
426 | assert(index(params, tiles, x2, y2) == 0); |
427 | index(params, tiles, x2, y2) |= d2; |
7bed19e1 |
428 | |
429 | /* |
430 | * If we have created a T-piece, remove its last |
431 | * possibility. |
432 | */ |
1185e3c5 |
433 | if (COUNT(index(params, tiles, x1, y1)) == 3) { |
7bed19e1 |
434 | struct xyd xyd1, *xydp; |
435 | |
436 | xyd1.x = x1; |
437 | xyd1.y = y1; |
1185e3c5 |
438 | xyd1.direction = 0x0F ^ index(params, tiles, x1, y1); |
7bed19e1 |
439 | |
440 | xydp = find234(possibilities, &xyd1, NULL); |
441 | |
442 | if (xydp) { |
95854b53 |
443 | #ifdef GENERATION_DIAGNOSTICS |
7bed19e1 |
444 | printf("T-piece; removing (%d,%d,%c)\n", |
445 | xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]); |
446 | #endif |
447 | del234(possibilities, xydp); |
448 | sfree(xydp); |
449 | } |
450 | } |
451 | |
452 | /* |
453 | * Remove all other possibilities that were pointing at the |
454 | * tile we've just moved into. |
455 | */ |
456 | for (d = 1; d < 0x10; d <<= 1) { |
457 | int x3, y3, d3; |
458 | struct xyd xyd1, *xydp; |
459 | |
1185e3c5 |
460 | OFFSET(x3, y3, x2, y2, d, params); |
7bed19e1 |
461 | d3 = F(d); |
462 | |
463 | xyd1.x = x3; |
464 | xyd1.y = y3; |
465 | xyd1.direction = d3; |
466 | |
467 | xydp = find234(possibilities, &xyd1, NULL); |
468 | |
469 | if (xydp) { |
95854b53 |
470 | #ifdef GENERATION_DIAGNOSTICS |
7bed19e1 |
471 | printf("Loop avoidance; removing (%d,%d,%c)\n", |
472 | xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]); |
473 | #endif |
474 | del234(possibilities, xydp); |
475 | sfree(xydp); |
476 | } |
477 | } |
478 | |
479 | /* |
480 | * Add new possibilities to the list for moving _out_ of |
481 | * the tile we have just moved into. |
482 | */ |
483 | for (d = 1; d < 0x10; d <<= 1) { |
484 | int x3, y3; |
485 | |
486 | if (d == d2) |
487 | continue; /* we've got this one already */ |
488 | |
1185e3c5 |
489 | if (!params->wrapping) { |
7bed19e1 |
490 | if (d == U && y2 == 0) |
491 | continue; |
1185e3c5 |
492 | if (d == D && y2 == h-1) |
7bed19e1 |
493 | continue; |
494 | if (d == L && x2 == 0) |
495 | continue; |
1185e3c5 |
496 | if (d == R && x2 == w-1) |
7bed19e1 |
497 | continue; |
498 | } |
499 | |
1185e3c5 |
500 | OFFSET(x3, y3, x2, y2, d, params); |
7bed19e1 |
501 | |
1185e3c5 |
502 | if (index(params, tiles, x3, y3)) |
7bed19e1 |
503 | continue; /* this would create a loop */ |
504 | |
95854b53 |
505 | #ifdef GENERATION_DIAGNOSTICS |
7bed19e1 |
506 | printf("New frontier; adding (%d,%d,%c)\n", |
507 | x2, y2, "0RU3L567D9abcdef"[d]); |
508 | #endif |
509 | add234(possibilities, new_xyd(x2, y2, d)); |
510 | } |
511 | } |
512 | /* Having done that, we should have no possibilities remaining. */ |
513 | assert(count234(possibilities) == 0); |
514 | freetree234(possibilities); |
515 | |
516 | /* |
517 | * Now compute a list of the possible barrier locations. |
518 | */ |
1185e3c5 |
519 | barriertree = newtree234(xyd_cmp); |
520 | for (y = 0; y < h; y++) { |
521 | for (x = 0; x < w; x++) { |
522 | |
523 | if (!(index(params, tiles, x, y) & R) && |
524 | (params->wrapping || x < w-1)) |
525 | add234(barriertree, new_xyd(x, y, R)); |
526 | if (!(index(params, tiles, x, y) & D) && |
527 | (params->wrapping || y < h-1)) |
528 | add234(barriertree, new_xyd(x, y, D)); |
7bed19e1 |
529 | } |
530 | } |
531 | |
532 | /* |
c566778e |
533 | * Save the unshuffled grid in aux. |
2ac6d24e |
534 | */ |
535 | { |
c566778e |
536 | char *solution; |
537 | int i; |
538 | |
539 | /* |
540 | * String format is exactly the same as a solve move, so we |
541 | * can just dupstr this in solve_game(). |
542 | */ |
2ac6d24e |
543 | |
c566778e |
544 | solution = snewn(w * h + 2, char); |
545 | solution[0] = 'S'; |
546 | for (i = 0; i < w * h; i++) |
547 | solution[i+1] = "0123456789abcdef"[tiles[i] & 0xF]; |
548 | solution[w*h+1] = '\0'; |
2ac6d24e |
549 | |
1185e3c5 |
550 | *aux = solution; |
2ac6d24e |
551 | } |
552 | |
553 | /* |
7bed19e1 |
554 | * Now shuffle the grid. |
aa27d493 |
555 | * FIXME - this simply does a set of random moves to shuffle the pieces, |
556 | * although we make a token effort to avoid boring cases by avoiding moves |
557 | * that directly undo the previous one, or that repeat so often as to |
558 | * turn into fewer moves. |
559 | * |
7bed19e1 |
560 | * A better way would be to number all the pieces, generate a placement |
561 | * for all the numbers as for "sixteen", observing parity constraints if |
562 | * neccessary, and then place the pieces according to their numbering. |
563 | * BUT - I'm not sure if this will work, since we disallow movement of |
564 | * the middle row and column. |
565 | */ |
566 | { |
567 | int i; |
1185e3c5 |
568 | int cols = w - 1; |
569 | int rows = h - 1; |
aa27d493 |
570 | int moves = params->movetarget; |
571 | int prevdir = -1, prevrowcol = -1, nrepeats = 0; |
572 | if (!moves) moves = cols * rows * 2; |
573 | for (i = 0; i < moves; /* incremented conditionally */) { |
7bed19e1 |
574 | /* Choose a direction: 0,1,2,3 = up, right, down, left. */ |
575 | int dir = random_upto(rs, 4); |
aa27d493 |
576 | int rowcol; |
7bed19e1 |
577 | if (dir % 2 == 0) { |
578 | int col = random_upto(rs, cols); |
aa27d493 |
579 | if (col >= cx) col += 1; /* avoid centre */ |
580 | if (col == prevrowcol) { |
581 | if (dir == 2-prevdir) |
582 | continue; /* undoes last move */ |
3fcd6cf0 |
583 | else if (dir == prevdir && (nrepeats+1)*2 > h) |
aa27d493 |
584 | continue; /* makes fewer moves */ |
585 | } |
1185e3c5 |
586 | slide_col_int(w, h, tiles, 1 - dir, col); |
aa27d493 |
587 | rowcol = col; |
7bed19e1 |
588 | } else { |
589 | int row = random_upto(rs, rows); |
aa27d493 |
590 | if (row >= cy) row += 1; /* avoid centre */ |
591 | if (row == prevrowcol) { |
592 | if (dir == 4-prevdir) |
593 | continue; /* undoes last move */ |
3fcd6cf0 |
594 | else if (dir == prevdir && (nrepeats+1)*2 > w) |
aa27d493 |
595 | continue; /* makes fewer moves */ |
596 | } |
1185e3c5 |
597 | slide_row_int(w, h, tiles, 2 - dir, row); |
aa27d493 |
598 | rowcol = row; |
7bed19e1 |
599 | } |
aa27d493 |
600 | if (dir == prevdir && rowcol == prevrowcol) |
601 | nrepeats++; |
602 | else |
603 | nrepeats = 1; |
604 | prevdir = dir; |
605 | prevrowcol = rowcol; |
606 | i++; /* if we got here, the move was accepted */ |
7bed19e1 |
607 | } |
608 | } |
609 | |
610 | /* |
611 | * And now choose barrier locations. (We carefully do this |
612 | * _after_ shuffling, so that changing the barrier rate in the |
1185e3c5 |
613 | * params while keeping the random seed the same will give the |
7bed19e1 |
614 | * same shuffled grid and _only_ change the barrier locations. |
615 | * Also the way we choose barrier locations, by repeatedly |
616 | * choosing one possibility from the list until we have enough, |
617 | * is designed to ensure that raising the barrier rate while |
618 | * keeping the seed the same will provide a superset of the |
619 | * previous barrier set - i.e. if you ask for 10 barriers, and |
620 | * then decide that's still too hard and ask for 20, you'll get |
621 | * the original 10 plus 10 more, rather than getting 20 new |
622 | * ones and the chance of remembering your first 10.) |
623 | */ |
1185e3c5 |
624 | nbarriers = (int)(params->barrier_probability * count234(barriertree)); |
625 | assert(nbarriers >= 0 && nbarriers <= count234(barriertree)); |
7bed19e1 |
626 | |
627 | while (nbarriers > 0) { |
628 | int i; |
629 | struct xyd *xyd; |
630 | int x1, y1, d1, x2, y2, d2; |
631 | |
632 | /* |
633 | * Extract a randomly chosen barrier from the list. |
634 | */ |
1185e3c5 |
635 | i = random_upto(rs, count234(barriertree)); |
636 | xyd = delpos234(barriertree, i); |
7bed19e1 |
637 | |
638 | assert(xyd != NULL); |
639 | |
640 | x1 = xyd->x; |
641 | y1 = xyd->y; |
642 | d1 = xyd->direction; |
643 | sfree(xyd); |
644 | |
1185e3c5 |
645 | OFFSET(x2, y2, x1, y1, d1, params); |
7bed19e1 |
646 | d2 = F(d1); |
647 | |
1185e3c5 |
648 | index(params, barriers, x1, y1) |= d1; |
649 | index(params, barriers, x2, y2) |= d2; |
7bed19e1 |
650 | |
651 | nbarriers--; |
652 | } |
653 | |
654 | /* |
655 | * Clean up the rest of the barrier list. |
656 | */ |
657 | { |
658 | struct xyd *xyd; |
659 | |
1185e3c5 |
660 | while ( (xyd = delpos234(barriertree, 0)) != NULL) |
7bed19e1 |
661 | sfree(xyd); |
662 | |
1185e3c5 |
663 | freetree234(barriertree); |
664 | } |
665 | |
666 | /* |
667 | * Finally, encode the grid into a string game description. |
668 | * |
669 | * My syntax is extremely simple: each square is encoded as a |
670 | * hex digit in which bit 0 means a connection on the right, |
671 | * bit 1 means up, bit 2 left and bit 3 down. (i.e. the same |
672 | * encoding as used internally). Each digit is followed by |
673 | * optional barrier indicators: `v' means a vertical barrier to |
674 | * the right of it, and `h' means a horizontal barrier below |
675 | * it. |
676 | */ |
677 | desc = snewn(w * h * 3 + 1, char); |
678 | p = desc; |
679 | for (y = 0; y < h; y++) { |
680 | for (x = 0; x < w; x++) { |
681 | *p++ = "0123456789abcdef"[index(params, tiles, x, y)]; |
682 | if ((params->wrapping || x < w-1) && |
683 | (index(params, barriers, x, y) & R)) |
684 | *p++ = 'v'; |
685 | if ((params->wrapping || y < h-1) && |
686 | (index(params, barriers, x, y) & D)) |
687 | *p++ = 'h'; |
688 | } |
689 | } |
690 | assert(p - desc <= w*h*3); |
366d045b |
691 | *p = '\0'; |
1185e3c5 |
692 | |
693 | sfree(tiles); |
694 | sfree(barriers); |
695 | |
696 | return desc; |
697 | } |
698 | |
1185e3c5 |
699 | static char *validate_desc(game_params *params, char *desc) |
700 | { |
701 | int w = params->width, h = params->height; |
702 | int i; |
703 | |
704 | for (i = 0; i < w*h; i++) { |
705 | if (*desc >= '0' && *desc <= '9') |
706 | /* OK */; |
707 | else if (*desc >= 'a' && *desc <= 'f') |
708 | /* OK */; |
709 | else if (*desc >= 'A' && *desc <= 'F') |
710 | /* OK */; |
711 | else if (!*desc) |
712 | return "Game description shorter than expected"; |
713 | else |
714 | return "Game description contained unexpected character"; |
715 | desc++; |
716 | while (*desc == 'h' || *desc == 'v') |
717 | desc++; |
718 | } |
719 | if (*desc) |
720 | return "Game description longer than expected"; |
721 | |
722 | return NULL; |
723 | } |
724 | |
725 | /* ---------------------------------------------------------------------- |
726 | * Construct an initial game state, given a description and parameters. |
727 | */ |
728 | |
dafd6cf6 |
729 | static game_state *new_game(midend *me, game_params *params, char *desc) |
1185e3c5 |
730 | { |
731 | game_state *state; |
732 | int w, h, x, y; |
733 | |
734 | assert(params->width > 0 && params->height > 0); |
735 | assert(params->width > 1 || params->height > 1); |
736 | |
737 | /* |
738 | * Create a blank game state. |
739 | */ |
740 | state = snew(game_state); |
741 | w = state->width = params->width; |
742 | h = state->height = params->height; |
743 | state->cx = state->width / 2; |
744 | state->cy = state->height / 2; |
745 | state->wrapping = params->wrapping; |
aa27d493 |
746 | state->movetarget = params->movetarget; |
1185e3c5 |
747 | state->completed = 0; |
a440f184 |
748 | state->used_solve = FALSE; |
1185e3c5 |
749 | state->move_count = 0; |
750 | state->last_move_row = -1; |
751 | state->last_move_col = -1; |
752 | state->last_move_dir = 0; |
753 | state->tiles = snewn(state->width * state->height, unsigned char); |
754 | memset(state->tiles, 0, state->width * state->height); |
755 | state->barriers = snewn(state->width * state->height, unsigned char); |
756 | memset(state->barriers, 0, state->width * state->height); |
757 | |
758 | |
759 | /* |
760 | * Parse the game description into the grid. |
761 | */ |
762 | for (y = 0; y < h; y++) { |
763 | for (x = 0; x < w; x++) { |
764 | if (*desc >= '0' && *desc <= '9') |
765 | tile(state, x, y) = *desc - '0'; |
766 | else if (*desc >= 'a' && *desc <= 'f') |
767 | tile(state, x, y) = *desc - 'a' + 10; |
768 | else if (*desc >= 'A' && *desc <= 'F') |
769 | tile(state, x, y) = *desc - 'A' + 10; |
770 | if (*desc) |
771 | desc++; |
772 | while (*desc == 'h' || *desc == 'v') { |
773 | int x2, y2, d1, d2; |
774 | if (*desc == 'v') |
775 | d1 = R; |
776 | else |
777 | d1 = D; |
778 | |
779 | OFFSET(x2, y2, x, y, d1, state); |
780 | d2 = F(d1); |
781 | |
782 | barrier(state, x, y) |= d1; |
783 | barrier(state, x2, y2) |= d2; |
784 | |
785 | desc++; |
786 | } |
787 | } |
788 | } |
789 | |
790 | /* |
791 | * Set up border barriers if this is a non-wrapping game. |
792 | */ |
793 | if (!state->wrapping) { |
794 | for (x = 0; x < state->width; x++) { |
795 | barrier(state, x, 0) |= U; |
796 | barrier(state, x, state->height-1) |= D; |
797 | } |
798 | for (y = 0; y < state->height; y++) { |
799 | barrier(state, 0, y) |= L; |
800 | barrier(state, state->width-1, y) |= R; |
801 | } |
7bed19e1 |
802 | } |
803 | |
804 | /* |
805 | * Set up the barrier corner flags, for drawing barriers |
806 | * prettily when they meet. |
807 | */ |
808 | for (y = 0; y < state->height; y++) { |
809 | for (x = 0; x < state->width; x++) { |
810 | int dir; |
811 | |
812 | for (dir = 1; dir < 0x10; dir <<= 1) { |
813 | int dir2 = A(dir); |
814 | int x1, y1, x2, y2, x3, y3; |
815 | int corner = FALSE; |
816 | |
817 | if (!(barrier(state, x, y) & dir)) |
818 | continue; |
819 | |
820 | if (barrier(state, x, y) & dir2) |
821 | corner = TRUE; |
822 | |
823 | x1 = x + X(dir), y1 = y + Y(dir); |
824 | if (x1 >= 0 && x1 < state->width && |
825 | y1 >= 0 && y1 < state->height && |
826 | (barrier(state, x1, y1) & dir2)) |
827 | corner = TRUE; |
828 | |
829 | x2 = x + X(dir2), y2 = y + Y(dir2); |
830 | if (x2 >= 0 && x2 < state->width && |
831 | y2 >= 0 && y2 < state->height && |
832 | (barrier(state, x2, y2) & dir)) |
833 | corner = TRUE; |
834 | |
835 | if (corner) { |
836 | barrier(state, x, y) |= (dir << 4); |
837 | if (x1 >= 0 && x1 < state->width && |
838 | y1 >= 0 && y1 < state->height) |
839 | barrier(state, x1, y1) |= (A(dir) << 4); |
840 | if (x2 >= 0 && x2 < state->width && |
841 | y2 >= 0 && y2 < state->height) |
842 | barrier(state, x2, y2) |= (C(dir) << 4); |
843 | x3 = x + X(dir) + X(dir2), y3 = y + Y(dir) + Y(dir2); |
844 | if (x3 >= 0 && x3 < state->width && |
845 | y3 >= 0 && y3 < state->height) |
846 | barrier(state, x3, y3) |= (F(dir) << 4); |
847 | } |
848 | } |
849 | } |
850 | } |
851 | |
7bed19e1 |
852 | return state; |
853 | } |
854 | |
be8d5aa1 |
855 | static game_state *dup_game(game_state *state) |
7bed19e1 |
856 | { |
857 | game_state *ret; |
858 | |
859 | ret = snew(game_state); |
860 | ret->width = state->width; |
861 | ret->height = state->height; |
862 | ret->cx = state->cx; |
863 | ret->cy = state->cy; |
864 | ret->wrapping = state->wrapping; |
aa27d493 |
865 | ret->movetarget = state->movetarget; |
7bed19e1 |
866 | ret->completed = state->completed; |
2ac6d24e |
867 | ret->used_solve = state->used_solve; |
7bed19e1 |
868 | ret->move_count = state->move_count; |
869 | ret->last_move_row = state->last_move_row; |
870 | ret->last_move_col = state->last_move_col; |
871 | ret->last_move_dir = state->last_move_dir; |
872 | ret->tiles = snewn(state->width * state->height, unsigned char); |
873 | memcpy(ret->tiles, state->tiles, state->width * state->height); |
874 | ret->barriers = snewn(state->width * state->height, unsigned char); |
875 | memcpy(ret->barriers, state->barriers, state->width * state->height); |
876 | |
877 | return ret; |
878 | } |
879 | |
be8d5aa1 |
880 | static void free_game(game_state *state) |
7bed19e1 |
881 | { |
882 | sfree(state->tiles); |
883 | sfree(state->barriers); |
884 | sfree(state); |
885 | } |
886 | |
df11cd4e |
887 | static char *solve_game(game_state *state, game_state *currstate, |
c566778e |
888 | char *aux, char **error) |
2ac6d24e |
889 | { |
1185e3c5 |
890 | if (!aux) { |
2ac6d24e |
891 | *error = "Solution not known for this puzzle"; |
892 | return NULL; |
893 | } |
894 | |
c566778e |
895 | return dupstr(aux); |
2ac6d24e |
896 | } |
897 | |
fa3abef5 |
898 | static int game_can_format_as_text_now(game_params *params) |
899 | { |
900 | return TRUE; |
901 | } |
902 | |
9b4b03d3 |
903 | static char *game_text_format(game_state *state) |
904 | { |
905 | return NULL; |
906 | } |
907 | |
7bed19e1 |
908 | /* ---------------------------------------------------------------------- |
909 | * Utility routine. |
910 | */ |
911 | |
912 | /* |
913 | * Compute which squares are reachable from the centre square, as a |
914 | * quick visual aid to determining how close the game is to |
915 | * completion. This is also a simple way to tell if the game _is_ |
916 | * completed - just call this function and see whether every square |
917 | * is marked active. |
918 | * |
919 | * squares in the moving_row and moving_col are always inactive - this |
920 | * is so that "current" doesn't appear to jump across moving lines. |
921 | */ |
922 | static unsigned char *compute_active(game_state *state, |
923 | int moving_row, int moving_col) |
924 | { |
925 | unsigned char *active; |
926 | tree234 *todo; |
927 | struct xyd *xyd; |
928 | |
929 | active = snewn(state->width * state->height, unsigned char); |
930 | memset(active, 0, state->width * state->height); |
931 | |
932 | /* |
933 | * We only store (x,y) pairs in todo, but it's easier to reuse |
934 | * xyd_cmp and just store direction 0 every time. |
935 | */ |
936 | todo = newtree234(xyd_cmp); |
937 | index(state, active, state->cx, state->cy) = ACTIVE; |
938 | add234(todo, new_xyd(state->cx, state->cy, 0)); |
939 | |
940 | while ( (xyd = delpos234(todo, 0)) != NULL) { |
941 | int x1, y1, d1, x2, y2, d2; |
942 | |
943 | x1 = xyd->x; |
944 | y1 = xyd->y; |
945 | sfree(xyd); |
946 | |
947 | for (d1 = 1; d1 < 0x10; d1 <<= 1) { |
948 | OFFSET(x2, y2, x1, y1, d1, state); |
949 | d2 = F(d1); |
950 | |
951 | /* |
952 | * If the next tile in this direction is connected to |
953 | * us, and there isn't a barrier in the way, and it |
954 | * isn't already marked active, then mark it active and |
955 | * add it to the to-examine list. |
956 | */ |
957 | if ((x2 != moving_col && y2 != moving_row) && |
958 | (tile(state, x1, y1) & d1) && |
959 | (tile(state, x2, y2) & d2) && |
960 | !(barrier(state, x1, y1) & d1) && |
961 | !index(state, active, x2, y2)) { |
962 | index(state, active, x2, y2) = ACTIVE; |
963 | add234(todo, new_xyd(x2, y2, 0)); |
964 | } |
965 | } |
966 | } |
967 | /* Now we expect the todo list to have shrunk to zero size. */ |
968 | assert(count234(todo) == 0); |
969 | freetree234(todo); |
970 | |
971 | return active; |
972 | } |
973 | |
974 | struct game_ui { |
975 | int cur_x, cur_y; |
976 | int cur_visible; |
977 | }; |
978 | |
be8d5aa1 |
979 | static game_ui *new_ui(game_state *state) |
7bed19e1 |
980 | { |
981 | game_ui *ui = snew(game_ui); |
3e17893b |
982 | ui->cur_x = 0; |
983 | ui->cur_y = -1; |
7bed19e1 |
984 | ui->cur_visible = FALSE; |
985 | |
986 | return ui; |
987 | } |
988 | |
be8d5aa1 |
989 | static void free_ui(game_ui *ui) |
7bed19e1 |
990 | { |
991 | sfree(ui); |
992 | } |
993 | |
844f605f |
994 | static char *encode_ui(game_ui *ui) |
ae8290c6 |
995 | { |
996 | return NULL; |
997 | } |
998 | |
844f605f |
999 | static void decode_ui(game_ui *ui, char *encoding) |
ae8290c6 |
1000 | { |
1001 | } |
1002 | |
7bed19e1 |
1003 | /* ---------------------------------------------------------------------- |
1004 | * Process a move. |
1005 | */ |
1006 | |
1185e3c5 |
1007 | static void slide_row_int(int w, int h, unsigned char *tiles, int dir, int row) |
7bed19e1 |
1008 | { |
1185e3c5 |
1009 | int x = dir > 0 ? -1 : w; |
7bed19e1 |
1010 | int tx = x + dir; |
1185e3c5 |
1011 | int n = w - 1; |
1012 | unsigned char endtile = tiles[row * w + tx]; |
7bed19e1 |
1013 | do { |
1014 | x = tx; |
1185e3c5 |
1015 | tx = (x + dir + w) % w; |
1016 | tiles[row * w + x] = tiles[row * w + tx]; |
7bed19e1 |
1017 | } while (--n > 0); |
1185e3c5 |
1018 | tiles[row * w + tx] = endtile; |
7bed19e1 |
1019 | } |
1020 | |
1185e3c5 |
1021 | static void slide_col_int(int w, int h, unsigned char *tiles, int dir, int col) |
7bed19e1 |
1022 | { |
1185e3c5 |
1023 | int y = dir > 0 ? -1 : h; |
7bed19e1 |
1024 | int ty = y + dir; |
1185e3c5 |
1025 | int n = h - 1; |
1026 | unsigned char endtile = tiles[ty * w + col]; |
7bed19e1 |
1027 | do { |
1028 | y = ty; |
1185e3c5 |
1029 | ty = (y + dir + h) % h; |
1030 | tiles[y * w + col] = tiles[ty * w + col]; |
7bed19e1 |
1031 | } while (--n > 0); |
1185e3c5 |
1032 | tiles[ty * w + col] = endtile; |
1033 | } |
1034 | |
1035 | static void slide_row(game_state *state, int dir, int row) |
1036 | { |
1037 | slide_row_int(state->width, state->height, state->tiles, dir, row); |
1038 | } |
1039 | |
1040 | static void slide_col(game_state *state, int dir, int col) |
1041 | { |
1042 | slide_col_int(state->width, state->height, state->tiles, dir, col); |
7bed19e1 |
1043 | } |
1044 | |
07dfb697 |
1045 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
1046 | game_state *newstate) |
1047 | { |
1048 | } |
1049 | |
1e3e152d |
1050 | struct game_drawstate { |
1051 | int started; |
1052 | int width, height; |
1053 | int tilesize; |
1054 | unsigned char *visible; |
3e17893b |
1055 | int cur_x, cur_y; |
1e3e152d |
1056 | }; |
1057 | |
df11cd4e |
1058 | static char *interpret_move(game_state *state, game_ui *ui, |
1059 | game_drawstate *ds, int x, int y, int button) |
7bed19e1 |
1060 | { |
1061 | int cx, cy; |
1062 | int n, dx, dy; |
df11cd4e |
1063 | char buf[80]; |
7bed19e1 |
1064 | |
f0ee053c |
1065 | button &= ~MOD_MASK; |
1066 | |
3e17893b |
1067 | if (IS_CURSOR_MOVE(button)) { |
1068 | int cpos, diff = 0; |
1069 | cpos = c2pos(state->width, state->height, ui->cur_x, ui->cur_y); |
1070 | diff = c2diff(state->width, state->height, ui->cur_x, ui->cur_y, button); |
1071 | |
1072 | if (diff != 0) { |
1073 | do { /* we might have to do this more than once to skip missing arrows */ |
1074 | cpos += diff; |
1075 | pos2c(state->width, state->height, cpos, &ui->cur_x, &ui->cur_y); |
1076 | } while (ui->cur_x == state->cx || ui->cur_y == state->cy); |
1077 | } |
1078 | |
1079 | ui->cur_visible = 1; |
1080 | return ""; |
1081 | } |
7bed19e1 |
1082 | |
3e17893b |
1083 | if (button == LEFT_BUTTON || button == RIGHT_BUTTON) { |
1084 | cx = (x - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2; |
1085 | cy = (y - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2; |
1086 | ui->cur_visible = 0; |
1087 | } else if (IS_CURSOR_SELECT(button)) { |
1088 | if (ui->cur_visible) { |
1089 | cx = ui->cur_x; |
1090 | cy = ui->cur_y; |
1091 | } else { |
1092 | /* 'click' when cursor is invisible just makes cursor visible. */ |
1093 | ui->cur_visible = 1; |
1094 | return ""; |
1095 | } |
1096 | } else |
1097 | return NULL; |
7bed19e1 |
1098 | |
1099 | if (cy >= 0 && cy < state->height && cy != state->cy) |
1100 | { |
1101 | if (cx == -1) dx = +1; |
1102 | else if (cx == state->width) dx = -1; |
1103 | else return NULL; |
1104 | n = state->width; |
1105 | dy = 0; |
1106 | } |
1107 | else if (cx >= 0 && cx < state->width && cx != state->cx) |
1108 | { |
1109 | if (cy == -1) dy = +1; |
1110 | else if (cy == state->height) dy = -1; |
1111 | else return NULL; |
1112 | n = state->height; |
1113 | dx = 0; |
1114 | } |
1115 | else |
1116 | return NULL; |
1117 | |
1118 | /* reverse direction if right hand button is pressed */ |
1119 | if (button == RIGHT_BUTTON) |
1120 | { |
1121 | dx = -dx; |
1122 | dy = -dy; |
1123 | } |
1124 | |
df11cd4e |
1125 | if (dx == 0) |
1126 | sprintf(buf, "C%d,%d", cx, dy); |
1127 | else |
1128 | sprintf(buf, "R%d,%d", cy, dx); |
1129 | return dupstr(buf); |
1130 | } |
1131 | |
1132 | static game_state *execute_move(game_state *from, char *move) |
1133 | { |
1134 | game_state *ret; |
1135 | int c, d, col; |
1136 | |
1137 | if ((move[0] == 'C' || move[0] == 'R') && |
1138 | sscanf(move+1, "%d,%d", &c, &d) == 2 && |
1139 | c >= 0 && c < (move[0] == 'C' ? from->width : from->height)) { |
1140 | col = (move[0] == 'C'); |
1141 | } else if (move[0] == 'S' && |
1142 | strlen(move) == from->width * from->height + 1) { |
1143 | int i; |
1144 | ret = dup_game(from); |
a440f184 |
1145 | ret->used_solve = TRUE; |
df11cd4e |
1146 | ret->completed = ret->move_count = 1; |
1147 | |
1148 | for (i = 0; i < from->width * from->height; i++) { |
1149 | c = move[i+1]; |
1150 | if (c >= '0' && c <= '9') |
1151 | c -= '0'; |
1152 | else if (c >= 'A' && c <= 'F') |
1153 | c -= 'A' - 10; |
1154 | else if (c >= 'a' && c <= 'f') |
1155 | c -= 'a' - 10; |
1156 | else { |
1157 | free_game(ret); |
1158 | return NULL; |
1159 | } |
1160 | ret->tiles[i] = c; |
1161 | } |
1162 | return ret; |
1163 | } else |
1164 | return NULL; /* can't parse move string */ |
1165 | |
1166 | ret = dup_game(from); |
7bed19e1 |
1167 | |
df11cd4e |
1168 | if (col) |
1169 | slide_col(ret, d, c); |
1170 | else |
1171 | slide_row(ret, d, c); |
7bed19e1 |
1172 | |
1173 | ret->move_count++; |
df11cd4e |
1174 | ret->last_move_row = col ? -1 : c; |
1175 | ret->last_move_col = col ? c : -1; |
1176 | ret->last_move_dir = d; |
7bed19e1 |
1177 | |
1178 | /* |
1179 | * See if the game has been completed. |
1180 | */ |
1181 | if (!ret->completed) { |
1182 | unsigned char *active = compute_active(ret, -1, -1); |
1183 | int x1, y1; |
1184 | int complete = TRUE; |
1185 | |
1186 | for (x1 = 0; x1 < ret->width; x1++) |
1187 | for (y1 = 0; y1 < ret->height; y1++) |
1188 | if (!index(ret, active, x1, y1)) { |
1189 | complete = FALSE; |
1190 | goto break_label; /* break out of two loops at once */ |
1191 | } |
1192 | break_label: |
1193 | |
1194 | sfree(active); |
1195 | |
1196 | if (complete) |
1197 | ret->completed = ret->move_count; |
1198 | } |
1199 | |
1200 | return ret; |
1201 | } |
1202 | |
1203 | /* ---------------------------------------------------------------------- |
1204 | * Routines for drawing the game position on the screen. |
1205 | */ |
1206 | |
dafd6cf6 |
1207 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
7bed19e1 |
1208 | { |
1209 | game_drawstate *ds = snew(game_drawstate); |
1210 | |
1211 | ds->started = FALSE; |
1212 | ds->width = state->width; |
1213 | ds->height = state->height; |
1214 | ds->visible = snewn(state->width * state->height, unsigned char); |
1e3e152d |
1215 | ds->tilesize = 0; /* not decided yet */ |
7bed19e1 |
1216 | memset(ds->visible, 0xFF, state->width * state->height); |
3e17893b |
1217 | ds->cur_x = ds->cur_y = -1; |
7bed19e1 |
1218 | |
1219 | return ds; |
1220 | } |
1221 | |
dafd6cf6 |
1222 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
7bed19e1 |
1223 | { |
1224 | sfree(ds->visible); |
1225 | sfree(ds); |
1226 | } |
1227 | |
1f3ee4ee |
1228 | static void game_compute_size(game_params *params, int tilesize, |
1229 | int *x, int *y) |
7bed19e1 |
1230 | { |
1f3ee4ee |
1231 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
1232 | struct { int tilesize; } ads, *ds = &ads; |
1233 | ads.tilesize = tilesize; |
1e3e152d |
1234 | |
7bed19e1 |
1235 | *x = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->width + TILE_BORDER; |
1236 | *y = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->height + TILE_BORDER; |
1237 | } |
1238 | |
dafd6cf6 |
1239 | static void game_set_size(drawing *dr, game_drawstate *ds, |
1240 | game_params *params, int tilesize) |
1f3ee4ee |
1241 | { |
1242 | ds->tilesize = tilesize; |
1243 | } |
1244 | |
8266f3fc |
1245 | static float *game_colours(frontend *fe, int *ncolours) |
7bed19e1 |
1246 | { |
1247 | float *ret; |
1248 | |
1249 | ret = snewn(NCOLOURS * 3, float); |
1250 | *ncolours = NCOLOURS; |
1251 | |
1252 | /* |
1253 | * Basic background colour is whatever the front end thinks is |
1254 | * a sensible default. |
1255 | */ |
1256 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
1257 | |
1258 | /* |
1259 | * Wires are black. |
1260 | */ |
1261 | ret[COL_WIRE * 3 + 0] = 0.0F; |
1262 | ret[COL_WIRE * 3 + 1] = 0.0F; |
1263 | ret[COL_WIRE * 3 + 2] = 0.0F; |
1264 | |
1265 | /* |
1266 | * Powered wires and powered endpoints are cyan. |
1267 | */ |
1268 | ret[COL_POWERED * 3 + 0] = 0.0F; |
1269 | ret[COL_POWERED * 3 + 1] = 1.0F; |
1270 | ret[COL_POWERED * 3 + 2] = 1.0F; |
1271 | |
1272 | /* |
1273 | * Barriers are red. |
1274 | */ |
1275 | ret[COL_BARRIER * 3 + 0] = 1.0F; |
1276 | ret[COL_BARRIER * 3 + 1] = 0.0F; |
1277 | ret[COL_BARRIER * 3 + 2] = 0.0F; |
1278 | |
1279 | /* |
1280 | * Unpowered endpoints are blue. |
1281 | */ |
1282 | ret[COL_ENDPOINT * 3 + 0] = 0.0F; |
1283 | ret[COL_ENDPOINT * 3 + 1] = 0.0F; |
1284 | ret[COL_ENDPOINT * 3 + 2] = 1.0F; |
1285 | |
1286 | /* |
1287 | * Tile borders are a darker grey than the background. |
1288 | */ |
1289 | ret[COL_BORDER * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0]; |
1290 | ret[COL_BORDER * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1]; |
1291 | ret[COL_BORDER * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2]; |
1292 | |
1293 | /* |
1294 | * Flashing tiles are a grey in between those two. |
1295 | */ |
1296 | ret[COL_FLASHING * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0]; |
1297 | ret[COL_FLASHING * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1]; |
1298 | ret[COL_FLASHING * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2]; |
1299 | |
1300 | ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.8F; |
1301 | ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.8F; |
1302 | ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.8F; |
1303 | ret[COL_TEXT * 3 + 0] = 0.0; |
1304 | ret[COL_TEXT * 3 + 1] = 0.0; |
1305 | ret[COL_TEXT * 3 + 2] = 0.0; |
1306 | |
1307 | return ret; |
1308 | } |
1309 | |
dafd6cf6 |
1310 | static void draw_thick_line(drawing *dr, int x1, int y1, int x2, int y2, |
7bed19e1 |
1311 | int colour) |
1312 | { |
dafd6cf6 |
1313 | draw_line(dr, x1-1, y1, x2-1, y2, COL_WIRE); |
1314 | draw_line(dr, x1+1, y1, x2+1, y2, COL_WIRE); |
1315 | draw_line(dr, x1, y1-1, x2, y2-1, COL_WIRE); |
1316 | draw_line(dr, x1, y1+1, x2, y2+1, COL_WIRE); |
1317 | draw_line(dr, x1, y1, x2, y2, colour); |
7bed19e1 |
1318 | } |
1319 | |
dafd6cf6 |
1320 | static void draw_rect_coords(drawing *dr, int x1, int y1, int x2, int y2, |
7bed19e1 |
1321 | int colour) |
1322 | { |
1323 | int mx = (x1 < x2 ? x1 : x2); |
1324 | int my = (y1 < y2 ? y1 : y2); |
1325 | int dx = (x2 + x1 - 2*mx + 1); |
1326 | int dy = (y2 + y1 - 2*my + 1); |
1327 | |
dafd6cf6 |
1328 | draw_rect(dr, mx, my, dx, dy, colour); |
7bed19e1 |
1329 | } |
1330 | |
dafd6cf6 |
1331 | static void draw_barrier_corner(drawing *dr, game_drawstate *ds, |
1e3e152d |
1332 | int x, int y, int dir, int phase) |
7bed19e1 |
1333 | { |
1334 | int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x; |
1335 | int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y; |
1336 | int x1, y1, dx, dy, dir2; |
1337 | |
1338 | dir >>= 4; |
1339 | |
1340 | dir2 = A(dir); |
1341 | dx = X(dir) + X(dir2); |
1342 | dy = Y(dir) + Y(dir2); |
1343 | x1 = (dx > 0 ? TILE_SIZE+TILE_BORDER-1 : 0); |
1344 | y1 = (dy > 0 ? TILE_SIZE+TILE_BORDER-1 : 0); |
1345 | |
1346 | if (phase == 0) { |
dafd6cf6 |
1347 | draw_rect_coords(dr, bx+x1, by+y1, |
7bed19e1 |
1348 | bx+x1-TILE_BORDER*dx, by+y1-(TILE_BORDER-1)*dy, |
1349 | COL_WIRE); |
dafd6cf6 |
1350 | draw_rect_coords(dr, bx+x1, by+y1, |
7bed19e1 |
1351 | bx+x1-(TILE_BORDER-1)*dx, by+y1-TILE_BORDER*dy, |
1352 | COL_WIRE); |
1353 | } else { |
dafd6cf6 |
1354 | draw_rect_coords(dr, bx+x1, by+y1, |
7bed19e1 |
1355 | bx+x1-(TILE_BORDER-1)*dx, by+y1-(TILE_BORDER-1)*dy, |
1356 | COL_BARRIER); |
1357 | } |
1358 | } |
1359 | |
dafd6cf6 |
1360 | static void draw_barrier(drawing *dr, game_drawstate *ds, |
1e3e152d |
1361 | int x, int y, int dir, int phase) |
7bed19e1 |
1362 | { |
1363 | int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x; |
1364 | int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y; |
1365 | int x1, y1, w, h; |
1366 | |
1367 | x1 = (X(dir) > 0 ? TILE_SIZE : X(dir) == 0 ? TILE_BORDER : 0); |
1368 | y1 = (Y(dir) > 0 ? TILE_SIZE : Y(dir) == 0 ? TILE_BORDER : 0); |
1369 | w = (X(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER); |
1370 | h = (Y(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER); |
1371 | |
1372 | if (phase == 0) { |
dafd6cf6 |
1373 | draw_rect(dr, bx+x1-X(dir), by+y1-Y(dir), w, h, COL_WIRE); |
7bed19e1 |
1374 | } else { |
dafd6cf6 |
1375 | draw_rect(dr, bx+x1, by+y1, w, h, COL_BARRIER); |
7bed19e1 |
1376 | } |
1377 | } |
1378 | |
dafd6cf6 |
1379 | static void draw_tile(drawing *dr, game_drawstate *ds, game_state *state, |
1e3e152d |
1380 | int x, int y, int tile, float xshift, float yshift) |
7bed19e1 |
1381 | { |
3e17893b |
1382 | int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x + (int)(xshift * TILE_SIZE); |
1383 | int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y + (int)(yshift * TILE_SIZE); |
7bed19e1 |
1384 | float cx, cy, ex, ey; |
1385 | int dir, col; |
1386 | |
1387 | /* |
1388 | * When we draw a single tile, we must draw everything up to |
1389 | * and including the borders around the tile. This means that |
1390 | * if the neighbouring tiles have connections to those borders, |
1391 | * we must draw those connections on the borders themselves. |
1392 | * |
1393 | * This would be terribly fiddly if we ever had to draw a tile |
1394 | * while its neighbour was in mid-rotate, because we'd have to |
1395 | * arrange to _know_ that the neighbour was being rotated and |
1396 | * hence had an anomalous effect on the redraw of this tile. |
1397 | * Fortunately, the drawing algorithm avoids ever calling us in |
1398 | * this circumstance: we're either drawing lots of straight |
1399 | * tiles at game start or after a move is complete, or we're |
1400 | * repeatedly drawing only the rotating tile. So no problem. |
1401 | */ |
1402 | |
1403 | /* |
1404 | * So. First blank the tile out completely: draw a big |
1405 | * rectangle in border colour, and a smaller rectangle in |
1406 | * background colour to fill it in. |
1407 | */ |
dafd6cf6 |
1408 | draw_rect(dr, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER, |
7bed19e1 |
1409 | COL_BORDER); |
dafd6cf6 |
1410 | draw_rect(dr, bx+TILE_BORDER, by+TILE_BORDER, |
7bed19e1 |
1411 | TILE_SIZE-TILE_BORDER, TILE_SIZE-TILE_BORDER, |
1412 | tile & FLASHING ? COL_FLASHING : COL_BACKGROUND); |
1413 | |
1414 | /* |
1415 | * Draw the wires. |
1416 | */ |
1417 | cx = cy = TILE_BORDER + (TILE_SIZE-TILE_BORDER) / 2.0F - 0.5F; |
1418 | col = (tile & ACTIVE ? COL_POWERED : COL_WIRE); |
1419 | for (dir = 1; dir < 0x10; dir <<= 1) { |
1420 | if (tile & dir) { |
1421 | ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir); |
1422 | ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir); |
dafd6cf6 |
1423 | draw_thick_line(dr, bx+(int)cx, by+(int)cy, |
7bed19e1 |
1424 | bx+(int)(cx+ex), by+(int)(cy+ey), |
1425 | COL_WIRE); |
1426 | } |
1427 | } |
1428 | for (dir = 1; dir < 0x10; dir <<= 1) { |
1429 | if (tile & dir) { |
1430 | ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir); |
1431 | ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir); |
dafd6cf6 |
1432 | draw_line(dr, bx+(int)cx, by+(int)cy, |
7bed19e1 |
1433 | bx+(int)(cx+ex), by+(int)(cy+ey), col); |
1434 | } |
1435 | } |
1436 | |
1437 | /* |
1438 | * Draw the box in the middle. We do this in blue if the tile |
1439 | * is an unpowered endpoint, in cyan if the tile is a powered |
1440 | * endpoint, in black if the tile is the centrepiece, and |
1441 | * otherwise not at all. |
1442 | */ |
1443 | col = -1; |
1444 | if (x == state->cx && y == state->cy) |
1445 | col = COL_WIRE; |
1446 | else if (COUNT(tile) == 1) { |
1447 | col = (tile & ACTIVE ? COL_POWERED : COL_ENDPOINT); |
1448 | } |
1449 | if (col >= 0) { |
1450 | int i, points[8]; |
1451 | |
1452 | points[0] = +1; points[1] = +1; |
1453 | points[2] = +1; points[3] = -1; |
1454 | points[4] = -1; points[5] = -1; |
1455 | points[6] = -1; points[7] = +1; |
1456 | |
1457 | for (i = 0; i < 8; i += 2) { |
1458 | ex = (TILE_SIZE * 0.24F) * points[i]; |
1459 | ey = (TILE_SIZE * 0.24F) * points[i+1]; |
1460 | points[i] = bx+(int)(cx+ex); |
1461 | points[i+1] = by+(int)(cy+ey); |
1462 | } |
1463 | |
dafd6cf6 |
1464 | draw_polygon(dr, points, 4, col, COL_WIRE); |
7bed19e1 |
1465 | } |
1466 | |
1467 | /* |
1468 | * Draw the points on the border if other tiles are connected |
1469 | * to us. |
1470 | */ |
1471 | for (dir = 1; dir < 0x10; dir <<= 1) { |
1472 | int dx, dy, px, py, lx, ly, vx, vy, ox, oy; |
1473 | |
1474 | dx = X(dir); |
1475 | dy = Y(dir); |
1476 | |
1477 | ox = x + dx; |
1478 | oy = y + dy; |
1479 | |
1480 | if (ox < 0 || ox >= state->width || oy < 0 || oy >= state->height) |
1481 | continue; |
1482 | |
1483 | if (!(tile(state, ox, oy) & F(dir))) |
1484 | continue; |
1485 | |
1486 | px = bx + (int)(dx>0 ? TILE_SIZE + TILE_BORDER - 1 : dx<0 ? 0 : cx); |
1487 | py = by + (int)(dy>0 ? TILE_SIZE + TILE_BORDER - 1 : dy<0 ? 0 : cy); |
1488 | lx = dx * (TILE_BORDER-1); |
1489 | ly = dy * (TILE_BORDER-1); |
1490 | vx = (dy ? 1 : 0); |
1491 | vy = (dx ? 1 : 0); |
1492 | |
1493 | if (xshift == 0.0 && yshift == 0.0 && (tile & dir)) { |
1494 | /* |
1495 | * If we are fully connected to the other tile, we must |
1496 | * draw right across the tile border. (We can use our |
1497 | * own ACTIVE state to determine what colour to do this |
1498 | * in: if we are fully connected to the other tile then |
1499 | * the two ACTIVE states will be the same.) |
1500 | */ |
dafd6cf6 |
1501 | draw_rect_coords(dr, px-vx, py-vy, px+lx+vx, py+ly+vy, COL_WIRE); |
1502 | draw_rect_coords(dr, px, py, px+lx, py+ly, |
7bed19e1 |
1503 | (tile & ACTIVE) ? COL_POWERED : COL_WIRE); |
1504 | } else { |
1505 | /* |
1506 | * The other tile extends into our border, but isn't |
1507 | * actually connected to us. Just draw a single black |
1508 | * dot. |
1509 | */ |
dafd6cf6 |
1510 | draw_rect_coords(dr, px, py, px, py, COL_WIRE); |
7bed19e1 |
1511 | } |
1512 | } |
1513 | |
dafd6cf6 |
1514 | draw_update(dr, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER); |
7bed19e1 |
1515 | } |
1516 | |
dafd6cf6 |
1517 | static void draw_tile_barriers(drawing *dr, game_drawstate *ds, |
1e3e152d |
1518 | game_state *state, int x, int y) |
7bed19e1 |
1519 | { |
1520 | int phase; |
1521 | int dir; |
1522 | int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x; |
1523 | int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y; |
1524 | /* |
1525 | * Draw barrier corners, and then barriers. |
1526 | */ |
1527 | for (phase = 0; phase < 2; phase++) { |
1528 | for (dir = 1; dir < 0x10; dir <<= 1) |
1529 | if (barrier(state, x, y) & (dir << 4)) |
dafd6cf6 |
1530 | draw_barrier_corner(dr, ds, x, y, dir << 4, phase); |
7bed19e1 |
1531 | for (dir = 1; dir < 0x10; dir <<= 1) |
1532 | if (barrier(state, x, y) & dir) |
dafd6cf6 |
1533 | draw_barrier(dr, ds, x, y, dir, phase); |
7bed19e1 |
1534 | } |
1535 | |
dafd6cf6 |
1536 | draw_update(dr, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER); |
7bed19e1 |
1537 | } |
1538 | |
dafd6cf6 |
1539 | static void draw_arrow(drawing *dr, game_drawstate *ds, |
3e17893b |
1540 | int x, int y, int xdx, int xdy, int cur) |
7bed19e1 |
1541 | { |
1542 | int coords[14]; |
1543 | int ydy = -xdx, ydx = xdy; |
1544 | |
1545 | x = x * TILE_SIZE + BORDER + WINDOW_OFFSET; |
1546 | y = y * TILE_SIZE + BORDER + WINDOW_OFFSET; |
1547 | |
1548 | #define POINT(n, xx, yy) ( \ |
1549 | coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \ |
1550 | coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy) |
1551 | |
1552 | POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */ |
1553 | POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */ |
1554 | POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */ |
1555 | POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */ |
1556 | POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */ |
1557 | POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */ |
1558 | POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */ |
1559 | |
3e17893b |
1560 | draw_polygon(dr, coords, 7, cur ? COL_POWERED : COL_LOWLIGHT, COL_TEXT); |
1561 | } |
1562 | |
1563 | static void draw_arrow_for_cursor(drawing *dr, game_drawstate *ds, |
1564 | int cur_x, int cur_y, int cur) |
1565 | { |
1566 | if (cur_x == -1 && cur_y == -1) |
1567 | return; /* 'no cursur here */ |
1568 | else if (cur_x == -1) /* LH column. */ |
1569 | draw_arrow(dr, ds, 0, cur_y+1, 0, -1, cur); |
1570 | else if (cur_x == ds->width) /* RH column */ |
1571 | draw_arrow(dr, ds, ds->width, cur_y, 0, +1, cur); |
1572 | else if (cur_y == -1) /* Top row */ |
1573 | draw_arrow(dr, ds, cur_x, 0, +1, 0, cur); |
1574 | else if (cur_y == ds->height) /* Bottom row */ |
1575 | draw_arrow(dr, ds, cur_x+1, ds->height, -1, 0, cur); |
1576 | else |
1577 | assert(!"Invalid cursor position"); |
1578 | |
1579 | draw_update(dr, |
1580 | cur_x * TILE_SIZE + BORDER + WINDOW_OFFSET, |
1581 | cur_y * TILE_SIZE + BORDER + WINDOW_OFFSET, |
1582 | TILE_SIZE, TILE_SIZE); |
7bed19e1 |
1583 | } |
1584 | |
dafd6cf6 |
1585 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
c822de4a |
1586 | game_state *state, int dir, game_ui *ui, float t, float ft) |
7bed19e1 |
1587 | { |
1588 | int x, y, tx, ty, frame; |
1589 | unsigned char *active; |
1590 | float xshift = 0.0; |
1591 | float yshift = 0.0; |
3e17893b |
1592 | int cur_x = -1, cur_y = -1; |
7bed19e1 |
1593 | |
1594 | /* |
1595 | * Clear the screen and draw the exterior barrier lines if this |
1596 | * is our first call. |
1597 | */ |
1598 | if (!ds->started) { |
1599 | int phase; |
1600 | |
1601 | ds->started = TRUE; |
1602 | |
dafd6cf6 |
1603 | draw_rect(dr, 0, 0, |
7bed19e1 |
1604 | BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * state->width + TILE_BORDER, |
1605 | BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * state->height + TILE_BORDER, |
1606 | COL_BACKGROUND); |
dafd6cf6 |
1607 | draw_update(dr, 0, 0, |
7bed19e1 |
1608 | BORDER * 2 + WINDOW_OFFSET*2 + TILE_SIZE*state->width + TILE_BORDER, |
1609 | BORDER * 2 + WINDOW_OFFSET*2 + TILE_SIZE*state->height + TILE_BORDER); |
1610 | |
1611 | for (phase = 0; phase < 2; phase++) { |
1612 | |
1613 | for (x = 0; x < ds->width; x++) { |
1614 | if (barrier(state, x, 0) & UL) |
dafd6cf6 |
1615 | draw_barrier_corner(dr, ds, x, -1, LD, phase); |
7bed19e1 |
1616 | if (barrier(state, x, 0) & RU) |
dafd6cf6 |
1617 | draw_barrier_corner(dr, ds, x, -1, DR, phase); |
7bed19e1 |
1618 | if (barrier(state, x, 0) & U) |
dafd6cf6 |
1619 | draw_barrier(dr, ds, x, -1, D, phase); |
7bed19e1 |
1620 | if (barrier(state, x, ds->height-1) & DR) |
dafd6cf6 |
1621 | draw_barrier_corner(dr, ds, x, ds->height, RU, phase); |
7bed19e1 |
1622 | if (barrier(state, x, ds->height-1) & LD) |
dafd6cf6 |
1623 | draw_barrier_corner(dr, ds, x, ds->height, UL, phase); |
7bed19e1 |
1624 | if (barrier(state, x, ds->height-1) & D) |
dafd6cf6 |
1625 | draw_barrier(dr, ds, x, ds->height, U, phase); |
7bed19e1 |
1626 | } |
1627 | |
1628 | for (y = 0; y < ds->height; y++) { |
1629 | if (barrier(state, 0, y) & UL) |
dafd6cf6 |
1630 | draw_barrier_corner(dr, ds, -1, y, RU, phase); |
7bed19e1 |
1631 | if (barrier(state, 0, y) & LD) |
dafd6cf6 |
1632 | draw_barrier_corner(dr, ds, -1, y, DR, phase); |
7bed19e1 |
1633 | if (barrier(state, 0, y) & L) |
dafd6cf6 |
1634 | draw_barrier(dr, ds, -1, y, R, phase); |
7bed19e1 |
1635 | if (barrier(state, ds->width-1, y) & RU) |
dafd6cf6 |
1636 | draw_barrier_corner(dr, ds, ds->width, y, UL, phase); |
7bed19e1 |
1637 | if (barrier(state, ds->width-1, y) & DR) |
dafd6cf6 |
1638 | draw_barrier_corner(dr, ds, ds->width, y, LD, phase); |
7bed19e1 |
1639 | if (barrier(state, ds->width-1, y) & R) |
dafd6cf6 |
1640 | draw_barrier(dr, ds, ds->width, y, L, phase); |
7bed19e1 |
1641 | } |
1642 | } |
1643 | |
1644 | /* |
1645 | * Arrows for making moves. |
1646 | */ |
1647 | for (x = 0; x < ds->width; x++) { |
1648 | if (x == state->cx) continue; |
3e17893b |
1649 | draw_arrow(dr, ds, x, 0, +1, 0, 0); |
1650 | draw_arrow(dr, ds, x+1, ds->height, -1, 0, 0); |
7bed19e1 |
1651 | } |
1652 | for (y = 0; y < ds->height; y++) { |
1653 | if (y == state->cy) continue; |
3e17893b |
1654 | draw_arrow(dr, ds, ds->width, y, 0, +1, 0); |
1655 | draw_arrow(dr, ds, 0, y+1, 0, -1, 0); |
7bed19e1 |
1656 | } |
1657 | } |
3e17893b |
1658 | if (ui->cur_visible) { |
1659 | cur_x = ui->cur_x; cur_y = ui->cur_y; |
1660 | } |
1661 | if (cur_x != ds->cur_x || cur_y != ds->cur_y) { |
1662 | /* Cursor has changed; redraw two (prev and curr) arrows. */ |
1663 | assert(cur_x != state->cx && cur_y != state->cy); |
1664 | |
1665 | draw_arrow_for_cursor(dr, ds, cur_x, cur_y, 1); |
1666 | draw_arrow_for_cursor(dr, ds, ds->cur_x, ds->cur_y, 0); |
1667 | ds->cur_x = cur_x; ds->cur_y = cur_y; |
1668 | } |
7bed19e1 |
1669 | |
1670 | /* Check if this is an undo. If so, we will need to run any animation |
1671 | * backwards. |
1672 | */ |
1673 | if (oldstate && oldstate->move_count > state->move_count) { |
1674 | game_state * tmpstate = state; |
1675 | state = oldstate; |
1676 | oldstate = tmpstate; |
1677 | t = ANIM_TIME - t; |
1678 | } |
1679 | |
1680 | tx = ty = -1; |
1681 | if (oldstate && (t < ANIM_TIME)) { |
1682 | /* |
1683 | * We're animating a slide, of row/column number |
1684 | * state->last_move_pos, in direction |
1685 | * state->last_move_dir |
1686 | */ |
3e17893b |
1687 | xshift = state->last_move_row == -1 ? 0.0F : |
7bed19e1 |
1688 | (1 - t / ANIM_TIME) * state->last_move_dir; |
3e17893b |
1689 | yshift = state->last_move_col == -1 ? 0.0F : |
7bed19e1 |
1690 | (1 - t / ANIM_TIME) * state->last_move_dir; |
1691 | } |
1692 | |
1693 | frame = -1; |
1694 | if (ft > 0) { |
1695 | /* |
1696 | * We're animating a completion flash. Find which frame |
1697 | * we're at. |
1698 | */ |
1699 | frame = (int)(ft / FLASH_FRAME); |
1700 | } |
1701 | |
1702 | /* |
1703 | * Draw any tile which differs from the way it was last drawn. |
1704 | */ |
1705 | if (xshift != 0.0 || yshift != 0.0) { |
1706 | active = compute_active(state, |
1707 | state->last_move_row, state->last_move_col); |
1708 | } else { |
1709 | active = compute_active(state, -1, -1); |
1710 | } |
1711 | |
dafd6cf6 |
1712 | clip(dr, |
7bed19e1 |
1713 | BORDER + WINDOW_OFFSET, BORDER + WINDOW_OFFSET, |
1714 | TILE_SIZE * state->width + TILE_BORDER, |
1715 | TILE_SIZE * state->height + TILE_BORDER); |
1716 | |
1717 | for (x = 0; x < ds->width; x++) |
1718 | for (y = 0; y < ds->height; y++) { |
1719 | unsigned char c = tile(state, x, y) | index(state, active, x, y); |
1720 | |
1721 | /* |
1722 | * In a completion flash, we adjust the FLASHING bit |
1723 | * depending on our distance from the centre point and |
1724 | * the frame number. |
1725 | */ |
1726 | if (frame >= 0) { |
1727 | int xdist, ydist, dist; |
1728 | xdist = (x < state->cx ? state->cx - x : x - state->cx); |
1729 | ydist = (y < state->cy ? state->cy - y : y - state->cy); |
1730 | dist = (xdist > ydist ? xdist : ydist); |
1731 | |
1732 | if (frame >= dist && frame < dist+4) { |
1733 | int flash = (frame - dist) & 1; |
1734 | flash = flash ? FLASHING : 0; |
1735 | c = (c &~ FLASHING) | flash; |
1736 | } |
1737 | } |
1738 | |
1739 | if (index(state, ds->visible, x, y) != c || |
1740 | index(state, ds->visible, x, y) == 0xFF || |
1741 | (x == state->last_move_col || y == state->last_move_row)) |
1742 | { |
3e17893b |
1743 | float xs = (y == state->last_move_row ? xshift : (float)0.0); |
1744 | float ys = (x == state->last_move_col ? yshift : (float)0.0); |
7bed19e1 |
1745 | |
dafd6cf6 |
1746 | draw_tile(dr, ds, state, x, y, c, xs, ys); |
7bed19e1 |
1747 | if (xs < 0 && x == 0) |
dafd6cf6 |
1748 | draw_tile(dr, ds, state, state->width, y, c, xs, ys); |
7bed19e1 |
1749 | else if (xs > 0 && x == state->width - 1) |
dafd6cf6 |
1750 | draw_tile(dr, ds, state, -1, y, c, xs, ys); |
7bed19e1 |
1751 | else if (ys < 0 && y == 0) |
dafd6cf6 |
1752 | draw_tile(dr, ds, state, x, state->height, c, xs, ys); |
7bed19e1 |
1753 | else if (ys > 0 && y == state->height - 1) |
dafd6cf6 |
1754 | draw_tile(dr, ds, state, x, -1, c, xs, ys); |
7bed19e1 |
1755 | |
1756 | if (x == state->last_move_col || y == state->last_move_row) |
1757 | index(state, ds->visible, x, y) = 0xFF; |
1758 | else |
1759 | index(state, ds->visible, x, y) = c; |
1760 | } |
1761 | } |
1762 | |
1763 | for (x = 0; x < ds->width; x++) |
1764 | for (y = 0; y < ds->height; y++) |
dafd6cf6 |
1765 | draw_tile_barriers(dr, ds, state, x, y); |
7bed19e1 |
1766 | |
dafd6cf6 |
1767 | unclip(dr); |
7bed19e1 |
1768 | |
1769 | /* |
1770 | * Update the status bar. |
1771 | */ |
1772 | { |
1773 | char statusbuf[256]; |
1774 | int i, n, a; |
1775 | |
1776 | n = state->width * state->height; |
1777 | for (i = a = 0; i < n; i++) |
1778 | if (active[i]) |
1779 | a++; |
1780 | |
2ac6d24e |
1781 | if (state->used_solve) |
1782 | sprintf(statusbuf, "Moves since auto-solve: %d", |
1783 | state->move_count - state->completed); |
1784 | else |
1785 | sprintf(statusbuf, "%sMoves: %d", |
1786 | (state->completed ? "COMPLETED! " : ""), |
1787 | (state->completed ? state->completed : state->move_count)); |
1788 | |
aa27d493 |
1789 | if (state->movetarget) |
1790 | sprintf(statusbuf + strlen(statusbuf), " (target %d)", |
1791 | state->movetarget); |
1792 | |
2ac6d24e |
1793 | sprintf(statusbuf + strlen(statusbuf), " Active: %d/%d", a, n); |
7bed19e1 |
1794 | |
dafd6cf6 |
1795 | status_bar(dr, statusbuf); |
7bed19e1 |
1796 | } |
1797 | |
1798 | sfree(active); |
1799 | } |
1800 | |
be8d5aa1 |
1801 | static float game_anim_length(game_state *oldstate, |
e3f21163 |
1802 | game_state *newstate, int dir, game_ui *ui) |
7bed19e1 |
1803 | { |
1804 | return ANIM_TIME; |
1805 | } |
1806 | |
be8d5aa1 |
1807 | static float game_flash_length(game_state *oldstate, |
e3f21163 |
1808 | game_state *newstate, int dir, game_ui *ui) |
7bed19e1 |
1809 | { |
1810 | /* |
1811 | * If the game has just been completed, we display a completion |
1812 | * flash. |
1813 | */ |
2ac6d24e |
1814 | if (!oldstate->completed && newstate->completed && |
1815 | !oldstate->used_solve && !newstate->used_solve) { |
7bed19e1 |
1816 | int size; |
1817 | size = 0; |
1818 | if (size < newstate->cx+1) |
1819 | size = newstate->cx+1; |
1820 | if (size < newstate->cy+1) |
1821 | size = newstate->cy+1; |
1822 | if (size < newstate->width - newstate->cx) |
1823 | size = newstate->width - newstate->cx; |
1824 | if (size < newstate->height - newstate->cy) |
1825 | size = newstate->height - newstate->cy; |
1826 | return FLASH_FRAME * (size+4); |
1827 | } |
1828 | |
1829 | return 0.0F; |
1830 | } |
1831 | |
4d08de49 |
1832 | static int game_timing_state(game_state *state, game_ui *ui) |
48dcdd62 |
1833 | { |
1834 | return FALSE; |
1835 | } |
1836 | |
ca58cbd1 |
1837 | static void game_print_size(game_params *params, float *x, float *y) |
1838 | { |
1839 | } |
1840 | |
1841 | static void game_print(drawing *dr, game_state *state, int tilesize) |
1842 | { |
1843 | } |
1844 | |
be8d5aa1 |
1845 | #ifdef COMBINED |
1846 | #define thegame netslide |
1847 | #endif |
1848 | |
1849 | const struct game thegame = { |
750037d7 |
1850 | "Netslide", "games.netslide", "netslide", |
be8d5aa1 |
1851 | default_params, |
1852 | game_fetch_preset, |
1853 | decode_params, |
1854 | encode_params, |
1855 | free_params, |
1856 | dup_params, |
1d228b10 |
1857 | TRUE, game_configure, custom_params, |
be8d5aa1 |
1858 | validate_params, |
1185e3c5 |
1859 | new_game_desc, |
1185e3c5 |
1860 | validate_desc, |
be8d5aa1 |
1861 | new_game, |
1862 | dup_game, |
1863 | free_game, |
2ac6d24e |
1864 | TRUE, solve_game, |
fa3abef5 |
1865 | FALSE, game_can_format_as_text_now, game_text_format, |
be8d5aa1 |
1866 | new_ui, |
1867 | free_ui, |
ae8290c6 |
1868 | encode_ui, |
1869 | decode_ui, |
07dfb697 |
1870 | game_changed_state, |
df11cd4e |
1871 | interpret_move, |
1872 | execute_move, |
1f3ee4ee |
1873 | PREFERRED_TILE_SIZE, game_compute_size, game_set_size, |
be8d5aa1 |
1874 | game_colours, |
1875 | game_new_drawstate, |
1876 | game_free_drawstate, |
1877 | game_redraw, |
1878 | game_anim_length, |
1879 | game_flash_length, |
dafd6cf6 |
1880 | FALSE, FALSE, game_print_size, game_print, |
ac9f41c4 |
1881 | TRUE, /* wants_statusbar */ |
48dcdd62 |
1882 | FALSE, game_timing_state, |
2705d374 |
1883 | 0, /* flags */ |
be8d5aa1 |
1884 | }; |
3e17893b |
1885 | |
1886 | /* vim: set shiftwidth=4 tabstop=8: */ |