RegionIO/internal/worldgen/carver_random_test.go
Master290 c6185d88c8 Configured carvers: caves and canyons
The router's noise caves are one kind of cave. The other kind -- the long
winding tunnels with rooms and side branches, and the ravines that cut down
through the terrain -- is walked, step by step, by a random source, and none of
it existed.

The shape of the work is unusual enough to state plainly. To carve one chunk,
vanilla replays every carver seeded in the 17x17 chunks around it and keeps only
what lands inside, so the same tunnel is walked up to 289 times across a world.
That redundancy is the point: it is what lets a chunk be carved without
generating its neighbours, which is the only way carving fits a generator that
produces one chunk at a time. A carve-once-write-into-neighbours design would be
cheaper and would not reproduce vanilla's mask and ordering.

Two primitives had to be right before any of it could be, and both are pinned
against values captured from the jar:

  * setLargeFeatureSeed, which decides which chunks start a cave. It combines
    its two products with XOR; setDecorationSeed, which it otherwise resembles,
    uses addition and forces the low bit. Getting them the wrong way round moves
    every tunnel in the world and nothing complains.
  * Mth.sin and Mth.cos, which are a 65536-entry lookup table and not libm.
    Mth.sin(-1.0) is -0.8414514 against Math.sin's -0.8414709848078965, and a
    tunnel that walks by adding cos(yaw) a hundred times ends up somewhere else
    entirely if that difference is smoothed away.

Carving lands between the surface pass and decoration, where vanilla puts it,
and both neighbours matter: the surface rules must already have placed grass for
a cave mouth to be retextured, and decoration must come after so nothing is
planted over a hole. The heights decoration plants against are recomputed
afterwards, which is why vanilla re-primes its heightmaps at the start of the
feature step.

The configs are extracted from the jar rather than transcribed, along with the
flattened #minecraft:overworld_carver_replaceables tag, so the probabilities and
Y ranges are data. Open volume below y=60 rises 28% over sixteen sampled chunks,
tunnels cut at or below y=-56 fill with lava rather than air (869 blocks, no
air), and the cost is inside the noise floor of the density pass.
2026-07-27 03:58:56 +03:00

78 lines
2.9 KiB
Go

package worldgen
import (
"math"
"testing"
)
// TestSetLargeFeatureSeedParity checks the carver seeding against values
// captured by running WorldgenRandom against the 26.1.2 jar.
//
// This is the single most dangerous primitive in the carvers: it decides which
// chunks start a cave and where. It also looks almost exactly like
// setDecorationSeed, which combines its two products with addition rather than
// XOR — getting them the wrong way round moves every tunnel in the world and
// nothing else complains.
func TestSetLargeFeatureSeedParity(t *testing.T) {
cases := []struct {
seed int64
chunkX, chunkZ int
wantFloat float32
wantInt16 int32
wantLong int64
}{
{12345, 0, 0, 0.361803055, 8, -1236052134575208584},
{12345, 1, -3, 0.430853248, 15, 2333035266122422630},
{12345, -17, 42, 0.756100893, 5, 3625585156103593602},
{12346, 0, 0, 0.362071812, 11, 7828065674307726589},
{12346, 1, -3, 0.904893756, 15, 6725298717481824139},
{12346, -17, 42, 0.276341736, 9, 1248907841123878499},
{12347, 0, 0, 0.361982226, 15, -7491136309694630448},
{12347, 1, -3, 0.511853278, 14, 7423281404700161236},
{12347, -17, 42, 0.031917453, 11, -7937379058403548373},
}
r := NewLegacy(0)
for _, c := range cases {
r.SetLargeFeatureSeed(c.seed, c.chunkX, c.chunkZ)
if got := r.NextFloat(); math.Abs(float64(got-c.wantFloat)) > 1e-7 {
t.Errorf("seed %d chunk (%d,%d): nextFloat = %v, want %v", c.seed, c.chunkX, c.chunkZ, got, c.wantFloat)
}
if got := r.NextIntN(16); got != c.wantInt16 {
t.Errorf("seed %d chunk (%d,%d): nextInt(16) = %d, want %d", c.seed, c.chunkX, c.chunkZ, got, c.wantInt16)
}
if got := r.NextLong(); got != c.wantLong {
t.Errorf("seed %d chunk (%d,%d): nextLong = %d, want %d", c.seed, c.chunkX, c.chunkZ, got, c.wantLong)
}
}
}
// TestMthTrigParity pins the sine table against the jar. Mth.sin is a
// 65536-entry lookup, not libm: it is visibly less accurate, and a tunnel that
// walks by adding cos(yaw) a hundred times drifts somewhere else entirely if
// the difference is smoothed away.
func TestMthTrigParity(t *testing.T) {
cases := []struct {
in float64
sin, cos float32
}{
{-1.0, -0.841451406, 0.540252149},
{0.0, 0.0, 1.0},
{0.5, 0.479409635, 0.877591252},
{3.1415927, 0.0, -1.0},
{-6.2831855, 0.0, 1.0},
{100.25, -0.277322441, 0.960776925},
}
for _, c := range cases {
if got := MthSin(c.in); math.Abs(float64(got-c.sin)) > 1e-7 {
t.Errorf("MthSin(%v) = %v, want %v", c.in, got, c.sin)
}
if got := MthCos(c.in); math.Abs(float64(got-c.cos)) > 1e-7 {
t.Errorf("MthCos(%v) = %v, want %v", c.in, got, c.cos)
}
}
// The table is deliberately coarse; if this ever matches libm the lookup
// has been replaced by math.Sin and every carver has moved.
if math.Abs(float64(MthSin(-1.0))-math.Sin(-1.0)) < 1e-6 {
t.Error("MthSin agrees with math.Sin to within 1e-6; the lookup table is gone")
}
}