diff --git a/core/src/test/java/art/arcane/iris/engine/decorator/DecoratorCoreTest.java b/core/src/test/java/art/arcane/iris/engine/decorator/DecoratorCoreTest.java index 514f87b2c..1746e6e4f 100644 --- a/core/src/test/java/art/arcane/iris/engine/decorator/DecoratorCoreTest.java +++ b/core/src/test/java/art/arcane/iris/engine/decorator/DecoratorCoreTest.java @@ -13,6 +13,8 @@ import org.junit.Test; import java.util.HashMap; import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -367,6 +369,94 @@ public class DecoratorCoreTest { assertSame(STONE, DecoratorCore.fixFacesForHunk(scene(hunk(1, 1, 1)), STONE, 0, 0, 0, 0, 0)); } + @Test + public void aCurtainHangsFromTheWallItsTopStrandFaces() { + Hunk output = hunk(16, 8, 16); + output.set(8, 6, 8, STONE); + output.set(8, 5, 7, STONE); + output.set(9, 4, 8, STONE); + + placeCurtain(output); + + assertEquals(Set.of("north", "up"), setFaces(output.get(8, 5, 8))); + assertEquals(Set.of("north"), setFaces(output.get(8, 4, 8))); + assertEquals(Set.of("north"), setFaces(output.get(8, 3, 8))); + } + + @Test + public void aCurtainHangsUnderAnOpenCeiling() { + Hunk output = hunk(16, 8, 16); + output.set(8, 6, 8, STONE); + + placeCurtain(output); + + assertEquals(Set.of("north", "up"), setFaces(output.get(8, 5, 8))); + assertEquals(Set.of("north"), setFaces(output.get(8, 4, 8))); + assertEquals(Set.of("north"), setFaces(output.get(8, 3, 8))); + } + + @Test + public void anAuthoredSideFaceNothingHoldsIsDroppedAndTheVineReattaches() { + Hunk walled = hunk(16, 8, 16); + walled.set(9, 3, 8, STONE); + + assertEquals(Set.of("east"), setFaces(DecoratorCore.fixFacesForHunk(scene(walled), authoredVine("north"), 8, 8, 8, 3, 8))); + assertEquals(Set.of("up"), setFaces(DecoratorCore.fixFacesForHunk(scene(hunk(16, 8, 16)), authoredVine("north"), 8, 8, 8, 3, 8))); + } + + @Test + public void anAuthoredSideFaceAWallHoldsKeepsItsDirection() { + Hunk output = hunk(16, 8, 16); + output.set(8, 3, 7, STONE); + output.set(9, 3, 8, STONE); + + assertEquals(Set.of("north"), setFaces(DecoratorCore.fixFacesForHunk(scene(output), authoredVine("north"), 8, 8, 8, 3, 8))); + } + + @Test + public void aVineWithoutAnAuthoredSideFaceReattachesToEverySturdyNeighbour() { + Hunk output = hunk(16, 8, 16); + output.set(7, 3, 8, STONE); + output.set(8, 3, 9, STONE); + output.set(8, 4, 8, STONE); + + BlockState fixed = DecoratorCore.fixFacesForHunk(scene(output), authoredVine("up"), 8, 8, 8, 3, 8); + + assertEquals(Set.of("south", "west", "up"), setFaces(fixed)); + } + + @Test + public void glowLichenSideFaceIsNotHeldByTheLichenAboveIt() { + BlockState lichen = BlockState.parse("minecraft:glow_lichen[down=false,east=false,north=true,south=false,up=false,west=false]"); + Hunk output = hunk(16, 8, 16); + output.set(8, 2, 8, STONE); + output.set(8, 4, 8, lichen); + + assertEquals(Set.of("down"), setFaces(DecoratorCore.fixFacesForHunk(scene(output), lichen, 8, 8, 8, 3, 8))); + } + + private static void placeCurtain(Hunk output) { + ResolvedDecorator decorator = mock(ResolvedDecorator.class); + BlockState vine = authoredVine("north"); + when(decorator.stackHeight(anyDouble(), anyDouble())).thenReturn(3); + when(decorator.topThreshold()).thenReturn(1.0); + when(decorator.pickBlock(anyDouble(), anyDouble())).thenReturn(vine); + when(decorator.pickTopBlock(anyDouble(), anyDouble())).thenReturn(vine); + + DecoratorCore.placeStackDown(decorator, scene(output), 8, 8, 8, 8, 5, 0, 3, new DecoratorCore.PlaceOpts()); + } + + private static BlockState authoredVine(String face) { + return BlockState.parse("minecraft:vine[east=false,north=false,south=false,up=false,west=false]").withProperty(face, "true"); + } + + private static Set setFaces(BlockState state) { + return state.properties().entrySet().stream() + .filter(property -> "true".equals(property.getValue())) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()); + } + private static BlockState vine() { return BlockState.parse("minecraft:vine[down=false,east=false,north=false,south=false,up=false,west=false]"); } diff --git a/core/src/test/java/art/arcane/iris/engine/modifier/ApiChunk.java b/core/src/test/java/art/arcane/iris/engine/modifier/ApiChunk.java index 7c172c926..6150c235e 100644 --- a/core/src/test/java/art/arcane/iris/engine/modifier/ApiChunk.java +++ b/core/src/test/java/art/arcane/iris/engine/modifier/ApiChunk.java @@ -133,7 +133,7 @@ public final class ApiChunk implements GenerationContext { private static final Set FOLIAGE = Set.of("minecraft:short_grass", "minecraft:poppy", "minecraft:tall_grass", "minecraft:fern"); private static final Set SOILS = Set.of("minecraft:grass_block", "minecraft:dirt", "minecraft:sand"); - private static final Set VINES = Set.of("minecraft:vine"); + private static final Set VINES = Set.of("minecraft:vine", "minecraft:glow_lichen"); private static final Comparator VISIT_ORDER = Comparator.comparingInt(BlockPosition::x) .thenComparingInt(BlockPosition::y).thenComparingInt(BlockPosition::z); diff --git a/module/src/main/java/art/arcane/iris/engine/decorator/DecoratorCore.java b/module/src/main/java/art/arcane/iris/engine/decorator/DecoratorCore.java index 7e3dc450c..4d244b1ad 100644 --- a/module/src/main/java/art/arcane/iris/engine/decorator/DecoratorCore.java +++ b/module/src/main/java/art/arcane/iris/engine/decorator/DecoratorCore.java @@ -34,6 +34,7 @@ final class DecoratorCore { private static final long SEED_OFFSET = 29356788L; private static final long PART_FACTOR = 10439677L; private static final String HALF = "half"; + private static final ResourceId VINE = ResourceId.parse("minecraft:vine"); private static final ResourceId WEEPING_VINES = ResourceId.parse("minecraft:weeping_vines"); private static final ResourceId WEEPING_VINES_PLANT = ResourceId.parse("minecraft:weeping_vines_plant"); private static final ResourceId TWISTING_VINES = ResourceId.parse("minecraft:twisting_vines"); @@ -49,6 +50,9 @@ final class DecoratorCore { new Face(BlockFace.DOWN, "down", 0, -1, 0)); private static final Map OPPOSITES = Map.of(BlockFace.NORTH, BlockFace.SOUTH, BlockFace.SOUTH, BlockFace.NORTH, BlockFace.EAST, BlockFace.WEST, BlockFace.WEST, BlockFace.EAST, BlockFace.UP, BlockFace.DOWN, BlockFace.DOWN, BlockFace.UP); + private static final Face UP_FACE = FACES.get(4); + private static final Face DOWN_FACE = FACES.get(5); + private static final List SIDE_FACES = FACES.subList(0, 4); private static final int CHUNK_LAST = 15; static final ThreadLocal SCRATCH_OPTS = ThreadLocal.withInitial(PlaceOpts::new); @@ -360,15 +364,20 @@ final class DecoratorCore { } /** - * A vine placed at {@code x}, {@code y}, {@code z} (the chunk's local column {@code rX}, {@code rZ}) faces the neighbours that - * can hold it: the blocks the mantle planned there, else the chunk's own blocks. A vine with no such neighbour faces down, - * or up when it cannot face down. Any other block is returned as it is. + * A vine placed at {@code x}, {@code y}, {@code z} (the chunk's local column {@code rX}, {@code rZ}) keeps the side faces its + * pack gave it where a sturdy neighbour or the vine above holds them, and faces up under a sturdy ceiling. A vine none of its side + * faces can hold faces the neighbours that can hold it instead, down, or up when it cannot face down. Any other block is returned + * as it is. */ static BlockState fixFacesForHunk(DecorationScene scene, BlockState b, int rX, int rZ, int x, int y, int z) { if (b == null || !scene.has(BlockTrait.VINE, b)) { return b; } - Hunk hunk = scene.blocks(); + BlockState kept = keepSupportedSides(scene, b, rX, rZ, x, y, z); + if (kept != null) { + return kept; + } + BlockState fixed = b; for (Face face : FACES) { if (b.properties().containsKey(face.property())) { @@ -378,30 +387,9 @@ final class DecoratorCore { boolean found = false; for (Face face : FACES) { - int yy = y + face.dy(); - BlockFace opposite = OPPOSITES.get(face.face()); - - BlockState planned = scene.plannedAt(x + face.dx(), yy, z + face.dz()); - BlockState support = planned == null ? AIR : planned; - if (scene.registry().canAttach(b, support, opposite)) { - if (b.properties().containsKey(face.property())) { - found = true; - fixed = fixed.withProperty(face.property(), "true"); - } - continue; - } - - int xx = rX + face.dx(); - int zz = rZ + face.dz(); - if (xx < 0 || xx > CHUNK_LAST || zz < 0 || zz > CHUNK_LAST || yy < 0 || yy >= hunk.height()) { - continue; - } - - if (scene.registry().canAttach(b, hunk.get(xx, yy, zz), opposite)) { - if (b.properties().containsKey(face.property())) { - found = true; - fixed = fixed.withProperty(face.property(), "true"); - } + if (b.properties().containsKey(face.property()) && holds(scene, b, face, rX, rZ, x, y, z)) { + found = true; + fixed = fixed.withProperty(face.property(), "true"); } } if (!found) { @@ -413,6 +401,68 @@ final class DecoratorCore { return fixed; } + private static BlockState keepSupportedSides(DecorationScene scene, BlockState b, int rX, int rZ, int x, int y, int z) { + boolean vine = VINE.equals(b.material()); + boolean ceiling = holds(scene, b, UP_FACE, rX, rZ, x, y, z); + BlockState kept = b; + boolean heldSide = false; + for (Face face : SIDE_FACES) { + if (!isSet(b, face)) { + continue; + } + if (holds(scene, b, face, rX, rZ, x, y, z) || vine && (ceiling || vineAboveCarries(scene, face, rX, rZ, x, y, z))) { + heldSide = true; + } else { + kept = kept.withProperty(face.property(), "false"); + } + } + if (!heldSide) { + return null; + } + if (b.properties().containsKey(UP_FACE.property())) { + kept = kept.withProperty(UP_FACE.property(), String.valueOf(ceiling)); + } + if (isSet(b, DOWN_FACE) && !holds(scene, b, DOWN_FACE, rX, rZ, x, y, z)) { + kept = kept.withProperty(DOWN_FACE.property(), "false"); + } + return kept; + } + + private static boolean isSet(BlockState block, Face face) { + return "true".equals(block.properties().get(face.property())); + } + + /** Whether the neighbour on {@code face}, the block the mantle planned there else the chunk's own, can hold {@code b}. */ + private static boolean holds(DecorationScene scene, BlockState b, Face face, int rX, int rZ, int x, int y, int z) { + int yy = y + face.dy(); + BlockFace opposite = OPPOSITES.get(face.face()); + + BlockState planned = scene.plannedAt(x + face.dx(), yy, z + face.dz()); + if (scene.registry().canAttach(b, planned == null ? AIR : planned, opposite)) { + return true; + } + + Hunk hunk = scene.blocks(); + int xx = rX + face.dx(); + int zz = rZ + face.dz(); + if (xx < 0 || xx > CHUNK_LAST || zz < 0 || zz > CHUNK_LAST || yy < 0 || yy >= hunk.height()) { + return false; + } + return scene.registry().canAttach(b, hunk.get(xx, yy, zz), opposite); + } + + private static boolean vineAboveCarries(DecorationScene scene, Face face, int rX, int rZ, int x, int y, int z) { + if (carries(scene.plannedAt(x, y + 1, z), face)) { + return true; + } + Hunk hunk = scene.blocks(); + return y + 1 < hunk.height() && carries(hunk.get(rX, y + 1, rZ), face); + } + + private static boolean carries(BlockState block, Face face) { + return block != null && VINE.equals(block.material()) && isSet(block, face); + } + static boolean canGoOn(DecorationScene scene, BlockState decorator, BlockState surface) { return decorator != null && surface != null && scene.canGoOn(decorator, surface); }