Lower rivers toward the sea

- Add SeaApproach: lowers the last stretch above an estuary in small steps after planning
- Plan arms and grottos against the network before the approach
- Keep inland falls, high coasts and water beside ponds
- Add mouthProbe with sea drop metrics, sections and reach digests
This commit is contained in:
Brian Fopiano committed 2026-11-04 01:11:51 -05:00
1 parent 343da05e3d
commit 0a863e6ed9
8 files changed
+1173 -5

No files matched your search

@@ -12,6 +12,7 @@ import art.arcane.iris.generation.hydrology.grid.HeightLattice;
import art.arcane.iris.generation.hydrology.grid.HydrologySampleBuffer;
import art.arcane.iris.generation.hydrology.grid.PriorityFlood;
import art.arcane.iris.generation.hydrology.raster.PondChannelSettlement;
import art.arcane.iris.generation.hydrology.raster.NaturalHeights;
import art.arcane.iris.generation.hydrology.raster.TongueGuard;
import art.arcane.iris.generation.hydrology.underground.Grotto;
import art.arcane.iris.generation.hydrology.underground.GrottoPlanner;
@@ -21,6 +22,7 @@ import art.arcane.iris.generation.hydrology.water.PondKind;
import art.arcane.iris.generation.hydrology.water.PondPlanner;
import art.arcane.iris.generation.hydrology.water.Reach;
import art.arcane.iris.generation.hydrology.water.ReachShaper;
import art.arcane.iris.generation.hydrology.water.SeaApproach;
import art.arcane.iris.generation.hydrology.water.ReachStyle;
import art.arcane.iris.generation.hydrology.water.ShapedNetwork;
import art.arcane.iris.generation.hydrology.water.TidalPlanner;
@@ -132,7 +134,7 @@ public final class WindowPlanner {
network = PondChannelSettlement.settle(key, guarded(shaper.shape(key, drainage.surface(), drainage.receivers(),
drainage.accumulation(), drainage.channels(), samples.ocean(), water, fine, settings.seed())), settings.seed(), settings.seaLevel());
}
List<Reach> reaches = network.reaches();
List<Reach> reaches = guarded(new ShapedNetwork(SeaApproach.apply(network.reaches(), network.ponds(), columnHeights(), settings.seaLevel()), network.ponds())).reaches();
List<Pond> windowPonds = windowPonds(network.ponds());
List<Reach> ownedReaches = new ArrayList<>(ownedReaches(key, reaches));
List<Reach> arms = tidal.plan(key, natural, samples, network);
@@ -144,15 +146,19 @@ public final class WindowPlanner {
}
private ShapedNetwork guarded(ShapedNetwork shaped) {
return TongueGuard.guard(shaped, columnHeights(), settings.seed(), settings.seaLevel(), settings.deformation().maxFill());
}
private NaturalHeights columnHeights() {
HydrologySampleBuffer column = new HydrologySampleBuffer(1);
int[] columnX = new int[1];
int[] columnZ = new int[1];
return TongueGuard.guard(shaped, (x, z) -> {
return (x, z) -> {
columnX[0] = x;
columnZ[0] = z;
fine.sampleColumns(columnX, columnZ, column);
return column.height()[0];
}, settings.seed(), settings.seaLevel(), settings.deformation().maxFill());
};
}
public WindowPlan plan(WindowKey key, List<RegionPlan> regions) {
@@ -0,0 +1,219 @@
package art.arcane.iris.generation.hydrology.water;
import art.arcane.iris.generation.hydrology.raster.NaturalHeights;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
public final class SeaApproach {
static final float GENTLE_SLOPE = 0.125f;
static final float STEEPEST_SLOPE = 0.25f;
static final float LENGTH = 128f;
static final float FIRST_STEP = 12f;
static final float COAST_REACH = 64f;
static final int CLIFF_HEIGHT = 16;
static final float CLIFF_BANK_REACH = 16f;
private final Map<Long, List<Reach>> feeders = new HashMap<>();
private final Map<Reach, int[]> lowered = new IdentityHashMap<>();
private final List<Pond> ponds;
private final NaturalHeights natural;
private final int seaLevel;
private int base;
private float slope;
private float firstStep;
private SeaApproach(List<Reach> reaches, List<Pond> ponds, NaturalHeights natural, int seaLevel) {
this.ponds = ponds;
this.natural = natural;
this.seaLevel = seaLevel;
for (Reach reach : reaches) {
if (reach.downstreamId() != 0L) {
feeders.computeIfAbsent(reach.downstreamId(), id -> new ArrayList<>()).add(reach);
}
}
}
public static List<Reach> apply(List<Reach> reaches, List<Pond> ponds, NaturalHeights natural, int seaLevel) {
SeaApproach approach = new SeaApproach(reaches, ponds, natural, seaLevel);
for (Reach reach : reaches) {
int last = reach.style().length - 1;
if (reach.downstreamId() == 0L && reach.style()[last] == ReachStyle.MOUTH) {
approach.lowerToward(reach);
}
}
if (approach.lowered.isEmpty()) {
return reaches;
}
List<Reach> result = new ArrayList<>(reaches.size());
for (Reach reach : reaches) {
int[] grade = approach.lowered.get(reach);
result.add(grade == null ? reach : rebuilt(reach, grade, seaLevel));
}
return result;
}
private void lowerToward(Reach mouth) {
int start = estuaryStart(mouth);
if (highCoast(mouth, start)) {
return;
}
float width = mouth.halfWidth()[start];
base = seaLevel + mouth.tier().stepHeight();
firstStep = Math.max(FIRST_STEP, width);
slope = slope(mouth, start, Math.min(GENTLE_SLOPE, 1f / Math.max(1f, width)));
walk(mouth, start, 0f, mouth.grade()[start]);
}
private static int estuaryStart(Reach mouth) {
int start = mouth.style().length - 1;
while (start > 0 && mouth.style()[start - 1] == ReachStyle.MOUTH) {
start--;
}
return start;
}
private boolean highCoast(Reach mouth, int start) {
Reach reach = mouth;
int point = start - 1;
if (start == 0) {
reach = largestFeeder(mouth);
if (reach == null) {
return false;
}
point = reach.xs().length - 2;
}
if (point < 0) {
return false;
}
int next = Math.min(reach.xs().length - 1, point + 1);
float dx = reach.xs()[next] - reach.xs()[point];
float dz = reach.zs()[next] - reach.zs()[point];
float span = Math.max(1e-3f, (float) Math.hypot(dx, dz));
return bankPeak(reach, point, -dz / span, dx / span) - seaLevel >= CLIFF_HEIGHT
&& bankPeak(reach, point, dz / span, -dx / span) - seaLevel >= CLIFF_HEIGHT;
}
private double bankPeak(Reach reach, int point, float nx, float nz) {
double peak = Double.NEGATIVE_INFINITY;
float half = reach.halfWidth()[point];
for (float out = half + 2f; out <= half + CLIFF_BANK_REACH; out += 2f) {
peak = Math.max(peak, natural.height((int) Math.floor(reach.xs()[point] + nx * out), (int) Math.floor(reach.zs()[point] + nz * out)));
}
return peak;
}
private float slope(Reach mouth, int start, float gentle) {
float rise = Float.POSITIVE_INFINITY;
Reach reach = mouth;
int point = start;
float along = 0f;
int below = mouth.grade()[start];
while (along <= LENGTH) {
if (point == 0) {
reach = largestFeeder(reach);
if (reach == null) {
break;
}
point = reach.xs().length - 1;
continue;
}
along += distance(reach, point - 1, point);
point--;
int level = reach.grade()[point];
if (stops(reach, point, along, level, below)) {
break;
}
if (along > COAST_REACH) {
rise = Math.min(rise, (level - base) / Math.max(1f, along - firstStep));
}
below = level;
}
return rise == Float.POSITIVE_INFINITY ? gentle : Math.clamp(rise, gentle, STEEPEST_SLOPE);
}
private void walk(Reach reach, int from, float start, int firstBelow) {
float along = start;
int below = firstBelow;
for (int point = from - 1; point >= 0; point--) {
along += distance(reach, point, point + 1);
int level = reach.grade()[point];
if (along > LENGTH || stops(reach, point, along, level, below)) {
return;
}
int allowed = base + (int) Math.floor(slope * Math.max(0f, along - firstStep));
if (level > allowed) {
lowered.computeIfAbsent(reach, known -> known.grade().clone())[point] = allowed;
} else if (along > COAST_REACH) {
return;
}
below = level;
}
for (Reach feeder : feeders.getOrDefault(reach.id(), List.of())) {
int end = feeder.grade().length - 1;
int[] grade = lowered.get(reach);
int ceiling = grade == null ? reach.grade()[0] : grade[0];
if (feeder.grade()[end] > ceiling) {
lowered.computeIfAbsent(feeder, known -> known.grade().clone())[end] = ceiling;
}
walk(feeder, end, along, feeder.grade()[end]);
}
}
private boolean stops(Reach reach, int point, float along, int level, int below) {
if (reach.style()[point] == ReachStyle.POND_OUTLET || besidePond(reach, point)) {
return true;
}
if (level - below < reach.tier().waterfallMinimumDrop()) {
return false;
}
return along > COAST_REACH || level - seaLevel >= CLIFF_HEIGHT;
}
private boolean besidePond(Reach reach, int point) {
for (Pond pond : ponds) {
if (ReachShaper.nearLip(pond.mask(), reach.xs()[point], reach.zs()[point], reach.halfWidth()[point])) {
return true;
}
}
return false;
}
private Reach largestFeeder(Reach reach) {
Reach largest = null;
for (Reach feeder : feeders.getOrDefault(reach.id(), List.of())) {
if (largest == null || feeder.catchment() > largest.catchment()) {
largest = feeder;
}
}
return largest;
}
private static float distance(Reach reach, int from, int to) {
return (float) Math.hypot(reach.xs()[to] - reach.xs()[from], reach.zs()[to] - reach.zs()[from]);
}
private static Reach rebuilt(Reach reach, int[] grade, int seaLevel) {
int count = grade.length;
boolean[] ocean = new boolean[count];
boolean[] outlet = new boolean[count];
for (int point = 0; point < count; point++) {
ocean[point] = reach.style()[point] == ReachStyle.MOUTH;
outlet[point] = reach.style()[point] == ReachStyle.POND_OUTLET;
}
ReachStyle[] styled = ReachShaper.styles(grade, ocean, outlet, reach.tier().waterfallMinimumDrop(), seaLevel, reach.tier().stepHeight());
ReachStyle[] style = reach.style().clone();
for (int point = 0; point < count; point++) {
boolean touched = grade[point] != reach.grade()[point] || point > 0 && grade[point - 1] != reach.grade()[point - 1]
|| point + 1 < count && grade[point + 1] != reach.grade()[point + 1];
if (touched) {
style[point] = styled[point];
}
}
return new Reach(reach.id(), reach.tier(), reach.downstreamId(), reach.xs(), reach.zs(), grade, reach.halfWidth(), reach.depth(), style, reach.catchment())
.withStraightFalls(reach.straightFalls());
}
}
@@ -1620,6 +1620,145 @@ public class ReachShaperTest {
assertEquals("a short estuary still meets the seabed", HydrologyTier.MINIMUM_DEPTH, estuary.depth()[last], 0f);
}
@Test
public void riverOnAPlateauComesDownToTheSeaOverAStretch() {
HydrologySettings settings = settings(config -> {
});
Reach reach = coastalRun(settings, 0, (mouth, x) -> SEA_LEVEL + 1D + Math.min(11D, 0.5D * Math.max(0D, mouth - x)));
assertGentleRunToTheSea(reach, settings);
int start = trailingMouth(reach);
assertEquals("the last water above the sea stands one step up", SEA_LEVEL + reach.tier().stepHeight(), reach.grade()[start - 1]);
float run = reach.xs()[start] - reach.xs()[firstPointAbove(reach, start, SEA_LEVEL + 6)];
assertTrue("the river takes " + run + " blocks over its last six", run >= 24f);
}
@Test
public void riverCommittedToRisingBanksStillArrivesInSmallSteps() {
HydrologySettings settings = settings(config -> {
});
assertGentleRunToTheSea(coastalRun(settings, 0, (mouth, x) -> SEA_LEVEL + 1D + 0.2D * Math.max(0D, mouth - x)), settings);
}
@Test
public void shortMouthReachLowersItsFeederToo() {
HydrologySettings settings = settings(config -> {
});
List<Reach> reaches = coastalRuns(settings, 48, (mouth, x) -> SEA_LEVEL + 1D + Math.min(11D, 0.1D * Math.max(0D, mouth - x)));
assertEquals(2, reaches.size());
Reach feeder = reaches.get(0).style()[reaches.get(0).style().length - 1] == ReachStyle.MOUTH ? reaches.get(1) : reaches.get(0);
int last = feeder.grade().length - 1;
int above = last - 1;
assertTrue("the feeder end " + feeder.grade()[above] + " stands a step above the sea", feeder.grade()[above] - SEA_LEVEL <= feeder.tier().stepHeight());
for (int point = 1; point <= last; point++) {
if (feeder.xs()[last] - feeder.xs()[point] > 24f) {
continue;
}
assertTrue("feeder step at " + point + ": " + feeder.grade()[point - 1] + " to " + feeder.grade()[point],
feeder.grade()[point - 1] - feeder.grade()[point] <= feeder.tier().stepHeight());
}
}
@Test
public void highCliffCoastKeepsItsWaterfallIntoTheSea() {
HydrologySettings settings = settings(config -> {
});
Reach reach = coastalRun(settings, 0, (mouth, x) -> x >= mouth - 2D ? SEA_LEVEL - 4D : SEA_LEVEL + 30D);
int start = trailingMouth(reach);
assertTrue("a high cliff stays a fall: " + reach.grade()[start - 1], reach.grade()[start - 1] >= SEA_LEVEL + 16);
assertEquals(ReachStyle.WATERFALL, reach.style()[start - 1]);
}
@Test
public void inlandFallAwayFromTheSeaKeepsItsHeight() {
HydrologySettings settings = settings(config -> {
});
Reach reach = coastalRun(settings, 0, (mouth, x) -> mouth - x > 150D ? SEA_LEVEL + 30D : SEA_LEVEL + 5D);
int drop = 0;
float at = 0f;
for (int point = 1; point < reach.grade().length; point++) {
if (reach.grade()[point - 1] - reach.grade()[point] > drop) {
drop = reach.grade()[point - 1] - reach.grade()[point];
at = reach.xs()[reach.xs().length - 1] - reach.xs()[point];
}
}
assertTrue("the fall stays " + drop + " tall at " + at + " from the mouth", drop >= 20 && at > 128f);
}
@Test
public void seaApproachLeavesOtherReachesAlone() {
HydrologySettings settings = settings(config -> {
});
Reach inland = coastalRun(settings, false);
assertTrue("a reach that does not meet the sea is the same object", inland == SeaApproach.apply(List.of(inland), List.of(), (x, z) -> SEA_LEVEL + 40D, SEA_LEVEL).get(0));
}
@Test
public void seaApproachStopsBesideAPond() {
HydrologySettings settings = settings(config -> {
});
DoubleBinaryOperator plateau = (mouth, x) -> SEA_LEVEL + 1D + Math.min(11D, 0.5D * Math.max(0D, mouth - x));
Reach open = coastalRun(settings, 0, plateau);
Reach plain = plainPlateau(settings, plateau);
int start = trailingMouth(open);
assertTrue("the open plateau lowers", open.grade()[start - 1] < plain.grade()[start - 1]);
float x = plain.xs()[start - 8];
float z = plain.zs()[start - 8];
int offset = Math.round(plain.halfWidth()[start - 8]) + 4;
Pond pond = new Pond(61L, PondKind.NATURAL, plain.grade()[start - 8], plain.grade()[start - 8] - 3, Math.round(x), Math.round(z), plain.grade()[start - 8] + 1f,
disk(Math.round(x), Math.round(z) + offset, 4), 0L, 0, "water", Math.round(x), Math.round(z) + offset);
Reach beside = SeaApproach.apply(List.of(plain), List.of(pond), (px, pz) -> SEA_LEVEL + 1D, SEA_LEVEL).get(0);
assertEquals("water above the pond's reach is left as planned", plain.grade()[0], beside.grade()[0]);
for (int point = 1; point < plain.grade().length; point++) {
assertTrue("no rise at " + point, beside.grade()[point - 1] >= beside.grade()[point]);
}
assertEquals("the water beside the pond keeps its level", plain.grade()[start - 8], beside.grade()[start - 8]);
}
private static Reach plainPlateau(HydrologySettings settings, DoubleBinaryOperator profile) {
int width = 30;
DemGrid surface = new DemGrid(0, 0, CELL, width, 3);
Arrays.fill(surface.values(), 90f);
int[] receivers = new int[width * 3];
Arrays.fill(receivers, -1);
float[] accumulation = new float[width * 3];
BitSet channels = new BitSet(width * 3);
for (int i = 0; i < width; i++) {
int cell = surface.index(i, 1);
receivers[cell] = i + 1 < width ? surface.index(i + 1, 1) : -1;
accumulation[cell] = (i + 1) * 1000f;
if (i + 1 < width) {
channels.set(cell);
}
}
surface.set(width - 1, 1, 30f);
double mouthX = (width - 1) * CELL;
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> profile.applyAsDouble(mouthX, x)), FINE, false, 1 << 16);
return new ReachShaper(settings).shape(WINDOW, surface, receivers, accumulation, channels,
SeaCells.atOrBelow(surface, SEA_LEVEL), List.of(), fine, SEED).reaches().get(0);
}
private static void assertGentleRunToTheSea(Reach reach, HydrologySettings settings) {
int start = trailingMouth(reach);
assertTrue("the reach ends in an estuary", start > 0);
assertTrue("no step of " + (reach.grade()[start - 1] - SEA_LEVEL) + " at the estuary " + Arrays.toString(reach.grade()),
reach.grade()[start - 1] - SEA_LEVEL <= reach.tier().stepHeight());
float mouthX = reach.xs()[reach.xs().length - 1];
for (int point = 1; point <= start; point++) {
if (mouthX - reach.xs()[point] <= 96f) {
assertTrue("step at " + point + ": " + reach.grade()[point - 1] + " to " + reach.grade()[point] + " in " + Arrays.toString(reach.grade()),
reach.grade()[point - 1] - reach.grade()[point] <= reach.tier().stepHeight());
}
}
}
private static int firstPointAbove(Reach reach, int from, int level) {
int point = from;
while (point > 0 && reach.grade()[point] < level) {
point--;
}
return point;
}
@Test
public void estuaryStopsAtAHeldPondLip() {
Reach outlet = coastalOutlet(PondKind.NATURAL);
@@ -2691,6 +2830,36 @@ public class ReachShaperTest {
assertTrue("estuary points over the low land " + inland, inland > 0);
}
private static Reach coastalRun(HydrologySettings settings, int originX, DoubleBinaryOperator profile) {
List<Reach> reaches = coastalRuns(settings, originX, profile);
assertEquals(1, reaches.size());
return reaches.get(0);
}
private static List<Reach> coastalRuns(HydrologySettings settings, int originX, DoubleBinaryOperator profile) {
int width = 30;
DemGrid surface = new DemGrid(originX, 0, CELL, width, 3);
Arrays.fill(surface.values(), 90f);
int[] receivers = new int[width * 3];
Arrays.fill(receivers, -1);
float[] accumulation = new float[width * 3];
BitSet channels = new BitSet(width * 3);
for (int i = 0; i < width; i++) {
int cell = surface.index(i, 1);
receivers[cell] = i + 1 < width ? surface.index(i + 1, 1) : -1;
accumulation[cell] = (i + 1) * 1000f;
if (i + 1 < width) {
channels.set(cell);
}
}
surface.set(width - 1, 1, 30f);
double mouthX = originX + (width - 1) * CELL;
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> profile.applyAsDouble(mouthX, x)), FINE, false, 1 << 16);
List<Reach> shaped = new ReachShaper(settings).shape(WINDOW, surface, receivers, accumulation, channels,
SeaCells.atOrBelow(surface, SEA_LEVEL), List.of(), fine, SEED).reaches();
return SeaApproach.apply(shaped, List.of(), (x, z) -> profile.applyAsDouble(mouthX, x), SEA_LEVEL);
}
private static Reach coastalRun(HydrologySettings settings, boolean ocean) {
return coastalRun(settings, ocean, 0.08D);
}