Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions core/src/main/java/gregtech/content/worldgen/StoneLayerScan.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package gregtech.content.worldgen;

/**
* GT6 {@code WorldgenStoneLayers}' 7-entry vertical sliding window.
*
* <p>{@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.</p>
*/
public final class StoneLayerScan<T> {
/** Layer lookup at world coordinates; the feature supplies noise sampling and tests can stub it. */
@FunctionalInterface
public interface Sampler<T> {
T at(int x, int y, int z);
}

private static final int WINDOW = 7;

private final Sampler<T> sampler;
private final int x;
private final int z;
private final T[] scan;
private int y;

@SuppressWarnings("unchecked")
public StoneLayerScan(Sampler<T> 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);
}
}
82 changes: 82 additions & 0 deletions core/src/main/java/gregtech/content/worldgen/SurfaceRockRules.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> scan = new StoneLayerScan<>((x, y, z) -> A, 0, 0, 0);
assertTrue(scan.isInterior());
assertTrue(scan.isCore());
assertFalse(scan.isNearBoundary());
}

@Test
void boundaryFlagsDetectDifferentLayers() {
StoneLayerScan<String> 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<String> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}
1 change: 1 addition & 0 deletions docs/decisions/parity/worldgen.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)。 |
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Loading
Loading