Documentation
¶
Index ¶
- Constants
- func BlockMatrixIsEmpty(matrix BlockMatrix) bool
- func BlockNoChange(diff DiffMatrix) bool
- func ChunkNoChange(diff ChunkDiffMatrix) bool
- func Index(pos DimChunk) []byte
- func IndexBlockDu(pos DimChunk, timeID uint) []byte
- func IndexNBTDu(pos DimChunk, timeID uint) []byte
- func LayerNoChange(diff LayersDiff) bool
- func MatrixToChunk(matrix ChunkMatrix, r define.Range, blockPalette *BlockPalette) (c *chunk.Chunk)
- func NBTNoChange(diff MultipleDiffNBT) bool
- func Sum(pos DimChunk, p ...byte) []byte
- func ToChunkNBT(nbts []NBTWithIndex) (result []map[string]any, err error)
- type BlockIndex
- type BlockMatrix
- type BlockPalette
- func (b *BlockPalette) AddBlock(blockRuntimeID uint32)
- func (b *BlockPalette) BlockPalette() []uint32
- func (b *BlockPalette) BlockPaletteIndex(blockRuntimeID uint32) uint32
- func (b *BlockPalette) BlockPaletteLen() int
- func (b *BlockPalette) BlockRuntimeID(blockPaletteIndex uint32) uint32
- func (b *BlockPalette) SetBlockPalette(newPalette []uint32)
- type ChunkBlockIndex
- type ChunkDiffMatrix
- type ChunkMatrix
- type DiffMatrix
- type DiffNBTWithIndex
- type DimChunk
- type Layers
- type LayersDiff
- type MultipleDiffNBT
- type NBTWithIndex
- func FromChunkNBT(chunkPos define.ChunkPos, nbts []map[string]any) (result []NBTWithIndex)
- func NBTDeepCopy(src []NBTWithIndex) (result []NBTWithIndex, err error)
- func NBTRestore(old []NBTWithIndex, diff MultipleDiffNBT) (result []NBTWithIndex, err error)
- func NewBlockNBT(relativePos [3]uint16, nbt map[string]any) *NBTWithIndex
- type SingleBlockDiff
Constants ¶
const ( ModifiedNBTBSDiff byte = iota ModifiedNBTOrigin )
const ( KeyChunkGlobalData = "tbplrg" KeyBlockDeltaUpdate = "du" KeyNBTDeltaUpdate = "du'" KeyLatestTimePointUnixTime = 'T' KeyLatestChunk = 'm' KeyLatestNBT = "m'" )
Keys on a per-chunk basis. These are prefixed by only the chunk coordinates.
const MatrixSize = 4096
MatrixSize is the size of block matrix. A single sub chunk only holds 4096 blocks, so we here use 4096 as the size.
Variables ¶
This section is empty.
Functions ¶
func BlockMatrixIsEmpty ¶
func BlockMatrixIsEmpty(matrix BlockMatrix) bool
BlockMatrixIsEmpty checks the given block martix is empty or not.
func BlockNoChange ¶
func BlockNoChange(diff DiffMatrix) bool
BlockNoChange reports diff is empty or not.
func ChunkNoChange ¶
func ChunkNoChange(diff ChunkDiffMatrix) bool
ChunkNoChange reports diff is empty or not.
func Index ¶
Index returns a bytes holding the written index of the chunk position passed.
Different from standard Minecraft world, we write the x and z position of this chunk first, and then 2 bytes to represents the dimension id of this chunk.
Therefore, we use and return 10 bytes in total.
func IndexBlockDu ¶
IndexBlockDu returns a bytes holding the written index of the chunk position passed, but specially for blocks delta update used key to index.
func IndexNBTDu ¶
IndexNBTDu returns a bytes holding the written index of the chunk position passed, but specially for NBTs delta update used key to index.
func LayerNoChange ¶
func LayerNoChange(diff LayersDiff) bool
LayerNoChange reports diff is empty or not.
func MatrixToChunk ¶
func MatrixToChunk(matrix ChunkMatrix, r define.Range, blockPalette *BlockPalette) (c *chunk.Chunk)
MatrixToChunk converts the chunk matrix to its chunk represents. r is the range of this chunk, and blockPalette is this chunk matrix used.
func NBTNoChange ¶
func NBTNoChange(diff MultipleDiffNBT) bool
NBTNoChange reports diff is empty or not.
func Sum ¶
Sum converts Index(pos) to its []byte representation and appends p. Note that Sum is very necessary because all Sum do is preventing users from believing that "append" can create new slices (however, it not).
func ToChunkNBT ¶
func ToChunkNBT(nbts []NBTWithIndex) (result []map[string]any, err error)
ToChunkNBT converts nbts to []map[string]any. Note that the returned slice is the deep copied NBTs.
Types ¶
type BlockIndex ¶
type BlockIndex uint16
BlockIndex is a integer that ranging from 0 to 4095, and can be decode and represents as a relative position to a sub chunk.
func (BlockIndex) Marshal ¶
func (b BlockIndex) Marshal(buf *bytes.Buffer)
Marshal encode BlockIndex to its bytes represents, and writes to bytes buffer.
func (*BlockIndex) Unmarshal ¶
func (b *BlockIndex) Unmarshal(buf *bytes.Buffer)
Unmarshal decode a BlockIndex from the underlying bytes buffer.
func (*BlockIndex) UpdateIndex ¶
func (b *BlockIndex) UpdateIndex(x uint8, y uint8, z uint8)
UpdateIndex computes the index of the block in the sub chunk based on the given relative coordinates of x, y, and z. Then, updates the index of this block.
func (BlockIndex) X ¶
func (b BlockIndex) X() uint8
X returns the X-axis relative coordinates of this block with respect to the sub chunk.
func (BlockIndex) Y ¶
func (b BlockIndex) Y() uint8
Y returns the Y-axis relative coordinates of this block with respect to the sub chunk.
func (BlockIndex) Z ¶
func (b BlockIndex) Z() uint8
Z returns the Z-axis relative coordinates of this block with respect to the sub chunk.
type BlockMatrix ¶
type BlockMatrix *[MatrixSize]uint32
BlockMatrix represents a block matrix at a specific point in time.
func BlockRestore ¶
func BlockRestore(old BlockMatrix, diff DiffMatrix) BlockMatrix
BlockRestore use old and diff to compute the newer block matrix. Note that the returned block martix is the same object of old when old is not empty. Time complexity: O(l), l=len(diff).
Note that you could do this operation for all difference array, then you will get the final block matrix that represents the latest one.
In this case, the time complexity is O(n×L) where n is the length of these difference array, and L is the average length of all the diff slice.
func NewBlockMatrix ¶
func NewBlockMatrix() BlockMatrix
NewBlockMatrix creates a new BlockMatrix that full of air and is not nil.
type BlockPalette ¶
type BlockPalette struct {
// contains filtered or unexported fields
}
BlockPalette is the block palette for a chunk timeline. All time point in this line will share the same palette.
func NewBlockPalette ¶
func NewBlockPalette() *BlockPalette
NewBlockPalette creates a new block palette that only have a air block. Technically speaking, air does not actually exist, but it can be found by agreement.
func (*BlockPalette) AddBlock ¶
func (b *BlockPalette) AddBlock(blockRuntimeID uint32)
AddBlock adds the block whose block runtime id is blockRuntimeID to the underlying block palette. If is exist or the given block is air, then do no operation.
func (*BlockPalette) BlockPalette ¶
func (b *BlockPalette) BlockPalette() []uint32
BlockPalette gets the deep copy of underlying block palette. The block palette is actually multiple block runtime IDs.
func (*BlockPalette) BlockPaletteIndex ¶
func (b *BlockPalette) BlockPaletteIndex(blockRuntimeID uint32) uint32
BlockPaletteIndex finds the index of blockRuntimeID in block palette. If not exist, then added it the underlying block palette.
Returned index is the real index plus 1. If you got 0, then that means this is an air block. We don't save air block in block palette, and you should to pay attention to it.
func (*BlockPalette) BlockPaletteLen ¶
func (b *BlockPalette) BlockPaletteLen() int
BlockPaletteLen returns the length of underlying block palette.
func (*BlockPalette) BlockRuntimeID ¶
func (b *BlockPalette) BlockRuntimeID(blockPaletteIndex uint32) uint32
BlockRuntimeID return the block runtime ID that crresponding to blockPaletteIndex. Will not check if blockPaletteIndex is out of index (if out of index, then runtime panic).
func (*BlockPalette) SetBlockPalette ¶
func (b *BlockPalette) SetBlockPalette(newPalette []uint32)
SetBlockPalette sets the underlying block palette to newPalette. We assume all elements (block runtime IDs) in newPalette is unique and all is valid Minecraft standard blocks.
type ChunkBlockIndex ¶
type ChunkBlockIndex struct {
// contains filtered or unexported fields
}
ChunkBlockIndex holds two (u)int16 integers which the first one is ranging from 0 to 4095, and the second one is the sub chunk Y position of this block.
Those two integers could be decode and represents as a block relative position to the chunk.
func (ChunkBlockIndex) Marshal ¶
func (c ChunkBlockIndex) Marshal(buf *bytes.Buffer)
Marshal encode ChunkBlockIndex to its bytes represents, and writes to bytes buffer.
func (*ChunkBlockIndex) Unmarshal ¶
func (c *ChunkBlockIndex) Unmarshal(buf *bytes.Buffer)
Unmarshal decode a ChunkBlockIndex from the underlying bytes buffer.
func (*ChunkBlockIndex) UpdateIndex ¶
func (c *ChunkBlockIndex) UpdateIndex(x uint8, y int16, z uint8)
UpdateIndex computes the index of the block in the chunk based on the given relative coordinates of x, y, and z. Then, updates the index of this block.
func (ChunkBlockIndex) X ¶
func (c ChunkBlockIndex) X() uint8
X returns the X-axis relative coordinates of this block with respect to the chunk.
func (ChunkBlockIndex) Y ¶
func (c ChunkBlockIndex) Y() int16
Y returns the Y-axis relative coordinates of this block with respect to the chunk.
func (ChunkBlockIndex) Z ¶
func (c ChunkBlockIndex) Z() uint8
Z returns the Z-axis relative coordinates of this block with respect to the chunk.
type ChunkDiffMatrix ¶
type ChunkDiffMatrix []LayersDiff
ChunkDiffMatrix represents the difference for all sub chunks in the target chunk between two different times.
func ChunkDifference ¶
func ChunkDifference(older ChunkMatrix, newer ChunkMatrix) ChunkDiffMatrix
ChunkDifference computes the difference between older and newer. We assume len(older) = len(newer).
Time complexity: O(n×L), n=len(older). L is the average changes for each sub chunk.
type ChunkMatrix ¶
type ChunkMatrix []Layers
ChunkMatrix represents the chunk matrix that holds all block of this chunk. A chunk can have multiple sub chunks, and each sub chunks can have multiple layers. A single layer in one sub chunk only have 4096 blocks.
func ChunkRestore ¶
func ChunkRestore(old ChunkMatrix, diff ChunkDiffMatrix) ChunkMatrix
BlockRestore use old and diff to compute the newer chunk matrix. We assume len(old) = len(diff).
Time complexity: O(n×L), n=len(old). L is the average count of changes that each sub chunk have.
To reduce the time causes, the block martix in layers of each sub chunk in the returned chunk martix is the same one in the corresponding layer that come from old.
Note that you could do this operation for all difference array, then you will get the final block matrix that represents the latest one.
In this case, the time complexity is O(k×n×L) where k is the length of these difference array.
func ChunkToMatrix ¶
func ChunkToMatrix(c *chunk.Chunk, blockPalette *BlockPalette) (result ChunkMatrix)
ChunkToMatrix converts a chunk to its chunk martix represents. blockPalette is the block palette of this chunk timeline used.
type DiffMatrix ¶
type DiffMatrix []SingleBlockDiff
DiffMatrix is a matrix that holds the difference of BlockMatrix between time i-1 and time i. Note that i must bigger than 0.
func BlockDifference ¶
func BlockDifference(older BlockMatrix, newer BlockMatrix) DiffMatrix
BlockDifference computes the difference between older and newer. Time complexity: O(4096).
type DiffNBTWithIndex ¶
type DiffNBTWithIndex struct {
Index ChunkBlockIndex
DiffNBT []byte
}
DiffNBTWithIndex represents the difference between the same NBT block but on different time. Index is an integer that range from 0 to 4095, and could be decode as a block relative position to the chunk.
func NewDiffNBT ¶
func NewDiffNBT(olderNBT *NBTWithIndex, newerNBT *NBTWithIndex) (result *DiffNBTWithIndex, err error)
NewDiffNBT returns the difference between between olderNBT and newerNBT. Note that olderNBT and newerNBT must represents the NBt block in the same position.
Time complexity: O(C). Note that C is not very small and is little big due to
- Use bsdiff to do restore to reduce bytes use.
- Use xxhash to ensure when user do restore operation, they can verify the data they get is correct.
func (DiffNBTWithIndex) Restore ¶
func (d DiffNBTWithIndex) Restore(olderNBT NBTWithIndex) (result *NBTWithIndex, err error)
Restore use olderNBT and DiffNBTWithIndex it self to compute the newer block NBT data, and return the restor result that with the same chunk block index to olderNBt but newer NBT.
Note that you could do this operation for all difference like []DiffNBTWithIndex, then you will get the final block NBT data that represents the latest one.
In this case, the time complexity is O(C×n) where n is the length of these difference array. Note that C is not very small and is little big due to we use bsdiff to do restore and use xxhash to ensure the data we get is correct.
type Layers ¶
type Layers []BlockMatrix
Layers represents the block matrix of a sub chunk. A single sub chunk could have multiple layers, and each layer holds 4096 blocks.
func LayerRestore ¶
func LayerRestore(old Layers, diff LayersDiff) Layers
LayerRestore use old and diff to compute the newer layers. Time complexity: O(L×n), n = len(diff). L is the average length of each element in diff.
To reduce the time causes, the block martix in returned layers is the same one in the old layers.
Note that you could do this operation for all difference array, then you will get the final layers that represents the latest one.
In this case, the time complexity is O(C) where C is the count of all block changes.
func (*Layers) Layer ¶
func (l *Layers) Layer(layer int) BlockMatrix
Layer get the the block matrix which in layer. If not exist, then create empty layer, as well as all layers between the current highest layer and the new highest layer.
type LayersDiff ¶
type LayersDiff []DiffMatrix
LayersDiff represents the difference for all layers in the target sub chunk between two different times.
func LayerDifference ¶
func LayerDifference(older Layers, newer Layers) LayersDiff
LayerDifference computes the difference between older and newer. Time complexity: O(L×n), n = max(len(older), len(newer)). L is the average changes of each layer in the sub chunk.
func (*LayersDiff) Layer ¶
func (d *LayersDiff) Layer(layer int) DiffMatrix
Layer get the the difference block matrix which in layer. If not exist, then create empty layer, as well as all layers between the current highest layer and the new highest layer.
type MultipleDiffNBT ¶
type MultipleDiffNBT struct {
Removed []ChunkBlockIndex
Added []NBTWithIndex
Modified []DiffNBTWithIndex
}
MultipleDiffNBT represents the difference between NBT blocks in the same chunk but different times. All the NBT blocks should in the same position in this chunk, and MultipleDiffNBT just refer to the states (add/remove/modify) of these blocks in different times.
func NBTDifference ¶
func NBTDifference(older []NBTWithIndex, newer []NBTWithIndex) (result *MultipleDiffNBT, err error)
NBTDifference computes the difference between multiple block NBT changes in one single chunk. older and newer are represents the different time of these NBT blocks in the same chunk.
Time complexity: O(C×k + (a+b)), a=len(older), b=len(newer).
k is the number that shown the counts of changed (modified) NBT blocks. Note that C is not very small and is little big due to we use bsdiff and xxhash for each modified NBT block.
type NBTWithIndex ¶
type NBTWithIndex struct {
Index ChunkBlockIndex
NBT map[string]any
}
NBTWithIndex represents a single NBT block entity data who in a chunk. Index is an integer that range from 0 to 4095, and could be decode as a block relative position to the chunk.
func FromChunkNBT ¶
func FromChunkNBT(chunkPos define.ChunkPos, nbts []map[string]any) (result []NBTWithIndex)
FromChunkNBT converts nbts to []NBTWithIndex, chunkPos is the position of this chunk. For all NBT block that not in this chunk (or their NBT is broken), then we will ignore. Note that the returned slice is not the deep copied NBTs.
func NBTDeepCopy ¶
func NBTDeepCopy(src []NBTWithIndex) (result []NBTWithIndex, err error)
NBTDeepCopy return the deep copy of src.
func NBTRestore ¶
func NBTRestore(old []NBTWithIndex, diff MultipleDiffNBT) (result []NBTWithIndex, err error)
NBTRestore computes the newer block NBT data of this chunk by given old and diff.
Time complexity: O(a+C×b), a=len(old), b=len(diff.Modified). Note that C is not very small and is little big due to we use bsdiff and xxhash for each modified NBT block.
func NewBlockNBT ¶
func NewBlockNBT(relativePos [3]uint16, nbt map[string]any) *NBTWithIndex
NewBlockNBT creates a new NBTWithIndex by given relativePos and nbt. relativePos is the relative position of this block NBT to chunk, and nbt is the block NBT data of this NBT block.
type SingleBlockDiff ¶
SingleBlockDiff represents a single block change who in a sub chunk.