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 43093ed6d..195186e38 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 @@ -6,6 +6,7 @@ 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 org.junit.AfterClass; import org.junit.Test; import java.util.List; @@ -17,59 +18,98 @@ import static org.junit.Assert.assertFalse; public class PlannedSceneTest { private static final long SEED = 1337L; private static final int SEA_LEVEL = SyntheticTerrain.SEA_LEVEL; + private static final double RAINFALL = 0.5D; private static final float SPACING = 2f; private static PlannedScene openFractal; - @Test - public void reproducesTodaysOutletGap() { - assertFalse("detached outlets on openFractal(1) over 2x2 windows", WaterInvariants.detachedOutlets(openFractal()).isEmpty()); + @AfterClass + public static void dropScene() { + openFractal = null; } @Test - public void detachedOutletsSeparateLipOutletsFromOffshoreOutlets() { + public void reproducesTodaysOutletGap() { + assertFalse("detached outlets on openFractal(1) with rainfall " + RAINFALL + " over 2x2 windows", + WaterInvariants.detachedOutlets(openFractal()).isEmpty()); + } + + @Test + public void detachedOutletsCatchShoreGapsAndLowerStarts() { HydrologySettings settings = PlannedScene.packSettings(SEED); HydrologyTier stream = settings.tiers().get(0); - Reach lip = straight(11L, stream, 0L, 2f, new int[]{69, 64}, new int[]{140, 64}, (x, z) -> 100); - Reach offshore = straight(12L, stream, 0L, 2f, new int[]{80, 64}, new int[]{140, 64}, (x, z) -> 98); - for (Reach outlet : List.of(lip, offshore)) { - Pond pond = pond(21L, 100, disk(64, 64, 8), outlet.id()); + int[] starts = {133, 144, 133, 144}; + int[] grades = {100, 100, 98, 98}; + for (int index = 0; index < starts.length; index++) { + int level = grades[index]; + Reach outlet = straight(11L + index, stream, 0L, 2f, new int[]{starts[index], 128}, new int[]{204, 128}, (x, z) -> level); + Pond pond = pond(21L, 100, disk(128, 128, 8), outlet.id()); PlannedScene scene = PlannedScene.of(SyntheticTerrain.flat(104D), settings, List.of(outlet), List.of(pond)); - assertEquals("outlet " + outlet.id(), outlet == lip ? List.of() : List.of(outlet.id()), WaterInvariants.detachedOutlets(scene)); + assertEquals("outlet starting at x " + starts[index] + " with grade " + level, index == 0 ? List.of() : List.of(outlet.id()), + WaterInvariants.detachedOutlets(scene)); } } @Test public void reproducesTodaysConfluenceTongue() { HydrologySettings settings = PlannedScene.packSettings(SEED); - HydrologyTier river = settings.tiers().get(1); - HydrologyTier stream = settings.tiers().get(0); - Reach below = straight(302L, river, 0L, 8f, new int[]{16, 8}, new int[]{120, 8}, (x, z) -> 66); - Reach main = straight(301L, river, below.id(), 8f, new int[]{-96, 8}, new int[]{16, 8}, (x, z) -> 66); for (int tributaryLevel : new int[]{70, 66}) { - Reach tributary = straight(303L, stream, below.id(), 2f, new int[]{16, -72}, new int[]{16, 8}, (x, z) -> z >= 8 ? 66 : tributaryLevel); - PlannedScene scene = PlannedScene.of(SyntheticTerrain.flat(75D), settings, List.of(main, tributary, below), List.of()); - assertEquals("tributary at " + tributaryLevel, tributaryLevel == 70 ? List.of(303L) : List.of(), - WaterInvariants.confluenceTongues(scene)); + List 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); } } + @Test + public void confluenceTonguesCoverSiblingFootprintsOutsideTheDownstreamCap() { + HydrologySettings settings = PlannedScene.packSettings(SEED); + IntBinaryOperator perchedUpstream = (x, z) -> x >= 128 ? 66 : 70; + assertEquals("perched tributary inside the sibling channel", List.of(303L), + 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)); + } + @Test public void subSeaAirFindsWaterStandingBelowTheSeaSurface() { HydrologySettings settings = PlannedScene.packSettings(SEED); for (int level : new int[]{SEA_LEVEL - 4, SEA_LEVEL}) { - Pond pond = pond(31L, level, disk(64, 64, 8), 0L); + Pond pond = pond(31L, level, disk(128, 128, 8), 0L); PlannedScene scene = PlannedScene.of(SyntheticTerrain.flat(SEA_LEVEL + 5D), settings, List.of(), List.of(pond)); assertEquals("pond at " + level, level < SEA_LEVEL ? List.of(31L) : List.of(), WaterInvariants.subSeaAir(scene, SEA_LEVEL)); } } + @Test + public void invariantsSkipFeaturesWithinTheEdgeMargin() { + HydrologySettings settings = PlannedScene.packSettings(SEED); + String skipped = "features within " + WaterInvariants.EDGE_MARGIN + " blocks of the scene edge are skipped"; + Reach offshore = straight(41L, settings.tiers().get(0), 0L, 2f, new int[]{36, 256}, new int[]{100, 256}, (x, z) -> 98); + 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)); + 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)); + } + private static synchronized PlannedScene openFractal() { if (openFractal == null) { - openFractal = PlannedScene.plan(SyntheticTerrain.openFractal(1L), PlannedScene.packSettings(SEED), 2, 2); + openFractal = PlannedScene.plan(SyntheticTerrain.openFractal(1L).withRainfall((x, z) -> RAINFALL), PlannedScene.packSettings(SEED), 2, 2); } return openFractal; } + private static List tongues(HydrologySettings settings, int[] junction, int[] tributaryStart, IntBinaryOperator tributaryGrade, + 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); + 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(tributary, below); + return WaterInvariants.confluenceTongues(PlannedScene.of(SyntheticTerrain.flat(75D), settings, reaches, List.of())); + } + 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 432413db9..5e77837af 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 @@ -17,6 +17,7 @@ import java.util.Set; import java.util.TreeSet; 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; @@ -30,7 +31,10 @@ final class WaterInvariants { List detached = new ArrayList<>(); for (Pond pond : scene.ponds()) { Reach outlet = pond.kind() == PondKind.LAKE ? null : reaches.get(pond.outletReachId()); - if (outlet != null && !floodsIntoOutlet(scene, pond, outlet, WaterHeads.snap(pond.level(), seaLevel))) { + if (outlet == null || nearEdge(scene, outletBox(pond.mask(), outlet))) { + continue; + } + if (!floodsIntoOutlet(scene, pond, outlet, WaterHeads.snap(pond.level(), seaLevel))) { detached.add(outlet.id()); } } @@ -39,11 +43,17 @@ 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 receiving = feeder.downstreamId() == 0L ? null : reaches.get(feeder.downstreamId()); - if (receiving != null && perchedInside(scene, feeder, receiving, seaLevel)) { + 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()); } } @@ -52,8 +62,8 @@ final class WaterInvariants { static List subSeaAir(PlannedScene scene, int seaLevel) { Set features = new TreeSet<>(); - for (int z = scene.minZ(); z <= scene.maxZ(); z++) { - for (int x = scene.minX(); x <= scene.maxX(); x++) { + for (int z = scene.minZ() + EDGE_MARGIN; z <= scene.maxZ() - EDGE_MARGIN; z++) { + for (int x = scene.minX() + EDGE_MARGIN; x <= scene.maxX() - EDGE_MARGIN; x++) { int head = scene.head(x, z); if (head != Integer.MIN_VALUE && head < seaLevel && scene.terrain(x, z) < head) { features.add(scene.feature(x, z)); @@ -64,13 +74,11 @@ final class WaterInvariants { } private static boolean floodsIntoOutlet(PlannedScene scene, Pond pond, Reach outlet, int head) { - PondMask mask = pond.mask(); + int[] box = maskBox(pond.mask()); Set seen = new HashSet<>(); ArrayDeque queue = new ArrayDeque<>(); - int maxX = mask.originX() + mask.width() * mask.step(); - int maxZ = mask.originZ() + mask.depth() * mask.step(); - for (int z = mask.originZ() - mask.step(); z <= maxZ; z++) { - for (int x = mask.originX() - mask.step(); x <= maxX; x++) { + for (int z = box[1]; z <= box[3]; z++) { + for (int x = box[0]; x <= box[2]; x++) { if (scene.role(x, z) == WaterRole.POND && scene.feature(x, z) == pond.id() && scene.head(x, z) == head) { seen.add(pack(x, z)); queue.add(new int[]{x, z}); @@ -95,24 +103,9 @@ final class WaterInvariants { return false; } - private static boolean perchedInside(PlannedScene scene, Reach feeder, Reach receiving, int seaLevel) { - float[] xs = feeder.xs(); - float[] zs = feeder.zs(); - int minX = Integer.MAX_VALUE; - int minZ = Integer.MAX_VALUE; - int maxX = Integer.MIN_VALUE; - int maxZ = Integer.MIN_VALUE; - for (int point = 0; point < xs.length; point++) { - float extent = feeder.halfWidth()[point] + TONGUE_MARGIN; - if (edgeDistance(receiving, xs[point], zs[point]) <= extent) { - minX = Math.min(minX, (int) Math.floor(xs[point] - extent)); - minZ = Math.min(minZ, (int) Math.floor(zs[point] - extent)); - maxX = Math.max(maxX, (int) Math.ceil(xs[point] + extent)); - maxZ = Math.max(maxZ, (int) Math.ceil(zs[point] + extent)); - } - } - for (int z = minZ; z <= maxZ; z++) { - for (int x = minX; x <= maxX; x++) { + private static boolean perched(PlannedScene scene, Reach feeder, List receiving, int[] box, int seaLevel) { + 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()) { continue; } @@ -125,24 +118,91 @@ final class WaterInvariants { return false; } - private static double edgeDistance(Reach reach, double x, double z) { + private static Map> feeders(List reaches, Map byId) { + Map> feeders = new HashMap<>(); + for (Reach reach : reaches) { + if (byId.containsKey(reach.downstreamId())) { + feeders.computeIfAbsent(reach.downstreamId(), ignored -> new ArrayList<>()).add(reach); + } + } + return feeders; + } + + private static List receiving(Reach feeder, Reach downstream, List siblings) { + List receiving = new ArrayList<>(siblings.size()); + receiving.add(downstream); + for (Reach sibling : siblings) { + if (sibling.id() != feeder.id()) { + receiving.add(sibling); + } + } + return receiving; + } + + private static int[] tongueBox(Reach feeder, List receiving) { + float[] xs = feeder.xs(); + float[] zs = feeder.zs(); + 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) { + 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])}; + } + return box; + } + + private static int[] outletBox(PondMask mask, Reach outlet) { + int[] box = maskBox(mask); + float extent = outlet.halfWidth()[0] + 1f; + return new int[]{Math.min(box[0], (int) Math.floor(outlet.xs()[0] - extent)), Math.min(box[1], (int) Math.floor(outlet.zs()[0] - extent)), + Math.max(box[2], (int) Math.ceil(outlet.xs()[0] + extent)), Math.max(box[3], (int) Math.ceil(outlet.zs()[0] + extent))}; + } + + private static int[] maskBox(PondMask mask) { + return new int[]{mask.originX() - mask.step(), mask.originZ() - mask.step(), + mask.originX() + mask.width() * mask.step(), mask.originZ() + mask.depth() * mask.step()}; + } + + private static boolean nearEdge(PlannedScene scene, int[] box) { + return box[0] < scene.minX() + EDGE_MARGIN || box[1] < scene.minZ() + EDGE_MARGIN + || box[2] > scene.maxX() - EDGE_MARGIN || box[3] > scene.maxZ() - EDGE_MARGIN; + } + + private static double edgeDistance(List reaches, double x, double z) { double best = Double.POSITIVE_INFINITY; - for (int segment = 0; segment + 1 < reach.xs().length; segment++) { - double t = along(reach, segment, x, z); - best = Math.min(best, offset(reach, segment, t, x, z) - width(reach, segment, t)); + for (Reach reach : reaches) { + for (int segment = 0; segment + 1 < reach.xs().length; segment++) { + double t = along(reach, segment, x, z); + best = Math.min(best, offset(reach, segment, t, x, z) - width(reach, segment, t)); + } } return best; } - private static int coveringGrade(Reach reach, int x, int z) { + private static int coveringGrade(List reaches, int x, int z) { double nearest = Double.POSITIVE_INFINITY; + long nearestId = 0L; + int nearestSegment = 0; int grade = Integer.MIN_VALUE; - 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; - grade = reach.grade()[t < 0.5D ? segment : segment + 1]; + 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]; + } } } return grade;