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 {
return &c, fmt.Errorf("read section[%d] error: %w", sectionY, err)
}
c.sections[sectionY] = sec
c.Sections[sectionY] = sec
}
}
return &c, nil
@ -93,7 +93,7 @@ type directSection struct {
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,
// 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).
@ -104,7 +104,7 @@ func (d *directSection) GetBlock(x, y, z int) (blockID uint32) {
l := 64 - offset%64
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 {
@ -112,7 +112,7 @@ type paletteSection struct {
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)
return p.palette[v]
return BlockStatus(p.palette[v])
}

View File

@ -4,22 +4,24 @@ import (
"github.com/Tnze/go-mc/bot/world/entity"
)
//World record all of the things in the world where player at
// World record all of the things in the world where player at
type World struct {
Entities map[int32]entity.Entity
Chunks map[ChunkLoc]*Chunk
}
//Chunk store a 256*16*16 column blocks
// Chunk store a 256*16*16 column blocks
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 {
GetBlock(x, y, z int) (blockID uint32)
GetBlock(x, y, z int) BlockStatus
}
type BlockStatus uint32
type ChunkLoc struct {
X, Z int
}
@ -42,25 +44,18 @@ type ChunkLoc struct {
// East
// )
// // getBlock return the block in the position (x, y, z)
// func (w *world) getBlock(x, y, z int) Block {
// c := w.chunks[chunkLoc{x >> 4, z >> 4}]
// if c != nil {
// cx, cy, cz := x&15, y&15, z&15
// /*
// n = n&(16-1)
// is equal to
// n %= 16
// if n < 0 { n += 16 }
// */
// return c.sections[y/16].blocks[cx][cy][cz]
// }
// return Block{id: 0}
// }
// getBlock return the block in the position (x, y, z)
func (w *World) GetBlockStatus(x, y, z int) BlockStatus {
// Use n>>4 rather then n/16. It acts wrong if n<0.
c := w.Chunks[ChunkLoc{x >> 4, z >> 4}]
if c != nil {
// (n&(16-1)) == (n<0 ? n%16+16 : n%16)
if sec := c.Sections[y>>4]; sec != nil {
return sec.GetBlock(x&15, y&15, z&15)
}
}
return 0
}
// func (b Block) String() string {
// return blockNameByID[b.id]