From 818dbef2614d43bf4af9f1790707feef155cef14 Mon Sep 17 00:00:00 2001 From: piexel Date: Thu, 8 Oct 2026 14:58:04 +0200 Subject: [PATCH] Hand IrisApi heights above the dimension's bottom: terrain bounds and the mantle bridge no longer use the world's minimum height, and a test covers a dimension that starts at -64 --- .../arcane/iris/core/modules/ApiHeights.java | 42 ++++ .../core/modules/IrisGenerationContext.java | 10 +- .../modules/IrisMantleGenerationContext.java | 6 +- .../iris/core/modules/IrisModuleMantle.java | 20 +- .../core/modules/IrisPlacementInputs.java | 2 +- .../iris/core/modules/IrisTerrainSampler.java | 17 +- .../iris/core/modules/PlacementMantle.java | 9 +- .../iris/core/modules/PlacerBlockView.java | 13 +- .../core/modules/DimensionBottomTest.java | 229 ++++++++++++++++++ .../core/modules/IrisModuleMantleTest.java | 144 +++++------ 10 files changed, 379 insertions(+), 113 deletions(-) create mode 100644 core/src/main/java/art/arcane/iris/core/modules/ApiHeights.java create mode 100644 core/src/test/java/art/arcane/iris/core/modules/DimensionBottomTest.java diff --git a/core/src/main/java/art/arcane/iris/core/modules/ApiHeights.java b/core/src/main/java/art/arcane/iris/core/modules/ApiHeights.java new file mode 100644 index 000000000..a2cd710e6 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/ApiHeights.java @@ -0,0 +1,42 @@ +/* + * 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 com.volmit.mantle.BlockBox; + +/** + * Where IrisApi's heights meet Iris's. IrisApi measures every Y from the dimension's bottom (0 is the bottom + * block), and so do Iris's engine, mantle and object placers: the platform adds the world's minimum height + * only where it touches the real world. IrisApi heights and engine heights are therefore the same numbers, + * and this class is the one place that says so and builds the bounds in them. Code that needs the real world + * Y of an IrisApi height adds {@link Engine#getMinHeight()} itself, outside the bridge. + */ +final class ApiHeights { + /** The height of the dimension's lowest block. */ + static final int BOTTOM = 0; + + private ApiHeights() { + } + + /** The columns from the minima, inclusive, to the maxima, exclusive, over the dimension's whole height. */ + static BlockBox columns(Engine engine, int minX, int minZ, int maxX, int maxZ) { + return new BlockBox(minX, BOTTOM, minZ, maxX, BOTTOM + engine.getHeight(), maxZ); + } +} 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 c7f16ae8e..4f94d0bc0 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 @@ -57,9 +57,8 @@ final class IrisGenerationContext implements GenerationContext { this.blocks = blocks; this.terrain = new IrisTerrainSampler(engine, ids, originX, originZ, CHUNK_STAGE_REACH); this.settings = new ChunkModuleSettings(engine, ids); - BlockBox chunk = new BlockBox(originX, engine.getMinHeight(), originZ, - originX + CHUNK_WIDTH, engine.getMinHeight() + engine.getHeight(), originZ + CHUNK_WIDTH); - this.mantle = new IrisModuleMantle(chunk, engine.getMinHeight(), mantleChunks, mantleLayers); + BlockBox chunk = ApiHeights.columns(engine, originX, originZ, originX + CHUNK_WIDTH, originZ + CHUNK_WIDTH); + this.mantle = new IrisModuleMantle(chunk, mantleChunks, mantleLayers); } @Override @@ -67,11 +66,6 @@ final class IrisGenerationContext implements GenerationContext { return originX; } - @Override - public int originY() { - return engine.getMinHeight(); - } - @Override public int originZ() { return originZ; 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 f0eb35134..a9151032b 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 @@ -53,9 +53,9 @@ final class IrisMantleGenerationContext implements MantleGenerationContext { this.originZ = chunkZ << CHUNK_SHIFT; this.terrain = new IrisTerrainSampler(engine, ids, originX, originZ, reachBlocks); this.settings = new ChunkModuleSettings(engine, ids); - BlockBox reach = new BlockBox(originX - reachBlocks, engine.getMinHeight(), originZ - reachBlocks, - originX + CHUNK_WIDTH + reachBlocks, engine.getMinHeight() + engine.getHeight(), originZ + CHUNK_WIDTH + reachBlocks); - this.mantle = new IrisModuleMantle(reach, engine.getMinHeight(), chunks, layers); + BlockBox reach = ApiHeights.columns(engine, originX - reachBlocks, originZ - reachBlocks, + originX + CHUNK_WIDTH + reachBlocks, originZ + CHUNK_WIDTH + reachBlocks); + this.mantle = new IrisModuleMantle(reach, chunks, layers); } @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 950f33ba8..820aaa6cc 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 @@ -44,7 +44,8 @@ import java.util.function.UnaryOperator; import java.util.stream.Collectors; /** - * Iris's mantle as IrisApi's {@link Mantle}, in world coordinates, over the chunks one invocation may touch. + * Iris's mantle as IrisApi's {@link Mantle}, in world X and Z and heights above the dimension's bottom (which is + * how the mantle stores them), over the chunks one invocation may touch. * Every module layer a block holds lives in one {@link ModuleLayerValues} slice, so a layer needs no slice type * of its own and a layer no loaded module registers survives unchanged. Only registered layers can be used. * Changing a block's values is atomic per mantle chunk, so stages running on neighboring chunks at once never @@ -66,15 +67,12 @@ final class IrisModuleMantle implements Mantle { } private final BlockBox bounds; - private final int minY; private final Chunks chunks; private final Map> registrations; private BufferedEdit openEdit; - /** @param minY the world height of the mantle's lowest block */ - IrisModuleMantle(BlockBox bounds, int minY, Chunks chunks, Collection> registrations) { + IrisModuleMantle(BlockBox bounds, Chunks chunks, Collection> registrations) { this.bounds = bounds; - this.minY = minY; this.chunks = chunks; this.registrations = registrations.stream().collect(Collectors.toMap(registration -> registration.key().id(), Function.identity())); } @@ -125,7 +123,7 @@ final class IrisModuleMantle implements Mantle { @Override public T get(int x, int y, int z) { - ModuleLayerValues values = valuesAt(chunkAt(x, y, z), x, y - minY, z); + ModuleLayerValues values = valuesAt(chunkAt(x, y, z), x, y, z); return values == null ? null : values.get(key, codec); } @@ -175,9 +173,8 @@ final class IrisModuleMantle implements Mantle { MantleChunk chunk = requireChunk(chunkX, chunkZ); int originX = chunkX << CHUNK_SHIFT; int originZ = chunkZ << CHUNK_SHIFT; - chunk.iterate(ModuleLayerValues.class, (localX, mantleY, localZ, values) -> { + chunk.iterate(ModuleLayerValues.class, (localX, y, localZ, values) -> { int x = originX + localX; - int y = mantleY + minY; int z = originZ + localZ; if (values.has(key.id()) && area.contains(x, y, z)) { stored.add(new Stored<>(x, y, z, values.get(key, codec))); @@ -361,12 +358,11 @@ final class IrisModuleMantle implements Mantle { /** Replaces the values of one block under the chunk's lock, so concurrent stages on neighboring chunks keep each other's layers. */ private void change(int x, int y, int z, UnaryOperator change) { MantleChunk chunk = chunkAt(x, y, z); - int mantleY = y - minY; synchronized (chunk) { - ModuleLayerValues current = valuesAt(chunk, x, mantleY, z); + ModuleLayerValues current = valuesAt(chunk, x, y, z); ModuleLayerValues changed = change.apply(current == null ? ModuleLayerValues.EMPTY : current); - MatterSlice slice = chunk.getOrCreate(mantleY >> SECTION_SHIFT).slice(ModuleLayerValues.class); - slice.set(x & LOCAL_MASK, mantleY & LOCAL_MASK, z & LOCAL_MASK, changed.isEmpty() ? null : changed); + MatterSlice slice = chunk.getOrCreate(y >> SECTION_SHIFT).slice(ModuleLayerValues.class); + slice.set(x & LOCAL_MASK, y & LOCAL_MASK, z & LOCAL_MASK, changed.isEmpty() ? null : changed); } } 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 f90ccf7a0..4305b0b11 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 @@ -59,7 +59,7 @@ final class IrisPlacementInputs implements PlacementContext { } @Override - public BiomeSample biome(int worldX, int worldY, int worldZ) { + public BiomeSample biome(int worldX, int y, 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 9e13a2bdd..14351049e 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 @@ -31,7 +31,8 @@ import java.util.OptionalInt; /** * Iris's terrain streams for one invocation. The streams are computed, not prepared, so the bounds are * only the area the invocation may touch: a chunk or a placement widened by the pipeline's reach, at - * full height. + * full height. Heights, in the bounds and in the samples, are above the dimension's bottom, as the engine's + * streams give them. */ final class IrisTerrainSampler implements TerrainSampler { private static final int CHUNK_WIDTH_BLOCKS = 16; @@ -50,7 +51,7 @@ final class IrisTerrainSampler implements TerrainSampler { IrisTerrainSampler(Engine engine, PackResourceIds ids, int minX, int minZ, int maxX, int maxZ) { this.engine = engine; this.ids = ids; - this.bounds = new BlockBox(minX, engine.getMinHeight(), minZ, maxX, engine.getMinHeight() + engine.getHeight(), maxZ); + this.bounds = ApiHeights.columns(engine, minX, minZ, maxX, maxZ); } @Override @@ -69,19 +70,19 @@ final class IrisTerrainSampler implements TerrainSampler { } @Override - public BiomeSample biome(int worldX, int worldY, int worldZ) { - requireInside(worldX, worldY, worldZ); + public BiomeSample biome(int worldX, int y, int worldZ) { + requireInside(worldX, y, worldZ); IrisBiome surface = engine.getSurfaceBiome(worldX, worldZ); - Optional cave = Optional.ofNullable(engine.getCaveBiome(worldX, worldY, worldZ)); + Optional cave = Optional.ofNullable(engine.getCaveBiome(worldX, y, worldZ)); return new BiomeSample( ids.of(engine.getRegion(worldX, worldZ).getLoadKey()), ids.of(surface.getLoadKey()), cave.map(biome -> ids.of(biome.getLoadKey()))); } - private void requireInside(int worldX, int worldY, int worldZ) { - if (!bounds.contains(worldX, worldY, worldZ)) { - throw new IndexOutOfBoundsException("Block " + worldX + ", " + worldY + ", " + worldZ + " is outside the sampled area " + bounds); + 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/PlacementMantle.java b/core/src/main/java/art/arcane/iris/core/modules/PlacementMantle.java index 7e9a8f4ed..1f7973d80 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 @@ -31,9 +31,8 @@ import java.util.Collection; import java.util.Objects; /** - * The mantle a placement's stages see: the engine's mantle around the placement, in the placer's coordinates - * (the placer's own height, where 0 is the bottom of the dimension, so a stage's mantle writes land where its - * block writes do). Everything a stage writes is buffered in one mantle edit of the placement: it reaches the + * The mantle a placement's stages see: the engine's mantle around the placement, with IrisApi's heights, which are + * the placer's own (0 is the bottom of the dimension), so a stage's mantle writes land where its block writes do. Everything a stage writes is buffered in one mantle edit of the placement: it reaches the * mantle only when {@link #commit()} runs with the placement's own commit, and {@link #close()} discards it. * IrisApi keeps stages from opening edits of their own: the placement owns this one. *

@@ -48,9 +47,9 @@ final class PlacementMantle implements Mantle, AutoCloseable { PlacementMantle(Engine engine, BlockBox area, Collection> layers) { this.bounds = area; - this.storedBounds = new BlockBox(area.minX(), 0, area.minZ(), area.maxX(), engine.getHeight(), area.maxZ()); + this.storedBounds = ApiHeights.columns(engine, area.minX(), area.minZ(), area.maxX(), area.maxZ()); this.chunks = new PinnedMantleChunks(engine); - this.placement = new IrisModuleMantle(storedBounds, 0, chunks, layers).edit(); + this.placement = new IrisModuleMantle(storedBounds, chunks, layers).edit(); } @Override diff --git a/core/src/main/java/art/arcane/iris/core/modules/PlacerBlockView.java b/core/src/main/java/art/arcane/iris/core/modules/PlacerBlockView.java index aefea7d59..d35ff650e 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/PlacerBlockView.java +++ b/core/src/main/java/art/arcane/iris/core/modules/PlacerBlockView.java @@ -23,7 +23,10 @@ import com.volmit.block.BlockState; import com.volmit.mantle.BlockBox; import com.volmit.world.BlockView; -/** The blocks an Iris object placer holds before a placement's writes, within the placement area. */ +/** + * The blocks an Iris object placer holds before a placement's writes, within the placement area. The placer's + * heights count from the dimension's bottom, like IrisApi's. + */ final class PlacerBlockView implements BlockView { private final IObjectPlacer placer; private final BlockBox bounds; @@ -41,10 +44,10 @@ final class PlacerBlockView implements BlockView { } @Override - public BlockState get(int worldX, int worldY, int worldZ) { - if (!bounds.contains(worldX, worldY, worldZ)) { - throw new IndexOutOfBoundsException("Block " + worldX + ", " + worldY + ", " + worldZ + " is outside the placement area " + bounds); + public BlockState get(int worldX, int y, int worldZ) { + if (!bounds.contains(worldX, y, worldZ)) { + throw new IndexOutOfBoundsException("Block " + worldX + ", " + y + ", " + worldZ + " is outside the placement area " + bounds); } - return states.toApi(placer.get(worldX, worldY, worldZ)); + return states.toApi(placer.get(worldX, y, worldZ)); } } 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 new file mode 100644 index 000000000..2e1c8e104 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/modules/DimensionBottomTest.java @@ -0,0 +1,229 @@ +/* + * 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.core.loader.IrisData; +import art.arcane.iris.engine.IrisComplex; +import art.arcane.iris.engine.UsableBlockPlatform; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.placement.IObjectPlacer; +import art.arcane.iris.engine.object.placement.IrisObjectPlacement; +import art.arcane.iris.engine.object.prefab.IrisObject; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import art.arcane.iris.engine.object.world.dimension.IrisDimension; +import art.arcane.iris.engine.object.world.region.IrisRegion; +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.iris.util.project.stream.ProceduralStream; +import art.arcane.volmlib.util.math.RNG; +import com.volmit.FeatureRegistry; +import com.volmit.IrisModule; +import com.volmit.ModuleInfo; +import com.volmit.block.BlockState; +import com.volmit.host.LoadedPack; +import com.volmit.host.ModuleHost; +import com.volmit.mantle.BlockBox; +import com.volmit.mantle.MantleCodec; +import com.volmit.mantle.MantleKey; +import com.volmit.mantle.MantleLifetime; +import com.volmit.placement.PlacedBlock; +import com.volmit.stage.Stage; +import com.volmit.stage.StageGraph; +import com.volmit.world.BlockPosition; +import com.volmit.world.TerrainSampler; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * IrisApi measures every height from the dimension's bottom, whatever the dimension's minimum height is. A + * dimension reaching down to world Y -64 is the case that tells the two apart: a chunk stage, a mantle stage and a + * placement stage each write at height 0 and must hit the bottom block of the dimension, with terrain samples and + * bounds in the same heights. + */ +public class DimensionBottomTest { + private static final int WORLD_MIN_HEIGHT = -64; + private static final int DIMENSION_HEIGHT = 64; + 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); + 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<>() { + public int version() { return 1; } + public void write(DataOutput output, String value) throws IOException { output.writeUTF(value); } + public String read(DataInput input, int storedVersion) throws IOException { return input.readUTF(); } + }; + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + private final InMemoryMantle storage = new InMemoryMantle(DIMENSION_HEIGHT / 16); + private final List terrainBounds = new ArrayList<>(); + private final List viewBounds = new ArrayList<>(); + private final List surfaces = new ArrayList<>(); + private Engine engine; + private IrisData data; + private LoadedPack pack; + + /** Writes its mark and block at height 0 in whichever pipeline it runs, and records the heights it was shown. */ + private final class BottomModule implements IrisModule { + public ModuleInfo info() { return ModuleInfo.of("test:bottom", "Bottom", "1"); } + + public void install(StageGraph graph) { + graph.define(Stage.chunk("test:bottom/chunk")).run(context -> { + record(context.terrain()); + viewBounds.add(context.mantle().bounds()); + context.hunk(BlockState.class).set(0, BOTTOM, 0, STONE); + }); + graph.define(Stage.mantle("test:bottom/mantle")).reach(0).writesMantle(MARKS).run(context -> { + record(context.terrain()); + viewBounds.add(context.mantle().bounds()); + context.mantle().layer(MARKS).set(context.originX(), BOTTOM, context.originZ(), "bottom"); + }); + graph.define(Stage.placement("test:bottom/placement")).run(context -> { + record(context.terrain()); + viewBounds.add(context.access().bounds()); + IrisPlacement.current().anchoredAt(BOTTOM); + context.access().set(new PlacedBlock(new BlockPosition(0, BOTTOM, 0), STONE, Optional.empty())); + context.access().mantle().layer(MARKS).set(0, BOTTOM, 0, "bottom"); + }); + } + + public void register(FeatureRegistry registry) { + registry.registerMantle(MARKS, MantleLifetime.GENERATION, TEXT); + } + } + + private void record(TerrainSampler terrain) { + terrainBounds.add(terrain.bounds()); + surfaces.add(terrain.column(0, 0).surfaceY()); + } + + @Before + public void loadPackOfADimensionBelowWorldYZero() throws IOException { + UsableBlockPlatform.bind(); + pack = LoadedPack.load(folder.newFolder("pack").toPath(), + ModuleHost.of(List.of(new BottomModule())).copyingJarsTo(folder.newFolder("copies").toPath())); + engine = mock(Engine.class); + data = mock(IrisData.class); + IrisComplex complex = mock(IrisComplex.class); + when(complex.getHeightStream()).thenReturn(ProceduralStream.ofDouble((x, z) -> (double) SURFACE_HEIGHT)); + when(complex.getSlopeStream()).thenReturn(ProceduralStream.ofDouble((x, z) -> 0D)); + when(engine.getComplex()).thenReturn(complex); + when(engine.getMinHeight()).thenReturn(WORLD_MIN_HEIGHT); + when(engine.getHeight()).thenReturn(DIMENSION_HEIGHT); + when(engine.getData()).thenReturn(data); + when(engine.getDimension()).thenReturn(new IrisDimension()); + when(engine.getRegion(anyInt(), anyInt())).thenReturn(new IrisRegion()); + when(engine.getSurfaceBiome(anyInt(), anyInt())).thenReturn(new IrisBiome()); + BuiltInPack.seedWorld(engine, 7L); + PackModuleSet moduleSet = mock(PackModuleSet.class); + when(moduleSet.pack()).thenReturn(pack); + when(data.getModules()).thenReturn(moduleSet); + when(data.getDataFolder()).thenReturn(folder.getRoot()); + when(data.getEngine()).thenReturn(engine); + storage.bindTo(engine); + } + + @After + public void unload() { + pack.close(); + UsableBlockPlatform.unbind(); + } + + @Test + public void aChunkStageWritingAtHeightZeroHitsTheBottomBlockOfTheChunk() { + Hunk blocks = Hunk.newArrayHunk(16, DIMENSION_HEIGHT, 16); + + ModuleChunkGeneration.generate(pack, engine, 0, 0, blocks, null, false, null, B::getState); + + 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(); + } + + @Test + public void aMantleStageWritingAtHeightZeroHitsTheBottomOfTheMantle() { + ModuleMantleComponent.forPack(engine, pack, Set.of()).stream() + .filter(component -> component.stage().stage().id().toString().equals("test:bottom/mantle")) + .forEach(component -> component.generate(0, 0, storage::chunk)); + + assertEquals("bottom", storage.value(MARKS, TEXT, 0, BOTTOM, 0)); + assertSamplesAndBoundsAgree(); + } + + @Test + public void aPlacementStageWritingAtHeightZeroHitsTheBottomBlockAndTheBottomOfTheMantle() { + IObjectPlacer placer = mock(IObjectPlacer.class); + when(placer.getEngine()).thenReturn(engine); + IrisObject object = new IrisObject(1, 1, 1); + object.setUnsigned(0, 0, 0, B.getState("minecraft:stone")); + + ModuleObjectPlacement.place(object, 0, BOTTOM, 0, placer, new IrisObjectPlacement(), new RNG(2L), null, null, data); + + verify(placer).set(eq(0), eq(BOTTOM), eq(0), any(PlatformBlockState.class)); + assertEquals("bottom", storage.value(MARKS, TEXT, 0, BOTTOM, 0)); + BlockBox terrain = terrainBounds.getFirst(); + assertEquals(BOTTOM, terrain.minY()); + assertEquals(DIMENSION_HEIGHT, terrain.maxY()); + assertEquals(List.of(Double.valueOf(SURFACE_HEIGHT)), surfaces); + assertTrue("a placement may reach beyond the dimension, so its view holds at least the terrain's heights", + viewBounds.getFirst().contains(terrain)); + } + + private void assertSamplesAndBoundsAgree() { + assertEquals(List.of(WHOLE_HEIGHT_OF_ONE_CHUNK), terrainBounds); + assertEquals("the mantle view covers the same heights as the terrain", terrainBounds, viewBounds); + assertEquals("the terrain samples count from the same bottom", List.of(Double.valueOf(SURFACE_HEIGHT)), surfaces); + assertTrue(terrainBounds.getFirst().contains(0, SURFACE_HEIGHT, 0)); + } + + private static int countBlocks(Hunk blocks) { + int count = 0; + for (int y = 0; y < blocks.getHeight(); y++) { + for (int x = 0; x < blocks.getWidth(); x++) { + for (int z = 0; z < blocks.getDepth(); z++) { + count += blocks.get(x, y, z) != null ? 1 : 0; + } + } + } + return count; + } +} 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 8237ad243..bc76b31b0 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 @@ -75,8 +75,10 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class IrisModuleMantleTest { - private static final int MIN_Y = -64; + /** A dimension whose bottom is below world Y 0: IrisApi heights must not depend on it. */ + private static final int WORLD_MIN_HEIGHT = -64; private static final int SECTIONS = 4; + private static final int HEIGHT = SECTIONS * 16; private static final MantleKey MARKS = new MantleKey<>("iris:marks/mark", String.class); private static final MantleKey DEPTHS = new MantleKey<>("iris:marks/depth", Integer.class); private static final MantleCodec TEXT = new MantleCodec<>() { @@ -109,55 +111,55 @@ public class IrisModuleMantleTest { } private IrisModuleMantle mantle(BlockBox bounds) { - return new IrisModuleMantle(bounds, MIN_Y, this::chunk, LAYERS); + return new IrisModuleMantle(bounds, this::chunk, LAYERS); } @Test public void layersShareABlockAndReadInWorldCoordinatesAcrossChunks() { - IrisModuleMantle mantle = mantle(new BlockBox(-16, MIN_Y, -16, 32, 0, 32)); + IrisModuleMantle mantle = mantle(new BlockBox(-16, 0, -16, 32, HEIGHT, 32)); - mantle.layer(MARKS).set(-1, -60, 20, "west"); - mantle.layer(MARKS).set(17, -1, 3, "east"); - mantle.layer(DEPTHS).set(17, -1, 3, 9); + mantle.layer(MARKS).set(-1, 4, 20, "west"); + mantle.layer(MARKS).set(17, 63, 3, "east"); + mantle.layer(DEPTHS).set(17, 63, 3, 9); - assertEquals("west", mantle.layer(MARKS).get(-1, -60, 20)); - assertEquals("east", mantle.layer(MARKS).get(17, -1, 3)); - assertEquals(Integer.valueOf(9), mantle.layer(DEPTHS).get(17, -1, 3)); - mantle.layer(MARKS).remove(17, -1, 3); - assertNull(mantle.layer(MARKS).get(17, -1, 3)); - assertEquals(Integer.valueOf(9), mantle.layer(DEPTHS).get(17, -1, 3)); + assertEquals("west", mantle.layer(MARKS).get(-1, 4, 20)); + assertEquals("east", mantle.layer(MARKS).get(17, 63, 3)); + assertEquals(Integer.valueOf(9), mantle.layer(DEPTHS).get(17, 63, 3)); + mantle.layer(MARKS).remove(17, 63, 3); + assertNull(mantle.layer(MARKS).get(17, 63, 3)); + assertEquals(Integer.valueOf(9), mantle.layer(DEPTHS).get(17, 63, 3)); assertEquals("another invocation sees the same mantle", Integer.valueOf(9), - mantle(new BlockBox(16, MIN_Y, 0, 32, 0, 16)).layer(DEPTHS).get(17, -1, 3)); + mantle(new BlockBox(16, 0, 0, 32, HEIGHT, 16)).layer(DEPTHS).get(17, 63, 3)); } @Test public void visitsInXThenYThenZOrderAndClearsOnlyItsArea() { - IrisModuleMantle mantle = mantle(new BlockBox(-16, MIN_Y, -16, 32, 0, 32)); - mantle.layer(MARKS).set(20, -50, 0, "c"); - mantle.layer(MARKS).set(-3, -10, 5, "a"); - mantle.layer(MARKS).set(20, -60, 9, "b"); - mantle.layer(DEPTHS).set(0, -20, 0, 1); + IrisModuleMantle mantle = mantle(new BlockBox(-16, 0, -16, 32, HEIGHT, 32)); + mantle.layer(MARKS).set(20, 14, 0, "c"); + mantle.layer(MARKS).set(-3, 54, 5, "a"); + mantle.layer(MARKS).set(20, 4, 9, "b"); + mantle.layer(DEPTHS).set(0, 44, 0, 1); List visited = new ArrayList<>(); - mantle.layer(MARKS).visit(new BlockBox(-16, MIN_Y, -16, 32, 0, 32), (x, y, z, value) -> visited.add(value + "@" + x + "," + y + "," + z)); - mantle.layer(MARKS).clear(new BlockBox(16, MIN_Y, -16, 32, 0, 32)); + mantle.layer(MARKS).visit(new BlockBox(-16, 0, -16, 32, HEIGHT, 32), (x, y, z, value) -> visited.add(value + "@" + x + "," + y + "," + z)); + mantle.layer(MARKS).clear(new BlockBox(16, 0, -16, 32, HEIGHT, 32)); - assertEquals(List.of("a@-3,-10,5", "b@20,-60,9", "c@20,-50,0"), visited); - assertNull(mantle.layer(MARKS).get(20, -50, 0)); - assertEquals("a", mantle.layer(MARKS).get(-3, -10, 5)); - assertEquals(Integer.valueOf(1), mantle.layer(DEPTHS).get(0, -20, 0)); + assertEquals(List.of("a@-3,54,5", "b@20,4,9", "c@20,14,0"), visited); + assertNull(mantle.layer(MARKS).get(20, 14, 0)); + assertEquals("a", mantle.layer(MARKS).get(-3, 54, 5)); + assertEquals(Integer.valueOf(1), mantle.layer(DEPTHS).get(0, 44, 0)); } @Test public void refusesLayersNobodyRegisteredWrongTypesAndBlocksOutsideItsBounds() { - IrisModuleMantle mantle = mantle(new BlockBox(0, MIN_Y, 0, 16, 0, 16)); + IrisModuleMantle mantle = mantle(new BlockBox(0, 0, 0, 16, HEIGHT, 16)); IllegalArgumentException unregistered = assertThrows(IllegalArgumentException.class, () -> mantle.layer(new MantleKey<>("iris:marks/other", String.class))); assertEquals("Mantle layer iris:marks/other is not registered by any module of the pack", unregistered.getMessage()); assertThrows(IllegalArgumentException.class, () -> mantle.layer(new MantleKey<>("iris:marks/mark", Integer.class))); - assertThrows(IndexOutOfBoundsException.class, () -> mantle.layer(MARKS).set(16, -60, 0, "out")); - assertThrows(IndexOutOfBoundsException.class, () -> mantle.layer(MARKS).visit(new BlockBox(0, MIN_Y, 0, 17, 0, 16), (x, y, z, v) -> { })); + assertThrows(IndexOutOfBoundsException.class, () -> mantle.layer(MARKS).set(16, 4, 0, "out")); + assertThrows(IndexOutOfBoundsException.class, () -> mantle.layer(MARKS).visit(new BlockBox(0, 0, 0, 17, HEIGHT, 16), (x, y, z, v) -> { })); } @Test @@ -173,88 +175,88 @@ public class IrisModuleMantleTest { @Test public void anEditBuffersChangesAcrossLayersUntilItCommits() { - IrisModuleMantle mantle = mantle(new BlockBox(-16, MIN_Y, -16, 32, 0, 32)); - mantle.layer(MARKS).set(1, -60, 1, "stored"); - mantle.layer(MARKS).set(2, -60, 1, "removed"); + IrisModuleMantle mantle = mantle(new BlockBox(-16, 0, -16, 32, HEIGHT, 32)); + mantle.layer(MARKS).set(1, 4, 1, "stored"); + mantle.layer(MARKS).set(2, 4, 1, "removed"); List visited = new ArrayList<>(); try (MantleEdit edit = mantle.edit()) { - edit.layer(MARKS).set(20, -50, 0, "added"); - edit.layer(MARKS).remove(2, -60, 1); - edit.layer(DEPTHS).set(20, -50, 0, 3); - edit.layer(MARKS).visit(new BlockBox(-16, MIN_Y, -16, 32, 0, 32), (x, y, z, value) -> visited.add(value + "@" + x)); + edit.layer(MARKS).set(20, 14, 0, "added"); + edit.layer(MARKS).remove(2, 4, 1); + edit.layer(DEPTHS).set(20, 14, 0, 3); + edit.layer(MARKS).visit(new BlockBox(-16, 0, -16, 32, HEIGHT, 32), (x, y, z, value) -> visited.add(value + "@" + x)); - assertEquals("added", edit.layer(MARKS).get(20, -50, 0)); - assertNull(edit.layer(MARKS).get(2, -60, 1)); - assertNull("the mantle is unchanged until the commit", mantle.layer(MARKS).get(20, -50, 0)); - assertEquals("removed", mantle.layer(MARKS).get(2, -60, 1)); + assertEquals("added", edit.layer(MARKS).get(20, 14, 0)); + assertNull(edit.layer(MARKS).get(2, 4, 1)); + assertNull("the mantle is unchanged until the commit", mantle.layer(MARKS).get(20, 14, 0)); + assertEquals("removed", mantle.layer(MARKS).get(2, 4, 1)); edit.commit(); } assertEquals(List.of("stored@1", "added@20"), visited); - assertEquals("added", mantle.layer(MARKS).get(20, -50, 0)); - assertEquals(Integer.valueOf(3), mantle.layer(DEPTHS).get(20, -50, 0)); - assertNull(mantle.layer(MARKS).get(2, -60, 1)); - assertEquals("stored", mantle.layer(MARKS).get(1, -60, 1)); + assertEquals("added", mantle.layer(MARKS).get(20, 14, 0)); + assertEquals(Integer.valueOf(3), mantle.layer(DEPTHS).get(20, 14, 0)); + assertNull(mantle.layer(MARKS).get(2, 4, 1)); + assertEquals("stored", mantle.layer(MARKS).get(1, 4, 1)); } @Test public void aClosedEditChangesNothingAndAnOpenEditKeepsTheMantleReadOnly() { - IrisModuleMantle mantle = mantle(new BlockBox(0, MIN_Y, 0, 16, 0, 16)); + IrisModuleMantle mantle = mantle(new BlockBox(0, 0, 0, 16, HEIGHT, 16)); MantleEdit edit = mantle.edit(); MantleLayer marks = edit.layer(MARKS); - marks.set(1, -60, 1, "discarded"); + marks.set(1, 4, 1, "discarded"); - assertThrows(IllegalStateException.class, () -> mantle.layer(MARKS).set(0, -60, 0, "direct")); + assertThrows(IllegalStateException.class, () -> mantle.layer(MARKS).set(0, 4, 0, "direct")); assertThrows(IllegalStateException.class, mantle::edit); assertThrows(UnsupportedOperationException.class, edit::edit); - assertThrows(IndexOutOfBoundsException.class, () -> marks.set(16, -60, 0, "out")); + assertThrows(IndexOutOfBoundsException.class, () -> marks.set(16, 4, 0, "out")); edit.close(); edit.close(); - assertNull(mantle.layer(MARKS).get(1, -60, 1)); - IllegalStateException closed = assertThrows(IllegalStateException.class, () -> marks.get(1, -60, 1)); + assertNull(mantle.layer(MARKS).get(1, 4, 1)); + IllegalStateException closed = assertThrows(IllegalStateException.class, () -> marks.get(1, 4, 1)); assertEquals("This mantle edit is closed; open a new one with edit()", closed.getMessage()); assertThrows(IllegalStateException.class, edit::commit); - mantle.layer(MARKS).set(0, -60, 0, "direct"); + mantle.layer(MARKS).set(0, 4, 0, "direct"); mantle.edit().commit(); } @Test public void aCommitThatCannotReachEveryChunkPublishesNothing() { - IrisModuleMantle mantle = new IrisModuleMantle(new BlockBox(0, MIN_Y, 0, 32, 0, 16), MIN_Y, + IrisModuleMantle mantle = new IrisModuleMantle(new BlockBox(0, 0, 0, 32, HEIGHT, 16), (chunkX, chunkZ) -> chunkX == 0 ? chunk(chunkX, chunkZ) : null, LAYERS); MantleEdit edit = mantle.edit(); - edit.layer(MARKS).set(1, -60, 1, "first"); - edit.layer(MARKS).set(20, -60, 1, "unprepared"); + edit.layer(MARKS).set(1, 4, 1, "first"); + edit.layer(MARKS).set(20, 4, 1, "unprepared"); assertThrows(IllegalStateException.class, edit::commit); - assertNull(mantle.layer(MARKS).get(1, -60, 1)); + assertNull(mantle.layer(MARKS).get(1, 4, 1)); assertThrows(IllegalStateException.class, edit::commit); - assertThrows(IllegalStateException.class, () -> mantle.layer(MARKS).set(2, -60, 1, "still open")); + assertThrows(IllegalStateException.class, () -> mantle.layer(MARKS).set(2, 4, 1, "still open")); edit.close(); - mantle.layer(MARKS).set(2, -60, 1, "after close"); + mantle.layer(MARKS).set(2, 4, 1, "after close"); } @Test public void cleanupRemovesGenerationLayersAndKeepsPersistentAndUnknownOnes() throws IOException { - IrisModuleMantle mantle = mantle(new BlockBox(0, MIN_Y, 0, 16, 0, 16)); - mantle.layer(MARKS).set(1, -60, 1, "short-lived"); - mantle.layer(DEPTHS).set(1, -60, 1, 7); - mantle.layer(MARKS).set(2, -10, 3, "alone"); + IrisModuleMantle mantle = mantle(new BlockBox(0, 0, 0, 16, HEIGHT, 16)); + mantle.layer(MARKS).set(1, 4, 1, "short-lived"); + mantle.layer(DEPTHS).set(1, 4, 1, 7); + mantle.layer(MARKS).set(2, 54, 3, "alone"); MantleKey unknown = new MantleKey<>("iris:gone/layer", String.class); ModuleLayerValues fromDisk = ModuleLayerValues.read(input(bytes(ModuleLayerValues.EMPTY.with(unknown, TEXT, "kept") .with(MARKS, TEXT, "old")))); - chunk(0, 0).getOrCreate((-30 - MIN_Y) >> 4).slice(ModuleLayerValues.class).set(4, (-30 - MIN_Y) & 15, 4, fromDisk); + chunk(0, 0).getOrCreate(34 >> 4).slice(ModuleLayerValues.class).set(4, 34 & 15, 4, fromDisk); GenerationLayers.of(LAYERS).removeFrom(chunk(0, 0)); - assertNull(mantle.layer(MARKS).get(1, -60, 1)); - assertEquals(Integer.valueOf(7), mantle.layer(DEPTHS).get(1, -60, 1)); - assertNull("a block left without values holds nothing", chunk(0, 0).get((-10 - MIN_Y) >> 4) - .slice(ModuleLayerValues.class).get(2, (-10 - MIN_Y) & 15, 3)); - assertNull(mantle.layer(MARKS).get(4, -30, 4)); - ModuleLayerValues left = chunk(0, 0).get((-30 - MIN_Y) >> 4).slice(ModuleLayerValues.class).get(4, (-30 - MIN_Y) & 15, 4); + assertNull(mantle.layer(MARKS).get(1, 4, 1)); + assertEquals(Integer.valueOf(7), mantle.layer(DEPTHS).get(1, 4, 1)); + assertNull("a block left without values holds nothing", chunk(0, 0).get(54 >> 4) + .slice(ModuleLayerValues.class).get(2, 54 & 15, 3)); + assertNull(mantle.layer(MARKS).get(4, 34, 4)); + ModuleLayerValues left = chunk(0, 0).get(34 >> 4).slice(ModuleLayerValues.class).get(4, 34 & 15, 4); assertEquals("a layer no module registers stays", "kept", left.get(unknown, TEXT)); } @@ -265,7 +267,7 @@ public class IrisModuleMantleTest { public ModuleInfo info() { return ModuleInfo.of("iris:veins", "Veins", "1"); } public void install(StageGraph graph) { graph.define(Stage.mantle("iris:veins/plan")).reach(16).writesMantle(planned) - .run(context -> context.mantle().layer(planned).set(context.originX() + 20, -30, context.originZ() + 3, "vein")); + .run(context -> context.mantle().layer(planned).set(context.originX() + 20, 34, context.originZ() + 3, "vein")); } public void register(FeatureRegistry registry) { registry.registerMantle(planned, MantleLifetime.GENERATION, TEXT); } }; @@ -278,15 +280,15 @@ public class IrisModuleMantleTest { when(engine.getDimension()).thenReturn(new IrisDimension()); when(engine.getRegion(anyInt(), anyInt())).thenReturn(new IrisRegion()); when(engine.getSurfaceBiome(anyInt(), anyInt())).thenReturn(new IrisBiome()); - when(engine.getMinHeight()).thenReturn(MIN_Y); + when(engine.getMinHeight()).thenReturn(WORLD_MIN_HEIGHT); when(engine.getHeight()).thenReturn(SECTIONS * 16); try (LoadedPack pack = LoadedPack.load(folder.newFolder("pack").toPath(), 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, MIN_Y, 0, 32, 0, 16), MIN_Y, this::chunk, pack.catalog().mantle()); - assertEquals("vein", neighbor.layer(planned).get(20, -30, 3)); + IrisModuleMantle neighbor = new IrisModuleMantle(new BlockBox(16, 0, 0, 32, HEIGHT, 16), this::chunk, pack.catalog().mantle()); + assertEquals("vein", neighbor.layer(planned).get(20, 34, 3)); } assertTrue(chunks.containsKey(1L << 32)); } @@ -311,7 +313,7 @@ public class IrisModuleMantleTest { when(engine.getDimension()).thenReturn(new IrisDimension()); when(engine.getRegion(anyInt(), anyInt())).thenReturn(new IrisRegion()); when(engine.getSurfaceBiome(anyInt(), anyInt())).thenReturn(new IrisBiome()); - when(engine.getMinHeight()).thenReturn(MIN_Y); + when(engine.getMinHeight()).thenReturn(WORLD_MIN_HEIGHT); when(engine.getHeight()).thenReturn(SECTIONS * 16); try (LoadedPack pack = LoadedPack.load(folder.newFolder("finder-pack").toPath(),