15 #define PI 3.141592653589793238462643383279502884197169399
17 #define MATMUL(xr,yr,m,x,y) do { \
18 float rx, ry, xx = (x), yy = (y), *mat = (m); \
19 rx = mat[0] * xx + mat[2] * yy; \
20 ry = mat[1] * xx + mat[3] * yy; \
21 (xr) = rx; (yr) = ry; \
24 /* Direction and other bitfields */
31 /* Corner flags go in the barriers array */
37 /* Rotations: Anticlockwise, Clockwise, Flip, general rotate */
38 #define A(x) ( (((x) & 0x07) << 1) | (((x) & 0x08) >> 3) )
39 #define C(x) ( (((x) & 0x0E) >> 1) | (((x) & 0x01) << 3) )
40 #define F(x) ( (((x) & 0x0C) >> 2) | (((x) & 0x03) << 2) )
41 #define ROT(x, n) ( ((n)&3) == 0 ? (x) : \
42 ((n)&3) == 1 ? A(x) : \
43 ((n)&3) == 2 ? F(x) : C(x) )
45 /* X and Y displacements */
46 #define X(x) ( (x) == R ? +1 : (x) == L ? -1 : 0 )
47 #define Y(x) ( (x) == D ? +1 : (x) == U ? -1 : 0 )
50 #define COUNT(x) ( (((x) & 0x08) >> 3) + (((x) & 0x04) >> 2) + \
51 (((x) & 0x02) >> 1) + ((x) & 0x01) )
55 #define WINDOW_OFFSET 16
57 #define ROTATE_TIME 0.13F
58 #define FLASH_FRAME 0.07F
75 float barrier_probability;
78 struct game_aux_info {
84 int width, height, cx, cy, wrapping, completed;
85 int last_rotate_x, last_rotate_y, last_rotate_dir;
86 int used_solve, just_used_solve;
88 unsigned char *barriers;
91 #define OFFSET(x2,y2,x1,y1,dir,state) \
92 ( (x2) = ((x1) + (state)->width + X((dir))) % (state)->width, \
93 (y2) = ((y1) + (state)->height + Y((dir))) % (state)->height)
95 #define index(state, a, x, y) ( a[(y) * (state)->width + (x)] )
96 #define tile(state, x, y) index(state, (state)->tiles, x, y)
97 #define barrier(state, x, y) index(state, (state)->barriers, x, y)
103 static int xyd_cmp(void *av, void *bv) {
104 struct xyd *a = (struct xyd *)av;
105 struct xyd *b = (struct xyd *)bv;
114 if (a->direction < b->direction)
116 if (a->direction > b->direction)
121 static struct xyd *new_xyd(int x, int y, int direction)
123 struct xyd *xyd = snew(struct xyd);
126 xyd->direction = direction;
130 /* ----------------------------------------------------------------------
131 * Manage game parameters.
133 static game_params *default_params(void)
135 game_params *ret = snew(game_params);
139 ret->wrapping = FALSE;
140 ret->barrier_probability = 0.0;
145 static int game_fetch_preset(int i, char **name, game_params **params)
149 static const struct { int x, y, wrap; } values[] = {
162 if (i < 0 || i >= lenof(values))
165 ret = snew(game_params);
166 ret->width = values[i].x;
167 ret->height = values[i].y;
168 ret->wrapping = values[i].wrap;
169 ret->barrier_probability = 0.0;
171 sprintf(str, "%dx%d%s", ret->width, ret->height,
172 ret->wrapping ? " wrapping" : "");
179 static void free_params(game_params *params)
184 static game_params *dup_params(game_params *params)
186 game_params *ret = snew(game_params);
187 *ret = *params; /* structure copy */
191 static void decode_params(game_params *ret, char const *string)
193 char const *p = string;
195 ret->width = atoi(p);
196 while (*p && isdigit(*p)) p++;
199 ret->height = atoi(p);
200 while (*p && isdigit(*p)) p++;
201 if ( (ret->wrapping = (*p == 'w')) != 0 )
204 ret->barrier_probability = atof(p+1);
206 ret->height = ret->width;
210 static char *encode_params(game_params *params, int full)
215 len = sprintf(ret, "%dx%d", params->width, params->height);
216 if (params->wrapping)
218 if (full && params->barrier_probability)
219 len += sprintf(ret+len, "b%g", params->barrier_probability);
220 assert(len < lenof(ret));
226 static config_item *game_configure(game_params *params)
231 ret = snewn(5, config_item);
233 ret[0].name = "Width";
234 ret[0].type = C_STRING;
235 sprintf(buf, "%d", params->width);
236 ret[0].sval = dupstr(buf);
239 ret[1].name = "Height";
240 ret[1].type = C_STRING;
241 sprintf(buf, "%d", params->height);
242 ret[1].sval = dupstr(buf);
245 ret[2].name = "Walls wrap around";
246 ret[2].type = C_BOOLEAN;
248 ret[2].ival = params->wrapping;
250 ret[3].name = "Barrier probability";
251 ret[3].type = C_STRING;
252 sprintf(buf, "%g", params->barrier_probability);
253 ret[3].sval = dupstr(buf);
264 static game_params *custom_params(config_item *cfg)
266 game_params *ret = snew(game_params);
268 ret->width = atoi(cfg[0].sval);
269 ret->height = atoi(cfg[1].sval);
270 ret->wrapping = cfg[2].ival;
271 ret->barrier_probability = (float)atof(cfg[3].sval);
276 static char *validate_params(game_params *params)
278 if (params->width <= 0 && params->height <= 0)
279 return "Width and height must both be greater than zero";
280 if (params->width <= 0)
281 return "Width must be greater than zero";
282 if (params->height <= 0)
283 return "Height must be greater than zero";
284 if (params->width <= 1 && params->height <= 1)
285 return "At least one of width and height must be greater than one";
286 if (params->barrier_probability < 0)
287 return "Barrier probability may not be negative";
288 if (params->barrier_probability > 1)
289 return "Barrier probability may not be greater than 1";
293 /* ----------------------------------------------------------------------
294 * Randomly select a new game description.
297 static char *new_game_desc(game_params *params, random_state *rs,
300 tree234 *possibilities, *barriertree;
301 int w, h, x, y, cx, cy, nbarriers;
302 unsigned char *tiles, *barriers;
308 tiles = snewn(w * h, unsigned char);
309 memset(tiles, 0, w * h);
310 barriers = snewn(w * h, unsigned char);
311 memset(barriers, 0, w * h);
317 * Construct the unshuffled grid.
319 * To do this, we simply start at the centre point, repeatedly
320 * choose a random possibility out of the available ways to
321 * extend a used square into an unused one, and do it. After
322 * extending the third line out of a square, we remove the
323 * fourth from the possibilities list to avoid any full-cross
324 * squares (which would make the game too easy because they
325 * only have one orientation).
327 * The slightly worrying thing is the avoidance of full-cross
328 * squares. Can this cause our unsophisticated construction
329 * algorithm to paint itself into a corner, by getting into a
330 * situation where there are some unreached squares and the
331 * only way to reach any of them is to extend a T-piece into a
334 * Answer: no it can't, and here's a proof.
336 * Any contiguous group of such unreachable squares must be
337 * surrounded on _all_ sides by T-pieces pointing away from the
338 * group. (If not, then there is a square which can be extended
339 * into one of the `unreachable' ones, and so it wasn't
340 * unreachable after all.) In particular, this implies that
341 * each contiguous group of unreachable squares must be
342 * rectangular in shape (any deviation from that yields a
343 * non-T-piece next to an `unreachable' square).
345 * So we have a rectangle of unreachable squares, with T-pieces
346 * forming a solid border around the rectangle. The corners of
347 * that border must be connected (since every tile connects all
348 * the lines arriving in it), and therefore the border must
349 * form a closed loop around the rectangle.
351 * But this can't have happened in the first place, since we
352 * _know_ we've avoided creating closed loops! Hence, no such
353 * situation can ever arise, and the naive grid construction
354 * algorithm will guaranteeably result in a complete grid
355 * containing no unreached squares, no full crosses _and_ no
358 possibilities = newtree234(xyd_cmp);
361 add234(possibilities, new_xyd(cx, cy, R));
363 add234(possibilities, new_xyd(cx, cy, U));
365 add234(possibilities, new_xyd(cx, cy, L));
367 add234(possibilities, new_xyd(cx, cy, D));
369 while (count234(possibilities) > 0) {
372 int x1, y1, d1, x2, y2, d2, d;
375 * Extract a randomly chosen possibility from the list.
377 i = random_upto(rs, count234(possibilities));
378 xyd = delpos234(possibilities, i);
384 OFFSET(x2, y2, x1, y1, d1, params);
387 printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n",
388 x1, y1, "0RU3L567D9abcdef"[d1], x2, y2, "0RU3L567D9abcdef"[d2]);
392 * Make the connection. (We should be moving to an as yet
395 index(params, tiles, x1, y1) |= d1;
396 assert(index(params, tiles, x2, y2) == 0);
397 index(params, tiles, x2, y2) |= d2;
400 * If we have created a T-piece, remove its last
403 if (COUNT(index(params, tiles, x1, y1)) == 3) {
404 struct xyd xyd1, *xydp;
408 xyd1.direction = 0x0F ^ index(params, tiles, x1, y1);
410 xydp = find234(possibilities, &xyd1, NULL);
414 printf("T-piece; removing (%d,%d,%c)\n",
415 xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
417 del234(possibilities, xydp);
423 * Remove all other possibilities that were pointing at the
424 * tile we've just moved into.
426 for (d = 1; d < 0x10; d <<= 1) {
428 struct xyd xyd1, *xydp;
430 OFFSET(x3, y3, x2, y2, d, params);
437 xydp = find234(possibilities, &xyd1, NULL);
441 printf("Loop avoidance; removing (%d,%d,%c)\n",
442 xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
444 del234(possibilities, xydp);
450 * Add new possibilities to the list for moving _out_ of
451 * the tile we have just moved into.
453 for (d = 1; d < 0x10; d <<= 1) {
457 continue; /* we've got this one already */
459 if (!params->wrapping) {
460 if (d == U && y2 == 0)
462 if (d == D && y2 == h-1)
464 if (d == L && x2 == 0)
466 if (d == R && x2 == w-1)
470 OFFSET(x3, y3, x2, y2, d, params);
472 if (index(params, tiles, x3, y3))
473 continue; /* this would create a loop */
476 printf("New frontier; adding (%d,%d,%c)\n",
477 x2, y2, "0RU3L567D9abcdef"[d]);
479 add234(possibilities, new_xyd(x2, y2, d));
482 /* Having done that, we should have no possibilities remaining. */
483 assert(count234(possibilities) == 0);
484 freetree234(possibilities);
487 * Now compute a list of the possible barrier locations.
489 barriertree = newtree234(xyd_cmp);
490 for (y = 0; y < h; y++) {
491 for (x = 0; x < w; x++) {
493 if (!(index(params, tiles, x, y) & R) &&
494 (params->wrapping || x < w-1))
495 add234(barriertree, new_xyd(x, y, R));
496 if (!(index(params, tiles, x, y) & D) &&
497 (params->wrapping || y < h-1))
498 add234(barriertree, new_xyd(x, y, D));
503 * Save the unshuffled grid in an aux_info.
506 game_aux_info *solution;
508 solution = snew(game_aux_info);
510 solution->height = h;
511 solution->tiles = snewn(w * h, unsigned char);
512 memcpy(solution->tiles, tiles, w * h);
518 * Now shuffle the grid.
520 for (y = 0; y < h; y++) {
521 for (x = 0; x < w; x++) {
522 int orig = index(params, tiles, x, y);
523 int rot = random_upto(rs, 4);
524 index(params, tiles, x, y) = ROT(orig, rot);
529 * And now choose barrier locations. (We carefully do this
530 * _after_ shuffling, so that changing the barrier rate in the
531 * params while keeping the random seed the same will give the
532 * same shuffled grid and _only_ change the barrier locations.
533 * Also the way we choose barrier locations, by repeatedly
534 * choosing one possibility from the list until we have enough,
535 * is designed to ensure that raising the barrier rate while
536 * keeping the seed the same will provide a superset of the
537 * previous barrier set - i.e. if you ask for 10 barriers, and
538 * then decide that's still too hard and ask for 20, you'll get
539 * the original 10 plus 10 more, rather than getting 20 new
540 * ones and the chance of remembering your first 10.)
542 nbarriers = (int)(params->barrier_probability * count234(barriertree));
543 assert(nbarriers >= 0 && nbarriers <= count234(barriertree));
545 while (nbarriers > 0) {
548 int x1, y1, d1, x2, y2, d2;
551 * Extract a randomly chosen barrier from the list.
553 i = random_upto(rs, count234(barriertree));
554 xyd = delpos234(barriertree, i);
563 OFFSET(x2, y2, x1, y1, d1, params);
566 index(params, barriers, x1, y1) |= d1;
567 index(params, barriers, x2, y2) |= d2;
573 * Clean up the rest of the barrier list.
578 while ( (xyd = delpos234(barriertree, 0)) != NULL)
581 freetree234(barriertree);
585 * Finally, encode the grid into a string game description.
587 * My syntax is extremely simple: each square is encoded as a
588 * hex digit in which bit 0 means a connection on the right,
589 * bit 1 means up, bit 2 left and bit 3 down. (i.e. the same
590 * encoding as used internally). Each digit is followed by
591 * optional barrier indicators: `v' means a vertical barrier to
592 * the right of it, and `h' means a horizontal barrier below
595 desc = snewn(w * h * 3 + 1, char);
597 for (y = 0; y < h; y++) {
598 for (x = 0; x < w; x++) {
599 *p++ = "0123456789abcdef"[index(params, tiles, x, y)];
600 if ((params->wrapping || x < w-1) &&
601 (index(params, barriers, x, y) & R))
603 if ((params->wrapping || y < h-1) &&
604 (index(params, barriers, x, y) & D))
608 assert(p - desc <= w*h*3);
617 static void game_free_aux_info(game_aux_info *aux)
623 static char *validate_desc(game_params *params, char *desc)
625 int w = params->width, h = params->height;
628 for (i = 0; i < w*h; i++) {
629 if (*desc >= '0' && *desc <= '9')
631 else if (*desc >= 'a' && *desc <= 'f')
633 else if (*desc >= 'A' && *desc <= 'F')
636 return "Game description shorter than expected";
638 return "Game description contained unexpected character";
640 while (*desc == 'h' || *desc == 'v')
644 return "Game description longer than expected";
649 /* ----------------------------------------------------------------------
650 * Construct an initial game state, given a description and parameters.
653 static game_state *new_game(game_params *params, char *desc)
658 assert(params->width > 0 && params->height > 0);
659 assert(params->width > 1 || params->height > 1);
662 * Create a blank game state.
664 state = snew(game_state);
665 w = state->width = params->width;
666 h = state->height = params->height;
667 state->cx = state->width / 2;
668 state->cy = state->height / 2;
669 state->wrapping = params->wrapping;
670 state->last_rotate_dir = state->last_rotate_x = state->last_rotate_y = 0;
671 state->completed = state->used_solve = state->just_used_solve = FALSE;
672 state->tiles = snewn(state->width * state->height, unsigned char);
673 memset(state->tiles, 0, state->width * state->height);
674 state->barriers = snewn(state->width * state->height, unsigned char);
675 memset(state->barriers, 0, state->width * state->height);
678 * Parse the game description into the grid.
680 for (y = 0; y < h; y++) {
681 for (x = 0; x < w; x++) {
682 if (*desc >= '0' && *desc <= '9')
683 tile(state, x, y) = *desc - '0';
684 else if (*desc >= 'a' && *desc <= 'f')
685 tile(state, x, y) = *desc - 'a' + 10;
686 else if (*desc >= 'A' && *desc <= 'F')
687 tile(state, x, y) = *desc - 'A' + 10;
690 while (*desc == 'h' || *desc == 'v') {
697 OFFSET(x2, y2, x, y, d1, state);
700 barrier(state, x, y) |= d1;
701 barrier(state, x2, y2) |= d2;
709 * Set up border barriers if this is a non-wrapping game.
711 if (!state->wrapping) {
712 for (x = 0; x < state->width; x++) {
713 barrier(state, x, 0) |= U;
714 barrier(state, x, state->height-1) |= D;
716 for (y = 0; y < state->height; y++) {
717 barrier(state, 0, y) |= L;
718 barrier(state, state->width-1, y) |= R;
723 * Set up the barrier corner flags, for drawing barriers
724 * prettily when they meet.
726 for (y = 0; y < state->height; y++) {
727 for (x = 0; x < state->width; x++) {
730 for (dir = 1; dir < 0x10; dir <<= 1) {
732 int x1, y1, x2, y2, x3, y3;
735 if (!(barrier(state, x, y) & dir))
738 if (barrier(state, x, y) & dir2)
741 x1 = x + X(dir), y1 = y + Y(dir);
742 if (x1 >= 0 && x1 < state->width &&
743 y1 >= 0 && y1 < state->height &&
744 (barrier(state, x1, y1) & dir2))
747 x2 = x + X(dir2), y2 = y + Y(dir2);
748 if (x2 >= 0 && x2 < state->width &&
749 y2 >= 0 && y2 < state->height &&
750 (barrier(state, x2, y2) & dir))
754 barrier(state, x, y) |= (dir << 4);
755 if (x1 >= 0 && x1 < state->width &&
756 y1 >= 0 && y1 < state->height)
757 barrier(state, x1, y1) |= (A(dir) << 4);
758 if (x2 >= 0 && x2 < state->width &&
759 y2 >= 0 && y2 < state->height)
760 barrier(state, x2, y2) |= (C(dir) << 4);
761 x3 = x + X(dir) + X(dir2), y3 = y + Y(dir) + Y(dir2);
762 if (x3 >= 0 && x3 < state->width &&
763 y3 >= 0 && y3 < state->height)
764 barrier(state, x3, y3) |= (F(dir) << 4);
773 static game_state *dup_game(game_state *state)
777 ret = snew(game_state);
778 ret->width = state->width;
779 ret->height = state->height;
782 ret->wrapping = state->wrapping;
783 ret->completed = state->completed;
784 ret->used_solve = state->used_solve;
785 ret->just_used_solve = state->just_used_solve;
786 ret->last_rotate_dir = state->last_rotate_dir;
787 ret->last_rotate_x = state->last_rotate_x;
788 ret->last_rotate_y = state->last_rotate_y;
789 ret->tiles = snewn(state->width * state->height, unsigned char);
790 memcpy(ret->tiles, state->tiles, state->width * state->height);
791 ret->barriers = snewn(state->width * state->height, unsigned char);
792 memcpy(ret->barriers, state->barriers, state->width * state->height);
797 static void free_game(game_state *state)
800 sfree(state->barriers);
804 static game_state *solve_game(game_state *state, game_aux_info *aux,
810 *error = "Solution not known for this puzzle";
814 assert(aux->width == state->width);
815 assert(aux->height == state->height);
816 ret = dup_game(state);
817 memcpy(ret->tiles, aux->tiles, ret->width * ret->height);
818 ret->used_solve = ret->just_used_solve = TRUE;
819 ret->completed = TRUE;
824 static char *game_text_format(game_state *state)
829 /* ----------------------------------------------------------------------
834 * Compute which squares are reachable from the centre square, as a
835 * quick visual aid to determining how close the game is to
836 * completion. This is also a simple way to tell if the game _is_
837 * completed - just call this function and see whether every square
840 static unsigned char *compute_active(game_state *state)
842 unsigned char *active;
846 active = snewn(state->width * state->height, unsigned char);
847 memset(active, 0, state->width * state->height);
850 * We only store (x,y) pairs in todo, but it's easier to reuse
851 * xyd_cmp and just store direction 0 every time.
853 todo = newtree234(xyd_cmp);
854 index(state, active, state->cx, state->cy) = ACTIVE;
855 add234(todo, new_xyd(state->cx, state->cy, 0));
857 while ( (xyd = delpos234(todo, 0)) != NULL) {
858 int x1, y1, d1, x2, y2, d2;
864 for (d1 = 1; d1 < 0x10; d1 <<= 1) {
865 OFFSET(x2, y2, x1, y1, d1, state);
869 * If the next tile in this direction is connected to
870 * us, and there isn't a barrier in the way, and it
871 * isn't already marked active, then mark it active and
872 * add it to the to-examine list.
874 if ((tile(state, x1, y1) & d1) &&
875 (tile(state, x2, y2) & d2) &&
876 !(barrier(state, x1, y1) & d1) &&
877 !index(state, active, x2, y2)) {
878 index(state, active, x2, y2) = ACTIVE;
879 add234(todo, new_xyd(x2, y2, 0));
883 /* Now we expect the todo list to have shrunk to zero size. */
884 assert(count234(todo) == 0);
893 random_state *rs; /* used for jumbling */
896 static game_ui *new_ui(game_state *state)
900 game_ui *ui = snew(game_ui);
901 ui->cur_x = state->width / 2;
902 ui->cur_y = state->height / 2;
903 ui->cur_visible = FALSE;
904 get_random_seed(&seed, &seedsize);
905 ui->rs = random_init(seed, seedsize);
911 static void free_ui(game_ui *ui)
917 /* ----------------------------------------------------------------------
920 static game_state *make_move(game_state *state, game_ui *ui,
921 int x, int y, int button)
923 game_state *ret, *nullret;
928 if (button == LEFT_BUTTON ||
929 button == MIDDLE_BUTTON ||
930 button == RIGHT_BUTTON) {
932 if (ui->cur_visible) {
933 ui->cur_visible = FALSE;
938 * The button must have been clicked on a valid tile.
940 x -= WINDOW_OFFSET + TILE_BORDER;
941 y -= WINDOW_OFFSET + TILE_BORDER;
946 if (tx >= state->width || ty >= state->height)
948 if (x % TILE_SIZE >= TILE_SIZE - TILE_BORDER ||
949 y % TILE_SIZE >= TILE_SIZE - TILE_BORDER)
951 } else if (button == CURSOR_UP || button == CURSOR_DOWN ||
952 button == CURSOR_RIGHT || button == CURSOR_LEFT) {
953 if (button == CURSOR_UP && ui->cur_y > 0)
955 else if (button == CURSOR_DOWN && ui->cur_y < state->height-1)
957 else if (button == CURSOR_LEFT && ui->cur_x > 0)
959 else if (button == CURSOR_RIGHT && ui->cur_x < state->width-1)
962 return nullret; /* no cursor movement */
963 ui->cur_visible = TRUE;
964 return state; /* UI activity has occurred */
965 } else if (button == 'a' || button == 's' || button == 'd' ||
966 button == 'A' || button == 'S' || button == 'D') {
969 if (button == 'a' || button == 'A')
970 button = LEFT_BUTTON;
971 else if (button == 's' || button == 'S')
972 button = MIDDLE_BUTTON;
973 else if (button == 'd' || button == 'D')
974 button = RIGHT_BUTTON;
975 ui->cur_visible = TRUE;
976 } else if (button == 'j' || button == 'J') {
977 /* XXX should we have some mouse control for this? */
978 button = 'J'; /* canonify */
979 tx = ty = -1; /* shut gcc up :( */
984 * The middle button locks or unlocks a tile. (A locked tile
985 * cannot be turned, and is visually marked as being locked.
986 * This is a convenience for the player, so that once they are
987 * sure which way round a tile goes, they can lock it and thus
988 * avoid forgetting later on that they'd already done that one;
989 * and the locking also prevents them turning the tile by
990 * accident. If they change their mind, another middle click
993 if (button == MIDDLE_BUTTON) {
995 ret = dup_game(state);
996 ret->just_used_solve = FALSE;
997 tile(ret, tx, ty) ^= LOCKED;
998 ret->last_rotate_dir = ret->last_rotate_x = ret->last_rotate_y = 0;
1001 } else if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
1004 * The left and right buttons have no effect if clicked on a
1007 if (tile(state, tx, ty) & LOCKED)
1011 * Otherwise, turn the tile one way or the other. Left button
1012 * turns anticlockwise; right button turns clockwise.
1014 ret = dup_game(state);
1015 ret->just_used_solve = FALSE;
1016 orig = tile(ret, tx, ty);
1017 if (button == LEFT_BUTTON) {
1018 tile(ret, tx, ty) = A(orig);
1019 ret->last_rotate_dir = +1;
1021 tile(ret, tx, ty) = C(orig);
1022 ret->last_rotate_dir = -1;
1024 ret->last_rotate_x = tx;
1025 ret->last_rotate_y = ty;
1027 } else if (button == 'J') {
1030 * Jumble all unlocked tiles to random orientations.
1033 ret = dup_game(state);
1034 ret->just_used_solve = FALSE;
1035 for (jy = 0; jy < ret->height; jy++) {
1036 for (jx = 0; jx < ret->width; jx++) {
1037 if (!(tile(ret, jx, jy) & LOCKED)) {
1038 int rot = random_upto(ui->rs, 4);
1039 orig = tile(ret, jx, jy);
1040 tile(ret, jx, jy) = ROT(orig, rot);
1044 ret->last_rotate_dir = 0; /* suppress animation */
1045 ret->last_rotate_x = ret->last_rotate_y = 0;
1050 * Check whether the game has been completed.
1053 unsigned char *active = compute_active(ret);
1055 int complete = TRUE;
1057 for (x1 = 0; x1 < ret->width; x1++)
1058 for (y1 = 0; y1 < ret->height; y1++)
1059 if ((tile(ret, x1, y1) & 0xF) && !index(ret, active, x1, y1)) {
1061 goto break_label; /* break out of two loops at once */
1068 ret->completed = TRUE;
1074 /* ----------------------------------------------------------------------
1075 * Routines for drawing the game position on the screen.
1078 struct game_drawstate {
1081 unsigned char *visible;
1084 static game_drawstate *game_new_drawstate(game_state *state)
1086 game_drawstate *ds = snew(game_drawstate);
1088 ds->started = FALSE;
1089 ds->width = state->width;
1090 ds->height = state->height;
1091 ds->visible = snewn(state->width * state->height, unsigned char);
1092 memset(ds->visible, 0xFF, state->width * state->height);
1097 static void game_free_drawstate(game_drawstate *ds)
1103 static void game_size(game_params *params, int *x, int *y)
1105 *x = WINDOW_OFFSET * 2 + TILE_SIZE * params->width + TILE_BORDER;
1106 *y = WINDOW_OFFSET * 2 + TILE_SIZE * params->height + TILE_BORDER;
1109 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
1113 ret = snewn(NCOLOURS * 3, float);
1114 *ncolours = NCOLOURS;
1117 * Basic background colour is whatever the front end thinks is
1118 * a sensible default.
1120 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1125 ret[COL_WIRE * 3 + 0] = 0.0F;
1126 ret[COL_WIRE * 3 + 1] = 0.0F;
1127 ret[COL_WIRE * 3 + 2] = 0.0F;
1130 * Powered wires and powered endpoints are cyan.
1132 ret[COL_POWERED * 3 + 0] = 0.0F;
1133 ret[COL_POWERED * 3 + 1] = 1.0F;
1134 ret[COL_POWERED * 3 + 2] = 1.0F;
1139 ret[COL_BARRIER * 3 + 0] = 1.0F;
1140 ret[COL_BARRIER * 3 + 1] = 0.0F;
1141 ret[COL_BARRIER * 3 + 2] = 0.0F;
1144 * Unpowered endpoints are blue.
1146 ret[COL_ENDPOINT * 3 + 0] = 0.0F;
1147 ret[COL_ENDPOINT * 3 + 1] = 0.0F;
1148 ret[COL_ENDPOINT * 3 + 2] = 1.0F;
1151 * Tile borders are a darker grey than the background.
1153 ret[COL_BORDER * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
1154 ret[COL_BORDER * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
1155 ret[COL_BORDER * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2];
1158 * Locked tiles are a grey in between those two.
1160 ret[COL_LOCKED * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0];
1161 ret[COL_LOCKED * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1];
1162 ret[COL_LOCKED * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2];
1167 static void draw_thick_line(frontend *fe, int x1, int y1, int x2, int y2,
1170 draw_line(fe, x1-1, y1, x2-1, y2, COL_WIRE);
1171 draw_line(fe, x1+1, y1, x2+1, y2, COL_WIRE);
1172 draw_line(fe, x1, y1-1, x2, y2-1, COL_WIRE);
1173 draw_line(fe, x1, y1+1, x2, y2+1, COL_WIRE);
1174 draw_line(fe, x1, y1, x2, y2, colour);
1177 static void draw_rect_coords(frontend *fe, int x1, int y1, int x2, int y2,
1180 int mx = (x1 < x2 ? x1 : x2);
1181 int my = (y1 < y2 ? y1 : y2);
1182 int dx = (x2 + x1 - 2*mx + 1);
1183 int dy = (y2 + y1 - 2*my + 1);
1185 draw_rect(fe, mx, my, dx, dy, colour);
1188 static void draw_barrier_corner(frontend *fe, int x, int y, int dir, int phase)
1190 int bx = WINDOW_OFFSET + TILE_SIZE * x;
1191 int by = WINDOW_OFFSET + TILE_SIZE * y;
1192 int x1, y1, dx, dy, dir2;
1197 dx = X(dir) + X(dir2);
1198 dy = Y(dir) + Y(dir2);
1199 x1 = (dx > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
1200 y1 = (dy > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
1203 draw_rect_coords(fe, bx+x1, by+y1,
1204 bx+x1-TILE_BORDER*dx, by+y1-(TILE_BORDER-1)*dy,
1206 draw_rect_coords(fe, bx+x1, by+y1,
1207 bx+x1-(TILE_BORDER-1)*dx, by+y1-TILE_BORDER*dy,
1210 draw_rect_coords(fe, bx+x1, by+y1,
1211 bx+x1-(TILE_BORDER-1)*dx, by+y1-(TILE_BORDER-1)*dy,
1216 static void draw_barrier(frontend *fe, int x, int y, int dir, int phase)
1218 int bx = WINDOW_OFFSET + TILE_SIZE * x;
1219 int by = WINDOW_OFFSET + TILE_SIZE * y;
1222 x1 = (X(dir) > 0 ? TILE_SIZE : X(dir) == 0 ? TILE_BORDER : 0);
1223 y1 = (Y(dir) > 0 ? TILE_SIZE : Y(dir) == 0 ? TILE_BORDER : 0);
1224 w = (X(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER);
1225 h = (Y(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER);
1228 draw_rect(fe, bx+x1-X(dir), by+y1-Y(dir), w, h, COL_WIRE);
1230 draw_rect(fe, bx+x1, by+y1, w, h, COL_BARRIER);
1234 static void draw_tile(frontend *fe, game_state *state, int x, int y, int tile,
1235 float angle, int cursor)
1237 int bx = WINDOW_OFFSET + TILE_SIZE * x;
1238 int by = WINDOW_OFFSET + TILE_SIZE * y;
1240 float cx, cy, ex, ey, tx, ty;
1241 int dir, col, phase;
1244 * When we draw a single tile, we must draw everything up to
1245 * and including the borders around the tile. This means that
1246 * if the neighbouring tiles have connections to those borders,
1247 * we must draw those connections on the borders themselves.
1249 * This would be terribly fiddly if we ever had to draw a tile
1250 * while its neighbour was in mid-rotate, because we'd have to
1251 * arrange to _know_ that the neighbour was being rotated and
1252 * hence had an anomalous effect on the redraw of this tile.
1253 * Fortunately, the drawing algorithm avoids ever calling us in
1254 * this circumstance: we're either drawing lots of straight
1255 * tiles at game start or after a move is complete, or we're
1256 * repeatedly drawing only the rotating tile. So no problem.
1260 * So. First blank the tile out completely: draw a big
1261 * rectangle in border colour, and a smaller rectangle in
1262 * background colour to fill it in.
1264 draw_rect(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER,
1266 draw_rect(fe, bx+TILE_BORDER, by+TILE_BORDER,
1267 TILE_SIZE-TILE_BORDER, TILE_SIZE-TILE_BORDER,
1268 tile & LOCKED ? COL_LOCKED : COL_BACKGROUND);
1271 * Draw an inset outline rectangle as a cursor, in whichever of
1272 * COL_LOCKED and COL_BACKGROUND we aren't currently drawing
1276 draw_line(fe, bx+TILE_SIZE/8, by+TILE_SIZE/8,
1277 bx+TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8,
1278 tile & LOCKED ? COL_BACKGROUND : COL_LOCKED);
1279 draw_line(fe, bx+TILE_SIZE/8, by+TILE_SIZE/8,
1280 bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE/8,
1281 tile & LOCKED ? COL_BACKGROUND : COL_LOCKED);
1282 draw_line(fe, bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE/8,
1283 bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8,
1284 tile & LOCKED ? COL_BACKGROUND : COL_LOCKED);
1285 draw_line(fe, bx+TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8,
1286 bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8,
1287 tile & LOCKED ? COL_BACKGROUND : COL_LOCKED);
1291 * Set up the rotation matrix.
1293 matrix[0] = (float)cos(angle * PI / 180.0);
1294 matrix[1] = (float)-sin(angle * PI / 180.0);
1295 matrix[2] = (float)sin(angle * PI / 180.0);
1296 matrix[3] = (float)cos(angle * PI / 180.0);
1301 cx = cy = TILE_BORDER + (TILE_SIZE-TILE_BORDER) / 2.0F - 0.5F;
1302 col = (tile & ACTIVE ? COL_POWERED : COL_WIRE);
1303 for (dir = 1; dir < 0x10; dir <<= 1) {
1305 ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir);
1306 ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir);
1307 MATMUL(tx, ty, matrix, ex, ey);
1308 draw_thick_line(fe, bx+(int)cx, by+(int)cy,
1309 bx+(int)(cx+tx), by+(int)(cy+ty),
1313 for (dir = 1; dir < 0x10; dir <<= 1) {
1315 ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir);
1316 ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir);
1317 MATMUL(tx, ty, matrix, ex, ey);
1318 draw_line(fe, bx+(int)cx, by+(int)cy,
1319 bx+(int)(cx+tx), by+(int)(cy+ty), col);
1324 * Draw the box in the middle. We do this in blue if the tile
1325 * is an unpowered endpoint, in cyan if the tile is a powered
1326 * endpoint, in black if the tile is the centrepiece, and
1327 * otherwise not at all.
1330 if (x == state->cx && y == state->cy)
1332 else if (COUNT(tile) == 1) {
1333 col = (tile & ACTIVE ? COL_POWERED : COL_ENDPOINT);
1338 points[0] = +1; points[1] = +1;
1339 points[2] = +1; points[3] = -1;
1340 points[4] = -1; points[5] = -1;
1341 points[6] = -1; points[7] = +1;
1343 for (i = 0; i < 8; i += 2) {
1344 ex = (TILE_SIZE * 0.24F) * points[i];
1345 ey = (TILE_SIZE * 0.24F) * points[i+1];
1346 MATMUL(tx, ty, matrix, ex, ey);
1347 points[i] = bx+(int)(cx+tx);
1348 points[i+1] = by+(int)(cy+ty);
1351 draw_polygon(fe, points, 4, TRUE, col);
1352 draw_polygon(fe, points, 4, FALSE, COL_WIRE);
1356 * Draw the points on the border if other tiles are connected
1359 for (dir = 1; dir < 0x10; dir <<= 1) {
1360 int dx, dy, px, py, lx, ly, vx, vy, ox, oy;
1368 if (ox < 0 || ox >= state->width || oy < 0 || oy >= state->height)
1371 if (!(tile(state, ox, oy) & F(dir)))
1374 px = bx + (int)(dx>0 ? TILE_SIZE + TILE_BORDER - 1 : dx<0 ? 0 : cx);
1375 py = by + (int)(dy>0 ? TILE_SIZE + TILE_BORDER - 1 : dy<0 ? 0 : cy);
1376 lx = dx * (TILE_BORDER-1);
1377 ly = dy * (TILE_BORDER-1);
1381 if (angle == 0.0 && (tile & dir)) {
1383 * If we are fully connected to the other tile, we must
1384 * draw right across the tile border. (We can use our
1385 * own ACTIVE state to determine what colour to do this
1386 * in: if we are fully connected to the other tile then
1387 * the two ACTIVE states will be the same.)
1389 draw_rect_coords(fe, px-vx, py-vy, px+lx+vx, py+ly+vy, COL_WIRE);
1390 draw_rect_coords(fe, px, py, px+lx, py+ly,
1391 (tile & ACTIVE) ? COL_POWERED : COL_WIRE);
1394 * The other tile extends into our border, but isn't
1395 * actually connected to us. Just draw a single black
1398 draw_rect_coords(fe, px, py, px, py, COL_WIRE);
1403 * Draw barrier corners, and then barriers.
1405 for (phase = 0; phase < 2; phase++) {
1406 for (dir = 1; dir < 0x10; dir <<= 1)
1407 if (barrier(state, x, y) & (dir << 4))
1408 draw_barrier_corner(fe, x, y, dir << 4, phase);
1409 for (dir = 1; dir < 0x10; dir <<= 1)
1410 if (barrier(state, x, y) & dir)
1411 draw_barrier(fe, x, y, dir, phase);
1414 draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER);
1417 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1418 game_state *state, int dir, game_ui *ui, float t, float ft)
1420 int x, y, tx, ty, frame, last_rotate_dir;
1421 unsigned char *active;
1425 * Clear the screen and draw the exterior barrier lines if this
1426 * is our first call.
1434 WINDOW_OFFSET * 2 + TILE_SIZE * state->width + TILE_BORDER,
1435 WINDOW_OFFSET * 2 + TILE_SIZE * state->height + TILE_BORDER,
1437 draw_update(fe, 0, 0,
1438 WINDOW_OFFSET*2 + TILE_SIZE*state->width + TILE_BORDER,
1439 WINDOW_OFFSET*2 + TILE_SIZE*state->height + TILE_BORDER);
1441 for (phase = 0; phase < 2; phase++) {
1443 for (x = 0; x < ds->width; x++) {
1444 if (barrier(state, x, 0) & UL)
1445 draw_barrier_corner(fe, x, -1, LD, phase);
1446 if (barrier(state, x, 0) & RU)
1447 draw_barrier_corner(fe, x, -1, DR, phase);
1448 if (barrier(state, x, 0) & U)
1449 draw_barrier(fe, x, -1, D, phase);
1450 if (barrier(state, x, ds->height-1) & DR)
1451 draw_barrier_corner(fe, x, ds->height, RU, phase);
1452 if (barrier(state, x, ds->height-1) & LD)
1453 draw_barrier_corner(fe, x, ds->height, UL, phase);
1454 if (barrier(state, x, ds->height-1) & D)
1455 draw_barrier(fe, x, ds->height, U, phase);
1458 for (y = 0; y < ds->height; y++) {
1459 if (barrier(state, 0, y) & UL)
1460 draw_barrier_corner(fe, -1, y, RU, phase);
1461 if (barrier(state, 0, y) & LD)
1462 draw_barrier_corner(fe, -1, y, DR, phase);
1463 if (barrier(state, 0, y) & L)
1464 draw_barrier(fe, -1, y, R, phase);
1465 if (barrier(state, ds->width-1, y) & RU)
1466 draw_barrier_corner(fe, ds->width, y, UL, phase);
1467 if (barrier(state, ds->width-1, y) & DR)
1468 draw_barrier_corner(fe, ds->width, y, LD, phase);
1469 if (barrier(state, ds->width-1, y) & R)
1470 draw_barrier(fe, ds->width, y, L, phase);
1476 last_rotate_dir = dir==-1 ? oldstate->last_rotate_dir :
1477 state->last_rotate_dir;
1478 if (oldstate && (t < ROTATE_TIME) && last_rotate_dir) {
1480 * We're animating a single tile rotation. Find the turning
1483 tx = (dir==-1 ? oldstate->last_rotate_x : state->last_rotate_x);
1484 ty = (dir==-1 ? oldstate->last_rotate_y : state->last_rotate_y);
1485 angle = last_rotate_dir * dir * 90.0F * (t / ROTATE_TIME);
1492 * We're animating a completion flash. Find which frame
1495 frame = (int)(ft / FLASH_FRAME);
1499 * Draw any tile which differs from the way it was last drawn.
1501 active = compute_active(state);
1503 for (x = 0; x < ds->width; x++)
1504 for (y = 0; y < ds->height; y++) {
1505 unsigned char c = tile(state, x, y) | index(state, active, x, y);
1508 * In a completion flash, we adjust the LOCKED bit
1509 * depending on our distance from the centre point and
1513 int xdist, ydist, dist;
1514 xdist = (x < state->cx ? state->cx - x : x - state->cx);
1515 ydist = (y < state->cy ? state->cy - y : y - state->cy);
1516 dist = (xdist > ydist ? xdist : ydist);
1518 if (frame >= dist && frame < dist+4) {
1519 int lock = (frame - dist) & 1;
1520 lock = lock ? LOCKED : 0;
1521 c = (c &~ LOCKED) | lock;
1525 if (index(state, ds->visible, x, y) != c ||
1526 index(state, ds->visible, x, y) == 0xFF ||
1527 (x == tx && y == ty) ||
1528 (ui->cur_visible && x == ui->cur_x && y == ui->cur_y)) {
1529 draw_tile(fe, state, x, y, c,
1530 (x == tx && y == ty ? angle : 0.0F),
1531 (ui->cur_visible && x == ui->cur_x && y == ui->cur_y));
1532 if ((x == tx && y == ty) ||
1533 (ui->cur_visible && x == ui->cur_x && y == ui->cur_y))
1534 index(state, ds->visible, x, y) = 0xFF;
1536 index(state, ds->visible, x, y) = c;
1541 * Update the status bar.
1544 char statusbuf[256];
1547 n = state->width * state->height;
1548 for (i = a = n2 = 0; i < n; i++) {
1551 if (state->tiles[i] & 0xF)
1555 sprintf(statusbuf, "%sActive: %d/%d",
1556 (state->used_solve ? "Auto-solved. " :
1557 state->completed ? "COMPLETED! " : ""), a, n2);
1559 status_bar(fe, statusbuf);
1565 static float game_anim_length(game_state *oldstate,
1566 game_state *newstate, int dir)
1568 int last_rotate_dir;
1571 * Don't animate an auto-solve move.
1573 if ((dir > 0 && newstate->just_used_solve) ||
1574 (dir < 0 && oldstate->just_used_solve))
1578 * Don't animate if last_rotate_dir is zero.
1580 last_rotate_dir = dir==-1 ? oldstate->last_rotate_dir :
1581 newstate->last_rotate_dir;
1582 if (last_rotate_dir)
1588 static float game_flash_length(game_state *oldstate,
1589 game_state *newstate, int dir)
1592 * If the game has just been completed, we display a completion
1595 if (!oldstate->completed && newstate->completed &&
1596 !oldstate->used_solve && !newstate->used_solve) {
1599 if (size < newstate->cx+1)
1600 size = newstate->cx+1;
1601 if (size < newstate->cy+1)
1602 size = newstate->cy+1;
1603 if (size < newstate->width - newstate->cx)
1604 size = newstate->width - newstate->cx;
1605 if (size < newstate->height - newstate->cy)
1606 size = newstate->height - newstate->cy;
1607 return FLASH_FRAME * (size+4);
1613 static int game_wants_statusbar(void)
1622 const struct game thegame = {
1630 TRUE, game_configure, custom_params,
1639 FALSE, game_text_format,
1646 game_free_drawstate,
1650 game_wants_statusbar,