Neighbor Updates as a Queue
This commit is contained in:
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user