Window-independent inland hollows

- InlandHollows classifies below-sea land from the shared height lattice, following each hollow past the planning area, so every window that sees a hollow treats it the same way
- A hollow is inland only when it is ringed by land, touches no ocean and stays under 16384 cells; anything else, or a hollow held by a region lake, is left open
- The basin draining into a hollow is flooded in a box set by the hollow itself, not by the planning area
- Burned trunks are never dragged to sea level through ground more than deformation.maxFill above the sea; the trunk link is cut there and the trunk restarts at its own height
- BasinExtractor loses the window-local inland and draining scans
- Tests: hollow cut by a grid edge, lowland open to the sea, hollow held by a lake, trunk floor on high ground, and a hollow on a window line planned alike by both neighbouring windows
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-02 03:25:53 -05:00
1 parent c0f2c10c1d
commit 0a082f76ee
9 files changed
+533 -99

No files matched your search

@@ -69,46 +69,6 @@ public final class BasinExtractor {
return basins;
}
public static BitSet inland(DemGrid natural, boolean[] ocean, int seaLevel) {
float[] heights = natural.values();
int count = heights.length;
if (ocean.length < count) {
throw new IllegalArgumentException("Ocean flags cover " + ocean.length + " cells, grid has " + count);
}
BitSet inland = new BitSet(count);
boolean[] visited = new boolean[count];
int[] queue = new int[count];
for (int start = 0; start < count; start++) {
if (visited[start] || !belowSea(heights, ocean, start, seaLevel)) {
continue;
}
int tail = component(natural, cell -> belowSea(heights, ocean, cell, seaLevel), visited, queue, start);
if (enclosed(natural, ocean, queue, tail)) {
for (int index = 0; index < tail; index++) {
inland.set(queue[index]);
}
}
}
return inland;
}
public static BitSet draining(DemGrid natural, float[] filled, BitSet inland, float minDepth) {
float[] heights = natural.values();
BitSet basins = new BitSet(heights.length);
boolean[] visited = new boolean[heights.length];
int[] queue = new int[heights.length];
for (int start = inland.nextSetBit(0); start >= 0; start = inland.nextSetBit(start + 1)) {
if (visited[start]) {
continue;
}
int tail = component(natural, cell -> inland.get(cell) || filled[cell] - heights[cell] > minDepth, visited, queue, start);
for (int index = 0; index < tail; index++) {
basins.set(queue[index]);
}
}
return basins;
}
public static BitSet hollow(DemGrid natural, BitSet inland, int cell) {
BitSet hollow = new BitSet(natural.values().length);
if (!inland.get(cell)) {
@@ -157,31 +117,6 @@ public final class BasinExtractor {
return true;
}
private static boolean belowSea(float[] heights, boolean[] ocean, int cell, int seaLevel) {
return !ocean[cell] && Math.round(heights[cell]) < seaLevel;
}
private static boolean enclosed(DemGrid grid, boolean[] ocean, int[] cells, int count) {
int width = grid.width();
int depth = grid.depth();
for (int index = 0; index < count; index++) {
int cell = cells[index];
int i = cell % width;
int j = cell / width;
if (i == 0 || j == 0 || i == width - 1 || j == depth - 1) {
return false;
}
for (int dz = -1; dz <= 1; dz++) {
for (int dx = -1; dx <= 1; dx++) {
if (ocean[(j + dz) * width + i + dx]) {
return false;
}
}
}
}
return true;
}
private static int component(DemGrid grid, IntPredicate member, boolean[] visited, int[] queue, int start) {
int width = grid.width();
int depth = grid.depth();
@@ -13,22 +13,24 @@ import java.util.Map;
final class BurnedTrunks {
private final BitSet cells;
private final Land land;
private final int[] next;
private final float[] catchment;
private final float epsilon;
private int[] order;
private BitSet entries;
private BurnedTrunks(int count, float epsilon) {
private BurnedTrunks(int count, float epsilon, Land land) {
this.cells = new BitSet(count);
this.land = land;
this.next = new int[count];
this.catchment = new float[count];
this.epsilon = epsilon;
Arrays.fill(next, -1);
}
static BurnedTrunks burn(DemGrid raw, List<Reach> reaches, float depth, float epsilon, BitSet hollows) {
BurnedTrunks trunks = new BurnedTrunks(raw.values().length, epsilon);
static BurnedTrunks burn(DemGrid raw, List<Reach> reaches, float depth, float epsilon, Land land) {
BurnedTrunks trunks = new BurnedTrunks(raw.values().length, epsilon, land);
Map<Long, Reach> byId = new HashMap<>();
for (Reach reach : reaches) {
byId.putIfAbsent(reach.id(), reach);
@@ -36,13 +38,14 @@ final class BurnedTrunks {
List<int[]> paths = new ArrayList<>();
for (Reach reach : reaches) {
for (int[] route : TrunkRouter.route(raw, reach, byId.get(reach.downstreamId()))) {
for (int[] path : outside(route, hollows)) {
for (int[] path : outside(route, land.hollows())) {
trunks.claim(path, reach.catchment());
paths.add(path);
}
}
}
StreamBurner.burn(raw, paths, depth);
trunks.keepAboveTheSea(raw.values());
trunks.sort();
trunks.descend(raw.values());
return trunks;
@@ -59,9 +62,18 @@ final class BurnedTrunks {
void descend(float[] heights) {
for (int cell : order) {
int successor = next[cell];
if (successor >= 0) {
heights[successor] = Math.min(heights[successor], Math.min(heights[cell] - epsilon, Math.nextDown(heights[cell])));
if (successor < 0) {
continue;
}
float lowered = Math.min(heights[successor], Math.min(heights[cell] - epsilon, Math.nextDown(heights[cell])));
if (lowered < heights[successor] && land.high(successor) && lowered <= land.seaLevel()) {
next[cell] = -1;
if (!linked(successor)) {
entries.set(successor);
}
continue;
}
heights[successor] = lowered;
}
}
@@ -85,6 +97,23 @@ final class BurnedTrunks {
}
}
private boolean linked(int target) {
for (int cell = cells.nextSetBit(0); cell >= 0; cell = cells.nextSetBit(cell + 1)) {
if (next[cell] == target) {
return true;
}
}
return false;
}
private void keepAboveTheSea(float[] heights) {
for (int cell = cells.nextSetBit(0); cell >= 0; cell = cells.nextSetBit(cell + 1)) {
if (land.high(cell)) {
heights[cell] = Math.max(heights[cell], land.seaLevel() + 1f);
}
}
}
private static List<int[]> outside(int[] route, BitSet hollows) {
List<int[]> runs = new ArrayList<>(1);
int from = 0;
@@ -153,4 +182,10 @@ final class BurnedTrunks {
}
return Arrays.copyOf(sorted, tail);
}
record Land(float[] natural, boolean[] ocean, int seaLevel, int highGround, BitSet hollows) {
boolean high(int cell) {
return !ocean[cell] && natural[cell] > highGround;
}
}
}
@@ -0,0 +1,256 @@
package art.arcane.iris.generation.hydrology.plan;
import art.arcane.iris.generation.hydrology.grid.DemGrid;
import art.arcane.iris.generation.hydrology.grid.HeightLattice;
import art.arcane.iris.generation.hydrology.grid.HydrologySampleBuffer;
import art.arcane.iris.generation.hydrology.grid.PriorityFlood;
import art.arcane.iris.generation.hydrology.water.Pond;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
final class InlandHollows {
private static final int MAX_HOLLOW_CELLS = 1 << 14;
private static final int BASIN_MARGIN = 64;
private static final int[] AROUND_X = {-1, 0, 1, -1, 1, -1, 0, 1};
private static final int[] AROUND_Z = {-1, -1, -1, 0, 0, 1, 1, 1};
private final HeightLattice fine;
private final DemGrid natural;
private final boolean[] ocean;
private final int seaLevel;
private final int stride;
private final int originX;
private final int originZ;
private final Map<Long, Float> heights = new HashMap<>();
private final Set<Long> oceans = new HashSet<>();
private InlandHollows(HeightLattice fine, DemGrid natural, boolean[] ocean, int seaLevel) {
this.fine = fine;
this.natural = natural;
this.ocean = ocean;
this.seaLevel = seaLevel;
this.stride = natural.cell() / fine.cell();
this.originX = Math.floorDiv(natural.originX(), natural.cell());
this.originZ = Math.floorDiv(natural.originZ(), natural.cell());
}
static Hollows classify(HeightLattice fine, DemGrid natural, boolean[] ocean, int seaLevel, float minDepth, float epsilon,
List<Pond> lakes) {
return new InlandHollows(fine, natural, ocean, seaLevel).classify(minDepth, epsilon, lakes);
}
private Hollows classify(float minDepth, float epsilon, List<Pond> lakes) {
int count = natural.values().length;
BitSet inland = new BitSet(count);
BitSet basins = new BitSet(count);
boolean[] seen = new boolean[count];
for (int start = 0; start < count; start++) {
if (seen[start] || !belowSea(natural.values()[start], ocean[start])) {
continue;
}
List<Long> local = localComponent(start, seen);
if (underLake(local, lakes)) {
continue;
}
Set<Long> hollow = enclosed(local);
if (hollow == null) {
continue;
}
mark(hollow, inland);
mark(basin(hollow, minDepth, epsilon), basins);
}
return new Hollows(inland, basins);
}
private List<Long> localComponent(int start, boolean[] seen) {
int width = natural.width();
int depth = natural.depth();
List<Long> cells = new ArrayList<>();
ArrayDeque<Integer> queue = new ArrayDeque<>();
queue.add(start);
seen[start] = true;
while (!queue.isEmpty()) {
int cell = queue.poll();
cells.add(key(originX + cell % width, originZ + cell / width));
for (int around = 0; around < AROUND_X.length; around++) {
int i = cell % width + AROUND_X[around];
int j = cell / width + AROUND_Z[around];
int next = j * width + i;
if (i >= 0 && j >= 0 && i < width && j < depth && !seen[next] && belowSea(natural.values()[next], ocean[next])) {
seen[next] = true;
queue.add(next);
}
}
}
return cells;
}
private Set<Long> enclosed(List<Long> local) {
Set<Long> members = new HashSet<>(local);
Set<Long> checked = new HashSet<>(local);
List<Long> frontier = new ArrayList<>(local);
while (!frontier.isEmpty()) {
List<Long> around = new ArrayList<>();
for (long cell : frontier) {
for (int index = 0; index < AROUND_X.length; index++) {
long next = key(x(cell) + AROUND_X[index], z(cell) + AROUND_Z[index]);
if (checked.add(next)) {
around.add(next);
}
}
}
sample(around);
frontier = new ArrayList<>();
for (long cell : around) {
if (oceans.contains(cell)) {
return null;
}
if (belowSea(heights.get(cell), false)) {
members.add(cell);
frontier.add(cell);
}
}
if (members.size() > MAX_HOLLOW_CELLS) {
return null;
}
}
return members;
}
private Set<Long> basin(Set<Long> hollow, float minDepth, float epsilon) {
int minX = Integer.MAX_VALUE;
int minZ = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int maxZ = Integer.MIN_VALUE;
for (long cell : hollow) {
minX = Math.min(minX, x(cell));
minZ = Math.min(minZ, z(cell));
maxX = Math.max(maxX, x(cell));
maxZ = Math.max(maxZ, z(cell));
}
int boxX = minX - BASIN_MARGIN;
int boxZ = minZ - BASIN_MARGIN;
int width = maxX - minX + 1 + 2 * BASIN_MARGIN;
int depth = maxZ - minZ + 1 + 2 * BASIN_MARGIN;
HydrologySampleBuffer samples = new HydrologySampleBuffer(width * depth);
fine.fill(boxX * stride, boxZ * stride, width, depth, stride, samples);
DemGrid box = new DemGrid(boxX * natural.cell(), boxZ * natural.cell(), natural.cell(), width, depth);
System.arraycopy(samples.height(), 0, box.values(), 0, width * depth);
BitSet seeds = new BitSet(width * depth);
for (int cell = 0; cell < width * depth; cell++) {
int i = cell % width;
int j = cell / width;
if (samples.ocean()[cell] || i == 0 || j == 0 || i == width - 1 || j == depth - 1) {
seeds.set(cell);
}
}
float[] filled = PriorityFlood.fill(box, seeds, epsilon);
float[] values = box.values();
Set<Long> basin = new HashSet<>(hollow);
ArrayDeque<Integer> queue = new ArrayDeque<>();
boolean[] seen = new boolean[width * depth];
for (long cell : hollow) {
int index = (z(cell) - boxZ) * width + x(cell) - boxX;
seen[index] = true;
queue.add(index);
}
while (!queue.isEmpty()) {
int cell = queue.poll();
for (int around = 0; around < AROUND_X.length; around++) {
int i = cell % width + AROUND_X[around];
int j = cell / width + AROUND_Z[around];
int next = j * width + i;
if (i >= 0 && j >= 0 && i < width && j < depth && !seen[next] && filled[next] - values[next] > minDepth) {
seen[next] = true;
queue.add(next);
basin.add(key(boxX + i, boxZ + j));
}
}
}
return basin;
}
private void sample(List<Long> cells) {
List<Long> missing = new ArrayList<>();
for (long cell : cells) {
int index = local(cell);
if (index >= 0) {
heights.put(cell, natural.values()[index]);
if (ocean[index]) {
oceans.add(cell);
}
} else if (!heights.containsKey(cell)) {
missing.add(cell);
}
}
if (missing.isEmpty()) {
return;
}
int[] nodeX = new int[missing.size()];
int[] nodeZ = new int[missing.size()];
for (int index = 0; index < nodeX.length; index++) {
nodeX[index] = x(missing.get(index)) * stride;
nodeZ[index] = z(missing.get(index)) * stride;
}
HydrologySampleBuffer samples = new HydrologySampleBuffer(nodeX.length);
fine.fill(nodeX, nodeZ, samples);
for (int index = 0; index < nodeX.length; index++) {
heights.put(missing.get(index), samples.height()[index]);
if (samples.ocean()[index]) {
oceans.add(missing.get(index));
}
}
}
private boolean underLake(List<Long> cells, List<Pond> lakes) {
for (Pond lake : lakes) {
for (long cell : cells) {
if (lake.mask().grownContains(x(cell) * natural.cell(), z(cell) * natural.cell())) {
return true;
}
}
}
return false;
}
private void mark(Set<Long> cells, BitSet out) {
for (long cell : cells) {
int index = local(cell);
if (index >= 0) {
out.set(index);
}
}
}
private int local(long cell) {
int i = x(cell) - originX;
int j = z(cell) - originZ;
return i < 0 || j < 0 || i >= natural.width() || j >= natural.depth() ? -1 : j * natural.width() + i;
}
private boolean belowSea(float height, boolean sea) {
return !sea && Math.round(height) < seaLevel;
}
private static long key(int x, int z) {
return ((long) x << 32) | (z & 0xFFFFFFFFL);
}
private static int x(long key) {
return (int) (key >> 32);
}
private static int z(long key) {
return (int) key;
}
record Hollows(BitSet inland, BitSet basins) {
}
}
@@ -89,15 +89,15 @@ public final class WindowPlanner {
System.arraycopy(samples.height(), 0, natural.values(), 0, SPAN * SPAN);
DemGrid raw = new DemGrid(natural.originX(), natural.originZ(), CELL, SPAN, SPAN);
System.arraycopy(natural.values(), 0, raw.values(), 0, SPAN * SPAN);
BitSet inland = BasinExtractor.inland(natural, samples.ocean(), settings.seaLevel());
BitSet open = open(samples.ocean());
BitSet hollowBasins = inland.isEmpty() ? inland
: BasinExtractor.draining(natural, PriorityFlood.fill(natural, open, settings.drainage().fillEpsilon()), inland, BASIN_DEPTH);
List<Pond> lakes = regionLakes(plans.values());
InlandHollows.Hollows hollows = InlandHollows.classify(fine, natural, samples.ocean(), settings.seaLevel(), BASIN_DEPTH,
settings.drainage().fillEpsilon(), lakes);
BitSet inland = hollows.inland();
BurnedTrunks trunks = BurnedTrunks.burn(raw, majorReaches(key, plans), settings.majorTier().inset(), settings.drainage().fillEpsilon(),
hollowBasins);
new BurnedTrunks.Land(natural.values(), samples.ocean(), settings.seaLevel(), settings.seaLevel() + settings.deformation().maxFill(),
hollows.basins()));
BitSet seeds = trunks.cells();
seeds.or(open);
seeds.or(open(samples.ocean()));
DemGrid preliminary = new DemGrid(raw.originX(), raw.originZ(), CELL, SPAN, SPAN);
System.arraycopy(raw.values(), 0, preliminary.values(), 0, SPAN * SPAN);
DrainContext context = new DrainContext(samples, trunks, inland);