#include <stdlib.h>
#include <string.h>
#include <assert.h>
+#include <ctype.h>
#include <math.h>
#include "puzzles.h"
#include "tree234.h"
-const char *const game_name = "Net";
-
#define PI 3.141592653589793238462643383279502884197169399
#define MATMUL(xr,yr,m,x,y) do { \
#define D 0x08
#define LOCKED 0x10
#define ACTIVE 0x20
-/* Corner flags go in the barriers array */
-#define RU 0x10
-#define UL 0x20
-#define LD 0x40
-#define DR 0x80
/* Rotations: Anticlockwise, Clockwise, Flip, general rotate */
#define A(x) ( (((x) & 0x07) << 1) | (((x) & 0x08) >> 3) )
#define TILE_BORDER 1
#define WINDOW_OFFSET 16
-#define ROTATE_TIME 0.1F
-#define FLASH_FRAME 0.05F
+#define ROTATE_TIME 0.13F
+#define FLASH_FRAME 0.07F
+
+/* Transform physical coords to game coords using game_drawstate ds */
+#define GX(x) (((x) + ds->org_x) % ds->width)
+#define GY(y) (((y) + ds->org_y) % ds->height)
+/* ...and game coords to physical coords */
+#define RX(x) (((x) + ds->width - ds->org_x) % ds->width)
+#define RY(y) (((y) + ds->height - ds->org_y) % ds->height)
enum {
COL_BACKGROUND,
int width;
int height;
int wrapping;
+ int unique;
float barrier_probability;
};
+struct game_aux_info {
+ int width, height;
+ unsigned char *tiles;
+};
+
struct game_state {
- int width, height, cx, cy, wrapping, completed, last_rotate_dir;
+ int width, height, wrapping, completed;
+ int last_rotate_x, last_rotate_y, last_rotate_dir;
+ int used_solve, just_used_solve;
unsigned char *tiles;
unsigned char *barriers;
};
+#define OFFSETWH(x2,y2,x1,y1,dir,width,height) \
+ ( (x2) = ((x1) + width + X((dir))) % width, \
+ (y2) = ((y1) + height + Y((dir))) % height)
+
#define OFFSET(x2,y2,x1,y1,dir,state) \
- ( (x2) = ((x1) + (state)->width + X((dir))) % (state)->width, \
- (y2) = ((y1) + (state)->height + Y((dir))) % (state)->height)
+ OFFSETWH(x2,y2,x1,y1,dir,(state)->width,(state)->height)
#define index(state, a, x, y) ( a[(y) * (state)->width + (x)] )
#define tile(state, x, y) index(state, (state)->tiles, x, y)
int x, y, direction;
};
-static int xyd_cmp(void *av, void *bv) {
- struct xyd *a = (struct xyd *)av;
- struct xyd *b = (struct xyd *)bv;
+static int xyd_cmp(const void *av, const void *bv) {
+ const struct xyd *a = (const struct xyd *)av;
+ const struct xyd *b = (const struct xyd *)bv;
if (a->x < b->x)
return -1;
if (a->x > b->x)
return 0;
};
+static int xyd_cmp_nc(void *av, void *bv) { return xyd_cmp(av, bv); }
+
static struct xyd *new_xyd(int x, int y, int direction)
{
struct xyd *xyd = snew(struct xyd);
/* ----------------------------------------------------------------------
* Manage game parameters.
*/
-game_params *default_params(void)
+static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->width = 5;
ret->height = 5;
ret->wrapping = FALSE;
+ ret->unique = TRUE;
ret->barrier_probability = 0.0;
return ret;
}
-int game_fetch_preset(int i, char **name, game_params **params)
+static int game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
char str[80];
ret->width = values[i].x;
ret->height = values[i].y;
ret->wrapping = values[i].wrap;
+ ret->unique = TRUE;
ret->barrier_probability = 0.0;
sprintf(str, "%dx%d%s", ret->width, ret->height,
return TRUE;
}
-void free_params(game_params *params)
+static void free_params(game_params *params)
{
sfree(params);
}
-game_params *dup_params(game_params *params)
+static game_params *dup_params(game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
return ret;
}
+static void decode_params(game_params *ret, char const *string)
+{
+ char const *p = string;
+
+ ret->width = atoi(p);
+ while (*p && isdigit((unsigned char)*p)) p++;
+ if (*p == 'x') {
+ p++;
+ ret->height = atoi(p);
+ while (*p && isdigit((unsigned char)*p)) p++;
+ } else {
+ ret->height = ret->width;
+ }
+
+ while (*p) {
+ if (*p == 'w') {
+ p++;
+ ret->wrapping = TRUE;
+ } else if (*p == 'b') {
+ p++;
+ ret->barrier_probability = atof(p);
+ while (*p && (*p == '.' || isdigit((unsigned char)*p))) p++;
+ } else if (*p == 'a') {
+ p++;
+ ret->unique = FALSE;
+ } else
+ p++; /* skip any other gunk */
+ }
+}
+
+static char *encode_params(game_params *params, int full)
+{
+ char ret[400];
+ int len;
+
+ len = sprintf(ret, "%dx%d", params->width, params->height);
+ if (params->wrapping)
+ ret[len++] = 'w';
+ if (full && params->barrier_probability)
+ len += sprintf(ret+len, "b%g", params->barrier_probability);
+ if (full && !params->unique)
+ ret[len++] = 'a';
+ assert(len < lenof(ret));
+ ret[len] = '\0';
+
+ return dupstr(ret);
+}
+
+static config_item *game_configure(game_params *params)
+{
+ config_item *ret;
+ char buf[80];
+
+ ret = snewn(6, config_item);
+
+ ret[0].name = "Width";
+ ret[0].type = C_STRING;
+ sprintf(buf, "%d", params->width);
+ ret[0].sval = dupstr(buf);
+ ret[0].ival = 0;
+
+ ret[1].name = "Height";
+ ret[1].type = C_STRING;
+ sprintf(buf, "%d", params->height);
+ ret[1].sval = dupstr(buf);
+ ret[1].ival = 0;
+
+ ret[2].name = "Walls wrap around";
+ ret[2].type = C_BOOLEAN;
+ ret[2].sval = NULL;
+ ret[2].ival = params->wrapping;
+
+ ret[3].name = "Barrier probability";
+ ret[3].type = C_STRING;
+ sprintf(buf, "%g", params->barrier_probability);
+ ret[3].sval = dupstr(buf);
+ ret[3].ival = 0;
+
+ ret[4].name = "Ensure unique solution";
+ ret[4].type = C_BOOLEAN;
+ ret[4].sval = NULL;
+ ret[4].ival = params->unique;
+
+ ret[5].name = NULL;
+ ret[5].type = C_END;
+ ret[5].sval = NULL;
+ ret[5].ival = 0;
+
+ return ret;
+}
+
+static game_params *custom_params(config_item *cfg)
+{
+ game_params *ret = snew(game_params);
+
+ ret->width = atoi(cfg[0].sval);
+ ret->height = atoi(cfg[1].sval);
+ ret->wrapping = cfg[2].ival;
+ ret->barrier_probability = (float)atof(cfg[3].sval);
+ ret->unique = cfg[4].ival;
+
+ return ret;
+}
+
+static char *validate_params(game_params *params)
+{
+ if (params->width <= 0 && params->height <= 0)
+ return "Width and height must both be greater than zero";
+ if (params->width <= 0)
+ return "Width must be greater than zero";
+ if (params->height <= 0)
+ return "Height must be greater than zero";
+ if (params->width <= 1 && params->height <= 1)
+ return "At least one of width and height must be greater than one";
+ if (params->barrier_probability < 0)
+ return "Barrier probability may not be negative";
+ if (params->barrier_probability > 1)
+ return "Barrier probability may not be greater than 1";
+
+ /*
+ * Specifying either grid dimension as 2 in a wrapping puzzle
+ * makes it actually impossible to ensure a unique puzzle
+ * solution.
+ *
+ * Proof:
+ *
+ * Without loss of generality, let us assume the puzzle _width_
+ * is 2, so we can conveniently discuss rows without having to
+ * say `rows/columns' all the time. (The height may be 2 as
+ * well, but that doesn't matter.)
+ *
+ * In each row, there are two edges between tiles: the inner
+ * edge (running down the centre of the grid) and the outer
+ * edge (the identified left and right edges of the grid).
+ *
+ * Lemma: In any valid 2xn puzzle there must be at least one
+ * row in which _exactly one_ of the inner edge and outer edge
+ * is connected.
+ *
+ * Proof: No row can have _both_ inner and outer edges
+ * connected, because this would yield a loop. So the only
+ * other way to falsify the lemma is for every row to have
+ * _neither_ the inner nor outer edge connected. But this
+ * means there is no connection at all between the left and
+ * right columns of the puzzle, so there are two disjoint
+ * subgraphs, which is also disallowed. []
+ *
+ * Given such a row, it is always possible to make the
+ * disconnected edge connected and the connected edge
+ * disconnected without changing the state of any other edge.
+ * (This is easily seen by case analysis on the various tiles:
+ * left-pointing and right-pointing endpoints can be exchanged,
+ * likewise T-pieces, and a corner piece can select its
+ * horizontal connectivity independently of its vertical.) This
+ * yields a distinct valid solution.
+ *
+ * Thus, for _every_ row in which exactly one of the inner and
+ * outer edge is connected, there are two valid states for that
+ * row, and hence the total number of solutions of the puzzle
+ * is at least 2^(number of such rows), and in particular is at
+ * least 2 since there must be at least one such row. []
+ */
+ if (params->unique && params->wrapping &&
+ (params->width == 2 || params->height == 2))
+ return "No wrapping puzzle with a width or height of 2 can have"
+ " a unique solution";
+
+ return NULL;
+}
+
/* ----------------------------------------------------------------------
- * Randomly select a new game seed.
+ * Solver used to assure solution uniqueness during generation.
*/
-char *new_game_seed(game_params *params)
+/*
+ * Test cases I used while debugging all this were
+ *
+ * ./net --generate 1 13x11w#12300
+ * which expands under the non-unique grid generation rules to
+ * 13x11w:5eaade1bd222664436d5e2965c12656b1129dd825219e3274d558d5eb2dab5da18898e571d5a2987be79746bd95726c597447d6da96188c513add829da7681da954db113d3cd244
+ * and has two ambiguous areas.
+ *
+ * An even better one is
+ * 13x11w#507896411361192
+ * which expands to
+ * 13x11w:b7125b1aec598eb31bd58d82572bc11494e5dee4e8db2bdd29b88d41a16bdd996d2996ddec8c83741a1e8674e78328ba71737b8894a9271b1cd1399453d1952e43951d9b712822e
+ * and has an ambiguous area _and_ a situation where loop avoidance
+ * is a necessary deductive technique.
+ *
+ * Then there's
+ * 48x25w#820543338195187
+ * becoming
+ * 48x25w:255989d14cdd185deaa753a93821a12edc1ab97943ac127e2685d7b8b3c48861b2192416139212b316eddd35de43714ebc7628d753db32e596284d9ec52c5a7dc1b4c811a655117d16dc28921b2b4161352cab1d89d18bc836b8b891d55ea4622a1251861b5bc9a8aa3e5bcd745c95229ca6c3b5e21d5832d397e917325793d7eb442dc351b2db2a52ba8e1651642275842d8871d5534aabc6d5b741aaa2d48ed2a7dbbb3151ddb49d5b9a7ed1ab98ee75d613d656dbba347bc514c84556b43a9bc65a3256ead792488b862a9d2a8a39b4255a4949ed7dbd79443292521265896b4399c95ede89d7c8c797a6a57791a849adea489359a158aa12e5dacce862b8333b7ebea7d344d1a3c53198864b73a9dedde7b663abb1b539e1e8853b1b7edb14a2a17ebaae4dbe63598a2e7e9a2dbdad415bc1d8cb88cbab5a8c82925732cd282e641ea3bd7d2c6e776de9117a26be86deb7c82c89524b122cb9397cd1acd2284e744ea62b9279bae85479ababe315c3ac29c431333395b24e6a1e3c43a2da42d4dce84aadd5b154aea555eaddcbd6e527d228c19388d9b424d94214555a7edbdeebe569d4a56dc51a86bd9963e377bb74752bd5eaa5761ba545e297b62a1bda46ab4aee423ad6c661311783cc18786d4289236563cb4a75ec67d481c14814994464cd1b87396dee63e5ab6e952cc584baa1d4c47cb557ec84dbb63d487c8728118673a166846dd3a4ebc23d6cb9c5827d96b4556e91899db32b517eda815ae271a8911bd745447121dc8d321557bc2a435ebec1bbac35b1a291669451174e6aa2218a4a9c5a6ca31ebc45d84e3a82c121e9ced7d55e9a
+ * which has a spot (far right) where slightly more complex loop
+ * avoidance is required.
+ */
+
+static int dsf_canonify(int *dsf, int val)
+{
+ int v2 = val;
+
+ while (dsf[val] != val)
+ val = dsf[val];
+
+ while (v2 != val) {
+ int tmp = dsf[v2];
+ dsf[v2] = val;
+ v2 = tmp;
+ }
+
+ return val;
+}
+
+static void dsf_merge(int *dsf, int v1, int v2)
{
+ v1 = dsf_canonify(dsf, v1);
+ v2 = dsf_canonify(dsf, v2);
+ dsf[v2] = v1;
+}
+
+struct todo {
+ unsigned char *marked;
+ int *buffer;
+ int buflen;
+ int head, tail;
+};
+
+static struct todo *todo_new(int maxsize)
+{
+ struct todo *todo = snew(struct todo);
+ todo->marked = snewn(maxsize, unsigned char);
+ memset(todo->marked, 0, maxsize);
+ todo->buflen = maxsize + 1;
+ todo->buffer = snewn(todo->buflen, int);
+ todo->head = todo->tail = 0;
+ return todo;
+}
+
+static void todo_free(struct todo *todo)
+{
+ sfree(todo->marked);
+ sfree(todo->buffer);
+ sfree(todo);
+}
+
+static void todo_add(struct todo *todo, int index)
+{
+ if (todo->marked[index])
+ return; /* already on the list */
+ todo->marked[index] = TRUE;
+ todo->buffer[todo->tail++] = index;
+ if (todo->tail == todo->buflen)
+ todo->tail = 0;
+}
+
+static int todo_get(struct todo *todo) {
+ int ret;
+
+ if (todo->head == todo->tail)
+ return -1; /* list is empty */
+ ret = todo->buffer[todo->head++];
+ if (todo->head == todo->buflen)
+ todo->head = 0;
+ todo->marked[ret] = FALSE;
+
+ return ret;
+}
+
+static int net_solver(int w, int h, unsigned char *tiles,
+ unsigned char *barriers, int wrapping)
+{
+ unsigned char *tilestate;
+ unsigned char *edgestate;
+ int *deadends;
+ int *equivalence;
+ struct todo *todo;
+ int i, j, x, y;
+ int area;
+ int done_something;
+
+ /*
+ * Set up the solver's data structures.
+ */
+
+ /*
+ * tilestate stores the possible orientations of each tile.
+ * There are up to four of these, so we'll index the array in
+ * fours. tilestate[(y * w + x) * 4] and its three successive
+ * members give the possible orientations, clearing to 255 from
+ * the end as things are ruled out.
+ *
+ * In this loop we also count up the area of the grid (which is
+ * not _necessarily_ equal to w*h, because there might be one
+ * or more blank squares present. This will never happen in a
+ * grid generated _by_ this program, but it's worth keeping the
+ * solver as general as possible.)
+ */
+ tilestate = snewn(w * h * 4, unsigned char);
+ area = 0;
+ for (i = 0; i < w*h; i++) {
+ tilestate[i * 4] = tiles[i] & 0xF;
+ for (j = 1; j < 4; j++) {
+ if (tilestate[i * 4 + j - 1] == 255 ||
+ A(tilestate[i * 4 + j - 1]) == tilestate[i * 4])
+ tilestate[i * 4 + j] = 255;
+ else
+ tilestate[i * 4 + j] = A(tilestate[i * 4 + j - 1]);
+ }
+ if (tiles[i] != 0)
+ area++;
+ }
+
/*
- * The full description of a Net game is far too large to
- * encode directly in the seed, so by default we'll have to go
- * for the simple approach of providing a random-number seed.
+ * edgestate stores the known state of each edge. It is 0 for
+ * unknown, 1 for open (connected) and 2 for closed (not
+ * connected).
*
- * (This does not restrict me from _later on_ inventing a seed
- * string syntax which can never be generated by this code -
- * for example, strings beginning with a letter - allowing me
- * to type in a precise game, and have new_game detect it and
- * understand it and do something completely different.)
+ * In principle we need only worry about each edge once each,
+ * but in fact it's easier to track each edge twice so that we
+ * can reference it from either side conveniently. Also I'm
+ * going to allocate _five_ bytes per tile, rather than the
+ * obvious four, so that I can index edgestate[(y*w+x) * 5 + d]
+ * where d is 1,2,4,8 and they never overlap.
+ */
+ edgestate = snewn((w * h - 1) * 5 + 9, unsigned char);
+ memset(edgestate, 0, (w * h - 1) * 5 + 9);
+
+ /*
+ * deadends tracks which edges have dead ends on them. It is
+ * indexed by tile and direction: deadends[(y*w+x) * 5 + d]
+ * tells you whether heading out of tile (x,y) in direction d
+ * can reach a limited amount of the grid. Values are area+1
+ * (no dead end known) or less than that (can reach _at most_
+ * this many other tiles by heading this way out of this tile).
+ */
+ deadends = snewn((w * h - 1) * 5 + 9, int);
+ for (i = 0; i < (w * h - 1) * 5 + 9; i++)
+ deadends[i] = area+1;
+
+ /*
+ * equivalence tracks which sets of tiles are known to be
+ * connected to one another, so we can avoid creating loops by
+ * linking together tiles which are already linked through
+ * another route.
+ *
+ * This is a disjoint set forest structure: equivalence[i]
+ * contains the index of another member of the equivalence
+ * class containing i, or contains i itself for precisely one
+ * member in each such class. To find a representative member
+ * of the equivalence class containing i, you keep replacing i
+ * with equivalence[i] until it stops changing; then you go
+ * _back_ along the same path and point everything on it
+ * directly at the representative member so as to speed up
+ * future searches. Then you test equivalence between tiles by
+ * finding the representative of each tile and seeing if
+ * they're the same; and you create new equivalence (merge
+ * classes) by finding the representative of each tile and
+ * setting equivalence[one]=the_other.
+ */
+ equivalence = snewn(w * h, int);
+ for (i = 0; i < w*h; i++)
+ equivalence[i] = i; /* initially all distinct */
+
+ /*
+ * On a non-wrapping grid, we instantly know that all the edges
+ * round the edge are closed.
+ */
+ if (!wrapping) {
+ for (i = 0; i < w; i++) {
+ edgestate[i * 5 + 2] = edgestate[((h-1) * w + i) * 5 + 8] = 2;
+ }
+ for (i = 0; i < h; i++) {
+ edgestate[(i * w + w-1) * 5 + 1] = edgestate[(i * w) * 5 + 4] = 2;
+ }
+ }
+
+ /*
+ * If we have barriers available, we can mark those edges as
+ * closed too.
+ */
+ if (barriers) {
+ for (y = 0; y < h; y++) for (x = 0; x < w; x++) {
+ int d;
+ for (d = 1; d <= 8; d += d) {
+ if (barriers[y*w+x] & d) {
+ int x2, y2;
+ /*
+ * In principle the barrier list should already
+ * contain each barrier from each side, but
+ * let's not take chances with our internal
+ * consistency.
+ */
+ OFFSETWH(x2, y2, x, y, d, w, h);
+ edgestate[(y*w+x) * 5 + d] = 2;
+ edgestate[(y2*w+x2) * 5 + F(d)] = 2;
+ }
+ }
+ }
+ }
+
+ /*
+ * Since most deductions made by this solver are local (the
+ * exception is loop avoidance, where joining two tiles
+ * together on one side of the grid can theoretically permit a
+ * fresh deduction on the other), we can address the scaling
+ * problem inherent in iterating repeatedly over the entire
+ * grid by instead working with a to-do list.
+ */
+ todo = todo_new(w * h);
+
+ /*
+ * Main deductive loop.
+ */
+ done_something = TRUE; /* prevent instant termination! */
+ while (1) {
+ int index;
+
+ /*
+ * Take a tile index off the todo list and process it.
+ */
+ index = todo_get(todo);
+ if (index == -1) {
+ /*
+ * If we have run out of immediate things to do, we
+ * have no choice but to scan the whole grid for
+ * longer-range things we've missed. Hence, I now add
+ * every square on the grid back on to the to-do list.
+ * I also set `done_something' to FALSE at this point;
+ * if we later come back here and find it still FALSE,
+ * we will know we've scanned the entire grid without
+ * finding anything new to do, and we can terminate.
+ */
+ if (!done_something)
+ break;
+ for (i = 0; i < w*h; i++)
+ todo_add(todo, i);
+ done_something = FALSE;
+
+ index = todo_get(todo);
+ }
+
+ y = index / w;
+ x = index % w;
+ {
+ int d, ourclass = dsf_canonify(equivalence, y*w+x);
+ int deadendmax[9];
+
+ deadendmax[1] = deadendmax[2] = deadendmax[4] = deadendmax[8] = 0;
+
+ for (i = j = 0; i < 4 && tilestate[(y*w+x) * 4 + i] != 255; i++) {
+ int valid;
+ int nnondeadends, nondeadends[4], deadendtotal;
+ int nequiv, equiv[5];
+ int val = tilestate[(y*w+x) * 4 + i];
+
+ valid = TRUE;
+ nnondeadends = deadendtotal = 0;
+ equiv[0] = ourclass;
+ nequiv = 1;
+ for (d = 1; d <= 8; d += d) {
+ /*
+ * Immediately rule out this orientation if it
+ * conflicts with any known edge.
+ */
+ if ((edgestate[(y*w+x) * 5 + d] == 1 && !(val & d)) ||
+ (edgestate[(y*w+x) * 5 + d] == 2 && (val & d)))
+ valid = FALSE;
+
+ if (val & d) {
+ /*
+ * Count up the dead-end statistics.
+ */
+ if (deadends[(y*w+x) * 5 + d] <= area) {
+ deadendtotal += deadends[(y*w+x) * 5 + d];
+ } else {
+ nondeadends[nnondeadends++] = d;
+ }
+
+ /*
+ * Ensure we aren't linking to any tiles,
+ * through edges not already known to be
+ * open, which create a loop.
+ */
+ if (edgestate[(y*w+x) * 5 + d] == 0) {
+ int c, k, x2, y2;
+
+ OFFSETWH(x2, y2, x, y, d, w, h);
+ c = dsf_canonify(equivalence, y2*w+x2);
+ for (k = 0; k < nequiv; k++)
+ if (c == equiv[k])
+ break;
+ if (k == nequiv)
+ equiv[nequiv++] = c;
+ else
+ valid = FALSE;
+ }
+ }
+ }
+
+ if (nnondeadends == 0) {
+ /*
+ * If this orientation links together dead-ends
+ * with a total area of less than the entire
+ * grid, it is invalid.
+ *
+ * (We add 1 to deadendtotal because of the
+ * tile itself, of course; one tile linking
+ * dead ends of size 2 and 3 forms a subnetwork
+ * with a total area of 6, not 5.)
+ */
+ if (deadendtotal > 0 && deadendtotal+1 < area)
+ valid = FALSE;
+ } else if (nnondeadends == 1) {
+ /*
+ * If this orientation links together one or
+ * more dead-ends with precisely one
+ * non-dead-end, then we may have to mark that
+ * non-dead-end as a dead end going the other
+ * way. However, it depends on whether all
+ * other orientations share the same property.
+ */
+ deadendtotal++;
+ if (deadendmax[nondeadends[0]] < deadendtotal)
+ deadendmax[nondeadends[0]] = deadendtotal;
+ } else {
+ /*
+ * If this orientation links together two or
+ * more non-dead-ends, then we can rule out the
+ * possibility of putting in new dead-end
+ * markings in those directions.
+ */
+ int k;
+ for (k = 0; k < nnondeadends; k++)
+ deadendmax[nondeadends[k]] = area+1;
+ }
+
+ if (valid)
+ tilestate[(y*w+x) * 4 + j++] = val;
+#ifdef SOLVER_DIAGNOSTICS
+ else
+ printf("ruling out orientation %x at %d,%d\n", val, x, y);
+#endif
+ }
+
+ assert(j > 0); /* we can't lose _all_ possibilities! */
+
+ if (j < i) {
+ done_something = TRUE;
+
+ /*
+ * We have ruled out at least one tile orientation.
+ * Make sure the rest are blanked.
+ */
+ while (j < 4)
+ tilestate[(y*w+x) * 4 + j++] = 255;
+ }
+
+ /*
+ * Now go through the tile orientations again and see
+ * if we've deduced anything new about any edges.
+ */
+ {
+ int a, o;
+ a = 0xF; o = 0;
+
+ for (i = 0; i < 4 && tilestate[(y*w+x) * 4 + i] != 255; i++) {
+ a &= tilestate[(y*w+x) * 4 + i];
+ o |= tilestate[(y*w+x) * 4 + i];
+ }
+ for (d = 1; d <= 8; d += d)
+ if (edgestate[(y*w+x) * 5 + d] == 0) {
+ int x2, y2, d2;
+ OFFSETWH(x2, y2, x, y, d, w, h);
+ d2 = F(d);
+ if (a & d) {
+ /* This edge is open in all orientations. */
+#ifdef SOLVER_DIAGNOSTICS
+ printf("marking edge %d,%d:%d open\n", x, y, d);
+#endif
+ edgestate[(y*w+x) * 5 + d] = 1;
+ edgestate[(y2*w+x2) * 5 + d2] = 1;
+ dsf_merge(equivalence, y*w+x, y2*w+x2);
+ done_something = TRUE;
+ todo_add(todo, y2*w+x2);
+ } else if (!(o & d)) {
+ /* This edge is closed in all orientations. */
+#ifdef SOLVER_DIAGNOSTICS
+ printf("marking edge %d,%d:%d closed\n", x, y, d);
+#endif
+ edgestate[(y*w+x) * 5 + d] = 2;
+ edgestate[(y2*w+x2) * 5 + d2] = 2;
+ done_something = TRUE;
+ todo_add(todo, y2*w+x2);
+ }
+ }
+
+ }
+
+ /*
+ * Now check the dead-end markers and see if any of
+ * them has lowered from the real ones.
+ */
+ for (d = 1; d <= 8; d += d) {
+ int x2, y2, d2;
+ OFFSETWH(x2, y2, x, y, d, w, h);
+ d2 = F(d);
+ if (deadendmax[d] > 0 &&
+ deadends[(y2*w+x2) * 5 + d2] > deadendmax[d]) {
+#ifdef SOLVER_DIAGNOSTICS
+ printf("setting dead end value %d,%d:%d to %d\n",
+ x2, y2, d2, deadendmax[d]);
+#endif
+ deadends[(y2*w+x2) * 5 + d2] = deadendmax[d];
+ done_something = TRUE;
+ todo_add(todo, y2*w+x2);
+ }
+ }
+
+ }
+ }
+
+ /*
+ * Mark all completely determined tiles as locked.
*/
- char buf[40];
- sprintf(buf, "%d", rand());
- return dupstr(buf);
+ j = TRUE;
+ for (i = 0; i < w*h; i++) {
+ if (tilestate[i * 4 + 1] == 255) {
+ assert(tilestate[i * 4 + 0] != 255);
+ tiles[i] = tilestate[i * 4] | LOCKED;
+ } else {
+ tiles[i] &= ~LOCKED;
+ j = FALSE;
+ }
+ }
+
+ /*
+ * Free up working space.
+ */
+ todo_free(todo);
+ sfree(tilestate);
+ sfree(edgestate);
+ sfree(deadends);
+ sfree(equivalence);
+
+ return j;
}
/* ----------------------------------------------------------------------
- * Construct an initial game state, given a seed and parameters.
+ * Randomly select a new game description.
*/
-game_state *new_game(game_params *params, char *seed)
+/*
+ * Function to randomly perturb an ambiguous section in a grid, to
+ * attempt to ensure unique solvability.
+ */
+static void perturb(int w, int h, unsigned char *tiles, int wrapping,
+ random_state *rs, int startx, int starty, int startd)
{
- random_state *rs;
- game_state *state;
- tree234 *possibilities, *barriers;
- int w, h, x, y, nbarriers;
+ struct xyd *perimeter, *perim2, *loop[2], looppos[2];
+ int nperim, perimsize, nloop[2], loopsize[2];
+ int x, y, d, i;
- assert(params->width > 2);
- assert(params->height > 2);
+ /*
+ * We know that the tile at (startx,starty) is part of an
+ * ambiguous section, and we also know that its neighbour in
+ * direction startd is fully specified. We begin by tracing all
+ * the way round the ambiguous area.
+ */
+ nperim = perimsize = 0;
+ perimeter = NULL;
+ x = startx;
+ y = starty;
+ d = startd;
+#ifdef PERTURB_DIAGNOSTICS
+ printf("perturb %d,%d:%d\n", x, y, d);
+#endif
+ do {
+ int x2, y2, d2;
+
+ if (nperim >= perimsize) {
+ perimsize = perimsize * 3 / 2 + 32;
+ perimeter = sresize(perimeter, perimsize, struct xyd);
+ }
+ perimeter[nperim].x = x;
+ perimeter[nperim].y = y;
+ perimeter[nperim].direction = d;
+ nperim++;
+#ifdef PERTURB_DIAGNOSTICS
+ printf("perimeter: %d,%d:%d\n", x, y, d);
+#endif
+
+ /*
+ * First, see if we can simply turn left from where we are
+ * and find another locked square.
+ */
+ d2 = A(d);
+ OFFSETWH(x2, y2, x, y, d2, w, h);
+ if ((!wrapping && (abs(x2-x) > 1 || abs(y2-y) > 1)) ||
+ (tiles[y2*w+x2] & LOCKED)) {
+ d = d2;
+ } else {
+ /*
+ * Failing that, step left into the new square and look
+ * in front of us.
+ */
+ x = x2;
+ y = y2;
+ OFFSETWH(x2, y2, x, y, d, w, h);
+ if ((wrapping || (abs(x2-x) <= 1 && abs(y2-y) <= 1)) &&
+ !(tiles[y2*w+x2] & LOCKED)) {
+ /*
+ * And failing _that_, we're going to have to step
+ * forward into _that_ square and look right at the
+ * same locked square as we started with.
+ */
+ x = x2;
+ y = y2;
+ d = C(d);
+ }
+ }
+
+ } while (x != startx || y != starty || d != startd);
/*
- * Create a blank game state.
+ * Our technique for perturbing this ambiguous area is to
+ * search round its edge for a join we can make: that is, an
+ * edge on the perimeter which is (a) not currently connected,
+ * and (b) connecting it would not yield a full cross on either
+ * side. Then we make that join, search round the network to
+ * find the loop thus constructed, and sever the loop at a
+ * randomly selected other point.
*/
- state = snew(game_state);
- w = state->width = params->width;
- h = state->height = params->height;
- state->cx = state->width / 2;
- state->cy = state->height / 2;
- state->wrapping = params->wrapping;
- state->last_rotate_dir = +1; /* *shrug* */
- state->completed = FALSE;
- state->tiles = snewn(state->width * state->height, unsigned char);
- memset(state->tiles, 0, state->width * state->height);
- state->barriers = snewn(state->width * state->height, unsigned char);
- memset(state->barriers, 0, state->width * state->height);
+ perim2 = snewn(nperim, struct xyd);
+ memcpy(perim2, perimeter, nperim * sizeof(struct xyd));
+ /* Shuffle the perimeter, so as to search it without directional bias. */
+ for (i = nperim; --i ;) {
+ int j = random_upto(rs, i+1);
+ struct xyd t;
+
+ t = perim2[j];
+ perim2[j] = perim2[i];
+ perim2[i] = t;
+ }
+ for (i = 0; i < nperim; i++) {
+ int x2, y2;
+
+ x = perim2[i].x;
+ y = perim2[i].y;
+ d = perim2[i].direction;
+
+ OFFSETWH(x2, y2, x, y, d, w, h);
+ if (!wrapping && (abs(x2-x) > 1 || abs(y2-y) > 1))
+ continue; /* can't link across non-wrapping border */
+ if (tiles[y*w+x] & d)
+ continue; /* already linked in this direction! */
+ if (((tiles[y*w+x] | d) & 15) == 15)
+ continue; /* can't turn this tile into a cross */
+ if (((tiles[y2*w+x2] | F(d)) & 15) == 15)
+ continue; /* can't turn other tile into a cross */
+
+ /*
+ * We've found the point at which we're going to make a new
+ * link.
+ */
+#ifdef PERTURB_DIAGNOSTICS
+ printf("linking %d,%d:%d\n", x, y, d);
+#endif
+ tiles[y*w+x] |= d;
+ tiles[y2*w+x2] |= F(d);
+
+ break;
+ }
+
+ if (i == nperim)
+ return; /* nothing we can do! */
/*
- * Set up border barriers if this is a non-wrapping game.
+ * Now we've constructed a new link, we need to find the entire
+ * loop of which it is a part.
+ *
+ * In principle, this involves doing a complete search round
+ * the network. However, I anticipate that in the vast majority
+ * of cases the loop will be quite small, so what I'm going to
+ * do is make _two_ searches round the network in parallel, one
+ * keeping its metaphorical hand on the left-hand wall while
+ * the other keeps its hand on the right. As soon as one of
+ * them gets back to its starting point, I abandon the other.
*/
- if (!state->wrapping) {
- for (x = 0; x < state->width; x++) {
- barrier(state, x, 0) |= U;
- barrier(state, x, state->height-1) |= D;
- }
- for (y = 0; y < state->height; y++) {
- barrier(state, 0, y) |= L;
- barrier(state, state->width-1, y) |= R;
+ for (i = 0; i < 2; i++) {
+ loopsize[i] = nloop[i] = 0;
+ loop[i] = NULL;
+ looppos[i].x = x;
+ looppos[i].y = y;
+ looppos[i].direction = d;
+ }
+ while (1) {
+ for (i = 0; i < 2; i++) {
+ int x2, y2, j;
+
+ x = looppos[i].x;
+ y = looppos[i].y;
+ d = looppos[i].direction;
+
+ OFFSETWH(x2, y2, x, y, d, w, h);
+
+ /*
+ * Add this path segment to the loop, unless it exactly
+ * reverses the previous one on the loop in which case
+ * we take it away again.
+ */
+#ifdef PERTURB_DIAGNOSTICS
+ printf("looppos[%d] = %d,%d:%d\n", i, x, y, d);
+#endif
+ if (nloop[i] > 0 &&
+ loop[i][nloop[i]-1].x == x2 &&
+ loop[i][nloop[i]-1].y == y2 &&
+ loop[i][nloop[i]-1].direction == F(d)) {
+#ifdef PERTURB_DIAGNOSTICS
+ printf("removing path segment %d,%d:%d from loop[%d]\n",
+ x2, y2, F(d), i);
+#endif
+ nloop[i]--;
+ } else {
+ if (nloop[i] >= loopsize[i]) {
+ loopsize[i] = loopsize[i] * 3 / 2 + 32;
+ loop[i] = sresize(loop[i], loopsize[i], struct xyd);
+ }
+#ifdef PERTURB_DIAGNOSTICS
+ printf("adding path segment %d,%d:%d to loop[%d]\n",
+ x, y, d, i);
+#endif
+ loop[i][nloop[i]++] = looppos[i];
+ }
+
+#ifdef PERTURB_DIAGNOSTICS
+ printf("tile at new location is %x\n", tiles[y2*w+x2] & 0xF);
+#endif
+ d = F(d);
+ for (j = 0; j < 4; j++) {
+ if (i == 0)
+ d = A(d);
+ else
+ d = C(d);
+#ifdef PERTURB_DIAGNOSTICS
+ printf("trying dir %d\n", d);
+#endif
+ if (tiles[y2*w+x2] & d) {
+ looppos[i].x = x2;
+ looppos[i].y = y2;
+ looppos[i].direction = d;
+ break;
+ }
+ }
+
+ assert(j < 4);
+ assert(nloop[i] > 0);
+
+ if (looppos[i].x == loop[i][0].x &&
+ looppos[i].y == loop[i][0].y &&
+ looppos[i].direction == loop[i][0].direction) {
+#ifdef PERTURB_DIAGNOSTICS
+ printf("loop %d finished tracking\n", i);
+#endif
+
+ /*
+ * Having found our loop, we now sever it at a
+ * randomly chosen point - absolutely any will do -
+ * which is not the one we joined it at to begin
+ * with. Conveniently, the one we joined it at is
+ * loop[i][0], so we just avoid that one.
+ */
+ j = random_upto(rs, nloop[i]-1) + 1;
+ x = loop[i][j].x;
+ y = loop[i][j].y;
+ d = loop[i][j].direction;
+ OFFSETWH(x2, y2, x, y, d, w, h);
+ tiles[y*w+x] &= ~d;
+ tiles[y2*w+x2] &= ~F(d);
+
+ break;
+ }
}
+ if (i < 2)
+ break;
}
+ sfree(loop[0]);
+ sfree(loop[1]);
/*
- * Seed the internal random number generator.
+ * Finally, we must mark the entire disputed section as locked,
+ * to prevent the perturb function being called on it multiple
+ * times.
+ *
+ * To do this, we _sort_ the perimeter of the area. The
+ * existing xyd_cmp function will arrange things into columns
+ * for us, in such a way that each column has the edges in
+ * vertical order. Then we can work down each column and fill
+ * in all the squares between an up edge and a down edge.
*/
- rs = random_init(seed, strlen(seed));
+ qsort(perimeter, nperim, sizeof(struct xyd), xyd_cmp);
+ x = y = -1;
+ for (i = 0; i <= nperim; i++) {
+ if (i == nperim || perimeter[i].x > x) {
+ /*
+ * Fill in everything from the last Up edge to the
+ * bottom of the grid, if necessary.
+ */
+ if (x != -1) {
+ while (y < h) {
+#ifdef PERTURB_DIAGNOSTICS
+ printf("resolved: locking tile %d,%d\n", x, y);
+#endif
+ tiles[y * w + x] |= LOCKED;
+ y++;
+ }
+ x = y = -1;
+ }
+
+ if (i == nperim)
+ break;
+
+ x = perimeter[i].x;
+ y = 0;
+ }
+
+ if (perimeter[i].direction == U) {
+ x = perimeter[i].x;
+ y = perimeter[i].y;
+ } else if (perimeter[i].direction == D) {
+ /*
+ * Fill in everything from the last Up edge to here.
+ */
+ assert(x == perimeter[i].x && y <= perimeter[i].y);
+ while (y <= perimeter[i].y) {
+#ifdef PERTURB_DIAGNOSTICS
+ printf("resolved: locking tile %d,%d\n", x, y);
+#endif
+ tiles[y * w + x] |= LOCKED;
+ y++;
+ }
+ x = y = -1;
+ }
+ }
+
+ sfree(perimeter);
+}
+
+static char *new_game_desc(game_params *params, random_state *rs,
+ game_aux_info **aux)
+{
+ tree234 *possibilities, *barriertree;
+ int w, h, x, y, cx, cy, nbarriers;
+ unsigned char *tiles, *barriers;
+ char *desc, *p;
+
+ w = params->width;
+ h = params->height;
+
+ cx = w / 2;
+ cy = h / 2;
+
+ tiles = snewn(w * h, unsigned char);
+ barriers = snewn(w * h, unsigned char);
+
+ begin_generation:
+
+ memset(tiles, 0, w * h);
+ memset(barriers, 0, w * h);
/*
* Construct the unshuffled grid.
* containing no unreached squares, no full crosses _and_ no
* closed loops. []
*/
- possibilities = newtree234(xyd_cmp);
-
- add234(possibilities, new_xyd(state->cx, state->cy, R));
- add234(possibilities, new_xyd(state->cx, state->cy, U));
- add234(possibilities, new_xyd(state->cx, state->cy, L));
- add234(possibilities, new_xyd(state->cx, state->cy, D));
+ possibilities = newtree234(xyd_cmp_nc);
+
+ if (cx+1 < w)
+ add234(possibilities, new_xyd(cx, cy, R));
+ if (cy-1 >= 0)
+ add234(possibilities, new_xyd(cx, cy, U));
+ if (cx-1 >= 0)
+ add234(possibilities, new_xyd(cx, cy, L));
+ if (cy+1 < h)
+ add234(possibilities, new_xyd(cx, cy, D));
while (count234(possibilities) > 0) {
int i;
d1 = xyd->direction;
sfree(xyd);
- OFFSET(x2, y2, x1, y1, d1, state);
+ OFFSET(x2, y2, x1, y1, d1, params);
d2 = F(d1);
#ifdef DEBUG
printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n",
* Make the connection. (We should be moving to an as yet
* unused tile.)
*/
- tile(state, x1, y1) |= d1;
- assert(tile(state, x2, y2) == 0);
- tile(state, x2, y2) |= d2;
+ index(params, tiles, x1, y1) |= d1;
+ assert(index(params, tiles, x2, y2) == 0);
+ index(params, tiles, x2, y2) |= d2;
/*
* If we have created a T-piece, remove its last
* possibility.
*/
- if (COUNT(tile(state, x1, y1)) == 3) {
+ if (COUNT(index(params, tiles, x1, y1)) == 3) {
struct xyd xyd1, *xydp;
xyd1.x = x1;
xyd1.y = y1;
- xyd1.direction = 0x0F ^ tile(state, x1, y1);
+ xyd1.direction = 0x0F ^ index(params, tiles, x1, y1);
xydp = find234(possibilities, &xyd1, NULL);
int x3, y3, d3;
struct xyd xyd1, *xydp;
- OFFSET(x3, y3, x2, y2, d, state);
+ OFFSET(x3, y3, x2, y2, d, params);
d3 = F(d);
xyd1.x = x3;
if (d == d2)
continue; /* we've got this one already */
- if (!state->wrapping) {
+ if (!params->wrapping) {
if (d == U && y2 == 0)
continue;
- if (d == D && y2 == state->height-1)
+ if (d == D && y2 == h-1)
continue;
if (d == L && x2 == 0)
continue;
- if (d == R && x2 == state->width-1)
+ if (d == R && x2 == w-1)
continue;
}
- OFFSET(x3, y3, x2, y2, d, state);
+ OFFSET(x3, y3, x2, y2, d, params);
- if (tile(state, x3, y3))
+ if (index(params, tiles, x3, y3))
continue; /* this would create a loop */
#ifdef DEBUG
assert(count234(possibilities) == 0);
freetree234(possibilities);
+ if (params->unique) {
+ int prevn = -1;
+
+ /*
+ * Run the solver to check unique solubility.
+ */
+ while (!net_solver(w, h, tiles, NULL, params->wrapping)) {
+ int n = 0;
+
+ /*
+ * We expect (in most cases) that most of the grid will
+ * be uniquely specified already, and the remaining
+ * ambiguous sections will be small and separate. So
+ * our strategy is to find each individual such
+ * section, and perform a perturbation on the network
+ * in that area.
+ */
+ for (y = 0; y < h; y++) for (x = 0; x < w; x++) {
+ if (x+1 < w && ((tiles[y*w+x] ^ tiles[y*w+x+1]) & LOCKED)) {
+ n++;
+ if (tiles[y*w+x] & LOCKED)
+ perturb(w, h, tiles, params->wrapping, rs, x+1, y, L);
+ else
+ perturb(w, h, tiles, params->wrapping, rs, x, y, R);
+ }
+ if (y+1 < h && ((tiles[y*w+x] ^ tiles[(y+1)*w+x]) & LOCKED)) {
+ n++;
+ if (tiles[y*w+x] & LOCKED)
+ perturb(w, h, tiles, params->wrapping, rs, x, y+1, U);
+ else
+ perturb(w, h, tiles, params->wrapping, rs, x, y, D);
+ }
+ }
+
+ /*
+ * Now n counts the number of ambiguous sections we
+ * have fiddled with. If we haven't managed to decrease
+ * it from the last time we ran the solver, give up and
+ * regenerate the entire grid.
+ */
+ if (prevn != -1 && prevn <= n)
+ goto begin_generation; /* (sorry) */
+
+ prevn = n;
+ }
+
+ /*
+ * The solver will have left a lot of LOCKED bits lying
+ * around in the tiles array. Remove them.
+ */
+ for (x = 0; x < w*h; x++)
+ tiles[x] &= ~LOCKED;
+ }
+
/*
* Now compute a list of the possible barrier locations.
*/
- barriers = newtree234(xyd_cmp);
- for (y = 0; y < state->height; y++) {
- for (x = 0; x < state->width; x++) {
-
- if (!(tile(state, x, y) & R) &&
- (state->wrapping || x < state->width-1))
- add234(barriers, new_xyd(x, y, R));
- if (!(tile(state, x, y) & D) &&
- (state->wrapping || y < state->height-1))
- add234(barriers, new_xyd(x, y, D));
+ barriertree = newtree234(xyd_cmp_nc);
+ for (y = 0; y < h; y++) {
+ for (x = 0; x < w; x++) {
+
+ if (!(index(params, tiles, x, y) & R) &&
+ (params->wrapping || x < w-1))
+ add234(barriertree, new_xyd(x, y, R));
+ if (!(index(params, tiles, x, y) & D) &&
+ (params->wrapping || y < h-1))
+ add234(barriertree, new_xyd(x, y, D));
}
}
/*
+ * Save the unshuffled grid in an aux_info.
+ */
+ {
+ game_aux_info *solution;
+
+ solution = snew(game_aux_info);
+ solution->width = w;
+ solution->height = h;
+ solution->tiles = snewn(w * h, unsigned char);
+ memcpy(solution->tiles, tiles, w * h);
+
+ *aux = solution;
+ }
+
+ /*
* Now shuffle the grid.
*/
- for (y = 0; y < state->height; y++) {
- for (x = 0; x < state->width; x++) {
- int orig = tile(state, x, y);
+ for (y = 0; y < h; y++) {
+ for (x = 0; x < w; x++) {
+ int orig = index(params, tiles, x, y);
int rot = random_upto(rs, 4);
- tile(state, x, y) = ROT(orig, rot);
+ index(params, tiles, x, y) = ROT(orig, rot);
}
}
/*
* And now choose barrier locations. (We carefully do this
* _after_ shuffling, so that changing the barrier rate in the
- * params while keeping the game seed the same will give the
+ * params while keeping the random seed the same will give the
* same shuffled grid and _only_ change the barrier locations.
* Also the way we choose barrier locations, by repeatedly
* choosing one possibility from the list until we have enough,
* the original 10 plus 10 more, rather than getting 20 new
* ones and the chance of remembering your first 10.)
*/
- nbarriers = (int)(params->barrier_probability * count234(barriers));
- assert(nbarriers >= 0 && nbarriers <= count234(barriers));
+ nbarriers = (int)(params->barrier_probability * count234(barriertree));
+ assert(nbarriers >= 0 && nbarriers <= count234(barriertree));
while (nbarriers > 0) {
int i;
/*
* Extract a randomly chosen barrier from the list.
*/
- i = random_upto(rs, count234(barriers));
- xyd = delpos234(barriers, i);
+ i = random_upto(rs, count234(barriertree));
+ xyd = delpos234(barriertree, i);
assert(xyd != NULL);
d1 = xyd->direction;
sfree(xyd);
- OFFSET(x2, y2, x1, y1, d1, state);
+ OFFSET(x2, y2, x1, y1, d1, params);
d2 = F(d1);
- barrier(state, x1, y1) |= d1;
- barrier(state, x2, y2) |= d2;
+ index(params, barriers, x1, y1) |= d1;
+ index(params, barriers, x2, y2) |= d2;
nbarriers--;
}
{
struct xyd *xyd;
- while ( (xyd = delpos234(barriers, 0)) != NULL)
+ while ( (xyd = delpos234(barriertree, 0)) != NULL)
sfree(xyd);
- freetree234(barriers);
+ freetree234(barriertree);
}
/*
- * Set up the barrier corner flags, for drawing barriers
- * prettily when they meet.
+ * Finally, encode the grid into a string game description.
+ *
+ * My syntax is extremely simple: each square is encoded as a
+ * hex digit in which bit 0 means a connection on the right,
+ * bit 1 means up, bit 2 left and bit 3 down. (i.e. the same
+ * encoding as used internally). Each digit is followed by
+ * optional barrier indicators: `v' means a vertical barrier to
+ * the right of it, and `h' means a horizontal barrier below
+ * it.
*/
- for (y = 0; y < state->height; y++) {
- for (x = 0; x < state->width; x++) {
- int dir;
+ desc = snewn(w * h * 3 + 1, char);
+ p = desc;
+ for (y = 0; y < h; y++) {
+ for (x = 0; x < w; x++) {
+ *p++ = "0123456789abcdef"[index(params, tiles, x, y)];
+ if ((params->wrapping || x < w-1) &&
+ (index(params, barriers, x, y) & R))
+ *p++ = 'v';
+ if ((params->wrapping || y < h-1) &&
+ (index(params, barriers, x, y) & D))
+ *p++ = 'h';
+ }
+ }
+ assert(p - desc <= w*h*3);
+ *p = '\0';
- for (dir = 1; dir < 0x10; dir <<= 1) {
- int dir2 = A(dir);
- int x1, y1, x2, y2, x3, y3;
- int corner = FALSE;
+ sfree(tiles);
+ sfree(barriers);
- if (!(barrier(state, x, y) & dir))
- continue;
+ return desc;
+}
- if (barrier(state, x, y) & dir2)
- corner = TRUE;
+static void game_free_aux_info(game_aux_info *aux)
+{
+ sfree(aux->tiles);
+ sfree(aux);
+}
- x1 = x + X(dir), y1 = y + Y(dir);
- if (x1 >= 0 && x1 < state->width &&
- y1 >= 0 && y1 < state->height &&
- (barrier(state, x1, y1) & dir2))
- corner = TRUE;
+static char *validate_desc(game_params *params, char *desc)
+{
+ int w = params->width, h = params->height;
+ int i;
+
+ for (i = 0; i < w*h; i++) {
+ if (*desc >= '0' && *desc <= '9')
+ /* OK */;
+ else if (*desc >= 'a' && *desc <= 'f')
+ /* OK */;
+ else if (*desc >= 'A' && *desc <= 'F')
+ /* OK */;
+ else if (!*desc)
+ return "Game description shorter than expected";
+ else
+ return "Game description contained unexpected character";
+ desc++;
+ while (*desc == 'h' || *desc == 'v')
+ desc++;
+ }
+ if (*desc)
+ return "Game description longer than expected";
- x2 = x + X(dir2), y2 = y + Y(dir2);
- if (x2 >= 0 && x2 < state->width &&
- y2 >= 0 && y2 < state->height &&
- (barrier(state, x2, y2) & dir))
- corner = TRUE;
+ return NULL;
+}
- if (corner) {
- barrier(state, x, y) |= (dir << 4);
- if (x1 >= 0 && x1 < state->width &&
- y1 >= 0 && y1 < state->height)
- barrier(state, x1, y1) |= (A(dir) << 4);
- if (x2 >= 0 && x2 < state->width &&
- y2 >= 0 && y2 < state->height)
- barrier(state, x2, y2) |= (C(dir) << 4);
- x3 = x + X(dir) + X(dir2), y3 = y + Y(dir) + Y(dir2);
- if (x3 >= 0 && x3 < state->width &&
- y3 >= 0 && y3 < state->height)
- barrier(state, x3, y3) |= (F(dir) << 4);
- }
+/* ----------------------------------------------------------------------
+ * Construct an initial game state, given a description and parameters.
+ */
+
+static game_state *new_game(midend_data *me, game_params *params, char *desc)
+{
+ game_state *state;
+ int w, h, x, y;
+
+ assert(params->width > 0 && params->height > 0);
+ assert(params->width > 1 || params->height > 1);
+
+ /*
+ * Create a blank game state.
+ */
+ state = snew(game_state);
+ w = state->width = params->width;
+ h = state->height = params->height;
+ state->wrapping = params->wrapping;
+ state->last_rotate_dir = state->last_rotate_x = state->last_rotate_y = 0;
+ state->completed = state->used_solve = state->just_used_solve = FALSE;
+ state->tiles = snewn(state->width * state->height, unsigned char);
+ memset(state->tiles, 0, state->width * state->height);
+ state->barriers = snewn(state->width * state->height, unsigned char);
+ memset(state->barriers, 0, state->width * state->height);
+
+ /*
+ * Parse the game description into the grid.
+ */
+ for (y = 0; y < h; y++) {
+ for (x = 0; x < w; x++) {
+ if (*desc >= '0' && *desc <= '9')
+ tile(state, x, y) = *desc - '0';
+ else if (*desc >= 'a' && *desc <= 'f')
+ tile(state, x, y) = *desc - 'a' + 10;
+ else if (*desc >= 'A' && *desc <= 'F')
+ tile(state, x, y) = *desc - 'A' + 10;
+ if (*desc)
+ desc++;
+ while (*desc == 'h' || *desc == 'v') {
+ int x2, y2, d1, d2;
+ if (*desc == 'v')
+ d1 = R;
+ else
+ d1 = D;
+
+ OFFSET(x2, y2, x, y, d1, state);
+ d2 = F(d1);
+
+ barrier(state, x, y) |= d1;
+ barrier(state, x2, y2) |= d2;
+
+ desc++;
}
- }
+ }
}
- random_free(rs);
+ /*
+ * Set up border barriers if this is a non-wrapping game.
+ */
+ if (!state->wrapping) {
+ for (x = 0; x < state->width; x++) {
+ barrier(state, x, 0) |= U;
+ barrier(state, x, state->height-1) |= D;
+ }
+ for (y = 0; y < state->height; y++) {
+ barrier(state, 0, y) |= L;
+ barrier(state, state->width-1, y) |= R;
+ }
+ } else {
+ /*
+ * We check whether this is de-facto a non-wrapping game
+ * despite the parameters, in case we were passed the
+ * description of a non-wrapping game. This is so that we
+ * can change some aspects of the UI behaviour.
+ */
+ state->wrapping = FALSE;
+ for (x = 0; x < state->width; x++)
+ if (!(barrier(state, x, 0) & U) ||
+ !(barrier(state, x, state->height-1) & D))
+ state->wrapping = TRUE;
+ for (y = 0; y < state->width; y++)
+ if (!(barrier(state, 0, y) & L) ||
+ !(barrier(state, state->width-1, y) & R))
+ state->wrapping = TRUE;
+ }
return state;
}
-game_state *dup_game(game_state *state)
+static game_state *dup_game(game_state *state)
{
game_state *ret;
ret = snew(game_state);
ret->width = state->width;
ret->height = state->height;
- ret->cx = state->cx;
- ret->cy = state->cy;
ret->wrapping = state->wrapping;
ret->completed = state->completed;
+ ret->used_solve = state->used_solve;
+ ret->just_used_solve = state->just_used_solve;
ret->last_rotate_dir = state->last_rotate_dir;
+ ret->last_rotate_x = state->last_rotate_x;
+ ret->last_rotate_y = state->last_rotate_y;
ret->tiles = snewn(state->width * state->height, unsigned char);
memcpy(ret->tiles, state->tiles, state->width * state->height);
ret->barriers = snewn(state->width * state->height, unsigned char);
return ret;
}
-void free_game(game_state *state)
+static void free_game(game_state *state)
{
sfree(state->tiles);
sfree(state->barriers);
sfree(state);
}
+static game_state *solve_game(game_state *state, game_aux_info *aux,
+ char **error)
+{
+ game_state *ret;
+
+ if (!aux) {
+ /*
+ * Run the internal solver on the provided grid. This might
+ * not yield a complete solution.
+ */
+ ret = dup_game(state);
+ net_solver(ret->width, ret->height, ret->tiles,
+ ret->barriers, ret->wrapping);
+ } else {
+ assert(aux->width == state->width);
+ assert(aux->height == state->height);
+ ret = dup_game(state);
+ memcpy(ret->tiles, aux->tiles, ret->width * ret->height);
+ ret->used_solve = ret->just_used_solve = TRUE;
+ ret->completed = TRUE;
+ }
+
+ return ret;
+}
+
+static char *game_text_format(game_state *state)
+{
+ return NULL;
+}
+
/* ----------------------------------------------------------------------
* Utility routine.
*/
* completed - just call this function and see whether every square
* is marked active.
*/
-static unsigned char *compute_active(game_state *state)
+static unsigned char *compute_active(game_state *state, int cx, int cy)
{
unsigned char *active;
tree234 *todo;
* We only store (x,y) pairs in todo, but it's easier to reuse
* xyd_cmp and just store direction 0 every time.
*/
- todo = newtree234(xyd_cmp);
- index(state, active, state->cx, state->cy) = ACTIVE;
- add234(todo, new_xyd(state->cx, state->cy, 0));
+ todo = newtree234(xyd_cmp_nc);
+ index(state, active, cx, cy) = ACTIVE;
+ add234(todo, new_xyd(cx, cy, 0));
while ( (xyd = delpos234(todo, 0)) != NULL) {
int x1, y1, d1, x2, y2, d2;
return active;
}
+struct game_ui {
+ int org_x, org_y; /* origin */
+ int cx, cy; /* source tile (game coordinates) */
+ int cur_x, cur_y;
+ int cur_visible;
+ random_state *rs; /* used for jumbling */
+};
+
+static game_ui *new_ui(game_state *state)
+{
+ void *seed;
+ int seedsize;
+ game_ui *ui = snew(game_ui);
+ ui->org_x = ui->org_y = 0;
+ ui->cur_x = ui->cx = state->width / 2;
+ ui->cur_y = ui->cy = state->height / 2;
+ ui->cur_visible = FALSE;
+ get_random_seed(&seed, &seedsize);
+ ui->rs = random_init(seed, seedsize);
+ sfree(seed);
+
+ return ui;
+}
+
+static void free_ui(game_ui *ui)
+{
+ random_free(ui->rs);
+ sfree(ui);
+}
+
/* ----------------------------------------------------------------------
* Process a move.
*/
-game_state *make_move(game_state *state, int x, int y, int button)
+static game_state *make_move(game_state *state, game_ui *ui,
+ int x, int y, int button)
{
- game_state *ret;
+ game_state *ret, *nullret;
int tx, ty, orig;
+ int shift = button & MOD_SHFT, ctrl = button & MOD_CTRL;
- /*
- * All moves in Net are made with the mouse.
- */
- if (button != LEFT_BUTTON &&
- button != MIDDLE_BUTTON &&
- button != RIGHT_BUTTON)
- return NULL;
+ button &= ~MOD_MASK;
+ nullret = NULL;
- /*
- * The button must have been clicked on a valid tile.
- */
- x -= WINDOW_OFFSET + TILE_BORDER;
- y -= WINDOW_OFFSET + TILE_BORDER;
- if (x < 0 || y < 0)
- return NULL;
- tx = x / TILE_SIZE;
- ty = y / TILE_SIZE;
- if (tx >= state->width || ty >= state->height)
- return NULL;
- if (tx % TILE_SIZE >= TILE_SIZE - TILE_BORDER ||
- ty % TILE_SIZE >= TILE_SIZE - TILE_BORDER)
- return NULL;
+ if (button == LEFT_BUTTON ||
+ button == MIDDLE_BUTTON ||
+ button == RIGHT_BUTTON) {
+
+ if (ui->cur_visible) {
+ ui->cur_visible = FALSE;
+ nullret = state;
+ }
+
+ /*
+ * The button must have been clicked on a valid tile.
+ */
+ x -= WINDOW_OFFSET + TILE_BORDER;
+ y -= WINDOW_OFFSET + TILE_BORDER;
+ if (x < 0 || y < 0)
+ return nullret;
+ tx = x / TILE_SIZE;
+ ty = y / TILE_SIZE;
+ if (tx >= state->width || ty >= state->height)
+ return nullret;
+ /* Transform from physical to game coords */
+ tx = (tx + ui->org_x) % state->width;
+ ty = (ty + ui->org_y) % state->height;
+ if (x % TILE_SIZE >= TILE_SIZE - TILE_BORDER ||
+ y % TILE_SIZE >= TILE_SIZE - TILE_BORDER)
+ return nullret;
+ } else if (button == CURSOR_UP || button == CURSOR_DOWN ||
+ button == CURSOR_RIGHT || button == CURSOR_LEFT) {
+ int dir;
+ switch (button) {
+ case CURSOR_UP: dir = U; break;
+ case CURSOR_DOWN: dir = D; break;
+ case CURSOR_LEFT: dir = L; break;
+ case CURSOR_RIGHT: dir = R; break;
+ default: return nullret;
+ }
+ if (shift) {
+ /*
+ * Move origin.
+ */
+ if (state->wrapping) {
+ OFFSET(ui->org_x, ui->org_y, ui->org_x, ui->org_y, dir, state);
+ } else return nullret; /* disallowed for non-wrapping grids */
+ }
+ if (ctrl) {
+ /*
+ * Change source tile.
+ */
+ OFFSET(ui->cx, ui->cy, ui->cx, ui->cy, dir, state);
+ }
+ if (!shift && !ctrl) {
+ /*
+ * Move keyboard cursor.
+ */
+ OFFSET(ui->cur_x, ui->cur_y, ui->cur_x, ui->cur_y, dir, state);
+ ui->cur_visible = TRUE;
+ }
+ return state; /* UI activity has occurred */
+ } else if (button == 'a' || button == 's' || button == 'd' ||
+ button == 'A' || button == 'S' || button == 'D') {
+ tx = ui->cur_x;
+ ty = ui->cur_y;
+ if (button == 'a' || button == 'A')
+ button = LEFT_BUTTON;
+ else if (button == 's' || button == 'S')
+ button = MIDDLE_BUTTON;
+ else if (button == 'd' || button == 'D')
+ button = RIGHT_BUTTON;
+ ui->cur_visible = TRUE;
+ } else if (button == 'j' || button == 'J') {
+ /* XXX should we have some mouse control for this? */
+ button = 'J'; /* canonify */
+ tx = ty = -1; /* shut gcc up :( */
+ } else
+ return nullret;
/*
* The middle button locks or unlocks a tile. (A locked tile
* unlocks it.)
*/
if (button == MIDDLE_BUTTON) {
+
ret = dup_game(state);
+ ret->just_used_solve = FALSE;
tile(ret, tx, ty) ^= LOCKED;
+ ret->last_rotate_dir = ret->last_rotate_x = ret->last_rotate_y = 0;
return ret;
- }
- /*
- * The left and right buttons have no effect if clicked on a
- * locked tile.
- */
- if (tile(state, tx, ty) & LOCKED)
- return NULL;
+ } else if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
- /*
- * Otherwise, turn the tile one way or the other. Left button
- * turns anticlockwise; right button turns clockwise.
- */
- ret = dup_game(state);
- orig = tile(ret, tx, ty);
- if (button == LEFT_BUTTON) {
- tile(ret, tx, ty) = A(orig);
- ret->last_rotate_dir = +1;
- } else {
- tile(ret, tx, ty) = C(orig);
- ret->last_rotate_dir = -1;
- }
+ /*
+ * The left and right buttons have no effect if clicked on a
+ * locked tile.
+ */
+ if (tile(state, tx, ty) & LOCKED)
+ return nullret;
+
+ /*
+ * Otherwise, turn the tile one way or the other. Left button
+ * turns anticlockwise; right button turns clockwise.
+ */
+ ret = dup_game(state);
+ ret->just_used_solve = FALSE;
+ orig = tile(ret, tx, ty);
+ if (button == LEFT_BUTTON) {
+ tile(ret, tx, ty) = A(orig);
+ ret->last_rotate_dir = +1;
+ } else {
+ tile(ret, tx, ty) = C(orig);
+ ret->last_rotate_dir = -1;
+ }
+ ret->last_rotate_x = tx;
+ ret->last_rotate_y = ty;
+
+ } else if (button == 'J') {
+
+ /*
+ * Jumble all unlocked tiles to random orientations.
+ */
+ int jx, jy;
+ ret = dup_game(state);
+ ret->just_used_solve = FALSE;
+ for (jy = 0; jy < ret->height; jy++) {
+ for (jx = 0; jx < ret->width; jx++) {
+ if (!(tile(ret, jx, jy) & LOCKED)) {
+ int rot = random_upto(ui->rs, 4);
+ orig = tile(ret, jx, jy);
+ tile(ret, jx, jy) = ROT(orig, rot);
+ }
+ }
+ }
+ ret->last_rotate_dir = 0; /* suppress animation */
+ ret->last_rotate_x = ret->last_rotate_y = 0;
+
+ } else assert(0);
/*
* Check whether the game has been completed.
*/
{
- unsigned char *active = compute_active(ret);
+ unsigned char *active = compute_active(ret, ui->cx, ui->cy);
int x1, y1;
int complete = TRUE;
for (x1 = 0; x1 < ret->width; x1++)
for (y1 = 0; y1 < ret->height; y1++)
- if (!index(ret, active, x1, y1)) {
+ if ((tile(ret, x1, y1) & 0xF) && !index(ret, active, x1, y1)) {
complete = FALSE;
goto break_label; /* break out of two loops at once */
}
struct game_drawstate {
int started;
int width, height;
+ int org_x, org_y;
unsigned char *visible;
};
-game_drawstate *game_new_drawstate(game_state *state)
+static game_drawstate *game_new_drawstate(game_state *state)
{
game_drawstate *ds = snew(game_drawstate);
ds->started = FALSE;
ds->width = state->width;
ds->height = state->height;
+ ds->org_x = ds->org_y = -1;
ds->visible = snewn(state->width * state->height, unsigned char);
memset(ds->visible, 0xFF, state->width * state->height);
return ds;
}
-void game_free_drawstate(game_drawstate *ds)
+static void game_free_drawstate(game_drawstate *ds)
{
sfree(ds->visible);
sfree(ds);
}
-void game_size(game_params *params, int *x, int *y)
+static void game_size(game_params *params, int *x, int *y)
{
*x = WINDOW_OFFSET * 2 + TILE_SIZE * params->width + TILE_BORDER;
*y = WINDOW_OFFSET * 2 + TILE_SIZE * params->height + TILE_BORDER;
}
-float *game_colours(frontend *fe, game_state *state, int *ncolours)
+static float *game_colours(frontend *fe, game_state *state, int *ncolours)
{
float *ret;
draw_rect(fe, mx, my, dx, dy, colour);
}
-static void draw_barrier_corner(frontend *fe, int x, int y, int dir, int phase)
+/*
+ * draw_barrier_corner() and draw_barrier() are passed physical coords
+ */
+static void draw_barrier_corner(frontend *fe, int x, int y, int dx, int dy,
+ int phase)
{
int bx = WINDOW_OFFSET + TILE_SIZE * x;
int by = WINDOW_OFFSET + TILE_SIZE * y;
- int x1, y1, dx, dy, dir2;
-
- dir >>= 4;
+ int x1, y1;
- dir2 = A(dir);
- dx = X(dir) + X(dir2);
- dy = Y(dir) + Y(dir2);
x1 = (dx > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
y1 = (dy > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
if (phase == 0) {
- draw_rect_coords(fe, bx+x1, by+y1,
+ draw_rect_coords(fe, bx+x1+dx, by+y1,
bx+x1-TILE_BORDER*dx, by+y1-(TILE_BORDER-1)*dy,
COL_WIRE);
- draw_rect_coords(fe, bx+x1, by+y1,
+ draw_rect_coords(fe, bx+x1, by+y1+dy,
bx+x1-(TILE_BORDER-1)*dx, by+y1-TILE_BORDER*dy,
COL_WIRE);
} else {
}
}
-static void draw_tile(frontend *fe, game_state *state, int x, int y, int tile,
- float angle)
+/*
+ * draw_tile() is passed physical coordinates
+ */
+static void draw_tile(frontend *fe, game_state *state, game_drawstate *ds,
+ int x, int y, int tile, int src, float angle, int cursor)
{
int bx = WINDOW_OFFSET + TILE_SIZE * x;
int by = WINDOW_OFFSET + TILE_SIZE * y;
* and including the borders around the tile. This means that
* if the neighbouring tiles have connections to those borders,
* we must draw those connections on the borders themselves.
- *
- * This would be terribly fiddly if we ever had to draw a tile
- * while its neighbour was in mid-rotate, because we'd have to
- * arrange to _know_ that the neighbour was being rotated and
- * hence had an anomalous effect on the redraw of this tile.
- * Fortunately, the drawing algorithm avoids ever calling us in
- * this circumstance: we're either drawing lots of straight
- * tiles at game start or after a move is complete, or we're
- * repeatedly drawing only the rotating tile. So no problem.
*/
+ clip(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER);
+
/*
* So. First blank the tile out completely: draw a big
* rectangle in border colour, and a smaller rectangle in
tile & LOCKED ? COL_LOCKED : COL_BACKGROUND);
/*
+ * Draw an inset outline rectangle as a cursor, in whichever of
+ * COL_LOCKED and COL_BACKGROUND we aren't currently drawing
+ * in.
+ */
+ if (cursor) {
+ draw_line(fe, bx+TILE_SIZE/8, by+TILE_SIZE/8,
+ bx+TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8,
+ tile & LOCKED ? COL_BACKGROUND : COL_LOCKED);
+ draw_line(fe, bx+TILE_SIZE/8, by+TILE_SIZE/8,
+ bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE/8,
+ tile & LOCKED ? COL_BACKGROUND : COL_LOCKED);
+ draw_line(fe, bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE/8,
+ bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8,
+ tile & LOCKED ? COL_BACKGROUND : COL_LOCKED);
+ draw_line(fe, bx+TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8,
+ bx+TILE_SIZE-TILE_SIZE/8, by+TILE_SIZE-TILE_SIZE/8,
+ tile & LOCKED ? COL_BACKGROUND : COL_LOCKED);
+ }
+
+ /*
* Set up the rotation matrix.
*/
matrix[0] = (float)cos(angle * PI / 180.0);
* otherwise not at all.
*/
col = -1;
- if (x == state->cx && y == state->cy)
+ if (src)
col = COL_WIRE;
else if (COUNT(tile) == 1) {
col = (tile & ACTIVE ? COL_POWERED : COL_ENDPOINT);
if (ox < 0 || ox >= state->width || oy < 0 || oy >= state->height)
continue;
- if (!(tile(state, ox, oy) & F(dir)))
+ if (!(tile(state, GX(ox), GY(oy)) & F(dir)))
continue;
px = bx + (int)(dx>0 ? TILE_SIZE + TILE_BORDER - 1 : dx<0 ? 0 : cx);
* Draw barrier corners, and then barriers.
*/
for (phase = 0; phase < 2; phase++) {
+ for (dir = 1; dir < 0x10; dir <<= 1) {
+ int x1, y1, corner = FALSE;
+ /*
+ * If at least one barrier terminates at the corner
+ * between dir and A(dir), draw a barrier corner.
+ */
+ if (barrier(state, GX(x), GY(y)) & (dir | A(dir))) {
+ corner = TRUE;
+ } else {
+ /*
+ * Only count barriers terminating at this corner
+ * if they're physically next to the corner. (That
+ * is, if they've wrapped round from the far side
+ * of the screen, they don't count.)
+ */
+ x1 = x + X(dir);
+ y1 = y + Y(dir);
+ if (x1 >= 0 && x1 < state->width &&
+ y1 >= 0 && y1 < state->height &&
+ (barrier(state, GX(x1), GY(y1)) & A(dir))) {
+ corner = TRUE;
+ } else {
+ x1 = x + X(A(dir));
+ y1 = y + Y(A(dir));
+ if (x1 >= 0 && x1 < state->width &&
+ y1 >= 0 && y1 < state->height &&
+ (barrier(state, GX(x1), GY(y1)) & dir))
+ corner = TRUE;
+ }
+ }
+
+ if (corner) {
+ /*
+ * At least one barrier terminates here. Draw a
+ * corner.
+ */
+ draw_barrier_corner(fe, x, y,
+ X(dir)+X(A(dir)), Y(dir)+Y(A(dir)),
+ phase);
+ }
+ }
+
for (dir = 1; dir < 0x10; dir <<= 1)
- if (barrier(state, x, y) & (dir << 4))
- draw_barrier_corner(fe, x, y, dir << 4, phase);
- for (dir = 1; dir < 0x10; dir <<= 1)
- if (barrier(state, x, y) & dir)
+ if (barrier(state, GX(x), GY(y)) & dir)
draw_barrier(fe, x, y, dir, phase);
}
+ unclip(fe);
+
draw_update(fe, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER);
}
-void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
- game_state *state, float t, float ft)
+static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
+ game_state *state, int dir, game_ui *ui, float t, float ft)
{
- int x, y, tx, ty, frame;
+ int x, y, tx, ty, frame, last_rotate_dir, moved_origin = FALSE;
unsigned char *active;
float angle = 0.0;
/*
- * Clear the screen and draw the exterior barrier lines if this
- * is our first call.
+ * Clear the screen, and draw the exterior barrier lines, if
+ * this is our first call or if the origin has changed.
*/
- if (!ds->started) {
+ if (!ds->started || ui->org_x != ds->org_x || ui->org_y != ds->org_y) {
int phase;
ds->started = TRUE;
WINDOW_OFFSET * 2 + TILE_SIZE * state->width + TILE_BORDER,
WINDOW_OFFSET * 2 + TILE_SIZE * state->height + TILE_BORDER,
COL_BACKGROUND);
+
+ ds->org_x = ui->org_x;
+ ds->org_y = ui->org_y;
+ moved_origin = TRUE;
+
draw_update(fe, 0, 0,
WINDOW_OFFSET*2 + TILE_SIZE*state->width + TILE_BORDER,
WINDOW_OFFSET*2 + TILE_SIZE*state->height + TILE_BORDER);
for (phase = 0; phase < 2; phase++) {
for (x = 0; x < ds->width; x++) {
- if (barrier(state, x, 0) & UL)
- draw_barrier_corner(fe, x, -1, LD, phase);
- if (barrier(state, x, 0) & RU)
- draw_barrier_corner(fe, x, -1, DR, phase);
- if (barrier(state, x, 0) & U)
+ if (x+1 < ds->width) {
+ if (barrier(state, GX(x), GY(0)) & R)
+ draw_barrier_corner(fe, x, -1, +1, +1, phase);
+ if (barrier(state, GX(x), GY(ds->height-1)) & R)
+ draw_barrier_corner(fe, x, ds->height, +1, -1, phase);
+ }
+ if (barrier(state, GX(x), GY(0)) & U) {
+ draw_barrier_corner(fe, x, -1, -1, +1, phase);
+ draw_barrier_corner(fe, x, -1, +1, +1, phase);
draw_barrier(fe, x, -1, D, phase);
- if (barrier(state, x, ds->height-1) & DR)
- draw_barrier_corner(fe, x, ds->height, RU, phase);
- if (barrier(state, x, ds->height-1) & LD)
- draw_barrier_corner(fe, x, ds->height, UL, phase);
- if (barrier(state, x, ds->height-1) & D)
+ }
+ if (barrier(state, GX(x), GY(ds->height-1)) & D) {
+ draw_barrier_corner(fe, x, ds->height, -1, -1, phase);
+ draw_barrier_corner(fe, x, ds->height, +1, -1, phase);
draw_barrier(fe, x, ds->height, U, phase);
+ }
}
for (y = 0; y < ds->height; y++) {
- if (barrier(state, 0, y) & UL)
- draw_barrier_corner(fe, -1, y, RU, phase);
- if (barrier(state, 0, y) & LD)
- draw_barrier_corner(fe, -1, y, DR, phase);
- if (barrier(state, 0, y) & L)
+ if (y+1 < ds->height) {
+ if (barrier(state, GX(0), GY(y)) & D)
+ draw_barrier_corner(fe, -1, y, +1, +1, phase);
+ if (barrier(state, GX(ds->width-1), GY(y)) & D)
+ draw_barrier_corner(fe, ds->width, y, -1, +1, phase);
+ }
+ if (barrier(state, GX(0), GY(y)) & L) {
+ draw_barrier_corner(fe, -1, y, +1, -1, phase);
+ draw_barrier_corner(fe, -1, y, +1, +1, phase);
draw_barrier(fe, -1, y, R, phase);
- if (barrier(state, ds->width-1, y) & RU)
- draw_barrier_corner(fe, ds->width, y, UL, phase);
- if (barrier(state, ds->width-1, y) & DR)
- draw_barrier_corner(fe, ds->width, y, LD, phase);
- if (barrier(state, ds->width-1, y) & R)
+ }
+ if (barrier(state, GX(ds->width-1), GY(y)) & R) {
+ draw_barrier_corner(fe, ds->width, y, -1, -1, phase);
+ draw_barrier_corner(fe, ds->width, y, -1, +1, phase);
draw_barrier(fe, ds->width, y, L, phase);
+ }
}
}
}
tx = ty = -1;
- if (oldstate && (t < ROTATE_TIME)) {
+ last_rotate_dir = dir==-1 ? oldstate->last_rotate_dir :
+ state->last_rotate_dir;
+ if (oldstate && (t < ROTATE_TIME) && last_rotate_dir) {
/*
- * We're animating a tile rotation. Find the turning tile,
- * if any.
+ * We're animating a single tile rotation. Find the turning
+ * tile.
*/
- for (x = 0; x < oldstate->width; x++)
- for (y = 0; y < oldstate->height; y++)
- if ((tile(oldstate, x, y) ^ tile(state, x, y)) & 0xF) {
- tx = x, ty = y;
- goto break_label; /* leave both loops at once */
- }
- break_label:
-
- if (tx >= 0) {
- if (tile(state, tx, ty) == ROT(tile(oldstate, tx, ty),
- state->last_rotate_dir))
- angle = state->last_rotate_dir * 90.0F * (t / ROTATE_TIME);
- else
- angle = state->last_rotate_dir * -90.0F * (t / ROTATE_TIME);
- state = oldstate;
- }
+ tx = (dir==-1 ? oldstate->last_rotate_x : state->last_rotate_x);
+ ty = (dir==-1 ? oldstate->last_rotate_y : state->last_rotate_y);
+ angle = last_rotate_dir * dir * 90.0F * (t / ROTATE_TIME);
+ state = oldstate;
}
-
+
frame = -1;
if (ft > 0) {
/*
/*
* Draw any tile which differs from the way it was last drawn.
*/
- active = compute_active(state);
+ active = compute_active(state, ui->cx, ui->cy);
for (x = 0; x < ds->width; x++)
for (y = 0; y < ds->height; y++) {
- unsigned char c = tile(state, x, y) | index(state, active, x, y);
+ unsigned char c = tile(state, GX(x), GY(y)) |
+ index(state, active, GX(x), GY(y));
+ int is_src = GX(x) == ui->cx && GY(y) == ui->cy;
+ int is_anim = GX(x) == tx && GY(y) == ty;
+ int is_cursor = ui->cur_visible &&
+ GX(x) == ui->cur_x && GY(y) == ui->cur_y;
/*
* In a completion flash, we adjust the LOCKED bit
* the frame number.
*/
if (frame >= 0) {
+ int rcx = RX(ui->cx), rcy = RY(ui->cy);
int xdist, ydist, dist;
- xdist = (x < state->cx ? state->cx - x : x - state->cx);
- ydist = (y < state->cy ? state->cy - y : y - state->cy);
+ xdist = (x < rcx ? rcx - x : x - rcx);
+ ydist = (y < rcy ? rcy - y : y - rcy);
dist = (xdist > ydist ? xdist : ydist);
if (frame >= dist && frame < dist+4) {
}
}
- if (index(state, ds->visible, x, y) != c ||
+ if (moved_origin ||
+ index(state, ds->visible, x, y) != c ||
index(state, ds->visible, x, y) == 0xFF ||
- (x == tx && y == ty)) {
- draw_tile(fe, state, x, y, c,
- (x == tx && y == ty ? angle : 0.0F));
- if (x == tx && y == ty)
+ is_src || is_anim || is_cursor) {
+ draw_tile(fe, state, ds, x, y, c,
+ is_src, (is_anim ? angle : 0.0F), is_cursor);
+ if (is_src || is_anim || is_cursor)
index(state, ds->visible, x, y) = 0xFF;
else
index(state, ds->visible, x, y) = c;
}
}
+ /*
+ * Update the status bar.
+ */
+ {
+ char statusbuf[256];
+ int i, n, n2, a;
+
+ n = state->width * state->height;
+ for (i = a = n2 = 0; i < n; i++) {
+ if (active[i])
+ a++;
+ if (state->tiles[i] & 0xF)
+ n2++;
+ }
+
+ sprintf(statusbuf, "%sActive: %d/%d",
+ (state->used_solve ? "Auto-solved. " :
+ state->completed ? "COMPLETED! " : ""), a, n2);
+
+ status_bar(fe, statusbuf);
+ }
+
sfree(active);
}
-float game_anim_length(game_state *oldstate, game_state *newstate)
+static float game_anim_length(game_state *oldstate,
+ game_state *newstate, int dir, game_ui *ui)
{
- int x, y;
+ int last_rotate_dir;
/*
- * If there's a tile which has been rotated, allow time to
- * animate its rotation.
+ * Don't animate an auto-solve move.
*/
- for (x = 0; x < oldstate->width; x++)
- for (y = 0; y < oldstate->height; y++)
- if ((tile(oldstate, x, y) ^ tile(newstate, x, y)) & 0xF) {
- return ROTATE_TIME;
- }
+ if ((dir > 0 && newstate->just_used_solve) ||
+ (dir < 0 && oldstate->just_used_solve))
+ return 0.0F;
+
+ /*
+ * Don't animate if last_rotate_dir is zero.
+ */
+ last_rotate_dir = dir==-1 ? oldstate->last_rotate_dir :
+ newstate->last_rotate_dir;
+ if (last_rotate_dir)
+ return ROTATE_TIME;
return 0.0F;
}
-float game_flash_length(game_state *oldstate, game_state *newstate)
+static float game_flash_length(game_state *oldstate,
+ game_state *newstate, int dir, game_ui *ui)
{
/*
* If the game has just been completed, we display a completion
* flash.
*/
- if (!oldstate->completed && newstate->completed) {
- int size;
- size = 0;
- if (size < newstate->cx+1)
- size = newstate->cx+1;
- if (size < newstate->cy+1)
- size = newstate->cy+1;
- if (size < newstate->width - newstate->cx)
- size = newstate->width - newstate->cx;
- if (size < newstate->height - newstate->cy)
- size = newstate->height - newstate->cy;
+ if (!oldstate->completed && newstate->completed &&
+ !oldstate->used_solve && !newstate->used_solve) {
+ int size = 0;
+ if (size < newstate->width)
+ size = newstate->width;
+ if (size < newstate->height)
+ size = newstate->height;
return FLASH_FRAME * (size+4);
}
return 0.0F;
}
+
+static int game_wants_statusbar(void)
+{
+ return TRUE;
+}
+
+#ifdef COMBINED
+#define thegame net
+#endif
+
+const struct game thegame = {
+ "Net", "games.net",
+ default_params,
+ game_fetch_preset,
+ decode_params,
+ encode_params,
+ free_params,
+ dup_params,
+ TRUE, game_configure, custom_params,
+ validate_params,
+ new_game_desc,
+ game_free_aux_info,
+ validate_desc,
+ new_game,
+ dup_game,
+ free_game,
+ TRUE, solve_game,
+ FALSE, game_text_format,
+ new_ui,
+ free_ui,
+ make_move,
+ game_size,
+ game_colours,
+ game_new_drawstate,
+ game_free_drawstate,
+ game_redraw,
+ game_anim_length,
+ game_flash_length,
+ game_wants_statusbar,
+};