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
;
85 struct game_aux_info
{
91 int width
, height
, cx
, cy
, wrapping
, completed
;
92 int used_solve
, just_used_solve
;
93 int move_count
, movetarget
;
95 /* position (row or col number, starting at 0) of last move. */
96 int last_move_row
, last_move_col
;
98 /* direction of last move: +1 or -1 */
101 unsigned char *tiles
;
102 unsigned char *barriers
;
105 #define OFFSET(x2,y2,x1,y1,dir,state) \
106 ( (x2) = ((x1) + (state)->width + X((dir))) % (state)->width, \
107 (y2) = ((y1) + (state)->height + Y((dir))) % (state)->height)
109 #define index(state, a, x, y) ( a[(y) * (state)->width + (x)] )
110 #define tile(state, x, y) index(state, (state)->tiles, x, y)
111 #define barrier(state, x, y) index(state, (state)->barriers, x, y)
117 static int xyd_cmp(void *av
, void *bv
) {
118 struct xyd
*a
= (struct xyd
*)av
;
119 struct xyd
*b
= (struct xyd
*)bv
;
128 if (a
->direction
< b
->direction
)
130 if (a
->direction
> b
->direction
)
135 static struct xyd
*new_xyd(int x
, int y
, int direction
)
137 struct xyd
*xyd
= snew(struct xyd
);
140 xyd
->direction
= direction
;
144 static void slide_col(game_state
*state
, int dir
, int col
);
145 static void slide_col_int(int w
, int h
, unsigned char *tiles
, int dir
, int col
);
146 static void slide_row(game_state
*state
, int dir
, int row
);
147 static void slide_row_int(int w
, int h
, unsigned char *tiles
, int dir
, int row
);
149 /* ----------------------------------------------------------------------
150 * Manage game parameters.
152 static game_params
*default_params(void)
154 game_params
*ret
= snew(game_params
);
158 ret
->wrapping
= FALSE
;
159 ret
->barrier_probability
= 1.0;
165 static const struct { int x
, y
, wrap
, bprob
; const char* desc
; }
166 netslide_presets
[] = {
167 {3, 3, FALSE
, 1.0, " easy"},
168 {3, 3, FALSE
, 0.0, " medium"},
169 {3, 3, TRUE
, 0.0, " hard"},
170 {4, 4, FALSE
, 1.0, " easy"},
171 {4, 4, FALSE
, 0.0, " medium"},
172 {4, 4, TRUE
, 0.0, " hard"},
173 {5, 5, FALSE
, 1.0, " easy"},
174 {5, 5, FALSE
, 0.0, " medium"},
175 {5, 5, TRUE
, 0.0, " hard"},
178 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
183 if (i
< 0 || i
>= lenof(netslide_presets
))
186 ret
= snew(game_params
);
187 ret
->width
= netslide_presets
[i
].x
;
188 ret
->height
= netslide_presets
[i
].y
;
189 ret
->wrapping
= netslide_presets
[i
].wrap
;
190 ret
->barrier_probability
= netslide_presets
[i
].bprob
;
193 sprintf(str
, "%dx%d%s", ret
->width
, ret
->height
, netslide_presets
[i
].desc
);
200 static void free_params(game_params
*params
)
205 static game_params
*dup_params(game_params
*params
)
207 game_params
*ret
= snew(game_params
);
208 *ret
= *params
; /* structure copy */
212 static void decode_params(game_params
*ret
, char const *string
)
214 char const *p
= string
;
216 ret
->wrapping
= FALSE
;
217 ret
->barrier_probability
= 0.0;
220 ret
->width
= atoi(p
);
221 while (*p
&& isdigit(*p
)) p
++;
224 ret
->height
= atoi(p
);
225 while (*p
&& isdigit(*p
)) p
++;
226 if ( (ret
->wrapping
= (*p
== 'w')) != 0 )
229 ret
->barrier_probability
= atof(++p
);
230 while (*p
&& (isdigit(*p
) || *p
== '.')) p
++;
233 ret
->movetarget
= atoi(++p
);
236 ret
->height
= ret
->width
;
240 static char *encode_params(game_params
*params
, int full
)
245 len
= sprintf(ret
, "%dx%d", params
->width
, params
->height
);
246 if (params
->wrapping
)
248 if (full
&& params
->barrier_probability
)
249 len
+= sprintf(ret
+len
, "b%g", params
->barrier_probability
);
250 /* Shuffle limit is part of the limited parameters, because we have to
251 * provide the target move count. */
252 if (params
->movetarget
)
253 len
+= sprintf(ret
+len
, "m%d", params
->movetarget
);
254 assert(len
< lenof(ret
));
260 static config_item
*game_configure(game_params
*params
)
265 ret
= snewn(6, config_item
);
267 ret
[0].name
= "Width";
268 ret
[0].type
= C_STRING
;
269 sprintf(buf
, "%d", params
->width
);
270 ret
[0].sval
= dupstr(buf
);
273 ret
[1].name
= "Height";
274 ret
[1].type
= C_STRING
;
275 sprintf(buf
, "%d", params
->height
);
276 ret
[1].sval
= dupstr(buf
);
279 ret
[2].name
= "Walls wrap around";
280 ret
[2].type
= C_BOOLEAN
;
282 ret
[2].ival
= params
->wrapping
;
284 ret
[3].name
= "Barrier probability";
285 ret
[3].type
= C_STRING
;
286 sprintf(buf
, "%g", params
->barrier_probability
);
287 ret
[3].sval
= dupstr(buf
);
290 ret
[4].name
= "Number of shuffling moves";
291 ret
[4].type
= C_STRING
;
292 sprintf(buf
, "%d", params
->movetarget
);
293 ret
[4].sval
= dupstr(buf
);
304 static game_params
*custom_params(config_item
*cfg
)
306 game_params
*ret
= snew(game_params
);
308 ret
->width
= atoi(cfg
[0].sval
);
309 ret
->height
= atoi(cfg
[1].sval
);
310 ret
->wrapping
= cfg
[2].ival
;
311 ret
->barrier_probability
= (float)atof(cfg
[3].sval
);
312 ret
->movetarget
= atoi(cfg
[4].sval
);
317 static char *validate_params(game_params
*params
)
319 if (params
->width
<= 1 || params
->height
<= 1)
320 return "Width and height must both be greater than one";
321 if (params
->barrier_probability
< 0)
322 return "Barrier probability may not be negative";
323 if (params
->barrier_probability
> 1)
324 return "Barrier probability may not be greater than 1";
328 /* ----------------------------------------------------------------------
329 * Randomly select a new game description.
332 static char *new_game_desc(game_params
*params
, random_state
*rs
,
333 game_aux_info
**aux
, int interactive
)
335 tree234
*possibilities
, *barriertree
;
336 int w
, h
, x
, y
, cx
, cy
, nbarriers
;
337 unsigned char *tiles
, *barriers
;
343 tiles
= snewn(w
* h
, unsigned char);
344 memset(tiles
, 0, w
* h
);
345 barriers
= snewn(w
* h
, unsigned char);
346 memset(barriers
, 0, w
* h
);
352 * Construct the unshuffled grid.
354 * To do this, we simply start at the centre point, repeatedly
355 * choose a random possibility out of the available ways to
356 * extend a used square into an unused one, and do it. After
357 * extending the third line out of a square, we remove the
358 * fourth from the possibilities list to avoid any full-cross
359 * squares (which would make the game too easy because they
360 * only have one orientation).
362 * The slightly worrying thing is the avoidance of full-cross
363 * squares. Can this cause our unsophisticated construction
364 * algorithm to paint itself into a corner, by getting into a
365 * situation where there are some unreached squares and the
366 * only way to reach any of them is to extend a T-piece into a
369 * Answer: no it can't, and here's a proof.
371 * Any contiguous group of such unreachable squares must be
372 * surrounded on _all_ sides by T-pieces pointing away from the
373 * group. (If not, then there is a square which can be extended
374 * into one of the `unreachable' ones, and so it wasn't
375 * unreachable after all.) In particular, this implies that
376 * each contiguous group of unreachable squares must be
377 * rectangular in shape (any deviation from that yields a
378 * non-T-piece next to an `unreachable' square).
380 * So we have a rectangle of unreachable squares, with T-pieces
381 * forming a solid border around the rectangle. The corners of
382 * that border must be connected (since every tile connects all
383 * the lines arriving in it), and therefore the border must
384 * form a closed loop around the rectangle.
386 * But this can't have happened in the first place, since we
387 * _know_ we've avoided creating closed loops! Hence, no such
388 * situation can ever arise, and the naive grid construction
389 * algorithm will guaranteeably result in a complete grid
390 * containing no unreached squares, no full crosses _and_ no
393 possibilities
= newtree234(xyd_cmp
);
396 add234(possibilities
, new_xyd(cx
, cy
, R
));
398 add234(possibilities
, new_xyd(cx
, cy
, U
));
400 add234(possibilities
, new_xyd(cx
, cy
, L
));
402 add234(possibilities
, new_xyd(cx
, cy
, D
));
404 while (count234(possibilities
) > 0) {
407 int x1
, y1
, d1
, x2
, y2
, d2
, d
;
410 * Extract a randomly chosen possibility from the list.
412 i
= random_upto(rs
, count234(possibilities
));
413 xyd
= delpos234(possibilities
, i
);
419 OFFSET(x2
, y2
, x1
, y1
, d1
, params
);
421 #ifdef GENERATION_DIAGNOSTICS
422 printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n",
423 x1
, y1
, "0RU3L567D9abcdef"[d1
], x2
, y2
, "0RU3L567D9abcdef"[d2
]);
427 * Make the connection. (We should be moving to an as yet
430 index(params
, tiles
, x1
, y1
) |= d1
;
431 assert(index(params
, tiles
, x2
, y2
) == 0);
432 index(params
, tiles
, x2
, y2
) |= d2
;
435 * If we have created a T-piece, remove its last
438 if (COUNT(index(params
, tiles
, x1
, y1
)) == 3) {
439 struct xyd xyd1
, *xydp
;
443 xyd1
.direction
= 0x0F ^ index(params
, tiles
, x1
, y1
);
445 xydp
= find234(possibilities
, &xyd1
, NULL
);
448 #ifdef GENERATION_DIAGNOSTICS
449 printf("T-piece; removing (%d,%d,%c)\n",
450 xydp
->x
, xydp
->y
, "0RU3L567D9abcdef"[xydp
->direction
]);
452 del234(possibilities
, xydp
);
458 * Remove all other possibilities that were pointing at the
459 * tile we've just moved into.
461 for (d
= 1; d
< 0x10; d
<<= 1) {
463 struct xyd xyd1
, *xydp
;
465 OFFSET(x3
, y3
, x2
, y2
, d
, params
);
472 xydp
= find234(possibilities
, &xyd1
, NULL
);
475 #ifdef GENERATION_DIAGNOSTICS
476 printf("Loop avoidance; removing (%d,%d,%c)\n",
477 xydp
->x
, xydp
->y
, "0RU3L567D9abcdef"[xydp
->direction
]);
479 del234(possibilities
, xydp
);
485 * Add new possibilities to the list for moving _out_ of
486 * the tile we have just moved into.
488 for (d
= 1; d
< 0x10; d
<<= 1) {
492 continue; /* we've got this one already */
494 if (!params
->wrapping
) {
495 if (d
== U
&& y2
== 0)
497 if (d
== D
&& y2
== h
-1)
499 if (d
== L
&& x2
== 0)
501 if (d
== R
&& x2
== w
-1)
505 OFFSET(x3
, y3
, x2
, y2
, d
, params
);
507 if (index(params
, tiles
, x3
, y3
))
508 continue; /* this would create a loop */
510 #ifdef GENERATION_DIAGNOSTICS
511 printf("New frontier; adding (%d,%d,%c)\n",
512 x2
, y2
, "0RU3L567D9abcdef"[d
]);
514 add234(possibilities
, new_xyd(x2
, y2
, d
));
517 /* Having done that, we should have no possibilities remaining. */
518 assert(count234(possibilities
) == 0);
519 freetree234(possibilities
);
522 * Now compute a list of the possible barrier locations.
524 barriertree
= newtree234(xyd_cmp
);
525 for (y
= 0; y
< h
; y
++) {
526 for (x
= 0; x
< w
; x
++) {
528 if (!(index(params
, tiles
, x
, y
) & R
) &&
529 (params
->wrapping
|| x
< w
-1))
530 add234(barriertree
, new_xyd(x
, y
, R
));
531 if (!(index(params
, tiles
, x
, y
) & D
) &&
532 (params
->wrapping
|| y
< h
-1))
533 add234(barriertree
, new_xyd(x
, y
, D
));
538 * Save the unshuffled grid. We do this using a separate
539 * reference-counted structure since it's a large chunk of
540 * memory which we don't want to have to replicate in every
541 * game state while playing.
544 game_aux_info
*solution
;
546 solution
= snew(game_aux_info
);
548 solution
->height
= h
;
549 solution
->tiles
= snewn(w
* h
, unsigned char);
550 memcpy(solution
->tiles
, tiles
, w
* h
);
556 * Now shuffle the grid.
557 * FIXME - this simply does a set of random moves to shuffle the pieces,
558 * although we make a token effort to avoid boring cases by avoiding moves
559 * that directly undo the previous one, or that repeat so often as to
560 * turn into fewer moves.
562 * A better way would be to number all the pieces, generate a placement
563 * for all the numbers as for "sixteen", observing parity constraints if
564 * neccessary, and then place the pieces according to their numbering.
565 * BUT - I'm not sure if this will work, since we disallow movement of
566 * the middle row and column.
572 int moves
= params
->movetarget
;
573 int prevdir
= -1, prevrowcol
= -1, nrepeats
= 0;
574 if (!moves
) moves
= cols
* rows
* 2;
575 for (i
= 0; i
< moves
; /* incremented conditionally */) {
576 /* Choose a direction: 0,1,2,3 = up, right, down, left. */
577 int dir
= random_upto(rs
, 4);
580 int col
= random_upto(rs
, cols
);
581 if (col
>= cx
) col
+= 1; /* avoid centre */
582 if (col
== prevrowcol
) {
583 if (dir
== 2-prevdir
)
584 continue; /* undoes last move */
585 else if (dir
== prevdir
&& (nrepeats
+1)*2 > h
)
586 continue; /* makes fewer moves */
588 slide_col_int(w
, h
, tiles
, 1 - dir
, col
);
591 int row
= random_upto(rs
, rows
);
592 if (row
>= cy
) row
+= 1; /* avoid centre */
593 if (row
== prevrowcol
) {
594 if (dir
== 4-prevdir
)
595 continue; /* undoes last move */
596 else if (dir
== prevdir
&& (nrepeats
+1)*2 > w
)
597 continue; /* makes fewer moves */
599 slide_row_int(w
, h
, tiles
, 2 - dir
, row
);
602 if (dir
== prevdir
&& rowcol
== prevrowcol
)
608 i
++; /* if we got here, the move was accepted */
613 * And now choose barrier locations. (We carefully do this
614 * _after_ shuffling, so that changing the barrier rate in the
615 * params while keeping the random seed the same will give the
616 * same shuffled grid and _only_ change the barrier locations.
617 * Also the way we choose barrier locations, by repeatedly
618 * choosing one possibility from the list until we have enough,
619 * is designed to ensure that raising the barrier rate while
620 * keeping the seed the same will provide a superset of the
621 * previous barrier set - i.e. if you ask for 10 barriers, and
622 * then decide that's still too hard and ask for 20, you'll get
623 * the original 10 plus 10 more, rather than getting 20 new
624 * ones and the chance of remembering your first 10.)
626 nbarriers
= (int)(params
->barrier_probability
* count234(barriertree
));
627 assert(nbarriers
>= 0 && nbarriers
<= count234(barriertree
));
629 while (nbarriers
> 0) {
632 int x1
, y1
, d1
, x2
, y2
, d2
;
635 * Extract a randomly chosen barrier from the list.
637 i
= random_upto(rs
, count234(barriertree
));
638 xyd
= delpos234(barriertree
, i
);
647 OFFSET(x2
, y2
, x1
, y1
, d1
, params
);
650 index(params
, barriers
, x1
, y1
) |= d1
;
651 index(params
, barriers
, x2
, y2
) |= d2
;
657 * Clean up the rest of the barrier list.
662 while ( (xyd
= delpos234(barriertree
, 0)) != NULL
)
665 freetree234(barriertree
);
669 * Finally, encode the grid into a string game description.
671 * My syntax is extremely simple: each square is encoded as a
672 * hex digit in which bit 0 means a connection on the right,
673 * bit 1 means up, bit 2 left and bit 3 down. (i.e. the same
674 * encoding as used internally). Each digit is followed by
675 * optional barrier indicators: `v' means a vertical barrier to
676 * the right of it, and `h' means a horizontal barrier below
679 desc
= snewn(w
* h
* 3 + 1, char);
681 for (y
= 0; y
< h
; y
++) {
682 for (x
= 0; x
< w
; x
++) {
683 *p
++ = "0123456789abcdef"[index(params
, tiles
, x
, y
)];
684 if ((params
->wrapping
|| x
< w
-1) &&
685 (index(params
, barriers
, x
, y
) & R
))
687 if ((params
->wrapping
|| y
< h
-1) &&
688 (index(params
, barriers
, x
, y
) & D
))
692 assert(p
- desc
<= w
*h
*3);
701 static void game_free_aux_info(game_aux_info
*aux
)
707 static char *validate_desc(game_params
*params
, char *desc
)
709 int w
= params
->width
, h
= params
->height
;
712 for (i
= 0; i
< w
*h
; i
++) {
713 if (*desc
>= '0' && *desc
<= '9')
715 else if (*desc
>= 'a' && *desc
<= 'f')
717 else if (*desc
>= 'A' && *desc
<= 'F')
720 return "Game description shorter than expected";
722 return "Game description contained unexpected character";
724 while (*desc
== 'h' || *desc
== 'v')
728 return "Game description longer than expected";
733 /* ----------------------------------------------------------------------
734 * Construct an initial game state, given a description and parameters.
737 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
742 assert(params
->width
> 0 && params
->height
> 0);
743 assert(params
->width
> 1 || params
->height
> 1);
746 * Create a blank game state.
748 state
= snew(game_state
);
749 w
= state
->width
= params
->width
;
750 h
= state
->height
= params
->height
;
751 state
->cx
= state
->width
/ 2;
752 state
->cy
= state
->height
/ 2;
753 state
->wrapping
= params
->wrapping
;
754 state
->movetarget
= params
->movetarget
;
755 state
->completed
= 0;
756 state
->used_solve
= state
->just_used_solve
= FALSE
;
757 state
->move_count
= 0;
758 state
->last_move_row
= -1;
759 state
->last_move_col
= -1;
760 state
->last_move_dir
= 0;
761 state
->tiles
= snewn(state
->width
* state
->height
, unsigned char);
762 memset(state
->tiles
, 0, state
->width
* state
->height
);
763 state
->barriers
= snewn(state
->width
* state
->height
, unsigned char);
764 memset(state
->barriers
, 0, state
->width
* state
->height
);
768 * Parse the game description into the grid.
770 for (y
= 0; y
< h
; y
++) {
771 for (x
= 0; x
< w
; x
++) {
772 if (*desc
>= '0' && *desc
<= '9')
773 tile(state
, x
, y
) = *desc
- '0';
774 else if (*desc
>= 'a' && *desc
<= 'f')
775 tile(state
, x
, y
) = *desc
- 'a' + 10;
776 else if (*desc
>= 'A' && *desc
<= 'F')
777 tile(state
, x
, y
) = *desc
- 'A' + 10;
780 while (*desc
== 'h' || *desc
== 'v') {
787 OFFSET(x2
, y2
, x
, y
, d1
, state
);
790 barrier(state
, x
, y
) |= d1
;
791 barrier(state
, x2
, y2
) |= d2
;
799 * Set up border barriers if this is a non-wrapping game.
801 if (!state
->wrapping
) {
802 for (x
= 0; x
< state
->width
; x
++) {
803 barrier(state
, x
, 0) |= U
;
804 barrier(state
, x
, state
->height
-1) |= D
;
806 for (y
= 0; y
< state
->height
; y
++) {
807 barrier(state
, 0, y
) |= L
;
808 barrier(state
, state
->width
-1, y
) |= R
;
813 * Set up the barrier corner flags, for drawing barriers
814 * prettily when they meet.
816 for (y
= 0; y
< state
->height
; y
++) {
817 for (x
= 0; x
< state
->width
; x
++) {
820 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
822 int x1
, y1
, x2
, y2
, x3
, y3
;
825 if (!(barrier(state
, x
, y
) & dir
))
828 if (barrier(state
, x
, y
) & dir2
)
831 x1
= x
+ X(dir
), y1
= y
+ Y(dir
);
832 if (x1
>= 0 && x1
< state
->width
&&
833 y1
>= 0 && y1
< state
->height
&&
834 (barrier(state
, x1
, y1
) & dir2
))
837 x2
= x
+ X(dir2
), y2
= y
+ Y(dir2
);
838 if (x2
>= 0 && x2
< state
->width
&&
839 y2
>= 0 && y2
< state
->height
&&
840 (barrier(state
, x2
, y2
) & dir
))
844 barrier(state
, x
, y
) |= (dir
<< 4);
845 if (x1
>= 0 && x1
< state
->width
&&
846 y1
>= 0 && y1
< state
->height
)
847 barrier(state
, x1
, y1
) |= (A(dir
) << 4);
848 if (x2
>= 0 && x2
< state
->width
&&
849 y2
>= 0 && y2
< state
->height
)
850 barrier(state
, x2
, y2
) |= (C(dir
) << 4);
851 x3
= x
+ X(dir
) + X(dir2
), y3
= y
+ Y(dir
) + Y(dir2
);
852 if (x3
>= 0 && x3
< state
->width
&&
853 y3
>= 0 && y3
< state
->height
)
854 barrier(state
, x3
, y3
) |= (F(dir
) << 4);
863 static game_state
*dup_game(game_state
*state
)
867 ret
= snew(game_state
);
868 ret
->width
= state
->width
;
869 ret
->height
= state
->height
;
872 ret
->wrapping
= state
->wrapping
;
873 ret
->movetarget
= state
->movetarget
;
874 ret
->completed
= state
->completed
;
875 ret
->used_solve
= state
->used_solve
;
876 ret
->just_used_solve
= state
->just_used_solve
;
877 ret
->move_count
= state
->move_count
;
878 ret
->last_move_row
= state
->last_move_row
;
879 ret
->last_move_col
= state
->last_move_col
;
880 ret
->last_move_dir
= state
->last_move_dir
;
881 ret
->tiles
= snewn(state
->width
* state
->height
, unsigned char);
882 memcpy(ret
->tiles
, state
->tiles
, state
->width
* state
->height
);
883 ret
->barriers
= snewn(state
->width
* state
->height
, unsigned char);
884 memcpy(ret
->barriers
, state
->barriers
, state
->width
* state
->height
);
889 static void free_game(game_state
*state
)
892 sfree(state
->barriers
);
896 static game_state
*solve_game(game_state
*state
, game_state
*currstate
,
897 game_aux_info
*aux
, char **error
)
902 *error
= "Solution not known for this puzzle";
906 assert(aux
->width
== state
->width
);
907 assert(aux
->height
== state
->height
);
908 ret
= dup_game(state
);
909 memcpy(ret
->tiles
, aux
->tiles
, ret
->width
* ret
->height
);
910 ret
->used_solve
= ret
->just_used_solve
= TRUE
;
911 ret
->completed
= ret
->move_count
= 1;
916 static char *game_text_format(game_state
*state
)
921 /* ----------------------------------------------------------------------
926 * Compute which squares are reachable from the centre square, as a
927 * quick visual aid to determining how close the game is to
928 * completion. This is also a simple way to tell if the game _is_
929 * completed - just call this function and see whether every square
932 * squares in the moving_row and moving_col are always inactive - this
933 * is so that "current" doesn't appear to jump across moving lines.
935 static unsigned char *compute_active(game_state
*state
,
936 int moving_row
, int moving_col
)
938 unsigned char *active
;
942 active
= snewn(state
->width
* state
->height
, unsigned char);
943 memset(active
, 0, state
->width
* state
->height
);
946 * We only store (x,y) pairs in todo, but it's easier to reuse
947 * xyd_cmp and just store direction 0 every time.
949 todo
= newtree234(xyd_cmp
);
950 index(state
, active
, state
->cx
, state
->cy
) = ACTIVE
;
951 add234(todo
, new_xyd(state
->cx
, state
->cy
, 0));
953 while ( (xyd
= delpos234(todo
, 0)) != NULL
) {
954 int x1
, y1
, d1
, x2
, y2
, d2
;
960 for (d1
= 1; d1
< 0x10; d1
<<= 1) {
961 OFFSET(x2
, y2
, x1
, y1
, d1
, state
);
965 * If the next tile in this direction is connected to
966 * us, and there isn't a barrier in the way, and it
967 * isn't already marked active, then mark it active and
968 * add it to the to-examine list.
970 if ((x2
!= moving_col
&& y2
!= moving_row
) &&
971 (tile(state
, x1
, y1
) & d1
) &&
972 (tile(state
, x2
, y2
) & d2
) &&
973 !(barrier(state
, x1
, y1
) & d1
) &&
974 !index(state
, active
, x2
, y2
)) {
975 index(state
, active
, x2
, y2
) = ACTIVE
;
976 add234(todo
, new_xyd(x2
, y2
, 0));
980 /* Now we expect the todo list to have shrunk to zero size. */
981 assert(count234(todo
) == 0);
992 static game_ui
*new_ui(game_state
*state
)
994 game_ui
*ui
= snew(game_ui
);
995 ui
->cur_x
= state
->width
/ 2;
996 ui
->cur_y
= state
->height
/ 2;
997 ui
->cur_visible
= FALSE
;
1002 static void free_ui(game_ui
*ui
)
1007 /* ----------------------------------------------------------------------
1011 static void slide_row_int(int w
, int h
, unsigned char *tiles
, int dir
, int row
)
1013 int x
= dir
> 0 ?
-1 : w
;
1016 unsigned char endtile
= tiles
[row
* w
+ tx
];
1019 tx
= (x
+ dir
+ w
) % w
;
1020 tiles
[row
* w
+ x
] = tiles
[row
* w
+ tx
];
1022 tiles
[row
* w
+ tx
] = endtile
;
1025 static void slide_col_int(int w
, int h
, unsigned char *tiles
, int dir
, int col
)
1027 int y
= dir
> 0 ?
-1 : h
;
1030 unsigned char endtile
= tiles
[ty
* w
+ col
];
1033 ty
= (y
+ dir
+ h
) % h
;
1034 tiles
[y
* w
+ col
] = tiles
[ty
* w
+ col
];
1036 tiles
[ty
* w
+ col
] = endtile
;
1039 static void slide_row(game_state
*state
, int dir
, int row
)
1041 slide_row_int(state
->width
, state
->height
, state
->tiles
, dir
, row
);
1044 static void slide_col(game_state
*state
, int dir
, int col
)
1046 slide_col_int(state
->width
, state
->height
, state
->tiles
, dir
, col
);
1049 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
1050 game_state
*newstate
)
1054 struct game_drawstate
{
1058 unsigned char *visible
;
1061 static game_state
*make_move(game_state
*state
, game_ui
*ui
,
1062 game_drawstate
*ds
, int x
, int y
, int button
)
1068 button
&= ~MOD_MASK
;
1070 if (button
!= LEFT_BUTTON
&& button
!= RIGHT_BUTTON
)
1073 cx
= (x
- (BORDER
+ WINDOW_OFFSET
+ TILE_BORDER
) + 2*TILE_SIZE
) / TILE_SIZE
- 2;
1074 cy
= (y
- (BORDER
+ WINDOW_OFFSET
+ TILE_BORDER
) + 2*TILE_SIZE
) / TILE_SIZE
- 2;
1076 if (cy
>= 0 && cy
< state
->height
&& cy
!= state
->cy
)
1078 if (cx
== -1) dx
= +1;
1079 else if (cx
== state
->width
) dx
= -1;
1084 else if (cx
>= 0 && cx
< state
->width
&& cx
!= state
->cx
)
1086 if (cy
== -1) dy
= +1;
1087 else if (cy
== state
->height
) dy
= -1;
1095 /* reverse direction if right hand button is pressed */
1096 if (button
== RIGHT_BUTTON
)
1102 ret
= dup_game(state
);
1103 ret
->just_used_solve
= FALSE
;
1105 if (dx
== 0) slide_col(ret
, dy
, cx
);
1106 else slide_row(ret
, dx
, cy
);
1109 ret
->last_move_row
= dx ? cy
: -1;
1110 ret
->last_move_col
= dx ?
-1 : cx
;
1111 ret
->last_move_dir
= dx
+ dy
;
1114 * See if the game has been completed.
1116 if (!ret
->completed
) {
1117 unsigned char *active
= compute_active(ret
, -1, -1);
1119 int complete
= TRUE
;
1121 for (x1
= 0; x1
< ret
->width
; x1
++)
1122 for (y1
= 0; y1
< ret
->height
; y1
++)
1123 if (!index(ret
, active
, x1
, y1
)) {
1125 goto break_label
; /* break out of two loops at once */
1132 ret
->completed
= ret
->move_count
;
1138 /* ----------------------------------------------------------------------
1139 * Routines for drawing the game position on the screen.
1142 static game_drawstate
*game_new_drawstate(game_state
*state
)
1144 game_drawstate
*ds
= snew(game_drawstate
);
1146 ds
->started
= FALSE
;
1147 ds
->width
= state
->width
;
1148 ds
->height
= state
->height
;
1149 ds
->visible
= snewn(state
->width
* state
->height
, unsigned char);
1150 ds
->tilesize
= 0; /* not decided yet */
1151 memset(ds
->visible
, 0xFF, state
->width
* state
->height
);
1156 static void game_free_drawstate(game_drawstate
*ds
)
1162 static void game_size(game_params
*params
, game_drawstate
*ds
, int *x
, int *y
,
1167 * Each window dimension equals the tile size times two more
1168 * than the grid dimension (the border containing the arrows is
1169 * the same width as the tiles), plus TILE_BORDER, plus twice
1172 tsx
= (*x
- 2*WINDOW_OFFSET
- TILE_BORDER
) / (params
->width
+ 2);
1173 tsy
= (*y
- 2*WINDOW_OFFSET
- TILE_BORDER
) / (params
->height
+ 2);
1179 ds
->tilesize
= min(ts
, PREFERRED_TILE_SIZE
);
1181 *x
= BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* params
->width
+ TILE_BORDER
;
1182 *y
= BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* params
->height
+ TILE_BORDER
;
1185 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1189 ret
= snewn(NCOLOURS
* 3, float);
1190 *ncolours
= NCOLOURS
;
1193 * Basic background colour is whatever the front end thinks is
1194 * a sensible default.
1196 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1201 ret
[COL_WIRE
* 3 + 0] = 0.0F
;
1202 ret
[COL_WIRE
* 3 + 1] = 0.0F
;
1203 ret
[COL_WIRE
* 3 + 2] = 0.0F
;
1206 * Powered wires and powered endpoints are cyan.
1208 ret
[COL_POWERED
* 3 + 0] = 0.0F
;
1209 ret
[COL_POWERED
* 3 + 1] = 1.0F
;
1210 ret
[COL_POWERED
* 3 + 2] = 1.0F
;
1215 ret
[COL_BARRIER
* 3 + 0] = 1.0F
;
1216 ret
[COL_BARRIER
* 3 + 1] = 0.0F
;
1217 ret
[COL_BARRIER
* 3 + 2] = 0.0F
;
1220 * Unpowered endpoints are blue.
1222 ret
[COL_ENDPOINT
* 3 + 0] = 0.0F
;
1223 ret
[COL_ENDPOINT
* 3 + 1] = 0.0F
;
1224 ret
[COL_ENDPOINT
* 3 + 2] = 1.0F
;
1227 * Tile borders are a darker grey than the background.
1229 ret
[COL_BORDER
* 3 + 0] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 0];
1230 ret
[COL_BORDER
* 3 + 1] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 1];
1231 ret
[COL_BORDER
* 3 + 2] = 0.5F
* ret
[COL_BACKGROUND
* 3 + 2];
1234 * Flashing tiles are a grey in between those two.
1236 ret
[COL_FLASHING
* 3 + 0] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 0];
1237 ret
[COL_FLASHING
* 3 + 1] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 1];
1238 ret
[COL_FLASHING
* 3 + 2] = 0.75F
* ret
[COL_BACKGROUND
* 3 + 2];
1240 ret
[COL_LOWLIGHT
* 3 + 0] = ret
[COL_BACKGROUND
* 3 + 0] * 0.8F
;
1241 ret
[COL_LOWLIGHT
* 3 + 1] = ret
[COL_BACKGROUND
* 3 + 1] * 0.8F
;
1242 ret
[COL_LOWLIGHT
* 3 + 2] = ret
[COL_BACKGROUND
* 3 + 2] * 0.8F
;
1243 ret
[COL_TEXT
* 3 + 0] = 0.0;
1244 ret
[COL_TEXT
* 3 + 1] = 0.0;
1245 ret
[COL_TEXT
* 3 + 2] = 0.0;
1250 static void draw_thick_line(frontend
*fe
, int x1
, int y1
, int x2
, int y2
,
1253 draw_line(fe
, x1
-1, y1
, x2
-1, y2
, COL_WIRE
);
1254 draw_line(fe
, x1
+1, y1
, x2
+1, y2
, COL_WIRE
);
1255 draw_line(fe
, x1
, y1
-1, x2
, y2
-1, COL_WIRE
);
1256 draw_line(fe
, x1
, y1
+1, x2
, y2
+1, COL_WIRE
);
1257 draw_line(fe
, x1
, y1
, x2
, y2
, colour
);
1260 static void draw_rect_coords(frontend
*fe
, int x1
, int y1
, int x2
, int y2
,
1263 int mx
= (x1
< x2 ? x1
: x2
);
1264 int my
= (y1
< y2 ? y1
: y2
);
1265 int dx
= (x2
+ x1
- 2*mx
+ 1);
1266 int dy
= (y2
+ y1
- 2*my
+ 1);
1268 draw_rect(fe
, mx
, my
, dx
, dy
, colour
);
1271 static void draw_barrier_corner(frontend
*fe
, game_drawstate
*ds
,
1272 int x
, int y
, int dir
, int phase
)
1274 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
;
1275 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
;
1276 int x1
, y1
, dx
, dy
, dir2
;
1281 dx
= X(dir
) + X(dir2
);
1282 dy
= Y(dir
) + Y(dir2
);
1283 x1
= (dx
> 0 ? TILE_SIZE
+TILE_BORDER
-1 : 0);
1284 y1
= (dy
> 0 ? TILE_SIZE
+TILE_BORDER
-1 : 0);
1287 draw_rect_coords(fe
, bx
+x1
, by
+y1
,
1288 bx
+x1
-TILE_BORDER
*dx
, by
+y1
-(TILE_BORDER
-1)*dy
,
1290 draw_rect_coords(fe
, bx
+x1
, by
+y1
,
1291 bx
+x1
-(TILE_BORDER
-1)*dx
, by
+y1
-TILE_BORDER
*dy
,
1294 draw_rect_coords(fe
, bx
+x1
, by
+y1
,
1295 bx
+x1
-(TILE_BORDER
-1)*dx
, by
+y1
-(TILE_BORDER
-1)*dy
,
1300 static void draw_barrier(frontend
*fe
, game_drawstate
*ds
,
1301 int x
, int y
, int dir
, int phase
)
1303 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
;
1304 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
;
1307 x1
= (X(dir
) > 0 ? TILE_SIZE
: X(dir
) == 0 ? TILE_BORDER
: 0);
1308 y1
= (Y(dir
) > 0 ? TILE_SIZE
: Y(dir
) == 0 ? TILE_BORDER
: 0);
1309 w
= (X(dir
) ? TILE_BORDER
: TILE_SIZE
- TILE_BORDER
);
1310 h
= (Y(dir
) ? TILE_BORDER
: TILE_SIZE
- TILE_BORDER
);
1313 draw_rect(fe
, bx
+x1
-X(dir
), by
+y1
-Y(dir
), w
, h
, COL_WIRE
);
1315 draw_rect(fe
, bx
+x1
, by
+y1
, w
, h
, COL_BARRIER
);
1319 static void draw_tile(frontend
*fe
, game_drawstate
*ds
, game_state
*state
,
1320 int x
, int y
, int tile
, float xshift
, float yshift
)
1322 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
+ (xshift
* TILE_SIZE
);
1323 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
+ (yshift
* TILE_SIZE
);
1324 float cx
, cy
, ex
, ey
;
1328 * When we draw a single tile, we must draw everything up to
1329 * and including the borders around the tile. This means that
1330 * if the neighbouring tiles have connections to those borders,
1331 * we must draw those connections on the borders themselves.
1333 * This would be terribly fiddly if we ever had to draw a tile
1334 * while its neighbour was in mid-rotate, because we'd have to
1335 * arrange to _know_ that the neighbour was being rotated and
1336 * hence had an anomalous effect on the redraw of this tile.
1337 * Fortunately, the drawing algorithm avoids ever calling us in
1338 * this circumstance: we're either drawing lots of straight
1339 * tiles at game start or after a move is complete, or we're
1340 * repeatedly drawing only the rotating tile. So no problem.
1344 * So. First blank the tile out completely: draw a big
1345 * rectangle in border colour, and a smaller rectangle in
1346 * background colour to fill it in.
1348 draw_rect(fe
, bx
, by
, TILE_SIZE
+TILE_BORDER
, TILE_SIZE
+TILE_BORDER
,
1350 draw_rect(fe
, bx
+TILE_BORDER
, by
+TILE_BORDER
,
1351 TILE_SIZE
-TILE_BORDER
, TILE_SIZE
-TILE_BORDER
,
1352 tile
& FLASHING ? COL_FLASHING
: COL_BACKGROUND
);
1357 cx
= cy
= TILE_BORDER
+ (TILE_SIZE
-TILE_BORDER
) / 2.0F
- 0.5F
;
1358 col
= (tile
& ACTIVE ? COL_POWERED
: COL_WIRE
);
1359 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
1361 ex
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* X(dir
);
1362 ey
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* Y(dir
);
1363 draw_thick_line(fe
, bx
+(int)cx
, by
+(int)cy
,
1364 bx
+(int)(cx
+ex
), by
+(int)(cy
+ey
),
1368 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
1370 ex
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* X(dir
);
1371 ey
= (TILE_SIZE
- TILE_BORDER
- 1.0F
) / 2.0F
* Y(dir
);
1372 draw_line(fe
, bx
+(int)cx
, by
+(int)cy
,
1373 bx
+(int)(cx
+ex
), by
+(int)(cy
+ey
), col
);
1378 * Draw the box in the middle. We do this in blue if the tile
1379 * is an unpowered endpoint, in cyan if the tile is a powered
1380 * endpoint, in black if the tile is the centrepiece, and
1381 * otherwise not at all.
1384 if (x
== state
->cx
&& y
== state
->cy
)
1386 else if (COUNT(tile
) == 1) {
1387 col
= (tile
& ACTIVE ? COL_POWERED
: COL_ENDPOINT
);
1392 points
[0] = +1; points
[1] = +1;
1393 points
[2] = +1; points
[3] = -1;
1394 points
[4] = -1; points
[5] = -1;
1395 points
[6] = -1; points
[7] = +1;
1397 for (i
= 0; i
< 8; i
+= 2) {
1398 ex
= (TILE_SIZE
* 0.24F
) * points
[i
];
1399 ey
= (TILE_SIZE
* 0.24F
) * points
[i
+1];
1400 points
[i
] = bx
+(int)(cx
+ex
);
1401 points
[i
+1] = by
+(int)(cy
+ey
);
1404 draw_polygon(fe
, points
, 4, TRUE
, col
);
1405 draw_polygon(fe
, points
, 4, FALSE
, COL_WIRE
);
1409 * Draw the points on the border if other tiles are connected
1412 for (dir
= 1; dir
< 0x10; dir
<<= 1) {
1413 int dx
, dy
, px
, py
, lx
, ly
, vx
, vy
, ox
, oy
;
1421 if (ox
< 0 || ox
>= state
->width
|| oy
< 0 || oy
>= state
->height
)
1424 if (!(tile(state
, ox
, oy
) & F(dir
)))
1427 px
= bx
+ (int)(dx
>0 ? TILE_SIZE
+ TILE_BORDER
- 1 : dx
<0 ?
0 : cx
);
1428 py
= by
+ (int)(dy
>0 ? TILE_SIZE
+ TILE_BORDER
- 1 : dy
<0 ?
0 : cy
);
1429 lx
= dx
* (TILE_BORDER
-1);
1430 ly
= dy
* (TILE_BORDER
-1);
1434 if (xshift
== 0.0 && yshift
== 0.0 && (tile
& dir
)) {
1436 * If we are fully connected to the other tile, we must
1437 * draw right across the tile border. (We can use our
1438 * own ACTIVE state to determine what colour to do this
1439 * in: if we are fully connected to the other tile then
1440 * the two ACTIVE states will be the same.)
1442 draw_rect_coords(fe
, px
-vx
, py
-vy
, px
+lx
+vx
, py
+ly
+vy
, COL_WIRE
);
1443 draw_rect_coords(fe
, px
, py
, px
+lx
, py
+ly
,
1444 (tile
& ACTIVE
) ? COL_POWERED
: COL_WIRE
);
1447 * The other tile extends into our border, but isn't
1448 * actually connected to us. Just draw a single black
1451 draw_rect_coords(fe
, px
, py
, px
, py
, COL_WIRE
);
1455 draw_update(fe
, bx
, by
, TILE_SIZE
+TILE_BORDER
, TILE_SIZE
+TILE_BORDER
);
1458 static void draw_tile_barriers(frontend
*fe
, game_drawstate
*ds
,
1459 game_state
*state
, int x
, int y
)
1463 int bx
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* x
;
1464 int by
= BORDER
+ WINDOW_OFFSET
+ TILE_SIZE
* y
;
1466 * Draw barrier corners, and then barriers.
1468 for (phase
= 0; phase
< 2; phase
++) {
1469 for (dir
= 1; dir
< 0x10; dir
<<= 1)
1470 if (barrier(state
, x
, y
) & (dir
<< 4))
1471 draw_barrier_corner(fe
, ds
, x
, y
, dir
<< 4, phase
);
1472 for (dir
= 1; dir
< 0x10; dir
<<= 1)
1473 if (barrier(state
, x
, y
) & dir
)
1474 draw_barrier(fe
, ds
, x
, y
, dir
, phase
);
1477 draw_update(fe
, bx
, by
, TILE_SIZE
+TILE_BORDER
, TILE_SIZE
+TILE_BORDER
);
1480 static void draw_arrow(frontend
*fe
, game_drawstate
*ds
,
1481 int x
, int y
, int xdx
, int xdy
)
1484 int ydy
= -xdx
, ydx
= xdy
;
1486 x
= x
* TILE_SIZE
+ BORDER
+ WINDOW_OFFSET
;
1487 y
= y
* TILE_SIZE
+ BORDER
+ WINDOW_OFFSET
;
1489 #define POINT(n, xx, yy) ( \
1490 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
1491 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
1493 POINT(0, TILE_SIZE
/ 2, 3 * TILE_SIZE
/ 4); /* top of arrow */
1494 POINT(1, 3 * TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* right corner */
1495 POINT(2, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* right concave */
1496 POINT(3, 5 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom right */
1497 POINT(4, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 4); /* bottom left */
1498 POINT(5, 3 * TILE_SIZE
/ 8, TILE_SIZE
/ 2); /* left concave */
1499 POINT(6, TILE_SIZE
/ 4, TILE_SIZE
/ 2); /* left corner */
1501 draw_polygon(fe
, coords
, 7, TRUE
, COL_LOWLIGHT
);
1502 draw_polygon(fe
, coords
, 7, FALSE
, COL_TEXT
);
1505 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1506 game_state
*state
, int dir
, game_ui
*ui
, float t
, float ft
)
1508 int x
, y
, tx
, ty
, frame
;
1509 unsigned char *active
;
1514 * Clear the screen and draw the exterior barrier lines if this
1515 * is our first call.
1523 BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* state
->width
+ TILE_BORDER
,
1524 BORDER
* 2 + WINDOW_OFFSET
* 2 + TILE_SIZE
* state
->height
+ TILE_BORDER
,
1526 draw_update(fe
, 0, 0,
1527 BORDER
* 2 + WINDOW_OFFSET
*2 + TILE_SIZE
*state
->width
+ TILE_BORDER
,
1528 BORDER
* 2 + WINDOW_OFFSET
*2 + TILE_SIZE
*state
->height
+ TILE_BORDER
);
1530 for (phase
= 0; phase
< 2; phase
++) {
1532 for (x
= 0; x
< ds
->width
; x
++) {
1533 if (barrier(state
, x
, 0) & UL
)
1534 draw_barrier_corner(fe
, ds
, x
, -1, LD
, phase
);
1535 if (barrier(state
, x
, 0) & RU
)
1536 draw_barrier_corner(fe
, ds
, x
, -1, DR
, phase
);
1537 if (barrier(state
, x
, 0) & U
)
1538 draw_barrier(fe
, ds
, x
, -1, D
, phase
);
1539 if (barrier(state
, x
, ds
->height
-1) & DR
)
1540 draw_barrier_corner(fe
, ds
, x
, ds
->height
, RU
, phase
);
1541 if (barrier(state
, x
, ds
->height
-1) & LD
)
1542 draw_barrier_corner(fe
, ds
, x
, ds
->height
, UL
, phase
);
1543 if (barrier(state
, x
, ds
->height
-1) & D
)
1544 draw_barrier(fe
, ds
, x
, ds
->height
, U
, phase
);
1547 for (y
= 0; y
< ds
->height
; y
++) {
1548 if (barrier(state
, 0, y
) & UL
)
1549 draw_barrier_corner(fe
, ds
, -1, y
, RU
, phase
);
1550 if (barrier(state
, 0, y
) & LD
)
1551 draw_barrier_corner(fe
, ds
, -1, y
, DR
, phase
);
1552 if (barrier(state
, 0, y
) & L
)
1553 draw_barrier(fe
, ds
, -1, y
, R
, phase
);
1554 if (barrier(state
, ds
->width
-1, y
) & RU
)
1555 draw_barrier_corner(fe
, ds
, ds
->width
, y
, UL
, phase
);
1556 if (barrier(state
, ds
->width
-1, y
) & DR
)
1557 draw_barrier_corner(fe
, ds
, ds
->width
, y
, LD
, phase
);
1558 if (barrier(state
, ds
->width
-1, y
) & R
)
1559 draw_barrier(fe
, ds
, ds
->width
, y
, L
, phase
);
1564 * Arrows for making moves.
1566 for (x
= 0; x
< ds
->width
; x
++) {
1567 if (x
== state
->cx
) continue;
1568 draw_arrow(fe
, ds
, x
, 0, +1, 0);
1569 draw_arrow(fe
, ds
, x
+1, ds
->height
, -1, 0);
1571 for (y
= 0; y
< ds
->height
; y
++) {
1572 if (y
== state
->cy
) continue;
1573 draw_arrow(fe
, ds
, ds
->width
, y
, 0, +1);
1574 draw_arrow(fe
, ds
, 0, y
+1, 0, -1);
1578 /* Check if this is an undo. If so, we will need to run any animation
1581 if (oldstate
&& oldstate
->move_count
> state
->move_count
) {
1582 game_state
* tmpstate
= state
;
1584 oldstate
= tmpstate
;
1589 if (oldstate
&& (t
< ANIM_TIME
)) {
1591 * We're animating a slide, of row/column number
1592 * state->last_move_pos, in direction
1593 * state->last_move_dir
1595 xshift
= state
->last_move_row
== -1 ?
0.0 :
1596 (1 - t
/ ANIM_TIME
) * state
->last_move_dir
;
1597 yshift
= state
->last_move_col
== -1 ?
0.0 :
1598 (1 - t
/ ANIM_TIME
) * state
->last_move_dir
;
1604 * We're animating a completion flash. Find which frame
1607 frame
= (int)(ft
/ FLASH_FRAME
);
1611 * Draw any tile which differs from the way it was last drawn.
1613 if (xshift
!= 0.0 || yshift
!= 0.0) {
1614 active
= compute_active(state
,
1615 state
->last_move_row
, state
->last_move_col
);
1617 active
= compute_active(state
, -1, -1);
1621 BORDER
+ WINDOW_OFFSET
, BORDER
+ WINDOW_OFFSET
,
1622 TILE_SIZE
* state
->width
+ TILE_BORDER
,
1623 TILE_SIZE
* state
->height
+ TILE_BORDER
);
1625 for (x
= 0; x
< ds
->width
; x
++)
1626 for (y
= 0; y
< ds
->height
; y
++) {
1627 unsigned char c
= tile(state
, x
, y
) | index(state
, active
, x
, y
);
1630 * In a completion flash, we adjust the FLASHING bit
1631 * depending on our distance from the centre point and
1635 int xdist
, ydist
, dist
;
1636 xdist
= (x
< state
->cx ? state
->cx
- x
: x
- state
->cx
);
1637 ydist
= (y
< state
->cy ? state
->cy
- y
: y
- state
->cy
);
1638 dist
= (xdist
> ydist ? xdist
: ydist
);
1640 if (frame
>= dist
&& frame
< dist
+4) {
1641 int flash
= (frame
- dist
) & 1;
1642 flash
= flash ? FLASHING
: 0;
1643 c
= (c
&~ FLASHING
) | flash
;
1647 if (index(state
, ds
->visible
, x
, y
) != c
||
1648 index(state
, ds
->visible
, x
, y
) == 0xFF ||
1649 (x
== state
->last_move_col
|| y
== state
->last_move_row
))
1651 float xs
= (y
== state
->last_move_row ? xshift
: 0.0);
1652 float ys
= (x
== state
->last_move_col ? yshift
: 0.0);
1654 draw_tile(fe
, ds
, state
, x
, y
, c
, xs
, ys
);
1655 if (xs
< 0 && x
== 0)
1656 draw_tile(fe
, ds
, state
, state
->width
, y
, c
, xs
, ys
);
1657 else if (xs
> 0 && x
== state
->width
- 1)
1658 draw_tile(fe
, ds
, state
, -1, y
, c
, xs
, ys
);
1659 else if (ys
< 0 && y
== 0)
1660 draw_tile(fe
, ds
, state
, x
, state
->height
, c
, xs
, ys
);
1661 else if (ys
> 0 && y
== state
->height
- 1)
1662 draw_tile(fe
, ds
, state
, x
, -1, c
, xs
, ys
);
1664 if (x
== state
->last_move_col
|| y
== state
->last_move_row
)
1665 index(state
, ds
->visible
, x
, y
) = 0xFF;
1667 index(state
, ds
->visible
, x
, y
) = c
;
1671 for (x
= 0; x
< ds
->width
; x
++)
1672 for (y
= 0; y
< ds
->height
; y
++)
1673 draw_tile_barriers(fe
, ds
, state
, x
, y
);
1678 * Update the status bar.
1681 char statusbuf
[256];
1684 n
= state
->width
* state
->height
;
1685 for (i
= a
= 0; i
< n
; i
++)
1689 if (state
->used_solve
)
1690 sprintf(statusbuf
, "Moves since auto-solve: %d",
1691 state
->move_count
- state
->completed
);
1693 sprintf(statusbuf
, "%sMoves: %d",
1694 (state
->completed ?
"COMPLETED! " : ""),
1695 (state
->completed ? state
->completed
: state
->move_count
));
1697 if (state
->movetarget
)
1698 sprintf(statusbuf
+ strlen(statusbuf
), " (target %d)",
1701 sprintf(statusbuf
+ strlen(statusbuf
), " Active: %d/%d", a
, n
);
1703 status_bar(fe
, statusbuf
);
1709 static float game_anim_length(game_state
*oldstate
,
1710 game_state
*newstate
, int dir
, game_ui
*ui
)
1713 * Don't animate an auto-solve move.
1715 if ((dir
> 0 && newstate
->just_used_solve
) ||
1716 (dir
< 0 && oldstate
->just_used_solve
))
1722 static float game_flash_length(game_state
*oldstate
,
1723 game_state
*newstate
, int dir
, game_ui
*ui
)
1726 * If the game has just been completed, we display a completion
1729 if (!oldstate
->completed
&& newstate
->completed
&&
1730 !oldstate
->used_solve
&& !newstate
->used_solve
) {
1733 if (size
< newstate
->cx
+1)
1734 size
= newstate
->cx
+1;
1735 if (size
< newstate
->cy
+1)
1736 size
= newstate
->cy
+1;
1737 if (size
< newstate
->width
- newstate
->cx
)
1738 size
= newstate
->width
- newstate
->cx
;
1739 if (size
< newstate
->height
- newstate
->cy
)
1740 size
= newstate
->height
- newstate
->cy
;
1741 return FLASH_FRAME
* (size
+4);
1747 static int game_wants_statusbar(void)
1752 static int game_timing_state(game_state
*state
)
1758 #define thegame netslide
1761 const struct game thegame
= {
1762 "Netslide", "games.netslide",
1769 TRUE
, game_configure
, custom_params
,
1778 FALSE
, game_text_format
,
1786 game_free_drawstate
,
1790 game_wants_statusbar
,
1791 FALSE
, game_timing_state
,
1792 0, /* mouse_priorities */