From 74c556af867b941e92d631e95cf270527fd2a516 Mon Sep 17 00:00:00 2001 From: p2r3 Date: Thu, 14 Aug 2025 08:13:33 +0300 Subject: [PATCH] precalculate networked block palette buffer --- build_registries.js | 16 ++++++++++++++++ src/packets.c | 13 ++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/build_registries.js b/build_registries.js index 7559c1a..bcd7ca9 100644 --- a/build_registries.js +++ b/build_registries.js @@ -194,6 +194,14 @@ function serializeTags (tags) { return Buffer.concat([lengthBuf, fullData]); } +function toVarIntBuffer (array) { + const parts = []; + for (const num of array) { + parts.push(writeVarInt(num)); + } + return Buffer.concat(parts); +} + // Convert to C-style hex byte array string function toCArray (buffer) { const hexBytes = [...buffer].map(b => `0x${b.toString(16).padStart(2, "0")}`); @@ -267,6 +275,8 @@ async function convert () { } }); + const networkBlockPalette = toVarIntBuffer(Object.values(itemsAndBlocks.palette)); + const sourceCode = `\ #include #include "registries.h" @@ -282,6 +292,11 @@ ${toCArray(tagBuffer)} // Block palette uint16_t block_palette[] = { ${Object.values(itemsAndBlocks.palette).join(", ")} }; +// Block palette as VarInt buffer +uint8_t network_block_palette[] = { +${toCArray(networkBlockPalette)} +}; + // Block-to-item mapping uint16_t B_to_I[] = { ${itemsAndBlocks.mappingWithOverrides.join(", ")} }; // Item-to-block mapping @@ -305,6 +320,7 @@ extern uint8_t registries_bin[${fullRegistryBuffer.length}]; extern uint8_t tags_bin[${tagBuffer.length}]; extern uint16_t block_palette[256]; // Block palette +extern uint8_t network_block_palette[${networkBlockPalette.length}]; // Block palette as VarInt buffer extern uint16_t B_to_I[256]; // Block-to-item mapping uint8_t I_to_B (uint16_t item); // Item-to-block mapping diff --git a/src/packets.c b/src/packets.c index 19d6dcb..9b5b8dd 100644 --- a/src/packets.c +++ b/src/packets.c @@ -284,10 +284,7 @@ int sc_setCenterChunk (int client_fd, int x, int y) { // S->C Chunk Data and Update Light int sc_chunkDataAndUpdateLight (int client_fd, int _x, int _z) { - int palette_size = 0; - for (int i = 0; i < 256; i ++) palette_size += sizeVarInt(block_palette[i]); - - const int chunk_data_size = (4101 + sizeVarInt(256) + palette_size) * 24; + const int chunk_data_size = (4101 + sizeVarInt(256) + sizeof(network_block_palette)) * 24; writeVarInt(client_fd, 17 + sizeVarInt(chunk_data_size) + chunk_data_size); writeByte(client_fd, 0x27); @@ -306,13 +303,15 @@ int sc_chunkDataAndUpdateLight (int client_fd, int _x, int _z) { y = i * 16 - 64; writeUint16(client_fd, 4096); // block count writeByte(client_fd, 8); // bits per entry - writeVarInt(client_fd, 256); - for (int j = 0; j < 256; j ++) writeVarInt(client_fd, block_palette[j]); + writeVarInt(client_fd, 256); // block palette length + // block palette as varint buffer + send(client_fd, network_block_palette, sizeof(network_block_palette), 0); + // chunk section buffer buildChunkSection(x, y, z); send(client_fd, chunk_section, 4096, 0); // biome data writeByte(client_fd, 0); // bits per entry - writeByte(client_fd, W_plains); // palette + writeByte(client_fd, W_plains); // biome palette // reset watchdog and yield wdt_reset(); }