diff --git a/core/src/test/java/art/arcane/iris/engine/decorator/IrisSeaSurfaceDecoratorBoundsTest.java b/core/src/test/java/art/arcane/iris/engine/decorator/IrisSeaSurfaceDecoratorBoundsTest.java new file mode 100644 index 000000000..1d74ab95e --- /dev/null +++ b/core/src/test/java/art/arcane/iris/engine/decorator/IrisSeaSurfaceDecoratorBoundsTest.java @@ -0,0 +1,70 @@ +package art.arcane.iris.engine.decorator; + +import art.arcane.iris.builtin.decorant.DecorationPart; +import art.arcane.iris.engine.modifier.ApiChunk; +import com.volmit.block.BlockState; +import com.volmit.host.ArrayHunk; +import com.volmit.host.ModuleLogs; +import com.volmit.hunk.Hunk; +import com.volmit.world.BiomeKind; +import com.volmit.world.TerrainSampler; +import org.junit.Test; + +import static org.junit.Assert.assertSame; +import static org.mockito.ArgumentMatchers.anyDouble; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class IrisSeaSurfaceDecoratorBoundsTest { + private static final int WORLD_HEIGHT = 8; + private static final int TALL_STACK = 30; + private static final double TOP_THRESHOLD = 0.9; + private static final BlockState AIR = ApiChunk.AIR; + private static final BlockState BODY = new BlockState("minecraft:kelp_plant"); + private static final BlockState TOP = new BlockState("minecraft:kelp"); + private static final DecoratedBiome BIOME = new DecoratedBiome("ocean", null, BiomeKind.SEA); + + @Test + public void stackClippedByTheCeilingEndsOnTheTopBlock() { + Hunk blocks = decorate(4, TALL_STACK); + + assertSame(BODY, blocks.get(0, 5, 0)); + assertSame(BODY, blocks.get(0, 6, 0)); + assertSame(TOP, blocks.get(0, 7, 0)); + } + + @Test + public void stackOfOneBelowTheCeilingIsTheTopBlock() { + Hunk blocks = decorate(6, 1); + + assertSame(TOP, blocks.get(0, 7, 0)); + } + + @Test + public void baseAtTheCeilingPlacesNothing() { + Hunk blocks = decorate(WORLD_HEIGHT - 1, TALL_STACK); + + for (int y = 0; y < WORLD_HEIGHT; y++) { + assertSame(AIR, blocks.get(0, y, 0)); + } + } + + private Hunk decorate(int height, int stackHeight) { + TerrainSampler terrain = mock(TerrainSampler.class); + ResolvedDecorator decorator = mock(ResolvedDecorator.class); + BiomeDecorators decorators = mock(BiomeDecorators.class); + when(decorators.decorators(BIOME, DecorationPart.SEA_SURFACE)).thenReturn(new ResolvedDecorator[]{decorator}); + when(decorator.passesChanceGate(anyDouble(), anyDouble())).thenReturn(true); + when(decorator.isStacking()).thenReturn(true); + when(decorator.stackHeight(anyDouble(), anyDouble())).thenReturn(stackHeight); + when(decorator.topThreshold()).thenReturn(TOP_THRESHOLD); + when(decorator.blockAt(anyDouble(), anyDouble(), anyDouble())).thenReturn(BODY); + when(decorator.topBlockAt(anyDouble(), anyDouble(), anyDouble())).thenReturn(TOP); + Hunk blocks = new ArrayHunk<>(BlockState.class, 1, WORLD_HEIGHT, 1, AIR); + DecorationScene scene = new DecorationScene(blocks, ApiChunk.blockRegistry(), terrain, null, decorators, 17L, + ModuleLogs.discarding()); + + new IrisSeaSurfaceDecorator(scene).decorate(0, 0, 0, 1, -1, 0, 1, -1, BIOME, height, WORLD_HEIGHT); + return blocks; + } +} diff --git a/module/src/main/java/art/arcane/iris/engine/decorator/IrisSeaSurfaceDecorator.java b/module/src/main/java/art/arcane/iris/engine/decorator/IrisSeaSurfaceDecorator.java index e5b1fd4e6..45da4ba34 100644 --- a/module/src/main/java/art/arcane/iris/engine/decorator/IrisSeaSurfaceDecorator.java +++ b/module/src/main/java/art/arcane/iris/engine/decorator/IrisSeaSurfaceDecorator.java @@ -29,6 +29,12 @@ public class IrisSeaSurfaceDecorator extends IrisEngineDecorator { @Override public void decorate(int x, int z, int realX, int realX1, int realX_1, int realZ, int realZ1, int realZ_1, DecoratedBiome biome, int height, int max) { + var data = scene.blocks(); + int maxBaseY = Math.min(max, Math.min(scene.worldHeight(), data.height()) - 1); + if (height < 0 || height >= maxBaseY) { + return; + } + RNG rng = getRNG(realX, realZ); ResolvedDecorator decorator = DecoratorCore.pickDecorator(decorators.decorators(biome, getPart()), rng, realX, realZ, decorators.log()); @@ -36,11 +42,9 @@ public class IrisSeaSurfaceDecorator extends IrisEngineDecorator { return; } - var data = scene.blocks(); if (!decorator.isStacking()) { int targetY = height + 1; - if (height >= 0 && targetY < scene.worldHeight() - && DecoratorCore.canReplaceStackTarget(scene, data.get(x, targetY, z), false)) { + if (DecoratorCore.canReplaceStackTarget(scene, data.get(x, targetY, z), false)) { BlockState block = decorator.blockAt(realX, height, realZ); if (block != null) { data.set(x, targetY, z, block); @@ -54,28 +58,15 @@ public class IrisSeaSurfaceDecorator extends IrisEngineDecorator { stack = (int) Math.ceil((double) (max - height) * ((double) stack / 100)); } - if (stack == 1) { - int targetY = height + 1; - if (targetY >= data.height() || !DecoratorCore.canReplaceStackTarget(scene, data.get(x, targetY, z), false)) { - return; - } + stack = Math.min(stack, maxBaseY - height); - BlockState block = decorator.topBlockAt(realX, height, realZ); - if (block != null) { - data.set(x, targetY, z, block); - } - return; - } - - int engineHeight = scene.worldHeight(); for (int i = 0; i < stack; i++) { int h = height + i; int targetY = h + 1; - if (h >= max || targetY >= engineHeight - || !DecoratorCore.canReplaceStackTarget(scene, data.get(x, targetY, z), false)) { + if (!DecoratorCore.canReplaceStackTarget(scene, data.get(x, targetY, z), false)) { break; } - double threshold = ((double) i) / (stack - 1); + double threshold = stack == 1 ? 1.0 : ((double) i) / (stack - 1); BlockState block = threshold >= decorator.topThreshold() ? decorator.topBlockAt(realX, h, realZ) : decorator.blockAt(realX, h, realZ);