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.
This commit is contained in:
Master290 2026-07-27 03:58:56 +03:00
parent 0f76058db6
commit c6185d88c8
13 changed files with 1402 additions and 9 deletions

46
internal/worldgen/mth.go Normal file
View file

@ -0,0 +1,46 @@
package worldgen
import "math"
// mth.go holds the bits of net.minecraft.util.Mth whose exact behaviour the
// generator depends on.
//
// The trigonometry is a 65536-entry lookup table, not the libm functions. That
// is not an optimisation detail to be tidied away: Mth.sin(-1.0) is -0.8414514
// where Math.sin(-1.0) is -0.8414709848078965, and every carver tunnel walks by
// repeatedly adding cos(yaw) and sin(pitch). Substituting math.Sin bends every
// tunnel in the world away from vanilla's.
const mthSinScale = 10430.378350470453
var mthSinTable [65536]float32
func init() {
for i := range mthSinTable {
mthSinTable[i] = float32(math.Sin(float64(i) / mthSinScale))
}
}
// MthSin is Mth.sin. The float-to-int conversion truncates towards zero in both
// Go and Java, and the mask makes the negative case wrap identically.
func MthSin(d float64) float32 {
return mthSinTable[int64(d*mthSinScale)&65535]
}
// MthCos is Mth.cos: the same table, a quarter turn along.
func MthCos(d float64) float32 {
return mthSinTable[int64(d*mthSinScale+16384.0)&65535]
}
// mthFloor is Mth.floor: a true floor, not a truncating cast.
func mthFloor(d float64) int { return int(math.Floor(d)) }
// randomBetween is Mth.randomBetween: a float in [lo, hi), one draw.
func randomBetween(r RandomSource, lo, hi float32) float32 {
return r.NextFloat()*(hi-lo) + lo
}
// randomBetweenInclusive is Mth.randomBetweenInclusive: an int in [lo, hi].
func randomBetweenInclusive(r RandomSource, lo, hi int) int {
return int(r.NextIntN(int32(hi-lo+1))) + lo
}