diff --git a/bot/world/chunk.go b/bot/world/chunk.go index cf25df6..675c69d 100644 --- a/bot/world/chunk.go +++ b/bot/world/chunk.go @@ -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<> 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]