mc-beta-server/server/Workers/ChunkWorker.js

82 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2021-08-24 15:29:46 +01:00
/*
===========- ChunkWorker.js -===========
Created by Holly (tgpethan) (c) 2021
Licenced under MIT
========================================
*/
2021-08-27 00:32:40 +01:00
const { parentPort } = require('worker_threads'),
{ deflateSync } = require("zlib");
2021-08-20 22:42:00 +01:00
const GeneratorFlat = require("../Generators/GeneratorFlat.js");
const GeneratorPerlin = require("../Generators/GeneratorPerlin.js");
2021-08-20 22:42:00 +01:00
const bufferStuff = require("../bufferStuff.js");
let busyInterval = null;
parentPort.on("message", (data) => {
// This stops the thread from stopping :)
if (busyInterval == null) busyInterval = setInterval(() => {}, 86400000); // Runs once a day
switch (data[0]) {
case "chunk":
parentPort.postMessage([data[0], doChunk(data[1]), data[2]]);
break;
case "generate":
2023-04-07 21:24:42 +01:00
const startTime = Date.now();
const chunkData = generateChunk(data[1], data[2], data[4]);
parentPort.postMessage([data[0], chunkData[0], data[1], data[2], data[3], chunkData[1]]);
2023-04-07 21:24:42 +01:00
console.log(`Chunk took ${Date.now() - startTime}ms`);
2021-08-20 22:42:00 +01:00
break;
}
});
function generateChunk(x = 0, z = 0, seed = 0) {
return GeneratorPerlin(x, z, seed);
2021-08-20 22:42:00 +01:00
}
function doChunk(chunk) {
2021-08-27 00:15:02 +01:00
const writer = new bufferStuff.Writer(18);
2021-08-20 22:42:00 +01:00
writer.writeByte(0x33); // Chunk
writer.writeInt(chunk[0] << 4); // Chunk X
writer.writeShort(0 << 7); // Chunk Y
writer.writeInt(chunk[1] << 4); // Chunk Z
writer.writeByte(15); // Size X
writer.writeByte(127); // Size Y
writer.writeByte(15); // Size Z
2021-08-21 08:22:23 +01:00
// pre-alloc since doing an alloc 98,304 times takes a while yknow.
2021-08-20 22:42:00 +01:00
const blocks = new bufferStuff.Writer(32768);
const metadata = new bufferStuff.Writer(32768);
const lighting = new bufferStuff.Writer(32768);
let blockMeta = false;
for (let x = 0; x < 16; x++) {
for (let z = 0; z < 16; z++) {
for (let y = 0; y < 128; y++) {
blocks.writeByte(chunk[2][y][x][z][0]);
2021-08-20 22:42:00 +01:00
if (blockMeta) {
metadata.writeNibble(chunk[2][y - 1][x][z][1], chunk[2][y][x][z][1]); // NOTE: This is sorta jank but it does work
2021-08-20 22:42:00 +01:00
// Light level 15 for 2 blocks (1111 1111)
lighting.writeNibble(15, 15); // TODO: Lighting (Client seems to do it's own (when a block update happens) so it's not top priority)
2021-08-20 22:42:00 +01:00
}
// Hack for nibble stuff
blockMeta = !blockMeta;
}
}
}
// These are hacks for the nibbles
blocks.writeBuffer(metadata.buffer);
blocks.writeBuffer(lighting.buffer); // Block lighting
//blocks.writeBuffer(lighting.buffer); // Sky lighting (Looks like this isn't needed???)
// We're on another thread we don't care if we halt
const deflated = deflateSync(blocks.buffer);
writer.writeInt(deflated.length); // Compressed Size
writer.writeBuffer(deflated); // Compressed chunk data
return writer.buffer;
}