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