diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/water/ConfluenceGrader.java b/core/src/main/java/art/arcane/iris/generation/hydrology/water/ConfluenceGrader.java index d5e4b32d2..1a2e3b70f 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/water/ConfluenceGrader.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/water/ConfluenceGrader.java @@ -51,7 +51,7 @@ public final class ConfluenceGrader { for (int feeder : downstream.incoming) { ReachDraft draft = drafts.get(feeder); requireJunction(window, draft, downstream); - boolean[] fixed = fixed(draft); + boolean[] fixed = draft.fixed(); int waterfallMinimumDrop = draft.tier.waterfallMinimumDrop(); int[] settled = settle(footprint(draft), draft.grade, fixed, receiving(drafts, downstream, feeder), downstream.grade[0], waterfallMinimumDrop); @@ -150,16 +150,6 @@ public final class ConfluenceGrader { return new Footprint(draft.xs, draft.zs, draft.halfWidth); } - private static boolean[] fixed(ReachDraft draft) { - boolean[] fixed = draft.outletZone.clone(); - if (draft.held != null) { - for (int point = 0; point < fixed.length; point++) { - fixed[point] |= draft.held[point]; - } - } - return fixed; - } - private static List receiving(List drafts, ReachDraft downstream, int feeder) { List receiving = new ArrayList<>(downstream.incoming.length); receiving.add(footprint(downstream)); diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachDraft.java b/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachDraft.java index e9ae13ad2..ec93ce722 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachDraft.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachDraft.java @@ -46,4 +46,14 @@ final class ReachDraft { int last() { return grade[grade.length - 1]; } + + boolean[] fixed() { + boolean[] fixed = outletZone.clone(); + if (held != null) { + for (int point = 0; point < fixed.length; point++) { + fixed[point] |= held[point]; + } + } + return fixed; + } } diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachShaper.java b/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachShaper.java index efb19843d..d606691f5 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachShaper.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachShaper.java @@ -380,7 +380,7 @@ public final class ReachShaper { } private static void restyle(ReachDraft draft) { - draft.style = styles(draft.grade, draft.ocean, draft.outletZone, draft.tier.waterfallMinimumDrop()); + draft.style = styles(draft.grade, draft.ocean, draft.fixed(), draft.tier.waterfallMinimumDrop()); } private static void chaikin(ReachDraft draft) { 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 08713af2e..2e23f74c0 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 @@ -19,6 +19,7 @@ public class PlannedSceneTest { private static final int SEA_LEVEL = SyntheticTerrain.SEA_LEVEL; private static final double RAINFALL = 0.5D; private static final float SPACING = 2f; + private static final float OUTLET_LENGTH = 8f; private static PlannedScene openFractal; @AfterClass @@ -74,7 +75,7 @@ public class PlannedSceneTest { for (int tributaryLevel : new int[]{70, 66}) { 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)); + tongues(settings, tributary, 66, true, List.of())); } } @@ -82,25 +83,40 @@ public class PlannedSceneTest { public void tonguesCountTheSiblingChannelAsReceivingWater() { HydrologySettings settings = PlannedScene.packSettings(SEED); 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)); + assertEquals("perched tributary beside the sibling channel", List.of(tributary.id()), tongues(settings, tributary, 66, true, List.of())); + assertEquals("without the sibling the perched stretch lies outside the pool", List.of(), tongues(settings, tributary, 66, false, List.of())); } @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)); + int[] lip = {144, 120}; + int[] junction = {144, 136}; + Reach falling = outlet(tributary(settings, lip, junction, 2f, (x, z) -> z >= 130 ? 66 : 70)); + assertEquals("outlet held on its pond's lip 4 blocks above the junction, cascading into it", List.of(), + tongues(settings, falling, 66, true, List.of(source(70, falling)))); + Reach stepping = outlet(tributary(settings, lip, junction, 2f, (x, z) -> z >= 130 ? 67 : 68)); + assertEquals("held outlet whose 1-block riffle into the junction shows as a 2-block step", List.of(stepping.id()), + tongues(settings, stepping, 67, true, List.of(source(68, stepping)))); + Reach above = outlet(tributary(settings, lip, junction, 2f, (x, z) -> 70)); + assertEquals("held outlet whose own end stays above the junction", List.of(above.id()), + tongues(settings, above, 66, true, List.of(source(70, above)))); + } + + @Test + public void pondOutletWithAPerchedUnheldTailIsATongue() { + HydrologySettings settings = PlannedScene.packSettings(SEED); + Reach outlet = outlet(tributary(settings, new int[]{144, 56}, new int[]{144, 136}, 2f, (x, z) -> z >= 136 ? 66 : 70)); + Pond pond = pond(21L, 70, disk(144, 46, 10), outlet.id()); + assertEquals("outlet held only near its pond, cascading into the junction from a perched tail", List.of(outlet.id()), + tongues(settings, outlet, 66, true, List.of(pond))); } @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)); + assertEquals("perched tributary columns inside the edge margin line", List.of(tributary.id()), tongues(settings, tributary, 66, true, List.of())); } @Test @@ -122,7 +138,7 @@ public class PlannedSceneTest { List.of(pond(42L, 100, disk(20, 256, 8), offshore.id()))); assertEquals(skipped, List.of(), WaterInvariants.detachedOutlets(outlet)); 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)); + assertEquals(skipped, List.of(), tongues(settings, tributary, 66, true, List.of())); 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)); @@ -139,7 +155,7 @@ public class PlannedSceneTest { return straight(303L, settings.tiers().get(0), 302L, halfWidth, from, junction, grade); } - private static List tongues(HydrologySettings settings, Reach tributary, int receivingLevel, boolean sibling) { + private static List tongues(HydrologySettings settings, Reach tributary, int receivingLevel, boolean sibling, List ponds) { HydrologyTier river = settings.tiers().get(1); int last = tributary.xs().length - 1; int[] junction = {Math.round(tributary.xs()[last]), Math.round(tributary.zs()[last])}; @@ -148,16 +164,28 @@ public class PlannedSceneTest { ? 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())); + return WaterInvariants.confluenceTongues(PlannedScene.of(SyntheticTerrain.flat(75D), settings, reaches, ponds)); } private static Reach outlet(Reach reach) { ReachStyle[] style = reach.style().clone(); - style[0] = ReachStyle.POND_OUTLET; + float along = 0f; + for (int point = 0; point < style.length; point++) { + if (point > 0) { + along += (float) Math.hypot(reach.xs()[point] - reach.xs()[point - 1], reach.zs()[point] - reach.zs()[point - 1]); + } + if (along <= OUTLET_LENGTH && (style[point] == ReachStyle.RUN || style[point] == ReachStyle.RIFFLE)) { + style[point] = 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 Pond source(int level, Reach outlet) { + return pond(21L, level, disk(144, 112, 10), outlet.id()); + } + 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 64294138d..8414ff039 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 @@ -7,6 +7,7 @@ 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.ReachShaper; import art.arcane.iris.generation.hydrology.water.ReachStyle; import java.util.ArrayDeque; @@ -136,6 +137,7 @@ final class WaterInvariants { private static List junctions(PlannedScene scene) { Map reaches = byId(scene.reaches()); Map> feeders = feeders(scene.reaches(), reaches); + Map> lips = lips(scene.ponds()); int seaLevel = scene.settings().seaLevel(); List junctions = new ArrayList<>(); for (Reach feeder : scene.reaches()) { @@ -147,7 +149,8 @@ final class WaterInvariants { 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)); + boolean[] held = held(feeder, lips.getOrDefault(feeder.id(), List.of())); + junctions.add(new Junction(feeder, WaterHeads.snap(level, seaLevel), pool, held, box)); } } return junctions; @@ -155,7 +158,7 @@ final class WaterInvariants { private static boolean perched(PlannedScene scene, Junction junction, int seaLevel) { Reach feeder = junction.feeder(); - boolean outlet = feeder.style()[0] == ReachStyle.POND_OUTLET; + boolean endsAtTheJunction = WaterHeads.snap(feeder.grade()[feeder.grade().length - 1], seaLevel) <= junction.head(); int[] box = junction.box(); for (int z = box[1]; z <= box[3]; z++) { for (int x = box[0]; x <= box[2]; x++) { @@ -163,7 +166,10 @@ final class WaterInvariants { continue; } int point = coveringPoint(feeder, x, z); - if (point >= 0 && junction.pool()[point] && !(outlet && fallsToTheJunction(feeder, point, seaLevel))) { + if (point < 0 || !junction.pool()[point]) { + continue; + } + if (!endsAtTheJunction || !junction.held()[point] || !fallsToTheJunction(feeder, point, seaLevel)) { return true; } } @@ -187,6 +193,35 @@ final class WaterInvariants { return pool; } + private static Map> lips(List ponds) { + Map> lips = new HashMap<>(); + for (Pond pond : ponds) { + if (pond.kind() != PondKind.LAKE && pond.outletReachId() != 0L) { + lips.computeIfAbsent(pond.outletReachId(), ignored -> new ArrayList<>()).add(pond.mask()); + } + } + return lips; + } + + private static boolean[] held(Reach outlet, List lips) { + boolean[] held = new boolean[outlet.xs().length]; + boolean lip = !lips.isEmpty(); + for (int point = 0; point < held.length; point++) { + lip = lip && (point == 0 || nearLip(lips, outlet.xs()[point], outlet.zs()[point], outlet.halfWidth()[point])); + held[point] = lip || outlet.style()[point] == ReachStyle.POND_OUTLET; + } + return held; + } + + private static boolean nearLip(List lips, float x, float z, float halfWidth) { + for (PondMask lip : lips) { + if (ReachShaper.nearLip(lip, x, z, halfWidth)) { + return true; + } + } + return false; + } + 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); @@ -311,6 +346,6 @@ final class WaterInvariants { return ((long) x << 32) | (z & 0xFFFFFFFFL); } - private record Junction(Reach feeder, int head, boolean[] pool, int[] box) { + private record Junction(Reach feeder, int head, boolean[] pool, boolean[] held, int[] box) { } } diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/water/ReachShaperTest.java b/core/src/test/java/art/arcane/iris/generation/hydrology/water/ReachShaperTest.java index a2f473ce9..9ccf992d0 100644 --- a/core/src/test/java/art/arcane/iris/generation/hydrology/water/ReachShaperTest.java +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/water/ReachShaperTest.java @@ -490,6 +490,37 @@ public class ReachShaperTest { assertEquals(99, PondChannelSettlement.settle(WINDOW, shaped, SEA_LEVEL).ponds().get(0).level()); } + @Test + public void outletHeldAlongItsPondsShoreIsStyledAsThePondOutlet() { + HydrologySettings settings = settings(config -> { + }); + DemGrid flat = outletGrid(); + int[] receivers = new int[flat.values().length]; + Arrays.fill(receivers, -1); + float[] accumulation = new float[receivers.length]; + BitSet channels = new BitSet(receivers.length); + for (int i = 9; i < flat.width(); i++) { + flow(flat, receivers, accumulation, channels, i, 8, i + 1 < flat.width() ? i + 1 : -1, 8, 1600f + 10f * i); + } + PondMask shore = rectangle(72, 52, 18, 3); + Pond pond = new Pond(81L, PondKind.NATURAL, 99, 94, 72, 64, 100f, shore, 0L, 0, "water", 104, 56); + HeightLattice fine = new HeightLattice(new Terrain((x, z) -> shore.contains((int) x, (int) z) ? 94D : 100D), FINE, false, 1 << 16); + ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, flat, receivers, accumulation, channels, List.of(pond), fine, SEED); + Reach outlet = byId(shaped.reaches()).get(shaped.ponds().get(0).outletReachId()); + assertNotNull("the pond drains through an outlet along its shore", outlet); + float farthestHeld = 0f; + for (int point = 0; point < outlet.xs().length; point++) { + float along = outlet.xs()[point] - outlet.xs()[0]; + if (outlet.xs()[point] <= 136f) { + assertEquals("outlet point " + point + " beside the shore at x=" + outlet.xs()[point], ReachStyle.POND_OUTLET, outlet.style()[point]); + farthestHeld = Math.max(farthestHeld, along); + } else if (outlet.xs()[point] >= 160f) { + assertEquals("outlet point " + point + " past the shore at x=" + outlet.xs()[point], ReachStyle.RUN, outlet.style()[point]); + } + } + assertTrue("the held shore runs " + farthestHeld + " blocks, past the 8-block outlet zone", farthestHeld > 40f); + } + @Test public void pondsSharingAnExitLeadInOnceFromTheirGradingPond() { HydrologySettings settings = settings(config -> {