chunk

package
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SubChunkVersion is the current version of the written sub chunks, specifying the format they are
	// written on disk and over network.
	SubChunkVersion = 8
)

Variables

View Source
var AirRuntimeID uint32
View Source
var FilteringBlocks = make([]uint8, 0, 7000)

FilteringBlocks is a map for checking if a block runtime ID filters light, and if so, how many levels. Light is able to propagate through these blocks, but will have its level reduced.

View Source
var LightBlocks = make([]uint8, 0, 7000)

LightBlocks is a list of block light levels (0-15) indexed by block runtime IDs. The map is used to do a fast lookup of block light.

View Source
var RuntimeIDToState func(runtimeID uint32) (name string, properties map[string]interface{}, found bool)

RuntimeIDToState must hold a function to convert a runtime ID to a name and its state properties.

View Source
var StateToRuntimeID func(name string, properties map[string]interface{}) (runtimeID uint32, found bool)

StateToRuntimeID must hold a function to convert a name and its state properties to a runtime ID.

Functions

func FillLight

func FillLight(c *Chunk)

FillLight executes the light 'filling' stage, where the chunk is filled with light coming only from the chunk itself, without light crossing chunk borders.

func SpreadLight

func SpreadLight(c *Chunk, neighbours []*Chunk)

SpreadLight executes the light 'spreading' stage, where the chunk has its light spread into the neighbouring chunks. The neighbouring chunks must have passed the light 'filling' stage before this function is called for a chunk.

Types

type BlockStorage

type BlockStorage struct {
	// contains filtered or unexported fields
}

BlockStorage is a storage of 4096 blocks encoded in a variable amount of uint32s, storages may have blocks with a bit size per block of 1, 2, 3, 4, 5, 6, 8 or 16 bits. 3 of these formats have additional padding in every uint32 and an additional uint32 at the end, to cater for the blocks that don't fit. This padding is present when the storage has a block size of 3, 5 or 6 bytes. Methods on BlockStorage must not be called simultaneously from multiple goroutines.

func (*BlockStorage) Palette

func (storage *BlockStorage) Palette() *Palette

Palette returns the Palette of the block storage.

func (*BlockStorage) RuntimeID

func (storage *BlockStorage) RuntimeID(x, y, z byte) uint32

RuntimeID returns the runtime ID of the block located at the given x, y and z.

func (*BlockStorage) SetRuntimeID

func (storage *BlockStorage) SetRuntimeID(x, y, z byte, runtimeID uint32)

SetRuntimeID sets the given runtime ID at the given x, y and z. The palette and block storage are expanded automatically to make space for the runtime ID, should that be needed.

type Chunk

type Chunk struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Chunk is a segment in the world with a size of 16x16x256 blocks. A chunk contains multiple sub chunks and stores other information such as biomes. It is not safe to call methods on Chunk simultaneously from multiple goroutines.

func DiskDecode

func DiskDecode(data SerialisedData) (*Chunk, error)

DiskDecode decodes the data from a SerialisedData object into a chunk and returns it. If the data was invalid, an error is returned.

func NetworkDecode

func NetworkDecode(data []byte, subChunkCount int) (*Chunk, error)

NetworkDecode decodes the network serialised data passed into a Chunk if successful. If not, the chunk returned is nil and the error non-nil. The sub chunk count passed must be that found in the LevelChunk packet. noinspection GoUnusedExportedFunction

func New

func New() *Chunk

New initialises a new chunk and returns it, so that it may be used.

func (*Chunk) BiomeID

func (chunk *Chunk) BiomeID(x, z uint8) uint8

BiomeID returns the biome ID at a specific column in the chunk.

func (*Chunk) BlockNBT

func (chunk *Chunk) BlockNBT() map[[3]int]map[string]interface{}

BlockNBT returns a list of all block NBT data set in the chunk.

func (*Chunk) Compact

func (chunk *Chunk) Compact()

Compact compacts the chunk as much as possible, getting rid of any sub chunks that are empty, and compacts all storages in the sub chunks to occupy as little space as possible. Compact should be called right before the chunk is saved in order to optimise the storage space.

func (*Chunk) HighestBlock

func (chunk *Chunk) HighestBlock(x, z uint8) uint8

HighestBlock iterates from the highest non-empty sub chunk downwards to find the Y value of the highest non-air block at an x and z. If no blocks are present in the column, 0 is returned.

func (*Chunk) HighestLightBlocker

func (chunk *Chunk) HighestLightBlocker(x, z uint8) uint8

HighestLightBlocker iterates from the highest non-empty sub chunk downwards to find the Y value of the highest block that completely blocks any light from going through. If none is found, the value returned is 0.

func (*Chunk) Light

func (chunk *Chunk) Light(x, y, z uint8) uint8

Light returns the light level at a specific position in the chunk.

func (*Chunk) RuntimeID

func (chunk *Chunk) RuntimeID(x, y, z uint8, layer uint8) uint32

