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