2023-11-09 21:59:45 +00:00
|
|
|
import { Endian, createWriter } from "bufferstuff";
|
2024-07-08 09:56:03 +01:00
|
|
|
import FunkyArray from "funky-array";
|
2023-04-08 20:52:47 +01:00
|
|
|
import { Chunk } from "./Chunk";
|
2023-04-11 07:47:56 +01:00
|
|
|
import { WorldSaveManager } from "./WorldSaveManager";
|
2023-08-12 22:06:16 +01:00
|
|
|
import { Block } from "./blocks/Block";
|
2023-11-09 16:30:40 +00:00
|
|
|
import { EntityItem } from "./entities/EntityItem";
|
2023-04-08 20:52:47 +01:00
|
|
|
import { IEntity } from "./entities/IEntity";
|
2023-04-09 04:19:10 +01:00
|
|
|
import { Player } from "./entities/Player";
|
2023-04-09 04:47:23 +01:00
|
|
|
//import { FlatGenerator } from "./generators/Flat";
|
2023-04-09 04:19:10 +01:00
|
|
|
import { HillyGenerator } from "./generators/Hilly";
|
2023-04-08 20:52:47 +01:00
|
|
|
import { IGenerator } from "./generators/IGenerator";
|
2023-04-10 21:52:30 +01:00
|
|
|
import { PacketBlockChange } from "./packets/BlockChange";
|
2023-11-05 10:56:57 +00:00
|
|
|
import { PacketDestroyEntity } from "./packets/DestroyEntity";
|
2023-11-09 16:30:40 +00:00
|
|
|
import { PacketPickupSpawn } from "./packets/PickupSpawn";
|
2023-04-13 23:52:13 +01:00
|
|
|
import { QueuedBlockUpdate } from "./queuedUpdateTypes/BlockUpdate";
|
|
|
|
import { IQueuedUpdate } from "./queuedUpdateTypes/IQueuedUpdate";
|
2023-12-18 01:23:52 +00:00
|
|
|
import AABB from "./AABB";
|
2023-04-08 20:52:47 +01:00
|
|
|
|
|
|
|
export class World {
|
2023-04-10 14:42:14 +01:00
|
|
|
public static ENTITY_MAX_SEND_DISTANCE = 50;
|
2023-09-04 02:05:14 +01:00
|
|
|
private static READ_CHUNKS_FROM_DISK = true;
|
2023-04-10 14:42:14 +01:00
|
|
|
|
2023-04-11 07:47:56 +01:00
|
|
|
private readonly saveManager;
|
2023-09-04 01:43:11 +01:00
|
|
|
private readonly chunksOnDisk:Array<number>;
|
2023-04-11 07:47:56 +01:00
|
|
|
|
2023-04-08 20:52:47 +01:00
|
|
|
public chunks:FunkyArray<number, Chunk>;
|
|
|
|
public entites:FunkyArray<number, IEntity>;
|
2023-04-10 14:42:14 +01:00
|
|
|
public players:FunkyArray<number, Player>;
|
2023-12-18 01:23:52 +00:00
|
|
|
public playerHitboxes:FunkyArray<number, AABB>;
|
2023-04-08 20:52:47 +01:00
|
|
|
|
2023-04-13 23:52:13 +01:00
|
|
|
public queuedChunkBlocks:Array<IQueuedUpdate>;
|
|
|
|
public queuedUpdates:Array<IQueuedUpdate>;
|
2023-04-08 20:52:47 +01:00
|
|
|
public generator:IGenerator;
|
|
|
|
|
2023-09-04 01:43:11 +01:00
|
|
|
public readonly dimension:number;
|
|
|
|
|
|
|
|
public constructor(saveManager:WorldSaveManager, dimension:number, seed:number, generator:IGenerator) {
|
|
|
|
this.dimension = dimension;
|
2023-04-11 07:47:56 +01:00
|
|
|
this.saveManager = saveManager;
|
2023-09-04 01:43:11 +01:00
|
|
|
this.chunksOnDisk = this.saveManager.chunksOnDisk.get(dimension) ?? new Array<number>;
|
2023-04-11 07:47:56 +01:00
|
|
|
|
2023-04-08 20:52:47 +01:00
|
|
|
this.chunks = new FunkyArray<number, Chunk>();
|
|
|
|
this.entites = new FunkyArray<number, IEntity>();
|
2023-04-10 14:42:14 +01:00
|
|
|
this.players = new FunkyArray<number, Player>();
|
2023-12-18 01:23:52 +00:00
|
|
|
this.playerHitboxes = new FunkyArray<number, AABB>();
|
2023-04-13 23:52:13 +01:00
|
|
|
this.queuedChunkBlocks = new Array<IQueuedUpdate>();
|
|
|
|
this.queuedUpdates = new Array<IQueuedUpdate>();
|
2023-09-04 01:43:11 +01:00
|
|
|
this.generator = generator;
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public addEntity(entity:IEntity) {
|
|
|
|
this.entites.set(entity.entityId, entity);
|
2023-12-18 01:23:52 +00:00
|
|
|
this.playerHitboxes.set(entity.entityId, entity.entityAABB);
|
2023-04-10 14:42:14 +01:00
|
|
|
if (entity instanceof Player) {
|
|
|
|
this.players.set(entity.entityId, entity);
|
2023-11-09 16:30:40 +00:00
|
|
|
} else if (entity instanceof EntityItem) {
|
|
|
|
const packet = new PacketPickupSpawn(entity.entityId, entity.itemStack.itemID, entity.itemStack.size, entity.itemStack.damage, Math.round(entity.position.x * 32), Math.round(entity.position.y * 32), Math.round(entity.position.z * 32), 0, 0, 0).writeData();
|
|
|
|
entity.sendToNearby(packet);
|
2023-04-10 14:42:14 +01:00
|
|
|
}
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
2023-04-09 04:19:10 +01:00
|
|
|
// TODO: getChunkByCoordPair failed in here during removeEntity, figure out why.
|
|
|
|
public removeEntity(entity:IEntity) {
|
|
|
|
if (entity instanceof Player) {
|
2023-04-09 04:47:23 +01:00
|
|
|
for (const coordPair of entity.loadedChunks) {
|
2023-05-02 08:50:49 +01:00
|
|
|
if (this.chunkExists(coordPair)) {
|
|
|
|
const chunk = this.getChunkByCoordPair(coordPair);
|
|
|
|
chunk.playersInChunk.remove(entity.entityId);
|
2023-04-09 04:19:10 +01:00
|
|
|
|
2023-05-02 08:50:49 +01:00
|
|
|
if (!chunk.forceLoaded && chunk.playersInChunk.length === 0) {
|
|
|
|
this.unloadChunk(coordPair);
|
|
|
|
}
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
}
|
2023-09-04 23:42:38 +01:00
|
|
|
// Clear player chunk list (they may be switching dimensions)
|
|
|
|
entity.loadedChunks = new Array<number>();
|
|
|
|
entity.justUnloaded = new Array<number>();
|
|
|
|
|
2023-12-18 01:23:52 +00:00
|
|
|
this.playerHitboxes.remove(entity.entityId);
|
2023-04-10 14:42:14 +01:00
|
|
|
this.players.remove(entity.entityId);
|
2023-11-09 21:59:45 +00:00
|
|
|
|
2023-11-13 00:00:17 +00:00
|
|
|
if (!entity.isDead) {
|
|
|
|
const writer = createWriter(Endian.BE);
|
|
|
|
entity.toSave(writer);
|
2023-11-09 21:59:45 +00:00
|
|
|
|
2023-11-13 00:00:17 +00:00
|
|
|
this.saveManager.writePlayerSaveToDisk(entity.username, writer);
|
|
|
|
}
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.entites.remove(entity.entityId);
|
2023-11-12 23:59:54 +00:00
|
|
|
this.sendToNearbyClients(entity, new PacketDestroyEntity(entity.entityId).writeData());
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
|
2023-05-02 08:50:49 +01:00
|
|
|
public chunkExists(coordPairOrX:number, chunkZ?:number) {
|
|
|
|
if (typeof(coordPairOrX) === "number" && typeof(chunkZ) === "number") {
|
|
|
|
return this.chunks.has(Chunk.CreateCoordPair(coordPairOrX, chunkZ));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.chunks.has(coordPairOrX);
|
|
|
|
}
|
|
|
|
|
2023-06-26 16:16:55 +01:00
|
|
|
public getChunk(x:number, z:number) {
|
2023-04-09 04:19:10 +01:00
|
|
|
const coordPair = Chunk.CreateCoordPair(x, z);
|
|
|
|
const existingChunk = this.chunks.get(coordPair);
|
|
|
|
if (!(existingChunk instanceof Chunk)) {
|
2023-04-11 07:47:56 +01:00
|
|
|
throw new Error(`BADLOOKUP: Chunk [${x}, ${z}] does not exist.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return existingChunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getChunkSafe(x:number, z:number) {
|
2023-06-26 09:53:45 +01:00
|
|
|
return new Promise<Chunk>((resolve) => {
|
2023-04-11 07:47:56 +01:00
|
|
|
const coordPair = Chunk.CreateCoordPair(x, z);
|
|
|
|
const existingChunk = this.chunks.get(coordPair);
|
|
|
|
if (!(existingChunk instanceof Chunk)) {
|
2023-09-04 01:43:11 +01:00
|
|
|
if (World.READ_CHUNKS_FROM_DISK && this.chunksOnDisk.includes(coordPair)) {
|
2023-04-11 07:47:56 +01:00
|
|
|
return this.saveManager.readChunkFromDisk(this, x, z)
|
2023-08-20 16:31:23 +01:00
|
|
|
.then(chunk => resolve(this.chunks.set(coordPair, chunk)));
|
2023-04-11 07:47:56 +01:00
|
|
|
} else {
|
2023-04-13 10:46:09 +01:00
|
|
|
resolve(this.chunks.set(coordPair, new Chunk(this, x, z, true)));
|
2023-06-19 18:29:16 +01:00
|
|
|
if (World.READ_CHUNKS_FROM_DISK) {
|
|
|
|
this.saveManager.writeChunkToDisk(this.getChunk(x, z));
|
|
|
|
}
|
2023-04-13 10:46:09 +01:00
|
|
|
return;
|
2023-04-11 07:47:56 +01:00
|
|
|
}
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
|
2023-04-11 07:47:56 +01:00
|
|
|
resolve(existingChunk);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public getChunkByCoordPair(coordPair:number) {
|
|
|
|
const existingChunk = this.chunks.get(coordPair);
|
|
|
|
if (!(existingChunk instanceof Chunk)) {
|
|
|
|
throw new Error(`BADLOOKUP: Chunk ${coordPair} does not exist.`);
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
2023-04-09 04:19:10 +01:00
|
|
|
return existingChunk;
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
public getBlockId(x:number, y:number, z:number) {
|
|
|
|
const chunkX = x >> 4,
|
|
|
|
chunkZ = z >> 4;
|
|
|
|
|
2023-04-13 00:26:01 +01:00
|
|
|
return this.getChunk(chunkX, chunkZ).getBlockId(x & 0xf, y, z & 0xf);
|
|
|
|
}
|
|
|
|
|
2023-08-20 02:10:49 +01:00
|
|
|
public getChunkBlockId(chunk:Chunk, x:number, y:number, z:number) {
|
|
|
|
return chunk.getBlockId(x & 0xf, y, z & 0xf);
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:26:01 +01:00
|
|
|
public getBlockMetadata(x:number, y:number, z:number) {
|
|
|
|
const chunkX = x >> 4,
|
|
|
|
chunkZ = z >> 4;
|
|
|
|
|
|
|
|
return this.getChunk(chunkX, chunkZ).getBlockMetadata(x & 0xf, y, z & 0xf);
|
2023-04-10 21:52:30 +01:00
|
|
|
}
|
|
|
|
|
2023-08-20 02:10:49 +01:00
|
|
|
public getChunkBlockMetadata(chunk:Chunk, x:number, y:number, z:number) {
|
|
|
|
return chunk.getBlockMetadata(x & 0xf, y, z & 0xf);
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:52:30 +01:00
|
|
|
public setBlock(blockId:number, x:number, y:number, z:number, doBlockUpdate?:boolean) {
|
|
|
|
const chunkX = x >> 4,
|
|
|
|
chunkZ = z >> 4;
|
|
|
|
|
|
|
|
const chunk = this.getChunk(chunkX, chunkZ);
|
2023-04-13 00:26:01 +01:00
|
|
|
chunk.setBlockWithMetadata(blockId, 0, x & 0xf, y, z & 0xf);
|
|
|
|
|
|
|
|
if (doBlockUpdate) {
|
|
|
|
const blockUpdatePacket = new PacketBlockChange(x, y, z, blockId, 0).writeData();
|
|
|
|
// Send block update to all players that have this chunk loaded
|
|
|
|
chunk.playersInChunk.forEach(player => {
|
|
|
|
player.mpClient?.send(blockUpdatePacket);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public setBlockWithMetadata(blockId:number, metadata:number, x:number, y:number, z:number, doBlockUpdate?:boolean) {
|
|
|
|
const chunkX = x >> 4,
|
|
|
|
chunkZ = z >> 4;
|
|
|
|
|
|
|
|
const chunk = this.getChunk(chunkX, chunkZ);
|
|
|
|
chunk.setBlockWithMetadata(blockId, metadata, x & 0xf, y, z & 0xf);
|
2023-04-10 21:52:30 +01:00
|
|
|
|
|
|
|
if (doBlockUpdate) {
|
2023-04-13 00:26:01 +01:00
|
|
|
const blockUpdatePacket = new PacketBlockChange(x, y, z, blockId, metadata).writeData(); // TODO: Handle metadata
|
2023-04-10 21:52:30 +01:00
|
|
|
// Send block update to all players that have this chunk loaded
|
|
|
|
chunk.playersInChunk.forEach(player => {
|
|
|
|
player.mpClient?.send(blockUpdatePacket);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-12 22:06:16 +01:00
|
|
|
public setBlockWithNotify(x:number, y:number, z:number, blockId:number) {
|
|
|
|
this.setBlock(blockId, x, y, z, true);
|
|
|
|
this.notifyNeighborBlocksOfChange(x, y, z, blockId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public setBlockAndMetadataWithNotify(x:number, y:number, z:number, blockId:number, metadata:number) {
|
|
|
|
this.setBlockWithMetadata(blockId, metadata, x, y, z, true);
|
|
|
|
this.notifyNeighborBlocksOfChange(x, y, z, blockId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public notifyNeighborBlocksOfChange(x:number, y:number, z:number, blockId:number) {
|
|
|
|
this.notifyNeighborBlockOfChange(x - 1, y, z, blockId);
|
|
|
|
this.notifyNeighborBlockOfChange(x + 1, y, z, blockId);
|
|
|
|
this.notifyNeighborBlockOfChange(x, y - 1, z, blockId);
|
|
|
|
this.notifyNeighborBlockOfChange(x, y + 1, z, blockId);
|
|
|
|
this.notifyNeighborBlockOfChange(x, y, z - 1, blockId);
|
|
|
|
this.notifyNeighborBlockOfChange(x, y, z + 1, blockId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private notifyNeighborBlockOfChange(x:number, y:number, z:number, blockId:number) {
|
|
|
|
const block = Block.blocks[this.getBlockId(x, y, z)];
|
|
|
|
if (block != null && block.blockId !== 0) {
|
|
|
|
block.neighborBlockChange(this, x, y, z, block.blockId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 14:42:14 +01:00
|
|
|
public sendToNearbyClients(sentFrom:IEntity, buffer:Buffer) {
|
|
|
|
this.players.forEach(player => {
|
|
|
|
if (sentFrom.entityId !== player.entityId && Math.abs(sentFrom.distanceTo(player)) < World.ENTITY_MAX_SEND_DISTANCE) {
|
|
|
|
player.mpClient?.send(buffer);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-13 23:52:13 +01:00
|
|
|
public sendToNearbyAllNearbyClients(sentFrom:IEntity, buffer:Buffer) {
|
|
|
|
this.players.forEach(player => {
|
|
|
|
if (Math.abs(sentFrom.distanceTo(player)) < World.ENTITY_MAX_SEND_DISTANCE) {
|
|
|
|
player.mpClient?.send(buffer);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-11 07:47:56 +01:00
|
|
|
public async unloadChunk(coordPair:number) {
|
|
|
|
const chunk = this.getChunkByCoordPair(coordPair);
|
|
|
|
if (!chunk.savingToDisk) {
|
|
|
|
chunk.savingToDisk = true;
|
2023-04-09 04:19:10 +01:00
|
|
|
|
2023-04-11 07:47:56 +01:00
|
|
|
await this.saveManager.writeChunkToDisk(chunk);
|
2023-04-09 04:19:10 +01:00
|
|
|
|
2023-04-11 07:47:56 +01:00
|
|
|
if (chunk.playersInChunk.length === 0) {
|
|
|
|
this.chunks.remove(coordPair);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A player loaded the chunk while we were, flushing to disk.
|
|
|
|
// Keep it loaded.
|
|
|
|
chunk.savingToDisk = false;
|
|
|
|
}
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
2023-04-09 04:47:23 +01:00
|
|
|
public tick() {
|
2023-04-13 23:52:13 +01:00
|
|
|
if (this.queuedUpdates.length > 0) {
|
|
|
|
for (let i = this.queuedUpdates.length - 1; i >= 0; i--) {
|
|
|
|
const update = this.queuedUpdates[i];
|
|
|
|
if (update instanceof QueuedBlockUpdate) {
|
|
|
|
if (this.chunks.keys.includes(update.coordPair)) {
|
|
|
|
this.queuedUpdates.splice(i, 1);
|
|
|
|
const thatChunk = this.getChunkByCoordPair(update.coordPair);
|
|
|
|
thatChunk.setBlockWithMetadata(update.blockId, update.metadata, update.x, update.y, update.z);
|
|
|
|
if (thatChunk.playersInChunk.length > 0) {
|
|
|
|
const blockUpdate = new PacketBlockChange((thatChunk.x << 4) + update.x, update.y, (thatChunk.z << 4) + update.z, update.blockId, update.metadata).writeData()
|
|
|
|
thatChunk.playersInChunk.forEach(player => {
|
|
|
|
player.mpClient?.send(blockUpdate);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:19:10 +01:00
|
|
|
this.entites.forEach(entity => {
|
2023-04-10 14:42:14 +01:00
|
|
|
entity.onTick();
|
|
|
|
|
2023-04-09 04:19:10 +01:00
|
|
|
if (entity instanceof Player) {
|
|
|
|
if (entity.justUnloaded.length > 0) {
|
2023-04-09 04:47:23 +01:00
|
|
|
for (const coordPair of entity.justUnloaded) {
|
2023-12-18 01:23:52 +00:00
|
|
|
if (this.chunks.get(coordPair) != undefined) {
|
2023-05-02 08:50:49 +01:00
|
|
|
const chunkToUnload = this.getChunkByCoordPair(coordPair);
|
|
|
|
chunkToUnload.playersInChunk.remove(entity.entityId);
|
|
|
|
if (!chunkToUnload.forceLoaded && chunkToUnload.playersInChunk.length === 0) {
|
|
|
|
this.unloadChunk(coordPair);
|
|
|
|
}
|
2023-04-09 04:19:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entity.justUnloaded = new Array<number>();
|
|
|
|
}
|
|
|
|
}
|
2023-12-06 00:04:57 +00:00
|
|
|
|
|
|
|
if (entity.markedForDisposal) {
|
|
|
|
this.removeEntity(entity);
|
|
|
|
}
|
2023-04-09 04:19:10 +01:00
|
|
|
})
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
}
|