2 * netslide.c: cross between Net and Sixteen, courtesy of Richard
16 const char *const game_name = "Netslide";
17 const char *const game_winhelp_topic = "games.netslide";
18 const int game_can_configure = TRUE;
20 #define PI 3.141592653589793238462643383279502884197169399
22 #define MATMUL(xr,yr,m,x,y) do { \
23 float rx, ry, xx = (x), yy = (y), *mat = (m); \
24 rx = mat[0] * xx + mat[2] * yy; \
25 ry = mat[1] * xx + mat[3] * yy; \
26 (xr) = rx; (yr) = ry; \
29 /* Direction and other bitfields */
36 /* Corner flags go in the barriers array */
42 /* Get tile at given coordinate */
43 #define T(state, x, y) ( (y) * (state)->width + (x) )
45 /* Rotations: Anticlockwise, Clockwise, Flip, general rotate */
46 #define A(x) ( (((x) & 0x07) << 1) | (((x) & 0x08) >> 3) )
47 #define C(x) ( (((x) & 0x0E) >> 1) | (((x) & 0x01) << 3) )
48 #define F(x) ( (((x) & 0x0C) >> 2) | (((x) & 0x03) << 2) )
49 #define ROT(x, n) ( ((n)&3) == 0 ? (x) : \
50 ((n)&3) == 1 ? A(x) : \
51 ((n)&3) == 2 ? F(x) : C(x) )
53 /* X and Y displacements */
54 #define X(x) ( (x) == R ? +1 : (x) == L ? -1 : 0 )
55 #define Y(x) ( (x) == D ? +1 : (x) == U ? -1 : 0 )
58 #define COUNT(x) ( (((x) & 0x08) >> 3) + (((x) & 0x04) >> 2) + \
59 (((x) & 0x02) >> 1) + ((x) & 0x01) )
62 #define BORDER TILE_SIZE
64 #define WINDOW_OFFSET 0
66 #define ANIM_TIME 0.13F
67 #define FLASH_FRAME 0.07F
86 float barrier_probability;
90 int width, height, cx, cy, wrapping, completed;
93 /* position (row or col number, starting at 0) of last move. */
94 int last_move_row, last_move_col;
96 /* direction of last move: +1 or -1 */
100 unsigned char *barriers;
103 #define OFFSET(x2,y2,x1,y1,dir,state) \
104 ( (x2) = ((x1) + (state)->width + X((dir))) % (state)->width, \
105 (y2) = ((y1) + (state)->height + Y((dir))) % (state)->height)
107 #define index(state, a, x, y) ( a[(y) * (state)->width + (x)] )
108 #define tile(state, x, y) index(state, (state)->tiles, x, y)
109 #define barrier(state, x, y) index(state, (state)->barriers, x, y)
115 static int xyd_cmp(void *av, void *bv) {
116 struct xyd *a = (struct xyd *)av;
117 struct xyd *b = (struct xyd *)bv;
126 if (a->direction < b->direction)
128 if (a->direction > b->direction)
133 static struct xyd *new_xyd(int x, int y, int direction)
135 struct xyd *xyd = snew(struct xyd);
138 xyd->direction = direction;
142 void slide_col(game_state *state, int dir, int col);
143 void slide_row(game_state *state, int dir, int row);
145 /* ----------------------------------------------------------------------
146 * Manage game parameters.
148 game_params *default_params(void)
150 game_params *ret = snew(game_params);
154 ret->wrapping = FALSE;
155 ret->barrier_probability = 1.0;
160 int game_fetch_preset(int i, char **name, game_params **params)
164 static const struct { int x, y, wrap, bprob; const char* desc; } values[] = {
165 {3, 3, FALSE, 1.0, " easy"},
166 {3, 3, FALSE, 0.0, " medium"},
167 {3, 3, TRUE, 0.0, " hard"},
168 {4, 4, FALSE, 1.0, " easy"},
169 {4, 4, FALSE, 0.0, " medium"},
170 {4, 4, TRUE, 0.0, " hard"},
171 {5, 5, FALSE, 1.0, " easy"},
172 {5, 5, FALSE, 0.0, " medium"},
173 {5, 5, TRUE, 0.0, " hard"},
176 if (i < 0 || i >= lenof(values))
179 ret = snew(game_params);
180 ret->width = values[i].x;
181 ret->height = values[i].y;
182 ret->wrapping = values[i].wrap;
183 ret->barrier_probability = values[i].bprob;
185 sprintf(str, "%dx%d%s", ret->width, ret->height,
193 void free_params(game_params *params)
198 game_params *dup_params(game_params *params)
200 game_params *ret = snew(game_params);
201 *ret = *params; /* structure copy */
205 game_params *decode_params(char const *string)
207 game_params *ret = default_params();
208 char const *p = string;
210 ret->wrapping = FALSE;
211 ret->barrier_probability = 0.0;
213 ret->width = atoi(p);
214 while (*p && isdigit(*p)) p++;
217 ret->height = atoi(p);
218 while (*p && isdigit(*p)) p++;
219 if ( (ret->wrapping = (*p == 'w')) != 0 )
222 ret->barrier_probability = atof(p+1);
224 ret->height = ret->width;
230 char *encode_params(game_params *params)
235 len = sprintf(ret, "%dx%d", params->width, params->height);
236 if (params->wrapping)
238 if (params->barrier_probability)
239 len += sprintf(ret+len, "b%g", params->barrier_probability);
240 assert(len < lenof(ret));
246 config_item *game_configure(game_params *params)
251 ret = snewn(5, config_item);
253 ret[0].name = "Width";
254 ret[0].type = C_STRING;
255 sprintf(buf, "%d", params->width);
256 ret[0].sval = dupstr(buf);
259 ret[1].name = "Height";
260 ret[1].type = C_STRING;
261 sprintf(buf, "%d", params->height);
262 ret[1].sval = dupstr(buf);
265 ret[2].name = "Walls wrap around";
266 ret[2].type = C_BOOLEAN;
268 ret[2].ival = params->wrapping;
270 ret[3].name = "Barrier probability";
271 ret[3].type = C_STRING;
272 sprintf(buf, "%g", params->barrier_probability);
273 ret[3].sval = dupstr(buf);
284 game_params *custom_params(config_item *cfg)
286 game_params *ret = snew(game_params);
288 ret->width = atoi(cfg[0].sval);
289 ret->height = atoi(cfg[1].sval);
290 ret->wrapping = cfg[2].ival;
291 ret->barrier_probability = (float)atof(cfg[3].sval);
296 char *validate_params(game_params *params)
298 if (params->width <= 1 && params->height <= 1)
299 return "Width and height must both be greater than one";
300 if (params->width <= 1)
301 return "Width must be greater than one";
302 if (params->height <= 1)
303 return "Height must be greater than one";
304 if (params->barrier_probability < 0)
305 return "Barrier probability may not be negative";
306 if (params->barrier_probability > 1)
307 return "Barrier probability may not be greater than 1";
311 /* ----------------------------------------------------------------------
312 * Randomly select a new game seed.
315 char *new_game_seed(game_params *params, random_state *rs)
318 * The full description of a Net game is far too large to
319 * encode directly in the seed, so by default we'll have to go
320 * for the simple approach of providing a random-number seed.
322 * (This does not restrict me from _later on_ inventing a seed
323 * string syntax which can never be generated by this code -
324 * for example, strings beginning with a letter - allowing me
325 * to type in a precise game, and have new_game detect it and
326 * understand it and do something completely different.)
329 sprintf(buf, "%lu", random_bits(rs, 32));
333 char *validate_seed(game_params *params, char *seed)
336 * Since any string at all will suffice to seed the RNG, there
337 * is no validation required.
342 /* ----------------------------------------------------------------------
343 * Construct an initial game state, given a seed and parameters.
346 game_state *new_game(game_params *params, char *seed)
350 tree234 *possibilities, *barriers;
351 int w, h, x, y, nbarriers;
353 assert(params->width > 0 && params->height > 0);
354 assert(params->width > 1 || params->height > 1);
357 * Create a blank game state.
359 state = snew(game_state);
360 w = state->width = params->width;
361 h = state->height = params->height;
362 state->cx = state->width / 2;
363 state->cy = state->height / 2;
364 state->wrapping = params->wrapping;
365 state->completed = 0;
366 state->move_count = 0;
367 state->last_move_row = -1;
368 state->last_move_col = -1;
369 state->last_move_dir = 0;
370 state->tiles = snewn(state->width * state->height, unsigned char);
371 memset(state->tiles, 0, state->width * state->height);
372 state->barriers = snewn(state->width * state->height, unsigned char);
373 memset(state->barriers, 0, state->width * state->height);
376 * Set up border barriers if this is a non-wrapping game.
378 if (!state->wrapping) {
379 for (x = 0; x < state->width; x++) {
380 barrier(state, x, 0) |= U;
381 barrier(state, x, state->height-1) |= D;
383 for (y = 0; y < state->height; y++) {
384 barrier(state, 0, y) |= L;
385 barrier(state, state->width-1, y) |= R;
390 * Seed the internal random number generator.
392 rs = random_init(seed, strlen(seed));
395 * Construct the unshuffled grid.
397 * To do this, we simply start at the centre point, repeatedly
398 * choose a random possibility out of the available ways to
399 * extend a used square into an unused one, and do it. After
400 * extending the third line out of a square, we remove the
401 * fourth from the possibilities list to avoid any full-cross
402 * squares (which would make the game too easy because they
403 * only have one orientation).
405 * The slightly worrying thing is the avoidance of full-cross
406 * squares. Can this cause our unsophisticated construction
407 * algorithm to paint itself into a corner, by getting into a
408 * situation where there are some unreached squares and the
409 * only way to reach any of them is to extend a T-piece into a
412 * Answer: no it can't, and here's a proof.
414 * Any contiguous group of such unreachable squares must be
415 * surrounded on _all_ sides by T-pieces pointing away from the
416 * group. (If not, then there is a square which can be extended
417 * into one of the `unreachable' ones, and so it wasn't
418 * unreachable after all.) In particular, this implies that
419 * each contiguous group of unreachable squares must be
420 * rectangular in shape (any deviation from that yields a
421 * non-T-piece next to an `unreachable' square).
423 * So we have a rectangle of unreachable squares, with T-pieces
424 * forming a solid border around the rectangle. The corners of
425 * that border must be connected (since every tile connects all
426 * the lines arriving in it), and therefore the border must
427 * form a closed loop around the rectangle.
429 * But this can't have happened in the first place, since we
430 * _know_ we've avoided creating closed loops! Hence, no such
431 * situation can ever arise, and the naive grid construction
432 * algorithm will guaranteeably result in a complete grid
433 * containing no unreached squares, no full crosses _and_ no
436 possibilities = newtree234(xyd_cmp);
438 if (state->cx+1 < state->width)
439 add234(possibilities, new_xyd(state->cx, state->cy, R));
440 if (state->cy-1 >= 0)
441 add234(possibilities, new_xyd(state->cx, state->cy, U));
442 if (state->cx-1 >= 0)
443 add234(possibilities, new_xyd(state->cx, state->cy, L));
444 if (state->cy+1 < state->height)
445 add234(possibilities, new_xyd(state->cx, state->cy, D));
447 while (count234(possibilities) > 0) {
450 int x1, y1, d1, x2, y2, d2, d;
453 * Extract a randomly chosen possibility from the list.
455 i = random_upto(rs, count234(possibilities));
456 xyd = delpos234(possibilities, i);
462 OFFSET(x2, y2, x1, y1, d1, state);
465 printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n",
466 x1, y1, "0RU3L567D9abcdef"[d1], x2, y2, "0RU3L567D9abcdef"[d2]);
470 * Make the connection. (We should be moving to an as yet
473 tile(state, x1, y1) |= d1;
474 assert(tile(state, x2, y2) == 0);
475 tile(state, x2, y2) |= d2;
478 * If we have created a T-piece, remove its last
481 if (COUNT(tile(state, x1, y1)) == 3) {
482 struct xyd xyd1, *xydp;
486 xyd1.direction = 0x0F ^ tile(state, x1, y1);
488 xydp = find234(possibilities, &xyd1, NULL);
492 printf("T-piece; removing (%d,%d,%c)\n",
493 xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
495 del234(possibilities, xydp);
501 * Remove all other possibilities that were pointing at the
502 * tile we've just moved into.
504 for (d = 1; d < 0x10; d <<= 1) {
506 struct xyd xyd1, *xydp;
508 OFFSET(x3, y3, x2, y2, d, state);
515 xydp = find234(possibilities, &xyd1, NULL);
519 printf("Loop avoidance; removing (%d,%d,%c)\n",
520 xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
522 del234(possibilities, xydp);
528 * Add new possibilities to the list for moving _out_ of
529 * the tile we have just moved into.
531 for (d = 1; d < 0x10; d <<= 1) {
535 continue; /* we've got this one already */
537 if (!state->wrapping) {
538 if (d == U && y2 == 0)
540 if (d == D && y2 == state->height-1)
542 if (d == L && x2 == 0)
544 if (d == R && x2 == state->width-1)
548 OFFSET(x3, y3, x2, y2, d, state);
550 if (tile(state, x3, y3))
551 continue; /* this would create a loop */
554 printf("New frontier; adding (%d,%d,%c)\n",
555 x2, y2, "0RU3L567D9abcdef"[d]);
557 add234(possibilities, new_xyd(x2, y2, d));
560 /* Having done that, we should have no possibilities remaining. */
561 assert(count234(possibilities) == 0);
562 freetree234(possibilities);
565 * Now compute a list of the possible barrier locations.
567 barriers = newtree234(xyd_cmp);
568 for (y = 0; y < state->height; y++) {
569 for (x = 0; x < state->width; x++) {
571 if (!(tile(state, x, y) & R) &&
572 (state->wrapping || x < state->width-1))
573 add234(barriers, new_xyd(x, y, R));
574 if (!(tile(state, x, y) & D) &&
575 (state->wrapping || y < state->height-1))
576 add234(barriers, new_xyd(x, y, D));
581 * Now shuffle the grid.
582 * FIXME - this simply does a set of random moves to shuffle the pieces.
583 * A better way would be to number all the pieces, generate a placement
584 * for all the numbers as for "sixteen", observing parity constraints if
585 * neccessary, and then place the pieces according to their numbering.
586 * BUT - I'm not sure if this will work, since we disallow movement of
587 * the middle row and column.
591 int cols = state->width - 1;
592 int rows = state->height - 1;
593 for (i = 0; i < cols * rows * 2; i++) {
594 /* Choose a direction: 0,1,2,3 = up, right, down, left. */
595 int dir = random_upto(rs, 4);
597 int col = random_upto(rs, cols);
598 if (col >= state->cx) col += 1;
599 slide_col(state, 1 - dir, col);
601 int row = random_upto(rs, rows);
602 if (row >= state->cy) row += 1;
603 slide_row(state, 2 - dir, row);
609 * And now choose barrier locations. (We carefully do this
610 * _after_ shuffling, so that changing the barrier rate in the
611 * params while keeping the game seed the same will give the
612 * same shuffled grid and _only_ change the barrier locations.
613 * Also the way we choose barrier locations, by repeatedly
614 * choosing one possibility from the list until we have enough,
615 * is designed to ensure that raising the barrier rate while
616 * keeping the seed the same will provide a superset of the
617 * previous barrier set - i.e. if you ask for 10 barriers, and
618 * then decide that's still too hard and ask for 20, you'll get
619 * the original 10 plus 10 more, rather than getting 20 new
620 * ones and the chance of remembering your first 10.)
622 nbarriers = (int)(params->barrier_probability * count234(barriers));
623 assert(nbarriers >= 0 && nbarriers <= count234(barriers));
625 while (nbarriers > 0) {
628 int x1, y1, d1, x2, y2, d2;
631 * Extract a randomly chosen barrier from the list.
633 i = random_upto(rs, count234(barriers));
634 xyd = delpos234(barriers, i);
643 OFFSET(x2, y2, x1, y1, d1, state);
646 barrier(state, x1, y1) |= d1;
647 barrier(state, x2, y2) |= d2;
653 * Clean up the rest of the barrier list.
658 while ( (xyd = delpos234(barriers, 0)) != NULL)
661 freetree234(barriers);
665 * Set up the barrier corner flags, for drawing barriers
666 * prettily when they meet.
668 for (y = 0; y < state->height; y++) {
669 for (x = 0; x < state->width; x++) {
672 for (dir = 1; dir < 0x10; dir <<= 1) {
674 int x1, y1, x2, y2, x3, y3;
677 if (!(barrier(state, x, y) & dir))
680 if (barrier(state, x, y) & dir2)
683 x1 = x + X(dir), y1 = y + Y(dir);
684 if (x1 >= 0 && x1 < state->width &&
685 y1 >= 0 && y1 < state->height &&
686 (barrier(state, x1, y1) & dir2))
689 x2 = x + X(dir2), y2 = y + Y(dir2);
690 if (x2 >= 0 && x2 < state->width &&
691 y2 >= 0 && y2 < state->height &&
692 (barrier(state, x2, y2) & dir))
696 barrier(state, x, y) |= (dir << 4);
697 if (x1 >= 0 && x1 < state->width &&
698 y1 >= 0 && y1 < state->height)
699 barrier(state, x1, y1) |= (A(dir) << 4);
700 if (x2 >= 0 && x2 < state->width &&
701 y2 >= 0 && y2 < state->height)
702 barrier(state, x2, y2) |= (C(dir) << 4);
703 x3 = x + X(dir) + X(dir2), y3 = y + Y(dir) + Y(dir2);
704 if (x3 >= 0 && x3 < state->width &&
705 y3 >= 0 && y3 < state->height)
706 barrier(state, x3, y3) |= (F(dir) << 4);
717 game_state *dup_game(game_state *state)
721 ret = snew(game_state);
722 ret->width = state->width;
723 ret->height = state->height;
726 ret->wrapping = state->wrapping;
727 ret->completed = state->completed;
728 ret->move_count = state->move_count;
729 ret->last_move_row = state->last_move_row;
730 ret->last_move_col = state->last_move_col;
731 ret->last_move_dir = state->last_move_dir;
732 ret->tiles = snewn(state->width * state->height, unsigned char);
733 memcpy(ret->tiles, state->tiles, state->width * state->height);
734 ret->barriers = snewn(state->width * state->height, unsigned char);
735 memcpy(ret->barriers, state->barriers, state->width * state->height);
740 void free_game(game_state *state)
743 sfree(state->barriers);
747 /* ----------------------------------------------------------------------
752 * Compute which squares are reachable from the centre square, as a
753 * quick visual aid to determining how close the game is to
754 * completion. This is also a simple way to tell if the game _is_
755 * completed - just call this function and see whether every square
758 * squares in the moving_row and moving_col are always inactive - this
759 * is so that "current" doesn't appear to jump across moving lines.
761 static unsigned char *compute_active(game_state *state,
762 int moving_row, int moving_col)
764 unsigned char *active;
768 active = snewn(state->width * state->height, unsigned char);
769 memset(active, 0, state->width * state->height);
772 * We only store (x,y) pairs in todo, but it's easier to reuse
773 * xyd_cmp and just store direction 0 every time.
775 todo = newtree234(xyd_cmp);
776 index(state, active, state->cx, state->cy) = ACTIVE;
777 add234(todo, new_xyd(state->cx, state->cy, 0));
779 while ( (xyd = delpos234(todo, 0)) != NULL) {
780 int x1, y1, d1, x2, y2, d2;
786 for (d1 = 1; d1 < 0x10; d1 <<= 1) {
787 OFFSET(x2, y2, x1, y1, d1, state);
791 * If the next tile in this direction is connected to
792 * us, and there isn't a barrier in the way, and it
793 * isn't already marked active, then mark it active and
794 * add it to the to-examine list.
796 if ((x2 != moving_col && y2 != moving_row) &&
797 (tile(state, x1, y1) & d1) &&
798 (tile(state, x2, y2) & d2) &&
799 !(barrier(state, x1, y1) & d1) &&
800 !index(state, active, x2, y2)) {
801 index(state, active, x2, y2) = ACTIVE;
802 add234(todo, new_xyd(x2, y2, 0));
806 /* Now we expect the todo list to have shrunk to zero size. */
807 assert(count234(todo) == 0);
818 game_ui *new_ui(game_state *state)
820 game_ui *ui = snew(game_ui);
821 ui->cur_x = state->width / 2;
822 ui->cur_y = state->height / 2;
823 ui->cur_visible = FALSE;
828 void free_ui(game_ui *ui)
833 /* ----------------------------------------------------------------------
837 void slide_row(game_state *state, int dir, int row)
839 int x = dir > 0 ? -1 : state->width;
841 int n = state->width - 1;
842 unsigned char endtile = state->tiles[T(state, tx, row)];
845 tx = (x + dir + state->width) % state->width;
846 state->tiles[T(state, x, row)] = state->tiles[T(state, tx, row)];
848 state->tiles[T(state, tx, row)] = endtile;
851 void slide_col(game_state *state, int dir, int col)
853 int y = dir > 0 ? -1 : state->height;
855 int n = state->height - 1;
856 unsigned char endtile = state->tiles[T(state, col, ty)];
859 ty = (y + dir + state->height) % state->height;
860 state->tiles[T(state, col, y)] = state->tiles[T(state, col, ty)];
862 state->tiles[T(state, col, ty)] = endtile;
865 game_state *make_move(game_state *state, game_ui *ui, int x, int y, int button)
871 if (button != LEFT_BUTTON && button != RIGHT_BUTTON)
874 cx = (x - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2;
875 cy = (y - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2;
877 if (cy >= 0 && cy < state->height && cy != state->cy)
879 if (cx == -1) dx = +1;
880 else if (cx == state->width) dx = -1;
885 else if (cx >= 0 && cx < state->width && cx != state->cx)
887 if (cy == -1) dy = +1;
888 else if (cy == state->height) dy = -1;
896 /* reverse direction if right hand button is pressed */
897 if (button == RIGHT_BUTTON)
903 ret = dup_game(state);
905 if (dx == 0) slide_col(ret, dy, cx);
906 else slide_row(ret, dx, cy);
909 ret->last_move_row = dx ? cy : -1;
910 ret->last_move_col = dx ? -1 : cx;
911 ret->last_move_dir = dx + dy;
914 * See if the game has been completed.
916 if (!ret->completed) {
917 unsigned char *active = compute_active(ret, -1, -1);
921 for (x1 = 0; x1 < ret->width; x1++)
922 for (y1 = 0; y1 < ret->height; y1++)
923 if (!index(ret, active, x1, y1)) {
925 goto break_label; /* break out of two loops at once */
932 ret->completed = ret->move_count;
938 /* ----------------------------------------------------------------------
939 * Routines for drawing the game position on the screen.
942 struct game_drawstate {
945 unsigned char *visible;
948 game_drawstate *game_new_drawstate(game_state *state)
950 game_drawstate *ds = snew(game_drawstate);
953 ds->width = state->width;
954 ds->height = state->height;
955 ds->visible = snewn(state->width * state->height, unsigned char);
956 memset(ds->visible, 0xFF, state->width * state->height);
961 void game_free_drawstate(game_drawstate *ds)
967 void game_size(game_params *params, int *x, int *y)
969 *x = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->width + TILE_BORDER;
970 *y = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->height + TILE_BORDER;
973 float *game_colours(frontend *fe, game_state *state, int *ncolours)
977 ret = snewn(NCOLOURS * 3, float);
978 *ncolours = NCOLOURS;
981 * Basic background colour is whatever the front end thinks is
982 * a sensible default.
984 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
989 ret[COL_WIRE * 3 + 0] = 0.0F;
990 ret[COL_WIRE * 3 + 1] = 0.0F;
991 ret[COL_WIRE * 3 + 2] = 0.0F;
994 * Powered wires and powered endpoints are cyan.
996 ret[COL_POWERED * 3 + 0] = 0.0F;
997 ret[COL_POWERED * 3 + 1] = 1.0F;
998 ret[COL_POWERED * 3 + 2] = 1.0F;
1003 ret[COL_BARRIER * 3 + 0] = 1.0F;
1004 ret[COL_BARRIER * 3 + 1] = 0.0F;
1005 ret[COL_BARRIER * 3 + 2] = 0.0F;
1008 * Unpowered endpoints are blue.
1010 ret[COL_ENDPOINT * 3 + 0] = 0.0F;
1011 ret[COL_ENDPOINT * 3 + 1] = 0.0F;
1012 ret[COL_ENDPOINT * 3 + 2] = 1.0F;
1015 * Tile borders are a darker grey than the background.
1017 ret[COL_BORDER * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
1018 ret[COL_BORDER * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
1019 ret[COL_BORDER * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2];
1022 * Flashing tiles are a grey in between those two.
1024 ret[COL_FLASHING * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0];
1025 ret[COL_FLASHING * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1];
1026 ret[COL_FLASHING * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2];
1028 ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.8F;
1029 ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.8F;
1030 ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.8F;
1031 ret[COL_TEXT * 3 + 0] = 0.0;
1032 ret[COL_TEXT * 3 + 1] = 0.0;
1033 ret[COL_TEXT * 3 + 2] = 0.0;
1038 static void draw_thick_line(frontend *fe, int x1, int y1, int x2, int y2,
1041 draw_line(fe, x1-1, y1, x2-1, y2, COL_WIRE);
1042 draw_line(fe, x1+1, y1, x2+1, y2, COL_WIRE);
1043 draw_line(fe, x1, y1-1, x2, y2-1, COL_WIRE);
1044 draw_line(fe, x1, y1+1, x2, y2+1, COL_WIRE);
1045 draw_line(fe, x1, y1, x2, y2, colour);
1048 static void draw_rect_coords(frontend *fe, int x1, int y1, int x2, int y2,
1051 int mx = (x1 < x2 ? x1 : x2);
1052 int my = (y1 < y2 ? y1 : y2);
1053 int dx = (x2 + x1 - 2*mx + 1);
1054 int dy = (y2 + y1 - 2*my + 1);
1056 draw_rect(fe, mx, my, dx, dy, colour);
1059 static void draw_barrier_corner(frontend *fe, int x, int y, int dir, int phase)
1061 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
1062 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
1063 int x1, y1, dx, dy, dir2;
1068 dx = X(dir) + X(dir2);
1069 dy = Y(dir) + Y(dir2);
1070 x1 = (dx > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
1071 y1 = (dy > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
1074 draw_rect_coords(fe, bx+x1, by+y1,
1075 bx+x1-TILE_BORDER*dx, by+y1-(TILE_BORDER-1)*dy,
1077 draw_rect_coords(fe, bx+x1, by+y1,
1078 bx+x1-(TILE_BORDER-1)*dx, by+y1-TILE_BORDER*dy,
1081 draw_rect_coords(fe, bx+x1, by+y1,
1082 bx+x1-(TILE_BORDER-1)*dx, by+y1-(TILE_BORDER-1)*dy,
1087 static void draw_barrier(frontend *fe, int x, int y, int dir, int phase)
1089 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
1090 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
1093 x1 = (X(dir) > 0 ? TILE_SIZE : X(dir) == 0 ? TILE_BORDER : 0);
1094 y1 = (Y(dir) > 0 ? TILE_SIZE : Y(dir) == 0 ? TILE_BORDER : 0);
1095 w = (X(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER);
1096 h = (Y(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER);
1099 draw_rect(fe, bx+x1-X(dir), by+y1-Y(dir), w, h, COL_WIRE);
1101 draw_rect(fe, bx+x1, by+y1, w, h, COL_BARRIER);
1105 static void draw_tile(frontend *fe, game_state *state, int x, int y, int tile,
1106 float xshift, float yshift)
1108 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x + (xshift * TILE_SIZE);
1109 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y + (yshift * TILE_SIZE);
1110 float cx, cy, ex, ey;
1114 * When we draw a single tile, we must draw everything up to
1115 * and including the borders around the tile. This means that
1116 * if the neighbouring tiles have connections to those borders,
1117 * we must draw those connections on the borders themselves.
1119 * This would be terribly fiddly if we ever had to draw a tile
1120 * while its neighbour was in mid-rotate, because we'd have to
1121 * arrange to _know_ that the neighbour was being rotated and
1122 * hence had an anomalous effect on the redraw of this tile.
1123 * Fortunately, the drawing algorithm avoids ever calling us in
1124 * this circumstance: we're either drawing lots of straight
1125 * tiles at game start or after a move is complete, or we're
1126 * repeatedly drawing only the rotating tile. So no problem.
1130 * So. First blank the tile out completely: draw a big
1131 * rectangle in border colour, and a smaller rectangle in
1132 * background colour to fill it in.
1134 draw_rect(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER,
1136 draw_rect(fe, bx+TILE_BORDER, by+TILE_BORDER,
1137 TILE_SIZE-TILE_BORDER, TILE_SIZE-TILE_BORDER,
1138 tile & FLASHING ? COL_FLASHING : COL_BACKGROUND);
1143 cx = cy = TILE_BORDER + (TILE_SIZE-TILE_BORDER) / 2.0F - 0.5F;
1144 col = (tile & ACTIVE ? COL_POWERED : COL_WIRE);
1145 for (dir = 1; dir < 0x10; dir <<= 1) {
1147 ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir);
1148 ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir);
1149 draw_thick_line(fe, bx+(int)cx, by+(int)cy,
1150 bx+(int)(cx+ex), by+(int)(cy+ey),
1154 for (dir = 1; dir < 0x10; dir <<= 1) {
1156 ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir);
1157 ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir);
1158 draw_line(fe, bx+(int)cx, by+(int)cy,
1159 bx+(int)(cx+ex), by+(int)(cy+ey), col);
1164 * Draw the box in the middle. We do this in blue if the tile
1165 * is an unpowered endpoint, in cyan if the tile is a powered
1166 * endpoint, in black if the tile is the centrepiece, and
1167 * otherwise not at all.
1170 if (x == state->cx && y == state->cy)
1172 else if (COUNT(tile) == 1) {
1173 col = (tile & ACTIVE ? COL_POWERED : COL_ENDPOINT);
1178 points[0] = +1; points[1] = +1;
1179 points[2] = +1; points[3] = -1;
1180 points[4] = -1; points[5] = -1;
1181 points[6] = -1; points[7] = +1;
1183 for (i = 0; i < 8; i += 2) {
1184 ex = (TILE_SIZE * 0.24F) * points[i];
1185 ey = (TILE_SIZE * 0.24F) * points[i+1];
1186 points[i] = bx+(int)(cx+ex);
1187 points[i+1] = by+(int)(cy+ey);
1190 draw_polygon(fe, points, 4, TRUE, col);
1191 draw_polygon(fe, points, 4, FALSE, COL_WIRE);
1195 * Draw the points on the border if other tiles are connected
1198 for (dir = 1; dir < 0x10; dir <<= 1) {
1199 int dx, dy, px, py, lx, ly, vx, vy, ox, oy;
1207 if (ox < 0 || ox >= state->width || oy < 0 || oy >= state->height)
1210 if (!(tile(state, ox, oy) & F(dir)))
1213 px = bx + (int)(dx>0 ? TILE_SIZE + TILE_BORDER - 1 : dx<0 ? 0 : cx);
1214 py = by + (int)(dy>0 ? TILE_SIZE + TILE_BORDER - 1 : dy<0 ? 0 : cy);
1215 lx = dx * (TILE_BORDER-1);
1216 ly = dy * (TILE_BORDER-1);
1220 if (xshift == 0.0 && yshift == 0.0 && (tile & dir)) {
1222 * If we are fully connected to the other tile, we must
1223 * draw right across the tile border. (We can use our
1224 * own ACTIVE state to determine what colour to do this
1225 * in: if we are fully connected to the other tile then
1226 * the two ACTIVE states will be the same.)
1228 draw_rect_coords(fe, px-vx, py-vy, px+lx+vx, py+ly+vy, COL_WIRE);
1229 draw_rect_coords(fe, px, py, px+lx, py+ly,
1230 (tile & ACTIVE) ? COL_POWERED : COL_WIRE);
1233 * The other tile extends into our border, but isn't
1234 * actually connected to us. Just draw a single black
1237 draw_rect_coords(fe, px, py, px, py, COL_WIRE);
1241 draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER);
1244 static void draw_tile_barriers(frontend *fe, game_state *state, int x, int y)
1248 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
1249 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
1251 * Draw barrier corners, and then barriers.
1253 for (phase = 0; phase < 2; phase++) {
1254 for (dir = 1; dir < 0x10; dir <<= 1)
1255 if (barrier(state, x, y) & (dir << 4))
1256 draw_barrier_corner(fe, x, y, dir << 4, phase);
1257 for (dir = 1; dir < 0x10; dir <<= 1)
1258 if (barrier(state, x, y) & dir)
1259 draw_barrier(fe, x, y, dir, phase);
1262 draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER);
1265 static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy)
1268 int ydy = -xdx, ydx = xdy;
1270 x = x * TILE_SIZE + BORDER + WINDOW_OFFSET;
1271 y = y * TILE_SIZE + BORDER + WINDOW_OFFSET;
1273 #define POINT(n, xx, yy) ( \
1274 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
1275 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
1277 POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */
1278 POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */
1279 POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */
1280 POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */
1281 POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */
1282 POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */
1283 POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */
1285 draw_polygon(fe, coords, 7, TRUE, COL_LOWLIGHT);
1286 draw_polygon(fe, coords, 7, FALSE, COL_TEXT);
1289 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1290 game_state *state, int dir, game_ui *ui, float t, float ft)
1292 int x, y, tx, ty, frame;
1293 unsigned char *active;
1298 * Clear the screen and draw the exterior barrier lines if this
1299 * is our first call.
1307 BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * state->width + TILE_BORDER,
1308 BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * state->height + TILE_BORDER,
1310 draw_update(fe, 0, 0,
1311 BORDER * 2 + WINDOW_OFFSET*2 + TILE_SIZE*state->width + TILE_BORDER,
1312 BORDER * 2 + WINDOW_OFFSET*2 + TILE_SIZE*state->height + TILE_BORDER);
1314 for (phase = 0; phase < 2; phase++) {
1316 for (x = 0; x < ds->width; x++) {
1317 if (barrier(state, x, 0) & UL)
1318 draw_barrier_corner(fe, x, -1, LD, phase);
1319 if (barrier(state, x, 0) & RU)
1320 draw_barrier_corner(fe, x, -1, DR, phase);
1321 if (barrier(state, x, 0) & U)
1322 draw_barrier(fe, x, -1, D, phase);
1323 if (barrier(state, x, ds->height-1) & DR)
1324 draw_barrier_corner(fe, x, ds->height, RU, phase);
1325 if (barrier(state, x, ds->height-1) & LD)
1326 draw_barrier_corner(fe, x, ds->height, UL, phase);
1327 if (barrier(state, x, ds->height-1) & D)
1328 draw_barrier(fe, x, ds->height, U, phase);
1331 for (y = 0; y < ds->height; y++) {
1332 if (barrier(state, 0, y) & UL)
1333 draw_barrier_corner(fe, -1, y, RU, phase);
1334 if (barrier(state, 0, y) & LD)
1335 draw_barrier_corner(fe, -1, y, DR, phase);
1336 if (barrier(state, 0, y) & L)
1337 draw_barrier(fe, -1, y, R, phase);
1338 if (barrier(state, ds->width-1, y) & RU)
1339 draw_barrier_corner(fe, ds->width, y, UL, phase);
1340 if (barrier(state, ds->width-1, y) & DR)
1341 draw_barrier_corner(fe, ds->width, y, LD, phase);
1342 if (barrier(state, ds->width-1, y) & R)
1343 draw_barrier(fe, ds->width, y, L, phase);
1348 * Arrows for making moves.
1350 for (x = 0; x < ds->width; x++) {
1351 if (x == state->cx) continue;
1352 draw_arrow(fe, x, 0, +1, 0);
1353 draw_arrow(fe, x+1, ds->height, -1, 0);
1355 for (y = 0; y < ds->height; y++) {
1356 if (y == state->cy) continue;
1357 draw_arrow(fe, ds->width, y, 0, +1);
1358 draw_arrow(fe, 0, y+1, 0, -1);
1362 /* Check if this is an undo. If so, we will need to run any animation
1365 if (oldstate && oldstate->move_count > state->move_count) {
1366 game_state * tmpstate = state;
1368 oldstate = tmpstate;
1373 if (oldstate && (t < ANIM_TIME)) {
1375 * We're animating a slide, of row/column number
1376 * state->last_move_pos, in direction
1377 * state->last_move_dir
1379 xshift = state->last_move_row == -1 ? 0.0 :
1380 (1 - t / ANIM_TIME) * state->last_move_dir;
1381 yshift = state->last_move_col == -1 ? 0.0 :
1382 (1 - t / ANIM_TIME) * state->last_move_dir;
1388 * We're animating a completion flash. Find which frame
1391 frame = (int)(ft / FLASH_FRAME);
1395 * Draw any tile which differs from the way it was last drawn.
1397 if (xshift != 0.0 || yshift != 0.0) {
1398 active = compute_active(state,
1399 state->last_move_row, state->last_move_col);
1401 active = compute_active(state, -1, -1);
1405 BORDER + WINDOW_OFFSET, BORDER + WINDOW_OFFSET,
1406 TILE_SIZE * state->width + TILE_BORDER,
1407 TILE_SIZE * state->height + TILE_BORDER);
1409 for (x = 0; x < ds->width; x++)
1410 for (y = 0; y < ds->height; y++) {
1411 unsigned char c = tile(state, x, y) | index(state, active, x, y);
1414 * In a completion flash, we adjust the FLASHING bit
1415 * depending on our distance from the centre point and
1419 int xdist, ydist, dist;
1420 xdist = (x < state->cx ? state->cx - x : x - state->cx);
1421 ydist = (y < state->cy ? state->cy - y : y - state->cy);
1422 dist = (xdist > ydist ? xdist : ydist);
1424 if (frame >= dist && frame < dist+4) {
1425 int flash = (frame - dist) & 1;
1426 flash = flash ? FLASHING : 0;
1427 c = (c &~ FLASHING) | flash;
1431 if (index(state, ds->visible, x, y) != c ||
1432 index(state, ds->visible, x, y) == 0xFF ||
1433 (x == state->last_move_col || y == state->last_move_row))
1435 float xs = (y == state->last_move_row ? xshift : 0.0);
1436 float ys = (x == state->last_move_col ? yshift : 0.0);
1438 draw_tile(fe, state, x, y, c, xs, ys);
1439 if (xs < 0 && x == 0)
1440 draw_tile(fe, state, state->width, y, c, xs, ys);
1441 else if (xs > 0 && x == state->width - 1)
1442 draw_tile(fe, state, -1, y, c, xs, ys);
1443 else if (ys < 0 && y == 0)
1444 draw_tile(fe, state, x, state->height, c, xs, ys);
1445 else if (ys > 0 && y == state->height - 1)
1446 draw_tile(fe, state, x, -1, c, xs, ys);
1448 if (x == state->last_move_col || y == state->last_move_row)
1449 index(state, ds->visible, x, y) = 0xFF;
1451 index(state, ds->visible, x, y) = c;
1455 for (x = 0; x < ds->width; x++)
1456 for (y = 0; y < ds->height; y++)
1457 draw_tile_barriers(fe, state, x, y);
1462 * Update the status bar.
1465 char statusbuf[256];
1468 n = state->width * state->height;
1469 for (i = a = 0; i < n; i++)
1473 sprintf(statusbuf, "%sMoves: %d Active: %d/%d",
1474 (state->completed ? "COMPLETED! " : ""),
1475 (state->completed ? state->completed : state->move_count),
1478 status_bar(fe, statusbuf);
1484 float game_anim_length(game_state *oldstate, game_state *newstate, int dir)
1489 float game_flash_length(game_state *oldstate, game_state *newstate, int dir)
1492 * If the game has just been completed, we display a completion
1495 if (!oldstate->completed && newstate->completed) {
1498 if (size < newstate->cx+1)
1499 size = newstate->cx+1;
1500 if (size < newstate->cy+1)
1501 size = newstate->cy+1;
1502 if (size < newstate->width - newstate->cx)
1503 size = newstate->width - newstate->cx;
1504 if (size < newstate->height - newstate->cy)
1505 size = newstate->height - newstate->cy;
1506 return FLASH_FRAME * (size+4);
1512 int game_wants_statusbar(void)