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;