From fb348d26c81c84ec4d5f8a35e27a74a525edea4e Mon Sep 17 00:00:00 2001 From: piexel Date: Thu, 8 Oct 2026 11:48:11 +0200 Subject: [PATCH] Run the pack's mantle pipeline as one mantle component and bridge the mantle to modules ModuleMantleComponent runs the mantle stages of every chunk the mantle prepares, after Iris's own components, with a radius of the pipeline's declared reach; it is disabled while the pack has no mantle stages. IrisModuleMantle gives stages Iris's mantle in world coordinates: every module layer of a block is kept in one ModuleLayerValues slice, encoded with the layer's codec, and layers no loaded module registers keep their bytes. Chunk stages read their own chunk's mantle. --- .../core/modules/IrisGenerationContext.java | 20 +- .../modules/IrisMantleGenerationContext.java | 96 +++++++ .../iris/core/modules/IrisModuleMantle.java | 218 ++++++++++++++++ .../core/modules/ModuleChunkGeneration.java | 11 +- .../iris/core/modules/ModuleLayerValues.java | 168 +++++++++++++ .../core/modules/ModuleMantleComponent.java | 90 +++++++ .../iris/core/modules/PinnedMantleChunk.java | 59 +++++ .../arcane/iris/engine/IrisEngineMantle.java | 2 + .../project/matter/IrisMatterSupport.java | 2 + .../matter/slices/ModuleLayerMatter.java | 55 ++++ .../core/modules/IrisModuleMantleTest.java | 235 ++++++++++++++++++ 11 files changed, 948 insertions(+), 8 deletions(-) create mode 100644 core/src/main/java/art/arcane/iris/core/modules/IrisMantleGenerationContext.java create mode 100644 core/src/main/java/art/arcane/iris/core/modules/IrisModuleMantle.java create mode 100644 core/src/main/java/art/arcane/iris/core/modules/ModuleLayerValues.java create mode 100644 core/src/main/java/art/arcane/iris/core/modules/ModuleMantleComponent.java create mode 100644 core/src/main/java/art/arcane/iris/core/modules/PinnedMantleChunk.java create mode 100644 core/src/main/java/art/arcane/iris/util/project/matter/slices/ModuleLayerMatter.java create mode 100644 core/src/test/java/art/arcane/iris/core/modules/IrisModuleMantleTest.java 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 d308a2952..fb83d5438 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 @@ -22,20 +22,25 @@ import art.arcane.iris.engine.framework.Engine; import com.volmit.block.BlockState; import com.volmit.config.FeatureSettings; import com.volmit.generation.GenerationContext; +import com.volmit.host.MantleRegistration; +import com.volmit.mantle.BlockBox; import com.volmit.mantle.Mantle; import com.volmit.resource.PackResources; import com.volmit.world.TerrainSampler; +import java.util.Collection; + /** * One chunk of an Iris engine as an IrisApi chunk invocation: the chunk's block hunk, Iris's terrain - * streams and the pack's module settings. Mantle access and pack resources are not bridged yet; a stage - * that asks for them fails the chunk with an error saying so, rather than seeing an empty mantle or - * pack. + * streams, the pack's module settings and the chunk's mantle, which IrisApi gives chunk stages read-only. + * Pack resources are not bridged yet; a stage that asks for them fails the chunk with an error saying so, + * rather than seeing an empty pack. */ final class IrisGenerationContext implements GenerationContext { private static final String NOT_BRIDGED = " is not bridged to Iris yet; modules cannot use it until a built-in port needs it"; /** Chunk stages write only their own chunk. */ private static final int CHUNK_STAGE_REACH = 0; + private static final int CHUNK_WIDTH = 16; private final Engine engine; private final int originX; @@ -43,8 +48,10 @@ final class IrisGenerationContext implements GenerationContext { private final BlockStateHunk blocks; private final IrisTerrainSampler terrain; private final ChunkModuleSettings settings; + private final IrisModuleMantle mantle; - IrisGenerationContext(Engine engine, int originX, int originZ, BlockStateHunk blocks) { + IrisGenerationContext(Engine engine, int originX, int originZ, BlockStateHunk blocks, IrisModuleMantle.Chunks mantleChunks, + Collection> mantleLayers) { PackResourceIds ids = new PackResourceIds(engine.getData().getDataFolder()); this.engine = engine; this.originX = originX; @@ -52,6 +59,9 @@ 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); } @Override @@ -81,7 +91,7 @@ final class IrisGenerationContext implements GenerationContext { @Override public Mantle mantle() { - throw new UnsupportedOperationException("Mantle access from module stages" + NOT_BRIDGED); + return mantle; } @Override diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisMantleGenerationContext.java b/core/src/main/java/art/arcane/iris/core/modules/IrisMantleGenerationContext.java new file mode 100644 index 000000000..b142adf8a --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisMantleGenerationContext.java @@ -0,0 +1,96 @@ +/* + * 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.config.FeatureSettings; +import com.volmit.generation.MantleGenerationContext; +import com.volmit.host.MantleRegistration; +import com.volmit.mantle.BlockBox; +import com.volmit.mantle.Mantle; +import com.volmit.resource.PackResources; +import com.volmit.world.TerrainSampler; + +import java.util.Collection; + +/** + * One chunk of an Iris engine as an IrisApi mantle invocation: the mantle around the chunk as far as the + * pipeline reaches, Iris's terrain streams over the same area and the pack's module settings. Pack resources + * are not bridged yet; a stage that asks for them fails with an error saying so. + */ +final class IrisMantleGenerationContext implements MantleGenerationContext { + private static final int CHUNK_SHIFT = 4; + private static final int CHUNK_WIDTH = 16; + + private final Engine engine; + private final int originX; + private final int originZ; + private final IrisTerrainSampler terrain; + private final ChunkModuleSettings settings; + private final IrisModuleMantle mantle; + + IrisMantleGenerationContext(Engine engine, int chunkX, int chunkZ, int reachBlocks, IrisModuleMantle.Chunks chunks, + Collection> layers) { + PackResourceIds ids = new PackResourceIds(engine.getData().getDataFolder()); + this.engine = engine; + this.originX = chunkX << CHUNK_SHIFT; + 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); + } + + @Override + public int originX() { + return originX; + } + + @Override + public int originZ() { + return originZ; + } + + @Override + public TerrainSampler terrain() { + return terrain; + } + + @Override + public FeatureSettings settings() { + return settings; + } + + @Override + public Mantle mantle() { + return mantle; + } + + @Override + public long worldSeed() { + return engine.getSeedManager().getSeed(); + } + + @Override + public PackResources resources() { + throw new UnsupportedOperationException("Pack resources for module stages are not bridged to Iris yet;" + + " modules cannot use them until a built-in port needs them"); + } +} 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 new file mode 100644 index 000000000..edf85c7ac --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisModuleMantle.java @@ -0,0 +1,218 @@ +/* + * 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.volmlib.util.mantle.runtime.MantleChunk; +import art.arcane.volmlib.util.matter.Matter; +import art.arcane.volmlib.util.matter.MatterSlice; +import com.volmit.host.MantleRegistration; +import com.volmit.mantle.BlockBox; +import com.volmit.mantle.Mantle; +import com.volmit.mantle.MantleCodec; +import com.volmit.mantle.MantleEdit; +import com.volmit.mantle.MantleKey; +import com.volmit.mantle.MantleLayer; +import com.volmit.mantle.MantleVisitor; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * Iris's mantle as IrisApi's {@link Mantle}, in world coordinates, 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 + * lose each other's layers. + */ +final class IrisModuleMantle implements Mantle { + private static final int CHUNK_SHIFT = 4; + private static final int SECTION_SHIFT = 4; + private static final int LOCAL_MASK = 15; + private static final Comparator> VISIT_ORDER = Comparator.>comparingInt(Stored::x) + .thenComparingInt(Stored::y).thenComparingInt(Stored::z); + + /** The mantle chunk at chunk coordinates, or null when it is outside what the invocation prepared. */ + interface Chunks { + MantleChunk chunk(int chunkX, int chunkZ); + } + + private final BlockBox bounds; + private final int minY; + private final Chunks chunks; + private final Map> registrations; + + /** @param minY the world height of the mantle's lowest block */ + IrisModuleMantle(BlockBox bounds, int minY, 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())); + } + + @Override + public BlockBox bounds() { + return bounds; + } + + @Override + @SuppressWarnings("unchecked") // The registration's type was compared with the key's. + public MantleLayer layer(MantleKey key) { + Objects.requireNonNull(key, "key"); + MantleRegistration registration = registrations.get(key.id()); + if (registration == null) { + throw new IllegalArgumentException("Mantle layer " + key.id() + " is not registered by any module of the pack"); + } + if (registration.key().type() != key.type()) { + throw new IllegalArgumentException("Mantle layer " + key.id() + " is registered as " + registration.key().type().getName() + + ", not " + key.type().getName()); + } + return new Layer<>(key, (MantleCodec) registration.codec()); + } + + @Override + public MantleEdit edit() { + throw new UnsupportedOperationException("Buffered mantle edits are not bridged to Iris yet; write through layer(...)"); + } + + private record Stored(int x, int y, int z, T value) {} + + private final class Layer implements MantleLayer { + private final MantleKey key; + private final MantleCodec codec; + + Layer(MantleKey key, MantleCodec codec) { + this.key = key; + this.codec = codec; + } + + @Override + public T get(int x, int y, int z) { + ModuleLayerValues values = valuesAt(chunkAt(x, y, z), x, y - minY, z); + return values == null ? null : values.get(key, codec); + } + + @Override + public void set(int x, int y, int z, T value) { + Objects.requireNonNull(value, "value"); + if (!key.type().isInstance(value)) { + throw new IllegalArgumentException("Mantle layer " + key.id() + " holds " + key.type().getName() + ", not " + + value.getClass().getName()); + } + change(x, y, z, values -> values.with(key, codec, value)); + } + + @Override + public void remove(int x, int y, int z) { + change(x, y, z, values -> values.without(key.id())); + } + + @Override + public void visit(BlockBox area, MantleVisitor visitor) { + Objects.requireNonNull(visitor, "visitor"); + for (Stored stored : storedIn(area)) { + visitor.accept(stored.x(), stored.y(), stored.z(), stored.value()); + } + } + + @Override + public void clear(BlockBox area) { + for (Stored stored : storedIn(area)) { + remove(stored.x(), stored.y(), stored.z()); + } + } + + private void change(int x, int y, int z, Function change) { + MantleChunk chunk = chunkAt(x, y, z); + int mantleY = y - minY; + synchronized (chunk) { + ModuleLayerValues current = valuesAt(chunk, x, mantleY, 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); + } + } + + /** This layer's values in {@code area}, ordered by x, then y, then z, as {@link MantleLayer#visit} promises. */ + private List> storedIn(BlockBox area) { + requireInside(area); + List> stored = new ArrayList<>(); + for (int chunkX = area.minX() >> CHUNK_SHIFT; chunkX <= (area.maxX() - 1) >> CHUNK_SHIFT; chunkX++) { + for (int chunkZ = area.minZ() >> CHUNK_SHIFT; chunkZ <= (area.maxZ() - 1) >> CHUNK_SHIFT; chunkZ++) { + collect(chunkX, chunkZ, area, stored); + } + } + stored.sort(VISIT_ORDER); + return stored; + } + + private void collect(int chunkX, int chunkZ, BlockBox area, List> stored) { + MantleChunk chunk = requireChunk(chunkX, chunkZ); + int originX = chunkX << CHUNK_SHIFT; + int originZ = chunkZ << CHUNK_SHIFT; + chunk.iterate(ModuleLayerValues.class, (localX, mantleY, 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))); + } + }); + } + } + + private MantleChunk chunkAt(int x, int y, int z) { + if (!bounds.contains(x, y, z)) { + throw new IndexOutOfBoundsException("Block " + x + ", " + y + ", " + z + " is outside the mantle this invocation prepared, " + bounds); + } + return requireChunk(x >> CHUNK_SHIFT, z >> CHUNK_SHIFT); + } + + private MantleChunk requireChunk(int chunkX, int chunkZ) { + MantleChunk chunk = chunks.chunk(chunkX, chunkZ); + if (chunk == null) { + throw new IllegalStateException("Mantle chunk " + chunkX + ", " + chunkZ + " is inside " + bounds + " but was not prepared"); + } + return chunk; + } + + private void requireInside(BlockBox area) { + Objects.requireNonNull(area, "area"); + if (!bounds.contains(area)) { + throw new IndexOutOfBoundsException("Area " + area + " is not inside the mantle this invocation prepared, " + bounds); + } + } + + private static ModuleLayerValues valuesAt(MantleChunk chunk, int x, int mantleY, int z) { + int section = mantleY >> SECTION_SHIFT; + if (!chunk.exists(section)) { + return null; + } + Matter matter = chunk.get(section); + if (matter == null || !matter.hasSlice(ModuleLayerValues.class)) { + return null; + } + return matter.getSlice(ModuleLayerValues.class).get(x & LOCAL_MASK, mantleY & LOCAL_MASK, z & LOCAL_MASK); + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/ModuleChunkGeneration.java b/core/src/main/java/art/arcane/iris/core/modules/ModuleChunkGeneration.java index a6ecde362..5b74935a0 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/ModuleChunkGeneration.java +++ b/core/src/main/java/art/arcane/iris/core/modules/ModuleChunkGeneration.java @@ -54,8 +54,13 @@ public final class ModuleChunkGeneration { if (chunk.stages().isEmpty()) { return; } - IrisGenerationContext inputs = new IrisGenerationContext(engine, x, z, new BlockStateHunk(blocks, platformStates)); - long seed = ModuleSeeds.chunk(engine.getSeedManager().getSeed(), x >> CHUNK_SHIFT, z >> CHUNK_SHIFT); - new IrisChunk(engine, x, z, blocks, context).runBound(() -> new ChunkRunner(chunk).generate(inputs, seed)); + int chunkX = x >> CHUNK_SHIFT; + int chunkZ = z >> CHUNK_SHIFT; + long seed = ModuleSeeds.chunk(engine.getSeedManager().getSeed(), chunkX, chunkZ); + try (PinnedMantleChunk mantle = new PinnedMantleChunk(engine, chunkX, chunkZ)) { + IrisGenerationContext inputs = new IrisGenerationContext(engine, x, z, new BlockStateHunk(blocks, platformStates), mantle, + pack.catalog().mantle()); + new IrisChunk(engine, x, z, blocks, context).runBound(() -> new ChunkRunner(chunk).generate(inputs, seed)); + } } } diff --git a/core/src/main/java/art/arcane/iris/core/modules/ModuleLayerValues.java b/core/src/main/java/art/arcane/iris/core/modules/ModuleLayerValues.java new file mode 100644 index 000000000..91fd7bf55 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/ModuleLayerValues.java @@ -0,0 +1,168 @@ +/* + * 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 com.volmit.mantle.MantleCodec; +import com.volmit.mantle.MantleKey; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.Map; +import java.util.Objects; +import java.util.TreeMap; + +/** + * The values module mantle layers hold at one block, by layer id. A value a stage wrote stays decoded together + * with its layer's codec; a value read from disk stays encoded until a stage reads it with its layer's codec. + * A layer no loaded module registers therefore keeps its stored bytes and is written back unchanged. + */ +public final class ModuleLayerValues { + public static final ModuleLayerValues EMPTY = new ModuleLayerValues(new TreeMap<>()); + + private final Map layers; + + private ModuleLayerValues(TreeMap layers) { + this.layers = Collections.unmodifiableMap(layers); + } + + /** The value of {@code key}, decoded with {@code codec} if it was read from disk; null when the layer holds none. */ + public T get(MantleKey key, MantleCodec codec) { + LayerValue value = layers.get(key.id()); + return value == null ? null : key.type().cast(value.decode(key, codec)); + } + + public boolean has(String layerId) { + return layers.containsKey(layerId); + } + + public boolean isEmpty() { + return layers.isEmpty(); + } + + /** These values with {@code key} holding {@code value}. */ + public ModuleLayerValues with(MantleKey key, MantleCodec codec, T value) { + TreeMap changed = new TreeMap<>(layers); + changed.put(key.id(), new Decoded<>(value, codec)); + return new ModuleLayerValues(changed); + } + + /** These values without {@code layerId}'s. */ + public ModuleLayerValues without(String layerId) { + if (!layers.containsKey(layerId)) { + return this; + } + TreeMap changed = new TreeMap<>(layers); + changed.remove(layerId); + return changed.isEmpty() ? EMPTY : new ModuleLayerValues(changed); + } + + /** Layer count, then per layer its id, codec version, payload length and payload. */ + public void write(DataOutputStream output) throws IOException { + output.writeInt(layers.size()); + for (Map.Entry layer : layers.entrySet()) { + output.writeUTF(layer.getKey()); + layer.getValue().encoded().write(output); + } + } + + public static ModuleLayerValues read(DataInputStream input) throws IOException { + int count = input.readInt(); + TreeMap layers = new TreeMap<>(); + for (int index = 0; index < count; index++) { + layers.put(input.readUTF(), Encoded.read(input)); + } + return layers.isEmpty() ? EMPTY : new ModuleLayerValues(layers); + } + + @Override + public boolean equals(Object other) { + return other instanceof ModuleLayerValues values && layers.equals(values.layers); + } + + @Override + public int hashCode() { + return layers.hashCode(); + } + + private sealed interface LayerValue permits Decoded, Encoded { + Object decode(MantleKey key, MantleCodec codec); + + Encoded encoded() throws IOException; + } + + private record Decoded(T value, MantleCodec codec) implements LayerValue { + @Override + public Object decode(MantleKey key, MantleCodec requested) { + return value; + } + + @Override + public Encoded encoded() throws IOException { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + codec.write(new DataOutputStream(bytes), value); + return new Encoded(codec.version(), bytes.toByteArray()); + } + } + + private record Encoded(int version, byte[] payload) implements LayerValue { + static Encoded read(DataInputStream input) throws IOException { + int version = input.readInt(); + byte[] payload = new byte[input.readInt()]; + input.readFully(payload); + return new Encoded(version, payload); + } + + void write(DataOutputStream output) throws IOException { + output.writeInt(version); + output.writeInt(payload.length); + output.write(payload); + } + + @Override + public Object decode(MantleKey key, MantleCodec codec) { + try { + return codec.read(new DataInputStream(new ByteArrayInputStream(payload)), version); + } catch (IOException failure) { + throw new UncheckedIOException("Mantle layer " + key.id() + " holds version " + version + + " data its codec cannot read", failure); + } + } + + @Override + public Encoded encoded() { + return this; + } + + @Override + public boolean equals(Object other) { + return other instanceof Encoded encoded && version == encoded.version && Arrays.equals(payload, encoded.payload); + } + + @Override + public int hashCode() { + return Objects.hash(version, Arrays.hashCode(payload)); + } + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/ModuleMantleComponent.java b/core/src/main/java/art/arcane/iris/core/modules/ModuleMantleComponent.java new file mode 100644 index 000000000..a5c4a9d15 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/ModuleMantleComponent.java @@ -0,0 +1,90 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.mantle.EngineMantle; +import art.arcane.iris.engine.mantle.IrisMantleComponent; +import art.arcane.iris.engine.mantle.MantleWriter; +import art.arcane.iris.util.project.context.ChunkContext; +import art.arcane.volmlib.util.mantle.flag.MantleFlag; +import com.volmit.host.LoadedPack; +import com.volmit.host.MantleRunner; +import com.volmit.host.ResolvedGraph; + +/** + * Runs the pack's mantle pipeline as one of Iris's mantle components: once for every chunk the mantle prepares + * around a generating chunk, as far as the pipeline's declared reach. It is the last pass, so its stages see what + * Iris's own components wrote, and it is disabled while the pack has no mantle stages. + */ +public final class ModuleMantleComponent extends IrisMantleComponent { + /** Marks a mantle chunk whose module stages have run. */ + public static final MantleFlag FLAG = MantleFlag.of(MantleFlag.MIN_ORDINAL); + /** After every Iris component, whatever priority they use. */ + private static final int PRIORITY = Integer.MAX_VALUE; + + /** Whether the pack has mantle stages; asked on every chunk, so kept until the next hotload. */ + private volatile Boolean hasStages; + + public ModuleMantleComponent(EngineMantle engineMantle) { + super(engineMantle, FLAG, PRIORITY); + } + + @Override + public boolean isEnabled() { + return super.isEnabled() && hasStages(); + } + + @Override + public void hotload() { + super.hotload(); + hasStages = null; + } + + @Override + protected int computeRadius() { + return pipeline().reach().blocks(); + } + + @Override + public void generateLayer(MantleWriter writer, int x, int z, ChunkContext context) { + Engine engine = getEngineMantle().getEngine(); + generate(engine.getData().getModules().pack(), engine, x, z, getRadius(), writer::acquireChunk); + } + + /** Runs the mantle stages of chunk {@code x}, {@code z}, with the mantle chunks within {@code reachBlocks} of it. */ + static void generate(LoadedPack pack, Engine engine, int x, int z, int reachBlocks, IrisModuleMantle.Chunks chunks) { + IrisMantleGenerationContext inputs = new IrisMantleGenerationContext(engine, x, z, reachBlocks, chunks, pack.catalog().mantle()); + long seed = ModuleSeeds.chunk(engine.getSeedManager().getSeed(), x, z); + new MantleRunner(pack.mantle()).generate(inputs, seed); + } + + private boolean hasStages() { + Boolean known = hasStages; + if (known == null) { + known = !pipeline().stages().isEmpty(); + hasStages = known; + } + return known; + } + + private ResolvedGraph pipeline() { + return getEngineMantle().getEngine().getData().getModules().pack().mantle(); + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/PinnedMantleChunk.java b/core/src/main/java/art/arcane/iris/core/modules/PinnedMantleChunk.java new file mode 100644 index 000000000..bee6ae674 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/PinnedMantleChunk.java @@ -0,0 +1,59 @@ +/* + * 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; + +/** + * The one mantle chunk a generating chunk's stages may read: pinned the first time a stage asks for it and + * released once the chunk's stages are done. + */ +final class PinnedMantleChunk implements IrisModuleMantle.Chunks, AutoCloseable { + private final Engine engine; + private final int chunkX; + private final int chunkZ; + private MantleChunk pinned; + + PinnedMantleChunk(Engine engine, int chunkX, int chunkZ) { + this.engine = engine; + this.chunkX = chunkX; + this.chunkZ = chunkZ; + } + + @Override + public synchronized MantleChunk chunk(int x, int z) { + if (x != chunkX || z != chunkZ) { + return null; + } + if (pinned == null) { + pinned = engine.getMantle().getMantle().getChunk(chunkX, chunkZ).use(); + } + return pinned; + } + + @Override + public synchronized void close() { + if (pinned != null) { + pinned.release(); + pinned = null; + } + } +} diff --git a/core/src/main/java/art/arcane/iris/engine/IrisEngineMantle.java b/core/src/main/java/art/arcane/iris/engine/IrisEngineMantle.java index 794d758d2..9ee7373f6 100644 --- a/core/src/main/java/art/arcane/iris/engine/IrisEngineMantle.java +++ b/core/src/main/java/art/arcane/iris/engine/IrisEngineMantle.java @@ -20,6 +20,7 @@ package art.arcane.iris.engine; import art.arcane.iris.core.IrisSettings; import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.core.modules.ModuleMantleComponent; import art.arcane.iris.core.tools.WorldMaintenance; import art.arcane.iris.engine.data.cache.AtomicCache; import art.arcane.iris.engine.framework.Engine; @@ -90,6 +91,7 @@ public class IrisEngineMantle implements EngineMantle { registerComponent(object); registerComponent(new MantleFloatingObjectComponent(this)); registerComponent(new IrisStructureComponent(this)); + registerComponent(new ModuleMantleComponent(this)); } @Override diff --git a/core/src/main/java/art/arcane/iris/util/project/matter/IrisMatterSupport.java b/core/src/main/java/art/arcane/iris/util/project/matter/IrisMatterSupport.java index 31c03221d..99a7c0a04 100644 --- a/core/src/main/java/art/arcane/iris/util/project/matter/IrisMatterSupport.java +++ b/core/src/main/java/art/arcane/iris/util/project/matter/IrisMatterSupport.java @@ -23,6 +23,7 @@ import art.arcane.iris.engine.object.prefab.IrisObject; import art.arcane.iris.spi.IrisLogging; import art.arcane.iris.util.project.matter.slices.EntityMatter; import art.arcane.iris.util.project.matter.slices.IdentifierMatter; +import art.arcane.iris.util.project.matter.slices.ModuleLayerMatter; import art.arcane.iris.util.project.matter.slices.NativeStructureOwnershipMatter; import art.arcane.iris.util.project.matter.slices.PlatformBlockMatter; import art.arcane.iris.util.project.matter.slices.SpawnerMatter; @@ -48,6 +49,7 @@ public final class IrisMatterSupport { IrisMatter.registerSliceType(new EntityMatter()); IrisMatter.registerSliceType(new IdentifierMatter()); + IrisMatter.registerSliceType(new ModuleLayerMatter()); IrisMatter.registerSliceType(new NativeStructureOwnershipMatter()); IrisMatter.registerSliceType(new PlatformBlockMatter()); IrisMatter.registerSliceType(new SpawnerMatter()); diff --git a/core/src/main/java/art/arcane/iris/util/project/matter/slices/ModuleLayerMatter.java b/core/src/main/java/art/arcane/iris/util/project/matter/slices/ModuleLayerMatter.java new file mode 100644 index 000000000..9f4cb35fa --- /dev/null +++ b/core/src/main/java/art/arcane/iris/util/project/matter/slices/ModuleLayerMatter.java @@ -0,0 +1,55 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.util.project.matter.slices; + +import art.arcane.iris.core.modules.ModuleLayerValues; +import art.arcane.volmlib.util.data.palette.Palette; +import art.arcane.volmlib.util.matter.Sliced; +import art.arcane.volmlib.util.matter.slices.RawMatter; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +/** Stores the values of every module mantle layer at a block in one slice. */ +@Sliced +public class ModuleLayerMatter extends RawMatter { + public ModuleLayerMatter() { + this(1, 1, 1); + } + + public ModuleLayerMatter(int width, int height, int depth) { + super(width, height, depth, ModuleLayerValues.class); + } + + @Override + public Palette getGlobalPalette() { + return null; + } + + @Override + public void writeNode(ModuleLayerValues values, DataOutputStream output) throws IOException { + values.write(output); + } + + @Override + public ModuleLayerValues readNode(DataInputStream input) throws IOException { + return ModuleLayerValues.read(input); + } +} 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 new file mode 100644 index 000000000..a8c06e3e8 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/modules/IrisModuleMantleTest.java @@ -0,0 +1,235 @@ +/* + * 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.framework.Engine; +import art.arcane.iris.engine.framework.SeedManager; +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.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.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.FeatureRegistry; +import com.volmit.IrisModule; +import com.volmit.ModuleInfo; +import com.volmit.host.LoadedPack; +import com.volmit.host.MantleRegistration; +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.stage.Stage; +import com.volmit.stage.StageGraph; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.DataOutput; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class IrisModuleMantleTest { + private static final int MIN_Y = -64; + private static final int SECTIONS = 4; + 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<>() { + 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 MantleCodec NUMBER = new MantleCodec<>() { + public int version() { return 0; } + public void write(DataOutput output, Integer value) throws IOException { output.writeInt(value); } + public Integer read(DataInput input, int storedVersion) throws IOException { return input.readInt(); } + }; + private static final List> LAYERS = List.of( + new MantleRegistration<>(MARKS, MantleLifetime.GENERATION, TEXT, 1), + new MantleRegistration<>(DEPTHS, MantleLifetime.PERSISTENT, NUMBER, 0)); + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + private final Map> chunks = new HashMap<>(); + + @Before + public void registerSlices() { + IrisMatterSupport.ensureRegistered(); + } + + 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)); + } + + private IrisModuleMantle mantle(BlockBox bounds) { + return new IrisModuleMantle(bounds, MIN_Y, this::chunk, LAYERS); + } + + @Test + public void layersShareABlockAndReadInWorldCoordinatesAcrossChunks() { + IrisModuleMantle mantle = mantle(new BlockBox(-16, MIN_Y, -16, 32, 0, 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); + + 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("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)); + } + + @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); + 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)); + + 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)); + } + + @Test + public void refusesLayersNobodyRegisteredWrongTypesAndBlocksOutsideItsBounds() { + IrisModuleMantle mantle = mantle(new BlockBox(0, MIN_Y, 0, 16, 0, 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) -> { })); + } + + @Test + public void storedValuesDecodeWithTheirCodecAndUnknownLayersSurviveUnchanged() throws IOException { + ModuleLayerValues written = ModuleLayerValues.EMPTY.with(MARKS, TEXT, "kept").with(DEPTHS, NUMBER, 4); + ModuleLayerValues read = ModuleLayerValues.read(input(bytes(written))); + + assertEquals("kept", read.get(MARKS, TEXT)); + assertEquals(Integer.valueOf(4), read.get(DEPTHS, NUMBER)); + ModuleLayerValues withoutCodecs = ModuleLayerValues.read(input(bytes(written))).without(MARKS.id()); + assertArrayEquals(bytes(ModuleLayerValues.EMPTY.with(DEPTHS, NUMBER, 4)), bytes(withoutCodecs)); + } + + @Test + public void mantleStagesWriteAroundTheirChunkThroughIrisChunks() throws IOException { + MantleKey planned = new MantleKey<>("iris:veins/planned", String.class); + IrisModule veins = new IrisModule() { + 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")); + } + public void register(FeatureRegistry registry) { registry.registerMantle(planned, MantleLifetime.GENERATION, TEXT); } + }; + Engine engine = mock(Engine.class); + IrisData data = mock(IrisData.class); + SeedManager seeds = mock(SeedManager.class); + when(engine.getData()).thenReturn(data); + when(data.getDataFolder()).thenReturn(folder.getRoot()); + when(engine.getSeedManager()).thenReturn(seeds); + 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.getHeight()).thenReturn(SECTIONS * 16); + + try (LoadedPack pack = LoadedPack.load(folder.newFolder("pack").toPath(), + ModuleHost.of(List.of(veins)).copyingJarsTo(folder.newFolder("copies").toPath()))) { + ModuleMantleComponent.generate(pack, engine, 0, 0, pack.mantle().reach().blocks(), 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)); + } + assertTrue(chunks.containsKey(1L << 32)); + } + + /** 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)); + return bytes.toByteArray(); + } + + private static DataInputStream input(byte[] bytes) { + return new DataInputStream(new ByteArrayInputStream(bytes)); + } +}