Hashed D8 ties

- Break equal-slope D8 ties with a world-keyed hash of seed and cell
- Pass the settings seed from window, region and pond planners
- Replace the scan-order test with world-hash tie tests
This commit is contained in:
Brian Fopiano committed 2026-11-03 11:58:41 -05:00
1 parent 52450fbc72
commit 750bade003
9 files changed
+128 -17

No files matched your search

@@ -2,23 +2,28 @@ package art.arcane.iris.generation.hydrology.grid;
public final class D8Router {
private static final float DIAGONAL = (float) (1.0D / Math.sqrt(2.0D));
private static final long SALT_TIE = 0x4438544945L;
private D8Router() {
}
public static int[] receivers(DemGrid grid, float[] filled) {
public static int[] receivers(DemGrid grid, float[] filled, long seed) {
int count = grid.width() * grid.depth();
if (filled.length != count) {
throw new IllegalArgumentException("Filled surface has " + filled.length + " cells, grid has " + count);
}
int[] receivers = new int[count];
for (int cell = 0; cell < count; cell++) {
receivers[cell] = receiver(grid, filled, cell);
receivers[cell] = steepest(grid, filled, cell, true, seed);
}
return receivers;
}
static int receiver(DemGrid grid, float[] filled, int cell) {
return steepest(grid, filled, cell, false, 0L);
}
private static int steepest(DemGrid grid, float[] filled, int cell, boolean hashTies, long seed) {
int width = grid.width();
int depth = grid.depth();
int i = cell % width;
@@ -26,6 +31,7 @@ public final class D8Router {
float level = filled[cell];
int best = -1;
float bestSlope = 0f;
int bestDirection = -1;
for (int dz = -1; dz <= 1; dz++) {
int nj = j + dz;
if (nj < 0 || nj >= depth) {
@@ -42,12 +48,20 @@ public final class D8Router {
continue;
}
float slope = dx != 0 && dz != 0 ? drop * DIAGONAL : drop;
if (best < 0 || slope > bestSlope) {
int direction = (dz + 1) * 3 + dx + 1;
boolean wins = best < 0 || slope > bestSlope
|| hashTies && slope == bestSlope && tie(grid, cell, seed, direction) < tie(grid, cell, seed, bestDirection);
if (wins) {
best = neighbour;
bestSlope = slope;
bestDirection = direction;
}
}
}
return best;
}
private static long tie(DemGrid grid, int cell, long seed, int direction) {
return CellHash.mix(seed, SALT_TIE + direction, grid.blockX(cell), grid.blockZ(cell));
}
}
@@ -118,7 +118,7 @@ public final class RegionPlanner {
private List<Reach> majorReaches(DemGrid raw, float[] filled, HydrologySampleBuffer samples) {
DemGrid surface = new DemGrid(raw.originX(), raw.originZ(), CELL, SPAN, SPAN);
System.arraycopy(filled, 0, surface.values(), 0, filled.length);
int[] receivers = D8Router.receivers(surface, filled);
int[] receivers = D8Router.receivers(surface, filled, settings.seed());
float[] weights = new float[filled.length];
for (int cell = 0; cell < filled.length; cell++) {
if (samples.ocean()[cell]) {
@@ -212,7 +212,7 @@ public final class WindowPlanner {
}
DemGrid surface = new DemGrid(raw.originX(), raw.originZ(), CELL, SPAN, SPAN);
System.arraycopy(filled, 0, surface.values(), 0, filled.length);
int[] receivers = D8Router.receivers(surface, filled);
int[] receivers = D8Router.receivers(surface, filled, settings.seed());
trunks.route(receivers);
float[] weights = new float[filled.length];
for (int cell = 0; cell < filled.length; cell++) {
@@ -447,7 +447,7 @@ public final class PondPlanner {
if (order.size() < 2) {
return;
}
int[] receivers = D8Router.receivers(raw, filled);
int[] receivers = D8Router.receivers(raw, filled, settings.seed());
order.sort(Comparator.naturalOrder());
for (long key : order) {
int index = (int) key;