diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChunkFeatures.java b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChunkFeatures.java index ea2b0df08..c54d18fb7 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChunkFeatures.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChunkFeatures.java @@ -295,6 +295,7 @@ final class ChunkFeatures { private static final class Links { private final Map windows; private final Map outletLevels = new HashMap<>(); + private final Map> outletMasks = new HashMap<>(); private final Map resolved = new HashMap<>(); private final Set resolving = new HashSet<>(); @@ -304,6 +305,7 @@ final class ChunkFeatures { for (Pond pond : window.plan().ponds()) { if (pond.outletReachId() != 0L) { outletLevels.merge(pond.outletReachId(), pond.level(), Math::min); + outletMasks.computeIfAbsent(pond.outletReachId(), ignored -> new ArrayList<>()).add(pond.mask()); } } } @@ -354,7 +356,18 @@ final class ChunkFeatures { } private int junction(Reach reach) { - return reach.xs().length > 1 && outletLevels.containsKey(reach.id()) ? 1 : 0; + List masks = outletMasks.get(reach.id()); + if (masks == null || reach.xs().length < 2) { + return 0; + } + int x = Math.round(reach.xs()[0]); + int z = Math.round(reach.zs()[0]); + for (PondMask mask : masks) { + if (mask.contains(x, z)) { + return 1; + } + } + return 0; } private long cornerBlock(Reach reach) { diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachGrader.java b/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachGrader.java index 76236dd62..a6fa39338 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachGrader.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/water/ReachGrader.java @@ -29,13 +29,7 @@ final class ReachGrader { || draft.downstream >= 0 && lakeFloor[draft.downstream] && downstreamFloor >= draft.endLevel; } int[] levels = ponds.levels().clone(); - int[] outletPond = new int[drafts.size()]; - Arrays.fill(outletPond, -1); - for (int pond = 0; pond < levels.length; pond++) { - if (ponds.outletDrafts()[pond] >= 0) { - outletPond[ponds.outletDrafts()[pond]] = pond; - } - } + int[] outletPond = outletPonds(ponds.outletDrafts(), drafts.size()); int[][] inflows = inflows(ponds, levels.length); int[] fed = new int[drafts.size()]; Arrays.fill(fed, Integer.MAX_VALUE); @@ -58,7 +52,7 @@ final class ReachGrader { } int[] grade = profile(drafts, draft); for (int point = 0; point < grade.length; point++) { - if (draft.held != null && draft.held[point]) { + if (draft.held != null && draft.held[point] && !draft.ocean[point]) { grade[point] = level; continue; } @@ -84,6 +78,17 @@ final class ReachGrader { return levels; } + static int[] outletPonds(int[] outletDrafts, int drafts) { + int[] outletPond = new int[drafts]; + Arrays.fill(outletPond, -1); + for (int pond = 0; pond < outletDrafts.length; pond++) { + if (outletDrafts[pond] >= 0) { + outletPond[outletDrafts[pond]] = pond; + } + } + return outletPond; + } + private static int[][] inflows(PondChain ponds, int count) { int[] sizes = new int[count]; for (int pond : ponds.inflowPond()) { 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 2d6635a3a..1407c05bf 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 @@ -61,9 +61,10 @@ public final class ReachShaper { drafts.get(outlet).outlet = true; } } - for (int pond = 0; pond < outletDrafts.length; pond++) { - if (leavesThroughALip(ponds.get(pond), outletDrafts[pond])) { - hold(drafts.get(outletDrafts[pond]), ponds.get(pond).mask()); + int[] sources = ReachGrader.outletPonds(outletDrafts, drafts.size()); + for (int index = 0; index < sources.length; index++) { + if (leavesThroughALip(ponds, sources[index])) { + hold(drafts.get(index), ponds.get(sources[index]).mask()); } } ReachGrader.PondChain chain = pondChain(drafts, ponds, outletDrafts); @@ -72,9 +73,9 @@ public final class ReachShaper { for (int index : order) { finish(drafts, drafts.get(index)); } - for (int pond = 0; pond < outletDrafts.length; pond++) { - if (leavesThroughALip(ponds.get(pond), outletDrafts[pond])) { - leadIn(drafts.get(outletDrafts[pond]), ponds.get(pond).mask()); + for (int index = 0; index < sources.length; index++) { + if (leavesThroughALip(ponds, sources[index])) { + leadIn(drafts.get(index), ponds.get(sources[index]).mask()); } } List linked = new ArrayList<>(ponds.size()); @@ -273,8 +274,8 @@ public final class ReachShaper { throw new IllegalStateException("Reach and pond links loop through " + (count - size) + " of " + count + " reaches"); } - private static boolean leavesThroughALip(Pond pond, int outletDraft) { - return outletDraft >= 0 && pond.kind() != PondKind.LAKE; + private static boolean leavesThroughALip(List ponds, int source) { + return source >= 0 && ponds.get(source).kind() != PondKind.LAKE; } private static void hold(ReachDraft draft, PondMask mask) { 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 daa9e7bfe..f88744e35 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 @@ -29,11 +29,8 @@ final class WaterInvariants { Map reaches = byId(scene.reaches()); int seaLevel = scene.settings().seaLevel(); List detached = new ArrayList<>(); - for (Pond pond : scene.ponds()) { - Reach outlet = pond.kind() == PondKind.LAKE ? null : reaches.get(pond.outletReachId()); - if (outlet == null || nearEdge(scene, outletBox(pond.mask(), outlet))) { - continue; - } + for (Pond pond : checkedPonds(scene)) { + Reach outlet = reaches.get(pond.outletReachId()); if (!floodsIntoOutlet(scene, pond, outlet, WaterHeads.snap(pond.level(), seaLevel))) { detached.add(outlet.id()); } @@ -41,6 +38,18 @@ final class WaterInvariants { return detached; } + static List checkedPonds(PlannedScene scene) { + Map reaches = byId(scene.reaches()); + List checked = new ArrayList<>(); + for (Pond pond : scene.ponds()) { + Reach outlet = pond.kind() == PondKind.LAKE ? null : reaches.get(pond.outletReachId()); + if (outlet != null && !nearEdge(scene, outletBox(pond.mask(), outlet))) { + checked.add(pond); + } + } + return checked; + } + static List confluenceTongues(PlannedScene scene) { Map reaches = byId(scene.reaches()); Map> feeders = feeders(scene.reaches(), reaches); @@ -73,6 +82,25 @@ final class WaterInvariants { return List.copyOf(features); } + static double along(Reach reach, int segment, double x, double z) { + double ax = reach.xs()[segment]; + double az = reach.zs()[segment]; + double dx = reach.xs()[segment + 1] - ax; + double dz = reach.zs()[segment + 1] - az; + double length = dx * dx + dz * dz; + return length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D); + } + + static double offset(Reach reach, int segment, double t, double x, double z) { + double px = reach.xs()[segment] + t * (reach.xs()[segment + 1] - reach.xs()[segment]); + double pz = reach.zs()[segment] + t * (reach.zs()[segment + 1] - reach.zs()[segment]); + return Math.hypot(x - px, z - pz); + } + + static double width(Reach reach, int segment, double t) { + return reach.halfWidth()[segment] + t * (reach.halfWidth()[segment + 1] - reach.halfWidth()[segment]); + } + private static boolean floodsIntoOutlet(PlannedScene scene, Pond pond, Reach outlet, int head) { int[] box = maskBox(pond.mask()); Set seen = new HashSet<>(); @@ -219,25 +247,6 @@ final class WaterInvariants { return false; } - private static double along(Reach reach, int segment, double x, double z) { - double ax = reach.xs()[segment]; - double az = reach.zs()[segment]; - double dx = reach.xs()[segment + 1] - ax; - double dz = reach.zs()[segment + 1] - az; - double length = dx * dx + dz * dz; - return length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D); - } - - private static double offset(Reach reach, int segment, double t, double x, double z) { - double px = reach.xs()[segment] + t * (reach.xs()[segment + 1] - reach.xs()[segment]); - double pz = reach.zs()[segment] + t * (reach.zs()[segment + 1] - reach.zs()[segment]); - return Math.hypot(x - px, z - pz); - } - - private static double width(Reach reach, int segment, double t) { - return reach.halfWidth()[segment] + t * (reach.halfWidth()[segment + 1] - reach.halfWidth()[segment]); - } - private static Map byId(List reaches) { Map byId = new HashMap<>(reaches.size() * 2); for (Reach reach : reaches) { 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 f81b9633c..51f45e352 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 @@ -14,7 +14,6 @@ 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 art.arcane.iris.generation.hydrology.water.ShapedNetwork; -import org.junit.AfterClass; import org.junit.Test; import java.util.ArrayList; @@ -46,13 +45,6 @@ public class WindowPlannerTest { private static final WindowKey ORIGIN = new WindowKey(0, 0); private static final WindowKey LAND = new WindowKey(4, 4); private static final int SEAM_DISTANCE = 256; - private static final Map OPEN_SCENES = new HashMap<>(); - - @AfterClass - public static void dropScenes() { - OPEN_SCENES.clear(); - } - @Test public void zeroDensityRemovesLocalChannelsAndPonds() { HydrologySettings settings = settings(new IrisHydrology().setDensity(0D)); @@ -631,20 +623,13 @@ public class WindowPlannerTest { public void everyPondOutletLeavesThroughConnectedWater() { int outlets = 0; for (long seed = 1L; seed <= 7L; seed++) { - PlannedScene scene = openScene(seed); + PlannedScene scene = PlannedScene.plan(SyntheticTerrain.openFractal(seed), PlannedScene.packSettings(SEED), 1, 1); assertEquals("detached outlets on openFractal(" + seed + ")", List.of(), WaterInvariants.detachedOutlets(scene)); - Map byId = byId(scene.reaches()); - for (Pond pond : scene.ponds()) { - outlets += pond.kind() != PondKind.LAKE && byId.containsKey(pond.outletReachId()) ? 1 : 0; - } + outlets += WaterInvariants.checkedPonds(scene).size(); } assertTrue("pond outlets checked " + outlets, outlets >= 50); } - private static synchronized PlannedScene openScene(long seed) { - return OPEN_SCENES.computeIfAbsent(seed, key -> PlannedScene.plan(SyntheticTerrain.openFractal(key), PlannedScene.packSettings(SEED), 1, 1)); - } - private static void assertOutletLeavesAtThePondHead(Pond pond, Reach outlet) { PondMask mask = pond.mask(); int leading = 0; @@ -695,9 +680,9 @@ public class WindowPlannerTest { double nearest = Double.POSITIVE_INFINITY; int[] found = {-1, -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) { + double t = WaterInvariants.along(reach, segment, x, z); + double distance = WaterInvariants.offset(reach, segment, t, x, z); + if (distance <= WaterInvariants.width(reach, segment, t) && distance < nearest) { nearest = distance; found[0] = segment; found[1] = t < 0.5D ? segment : segment + 1; @@ -707,27 +692,8 @@ public class WindowPlannerTest { } private static boolean covers(Reach reach, int segment, int x, int z) { - double t = along(reach, segment, x, z); - return offset(reach, segment, t, x, z) <= width(reach, segment, t); - } - - private static double along(Reach reach, int segment, double x, double z) { - double ax = reach.xs()[segment]; - double az = reach.zs()[segment]; - double dx = reach.xs()[segment + 1] - ax; - double dz = reach.zs()[segment + 1] - az; - double length = dx * dx + dz * dz; - return length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D); - } - - private static double offset(Reach reach, int segment, double t, double x, double z) { - double px = reach.xs()[segment] + t * (reach.xs()[segment + 1] - reach.xs()[segment]); - double pz = reach.zs()[segment] + t * (reach.zs()[segment + 1] - reach.zs()[segment]); - return Math.hypot(x - px, z - pz); - } - - private static double width(Reach reach, int segment, double t) { - return reach.halfWidth()[segment] + t * (reach.halfWidth()[segment + 1] - reach.halfWidth()[segment]); + double t = WaterInvariants.along(reach, segment, x, z); + return WaterInvariants.offset(reach, segment, t, x, z) <= WaterInvariants.width(reach, segment, t); } private static boolean nearMask(PondMask mask, float x, float z, int distance) { diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/raster/ChunkWaterRasterTest.java b/core/src/test/java/art/arcane/iris/generation/hydrology/raster/ChunkWaterRasterTest.java index 1841f55c2..b41eb46da 100644 --- a/core/src/test/java/art/arcane/iris/generation/hydrology/raster/ChunkWaterRasterTest.java +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/raster/ChunkWaterRasterTest.java @@ -676,6 +676,25 @@ public class ChunkWaterRasterTest { } } + @Test + public void skippedLeadInKeepsTheOutletJunctionAtItsFirstVertex() { + HydrologyTier river = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH); + Reach outlet = line(201L, river, 0L, 255f, 200f, 335f, 200f, (x, z) -> 68); + Reach feeder = line(202L, river, outlet.id(), 255f, 140f, 255f, 200f, (x, z) -> 66); + Reach upstream = line(203L, river, feeder.id(), 255f, 60f, 255f, 140f, (x, z) -> z < 100f ? 70 : 62); + Pond pond = new Pond(200L, PondKind.NATURAL, 68, 60, 235, 200, 69.5f, circle(235, 200, 8, 1), outlet.id(), 0, "water", 235, 200); + assertFalse("the outlet starts off the pond", pond.mask().contains(255, 200)); + assertNotEquals("the first two outlet vertices straddle a corner-block line", + Math.floorDiv((int) Math.floor(outlet.xs()[0]) + WindowKey.SIZE / 2, WindowKey.SIZE), + Math.floorDiv((int) Math.floor(outlet.xs()[1]) + WindowKey.SIZE / 2, WindowKey.SIZE)); + Scene scene = new Scene(settings(new IrisHydrology()), (x, z) -> 75).reach(outlet).reach(feeder).reach(upstream).pond(pond); + for (int x = 260; x <= 335; x++) { + int head = scene.head(x, 200); + assertNotEquals("centre line is water at x=" + x, Integer.MIN_VALUE, head); + assertTrue("outlet water at " + head + " above the resolved feeder end 62 at x=" + x, head <= 62); + } + } + @Test public void everyBankStyleRasterizes() { IrisHydrology config = new IrisHydrology(); 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 d633f385a..3b21e08a6 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 @@ -414,6 +414,65 @@ public class ReachShaperTest { assertEquals(99, PondChannelSettlement.settle(shaped, SEA_LEVEL).ponds().get(0).level()); } + @Test + public void pondsSharingAnExitLeadInOnceFromTheirGradingPond() { + HydrologySettings settings = settings(config -> { + }); + DemGrid flat = outletGrid(); + int[] receivers = new int[flat.values().length]; + float[] accumulation = new float[receivers.length]; + BitSet channels = new BitSet(receivers.length); + outletRow(flat, receivers, accumulation, channels); + receivers[flat.index(13, 7)] = flat.index(13, 8); + accumulation[flat.index(13, 7)] = 100f; + Pond fed = new Pond(61L, PondKind.NATURAL, 99, 94, 104, 64, 100f, disk(96, 64, 5), 0L, 0, "water", 96, 64); + Pond side = new Pond(62L, PondKind.NATURAL, 99, 94, 104, 60, 100f, disk(104, 56, 3), 0L, 0, "water", 104, 56); + HeightLattice fine = new HeightLattice(new Terrain((x, z) -> fed.mask().contains((int) x, (int) z) + || side.mask().contains((int) x, (int) z) ? 94D : 100D), FINE, false, 1 << 16); + ShapedNetwork shaped = new ReachShaper(settings).shape(flat, receivers, accumulation, channels, List.of(fed, side), fine, SEED); + assertEquals("both ponds drain through the same exit", shaped.ponds().get(0).outletReachId(), shaped.ponds().get(1).outletReachId()); + Reach outlet = byId(shaped.reaches()).get(shaped.ponds().get(1).outletReachId()); + assertNotNull(outlet); + assertEquals("the exit stays the junction vertex", 104f, outlet.xs()[1], 0f); + assertEquals(64f, outlet.zs()[1], 0f); + assertTrue("one lead-in from the grading pond " + outlet.xs()[0] + "," + outlet.zs()[0], + side.mask().contains(Math.round(outlet.xs()[0]), Math.round(outlet.zs()[0]))); + assertEquals(99, outlet.grade()[0]); + } + + @Test + public void heldOutletPointsOverTheSeaStayAtTheSeaSurface() { + HydrologySettings settings = settings(config -> { + }); + int width = 24; + DemGrid surface = new DemGrid(0, 0, CELL, width, 3); + Arrays.fill(surface.values(), 60f); + int[] receivers = new int[width * 3]; + Arrays.fill(receivers, -1); + float[] accumulation = new float[width * 3]; + BitSet channels = new BitSet(width * 3); + for (int i = 0; i < 19; i++) { + flow(surface, receivers, accumulation, channels, i, 1, i + 1, 1, 1600f + 10f * i); + } + surface.set(19, 1, 30f); + Pond pond = new Pond(71L, PondKind.NATURAL, 59, 54, 136, 8, 60f, disk(128, 8, 7), 0L, 0, "water", 128, 8); + HeightLattice fine = new HeightLattice(new Terrain((x, z) -> x < 138D ? 60D : 35D), FINE, false, 1 << 16); + ShapedNetwork shaped = new ReachShaper(settings).shape(surface, receivers, accumulation, channels, List.of(pond), fine, SEED); + Pond shapedPond = shaped.ponds().get(0); + Reach outlet = byId(shaped.reaches()).get(shapedPond.outletReachId()); + assertNotNull(outlet); + assertTrue(shapedPond.mask().contains(Math.round(outlet.xs()[0]), Math.round(outlet.zs()[0]))); + assertEquals("the lip holds the pond level", shapedPond.level(), outlet.grade()[0]); + int mouths = 0; + for (int point = 0; point < outlet.grade().length; point++) { + if (outlet.style()[point] == ReachStyle.MOUTH) { + mouths++; + assertEquals("ocean point " + point + " at " + outlet.xs()[point], SEA_LEVEL, outlet.grade()[point]); + } + } + assertTrue("ocean points " + mouths, mouths > 1); + } + @Test public void gorgeCutWhenRidgeMissedByGrid() { HydrologySettings settings = settings(config -> {