diff --git a/client/mods.ts b/client/mods.ts index d5c4162..3d45ae1 100644 --- a/client/mods.ts +++ b/client/mods.ts @@ -10,7 +10,7 @@ import { ModListing, ModLoadError, RecipeBook, register_mod_data } from "$/commo import { type Bmod, read_bmod } from "$/common/bmod.ts"; import { display_name, get_state_value, set_state_value } from "$/common/utils.ts"; import { BlockRules } from "$/common/block_rules.ts"; -import { covers_face, EMPTY_SHAPE, FULL_SHAPE } from "$/common/block_shapes.ts"; +import { covers_face, EMPTY_SHAPE, FULL_SHAPE, is_solid } from "$/common/block_shapes.ts"; import type { SharedContext, SharedWorld } from "$/common/mod_api/shared.ts"; import { tag_api } from "$/common/tags.ts"; import { AIR_ID } from "$/common/protocol.ts"; @@ -205,6 +205,12 @@ function blocks_api(mod: string): SharedContext["blocks"] { if (!block) throw new Error(`[${mod}] unknown block ${id}`); return structuredClone(block.components ?? {}); }, + solid(id) { + if (id === AIR_ID) return false; + const block = EverythingRegistry.get("blocks", id); + if (!block) throw new Error(`[${mod}] unknown block ${id}`); + return is_solid(block); + }, }; } diff --git a/common/block_shapes.ts b/common/block_shapes.ts index a23f05f..ae16f01 100644 --- a/common/block_shapes.ts +++ b/common/block_shapes.ts @@ -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(); + +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("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]]; diff --git a/common/mod_api/client.ts b/common/mod_api/client.ts index 9da85dd..9fe5342 100644 --- a/common/mod_api/client.ts +++ b/common/mod_api/client.ts @@ -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; + // 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 diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index 3c4bfbd..1a3bc32 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -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; + // 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 { diff --git a/common/mod_api/shared.ts b/common/mod_api/shared.ts index b4b0aca..8580f43 100644 --- a/common/mod_api/shared.ts +++ b/common/mod_api/shared.ts @@ -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; + // 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 diff --git a/docs/modding/blocks/components.md b/docs/modding/blocks/components.md index 7fac1d0..fabdb49 100644 --- a/docs/modding/blocks/components.md +++ b/docs/modding/blocks/components.md @@ -151,7 +151,7 @@ Here `block` is `{ id, x, y, z }`, without tile data, which clients never have. `get_block`, `get_state`, `set_state`, `is_face_sturdy`, `biome_at` and `season_at` (see [Seasons](../worldgen/biomes.md#seasons)), and only works inside rules. `ctx.blocks.components(id)` gives any block's components and their params, like on the server, for rules that look at what's around them, like a shaft lining up with -the axle of the block it's placed against. +the axle of the block it's placed against, and `ctx.blocks.solid(id)` whether it's [solid](index.md#solid-blocks). ### Neighbors diff --git a/docs/modding/blocks/index.md b/docs/modding/blocks/index.md index ca7d760..2f34ecd 100644 --- a/docs/modding/blocks/index.md +++ b/docs/modding/blocks/index.md @@ -75,6 +75,14 @@ there will be holes in the world behind them. A block's definition goes to every player, since clients need it to draw the block, time breaking and collide with it. It can't hold functions. +### Solid Blocks + +A block is solid when it's drawn in the `solid` layer and entities bump into a whole block in every state: stone, +planks, logs and furnaces are, glass, leaves, slabs, chests and fences aren't. Every variant counts, even one that no +state shows, and blocks made of `parts` never are. Scripts on every side ask with `ctx.blocks.solid(id)`, which is +`false` for air and throws for a block that doesn't exist. It's worked out once per block, so it's cheap enough to ask +for every block a walk goes through, unlike `ctx.world.is_face_sturdy`, which looks at the block at a position. + ## Mining Breaking takes `mining.toughness` seconds by hand. A tool whose type matches `mining.tool` breaks it `tool.speed` times diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index eaf24c5..2d7308c 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -7,7 +7,7 @@ import { BlockRegistry, EverythingRegistry, ItemRegistry, liquid_height } from " import { Container as EngineContainer, ItemStack as EngineItemStack } from "$/common/inventory.ts"; import { block_states, get_state_value, is_waterlogged, liquid_block, set_state_value } from "$/common/utils.ts"; import { LootTables } from "./loot.ts"; -import { collision_shape_at, covers_face, raycast_blocks, type Shape } from "$/common/block_shapes.ts"; +import { collision_shape_at, covers_face, is_solid, raycast_blocks, type Shape } from "$/common/block_shapes.ts"; import { AIR, CHUNK_SIZE, faces, ID_MASK } from "$/common/constants.ts"; import { physics } from "$/common/physics.ts"; import { clock } from "$/common/time.ts"; @@ -919,6 +919,12 @@ export class ModRuntime { if (!block) throw new Error(`unknown block ${id}`); return structuredClone(block.components ?? {}); }, + solid: (id) => { + if (id === AIR_ID) return false; + const block = EverythingRegistry.get("blocks", id); + if (!block) throw new Error(`unknown block ${id}`); + return is_solid(block); + }, }, recipes: { furnace_result: (input) => { diff --git a/tests/block_shapes_test.ts b/tests/block_shapes_test.ts index cd20072..2ae39a6 100644 --- a/tests/block_shapes_test.ts +++ b/tests/block_shapes_test.ts @@ -9,11 +9,12 @@ import { collision_shape_at, EMPTY_SHAPE, FULL_SHAPE, + is_solid, model_shape, raycast_blocks, type Shape, } from "$/common/block_shapes.ts"; -import { block_from_json, validate_block } from "$/common/mod_data.ts"; +import { block_from_json, type BlockJson, validate_block } from "$/common/mod_data.ts"; import { move_body } from "$/common/physics.ts"; import { box_overlaps_block } from "$/common/utils.ts"; @@ -173,3 +174,24 @@ Deno.test("an x turn turns a model's shape with it", () => { // then y turns the south half to the west assertEquals(model_shape(slab, 90, 90), [[0, 0, 0, 0.5, 1, 1]]); }); + +Deno.test("a solid block is drawn in the solid layer and is a whole block to bump into, whatever its states", () => { + const block = (json: Partial) => + block_from_json({ id: "test:block", textures: "test:stone", ...json }).block; + const states = [{ name: "age", bits: 1, default: 0 }]; + assert(is_solid(block({}))); + assert( + is_solid(block({ states, variants: { "age=1": { y: 90, textures: "test:mossy" } } })), + "turned, other textures", + ); + assert(!is_solid(block({ render_layer: "cutout" })), "like leaves"); + assert(!is_solid(block({ render_layer: "translucent" })), "like glass"); + assert(!is_solid(block({ collision: false }))); + assert(!is_solid(block({ shape: [[0, 0, 0, 16, 8, 16]] })), "like a slab"); + assert(!is_solid(block({ states, variants: { "age=1": { model: "engine:cross" } } })), "not in one of its states"); + assert(!is_solid(block({ states, variants: { "age=1": { collision_shape: [[0, 0, 0, 16, 8, 16]] } } }))); + assert( + is_solid(block({ shape: [[0, 0, 0, 16, 8, 16]], collision_shape: [[0, 0, 0, 16, 16, 16]] })), + "what it bumps into counts", + ); +}); diff --git a/tests/server_scripts_test.ts b/tests/server_scripts_test.ts index f2aaa1c..dae37ad 100644 --- a/tests/server_scripts_test.ts +++ b/tests/server_scripts_test.ts @@ -99,6 +99,16 @@ export function setup(ctx: ServerContext) { }, }); + ctx.commands.register("solid", { + description: "", usage: "", + run([id], player) { + try { + player.send_message("solid " + ctx.blocks.solid(id)); + } catch (e) { + player.send_message("error " + (e as Error).message); + } + }, + }); ctx.commands.register("loaded", { description: "", usage: "", run([x, z], player) { player.send_message("loaded " + ctx.world.is_loaded(Number(x), Number(z))); }, @@ -325,6 +335,19 @@ Deno.test("is_loaded says which chunks were ever generated, saved ones too, with Deno.removeSync(again.dir, { recursive: true }); }); +Deno.test("ctx.blocks.solid says which blocks are solid, air isn't, and unknown blocks throw", async () => { + const { send, take, dir } = await setup(); + const solid = (id: string) => { + send(1, { type: "chat", text: `/solid ${id}` }); + return take(1).filter((m) => m.type === "chat").map((m) => m.text).at(-1); + }; + assertEquals(solid("bworld:stone"), "solid true"); + assertEquals(solid("testmod:lamp"), "solid true"); + assertEquals(solid("bworld:air"), "solid false"); + assertEquals(solid("nobody:block"), "error unknown block nobody:block"); + Deno.removeSync(dir, { recursive: true }); +}); + Deno.test("inventory on_change fires for the slots that changed", async () => { const { game, send, log, dir } = await setup(); send(1, { type: "chat", text: "/give bworld:stone 3" }); diff --git a/tests/shared_rules_test.ts b/tests/shared_rules_test.ts index 7ca21c3..882142c 100644 --- a/tests/shared_rules_test.ts +++ b/tests/shared_rules_test.ts @@ -5,7 +5,7 @@ // a block is what the server ends up with import { assertEquals } from "@std/assert"; import { BlockRules } from "$/common/block_rules.ts"; -import { collision_shape_at, covers_face } from "$/common/block_shapes.ts"; +import { collision_shape_at, covers_face, is_solid } from "$/common/block_shapes.ts"; import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts"; import { tag_api } from "$/common/tags.ts"; import { AIR_ID } from "$/common/protocol.ts"; @@ -80,6 +80,7 @@ function client_of(t: Game) { blocks: { exists: (id) => info(id) !== undefined, components: (id) => structuredClone(info(id)!.components ?? {}), + solid: (id) => id !== AIR_ID && is_solid(info(id)!), }, world, components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) },