mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 08:14:45 +00:00
Agree sea inlets across windows
- Pass the planning window to SeaInletPlanner from WindowPlanner - Mark a site clipped when its candidate area or any trace, cover, mouth or chamber lookup reaches the span edge - Keep clipped inlets out of separation and the planned list unless the planning window owns them - Avoid only reaches owned by the planning window or its 8 neighbours - Add InletTerrain span views and an open-coast 3x3 window agreement test
This commit is contained in:
1 parent
9c95e7901c
commit
21ed677bf4
4 files changed
+187
-35
No files matched your search
@@ -118,7 +118,7 @@ public final class WindowPlanner {
|
||||
}
|
||||
List<Reach> reaches = network.reaches();
|
||||
List<Pond> windowPonds = windowPonds(network.ponds());
|
||||
List<SeaInlet> planned = inlets.plan(raw, samples, drainage.receivers(), drainage.accumulation(), network, settings.seed());
|
||||
List<SeaInlet> planned = inlets.plan(key, raw, samples, drainage.receivers(), drainage.accumulation(), network, settings.seed());
|
||||
WindowPlan owned = new WindowPlan(key, ownedReaches(key, reaches), ownedPonds(key, windowPonds), ownedInlets(key, planned));
|
||||
return new WindowComputation(owned, reaches, windowPonds);
|
||||
}
|
||||
|
||||
+93
-23
@@ -41,8 +41,9 @@ public final class SeaInletPlanner {
|
||||
this.settings = Objects.requireNonNull(settings, "settings");
|
||||
}
|
||||
|
||||
public List<SeaInlet> plan(DemGrid raw, HydrologySampleBuffer samples, int[] receivers, float[] accumulation, ShapedNetwork network,
|
||||
long seed) {
|
||||
public List<SeaInlet> plan(WindowKey window, DemGrid raw, HydrologySampleBuffer samples, int[] receivers, float[] accumulation,
|
||||
ShapedNetwork network, long seed) {
|
||||
Objects.requireNonNull(window, "window");
|
||||
HydrologySettings.SeaInlets config = settings.underground().seaInlets();
|
||||
if (!config.enabled() || settings.density() == 0f) {
|
||||
return List.of();
|
||||
@@ -52,11 +53,11 @@ public final class SeaInletPlanner {
|
||||
throw new IllegalArgumentException("Sea inlet planning needs " + count + " cells, got samples=" + samples.height().length
|
||||
+ " receivers=" + receivers.length + " accumulation=" + accumulation.length);
|
||||
}
|
||||
Terrain terrain = new Terrain(raw, samples, receivers, accumulation, cover(raw, samples.height(), network), seed);
|
||||
Terrain terrain = new Terrain(raw, samples, receivers, accumulation, cover(window, raw, samples.height(), network), seed);
|
||||
List<SeaInlet> inlets = new ArrayList<>();
|
||||
for (Map.Entry<Long, List<Integer>> site : sites(terrain, config).entrySet()) {
|
||||
SeaInlet inlet = site(terrain, config, site.getKey(), site.getValue());
|
||||
if (inlet != null) {
|
||||
if (inlet != null && (!terrain.clipped || window.equals(owner(inlet)))) {
|
||||
inlets.add(inlet);
|
||||
}
|
||||
}
|
||||
@@ -110,6 +111,7 @@ public final class SeaInletPlanner {
|
||||
double targetZ = (siteZ + CellHash.unit(CellHash.mix(terrain.seed, SALT_SITE_Z, siteX, siteZ))) * config.spacing();
|
||||
DemGrid grid = terrain.grid;
|
||||
WindowKey owner = WindowKey.containing((int) Math.floor(targetX), (int) Math.floor(targetZ));
|
||||
terrain.clipped = reachesEdge(terrain, config, siteX, siteZ, owner);
|
||||
List<Integer> ordered = new ArrayList<>(candidates.size());
|
||||
for (int candidate : candidates) {
|
||||
if (owner.equals(WindowKey.containing(grid.blockX(candidate), grid.blockZ(candidate)))) {
|
||||
@@ -251,12 +253,14 @@ public final class SeaInletPlanner {
|
||||
int i = cell % grid.width();
|
||||
int j = cell / grid.width();
|
||||
float lowest = Float.POSITIVE_INFINITY;
|
||||
boolean outside = false;
|
||||
for (int dj = -ring; dj <= ring; dj++) {
|
||||
for (int di = -ring; di <= ring; di++) {
|
||||
int ni = i + di;
|
||||
int nj = j + dj;
|
||||
if (ni < 0 || nj < 0 || ni >= grid.width() || nj >= grid.depth()) {
|
||||
return Float.NEGATIVE_INFINITY;
|
||||
outside = true;
|
||||
continue;
|
||||
}
|
||||
int neighbour = grid.index(ni, nj);
|
||||
if (terrain.ocean(neighbour)) {
|
||||
@@ -265,6 +269,10 @@ public final class SeaInletPlanner {
|
||||
lowest = Math.min(lowest, terrain.cover[neighbour]);
|
||||
}
|
||||
}
|
||||
if (outside) {
|
||||
terrain.clipped = true;
|
||||
return Float.NEGATIVE_INFINITY;
|
||||
}
|
||||
return lowest - config.roofClearance() - ROOF_MARGIN;
|
||||
}
|
||||
|
||||
@@ -272,16 +280,20 @@ public final class SeaInletPlanner {
|
||||
DemGrid grid = terrain.grid;
|
||||
int i = cell % grid.width();
|
||||
int j = cell / grid.width();
|
||||
boolean outside = false;
|
||||
for (int dj = -1; dj <= 1; dj++) {
|
||||
for (int di = -1; di <= 1; di++) {
|
||||
int ni = i + di;
|
||||
int nj = j + dj;
|
||||
if (ni < 0 || nj < 0 || ni >= grid.width() || nj >= grid.depth() || terrain.wet(grid.index(ni, nj))) {
|
||||
if (ni < 0 || nj < 0 || ni >= grid.width() || nj >= grid.depth()) {
|
||||
outside = true;
|
||||
} else if (terrain.wet(grid.index(ni, nj))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
terrain.clipped |= outside;
|
||||
return !outside;
|
||||
}
|
||||
|
||||
private static int mouth(Terrain terrain, int land) {
|
||||
@@ -310,14 +322,8 @@ public final class SeaInletPlanner {
|
||||
int best = -1;
|
||||
for (int dj = -1; dj <= 1; dj++) {
|
||||
for (int di = -1; di <= 1; di++) {
|
||||
int ni = i + di;
|
||||
int nj = j + dj;
|
||||
if ((di == 0 && dj == 0) || ni <= 0 || nj <= 0 || ni >= grid.width() - 1 || nj >= grid.depth() - 1) {
|
||||
continue;
|
||||
}
|
||||
int neighbour = grid.index(ni, nj);
|
||||
if (terrain.receivers[neighbour] != cell || terrain.ocean(neighbour) || path.contains(neighbour)
|
||||
|| distanceSquared(grid, neighbour, grid.blockX(mouth), grid.blockZ(mouth)) <= away) {
|
||||
int neighbour = onward(terrain, mouth, i + di, j + dj, away, path);
|
||||
if (neighbour < 0 || terrain.receivers[neighbour] != cell) {
|
||||
continue;
|
||||
}
|
||||
if (best < 0 || terrain.accumulation[neighbour] > terrain.accumulation[best]
|
||||
@@ -339,16 +345,11 @@ public final class SeaInletPlanner {
|
||||
double bestAway = 0D;
|
||||
for (int dj = -1; dj <= 1; dj++) {
|
||||
for (int di = -1; di <= 1; di++) {
|
||||
int ni = i + di;
|
||||
int nj = j + dj;
|
||||
if ((di == 0 && dj == 0) || ni <= 0 || nj <= 0 || ni >= grid.width() - 1 || nj >= grid.depth() - 1) {
|
||||
int neighbour = onward(terrain, mouth, i + di, j + dj, away, path);
|
||||
if (neighbour < 0) {
|
||||
continue;
|
||||
}
|
||||
int neighbour = grid.index(ni, nj);
|
||||
double distance = distanceSquared(grid, neighbour, grid.blockX(mouth), grid.blockZ(mouth));
|
||||
if (terrain.ocean(neighbour) || path.contains(neighbour) || distance <= away) {
|
||||
continue;
|
||||
}
|
||||
boolean covers = canCover(terrain, config, neighbour);
|
||||
if (best < 0 || covers && !bestCovers || covers == bestCovers && (distance > bestAway
|
||||
|| distance == bestAway && terrain.cover[neighbour] > terrain.cover[best])) {
|
||||
@@ -361,12 +362,15 @@ public final class SeaInletPlanner {
|
||||
return best;
|
||||
}
|
||||
|
||||
private Cover cover(DemGrid grid, float[] natural, ShapedNetwork network) {
|
||||
private Cover cover(WindowKey window, DemGrid grid, float[] natural, ShapedNetwork network) {
|
||||
int count = grid.values().length;
|
||||
float[] cover = new float[count];
|
||||
System.arraycopy(natural, 0, cover, 0, count);
|
||||
boolean[] wet = new boolean[count];
|
||||
for (Reach reach : network.reaches()) {
|
||||
if (!nearby(window, owner(reach))) {
|
||||
continue;
|
||||
}
|
||||
int band = reach.tier().bankBand() + CONTAINMENT + grid.cell();
|
||||
for (int point = 0; point + 1 < reach.xs().length; point++) {
|
||||
float bed = Math.min(reach.grade()[point] - reach.depth()[point], reach.grade()[point + 1] - reach.depth()[point + 1]) - 1f;
|
||||
@@ -426,6 +430,71 @@ public final class SeaInletPlanner {
|
||||
}
|
||||
}
|
||||
|
||||
private static int onward(Terrain terrain, int mouth, int ni, int nj, double away, List<Integer> path) {
|
||||
DemGrid grid = terrain.grid;
|
||||
if (ni < 0 || nj < 0 || ni >= grid.width() || nj >= grid.depth()) {
|
||||
terrain.clipped = true;
|
||||
return -1;
|
||||
}
|
||||
int neighbour = grid.index(ni, nj);
|
||||
if (terrain.ocean(neighbour) || path.contains(neighbour)
|
||||
|| distanceSquared(grid, neighbour, grid.blockX(mouth), grid.blockZ(mouth)) <= away) {
|
||||
return -1;
|
||||
}
|
||||
if (ni == 0 || nj == 0 || ni == grid.width() - 1 || nj == grid.depth() - 1) {
|
||||
terrain.clipped = true;
|
||||
return -1;
|
||||
}
|
||||
return neighbour;
|
||||
}
|
||||
|
||||
private boolean reachesEdge(Terrain terrain, HydrologySettings.SeaInlets config, int siteX, int siteZ, WindowKey owner) {
|
||||
DemGrid grid = terrain.grid;
|
||||
long[] columns = siteCells(siteX, config.spacing(), owner.wx(), grid.originX(), grid.cell());
|
||||
long[] rows = siteCells(siteZ, config.spacing(), owner.wz(), grid.originZ(), grid.cell());
|
||||
if (columns[0] < 0L || rows[0] < 0L || columns[1] >= grid.width() || rows[1] >= grid.depth()) {
|
||||
return true;
|
||||
}
|
||||
float cliff = settings.seaLevel() + config.minimumCliffHeight();
|
||||
int lastColumn = grid.width() - 1;
|
||||
int lastRow = grid.depth() - 1;
|
||||
for (int i = (int) columns[0]; i <= columns[1]; i++) {
|
||||
if (rows[0] == 0L && cliffLand(terrain, i, 0, cliff) || rows[1] == lastRow && cliffLand(terrain, i, lastRow, cliff)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (int j = (int) rows[0]; j <= rows[1]; j++) {
|
||||
if (columns[0] == 0L && cliffLand(terrain, 0, j, cliff) || columns[1] == lastColumn && cliffLand(terrain, lastColumn, j, cliff)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static long[] siteCells(int site, int spacing, int window, int origin, int cell) {
|
||||
long from = Math.max((long) site * spacing, (long) window * WindowKey.SIZE);
|
||||
long to = Math.min(((long) site + 1L) * spacing, ((long) window + 1L) * WindowKey.SIZE);
|
||||
return new long[]{Math.ceilDiv(from - origin, cell), Math.ceilDiv(to - origin, cell) - 1L};
|
||||
}
|
||||
|
||||
private static boolean cliffLand(Terrain terrain, int i, int j, float cliff) {
|
||||
int cell = terrain.grid.index(i, j);
|
||||
return !terrain.ocean(cell) && terrain.natural(cell) >= cliff;
|
||||
}
|
||||
|
||||
private static WindowKey owner(SeaInlet inlet) {
|
||||
return WindowKey.containing((int) Math.floor(inlet.xs()[1]), (int) Math.floor(inlet.zs()[1]));
|
||||
}
|
||||
|
||||
private static WindowKey owner(Reach reach) {
|
||||
int last = reach.xs().length - 1;
|
||||
return WindowKey.containing((int) Math.floor(reach.xs()[last]), (int) Math.floor(reach.zs()[last]));
|
||||
}
|
||||
|
||||
private static boolean nearby(WindowKey window, WindowKey other) {
|
||||
return Math.abs(other.wx() - window.wx()) <= 1 && Math.abs(other.wz() - window.wz()) <= 1;
|
||||
}
|
||||
|
||||
private static double squaredAbove(float radius) {
|
||||
return (double) radius * radius * (1D + SQUARED_MARGIN);
|
||||
}
|
||||
@@ -475,6 +544,7 @@ public final class SeaInletPlanner {
|
||||
private final float[] cover;
|
||||
private final boolean[] wet;
|
||||
private final long seed;
|
||||
private boolean clipped;
|
||||
|
||||
private Terrain(DemGrid grid, HydrologySampleBuffer samples, int[] receivers, float[] accumulation, Cover cover, long seed) {
|
||||
this.grid = grid;
|
||||
|
||||
+25
-10
@@ -7,6 +7,7 @@ import art.arcane.iris.generation.hydrology.grid.FlowAccumulator;
|
||||
import art.arcane.iris.generation.hydrology.grid.HydrologySampleBuffer;
|
||||
import art.arcane.iris.generation.hydrology.grid.PriorityFlood;
|
||||
import art.arcane.iris.generation.hydrology.plan.HydrologySettings;
|
||||
import art.arcane.iris.generation.hydrology.plan.WindowKey;
|
||||
import art.arcane.iris.generation.hydrology.water.Reach;
|
||||
import art.arcane.iris.generation.hydrology.water.ReachStyle;
|
||||
import art.arcane.iris.generation.hydrology.water.ShapedNetwork;
|
||||
@@ -24,21 +25,24 @@ final class InletTerrain {
|
||||
static final int CELL = 8;
|
||||
static final int WIDTH = 96;
|
||||
static final int DEPTH = 64;
|
||||
static final int SPAN = 3 * WindowKey.SIZE / CELL;
|
||||
|
||||
final DoubleBinaryOperator natural;
|
||||
final WindowKey window;
|
||||
final DemGrid grid;
|
||||
final HydrologySampleBuffer samples;
|
||||
final int[] receivers;
|
||||
final float[] accumulation;
|
||||
|
||||
private InletTerrain(DoubleBinaryOperator natural) {
|
||||
this(natural, 0, 0);
|
||||
this(natural, 0, 0, WIDTH, DEPTH);
|
||||
}
|
||||
|
||||
private InletTerrain(DoubleBinaryOperator natural, int originX, int originZ) {
|
||||
private InletTerrain(DoubleBinaryOperator natural, int originX, int originZ, int width, int depth) {
|
||||
this.natural = natural;
|
||||
this.grid = new DemGrid(originX, originZ, CELL, WIDTH, DEPTH);
|
||||
int count = WIDTH * DEPTH;
|
||||
this.window = WindowKey.containing(originX + width * CELL / 2, originZ + depth * CELL / 2);
|
||||
this.grid = new DemGrid(originX, originZ, CELL, width, depth);
|
||||
int count = width * depth;
|
||||
this.samples = new HydrologySampleBuffer(count);
|
||||
BitSet seeds = new BitSet(count);
|
||||
for (int cell = 0; cell < count; cell++) {
|
||||
@@ -48,14 +52,14 @@ final class InletTerrain {
|
||||
grid.values()[cell] = height;
|
||||
samples.height()[cell] = height;
|
||||
samples.ocean()[cell] = Math.round(height) < SEA;
|
||||
int i = cell % WIDTH;
|
||||
int j = cell / WIDTH;
|
||||
if (samples.ocean()[cell] || i == 0 || j == 0 || i == WIDTH - 1 || j == DEPTH - 1) {
|
||||
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(grid, seeds, 0.001f);
|
||||
DemGrid surface = new DemGrid(originX, originZ, CELL, WIDTH, DEPTH);
|
||||
DemGrid surface = new DemGrid(originX, originZ, CELL, width, depth);
|
||||
System.arraycopy(filled, 0, surface.values(), 0, count);
|
||||
this.receivers = D8Router.receivers(surface, filled);
|
||||
float[] weights = new float[count];
|
||||
@@ -72,6 +76,13 @@ final class InletTerrain {
|
||||
return new InletTerrain((x, z) -> x < COAST_X ? SEA - 20D : SEA + cliffHeight + 0.08D * (x - COAST_X));
|
||||
}
|
||||
|
||||
static InletTerrain openCoast() {
|
||||
return new InletTerrain((x, z) -> {
|
||||
double coast = 768D + 120D * Math.sin(z / 180D) + 40D * Math.sin(z / 53D);
|
||||
return x < coast ? SEA - 20D : SEA + 30D + 0.08D * (x - coast);
|
||||
});
|
||||
}
|
||||
|
||||
static InletTerrain cliffWithTrough(double cliffHeight, double troughFrom, double troughTo, double troughHeight) {
|
||||
return new InletTerrain((x, z) -> {
|
||||
if (x < COAST_X) {
|
||||
@@ -124,11 +135,15 @@ final class InletTerrain {
|
||||
}
|
||||
|
||||
InletTerrain shifted(int originX, int originZ) {
|
||||
return new InletTerrain(natural, originX, originZ);
|
||||
return new InletTerrain(natural, originX, originZ, WIDTH, DEPTH);
|
||||
}
|
||||
|
||||
InletTerrain span(WindowKey window) {
|
||||
return new InletTerrain(natural, (window.wx() - 1) * WindowKey.SIZE, (window.wz() - 1) * WindowKey.SIZE, SPAN, SPAN);
|
||||
}
|
||||
|
||||
List<SeaInlet> plan(HydrologySettings settings, ShapedNetwork network) {
|
||||
return new SeaInletPlanner(settings).plan(grid, samples, receivers, accumulation, network, SEED);
|
||||
return new SeaInletPlanner(settings).plan(window, grid, samples, receivers, accumulation, network, SEED);
|
||||
}
|
||||
|
||||
List<SeaInlet> plan(HydrologySettings settings) {
|
||||
|
||||
+68
-1
@@ -8,8 +8,11 @@ import art.arcane.iris.generation.hydrology.water.ShapedNetwork;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -336,11 +339,75 @@ public class SeaInletPlannerTest {
|
||||
assertEquals(first.size(), first.stream().mapToLong(SeaInlet::id).distinct().count());
|
||||
InletTerrain terrain = InletTerrain.cliff(30D);
|
||||
List<SeaInlet> otherSeed = new SeaInletPlanner(HydrologySettings.compile(new IrisHydrology(), 99L, SEA, InletTerrain.MIN_Y,
|
||||
InletTerrain.MAX_Y)).plan(terrain.grid, terrain.samples, terrain.receivers, terrain.accumulation,
|
||||
InletTerrain.MAX_Y)).plan(terrain.window, terrain.grid, terrain.samples, terrain.receivers, terrain.accumulation,
|
||||
new ShapedNetwork(List.of(), List.of()), 99L);
|
||||
assertNotEquals(first, otherSeed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void inletsAgreeAcrossWindowsOnOpenCoast() {
|
||||
IrisHydrology dense = new IrisHydrology();
|
||||
dense.getUnderground().getSeaInlets().setSpacing(64);
|
||||
HydrologySettings settings = InletTerrain.settings(dense);
|
||||
InletTerrain coast = InletTerrain.openCoast();
|
||||
List<WindowKey> windows = new ArrayList<>(9);
|
||||
for (int wz = 0; wz <= 2; wz++) {
|
||||
for (int wx = 0; wx <= 2; wx++) {
|
||||
windows.add(new WindowKey(wx, wz));
|
||||
}
|
||||
}
|
||||
Map<WindowKey, List<SeaInlet>> views = views(coast, settings, windows);
|
||||
assertEquals("planning the windows in reverse order changes nothing", views, views(coast, settings, windows.reversed()));
|
||||
List<String> disagreements = new ArrayList<>();
|
||||
int owned = 0;
|
||||
int crossing = 0;
|
||||
int seen = 0;
|
||||
for (WindowKey owner : windows) {
|
||||
for (SeaInlet inlet : views.get(owner)) {
|
||||
if (!owner.equals(owner(inlet))) {
|
||||
continue;
|
||||
}
|
||||
owned++;
|
||||
crossing += leavesWindow(inlet, owner) ? 1 : 0;
|
||||
for (WindowKey view : windows) {
|
||||
if (view.equals(owner)) {
|
||||
continue;
|
||||
}
|
||||
for (SeaInlet other : views.get(view)) {
|
||||
if (other.id() != inlet.id()) {
|
||||
continue;
|
||||
}
|
||||
seen++;
|
||||
if (!other.equals(inlet)) {
|
||||
disagreements.add(inlet + " of " + owner + " is " + other + " in " + view);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assertTrue(disagreements.size() + " views disagree with the owner: " + disagreements, disagreements.isEmpty());
|
||||
assertTrue("owned inlets " + owned, owned >= 20);
|
||||
assertTrue("owned inlets that neighbouring windows also plan " + seen, seen >= 60);
|
||||
assertTrue("owned inlets crossing a window line " + crossing, crossing > 0);
|
||||
}
|
||||
|
||||
private static Map<WindowKey, List<SeaInlet>> views(InletTerrain coast, HydrologySettings settings, List<WindowKey> order) {
|
||||
Map<WindowKey, List<SeaInlet>> views = new LinkedHashMap<>();
|
||||
for (WindowKey window : order) {
|
||||
views.put(window, coast.span(window).plan(settings));
|
||||
}
|
||||
return views;
|
||||
}
|
||||
|
||||
private static WindowKey owner(SeaInlet inlet) {
|
||||
return WindowKey.containing((int) Math.floor(inlet.xs()[1]), (int) Math.floor(inlet.zs()[1]));
|
||||
}
|
||||
|
||||
private static boolean leavesWindow(SeaInlet inlet, WindowKey owner) {
|
||||
int[] bounds = SeaInletRasterizer.bounds(inlet);
|
||||
return !WindowKey.containing(bounds[0], bounds[1]).equals(owner) || !WindowKey.containing(bounds[2], bounds[3]).equals(owner);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void inletsReachingPastTheNextWindowAreRejected() {
|
||||
IrisHydrology fits = new IrisHydrology();
|
||||
|
||||
Reference in new issue
Block a user