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
+7 -5
View File
@@ -119,20 +119,22 @@ export class ItemEntity extends Entity {
return true;
}
// whether two stacks could be one
// whether two stacks could be one. the cheap checks first, comparing data last
can_merge_with(other: ItemEntity) {
return other !== this && !other.removed && other.item.type_id === this.item.type_id &&
JSON.stringify(other.item.data) === JSON.stringify(this.item.data) &&
this.item.amount < this.item.max_amount && other.item.amount < other.item.max_amount;
this.item.amount < this.item.max_amount && other.item.amount < other.item.max_amount &&
(other.item.data === this.item.data || JSON.stringify(other.item.data) === JSON.stringify(this.item.data));
}
// the smaller stack goes into the bigger one, like minecraft's ItemEntity.tryToMerge
merge_with_neighbours() {
// a full stack can't take more or give any, so there's no one to look for
if (this.item.amount >= this.item.max_amount) return;
const reach = MERGE_DISTANCE + ITEM_SIZE;
for (const other of this.level.entities_near(this.x, this.y, this.z, reach)) {
if (this.removed) return;
if (!(other instanceof ItemEntity) || !this.can_merge_with(other)) continue;
if (Math.abs(this.y - other.y) > ITEM_SIZE) continue;
if (!(other instanceof ItemEntity) || Math.abs(this.y - other.y) > ITEM_SIZE) continue;
if (!this.can_merge_with(other)) continue;
const [into, from] = this.item.amount >= other.item.amount ? [this, other] : [other, this];
const moving = Math.min(from.item.amount, into.item.max_amount - into.item.amount);
+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) {
+37 -7
View File
@@ -94,6 +94,14 @@ export class Signal<T> {
let running_mod = "";
// a mod's script, imported from url. engine_access lets it import bworld/engine
// a component a block lists, with its params from the block's json
export interface BlockComponentOf {
mod: string;
id: string;
component: BlockComponent;
params: unknown;
}
interface ScriptSource {
mod: string;
version: string;
@@ -434,14 +442,34 @@ export class ModRuntime {
}
// components on a block, with their params from its json
components_of(block_id: string): { mod: string; id: string; component: BlockComponent; params: unknown }[] {
const block = EverythingRegistry.get<{ components?: Record<string, unknown> }>("blocks", block_id);
// components that only have shared rules have nothing here
return Object.entries(block?.components ?? {}).flatMap(([id, params]) => {
const registered = this.block_components.get(id);
return registered ? [{ ...registered, id, params }] : [];
});
// worked out once per block, blocks and components don't change after loading. shared, so callers don't change it
components_of(block_id: string): readonly BlockComponentOf[] {
let components = this.#components_of.get(block_id);
if (!components) {
const block = EverythingRegistry.get<{ components?: Record<string, unknown> }>("blocks", block_id);
// components that only have shared rules have nothing here
components = Object.entries(block?.components ?? {}).flatMap(([id, params]) => {
const registered = this.block_components.get(id);
return registered ? [{ ...registered, id, params }] : [];
});
this.#components_of.set(block_id, components);
}
return components;
}
#components_of = new Map<string, BlockComponentOf[]>();
// the ones with on_tick or on_second, for ticking blocks every tick. cached like components_of
ticking_components_of(block_id: string): readonly BlockComponentOf[] {
let components = this.#ticking_components_of.get(block_id);
if (!components) {
components = this.components_of(block_id).filter(({ component }) =>
component.on_tick || component.on_second
);
this.#ticking_components_of.set(block_id, components);
}
return components;
}
#ticking_components_of = new Map<string, BlockComponentOf[]>();
// components on an item, with their params from its json
item_components_of(item_id: string): { mod: string; id: string; component: ItemComponent; params: unknown }[] {
@@ -558,6 +586,8 @@ export class ModRuntime {
throw new ModLoadError(mod, `component ${id} is registered twice`);
}
this.block_components.set(id, { mod, component: component as BlockComponent });
this.#components_of.clear();
this.#ticking_components_of.clear();
},
register_item: (id, component) => {
setup_only("components");