mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-29 17:23:59 +00:00
Bound find river search
- HydrologyFeatureSearch stops after its timeout or when the caller cancels, waiting on plans in 50 ms slices - Types the dimension cannot form answer at once without planning, naming the setting that turns them off - channelType reads the tier name (stream, river, major or major_river) and falls back to rank, so one-tier packs match stream - HydrologyRuntime adds unavailable(type) and passes FEATURE_SEARCH_TIMEOUT (30 s) and a running check to the search - nearestRiver returns a RiverSearch outcome, reports disabled and timed-out searches, and keeps recorded water on timeout - startRiverSearch keeps one search per player; a new search cancels the previous one without a message - Bukkit and modded find river run through the shared per-player search - Tier name description and schema digest follow the new tier naming - HydrologyFeatureSearchTest plus new query, Bukkit and modded cases cover the bounds
This commit is contained in:
1 parent
75d28d16b2
commit
474b1e873a
12 files changed
+826
-167
No files matched your search
+230
@@ -0,0 +1,230 @@
|
||||
package art.arcane.iris.generation.hydrology.engine;
|
||||
|
||||
import art.arcane.iris.generation.hydrology.config.IrisHydrology;
|
||||
import art.arcane.iris.generation.hydrology.config.IrisHydrologyTier;
|
||||
import art.arcane.iris.generation.hydrology.plan.HydrologyPlanCache;
|
||||
import art.arcane.iris.generation.hydrology.plan.HydrologySettings;
|
||||
import art.arcane.iris.generation.hydrology.plan.RegionKey;
|
||||
import art.arcane.iris.generation.hydrology.plan.RegionPlan;
|
||||
import art.arcane.iris.generation.hydrology.plan.WindowKey;
|
||||
import art.arcane.iris.generation.hydrology.plan.WindowPlan;
|
||||
import art.arcane.iris.generation.hydrology.query.HydrologyFeatureQuery;
|
||||
import art.arcane.iris.generation.hydrology.query.HydrologyFeatureRef;
|
||||
import art.arcane.iris.generation.hydrology.query.HydrologyFeatureType;
|
||||
import art.arcane.iris.generation.hydrology.water.HydrologyTier;
|
||||
import art.arcane.iris.generation.hydrology.water.Reach;
|
||||
import art.arcane.iris.generation.hydrology.water.ReachStyle;
|
||||
import art.arcane.iris.testsupport.FakeHydrology;
|
||||
import art.arcane.volmlib.util.collection.KList;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class HydrologyFeatureSearchTest {
|
||||
private static final long SEED = 1337L;
|
||||
private static final long CHANNEL = 0x4348414eL;
|
||||
private static final Duration LONG_TIMEOUT = Duration.ofSeconds(30L);
|
||||
|
||||
private final List<ExecutorService> pools = new ArrayList<>();
|
||||
private final List<HydrologyPlanCache> caches = new ArrayList<>();
|
||||
private final CountDownLatch gate = new CountDownLatch(1);
|
||||
|
||||
@After
|
||||
public void shutDown() {
|
||||
gate.countDown();
|
||||
for (HydrologyPlanCache cache : caches) {
|
||||
cache.close();
|
||||
}
|
||||
for (ExecutorService pool : pools) {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test(timeout = 20_000L)
|
||||
public void disabledTypeAnswersImmediately() throws Exception {
|
||||
HydrologySettings settings = settings(config -> config.getLakes().setEnabled(false));
|
||||
Planners planners = new Planners(gate);
|
||||
HydrologyPlanCache cache = cache(settings, planners);
|
||||
|
||||
assertEquals(Optional.empty(), search(settings, cache, HydrologyFeatureQuery.of(HydrologyFeatureType.LAKE))
|
||||
.nearest(LONG_TIMEOUT, () -> true));
|
||||
assertEquals(Optional.empty(), search(settings, cache, HydrologyFeatureQuery.of(HydrologyFeatureType.LAKE, HydrologyFeatureType.DAM))
|
||||
.nearest(LONG_TIMEOUT, () -> true));
|
||||
assertEquals(0, planners.calls.get());
|
||||
assertEquals(Optional.of("hydrology.lakes is disabled"), HydrologyFeatureSearch.disabledReason(settings, HydrologyFeatureType.LAKE));
|
||||
assertEquals(Optional.of("hydrology.ponds.dams is disabled"), HydrologyFeatureSearch.disabledReason(settings, HydrologyFeatureType.DAM));
|
||||
assertEquals(Optional.empty(), HydrologyFeatureSearch.disabledReason(settings, HydrologyFeatureType.POND));
|
||||
assertEquals(Optional.empty(), HydrologyFeatureSearch.disabledReason(settings, HydrologyFeatureType.WATERFALL));
|
||||
|
||||
HydrologySettings dry = settings(config -> {
|
||||
config.getPonds().getNatural().setEnabled(false);
|
||||
config.getPonds().getSeeded().setEnabled(false);
|
||||
config.getPonds().getDams().setEnabled(true);
|
||||
config.getUnderground().getSeaInlets().setEnabled(false);
|
||||
config.getUnderground().getCavePools().setEnabled(false);
|
||||
});
|
||||
String ponds = "hydrology.ponds.natural and hydrology.ponds.seeded are disabled";
|
||||
assertEquals(Optional.of(ponds), HydrologyFeatureSearch.disabledReason(dry, HydrologyFeatureType.POND));
|
||||
assertEquals(Optional.of(ponds), HydrologyFeatureSearch.disabledReason(dry, HydrologyFeatureType.DAM));
|
||||
assertEquals(Optional.of("hydrology.underground.seaInlets is disabled"),
|
||||
HydrologyFeatureSearch.disabledReason(dry, HydrologyFeatureType.SEA_INLET));
|
||||
assertEquals(Optional.of("hydrology.underground.cavePools is disabled"),
|
||||
HydrologyFeatureSearch.disabledReason(dry, HydrologyFeatureType.CAVE_POOL));
|
||||
assertEquals(Optional.empty(), HydrologyFeatureSearch.disabledReason(dry, HydrologyFeatureType.LAKE));
|
||||
assertEquals(Optional.of("hydrology is off"), FakeHydrology.disabled().unavailable(HydrologyFeatureType.RIVER));
|
||||
}
|
||||
|
||||
@Test(timeout = 20_000L)
|
||||
public void searchStopsAtTheTimeout() {
|
||||
HydrologySettings settings = settings(config -> {
|
||||
});
|
||||
Planners planners = new Planners(gate);
|
||||
HydrologyPlanCache cache = cache(settings, planners);
|
||||
Duration timeout = Duration.ofMillis(200L);
|
||||
HydrologyFeatureSearch search = search(settings, cache, HydrologyFeatureQuery.of(HydrologyFeatureType.POND));
|
||||
|
||||
long started = System.nanoTime();
|
||||
assertThrows(TimeoutException.class, () -> search.nearest(timeout, () -> true));
|
||||
|
||||
assertTrue(System.nanoTime() - started >= timeout.toNanos());
|
||||
assertTrue(planners.calls.get() > 0);
|
||||
}
|
||||
|
||||
@Test(timeout = 20_000L)
|
||||
public void cancelledSearchStopsWaitingForPlans() {
|
||||
HydrologySettings settings = settings(config -> {
|
||||
});
|
||||
Planners planners = new Planners(gate);
|
||||
HydrologyPlanCache cache = cache(settings, planners);
|
||||
AtomicInteger checks = new AtomicInteger();
|
||||
|
||||
assertThrows(CancellationException.class, () -> search(settings, cache, HydrologyFeatureQuery.of(HydrologyFeatureType.RIVER))
|
||||
.nearest(LONG_TIMEOUT, () -> checks.incrementAndGet() < 3));
|
||||
assertTrue(checks.get() >= 3);
|
||||
}
|
||||
|
||||
@Test(timeout = 20_000L)
|
||||
public void oneTierPacksMatchStream() throws Exception {
|
||||
HydrologySettings oneTier = settings(config -> config.setTiers(new KList<>(new IrisHydrologyTier())));
|
||||
HydrologyTier only = oneTier.tiers().getFirst();
|
||||
Planners planners = new Planners(null);
|
||||
planners.channelTier = only;
|
||||
HydrologyPlanCache cache = cache(oneTier, planners);
|
||||
|
||||
assertEquals(HydrologyFeatureType.STREAM, HydrologyFeatureSearch.channelType(oneTier, only));
|
||||
assertEquals(new HydrologyFeatureRef(CHANNEL, HydrologyFeatureType.STREAM, 100, 70, 100),
|
||||
search(oneTier, cache, HydrologyFeatureQuery.of(HydrologyFeatureType.STREAM)).nearest(LONG_TIMEOUT, () -> true).orElseThrow());
|
||||
assertEquals(Optional.of("it has no river tier"), HydrologyFeatureSearch.disabledReason(oneTier, HydrologyFeatureType.RIVER));
|
||||
assertEquals(Optional.of("it has no major river tier"), HydrologyFeatureSearch.disabledReason(oneTier, HydrologyFeatureType.MAJOR_RIVER));
|
||||
|
||||
HydrologySettings creek = settings(config -> config.setTiers(new KList<>(new IrisHydrologyTier().setName("creek"))));
|
||||
assertEquals(HydrologyFeatureType.STREAM, HydrologyFeatureSearch.channelType(creek, creek.tiers().getFirst()));
|
||||
|
||||
HydrologySettings twoTier = settings(config -> config.setTiers(new KList<>(new IrisHydrologyTier(),
|
||||
new IrisHydrologyTier().setName("river").setMinCatchment(20000D))));
|
||||
assertEquals(HydrologyFeatureType.STREAM, HydrologyFeatureSearch.channelType(twoTier, twoTier.tiers().getFirst()));
|
||||
assertEquals(HydrologyFeatureType.RIVER, HydrologyFeatureSearch.channelType(twoTier, twoTier.majorTier()));
|
||||
assertEquals(Optional.of("it has no major river tier"), HydrologyFeatureSearch.disabledReason(twoTier, HydrologyFeatureType.MAJOR_RIVER));
|
||||
|
||||
HydrologySettings ranked = settings(config -> config.setTiers(new KList<>(new IrisHydrologyTier().setName("brook"),
|
||||
new IrisHydrologyTier().setName("channel").setMinCatchment(20000D),
|
||||
new IrisHydrologyTier().setName("trunk").setMinCatchment(320000D))));
|
||||
assertEquals(HydrologyFeatureType.STREAM, HydrologyFeatureSearch.channelType(ranked, ranked.tiers().get(0)));
|
||||
assertEquals(HydrologyFeatureType.RIVER, HydrologyFeatureSearch.channelType(ranked, ranked.tiers().get(1)));
|
||||
assertEquals(HydrologyFeatureType.MAJOR_RIVER, HydrologyFeatureSearch.channelType(ranked, ranked.tiers().get(2)));
|
||||
}
|
||||
|
||||
private HydrologyPlanCache cache(HydrologySettings settings, Planners planners) {
|
||||
ExecutorService pool = Executors.newFixedThreadPool(2, runnable -> {
|
||||
Thread thread = new Thread(runnable, "feature-search-planner");
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
pools.add(pool);
|
||||
HydrologyPlanCache cache = new HydrologyPlanCache(settings, planners::region, planners::window, pool, () -> false);
|
||||
caches.add(cache);
|
||||
return cache;
|
||||
}
|
||||
|
||||
private static HydrologyFeatureSearch search(HydrologySettings settings, HydrologyPlanCache cache, HydrologyFeatureQuery query) {
|
||||
return new HydrologyFeatureSearch(settings, cache, 0, 0, query, feature -> true, 2048);
|
||||
}
|
||||
|
||||
private static HydrologySettings settings(Consumer<IrisHydrology> change) {
|
||||
IrisHydrology config = new IrisHydrology().setEnabled(true).setPersist(false);
|
||||
change.accept(config);
|
||||
return HydrologySettings.compile(config, SEED, 40, 0, 384);
|
||||
}
|
||||
|
||||
private static final class Planners {
|
||||
private final CountDownLatch gate;
|
||||
private final AtomicInteger calls = new AtomicInteger();
|
||||
private volatile HydrologyTier channelTier;
|
||||
|
||||
private Planners(CountDownLatch gate) {
|
||||
this.gate = gate;
|
||||
}
|
||||
|
||||
RegionPlan region(RegionKey key) {
|
||||
calls.incrementAndGet();
|
||||
hold();
|
||||
return new RegionPlan(key, List.of(), List.of());
|
||||
}
|
||||
|
||||
WindowPlan window(WindowKey key, List<RegionPlan> plans) {
|
||||
calls.incrementAndGet();
|
||||
hold();
|
||||
if (channelTier != null && key.equals(new WindowKey(0, 0))) {
|
||||
return new WindowPlan(key, List.of(channel(channelTier)), List.of(), List.of());
|
||||
}
|
||||
return new WindowPlan(key, List.of(), List.of(), List.of());
|
||||
}
|
||||
|
||||
private void hold() {
|
||||
if (gate == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
gate.await(60L, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException interruption) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new CancellationException("Planning interrupted.");
|
||||
}
|
||||
}
|
||||
|
||||
private static Reach channel(HydrologyTier tier) {
|
||||
int points = 3;
|
||||
float[] xs = new float[points];
|
||||
float[] zs = new float[points];
|
||||
int[] grade = new int[points];
|
||||
float[] halfWidth = new float[points];
|
||||
float[] depth = new float[points];
|
||||
ReachStyle[] style = new ReachStyle[points];
|
||||
for (int index = 0; index < points; index++) {
|
||||
xs[index] = 100f + index * 50f;
|
||||
zs[index] = 100f;
|
||||
grade[index] = 70;
|
||||
halfWidth[index] = 1f;
|
||||
depth[index] = 1f;
|
||||
style[index] = ReachStyle.RUN;
|
||||
}
|
||||
return new Reach(CHANNEL, tier, 0L, xs, zs, grade, halfWidth, depth, style, 2000f);
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
-11
@@ -62,8 +62,10 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
@@ -369,11 +371,11 @@ public class HydrologyRuntimeTest {
|
||||
HydrologyRuntime runtime = runtime(planners, () -> false);
|
||||
|
||||
HydrologyFeatureRef seeded = runtime.nearestFeature(0, 0, HydrologyFeatureQuery.of(HydrologyFeatureType.POND), 2048,
|
||||
feature -> feature.id() != POND).orElseThrow();
|
||||
feature -> feature.id() != POND, () -> true).orElseThrow();
|
||||
|
||||
assertEquals(POND + 1, seeded.id());
|
||||
assertEquals(Optional.empty(), runtime.nearestFeature(0, 0, HydrologyFeatureQuery.of(HydrologyFeatureType.POND), 2048,
|
||||
feature -> false));
|
||||
feature -> false, () -> true));
|
||||
}
|
||||
|
||||
@Test(timeout = 180_000L)
|
||||
@@ -822,14 +824,13 @@ public class HydrologyRuntimeTest {
|
||||
CountDownLatch regions = new CountDownLatch(1);
|
||||
planners.regionGate = regions;
|
||||
int before = planners.regionArrivals.get();
|
||||
AtomicReference<Optional<HydrologyFeatureRef>> lake = new AtomicReference<>();
|
||||
Thread search = new Thread(() -> lake.set(runtime.nearestFeature(-20_000, -20_000,
|
||||
HydrologyFeatureQuery.of(HydrologyFeatureType.LAKE), 64)));
|
||||
FutureTask<Optional<HydrologyFeatureRef>> lake = new FutureTask<>(() -> runtime.nearestFeature(-20_000, -20_000,
|
||||
HydrologyFeatureQuery.of(HydrologyFeatureType.LAKE), 64));
|
||||
Thread search = new Thread(lake);
|
||||
search.start();
|
||||
await(() -> planners.regionArrivals.get() - before >= 4, "every lake region to plan at once");
|
||||
regions.countDown();
|
||||
search.join(TimeUnit.SECONDS.toMillis(TIMEOUT_SECONDS));
|
||||
assertEquals(Optional.empty(), lake.get());
|
||||
assertEquals(Optional.empty(), lake.get(TIMEOUT_SECONDS, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
@Test(timeout = 180_000L)
|
||||
@@ -878,7 +879,7 @@ public class HydrologyRuntimeTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createStaysDisabledWithoutHydrology() {
|
||||
public void createStaysDisabledWithoutHydrology() throws Exception {
|
||||
Engine engine = mock(Engine.class);
|
||||
IrisComplex complex = mock(IrisComplex.class);
|
||||
IrisDimension dimension = new IrisDimension();
|
||||
@@ -895,7 +896,7 @@ public class HydrologyRuntimeTest {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void createChecksEveryReachableBiomeFluidWhileCavePoolsAreEnabled() {
|
||||
public void createChecksEveryReachableBiomeFluidWhileCavePoolsAreEnabled() throws Exception {
|
||||
IrisDimension dimension = new IrisDimension().setRegions(new KList<>("tropical"));
|
||||
dimension.setHydrology(new IrisHydrology().setEnabled(true).setDensity(0D));
|
||||
IrisRegion tropical = new IrisRegion().setLandBiomes(new KList<>("volcanoes"));
|
||||
@@ -924,7 +925,7 @@ public class HydrologyRuntimeTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zeroDensitySkipsRuntimePlanningAfterValidatingSettings() {
|
||||
public void zeroDensitySkipsRuntimePlanningAfterValidatingSettings() throws Exception {
|
||||
Engine engine = mock(Engine.class);
|
||||
IrisComplex complex = mock(IrisComplex.class);
|
||||
IrisDimension dimension = new IrisDimension();
|
||||
@@ -1059,8 +1060,9 @@ public class HydrologyRuntimeTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertDisabled(HydrologyRuntime runtime) {
|
||||
private static void assertDisabled(HydrologyRuntime runtime) throws TimeoutException {
|
||||
assertFalse(runtime.enabled());
|
||||
assertEquals(Optional.of("hydrology is off"), runtime.unavailable(HydrologyFeatureType.RIVER));
|
||||
assertEquals(0L, runtime.previewRevision());
|
||||
assertSame(ChunkWaterRaster.EMPTY, runtime.raster(0, 0));
|
||||
assertSame(ChunkWaterRaster.EMPTY, runtime.rasterSnapshot(0, 0));
|
||||
|
||||
@@ -84,7 +84,7 @@ public class SchemaBuilderIdentityTest {
|
||||
@ClassRule
|
||||
public static final PlatformLeakGuard PLATFORM_GUARD = PlatformLeakGuard.clean();
|
||||
|
||||
private static final String EXPECTED_SCHEMA_DIGEST = "87e3f95ad75204881cdcf766e9a318b69ca9fb365fe0249de112edaa8461dd00";
|
||||
private static final String EXPECTED_SCHEMA_DIGEST = "cc8006323ee6ce8f8cda87ddb7daf2e73fbb832937c9fce0afe4b71c1f1eae37";
|
||||
|
||||
private static final List<Class<?>> SCHEMA_ROOTS = List.of(
|
||||
IrisDimension.class,
|
||||
|
||||
+183
-31
@@ -12,8 +12,17 @@ import art.arcane.iris.generation.terrain.IrisDimension;
|
||||
import org.junit.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -25,9 +34,11 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -135,7 +146,7 @@ public final class GenerationSemanticQueriesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rejectsTaperedPredictionsAndMergesTheNearestEligibleRiver() {
|
||||
public void rejectsTaperedPredictionsAndMergesTheNearestEligibleRiver() throws Exception {
|
||||
IrisEngine engine = mock(IrisEngine.class);
|
||||
IrisComplex complex = mock(IrisComplex.class);
|
||||
HydrologyRuntime runtime = mock(HydrologyRuntime.class);
|
||||
@@ -178,11 +189,10 @@ 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()))
|
||||
when(runtime.nearestFeature(eq(0), eq(0), eq(HydrologyFeatureQuery.of(HydrologyFeatureType.RIVER)), eq(200), any(), any()))
|
||||
.thenAnswer(invocation -> firstEligible(List.of(tapered, eligible), invocation.getArgument(4)));
|
||||
|
||||
GenerationSemanticQueries.RiverResult active = GenerationSemanticQueries.nearestRiver(
|
||||
engine, HydrologyFeatureType.RIVER, 0, 0, 200).orElseThrow();
|
||||
GenerationSemanticQueries.RiverResult active = found(GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.RIVER, 0, 0, 200, () -> true));
|
||||
|
||||
assertEquals(GenerationSemanticQueries.RiverSource.ACTIVE_PREDICTION, active.source());
|
||||
assertEquals(HydrologyFeatureType.RIVER, active.type());
|
||||
@@ -191,10 +201,9 @@ public final class GenerationSemanticQueriesTest {
|
||||
|
||||
HydrologyFeatureRef fartherActive = feature(4L, 120, 20, 0);
|
||||
doAnswer(invocation -> firstEligible(List.of(tapered, fartherActive), invocation.getArgument(4)))
|
||||
.when(runtime).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any());
|
||||
.when(runtime).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any());
|
||||
|
||||
GenerationSemanticQueries.RiverResult historical = GenerationSemanticQueries.nearestRiver(
|
||||
engine, HydrologyFeatureType.RIVER, 0, 0, 200).orElseThrow();
|
||||
GenerationSemanticQueries.RiverResult historical = found(GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.RIVER, 0, 0, 200, () -> true));
|
||||
|
||||
assertEquals(GenerationSemanticQueries.RiverSource.RECORDED, historical.source());
|
||||
assertEquals(100, historical.x());
|
||||
@@ -202,7 +211,7 @@ public final class GenerationSemanticQueriesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void seamWeightIsJudgedPerCandidateInsideOneTransitionChunk() {
|
||||
public void seamWeightIsJudgedPerCandidateInsideOneTransitionChunk() throws Exception {
|
||||
IrisEngine engine = mock(IrisEngine.class);
|
||||
IrisComplex complex = mock(IrisComplex.class);
|
||||
HydrologyRuntime runtime = mock(HydrologyRuntime.class);
|
||||
@@ -220,29 +229,28 @@ public final class GenerationSemanticQueriesTest {
|
||||
when(displacement.seamWeight(anyInt(), anyInt())).thenAnswer(invocation ->
|
||||
invocation.<Integer>getArgument(0) < 8 ? 0.4D : 1D);
|
||||
when(dimension.getMinHeight()).thenReturn(-64);
|
||||
when(runtime.nearestFeature(anyInt(), anyInt(), any(), anyInt(), any()))
|
||||
when(runtime.nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any()))
|
||||
.thenAnswer(invocation -> firstEligible(List.of(tapered, solid), invocation.getArgument(4)));
|
||||
|
||||
GenerationSemanticQueries.RiverResult taperedFirst = GenerationSemanticQueries.nearestRiver(
|
||||
engine, HydrologyFeatureType.RIVER, 0, 0, 200).orElseThrow();
|
||||
GenerationSemanticQueries.RiverResult taperedFirst = found(GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.RIVER, 0, 0, 200, () -> true));
|
||||
doAnswer(invocation -> firstEligible(List.of(solid, tapered), invocation.getArgument(4)))
|
||||
.when(runtime).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any());
|
||||
GenerationSemanticQueries.RiverResult solidFirst = GenerationSemanticQueries.nearestRiver(
|
||||
engine, HydrologyFeatureType.RIVER, 0, 0, 200).orElseThrow();
|
||||
.when(runtime).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any());
|
||||
GenerationSemanticQueries.RiverResult solidFirst = found(GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.RIVER, 0, 0, 200, () -> true));
|
||||
doAnswer(invocation -> {
|
||||
Predicate<HydrologyFeatureRef> eligible = invocation.getArgument(4);
|
||||
assertTrue(eligible.test(solid));
|
||||
assertFalse(eligible.test(tapered));
|
||||
return Optional.empty();
|
||||
}).when(runtime).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any());
|
||||
}).when(runtime).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any());
|
||||
|
||||
assertEquals(2L, taperedFirst.featureId());
|
||||
assertEquals(2L, solidFirst.featureId());
|
||||
assertTrue(GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.RIVER, 0, 0, 200).isEmpty());
|
||||
assertEquals(new GenerationSemanticQueries.RiverSearch.Missing("No river found within 200 blocks."),
|
||||
GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.RIVER, 0, 0, 200, () -> true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsRecordedRiverWhenTheActivePackHasNoHydrology() {
|
||||
public void returnsRecordedRiverWhenTheActivePackHasNoHydrology() throws Exception {
|
||||
IrisEngine engine = mock(IrisEngine.class);
|
||||
IrisComplex complex = mock(IrisComplex.class);
|
||||
HydrologyRuntime runtime = mock(HydrologyRuntime.class);
|
||||
@@ -258,6 +266,7 @@ public final class GenerationSemanticQueriesTest {
|
||||
|
||||
when(engine.getComplex()).thenReturn(complex);
|
||||
when(complex.getHydrologyRuntime()).thenReturn(runtime);
|
||||
when(runtime.unavailable(any())).thenReturn(Optional.of("hydrology is off"));
|
||||
when(engine.getGenerationHistoryRuntimeRouter()).thenReturn(Optional.of(router));
|
||||
when(router.history()).thenReturn(history);
|
||||
when(history.findRecordedRiver(any())).thenReturn(Optional.of(
|
||||
@@ -267,16 +276,154 @@ public final class GenerationSemanticQueriesTest {
|
||||
)
|
||||
));
|
||||
|
||||
GenerationSemanticQueries.RiverResult result = GenerationSemanticQueries.nearestRiver(
|
||||
engine, HydrologyFeatureType.WATERFALL, 0, 0, 100).orElseThrow();
|
||||
GenerationSemanticQueries.RiverResult result = found(GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.WATERFALL, 0, 0, 100, () -> true));
|
||||
|
||||
assertEquals(GenerationSemanticQueries.RiverSource.RECORDED, result.source());
|
||||
assertEquals(HydrologyFeatureType.WATERFALL, result.type());
|
||||
assertEquals(9L, result.featureId());
|
||||
verify(runtime, never()).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any());
|
||||
|
||||
when(history.findRecordedRiver(any())).thenReturn(Optional.empty());
|
||||
assertEquals(new GenerationSemanticQueries.RiverSearch.Missing("No waterfall forms in this dimension: hydrology is off."),
|
||||
GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.WATERFALL, 0, 0, 100, () -> true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cavePoolsAreFoundOnlyInRecordedHistory() {
|
||||
public void disabledTypeAnswersWithItsReasonWithoutPlanning() throws Exception {
|
||||
IrisEngine engine = mock(IrisEngine.class);
|
||||
IrisComplex complex = mock(IrisComplex.class);
|
||||
HydrologyRuntime runtime = mock(HydrologyRuntime.class);
|
||||
GenerationHistory history = mock(GenerationHistory.class);
|
||||
GenerationHistoryRuntimeRouter router = mock(GenerationHistoryRuntimeRouter.class);
|
||||
when(engine.getComplex()).thenReturn(complex);
|
||||
when(complex.getHydrologyRuntime()).thenReturn(runtime);
|
||||
when(runtime.enabled()).thenReturn(true);
|
||||
when(runtime.unavailable(HydrologyFeatureType.DAM)).thenReturn(Optional.of("hydrology.ponds.dams is disabled"));
|
||||
when(runtime.unavailable(HydrologyFeatureType.CAVE_POOL)).thenReturn(Optional.of("hydrology.underground.cavePools is disabled"));
|
||||
when(engine.getGenerationHistoryRuntimeRouter()).thenReturn(Optional.of(router));
|
||||
when(router.history()).thenReturn(history);
|
||||
when(history.findRecordedRiver(any())).thenReturn(Optional.empty());
|
||||
|
||||
assertEquals(new GenerationSemanticQueries.RiverSearch.Missing("No dam forms in this dimension: hydrology.ponds.dams is disabled."),
|
||||
GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.DAM, 0, 0, 100, () -> true));
|
||||
assertEquals(new GenerationSemanticQueries.RiverSearch.Missing(
|
||||
"No cave pool forms in this dimension: hydrology.underground.cavePools is disabled."),
|
||||
GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.CAVE_POOL, 0, 0, 100, () -> true));
|
||||
|
||||
ChunkGenerationSemantics.RiverFeatureOccurrence older = new ChunkGenerationSemantics.RiverFeatureOccurrence(
|
||||
"water", HydrologyFeatureType.DAM, 5L, new ChunkGenerationSemantics.BlockPosition(30, 64, 0));
|
||||
when(history.findRecordedRiver(any())).thenReturn(Optional.of(
|
||||
new GenerationSemanticIndex.RiverMatch(older, new GenerationSemanticIndex.ChunkReference(1, 0, 1L))));
|
||||
GenerationSemanticQueries.RiverResult recorded = found(
|
||||
GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.DAM, 0, 0, 100, () -> true));
|
||||
|
||||
assertEquals(GenerationSemanticQueries.RiverSource.RECORDED, recorded.source());
|
||||
assertEquals(5L, recorded.featureId());
|
||||
verify(runtime, never()).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void timedOutSearchUsesRecordedWaterOrSaysItStopped() throws Exception {
|
||||
IrisEngine engine = mock(IrisEngine.class);
|
||||
IrisComplex complex = mock(IrisComplex.class);
|
||||
HydrologyRuntime runtime = mock(HydrologyRuntime.class);
|
||||
GenerationHistory history = mock(GenerationHistory.class);
|
||||
GenerationHistoryRuntimeRouter router = mock(GenerationHistoryRuntimeRouter.class);
|
||||
when(engine.getComplex()).thenReturn(complex);
|
||||
when(complex.getHydrologyRuntime()).thenReturn(runtime);
|
||||
when(runtime.enabled()).thenReturn(true);
|
||||
when(runtime.nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any()))
|
||||
.thenThrow(new TimeoutException("The hydrology feature search ran past its 30000 ms limit."));
|
||||
when(engine.getGenerationHistoryRuntimeRouter()).thenReturn(Optional.empty());
|
||||
|
||||
assertEquals(new GenerationSemanticQueries.RiverSearch.Missing("Stopped searching for the nearest river after 30 seconds."),
|
||||
GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.RIVER, 0, 0, 100, () -> true));
|
||||
|
||||
ChunkGenerationSemantics.RiverFeatureOccurrence occurrence = new ChunkGenerationSemantics.RiverFeatureOccurrence(
|
||||
"water", HydrologyFeatureType.RIVER, 8L, new ChunkGenerationSemantics.BlockPosition(60, 70, 0));
|
||||
when(engine.getGenerationHistoryRuntimeRouter()).thenReturn(Optional.of(router));
|
||||
when(router.history()).thenReturn(history);
|
||||
when(history.findRecordedRiver(any())).thenReturn(Optional.of(
|
||||
new GenerationSemanticIndex.RiverMatch(occurrence, new GenerationSemanticIndex.ChunkReference(3, 0, 1L))));
|
||||
|
||||
assertEquals(8L, found(GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.RIVER, 0, 0, 100, () -> true))
|
||||
.featureId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aNewSearchCancelsThePlayersPreviousSearch() throws Exception {
|
||||
IrisEngine engine = mock(IrisEngine.class);
|
||||
IrisComplex complex = mock(IrisComplex.class);
|
||||
HydrologyRuntime runtime = mock(HydrologyRuntime.class);
|
||||
when(engine.getComplex()).thenReturn(complex);
|
||||
when(engine.getGenerationHistoryRuntimeRouter()).thenReturn(Optional.empty());
|
||||
when(complex.getHydrologyRuntime()).thenReturn(runtime);
|
||||
when(runtime.enabled()).thenReturn(true);
|
||||
when(runtime.nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any())).thenReturn(Optional.empty());
|
||||
List<Runnable> queued = new ArrayList<>();
|
||||
UUID player = UUID.randomUUID();
|
||||
UUID other = UUID.randomUUID();
|
||||
GenerationSemanticQueries.RiverSearch missing = new GenerationSemanticQueries.RiverSearch.Missing(
|
||||
"No river found within " + GenerationSemanticQueries.RIVER_SEARCH_DISTANCE + " blocks.");
|
||||
|
||||
CompletableFuture<GenerationSemanticQueries.RiverSearch> first = GenerationSemanticQueries.startRiverSearch(
|
||||
player, queued::add, engine, HydrologyFeatureType.RIVER, 0, 0);
|
||||
CompletableFuture<GenerationSemanticQueries.RiverSearch> elsewhere = GenerationSemanticQueries.startRiverSearch(
|
||||
other, queued::add, engine, HydrologyFeatureType.RIVER, 0, 0);
|
||||
CompletableFuture<GenerationSemanticQueries.RiverSearch> second = GenerationSemanticQueries.startRiverSearch(
|
||||
player, queued::add, engine, HydrologyFeatureType.RIVER, 0, 0);
|
||||
|
||||
assertTrue(first.isCancelled());
|
||||
assertFalse(elsewhere.isDone());
|
||||
for (Runnable search : queued) {
|
||||
search.run();
|
||||
}
|
||||
assertEquals(missing, second.join());
|
||||
assertEquals(missing, elsewhere.join());
|
||||
verify(runtime, times(2)).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supersededSearchStopsAndOtherCancellationsAreFailures() throws Exception {
|
||||
IrisEngine engine = mock(IrisEngine.class);
|
||||
IrisComplex complex = mock(IrisComplex.class);
|
||||
HydrologyRuntime runtime = mock(HydrologyRuntime.class);
|
||||
when(engine.getComplex()).thenReturn(complex);
|
||||
when(engine.getGenerationHistoryRuntimeRouter()).thenReturn(Optional.empty());
|
||||
when(complex.getHydrologyRuntime()).thenReturn(runtime);
|
||||
when(runtime.enabled()).thenReturn(true);
|
||||
UUID player = UUID.randomUUID();
|
||||
AtomicReference<CompletableFuture<GenerationSemanticQueries.RiverSearch>> replacement = new AtomicReference<>();
|
||||
AtomicBoolean stillRunning = new AtomicBoolean(true);
|
||||
doAnswer(invocation -> {
|
||||
BooleanSupplier running = invocation.getArgument(5);
|
||||
assertTrue(running.getAsBoolean());
|
||||
replacement.set(GenerationSemanticQueries.startRiverSearch(player, ignored -> {
|
||||
}, engine, HydrologyFeatureType.RIVER, 0, 0));
|
||||
stillRunning.set(running.getAsBoolean());
|
||||
throw new CancellationException("The hydrology feature search was cancelled.");
|
||||
}).when(runtime).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any());
|
||||
|
||||
CompletableFuture<GenerationSemanticQueries.RiverSearch> superseded = GenerationSemanticQueries.startRiverSearch(
|
||||
player, Runnable::run, engine, HydrologyFeatureType.RIVER, 0, 0);
|
||||
|
||||
assertFalse(stillRunning.get());
|
||||
assertTrue(superseded.isCancelled());
|
||||
replacement.get().cancel(false);
|
||||
|
||||
doThrow(new CancellationException("Hydrology plan cache is closed."))
|
||||
.when(runtime).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any());
|
||||
CompletableFuture<GenerationSemanticQueries.RiverSearch> closed = GenerationSemanticQueries.startRiverSearch(
|
||||
player, Runnable::run, engine, HydrologyFeatureType.RIVER, 0, 0);
|
||||
|
||||
assertFalse(closed.isCancelled());
|
||||
ExecutionException failure = assertThrows(ExecutionException.class, closed::get);
|
||||
assertTrue(String.valueOf(failure.getCause()), failure.getCause() instanceof IllegalStateException);
|
||||
assertEquals("Hydrology plan cache is closed.", failure.getCause().getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cavePoolsAreFoundOnlyInRecordedHistory() throws Exception {
|
||||
IrisEngine engine = mock(IrisEngine.class);
|
||||
IrisComplex complex = mock(IrisComplex.class);
|
||||
HydrologyRuntime runtime = mock(HydrologyRuntime.class);
|
||||
@@ -292,21 +439,19 @@ public final class GenerationSemanticQueriesTest {
|
||||
when(history.findRecordedRiver(any())).thenReturn(Optional.of(
|
||||
new GenerationSemanticIndex.RiverMatch(occurrence, new GenerationSemanticIndex.ChunkReference(2, 0, 1L))));
|
||||
|
||||
GenerationSemanticQueries.RiverResult found = GenerationSemanticQueries.nearestRiver(
|
||||
engine, HydrologyFeatureType.CAVE_POOL, 0, 0, 100).orElseThrow();
|
||||
GenerationSemanticQueries.RiverResult found = found(GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.CAVE_POOL, 0, 0, 100, () -> true));
|
||||
|
||||
assertEquals(GenerationSemanticQueries.RiverSource.RECORDED, found.source());
|
||||
assertEquals(40, found.x());
|
||||
assertEquals(-20, found.y());
|
||||
verify(runtime, never()).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any());
|
||||
verify(runtime, never()).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any());
|
||||
|
||||
when(history.findRecordedRiver(any())).thenReturn(Optional.empty());
|
||||
assertTrue(GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.CAVE_POOL, 0, 0, 100).isEmpty());
|
||||
verify(runtime, never()).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any());
|
||||
assertTrue(GenerationSemanticQueries.riverNotFoundMessage(HydrologyFeatureType.CAVE_POOL, 100)
|
||||
.startsWith("No cave pool found in generated chunks within 100 blocks."));
|
||||
assertEquals(new GenerationSemanticQueries.RiverSearch.Missing("No cave pool found in generated chunks within 100 blocks. Cave pools "
|
||||
+ "form in carved caves, so only chunks that have already generated can hold one."),
|
||||
GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.CAVE_POOL, 0, 0, 100, () -> true));
|
||||
verify(runtime, never()).nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any());
|
||||
assertTrue(GenerationSemanticQueries.riverSearchMessage(HydrologyFeatureType.CAVE_POOL, 100).contains("generated chunks"));
|
||||
assertEquals("No river found within 100 blocks.", GenerationSemanticQueries.riverNotFoundMessage(HydrologyFeatureType.RIVER, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -328,7 +473,7 @@ public final class GenerationSemanticQueriesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void plannerFailureEscapesInsteadOfReportingNothingFound() {
|
||||
public void plannerFailureEscapesInsteadOfReportingNothingFound() throws Exception {
|
||||
IrisEngine engine = mock(IrisEngine.class);
|
||||
IrisComplex complex = mock(IrisComplex.class);
|
||||
HydrologyRuntime runtime = mock(HydrologyRuntime.class);
|
||||
@@ -337,10 +482,10 @@ public final class GenerationSemanticQueriesTest {
|
||||
when(engine.getGenerationHistoryRuntimeRouter()).thenReturn(Optional.empty());
|
||||
when(complex.getHydrologyRuntime()).thenReturn(runtime);
|
||||
when(runtime.enabled()).thenReturn(true);
|
||||
when(runtime.nearestFeature(anyInt(), anyInt(), any(), anyInt(), any())).thenThrow(failure);
|
||||
when(runtime.nearestFeature(anyInt(), anyInt(), any(), anyInt(), any(), any())).thenThrow(failure);
|
||||
|
||||
IllegalStateException thrown = assertThrows(IllegalStateException.class,
|
||||
() -> GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.LAKE, 0, 0, 100));
|
||||
() -> GenerationSemanticQueries.nearestRiver(engine, HydrologyFeatureType.LAKE, 0, 0, 100, () -> true));
|
||||
|
||||
assertSame(failure, thrown);
|
||||
}
|
||||
@@ -357,6 +502,13 @@ public final class GenerationSemanticQueriesTest {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private static GenerationSemanticQueries.RiverResult found(GenerationSemanticQueries.RiverSearch search) {
|
||||
if (search instanceof GenerationSemanticQueries.RiverSearch.Found found) {
|
||||
return found.feature();
|
||||
}
|
||||
throw new AssertionError("Expected a river, the search said: " + search);
|
||||
}
|
||||
|
||||
private static HydrologyFeatureRef feature(long id, int x, int y, int z) {
|
||||
return new HydrologyFeatureRef(id, HydrologyFeatureType.RIVER, x, y, z);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user