Cache ticking blocks
This commit is contained in:
+52
-24
@@ -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 } {
|
||||
|
||||
Reference in New Issue
Block a user