static int **own; /* owner of the sector. -1 means water */
+/*
+ * Adjacent land sectors
+ * adj_land[XYOFFSET(x, y)] bit d is set exactly when the sector next
+ * to x, y in direction d is land.
+ */
+static unsigned char *adj_land;
+
/*
* Exclusive zones
* Each island is surrounded by an exclusive zone where only it may
capx = calloc(nc, sizeof(int));
capy = calloc(nc, sizeof(int));
own = calloc(WORLD_X, sizeof(int *));
+ adj_land = malloc(WORLD_SZ() * sizeof(*adj_land));
xzone = malloc(WORLD_SZ() * sizeof(*xzone));
elev = calloc(WORLD_X, sizeof(int *));
for (i = 0; i < WORLD_X; ++i) {
own[i][j] = -1;
}
}
+ memset(adj_land, 0, WORLD_SZ() * sizeof(*adj_land));
}
/****************************************************************************
return own[x][y] == -1 && xzone_ok(c, x, y);
}
+static void
+adj_land_update(int x, int y)
+{
+ int dir, nx, ny, noff;
+
+ assert(own[x][y] != -1);
+
+ for (dir = DIR_FIRST; dir <= DIR_LAST; dir++) {
+ nx = new_x(x + diroff[dir][0]);
+ ny = new_y(y + diroff[dir][1]);
+ noff = XYOFFSET(nx, ny);
+ adj_land[noff] |= 1u << DIR_BACK(dir);
+ }
+}
+
static void
add_sector(int c, int x, int y)
{
secty[c][isecs[c]] = y;
isecs[c]++;
own[x][y] = c;
+ adj_land_update(x, y);
}
static int
-try_to_grow(int c, int newx, int newy, int extra_dist)
+try_to_grow(int c, int newx, int newy)
{
- int d = c < nc ? di : id;
- int i, px, py;
- struct hexagon_iter hexit;
-
if (!can_grow_at(c, newx, newy))
return 0;
- for (i = 1; i <= extra_dist; i++) {
- hexagon_first(&hexit, newx, newy, d + i, &px, &py);
- do {
- if (own[px][py] != -1 &&
- own[px][py] != c &&
- (DISTINCT_ISLANDS || own[px][py] < nc))
- return 0;
- } while (hexagon_next(&hexit, &px, &py));
- }
-
add_sector(c, newx, newy);
return 1;
}
if (n > 5 ||
(own[new_x(x+dirx[(i+5)%6])][new_y(y+diry[(i+5)%6])] == -1 &&
own[new_x(x+dirx[(i+1)%6])][new_y(y+diry[(i+1)%6])] == -1))
- if (try_to_grow(c, newx, newy, 0))
+ if (try_to_grow(c, newx, newy))
done = 1;
}
} else
for (i = roll0(6), n = 0; n < 6 && !done; i = (i + 1) % 6, ++n) {
newx = new_x(x + dirx[i]);
newy = new_y(y + diry[i]);
- if (try_to_grow(c, newx, newy, 0))
+ if (try_to_grow(c, newx, newy))
done = 1;
}
next_coast(c, x, y, &x, &y);
for (c = 0; c < nc; ++c) {
isecs[c] = 0;
- if (!try_to_grow(c, capx[c], capy[c], 0)
- || !try_to_grow(c, new_x(capx[c] + 2), capy[c], 0)) {
+ if (!try_to_grow(c, capx[c], capy[c])
+ || !try_to_grow(c, new_x(capx[c] + 2), capy[c])) {
done = 0;
continue;
}