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
+12 -4
View File
@@ -1,7 +1,7 @@
// block models, in the shape of minecraft's block model json: boxes ("elements") with a texture per face.
// a block picks one with "model" and fills in its texture variables with "textures". baking turns a model
// into quads in block space (0 to 1) that the chunk mesher and the item renderers draw
import type { BlockRegistry, BlockTextures } from "./everything_registry.ts";
import { type BlockRegistry, type BlockTextures, liquid_textures } from "./everything_registry.ts";
import { tint_without_biome } from "./worldgen/biome_colors.ts";
import { get_state_value } from "./utils.ts";
@@ -190,7 +190,7 @@ export function block_variant(block: BlockRegistry, states?: Record<string, numb
}
return {
model: model ?? block.model ?? DEFAULT_MODEL,
textures: textures ?? block.textures,
textures: textures ?? cube_textures(block),
y: y ?? 0,
shape: shape ?? block.shape,
collision_shape: collision_shape ?? block.collision_shape,
@@ -263,13 +263,21 @@ export function block_item_texture(block: BlockRegistry): string | undefined {
}
export function cube_face_texture(block: BlockRegistry, face: "top" | "bottom" | "front" | "side") {
return resolve_texture(`#${face}`, block.textures, BUILTIN_MODELS[DEFAULT_MODEL]);
return resolve_texture(`#${face}`, cube_textures(block), BUILTIN_MODELS[DEFAULT_MODEL]);
}
// the block's textures as engine:cube's. a liquid's { still, flowing } are its top and bottom, and its sides, for its
// item and its particles. the chunk mesher draws liquids itself
export function cube_textures(block: BlockRegistry): BlockTextures {
if (!block.liquid) return block.textures;
// through the variables, so a "tint" on still or flowing still finds them
return { ...liquid_textures(block.textures), top: "#still", bottom: "#still", side: "#flowing" };
}
// the color to draw a face of a block's item in, as r g b from 0 to 1. items aren't in a biome, so a biome "tint"
// gets the default grass or foliage color, like minecraft's grass block item
export function cube_face_tint(block: BlockRegistry, face: "top" | "bottom" | "front" | "side"): number[] {
return item_tint(block, texture_variables(`#${face}`, block.textures, BUILTIN_MODELS[DEFAULT_MODEL]));
return item_tint(block, texture_variables(`#${face}`, cube_textures(block), BUILTIN_MODELS[DEFAULT_MODEL]));
}
// the color to draw a part of a block's item in that got its texture through these variables, see cube_face_tint
+35 -3
View File
@@ -153,14 +153,46 @@ export interface BlockRegistry {
// draws the item a server script gives it (ServerWorld.set_block_item) on top of it, like farmer's delight's
// cutting board. surface is the height in pixels it lies on. turned with the variant's y
item_display?: { surface: number };
// a liquid like water, drawn like luanti's: its top slopes down towards where it flows. see LiquidProperties
liquid?: LiquidProperties;
compiled_states?: CompiledStateDefinition[];
}
// water, and any other block entities pass through and players can place into. what entities swim in, and where
// falling doesn't hurt
// a liquid's "level" state is 0 for a source, the full block, and 1 to range as it flows away from one, each lower,
// like minecraft's. liquids get that state without listing it. their textures are one texture, or { still, flowing }:
// still on the top and bottom, flowing on the sides, like luanti's special tiles
export interface LiquidProperties {
// how many blocks it flows from a source, 1 to 7, like luanti's liquid_range
range: number;
// ticks between each step it flows, like minecraft's 5 for water and 30 for lava
ticks: number;
// a spot with two sources beside it and something solid or a source under it becomes a source, like minecraft's
// infinite water and luanti's liquid_renewable
renewable: boolean;
}
export const LIQUID_LEVEL: BlockStateDefinition = { name: "level", bits: 3, 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 };
// what entities swim in, and where falling doesn't hurt
export function is_liquid_block(block: BlockRegistry | undefined): boolean {
return block !== undefined && !block.has_collision && block.replaceable === true;
return block?.liquid !== undefined;
}
// a liquid's textures as the top, bottom and sides of a cube, for its item and its particles
export function liquid_textures(textures: BlockTextures): { still: string; flowing: string } {
if (typeof textures === "string") return { still: textures, flowing: textures };
const still = textures.still ?? "engine:missing";
return { still, flowing: textures.flowing ?? still };
}
// how high a liquid's surface is in a block of it at this level, 0 to 1, like luanti's flowing levels
export function liquid_height(level: number, range: number) {
if (level === 0) return 1;
return (range - Math.min(level, range) + 0.5) / range;
}
// what a tool is made of, like minecraft's ToolMaterial
+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) =>