diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index bdf7ba2..8c70090 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -627,7 +627,7 @@ export interface BlockApi { // how long breaking it by hand takes without a tool, in seconds, from the block's mining. Infinity for one that // can't be broken. for machines that break blocks, like a drill toughness(id: Id): number; - // the components its json lists and their params, a copy, like ItemApi.components + // the components its json lists and their params, a copy, like ItemApi.components. air has none 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 diff --git a/common/shared_context.ts b/common/shared_context.ts index 2fd66cb..8af4d19 100644 --- a/common/shared_context.ts +++ b/common/shared_context.ts @@ -61,7 +61,7 @@ export function blocks_api(): SharedContext["blocks"] { }; return { exists: (id) => EverythingRegistry.get("blocks", id) !== undefined, - components: (id) => structuredClone(known(id).components ?? {}), + components: (id) => id === AIR_ID ? {} : structuredClone(known(id).components ?? {}), solid: (id) => id !== AIR_ID && is_solid(known(id)), }; } diff --git a/mods/dangerous_business/blocks/facility_door.json b/mods/dangerous_business/blocks/facility_door.json new file mode 100644 index 0000000..b112d2f --- /dev/null +++ b/mods/dangerous_business/blocks/facility_door.json @@ -0,0 +1,85 @@ +{ + "format_version": 1, + "block": { + "id": "dangerous_business:facility_door", + "name": "Facility Door", + "textures": { + "item": "dangerous_business:facility_door", + "bottom": "dangerous_business:facility_door_bottom", + "top": "dangerous_business:facility_door_top" + }, + "model": "dangerous_business:door_bottom", + "render_layer": "cutout", + "states": [ + { + "name": "facing", + "bits": 2, + "default": 0 + }, + { + "name": "half", + "bits": 1, + "default": 0 + }, + { + "name": "hinge", + "bits": 1, + "default": 0 + } + ], + "variants": { + "half=0,hinge=1": { + "model": "dangerous_business:door_bottom_rh" + }, + "half=1,hinge=0": { + "model": "dangerous_business:door_top_rh" + }, + "half=1,hinge=1": { + "model": "dangerous_business:door_top_rh" + }, + "half=1": { + "model": "dangerous_business:door_top" + }, + "facing=0": { + "y": 270 + }, + "facing=0,hinge=0": { + "y": 0 + }, + "facing=0,hinge=1": { + "y": 180 + }, + "facing=1": { + "y": 0 + }, + "facing=1,hinge=0": { + "y": 90 + }, + "facing=1,hinge=1": { + "y": 270 + }, + "facing=2": { + "y": 90 + }, + "facing=2,hinge=0": { + "y": 180 + }, + "facing=2,hinge=1": { + "y": 0 + }, + "facing=3": { + "y": 180 + }, + "facing=3,hinge=0": { + "y": 270 + }, + "facing=3,hinge=1": { + "y": 90 + } + }, + "interactive": true, + "components": { + "dangerous_business:door": {} + } + } +} diff --git a/mods/dangerous_business/models/door_bottom.json b/mods/dangerous_business/models/door_bottom.json new file mode 100644 index 0000000..943edfa --- /dev/null +++ b/mods/dangerous_business/models/door_bottom.json @@ -0,0 +1,38 @@ +{ + "format_version": 1, + "model": { + "id": "dangerous_business:door_bottom", + "elements": [ + { + "from": [0, 0, 0], + "to": [3, 16, 16], + "faces": { + "bottom": { + "texture": "#bottom", + "uv": [13, 0, 16, 16], + "cullface": "bottom" + }, + "north": { + "texture": "#bottom", + "uv": [3, 0, 0, 16], + "cullface": "north" + }, + "south": { + "texture": "#bottom", + "uv": [0, 0, 3, 16], + "cullface": "south" + }, + "west": { + "texture": "#bottom", + "uv": [0, 0, 16, 16], + "cullface": "west" + }, + "east": { + "texture": "#bottom", + "uv": [16, 0, 0, 16] + } + } + } + ] + } +} diff --git a/mods/dangerous_business/models/door_bottom_rh.json b/mods/dangerous_business/models/door_bottom_rh.json new file mode 100644 index 0000000..b2f768c --- /dev/null +++ b/mods/dangerous_business/models/door_bottom_rh.json @@ -0,0 +1,38 @@ +{ + "format_version": 1, + "model": { + "id": "dangerous_business:door_bottom_rh", + "elements": [ + { + "from": [0, 0, 0], + "to": [3, 16, 16], + "faces": { + "bottom": { + "texture": "#bottom", + "uv": [13, 0, 16, 16], + "cullface": "bottom" + }, + "north": { + "texture": "#bottom", + "uv": [3, 0, 0, 16], + "cullface": "north" + }, + "south": { + "texture": "#bottom", + "uv": [0, 0, 3, 16], + "cullface": "south" + }, + "west": { + "texture": "#bottom", + "uv": [16, 0, 0, 16], + "cullface": "west" + }, + "east": { + "texture": "#bottom", + "uv": [0, 0, 16, 16] + } + } + } + ] + } +} diff --git a/mods/dangerous_business/models/door_top.json b/mods/dangerous_business/models/door_top.json new file mode 100644 index 0000000..a8ca33d --- /dev/null +++ b/mods/dangerous_business/models/door_top.json @@ -0,0 +1,38 @@ +{ + "format_version": 1, + "model": { + "id": "dangerous_business:door_top", + "elements": [ + { + "from": [0, 0, 0], + "to": [3, 16, 16], + "faces": { + "top": { + "texture": "#bottom", + "uv": [13, 0, 16, 16], + "cullface": "top" + }, + "north": { + "texture": "#top", + "uv": [3, 0, 0, 16], + "cullface": "north" + }, + "south": { + "texture": "#top", + "uv": [0, 0, 3, 16], + "cullface": "south" + }, + "west": { + "texture": "#top", + "uv": [0, 0, 16, 16], + "cullface": "west" + }, + "east": { + "texture": "#top", + "uv": [16, 0, 0, 16] + } + } + } + ] + } +} diff --git a/mods/dangerous_business/models/door_top_rh.json b/mods/dangerous_business/models/door_top_rh.json new file mode 100644 index 0000000..6bd9ec5 --- /dev/null +++ b/mods/dangerous_business/models/door_top_rh.json @@ -0,0 +1,38 @@ +{ + "format_version": 1, + "model": { + "id": "dangerous_business:door_top_rh", + "elements": [ + { + "from": [0, 0, 0], + "to": [3, 16, 16], + "faces": { + "top": { + "texture": "#bottom", + "uv": [13, 0, 16, 16], + "cullface": "top" + }, + "north": { + "texture": "#top", + "uv": [3, 0, 0, 16], + "cullface": "north" + }, + "south": { + "texture": "#top", + "uv": [0, 0, 3, 16], + "cullface": "south" + }, + "west": { + "texture": "#top", + "uv": [16, 0, 0, 16], + "cullface": "west" + }, + "east": { + "texture": "#top", + "uv": [0, 0, 16, 16] + } + } + } + ] + } +} diff --git a/mods/dangerous_business/scripts/main.ts b/mods/dangerous_business/scripts/main.ts index 79ff004..0f3427a 100644 --- a/mods/dangerous_business/scripts/main.ts +++ b/mods/dangerous_business/scripts/main.ts @@ -1,10 +1,51 @@ import type { ClientContext } from "cubi/client"; import type { ServerContext } from "cubi/server"; +import { DOOR } from "./shared.ts"; + +export { shared } from "./shared.ts"; // a standalone game that doesn't build on bworld, so everything it needs comes from this mod or the engine -export function server(_ctx: ServerContext) { +export function server(ctx: ServerContext) { + register_door(ctx); } export function client(_ctx: ClientContext) { } + +// doors are two blocks, a lower and an upper half, see register_door in shared.ts. one is only placed with air above +// it, where the upper half goes, and breaking either half takes the other away with it. only the half that was broken +// drops the door, the other is just taken away +function register_door(ctx: ServerContext) { + const is_door = (id: string) => DOOR in ctx.blocks.components(id); + ctx.events.before.block_place.subscribe((event) => { + const { id, x, y, z } = event.block; + if (is_door(id) && ctx.world.get_block(x, y + 1, z) !== "cubi:air") event.cancel = true; + }); + ctx.events.after.block_place.subscribe(({ block: { id, x, y, z } }) => { + if (!is_door(id)) return; + ctx.world.set_block(x, y + 1, z, id); + for (const state of ["facing", "hinge"]) { + ctx.world.set_state(x, y + 1, z, state, ctx.world.get_state(x, y, z, state) ?? 0); + } + ctx.world.set_state(x, y + 1, z, "half", 1); + }); + + // the halves being taken away, so the other half's on_break doesn't take the first away again + const breaking = new Set(); + ctx.components.register_block(DOOR, { + on_break(block) { + const { x, y, z } = block; + if (breaking.has(`${x},${y},${z}`)) return; + const other = ctx.world.get_state(x, y, z, "half") ? y - 1 : y + 1; + if (ctx.world.get_block(x, other, z) !== block.id) return; + const halves = [`${x},${y},${z}`, `${x},${other},${z}`]; + for (const half of halves) breaking.add(half); + try { + ctx.world.set_block(x, other, z, "cubi:air"); + } finally { + for (const half of halves) breaking.delete(half); + } + }, + }); +} diff --git a/mods/dangerous_business/scripts/shared.ts b/mods/dangerous_business/scripts/shared.ts new file mode 100644 index 0000000..4492e03 --- /dev/null +++ b/mods/dangerous_business/scripts/shared.ts @@ -0,0 +1,37 @@ +import type { SharedContext } from "cubi/shared"; + +export const DOOR = "dangerous_business:door"; + +const FACINGS = ["north", "east", "south", "west"]; +// the way along the ground each facing goes, in FACINGS' order +const FACING_STEPS = [[0, -1], [1, 0], [0, 1], [-1, 0]]; + +export function shared(ctx: SharedContext) { + register_door(ctx); +} + +// a door is two blocks, its lower half (half 0) and its upper half (half 1), with the same states. it faces the way the +// player placing it looks, and is hinged on the side away from a door of the same kind beside it, so the two meet in +// the middle as a double door. without one, on the side of the block the player clicked toward. the server places the +// upper half and takes away the other half when one breaks, see register_door in main.ts +function register_door(ctx: SharedContext) { + ctx.components.register_block(DOOR, { + state_for_placement(placement): Record { + const facing = FACINGS.indexOf(placement.facing); + const { x, y, z } = placement; + const door_at = ([step_x, step_z]: number[]) => { + const id = ctx.world.get_block(x + step_x, y, z + step_z); + return id !== undefined && DOOR in ctx.blocks.components(id) && + ctx.world.get_state(x + step_x, y, z + step_z, "half") === 0; + }; + const door_left = door_at(FACING_STEPS[(facing + 3) % 4]); + const door_right = door_at(FACING_STEPS[(facing + 1) % 4]); + if (door_left !== door_right) return { facing, half: 0, hinge: door_left ? 1 : 0 }; + const [step_x, step_z] = FACING_STEPS[facing]; + const [hit_x, , hit_z] = placement.hit; + const right = (step_x < 0 && hit_z < 0.5) || (step_x > 0 && hit_z > 0.5) || + (step_z < 0 && hit_x > 0.5) || (step_z > 0 && hit_x < 0.5); + return { facing, half: 0, hinge: right ? 1 : 0 }; + }, + }); +} diff --git a/mods/dangerous_business/textures/facility_door.png b/mods/dangerous_business/textures/facility_door.png new file mode 100644 index 0000000..10c9a52 Binary files /dev/null and b/mods/dangerous_business/textures/facility_door.png differ diff --git a/mods/dangerous_business/textures/facility_door_bottom.png b/mods/dangerous_business/textures/facility_door_bottom.png new file mode 100644 index 0000000..2af136d Binary files /dev/null and b/mods/dangerous_business/textures/facility_door_bottom.png differ diff --git a/mods/dangerous_business/textures/facility_door_top.png b/mods/dangerous_business/textures/facility_door_top.png new file mode 100644 index 0000000..00cb756 Binary files /dev/null and b/mods/dangerous_business/textures/facility_door_top.png differ