mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 08:14:45 +00:00
Drop blocks with no valid orientation when an object is rotated
A wall-mounted block that cannot face the new way put a null into the rotated object, and its tile was copied anyway. Rotation now omits the block and its tile, the module placement skips blocks that turn into air, and scaling reads geometry and volume under one read lock.
Upstream: 606aa2e70
This commit is contained in:
1 parent
8e0e6d7d45
commit
c3c137a33c
4 files changed
+181
-21
No files matched your search
@@ -30,7 +30,9 @@ import art.arcane.iris.util.project.interpolation.Interpolation3D;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Geometric transforms for {@link IrisObject}: rotation, scaling and the interpolated upscalers.
|
||||
@@ -50,14 +52,23 @@ final class IrisObjectTransforms {
|
||||
try {
|
||||
VectorMap<PlatformBlockState> d = new VectorMap<>();
|
||||
|
||||
Set<IrisBlockVector> omitted = new HashSet<>();
|
||||
|
||||
for (var entry : self.blocks) {
|
||||
d.put(turn.rotate(entry.getKey()), BlockStateRotation.rotate(turn, entry.getValue()));
|
||||
PlatformBlockState rotated = BlockStateRotation.rotate(turn, entry.getValue());
|
||||
if (rotated == null) {
|
||||
omitted.add(entry.getKey());
|
||||
continue;
|
||||
}
|
||||
d.put(turn.rotate(entry.getKey()), rotated);
|
||||
}
|
||||
|
||||
VectorMap<TileData> dx = new VectorMap<>();
|
||||
|
||||
for (var entry : self.states) {
|
||||
dx.put(turn.rotate(entry.getKey()), entry.getValue());
|
||||
if (!omitted.contains(entry.getKey())) {
|
||||
dx.put(turn.rotate(entry.getKey()), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
self.blocks = d;
|
||||
@@ -79,28 +90,27 @@ final class IrisObjectTransforms {
|
||||
scale = scale - 0.0001;
|
||||
}
|
||||
|
||||
IrisPosition l1 = self.getAABB().max();
|
||||
IrisPosition l2 = self.getAABB().min();
|
||||
VectorMap<PlatformBlockState> placeBlock = new VectorMap<>();
|
||||
|
||||
IrisVector center = new IrisVector(self.getCenter().getX(), self.getCenter().getY(), self.getCenter().getZ());
|
||||
if (self.getH() == 2) {
|
||||
center = center.setY(center.getBlockY() + 0.5);
|
||||
}
|
||||
if (self.getW() == 2) {
|
||||
center = center.setX(center.getBlockX() + 0.5);
|
||||
}
|
||||
if (self.getD() == 2) {
|
||||
center = center.setZ(center.getBlockZ() + 0.5);
|
||||
}
|
||||
|
||||
IrisObject oo = new IrisObject((int) Math.ceil((self.w * scale) + (scale * 2)), (int) Math.ceil((self.h * scale) + (scale * 2)), (int) Math.ceil((self.d * scale) + (scale * 2)));
|
||||
oo.setLoadKey(self.getLoadKey());
|
||||
oo.setLoader(self.getLoader());
|
||||
oo.setLoadFile(self.getLoadFile());
|
||||
IrisVector center;
|
||||
IrisObject oo;
|
||||
|
||||
self.readLock.lock();
|
||||
try {
|
||||
center = new IrisVector(self.getCenter().getX(), self.getCenter().getY(), self.getCenter().getZ());
|
||||
if (self.getH() == 2) {
|
||||
center = center.setY(center.getBlockY() + 0.5);
|
||||
}
|
||||
if (self.getW() == 2) {
|
||||
center = center.setX(center.getBlockX() + 0.5);
|
||||
}
|
||||
if (self.getD() == 2) {
|
||||
center = center.setZ(center.getBlockZ() + 0.5);
|
||||
}
|
||||
|
||||
oo = new IrisObject((int) Math.ceil((self.w * scale) + (scale * 2)), (int) Math.ceil((self.h * scale) + (scale * 2)), (int) Math.ceil((self.d * scale) + (scale * 2)));
|
||||
oo.setLoadKey(self.getLoadKey());
|
||||
oo.setLoader(self.getLoader());
|
||||
oo.setLoadFile(self.getLoadFile());
|
||||
for (var entry : self.blocks) {
|
||||
PlatformBlockState bd = entry.getValue();
|
||||
placeBlock.put(entry.getKey().clone().add(IrisObject.HALF).subtract(center)
|
||||
|
||||
+11
@@ -1,5 +1,6 @@
|
||||
package art.arcane.iris.engine.object.placement;
|
||||
|
||||
import com.volmit.block.BlockState;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -18,6 +19,16 @@ public class IrisObjectStructurePieceAirTest {
|
||||
assertTrue(BlocksModule.shouldPlaceObjectBlock(false, false, false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aBlockTurnedIntoAirHadNoValidOrientationAndIsDropped() {
|
||||
BlockState torch = new BlockState("minecraft:wall_torch");
|
||||
BlockState air = new BlockState("minecraft:air");
|
||||
|
||||
assertTrue(BlocksModule.isDroppedByRotation(torch, air));
|
||||
assertFalse(BlocksModule.isDroppedByRotation(torch, torch));
|
||||
assertFalse(BlocksModule.isDroppedByRotation(air, air));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void vineReplacementRemainsRejectedForEveryPlacementMode() {
|
||||
assertFalse(BlocksModule.shouldPlaceObjectBlock(false, false, true));
|
||||
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
package art.arcane.iris.engine.object.prefab;
|
||||
|
||||
import art.arcane.iris.spi.IrisPlatform;
|
||||
import art.arcane.iris.spi.IrisPlatforms;
|
||||
import art.arcane.iris.spi.PlatformBlockState;
|
||||
import art.arcane.iris.spi.PlatformRegistries;
|
||||
import art.arcane.volmlib.util.collection.KMap;
|
||||
import art.arcane.volmlib.util.math.Vector3i;
|
||||
import com.volmit.object.ScaleMethod;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** Rotation drops blocks with no valid orientation and their tiles; scaling reads one snapshot of the object. */
|
||||
public class IrisObjectTransformsStabilityTest {
|
||||
private static final long WAIT_SECONDS = 5;
|
||||
private static final long PARK_MILLIS = 1;
|
||||
private static final int QUARTER_TURN_DEGREES = 90;
|
||||
private static final double SCALE = 2;
|
||||
|
||||
@Before
|
||||
public void bindPlatform() {
|
||||
IrisPlatforms.unbind();
|
||||
PlatformRegistries registries = mock(PlatformRegistries.class);
|
||||
when(registries.block(anyString())).thenAnswer(call -> state(call.getArgument(0)));
|
||||
IrisPlatform platform = mock(IrisPlatform.class);
|
||||
when(platform.registries()).thenReturn(registries);
|
||||
IrisPlatforms.bind(platform);
|
||||
}
|
||||
|
||||
@After
|
||||
public void unbindPlatform() {
|
||||
IrisPlatforms.unbind();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rotationOmitsUnsupportedBlocksAndTheirTiles() {
|
||||
IrisObject source = new IrisObject(3, 1, 1);
|
||||
PlatformBlockState unsupported = state("minecraft:wall_torch");
|
||||
PlatformBlockState chest = state("minecraft:chest");
|
||||
source.setUnsigned(0, 0, 0, unsupported);
|
||||
source.setUnsignedTile(0, 0, 0, tile("minecraft:wall_torch", "omitted"));
|
||||
source.setUnsigned(1, 0, 0, chest);
|
||||
source.setUnsignedTile(1, 0, 0, tile("minecraft:chest", "kept"));
|
||||
source.setUnsigned(2, 0, 0, state("minecraft:stone"));
|
||||
BlockStateRotation.StateRotator previous = BlockStateRotation.bindPlatformRotator(
|
||||
(turn, block) -> block == unsupported ? null : block);
|
||||
IrisObject rotated;
|
||||
try {
|
||||
rotated = source.rotateCopy(ObjectTurn.of(0, QUARTER_TURN_DEGREES, 0));
|
||||
} finally {
|
||||
BlockStateRotation.restorePlatformRotator(previous);
|
||||
}
|
||||
|
||||
assertEquals(2, rotated.getBlocks().size());
|
||||
assertEquals(1, rotated.getStates().size());
|
||||
rotated.getStates().forEach(entry -> {
|
||||
assertEquals(chest, rotated.getBlocks().get(entry.getKey()));
|
||||
assertEquals("kept", entry.getValue().getProperties().get("name"));
|
||||
});
|
||||
assertEquals(3, source.getBlocks().size());
|
||||
assertEquals(2, source.getStates().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scalingWaitsForMatchingGeometryAndVolume() throws Exception {
|
||||
IrisObject source = new IrisObject(9, 5, 3);
|
||||
source.setUnsigned(0, 0, 0, state("minecraft:stone"));
|
||||
FutureTask<IrisObject> scaling = new FutureTask<>(() -> source.scaled(SCALE, ScaleMethod.NONE));
|
||||
Thread worker = new Thread(scaling, "object-scaling-snapshot");
|
||||
source.writeLock.lock();
|
||||
try {
|
||||
worker.start();
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(WAIT_SECONDS);
|
||||
while (worker.getState() != Thread.State.WAITING && System.nanoTime() < deadline) {
|
||||
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(PARK_MILLIS));
|
||||
}
|
||||
assertEquals(Thread.State.WAITING, worker.getState());
|
||||
source.setW(2);
|
||||
source.setH(2);
|
||||
source.setD(2);
|
||||
source.setCenter(new Vector3i(1, 1, 1));
|
||||
source.getBlocks().clear();
|
||||
source.setUnsigned(0, 0, 0, state("minecraft:chest"));
|
||||
} finally {
|
||||
source.writeLock.unlock();
|
||||
}
|
||||
try {
|
||||
IrisObject actual = scaling.get(WAIT_SECONDS, TimeUnit.SECONDS);
|
||||
IrisObject expected = source.scaled(SCALE, ScaleMethod.NONE);
|
||||
|
||||
assertEquals(expected.getW(), actual.getW());
|
||||
assertEquals(expected.getH(), actual.getH());
|
||||
assertEquals(expected.getD(), actual.getD());
|
||||
assertEquals(expected.getBlocks().size(), actual.getBlocks().size());
|
||||
expected.getBlocks().forEach(entry -> {
|
||||
assertNotNull(actual.getBlocks().get(entry.getKey()));
|
||||
assertEquals(entry.getValue(), actual.getBlocks().get(entry.getKey()));
|
||||
});
|
||||
} finally {
|
||||
worker.interrupt();
|
||||
worker.join(TimeUnit.SECONDS.toMillis(WAIT_SECONDS));
|
||||
}
|
||||
}
|
||||
|
||||
private static PlatformBlockState state(String key) {
|
||||
PlatformBlockState state = mock(PlatformBlockState.class);
|
||||
when(state.key()).thenReturn(key);
|
||||
when(state.materialKey()).thenReturn(key);
|
||||
when(state.isAir()).thenReturn(false);
|
||||
when(state.isSolid()).thenReturn(true);
|
||||
return state;
|
||||
}
|
||||
|
||||
private static TileData tile(String id, String name) {
|
||||
TileData tile = new TileData(id, new KMap<>());
|
||||
tile.getProperties().put("name", name);
|
||||
return tile;
|
||||
}
|
||||
}
|
||||
@@ -107,7 +107,11 @@ public final class BlocksModule implements IrisModule {
|
||||
data = edited.data();
|
||||
blockEntity = edited.blockEntity();
|
||||
}
|
||||
data = prepared.rotate(data);
|
||||
BlockState rotated = prepared.rotate(data);
|
||||
if (isDroppedByRotation(data, rotated)) {
|
||||
return;
|
||||
}
|
||||
data = rotated;
|
||||
int x = placement.x() + offset.x();
|
||||
int y = anchorY + offset.y();
|
||||
int z = placement.z() + offset.z();
|
||||
@@ -152,6 +156,11 @@ public final class BlocksModule implements IrisModule {
|
||||
return data;
|
||||
}
|
||||
|
||||
/** A block that turned into air had no valid orientation: it is left out with its block entity, even for structure pieces that write air. */
|
||||
static boolean isDroppedByRotation(BlockState source, BlockState rotated) {
|
||||
return !AIR_MATERIALS.contains(source.material().toString()) && AIR_MATERIALS.contains(rotated.material().toString());
|
||||
}
|
||||
|
||||
private static boolean shouldWrite(PlacementWorld world, BlockState data, int x, int y, int z, boolean structurePiece) {
|
||||
if (world.has(data, BlockTrait.CUSTOM)) {
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user