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
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-02 00:19:04 -05:00
1 parent d3b364fa15
commit d61b5c3041
7 files changed
+605 -369

No files matched your search

@@ -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());
@@ -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<Reach> 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();
}
}
@@ -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<Reach> plan(List<ReachDraft> drafts, List<Reach> reaches, List<Pond> ponds, HeightLattice fine, long seed) {
List<Integer> 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<Reach> accepted = new ArrayList<>();
for (int index : mouths) {
Reach parent = reaches.get(index);
List<Reach> 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<Reach> 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<Reach> 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<Reach> reaches, List<Reach> earlier, List<Pond> 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<Reach> 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<Pond> 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) {
}
}
@@ -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<Pond> ponds,
@@ -99,270 +94,10 @@ public final class ReachShaper {
linked.add(outletDrafts[pond] < 0 ? settled : settled.withOutletReachId(drafts.get(outletDrafts[pond]).id));
}
List<Reach> 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<Reach> deltas(List<ReachDraft> drafts, List<Reach> reaches, List<Pond> ponds, HeightLattice fine, long seed) {
List<Integer> 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<Reach> accepted = new ArrayList<>();
for (int index : mouths) {
ReachDraft mouth = drafts.get(index);
Reach parent = reaches.get(index);
List<Reach> 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<Reach> 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<Reach> 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<Reach> reaches, List<Reach> earlier, List<Pond> 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<Reach> 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<Pond> 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<Reach> coarsePaths(DemGrid filled, int[] receivers, float[] accumulation, BitSet channels, long seed) {
requireNetwork(filled, receivers, accumulation, channels);
List<ReachTracer.Chain> 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) {
}
}