add GetBlock function

This commit is contained in:
Tnze
2020-05-17 14:45:27 +08:00
parent eba832b424
commit fa75535f37
2 changed files with 24 additions and 29 deletions

View File

@ -21,7 +21,7 @@ func DecodeChunkColumn(mask int32, data []byte) (*Chunk, error) {
if err != nil { if err != nil {
return &c, fmt.Errorf("read section[%d] error: %w", sectionY, err) return &c, fmt.Errorf("read section[%d] error: %w", sectionY, err)
} }
c.sections[sectionY] = sec c.Sections[sectionY] = sec
} }
} }
return &c, nil return &c, nil
@ -93,7 +93,7 @@ type directSection struct {
data []int64 data []int64
} }
func (d *directSection) GetBlock(x, y, z int) (blockID uint32) { func (d *directSection) GetBlock(x, y, z int) BlockStatus {
// According to wiki.vg: Data Array is given for each block with increasing x coordinates, // According to wiki.vg: Data Array is given for each block with increasing x coordinates,
// within rows of increasing z coordinates, within layers of increasing y coordinates. // within rows of increasing z coordinates, within layers of increasing y coordinates.
// So offset equals to ( x*16^0 + z*16^1 + y*16^2 )*(bits per block). // So offset equals to ( x*16^0 + z*16^1 + y*16^2 )*(bits per block).
@ -104,7 +104,7 @@ func (d *directSection) GetBlock(x, y, z int) (blockID uint32) {
l := 64 - offset%64 l := 64 - offset%64
block |= uint32(d.data[offset/64+1] << l) block |= uint32(d.data[offset/64+1] << l)
} }
return block & (1<<d.bpb - 1) // mask return BlockStatus(block & (1<<d.bpb - 1)) // mask
} }
type paletteSection struct { type paletteSection struct {
@ -112,7 +112,7 @@ type paletteSection struct {
directSection directSection
} }
func (p *paletteSection) GetBlock(x, y, z int) (blockID uint32) { func (p *paletteSection) GetBlock(x, y, z int) BlockStatus {
v := p.directSection.GetBlock(x, y, z) v := p.directSection.GetBlock(x, y, z)
return p.palette[v] return BlockStatus(p.palette[v])
} }

View File

@ -12,14 +12,16 @@ type World struct {
// Chunk store a 256*16*16 column blocks // Chunk store a 256*16*16 column blocks
type Chunk struct { type Chunk struct {
sections [16]Section Sections [16]Section
} }
// Section store a 16*16*16 cube blocks // Section store a 16*16*16 cube blocks
type Section interface { type Section interface {
GetBlock(x, y, z int) (blockID uint32) GetBlock(x, y, z int) BlockStatus
} }
type BlockStatus uint32
type ChunkLoc struct { type ChunkLoc struct {
X, Z int X, Z int
} }
@ -42,25 +44,18 @@ type ChunkLoc struct {
// East // East
// ) // )
// // getBlock return the block in the position (x, y, z) // getBlock return the block in the position (x, y, z)
// func (w *world) getBlock(x, y, z int) Block { func (w *World) GetBlockStatus(x, y, z int) BlockStatus {
// c := w.chunks[chunkLoc{x >> 4, z >> 4}] // Use n>>4 rather then n/16. It acts wrong if n<0.
// if c != nil { c := w.Chunks[ChunkLoc{x >> 4, z >> 4}]
// cx, cy, cz := x&15, y&15, z&15 if c != nil {
// /* // (n&(16-1)) == (n<0 ? n%16+16 : n%16)
// n = n&(16-1) if sec := c.Sections[y>>4]; sec != nil {
return sec.GetBlock(x&15, y&15, z&15)
// is equal to }
}
// n %= 16 return 0
// if n < 0 { n += 16 } }
// */
// return c.sections[y/16].blocks[cx][cy][cz]
// }
// return Block{id: 0}
// }
// func (b Block) String() string { // func (b Block) String() string {
// return blockNameByID[b.id] // return blockNameByID[b.id]