diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChunkFeatures.java b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChunkFeatures.java index 54eba5f04..340defbbf 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChunkFeatures.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/ChunkFeatures.java @@ -50,7 +50,7 @@ final class ChunkFeatures { static ChunkFeatures gather(HydrologySettings settings, int chunkX, int chunkZ, int halo, List windows, List lakeRegionPlans, List lakeRegions) { - Links links = new Links(windowsByKey(chunkX, chunkZ, windows)); + Links links = new Links(windowsByKey(chunkX, chunkZ, windows), settings.seaLevel()); List reaches = new ArrayList<>(); List ponds = new ArrayList<>(); List inlets = new ArrayList<>(); @@ -296,11 +296,13 @@ final class ChunkFeatures { private static final class Links { private final Map windows; + private final int seaLevel; private final Map resolved = new HashMap<>(); private final Set resolving = new HashSet<>(); - private Links(Map windows) { + private Links(Map windows, int seaLevel) { this.windows = windows; + this.seaLevel = seaLevel; } private Reach resolve(Reach reach, WindowKey owner) { @@ -320,7 +322,7 @@ final class ChunkFeatures { private Reach link(Reach reach, WindowKey owner) { Integer pondLevel = outletLevel(reach, owner); Reach linked = feederLink(reach, owner, pondLevel != null || reach.style()[0] == ReachStyle.POND_OUTLET); - return settle(reach, pondLevel == null ? linked : LinkBlend.clamp(linked, pondLevel), owner); + return settle(reach, pondLevel == null ? linked : LinkBlend.clamp(linked, pondLevel, seaLevel), owner); } private Reach settle(Reach feeder, Reach linked, WindowKey owner) { @@ -342,7 +344,7 @@ final class ChunkFeatures { receiving.add(LinkBlend.footprint(sibling)); } } - return LinkBlend.settleInto(linked, level, receiving, lips(feeder, owner)); + return LinkBlend.settleInto(linked, new LinkBlend.Junction(level, fed(feeder)), receiving, lips(feeder, owner), seaLevel); } private IndexedWindow ownerOf(long id) { @@ -376,7 +378,12 @@ final class ChunkFeatures { for (Reach reach : joining) { feeders.add(cornerBlock(reach) == block ? resolve(reach, link) : reach); } - return link.equals(owner) || outlet ? LinkBlend.clamp(downstream, feeders) : LinkBlend.blend(downstream, feeders); + return link.equals(owner) || outlet ? LinkBlend.clamp(downstream, feeders, seaLevel) : LinkBlend.blend(downstream, feeders, seaLevel); + } + + private boolean fed(Reach reach) { + int junction = junction(reach); + return !feeders(windowAt(reach, junction), reach.id()).isEmpty() || junction > 0 && !feeders(windowAt(reach, 0), reach.id()).isEmpty(); } private List feeders(WindowKey key, long downstreamId) { diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/LinkBlend.java b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/LinkBlend.java index 256e5a9fe..ebc6fd741 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/LinkBlend.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/LinkBlend.java @@ -22,7 +22,7 @@ public final class LinkBlend { return from + (to - from) * share; } - static Reach blend(Reach downstream, List feeders) { + static Reach blend(Reach downstream, List feeders, int seaLevel) { float width = 0f; float depth = 0f; for (Reach feeder : feeders) { @@ -44,21 +44,22 @@ public final class LinkBlend { halfWidth[point] += blend(widthShift, 0f, along); depths[point] += blend(depthShift, 0f, along); } - return regraded(downstream, clamped(downstream.grade(), lowestEnd(feeders)), halfWidth, depths); + return regraded(downstream, clamped(downstream.grade(), lowestEnd(feeders)), halfWidth, depths, seaLevel); } - static Reach clamp(Reach downstream, List feeders) { - return clamp(downstream, lowestEnd(feeders)); + static Reach clamp(Reach downstream, List feeders, int seaLevel) { + return clamp(downstream, lowestEnd(feeders), seaLevel); } - static Reach clamp(Reach downstream, int end) { + static Reach clamp(Reach downstream, int end, int seaLevel) { int[] grade = clamped(downstream.grade(), end); - return grade == downstream.grade() ? downstream : regraded(downstream, grade, downstream.halfWidth(), downstream.depth()); + return grade == downstream.grade() ? downstream : regraded(downstream, grade, downstream.halfWidth(), downstream.depth(), seaLevel); } - static Reach settleInto(Reach feeder, int level, List receiving, List lips) { + static Reach settleInto(Reach feeder, Junction junction, List receiving, List lips, int seaLevel) { int[] grade = feeder.grade(); - boolean[] fixed = fixed(feeder, lips); + int level = junction.level(); + boolean[] fixed = fixed(feeder, lips, junction.fed() && grade[0] - level > ConfluenceGrader.MAX_START_LOWERING); int waterfallMinimumDrop = feeder.tier().waterfallMinimumDrop(); int[] settled = ConfluenceGrader.settle(footprint(feeder), grade, fixed, receiving, level, waterfallMinimumDrop); if (settled == grade) { @@ -67,18 +68,18 @@ public final class LinkBlend { if (!feeder.tier().cascades()) { ConfluenceGrader.spreadJunction(settled, grade, fixed, waterfallMinimumDrop, Integer.MIN_VALUE); } - return regraded(feeder, settled, feeder.halfWidth(), feeder.depth()); + return regraded(feeder, settled, feeder.halfWidth(), feeder.depth(), seaLevel); } static ConfluenceGrader.Footprint footprint(Reach reach) { return new ConfluenceGrader.Footprint(reach.xs(), reach.zs(), reach.halfWidth()); } - private static boolean[] fixed(Reach feeder, List lips) { + private static boolean[] fixed(Reach feeder, List lips, boolean heldStart) { ReachStyle[] style = feeder.style(); boolean[] fixed = new boolean[style.length]; boolean held = !lips.isEmpty(); - fixed[0] = true; + fixed[0] = held || heldStart || style[0] == ReachStyle.POND_OUTLET; for (int point = 1; point < style.length; point++) { held = held && nearLip(lips, feeder.xs()[point], feeder.zs()[point], feeder.halfWidth()[point]); fixed[point] = held || style[point] == ReachStyle.POND_OUTLET; @@ -95,20 +96,20 @@ public final class LinkBlend { return false; } - private static Reach regraded(Reach reach, int[] grade, float[] halfWidth, float[] depth) { - ReachStyle[] style = grade == reach.grade() ? reach.style() : restyled(reach.style(), grade, reach.tier().waterfallMinimumDrop()); + private static Reach regraded(Reach reach, int[] grade, float[] halfWidth, float[] depth, int seaLevel) { + ReachStyle[] style = grade == reach.grade() ? reach.style() : restyled(reach.style(), grade, reach.tier().waterfallMinimumDrop(), seaLevel); return new Reach(reach.id(), reach.tier(), reach.downstreamId(), reach.xs(), reach.zs(), grade, halfWidth, depth, style, reach.catchment()); } - private static ReachStyle[] restyled(ReachStyle[] style, int[] grade, int waterfallMinimumDrop) { + private static ReachStyle[] restyled(ReachStyle[] style, int[] grade, int waterfallMinimumDrop, int seaLevel) { boolean[] mouth = new boolean[style.length]; boolean[] outlet = new boolean[style.length]; for (int point = 0; point < style.length; point++) { mouth[point] = style[point] == ReachStyle.MOUTH; outlet[point] = style[point] == ReachStyle.POND_OUTLET; } - return ReachShaper.styles(grade, mouth, outlet, waterfallMinimumDrop); + return ReachShaper.styles(grade, mouth, outlet, waterfallMinimumDrop, seaLevel); } private static int lowestEnd(List feeders) { @@ -131,4 +132,7 @@ public final class LinkBlend { } return result; } + + record Junction(int level, boolean fed) { + } } diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/RasterComposer.java b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/RasterComposer.java index e595c9477..34c77860d 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/RasterComposer.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/RasterComposer.java @@ -73,6 +73,7 @@ final class RasterComposer { column(cell); } } + styleSeams(); gradeRiffles(); raiseDams(); bermEdges(); @@ -277,6 +278,30 @@ final class RasterComposer { return (int) Math.clamp((long) Math.floor(level + rise), level, ground); } + private void styleSeams() { + for (int cell = 0; cell < CELLS; cell++) { + if (!interior(cell) || role[cell] != WaterRole.CHANNEL) { + continue; + } + int drop = 0; + int waterfallMinimumDrop = Integer.MAX_VALUE; + for (int side : SIDES) { + int upper = cell + side; + if (!role[upper].flowing() || feature[upper] == feature[cell]) { + continue; + } + int step = head[upper] - head[cell]; + if (step >= 2 && step > drop) { + drop = step; + waterfallMinimumDrop = tier[upper].waterfallMinimumDrop(); + } + } + if (drop > 0) { + role[cell] = drop >= waterfallMinimumDrop ? WaterRole.WATERFALL : WaterRole.CASCADE; + } + } + } + private void gradeRiffles() { int surface = Integer.MIN_VALUE; for (int cell = 0; cell < CELLS && surface == Integer.MIN_VALUE; cell++) { @@ -287,7 +312,7 @@ final class RasterComposer { if (surface == Integer.MIN_VALUE || features.hasOnlyLevel(surface, settings.seaLevel())) { return; } - RiffleProfile riffles = new RiffleProfile((originX + 1) >> 4, (originZ + 1) >> 4, this::headAt); + RiffleProfile riffles = new RiffleProfile((originX + 1) >> 4, (originZ + 1) >> 4, this::sample); for (int cell = 0; cell < CELLS; cell++) { if (interior(cell) && head[cell] != Integer.MIN_VALUE) { int x = originX + cell % GRID; @@ -298,26 +323,34 @@ final class RasterComposer { } } - private int headAt(int x, int z) { + private void sample(int x, int z, RiffleProfile.Sample sample) { 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]; + int cell = gz * GRID + gx; + sample.set(head[cell], feature[cell], role[cell]); + return; } SeaInlet inlet = features.openInletAt(x, z, inletColumn); if (inlet != null) { - return WaterHeads.snap(inlet.waterLevel(), settings.seaLevel()); + sample.set(WaterHeads.snap(inlet.waterLevel(), settings.seaLevel()), inlet.id(), WaterRole.INLET); + return; } int lake = features.lakeCovers(x, z) ? features.lakeAt(x, z, naturalAt(x, z)) : -1; if (lake >= 0) { - return WaterHeads.snap(features.lake(lake).level(), settings.seaLevel()); + sample.set(WaterHeads.snap(features.lake(lake).level(), settings.seaLevel()), features.lake(lake).id(), WaterRole.POND); + return; } int pond = features.pondAt(x, z); if (pond >= 0) { - return WaterHeads.snap(features.pond(pond).level(), settings.seaLevel()); + sample.set(WaterHeads.snap(features.pond(pond).level(), settings.seaLevel()), features.pond(pond).id(), WaterRole.POND); + return; } features.segments().nearest(x, z, ReachSegments.Scope.CHANNEL, cursor); - return cursor.channel.reach == null ? Integer.MIN_VALUE : WaterHeads.snap(cursor.channel.reach.grade()[cursor.channel.point()], settings.seaLevel()); + 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()])); + } } private int pondFloor(int index, int level, int x, int z, int ground, double variation, double detail) { diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/RiffleProfile.java b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/RiffleProfile.java index d56581969..f32a219a3 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/RiffleProfile.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/RiffleProfile.java @@ -3,11 +3,18 @@ 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; +import java.util.Map; +import java.util.TreeMap; final class RiffleProfile { static final int RADIUS = 16; + static final int PLUNGE_MIN_DROP = 6; + static final double PLUNGE_RATIO = 0.5D; + static final int PLUNGE_MAX_DEPTH = 8; + private static final int PLUNGE_MIN_DEPTH = 2; + private static final double PLUNGE_RADIUS_RATIO = 0.75D; + private static final double PLUNGE_MIN_RADIUS = 3D; + private static final double PLUNGE_MAX_RADIUS = 12D; private static final int CHUNK = 16; private static final int PADDING = RADIUS + 1; private static final int GRID = CHUNK + 2 * PADDING; @@ -17,48 +24,44 @@ final class RiffleProfile { private static final int[] SIDES = {1, -1, GRID, -GRID}; private static final int[] REMAINING = remaining(); + private final int[] heads = new int[GRID * GRID]; + private final long[] features = new long[GRID * GRID]; + private final boolean[] flowing = new boolean[GRID * GRID]; private final List fields = new ArrayList<>(); + private final List plunges = new ArrayList<>(); - RiffleProfile(int chunkX, int chunkZ, IntBinaryOperator headAt) { - int[] heads = new int[GRID * GRID]; - int[] crests = new int[GRID * GRID]; + RiffleProfile(int chunkX, int chunkZ, Water water) { int originX = (chunkX << 4) - PADDING; int originZ = (chunkZ << 4) - PADDING; - Arrays.fill(crests, DRY); + Sample sample = new Sample(); for (int cell = 0; cell < heads.length; cell++) { - heads[cell] = headAt.applyAsInt(originX + cell % GRID, originZ + cell / GRID); + sample.set(DRY, 0L, WaterRole.NONE); + water.sample(originX + cell % GRID, originZ + cell / GRID, sample); + heads[cell] = sample.head; + features[cell] = sample.feature; + flowing[cell] = sample.role.flowing(); } - TreeSet levels = new TreeSet<>(); + TreeMap crests = new TreeMap<>(); + TreeMap feet = new TreeMap<>(); 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; - } - } + collect(z * GRID + x, crests, feet); } } - if (levels.isEmpty()) { + if (crests.isEmpty() && feet.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(); + for (Map.Entry crest : crests.entrySet()) { + int[] output = distances.measure(crest.getValue(), DistanceField.ANY_LEVEL); if (output != null) { - fields.add(new Field(crest, output)); + fields.add(new Field(crest.getKey(), output)); + } + } + for (Map.Entry fall : feet.entrySet()) { + int[] output = distances.measure(fall.getValue(), fall.getKey().foot()); + if (output != null) { + plunges.add(new Plunge(fall.getKey(), output)); } } } @@ -68,9 +71,13 @@ final class RiffleProfile { return original; } int cell = (localZ + 1) * OUTPUT + localX + 1; + int grid = (localZ + PADDING) * GRID + localX + PADDING; double raised = original; for (Field field : fields) { - int crest = Math.min(head - 1, field.crest()); + if (skips(field.crest(), grid, head)) { + continue; + } + int crest = Math.min(head - 1, field.crest().level()); if (crest <= original) { continue; } @@ -83,7 +90,52 @@ final class RiffleProfile { 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))); + int riffled = (int) Math.max(original, Math.min(head - 1L, Math.round(raised))); + return flowing[grid] ? plunge(cell, riffled, head, variation) : riffled; + } + + private void collect(int cell, TreeMap crests, TreeMap feet) { + int head = heads[cell]; + if (head == DRY) { + return; + } + boolean crest = false; + for (int side : SIDES) { + int neighbour = heads[cell + side]; + if (neighbour == DRY) { + continue; + } + crest |= neighbour <= head - 2; + if (flowing[cell] && neighbour - head >= PLUNGE_MIN_DROP) { + feet.computeIfAbsent(new Fall(head, neighbour), ignored -> new Seeds()).add(cell); + } + } + if (crest) { + crests.computeIfAbsent(new Crest(head - 1, features[cell], flowing[cell]), ignored -> new Seeds()).add(cell); + } + } + + private boolean skips(Crest crest, int grid, int head) { + if (crest.level() - (head - 1) >= PLUNGE_MIN_DROP - 1) { + return true; + } + return crest.level() > head - 1 && crest.flowing() && flowing[grid] && crest.feature() != features[grid]; + } + + private int plunge(int cell, int bed, int head, double variation) { + int carved = bed; + for (Plunge plunge : plunges) { + Fall fall = plunge.fall(); + int distance = plunge.distances()[cell]; + if (fall.foot() != head || distance >= 10D * fall.radius()) { + continue; + } + long depth = Math.clamp(Math.round(PLUNGE_RATIO * fall.drop() * (0.85D + 0.15D * variation)), PLUNGE_MIN_DEPTH, PLUNGE_MAX_DEPTH); + double u = distance / (10D * fall.radius()); + double bowl = 1D - u * u * (3D - 2D * u); + carved = (int) Math.min(carved, head - 1L - Math.round((depth - 1L) * bowl)); + } + return carved; } private static int[] remaining() { @@ -100,25 +152,98 @@ final class RiffleProfile { return remaining; } - private record Field(int crest, int[] distances) { + @FunctionalInterface + interface Water { + void sample(int x, int z, Sample sample); + } + + static final class Sample { + private int head; + private long feature; + private WaterRole role; + + void set(int head, long feature, WaterRole role) { + this.head = head; + this.feature = feature; + this.role = role; + } + } + + private record Crest(int level, long feature, boolean flowing) implements Comparable { + @Override + public int compareTo(Crest other) { + if (level != other.level) { + return Integer.compare(level, other.level); + } + if (feature != other.feature) { + return Long.compare(feature, other.feature); + } + return Boolean.compare(flowing, other.flowing); + } + } + + private record Fall(int foot, int top) implements Comparable { + int drop() { + return top - foot; + } + + double radius() { + return Math.clamp(PLUNGE_RADIUS_RATIO * drop(), PLUNGE_MIN_RADIUS, PLUNGE_MAX_RADIUS); + } + + @Override + public int compareTo(Fall other) { + return foot != other.foot ? Integer.compare(foot, other.foot) : Integer.compare(top, other.top); + } + } + + private record Field(Crest crest, int[] distances) { + } + + private static final class Seeds { + private int[] cells = new int[16]; + private int size; + + private void add(int cell) { + if (size == cells.length) { + cells = Arrays.copyOf(cells, 2 * size); + } + cells[size++] = cell; + } + } + + private record Plunge(Fall fall, int[] distances) { } private static final class DistanceField { + private static final int ANY_LEVEL = Integer.MAX_VALUE; + 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 int level; private DistanceField(int[] heads) { this.heads = heads; } - private void reset() { + private int[] measure(Seeds seeds, int passable) { + level = passable; Arrays.fill(distances, LIMIT); Arrays.fill(next, -1); Arrays.fill(previous, -1); Arrays.fill(first, -1); + for (int seed = 0; seed < seeds.size; seed++) { + offer(seeds.cells[seed], 0); + } + spread(); + return output(); + } + + private boolean blocked(int cell) { + return heads[cell] == DRY || level != ANY_LEVEL && heads[cell] != level; } private void offer(int cell, int distance) { @@ -166,10 +291,10 @@ final class RiffleProfile { } int neighbour = nz * GRID + nx; int candidate = distance + (dx != 0 && dz != 0 ? 14 : 10); - if (heads[neighbour] == DRY || candidate >= distances[neighbour]) { + if (blocked(neighbour) || candidate >= distances[neighbour]) { continue; } - if (dx != 0 && dz != 0 && (heads[z * GRID + nx] == DRY || heads[nz * GRID + x] == DRY)) { + if (dx != 0 && dz != 0 && (blocked(z * GRID + nx) || blocked(nz * GRID + x))) { continue; } offer(neighbour, candidate); diff --git a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/WaterRole.java b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/WaterRole.java index 4d758e1af..904e10bb3 100644 --- a/core/src/main/java/art/arcane/iris/generation/hydrology/raster/WaterRole.java +++ b/core/src/main/java/art/arcane/iris/generation/hydrology/raster/WaterRole.java @@ -29,4 +29,11 @@ public enum WaterRole { case NONE, BANK, SHORE, POND_SHORE, DAM -> false; }; } + + public boolean flowing() { + return switch (this) { + case CHANNEL, CASCADE, WATERFALL -> true; + case NONE, BANK, SHORE, POND, POND_SHORE, DAM, MOUTH, INLET -> false; + }; + } } 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 0e12a8134..1e887a69d 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 @@ -6,6 +6,7 @@ import art.arcane.iris.generation.hydrology.grid.HeightLattice; import art.arcane.iris.generation.hydrology.plan.HydrologySettings; import art.arcane.iris.generation.hydrology.plan.WindowKey; import art.arcane.iris.generation.hydrology.raster.LinkBlend; +import art.arcane.iris.generation.hydrology.raster.WaterHeads; import java.util.ArrayList; import java.util.Arrays; @@ -75,7 +76,7 @@ public final class ReachShaper { int[] order = upstreamFirst(drafts, chain); int[] levels = ReachGrader.grade(drafts, order, settings.seaLevel(), settings.deformation().maxFill(), chain); for (int index : order) { - finish(drafts, drafts.get(index)); + finish(drafts, drafts.get(index), settings.seaLevel()); } for (int index = 0; index < sources.length; index++) { if (leavesThroughALip(ponds, sources[index])) { @@ -85,7 +86,7 @@ public final class ReachShaper { boolean[] touched = ConfluenceGrader.level(window, drafts, order); for (int index = 0; index < touched.length; index++) { if (touched[index]) { - restyle(drafts.get(index)); + restyle(drafts.get(index), settings.seaLevel()); } } List linked = new ArrayList<>(ponds.size()); @@ -362,7 +363,7 @@ public final class ReachShaper { return result; } - private static void finish(List drafts, ReachDraft draft) { + private static void finish(List drafts, ReachDraft draft, int seaLevel) { draft.outletZone = new boolean[draft.xs.length]; float[] distance = distances(draft.xs, draft.zs); for (int point = 0; point < distance.length; point++) { @@ -380,11 +381,11 @@ public final class ReachShaper { feederEnd = Math.max(feederEnd, feeder.halfWidth[feeder.halfWidth.length - 1]); } widths(draft, upstream, feederEnd); - restyle(draft); + restyle(draft, seaLevel); } - private static void restyle(ReachDraft draft) { - draft.style = styles(draft.grade, draft.ocean, draft.fixed(), draft.tier.waterfallMinimumDrop()); + private static void restyle(ReachDraft draft, int seaLevel) { + draft.style = styles(draft.grade, draft.ocean, draft.fixed(), draft.tier.waterfallMinimumDrop(), seaLevel); } private static void chaikin(ReachDraft draft) { @@ -547,11 +548,15 @@ public final class ReachShaper { return Math.clamp(1f - remaining / length, 0f, 1f); } - public static ReachStyle[] styles(int[] grade, boolean[] ocean, boolean[] outletZone, int waterfallMinimumDrop) { + public static ReachStyle[] styles(int[] grade, boolean[] ocean, boolean[] outletZone, int waterfallMinimumDrop, int seaLevel) { ReachStyle[] styles = new ReachStyle[grade.length]; for (int point = 0; point < grade.length; point++) { int drop = point == 0 ? 0 : grade[point - 1] - grade[point]; styles[point] = style(drop, waterfallMinimumDrop, ocean[point], outletZone[point]); + if (drop == 1 && !ocean[point] && outletZone[point - 1] + && WaterHeads.snap(grade[point - 1], seaLevel) - WaterHeads.snap(grade[point], seaLevel) >= 2) { + styles[point] = ReachStyle.CASCADE; + } } for (int point = 1; point < grade.length; point++) { if (styles[point] == ReachStyle.MOUTH && styles[point - 1] != ReachStyle.MOUTH diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/ConfluenceIncisionTest.java b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/ConfluenceIncisionTest.java index 089dd013a..f83e5b08e 100644 --- a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/ConfluenceIncisionTest.java +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/ConfluenceIncisionTest.java @@ -6,6 +6,7 @@ import art.arcane.iris.generation.hydrology.water.Pond; import art.arcane.iris.generation.hydrology.water.PondPlanner; import art.arcane.iris.generation.hydrology.water.Reach; import art.arcane.iris.generation.hydrology.water.ReachShaper; +import org.junit.AfterClass; import org.junit.Test; import java.util.ArrayList; @@ -26,10 +27,16 @@ public class ConfluenceIncisionTest { private static final double WALL_FACE_REACH = 24D; private static final double MAX_WALL_CUT = 24D; private static final int MAX_COPY_DIFFERENCE = 4; + private static PlannedScene valley; + + @AfterClass + public static void dropScene() { + valley = null; + } @Test public void junctionPoolsNeverTrenchASlopingValley() { - PlannedScene scene = PlannedScene.plan(SyntheticTerrain.valleySlope(), settings(), 2, 2); + PlannedScene scene = valley(); int points = 0; int deep = 0; String worst = ""; @@ -52,6 +59,11 @@ public class ConfluenceIncisionTest { assertEquals("reach points cut more than " + DEEP_CUT + " below the valley floor, deepest " + worst, 0, deep); } + @Test + public void slopingValleyConfluencesHaveNoTongues() { + assertEquals("confluence tongues on the sloping valley over 2x2 windows", List.of(), WaterInvariants.confluenceTongues(valley())); + } + @Test public void majorDownAWallBesideTheValleyRiverFollowsTheWall() { SyntheticTerrain terrain = SyntheticTerrain.valleyBelowAWall(WALL_FOOT, WALL_RUN, WALL_HEIGHT); @@ -115,6 +127,13 @@ public class ConfluenceIncisionTest { assertTrue("grades of one reach differ by " + worst + " between the windows that plan it: " + worstCopy, worst <= MAX_COPY_DIFFERENCE); } + private static synchronized PlannedScene valley() { + if (valley == null) { + valley = PlannedScene.plan(SyntheticTerrain.valleySlope(), settings(), 2, 2); + } + return valley; + } + private static HydrologySettings settings() { return HydrologySettings.compile(new IrisHydrology(), SEED, SyntheticTerrain.SEA_LEVEL, SyntheticTerrain.MIN_Y, SyntheticTerrain.MAX_Y); } diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/PlannedSceneTest.java b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/PlannedSceneTest.java index c061137ee..8174313da 100644 --- a/core/src/test/java/art/arcane/iris/generation/hydrology/plan/PlannedSceneTest.java +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/plan/PlannedSceneTest.java @@ -40,6 +40,12 @@ public class PlannedSceneTest { WaterInvariants.detachedOutlets(openFractal())); } + @Test + public void confluencesHaveNoTongues() { + assertEquals("confluence tongues on openFractal(1) with rainfall " + RAINFALL + " over 2x2 windows", List.of(), + WaterInvariants.confluenceTongues(openFractal())); + } + @Test public void feedersAtALedInOutletsExitPoolBesideTheLeadIn() { PlannedScene scene = openFractal(); 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 4502c79f2..2130b4e49 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 @@ -275,6 +275,28 @@ public class ChunkWaterRasterTest { } } + @Test + public void fallsIntoAPondLeaveItsFloor() { + IntBinaryOperator natural = (x, z) -> 70 + ((x - 16) * (x - 16) + (z - 16) * (z - 16)) / 20; + Pond pond = pond(21L, PondKind.NATURAL, 75, circle(16, 16, 14, 4), 16, 16, 0); + HydrologyTier stream = tier("stream", 1.5f, 0.5f, 4, IrisBankStyle.SMOOTH); + Scene level = new Scene(settings(new IrisHydrology()), natural).pond(pond).reach(line(23L, stream, 0L, 16f, -48f, 16f, 6f, (x, z) -> 75)); + Scene falling = new Scene(settings(new IrisHydrology()), natural).pond(pond).reach(line(23L, stream, 0L, 16f, -48f, 16f, 6f, (x, z) -> 86)); + int checked = 0; + for (int x = -8; x < 40; x++) { + for (int z = -8; z < 40; z++) { + if (falling.role(x, z) != WaterRole.POND) { + continue; + } + checked++; + assertEquals("pond floor under the fall at " + x + "," + z, level.terrain(x, z), falling.terrain(x, z)); + } + } + assertTrue("pond columns " + checked, checked > 400); + assertEquals("the stream falls into the pond", 86, falling.head(16, -2)); + assertEquals(74, falling.head(16, 6)); + } + @Test public void pondColumnsShareOneLevel() { IntBinaryOperator natural = (x, z) -> 70 + ((x - 16) * (x - 16) + (z - 16) * (z - 16)) / 20; @@ -615,6 +637,26 @@ public class ChunkWaterRasterTest { } } + @Test + public void stepsAtTheEdgeOfALowerChannelAreStyledAsFalls() { + HydrologyTier river = tier("river", 8f, 1f, 16, IrisBankStyle.SMOOTH); + HydrologyTier stream = tier("stream", 2f, 0.5f, 4, IrisBankStyle.SMOOTH); + Reach lower = line(302L, river, 0L, -96f, 8f, 120f, 8f, (x, z) -> 66); + Map styles = Map.of(68, WaterRole.CASCADE, 70, WaterRole.CASCADE, 72, WaterRole.WATERFALL); + for (int level : new int[]{68, 70, 72}) { + Reach perched = line(303L, stream, 0L, 16f, -72f, 16f, 8f, (x, z) -> level); + Scene scene = new Scene(settings(new IrisHydrology()), (x, z) -> 80).reach(lower).reach(perched); + for (int x = 15; x <= 17; x++) { + assertEquals(level, scene.head(x, -1)); + assertEquals(66, scene.head(x, 0)); + assertEquals("the " + (level - 66) + "-block step onto the lower channel's edge at x " + x, styles.get(level), scene.role(x, 0)); + assertEquals("the perched channel above the step at x " + x, WaterRole.CHANNEL, scene.role(x, -1)); + } + assertEquals("the lower channel away from the step", WaterRole.CHANNEL, scene.role(40, 0)); + assertEquals("the lower channel inside the perched channel's footprint", WaterRole.CHANNEL, scene.role(16, 6)); + } + } + @Test public void crossWindowLinkNeverRaisesWaterDownstream() { HydrologyTier narrow = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH); @@ -845,6 +887,30 @@ public class ChunkWaterRasterTest { } } + @Test + public void crossWindowFedStartOneBlockAboveTheJunctionJoinsIt() { + HydrologyTier narrow = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH); + HydrologyTier wide = tier("river", 3f, 0.5f, 4, IrisBankStyle.SMOOTH); + Reach low = line(342L, wide, 0L, 504f, 200f, 600f, 200f, (x, z) -> 61); + Reach stub = line(341L, narrow, low.id(), 498f, 200f, 504f, 200f, (x, z) -> 62); + Reach upstream = line(340L, narrow, stub.id(), 440f, 200f, 498f, 200f, (x, z) -> 62); + assertEquals(new WindowKey(0, 0), WindowKey.containing(504, 200)); + assertEquals(new WindowKey(1, 0), WindowKey.containing(600, 200)); + Scene scene = new Scene(settings(new IrisHydrology()), (x, z) -> 90).reach(upstream).reach(stub).reach(low); + assertEquals("the stub's start one block above the junction joins it", 60, scene.head(498, 200)); + int previous = Integer.MAX_VALUE; + for (int x = 440; x <= 600; x++) { + int head = scene.head(x, 200); + assertNotEquals("centre line is water at x=" + x, Integer.MIN_VALUE, head); + assertTrue("water rises from " + previous + " to " + head + " at x=" + x, head <= previous); + if (head < previous && previous != Integer.MAX_VALUE) { + assertEquals("the step at x=" + x + " is styled", WaterRole.CASCADE, scene.role(x, 200)); + } + previous = head; + } + assertWindowOrderFree(scene, 27, 37, 12); + } + @Test public void crossWindowSettlingKeepsAnOutletsHeldLipAtThePondLevel() { HydrologyTier narrow = tier("river", 2f, 0.5f, 4, IrisBankStyle.SMOOTH); @@ -1119,6 +1185,9 @@ public class ChunkWaterRasterTest { if (scene.role(x, z) == WaterRole.MOUTH) { mouths++; assertEquals(banks + "mouth at sea level at " + x + "," + z, SEA_LEVEL, scene.head(x, z)); + if (x >= 40) { + assertEquals(banks + "the fall leaves the sea floor under the mouth at " + x + "," + z, coast.applyAsInt(x, z), scene.terrain(x, z)); + } } if (scene.water(x, z)) { continue; diff --git a/core/src/test/java/art/arcane/iris/generation/hydrology/raster/RiffleProfileTest.java b/core/src/test/java/art/arcane/iris/generation/hydrology/raster/RiffleProfileTest.java index 1cf73cfa6..46efd3162 100644 --- a/core/src/test/java/art/arcane/iris/generation/hydrology/raster/RiffleProfileTest.java +++ b/core/src/test/java/art/arcane/iris/generation/hydrology/raster/RiffleProfileTest.java @@ -67,6 +67,99 @@ public class RiffleProfileTest { } } + @Test + public void fallsKeepADeepPlungePoolBelowTheCurtain() { + Surface surface = new Surface((x, z) -> x < 0 ? 110 : 98); + for (int z = -8; z <= 8; z++) { + for (int original : new int[]{92, 96}) { + String bed = original + " bed at z " + z; + assertEquals("lip sill for the " + bed, 109, surface.bed(-1, z, original)); + assertTrue("plunge pool at the foot for the " + bed, surface.bed(0, z, original) <= 95); + assertTrue("deepest at the foot for the " + bed, surface.bed(0, z, original) <= surface.bed(6, z, original)); + for (int x = 0; x <= 20; x++) { + int here = surface.bed(x, z, original); + assertTrue("pool at " + x + " stays below the " + bed, here <= original); + assertTrue("downstream face at " + x + " in the " + bed, Math.abs(here - surface.bed(x + 1, z, original)) <= 2); + } + assertEquals(original, surface.bed(17, z, original)); + } + assertTrue("the bowl cuts below a shallow bed at z " + z, surface.bed(0, z, 96) <= 94); + } + } + + @Test + public void plungeFieldsMatchAcrossChunkBorders() { + IntBinaryOperator heads = (x, z) -> x + z < -7 ? 110 : 98; + Surface surface = new Surface(heads); + for (int x = -24; x <= 16; x++) { + assertTrue("the foot at " + x + "," + (-7 - x) + " is carved", surface.bed(x, -7 - x, 96) < 96); + } + for (int chunkX = -2; chunkX <= 1; chunkX++) { + for (int chunkZ = -2; chunkZ <= 1; chunkZ++) { + RiffleProfile first = new RiffleProfile(chunkX, chunkZ, channel(heads)); + RiffleProfile east = new RiffleProfile(chunkX + 1, chunkZ, channel(heads)); + RiffleProfile south = new RiffleProfile(chunkX, chunkZ + 1, channel(heads)); + for (int edge = 0; edge < 16; edge++) { + for (int original : new int[]{92, 96}) { + int x = (chunkX + 1) << 4; + int z = (chunkZ << 4) + edge; + double variation = BedProfile.variation(1337L, x, z); + assertEquals(first.bed(16, edge, original, heads.applyAsInt(x, z), variation), + east.bed(0, edge, original, heads.applyAsInt(x, z), variation)); + x = (chunkX << 4) + edge; + z = (chunkZ + 1) << 4; + variation = BedProfile.variation(1337L, x, z); + assertEquals(first.bed(edge, 16, original, heads.applyAsInt(x, z), variation), + south.bed(edge, 0, original, heads.applyAsInt(x, z), variation)); + } + } + } + } + } + + @Test + public void plungeSkipsStandingWaterAndMouths() { + IntBinaryOperator heads = (x, z) -> x < 0 ? 110 : 98; + IntBinaryOperator features = (x, z) -> x < 0 ? 1 : 2; + for (WaterRole standing : new WaterRole[]{WaterRole.POND, WaterRole.MOUTH, WaterRole.INLET}) { + Surface surface = new Surface(heads, water(heads, features, (x, z) -> x < 0 ? WaterRole.CHANNEL : standing)); + for (int z = -8; z <= 8; z++) { + assertEquals(standing + " lip sill at z " + z, 109, surface.bed(-1, z, 92)); + for (int x = 0; x <= 20; x++) { + assertEquals(standing + " floor at " + x + "," + z, 92, surface.bed(x, z, 92)); + assertEquals(standing + " shallow floor at " + x + "," + z, 96, surface.bed(x, z, 96)); + } + } + } + Surface beside = new Surface(heads, water(heads, (x, z) -> x < 0 ? 1 : z < 0 ? 2 : 3, + (x, z) -> x >= 0 && z >= 0 ? WaterRole.POND : WaterRole.CHANNEL)); + for (int z = -8; z <= 8; z++) { + for (int x = 0; x <= 20; x++) { + if (z >= 0) { + assertEquals("pond floor beside a carved channel foot at " + x + "," + z, 96, beside.bed(x, z, 96)); + } + } + if (z < 0) { + assertTrue("channel foot beside the pond at z " + z, beside.bed(0, z, 96) < 96); + } + } + } + + @Test + public void receivingChannelKeepsItsBedBelowAJoiningCascade() { + IntBinaryOperator heads = (x, z) -> x < 0 ? 104 : 100; + Surface joining = new Surface(heads, water(heads, (x, z) -> x < 0 ? 2 : 1, (x, z) -> WaterRole.CHANNEL)); + for (int z = -8; z <= 8; z++) { + assertEquals("the joining channel keeps its lip sill at z " + z, 103, joining.bed(-1, z, 96)); + assertTrue("the joining channel's approach rises toward its lip at z " + z, joining.bed(-6, z, 96) > 96); + for (int x = 0; x <= 20; x++) { + assertEquals("receiving bed at " + x + "," + z, 96, joining.bed(x, z, 96)); + } + } + Surface outlet = new Surface(heads, water(heads, (x, z) -> x < 0 ? 2 : 1, (x, z) -> x < 0 ? WaterRole.POND : WaterRole.CHANNEL)); + assertEquals("a channel below a pond's shore still rises toward the lip", 99, outlet.bed(0, 0, 96)); + } + @Test public void nearbyDisconnectedPoolsKeepTheirDepth() { Surface surface = new Surface((x, z) -> z == 0 ? DRY : z > 0 ? 100 : x < 0 ? 100 : 98); @@ -97,9 +190,9 @@ public class RiffleProfileTest { IntBinaryOperator heads = (x, z) -> x + z < -7 ? 100 : 98; for (int chunkX = -2; chunkX <= 1; chunkX++) { for (int chunkZ = -2; chunkZ <= 1; chunkZ++) { - RiffleProfile first = new RiffleProfile(chunkX, chunkZ, heads); - RiffleProfile east = new RiffleProfile(chunkX + 1, chunkZ, heads); - RiffleProfile south = new RiffleProfile(chunkX, chunkZ + 1, heads); + RiffleProfile first = new RiffleProfile(chunkX, chunkZ, channel(heads)); + RiffleProfile east = new RiffleProfile(chunkX + 1, chunkZ, channel(heads)); + RiffleProfile south = new RiffleProfile(chunkX, chunkZ + 1, channel(heads)); for (int edge = 0; edge < 16; edge++) { int x = (chunkX + 1) << 4; int z = (chunkZ << 4) + edge; @@ -116,19 +209,40 @@ public class RiffleProfileTest { } } + private static RiffleProfile.Water channel(IntBinaryOperator heads) { + return water(heads, (x, z) -> 1, (x, z) -> WaterRole.CHANNEL); + } + + private static RiffleProfile.Water water(IntBinaryOperator heads, IntBinaryOperator features, Roles roles) { + return (x, z, sample) -> { + int head = heads.applyAsInt(x, z); + sample.set(head, features.applyAsInt(x, z), head == DRY ? WaterRole.NONE : roles.at(x, z)); + }; + } + + private interface Roles { + WaterRole at(int x, int z); + } + private static final class Surface { private final IntBinaryOperator heads; + private final RiffleProfile.Water water; private final Map chunks = new HashMap<>(); private Surface(IntBinaryOperator heads) { + this(heads, channel(heads)); + } + + private Surface(IntBinaryOperator heads, RiffleProfile.Water water) { this.heads = heads; + this.water = water; } private int bed(int x, int z, int original) { int cx = x >> 4; int cz = z >> 4; long key = ((long) cx << 32) | (cz & 0xFFFFFFFFL); - RiffleProfile profile = chunks.computeIfAbsent(key, ignored -> new RiffleProfile(cx, cz, heads)); + RiffleProfile profile = chunks.computeIfAbsent(key, ignored -> new RiffleProfile(cx, cz, water)); return profile.bed(x & 15, z & 15, original, heads.applyAsInt(x, z), BedProfile.variation(1337L, x, z)); } } 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 ae2dd6801..234d59af7 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 @@ -1581,10 +1581,30 @@ public class ReachShaperTest { int[] grade = {62, 62, 62, 40, 40}; boolean[] ocean = {false, false, false, true, true}; - ReachStyle[] styles = ReachShaper.styles(grade, ocean, new boolean[grade.length], 6); + ReachStyle[] styles = ReachShaper.styles(grade, ocean, new boolean[grade.length], 6, SEA_LEVEL); assertEquals(List.of(ReachStyle.RUN, ReachStyle.RUN, ReachStyle.WATERFALL, ReachStyle.MOUTH, ReachStyle.MOUTH), List.of(styles)); } + @Test + public void heldLipStepThatShowsAsTwoBlocksIsACascade() { + int[] grade = {76, 76, 76, 75, 75}; + boolean[] ocean = new boolean[grade.length]; + boolean[] held = {true, true, true, false, false}; + assertEquals("a one-block step off a held lip that the even head snap shows as two blocks", + List.of(ReachStyle.POND_OUTLET, ReachStyle.POND_OUTLET, ReachStyle.POND_OUTLET, ReachStyle.CASCADE, ReachStyle.RUN), + List.of(ReachShaper.styles(grade, ocean, held, 6, SEA_LEVEL))); + assertEquals("the same step hidden by the head snap", ReachStyle.RIFFLE, ReachShaper.styles(grade, ocean, held, 6, SEA_LEVEL + 1)[3]); + assertEquals("a one-block step that leaves no held lip", ReachStyle.RIFFLE, + ReachShaper.styles(grade, ocean, new boolean[grade.length], 6, SEA_LEVEL)[3]); + int[] zoned = {78, 78, 77, 77, 77}; + boolean[] zone = {true, true, true, true, false}; + assertEquals("a one-block step inside an outlet zone that the head snap shows as two blocks", + List.of(ReachStyle.POND_OUTLET, ReachStyle.POND_OUTLET, ReachStyle.CASCADE, ReachStyle.POND_OUTLET, ReachStyle.RUN), + List.of(ReachShaper.styles(zoned, ocean, zone, 6, SEA_LEVEL))); + assertEquals("the same step inside the zone hidden by the head snap", ReachStyle.POND_OUTLET, + ReachShaper.styles(zoned, ocean, zone, 6, SEA_LEVEL + 1)[2]); + } + @Test public void channelWaterStaysBelowNaturalGroundOnSmoothValleys() { HydrologySettings settings = settings(config -> {