diff --git a/DESIGN.md b/DESIGN.md index febbdb4c3..5b482f435 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -913,7 +913,8 @@ from a sapling, which drops a block has. A module answers such questions through - **Threading.** The host may call a provider from any thread, several at once. A provider that searches long (`StructureLocator.locate`) is called off the server's main thread. - **Services now.** `ObjectsAt` (`ALL`): pack object keys a module would place in a chunk, predicted without - generating it. `StructureLocator` (`FIRST`): every structure key a module knows, the ones a world places, and the + generating it, and the biomes and regions whose settings place a given object (`PlacedBy`, named by pack keys; the + host builds its search area from them through the biome tree it owns). `StructureLocator` (`FIRST`): every structure key a module knows, the ones a world places, and the nearest start of one as a `LocateResult` (`Found`, `NotFound`, `SearchLimitReached`; a sealed interface, so callers switch over every outcome). Keys are pack keys as packs write them and match without regard to case. - **Why not per-world provider instances.** A factory per world (`world -> provider`) would let a provider hold its diff --git a/README.md b/README.md index f4c26d504..befb31e85 100644 --- a/README.md +++ b/README.md @@ -264,11 +264,17 @@ them up in the pack's catalog instead of knowing the feature. ```java public void register(FeatureRegistry registry) { - registry.registerService(ObjectsAt.class, RuinsModule::objectsAt); + registry.registerService(ObjectsAt.class, new RuinsObjects()); } -private static Set objectsAt(PackWorld world, int chunkX, int chunkZ) { - return planted(world).contains(chunkX, chunkZ) ? Set.of("ruins/arch") : Set.of(); +final class RuinsObjects implements ObjectsAt { + public Set objectsAt(PackWorld world, int chunkX, int chunkZ) { + return planted(world).contains(chunkX, chunkZ) ? Set.of("ruins/arch") : Set.of(); + } + + public PlacedBy placedBy(PackWorld world, String objectKey) { + return PlacedBy.NOTHING; + } } ``` @@ -283,7 +289,7 @@ private static Set objectsAt(PackWorld world, int chunkX, int chunkZ) { | Service | Providers | What the host asks | | --- | --- | --- | -| `ObjectsAt` | `ALL` | The pack objects a module would place in a chunk, predicted without generating it (object search, studio tools). | +| `ObjectsAt` | `ALL` | The pack objects a module would place in a chunk, predicted without generating it, and the biomes and regions of the layout that place a given object (`PlacedBy`), so the host can narrow an object search to the areas it can appear in. | | `StructureLocator` | `FIRST` | Every structure key the module knows (the structure index), the ones a world places (commands complete them), and the nearest start of one, as a `LocateResult`: `Found(position)`, `NotFound` or `SearchLimitReached`. Keys match without regard to case. | ``` diff --git a/src/main/java/com/volmit/service/ObjectsAt.java b/src/main/java/com/volmit/service/ObjectsAt.java index 03f6e183b..01c9945d2 100644 --- a/src/main/java/com/volmit/service/ObjectsAt.java +++ b/src/main/java/com/volmit/service/ObjectsAt.java @@ -1,10 +1,11 @@ package com.volmit.service; import com.volmit.world.PackWorld; +import java.util.Objects; import java.util.Set; -/** Which pack objects a module places in a chunk, predicted without generating it. The host's object search and its - * studio tools ask every provider and use all the keys together. +/** Which pack objects a module places, predicted without generating anything. The host's object search and its + * studio tools ask every provider and use all the answers together. */ @Service(Providers.ALL) public interface ObjectsAt { @@ -15,4 +16,26 @@ public interface ObjectsAt { * there. */ Set objectsAt(PackWorld world, int chunkX, int chunkZ); + + /** + * The biomes and regions of {@code world}'s layout whose own settings make this module place the object + * {@code objectKey}. The host finds the areas where the object can appear from these: a biome counts for the areas + * it generates in and, through its children, for the areas of the biomes inside it, so a provider names only the + * layout entries that place the object themselves. Only biomes and regions the dimension can reach are named. + */ + PlacedBy placedBy(PackWorld world, String objectKey); + + /** Layout entries that place an object, named by the keys packs write ({@code land/plains}, {@code temperate}). */ + record PlacedBy(Set biomes, Set regions) { + public static final PlacedBy NOTHING = new PlacedBy(Set.of(), Set.of()); + + public PlacedBy { + biomes = Set.copyOf(Objects.requireNonNull(biomes, "biomes")); + regions = Set.copyOf(Objects.requireNonNull(regions, "regions")); + } + + public boolean isEmpty() { + return biomes.isEmpty() && regions.isEmpty(); + } + } } diff --git a/src/test/java/com/volmit/example/world/WorldStateExampleTest.java b/src/test/java/com/volmit/example/world/WorldStateExampleTest.java index 4fbf30f66..1671313e0 100644 --- a/src/test/java/com/volmit/example/world/WorldStateExampleTest.java +++ b/src/test/java/com/volmit/example/world/WorldStateExampleTest.java @@ -49,7 +49,17 @@ class WorldStateExampleTest { public void register(FeatureRegistry registry) { registry.registerLifecycle(this); - registry.registerService(ObjectsAt.class, RuinsModule::objectsAt); + registry.registerService(ObjectsAt.class, new ObjectsAt() { + @Override + public Set objectsAt(PackWorld world, int chunkX, int chunkZ) { + return RuinsModule.objectsAt(world, chunkX, chunkZ); + } + + @Override + public PlacedBy placedBy(PackWorld world, String objectKey) { + return PlacedBy.NOTHING; + } + }); } @Override