2 * netslide.c: cross between Net and Sixteen, courtesy of Richard
16 #define PI 3.141592653589793238462643383279502884197169399
18 #define MATMUL(xr,yr,m,x,y) do { \
19 float rx, ry, xx = (x), yy = (y), *mat = (m); \
20 rx = mat[0] * xx + mat[2] * yy; \
21 ry = mat[1] * xx + mat[3] * yy; \
22 (xr) = rx; (yr) = ry; \
25 /* Direction and other bitfields */
32 /* Corner flags go in the barriers array */
38 /* Get tile at given coordinate */
39 #define T(state, x, y) ( (y) * (state)->width + (x) )
41 /* Rotations: Anticlockwise, Clockwise, Flip, general rotate */
42 #define A(x) ( (((x) & 0x07) << 1) | (((x) & 0x08) >> 3) )
43 #define C(x) ( (((x) & 0x0E) >> 1) | (((x) & 0x01) << 3) )
44 #define F(x) ( (((x) & 0x0C) >> 2) | (((x) & 0x03) << 2) )
45 #define ROT(x, n) ( ((n)&3) == 0 ? (x) : \
46 ((n)&3) == 1 ? A(x) : \
47 ((n)&3) == 2 ? F(x) : C(x) )
49 /* X and Y displacements */
50 #define X(x) ( (x) == R ? +1 : (x) == L ? -1 : 0 )
51 #define Y(x) ( (x) == D ? +1 : (x) == U ? -1 : 0 )
54 #define COUNT(x) ( (((x) & 0x08) >> 3) + (((x) & 0x04) >> 2) + \
55 (((x) & 0x02) >> 1) + ((x) & 0x01) )
58 #define BORDER TILE_SIZE
60 #define WINDOW_OFFSET 0
62 #define ANIM_TIME 0.13F
63 #define FLASH_FRAME 0.07F
82 float barrier_probability;
85 struct game_aux_info {
91 int width, height, cx, cy, wrapping, completed;
92 int used_solve, just_used_solve;
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;
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;
189 sprintf(str, "%dx%d%s", ret->width, ret->height,
197 static void free_params(game_params *params)
202 static game_params *dup_params(game_params *params)
204 game_params *ret = snew(game_params);
205 *ret = *params; /* structure copy */
209 static void decode_params(game_params *ret, char const *string)
211 char const *p = string;
213 ret->wrapping = FALSE;
214 ret->barrier_probability = 0.0;
216 ret->width = atoi(p);
217 while (*p && isdigit(*p)) p++;
220 ret->height = atoi(p);
221 while (*p && isdigit(*p)) p++;
222 if ( (ret->wrapping = (*p == 'w')) != 0 )
225 ret->barrier_probability = atof(p+1);
227 ret->height = ret->width;
231 static char *encode_params(game_params *params, int full)
236 len = sprintf(ret, "%dx%d", params->width, params->height);
237 if (params->wrapping)
239 if (full && params->barrier_probability)
240 len += sprintf(ret+len, "b%g", params->barrier_probability);
241 assert(len < lenof(ret));
247 static config_item *game_configure(game_params *params)
252 ret = snewn(5, config_item);
254 ret[0].name = "Width";
255 ret[0].type = C_STRING;
256 sprintf(buf, "%d", params->width);
257 ret[0].sval = dupstr(buf);
260 ret[1].name = "Height";
261 ret[1].type = C_STRING;
262 sprintf(buf, "%d", params->height);
263 ret[1].sval = dupstr(buf);
266 ret[2].name = "Walls wrap around";
267 ret[2].type = C_BOOLEAN;
269 ret[2].ival = params->wrapping;
271 ret[3].name = "Barrier probability";
272 ret[3].type = C_STRING;
273 sprintf(buf, "%g", params->barrier_probability);
274 ret[3].sval = dupstr(buf);
285 static game_params *custom_params(config_item *cfg)
287 game_params *ret = snew(game_params);
289 ret->width = atoi(cfg[0].sval);
290 ret->height = atoi(cfg[1].sval);
291 ret->wrapping = cfg[2].ival;
292 ret->barrier_probability = (float)atof(cfg[3].sval);
297 static char *validate_params(game_params *params)
299 if (params->width <= 1 && params->height <= 1)
300 return "Width and height must both be greater than one";
301 if (params->width <= 1)
302 return "Width must be greater than one";
303 if (params->height <= 1)
304 return "Height must be greater than one";
305 if (params->barrier_probability < 0)
306 return "Barrier probability may not be negative";
307 if (params->barrier_probability > 1)
308 return "Barrier probability may not be greater than 1";
312 /* ----------------------------------------------------------------------
313 * Randomly select a new game description.
316 static char *new_game_desc(game_params *params, random_state *rs,
319 tree234 *possibilities, *barriertree;
320 int w, h, x, y, cx, cy, nbarriers;
321 unsigned char *tiles, *barriers;
327 tiles = snewn(w * h, unsigned char);
328 memset(tiles, 0, w * h);
329 barriers = snewn(w * h, unsigned char);
330 memset(barriers, 0, w * h);
336 * Construct the unshuffled grid.
338 * To do this, we simply start at the centre point, repeatedly
339 * choose a random possibility out of the available ways to
340 * extend a used square into an unused one, and do it. After
341 * extending the third line out of a square, we remove the
342 * fourth from the possibilities list to avoid any full-cross
343 * squares (which would make the game too easy because they
344 * only have one orientation).
346 * The slightly worrying thing is the avoidance of full-cross
347 * squares. Can this cause our unsophisticated construction
348 * algorithm to paint itself into a corner, by getting into a
349 * situation where there are some unreached squares and the
350 * only way to reach any of them is to extend a T-piece into a
353 * Answer: no it can't, and here's a proof.
355 * Any contiguous group of such unreachable squares must be
356 * surrounded on _all_ sides by T-pieces pointing away from the
357 * group. (If not, then there is a square which can be extended
358 * into one of the `unreachable' ones, and so it wasn't
359 * unreachable after all.) In particular, this implies that
360 * each contiguous group of unreachable squares must be
361 * rectangular in shape (any deviation from that yields a
362 * non-T-piece next to an `unreachable' square).
364 * So we have a rectangle of unreachable squares, with T-pieces
365 * forming a solid border around the rectangle. The corners of
366 * that border must be connected (since every tile connects all
367 * the lines arriving in it), and therefore the border must
368 * form a closed loop around the rectangle.
370 * But this can't have happened in the first place, since we
371 * _know_ we've avoided creating closed loops! Hence, no such
372 * situation can ever arise, and the naive grid construction
373 * algorithm will guaranteeably result in a complete grid
374 * containing no unreached squares, no full crosses _and_ no
377 possibilities = newtree234(xyd_cmp);
380 add234(possibilities, new_xyd(cx, cy, R));
382 add234(possibilities, new_xyd(cx, cy, U));
384 add234(possibilities, new_xyd(cx, cy, L));
386 add234(possibilities, new_xyd(cx, cy, D));
388 while (count234(possibilities) > 0) {
391 int x1, y1, d1, x2, y2, d2, d;
394 * Extract a randomly chosen possibility from the list.
396 i = random_upto(rs, count234(possibilities));
397 xyd = delpos234(possibilities, i);
403 OFFSET(x2, y2, x1, y1, d1, params);
406 printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n",
407 x1, y1, "0RU3L567D9abcdef"[d1], x2, y2, "0RU3L567D9abcdef"[d2]);
411 * Make the connection. (We should be moving to an as yet
414 index(params, tiles, x1, y1) |= d1;
415 assert(index(params, tiles, x2, y2) == 0);
416 index(params, tiles, x2, y2) |= d2;
419 * If we have created a T-piece, remove its last
422 if (COUNT(index(params, tiles, x1, y1)) == 3) {
423 struct xyd xyd1, *xydp;
427 xyd1.direction = 0x0F ^ index(params, tiles, x1, y1);
429 xydp = find234(possibilities, &xyd1, NULL);
433 printf("T-piece; removing (%d,%d,%c)\n",
434 xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
436 del234(possibilities, xydp);
442 * Remove all other possibilities that were pointing at the
443 * tile we've just moved into.
445 for (d = 1; d < 0x10; d <<= 1) {
447 struct xyd xyd1, *xydp;
449 OFFSET(x3, y3, x2, y2, d, params);
456 xydp = find234(possibilities, &xyd1, NULL);
460 printf("Loop avoidance; removing (%d,%d,%c)\n",
461 xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
463 del234(possibilities, xydp);
469 * Add new possibilities to the list for moving _out_ of
470 * the tile we have just moved into.
472 for (d = 1; d < 0x10; d <<= 1) {
476 continue; /* we've got this one already */
478 if (!params->wrapping) {
479 if (d == U && y2 == 0)
481 if (d == D && y2 == h-1)
483 if (d == L && x2 == 0)
485 if (d == R && x2 == w-1)
489 OFFSET(x3, y3, x2, y2, d, params);
491 if (index(params, tiles, x3, y3))
492 continue; /* this would create a loop */
495 printf("New frontier; adding (%d,%d,%c)\n",
496 x2, y2, "0RU3L567D9abcdef"[d]);
498 add234(possibilities, new_xyd(x2, y2, d));
501 /* Having done that, we should have no possibilities remaining. */
502 assert(count234(possibilities) == 0);
503 freetree234(possibilities);
506 * Now compute a list of the possible barrier locations.
508 barriertree = newtree234(xyd_cmp);
509 for (y = 0; y < h; y++) {
510 for (x = 0; x < w; x++) {
512 if (!(index(params, tiles, x, y) & R) &&
513 (params->wrapping || x < w-1))
514 add234(barriertree, new_xyd(x, y, R));
515 if (!(index(params, tiles, x, y) & D) &&
516 (params->wrapping || y < h-1))
517 add234(barriertree, new_xyd(x, y, D));
522 * Save the unshuffled grid. We do this using a separate
523 * reference-counted structure since it's a large chunk of
524 * memory which we don't want to have to replicate in every
525 * game state while playing.
528 game_aux_info *solution;
530 solution = snew(game_aux_info);
532 solution->height = h;
533 solution->tiles = snewn(w * h, unsigned char);
534 memcpy(solution->tiles, tiles, w * h);
540 * Now shuffle the grid.
541 * FIXME - this simply does a set of random moves to shuffle the pieces.
542 * A better way would be to number all the pieces, generate a placement
543 * for all the numbers as for "sixteen", observing parity constraints if
544 * neccessary, and then place the pieces according to their numbering.
545 * BUT - I'm not sure if this will work, since we disallow movement of
546 * the middle row and column.
552 for (i = 0; i < cols * rows * 2; i++) {
553 /* Choose a direction: 0,1,2,3 = up, right, down, left. */
554 int dir = random_upto(rs, 4);
556 int col = random_upto(rs, cols);
557 if (col >= cx) col += 1;
558 slide_col_int(w, h, tiles, 1 - dir, col);
560 int row = random_upto(rs, rows);
561 if (row >= cy) row += 1;
562 slide_row_int(w, h, tiles, 2 - dir, row);
568 * And now choose barrier locations. (We carefully do this
569 * _after_ shuffling, so that changing the barrier rate in the
570 * params while keeping the random seed the same will give the
571 * same shuffled grid and _only_ change the barrier locations.
572 * Also the way we choose barrier locations, by repeatedly
573 * choosing one possibility from the list until we have enough,
574 * is designed to ensure that raising the barrier rate while
575 * keeping the seed the same will provide a superset of the
576 * previous barrier set - i.e. if you ask for 10 barriers, and
577 * then decide that's still too hard and ask for 20, you'll get
578 * the original 10 plus 10 more, rather than getting 20 new
579 * ones and the chance of remembering your first 10.)
581 nbarriers = (int)(params->barrier_probability * count234(barriertree));
582 assert(nbarriers >= 0 && nbarriers <= count234(barriertree));
584 while (nbarriers > 0) {
587 int x1, y1, d1, x2, y2, d2;
590 * Extract a randomly chosen barrier from the list.
592 i = random_upto(rs, count234(barriertree));
593 xyd = delpos234(barriertree, i);
602 OFFSET(x2, y2, x1, y1, d1, params);
605 index(params, barriers, x1, y1) |= d1;
606 index(params, barriers, x2, y2) |= d2;
612 * Clean up the rest of the barrier list.
617 while ( (xyd = delpos234(barriertree, 0)) != NULL)
620 freetree234(barriertree);
624 * Finally, encode the grid into a string game description.
626 * My syntax is extremely simple: each square is encoded as a
627 * hex digit in which bit 0 means a connection on the right,
628 * bit 1 means up, bit 2 left and bit 3 down. (i.e. the same
629 * encoding as used internally). Each digit is followed by
630 * optional barrier indicators: `v' means a vertical barrier to
631 * the right of it, and `h' means a horizontal barrier below
634 desc = snewn(w * h * 3 + 1, char);
636 for (y = 0; y < h; y++) {
637 for (x = 0; x < w; x++) {
638 *p++ = "0123456789abcdef"[index(params, tiles, x, y)];
639 if ((params->wrapping || x < w-1) &&
640 (index(params, barriers, x, y) & R))
642 if ((params->wrapping || y < h-1) &&
643 (index(params, barriers, x, y) & D))
647 assert(p - desc <= w*h*3);
655 static void game_free_aux_info(game_aux_info *aux)
661 static char *validate_desc(game_params *params, char *desc)
663 int w = params->width, h = params->height;
666 for (i = 0; i < w*h; i++) {
667 if (*desc >= '0' && *desc <= '9')
669 else if (*desc >= 'a' && *desc <= 'f')
671 else if (*desc >= 'A' && *desc <= 'F')
674 return "Game description shorter than expected";
676 return "Game description contained unexpected character";
678 while (*desc == 'h' || *desc == 'v')
682 return "Game description longer than expected";
687 /* ----------------------------------------------------------------------
688 * Construct an initial game state, given a description and parameters.
691 static game_state *new_game(game_params *params, char *desc)
696 assert(params->width > 0 && params->height > 0);
697 assert(params->width > 1 || params->height > 1);
700 * Create a blank game state.
702 state = snew(game_state);
703 w = state->width = params->width;
704 h = state->height = params->height;
705 state->cx = state->width / 2;
706 state->cy = state->height / 2;
707 state->wrapping = params->wrapping;
708 state->completed = 0;
709 state->used_solve = state->just_used_solve = FALSE;
710 state->move_count = 0;
711 state->last_move_row = -1;
712 state->last_move_col = -1;
713 state->last_move_dir = 0;
714 state->tiles = snewn(state->width * state->height, unsigned char);
715 memset(state->tiles, 0, state->width * state->height);
716 state->barriers = snewn(state->width * state->height, unsigned char);
717 memset(state->barriers, 0, state->width * state->height);
721 * Parse the game description into the grid.
723 for (y = 0; y < h; y++) {
724 for (x = 0; x < w; x++) {
725 if (*desc >= '0' && *desc <= '9')
726 tile(state, x, y) = *desc - '0';
727 else if (*desc >= 'a' && *desc <= 'f')
728 tile(state, x, y) = *desc - 'a' + 10;
729 else if (*desc >= 'A' && *desc <= 'F')
730 tile(state, x, y) = *desc - 'A' + 10;
733 while (*desc == 'h' || *desc == 'v') {
740 OFFSET(x2, y2, x, y, d1, state);
743 barrier(state, x, y) |= d1;
744 barrier(state, x2, y2) |= d2;
752 * Set up border barriers if this is a non-wrapping game.
754 if (!state->wrapping) {
755 for (x = 0; x < state->width; x++) {
756 barrier(state, x, 0) |= U;
757 barrier(state, x, state->height-1) |= D;
759 for (y = 0; y < state->height; y++) {
760 barrier(state, 0, y) |= L;
761 barrier(state, state->width-1, y) |= R;
766 * Set up the barrier corner flags, for drawing barriers
767 * prettily when they meet.
769 for (y = 0; y < state->height; y++) {
770 for (x = 0; x < state->width; x++) {
773 for (dir = 1; dir < 0x10; dir <<= 1) {
775 int x1, y1, x2, y2, x3, y3;
778 if (!(barrier(state, x, y) & dir))
781 if (barrier(state, x, y) & dir2)
784 x1 = x + X(dir), y1 = y + Y(dir);
785 if (x1 >= 0 && x1 < state->width &&
786 y1 >= 0 && y1 < state->height &&
787 (barrier(state, x1, y1) & dir2))
790 x2 = x + X(dir2), y2 = y + Y(dir2);
791 if (x2 >= 0 && x2 < state->width &&
792 y2 >= 0 && y2 < state->height &&
793 (barrier(state, x2, y2) & dir))
797 barrier(state, x, y) |= (dir << 4);
798 if (x1 >= 0 && x1 < state->width &&
799 y1 >= 0 && y1 < state->height)
800 barrier(state, x1, y1) |= (A(dir) << 4);
801 if (x2 >= 0 && x2 < state->width &&
802 y2 >= 0 && y2 < state->height)
803 barrier(state, x2, y2) |= (C(dir) << 4);
804 x3 = x + X(dir) + X(dir2), y3 = y + Y(dir) + Y(dir2);
805 if (x3 >= 0 && x3 < state->width &&
806 y3 >= 0 && y3 < state->height)
807 barrier(state, x3, y3) |= (F(dir) << 4);
816 static game_state *dup_game(game_state *state)
820 ret = snew(game_state);
821 ret->width = state->width;
822 ret->height = state->height;
825 ret->wrapping = state->wrapping;
826 ret->completed = state->completed;
827 ret->used_solve = state->used_solve;
828 ret->just_used_solve = state->just_used_solve;
829 ret->move_count = state->move_count;
830 ret->last_move_row = state->last_move_row;
831 ret->last_move_col = state->last_move_col;
832 ret->last_move_dir = state->last_move_dir;
833 ret->tiles = snewn(state->width * state->height, unsigned char);
834 memcpy(ret->tiles, state->tiles, state->width * state->height);
835 ret->barriers = snewn(state->width * state->height, unsigned char);
836 memcpy(ret->barriers, state->barriers, state->width * state->height);
841 static void free_game(game_state *state)
844 sfree(state->barriers);
848 static game_state *solve_game(game_state *state, game_aux_info *aux,
854 *error = "Solution not known for this puzzle";
858 assert(aux->width == state->width);
859 assert(aux->height == state->height);
860 ret = dup_game(state);
861 memcpy(ret->tiles, aux->tiles, ret->width * ret->height);
862 ret->used_solve = ret->just_used_solve = TRUE;
863 ret->completed = ret->move_count = 1;
868 static char *game_text_format(game_state *state)
873 /* ----------------------------------------------------------------------
878 * Compute which squares are reachable from the centre square, as a
879 * quick visual aid to determining how close the game is to
880 * completion. This is also a simple way to tell if the game _is_
881 * completed - just call this function and see whether every square
884 * squares in the moving_row and moving_col are always inactive - this
885 * is so that "current" doesn't appear to jump across moving lines.
887 static unsigned char *compute_active(game_state *state,
888 int moving_row, int moving_col)
890 unsigned char *active;
894 active = snewn(state->width * state->height, unsigned char);
895 memset(active, 0, state->width * state->height);
898 * We only store (x,y) pairs in todo, but it's easier to reuse
899 * xyd_cmp and just store direction 0 every time.
901 todo = newtree234(xyd_cmp);
902 index(state, active, state->cx, state->cy) = ACTIVE;
903 add234(todo, new_xyd(state->cx, state->cy, 0));
905 while ( (xyd = delpos234(todo, 0)) != NULL) {
906 int x1, y1, d1, x2, y2, d2;
912 for (d1 = 1; d1 < 0x10; d1 <<= 1) {
913 OFFSET(x2, y2, x1, y1, d1, state);
917 * If the next tile in this direction is connected to
918 * us, and there isn't a barrier in the way, and it
919 * isn't already marked active, then mark it active and
920 * add it to the to-examine list.
922 if ((x2 != moving_col && y2 != moving_row) &&
923 (tile(state, x1, y1) & d1) &&
924 (tile(state, x2, y2) & d2) &&
925 !(barrier(state, x1, y1) & d1) &&
926 !index(state, active, x2, y2)) {
927 index(state, active, x2, y2) = ACTIVE;
928 add234(todo, new_xyd(x2, y2, 0));
932 /* Now we expect the todo list to have shrunk to zero size. */
933 assert(count234(todo) == 0);
944 static game_ui *new_ui(game_state *state)
946 game_ui *ui = snew(game_ui);
947 ui->cur_x = state->width / 2;
948 ui->cur_y = state->height / 2;
949 ui->cur_visible = FALSE;
954 static void free_ui(game_ui *ui)
959 /* ----------------------------------------------------------------------
963 static void slide_row_int(int w, int h, unsigned char *tiles, int dir, int row)
965 int x = dir > 0 ? -1 : w;
968 unsigned char endtile = tiles[row * w + tx];
971 tx = (x + dir + w) % w;
972 tiles[row * w + x] = tiles[row * w + tx];
974 tiles[row * w + tx] = endtile;
977 static void slide_col_int(int w, int h, unsigned char *tiles, int dir, int col)
979 int y = dir > 0 ? -1 : h;
982 unsigned char endtile = tiles[ty * w + col];
985 ty = (y + dir + h) % h;
986 tiles[y * w + col] = tiles[ty * w + col];
988 tiles[ty * w + col] = endtile;
991 static void slide_row(game_state *state, int dir, int row)
993 slide_row_int(state->width, state->height, state->tiles, dir, row);
996 static void slide_col(game_state *state, int dir, int col)
998 slide_col_int(state->width, state->height, state->tiles, dir, col);
1001 static game_state *make_move(game_state *state, game_ui *ui,
1002 int x, int y, int button)
1008 if (button != LEFT_BUTTON && button != RIGHT_BUTTON)
1011 cx = (x - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2;
1012 cy = (y - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2;
1014 if (cy >= 0 && cy < state->height && cy != state->cy)
1016 if (cx == -1) dx = +1;
1017 else if (cx == state->width) dx = -1;
1022 else if (cx >= 0 && cx < state->width && cx != state->cx)
1024 if (cy == -1) dy = +1;
1025 else if (cy == state->height) dy = -1;
1033 /* reverse direction if right hand button is pressed */
1034 if (button == RIGHT_BUTTON)
1040 ret = dup_game(state);
1041 ret->just_used_solve = FALSE;
1043 if (dx == 0) slide_col(ret, dy, cx);
1044 else slide_row(ret, dx, cy);
1047 ret->last_move_row = dx ? cy : -1;
1048 ret->last_move_col = dx ? -1 : cx;
1049 ret->last_move_dir = dx + dy;
1052 * See if the game has been completed.
1054 if (!ret->completed) {
1055 unsigned char *active = compute_active(ret, -1, -1);
1057 int complete = TRUE;
1059 for (x1 = 0; x1 < ret->width; x1++)
1060 for (y1 = 0; y1 < ret->height; y1++)
1061 if (!index(ret, active, x1, y1)) {
1063 goto break_label; /* break out of two loops at once */
1070 ret->completed = ret->move_count;
1076 /* ----------------------------------------------------------------------
1077 * Routines for drawing the game position on the screen.
1080 struct game_drawstate {
1083 unsigned char *visible;
1086 static game_drawstate *game_new_drawstate(game_state *state)
1088 game_drawstate *ds = snew(game_drawstate);
1090 ds->started = FALSE;
1091 ds->width = state->width;
1092 ds->height = state->height;
1093 ds->visible = snewn(state->width * state->height, unsigned char);
1094 memset(ds->visible, 0xFF, state->width * state->height);
1099 static void game_free_drawstate(game_drawstate *ds)
1105 static void game_size(game_params *params, int *x, int *y)
1107 *x = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->width + TILE_BORDER;
1108 *y = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->height + TILE_BORDER;
1111 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
1115 ret = snewn(NCOLOURS * 3, float);
1116 *ncolours = NCOLOURS;
1119 * Basic background colour is whatever the front end thinks is
1120 * a sensible default.
1122 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1127 ret[COL_WIRE * 3 + 0] = 0.0F;
1128 ret[COL_WIRE * 3 + 1] = 0.0F;
1129 ret[COL_WIRE * 3 + 2] = 0.0F;
1132 * Powered wires and powered endpoints are cyan.
1134 ret[COL_POWERED * 3 + 0] = 0.0F;
1135 ret[COL_POWERED * 3 + 1] = 1.0F;
1136 ret[COL_POWERED * 3 + 2] = 1.0F;
1141 ret[COL_BARRIER * 3 + 0] = 1.0F;
1142 ret[COL_BARRIER * 3 + 1] = 0.0F;
1143 ret[COL_BARRIER * 3 + 2] = 0.0F;
1146 * Unpowered endpoints are blue.
1148 ret[COL_ENDPOINT * 3 + 0] = 0.0F;
1149 ret[COL_ENDPOINT * 3 + 1] = 0.0F;
1150 ret[COL_ENDPOINT * 3 + 2] = 1.0F;
1153 * Tile borders are a darker grey than the background.
1155 ret[COL_BORDER * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
1156 ret[COL_BORDER * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
1157 ret[COL_BORDER * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2];
1160 * Flashing tiles are a grey in between those two.
1162 ret[COL_FLASHING * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0];
1163 ret[COL_FLASHING * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1];
1164 ret[COL_FLASHING * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2];
1166 ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.8F;
1167 ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.8F;
1168 ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.8F;
1169 ret[COL_TEXT * 3 + 0] = 0.0;
1170 ret[COL_TEXT * 3 + 1] = 0.0;
1171 ret[COL_TEXT * 3 + 2] = 0.0;
1176 static void draw_thick_line(frontend *fe, int x1, int y1, int x2, int y2,
1179 draw_line(fe, x1-1, y1, x2-1, y2, COL_WIRE);
1180 draw_line(fe, x1+1, y1, x2+1, y2, COL_WIRE);
1181 draw_line(fe, x1, y1-1, x2, y2-1, COL_WIRE);
1182 draw_line(fe, x1, y1+1, x2, y2+1, COL_WIRE);
1183 draw_line(fe, x1, y1, x2, y2, colour);
1186 static void draw_rect_coords(frontend *fe, int x1, int y1, int x2, int y2,
1189 int mx = (x1 < x2 ? x1 : x2);
1190 int my = (y1 < y2 ? y1 : y2);
1191 int dx = (x2 + x1 - 2*mx + 1);
1192 int dy = (y2 + y1 - 2*my + 1);
1194 draw_rect(fe, mx, my, dx, dy, colour);
1197 static void draw_barrier_corner(frontend *fe, int x, int y, int dir, int phase)
1199 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
1200 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
1201 int x1, y1, dx, dy, dir2;
1206 dx = X(dir) + X(dir2);
1207 dy = Y(dir) + Y(dir2);
1208 x1 = (dx > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
1209 y1 = (dy > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
1212 draw_rect_coords(fe, bx+x1, by+y1,
1213 bx+x1-TILE_BORDER*dx, by+y1-(TILE_BORDER-1)*dy,
1215 draw_rect_coords(fe, bx+x1, by+y1,
1216 bx+x1-(TILE_BORDER-1)*dx, by+y1-TILE_BORDER*dy,
1219 draw_rect_coords(fe, bx+x1, by+y1,
1220 bx+x1-(TILE_BORDER-1)*dx, by+y1-(TILE_BORDER-1)*dy,
1225 static void draw_barrier(frontend *fe, int x, int y, int dir, int phase)
1227 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
1228 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
1231 x1 = (X(dir) > 0 ? TILE_SIZE : X(dir) == 0 ? TILE_BORDER : 0);
1232 y1 = (Y(dir) > 0 ? TILE_SIZE : Y(dir) == 0 ? TILE_BORDER : 0);
1233 w = (X(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER);
1234 h = (Y(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER);
1237 draw_rect(fe, bx+x1-X(dir), by+y1-Y(dir), w, h, COL_WIRE);
1239 draw_rect(fe, bx+x1, by+y1, w, h, COL_BARRIER);
1243 static void draw_tile(frontend *fe, game_state *state, int x, int y, int tile,
1244 float xshift, float yshift)
1246 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x + (xshift * TILE_SIZE);
1247 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y + (yshift * TILE_SIZE);
1248 float cx, cy, ex, ey;
1252 * When we draw a single tile, we must draw everything up to
1253 * and including the borders around the tile. This means that
1254 * if the neighbouring tiles have connections to those borders,
1255 * we must draw those connections on the borders themselves.
1257 * This would be terribly fiddly if we ever had to draw a tile
1258 * while its neighbour was in mid-rotate, because we'd have to
1259 * arrange to _know_ that the neighbour was being rotated and
1260 * hence had an anomalous effect on the redraw of this tile.
1261 * Fortunately, the drawing algorithm avoids ever calling us in
1262 * this circumstance: we're either drawing lots of straight
1263 * tiles at game start or after a move is complete, or we're
1264 * repeatedly drawing only the rotating tile. So no problem.
1268 * So. First blank the tile out completely: draw a big
1269 * rectangle in border colour, and a smaller rectangle in
1270 * background colour to fill it in.
1272 draw_rect(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER,
1274 draw_rect(fe, bx+TILE_BORDER, by+TILE_BORDER,
1275 TILE_SIZE-TILE_BORDER, TILE_SIZE-TILE_BORDER,
1276 tile & FLASHING ? COL_FLASHING : COL_BACKGROUND);
1281 cx = cy = TILE_BORDER + (TILE_SIZE-TILE_BORDER) / 2.0F - 0.5F;
1282 col = (tile & ACTIVE ? COL_POWERED : COL_WIRE);
1283 for (dir = 1; dir < 0x10; dir <<= 1) {
1285 ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir);
1286 ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir);
1287 draw_thick_line(fe, bx+(int)cx, by+(int)cy,
1288 bx+(int)(cx+ex), by+(int)(cy+ey),
1292 for (dir = 1; dir < 0x10; dir <<= 1) {
1294 ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir);
1295 ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir);
1296 draw_line(fe, bx+(int)cx, by+(int)cy,
1297 bx+(int)(cx+ex), by+(int)(cy+ey), col);
1302 * Draw the box in the middle. We do this in blue if the tile
1303 * is an unpowered endpoint, in cyan if the tile is a powered
1304 * endpoint, in black if the tile is the centrepiece, and
1305 * otherwise not at all.
1308 if (x == state->cx && y == state->cy)
1310 else if (COUNT(tile) == 1) {
1311 col = (tile & ACTIVE ? COL_POWERED : COL_ENDPOINT);
1316 points[0] = +1; points[1] = +1;
1317 points[2] = +1; points[3] = -1;
1318 points[4] = -1; points[5] = -1;
1319 points[6] = -1; points[7] = +1;
1321 for (i = 0; i < 8; i += 2) {
1322 ex = (TILE_SIZE * 0.24F) * points[i];
1323 ey = (TILE_SIZE * 0.24F) * points[i+1];
1324 points[i] = bx+(int)(cx+ex);
1325 points[i+1] = by+(int)(cy+ey);
1328 draw_polygon(fe, points, 4, TRUE, col);
1329 draw_polygon(fe, points, 4, FALSE, COL_WIRE);
1333 * Draw the points on the border if other tiles are connected
1336 for (dir = 1; dir < 0x10; dir <<= 1) {
1337 int dx, dy, px, py, lx, ly, vx, vy, ox, oy;
1345 if (ox < 0 || ox >= state->width || oy < 0 || oy >= state->height)
1348 if (!(tile(state, ox, oy) & F(dir)))
1351 px = bx + (int)(dx>0 ? TILE_SIZE + TILE_BORDER - 1 : dx<0 ? 0 : cx);
1352 py = by + (int)(dy>0 ? TILE_SIZE + TILE_BORDER - 1 : dy<0 ? 0 : cy);
1353 lx = dx * (TILE_BORDER-1);
1354 ly = dy * (TILE_BORDER-1);
1358 if (xshift == 0.0 && yshift == 0.0 && (tile & dir)) {
1360 * If we are fully connected to the other tile, we must
1361 * draw right across the tile border. (We can use our
1362 * own ACTIVE state to determine what colour to do this
1363 * in: if we are fully connected to the other tile then
1364 * the two ACTIVE states will be the same.)
1366 draw_rect_coords(fe, px-vx, py-vy, px+lx+vx, py+ly+vy, COL_WIRE);
1367 draw_rect_coords(fe, px, py, px+lx, py+ly,
1368 (tile & ACTIVE) ? COL_POWERED : COL_WIRE);
1371 * The other tile extends into our border, but isn't
1372 * actually connected to us. Just draw a single black
1375 draw_rect_coords(fe, px, py, px, py, COL_WIRE);
1379 draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER);
1382 static void draw_tile_barriers(frontend *fe, game_state *state, int x, int y)
1386 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
1387 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
1389 * Draw barrier corners, and then barriers.
1391 for (phase = 0; phase < 2; phase++) {
1392 for (dir = 1; dir < 0x10; dir <<= 1)
1393 if (barrier(state, x, y) & (dir << 4))
1394 draw_barrier_corner(fe, x, y, dir << 4, phase);
1395 for (dir = 1; dir < 0x10; dir <<= 1)
1396 if (barrier(state, x, y) & dir)
1397 draw_barrier(fe, x, y, dir, phase);
1400 draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER);
1403 static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy)
1406 int ydy = -xdx, ydx = xdy;
1408 x = x * TILE_SIZE + BORDER + WINDOW_OFFSET;
1409 y = y * TILE_SIZE + BORDER + WINDOW_OFFSET;
1411 #define POINT(n, xx, yy) ( \
1412 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
1413 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
1415 POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */
1416 POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */
1417 POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */
1418 POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */
1419 POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */
1420 POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */
1421 POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */
1423 draw_polygon(fe, coords, 7, TRUE, COL_LOWLIGHT);
1424 draw_polygon(fe, coords, 7, FALSE, COL_TEXT);
1427 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1428 game_state *state, int dir, game_ui *ui, float t, float ft)
1430 int x, y, tx, ty, frame;
1431 unsigned char *active;
1436 * Clear the screen and draw the exterior barrier lines if this
1437 * is our first call.
1445 BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * state->width + TILE_BORDER,
1446 BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * state->height + TILE_BORDER,
1448 draw_update(fe, 0, 0,
1449 BORDER * 2 + WINDOW_OFFSET*2 + TILE_SIZE*state->width + TILE_BORDER,
1450 BORDER * 2 + WINDOW_OFFSET*2 + TILE_SIZE*state->height + TILE_BORDER);
1452 for (phase = 0; phase < 2; phase++) {
1454 for (x = 0; x < ds->width; x++) {
1455 if (barrier(state, x, 0) & UL)
1456 draw_barrier_corner(fe, x, -1, LD, phase);
1457 if (barrier(state, x, 0) & RU)
1458 draw_barrier_corner(fe, x, -1, DR, phase);
1459 if (barrier(state, x, 0) & U)
1460 draw_barrier(fe, x, -1, D, phase);
1461 if (barrier(state, x, ds->height-1) & DR)
1462 draw_barrier_corner(fe, x, ds->height, RU, phase);
1463 if (barrier(state, x, ds->height-1) & LD)
1464 draw_barrier_corner(fe, x, ds->height, UL, phase);
1465 if (barrier(state, x, ds->height-1) & D)
1466 draw_barrier(fe, x, ds->height, U, phase);
1469 for (y = 0; y < ds->height; y++) {
1470 if (barrier(state, 0, y) & UL)
1471 draw_barrier_corner(fe, -1, y, RU, phase);
1472 if (barrier(state, 0, y) & LD)
1473 draw_barrier_corner(fe, -1, y, DR, phase);
1474 if (barrier(state, 0, y) & L)
1475 draw_barrier(fe, -1, y, R, phase);
1476 if (barrier(state, ds->width-1, y) & RU)
1477 draw_barrier_corner(fe, ds->width, y, UL, phase);
1478 if (barrier(state, ds->width-1, y) & DR)
1479 draw_barrier_corner(fe, ds->width, y, LD, phase);
1480 if (barrier(state, ds->width-1, y) & R)
1481 draw_barrier(fe, ds->width, y, L, phase);
1486 * Arrows for making moves.
1488 for (x = 0; x < ds->width; x++) {
1489 if (x == state->cx) continue;
1490 draw_arrow(fe, x, 0, +1, 0);
1491 draw_arrow(fe, x+1, ds->height, -1, 0);
1493 for (y = 0; y < ds->height; y++) {
1494 if (y == state->cy) continue;
1495 draw_arrow(fe, ds->width, y, 0, +1);
1496 draw_arrow(fe, 0, y+1, 0, -1);
1500 /* Check if this is an undo. If so, we will need to run any animation
1503 if (oldstate && oldstate->move_count > state->move_count) {
1504 game_state * tmpstate = state;
1506 oldstate = tmpstate;
1511 if (oldstate && (t < ANIM_TIME)) {
1513 * We're animating a slide, of row/column number
1514 * state->last_move_pos, in direction
1515 * state->last_move_dir
1517 xshift = state->last_move_row == -1 ? 0.0 :
1518 (1 - t / ANIM_TIME) * state->last_move_dir;
1519 yshift = state->last_move_col == -1 ? 0.0 :
1520 (1 - t / ANIM_TIME) * state->last_move_dir;
1526 * We're animating a completion flash. Find which frame
1529 frame = (int)(ft / FLASH_FRAME);
1533 * Draw any tile which differs from the way it was last drawn.
1535 if (xshift != 0.0 || yshift != 0.0) {
1536 active = compute_active(state,
1537 state->last_move_row, state->last_move_col);
1539 active = compute_active(state, -1, -1);
1543 BORDER + WINDOW_OFFSET, BORDER + WINDOW_OFFSET,
1544 TILE_SIZE * state->width + TILE_BORDER,
1545 TILE_SIZE * state->height + TILE_BORDER);
1547 for (x = 0; x < ds->width; x++)
1548 for (y = 0; y < ds->height; y++) {
1549 unsigned char c = tile(state, x, y) | index(state, active, x, y);
1552 * In a completion flash, we adjust the FLASHING bit
1553 * depending on our distance from the centre point and
1557 int xdist, ydist, dist;
1558 xdist = (x < state->cx ? state->cx - x : x - state->cx);
1559 ydist = (y < state->cy ? state->cy - y : y - state->cy);
1560 dist = (xdist > ydist ? xdist : ydist);
1562 if (frame >= dist && frame < dist+4) {
1563 int flash = (frame - dist) & 1;
1564 flash = flash ? FLASHING : 0;
1565 c = (c &~ FLASHING) | flash;
1569 if (index(state, ds->visible, x, y) != c ||
1570 index(state, ds->visible, x, y) == 0xFF ||
1571 (x == state->last_move_col || y == state->last_move_row))
1573 float xs = (y == state->last_move_row ? xshift : 0.0);
1574 float ys = (x == state->last_move_col ? yshift : 0.0);
1576 draw_tile(fe, state, x, y, c, xs, ys);
1577 if (xs < 0 && x == 0)
1578 draw_tile(fe, state, state->width, y, c, xs, ys);
1579 else if (xs > 0 && x == state->width - 1)
1580 draw_tile(fe, state, -1, y, c, xs, ys);
1581 else if (ys < 0 && y == 0)
1582 draw_tile(fe, state, x, state->height, c, xs, ys);
1583 else if (ys > 0 && y == state->height - 1)
1584 draw_tile(fe, state, x, -1, c, xs, ys);
1586 if (x == state->last_move_col || y == state->last_move_row)
1587 index(state, ds->visible, x, y) = 0xFF;
1589 index(state, ds->visible, x, y) = c;
1593 for (x = 0; x < ds->width; x++)
1594 for (y = 0; y < ds->height; y++)
1595 draw_tile_barriers(fe, state, x, y);
1600 * Update the status bar.
1603 char statusbuf[256];
1606 n = state->width * state->height;
1607 for (i = a = 0; i < n; i++)
1611 if (state->used_solve)
1612 sprintf(statusbuf, "Moves since auto-solve: %d",
1613 state->move_count - state->completed);
1615 sprintf(statusbuf, "%sMoves: %d",
1616 (state->completed ? "COMPLETED! " : ""),
1617 (state->completed ? state->completed : state->move_count));
1619 sprintf(statusbuf + strlen(statusbuf), " Active: %d/%d", a, n);
1621 status_bar(fe, statusbuf);
1627 static float game_anim_length(game_state *oldstate,
1628 game_state *newstate, int dir)
1631 * Don't animate an auto-solve move.
1633 if ((dir > 0 && newstate->just_used_solve) ||
1634 (dir < 0 && oldstate->just_used_solve))
1640 static float game_flash_length(game_state *oldstate,
1641 game_state *newstate, int dir)
1644 * If the game has just been completed, we display a completion
1647 if (!oldstate->completed && newstate->completed &&
1648 !oldstate->used_solve && !newstate->used_solve) {
1651 if (size < newstate->cx+1)
1652 size = newstate->cx+1;
1653 if (size < newstate->cy+1)
1654 size = newstate->cy+1;
1655 if (size < newstate->width - newstate->cx)
1656 size = newstate->width - newstate->cx;
1657 if (size < newstate->height - newstate->cy)
1658 size = newstate->height - newstate->cy;
1659 return FLASH_FRAME * (size+4);
1665 static int game_wants_statusbar(void)
1671 #define thegame netslide
1674 const struct game thegame = {
1675 "Netslide", "games.netslide",
1682 TRUE, game_configure, custom_params,
1691 FALSE, game_text_format,
1698 game_free_drawstate,
1702 game_wants_statusbar,