diff --git a/src/main/java/art/arcane/iris/builtin/PendingSettings.java b/src/main/java/art/arcane/iris/builtin/PendingSettings.java new file mode 100644 index 000000000..e9010b830 --- /dev/null +++ b/src/main/java/art/arcane/iris/builtin/PendingSettings.java @@ -0,0 +1,65 @@ +/* + * 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.builtin; + +import com.volmit.block.BlockState; +import com.volmit.world.PackWorld; + +import java.util.Optional; + +/** + * Transitional: settings Iris's built-in modules read that still live on Iris's dimension and biome classes while + * wave 3a moves them into module settings, {@code hideOresForHiddenOre} (agent C) and {@code postProcessingWalls}, + * {@code postProcessingSlabs} and a biome's {@code wall} and {@code slab} (agent T). Iris's worlds implement it. When + * those settings land, each method gives way to a read of its module setting and this type is deleted; nothing else + * may use it. + */ +public interface PendingSettings { + /** The pending settings of {@code world}, which must be a world Iris generates. */ + static PendingSettings of(PackWorld world) { + if (world instanceof PendingSettings pending) { + return pending; + } + throw new IllegalArgumentException("Built-in modules generate Iris's own worlds, not " + world); + } + + /** The dimension's {@code hideOresForHiddenOre}. */ + boolean hidesOres(); + + /** The dimension's {@code postProcessingWalls}. */ + boolean postProcessingWalls(); + + /** The dimension's {@code postProcessingSlabs}. */ + boolean postProcessingSlabs(); + + /** The {@code wall} of the surface biome of the column at {@code x}, {@code z}; empty when its palette lists no block. */ + Optional wall(int x, int z); + + /** The {@code slab} of the surface biome of the column at {@code x}, {@code z}. */ + BiomeLayer slab(int x, int z); + + /** One of a biome's palette layers, picking as Iris always picked it, with the world's post-processing seed. */ + interface BiomeLayer { + /** The block the layer picks at {@code x}, {@code y}, {@code z}; empty when none of its blocks exists. */ + Optional at(double x, double y, double z); + + /** Whether the layer's slope condition holds where the terrain's slope is {@code slope}. */ + boolean allowsSlope(double slope); + } +} diff --git a/src/main/java/art/arcane/iris/engine/actuator/DecorantModule.java b/src/main/java/art/arcane/iris/engine/actuator/DecorantModule.java index bf486e46c..a86b1b286 100644 --- a/src/main/java/art/arcane/iris/engine/actuator/DecorantModule.java +++ b/src/main/java/art/arcane/iris/engine/actuator/DecorantModule.java @@ -72,7 +72,8 @@ public final class DecorantModule implements IrisModule, ModuleLifecycle { @Override public void install(StageGraph graph) { - graph.define(IrisStages.Decorant.DECORATE).requires(IrisStages.Matter.INSERT).run(DecorantModule::decorate); + // Ordering only: iris:matter ships in the built-ins jar, which a pack may not have. + graph.define(IrisStages.Decorant.DECORATE).softAfter(IrisStages.Matter.INSERT.ref()).run(DecorantModule::decorate); } private static void decorate(ChunkStageContext context) { diff --git a/src/main/java/art/arcane/iris/engine/modifier/CustomModule.java b/src/main/java/art/arcane/iris/engine/modifier/CustomModule.java index a88cf7566..763bc40a9 100644 --- a/src/main/java/art/arcane/iris/engine/modifier/CustomModule.java +++ b/src/main/java/art/arcane/iris/engine/modifier/CustomModule.java @@ -51,7 +51,8 @@ public final class CustomModule implements IrisModule { @Override public void install(StageGraph graph) { - graph.define(IrisStages.Custom.BLOCKS).requires(IrisStages.Perfection.SURFACE).run(CustomModule::record); + // Ordering only: iris:perfection ships in the built-ins jar, which a pack may not have. + graph.define(IrisStages.Custom.BLOCKS).softAfter(IrisStages.Perfection.SURFACE.ref()).run(CustomModule::record); } private static void record(ChunkStageContext context) { diff --git a/src/main/java/art/arcane/iris/engine/modifier/FloatingModule.java b/src/main/java/art/arcane/iris/engine/modifier/FloatingModule.java index 691d6ba22..27c7fc31b 100644 --- a/src/main/java/art/arcane/iris/engine/modifier/FloatingModule.java +++ b/src/main/java/art/arcane/iris/engine/modifier/FloatingModule.java @@ -57,7 +57,8 @@ public final class FloatingModule implements IrisModule { public void install(StageGraph graph) { graph.define(IrisStages.Floating.OBJECTS).softAfter(IrisStages.Objects.PLACE.ref()).reach(FloatingModule::objectReach) .run(FloatingModule::objects); - graph.define(IrisStages.Floating.SOLIDS).requires(IrisStages.Post.SMOOTH).run(FloatingModule::solids); + // Ordering only: iris:post ships in the built-ins jar, which a pack may not have. + graph.define(IrisStages.Floating.SOLIDS).softAfter(IrisStages.Post.SMOOTH.ref()).run(FloatingModule::solids); graph.define(IrisStages.Floating.DECORATE).requires(IrisStages.Decorant.DECORATE).run(FloatingModule::decorate); } diff --git a/src/main/java/art/arcane/iris/engine/modifier/MatterModule.java b/src/main/java/art/arcane/iris/engine/modifier/MatterModule.java index 4bbb8e0cc..248cdd926 100644 --- a/src/main/java/art/arcane/iris/engine/modifier/MatterModule.java +++ b/src/main/java/art/arcane/iris/engine/modifier/MatterModule.java @@ -19,29 +19,25 @@ package art.arcane.iris.engine.modifier; import art.arcane.iris.builtin.IrisStages; -import art.arcane.iris.core.modules.IrisChunk; -import art.arcane.iris.engine.UpperDimensionContext; -import art.arcane.iris.engine.framework.Engine; -import art.arcane.iris.spi.PlatformBlockState; -import art.arcane.iris.util.project.hunk.Hunk; -import art.arcane.volmlib.util.documentation.ChunkCoordinates; -import art.arcane.volmlib.util.mantle.runtime.MantleChunk; -import art.arcane.volmlib.util.matter.Matter; import com.volmit.IrisModule; import com.volmit.ModuleInfo; +import com.volmit.block.BlockState; +import com.volmit.hunk.Hunk; +import com.volmit.mantle.BlockBox; import com.volmit.stage.ChunkStageContext; import com.volmit.stage.StageGraph; +import com.volmit.world.TerrainSampler; /** * Module {@code iris:matter}: copies the blocks the mantle planned for the chunk, its objects and structures, - * into it, unless the dimension turns the mantle off ({@code useMantle}). Where an upper dimension sits above, - * blocks at or above its surface are left out. It orders itself after deposits without requiring them, since - * deposits run only where a level declares some. + * into it (a dimension with {@code useMantle} off plans none). Where an upper dimension sits above, blocks at or + * above where it begins are left out. It orders itself after deposits without requiring them, since deposits run + * only where a level declares some. */ public final class MatterModule implements IrisModule { private static final String VERSION = "built-in"; - private static final int CHUNK_SHIFT = 4; - private static final int CHUNK_COLUMNS = 16 * 16; + private static final int CHUNK_WIDTH = 16; + private static final int NO_LIMIT = Integer.MAX_VALUE; @Override public ModuleInfo info() { @@ -57,59 +53,32 @@ public final class MatterModule implements IrisModule { } private static void insert(ChunkStageContext context) { - IrisChunk chunk = IrisChunk.current(); - if (chunk.bypassesMantleStages()) { - return; - } - chunk.state(MatterInsertion.class, MatterInsertion::new).insert(chunk.originX() >> CHUNK_SHIFT, chunk.originZ() >> CHUNK_SHIFT, chunk.blocks()); + int originX = context.originX(); + int originZ = context.originZ(); + Hunk blocks = context.hunk(BlockState.class); + int[] limits = upperLimits(context.terrain(), originX, originZ); + BlockBox chunk = new BlockBox(originX, 0, originZ, originX + CHUNK_WIDTH, blocks.getHeight(), originZ + CHUNK_WIDTH); + context.mantle().blocks().visit(chunk, (x, y, z, block) -> { + int localX = x - originX; + int localZ = z - originZ; + if (y < limits[column(localX, localZ)]) { + blocks.set(localX, y, localZ, block); + } + }); } - /** Copies the mantle's blocks into chunks for one engine runtime. */ - static final class MatterInsertion { - private final Engine engine; - - MatterInsertion(Engine engine) { - this.engine = engine; - } - - /** Copies the blocks the mantle holds for chunk {@code x}, {@code z} into {@code blocks}. */ - @ChunkCoordinates - void insert(int x, int z, Hunk blocks) { - if (!engine.getDimension().isUseMantle()) { - return; - } - - UpperDimensionContext upperCtx = engine.getUpperContext(); - boolean protectUpper = upperCtx != null; - - MantleChunk chunk = engine.getMantle().getMantle().getChunk(x, z).use(); - try { - if (protectUpper) { - int chunkBlockX = x << CHUNK_SHIFT; - int chunkBlockZ = z << CHUNK_SHIFT; - int gap = engine.getDimension().getUpperDimensionGap(); - int[] upperYs = new int[CHUNK_COLUMNS]; - for (int i = 0; i < CHUNK_COLUMNS; i++) { - int lx = i >> 4; - int lz = i & 15; - int worldX = chunkBlockX + lx; - int worldZ = chunkBlockZ + lz; - int he = (int) Math.round(engine.getComplex().getHeightStream().get((double) worldX, (double) worldZ)); - int rawUpper = upperCtx.getUpperSurfaceY(worldX, worldZ); - upperYs[i] = Math.max(rawUpper, he + gap); - } - chunk.iterate(PlatformBlockState.class, (lx, y, lz, value) -> { - int colIdx = (lx << 4) | (lz & 15); - if (y < upperYs[colIdx]) { - blocks.set(lx, y, lz, value); - } - }); - } else { - chunk.iterate(PlatformBlockState.class, (lx, y, lz, value) -> blocks.set(lx, y, lz, value)); - } - } finally { - chunk.release(); + /** Per column, the height from which the upper dimension takes over, or no limit. */ + static int[] upperLimits(TerrainSampler terrain, int originX, int originZ) { + int[] limits = new int[CHUNK_WIDTH * CHUNK_WIDTH]; + for (int localX = 0; localX < CHUNK_WIDTH; localX++) { + for (int localZ = 0; localZ < CHUNK_WIDTH; localZ++) { + limits[column(localX, localZ)] = terrain.upperDimensionY(originX + localX, originZ + localZ).orElse(NO_LIMIT); } } + return limits; + } + + private static int column(int localX, int localZ) { + return localX * CHUNK_WIDTH + localZ; } } diff --git a/src/main/java/art/arcane/iris/engine/modifier/PerfectionModule.java b/src/main/java/art/arcane/iris/engine/modifier/PerfectionModule.java index 6e45f193f..897ef2a90 100644 --- a/src/main/java/art/arcane/iris/engine/modifier/PerfectionModule.java +++ b/src/main/java/art/arcane/iris/engine/modifier/PerfectionModule.java @@ -19,21 +19,20 @@ package art.arcane.iris.engine.modifier; import art.arcane.iris.builtin.IrisStages; -import art.arcane.iris.core.modules.IrisChunk; -import art.arcane.iris.engine.framework.Engine; -import art.arcane.iris.engine.object.procedural.IrisProceduralBlocks; -import art.arcane.iris.engine.object.world.dimension.IrisDimension; -import art.arcane.iris.spi.PlatformBlockState; -import art.arcane.iris.util.common.data.B; -import art.arcane.iris.util.project.hunk.Hunk; -import art.arcane.volmlib.util.scheduling.PrecisionStopwatch; +import art.arcane.iris.builtin.PendingSettings; import com.volmit.IrisModule; import com.volmit.ModuleInfo; +import com.volmit.block.BlockRegistry; +import com.volmit.block.BlockState; +import com.volmit.block.BlockTrait; +import com.volmit.hunk.Hunk; +import com.volmit.resource.ResourceId; import com.volmit.stage.ChunkStageContext; import com.volmit.stage.StageGraph; import java.util.ArrayList; import java.util.HashMap; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; @@ -45,12 +44,8 @@ import java.util.Map; public final class PerfectionModule implements IrisModule { private static final String VERSION = "built-in"; private static final String HALF = "half"; - private static final char PROPERTIES_START = '['; - - private static final class States { - private static final PlatformBlockState AIR = B.getState("AIR"); - private static final Map ORE_BASES = oreBases(); - } + private static final BlockState AIR = new BlockState("minecraft:air"); + private static final Map ORE_BASES = oreBases(); @Override public ModuleInfo info() { @@ -63,121 +58,123 @@ public final class PerfectionModule implements IrisModule { } private static void perfect(ChunkStageContext context) { - IrisChunk chunk = IrisChunk.current(); - Engine engine = chunk.engine(); - IrisDimension dimension = engine.getDimension(); - PrecisionStopwatch stopwatch = PrecisionStopwatch.start(); - Hunk blocks = chunk.blocks(); - if (dimension.isHideOresForHiddenOre()) { - hideOres(blocks); + Hunk blocks = context.hunk(BlockState.class); + if (PendingSettings.of(context.world()).hidesOres()) { + hideOres(blocks, new IdentityHashMap<>()); } + Surfaces surfaces = new Surfaces(blocks, context.resources().blocks(), new IdentityHashMap<>()); boolean removed; do { - removed = removeUnsupportedDecorants(blocks); + removed = surfaces.removeUnsupportedDecorants(); } while (removed); - engine.getMetrics().getPerfection().put(stopwatch.getMilliseconds()); } - private static void hideOres(Hunk blocks) { + /** {@code hidden} remembers per state instance what it is hidden as, itself for a block that is no ore. */ + private static void hideOres(Hunk blocks, Map hidden) { for (int x = 0; x < blocks.getWidth(); x++) { for (int z = 0; z < blocks.getDepth(); z++) { for (int y = blocks.getHeight() - 1; y >= 0; y--) { - PlatformBlockState block = blocks.get(x, y, z); - PlatformBlockState base = block == null ? null : States.ORE_BASES.get(materialOf(block)); - if (base != null) { - blocks.set(x, y, z, base); + BlockState block = blocks.get(x, y, z); + BlockState shown = hidden.computeIfAbsent(block, state -> ORE_BASES.getOrDefault(state.material(), state)); + if (shown != block) { + blocks.set(x, y, z, shown); } } } } } - /** One pass over every column; whether it removed anything. */ - private static boolean removeUnsupportedDecorants(Hunk blocks) { - boolean removed = false; - for (int x = 0; x < blocks.getWidth(); x++) { - for (int z = 0; z < blocks.getDepth(); z++) { - for (int surface : surfaces(blocks, x, z)) { - removed |= removeIfUnsupported(blocks, x, surface, z); + /** + * The decorations of one chunk and the ground they stand on. Finding the surfaces asks about every block of the chunk, + * which holds few distinct states, so whether a state is solid is asked once per state instance. + */ + private record Surfaces(Hunk blocks, BlockRegistry registry, Map solidStates) { + /** One pass over every column; whether it removed anything. */ + boolean removeUnsupportedDecorants() { + boolean removed = false; + for (int x = 0; x < blocks.getWidth(); x++) { + for (int z = 0; z < blocks.getDepth(); z++) { + for (int surface : surfaces(x, z)) { + removed |= removeIfUnsupported(x, surface, z); + } } } + return removed; } - return removed; - } - /** The topmost solid block of a column and each solid block with open space above it below that. */ - private static List surfaces(Hunk blocks, int x, int z) { - List surfaces = new ArrayList<>(); - int top = topSolid(blocks, x, z); - surfaces.add(top); - boolean inside = true; - for (int y = top; y >= 0; y--) { - boolean solid = isSolid(blocks.get(x, y, z)); - if (solid != inside) { - inside = solid; - if (inside) { - surfaces.add(y); + /** The topmost solid block of a column and each solid block with open space above it below that. */ + private List surfaces(int x, int z) { + List surfaces = new ArrayList<>(); + int top = topSolid(x, z); + surfaces.add(top); + boolean inside = true; + for (int y = top; y >= 0; y--) { + boolean solid = isSolid(blocks.get(x, y, z)); + if (solid != inside) { + inside = solid; + if (inside) { + surfaces.add(y); + } } } + return surfaces; } - return surfaces; - } - private static boolean removeIfUnsupported(Hunk blocks, int x, int y, int z) { - PlatformBlockState tip = blocks.get(x, y, z); - if (tip == null || !B.isDecorant(tip)) { - return false; - } - PlatformBlockState below = blocks.get(x, y - 1, z); - if (below == null || !B.canPlaceOnto(tip, below)) { - blocks.set(x, y, z, States.AIR); + private boolean removeIfUnsupported(int x, int y, int z) { + BlockState tip = blocks.get(x, y, z); + if (!registry.hasTrait(tip, BlockTrait.DECORATION)) { + return false; + } + BlockState below = blockAt(x, y - 1, z); + if (below == null || !registry.canPlaceOn(tip, below)) { + blocks.set(x, y, z, AIR); + return true; + } + if (!below.properties().containsKey(HALF)) { + return false; + } + BlockState belowBelow = blockAt(x, y - 2, z); + if (belowBelow != null && registry.canPlaceOn(below, belowBelow)) { + return false; + } + blocks.set(x, y, z, AIR); + blocks.set(x, y - 1, z, AIR); return true; } - if (!IrisProceduralBlocks.hasProperty(below, HALF)) { - return false; - } - PlatformBlockState belowBelow = blocks.get(x, y - 2, z); - if (belowBelow != null && B.canPlaceOnto(below, belowBelow)) { - return false; - } - blocks.set(x, y, z, States.AIR); - blocks.set(x, y - 1, z, States.AIR); - return true; - } - private static int topSolid(Hunk blocks, int x, int z) { - for (int y = blocks.getHeight() - 1; y >= 0; y--) { - if (isSolid(blocks.get(x, y, z))) { - return y; + private int topSolid(int x, int z) { + for (int y = blocks.getHeight() - 1; y >= 0; y--) { + if (isSolid(blocks.get(x, y, z))) { + return y; + } } + return 0; + } + + private boolean isSolid(BlockState block) { + return solidStates.computeIfAbsent(block, state -> !(registry.hasTrait(state, BlockTrait.AIR) || registry.hasTrait(state, BlockTrait.FLUID))); + } + + /** The block at {@code y}, or null below the chunk's bottom. */ + private BlockState blockAt(int x, int y, int z) { + return y < 0 ? null : blocks.get(x, y, z); } - return 0; } - private static boolean isSolid(PlatformBlockState block) { - return block != null && !(B.isAir(block) || B.isFluid(block)); - } - - private static String materialOf(PlatformBlockState state) { - String key = state.key(); - int properties = key.indexOf(PROPERTIES_START); - return properties < 0 ? key : key.substring(0, properties); - } - - private static Map oreBases() { - Map bases = new HashMap<>(); - PlatformBlockState stone = B.getState("STONE"); - PlatformBlockState deepslate = B.getState("DEEPSLATE"); - PlatformBlockState netherrack = B.getState("NETHERRACK"); - PlatformBlockState blackstone = B.getState("BLACKSTONE"); + private static Map oreBases() { + Map bases = new HashMap<>(); + BlockState stone = new BlockState("minecraft:stone"); + BlockState deepslate = new BlockState("minecraft:deepslate"); + BlockState netherrack = new BlockState("minecraft:netherrack"); + BlockState blackstone = new BlockState("minecraft:blackstone"); for (String ore : List.of("coal", "copper", "iron", "gold", "redstone", "lapis", "diamond", "emerald")) { - bases.put("minecraft:" + ore + "_ore", stone); - bases.put("minecraft:deepslate_" + ore + "_ore", deepslate); + bases.put(ResourceId.parse("minecraft:" + ore + "_ore"), stone); + bases.put(ResourceId.parse("minecraft:deepslate_" + ore + "_ore"), deepslate); } - bases.put("minecraft:nether_gold_ore", netherrack); - bases.put("minecraft:nether_quartz_ore", netherrack); - bases.put("minecraft:ancient_debris", netherrack); - bases.put("minecraft:gilded_blackstone", blackstone); + bases.put(ResourceId.parse("minecraft:nether_gold_ore"), netherrack); + bases.put(ResourceId.parse("minecraft:nether_quartz_ore"), netherrack); + bases.put(ResourceId.parse("minecraft:ancient_debris"), netherrack); + bases.put(ResourceId.parse("minecraft:gilded_blackstone"), blackstone); return bases; } } diff --git a/src/main/java/art/arcane/iris/engine/modifier/PostModule.java b/src/main/java/art/arcane/iris/engine/modifier/PostModule.java index a39f8546f..1f7146c74 100644 --- a/src/main/java/art/arcane/iris/engine/modifier/PostModule.java +++ b/src/main/java/art/arcane/iris/engine/modifier/PostModule.java @@ -19,16 +19,11 @@ package art.arcane.iris.engine.modifier; import art.arcane.iris.builtin.IrisStages; -import art.arcane.iris.core.modules.IrisChunk; import com.volmit.IrisModule; import com.volmit.ModuleInfo; -import com.volmit.stage.ChunkStageContext; import com.volmit.stage.StageGraph; -/** - * Module {@code iris:post}: smooths the carved surface with {@link SurfaceSmoothing}, except while maintenance - * regeneration bypasses the mantle. - */ +/** Module {@code iris:post}: smooths the carved surface with {@link SurfaceSmoothing}. */ public final class PostModule implements IrisModule { private static final String VERSION = "built-in"; @@ -39,15 +34,6 @@ public final class PostModule implements IrisModule { @Override public void install(StageGraph graph) { - graph.define(IrisStages.Post.SMOOTH).requires(IrisStages.Carve.APPLY).run(PostModule::smooth); - } - - private static void smooth(ChunkStageContext context) { - IrisChunk chunk = IrisChunk.current(); - if (chunk.bypassesMantleStages()) { - return; - } - chunk.state(SurfaceSmoothing.class, SurfaceSmoothing::new) - .smooth(chunk.originX(), chunk.originZ(), chunk.blocks(), chunk.multicore(), chunk.context()); + graph.define(IrisStages.Post.SMOOTH).requires(IrisStages.Carve.APPLY).run(SurfaceSmoothing::smooth); } } diff --git a/src/main/java/art/arcane/iris/engine/modifier/SurfaceSmoothing.java b/src/main/java/art/arcane/iris/engine/modifier/SurfaceSmoothing.java index 6b15edda1..61baa072b 100644 --- a/src/main/java/art/arcane/iris/engine/modifier/SurfaceSmoothing.java +++ b/src/main/java/art/arcane/iris/engine/modifier/SurfaceSmoothing.java @@ -18,333 +18,321 @@ package art.arcane.iris.engine.modifier; -import art.arcane.iris.builtin.prepare.SlopeClip; -import art.arcane.iris.engine.framework.Engine; -import art.arcane.iris.engine.mantle.EngineMantle; -import art.arcane.iris.engine.object.world.biome.IrisBiome; -import art.arcane.iris.engine.object.world.dimension.IrisDimension; -import art.arcane.iris.engine.object.procedural.IrisProceduralBlocks; -import art.arcane.iris.util.project.context.ChunkContext; -import art.arcane.iris.util.common.data.B; -import art.arcane.iris.util.project.hunk.Hunk; -import art.arcane.volmlib.util.math.RNG; -import art.arcane.volmlib.util.scheduling.PrecisionStopwatch; -import art.arcane.iris.spi.PlatformBlockState; +import art.arcane.iris.builtin.PendingSettings; +import art.arcane.iris.builtin.PendingSettings.BiomeLayer; +import com.volmit.block.BlockRegistry; +import com.volmit.block.BlockState; +import com.volmit.block.BlockTrait; +import com.volmit.hunk.Hunk; +import com.volmit.resource.ResourceId; +import com.volmit.stage.ChunkStageContext; +import com.volmit.world.TerrainSampler; + +import java.util.List; +import java.util.Optional; /** - * Smooths the carved surface for {@link PostModule}: floating nibs, single-block bumps and potholes, walls under - * steep faces, slabs on steps, waterlogging and the foliage that lost its ground. One per engine runtime, - * holding the post stream's random. + * Smooths the carved surface of one chunk for {@link PostModule}: floating nibs, single-block bumps and potholes, + * walls under steep faces, slabs on steps, waterlogging and the foliage that lost its ground. */ -public class SurfaceSmoothing { - private static final class States { - private static final PlatformBlockState AIR = B.getState("AIR"); +final class SurfaceSmoothing { + private static final BlockState AIR = new BlockState("minecraft:air"); + private static final ResourceId AIR_ID = AIR.material(); + private static final ResourceId CAVE_AIR_ID = ResourceId.parse("minecraft:cave_air"); + private static final ResourceId WATER_ID = ResourceId.parse("minecraft:water"); + private static final ResourceId SNOW_ID = ResourceId.parse("minecraft:snow"); + private static final ResourceId DEAD_BUSH_ID = ResourceId.parse("minecraft:dead_bush"); + private static final String SLAB_SUFFIX = "_slab"; + private static final String WATERLOGGED = "waterlogged"; + private static final String TRUE = "true"; + /** The face properties of a connecting block, such as a vine's, each with the neighbour it faces. */ + private static final List FACES = List.of(new Face("north", 0, 0, -1), new Face("east", 1, 0, 0), + new Face("south", 0, 0, 1), new Face("west", -1, 0, 0), new Face("up", 0, 1, 0), new Face("down", 0, -1, 0)); + + private final Hunk blocks; + private final BlockRegistry registry; + private final TerrainSampler terrain; + private final PendingSettings settings; + private final int originX; + private final int originZ; + private final int planeWidth; + private final int[] heights; + private final boolean walls; + private final boolean slabs; + private final int fluidHeight; + + private SurfaceSmoothing(ChunkStageContext context) { + this.blocks = context.hunk(BlockState.class); + this.registry = context.resources().blocks(); + this.terrain = context.terrain(); + this.settings = PendingSettings.of(context.world()); + this.originX = context.originX(); + this.originZ = context.originZ(); + this.planeWidth = blocks.getWidth() + 2; + this.heights = heightPlane(); + this.walls = settings.postProcessingWalls(); + this.slabs = settings.postProcessingSlabs(); + this.fluidHeight = terrain.fluidHeight(); } - private final Engine engine; - private final RNG rng; - - public SurfaceSmoothing(Engine engine) { - this.engine = engine; - rng = new RNG(engine.getSeedManager().getPost()); + /** Smooths the chunk of {@code context}. */ + static void smooth(ChunkStageContext context) { + new SurfaceSmoothing(context).smoothColumns(); } - /** Smooths the chunk whose first block is {@code x}, {@code z}. */ - public void smooth(int x, int z, Hunk output, boolean multicore, ChunkContext context) { - PrecisionStopwatch p = PrecisionStopwatch.start(); - // The post stage runs sequentially on production (multicore false); an uncontended - // monitor per probe is still a monitor times ~10k probes per chunk. - Hunk sync = multicore ? output.synchronize() : output; - int width = output.getWidth(); - int depth = output.getDepth(); - int planeWidth = width + 2; - int[] heights = heightPlane(x, z, width, depth, planeWidth); - IrisDimension dimension = engine.getDimension(); - boolean walls = dimension.isPostProcessingWalls(); - boolean slabs = dimension.isPostProcessingSlabs(); - int fluidHeight = dimension.getFluidHeight(); - for (int i = 0; i < width; i++) { - for (int j = 0; j < depth; j++) { - post(i, j, sync, i + x, j + z, context, heights, planeWidth, walls, slabs, fluidHeight); + private void smoothColumns() { + for (int i = 0; i < blocks.getWidth(); i++) { + for (int j = 0; j < blocks.getDepth(); j++) { + post(i, j); } } - - engine.getMetrics().getPost().put(p.getMilliseconds()); } /** * Every column reads its own height plus its four neighbours, so adjacent columns would otherwise - * resolve the same height stream entry up to five times. Resolve the padded plane once instead. The - * four diagonal corners are never read, so they are left unresolved. + * resolve the same height up to five times. Resolve the padded plane once instead. The four diagonal + * corners are never read, so they are left unresolved. */ - private int[] heightPlane(int x, int z, int width, int depth, int planeWidth) { - int[] heights = new int[planeWidth * (depth + 2)]; - EngineMantle mantle = engine.getMantle(); - + private int[] heightPlane() { + int width = blocks.getWidth(); + int depth = blocks.getDepth(); + int[] plane = new int[planeWidth * (depth + 2)]; for (int j = -1; j <= depth; j++) { boolean edge = j == -1 || j == depth; int from = edge ? 0 : -1; int to = edge ? width - 1 : width; int row = (j + 1) * planeWidth; - for (int i = from; i <= to; i++) { - heights[row + i + 1] = mantle.trueHeight(x + i, z + j); + plane[row + i + 1] = surfaceHeight(originX + i, originZ + j); } } - - return heights; + return plane; } - private void post(int currentPostX, int currentPostZ, Hunk currentData, int x, int z, ChunkContext context, int[] heights, int planeWidth, boolean walls, boolean slabs, int fluidHeight) { - // x/z are world coordinates, the hunk is indexed relative to this chunk origin. - int originX = x - currentPostX; - int originZ = z - currentPostZ; - int center = (currentPostZ + 1) * planeWidth + currentPostX + 1; + private int surfaceHeight(int x, int z) { + return (int) Math.round(terrain.column(x, z).surfaceY()); + } + + private void post(int localX, int localZ) { + int x = originX + localX; + int z = originZ + localZ; + int center = (localZ + 1) * planeWidth + localX + 1; int h = heights[center]; int ha = heights[center + 1]; int hb = heights[center + planeWidth]; int hc = heights[center - 1]; int hd = heights[center - planeWidth]; - // Floating Nibs - int g = 0; - if (h < 1) { return; } - g += ha < h - 1 ? 1 : 0; - g += hb < h - 1 ? 1 : 0; - g += hc < h - 1 ? 1 : 0; - g += hd < h - 1 ? 1 : 0; - - if (g == 4 && isAir(x, h - 1, z, originX, originZ, currentData)) { - setPostBlock(x, h, z, States.AIR, originX, originZ, currentData); - - for (int i = h - 1; i > 0; i--) { - if (!isAir(x, i, z, originX, originZ, currentData)) { - h = i; - break; - } - } - } - - // Nibs - g = 0; - g += ha == h - 1 ? 1 : 0; - g += hb == h - 1 ? 1 : 0; - g += hc == h - 1 ? 1 : 0; - g += hd == h - 1 ? 1 : 0; - - if (g >= 4) { - PlatformBlockState bcState = getPostBlock(x, h, z, originX, originZ, currentData); - PlatformBlockState bState = getPostBlock(x, h + 1, z, originX, originZ, currentData); - - if (bState.isOccluding() && bState.isSolid()) { - if (bcState.isSolid()) { - setPostBlock(x, h, z, bState, originX, originZ, currentData); - h--; - } - } - } else { - // Potholes - g = 0; - g += ha == h + 1 ? 1 : 0; - g += hb == h + 1 ? 1 : 0; - g += hc == h + 1 ? 1 : 0; - g += hd == h + 1 ? 1 : 0; - - if (g >= 4) { - PlatformBlockState ba = getPostBlock(x, ha, z, originX, originZ, currentData); - PlatformBlockState bb = getPostBlock(x, hb, z, originX, originZ, currentData); - PlatformBlockState bc = getPostBlock(x, hc, z, originX, originZ, currentData); - PlatformBlockState bd = getPostBlock(x, hd, z, originX, originZ, currentData); - g = 0; - g = B.isSolid(ba) ? g + 1 : g; - g = B.isSolid(bb) ? g + 1 : g; - g = B.isSolid(bc) ? g + 1 : g; - g = B.isSolid(bd) ? g + 1 : g; - - if (g >= 3) { - setPostBlock(x, h + 1, z, getPostBlock(x, h, z, originX, originZ, currentData), originX, originZ, currentData); - h++; - } - } - } - - // Wall Patcher - IrisBiome biome = context.getBiome().get(currentPostX, currentPostZ); - + h = removeFloatingNib(x, z, h, ha, hb, hc, hd); + h = levelBumpOrPothole(x, z, h, ha, hb, hc, hd); if (walls) { - if (!biome.getWall().getPalette().isEmpty()) { - if (ha < h - 2 || hb < h - 2 || hc < h - 2 || hd < h - 2) { - boolean brokeGround = false; - int max = Math.abs(Math.max(h - ha, Math.max(h - hb, Math.max(h - hc, h - hd)))); - - for (int i = h; i > h - max; i--) { - PlatformBlockState d = biome.getWall().get(rng, x + i, i + h, z + i, engine.getData()); - - if (d != null) { - if (isAirOrWater(x, i, z, originX, originZ, currentData)) { - if (brokeGround) { - break; - } - - continue; - } - - setPostBlock(x, i, z, d, originX, originZ, currentData); - brokeGround = true; - } - } - } - } + paintWall(x, z, h, ha, hb, hc, hd); } - - // Slab if (slabs) { - //@builder - if ((ha == h + 1 && isSolidNonSlab(x + 1, ha, z, originX, originZ, currentData)) - || (hb == h + 1 && isSolidNonSlab(x, hb, z + 1, originX, originZ, currentData)) - || (hc == h + 1 && isSolidNonSlab(x - 1, hc, z, originX, originZ, currentData)) - || (hd == h + 1 && isSolidNonSlab(x, hd, z - 1, originX, originZ, currentData))) - //@done - { - SlopeClip sc = biome.getSlab().getSlopeCondition(); - PlatformBlockState d = sc.isValid(engine.getComplex().getSlopeStream().get(x, z)) ? biome.getSlab().get(rng, x, h, z, engine.getData()) : null; + h = placeSlab(x, z, h, ha, hb, hc, hd); + } + settleWaterlogging(x, z, h); + settleFoliage(x, z, h); + } - if (d != null) { - boolean cancel = B.isAir(d); - - if (IrisProceduralBlocks.materialKey(d).equals("minecraft:snow") && h + 1 <= fluidHeight) { - cancel = true; - } - - if (isSnowLayer(x, h, z, originX, originZ, currentData)) { - cancel = true; - } - - if (!cancel && isAirOrWater(x, h + 1, z, originX, originZ, currentData)) { - setPostBlock(x, h + 1, z, d, originX, originZ, currentData); - h++; - } - } + private int removeFloatingNib(int x, int z, int h, int ha, int hb, int hc, int hd) { + int g = lower(ha, h - 1) + lower(hb, h - 1) + lower(hc, h - 1) + lower(hd, h - 1); + if (g != 4 || !isAir(x, h - 1, z)) { + return h; + } + set(x, h, z, AIR); + for (int i = h - 1; i > 0; i--) { + if (!isAir(x, i, z)) { + return i; } } + return h; + } - // Waterlogging - PlatformBlockState b = getPostBlock(x, h, z, originX, originZ, currentData); - - if (IrisProceduralBlocks.hasProperty(b, "waterlogged")) { - boolean w = false; - - if (h <= fluidHeight + 1) { - if (isWaterOrWaterlogged(x, h + 1, z, originX, originZ, currentData)) { - w = true; - } else if ((isWaterOrWaterlogged(x + 1, h, z, originX, originZ, currentData) || isWaterOrWaterlogged(x - 1, h, z, originX, originZ, currentData) || isWaterOrWaterlogged(x, h, z + 1, originX, originZ, currentData) || isWaterOrWaterlogged(x, h, z - 1, originX, originZ, currentData))) { - w = true; - } - } - - if (w != "true".equals(IrisProceduralBlocks.propertyValue(b, "waterlogged"))) { - setPostBlock(x, h, z, b.withProperty("waterlogged", String.valueOf(w)), originX, originZ, currentData); + private int levelBumpOrPothole(int x, int z, int h, int ha, int hb, int hc, int hd) { + int g = same(ha, h - 1) + same(hb, h - 1) + same(hc, h - 1) + same(hd, h - 1); + if (g >= 4) { + BlockState bump = get(x, h, z); + BlockState above = get(x, h + 1, z); + if (has(above, BlockTrait.OCCLUDING) && has(above, BlockTrait.SOLID) && has(bump, BlockTrait.SOLID)) { + set(x, h, z, above); + return h - 1; } + return h; } - - // Foliage - b = getPostBlock(x, h + 1, z, originX, originZ, currentData); - - if (B.isVineBlock(b)) { - PlatformBlockState result = b; - int finalH = h + 1; - - for (String face : IrisProceduralBlocks.FACE_PROPERTIES) { - if (!IrisProceduralBlocks.hasProperty(b, face)) { - continue; - } - int[] mod = IrisProceduralBlocks.faceOffset(face); - PlatformBlockState d = getPostBlock(x + mod[0], finalH + mod[1], z + mod[2], originX, originZ, currentData); - result = result.withProperty(face, String.valueOf(!B.isAir(d) && !B.isVineBlock(d))); - } - if (!result.equals(b)) { - setPostBlock(x, h + 1, z, result, originX, originZ, currentData); - } + g = same(ha, h + 1) + same(hb, h + 1) + same(hc, h + 1) + same(hd, h + 1); + if (g < 4) { + return h; } - - if (B.isFoliage(b) || IrisProceduralBlocks.materialKey(b).equals("minecraft:dead_bush")) { - PlatformBlockState onto = getPostBlock(x, h, z, originX, originZ, currentData); - - if (!B.canPlaceOnto(b, onto) && !B.isDecorant(b)) { - setPostBlock(x, h + 1, z, States.AIR, originX, originZ, currentData); - } + int solidSides = solid(get(x, ha, z)) + solid(get(x, hb, z)) + solid(get(x, hc, z)) + solid(get(x, hd, z)); + if (solidSides < 3) { + return h; } + set(x, h + 1, z, get(x, h, z)); + return h + 1; } - public boolean isAir(int x, int y, int z, int originX, int originZ, Hunk currentData) { - String material = IrisProceduralBlocks.materialKey(getPostBlock(x, y, z, originX, originZ, currentData)); - return material.equals("minecraft:air") || material.equals("minecraft:cave_air"); - } - - public boolean hasGravity(int x, int y, int z, int originX, int originZ, Hunk currentData) { - String material = IrisProceduralBlocks.materialKey(getPostBlock(x, y, z, originX, originZ, currentData)); - return material.equals("minecraft:sand") || material.equals("minecraft:red_sand") || material.endsWith("_concrete_powder"); - } - - public boolean isSolid(int x, int y, int z, int originX, int originZ, Hunk currentData) { - PlatformBlockState d = getPostBlock(x, y, z, originX, originZ, currentData); - return B.isSolid(d) && !B.isVineBlock(d); - } - - public boolean isSolidNonSlab(int x, int y, int z, int originX, int originZ, Hunk currentData) { - PlatformBlockState d = getPostBlock(x, y, z, originX, originZ, currentData); - return B.isSolid(d) && !IrisProceduralBlocks.materialKey(d).endsWith("_slab"); - } - - public boolean isAirOrWater(int x, int y, int z, int originX, int originZ, Hunk currentData) { - String material = IrisProceduralBlocks.materialKey(getPostBlock(x, y, z, originX, originZ, currentData)); - return material.equals("minecraft:water") || material.equals("minecraft:air") || material.equals("minecraft:cave_air"); - } - - public boolean isSlab(int x, int y, int z, int originX, int originZ, Hunk currentData) { - return IrisProceduralBlocks.materialKey(getPostBlock(x, y, z, originX, originZ, currentData)).endsWith("_slab"); - } - - public boolean isSnowLayer(int x, int y, int z, int originX, int originZ, Hunk currentData) { - return IrisProceduralBlocks.materialKey(getPostBlock(x, y, z, originX, originZ, currentData)).equals("minecraft:snow"); - } - - public boolean isWater(int x, int y, int z, int originX, int originZ, Hunk currentData) { - return IrisProceduralBlocks.materialKey(getPostBlock(x, y, z, originX, originZ, currentData)).equals("minecraft:water"); - } - - public boolean isWaterOrWaterlogged(int x, int y, int z, int originX, int originZ, Hunk currentData) { - PlatformBlockState d = getPostBlock(x, y, z, originX, originZ, currentData); - return IrisProceduralBlocks.materialKey(d).equals("minecraft:water") || "true".equals(IrisProceduralBlocks.propertyValue(d, "waterlogged")); - } - - public boolean isLiquid(int x, int y, int z, int originX, int originZ, Hunk currentData) { - return IrisProceduralBlocks.hasProperty(getPostBlock(x, y, z, originX, originZ, currentData), "level"); - } - - public void setPostBlock(int x, int y, int z, PlatformBlockState d, int originX, int originZ, Hunk currentData) { - int lx = x - originX; - int lz = z - originZ; - - if (lx < 0 || lz < 0 || lx >= currentData.getWidth() || lz >= currentData.getDepth() || y < 0 || y >= currentData.getHeight()) { + private void paintWall(int x, int z, int h, int ha, int hb, int hc, int hd) { + if (ha >= h - 2 && hb >= h - 2 && hc >= h - 2 && hd >= h - 2) { return; } + Optional wall = settings.wall(x, z); + if (wall.isEmpty()) { + return; + } + boolean brokeGround = false; + int max = Math.abs(Math.max(h - ha, Math.max(h - hb, Math.max(h - hc, h - hd)))); + for (int i = h; i > h - max; i--) { + Optional block = wall.get().at(x + i, i + h, z + i); + if (block.isEmpty()) { + continue; + } + if (isAirOrWater(x, i, z)) { + if (brokeGround) { + break; + } + continue; + } + set(x, i, z, block.get()); + brokeGround = true; + } + } - currentData.set(lx, y, lz, d); + private int placeSlab(int x, int z, int h, int ha, int hb, int hc, int hd) { + boolean step = (ha == h + 1 && isSolidNonSlab(x + 1, ha, z)) + || (hb == h + 1 && isSolidNonSlab(x, hb, z + 1)) + || (hc == h + 1 && isSolidNonSlab(x - 1, hc, z)) + || (hd == h + 1 && isSolidNonSlab(x, hd, z - 1)); + if (!step) { + return h; + } + BiomeLayer slab = settings.slab(x, z); + if (!slab.allowsSlope(terrain.column(x, z).slope())) { + return h; + } + Optional picked = slab.at(x, h, z); + if (picked.isEmpty()) { + return h; + } + BlockState block = picked.get(); + boolean cancel = has(block, BlockTrait.AIR) + || block.material().equals(SNOW_ID) && h + 1 <= fluidHeight + || isSnowLayer(x, h, z); + if (cancel || !isAirOrWater(x, h + 1, z)) { + return h; + } + set(x, h + 1, z, block); + return h + 1; + } + + private void settleWaterlogging(int x, int z, int h) { + BlockState block = get(x, h, z); + if (!block.properties().containsKey(WATERLOGGED)) { + return; + } + boolean waterlogged = h <= fluidHeight + 1 && (isWaterOrWaterlogged(x, h + 1, z) + || isWaterOrWaterlogged(x + 1, h, z) || isWaterOrWaterlogged(x - 1, h, z) + || isWaterOrWaterlogged(x, h, z + 1) || isWaterOrWaterlogged(x, h, z - 1)); + if (waterlogged != TRUE.equals(block.properties().get(WATERLOGGED))) { + set(x, h, z, block.withProperty(WATERLOGGED, String.valueOf(waterlogged))); + } + } + + private void settleFoliage(int x, int z, int h) { + BlockState block = get(x, h + 1, z); + if (has(block, BlockTrait.VINE)) { + connectVine(x, h + 1, z, block); + } + if (!has(block, BlockTrait.FOLIAGE) && !block.material().equals(DEAD_BUSH_ID)) { + return; + } + if (!registry.canPlaceOn(block, get(x, h, z)) && !has(block, BlockTrait.DECORATION)) { + set(x, h + 1, z, AIR); + } + } + + /** A vine faces each neighbour it can cling to, a block that is neither air nor another vine. */ + private void connectVine(int x, int y, int z, BlockState vine) { + BlockState connected = vine; + for (Face face : FACES) { + if (!vine.properties().containsKey(face.property())) { + continue; + } + BlockState neighbour = get(x + face.dx(), y + face.dy(), z + face.dz()); + connected = connected.withProperty(face.property(), String.valueOf(!has(neighbour, BlockTrait.AIR) && !has(neighbour, BlockTrait.VINE))); + } + if (!connected.equals(vine)) { + set(x, y, z, connected); + } + } + + private boolean isAir(int x, int y, int z) { + ResourceId material = get(x, y, z).material(); + return material.equals(AIR_ID) || material.equals(CAVE_AIR_ID); + } + + private boolean isSolidNonSlab(int x, int y, int z) { + BlockState block = get(x, y, z); + return has(block, BlockTrait.SOLID) && !block.material().path().endsWith(SLAB_SUFFIX); + } + + private boolean isAirOrWater(int x, int y, int z) { + ResourceId material = get(x, y, z).material(); + return material.equals(WATER_ID) || material.equals(AIR_ID) || material.equals(CAVE_AIR_ID); + } + + private boolean isSnowLayer(int x, int y, int z) { + return get(x, y, z).material().equals(SNOW_ID); + } + + private boolean isWaterOrWaterlogged(int x, int y, int z) { + BlockState block = get(x, y, z); + return block.material().equals(WATER_ID) || TRUE.equals(block.properties().get(WATERLOGGED)); + } + + private boolean has(BlockState block, BlockTrait trait) { + return registry.hasTrait(block, trait); + } + + private int solid(BlockState block) { + return has(block, BlockTrait.SOLID) ? 1 : 0; + } + + private static int lower(int neighbour, int than) { + return neighbour < than ? 1 : 0; + } + + private static int same(int neighbour, int as) { + return neighbour == as ? 1 : 0; + } + + /** Writes inside the chunk only; a step on a neighbour column outside it is left alone. */ + private void set(int x, int y, int z, BlockState block) { + int localX = x - originX; + int localZ = z - originZ; + if (localX < 0 || localZ < 0 || localX >= blocks.getWidth() || localZ >= blocks.getDepth() || y < 0 || y >= blocks.getHeight()) { + return; + } + blocks.set(localX, y, localZ, block); } /** * Neighbour columns can sit one block outside this chunk, and the blocks of an adjacent chunk are not - * available while generating this one. Resolve the hunk index relative to the chunk origin and let - * getClosest clamp to the nearest in-chunk column instead of wrapping to the opposite chunk edge. + * available while generating this one. Clamp to the nearest block of the chunk instead of wrapping to the + * opposite edge. */ - public PlatformBlockState getPostBlock(int x, int y, int z, int originX, int originZ, Hunk h) { - PlatformBlockState b = h.getClosest(x - originX, y, z - originZ); + private BlockState get(int x, int y, int z) { + return blocks.get(clamp(x - originX, blocks.getWidth()), clamp(y, blocks.getHeight()), clamp(z - originZ, blocks.getDepth())); + } - return b == null ? States.AIR : b; + private static int clamp(int value, int size) { + return value < 0 ? 0 : Math.min(value, size - 1); + } + + private record Face(String property, int dx, int dy, int dz) { } }