mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 00:04:42 +00:00
Transform objects for placement stages with Iris's object rotation
This commit is contained in:
1 parent
d2b4904263
commit
a07788fffe
4 files changed
+275
-3
No files matched your search
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* 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.object.prefab.IrisObjectRotation;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.util.common.math.IrisBlockVector;
|
||||
import com.volmit.mantle.BlockBox;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import com.volmit.object.ObjectMarker;
|
||||
import com.volmit.object.ObjectTransform;
|
||||
import com.volmit.object.ObjectTransformer;
|
||||
import com.volmit.world.BlockPosition;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Transforms geometry with the rotation Iris places objects with ({@link IrisObjectRotation}, its axis order
|
||||
* included) about the anchor, turning block states with the object so stairs and logs face the way it does.
|
||||
* A Y mirror flips positions the way Iris hangs an object from a ceiling, without changing block states.
|
||||
* Iris has no way to mirror along X or Z, or to scale geometry, so those requests fail with
|
||||
* {@link UnsupportedOperationException} rather than changing the request. Blocks that a turn by an angle that is
|
||||
* not a quarter turn sends to one position keep the last one, as Iris overwrites them when it places.
|
||||
*/
|
||||
final class IrisObjectTransformer implements ObjectTransformer {
|
||||
private static final double NO_SCALE = 1D;
|
||||
private static final int NO_SPIN = 0;
|
||||
|
||||
@Override
|
||||
public GeneratedObject transform(GeneratedObject object, ObjectTransform transform) {
|
||||
requireSupported(transform);
|
||||
if (transform.equals(ObjectTransform.identity())) {
|
||||
return object;
|
||||
}
|
||||
Turn turn = new Turn(object.anchor(), transform);
|
||||
Map<BlockPosition, ObjectBlock> blocks = new LinkedHashMap<>();
|
||||
for (ObjectBlock block : object.blocks()) {
|
||||
BlockPosition moved = turn.position(block.position());
|
||||
blocks.put(moved, new ObjectBlock(moved, turn.state(block), block.blockEntity()));
|
||||
}
|
||||
List<ObjectMarker> markers = new ArrayList<>();
|
||||
for (ObjectMarker marker : object.markers()) {
|
||||
markers.add(new ObjectMarker(turn.position(marker.position()), marker.type(), marker.attributes()));
|
||||
}
|
||||
return new GeneratedObject(boundsOf(blocks.keySet(), markers, object.bounds()), object.anchor(), List.copyOf(blocks.values()), markers);
|
||||
}
|
||||
|
||||
private static void requireSupported(ObjectTransform transform) {
|
||||
if (transform.mirrorX() || transform.mirrorZ()) {
|
||||
throw new UnsupportedOperationException("Iris can mirror an object along Y only, not along X or Z: " + transform);
|
||||
}
|
||||
if (transform.scaleX() != NO_SCALE || transform.scaleY() != NO_SCALE || transform.scaleZ() != NO_SCALE) {
|
||||
throw new UnsupportedOperationException("Iris cannot scale an object's geometry: " + transform);
|
||||
}
|
||||
}
|
||||
|
||||
private static BlockBox boundsOf(Iterable<BlockPosition> blocks, List<ObjectMarker> markers, BlockBox whenEmpty) {
|
||||
List<BlockPosition> positions = new ArrayList<>();
|
||||
blocks.forEach(positions::add);
|
||||
markers.forEach(marker -> positions.add(marker.position()));
|
||||
if (positions.isEmpty()) {
|
||||
return whenEmpty;
|
||||
}
|
||||
int minX = Integer.MAX_VALUE, minY = Integer.MAX_VALUE, minZ = Integer.MAX_VALUE;
|
||||
int maxX = Integer.MIN_VALUE, maxY = Integer.MIN_VALUE, maxZ = Integer.MIN_VALUE;
|
||||
for (BlockPosition position : positions) {
|
||||
minX = Math.min(minX, position.x());
|
||||
minY = Math.min(minY, position.y());
|
||||
minZ = Math.min(minZ, position.z());
|
||||
maxX = Math.max(maxX, position.x());
|
||||
maxY = Math.max(maxY, position.y());
|
||||
maxZ = Math.max(maxZ, position.z());
|
||||
}
|
||||
return new BlockBox(minX, minY, minZ, maxX + 1, maxY + 1, maxZ + 1);
|
||||
}
|
||||
|
||||
/** One requested turn about an anchor. */
|
||||
private static final class Turn {
|
||||
private final BlockPosition anchor;
|
||||
private final boolean mirrorY;
|
||||
private final IrisObjectRotation rotation;
|
||||
private final PlatformBlockStates states = new PlatformBlockStates(key -> IrisPlatforms.get().registries().block(key));
|
||||
|
||||
Turn(BlockPosition anchor, ObjectTransform transform) {
|
||||
this.anchor = anchor;
|
||||
this.mirrorY = transform.mirrorY();
|
||||
this.rotation = IrisObjectRotation.of(transform.rotationX(), transform.rotationY(), transform.rotationZ());
|
||||
}
|
||||
|
||||
BlockPosition position(BlockPosition local) {
|
||||
IrisBlockVector offset = new IrisBlockVector(local.x() - anchor.x(), local.y() - anchor.y(), local.z() - anchor.z());
|
||||
if (mirrorY) {
|
||||
offset.setY(-offset.getY());
|
||||
}
|
||||
IrisBlockVector turned = rotation.rotate(offset, NO_SPIN, NO_SPIN, NO_SPIN);
|
||||
return new BlockPosition(anchor.x() + (int) Math.round(turned.getX()), anchor.y() + (int) Math.round(turned.getY()),
|
||||
anchor.z() + (int) Math.round(turned.getZ()));
|
||||
}
|
||||
|
||||
com.volmit.block.BlockState state(ObjectBlock block) {
|
||||
return states.toApi(rotation.rotate(states.toPlatform(block.state()), NO_SPIN, NO_SPIN, NO_SPIN));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,8 +30,7 @@ import com.volmit.world.TerrainSampler;
|
||||
|
||||
/** The read-only IrisApi inputs of one Iris object placement. */
|
||||
final class IrisPlacementInputs implements PlacementContext {
|
||||
private static final String NOT_BRIDGED = " is not bridged to Iris yet; modules cannot use it until a built-in port needs it";
|
||||
|
||||
private final ObjectTransformer objects = new IrisObjectTransformer();
|
||||
private final long worldSeed;
|
||||
private final TerrainSampler terrain;
|
||||
private final FeatureSettings settings;
|
||||
@@ -82,7 +81,7 @@ final class IrisPlacementInputs implements PlacementContext {
|
||||
|
||||
@Override
|
||||
public ObjectTransformer objects() {
|
||||
throw new UnsupportedOperationException("Object transforms for placement stages" + NOT_BRIDGED);
|
||||
return objects;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.engine.UsableBlockPlatform;
|
||||
import art.arcane.iris.engine.object.prefab.IrisObjectRotation;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.mantle.BlockBox;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import com.volmit.object.ObjectMarker;
|
||||
import com.volmit.object.ObjectTransform;
|
||||
import com.volmit.resource.ResourceId;
|
||||
import com.volmit.world.BlockPosition;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
/** Objects turn about their anchor the way Iris places them; what Iris cannot do is refused. */
|
||||
public class IrisObjectTransformerTest {
|
||||
private static final BlockState LOG = BlockState.parse("minecraft:oak_log");
|
||||
private static final BlockState LEAVES = BlockState.parse("minecraft:oak_leaves");
|
||||
private static final ResourceId SPAWN = ResourceId.parse("test:marks/spawn");
|
||||
|
||||
private final IrisObjectTransformer transformer = new IrisObjectTransformer();
|
||||
private IrisObjectRotation.StateRotator previousRotator;
|
||||
|
||||
/** A trunk and a leaf east of its top, with a marker beside the trunk. */
|
||||
private final GeneratedObject tree = new GeneratedObject(new BlockBox(0, 0, 0, 2, 2, 1), new BlockPosition(0, 0, 0),
|
||||
List.of(new ObjectBlock(new BlockPosition(0, 0, 0), LOG), new ObjectBlock(new BlockPosition(1, 1, 0), LEAVES)),
|
||||
List.of(new ObjectMarker(new BlockPosition(1, 0, 0), SPAWN, Map.of("kind", "bird"))));
|
||||
|
||||
@Before
|
||||
public void bindPlatform() {
|
||||
UsableBlockPlatform.bind();
|
||||
previousRotator = IrisObjectRotation.bindPlatformRotator((rotation, state, spinX, spinY, spinZ) -> state);
|
||||
}
|
||||
|
||||
@After
|
||||
public void unbindPlatform() {
|
||||
IrisObjectRotation.restorePlatformRotator(previousRotator);
|
||||
UsableBlockPlatform.unbind();
|
||||
}
|
||||
|
||||
private static ObjectTransform rotatedY(double degrees) {
|
||||
return new ObjectTransform(0, degrees, 0, 1, 1, 1, false, false, false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void theIdentityIsTheSameObject() {
|
||||
assertSame(tree, transformer.transform(tree, ObjectTransform.identity()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aQuarterTurnAboutYMovesBlocksAndMarkersAboutTheAnchor() {
|
||||
GeneratedObject turned = transformer.transform(tree, rotatedY(90));
|
||||
|
||||
assertEquals(List.of(new BlockPosition(0, 0, 0), new BlockPosition(0, 1, -1)),
|
||||
turned.blocks().stream().map(ObjectBlock::position).sorted().toList());
|
||||
assertEquals(new BlockPosition(0, 0, -1), turned.markers().getFirst().position());
|
||||
assertEquals(SPAWN, turned.markers().getFirst().type());
|
||||
assertEquals(Map.of("kind", "bird"), turned.markers().getFirst().attributes());
|
||||
assertEquals(tree.anchor(), turned.anchor());
|
||||
assertEquals(new BlockBox(0, 0, -1, 1, 2, 1), turned.bounds());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void theSourceObjectIsLeftAsItWas() {
|
||||
transformer.transform(tree, rotatedY(90));
|
||||
|
||||
assertEquals(new BlockPosition(1, 1, 0), tree.blocks().getLast().position());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aYMirrorFlipsHeightsAboutTheAnchorWithoutChangingBlocks() {
|
||||
GeneratedObject hung = transformer.transform(tree, new ObjectTransform(0, 0, 0, 1, 1, 1, false, true, false));
|
||||
|
||||
assertEquals(List.of(new BlockPosition(0, 0, 0), new BlockPosition(1, -1, 0)),
|
||||
hung.blocks().stream().map(ObjectBlock::position).sorted().toList());
|
||||
assertEquals(List.of(LEAVES, LOG), hung.blocks().stream().map(ObjectBlock::state).sorted(java.util.Comparator.comparing(BlockState::key)).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void blocksTurnedOntoOnePositionKeepTheLastOne() {
|
||||
GeneratedObject squeezed = transformer.transform(new GeneratedObject(new BlockBox(0, 0, 0, 2, 1, 1), new BlockPosition(0, 0, 0),
|
||||
List.of(new ObjectBlock(new BlockPosition(0, 0, 0), LOG), new ObjectBlock(new BlockPosition(1, 0, 0), LEAVES)), List.of()),
|
||||
new ObjectTransform(0, 0, 0, 1, 1, 1, false, true, false));
|
||||
|
||||
assertEquals(2, squeezed.blocks().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mirroringAlongXOrZAndScalingAreRefused() {
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> transformer.transform(tree, new ObjectTransform(0, 0, 0, 1, 1, 1, true, false, false)));
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> transformer.transform(tree, new ObjectTransform(0, 0, 0, 1, 1, 1, false, false, true)));
|
||||
assertThrows(UnsupportedOperationException.class,
|
||||
() -> transformer.transform(tree, new ObjectTransform(0, 0, 0, 2, 1, 1, false, false, false)));
|
||||
}
|
||||
}
|
||||
+42
@@ -35,6 +35,10 @@ import art.arcane.volmlib.util.math.RNG;
|
||||
import com.volmit.IrisModule;
|
||||
import com.volmit.ModuleInfo;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.mantle.BlockBox;
|
||||
import com.volmit.object.GeneratedObject;
|
||||
import com.volmit.object.ObjectBlock;
|
||||
import com.volmit.object.ObjectTransform;
|
||||
import com.volmit.host.LoadedPack;
|
||||
import com.volmit.placement.PlacedBlock;
|
||||
import com.volmit.stage.PlacementStage;
|
||||
@@ -443,6 +447,44 @@ public class IrisObjectPlacementRegressionTest {
|
||||
assertEquals(List.of(PackResourceFixture.OAK), found);
|
||||
}
|
||||
|
||||
/** Lays the pack's oak on its side, a quarter turn about X, through the placement's object transformer. */
|
||||
private static final class TurnModule implements IrisModule {
|
||||
private static final PlacementStage<Void> TURN = Stage.placement("test:turn/oak");
|
||||
private final java.io.File packFolder;
|
||||
private final List<GeneratedObject> turned;
|
||||
|
||||
private TurnModule(java.io.File packFolder, List<GeneratedObject> turned) {
|
||||
this.packFolder = packFolder;
|
||||
this.turned = turned;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModuleInfo info() {
|
||||
return ModuleInfo.of("test:turn", "Turn", "1");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void install(StageGraph graph) {
|
||||
graph.define(TURN).requires(BlocksStages.COMPLETE).run(context -> turned.add(context.objects().transform(
|
||||
context.resources().require(PackResourceFixture.oakKey(packFolder)),
|
||||
new ObjectTransform(90, 0, 0, 1, 1, 1, false, false, false))));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPlacementStageTurnsAnObjectThroughTheObjectTransformer() throws IOException {
|
||||
PackResourceFixture.serve(data);
|
||||
List<GeneratedObject> turned = new ArrayList<>();
|
||||
usePack(List.of(new TurnModule(folder.getRoot(), turned)));
|
||||
|
||||
ObjectPlacementResult result = place(placement(), ANCHOR_Y, new RecordingPlacer(engine), null);
|
||||
|
||||
assertTrue(result.isPlaced());
|
||||
assertEquals(List.of(new BlockPosition(0, 0, 0), new BlockPosition(0, 0, 1)),
|
||||
turned.getFirst().blocks().stream().map(ObjectBlock::position).toList());
|
||||
assertEquals("the oak's leaves moved with the turn", new BlockBox(0, 0, 0, 1, 1, 2), turned.getFirst().bounds());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromBottomKeepsItsAnchorWithoutSamplingTerrain() {
|
||||
RecordingPlacer placer = new RecordingPlacer(null);
|
||||
|
||||
Reference in new issue
Block a user