Resolve cut-off inlet competitors

- Let a cut-off inlet still make a later conflicting inlet yield, and never list it
- Throw when a window's own inlet reaches the edge of its planning span
- Check the planner's chamber and step lookups against the sea-inlet extent guard
- Remove the reach ownership filter, which never removed a span reach
- Plan inlet tests on window spans and add bay, guard, edge and fail-closed tests
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-01 19:56:27 -05:00
1 parent 21ed677bf4
commit 0ca3a61046
4 files changed
+183 -61

No files matched your search

@@ -19,6 +19,7 @@ import art.arcane.iris.generation.hydrology.config.IrisPondNatural;
import art.arcane.iris.generation.hydrology.config.IrisPondSeeded;
import art.arcane.iris.generation.hydrology.config.IrisPondShore;
import art.arcane.iris.generation.hydrology.config.IrisSeaInlets;
import art.arcane.iris.generation.hydrology.underground.SeaInletPlanner;
import art.arcane.iris.generation.hydrology.underground.SeaInletRasterizer;
import art.arcane.iris.generation.hydrology.water.HydrologyTier;
import art.arcane.iris.generation.noise.IrisGeneratorStyle;
@@ -309,6 +310,12 @@ public final class HydrologySettings {
+ inlets.length().max() + " + " + INLET_LAST_STEP + " for the last step + the wider of halfWidth max and chamberRadius max"
+ " + " + (1 + INLET_SHELL) + " for the seal); an inlet must stay within " + WindowKey.SIZE + " blocks");
}
long lookups = SeaInletPlanner.lookupReach(inlets.length().max(), inlets.chamberRadius().max());
if (lookups > WindowKey.SIZE) {
throw new IllegalArgumentException("Hydrology underground.seaInlets planning looks " + lookups
+ " blocks from the coast (length max " + inlets.length().max() + ", chamberRadius max " + inlets.chamberRadius().max()
+ "); it must stay within " + WindowKey.SIZE + " blocks");
}
return inlets;
}
@@ -5,6 +5,7 @@ import art.arcane.iris.generation.hydrology.grid.DemGrid;
import art.arcane.iris.generation.hydrology.grid.HydrologySampleBuffer;
import art.arcane.iris.generation.hydrology.plan.HydrologySettings;
import art.arcane.iris.generation.hydrology.plan.WindowKey;
import art.arcane.iris.generation.hydrology.plan.WindowPlanner;
import art.arcane.iris.generation.hydrology.water.Pond;
import art.arcane.iris.generation.hydrology.water.PondMask;
import art.arcane.iris.generation.hydrology.water.Reach;
@@ -20,6 +21,7 @@ import java.util.TreeMap;
public final class SeaInletPlanner {
private static final int ROOF_MARGIN = 2;
private static final int OPEN_LAND_STATIONS = 2;
private static final int STEP_LOOKUP_CELLS = 2;
private static final float NOISE_STEP = 32f;
private static final int CONTAINMENT = 2;
private static final float WET_SLACK = CONTAINMENT + 1 + 6f;
@@ -41,6 +43,12 @@ public final class SeaInletPlanner {
this.settings = Objects.requireNonNull(settings, "settings");
}
public static long lookupReach(int lengthMax, int chamberRadiusMax) {
int cell = WindowPlanner.CELL;
long farthestStation = cell * (long) Math.floor((lengthMax + cell * Math.sqrt(2D) - cell) / cell);
return farthestStation + (long) cell * Math.max(STEP_LOOKUP_CELLS, chamberRing(chamberRadiusMax, cell));
}
public List<SeaInlet> plan(WindowKey window, DemGrid raw, HydrologySampleBuffer samples, int[] receivers, float[] accumulation,
ShapedNetwork network, long seed) {
Objects.requireNonNull(window, "window");
@@ -53,32 +61,37 @@ public final class SeaInletPlanner {
throw new IllegalArgumentException("Sea inlet planning needs " + count + " cells, got samples=" + samples.height().length
+ " receivers=" + receivers.length + " accumulation=" + accumulation.length);
}
Terrain terrain = new Terrain(raw, samples, receivers, accumulation, cover(window, raw, samples.height(), network), seed);
List<SeaInlet> inlets = new ArrayList<>();
Terrain terrain = new Terrain(raw, samples, receivers, accumulation, cover(raw, samples.height(), network), seed);
List<Traced> traced = new ArrayList<>();
for (Map.Entry<Long, List<Integer>> site : sites(terrain, config).entrySet()) {
SeaInlet inlet = site(terrain, config, site.getKey(), site.getValue());
if (inlet != null && (!terrain.clipped || window.equals(owner(inlet)))) {
inlets.add(inlet);
if (inlet == null) {
continue;
}
if (terrain.clipped && window.equals(owner(inlet))) {
throw new IllegalStateException("Sea inlet " + inlet.id() + " of window " + window
+ " reaches the edge of the window's planning span");
}
traced.add(new Traced(inlet, !terrain.clipped));
}
inlets.sort(Comparator.comparingLong(SeaInlet::id).thenComparingDouble(inlet -> inlet.xs()[0])
.thenComparingDouble(inlet -> inlet.zs()[0]));
return separated(inlets);
traced.sort(Comparator.comparingLong((Traced entry) -> entry.inlet().id()).thenComparingDouble(entry -> entry.inlet().xs()[0])
.thenComparingDouble(entry -> entry.inlet().zs()[0]));
return separated(traced);
}
private static List<SeaInlet> separated(List<SeaInlet> inlets) {
List<SeaInletFootprint> footprints = new ArrayList<>(inlets.size());
for (SeaInlet inlet : inlets) {
footprints.add(SeaInletFootprint.of(inlet));
private static List<SeaInlet> separated(List<Traced> traced) {
List<SeaInletFootprint> footprints = new ArrayList<>(traced.size());
for (Traced entry : traced) {
footprints.add(SeaInletFootprint.of(entry.inlet()));
}
List<SeaInlet> kept = new ArrayList<>(inlets.size());
for (int index = 0; index < inlets.size(); index++) {
List<SeaInlet> kept = new ArrayList<>(traced.size());
for (int index = 0; index < traced.size(); index++) {
boolean yields = false;
for (int earlier = 0; earlier < index && !yields; earlier++) {
yields = footprints.get(earlier).conflicts(footprints.get(index));
}
if (!yields) {
kept.add(inlets.get(index));
if (!yields && traced.get(index).whole()) {
kept.add(traced.get(index).inlet());
}
}
return List.copyOf(kept);
@@ -111,7 +124,7 @@ public final class SeaInletPlanner {
double targetZ = (siteZ + CellHash.unit(CellHash.mix(terrain.seed, SALT_SITE_Z, siteX, siteZ))) * config.spacing();
DemGrid grid = terrain.grid;
WindowKey owner = WindowKey.containing((int) Math.floor(targetX), (int) Math.floor(targetZ));
terrain.clipped = reachesEdge(terrain, config, siteX, siteZ, owner);
terrain.clipped = siteTouchesBorder(terrain, config, siteX, siteZ, owner);
List<Integer> ordered = new ArrayList<>(candidates.size());
for (int candidate : candidates) {
if (owner.equals(WindowKey.containing(grid.blockX(candidate), grid.blockZ(candidate)))) {
@@ -182,7 +195,7 @@ public final class SeaInletPlanner {
int radius = 0;
int preferred = Math.round(lerp(config.chamberRadius().min(), config.chamberRadius().max(), unit(terrain, SALT_RADIUS, end)));
for (int candidate : new int[]{preferred, config.chamberRadius().min()}) {
float capacity = capacity(terrain, config, end, (candidate + grid.cell() - 1) / grid.cell() + 1);
float capacity = capacity(terrain, config, end, chamberRing(candidate, grid.cell()));
if (capacity - water >= config.headroom().min()) {
radius = candidate;
chamberCapacity = capacity;
@@ -362,15 +375,12 @@ public final class SeaInletPlanner {
return best;
}
private Cover cover(WindowKey window, DemGrid grid, float[] natural, ShapedNetwork network) {
private Cover cover(DemGrid grid, float[] natural, ShapedNetwork network) {
int count = grid.values().length;
float[] cover = new float[count];
System.arraycopy(natural, 0, cover, 0, count);
boolean[] wet = new boolean[count];
for (Reach reach : network.reaches()) {
if (!nearby(window, owner(reach))) {
continue;
}
int band = reach.tier().bankBand() + CONTAINMENT + grid.cell();
for (int point = 0; point + 1 < reach.xs().length; point++) {
float bed = Math.min(reach.grade()[point] - reach.depth()[point], reach.grade()[point + 1] - reach.depth()[point + 1]) - 1f;
@@ -448,33 +458,34 @@ public final class SeaInletPlanner {
return neighbour;
}
private boolean reachesEdge(Terrain terrain, HydrologySettings.SeaInlets config, int siteX, int siteZ, WindowKey owner) {
private boolean siteTouchesBorder(Terrain terrain, HydrologySettings.SeaInlets config, int siteX, int siteZ, WindowKey owner) {
DemGrid grid = terrain.grid;
long[] columns = siteCells(siteX, config.spacing(), owner.wx(), grid.originX(), grid.cell());
long[] rows = siteCells(siteZ, config.spacing(), owner.wz(), grid.originZ(), grid.cell());
if (columns[0] < 0L || rows[0] < 0L || columns[1] >= grid.width() || rows[1] >= grid.depth()) {
CellRange columns = siteCells(siteX, config.spacing(), owner.wx(), grid.originX(), grid.cell());
CellRange rows = siteCells(siteZ, config.spacing(), owner.wz(), grid.originZ(), grid.cell());
int lastColumn = grid.width() - 1;
int lastRow = grid.depth() - 1;
if (columns.first() < 0L || rows.first() < 0L || columns.last() > lastColumn || rows.last() > lastRow) {
return true;
}
float cliff = settings.seaLevel() + config.minimumCliffHeight();
int lastColumn = grid.width() - 1;
int lastRow = grid.depth() - 1;
for (int i = (int) columns[0]; i <= columns[1]; i++) {
if (rows[0] == 0L && cliffLand(terrain, i, 0, cliff) || rows[1] == lastRow && cliffLand(terrain, i, lastRow, cliff)) {
for (int i = (int) columns.first(); i <= columns.last(); i++) {
if (rows.first() == 0L && cliffLand(terrain, i, 0, cliff) || rows.last() == lastRow && cliffLand(terrain, i, lastRow, cliff)) {
return true;
}
}
for (int j = (int) rows[0]; j <= rows[1]; j++) {
if (columns[0] == 0L && cliffLand(terrain, 0, j, cliff) || columns[1] == lastColumn && cliffLand(terrain, lastColumn, j, cliff)) {
for (int j = (int) rows.first(); j <= rows.last(); j++) {
if (columns.first() == 0L && cliffLand(terrain, 0, j, cliff)
|| columns.last() == lastColumn && cliffLand(terrain, lastColumn, j, cliff)) {
return true;
}
}
return false;
}
private static long[] siteCells(int site, int spacing, int window, int origin, int cell) {
private static CellRange siteCells(int site, int spacing, int window, int origin, int cell) {
long from = Math.max((long) site * spacing, (long) window * WindowKey.SIZE);
long to = Math.min(((long) site + 1L) * spacing, ((long) window + 1L) * WindowKey.SIZE);
return new long[]{Math.ceilDiv(from - origin, cell), Math.ceilDiv(to - origin, cell) - 1L};
return new CellRange(Math.ceilDiv(from - origin, cell), Math.ceilDiv(to - origin, cell) - 1L);
}
private static boolean cliffLand(Terrain terrain, int i, int j, float cliff) {
@@ -482,19 +493,14 @@ public final class SeaInletPlanner {
return !terrain.ocean(cell) && terrain.natural(cell) >= cliff;
}
private static int chamberRing(int radius, int cell) {
return (radius + cell - 1) / cell + 1;
}
private static WindowKey owner(SeaInlet inlet) {
return WindowKey.containing((int) Math.floor(inlet.xs()[1]), (int) Math.floor(inlet.zs()[1]));
}
private static WindowKey owner(Reach reach) {
int last = reach.xs().length - 1;
return WindowKey.containing((int) Math.floor(reach.xs()[last]), (int) Math.floor(reach.zs()[last]));
}
private static boolean nearby(WindowKey window, WindowKey other) {
return Math.abs(other.wx() - window.wx()) <= 1 && Math.abs(other.wz() - window.wz()) <= 1;
}
private static double squaredAbove(float radius) {
return (double) radius * radius * (1D + SQUARED_MARGIN);
}
@@ -536,6 +542,12 @@ public final class SeaInletPlanner {
private record Cover(float[] cover, boolean[] wet) {
}
private record Traced(SeaInlet inlet, boolean whole) {
}
private record CellRange(long first, long last) {
}
private static final class Terrain {
private final DemGrid grid;
private final HydrologySampleBuffer samples;
@@ -35,12 +35,16 @@ final class InletTerrain {
final float[] accumulation;
private InletTerrain(DoubleBinaryOperator natural) {
this(natural, 0, 0, WIDTH, DEPTH);
this(natural, new WindowKey(0, 0));
}
private InletTerrain(DoubleBinaryOperator natural, int originX, int originZ, int width, int depth) {
private InletTerrain(DoubleBinaryOperator natural, WindowKey window) {
this(natural, window, (window.wx() - 1) * WindowKey.SIZE, (window.wz() - 1) * WindowKey.SIZE, SPAN, SPAN);
}
private InletTerrain(DoubleBinaryOperator natural, WindowKey window, int originX, int originZ, int width, int depth) {
this.natural = natural;
this.window = WindowKey.containing(originX + width * CELL / 2, originZ + depth * CELL / 2);
this.window = window;
this.grid = new DemGrid(originX, originZ, CELL, width, depth);
int count = width * depth;
this.samples = new HydrologySampleBuffer(count);
@@ -76,6 +80,27 @@ final class InletTerrain {
return new InletTerrain((x, z) -> x < COAST_X ? SEA - 20D : SEA + cliffHeight + 0.08D * (x - COAST_X));
}
InletTerrain wide(WindowKey window, int windows) {
int cells = (2 * windows + 1) * WindowKey.SIZE / CELL;
int originX = (window.wx() - windows) * WindowKey.SIZE;
int originZ = (window.wz() - windows) * WindowKey.SIZE;
return new InletTerrain(natural, window, originX, originZ, cells, cells);
}
static InletTerrain bay(int westX, int eastX, int bayZ) {
return new InletTerrain((x, z) -> {
if (z > bayZ - CELL / 2D) {
return Math.abs(z - bayZ) < CELL / 2D && x > westX - CELL / 2D && x < eastX + CELL / 2D ? SEA - 20D : SEA + 5D;
}
return SEA + 30D + 0.08D * (bayZ - z);
});
}
static InletTerrain eastEdgeCoast() {
double coast = WindowKey.SIZE - CELL;
return new InletTerrain((x, z) -> x < coast - CELL / 2D ? SEA - 20D : SEA + 30D + 0.08D * (x - coast));
}
static InletTerrain openCoast() {
return new InletTerrain((x, z) -> {
double coast = 768D + 120D * Math.sin(z / 180D) + 40D * Math.sin(z / 53D);
@@ -134,12 +159,12 @@ final class InletTerrain {
return HydrologySettings.compile(config, SEED, SEA, MIN_Y, MAX_Y);
}
InletTerrain shifted(int originX, int originZ) {
return new InletTerrain(natural, originX, originZ, WIDTH, DEPTH);
InletTerrain span(WindowKey window) {
return new InletTerrain(natural, window);
}
InletTerrain span(WindowKey window) {
return new InletTerrain(natural, (window.wx() - 1) * WindowKey.SIZE, (window.wz() - 1) * WindowKey.SIZE, SPAN, SPAN);
InletTerrain narrow(int originX, int originZ) {
return new InletTerrain(natural, WindowKey.containing(originX, originZ), originX, originZ, WIDTH, DEPTH);
}
List<SeaInlet> plan(HydrologySettings settings, ShapedNetwork network) {
@@ -151,7 +176,7 @@ final class InletTerrain {
}
boolean ocean(float x, float z) {
int cell = grid.index(Math.round(x / CELL), Math.round(z / CELL));
int cell = grid.index(Math.round((x - grid.originX()) / CELL), Math.round((z - grid.originZ()) / CELL));
return samples.ocean()[cell];
}
@@ -179,7 +204,7 @@ final class InletTerrain {
}
static Reach northSouthReach(HydrologySettings settings, float x, int grade, float halfWidth, float depth) {
int points = 9;
int points = 25;
float[] xs = new float[points];
float[] zs = new float[points];
int[] grades = new int[points];
@@ -188,7 +213,7 @@ final class InletTerrain {
ReachStyle[] styles = new ReachStyle[points];
for (int point = 0; point < points; point++) {
xs[point] = x;
zs[point] = point * 64f;
zs[point] = point * 64f - WindowKey.SIZE;
grades[point] = grade;
widths[point] = halfWidth;
depths[point] = depth;
@@ -34,15 +34,15 @@ public class SeaInletPlannerTest {
int baseline = 0;
int retained = 0;
for (int offset = 0; offset < 32; offset++) {
InletTerrain shifted = terrain.shifted(0, offset * 512);
List<SeaInlet> all = shifted.plan(InletTerrain.settings(full));
List<SeaInlet> reduced = shifted.plan(InletTerrain.settings(half));
InletTerrain view = terrain.span(new WindowKey(0, offset));
List<SeaInlet> all = view.plan(InletTerrain.settings(full));
List<SeaInlet> reduced = view.plan(InletTerrain.settings(half));
baseline += all.size();
retained += reduced.size();
for (SeaInlet inlet : reduced) {
assertTrue("retained inlets keep the same full passage", all.contains(inlet));
}
assertEquals(reduced, shifted.plan(InletTerrain.settings(half)));
assertEquals(reduced, view.plan(InletTerrain.settings(half)));
}
assertTrue("cliff provides enough candidates", baseline >= 32);
assertTrue("half density retains approximately half the sites", retained > baseline * 0.3 && retained < baseline * 0.7);
@@ -190,11 +190,11 @@ public class SeaInletPlannerTest {
IrisHydrology dense = new IrisHydrology();
dense.getUnderground().getSeaInlets().setSpacing(32);
HydrologySettings settings = InletTerrain.settings(dense);
InletTerrain pocket = InletTerrain.pocket(152, 256);
List<SeaInlet> here = fromPocket(pocket.shifted(-232, 0).plan(settings));
InletTerrain pocket = InletTerrain.pocket(512, 256);
List<SeaInlet> here = fromPocket(pocket.span(new WindowKey(0, 0)).plan(settings), 512, 256);
assertTrue("inlets opening from the pocket " + here, here.size() >= 2);
assertEquals("another grid holding every competitor well inside decides the same", here,
fromPocket(pocket.shifted(-200, -24).plan(settings)));
fromPocket(pocket.span(new WindowKey(1, 0)).plan(settings), 512, 256));
for (int first = 0; first < here.size(); first++) {
for (int second = first + 1; second < here.size(); second++) {
assertFalse(here.get(first) + " and " + here.get(second) + " cross a covered passage with an open slot",
@@ -203,8 +203,8 @@ public class SeaInletPlannerTest {
}
}
private static List<SeaInlet> fromPocket(List<SeaInlet> inlets) {
return inlets.stream().filter(inlet -> inlet.xs()[0] == 152f && inlet.zs()[0] == 256f).toList();
private static List<SeaInlet> fromPocket(List<SeaInlet> inlets, float pocketX, float pocketZ) {
return inlets.stream().filter(inlet -> inlet.xs()[0] == pocketX && inlet.zs()[0] == pocketZ).toList();
}
private static boolean coveredMeetsOpen(SeaInlet covered, SeaInlet open) {
@@ -408,6 +408,84 @@ public class SeaInletPlannerTest {
return !WindowKey.containing(bounds[0], bounds[1]).equals(owner) || !WindowKey.containing(bounds[2], bounds[3]).equals(owner);
}
@Test
public void ownedInletsNeverConflictAcrossAWindowLine() {
IrisHydrology config = new IrisHydrology();
config.getUnderground().getSeaInlets().setSpacing(WindowKey.SIZE);
config.getUnderground().getSeaInlets().setLength(new KList<>(256, 400));
config.getUnderground().getSeaInlets().setHalfWidth(new KList<>(12D, 12D));
HydrologySettings settings = InletTerrain.settings(config);
int bayZ = 904;
WindowKey west = new WindowKey(1, 1);
WindowKey east = new WindowKey(2, 1);
single(InletTerrain.bay(1008, 1008, bayZ).wide(west, 3).plan(settings), west);
single(InletTerrain.bay(1032, 1032, bayZ).wide(east, 3).plan(settings), east);
InletTerrain bay = InletTerrain.bay(1016, 1024, bayZ);
List<SeaInlet> reference = bay.wide(west, 3).plan(settings);
List<SeaInlet> union = new ArrayList<>();
for (int wz = 0; wz <= 2; wz++) {
for (int wx = 0; wx <= 2; wx++) {
WindowKey window = new WindowKey(wx, wz);
List<SeaInlet> own = bay.span(window).plan(settings).stream().filter(inlet -> owner(inlet).equals(window)).toList();
assertEquals(window + " owns what the full-context plan gives it", reference.stream()
.filter(inlet -> owner(inlet).equals(window)).toList(), own);
union.addAll(own);
}
}
assertEquals("the two windows beside the bay keep one inlet between them: " + union, 1, union.size());
for (int first = 0; first < union.size(); first++) {
for (int second = first + 1; second < union.size(); second++) {
assertFalse(union.get(first) + " and " + union.get(second) + " cross a covered passage with an open slot",
coveredMeetsOpen(union.get(first), union.get(second)) || coveredMeetsOpen(union.get(second), union.get(first)));
}
}
}
private static SeaInlet single(List<SeaInlet> inlets, WindowKey window) {
List<SeaInlet> own = inlets.stream().filter(inlet -> owner(inlet).equals(window)).toList();
assertEquals(window + " owns one bay inlet: " + own, 1, own.size());
return own.getFirst();
}
@Test
public void extentGuardKeepsEveryPlannerLookupInsideTheOwnersSpan() {
for (double halfWidth : new double[]{1D, 16D}) {
for (int chamber = 1; chamber <= 32; chamber++) {
int length = WindowKey.SIZE - 12 - Math.max((int) Math.ceil(halfWidth), chamber) - 3;
IrisHydrology config = new IrisHydrology();
config.getUnderground().getSeaInlets().setLength(new KList<>(64, length));
config.getUnderground().getSeaInlets().setHalfWidth(new KList<>(1D, halfWidth));
config.getUnderground().getSeaInlets().setChamberRadius(new KList<>(1, chamber));
InletTerrain.settings(config);
long reach = SeaInletPlanner.lookupReach(length, chamber);
assertTrue("length " + length + " chamber " + chamber + " looks " + reach + " blocks out", reach <= WindowKey.SIZE);
}
}
}
@Test
public void longestInletAtTheFarEdgeOfItsWindowStaysInsideItsSpan() {
IrisHydrology config = new IrisHydrology();
config.getUnderground().getSeaInlets().setLength(new KList<>(465, 465));
config.getUnderground().getSeaInlets().setChamberRadius(new KList<>(32, 32));
HydrologySettings settings = InletTerrain.settings(config);
WindowKey window = new WindowKey(0, 0);
List<SeaInlet> owned = InletTerrain.eastEdgeCoast().plan(settings).stream().filter(inlet -> owner(inlet).equals(window)).toList();
assertFalse("inlets start on the window's last column", owned.isEmpty());
for (SeaInlet inlet : owned) {
assertEquals(inlet + " starts on the window's last column", WindowKey.SIZE - InletTerrain.CELL, inlet.xs()[1], 0f);
assertTrue(inlet + " runs the full length toward the span edge", inlet.chamberX() >= WindowKey.SIZE + 448);
}
}
@Test
public void ownInletCutOffByTheGridEdgeFailsClosed() {
InletTerrain narrow = InletTerrain.cliff(30D).narrow(0, 0);
IllegalStateException failure = assertThrows(IllegalStateException.class, () -> narrow.plan(InletTerrain.settings()));
assertTrue(failure.getMessage(),
failure.getMessage().matches("Sea inlet -?\\d+ of window WindowKey\\[wx=0, wz=0\\] reaches the edge of .*"));
}
@Test
public void inletsReachingPastTheNextWindowAreRejected() {
IrisHydrology fits = new IrisHydrology();