Surface for ported built-ins: VINE trait, canPlaceOn, the sampler's fluid height and upper dimension, the mantle's planned blocks, a chunk sampler that covers the neighbors, stage timings from the chunk runner

This commit is contained in:
piexel committed 2026-10-10 08:35:10 +02:00
1 parent f8d85dcd5e
commit 6c0c072afe
11 files changed
+139 -5

No files matched your search

@@ -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);
}
@@ -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
}
@@ -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();
+18 -1
View File
@@ -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);
}
}
@@ -105,6 +105,12 @@ final class PlacementViews {
bounds();
return new RestrictedMantleLayer<>(this, parent.layer(key));
}
@Override
public MantleLayer<BlockState> blocks() {
bounds();
return new RestrictedMantleLayer<>(this, parent.blocks());
}
}
private static final class RestrictedMantleLayer<T> implements MantleLayer<T> {
@@ -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<BlockState> blocks() {
return new ReadOnlyLayer<>(mantle.blocks());
}
@Override
public MantleEdit edit() {
throw new UnsupportedOperationException(READ_ONLY);
@@ -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);
}
@@ -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 {
<T> MantleLayer<T> layer(MantleKey<T> 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<BlockState> blocks();
/** Only one edit may be active per view. Parent reads remain allowed, but parent
* mutations are prohibited until the edit ends (IllegalStateException).
*/
@@ -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);
}
@@ -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<String>();
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");
@@ -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<BlockPosition> 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<BlockState> PLANNED_BLOCKS = new MantleKey<>("host:planned-blocks", BlockState.class);
private final Map<String, TreeMap<BlockPosition, Object>> 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 <T> MantleLayer<T> layer(MantleKey<T> key) { return new Layer<>(key, bounds); }
public <T> MantleLayer<T> layer(MantleKey<T> key) { return new Layer<>(key, bounds, true); }
public MantleLayer<BlockState> 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> T get(MantleKey<T> 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<T> implements MantleLayer<T> {
private final MantleKey<T> key;
private final BlockBox bounds;
private final boolean writable;
Layer(MantleKey<T> key, BlockBox bounds) {
Layer(MantleKey<T> 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);
}