Water flowing

This commit is contained in:
2026-10-07 00:09:08 -03:00
parent 69c5c187db
commit b830196829
14 changed files with 1033 additions and 55 deletions
+68 -6
View File
@@ -6,6 +6,10 @@ import {
type BlockStateDefinition,
type FoodProperties,
type ItemRegistry,
LIQUID_DEFAULTS,
LIQUID_LEVEL,
type LiquidProperties,
MAX_LIQUID_RANGE,
RENDER_LAYERS,
type RenderLayer,
type ToolProperties,
@@ -72,6 +76,8 @@ export interface BlockJson {
states?: BlockStateDefinition[];
components?: Record<string, unknown>;
item_display?: { surface: number };
// makes it a liquid, see LiquidProperties. range is 7 when not set
liquid?: Partial<LiquidProperties>;
}
export interface OreJson {
@@ -275,7 +281,8 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
if (block.light_emission !== undefined) json.light_emission = block.light_emission;
if (block.light_opacity !== undefined) json.light_opacity = block.light_opacity;
if (block.alpha !== undefined) json.alpha = block.alpha;
if (!block.has_collision) json.collision = false;
// liquids are without collision and replaceable unless they say otherwise
if (block.has_collision === !!block.liquid) json.collision = block.has_collision;
if (block.shape !== undefined) json.shape = block.shape;
if (block.collision_shape !== undefined) json.collision_shape = block.collision_shape;
if (block.parts !== undefined) json.parts = block.parts;
@@ -289,12 +296,21 @@ export function block_to_json(block: BlockRegistry, has_item: boolean): BlockJso
if (block.drop_table !== undefined) json.drops = block.drop_table;
if (!has_item) json.item = false;
if (block.interactive) json.interactive = true;
if (block.replaceable) json.replaceable = true;
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;
if (block.states) json.states = block.states;
// 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;
if (block.components) json.components = block.components;
if (block.item_display) json.item_display = block.item_display;
if (block.liquid) {
// only what isn't the default
const liquid = block.liquid;
json.liquid = Object.fromEntries(
Object.entries(liquid).filter(([key, value]) => LIQUID_DEFAULTS[key as keyof LiquidProperties] !== value),
);
}
return json;
}
@@ -302,7 +318,7 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
const block: BlockRegistry = {
id: json.id,
textures: json.textures,
has_collision: json.collision ?? true,
has_collision: json.collision ?? !json.liquid,
};
if (json.name !== undefined) block.name = json.name;
if (json.model !== undefined) block.model = json.model;
@@ -326,12 +342,17 @@ export function block_from_json(json: BlockJson): { block: BlockRegistry; has_it
}
if (json.drops !== undefined) block.drop_table = json.drops;
if (json.interactive) block.interactive = true;
if (json.replaceable) block.replaceable = true;
if (json.replaceable ?? json.liquid) block.replaceable = true;
if (json.place_on) block.place_on = json.place_on;
if (json.place_in) block.place_in = json.place_in;
if (json.states) block.states = json.states;
if (json.components) block.components = json.components;
if (json.item_display) block.item_display = json.item_display;
if (json.liquid) {
block.liquid = { ...LIQUID_DEFAULTS, ...json.liquid };
// last, so the block's own states keep their bits
block.states = [...block.states ?? [], { ...LIQUID_LEVEL }];
}
return { block, has_item: json.item ?? true };
}
@@ -504,7 +525,9 @@ export function validate_block(json: unknown): Problems {
if (json.model !== undefined) problems.push(...validate_id(json.model, "model"));
const is_cube = json.parts === undefined && (json.model === undefined || json.model === "engine:cube");
const textures = json.textures;
if (is_cube) {
if (json.liquid !== undefined) {
problems.push(...validate_liquid(json));
} else if (is_cube) {
const texture_ok = typeof textures === "string" ||
(is_object(textures) &&
(["top", "bottom", "side"].every((k) => typeof textures[k] === "string") ||
@@ -647,6 +670,45 @@ export function validate_block(json: unknown): Problems {
return problems;
}
// a liquid is drawn by the engine, so it has no model of its own, and its level is a state it gets
function validate_liquid(json: Record<string, unknown>): Problems {
const problems: Problems = [];
const liquid = json.liquid;
if (!is_object(liquid)) return ["liquid must be { range, ticks, renewable }, or {}"];
for (const key of Object.keys(liquid)) {
if (!(key in LIQUID_DEFAULTS)) problems.push(`liquid.${key} isn't a liquid property`);
}
if (liquid.ticks !== undefined && (!Number.isInteger(liquid.ticks) || (liquid.ticks as number) < 1)) {
problems.push("liquid.ticks must be a whole number of 1 or more");
}
if (liquid.renewable !== undefined && typeof liquid.renewable !== "boolean") {
problems.push("liquid.renewable must be true or false");
}
const range = liquid.range;
if (
range !== undefined &&
(!Number.isInteger(range) || (range as number) < 1 || (range as number) > MAX_LIQUID_RANGE)
) {
problems.push(`liquid.range must be a whole number from 1 to ${MAX_LIQUID_RANGE}`);
}
const textures = json.textures;
const texture_ok = typeof textures === "string" ||
(is_object(textures) && typeof textures.still === "string" &&
(textures.flowing === undefined || typeof textures.flowing === "string") &&
Object.keys(textures).every((key) => key === "still" || key === "flowing"));
if (!texture_ok) problems.push("a liquid's textures must be a texture id or { still, flowing }");
for (const key of ["model", "variants", "parts", "shape", "collision_shape", "item_model"]) {
if (json[key] !== undefined) problems.push(`a liquid can't have ${key}, the engine draws it`);
}
const states = Array.isArray(json.states) ? json.states : [];
if (states.some((state) => is_object(state) && state.name === LIQUID_LEVEL.name)) {
problems.push(`a liquid gets a "${LIQUID_LEVEL.name}" state of its own`);
}
const bits = states.reduce((sum, s) => sum + (is_object(s) && typeof s.bits === "number" ? s.bits : 0), 0);
if (bits + LIQUID_LEVEL.bits > 16) problems.push(`its states and its level use more than 16 bits`);
return problems;
}
// boxes in pixels: [[x1, y1, z1, x2, y2, z2], ...], each corner from 0 to 16 (y to top) and the second past the first
function validate_shape(shape: unknown, where: string, top = 16): Problems {
const is_box = (box: unknown) =>