precalculate networked block palette buffer

This commit is contained in:
p2r3
2025-08-14 08:13:33 +03:00
parent 374943669e
commit 74c556af86
2 changed files with 22 additions and 7 deletions

View File

@@ -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 <stdint.h>
#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

View File

@@ -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();
}