github.com/df-mc/dragonfly@v0.9.13/server/world/chunk/sub_chunk.go (about)

     1  package chunk
     2  
     3  // SubChunk is a cube of blocks located in a chunk. It has a size of 16x16x16 blocks and forms part of a stack
     4  // that forms a Chunk.
     5  type SubChunk struct {
     6  	air        uint32
     7  	storages   []*PalettedStorage
     8  	blockLight []uint8
     9  	skyLight   []uint8
    10  }
    11  
    12  // NewSubChunk creates a new sub chunk. All sub chunks should be created through this function
    13  func NewSubChunk(air uint32) *SubChunk {
    14  	return &SubChunk{air: air}
    15  }
    16  
    17  // Empty checks if the SubChunk is considered empty. This is the case if the SubChunk has 0 block storages or if it has
    18  // a single one that is completely filled with air.
    19  func (sub *SubChunk) Empty() bool {
    20  	return len(sub.storages) == 0 || (len(sub.storages) == 1 && len(sub.storages[0].palette.values) == 1 && sub.storages[0].palette.values[0] == sub.air)
    21  }
    22  
    23  // Layer returns a certain block storage/layer from a sub chunk. If no storage at the layer exists, the layer
    24  // is created, as well as all layers between the current highest layer and the new highest layer.
    25  func (sub *SubChunk) Layer(layer uint8) *PalettedStorage {
    26  	for uint8(len(sub.storages)) <= layer {
    27  		// Keep appending to storages until the requested layer is achieved. Makes working with new layers
    28  		// much easier.
    29  		sub.storages = append(sub.storages, emptyStorage(sub.air))
    30  	}
    31  	return sub.storages[layer]
    32  }
    33  
    34  // Layers returns all layers in the sub chunk. This method may also return an empty slice.
    35  func (sub *SubChunk) Layers() []*PalettedStorage {
    36  	return sub.storages
    37  }
    38  
    39  // Block returns the runtime ID of the block located at the given X, Y and Z. X, Y and Z must be in a
    40  // range of 0-15.
    41  func (sub *SubChunk) Block(x, y, z byte, layer uint8) uint32 {
    42  	if uint8(len(sub.storages)) <= layer {
    43  		return sub.air
    44  	}
    45  	return sub.storages[layer].At(x, y, z)
    46  }
    47  
    48  // SetBlock sets the given block runtime ID at the given X, Y and Z. X, Y and Z must be in a range of 0-15.
    49  func (sub *SubChunk) SetBlock(x, y, z byte, layer uint8, block uint32) {
    50  	sub.Layer(layer).Set(x, y, z, block)
    51  }
    52  
    53  // SetBlockLight sets the block light value at a specific position in the sub chunk.
    54  func (sub *SubChunk) SetBlockLight(x, y, z byte, level uint8) {
    55  	if ptr := &sub.blockLight[0]; ptr == noLightPtr {
    56  		// Copy the block light as soon as it is changed to create a COW system.
    57  		sub.blockLight = append([]byte(nil), sub.blockLight...)
    58  	}
    59  	index := (uint16(x) << 8) | (uint16(z) << 4) | uint16(y)
    60  
    61  	i := index >> 1
    62  	bit := (index & 1) << 2
    63  	sub.blockLight[i] = (sub.blockLight[i] & (0xf0 >> bit)) | (level << bit)
    64  }
    65  
    66  // BlockLight returns the block light value at a specific value at a specific position in the sub chunk.
    67  func (sub *SubChunk) BlockLight(x, y, z byte) uint8 {
    68  	index := (uint16(x) << 8) | (uint16(z) << 4) | uint16(y)
    69  	return (sub.blockLight[index>>1] >> ((index & 1) << 2)) & 0xf
    70  }
    71  
    72  // SetSkyLight sets the skylight value at a specific position in the sub chunk.
    73  func (sub *SubChunk) SetSkyLight(x, y, z byte, level uint8) {
    74  	if ptr := &sub.skyLight[0]; ptr == fullLightPtr || ptr == noLightPtr {
    75  		// Copy the skylight as soon as it is changed to create a COW system.
    76  		sub.skyLight = append([]byte(nil), sub.skyLight...)
    77  	}
    78  	index := (uint16(x) << 8) | (uint16(z) << 4) | uint16(y)
    79  
    80  	i := index >> 1
    81  	bit := (index & 1) << 2
    82  	sub.skyLight[i] = (sub.skyLight[i] & (0xf0 >> bit)) | (level << bit)
    83  }
    84  
    85  // SkyLight returns the skylight value at a specific value at a specific position in the sub chunk.
    86  func (sub *SubChunk) SkyLight(x, y, z byte) uint8 {
    87  	index := (uint16(x) << 8) | (uint16(z) << 4) | uint16(y)
    88  	return (sub.skyLight[index>>1] >> ((index & 1) << 2)) & 0xf
    89  }
    90  
    91  // Compact cleans the garbage from all block storages that sub chunk contains, so that they may be
    92  // cleanly written to a database.
    93  func (sub *SubChunk) compact() {
    94  	newStorages := make([]*PalettedStorage, 0, len(sub.storages))
    95  	for _, storage := range sub.storages {
    96  		storage.compact()
    97  		if len(storage.palette.values) == 1 && storage.palette.values[0] == sub.air {
    98  			// If the palette has only air in it, it means the storage is empty, so we can ignore it.
    99  			continue
   100  		}
   101  		newStorages = append(newStorages, storage)
   102  	}
   103  	sub.storages = newStorages
   104  }