Plan curves across windows

- Decide which falls curve from the plan reach, so every chunk and settlement agree
- Keep a clamped drop under the curve minimum straight
- Test feeder falls across a window line on 24 seeds
This commit is contained in:
Brian Fopiano committed 2026-11-03 09:52:42 -05:00
1 parent c4a076b587
commit 6e2cb959dd
3 files changed
+74 -7

No files matched your search

@@ -17,6 +17,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -40,8 +41,9 @@ final class ChunkFeatures {
private final MaskEdges lakeReach = new MaskEdges(MaskEdges.Kind.GROWN_WATER);
private boolean inert;
private ChunkFeatures(HydrologySettings settings, List<Reach> reaches, List<Pond> ponds, List<Pond> lakes, List<SeaInlet> inlets, int[] inletBoxes) {
this.brinks = new ReachGeometry.Lookup(settings.seed(), settings.seaLevel());
private ChunkFeatures(HydrologySettings settings, List<Reach> reaches, Map<Reach, Reach> plans, List<Pond> ponds, List<Pond> lakes,
List<SeaInlet> inlets, int[] inletBoxes) {
this.brinks = new ReachGeometry.Lookup(settings.seed(), settings.seaLevel(), plans);
this.reaches = reaches;
this.ponds = ponds;
this.lakes = lakes;
@@ -54,6 +56,7 @@ final class ChunkFeatures {
List<RegionPlan> lakeRegionPlans, List<RegionKey> lakeRegions) {
Links links = new Links(windowsByKey(chunkX, chunkZ, windows), settings.seaLevel());
List<Reach> reaches = new ArrayList<>();
Map<Reach, Reach> plans = new IdentityHashMap<>();
List<Pond> ponds = new ArrayList<>();
List<SeaInlet> inlets = new ArrayList<>();
int[] inletBoxes = new int[0];
@@ -71,11 +74,14 @@ final class ChunkFeatures {
int before = reaches.size();
window.index().candidates(chunkX, chunkZ, halo, reaches, ponds);
for (int index = before; index < reaches.size(); index++) {
reaches.set(index, links.resolve(reaches.get(index), window.plan().key()));
Reach plan = reaches.get(index);
Reach linked = links.resolve(plan, window.plan().key());
reaches.set(index, linked);
plans.put(linked, plan);
}
}
List<Pond> lakes = lakes(chunkX, chunkZ, halo, lakeRegionPlans, lakeRegions);
ChunkFeatures features = new ChunkFeatures(settings, reaches, ponds, lakes, inlets, inletBoxes);
ChunkFeatures features = new ChunkFeatures(settings, reaches, plans, ponds, lakes, inlets, inletBoxes);
features.index(settings, chunkX, chunkZ);
return features;
}
@@ -33,7 +33,7 @@ final class ReachGeometry {
this.starts = starts;
}
static ReachGeometry of(Reach reach, long seed, int seaLevel) {
static ReachGeometry of(Reach reach, Reach linked, long seed, int seaLevel) {
int[] grade = reach.grade();
int[] steps = new int[grade.length - 1];
int count = 0;
@@ -65,7 +65,7 @@ final class ReachGeometry {
double middle = middle(along, segment);
int upper = WaterHeads.snap(grade[segment], seaLevel);
int lower = WaterHeads.snap(grade[segment + 1], seaLevel);
if (grade[segment] - grade[segment + 1] < BRINK_MIN_DROP || middle <= held || outlet && upper == start || feeder && lower == landing
if (grade[segment] - grade[segment + 1] < BRINK_MIN_DROP || linked.grade()[segment] - linked.grade()[segment + 1] < BRINK_MIN_DROP || middle <= held || outlet && upper == start || feeder && lower == landing
|| reach.style()[segment] == ReachStyle.MOUTH || reach.style()[segment + 1] == ReachStyle.MOUTH) {
continue;
}
@@ -170,18 +170,25 @@ final class ReachGeometry {
static final class Lookup {
private final Map<Reach, ReachGeometry> geometries = new IdentityHashMap<>();
private final Map<Reach, Reach> plans;
private final long seed;
private final int seaLevel;
Lookup(long seed, int seaLevel) {
this(seed, seaLevel, Map.of());
}
Lookup(long seed, int seaLevel, Map<Reach, Reach> plans) {
this.seed = seed;
this.seaLevel = seaLevel;
this.plans = plans;
}
ReachGeometry of(Reach reach) {
ReachGeometry known = geometries.get(reach);
if (known == null) {
known = ReachGeometry.of(reach, seed, seaLevel);
Reach plan = plans.get(reach);
known = ReachGeometry.of(plan == null ? reach : plan, reach, seed, seaLevel);
geometries.put(reach, known);
}
return known;
@@ -1733,6 +1733,60 @@ public class ChunkWaterRasterTest {
assertTrue("curved cells " + curved, curved > 100);
}
@Test
public void aFeedersFallsCurveTheSameInEveryChunkAcrossAWindowLine() {
assertEquals(new WindowKey(0, 0), WindowKey.containing(508, 200));
assertEquals(new WindowKey(1, 0), WindowKey.containing(700, 200));
assertEquals(new WindowKey(2, 0), WindowKey.containing(1100, 200));
int cells = 0;
for (float width : new float[]{4f, 8f, 12f, 20f}) {
HydrologyTier tier = tier("river", width, 0.5f, 8, IrisBankStyle.SMOOTH);
for (int fall : new int[]{498, 502, 506, 508, 512, 516}) {
Reach downstream = line(702L, tier, 0L, 700f, 200f, 1100f, 200f, (x, z) -> 94);
Reach feeder = line(701L, tier, downstream.id(), 440f, 200f, 700f, 200f, (x, z) -> x < fall ? 110 : 98);
for (long seed = 1L; seed <= 24L; seed++) {
cells += assertHeadsFollowThePlan("width " + width + " fall " + fall + " seed " + seed + ": ", feeder, downstream, seed, 480, 540);
}
}
}
assertTrue("cells compared " + cells, cells > 10000);
}
@Test
public void anUpperFallOfAFeederKeepsItsCurveInEveryChunkAcrossAWindowLine() {
HydrologyTier tier = tier("river", 12f, 0.5f, 8, IrisBankStyle.SMOOTH);
Reach downstream = line(712L, tier, 0L, 700f, 200f, 1100f, 200f, (x, z) -> 94);
Reach feeder = line(711L, tier, downstream.id(), 440f, 200f, 700f, 200f, (x, z) -> x < 508f ? 122 : x < 600f ? 110 : 98);
int curved = 0;
for (long seed = 1L; seed <= 24L; seed++) {
assertHeadsFollowThePlan("seed " + seed + ": ", feeder, downstream, seed, 480, 540);
BrinkRule rule = new BrinkRule(seed, feeder);
for (int x = 480; x <= 540; x++) {
curved += rule.head(x, 200) != rule.straightHead(x, 200) ? 1 : 0;
}
}
assertTrue("the fall above the last one curves: " + curved, curved > 0);
}
private static int assertHeadsFollowThePlan(String label, Reach feeder, Reach downstream, long seed, int minX, int maxX) {
Scene scene = new Scene(seededSettings(seed), (x, z) -> 130).reach(feeder).reach(downstream);
BrinkRule plan = new BrinkRule(seed, feeder);
int reach = (int) feeder.halfWidth()[0];
int cells = 0;
for (int z = 200 - reach; z <= 200 + reach; z++) {
int previous = Integer.MAX_VALUE;
for (int x = minX; x <= maxX; x++) {
if (scene.water(x, z)) {
assertEquals(label + "head at " + x + "," + z, plan.head(x, z), scene.head(x, z));
assertTrue(label + "water rises from " + previous + " to " + scene.head(x, z) + " at " + x + "," + z, scene.head(x, z) <= previous);
previous = scene.head(x, z);
cells++;
}
}
}
return cells;
}
private static Reach withStartStyle(Reach reach, ReachStyle style, int points) {
ReachStyle[] styles = reach.style().clone();
for (int point = 0; point < points; point++) {