improve speed of tree block queueing

This commit is contained in:
Holly Stubbs 2021-10-21 11:38:19 +01:00
parent 3ece5a575d
commit 65852606b3
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
2 changed files with 7 additions and 9 deletions

View file

@ -43,15 +43,14 @@ module.exports = class {
break; break;
case "generate": case "generate":
const startTime = new Date().getTime();
this.chunks[data[2]][data[3]] = data[1]; this.chunks[data[2]][data[3]] = data[1];
this.toRemove.push(data[4]); this.toRemove.push(data[4]);
const treeBlocksRef = data[5]; const treeBlocksRef = data[5];
treeBlocksRef.forEach((block) => { treeBlocksRef.forEach((block) => {
this.setBlock(block[0], block[1], block[2], block[3]); this.setBlock(block[0], block[1], block[2], block[3], 0, false);
}); });
this.queuedBlockUpdates.regenerateIterableArray();
this.threadPool[myID][0] = false; this.threadPool[myID][0] = false;
console.log(`Trees fin took ${new Date().getTime() - startTime}ms`);
break; break;
} }
}); });
@ -113,7 +112,7 @@ module.exports = class {
this.workPool.add([false, ["chunk", [chunkX, chunkZ, this.chunks[chunkX][chunkZ]], user.id, null]]); this.workPool.add([false, ["chunk", [chunkX, chunkZ, this.chunks[chunkX][chunkZ]], user.id, null]]);
} }
setBlock(x = 0, y = 0, z = 0, id = 0, metadata = 0) { setBlock(x = 0, y = 0, z = 0, id = 0, metadata = 0, regenerate = true) {
if (y < 0 || y > 127) return console.error("Tried to set a block outside of the world!"); if (y < 0 || y > 127) return console.error("Tried to set a block outside of the world!");
const chunkX = x >> 4; const chunkX = x >> 4;
@ -126,6 +125,6 @@ module.exports = class {
if (this.chunks[chunkX][chunkZ] != null) if (this.chunks[chunkX][chunkZ] != null)
if (this.chunks[chunkX][chunkZ][y][blockX][blockZ] == id) return; if (this.chunks[chunkX][chunkZ][y][blockX][blockZ] == id) return;
this.queuedBlockUpdates.add([chunkX, chunkZ, y, blockX, blockZ, id, metadata]); this.queuedBlockUpdates.add([chunkX, chunkZ, y, blockX, blockZ, id, metadata], regenerate);
} }
} }

View file

@ -90,11 +90,10 @@ module.exports.init = function(config) {
for (let i = 0; i < Math.min(global.chunkManager.queuedBlockUpdates.getLength(), 128); i++) { for (let i = 0; i < Math.min(global.chunkManager.queuedBlockUpdates.getLength(), 128); i++) {
const chunkUpdateKey = global.chunkManager.queuedBlockUpdates.itemKeys[i]; const chunkUpdateKey = global.chunkManager.queuedBlockUpdates.itemKeys[i];
const chunkUpdate = global.chunkManager.queuedBlockUpdates.items[chunkUpdateKey]; const chunkUpdate = global.chunkManager.queuedBlockUpdates.items[chunkUpdateKey];
// Don't update if chunk is nonexistant
// TODO: Remove this once infinite terrain is in :) // TODO: Remove this once infinite terrain is in :)
if (chunkUpdate[0] < -10 || chunkUpdate[0] > 10 || chunkUpdate[1] < -10 || chunkUpdate[1] > 10) { if (chunkUpdate[0] < -3 || chunkUpdate[0] > 4 || chunkUpdate[1] < -3 || chunkUpdate[1] > 4) {
itemsToRemove.push(chunkUpdateKey); itemsToRemove.push(chunkUpdateKey, false);
continue; continue;
} }