ctx.blocks.solid(id) api

This commit is contained in:
2026-10-20 14:22:11 -03:00
parent 987e7a0f9a
commit 0bc66c21b9
11 changed files with 115 additions and 5 deletions
+37
View File
@@ -6,6 +6,7 @@
// (getCollisionShape). blocks get theirs from the boxes of their model, or set "shape" like minecraft's Block.box
import {
block_parts,
DEFAULT_MODEL,
element_rotation,
find_model,
looks_by_state,
@@ -219,6 +220,42 @@ export function covers_face(shape: Shape, face: "west" | "east" | "bottom" | "to
return true;
}
// whether the block is solid like stone: drawn in the solid layer, and a whole block to bump into in every state. it's
// what passes signals on, see BlockApi.solid. glass, leaves, slabs and chests aren't. worked out once per block from
// every collision shape, shape and model its variants can give it, so one that no state shows counts too. blocks made
// of parts aren't
export function is_solid(block: BlockRegistry): boolean {
let solid = solid_cache.get(block);
if (solid === undefined) {
solid = whole_in_every_state(block);
solid_cache.set(block, solid);
}
return solid;
}
const solid_cache = new WeakMap<BlockRegistry, boolean>();
function whole_in_every_state(block: BlockRegistry) {
if ((block.render_layer ?? "solid") !== "solid" || !block.has_collision || block.parts) return false;
const looks = [block, ...Object.values(block.variants ?? {})];
const whole = (shape: Shape) =>
shape.some((box) => [0, 1, 2].every((axis) => box[axis] <= FLAT && box[axis + 3] >= 1 - FLAT));
// a state bumps into its collision_shape, or its shape, or its model's boxes, the first it has. the block's own is
// there in every state its variants don't set one, so what comes after it never shows
if (!looks.every((look) => !look.collision_shape || whole(pixel_shape(look.collision_shape, 0, COLLISION_TOP)))) {
return false;
}
if (block.collision_shape) return true;
if (!looks.every((look) => !look.shape || whole(pixel_shape(look.shape)))) return false;
if (block.shape) return true;
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
return looks.every((look) => {
const model = find_model(look.model ?? block.model ?? DEFAULT_MODEL, models);
// drawn as a cube when its model is missing, see block_shape
return !model || whole(model_shape(model));
});
}
function clamp(box: number[], top = 1): Box {
const c = box.map((v, i) => Math.min(Math.max(v, 0), i % 3 === 1 ? top : 1));
return [c[0], c[1], c[2], c[3], c[4], c[5]];
+2
View File
@@ -56,6 +56,8 @@ export interface ClientContext {
// the components its json lists and their params, a copy, like the server's ctx.blocks.components. for showing
// what a component does, like what a crab pot catches
components(id: Id): Record<Id, unknown>;
// whether it's solid like stone, like the server's ctx.blocks.solid
solid(id: Id): boolean;
};
// the mods' loot table files (see docs/modding/resources/loot_tables.md), to show what they give. tables server
// scripts make with ctx.loot.register_table aren't here
+3
View File
@@ -511,6 +511,9 @@ export interface BlockApi {
toughness(id: Id): number;
// the components its json lists and their params, a copy, like ItemApi.components
components(id: Id): Record<Id, unknown>;
// whether it's solid like stone: drawn in the solid layer, and a whole block to bump into in every state. glass,
// leaves, slabs and chests aren't. what passes signals on, and what wire turns corners around. false for air
solid(id: Id): boolean;
}
export interface RecipeApi {
+2
View File
@@ -72,6 +72,8 @@ export interface SharedContext {
exists(id: Id): boolean;
// the components its json lists and their params, a copy
components(id: Id): Record<Id, unknown>;
// whether it's solid like stone, like the server's ctx.blocks.solid
solid(id: Id): boolean;
};
readonly components: {
// the rules of a block component, the same id the server script registers the rest of it under