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
+52 -24
View File
@@ -45,14 +45,7 @@ import {
import { collision_shape_at, type Shape } from "$/common/block_shapes.ts";
import { physics } from "$/common/physics.ts";
import { EXHAUSTION } from "./vitals.ts";
import {
type BlockComponentOf,
entity_ref,
ModRuntime,
new_stack,
run_guarded,
type ServerCommandSource,
} from "./mod_runtime.ts";
import { entity_ref, ModRuntime, new_stack, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
import type { LootContext } from "$/common/mod_api/server.ts";
import type { EntityJson } from "$/common/mod_data.ts";
import {
@@ -160,6 +153,8 @@ interface SavedTile {
mod_data?: unknown;
// what it shows, see ServerWorld.set_block_item in the mod api
item?: ItemData;
// false when its components turned ticking off for it
ticking?: false;
}
// the loaded mods, see load_mods.ts
@@ -283,6 +278,7 @@ export class GameServer {
z: tile.z,
mod_data: tile.mod_data,
item: tile.item,
ticking: tile.ticking,
});
}
this.entities.load(saved?.version === 4 ? upgrade_v4_entities(saved.entities) : saved?.entities ?? []);
@@ -371,25 +367,20 @@ export class GameServer {
this.#broadcast({ type: "time", time: this.world.time });
}
// the tiles that tick, picked before any runs so one that a handler adds waits for the next tick
const ticking: [Tile, readonly BlockComponentOf[]][] = [];
let near: Map<number, [number, number]> | undefined;
for (const tile of this.world.tiles.values()) {
if (tile.mod_data === undefined) {
continue;
}
const components = this.mods.ticking_components_of(tile.id);
if (components.length === 0) {
continue;
}
near ??= this.#chunks_near_players();
if (near.has(chunk_key(Math.floor(tile.x / CHUNK_SIZE), Math.floor(tile.z / CHUNK_SIZE)))) {
ticking.push([tile, components]);
// the tiles near players that tick, picked before any runs so one that a handler adds waits for the next tick
const ticking: Tile[] = [];
if (this.world.has_ticking_tiles) {
for (const [chunk_x, chunk_z] of this.#chunks_near_players().values()) {
for (const tile of this.world.ticking_tiles(chunk_x, chunk_z)) ticking.push(tile);
}
}
for (const [tile, components] of ticking) {
for (const tile of ticking) {
// a handler that ran before it broke it or turned its ticking off
if (!this.world.ticks(tile)) {
continue;
}
const block = this.mods.block_ref(tile.x, tile.y, tile.z, tile.id);
for (const { mod, id, component, params } of components) {
for (const { mod, id, component, params } of this.mods.ticking_components_of(tile.id)) {
if (component.on_tick) {
run_guarded(mod, `${id} on_tick`, () => component.on_tick!(block, params, TICK_DELTA));
}
@@ -446,6 +437,7 @@ export class GameServer {
z: tile.z,
mod_data: tile.mod_data,
item: tile.item,
ticking: tile.ticking,
})),
containers: Object.fromEntries(
[...this.containers].map(([id, container]) => [id, container.to_data()]),
@@ -717,6 +709,42 @@ export class GameServer {
return tile;
}
// BlockRef.data's setter
set_tile_data(x: number, y: number, z: number, data: unknown) {
const tile = this.get_or_create_tile(x, y, z);
tile.mod_data = data;
this.#update_ticking(tile);
this.world.dirty = true;
}
// a component turning ticking off for one block, or back on, see BlockRef.set_ticking. it stays off when saved
set_tile_ticking(x: number, y: number, z: number, ticking: boolean) {
if (this.world.get_block_id(x, y, z) === AIR_ID) {
return;
}
// ticking is on unless a tile says it's off
const tile = ticking ? this.world.get_tile(x, y, z) : this.get_or_create_tile(x, y, z);
if (!tile) {
return;
}
if (ticking) delete tile.ticking;
else tile.ticking = false;
this.#update_ticking(tile);
this.world.dirty = true;
}
// which of the tiles loaded with the world tick, which needs every mod's components registered first
find_ticking_tiles() {
for (const tile of this.world.tiles.values()) this.#update_ticking(tile);
}
// a tile ticks while it has data, its block has components with on_tick or on_second, and they haven't turned it off
#update_ticking(tile: Tile) {
const ticks = tile.mod_data !== undefined && tile.ticking !== false &&
this.mods.ticking_components_of(tile.id).length > 0;
this.world.set_ticks(tile, ticks);
}
// containers
create_container(size: number): { id: string; container: Container } {
+1
View File
@@ -77,6 +77,7 @@ export async function start_game(
mods.flatMap((mod) => mod.server_url ? [{ ...script_of(mod), url: mod.server_url }] : []),
);
runtime.finish_setup();
game.find_ticking_tiles();
// what a worldgen worker needs to generate the same chunks, see worldgen_pool.ts
game.worldgen_bundle = worldgen_bundle(
game.world.seed,
+5 -2
View File
@@ -547,8 +547,10 @@ export class ModRuntime {
return game.world.get_tile(x, y, z)?.mod_data;
},
set data(value) {
game.get_or_create_tile(x, y, z).mod_data = value;
game.world.dirty = true;
game.set_tile_data(x, y, z, value);
},
set_ticking(ticking) {
game.set_tile_ticking(x, y, z, ticking);
},
};
}
@@ -687,6 +689,7 @@ export class ModRuntime {
},
is_face_sturdy: (x, y, z, face) => covers_face(game().world.collision_shape(x, y, z), face),
get_block_data: (x, y, z) => game().world.get_tile(x, y, z)?.mod_data as never,
set_block_ticking: (x, y, z, ticking) => game().set_tile_ticking(x, y, z, ticking),
set_block_item: (x, y, z, item) => {
if (game().world.get_block_id(x, y, z) === AIR_ID) return;
if (!item || item.count <= 0) {
+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) {