Bridge mantle access for placement stages: writes are buffered with the placement and reach the mantle on its commit

This commit is contained in:
piexel committed 2026-10-08 14:09:27 +02:00
1 parent a07788fffe
commit 9b20fc9c91
8 files changed
+551 -28

No files matched your search

@@ -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);
}
@@ -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<art.arcane.volmlib.util.math.BlockPosition, PlatformBlockState> listener;
private final Supplier<PlacementMantle> mantleSource;
private final Map<BlockPosition, PlacedBlock> blocks = new LinkedHashMap<>();
private final List<PlacedMarker> markers = new ArrayList<>();
private final List<BlockPosition> 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<art.arcane.volmlib.util.math.BlockPosition, PlatformBlockState> listener) {
BiConsumer<art.arcane.volmlib.util.math.BlockPosition, PlatformBlockState> listener,
Supplier<PlacementMantle> 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();
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<Long, MantleChunk<Matter>> pinned = new HashMap<>();
PinnedMantleChunks(Engine engine) {
this.engine = engine;
}
@Override
public synchronized MantleChunk<Matter> 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);
}
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
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.
* <p>
* 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<MantleRegistration<?>> 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 <T> MantleLayer<T> layer(MantleKey<T> 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<T> implements MantleLayer<T> {
private final MantleLayer<T> layer;
StoredRange(MantleLayer<T> 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<T> 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();
}
}
}