Ejemplo n.º 1
0
func serializeChunkData(chunk *mapgen.Chunk) ([]byte, error) {
	raw := make([]byte, 1+coords.BlocksPerChunk)
	offset := 0

	raw[offset] = 1 // version
	offset++

	for i := 0; i < coords.BlocksPerChunk; i++ {
		raw[offset] = byte(chunk.Block(coords.IndexOffset(i)))
		offset++
	}
	return raw, nil
}
Ejemplo n.º 2
0
func deserializeChunkData(raw []byte) (*mapgen.Chunk, error) {
	offset := 0

	version := raw[offset]
	offset++
	if version != 1 {
		return nil, errors.New("Persist: Unrecognized chunk version.")
	}

	chunk := &mapgen.Chunk{}
	for i := 0; i < coords.BlocksPerChunk; i++ {
		chunk.SetBlock(coords.IndexOffset(i), mapgen.Block(raw[offset]))
		offset++
	}

	return chunk, nil
}