RuntimeID returns the runtime ID of the block at a given x, y and z in a chunk at the given layer. If no sub chunk exists at the given y, the block is assumed to be air.

func (*Chunk) SetBiomeID

func (chunk *Chunk) SetBiomeID(x, z, biomeID uint8)

SetBiomeID sets the biome ID at a specific column in the chunk.

func (*Chunk) SetBlockNBT

func (chunk *Chunk) SetBlockNBT(pos [3]int, data map[string]interface{})

SetBlockNBT sets block NBT data to a given position in the chunk. If the data passed is nil, the block NBT currently present will be cleared.

func (*Chunk) SetRuntimeID

func (chunk *Chunk) SetRuntimeID(x, y, z uint8, layer uint8, runtimeID uint32)

SetRuntimeID sets the runtime ID of a block at a given x, y and z in a chunk at the given layer. If no SubChunk exists at the given y, a new SubChunk is created and the block is set.

func (*Chunk) SkyLight

func (chunk *Chunk) SkyLight(x, y, z uint8) uint8

SkyLight returns the sky light level at a specific position in the chunk.

func (*Chunk) Sub

func (chunk *Chunk) Sub() []*SubChunk

Sub returns a list of all sub chunks present in the chunk.

type Palette

type Palette struct {
	// contains filtered or unexported fields
}

Palette is a palette of runtime IDs that every block storage has. Block storages hold 'pointers' to indexes in this palette.

func (*Palette) Add

func (palette *Palette) Add(runtimeID uint32) uint16

Add adds a runtime ID to the palette. It does not first if the runtime ID was already set in the palette. The index at which the runtime ID was added is returned.

func (*Palette) Index

func (palette *Palette) Index(runtimeID uint32) int

Index loops through the runtime IDs of the palette and looks for the index of the given runtime ID. If the runtime ID can not be found, -1 is returned.

func (*Palette) Len

func (palette *Palette) Len() int

Len returns the amount of unique block runtime IDs in the palette.

func (*Palette) Replace

func (palette *Palette) Replace(f func(runtimeID uint32) uint32)

Replace calls the function passed for each runtime ID present in the palette. The value returned by the function replaces the runtime ID present at the index of the runtime ID passed.

func (*Palette) RuntimeID

func (palette *Palette) RuntimeID(paletteIndex uint16) uint32

RuntimeID returns the runtime ID at the palette index given.

type SerialisedData

type SerialisedData struct {
	// sub holds the data of the serialised sub chunks in a chunk. Sub chunks that are empty or that otherwise
	// don't exist are represented as an empty slice (or technically, nil).
	SubChunks [16][]byte
	// Data2D is the 2D data of the chunk, which is composed of the biome IDs (256 bytes) and optionally the
	// height map of the chunk.
	Data2D []byte
	// BlockNBT is an encoded NBT array of all blocks that carry additional NBT, such as chests, with all
	// their contents.
	BlockNBT []byte
}

SerialisedData holds the serialised data of a chunk. It consists of the chunk's block data itself, a height map, the biomes and entities and block entities.

func DiskEncode

func DiskEncode(c *Chunk, blob bool) (d SerialisedData)

DiskEncode encodes a chunk to its disk representation, so that it may be stored in a database, giving other servers the ability to read the chunk.

func NetworkEncode

func NetworkEncode(c *Chunk) (d SerialisedData)

NetworkEncode encodes a chunk passed to its network representation and returns it as a SerialisedData, which may be sent over network.

type SubChunk

type SubChunk struct {
	// contains filtered or unexported fields
}

SubChunk is a cube of blocks located in a chunk. It has a size of 16x16x16 blocks and forms part of a stack that forms a Chunk.

func (*SubChunk) Layer

func (sub *SubChunk) Layer(layer uint8) *BlockStorage

Layer returns a certain block storage/layer from a sub chunk. If no storage at the layer exists, the layer is created, as well as all layers between the current highest layer and the new highest layer.

func (*SubChunk) Layers

func (sub *SubChunk) Layers() []*BlockStorage

Layers returns all layers in the sub chunk. This method may also return an empty slice.

func (*SubChunk) Light

func (sub *SubChunk) Light(x, y, z byte) uint8

Light returns the light level at a specific position in the sub chunk. It is max(block light, sky light).

func (*SubChunk) RuntimeID

func (sub *SubChunk) RuntimeID(x, y, z byte, layer uint8) uint32

RuntimeID returns the runtime ID of the block located at the given X, Y and Z. X, Y and Z must be in a range of 0-15.

func (*SubChunk) SetRuntimeID

func (sub *SubChunk) SetRuntimeID(x, y, z byte, layer uint8, runtimeID uint32)

SetRuntimeID sets the given runtime ID at the given X, Y and Z. X, Y and Z must be in a range of 0-15.

func (*SubChunk) SkyLightAt

func (sub *SubChunk) SkyLightAt(x, y, z byte) uint8

SkyLightAt returns the sky light value at a specific value at a specific position in the sub chunk.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL