From 8fc089834f5010be4a5fdc927bd188f0935fa523 Mon Sep 17 00:00:00 2001 From: Holly Date: Tue, 19 Oct 2021 08:20:00 +0100 Subject: [PATCH] change ChunkWorker to support different terrain generators --- server/Workers/ChunkWorker.js | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/server/Workers/ChunkWorker.js b/server/Workers/ChunkWorker.js index 513b77c..fad6beb 100644 --- a/server/Workers/ChunkWorker.js +++ b/server/Workers/ChunkWorker.js @@ -8,6 +8,9 @@ const { parentPort } = require('worker_threads'), { deflateSync } = require("zlib"); +const GeneratorFlat = require("../Generators/GeneratorFlat.js"); +const GeneratorPerlin = require("../Generators/GeneratorPerlin.js"); + const bufferStuff = require("../bufferStuff.js"); let busyInterval = null; @@ -22,30 +25,15 @@ parentPort.on("message", (data) => { break; case "generate": - parentPort.postMessage([data[0], generateChunk(), data[1], data[2], data[3]]); + 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`); break; } }); -function generateChunk() { - let chunk = {}; - for (let y = 0; y < 128; y++) { - chunk[y] = {}; - for (let x = 0; x < 16; x++) { - chunk[y][x] = {}; - for (let z = 0; z < 16; z++) { - if (y == 64) { - chunk[y][x][z] = 2; - } - else if (y == 63 || y == 62) chunk[y][x][z] = 3; - else if (y == 0) chunk[y][x][z] = 7; - else if (y < 62) chunk[y][x][z] = 1; - else chunk[y][x][z] = 0; - } - } - } - - return chunk; +function generateChunk(x = 0, z = 0) { + return GeneratorPerlin(x, z); } function doChunk(chunk) {