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