diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/grid/BasinExtractor.java b/core/src/main/java/art/arcane/iris/generation/hydrology/grid/BasinExtractor.java index 98ac16f8c..0321c7731 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/grid/BasinExtractor.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/grid/BasinExtractor.java @@ -69,46 +69,6 @@ public final class BasinExtractor { return basins; } - public static BitSet inland(DemGrid natural, boolean[] ocean, int seaLevel) { - float[] heights = natural.values(); - int count = heights.length; - if (ocean.length < count) { - throw new IllegalArgumentException("Ocean flags cover " + ocean.length + " cells, grid has " + count); - } - BitSet inland = new BitSet(count); - boolean[] visited = new boolean[count]; - int[] queue = new int[count]; - for (int start = 0; start < count; start++) { - if (visited[start] || !belowSea(heights, ocean, start, seaLevel)) { - continue; - } - int tail = component(natural, cell -> belowSea(heights, ocean, cell, seaLevel), visited, queue, start); - if (enclosed(natural, ocean, queue, tail)) { - for (int index = 0; index < tail; index++) { - inland.set(queue[index]); - } - } - } - return inland; - } - - public static BitSet draining(DemGrid natural, float[] filled, BitSet inland, float minDepth) { - float[] heights = natural.values(); - BitSet basins = new BitSet(heights.length); - boolean[] visited = new boolean[heights.length]; - int[] queue = new int[heights.length]; - for (int start = inland.nextSetBit(0); start >= 0; start = inland.nextSetBit(start + 1)) { - if (visited[start]) { - continue; - } - int tail = component(natural, cell -> inland.get(cell) || filled[cell] - heights[cell] > minDepth, visited, queue, start); - for (int index = 0; index < tail; index++) { - basins.set(queue[index]); - } - } - return basins; - } - public static BitSet hollow(DemGrid natural, BitSet inland, int cell) { BitSet hollow = new BitSet(natural.values().length); if (!inland.get(cell)) { @@ -157,31 +117,6 @@ public final class BasinExtractor { return true; } - private static boolean belowSea(float[] heights, boolean[] ocean, int cell, int seaLevel) { - return !ocean[cell] && Math.round(heights[cell]) < seaLevel; - } - - private static boolean enclosed(DemGrid grid, boolean[] ocean, int[] cells, int count) { - int width = grid.width(); - int depth = grid.depth(); - for (int index = 0; index < count; index++) { - int cell = cells[index]; - int i = cell % width; - int j = cell / width; - if (i == 0 || j == 0 || i == width - 1 || j == depth - 1) { - return false; - } - for (int dz = -1; dz <= 1; dz++) { - for (int dx = -1; dx <= 1; dx++) { - if (ocean[(j + dz) * width + i + dx]) { - return false; - } - } - } - } - return true; - } - private static int component(DemGrid grid, IntPredicate member, boolean[] visited, int[] queue, int start) { int width = grid.width(); int depth = grid.depth(); diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/plan/BurnedTrunks.java b/core/src/main/java/art/arcane/iris/generation/hydrology/plan/BurnedTrunks.java index 1cd1c340b..86c61eb2e 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/plan/BurnedTrunks.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/plan/BurnedTrunks.java @@ -13,22 +13,24 @@ import java.util.Map; final class BurnedTrunks { private final BitSet cells; + private final Land land; private final int[] next; private final float[] catchment; private final float epsilon; private int[] order; private BitSet entries; - private BurnedTrunks(int count, float epsilon) { + private BurnedTrunks(int count, float epsilon, Land land) { this.cells = new BitSet(count); + this.land = land; this.next = new int[count]; this.catchment = new float[count]; this.epsilon = epsilon; Arrays.fill(next, -1); } - static BurnedTrunks burn(DemGrid raw, List reaches, float depth, float epsilon, BitSet hollows) { - BurnedTrunks trunks = new BurnedTrunks(raw.values().length, epsilon); + static BurnedTrunks burn(DemGrid raw, List reaches, float depth, float epsilon, Land land) { + BurnedTrunks trunks = new BurnedTrunks(raw.values().length, epsilon, land); Map byId = new HashMap<>(); for (Reach reach : reaches) { byId.putIfAbsent(reach.id(), reach); @@ -36,13 +38,14 @@ final class BurnedTrunks { List paths = new ArrayList<>(); for (Reach reach : reaches) { for (int[] route : TrunkRouter.route(raw, reach, byId.get(reach.downstreamId()))) { - for (int[] path : outside(route, hollows)) { + for (int[] path : outside(route, land.hollows())) { trunks.claim(path, reach.catchment()); paths.add(path); } } } StreamBurner.burn(raw, paths, depth); + trunks.keepAboveTheSea(raw.values()); trunks.sort(); trunks.descend(raw.values()); return trunks; @@ -59,9 +62,18 @@ final class BurnedTrunks { void descend(float[] heights) { for (int cell : order) { int successor = next[cell]; - if (successor >= 0) { - heights[successor] = Math.min(heights[successor], Math.min(heights[cell] - epsilon, Math.nextDown(heights[cell]))); + if (successor < 0) { + continue; } + float lowered = Math.min(heights[successor], Math.min(heights[cell] - epsilon, Math.nextDown(heights[cell]))); + if (lowered < heights[successor] && land.high(successor) && lowered <= land.seaLevel()) { + next[cell] = -1; + if (!linked(successor)) { + entries.set(successor); + } + continue; + } + heights[successor] = lowered; } } @@ -85,6 +97,23 @@ final class BurnedTrunks { } } + private boolean linked(int target) { + for (int cell = cells.nextSetBit(0); cell >= 0; cell = cells.nextSetBit(cell + 1)) { + if (next[cell] == target) { + return true; + } + } + return false; + } + + private void keepAboveTheSea(float[] heights) { + for (int cell = cells.nextSetBit(0); cell >= 0; cell = cells.nextSetBit(cell + 1)) { + if (land.high(cell)) { + heights[cell] = Math.max(heights[cell], land.seaLevel() + 1f); + } + } + } + private static List outside(int[] route, BitSet hollows) { List runs = new ArrayList<>(1); int from = 0; @@ -153,4 +182,10 @@ final class BurnedTrunks { } return Arrays.copyOf(sorted, tail); } + + record Land(float[] natural, boolean[] ocean, int seaLevel, int highGround, BitSet hollows) { + boolean high(int cell) { + return !ocean[cell] && natural[cell] > highGround; + } + } } diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/plan/InlandHollows.java b/core/src/main/java/art/arcane/iris/generation/hydrology/plan/InlandHollows.java new file mode 100644 index 000000000..ee125ac27 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/plan/InlandHollows.java @@ -0,0 +1,256 @@ +package art.arcane.iris.generation.hydrology.plan; + +import art.arcane.iris.generation.hydrology.grid.DemGrid; +import art.arcane.iris.generation.hydrology.grid.HeightLattice; +import art.arcane.iris.generation.hydrology.grid.HydrologySampleBuffer; +import art.arcane.iris.generation.hydrology.grid.PriorityFlood; +import art.arcane.iris.generation.hydrology.water.Pond; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.BitSet; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +final class InlandHollows { + private static final int MAX_HOLLOW_CELLS = 1 << 14; + private static final int BASIN_MARGIN = 64; + private static final int[] AROUND_X = {-1, 0, 1, -1, 1, -1, 0, 1}; + private static final int[] AROUND_Z = {-1, -1, -1, 0, 0, 1, 1, 1}; + + private final HeightLattice fine; + private final DemGrid natural; + private final boolean[] ocean; + private final int seaLevel; + private final int stride; + private final int originX; + private final int originZ; + private final Map heights = new HashMap<>(); + private final Set oceans = new HashSet<>(); + + private InlandHollows(HeightLattice fine, DemGrid natural, boolean[] ocean, int seaLevel) { + this.fine = fine; + this.natural = natural; + this.ocean = ocean; + this.seaLevel = seaLevel; + this.stride = natural.cell() / fine.cell(); + this.originX = Math.floorDiv(natural.originX(), natural.cell()); + this.originZ = Math.floorDiv(natural.originZ(), natural.cell()); + } + + static Hollows classify(HeightLattice fine, DemGrid natural, boolean[] ocean, int seaLevel, float minDepth, float epsilon, + List lakes) { + return new InlandHollows(fine, natural, ocean, seaLevel).classify(minDepth, epsilon, lakes); + } + + private Hollows classify(float minDepth, float epsilon, List lakes) { + int count = natural.values().length; + BitSet inland = new BitSet(count); + BitSet basins = new BitSet(count); + boolean[] seen = new boolean[count]; + for (int start = 0; start < count; start++) { + if (seen[start] || !belowSea(natural.values()[start], ocean[start])) { + continue; + } + List local = localComponent(start, seen); + if (underLake(local, lakes)) { + continue; + } + Set hollow = enclosed(local); + if (hollow == null) { + continue; + } + mark(hollow, inland); + mark(basin(hollow, minDepth, epsilon), basins); + } + return new Hollows(inland, basins); + } + + private List localComponent(int start, boolean[] seen) { + int width = natural.width(); + int depth = natural.depth(); + List cells = new ArrayList<>(); + ArrayDeque queue = new ArrayDeque<>(); + queue.add(start); + seen[start] = true; + while (!queue.isEmpty()) { + int cell = queue.poll(); + cells.add(key(originX + cell % width, originZ + cell / width)); + for (int around = 0; around < AROUND_X.length; around++) { + int i = cell % width + AROUND_X[around]; + int j = cell / width + AROUND_Z[around]; + int next = j * width + i; + if (i >= 0 && j >= 0 && i < width && j < depth && !seen[next] && belowSea(natural.values()[next], ocean[next])) { + seen[next] = true; + queue.add(next); + } + } + } + return cells; + } + + private Set enclosed(List local) { + Set members = new HashSet<>(local); + Set checked = new HashSet<>(local); + List frontier = new ArrayList<>(local); + while (!frontier.isEmpty()) { + List around = new ArrayList<>(); + for (long cell : frontier) { + for (int index = 0; index < AROUND_X.length; index++) { + long next = key(x(cell) + AROUND_X[index], z(cell) + AROUND_Z[index]); + if (checked.add(next)) { + around.add(next); + } + } + } + sample(around); + frontier = new ArrayList<>(); + for (long cell : around) { + if (oceans.contains(cell)) { + return null; + } + if (belowSea(heights.get(cell), false)) { + members.add(cell); + frontier.add(cell); + } + } + if (members.size() > MAX_HOLLOW_CELLS) { + return null; + } + } + return members; + } + + private Set basin(Set hollow, float minDepth, float epsilon) { + int minX = Integer.MAX_VALUE; + int minZ = Integer.MAX_VALUE; + int maxX = Integer.MIN_VALUE; + int maxZ = Integer.MIN_VALUE; + for (long cell : hollow) { + minX = Math.min(minX, x(cell)); + minZ = Math.min(minZ, z(cell)); + maxX = Math.max(maxX, x(cell)); + maxZ = Math.max(maxZ, z(cell)); + } + int boxX = minX - BASIN_MARGIN; + int boxZ = minZ - BASIN_MARGIN; + int width = maxX - minX + 1 + 2 * BASIN_MARGIN; + int depth = maxZ - minZ + 1 + 2 * BASIN_MARGIN; + HydrologySampleBuffer samples = new HydrologySampleBuffer(width * depth); + fine.fill(boxX * stride, boxZ * stride, width, depth, stride, samples); + DemGrid box = new DemGrid(boxX * natural.cell(), boxZ * natural.cell(), natural.cell(), width, depth); + System.arraycopy(samples.height(), 0, box.values(), 0, width * depth); + BitSet seeds = new BitSet(width * depth); + for (int cell = 0; cell < width * depth; cell++) { + int i = cell % width; + int j = cell / width; + if (samples.ocean()[cell] || i == 0 || j == 0 || i == width - 1 || j == depth - 1) { + seeds.set(cell); + } + } + float[] filled = PriorityFlood.fill(box, seeds, epsilon); + float[] values = box.values(); + Set basin = new HashSet<>(hollow); + ArrayDeque queue = new ArrayDeque<>(); + boolean[] seen = new boolean[width * depth]; + for (long cell : hollow) { + int index = (z(cell) - boxZ) * width + x(cell) - boxX; + seen[index] = true; + queue.add(index); + } + while (!queue.isEmpty()) { + int cell = queue.poll(); + for (int around = 0; around < AROUND_X.length; around++) { + int i = cell % width + AROUND_X[around]; + int j = cell / width + AROUND_Z[around]; + int next = j * width + i; + if (i >= 0 && j >= 0 && i < width && j < depth && !seen[next] && filled[next] - values[next] > minDepth) { + seen[next] = true; + queue.add(next); + basin.add(key(boxX + i, boxZ + j)); + } + } + } + return basin; + } + + private void sample(List cells) { + List missing = new ArrayList<>(); + for (long cell : cells) { + int index = local(cell); + if (index >= 0) { + heights.put(cell, natural.values()[index]); + if (ocean[index]) { + oceans.add(cell); + } + } else if (!heights.containsKey(cell)) { + missing.add(cell); + } + } + if (missing.isEmpty()) { + return; + } + int[] nodeX = new int[missing.size()]; + int[] nodeZ = new int[missing.size()]; + for (int index = 0; index < nodeX.length; index++) { + nodeX[index] = x(missing.get(index)) * stride; + nodeZ[index] = z(missing.get(index)) * stride; + } + HydrologySampleBuffer samples = new HydrologySampleBuffer(nodeX.length); + fine.fill(nodeX, nodeZ, samples); + for (int index = 0; index < nodeX.length; index++) { + heights.put(missing.get(index), samples.height()[index]); + if (samples.ocean()[index]) { + oceans.add(missing.get(index)); + } + } + } + + private boolean underLake(List cells, List lakes) { + for (Pond lake : lakes) { + for (long cell : cells) { + if (lake.mask().grownContains(x(cell) * natural.cell(), z(cell) * natural.cell())) { + return true; + } + } + } + return false; + } + + private void mark(Set cells, BitSet out) { + for (long cell : cells) { + int index = local(cell); + if (index >= 0) { + out.set(index); + } + } + } + + private int local(long cell) { + int i = x(cell) - originX; + int j = z(cell) - originZ; + return i < 0 || j < 0 || i >= natural.width() || j >= natural.depth() ? -1 : j * natural.width() + i; + } + + private boolean belowSea(float height, boolean sea) { + return !sea && Math.round(height) < seaLevel; + } + + private static long key(int x, int z) { + return ((long) x << 32) | (z & 0xFFFFFFFFL); + } + + private static int x(long key) { + return (int) (key >> 32); + } + + private static int z(long key) { + return (int) key; + } + + record Hollows(BitSet inland, BitSet basins) { + } +} diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/plan/WindowPlanner.java b/core/src/main/java/art/arcane/iris/generation/hydrology/plan/WindowPlanner.java index b1dc52fa1..b1330f231 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/plan/WindowPlanner.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/plan/WindowPlanner.java @@ -89,15 +89,15 @@ public final class WindowPlanner { System.arraycopy(samples.height(), 0, natural.values(), 0, SPAN * SPAN); DemGrid raw = new DemGrid(natural.originX(), natural.originZ(), CELL, SPAN, SPAN); System.arraycopy(natural.values(), 0, raw.values(), 0, SPAN * SPAN); - BitSet inland = BasinExtractor.inland(natural, samples.ocean(), settings.seaLevel()); - BitSet open = open(samples.ocean()); - BitSet hollowBasins = inland.isEmpty() ? inland - : BasinExtractor.draining(natural, PriorityFlood.fill(natural, open, settings.drainage().fillEpsilon()), inland, BASIN_DEPTH); List lakes = regionLakes(plans.values()); + InlandHollows.Hollows hollows = InlandHollows.classify(fine, natural, samples.ocean(), settings.seaLevel(), BASIN_DEPTH, + settings.drainage().fillEpsilon(), lakes); + BitSet inland = hollows.inland(); BurnedTrunks trunks = BurnedTrunks.burn(raw, majorReaches(key, plans), settings.majorTier().inset(), settings.drainage().fillEpsilon(), - hollowBasins); + new BurnedTrunks.Land(natural.values(), samples.ocean(), settings.seaLevel(), settings.seaLevel() + settings.deformation().maxFill(), + hollows.basins())); BitSet seeds = trunks.cells(); - seeds.or(open); + seeds.or(open(samples.ocean())); DemGrid preliminary = new DemGrid(raw.originX(), raw.originZ(), CELL, SPAN, SPAN); System.arraycopy(raw.values(), 0, preliminary.values(), 0, SPAN * SPAN); DrainContext context = new DrainContext(samples, trunks, inland); diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/grid/BasinExtractorTest.java b/core/src/test/java/art/arcane/iris/generation/hydrology/grid/BasinExtractorTest.java index 0b7b64631..c14d2b361 100644 --- a/core/src/test/java/art/arcane/iris/generation/hydrology/grid/BasinExtractorTest.java +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/grid/BasinExtractorTest.java @@ -7,7 +7,6 @@ import java.util.BitSet; import java.util.List; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class BasinExtractorTest { @@ -55,32 +54,24 @@ public class BasinExtractorTest { } @Test - public void inlandHollowsAreBelowTheSeaAndRingedByLand() { + public void inlandHollowsBecomeBelowSeaBasinsOnceDrained() { DemGrid natural = new DemGrid(0, 0, 8, 17, 17); Arrays.fill(natural.values(), 60f); - boolean[] ocean = new boolean[natural.values().length]; + BitSet inland = new BitSet(natural.values().length); for (int j = 6; j <= 8; j++) { for (int i = 6; i <= 8; i++) { natural.set(i, j, i == 7 && j == 7 ? 20f : 30f); + inland.set(natural.index(i, j)); } } - natural.set(0, 12, 30f); - natural.set(1, 12, 30f); natural.set(12, 12, 30f); - natural.set(13, 12, 10f); - ocean[natural.index(13, 12)] = true; - natural.set(12, 2, SEA + 0.4f); - BitSet inland = BasinExtractor.inland(natural, ocean, (int) SEA); - assertEquals("only the ringed hollow is inland " + inland, 9, inland.cardinality()); - assertTrue(inland.get(natural.index(7, 7))); - assertFalse("a lowland open to the window edge is not inland", inland.get(natural.index(1, 12))); - assertFalse("a lowland beside the sea is not inland", inland.get(natural.index(12, 12))); - assertFalse("ground that rounds to the sea surface is not below it", inland.get(natural.index(12, 2))); - assertEquals(inland, BasinExtractor.hollow(natural, inland, natural.index(6, 8))); + inland.set(natural.index(12, 12)); + BitSet first = BasinExtractor.hollow(natural, inland, natural.index(6, 8)); + assertEquals("one hollow is the 3 by 3 pit " + first, 9, first.cardinality()); assertTrue(BasinExtractor.hollow(natural, inland, natural.index(3, 3)).isEmpty()); float[] filled = natural.values().clone(); List hollows = BasinExtractor.hollows(natural, inland, filled, (int) SEA, 5); - assertEquals(1, hollows.size()); + assertEquals(2, hollows.size()); Basin hollow = hollows.get(0); assertTrue(hollow.belowSea()); assertEquals(5, hollow.id()); @@ -89,8 +80,8 @@ public class BasinExtractorTest { assertEquals(SEA - 20f, hollow.maxDepth(), 0f); assertEquals(9, hollow.cells().length); filled[natural.index(6, 6)] = SEA + 1f; - assertTrue("a hollow that still fills above the sea is an ordinary basin", - BasinExtractor.hollows(natural, inland, filled, (int) SEA, 0).isEmpty()); + assertEquals("a hollow that still fills above the sea is an ordinary basin", 1, + BasinExtractor.hollows(natural, inland, filled, (int) SEA, 0).size()); } private static DemGrid bowl() { diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/BurnedTrunksTest.java b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/BurnedTrunksTest.java index b473bea71..8422eb639 100644 --- a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/BurnedTrunksTest.java +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/BurnedTrunksTest.java @@ -19,6 +19,9 @@ public class BurnedTrunksTest { private static final int CELL = 8; private static final int SIZE = 32; private static final int TRENCH_ROW = 10; + private static final float SEA = 40f; + private static final int HIGH_GROUND = 6; + private static final float DEEP_BURN = 10f; private static final int HOLLOW_FROM = 8; private static final int HOLLOW_TO = 12; @@ -32,7 +35,7 @@ public class BurnedTrunksTest { HydrologyTier major = HydrologySettings.compile(new IrisHydrology(), 1337L, 40, -64, 320).majorTier(); Reach feeder = reach(1L, 2L, major, 2, 20); Reach backtrack = reach(2L, 0L, major, 20, 12); - BurnedTrunks trunks = BurnedTrunks.burn(raw, List.of(feeder, backtrack), 3f, 0.001f, new BitSet()); + BurnedTrunks trunks = BurnedTrunks.burn(raw, List.of(feeder, backtrack), 3f, 0.001f, land(raw, new BitSet())); int[] receivers = new int[SIZE * SIZE]; Arrays.fill(receivers, -1); trunks.route(receivers); @@ -61,7 +64,7 @@ public class BurnedTrunksTest { } } HydrologyTier major = HydrologySettings.compile(new IrisHydrology(), 1337L, 40, -64, 320).majorTier(); - BurnedTrunks trunks = BurnedTrunks.burn(raw, List.of(reach(1L, 0L, major, 2, 24)), 3f, 0.001f, basin); + BurnedTrunks trunks = BurnedTrunks.burn(raw, List.of(reach(1L, 0L, major, 2, 24)), 3f, 0.001f, land(raw, basin)); int[] receivers = new int[SIZE * SIZE]; Arrays.fill(receivers, -1); trunks.route(receivers); @@ -86,6 +89,39 @@ public class BurnedTrunksTest { assertTrue("the trunk beyond the hollow restarts with the river's catchment", weights[raw.index(HOLLOW_TO + 1, TRENCH_ROW)] > 0f); } + @Test + public void burnedTrunkNeverDigsBelowTheSeaOnDryLand() { + DemGrid raw = new DemGrid(0, 0, CELL, SIZE, SIZE); + Arrays.fill(raw.values(), 200f); + for (int i = 0; i < SIZE; i++) { + raw.set(i, TRENCH_ROW, i < HOLLOW_FROM ? SEA + HIGH_GROUND + 2f : i <= HOLLOW_TO ? SEA - 10f : 60f); + } + HydrologyTier major = HydrologySettings.compile(new IrisHydrology(), 1337L, 40, -64, 320).majorTier(); + BurnedTrunks trunks = BurnedTrunks.burn(raw, List.of(reach(1L, 0L, major, 2, 24)), DEEP_BURN, 0.001f, land(raw, new BitSet())); + for (int i = 2; i < HOLLOW_FROM; i++) { + float height = raw.values()[raw.index(i, TRENCH_ROW)]; + assertTrue("a deep burn on high ground stays above the sea at " + i + ": " + height, height > SEA && height <= SEA + 1f); + } + int restart = raw.index(HOLLOW_TO + 1, TRENCH_ROW); + assertEquals("the trunk is not dragged below the sea where it climbs out of low ground", 60f - DEEP_BURN, raw.values()[restart], 0f); + int[] receivers = new int[SIZE * SIZE]; + Arrays.fill(receivers, -1); + trunks.route(receivers); + assertEquals("the trunk link up out of low ground is cut", -1, receivers[raw.index(HOLLOW_TO, TRENCH_ROW)]); + float[] weights = new float[SIZE * SIZE]; + trunks.inject(weights); + assertTrue("the trunk restarts with the river's catchment", weights[restart] > 0f); + for (int i = HOLLOW_TO + 1; i < 24; i++) { + assertTrue("the trunk beyond descends from its own height at " + i, + raw.values()[raw.index(i + 1, TRENCH_ROW)] < raw.values()[raw.index(i, TRENCH_ROW)]); + assertTrue("the trunk beyond stays above the sea at " + i, raw.values()[raw.index(i, TRENCH_ROW)] > 45f); + } + } + + private static BurnedTrunks.Land land(DemGrid raw, BitSet hollows) { + return new BurnedTrunks.Land(raw.values().clone(), new boolean[raw.values().length], (int) SEA, (int) SEA + HIGH_GROUND, hollows); + } + private static Reach reach(long id, long downstreamId, HydrologyTier tier, int fromI, int toI) { float[] xs = {fromI * CELL, toI * CELL}; float[] zs = {TRENCH_ROW * CELL, TRENCH_ROW * CELL}; diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/InlandHollowsTest.java b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/InlandHollowsTest.java new file mode 100644 index 000000000..1e32596f6 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/InlandHollowsTest.java @@ -0,0 +1,104 @@ +package art.arcane.iris.generation.hydrology.plan; + +import art.arcane.iris.generation.hydrology.grid.DemGrid; +import art.arcane.iris.generation.hydrology.grid.HeightLattice; +import art.arcane.iris.generation.hydrology.grid.HydrologySampleBuffer; +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 org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class InlandHollowsTest { + private static final int SEA_LEVEL = SyntheticTerrain.SEA_LEVEL; + private static final int CELL = 8; + private static final int SIZE = 128; + private static final double PIT_X = 512D; + private static final double PIT_Z = 512D; + private static final double PIT_RADIUS = 100D; + private static final double PIT_DEPTH = 64D; + private static final int LAKE_STEP = 16; + + @Test + public void hollowCutByTheGridEdgeIsClassifiedLikeAWholeOne() { + SyntheticTerrain terrain = SyntheticTerrain.valleySlope().withPit(PIT_X, PIT_Z, PIT_RADIUS, PIT_DEPTH).withLand((x, z) -> true); + HeightLattice fine = new HeightLattice(terrain, WindowPlanner.FINE_CELL, false, 1 << 20); + Window whole = new Window(fine, 0, 0); + Window cut = new Window(fine, (int) PIT_X / CELL, 0); + InlandHollows.Hollows wholeView = whole.classify(); + InlandHollows.Hollows cutView = cut.classify(); + int inland = 0; + int basin = 0; + for (int j = 0; j < SIZE; j++) { + for (int i = 0; i < SIZE - (int) PIT_X / CELL; i++) { + int inWhole = j * SIZE + i + (int) PIT_X / CELL; + int inCut = j * SIZE + i; + assertEquals("inland at cell " + i + "," + j, wholeView.inland().get(inWhole), cutView.inland().get(inCut)); + assertEquals("hollow basin at cell " + i + "," + j, wholeView.basins().get(inWhole), cutView.basins().get(inCut)); + inland += cutView.inland().get(inCut) ? 1 : 0; + basin += cutView.basins().get(inCut) ? 1 : 0; + } + } + assertTrue("the grid edge cuts through inland cells " + inland, inland > 20); + assertTrue("the basin reaches past the below-sea floor up the walls " + basin + " > " + inland, basin > inland + 20); + int crest = cut.index(PIT_X + PIT_RADIUS + 8D, PIT_Z); + assertTrue("the rim crest is outside the basin", !cutView.basins().get(crest)); + } + + @Test + public void lowlandOpenToTheSeaIsNotInland() { + SyntheticTerrain terrain = SyntheticTerrain.valleySlope().withPit(PIT_X, PIT_Z, PIT_RADIUS, PIT_DEPTH) + .withLand((x, z) -> Math.hypot(x - PIT_X, z - PIT_Z) > PIT_RADIUS * 0.3D); + HeightLattice fine = new HeightLattice(terrain, WindowPlanner.FINE_CELL, false, 1 << 20); + InlandHollows.Hollows hollows = new Window(fine, 0, 0).classify(); + assertEquals("below-sea land around a patch of sea is the sea's shore, not an inland hollow", 0, hollows.inland().cardinality()); + assertEquals(0, hollows.basins().cardinality()); + } + + @Test + public void hollowHeldByARegionLakeIsLeftToTheLake() { + SyntheticTerrain terrain = SyntheticTerrain.valleySlope().withPit(PIT_X, PIT_Z, PIT_RADIUS, PIT_DEPTH).withLand((x, z) -> true); + HeightLattice fine = new HeightLattice(terrain, WindowPlanner.FINE_CELL, false, 1 << 20); + Window window = new Window(fine, 0, 0); + assertTrue("the hollow is inland without a lake", window.classify().inland().cardinality() > 20); + int nodes = (int) (2D * PIT_RADIUS) / LAKE_STEP + 1; + long[] bits = new long[(nodes * nodes + 63) >>> 6]; + for (int cell = 0; cell < nodes * nodes; cell++) { + bits[cell >>> 6] |= 1L << cell; + } + int origin = (int) (PIT_X - PIT_RADIUS); + PondMask mask = new PondMask(origin, origin, LAKE_STEP, nodes, nodes, bits); + Pond lake = new Pond(5L, PondKind.LAKE, 74, 14, origin, (int) PIT_Z, 77f, mask, 0L, 0, "water", (int) PIT_X, (int) PIT_Z); + InlandHollows.Hollows held = InlandHollows.classify(fine, window.natural, window.ocean, SEA_LEVEL, 0.5f, 0.001f, List.of(lake)); + assertEquals("a region lake holds the hollow, so it is no inland hollow", 0, held.inland().cardinality()); + assertEquals("and its basin keeps the trunk", 0, held.basins().cardinality()); + } + + private static final class Window { + private final HeightLattice fine; + private final DemGrid natural; + private final boolean[] ocean; + + private Window(HeightLattice fine, int cellX, int cellZ) { + this.fine = fine; + HydrologySampleBuffer samples = new HydrologySampleBuffer(SIZE * SIZE); + int stride = CELL / WindowPlanner.FINE_CELL; + fine.fill(cellX * stride, cellZ * stride, SIZE, SIZE, stride, samples); + this.natural = new DemGrid(cellX * CELL, cellZ * CELL, CELL, SIZE, SIZE); + System.arraycopy(samples.height(), 0, natural.values(), 0, SIZE * SIZE); + this.ocean = samples.ocean().clone(); + } + + private InlandHollows.Hollows classify() { + return InlandHollows.classify(fine, natural, ocean, SEA_LEVEL, 0.5f, 0.001f, List.of()); + } + + private int index(double x, double z) { + return (int) Math.floor((z - natural.originZ()) / CELL) * SIZE + (int) Math.floor((x - natural.originX()) / CELL); + } + } +} diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/SyntheticTerrain.java b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/SyntheticTerrain.java index 10d1d5423..049e088c2 100644 --- a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/SyntheticTerrain.java +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/SyntheticTerrain.java @@ -275,6 +275,15 @@ final class SyntheticTerrain implements HydrologyTerrainSource { return new SyntheticTerrain((x, z) -> x >= stripMinX && x < stripMinX + stripWidth ? SEA_LEVEL - 2D : valleySlope(x, z)); } + static SyntheticTerrain walledValley(double axisZ, double halfWidth, double seaX) { + double wallTop = 300D; + double wallSlope = 2D; + return new SyntheticTerrain((x, z) -> { + double valley = x >= seaX ? OCEAN_FLOOR : SEA_LEVEL + 0.07D * (seaX - x) + 0.1D * Math.abs(z - axisZ); + return Math.max(valley, wallTop - wallSlope * Math.abs(halfWidth - Math.abs(z - axisZ))); + }); + } + static SyntheticTerrain valleySlope() { return new SyntheticTerrain(SyntheticTerrain::valleySlope); } 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 b05590cfe..a122c060b 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 @@ -63,6 +63,11 @@ public class WindowPlannerTest { private static final int DEEP_CUT = 12; private static final int SLIVER_REACH = 32; private static final int RIM_CUT = 24; + private static final double VALLEY_HALF_WIDTH = 200D; + private static final double VALLEY_SEA_INSET = 320D; + private static final double LINE_STREAM_CATCHMENT = 10000D; + private static final double LINE_HOLLOW_RADIUS = 240D; + private static final double LINE_HOLLOW_DEPTH = 90D; private static final double HOLLOW_EDGE = 8D; private static final double CRATER_X = 512D; private static final double CRATER_Z = 512D; @@ -1067,11 +1072,24 @@ public class WindowPlannerTest { assertNoSeaLevelCanyon(scene, hollow); } + private static boolean inSquare(WindowKey key, float[] point) { + return inSquare(key, point[0], point[1]); + } + + private static float[] lastPoint(Reach reach) { + int last = reach.xs().length - 1; + return new float[]{reach.xs()[last], reach.zs()[last]}; + } + private static void assertNoSeaLevelCanyon(PlannedScene scene, SyntheticTerrain terrain) { - for (Reach reach : scene.reaches()) { + assertNoSeaLevelCanyon(scene.reaches(), scene.ponds(), terrain); + } + + private static void assertNoSeaLevelCanyon(List reaches, List ponds, SyntheticTerrain terrain) { + for (Reach reach : reaches) { for (int point = 0; point < reach.grade().length; point++) { double ground = terrain.natural(reach.xs()[point], reach.zs()[point]); - if (pondAt(scene.ponds(), reach.xs()[point], reach.zs()[point]) == null) { + if (pondAt(ponds, reach.xs()[point], reach.zs()[point]) == null) { assertTrue(reach + " point " + point + " is held at the sea under ground at " + ground, reach.grade()[point] > SEA_LEVEL + 2 || ground < SEA_LEVEL + RIM_CUT); } @@ -1093,6 +1111,56 @@ public class WindowPlannerTest { return deep; } + @Test + public void hollowOnAWindowLineIsPlannedAlikeByBothNeighbours() { + IrisHydrology config = new IrisHydrology(); + config.getLakes().setEnabled(false); + config.getPonds().getSeeded().setAlongChannels(false); + config.getTiers().get(0).setMinCatchment(LINE_STREAM_CATCHMENT); + HydrologySettings settings = settings(config); + int line = LAND.wx() * WindowKey.SIZE; + double axis = LAND.wz() * WindowKey.SIZE + SEAM_DISTANCE; + SyntheticTerrain terrain = SyntheticTerrain.walledValley(axis, VALLEY_HALF_WIDTH, line + 2 * WindowKey.SIZE - VALLEY_SEA_INSET) + .withPit(line, axis, LINE_HOLLOW_RADIUS, LINE_HOLLOW_DEPTH) + .withLand((x, z) -> Math.hypot(x - line, z - axis) < LINE_HOLLOW_RADIUS + 2D * WindowPlanner.CELL); + assertTrue("the hollow floor lies far below the sea", terrain.natural(line, axis) < SEA_LEVEL - 20D); + WindowKey west = LAND; + WindowKey east = new WindowKey(LAND.wx() + 1, LAND.wz()); + assertEquals("the east window's planning area starts on the hollow's centre line", line, (east.wx() - 1) * WindowKey.SIZE); + Map cache = new HashMap<>(); + WindowComputation westView = planner(settings, terrain).compute(west, regionPlans(settings, terrain, west, cache)); + WindowComputation eastView = planner(settings, terrain).compute(east, regionPlans(settings, terrain, east, cache)); + Set westOwned = nearSeamReaches(west, east, westView.owned().reaches()); + Set eastOwned = nearSeamReaches(east, west, eastView.owned().reaches()); + assertEquals(west + " reaches seen from " + east, westOwned, nearSeamReaches(west, east, eastView.allReaches())); + assertEquals(east + " reaches seen from " + west, eastOwned, nearSeamReaches(east, west, westView.allReaches())); + List owned = new ArrayList<>(westView.owned().reaches()); + owned.addAll(eastView.owned().reaches()); + List water = new ArrayList<>(westView.allPonds()); + water.addAll(eastView.allPonds()); + for (RegionPlan region : cache.values()) { + water.addAll(region.lakes()); + } + assertNoSeaLevelCanyon(owned, water, terrain); + Map byId = byId(owned); + int links = 0; + for (Reach feeder : owned) { + Reach downstream = byId.get(feeder.downstreamId()); + if (downstream == null || inSquare(west, lastPoint(feeder)) == inSquare(west, lastPoint(downstream))) { + continue; + } + links++; + int last = feeder.xs().length - 1; + int drop = feeder.grade()[last] - downstream.grade()[0]; + assertTrue(feeder + " meets " + downstream + " across the window line with a drop of " + drop, + drop >= 0 && drop < downstream.tier().waterfallMinimumDrop()); + } + assertTrue("reaches near the window line " + westOwned.size() + " + " + eastOwned.size(), westOwned.size() + eastOwned.size() >= 3); + assertTrue("reach links between the two windows' plans " + links, links >= 1); + assertTrue("a major river crosses the window line", tier(new ArrayList<>(eastOwned), settings.majorTier()).size() + + tier(new ArrayList<>(westOwned), settings.majorTier()).size() >= 1); + } + @Test public void deepInlandHollowsKeepTheirRim() { HydrologySettings settings = settings(new IrisHydrology());