return own[x][y] == -1 && xzone_ok(c, x, y);
}
-static int
-try_to_grow(int c, int newx, int newy, int extra_dist)
+static void
+add_sector(int c, int x, int y)
{
- int d = c < nc ? di : id;
- int i, px, py;
- struct hexagon_iter hexit;
+ assert(own[x][y] == -1);
+ xzone_around_sector(c, x, y, c < nc ? di : DISTINCT_ISLANDS ? id : 0);
+ sectx[c][isecs[c]] = x;
+ secty[c][isecs[c]] = y;
+ isecs[c]++;
+ own[x][y] = c;
+}
+static int
+try_to_grow(int c, int newx, int newy)
+{
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));
- }
-
- xzone_around_sector(c, newx, newy,
- c < nc ? di : DISTINCT_ISLANDS ? id : 0);
- sectx[c][isecs[c]] = newx;
- secty[c][isecs[c]] = newy;
- isecs[c]++;
- own[newx][newy] = c;
+ 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;
}
GROW THE ISLANDS
****************************************************************************/
-/* Choose a place to start growing an island from
-*/
+/*
+ * Place additional island @c's first sector.
+ * Return 1 on success, 0 on error.
+ */
static int
-place_island(int c, int *xp, int *yp)
+place_island(int c)
{
- int d, sx, sy;
- int ssy = roll0(WORLD_Y);
- int ssx = new_x(roll0(WORLD_X / 2) * 2 + ssy % 2);
-
- if (ssx > WORLD_X - 2)
- ssx = new_x(ssx + 2);
- for (d = di; d >= 0; --d) {
- sx = ssx;
- sy = ssy;
- *xp = new_x(sx + 2);
- for (*yp = sy; *xp != sx || *yp != sy; *xp += 2) {
- if (*xp >= WORLD_X) {
- *yp = new_y(*yp + 1);
- *xp = *yp % 2;
- if (*xp == sx && *yp == sy)
- break;
+ int n, x, y, newx, newy;
+
+ n = 0;
+
+ for (y = 0; y < WORLD_Y; y++) {
+ for (x = y % 2; x < WORLD_X; x += 2) {
+ if (can_grow_at(c, x, y)) {
+ n++;
+ if (!roll0(n)) {
+ newx = x;
+ newy = y;
+ }
}
- if (try_to_grow(c, *xp, *yp, d))
- return 1;
}
}
- return 0;
+
+ if (n)
+ add_sector(c, newx, newy);
+ return n;
}
/* Grow all the islands
grow_islands(void)
{
int stunted_islands = 0;
- int c, secs, x, y, isiz;
+ int c, secs, isiz;
xzone_init(nc);
for (c = nc; c < nc + ni; ++c) {
- if (!place_island(c, &x, &y)) {
+ if (!place_island(c)) {
qprint("\nNo room for island #%d", c - nc + 1);
break;
}