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