Land elevation is computed by first placing mountains, then elevating
land to fit.
Mountains placement is a weighted random sampling. A sector's weight
is its distance to sea capped at five and squared.
Non-mountain, non-capital sectors get assigned equidistant elevations
from 1 up to 97 rounded to integer in an order that depends on
distance to mountain and distance to sea, both capped at 5. Mountains
get equidistant elevations starting at 98 (see recent commit
"fairland: Fair mountain resources"). Capitals get 36.
Sea elevation is randomly chosen from a range that depends on the
sector's distance to land. The range increases from [-27,-1] next to
land to [-127,-1] for distance 5 and up.
Without mountains, the result is boring: elevation increases pretty
much linearly with the distance from the coast, i.e. each islands is a
single cone. With mountains, we get one cone per mountain. The sea
sea elevations are basically noise.
Worse, it's buggy: mountain placement can place fewer mountains than
requested. The weighted random sampling assigns weights even to
sectors that cannot be picked (capitals and sectors that have been
picked already). When the dice land on such a sector, it instead
picks the next one (in island growth order) that can be picked. If
there is no next one, the mountain is not placed. This is a fairness
issue.
Impact varies with fairland parameters. For 60 sector islands with 5%
mountains, around one in 10000 islands is short one mountain in my
testing. For 10 sector islands with 50% mountains, some 760 out of
10000 islands are short one sector, 80 short two, and five short
three. The numbers get worse as spikiness increases.
Undocumented misfeature: the placement loop is limited to 1000
iterations, supposedly to catch a runaway loop. Since the loop
iterates exactly once per mountain, all this accomplishes it limiting
mountains to 1000 per island.
When too few mountains are placed, the loop assigning elevations to
non-mountain, non-capital decrements elevations below 1. Since
Chainsaw 3, such elevations then get mapped to 1, plastering over the
bug. We get non-mountain sectors with elevation 1 instead of
mountains.
Rewrite as follows.
Use a simple random hill algorithm to assign raw elevations:
initialize elevation to zero, then randomly raise circular hills on
land / lower circular depressions at sea. Their size and height
depends on the distance to the coast, capped at 3. After a sufficient
number of iterations, the resulting terrain has a "natural" look.
This is elevate_prep().
elevate_land() then sorts non-capital sectors by raw elevation. The
highest become mountains. Sectors get assigned the same equidistant
elevations as before, just in raw elevation order.
elevate_sea() simply normalizes raw elevation to [-127,-1].
Elevations now show a nice undulating pattern independent of mountain
percentage.
Implementation detail: replace elev[x][y] by elev[XYOFFSET(x, y)], and
narrow the element type from int to short. Takes a fourth the space.
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
When an island gets placed too close to the edge of the sphere of
influence, its side facing the edge will likely be formed by the edge.
Looks unnatural, and can give a clue on the location of the other
continent.
Make place_island() prefer sectors away from the edge: instead of
picking one of the admissible sectors with equal probability, reduce
the probability as we get closer to the edge.
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
Planned island sizes are random with an expected value that matches
the average size requested by the user. Can be off quite a bit when
the number of islands is small. Also, actual island size can be
smaller than planned size when space is tight.
Instead of picking random island sizes independently, pick a random
split of their requested total size.
To reduce the probability of islands not growing to their planned
size, grow large islands before smaller ones.
To compensate for inability to grow, carry the difference over to the
next island size.
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
The previous commit reduced the difference in island size within the
same batch of islands to at most one. Eliminate the remaining
difference by shrinking the bigger islands by one sector.
This invalidates the precomputed exclusive zones, so recompute them.
fairland-test needs a tweak to avoid loss of test coverage.
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
The previous commits changed grow_island() to create islands in
batches consisting of one island per continent, all of the same
planned size. grow_island() still places and grows one island after
the other. When an island can't grow to the actual size, the others
in the same batch are not affected. Island size can therefore differ
a lot within the same batch.
Change grow_island() to interleave the work on a batch's island: first
place them all, then add one sector to each in turn. Stop after all
reached the planned size, or one or more could not be grown further.
This is similar to how we grow continents: drift() places them all,
and grow_continent() adds one sector to each continent in turn.
Island size within the same batch can now differ at most by one
sector. The next commit will eliminate that remaining difference.
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
The previous two commits put the same number of islands closest to
each continent. This one makes their planned sizes match: instead of
rolling dice separately for each island's size, we roll dice only for
the first continent's islands. The other continent's islands get the
same sizes.
Actual island sizes still differ when islands can't be grown to their
planned size. To be addressed next.
fairland-test needs a tweak to avoid loss of test coverage.
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
fairland places islands of random size in random places, subject to
minimum distances. Results are often less than fair, in particular
when the number of islands per continent is low: some continents have
more land nearby than others. Increasing distances between islands
doesn't help much. Deities commonly run fairland until they find the
result acceptable.
The next few commits will tackle this issue. As a first step, this
one places islands closest to continents in turn, so that each
continent is closest to the same number of islands. A continent is
closest to an island when it is closest to each of its sectors.
The number of islands must be a multiple of the number of continents
now.
Since fairland may be unable to place all islands, a continent may
still get fewer islands than it should. The next commit will address
that.
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
grow_one_sector() picks a coastal start sector, then moves along the
coast trying to add an adjacent sector to the island.
It operates in spiking mode with a chance of @sp percent.
When spiking, the neighbors with sea on both sides are preferred. For
instance, when the area around the sector being considered looks like
this
- .
- - .
- .
then the neighbor in direction j is preferred, because it has sea in
directions u and n. No other neighbor is preferred.
The start sector is the first coastal sector found in a linear search
in growth order, starting at the last sector grown when spiking, or
else at a random sector. This is new_try().
grow_one_sector() tries adding a neighbor in clockwise order, starting
with a random direction. When spiking, it goes around the clock two
times, trying only preferred sectors in the first round.
When it can't add a neighbor, it moves to the next coastal sector with
next_coast().
Taken together, this randomly picks one element from the set of
pairs (S, D) where the sector in direction D off base sector S can be
added to the island. How does the probability distribution look like?
Bias: a sector's probability to be added to the island land increases
with the number of base sectors it is adjacent to. This tends to fill
in bays and lakes, which is fine.
Bias: coastal sectors following runs of non-coastal ones are more
likely to be picked as start sector. Perhaps less of an issue when
spiking, where the pick is not intended to be random.
Bias: a pair (S, D) is more likely to be picked when base sector S
follows a run of coastal sectors that aren't base sectors, or
direction D follows a a run of directions that don't permit growth.
The impact of these two biases is not obvious. I suspect they are the
reason behind the tendency of large islands to curve around obstacles
in a counterclockwise direction. This can result in multiple islands
wrapping around a central one like layers of an onion.
Bug: the move along the coast is broken. next_coast() searches for
the first adjacent sea in clockwise order, starting in direction g,
then from there clockwise for a sector belonging to the island.
Amazingly, this does move along the coast in a clockwise direction.
It can get caught in a loop, though. Consider this island:
-
- - -
-
If we start at the central sector (marked 0), the search along the
coast progresses like this:
1
- 0 2
-
It reaches the central sector again after three moves (to 1, to 2,
back to 0), and stops without having reached the two sectors on the
left.
If we start with the leftmost sector, the search loops: 0, 1, 2, 3, 1,
...
2
0 1 3
-
grow_one_sector() ensures termination by giving up after 200 moves.
Nuts!
Because of this, grow_one_sector() can fail when it shouldn't, either
because the search along the coast stops early, or goes into a loop,
or simply because there's too much coast. The latter should not
happen in common usage, where island sizes are in the tens, not the
hundreds.
Clean up this hot mess by rewriting grow_one_sector() to pick a sector
adjacent to the island with a clearly defined probability, as follows.
Use weighted random sampling to pick one sector from the set of
possible adjacent sectors.
When spiking, a sector's weight increases with number of adjacent sea
sectors. This directs the growth away from land, resulting in spikes.
When not spiking, the weight increases with the number of adjacent
land sectors. This makes the island more rounded.
To visit the adjacent sectors, grow_one_sector() iterates over the
neighbors of all island sectors, skipping neighbors that have been
visited already.
This produces comparable results for low spike percentages. The weird
onions are gone, though.
Noticeable differences emerge as the spike percentage grows. Whereas
the old one produces long snakes that tend to curve into themselves,
the new one produces shorter spikes extending from a core, a bit like
tentacles. Moreover, islands are more centered on their first sector
now. The probability for coastal capitals is lower, in particular for
moderate spike percentages.
I consider this improvements.
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
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>
"fairland: unstable drift -- try increasing DRIFT_MAX" is confusing:
it looks like an error, but isn't, and increasing DRIFT_MAX requires a
recompile. I'm not sure it can happen. Replace by just "unstable
drift".
"fairland: error -- continent %c had no room to grow!" is pretty
redundant: it's always followed by "Only managed to grow %d out of %d
sectors." and then "ERROR: World not large enough to hold
continents". All it adds is which of the continents failed to grow,
and that's not actionable. Drop the message.
The message sequence "designating sectors...", "adding resources...",
"setting coastal flags...", and "writing to sectors file..." is a bit
of a lie as these four tasks aren't actually done one after the other.
Replace by just "writing to sectors file..."
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
Rename the existing fairland run to "plain".
New run "stunted" to cover larger minimal distances, islands
that can't fully grow, and islands that can't be placed.
New run "no-spike" to cover 0% spike.
New run "spike" to cover high spike percentage, mountains and -i.
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>