Junction errors name window

- Reach indexes take their window key and name it when a reach id appears twice in one plan
- Window planning passes its key through reach shaping, pond-channel settlement and confluence grading
- A downstream reach starting above its feeder's end fails closed naming the window
- Index, grader and plan-cache tests check that each failure names its window
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-01 20:48:08 -05:00
1 parent 41e38debad
commit 358f7218f8
17 files changed
+101 -64

No files matched your search

@@ -434,7 +434,7 @@ public final class HydrologyPlanCache {
}
private static IndexedWindow indexed(WindowPlan plan) {
return new IndexedWindow(plan, ReachIndex.build(plan.reaches(), plan.ponds()));
return new IndexedWindow(plan, ReachIndex.build(plan.key(), plan.reaches(), plan.ponds()));
}
private void persist(Consumer<HydrologyPlanStore> write, Object key) {
@@ -106,16 +106,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 = PondChannelSettlement.settle(shaper.shape(drainage.surface(), drainage.receivers(), drainage.accumulation(),
drainage.channels(), water, fine, settings.seed()), settings.seaLevel());
ShapedNetwork network = PondChannelSettlement.settle(key, shaper.shape(key, 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 = PondChannelSettlement.settle(shaper.shape(drainage.surface(), drainage.receivers(), drainage.accumulation(),
drainage.channels(), water, fine, settings.seed()), settings.seaLevel());
network = PondChannelSettlement.settle(key, shaper.shape(key, 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());
@@ -1,5 +1,6 @@
package art.arcane.iris.generation.hydrology.raster;
import art.arcane.iris.generation.hydrology.plan.WindowKey;
import art.arcane.iris.generation.hydrology.water.Pond;
import art.arcane.iris.generation.hydrology.water.PondKind;
import art.arcane.iris.generation.hydrology.water.PondMask;
@@ -16,12 +17,12 @@ public final class PondChannelSettlement {
private PondChannelSettlement() {
}
public static ShapedNetwork settle(ShapedNetwork network, int seaLevel) {
public static ShapedNetwork settle(WindowKey window, 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);
int[] levels = levels(window, 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);
@@ -31,7 +32,7 @@ public final class PondChannelSettlement {
return new ShapedNetwork(network.reaches(), water);
}
static int[] levels(List<Reach> reaches, List<Pond> ponds, int[] initial, int seaLevel) {
static int[] levels(WindowKey window, List<Reach> reaches, List<Pond> ponds, int[] initial, int seaLevel) {
int[] levels = initial.clone();
if (reaches.isEmpty() || ponds.isEmpty()) {
return levels;
@@ -43,7 +44,7 @@ public final class PondChannelSettlement {
if (!eligible) {
return levels;
}
ReachIndex index = ReachIndex.build(reaches, List.of());
ReachIndex index = ReachIndex.build(window, reaches, List.of());
List<Reach> nearby = new ArrayList<>();
List<Pond> unused = new ArrayList<>();
for (int pond = 0; pond < ponds.size(); pond++) {
@@ -1,5 +1,6 @@
package art.arcane.iris.generation.hydrology.raster;
import art.arcane.iris.generation.hydrology.plan.WindowKey;
import art.arcane.iris.generation.hydrology.water.Pond;
import art.arcane.iris.generation.hydrology.water.PondMask;
import art.arcane.iris.generation.hydrology.water.Reach;
@@ -10,6 +11,7 @@ import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public final class ReachIndex {
private static final int BUCKET = 16;
@@ -29,7 +31,7 @@ public final class ReachIndex {
private final Map<Long, Reach> byId;
private final Map<Long, List<Pond>> outlets;
private ReachIndex(List<Reach> reaches, List<Pond> ponds) {
private ReachIndex(WindowKey window, List<Reach> reaches, List<Pond> ponds) {
this.reaches = List.copyOf(reaches);
this.ponds = List.copyOf(ponds);
this.corridors = new float[this.reaches.size()][];
@@ -55,12 +57,12 @@ public final class ReachIndex {
this.reachBuckets = buckets(corridors.length, this::visitReach);
this.pondBuckets = buckets(footprints.length, this::visitPond);
this.feeders = feeders(this.reaches);
this.byId = byId(this.reaches);
this.byId = byId(window, this.reaches);
this.outlets = outlets(this.ponds);
}
public static ReachIndex build(List<Reach> reaches, List<Pond> ponds) {
return new ReachIndex(reaches, ponds);
public static ReachIndex build(WindowKey window, List<Reach> reaches, List<Pond> ponds) {
return new ReachIndex(Objects.requireNonNull(window, "window"), reaches, ponds);
}
public void candidates(int chunkX, int chunkZ, int halo, List<Reach> outReaches, List<Pond> outPonds) {
@@ -128,11 +130,11 @@ public final class ReachIndex {
return Map.copyOf(frozen);
}
private static Map<Long, Reach> byId(List<Reach> reaches) {
private static Map<Long, Reach> byId(WindowKey window, List<Reach> reaches) {
Map<Long, Reach> byId = new HashMap<>();
for (Reach reach : reaches) {
if (byId.putIfAbsent(reach.id(), reach) != null) {
throw new IllegalStateException("Reach id " + reach.id() + " appears twice in one plan");
throw new IllegalStateException("Reach id " + reach.id() + " appears twice in the plan of window " + window);
}
}
return Map.copyOf(byId);
@@ -1,5 +1,7 @@
package art.arcane.iris.generation.hydrology.water;
import art.arcane.iris.generation.hydrology.plan.WindowKey;
import java.util.ArrayList;
import java.util.List;
@@ -42,13 +44,13 @@ public final class ConfluenceGrader {
}
}
static boolean[] level(List<ReachDraft> drafts, int[] upstreamFirst) {
static boolean[] level(WindowKey window, List<ReachDraft> drafts, int[] upstreamFirst) {
boolean[] touched = new boolean[drafts.size()];
for (int position = upstreamFirst.length - 1; position >= 0; position--) {
ReachDraft downstream = drafts.get(upstreamFirst[position]);
for (int feeder : downstream.incoming) {
ReachDraft draft = drafts.get(feeder);
requireJunction(draft, downstream);
requireJunction(window, draft, downstream);
boolean[] fixed = fixed(draft);
int waterfallMinimumDrop = draft.tier.waterfallMinimumDrop();
int[] settled = settle(footprint(draft), draft.grade, fixed, receiving(drafts, downstream, feeder), downstream.grade[0],
@@ -86,13 +88,13 @@ public final class ConfluenceGrader {
return nearest;
}
private static void requireJunction(ReachDraft feeder, ReachDraft downstream) {
private static void requireJunction(WindowKey window, ReachDraft feeder, ReachDraft downstream) {
if (downstream.grade[0] <= feeder.last()) {
return;
}
int end = feeder.xs.length - 1;
throw new IllegalStateException("Reach " + downstream.id + " starts at " + downstream.grade[0] + ", above the end " + feeder.last()
+ " of its feeder reach " + feeder.id + " at junction " + feeder.xs[end] + "," + feeder.zs[end]);
throw new IllegalStateException("Window " + window + " reach " + downstream.id + " starts at " + downstream.grade[0] + ", above the end "
+ feeder.last() + " of its feeder reach " + feeder.id + " at junction " + feeder.xs[end] + "," + feeder.zs[end]);
}
private static boolean pooled(Footprint feeder, boolean[] fixed, List<Footprint> receiving, double[] arc, int point) {
@@ -4,6 +4,7 @@ import art.arcane.iris.generation.hydrology.grid.CellHash;
import art.arcane.iris.generation.hydrology.grid.DemGrid;
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 java.util.ArrayList;
@@ -32,7 +33,7 @@ public final class ReachShaper {
this.settings = Objects.requireNonNull(settings, "settings");
}
public ShapedNetwork shape(DemGrid filled, int[] receivers, float[] accumulation, BitSet channels, List<Pond> ponds,
public ShapedNetwork shape(WindowKey window, DemGrid filled, int[] receivers, float[] accumulation, BitSet channels, List<Pond> ponds,
HeightLattice fine, long seed) {
requireNetwork(filled, receivers, accumulation, channels);
PondLookup lookup = new PondLookup(ponds);
@@ -74,7 +75,7 @@ public final class ReachShaper {
for (int index : order) {
finish(drafts, drafts.get(index));
}
boolean[] touched = ConfluenceGrader.level(drafts, order);
boolean[] touched = ConfluenceGrader.level(window, drafts, order);
for (int index = 0; index < touched.length; index++) {
if (touched[index]) {
restyle(drafts.get(index));
@@ -311,6 +311,19 @@ public class HydrologyPlanCacheTest {
new WindowKey(0, -1)), new HashSet<>(order.subList(1, 5)));
}
@Test
public void aWindowPlanWithADuplicateReachIdFailsClosedNamingTheWindow() {
FakePlanners planners = new FakePlanners(unpersisted());
WindowKey key = new WindowKey(5, -3);
HydrologyPlanCache cache = new HydrologyPlanCache(planners.settings, planners::region, (window, regions) -> {
Reach reach = planners.window(window, regions).reaches().get(0);
return new WindowPlan(window, List.of(reach, reach), List.of(), List.of());
}, pool(1), () -> false);
caches.add(cache);
Throwable failure = failureOf(() -> cache.window(key));
assertTrue("" + failure, failure instanceof IllegalStateException && failure.getMessage().contains(key.toString()));
}
@Test
public void plannerFailurePropagatesAndIsRetriedOnNextRequest() throws Exception {
FakePlanners planners = new FakePlanners(unpersisted());
@@ -177,7 +177,7 @@ final class PlannedScene {
private IndexedWindow window(WindowKey key) {
return windows.computeIfAbsent(key, ignored -> {
WindowPlan plan = plans.getOrDefault(key, new WindowPlan(key, List.of(), List.of(), List.of()));
return new IndexedWindow(plan, ReachIndex.build(plan.reaches(), plan.ponds()));
return new IndexedWindow(plan, ReachIndex.build(key, plan.reaches(), plan.ponds()));
});
}
@@ -80,14 +80,14 @@ public class WindowPlannerTest {
ReachShaper shaper = mock(ReachShaper.class);
AtomicInteger passes = new AtomicInteger();
doAnswer(call -> {
List<Pond> water = call.getArgument(4);
List<Pond> water = call.getArgument(5);
Pond pond = single(water);
passes.incrementAndGet();
int head = pond.level() > 96 ? 96 : 94;
Reach outlet = new Reach(20L, settings.tiers().get(0), 0L, new float[]{64f, 64f}, new float[]{32f, 96f},
new int[]{head, head}, new float[]{2f, 2f}, new float[]{2f, 2f}, new ReachStyle[]{ReachStyle.RUN, ReachStyle.RUN}, 1000f);
return new ShapedNetwork(List.of(outlet), List.of(pond.withOutletReachId(outlet.id())));
}).when(shaper).shape(any(), any(), any(), any(), any(), any(), anyLong());
}).when(shaper).shape(any(), any(), any(), any(), any(), any(), any(), anyLong());
WindowPlanner planner = new WindowPlanner(settings,
new HeightLattice(terrain, WindowPlanner.FINE_CELL, false, 1 << 18), ponds, shaper);
@@ -577,7 +577,7 @@ public class WindowPlannerTest {
private static WindowPlanner loweringWatched(HydrologySettings settings, SyntheticTerrain terrain, Set<Long> lowered) {
ReachShaper shaper = spy(new ReachShaper(settings));
doAnswer(call -> {
List<Pond> planned = call.getArgument(4);
List<Pond> planned = call.getArgument(5);
ShapedNetwork network = (ShapedNetwork) call.callRealMethod();
Map<Long, Integer> levels = new HashMap<>();
for (Pond pond : planned) {
@@ -590,7 +590,7 @@ public class WindowPlannerTest {
}
}
return network;
}).when(shaper).shape(any(), any(), any(), any(), any(), any(), anyLong());
}).when(shaper).shape(any(), any(), any(), any(), any(), any(), any(), anyLong());
return new WindowPlanner(settings,
new HeightLattice(terrain, WindowPlanner.FINE_CELL, false, 1 << 21), new PondPlanner(settings), shaper);
}
@@ -874,7 +874,7 @@ public class WindowPlannerTest {
doReturn(CupSet.EMPTY).when(ponds).subtractCups(any(), any(), any(), any());
doReturn(water).when(ponds).ponds(any(), any(), any(), any(), any(), any());
ReachShaper shaper = mock(ReachShaper.class);
doReturn(new ShapedNetwork(reaches, water)).when(shaper).shape(any(), any(), any(), any(), any(), any(), anyLong());
doReturn(new ShapedNetwork(reaches, water)).when(shaper).shape(any(), any(), any(), any(), any(), any(), any(), anyLong());
return new WindowPlanner(settings, new HeightLattice(SyntheticTerrain.flat(100D), WindowPlanner.FINE_CELL, false, 1 << 18), ponds, shaper);
}
@@ -403,7 +403,7 @@ public class ChunkWaterRasterTest {
}
SeaInlet inlet = new SeaInlet(71L, xs, zs, halfWidth, SEA_LEVEL, floor, ceiling, 64, 8, 6);
WindowKey key = new WindowKey(0, 0);
IndexedWindow own = new IndexedWindow(new WindowPlan(key, List.of(), List.of(), List.of(inlet)), ReachIndex.build(List.of(), List.of()));
IndexedWindow own = new IndexedWindow(new WindowPlan(key, List.of(), List.of(), List.of(inlet)), ReachIndex.build(key, List.of(), List.of()));
ChunkRasterizer rasterizer = new ChunkRasterizer(settings, ChannelFluids.TIER);
ChunkWaterRaster mouth = rasterizer.rasterize(0, 0, (x, z) -> 70, List.of(own), regions(0, 0, List.of()));
int[] column = new int[2];
@@ -435,7 +435,7 @@ public class ChunkWaterRasterTest {
HydrologySettings settings = settings(new IrisHydrology());
Reach far = line(61L, tier("stream", 1.5f, 0.5f, 0, IrisBankStyle.SMOOTH), 0L, 400f, 400f, 480f, 400f, (x, z) -> 69);
WindowKey key = new WindowKey(0, 0);
IndexedWindow own = new IndexedWindow(new WindowPlan(key, List.of(far), List.of(), List.of()), ReachIndex.build(List.of(far), List.of()));
IndexedWindow own = new IndexedWindow(new WindowPlan(key, List.of(far), List.of(), List.of()), ReachIndex.build(key, List.of(far), List.of()));
AtomicInteger samples = new AtomicInteger();
IntBinaryOperator natural = (x, z) -> {
samples.incrementAndGet();
@@ -473,7 +473,7 @@ public class ChunkWaterRasterTest {
Reach inflow = line(62L, stream, 0L, 480f, 412f, 400f, 412f, (x, z) -> 69);
WindowKey key = new WindowKey(0, 0);
IndexedWindow own = new IndexedWindow(new WindowPlan(key, List.of(outflow, inflow), List.of(), List.of()),
ReachIndex.build(List.of(outflow, inflow), List.of()));
ReachIndex.build(key, List.of(outflow, inflow), List.of()));
ChannelFluids volcanic = (x, z) -> x < 440 ? "lava" : null;
ChunkWaterRaster raster = new ChunkRasterizer(settings, volcanic).rasterize(27, 25, (x, z) -> 70, List.of(own), regions(27, 25, List.of()));
int lava = 0;
@@ -500,7 +500,7 @@ public class ChunkWaterRasterTest {
HydrologySettings settings = settings(new IrisHydrology());
Reach river = line(81L, tier("river", 3f, 0.5f, 32, IrisBankStyle.SMOOTH), 0L, 96f, 256f, 416f, 256f, (x, z) -> 69);
WindowKey key = new WindowKey(0, 0);
IndexedWindow own = new IndexedWindow(new WindowPlan(key, List.of(river), List.of(), List.of()), ReachIndex.build(List.of(river), List.of()));
IndexedWindow own = new IndexedWindow(new WindowPlan(key, List.of(river), List.of(), List.of()), ReachIndex.build(key, List.of(river), List.of()));
NaturalHeights natural = (x, z) -> {
double away = Math.abs(z - 256);
if (x < 256) {
@@ -870,7 +870,7 @@ public class ChunkWaterRasterTest {
WindowKey key = new WindowKey(dx, dz);
List<Reach> owned = key.equals(new WindowKey(0, 0)) ? List.of(feeder)
: key.equals(new WindowKey(1, 0)) || key.equals(new WindowKey(1, -1)) ? List.of(downstream) : List.of();
windows.add(new IndexedWindow(new WindowPlan(key, owned, List.of(), List.of()), ReachIndex.build(owned, List.of())));
windows.add(new IndexedWindow(new WindowPlan(key, owned, List.of(), List.of()), ReachIndex.build(key, owned, List.of())));
}
}
ChunkRasterizer rasterizer = new ChunkRasterizer(settings(new IrisHydrology()), ChannelFluids.TIER);
@@ -1353,7 +1353,7 @@ public class ChunkWaterRasterTest {
ownedPonds.add(pond);
}
}
return new IndexedWindow(new WindowPlan(key, owned, ownedPonds, List.of()), ReachIndex.build(owned, ownedPonds));
return new IndexedWindow(new WindowPlan(key, owned, ownedPonds, List.of()), ReachIndex.build(key, owned, ownedPonds));
});
}
@@ -1,6 +1,7 @@
package art.arcane.iris.generation.hydrology.raster;
import art.arcane.iris.generation.hydrology.config.IrisBankStyle;
import art.arcane.iris.generation.hydrology.plan.WindowKey;
import art.arcane.iris.generation.hydrology.water.HydrologyTier;
import art.arcane.iris.generation.hydrology.water.Pond;
import art.arcane.iris.generation.hydrology.water.PondKind;
@@ -16,6 +17,7 @@ import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class PondChannelSettlementTest {
private static final WindowKey WINDOW = new WindowKey(0, 0);
private static final int SEA_LEVEL = 40;
@Test
@@ -88,14 +90,14 @@ public class PondChannelSettlementTest {
Reach across = line(3L, -20, 0, 40, 0, 4f, 96, 96);
Reach lower = line(4L, 17, -20, 17, 20, 4f, 94, 94);
assertArrayEquals(new int[]{96, 94}, PondChannelSettlement.levels(List.of(across, lower), List.of(first, second),
assertArrayEquals(new int[]{96, 94}, PondChannelSettlement.levels(WINDOW, List.of(across, lower), List.of(first, second),
new int[]{104, 102}, SEA_LEVEL));
assertArrayEquals(new int[]{94, 96}, PondChannelSettlement.levels(List.of(lower, across), List.of(second, first),
assertArrayEquals(new int[]{94, 96}, PondChannelSettlement.levels(WINDOW, List.of(lower, across), List.of(second, first),
new int[]{102, 104}, SEA_LEVEL));
}
private static int settle(List<Reach> reaches, Pond pond) {
return PondChannelSettlement.levels(reaches, List.of(pond), new int[]{pond.level()}, SEA_LEVEL)[0];
return PondChannelSettlement.levels(WINDOW, reaches, List.of(pond), new int[]{pond.level()}, SEA_LEVEL)[0];
}
private static Pond pond(long id, PondKind kind, int level, PondMask mask) {
@@ -2,6 +2,7 @@ package art.arcane.iris.generation.hydrology.raster;
import art.arcane.iris.generation.hydrology.config.IrisBankStyle;
import art.arcane.iris.generation.hydrology.config.IrisHydrologyMaterials;
import art.arcane.iris.generation.hydrology.plan.WindowKey;
import art.arcane.iris.generation.hydrology.water.HydrologyTier;
import art.arcane.iris.generation.hydrology.water.Pond;
import art.arcane.iris.generation.hydrology.water.PondKind;
@@ -18,13 +19,23 @@ import java.util.SplittableRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
public class ReachIndexTest {
private static final WindowKey WINDOW = new WindowKey(0, 0);
private static final int BANK_BAND = 4;
private static final HydrologyTier TIER = new HydrologyTier("test", 0f, 1f, 4f, 1f, 0.4f, 1, true, 6, BANK_BAND, IrisBankStyle.SMOOTH,
false, new IrisHydrologyMaterials(), "water", List.of());
@Test
public void aReachIdTwiceInOnePlanFailsClosedNamingTheWindow() {
WindowKey window = new WindowKey(5, -3);
Reach reach = reach(7L, new float[]{0f, 16f}, new float[]{0f, 0f}, 1f);
IllegalStateException failure = assertThrows(IllegalStateException.class, () -> ReachIndex.build(window, List.of(reach, reach), List.of()));
assertTrue(failure.getMessage(), failure.getMessage().contains("Reach id 7") && failure.getMessage().contains(window.toString()));
}
@Test
public void reachIndexReturnsReachesWithinHalo() {
Reach inside = reach(1L, new float[]{-40f, 10f, 60f}, new float[]{8f, 8f, 9f}, 2f);
@@ -33,7 +44,7 @@ public class ReachIndexTest {
Reach diagonal = reach(4L, new float[]{-200f, 200f}, new float[]{-200f, 200f}, 1f);
Pond pond = pond(10L, new PondMask(100, 100, 4, 3, 3, new long[]{0x1FFL}));
Pond distant = pond(11L, new PondMask(-900, 400, 4, 2, 2, new long[]{0xFL}));
ReachIndex index = ReachIndex.build(List.of(inside, east, far, diagonal), List.of(pond, distant));
ReachIndex index = ReachIndex.build(WINDOW, List.of(inside, east, far, diagonal), List.of(pond, distant));
assertEquals(List.of(inside, diagonal), reaches(index, 0, 0, 0));
assertEquals("corridor edge at x=36 is 20 blocks east of the chunk", List.of(inside, diagonal), reaches(index, 0, 0, 19));
@@ -53,7 +64,7 @@ public class ReachIndexTest {
List<Reach> none = new ArrayList<>();
List<Pond> noPonds = new ArrayList<>();
ReachIndex.build(List.of(), List.of()).candidates(0, 0, 48, none, noPonds);
ReachIndex.build(WINDOW, List.of(), List.of()).candidates(0, 0, 48, none, noPonds);
assertTrue(none.isEmpty() && noPonds.isEmpty());
SplittableRandom random = new SplittableRandom(1337L);
@@ -69,7 +80,7 @@ public class ReachIndexTest {
scattered.add(pond(500L + count, new PondMask(random.nextInt(-700, 700), random.nextInt(-700, 700), step, width, depth,
new long[(width * depth + 63) >>> 6])));
}
ReachIndex randomIndex = ReachIndex.build(walks, scattered);
ReachIndex randomIndex = ReachIndex.build(WINDOW, walks, scattered);
int matched = 0;
for (int query = 0; query < 400; query++) {
int chunkX = random.nextInt(-50, 50);
@@ -163,7 +174,7 @@ public class ReachIndexTest {
Reach west = linked(21L, 30L, -20f);
Reach east = linked(22L, 30L, 20f);
Reach other = linked(23L, 31L, 60f);
ReachIndex index = ReachIndex.build(List.of(west, east, other), List.of());
ReachIndex index = ReachIndex.build(WINDOW, List.of(west, east, other), List.of());
assertEquals(List.of(west, east), index.feeders(30L));
assertEquals(List.of(other), index.feeders(31L));
assertTrue(index.feeders(99L).isEmpty());
@@ -3,6 +3,7 @@ package art.arcane.iris.generation.hydrology.water;
import art.arcane.iris.generation.hydrology.config.IrisHydrology;
import art.arcane.iris.generation.hydrology.config.IrisHydrologyTier;
import art.arcane.iris.generation.hydrology.plan.HydrologySettings;
import art.arcane.iris.generation.hydrology.plan.WindowKey;
import org.junit.Test;
import java.util.Arrays;
@@ -15,6 +16,7 @@ import static org.junit.Assert.assertTrue;
public class ConfluenceGraderTest {
private static final long SEED = 1337L;
private static final WindowKey WINDOW = new WindowKey(5, -3);
private static final int SEA_LEVEL = 40;
private static final int JUNCTION = 100;
private static final int POOL_EDGE_Z = -12;
@@ -48,8 +50,9 @@ public class ConfluenceGraderTest {
feeder.downstream = 1;
river.incoming = new int[]{0};
IllegalStateException failure = assertThrows(IllegalStateException.class,
() -> ConfluenceGrader.level(List.of(feeder, river), new int[]{0, 1}));
assertTrue(failure.getMessage(), failure.getMessage().contains("11") && failure.getMessage().contains("12"));
() -> ConfluenceGrader.level(WINDOW, List.of(feeder, river), new int[]{0, 1}));
assertTrue(failure.getMessage(), failure.getMessage().contains("11") && failure.getMessage().contains("12")
&& failure.getMessage().contains(WINDOW.toString()));
}
private static int[] graded(HydrologyTier tier, boolean fixedApproach) {
@@ -60,7 +63,7 @@ public class ConfluenceGraderTest {
}
feeder.downstream = 1;
river.incoming = new int[]{0};
ConfluenceGrader.level(List.of(feeder, river), new int[]{0, 1});
ConfluenceGrader.level(WINDOW, List.of(feeder, river), new int[]{0, 1});
return feeder.grade;
}
@@ -701,7 +701,7 @@ public class PondPlannerTest {
ChunkWaterRaster raster = rasters.get(key);
if (raster == null) {
WindowPlan window = new WindowPlan(WindowKey.containing(x, z), List.of(), List.of(pond), List.of());
IndexedWindow indexed = new IndexedWindow(window, ReachIndex.build(List.of(), List.of(pond)));
IndexedWindow indexed = new IndexedWindow(window, ReachIndex.build(window.key(), List.of(), List.of(pond)));
List<RegionPlan> regions = new ArrayList<>();
for (RegionKey region : ChunkRasterizer.lakeRegions(x >> 4, z >> 4)) {
regions.add(new RegionPlan(region, List.of(), List.of()));
@@ -14,6 +14,7 @@ import art.arcane.iris.generation.hydrology.grid.HydrologySampleBuffer;
import art.arcane.iris.generation.hydrology.grid.HydrologyTerrainSource;
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.raster.PondChannelSettlement;
import art.arcane.volmlib.util.collection.KList;
import org.junit.Test;
@@ -36,6 +37,7 @@ import static org.junit.Assert.assertTrue;
public class ReachShaperTest {
private static final long SEED = 1337L;
private static final WindowKey WINDOW = new WindowKey(0, 0);
private static final int SEA_LEVEL = 40;
private static final int MIN_Y = -64;
private static final int MAX_Y = 320;
@@ -288,7 +290,7 @@ public class ReachShaperTest {
for (int cell = 0; cell < quadrupled.length; cell++) {
quadrupled[cell] *= 4f;
}
ShapedNetwork wider = new ReachShaper(settings).shape(network.grid(), network.receivers(), quadrupled, network.channels(),
ShapedNetwork wider = new ReachShaper(settings).shape(WINDOW, network.grid(), network.receivers(), quadrupled, network.channels(),
network.ponds(), network.fine(), SEED);
Map<Long, Reach> widerById = byId(wider.reaches());
Map<Long, Integer> incoming = incoming(shaped.reaches());
@@ -397,11 +399,11 @@ public class ReachShaperTest {
Pond pond = new Pond(31L, PondKind.NATURAL, 99, 94, 104, 64, 100f, disk(96, 64, 5), 0L, 0, "water", 96, 64);
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> Math.hypot(x - 96D, z - 64D) <= 5D ? 94D
: x <= 104D ? 100D : 100D - 0.5D * (x - 104D)), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(flat, receivers, accumulation, channels, List.of(pond), fine, SEED);
ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, flat, receivers, accumulation, channels, List.of(pond), fine, SEED);
Pond shapedPond = shaped.ponds().get(0);
assertEquals(99, shapedPond.level());
assertOutletLeavesAtThePondLevel("lip outlet", shapedPond, byId(shaped.reaches()).get(shapedPond.outletReachId()));
assertEquals(99, PondChannelSettlement.settle(shaped, SEA_LEVEL).ponds().get(0).level());
assertEquals(99, PondChannelSettlement.settle(WINDOW, shaped, SEA_LEVEL).ponds().get(0).level());
}
@Test
@@ -429,7 +431,7 @@ public class ReachShaperTest {
PondPlanner planner = new PondPlanner(settings);
ReachShaper shaper = new ReachShaper(settings);
List<Pond> water = List.of(new Pond(41L, PondKind.NATURAL, 99, 93, 104, 64, 100f, disk(96, 64, 5), 0L, 0, "water", 96, 64));
ShapedNetwork network = PondChannelSettlement.settle(shaper.shape(flat, receivers, accumulation, channels, water, fine, SEED), SEA_LEVEL);
ShapedNetwork network = PondChannelSettlement.settle(WINDOW, shaper.shape(WINDOW, flat, receivers, accumulation, channels, water, fine, SEED), SEA_LEVEL);
int lowerings = 0;
while (network.ponds().get(0).level() < water.get(0).level()) {
lowerings++;
@@ -437,7 +439,7 @@ public class ReachShaperTest {
Pond reflooded = planner.reflood(flat, fine, CupSet.EMPTY, network.ponds().get(0));
assertNotNull("reflooded pond at " + network.ponds().get(0).level(), reflooded);
water = List.of(reflooded.withOutletReachId(0L));
network = PondChannelSettlement.settle(shaper.shape(flat, receivers, accumulation, channels, water, fine, SEED), SEA_LEVEL);
network = PondChannelSettlement.settle(WINDOW, shaper.shape(WINDOW, flat, receivers, accumulation, channels, water, fine, SEED), SEA_LEVEL);
}
Pond settled = network.ponds().get(0);
Map<Long, Reach> byId = byId(network.reaches());
@@ -474,7 +476,7 @@ public class ReachShaperTest {
assertFalse("no DEM node inside the pond", mask.contains(32, 40) || mask.contains(32, 48));
Pond pond = new Pond(51L, PondKind.NATURAL, 99, 94, 32, 34, 100f, mask, 0L, 0, "water", 32, 43);
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> 100D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(flat, receivers, accumulation, channels, List.of(pond), fine, SEED);
ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, flat, receivers, accumulation, channels, List.of(pond), fine, SEED);
Pond shapedPond = shaped.ponds().get(0);
Reach outlet = byId(shaped.reaches()).get(shapedPond.outletReachId());
assertNotNull(outlet);
@@ -485,7 +487,7 @@ public class ReachShaperTest {
assertEquals("held grade at " + point, shapedPond.level(), outlet.grade()[point]);
}
assertOutletTouchesAtThePondLevel("fallback outlet", shapedPond, outlet);
assertEquals(99, PondChannelSettlement.settle(shaped, SEA_LEVEL).ponds().get(0).level());
assertEquals(99, PondChannelSettlement.settle(WINDOW, shaped, SEA_LEVEL).ponds().get(0).level());
}
@Test
@@ -503,7 +505,7 @@ public class ReachShaperTest {
Pond side = new Pond(62L, PondKind.NATURAL, 99, 94, 104, 60, 100f, disk(104, 56, 3), 0L, 0, "water", 104, 56);
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> fed.mask().contains((int) x, (int) z)
|| side.mask().contains((int) x, (int) z) ? 94D : 100D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(flat, receivers, accumulation, channels, List.of(fed, side), fine, SEED);
ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, flat, receivers, accumulation, channels, List.of(fed, side), fine, SEED);
assertEquals("both ponds drain through the same exit", shaped.ponds().get(0).outletReachId(), shaped.ponds().get(1).outletReachId());
Reach outlet = byId(shaped.reaches()).get(shaped.ponds().get(1).outletReachId());
assertNotNull(outlet);
@@ -531,7 +533,7 @@ public class ReachShaperTest {
surface.set(19, 1, 30f);
Pond pond = new Pond(71L, PondKind.NATURAL, 59, 54, 136, 8, 60f, disk(128, 8, 7), 0L, 0, "water", 128, 8);
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> x < 138D ? 60D : 35D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(surface, receivers, accumulation, channels, List.of(pond), fine, SEED);
ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, surface, receivers, accumulation, channels, List.of(pond), fine, SEED);
Pond shapedPond = shaped.ponds().get(0);
Reach outlet = byId(shaped.reaches()).get(shapedPond.outletReachId());
assertNotNull(outlet);
@@ -790,7 +792,7 @@ public class ReachShaperTest {
accumulation[flat.index(12, 10)] = 300f;
Pond pond = new Pond(21L, PondKind.NATURAL, 101, 98, 0, 0, 102f, rectangle(80, 48, 9, 9), 0L, 0, "water", 96, 64);
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> 100D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(flat, receivers, accumulation, channels, List.of(pond), fine, SEED);
ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, flat, receivers, accumulation, channels, List.of(pond), fine, SEED);
Reach outlet = byId(shaped.reaches()).get(shaped.ponds().get(0).outletReachId());
assertNotNull(outlet);
assertTrue(pond.mask().contains(Math.round(outlet.xs()[0]), Math.round(outlet.zs()[0])));
@@ -819,7 +821,7 @@ public class ReachShaperTest {
}
surface.set(width - 1, 1, 30f);
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> 45D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(surface, receivers, accumulation, channels, List.of(), fine, SEED);
ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, surface, receivers, accumulation, channels, List.of(), fine, SEED);
int mouths = 0;
for (Reach reach : shaped.reaches()) {
int last = reach.xs().length - 1;
@@ -856,7 +858,7 @@ public class ReachShaperTest {
Pond sunken = new Pond(77L, PondKind.NATURAL, SEA_LEVEL - 1, SEA_LEVEL - 6, CELL, CELL, SEA_LEVEL + 0.5f,
new PondMask(0, CELL, CELL, 2, 1, new long[]{0b11L}), 0L, 0, "water", 0, CELL);
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> 45D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(surface, receivers, accumulation, channels, List.of(sunken), fine, SEED);
ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, surface, receivers, accumulation, channels, List.of(sunken), fine, SEED);
int mouths = 0;
for (Reach reach : shaped.reaches()) {
for (int grade : reach.grade()) {
@@ -938,7 +940,7 @@ public class ReachShaperTest {
Pond fed = new Pond(32L, PondKind.NATURAL, 100 + maxFill + 4, 98, 192, 128, 100 + maxFill + 5f, rectangle(160, 112, 9, 9), 0L, 0,
"water", 176, 128);
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> 100D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(flat, receivers, accumulation, channels, List.of(looped, fed), fine, SEED);
ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, flat, receivers, accumulation, channels, List.of(looped, fed), fine, SEED);
int inflows = 0;
for (Reach reach : shaped.reaches()) {
if (reach.downstreamId() == 0L && endsIn(reach, fed)) {
@@ -1010,7 +1012,7 @@ public class ReachShaperTest {
channels.set(cell);
}
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> 100D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(flat, receivers, accumulation, channels, List.of(), fine, SEED);
ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, flat, receivers, accumulation, channels, List.of(), fine, SEED);
Map<Long, Reach> byId = byId(shaped.reaches());
int links = 0;
for (Reach reach : shaped.reaches()) {
@@ -1122,7 +1124,7 @@ public class ReachShaperTest {
flow(flat, receivers, accumulation, channels, i, 8, i + 1 < flat.width() ? i + 1 : -1, 8, 6400f);
}
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> 100D), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(flat, receivers, accumulation, channels, List.of(), fine, SEED);
ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, flat, receivers, accumulation, channels, List.of(), fine, SEED);
Map<Long, List<Reach>> feeders = feeders(shaped.reaches());
assertEquals("one confluence " + feeders.keySet(), 1, feeders.size());
Map.Entry<Long, List<Reach>> confluence = feeders.entrySet().iterator().next();
@@ -1173,7 +1175,7 @@ public class ReachShaperTest {
Pond pond = new Pond(71L, PondKind.NATURAL, 99, 94, 104, 64, 100f, disk(96, 64, 5), 0L, 0, "water", 96, 64);
HeightLattice fine = new HeightLattice(new Terrain((x, z) -> Math.hypot(x - 96D, z - 64D) <= 5D ? 94D
: x <= 104D ? 100D : 100D - 0.5D * (x - 104D)), FINE, false, 1 << 16);
ShapedNetwork shaped = new ReachShaper(settings).shape(flat, receivers, accumulation, channels, List.of(pond), fine, SEED);
ShapedNetwork shaped = new ReachShaper(settings).shape(WINDOW, flat, receivers, accumulation, channels, List.of(pond), fine, SEED);
Reach outlet = byId(shaped.reaches()).get(shaped.ponds().get(0).outletReachId());
assertNotNull("the pond drains through an outlet", outlet);
assertEquals("the outlet is a river", "river", outlet.tier().name());
@@ -1797,7 +1799,7 @@ public class ReachShaperTest {
}
private static ShapedNetwork shape(HydrologySettings settings, Network network) {
return new ReachShaper(settings).shape(network.grid(), network.receivers(), network.accumulation(), network.channels(),
return new ReachShaper(settings).shape(WINDOW, network.grid(), network.receivers(), network.accumulation(), network.channels(),
network.ponds(), network.fine(), SEED);
}
@@ -156,7 +156,7 @@ public final class WaterScene {
ownedPonds.add(pond);
}
}
return new IndexedWindow(new WindowPlan(key, owned, ownedPonds, List.of()), ReachIndex.build(owned, ownedPonds));
return new IndexedWindow(new WindowPlan(key, owned, ownedPonds, List.of()), ReachIndex.build(key, owned, ownedPonds));
});
}
@@ -87,7 +87,7 @@ final class HydrologyPlanHarness {
double started = cpuMillis();
try {
WindowPlan plan = planners.windows().plan(key, needed);
windows.put(key, new IndexedWindow(plan, ReachIndex.build(plan.reaches(), plan.ponds())));
windows.put(key, new IndexedWindow(plan, ReachIndex.build(plan.key(), plan.reaches(), plan.ponds())));
} catch (RuntimeException failure) {
failures.add("window " + key.wx() + "," + key.wz() + " plan: " + cause(failure));
}