Improved riverbed generation

- Pond levels follow intersecting lower channels
- Pond sampling covers requested flood margins
- Water steps align with sea level
- River crests grade into surrounding beds
- Riverbeds include varied cross sections and finer noise
- Submerged soils use stable block types
- Hydrology probes inspect wet bed geometry
This commit is contained in:
Brian Neumann-Fopiano committed 2026-10-26 18:01:26 -04:00
1 parent 494cdb1a5c
commit 75837d757b
25 files changed
+1566 -144

No files matched your search

@@ -11,6 +11,7 @@ import art.arcane.iris.generation.hydrology.grid.FlowAccumulator;
import art.arcane.iris.generation.hydrology.grid.HeightLattice;
import art.arcane.iris.generation.hydrology.grid.HydrologySampleBuffer;
import art.arcane.iris.generation.hydrology.grid.PriorityFlood;
import art.arcane.iris.generation.hydrology.raster.PondChannelSettlement;
import art.arcane.iris.generation.hydrology.underground.SeaInlet;
import art.arcane.iris.generation.hydrology.underground.SeaInletPlanner;
import art.arcane.iris.generation.hydrology.water.CupSet;
@@ -108,16 +109,16 @@ public final class WindowPlanner {
}
List<Pond> water = new ArrayList<>(ponds.ponds(raw, drainage.filled(), drainage.basins(), lakes, fine, cups));
water.addAll(lakes);
ShapedNetwork network = shaper.shape(drainage.surface(), drainage.receivers(), drainage.accumulation(), drainage.channels(), water,
fine, settings.seed());
int passes = water.size() + 1;
for (int pass = 1; lowered(water, network.ponds()); pass++) {
ShapedNetwork network = PondChannelSettlement.settle(shaper.shape(drainage.surface(), drainage.receivers(), drainage.accumulation(),
drainage.channels(), water, fine, settings.seed()), settings.seaLevel());
long passes = settlementPasses(water);
for (long pass = 1; lowered(water, network.ponds()); pass++) {
if (pass >= passes) {
throw new IllegalStateException("Window " + key + " ponds did not settle within " + passes + " shaping passes");
}
water = replanned(settled(raw, cups, water, network.ponds()));
network = shaper.shape(drainage.surface(), drainage.receivers(), drainage.accumulation(), drainage.channels(), water, fine,
settings.seed());
network = PondChannelSettlement.settle(shaper.shape(drainage.surface(), drainage.receivers(), drainage.accumulation(),
drainage.channels(), water, fine, settings.seed()), settings.seaLevel());
}
List<Reach> reaches = network.reaches();
List<Pond> windowPonds = windowPonds(network.ponds());
@@ -263,6 +264,16 @@ public final class WindowPlanner {
return settled;
}
private long settlementPasses(List<Pond> water) {
long passes = 1L;
for (Pond pond : water) {
if (pond.kind() != PondKind.LAKE) {
passes += Math.max(0L, (long) pond.level() - settings.seaLevel()) + 1L;
}
}
return passes;
}
private static boolean lowered(List<Pond> planned, List<Pond> shaped) {
for (int index = 0; index < shaped.size(); index++) {
if (shaped.get(index).level() < planned.get(index).level()) {
@@ -0,0 +1,55 @@
package art.arcane.iris.generation.hydrology.raster;
import art.arcane.iris.generation.hydrology.grid.CellHash;
final class BedProfile {
private static final long SALT_BROAD = 0x42454442524f4144L;
private static final long SALT_DETAIL = 0x4245444445544149L;
private static final long SALT_TEXTURE = 0x4245445445585455L;
private BedProfile() {
}
static double variation(long seed, int x, int z) {
return 0.65D * noise(seed, SALT_BROAD, x, z, 32) + 0.35D * noise(seed, SALT_DETAIL, x, z, 8);
}
static double detail(long seed, int x, int z) {
return 0.35D * noise(seed, SALT_TEXTURE, x, z, 2);
}
static double position(double u, double variation) {
double bounded = Math.clamp(u, 0D, 1D);
return bounded + 0.8D * variation * bounded * (1D - bounded);
}
static int channelDepth(double depth, double ratio, double variation, double detail) {
double across = position(ratio, -variation);
double bowl = 1D - across * across;
double cut = bowl * (depth + variation * Math.min(2D, depth * 0.3D) + detail);
return (int) Math.clamp(Math.round(cut), 1L, Math.max(1L, Math.round(depth)));
}
static int pondDepth(int maximum, double u, double variation, double detail) {
double across = position(u, variation);
double bowl = across * across * (3D - 2D * across);
double cut = 1D + bowl * (maximum - 1D + variation * Math.min(2D, (maximum - 1D) * 0.3D) + detail);
return (int) Math.clamp(Math.round(cut), 1L, maximum);
}
private static double noise(long seed, long salt, int x, int z, int scale) {
int ix = Math.floorDiv(x, scale);
int iz = Math.floorDiv(z, scale);
double u = fade(Math.floorMod(x, scale) / (double) scale);
double v = fade(Math.floorMod(z, scale) / (double) scale);
double a = CellHash.unit(CellHash.mix(seed, salt, ix, iz));
double b = CellHash.unit(CellHash.mix(seed, salt, ix + 1, iz));
double c = CellHash.unit(CellHash.mix(seed, salt, ix, iz + 1));
double d = CellHash.unit(CellHash.mix(seed, salt, ix + 1, iz + 1));
return 2D * ((a + (b - a) * u) * (1D - v) + (c + (d - c) * u) * v) - 1D;
}
private static double fade(double value) {
return value * value * value * (value * (value * 6D - 15D) + 10D);
}
}
@@ -50,7 +50,10 @@ final class ChunkFeatures {
List<SeaInlet> inlets = new ArrayList<>();
for (IndexedWindow window : windows) {
for (SeaInlet inlet : window.plan().inlets()) {
if (SeaInletRasterizer.touches(inlet, chunkX, chunkZ)) {
int[] bounds = SeaInletRasterizer.bounds(inlet);
int padding = RiffleProfile.RADIUS + 1;
if (bounds[0] <= chunkX * CHUNK + CHUNK - 1 + padding && bounds[2] >= chunkX * CHUNK - padding
&& bounds[1] <= chunkZ * CHUNK + CHUNK - 1 + padding && bounds[3] >= chunkZ * CHUNK - padding) {
inlets.add(inlet);
}
}
@@ -83,6 +86,25 @@ final class ChunkFeatures {
return segments;
}
boolean hasOnlyLevel(int head, int seaLevel) {
for (Pond pond : ponds) {
if (pond.level() - Math.floorMod(pond.level() - seaLevel, 2) != head) {
return false;
}
}
for (Pond lake : lakes) {
if (lake.level() - Math.floorMod(lake.level() - seaLevel, 2) != head) {
return false;
}
}
for (SeaInlet inlet : inlets) {
if (inlet.waterLevel() - Math.floorMod(inlet.waterLevel() - seaLevel, 2) != head) {
return false;
}
}
return segments.hasOnlyLevel(head, seaLevel);
}
int ponds() {
return ponds.size();
}
@@ -28,7 +28,9 @@ public final class ChunkRasterizer {
}
public static List<RegionKey> lakeRegions(int chunkX, int chunkZ) {
return RegionKey.lakeOwners(chunkX * CHUNK, chunkZ * CHUNK, chunkX * CHUNK + CHUNK - 1, chunkZ * CHUNK + CHUNK - 1);
int halo = RiffleProfile.RADIUS + 1;
return RegionKey.lakeOwners(chunkX * CHUNK - halo, chunkZ * CHUNK - halo,
chunkX * CHUNK + CHUNK - 1 + halo, chunkZ * CHUNK + CHUNK - 1 + halo);
}
public ChunkWaterRaster rasterize(int chunkX, int chunkZ, IntBinaryOperator naturalHeight, List<IndexedWindow> windows,
@@ -0,0 +1,129 @@
package art.arcane.iris.generation.hydrology.raster;
import art.arcane.iris.generation.hydrology.water.Pond;
import art.arcane.iris.generation.hydrology.water.PondKind;
import art.arcane.iris.generation.hydrology.water.PondMask;
import art.arcane.iris.generation.hydrology.water.Reach;
import art.arcane.iris.generation.hydrology.water.ShapedNetwork;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
public final class PondChannelSettlement {
private static final int[][] SIDES = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
private PondChannelSettlement() {
}
public static ShapedNetwork settle(ShapedNetwork network, 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(network.reaches(), network.ponds(), initial, seaLevel);
List<Pond> water = new ArrayList<>(initial.length);
for (int index = 0; index < initial.length; index++) {
Pond pond = network.ponds().get(index);
water.add(levels[index] == initial[index] ? pond : new Pond(pond.id(), pond.kind(), levels[index], pond.floor(), pond.spillX(),
pond.spillZ(), pond.spillHeight(), pond.mask(), pond.outletReachId(), pond.damRaise(), pond.fluid(), pond.floorX(), pond.floorZ()));
}
return new ShapedNetwork(network.reaches(), water);
}
static int[] levels(List<Reach> reaches, List<Pond> ponds, int[] initial, int seaLevel) {
int[] levels = initial.clone();
if (reaches.isEmpty() || ponds.isEmpty()) {
return levels;
}
boolean eligible = false;
for (int pond = 0; pond < ponds.size() && !eligible; pond++) {
eligible = ponds.get(pond).kind() != PondKind.LAKE && levels[pond] > seaLevel;
}
if (!eligible) {
return levels;
}
ReachIndex index = ReachIndex.build(reaches, List.of());
List<Reach> nearby = new ArrayList<>();
List<Pond> unused = new ArrayList<>();
for (int pond = 0; pond < ponds.size(); pond++) {
Pond water = ponds.get(pond);
if (water.kind() == PondKind.LAKE || levels[pond] <= seaLevel || water.mask().width() == 0 || water.mask().depth() == 0) {
continue;
}
Bounds bounds = Bounds.of(water.mask());
int chunkX = (bounds.minX() + (bounds.maxX() - bounds.minX()) / 2) >> 4;
int chunkZ = (bounds.minZ() + (bounds.maxZ() - bounds.minZ()) / 2) >> 4;
int halo = Math.max(0, Math.max(Math.max((chunkX << 4) - bounds.minX(), bounds.maxX() - (chunkX << 4) - 15),
Math.max((chunkZ << 4) - bounds.minZ(), bounds.maxZ() - (chunkZ << 4) - 15))) + 1;
nearby.clear();
index.candidates(chunkX, chunkZ, halo, nearby, unused);
if (!nearby.isEmpty()) {
levels[pond] = lower(nearby, water.mask(), bounds, levels[pond], seaLevel);
}
}
return levels;
}
private static int lower(List<Reach> reaches, PondMask mask, Bounds bounds, int level, 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();
int width = bounds.maxX() - bounds.minX() + 1;
BitSet visited = new BitSet(width * (bounds.maxZ() - bounds.minZ() + 1));
for (Reach reach : reaches) {
for (int segment = 0; segment + 1 < reach.xs().length; segment++) {
if (Math.min(head(reach.grade()[segment], seaLevel), head(reach.grade()[segment + 1], seaLevel)) >= head(level, seaLevel)) {
continue;
}
float radius = Math.max(reach.halfWidth()[segment], reach.halfWidth()[segment + 1]) + 1f;
int minX = Math.max(bounds.minX(), (int) Math.ceil(Math.min(reach.xs()[segment], reach.xs()[segment + 1]) - radius));
int minZ = Math.max(bounds.minZ(), (int) Math.ceil(Math.min(reach.zs()[segment], reach.zs()[segment + 1]) - radius));
int maxX = Math.min(bounds.maxX(), (int) Math.floor(Math.max(reach.xs()[segment], reach.xs()[segment + 1]) + radius));
int maxZ = Math.min(bounds.maxZ(), (int) Math.floor(Math.max(reach.zs()[segment], reach.zs()[segment + 1]) + radius));
for (int z = minZ; z <= maxZ; z++) {
for (int x = minX; x <= maxX; x++) {
int cell = (z - bounds.minZ()) * width + x - bounds.minX();
if (visited.get(cell) || !mask.contains(x, z)) {
continue;
}
visited.set(cell);
level = Math.min(level, touchingHead(segments, cursor, 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));
}
}
}
}
}
}
return level;
}
private static int touchingHead(ReachSegments segments, ReachCursor cursor, 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 = head(channel.reach.grade()[channel.point()], seaLevel);
return head < head(level, seaLevel) ? head : level;
}
private static int head(int level, int seaLevel) {
return level - Math.floorMod(level - seaLevel, 2);
}
private record Bounds(int minX, int minZ, int maxX, int maxZ) {
private static Bounds of(PondMask mask) {
int minX = mask.originX() - (mask.step() >> 1);
int minZ = mask.originZ() - (mask.step() >> 1);
return new Bounds(minX, minZ, minX + mask.width() * mask.step() - 1, minZ + mask.depth() * mask.step() - 1);
}
}
}
@@ -67,6 +67,7 @@ final class RasterComposer {
column(cell);
}
}
gradeRiffles();
raiseDams();
bermEdges();
measureLakeShores();
@@ -80,8 +81,8 @@ final class RasterComposer {
int ground = natural[cell];
SeaInlet inlet = features.openInletAt(x, z, inletColumn);
if (inlet != null) {
head[cell] = inlet.waterLevel();
terrain[cell] = Math.min(ground, inletColumn[0]);
head[cell] = level(inlet.waterLevel());
terrain[cell] = Math.min(Math.min(ground, inletColumn[0]), head[cell] - 1);
role[cell] = WaterRole.INLET;
feature[cell] = inlet.id();
fluid[cell] = HydrologyCaveCell.SEA_FLUID;
@@ -91,72 +92,77 @@ final class RasterComposer {
int pondIndex = lake >= 0 ? -1 : features.pondAt(x, z);
boolean shore = interior(cell) && lake < 0 && pondIndex < 0;
features.segments().nearest(x, z, shore ? ReachSegments.Scope.SHORE : ReachSegments.Scope.CHANNEL, cursor);
double variation = BedProfile.variation(settings.seed(), x, z);
if (lake >= 0) {
Pond water = features.lake(lake);
standing(cell, water, trench(ground, water.level(), ground));
standing(cell, water, trench(ground, level(water.level()), ground, variation, BedProfile.detail(settings.seed(), x, z)));
return;
}
if (pondIndex >= 0) {
Pond water = features.pond(pondIndex);
pond[cell] = pondIndex;
int floor = interior(cell) ? pondFloor(pondIndex, water.level(), x, z, ground) : ground;
standing(cell, water, trench(floor, water.level(), ground));
double detail = BedProfile.detail(settings.seed(), x, z);
int floor = interior(cell) ? pondFloor(pondIndex, level(water.level()), x, z, ground, variation, detail) : ground;
standing(cell, water, trench(floor, level(water.level()), ground, variation, detail));
return;
}
if (cursor.channel.reach != null) {
channel(cell, ground);
channel(cell, ground, variation, BedProfile.detail(settings.seed(), x, z));
} else if (interior(cell)) {
dry(cell, x, z, ground);
dry(cell, x, z, ground, variation);
}
}
private void standing(int cell, Pond water, int floor) {
head[cell] = water.level();
terrain[cell] = floor;
head[cell] = level(water.level());
terrain[cell] = Math.min(floor, head[cell] - 1);
role[cell] = WaterRole.POND;
feature[cell] = water.id();
fluid[cell] = water.fluid();
}
private int trench(int floor, int level, int ground) {
private int trench(int floor, int level, int ground, double variation, double detail) {
ReachCursor.Choice hit = cursor.channel;
if (hit.reach == null) {
return floor;
}
return Math.min(floor, bed(hit, Math.min(level, hit.reach.grade()[hit.point()]), ground));
return Math.min(floor, bed(hit, Math.min(level, level(hit.reach.grade()[hit.point()])), ground, variation, detail));
}
private void channel(int cell, int ground) {
private void channel(int cell, int ground, double variation, double detail) {
ReachCursor.Choice hit = cursor.channel;
Reach reach = hit.reach;
int point = hit.point();
head[cell] = reach.grade()[point];
terrain[cell] = bed(hit, head[cell], ground);
head[cell] = level(reach.grade()[point]);
terrain[cell] = bed(hit, head[cell], ground, variation, detail);
role[cell] = role(reach.style()[point]);
feature[cell] = reach.id();
tier[cell] = reach.tier();
fluid[cell] = reach.tier().fluid();
}
private static int bed(ReachCursor.Choice hit, int surface, int ground) {
private static int bed(ReachCursor.Choice hit, int surface, int ground, double variation, double detail) {
double width = hit.halfWidth();
double ratio = width > 0D ? hit.distance / width : 0D;
long cut = Math.max(1L, Math.round(hit.depth() * (1D - ratio * ratio)));
long cut = BedProfile.channelDepth(hit.depth(), ratio, variation, detail);
return (int) Math.min(ground, surface - cut);
}
private void dry(int cell, int x, int z, int ground) {
private void dry(int cell, int x, int z, int ground, double variation) {
int cut = ground;
int raise = Integer.MIN_VALUE;
double bankEdge = Double.POSITIVE_INFINITY;
ReachCursor.Choice bank = cursor.bank;
if (bank.reach != null) {
HydrologyTier bankTier = bank.reach.tier();
int surface = bank.reach.grade()[bank.point()];
int surface = level(bank.reach.grade()[bank.point()]);
bankEdge = bank.distance - bank.halfWidth();
double u = bankEdge / bankTier.bankBand();
if (bankTier.bankStyle() != IrisBankStyle.CLIFF && bankTier.bankStyle() != IrisBankStyle.TERRACED) {
u = BedProfile.position(u, -variation);
}
if (ground >= surface) {
cut = BankProfile.cut(bankTier.bankStyle(), surface, ground, u, coneSlope(cell) * bankEdge, noise(x, z));
cut = BankProfile.cut(bankTier.bankStyle(), surface, ground, u, coneSlope(cell) * u * bankTier.bankBand(), noise(x, z));
} else {
raise = BankProfile.levee(bankTier.bankStyle(), surface, ground, u, noise(x, z), settings.deformation().maxFill());
}
@@ -172,8 +178,8 @@ final class RasterComposer {
}
int taper = settings.ponds().shore().taper();
boolean tapered = nearPond >= 0 && pondEdge <= taper;
if (tapered && ground > features.pond(nearPond).level()) {
cut = Math.min(cut, taperCut(features.pond(nearPond).level(), ground, pondEdge / taper));
if (tapered && ground > level(features.pond(nearPond).level())) {
cut = Math.min(cut, taperCut(level(features.pond(nearPond).level()), ground, BedProfile.position(pondEdge / taper, -variation)));
}
terrain[cell] = raise == Integer.MIN_VALUE ? cut : Math.max(cut, raise);
if (bank.reach != null && (!tapered || bankEdge <= pondEdge)) {
@@ -215,11 +221,58 @@ final class RasterComposer {
return (int) Math.clamp((long) Math.floor(level + rise), level, ground);
}
private int pondFloor(int index, int level, int x, int z, int ground) {
private int level(int planned) {
return planned - Math.floorMod(planned - settings.seaLevel(), 2);
}
private void gradeRiffles() {
int surface = Integer.MIN_VALUE;
for (int cell = 0; cell < CELLS && surface == Integer.MIN_VALUE; cell++) {
if (interior(cell)) {
surface = head[cell];
}
}
if (surface == Integer.MIN_VALUE || features.hasOnlyLevel(surface, settings.seaLevel())) {
return;
}
RiffleProfile riffles = new RiffleProfile((originX + 1) >> 4, (originZ + 1) >> 4, this::headAt);
for (int cell = 0; cell < CELLS; cell++) {
if (interior(cell) && head[cell] != Integer.MIN_VALUE) {
int x = originX + cell % GRID;
int z = originZ + cell / GRID;
terrain[cell] = riffles.bed(cell % GRID - 1, cell / GRID - 1, terrain[cell], head[cell],
BedProfile.variation(settings.seed(), x, z));
}
}
}
private int headAt(int x, int z) {
int gx = x - originX;
int gz = z - originZ;
if (gx >= 0 && gx < GRID && gz >= 0 && gz < GRID && !corner(gz * GRID + gx)) {
return head[gz * GRID + gx];
}
SeaInlet inlet = features.openInletAt(x, z, inletColumn);
if (inlet != null) {
return level(inlet.waterLevel());
}
int lake = features.lakeCovers(x, z) ? features.lakeAt(x, z, naturalAt(x, z)) : -1;
if (lake >= 0) {
return level(features.lake(lake).level());
}
int pond = features.pondAt(x, z);
if (pond >= 0) {
return level(features.pond(pond).level());
}
features.segments().nearest(x, z, ReachSegments.Scope.CHANNEL, cursor);
return cursor.channel.reach == null ? Integer.MIN_VALUE : level(cursor.channel.reach.grade()[cursor.channel.point()]);
}
private int pondFloor(int index, int level, int x, int z, int ground, double variation, double detail) {
HydrologySettings.PondShore shore = settings.ponds().shore();
double bankDistance = features.pondBankDistance(index, x, z);
double u = shore.shelf() <= 0 ? 1D : Math.clamp((bankDistance - 1D) / shore.shelf(), 0D, 1D);
long bowl = Math.max(1L, Math.round(1D + (shore.maxDepth() - 1D) * BankProfile.shape(IrisBankStyle.SMOOTH, u)));
long bowl = BedProfile.pondDepth(shore.maxDepth(), u, variation, detail);
return (int) Math.min(ground, level - bowl);
}
@@ -236,8 +289,9 @@ final class RasterComposer {
continue;
}
Pond held = features.pond(index);
if (held.damRaise() > 0 && terrain[cell] < held.level() && held.level() + 1 > crest) {
crest = held.level() + 1;
int level = level(held.level());
if (held.damRaise() > 0 && terrain[cell] < level && level + 1 > crest) {
crest = level + 1;
dam = held.id();
}
}
@@ -45,6 +45,18 @@ final class ReachSegments {
}
}
boolean hasOnlyLevel(int head, int seaLevel) {
for (int index = 0; index < size; index++) {
int[] grade = reaches[index].grade();
int start = grade[segments[index]];
int end = grade[segments[index] + 1];
if (start - Math.floorMod(start - seaLevel, 2) != head || end - Math.floorMod(end - seaLevel, 2) != head) {
return false;
}
}
return true;
}
void addNear(Reach reach, float minX, float minZ, float maxX, float maxZ, float outward) {
float[] xs = reach.xs();
float[] zs = reach.zs();
@@ -0,0 +1,177 @@
package art.arcane.iris.generation.hydrology.raster;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
import java.util.function.IntBinaryOperator;
final class RiffleProfile {
static final int RADIUS = 16;
private static final int CHUNK = 16;
private static final int PADDING = RADIUS + 1;
private static final int GRID = CHUNK + 2 * PADDING;
private static final int OUTPUT = CHUNK + 2;
private static final int LIMIT = 10 * RADIUS;
private static final int DRY = Integer.MIN_VALUE;
private static final int[] SIDES = {1, -1, GRID, -GRID};
private final List<Field> fields = new ArrayList<>();
RiffleProfile(int chunkX, int chunkZ, IntBinaryOperator headAt) {
int[] heads = new int[GRID * GRID];
int[] crests = new int[GRID * GRID];
int originX = (chunkX << 4) - PADDING;
int originZ = (chunkZ << 4) - PADDING;
Arrays.fill(crests, DRY);
for (int cell = 0; cell < heads.length; cell++) {
heads[cell] = headAt.applyAsInt(originX + cell % GRID, originZ + cell / GRID);
}
TreeSet<Integer> levels = new TreeSet<>();
for (int z = 1; z < GRID - 1; z++) {
for (int x = 1; x < GRID - 1; x++) {
int cell = z * GRID + x;
if (heads[cell] == DRY) {
continue;
}
for (int side : SIDES) {
if (heads[cell + side] != DRY && heads[cell + side] <= heads[cell] - 2) {
crests[cell] = heads[cell] - 1;
levels.add(crests[cell]);
break;
}
}
}
}
if (levels.isEmpty()) {
return;
}
DistanceField distances = new DistanceField(heads);
for (int crest : levels) {
distances.reset();
for (int cell = 0; cell < crests.length; cell++) {
if (crests[cell] == crest) {
distances.offer(cell, 0);
}
}
distances.spread();
int[] output = distances.output();
if (output != null) {
fields.add(new Field(crest, output));
}
}
}
int bed(int localX, int localZ, int original, int head, double variation) {
if (head == DRY) {
return original;
}
int cell = (localZ + 1) * OUTPUT + localX + 1;
double raised = original;
for (Field field : fields) {
int crest = Math.min(head - 1, field.crest());
if (crest <= original) {
continue;
}
double reach = 10D * Math.min(RADIUS, Math.max(4D, 2D * (crest - (double) original)))
* (0.925D + 0.075D * variation);
if (field.distances()[cell] >= reach) {
continue;
}
double u = field.distances()[cell] / reach;
double blend = 1D - u * u * (3D - 2D * u);
raised = Math.max(raised, original + (crest - (double) original) * blend);
}
return (int) Math.max(original, Math.min(head - 1L, Math.round(raised)));
}
private record Field(int crest, int[] distances) {
}
private static final class DistanceField {
private final int[] heads;
private final int[] distances = new int[GRID * GRID];
private final int[] next = new int[GRID * GRID];
private final int[] previous = new int[GRID * GRID];
private final int[] first = new int[LIMIT];
private DistanceField(int[] heads) {
this.heads = heads;
}
private void reset() {
Arrays.fill(distances, LIMIT);
Arrays.fill(next, -1);
Arrays.fill(previous, -1);
Arrays.fill(first, -1);
}
private void offer(int cell, int distance) {
if (distances[cell] < LIMIT) {
remove(cell);
}
distances[cell] = distance;
next[cell] = first[distance];
previous[cell] = -1;
if (next[cell] >= 0) {
previous[next[cell]] = cell;
}
first[distance] = cell;
}
private void remove(int cell) {
if (previous[cell] < 0) {
first[distances[cell]] = next[cell];
} else {
next[previous[cell]] = next[cell];
}
if (next[cell] >= 0) {
previous[next[cell]] = previous[cell];
}
next[cell] = -1;
previous[cell] = -1;
}
private void spread() {
for (int distance = 0; distance < LIMIT; distance++) {
while (first[distance] >= 0) {
int cell = first[distance];
remove(cell);
int x = cell % GRID;
int z = cell / GRID;
for (int dz = -1; dz <= 1; dz++) {
for (int dx = -1; dx <= 1; dx++) {
int nx = x + dx;
int nz = z + dz;
if (dx == 0 && dz == 0 || nx < 0 || nx >= GRID || nz < 0 || nz >= GRID) {
continue;
}
int neighbour = nz * GRID + nx;
int candidate = distance + (dx != 0 && dz != 0 ? 14 : 10);
if (heads[neighbour] == DRY || candidate >= distances[neighbour]) {
continue;
}
if (dx != 0 && dz != 0 && (heads[z * GRID + nx] == DRY || heads[nz * GRID + x] == DRY)) {
continue;
}
offer(neighbour, candidate);
}
}
}
}
}
private int[] output() {
int[] output = new int[OUTPUT * OUTPUT];
boolean affected = false;
for (int z = 0; z < OUTPUT; z++) {
for (int x = 0; x < OUTPUT; x++) {
int distance = distances[(z + PADDING - 1) * GRID + x + PADDING - 1];
output[z * OUTPUT + x] = distance;
affected |= distance < LIMIT;
}
}
return affected ? output : null;
}
}
}
@@ -126,8 +126,10 @@ public final class PondPlanner {
public Pond reflood(DemGrid raw, HeightLattice fine, CupSet cups, Pond pond) {
PondMask mask = pond.mask();
int step = mask.step();
Box box = box(raw, fine, mask.originX() - step, mask.originZ() - step, mask.originX() + mask.width() * step,
mask.originZ() + mask.depth() * step, cups, Math.max(1, step / fine.cell()));
int margin = Math.max(step, fine.cell());
Box box = box(raw, fine, mask.originX() - margin, mask.originZ() - margin,
mask.originX() + (mask.width() - 1) * step + margin, mask.originZ() + (mask.depth() - 1) * step + margin,
cups, Math.max(1, step / fine.cell()));
if (box == null) {
return null;
}
@@ -669,6 +671,9 @@ public final class PondPlanner {
}
int i = node % box.width();
int j = node / box.width();
if (i == 0 || j == 0 || i == box.width() - 1 || j == box.depth() - 1) {
return false;
}
for (int axis = 0; axis < AXIS_X.length; axis++) {
int neighbour = (j + AXIS_Z[axis]) * box.width() + i + AXIS_X[axis];
if (!wet[neighbour] && ground[neighbour] < level) {
@@ -29,11 +29,7 @@ final class EffectiveWater {
if (head == DRY) {
return highest == DRY ? column : column(Math.max(ground, highest + 1), DRY);
}
int oneAbove = oneAbove(head, east) + oneAbove(head, west) + oneAbove(head, south) + oneAbove(head, north);
if (oneAbove >= 2) {
return column(Math.max(ground, highest), DRY);
}
return dropsBelow(head, east, west, south, north) ? column(Math.max(ground, head - 1), head) : column;
return column;
}
static void settleGrid(int originX, int originZ, int width, int depth, Columns columns, long[] settled) {
@@ -61,18 +57,6 @@ final class EffectiveWater {
}
}
private static int oneAbove(int head, long neighbour) {
return head(neighbour) != DRY && head(neighbour) == head + 1 ? 1 : 0;
}
private static boolean dropsBelow(int head, long east, long west, long south, long north) {
return dropsBelow(head, east) || dropsBelow(head, west) || dropsBelow(head, south) || dropsBelow(head, north);
}
private static boolean dropsBelow(int head, long neighbour) {
return head(neighbour) != DRY && head(neighbour) <= head - 2;
}
interface Columns {
long pre(int x, int z);
@@ -48,6 +48,8 @@ import lombok.Getter;
public class IrisTerrainNormalActuator extends EngineAssignedActuator<NativeBlockState> {
private static final BoundBlockState BEDROCK = BoundBlockState.of("BEDROCK");
private static final BoundBlockState AIR = BoundBlockState.of("AIR");
private static final BoundBlockState DIRT = BoundBlockState.of("minecraft:dirt");
private static final BoundBlockState NETHERRACK = BoundBlockState.of("minecraft:netherrack");
private static final int SLOT_SEA_LAYERS = 1;
@Getter
private final RNG rng;
@@ -132,6 +134,17 @@ public class IrisTerrainNormalActuator extends EngineAssignedActuator<NativeBloc
return painted == null ? layerBlock : painted;
}
private static NativeBlockState submergedSoil(NativeBlockState block) {
if (block == null) {
return null;
}
return switch (IrisProceduralBlocks.materialKey(block)) {
case "minecraft:grass_block", "minecraft:mycelium", "minecraft:dirt_path", "minecraft:farmland" -> DIRT.get();
case "minecraft:crimson_nylium", "minecraft:warped_nylium" -> NETHERRACK.get();
default -> block;
};
}
private final class ColumnPainter {
private final int x;
private final int z;
@@ -214,6 +227,7 @@ public class IrisTerrainNormalActuator extends EngineAssignedActuator<NativeBloc
int topY = Math.min(hf, chunkHeight - 1);
WaterRole role = waterRoleStream.get(realX, realZ);
boolean wet = role.wet();
boolean submerged = wet && he < surfaceFluidHeight;
boolean seaFloor = !wet || role == WaterRole.INLET;
IrisHydrologyMaterials materials = role == WaterRole.NONE || role == WaterRole.INLET ? null
: complex.resolveWaterMaterials(realX, realZ);
@@ -292,7 +306,7 @@ public class IrisTerrainNormalActuator extends EngineAssignedActuator<NativeBloc
if (i <= he) {
int depth = layerSurfaceY - i;
if (depth == 0 && mappedSurfaceBlock != null) {
put(i, mappedSurfaceBlock);
put(i, submerged && layerSurfaceY == he ? submergedSoil(mappedSurfaceBlock) : mappedSurfaceBlock);
continue;
}
if (blocks == null) {
@@ -327,6 +341,9 @@ public class IrisTerrainNormalActuator extends EngineAssignedActuator<NativeBloc
NativeBlockState layerBlock = paintWaterMaterial(
blocks.hasIndex(strataIndex) ? blocks.get(strataIndex) : null,
rolePalette, paintDepth, depth, rng, realX, i, realZ, data);
if (submerged && layerSurfaceY == he) {
layerBlock = submergedSoil(layerBlock);
}
if (layerBlock != null) {
if (keepGravityAway && IrisProceduralBlocks.isGravityAffected(layerBlock)) {
layerBlock = rock;