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