mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 08:14:45 +00:00
Port a slice of the Iris placement lifecycle as example modules
Prepare/transforms, anchor height, block writes, stilt and snow are rebuilt as independent iris:* modules, plus a third-party moss module that integrates via requires, softAfter and a typed output. A two-module chunk example proves the generic machinery outside placement.
This commit is contained in:
1 parent
eeb5486098
commit
d78a66772a
21 files changed
+849
No files matched your search
@@ -0,0 +1,64 @@
|
||||
package com.volmit.example;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.example.chunk.GrassModule;
|
||||
import com.volmit.example.chunk.GrassSettings;
|
||||
import com.volmit.example.chunk.TerrainModule;
|
||||
import com.volmit.host.ArrayHunk;
|
||||
import com.volmit.host.ChunkRunner;
|
||||
import com.volmit.host.ConfiguredSettings;
|
||||
import com.volmit.host.StageGraphBuilder;
|
||||
import com.volmit.hunk.Hunk;
|
||||
import com.volmit.stage.Pipeline;
|
||||
import com.volmit.support.TestContexts;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** The chunk pipeline runs on the same graph machinery as placement. */
|
||||
class ChunkPipelineTest {
|
||||
private static final int SIZE = 4;
|
||||
private static final int HEIGHT = 8;
|
||||
private static final BlockState AIR = new BlockState("minecraft:air");
|
||||
private static final BlockState GRASS = new BlockState("minecraft:grass_block");
|
||||
|
||||
private static ChunkRunner runner() {
|
||||
var builder = new StageGraphBuilder();
|
||||
builder.install(new GrassModule());
|
||||
builder.install(new TerrainModule());
|
||||
var graph = builder.resolve(Pipeline.CHUNK);
|
||||
assertEquals(List.of("iris:terrain/surface", "example:grass/top"),
|
||||
graph.stages().stream().map(stage -> stage.stage().id().toString()).toList());
|
||||
assertTrue(graph.blockPasses().isEmpty());
|
||||
return new ChunkRunner(graph);
|
||||
}
|
||||
|
||||
private static Hunk<BlockState> generate(ConfiguredSettings settings) {
|
||||
var blocks = new ArrayHunk<>(BlockState.class, SIZE, HEIGHT, SIZE, AIR);
|
||||
runner().generate(TestContexts.generation(Map.of(BlockState.class, blocks)), settings);
|
||||
return blocks;
|
||||
}
|
||||
|
||||
@Test
|
||||
void decorationReadsTerrainOutputWhenConfigured() {
|
||||
var blocks = generate(ConfiguredSettings.of(new GrassSettings()));
|
||||
|
||||
for (int x = 0; x < SIZE; x++) {
|
||||
for (int z = 0; z < SIZE; z++) {
|
||||
int height = TerrainModule.surfaceHeight(x, z);
|
||||
assertEquals(TerrainModule.STONE, blocks.get(x, height - 2, z));
|
||||
assertEquals(GRASS, blocks.get(x, height - 1, z));
|
||||
assertEquals(AIR, blocks.get(x, height, z));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void decorationIsInactiveWithoutItsSettings() {
|
||||
var blocks = generate(ConfiguredSettings.none());
|
||||
|
||||
int height = TerrainModule.surfaceHeight(0, 0);
|
||||
assertEquals(TerrainModule.STONE, blocks.get(0, height - 1, 0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package com.volmit.example;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.example.placement.*;
|
||||
import com.volmit.host.ConfiguredSettings;
|
||||
import com.volmit.host.LoadedModules;
|
||||
import com.volmit.host.PlacementRunner;
|
||||
import com.volmit.host.ResolvedGraph;
|
||||
import com.volmit.host.StageGraphBuilder;
|
||||
import com.volmit.mantle.BlockBox;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import com.volmit.placement.PlacedBlock;
|
||||
import com.volmit.placement.PlacementRequest;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.stage.Pipeline;
|
||||
import com.volmit.stage.PlacementStage;
|
||||
import com.volmit.stage.Stage;
|
||||
import com.volmit.stage.StageGraph;
|
||||
import com.volmit.support.MemoryEdit;
|
||||
import com.volmit.support.TestContexts;
|
||||
import com.volmit.support.TestTerrain;
|
||||
import com.volmit.world.BlockPosition;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** The ported slice of Iris's place() lifecycle, composed only from module-owned stages. */
|
||||
class PlacementLifecyclePortTest {
|
||||
private static final BlockState PLANKS = new BlockState("minecraft:oak_planks");
|
||||
private static final int ANCHOR_GROUND = 10;
|
||||
private static final int SURROUNDING_GROUND = 7;
|
||||
private static final BlockPosition SURFACE_ANCHOR = new BlockPosition(0, ANCHOR_GROUND, 0);
|
||||
|
||||
private static final List<String> EXPECTED_ORDER = List.of(
|
||||
"iris:prepare/transforms", "iris:anchor/height", "iris:prepare/rotate", "iris:blocks/write",
|
||||
"iris:snow/collect", "iris:stilt/bounds", "iris:blocks/complete", "iris:stilt/fill",
|
||||
"iris:snow/finish", "example:moss/spread");
|
||||
|
||||
private static final String EXPECTED_DESCRIPTION = """
|
||||
PLACEMENT pipeline: 10 stages, 1 block pass
|
||||
order:
|
||||
1. iris:prepare/transforms [invocation] module iris:prepare
|
||||
2. iris:anchor/height [invocation] module iris:anchor
|
||||
requires iris:prepare/transforms
|
||||
3. iris:prepare/rotate [block pass 1] module iris:prepare
|
||||
requires iris:prepare/transforms
|
||||
4. iris:blocks/write [block pass 1] module iris:blocks
|
||||
requires iris:anchor/height, iris:prepare/rotate
|
||||
5. iris:snow/collect [block pass 1] module iris:snow when iris:snow
|
||||
requires iris:blocks/write
|
||||
6. iris:stilt/bounds [block pass 1] module iris:stilt when iris:stilt
|
||||
requires iris:anchor/height, iris:prepare/rotate
|
||||
7. iris:blocks/complete [invocation] module iris:blocks
|
||||
requires iris:blocks/write
|
||||
8. iris:stilt/fill [invocation] module iris:stilt when iris:stilt
|
||||
requires iris:stilt/bounds, iris:blocks/complete
|
||||
9. iris:snow/finish [invocation] module iris:snow when iris:snow
|
||||
requires iris:snow/collect, iris:blocks/complete
|
||||
soft after iris:stilt/fill
|
||||
absent soft targets iris:vacuum/finish
|
||||
10. example:moss/spread [invocation] module example:moss when example:moss
|
||||
requires iris:anchor/height, iris:blocks/complete
|
||||
soft after iris:snow/finish
|
||||
block passes:
|
||||
pass 1: iris:prepare/rotate, iris:blocks/write, iris:snow/collect, iris:stilt/bounds
|
||||
mutually unordered:
|
||||
iris:anchor/height <-> iris:prepare/rotate
|
||||
iris:blocks/write <-> iris:stilt/bounds
|
||||
iris:snow/collect <-> iris:stilt/bounds
|
||||
iris:snow/collect <-> iris:blocks/complete
|
||||
iris:snow/collect <-> iris:stilt/fill
|
||||
iris:stilt/bounds <-> iris:blocks/complete
|
||||
""";
|
||||
|
||||
private static ResolvedGraph resolve(List<? extends IrisModule> modules) {
|
||||
var builder = new StageGraphBuilder();
|
||||
modules.forEach(builder::install);
|
||||
return builder.resolve(Pipeline.PLACEMENT);
|
||||
}
|
||||
|
||||
private static List<String> order(ResolvedGraph graph) {
|
||||
return graph.stages().stream().map(stage -> stage.stage().id().toString()).toList();
|
||||
}
|
||||
|
||||
/** An L-shaped object: two bottom blocks and one block stacked on the second. */
|
||||
private static PlacementRequest request() {
|
||||
var object = new GeneratedObject(new BlockBox(0, 0, 0, 2, 2, 1), new BlockPosition(0, 0, 0),
|
||||
List.of(new ObjectBlock(new BlockPosition(0, 0, 0), PLANKS),
|
||||
new ObjectBlock(new BlockPosition(1, 0, 0), PLANKS),
|
||||
new ObjectBlock(new BlockPosition(1, 1, 0), PLANKS)),
|
||||
List.of());
|
||||
return new PlacementRequest(ResourceId.parse("test:attempt"), object, new BlockPosition(0, 0, 0));
|
||||
}
|
||||
|
||||
private static TestTerrain terrain() {
|
||||
return new TestTerrain((x, z) -> x == 0 && z == 0 ? ANCHOR_GROUND : SURROUNDING_GROUND);
|
||||
}
|
||||
|
||||
private static MemoryEdit place(ResolvedGraph graph, ConfiguredSettings settings) {
|
||||
var terrain = terrain();
|
||||
var edit = new MemoryEdit(terrain);
|
||||
var outcome = new PlacementRunner(graph).place(request(), edit, TestContexts.placement(terrain), settings);
|
||||
assertTrue(outcome.isPlaced());
|
||||
return edit;
|
||||
}
|
||||
|
||||
/** World position of a local block after the seeded rotation and surface anchoring. */
|
||||
private static BlockPosition placed(int x, int y, int z) {
|
||||
var transforms = new Transforms(new Random(TestContexts.SEED).nextInt(4));
|
||||
var rotated = transforms.rotate(new BlockPosition(x, y, z), new BlockPosition(0, 0, 0));
|
||||
return rotated.add(SURFACE_ANCHOR);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesThePortedLifecycleWithOneFusedBlockPass() {
|
||||
var graph = resolve(PlacementModules.all());
|
||||
|
||||
assertEquals(EXPECTED_ORDER, order(graph));
|
||||
assertEquals(1, graph.blockPasses().size());
|
||||
assertEquals(EXPECTED_DESCRIPTION, graph.describe());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvedOrderIsIndependentOfModuleInstallOrder() {
|
||||
var expected = resolve(PlacementModules.all()).describe();
|
||||
var modules = new ArrayList<>(PlacementModules.all());
|
||||
Collections.reverse(modules);
|
||||
assertEquals(expected, resolve(modules).describe());
|
||||
for (long seed = 0; seed < 16; seed++) {
|
||||
Collections.shuffle(modules, new Random(seed));
|
||||
assertEquals(expected, resolve(modules).describe(), "install order " + modules);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void placesObjectThenStiltSnowAndMossFromTypedOutputs() {
|
||||
var edit = place(resolve(PlacementModules.all()),
|
||||
ConfiguredSettings.of(new StiltSettings(), new SnowSettings(2), new MossSettings()));
|
||||
|
||||
var bottom = placed(0, 0, 0);
|
||||
var corner = placed(1, 0, 0);
|
||||
var top = placed(1, 1, 0);
|
||||
var snow = SnowModule.SNOW.withProperty("layers", "2");
|
||||
var expected = new HashMap<BlockPosition, BlockState>();
|
||||
expected.put(bottom, PLANKS);
|
||||
expected.put(corner, PLANKS);
|
||||
expected.put(top, PLANKS);
|
||||
for (int y = SURROUNDING_GROUND; y < ANCHOR_GROUND; y++) {
|
||||
expected.put(new BlockPosition(corner.x(), y, corner.z()), PLANKS);
|
||||
}
|
||||
expected.put(bottom.add(new BlockPosition(0, 1, 0)), snow);
|
||||
expected.put(top.add(new BlockPosition(0, 1, 0)), snow);
|
||||
expected.put(new BlockPosition(0, ANCHOR_GROUND - 1, 0), MossModule.MOSS);
|
||||
|
||||
assertTrue(edit.committed());
|
||||
assertEquals(expected, edit.published());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unconfiguredFeaturesStayInactiveWhileTheCoreStillPlaces() {
|
||||
var edit = place(resolve(PlacementModules.all()), ConfiguredSettings.none());
|
||||
|
||||
assertEquals(Map.of(placed(0, 0, 0), PLANKS, placed(1, 0, 0), PLANKS, placed(1, 1, 0), PLANKS), edit.published());
|
||||
}
|
||||
|
||||
@Test
|
||||
void inactiveRequirementDeactivatesThirdPartyDependents() {
|
||||
var capModule = new IrisModule() {
|
||||
static final PlacementStage<Void> CAP = Stage.placement("example:cap/top");
|
||||
public ModuleInfo info() { return ModuleInfo.of("example:cap", "1"); }
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(CAP).requires(StiltModule.FILL).run(context -> context.access().set(
|
||||
new PlacedBlock(new BlockPosition(5, 20, 5), PLANKS, Optional.empty())));
|
||||
}
|
||||
};
|
||||
var modules = new ArrayList<IrisModule>(PlacementModules.all());
|
||||
modules.add(capModule);
|
||||
var graph = resolve(modules);
|
||||
|
||||
var withoutStilt = place(graph, ConfiguredSettings.of(new SnowSettings(), new MossSettings()));
|
||||
assertEquals(TestTerrain.AIR, withoutStilt.publishedAt(5, 20, 5));
|
||||
assertEquals(MossModule.MOSS, withoutStilt.publishedAt(0, ANCHOR_GROUND - 1, 0));
|
||||
|
||||
var withStilt = place(graph, ConfiguredSettings.of(new StiltSettings()));
|
||||
assertEquals(PLANKS, withStilt.publishedAt(5, 20, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void loadingThePortCatalogsEveryConditioningSettingsType() {
|
||||
var loaded = LoadedModules.load(PlacementModules.all(), Map.of());
|
||||
|
||||
assertEquals(List.of("example:moss", "iris:snow", "iris:stilt"),
|
||||
loaded.catalog().settings().stream().map(settings -> settings.id().toString()).toList());
|
||||
assertEquals(EXPECTED_ORDER, order(loaded.placement()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.volmit.example.chunk;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.stage.ChunkStage;
|
||||
import com.volmit.stage.ChunkStageContext;
|
||||
import com.volmit.stage.Stage;
|
||||
import com.volmit.stage.StageGraph;
|
||||
|
||||
/** Third-party decoration: tops terrain columns using the terrain module's typed output. */
|
||||
public final class GrassModule implements IrisModule {
|
||||
public static final ChunkStage<Void> TOP = Stage.chunk("example:grass/top");
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("example:grass", "1.0.0");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(TOP)
|
||||
.when(GrassSettings.class)
|
||||
.requires(TerrainModule.SURFACE)
|
||||
.run(GrassModule::cover);
|
||||
}
|
||||
|
||||
private static void cover(ChunkStageContext context, GrassSettings grass) {
|
||||
var top = new BlockState(grass.block());
|
||||
var heights = context.output(TerrainModule.SURFACE);
|
||||
var blocks = context.hunk(BlockState.class);
|
||||
for (int x = 0; x < blocks.getWidth(); x++) {
|
||||
for (int z = 0; z < blocks.getDepth(); z++) {
|
||||
int height = heights.at(x, z);
|
||||
if (height > 0) {
|
||||
blocks.set(x, height - 1, z, top);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.volmit.example.chunk;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.config.Feature;
|
||||
|
||||
@Feature(id = "example:grass", name = "Grass", field = "grass", targets = FeatureAttachment.Target.BIOME)
|
||||
public final class GrassSettings implements IrisFeature {
|
||||
private String block = "minecraft:grass_block";
|
||||
|
||||
public String block() {
|
||||
return block;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.volmit.example.chunk;
|
||||
|
||||
/** Per-chunk output: the first air y of each local column. */
|
||||
public final class SurfaceHeights {
|
||||
private final int width;
|
||||
private final int[] heights;
|
||||
|
||||
SurfaceHeights(int width, int depth) {
|
||||
this.width = width;
|
||||
heights = new int[width * depth];
|
||||
}
|
||||
|
||||
void set(int x, int z, int height) {
|
||||
heights[z * width + x] = height;
|
||||
}
|
||||
|
||||
public int at(int x, int z) {
|
||||
return heights[z * width + x];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.volmit.example.chunk;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.stage.ChunkStage;
|
||||
import com.volmit.stage.ChunkStageContext;
|
||||
import com.volmit.stage.Stage;
|
||||
import com.volmit.stage.StageGraph;
|
||||
|
||||
/** Core terrain shaping as an ordinary module: fills stone and reports surface heights. */
|
||||
public final class TerrainModule implements IrisModule {
|
||||
public static final ChunkStage<SurfaceHeights> SURFACE = Stage.chunk("iris:terrain/surface", SurfaceHeights.class);
|
||||
public static final BlockState STONE = new BlockState("minecraft:stone");
|
||||
private static final int BASE_HEIGHT = 3;
|
||||
private static final int RIPPLE = 3;
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:terrain", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(SURFACE).produce(TerrainModule::shape);
|
||||
}
|
||||
|
||||
public static int surfaceHeight(int worldX, int worldZ) {
|
||||
return BASE_HEIGHT + Math.floorMod(worldX + worldZ, RIPPLE);
|
||||
}
|
||||
|
||||
private static SurfaceHeights shape(ChunkStageContext context) {
|
||||
var blocks = context.hunk(BlockState.class);
|
||||
var heights = new SurfaceHeights(blocks.getWidth(), blocks.getDepth());
|
||||
for (int x = 0; x < blocks.getWidth(); x++) {
|
||||
for (int z = 0; z < blocks.getDepth(); z++) {
|
||||
int height = Math.min(surfaceHeight(context.originX() + x, context.originZ() + z), blocks.getHeight());
|
||||
for (int y = 0; y < height; y++) {
|
||||
blocks.set(x, y, z, STONE);
|
||||
}
|
||||
heights.set(x, z, height);
|
||||
}
|
||||
}
|
||||
return heights;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.stage.PlacementStage;
|
||||
import com.volmit.stage.PlacementStageContext;
|
||||
import com.volmit.stage.Stage;
|
||||
import com.volmit.stage.StageGraph;
|
||||
import com.volmit.world.BlockPosition;
|
||||
import com.volmit.world.BlockView;
|
||||
|
||||
/** Port of PlacementModuleAnchor with center-height sampling: rest the anchor on the surface. */
|
||||
public final class AnchorModule implements IrisModule {
|
||||
public static final PlacementStage<AnchorResult> HEIGHT = Stage.placement("iris:anchor/height", AnchorResult.class);
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:anchor", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(HEIGHT)
|
||||
.requires(PrepareModule.TRANSFORMS)
|
||||
.produce(AnchorModule::restOnSurface);
|
||||
}
|
||||
|
||||
private static AnchorResult restOnSurface(PlacementStageContext context) {
|
||||
var requested = context.request().anchor();
|
||||
int surfaceY = firstAirAboveGround(context.blocks(), requested.x(), requested.z());
|
||||
return new AnchorResult(new BlockPosition(requested.x(), surfaceY, requested.z()));
|
||||
}
|
||||
|
||||
private static int firstAirAboveGround(BlockView blocks, int x, int z) {
|
||||
var bounds = blocks.bounds();
|
||||
for (int y = bounds.maxY() - 1; y >= bounds.minY(); y--) {
|
||||
if (!ExampleBlocks.isAir(blocks.get(x, y, z))) {
|
||||
return y + 1;
|
||||
}
|
||||
}
|
||||
return bounds.minY();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.placement.PlacementRequest;
|
||||
import com.volmit.world.BlockPosition;
|
||||
|
||||
/** The world position the object's local anchor is placed at. */
|
||||
public record AnchorResult(BlockPosition anchor) {
|
||||
public BlockPosition worldPosition(PlacementRequest request, BlockPosition local) {
|
||||
return new PlacementRequest(request.id(), request.object(), anchor).worldPosition(local);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.placement.PlacedBlock;
|
||||
import com.volmit.stage.BlockStage;
|
||||
import com.volmit.stage.BlockStageContext;
|
||||
import com.volmit.stage.PlacementStage;
|
||||
import com.volmit.stage.Stage;
|
||||
import com.volmit.stage.StageGraph;
|
||||
|
||||
/** Port of PlacementModuleBlocks: write each transformed block; COMPLETE marks the pass done. */
|
||||
public final class BlocksModule implements IrisModule {
|
||||
public static final BlockStage<Void> WRITE = Stage.placementBlock("iris:blocks/write");
|
||||
/** Milestone: stages requiring it run after every object block has been written. */
|
||||
public static final PlacementStage<Void> COMPLETE = Stage.placement("iris:blocks/complete");
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:blocks", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(WRITE)
|
||||
.requires(PrepareModule.ROTATE, AnchorModule.HEIGHT)
|
||||
.run(BlocksModule::write);
|
||||
graph.define(COMPLETE)
|
||||
.requires(WRITE)
|
||||
.run(context -> {});
|
||||
}
|
||||
|
||||
private static void write(BlockStageContext context) {
|
||||
var block = context.block();
|
||||
var position = context.output(AnchorModule.HEIGHT).worldPosition(context.request(), block.position());
|
||||
context.access().set(new PlacedBlock(position, block.state(), block.blockEntity()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
|
||||
final class ExampleBlocks {
|
||||
static final BlockState AIR = new BlockState("minecraft:air");
|
||||
|
||||
private ExampleBlocks() {}
|
||||
|
||||
static boolean isAir(BlockState state) {
|
||||
return state.material().equals(AIR.material());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.placement.PlacedBlock;
|
||||
import com.volmit.stage.PlacementStage;
|
||||
import com.volmit.stage.PlacementStageContext;
|
||||
import com.volmit.stage.Stage;
|
||||
import com.volmit.stage.StageGraph;
|
||||
import com.volmit.stage.StageRef;
|
||||
import com.volmit.world.BlockPosition;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Third-party module: turns the ground under the anchor into moss. It requires the core
|
||||
* anchor height (typed output) and block completion, and runs after snow only if snow exists.
|
||||
*/
|
||||
public final class MossModule implements IrisModule {
|
||||
public static final PlacementStage<Void> SPREAD = Stage.placement("example:moss/spread");
|
||||
public static final BlockState MOSS = new BlockState("minecraft:moss_block");
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("example:moss", "1.0.0");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(SPREAD)
|
||||
.when(MossSettings.class)
|
||||
.requires(AnchorModule.HEIGHT, BlocksModule.COMPLETE)
|
||||
.softAfter(StageRef.of("iris:snow/finish"))
|
||||
.run(MossModule::spread);
|
||||
}
|
||||
|
||||
private static void spread(PlacementStageContext context, MossSettings moss) {
|
||||
var anchor = context.output(AnchorModule.HEIGHT).anchor();
|
||||
var access = context.access();
|
||||
int groundY = anchor.y() - 1;
|
||||
for (int dx = -moss.radius(); dx <= moss.radius(); dx++) {
|
||||
for (int dz = -moss.radius(); dz <= moss.radius(); dz++) {
|
||||
int x = anchor.x() + dx;
|
||||
int z = anchor.z() + dz;
|
||||
if (!ExampleBlocks.isAir(access.get(x, groundY, z))) {
|
||||
access.set(new PlacedBlock(new BlockPosition(x, groundY, z), MOSS, Optional.empty()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.config.Feature;
|
||||
import com.volmit.config.MinNumber;
|
||||
|
||||
@Feature(id = "example:moss", name = "Moss", field = "moss", targets = FeatureAttachment.Target.OBJECT_PLACEMENT)
|
||||
public final class MossSettings implements IrisFeature {
|
||||
@MinNumber(0)
|
||||
private int radius;
|
||||
|
||||
public MossSettings() {}
|
||||
|
||||
public MossSettings(int radius) {
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
public int radius() {
|
||||
return radius;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import java.util.List;
|
||||
|
||||
public final class PlacementModules {
|
||||
private PlacementModules() {}
|
||||
|
||||
/** The ported core lifecycle slice plus one third-party module. */
|
||||
public static List<IrisModule> all() {
|
||||
return List.of(new PrepareModule(), new AnchorModule(), new BlocksModule(),
|
||||
new StiltModule(), new SnowModule(), new MossModule());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import com.volmit.stage.BlockStage;
|
||||
import com.volmit.stage.BlockStageContext;
|
||||
import com.volmit.stage.PlacementStage;
|
||||
import com.volmit.stage.Stage;
|
||||
import com.volmit.stage.StageGraph;
|
||||
|
||||
/** Port of PlacementModulePreparation: choose transforms once, rotate each block in the pass. */
|
||||
public final class PrepareModule implements IrisModule {
|
||||
public static final PlacementStage<Transforms> TRANSFORMS = Stage.placement("iris:prepare/transforms", Transforms.class);
|
||||
public static final BlockStage<Void> ROTATE = Stage.placementBlock("iris:prepare/rotate");
|
||||
private static final int QUARTER_TURNS = 4;
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:prepare", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(TRANSFORMS)
|
||||
.produce(context -> new Transforms(context.random().nextInt(QUARTER_TURNS)));
|
||||
graph.define(ROTATE)
|
||||
.requires(TRANSFORMS)
|
||||
.run(PrepareModule::rotate);
|
||||
}
|
||||
|
||||
private static void rotate(BlockStageContext context) {
|
||||
var transforms = context.output(TRANSFORMS);
|
||||
var block = context.block();
|
||||
var pivot = context.request().object().anchor();
|
||||
context.replace(new ObjectBlock(transforms.rotate(block.position(), pivot), block.state(), block.blockEntity()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.world.BlockPosition;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/** Per-placement accumulator: the highest written world block of each column. */
|
||||
public final class SnowHeights {
|
||||
private final Map<Column, Integer> highest = new TreeMap<>();
|
||||
|
||||
public void include(BlockPosition written) {
|
||||
highest.merge(new Column(written.x(), written.z()), written.y(), Math::max);
|
||||
}
|
||||
|
||||
public List<BlockPosition> tops() {
|
||||
return highest.entrySet().stream()
|
||||
.map(entry -> new BlockPosition(entry.getKey().x(), entry.getValue(), entry.getKey().z()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private record Column(int x, int z) implements Comparable<Column> {
|
||||
@Override
|
||||
public int compareTo(Column other) {
|
||||
int result = Integer.compare(x, other.x);
|
||||
return result == 0 ? Integer.compare(z, other.z) : result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.placement.PlacedBlock;
|
||||
import com.volmit.stage.BlockStage;
|
||||
import com.volmit.stage.PlacementStage;
|
||||
import com.volmit.stage.PlacementStageContext;
|
||||
import com.volmit.stage.Stage;
|
||||
import com.volmit.stage.StageGraph;
|
||||
import com.volmit.stage.StageRef;
|
||||
import com.volmit.world.BlockPosition;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Port of PlacementModuleSnow: collect written tops, then cover them after the finishing work
|
||||
* of whichever stilt or vacuum module is installed (soft, so neither is required).
|
||||
*/
|
||||
public final class SnowModule implements IrisModule {
|
||||
public static final BlockStage<SnowHeights> COLLECT = Stage.placementBlock("iris:snow/collect", SnowHeights.class);
|
||||
public static final PlacementStage<Void> FINISH = Stage.placement("iris:snow/finish");
|
||||
public static final BlockState SNOW = new BlockState("minecraft:snow");
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:snow", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(COLLECT)
|
||||
.when(SnowSettings.class)
|
||||
.requires(BlocksModule.WRITE)
|
||||
.accumulate(SnowHeights::new, (context, snow, heights) -> heights.include(
|
||||
context.output(AnchorModule.HEIGHT).worldPosition(context.request(), context.block().position())));
|
||||
graph.define(FINISH)
|
||||
.when(SnowSettings.class)
|
||||
.requires(COLLECT, BlocksModule.COMPLETE)
|
||||
.softAfter(StageRef.of("iris:stilt/fill"), StageRef.of("iris:vacuum/finish"))
|
||||
.run(SnowModule::cover);
|
||||
}
|
||||
|
||||
private static void cover(PlacementStageContext context, SnowSettings snow) {
|
||||
var layer = SNOW.withProperty("layers", Integer.toString(snow.layers()));
|
||||
var access = context.access();
|
||||
for (var top : context.output(COLLECT).tops()) {
|
||||
var above = new BlockPosition(top.x(), top.y() + 1, top.z());
|
||||
if (ExampleBlocks.isAir(access.get(above.x(), above.y(), above.z()))) {
|
||||
access.set(new PlacedBlock(above, layer, Optional.empty()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.config.Feature;
|
||||
import com.volmit.config.MaxNumber;
|
||||
import com.volmit.config.MinNumber;
|
||||
|
||||
@Feature(id = "iris:snow", name = "Snow", field = "snow", targets = FeatureAttachment.Target.OBJECT_PLACEMENT)
|
||||
public final class SnowSettings implements IrisFeature {
|
||||
@MinNumber(1)
|
||||
@MaxNumber(8)
|
||||
private int layers = 1;
|
||||
|
||||
public SnowSettings() {}
|
||||
|
||||
public SnowSettings(int layers) {
|
||||
this.layers = layers;
|
||||
}
|
||||
|
||||
public int layers() {
|
||||
return layers;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Per-placement accumulator: the transformed blocks of the object's lowest layer. */
|
||||
public final class StiltBounds {
|
||||
private int lowestY = Integer.MAX_VALUE;
|
||||
private final List<ObjectBlock> lowestLayer = new ArrayList<>();
|
||||
|
||||
public void include(ObjectBlock block) {
|
||||
int y = block.position().y();
|
||||
if (y < lowestY) {
|
||||
lowestY = y;
|
||||
lowestLayer.clear();
|
||||
}
|
||||
if (y == lowestY) {
|
||||
lowestLayer.add(block);
|
||||
}
|
||||
}
|
||||
|
||||
public List<ObjectBlock> lowestLayer() {
|
||||
return List.copyOf(lowestLayer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import com.volmit.placement.PlacedBlock;
|
||||
import com.volmit.stage.BlockStage;
|
||||
import com.volmit.stage.PlacementStage;
|
||||
import com.volmit.stage.PlacementStageContext;
|
||||
import com.volmit.stage.Stage;
|
||||
import com.volmit.stage.StageGraph;
|
||||
import com.volmit.world.BlockPosition;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Port of PlacementModuleStilt: collect the lowest layer per block, then fill down after writes. */
|
||||
public final class StiltModule implements IrisModule {
|
||||
public static final BlockStage<StiltBounds> BOUNDS = Stage.placementBlock("iris:stilt/bounds", StiltBounds.class);
|
||||
public static final PlacementStage<Void> FILL = Stage.placement("iris:stilt/fill");
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("iris:stilt", "test");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(BOUNDS)
|
||||
.when(StiltSettings.class)
|
||||
.requires(PrepareModule.ROTATE, AnchorModule.HEIGHT)
|
||||
.accumulate(StiltBounds::new, (context, stilt, bounds) -> bounds.include(context.block()));
|
||||
graph.define(FILL)
|
||||
.when(StiltSettings.class)
|
||||
.requires(BOUNDS, BlocksModule.COMPLETE)
|
||||
.run(StiltModule::fill);
|
||||
}
|
||||
|
||||
private static void fill(PlacementStageContext context, StiltSettings stilt) {
|
||||
var anchor = context.output(AnchorModule.HEIGHT);
|
||||
for (var block : context.output(BOUNDS).lowestLayer()) {
|
||||
var bottom = anchor.worldPosition(context.request(), block.position());
|
||||
fillColumnBelow(context, bottom, block, stilt.maxDepth());
|
||||
}
|
||||
}
|
||||
|
||||
private static void fillColumnBelow(PlacementStageContext context, BlockPosition bottom,
|
||||
ObjectBlock source, int maxDepth) {
|
||||
var access = context.access();
|
||||
for (int depth = 1; depth <= maxDepth; depth++) {
|
||||
int y = bottom.y() - depth;
|
||||
if (y < access.bounds().minY() || !ExampleBlocks.isAir(access.get(bottom.x(), y, bottom.z()))) {
|
||||
return;
|
||||
}
|
||||
access.set(new PlacedBlock(new BlockPosition(bottom.x(), y, bottom.z()), source.state(), Optional.empty()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.FeatureAttachment;
|
||||
import com.volmit.IrisFeature;
|
||||
import com.volmit.config.Desc;
|
||||
import com.volmit.config.Feature;
|
||||
import com.volmit.config.MinNumber;
|
||||
|
||||
@Feature(id = "iris:stilt", name = "Stilt", field = "stilt", targets = FeatureAttachment.Target.OBJECT_PLACEMENT)
|
||||
public final class StiltSettings implements IrisFeature {
|
||||
@Desc("Maximum blocks filled below each lowest object block")
|
||||
@MinNumber(1)
|
||||
private int maxDepth = 16;
|
||||
|
||||
public StiltSettings() {}
|
||||
|
||||
public StiltSettings(int maxDepth) {
|
||||
this.maxDepth = maxDepth;
|
||||
}
|
||||
|
||||
public int maxDepth() {
|
||||
return maxDepth;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.volmit.example.placement;
|
||||
|
||||
import com.volmit.world.BlockPosition;
|
||||
|
||||
/** Placement-wide rotation chosen once per attempt, like Iris's spin kernel. */
|
||||
public record Transforms(int quarterTurns) {
|
||||
/** Rotates a local position clockwise around the pivot's vertical axis. */
|
||||
public BlockPosition rotate(BlockPosition local, BlockPosition pivot) {
|
||||
int dx = local.x() - pivot.x();
|
||||
int dz = local.z() - pivot.z();
|
||||
for (int turn = 0; turn < quarterTurns; turn++) {
|
||||
int rotatedX = -dz;
|
||||
dz = dx;
|
||||
dx = rotatedX;
|
||||
}
|
||||
return new BlockPosition(pivot.x() + dx, local.y(), pivot.z() + dz);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user