Cache ticking blocks

This commit is contained in:
2026-10-12 19:37:04 -03:00
parent c5ffc23bcd
commit 02cbeda0a0
9 changed files with 195 additions and 56 deletions
+67 -18
View File
@@ -46,6 +46,44 @@ export interface Tile {
mod_data?: unknown;
// the item it shows, sent to clients, see BlockRegistry.item_display. only set through set_tile_item
item?: ItemData;
// false when its components turned ticking off for it, see GameServer.set_tile_ticking
ticking?: false;
}
// some of the tiles, kept by chunk so a chunk's are found without going through every tile in the world
class ChunkTiles {
#chunks = new Map<number, Set<Tile>>();
get empty() {
return this.#chunks.size === 0;
}
// adds or removes it
set(tile: Tile, has: boolean) {
const key = tile_chunk_key(tile);
let tiles = this.#chunks.get(key);
if (has) {
if (!tiles) {
tiles = new Set();
this.#chunks.set(key, tiles);
}
tiles.add(tile);
} else if (tiles?.delete(tile) && tiles.size === 0) {
this.#chunks.delete(key);
}
}
has(tile: Tile) {
return this.#chunks.get(tile_chunk_key(tile))?.has(tile) ?? false;
}
in_chunk(chunk_x: number, chunk_z: number): Iterable<Tile> {
return this.#chunks.get(chunk_key(chunk_x, chunk_z)) ?? [];
}
}
function tile_chunk_key(tile: Tile) {
return chunk_key(Math.floor(tile.x / CHUNK_SIZE), Math.floor(tile.z / CHUNK_SIZE));
}
// a chunk as it is now, loaded from the save or generated
@@ -94,8 +132,10 @@ export class ServerWorld {
// unsaved chunks #chunks forgot, ready to save, by chunk_key
#evicted = new Map<number, SavedChunk>();
tiles = new Map<string, Tile>();
// the tiles showing an item, by chunk_key, to send with their chunk
#item_tiles = new Map<number, Set<Tile>>();
// the tiles showing an item, to send with their chunk
#item_tiles = new ChunkTiles();
// the tiles that tick, which the game decides, see GameServer.set_tile_ticking
#ticking_tiles = new ChunkTiles();
// ticks since 06:00 of day 1, see common/time.ts
time = 0;
@@ -480,33 +520,42 @@ export class ServerWorld {
remove_tile(x: number, y: number, z: number) {
const tile = this.get_tile(x, y, z);
if (tile?.item) this.set_tile_item(tile, undefined);
if (!tile) return;
if (tile.item) this.set_tile_item(tile, undefined);
this.#ticking_tiles.set(tile, false);
this.tiles.delete(position_key(x, y, z));
this.dirty = true;
}
set_tile_item(tile: Tile, item: ItemData | undefined) {
const key = chunk_key(Math.floor(tile.x / CHUNK_SIZE), Math.floor(tile.z / CHUNK_SIZE));
let tiles = this.#item_tiles.get(key);
if (item) {
tile.item = item;
if (!tiles) {
tiles = new Set();
this.#item_tiles.set(key, tiles);
}
tiles.add(tile);
} else {
delete tile.item;
tiles?.delete(tile);
if (tiles?.size === 0) this.#item_tiles.delete(key);
}
if (item) tile.item = item;
else delete tile.item;
this.#item_tiles.set(tile, item !== undefined);
this.dirty = true;
this.on_tile_item_change?.(tile.x, tile.y, tile.z, item);
}
// the tiles in a chunk that show an item
item_tiles(chunk_x: number, chunk_z: number): Iterable<Tile> {
return this.#item_tiles.get(chunk_key(chunk_x, chunk_z)) ?? [];
return this.#item_tiles.in_chunk(chunk_x, chunk_z);
}
// whether it runs its components' on_tick and on_second near players
set_ticks(tile: Tile, ticks: boolean) {
this.#ticking_tiles.set(tile, ticks);
}
ticks(tile: Tile) {
return this.#ticking_tiles.has(tile);
}
// the tiles in a chunk that tick
ticking_tiles(chunk_x: number, chunk_z: number): Iterable<Tile> {
return this.#ticking_tiles.in_chunk(chunk_x, chunk_z);
}
get has_ticking_tiles() {
return !this.#ticking_tiles.empty;
}
#mark_unsaved(chunk_x: number, chunk_z: number) {