2023-04-09 04:19:10 +01:00
|
|
|
import { FunkyArray } from "../funkyArray";
|
2023-04-11 07:47:56 +01:00
|
|
|
import { NibbleArray } from "../nibbleArray";
|
2023-04-09 04:19:10 +01:00
|
|
|
import { Player } from "./entities/Player";
|
2023-04-13 23:52:13 +01:00
|
|
|
import { QueuedBlockUpdate } from "./queuedUpdateTypes/BlockUpdate";
|
2023-04-08 20:52:47 +01:00
|
|
|
import { World } from "./World";
|
|
|
|
|
|
|
|
export class Chunk {
|
|
|
|
private readonly MAX_HEIGHT:number = 128;
|
2023-04-13 23:52:13 +01:00
|
|
|
public readonly world:World;
|
2023-04-09 04:19:10 +01:00
|
|
|
public readonly x:number;
|
|
|
|
public readonly z:number;
|
|
|
|
public readonly playersInChunk:FunkyArray<number, Player>;
|
2023-04-08 20:52:47 +01:00
|
|
|
|
2023-04-11 07:47:56 +01:00
|
|
|
public savingToDisk:boolean = false;
|
|
|
|
public forceLoaded:boolean = false;
|
|
|
|
|
2023-04-08 20:52:47 +01:00
|
|
|
private blocks:Uint8Array;
|
2023-04-11 07:47:56 +01:00
|
|
|
private metadata:NibbleArray;
|
2023-04-08 20:52:47 +01:00
|
|
|
|
|
|
|
public static CreateCoordPair(x:number, z:number) {
|
|
|
|
return (x >= 0 ? 0 : 2147483648) | (x & 0x7fff) << 16 | (z >= 0 ? 0 : 0x8000) | z & 0x7fff;
|
|
|
|
}
|
|
|
|
|
2023-04-13 00:26:01 +01:00
|
|
|
public constructor(world:World, x:number, z:number, generateOrBlockData?:boolean|Uint8Array, metadata?:Uint8Array) {
|
2023-04-08 20:52:47 +01:00
|
|
|
this.world = world;
|
|
|
|
this.x = x;
|
|
|
|
this.z = z;
|
2023-04-09 04:19:10 +01:00
|
|
|
this.playersInChunk = new FunkyArray<number, Player>();
|
|
|
|
|
2023-04-13 00:26:01 +01:00
|
|
|
if (generateOrBlockData instanceof Uint8Array && metadata instanceof Uint8Array) {
|
2023-04-11 07:47:56 +01:00
|
|
|
this.blocks = new Uint8Array(generateOrBlockData);
|
|
|
|
this.metadata = new NibbleArray(metadata);
|
|
|
|
} else {
|
|
|
|
this.blocks = new Uint8Array(16 * 16 * this.MAX_HEIGHT);
|
|
|
|
this.metadata = new NibbleArray(16 * 16 * this.MAX_HEIGHT);
|
2023-04-08 20:52:47 +01:00
|
|
|
|
2023-04-13 00:26:01 +01:00
|
|
|
if (typeof(generateOrBlockData) === "boolean" && generateOrBlockData) {
|
2023-04-11 07:47:56 +01:00
|
|
|
this.world.generator.generate(this);
|
|
|
|
}
|
|
|
|
}
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
2023-04-13 23:52:13 +01:00
|
|
|
public calculateLighting() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public queueBlockUpdateForOuterChunkBlock(blockId:number, metadata:number, x:number, y:number, z:number) {
|
|
|
|
const cPair = Chunk.CreateCoordPair(this.x + (x >> 4), this.z + (z >> 4));
|
|
|
|
if (this.world.chunks.keys.includes(cPair)) {
|
|
|
|
this.world.queuedUpdates.push(new QueuedBlockUpdate(cPair, x & 0xf, y, z & 0xf, blockId, metadata));
|
|
|
|
} else {
|
|
|
|
this.world.queuedChunkBlocks.push(new QueuedBlockUpdate(cPair, x & 0xf, y, z & 0xf, blockId, metadata));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 20:52:47 +01:00
|
|
|
public setBlock(blockId:number, x:number, y:number, z:number) {
|
2023-04-11 07:47:56 +01:00
|
|
|
if (x < 0 || x > 15 || y < 0 || y > 127 || z < 0 || z > 15) {
|
2023-04-13 23:52:13 +01:00
|
|
|
this.queueBlockUpdateForOuterChunkBlock(blockId, 0, x, y, z);
|
2023-04-11 07:47:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-04-13 00:26:01 +01:00
|
|
|
|
2023-04-08 20:52:47 +01:00
|
|
|
this.blocks[x << 11 | z << 7 | y] = blockId;
|
|
|
|
}
|
|
|
|
|
2023-04-11 07:47:56 +01:00
|
|
|
public setBlockWithMetadata(blockId:number, metadata:number, x:number, y:number, z:number) {
|
|
|
|
if (x < 0 || x > 15 || y < 0 || y > 127 || z < 0 || z > 15) {
|
2023-04-13 23:52:13 +01:00
|
|
|
this.queueBlockUpdateForOuterChunkBlock(blockId, metadata, x, y, z);
|
2023-04-11 07:47:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
x = x << 11 | z << 7 | y;
|
|
|
|
|
|
|
|
this.blocks[x] = blockId;
|
|
|
|
this.metadata.set(x, metadata);
|
|
|
|
}
|
|
|
|
|
2023-04-08 20:52:47 +01:00
|
|
|
public getBlockId(x:number, y:number, z:number) {
|
|
|
|
return this.blocks[x << 11 | z << 7 | y];
|
|
|
|
}
|
|
|
|
|
2023-04-11 07:47:56 +01:00
|
|
|
public getBlockMetadata(x:number, y:number, z:number) {
|
|
|
|
return this.metadata.get(x << 11 | z << 7 | y);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getMetadataBuffer() {
|
|
|
|
return this.metadata.toBuffer();
|
|
|
|
}
|
|
|
|
|
2023-04-08 20:52:47 +01:00
|
|
|
public getData() {
|
|
|
|
return this.blocks;
|
|
|
|
}
|
|
|
|
}
|