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) )
56 #define BORDER TILE_SIZE
58 #define WINDOW_OFFSET 0
60 #define ANIM_TIME 0.13F
61 #define FLASH_FRAME 0.07F
80 float barrier_probability
;
84 struct game_aux_info
{
90 int width
, height
, cx
, cy
, wrapping
, completed
;
91 int used_solve
, just_used_solve
;
92 int move_count
, movetarget
;
94 /* position (row or col number, starting at 0) of last move. */
95 int last_move_row
, last_move_col
;
97 /* direction of last move: +1 or -1 */
100 unsigned char *tiles
;
101 unsigned char *barriers
;
104 #define OFFSET(x2,y2,x1,y1,dir,state) \
105 ( (x2) = ((x1) + (state)->width + X((dir))) % (state)->width, \
106 (y2) = ((y1) + (state)->height + Y((dir))) % (state)->height)
108 #define index(state, a, x, y) ( a[(y) * (state)->width + (x)] )
109 #define tile(state, x, y) index(state, (state)->tiles, x, y)
110 #define barrier(state, x, y) index(state, (state)->barriers, x, y)
116 static int xyd_cmp(void *av
, void *bv
) {
117 struct xyd
*a
= (struct xyd
*)av
;
118 struct xyd
*b
= (struct xyd
*)bv
;
127 if (a
->direction
< b
->direction
)
129 if (a
->direction
> b
->direction
)
134 static struct xyd
*new_xyd(int x
, int y
, int direction
)
136 struct xyd
*xyd
= snew(struct xyd
);
139 xyd
->direction
= direction
;
143 static void slide_col(game_state
*state
, int dir
, int col
);
144 static void slide_col_int(int w
, int h
, unsigned char *tiles
, int dir
, int col
);
145 static void slide_row(game_state
*state
, int dir
, int row
);
146 static void slide_row_int(int w
, int h
, unsigned char *tiles
, int dir
, int row
);
148 /* ----------------------------------------------------------------------
149 * Manage game parameters.
151 static game_params
*default_params(void)
153 game_params
*ret
= snew(game_params
);
157 ret
->wrapping
= FALSE
;
158 ret
->barrier_probability
= 1.0;
164 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
168 static const struct { int x
, y
, wrap
, bprob
; const char* desc
; } values
[] = {
169 {3, 3, FALSE
, 1.0, " easy"},
170 {3, 3, FALSE
, 0.0, " medium"},
171 {3, 3, TRUE
, 0.0, " hard"},
172 {4, 4, FALSE
, 1.0, " easy"},
173 {4, 4, FALSE
, 0.0, " medium"},
174 {4, 4, TRUE
, 0.0, " hard"},
175 {5, 5, FALSE
, 1.0, " easy"},
176 {5, 5, FALSE
, 0.0, " medium"},
177 {5, 5, TRUE
, 0.0, " hard"},
180 if (i
< 0 || i
>= lenof(values
))
183 ret
= snew(game_params
);
184 ret
->width
= values
[i
].x
;
185 ret
->height
= values
[i
].y
;
186 ret
->wrapping
= values
[i
].wrap
;
187 ret
->barrier_probability
= values
[i
].bprob
;
190 sprintf(str
, "%dx%d%s", ret
->width
, ret
->height
,
198 static void free_params(game_params
*params
)
203 static game_params
*dup_params(game_params
*params
)
205 game_params
*ret
= snew(game_params
);
206 *ret
= *params
; /* structure copy */
210 static void decode_params(game_params
*ret
, char const *string
)
212 char const *p
= string
;
214 ret
->wrapping
= FALSE
;
215 ret
->barrier_probability
= 0.0;
218 ret
->width
= atoi(p
);
219 while (*p
&& isdigit(*p
)) p
++;
222 ret
->height
= atoi(p
);
223 while (*p
&& isdigit(*p
)) p
++;
224 if ( (ret
->wrapping
= (*p
== 'w')) != 0 )
227 ret
->barrier_probability
= atof(++p
);
228 while (*p
&& (isdigit(*p
) || *p
== '.')) p
++;
231 ret
->movetarget
= atoi(++p
);
234 ret
->height
= ret
->width
;
238 static char *encode_params(game_params
*params
, int full
)
243 len
= sprintf(ret
, "%dx%d", params
->width
, params
->height
);
244 if (params
->wrapping
)
246 if (full
&& params
->barrier_probability
)
247 len
+= sprintf(ret
+len
, "b%g", params
->barrier_probability
);
248 /* Shuffle limit is part of the limited parameters, because we have to
249 * provide the target move count. */
250 if (params
->movetarget
)
251 len
+= sprintf(ret
+len
, "m%d", params
->movetarget
);
252 assert(len
< lenof(ret
));
258 static config_item
*game_configure(game_params
*params
)
263 ret
= snewn(6, config_item
);
265 ret
[0].name
= "Width";
266 ret
[0].type
= C_STRING
;
267 sprintf(buf
, "%d", params
->width
);
268 ret
[0].sval
= dupstr(buf
);
271 ret
[1].name
= "Height";
272 ret
[1].type
= C_STRING
;
273 sprintf(buf
, "%d", params
->height
);
274 ret
[1].sval
= dupstr(buf
);
277 ret
[2].name
= "Walls wrap around";
278 ret
[2].type
= C_BOOLEAN
;
280 ret
[2].ival
= params
->wrapping
;
282 ret
[3].name
= "Barrier probability";
283 ret
[3].type
= C_STRING
;
284 sprintf(buf
, "%g", params
->barrier_probability
);
285 ret
[3].sval
= dupstr(buf
);
288 ret
[4].name
= "Number of shuffling moves";
289 ret
[4].type
= C_STRING
;
290 sprintf(buf
, "%d", params
->movetarget
);
291 ret
[4].sval
= dupstr(buf
);
302 static game_params
*custom_params(config_item
*cfg
)
304 game_params
*ret
= snew(game_params
);
306 ret
->width
= atoi(cfg
[0].sval
);
307 ret
->height
= atoi(cfg
[1].sval
);
308 ret
->wrapping
= cfg
[2].ival
;
309 ret
->barrier_probability
= (float)atof(cfg
[3].sval
);
310 ret
->movetarget
= atoi(cfg
[4].sval
);
315 static char *validate_params(game_params
*params
)
317 if (params
->width
<= 1 && params
->height
<= 1)
318 return "Width and height must both be greater than one";
319 if (params
->width
<= 1)
320 return "Width must be greater than one";
321 if (params
->height
<= 1)
322 return "Height must be greater than one";
323 if (params
->barrier_probability
< 0)
324 return "Barrier probability may not be negative";
325 if (params
->barrier_probability
> 1)
326 return "Barrier probability may not be greater than 1";
330 /* ----------------------------------------------------------------------
331 * Randomly select a new game description.
334 static char *new_game_desc(game_params
*params
, random_state
*rs
,
335 game_aux_info
**aux
, int interactive
)
337 tree234
*possibilities
, *barriertree
;
338 int w
, h
, x
, y
, cx
, cy
, nbarriers
;
339 unsigned char *tiles
, *barriers
;
345 tiles
= snewn(w
* h
, unsigned char);
346 memset(tiles
, 0, w
* h
);
347 barriers
= snewn(w
* h
, unsigned char);
348 memset(barriers
, 0, w
* h
);
354 * Construct the unshuffled grid.
356 * To do this, we simply start at the centre point, repeatedly
357 * choose a random possibility out of the available ways to
358 * extend a used square into an unused one, and do it. After
359 * extending the third line out of a square, we remove the
360 * fourth from the possibilities list to avoid any full-cross
361 * squares (which would make the game too easy because they
362 * only have one orientation).
364 * The slightly worrying thing is the avoidance of full-cross
365 * squares. Can this cause our unsophisticated construction
366 * algorithm to paint itself into a corner, by getting into a
367 * situation where there are some unreached squares and the
368 * only way to reach any of them is to extend a T-piece into a
371 * Answer: no it can't, and here's a proof.
373 * Any contiguous group of such unreachable squares must be
374 * surrounded on _all_ sides by T-pieces pointing away from the
375 * group. (If not, then there is a square which can be extended
376 * into one of the `unreachable' ones, and so it wasn't
377 * unreachable after all.) In particular, this implies that
378 * each contiguous group of unreachable squares must be
379 * rectangular in shape (any deviation from that yields a
380 * non-T-piece next to an `unreachable' square).
382 * So we have a rectangle of unreachable squares, with T-pieces
383 * forming a solid border around the rectangle. The corners of
384 * that border must be connected (since every tile connects all
385 * the lines arriving in it), and therefore the border must
386 * form a closed loop around the rectangle.
388 * But this can't have happened in the first place, since we
389 * _know_ we've avoided creating closed loops! Hence, no such
390 * situation can ever arise, and the naive grid construction
391 * algorithm will guaranteeably result in a complete grid
392 * containing no unreached squares, no full crosses _and_ no
395 possibilities
= newtree234(xyd_cmp
);
398 add234(possibilities
, new_xyd(cx
, cy
, R
));
400 add234(possibilities
, new_xyd(cx
, cy
, U
));
402 add234(possibilities
, new_xyd(cx
, cy
, L
));
404 add234(possibilities
, new_xyd(cx
, cy
, D
));
406 while (count234(possibilities
) > 0) {
409 int x1
, y1
, d1
, x2
, y2
, d2
, d
;
412 * Extract a randomly chosen possibility from the list.
414 i
= random_upto(rs
, count234(possibilities
));
415 xyd
= delpos234(possibilities
, i
);
421 OFFSET(x2
, y2
, x1
, y1
, d1
, params
);
424 printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n",
425 x1
, y1
, "0RU3L567D9abcdef"[d1
], x2
, y2
, "0RU3L567D9abcdef"[d2
]);
429 * Make the connection. (We should be moving to an as yet
432 index(params
, tiles
, x1
, y1
) |= d1
;
433 assert(index(params
, tiles
, x2
, y2
) == 0);
434 index(params
, tiles
, x2
, y2
) |= d2
;
437 * If we have created a T-piece, remove its last
440 if (COUNT(index(params
, tiles
, x1
, y1
)) == 3) {
441 struct xyd xyd1
, *xydp
;
445 xyd1
.direction
= 0x0F ^ index(params
, tiles
, x1
, y1
);
447 xydp
= find234(possibilities
, &xyd1
, NULL
);
451 printf("T-piece; removing (%d,%d,%c)\n",
452 xydp
->x
, xydp
->y
, "0RU3L567D9abcdef"[xydp
->direction
]);
454 del234(possibilities
, xydp
);
460 * Remove all other possibilities that were pointing at the
461 * tile we've just moved into.
463 for (d
= 1; d
< 0x10; d
<<= 1) {
465 struct xyd xyd1
, *xydp
;
467 OFFSET(x3
, y3
, x2
, y2
, d
, params
);
474 xydp
= find234(possibilities
, &xyd1
, NULL
);
478 printf("Loop avoidance; removing (%d,%d,%c)\n",
479 xydp
->x
, xydp
->y
, "0RU3L567D9abcdef"[xydp
->direction
]);
481 del234(possibilities
, xydp
);
487 * Add new possibilities to the list for moving _out_ of
488 * the tile we have just moved into.
490 for (d
= 1; d
< 0x10; d
<<= 1) {
494 continue; /* we've got this one already */
496 if (!params
->wrapping
) {
497 if (d
== U
&& y2
== 0)
499 if (d
== D
&& y2
== h
-1)
501 if (d
== L
&& x2
== 0)
503 if (d
== R
&& x2
== w
-1)
507 OFFSET(x3
, y3
, x2
, y2
, d
, params
);
509 if (index(params
, tiles
, x3
, y3
))
510 continue; /* this would create a loop */
513 printf("New frontier; adding (%d,%d,%c)\n",
514 x2
, y2
, "0RU3L567D9abcdef"[d
]);
516 add234(possibilities
, new_xyd(x2
, y2
, d
));
519 /* Having done that, we should have no possibilities remaining. */
520 assert(count234(possibilities
) == 0);
521 freetree234(possibilities
);
524 * Now compute a list of the possible barrier locations.
526 barriertree
= newtree234(xyd_cmp
);
527 for (y
= 0; y
< h
; y
++) {
528 for (x
= 0; x
< w
; x
++) {
530 if (!(index(params
, tiles
, x
, y
) & R
) &&
531 (params
->wrapping
|| x
< w
-1))
532 add234(barriertree
, new_xyd(x
, y
, R
));
533 if (!(index(params
, tiles
, x
, y
) & D
) &&
534 (params
->wrapping
|| y
< h
-1))
535 add234(barriertree
, new_xyd(x
, y
, D
));
540 * Save the unshuffled grid. We do this using a separate
541 * reference-counted structure since it's a large chunk of
542 * memory which we don't want to have to replicate in every
543 * game state while playing.
546 game_aux_info
*solution
;
548 solution
= snew(game_aux_info
);
550 solution
->height
= h
;
551 solution
->tiles
= snewn(w
* h
, unsigned char);
552 memcpy(solution
->tiles
, tiles
, w
* h
);
558 * Now shuffle the grid.
559 * FIXME - this simply does a set of random moves to shuffle the pieces,
560 * although we make a token effort to avoid boring cases by avoiding moves
561 * that directly undo the previous one, or that repeat so often as to
562 * turn into fewer moves.
564 * A better way would be to number all the pieces, generate a placement
565 * for all the numbers as for "sixteen", observing parity constraints if
566 * neccessary, and then place the pieces according to their numbering.
567 * BUT - I'm not sure if this will work, since we disallow movement of
568 * the middle row and column.
574 int moves
= params
->movetarget
;
575 int prevdir
= -1, prevrowcol
= -1, nrepeats
= 0;
576 if (!moves
) moves
= cols
* rows
* 2;
577 for (i
= 0; i
< moves
; /* incremented conditionally */) {
578 /* Choose a direction: 0,1,2,3 = up, right, down, left. */
579 int dir
= random_upto(rs
, 4);
582 int col
= random_upto(rs
, cols
);
583 if (col
>= cx
) col
+= 1; /* avoid centre */
584 if (col
== prevrowcol
) {
585 if (dir
== 2-prevdir
)
586 continue; /* undoes last move */
587 else if ((nrepeats
+1)*2 > h
)
588 continue; /* makes fewer moves */
590 slide_col_int(w
, h
, tiles
, 1 - dir
, col
);
593 int row
= random_upto(rs
, rows
);
594 if (row
>= cy
) row
+= 1; /* avoid centre */
595 if (row
== prevrowcol
) {
596 if (dir
== 4-prevdir
)
597 continue; /* undoes last move */
598 else if ((nrepeats
+1)*2 > w
)
599 continue; /* makes fewer moves */
601 slide_row_int(w
, h
, tiles
, 2 - dir
, row
);
604 if (dir
== prevdir
&& rowcol
== prevrowcol
)
610 i
++; /* if we got here, the move was accepted */
615 * And now choose barrier locations. (We carefully do this
616 * _after_ shuffling, so that changing the barrier rate in the
617 * params while keeping the random seed the same will give the
618 * same shuffled grid and _only_ change the barrier locations.
619 * Also the way we choose barrier locations, by repeatedly
620 * choosing one possibility from the list until we have enough,
621 * is designed to ensure that raising the barrier rate while
622 * keeping the seed the same will provide a superset of the
623 * previous barrier set - i.e. if you ask for 10 barriers, and
624 * then decide that's still too hard and ask for 20, you'll get
625 * the original 10 plus 10 more, rather than getting 20 new
626 * ones and the chance of remembering your first 10.)
628 nbarriers
= (int)(params
->barrier_probability
* count234(barriertree
));
629 assert(nbarriers
>= 0 && nbarriers
<= count234(barriertree
));
631 while (nbarriers
> 0) {
634 int x1
, y1
, d1
, x2
, y2
, d2
;
637 * Extract a randomly chosen barrier from the list.
639 i
= random_upto(rs
, count234(barriertree
));
640 xyd
= delpos234(barriertree
, i
);
649 OFFSET(x2
, y2
, x1
, y1
, d1
, params
);
652 index(params
, barriers
, x1
, y1
) |= d1
;
653 index(params
, barriers
, x2
, y2
) |= d2
;
659 * Clean up the rest of the barrier list.
664 while ( (xyd
= delpos234(barriertree
, 0)) != NULL
)
667 freetree234(barriertree
);
671 * Finally, encode the grid into a string game description.
673 * My syntax is extremely simple: each square is encoded as a
674 * hex digit in which bit 0 means a connection on the right,
675 * bit 1 means up, bit 2 left and bit 3 down. (i.e. the same
676 * encoding as used internally). Each digit is followed by
677 * optional barrier indicators: `v' means a vertical barrier to
678 * the right of it, and `h' means a horizontal barrier below
681 desc
= snewn(w
* h
* 3 + 1, char);
683 for (y
= 0; y
< h
; y
++) {
684 for (x
= 0; x
< w
; x
++) {
685 *p
++ = "0123456789abcdef"[index(params
, tiles
, x
, y
)];
686 if ((params
->wrapping
|| x
< w
-1) &&
687 (index(params
, barriers
, x
, y
) & R
))
689 if ((params
->wrapping
|| y
< h
-1) &&
690 (index(params
, barriers
, x
, y
) & D
))
694 assert(p
- desc
<= w
*h
*3);
703 static void game_free_aux_info(game_aux_info
*aux
)
709 static char *validate_desc(game_params
*params
, char *desc
)
711 int w
= params
->width
, h
= params
->height
;
714 for (i
= 0; i
< w
*h
; i
++) {
715 if (*desc
>= '0' && *desc
<= '9')
717 else if (*desc
>= 'a' && *desc
<= 'f')
719 else if (*desc
>= 'A' && *desc
<= 'F')
722 return "Game description shorter than expected";
724 return "Game description contained unexpected character";
726 while (*desc
== 'h' || *desc
== 'v')
730 return "Game description longer than expected";
735 /* ----------------------------------------------------------------------
736 * Construct an initial game state, given a description and parameters.
739 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
744 assert(params
->width
> 0 && params
->height
> 0);
745 assert(params
->width
> 1 || params
->height
> 1);
748 * Create a blank game state.
750 state
= snew(game_state
);
751 w
= state
->width
= params
->width
;
752 h
= state
->height
= params
->height
;
753 state
->cx
= state
->width
/ 2;
754 state
->cy
= state
->height
/ 2;
755 state
->wrapping
= params
->wrapping
;
756 state
->movetarget
= params
->movetarget
;
757 state
->completed
= 0;
758 state
->used_solve
= state
->just_used_solve
= FALSE
;
759 state
->move_count
= 0;
760 state
->last_move_row
= -1;
761 state
->last_move_col
= -1;
762 state
->last_move_dir
= 0;
763 state
->tiles
= snewn(state
->width
* state
->height
, unsigned char);
764 memset(state
->tiles
, 0, state
->width
* state
->height
);
765 state
->barriers
= snewn(state
->width
* state
->height
, unsigned char);
766 memset(state
->barriers
, 0, state
->width
* state
->height
);
770 * Parse the game description into the grid.
772 for (y
= 0; y
< h
; y
++) {
773 for (x
= 0; x
< w
; x
++) {
774 if (*desc
>= '0' && *desc
<= '9')
775 tile(state
, x
, y
) = *desc
- '0';
776 else if (*desc
>= 'a' && *desc
<= 'f')
777 tile(state
, x
, y
) = *desc
- 'a' + 10;
778 else if (*desc
>= 'A' && *desc
<= 'F')
779 tile(state
, x
, y
) = *desc
- 'A' + 10;
782 while (*desc
== 'h' || *desc
== 'v') {
789 OFFSET(x2
, y2
, x
, y
, d1
, state
);
792 barrier(state
, x
, y
) |= d1
;
793 barrier(state
, x2
, y2
) |= d2
;
801 * Set up border barriers if this is a non-wrapping game.
803 if (!state
->wrapping
) {
804 for (x
= 0; x
< state
->width
; x
++) {
805 barrier(state
, x
, 0) |= U
;
806 barrier(state
, x
, state
->height
-1) |= D
;
808 for (y
= 0; y
< state
->height
; y
++) {
809 barrier(state
, 0, y
) |= L
;
810 barrier(state
, state
->width
-1, y
) |= R
;
815 * Set up the barrier corner flags, for drawing barriers
816 * prettily when they meet.
818 for (y
= 0; y
< state
->height
; y
++) {
819 for (x
= 0; x
< state
->width
; x
++) {
822 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
824 int x1
, y1
, x2
, y2
, x3
, y3
;
827 if (!(barrier(state
, x
, y
) & dir
))
830 if (barrier(state
, x
, y
) & dir2
)
833 x1
= x
+ X(dir
), y1
= y
+ Y(dir
);
834 if (x1
>= 0 && x1
< state
->width
&&
835 y1
>= 0 && y1
< state
->height
&&
836 (barrier(state
, x1
, y1
) & dir2
))
839 x2
= x
+ X(dir2
), y2
= y
+ Y(dir2
);
840 if (x2
>= 0 && x2
< state
->width
&&
841 y2
>= 0 && y2
< state
->height
&&
842 (barrier(state
, x2
, y2
) & dir
))
846 barrier(state
, x
, y
) |= (dir
<< 4);
847 if (x1
>= 0 && x1
< state
->width
&&
848 y1
>= 0 && y1
< state
->height
)
849 barrier(state
, x1
, y1
) |= (A(dir
) << 4);
850 if (x2
>= 0 && x2
< state
->width
&&
851 y2
>= 0 && y2
< state
->height
)
852 barrier(state
, x2
, y2
) |= (C(dir
) << 4);
853 x3
= x
+ X(dir
) + X(dir2
), y3
= y
+ Y(dir
) + Y(dir2
);
854 if (x3
>= 0 && x3
< state
->width
&&
855 y3
>= 0 && y3
< state
->height
)
856 barrier(state
, x3
, y3
) |= (F(dir
) << 4);
865 static game_state
*dup_game(game_state
*state
)
869 ret
= snew(game_state
);
870 ret
->width
= state
->width
;
871 ret
->height
= state
->height
;
874 ret
->wrapping
= state
->wrapping
;
875 ret
->movetarget
= state
->movetarget
;
876 ret
->completed
= state
->completed
;
877 ret
->used_solve
= state
->used_solve
;
878 ret
->just_used_solve
= state
->just_used_solve
;
879 ret
->move_count
= state
->move_count
;
880 ret
->last_move_row
= state
->last_move_row
;
881 ret
->last_move_col
= state
->last_move_col
;
882 ret
->last_move_dir
= state
->last_move_dir
;
883 ret
->tiles
= snewn(state
->width
* state
->height
, unsigned char);
884 memcpy(ret
->tiles
, state
->tiles
, state
->width
* state
->height
);
885 ret
->barriers
= snewn(state
->width
* state
->height
, unsigned char);
886 memcpy(ret
->barriers
, state
->barriers
, state
->width
* state
->height
);
891 static void free_game(game_state
*state
)
894 sfree(state
->barriers
);
898 static game_state
*solve_game(game_state
*state
, game_aux_info
*aux
,
904 *error
= "Solution not known for this puzzle";
908 assert(aux
->width
== state
->width
);
909 assert(aux
->height
== state
->height
);
910 ret
= dup_game(state
);
911 memcpy(ret
->tiles
, aux
->tiles
, ret
->width
* ret
->height
);
912 ret
->used_solve
= ret
->just_used_solve
= TRUE
;
913 ret
->completed
= ret
->move_count
= 1;
918 static char *game_text_format(game_state
*state
)
923 /* ----------------------------------------------------------------------
928 * Compute which squares are reachable from the centre square, as a
929 * quick visual aid to determining how close the game is to
930 * completion. This is also a simple way to tell if the game _is_
931 * completed - just call this function and see whether every square
934 * squares in the moving_row and moving_col are always inactive - this
935 * is so that "current" doesn't appear to jump across moving lines.
937 static unsigned char *compute_active(game_state
*state
,
938 int moving_row
, int moving_col
)
940 unsigned char *active
;
944 active
= snewn(state
->width
* state
->height
, unsigned char);
945 memset(active
, 0, state
->width
* state
->height
);
948 * We only store (x,y) pairs in todo, but it's easier to reuse
949 * xyd_cmp and just store direction 0 every time.
951 todo
= newtree234(xyd_cmp
);
952 index(state
, active
, state
->cx
, state
->cy
) = ACTIVE
;
953 add234(todo
, new_xyd(state
->cx
, state
->cy
, 0));
955 while ( (xyd
= delpos234(todo
, 0)) != NULL
) {
956 int x1
, y1
, d1
, x2
, y2
, d2
;
962 for (d1
= 1; d1
< 0x10; d1
<<= 1) {
963 OFFSET(x2
, y2
, x1
, y1
, d1
, state
);
967 * If the next tile in this direction is connected to
968 * us, and there isn't a barrier in the way, and it
969 * isn't already marked active, then mark it active and
970 * add it to the to-examine list.
972 if ((x2
!= moving_col
&& y2
!= moving_row
) &&
973 (tile(state
, x1
, y1
) & d1
) &&
974 (tile(state
, x2
, y2
) & d2
) &&
975 !(barrier(state
, x1
, y1
) & d1
) &&
976 !index(state
, active
, x2
, y2
)) {
977 index(state
, active
, x2
, y2
) = ACTIVE
;
978 add234(todo
, new_xyd(x2
, y2
, 0));
982 /* Now we expect the todo list to have shrunk to zero size. */
983 assert(count234(todo
) == 0);
994 static game_ui
*new_ui(game_state
*state
)
996 game_ui
*ui
= snew(game_ui
);
997 ui
->cur_x
= state
->width
/ 2;
998 ui
->cur_y
= state
->height
/ 2;
999 ui
->cur_visible
= FALSE
;
1004 static void free_ui(game_ui
*ui
)
1009 /* ----------------------------------------------------------------------
1013 static void slide_row_int(int w
, int h
, unsigned char *tiles
, int dir
, int row
)
1015 int x
= dir
> 0 ?
-1 : w
;
1018 unsigned char endtile
= tiles
[row
* w
+ tx
];
1021 tx
= (x
+ dir
+ w
) % w
;
1022 tiles
[row
* w
+ x
] = tiles
[row
* w
+ tx
];
1024 tiles
[row
* w
+ tx
] = endtile
;
1027 static void slide_col_int(int w
, int h
, unsigned char *tiles
, int dir
, int col
)
1029 int y
= dir
> 0 ?
-1 : h
;
1032 unsigned char endtile
= tiles
[ty
* w
+ col
];
1035 ty
= (y
+ dir
+ h
) % h
;
1036 tiles
[y
* w
+ col
] = tiles
[ty
* w
+ col
];
1038 tiles
[ty
* w
+ col
] = endtile
;
1041 static void slide_row(game_state
*state
, int dir
, int row
)
1043 slide_row_int(state
->width
, state
->height
, state
->tiles
, dir
, row
);
1046 static void slide_col(game_state
*state
, int dir
, int col
)
1048 slide_col_int(state
->width
, state
->height
, state
->tiles
, dir
, col
);
1051 static game_state
*make_move(game_state
*state
, game_ui
*ui
,
1052 game_drawstate
*ds
, int x
, int y
, int button
)
1058 button
&= ~MOD_MASK
;
1060 if (button
!= LEFT_BUTTON
&& button
!= RIGHT_BUTTON
)
1063 cx
= (x
- (BORDER
+ WINDOW_OFFSET
+ TILE_BORDER
) + 2*TILE_SIZE
) / TILE_SIZE
- 2;
1064 cy
= (y
- (BORDER
+ WINDOW_OFFSET
+ TILE_BORDER
) + 2*TILE_SIZE
) / TILE_SIZE
- 2;
1066 if (cy
>= 0 && cy
< state
->height
&& cy
!= state
->cy
)
1068 if (cx
== -1) dx
= +1;
1069 else if (cx
== state
->width
) dx
= -1;
1074 else if (cx
>= 0 && cx
< state
->width
&& cx
!= state
->cx
)
1076 if (cy
== -1) dy
= +1;
1077 else if (cy
== state
->height
) dy
= -1;
1085 /* reverse direction if right hand button is pressed */
1086 if (button
== RIGHT_BUTTON
)
1092 ret
= dup_game(state
);
1093 ret
->just_used_solve
= FALSE
;
1095 if (dx
== 0) slide_col(ret
, dy
, cx
);
1096 else slide_row(ret
, dx
, cy
);
1099 ret
->last_move_row
= dx ? cy
: -1;
1100 ret
->last_move_col
= dx ?
-1 : cx
;
1101 ret
->last_move_dir
= dx
+ dy
;
1104 * See if the game has been completed.
1106 if (!ret
->completed
) {
1107 unsigned char *active
= compute_active(ret
, -1, -1);
1109 int complete
= TRUE
;
1111 for (x1
= 0; x1
< ret
->width
; x1
++)
1112 for (y1
= 0; y1
< ret
->height
; y1
++)
1113 if (!index(ret
, active
, x1
, y1
)) {
1115 goto break_label
; /* break out of two loops at once */
1122 ret
->completed
= ret
->move_count
;
1128 /* ----------------------------------------------------------------------
1129 * Routines for drawing the game position on the screen.
1132 struct game_drawstate
{
1135 unsigned char *visible
;
1138 static game_drawstate
*game_new_drawstate(game_state
*state
)
1140 game_drawstate
*ds
= snew(game_drawstate
);
1142 ds
->started
= FALSE
;
1143 ds
->width
= state
->width
;
1144 ds
->height
= state
->height
;
1145 ds
->visible
= snewn(state
->width
* state
->height
, unsigned char);
1146 memset(ds
->visible
, 0xFF, state
->width
* state
->height
);
1151 static void game_free_drawstate(game_drawstate
*ds
)
1157 static void game_size(game_params
*params
, int *x
, int *y
)
1159 *x
= BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* params
->width
+ TILE_BORDER
;
1160 *y
= BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* params
->height
+ TILE_BORDER
;
1163 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1167 ret
= snewn(NCOLOURS
* 3, float);
1168 *ncolours
= NCOLOURS
;
1171 * Basic background colour is whatever the front end thinks is
1172 * a sensible default.
1174 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1179 ret
[COL_WIRE
* 3 + 0] = 0.0F
;
1180 ret
[COL_WIRE
* 3 + 1] = 0.0F
;
1181 ret
[COL_WIRE
* 3 + 2] = 0.0F
;
1184 * Powered wires and powered endpoints are cyan.
1186 ret
[COL_POWERED
* 3 + 0] = 0.0F
;
1187 ret
[COL_POWERED
* 3 + 1] = 1.0F
;
1188 ret
[COL_POWERED
* 3 + 2] = 1.0F
;
1193 ret
[COL_BARRIER
* 3 + 0] = 1.0F
;
1194 ret
[COL_BARRIER
* 3 + 1] = 0.0F
;
1195 ret
[COL_BARRIER
* 3 + 2] = 0.0F
;
1198 * Unpowered endpoints are blue.
1200 ret
[COL_ENDPOINT
* 3 + 0] = 0.0F
;
1201 ret
[COL_ENDPOINT
* 3 + 1] = 0.0F
;
1202 ret
[COL_ENDPOINT
* 3 + 2] = 1.0F
;
1205 * Tile borders are a darker grey than the background.
1207 ret
[COL_BORDER
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
1208 ret
[COL_BORDER
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
1209 ret
[COL_BORDER
* 3 + 2] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 2];
1212 * Flashing tiles are a grey in between those two.
1214 ret
[COL_FLASHING
* 3 + 0] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 0];
1215 ret
[COL_FLASHING
* 3 + 1] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 1];
1216 ret
[COL_FLASHING
* 3 + 2] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 2];
1218 ret
[COL_LOWLIGHT
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 0.8F
;
1219 ret
[COL_LOWLIGHT
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 0.8F
;
1220 ret
[COL_LOWLIGHT
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 0.8F
;
1221 ret
[COL_TEXT
* 3 + 0] = 0.0;
1222 ret
[COL_TEXT
* 3 + 1] = 0.0;
1223 ret
[COL_TEXT
* 3 + 2] = 0.0;
1228 static void draw_thick_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
,
1231 draw_line(fe
, x1
-1, y1
, x2
-1, y2
, COL_WIRE
);
1232 draw_line(fe
, x1
+1, y1
, x2
+1, y2
, COL_WIRE
);
1233 draw_line(fe
, x1
, y1
-1, x2
, y2
-1, COL_WIRE
);
1234 draw_line(fe
, x1
, y1
+1, x2
, y2
+1, COL_WIRE
);
1235 draw_line(fe
, x1
, y1
, x2
, y2
, colour
);
1238 static void draw_rect_coords(frontend
*fe
, int x1
, int y1
, int x2
, int y2
,
1241 int mx
= (x1
< x2 ? x1
: x2
);
1242 int my
= (y1
< y2 ? y1
: y2
);
1243 int dx
= (x2
+ x1
- 2*mx
+ 1);
1244 int dy
= (y2
+ y1
- 2*my
+ 1);
1246 draw_rect(fe
, mx
, my
, dx
, dy
, colour
);
1249 static void draw_barrier_corner(frontend
*fe
, int x
, int y
, int dir
, int phase
)
1251 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
;
1252 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
;
1253 int x1
, y1
, dx
, dy
, dir2
;
1258 dx
= X(dir
) + X(dir2
);
1259 dy
= Y(dir
) + Y(dir2
);
1260 x1
= (dx
> 0 ? TILE_SIZE
+TILE_BORDER
-1 : 0);
1261 y1
= (dy
> 0 ? TILE_SIZE
+TILE_BORDER
-1 : 0);
1264 draw_rect_coords(fe
, bx
+x1
, by
+y1
,
1265 bx
+x1
-TILE_BORDER
*dx
, by
+y1
-(TILE_BORDER
-1)*dy
,
1267 draw_rect_coords(fe
, bx
+x1
, by
+y1
,
1268 bx
+x1
-(TILE_BORDER
-1)*dx
, by
+y1
-TILE_BORDER
*dy
,
1271 draw_rect_coords(fe
, bx
+x1
, by
+y1
,
1272 bx
+x1
-(TILE_BORDER
-1)*dx
, by
+y1
-(TILE_BORDER
-1)*dy
,
1277 static void draw_barrier(frontend
*fe
, int x
, int y
, int dir
, int phase
)
1279 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
;
1280 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
;
1283 x1
= (X(dir
) > 0 ? TILE_SIZE
: X(dir
) == 0 ? TILE_BORDER
: 0);
1284 y1
= (Y(dir
) > 0 ? TILE_SIZE
: Y(dir
) == 0 ? TILE_BORDER
: 0);
1285 w
= (X(dir
) ? TILE_BORDER
: TILE_SIZE
- TILE_BORDER
);
1286 h
= (Y(dir
) ? TILE_BORDER
: TILE_SIZE
- TILE_BORDER
);
1289 draw_rect(fe
, bx
+x1
-X(dir
), by
+y1
-Y(dir
), w
, h
, COL_WIRE
);
1291 draw_rect(fe
, bx
+x1
, by
+y1
, w
, h
, COL_BARRIER
);
1295 static void draw_tile(frontend
*fe
, game_state
*state
, int x
, int y
, int tile
,
1296 float xshift
, float yshift
)
1298 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
+ (xshift
* TILE_SIZE
);
1299 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
+ (yshift
* TILE_SIZE
);
1300 float cx
, cy
, ex
, ey
;
1304 * When we draw a single tile, we must draw everything up to
1305 * and including the borders around the tile. This means that
1306 * if the neighbouring tiles have connections to those borders,
1307 * we must draw those connections on the borders themselves.
1309 * This would be terribly fiddly if we ever had to draw a tile
1310 * while its neighbour was in mid-rotate, because we'd have to
1311 * arrange to _know_ that the neighbour was being rotated and
1312 * hence had an anomalous effect on the redraw of this tile.
1313 * Fortunately, the drawing algorithm avoids ever calling us in
1314 * this circumstance: we're either drawing lots of straight
1315 * tiles at game start or after a move is complete, or we're
1316 * repeatedly drawing only the rotating tile. So no problem.
1320 * So. First blank the tile out completely: draw a big
1321 * rectangle in border colour, and a smaller rectangle in
1322 * background colour to fill it in.
1324 draw_rect(fe
, bx
, by
, TILE_SIZE
+TILE_BORDER
, TILE_SIZE
+TILE_BORDER
,
1326 draw_rect(fe
, bx
+TILE_BORDER
, by
+TILE_BORDER
,
1327 TILE_SIZE
-TILE_BORDER
, TILE_SIZE
-TILE_BORDER
,
1328 tile
& FLASHING ? COL_FLASHING
: COL_BACKGROUND
);
1333 cx
= cy
= TILE_BORDER
+ (TILE_SIZE
-TILE_BORDER
) / 2.0F
- 0.5F
;
1334 col
= (tile
& ACTIVE ? COL_POWERED
: COL_WIRE
);
1335 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
1337 ex
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* X(dir
);
1338 ey
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* Y(dir
);
1339 draw_thick_line(fe
, bx
+(int)cx
, by
+(int)cy
,
1340 bx
+(int)(cx
+ex
), by
+(int)(cy
+ey
),
1344 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
1346 ex
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* X(dir
);
1347 ey
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* Y(dir
);
1348 draw_line(fe
, bx
+(int)cx
, by
+(int)cy
,
1349 bx
+(int)(cx
+ex
), by
+(int)(cy
+ey
), col
);
1354 * Draw the box in the middle. We do this in blue if the tile
1355 * is an unpowered endpoint, in cyan if the tile is a powered
1356 * endpoint, in black if the tile is the centrepiece, and
1357 * otherwise not at all.
1360 if (x
== state
->cx
&& y
== state
->cy
)
1362 else if (COUNT(tile
) == 1) {
1363 col
= (tile
& ACTIVE ? COL_POWERED
: COL_ENDPOINT
);
1368 points
[0] = +1; points
[1] = +1;
1369 points
[2] = +1; points
[3] = -1;
1370 points
[4] = -1; points
[5] = -1;
1371 points
[6] = -1; points
[7] = +1;
1373 for (i
= 0; i
< 8; i
+= 2) {
1374 ex
= (TILE_SIZE
* 0.24F
) * points
[i
];
1375 ey
= (TILE_SIZE
* 0.24F
) * points
[i
+1];
1376 points
[i
] = bx
+(int)(cx
+ex
);
1377 points
[i
+1] = by
+(int)(cy
+ey
);
1380 draw_polygon(fe
, points
, 4, TRUE
, col
);
1381 draw_polygon(fe
, points
, 4, FALSE
, COL_WIRE
);
1385 * Draw the points on the border if other tiles are connected
1388 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
1389 int dx
, dy
, px
, py
, lx
, ly
, vx
, vy
, ox
, oy
;
1397 if (ox
< 0 || ox
>= state
->width
|| oy
< 0 || oy
>= state
->height
)
1400 if (!(tile(state
, ox
, oy
) & F(dir
)))
1403 px
= bx
+ (int)(dx
>0 ? TILE_SIZE
+ TILE_BORDER
- 1 : dx
<0 ?
0 : cx
);
1404 py
= by
+ (int)(dy
>0 ? TILE_SIZE
+ TILE_BORDER
- 1 : dy
<0 ?
0 : cy
);
1405 lx
= dx
* (TILE_BORDER
-1);
1406 ly
= dy
* (TILE_BORDER
-1);
1410 if (xshift
== 0.0 && yshift
== 0.0 && (tile
& dir
)) {
1412 * If we are fully connected to the other tile, we must
1413 * draw right across the tile border. (We can use our
1414 * own ACTIVE state to determine what colour to do this
1415 * in: if we are fully connected to the other tile then
1416 * the two ACTIVE states will be the same.)
1418 draw_rect_coords(fe
, px
-vx
, py
-vy
, px
+lx
+vx
, py
+ly
+vy
, COL_WIRE
);
1419 draw_rect_coords(fe
, px
, py
, px
+lx
, py
+ly
,
1420 (tile
& ACTIVE
) ? COL_POWERED
: COL_WIRE
);
1423 * The other tile extends into our border, but isn't
1424 * actually connected to us. Just draw a single black
1427 draw_rect_coords(fe
, px
, py
, px
, py
, COL_WIRE
);
1431 draw_update(fe
, bx
, by
, TILE_SIZE
+TILE_BORDER
, TILE_SIZE
+TILE_BORDER
);
1434 static void draw_tile_barriers(frontend
*fe
, game_state
*state
, int x
, int y
)
1438 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
;
1439 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
;
1441 * Draw barrier corners, and then barriers.
1443 for (phase
= 0; phase
< 2; phase
++) {
1444 for (dir
= 1; dir
< 0x10; dir
<<= 1)
1445 if (barrier(state
, x
, y
) & (dir
<< 4))
1446 draw_barrier_corner(fe
, x
, y
, dir
<< 4, phase
);
1447 for (dir
= 1; dir
< 0x10; dir
<<= 1)
1448 if (barrier(state
, x
, y
) & dir
)
1449 draw_barrier(fe
, x
, y
, dir
, phase
);
1452 draw_update(fe
, bx
, by
, TILE_SIZE
+TILE_BORDER
, TILE_SIZE
+TILE_BORDER
);
1455 static void draw_arrow(frontend
*fe
, int x
, int y
, int xdx
, int xdy
)
1458 int ydy
= -xdx
, ydx
= xdy
;
1460 x
= x
* TILE_SIZE
+ BORDER
+ WINDOW_OFFSET
;
1461 y
= y
* TILE_SIZE
+ BORDER
+ WINDOW_OFFSET
;
1463 #define POINT(n, xx, yy) ( \
1464 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
1465 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
1467 POINT(0, TILE_SIZE
/ 2, 3 * TILE_SIZE
/ 4); /* top of arrow */
1468 POINT(1, 3 * TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* right corner */
1469 POINT(2, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* right concave */
1470 POINT(3, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom right */
1471 POINT(4, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom left */
1472 POINT(5, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* left concave */
1473 POINT(6, TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* left corner */
1475 draw_polygon(fe
, coords
, 7, TRUE
, COL_LOWLIGHT
);
1476 draw_polygon(fe
, coords
, 7, FALSE
, COL_TEXT
);
1479 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1480 game_state
*state
, int dir
, game_ui
*ui
, float t
, float ft
)
1482 int x
, y
, tx
, ty
, frame
;
1483 unsigned char *active
;
1488 * Clear the screen and draw the exterior barrier lines if this
1489 * is our first call.
1497 BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* state
->width
+ TILE_BORDER
,
1498 BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* state
->height
+ TILE_BORDER
,
1500 draw_update(fe
, 0, 0,
1501 BORDER
* 2 + WINDOW_OFFSET
*2 + TILE_SIZE
*state
->width
+ TILE_BORDER
,
1502 BORDER
* 2 + WINDOW_OFFSET
*2 + TILE_SIZE
*state
->height
+ TILE_BORDER
);
1504 for (phase
= 0; phase
< 2; phase
++) {
1506 for (x
= 0; x
< ds
->width
; x
++) {
1507 if (barrier(state
, x
, 0) & UL
)
1508 draw_barrier_corner(fe
, x
, -1, LD
, phase
);
1509 if (barrier(state
, x
, 0) & RU
)
1510 draw_barrier_corner(fe
, x
, -1, DR
, phase
);
1511 if (barrier(state
, x
, 0) & U
)
1512 draw_barrier(fe
, x
, -1, D
, phase
);
1513 if (barrier(state
, x
, ds
->height
-1) & DR
)
1514 draw_barrier_corner(fe
, x
, ds
->height
, RU
, phase
);
1515 if (barrier(state
, x
, ds
->height
-1) & LD
)
1516 draw_barrier_corner(fe
, x
, ds
->height
, UL
, phase
);
1517 if (barrier(state
, x
, ds
->height
-1) & D
)
1518 draw_barrier(fe
, x
, ds
->height
, U
, phase
);
1521 for (y
= 0; y
< ds
->height
; y
++) {
1522 if (barrier(state
, 0, y
) & UL
)
1523 draw_barrier_corner(fe
, -1, y
, RU
, phase
);
1524 if (barrier(state
, 0, y
) & LD
)
1525 draw_barrier_corner(fe
, -1, y
, DR
, phase
);
1526 if (barrier(state
, 0, y
) & L
)
1527 draw_barrier(fe
, -1, y
, R
, phase
);
1528 if (barrier(state
, ds
->width
-1, y
) & RU
)
1529 draw_barrier_corner(fe
, ds
->width
, y
, UL
, phase
);
1530 if (barrier(state
, ds
->width
-1, y
) & DR
)
1531 draw_barrier_corner(fe
, ds
->width
, y
, LD
, phase
);
1532 if (barrier(state
, ds
->width
-1, y
) & R
)
1533 draw_barrier(fe
, ds
->width
, y
, L
, phase
);
1538 * Arrows for making moves.
1540 for (x
= 0; x
< ds
->width
; x
++) {
1541 if (x
== state
->cx
) continue;
1542 draw_arrow(fe
, x
, 0, +1, 0);
1543 draw_arrow(fe
, x
+1, ds
->height
, -1, 0);
1545 for (y
= 0; y
< ds
->height
; y
++) {
1546 if (y
== state
->cy
) continue;
1547 draw_arrow(fe
, ds
->width
, y
, 0, +1);
1548 draw_arrow(fe
, 0, y
+1, 0, -1);
1552 /* Check if this is an undo. If so, we will need to run any animation
1555 if (oldstate
&& oldstate
->move_count
> state
->move_count
) {
1556 game_state
* tmpstate
= state
;
1558 oldstate
= tmpstate
;
1563 if (oldstate
&& (t
< ANIM_TIME
)) {
1565 * We're animating a slide, of row/column number
1566 * state->last_move_pos, in direction
1567 * state->last_move_dir
1569 xshift
= state
->last_move_row
== -1 ?
0.0 :
1570 (1 - t
/ ANIM_TIME
) * state
->last_move_dir
;
1571 yshift
= state
->last_move_col
== -1 ?
0.0 :
1572 (1 - t
/ ANIM_TIME
) * state
->last_move_dir
;
1578 * We're animating a completion flash. Find which frame
1581 frame
= (int)(ft
/ FLASH_FRAME
);
1585 * Draw any tile which differs from the way it was last drawn.
1587 if (xshift
!= 0.0 || yshift
!= 0.0) {
1588 active
= compute_active(state
,
1589 state
->last_move_row
, state
->last_move_col
);
1591 active
= compute_active(state
, -1, -1);
1595 BORDER
+ WINDOW_OFFSET
, BORDER
+ WINDOW_OFFSET
,
1596 TILE_SIZE
* state
->width
+ TILE_BORDER
,
1597 TILE_SIZE
* state
->height
+ TILE_BORDER
);
1599 for (x
= 0; x
< ds
->width
; x
++)
1600 for (y
= 0; y
< ds
->height
; y
++) {
1601 unsigned char c
= tile(state
, x
, y
) | index(state
, active
, x
, y
);
1604 * In a completion flash, we adjust the FLASHING bit
1605 * depending on our distance from the centre point and
1609 int xdist
, ydist
, dist
;
1610 xdist
= (x
< state
->cx ? state
->cx
- x
: x
- state
->cx
);
1611 ydist
= (y
< state
->cy ? state
->cy
- y
: y
- state
->cy
);
1612 dist
= (xdist
> ydist ? xdist
: ydist
);
1614 if (frame
>= dist
&& frame
< dist
+4) {
1615 int flash
= (frame
- dist
) & 1;
1616 flash
= flash ? FLASHING
: 0;
1617 c
= (c
&~ FLASHING
) | flash
;
1621 if (index(state
, ds
->visible
, x
, y
) != c
||
1622 index(state
, ds
->visible
, x
, y
) == 0xFF ||
1623 (x
== state
->last_move_col
|| y
== state
->last_move_row
))
1625 float xs
= (y
== state
->last_move_row ? xshift
: 0.0);
1626 float ys
= (x
== state
->last_move_col ? yshift
: 0.0);
1628 draw_tile(fe
, state
, x
, y
, c
, xs
, ys
);
1629 if (xs
< 0 && x
== 0)
1630 draw_tile(fe
, state
, state
->width
, y
, c
, xs
, ys
);
1631 else if (xs
> 0 && x
== state
->width
- 1)
1632 draw_tile(fe
, state
, -1, y
, c
, xs
, ys
);
1633 else if (ys
< 0 && y
== 0)
1634 draw_tile(fe
, state
, x
, state
->height
, c
, xs
, ys
);
1635 else if (ys
> 0 && y
== state
->height
- 1)
1636 draw_tile(fe
, state
, x
, -1, c
, xs
, ys
);
1638 if (x
== state
->last_move_col
|| y
== state
->last_move_row
)
1639 index(state
, ds
->visible
, x
, y
) = 0xFF;
1641 index(state
, ds
->visible
, x
, y
) = c
;
1645 for (x
= 0; x
< ds
->width
; x
++)
1646 for (y
= 0; y
< ds
->height
; y
++)
1647 draw_tile_barriers(fe
, state
, x
, y
);
1652 * Update the status bar.
1655 char statusbuf
[256];
1658 n
= state
->width
* state
->height
;
1659 for (i
= a
= 0; i
< n
; i
++)
1663 if (state
->used_solve
)
1664 sprintf(statusbuf
, "Moves since auto-solve: %d",
1665 state
->move_count
- state
->completed
);
1667 sprintf(statusbuf
, "%sMoves: %d",
1668 (state
->completed ?
"COMPLETED! " : ""),
1669 (state
->completed ? state
->completed
: state
->move_count
));
1671 if (state
->movetarget
)
1672 sprintf(statusbuf
+ strlen(statusbuf
), " (target %d)",
1675 sprintf(statusbuf
+ strlen(statusbuf
), " Active: %d/%d", a
, n
);
1677 status_bar(fe
, statusbuf
);
1683 static float game_anim_length(game_state
*oldstate
,
1684 game_state
*newstate
, int dir
, game_ui
*ui
)
1687 * Don't animate an auto-solve move.
1689 if ((dir
> 0 && newstate
->just_used_solve
) ||
1690 (dir
< 0 && oldstate
->just_used_solve
))
1696 static float game_flash_length(game_state
*oldstate
,
1697 game_state
*newstate
, int dir
, game_ui
*ui
)
1700 * If the game has just been completed, we display a completion
1703 if (!oldstate
->completed
&& newstate
->completed
&&
1704 !oldstate
->used_solve
&& !newstate
->used_solve
) {
1707 if (size
< newstate
->cx
+1)
1708 size
= newstate
->cx
+1;
1709 if (size
< newstate
->cy
+1)
1710 size
= newstate
->cy
+1;
1711 if (size
< newstate
->width
- newstate
->cx
)
1712 size
= newstate
->width
- newstate
->cx
;
1713 if (size
< newstate
->height
- newstate
->cy
)
1714 size
= newstate
->height
- newstate
->cy
;
1715 return FLASH_FRAME
* (size
+4);
1721 static int game_wants_statusbar(void)
1726 static int game_timing_state(game_state
*state
)
1732 #define thegame netslide
1735 const struct game thegame
= {
1736 "Netslide", "games.netslide",
1743 TRUE
, game_configure
, custom_params
,
1752 FALSE
, game_text_format
,
1759 game_free_drawstate
,
1763 game_wants_statusbar
,
1764 FALSE
, game_timing_state
,
1765 0, /* mouse_priorities */