items and struture loading
This commit is contained in:
+16
-11
@@ -2,8 +2,6 @@ package level
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
|
||||
@@ -22,7 +20,8 @@ func UnpackArrayPalette(r io.Reader, bitsPerEntry uint8, numberOfEntries int) (d
|
||||
}
|
||||
var pallet []int32 // remember we can't use make because this is user-controlled data
|
||||
for range palletLen {
|
||||
entry, err := proto_base.DecodeVarInt(r)
|
||||
var entry int32
|
||||
entry, err = proto_base.DecodeVarInt(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -76,20 +75,18 @@ func LongsToData(data []uint64, n, bpe int32) (ret []int32) {
|
||||
return
|
||||
}
|
||||
|
||||
// UnpackBlockData minecraft does this at net.minecraft.world.chunk.PaletteProvider
|
||||
func UnpackBlockData(r io.Reader) (blocks []int32, err error) {
|
||||
var bitsPerEntry uint8
|
||||
err = binary.Read(r, binary.BigEndian, &bitsPerEntry)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println("blocks bpe", bitsPerEntry)
|
||||
switch bitsPerEntry {
|
||||
case 0:
|
||||
blocks, err = UnpackSingleValuePalette(r, BlocksPerChunkSection)
|
||||
case 1, 2, 3, 4:
|
||||
case 1, 2, 3, 4, 5, 6, 7, 8:
|
||||
blocks, err = UnpackArrayPalette(r, bitsPerEntry, BlocksPerChunkSection)
|
||||
case 5, 6, 7, 8:
|
||||
err = errors.New("unimplemented")
|
||||
default:
|
||||
blocks, err = UnpackLongData(r, bitsPerEntry, BlocksPerChunkSection)
|
||||
}
|
||||
@@ -106,7 +103,6 @@ func UnpackBiomeData(r io.Reader) (biomes []int32, err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println("biome bpe", bitsPerEntry)
|
||||
switch bitsPerEntry {
|
||||
case 0:
|
||||
biomes, err = UnpackSingleValuePalette(r, BiomesPerChunkSection)
|
||||
@@ -132,9 +128,18 @@ func UnpackSection(r io.Reader) (blocks, biomes []int32, err error) {
|
||||
return
|
||||
}
|
||||
biomes, err = UnpackBiomeData(r)
|
||||
if err != nil {
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
func UnpackNSections(r io.Reader, n int) (blocks, biomes [][]int32, err error) {
|
||||
for range n {
|
||||
var blocksSection, biomesSections []int32
|
||||
blocksSection, biomesSections, err = UnpackSection(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
blocks = append(blocks, blocksSection)
|
||||
biomes = append(biomes, biomesSections)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package level
|
||||
|
||||
import (
|
||||
"github.com/admin-else/strom/nbt"
|
||||
"github.com/go-viper/mapstructure/v2"
|
||||
)
|
||||
|
||||
func LoadStructure(fileName string) (s *Structure, err error) {
|
||||
n, err := nbt.ReadFilePath(fileName)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s = &Structure{}
|
||||
err = mapstructure.Decode(n.Value, s)
|
||||
return
|
||||
}
|
||||
|
||||
type Structure struct {
|
||||
DataVersion int32
|
||||
Blocks []struct {
|
||||
Pos [3]int32
|
||||
State int
|
||||
}
|
||||
Palette []struct {
|
||||
Name string
|
||||
Properties map[string]string
|
||||
}
|
||||
Size [3]int32
|
||||
}
|
||||
|
||||
func (s *Structure) GetBlock(x, y, z int32) (name string) {
|
||||
for _, block := range s.Blocks {
|
||||
if block.Pos[0] == x && block.Pos[1] == y && block.Pos[2] == z {
|
||||
palletIndex := block.State
|
||||
|
||||
if palletIndex >= len(s.Palette) {
|
||||
return ""
|
||||
}
|
||||
|
||||
return s.Palette[palletIndex].Name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user