Fed start riffle fix

- Hold a fed feeder's start only when it stands more than 1 block above its junction, so 1-block riffle starts join the receiving water
- Keep a fed start in the tongue invariant's pool zone as a held point that must cascade into its junction
- Drop the held-lip riffle tolerance and assert no tongues on the seeds again
- Stop the junction-level and led-in pool walks only at a styled fall from a held start or from more than 5 blocks beside the bank
- Cover 1-block fed starts in grader unit tests and riffling held starts in the scene invariant tests
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-02 05:00:56 -05:00
1 parent 06b7001132
commit 2edf610e8b
6 files changed
+83 -65

No files matched your search

@@ -10,6 +10,7 @@ public final class ConfluenceGrader {
public static final float POOL_CAP = 64f;
public static final float APPROACH_SLOPE = 0.25f;
public static final int MARGIN_LOWERING = 5;
public static final int MAX_START_LOWERING = 1;
private ConfluenceGrader() {
}
@@ -53,7 +54,7 @@ public final class ConfluenceGrader {
ReachDraft draft = drafts.get(feeder);
requireJunction(window, draft, downstream);
boolean[] fixed = draft.fixed();
fixed[0] |= draft.incoming.length > 0;
fixed[0] |= draft.incoming.length > 0 && draft.grade[0] - downstream.grade[0] > MAX_START_LOWERING;
int waterfallMinimumDrop = draft.tier.waterfallMinimumDrop();
int[] settled = settle(footprint(draft), draft.grade, fixed, receiving(drafts, downstream, feeder), downstream.grade[0],
waterfallMinimumDrop);
@@ -59,15 +59,23 @@ public class PlannedSceneTest {
continue;
}
junctions++;
int level = outlet.grade()[0];
double arc = 0D;
for (int point = last - 1; point >= 0 && feeder.style()[point] != ReachStyle.POND_OUTLET; point--) {
arc += Math.hypot(feeder.xs()[point + 1] - feeder.xs()[point], feeder.zs()[point + 1] - feeder.zs()[point]);
float reach = feeder.halfWidth()[point] + ConfluenceGrader.POOL_MARGIN;
if (arc > ConfluenceGrader.POOL_CAP || edgeDistance(outlet, 0, feeder.xs()[point], feeder.zs()[point]) > reach
|| point == 0 && fed.contains(feeder.id())) {
double edge = edgeDistance(outlet, 0, feeder.xs()[point], feeder.zs()[point]);
if (arc > ConfluenceGrader.POOL_CAP || edge > reach) {
break;
}
assertEquals(feeder + " point " + point + " in the pool of led-in outlet " + outlet, outlet.grade()[0], feeder.grade()[point]);
int grade = feeder.grade()[point];
if (grade != level && (point == 0 && fed.contains(feeder.id()) || edge > 0D && grade - level > ConfluenceGrader.MARGIN_LOWERING)) {
ReachStyle style = feeder.style()[point + 1];
assertTrue(feeder + " point " + point + " stands above the pool of led-in outlet " + outlet + " and drops into it as " + style,
style == ReachStyle.CASCADE || style == ReachStyle.WATERFALL);
break;
}
assertEquals(feeder + " point " + point + " in the pool of led-in outlet " + outlet, level, grade);
besideTheLeadIn += edgeDistance(outlet, 1, feeder.xs()[point], feeder.zs()[point]) > reach ? 1 : 0;
}
}
@@ -133,13 +141,17 @@ public class PlannedSceneTest {
}
@Test
public void startHeldForItsOwnFeederIsNoTongue() {
public void startHeldForItsOwnFeederMustFallIntoTheJunction() {
HydrologySettings settings = PlannedScene.packSettings(SEED);
Reach stub = tributary(settings, new int[]{144, 124}, new int[]{144, 136}, 2f, (x, z) -> z <= 124 ? 70 : 66);
Reach upstream = straight(304L, settings.tiers().get(0), stub.id(), 1f, new int[]{144, 56}, new int[]{144, 124}, (x, z) -> 70);
assertEquals("a stub perched at its start", List.of(stub.id()), tongues(settings, stub, 66, true, List.of()));
assertEquals("a stub holding its start for the reach that feeds it", List.of(),
assertEquals("a stub holding its start for the reach that feeds it, cascading into the junction", List.of(),
WaterInvariants.confluenceTongues(scene(settings, List.of(stub, upstream), 66, true, List.of())));
Reach riffling = tributary(settings, new int[]{144, 124}, new int[]{144, 136}, 2f, (x, z) -> z <= 124 ? 68 : 67);
Reach feeding = straight(304L, settings.tiers().get(0), riffling.id(), 1f, new int[]{144, 56}, new int[]{144, 124}, (x, z) -> 68);
assertEquals("a held start whose 1-block riffle into the junction shows as a 2-block step", List.of(riffling.id()),
WaterInvariants.confluenceTongues(scene(settings, List.of(riffling, feeding), 67, true, List.of())));
}
@Test
@@ -159,14 +171,11 @@ public class PlannedSceneTest {
assertEquals("outlet held on its pond's lip 4 blocks above the junction, cascading into it", List.of(),
tongues(settings, falling, 66, true, List.of(source(70, falling))));
Reach stepping = outlet(tributary(settings, lip, junction, 2f, (x, z) -> z >= 130 ? 67 : 68));
PlannedScene riffling = scene(settings, List.of(stepping), 67, true, List.of(source(68, stepping)));
assertEquals("held outlet whose 1-block riffle into the junction shows as a 2-block step", List.of(stepping.id()),
WaterInvariants.confluenceTongues(riffling));
assertEquals("and that tongue is a held lip riffling into the junction", List.of(stepping.id()), WaterInvariants.heldLipRiffles(riffling));
tongues(settings, stepping, 67, true, List.of(source(68, stepping))));
Reach above = outlet(tributary(settings, lip, junction, 2f, (x, z) -> 70));
PlannedScene perched = scene(settings, List.of(above), 66, true, List.of(source(70, above)));
assertEquals("held outlet whose own end stays above the junction", List.of(above.id()), WaterInvariants.confluenceTongues(perched));
assertEquals("a held outlet ending above the junction is no lip riffle", List.of(), WaterInvariants.heldLipRiffles(perched));
assertEquals("held outlet whose own end stays above the junction", List.of(above.id()),
tongues(settings, above, 66, true, List.of(source(70, above))));
}
@Test
@@ -174,10 +183,8 @@ public class PlannedSceneTest {
HydrologySettings settings = PlannedScene.packSettings(SEED);
Reach outlet = outlet(tributary(settings, new int[]{144, 56}, new int[]{144, 136}, 2f, (x, z) -> z >= 136 ? 66 : 70));
Pond pond = pond(21L, 70, disk(144, 46, 10), outlet.id());
PlannedScene scene = scene(settings, List.of(outlet), 66, true, List.of(pond));
assertEquals("outlet held only near its pond, cascading into the junction from a perched tail", List.of(outlet.id()),
WaterInvariants.confluenceTongues(scene));
assertEquals("a perched unheld tail is no lip riffle", List.of(), WaterInvariants.heldLipRiffles(scene));
tongues(settings, outlet, 66, true, List.of(pond)));
}
@Test
@@ -56,24 +56,13 @@ final class WaterInvariants {
int seaLevel = scene.settings().seaLevel();
List<Long> tongues = new ArrayList<>();
for (Junction junction : junctions(scene)) {
if (perched(scene, junction, seaLevel, false)) {
if (perched(scene, junction, seaLevel)) {
tongues.add(junction.feeder().id());
}
}
return tongues;
}
static List<Long> heldLipRiffles(PlannedScene scene) {
int seaLevel = scene.settings().seaLevel();
List<Long> riffles = new ArrayList<>();
for (Junction junction : junctions(scene)) {
if (perched(scene, junction, seaLevel, false) && !perched(scene, junction, seaLevel, true)) {
riffles.add(junction.feeder().id());
}
}
return riffles;
}
static List<Reach> checkedFeeders(PlannedScene scene) {
List<Reach> checked = new ArrayList<>();
for (Junction junction : junctions(scene)) {
@@ -157,17 +146,18 @@ final class WaterInvariants {
continue;
}
int level = Math.min(feeder.grade()[feeder.grade().length - 1], downstream.grade()[0]);
boolean[] pool = pool(feeder, receiving(feeder, downstream, feeders.get(downstream.id())), level, feeders.containsKey(feeder.id()));
boolean[] pool = pool(feeder, receiving(feeder, downstream, feeders.get(downstream.id())), level);
int[] box = interior(scene, poolBox(feeder, pool));
if (box != null) {
boolean[] held = held(feeder, lips.getOrDefault(feeder.id(), List.of()));
held[0] |= feeders.containsKey(feeder.id());
junctions.add(new Junction(feeder, WaterHeads.snap(level, seaLevel), pool, held, box));
}
}
return junctions;
}
private static boolean perched(PlannedScene scene, Junction junction, int seaLevel, boolean riffles) {
private static boolean perched(PlannedScene scene, Junction junction, int seaLevel) {
Reach feeder = junction.feeder();
boolean endsAtTheJunction = WaterHeads.snap(feeder.grade()[feeder.grade().length - 1], seaLevel) <= junction.head();
int[] box = junction.box();
@@ -180,7 +170,7 @@ final class WaterInvariants {
if (point < 0 || !junction.pool()[point]) {
continue;
}
if (!endsAtTheJunction || !junction.held()[point] || !fallsToTheJunction(feeder, point, seaLevel, riffles)) {
if (!endsAtTheJunction || !junction.held()[point] || !fallsToTheJunction(feeder, point, seaLevel)) {
return true;
}
}
@@ -188,12 +178,12 @@ final class WaterInvariants {
return false;
}
private static boolean[] pool(Reach feeder, List<Reach> receiving, int level, boolean fedStart) {
private static boolean[] pool(Reach feeder, List<Reach> receiving, int level) {
int last = feeder.xs().length - 1;
boolean[] pool = new boolean[last + 1];
pool[last] = true;
double arc = 0D;
for (int point = last - 1; point >= (fedStart ? 1 : 0); point--) {
for (int point = last - 1; point >= 0; point--) {
arc += Math.hypot(feeder.xs()[point + 1] - feeder.xs()[point], feeder.zs()[point + 1] - feeder.zs()[point]);
double edge = edgeDistance(receiving, feeder.xs()[point], feeder.zs()[point]);
if (arc > ConfluenceGrader.POOL_CAP || edge > feeder.halfWidth()[point] + ConfluenceGrader.POOL_MARGIN
@@ -234,12 +224,11 @@ final class WaterInvariants {
return false;
}
private static boolean fallsToTheJunction(Reach feeder, int point, int seaLevel, boolean riffles) {
private static boolean fallsToTheJunction(Reach feeder, int point, int seaLevel) {
for (int next = point + 1; next < feeder.grade().length; next++) {
boolean step = WaterHeads.snap(feeder.grade()[next], seaLevel) < WaterHeads.snap(feeder.grade()[next - 1], seaLevel);
ReachStyle style = feeder.style()[next];
boolean riffle = riffles && feeder.grade()[next - 1] - feeder.grade()[next] == 1;
if (step && style != ReachStyle.CASCADE && style != ReachStyle.WATERFALL && !riffle) {
if (step && style != ReachStyle.CASCADE && style != ReachStyle.WATERFALL) {
return false;
}
}
@@ -61,7 +61,6 @@ public class WindowPlannerTest {
private static final double PIT_RADIUS = 50D;
private static final double PIT_DEPTH = 30D;
private static final int DEEP_CUT = 12;
private static final int MAX_LIP_RIFFLES = 3;
private static final int SLIVER_REACH = 32;
private static final int RIM_CUT = 24;
private static final double VALLEY_HALF_WIDTH = 200D;
@@ -654,21 +653,15 @@ public class WindowPlannerTest {
public void pondOutletsAndConfluencesMeetConnectedWater() {
int outlets = 0;
int feeders = 0;
int lipRiffles = 0;
for (long seed = 1L; seed <= 7L; seed++) {
PlannedScene scene = PlannedScene.plan(SyntheticTerrain.openFractal(seed), PlannedScene.packSettings(SEED), 1, 1);
assertEquals("detached outlets on openFractal(" + seed + ")", List.of(), WaterInvariants.detachedOutlets(scene));
List<Long> riffles = WaterInvariants.heldLipRiffles(scene);
List<Long> tongues = new ArrayList<>(WaterInvariants.confluenceTongues(scene));
tongues.removeAll(riffles);
assertEquals("confluence tongues on openFractal(" + seed + ")", List.of(), tongues);
lipRiffles += riffles.size();
assertEquals("confluence tongues on openFractal(" + seed + ")", List.of(), WaterInvariants.confluenceTongues(scene));
outlets += WaterInvariants.checkedPonds(scene).size();
feeders += WaterInvariants.checkedFeeders(scene).size();
}
assertTrue("pond outlets checked " + outlets, outlets >= 50);
assertTrue("confluence feeders checked " + feeders, feeders >= 300);
assertTrue("held outlet lips riffling into their junction " + lipRiffles, lipRiffles <= MAX_LIP_RIFFLES);
}
private static void assertOutletLeavesAtThePondHead(Pond pond, Reach outlet) {
@@ -21,7 +21,7 @@ public class ConfluenceGraderTest {
private static final int JUNCTION = 100;
private static final int POOL_EDGE_Z = -12;
private static final int FEEDER_Z = -48;
private static final int STUB_Z = -8;
private static final int STUB_Z = -10;
private static final int BANK_Z = -8;
private static final int MARGIN_Z = -12;
@@ -60,15 +60,11 @@ public class ConfluenceGraderTest {
@Test
public void aFeederWithItsOwnFeedersKeepsItsStartAtTheirJunction() {
HydrologyTier tier = tier(true);
ReachDraft upstream = line(tier, 31L, 0f, FEEDER_Z, 0f, STUB_Z, 2f, z -> JUNCTION + 3);
ReachDraft stub = line(tier, 32L, 0f, STUB_Z, 0f, 0f, 2f, z -> z == STUB_Z ? JUNCTION + 3 : z == 0 ? JUNCTION : JUNCTION + 2);
ReachDraft river = line(tier, 33L, 0f, 0f, 64f, 0f, 8f, z -> JUNCTION);
upstream.downstream = 1;
stub.incoming = new int[]{0};
stub.downstream = 2;
river.incoming = new int[]{1};
ConfluenceGrader.level(WINDOW, List.of(upstream, stub, river), new int[]{0, 1, 2});
List<ReachDraft> drafts = stubbed(JUNCTION + 3);
ReachDraft upstream = drafts.get(0);
ReachDraft stub = drafts.get(1);
assertTrue("the stub starts beside the river, outside its channel",
ConfluenceGrader.edgeDistance(footprint(drafts.get(2)), stub.xs[0], stub.zs[0]) > 0D);
assertEquals("the stub keeps its start for the reach that feeds it " + Arrays.toString(stub.grade), JUNCTION + 3, stub.grade[0]);
for (int point = 1; point < stub.grade.length; point++) {
assertEquals("stub point " + point + " pooled " + Arrays.toString(stub.grade), JUNCTION, stub.grade[point]);
@@ -78,6 +74,38 @@ public class ConfluenceGraderTest {
}
}
@Test
public void aFeederStartingOneBlockAboveItsJunctionJoinsIt() {
List<ReachDraft> drafts = stubbed(JUNCTION + ConfluenceGrader.MAX_START_LOWERING);
ReachDraft upstream = drafts.get(0);
ReachDraft stub = drafts.get(1);
for (int point = 0; point < stub.grade.length; point++) {
assertEquals("stub point " + point + " " + Arrays.toString(stub.grade), JUNCTION, stub.grade[point]);
}
assertEquals("the upstream reach meets the lowered start " + Arrays.toString(upstream.grade), JUNCTION, upstream.grade[upstream.grade.length - 1]);
}
private static List<ReachDraft> stubbed(int start) {
HydrologyTier tier = tier(true);
ReachDraft upstream = line(tier, 31L, 0f, FEEDER_Z, 0f, STUB_Z, 2f, z -> start);
ReachDraft stub = line(tier, 32L, 0f, STUB_Z, 0f, 0f, 2f, z -> z == STUB_Z ? start : z == 0 ? JUNCTION : Math.min(start, JUNCTION + 2));
ReachDraft river = line(tier, 33L, 0f, 0f, 64f, 0f, 8f, z -> JUNCTION);
link(upstream, stub, river);
return List.of(upstream, stub, river);
}
private static void link(ReachDraft upstream, ReachDraft stub, ReachDraft river) {
upstream.downstream = 1;
stub.incoming = new int[]{0};
stub.downstream = 2;
river.incoming = new int[]{1};
ConfluenceGrader.level(WINDOW, List.of(upstream, stub, river), new int[]{0, 1, 2});
}
private static ConfluenceGrader.Footprint footprint(ReachDraft draft) {
return new ConfluenceGrader.Footprint(draft.xs, draft.zs, draft.halfWidth);
}
@Test
public void feederBesideTheReceivingBankPoolsOnlyWhenItIsNearTheJunctionLevel() {
int[] near = besideTheBank(ConfluenceGrader.MARGIN_LOWERING);
@@ -143,22 +143,22 @@ public class ReachShaperTest {
int level = downstream.grade()[0];
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 || point == 0 && feeders.containsKey(feeder.id())) {
if (clearance > feeder.halfWidth()[point] + 1f) {
break;
}
String at = "seed " + seed + " " + feeder + " into " + downstream + " at point " + point + " with clearance " + clearance;
int grade = feeder.grade()[point];
if (clearance <= 0D || grade == level) {
assertEquals(at, level, grade);
pooled++;
inside += clearance <= 0D ? 1 : 0;
continue;
boolean heldStart = point == 0 && feeders.containsKey(feeder.id());
if (grade != level && (heldStart || clearance > 0D && grade - level > ConfluenceGrader.MARGIN_LOWERING)) {
ReachStyle style = feeder.style()[point + 1];
assertTrue(at + " stands " + (grade - level) + " above the junction and drops into it as " + style,
style == ReachStyle.CASCADE || style == ReachStyle.WATERFALL);
falls++;
break;
}
assertTrue(at + " stands " + (grade - level) + " above the junction beside the bank", grade - level > ConfluenceGrader.MARGIN_LOWERING);
ReachStyle style = feeder.style()[point + 1];
assertTrue(at + " drops into the junction as " + style, style == ReachStyle.CASCADE || style == ReachStyle.WATERFALL);
falls++;
break;
assertEquals(at, level, grade);
pooled++;
inside += clearance <= 0D ? 1 : 0;
}
}
}
@@ -167,7 +167,7 @@ public class ReachShaperTest {
}
assertTrue("pooled points " + pooled + " at " + confluences + " confluences", pooled > 3 * confluences);
assertTrue("pooled points inside the receiving channel " + inside + " at " + confluences + " confluences", inside > 3 * confluences);
assertTrue("falls at the receiving bank " + falls, falls > 0);
assertTrue("falls into the junction from beside the bank or a held start " + falls, falls > 0);
}
@Test