mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
Use binary storage for recipe overlay for 8x space reduction
This commit is contained in:
1 parent
1c06611cca
commit
c5c26bfad1
4 files changed
+413
-185
No files matched your search
+139
-52
@@ -74,19 +74,19 @@ export async function renderChunkToPixels(chunkData: BinaryData): Promise<Uint16
|
||||
try {
|
||||
const decompressed = await inflateData(chunkData);
|
||||
const pixels = new Uint16Array(32 * 32);
|
||||
|
||||
|
||||
// Ensure we don't read beyond available data
|
||||
const maxPixels = Math.min(decompressed.length / 2, 32 * 32);
|
||||
|
||||
|
||||
for (let i = 0; i < maxPixels; i++) {
|
||||
pixels[i] = readUInt16LE(decompressed, i * 2);
|
||||
}
|
||||
|
||||
|
||||
// Fill remaining pixels with black (0) if we have less data than expected
|
||||
for (let i = maxPixels; i < 32 * 32; i++) {
|
||||
pixels[i] = 0;
|
||||
}
|
||||
|
||||
|
||||
return pixels;
|
||||
} catch (decompressError) {
|
||||
console.error(`Failed to decompress chunk data: ${decompressError}`);
|
||||
@@ -100,7 +100,7 @@ export async function renderChunkToPixels(chunkData: BinaryData): Promise<Uint16
|
||||
*/
|
||||
export function extractChunkFromTile(tilePixels: Uint16Array, chunkX: number, chunkY: number): Uint16Array {
|
||||
const chunkPixels = new Uint16Array(32 * 32);
|
||||
|
||||
|
||||
for (let y = 0; y < 32; y++) {
|
||||
for (let x = 0; x < 32; x++) {
|
||||
const tileX = chunkX * 32 + x;
|
||||
@@ -110,7 +110,7 @@ export function extractChunkFromTile(tilePixels: Uint16Array, chunkX: number, ch
|
||||
chunkPixels[chunkIndex] = tilePixels[tileIndex];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return chunkPixels;
|
||||
}
|
||||
|
||||
@@ -120,13 +120,13 @@ export function extractChunkFromTile(tilePixels: Uint16Array, chunkX: number, ch
|
||||
*/
|
||||
export async function renderTileToPixels(tileData: BinaryData, maxTick?: number): Promise<Uint16Array> {
|
||||
const currentPixels = new Uint16Array(256 * 256); // 256x256 tile
|
||||
|
||||
|
||||
let offset = 0;
|
||||
while (offset < tileData.length) {
|
||||
if (offset + 1 > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
const type = readUInt8(tileData, offset);
|
||||
offset += 1;
|
||||
|
||||
@@ -134,10 +134,10 @@ export async function renderTileToPixels(tileData: BinaryData, maxTick?: number)
|
||||
if (offset + 7 > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
const tick = readUInt32BE(tileData, offset) * 60; // Convert back to actual tick
|
||||
offset += 4;
|
||||
|
||||
|
||||
// Skip this chunk if it's beyond our target tick
|
||||
if (maxTick !== undefined && tick > maxTick) {
|
||||
const chunkCoordsByte = readUInt8(tileData, offset);
|
||||
@@ -147,7 +147,7 @@ export async function renderTileToPixels(tileData: BinaryData, maxTick?: number)
|
||||
offset += length; // Skip chunk data
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
const chunkCoordsByte = readUInt8(tileData, offset);
|
||||
offset += 1;
|
||||
const length = readUInt16BE(tileData, offset);
|
||||
@@ -163,7 +163,7 @@ export async function renderTileToPixels(tileData: BinaryData, maxTick?: number)
|
||||
|
||||
try {
|
||||
const chunkPixels = await renderChunkToPixels(chunkData);
|
||||
|
||||
|
||||
// Update current pixel state
|
||||
for (let y = 0; y < 32; y++) {
|
||||
for (let x = 0; x < 32; x++) {
|
||||
@@ -177,29 +177,29 @@ export async function renderTileToPixels(tileData: BinaryData, maxTick?: number)
|
||||
} catch (decompressError) {
|
||||
console.error(`Failed to decompress chunk data: ${decompressError}`);
|
||||
}
|
||||
|
||||
|
||||
offset += length;
|
||||
} else if (type === 2) { // Pixels
|
||||
if (offset + 6 > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
const tick = readUInt32BE(tileData, offset) * 60; // Convert back to actual tick
|
||||
offset += 4;
|
||||
const pixelCount = readUInt16BE(tileData, offset);
|
||||
offset += 2;
|
||||
|
||||
|
||||
const expectedDataLength = pixelCount * 6;
|
||||
if (offset + expectedDataLength > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// Skip this pixel changeset if it's beyond our target tick
|
||||
if (maxTick !== undefined && tick > maxTick) {
|
||||
offset += expectedDataLength;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Apply pixel changes to current state
|
||||
for (let i = 0; i < pixelCount; i++) {
|
||||
const pixelOffset = offset + i * 6;
|
||||
@@ -207,14 +207,14 @@ export async function renderTileToPixels(tileData: BinaryData, maxTick?: number)
|
||||
const y = readUInt8(tileData, pixelOffset + 1);
|
||||
const newColor = readUInt16BE(tileData, pixelOffset + 2);
|
||||
// oldColor at pixelOffset + 4 is not needed for rendering
|
||||
|
||||
|
||||
// Update current pixel state
|
||||
if (x < 256 && y < 256) {
|
||||
const tileIndex = y * 256 + x;
|
||||
currentPixels[tileIndex] = newColor;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
offset += expectedDataLength;
|
||||
} else {
|
||||
break;
|
||||
@@ -230,18 +230,18 @@ export async function renderTileToPixels(tileData: BinaryData, maxTick?: number)
|
||||
*/
|
||||
export function pixelsToRGBA(pixels: Uint16Array): Uint8ClampedArray {
|
||||
const imageData = new Uint8ClampedArray(256 * 256 * 4);
|
||||
|
||||
|
||||
for (let i = 0; i < 256 * 256; i++) {
|
||||
const rgb565Value = pixels[i];
|
||||
const [r, g, b] = rgb565ToRgb888(rgb565Value);
|
||||
const bufferIndex = i * 4;
|
||||
|
||||
|
||||
imageData[bufferIndex] = r;
|
||||
imageData[bufferIndex + 1] = g;
|
||||
imageData[bufferIndex + 2] = b;
|
||||
imageData[bufferIndex + 3] = 255; // Alpha
|
||||
}
|
||||
|
||||
|
||||
return imageData;
|
||||
}
|
||||
|
||||
@@ -261,13 +261,13 @@ export function pixelsToImageData(pixels: Uint16Array): any {
|
||||
*/
|
||||
export function extractAvailableTicks(tileData: BinaryData): number[] {
|
||||
const ticks = new Set<number>();
|
||||
|
||||
|
||||
let offset = 0;
|
||||
while (offset < tileData.length) {
|
||||
if (offset + 1 > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
const type = readUInt8(tileData, offset);
|
||||
offset += 1;
|
||||
|
||||
@@ -275,11 +275,11 @@ export function extractAvailableTicks(tileData: BinaryData): number[] {
|
||||
if (offset + 7 > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
const tick = readUInt32BE(tileData, offset) * 60; // Convert back to actual tick
|
||||
ticks.add(tick);
|
||||
offset += 4;
|
||||
|
||||
|
||||
const chunkCoordsByte = readUInt8(tileData, offset);
|
||||
offset += 1;
|
||||
const length = readUInt16BE(tileData, offset);
|
||||
@@ -289,19 +289,19 @@ export function extractAvailableTicks(tileData: BinaryData): number[] {
|
||||
if (offset + 6 > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
const tick = readUInt32BE(tileData, offset) * 60; // Convert back to actual tick
|
||||
ticks.add(tick);
|
||||
offset += 4;
|
||||
|
||||
|
||||
const pixelCount = readUInt16BE(tileData, offset);
|
||||
offset += 2;
|
||||
|
||||
|
||||
const expectedDataLength = pixelCount * 6;
|
||||
if (offset + expectedDataLength > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
offset += expectedDataLength;
|
||||
} else {
|
||||
break;
|
||||
@@ -355,13 +355,13 @@ export function parseTileData(tileData: BinaryData): ParsedTileData {
|
||||
const chunks: ChunkRecord[] = [];
|
||||
const pixelChanges: PixelChangeRecord[] = [];
|
||||
const ticks = new Set<number>();
|
||||
|
||||
|
||||
let offset = 0;
|
||||
while (offset < tileData.length) {
|
||||
if (offset + 1 > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
const type = readUInt8(tileData, offset);
|
||||
offset += 1;
|
||||
|
||||
@@ -369,11 +369,11 @@ export function parseTileData(tileData: BinaryData): ParsedTileData {
|
||||
if (offset + 7 > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
const tick = readUInt32BE(tileData, offset) * 60; // Convert back to actual tick
|
||||
ticks.add(tick);
|
||||
offset += 4;
|
||||
|
||||
|
||||
const chunkCoordsByte = readUInt8(tileData, offset);
|
||||
offset += 1;
|
||||
const length = readUInt16BE(tileData, offset);
|
||||
@@ -393,24 +393,24 @@ export function parseTileData(tileData: BinaryData): ParsedTileData {
|
||||
chunkY,
|
||||
data
|
||||
});
|
||||
|
||||
|
||||
offset += length;
|
||||
} else if (type === 2) { // Pixels
|
||||
if (offset + 6 > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
const tick = readUInt32BE(tileData, offset) * 60; // Convert back to actual tick
|
||||
ticks.add(tick);
|
||||
offset += 4;
|
||||
const pixelCount = readUInt16BE(tileData, offset);
|
||||
offset += 2;
|
||||
|
||||
|
||||
const expectedDataLength = pixelCount * 6;
|
||||
if (offset + expectedDataLength > tileData.length) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
const changes: PixelChange[] = [];
|
||||
for (let i = 0; i < pixelCount; i++) {
|
||||
const pixelOffset = offset + i * 6;
|
||||
@@ -418,19 +418,19 @@ export function parseTileData(tileData: BinaryData): ParsedTileData {
|
||||
const y = readUInt8(tileData, pixelOffset + 1);
|
||||
const newColor = readUInt16BE(tileData, pixelOffset + 2);
|
||||
const oldColor = readUInt16BE(tileData, pixelOffset + 4);
|
||||
|
||||
|
||||
if (x < 256 && y < 256) {
|
||||
changes.push({ x, y, newColor, oldColor });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (changes.length > 0) {
|
||||
pixelChanges.push({
|
||||
tick,
|
||||
changes
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
offset += expectedDataLength;
|
||||
} else {
|
||||
break;
|
||||
@@ -450,7 +450,7 @@ export function parseTileData(tileData: BinaryData): ParsedTileData {
|
||||
export async function applyChunkToPixels(pixels: Uint16Array, chunkRecord: ChunkRecord): Promise<void> {
|
||||
try {
|
||||
const chunkPixels = await renderChunkToPixels(chunkRecord.data);
|
||||
|
||||
|
||||
// Update tile pixels with chunk data
|
||||
for (let y = 0; y < 32; y++) {
|
||||
for (let x = 0; x < 32; x++) {
|
||||
@@ -505,29 +505,29 @@ export function revertPixelChanges(pixels: Uint16Array, changes: PixelChange[]):
|
||||
* Much more efficient than re-rendering from scratch
|
||||
*/
|
||||
export async function renderTileIncremental(
|
||||
parsedData: ParsedTileData,
|
||||
currentPixels: Uint16Array,
|
||||
fromTick: number,
|
||||
parsedData: ParsedTileData,
|
||||
currentPixels: Uint16Array,
|
||||
fromTick: number,
|
||||
toTick: number
|
||||
): Promise<void> {
|
||||
const { chunks, pixelChanges } = parsedData;
|
||||
|
||||
|
||||
if (fromTick === toTick) {
|
||||
return; // No changes needed
|
||||
}
|
||||
|
||||
|
||||
const forward = toTick > fromTick;
|
||||
|
||||
|
||||
if (forward) {
|
||||
// Apply changes forward from fromTick to toTick
|
||||
|
||||
|
||||
// Apply chunks that occur in this range
|
||||
for (const chunk of chunks) {
|
||||
if (chunk.tick > fromTick && chunk.tick <= toTick) {
|
||||
await applyChunkToPixels(currentPixels, chunk);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Apply pixel changes that occur in this range
|
||||
for (const record of pixelChanges) {
|
||||
if (record.tick > fromTick && record.tick <= toTick) {
|
||||
@@ -536,7 +536,7 @@ export async function renderTileIncremental(
|
||||
}
|
||||
} else {
|
||||
// Revert changes backward from fromTick to toTick
|
||||
|
||||
|
||||
// Revert pixel changes in reverse order
|
||||
for (let i = pixelChanges.length - 1; i >= 0; i--) {
|
||||
const record = pixelChanges[i];
|
||||
@@ -544,7 +544,7 @@ export async function renderTileIncremental(
|
||||
revertPixelChanges(currentPixels, record.changes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Revert chunks (clear to black if we're going before their creation time)
|
||||
for (let i = chunks.length - 1; i >= 0; i--) {
|
||||
const chunk = chunks[i];
|
||||
@@ -553,4 +553,91 @@ export async function renderTileIncremental(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for parsed recipe tile data
|
||||
*/
|
||||
export interface ParsedRecipeTile {
|
||||
ticks: number[]; // ticks present in this tile
|
||||
activeRecipes: Map<string, string>; // posKey -> recipeName
|
||||
dictionary: Map<number, string>; // id -> recipeName
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse binary recipe tile data and return timeline ticks and active recipes at target tick
|
||||
* @param tileX Tile X coordinate for world position calculation
|
||||
* @param tileY Tile Y coordinate for world position calculation
|
||||
* @param buf Binary recipe tile data
|
||||
* @param targetTick Target tick for filtering (null = latest state)
|
||||
* @returns Parsed recipe data with ticks and active recipes
|
||||
*/
|
||||
export function parseRecipeTileBinary(tileX: number, tileY: number, buf: BinaryData, targetTick: number | null): ParsedRecipeTile {
|
||||
const ticks: number[] = [];
|
||||
const dict = new Map<number, string>();
|
||||
const active = new Map<string, string>();
|
||||
const targetSec = targetTick !== null ? Math.floor(targetTick / 60) : Infinity;
|
||||
|
||||
let offset = 0;
|
||||
while (offset < buf.length) {
|
||||
const type = readUInt8(buf, offset);
|
||||
offset += 1;
|
||||
|
||||
if (type === 0) { // Dictionary entry
|
||||
const idx = readUInt16BE(buf, offset);
|
||||
offset += 2;
|
||||
const len = readUInt8(buf, offset);
|
||||
offset += 1;
|
||||
const nameBytes = sliceData(buf, offset, offset + len);
|
||||
offset += len;
|
||||
|
||||
// Convert to string using cross-platform method
|
||||
let recipeName: string;
|
||||
if (isNode && nameBytes instanceof Buffer) {
|
||||
recipeName = nameBytes.toString('utf-8');
|
||||
} else {
|
||||
recipeName = new TextDecoder().decode(nameBytes as Uint8Array);
|
||||
}
|
||||
|
||||
dict.set(idx, recipeName);
|
||||
} else if (type === 1 || type === 2) { // Set recipe (1) or Clear recipe (2)
|
||||
const sec = readUInt32BE(buf, offset);
|
||||
offset += 4;
|
||||
const px = readUInt8(buf, offset);
|
||||
offset += 1;
|
||||
const py = readUInt8(buf, offset);
|
||||
offset += 1;
|
||||
|
||||
let idx: number | undefined;
|
||||
if (type === 1) {
|
||||
idx = readUInt16BE(buf, offset);
|
||||
offset += 2;
|
||||
}
|
||||
|
||||
const tickVal = sec * 60;
|
||||
ticks.push(tickVal);
|
||||
|
||||
if (sec > targetSec) {
|
||||
// Future to target time – we can skip rest if file is chronological
|
||||
// but to be safe just continue without applying
|
||||
continue;
|
||||
}
|
||||
|
||||
const worldX = tileX * 256 + px;
|
||||
const worldY = tileY * 256 + py;
|
||||
const posKey = `${worldX},${worldY}`;
|
||||
|
||||
if (type === 1 && idx !== undefined) {
|
||||
const name = dict.get(idx);
|
||||
if (name) active.set(posKey, name);
|
||||
} else if (type === 2) {
|
||||
active.delete(posKey);
|
||||
}
|
||||
} else {
|
||||
console.warn("Unknown recipe record type", type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return { ticks, activeRecipes: active, dictionary: dict };
|
||||
}
|
||||
Reference in new issue
Block a user