diff --git a/core/src/main/java/gregtech/content/worldgen/StoneLayerScan.java b/core/src/main/java/gregtech/content/worldgen/StoneLayerScan.java new file mode 100644 index 000000000..ac23be11a --- /dev/null +++ b/core/src/main/java/gregtech/content/worldgen/StoneLayerScan.java @@ -0,0 +1,102 @@ +package gregtech.content.worldgen; + +/** + * GT6 {@code WorldgenStoneLayers}' 7-entry vertical sliding window. + * + *

{@code scan[3]} is the layer at the current {@code y}; {@code scan[1]}/{@code scan[5]} are + * the layers two blocks below/above, {@code scan[0]}/{@code scan[6]} are the outer pair used for + * the normal/small ore distinction, and {@code scan[2]}/{@code scan[4]} are the near pair around + * layer boundaries.

+ */ +public final class StoneLayerScan { + /** Layer lookup at world coordinates; the feature supplies noise sampling and tests can stub it. */ + @FunctionalInterface + public interface Sampler { + T at(int x, int y, int z); + } + + private static final int WINDOW = 7; + + private final Sampler sampler; + private final int x; + private final int z; + private final T[] scan; + private int y; + + @SuppressWarnings("unchecked") + public StoneLayerScan(Sampler sampler, int x, int startY, int z) { + this.sampler = sampler; + this.x = x; + this.z = z; + this.y = startY; + this.scan = (T[]) new Object[WINDOW]; + for (int k = 0; k < WINDOW; k++) { + scan[k] = sampler.at(x, startY + k - 3, z); + } + } + + public int y() { + return y; + } + + public T current() { + return scan[3]; + } + + public T below() { + return scan[1]; + } + + public T above() { + return scan[5]; + } + + public T outerBelow() { + return scan[0]; + } + + public T outerAbove() { + return scan[6]; + } + + public T nearBelow() { + return scan[2]; + } + + public T nearAbove() { + return scan[4]; + } + + /** {@code tScan[5] == tScan[1]}: the column is inside a single layer rather than on a boundary. */ + public boolean isInterior() { + return scan[5] == scan[1]; + } + + /** {@code tScan[6] == tScan[0]}: deep core of one layer, eligible for normal-sized ores. */ + public boolean isCore() { + return scan[6] == scan[0]; + } + + /** {@code tScan[4] != tScan[2]}: close to a layer transition. */ + public boolean isNearBoundary() { + return scan[4] != scan[2]; + } + + /** Slides the window up by one block to {@code y + 1}. */ + public void advance() { + y++; + System.arraycopy(scan, 1, scan, 0, WINDOW - 1); + scan[WINDOW - 1] = sampler.at(x, y + 3, z); + } + + /** + * Inclusive upper Y bound for scanning a chunk, extending one block above the highest filled + * 16-block section so rocks resting directly on section boundaries (e.g. Y=63 or Y=143) are + * still visited. Returns {@code minBuildHeight - 1} when the chunk has no filled sections. + */ + public static int maxScanY(int minBuildHeight, int maxBuildHeight, int highestFilledSectionIndex) { + if (highestFilledSectionIndex < 0) return minBuildHeight - 1; + int sectionTopNext = minBuildHeight + (highestFilledSectionIndex + 1) * 16; + return Math.min(maxBuildHeight - 1, sectionTopNext); + } +} diff --git a/core/src/main/java/gregtech/content/worldgen/SurfaceRockRules.java b/core/src/main/java/gregtech/content/worldgen/SurfaceRockRules.java new file mode 100644 index 000000000..bfd407671 --- /dev/null +++ b/core/src/main/java/gregtech/content/worldgen/SurfaceRockRules.java @@ -0,0 +1,82 @@ +package gregtech.content.worldgen; + +import java.util.function.BooleanSupplier; +import java.util.function.IntUnaryOperator; +import java.util.function.UnaryOperator; + +/** + * GT6 {@code WorldgenStoneLayers}' per-column surface-rock tracking ({@code tLastRock}, + * {@code tLastOre}, {@code tCanPlaceRocks}) and the 1/128 rock placement roll. + */ +public final class SurfaceRockRules { + /** GT6 {@code aRandom.nextInt(128) == 0}. */ + public static final int CHANCE_DIVISOR = 128; + + /** GT6 initial {@code tLastRock = MT.STONES.Deepslate}. */ + public static final String INITIAL_ROCK = "Deepslate"; + + private SurfaceRockRules() {} + + /** Tracks the exposed stone layer and indicator ore while scanning a single column upward. */ + public static final class ColumnState { + private String lastRock = INITIAL_ROCK; + private String lastOre; + private boolean canPlaceRocks; + + public String lastRock() { + return lastRock; + } + + public String lastOre() { + return lastOre; + } + + public boolean canPlaceRocks() { + return canPlaceRocks; + } + + /** Bedrock allows a surface rock directly above if exposed. */ + public void bedrock() { + canPlaceRocks = true; + } + + /** Replacing stone, cobble, or mossy host updates the column's topmost rock material. */ + public void replaced(String surfaceMaterial) { + lastRock = surfaceMaterial; + canPlaceRocks = true; + } + + /** Placing an ore records its material only when the entry requests surface indicators. */ + public void ore(String material, boolean indicators) { + if (indicators) lastOre = material; + } + + /** Air, fluids, or non-solid blocks break column continuity so buried ores cannot leak up. */ + public void reset() { + lastOre = null; + canPlaceRocks = false; + } + + /** Opaque hosts that do not replace stone update placement eligibility without changing rock. */ + public void opaque(boolean suitable) { + canPlaceRocks = suitable; + } + } + + /** + * Picks the rock's material matching GT6's short-circuit order: + * {@code nextBoolean() && tLastOre != null ? targetCrushing(tLastOre) : tLastRock}. + */ + public static String rockMaterial( + ColumnState state, BooleanSupplier nextBoolean, UnaryOperator targetCrushing) { + if (nextBoolean.getAsBoolean() && state.lastOre() != null) { + return targetCrushing.apply(state.lastOre()); + } + return state.lastRock(); + } + + /** GT6 1/128 surface rock roll. */ + public static boolean roll(IntUnaryOperator nextInt) { + return nextInt.applyAsInt(CHANCE_DIVISOR) == 0; + } +} diff --git a/core/src/test/java/gregtech/content/worldgen/StoneLayerScanTest.java b/core/src/test/java/gregtech/content/worldgen/StoneLayerScanTest.java new file mode 100644 index 000000000..0e6dc71ab --- /dev/null +++ b/core/src/test/java/gregtech/content/worldgen/StoneLayerScanTest.java @@ -0,0 +1,81 @@ +package gregtech.content.worldgen; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +/** Locks GT6 {@code WorldgenStoneLayers}' 7-entry window and section-based scan bounds. */ +class StoneLayerScanTest { + private static final String A = "A"; + private static final String B = "B"; + private static final String C = "C"; + + @Test + void windowExposesThreeBlocksAroundCurrentY() { + StoneLayerScan scan = new StoneLayerScan<>((x, y, z) -> "Y" + y, 4, 10, -2); + assertEquals(10, scan.y()); + assertEquals("Y7", scan.outerBelow()); + assertEquals("Y8", scan.below()); + assertEquals("Y9", scan.nearBelow()); + assertEquals("Y10", scan.current()); + assertEquals("Y11", scan.nearAbove()); + assertEquals("Y12", scan.above()); + assertEquals("Y13", scan.outerAbove()); + } + + @Test + void interiorAndCoreFlagsFollowTheWindow() { + StoneLayerScan scan = new StoneLayerScan<>((x, y, z) -> A, 0, 0, 0); + assertTrue(scan.isInterior()); + assertTrue(scan.isCore()); + assertFalse(scan.isNearBoundary()); + } + + @Test + void boundaryFlagsDetectDifferentLayers() { + StoneLayerScan scan = new StoneLayerScan<>((x, y, z) -> y < 0 ? A : B, 0, 0, 0); + assertFalse(scan.isInterior()); + assertFalse(scan.isCore()); + assertTrue(scan.isNearBoundary()); + } + + @Test + void advanceSlidesUnboundedPastOldHeightCap() { + StoneLayerScan scan = new StoneLayerScan<>( + (x, y, z) -> { + if (y < 144) return A; + if (y == 144) return B; + return C; + }, + 0, + 142, + 0); + assertSame(A, scan.current()); + scan.advance(); + assertEquals(143, scan.y()); + assertSame(A, scan.current()); + scan.advance(); + assertEquals(144, scan.y()); + assertSame(B, scan.current()); + scan.advance(); + assertEquals(145, scan.y()); + assertSame(C, scan.current()); + } + + @Test + void maxScanYIncludesSectionBoundarySurfaceAndClampsToBuildHeight() { + // Empty chunk (-1) scans nothing. + assertEquals(-65, StoneLayerScan.maxScanY(-64, 320, -1)); + // Section 7 covers [48..63]; maxScanY reaches 64 so a rock sitting on y=63 is placed. + assertEquals(64, StoneLayerScan.maxScanY(-64, 320, 7)); + // Section 12 covers [128..143]; maxScanY reaches 144 instead of cutting off at 143. + assertEquals(144, StoneLayerScan.maxScanY(-64, 320, 12)); + // Mountain section 20 covers [256..271]; high-altitude terrain above Y=144 is included. + assertEquals(272, StoneLayerScan.maxScanY(-64, 320, 20)); + // Top-most section clamps at maxBuildHeight - 1. + assertEquals(319, StoneLayerScan.maxScanY(-64, 320, 23)); + } +} diff --git a/core/src/test/java/gregtech/content/worldgen/SurfaceRockRulesTest.java b/core/src/test/java/gregtech/content/worldgen/SurfaceRockRulesTest.java new file mode 100644 index 000000000..8c932b092 --- /dev/null +++ b/core/src/test/java/gregtech/content/worldgen/SurfaceRockRulesTest.java @@ -0,0 +1,81 @@ +package gregtech.content.worldgen; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.function.UnaryOperator; +import org.junit.jupiter.api.Test; + +/** Locks GT6 {@code WorldgenStoneLayers}' {@code tLastRock}/{@code tLastOre} state. */ +class SurfaceRockRulesTest { + private static final UnaryOperator TARGET = name -> "crushed-" + name; + + @Test + void initialRockIsDeepslate() { + SurfaceRockRules.ColumnState state = new SurfaceRockRules.ColumnState(); + assertEquals(SurfaceRockRules.INITIAL_ROCK, state.lastRock()); + assertFalse(state.canPlaceRocks()); + } + + @Test + void replacedLayerBecomesTheRockMaterial() { + SurfaceRockRules.ColumnState state = new SurfaceRockRules.ColumnState(); + state.replaced("GraniteRed"); + assertTrue(state.canPlaceRocks()); + assertEquals("GraniteRed", state.lastRock()); + } + + @Test + void indicatorOreIsUsedOnTheFiftyPercentRoll() { + SurfaceRockRules.ColumnState state = new SurfaceRockRules.ColumnState(); + state.replaced("GraniteRed"); + state.ore("Tetrahedrite", true); + assertEquals("Tetrahedrite", state.lastOre()); + assertEquals("crushed-Tetrahedrite", SurfaceRockRules.rockMaterial(state, () -> true, TARGET)); + assertEquals("GraniteRed", SurfaceRockRules.rockMaterial(state, () -> false, TARGET)); + } + + @Test + void nonIndicatorOreDoesNotReplaceTheRockMaterial() { + SurfaceRockRules.ColumnState state = new SurfaceRockRules.ColumnState(); + state.replaced("GraniteRed"); + state.ore("Diamond", false); + assertNull(state.lastOre()); + assertEquals("GraniteRed", SurfaceRockRules.rockMaterial(state, () -> true, TARGET)); + } + + @Test + void opaqueGroundPreservesExistingLastRock() { + SurfaceRockRules.ColumnState state = new SurfaceRockRules.ColumnState(); + state.replaced("Basalt"); + state.ore("Chalcopyrite", true); + state.opaque(true); + assertTrue(state.canPlaceRocks()); + assertEquals("Basalt", state.lastRock()); + assertEquals("Chalcopyrite", state.lastOre()); + + state.opaque(false); + assertFalse(state.canPlaceRocks()); + assertEquals("Basalt", state.lastRock()); + } + + @Test + void resetClearsTheOreAndTheRockFlag() { + SurfaceRockRules.ColumnState state = new SurfaceRockRules.ColumnState(); + state.replaced("GraniteRed"); + state.ore("Tetrahedrite", true); + state.reset(); + assertFalse(state.canPlaceRocks()); + assertNull(state.lastOre()); + assertEquals("GraniteRed", state.lastRock()); + } + + @Test + void rollUsesOneIn128() { + assertTrue(SurfaceRockRules.roll(bound -> 0)); + assertFalse(SurfaceRockRules.roll(bound -> 1)); + assertEquals(128, SurfaceRockRules.CHANCE_DIVISOR); + } +} diff --git a/docs/decisions/parity/worldgen.md b/docs/decisions/parity/worldgen.md index 2dc8bbeaf..012b003d1 100644 --- a/docs/decisions/parity/worldgen.md +++ b/docs/decisions/parity/worldgen.md @@ -8,3 +8,4 @@ | 下界基岩矿 | 保留 `glowstone`、`netherquartz`、`ancientdebris`、`efrine`、`firestone` 五种;`voidquartz`、`gloomstone` 停注,灵魂沙散矿的 Gloomstone 分支改为空。 | 停注材料后只恢复仍有独立来源的绿晶与火石,其余下界基岩矿不再生成。 | | DominicanAmber 接触矿 | 删除 `Coal`/`Lignite`/`Oilshale` 与 `Stone` 接触面的 `AmberDominican` 条目及 SHROOM 群系规则;普通 `Amber` 保留。 | 对应材料停注,见[材料移除](material-removal.md)。 | | 装饰石生成 | 默认禁用 MC 花岗岩、闪长岩、安山岩矿团,可用 `DisableVanillaDecorativeStone` 调整。 | 避免与 GT 地层生成重复。 | +| 地表散落特殊石子 | 移除 GT6 `WorldgenRocks` 中 1/12 概率的 `MeteoricIron` 粗矿/碎石分支,保留 1/2 空与 1/2 燧石。 | `MeteoricIron` 及其陨钢材料链已停注,见[材料移除](material-removal.md)。 | diff --git a/minecraft/src/main/java/gregtech/content/worldgen/GTRockPlacement.java b/minecraft/src/main/java/gregtech/content/worldgen/GTRockPlacement.java index d20b49244..36ddb63a9 100644 --- a/minecraft/src/main/java/gregtech/content/worldgen/GTRockPlacement.java +++ b/minecraft/src/main/java/gregtech/content/worldgen/GTRockPlacement.java @@ -5,6 +5,7 @@ import gregtech.content.registry.GTBlocks; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.tags.BlockTags; import net.minecraft.world.level.WorldGenLevel; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.levelgen.Heightmap; @@ -27,9 +28,10 @@ public static boolean placeRock(WorldGenLevel level, int x, int z, GTMaterial ma if (level.isOutsideBuildHeight(y) || y <= level.getMinBuildHeight()) return false; BlockPos pos = new BlockPos(x, y, z); BlockPos below = pos.below(); + BlockState atPos = level.getBlockState(pos); BlockState ground = level.getBlockState(below); - if (!level.getBlockState(pos).isAir()) return false; - if (!ground.isFaceSturdy(level, below, Direction.UP)) return false; + if (!atPos.getFluidState().isEmpty() || !GTRocksFeature.replaceable(atPos)) return false; + if (ground.is(BlockTags.LEAVES) || !ground.isFaceSturdy(level, below, Direction.UP)) return false; String itemId = null; if (rawOre) { if (material == null) return false; diff --git a/minecraft/src/main/java/gregtech/content/worldgen/GTRocksFeature.java b/minecraft/src/main/java/gregtech/content/worldgen/GTRocksFeature.java index 74f782544..a92c74ec6 100644 --- a/minecraft/src/main/java/gregtech/content/worldgen/GTRocksFeature.java +++ b/minecraft/src/main/java/gregtech/content/worldgen/GTRocksFeature.java @@ -108,9 +108,10 @@ public static boolean castRay(WorldGenLevel level, int x, int z, int maxHeight, } if (!rockGround(state)) return false; BlockPos above = cursor.above(); - // GT6's `WD.easyRep`: the block above must be replaceable — air, a snow layer or the - // plants the vanilla REPLACEABLE tag covers, not just air. - if (!replaceable(level.getBlockState(above))) return false; + // GT6's `WD.easyRep`: the block above must be replaceable — air, a snow layer, leaves, + // fire, or the plants the vanilla REPLACEABLE tag covers, and not filled with liquid. + BlockState aboveState = level.getBlockState(above); + if (!aboveState.getFluidState().isEmpty() || !replaceable(aboveState)) return false; return placeRock(level, above, litterFor(random)); } return false; @@ -123,11 +124,16 @@ public static boolean rockGround(BlockState state) { } /** - * GT6's {@code WD.easyRep}: what a rock may be placed into — air, a snow layer, or anything the - * vanilla replaceable tag covers (grass, ferns, flowers); GT6's rocks sit on top of low plants. + * GT6's {@code WD.easyRep}: what a rock may replace — air, snow layers, leaves, fire, or + * anything covered by the vanilla {@code REPLACEABLE} tag (grass, ferns, flowers). */ public static boolean replaceable(BlockState state) { - return state.isAir() || state.is(BlockTags.REPLACEABLE) || state.is(Blocks.SNOW); + return state.isAir() + || state.is(BlockTags.REPLACEABLE) + || state.is(Blocks.SNOW) + || state.is(BlockTags.LEAVES) + || state.is(Blocks.FIRE) + || state.is(Blocks.SOUL_FIRE); } /** Selects the retained surface-litter contents. */ diff --git a/minecraft/src/main/java/gregtech/content/worldgen/GTStoneLayerFeature.java b/minecraft/src/main/java/gregtech/content/worldgen/GTStoneLayerFeature.java index 260b6ecf9..c8ad58431 100644 --- a/minecraft/src/main/java/gregtech/content/worldgen/GTStoneLayerFeature.java +++ b/minecraft/src/main/java/gregtech/content/worldgen/GTStoneLayerFeature.java @@ -15,12 +15,13 @@ import javax.annotation.Nullable; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; +import net.minecraft.tags.BlockTags; import net.minecraft.util.RandomSource; import net.minecraft.world.level.WorldGenLevel; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.level.levelgen.Heightmap; +import net.minecraft.world.level.chunk.ChunkAccess; import net.minecraft.world.level.levelgen.feature.Feature; import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext; import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration; @@ -30,20 +31,19 @@ * cellular noise ({@link GTCellNoise}, ~110-block horizontal regions, ~13-block * vertical strata) over the full {@link GTStoneLayersGen#LAYERS} table. * - *

A 7-entry sliding window of layer samples (y-3..y+3) drives ore placement exactly + *

A 7-entry sliding window ({@link StoneLayerScan}, y-3..y+3) drives ore placement exactly * like GT6: deep inside one layer (scan[5]==scan[1]) the layer's own ores roll — * normal ore in the core (scan[6]==scan[0]), small ore near the fringe; where two - * different layers meet, the contact-ore table (GT6 doublelayers) applies. This is - * what confines each ore to its regional stone layer instead of blanketing the world - * at a fixed height.

+ * different layers meet, the contact-ore table (GT6 doublelayers) applies. Per-column + * surface-rock tracking uses {@link SurfaceRockRules}.

*/ public class GTStoneLayerFeature extends Feature { private static final int MIN_GEN_Y = GTWorldgenScale.WORLD_FLOOR + 1; - private static final int MAX_GEN_Y = GTWorldgenScale.BAND_TOP; private static final Set REPLACEABLE_STONE = Set.of( Blocks.STONE, + Blocks.INFESTED_STONE, Blocks.GRANITE, Blocks.DIORITE, Blocks.ANDESITE, @@ -112,109 +112,79 @@ public boolean place(FeaturePlaceContext context) { int chunkMinX = origin.getX() & ~15; int chunkMinZ = origin.getZ() & ~15; + int minY = Math.max(MIN_GEN_Y, level.getMinBuildHeight()); + int maxY = maxScanY(level, chunkMinX, chunkMinZ); boolean placedAny = false; boolean surfaceRocks = gregtech.GregTechConfig.Worldgen.SURFACE_ROCKS.enabled(); - int minY = Math.max(MIN_GEN_Y, level.getMinBuildHeight()); BlockPos.MutableBlockPos cursor = new BlockPos.MutableBlockPos(); - Layer[] scan = new Layer[7]; + // Mirrors GT6 tLastReplaced so continuous layers of other mods' stones keep generating. + Block lastReplaced = Blocks.STONE; for (int dx = 0; dx < 16; dx++) { int x = chunkMinX + dx; for (int dz = 0; dz < 16; dz++) { int z = chunkMinZ + dz; + SurfaceRockRules.ColumnState rockState = new SurfaceRockRules.ColumnState(); + StoneLayerScan scan = + new StoneLayerScan<>((sx, sy, sz) -> layers[noise.get(sx, sy, sz, listSize)], x, minY, z); - // scan[k] = layer at y + (k - 3). - for (int k = 0; k < 7; k++) { - scan[k] = layers[noise.get(x, minY + k - 3, z, listSize)]; - } - - boolean canPlaceRocks = false; - String lastRock = "Deepslate"; - GTMaterial lastOre = null; - // Continue observing existing terrain above the generation band, without replacing - // it. An indicator belongs to an exposed surface, not the layer sampled at the cap. - int topY = Math.min( - level.getMaxBuildHeight() - 1, - Math.max(MAX_GEN_Y, level.getHeight(Heightmap.Types.WORLD_SURFACE_WG, x, z))); - for (int y = minY; y <= topY; y++) { + for (int y = minY; y <= maxY; y++) { cursor.set(x, y, z); BlockState host = level.getBlockState(cursor); Block hostBlock = host.getBlock(); - Layer layer = scan[3]; - boolean generateLayer = y < MAX_GEN_Y && !(layer.noDeep() && y < NO_DEEP_Y); + Layer layer = scan.current(); + boolean generateLayer = !(layer.noDeep() && y < NO_DEEP_Y); if (GTRocksFeature.replaceable(host) || !host.getFluidState().isEmpty()) { - if (surfaceRocks - && canPlaceRocks - && host.getFluidState().isEmpty() - && random.nextInt(128) == 0) { - String material = random.nextBoolean() && lastOre != null - ? lastOre.getTargetCrushingMaterial().getName() - : lastRock; - BlockPos below = cursor.below(); - if (level.getBlockState(below).isFaceSturdy(level, below, Direction.UP)) - placedAny |= GTRockPlacement.place(level, cursor, material, null, false); + if (surfaceRocks && host.getFluidState().isEmpty()) { + placedAny |= maybePlaceSurfaceRock(level, cursor, rockState, random); } - // Air, plants and water break the solid column. An ore below a cave must - // not be advertised by rocks above an unrelated roof. - lastOre = null; - canPlaceRocks = false; + // Air, plants and water break the solid column so buried ores never advertise above. + rockState.reset(); } else if (host.is(Blocks.BEDROCK)) { - canPlaceRocks = true; - } else if (REPLACEABLE_STONE.contains(hostBlock)) { - canPlaceRocks = true; + rockState.bedrock(); + } else if (hostBlock instanceof GTStoneBlock stone) { + // GT6 BlockStones (stone/cobble/mossy): keep placement eligibility without + // overwriting tLastRock so structures or pre-placed GT stones preserve strata hints. + boolean natural = stone.variant() == StoneVariant.STONE + || stone.variant() == StoneVariant.COBBLE + || stone.variant() == StoneVariant.COBBLE_MOSSY; + if (natural) { + rockState.opaque(true); + } else { + rockState.reset(); + } + } else if (isReplaceableStone(host, hostBlock, lastReplaced)) { + lastReplaced = hostBlock; // The ore background must be the layer's stone (what the host is about // to become), not the pre-replacement host — otherwise ores show the // vanilla rock (e.g. deepslate) inside a GT stone layer. BlockState background = generateLayer && layer.stone() != null ? layer.stone() : host; - boolean orePlaced = false; - if (generateLayer && scan[5] == scan[1]) { - // Interior of one layer: the layer's own ores. - boolean core = scan[6] == scan[0]; - for (int oreIndex = 0; oreIndex < layer.ores().size(); oreIndex++) { - Ore ore = layer.ores().get(oreIndex); - if (y < ore.minY() || y > ore.maxY()) continue; - if (!StoneLayerOrePlacement.enabled(ore.explicitBlock())) continue; - if (!ore.rule().biomes().isEmpty() - && !StoneLayerOrePlacement.matches(ore.rule(), level.getBiome(cursor))) - continue; - if (random.nextInt(ore.chanceDiv()) != 0) continue; - if (placeOre(level, cursor, background, ore, !core)) { - if (ore.indicators()) lastOre = ore.material(); - orePlaced = true; - placedAny = true; - break; - } - } + Ore placedOre = null; + if (generateLayer && scan.isInterior()) { + boolean core = scan.isCore(); + placedOre = tryPlaceOre(level, cursor, background, layer.ores(), y, !core, false, random); } else if (generateLayer) { - // Layer boundary: contact ores (GT6 doublelayers). - var below = contacts.get(scan[5].material()); - List contactOres = below == null ? null : below.get(scan[1].material()); + var below = contacts.get(scan.above().material()); + List contactOres = below == null + ? null + : below.get(scan.below().material()); if (contactOres != null) { - for (int oreIndex = 0; oreIndex < contactOres.size(); oreIndex++) { - Ore ore = contactOres.get(oreIndex); - if (y < ore.minY() || y > ore.maxY()) continue; - if (!StoneLayerOrePlacement.enabled(ore.explicitBlock())) continue; - if (!ore.rule().biomes().isEmpty() - && !StoneLayerOrePlacement.matches(ore.rule(), level.getBiome(cursor))) - continue; - if (random.nextInt(ore.chanceDiv()) != 0) continue; - if (placeOre(level, cursor, background, ore, random.nextBoolean())) { - if (ore.indicators()) lastOre = ore.material(); - orePlaced = true; - placedAny = true; - break; - } - } + placedOre = tryPlaceOre(level, cursor, background, contactOres, y, false, true, random); } } - if (!orePlaced) { - lastRock = generateLayer + if (placedOre != null) { + rockState.opaque(true); + rockState.ore(placedOre.material().getName(), placedOre.indicators()); + placedAny = true; + } else { + String rockMat = generateLayer && layer.stone() != null && !(layer.stone().getBlock() instanceof GTStoneBlock) ? layer.material() : rockMaterial(background); + rockState.replaced(rockMat); if (generateLayer && layer.stone() != null && !host.is(layer.stone().getBlock())) { @@ -223,31 +193,23 @@ public boolean place(FeaturePlaceContext context) { } } } else if (REPLACEABLE_COBBLE.contains(hostBlock)) { - canPlaceRocks = true; - lastRock = generateLayer && layer.cobble() != null ? rockMaterial(layer.cobble()) : "Stone"; + rockState.replaced( + generateLayer && layer.cobble() != null ? rockMaterial(layer.cobble()) : "Stone"); if (generateLayer && layer.cobble() != null) { level.setBlock(cursor, layer.cobble(), 2); placedAny = true; } } else if (REPLACEABLE_MOSSY.contains(hostBlock)) { - canPlaceRocks = true; - lastRock = generateLayer && layer.mossy() != null ? rockMaterial(layer.mossy()) : "Stone"; + rockState.replaced( + generateLayer && layer.mossy() != null ? rockMaterial(layer.mossy()) : "Stone"); if (generateLayer && layer.mossy() != null) { level.setBlock(cursor, layer.mossy(), 2); placedAny = true; } - } else if (hostBlock instanceof GTStoneBlock stone) { - canPlaceRocks = stone.variant() == StoneVariant.STONE - || stone.variant() == StoneVariant.COBBLE - || stone.variant() == StoneVariant.COBBLE_MOSSY; - if (canPlaceRocks) - lastRock = stone.stoneType().material().getName(); } else if (host.isSolidRender(level, cursor)) { - canPlaceRocks = - GTRocksFeature.rockGround(host) || host.is(Blocks.CLAY) || host.is(Blocks.GRAVEL); + rockState.opaque(suitableRockGround(host)); } else { - lastOre = null; - canPlaceRocks = false; + rockState.reset(); } // Existing vanilla structure half-blocks follow the same local host material. @@ -259,17 +221,70 @@ public boolean place(FeaturePlaceContext context) { if (replacement != null) placedAny |= level.setBlock(cursor, replacement, 2); } - // Slide the window up by one block. - if (y < MAX_GEN_Y) { - System.arraycopy(scan, 1, scan, 0, 6); - scan[6] = layers[noise.get(x, y + 4, z, listSize)]; - } + scan.advance(); } } } return placedAny; } + @Nullable + private static Ore tryPlaceOre( + WorldGenLevel level, + BlockPos pos, + BlockState background, + List ores, + int y, + boolean small, + boolean randomSmall, + RandomSource random) { + for (int oreIndex = 0; oreIndex < ores.size(); oreIndex++) { + Ore ore = ores.get(oreIndex); + if (y < ore.minY() || y > ore.maxY()) continue; + if (!StoneLayerOrePlacement.enabled(ore.explicitBlock())) continue; + if (!ore.rule().biomes().isEmpty() && !StoneLayerOrePlacement.matches(ore.rule(), level.getBiome(pos))) + continue; + if (random.nextInt(ore.chanceDiv()) != 0) continue; + boolean useSmall = randomSmall ? random.nextBoolean() : small; + if (placeOre(level, pos, background, ore, useSmall)) { + return ore; + } + } + return null; + } + + private static boolean maybePlaceSurfaceRock( + WorldGenLevel level, BlockPos pos, SurfaceRockRules.ColumnState rockState, RandomSource random) { + if (!rockState.canPlaceRocks() || !SurfaceRockRules.roll(random::nextInt)) return false; + BlockPos below = pos.below(); + if (!level.getBlockState(below).isFaceSturdy(level, below, Direction.UP)) return false; + String material = + SurfaceRockRules.rockMaterial(rockState, random::nextBoolean, GTStoneLayerFeature::targetCrushing); + return GTRockPlacement.place(level, pos, material, null, false); + } + + private static boolean isReplaceableStone(BlockState host, Block hostBlock, Block lastReplaced) { + return hostBlock == lastReplaced + || REPLACEABLE_STONE.contains(hostBlock) + || host.is(BlockTags.BASE_STONE_OVERWORLD); + } + + private static boolean suitableRockGround(BlockState state) { + return GTRocksFeature.rockGround(state) || state.is(Blocks.CLAY) || state.is(Blocks.GRAVEL); + } + + private static int maxScanY(WorldGenLevel level, int chunkMinX, int chunkMinZ) { + ChunkAccess chunk = level.getChunk(chunkMinX >> 4, chunkMinZ >> 4); + return StoneLayerScan.maxScanY( + level.getMinBuildHeight(), level.getMaxBuildHeight(), chunk.getHighestFilledSectionIndex()); + } + + private static String targetCrushing(String name) { + GTMaterial material = GTMaterialRegistry.get(name); + if (material == null || !material.resolve().isValid()) return name; + return material.resolve().getTargetCrushingMaterial().getName(); + } + private static String rockMaterial(BlockState state) { if (state.getBlock() instanceof GTStoneBlock stone) return stone.stoneType().material().getName();