Neighbor Updates as a Queue

This commit is contained in:
2026-10-20 13:40:44 -03:00
parent 0af35d1e17
commit 77cd327f94
9 changed files with 348 additions and 138 deletions
+47 -62
View File
@@ -582,87 +582,72 @@ export class GameServer {
}
// changes a block's state bits without replacing it, see ServerWorld.get_block_value: its on_state_change, then the
// blocks beside it hear of it
set_block_state(x: number, y: number, z: number, state: number) {
// blocks beside it hear of it, unless update_neighbors is false
set_block_state(x: number, y: number, z: number, state: number, update_neighbors = true) {
const id = this.world.get_block_id(x, y, z);
const value = this.world.get_block_value(x, y, z);
if (id === AIR_ID || value >>> 16 === state) return;
const components = this.mods.components_with(id, "on_state_change");
// only worked out for blocks that want it, states change often
const before = components.length > 0 ? block_states(value, this.world.get_block_info(x, y, z)!) : undefined;
this.world.set_block(x, y, z, id, state);
if (before) {
const block = this.mods.block_ref(x, y, z, id);
for (const { mod, id: component_id, component, params } of components) {
run_guarded(
mod,
`${component_id} on_state_change`,
() => component.on_state_change!(block, params, before),
);
}
}
this.#notify_neighbors(x, y, z);
}
// tells the blocks around one that changed, so they can change with it like a fence joining it: their rules (see
// BlockRules), then their components' on_neighbor_change
#notify_neighbors(x: number, y: number, z: number) {
this.mods.rules.notify_neighbors(
(bx, by, bz) => this.world.get_block_id(bx, by, bz),
x,
y,
z,
(nx, ny, nz, neighbor) => {
// read again, a rule's state change can have had a component replace it
const id = this.world.get_block_id(nx, ny, nz);
const components = this.mods.components_with(id, "on_neighbor_change");
if (components.length === 0) return;
const block = this.mods.block_ref(nx, ny, nz, id);
this.mods.rules.change(() => {
const components = this.mods.components_with(id, "on_state_change");
// only worked out for blocks that want it, states change often
const before = components.length > 0 ? block_states(value, this.world.get_block_info(x, y, z)!) : undefined;
this.world.set_block(x, y, z, id, state);
if (before) {
const block = this.mods.block_ref(x, y, z, id);
for (const { mod, id: component_id, component, params } of components) {
run_guarded(
mod,
`${component_id} on_neighbor_change`,
() => component.on_neighbor_change!(block, params, neighbor),
`${component_id} on_state_change`,
() => component.on_state_change!(block, params, before),
);
}
},
);
}
if (update_neighbors) this.mods.rules.notify_neighbors(x, y, z);
});
}
// replaces a block: the old one's on_break, then the new one's on_create, then the blocks beside it hear of it
set_block(x: number, y: number, z: number, id: string, player?: ServerPlayer, state?: number) {
const old_id = this.world.get_block_id(x, y, z);
if (this.#break_stages.size > 0) this.set_break_progress(x, y, z, undefined);
if (old_id !== AIR_ID) {
const components = this.mods.components_of(old_id).filter(({ component }) => component.on_break);
if (components.length > 0) {
const block = this.mods.block_ref(x, y, z, old_id);
const player_api = player && this.mods.player(player);
for (const { mod, id: component_id, component, params } of components) {
run_guarded(mod, `${component_id} on_break`, () => component.on_break!(block, params, player_api));
this.mods.rules.change(() => {
const old_id = this.world.get_block_id(x, y, z);
if (this.#break_stages.size > 0) this.set_break_progress(x, y, z, undefined);
if (old_id !== AIR_ID) {
const components = this.mods.components_of(old_id).filter(({ component }) => component.on_break);
if (components.length > 0) {
const block = this.mods.block_ref(x, y, z, old_id);
const player_api = player && this.mods.player(player);
for (const { mod, id: component_id, component, params } of components) {
run_guarded(
mod,
`${component_id} on_break`,
() => component.on_break!(block, params, player_api),
);
}
}
}
}
this.world.remove_tile(x, y, z);
this.world.set_block(x, y, z, id, state);
this.world.remove_tile(x, y, z);
this.world.set_block(x, y, z, id, state);
if (id !== AIR_ID) {
const block = this.mods.block_ref(x, y, z, id);
for (const { mod, id: component_id, component, params } of this.mods.components_of(id)) {
if (component.on_create) {
run_guarded(mod, `${component_id} on_create`, () => component.on_create!(block, params));
if (id !== AIR_ID) {
const block = this.mods.block_ref(x, y, z, id);
for (const { mod, id: component_id, component, params } of this.mods.components_of(id)) {
if (component.on_create) {
run_guarded(mod, `${component_id} on_create`, () => component.on_create!(block, params));
}
}
}
}
this.#notify_neighbors(x, y, z);
this.mods.rules.notify_neighbors(x, y, z);
// a block standing on this one that can't stand on what it is now breaks, like a crop on farmland that was dug
if (y + 1 < CHUNK_HEIGHT) {
const above = this.world.get_block_info(x, y + 1, z);
if (above && !can_stand_on(above, id)) {
this.destroy_block(x, y + 1, z, player);
// a block standing on this one that can't stand on what it is now breaks, like a crop on farmland that was
// dug
if (y + 1 < CHUNK_HEIGHT) {
const above = this.world.get_block_info(x, y + 1, z);
if (above && !can_stand_on(above, id)) {
this.destroy_block(x, y + 1, z, player);
}
}
}
});
}
// the cracks machines are making on blocks, as the stage clients draw, by position. see set_break_progress
+30 -4
View File
@@ -34,6 +34,7 @@ import type {
MenuType,
Player,
PlayerMenuBuilder,
Position,
ServerAfterEvents,
ServerBeforeEvents,
ServerContext,
@@ -143,8 +144,12 @@ interface Timer {
export class ModRuntime {
block_components = new Map<string, { mod: string; component: BlockComponent }>();
// what shared scripts registered, see common/block_rules.ts
rules = new BlockRules(run_guarded);
// what shared scripts registered, see common/block_rules.ts, running on the game's world
rules = new BlockRules(
run_guarded,
(x, y, z) => this.game.world.get_block_id(x, y, z),
(x, y, z, from) => this.#neighbor_changed(x, y, z, from),
);
item_components = new Map<string, { mod: string; component: ItemComponent }>();
effect_components = new Map<string, { mod: string; component: EffectComponent }>();
// every command, the engine's and mods', see common/commands.ts
@@ -492,6 +497,25 @@ export class ModRuntime {
}
#components_with = new Map<keyof BlockComponent, Map<string, BlockComponentOf[]>>();
// the block at x y z was told the one at from changed, and its rules have had their say: its components'
// on_neighbor_change
#neighbor_changed(x: number, y: number, z: number, from: Position) {
const world = this.game.world;
// read again, a rule's state change can have had a component replace it
const id = world.get_block_id(x, y, z);
const components = this.components_with(id, "on_neighbor_change");
if (components.length === 0) return;
const block = this.block_ref(x, y, z, id);
const neighbor = { ...from, id: world.get_block_id(from.x, from.y, from.z) };
for (const { mod, id: component_id, component, params } of components) {
run_guarded(
mod,
`${component_id} on_neighbor_change`,
() => component.on_neighbor_change!(block, params, neighbor),
);
}
}
// components on an item, with their params from its json
item_components_of(item_id: string): { mod: string; id: string; component: ItemComponent; params: unknown }[] {
const item = EverythingRegistry.get<ItemRegistry>("items", item_id);
@@ -708,7 +732,7 @@ export class ModRuntime {
const info = state_owner(id, name);
return get_state_value(game().world.get_block_value(x, y, z), info, name);
},
set_state: (x, y, z, name, value) => {
set_state: (x, y, z, name, value, options) => {
const id = game().world.get_block_id(x, y, z);
if (id === AIR_ID) return false;
const info = state_owner(id, name);
@@ -717,9 +741,11 @@ export class ModRuntime {
throw new Error(`${id} state "${name}" has ${bits} bits, ${value} doesn't fit`);
}
const new_value = set_state_value(game().world.get_block_value(x, y, z), info, name, value)!;
game().set_block_state(x, y, z, new_value >>> 16);
game().set_block_state(x, y, z, new_value >>> 16, options?.update_neighbors !== false);
return true;
},
neighbor_changed: (x, y, z, from) =>
this.rules.neighbor_changed(x, y, z, { x: from.x, y: from.y, z: from.z }),
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,
get_block_ref: (x, y, z) => {