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