Task 19 WIP

- Add ReachGeometry and the shared curved brink head rule
- Use it in RasterComposer and PondChannelSettlement
- Add T8, T9 and multi-seed brink tests
- Unfinished: see task-19-report.md
This commit is contained in:
Brian Fopiano committed 2026-11-02 21:11:48 -05:00
1 parent 6c0c42cd8c
commit 6458b2331c
12 files changed
+622 -37

No files matched your search

@@ -118,7 +118,7 @@ public final class WindowPlanner {
List<Pond> water = new ArrayList<>(ponds.ponds(raw, drainage.filled(), basins, lakes, fine, cups));
water.addAll(lakes);
ShapedNetwork network = PondChannelSettlement.settle(key, shaper.shape(key, drainage.surface(), drainage.receivers(),
drainage.accumulation(), drainage.channels(), samples.ocean(), water, fine, settings.seed()), settings.seaLevel());
drainage.accumulation(), drainage.channels(), samples.ocean(), water, fine, settings.seed()), settings.seed(), settings.seaLevel());
long passes = settlementPasses(water);
for (long pass = 1; lowered(water, network.ponds()); pass++) {
if (pass >= passes) {
@@ -126,7 +126,7 @@ public final class WindowPlanner {
}
water = replanned(settled(raw, cups, water, network.ponds()));
network = PondChannelSettlement.settle(key, shaper.shape(key, drainage.surface(), drainage.receivers(),
drainage.accumulation(), drainage.channels(), samples.ocean(), water, fine, settings.seed()), settings.seaLevel());
drainage.accumulation(), drainage.channels(), samples.ocean(), water, fine, settings.seed()), settings.seed(), settings.seaLevel());
}
List<Reach> reaches = network.reaches();
List<Pond> windowPonds = windowPonds(network.ponds());
@@ -34,12 +34,14 @@ final class ChunkFeatures {
private final int[] inletBoxes;
private final int[] lakeBoxes;
private final ReachSegments segments = new ReachSegments(SEGMENT_CAPACITY);
private final ReachGeometry.Lookup brinks;
private final MaskEdges pondShores = new MaskEdges(MaskEdges.Kind.WATER);
private final MaskEdges pondBanks = new MaskEdges(MaskEdges.Kind.BANK);
private final MaskEdges lakeReach = new MaskEdges(MaskEdges.Kind.GROWN_WATER);
private boolean inert;
private ChunkFeatures(List<Reach> reaches, List<Pond> ponds, List<Pond> lakes, List<SeaInlet> inlets, int[] inletBoxes) {
private ChunkFeatures(HydrologySettings settings, List<Reach> reaches, List<Pond> ponds, List<Pond> lakes, List<SeaInlet> inlets, int[] inletBoxes) {
this.brinks = new ReachGeometry.Lookup(settings.seed(), settings.seaLevel());
this.reaches = reaches;
this.ponds = ponds;
this.lakes = lakes;
@@ -73,7 +75,7 @@ final class ChunkFeatures {
}
}
List<Pond> lakes = lakes(chunkX, chunkZ, halo, lakeRegionPlans, lakeRegions);
ChunkFeatures features = new ChunkFeatures(reaches, ponds, lakes, inlets, inletBoxes);
ChunkFeatures features = new ChunkFeatures(settings, reaches, ponds, lakes, inlets, inletBoxes);
features.index(settings, chunkX, chunkZ);
return features;
}
@@ -96,6 +98,10 @@ final class ChunkFeatures {
return null;
}
ReachGeometry.Lookup brinks() {
return brinks;
}
ReachSegments segments() {
return segments;
}
@@ -17,12 +17,12 @@ public final class PondChannelSettlement {
private PondChannelSettlement() {
}
public static ShapedNetwork settle(WindowKey window, ShapedNetwork network, int seaLevel) {
public static ShapedNetwork settle(WindowKey window, ShapedNetwork network, long seed, int seaLevel) {
int[] initial = new int[network.ponds().size()];
for (int index = 0; index < initial.length; index++) {
initial[index] = network.ponds().get(index).level();
}
int[] levels = levels(window, network.reaches(), network.ponds(), initial, seaLevel);
int[] levels = levels(window, network.reaches(), network.ponds(), initial, seed, seaLevel);
List<Pond> water = new ArrayList<>(initial.length);
for (int index = 0; index < initial.length; index++) {
Pond pond = network.ponds().get(index);
@@ -32,7 +32,7 @@ public final class PondChannelSettlement {
return new ShapedNetwork(network.reaches(), water);
}
static int[] levels(WindowKey window, List<Reach> reaches, List<Pond> ponds, int[] initial, int seaLevel) {
static int[] levels(WindowKey window, List<Reach> reaches, List<Pond> ponds, int[] initial, long seed, int seaLevel) {
int[] levels = initial.clone();
if (reaches.isEmpty() || ponds.isEmpty()) {
return levels;
@@ -45,6 +45,7 @@ public final class PondChannelSettlement {
return levels;
}
ReachIndex index = ReachIndex.build(window, reaches, List.of());
ReachGeometry.Lookup brinks = new ReachGeometry.Lookup(seed, seaLevel);
List<Reach> nearby = new ArrayList<>();
List<Pond> unused = new ArrayList<>();
for (int pond = 0; pond < ponds.size(); pond++) {
@@ -60,18 +61,18 @@ public final class PondChannelSettlement {
nearby.clear();
index.candidates(chunkX, chunkZ, halo, nearby, unused);
if (!nearby.isEmpty()) {
levels[pond] = lower(nearby, water.mask(), bounds, levels[pond], seaLevel);
levels[pond] = lower(nearby, water.mask(), bounds, levels[pond], brinks, seaLevel);
}
}
return levels;
}
private static int lower(List<Reach> reaches, PondMask mask, Bounds bounds, int level, int seaLevel) {
private static int lower(List<Reach> reaches, PondMask mask, Bounds bounds, int level, ReachGeometry.Lookup brinks, int seaLevel) {
ReachSegments segments = new ReachSegments(64);
for (Reach reach : reaches) {
segments.addNear(reach, bounds.minX() - 1, bounds.minZ() - 1, bounds.maxX() + 1, bounds.maxZ() + 1, 0f);
}
ReachCursor cursor = new ReachCursor();
ReachCursor cursor = new ReachCursor(brinks);
int width = bounds.maxX() - bounds.minX() + 1;
BitSet visited = new BitSet(width * (bounds.maxZ() - bounds.minZ() + 1));
for (Reach reach : reaches) {
@@ -91,12 +92,12 @@ public final class PondChannelSettlement {
continue;
}
visited.set(cell);
level = Math.min(level, touchingHead(segments, cursor, x, z, seaLevel, level));
level = Math.min(level, touchingHead(segments, cursor, brinks, x, z, seaLevel, level));
for (int[] side : SIDES) {
int nx = x + side[0];
int nz = z + side[1];
if (!mask.contains(nx, nz)) {
level = Math.min(level, touchingHead(segments, cursor, nx, nz, seaLevel, level));
level = Math.min(level, touchingHead(segments, cursor, brinks, nx, nz, seaLevel, level));
}
}
}
@@ -106,13 +107,13 @@ public final class PondChannelSettlement {
return level;
}
private static int touchingHead(ReachSegments segments, ReachCursor cursor, int x, int z, int seaLevel, int level) {
private static int touchingHead(ReachSegments segments, ReachCursor cursor, ReachGeometry.Lookup brinks, int x, int z, int seaLevel, int level) {
segments.nearest(x, z, ReachSegments.Scope.CHANNEL, cursor);
ReachCursor.Choice channel = cursor.channel;
if (channel.reach == null) {
return level;
}
int head = WaterHeads.snap(channel.reach.grade()[channel.point()], seaLevel);
int head = brinks.head(channel);
return head < WaterHeads.snap(level, seaLevel) ? head : level;
}
@@ -42,7 +42,8 @@ final class RasterComposer {
private final double[] distance = new double[CELLS];
private final long[] nearFeature = new long[CELLS];
private final HydrologyTier[] nearTier = new HydrologyTier[CELLS];
private final ReachCursor cursor = new ReachCursor();
private final ReachGeometry.Lookup brinks;
private final ReachCursor cursor;
private final int[] inletColumn = new int[2];
private final Map<Long, String> reachFluids = new HashMap<>();
private NaturalHeights naturalHeight;
@@ -51,6 +52,8 @@ final class RasterComposer {
this.settings = settings;
this.features = features;
this.channelFluids = channelFluids;
this.brinks = features.brinks();
this.cursor = new ReachCursor(brinks);
this.originX = chunkX * CHUNK - 1;
this.originZ = chunkZ * CHUNK - 1;
Arrays.fill(head, Integer.MIN_VALUE);
@@ -133,15 +136,15 @@ final class RasterComposer {
if (hit.reach == null) {
return floor;
}
int ownerHead = WaterHeads.snap(hit.grade(), settings.seaLevel());
int ownerHead = brinks.head(hit);
return Math.min(floor, deepestBed(ownerHead, Math.min(level, ownerHead), ground, variation, detail));
}
private void channel(int cell, int x, int z, int ground, double variation) {
ReachCursor.Choice hit = cursor.channel;
Reach reach = hit.reach;
int point = hit.point();
head[cell] = WaterHeads.snap(reach.grade()[point], settings.seaLevel());
int point = brinks.point(hit);
head[cell] = brinks.head(hit);
terrain[cell] = deepestBed(head[cell], head[cell], ground, variation, BedProfile.detail(settings.seed(), x, z));
role[cell] = role(reach.style()[point]);
feature[cell] = reach.id();
@@ -158,7 +161,7 @@ final class RasterComposer {
int deepest = Integer.MAX_VALUE;
for (int entry = 0; entry < waters.size(); entry++) {
ReachCursor.Choice water = waters.get(entry);
if (WaterHeads.snap(water.grade(), settings.seaLevel()) == ownerHead) {
if (brinks.head(water) == ownerHead) {
deepest = Math.min(deepest, bed(water, surface, ground, variation, detail));
}
}
@@ -179,7 +182,7 @@ final class RasterComposer {
ReachCursor.Choice bank = cursor.bank;
if (bank.reach != null) {
HydrologyTier bankTier = bank.reach.tier();
int surface = WaterHeads.snap(bank.reach.grade()[bank.point()], settings.seaLevel());
int surface = brinks.head(bank);
bankEdge = bank.distance - bank.halfWidth();
double u = bankEdge / bankTier.bankBand();
if (bankTier.bankStyle() != IrisBankStyle.CLIFF && bankTier.bankStyle() != IrisBankStyle.TERRACED) {
@@ -349,7 +352,7 @@ final class RasterComposer {
features.segments().nearest(x, z, ReachSegments.Scope.CHANNEL, cursor);
ReachCursor.Choice hit = cursor.channel;
if (hit.reach != null) {
sample.set(WaterHeads.snap(hit.grade(), settings.seaLevel()), hit.reach.id(), role(hit.reach.style()[hit.point()]));
sample.set(brinks.head(hit), hit.reach.id(), role(hit.reach.style()[brinks.point(hit)]));
}
}
@@ -10,6 +10,19 @@ final class ReachCursor {
final Choice nearest = new Choice();
final Choice edge = new Choice();
final Waters waters = new Waters();
private final ReachGeometry.Lookup brinks;
ReachCursor() {
this(null);
}
ReachCursor(ReachGeometry.Lookup brinks) {
this.brinks = brinks;
}
void resolveChannel() {
waters.resolve(channel, brinks);
}
void reset() {
channel.reset();
@@ -118,23 +131,23 @@ final class ReachCursor {
entries[size++].set(reach, segment, index, t, distance);
}
void resolve(Choice channel) {
private void resolve(Choice channel, ReachGeometry.Lookup brinks) {
channel.reset();
if (size == 0) {
return;
}
Choice owner = entries[0];
for (int entry = 1; entry < size; entry++) {
if (owns(entries[entry], owner)) {
if (owns(entries[entry], owner, brinks)) {
owner = entries[entry];
}
}
channel.set(owner.reach, owner.segment, owner.index, owner.t, owner.distance);
}
private static boolean owns(Choice candidate, Choice held) {
int candidateGrade = candidate.grade();
int heldGrade = held.grade();
private static boolean owns(Choice candidate, Choice held, ReachGeometry.Lookup brinks) {
int candidateGrade = brinks == null ? candidate.grade() : candidate.reach.grade()[brinks.point(candidate)];
int heldGrade = brinks == null ? held.grade() : held.reach.grade()[brinks.point(held)];
if (candidateGrade != heldGrade) {
return candidateGrade < heldGrade;
}
@@ -0,0 +1,187 @@
package art.arcane.iris.generation.hydrology.raster;
import art.arcane.iris.generation.hydrology.grid.CellHash;
import art.arcane.iris.generation.hydrology.water.Reach;
import art.arcane.iris.generation.hydrology.water.ReachShaper;
import art.arcane.iris.generation.hydrology.water.ReachStyle;
import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.Map;
final class ReachGeometry {
static final int BRINK_MIN_DROP = 4;
static final double BRINK_CURVE_MIN = 0.25D;
static final double BRINK_CURVE_MAX = 0.6D;
static final double BRINK_MIN_SHIFT = 1D;
static final double BRINK_MAX_SHIFT = 11D;
static final double EDGE_MARGIN = 1D;
private static final long SALT_BRINK = 0x4252494e4bL;
private static final ReachGeometry STRAIGHT = new ReachGeometry(new double[0], new int[0], new double[0], new double[0], new double[0]);
private final double[] along;
private final int[] segments;
private final double[] middles;
private final double[] shifts;
private final double[] starts;
private ReachGeometry(double[] along, int[] segments, double[] middles, double[] shifts, double[] starts) {
this.along = along;
this.segments = segments;
this.middles = middles;
this.shifts = shifts;
this.starts = starts;
}
static ReachGeometry of(Reach reach, long seed, int seaLevel) {
int[] grade = reach.grade();
int[] steps = new int[grade.length - 1];
int count = 0;
boolean falls = false;
for (int segment = 0; segment + 1 < grade.length; segment++) {
if (WaterHeads.snap(grade[segment], seaLevel) != WaterHeads.snap(grade[segment + 1], seaLevel)) {
steps[count++] = segment;
falls |= grade[segment] - grade[segment + 1] >= BRINK_MIN_DROP;
}
}
if (!falls) {
return STRAIGHT;
}
double[] along = along(reach);
double end = along[along.length - 1];
double held = protectedEnd(reach, along);
double mouth = firstMouth(reach, along);
int[] segments = new int[count];
double[] middles = new double[count];
double[] shifts = new double[count];
double[] starts = new double[count];
int curves = 0;
int start = WaterHeads.snap(grade[0], seaLevel);
boolean outlet = reach.style()[0] == ReachStyle.POND_OUTLET;
boolean feeder = reach.downstreamId() != 0L;
int landing = WaterHeads.snap(grade[grade.length - 1], seaLevel);
for (int step = 0; step < count; step++) {
int segment = steps[step];
double middle = middle(along, segment);
int upper = WaterHeads.snap(grade[segment], seaLevel);
int lower = WaterHeads.snap(grade[segment + 1], seaLevel);
if (grade[segment] - grade[segment + 1] < BRINK_MIN_DROP || middle <= held || outlet && upper == start || feeder && lower == landing
|| reach.style()[segment] == ReachStyle.MOUTH || reach.style()[segment + 1] == ReachStyle.MOUTH) {
continue;
}
double width = (reach.halfWidth()[segment] + reach.halfWidth()[segment + 1]) / 2D;
if (width - EDGE_MARGIN < BRINK_MIN_SHIFT) {
continue;
}
long hash = CellHash.mix(seed, SALT_BRINK, Math.round((reach.xs()[segment] + reach.xs()[segment + 1]) / 2f),
Math.round((reach.zs()[segment] + reach.zs()[segment + 1]) / 2f));
double curve = BRINK_CURVE_MIN + (BRINK_CURVE_MAX - BRINK_CURVE_MIN) * CellHash.unit(hash);
double wanted = Math.min(BRINK_MAX_SHIFT, Math.max(BRINK_MIN_SHIFT, curve * (width - EDGE_MARGIN)));
boolean tongue = (hash & 1L) == 0L;
double room = tongue
? Math.min(step + 1 < count ? (middle(along, steps[step + 1]) - middle) / 2D : end - middle, mouth - middle)
: Math.min(step > 0 ? (middle - middle(along, steps[step - 1])) / 2D : middle, middle - held);
double shift = Math.min(wanted, room);
if (shift > 0D) {
segments[curves] = segment;
middles[curves] = middle;
shifts[curves] = tongue ? shift : -shift;
starts[curves] = tongue ? middle : middle - shift;
curves++;
}
}
if (curves == 0) {
return STRAIGHT;
}
return new ReachGeometry(along, Arrays.copyOf(segments, curves), Arrays.copyOf(middles, curves), Arrays.copyOf(shifts, curves),
Arrays.copyOf(starts, curves));
}
int point(ReachCursor.Choice choice) {
int straight = choice.point();
if (segments.length == 0) {
return straight;
}
double inner = choice.halfWidth() - EDGE_MARGIN;
if (choice.distance >= inner) {
return straight;
}
double at = along[choice.segment] + choice.t * (along[choice.segment + 1] - along[choice.segment]);
int found = Arrays.binarySearch(starts, at);
int curve = found >= 0 ? found : -found - 2;
if (curve < 0 || at > starts[curve] + Math.abs(shifts[curve])) {
return straight;
}
double ratio = choice.distance / inner;
boolean below = at - shifts[curve] * (1D - ratio * ratio) >= middles[curve];
if (shifts[curve] > 0D) {
return below ? straight : segments[curve];
}
return below ? segments[curve] + 1 : straight;
}
private static double[] along(Reach reach) {
float[] xs = reach.xs();
float[] zs = reach.zs();
double[] along = new double[xs.length];
for (int point = 1; point < xs.length; point++) {
double dx = xs[point] - xs[point - 1];
double dz = zs[point] - zs[point - 1];
along[point] = along[point - 1] + Math.sqrt(dx * dx + dz * dz);
}
return along;
}
private static double middle(double[] along, int segment) {
return (along[segment] + along[segment + 1]) / 2D;
}
private static double protectedEnd(Reach reach, double[] along) {
double end = reach.style()[0] == ReachStyle.POND_OUTLET
? Math.max(ReachShaper.OUTLET_LENGTH, 2D * (reach.halfWidth()[0] + 1D))
: Double.NEGATIVE_INFINITY;
for (int point = 0; point < along.length; point++) {
if (reach.style()[point] == ReachStyle.POND_OUTLET) {
end = Math.max(end, along[point]);
}
}
return end;
}
private static double firstMouth(Reach reach, double[] along) {
for (int point = 0; point < along.length; point++) {
if (reach.style()[point] == ReachStyle.MOUTH) {
return along[point];
}
}
return Double.POSITIVE_INFINITY;
}
static final class Lookup {
private final Map<Reach, ReachGeometry> geometries = new IdentityHashMap<>();
private final long seed;
private final int seaLevel;
Lookup(long seed, int seaLevel) {
this.seed = seed;
this.seaLevel = seaLevel;
}
ReachGeometry of(Reach reach) {
ReachGeometry known = geometries.get(reach);
if (known == null) {
known = ReachGeometry.of(reach, seed, seaLevel);
geometries.put(reach, known);
}
return known;
}
int head(ReachCursor.Choice choice) {
return WaterHeads.channelHead(choice, of(choice.reach), seaLevel);
}
int point(ReachCursor.Choice choice) {
return of(choice.reach).point(choice);
}
}
}
@@ -137,7 +137,7 @@ final class ReachSegments {
first = end;
}
}
cursor.waters.resolve(cursor.channel);
cursor.resolveChannel();
}
private int coveredCell(double x, double z) {
@@ -10,6 +10,10 @@ public final class WaterHeads {
return planned - Math.floorMod(planned - seaLevel, STEP);
}
static int channelHead(ReachCursor.Choice choice, ReachGeometry geometry, int seaLevel) {
return snap(choice.reach.grade()[geometry.point(choice)], seaLevel);
}
public static int lowestAboveSea(int seaLevel) {
return seaLevel + STEP;
}
@@ -18,7 +18,7 @@ public final class ReachShaper {
private static final int SMOOTHING_PASSES = 2;
private static final float TAPER_LENGTH = 24f;
private static final float WAIST = 0.5f;
static final float OUTLET_LENGTH = 8f;
public static final float OUTLET_LENGTH = 8f;
private static final float LIP_MARGIN = 4f;
private static final int LEAD_IN_REACH = 16;
private static final float LIP_HALF_WIDTH = 1f;