diff --git a/core/src/main/java/art/arcane/iris/core/modules/BlockStateHunk.java b/core/src/main/java/art/arcane/iris/core/modules/BlockStateHunk.java index c9a2221f8..0bb54444f 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/BlockStateHunk.java +++ b/core/src/main/java/art/arcane/iris/core/modules/BlockStateHunk.java @@ -22,21 +22,20 @@ import art.arcane.iris.spi.PlatformBlockState; import com.volmit.block.BlockState; import com.volmit.hunk.Hunk; -import java.util.function.Function; /** * Iris's chunk block hunk seen as IrisApi block states. Iris leaves unset blocks null, which IrisApi - * forbids, so they read as air. States are converted through their keys and remembered for the - * invocation, since a chunk repeats few states many times. + * forbids, so they read as air. States convert as {@link PlatformBlockStates} says, shared with the chunk's planned + * blocks, so a stage that copies those into the chunk writes the very platform states the mantle held. */ final class BlockStateHunk implements Hunk { private final art.arcane.iris.util.project.hunk.Hunk blocks; private final PlatformBlockStates states; - /** {@code platformStates} turns a block state key into the platform's state, such as {@code B::getState}. */ - BlockStateHunk(art.arcane.iris.util.project.hunk.Hunk blocks, Function platformStates) { + /** {@code states} are the invocation's conversions, which the chunk's other views share. */ + BlockStateHunk(art.arcane.iris.util.project.hunk.Hunk blocks, PlatformBlockStates states) { this.blocks = blocks; - this.states = new PlatformBlockStates(platformStates); + this.states = states; } @Override diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisBlockRegistry.java b/core/src/main/java/art/arcane/iris/core/modules/IrisBlockRegistry.java index 7113a79a8..e612a56d5 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisBlockRegistry.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisBlockRegistry.java @@ -57,6 +57,8 @@ final class IrisBlockRegistry implements BlockRegistry { private final IrisData data; private final Map specs = new ConcurrentHashMap<>(); + /** Stages ask about every block of a chunk; the platform's state of each block is found once per snapshot. */ + private final Map platformStates = new ConcurrentHashMap<>(); private final LazyBoundedCache palettes = new LazyBoundedCache<>(PALETTE_CACHE_SIZE); /** @param data the pack whose {@code blocks} folder custom blocks come from */ @@ -100,7 +102,7 @@ final class IrisBlockRegistry implements BlockRegistry { @Override public boolean hasTrait(BlockState state, BlockTrait trait) { - PlatformBlockState platform = platformState(state.key()); + PlatformBlockState platform = platformState(state); return switch (trait) { case AIR -> platform.isAir(); case SOLID -> platform.isSolid(); @@ -109,6 +111,7 @@ final class IrisBlockRegistry implements BlockRegistry { case WATER -> platform.isWater(); case FOLIAGE -> platform.isFoliage(); case DECORATION -> platform.isDecorant(); + case VINE -> platform.isVineBlock(); case ORE -> platform.isOre(); case INVENTORY -> platform.isStorage(); case BLOCK_ENTITY -> platform.hasTileEntity(); @@ -122,12 +125,21 @@ final class IrisBlockRegistry implements BlockRegistry { @Override public boolean inTag(BlockState state, ResourceId tag) { - return IrisPlatforms.get().registries().isInTag(platformState(state.key()), tag.toString()); + return IrisPlatforms.get().registries().isInTag(platformState(state), tag.toString()); } @Override public boolean canAttach(BlockState state, BlockState support, BlockFace face) { - return platformState(state.key()).canAttach(platformState(support.key()), PlatformFace.valueOf(face.name())); + return platformState(state).canAttach(platformState(support), PlatformFace.valueOf(face.name())); + } + + @Override + public boolean canPlaceOn(BlockState state, BlockState below) { + return platformState(state).canPlaceOnto(platformState(below)); + } + + private PlatformBlockState platformState(BlockState state) { + return platformStates.computeIfAbsent(state, known -> platformState(known.key())); } private static PlatformBlockState platformState(String key) { diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisGenerationContext.java b/core/src/main/java/art/arcane/iris/core/modules/IrisGenerationContext.java index 5a6a8a564..4ac2b8f90 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisGenerationContext.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisGenerationContext.java @@ -19,6 +19,7 @@ package art.arcane.iris.core.modules; import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.spi.PlatformBlockState; import com.volmit.block.BlockState; import com.volmit.config.FeatureSettings; import com.volmit.generation.GenerationContext; @@ -37,8 +38,8 @@ import java.util.Collection; * Pack resources are the pack snapshot's, shared by every invocation of it. */ final class IrisGenerationContext implements GenerationContext { - /** Chunk stages write only their own chunk. */ - private static final int CHUNK_STAGE_REACH = 0; + /** Chunk stages write only their own chunk, but see the terrain this far around it (IrisApi's promise). */ + private static final int TERRAIN_MARGIN_BLOCKS = 16; private static final int CHUNK_WIDTH = 16; private final Engine engine; @@ -50,17 +51,18 @@ final class IrisGenerationContext implements GenerationContext { private final IrisModuleMantle mantle; private PackWorld world; - IrisGenerationContext(Engine engine, int originX, int originZ, BlockStateHunk blocks, IrisModuleMantle.Chunks mantleChunks, - Collection> mantleLayers) { + /** {@code states} convert the chunk's blocks, in the hunk and in the planned blocks of its mantle alike. */ + IrisGenerationContext(Engine engine, int originX, int originZ, art.arcane.iris.util.project.hunk.Hunk blocks, + PlatformBlockStates states, IrisModuleMantle.Chunks mantleChunks, Collection> mantleLayers) { PackResourceIds ids = new PackResourceIds(engine.getData().getDataFolder()); this.engine = engine; this.originX = originX; this.originZ = originZ; - this.blocks = blocks; - this.terrain = new IrisTerrainSampler(engine, ids, originX, originZ, CHUNK_STAGE_REACH); + this.blocks = new BlockStateHunk(blocks, states); + this.terrain = new IrisTerrainSampler(engine, ids, originX, originZ, TERRAIN_MARGIN_BLOCKS); this.settings = new ChunkModuleSettings(engine, ids); BlockBox chunk = ApiHeights.columns(engine, originX, originZ, originX + CHUNK_WIDTH, originZ + CHUNK_WIDTH); - this.mantle = new IrisModuleMantle(chunk, mantleChunks, mantleLayers); + this.mantle = new IrisModuleMantle(chunk, mantleChunks, mantleLayers, PlannedBlocks.of(engine, chunk, mantleChunks, states)); } @Override diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisMantleGenerationContext.java b/core/src/main/java/art/arcane/iris/core/modules/IrisMantleGenerationContext.java index 0a9bf1293..4cd06f77a 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisMantleGenerationContext.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisMantleGenerationContext.java @@ -19,6 +19,7 @@ package art.arcane.iris.core.modules; import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.util.common.data.B; import com.volmit.config.FeatureSettings; import com.volmit.generation.MantleGenerationContext; import com.volmit.host.MantleRegistration; @@ -57,7 +58,8 @@ final class IrisMantleGenerationContext implements MantleGenerationContext { this.settings = new ChunkModuleSettings(engine, ids); BlockBox reach = ApiHeights.columns(engine, originX - reachBlocks, originZ - reachBlocks, originX + CHUNK_WIDTH + reachBlocks, originZ + CHUNK_WIDTH + reachBlocks); - this.mantle = new IrisModuleMantle(reach, chunks, layers); + PlannedBlocks plannedBlocks = PlannedBlocks.of(engine, reach, chunks, new PlatformBlockStates(B::getState)); + this.mantle = new IrisModuleMantle(reach, chunks, layers, plannedBlocks); } @Override diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisModuleMantle.java b/core/src/main/java/art/arcane/iris/core/modules/IrisModuleMantle.java index 820aaa6cc..438efe5df 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisModuleMantle.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisModuleMantle.java @@ -21,6 +21,7 @@ package art.arcane.iris.core.modules; import art.arcane.volmlib.util.mantle.runtime.MantleChunk; import art.arcane.volmlib.util.matter.Matter; import art.arcane.volmlib.util.matter.MatterSlice; +import com.volmit.block.BlockState; import com.volmit.host.MantleRegistration; import com.volmit.mantle.BlockBox; import com.volmit.mantle.Mantle; @@ -69,12 +70,14 @@ final class IrisModuleMantle implements Mantle { private final BlockBox bounds; private final Chunks chunks; private final Map> registrations; + private final PlannedBlocks plannedBlocks; private BufferedEdit openEdit; - IrisModuleMantle(BlockBox bounds, Chunks chunks, Collection> registrations) { + IrisModuleMantle(BlockBox bounds, Chunks chunks, Collection> registrations, PlannedBlocks plannedBlocks) { this.bounds = bounds; this.chunks = chunks; this.registrations = registrations.stream().collect(Collectors.toMap(registration -> registration.key().id(), Function.identity())); + this.plannedBlocks = plannedBlocks; } @Override @@ -87,6 +90,11 @@ final class IrisModuleMantle implements Mantle { return new Layer<>(key, codecOf(key)); } + @Override + public MantleLayer blocks() { + return plannedBlocks; + } + @Override public MantleEdit edit() { if (openEdit != null) { @@ -226,6 +234,12 @@ final class IrisModuleMantle implements Mantle { return new EditLayer<>(new Layer<>(key, codecOf(key))); } + @Override + public MantleLayer blocks() { + requireOpen(); + return plannedBlocks; + } + @Override public MantleEdit edit() { throw new UnsupportedOperationException("Mantle edits cannot be nested; write through this edit"); diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisPackWorld.java b/core/src/main/java/art/arcane/iris/core/modules/IrisPackWorld.java index c235225c5..8b37a9fb7 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisPackWorld.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisPackWorld.java @@ -18,10 +18,12 @@ package art.arcane.iris.core.modules; +import art.arcane.iris.builtin.PendingSettings; import art.arcane.iris.engine.framework.Engine; import com.volmit.config.PackSettings; import com.volmit.host.WorldStates; import com.volmit.world.PackWorld; +import java.util.Optional; import java.util.function.Function; /** @@ -30,14 +32,16 @@ import java.util.function.Function; * pack's resources reload under it. Iris's built-in modules reach the engine through {@link #of(PackWorld)}, as built-in * reaches do through {@link IrisPackSettings#of}. */ -public final class IrisPackWorld implements PackWorld { +public final class IrisPackWorld implements PackWorld, PendingSettings { private final Engine engine; private final IrisPackResources resources; private final WorldStates states = new WorldStates(); + private final IrisPendingSettings pendingSettings; IrisPackWorld(Engine engine) { this.engine = engine; this.resources = engine.getData().getPackResources(); + this.pendingSettings = new IrisPendingSettings(engine); } /** Iris's own view of {@code world}, which Iris hands to every stage, service and lifecycle hook. */ @@ -73,6 +77,31 @@ public final class IrisPackWorld implements PackWorld { return states.state(type, () -> create.apply(this)); } + @Override + public boolean hidesOres() { + return pendingSettings.hidesOres(); + } + + @Override + public boolean postProcessingWalls() { + return pendingSettings.postProcessingWalls(); + } + + @Override + public boolean postProcessingSlabs() { + return pendingSettings.postProcessingSlabs(); + } + + @Override + public Optional wall(int x, int z) { + return pendingSettings.wall(x, z); + } + + @Override + public BiomeLayer slab(int x, int z) { + return pendingSettings.slab(x, z); + } + /** Whether the pack's resources were reloaded since this world was made, so it generates with an old snapshot. */ boolean isStale() { return engine.getData().getPackResources() != resources; diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisPendingSettings.java b/core/src/main/java/art/arcane/iris/core/modules/IrisPendingSettings.java new file mode 100644 index 000000000..38100717f --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisPendingSettings.java @@ -0,0 +1,100 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.builtin.PendingSettings; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.terrain.IrisBiomePaletteLayer; +import art.arcane.iris.spi.PlatformBlockState; +import art.arcane.volmlib.util.math.RNG; +import com.volmit.block.BlockState; + +import java.util.Optional; + +/** + * Transitional: what {@link PendingSettings} reads from Iris's dimension and biomes for one world, until those settings + * are module settings. A palette layer picks with the world's post-processing random, as the post-processing worker did. + */ +final class IrisPendingSettings implements PendingSettings { + private final Engine engine; + private volatile RNG postRandom; + + IrisPendingSettings(Engine engine) { + this.engine = engine; + } + + @Override + public boolean hidesOres() { + return engine.getDimension().isHideOresForHiddenOre(); + } + + @Override + public boolean postProcessingWalls() { + return engine.getDimension().isPostProcessingWalls(); + } + + @Override + public boolean postProcessingSlabs() { + return engine.getDimension().isPostProcessingSlabs(); + } + + @Override + public Optional wall(int x, int z) { + IrisBiomePaletteLayer wall = engine.getSurfaceBiome(x, z).getWall(); + return wall.getPalette().isEmpty() ? Optional.empty() : Optional.of(new PaletteLayer(wall)); + } + + @Override + public BiomeLayer slab(int x, int z) { + return new PaletteLayer(engine.getSurfaceBiome(x, z).getSlab()); + } + + /** Made on first use, so a world that never picks from a layer never asks for the seed. */ + private RNG postRandom() { + RNG random = postRandom; + if (random != null) { + return random; + } + synchronized (this) { + if (postRandom == null) { + postRandom = new RNG(engine.getSeedManager().getPost()); + } + return postRandom; + } + } + + private final class PaletteLayer implements BiomeLayer { + private final IrisBiomePaletteLayer layer; + + PaletteLayer(IrisBiomePaletteLayer layer) { + this.layer = layer; + } + + @Override + public Optional at(double x, double y, double z) { + PlatformBlockState picked = layer.get(postRandom(), x, y, z, engine.getData()); + return picked == null ? Optional.empty() : Optional.of(BlockState.parse(PlatformBlockStates.namespaced(picked.key()))); + } + + @Override + public boolean allowsSlope(double slope) { + return layer.getSlopeCondition().isValid(slope); + } + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisPlacementInputs.java b/core/src/main/java/art/arcane/iris/core/modules/IrisPlacementInputs.java index 26723d757..d5e76a611 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisPlacementInputs.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisPlacementInputs.java @@ -30,6 +30,8 @@ import com.volmit.world.TerrainSample; import com.volmit.world.PackWorld; import com.volmit.world.TerrainSampler; +import java.util.OptionalInt; + /** The read-only IrisApi inputs of one Iris object placement. */ final class IrisPlacementInputs implements PlacementContext { private final ObjectTransformer objects = new IrisObjectTransformer(); @@ -69,6 +71,16 @@ final class IrisPlacementInputs implements PlacementContext { public BiomeSample biome(int worldX, int y, int worldZ) { throw withoutEngine(); } + + @Override + public int fluidHeight() { + throw withoutEngine(); + } + + @Override + public OptionalInt upperDimensionY(int worldX, int worldZ) { + throw withoutEngine(); + } }; } diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisTerrainSampler.java b/core/src/main/java/art/arcane/iris/core/modules/IrisTerrainSampler.java index 14351049e..b811cd1df 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisTerrainSampler.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisTerrainSampler.java @@ -18,6 +18,7 @@ package art.arcane.iris.core.modules; +import art.arcane.iris.engine.UpperDimensionContext; import art.arcane.iris.engine.framework.Engine; import art.arcane.iris.engine.object.world.biome.IrisBiome; import com.volmit.mantle.BlockBox; @@ -80,6 +81,23 @@ final class IrisTerrainSampler implements TerrainSampler { cave.map(biome -> ids.of(biome.getLoadKey()))); } + @Override + public int fluidHeight() { + return engine.getDimension().getFluidHeight(); + } + + /** The upper dimension's surface, but never closer above the terrain than the dimension's gap. */ + @Override + public OptionalInt upperDimensionY(int worldX, int worldZ) { + requireInside(worldX, bounds.minY(), worldZ); + UpperDimensionContext upper = engine.getUpperContext(); + if (upper == null) { + return OptionalInt.empty(); + } + int surfaceY = (int) Math.round(engine.getComplex().getHeightStream().get((double) worldX, (double) worldZ)); + return OptionalInt.of(Math.max(upper.getUpperSurfaceY(worldX, worldZ), surfaceY + engine.getDimension().getUpperDimensionGap())); + } + private void requireInside(int worldX, int y, int worldZ) { if (!bounds.contains(worldX, y, worldZ)) { throw new IndexOutOfBoundsException("Block " + worldX + ", " + y + ", " + worldZ + " is outside the sampled area " + bounds); diff --git a/core/src/main/java/art/arcane/iris/core/modules/ModuleChunkGeneration.java b/core/src/main/java/art/arcane/iris/core/modules/ModuleChunkGeneration.java index 5a0123778..8d6f55e0e 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/ModuleChunkGeneration.java +++ b/core/src/main/java/art/arcane/iris/core/modules/ModuleChunkGeneration.java @@ -61,9 +61,10 @@ public final class ModuleChunkGeneration { int chunkZ = z >> CHUNK_SHIFT; long seed = ModuleSeeds.chunk(engine.getSeedManager().getSeed(), chunkX, chunkZ); try (PinnedMantleChunk mantle = new PinnedMantleChunk(engine, chunkX, chunkZ)) { - IrisGenerationContext inputs = new IrisGenerationContext(engine, x, z, new BlockStateHunk(blocks, platformStates), mantle, + IrisGenerationContext inputs = new IrisGenerationContext(engine, x, z, blocks, new PlatformBlockStates(platformStates), mantle, pack.catalog().mantle()); - new IrisChunk(engine, x, z, blocks, biomes, multicore, context).runBound(() -> new ChunkRunner(chunk).generate(inputs, seed)); + StageMetrics metrics = new StageMetrics(engine.getMetrics()); + new IrisChunk(engine, x, z, blocks, biomes, multicore, context).runBound(() -> new ChunkRunner(chunk).generate(inputs, seed, metrics)); } } } diff --git a/core/src/main/java/art/arcane/iris/core/modules/PlacementMantle.java b/core/src/main/java/art/arcane/iris/core/modules/PlacementMantle.java index 1f7973d80..bf6513a1f 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/PlacementMantle.java +++ b/core/src/main/java/art/arcane/iris/core/modules/PlacementMantle.java @@ -19,6 +19,8 @@ package art.arcane.iris.core.modules; import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.util.common.data.B; +import com.volmit.block.BlockState; import com.volmit.host.MantleRegistration; import com.volmit.mantle.BlockBox; import com.volmit.mantle.Mantle; @@ -49,7 +51,8 @@ final class PlacementMantle implements Mantle, AutoCloseable { this.bounds = area; this.storedBounds = ApiHeights.columns(engine, area.minX(), area.minZ(), area.maxX(), area.maxZ()); this.chunks = new PinnedMantleChunks(engine); - this.placement = new IrisModuleMantle(storedBounds, chunks, layers).edit(); + PlannedBlocks plannedBlocks = PlannedBlocks.of(engine, storedBounds, chunks, new PlatformBlockStates(B::getState)); + this.placement = new IrisModuleMantle(storedBounds, chunks, layers, plannedBlocks).edit(); } @Override @@ -62,6 +65,11 @@ final class PlacementMantle implements Mantle, AutoCloseable { return new StoredRange<>(placement.layer(key)); } + @Override + public MantleLayer blocks() { + return new StoredRange<>(placement.blocks()); + } + @Override public MantleEdit edit() { throw new UnsupportedOperationException("The placement owns the mantle edit; write through layer()"); diff --git a/core/src/main/java/art/arcane/iris/core/modules/PlannedBlocks.java b/core/src/main/java/art/arcane/iris/core/modules/PlannedBlocks.java new file mode 100644 index 000000000..a4ec5729c --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/PlannedBlocks.java @@ -0,0 +1,179 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.spi.PlatformBlockState; +import art.arcane.volmlib.util.mantle.runtime.MantleChunk; +import art.arcane.volmlib.util.matter.Matter; +import com.volmit.block.BlockState; +import com.volmit.mantle.BlockBox; +import com.volmit.mantle.MantleLayer; +import com.volmit.mantle.MantleVisitor; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.function.BooleanSupplier; + +/** + * The blocks Iris's mantle holds for chunks to take over, IrisApi's {@link com.volmit.mantle.Mantle#blocks()}: what object + * and structure placements wrote into the mantle's block slice, read as IrisApi block states over the chunks one + * invocation may touch. Stages only read them. A dimension that keeps no mantle ({@code useMantle: false}) holds none, + * even where an older mantle file still has some, as Iris never copied those into chunks; neither does a chunk that + * generates without its mantle (maintenance regeneration on Folia, {@link art.arcane.iris.engine.framework.EngineGeneration#bypassesMantleStages()}). + */ +final class PlannedBlocks implements MantleLayer { + private static final int CHUNK_SHIFT = 4; + private static final int SECTION_SHIFT = 4; + private static final int LOCAL_MASK = 15; + private static final String READ_ONLY = "Stages read the planned blocks; placements write them with PlacementAccess.set"; + private static final Comparator VISIT_ORDER = Comparator.comparingInt(Planned::x) + .thenComparingInt(Planned::y).thenComparingInt(Planned::z); + + private final BlockBox bounds; + private final IrisModuleMantle.Chunks chunks; + private final PlatformBlockStates states; + private final BooleanSupplier kept; + private Boolean keptHere; + + private PlannedBlocks(BlockBox bounds, IrisModuleMantle.Chunks chunks, PlatformBlockStates states, BooleanSupplier kept) { + this.bounds = bounds; + this.chunks = chunks; + this.states = states; + this.kept = kept; + } + + /** The planned blocks of {@code engine}'s mantle in {@code bounds}, converted with the invocation's {@code states}. */ + static PlannedBlocks of(Engine engine, BlockBox bounds, IrisModuleMantle.Chunks chunks, PlatformBlockStates states) { + return new PlannedBlocks(bounds, chunks, states, + () -> engine.getDimension().isUseMantle() && !engine.getGeneration().bypassesMantleStages()); + } + + /** No planned blocks in {@code bounds}, as in a dimension that keeps no mantle. */ + static PlannedBlocks none(BlockBox bounds) { + return new PlannedBlocks(bounds, (chunkX, chunkZ) -> null, null, () -> false); + } + + @Override + public BlockState get(int x, int y, int z) { + requireInside(x, y, z); + if (!isKept()) { + return null; + } + PlatformBlockState planned = plannedAt(requireChunk(x >> CHUNK_SHIFT, z >> CHUNK_SHIFT), x, y, z); + return planned == null ? null : states.toApi(planned); + } + + @Override + public void visit(BlockBox area, MantleVisitor visitor) { + Objects.requireNonNull(visitor, "visitor"); + requireInside(area); + if (!isKept()) { + return; + } + for (Planned planned : plannedIn(area)) { + visitor.accept(planned.x(), planned.y(), planned.z(), states.toApi(planned.block())); + } + } + + @Override + public void set(int x, int y, int z, BlockState value) { + throw new UnsupportedOperationException(READ_ONLY); + } + + @Override + public void remove(int x, int y, int z) { + throw new UnsupportedOperationException(READ_ONLY); + } + + @Override + public void clear(BlockBox area) { + throw new UnsupportedOperationException(READ_ONLY); + } + + /** Asked when a stage first reads, so invocations that never do never ask the dimension. */ + private boolean isKept() { + if (keptHere == null) { + keptHere = kept.getAsBoolean(); + } + return keptHere; + } + + /** The planned blocks in {@code area}, ordered by x, then y, then z, as {@link MantleLayer#visit} promises. */ + private List plannedIn(BlockBox area) { + List planned = new ArrayList<>(); + for (int chunkX = area.minX() >> CHUNK_SHIFT; chunkX <= (area.maxX() - 1) >> CHUNK_SHIFT; chunkX++) { + for (int chunkZ = area.minZ() >> CHUNK_SHIFT; chunkZ <= (area.maxZ() - 1) >> CHUNK_SHIFT; chunkZ++) { + collect(chunkX, chunkZ, area, planned); + } + } + planned.sort(VISIT_ORDER); + return planned; + } + + private void collect(int chunkX, int chunkZ, BlockBox area, List planned) { + int originX = chunkX << CHUNK_SHIFT; + int originZ = chunkZ << CHUNK_SHIFT; + requireChunk(chunkX, chunkZ).iterate(PlatformBlockState.class, (localX, y, localZ, block) -> { + int x = originX + localX; + int z = originZ + localZ; + if (area.contains(x, y, z)) { + planned.add(new Planned(x, y, z, block)); + } + }); + } + + private static PlatformBlockState plannedAt(MantleChunk chunk, int x, int y, int z) { + int section = y >> SECTION_SHIFT; + if (!chunk.exists(section)) { + return null; + } + Matter matter = chunk.get(section); + if (matter == null || !matter.hasSlice(PlatformBlockState.class)) { + return null; + } + return matter.getSlice(PlatformBlockState.class).get(x & LOCAL_MASK, y & LOCAL_MASK, z & LOCAL_MASK); + } + + private MantleChunk requireChunk(int chunkX, int chunkZ) { + MantleChunk chunk = chunks.chunk(chunkX, chunkZ); + if (chunk == null) { + throw new IllegalStateException("Mantle chunk " + chunkX + ", " + chunkZ + " is inside " + bounds + " but was not prepared"); + } + return chunk; + } + + private void requireInside(int x, int y, int z) { + if (!bounds.contains(x, y, z)) { + throw new IndexOutOfBoundsException("Block " + x + ", " + y + ", " + z + " is outside the mantle this invocation prepared, " + bounds); + } + } + + private void requireInside(BlockBox area) { + Objects.requireNonNull(area, "area"); + if (!bounds.contains(area)) { + throw new IndexOutOfBoundsException("Area " + area + " is not inside the mantle this invocation prepared, " + bounds); + } + } + + private record Planned(int x, int y, int z, PlatformBlockState block) { + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/PlatformBlockStates.java b/core/src/main/java/art/arcane/iris/core/modules/PlatformBlockStates.java index f70baa658..7ce3af137 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/PlatformBlockStates.java +++ b/core/src/main/java/art/arcane/iris/core/modules/PlatformBlockStates.java @@ -22,12 +22,19 @@ import art.arcane.iris.spi.PlatformBlockState; import com.volmit.block.BlockState; import java.util.HashMap; +import java.util.IdentityHashMap; import java.util.Map; import java.util.function.Function; /** * Converts between IrisApi block states and the platform's through their keys, remembering each * conversion. Meant for one invocation, which repeats few states many times; not thread-safe. + *

+ * A state read from the platform and written back as the same instance keeps its platform identity. IrisApi's + * {@link BlockState} is only a key, and some platform states share a key with another, such as a content plugin's + * custom block and the base block it is drawn as; a stage that copies such a block (planned mantle blocks into the + * chunk) must write the custom block back, not its base. Every distinct platform state therefore reads as its own + * instance, and writing that instance gives the platform state back; any other instance converts by its key. */ public final class PlatformBlockStates { public static final BlockState AIR = BlockState.parse("minecraft:air"); @@ -38,6 +45,7 @@ public final class PlatformBlockStates { private final Function platformStates; private final Map read = new HashMap<>(); private final Map written = new HashMap<>(); + private final Map origins = new IdentityHashMap<>(); /** {@code platformStates} turns a block state key into the platform's state, such as {@code B::getState}. */ public PlatformBlockStates(Function platformStates) { @@ -52,10 +60,21 @@ public final class PlatformBlockStates { if (state == null) { return AIR; } - return read.computeIfAbsent(state, platform -> BlockState.parse(namespaced(platform.key()))); + BlockState known = read.get(state); + if (known != null) { + return known; + } + BlockState api = BlockState.parse(namespaced(state.key())); + read.put(state, api); + origins.put(api, state); + return api; } public PlatformBlockState toPlatform(BlockState state) { + PlatformBlockState origin = origins.get(state); + if (origin != null) { + return origin; + } return written.computeIfAbsent(state, api -> platformStates.apply(api.key())); } diff --git a/core/src/main/java/art/arcane/iris/core/modules/StageMetrics.java b/core/src/main/java/art/arcane/iris/core/modules/StageMetrics.java new file mode 100644 index 000000000..e910d35dc --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/StageMetrics.java @@ -0,0 +1,55 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.engine.framework.EngineMetrics; +import art.arcane.volmlib.util.atomics.AtomicRollingSequence; +import com.volmit.host.ResolvedStage; +import com.volmit.host.StageTimings; + +import java.util.Map; +import java.util.function.Function; + +/** + * Iris's generation metrics for the built-in chunk stages that Iris timed under a name of its own before they were + * modules, so studio and the integration metrics keep showing them: Iris times each such stage around its run + * instead of the module timing itself. A ported built-in that reported a metric adds its stage here. + */ +final class StageMetrics implements StageTimings { + private static final double NANOS_PER_MILLISECOND = 1_000_000D; + private static final Map> NAMED = Map.of( + "iris:post/smooth", EngineMetrics::getPost, + "iris:perfection/surface", EngineMetrics::getPerfection); + + private final EngineMetrics metrics; + + /** {@code metrics} may be null, as for a mocked engine: nothing is recorded then. */ + StageMetrics(EngineMetrics metrics) { + this.metrics = metrics; + } + + @Override + public void record(ResolvedStage stage, long nanos) { + Function named = NAMED.get(stage.stage().id().toString()); + if (metrics == null || named == null) { + return; + } + named.apply(metrics).put(nanos / NANOS_PER_MILLISECOND); + } +} diff --git a/core/src/test/java/art/arcane/iris/core/modules/DimensionBottomTest.java b/core/src/test/java/art/arcane/iris/core/modules/DimensionBottomTest.java index 2e1c8e104..e7c8d931e 100644 --- a/core/src/test/java/art/arcane/iris/core/modules/DimensionBottomTest.java +++ b/core/src/test/java/art/arcane/iris/core/modules/DimensionBottomTest.java @@ -83,6 +83,8 @@ public class DimensionBottomTest { private static final int BOTTOM = 0; private static final int SURFACE_HEIGHT = 10; private static final BlockBox WHOLE_HEIGHT_OF_ONE_CHUNK = new BlockBox(0, BOTTOM, 0, 16, DIMENSION_HEIGHT, 16); + /** A chunk stage sees the terrain of its chunk and of the 16 blocks around it. */ + private static final BlockBox WHOLE_HEIGHT_AROUND_ONE_CHUNK = new BlockBox(-16, BOTTOM, -16, 32, DIMENSION_HEIGHT, 32); private static final BlockState STONE = BlockState.parse("minecraft:stone"); private static final MantleKey MARKS = new MantleKey<>("test:bottom/mark", String.class); private static final MantleCodec TEXT = new MantleCodec<>() { @@ -176,7 +178,9 @@ public class DimensionBottomTest { assertEquals("minecraft:stone", blocks.get(0, BOTTOM, 0).key()); assertEquals("the chunk's block hunk starts at the bottom, not at the world's minimum height", 1, countBlocks(blocks)); - assertSamplesAndBoundsAgree(); + assertEquals(List.of(WHOLE_HEIGHT_AROUND_ONE_CHUNK), terrainBounds); + assertEquals(List.of(WHOLE_HEIGHT_OF_ONE_CHUNK), viewBounds); + assertEquals("the terrain samples count from the same bottom", List.of(Double.valueOf(SURFACE_HEIGHT)), surfaces); } @Test diff --git a/core/src/test/java/art/arcane/iris/core/modules/IrisBlockRegistryTest.java b/core/src/test/java/art/arcane/iris/core/modules/IrisBlockRegistryTest.java index d3c97861c..5d493b8ff 100644 --- a/core/src/test/java/art/arcane/iris/core/modules/IrisBlockRegistryTest.java +++ b/core/src/test/java/art/arcane/iris/core/modules/IrisBlockRegistryTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -117,4 +118,27 @@ public class IrisBlockRegistryTest { verify(stone).canAttach(sand, PlatformFace.valueOf(face.name())); } } + + @Test + public void vinesAndStandingOnABlockAreThePlatformsAnswers() { + when(sand.isVineBlock()).thenReturn(true); + when(stone.canPlaceOnto(sand)).thenReturn(true); + BlockState stoneState = BlockState.parse("minecraft:stone"); + BlockState sandState = BlockState.parse("minecraft:sand"); + + assertTrue(blocks.hasTrait(sandState, BlockTrait.VINE)); + assertFalse(blocks.hasTrait(stoneState, BlockTrait.VINE)); + assertTrue(blocks.canPlaceOn(stoneState, sandState)); + assertFalse(blocks.canPlaceOn(sandState, stoneState)); + } + + @Test + public void eachStateIsLookedUpOnceAndAskedAgainFromMemory() { + BlockState stoneState = BlockState.parse("minecraft:stone"); + + blocks.hasTrait(stoneState, BlockTrait.SOLID); + blocks.hasTrait(BlockState.parse("minecraft:stone"), BlockTrait.AIR); + + verify(registries, times(1)).blockOrNull(eq("minecraft:stone"), anyBoolean()); + } } diff --git a/core/src/test/java/art/arcane/iris/core/modules/IrisModuleMantleTest.java b/core/src/test/java/art/arcane/iris/core/modules/IrisModuleMantleTest.java index bc76b31b0..a0807a793 100644 --- a/core/src/test/java/art/arcane/iris/core/modules/IrisModuleMantleTest.java +++ b/core/src/test/java/art/arcane/iris/core/modules/IrisModuleMantleTest.java @@ -111,7 +111,7 @@ public class IrisModuleMantleTest { } private IrisModuleMantle mantle(BlockBox bounds) { - return new IrisModuleMantle(bounds, this::chunk, LAYERS); + return new IrisModuleMantle(bounds, this::chunk, LAYERS, PlannedBlocks.none(bounds)); } @Test @@ -224,8 +224,9 @@ public class IrisModuleMantleTest { @Test public void aCommitThatCannotReachEveryChunkPublishesNothing() { - IrisModuleMantle mantle = new IrisModuleMantle(new BlockBox(0, 0, 0, 32, HEIGHT, 16), - (chunkX, chunkZ) -> chunkX == 0 ? chunk(chunkX, chunkZ) : null, LAYERS); + BlockBox bounds = new BlockBox(0, 0, 0, 32, HEIGHT, 16); + IrisModuleMantle mantle = new IrisModuleMantle(bounds, (chunkX, chunkZ) -> chunkX == 0 ? chunk(chunkX, chunkZ) : null, LAYERS, + PlannedBlocks.none(bounds)); MantleEdit edit = mantle.edit(); edit.layer(MARKS).set(1, 4, 1, "first"); edit.layer(MARKS).set(20, 4, 1, "unprepared"); @@ -287,7 +288,8 @@ public class IrisModuleMantleTest { ModuleHost.of(List.of(veins)).copyingJarsTo(folder.newFolder("copies").toPath()))) { ModuleMantleComponent.forPack(engine, pack, Set.of()).getFirst().generate(0, 0, this::chunk); - IrisModuleMantle neighbor = new IrisModuleMantle(new BlockBox(16, 0, 0, 32, HEIGHT, 16), this::chunk, pack.catalog().mantle()); + BlockBox neighborBounds = new BlockBox(16, 0, 0, 32, HEIGHT, 16); + IrisModuleMantle neighbor = new IrisModuleMantle(neighborBounds, this::chunk, pack.catalog().mantle(), PlannedBlocks.none(neighborBounds)); assertEquals("vein", neighbor.layer(planned).get(20, 34, 3)); } assertTrue(chunks.containsKey(1L << 32)); diff --git a/core/src/test/java/art/arcane/iris/core/modules/PerfectionModuleTest.java b/core/src/test/java/art/arcane/iris/core/modules/PerfectionModuleTest.java deleted file mode 100644 index 92cf854f6..000000000 --- a/core/src/test/java/art/arcane/iris/core/modules/PerfectionModuleTest.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Iris is a World Generator for Minecraft Servers - * Copyright (c) 2026 Arcane Arts (Volmit Software) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package art.arcane.iris.core.modules; - -import art.arcane.iris.builtin.IrisStages; -import art.arcane.iris.core.loader.IrisData; -import art.arcane.iris.engine.framework.Engine; -import art.arcane.iris.engine.framework.EngineMetrics; -import art.arcane.iris.engine.framework.SeedManager; -import art.arcane.iris.engine.modifier.PerfectionModule; -import art.arcane.iris.engine.object.world.dimension.IrisDimension; -import art.arcane.iris.spi.IrisPlatform; -import art.arcane.iris.spi.IrisPlatforms; -import art.arcane.iris.spi.PlatformBlockState; -import art.arcane.iris.spi.PlatformRegistries; -import art.arcane.iris.util.project.hunk.Hunk; -import com.volmit.IrisModule; -import com.volmit.ModuleInfo; -import com.volmit.host.LoadedPack; -import com.volmit.host.ModuleHost; -import com.volmit.stage.StageGraph; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -import java.io.IOException; -import java.util.List; - -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -/** The built-in iris:perfection chunk stage, run as part of the pack's chunk graph. */ -public class PerfectionModuleTest { - private static final int WIDTH = 16; - private static final int HEIGHT = 4; - - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - private LoadedPack pack; - private Engine engine; - private IrisDimension dimension; - private Hunk blocks; - private PlatformBlockState ground; - - @Before - public void loadPack() throws IOException { - IrisPlatforms.unbind(); - PlatformRegistries registries = mock(PlatformRegistries.class); - when(registries.block(anyString())).thenAnswer(call -> block(PlatformBlockStates.namespaced(call.getArgument(0).toLowerCase()), false)); - IrisPlatform platform = mock(IrisPlatform.class); - when(platform.registries()).thenReturn(registries); - IrisPlatforms.bind(platform); - pack = LoadedPack.load(folder.newFolder("pack").toPath(), - ModuleHost.of(List.of(new PerfectionModule(), new FloatingDecorationStub())).copyingJarsTo(folder.newFolder("copies").toPath())); - engine = mock(Engine.class); - IrisData data = mock(IrisData.class); - SeedManager seeds = mock(SeedManager.class); - dimension = new IrisDimension(); - when(engine.getData()).thenReturn(data); - when(data.getDataFolder()).thenReturn(folder.getRoot()); - when(engine.getSeedManager()).thenReturn(seeds); - when(engine.getDimension()).thenReturn(dimension); - when(engine.getMetrics()).thenReturn(new EngineMetrics(1)); - blocks = Hunk.newArrayHunk(WIDTH, HEIGHT, WIDTH); - ground = block("minecraft:grass_block", false); - blocks.set(0, 0, 0, ground); - blocks.set(1, 0, 0, ground); - } - - @After - public void closePack() { - pack.close(); - IrisPlatforms.unbind(); - } - - @Test - public void chunksLoseDecorationsTheirGroundCannotHold() { - PlatformBlockState floating = decorant(false); - PlatformBlockState rooted = decorant(true); - blocks.set(0, 1, 0, floating); - blocks.set(1, 1, 0, rooted); - - generate(); - - assertNotSame(floating, blocks.get(0, 1, 0)); - assertSame(rooted, blocks.get(1, 1, 0)); - assertSame(ground, blocks.get(0, 0, 0)); - } - - private void generate() { - ModuleChunkGeneration.generate(pack, engine, 0, 0, blocks, null, false, null, key -> block(key, false)); - } - - /** The stage perfection runs after; perfection needs it installed, not doing anything. */ - private static final class FloatingDecorationStub implements IrisModule { - @Override - public ModuleInfo info() { - return ModuleInfo.of("iris:floating", "Floating islands", "test"); - } - - @Override - public void install(StageGraph graph) { - graph.define(IrisStages.Floating.DECORATE).run(context -> { - }); - } - } - - private static PlatformBlockState decorant(boolean held) { - PlatformBlockState decorant = block("minecraft:poppy", true); - when(decorant.canPlaceOnto(any())).thenReturn(held); - return decorant; - } - - private static PlatformBlockState block(String key, boolean decorant) { - PlatformBlockState block = mock(PlatformBlockState.class); - when(block.key()).thenReturn(key); - when(block.isDecorant()).thenReturn(decorant); - when(block.isAir()).thenReturn(key.endsWith("air")); - return block; - } -} diff --git a/core/src/test/java/art/arcane/iris/core/modules/PlannedBlocksTest.java b/core/src/test/java/art/arcane/iris/core/modules/PlannedBlocksTest.java new file mode 100644 index 000000000..01399b5a3 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/modules/PlannedBlocksTest.java @@ -0,0 +1,138 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.engine.UsableBlockPlatform; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.framework.EngineGeneration; +import art.arcane.iris.engine.object.world.dimension.IrisDimension; +import art.arcane.iris.spi.PlatformBlockState; +import art.arcane.iris.util.project.hunk.Hunk; +import com.volmit.block.BlockState; +import com.volmit.mantle.BlockBox; +import com.volmit.mantle.MantleLayer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * The mantle's planned blocks as IrisApi's {@code Mantle.blocks()}: what placements wrote into the mantle's block slice, + * nothing where the dimension keeps no mantle or the chunk generates without it, read-only, and copied into the chunk + * as the very platform states the mantle held. + */ +public class PlannedBlocksTest { + private static final int SECTIONS = 4; + private static final BlockBox CHUNK = new BlockBox(16, 0, 32, 32, SECTIONS * 16, 48); + + private final InMemoryMantle mantle = new InMemoryMantle(SECTIONS); + private PlatformBlockState planks; + + @Before + public void bindPlatform() { + UsableBlockPlatform.bind(); + planks = UsableBlockPlatform.block("minecraft:oak_planks"); + } + + @After + public void unbindPlatform() { + UsableBlockPlatform.unbind(); + } + + @Test + public void placementsBlocksReadInWorldCoordinatesInVisitOrder() { + plan(20, 10, 33, planks); + plan(17, 40, 35, planks); + PlannedBlocks blocks = planned(true, false); + List visited = new ArrayList<>(); + + blocks.visit(CHUNK, (x, y, z, block) -> visited.add(x + "," + y + "," + z + " " + block.key())); + + assertEquals(List.of("17,40,35 minecraft:oak_planks", "20,10,33 minecraft:oak_planks"), visited); + assertEquals(BlockState.parse("minecraft:oak_planks"), blocks.get(20, 10, 33)); + assertNull(blocks.get(20, 11, 33)); + } + + @Test + public void aDimensionWithoutAMantleOrAChunkWithoutItPlansNothing() { + plan(20, 10, 33, planks); + + for (PlannedBlocks blocks : List.of(planned(false, false), planned(true, true))) { + List visited = new ArrayList<>(); + blocks.visit(CHUNK, (x, y, z, block) -> visited.add(block)); + assertEquals(List.of(), visited); + assertNull(blocks.get(20, 10, 33)); + } + } + + @Test + public void stagesCannotPlanBlocks() { + MantleLayer blocks = planned(true, false); + BlockState stone = BlockState.parse("minecraft:stone"); + + assertThrows(UnsupportedOperationException.class, () -> blocks.set(20, 10, 33, stone)); + assertThrows(UnsupportedOperationException.class, () -> blocks.remove(20, 10, 33)); + assertThrows(UnsupportedOperationException.class, () -> blocks.clear(CHUNK)); + } + + @Test + public void aCopiedBlockIsTheMantlesPlatformStateEvenWhereAnotherHasItsKey() { + PlatformBlockState custom = UsableBlockPlatform.block("minecraft:note_block"); + PlatformBlockState vanilla = UsableBlockPlatform.block("minecraft:note_block"); + plan(20, 10, 33, custom); + PlatformBlockStates states = new PlatformBlockStates(key -> vanilla); + PlannedBlocks blocks = PlannedBlocks.of(engine(true, false), CHUNK, mantle::chunk, states); + Hunk chunk = Hunk.newArrayHunk(16, SECTIONS * 16, 16); + BlockStateHunk hunk = new BlockStateHunk(chunk, states); + + blocks.visit(CHUNK, (x, y, z, block) -> hunk.set(x - 16, y, z - 32, block)); + hunk.set(0, 0, 0, BlockState.parse("minecraft:note_block")); + + assertSame("a content plugin's block keeps its identity", custom, chunk.get(4, 10, 1)); + assertSame("a block of the same key a stage writes itself is the platform's", vanilla, chunk.get(0, 0, 0)); + } + + private void plan(int x, int y, int z, PlatformBlockState block) { + mantle.chunk(x >> 4, z >> 4).getOrCreate(y >> 4).slice(PlatformBlockState.class).set(x & 15, y & 15, z & 15, block); + } + + private PlannedBlocks planned(boolean useMantle, boolean bypass) { + return PlannedBlocks.of(engine(useMantle, bypass), CHUNK, mantle::chunk, new PlatformBlockStates(UsableBlockPlatform::block)); + } + + private static Engine engine(boolean useMantle, boolean bypass) { + IrisDimension dimension = mock(IrisDimension.class); + when(dimension.isUseMantle()).thenReturn(useMantle); + EngineGeneration generation = mock(EngineGeneration.class); + when(generation.bypassesMantleStages()).thenReturn(bypass); + Engine engine = mock(Engine.class); + when(engine.getDimension()).thenReturn(dimension); + when(engine.getGeneration()).thenReturn(generation); + return engine; + } +} diff --git a/core/src/test/java/art/arcane/iris/core/modules/StageMetricsTest.java b/core/src/test/java/art/arcane/iris/core/modules/StageMetricsTest.java new file mode 100644 index 000000000..2d7975932 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/modules/StageMetricsTest.java @@ -0,0 +1,77 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.engine.framework.EngineMetrics; +import com.volmit.IrisModule; +import com.volmit.ModuleInfo; +import com.volmit.host.DiscoveredModule; +import com.volmit.host.ResolvedStage; +import com.volmit.host.StageGraphBuilder; +import com.volmit.stage.Pipeline; +import com.volmit.stage.Stage; +import com.volmit.stage.StageGraph; +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** Iris times the built-in stages it reported under names of their own, post and perfection, around their runs. */ +public class StageMetricsTest { + private static final long TWO_MILLISECONDS = 2_000_000L; + + @Test + public void namedBuiltInStagesFeedTheirMetricAndOthersNothing() { + EngineMetrics metrics = new EngineMetrics(4); + StageMetrics timings = new StageMetrics(metrics); + + for (ResolvedStage stage : stages("iris:post/smooth", "iris:perfection/surface", "iris:other/work")) { + timings.record(stage, TWO_MILLISECONDS); + } + + assertEquals(2D, metrics.getPost().getAverage(), 0D); + assertEquals(2D, metrics.getPerfection().getAverage(), 0D); + } + + @Test + public void anEngineWithoutMetricsRecordsNothing() { + new StageMetrics(null).record(stages("iris:post/smooth").getFirst(), TWO_MILLISECONDS); + } + + private static List stages(String... ids) { + StageGraphBuilder builder = new StageGraphBuilder(); + for (String id : ids) { + String module = id.substring(0, id.indexOf('/')); + builder.install(DiscoveredModule.builtIn(new IrisModule() { + @Override + public ModuleInfo info() { + return ModuleInfo.of(module, module, "test"); + } + + @Override + public void install(StageGraph graph) { + graph.define(Stage.chunk(id)).run(context -> { + }); + } + })); + } + return builder.resolve(Pipeline.CHUNK).stages(); + } +}