Add origin-shifting (Shift+cursors) and source-shifting (Ctrl+cursors) to Net.
[sgt/puzzles] / netslide.c
1 /*
2 * netslide.c: cross between Net and Sixteen, courtesy of Richard
3 * Boulton.
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <ctype.h>
11 #include <math.h>
12
13 #include "puzzles.h"
14 #include "tree234.h"
15
16 #define PI 3.141592653589793238462643383279502884197169399
17
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; \
23 } while (0)
24
25 /* Direction and other bitfields */
26 #define R 0x01
27 #define U 0x02
28 #define L 0x04
29 #define D 0x08
30 #define FLASHING 0x10
31 #define ACTIVE 0x20
32 /* Corner flags go in the barriers array */
33 #define RU 0x10
34 #define UL 0x20
35 #define LD 0x40
36 #define DR 0x80
37
38 /* Get tile at given coordinate */
39 #define T(state, x, y) ( (y) * (state)->width + (x) )
40
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) )
48
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 )
52
53 /* Bit count */
54 #define COUNT(x) ( (((x) & 0x08) >> 3) + (((x) & 0x04) >> 2) + \
55 (((x) & 0x02) >> 1) + ((x) & 0x01) )
56
57 #define TILE_SIZE 48
58 #define BORDER TILE_SIZE
59 #define TILE_BORDER 1
60 #define WINDOW_OFFSET 0
61
62 #define ANIM_TIME 0.13F
63 #define FLASH_FRAME 0.07F
64
65 enum {
66 COL_BACKGROUND,
67 COL_FLASHING,
68 COL_BORDER,
69 COL_WIRE,
70 COL_ENDPOINT,
71 COL_POWERED,
72 COL_BARRIER,
73 COL_LOWLIGHT,
74 COL_TEXT,
75 NCOLOURS
76 };
77
78 struct game_params {
79 int width;
80 int height;
81 int wrapping;
82 float barrier_probability;
83 };
84
85 struct game_aux_info {
86 int width, height;
87 unsigned char *tiles;
88 };
89
90 struct game_state {
91 int width, height, cx, cy, wrapping, completed;
92 int used_solve, just_used_solve;
93 int move_count;
94
95 /* position (row or col number, starting at 0) of last move. */
96 int last_move_row, last_move_col;
97
98 /* direction of last move: +1 or -1 */
99 int last_move_dir;
100
101 unsigned char *tiles;
102 unsigned char *barriers;
103 };
104
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)
108
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)
112
113 struct xyd {
114 int x, y, direction;
115 };
116
117 static int xyd_cmp(void *av, void *bv) {
118 struct xyd *a = (struct xyd *)av;
119 struct xyd *b = (struct xyd *)bv;
120 if (a->x < b->x)
121 return -1;
122 if (a->x > b->x)
123 return +1;
124 if (a->y < b->y)
125 return -1;
126 if (a->y > b->y)
127 return +1;
128 if (a->direction < b->direction)
129 return -1;
130 if (a->direction > b->direction)
131 return +1;
132 return 0;
133 };
134
135 static struct xyd *new_xyd(int x, int y, int direction)
136 {
137 struct xyd *xyd = snew(struct xyd);
138 xyd->x = x;
139 xyd->y = y;
140 xyd->direction = direction;
141 return xyd;
142 }
143
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);
148
149 /* ----------------------------------------------------------------------
150 * Manage game parameters.
151 */
152 static game_params *default_params(void)
153 {
154 game_params *ret = snew(game_params);
155
156 ret->width = 3;
157 ret->height = 3;
158 ret->wrapping = FALSE;
159 ret->barrier_probability = 1.0;
160
161 return ret;
162 }
163
164 static int game_fetch_preset(int i, char **name, game_params **params)
165 {
166 game_params *ret;
167 char str[80];
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"},
178 };
179
180 if (i < 0 || i >= lenof(values))
181 return FALSE;
182
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;
188
189 sprintf(str, "%dx%d%s", ret->width, ret->height,
190 values[i].desc);
191
192 *name = dupstr(str);
193 *params = ret;
194 return TRUE;
195 }
196
197 static void free_params(game_params *params)
198 {
199 sfree(params);
200 }
201
202 static game_params *dup_params(game_params *params)
203 {
204 game_params *ret = snew(game_params);
205 *ret = *params; /* structure copy */
206 return ret;
207 }
208
209 static void decode_params(game_params *ret, char const *string)
210 {
211 char const *p = string;
212
213 ret->wrapping = FALSE;
214 ret->barrier_probability = 0.0;
215
216 ret->width = atoi(p);
217 while (*p && isdigit(*p)) p++;
218 if (*p == 'x') {
219 p++;
220 ret->height = atoi(p);
221 while (*p && isdigit(*p)) p++;
222 if ( (ret->wrapping = (*p == 'w')) != 0 )
223 p++;
224 if (*p == 'b')
225 ret->barrier_probability = atof(p+1);
226 } else {
227 ret->height = ret->width;
228 }
229 }
230
231 static char *encode_params(game_params *params, int full)
232 {
233 char ret[400];
234 int len;
235
236 len = sprintf(ret, "%dx%d", params->width, params->height);
237 if (params->wrapping)
238 ret[len++] = 'w';
239 if (full && params->barrier_probability)
240 len += sprintf(ret+len, "b%g", params->barrier_probability);
241 assert(len < lenof(ret));
242 ret[len] = '\0';
243
244 return dupstr(ret);
245 }
246
247 static config_item *game_configure(game_params *params)
248 {
249 config_item *ret;
250 char buf[80];
251
252 ret = snewn(5, config_item);
253
254 ret[0].name = "Width";
255 ret[0].type = C_STRING;
256 sprintf(buf, "%d", params->width);
257 ret[0].sval = dupstr(buf);
258 ret[0].ival = 0;
259
260 ret[1].name = "Height";
261 ret[1].type = C_STRING;
262 sprintf(buf, "%d", params->height);
263 ret[1].sval = dupstr(buf);
264 ret[1].ival = 0;
265
266 ret[2].name = "Walls wrap around";
267 ret[2].type = C_BOOLEAN;
268 ret[2].sval = NULL;
269 ret[2].ival = params->wrapping;
270
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);
275 ret[3].ival = 0;
276
277 ret[4].name = NULL;
278 ret[4].type = C_END;
279 ret[4].sval = NULL;
280 ret[4].ival = 0;
281
282 return ret;
283 }
284
285 static game_params *custom_params(config_item *cfg)
286 {
287 game_params *ret = snew(game_params);
288
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);
293
294 return ret;
295 }
296
297 static char *validate_params(game_params *params)
298 {
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";
309 return NULL;
310 }
311
312 /* ----------------------------------------------------------------------
313 * Randomly select a new game description.
314 */
315
316 static char *new_game_desc(game_params *params, random_state *rs,
317 game_aux_info **aux)
318 {
319 tree234 *possibilities, *barriertree;
320 int w, h, x, y, cx, cy, nbarriers;
321 unsigned char *tiles, *barriers;
322 char *desc, *p;
323
324 w = params->width;
325 h = params->height;
326
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);
331
332 cx = w / 2;
333 cy = h / 2;
334
335 /*
336 * Construct the unshuffled grid.
337 *
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).
345 *
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
351 * full cross?
352 *
353 * Answer: no it can't, and here's a proof.
354 *
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).
363 *
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.
369 *
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
375 * closed loops. []
376 */
377 possibilities = newtree234(xyd_cmp);
378
379 if (cx+1 < w)
380 add234(possibilities, new_xyd(cx, cy, R));
381 if (cy-1 >= 0)
382 add234(possibilities, new_xyd(cx, cy, U));
383 if (cx-1 >= 0)
384 add234(possibilities, new_xyd(cx, cy, L));
385 if (cy+1 < h)
386 add234(possibilities, new_xyd(cx, cy, D));
387
388 while (count234(possibilities) > 0) {
389 int i;
390 struct xyd *xyd;
391 int x1, y1, d1, x2, y2, d2, d;
392
393 /*
394 * Extract a randomly chosen possibility from the list.
395 */
396 i = random_upto(rs, count234(possibilities));
397 xyd = delpos234(possibilities, i);
398 x1 = xyd->x;
399 y1 = xyd->y;
400 d1 = xyd->direction;
401 sfree(xyd);
402
403 OFFSET(x2, y2, x1, y1, d1, params);
404 d2 = F(d1);
405 #ifdef DEBUG
406 printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n",
407 x1, y1, "0RU3L567D9abcdef"[d1], x2, y2, "0RU3L567D9abcdef"[d2]);
408 #endif
409
410 /*
411 * Make the connection. (We should be moving to an as yet
412 * unused tile.)
413 */
414 index(params, tiles, x1, y1) |= d1;
415 assert(index(params, tiles, x2, y2) == 0);
416 index(params, tiles, x2, y2) |= d2;
417
418 /*
419 * If we have created a T-piece, remove its last
420 * possibility.
421 */
422 if (COUNT(index(params, tiles, x1, y1)) == 3) {
423 struct xyd xyd1, *xydp;
424
425 xyd1.x = x1;
426 xyd1.y = y1;
427 xyd1.direction = 0x0F ^ index(params, tiles, x1, y1);
428
429 xydp = find234(possibilities, &xyd1, NULL);
430
431 if (xydp) {
432 #ifdef DEBUG
433 printf("T-piece; removing (%d,%d,%c)\n",
434 xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
435 #endif
436 del234(possibilities, xydp);
437 sfree(xydp);
438 }
439 }
440
441 /*
442 * Remove all other possibilities that were pointing at the
443 * tile we've just moved into.
444 */
445 for (d = 1; d < 0x10; d <<= 1) {
446 int x3, y3, d3;
447 struct xyd xyd1, *xydp;
448
449 OFFSET(x3, y3, x2, y2, d, params);
450 d3 = F(d);
451
452 xyd1.x = x3;
453 xyd1.y = y3;
454 xyd1.direction = d3;
455
456 xydp = find234(possibilities, &xyd1, NULL);
457
458 if (xydp) {
459 #ifdef DEBUG
460 printf("Loop avoidance; removing (%d,%d,%c)\n",
461 xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
462 #endif
463 del234(possibilities, xydp);
464 sfree(xydp);
465 }
466 }
467
468 /*
469 * Add new possibilities to the list for moving _out_ of
470 * the tile we have just moved into.
471 */
472 for (d = 1; d < 0x10; d <<= 1) {
473 int x3, y3;
474
475 if (d == d2)
476 continue; /* we've got this one already */
477
478 if (!params->wrapping) {
479 if (d == U && y2 == 0)
480 continue;
481 if (d == D && y2 == h-1)
482 continue;
483 if (d == L && x2 == 0)
484 continue;
485 if (d == R && x2 == w-1)
486 continue;
487 }
488
489 OFFSET(x3, y3, x2, y2, d, params);
490
491 if (index(params, tiles, x3, y3))
492 continue; /* this would create a loop */
493
494 #ifdef DEBUG
495 printf("New frontier; adding (%d,%d,%c)\n",
496 x2, y2, "0RU3L567D9abcdef"[d]);
497 #endif
498 add234(possibilities, new_xyd(x2, y2, d));
499 }
500 }
501 /* Having done that, we should have no possibilities remaining. */
502 assert(count234(possibilities) == 0);
503 freetree234(possibilities);
504
505 /*
506 * Now compute a list of the possible barrier locations.
507 */
508 barriertree = newtree234(xyd_cmp);
509 for (y = 0; y < h; y++) {
510 for (x = 0; x < w; x++) {
511
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));
518 }
519 }
520
521 /*
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.
526 */
527 {
528 game_aux_info *solution;
529
530 solution = snew(game_aux_info);
531 solution->width = w;
532 solution->height = h;
533 solution->tiles = snewn(w * h, unsigned char);
534 memcpy(solution->tiles, tiles, w * h);
535
536 *aux = solution;
537 }
538
539 /*
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.
547 */
548 {
549 int i;
550 int cols = w - 1;
551 int rows = h - 1;
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);
555 if (dir % 2 == 0) {
556 int col = random_upto(rs, cols);
557 if (col >= cx) col += 1;
558 slide_col_int(w, h, tiles, 1 - dir, col);
559 } else {
560 int row = random_upto(rs, rows);
561 if (row >= cy) row += 1;
562 slide_row_int(w, h, tiles, 2 - dir, row);
563 }
564 }
565 }
566
567 /*
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.)
580 */
581 nbarriers = (int)(params->barrier_probability * count234(barriertree));
582 assert(nbarriers >= 0 && nbarriers <= count234(barriertree));
583
584 while (nbarriers > 0) {
585 int i;
586 struct xyd *xyd;
587 int x1, y1, d1, x2, y2, d2;
588
589 /*
590 * Extract a randomly chosen barrier from the list.
591 */
592 i = random_upto(rs, count234(barriertree));
593 xyd = delpos234(barriertree, i);
594
595 assert(xyd != NULL);
596
597 x1 = xyd->x;
598 y1 = xyd->y;
599 d1 = xyd->direction;
600 sfree(xyd);
601
602 OFFSET(x2, y2, x1, y1, d1, params);
603 d2 = F(d1);
604
605 index(params, barriers, x1, y1) |= d1;
606 index(params, barriers, x2, y2) |= d2;
607
608 nbarriers--;
609 }
610
611 /*
612 * Clean up the rest of the barrier list.
613 */
614 {
615 struct xyd *xyd;
616
617 while ( (xyd = delpos234(barriertree, 0)) != NULL)
618 sfree(xyd);
619
620 freetree234(barriertree);
621 }
622
623 /*
624 * Finally, encode the grid into a string game description.
625 *
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
632 * it.
633 */
634 desc = snewn(w * h * 3 + 1, char);
635 p = desc;
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))
641 *p++ = 'v';
642 if ((params->wrapping || y < h-1) &&
643 (index(params, barriers, x, y) & D))
644 *p++ = 'h';
645 }
646 }
647 assert(p - desc <= w*h*3);
648 *p = '\0';
649
650 sfree(tiles);
651 sfree(barriers);
652
653 return desc;
654 }
655
656 static void game_free_aux_info(game_aux_info *aux)
657 {
658 sfree(aux->tiles);
659 sfree(aux);
660 }
661
662 static char *validate_desc(game_params *params, char *desc)
663 {
664 int w = params->width, h = params->height;
665 int i;
666
667 for (i = 0; i < w*h; i++) {
668 if (*desc >= '0' && *desc <= '9')
669 /* OK */;
670 else if (*desc >= 'a' && *desc <= 'f')
671 /* OK */;
672 else if (*desc >= 'A' && *desc <= 'F')
673 /* OK */;
674 else if (!*desc)
675 return "Game description shorter than expected";
676 else
677 return "Game description contained unexpected character";
678 desc++;
679 while (*desc == 'h' || *desc == 'v')
680 desc++;
681 }
682 if (*desc)
683 return "Game description longer than expected";
684
685 return NULL;
686 }
687
688 /* ----------------------------------------------------------------------
689 * Construct an initial game state, given a description and parameters.
690 */
691
692 static game_state *new_game(game_params *params, char *desc)
693 {
694 game_state *state;
695 int w, h, x, y;
696
697 assert(params->width > 0 && params->height > 0);
698 assert(params->width > 1 || params->height > 1);
699
700 /*
701 * Create a blank game state.
702 */
703 state = snew(game_state);
704 w = state->width = params->width;
705 h = state->height = params->height;
706 state->cx = state->width / 2;
707 state->cy = state->height / 2;
708 state->wrapping = params->wrapping;
709 state->completed = 0;
710 state->used_solve = state->just_used_solve = FALSE;
711 state->move_count = 0;
712 state->last_move_row = -1;
713 state->last_move_col = -1;
714 state->last_move_dir = 0;
715 state->tiles = snewn(state->width * state->height, unsigned char);
716 memset(state->tiles, 0, state->width * state->height);
717 state->barriers = snewn(state->width * state->height, unsigned char);
718 memset(state->barriers, 0, state->width * state->height);
719
720
721 /*
722 * Parse the game description into the grid.
723 */
724 for (y = 0; y < h; y++) {
725 for (x = 0; x < w; x++) {
726 if (*desc >= '0' && *desc <= '9')
727 tile(state, x, y) = *desc - '0';
728 else if (*desc >= 'a' && *desc <= 'f')
729 tile(state, x, y) = *desc - 'a' + 10;
730 else if (*desc >= 'A' && *desc <= 'F')
731 tile(state, x, y) = *desc - 'A' + 10;
732 if (*desc)
733 desc++;
734 while (*desc == 'h' || *desc == 'v') {
735 int x2, y2, d1, d2;
736 if (*desc == 'v')
737 d1 = R;
738 else
739 d1 = D;
740
741 OFFSET(x2, y2, x, y, d1, state);
742 d2 = F(d1);
743
744 barrier(state, x, y) |= d1;
745 barrier(state, x2, y2) |= d2;
746
747 desc++;
748 }
749 }
750 }
751
752 /*
753 * Set up border barriers if this is a non-wrapping game.
754 */
755 if (!state->wrapping) {
756 for (x = 0; x < state->width; x++) {
757 barrier(state, x, 0) |= U;
758 barrier(state, x, state->height-1) |= D;
759 }
760 for (y = 0; y < state->height; y++) {
761 barrier(state, 0, y) |= L;
762 barrier(state, state->width-1, y) |= R;
763 }
764 }
765
766 /*
767 * Set up the barrier corner flags, for drawing barriers
768 * prettily when they meet.
769 */
770 for (y = 0; y < state->height; y++) {
771 for (x = 0; x < state->width; x++) {
772 int dir;
773
774 for (dir = 1; dir < 0x10; dir <<= 1) {
775 int dir2 = A(dir);
776 int x1, y1, x2, y2, x3, y3;
777 int corner = FALSE;
778
779 if (!(barrier(state, x, y) & dir))
780 continue;
781
782 if (barrier(state, x, y) & dir2)
783 corner = TRUE;
784
785 x1 = x + X(dir), y1 = y + Y(dir);
786 if (x1 >= 0 && x1 < state->width &&
787 y1 >= 0 && y1 < state->height &&
788 (barrier(state, x1, y1) & dir2))
789 corner = TRUE;
790
791 x2 = x + X(dir2), y2 = y + Y(dir2);
792 if (x2 >= 0 && x2 < state->width &&
793 y2 >= 0 && y2 < state->height &&
794 (barrier(state, x2, y2) & dir))
795 corner = TRUE;
796
797 if (corner) {
798 barrier(state, x, y) |= (dir << 4);
799 if (x1 >= 0 && x1 < state->width &&
800 y1 >= 0 && y1 < state->height)
801 barrier(state, x1, y1) |= (A(dir) << 4);
802 if (x2 >= 0 && x2 < state->width &&
803 y2 >= 0 && y2 < state->height)
804 barrier(state, x2, y2) |= (C(dir) << 4);
805 x3 = x + X(dir) + X(dir2), y3 = y + Y(dir) + Y(dir2);
806 if (x3 >= 0 && x3 < state->width &&
807 y3 >= 0 && y3 < state->height)
808 barrier(state, x3, y3) |= (F(dir) << 4);
809 }
810 }
811 }
812 }
813
814 return state;
815 }
816
817 static game_state *dup_game(game_state *state)
818 {
819 game_state *ret;
820
821 ret = snew(game_state);
822 ret->width = state->width;
823 ret->height = state->height;
824 ret->cx = state->cx;
825 ret->cy = state->cy;
826 ret->wrapping = state->wrapping;
827 ret->completed = state->completed;
828 ret->used_solve = state->used_solve;
829 ret->just_used_solve = state->just_used_solve;
830 ret->move_count = state->move_count;
831 ret->last_move_row = state->last_move_row;
832 ret->last_move_col = state->last_move_col;
833 ret->last_move_dir = state->last_move_dir;
834 ret->tiles = snewn(state->width * state->height, unsigned char);
835 memcpy(ret->tiles, state->tiles, state->width * state->height);
836 ret->barriers = snewn(state->width * state->height, unsigned char);
837 memcpy(ret->barriers, state->barriers, state->width * state->height);
838
839 return ret;
840 }
841
842 static void free_game(game_state *state)
843 {
844 sfree(state->tiles);
845 sfree(state->barriers);
846 sfree(state);
847 }
848
849 static game_state *solve_game(game_state *state, game_aux_info *aux,
850 char **error)
851 {
852 game_state *ret;
853
854 if (!aux) {
855 *error = "Solution not known for this puzzle";
856 return NULL;
857 }
858
859 assert(aux->width == state->width);
860 assert(aux->height == state->height);
861 ret = dup_game(state);
862 memcpy(ret->tiles, aux->tiles, ret->width * ret->height);
863 ret->used_solve = ret->just_used_solve = TRUE;
864 ret->completed = ret->move_count = 1;
865
866 return ret;
867 }
868
869 static char *game_text_format(game_state *state)
870 {
871 return NULL;
872 }
873
874 /* ----------------------------------------------------------------------
875 * Utility routine.
876 */
877
878 /*
879 * Compute which squares are reachable from the centre square, as a
880 * quick visual aid to determining how close the game is to
881 * completion. This is also a simple way to tell if the game _is_
882 * completed - just call this function and see whether every square
883 * is marked active.
884 *
885 * squares in the moving_row and moving_col are always inactive - this
886 * is so that "current" doesn't appear to jump across moving lines.
887 */
888 static unsigned char *compute_active(game_state *state,
889 int moving_row, int moving_col)
890 {
891 unsigned char *active;
892 tree234 *todo;
893 struct xyd *xyd;
894
895 active = snewn(state->width * state->height, unsigned char);
896 memset(active, 0, state->width * state->height);
897
898 /*
899 * We only store (x,y) pairs in todo, but it's easier to reuse
900 * xyd_cmp and just store direction 0 every time.
901 */
902 todo = newtree234(xyd_cmp);
903 index(state, active, state->cx, state->cy) = ACTIVE;
904 add234(todo, new_xyd(state->cx, state->cy, 0));
905
906 while ( (xyd = delpos234(todo, 0)) != NULL) {
907 int x1, y1, d1, x2, y2, d2;
908
909 x1 = xyd->x;
910 y1 = xyd->y;
911 sfree(xyd);
912
913 for (d1 = 1; d1 < 0x10; d1 <<= 1) {
914 OFFSET(x2, y2, x1, y1, d1, state);
915 d2 = F(d1);
916
917 /*
918 * If the next tile in this direction is connected to
919 * us, and there isn't a barrier in the way, and it
920 * isn't already marked active, then mark it active and
921 * add it to the to-examine list.
922 */
923 if ((x2 != moving_col && y2 != moving_row) &&
924 (tile(state, x1, y1) & d1) &&
925 (tile(state, x2, y2) & d2) &&
926 !(barrier(state, x1, y1) & d1) &&
927 !index(state, active, x2, y2)) {
928 index(state, active, x2, y2) = ACTIVE;
929 add234(todo, new_xyd(x2, y2, 0));
930 }
931 }
932 }
933 /* Now we expect the todo list to have shrunk to zero size. */
934 assert(count234(todo) == 0);
935 freetree234(todo);
936
937 return active;
938 }
939
940 struct game_ui {
941 int cur_x, cur_y;
942 int cur_visible;
943 };
944
945 static game_ui *new_ui(game_state *state)
946 {
947 game_ui *ui = snew(game_ui);
948 ui->cur_x = state->width / 2;
949 ui->cur_y = state->height / 2;
950 ui->cur_visible = FALSE;
951
952 return ui;
953 }
954
955 static void free_ui(game_ui *ui)
956 {
957 sfree(ui);
958 }
959
960 /* ----------------------------------------------------------------------
961 * Process a move.
962 */
963
964 static void slide_row_int(int w, int h, unsigned char *tiles, int dir, int row)
965 {
966 int x = dir > 0 ? -1 : w;
967 int tx = x + dir;
968 int n = w - 1;
969 unsigned char endtile = tiles[row * w + tx];
970 do {
971 x = tx;
972 tx = (x + dir + w) % w;
973 tiles[row * w + x] = tiles[row * w + tx];
974 } while (--n > 0);
975 tiles[row * w + tx] = endtile;
976 }
977
978 static void slide_col_int(int w, int h, unsigned char *tiles, int dir, int col)
979 {
980 int y = dir > 0 ? -1 : h;
981 int ty = y + dir;
982 int n = h - 1;
983 unsigned char endtile = tiles[ty * w + col];
984 do {
985 y = ty;
986 ty = (y + dir + h) % h;
987 tiles[y * w + col] = tiles[ty * w + col];
988 } while (--n > 0);
989 tiles[ty * w + col] = endtile;
990 }
991
992 static void slide_row(game_state *state, int dir, int row)
993 {
994 slide_row_int(state->width, state->height, state->tiles, dir, row);
995 }
996
997 static void slide_col(game_state *state, int dir, int col)
998 {
999 slide_col_int(state->width, state->height, state->tiles, dir, col);
1000 }
1001
1002 static game_state *make_move(game_state *state, game_ui *ui,
1003 int x, int y, int button)
1004 {
1005 int cx, cy;
1006 int n, dx, dy;
1007 game_state *ret;
1008
1009 button &= ~MOD_MASK;
1010
1011 if (button != LEFT_BUTTON && button != RIGHT_BUTTON)
1012 return NULL;
1013
1014 cx = (x - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2;
1015 cy = (y - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2;
1016
1017 if (cy >= 0 && cy < state->height && cy != state->cy)
1018 {
1019 if (cx == -1) dx = +1;
1020 else if (cx == state->width) dx = -1;
1021 else return NULL;
1022 n = state->width;
1023 dy = 0;
1024 }
1025 else if (cx >= 0 && cx < state->width && cx != state->cx)
1026 {
1027 if (cy == -1) dy = +1;
1028 else if (cy == state->height) dy = -1;
1029 else return NULL;
1030 n = state->height;
1031 dx = 0;
1032 }
1033 else
1034 return NULL;
1035
1036 /* reverse direction if right hand button is pressed */
1037 if (button == RIGHT_BUTTON)
1038 {
1039 dx = -dx;
1040 dy = -dy;
1041 }
1042
1043 ret = dup_game(state);
1044 ret->just_used_solve = FALSE;
1045
1046 if (dx == 0) slide_col(ret, dy, cx);
1047 else slide_row(ret, dx, cy);
1048
1049 ret->move_count++;
1050 ret->last_move_row = dx ? cy : -1;
1051 ret->last_move_col = dx ? -1 : cx;
1052 ret->last_move_dir = dx + dy;
1053
1054 /*
1055 * See if the game has been completed.
1056 */
1057 if (!ret->completed) {
1058 unsigned char *active = compute_active(ret, -1, -1);
1059 int x1, y1;
1060 int complete = TRUE;
1061
1062 for (x1 = 0; x1 < ret->width; x1++)
1063 for (y1 = 0; y1 < ret->height; y1++)
1064 if (!index(ret, active, x1, y1)) {
1065 complete = FALSE;
1066 goto break_label; /* break out of two loops at once */
1067 }
1068 break_label:
1069
1070 sfree(active);
1071
1072 if (complete)
1073 ret->completed = ret->move_count;
1074 }
1075
1076 return ret;
1077 }
1078
1079 /* ----------------------------------------------------------------------
1080 * Routines for drawing the game position on the screen.
1081 */
1082
1083 struct game_drawstate {
1084 int started;
1085 int width, height;
1086 unsigned char *visible;
1087 };
1088
1089 static game_drawstate *game_new_drawstate(game_state *state)
1090 {
1091 game_drawstate *ds = snew(game_drawstate);
1092
1093 ds->started = FALSE;
1094 ds->width = state->width;
1095 ds->height = state->height;
1096 ds->visible = snewn(state->width * state->height, unsigned char);
1097 memset(ds->visible, 0xFF, state->width * state->height);
1098
1099 return ds;
1100 }
1101
1102 static void game_free_drawstate(game_drawstate *ds)
1103 {
1104 sfree(ds->visible);
1105 sfree(ds);
1106 }
1107
1108 static void game_size(game_params *params, int *x, int *y)
1109 {
1110 *x = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->width + TILE_BORDER;
1111 *y = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->height + TILE_BORDER;
1112 }
1113
1114 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
1115 {
1116 float *ret;
1117
1118 ret = snewn(NCOLOURS * 3, float);
1119 *ncolours = NCOLOURS;
1120
1121 /*
1122 * Basic background colour is whatever the front end thinks is
1123 * a sensible default.
1124 */
1125 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1126
1127 /*
1128 * Wires are black.
1129 */
1130 ret[COL_WIRE * 3 + 0] = 0.0F;
1131 ret[COL_WIRE * 3 + 1] = 0.0F;
1132 ret[COL_WIRE * 3 + 2] = 0.0F;
1133
1134 /*
1135 * Powered wires and powered endpoints are cyan.
1136 */
1137 ret[COL_POWERED * 3 + 0] = 0.0F;
1138 ret[COL_POWERED * 3 + 1] = 1.0F;
1139 ret[COL_POWERED * 3 + 2] = 1.0F;
1140
1141 /*
1142 * Barriers are red.
1143 */
1144 ret[COL_BARRIER * 3 + 0] = 1.0F;
1145 ret[COL_BARRIER * 3 + 1] = 0.0F;
1146 ret[COL_BARRIER * 3 + 2] = 0.0F;
1147
1148 /*
1149 * Unpowered endpoints are blue.
1150 */
1151 ret[COL_ENDPOINT * 3 + 0] = 0.0F;
1152 ret[COL_ENDPOINT * 3 + 1] = 0.0F;
1153 ret[COL_ENDPOINT * 3 + 2] = 1.0F;
1154
1155 /*
1156 * Tile borders are a darker grey than the background.
1157 */
1158 ret[COL_BORDER * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
1159 ret[COL_BORDER * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
1160 ret[COL_BORDER * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2];
1161
1162 /*
1163 * Flashing tiles are a grey in between those two.
1164 */
1165 ret[COL_FLASHING * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0];
1166 ret[COL_FLASHING * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1];
1167 ret[COL_FLASHING * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2];
1168
1169 ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.8F;
1170 ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.8F;
1171 ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.8F;
1172 ret[COL_TEXT * 3 + 0] = 0.0;
1173 ret[COL_TEXT * 3 + 1] = 0.0;
1174 ret[COL_TEXT * 3 + 2] = 0.0;
1175
1176 return ret;
1177 }
1178
1179 static void draw_thick_line(frontend *fe, int x1, int y1, int x2, int y2,
1180 int colour)
1181 {
1182 draw_line(fe, x1-1, y1, x2-1, y2, COL_WIRE);
1183 draw_line(fe, x1+1, y1, x2+1, y2, COL_WIRE);
1184 draw_line(fe, x1, y1-1, x2, y2-1, COL_WIRE);
1185 draw_line(fe, x1, y1+1, x2, y2+1, COL_WIRE);
1186 draw_line(fe, x1, y1, x2, y2, colour);
1187 }
1188
1189 static void draw_rect_coords(frontend *fe, int x1, int y1, int x2, int y2,
1190 int colour)
1191 {
1192 int mx = (x1 < x2 ? x1 : x2);
1193 int my = (y1 < y2 ? y1 : y2);
1194 int dx = (x2 + x1 - 2*mx + 1);
1195 int dy = (y2 + y1 - 2*my + 1);
1196
1197 draw_rect(fe, mx, my, dx, dy, colour);
1198 }
1199
1200 static void draw_barrier_corner(frontend *fe, int x, int y, int dir, int phase)
1201 {
1202 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
1203 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
1204 int x1, y1, dx, dy, dir2;
1205
1206 dir >>= 4;
1207
1208 dir2 = A(dir);
1209 dx = X(dir) + X(dir2);
1210 dy = Y(dir) + Y(dir2);
1211 x1 = (dx > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
1212 y1 = (dy > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
1213
1214 if (phase == 0) {
1215 draw_rect_coords(fe, bx+x1, by+y1,
1216 bx+x1-TILE_BORDER*dx, by+y1-(TILE_BORDER-1)*dy,
1217 COL_WIRE);
1218 draw_rect_coords(fe, bx+x1, by+y1,
1219 bx+x1-(TILE_BORDER-1)*dx, by+y1-TILE_BORDER*dy,
1220 COL_WIRE);
1221 } else {
1222 draw_rect_coords(fe, bx+x1, by+y1,
1223 bx+x1-(TILE_BORDER-1)*dx, by+y1-(TILE_BORDER-1)*dy,
1224 COL_BARRIER);
1225 }
1226 }
1227
1228 static void draw_barrier(frontend *fe, int x, int y, int dir, int phase)
1229 {
1230 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
1231 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
1232 int x1, y1, w, h;
1233
1234 x1 = (X(dir) > 0 ? TILE_SIZE : X(dir) == 0 ? TILE_BORDER : 0);
1235 y1 = (Y(dir) > 0 ? TILE_SIZE : Y(dir) == 0 ? TILE_BORDER : 0);
1236 w = (X(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER);
1237 h = (Y(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER);
1238
1239 if (phase == 0) {
1240 draw_rect(fe, bx+x1-X(dir), by+y1-Y(dir), w, h, COL_WIRE);
1241 } else {
1242 draw_rect(fe, bx+x1, by+y1, w, h, COL_BARRIER);
1243 }
1244 }
1245
1246 static void draw_tile(frontend *fe, game_state *state, int x, int y, int tile,
1247 float xshift, float yshift)
1248 {
1249 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x + (xshift * TILE_SIZE);
1250 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y + (yshift * TILE_SIZE);
1251 float cx, cy, ex, ey;
1252 int dir, col;
1253
1254 /*
1255 * When we draw a single tile, we must draw everything up to
1256 * and including the borders around the tile. This means that
1257 * if the neighbouring tiles have connections to those borders,
1258 * we must draw those connections on the borders themselves.
1259 *
1260 * This would be terribly fiddly if we ever had to draw a tile
1261 * while its neighbour was in mid-rotate, because we'd have to
1262 * arrange to _know_ that the neighbour was being rotated and
1263 * hence had an anomalous effect on the redraw of this tile.
1264 * Fortunately, the drawing algorithm avoids ever calling us in
1265 * this circumstance: we're either drawing lots of straight
1266 * tiles at game start or after a move is complete, or we're
1267 * repeatedly drawing only the rotating tile. So no problem.
1268 */
1269
1270 /*
1271 * So. First blank the tile out completely: draw a big
1272 * rectangle in border colour, and a smaller rectangle in
1273 * background colour to fill it in.
1274 */
1275 draw_rect(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER,
1276 COL_BORDER);
1277 draw_rect(fe, bx+TILE_BORDER, by+TILE_BORDER,
1278 TILE_SIZE-TILE_BORDER, TILE_SIZE-TILE_BORDER,
1279 tile & FLASHING ? COL_FLASHING : COL_BACKGROUND);
1280
1281 /*
1282 * Draw the wires.
1283 */
1284 cx = cy = TILE_BORDER + (TILE_SIZE-TILE_BORDER) / 2.0F - 0.5F;
1285 col = (tile & ACTIVE ? COL_POWERED : COL_WIRE);
1286 for (dir = 1; dir < 0x10; dir <<= 1) {
1287 if (tile & dir) {
1288 ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir);
1289 ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir);
1290 draw_thick_line(fe, bx+(int)cx, by+(int)cy,
1291 bx+(int)(cx+ex), by+(int)(cy+ey),
1292 COL_WIRE);
1293 }
1294 }
1295 for (dir = 1; dir < 0x10; dir <<= 1) {
1296 if (tile & dir) {
1297 ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir);
1298 ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir);
1299 draw_line(fe, bx+(int)cx, by+(int)cy,
1300 bx+(int)(cx+ex), by+(int)(cy+ey), col);
1301 }
1302 }
1303
1304 /*
1305 * Draw the box in the middle. We do this in blue if the tile
1306 * is an unpowered endpoint, in cyan if the tile is a powered
1307 * endpoint, in black if the tile is the centrepiece, and
1308 * otherwise not at all.
1309 */
1310 col = -1;
1311 if (x == state->cx && y == state->cy)
1312 col = COL_WIRE;
1313 else if (COUNT(tile) == 1) {
1314 col = (tile & ACTIVE ? COL_POWERED : COL_ENDPOINT);
1315 }
1316 if (col >= 0) {
1317 int i, points[8];
1318
1319 points[0] = +1; points[1] = +1;
1320 points[2] = +1; points[3] = -1;
1321 points[4] = -1; points[5] = -1;
1322 points[6] = -1; points[7] = +1;
1323
1324 for (i = 0; i < 8; i += 2) {
1325 ex = (TILE_SIZE * 0.24F) * points[i];
1326 ey = (TILE_SIZE * 0.24F) * points[i+1];
1327 points[i] = bx+(int)(cx+ex);
1328 points[i+1] = by+(int)(cy+ey);
1329 }
1330
1331 draw_polygon(fe, points, 4, TRUE, col);
1332 draw_polygon(fe, points, 4, FALSE, COL_WIRE);
1333 }
1334
1335 /*
1336 * Draw the points on the border if other tiles are connected
1337 * to us.
1338 */
1339 for (dir = 1; dir < 0x10; dir <<= 1) {
1340 int dx, dy, px, py, lx, ly, vx, vy, ox, oy;
1341
1342 dx = X(dir);
1343 dy = Y(dir);
1344
1345 ox = x + dx;
1346 oy = y + dy;
1347
1348 if (ox < 0 || ox >= state->width || oy < 0 || oy >= state->height)
1349 continue;
1350
1351 if (!(tile(state, ox, oy) & F(dir)))
1352 continue;
1353
1354 px = bx + (int)(dx>0 ? TILE_SIZE + TILE_BORDER - 1 : dx<0 ? 0 : cx);
1355 py = by + (int)(dy>0 ? TILE_SIZE + TILE_BORDER - 1 : dy<0 ? 0 : cy);
1356 lx = dx * (TILE_BORDER-1);
1357 ly = dy * (TILE_BORDER-1);
1358 vx = (dy ? 1 : 0);
1359 vy = (dx ? 1 : 0);
1360
1361 if (xshift == 0.0 && yshift == 0.0 && (tile & dir)) {
1362 /*
1363 * If we are fully connected to the other tile, we must
1364 * draw right across the tile border. (We can use our
1365 * own ACTIVE state to determine what colour to do this
1366 * in: if we are fully connected to the other tile then
1367 * the two ACTIVE states will be the same.)
1368 */
1369 draw_rect_coords(fe, px-vx, py-vy, px+lx+vx, py+ly+vy, COL_WIRE);
1370 draw_rect_coords(fe, px, py, px+lx, py+ly,
1371 (tile & ACTIVE) ? COL_POWERED : COL_WIRE);
1372 } else {
1373 /*
1374 * The other tile extends into our border, but isn't
1375 * actually connected to us. Just draw a single black
1376 * dot.
1377 */
1378 draw_rect_coords(fe, px, py, px, py, COL_WIRE);
1379 }
1380 }
1381
1382 draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER);
1383 }
1384
1385 static void draw_tile_barriers(frontend *fe, game_state *state, int x, int y)
1386 {
1387 int phase;
1388 int dir;
1389 int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
1390 int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
1391 /*
1392 * Draw barrier corners, and then barriers.
1393 */
1394 for (phase = 0; phase < 2; phase++) {
1395 for (dir = 1; dir < 0x10; dir <<= 1)
1396 if (barrier(state, x, y) & (dir << 4))
1397 draw_barrier_corner(fe, x, y, dir << 4, phase);
1398 for (dir = 1; dir < 0x10; dir <<= 1)
1399 if (barrier(state, x, y) & dir)
1400 draw_barrier(fe, x, y, dir, phase);
1401 }
1402
1403 draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER);
1404 }
1405
1406 static void draw_arrow(frontend *fe, int x, int y, int xdx, int xdy)
1407 {
1408 int coords[14];
1409 int ydy = -xdx, ydx = xdy;
1410
1411 x = x * TILE_SIZE + BORDER + WINDOW_OFFSET;
1412 y = y * TILE_SIZE + BORDER + WINDOW_OFFSET;
1413
1414 #define POINT(n, xx, yy) ( \
1415 coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
1416 coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
1417
1418 POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */
1419 POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */
1420 POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */
1421 POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */
1422 POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */
1423 POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */
1424 POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */
1425
1426 draw_polygon(fe, coords, 7, TRUE, COL_LOWLIGHT);
1427 draw_polygon(fe, coords, 7, FALSE, COL_TEXT);
1428 }
1429
1430 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1431 game_state *state, int dir, game_ui *ui, float t, float ft)
1432 {
1433 int x, y, tx, ty, frame;
1434 unsigned char *active;
1435 float xshift = 0.0;
1436 float yshift = 0.0;
1437
1438 /*
1439 * Clear the screen and draw the exterior barrier lines if this
1440 * is our first call.
1441 */
1442 if (!ds->started) {
1443 int phase;
1444
1445 ds->started = TRUE;
1446
1447 draw_rect(fe, 0, 0,
1448 BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * state->width + TILE_BORDER,
1449 BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * state->height + TILE_BORDER,
1450 COL_BACKGROUND);
1451 draw_update(fe, 0, 0,
1452 BORDER * 2 + WINDOW_OFFSET*2 + TILE_SIZE*state->width + TILE_BORDER,
1453 BORDER * 2 + WINDOW_OFFSET*2 + TILE_SIZE*state->height + TILE_BORDER);
1454
1455 for (phase = 0; phase < 2; phase++) {
1456
1457 for (x = 0; x < ds->width; x++) {
1458 if (barrier(state, x, 0) & UL)
1459 draw_barrier_corner(fe, x, -1, LD, phase);
1460 if (barrier(state, x, 0) & RU)
1461 draw_barrier_corner(fe, x, -1, DR, phase);
1462 if (barrier(state, x, 0) & U)
1463 draw_barrier(fe, x, -1, D, phase);
1464 if (barrier(state, x, ds->height-1) & DR)
1465 draw_barrier_corner(fe, x, ds->height, RU, phase);
1466 if (barrier(state, x, ds->height-1) & LD)
1467 draw_barrier_corner(fe, x, ds->height, UL, phase);
1468 if (barrier(state, x, ds->height-1) & D)
1469 draw_barrier(fe, x, ds->height, U, phase);
1470 }
1471
1472 for (y = 0; y < ds->height; y++) {
1473 if (barrier(state, 0, y) & UL)
1474 draw_barrier_corner(fe, -1, y, RU, phase);
1475 if (barrier(state, 0, y) & LD)
1476 draw_barrier_corner(fe, -1, y, DR, phase);
1477 if (barrier(state, 0, y) & L)
1478 draw_barrier(fe, -1, y, R, phase);
1479 if (barrier(state, ds->width-1, y) & RU)
1480 draw_barrier_corner(fe, ds->width, y, UL, phase);
1481 if (barrier(state, ds->width-1, y) & DR)
1482 draw_barrier_corner(fe, ds->width, y, LD, phase);
1483 if (barrier(state, ds->width-1, y) & R)
1484 draw_barrier(fe, ds->width, y, L, phase);
1485 }
1486 }
1487
1488 /*
1489 * Arrows for making moves.
1490 */
1491 for (x = 0; x < ds->width; x++) {
1492 if (x == state->cx) continue;
1493 draw_arrow(fe, x, 0, +1, 0);
1494 draw_arrow(fe, x+1, ds->height, -1, 0);
1495 }
1496 for (y = 0; y < ds->height; y++) {
1497 if (y == state->cy) continue;
1498 draw_arrow(fe, ds->width, y, 0, +1);
1499 draw_arrow(fe, 0, y+1, 0, -1);
1500 }
1501 }
1502
1503 /* Check if this is an undo. If so, we will need to run any animation
1504 * backwards.
1505 */
1506 if (oldstate && oldstate->move_count > state->move_count) {
1507 game_state * tmpstate = state;
1508 state = oldstate;
1509 oldstate = tmpstate;
1510 t = ANIM_TIME - t;
1511 }
1512
1513 tx = ty = -1;
1514 if (oldstate && (t < ANIM_TIME)) {
1515 /*
1516 * We're animating a slide, of row/column number
1517 * state->last_move_pos, in direction
1518 * state->last_move_dir
1519 */
1520 xshift = state->last_move_row == -1 ? 0.0 :
1521 (1 - t / ANIM_TIME) * state->last_move_dir;
1522 yshift = state->last_move_col == -1 ? 0.0 :
1523 (1 - t / ANIM_TIME) * state->last_move_dir;
1524 }
1525
1526 frame = -1;
1527 if (ft > 0) {
1528 /*
1529 * We're animating a completion flash. Find which frame
1530 * we're at.
1531 */
1532 frame = (int)(ft / FLASH_FRAME);
1533 }
1534
1535 /*
1536 * Draw any tile which differs from the way it was last drawn.
1537 */
1538 if (xshift != 0.0 || yshift != 0.0) {
1539 active = compute_active(state,
1540 state->last_move_row, state->last_move_col);
1541 } else {
1542 active = compute_active(state, -1, -1);
1543 }
1544
1545 clip(fe,
1546 BORDER + WINDOW_OFFSET, BORDER + WINDOW_OFFSET,
1547 TILE_SIZE * state->width + TILE_BORDER,
1548 TILE_SIZE * state->height + TILE_BORDER);
1549
1550 for (x = 0; x < ds->width; x++)
1551 for (y = 0; y < ds->height; y++) {
1552 unsigned char c = tile(state, x, y) | index(state, active, x, y);
1553
1554 /*
1555 * In a completion flash, we adjust the FLASHING bit
1556 * depending on our distance from the centre point and
1557 * the frame number.
1558 */
1559 if (frame >= 0) {
1560 int xdist, ydist, dist;
1561 xdist = (x < state->cx ? state->cx - x : x - state->cx);
1562 ydist = (y < state->cy ? state->cy - y : y - state->cy);
1563 dist = (xdist > ydist ? xdist : ydist);
1564
1565 if (frame >= dist && frame < dist+4) {
1566 int flash = (frame - dist) & 1;
1567 flash = flash ? FLASHING : 0;
1568 c = (c &~ FLASHING) | flash;
1569 }
1570 }
1571
1572 if (index(state, ds->visible, x, y) != c ||
1573 index(state, ds->visible, x, y) == 0xFF ||
1574 (x == state->last_move_col || y == state->last_move_row))
1575 {
1576 float xs = (y == state->last_move_row ? xshift : 0.0);
1577 float ys = (x == state->last_move_col ? yshift : 0.0);
1578
1579 draw_tile(fe, state, x, y, c, xs, ys);
1580 if (xs < 0 && x == 0)
1581 draw_tile(fe, state, state->width, y, c, xs, ys);
1582 else if (xs > 0 && x == state->width - 1)
1583 draw_tile(fe, state, -1, y, c, xs, ys);
1584 else if (ys < 0 && y == 0)
1585 draw_tile(fe, state, x, state->height, c, xs, ys);
1586 else if (ys > 0 && y == state->height - 1)
1587 draw_tile(fe, state, x, -1, c, xs, ys);
1588
1589 if (x == state->last_move_col || y == state->last_move_row)
1590 index(state, ds->visible, x, y) = 0xFF;
1591 else
1592 index(state, ds->visible, x, y) = c;
1593 }
1594 }
1595
1596 for (x = 0; x < ds->width; x++)
1597 for (y = 0; y < ds->height; y++)
1598 draw_tile_barriers(fe, state, x, y);
1599
1600 unclip(fe);
1601
1602 /*
1603 * Update the status bar.
1604 */
1605 {
1606 char statusbuf[256];
1607 int i, n, a;
1608
1609 n = state->width * state->height;
1610 for (i = a = 0; i < n; i++)
1611 if (active[i])
1612 a++;
1613
1614 if (state->used_solve)
1615 sprintf(statusbuf, "Moves since auto-solve: %d",
1616 state->move_count - state->completed);
1617 else
1618 sprintf(statusbuf, "%sMoves: %d",
1619 (state->completed ? "COMPLETED! " : ""),
1620 (state->completed ? state->completed : state->move_count));
1621
1622 sprintf(statusbuf + strlen(statusbuf), " Active: %d/%d", a, n);
1623
1624 status_bar(fe, statusbuf);
1625 }
1626
1627 sfree(active);
1628 }
1629
1630 static float game_anim_length(game_state *oldstate,
1631 game_state *newstate, int dir)
1632 {
1633 /*
1634 * Don't animate an auto-solve move.
1635 */
1636 if ((dir > 0 && newstate->just_used_solve) ||
1637 (dir < 0 && oldstate->just_used_solve))
1638 return 0.0F;
1639
1640 return ANIM_TIME;
1641 }
1642
1643 static float game_flash_length(game_state *oldstate,
1644 game_state *newstate, int dir)
1645 {
1646 /*
1647 * If the game has just been completed, we display a completion
1648 * flash.
1649 */
1650 if (!oldstate->completed && newstate->completed &&
1651 !oldstate->used_solve && !newstate->used_solve) {
1652 int size;
1653 size = 0;
1654 if (size < newstate->cx+1)
1655 size = newstate->cx+1;
1656 if (size < newstate->cy+1)
1657 size = newstate->cy+1;
1658 if (size < newstate->width - newstate->cx)
1659 size = newstate->width - newstate->cx;
1660 if (size < newstate->height - newstate->cy)
1661 size = newstate->height - newstate->cy;
1662 return FLASH_FRAME * (size+4);
1663 }
1664
1665 return 0.0F;
1666 }
1667
1668 static int game_wants_statusbar(void)
1669 {
1670 return TRUE;
1671 }
1672
1673 #ifdef COMBINED
1674 #define thegame netslide
1675 #endif
1676
1677 const struct game thegame = {
1678 "Netslide", "games.netslide",
1679 default_params,
1680 game_fetch_preset,
1681 decode_params,
1682 encode_params,
1683 free_params,
1684 dup_params,
1685 TRUE, game_configure, custom_params,
1686 validate_params,
1687 new_game_desc,
1688 game_free_aux_info,
1689 validate_desc,
1690 new_game,
1691 dup_game,
1692 free_game,
1693 TRUE, solve_game,
1694 FALSE, game_text_format,
1695 new_ui,
1696 free_ui,
1697 make_move,
1698 game_size,
1699 game_colours,
1700 game_new_drawstate,
1701 game_free_drawstate,
1702 game_redraw,
1703 game_anim_length,
1704 game_flash_length,
1705 game_wants_statusbar,
1706 };