Neighbor Updates as a Queue
This commit is contained in:
+79
-24
@@ -9,6 +9,7 @@ import type {
|
||||
BlockPlacement,
|
||||
BlockRules as Rules,
|
||||
Face,
|
||||
Position,
|
||||
SharedBlock,
|
||||
SharedWorld,
|
||||
} from "./mod_api/shared.ts";
|
||||
@@ -49,11 +50,25 @@ export function default_placement_value(block: BlockRegistry, yaw: number): numb
|
||||
return set_state_value(value, block, "facing", toward)!;
|
||||
}
|
||||
|
||||
// the most blocks one change can tell of changes, counting the ones those set off, before the rest are dropped. a
|
||||
// loop between two rules would otherwise never stop
|
||||
export const MAX_NEIGHBOR_UPDATES = 65536;
|
||||
|
||||
export class BlockRules {
|
||||
#rules = new Map<string, { mod: string; rules: Rules }>();
|
||||
// blocks waiting to hear of a change, 6 numbers each: where the block is, then where the change was
|
||||
#queue: number[] = [];
|
||||
// changes going on. the queue waits until they're all done, see change
|
||||
#changing = 0;
|
||||
|
||||
// runs a mod's code, reporting what it throws instead of letting it take the game down
|
||||
constructor(readonly run: <T>(mod: string, what: string, fn: () => T) => T | undefined) {}
|
||||
// run runs a mod's code, reporting what it throws instead of letting it take the game down. get_block reads the
|
||||
// side's world. told runs for each block told of a change once its rules have, where the server runs its
|
||||
// components' on_neighbor_change
|
||||
constructor(
|
||||
readonly run: <T>(mod: string, what: string, fn: () => T) => T | undefined,
|
||||
readonly get_block: SharedWorld["get_block"],
|
||||
readonly told?: (x: number, y: number, z: number, from: Position) => void,
|
||||
) {}
|
||||
|
||||
register(mod: string, id: string, rules: Rules) {
|
||||
if (this.#rules.has(id)) throw new Error(`[${mod}] the rules of ${id} are registered twice`);
|
||||
@@ -130,28 +145,68 @@ export class BlockRules {
|
||||
return handled;
|
||||
}
|
||||
|
||||
// tells the blocks beside x y z it changed, so they can change with it like a fence joining it: their rules'
|
||||
// on_neighbor_change, then after for each, which the server runs its components' on_neighbor_change in. the
|
||||
// world's set_state calls this again for what they change, until nothing changes
|
||||
notify_neighbors(
|
||||
get_block: SharedWorld["get_block"],
|
||||
x: number,
|
||||
y: number,
|
||||
z: number,
|
||||
after?: (x: number, y: number, z: number, neighbor: SharedBlock) => void,
|
||||
) {
|
||||
const neighbor = { x, y, z, id: get_block(x, y, z) ?? "bworld:air" };
|
||||
for (const [dx, dy, dz] of NEIGHBORS) {
|
||||
const [nx, ny, nz] = [x + dx, y + dy, z + dz];
|
||||
const id = get_block(nx, ny, nz);
|
||||
const block = id && EverythingRegistry.get<BlockRegistry>("blocks", id);
|
||||
if (!block) continue;
|
||||
for (const { mod, id: rules_id, rules, params } of this.#of(block)) {
|
||||
if (!rules.on_neighbor_change) continue;
|
||||
const at = { x: nx, y: ny, z: nz, id };
|
||||
this.run(mod, `${rules_id} on_neighbor_change`, () => rules.on_neighbor_change!(at, params, neighbor));
|
||||
}
|
||||
after?.(nx, ny, nz, neighbor);
|
||||
// tells the blocks beside x y z it changed, so they can change with it like a fence joining it. the world's
|
||||
// set_state calls this again for what they change, until nothing changes. see neighbor_changed
|
||||
notify_neighbors(x: number, y: number, z: number) {
|
||||
for (const [dx, dy, dz] of NEIGHBORS) this.#queue.push(x + dx, y + dy, z + dz, x, y, z);
|
||||
this.#settle();
|
||||
}
|
||||
|
||||
// tells the block at x y z the one at from changed: its rules' on_neighbor_change, then told. blocks hear in the
|
||||
// order they were told, the same on every side, once the change that told them is done. a queue instead of
|
||||
// calling them right away, so a long chain of changes doesn't grow the stack
|
||||
neighbor_changed(x: number, y: number, z: number, from: Position) {
|
||||
this.#queue.push(x, y, z, from.x, from.y, from.z);
|
||||
this.#settle();
|
||||
}
|
||||
|
||||
// runs a change to the world, holding back the blocks it tells until it's done, so they hear of all of it: one
|
||||
// told in a block's on_break hears once it's gone. the outermost change tells them all before it returns, so its
|
||||
// caller sees a settled world
|
||||
change<T>(fn: () => T): T {
|
||||
this.#changing++;
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
this.#changing--;
|
||||
this.#settle();
|
||||
}
|
||||
}
|
||||
|
||||
// tells the blocks waiting, and the ones they set off, unless a change is still going on
|
||||
#settle() {
|
||||
if (this.#changing > 0) return;
|
||||
this.#changing++;
|
||||
const queue = this.#queue;
|
||||
try {
|
||||
for (let i = 0; i < queue.length; i += 6) {
|
||||
if (i === MAX_NEIGHBOR_UPDATES * 6) {
|
||||
console.warn(
|
||||
`one change told blocks of changes ${MAX_NEIGHBOR_UPDATES} times and was stopped, last the ` +
|
||||
`block at ${queue[i - 6]} ${queue[i - 5]} ${queue[i - 4]}`,
|
||||
);
|
||||
break;
|
||||
}
|
||||
this.#tell(queue[i], queue[i + 1], queue[i + 2], { x: queue[i + 3], y: queue[i + 4], z: queue[i + 5] });
|
||||
}
|
||||
} finally {
|
||||
queue.length = 0;
|
||||
this.#changing--;
|
||||
}
|
||||
}
|
||||
|
||||
#tell(x: number, y: number, z: number, from: Position) {
|
||||
const id = this.get_block(x, y, z);
|
||||
const block = id && EverythingRegistry.get<BlockRegistry>("blocks", id);
|
||||
if (!block) return;
|
||||
// what's there now, read only for rules that want it
|
||||
let neighbor: SharedBlock | undefined;
|
||||
for (const { mod, id: rules_id, rules, params } of this.#of(block)) {
|
||||
if (!rules.on_neighbor_change) continue;
|
||||
const changed = neighbor ??= { ...from, id: this.get_block(from.x, from.y, from.z) ?? "bworld:air" };
|
||||
const at = { x, y, z, id };
|
||||
this.run(mod, `${rules_id} on_neighbor_change`, () => rules.on_neighbor_change!(at, params, changed));
|
||||
}
|
||||
this.told?.(x, y, z, from);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,7 +248,13 @@ export interface ServerWorld {
|
||||
// destroyBlockProgress: a drill getting through it. undefined takes them off, and so does the block changing
|
||||
set_break_progress(x: number, y: number, z: number, progress: number | undefined): void;
|
||||
get_state(x: number, y: number, z: number, name: string): number | undefined;
|
||||
set_state(x: number, y: number, z: number, name: string, value: number): boolean;
|
||||
// update_neighbors false changes the state without telling the blocks around it, for a script that tells them
|
||||
// itself, like a wire telling each block beside its whole length once. on_state_change still runs
|
||||
set_state(x: number, y: number, z: number, name: string, value: number, options?: SetStateOptions): boolean;
|
||||
// tells the block at x y z that the one at from changed, running its rules' and components' on_neighbor_change,
|
||||
// like neoforge's neighborChanged. for a block whose effect on others changed without it changing, or for blocks
|
||||
// it reaches that aren't beside it. it hears once the change going on is done, like the blocks beside one do
|
||||
neighbor_changed(x: number, y: number, z: number, from: Position): void;
|
||||
// whether the block's side is a full square things can attach to, like minecraft's isFaceSturdy: its collision
|
||||
// shape covers the whole face. false for air, plants, water and the sides of a slab
|
||||
is_face_sturdy(x: number, y: number, z: number, face: Face): boolean;
|
||||
@@ -301,6 +307,11 @@ export interface ServerWorld {
|
||||
set_time(time: number): void;
|
||||
}
|
||||
|
||||
export interface SetStateOptions {
|
||||
// true when not given
|
||||
update_neighbors?: boolean;
|
||||
}
|
||||
|
||||
// how a machine breaks a block, see ServerWorld.break_block
|
||||
export interface BreakOptions {
|
||||
// it drops what it would for the right tool, though nothing holds one, like create's drills and saws
|
||||
|
||||
Reference in New Issue
Block a user