Skip to content

Commit

Permalink
SkyLight and BlockLight
Browse files Browse the repository at this point in the history
  • Loading branch information
Tnze committed May 21, 2022
1 parent 2b92ad6 commit 5f3f66e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
52 changes: 38 additions & 14 deletions level/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"math/bits"
"strings"
"unsafe"

"github.com/Tnze/go-mc/level/block"
"github.com/Tnze/go-mc/nbt"
Expand Down Expand Up @@ -194,15 +193,23 @@ func ChunkFromSave(c *save.Chunk) *Chunk {
sections := make([]Section, secs)
for _, v := range c.Sections {
i := int32(v.Y) - c.YPos
// TODO: the error is ignored
sections[i].BlockCount, sections[i].States, _ = readStatesPalette(v.BlockStates.Palette, v.BlockStates.Data)
sections[i].Biomes, _ = readBiomesPalette(v.Biomes.Palette, v.Biomes.Data)
var err error
sections[i].BlockCount, sections[i].States, err = readStatesPalette(v.BlockStates.Palette, v.BlockStates.Data)
if err != nil {
panic(err)
}
sections[i].Biomes, err = readBiomesPalette(v.Biomes.Palette, v.Biomes.Data)
if err != nil {
panic(err)
}
sections[i].SkyLight = v.SkyLight
sections[i].BlockLight = v.BlockLight
}

motionBlocking := *(*[]uint64)(unsafe.Pointer(&c.Heightmaps.MotionBlocking))
motionBlockingNoLeaves := *(*[]uint64)(unsafe.Pointer(&c.Heightmaps.MotionBlockingNoLeaves))
oceanFloor := *(*[]uint64)(unsafe.Pointer(&c.Heightmaps.OceanFloor))
worldSurface := *(*[]uint64)(unsafe.Pointer(&c.Heightmaps.WorldSurface))
motionBlocking := c.Heightmaps.MotionBlocking
motionBlockingNoLeaves := c.Heightmaps.MotionBlockingNoLeaves
oceanFloor := c.Heightmaps.OceanFloor
worldSurface := c.Heightmaps.WorldSurface

bitsForHeight := bits.Len( /* chunk height in blocks */ uint(secs) * 16)
return &Chunk{
Expand Down Expand Up @@ -264,6 +271,8 @@ func ChunkToSave(c *Chunk, dst *save.Chunk) {
s.Y = int8(int32(i) + dst.YPos)
states.Palette, states.Data = writeStatesPalette(v.States)
biomes.Palette, biomes.Data = writeBiomesPalette(v.Biomes)
s.SkyLight = v.SkyLight
s.BlockLight = v.BlockLight
}
dst.Sections = sections
//dst.Heightmaps.MotionBlocking = c.HeightMaps.MotionBlocking.Raw()
Expand Down Expand Up @@ -306,6 +315,22 @@ func (c *Chunk) WriteTo(w io.Writer) (int64, error) {
if err != nil {
return 0, err
}
light := lightData{
SkyLightMask: make(pk.BitSet, (16*16*16-1)>>6+1),
BlockLightMask: make(pk.BitSet, (16*16*16-1)>>6+1),
SkyLight: []pk.ByteArray{},
BlockLight: []pk.ByteArray{},
}
for i, v := range c.Sections {
if v.SkyLight != nil {
light.SkyLightMask.Set(i, true)
light.SkyLight = append(light.SkyLight, v.SkyLight)
}
if v.BlockLight != nil {
light.BlockLightMask.Set(i, true)
light.BlockLight = append(light.BlockLight, v.BlockLight)
}
}
return pk.Tuple{
// Heightmaps
pk.NBT(struct {
Expand All @@ -317,12 +342,7 @@ func (c *Chunk) WriteTo(w io.Writer) (int64, error) {
}),
pk.ByteArray(data),
pk.Array(c.BlockEntity),
&lightData{
SkyLightMask: make(pk.BitSet, (16*16*16-1)>>6+1),
BlockLightMask: make(pk.BitSet, (16*16*16-1)>>6+1),
SkyLight: []pk.ByteArray{},
BlockLight: []pk.ByteArray{},
},
&light,
}.WriteTo(w)
}

Expand Down Expand Up @@ -416,6 +436,10 @@ type Section struct {
BlockCount int16
States *PaletteContainer[BlocksState]
Biomes *PaletteContainer[BiomesState]
// Half a byte per light value.
// Could be nil if not exist
SkyLight []byte // len() == 2048
BlockLight []byte // len() == 2048
}

func (s *Section) GetBlock(i int) BlocksState {
Expand Down
4 changes: 2 additions & 2 deletions save/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ type Section struct {
Palette []string `nbt:"palette"`
Data []uint64 `nbt:"data"`
} `nbt:"biomes"`
SkyLight []int8
BlockLight []int8
SkyLight []byte
BlockLight []byte
}

type BlockState struct {
Expand Down

0 comments on commit 5f3f66e

Please sign in to comment.