RegionIO/internal/world/surface_verify_test.go
Master290 113a59e365 Resolve block names to the default state, not the first one
blocks.json lists a block's states in getPossibleStates() order -- the property
cartesian product -- and separately marks which one is the default. The parser
declared only id and properties, so the default flag was dropped on the floor
and nameToStateID returned states[0]. Those differ for 642 of the 1168 blocks.

What that produced, measured rather than guessed: every redstone vein in the
world was lit=true and glowing, every sunflower was placed as its own top half
with nothing under it, and oak stairs came out upside down and waterlogged. It
also quietly disagreed with the StateGrass, StateOakLog and StateOakLeaf
constants next door in encode.go, which are the real defaults.

Now it starts from the default state and applies the properties it recognises,
keeping the default's value for an unknown key or an illegal value -- which is
what vanilla does when it reads a palette entry. That matters on the disk path:
a chunk written with a property we no longer know used to decode to a random
corner state instead of something sane.

The signature grows an ok result, because air was doing double duty as both a
real block and "no such name".

Separately, blockPaletteEntry filled the Properties compound by ranging a Go
map. nbt.Compound preserves insertion order precisely so encoding is
deterministic, so saving one chunk twice produced different region-file bytes
for every multi-property block. Keys are sorted now.
2026-07-27 03:35:39 +03:00

56 lines
1.7 KiB
Go

package world
import (
"testing"
)
// TestSurfaceVariesByBiome is the load-bearing correctness check for surface
// rules: it generates real chunks and confirms the top surface block differs
// across biomes. Before surface rules every chunk resolved to grass (9); after,
// deserts/oceans/badlands carry sand/gravel/terracotta-family blocks.
func TestSurfaceVariesByBiome(t *testing.T) {
gen := NewVanillaGenerator(12345)
seen := make(map[uint16]int) // surfaceBlockID → chunk count
// Scan a moderate area to find dry land (surface above sea level), where
// surface rules actually place biome-specific blocks. Ocean columns sit
// below sea level and stay stone, which is correct.
for cx := 0; cx < 16; cx++ {
for cz := 0; cz < 16; cz++ {
ch := gen(int32(cx)-8, int32(cz)-8)
if ch == nil {
continue
}
blk, dry := centreSurfaceBlock(ch)
if !dry {
continue // skip ocean/underwater columns
}
if blk != 0 {
seen[blk]++
}
}
}
if len(seen) < 2 {
t.Fatalf("expected >=2 distinct dry-land surface blocks, got %d (%v)", len(seen), seen)
}
t.Logf("dry-land surface block distribution: %v", seen)
}
// centreSurfaceBlock returns the topmost non-air/non-water block at (8,8) and
// whether that block sits at or above sea level (i.e. it is dry land, where
// surface rules apply rather than being submerged).
func centreSurfaceBlock(ch *Chunk) (uint16, bool) {
for i := SectionCount - 1; i >= 0; i-- {
s := ch.sections[i]
if s == nil {
continue
}
for ly := 15; ly >= 0; ly-- {
st := s[blockIndex(8, MinY+i*16+ly, 8)]
if st != StateAir && st != StateWater {
// Dry only if this top block is at/above sea level.
return st, (MinY + i*16 + ly) >= SeaLevel
}
}
}
return 0, false
}