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