fairland: Correct island placement bias
A sector is admissible for island placement when land can be grown in every sector within a certain distance. place_island() picks a random start sector, then searches linearly for an admissible sector. If it finds one, it places the island there. Else, it reduces the distance by one and tries again. It fails when none is found even for distance zero. Trying with extra distance is obviously meant to reduce the risk of islands from running into each other without need. Initial distance is @di, the minimum distance between continents, which doesn't really make sense, and is undocumented. Bug: place_island() never tries the start sector. Bias: placement probability is higher for sectors immediately following inadmissible sectors. Because of that, islands are more often placed to the right of existing islands. Players could exploit that to guide their search for land. Rewrite place_island() to pick sectors with equal probability, dropping the undocumented extra distance feature. If it's missed, we can bring it back. The new code visits each sector once. The old code visits only one sector in the best case, but each sector several times in the worst case. fairland performance improves measurably for crowded setups with large @di, else it suffers. For instance, Hvy Fever example given in the commit before previous runs seven times faster for me. With @di reduced to 2, its run time more than doubles. Not that it matters. Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
parent
001674e5c5
commit
eecb9c9825
9 changed files with 3661 additions and 3658 deletions
|
@ -766,6 +766,17 @@ can_grow_at(int c, int x, int y)
|
|||
return own[x][y] == -1 && xzone_ok(c, x, y);
|
||||
}
|
||||
|
||||
static void
|
||||
add_sector(int c, int x, int y)
|
||||
{
|
||||
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, int extra_dist)
|
||||
{
|
||||
|
@ -786,12 +797,7 @@ try_to_grow(int c, int newx, int newy, int extra_dist)
|
|||
} 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;
|
||||
}
|
||||
|
||||
|
@ -943,28 +949,25 @@ grow_continents(void)
|
|||
static int
|
||||
place_island(int c)
|
||||
{
|
||||
int d, sx, sy, x, y;
|
||||
int ssy = roll0(WORLD_Y);
|
||||
int ssx = new_x(roll0(WORLD_X / 2) * 2 + ssy % 2);
|
||||
int n, x, y, newx, newy;
|
||||
|
||||
if (ssx > WORLD_X - 2)
|
||||
ssx = new_x(ssx + 2);
|
||||
for (d = di; d >= 0; --d) {
|
||||
sx = ssx;
|
||||
sy = ssy;
|
||||
x = new_x(sx + 2);
|
||||
for (y = sy; x != sx || y != sy; x += 2) {
|
||||
if (x >= WORLD_X) {
|
||||
y = new_y(y + 1);
|
||||
x = y % 2;
|
||||
if (x == sx && y == sy)
|
||||
break;
|
||||
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, x, y, d))
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
if (n)
|
||||
add_sector(c, newx, newy);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* Grow all the islands
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue