Meet river banks at midline

- End a tidal bank halfway to any other water within its band
- Ease it up to that water's own bank height, so the two sides join
- Leave river, pond and lake banks unchanged
This commit is contained in:
Brian Fopiano committed 2026-11-03 19:50:32 -05:00
1 parent f4cd64ee33
commit 8d18e0de7a
3 files changed
+140 -20

No files matched your search

@@ -43,6 +43,7 @@ final class RasterComposer {
private final HydrologyTier[] nearTier = new HydrologyTier[CELLS];
private final ReachGeometry.Lookup brinks;
private final ReachCursor cursor;
private final ReachCursor.Choice other = new ReachCursor.Choice();
private final Map<Long, String> reachFluids = new HashMap<>();
private NaturalHeights naturalHeight;
@@ -165,26 +166,6 @@ final class RasterComposer {
}
private void dry(int cell, int x, int z, int ground, double variation) {
int cut = ground;
int raise = Integer.MIN_VALUE;
double bankEdge = Double.POSITIVE_INFINITY;
ReachCursor.Choice bank = cursor.bank;
if (bank.reach != null) {
HydrologyTier bankTier = bank.reach.tier();
int surface = brinks.head(bank);
bankEdge = bank.distance - bank.halfWidth();
double u = bankEdge / bankTier.bankBand();
if (bankTier.bankStyle() != IrisBankStyle.CLIFF && bankTier.bankStyle() != IrisBankStyle.TERRACED) {
u = BedProfile.position(u, -variation);
}
if (ground >= surface) {
double slope = bank.reach.style()[0] == ReachStyle.TIDAL ? settings.deformation().bankSlope() : coneSlope(steepestToWater(x, z, bank, bankEdge));
double cone = slope * u * bankTier.bankBand();
cut = BankProfile.cut(bankTier.bankStyle(), surface, ground, u, cone, noise(x, z));
} else if (ground >= settings.seaLevel()) {
raise = BankProfile.levee(bankTier.bankStyle(), surface, ground, u, noise(x, z), settings.deformation().maxFill());
}
}
int nearPond = -1;
double pondEdge = Double.POSITIVE_INFINITY;
for (int index = 0; index < features.ponds(); index++) {
@@ -194,6 +175,29 @@ final class RasterComposer {
nearPond = index;
}
}
int cut = ground;
int raise = Integer.MIN_VALUE;
double bankEdge = Double.POSITIVE_INFINITY;
ReachCursor.Choice bank = cursor.bank;
if (bank.reach != null) {
bankEdge = bank.distance - bank.halfWidth();
if (bank.reach.style()[0] == ReachStyle.TIDAL) {
cut = tidalBank(bank, x, z, ground, variation, bankEdge, pondEdge);
} else {
HydrologyTier bankTier = bank.reach.tier();
int surface = brinks.head(bank);
double u = bankEdge / bankTier.bankBand();
if (bankTier.bankStyle() != IrisBankStyle.CLIFF && bankTier.bankStyle() != IrisBankStyle.TERRACED) {
u = BedProfile.position(u, -variation);
}
if (ground >= surface) {
double cone = coneSlope(steepestToWater(x, z, bank, bankEdge)) * u * bankTier.bankBand();
cut = BankProfile.cut(bankTier.bankStyle(), surface, ground, u, cone, noise(x, z));
} else if (ground >= settings.seaLevel()) {
raise = BankProfile.levee(bankTier.bankStyle(), surface, ground, u, noise(x, z), settings.deformation().maxFill());
}
}
}
int taper = settings.ponds().shore().taper();
boolean tapered = nearPond >= 0 && pondEdge <= taper;
if (tapered && ground > WaterHeads.standing(features.pond(nearPond).level(), settings.seaLevel())) {
@@ -208,6 +212,47 @@ final class RasterComposer {
nearestWater(cell, nearPond, pondEdge);
}
private int tidalBank(ReachCursor.Choice bank, int x, int z, int ground, double variation, double bankEdge, double pondEdge) {
HydrologyTier tidal = bank.reach.tier();
int surface = brinks.head(bank);
if (ground < surface) {
return ground;
}
double band = tidal.bankBand();
features.segments().nearestOther(x, z, bank.reach.id(), band, other);
double otherEdge = other.reach == null ? Double.POSITIVE_INFINITY : other.distance - other.halfWidth();
int meet = ground;
if (other.reach != null && otherEdge <= other.reach.tier().bankBand()) {
meet = Math.min(meet, otherBank(x, z, ground, variation, otherEdge));
}
double nearest = Math.min(otherEdge, pondEdge);
double reach = nearest == Double.POSITIVE_INFINITY ? band : Math.min(band, 0.5D * (bankEdge + nearest));
if (bankEdge >= reach) {
return Math.max(surface, meet);
}
double u = bankEdge / reach;
if (tidal.bankStyle() != IrisBankStyle.CLIFF && tidal.bankStyle() != IrisBankStyle.TERRACED) {
u = BedProfile.position(u, -variation);
}
double cone = settings.deformation().bankSlope() * u * reach;
return Math.min(ground, BankProfile.cut(tidal.bankStyle(), surface, Math.max(surface, meet), u, cone, noise(x, z)));
}
private int otherBank(int x, int z, int ground, double variation, double otherEdge) {
HydrologyTier otherTier = other.reach.tier();
int surface = brinks.head(other);
double u = otherEdge / otherTier.bankBand();
if (otherTier.bankStyle() != IrisBankStyle.CLIFF && otherTier.bankStyle() != IrisBankStyle.TERRACED) {
u = BedProfile.position(u, -variation);
}
if (ground < surface) {
return ground;
}
boolean tidal = other.reach.style()[0] == ReachStyle.TIDAL;
double slope = tidal ? settings.deformation().bankSlope() : coneSlope(steepestToWater(x, z, other, otherEdge));
return BankProfile.cut(otherTier.bankStyle(), surface, ground, u, slope * u * otherTier.bankBand(), noise(x, z));
}
private void nearestWater(int cell, int nearPond, double pondEdge) {
double best = Double.POSITIVE_INFINITY;
if (cursor.edge.reach != null) {
@@ -141,6 +141,33 @@ final class ReachSegments {
cursor.resolveChannel();
}
void nearestOther(double x, double z, long excluded, double reach, ReachCursor.Choice out) {
if (!sealed) {
seal();
}
out.reset();
double best = Double.POSITIVE_INFINITY;
for (int index = 0; index < size; index++) {
if (reaches[index].id() == excluded || beyond(x, z, Math.min(startX[index], endX[index]), Math.min(startZ[index], endZ[index]),
Math.max(startX[index], endX[index]), Math.max(startZ[index], endZ[index]), radius[index] + reach)) {
continue;
}
double ax = startX[index];
double az = startZ[index];
double dx = endX[index] - ax;
double dz = endZ[index] - az;
double length = dx * dx + dz * dz;
double t = length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D);
double distance = Math.hypot(x - (ax + t * dx), z - (az + t * dz));
double edge = distance - (startWidth[index] + t * (endWidth[index] - startWidth[index]));
if (edge < best && edge <= reach) {
best = edge;
out.reset();
out.offer(reaches[index], segments[index], index, t, distance);
}
}
}
private int coveredCell(double x, double z) {
if (coverStarts == null) {
return -1;
@@ -414,6 +414,54 @@ public class ChunkWaterRasterTest {
}
}
private static Reach straight(long id, HydrologyTier tier, float z, int head, ReachStyle style) {
int points = 9;
float[] xs = new float[points];
float[] zs = new float[points];
int[] grade = new int[points];
float[] halfWidth = new float[points];
float[] depth = new float[points];
ReachStyle[] styles = new ReachStyle[points];
for (int point = 0; point < points; point++) {
xs[point] = -16f + point * 8f;
zs[point] = z;
grade[point] = head;
halfWidth[point] = 3f;
depth[point] = 2f;
styles[point] = style;
}
return new Reach(id, tier, 0L, xs, zs, grade, halfWidth, depth, styles, 30000f);
}
private static ChunkWaterRaster rasterOf(HydrologySettings settings, List<Reach> reaches, int ground) {
WindowKey key = new WindowKey(0, 0);
IndexedWindow own = new IndexedWindow(new WindowPlan(key, reaches, List.of()), ReachIndex.build(key, reaches, List.of()));
return new ChunkRasterizer(settings, ChannelFluids.TIER).rasterize(0, 0, (x, z) -> ground, List.of(own), regions(0, 0, List.of()));
}
@Test
public void tidalBanksMeetANeighbouringRiverBankWithoutACliff() {
HydrologySettings settings = settings(new IrisHydrology());
int ground = SEA_LEVEL + 32;
Reach arm = straight(71L, settings.tidalTier(), -6f, SEA_LEVEL, ReachStyle.TIDAL);
for (int riverHead : new int[]{SEA_LEVEL, SEA_LEVEL + 24}) {
for (HydrologyTier tier : settings.tiers()) {
Reach river = straight(72L, tier, 22f, riverHead, ReachStyle.RUN);
ChunkWaterRaster raster = rasterOf(settings, List.of(arm, river), ground);
ChunkWaterRaster alone = rasterOf(settings, List.of(river), ground);
for (int lx = 0; lx < 16; lx++) {
for (int lz = 0; lz < 15; lz++) {
int step = Math.abs(raster.terrainHeight(lx, lz + 1) - raster.terrainHeight(lx, lz));
assertTrue(tier.name() + " head " + riverHead + " steps " + step + " at " + lx + "," + lz, step <= 8);
}
for (int lz = 9; lz < 16; lz++) {
assertEquals("the river side is the river's own bank at " + lx + "," + lz, alone.terrainHeight(lx, lz), raster.terrainHeight(lx, lz));
}
}
}
}
}
@Test
public void tidalBanksFollowTheBandInsteadOfCuttingRaysToSteepGround() {
HydrologySettings settings = settings(new IrisHydrology());