Update level/chunk and heightmaps

This commit is contained in:
Tnze
2023-04-22 17:43:10 +08:00
parent e17642945d
commit de2996336c
5 changed files with 71 additions and 52 deletions

View File

@ -12,27 +12,24 @@ import (
// Chunk is 16* chunk
type Chunk struct {
DataVersion int32
XPos int32 `nbt:"xPos"`
YPos int32 `nbt:"yPos"`
ZPos int32 `nbt:"zPos"`
BlockEntities []nbt.RawMessage `nbt:"block_entities"`
Structures nbt.RawMessage `nbt:"structures"`
Heightmaps struct {
MotionBlocking []uint64 `nbt:"MOTION_BLOCKING"`
MotionBlockingNoLeaves []uint64 `nbt:"MOTION_BLOCKING_NO_LEAVES"`
OceanFloor []uint64 `nbt:"OCEAN_FLOOR"`
WorldSurface []uint64 `nbt:"WORLD_SURFACE"`
}
Sections []Section `nbt:"sections"`
BlockTicks nbt.RawMessage `nbt:"block_ticks"`
FluidTicks nbt.RawMessage `nbt:"fluid_ticks"`
PostProcessing nbt.RawMessage
BlockEntities []nbt.RawMessage `nbt:"block_entities"`
BlockTicks nbt.RawMessage `nbt:"block_ticks"`
CarvingMasks map[string][]uint64
DataVersion int32
Entities []nbt.RawMessage `nbt:"entities"`
FluidTicks nbt.RawMessage `nbt:"fluid_ticks"`
Heightmaps map[string][]uint64 // keys: "WORLD_SURFACE_WG", "WORLD_SURFACE", "WORLD_SURFACE_IGNORE_SNOW", "OCEAN_FLOOR_WG", "OCEAN_FLOOR", "MOTION_BLOCKING", "MOTION_BLOCKING_NO_LEAVES"
InhabitedTime int64
IsLightOn byte `nbt:"isLightOn"`
LastUpdate int64
Lights []nbt.RawMessage
PostProcessing nbt.RawMessage
Sections []Section `nbt:"sections"`
Status string
Structures nbt.RawMessage `nbt:"structures"`
XPos int32 `nbt:"xPos"`
YPos int32 `nbt:"yPos"`
ZPos int32 `nbt:"zPos"`
}
type Section struct {
@ -68,12 +65,13 @@ func (c *Chunk) Load(data []byte) (err error) {
case 3:
// none compression
}
if err != nil {
return err
}
_, err = nbt.NewDecoder(r).Decode(c)
d := nbt.NewDecoder(r)
//d.DisallowUnknownFields()
_, err = d.Decode(c)
return
}

View File

@ -1,29 +1,47 @@
package save
import (
"path/filepath"
"testing"
"github.com/Tnze/go-mc/save/region"
)
func TestColumn(t *testing.T) {
r, err := region.Open("testdata/region/r.0.0.mca")
files, err := filepath.Glob("testdata/region/r.*.*.mca")
if err != nil {
t.Fatal(err)
return
}
defer r.Close()
for _, filename := range files {
r, err := region.Open(filename)
if err != nil {
t.Fatal(err)
}
var c Chunk
data, err := r.ReadSector(0, 0)
if err != nil {
t.Fatal(err)
}
err = c.Load(data)
if err != nil {
t.Fatal(err)
}
for x := 0; x < 32; x++ {
for z := 0; z < 32; z++ {
if !r.ExistSector(x, z) {
continue
}
t.Logf("%+v", c)
data, err := r.ReadSector(x, z)
if err != nil {
t.Fatalf("read %s sec (%d, %d) fail: %v", filepath.Base(filename), x, z, err)
}
var c Chunk
err = c.Load(data)
if err != nil {
t.Fatalf("read %s sec (%d, %d) fail: %v", filepath.Base(filename), x, z, err)
}
}
}
err = r.Close()
if err != nil {
t.Fatal(err)
}
}
}
func BenchmarkColumn_Load(b *testing.B) {

View File

@ -233,9 +233,9 @@ func (r *Region) ExistSector(x, z int) bool {
return r.offsets[z][x] != 0
}
// PadToFullSector writes zeros to the end of the file to make size a multiple of 4096
// Legacy versions of Minecraft require this
// Need to be called right before Close
// PadToFullSector writes zeros to the end of the file to make size a multiple of 4096.
// Legacy versions of Minecraft require this.
// Need to be called right before Close.
func (r *Region) PadToFullSector() error {
size, err := r.f.Seek(0, io.SeekEnd)
if err != nil {