generate spawn chunks smarter

This commit is contained in:
Holly Stubbs 2023-11-08 13:36:22 +00:00
parent de4203d9f0
commit 64709abfb9
1 changed files with 22 additions and 25 deletions

View File

@ -23,6 +23,10 @@ import { HillyGenerator } from "./generators/Hilly";
import { NetherGenerator } from "./generators/Nether"; import { NetherGenerator } from "./generators/Nether";
import { PacketWindowItems } from "./packets/WindowItems"; import { PacketWindowItems } from "./packets/WindowItems";
const chunkFrom = -15;
const chunkTo = 15;
const chunkCount = Math.abs(chunkFrom * 2) * (chunkTo * 2);
export class MinecraftServer { export class MinecraftServer {
private static readonly PROTOCOL_VERSION = 14; private static readonly PROTOCOL_VERSION = 14;
private static readonly TICK_RATE = 20; private static readonly TICK_RATE = 20;
@ -127,49 +131,42 @@ export class MinecraftServer {
} }
Console.printInfo(`Done! Took ${Date.now() - generateStartTime}ms`); Console.printInfo(`Done! Took ${Date.now() - generateStartTime}ms`);
}).bind(this)();*/ }).bind(this)();*/
let chunksGenerated = 0;
(async () => { (async () => {
const generateStartTime = Date.now(); const generateStartTime = Date.now();
let timer = Date.now(); let timer = Date.now();
Console.printInfo("Generating spawn area for DIM0...");
for (let x = -10; x < 10; x++) { await this.worlds.forEach(async world => {
for (let z = -10; z < 10; z++) { let chunksGenerated = 0;
const chunk = await this.overworld.getChunkSafe(x, z); Console.printInfo(`Generating spawn area for DIM${world.dimension}...`);
chunk.forceLoaded = true; for (let x = chunkFrom; x < chunkTo; x++) {
chunksGenerated++; for (let z = chunkFrom; z < chunkTo; z++) {
if (Date.now() - timer >= 1000) { const chunk = await world.getChunkSafe(x, z);
Console.printInfo(`Progress [${chunksGenerated}/400] ${((chunksGenerated / 400) * 100).toFixed(2)}%`); chunk.forceLoaded = true;
timer = Date.now(); chunksGenerated++;
if (Date.now() - timer >= 1000) {
Console.printInfo(`DIM${world.dimension} Progress [${chunksGenerated}/${chunkCount}] ${((chunksGenerated / chunkCount) * 100).toFixed(2)}%`);
timer = Date.now();
}
} }
} }
} });
chunksGenerated = 0;
Console.printInfo("Generating spawn area for DIM-1...");
for (let x = -10; x < 10; x++) {
for (let z = -10; z < 10; z++) {
const chunk = await this.nether.getChunkSafe(x, z);
chunk.forceLoaded = true;
chunksGenerated++;
if (Date.now() - timer >= 1000) {
Console.printInfo(`Progress [${chunksGenerated}/400] ${((chunksGenerated / 400) * 100).toFixed(2)}%`);
timer = Date.now();
}
}
}
Console.printInfo(`Done! Took ${Date.now() - generateStartTime}ms`); Console.printInfo(`Done! Took ${Date.now() - generateStartTime}ms`);
this.initServer(); this.initServer();
}).bind(this)(); }).bind(this)();
const timeUpdate = new PacketTimeUpdate(BigInt(this.tickCounter));
this.serverClock = setInterval(() => { this.serverClock = setInterval(() => {
// Every 1 sec // Every 1 sec
if (this.tickCounter % MinecraftServer.TICK_RATE === 0) { if (this.tickCounter % MinecraftServer.TICK_RATE === 0) {
if (this.clients.length !== 0) { if (this.clients.length !== 0) {
const timePacket = new PacketTimeUpdate(BigInt(this.tickCounter)).writeData(); timeUpdate.time = BigInt(this.tickCounter);
const timePacket = timeUpdate.writeData();
this.clients.forEach(client => { this.clients.forEach(client => {
// Keep the client happy // Keep the client happy
client.send(this.keepalivePacket); client.send(this.keepalivePacket);
client.send(timePacket); client.send(timePacket);
}); });
console.log(this.tickCounter);
} }
// const memoryUsage = process.memoryUsage(); // const memoryUsage = process.memoryUsage();