Lowest water owns confluences

- Add ReachCursor.Waters, which keeps the nearest covering segment per reach and resolves the channel owner by planned head, then wider half-width, distance, id and segment
- Offer covering segments to Waters in ReachSegments.evaluate and resolve the owner at the end of every nearest lookup
- Prune channel candidates by segment radius once the scope is CHANNEL or any water covers the column
- Cut channel and trench beds to the deepest bed among covering reaches whose snapped head equals the owner's head
- Add ChunkWaterRasterTest.receivingRiverOwnsItsFootprintAtAConfluence
- Flip the PlannedSceneTest confluence tongue cases to expect no tongue
- Expect the wider equal-level channel in ReachIndexTest and the lowest covering channel in PondChannelSettlementTest
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-01 16:41:40 -05:00
1 parent a06f487279
commit 06bd50d1a0
7 files changed
+168 -38

No files matched your search

@@ -132,7 +132,8 @@ final class RasterComposer {
if (hit.reach == null) {
return floor;
}
return Math.min(floor, bed(hit, Math.min(level, WaterHeads.snap(hit.reach.grade()[hit.point()], settings.seaLevel())), ground, variation, detail));
int ownerHead = WaterHeads.snap(hit.grade(), settings.seaLevel());
return Math.min(floor, deepestBed(ownerHead, Math.min(level, ownerHead), ground, variation, detail));
}
private void channel(int cell, int x, int z, int ground, double variation) {
@@ -140,7 +141,7 @@ final class RasterComposer {
Reach reach = hit.reach;
int point = hit.point();
head[cell] = WaterHeads.snap(reach.grade()[point], settings.seaLevel());
terrain[cell] = bed(hit, head[cell], ground, variation, BedProfile.detail(settings.seed(), x, z));
terrain[cell] = deepestBed(head[cell], head[cell], ground, variation, BedProfile.detail(settings.seed(), x, z));
role[cell] = role(reach.style()[point]);
feature[cell] = reach.id();
tier[cell] = reach.tier();
@@ -154,6 +155,18 @@ final class RasterComposer {
});
}
private int deepestBed(int ownerHead, int surface, int ground, double variation, double detail) {
ReachCursor.Waters waters = cursor.waters;
int deepest = Integer.MAX_VALUE;
for (int entry = 0; entry < waters.size(); entry++) {
ReachCursor.Choice water = waters.get(entry);
if (WaterHeads.snap(water.grade(), settings.seaLevel()) == ownerHead) {
deepest = Math.min(deepest, bed(water, surface, ground, variation, detail));
}
}
return deepest;
}
private static int bed(ReachCursor.Choice hit, int surface, int ground, double variation, double detail) {
double width = hit.halfWidth();
double ratio = width > 0D ? hit.distance / width : 0D;
@@ -2,17 +2,21 @@ package art.arcane.iris.generation.hydrology.raster;
import art.arcane.iris.generation.hydrology.water.Reach;
import java.util.Arrays;
final class ReachCursor {
final Choice channel = new Choice();
final Choice bank = new Choice();
final Choice nearest = new Choice();
final Choice edge = new Choice();
final Waters waters = new Waters();
void reset() {
channel.reset();
bank.reset();
nearest.reset();
edge.reset();
waters.reset();
}
ReachHit hit() {
@@ -43,11 +47,7 @@ final class ReachCursor {
void offer(Reach candidate, int candidateSegment, int candidateIndex, double candidateT, double candidateDistance) {
if (candidateDistance < distance || candidateDistance == distance && reach != null && (candidate.id() < reach.id()
|| candidate.id() == reach.id() && candidateSegment < segment)) {
reach = candidate;
segment = candidateSegment;
index = candidateIndex;
t = candidateT;
distance = candidateDistance;
set(candidate, candidateSegment, candidateIndex, candidateT, candidateDistance);
}
}
@@ -55,6 +55,10 @@ final class ReachCursor {
return t < 0.5D ? segment : segment + 1;
}
int grade() {
return reach.grade()[point()];
}
double halfWidth() {
float[] widths = reach.halfWidth();
return widths[segment] + t * (widths[segment + 1] - widths[segment]);
@@ -65,8 +69,96 @@ final class ReachCursor {
return depths[segment] + t * (depths[segment + 1] - depths[segment]);
}
private void set(Reach candidate, int candidateSegment, int candidateIndex, double candidateT, double candidateDistance) {
reach = candidate;
segment = candidateSegment;
index = candidateIndex;
t = candidateT;
distance = candidateDistance;
}
private ReachHit hit(ReachHit.Coverage coverage) {
return new ReachHit(reach, segment, (float) t, (float) distance, coverage);
}
}
static final class Waters {
private Choice[] entries = {new Choice(), new Choice(), new Choice(), new Choice()};
private int size;
void reset() {
size = 0;
}
boolean isEmpty() {
return size == 0;
}
int size() {
return size;
}
Choice get(int entry) {
return entries[entry];
}
void offer(Reach reach, int segment, int index, double t, double distance) {
for (int entry = 0; entry < size; entry++) {
Choice held = entries[entry];
if (held.reach.id() == reach.id()) {
if (distance < held.distance || distance == held.distance && segment < held.segment) {
held.set(reach, segment, index, t, distance);
}
return;
}
}
if (size == entries.length) {
grow();
}
entries[size++].set(reach, segment, index, t, distance);
}
void resolve(Choice channel) {
channel.reset();
if (size == 0) {
return;
}
Choice owner = entries[0];
for (int entry = 1; entry < size; entry++) {
if (owns(entries[entry], owner)) {
owner = entries[entry];
}
}
channel.set(owner.reach, owner.segment, owner.index, owner.t, owner.distance);
}
private static boolean owns(Choice candidate, Choice held) {
int candidateGrade = candidate.grade();
int heldGrade = held.grade();
if (candidateGrade != heldGrade) {
return candidateGrade < heldGrade;
}
double candidateWidth = candidate.halfWidth();
double heldWidth = held.halfWidth();
if (candidateWidth != heldWidth) {
return candidateWidth > heldWidth;
}
if (candidate.distance != held.distance) {
return candidate.distance < held.distance;
}
if (candidate.reach.id() != held.reach.id()) {
return candidate.reach.id() < held.reach.id();
}
return candidate.segment < held.segment;
}
private void grow() {
int grown = 2 * entries.length;
Choice[] larger = Arrays.copyOf(entries, grown);
for (int entry = entries.length; entry < grown; entry++) {
larger[entry] = new Choice();
}
entries = larger;
}
}
}
@@ -119,24 +119,25 @@ final class ReachSegments {
evaluate(index, x, z, false, cursor);
}
}
return;
}
int first = 0;
for (int run = 0; run < runs; run++) {
int end = runEnds[run];
int box = 4 * run;
if (beyond(x, z, runBoxes[box], runBoxes[box + 1], runBoxes[box + 2], runBoxes[box + 3],
limit(runRadius[run], runBands[run], scope, cursor))) {
first = end;
continue;
}
for (int index = first; index < end; index++) {
if (!beyondSegment(index, x, z, scope, cursor) && index != channelHint && index != bankHint && index != edgeHint) {
evaluate(index, x, z, unbounded, cursor);
} else {
int first = 0;
for (int run = 0; run < runs; run++) {
int end = runEnds[run];
int box = 4 * run;
if (beyond(x, z, runBoxes[box], runBoxes[box + 1], runBoxes[box + 2], runBoxes[box + 3],
limit(runRadius[run], runBands[run], scope, cursor))) {
first = end;
continue;
}
for (int index = first; index < end; index++) {
if (!beyondSegment(index, x, z, scope, cursor) && index != channelHint && index != bankHint && index != edgeHint) {
evaluate(index, x, z, unbounded, cursor);
}
}
first = end;
}
first = end;
}
cursor.waters.resolve(cursor.channel);
}
private int coveredCell(double x, double z) {
@@ -163,8 +164,8 @@ final class ReachSegments {
}
private static double limit(double radius, int band, Scope scope, ReachCursor cursor) {
if (scope == Scope.CHANNEL || cursor.channel.reach != null) {
return Math.min(radius, cursor.channel.distance);
if (scope == Scope.CHANNEL || !cursor.waters.isEmpty()) {
return radius;
}
double limit = Math.max(radius, cursor.bank.reach == null ? radius + band : Math.min(radius + band, cursor.bank.distance));
return switch (scope) {
@@ -200,7 +201,7 @@ final class ReachSegments {
cursor.nearest.offer(reach, segment, index, t, distance);
}
if (distance <= width) {
cursor.channel.offer(reach, segment, index, t, distance);
cursor.waters.offer(reach, segment, index, t, distance);
} else if (distance <= width + bands[index]) {
cursor.bank.offer(reach, segment, index, t, distance);
}
@@ -50,19 +50,19 @@ public class PlannedSceneTest {
}
@Test
public void reproducesTodaysConfluenceTongue() {
public void confluenceHasNoTongueAtEitherTributaryLevel() {
HydrologySettings settings = PlannedScene.packSettings(SEED);
for (int tributaryLevel : new int[]{70, 66}) {
List<Long> tongues = tongues(settings, new int[]{144, 136}, new int[]{144, 56}, (x, z) -> z >= 136 ? 66 : tributaryLevel, true);
assertEquals("tributary at " + tributaryLevel, tributaryLevel == 70 ? List.of(303L) : List.of(), tongues);
assertEquals("tributary at " + tributaryLevel, List.of(), tongues);
}
}
@Test
public void confluenceTonguesCoverSiblingFootprintsOutsideTheDownstreamCap() {
public void confluenceHasNoTongueInsideTheSiblingChannel() {
HydrologySettings settings = PlannedScene.packSettings(SEED);
IntBinaryOperator perchedUpstream = (x, z) -> x >= 128 ? 66 : 70;
assertEquals("perched tributary inside the sibling channel", List.of(303L),
assertEquals("perched tributary inside the sibling channel", List.of(),
tongues(settings, new int[]{144, 136}, new int[]{48, 112}, perchedUpstream, true));
assertEquals("without the sibling the perched stretch lies outside the downstream cap", List.of(),
tongues(settings, new int[]{144, 136}, new int[]{48, 112}, perchedUpstream, false));
@@ -588,6 +588,27 @@ public class ChunkWaterRasterTest {
}
}
@Test
public void receivingRiverOwnsItsFootprintAtAConfluence() {
HydrologyTier river = tier("river", 8f, 1f, 16, IrisBankStyle.SMOOTH);
HydrologyTier stream = tier("stream", 2f, 0.5f, 4, IrisBankStyle.SMOOTH);
Reach below = line(302L, river, 0L, 16f, 8f, 120f, 8f, (x, z) -> 66);
Reach main = line(301L, river, below.id(), -96f, 8f, 16f, 8f, (x, z) -> 66);
for (int tributaryLevel : new int[]{70, 66}) {
Reach tributary = line(303L, stream, below.id(), 16f, -72f, 16f, 8f, (x, z) -> z >= 8f ? 66 : tributaryLevel);
Scene scene = new Scene(settings(new IrisHydrology()), (x, z) -> 75).reach(main).reach(tributary).reach(below);
for (int x = -16; x <= 48; x++) {
for (int z = 0; z <= 16; z++) {
assertEquals("river head at " + x + "," + z, 66, scene.head(x, z));
if (tributaryLevel == 66 && Math.abs(z - 8) <= 2) {
assertTrue("river bed under the tributary mouth at " + x + "," + z, scene.terrain(x, z) <= 63);
}
}
}
assertEquals("the tributary keeps its own water upstream", tributaryLevel, scene.head(16, -24));
}
}
@Test
public void crossWindowLinkNeverRaisesWaterDownstream() {
HydrologyTier narrow = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH);
@@ -60,16 +60,17 @@ public class PondChannelSettlementTest {
}
@Test
public void nearerChannelsAndIdentityTiesMatchTheRasterWinner() {
public void theLowestCoveringChannelSettlesThePondLikeTheRasterOwner() {
Pond pond = pond(1L, PondKind.CHAINED, 100, circle(0, 0, 4));
Reach lower = line(4L, 2, -20, 2, 20, 8f, 94, 94);
Reach nearer = line(3L, 0, -20, 0, 20, 8f, 100, 100);
Pond single = pond(2L, PondKind.NATURAL, 100, circle(0, 0, 0));
Reach tied = line(3L, 2, -20, 2, 20, 8f, 100, 100);
Reach alongside = line(3L, 2, -20, 2, 20, 8f, 100, 100);
assertEquals(100, settle(List.of(lower, nearer), single));
assertEquals(100, settle(List.of(lower, tied), pond));
assertEquals(100, settle(List.of(tied, lower), pond));
assertEquals(94, settle(List.of(lower, nearer), single));
assertEquals(94, settle(List.of(nearer, lower), single));
assertEquals(94, settle(List.of(lower, alongside), pond));
assertEquals(94, settle(List.of(alongside, lower), pond));
}
@Test
@@ -117,10 +117,12 @@ public class ReachIndexTest {
assertEquals(0.85f, hit.t(), 1e-6f);
assertEquals(8f, hit.distance(), 1e-5f);
}
ReachHit both = ReachIndex.nearest(List.of(trunk, tributary), 20.5D, 6D);
assertSame("inside both channels the nearer covering centre wins", tributary, both.reach());
assertEquals(ReachHit.Coverage.CHANNEL, both.coverage());
assertEquals(0.5f, both.distance(), 1e-5f);
for (List<Reach> candidates : List.of(List.of(tributary, trunk), List.of(trunk, tributary))) {
ReachHit both = ReachIndex.nearest(candidates, 20.5D, 6D);
assertSame("inside both channels at one level the wider channel owns the column", trunk, both.reach());
assertEquals(ReachHit.Coverage.CHANNEL, both.coverage());
assertEquals(6f, both.distance(), 1e-5f);
}
}
@Test