Bound planned search by recorded

- nearestRiver limits the planned search to the recorded water's distance, rounded up, when generated chunks hold a match
- Explored areas end with a confirmed Found instead of waiting out the 30 s timeout; nearer planned water inside the bound still wins
- Tests cover the bounded radius in queries and a bounded search that finishes under a short timeout when nothing near is eligible
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-01 21:57:14 -05:00
1 parent 9afd70e69f
commit c0169d6dcd
3 files changed
+68 -3

No files matched your search

@@ -215,9 +215,10 @@ public final class GenerationSemanticQueries {
return new RiverSearch.Missing(unavailable.map(reason -> riverUnavailableMessage(requiredType, reason))
.orElseGet(() -> riverNotFoundMessage(requiredType, maximumDistance)));
}
int activeDistance = recorded == null ? maximumDistance : Math.min(maximumDistance, distanceCeiling(blockX, blockZ, recorded));
RiverResult active;
try {
active = activeRiver(requiredEngine, history, requiredType, blockX, blockZ, maximumDistance, running);
active = activeRiver(requiredEngine, history, requiredType, blockX, blockZ, activeDistance, running);
} catch (HydrologySearchTimeoutException timedOut) {
RiverResult planned = timedOut.nearestSoFar().map(feature -> activeResult(requiredEngine, history, feature)).orElse(null);
return stoppedSearch(requiredType, nearer(blockX, blockZ, recorded, planned));
@@ -247,6 +248,12 @@ public final class GenerationSemanticQueries {
}
}
private static int distanceCeiling(int blockX, int blockZ, RiverResult feature) {
BigInteger squared = horizontalDistanceSquared(blockX, blockZ, feature.x(), feature.z());
BigInteger root = squared.sqrt();
return (root.multiply(root).equals(squared) ? root : root.add(BigInteger.ONE)).intValueExact();
}
private static RiverResult nearer(int blockX, int blockZ, RiverResult recorded, RiverResult active) {
if (recorded == null) {
return active;
@@ -122,6 +122,22 @@ public class HydrologyFeatureSearchTest {
assertEquals(Optional.of(new HydrologyFeatureRef(CHANNEL, HydrologyFeatureType.STREAM, 100, 70, 100)), stopped.nearestSoFar());
}
@Test(timeout = 20_000L)
public void boundedRadiusFinishesWhenNothingNearIsEligible() throws Exception {
HydrologySettings settings = settings(config -> {
});
Planners planners = new Planners(gate);
planners.channelTier = settings.tiers().getFirst();
planners.openRing = 2;
HydrologyPlanCache cache = cache(settings, planners);
Duration timeout = Duration.ofSeconds(2L);
assertEquals(Optional.empty(), new HydrologyFeatureSearch(settings, cache, 0, 0, HydrologyFeatureQuery.of(HydrologyFeatureType.STREAM),
feature -> false, 50).nearest(timeout, () -> true));
assertThrows(HydrologySearchTimeoutException.class, () -> new HydrologyFeatureSearch(settings, cache, 0, 0,
HydrologyFeatureQuery.of(HydrologyFeatureType.STREAM), feature -> false, 7168).nearest(timeout, () -> true));
}
@Test(timeout = 20_000L)
public void cancelledSearchStopsWaitingForPlans() {
HydrologySettings settings = settings(config -> {
@@ -192,6 +208,7 @@ public class HydrologyFeatureSearchTest {
private final CountDownLatch gate;
private final AtomicInteger calls = new AtomicInteger();
private volatile HydrologyTier channelTier;
private volatile int openRing = -1;
private Planners(CountDownLatch gate) {
this.gate = gate;
@@ -207,7 +224,9 @@ public class HydrologyFeatureSearchTest {
if (channelTier != null && key.equals(new WindowKey(0, 0))) {
return new WindowPlan(key, List.of(channel(channelTier)), List.of(), List.of());
}
hold();
if (Math.max(Math.abs(key.wx()), Math.abs(key.wz())) > openRing) {
hold();
}
return new WindowPlan(key, List.of(), List.of(), List.of());
}
@@ -190,7 +190,7 @@ public final class GenerationSemanticQueriesTest {
when(history.semantics(anyInt(), anyInt())).thenReturn(Optional.empty());
when(history.activeActivation()).thenReturn(activation);
when(activation.activationId()).thenReturn(2L);
when(runtime.nearestFeature(eq(0), eq(0), eq(HydrologyFeatureQuery.of(HydrologyFeatureType.RIVER)), eq(200), any(), any()))
when(runtime.nearestFeature(eq(0), eq(0), eq(HydrologyFeatureQuery.of(HydrologyFeatureType.RIVER)), eq(100), any(), any()))
.thenAnswer(invocation -> firstEligible(List.of(tapered, eligible), invocation.getArgument(4)));
GenerationSemanticQueries.RiverResult active = found(GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.RIVER, 0, 0, 200, () -> true));
@@ -372,6 +372,45 @@ public final class GenerationSemanticQueriesTest {
assertTrue(planned.message(), planned.message().contains("at 20, -44, 0;"));
}
@Test
public void recordedWaterBoundsThePlannedSearchSoExploredAreasFinishEarly() throws Exception {
IrisEngine engine = mock(IrisEngine.class);
IrisComplex complex = mock(IrisComplex.class);
HydrologyRuntime runtime = mock(HydrologyRuntime.class);
IrisDimension dimension = mock(IrisDimension.class);
GenerationHistory history = mock(GenerationHistory.class);
GenerationHistoryRuntimeRouter router = mock(GenerationHistoryRuntimeRouter.class);
GenerationActivation activation = mock(GenerationActivation.class);
ChunkGenerationSemantics.RiverFeatureOccurrence occurrence = new ChunkGenerationSemantics.RiverFeatureOccurrence(
"water", HydrologyFeatureType.RIVER, 7L, new ChunkGenerationSemantics.BlockPosition(30, 70, 40));
when(engine.getComplex()).thenReturn(complex);
when(engine.getDimension()).thenReturn(dimension);
when(dimension.getMinHeight()).thenReturn(-64);
when(complex.getHydrologyRuntime()).thenReturn(runtime);
when(engine.getGenerationHistoryRuntimeRouter()).thenReturn(Optional.of(router));
when(router.history()).thenReturn(history);
when(history.activeActivation()).thenReturn(activation);
when(activation.activationId()).thenReturn(2L);
when(history.findRecordedRiver(any())).thenReturn(Optional.of(
new GenerationSemanticIndex.RiverMatch(occurrence, new GenerationSemanticIndex.ChunkReference(1, 2, 1L))));
when(runtime.nearestFeature(eq(0), eq(0), any(), eq(7168), any(), any()))
.thenThrow(new HydrologySearchTimeoutException(Duration.ofSeconds(30L), null));
when(runtime.nearestFeature(eq(0), eq(0), any(), eq(50), any(), any())).thenReturn(Optional.empty());
GenerationSemanticQueries.RiverResult recorded = found(
GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.RIVER, 0, 0, 7168, () -> true));
assertEquals(GenerationSemanticQueries.RiverSource.RECORDED, recorded.source());
assertEquals(7L, recorded.featureId());
when(runtime.nearestFeature(eq(0), eq(0), any(), eq(50), any(), any())).thenReturn(Optional.of(feature(6L, 20, 100, 30)));
GenerationSemanticQueries.RiverResult planned = found(
GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.RIVER, 0, 0, 7168, () -> true));
assertEquals(GenerationSemanticQueries.RiverSource.ACTIVE_PREDICTION, planned.source());
assertEquals(6L, planned.featureId());
}
@Test
public void aNewSearchCancelsThePlayersPreviousSearch() throws Exception {
IrisEngine engine = mock(IrisEngine.class);