2 * netslide.c: cross between Net and Sixteen, courtesy of Richard
16 #define MATMUL(xr,yr,m,x,y) do { \
17 float rx, ry, xx = (x), yy = (y), *mat = (m); \
18 rx = mat[0] * xx + mat[2] * yy; \
19 ry = mat[1] * xx + mat[3] * yy; \
20 (xr) = rx; (yr) = ry; \
23 /* Direction and other bitfields */
30 /* Corner flags go in the barriers array */
36 /* Get tile at given coordinate */
37 #define T(state, x, y) ( (y) * (state)->width + (x) )
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) )
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 )
52 #define COUNT(x) ( (((x) & 0x08) >> 3) + (((x) & 0x04) >> 2) + \
53 (((x) & 0x02) >> 1) + ((x) & 0x01) )
55 #define PREFERRED_TILE_SIZE 48
56 #define TILE_SIZE (ds->tilesize)
57 #define BORDER TILE_SIZE
59 #define WINDOW_OFFSET 0
61 #define ANIM_TIME 0.13F
62 #define FLASH_FRAME 0.07F
81 float barrier_probability
;
86 int width
, height
, cx
, cy
, wrapping
, completed
;
88 int move_count
, movetarget
;
90 /* position (row or col number, starting at 0) of last move. */
91 int last_move_row
, last_move_col
;
93 /* direction of last move: +1 or -1 */
97 unsigned char *barriers
;
100 #define OFFSET(x2,y2,x1,y1,dir,state) \
101 ( (x2) = ((x1) + (state)->width + X((dir))) % (state)->width, \
102 (y2) = ((y1) + (state)->height + Y((dir))) % (state)->height)
104 #define index(state, a, x, y) ( a[(y) * (state)->width + (x)] )
105 #define tile(state, x, y) index(state, (state)->tiles, x, y)
106 #define barrier(state, x, y) index(state, (state)->barriers, x, y)
112 static int xyd_cmp(void *av
, void *bv
) {
113 struct xyd
*a
= (struct xyd
*)av
;
114 struct xyd
*b
= (struct xyd
*)bv
;
123 if (a
->direction
< b
->direction
)
125 if (a
->direction
> b
->direction
)
130 static struct xyd
*new_xyd(int x
, int y
, int direction
)
132 struct xyd
*xyd
= snew(struct xyd
);
135 xyd
->direction
= direction
;
139 static void slide_col(game_state
*state
, int dir
, int col
);
140 static void slide_col_int(int w
, int h
, unsigned char *tiles
, int dir
, int col
);
141 static void slide_row(game_state
*state
, int dir
, int row
);
142 static void slide_row_int(int w
, int h
, unsigned char *tiles
, int dir
, int row
);
144 /* ----------------------------------------------------------------------
145 * Manage game parameters.
147 static game_params
*default_params(void)
149 game_params
*ret
= snew(game_params
);
153 ret
->wrapping
= FALSE
;
154 ret
->barrier_probability
= 1.0;
160 static const struct { int x
, y
, wrap
, bprob
; const char* desc
; }
161 netslide_presets
[] = {
162 {3, 3, FALSE
, 1.0, " easy"},
163 {3, 3, FALSE
, 0.0, " medium"},
164 {3, 3, TRUE
, 0.0, " hard"},
165 {4, 4, FALSE
, 1.0, " easy"},
166 {4, 4, FALSE
, 0.0, " medium"},
167 {4, 4, TRUE
, 0.0, " hard"},
168 {5, 5, FALSE
, 1.0, " easy"},
169 {5, 5, FALSE
, 0.0, " medium"},
170 {5, 5, TRUE
, 0.0, " hard"},
173 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
178 if (i
< 0 || i
>= lenof(netslide_presets
))
181 ret
= snew(game_params
);
182 ret
->width
= netslide_presets
[i
].x
;
183 ret
->height
= netslide_presets
[i
].y
;
184 ret
->wrapping
= netslide_presets
[i
].wrap
;
185 ret
->barrier_probability
= netslide_presets
[i
].bprob
;
188 sprintf(str
, "%dx%d%s", ret
->width
, ret
->height
, netslide_presets
[i
].desc
);
195 static void free_params(game_params
*params
)
200 static game_params
*dup_params(game_params
*params
)
202 game_params
*ret
= snew(game_params
);
203 *ret
= *params
; /* structure copy */
207 static void decode_params(game_params
*ret
, char const *string
)
209 char const *p
= string
;
211 ret
->wrapping
= FALSE
;
212 ret
->barrier_probability
= 0.0;
215 ret
->width
= atoi(p
);
216 while (*p
&& isdigit((unsigned char)*p
)) p
++;
219 ret
->height
= atoi(p
);
220 while (*p
&& isdigit((unsigned char)*p
)) p
++;
221 if ( (ret
->wrapping
= (*p
== 'w')) != 0 )
224 ret
->barrier_probability
= atof(++p
);
225 while (*p
&& (isdigit((unsigned char)*p
) || *p
== '.')) p
++;
228 ret
->movetarget
= atoi(++p
);
231 ret
->height
= ret
->width
;
235 static char *encode_params(game_params
*params
, int full
)
240 len
= sprintf(ret
, "%dx%d", params
->width
, params
->height
);
241 if (params
->wrapping
)
243 if (full
&& params
->barrier_probability
)
244 len
+= sprintf(ret
+len
, "b%g", params
->barrier_probability
);
245 /* Shuffle limit is part of the limited parameters, because we have to
246 * provide the target move count. */
247 if (params
->movetarget
)
248 len
+= sprintf(ret
+len
, "m%d", params
->movetarget
);
249 assert(len
< lenof(ret
));
255 static config_item
*game_configure(game_params
*params
)
260 ret
= snewn(6, config_item
);
262 ret
[0].name
= "Width";
263 ret
[0].type
= C_STRING
;
264 sprintf(buf
, "%d", params
->width
);
265 ret
[0].sval
= dupstr(buf
);
268 ret
[1].name
= "Height";
269 ret
[1].type
= C_STRING
;
270 sprintf(buf
, "%d", params
->height
);
271 ret
[1].sval
= dupstr(buf
);
274 ret
[2].name
= "Walls wrap around";
275 ret
[2].type
= C_BOOLEAN
;
277 ret
[2].ival
= params
->wrapping
;
279 ret
[3].name
= "Barrier probability";
280 ret
[3].type
= C_STRING
;
281 sprintf(buf
, "%g", params
->barrier_probability
);
282 ret
[3].sval
= dupstr(buf
);
285 ret
[4].name
= "Number of shuffling moves";
286 ret
[4].type
= C_STRING
;
287 sprintf(buf
, "%d", params
->movetarget
);
288 ret
[4].sval
= dupstr(buf
);
299 static game_params
*custom_params(config_item
*cfg
)
301 game_params
*ret
= snew(game_params
);
303 ret
->width
= atoi(cfg
[0].sval
);
304 ret
->height
= atoi(cfg
[1].sval
);
305 ret
->wrapping
= cfg
[2].ival
;
306 ret
->barrier_probability
= (float)atof(cfg
[3].sval
);
307 ret
->movetarget
= atoi(cfg
[4].sval
);
312 static char *validate_params(game_params
*params
, int full
)
314 if (params
->width
<= 1 || params
->height
<= 1)
315 return "Width and height must both be greater than one";
316 if (params
->barrier_probability
< 0)
317 return "Barrier probability may not be negative";
318 if (params
->barrier_probability
> 1)
319 return "Barrier probability may not be greater than 1";
323 /* ----------------------------------------------------------------------
324 * Randomly select a new game description.
327 static char *new_game_desc(game_params
*params
, random_state
*rs
,
328 char **aux
, int interactive
)
330 tree234
*possibilities
, *barriertree
;
331 int w
, h
, x
, y
, cx
, cy
, nbarriers
;
332 unsigned char *tiles
, *barriers
;
338 tiles
= snewn(w
* h
, unsigned char);
339 memset(tiles
, 0, w
* h
);
340 barriers
= snewn(w
* h
, unsigned char);
341 memset(barriers
, 0, w
* h
);
347 * Construct the unshuffled grid.
349 * To do this, we simply start at the centre point, repeatedly
350 * choose a random possibility out of the available ways to
351 * extend a used square into an unused one, and do it. After
352 * extending the third line out of a square, we remove the
353 * fourth from the possibilities list to avoid any full-cross
354 * squares (which would make the game too easy because they
355 * only have one orientation).
357 * The slightly worrying thing is the avoidance of full-cross
358 * squares. Can this cause our unsophisticated construction
359 * algorithm to paint itself into a corner, by getting into a
360 * situation where there are some unreached squares and the
361 * only way to reach any of them is to extend a T-piece into a
364 * Answer: no it can't, and here's a proof.
366 * Any contiguous group of such unreachable squares must be
367 * surrounded on _all_ sides by T-pieces pointing away from the
368 * group. (If not, then there is a square which can be extended
369 * into one of the `unreachable' ones, and so it wasn't
370 * unreachable after all.) In particular, this implies that
371 * each contiguous group of unreachable squares must be
372 * rectangular in shape (any deviation from that yields a
373 * non-T-piece next to an `unreachable' square).
375 * So we have a rectangle of unreachable squares, with T-pieces
376 * forming a solid border around the rectangle. The corners of
377 * that border must be connected (since every tile connects all
378 * the lines arriving in it), and therefore the border must
379 * form a closed loop around the rectangle.
381 * But this can't have happened in the first place, since we
382 * _know_ we've avoided creating closed loops! Hence, no such
383 * situation can ever arise, and the naive grid construction
384 * algorithm will guaranteeably result in a complete grid
385 * containing no unreached squares, no full crosses _and_ no
388 possibilities
= newtree234(xyd_cmp
);
391 add234(possibilities
, new_xyd(cx
, cy
, R
));
393 add234(possibilities
, new_xyd(cx
, cy
, U
));
395 add234(possibilities
, new_xyd(cx
, cy
, L
));
397 add234(possibilities
, new_xyd(cx
, cy
, D
));
399 while (count234(possibilities
) > 0) {
402 int x1
, y1
, d1
, x2
, y2
, d2
, d
;
405 * Extract a randomly chosen possibility from the list.
407 i
= random_upto(rs
, count234(possibilities
));
408 xyd
= delpos234(possibilities
, i
);
414 OFFSET(x2
, y2
, x1
, y1
, d1
, params
);
416 #ifdef GENERATION_DIAGNOSTICS
417 printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n",
418 x1
, y1
, "0RU3L567D9abcdef"[d1
], x2
, y2
, "0RU3L567D9abcdef"[d2
]);
422 * Make the connection. (We should be moving to an as yet
425 index(params
, tiles
, x1
, y1
) |= d1
;
426 assert(index(params
, tiles
, x2
, y2
) == 0);
427 index(params
, tiles
, x2
, y2
) |= d2
;
430 * If we have created a T-piece, remove its last
433 if (COUNT(index(params
, tiles
, x1
, y1
)) == 3) {
434 struct xyd xyd1
, *xydp
;
438 xyd1
.direction
= 0x0F ^ index(params
, tiles
, x1
, y1
);
440 xydp
= find234(possibilities
, &xyd1
, NULL
);
443 #ifdef GENERATION_DIAGNOSTICS
444 printf("T-piece; removing (%d,%d,%c)\n",
445 xydp
->x
, xydp
->y
, "0RU3L567D9abcdef"[xydp
->direction
]);
447 del234(possibilities
, xydp
);
453 * Remove all other possibilities that were pointing at the
454 * tile we've just moved into.
456 for (d
= 1; d
< 0x10; d
<<= 1) {
458 struct xyd xyd1
, *xydp
;
460 OFFSET(x3
, y3
, x2
, y2
, d
, params
);
467 xydp
= find234(possibilities
, &xyd1
, NULL
);
470 #ifdef GENERATION_DIAGNOSTICS
471 printf("Loop avoidance; removing (%d,%d,%c)\n",
472 xydp
->x
, xydp
->y
, "0RU3L567D9abcdef"[xydp
->direction
]);
474 del234(possibilities
, xydp
);
480 * Add new possibilities to the list for moving _out_ of
481 * the tile we have just moved into.
483 for (d
= 1; d
< 0x10; d
<<= 1) {
487 continue; /* we've got this one already */
489 if (!params
->wrapping
) {
490 if (d
== U
&& y2
== 0)
492 if (d
== D
&& y2
== h
-1)
494 if (d
== L
&& x2
== 0)
496 if (d
== R
&& x2
== w
-1)
500 OFFSET(x3
, y3
, x2
, y2
, d
, params
);
502 if (index(params
, tiles
, x3
, y3
))
503 continue; /* this would create a loop */
505 #ifdef GENERATION_DIAGNOSTICS
506 printf("New frontier; adding (%d,%d,%c)\n",
507 x2
, y2
, "0RU3L567D9abcdef"[d
]);
509 add234(possibilities
, new_xyd(x2
, y2
, d
));
512 /* Having done that, we should have no possibilities remaining. */
513 assert(count234(possibilities
) == 0);
514 freetree234(possibilities
);
517 * Now compute a list of the possible barrier locations.
519 barriertree
= newtree234(xyd_cmp
);
520 for (y
= 0; y
< h
; y
++) {
521 for (x
= 0; x
< w
; x
++) {
523 if (!(index(params
, tiles
, x
, y
) & R
) &&
524 (params
->wrapping
|| x
< w
-1))
525 add234(barriertree
, new_xyd(x
, y
, R
));
526 if (!(index(params
, tiles
, x
, y
) & D
) &&
527 (params
->wrapping
|| y
< h
-1))
528 add234(barriertree
, new_xyd(x
, y
, D
));
533 * Save the unshuffled grid in aux.
540 * String format is exactly the same as a solve move, so we
541 * can just dupstr this in solve_game().
544 solution
= snewn(w
* h
+ 2, char);
546 for (i
= 0; i
< w
* h
; i
++)
547 solution
[i
+1] = "0123456789abcdef"[tiles
[i
] & 0xF];
548 solution
[w
*h
+1] = '\0';
554 * Now shuffle the grid.
555 * FIXME - this simply does a set of random moves to shuffle the pieces,
556 * although we make a token effort to avoid boring cases by avoiding moves
557 * that directly undo the previous one, or that repeat so often as to
558 * turn into fewer moves.
560 * A better way would be to number all the pieces, generate a placement
561 * for all the numbers as for "sixteen", observing parity constraints if
562 * neccessary, and then place the pieces according to their numbering.
563 * BUT - I'm not sure if this will work, since we disallow movement of
564 * the middle row and column.
570 int moves
= params
->movetarget
;
571 int prevdir
= -1, prevrowcol
= -1, nrepeats
= 0;
572 if (!moves
) moves
= cols
* rows
* 2;
573 for (i
= 0; i
< moves
; /* incremented conditionally */) {
574 /* Choose a direction: 0,1,2,3 = up, right, down, left. */
575 int dir
= random_upto(rs
, 4);
578 int col
= random_upto(rs
, cols
);
579 if (col
>= cx
) col
+= 1; /* avoid centre */
580 if (col
== prevrowcol
) {
581 if (dir
== 2-prevdir
)
582 continue; /* undoes last move */
583 else if (dir
== prevdir
&& (nrepeats
+1)*2 > h
)
584 continue; /* makes fewer moves */
586 slide_col_int(w
, h
, tiles
, 1 - dir
, col
);
589 int row
= random_upto(rs
, rows
);
590 if (row
>= cy
) row
+= 1; /* avoid centre */
591 if (row
== prevrowcol
) {
592 if (dir
== 4-prevdir
)
593 continue; /* undoes last move */
594 else if (dir
== prevdir
&& (nrepeats
+1)*2 > w
)
595 continue; /* makes fewer moves */
597 slide_row_int(w
, h
, tiles
, 2 - dir
, row
);
600 if (dir
== prevdir
&& rowcol
== prevrowcol
)
606 i
++; /* if we got here, the move was accepted */
611 * And now choose barrier locations. (We carefully do this
612 * _after_ shuffling, so that changing the barrier rate in the
613 * params while keeping the random seed the same will give the
614 * same shuffled grid and _only_ change the barrier locations.
615 * Also the way we choose barrier locations, by repeatedly
616 * choosing one possibility from the list until we have enough,
617 * is designed to ensure that raising the barrier rate while
618 * keeping the seed the same will provide a superset of the
619 * previous barrier set - i.e. if you ask for 10 barriers, and
620 * then decide that's still too hard and ask for 20, you'll get
621 * the original 10 plus 10 more, rather than getting 20 new
622 * ones and the chance of remembering your first 10.)
624 nbarriers
= (int)(params
->barrier_probability
* count234(barriertree
));
625 assert(nbarriers
>= 0 && nbarriers
<= count234(barriertree
));
627 while (nbarriers
> 0) {
630 int x1
, y1
, d1
, x2
, y2
, d2
;
633 * Extract a randomly chosen barrier from the list.
635 i
= random_upto(rs
, count234(barriertree
));
636 xyd
= delpos234(barriertree
, i
);
645 OFFSET(x2
, y2
, x1
, y1
, d1
, params
);
648 index(params
, barriers
, x1
, y1
) |= d1
;
649 index(params
, barriers
, x2
, y2
) |= d2
;
655 * Clean up the rest of the barrier list.
660 while ( (xyd
= delpos234(barriertree
, 0)) != NULL
)
663 freetree234(barriertree
);
667 * Finally, encode the grid into a string game description.
669 * My syntax is extremely simple: each square is encoded as a
670 * hex digit in which bit 0 means a connection on the right,
671 * bit 1 means up, bit 2 left and bit 3 down. (i.e. the same
672 * encoding as used internally). Each digit is followed by
673 * optional barrier indicators: `v' means a vertical barrier to
674 * the right of it, and `h' means a horizontal barrier below
677 desc
= snewn(w
* h
* 3 + 1, char);
679 for (y
= 0; y
< h
; y
++) {
680 for (x
= 0; x
< w
; x
++) {
681 *p
++ = "0123456789abcdef"[index(params
, tiles
, x
, y
)];
682 if ((params
->wrapping
|| x
< w
-1) &&
683 (index(params
, barriers
, x
, y
) & R
))
685 if ((params
->wrapping
|| y
< h
-1) &&
686 (index(params
, barriers
, x
, y
) & D
))
690 assert(p
- desc
<= w
*h
*3);
699 static char *validate_desc(game_params
*params
, char *desc
)
701 int w
= params
->width
, h
= params
->height
;
704 for (i
= 0; i
< w
*h
; i
++) {
705 if (*desc
>= '0' && *desc
<= '9')
707 else if (*desc
>= 'a' && *desc
<= 'f')
709 else if (*desc
>= 'A' && *desc
<= 'F')
712 return "Game description shorter than expected";
714 return "Game description contained unexpected character";
716 while (*desc
== 'h' || *desc
== 'v')
720 return "Game description longer than expected";
725 /* ----------------------------------------------------------------------
726 * Construct an initial game state, given a description and parameters.
729 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
734 assert(params
->width
> 0 && params
->height
> 0);
735 assert(params
->width
> 1 || params
->height
> 1);
738 * Create a blank game state.
740 state
= snew(game_state
);
741 w
= state
->width
= params
->width
;
742 h
= state
->height
= params
->height
;
743 state
->cx
= state
->width
/ 2;
744 state
->cy
= state
->height
/ 2;
745 state
->wrapping
= params
->wrapping
;
746 state
->movetarget
= params
->movetarget
;
747 state
->completed
= 0;
748 state
->used_solve
= FALSE
;
749 state
->move_count
= 0;
750 state
->last_move_row
= -1;
751 state
->last_move_col
= -1;
752 state
->last_move_dir
= 0;
753 state
->tiles
= snewn(state
->width
* state
->height
, unsigned char);
754 memset(state
->tiles
, 0, state
->width
* state
->height
);
755 state
->barriers
= snewn(state
->width
* state
->height
, unsigned char);
756 memset(state
->barriers
, 0, state
->width
* state
->height
);
760 * Parse the game description into the grid.
762 for (y
= 0; y
< h
; y
++) {
763 for (x
= 0; x
< w
; x
++) {
764 if (*desc
>= '0' && *desc
<= '9')
765 tile(state
, x
, y
) = *desc
- '0';
766 else if (*desc
>= 'a' && *desc
<= 'f')
767 tile(state
, x
, y
) = *desc
- 'a' + 10;
768 else if (*desc
>= 'A' && *desc
<= 'F')
769 tile(state
, x
, y
) = *desc
- 'A' + 10;
772 while (*desc
== 'h' || *desc
== 'v') {
779 OFFSET(x2
, y2
, x
, y
, d1
, state
);
782 barrier(state
, x
, y
) |= d1
;
783 barrier(state
, x2
, y2
) |= d2
;
791 * Set up border barriers if this is a non-wrapping game.
793 if (!state
->wrapping
) {
794 for (x
= 0; x
< state
->width
; x
++) {
795 barrier(state
, x
, 0) |= U
;
796 barrier(state
, x
, state
->height
-1) |= D
;
798 for (y
= 0; y
< state
->height
; y
++) {
799 barrier(state
, 0, y
) |= L
;
800 barrier(state
, state
->width
-1, y
) |= R
;
805 * Set up the barrier corner flags, for drawing barriers
806 * prettily when they meet.
808 for (y
= 0; y
< state
->height
; y
++) {
809 for (x
= 0; x
< state
->width
; x
++) {
812 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
814 int x1
, y1
, x2
, y2
, x3
, y3
;
817 if (!(barrier(state
, x
, y
) & dir
))
820 if (barrier(state
, x
, y
) & dir2
)
823 x1
= x
+ X(dir
), y1
= y
+ Y(dir
);
824 if (x1
>= 0 && x1
< state
->width
&&
825 y1
>= 0 && y1
< state
->height
&&
826 (barrier(state
, x1
, y1
) & dir2
))
829 x2
= x
+ X(dir2
), y2
= y
+ Y(dir2
);
830 if (x2
>= 0 && x2
< state
->width
&&
831 y2
>= 0 && y2
< state
->height
&&
832 (barrier(state
, x2
, y2
) & dir
))
836 barrier(state
, x
, y
) |= (dir
<< 4);
837 if (x1
>= 0 && x1
< state
->width
&&
838 y1
>= 0 && y1
< state
->height
)
839 barrier(state
, x1
, y1
) |= (A(dir
) << 4);
840 if (x2
>= 0 && x2
< state
->width
&&
841 y2
>= 0 && y2
< state
->height
)
842 barrier(state
, x2
, y2
) |= (C(dir
) << 4);
843 x3
= x
+ X(dir
) + X(dir2
), y3
= y
+ Y(dir
) + Y(dir2
);
844 if (x3
>= 0 && x3
< state
->width
&&
845 y3
>= 0 && y3
< state
->height
)
846 barrier(state
, x3
, y3
) |= (F(dir
) << 4);
855 static game_state
*dup_game(game_state
*state
)
859 ret
= snew(game_state
);
860 ret
->width
= state
->width
;
861 ret
->height
= state
->height
;
864 ret
->wrapping
= state
->wrapping
;
865 ret
->movetarget
= state
->movetarget
;
866 ret
->completed
= state
->completed
;
867 ret
->used_solve
= state
->used_solve
;
868 ret
->move_count
= state
->move_count
;
869 ret
->last_move_row
= state
->last_move_row
;
870 ret
->last_move_col
= state
->last_move_col
;
871 ret
->last_move_dir
= state
->last_move_dir
;
872 ret
->tiles
= snewn(state
->width
* state
->height
, unsigned char);
873 memcpy(ret
->tiles
, state
->tiles
, state
->width
* state
->height
);
874 ret
->barriers
= snewn(state
->width
* state
->height
, unsigned char);
875 memcpy(ret
->barriers
, state
->barriers
, state
->width
* state
->height
);
880 static void free_game(game_state
*state
)
883 sfree(state
->barriers
);
887 static char *solve_game(game_state
*state
, game_state
*currstate
,
888 char *aux
, char **error
)
891 *error
= "Solution not known for this puzzle";
898 static char *game_text_format(game_state
*state
)
903 /* ----------------------------------------------------------------------
908 * Compute which squares are reachable from the centre square, as a
909 * quick visual aid to determining how close the game is to
910 * completion. This is also a simple way to tell if the game _is_
911 * completed - just call this function and see whether every square
914 * squares in the moving_row and moving_col are always inactive - this
915 * is so that "current" doesn't appear to jump across moving lines.
917 static unsigned char *compute_active(game_state
*state
,
918 int moving_row
, int moving_col
)
920 unsigned char *active
;
924 active
= snewn(state
->width
* state
->height
, unsigned char);
925 memset(active
, 0, state
->width
* state
->height
);
928 * We only store (x,y) pairs in todo, but it's easier to reuse
929 * xyd_cmp and just store direction 0 every time.
931 todo
= newtree234(xyd_cmp
);
932 index(state
, active
, state
->cx
, state
->cy
) = ACTIVE
;
933 add234(todo
, new_xyd(state
->cx
, state
->cy
, 0));
935 while ( (xyd
= delpos234(todo
, 0)) != NULL
) {
936 int x1
, y1
, d1
, x2
, y2
, d2
;
942 for (d1
= 1; d1
< 0x10; d1
<<= 1) {
943 OFFSET(x2
, y2
, x1
, y1
, d1
, state
);
947 * If the next tile in this direction is connected to
948 * us, and there isn't a barrier in the way, and it
949 * isn't already marked active, then mark it active and
950 * add it to the to-examine list.
952 if ((x2
!= moving_col
&& y2
!= moving_row
) &&
953 (tile(state
, x1
, y1
) & d1
) &&
954 (tile(state
, x2
, y2
) & d2
) &&
955 !(barrier(state
, x1
, y1
) & d1
) &&
956 !index(state
, active
, x2
, y2
)) {
957 index(state
, active
, x2
, y2
) = ACTIVE
;
958 add234(todo
, new_xyd(x2
, y2
, 0));
962 /* Now we expect the todo list to have shrunk to zero size. */
963 assert(count234(todo
) == 0);
974 static game_ui
*new_ui(game_state
*state
)
976 game_ui
*ui
= snew(game_ui
);
977 ui
->cur_x
= state
->width
/ 2;
978 ui
->cur_y
= state
->height
/ 2;
979 ui
->cur_visible
= FALSE
;
984 static void free_ui(game_ui
*ui
)
989 static char *encode_ui(game_ui
*ui
)
994 static void decode_ui(game_ui
*ui
, char *encoding
)
998 /* ----------------------------------------------------------------------
1002 static void slide_row_int(int w
, int h
, unsigned char *tiles
, int dir
, int row
)
1004 int x
= dir
> 0 ?
-1 : w
;
1007 unsigned char endtile
= tiles
[row
* w
+ tx
];
1010 tx
= (x
+ dir
+ w
) % w
;
1011 tiles
[row
* w
+ x
] = tiles
[row
* w
+ tx
];
1013 tiles
[row
* w
+ tx
] = endtile
;
1016 static void slide_col_int(int w
, int h
, unsigned char *tiles
, int dir
, int col
)
1018 int y
= dir
> 0 ?
-1 : h
;
1021 unsigned char endtile
= tiles
[ty
* w
+ col
];
1024 ty
= (y
+ dir
+ h
) % h
;
1025 tiles
[y
* w
+ col
] = tiles
[ty
* w
+ col
];
1027 tiles
[ty
* w
+ col
] = endtile
;
1030 static void slide_row(game_state
*state
, int dir
, int row
)
1032 slide_row_int(state
->width
, state
->height
, state
->tiles
, dir
, row
);
1035 static void slide_col(game_state
*state
, int dir
, int col
)
1037 slide_col_int(state
->width
, state
->height
, state
->tiles
, dir
, col
);
1040 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
1041 game_state
*newstate
)
1045 struct game_drawstate
{
1049 unsigned char *visible
;
1052 static char *interpret_move(game_state
*state
, game_ui
*ui
,
1053 game_drawstate
*ds
, int x
, int y
, int button
)
1059 button
&= ~MOD_MASK
;
1061 if (button
!= LEFT_BUTTON
&& button
!= RIGHT_BUTTON
)
1064 cx
= (x
- (BORDER
+ WINDOW_OFFSET
+ TILE_BORDER
) + 2*TILE_SIZE
) / TILE_SIZE
- 2;
1065 cy
= (y
- (BORDER
+ WINDOW_OFFSET
+ TILE_BORDER
) + 2*TILE_SIZE
) / TILE_SIZE
- 2;
1067 if (cy
>= 0 && cy
< state
->height
&& cy
!= state
->cy
)
1069 if (cx
== -1) dx
= +1;
1070 else if (cx
== state
->width
) dx
= -1;
1075 else if (cx
>= 0 && cx
< state
->width
&& cx
!= state
->cx
)
1077 if (cy
== -1) dy
= +1;
1078 else if (cy
== state
->height
) dy
= -1;
1086 /* reverse direction if right hand button is pressed */
1087 if (button
== RIGHT_BUTTON
)
1094 sprintf(buf
, "C%d,%d", cx
, dy
);
1096 sprintf(buf
, "R%d,%d", cy
, dx
);
1100 static game_state
*execute_move(game_state
*from
, char *move
)
1105 if ((move
[0] == 'C' || move
[0] == 'R') &&
1106 sscanf(move
+1, "%d,%d", &c
, &d
) == 2 &&
1107 c
>= 0 && c
< (move
[0] == 'C' ? from
->width
: from
->height
)) {
1108 col
= (move
[0] == 'C');
1109 } else if (move
[0] == 'S' &&
1110 strlen(move
) == from
->width
* from
->height
+ 1) {
1112 ret
= dup_game(from
);
1113 ret
->used_solve
= TRUE
;
1114 ret
->completed
= ret
->move_count
= 1;
1116 for (i
= 0; i
< from
->width
* from
->height
; i
++) {
1118 if (c
>= '0' && c
<= '9')
1120 else if (c
>= 'A' && c
<= 'F')
1122 else if (c
>= 'a' && c
<= 'f')
1132 return NULL
; /* can't parse move string */
1134 ret
= dup_game(from
);
1137 slide_col(ret
, d
, c
);
1139 slide_row(ret
, d
, c
);
1142 ret
->last_move_row
= col ?
-1 : c
;
1143 ret
->last_move_col
= col ? c
: -1;
1144 ret
->last_move_dir
= d
;
1147 * See if the game has been completed.
1149 if (!ret
->completed
) {
1150 unsigned char *active
= compute_active(ret
, -1, -1);
1152 int complete
= TRUE
;
1154 for (x1
= 0; x1
< ret
->width
; x1
++)
1155 for (y1
= 0; y1
< ret
->height
; y1
++)
1156 if (!index(ret
, active
, x1
, y1
)) {
1158 goto break_label
; /* break out of two loops at once */
1165 ret
->completed
= ret
->move_count
;
1171 /* ----------------------------------------------------------------------
1172 * Routines for drawing the game position on the screen.
1175 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
1177 game_drawstate
*ds
= snew(game_drawstate
);
1179 ds
->started
= FALSE
;
1180 ds
->width
= state
->width
;
1181 ds
->height
= state
->height
;
1182 ds
->visible
= snewn(state
->width
* state
->height
, unsigned char);
1183 ds
->tilesize
= 0; /* not decided yet */
1184 memset(ds
->visible
, 0xFF, state
->width
* state
->height
);
1189 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
1195 static void game_compute_size(game_params
*params
, int tilesize
,
1198 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1199 struct { int tilesize
; } ads
, *ds
= &ads
;
1200 ads
.tilesize
= tilesize
;
1202 *x
= BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* params
->width
+ TILE_BORDER
;
1203 *y
= BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* params
->height
+ TILE_BORDER
;
1206 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
1207 game_params
*params
, int tilesize
)
1209 ds
->tilesize
= tilesize
;
1212 static float *game_colours(frontend
*fe
, int *ncolours
)
1216 ret
= snewn(NCOLOURS
* 3, float);
1217 *ncolours
= NCOLOURS
;
1220 * Basic background colour is whatever the front end thinks is
1221 * a sensible default.
1223 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1228 ret
[COL_WIRE
* 3 + 0] = 0.0F
;
1229 ret
[COL_WIRE
* 3 + 1] = 0.0F
;
1230 ret
[COL_WIRE
* 3 + 2] = 0.0F
;
1233 * Powered wires and powered endpoints are cyan.
1235 ret
[COL_POWERED
* 3 + 0] = 0.0F
;
1236 ret
[COL_POWERED
* 3 + 1] = 1.0F
;
1237 ret
[COL_POWERED
* 3 + 2] = 1.0F
;
1242 ret
[COL_BARRIER
* 3 + 0] = 1.0F
;
1243 ret
[COL_BARRIER
* 3 + 1] = 0.0F
;
1244 ret
[COL_BARRIER
* 3 + 2] = 0.0F
;
1247 * Unpowered endpoints are blue.
1249 ret
[COL_ENDPOINT
* 3 + 0] = 0.0F
;
1250 ret
[COL_ENDPOINT
* 3 + 1] = 0.0F
;
1251 ret
[COL_ENDPOINT
* 3 + 2] = 1.0F
;
1254 * Tile borders are a darker grey than the background.
1256 ret
[COL_BORDER
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
1257 ret
[COL_BORDER
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
1258 ret
[COL_BORDER
* 3 + 2] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 2];
1261 * Flashing tiles are a grey in between those two.
1263 ret
[COL_FLASHING
* 3 + 0] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 0];
1264 ret
[COL_FLASHING
* 3 + 1] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 1];
1265 ret
[COL_FLASHING
* 3 + 2] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 2];
1267 ret
[COL_LOWLIGHT
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 0.8F
;
1268 ret
[COL_LOWLIGHT
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 0.8F
;
1269 ret
[COL_LOWLIGHT
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 0.8F
;
1270 ret
[COL_TEXT
* 3 + 0] = 0.0;
1271 ret
[COL_TEXT
* 3 + 1] = 0.0;
1272 ret
[COL_TEXT
* 3 + 2] = 0.0;
1277 static void draw_thick_line(drawing
*dr
, int x1
, int y1
, int x2
, int y2
,
1280 draw_line(dr
, x1
-1, y1
, x2
-1, y2
, COL_WIRE
);
1281 draw_line(dr
, x1
+1, y1
, x2
+1, y2
, COL_WIRE
);
1282 draw_line(dr
, x1
, y1
-1, x2
, y2
-1, COL_WIRE
);
1283 draw_line(dr
, x1
, y1
+1, x2
, y2
+1, COL_WIRE
);
1284 draw_line(dr
, x1
, y1
, x2
, y2
, colour
);
1287 static void draw_rect_coords(drawing
*dr
, int x1
, int y1
, int x2
, int y2
,
1290 int mx
= (x1
< x2 ? x1
: x2
);
1291 int my
= (y1
< y2 ? y1
: y2
);
1292 int dx
= (x2
+ x1
- 2*mx
+ 1);
1293 int dy
= (y2
+ y1
- 2*my
+ 1);
1295 draw_rect(dr
, mx
, my
, dx
, dy
, colour
);
1298 static void draw_barrier_corner(drawing
*dr
, game_drawstate
*ds
,
1299 int x
, int y
, int dir
, int phase
)
1301 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
;
1302 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
;
1303 int x1
, y1
, dx
, dy
, dir2
;
1308 dx
= X(dir
) + X(dir2
);
1309 dy
= Y(dir
) + Y(dir2
);
1310 x1
= (dx
> 0 ? TILE_SIZE
+TILE_BORDER
-1 : 0);
1311 y1
= (dy
> 0 ? TILE_SIZE
+TILE_BORDER
-1 : 0);
1314 draw_rect_coords(dr
, bx
+x1
, by
+y1
,
1315 bx
+x1
-TILE_BORDER
*dx
, by
+y1
-(TILE_BORDER
-1)*dy
,
1317 draw_rect_coords(dr
, bx
+x1
, by
+y1
,
1318 bx
+x1
-(TILE_BORDER
-1)*dx
, by
+y1
-TILE_BORDER
*dy
,
1321 draw_rect_coords(dr
, bx
+x1
, by
+y1
,
1322 bx
+x1
-(TILE_BORDER
-1)*dx
, by
+y1
-(TILE_BORDER
-1)*dy
,
1327 static void draw_barrier(drawing
*dr
, game_drawstate
*ds
,
1328 int x
, int y
, int dir
, int phase
)
1330 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
;
1331 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
;
1334 x1
= (X(dir
) > 0 ? TILE_SIZE
: X(dir
) == 0 ? TILE_BORDER
: 0);
1335 y1
= (Y(dir
) > 0 ? TILE_SIZE
: Y(dir
) == 0 ? TILE_BORDER
: 0);
1336 w
= (X(dir
) ? TILE_BORDER
: TILE_SIZE
- TILE_BORDER
);
1337 h
= (Y(dir
) ? TILE_BORDER
: TILE_SIZE
- TILE_BORDER
);
1340 draw_rect(dr
, bx
+x1
-X(dir
), by
+y1
-Y(dir
), w
, h
, COL_WIRE
);
1342 draw_rect(dr
, bx
+x1
, by
+y1
, w
, h
, COL_BARRIER
);
1346 static void draw_tile(drawing
*dr
, game_drawstate
*ds
, game_state
*state
,
1347 int x
, int y
, int tile
, float xshift
, float yshift
)
1349 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
+ (xshift
* TILE_SIZE
);
1350 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
+ (yshift
* TILE_SIZE
);
1351 float cx
, cy
, ex
, ey
;
1355 * When we draw a single tile, we must draw everything up to
1356 * and including the borders around the tile. This means that
1357 * if the neighbouring tiles have connections to those borders,
1358 * we must draw those connections on the borders themselves.
1360 * This would be terribly fiddly if we ever had to draw a tile
1361 * while its neighbour was in mid-rotate, because we'd have to
1362 * arrange to _know_ that the neighbour was being rotated and
1363 * hence had an anomalous effect on the redraw of this tile.
1364 * Fortunately, the drawing algorithm avoids ever calling us in
1365 * this circumstance: we're either drawing lots of straight
1366 * tiles at game start or after a move is complete, or we're
1367 * repeatedly drawing only the rotating tile. So no problem.
1371 * So. First blank the tile out completely: draw a big
1372 * rectangle in border colour, and a smaller rectangle in
1373 * background colour to fill it in.
1375 draw_rect(dr
, bx
, by
, TILE_SIZE
+TILE_BORDER
, TILE_SIZE
+TILE_BORDER
,
1377 draw_rect(dr
, bx
+TILE_BORDER
, by
+TILE_BORDER
,
1378 TILE_SIZE
-TILE_BORDER
, TILE_SIZE
-TILE_BORDER
,
1379 tile
& FLASHING ? COL_FLASHING
: COL_BACKGROUND
);
1384 cx
= cy
= TILE_BORDER
+ (TILE_SIZE
-TILE_BORDER
) / 2.0F
- 0.5F
;
1385 col
= (tile
& ACTIVE ? COL_POWERED
: COL_WIRE
);
1386 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
1388 ex
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* X(dir
);
1389 ey
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* Y(dir
);
1390 draw_thick_line(dr
, bx
+(int)cx
, by
+(int)cy
,
1391 bx
+(int)(cx
+ex
), by
+(int)(cy
+ey
),
1395 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
1397 ex
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* X(dir
);
1398 ey
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* Y(dir
);
1399 draw_line(dr
, bx
+(int)cx
, by
+(int)cy
,
1400 bx
+(int)(cx
+ex
), by
+(int)(cy
+ey
), col
);
1405 * Draw the box in the middle. We do this in blue if the tile
1406 * is an unpowered endpoint, in cyan if the tile is a powered
1407 * endpoint, in black if the tile is the centrepiece, and
1408 * otherwise not at all.
1411 if (x
== state
->cx
&& y
== state
->cy
)
1413 else if (COUNT(tile
) == 1) {
1414 col
= (tile
& ACTIVE ? COL_POWERED
: COL_ENDPOINT
);
1419 points
[0] = +1; points
[1] = +1;
1420 points
[2] = +1; points
[3] = -1;
1421 points
[4] = -1; points
[5] = -1;
1422 points
[6] = -1; points
[7] = +1;
1424 for (i
= 0; i
< 8; i
+= 2) {
1425 ex
= (TILE_SIZE
* 0.24F
) * points
[i
];
1426 ey
= (TILE_SIZE
* 0.24F
) * points
[i
+1];
1427 points
[i
] = bx
+(int)(cx
+ex
);
1428 points
[i
+1] = by
+(int)(cy
+ey
);
1431 draw_polygon(dr
, points
, 4, col
, COL_WIRE
);
1435 * Draw the points on the border if other tiles are connected
1438 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
1439 int dx
, dy
, px
, py
, lx
, ly
, vx
, vy
, ox
, oy
;
1447 if (ox
< 0 || ox
>= state
->width
|| oy
< 0 || oy
>= state
->height
)
1450 if (!(tile(state
, ox
, oy
) & F(dir
)))
1453 px
= bx
+ (int)(dx
>0 ? TILE_SIZE
+ TILE_BORDER
- 1 : dx
<0 ?
0 : cx
);
1454 py
= by
+ (int)(dy
>0 ? TILE_SIZE
+ TILE_BORDER
- 1 : dy
<0 ?
0 : cy
);
1455 lx
= dx
* (TILE_BORDER
-1);
1456 ly
= dy
* (TILE_BORDER
-1);
1460 if (xshift
== 0.0 && yshift
== 0.0 && (tile
& dir
)) {
1462 * If we are fully connected to the other tile, we must
1463 * draw right across the tile border. (We can use our
1464 * own ACTIVE state to determine what colour to do this
1465 * in: if we are fully connected to the other tile then
1466 * the two ACTIVE states will be the same.)
1468 draw_rect_coords(dr
, px
-vx
, py
-vy
, px
+lx
+vx
, py
+ly
+vy
, COL_WIRE
);
1469 draw_rect_coords(dr
, px
, py
, px
+lx
, py
+ly
,
1470 (tile
& ACTIVE
) ? COL_POWERED
: COL_WIRE
);
1473 * The other tile extends into our border, but isn't
1474 * actually connected to us. Just draw a single black
1477 draw_rect_coords(dr
, px
, py
, px
, py
, COL_WIRE
);
1481 draw_update(dr
, bx
, by
, TILE_SIZE
+TILE_BORDER
, TILE_SIZE
+TILE_BORDER
);
1484 static void draw_tile_barriers(drawing
*dr
, game_drawstate
*ds
,
1485 game_state
*state
, int x
, int y
)
1489 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
;
1490 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
;
1492 * Draw barrier corners, and then barriers.
1494 for (phase
= 0; phase
< 2; phase
++) {
1495 for (dir
= 1; dir
< 0x10; dir
<<= 1)
1496 if (barrier(state
, x
, y
) & (dir
<< 4))
1497 draw_barrier_corner(dr
, ds
, x
, y
, dir
<< 4, phase
);
1498 for (dir
= 1; dir
< 0x10; dir
<<= 1)
1499 if (barrier(state
, x
, y
) & dir
)
1500 draw_barrier(dr
, ds
, x
, y
, dir
, phase
);
1503 draw_update(dr
, bx
, by
, TILE_SIZE
+TILE_BORDER
, TILE_SIZE
+TILE_BORDER
);
1506 static void draw_arrow(drawing
*dr
, game_drawstate
*ds
,
1507 int x
, int y
, int xdx
, int xdy
)
1510 int ydy
= -xdx
, ydx
= xdy
;
1512 x
= x
* TILE_SIZE
+ BORDER
+ WINDOW_OFFSET
;
1513 y
= y
* TILE_SIZE
+ BORDER
+ WINDOW_OFFSET
;
1515 #define POINT(n, xx, yy) ( \
1516 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
1517 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
1519 POINT(0, TILE_SIZE
/ 2, 3 * TILE_SIZE
/ 4); /* top of arrow */
1520 POINT(1, 3 * TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* right corner */
1521 POINT(2, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* right concave */
1522 POINT(3, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom right */
1523 POINT(4, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom left */
1524 POINT(5, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* left concave */
1525 POINT(6, TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* left corner */
1527 draw_polygon(dr
, coords
, 7, COL_LOWLIGHT
, COL_TEXT
);
1530 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
1531 game_state
*state
, int dir
, game_ui
*ui
, float t
, float ft
)
1533 int x
, y
, tx
, ty
, frame
;
1534 unsigned char *active
;
1539 * Clear the screen and draw the exterior barrier lines if this
1540 * is our first call.
1548 BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* state
->width
+ TILE_BORDER
,
1549 BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* state
->height
+ TILE_BORDER
,
1551 draw_update(dr
, 0, 0,
1552 BORDER
* 2 + WINDOW_OFFSET
*2 + TILE_SIZE
*state
->width
+ TILE_BORDER
,
1553 BORDER
* 2 + WINDOW_OFFSET
*2 + TILE_SIZE
*state
->height
+ TILE_BORDER
);
1555 for (phase
= 0; phase
< 2; phase
++) {
1557 for (x
= 0; x
< ds
->width
; x
++) {
1558 if (barrier(state
, x
, 0) & UL
)
1559 draw_barrier_corner(dr
, ds
, x
, -1, LD
, phase
);
1560 if (barrier(state
, x
, 0) & RU
)
1561 draw_barrier_corner(dr
, ds
, x
, -1, DR
, phase
);
1562 if (barrier(state
, x
, 0) & U
)
1563 draw_barrier(dr
, ds
, x
, -1, D
, phase
);
1564 if (barrier(state
, x
, ds
->height
-1) & DR
)
1565 draw_barrier_corner(dr
, ds
, x
, ds
->height
, RU
, phase
);
1566 if (barrier(state
, x
, ds
->height
-1) & LD
)
1567 draw_barrier_corner(dr
, ds
, x
, ds
->height
, UL
, phase
);
1568 if (barrier(state
, x
, ds
->height
-1) & D
)
1569 draw_barrier(dr
, ds
, x
, ds
->height
, U
, phase
);
1572 for (y
= 0; y
< ds
->height
; y
++) {
1573 if (barrier(state
, 0, y
) & UL
)
1574 draw_barrier_corner(dr
, ds
, -1, y
, RU
, phase
);
1575 if (barrier(state
, 0, y
) & LD
)
1576 draw_barrier_corner(dr
, ds
, -1, y
, DR
, phase
);
1577 if (barrier(state
, 0, y
) & L
)
1578 draw_barrier(dr
, ds
, -1, y
, R
, phase
);
1579 if (barrier(state
, ds
->width
-1, y
) & RU
)
1580 draw_barrier_corner(dr
, ds
, ds
->width
, y
, UL
, phase
);
1581 if (barrier(state
, ds
->width
-1, y
) & DR
)
1582 draw_barrier_corner(dr
, ds
, ds
->width
, y
, LD
, phase
);
1583 if (barrier(state
, ds
->width
-1, y
) & R
)
1584 draw_barrier(dr
, ds
, ds
->width
, y
, L
, phase
);
1589 * Arrows for making moves.
1591 for (x
= 0; x
< ds
->width
; x
++) {
1592 if (x
== state
->cx
) continue;
1593 draw_arrow(dr
, ds
, x
, 0, +1, 0);
1594 draw_arrow(dr
, ds
, x
+1, ds
->height
, -1, 0);
1596 for (y
= 0; y
< ds
->height
; y
++) {
1597 if (y
== state
->cy
) continue;
1598 draw_arrow(dr
, ds
, ds
->width
, y
, 0, +1);
1599 draw_arrow(dr
, ds
, 0, y
+1, 0, -1);
1603 /* Check if this is an undo. If so, we will need to run any animation
1606 if (oldstate
&& oldstate
->move_count
> state
->move_count
) {
1607 game_state
* tmpstate
= state
;
1609 oldstate
= tmpstate
;
1614 if (oldstate
&& (t
< ANIM_TIME
)) {
1616 * We're animating a slide, of row/column number
1617 * state->last_move_pos, in direction
1618 * state->last_move_dir
1620 xshift
= state
->last_move_row
== -1 ?
0.0 :
1621 (1 - t
/ ANIM_TIME
) * state
->last_move_dir
;
1622 yshift
= state
->last_move_col
== -1 ?
0.0 :
1623 (1 - t
/ ANIM_TIME
) * state
->last_move_dir
;
1629 * We're animating a completion flash. Find which frame
1632 frame
= (int)(ft
/ FLASH_FRAME
);
1636 * Draw any tile which differs from the way it was last drawn.
1638 if (xshift
!= 0.0 || yshift
!= 0.0) {
1639 active
= compute_active(state
,
1640 state
->last_move_row
, state
->last_move_col
);
1642 active
= compute_active(state
, -1, -1);
1646 BORDER
+ WINDOW_OFFSET
, BORDER
+ WINDOW_OFFSET
,
1647 TILE_SIZE
* state
->width
+ TILE_BORDER
,
1648 TILE_SIZE
* state
->height
+ TILE_BORDER
);
1650 for (x
= 0; x
< ds
->width
; x
++)
1651 for (y
= 0; y
< ds
->height
; y
++) {
1652 unsigned char c
= tile(state
, x
, y
) | index(state
, active
, x
, y
);
1655 * In a completion flash, we adjust the FLASHING bit
1656 * depending on our distance from the centre point and
1660 int xdist
, ydist
, dist
;
1661 xdist
= (x
< state
->cx ? state
->cx
- x
: x
- state
->cx
);
1662 ydist
= (y
< state
->cy ? state
->cy
- y
: y
- state
->cy
);
1663 dist
= (xdist
> ydist ? xdist
: ydist
);
1665 if (frame
>= dist
&& frame
< dist
+4) {
1666 int flash
= (frame
- dist
) & 1;
1667 flash
= flash ? FLASHING
: 0;
1668 c
= (c
&~ FLASHING
) | flash
;
1672 if (index(state
, ds
->visible
, x
, y
) != c
||
1673 index(state
, ds
->visible
, x
, y
) == 0xFF ||
1674 (x
== state
->last_move_col
|| y
== state
->last_move_row
))
1676 float xs
= (y
== state
->last_move_row ? xshift
: 0.0);
1677 float ys
= (x
== state
->last_move_col ? yshift
: 0.0);
1679 draw_tile(dr
, ds
, state
, x
, y
, c
, xs
, ys
);
1680 if (xs
< 0 && x
== 0)
1681 draw_tile(dr
, ds
, state
, state
->width
, y
, c
, xs
, ys
);
1682 else if (xs
> 0 && x
== state
->width
- 1)
1683 draw_tile(dr
, ds
, state
, -1, y
, c
, xs
, ys
);
1684 else if (ys
< 0 && y
== 0)
1685 draw_tile(dr
, ds
, state
, x
, state
->height
, c
, xs
, ys
);
1686 else if (ys
> 0 && y
== state
->height
- 1)
1687 draw_tile(dr
, ds
, state
, x
, -1, c
, xs
, ys
);
1689 if (x
== state
->last_move_col
|| y
== state
->last_move_row
)
1690 index(state
, ds
->visible
, x
, y
) = 0xFF;
1692 index(state
, ds
->visible
, x
, y
) = c
;
1696 for (x
= 0; x
< ds
->width
; x
++)
1697 for (y
= 0; y
< ds
->height
; y
++)
1698 draw_tile_barriers(dr
, ds
, state
, x
, y
);
1703 * Update the status bar.
1706 char statusbuf
[256];
1709 n
= state
->width
* state
->height
;
1710 for (i
= a
= 0; i
< n
; i
++)
1714 if (state
->used_solve
)
1715 sprintf(statusbuf
, "Moves since auto-solve: %d",
1716 state
->move_count
- state
->completed
);
1718 sprintf(statusbuf
, "%sMoves: %d",
1719 (state
->completed ?
"COMPLETED! " : ""),
1720 (state
->completed ? state
->completed
: state
->move_count
));
1722 if (state
->movetarget
)
1723 sprintf(statusbuf
+ strlen(statusbuf
), " (target %d)",
1726 sprintf(statusbuf
+ strlen(statusbuf
), " Active: %d/%d", a
, n
);
1728 status_bar(dr
, statusbuf
);
1734 static float game_anim_length(game_state
*oldstate
,
1735 game_state
*newstate
, int dir
, game_ui
*ui
)
1740 static float game_flash_length(game_state
*oldstate
,
1741 game_state
*newstate
, int dir
, game_ui
*ui
)
1744 * If the game has just been completed, we display a completion
1747 if (!oldstate
->completed
&& newstate
->completed
&&
1748 !oldstate
->used_solve
&& !newstate
->used_solve
) {
1751 if (size
< newstate
->cx
+1)
1752 size
= newstate
->cx
+1;
1753 if (size
< newstate
->cy
+1)
1754 size
= newstate
->cy
+1;
1755 if (size
< newstate
->width
- newstate
->cx
)
1756 size
= newstate
->width
- newstate
->cx
;
1757 if (size
< newstate
->height
- newstate
->cy
)
1758 size
= newstate
->height
- newstate
->cy
;
1759 return FLASH_FRAME
* (size
+4);
1765 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1770 static void game_print_size(game_params
*params
, float *x
, float *y
)
1774 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
1779 #define thegame netslide
1782 const struct game thegame
= {
1783 "Netslide", "games.netslide",
1790 TRUE
, game_configure
, custom_params
,
1798 FALSE
, game_text_format
,
1806 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
1809 game_free_drawstate
,
1813 FALSE
, FALSE
, game_print_size
, game_print
,
1814 TRUE
, /* wants_statusbar */
1815 FALSE
, game_timing_state
,