diff --git a/src/main/java/com/volmit/block/BlockRegistry.java b/src/main/java/com/volmit/block/BlockRegistry.java index adccb766b..1bdd429c6 100644 --- a/src/main/java/com/volmit/block/BlockRegistry.java +++ b/src/main/java/com/volmit/block/BlockRegistry.java @@ -38,4 +38,9 @@ public interface BlockRegistry { boolean inTag(BlockState state, ResourceId tag); /** Whether state can attach to the specified face of support, ignoring other neighbors. */ boolean canAttach(BlockState state, BlockState support, BlockFace face); + + /** Whether {@code state} can stand on {@code below}: the support rule the host checks before it writes a + * decoration, such as a flower on grass but not on stone. Only the kinds of block count, not their properties. + */ + boolean canPlaceOn(BlockState state, BlockState below); } diff --git a/src/main/java/com/volmit/block/BlockTrait.java b/src/main/java/com/volmit/block/BlockTrait.java index db39ce0d7..d8990f216 100644 --- a/src/main/java/com/volmit/block/BlockTrait.java +++ b/src/main/java/com/volmit/block/BlockTrait.java @@ -1,7 +1,9 @@ package com.volmit.block; -/** Host-defined block behavior; tags provide extensible material groups. */ +/** Host-defined block behavior; tags provide extensible material groups. {@code VINE} is a block that hangs from or + * clings to the faces around it, such as vines and glow lichen. + */ public enum BlockTrait { - AIR, SOLID, OCCLUDING, FLUID, WATER, GRAVITY, FOLIAGE, DECORATION, + AIR, SOLID, OCCLUDING, FLUID, WATER, GRAVITY, FOLIAGE, DECORATION, VINE, LOG, LEAVES, ORE, INVENTORY, BLOCK_ENTITY, CUSTOM, NEEDS_UPDATE } diff --git a/src/main/java/com/volmit/generation/GenerationContext.java b/src/main/java/com/volmit/generation/GenerationContext.java index 59454b1c1..ad27af927 100644 --- a/src/main/java/com/volmit/generation/GenerationContext.java +++ b/src/main/java/com/volmit/generation/GenerationContext.java @@ -13,6 +13,9 @@ public interface GenerationContext extends InvocationContext { int originX(); int originZ(); + /** The terrain of the chunk and of the 16 blocks around it on every side, at full height: a chunk stage writes only + * its own chunk, but may look at its neighbors' terrain, such as their heights. + */ TerrainSampler terrain(); FeatureSettings settings(); diff --git a/src/main/java/com/volmit/host/ChunkRunner.java b/src/main/java/com/volmit/host/ChunkRunner.java index 7f2c00bad..0dfcc868e 100644 --- a/src/main/java/com/volmit/host/ChunkRunner.java +++ b/src/main/java/com/volmit/host/ChunkRunner.java @@ -24,13 +24,30 @@ public final class ChunkRunner { * stage draws from its own stream of that seed ({@link StageStreams}). */ public void generate(GenerationContext inputs, long invocationSeed) { + generate(inputs, invocationSeed, StageTimings.NONE); + } + + /** Runs one chunk like {@link #generate(GenerationContext, long)} and reports to {@code timings} how long each + * stage that ran took. + */ + public void generate(GenerationContext inputs, long invocationSeed, StageTimings timings) { + Objects.requireNonNull(timings, "timings"); if (graph.size() == 0) { return; } var settings = ChunkSettings.collect(graph.settingsTypes(), inputs::settings, inputs.originX(), inputs.originZ()); var chunk = new ChunkInvocation(new StageInvocation(graph, invocationSeed, settings), inputs); for (int position = 0; position < graph.size(); position++) { - chunk.stages().runInvocationStage(position, chunk); + runTimed(chunk, position, timings); } } + + private void runTimed(ChunkInvocation chunk, int position, StageTimings timings) { + if (!chunk.stages().isActive(position)) { + return; + } + long started = System.nanoTime(); + chunk.stages().runInvocationStage(position, chunk); + timings.record(graph.stages().get(position), System.nanoTime() - started); + } } diff --git a/src/main/java/com/volmit/host/PlacementViews.java b/src/main/java/com/volmit/host/PlacementViews.java index e0ca1ba2a..ff05e219e 100644 --- a/src/main/java/com/volmit/host/PlacementViews.java +++ b/src/main/java/com/volmit/host/PlacementViews.java @@ -105,6 +105,12 @@ final class PlacementViews { bounds(); return new RestrictedMantleLayer<>(this, parent.layer(key)); } + + @Override + public MantleLayer blocks() { + bounds(); + return new RestrictedMantleLayer<>(this, parent.blocks()); + } } private static final class RestrictedMantleLayer implements MantleLayer { diff --git a/src/main/java/com/volmit/host/ReadOnlyMantle.java b/src/main/java/com/volmit/host/ReadOnlyMantle.java index c7a810f75..577e41c60 100644 --- a/src/main/java/com/volmit/host/ReadOnlyMantle.java +++ b/src/main/java/com/volmit/host/ReadOnlyMantle.java @@ -1,5 +1,6 @@ package com.volmit.host; +import com.volmit.block.BlockState; import com.volmit.mantle.BlockBox; import com.volmit.mantle.Mantle; import com.volmit.mantle.MantleEdit; @@ -30,6 +31,11 @@ final class ReadOnlyMantle implements Mantle { return new ReadOnlyLayer<>(mantle.layer(key)); } + @Override + public MantleLayer blocks() { + return new ReadOnlyLayer<>(mantle.blocks()); + } + @Override public MantleEdit edit() { throw new UnsupportedOperationException(READ_ONLY); diff --git a/src/main/java/com/volmit/host/StageTimings.java b/src/main/java/com/volmit/host/StageTimings.java new file mode 100644 index 000000000..beca01290 --- /dev/null +++ b/src/main/java/com/volmit/host/StageTimings.java @@ -0,0 +1,13 @@ +package com.volmit.host; + +/** Where a runner reports how long each stage it ran took, such as the host's generation metrics. Called on the + * invocation's thread, once per stage that ran, in run order; a stage the invocation left inactive is not reported. + */ +@FunctionalInterface +public interface StageTimings { + /** Reports nothing. */ + StageTimings NONE = (stage, nanos) -> { + }; + + void record(ResolvedStage stage, long nanos); +} diff --git a/src/main/java/com/volmit/mantle/Mantle.java b/src/main/java/com/volmit/mantle/Mantle.java index a69f37d1a..06fd93357 100644 --- a/src/main/java/com/volmit/mantle/Mantle.java +++ b/src/main/java/com/volmit/mantle/Mantle.java @@ -1,5 +1,7 @@ package com.volmit.mantle; +import com.volmit.block.BlockState; + /** * Invocation-scoped access to typed mantle data. X and Z are world block coordinates; Y is * the height above the dimension's bottom (0 = bottom), so it is never negative. The host translates @@ -22,6 +24,14 @@ public interface Mantle { MantleLayer layer(MantleKey key); + /** The blocks the mantle holds for chunks to take over: what placements wrote, such as objects and structures. + * A chunk stage copies them into its chunk (Iris's {@code iris:matter}); absent positions read null, and air a + * placement wrote is a block like any other. The host owns this layer, so it needs no registration. Stages read + * it only, and writes throw UnsupportedOperationException: placements write blocks with + * {@link com.volmit.placement.PlacementAccess#set}. A dimension that keeps no mantle holds none. + */ + MantleLayer blocks(); + /** Only one edit may be active per view. Parent reads remain allowed, but parent * mutations are prohibited until the edit ends (IllegalStateException). */ diff --git a/src/main/java/com/volmit/world/TerrainSampler.java b/src/main/java/com/volmit/world/TerrainSampler.java index 44cc518c5..2b3bf9363 100644 --- a/src/main/java/com/volmit/world/TerrainSampler.java +++ b/src/main/java/com/volmit/world/TerrainSampler.java @@ -1,6 +1,7 @@ package com.volmit.world; import com.volmit.mantle.BlockBox; +import java.util.OptionalInt; /** Read-only host sampling for the same pack snapshot as the invocation. * X and Z are world coordinates; Y is the height above the dimension's bottom (0 = bottom). @@ -11,4 +12,15 @@ public interface TerrainSampler { BlockBox bounds(); TerrainSample column(int worldX, int worldZ); BiomeSample biome(int worldX, int y, int worldZ); + + /** The height up to which the host fills the world with fluid wherever the terrain lies lower, the sea level of the + * dimension: a column's {@link TerrainSample#fluidY()} is this height when its surface is below it. + */ + int fluidHeight(); + + /** Where another dimension stacked above this one begins in the column, when one does: generation of this + * dimension leaves that height and everything above it to the dimension above, so a module writes nothing there. + * Empty when nothing sits above. Outside the bounds it throws like {@link #column}. + */ + OptionalInt upperDimensionY(int worldX, int worldZ); } diff --git a/src/test/java/com/volmit/example/ChunkPipelineTest.java b/src/test/java/com/volmit/example/ChunkPipelineTest.java index 1996d4e73..a64959768 100644 --- a/src/test/java/com/volmit/example/ChunkPipelineTest.java +++ b/src/test/java/com/volmit/example/ChunkPipelineTest.java @@ -155,6 +155,47 @@ class ChunkPipelineTest { assertEquals(AIR, blocks.get(6, 4, 7)); } + @Test + void chunkStagesCopyTheBlocksPlacementsPlannedButCannotPlanAny() { + var mantle = new MemoryMantle(); + var stone = new BlockState("minecraft:stone"); + mantle.plan(3, 2, 5, stone); + mantle.plan(4, 2, 5, AIR); + var chunkArea = new BlockBox(0, BOTTOM, 0, CHUNK, TOP, CHUNK); + var copy = Stage.chunk("test:copy/insert"); + IrisModule module = new IrisModule() { + public ModuleInfo info() { return ModuleInfo.of("test:copy", "Copy", "1"); } + public void install(StageGraph graph) { + graph.define(copy).run(context -> { + var blocks = context.hunk(BlockState.class); + context.mantle().blocks().visit(chunkArea, (x, y, z, block) -> blocks.set(x, y, z, block)); + }); + } + }; + var builder = new StageGraphBuilder(); + builder.install(DiscoveredModule.builtIn(module)); + var blocks = new ArrayHunk<>(BlockState.class, CHUNK, HEIGHT, CHUNK, GRASS); + + new ChunkRunner(builder.resolve(Pipeline.CHUNK)).generate( + TestContexts.generation(Map.of(BlockState.class, blocks), ColumnSettings.NONE, mantle.within(chunkArea)), TestContexts.SEED); + + assertEquals(stone, blocks.get(3, 2, 5)); + assertEquals(AIR, blocks.get(4, 2, 5), "air a placement planned replaces what the chunk had"); + assertEquals(GRASS, blocks.get(5, 2, 5)); + assertThrows(UnsupportedOperationException.class, () -> mantle.within(chunkArea).blocks().set(0, 0, 0, stone)); + } + + @Test + void theRunnerReportsHowLongEachStageThatRanTook() { + var timed = new ArrayList(); + var blocks = new ArrayHunk<>(BlockState.class, SIZE, HEIGHT, SIZE, AIR); + + runner().generate(TestContexts.generation(Map.of(BlockState.class, blocks), ColumnSettings.NONE, null), TestContexts.SEED, + (stage, nanos) -> timed.add(stage.stage().id() + (nanos >= 0 ? "" : " ran backwards"))); + + assertEquals(List.of("iris:terrain/surface"), timed, "grass has no settings here, so it does not run and is not reported"); + } + @Test void chunkStagesReadTheMantleButCannotWriteIt() { var stage = Stage.chunk("test:writer/work"); diff --git a/src/test/java/com/volmit/support/MemoryMantle.java b/src/test/java/com/volmit/support/MemoryMantle.java index 0d1708acf..88c77b8a0 100644 --- a/src/test/java/com/volmit/support/MemoryMantle.java +++ b/src/test/java/com/volmit/support/MemoryMantle.java @@ -1,5 +1,6 @@ package com.volmit.support; +import com.volmit.block.BlockState; import com.volmit.mantle.BlockBox; import com.volmit.mantle.Mantle; import com.volmit.mantle.MantleEdit; @@ -17,17 +18,26 @@ public final class MemoryMantle { private static final Comparator VISIT_ORDER = Comparator.comparingInt(BlockPosition::x) .thenComparingInt(BlockPosition::y).thenComparingInt(BlockPosition::z); + /** Where the planned blocks are kept; the host owns them, so no module registers this layer. */ + private static final MantleKey PLANNED_BLOCKS = new MantleKey<>("host:planned-blocks", BlockState.class); + private final Map> layers = new HashMap<>(); /** A view that accepts reads and writes inside {@code bounds} only. */ public Mantle within(BlockBox bounds) { return new Mantle() { public BlockBox bounds() { return bounds; } - public MantleLayer layer(MantleKey key) { return new Layer<>(key, bounds); } + public MantleLayer layer(MantleKey key) { return new Layer<>(key, bounds, true); } + public MantleLayer blocks() { return new Layer<>(PLANNED_BLOCKS, bounds, false); } public MantleEdit edit() { throw new UnsupportedOperationException("Edits are not used in these tests"); } }; } + /** Puts a planned block into the mantle, as a placement would; stages only read them. */ + public void plan(int x, int y, int z, BlockState block) { + values(PLANNED_BLOCKS).put(new BlockPosition(x, y, z), block); + } + public T get(MantleKey key, int x, int y, int z) { return key.type().cast(values(key).get(new BlockPosition(x, y, z))); } @@ -39,10 +49,12 @@ public final class MemoryMantle { private final class Layer implements MantleLayer { private final MantleKey key; private final BlockBox bounds; + private final boolean writable; - Layer(MantleKey key, BlockBox bounds) { + Layer(MantleKey key, BlockBox bounds, boolean writable) { this.key = key; this.bounds = bounds; + this.writable = writable; } public T get(int x, int y, int z) { @@ -51,11 +63,13 @@ public final class MemoryMantle { } public void set(int x, int y, int z, T value) { + requireWritable(); requireInside(x, y, z); values(key).put(new BlockPosition(x, y, z), key.type().cast(value)); } public void remove(int x, int y, int z) { + requireWritable(); requireInside(x, y, z); values(key).remove(new BlockPosition(x, y, z)); } @@ -70,10 +84,15 @@ public final class MemoryMantle { } public void clear(BlockBox area) { + requireWritable(); if (!bounds.contains(area)) throw new IndexOutOfBoundsException("Outside the mantle view: " + area); values(key).keySet().removeIf(position -> area.contains(position.x(), position.y(), position.z())); } + private void requireWritable() { + if (!writable) throw new UnsupportedOperationException("Planned blocks are read-only to stages"); + } + private void requireInside(int x, int y, int z) { if (!bounds.contains(x, y, z)) throw new IndexOutOfBoundsException("Outside the mantle view: " + x + "," + y + "," + z); }