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
+7
View File
@@ -155,6 +155,10 @@ export interface BlockRegistry {
item_display?: { surface: number };
// a liquid like water, drawn like luanti's: its top slopes down towards where it flows. see LiquidProperties
liquid?: LiquidProperties;
// the liquid it can hold, like minecraft's waterlogged fences. it gets a "waterlogged" state, see WATERLOGGED
waterloggable?: string;
// it can only be placed into a source of that liquid, so it's always placed holding it, like farmer's delight's rice
waterlogged_only?: boolean;
compiled_states?: CompiledStateDefinition[];
}
@@ -173,6 +177,9 @@ export interface LiquidProperties {
}
export const LIQUID_LEVEL: BlockStateDefinition = { name: "level", bits: 3, default: 0 };
// a waterloggable block's state, 1 while it holds a source of its liquid. blocks get it without listing it, after
// their own states
export const WATERLOGGED: BlockStateDefinition = { name: "waterlogged", bits: 1, default: 0 };
export const MAX_LIQUID_RANGE = 7;
// what a liquid that doesn't say has
export const LIQUID_DEFAULTS: LiquidProperties = { range: MAX_LIQUID_RANGE, ticks: 5, renewable: true };
+8
View File
@@ -86,6 +86,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
const entity_model_jsons = new Map<string, EntityModelJson>();
const effect_owners = new Map<string, string[]>();
const tag_owners = new Map<TagRegistry, Set<string>>();
const liquids = new Set<string>();
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
const textures = new Set<string>(engine_textures);
@@ -94,6 +95,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
for (const { json } of mod.blocks) {
add(block_owners, json.id, mod.id);
if (json.item !== false) add(item_owners, json.id, mod.id);
if (json.liquid) liquids.add(json.id);
}
// one without a texture is the item of the mod's block, which is already counted
for (const { json } of mod.items) if (json.texture !== undefined) add(item_owners, json.id, mod.id);
@@ -165,6 +167,12 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
}
}
if (json.drops) uses(file, json.drops, "item");
if (json.waterloggable) {
uses(file, json.waterloggable, "block");
if (block_owners.has(json.waterloggable) && !liquids.has(json.waterloggable)) {
errors.push(`${file}: waterloggable ${json.waterloggable} isn't a liquid`);
}
}
for (const id of [...(json.place_on ?? []), ...(json.place_in ?? [])]) {
if (id !== "bworld:air") uses(file, id, "block");
}
+34 -3
View File
@@ -13,6 +13,7 @@ import {
RENDER_LAYERS,
type RenderLayer,
type ToolProperties,
WATERLOGGED,
type WornProperties,
} from "./everything_registry.ts";
import { type BlockPart, type BlockVariant, MODEL_FACES, type ModelFace } from "./block_models.ts";
@@ -78,6 +79,10 @@ export interface BlockJson {
item_display?: { surface: number };
// makes it a liquid, see LiquidProperties. range is 7 when not set
liquid?: Partial<LiquidProperties>;
// the liquid it can hold, see BlockRegistry.waterloggable
waterloggable?: string;
// only placed into a source of it, see BlockRegistry.waterlogged_only
waterlogged_only?: boolean;
}
export interface OreJson {
@@ -301,9 +306,12 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
if (!!block.replaceable !== !!block.liquid) json.replaceable = !!block.replaceable;
if (block.place_on) json.place_on = block.place_on;
if (block.place_in) json.place_in = block.place_in;
// a liquid's level state is its own, added when it loads
const states = block.liquid ? block.states?.slice(0, -1) : block.states;
if (states && (states.length > 0 || !block.liquid)) json.states = states;
// a liquid's level state and a waterloggable block's waterlogged state are its own, added when it loads
const added = block.liquid || block.waterloggable;
const states = added ? block.states?.slice(0, -1) : block.states;
if (states && (states.length > 0 || !added)) json.states = states;
if (block.waterloggable) json.waterloggable = block.waterloggable;
if (block.waterlogged_only) json.waterlogged_only = true;
if (block.components) json.components = block.components;
if (block.item_display) json.item_display = block.item_display;
if (block.liquid) {
@@ -355,6 +363,12 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
// last, so the block's own states keep their bits
block.states = [...block.states ?? [], { ...LIQUID_LEVEL }];
}
if (json.waterlogged_only) block.waterlogged_only = true;
if (json.waterloggable) {
block.waterloggable = json.waterloggable;
// last too, see WATERLOGGED
block.states = [...block.states ?? [], { ...WATERLOGGED }];
}
return { block, has_item: json.item ?? true };
}
@@ -669,6 +683,23 @@ export function validate_block(json: unknown): Problems {
} else {
const bits = states.reduce((sum, s) => sum + (is_object(s) && typeof s.bits === "number" ? s.bits : 0), 0);
if (bits > 16) problems.push(`states use ${bits} bits, the most is 16`);
if (json.waterloggable !== undefined && bits + WATERLOGGED.bits > 16) {
problems.push(`its states and waterlogged use more than 16 bits`);
}
}
}
if (json.waterloggable !== undefined) {
problems.push(...validate_id(json.waterloggable, "waterloggable"));
if (json.liquid !== undefined) problems.push("a liquid can't be waterloggable");
const states = Array.isArray(json.states) ? json.states : [];
if (states.some((state) => is_object(state) && state.name === WATERLOGGED.name)) {
problems.push(`a waterloggable block gets a "${WATERLOGGED.name}" state of its own`);
}
}
if (json.waterlogged_only !== undefined) {
if (typeof json.waterlogged_only !== "boolean") problems.push("waterlogged_only must be true or false");
else if (json.waterlogged_only && json.waterloggable === undefined) {
problems.push("waterlogged_only needs waterloggable, the liquid it's placed into");
}
}
return problems;
+2 -2
View File
@@ -246,8 +246,8 @@ export function eyes_in_water(
// a liquid in a cell, for liquids pushing things along
export interface LiquidInCell {
// which liquid, its block's numeric id. a liquid's flow only goes by cells of the same one
id: number;
// which liquid, its block's id. a liquid's flow only goes by cells of the same one
id: string;
// its surface, 0 to 1 (see liquid_height), not counting more of it above
height: number;
}
+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)!;
}