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
|
|
|
|
2021-10-19 08:20: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":
|
2021-10-19 08:20:00 +01:00
|
|
|
const startTime = new Date().getTime();
|
|
|
|
parentPort.postMessage([data[0], generateChunk(data[1], data[2]), data[1], data[2], data[3]]);
|
|
|
|
console.log(`Chunk (${data[1]}, ${data[2]}) took ${new Date().getTime() - startTime}ms to generate`);
|
2021-08-20 22:42:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-19 08:20:00 +01:00
|
|
|
function generateChunk(x = 0, z = 0) {
|
|
|
|
return GeneratorPerlin(x, z);
|
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]); // The block
|
|
|
|
if (blockMeta) {
|
|
|
|
metadata.writeByte(0x00); // TODO: Metadata
|
|
|
|
// Light level 15 for 2 blocks (1111 1111)
|
|
|
|
lighting.writeUByte(0xFF); // TODO: Lighting (Client seems to do it's own so it's not top priority)
|
|
|
|
}
|
|
|
|
// 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;
|
|
|
|
}
|