diff --git a/CONTRACTS.md b/CONTRACTS.md index 9a44cad05..fd4f2f4dc 100644 --- a/CONTRACTS.md +++ b/CONTRACTS.md @@ -159,8 +159,10 @@ running Iris is the pack publisher's responsibility. - Serve `Mantle.blocks()` in every invocation: the blocks placements wrote into the mantle, read-only, none where the dimension keeps no mantle or the chunk generates without it. A block a stage reads and writes back unchanged keeps the host's own identity of it (a custom block of a content plugin stays one). -- Answer `Mantle.isCarved(x, y, z)` in every invocation (false where the dimension keeps no mantle) and cover the chunk and the 16 blocks - around it in a chunk stage's mantle, as the sampler does. +- Serve `Mantle.carved()` in every invocation, keeping the voids in the host's own form: a mantle stage's view writes them, a chunk + stage's view only reads them (its writes throw UnsupportedOperationException), and a dimension that keeps no mantle holds none. Answer + `Mantle.isCarved(x, y, z)` (false where it holds none) and cover the chunk and the 16 blocks around it in a chunk stage's mantle, as the + sampler does. - Name pack resources to services by `ResourceId` and convert to the host's keys at the host's edge; read a `ResourceId` setting written as a bare pack key as the pack's id of that key. - Answer `TerrainSampler.surfaceBiome(x, z)` (the id `biome(x, y, z).surfaceBiome()` gives), `surfaceKind`, `minWorldY()`, `surfaceBlock` (empty diff --git a/DESIGN.md b/DESIGN.md index 67623317c..b167cc734 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -625,9 +625,12 @@ made of. A biome that is no column's surface biome, such as the cave biome of a The chunk stage's mantle covers the chunk and the 16 blocks around it like the sampler, so a decoration that attaches to a neighbor sees the blocks placements planned there. It writes registered layers inside the chunk's own 16 by 16 columns only: the mantle pipeline has finished around the chunk, and a neighbor may already be generated, so its mantle stays as the pipeline left it (a write outside throws, naming `Stage.mantle`). -`Mantle.isCarved(x, y, z)` is the host's answer whether the cave planner carved a block, as `blocks()` is its read-only layer of planned blocks: a -void the planner marked need not show in the chunk (under a dimension above, the cave stage leaves it solid), and a stage that places something -solid asks it. A yes-or-no question rather than a layer, because no module writes it and none needs to visit it. +`Mantle.carved()` is the host's layer of carved voids (`CarvedVoid`: what fills the void, `OPEN`, `FLUID`, `LAVA` or `AIR`, and optionally the +biome it shows), as `blocks()` is its layer of planned blocks. It is the one host layer a module writes: a mantle stage plans the voids +(Iris's cave carver does, thousands per chunk, so the plain fills are shared constants and the host keeps them in its own form), and a chunk +stage reads them, around its chunk as far as its mantle reaches, and cuts them out of its terrain. A chunk stage's writes throw, as the +mantle is complete when chunk stages run. A void the planner marked need not show in the chunk (under a dimension above, the cave stage +leaves it solid), so `Mantle.isCarved(x, y, z)` stays as the short question for a stage that places something solid. `Mantle.setPlatformBiome(x, y, z, biome)` is the other way round: modules write it and only the host's biome pass reads it, in the host's own form (Iris stores the platform's id for the biome), so it is a write rather than a layer a module could read back. It is general rather than a floating-island call: any stage that makes a block belong elsewhere, such as an island floating over another biome, can set the biome it shows. @@ -961,10 +964,9 @@ Why these lines: have to know that rule, so the resources say it; a default method (namespace and key) serves a host without such characters. - **`TerrainSampler.caveBiome(x, y, z)`** is `biome(...).caveBiome()` without the region and the surface biome: a cave wall asks for the cave biome around it five times per block, and the other two lookups were most of the cost. A default method keeps every sampler valid. -- **A chunk stage writes its own chunk's mantle.** Iris's cave stage stamps markers into the matter the platform reads, in the chunk it - generates. Earlier the chunk's mantle was read-only and a built-in facade reached past the view to the host's mantle; now the view lets - any chunk stage write registered layers inside its own columns and reads the neighbors only, so no stage, built-in or not, needs a way - around it. +- **A chunk stage writes its own chunk's mantle.** A chunk stage may need to keep something beside the chunk it generates. Earlier the + chunk's mantle was read-only and a built-in facade reached past the view to the host's mantle; now the view lets any chunk stage write + registered layers inside its own columns and reads the neighbors only, so no stage, built-in or not, needs a way around it. - **One `minWorldY()`, never defaulted**: all heights are above the dimension's bottom, but packs write some settings as absolute world Y (cave biome bands, floating island altitudes, structure bands). Those are converted by the module with the platform's Y of the bottom. The world, the terrain sampler, platform starts and structure fits once called it @@ -1333,12 +1335,9 @@ structure placer behind `StructureHost`) are Iris's and live there. What Iris's built-ins still ask through item 3, and why the API does not answer it yet. Each entry names what would let it go. Everything else they once asked a facade (a running world's sky, studio flag, cave floors and loot tables, the world seeds per -feature, the layout at a column, the declarations of a biome or region, the rock of a column, whether the dimension has bedrock) is -API now: `world.running()`, `world.seed(purpose)`, `TerrainSampler`, `FeatureSettings`, or the module's own settings. +feature, the layout at a column, the declarations of a biome or region, the rock of a column, whether the dimension has bedrock, the voids the cave carver plans in the mantle) is +API now: `world.running()`, `world.seed(purpose)`, `TerrainSampler`, `FeatureSettings`, `Mantle.carved()`, or the module's own settings. -- **`MatterMantle`** (invocation). Carve plans its caves as VolmLib matter slices (`MatterCavern`, markers) that Iris and the - platform read back as slices. The API mantle has typed layers but no cavern layer; it goes when carve writes a layer the - API defines and Iris reads that instead. - **`objects.ObjectRun`** (the input of a placement run, `PlacementRun.input`). The object and the module's own `objects.ObjectPlacement` settings that Iris's built-in placement stages read, and what they report back (the anchor height, which written blocks are the object's). The placement-level module settings no longer need it: the run carries diff --git a/README.md b/README.md index d0bf9d731..e6e11bfe7 100644 --- a/README.md +++ b/README.md @@ -181,8 +181,9 @@ ground of a biome somewhere else, such as a floating island, shapes and paints i block show that platform biome instead of its column's, inside the stage's own chunk. `ctx.settings().declaredBy(biomeId, Type.class)` reads what a biome declares itself, such as a cave biome of a `BiomeSample`, which is no level of `at(...)`. A mantle stage's `ctx.terrain()` covers the chunk and the larger of its reach and 16 blocks around it; a chunk stage's `ctx.mantle()` covers the same area as its terrain. `ctx.mantle().blocks()` holds the blocks placements wrote into the mantle for the chunks to -take over, such as objects and structures; a chunk stage copies them in (Iris's `iris:matter`). `ctx.mantle().isCarved(x, y, z)` says whether the cave planner -carved a void there, whatever the chunk's blocks show. Stages only read both. A chunk stage may write the layers its module registered, inside +take over, such as objects and structures; a chunk stage copies them in (Iris's `iris:matter`). `ctx.mantle().carved()` is the layer of voids the cave planner carved (`CarvedVoid`: `OPEN`, `FLUID`, `LAVA` or `AIR`, optionally with a biome), and +`isCarved(x, y, z)` asks whether one is there, whatever the chunk's blocks show. A chunk stage only reads both; a mantle stage plans the voids +with `carved().set(...)`, and a chunk stage's write throws. A chunk stage may write the layers its module registered, inside its own chunk's columns; around it, the mantle stays as the mantle stages left it. `blocks().oreFor(rock, ore)` gives the ore variant that suits the rock (deepslate ores in deepslate), which the platform decides, and `PackBiome.vanillaBiome()` the biome vanilla's rules see a pack biome as. diff --git a/api/src/main/java/com/volmit/mantle/CarvedVoid.java b/api/src/main/java/com/volmit/mantle/CarvedVoid.java new file mode 100644 index 000000000..a17016a59 --- /dev/null +++ b/api/src/main/java/com/volmit/mantle/CarvedVoid.java @@ -0,0 +1,64 @@ +package com.volmit.mantle; + +import com.volmit.resource.ResourceId; + +import java.util.Objects; +import java.util.Optional; + +/** A block the mantle holds as carved out: what fills the void once the chunk takes it over, and optionally the biome it shows. + * Instances are immutable and the four plain fills are shared constants ({@link #of}), so a carver marking thousands of blocks + * allocates nothing. + * + * @param fill what fills the void + * @param biome a pack biome the void shows instead of the cave biome its column would get; empty for the usual one + */ +public record CarvedVoid(Fill fill, Optional biome) { + /** Cut open, and left to the dimension. */ + public static final CarvedVoid OPEN = new CarvedVoid(Fill.OPEN, Optional.empty()); + + /** Filled with the dimension's fluid. */ + public static final CarvedVoid FLUID = new CarvedVoid(Fill.FLUID, Optional.empty()); + + /** Filled with lava. */ + public static final CarvedVoid LAVA = new CarvedVoid(Fill.LAVA, Optional.empty()); + + /** Filled with air, at any height. */ + public static final CarvedVoid AIR = new CarvedVoid(Fill.AIR, Optional.empty()); + + public CarvedVoid { + Objects.requireNonNull(fill, "fill"); + Objects.requireNonNull(biome, "biome"); + } + + /** The shared void of a fill, with no biome of its own. */ + public static CarvedVoid of(Fill fill) { + return switch (Objects.requireNonNull(fill, "fill")) { + case OPEN -> OPEN; + case FLUID -> FLUID; + case LAVA -> LAVA; + case AIR -> AIR; + }; + } + + /** This void's fill, showing {@code biome}. */ + public CarvedVoid withBiome(ResourceId biome) { + return new CarvedVoid(fill, Optional.of(biome)); + } + + /** What fills a carved void. */ + public enum Fill { + /** The solid is cut away and the dimension decides what is left: cave air, or lava at and below its lava height. + * Air and fluid the terrain already has there stay. + */ + OPEN, + + /** The dimension's fluid, replacing what the terrain has there. */ + FLUID, + + /** Lava, replacing what the terrain has there. */ + LAVA, + + /** Cave air at any height, replacing fluid and lava as well. */ + AIR + } +} diff --git a/api/src/main/java/com/volmit/mantle/Mantle.java b/api/src/main/java/com/volmit/mantle/Mantle.java index dd842c654..0ca469f34 100644 --- a/api/src/main/java/com/volmit/mantle/Mantle.java +++ b/api/src/main/java/com/volmit/mantle/Mantle.java @@ -34,9 +34,16 @@ public interface Mantle { */ MantleLayer blocks(); - /** Whether the mantle holds a carved void at the position for the chunk to take over: a cave's air or fluid, planned - * before the chunks were generated. Also true where the chunk's own blocks show no void, such as under a dimension - * stacked above. The host owns the carving; stages only ask. A dimension that keeps no mantle carves nothing. + /** The voids the mantle holds for chunks to take over: a cave's air or fluid, planned before the chunks were generated. + * Mantle stages write it ({@code set} plans a void, {@code remove} takes it back); a chunk stage only reads it, around + * its chunk as far as its mantle reaches, and its writes throw UnsupportedOperationException. A chunk stage takes the + * voids over into its chunk (Iris's {@code iris:carve} cuts them out of the terrain). The host owns this layer and keeps + * it in its own form, so it needs no registration. A dimension that keeps no mantle carves nothing. + */ + MantleLayer carved(); + + /** Whether the mantle holds a carved void at the position, whether or not the chunk's own blocks show it, such as + * under a dimension stacked above: the same as {@code carved().get(x, y, z) != null}, for a stage that only asks. */ boolean isCarved(int x, int y, int z); diff --git a/api/src/test/java/com/volmit/mantle/MantleValueTest.java b/api/src/test/java/com/volmit/mantle/MantleValueTest.java index 6e6b678b2..99f7e5f83 100644 --- a/api/src/test/java/com/volmit/mantle/MantleValueTest.java +++ b/api/src/test/java/com/volmit/mantle/MantleValueTest.java @@ -48,4 +48,26 @@ class MantleValueTest { assertThrows(NullPointerException.class, () -> new MantleKey<>("a:b", null)); assertThrows(IllegalArgumentException.class, () -> new MantleKey<>("a:b", int.class)); } + + @Test + void carvedVoidsOfAPlainFillAreTheSharedConstants() { + for (var fill : CarvedVoid.Fill.values()) { + assertSame(CarvedVoid.of(fill), CarvedVoid.of(fill)); + assertEquals(fill, CarvedVoid.of(fill).fill()); + assertTrue(CarvedVoid.of(fill).biome().isEmpty()); + } + assertSame(CarvedVoid.LAVA, CarvedVoid.of(CarvedVoid.Fill.LAVA)); + } + + @Test + void aCarvedVoidMayShowABiomeAndHasValueEquality() { + var biome = new com.volmit.resource.ResourceId("pack", "cave/lush"); + var lush = CarvedVoid.AIR.withBiome(biome); + assertEquals(CarvedVoid.Fill.AIR, lush.fill()); + assertEquals(biome, lush.biome().orElseThrow()); + assertEquals(CarvedVoid.AIR.withBiome(biome), lush); + assertNotEquals(CarvedVoid.AIR, lush); + assertThrows(NullPointerException.class, () -> new CarvedVoid(null, java.util.Optional.empty())); + assertThrows(NullPointerException.class, () -> new CarvedVoid(CarvedVoid.Fill.OPEN, null)); + } } diff --git a/host/src/main/java/com/volmit/host/ChunkMantle.java b/host/src/main/java/com/volmit/host/ChunkMantle.java index 80144dac9..292a75054 100644 --- a/host/src/main/java/com/volmit/host/ChunkMantle.java +++ b/host/src/main/java/com/volmit/host/ChunkMantle.java @@ -3,6 +3,7 @@ package com.volmit.host; import com.volmit.block.BlockState; import com.volmit.generation.ChunkContext; import com.volmit.geometry.BlockBox; +import com.volmit.mantle.CarvedVoid; import com.volmit.mantle.Mantle; import com.volmit.mantle.MantleEdit; import com.volmit.mantle.MantleKey; @@ -21,11 +22,13 @@ final class ChunkMantle implements Mantle { private static final String NEIGHBOUR_WRITE = "A chunk stage writes the mantle of its own chunk only;" + " write around it from a mantle stage (Stage.mantle), which runs before the chunks it reaches"; private static final String HOST_BLOCKS = "The mantle's blocks are written by placements, not by stages"; + private static final String CARVED_VOIDS = "Carved voids are planned by mantle stages (Stage.mantle), not by chunk stages"; private final Mantle mantle; private final BlockBox ownChunk; private final Map, MantleLayer> layers = new HashMap<>(); private MantleLayer blocks; + private MantleLayer carved; ChunkMantle(Mantle mantle, int originX, int originZ) { this.mantle = mantle; @@ -47,10 +50,16 @@ final class ChunkMantle implements Mantle { @Override public MantleLayer blocks() { - if (blocks == null) blocks = new ReadOnlyLayer<>(mantle.blocks()); + if (blocks == null) blocks = new ReadOnlyLayer<>(mantle.blocks(), HOST_BLOCKS); return blocks; } + @Override + public MantleLayer carved() { + if (carved == null) carved = new ReadOnlyLayer<>(mantle.carved(), CARVED_VOIDS); + return carved; + } + @Override public boolean isCarved(int x, int y, int z) { return mantle.isCarved(x, y, z); @@ -115,7 +124,7 @@ final class ChunkMantle implements Mantle { } } - private record ReadOnlyLayer(MantleLayer layer) implements MantleLayer { + private record ReadOnlyLayer(MantleLayer layer, String reason) implements MantleLayer { @Override public T get(int x, int y, int z) { return layer.get(x, y, z); @@ -123,12 +132,12 @@ final class ChunkMantle implements Mantle { @Override public void set(int x, int y, int z, T value) { - throw new UnsupportedOperationException(HOST_BLOCKS); + throw new UnsupportedOperationException(reason); } @Override public void remove(int x, int y, int z) { - throw new UnsupportedOperationException(HOST_BLOCKS); + throw new UnsupportedOperationException(reason); } @Override @@ -138,7 +147,7 @@ final class ChunkMantle implements Mantle { @Override public void clear(BlockBox area) { - throw new UnsupportedOperationException(HOST_BLOCKS); + throw new UnsupportedOperationException(reason); } } } diff --git a/host/src/main/java/com/volmit/host/PlacementViews.java b/host/src/main/java/com/volmit/host/PlacementViews.java index c25420e12..c2e38e32f 100644 --- a/host/src/main/java/com/volmit/host/PlacementViews.java +++ b/host/src/main/java/com/volmit/host/PlacementViews.java @@ -2,6 +2,7 @@ package com.volmit.host; import com.volmit.block.BlockState; import com.volmit.geometry.BlockBox; +import com.volmit.mantle.CarvedVoid; import com.volmit.mantle.Mantle; import com.volmit.mantle.MantleEdit; import com.volmit.mantle.MantleKey; @@ -99,6 +100,7 @@ final class PlacementViews { private final BlockBox allowedBounds; private final Map, MantleLayer> layers = new HashMap<>(); private MantleLayer blocks; + private MantleLayer carved; private RestrictedMantle(Mantle parent, BlockBox allowedBounds) { this.parent = Objects.requireNonNull(parent, "parent"); @@ -131,6 +133,13 @@ final class PlacementViews { return blocks; } + @Override + public MantleLayer carved() { + bounds(); + if (carved == null) carved = new RestrictedMantleLayer<>(this, parent.carved()); + return carved; + } + @Override public boolean isCarved(int x, int y, int z) { checkPosition(bounds(), x, y, z); diff --git a/host/src/test/java/com/volmit/example/ChunkPipelineTest.java b/host/src/test/java/com/volmit/example/ChunkPipelineTest.java index 669621f19..85e009940 100644 --- a/host/src/test/java/com/volmit/example/ChunkPipelineTest.java +++ b/host/src/test/java/com/volmit/example/ChunkPipelineTest.java @@ -21,6 +21,7 @@ import com.volmit.host.MantleRunner; import com.volmit.host.StageTimings; import com.volmit.hunk.Hunk; import com.volmit.geometry.BlockBox; +import com.volmit.mantle.CarvedVoid; import com.volmit.mantle.MantleLayer; import com.volmit.resource.ResourceId; import com.volmit.stage.ChunkStageContext; @@ -199,6 +200,12 @@ class ChunkPipelineTest { setup.stages().define(probe).run(context -> { carved.add(String.valueOf(context.mantle().isCarved(6, 9, 2))); carved.add(String.valueOf(context.mantle().isCarved(6, 10, 2))); + carved.add(String.valueOf(context.mantle().carved().get(6, 9, 2))); + carved.add(String.valueOf(context.mantle().carved().get(6, 10, 2))); + carved.add(assertThrows(UnsupportedOperationException.class, + () -> context.mantle().carved().set(6, 10, 2, CarvedVoid.AIR)).getMessage()); + carved.add(assertThrows(UnsupportedOperationException.class, + () -> context.mantle().carved().remove(6, 9, 2)).getMessage()); }); } }; @@ -208,7 +215,8 @@ class ChunkPipelineTest { TestContexts.generation(blocks, ColumnSettings.NONE, mantle.within(chunkArea)), TestContexts.SEED, StageTimings.NONE); - assertEquals(List.of("true", "false"), carved); + var cannotCarve = "Carved voids are planned by mantle stages (Stage.mantle), not by chunk stages"; + assertEquals(List.of("true", "false", CarvedVoid.OPEN.toString(), "null", cannotCarve, cannotCarve), carved); } @Test diff --git a/host/src/test/java/com/volmit/support/MemoryMantle.java b/host/src/test/java/com/volmit/support/MemoryMantle.java index 71d0efd7b..83f27efa8 100644 --- a/host/src/test/java/com/volmit/support/MemoryMantle.java +++ b/host/src/test/java/com/volmit/support/MemoryMantle.java @@ -3,6 +3,7 @@ package com.volmit.support; import com.volmit.block.BlockState; import com.volmit.geometry.BlockBox; import com.volmit.geometry.BlockPosition; +import com.volmit.mantle.CarvedVoid; import com.volmit.mantle.Mantle; import com.volmit.mantle.MantleEdit; import com.volmit.mantle.MantleKey; @@ -23,7 +24,7 @@ public final class MemoryMantle { private static final MantleKey PLANNED_BLOCKS = new MantleKey<>("host:planned-blocks", BlockState.class); /** Where the carved voids are kept; the host owns them, so no module registers this layer. */ - private static final MantleKey CAVERNS = new MantleKey<>("host:caverns", Boolean.class); + private static final MantleKey CAVERNS = new MantleKey<>("host:caverns", CarvedVoid.class); /** Where the platform biomes stages set are kept; the host owns them, so no module registers this layer. */ private static final MantleKey PLATFORM_BIOMES = new MantleKey<>("host:platform-biomes", ResourceId.class); @@ -36,6 +37,7 @@ public final class MemoryMantle { public BlockBox bounds() { return bounds; } public MantleLayer layer(MantleKey key) { return new Layer<>(key, bounds, true); } public MantleLayer blocks() { return new Layer<>(PLANNED_BLOCKS, bounds, false); } + public MantleLayer carved() { return new Layer<>(CAVERNS, bounds, true); } public boolean isCarved(int x, int y, int z) { return new Layer<>(CAVERNS, bounds, false).get(x, y, z) != null; } public void setPlatformBiome(int x, int y, int z, ResourceId biome) { if (!bounds.contains(x, y, z)) throw new IndexOutOfBoundsException("Outside the mantle view: " + x + "," + y + "," + z); @@ -50,9 +52,9 @@ public final class MemoryMantle { values(PLANNED_BLOCKS).put(new BlockPosition(x, y, z), block); } - /** Carves a void into the mantle, as the cave planner would; stages only read them. */ + /** Carves an open void into the mantle, as the cave planner would. */ public void carve(int x, int y, int z) { - values(CAVERNS).put(new BlockPosition(x, y, z), Boolean.TRUE); + values(CAVERNS).put(new BlockPosition(x, y, z), CarvedVoid.OPEN); } /** The platform biome a stage set at the block; null where none did. */