diff --git a/core/src/main/java/art/arcane/iris/core/modules/ModuleObjectPlacement.java b/core/src/main/java/art/arcane/iris/core/modules/ModuleObjectPlacement.java index 22ee247bd..fb2995c78 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/ModuleObjectPlacement.java +++ b/core/src/main/java/art/arcane/iris/core/modules/ModuleObjectPlacement.java @@ -30,6 +30,7 @@ import art.arcane.iris.spi.IrisPlatforms; import art.arcane.volmlib.util.math.RNG; import com.volmit.config.FeatureSettings; import com.volmit.host.PlacementOutcome; +import com.volmit.host.LoadedPack; import com.volmit.host.PlacementRunner; import com.volmit.host.ResolvedGraph; import com.volmit.mantle.BlockBox; @@ -70,11 +71,12 @@ public final class ModuleObjectPlacement { Objects.requireNonNull(config, "Object placement config is required."); Objects.requireNonNull(rng, "Object placement RNG is required."); Objects.requireNonNull(data, "Object placement data is required."); - ResolvedGraph graph = data.getModules().pack().placement(); + LoadedPack pack = data.getModules().pack(); + ResolvedGraph graph = pack.placement(); GeneratedObject geometry = geometryOf(object, config); BlockBox area = PlacementArea.around(object, geometry, config, x, z, graph.reach().blocks()); PlatformBlockStates states = new PlatformBlockStates(ModuleObjectPlacement::platformState); - ObjectPlacerEdit edit = new ObjectPlacerEdit(placer, area, states, listener); + ObjectPlacerEdit edit = new ObjectPlacerEdit(placer, area, states, listener, () -> mantle(placer.getEngine(), area, pack)); IrisPlacement placement = new IrisPlacement(object, config, placer, cave, x, requestedY, z, data, states, edit); PackResourceIds ids = new PackResourceIds(data.getDataFolder()); String objectKey = keyOf(object); @@ -109,6 +111,13 @@ public final class ModuleObjectPlacement { return new IrisTerrainSampler(engine, ids, area.minX(), area.minZ(), area.maxX(), area.maxZ()); } + private static PlacementMantle mantle(Engine engine, BlockBox area, LoadedPack pack) { + if (engine == null) { + throw new UnsupportedOperationException("This placement runs without an Iris engine, so it has no mantle"); + } + return new PlacementMantle(engine, area, pack.catalog().mantle()); + } + private static FeatureSettings worldSettings(Engine engine, PackResourceIds ids) { return engine == null ? PlacementModuleSettings.noWorld() : new ChunkModuleSettings(engine, ids); } diff --git a/core/src/main/java/art/arcane/iris/core/modules/ObjectPlacerEdit.java b/core/src/main/java/art/arcane/iris/core/modules/ObjectPlacerEdit.java index c4e68f532..a18eda7f2 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/ObjectPlacerEdit.java +++ b/core/src/main/java/art/arcane/iris/core/modules/ObjectPlacerEdit.java @@ -36,6 +36,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.function.BiConsumer; +import java.util.function.Supplier; /** * One placement's writes, buffered over an Iris object placer until IrisApi commits them. Reads see the @@ -50,14 +51,22 @@ final class ObjectPlacerEdit implements PlacementEdit { private final BlockBox bounds; private final PlatformBlockStates states; private final BiConsumer listener; + private final Supplier mantleSource; private final Map blocks = new LinkedHashMap<>(); private final List markers = new ArrayList<>(); private final List objectBlocks = new ArrayList<>(); + private PlacementMantle mantle; private State state = State.OPEN; - /** {@code listener} hears each object block after the commit, as Iris placements report them; null for none. */ + /** + * {@code listener} hears each object block after the commit, as Iris placements report them; null for none. + * {@code mantleSource} makes the placement's mantle the first time a stage asks for it, and fails when the + * placement has none. + */ ObjectPlacerEdit(IObjectPlacer placer, BlockBox bounds, PlatformBlockStates states, - BiConsumer listener) { + BiConsumer listener, + Supplier mantleSource) { + this.mantleSource = mantleSource; this.placer = placer; this.bounds = bounds; this.states = states; @@ -95,7 +104,10 @@ final class ObjectPlacerEdit implements PlacementEdit { @Override public Mantle mantle() { requireOpen(); - throw new UnsupportedOperationException("Mantle access from placement stages is not bridged to Iris yet"); + if (mantle == null) { + mantle = mantleSource.get(); + } + return mantle; } /** Marks a buffered write as one of the object's own blocks, which the listener hears after the commit. */ @@ -108,6 +120,7 @@ final class ObjectPlacerEdit implements PlacementEdit { public void commit() { requireOpen(); state = State.FAILED; + commitMantle(); writeBlocks(); writeMarkers(); reportObjectBlocks(); @@ -116,12 +129,26 @@ final class ObjectPlacerEdit implements PlacementEdit { @Override public void close() { + discardMantle(); blocks.clear(); markers.clear(); objectBlocks.clear(); state = State.CLOSED; } + /** First, so a mantle chunk that is missing fails the commit before any block is written. */ + private void commitMantle() { + if (mantle != null) { + mantle.commit(); + } + } + + private void discardMantle() { + if (mantle != null) { + mantle.close(); + } + } + private void writeBlocks() { for (PlacedBlock block : blocks.values()) { BlockPosition position = block.position(); diff --git a/core/src/main/java/art/arcane/iris/core/modules/PinnedMantleChunks.java b/core/src/main/java/art/arcane/iris/core/modules/PinnedMantleChunks.java new file mode 100644 index 000000000..a45da4eb6 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/PinnedMantleChunks.java @@ -0,0 +1,54 @@ +/* + * 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.volmlib.util.mantle.runtime.MantleChunk; +import art.arcane.volmlib.util.matter.Matter; + +import java.util.HashMap; +import java.util.Map; + +/** + * The mantle chunks one placement touches: each is pinned the first time a stage asks for it and all are + * released together once the placement is committed or discarded. + */ +final class PinnedMantleChunks implements IrisModuleMantle.Chunks, AutoCloseable { + private final Engine engine; + private final Map> pinned = new HashMap<>(); + + PinnedMantleChunks(Engine engine) { + this.engine = engine; + } + + @Override + public synchronized MantleChunk chunk(int chunkX, int chunkZ) { + return pinned.computeIfAbsent(key(chunkX, chunkZ), ignored -> engine.getMantle().getMantle().getChunk(chunkX, chunkZ).use()); + } + + @Override + public synchronized void close() { + pinned.values().forEach(MantleChunk::release); + pinned.clear(); + } + + private static long key(int chunkX, int chunkZ) { + return ((long) chunkX << Integer.SIZE) ^ (chunkZ & 0xffffffffL); + } +} 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 new file mode 100644 index 000000000..7e9a8f4ed --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/PlacementMantle.java @@ -0,0 +1,154 @@ +/* + * 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.host.MantleRegistration; +import com.volmit.mantle.BlockBox; +import com.volmit.mantle.Mantle; +import com.volmit.mantle.MantleEdit; +import com.volmit.mantle.MantleKey; +import com.volmit.mantle.MantleLayer; +import com.volmit.mantle.MantleVisitor; + +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 + * 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. + *

+ * The placement area has no vertical limit, so the bounds say so; a block above or below the dimension holds + * nothing and cannot be written. + */ +final class PlacementMantle implements Mantle, AutoCloseable { + private final BlockBox bounds; + private final BlockBox storedBounds; + private final PinnedMantleChunks chunks; + private final MantleEdit placement; + + 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.chunks = new PinnedMantleChunks(engine); + this.placement = new IrisModuleMantle(storedBounds, 0, chunks, layers).edit(); + } + + @Override + public BlockBox bounds() { + return bounds; + } + + @Override + public MantleLayer layer(MantleKey key) { + return new StoredRange<>(placement.layer(key)); + } + + @Override + public MantleEdit edit() { + throw new UnsupportedOperationException("The placement owns the mantle edit; write through layer()"); + } + + /** Publishes everything the placement wrote, or nothing when a mantle chunk it touches is missing. */ + void commit() { + try { + placement.commit(); + } finally { + close(); + } + } + + /** Discards what was not committed and lets go of the mantle chunks. */ + @Override + public void close() { + placement.close(); + chunks.close(); + } + + /** A layer of the placement edit, limited to the heights the mantle stores. */ + private final class StoredRange implements MantleLayer { + private final MantleLayer layer; + + StoredRange(MantleLayer layer) { + this.layer = layer; + } + + @Override + public T get(int x, int y, int z) { + requireInArea(x, y, z); + return isStored(y) ? layer.get(x, y, z) : null; + } + + @Override + public void set(int x, int y, int z, T value) { + requireInArea(x, y, z); + requireStored(x, y, z); + layer.set(x, y, z, value); + } + + @Override + public void remove(int x, int y, int z) { + requireInArea(x, y, z); + if (isStored(y)) { + layer.remove(x, y, z); + } + } + + @Override + public void visit(BlockBox area, MantleVisitor visitor) { + Objects.requireNonNull(visitor, "visitor"); + layer.visit(storedPart(area), visitor); + } + + @Override + public void clear(BlockBox area) { + layer.clear(storedPart(area)); + } + + private BlockBox storedPart(BlockBox area) { + Objects.requireNonNull(area, "area"); + if (!bounds.contains(area)) { + throw new IndexOutOfBoundsException("Area " + area + " is not inside the placement area " + bounds); + } + int minY = Math.max(area.minY(), storedBounds.minY()); + int maxY = Math.min(area.maxY(), storedBounds.maxY()); + return new BlockBox(area.minX(), Math.min(minY, maxY), area.minZ(), area.maxX(), maxY, area.maxZ()); + } + + private void requireInArea(int x, int y, int z) { + if (!bounds.contains(x, y, z)) { + throw new IndexOutOfBoundsException("Block " + x + ", " + y + ", " + z + " is outside the placement area " + bounds); + } + } + + private void requireStored(int x, int y, int z) { + if (!isStored(y)) { + throw new IndexOutOfBoundsException("Block " + x + ", " + y + ", " + z + " is outside the heights the mantle stores, " + + storedBounds.minY() + " to " + (storedBounds.maxY() - 1)); + } + } + + private boolean isStored(int y) { + return y >= storedBounds.minY() && y < storedBounds.maxY(); + } + } +} diff --git a/core/src/test/java/art/arcane/iris/core/modules/InMemoryMantle.java b/core/src/test/java/art/arcane/iris/core/modules/InMemoryMantle.java new file mode 100644 index 000000000..7e83a969d --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/modules/InMemoryMantle.java @@ -0,0 +1,88 @@ +package art.arcane.iris.core.modules; + +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.mantle.EngineMantle; +import art.arcane.iris.util.project.matter.IrisMatterSupport; +import art.arcane.volmlib.util.function.Consumer4; +import art.arcane.volmlib.util.io.CountingDataInputStream; +import art.arcane.volmlib.util.mantle.runtime.Mantle; +import art.arcane.volmlib.util.mantle.runtime.MantleChunk; +import art.arcane.volmlib.util.mantle.runtime.MantleDataAdapter; +import art.arcane.volmlib.util.mantle.runtime.MantleHooks; +import art.arcane.volmlib.util.matter.IrisMatter; +import art.arcane.volmlib.util.matter.Matter; +import art.arcane.volmlib.util.matter.MatterSlice; +import com.volmit.mantle.MantleCodec; +import com.volmit.mantle.MantleKey; + +import java.io.DataOutputStream; +import java.util.HashMap; +import java.util.Map; + +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** A mantle held in memory, for tests of what stages write into it. Bind an engine to it before the engine's mantle is used. */ +public final class InMemoryMantle { + private final int sections; + private final Map> chunks = new HashMap<>(); + + /** @param sections how many 16 block sections high the mantle is */ + public InMemoryMantle(int sections) { + IrisMatterSupport.ensureRegistered(); + this.sections = sections; + } + + public MantleChunk chunk(int chunkX, int chunkZ) { + return chunks.computeIfAbsent(((long) chunkX << 32) ^ (chunkZ & 0xffffffffL), ignored -> + new MantleChunk<>(sections, chunkX, chunkZ, new Sections(), MantleHooks.NONE)); + } + + /** Whether any chunk has been created, which is whether anything asked for a chunk. */ + public boolean isTouched() { + return !chunks.isEmpty(); + } + + /** Makes {@code engine}'s mantle this one. The platform must be bound already: the engine mantle's constants read it. */ + @SuppressWarnings("unchecked") + public void bindTo(Engine engine) { + Mantle mantle = mock(Mantle.class); + when(mantle.getChunk(anyInt(), anyInt())).thenAnswer(call -> chunk(call.getArgument(0), call.getArgument(1))); + EngineMantle engineMantle = mock(EngineMantle.class); + when(engineMantle.getMantle()).thenReturn(mantle); + when(engine.getMantle()).thenReturn(engineMantle); + } + + /** What the layer holds at a block of the mantle's storage coordinates, or null. */ + public T value(MantleKey key, MantleCodec codec, int x, int mantleY, int z) { + MantleChunk chunk = chunk(x >> 4, z >> 4); + if (!chunk.exists(mantleY >> 4) || !chunk.get(mantleY >> 4).hasSlice(ModuleLayerValues.class)) { + return null; + } + ModuleLayerValues values = chunk.get(mantleY >> 4).getSlice(ModuleLayerValues.class).get(x & 15, mantleY & 15, z & 15); + return values == null ? null : values.get(key, codec); + } + + /** Iris's in-memory mantle sections, without the engine's static state that would bind a platform for every later test. */ + public static final class Sections implements MantleDataAdapter { + public Matter createSection() { return new IrisMatter(16, 16, 16); } + public Matter readSection(CountingDataInputStream input) { throw new UnsupportedOperationException(); } + public void writeSection(Matter section, DataOutputStream output) { throw new UnsupportedOperationException(); } + public void trimSection(Matter section) { section.trimSlices(); } + public boolean isSectionEmpty(Matter section) { return section.getSliceMap().isEmpty(); } + public Class classifyValue(Object value) { return value.getClass(); } + public void set(Matter section, int x, int y, int z, Class type, T value) { throw new UnsupportedOperationException(); } + public void remove(Matter section, int x, int y, int z, Class type) { throw new UnsupportedOperationException(); } + public T get(Matter section, int x, int y, int z, Class type) { return section.slice(type).get(x, y, z); } + public boolean hasSlice(Matter section, Class type) { return section.hasSlice(type); } + public void deleteSlice(Matter section, Class type) { section.deleteSlice(type); } + + public void iterate(Matter section, Class type, Consumer4 iterator) { + MatterSlice slice = section.getSlice(type); + if (slice != null) { + slice.iterateSync(iterator); + } + } + } +} 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 f054d75a9..8237ad243 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 @@ -105,7 +105,7 @@ public class IrisModuleMantleTest { private MantleChunk chunk(int chunkX, int chunkZ) { return chunks.computeIfAbsent(((long) chunkX << 32) ^ (chunkZ & 0xffffffffL), ignored -> - new MantleChunk<>(SECTIONS, chunkX, chunkZ, new Sections(), MantleHooks.NONE)); + new MantleChunk<>(SECTIONS, chunkX, chunkZ, new InMemoryMantle.Sections(), MantleHooks.NONE)); } private IrisModuleMantle mantle(BlockBox bounds) { @@ -322,28 +322,6 @@ public class IrisModuleMantleTest { assertEquals(List.of(PackResourceFixture.OAK), found); } - /** Iris's in-memory mantle sections, without the engine's static state that would bind a platform for every later test. */ - private static final class Sections implements MantleDataAdapter { - public Matter createSection() { return new IrisMatter(16, 16, 16); } - public Matter readSection(CountingDataInputStream input) { throw new UnsupportedOperationException(); } - public void writeSection(Matter section, DataOutputStream output) { throw new UnsupportedOperationException(); } - public void trimSection(Matter section) { section.trimSlices(); } - public boolean isSectionEmpty(Matter section) { return section.getSliceMap().isEmpty(); } - public Class classifyValue(Object value) { return value.getClass(); } - public void set(Matter section, int x, int y, int z, Class type, T value) { throw new UnsupportedOperationException(); } - public void remove(Matter section, int x, int y, int z, Class type) { throw new UnsupportedOperationException(); } - public T get(Matter section, int x, int y, int z, Class type) { return section.slice(type).get(x, y, z); } - public boolean hasSlice(Matter section, Class type) { return section.hasSlice(type); } - public void deleteSlice(Matter section, Class type) { section.deleteSlice(type); } - - public void iterate(Matter section, Class type, Consumer4 iterator) { - MatterSlice slice = section.getSlice(type); - if (slice != null) { - slice.iterateSync(iterator); - } - } - } - private static byte[] bytes(ModuleLayerValues values) throws IOException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); values.write(new DataOutputStream(bytes)); diff --git a/core/src/test/java/art/arcane/iris/core/modules/PlacementMantleTest.java b/core/src/test/java/art/arcane/iris/core/modules/PlacementMantleTest.java new file mode 100644 index 000000000..2c59af1c1 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/modules/PlacementMantleTest.java @@ -0,0 +1,149 @@ +package art.arcane.iris.core.modules; + +import art.arcane.iris.engine.UsableBlockPlatform; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.placement.IObjectPlacer; +import com.volmit.host.MantleRegistration; +import com.volmit.mantle.BlockBox; +import com.volmit.mantle.MantleCodec; +import com.volmit.mantle.MantleKey; +import com.volmit.mantle.MantleLifetime; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** A placement's mantle writes, direct or through an edit, reach the engine's mantle only with the placement's commit. */ +public class PlacementMantleTest { + private static final int HEIGHT = 64; + private static final int Y = 20; + private static final MantleKey MARKS = new MantleKey<>("iris:marks/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(); } + }; + private static final List> LAYERS = + List.of(new MantleRegistration<>(MARKS, MantleLifetime.GENERATION, TEXT, 1)); + + private final InMemoryMantle storage = new InMemoryMantle(HEIGHT / 16); + /** A placement area has no vertical limit. */ + private final BlockBox area = new BlockBox(-8, Integer.MIN_VALUE, -8, 24, Integer.MAX_VALUE, 24); + + @Before + public void bindPlatform() { + UsableBlockPlatform.bind(); + } + + @After + public void unbindPlatform() { + UsableBlockPlatform.unbind(); + } + + private Engine engine() { + Engine engine = mock(Engine.class); + when(engine.getHeight()).thenReturn(HEIGHT); + storage.bindTo(engine); + return engine; + } + + private ObjectPlacerEdit edit(Engine engine) { + return new ObjectPlacerEdit(mock(IObjectPlacer.class), area, new PlatformBlockStates(key -> null), null, + () -> new PlacementMantle(engine, area, LAYERS)); + } + + private String stored(int x, int z) { + return storage.value(MARKS, TEXT, x, Y, z); + } + + @Test + public void writesReachTheMantleTogetherWhenThePlacementCommits() { + ObjectPlacerEdit placement = edit(engine()); + placement.mantle().layer(MARKS).set(1, Y, 1, "near"); + placement.mantle().layer(MARKS).set(17, Y, 2, "next chunk"); + placement.mantle().layer(MARKS).set(17, Y, 3, "removed"); + placement.mantle().layer(MARKS).remove(17, Y, 3); + + assertEquals("a stage reads what it wrote", "near", placement.mantle().layer(MARKS).get(1, Y, 1)); + assertNull(placement.mantle().layer(MARKS).get(17, Y, 3)); + assertNull("nothing is in the mantle before the placement commits", stored(1, 1)); + assertNull(stored(17, 2)); + placement.commit(); + + assertEquals("near", stored(1, 1)); + assertEquals("next chunk", stored(17, 2)); + assertNull(stored(17, 3)); + } + + @Test + public void aDiscardedPlacementLeavesTheMantleAsItWas() { + ObjectPlacerEdit placement = edit(engine()); + placement.mantle().layer(MARKS).set(1, Y, 1, "written"); + + placement.close(); + + assertNull(stored(1, 1)); + } + + @Test + public void aStageCannotOpenAnEditOfItsOwn() { + ObjectPlacerEdit placement = edit(engine()); + + assertThrows(UnsupportedOperationException.class, () -> placement.mantle().edit()); + } + + @Test + public void visitsAndClearsStayWithinTheHeightsTheMantleStores() { + ObjectPlacerEdit placement = edit(engine()); + placement.mantle().layer(MARKS).set(1, Y, 1, "low"); + placement.mantle().layer(MARKS).set(2, HEIGHT - 1, 1, "high"); + List visited = new ArrayList<>(); + + placement.mantle().layer(MARKS).visit(area, (x, y, z, value) -> visited.add(value)); + placement.mantle().layer(MARKS).clear(area); + + assertEquals(List.of("low", "high"), visited); + assertNull(placement.mantle().layer(MARKS).get(1, Y, 1)); + } + + @Test + public void aPlacementWhoseStagesNeverAskForTheMantleNeverTouchesIt() { + Engine engine = engine(); + ObjectPlacerEdit placement = edit(engine); + + placement.commit(); + + assertFalse(storage.isTouched()); + } + + @Test + public void writesOutsideThePlacementAreaFail() { + ObjectPlacerEdit placement = edit(engine()); + + assertThrows(IndexOutOfBoundsException.class, () -> placement.mantle().layer(MARKS).set(24, Y, 0, "out")); + assertThrows("above the heights the mantle stores", IndexOutOfBoundsException.class, + () -> placement.mantle().layer(MARKS).set(0, HEIGHT, 0, "above")); + assertNull("but reading there is allowed and finds nothing", placement.mantle().layer(MARKS).get(0, HEIGHT + 5, 0)); + } + + @Test + public void aPlacementWithoutAnEngineHasNoMantle() { + ObjectPlacerEdit placement = new ObjectPlacerEdit(mock(IObjectPlacer.class), area, new PlatformBlockStates(key -> null), null, () -> { + throw new UnsupportedOperationException("This placement runs without an Iris engine, so it has no mantle"); + }); + + assertThrows(UnsupportedOperationException.class, placement::mantle); + } +} diff --git a/core/src/test/java/art/arcane/iris/engine/object/placement/IrisObjectPlacementRegressionTest.java b/core/src/test/java/art/arcane/iris/engine/object/placement/IrisObjectPlacementRegressionTest.java index 549a858b8..0af79aa90 100644 --- a/core/src/test/java/art/arcane/iris/engine/object/placement/IrisObjectPlacementRegressionTest.java +++ b/core/src/test/java/art/arcane/iris/engine/object/placement/IrisObjectPlacementRegressionTest.java @@ -17,6 +17,7 @@ import art.arcane.iris.builtin.blocks.BlocksStages; import art.arcane.iris.builtin.bore.BoreStages; import art.arcane.iris.core.loader.IrisData; import art.arcane.iris.core.modules.BuiltInPack; +import art.arcane.iris.core.modules.InMemoryMantle; import art.arcane.iris.core.modules.ModuleObjectPlacement; import art.arcane.iris.core.modules.PackResourceFixture; import art.arcane.iris.core.modules.ObjectPlacementResult; @@ -32,10 +33,15 @@ import art.arcane.iris.spi.PlatformRegistries; import art.arcane.iris.util.project.stream.ProceduralStream; import art.arcane.volmlib.util.collection.KList; 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.mantle.BlockBox; +import com.volmit.mantle.Mantle; +import com.volmit.mantle.MantleCodec; +import com.volmit.mantle.MantleKey; +import com.volmit.mantle.MantleLifetime; import com.volmit.object.GeneratedObject; import com.volmit.object.ObjectBlock; import com.volmit.object.ObjectTransform; @@ -60,6 +66,7 @@ import java.util.Optional; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; @@ -447,6 +454,63 @@ public class IrisObjectPlacementRegressionTest { assertEquals(List.of(PackResourceFixture.OAK), found); } + private static final MantleKey MARKS = new MantleKey<>("test:mark/spot", String.class); + private static final MantleCodec TEXT = new MantleCodec<>() { + public int version() { return 1; } + public void write(java.io.DataOutput output, String value) throws IOException { output.writeUTF(value); } + public String read(java.io.DataInput input, int storedVersion) throws IOException { return input.readUTF(); } + }; + + /** Marks the mantle beside the anchor. */ + private static final class MarkModule implements IrisModule { + private static final PlacementStage MARK = Stage.placement("test:mark/spot"); + + @Override + public ModuleInfo info() { + return ModuleInfo.of("test:mark", "Mark", "1"); + } + + @Override + public void install(StageGraph graph) { + graph.define(MARK).requires(BlocksStages.COMPLETE).run(context -> { + Mantle mantle = context.access().mantle(); + mantle.layer(MARKS).set(0, ANCHOR_Y, 0, "first"); + mantle.layer(MARKS).set(1, ANCHOR_Y, 0, "second"); + }); + } + + @Override + public void register(FeatureRegistry registry) { + registry.registerMantle(MARKS, MantleLifetime.GENERATION, TEXT); + } + } + + @Test + public void aPlacementStageMarksTheMantleWithTheCommitOfThePlacement() throws IOException { + InMemoryMantle storage = new InMemoryMantle(16); + storage.bindTo(engine); + usePack(List.of(new MarkModule())); + + ObjectPlacementResult result = place(placement(), ANCHOR_Y, new RecordingPlacer(engine), null); + + assertTrue(result.isPlaced()); + assertEquals("first", storage.value(MARKS, TEXT, 0, ANCHOR_Y, 0)); + assertEquals("second", storage.value(MARKS, TEXT, 1, ANCHOR_Y, 0)); + } + + @Test + public void aRejectedPlacementLeavesTheMantleUnmarked() throws IOException { + InMemoryMantle storage = new InMemoryMantle(16); + storage.bindTo(engine); + usePack(List.of(new MarkModule(), new VetoModule())); + + ObjectPlacementResult result = place(placement(), ANCHOR_Y, new RecordingPlacer(engine), null); + + assertFalse(result.isPlaced()); + assertNull(storage.value(MARKS, TEXT, 0, ANCHOR_Y, 0)); + assertNull(storage.value(MARKS, TEXT, 1, ANCHOR_Y, 0)); + } + /** Lays the pack's oak on its side, a quarter turn about X, through the placement's object transformer. */ private static final class TurnModule implements IrisModule { private static final PlacementStage TURN = Stage.placement("test:turn/oak");