Confluence settling review fixes

- Look reaches and outlet ponds up through ReachIndex.reach and ReachIndex.outletPonds, built once per plan, instead of per-chunk maps in ChunkFeatures.Links
- Find a feeder's downstream reach across the loaded windows and fail closed when two windows own it
- Clamp a no-lead-in shared outlet to the last pond with its id in its own plan when every sharing pond is in that plan, matching ReachGrader.outletPonds
- Rebuild an outlet's held lip with the shared ReachShaper.nearLip rule and keep it fixed when the outlet settles across a window line
- Rename the feederLink local that shadowed the owned-reach lookup
- Add ChunkWaterRasterTest cases for the downstream outlet pond level, a fully pooled stub, the held lip, the same-plan shared outlet and the duplicate owner
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-01 19:27:01 -05:00
1 parent e62585b450
commit 1cdc30e1ca
5 files changed
+244 -53

No files matched your search

@@ -295,27 +295,11 @@ final class ChunkFeatures {
private static final class Links {
private final Map<WindowKey, IndexedWindow> windows;
private final Map<Long, Reach> owned = new HashMap<>();
private final Map<Long, List<Pond>> outletPonds = new HashMap<>();
private final Map<Long, Reach> resolved = new HashMap<>();
private final Set<Long> resolving = new HashSet<>();
private Links(Map<WindowKey, IndexedWindow> windows) {
this.windows = windows;
for (IndexedWindow window : windows.values()) {
for (Reach reach : window.plan().reaches()) {
Reach other = owned.putIfAbsent(reach.id(), reach);
if (other != null) {
throw new IllegalStateException("Reach " + reach.id() + " is owned by window " + window.plan().key()
+ " and by window " + windowAt(other, other.xs().length - 1));
}
}
for (Pond pond : window.plan().ponds()) {
if (pond.outletReachId() != 0L) {
outletPonds.computeIfAbsent(pond.outletReachId(), ignored -> new ArrayList<>()).add(pond);
}
}
}
}
private Reach resolve(Reach reach, WindowKey owner) {
@@ -334,17 +318,18 @@ final class ChunkFeatures {
private Reach link(Reach reach, WindowKey owner) {
Reach linked = feederLink(reach, owner);
Integer pondLevel = outletLevel(reach);
Integer pondLevel = outletLevel(reach, owner);
return settle(reach, pondLevel == null ? linked : LinkBlend.clamp(linked, pondLevel), owner);
}
private Reach settle(Reach feeder, Reach linked, WindowKey owner) {
Reach downstream = owned.get(feeder.downstreamId());
if (downstream == null || owner.equals(windowAt(downstream, downstream.xs().length - 1))) {
IndexedWindow home = ownerOf(feeder.downstreamId());
if (home == null || home.plan().key().equals(owner)) {
return linked;
}
Reach downstream = home.index().reach(feeder.downstreamId());
int level = downstream.grade()[0];
Integer pondLevel = outletLevel(downstream);
Integer pondLevel = outletLevel(downstream, home.plan().key());
if (pondLevel != null) {
level = Math.min(level, pondLevel);
}
@@ -356,23 +341,38 @@ final class ChunkFeatures {
receiving.add(LinkBlend.footprint(sibling));
}
}
return LinkBlend.settleInto(linked, level, receiving);
return LinkBlend.settleInto(linked, level, receiving, lips(feeder, owner));
}
private IndexedWindow ownerOf(long id) {
IndexedWindow found = null;
for (IndexedWindow window : windows.values()) {
if (window.index().reach(id) == null) {
continue;
}
if (found != null) {
throw new IllegalStateException("Reach " + id + " is owned by window " + found.plan().key() + " and by window "
+ window.plan().key());
}
found = window;
}
return found;
}
private Reach feederLink(Reach downstream, WindowKey owner) {
int junction = junction(downstream);
WindowKey link = windowAt(downstream, junction);
List<Reach> owned = feeders(link, downstream.id());
if (owned.isEmpty() && junction > 0) {
List<Reach> joining = feeders(link, downstream.id());
if (joining.isEmpty() && junction > 0) {
link = windowAt(downstream, 0);
owned = feeders(link, downstream.id());
joining = feeders(link, downstream.id());
}
if (owned.isEmpty()) {
if (joining.isEmpty()) {
return downstream;
}
long block = cornerBlock(downstream);
List<Reach> feeders = new ArrayList<>(owned.size());
for (Reach reach : owned) {
List<Reach> feeders = new ArrayList<>(joining.size());
for (Reach reach : joining) {
feeders.add(cornerBlock(reach) == block ? resolve(reach, link) : reach);
}
return link.equals(owner) ? LinkBlend.clamp(downstream, feeders) : LinkBlend.blend(downstream, feeders, junction);
@@ -387,33 +387,62 @@ final class ChunkFeatures {
return source(reach) == null ? 0 : 1;
}
private Integer outletLevel(Reach reach) {
List<Pond> ponds = outletPonds.get(reach.id());
if (ponds == null) {
return null;
private Integer outletLevel(Reach outlet, WindowKey owner) {
Pond pond = outletPond(outlet, owner);
if (pond != null) {
return pond.level();
}
Pond source = source(reach);
if (source != null) {
return source.level();
}
int level = Integer.MAX_VALUE;
for (Pond pond : ponds) {
level = Math.min(level, pond.level());
Integer level = null;
for (IndexedWindow window : windows.values()) {
for (Pond shared : window.index().outletPonds(outlet.id())) {
level = level == null ? shared.level() : Math.min(level, shared.level());
}
}
return level;
}
private Pond source(Reach reach) {
List<Pond> ponds = outletPonds.get(reach.id());
if (ponds == null) {
return null;
private List<PondMask> lips(Reach outlet, WindowKey owner) {
Pond pond = outletPond(outlet, owner);
if (pond != null) {
return List.of(pond.mask());
}
int x = Math.round(reach.xs()[0]);
int z = Math.round(reach.zs()[0]);
List<PondMask> lips = new ArrayList<>();
for (IndexedWindow window : windows.values()) {
for (Pond shared : window.index().outletPonds(outlet.id())) {
lips.add(shared.mask());
}
}
return lips;
}
private Pond outletPond(Reach outlet, WindowKey owner) {
Pond source = source(outlet);
if (source != null) {
return source;
}
Pond last = null;
for (IndexedWindow window : windows.values()) {
List<Pond> ponds = window.index().outletPonds(outlet.id());
if (ponds.isEmpty()) {
continue;
}
if (!window.plan().key().equals(owner)) {
return null;
}
last = ponds.get(ponds.size() - 1);
}
return last;
}
private Pond source(Reach outlet) {
int x = Math.round(outlet.xs()[0]);
int z = Math.round(outlet.zs()[0]);
Pond source = null;
for (Pond pond : ponds) {
if (pond.mask().contains(x, z) && (source == null || lower(pond, source))) {
source = pond;
for (IndexedWindow window : windows.values()) {
for (Pond pond : window.index().outletPonds(outlet.id())) {
if (pond.mask().contains(x, z) && (source == null || lower(pond, source))) {
source = pond;
}
}
}
return source;
@@ -1,6 +1,7 @@
package art.arcane.iris.generation.hydrology.raster;
import art.arcane.iris.generation.hydrology.water.ConfluenceGrader;
import art.arcane.iris.generation.hydrology.water.PondMask;
import art.arcane.iris.generation.hydrology.water.Reach;
import art.arcane.iris.generation.hydrology.water.ReachShaper;
import art.arcane.iris.generation.hydrology.water.ReachStyle;
@@ -56,9 +57,9 @@ public final class LinkBlend {
return grade == downstream.grade() ? downstream : regraded(downstream, grade, downstream.halfWidth(), downstream.depth());
}
static Reach settleInto(Reach feeder, int level, List<ConfluenceGrader.Footprint> receiving) {
static Reach settleInto(Reach feeder, int level, List<ConfluenceGrader.Footprint> receiving, List<PondMask> lips) {
int[] grade = feeder.grade();
boolean[] fixed = fixed(feeder);
boolean[] fixed = fixed(feeder, lips);
int waterfallMinimumDrop = feeder.tier().waterfallMinimumDrop();
int[] settled = ConfluenceGrader.settle(footprint(feeder), grade, fixed, receiving, level, waterfallMinimumDrop);
if (settled == grade) {
@@ -74,16 +75,27 @@ public final class LinkBlend {
return new ConfluenceGrader.Footprint(reach.xs(), reach.zs(), reach.halfWidth());
}
private static boolean[] fixed(Reach feeder) {
private static boolean[] fixed(Reach feeder, List<PondMask> lips) {
ReachStyle[] style = feeder.style();
boolean[] fixed = new boolean[style.length];
boolean held = !lips.isEmpty();
fixed[0] = true;
for (int point = 1; point < style.length; point++) {
fixed[point] = style[point] == ReachStyle.POND_OUTLET;
held = held && nearLip(lips, feeder.xs()[point], feeder.zs()[point], feeder.halfWidth()[point]);
fixed[point] = held || style[point] == ReachStyle.POND_OUTLET;
}
return fixed;
}
private static boolean nearLip(List<PondMask> lips, float x, float z, float halfWidth) {
for (PondMask lip : lips) {
if (ReachShaper.nearLip(lip, x, z, halfWidth)) {
return true;
}
}
return false;
}
private static Reach regraded(Reach reach, int[] grade, float[] halfWidth, float[] depth) {
ReachStyle[] style = grade == reach.grade() ? reach.style() : restyled(reach.style(), grade, reach.tier().waterfallMinimumDrop());
return new Reach(reach.id(), reach.tier(), reach.downstreamId(), reach.xs(), reach.zs(), grade, halfWidth, depth, style,
@@ -26,6 +26,8 @@ public final class ReachIndex {
private final Buckets reachBuckets;
private final Buckets pondBuckets;
private final Map<Long, List<Reach>> feeders;
private final Map<Long, Reach> byId;
private final Map<Long, List<Pond>> outlets;
private ReachIndex(List<Reach> reaches, List<Pond> ponds) {
this.reaches = List.copyOf(reaches);
@@ -53,6 +55,8 @@ public final class ReachIndex {
this.reachBuckets = buckets(corridors.length, this::visitReach);
this.pondBuckets = buckets(footprints.length, this::visitPond);
this.feeders = feeders(this.reaches);
this.byId = byId(this.reaches);
this.outlets = outlets(this.ponds);
}
public static ReachIndex build(List<Reach> reaches, List<Pond> ponds) {
@@ -90,6 +94,14 @@ public final class ReachIndex {
return feeders.getOrDefault(downstreamId, List.of());
}
public Reach reach(long id) {
return byId.get(id);
}
public List<Pond> outletPonds(long outletReachId) {
return outlets.getOrDefault(outletReachId, List.of());
}
public static ReachHit nearest(List<Reach> candidates, double x, double z) {
int segments = 0;
for (Reach reach : candidates) {
@@ -116,6 +128,30 @@ public final class ReachIndex {
return Map.copyOf(frozen);
}
private static Map<Long, Reach> byId(List<Reach> reaches) {
Map<Long, Reach> byId = new HashMap<>();
for (Reach reach : reaches) {
if (byId.putIfAbsent(reach.id(), reach) != null) {
throw new IllegalStateException("Reach id " + reach.id() + " appears twice in one plan");
}
}
return Map.copyOf(byId);
}
private static Map<Long, List<Pond>> outlets(List<Pond> ponds) {
Map<Long, List<Pond>> grouped = new HashMap<>();
for (Pond pond : ponds) {
if (pond.outletReachId() != 0L) {
grouped.computeIfAbsent(pond.outletReachId(), ignored -> new ArrayList<>()).add(pond);
}
}
Map<Long, List<Pond>> frozen = new HashMap<>();
for (Map.Entry<Long, List<Pond>> entry : grouped.entrySet()) {
frozen.put(entry.getKey(), List.copyOf(entry.getValue()));
}
return Map.copyOf(frozen);
}
private static float[] corridor(Reach reach) {
int segments = reach.xs().length - 1;
float[] boxes = new float[segments * 4];
@@ -289,14 +289,17 @@ public final class ReachShaper {
draft.held = new boolean[draft.xs.length];
draft.held[0] = true;
for (int point = 1; point < draft.xs.length; point++) {
float radius = draft.tier.halfWidth(draft.catchments[point]) + 1f + LIP_MARGIN;
if (mask.nearest(draft.xs[point], draft.zs[point], radius) == null) {
if (!nearLip(mask, draft.xs[point], draft.zs[point], draft.tier.halfWidth(draft.catchments[point]))) {
return;
}
draft.held[point] = true;
}
}
public static boolean nearLip(PondMask mask, float x, float z, float halfWidth) {
return mask.nearest(x, z, halfWidth + 1f + LIP_MARGIN) != null;
}
private static void leadIn(ReachDraft draft, PondMask mask) {
if (mask.contains(Math.round(draft.xs[0]), Math.round(draft.zs[0]))) {
return;
@@ -765,6 +765,117 @@ public class ChunkWaterRasterTest {
assertEquals("a waterfall clamped to a 3-block drop is a cascade", WaterRole.CASCADE, clamped.role(519, 200));
}
@Test
public void crossWindowFeederSettlesToItsDownstreamOutletsPond() {
HydrologyTier river = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH);
Reach outlet = ledIn(line(281L, river, 0L, 508f, 200f, 600f, 200f, (x, z) -> 68), 503f, 200f);
Reach feeder = line(282L, river, outlet.id(), 508f, 120f, 508f, 200f, (x, z) -> 68);
Pond pond = new Pond(280L, PondKind.NATURAL, 64, 56, 504, 200, 65.5f, circle(488, 200, 16, 1), outlet.id(), 0, "water", 488, 200);
assertTrue("the lead-in starts in the pond", pond.mask().contains(503, 200));
assertEquals(new WindowKey(0, 0), WindowKey.containing(508, 200));
assertEquals(new WindowKey(1, 0), WindowKey.containing(600, 200));
assertTrue("the pond sits below the outlet's planned start", pond.level() < outlet.grade()[0]);
Scene scene = new Scene(settings(new IrisHydrology()), (x, z) -> 75).reach(outlet).reach(feeder).pond(pond);
int previous = Integer.MAX_VALUE;
for (int z = 120; z <= 199; z++) {
int head = scene.head(508, z);
assertNotEquals("feeder centre line is water at z=" + z, Integer.MIN_VALUE, head);
assertTrue("water rises from " + previous + " to " + head + " at z=" + z, head <= previous);
if (z >= 194) {
assertTrue("feeder water at " + head + " above the outlet's pond " + pond.level() + " at z=" + z, head <= pond.level());
}
previous = head;
}
for (int x = 508; x <= 600; x++) {
assertTrue("outlet water above its pond at x=" + x, scene.head(x, 200) <= pond.level());
}
}
@Test
public void fullyPooledShortFeederKeepsItsStartForItsOwnFeeders() {
HydrologyTier narrow = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH);
HydrologyTier wide = tier("river", 3f, 0.5f, 4, IrisBankStyle.SMOOTH);
Reach low = line(292L, wide, 0L, 504f, 200f, 600f, 200f, (x, z) -> 62);
Reach stub = line(291L, narrow, low.id(), 498f, 200f, 504f, 200f, (x, z) -> 66);
Reach upstream = line(290L, narrow, stub.id(), 440f, 200f, 498f, 200f, (x, z) -> 66);
assertEquals(4, stub.xs().length);
Scene scene = new Scene(settings(new IrisHydrology()), (x, z) -> 90).reach(upstream).reach(stub).reach(low);
assertEquals("the stub keeps its start level for the reach that feeds it", 66, scene.head(498, 200));
assertEquals(62, scene.head(499, 200));
assertEquals("the stub drops inside itself as a cascade", WaterRole.CASCADE, scene.role(499, 200));
int previous = Integer.MAX_VALUE;
for (int x = 440; x <= 600; x++) {
int head = scene.head(x, 200);
assertNotEquals("centre line is water at x=" + x, Integer.MIN_VALUE, head);
assertTrue("water rises from " + previous + " to " + head + " at x=" + x, head <= previous);
previous = head;
}
}
@Test
public void crossWindowSettlingKeepsAnOutletsHeldLipAtThePondLevel() {
HydrologyTier narrow = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH);
HydrologyTier wide = tier("river", 3f, 0.5f, 4, IrisBankStyle.SMOOTH);
Reach low = line(322L, wide, 0L, 504f, 200f, 600f, 200f, (x, z) -> 62);
Reach shore = line(321L, narrow, low.id(), 486f, 200f, 504f, 200f, (x, z) -> 66);
ReachStyle[] style = shore.style().clone();
for (int point = 0; point < style.length; point++) {
style[point] = shore.xs()[point] - shore.xs()[0] <= 8f ? ReachStyle.POND_OUTLET : style[point];
}
Reach outlet = new Reach(shore.id(), shore.tier(), shore.downstreamId(), shore.xs(), shore.zs(), shore.grade(), shore.halfWidth(),
shore.depth(), style, shore.catchment());
Pond pond = new Pond(320L, PondKind.NATURAL, 66, 58, 490, 202, 67.5f, circle(490, 210, 8, 1), outlet.id(), 0, "water", 490, 210);
assertFalse("the outlet is not led in", pond.mask().contains(486, 200));
assertEquals("the lip vertices past the outlet zone run", ReachStyle.RUN, outlet.style()[7]);
Scene scene = new Scene(settings(new IrisHydrology()), (x, z) -> 75).reach(outlet).reach(low).pond(pond);
for (int x = 487; x <= 500; x++) {
assertEquals("outlet water along its pond's lip at x=" + x, pond.level(), scene.head(x, 200));
}
assertEquals("the outlet drops into the lower river past its lip", WaterRole.CASCADE, scene.role(501, 200));
}
@Test
public void sharedOutletWithoutALeadInClampsToTheLastPondInItsPlan() {
HydrologyTier river = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH);
Reach outlet = line(311L, river, 0L, 300f, 200f, 380f, 200f, (x, z) -> 68);
Pond low = new Pond(310L, PondKind.NATURAL, 64, 56, 290, 200, 65.5f, circle(280, 200, 10, 1), outlet.id(), 0, "water", 280, 200);
Pond high = new Pond(312L, PondKind.NATURAL, 68, 60, 300, 224, 69.5f, circle(300, 230, 6, 1), outlet.id(), 0, "water", 300, 230);
assertFalse(low.mask().contains(300, 200));
assertFalse(high.mask().contains(300, 200));
for (boolean highLast : new boolean[]{true, false}) {
Scene scene = new Scene(settings(new IrisHydrology()), (x, z) -> 75).reach(outlet);
if (highLast) {
scene.pond(low).pond(high);
} else {
scene.pond(high).pond(low);
}
int expected = highLast ? high.level() : low.level();
for (int x = 302; x <= 320; x++) {
assertEquals("outlet water with the " + (highLast ? "higher" : "lower") + " pond last at x=" + x, expected, scene.head(x, 200));
}
}
}
@Test
public void aDownstreamReachOwnedByTwoWindowsFailsClosed() {
HydrologyTier river = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH);
Reach downstream = line(332L, river, 0L, 504f, 200f, 600f, 200f, (x, z) -> 62);
Reach feeder = line(331L, river, downstream.id(), 440f, 200f, 504f, 200f, (x, z) -> 66);
List<IndexedWindow> windows = new ArrayList<>();
for (int dz = -1; dz <= 1; dz++) {
for (int dx = -1; dx <= 1; dx++) {
WindowKey key = new WindowKey(dx, dz);
List<Reach> owned = key.equals(new WindowKey(0, 0)) ? List.of(feeder)
: key.equals(new WindowKey(1, 0)) || key.equals(new WindowKey(1, -1)) ? List.of(downstream) : List.of();
windows.add(new IndexedWindow(new WindowPlan(key, owned, List.of(), List.of()), ReachIndex.build(owned, List.of())));
}
}
ChunkRasterizer rasterizer = new ChunkRasterizer(settings(new IrisHydrology()), ChannelFluids.TIER);
IllegalStateException failure = assertThrows(IllegalStateException.class,
() -> rasterizer.rasterize(31, 12, (x, z) -> 75, windows, regions(31, 12, List.of())));
assertTrue(failure.getMessage(), failure.getMessage().contains("Reach " + downstream.id() + " is owned by window"));
}
@Test
public void crossWindowJunctionDropsSpreadOnTiersWithoutCascades() {
HydrologyTier gentle = new HydrologyTier("stream", 0f, 2f, 2f, 0f, 0.5f, 1, false, 6, 4, IrisBankStyle.SMOOTH, false,