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