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