diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/PlannedSceneTest.java b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/PlannedSceneTest.java index 529555f47..08713af2e 100644 --- a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/PlannedSceneTest.java +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/PlannedSceneTest.java @@ -69,22 +69,38 @@ public class PlannedSceneTest { } @Test - public void confluenceHasNoTongueAtEitherTributaryLevel() { + public void unsettledFeederPerchedAboveTheJunctionIsATongue() { HydrologySettings settings = PlannedScene.packSettings(SEED); for (int tributaryLevel : new int[]{70, 66}) { - List tongues = tongues(settings, new int[]{144, 136}, new int[]{144, 56}, (x, z) -> z >= 136 ? 66 : tributaryLevel, true); - assertEquals("tributary at " + tributaryLevel, List.of(), tongues); + Reach tributary = tributary(settings, new int[]{144, 56}, new int[]{144, 136}, 2f, (x, z) -> z >= 136 ? 66 : tributaryLevel); + assertEquals("tributary at " + tributaryLevel, tributaryLevel > 66 ? List.of(tributary.id()) : List.of(), + tongues(settings, tributary, 66, true)); } } @Test - public void confluenceHasNoTongueInsideTheSiblingChannel() { + public void tonguesCountTheSiblingChannelAsReceivingWater() { HydrologySettings settings = PlannedScene.packSettings(SEED); - IntBinaryOperator perchedUpstream = (x, z) -> x >= 128 ? 66 : 70; - 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)); + Reach tributary = tributary(settings, new int[]{48, 112}, new int[]{144, 136}, 2f, (x, z) -> x >= 128 ? 66 : 70); + assertEquals("perched tributary beside the sibling channel", List.of(tributary.id()), tongues(settings, tributary, 66, true)); + assertEquals("without the sibling the perched stretch lies outside the pool", List.of(), tongues(settings, tributary, 66, false)); + } + + @Test + public void heldPondOutletMustFallIntoTheJunction() { + HydrologySettings settings = PlannedScene.packSettings(SEED); + Reach falling = outlet(tributary(settings, new int[]{144, 56}, new int[]{144, 136}, 2f, (x, z) -> z >= 136 ? 66 : 70)); + assertEquals("outlet held 4 blocks above the junction, cascading into it", List.of(), tongues(settings, falling, 66, true)); + Reach stepping = outlet(tributary(settings, new int[]{144, 56}, new int[]{144, 136}, 2f, (x, z) -> z >= 136 ? 67 : 68)); + assertEquals("outlet whose 1-block riffle into the junction shows as a 2-block step", List.of(stepping.id()), + tongues(settings, stepping, 67, true)); + } + + @Test + public void tongueScanKeepsTheInteriorPartOfAPoolCrossingTheEdgeMargin() { + HydrologySettings settings = PlannedScene.packSettings(SEED); + Reach tributary = tributary(settings, new int[]{36, 176}, new int[]{36, 256}, 4f, (x, z) -> z >= 256 ? 66 : 70); + assertEquals("perched tributary columns inside the edge margin line", List.of(tributary.id()), tongues(settings, tributary, 66, true)); } @Test @@ -105,7 +121,8 @@ public class PlannedSceneTest { PlannedScene outlet = PlannedScene.of(SyntheticTerrain.flat(104D), settings, List.of(offshore), List.of(pond(42L, 100, disk(20, 256, 8), offshore.id()))); assertEquals(skipped, List.of(), WaterInvariants.detachedOutlets(outlet)); - assertEquals(skipped, List.of(), tongues(settings, new int[]{16, 256}, new int[]{16, 176}, (x, z) -> z >= 256 ? 66 : 70, true)); + Reach tributary = tributary(settings, new int[]{16, 176}, new int[]{16, 256}, 2f, (x, z) -> z >= 256 ? 66 : 70); + assertEquals(skipped, List.of(), tongues(settings, tributary, 66, true)); PlannedScene sunken = PlannedScene.of(SyntheticTerrain.flat(SEA_LEVEL + 5D), settings, List.of(), List.of(pond(43L, SEA_LEVEL - 4, disk(20, 256, 8), 0L))); assertEquals(skipped, List.of(), WaterInvariants.subSeaAir(sunken, SEA_LEVEL)); @@ -118,17 +135,29 @@ public class PlannedSceneTest { return openFractal; } - private static List tongues(HydrologySettings settings, int[] junction, int[] tributaryStart, IntBinaryOperator tributaryGrade, - boolean sibling) { + private static Reach tributary(HydrologySettings settings, int[] from, int[] junction, float halfWidth, IntBinaryOperator grade) { + return straight(303L, settings.tiers().get(0), 302L, halfWidth, from, junction, grade); + } + + private static List tongues(HydrologySettings settings, Reach tributary, int receivingLevel, boolean sibling) { HydrologyTier river = settings.tiers().get(1); - Reach below = straight(302L, river, 0L, 8f, junction, new int[]{junction[0] + 104, junction[1]}, (x, z) -> 66); - Reach tributary = straight(303L, settings.tiers().get(0), below.id(), 2f, tributaryStart, junction, tributaryGrade); + int last = tributary.xs().length - 1; + int[] junction = {Math.round(tributary.xs()[last]), Math.round(tributary.zs()[last])}; + Reach below = straight(302L, river, 0L, 8f, junction, new int[]{junction[0] + 104, junction[1]}, (x, z) -> receivingLevel); List reaches = sibling - ? List.of(straight(301L, river, below.id(), 8f, new int[]{junction[0] - 112, junction[1]}, junction, (x, z) -> 66), tributary, below) + ? List.of(straight(301L, river, below.id(), 8f, new int[]{junction[0] - 112, junction[1]}, junction, (x, z) -> receivingLevel), + tributary, below) : List.of(tributary, below); return WaterInvariants.confluenceTongues(PlannedScene.of(SyntheticTerrain.flat(75D), settings, reaches, List.of())); } + private static Reach outlet(Reach reach) { + ReachStyle[] style = reach.style().clone(); + style[0] = ReachStyle.POND_OUTLET; + return new Reach(reach.id(), reach.tier(), reach.downstreamId(), reach.xs(), reach.zs(), reach.grade(), reach.halfWidth(), reach.depth(), + style, reach.catchment()); + } + private static Reach straight(long id, HydrologyTier tier, long downstreamId, float halfWidth, int[] from, int[] to, IntBinaryOperator grade) { double length = Math.hypot(to[0] - from[0], to[1] - from[1]); int points = Math.max(2, (int) Math.round(length / SPACING) + 1); diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/WaterInvariants.java b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/WaterInvariants.java index f88744e35..64294138d 100644 --- a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/WaterInvariants.java +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/WaterInvariants.java @@ -2,10 +2,12 @@ package art.arcane.iris.generation.hydrology.plan; import art.arcane.iris.generation.hydrology.raster.WaterHeads; import art.arcane.iris.generation.hydrology.raster.WaterRole; +import art.arcane.iris.generation.hydrology.water.ConfluenceGrader; import art.arcane.iris.generation.hydrology.water.Pond; import art.arcane.iris.generation.hydrology.water.PondKind; import art.arcane.iris.generation.hydrology.water.PondMask; import art.arcane.iris.generation.hydrology.water.Reach; +import art.arcane.iris.generation.hydrology.water.ReachStyle; import java.util.ArrayDeque; import java.util.ArrayList; @@ -20,7 +22,6 @@ final class WaterInvariants { static final int EDGE_MARGIN = 32; private static final int[] DX = {1, -1, 0, 0}; private static final int[] DZ = {0, 0, 1, -1}; - private static final float TONGUE_MARGIN = 2f; private WaterInvariants() { } @@ -51,24 +52,24 @@ final class WaterInvariants { } static List confluenceTongues(PlannedScene scene) { - Map reaches = byId(scene.reaches()); - Map> feeders = feeders(scene.reaches(), reaches); int seaLevel = scene.settings().seaLevel(); List tongues = new ArrayList<>(); - for (Reach feeder : scene.reaches()) { - Reach downstream = feeder.downstreamId() == 0L ? null : reaches.get(feeder.downstreamId()); - if (downstream == null) { - continue; - } - List receiving = receiving(feeder, downstream, feeders.get(downstream.id())); - int[] box = tongueBox(feeder, receiving); - if (box != null && !nearEdge(scene, box) && perched(scene, feeder, receiving, box, seaLevel)) { - tongues.add(feeder.id()); + for (Junction junction : junctions(scene)) { + if (perched(scene, junction, seaLevel)) { + tongues.add(junction.feeder().id()); } } return tongues; } + static List checkedFeeders(PlannedScene scene) { + List checked = new ArrayList<>(); + for (Junction junction : junctions(scene)) { + checked.add(junction.feeder()); + } + return checked; + } + static List subSeaAir(PlannedScene scene, int seaLevel) { Set features = new TreeSet<>(); for (int z = scene.minZ() + EDGE_MARGIN; z <= scene.maxZ() - EDGE_MARGIN; z++) { @@ -132,14 +133,37 @@ final class WaterInvariants { return false; } - private static boolean perched(PlannedScene scene, Reach feeder, List receiving, int[] box, int seaLevel) { + private static List junctions(PlannedScene scene) { + Map reaches = byId(scene.reaches()); + Map> feeders = feeders(scene.reaches(), reaches); + int seaLevel = scene.settings().seaLevel(); + List junctions = new ArrayList<>(); + for (Reach feeder : scene.reaches()) { + Reach downstream = feeder.downstreamId() == 0L ? null : reaches.get(feeder.downstreamId()); + if (downstream == null) { + continue; + } + boolean[] pool = pool(feeder, receiving(feeder, downstream, feeders.get(downstream.id()))); + int[] box = interior(scene, poolBox(feeder, pool)); + if (box != null) { + int level = Math.min(feeder.grade()[feeder.grade().length - 1], downstream.grade()[0]); + junctions.add(new Junction(feeder, WaterHeads.snap(level, seaLevel), pool, box)); + } + } + return junctions; + } + + private static boolean perched(PlannedScene scene, Junction junction, int seaLevel) { + Reach feeder = junction.feeder(); + boolean outlet = feeder.style()[0] == ReachStyle.POND_OUTLET; + int[] box = junction.box(); for (int z = box[1]; z <= box[3]; z++) { for (int x = box[0]; x <= box[2]; x++) { - if (scene.feature(x, z) != feeder.id()) { + if (scene.feature(x, z) != feeder.id() || scene.head(x, z) <= junction.head()) { continue; } - int receivingGrade = coveringGrade(receiving, x, z); - if (receivingGrade != Integer.MIN_VALUE && scene.head(x, z) > WaterHeads.snap(receivingGrade, seaLevel)) { + int point = coveringPoint(feeder, x, z); + if (point >= 0 && junction.pool()[point] && !(outlet && fallsToTheJunction(feeder, point, seaLevel))) { return true; } } @@ -147,6 +171,33 @@ final class WaterInvariants { return false; } + private static boolean[] pool(Reach feeder, List receiving) { + int last = feeder.xs().length - 1; + boolean[] pool = new boolean[last + 1]; + pool[last] = true; + double arc = 0D; + 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]); + if (arc > ConfluenceGrader.POOL_CAP + || edgeDistance(receiving, feeder.xs()[point], feeder.zs()[point]) > feeder.halfWidth()[point] + ConfluenceGrader.POOL_MARGIN) { + return pool; + } + pool[point] = true; + } + return pool; + } + + 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]; + if (step && style != ReachStyle.CASCADE && style != ReachStyle.WATERFALL) { + return false; + } + } + return true; + } + private static Map> feeders(List reaches, Map byId) { Map> feeders = new HashMap<>(); for (Reach reach : reaches) { @@ -168,23 +219,34 @@ final class WaterInvariants { return receiving; } - private static int[] tongueBox(Reach feeder, List receiving) { - float[] xs = feeder.xs(); - float[] zs = feeder.zs(); + private static int[] poolBox(Reach feeder, boolean[] pool) { int[] box = null; - for (int point = 0; point < xs.length; point++) { - float extent = feeder.halfWidth()[point] + TONGUE_MARGIN; - if (edgeDistance(receiving, xs[point], zs[point]) > extent) { + for (int point = 0; point < pool.length; point++) { + if (!pool[point]) { continue; } - int[] around = {(int) Math.floor(xs[point] - extent), (int) Math.floor(zs[point] - extent), - (int) Math.ceil(xs[point] + extent), (int) Math.ceil(zs[point] + extent)}; - box = box == null ? around : new int[]{Math.min(box[0], around[0]), Math.min(box[1], around[1]), - Math.max(box[2], around[2]), Math.max(box[3], around[3])}; + int from = Math.max(0, point - 1); + int to = Math.min(pool.length - 1, point + 1); + float extent = 0f; + for (int near = from; near <= to; near++) { + extent = Math.max(extent, feeder.halfWidth()[near] + 1f); + } + for (int near = from; near <= to; near++) { + int[] around = {(int) Math.floor(feeder.xs()[near] - extent), (int) Math.floor(feeder.zs()[near] - extent), + (int) Math.ceil(feeder.xs()[near] + extent), (int) Math.ceil(feeder.zs()[near] + extent)}; + box = box == null ? around : new int[]{Math.min(box[0], around[0]), Math.min(box[1], around[1]), + Math.max(box[2], around[2]), Math.max(box[3], around[3])}; + } } return box; } + private static int[] interior(PlannedScene scene, int[] box) { + int[] clipped = {Math.max(box[0], scene.minX() + EDGE_MARGIN), Math.max(box[1], scene.minZ() + EDGE_MARGIN), + Math.min(box[2], scene.maxX() - EDGE_MARGIN), Math.min(box[3], scene.maxZ() - EDGE_MARGIN)}; + return clipped[0] > clipped[2] || clipped[1] > clipped[3] ? null : clipped; + } + private static int[] outletBox(PondMask mask, Reach outlet) { int[] box = maskBox(mask); float extent = outlet.halfWidth()[0] + 1f; @@ -213,28 +275,18 @@ final class WaterInvariants { return best; } - private static int coveringGrade(List reaches, int x, int z) { + private static int coveringPoint(Reach reach, int x, int z) { double nearest = Double.POSITIVE_INFINITY; - long nearestId = 0L; - int nearestSegment = 0; - int grade = Integer.MIN_VALUE; - for (Reach reach : reaches) { - for (int segment = 0; segment + 1 < reach.xs().length; segment++) { - double t = along(reach, segment, x, z); - double distance = offset(reach, segment, t, x, z); - if (distance > width(reach, segment, t)) { - continue; - } - if (grade == Integer.MIN_VALUE || distance < nearest || distance == nearest - && (reach.id() < nearestId || reach.id() == nearestId && segment < nearestSegment)) { - nearest = distance; - nearestId = reach.id(); - nearestSegment = segment; - grade = reach.grade()[t < 0.5D ? segment : segment + 1]; - } + int point = -1; + for (int segment = 0; segment + 1 < reach.xs().length; segment++) { + double t = along(reach, segment, x, z); + double distance = offset(reach, segment, t, x, z); + if (distance <= width(reach, segment, t) && distance < nearest) { + nearest = distance; + point = t < 0.5D ? segment : segment + 1; } } - return grade; + return point; } private static boolean covers(Reach reach, int x, int z) { @@ -258,4 +310,7 @@ final class WaterInvariants { private static long pack(int x, int z) { return ((long) x << 32) | (z & 0xFFFFFFFFL); } + + private record Junction(Reach feeder, int head, boolean[] pool, int[] box) { + } } diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/WindowPlannerTest.java b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/WindowPlannerTest.java index 2fd5e3404..4ab617ab3 100644 --- a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/WindowPlannerTest.java +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/WindowPlannerTest.java @@ -625,28 +625,18 @@ public class WindowPlannerTest { } @Test - public void everyPondOutletLeavesThroughConnectedWater() { + public void pondOutletsAndConfluencesMeetConnectedWater() { int outlets = 0; + int feeders = 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)); + 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); - } - - @Test - public void confluencesLeaveNoPerchedTongue() { - int feeders = 0; - for (long seed = 1L; seed <= 3L; seed++) { - PlannedScene scene = PlannedScene.plan(SyntheticTerrain.openFractal(seed), PlannedScene.packSettings(SEED), 1, 1); - assertEquals("confluence tongues on openFractal(" + seed + ")", List.of(), WaterInvariants.confluenceTongues(scene)); - Map reaches = byId(scene.reaches()); - for (Reach reach : scene.reaches()) { - feeders += reaches.containsKey(reach.downstreamId()) ? 1 : 0; - } - } - assertTrue("feeders checked " + feeders, feeders >= 100); + assertTrue("confluence feeders checked " + feeders, feeders >= 300); } private static void assertOutletLeavesAtThePondHead(Pond pond, Reach outlet) {