Some optimizations

This commit is contained in:
2026-10-10 20:36:00 -03:00
parent bf91450a9d
commit afe6e03d4c
3 changed files with 78 additions and 26 deletions
+34 -14
View File
@@ -39,7 +39,14 @@ import {
} from "$/common/utils.ts";
import { collision_shape_at, type Shape } from "$/common/block_shapes.ts";
import { EXHAUSTION } from "./vitals.ts";
import { entity_ref, ModRuntime, new_stack, run_guarded, type ServerCommandSource } from "./mod_runtime.ts";
import {
type BlockComponentOf,
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 {
@@ -354,15 +361,23 @@ export class GameServer {
this.#broadcast({ type: "time", time: this.world.time });
}
for (const tile of [...this.world.tiles.values()]) {
// 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.components_of(tile.id)
.filter(({ component }) => component.on_tick || component.on_second);
if (components.length === 0 || !this.#near_any_player(tile.x, tile.z)) {
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]);
}
}
for (const [tile, components] of ticking) {
const block = this.mods.block_ref(tile.x, tile.y, tile.z, tile.id);
for (const { mod, id, component, params } of components) {
if (component.on_tick) {
@@ -1960,6 +1975,19 @@ export class GameServer {
if (!wanted.includes(1)) {
return [];
}
const found: [number, number, number, string][] = [];
for (const [cx, cz] of this.#chunks_near_players().values()) {
for (const [x, y, z] of this.world.find_blocks(cx, cz, wanted)) {
if (this.world.get_tile(x, y, z)?.mod_data === undefined) {
found.push([x, y, z, this.world.get_block_id(x, y, z)]);
}
}
}
return found;
}
// the chunks within SIMULATION_DISTANCE of a player, by chunk_key, with their coordinates
#chunks_near_players(): Map<number, [number, number]> {
const chunks = new Map<number, [number, number]>();
for (const player of this.#players.values()) {
const px = Math.floor(player.x / CHUNK_SIZE);
@@ -1970,15 +1998,7 @@ export class GameServer {
}
}
}
const found: [number, number, number, string][] = [];
for (const [cx, cz] of chunks.values()) {
for (const [x, y, z] of this.world.find_blocks(cx, cz, wanted)) {
if (this.world.get_tile(x, y, z)?.mod_data === undefined) {
found.push([x, y, z, this.world.get_block_id(x, y, z)]);
}
}
}
return found;
return chunks;
}
#near_any_player(x: number, z: number) {