From d61b5c3041bde98b32fa134d13323c713c1869c0 Mon Sep 17 00:00:00 2001 From: Brian Neumann-Fopiano Date: Mon, 2 Nov 2026 00:19:04 -0500 Subject: [PATCH] Delta planner split - Move delta planning out of ReachShaper into a package-private DeltaPlanner that ReachShaper calls once - Drop the redundant tier parameter from the distributary methods - Test delta crossings through a new raster ChannelCoverage built on ReachSegments and ReachCursor, limited to segments near the channel - Keep DELTA_STEP in HydrologySettings beside its extent guard - Share the delta coast fixture through testsupport and split the pond, hill and neighbouring-river cases into their own tests - Cover delta rendering: MOUTH sea-fluid water at sea level, no dry gap or leak at the branch, and the river above its estuary keeping its head --- .../hydrology/plan/HydrologySettings.java | 6 +- .../hydrology/raster/ChannelCoverage.java | 26 ++ .../hydrology/water/DeltaPlanner.java | 256 ++++++++++++++++ .../hydrology/water/ReachShaper.java | 276 +----------------- .../raster/ChunkWaterRasterTest.java | 98 +++++++ .../hydrology/water/ReachShaperTest.java | 157 ++++------ .../arcane/iris/testsupport/DeltaCoast.java | 155 ++++++++++ 7 files changed, 605 insertions(+), 369 deletions(-) create mode 100644 core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChannelCoverage.java create mode 100644 core/src/main/java/art/arcane/iris/generation/hydrology/water/DeltaPlanner.java create mode 100644 core/src/test/java/art/arcane/iris/testsupport/DeltaCoast.java diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/plan/HydrologySettings.java b/core/src/main/java/art/arcane/iris/generation/hydrology/plan/HydrologySettings.java index 6e83583d1..92583cceb 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/plan/HydrologySettings.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/plan/HydrologySettings.java @@ -23,7 +23,6 @@ import art.arcane.iris.generation.hydrology.config.IrisPondShore; import art.arcane.iris.generation.hydrology.config.IrisSeaInlets; import art.arcane.iris.generation.hydrology.underground.SeaInletRasterizer; import art.arcane.iris.generation.hydrology.water.HydrologyTier; -import art.arcane.iris.generation.hydrology.water.ReachShaper; import art.arcane.iris.generation.noise.IrisGeneratorStyle; import art.arcane.iris.generation.terrain.IrisMaterialPalette; import com.google.gson.Gson; @@ -48,6 +47,7 @@ public final class HydrologySettings { private static final int MIN_DELTA_LENGTH = 8; private static final int MAX_DELTA_LENGTH = 384; public static final float MAX_HALF_WIDTH = 64f; + public static final int DELTA_STEP = 2; private static final double MIN_INLET_HALF_WIDTH = 1D; private static final double MAX_INLET_HALF_WIDTH = 16D; private static final int MIN_INLET_DEPTH = 2; @@ -272,10 +272,10 @@ public final class HydrologySettings { throw new IllegalArgumentException(named + " has length " + config.getLength() + "; it must stay within " + MIN_DELTA_LENGTH + ".." + MAX_DELTA_LENGTH); } - long extent = (long) config.getLength() + ReachShaper.DELTA_STEP + (long) Math.ceil(maxHalfWidth) + tier.getBankBand() + 1L; + long extent = (long) config.getLength() + DELTA_STEP + (long) Math.ceil(maxHalfWidth) + tier.getBankBand() + 1L; if (config.getChannels() > 0 && extent > WindowKey.SIZE) { throw new IllegalArgumentException(named + " reaches " + extent + " blocks from its sea end (length " + config.getLength() - + " + " + ReachShaper.DELTA_STEP + " for the last step + halfWidth max " + maxHalfWidth + " + bankBand " + tier.getBankBand() + + " + " + DELTA_STEP + " for the last step + halfWidth max " + maxHalfWidth + " + bankBand " + tier.getBankBand() + " + 1); a delta channel and its banks must stay within " + WindowKey.SIZE + " blocks"); } return new HydrologyTier.Delta(config.getChannels(), (float) config.getSpreadDegrees(), config.getLength()); diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChannelCoverage.java b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChannelCoverage.java new file mode 100644 index 000000000..cae1d9a7c --- /dev/null +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChannelCoverage.java @@ -0,0 +1,26 @@ +package art.arcane.iris.generation.hydrology.raster; + +import art.arcane.iris.generation.hydrology.water.Reach; + +import java.util.List; + +public final class ChannelCoverage { + private static final int CAPACITY = 64; + + private final ReachSegments segments = new ReachSegments(CAPACITY); + private final ReachCursor cursor = new ReachCursor(); + + public ChannelCoverage(List reaches, float minX, float minZ, float maxX, float maxZ) { + for (Reach reach : reaches) { + segments.addNear(reach, minX, minZ, maxX, maxZ, 0f); + } + } + + public int grade(int x, int z) { + if (segments.isEmpty()) { + return Integer.MIN_VALUE; + } + segments.nearest(x, z, ReachSegments.Scope.CHANNEL, cursor); + return cursor.channel.reach == null ? Integer.MIN_VALUE : cursor.channel.grade(); + } +} diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/water/DeltaPlanner.java b/core/src/main/java/art/arcane/iris/generation/hydrology/water/DeltaPlanner.java new file mode 100644 index 000000000..92c64910a --- /dev/null +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/water/DeltaPlanner.java @@ -0,0 +1,256 @@ +package art.arcane.iris.generation.hydrology.water; + +import art.arcane.iris.generation.hydrology.grid.CellHash; +import art.arcane.iris.generation.hydrology.grid.HeightLattice; +import art.arcane.iris.generation.hydrology.grid.HydrologySampleBuffer; +import art.arcane.iris.generation.hydrology.plan.HydrologySettings; +import art.arcane.iris.generation.hydrology.raster.ChannelCoverage; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +final class DeltaPlanner { + private static final long SALT_DELTA = 0x44454c5441L; + private static final float CLEARANCE = 2f; + private static final double BEND = 0.5D; + private static final double JITTER = 0.25D; + + private final HydrologySettings settings; + + DeltaPlanner(HydrologySettings settings) { + this.settings = settings; + } + + List plan(List drafts, List reaches, List ponds, HeightLattice fine, long seed) { + List mouths = new ArrayList<>(); + for (int index = 0; index < drafts.size(); index++) { + ReachDraft draft = drafts.get(index); + if (draft.tier.delta().channels() > 0 && draft.chain.end() == ReachTracer.End.OCEAN) { + mouths.add(index); + } + } + if (mouths.isEmpty()) { + return List.of(); + } + mouths.sort(Comparator.comparingLong((Integer index) -> drafts.get(index).id).thenComparingDouble(index -> drafts.get(index).xs[0]) + .thenComparingDouble(index -> drafts.get(index).zs[0])); + List accepted = new ArrayList<>(); + for (int index : mouths) { + Reach parent = reaches.get(index); + List earlier = List.copyOf(accepted); + for (Reach channel : distributaries(drafts.get(index), fine, seed)) { + if (!crosses(channel, parent, reaches, earlier, ponds)) { + accepted.add(channel); + } + } + } + return accepted; + } + + private List distributaries(ReachDraft mouth, HeightLattice fine, long seed) { + HydrologyTier.Delta delta = mouth.tier.delta(); + int last = mouth.xs.length - 1; + int start = estuaryStart(mouth.style); + int channels = delta.channels(); + float halfWidth = Math.min(mouth.tier.maxHalfWidth(), mouth.halfWidth[start] / (float) Math.sqrt(channels)); + float[] distance = ReachShaper.distances(mouth.xs, mouth.zs); + float lip = start == 0 ? 0f : 0.5f * (distance[start] - distance[start - 1]); + float branch = distance[start] + halfWidth + CLEARANCE + 1f + lip; + double axisX = mouth.xs[last] - mouth.xs[start]; + double axisZ = mouth.zs[last] - mouth.zs[start]; + if (start == last || branch >= distance[last] || Math.hypot(axisX, axisZ) < 1e-3D) { + return List.of(); + } + float[] at = pointAt(mouth.xs, mouth.zs, distance, start, branch); + double heading = Math.atan2(axisZ, axisX); + long choices = CellHash.mix(seed, SALT_DELTA, (int) Math.floor(mouth.xs[last]), (int) Math.floor(mouth.zs[last])); + int firstSide = CellHash.unit(CellHash.mix(choices, SALT_DELTA, -1, 0)) < 0.5D ? 1 : -1; + List made = new ArrayList<>(channels); + for (int channel = 0; channel < channels; channel++) { + long channelChoices = CellHash.mix(choices, SALT_DELTA, channel, 0); + Fan fan = new Fan(CellHash.mix(mouth.id, SALT_DELTA, channel, channels), at[0], at[1], heading, + angle(delta, channelChoices, channel, firstSide), BEND * CellHash.unit(CellHash.mix(channelChoices, SALT_DELTA, 2, 0)), + halfWidth); + Reach reach = distributary(mouth, fine, fan); + if (reach != null) { + made.add(reach); + } + } + return made; + } + + private static int estuaryStart(ReachStyle[] style) { + int start = style.length - 1; + if (style[start] != ReachStyle.MOUTH) { + return start; + } + while (start > 0 && style[start - 1] == ReachStyle.MOUTH) { + start--; + } + return start; + } + + private static float[] pointAt(float[] xs, float[] zs, float[] distance, int from, float along) { + int segment = from; + while (distance[segment + 1] < along) { + segment++; + } + float share = (along - distance[segment]) / Math.max(1e-6f, distance[segment + 1] - distance[segment]); + return new float[]{xs[segment] + share * (xs[segment + 1] - xs[segment]), zs[segment] + share * (zs[segment + 1] - zs[segment])}; + } + + private static double angle(HydrologyTier.Delta delta, long channelChoices, int channel, int firstSide) { + int side = channel % 2 == 0 ? firstSide : -firstSide; + int onSide = side == firstSide ? (delta.channels() + 1) / 2 : delta.channels() / 2; + double turn = CellHash.unit(CellHash.mix(channelChoices, SALT_DELTA, 1, 0)); + return side * Math.toRadians(delta.spreadDegrees()) * (channel / 2 + 1 - JITTER * turn) / onSide; + } + + private Reach distributary(ReachDraft mouth, HeightLattice fine, Fan fan) { + int step = HydrologySettings.DELTA_STEP; + int length = mouth.tier.delta().length(); + int stations = (length + step - 1) / step + 1; + float[] xs = new float[stations]; + float[] zs = new float[stations]; + xs[0] = fan.x(); + zs[0] = fan.z(); + for (int station = 1; station < stations; station++) { + double direction = fan.heading() + fan.angle() * (1D - fan.bend() * ((station - 1) * step) / length); + xs[station] = (float) (xs[station - 1] + step * Math.cos(direction)); + zs[station] = (float) (zs[station - 1] + step * Math.sin(direction)); + } + int cell = fine.cell(); + int[] nodeX = new int[3 * stations]; + int[] nodeZ = new int[3 * stations]; + for (int station = 0; station < stations; station++) { + int next = Math.min(stations - 1, station + 1); + int previous = Math.max(0, station - 1); + double dx = xs[next] - xs[previous]; + double dz = zs[next] - zs[previous]; + double run = Math.max(1e-6D, Math.hypot(dx, dz)); + for (int offset = -1; offset <= 1; offset++) { + int node = 3 * station + offset + 1; + nodeX[node] = (int) Math.floor((xs[station] - offset * fan.halfWidth() * dz / run) / cell + 0.5D); + nodeZ[node] = (int) Math.floor((zs[station] + offset * fan.halfWidth() * dx / run) / cell + 0.5D); + } + } + HydrologySampleBuffer samples = new HydrologySampleBuffer(nodeX.length); + fine.fill(nodeX, nodeZ, samples); + float highest = settings.seaLevel() + settings.deformation().maxFill(); + int sea = -1; + for (int station = 0; station < stations; station++) { + if (samples.ocean()[3 * station + 1]) { + sea = station; + break; + } + for (int node = 3 * station; node < 3 * station + 3; node++) { + if (samples.height()[node] > highest) { + return null; + } + } + } + if (sea < 2) { + return null; + } + int end = Math.min(stations - 1, sea + 1); + return reach(mouth, fan, Arrays.copyOf(xs, end + 1), Arrays.copyOf(zs, end + 1)); + } + + private Reach reach(ReachDraft mouth, Fan fan, float[] xs, float[] zs) { + HydrologyTier tier = mouth.tier; + int count = xs.length; + float[] distance = ReachShaper.distances(xs, zs); + int[] grade = new int[count]; + float[] halfWidth = new float[count]; + float[] depth = new float[count]; + ReachStyle[] style = new ReachStyle[count]; + float channelDepth = tier.depth(fan.halfWidth()); + for (int point = 0; point < count; point++) { + grade[point] = settings.seaLevel(); + halfWidth[point] = fan.halfWidth(); + depth[point] = channelDepth + (HydrologyTier.MINIMUM_DEPTH - channelDepth) * distance[point] / distance[count - 1]; + style[point] = ReachStyle.MOUTH; + } + return new Reach(fan.id(), tier, 0L, xs, zs, grade, halfWidth, depth, style, mouth.catchment / tier.delta().channels()); + } + + private boolean crosses(Reach channel, Reach parent, List reaches, List earlier, List ponds) { + float reach = channel.halfWidth()[0] + CLEARANCE; + int minX = (int) Math.floor(minimum(channel.xs()) - reach); + int minZ = (int) Math.floor(minimum(channel.zs()) - reach); + int maxX = (int) Math.ceil(maximum(channel.xs()) + reach); + int maxZ = (int) Math.ceil(maximum(channel.zs()) + reach); + List others = new ArrayList<>(reaches.size() + earlier.size()); + for (Reach other : reaches) { + if (other != parent) { + others.add(other); + } + } + others.addAll(earlier); + ChannelCoverage otherWater = new ChannelCoverage(others, minX, minZ, maxX, maxZ); + ChannelCoverage parentWater = new ChannelCoverage(List.of(parent), minX, minZ, maxX, maxZ); + List nearby = new ArrayList<>(); + for (Pond pond : ponds) { + if (near(pond.mask(), minX, minZ, maxX, maxZ)) { + nearby.add(pond); + } + } + for (int z = minZ; z <= maxZ; z++) { + for (int x = minX; x <= maxX; x++) { + if (distanceTo(channel, x, z) > reach) { + continue; + } + for (Pond pond : nearby) { + if (pond.mask().contains(x, z)) { + return true; + } + } + if (otherWater.grade(x, z) != Integer.MIN_VALUE || parentWater.grade(x, z) > settings.seaLevel()) { + return true; + } + } + } + return false; + } + + private static double distanceTo(Reach reach, double x, double z) { + double nearest = Double.POSITIVE_INFINITY; + for (int segment = 0; segment + 1 < reach.xs().length; segment++) { + 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; + double t = length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D); + nearest = Math.min(nearest, Math.hypot(x - (ax + t * dx), z - (az + t * dz))); + } + return nearest; + } + + private static boolean near(PondMask mask, int minX, int minZ, int maxX, int maxZ) { + int half = mask.step() >> 1; + return mask.originX() - half <= maxX && mask.originX() + (mask.width() - 1) * mask.step() + half >= minX + && mask.originZ() - half <= maxZ && mask.originZ() + (mask.depth() - 1) * mask.step() + half >= minZ; + } + + private static float minimum(float[] values) { + float minimum = Float.POSITIVE_INFINITY; + for (float value : values) { + minimum = Math.min(minimum, value); + } + return minimum; + } + + private static float maximum(float[] values) { + float maximum = Float.NEGATIVE_INFINITY; + for (float value : values) { + maximum = Math.max(maximum, value); + } + return maximum; + } + + private record Fan(long id, float x, float z, double heading, double angle, double bend, float halfWidth) { + } +} 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 3dd54203e..59f763107 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 @@ -3,7 +3,6 @@ package art.arcane.iris.generation.hydrology.water; import art.arcane.iris.generation.hydrology.grid.CellHash; 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.plan.HydrologySettings; import art.arcane.iris.generation.hydrology.plan.WindowKey; import art.arcane.iris.generation.hydrology.raster.LinkBlend; @@ -11,12 +10,10 @@ import art.arcane.iris.generation.hydrology.raster.LinkBlend; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; -import java.util.Comparator; import java.util.List; import java.util.Objects; public final class ReachShaper { - public static final int DELTA_STEP = 2; private static final int SMOOTHING_PASSES = 2; private static final float TAPER_LENGTH = 24f; private static final float WAIST = 0.5f; @@ -29,15 +26,13 @@ public final class ReachShaper { private static final long SALT_COARSE_START = 0x434f4152534553L; private static final long SALT_COARSE_END = 0x434f415253454eL; private static final long SALT_SHAPE = 0x5348415045L; - private static final long SALT_DELTA = 0x44454c5441L; - private static final float DELTA_CLEARANCE = 2f; - private static final double DELTA_BEND = 0.5D; - private static final double DELTA_JITTER = 0.25D; private final HydrologySettings settings; + private final DeltaPlanner deltas; public ReachShaper(HydrologySettings settings) { this.settings = Objects.requireNonNull(settings, "settings"); + this.deltas = new DeltaPlanner(settings); } public ShapedNetwork shape(WindowKey window, DemGrid filled, int[] receivers, float[] accumulation, BitSet channels, List ponds, @@ -99,270 +94,10 @@ public final class ReachShaper { linked.add(outletDrafts[pond] < 0 ? settled : settled.withOutletReachId(drafts.get(outletDrafts[pond]).id)); } List reaches = reaches(drafts); - reaches.addAll(deltas(drafts, reaches, linked, fine, seed)); + reaches.addAll(deltas.plan(drafts, reaches, linked, fine, seed)); return new ShapedNetwork(reaches, linked); } - private List deltas(List drafts, List reaches, List ponds, HeightLattice fine, long seed) { - List mouths = new ArrayList<>(); - for (int index = 0; index < drafts.size(); index++) { - ReachDraft draft = drafts.get(index); - if (draft.tier.delta().channels() > 0 && draft.chain.end() == ReachTracer.End.OCEAN) { - mouths.add(index); - } - } - if (mouths.isEmpty()) { - return List.of(); - } - mouths.sort(Comparator.comparingLong((Integer index) -> drafts.get(index).id).thenComparingDouble(index -> drafts.get(index).xs[0]) - .thenComparingDouble(index -> drafts.get(index).zs[0])); - List accepted = new ArrayList<>(); - for (int index : mouths) { - ReachDraft mouth = drafts.get(index); - Reach parent = reaches.get(index); - List earlier = List.copyOf(accepted); - for (Reach channel : distributaries(mouth, mouth.tier, fine, seed)) { - if (!crosses(channel, parent, reaches, earlier, ponds)) { - accepted.add(channel); - } - } - } - return accepted; - } - - private List distributaries(ReachDraft mouth, HydrologyTier tier, HeightLattice fine, long seed) { - int last = mouth.xs.length - 1; - int start = estuaryStart(mouth.style); - int channels = tier.delta().channels(); - float halfWidth = Math.min(tier.maxHalfWidth(), mouth.halfWidth[start] / (float) Math.sqrt(channels)); - float[] distance = distances(mouth.xs, mouth.zs); - float lip = start == 0 ? 0f : 0.5f * (distance[start] - distance[start - 1]); - float branch = distance[start] + halfWidth + DELTA_CLEARANCE + 1f + lip; - double axisX = mouth.xs[last] - mouth.xs[start]; - double axisZ = mouth.zs[last] - mouth.zs[start]; - if (start == last || branch >= distance[last] || Math.hypot(axisX, axisZ) < 1e-3D) { - return List.of(); - } - float[] at = pointAt(mouth.xs, mouth.zs, distance, start, branch); - double heading = Math.atan2(axisZ, axisX); - long choices = CellHash.mix(seed, SALT_DELTA, (int) Math.floor(mouth.xs[last]), (int) Math.floor(mouth.zs[last])); - int firstSide = CellHash.unit(CellHash.mix(choices, SALT_DELTA, -1, 0)) < 0.5D ? 1 : -1; - List made = new ArrayList<>(channels); - for (int channel = 0; channel < channels; channel++) { - long channelChoices = CellHash.mix(choices, SALT_DELTA, channel, 0); - Fan fan = new Fan(CellHash.mix(mouth.id, SALT_DELTA, channel, channels), at[0], at[1], heading, - angle(tier.delta(), channelChoices, channel, firstSide), DELTA_BEND * CellHash.unit(CellHash.mix(channelChoices, SALT_DELTA, 2, 0)), - halfWidth); - Reach reach = distributary(mouth, tier, fine, fan); - if (reach != null) { - made.add(reach); - } - } - return made; - } - - private static int estuaryStart(ReachStyle[] style) { - int start = style.length - 1; - if (style[start] != ReachStyle.MOUTH) { - return start; - } - while (start > 0 && style[start - 1] == ReachStyle.MOUTH) { - start--; - } - return start; - } - - private static float[] pointAt(float[] xs, float[] zs, float[] distance, int from, float along) { - int segment = from; - while (distance[segment + 1] < along) { - segment++; - } - float share = (along - distance[segment]) / Math.max(1e-6f, distance[segment + 1] - distance[segment]); - return new float[]{xs[segment] + share * (xs[segment + 1] - xs[segment]), zs[segment] + share * (zs[segment + 1] - zs[segment])}; - } - - private static double angle(HydrologyTier.Delta delta, long channelChoices, int channel, int firstSide) { - int side = channel % 2 == 0 ? firstSide : -firstSide; - int onSide = side == firstSide ? (delta.channels() + 1) / 2 : delta.channels() / 2; - double turn = CellHash.unit(CellHash.mix(channelChoices, SALT_DELTA, 1, 0)); - return side * Math.toRadians(delta.spreadDegrees()) * (channel / 2 + 1 - DELTA_JITTER * turn) / onSide; - } - - private Reach distributary(ReachDraft mouth, HydrologyTier tier, HeightLattice fine, Fan fan) { - int length = tier.delta().length(); - int stations = (length + DELTA_STEP - 1) / DELTA_STEP + 1; - float[] xs = new float[stations]; - float[] zs = new float[stations]; - xs[0] = fan.x(); - zs[0] = fan.z(); - for (int station = 1; station < stations; station++) { - double direction = fan.heading() + fan.angle() * (1D - fan.bend() * ((station - 1) * DELTA_STEP) / length); - xs[station] = (float) (xs[station - 1] + DELTA_STEP * Math.cos(direction)); - zs[station] = (float) (zs[station - 1] + DELTA_STEP * Math.sin(direction)); - } - int step = fine.cell(); - int[] nodeX = new int[3 * stations]; - int[] nodeZ = new int[3 * stations]; - for (int station = 0; station < stations; station++) { - int next = Math.min(stations - 1, station + 1); - int previous = Math.max(0, station - 1); - double dx = xs[next] - xs[previous]; - double dz = zs[next] - zs[previous]; - double run = Math.max(1e-6D, Math.hypot(dx, dz)); - for (int offset = -1; offset <= 1; offset++) { - int node = 3 * station + offset + 1; - nodeX[node] = (int) Math.floor((xs[station] - offset * fan.halfWidth() * dz / run) / step + 0.5D); - nodeZ[node] = (int) Math.floor((zs[station] + offset * fan.halfWidth() * dx / run) / step + 0.5D); - } - } - HydrologySampleBuffer samples = new HydrologySampleBuffer(nodeX.length); - fine.fill(nodeX, nodeZ, samples); - float highest = settings.seaLevel() + settings.deformation().maxFill(); - int sea = -1; - for (int station = 0; station < stations; station++) { - if (samples.ocean()[3 * station + 1]) { - sea = station; - break; - } - for (int node = 3 * station; node < 3 * station + 3; node++) { - if (samples.height()[node] > highest) { - return null; - } - } - } - if (sea < 2) { - return null; - } - int end = Math.min(stations - 1, sea + 1); - return reach(mouth, tier, fan, Arrays.copyOf(xs, end + 1), Arrays.copyOf(zs, end + 1)); - } - - private Reach reach(ReachDraft mouth, HydrologyTier tier, Fan fan, float[] xs, float[] zs) { - int count = xs.length; - float[] distance = distances(xs, zs); - int[] grade = new int[count]; - float[] halfWidth = new float[count]; - float[] depth = new float[count]; - ReachStyle[] style = new ReachStyle[count]; - float channelDepth = tier.depth(fan.halfWidth()); - for (int point = 0; point < count; point++) { - grade[point] = settings.seaLevel(); - halfWidth[point] = fan.halfWidth(); - depth[point] = channelDepth + (HydrologyTier.MINIMUM_DEPTH - channelDepth) * distance[point] / distance[count - 1]; - style[point] = ReachStyle.MOUTH; - } - return new Reach(fan.id(), tier, 0L, xs, zs, grade, halfWidth, depth, style, mouth.catchment / tier.delta().channels()); - } - - private boolean crosses(Reach channel, Reach parent, List reaches, List earlier, List ponds) { - float reach = channel.halfWidth()[0] + DELTA_CLEARANCE; - int minX = (int) Math.floor(minimum(channel.xs()) - reach); - int minZ = (int) Math.floor(minimum(channel.zs()) - reach); - int maxX = (int) Math.ceil(maximum(channel.xs()) + reach); - int maxZ = (int) Math.ceil(maximum(channel.zs()) + reach); - List others = new ArrayList<>(); - for (Reach other : reaches) { - if (other != parent && near(other, minX, minZ, maxX, maxZ)) { - others.add(other); - } - } - for (Reach other : earlier) { - if (near(other, minX, minZ, maxX, maxZ)) { - others.add(other); - } - } - List nearby = new ArrayList<>(); - for (Pond pond : ponds) { - if (near(pond.mask(), minX, minZ, maxX, maxZ)) { - nearby.add(pond); - } - } - boolean parentNear = near(parent, minX, minZ, maxX, maxZ); - for (int z = minZ; z <= maxZ; z++) { - for (int x = minX; x <= maxX; x++) { - if (distanceTo(channel, x, z) > reach) { - continue; - } - for (Pond pond : nearby) { - if (pond.mask().contains(x, z)) { - return true; - } - } - for (Reach other : others) { - if (waterGrade(other, x, z) != Integer.MIN_VALUE) { - return true; - } - } - if (parentNear && waterGrade(parent, x, z) > settings.seaLevel()) { - return true; - } - } - } - return false; - } - - private static int waterGrade(Reach reach, double x, double z) { - double nearest = Double.POSITIVE_INFINITY; - int grade = Integer.MIN_VALUE; - for (int segment = 0; segment + 1 < reach.xs().length; segment++) { - 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; - double t = length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D); - double distance = Math.hypot(x - (ax + t * dx), z - (az + t * dz)); - double width = reach.halfWidth()[segment] + t * (reach.halfWidth()[segment + 1] - reach.halfWidth()[segment]); - if (distance <= width && distance < nearest) { - nearest = distance; - grade = reach.grade()[t < 0.5D ? segment : segment + 1]; - } - } - return grade; - } - - private static double distanceTo(Reach reach, double x, double z) { - double nearest = Double.POSITIVE_INFINITY; - for (int segment = 0; segment + 1 < reach.xs().length; segment++) { - 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; - double t = length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D); - nearest = Math.min(nearest, Math.hypot(x - (ax + t * dx), z - (az + t * dz))); - } - return nearest; - } - - private static boolean near(Reach reach, int minX, int minZ, int maxX, int maxZ) { - float widest = maximum(reach.halfWidth()); - return minimum(reach.xs()) - widest <= maxX && maximum(reach.xs()) + widest >= minX - && minimum(reach.zs()) - widest <= maxZ && maximum(reach.zs()) + widest >= minZ; - } - - private static boolean near(PondMask mask, int minX, int minZ, int maxX, int maxZ) { - int half = mask.step() >> 1; - return mask.originX() - half <= maxX && mask.originX() + (mask.width() - 1) * mask.step() + half >= minX - && mask.originZ() - half <= maxZ && mask.originZ() + (mask.depth() - 1) * mask.step() + half >= minZ; - } - - private static float minimum(float[] values) { - float minimum = Float.POSITIVE_INFINITY; - for (float value : values) { - minimum = Math.min(minimum, value); - } - return minimum; - } - - private static float maximum(float[] values) { - float maximum = Float.NEGATIVE_INFINITY; - for (float value : values) { - maximum = Math.max(maximum, value); - } - return maximum; - } - public List coarsePaths(DemGrid filled, int[] receivers, float[] accumulation, BitSet channels, long seed) { requireNetwork(filled, receivers, accumulation, channels); List chains = ReachTracer.trace(filled, receivers, channels, null, new BitSet(), settings.seaLevel()); @@ -899,7 +634,7 @@ public final class ReachShaper { return i < 0 || j < 0 || i >= grid.width() || j >= grid.depth() ? -1 : j * grid.width() + i; } - private static float[] distances(float[] xs, float[] zs) { + static float[] distances(float[] xs, float[] zs) { float[] distance = new float[xs.length]; for (int point = 1; point < xs.length; point++) { float dx = xs[point] - xs[point - 1]; @@ -927,7 +662,4 @@ public final class ReachShaper { return new Opening(new float[count], 0f); } } - - private record Fan(long id, float x, float z, double heading, double angle, double bend, float halfWidth) { - } } 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 19310a4a7..a3844710c 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 @@ -20,7 +20,9 @@ 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 art.arcane.iris.generation.hydrology.water.ShapedNetwork; import art.arcane.iris.generation.terrain.IrisMaterialPalette; +import art.arcane.iris.testsupport.DeltaCoast; import org.junit.Test; import java.util.ArrayList; @@ -33,6 +35,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.DoubleBinaryOperator; import java.util.function.IntBinaryOperator; import static org.junit.Assert.assertEquals; @@ -1152,6 +1155,101 @@ public class ChunkWaterRasterTest { assertTrue("mouth columns " + mouths + ", channel columns " + channels, mouths > 20 && channels > 20); } + @Test + public void deltaChannelsRenderAsSeaLevelMouthWaterWithoutAGapAtTheBranch() { + assertDeltaRendersAtSeaLevel(DeltaCoast::ground, false); + assertDeltaRendersAtSeaLevel(DeltaCoast::cliffBacked, true); + } + + private static void assertDeltaRendersAtSeaLevel(DoubleBinaryOperator ground, boolean raisedAboveTheEstuary) { + HydrologySettings settings = DeltaCoast.settings(2); + ShapedNetwork shaped = DeltaCoast.shape(settings, 0, 0, List.of(), DeltaCoast.NO_NEIGHBOUR, DeltaCoast.SEED, ground); + Reach river = DeltaCoast.river(shaped, DeltaCoast.ROW); + List fan = DeltaCoast.deltaChannels(shaped); + assertEquals("two delta channels", 2, fan.size()); + IntBinaryOperator natural = (x, z) -> (int) Math.round(ground.applyAsDouble(x, z)); + Scene plain = new Scene(settings, natural).reach(river); + Scene delta = new Scene(settings, natural).reach(river); + for (Reach channel : fan) { + delta.reach(channel); + } + int estuary = river.style().length - 1; + while (estuary > 0 && river.style()[estuary - 1] == ReachStyle.MOUTH) { + estuary--; + } + int minX = (int) Math.floor(river.xs()[estuary]) - 24; + int maxX = DeltaCoast.SEA_X + 8; + int minZ = DeltaCoast.ROW - 40; + int maxZ = DeltaCoast.ROW + 40; + int added = 0; + int raised = 0; + for (int z = minZ; z <= maxZ; z++) { + for (int x = minX; x <= maxX; x++) { + if (inDelta(fan, x, z)) { + assertEquals("delta water at " + x + "," + z, WaterRole.MOUTH, delta.role(x, z)); + assertEquals("delta head at " + x + "," + z, SEA_LEVEL, delta.head(x, z)); + assertEquals("delta fluid at " + x + "," + z, HydrologyCaveCell.SEA_FLUID, delta.fluid(x, z)); + added += plain.water(x, z) ? 0 : 1; + } + if (plain.water(x, z)) { + assertTrue("the river stays wet at " + x + "," + z, delta.water(x, z)); + if (plain.head(x, z) > SEA_LEVEL) { + assertEquals("the river above its estuary keeps its head at " + x + "," + z, plain.head(x, z), delta.head(x, z)); + raised += Math.hypot(x - fan.get(0).xs()[0], z - fan.get(0).zs()[0]) <= 2D * fan.get(0).halfWidth()[0] + 8D ? 1 : 0; + } + } + } + } + assertTrue("delta water beyond the estuary " + added, added > 100); + assertTrue("river water above the sea next to the branch " + raised, !raisedAboveTheEstuary || raised > 0); + assertEquals("delta leaks", 0, delta.leaks(minX, minZ, maxX, maxZ)); + Set reached = wetFrom(delta, Math.round(river.xs()[estuary]) - 16, Math.round(river.zs()[estuary]), minX, minZ, maxX, maxZ); + for (Reach channel : fan) { + for (int point = 0; point < channel.xs().length; point++) { + int x = Math.round(channel.xs()[point]); + int z = Math.round(channel.zs()[point]); + assertTrue(channel + " point " + point + " joins the river's water", reached.contains(pack(x, z))); + } + } + } + + private static boolean inDelta(List fan, int x, int z) { + for (Reach channel : fan) { + for (int segment = 0; segment + 1 < channel.xs().length; segment++) { + double ax = channel.xs()[segment]; + double az = channel.zs()[segment]; + double dx = channel.xs()[segment + 1] - ax; + double dz = channel.zs()[segment + 1] - az; + double length = dx * dx + dz * dz; + double t = length == 0D ? 0D : Math.clamp(((x - ax) * dx + (z - az) * dz) / length, 0D, 1D); + if (Math.hypot(x - (ax + t * dx), z - (az + t * dz)) <= channel.halfWidth()[segment]) { + return true; + } + } + } + return false; + } + + private static Set wetFrom(Scene scene, int startX, int startZ, int minX, int minZ, int maxX, int maxZ) { + Set reached = new HashSet<>(); + List frontier = new ArrayList<>(); + assertTrue("the river is wet at " + startX + "," + startZ, scene.water(startX, startZ)); + reached.add(pack(startX, startZ)); + frontier.add(new int[]{startX, startZ}); + int[][] sides = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; + for (int index = 0; index < frontier.size(); index++) { + int[] column = frontier.get(index); + for (int[] side : sides) { + int x = column[0] + side[0]; + int z = column[1] + side[1]; + if (x >= minX && x <= maxX && z >= minZ && z <= maxZ && scene.water(x, z) && reached.add(pack(x, z))) { + frontier.add(new int[]{x, z}); + } + } + } + return reached; + } + @Test public void lavaMouthMeetsTheSeaThroughTheSeam() { HydrologySettings settings = settings(lavaAndCrust()); 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 f1669e4c9..728fb1c7b 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 @@ -16,6 +16,7 @@ import art.arcane.iris.generation.hydrology.grid.PriorityFlood; import art.arcane.iris.generation.hydrology.plan.HydrologySettings; import art.arcane.iris.generation.hydrology.plan.WindowKey; import art.arcane.iris.generation.hydrology.raster.PondChannelSettlement; +import art.arcane.iris.testsupport.DeltaCoast; import art.arcane.volmlib.util.collection.KList; import org.junit.Test; @@ -50,11 +51,6 @@ public class ReachShaperTest { private static final int WINDOW_MIN_X = 140; private static final int WINDOW_MAX_X = 250; private static final int OUTLET_POND_LEVEL = SEA_LEVEL + 3; - private static final int DELTA_ROW = 128; - private static final int DELTA_SEA_X = 240; - private static final double DELTA_COAST_X = 236D; - private static final double DELTA_RISE = 0.1D; - private static final int DELTA_HILL_Z = DELTA_ROW + 12; private static final byte ALLOW_ALL = HydrologySampleBuffer.ALLOW_STREAMS | HydrologySampleBuffer.ALLOW_PONDS; @Test @@ -1036,9 +1032,9 @@ public class ReachShaperTest { @Test public void majorMouthSplitsIntoSeededDistributaries() { for (int channels = 2; channels <= 4; channels++) { - HydrologySettings settings = deltaSettings(channels); - ShapedNetwork shaped = deltaCoast(settings, 0, 0, List.of(), Integer.MIN_VALUE, SEED); - Reach parent = deltaParent(shaped, DELTA_ROW); + HydrologySettings settings = DeltaCoast.settings(channels); + ShapedNetwork shaped = cleanDelta(settings, SEED); + Reach parent = DeltaCoast.river(shaped, DeltaCoast.ROW); List fan = distributaries(shaped, parent); assertEquals(channels + " delta channels", channels, fan.size()); assertEquals("only the river and its delta", channels + 1, shaped.reaches().size()); @@ -1054,51 +1050,67 @@ public class ReachShaperTest { assertEquals(channel + " half-width at " + point, halfWidth, channel.halfWidth()[point], 1e-6f); } assertTrue(channel + " branches below the estuary start", channel.xs()[0] > parent.xs()[estuary]); - assertTrue(channel + " ends where the sea begins", deltaGround(fineNode(channel.xs()[last]), fineNode(channel.zs()[last])) <= SEA_LEVEL); - assertTrue(channel + " crosses land first", deltaGround(fineNode(channel.xs()[1]), fineNode(channel.zs()[1])) > SEA_LEVEL); + assertTrue(channel + " ends where the sea begins", + DeltaCoast.ground(fineNode(channel.xs()[last]), fineNode(channel.zs()[last])) <= SEA_LEVEL); + assertTrue(channel + " crosses land first", DeltaCoast.ground(fineNode(channel.xs()[1]), fineNode(channel.zs()[1])) > SEA_LEVEL); double angle = Math.toDegrees(Math.atan2(Math.abs(channel.zs()[last] - channel.zs()[0]), channel.xs()[last] - channel.xs()[0])); assertTrue(channel + " fans out within the spread: " + angle, angle > 3D && angle < 37D); assertEquals(channel + " depth meets the seabed", HydrologyTier.MINIMUM_DEPTH, channel.depth()[last], 1e-6f); } assertDeltaClear(shaped, List.of()); - assertEquals("deterministic", byId(shaped.reaches()), byId(deltaCoast(settings, 0, 0, List.of(), Integer.MIN_VALUE, SEED).reaches())); + assertEquals("deterministic", byId(shaped.reaches()), byId(cleanDelta(settings, SEED).reaches())); assertEquals("independent of the grid origin", byId(shaped.reaches()), - byId(deltaCoast(settings, -64, -40, List.of(), Integer.MIN_VALUE, SEED).reaches())); - List reseeded = distributaries(deltaCoast(settings, 0, 0, List.of(), Integer.MIN_VALUE, SEED + 1L), null); + byId(DeltaCoast.shape(settings, -64, -40, List.of(), DeltaCoast.NO_NEIGHBOUR, SEED).reaches())); + List reseeded = DeltaCoast.deltaChannels(cleanDelta(settings, SEED + 1L)); assertEquals(channels, reseeded.size()); assertNotEquals("the seed chooses the channels", ends(fan), ends(reseeded)); } + } - HydrologySettings settings = deltaSettings(2); - List clean = distributaries(deltaCoast(settings, 0, 0, List.of(), Integer.MIN_VALUE, SEED), null); - Reach north = clean.get(0).zs()[clean.get(0).xs().length - 1] > DELTA_ROW ? clean.get(0) : clean.get(1); - Reach south = north == clean.get(0) ? clean.get(1) : clean.get(0); - int far = north.xs().length - 3; - Pond pond = new Pond(91L, PondKind.NATURAL, SEA_LEVEL + 1, SEA_LEVEL - 1, 0, 0, SEA_LEVEL + 1.5f, - disk(Math.round(north.xs()[far]), Math.round(north.zs()[far]), 2), 0L, 0, "water", Math.round(north.xs()[far]), - Math.round(north.zs()[far])); - assertTrue("the pond lies on the northern channel", coversMask(north, pond.mask())); - ShapedNetwork ponded = deltaCoast(settings, 0, 0, List.of(pond), Integer.MIN_VALUE, SEED); - assertFalse("the pond lies clear of the river", coversMask(deltaParent(ponded, DELTA_ROW), pond.mask())); - assertEquals("a channel that would cross a pond is left out", List.of(south), distributaries(ponded, null)); + @Test + public void deltaChannelAcrossAPondIsLeftOut() { + HydrologySettings settings = DeltaCoast.settings(2); + CleanFan fan = cleanFan(settings); + int far = fan.north().xs().length - 3; + int pondX = Math.round(fan.north().xs()[far]); + int pondZ = Math.round(fan.north().zs()[far]); + Pond pond = new Pond(91L, PondKind.NATURAL, SEA_LEVEL + 1, SEA_LEVEL - 1, 0, 0, SEA_LEVEL + 1.5f, disk(pondX, pondZ, 2), 0L, 0, "water", + pondX, pondZ); + assertTrue("the pond lies on the northern channel", coversMask(fan.north(), pond.mask())); + ShapedNetwork ponded = DeltaCoast.shape(settings, 0, 0, List.of(pond), DeltaCoast.NO_NEIGHBOUR, SEED); + assertFalse("the pond lies clear of the river", coversMask(DeltaCoast.river(ponded, DeltaCoast.ROW), pond.mask())); + assertEquals("a channel that would cross a pond is left out", List.of(fan.south()), DeltaCoast.deltaChannels(ponded)); assertDeltaClear(ponded, List.of(pond)); + } + @Test + public void deltaChannelOverHighGroundIsLeftOut() { + HydrologySettings settings = DeltaCoast.settings(2); + CleanFan fan = cleanFan(settings); boolean climbs = false; - for (int point = 0; point < north.xs().length; point++) { - climbs |= north.zs()[point] > DELTA_HILL_Z + 2 && north.xs()[point] < DELTA_COAST_X - 4D; + for (int point = 0; point < fan.north().xs().length; point++) { + climbs |= fan.north().zs()[point] > DeltaCoast.HILL_Z + 2 && fan.north().xs()[point] < DeltaCoast.COAST_X - 4D; } assertTrue("the northern channel would climb the hill", climbs); - ShapedNetwork hill = deltaCoast(settings, 0, 0, List.of(), Integer.MIN_VALUE, SEED, ReachShaperTest::northHill); - assertEquals("a channel that would cut through ground above the fill limit is left out", List.of(south), distributaries(hill, null)); + ShapedNetwork hill = DeltaCoast.shape(settings, 0, 0, List.of(), DeltaCoast.NO_NEIGHBOUR, SEED, DeltaCoast::northHill); + assertEquals("a channel that would cut through ground above the fill limit is left out", List.of(fan.south()), + DeltaCoast.deltaChannels(hill)); + assertDeltaClear(hill, List.of()); + } - ShapedNetwork twin = deltaCoast(settings, 0, 0, List.of(), DELTA_ROW + 32, SEED); - Reach neighbour = deltaParent(twin, DELTA_ROW + 32); - assertTrue("the northern channel would cross the neighbouring river", crossesWater(north, neighbour)); - List kept = distributaries(twin, deltaParent(twin, DELTA_ROW)); - assertEquals("a channel that would cross another river is left out", List.of(south), kept); - for (Reach channel : distributaries(twin, neighbour)) { - assertTrue(channel + " turns away from the other river", channel.zs()[channel.xs().length - 1] > DELTA_ROW + 32); - } + @Test + public void deltaChannelsTowardAnotherRiverAreLeftOut() { + HydrologySettings settings = DeltaCoast.settings(2); + CleanFan fan = cleanFan(settings); + int neighbourRow = DeltaCoast.ROW + 32; + ShapedNetwork twin = DeltaCoast.shape(settings, 0, 0, List.of(), neighbourRow, SEED); + Reach neighbour = DeltaCoast.river(twin, neighbourRow); + assertTrue("the northern channel would cross the neighbouring river", crossesWater(fan.north(), neighbour)); + assertEquals("a channel that would cross another river is left out", List.of(fan.south()), + distributaries(twin, DeltaCoast.river(twin, DeltaCoast.ROW))); + List outward = distributaries(twin, neighbour); + assertEquals("the other river keeps its outward channel", 1, outward.size()); + assertTrue(outward.get(0) + " turns away from the first river", outward.get(0).zs()[outward.get(0).xs().length - 1] > neighbourRow); assertDeltaClear(twin, List.of()); } @@ -2028,26 +2040,22 @@ public class ReachShaperTest { return shaped.reaches().get(0); } - private static HydrologySettings deltaSettings(int channels) { - return settings(config -> { - config.getTiers().get(2).setHalfWidth(new KList<>(3D, 5D)); - config.getTiers().get(2).getDelta().setChannels(channels); - }); + private static ShapedNetwork cleanDelta(HydrologySettings settings, long seed) { + return DeltaCoast.shape(settings, 0, 0, List.of(), DeltaCoast.NO_NEIGHBOUR, seed); } - private static Reach deltaParent(ShapedNetwork shaped, int row) { - for (Reach reach : shaped.reaches()) { - if (reach.style()[0] != ReachStyle.MOUTH && Math.abs(reach.zs()[0] - row) < 1f) { - return reach; - } - } - throw new AssertionError("no river along z=" + row + " in " + shaped.reaches()); + private static CleanFan cleanFan(HydrologySettings settings) { + List clean = DeltaCoast.deltaChannels(cleanDelta(settings, SEED)); + assertEquals("two delta channels on the clean coast", 2, clean.size()); + Reach first = clean.get(0); + boolean firstNorth = first.zs()[first.xs().length - 1] > DeltaCoast.ROW; + return firstNorth ? new CleanFan(first, clean.get(1)) : new CleanFan(clean.get(1), first); } private static List distributaries(ShapedNetwork shaped, Reach parent) { List fan = new ArrayList<>(); - for (Reach reach : shaped.reaches()) { - if (reach.style()[0] == ReachStyle.MOUTH && (parent == null || branchesFrom(reach, parent))) { + for (Reach reach : DeltaCoast.deltaChannels(shaped)) { + if (branchesFrom(reach, parent)) { fan.add(reach); } } @@ -2060,7 +2068,7 @@ public class ReachShaperTest { } private static void assertDeltaClear(ShapedNetwork shaped, List ponds) { - List fan = distributaries(shaped, null); + List fan = DeltaCoast.deltaChannels(shaped); for (Reach channel : fan) { Reach parent = null; for (Reach reach : shaped.reaches()) { @@ -2154,48 +2162,6 @@ public class ReachShaperTest { return Math.floor(coordinate / FINE + 0.5D) * FINE; } - private static ShapedNetwork deltaCoast(HydrologySettings settings, int originX, int originZ, List ponds, int neighbourZ, long seed) { - return deltaCoast(settings, originX, originZ, ponds, neighbourZ, seed, ReachShaperTest::deltaGround); - } - - private static ShapedNetwork deltaCoast(HydrologySettings settings, int originX, int originZ, List ponds, int neighbourZ, long seed, - DoubleBinaryOperator ground) { - int width = (DELTA_SEA_X + 32 - originX) / CELL + 1; - int depth = (DELTA_ROW + 96 - originZ) / CELL + 1; - DemGrid surface = new DemGrid(originX, originZ, CELL, width, depth); - for (int j = 0; j < depth; j++) { - for (int i = 0; i < width; i++) { - surface.set(i, j, originX + i * CELL >= DELTA_SEA_X ? 30f : 60f); - } - } - int[] receivers = new int[width * depth]; - Arrays.fill(receivers, -1); - float[] accumulation = new float[width * depth]; - BitSet channels = new BitSet(width * depth); - deltaRow(surface, receivers, accumulation, channels, 0, DELTA_ROW, 20000f); - if (neighbourZ != Integer.MIN_VALUE) { - deltaRow(surface, receivers, accumulation, channels, 0, neighbourZ, 20000f); - } - HeightLattice fine = new HeightLattice(new Terrain(ground), FINE, false, 1 << 16); - return new ReachShaper(settings).shape(WINDOW, surface, receivers, accumulation, channels, ponds, fine, seed); - } - - private static void deltaRow(DemGrid grid, int[] receivers, float[] accumulation, BitSet channels, int fromX, int z, float perCell) { - int j = (z - grid.originZ()) / CELL; - for (int x = fromX; x < DELTA_SEA_X; x += CELL) { - int i = (x - grid.originX()) / CELL; - flow(grid, receivers, accumulation, channels, i, j, i + 1, j, perCell * ((x - fromX) / CELL + 1)); - } - } - - private static double deltaGround(double x, double z) { - return x < DELTA_COAST_X ? SEA_LEVEL + 0.5D + DELTA_RISE * (DELTA_COAST_X - x) : SEA_LEVEL - 4D; - } - - private static double northHill(double x, double z) { - return z > DELTA_HILL_Z && x < DELTA_COAST_X ? SEA_LEVEL + 10D : deltaGround(x, z); - } - private static int trailingMouth(Reach reach) { int start = reach.style().length; while (start > 0 && reach.style()[start - 1] == ReachStyle.MOUTH) { @@ -2333,6 +2299,9 @@ public class ReachShaperTest { return new Network(surface, receivers, accumulation, channels, ponds, fine); } + private record CleanFan(Reach north, Reach south) { + } + private record Network(DemGrid grid, int[] receivers, float[] accumulation, BitSet channels, List ponds, HeightLattice fine) { } diff --git a/core/src/test/java/art/arcane/iris/testsupport/DeltaCoast.java b/core/src/test/java/art/arcane/iris/testsupport/DeltaCoast.java new file mode 100644 index 000000000..c7b5e6d21 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/testsupport/DeltaCoast.java @@ -0,0 +1,155 @@ +package art.arcane.iris.testsupport; + +import art.arcane.iris.generation.hydrology.config.IrisHydrology; +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.HydrologyTerrainSource; +import art.arcane.iris.generation.hydrology.plan.HydrologySettings; +import art.arcane.iris.generation.hydrology.plan.WindowKey; +import art.arcane.iris.generation.hydrology.water.Pond; +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 art.arcane.volmlib.util.collection.KList; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.BitSet; +import java.util.List; +import java.util.function.DoubleBinaryOperator; + +public final class DeltaCoast { + public static final long SEED = 1337L; + public static final int SEA_LEVEL = 40; + public static final int ROW = 128; + public static final int SEA_X = 240; + public static final double COAST_X = 236D; + public static final int HILL_Z = ROW + 12; + public static final int NO_NEIGHBOUR = Integer.MIN_VALUE; + private static final double RISE = 0.1D; + private static final double BACKED_PLAIN = 30D; + private static final double BACKED_RISE = 0.5D; + private static final int MIN_Y = -64; + private static final int MAX_Y = 320; + private static final int CELL = 8; + private static final int FINE = 4; + private static final float ROW_CATCHMENT = 20000f; + private static final WindowKey WINDOW = new WindowKey(0, 0); + private static final byte ALLOW_ALL = HydrologySampleBuffer.ALLOW_STREAMS | HydrologySampleBuffer.ALLOW_PONDS; + + private DeltaCoast() { + } + + public static HydrologySettings settings(int channels) { + IrisHydrology config = new IrisHydrology(); + config.getTiers().get(2).setHalfWidth(new KList<>(3D, 5D)); + config.getTiers().get(2).getDelta().setChannels(channels); + return HydrologySettings.compile(config, SEED, SEA_LEVEL, MIN_Y, MAX_Y); + } + + public static ShapedNetwork shape(HydrologySettings settings, int originX, int originZ, List ponds, int neighbourZ, long seed) { + return shape(settings, originX, originZ, ponds, neighbourZ, seed, DeltaCoast::ground); + } + + public static ShapedNetwork shape(HydrologySettings settings, int originX, int originZ, List ponds, int neighbourZ, long seed, + DoubleBinaryOperator ground) { + int width = (SEA_X + 32 - originX) / CELL + 1; + int depth = (ROW + 96 - originZ) / CELL + 1; + DemGrid surface = new DemGrid(originX, originZ, CELL, width, depth); + for (int j = 0; j < depth; j++) { + for (int i = 0; i < width; i++) { + surface.set(i, j, originX + i * CELL >= SEA_X ? 30f : 60f); + } + } + int[] receivers = new int[width * depth]; + Arrays.fill(receivers, -1); + float[] accumulation = new float[width * depth]; + BitSet channels = new BitSet(width * depth); + row(surface, receivers, accumulation, channels, ROW); + if (neighbourZ != NO_NEIGHBOUR) { + row(surface, receivers, accumulation, channels, neighbourZ); + } + HeightLattice fine = new HeightLattice(new Terrain(ground), FINE, false, 1 << 16); + return new ReachShaper(settings).shape(WINDOW, surface, receivers, accumulation, channels, ponds, fine, seed); + } + + public static double ground(double x, double z) { + return x < COAST_X ? SEA_LEVEL + 0.5D + RISE * (COAST_X - x) : SEA_LEVEL - 4D; + } + + public static double cliffBacked(double x, double z) { + return x < COAST_X - BACKED_PLAIN ? ground(COAST_X - BACKED_PLAIN, z) + BACKED_RISE * (COAST_X - BACKED_PLAIN - x) : ground(x, z); + } + + public static double northHill(double x, double z) { + return z > HILL_Z && x < COAST_X ? SEA_LEVEL + 10D : ground(x, z); + } + + public static Reach river(ShapedNetwork shaped, int row) { + for (Reach reach : shaped.reaches()) { + if (reach.style()[0] != ReachStyle.MOUTH && Math.abs(reach.zs()[0] - row) < 1f) { + return reach; + } + } + throw new AssertionError("no river along z=" + row + " in " + shaped.reaches()); + } + + public static List deltaChannels(ShapedNetwork shaped) { + List fan = new ArrayList<>(); + for (Reach reach : shaped.reaches()) { + if (reach.style()[0] == ReachStyle.MOUTH) { + fan.add(reach); + } + } + return fan; + } + + private static void row(DemGrid grid, int[] receivers, float[] accumulation, BitSet channels, int z) { + int j = (z - grid.originZ()) / CELL; + for (int x = 0; x < SEA_X; x += CELL) { + int i = (x - grid.originX()) / CELL; + int cell = grid.index(i, j); + receivers[cell] = grid.index(i + 1, j); + accumulation[cell] = ROW_CATCHMENT * (x / CELL + 1); + channels.set(cell); + } + } + + private static final class Terrain implements HydrologyTerrainSource { + private final DoubleBinaryOperator height; + + private Terrain(DoubleBinaryOperator height) { + this.height = height; + } + + @Override + public void sample(int[] xs, int[] zs, HydrologySampleBuffer out, boolean baseOnly) { + for (int index = 0; index < xs.length; index++) { + float value = (float) height.applyAsDouble(xs[index], zs[index]); + out.height()[index] = value; + out.ocean()[index] = value <= SEA_LEVEL; + out.rainfall()[index] = 1f; + out.pondDensity()[index] = 1f; + out.permission()[index] = ALLOW_ALL; + out.fluidProfile()[index] = null; + } + } + + @Override + public int seaLevel() { + return SEA_LEVEL; + } + + @Override + public int minY() { + return MIN_Y; + } + + @Override + public int maxY() { + return MAX_Y; + } + } +}