|
| 1 | +# Enhanced Chunk Data Structure Design |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document outlines the design for a high-performance chunk data structure optimized for Minecraft server implementation. The new architecture eliminates intermediate conversions and provides direct NBT-to-network serialization. |
| 6 | + |
| 7 | +## Architecture Goals |
| 8 | + |
| 9 | +- **Zero-Copy Operations**: Direct parsing from NBT to network format |
| 10 | +- **Memory Efficiency**: Optimal palette encoding and bit packing |
| 11 | +- **Protocol Compliance**: Full compatibility with Minecraft 1.21.5+ protocol |
| 12 | +- **Performance**: Minimal allocations and fast serialization |
| 13 | +- **Extensibility**: Support for custom world heights and future protocol changes |
| 14 | + |
| 15 | +## Core Components |
| 16 | + |
| 17 | +### 1. Paletted Container System |
| 18 | + |
| 19 | +The paletted container handles efficient storage of repetitive data (blocks, biomes) using three encoding strategies: |
| 20 | + |
| 21 | +- **Single Valued**: All entries are identical (0 bits per entry) |
| 22 | +- **Indirect**: Small palette with indices (1-8 bits for blocks, 1-3 for biomes) |
| 23 | +- **Direct**: Raw values (15 bits for blocks, 6 for biomes) |
| 24 | + |
| 25 | +```cpp |
| 26 | +class PalettedContainer { |
| 27 | +public: |
| 28 | + enum class Type { |
| 29 | + SINGLE_VALUED = 0, |
| 30 | + INDIRECT = 1, |
| 31 | + DIRECT = 2 |
| 32 | + }; |
| 33 | + |
| 34 | +private: |
| 35 | + Type _type; |
| 36 | + uint8_t _bitsPerEntry; |
| 37 | + std::vector<uint32_t> _palette; |
| 38 | + std::vector<uint64_t> _data; |
| 39 | + size_t _size; |
| 40 | + bool _isBlockContainer; |
| 41 | + |
| 42 | +public: |
| 43 | + explicit PalettedContainer(size_t size); |
| 44 | + |
| 45 | + void setSingleValue(uint32_t value); |
| 46 | + void setFromArray(const std::vector<uint32_t>& values); |
| 47 | + uint32_t getValue(size_t index) const; |
| 48 | + std::vector<uint8_t> serialize() const; |
| 49 | +}; |
| 50 | +``` |
| 51 | +
|
| 52 | +### 2. Height Map Handler |
| 53 | +
|
| 54 | +Manages multiple heightmap types with proper bit packing according to Minecraft specifications: |
| 55 | +
|
| 56 | +```cpp |
| 57 | +class HeightMapHandler { |
| 58 | +public: |
| 59 | + enum class Type { |
| 60 | + MOTION_BLOCKING = 1, |
| 61 | + MOTION_BLOCKING_NO_LEAVES = 2, |
| 62 | + OCEAN_FLOOR = 3, |
| 63 | + WORLD_SURFACE = 4, |
| 64 | + WORLD_SURFACE_WG = 5 |
| 65 | + }; |
| 66 | +
|
| 67 | +private: |
| 68 | + std::map<Type, std::vector<uint16_t>> _heightmaps; |
| 69 | + int _worldHeight; |
| 70 | + int _minY; |
| 71 | +
|
| 72 | +public: |
| 73 | + HeightMapHandler(int worldHeight = 384, int minY = -64); |
| 74 | + |
| 75 | + void setHeightMap(Type type, const std::vector<int64_t>& packedData); |
| 76 | + void setHeightMapDirect(Type type, const std::vector<uint16_t>& heights); |
| 77 | + void generateEmpty(); |
| 78 | + std::vector<uint8_t> serializeToNBT() const; |
| 79 | +}; |
| 80 | +``` |
| 81 | + |
| 82 | +### 3. Chunk Section Structure |
| 83 | + |
| 84 | +Represents a 16x16x16 section of blocks with associated data: |
| 85 | + |
| 86 | +```cpp |
| 87 | +struct ChunkSection { |
| 88 | + uint16_t blockCount = 0; |
| 89 | + std::unique_ptr<PalettedContainer> blockStates; // 4096 entries |
| 90 | + std::unique_ptr<PalettedContainer> biomes; // 64 entries |
| 91 | + |
| 92 | + std::vector<uint8_t> skyLight; // 2048 bytes |
| 93 | + std::vector<uint8_t> blockLight; // 2048 bytes |
| 94 | + |
| 95 | + bool hasSkyLight = false; |
| 96 | + bool hasBlockLight = false; |
| 97 | + bool isEmpty = true; |
| 98 | + |
| 99 | + ChunkSection(); |
| 100 | + |
| 101 | + void calculateBlockCount(const std::vector<uint32_t>& blockStateIds); |
| 102 | + void setBlocks(const std::vector<uint32_t>& blockStateIds); |
| 103 | + void setBiomes(const std::vector<uint32_t>& biomeIds); |
| 104 | + void initializeLighting(bool withSkyLight = true, bool withBlockLight = false); |
| 105 | + std::vector<uint8_t> serialize() const; |
| 106 | +}; |
| 107 | +``` |
| 108 | +
|
| 109 | +### 4. Enhanced Chunk Data |
| 110 | +
|
| 111 | +Complete chunk representation with all necessary components: |
| 112 | +
|
| 113 | +```cpp |
| 114 | +struct EnhancedChunkData { |
| 115 | + int32_t chunkX, chunkZ; |
| 116 | + |
| 117 | + HeightMapHandler heightmaps; |
| 118 | + std::vector<ChunkSection> sections; |
| 119 | + |
| 120 | + struct BlockEntity { |
| 121 | + uint8_t packedXZ; // (x & 15) << 4 | (z & 15) |
| 122 | + int16_t y; |
| 123 | + uint32_t type; |
| 124 | + std::vector<uint8_t> nbtData; |
| 125 | + |
| 126 | + BlockEntity(uint8_t x, uint8_t z, int16_t yPos, uint32_t typeId); |
| 127 | + }; |
| 128 | + std::vector<BlockEntity> blockEntities; |
| 129 | + |
| 130 | + bool isFullyGenerated = false; |
| 131 | + int64_t inhabitedTime = 0; |
| 132 | + int64_t lastUpdate = 0; |
| 133 | +
|
| 134 | + EnhancedChunkData(int32_t x, int32_t z, int worldHeight = 384, int minY = -64); |
| 135 | + |
| 136 | + ChunkSection* getSectionByY(int worldY, int minY = -64); |
| 137 | + bool isEmpty() const; |
| 138 | + |
| 139 | + static EnhancedChunkData generateEmpty(int32_t x, int32_t z, int worldHeight = 384); |
| 140 | +}; |
| 141 | +``` |
| 142 | + |
| 143 | +## Direct Parsing Architecture |
| 144 | + |
| 145 | +### 5. Direct Chunk Parser |
| 146 | + |
| 147 | +Eliminates intermediate conversions by parsing directly from NBT to enhanced structure: |
| 148 | + |
| 149 | +```cpp |
| 150 | +class DirectChunkParser { |
| 151 | +public: |
| 152 | + struct ParseConfig { |
| 153 | + int worldHeight = 384; |
| 154 | + int minY = -64; |
| 155 | + bool loadLighting = true; |
| 156 | + bool optimizePalettes = true; |
| 157 | + }; |
| 158 | + |
| 159 | +private: |
| 160 | + ParseConfig _config; |
| 161 | + |
| 162 | +public: |
| 163 | + DirectChunkParser(const ParseConfig& config = {}); |
| 164 | + |
| 165 | + EnhancedChunkData parseChunkFromRegion( |
| 166 | + const std::filesystem::path& regionPath, |
| 167 | + int32_t chunkX, |
| 168 | + int32_t chunkZ |
| 169 | + ); |
| 170 | + |
| 171 | + EnhancedChunkData parseChunkFromNBT( |
| 172 | + const nbt::NBT& chunkNBT, |
| 173 | + int32_t chunkX, |
| 174 | + int32_t chunkZ |
| 175 | + ); |
| 176 | + |
| 177 | +private: |
| 178 | + void parseHeightMaps(const nbt::TagCompound& heightmaps, EnhancedChunkData& chunk); |
| 179 | + void parseSections(const nbt::TagList& sections, EnhancedChunkData& chunk); |
| 180 | + void parseBlockStates(const nbt::TagCompound& blockStates, ChunkSection& section); |
| 181 | + void parseBiomes(const nbt::TagCompound& biomes, ChunkSection& section); |
| 182 | + |
| 183 | + std::vector<uint32_t> unpackPalettedData( |
| 184 | + const nbt::TagLongArray& data, |
| 185 | + const nbt::TagList* palette, |
| 186 | + size_t expectedSize, |
| 187 | + uint32_t defaultValue = 0 |
| 188 | + ); |
| 189 | +}; |
| 190 | +``` |
| 191 | +
|
| 192 | +### 6. Optimized World Query |
| 193 | +
|
| 194 | +High-performance chunk loading with caching support: |
| 195 | +
|
| 196 | +```cpp |
| 197 | +namespace World { |
| 198 | +
|
| 199 | +class OptimizedQuery { |
| 200 | +private: |
| 201 | + Manager& _worldManager; |
| 202 | + DirectChunkParser _parser; |
| 203 | + std::unordered_map<uint64_t, std::shared_ptr<EnhancedChunkData>> _chunkCache; |
| 204 | +
|
| 205 | +public: |
| 206 | + explicit OptimizedQuery(Manager& manager); |
| 207 | + |
| 208 | + EnhancedChunkData fetchChunkDirect(int32_t chunkX, int32_t chunkZ); |
| 209 | + std::shared_ptr<EnhancedChunkData> fetchChunkCached(int32_t chunkX, int32_t chunkZ); |
| 210 | + |
| 211 | + std::vector<EnhancedChunkData> fetchChunkBatch( |
| 212 | + const std::vector<std::pair<int32_t, int32_t>>& coordinates |
| 213 | + ); |
| 214 | + |
| 215 | + void clearCache(); |
| 216 | +}; |
| 217 | +
|
| 218 | +} // namespace World |
| 219 | +``` |
| 220 | + |
| 221 | +## Network Protocol Integration |
| 222 | + |
| 223 | +### 7. Packet Handler |
| 224 | + |
| 225 | +Optimized packet creation with direct serialization: |
| 226 | + |
| 227 | +```cpp |
| 228 | +class LevelChunkWithLightPacket { |
| 229 | +private: |
| 230 | + static constexpr uint32_t PACKET_ID = 0x27; |
| 231 | + |
| 232 | +public: |
| 233 | + static void send(Packet& packet, int32_t chunkX, int32_t chunkZ, Server& server); |
| 234 | + |
| 235 | +private: |
| 236 | + static void writeLightData(Buffer& buffer, const EnhancedChunkData& chunk); |
| 237 | +}; |
| 238 | +``` |
| 239 | +
|
| 240 | +## Performance Benefits |
| 241 | +
|
| 242 | +### Memory Efficiency |
| 243 | +- **Palette Optimization**: Automatic selection of most efficient encoding |
| 244 | +- **Bit Packing**: Optimal use of memory for height maps and light data |
| 245 | +- **Zero Copies**: Direct NBT to network conversion |
| 246 | +
|
| 247 | +### Processing Speed |
| 248 | +- **Single Pass**: Parse NBT directly to final structure |
| 249 | +- **Batch Loading**: Efficient I/O for multiple chunks |
| 250 | +- **Lazy Evaluation**: Load sections only when needed |
| 251 | +
|
| 252 | +### Network Optimization |
| 253 | +- **Protocol Compliance**: Exact adherence to Minecraft packet format |
| 254 | +- **Compression Ready**: Structures optimized for network transmission |
| 255 | +- **Light Data Masking**: Efficient sky/block light handling |
| 256 | +
|
| 257 | +## Migration Strategy |
| 258 | +
|
| 259 | +1. **Phase 1**: Implement core structures (PalettedContainer, HeightMapHandler) |
| 260 | +2. **Phase 2**: Create DirectChunkParser and integrate with existing NBT system |
| 261 | +3. **Phase 3**: Replace packet handlers to use enhanced structures |
| 262 | +4. **Phase 4**: Add caching and batch loading optimizations |
| 263 | +
|
| 264 | +## Extensibility Features |
| 265 | +
|
| 266 | +- **Custom World Heights**: Support for modded dimensions |
| 267 | +- **Additional Heightmaps**: Easy addition of new heightmap types |
| 268 | +- **Block Entity Extensions**: Flexible NBT data handling |
| 269 | +- **Version Compatibility**: Designed for future protocol changes |
| 270 | +
|
| 271 | +## Implementation Notes |
| 272 | +
|
| 273 | +- All bit operations follow Minecraft's little-endian conventions |
| 274 | +- Light data uses 4-bit nibbles packed into bytes |
| 275 | +- Palette indices are packed using variable bit widths |
| 276 | +- Height maps use ceil(log2(world_height + 1)) bits per entry |
| 277 | +- Block entities store coordinates in packed format for network efficiency |
0 commit comments