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 PI 3.141592653589793238462643383279502884197169399 |
17 | |
18 | #define MATMUL(xr,yr,m,x,y) do { \ |
19 | float rx, ry, xx = (x), yy = (y), *mat = (m); \ |
20 | rx = mat[0] * xx + mat[2] * yy; \ |
21 | ry = mat[1] * xx + mat[3] * yy; \ |
22 | (xr) = rx; (yr) = ry; \ |
23 | } while (0) |
24 | |
25 | /* Direction and other bitfields */ |
26 | #define R 0x01 |
27 | #define U 0x02 |
28 | #define L 0x04 |
29 | #define D 0x08 |
30 | #define FLASHING 0x10 |
31 | #define ACTIVE 0x20 |
32 | /* Corner flags go in the barriers array */ |
33 | #define RU 0x10 |
34 | #define UL 0x20 |
35 | #define LD 0x40 |
36 | #define DR 0x80 |
37 | |
38 | /* Get tile at given coordinate */ |
39 | #define T(state, x, y) ( (y) * (state)->width + (x) ) |
40 | |
41 | /* Rotations: Anticlockwise, Clockwise, Flip, general rotate */ |
42 | #define A(x) ( (((x) & 0x07) << 1) | (((x) & 0x08) >> 3) ) |
43 | #define C(x) ( (((x) & 0x0E) >> 1) | (((x) & 0x01) << 3) ) |
44 | #define F(x) ( (((x) & 0x0C) >> 2) | (((x) & 0x03) << 2) ) |
45 | #define ROT(x, n) ( ((n)&3) == 0 ? (x) : \ |
46 | ((n)&3) == 1 ? A(x) : \ |
47 | ((n)&3) == 2 ? F(x) : C(x) ) |
48 | |
49 | /* X and Y displacements */ |
50 | #define X(x) ( (x) == R ? +1 : (x) == L ? -1 : 0 ) |
51 | #define Y(x) ( (x) == D ? +1 : (x) == U ? -1 : 0 ) |
52 | |
53 | /* Bit count */ |
54 | #define COUNT(x) ( (((x) & 0x08) >> 3) + (((x) & 0x04) >> 2) + \ |
55 | (((x) & 0x02) >> 1) + ((x) & 0x01) ) |
56 | |
57 | #define TILE_SIZE 48 |
58 | #define BORDER TILE_SIZE |
59 | #define TILE_BORDER 1 |
60 | #define WINDOW_OFFSET 0 |
61 | |
62 | #define ANIM_TIME 0.13F |
63 | #define FLASH_FRAME 0.07F |
64 | |
65 | enum { |
66 | COL_BACKGROUND, |
67 | COL_FLASHING, |
68 | COL_BORDER, |
69 | COL_WIRE, |
70 | COL_ENDPOINT, |
71 | COL_POWERED, |
72 | COL_BARRIER, |
73 | COL_LOWLIGHT, |
74 | COL_TEXT, |
75 | NCOLOURS |
76 | }; |
77 | |
78 | struct game_params { |
79 | int width; |
80 | int height; |
81 | int wrapping; |
82 | float barrier_probability; |
83 | }; |
84 | |
85 | struct game_state { |
86 | int width, height, cx, cy, wrapping, completed; |
87 | int move_count; |
88 | |
89 | /* position (row or col number, starting at 0) of last move. */ |
90 | int last_move_row, last_move_col; |
91 | |
92 | /* direction of last move: +1 or -1 */ |
93 | int last_move_dir; |
94 | |
95 | unsigned char *tiles; |
96 | unsigned char *barriers; |
97 | }; |
98 | |
99 | #define OFFSET(x2,y2,x1,y1,dir,state) \ |
100 | ( (x2) = ((x1) + (state)->width + X((dir))) % (state)->width, \ |
101 | (y2) = ((y1) + (state)->height + Y((dir))) % (state)->height) |
102 | |
103 | #define index(state, a, x, y) ( a[(y) * (state)->width + (x)] ) |
104 | #define tile(state, x, y) index(state, (state)->tiles, x, y) |
105 | #define barrier(state, x, y) index(state, (state)->barriers, x, y) |
106 | |
107 | struct xyd { |
108 | int x, y, direction; |
109 | }; |
110 | |
111 | static int xyd_cmp(void *av, void *bv) { |
112 | struct xyd *a = (struct xyd *)av; |
113 | struct xyd *b = (struct xyd *)bv; |
114 | if (a->x < b->x) |
115 | return -1; |
116 | if (a->x > b->x) |
117 | return +1; |
118 | if (a->y < b->y) |
119 | return -1; |
120 | if (a->y > b->y) |
121 | return +1; |
122 | if (a->direction < b->direction) |
123 | return -1; |
124 | if (a->direction > b->direction) |
125 | return +1; |
126 | return 0; |
127 | }; |
128 | |
129 | static struct xyd *new_xyd(int x, int y, int direction) |
130 | { |
131 | struct xyd *xyd = snew(struct xyd); |
132 | xyd->x = x; |
133 | xyd->y = y; |
134 | xyd->direction = direction; |
135 | return xyd; |
136 | } |
137 | |
be8d5aa1 |
138 | static void slide_col(game_state *state, int dir, int col); |
139 | static void slide_row(game_state *state, int dir, int row); |
7bed19e1 |
140 | |
141 | /* ---------------------------------------------------------------------- |
142 | * Manage game parameters. |
143 | */ |
be8d5aa1 |
144 | static game_params *default_params(void) |
7bed19e1 |
145 | { |
146 | game_params *ret = snew(game_params); |
147 | |
148 | ret->width = 3; |
149 | ret->height = 3; |
150 | ret->wrapping = FALSE; |
151 | ret->barrier_probability = 1.0; |
152 | |
153 | return ret; |
154 | } |
155 | |
be8d5aa1 |
156 | static int game_fetch_preset(int i, char **name, game_params **params) |
7bed19e1 |
157 | { |
158 | game_params *ret; |
159 | char str[80]; |
160 | static const struct { int x, y, wrap, bprob; const char* desc; } values[] = { |
161 | {3, 3, FALSE, 1.0, " easy"}, |
162 | {3, 3, FALSE, 0.0, " medium"}, |
163 | {3, 3, TRUE, 0.0, " hard"}, |
164 | {4, 4, FALSE, 1.0, " easy"}, |
165 | {4, 4, FALSE, 0.0, " medium"}, |
166 | {4, 4, TRUE, 0.0, " hard"}, |
167 | {5, 5, FALSE, 1.0, " easy"}, |
168 | {5, 5, FALSE, 0.0, " medium"}, |
169 | {5, 5, TRUE, 0.0, " hard"}, |
170 | }; |
171 | |
172 | if (i < 0 || i >= lenof(values)) |
173 | return FALSE; |
174 | |
175 | ret = snew(game_params); |
176 | ret->width = values[i].x; |
177 | ret->height = values[i].y; |
178 | ret->wrapping = values[i].wrap; |
179 | ret->barrier_probability = values[i].bprob; |
180 | |
181 | sprintf(str, "%dx%d%s", ret->width, ret->height, |
182 | values[i].desc); |
183 | |
184 | *name = dupstr(str); |
185 | *params = ret; |
186 | return TRUE; |
187 | } |
188 | |
be8d5aa1 |
189 | static void free_params(game_params *params) |
7bed19e1 |
190 | { |
191 | sfree(params); |
192 | } |
193 | |
be8d5aa1 |
194 | static game_params *dup_params(game_params *params) |
7bed19e1 |
195 | { |
196 | game_params *ret = snew(game_params); |
197 | *ret = *params; /* structure copy */ |
198 | return ret; |
199 | } |
200 | |
be8d5aa1 |
201 | static game_params *decode_params(char const *string) |
7bed19e1 |
202 | { |
203 | game_params *ret = default_params(); |
204 | char const *p = string; |
205 | |
206 | ret->wrapping = FALSE; |
207 | ret->barrier_probability = 0.0; |
208 | |
209 | ret->width = atoi(p); |
210 | while (*p && isdigit(*p)) p++; |
211 | if (*p == 'x') { |
212 | p++; |
213 | ret->height = atoi(p); |
214 | while (*p && isdigit(*p)) p++; |
215 | if ( (ret->wrapping = (*p == 'w')) != 0 ) |
216 | p++; |
217 | if (*p == 'b') |
218 | ret->barrier_probability = atof(p+1); |
219 | } else { |
220 | ret->height = ret->width; |
221 | } |
222 | |
223 | return ret; |
224 | } |
225 | |
be8d5aa1 |
226 | static char *encode_params(game_params *params) |
7bed19e1 |
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'; |
234 | if (params->barrier_probability) |
235 | len += sprintf(ret+len, "b%g", params->barrier_probability); |
236 | assert(len < lenof(ret)); |
237 | ret[len] = '\0'; |
238 | |
239 | return dupstr(ret); |
240 | } |
241 | |
be8d5aa1 |
242 | static config_item *game_configure(game_params *params) |
7bed19e1 |
243 | { |
244 | config_item *ret; |
245 | char buf[80]; |
246 | |
247 | ret = snewn(5, config_item); |
248 | |
249 | ret[0].name = "Width"; |
250 | ret[0].type = C_STRING; |
251 | sprintf(buf, "%d", params->width); |
252 | ret[0].sval = dupstr(buf); |
253 | ret[0].ival = 0; |
254 | |
255 | ret[1].name = "Height"; |
256 | ret[1].type = C_STRING; |
257 | sprintf(buf, "%d", params->height); |
258 | ret[1].sval = dupstr(buf); |
259 | ret[1].ival = 0; |
260 | |
261 | ret[2].name = "Walls wrap around"; |
262 | ret[2].type = C_BOOLEAN; |
263 | ret[2].sval = NULL; |
264 | ret[2].ival = params->wrapping; |
265 | |
266 | ret[3].name = "Barrier probability"; |
267 | ret[3].type = C_STRING; |
268 | sprintf(buf, "%g", params->barrier_probability); |
269 | ret[3].sval = dupstr(buf); |
270 | ret[3].ival = 0; |
271 | |
272 | ret[4].name = NULL; |
273 | ret[4].type = C_END; |
274 | ret[4].sval = NULL; |
275 | ret[4].ival = 0; |
276 | |
277 | return ret; |
278 | } |
279 | |
be8d5aa1 |
280 | static game_params *custom_params(config_item *cfg) |
7bed19e1 |
281 | { |
282 | game_params *ret = snew(game_params); |
283 | |
284 | ret->width = atoi(cfg[0].sval); |
285 | ret->height = atoi(cfg[1].sval); |
286 | ret->wrapping = cfg[2].ival; |
287 | ret->barrier_probability = (float)atof(cfg[3].sval); |
288 | |
289 | return ret; |
290 | } |
291 | |
be8d5aa1 |
292 | static char *validate_params(game_params *params) |
7bed19e1 |
293 | { |
294 | if (params->width <= 1 && params->height <= 1) |
295 | return "Width and height must both be greater than one"; |
296 | if (params->width <= 1) |
297 | return "Width must be greater than one"; |
298 | if (params->height <= 1) |
299 | return "Height must be greater than one"; |
300 | if (params->barrier_probability < 0) |
301 | return "Barrier probability may not be negative"; |
302 | if (params->barrier_probability > 1) |
303 | return "Barrier probability may not be greater than 1"; |
304 | return NULL; |
305 | } |
306 | |
307 | /* ---------------------------------------------------------------------- |
308 | * Randomly select a new game seed. |
309 | */ |
310 | |
6f2d8d7c |
311 | static char *new_game_seed(game_params *params, random_state *rs, |
312 | game_aux_info **aux) |
7bed19e1 |
313 | { |
314 | /* |
315 | * The full description of a Net game is far too large to |
316 | * encode directly in the seed, so by default we'll have to go |
317 | * for the simple approach of providing a random-number seed. |
318 | * |
319 | * (This does not restrict me from _later on_ inventing a seed |
320 | * string syntax which can never be generated by this code - |
321 | * for example, strings beginning with a letter - allowing me |
322 | * to type in a precise game, and have new_game detect it and |
323 | * understand it and do something completely different.) |
324 | */ |
325 | char buf[40]; |
326 | sprintf(buf, "%lu", random_bits(rs, 32)); |
327 | return dupstr(buf); |
328 | } |
329 | |
6f2d8d7c |
330 | void game_free_aux_info(game_aux_info *aux) |
331 | { |
332 | assert(!"Shouldn't happen"); |
333 | } |
334 | |
be8d5aa1 |
335 | static char *validate_seed(game_params *params, char *seed) |
7bed19e1 |
336 | { |
337 | /* |
338 | * Since any string at all will suffice to seed the RNG, there |
339 | * is no validation required. |
340 | */ |
341 | return NULL; |
342 | } |
343 | |
344 | /* ---------------------------------------------------------------------- |
345 | * Construct an initial game state, given a seed and parameters. |
346 | */ |
347 | |
be8d5aa1 |
348 | static game_state *new_game(game_params *params, char *seed) |
7bed19e1 |
349 | { |
350 | random_state *rs; |
351 | game_state *state; |
352 | tree234 *possibilities, *barriers; |
353 | int w, h, x, y, nbarriers; |
354 | |
355 | assert(params->width > 0 && params->height > 0); |
356 | assert(params->width > 1 || params->height > 1); |
357 | |
358 | /* |
359 | * Create a blank game state. |
360 | */ |
361 | state = snew(game_state); |
362 | w = state->width = params->width; |
363 | h = state->height = params->height; |
364 | state->cx = state->width / 2; |
365 | state->cy = state->height / 2; |
366 | state->wrapping = params->wrapping; |
367 | state->completed = 0; |
368 | state->move_count = 0; |
369 | state->last_move_row = -1; |
370 | state->last_move_col = -1; |
371 | state->last_move_dir = 0; |
372 | state->tiles = snewn(state->width * state->height, unsigned char); |
373 | memset(state->tiles, 0, state->width * state->height); |
374 | state->barriers = snewn(state->width * state->height, unsigned char); |
375 | memset(state->barriers, 0, state->width * state->height); |
376 | |
377 | /* |
378 | * Set up border barriers if this is a non-wrapping game. |
379 | */ |
380 | if (!state->wrapping) { |
381 | for (x = 0; x < state->width; x++) { |
382 | barrier(state, x, 0) |= U; |
383 | barrier(state, x, state->height-1) |= D; |
384 | } |
385 | for (y = 0; y < state->height; y++) { |
386 | barrier(state, 0, y) |= L; |
387 | barrier(state, state->width-1, y) |= R; |
388 | } |
389 | } |
390 | |
391 | /* |
392 | * Seed the internal random number generator. |
393 | */ |
394 | rs = random_init(seed, strlen(seed)); |
395 | |
396 | /* |
397 | * Construct the unshuffled grid. |
398 | * |
399 | * To do this, we simply start at the centre point, repeatedly |
400 | * choose a random possibility out of the available ways to |
401 | * extend a used square into an unused one, and do it. After |
402 | * extending the third line out of a square, we remove the |
403 | * fourth from the possibilities list to avoid any full-cross |
404 | * squares (which would make the game too easy because they |
405 | * only have one orientation). |
406 | * |
407 | * The slightly worrying thing is the avoidance of full-cross |
408 | * squares. Can this cause our unsophisticated construction |
409 | * algorithm to paint itself into a corner, by getting into a |
410 | * situation where there are some unreached squares and the |
411 | * only way to reach any of them is to extend a T-piece into a |
412 | * full cross? |
413 | * |
414 | * Answer: no it can't, and here's a proof. |
415 | * |
416 | * Any contiguous group of such unreachable squares must be |
417 | * surrounded on _all_ sides by T-pieces pointing away from the |
418 | * group. (If not, then there is a square which can be extended |
419 | * into one of the `unreachable' ones, and so it wasn't |
420 | * unreachable after all.) In particular, this implies that |
421 | * each contiguous group of unreachable squares must be |
422 | * rectangular in shape (any deviation from that yields a |
423 | * non-T-piece next to an `unreachable' square). |
424 | * |
425 | * So we have a rectangle of unreachable squares, with T-pieces |
426 | * forming a solid border around the rectangle. The corners of |
427 | * that border must be connected (since every tile connects all |
428 | * the lines arriving in it), and therefore the border must |
429 | * form a closed loop around the rectangle. |
430 | * |
431 | * But this can't have happened in the first place, since we |
432 | * _know_ we've avoided creating closed loops! Hence, no such |
433 | * situation can ever arise, and the naive grid construction |
434 | * algorithm will guaranteeably result in a complete grid |
435 | * containing no unreached squares, no full crosses _and_ no |
436 | * closed loops. [] |
437 | */ |
438 | possibilities = newtree234(xyd_cmp); |
439 | |
440 | if (state->cx+1 < state->width) |
441 | add234(possibilities, new_xyd(state->cx, state->cy, R)); |
442 | if (state->cy-1 >= 0) |
443 | add234(possibilities, new_xyd(state->cx, state->cy, U)); |
444 | if (state->cx-1 >= 0) |
445 | add234(possibilities, new_xyd(state->cx, state->cy, L)); |
446 | if (state->cy+1 < state->height) |
447 | add234(possibilities, new_xyd(state->cx, state->cy, D)); |
448 | |
449 | while (count234(possibilities) > 0) { |
450 | int i; |
451 | struct xyd *xyd; |
452 | int x1, y1, d1, x2, y2, d2, d; |
453 | |
454 | /* |
455 | * Extract a randomly chosen possibility from the list. |
456 | */ |
457 | i = random_upto(rs, count234(possibilities)); |
458 | xyd = delpos234(possibilities, i); |
459 | x1 = xyd->x; |
460 | y1 = xyd->y; |
461 | d1 = xyd->direction; |
462 | sfree(xyd); |
463 | |
464 | OFFSET(x2, y2, x1, y1, d1, state); |
465 | d2 = F(d1); |
466 | #ifdef DEBUG |
467 | printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n", |
468 | x1, y1, "0RU3L567D9abcdef"[d1], x2, y2, "0RU3L567D9abcdef"[d2]); |
469 | #endif |
470 | |
471 | /* |
472 | * Make the connection. (We should be moving to an as yet |
473 | * unused tile.) |
474 | */ |
475 | tile(state, x1, y1) |= d1; |
476 | assert(tile(state, x2, y2) == 0); |
477 | tile(state, x2, y2) |= d2; |
478 | |
479 | /* |
480 | * If we have created a T-piece, remove its last |
481 | * possibility. |
482 | */ |
483 | if (COUNT(tile(state, x1, y1)) == 3) { |
484 | struct xyd xyd1, *xydp; |
485 | |
486 | xyd1.x = x1; |
487 | xyd1.y = y1; |
488 | xyd1.direction = 0x0F ^ tile(state, x1, y1); |
489 | |
490 | xydp = find234(possibilities, &xyd1, NULL); |
491 | |
492 | if (xydp) { |
493 | #ifdef DEBUG |
494 | printf("T-piece; removing (%d,%d,%c)\n", |
495 | xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]); |
496 | #endif |
497 | del234(possibilities, xydp); |
498 | sfree(xydp); |
499 | } |
500 | } |
501 | |
502 | /* |
503 | * Remove all other possibilities that were pointing at the |
504 | * tile we've just moved into. |
505 | */ |
506 | for (d = 1; d < 0x10; d <<= 1) { |
507 | int x3, y3, d3; |
508 | struct xyd xyd1, *xydp; |
509 | |
510 | OFFSET(x3, y3, x2, y2, d, state); |
511 | d3 = F(d); |
512 | |
513 | xyd1.x = x3; |
514 | xyd1.y = y3; |
515 | xyd1.direction = d3; |
516 | |
517 | xydp = find234(possibilities, &xyd1, NULL); |
518 | |
519 | if (xydp) { |
520 | #ifdef DEBUG |
521 | printf("Loop avoidance; removing (%d,%d,%c)\n", |
522 | xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]); |
523 | #endif |
524 | del234(possibilities, xydp); |
525 | sfree(xydp); |
526 | } |
527 | } |
528 | |
529 | /* |
530 | * Add new possibilities to the list for moving _out_ of |
531 | * the tile we have just moved into. |
532 | */ |
533 | for (d = 1; d < 0x10; d <<= 1) { |
534 | int x3, y3; |
535 | |
536 | if (d == d2) |
537 | continue; /* we've got this one already */ |
538 | |
539 | if (!state->wrapping) { |
540 | if (d == U && y2 == 0) |
541 | continue; |
542 | if (d == D && y2 == state->height-1) |
543 | continue; |
544 | if (d == L && x2 == 0) |
545 | continue; |
546 | if (d == R && x2 == state->width-1) |
547 | continue; |
548 | } |
549 | |
550 | OFFSET(x3, y3, x2, y2, d, state); |
551 | |
552 | if (tile(state, x3, y3)) |
553 | continue; /* this would create a loop */ |
554 | |
555 | #ifdef DEBUG |
556 | printf("New frontier; adding (%d,%d,%c)\n", |
557 | x2, y2, "0RU3L567D9abcdef"[d]); |
558 | #endif |
559 | add234(possibilities, new_xyd(x2, y2, d)); |
560 | } |
561 | } |
562 | /* Having done that, we should have no possibilities remaining. */ |
563 | assert(count234(possibilities) == 0); |
564 | freetree234(possibilities); |
565 | |
566 | /* |
567 | * Now compute a list of the possible barrier locations. |
568 | */ |
569 | barriers = newtree234(xyd_cmp); |
570 | for (y = 0; y < state->height; y++) { |
571 | for (x = 0; x < state->width; x++) { |
572 | |
573 | if (!(tile(state, x, y) & R) && |
574 | (state->wrapping || x < state->width-1)) |
575 | add234(barriers, new_xyd(x, y, R)); |
576 | if (!(tile(state, x, y) & D) && |
577 | (state->wrapping || y < state->height-1)) |
578 | add234(barriers, new_xyd(x, y, D)); |
579 | } |
580 | } |
581 | |
582 | /* |
583 | * Now shuffle the grid. |
584 | * FIXME - this simply does a set of random moves to shuffle the pieces. |
585 | * A better way would be to number all the pieces, generate a placement |
586 | * for all the numbers as for "sixteen", observing parity constraints if |
587 | * neccessary, and then place the pieces according to their numbering. |
588 | * BUT - I'm not sure if this will work, since we disallow movement of |
589 | * the middle row and column. |
590 | */ |
591 | { |
592 | int i; |
593 | int cols = state->width - 1; |
594 | int rows = state->height - 1; |
595 | for (i = 0; i < cols * rows * 2; i++) { |
596 | /* Choose a direction: 0,1,2,3 = up, right, down, left. */ |
597 | int dir = random_upto(rs, 4); |
598 | if (dir % 2 == 0) { |
599 | int col = random_upto(rs, cols); |
600 | if (col >= state->cx) col += 1; |
601 | slide_col(state, 1 - dir, col); |
602 | } else { |
603 | int row = random_upto(rs, rows); |
604 | if (row >= state->cy) row += 1; |
605 | slide_row(state, 2 - dir, row); |
606 | } |
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 |
613 | * params while keeping the game seed the same will give the |
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 | */ |
624 | nbarriers = (int)(params->barrier_probability * count234(barriers)); |
625 | assert(nbarriers >= 0 && nbarriers <= count234(barriers)); |
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 | */ |
635 | i = random_upto(rs, count234(barriers)); |
636 | xyd = delpos234(barriers, i); |
637 | |
638 | assert(xyd != NULL); |
639 | |
640 | x1 = xyd->x; |
641 | y1 = xyd->y; |
642 | d1 = xyd->direction; |
643 | sfree(xyd); |
644 | |
645 | OFFSET(x2, y2, x1, y1, d1, state); |
646 | d2 = F(d1); |
647 | |
648 | barrier(state, x1, y1) |= d1; |
649 | barrier(state, x2, y2) |= d2; |
650 | |
651 | nbarriers--; |
652 | } |
653 | |
654 | /* |
655 | * Clean up the rest of the barrier list. |
656 | */ |
657 | { |
658 | struct xyd *xyd; |
659 | |
660 | while ( (xyd = delpos234(barriers, 0)) != NULL) |
661 | sfree(xyd); |
662 | |
663 | freetree234(barriers); |
664 | } |
665 | |
666 | /* |
667 | * Set up the barrier corner flags, for drawing barriers |
668 | * prettily when they meet. |
669 | */ |
670 | for (y = 0; y < state->height; y++) { |
671 | for (x = 0; x < state->width; x++) { |
672 | int dir; |
673 | |
674 | for (dir = 1; dir < 0x10; dir <<= 1) { |
675 | int dir2 = A(dir); |
676 | int x1, y1, x2, y2, x3, y3; |
677 | int corner = FALSE; |
678 | |
679 | if (!(barrier(state, x, y) & dir)) |
680 | continue; |
681 | |
682 | if (barrier(state, x, y) & dir2) |
683 | corner = TRUE; |
684 | |
685 | x1 = x + X(dir), y1 = y + Y(dir); |
686 | if (x1 >= 0 && x1 < state->width && |
687 | y1 >= 0 && y1 < state->height && |
688 | (barrier(state, x1, y1) & dir2)) |
689 | corner = TRUE; |
690 | |
691 | x2 = x + X(dir2), y2 = y + Y(dir2); |
692 | if (x2 >= 0 && x2 < state->width && |
693 | y2 >= 0 && y2 < state->height && |
694 | (barrier(state, x2, y2) & dir)) |
695 | corner = TRUE; |
696 | |
697 | if (corner) { |
698 | barrier(state, x, y) |= (dir << 4); |
699 | if (x1 >= 0 && x1 < state->width && |
700 | y1 >= 0 && y1 < state->height) |
701 | barrier(state, x1, y1) |= (A(dir) << 4); |
702 | if (x2 >= 0 && x2 < state->width && |
703 | y2 >= 0 && y2 < state->height) |
704 | barrier(state, x2, y2) |= (C(dir) << 4); |
705 | x3 = x + X(dir) + X(dir2), y3 = y + Y(dir) + Y(dir2); |
706 | if (x3 >= 0 && x3 < state->width && |
707 | y3 >= 0 && y3 < state->height) |
708 | barrier(state, x3, y3) |= (F(dir) << 4); |
709 | } |
710 | } |
711 | } |
712 | } |
713 | |
714 | random_free(rs); |
715 | |
716 | return state; |
717 | } |
718 | |
be8d5aa1 |
719 | static game_state *dup_game(game_state *state) |
7bed19e1 |
720 | { |
721 | game_state *ret; |
722 | |
723 | ret = snew(game_state); |
724 | ret->width = state->width; |
725 | ret->height = state->height; |
726 | ret->cx = state->cx; |
727 | ret->cy = state->cy; |
728 | ret->wrapping = state->wrapping; |
729 | ret->completed = state->completed; |
730 | ret->move_count = state->move_count; |
731 | ret->last_move_row = state->last_move_row; |
732 | ret->last_move_col = state->last_move_col; |
733 | ret->last_move_dir = state->last_move_dir; |
734 | ret->tiles = snewn(state->width * state->height, unsigned char); |
735 | memcpy(ret->tiles, state->tiles, state->width * state->height); |
736 | ret->barriers = snewn(state->width * state->height, unsigned char); |
737 | memcpy(ret->barriers, state->barriers, state->width * state->height); |
738 | |
739 | return ret; |
740 | } |
741 | |
be8d5aa1 |
742 | static void free_game(game_state *state) |
7bed19e1 |
743 | { |
744 | sfree(state->tiles); |
745 | sfree(state->barriers); |
746 | sfree(state); |
747 | } |
748 | |
9b4b03d3 |
749 | static char *game_text_format(game_state *state) |
750 | { |
751 | return NULL; |
752 | } |
753 | |
7bed19e1 |
754 | /* ---------------------------------------------------------------------- |
755 | * Utility routine. |
756 | */ |
757 | |
758 | /* |
759 | * Compute which squares are reachable from the centre square, as a |
760 | * quick visual aid to determining how close the game is to |
761 | * completion. This is also a simple way to tell if the game _is_ |
762 | * completed - just call this function and see whether every square |
763 | * is marked active. |
764 | * |
765 | * squares in the moving_row and moving_col are always inactive - this |
766 | * is so that "current" doesn't appear to jump across moving lines. |
767 | */ |
768 | static unsigned char *compute_active(game_state *state, |
769 | int moving_row, int moving_col) |
770 | { |
771 | unsigned char *active; |
772 | tree234 *todo; |
773 | struct xyd *xyd; |
774 | |
775 | active = snewn(state->width * state->height, unsigned char); |
776 | memset(active, 0, state->width * state->height); |
777 | |
778 | /* |
779 | * We only store (x,y) pairs in todo, but it's easier to reuse |
780 | * xyd_cmp and just store direction 0 every time. |
781 | */ |
782 | todo = newtree234(xyd_cmp); |
783 | index(state, active, state->cx, state->cy) = ACTIVE; |
784 | add234(todo, new_xyd(state->cx, state->cy, 0)); |
785 | |
786 | while ( (xyd = delpos234(todo, 0)) != NULL) { |
787 | int x1, y1, d1, x2, y2, d2; |
788 | |
789 | x1 = xyd->x; |
790 | y1 = xyd->y; |
791 | sfree(xyd); |
792 | |
793 | for (d1 = 1; d1 < 0x10; d1 <<= 1) { |
794 | OFFSET(x2, y2, x1, y1, d1, state); |
795 | d2 = F(d1); |
796 | |
797 | /* |
798 | * If the next tile in this direction is connected to |
799 | * us, and there isn't a barrier in the way, and it |
800 | * isn't already marked active, then mark it active and |
801 | * add it to the to-examine list. |
802 | */ |
803 | if ((x2 != moving_col && y2 != moving_row) && |
804 | (tile(state, x1, y1) & d1) && |
805 | (tile(state, x2, y2) & d2) && |
806 | !(barrier(state, x1, y1) & d1) && |
807 | !index(state, active, x2, y2)) { |
808 | index(state, active, x2, y2) = ACTIVE; |
809 | add234(todo, new_xyd(x2, y2, 0)); |
810 | } |
811 | } |
812 | } |
813 | /* Now we expect the todo list to have shrunk to zero size. */ |
814 | assert(count234(todo) == 0); |
815 | freetree234(todo); |
816 | |
817 | return active; |
818 | } |
819 | |
820 | struct game_ui { |
821 | int cur_x, cur_y; |
822 | int cur_visible; |
823 | }; |
824 | |
be8d5aa1 |
825 | static game_ui *new_ui(game_state *state) |
7bed19e1 |
826 | { |
827 | game_ui *ui = snew(game_ui); |
828 | ui->cur_x = state->width / 2; |
829 | ui->cur_y = state->height / 2; |
830 | ui->cur_visible = FALSE; |
831 | |
832 | return ui; |
833 | } |
834 | |
be8d5aa1 |
835 | static void free_ui(game_ui *ui) |
7bed19e1 |
836 | { |
837 | sfree(ui); |
838 | } |
839 | |
840 | /* ---------------------------------------------------------------------- |
841 | * Process a move. |
842 | */ |
843 | |
be8d5aa1 |
844 | static void slide_row(game_state *state, int dir, int row) |
7bed19e1 |
845 | { |
846 | int x = dir > 0 ? -1 : state->width; |
847 | int tx = x + dir; |
848 | int n = state->width - 1; |
849 | unsigned char endtile = state->tiles[T(state, tx, row)]; |
850 | do { |
851 | x = tx; |
852 | tx = (x + dir + state->width) % state->width; |
853 | state->tiles[T(state, x, row)] = state->tiles[T(state, tx, row)]; |
854 | } while (--n > 0); |
855 | state->tiles[T(state, tx, row)] = endtile; |
856 | } |
857 | |
be8d5aa1 |
858 | static void slide_col(game_state *state, int dir, int col) |
7bed19e1 |
859 | { |
860 | int y = dir > 0 ? -1 : state->height; |
861 | int ty = y + dir; |
862 | int n = state->height - 1; |
863 | unsigned char endtile = state->tiles[T(state, col, ty)]; |
864 | do { |
865 | y = ty; |
866 | ty = (y + dir + state->height) % state->height; |
867 | state->tiles[T(state, col, y)] = state->tiles[T(state, col, ty)]; |
868 | } while (--n > 0); |
869 | state->tiles[T(state, col, ty)] = endtile; |
870 | } |
871 | |
be8d5aa1 |
872 | static game_state *make_move(game_state *state, game_ui *ui, |
873 | int x, int y, int button) |
7bed19e1 |
874 | { |
875 | int cx, cy; |
876 | int n, dx, dy; |
877 | game_state *ret; |
878 | |
879 | if (button != LEFT_BUTTON && button != RIGHT_BUTTON) |
880 | return NULL; |
881 | |
882 | cx = (x - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2; |
883 | cy = (y - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2; |
884 | |
885 | if (cy >= 0 && cy < state->height && cy != state->cy) |
886 | { |
887 | if (cx == -1) dx = +1; |
888 | else if (cx == state->width) dx = -1; |
889 | else return NULL; |
890 | n = state->width; |
891 | dy = 0; |
892 | } |
893 | else if (cx >= 0 && cx < state->width && cx != state->cx) |
894 | { |
895 | if (cy == -1) dy = +1; |
896 | else if (cy == state->height) dy = -1; |
897 | else return NULL; |
898 | n = state->height; |
899 | dx = 0; |
900 | } |
901 | else |
902 | return NULL; |
903 | |
904 | /* reverse direction if right hand button is pressed */ |
905 | if (button == RIGHT_BUTTON) |
906 | { |
907 | dx = -dx; |
908 | dy = -dy; |
909 | } |
910 | |
911 | ret = dup_game(state); |
912 | |
913 | if (dx == 0) slide_col(ret, dy, cx); |
914 | else slide_row(ret, dx, cy); |
915 | |
916 | ret->move_count++; |
917 | ret->last_move_row = dx ? cy : -1; |
918 | ret->last_move_col = dx ? -1 : cx; |
919 | ret->last_move_dir = dx + dy; |
920 | |
921 | /* |
922 | * See if the game has been completed. |
923 | */ |
924 | if (!ret->completed) { |
925 | unsigned char *active = compute_active(ret, -1, -1); |
926 | int x1, y1; |
927 | int complete = TRUE; |
928 | |
929 | for (x1 = 0; x1 < ret->width; x1++) |
930 | for (y1 = 0; y1 < ret->height; y1++) |
931 | if (!index(ret, active, x1, y1)) { |
932 | complete = FALSE; |
933 | goto break_label; /* break out of two loops at once */ |
934 | } |
935 | break_label: |
936 | |
937 | sfree(active); |
938 | |
939 | if (complete) |
940 | ret->completed = ret->move_count; |
941 | } |
942 | |
943 | return ret; |
944 | } |
945 | |
946 | /* ---------------------------------------------------------------------- |
947 | * Routines for drawing the game position on the screen. |
948 | */ |
949 | |
950 | struct game_drawstate { |
951 | int started; |
952 | int width, height; |
953 | unsigned char *visible; |
954 | }; |
955 | |
be8d5aa1 |
956 | static game_drawstate *game_new_drawstate(game_state *state) |
7bed19e1 |
957 | { |
958 | game_drawstate *ds = snew(game_drawstate); |
959 | |
960 | ds->started = FALSE; |
961 | ds->width = state->width; |
962 | ds->height = state->height; |
963 | ds->visible = snewn(state->width * state->height, unsigned char); |
964 | memset(ds->visible, 0xFF, state->width * state->height); |
965 | |
966 | return ds; |
967 | } |
968 | |
be8d5aa1 |
969 | static void game_free_drawstate(game_drawstate *ds) |
7bed19e1 |
970 | { |
971 | sfree(ds->visible); |
972 | sfree(ds); |
973 | } |
974 | |
be8d5aa1 |
975 | static void game_size(game_params *params, int *x, int *y) |
7bed19e1 |
976 | { |
977 | *x = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->width + TILE_BORDER; |
978 | *y = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->height + TILE_BORDER; |
979 | } |
980 | |
be8d5aa1 |
981 | static float *game_colours(frontend *fe, game_state *state, int *ncolours) |
7bed19e1 |
982 | { |
983 | float *ret; |
984 | |
985 | ret = snewn(NCOLOURS * 3, float); |
986 | *ncolours = NCOLOURS; |
987 | |
988 | /* |
989 | * Basic background colour is whatever the front end thinks is |
990 | * a sensible default. |
991 | */ |
992 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
993 | |
994 | /* |
995 | * Wires are black. |
996 | */ |
997 | ret[COL_WIRE * 3 + 0] = 0.0F; |
998 | ret[COL_WIRE * 3 + 1] = 0.0F; |
999 | ret[COL_WIRE * 3 + 2] = 0.0F; |
1000 | |
1001 | /* |
1002 | * Powered wires and powered endpoints are cyan. |
1003 | */ |
1004 | ret[COL_POWERED * 3 + 0] = 0.0F; |
1005 | ret[COL_POWERED * 3 + 1] = 1.0F; |
1006 | ret[COL_POWERED * 3 + 2] = 1.0F; |
1007 | |
1008 | /* |
1009 | * Barriers are red. |
1010 | */ |
1011 | ret[COL_BARRIER * 3 + 0] = 1.0F; |
1012 | ret[COL_BARRIER * 3 + 1] = 0.0F; |
1013 | ret[COL_BARRIER * 3 + 2] = 0.0F; |
1014 | |
1015 | /* |
1016 | * Unpowered endpoints are blue. |
1017 | */ |
1018 | ret[COL_ENDPOINT * 3 + 0] = 0.0F; |
1019 | ret[COL_ENDPOINT * 3 + 1] = 0.0F; |
1020 | ret[COL_ENDPOINT * 3 + 2] = 1.0F; |
1021 | |
1022 | /* |
1023 | * Tile borders are a darker grey than the background. |
1024 | */ |
1025 | ret[COL_BORDER * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0]; |
1026 | ret[COL_BORDER * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1]; |
1027 | ret[COL_BORDER * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2]; |
1028 | |
1029 | /* |
1030 | * Flashing tiles are a grey in between those two. |
1031 | */ |
1032 | ret[COL_FLASHING * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0]; |
1033 | ret[COL_FLASHING * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1]; |
1034 | ret[COL_FLASHING * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2]; |
1035 | |
1036 | ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.8F; |
1037 | ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.8F; |
1038 | ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.8F; |
1039 | ret[COL_TEXT * 3 + 0] = 0.0; |
1040 | ret[COL_TEXT * 3 + 1] = 0.0; |
1041 | ret[COL_TEXT * 3 + 2] = 0.0; |
1042 | |
1043 | return ret; |
1044 | } |
1045 | |
1046 | static void draw_thick_line(frontend *fe, int x1, int y1, int x2, int y2, |
1047 | int colour) |
1048 | { |
1049 | draw_line(fe, x1-1, y1, x2-1, y2, COL_WIRE); |
1050 | draw_line(fe, x1+1, y1, x2+1, y2, COL_WIRE); |
1051 | draw_line(fe, x1, y1-1, x2, y2-1, COL_WIRE); |
1052 | draw_line(fe, x1, y1+1, x2, y2+1, COL_WIRE); |
1053 | draw_line(fe, x1, y1, x2, y2, colour); |
1054 | } |
1055 | |
1056 | static void draw_rect_coords(frontend *fe, int x1, int y1, int x2, int y2, |
1057 | int colour) |
1058 | { |
1059 | int mx = (x1 < x2 ? x1 : x2); |
1060 | int my = (y1 < y2 ? y1 : y2); |
1061 | int dx = (x2 + x1 - 2*mx + 1); |
1062 | int dy = (y2 + y1 - 2*my + 1); |
1063 | |
1064 | draw_rect(fe, mx, my, dx, dy, colour); |
1065 | } |
1066 | |
1067 | static void draw_barrier_corner(frontend *fe, int x, int y, int dir, int phase) |
1068 | { |
1069 | int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x; |
1070 | int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y; |
1071 | int x1, y1, dx, dy, dir2; |
1072 | |
1073 | dir >>= 4; |
1074 | |
1075 | dir2 = A(dir); |
1076 | dx = X(dir) + X(dir2); |
1077 | dy = Y(dir) + Y(dir2); |
1078 | x1 = (dx > 0 ? TILE_SIZE+TILE_BORDER-1 : 0); |
1079 | y1 = (dy > 0 ? TILE_SIZE+TILE_BORDER-1 : 0); |
1080 | |
1081 | if (phase == 0) { |
1082 | draw_rect_coords(fe, bx+x1, by+y1, |
1083 | bx+x1-TILE_BORDER*dx, by+y1-(TILE_BORDER-1)*dy, |
1084 | COL_WIRE); |
1085 | draw_rect_coords(fe, bx+x1, by+y1, |
1086 | bx+x1-(TILE_BORDER-1)*dx, by+y1-TILE_BORDER*dy, |
1087 | COL_WIRE); |
1088 | } else { |
1089 | draw_rect_coords(fe, bx+x1, by+y1, |
1090 | bx+x1-(TILE_BORDER-1)*dx, by+y1-(TILE_BORDER-1)*dy, |
1091 | COL_BARRIER); |
1092 | } |
1093 | } |
1094 | |
1095 | static void draw_barrier(frontend *fe, int x, int y, int dir, int phase) |
1096 | { |
1097 | int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x; |
1098 | int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y; |
1099 | int x1, y1, w, h; |
1100 | |
1101 | x1 = (X(dir) > 0 ? TILE_SIZE : X(dir) == 0 ? TILE_BORDER : 0); |
1102 | y1 = (Y(dir) > 0 ? TILE_SIZE : Y(dir) == 0 ? TILE_BORDER : 0); |
1103 | w = (X(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER); |
1104 | h = (Y(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER); |
1105 | |
1106 | if (phase == 0) { |
1107 | draw_rect(fe, bx+x1-X(dir), by+y1-Y(dir), w, h, COL_WIRE); |
1108 | } else { |
1109 | draw_rect(fe, bx+x1, by+y1, w, h, COL_BARRIER); |
1110 | } |
1111 | } |
1112 | |
1113 | static void draw_tile(frontend *fe, game_state *state, int x, int y, int tile, |
1114 | float xshift, float yshift) |
1115 | { |
1116 | int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x + (xshift * TILE_SIZE); |
1117 | int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y + (yshift * TILE_SIZE); |
1118 | float cx, cy, ex, ey; |
1119 | int dir, col; |
1120 | |
1121 | /* |
1122 | * When we draw a single tile, we must draw everything up to |
1123 | * and including the borders around the tile. This means that |
1124 | * if the neighbouring tiles have connections to those borders, |
1125 | * we must draw those connections on the borders themselves. |
1126 | * |
1127 | * This would be terribly fiddly if we ever had to draw a tile |
1128 | * while its neighbour was in mid-rotate, because we'd have to |
1129 | * arrange to _know_ that the neighbour was being rotated and |
1130 | * hence had an anomalous effect on the redraw of this tile. |
1131 | * Fortunately, the drawing algorithm avoids ever calling us in |
1132 | * this circumstance: we're either drawing lots of straight |
1133 | * tiles at game start or after a move is complete, or we're |
1134 | * repeatedly drawing only the rotating tile. So no problem. |
1135 | */ |
1136 | |
1137 | /* |
1138 | * So. First blank the tile out completely: draw a big |
1139 | * rectangle in border colour, and a smaller rectangle in |
1140 | * background colour to fill it in. |
1141 | */ |
1142 | draw_rect(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER, |
1143 | COL_BORDER); |
1144 | draw_rect(fe, bx+TILE_BORDER, by+TILE_BORDER, |
1145 | TILE_SIZE-TILE_BORDER, TILE_SIZE-TILE_BORDER, |
1146 | tile & FLASHING ? COL_FLASHING : COL_BACKGROUND); |
1147 | |
1148 | /* |
1149 | * Draw the wires. |
1150 | */ |
1151 | cx = cy = TILE_BORDER + (TILE_SIZE-TILE_BORDER) / 2.0F - 0.5F; |
1152 | col = (tile & ACTIVE ? COL_POWERED : COL_WIRE); |
1153 | for (dir = 1; dir < 0x10; dir <<= 1) { |
1154 | if (tile & dir) { |
1155 | ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir); |
1156 | ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir); |
1157 | draw_thick_line(fe, bx+(int)cx, by+(int)cy, |
1158 | bx+(int)(cx+ex), by+(int)(cy+ey), |
1159 | COL_WIRE); |
1160 | } |
1161 | } |
1162 | for (dir = 1; dir < 0x10; dir <<= 1) { |
1163 | if (tile & dir) { |
1164 | ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir); |
1165 | ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir); |
1166 | draw_line(fe, bx+(int)cx, by+(int)cy, |
1167 | bx+(int)(cx+ex), by+(int)(cy+ey), col); |
1168 | } |
1169 | } |
1170 | |
1171 | /* |
1172 | * Draw the box in the middle. We do this in blue if the tile |
1173 | * is an unpowered endpoint, in cyan if the tile is a powered |
1174 | * endpoint, in black if the tile is the centrepiece, and |
1175 | * otherwise not at all. |
1176 | */ |
1177 | col = -1; |
1178 | if (x == state->cx && y == state->cy) |
1179 | col = COL_WIRE; |
1180 | else if (COUNT(tile) == 1) { |
1181 | col = (tile & ACTIVE ? COL_POWERED : COL_ENDPOINT); |
1182 | } |
1183 | if (col >= 0) { |
1184 | int i, points[8]; |
1185 | |
1186 | points[0] = +1; points[1] = +1; |
1187 | points[2] = +1; points[3] = -1; |
1188 | points[4] = -1; points[5] = -1; |
1189 | points[6] = -1; points[7] = +1; |
1190 | |
1191 | for (i = 0; i < 8; i += 2) { |
1192 | ex = (TILE_SIZE * 0.24F) * points[i]; |
1193 | ey = (TILE_SIZE * 0.24F) * points[i+1]; |
1194 | points[i] = bx+(int)(cx+ex); |
1195 | points[i+1] = by+(int)(cy+ey); |
1196 | } |
1197 | |
1198 | draw_polygon(fe, points, 4, TRUE, col); |
1199 | draw_polygon(fe, points, 4, FALSE, COL_WIRE); |
1200 | } |
1201 | |
1202 | /* |
1203 | * Draw the points on the border if other tiles are connected |
1204 | * to us. |
1205 | */ |
1206 | for (dir = 1; dir < 0x10; dir <<= 1) { |
1207 | int dx, dy, px, py, lx, ly, vx, vy, ox, oy; |
1208 | |
1209 | dx = X(dir); |
1210 | dy = Y(dir); |
1211 | |
1212 | ox = x + dx; |
1213 | oy = y + dy; |
1214 | |
1215 | if (ox < 0 || ox >= state->width || oy < 0 || oy >= state->height) |
1216 | continue; |
1217 | |
1218 | if (!(tile(state, ox, oy) & F(dir))) |
1219 | continue; |
1220 | |
1221 | px = bx + (int)(dx>0 ? TILE_SIZE + TILE_BORDER - 1 : dx<0 ? 0 : cx); |
1222 | py = by + (int)(dy>0 ? TILE_SIZE + TILE_BORDER - 1 : dy<0 ? 0 : cy); |
1223 | lx = dx * (TILE_BORDER-1); |
1224 | ly = dy * (TILE_BORDER-1); |
1225 | vx = (dy ? 1 : 0); |
1226 | vy = (dx ? 1 : 0); |
1227 | |
1228 | if (xshift == 0.0 && yshift == 0.0 && (tile & dir)) { |
1229 | /* |
1230 | * If we are fully connected to the other tile, we must |
1231 | * draw right across the tile border. (We can use our |
1232 | * own ACTIVE state to determine what colour to do this |
1233 | * in: if we are fully connected to the other tile then |
1234 | * the two ACTIVE states will be the same.) |
1235 | */ |
1236 | draw_rect_coords(fe, px-vx, py-vy, px+lx+vx, py+ly+vy, COL_WIRE); |
1237 | draw_rect_coords(fe, px, py, px+lx, py+ly, |
1238 | (tile & ACTIVE) ? COL_POWERED : COL_WIRE); |
1239 | } else { |
1240 | /* |
1241 | * The other tile extends into our border, but isn't |
1242 | * actually connected to us. Just draw a single black |
1243 | * dot. |
1244 | */ |
1245 | draw_rect_coords(fe, px, py, px, py, COL_WIRE); |
1246 | } |
1247 | } |
1248 | |
1249 | draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER); |
1250 | } |
1251 | |
1252 | static void draw_tile_barriers(frontend *fe, game_state *state, int x, int y) |
1253 | { |
1254 | int phase; |
1255 | int dir; |
1256 | int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x; |
1257 | int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y; |
1258 | /* |
1259 | * Draw barrier corners, and then barriers. |
1260 | */ |
1261 | for (phase = 0; phase < 2; phase++) { |
1262 | for (dir = 1; dir < 0x10; dir <<= 1) |
1263 | if (barrier(state, x, y) & (dir << 4)) |
1264 | draw_barrier_corner(fe, x, y, dir << 4, phase); |
1265 | for (dir = 1; dir < 0x10; dir <<= 1) |
1266 | if (barrier(state, x, y) & dir) |
1267 | draw_barrier(fe, x, y, dir, phase); |
1268 | } |
1269 | |
1270 | draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER); |
1271 | } |
1272 | |
1273 | static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy) |
1274 | { |
1275 | int coords[14]; |
1276 | int ydy = -xdx, ydx = xdy; |
1277 | |
1278 | x = x * TILE_SIZE + BORDER + WINDOW_OFFSET; |
1279 | y = y * TILE_SIZE + BORDER + WINDOW_OFFSET; |
1280 | |
1281 | #define POINT(n, xx, yy) ( \ |
1282 | coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \ |
1283 | coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy) |
1284 | |
1285 | POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */ |
1286 | POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */ |
1287 | POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */ |
1288 | POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */ |
1289 | POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */ |
1290 | POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */ |
1291 | POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */ |
1292 | |
1293 | draw_polygon(fe, coords, 7, TRUE, COL_LOWLIGHT); |
1294 | draw_polygon(fe, coords, 7, FALSE, COL_TEXT); |
1295 | } |
1296 | |
be8d5aa1 |
1297 | static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, |
c822de4a |
1298 | game_state *state, int dir, game_ui *ui, float t, float ft) |
7bed19e1 |
1299 | { |
1300 | int x, y, tx, ty, frame; |
1301 | unsigned char *active; |
1302 | float xshift = 0.0; |
1303 | float yshift = 0.0; |
1304 | |
1305 | /* |
1306 | * Clear the screen and draw the exterior barrier lines if this |
1307 | * is our first call. |
1308 | */ |
1309 | if (!ds->started) { |
1310 | int phase; |
1311 | |
1312 | ds->started = TRUE; |
1313 | |
1314 | draw_rect(fe, 0, 0, |
1315 | BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * state->width + TILE_BORDER, |
1316 | BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * state->height + TILE_BORDER, |
1317 | COL_BACKGROUND); |
1318 | draw_update(fe, 0, 0, |
1319 | BORDER * 2 + WINDOW_OFFSET*2 + TILE_SIZE*state->width + TILE_BORDER, |
1320 | BORDER * 2 + WINDOW_OFFSET*2 + TILE_SIZE*state->height + TILE_BORDER); |
1321 | |
1322 | for (phase = 0; phase < 2; phase++) { |
1323 | |
1324 | for (x = 0; x < ds->width; x++) { |
1325 | if (barrier(state, x, 0) & UL) |
1326 | draw_barrier_corner(fe, x, -1, LD, phase); |
1327 | if (barrier(state, x, 0) & RU) |
1328 | draw_barrier_corner(fe, x, -1, DR, phase); |
1329 | if (barrier(state, x, 0) & U) |
1330 | draw_barrier(fe, x, -1, D, phase); |
1331 | if (barrier(state, x, ds->height-1) & DR) |
1332 | draw_barrier_corner(fe, x, ds->height, RU, phase); |
1333 | if (barrier(state, x, ds->height-1) & LD) |
1334 | draw_barrier_corner(fe, x, ds->height, UL, phase); |
1335 | if (barrier(state, x, ds->height-1) & D) |
1336 | draw_barrier(fe, x, ds->height, U, phase); |
1337 | } |
1338 | |
1339 | for (y = 0; y < ds->height; y++) { |
1340 | if (barrier(state, 0, y) & UL) |
1341 | draw_barrier_corner(fe, -1, y, RU, phase); |
1342 | if (barrier(state, 0, y) & LD) |
1343 | draw_barrier_corner(fe, -1, y, DR, phase); |
1344 | if (barrier(state, 0, y) & L) |
1345 | draw_barrier(fe, -1, y, R, phase); |
1346 | if (barrier(state, ds->width-1, y) & RU) |
1347 | draw_barrier_corner(fe, ds->width, y, UL, phase); |
1348 | if (barrier(state, ds->width-1, y) & DR) |
1349 | draw_barrier_corner(fe, ds->width, y, LD, phase); |
1350 | if (barrier(state, ds->width-1, y) & R) |
1351 | draw_barrier(fe, ds->width, y, L, phase); |
1352 | } |
1353 | } |
1354 | |
1355 | /* |
1356 | * Arrows for making moves. |
1357 | */ |
1358 | for (x = 0; x < ds->width; x++) { |
1359 | if (x == state->cx) continue; |
1360 | draw_arrow(fe, x, 0, +1, 0); |
1361 | draw_arrow(fe, x+1, ds->height, -1, 0); |
1362 | } |
1363 | for (y = 0; y < ds->height; y++) { |
1364 | if (y == state->cy) continue; |
1365 | draw_arrow(fe, ds->width, y, 0, +1); |
1366 | draw_arrow(fe, 0, y+1, 0, -1); |
1367 | } |
1368 | } |
1369 | |
1370 | /* Check if this is an undo. If so, we will need to run any animation |
1371 | * backwards. |
1372 | */ |
1373 | if (oldstate && oldstate->move_count > state->move_count) { |
1374 | game_state * tmpstate = state; |
1375 | state = oldstate; |
1376 | oldstate = tmpstate; |
1377 | t = ANIM_TIME - t; |
1378 | } |
1379 | |
1380 | tx = ty = -1; |
1381 | if (oldstate && (t < ANIM_TIME)) { |
1382 | /* |
1383 | * We're animating a slide, of row/column number |
1384 | * state->last_move_pos, in direction |
1385 | * state->last_move_dir |
1386 | */ |
1387 | xshift = state->last_move_row == -1 ? 0.0 : |
1388 | (1 - t / ANIM_TIME) * state->last_move_dir; |
1389 | yshift = state->last_move_col == -1 ? 0.0 : |
1390 | (1 - t / ANIM_TIME) * state->last_move_dir; |
1391 | } |
1392 | |
1393 | frame = -1; |
1394 | if (ft > 0) { |
1395 | /* |
1396 | * We're animating a completion flash. Find which frame |
1397 | * we're at. |
1398 | */ |
1399 | frame = (int)(ft / FLASH_FRAME); |
1400 | } |
1401 | |
1402 | /* |
1403 | * Draw any tile which differs from the way it was last drawn. |
1404 | */ |
1405 | if (xshift != 0.0 || yshift != 0.0) { |
1406 | active = compute_active(state, |
1407 | state->last_move_row, state->last_move_col); |
1408 | } else { |
1409 | active = compute_active(state, -1, -1); |
1410 | } |
1411 | |
1412 | clip(fe, |
1413 | BORDER + WINDOW_OFFSET, BORDER + WINDOW_OFFSET, |
1414 | TILE_SIZE * state->width + TILE_BORDER, |
1415 | TILE_SIZE * state->height + TILE_BORDER); |
1416 | |
1417 | for (x = 0; x < ds->width; x++) |
1418 | for (y = 0; y < ds->height; y++) { |
1419 | unsigned char c = tile(state, x, y) | index(state, active, x, y); |
1420 | |
1421 | /* |
1422 | * In a completion flash, we adjust the FLASHING bit |
1423 | * depending on our distance from the centre point and |
1424 | * the frame number. |
1425 | */ |
1426 | if (frame >= 0) { |
1427 | int xdist, ydist, dist; |
1428 | xdist = (x < state->cx ? state->cx - x : x - state->cx); |
1429 | ydist = (y < state->cy ? state->cy - y : y - state->cy); |
1430 | dist = (xdist > ydist ? xdist : ydist); |
1431 | |
1432 | if (frame >= dist && frame < dist+4) { |
1433 | int flash = (frame - dist) & 1; |
1434 | flash = flash ? FLASHING : 0; |
1435 | c = (c &~ FLASHING) | flash; |
1436 | } |
1437 | } |
1438 | |
1439 | if (index(state, ds->visible, x, y) != c || |
1440 | index(state, ds->visible, x, y) == 0xFF || |
1441 | (x == state->last_move_col || y == state->last_move_row)) |
1442 | { |
1443 | float xs = (y == state->last_move_row ? xshift : 0.0); |
1444 | float ys = (x == state->last_move_col ? yshift : 0.0); |
1445 | |
1446 | draw_tile(fe, state, x, y, c, xs, ys); |
1447 | if (xs < 0 && x == 0) |
1448 | draw_tile(fe, state, state->width, y, c, xs, ys); |
1449 | else if (xs > 0 && x == state->width - 1) |
1450 | draw_tile(fe, state, -1, y, c, xs, ys); |
1451 | else if (ys < 0 && y == 0) |
1452 | draw_tile(fe, state, x, state->height, c, xs, ys); |
1453 | else if (ys > 0 && y == state->height - 1) |
1454 | draw_tile(fe, state, x, -1, c, xs, ys); |
1455 | |
1456 | if (x == state->last_move_col || y == state->last_move_row) |
1457 | index(state, ds->visible, x, y) = 0xFF; |
1458 | else |
1459 | index(state, ds->visible, x, y) = c; |
1460 | } |
1461 | } |
1462 | |
1463 | for (x = 0; x < ds->width; x++) |
1464 | for (y = 0; y < ds->height; y++) |
1465 | draw_tile_barriers(fe, state, x, y); |
1466 | |
1467 | unclip(fe); |
1468 | |
1469 | /* |
1470 | * Update the status bar. |
1471 | */ |
1472 | { |
1473 | char statusbuf[256]; |
1474 | int i, n, a; |
1475 | |
1476 | n = state->width * state->height; |
1477 | for (i = a = 0; i < n; i++) |
1478 | if (active[i]) |
1479 | a++; |
1480 | |
1481 | sprintf(statusbuf, "%sMoves: %d Active: %d/%d", |
1482 | (state->completed ? "COMPLETED! " : ""), |
1483 | (state->completed ? state->completed : state->move_count), |
1484 | a, n); |
1485 | |
1486 | status_bar(fe, statusbuf); |
1487 | } |
1488 | |
1489 | sfree(active); |
1490 | } |
1491 | |
be8d5aa1 |
1492 | static float game_anim_length(game_state *oldstate, |
1493 | game_state *newstate, int dir) |
7bed19e1 |
1494 | { |
1495 | return ANIM_TIME; |
1496 | } |
1497 | |
be8d5aa1 |
1498 | static float game_flash_length(game_state *oldstate, |
1499 | game_state *newstate, int dir) |
7bed19e1 |
1500 | { |
1501 | /* |
1502 | * If the game has just been completed, we display a completion |
1503 | * flash. |
1504 | */ |
1505 | if (!oldstate->completed && newstate->completed) { |
1506 | int size; |
1507 | size = 0; |
1508 | if (size < newstate->cx+1) |
1509 | size = newstate->cx+1; |
1510 | if (size < newstate->cy+1) |
1511 | size = newstate->cy+1; |
1512 | if (size < newstate->width - newstate->cx) |
1513 | size = newstate->width - newstate->cx; |
1514 | if (size < newstate->height - newstate->cy) |
1515 | size = newstate->height - newstate->cy; |
1516 | return FLASH_FRAME * (size+4); |
1517 | } |
1518 | |
1519 | return 0.0F; |
1520 | } |
1521 | |
be8d5aa1 |
1522 | static int game_wants_statusbar(void) |
7bed19e1 |
1523 | { |
1524 | return TRUE; |
1525 | } |
be8d5aa1 |
1526 | |
1527 | #ifdef COMBINED |
1528 | #define thegame netslide |
1529 | #endif |
1530 | |
1531 | const struct game thegame = { |
1d228b10 |
1532 | "Netslide", "games.netslide", |
be8d5aa1 |
1533 | default_params, |
1534 | game_fetch_preset, |
1535 | decode_params, |
1536 | encode_params, |
1537 | free_params, |
1538 | dup_params, |
1d228b10 |
1539 | TRUE, game_configure, custom_params, |
be8d5aa1 |
1540 | validate_params, |
1541 | new_game_seed, |
6f2d8d7c |
1542 | game_free_aux_info, |
be8d5aa1 |
1543 | validate_seed, |
1544 | new_game, |
1545 | dup_game, |
1546 | free_game, |
9b4b03d3 |
1547 | FALSE, game_text_format, |
be8d5aa1 |
1548 | new_ui, |
1549 | free_ui, |
1550 | make_move, |
1551 | game_size, |
1552 | game_colours, |
1553 | game_new_drawstate, |
1554 | game_free_drawstate, |
1555 | game_redraw, |
1556 | game_anim_length, |
1557 | game_flash_length, |
1558 | game_wants_statusbar, |
1559 | }; |