Waterlogging

This commit is contained in:
2026-10-07 20:56:24 -03:00
parent 78117f65c9
commit 757ee74bc7
26 changed files with 459 additions and 73 deletions
+43 -8
View File
@@ -152,17 +152,23 @@ export function placement_target(
return clicked?.replaceable ? { x, y, z } : { x: x + offset.x, y: y + offset.y, z: z + offset.z };
}
// whether a player can place block where current is now, with below under it. current is undefined for air
// whether a player can place block where current is now, with below under it. current is undefined for air,
// current_value is its value with its states
export function can_place(
block: BlockRegistry | undefined,
current: BlockRegistry | undefined,
current_value: number,
below: string,
): boolean {
if (!block) return false;
const into = current?.id ?? "bworld:air";
const replaceable = !current || current.replaceable === true;
const in_ok = block.place_in ? list_matches("blocks", block.place_in, into) : replaceable;
return in_ok && can_stand_on(block, below);
// one that's only ever waterlogged goes into a source of its liquid and nothing else
const source_ok = !block.waterlogged_only ||
(current !== undefined && current.id === block.waterloggable &&
(get_state_value(current_value, current, "level") ?? 0) === 0);
return in_ok && source_ok && can_stand_on(block, below);
}
// whether an entity's collision box, centered on x and z with its feet at y, overlaps the block at bx, by, bz. only
@@ -187,10 +193,39 @@ export function chunk_key(x: number, z: number) {
return (x + 32768) * 65536 + (z + 32768);
}
// the liquid a block value is, for liquids pushing things (see push_by_liquid in physics.ts), or undefined for blocks
// that aren't liquids
export function liquid_in_cell(value: number, block: BlockRegistry | undefined): LiquidInCell | undefined {
if (!block?.liquid) return undefined;
const level = get_state_value(value, block, "level") ?? 0;
return { id: value & ID_MASK, height: liquid_height(level, block.liquid.range) };
// whether a block value is a waterloggable block holding its liquid
export function is_waterlogged(value: number, block: BlockRegistry | undefined): boolean {
return block?.waterloggable !== undefined && get_state_value(value, block, "waterlogged") === 1;
}
// the liquid in a cell with this block value: the block itself, or the liquid it's waterlogged with. undefined for
// none. what entities swim in, see in_water in physics.ts
export function liquid_block(value: number, block: BlockRegistry | undefined): BlockRegistry | undefined {
if (block?.liquid) return block;
if (!is_waterlogged(value, block)) return undefined;
return EverythingRegistry.get<BlockRegistry>("blocks", block!.waterloggable!);
}
// the liquid in a cell and how high it is, for liquids pushing things (see push_by_liquid in physics.ts). a waterlogged
// block holds a source
export function liquid_in_cell(value: number, block: BlockRegistry | undefined): LiquidInCell | undefined {
if (block?.liquid) {
const level = get_state_value(value, block, "level") ?? 0;
return { id: block.id, height: liquid_height(level, block.liquid.range) };
}
const liquid = liquid_block(value, block);
return liquid && { id: liquid.id, height: 1 };
}
// a waterloggable block placed where a source of its liquid is holds it, like minecraft's getStateForPlacement.
// value is what it's placed as, current_value and current what's there now
export function waterlog_placement(
value: number,
block: BlockRegistry,
current_value: number,
current: BlockRegistry | undefined,
): number {
if (!block.waterloggable || current?.id !== block.waterloggable) return value;
if ((get_state_value(current_value, current, "level") ?? 0) !== 0) return value;
return set_state_value(value, block, "waterlogged", 1)!;
}