Confluence grading

- Add ConfluenceGrader: feeder tails touching the receiving channel or a sibling settle to the junction level, capped at 64 blocks of arc
- Grade small junction differences over an approach ramp of one block per four, and keep larger ones as one fall at the receiving bank
- Never move pond outlet zone or held outlet points while grading confluences
- Run confluence grading after the finish pass in ReachShaper and restyle the reaches it lowers
- Add ReachShaperTest cases for feeders meeting the junction level and a hanging tributary falling at the receiving bank
- Compare drop counts by size instead of cascade and waterfall point styles in gradeNeverRisesDownstream
- Assert no confluence tongues on openFractal seeds 1 to 3 in WindowPlannerTest
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-01 17:58:33 -05:00
1 parent 50839c4833
commit 4cf141b97b
4 files changed
+292 -7

No files matched your search

@@ -0,0 +1,143 @@
package art.arcane.iris.generation.hydrology.water;
import java.util.ArrayList;
import java.util.List;
public final class ConfluenceGrader {
public static final float POOL_MARGIN = 2f;
public static final float POOL_CAP = 64f;
public static final float APPROACH_SLOPE = 0.25f;
private ConfluenceGrader() {
}
public record Footprint(float[] xs, float[] zs, float[] halfWidth) {
}
public static int[] settle(Footprint feeder, int[] grade, boolean[] fixed, List<Footprint> receiving, int level, int waterfallMinimumDrop) {
int last = grade.length - 1;
double[] arc = arcFromEnd(feeder);
int[] settled = lower(grade, grade, last, level);
int edge = last;
for (int point = last - 1; point >= 0 && pooled(feeder, fixed, receiving, arc, point); point--) {
settled = lower(settled, grade, point, level);
edge = point;
}
int meet = approach(grade, fixed, arc, edge, level, waterfallMinimumDrop);
for (int point = edge - 1; point > meet; point--) {
settled = lower(settled, grade, point, envelope(arc, edge, point, level));
}
return settled;
}
static boolean[] level(List<ReachDraft> drafts, int[] upstreamFirst) {
boolean[] touched = new boolean[drafts.size()];
for (int position = upstreamFirst.length - 1; position >= 0; position--) {
ReachDraft downstream = drafts.get(upstreamFirst[position]);
for (int feeder : downstream.incoming) {
ReachDraft draft = drafts.get(feeder);
int[] settled = settle(footprint(draft), draft.grade, fixed(draft), receiving(drafts, downstream, feeder), downstream.grade[0],
draft.tier.waterfallMinimumDrop());
if (settled != draft.grade) {
draft.grade = settled;
touched[feeder] = true;
}
}
}
return touched;
}
static double edgeDistance(Footprint channel, double x, double z) {
float[] xs = channel.xs();
float[] zs = channel.zs();
float[] halfWidth = channel.halfWidth();
double nearest = Double.POSITIVE_INFINITY;
for (int segment = 0; segment + 1 < xs.length; segment++) {
double ax = xs[segment];
double az = zs[segment];
double dx = xs[segment + 1] - ax;
double dz = zs[segment + 1] - az;
double length = dx * dx + dz * dz;
double t = length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D);
double offsetX = x - (ax + t * dx);
double offsetZ = z - (az + t * dz);
double width = halfWidth[segment] + t * (halfWidth[segment + 1] - halfWidth[segment]);
nearest = Math.min(nearest, Math.sqrt(offsetX * offsetX + offsetZ * offsetZ) - width);
}
return nearest;
}
private static boolean pooled(Footprint feeder, boolean[] fixed, List<Footprint> receiving, double[] arc, int point) {
if (arc[point] > POOL_CAP || fixed[point]) {
return false;
}
double reach = feeder.halfWidth()[point] + POOL_MARGIN;
for (Footprint channel : receiving) {
if (edgeDistance(channel, feeder.xs()[point], feeder.zs()[point]) <= reach) {
return true;
}
}
return false;
}
private static int approach(int[] grade, boolean[] fixed, double[] arc, int edge, int level, int waterfallMinimumDrop) {
double window = (waterfallMinimumDrop - 1) / (double) APPROACH_SLOPE;
for (int point = edge - 1; point >= 0 && arc[point] - arc[edge] <= window; point--) {
if (grade[point] <= envelope(arc, edge, point, level)) {
return point;
}
if (fixed[point]) {
return edge;
}
}
return edge;
}
private static int envelope(double[] arc, int edge, int point, int level) {
return level + (int) Math.floor(APPROACH_SLOPE * (arc[point] - arc[edge]));
}
private static int[] lower(int[] settled, int[] grade, int point, int ceiling) {
if (settled[point] <= ceiling) {
return settled;
}
int[] lowered = settled == grade ? grade.clone() : settled;
lowered[point] = ceiling;
return lowered;
}
private static double[] arcFromEnd(Footprint feeder) {
float[] xs = feeder.xs();
float[] zs = feeder.zs();
double[] arc = new double[xs.length];
for (int point = xs.length - 2; point >= 0; point--) {
arc[point] = arc[point + 1] + Math.hypot(xs[point + 1] - xs[point], zs[point + 1] - zs[point]);
}
return arc;
}
private static Footprint footprint(ReachDraft draft) {
return new Footprint(draft.xs, draft.zs, draft.halfWidth);
}
private static boolean[] fixed(ReachDraft draft) {
boolean[] fixed = draft.outletZone.clone();
if (draft.held != null) {
for (int point = 0; point < fixed.length; point++) {
fixed[point] |= draft.held[point];
}
}
return fixed;
}
private static List<Footprint> receiving(List<ReachDraft> drafts, ReachDraft downstream, int feeder) {
List<Footprint> receiving = new ArrayList<>(downstream.incoming.length);
receiving.add(footprint(downstream));
for (int sibling : downstream.incoming) {
if (sibling != feeder) {
receiving.add(footprint(drafts.get(sibling)));
}
}
return receiving;
}
}
@@ -73,6 +73,12 @@ public final class ReachShaper {
for (int index : order) {
finish(drafts, drafts.get(index));
}
boolean[] touched = ConfluenceGrader.level(drafts, order);
for (int index = 0; index < touched.length; index++) {
if (touched[index]) {
restyle(drafts.get(index));
}
}
for (int index = 0; index < sources.length; index++) {
if (leavesThroughALip(ponds, sources[index])) {
leadIn(drafts.get(index), ponds.get(sources[index]).mask());
@@ -362,6 +368,10 @@ public final class ReachShaper {
upstream = Math.max(upstream, drafts.get(index).maxHalfWidth);
}
widths(draft, upstream);
restyle(draft);
}
private static void restyle(ReachDraft draft) {
draft.style = styles(draft.grade, draft.ocean, draft.outletZone, draft.tier.waterfallMinimumDrop());
}
@@ -630,6 +630,20 @@ public class WindowPlannerTest {
assertTrue("pond outlets checked " + outlets, outlets >= 50);
}
@Test
public void confluencesLeaveNoPerchedTongue() {
int feeders = 0;
for (long seed = 1L; seed <= 3L; seed++) {
PlannedScene scene = PlannedScene.plan(SyntheticTerrain.openFractal(seed), PlannedScene.packSettings(SEED), 1, 1);
assertEquals("confluence tongues on openFractal(" + seed + ")", List.of(), WaterInvariants.confluenceTongues(scene));
Map<Long, Reach> reaches = byId(scene.reaches());
for (Reach reach : scene.reaches()) {
feeders += reaches.containsKey(reach.downstreamId()) ? 1 : 0;
}
}
assertTrue("feeders checked " + feeders, feeders >= 100);
}
private static void assertOutletLeavesAtThePondHead(Pond pond, Reach outlet) {
PondMask mask = pond.mask();
int leading = 0;
@@ -42,6 +42,7 @@ public class ReachShaperTest {
private static final int CELL = 8;
private static final int FINE = 4;
private static final int VALLEY_Z = 256;
private static final int HANGING_X = 256;
private static final float VALLEY_THRESHOLD = 5000f;
private static final int STEP_X = 196;
private static final int WINDOW_MIN_X = 140;
@@ -57,6 +58,8 @@ public class ReachShaperTest {
int[] totals = new int[6];
int cascadingSteps = 0;
int spreadSteps = 0;
int cascadingFalls = 0;
int spreadFalls = 0;
for (int trial = 0; trial < 200; trial++) {
Network network = network(cascading, randomTerrain(random), 40, 40, 2000f, trial % 4 == 0);
ShapedNetwork stepped = shape(cascading, network);
@@ -67,9 +70,11 @@ public class ReachShaperTest {
for (Reach reach : spread.reaches()) {
Reach twin = steppedById.get(reach.id());
assertNotNull(twin);
assertTrue("trial " + trial + " " + reach + " gained a waterfall", count(reach, ReachStyle.WATERFALL) <= count(twin, ReachStyle.WATERFALL));
spreadSteps += count(reach, ReachStyle.CASCADE);
cascadingSteps += count(twin, ReachStyle.CASCADE);
int fall = reach.tier().waterfallMinimumDrop();
spreadSteps += dropsBetween(reach, 2, fall);
cascadingSteps += dropsBetween(twin, 2, fall);
spreadFalls += dropsBetween(reach, fall, Integer.MAX_VALUE);
cascadingFalls += dropsBetween(twin, fall, Integer.MAX_VALUE);
}
}
assertTrue("reaches " + totals[0], totals[0] > 4000);
@@ -77,7 +82,8 @@ public class ReachShaperTest {
assertTrue("links " + totals[2], totals[2] > 1000);
assertTrue("mouth points " + totals[3], totals[3] > 100);
assertTrue("pond outlets " + totals[4], totals[4] > 40);
assertTrue("cascade steps spread " + spreadSteps + " kept " + cascadingSteps, 2 * spreadSteps < cascadingSteps);
assertTrue("waterfall drops spread " + spreadFalls + " kept " + cascadingFalls, spreadFalls <= cascadingFalls);
assertTrue("cascade drops spread " + spreadSteps + " kept " + cascadingSteps, 2 * spreadSteps < cascadingSteps);
}
@Test
@@ -111,6 +117,76 @@ public class ReachShaperTest {
assertTrue("confluences " + confluences, confluences >= 3);
}
@Test
public void feedersMeetTheConfluenceLevelWhereverTheyTouchTheReceivingWater() {
HydrologySettings settings = settings(config -> {
});
int confluences = 0;
int pooled = 0;
for (long seed : new long[]{SEED, SEED + 5L, SEED + 9L}) {
ShapedNetwork shaped = shape(settings, network(settings, fractal(seed), 64, 64, 2000f, false));
Map<Long, Reach> byId = byId(shaped.reaches());
int joins = 0;
for (Map.Entry<Long, List<Reach>> entry : feeders(shaped.reaches()).entrySet()) {
Reach downstream = byId.get(entry.getKey());
List<Reach> siblings = entry.getValue();
joins += siblings.size() >= 2 ? 1 : 0;
for (Reach feeder : siblings) {
List<ConfluenceGrader.Footprint> receiving = receiving(downstream, siblings, feeder);
for (int point = feeder.xs().length - 1; point >= 0; point--) {
double clearance = clearance(receiving, feeder.xs()[point], feeder.zs()[point]);
if (clearance > feeder.halfWidth()[point] + 1f) {
break;
}
assertEquals("seed " + seed + " " + feeder + " into " + downstream + " at point " + point + " with clearance " + clearance,
downstream.grade()[0], feeder.grade()[point]);
pooled++;
}
}
}
assertTrue("seed " + seed + " confluences " + joins, joins >= 3);
confluences += joins;
}
assertTrue("pooled points " + pooled + " at " + confluences + " confluences", pooled > 3 * confluences);
}
@Test
public void hangingTributaryFallsAtTheReceivingBank() {
HydrologySettings settings = settings(config -> {
});
ShapedNetwork shaped = shape(settings, network(settings, hangingValley(), 64, 64, 2000f, false));
Map<Long, Reach> byId = byId(shaped.reaches());
List<Reach> hanging = new ArrayList<>();
for (Reach reach : shaped.reaches()) {
if (reach.downstreamId() != 0L && Math.abs(reach.xs()[0] - HANGING_X) <= 32f && reach.zs()[0] < VALLEY_Z - 64f) {
hanging.add(reach);
}
}
assertEquals("tributaries draining the plateau " + hanging, 1, hanging.size());
Reach tributary = hanging.get(0);
Reach downstream = byId.get(tributary.downstreamId());
assertEquals(tributary.toString(), settings.tiers().get(0), tributary.tier());
assertEquals(downstream.toString(), settings.tiers().get(1), downstream.tier());
ConfluenceGrader.Footprint receiving = footprint(downstream);
int waterfalls = 0;
for (int point = 0; point < tributary.xs().length; point++) {
if (tributary.style()[point] == ReachStyle.WATERFALL) {
waterfalls++;
double edge = ConfluenceGrader.edgeDistance(receiving, tributary.xs()[point], tributary.zs()[point]);
assertTrue(tributary + " falls at point " + point + " inside the receiving channel, " + edge + " from its edge", edge > 0D);
}
}
assertEquals(tributary + " waterfalls", 1, waterfalls);
int inside = 0;
for (int point = 0; point < tributary.xs().length; point++) {
if (ConfluenceGrader.edgeDistance(receiving, tributary.xs()[point], tributary.zs()[point]) <= 0D) {
inside++;
assertEquals(tributary + " water inside the receiving channel at point " + point, downstream.grade()[0], tributary.grade()[point]);
}
}
assertTrue(tributary + " points inside the receiving channel " + inside, inside > 0);
}
@Test
public void cascadeStepsSurviveWhenTierCascades() {
List<Drop> cascading = drops(settings(config -> {
@@ -1217,10 +1293,11 @@ public class ReachShaperTest {
}
}
private static int count(Reach reach, ReachStyle style) {
private static int dropsBetween(Reach reach, int minimum, int limit) {
int count = 0;
for (ReachStyle point : reach.style()) {
count += point == style ? 1 : 0;
for (int index = 1; index < reach.grade().length; index++) {
int drop = reach.grade()[index - 1] - reach.grade()[index];
count += drop >= minimum && drop < limit ? 1 : 0;
}
return count;
}
@@ -1415,6 +1492,14 @@ public class ReachShaperTest {
};
}
private static DoubleBinaryOperator hangingValley() {
return (x, z) -> {
double ground = 150D - 0.05D * x + 0.1D * Math.abs(z - VALLEY_Z);
double across = Math.abs(x - HANGING_X);
return z < VALLEY_Z - 2 && across <= 32D ? ground + 20D + 0.25D * across : ground;
};
}
private static DoubleBinaryOperator bowlValley(int[] centres) {
return (x, z) -> {
double height = 150D - 0.05D * x + 0.1D * Math.abs(z - VALLEY_Z);
@@ -1576,6 +1661,39 @@ public class ReachShaperTest {
return incoming;
}
private static Map<Long, List<Reach>> feeders(List<Reach> reaches) {
Map<Long, List<Reach>> feeders = new HashMap<>();
for (Reach reach : reaches) {
if (reach.downstreamId() != 0L) {
feeders.computeIfAbsent(reach.downstreamId(), id -> new ArrayList<>()).add(reach);
}
}
return feeders;
}
private static List<ConfluenceGrader.Footprint> receiving(Reach downstream, List<Reach> siblings, Reach feeder) {
List<ConfluenceGrader.Footprint> receiving = new ArrayList<>(siblings.size());
receiving.add(footprint(downstream));
for (Reach sibling : siblings) {
if (sibling != feeder) {
receiving.add(footprint(sibling));
}
}
return receiving;
}
private static ConfluenceGrader.Footprint footprint(Reach reach) {
return new ConfluenceGrader.Footprint(reach.xs(), reach.zs(), reach.halfWidth());
}
private static double clearance(List<ConfluenceGrader.Footprint> receiving, float x, float z) {
double clearance = Double.POSITIVE_INFINITY;
for (ConfluenceGrader.Footprint channel : receiving) {
clearance = Math.min(clearance, ConfluenceGrader.edgeDistance(channel, x, z));
}
return clearance;
}
private static void noCascades(IrisHydrology config) {
for (IrisHydrologyTier tier : config.getTiers()) {
tier.setCascades(false);