fix bugs in PaletteContainer
This commit is contained in:
@ -2,13 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/server"
|
||||
"image"
|
||||
_ "image/png"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/server"
|
||||
)
|
||||
|
||||
const MaxPlayer = 20
|
||||
@ -23,7 +22,13 @@ func main() {
|
||||
log.Fatalf("Set server info error: %v", err)
|
||||
}
|
||||
defaultDimension := server.NewSimpleDim(16)
|
||||
defaultDimension.LoadChunk(server.ChunkPos{X: 0, Z: 0}, server.EmptyChunk(16))
|
||||
chunk00 := server.EmptyChunk(16)
|
||||
for s := 0; s < 16; s++ {
|
||||
for i := 0; i < 16*16; i++ {
|
||||
chunk00.Sections[s].SetBlock(i, 1)
|
||||
}
|
||||
}
|
||||
defaultDimension.LoadChunk(server.ChunkPos{X: 0, Z: 0}, chunk00)
|
||||
s := server.Server{
|
||||
ListPingHandler: serverInfo,
|
||||
LoginHandler: &server.MojangLoginHandler{
|
||||
|
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/Tnze/go-mc/level"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
@ -189,7 +190,7 @@ func drawSection(s *save.Chunk, img *image.RGBA) {
|
||||
|
||||
// decode status
|
||||
data := *(*[]uint64)(unsafe.Pointer(&s.BlockStates)) // convert []int64 into []uint64
|
||||
bs := save.NewBitStorage(bpb, 4096, data)
|
||||
bs := level.NewBitStorage(bpb, 4096, data)
|
||||
for y := 0; y < 16; y++ {
|
||||
layerImg := image.NewRGBA(image.Rect(0, 0, 16, 16))
|
||||
for i := 16*16 - 1; i >= 0; i-- {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package save
|
||||
package level
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -29,7 +29,13 @@ type BitStorage struct {
|
||||
// The "data" is optional for initializing. Panic if data != nil && len(data) != calcBitStorageSize(bits, length).
|
||||
func NewBitStorage(bits, length int, data []uint64) (b *BitStorage) {
|
||||
if bits == 0 {
|
||||
return nil
|
||||
return &BitStorage{
|
||||
data: nil,
|
||||
mask: 0,
|
||||
bits: 0,
|
||||
length: length,
|
||||
valuesPerLong: 0,
|
||||
}
|
||||
}
|
||||
|
||||
b = &BitStorage{
|
||||
@ -76,13 +82,15 @@ func (b *BitStorage) calcIndex(n int) (c, o int) {
|
||||
|
||||
// Swap sets v into [i], and return the previous [i] value.
|
||||
func (b *BitStorage) Swap(i, v int) (old int) {
|
||||
if b == nil || i < 0 || i > b.length-1 {
|
||||
panic(indexOutOfBounds)
|
||||
if b.valuesPerLong == 0 {
|
||||
return 0
|
||||
}
|
||||
if v < 0 || uint64(v) > b.mask {
|
||||
panic(valueOutOfBounds)
|
||||
}
|
||||
|
||||
if i < 0 || i > b.length-1 {
|
||||
panic(indexOutOfBounds)
|
||||
}
|
||||
c, offset := b.calcIndex(i)
|
||||
l := b.data[c]
|
||||
old = int(l >> offset & b.mask)
|
||||
@ -92,12 +100,15 @@ func (b *BitStorage) Swap(i, v int) (old int) {
|
||||
|
||||
// Set sets v into [i].
|
||||
func (b *BitStorage) Set(i, v int) {
|
||||
if b == nil || i < 0 || i > b.length-1 {
|
||||
panic(indexOutOfBounds)
|
||||
if b.valuesPerLong == 0 {
|
||||
return
|
||||
}
|
||||
if v < 0 || uint64(v) > b.mask {
|
||||
panic(valueOutOfBounds)
|
||||
}
|
||||
if i < 0 || i > b.length-1 {
|
||||
panic(indexOutOfBounds)
|
||||
}
|
||||
|
||||
c, offset := b.calcIndex(i)
|
||||
l := b.data[c]
|
||||
@ -106,7 +117,10 @@ func (b *BitStorage) Set(i, v int) {
|
||||
|
||||
// Get gets [i] value.
|
||||
func (b *BitStorage) Get(i int) int {
|
||||
if b == nil || i < 0 || i > b.length-1 {
|
||||
if b.valuesPerLong == 0 {
|
||||
return 0
|
||||
}
|
||||
if i < 0 || i > b.length-1 {
|
||||
panic(indexOutOfBounds)
|
||||
}
|
||||
|
||||
@ -117,9 +131,6 @@ func (b *BitStorage) Get(i int) int {
|
||||
|
||||
// Len is the number of stored values.
|
||||
func (b *BitStorage) Len() int {
|
||||
if b == nil {
|
||||
return 0
|
||||
}
|
||||
return b.length
|
||||
}
|
||||
|
||||
@ -155,6 +166,9 @@ func (b *BitStorage) ReadFrom(r io.Reader) (int64, error) {
|
||||
}
|
||||
|
||||
func (b *BitStorage) WriteTo(w io.Writer) (int64, error) {
|
||||
if b == nil {
|
||||
return pk.VarInt(0).WriteTo(w)
|
||||
}
|
||||
n, err := pk.VarInt(len(b.data)).WriteTo(w)
|
||||
if err != nil {
|
||||
return n, err
|
@ -1,4 +1,4 @@
|
||||
package save
|
||||
package level
|
||||
|
||||
import (
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
@ -1,4 +1,4 @@
|
||||
package save
|
||||
package level
|
||||
|
||||
import (
|
||||
"io"
|
||||
@ -7,42 +7,38 @@ import (
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
)
|
||||
|
||||
type BlockState interface {
|
||||
}
|
||||
type state = int
|
||||
|
||||
type PaletteContainer struct {
|
||||
bits int
|
||||
maps IdMaps
|
||||
config func(maps IdMaps, bits int) palette
|
||||
config func(bits int) palette
|
||||
palette palette
|
||||
data *BitStorage
|
||||
}
|
||||
|
||||
func NewStatesPaletteContainer(maps IdMaps, length int) *PaletteContainer {
|
||||
func NewStatesPaletteContainer(length int, defaultValue state) *PaletteContainer {
|
||||
return &PaletteContainer{
|
||||
bits: 0,
|
||||
maps: maps,
|
||||
config: createStatesPalette,
|
||||
palette: createStatesPalette(maps, 0),
|
||||
palette: &singleValuePalette{v: defaultValue},
|
||||
data: NewBitStorage(0, length, nil),
|
||||
}
|
||||
}
|
||||
|
||||
func NewBiomesPaletteContainer(maps IdMaps, length int) *PaletteContainer {
|
||||
func NewBiomesPaletteContainer(length int, defaultValue state) *PaletteContainer {
|
||||
return &PaletteContainer{
|
||||
bits: 0,
|
||||
maps: maps,
|
||||
config: createBiomesPalette,
|
||||
palette: createBiomesPalette(maps, 0),
|
||||
palette: &singleValuePalette{v: defaultValue},
|
||||
data: NewBitStorage(0, length, nil),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PaletteContainer) Get(i int) BlockState {
|
||||
func (p *PaletteContainer) Get(i int) state {
|
||||
return p.palette.value(p.data.Get(i))
|
||||
}
|
||||
|
||||
func (p *PaletteContainer) Set(i int, v BlockState) {
|
||||
func (p *PaletteContainer) Set(i int, v state) {
|
||||
if vv, ok := p.palette.id(v); ok {
|
||||
p.data.Set(i, vv)
|
||||
} else {
|
||||
@ -50,14 +46,13 @@ func (p *PaletteContainer) Set(i int, v BlockState) {
|
||||
oldLen := p.data.Len()
|
||||
newPalette := PaletteContainer{
|
||||
bits: vv,
|
||||
maps: p.maps,
|
||||
config: p.config,
|
||||
palette: p.config(p.maps, vv),
|
||||
palette: p.config(vv),
|
||||
data: NewBitStorage(vv, oldLen+1, nil),
|
||||
}
|
||||
// copy
|
||||
for i := 0; i < oldLen; i++ {
|
||||
raw := p.palette.value(i)
|
||||
raw := p.data.Get(i)
|
||||
if vv, ok := newPalette.palette.id(raw); !ok {
|
||||
panic("not reachable")
|
||||
} else {
|
||||
@ -80,7 +75,7 @@ func (p *PaletteContainer) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
p.palette = p.config(p.maps, int(bits))
|
||||
p.palette = p.config(int(bits))
|
||||
|
||||
nn, err := p.palette.ReadFrom(r)
|
||||
n += nn
|
||||
@ -96,47 +91,28 @@ func (p *PaletteContainer) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func createStatesPalette(maps IdMaps, bits int) palette {
|
||||
func createStatesPalette(bits int) palette {
|
||||
switch bits {
|
||||
case 0:
|
||||
return &singleValuePalette{
|
||||
maps: maps,
|
||||
v: nil,
|
||||
}
|
||||
return &singleValuePalette{v: -1}
|
||||
case 1, 2, 3, 4:
|
||||
return &linearPalette{
|
||||
maps: maps,
|
||||
bits: 4,
|
||||
}
|
||||
return &linearPalette{bits: 4, values: make([]state, 0, 1<<4)}
|
||||
case 5, 6, 7, 8:
|
||||
// TODO: HashMapPalette
|
||||
return &linearPalette{
|
||||
maps: maps,
|
||||
bits: bits,
|
||||
}
|
||||
return &linearPalette{bits: bits, values: make([]state, 0, 1<<bits)}
|
||||
default:
|
||||
return &globalPalette{
|
||||
maps: maps,
|
||||
}
|
||||
return &globalPalette{}
|
||||
}
|
||||
}
|
||||
|
||||
func createBiomesPalette(maps IdMaps, bits int) palette {
|
||||
func createBiomesPalette(bits int) palette {
|
||||
switch bits {
|
||||
case 0:
|
||||
return &singleValuePalette{
|
||||
maps: maps,
|
||||
v: nil,
|
||||
}
|
||||
return &singleValuePalette{v: -1}
|
||||
case 1, 2, 3:
|
||||
return &linearPalette{
|
||||
maps: maps,
|
||||
bits: bits,
|
||||
}
|
||||
return &linearPalette{bits: bits, values: make([]state, 0, 1<<bits)}
|
||||
default:
|
||||
return &globalPalette{
|
||||
maps: maps,
|
||||
}
|
||||
return &globalPalette{}
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,25 +127,15 @@ func (p *PaletteContainer) WriteTo(w io.Writer) (n int64, err error) {
|
||||
type palette interface {
|
||||
pk.FieldEncoder
|
||||
pk.FieldDecoder
|
||||
id(v BlockState) (int, bool)
|
||||
value(i int) BlockState
|
||||
}
|
||||
|
||||
type IdMaps interface {
|
||||
getID(state BlockState) (id int)
|
||||
getValue(id int) (state BlockState)
|
||||
id(v state) (int, bool)
|
||||
value(i int) state
|
||||
}
|
||||
|
||||
type singleValuePalette struct {
|
||||
maps IdMaps
|
||||
v BlockState
|
||||
v state
|
||||
}
|
||||
|
||||
func (s *singleValuePalette) id(v BlockState) (int, bool) {
|
||||
if s.v == nil {
|
||||
s.v = v
|
||||
return 0, true
|
||||
}
|
||||
func (s *singleValuePalette) id(v state) (int, bool) {
|
||||
if s.v == v {
|
||||
return 0, true
|
||||
}
|
||||
@ -177,8 +143,8 @@ func (s *singleValuePalette) id(v BlockState) (int, bool) {
|
||||
return 1, false
|
||||
}
|
||||
|
||||
func (s *singleValuePalette) value(i int) BlockState {
|
||||
if s.v != nil && i == 0 {
|
||||
func (s *singleValuePalette) value(i int) state {
|
||||
if i == 0 {
|
||||
return s.v
|
||||
}
|
||||
panic("singleValuePalette: " + strconv.Itoa(i) + " out of bounds")
|
||||
@ -190,21 +156,20 @@ func (s *singleValuePalette) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s.v = s.maps.getValue(int(i))
|
||||
s.v = state(i)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *singleValuePalette) WriteTo(w io.Writer) (n int64, err error) {
|
||||
return pk.VarInt(s.maps.getID(s.v)).WriteTo(w)
|
||||
return pk.VarInt(s.v).WriteTo(w)
|
||||
}
|
||||
|
||||
type linearPalette struct {
|
||||
maps IdMaps
|
||||
values []BlockState
|
||||
values []state
|
||||
bits int
|
||||
}
|
||||
|
||||
func (l *linearPalette) id(v BlockState) (int, bool) {
|
||||
func (l *linearPalette) id(v state) (int, bool) {
|
||||
for i, t := range l.values {
|
||||
if t == v {
|
||||
return i, true
|
||||
@ -217,11 +182,11 @@ func (l *linearPalette) id(v BlockState) (int, bool) {
|
||||
return l.bits + 1, false
|
||||
}
|
||||
|
||||
func (l *linearPalette) value(i int) BlockState {
|
||||
func (l *linearPalette) value(i int) state {
|
||||
if i >= 0 && i < len(l.values) {
|
||||
return l.values[i]
|
||||
}
|
||||
return nil
|
||||
panic("linearPalette: " + strconv.Itoa(i) + " out of bounds")
|
||||
}
|
||||
|
||||
func (l *linearPalette) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
@ -235,7 +200,7 @@ func (l *linearPalette) ReadFrom(r io.Reader) (n int64, err error) {
|
||||
} else {
|
||||
n += nn
|
||||
}
|
||||
l.values[i] = l.maps.getValue(int(value))
|
||||
l.values[i] = state(value)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -245,7 +210,7 @@ func (l *linearPalette) WriteTo(w io.Writer) (n int64, err error) {
|
||||
return
|
||||
}
|
||||
for _, v := range l.values {
|
||||
if nn, err := pk.VarInt(l.maps.getID(v)).WriteTo(w); err != nil {
|
||||
if nn, err := pk.VarInt(v).WriteTo(w); err != nil {
|
||||
return n + nn, err
|
||||
} else {
|
||||
n += nn
|
||||
@ -254,15 +219,13 @@ func (l *linearPalette) WriteTo(w io.Writer) (n int64, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
type globalPalette struct {
|
||||
maps IdMaps
|
||||
type globalPalette struct{}
|
||||
|
||||
func (g *globalPalette) id(v state) (int, bool) {
|
||||
return v, true
|
||||
}
|
||||
|
||||
func (g *globalPalette) id(v BlockState) (int, bool) {
|
||||
return g.maps.getID(v), true
|
||||
}
|
||||
|
||||
func (g *globalPalette) value(i int) BlockState {
|
||||
func (g *globalPalette) value(i int) state {
|
||||
return g.value(i)
|
||||
}
|
||||
|
@ -2,12 +2,13 @@ package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
"github.com/Tnze/go-mc/save"
|
||||
"io"
|
||||
"math/bits"
|
||||
"sync"
|
||||
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
"github.com/Tnze/go-mc/level"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
)
|
||||
|
||||
type Dimension interface {
|
||||
@ -24,14 +25,28 @@ type DimInfo struct {
|
||||
type ChunkPos struct{ X, Z int }
|
||||
type Chunk struct {
|
||||
sync.Mutex
|
||||
sections []Section
|
||||
HeightMaps *save.BitStorage
|
||||
Sections []Section
|
||||
HeightMaps *level.BitStorage
|
||||
}
|
||||
|
||||
type Section struct {
|
||||
blockCount int16
|
||||
States *save.PaletteContainer
|
||||
Biomes *save.PaletteContainer
|
||||
States *level.PaletteContainer
|
||||
Biomes *level.PaletteContainer
|
||||
}
|
||||
|
||||
func (s *Section) GetBlock(i int) int {
|
||||
return s.States.Get(i)
|
||||
}
|
||||
func (s *Section) SetBlock(i int, v int) {
|
||||
// TODO: Handle cave air and void air
|
||||
if s.States.Get(i) != 0 {
|
||||
s.blockCount--
|
||||
}
|
||||
if v != 0 {
|
||||
s.blockCount++
|
||||
}
|
||||
s.States.Set(i, v)
|
||||
}
|
||||
|
||||
func (s *Section) WriteTo(w io.Writer) (int64, error) {
|
||||
@ -55,13 +70,13 @@ func EmptyChunk(secs int) *Chunk {
|
||||
for i := range sections {
|
||||
sections[i] = Section{
|
||||
blockCount: 0,
|
||||
States: save.NewStatesPaletteContainer(nil, 16*16*16),
|
||||
Biomes: save.NewBiomesPaletteContainer(nil, 4*4*4),
|
||||
States: level.NewStatesPaletteContainer(16*16*16, 0),
|
||||
Biomes: level.NewBiomesPaletteContainer(4*4*4, 0),
|
||||
}
|
||||
}
|
||||
return &Chunk{
|
||||
sections: sections,
|
||||
HeightMaps: save.NewBitStorage(bits.Len(uint(secs)*16), 16*16, nil),
|
||||
Sections: sections,
|
||||
HeightMaps: level.NewBitStorage(bits.Len(uint(secs)*16), 16*16, nil),
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,7 +97,7 @@ func (c *Chunk) WriteTo(w io.Writer) (int64, error) {
|
||||
|
||||
func (c *Chunk) Data() ([]byte, error) {
|
||||
var buff bytes.Buffer
|
||||
for _, section := range c.sections {
|
||||
for _, section := range c.Sections {
|
||||
_, err := section.WriteTo(&buff)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Reference in New Issue
Block a user