Placing schematics
This commit is contained in:
@@ -189,6 +189,20 @@ export function default_block_value(nid: number, block_info: BlockRegistry | und
|
||||
return value;
|
||||
}
|
||||
|
||||
// a block's value with the states named set over its defaults, like { facing: 1 }. throws for a state it doesn't have
|
||||
// or a value that doesn't fit in it
|
||||
export function value_with_states(nid: number, block_info: BlockRegistry, states: Record<string, number>): number {
|
||||
let value = default_block_value(nid, block_info);
|
||||
for (const [name, state] of Object.entries(states)) {
|
||||
const bits = block_info.states?.find((s) => s.name === name)?.bits;
|
||||
if (bits === undefined || !Number.isInteger(state) || state < 0 || state >= 2 ** bits) {
|
||||
throw new Error(`${block_info.id} has no state ${name} that fits ${state}`);
|
||||
}
|
||||
value = set_state_value(value, block_info, name, state)!;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function block_value(nid: number, state: number): number {
|
||||
return ((state << STATE_SHIFT) | nid) >>> 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user