From c2e885f23a0084adb1fa4dfc4897b2547927744e Mon Sep 17 00:00:00 2001 From: Paula Date: Sun, 22 Nov 2026 11:22:07 -0300 Subject: [PATCH] Fill on serverworld and clear out tiles on new day --- common/mod_api/server.ts | 3 +++ docs/modding/blocks/index.md | 14 ++++++++++++++ mods/dangerous_business/scripts/interior.ts | 9 ++++++--- mods/dangerous_business/scripts/main.ts | 7 ++++++- server/game/game_server.ts | 20 +++++++++----------- server/game/mod_runtime.ts | 4 ++++ tests/dangerous_business_interior_test.ts | 8 ++++++-- tests/machines_test.ts | 17 ++++++++++++++++- 8 files changed, 64 insertions(+), 18 deletions(-) diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index d9c78b5..22f0e1f 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -437,6 +437,9 @@ export interface ServerWorld { // runs on_break / on_create, synced to everyone. states are the ones it's placed with, the rest keep their defaults, // like minecraft's setBlock with a block state: a shaft turned into an encased one keeps its axis set_block(x: number, y: number, z: number, id: Id, states?: Record): boolean; + // every block between two corners, both included, set to id like set_block, like a schematic all at once: they all + // change before any of them hears of it. parts above or below the world are left out. says how many it changed + fill(from: Position, to: Position, id: Id): number; // like breaking it by hand without a tool: on_break, then its drops, which it returns (copies). undefined when there // was nothing to break. see BreakOptions for machines breaking it break_block(x: number, y: number, z: number, options?: BreakOptions): ItemStack[] | undefined; diff --git a/docs/modding/blocks/index.md b/docs/modding/blocks/index.md index 6174f1c..83a75ff 100644 --- a/docs/modding/blocks/index.md +++ b/docs/modding/blocks/index.md @@ -117,6 +117,20 @@ replaced. Players who load the chunk later don't see cracks set before, so keep cobblestone. `collect` returns the drops without dropping them, for a machine that puts them somewhere, like onto a belt. +### Filling From Scripts + +`ctx.world.fill(from, to, id)` sets every block in the box between two corners, both included, like `set_block` on each, +and says how many it changed. Like [placing a schematic](../misc/schematics.md), they all change before any of them +hears of it, so a door's halves or a torch's wall are gone together. It's for clearing or walling off a whole area, like +an interior before the next one goes in: + +```ts +ctx.world.fill({ x: 0, y: 60, z: 0 }, { x: 31, y: 79, z: 31 }, "cubi:air"); +``` + +Parts above or below the world are left out, and ungenerated chunks it reaches are generated first. `/fill` does the +same, up to 32768 blocks at a time. + ## Placement Players place a block by right clicking a face of another block with its item. It goes next to the face they clicked, or diff --git a/mods/dangerous_business/scripts/interior.ts b/mods/dangerous_business/scripts/interior.ts index c430332..1880621 100644 --- a/mods/dangerous_business/scripts/interior.ts +++ b/mods/dangerous_business/scripts/interior.ts @@ -51,6 +51,8 @@ export interface PlacedTile { // where the schematic's lowest corner goes, before it's turned around it position: Position; rotation: 0 | 90 | 180 | 270; + // the blocks it takes up once it's turned, its outer walls too + box: Box; } // a doorway in the world, once. connected when it has a tile on both sides, otherwise it leads nowhere and gets @@ -131,7 +133,7 @@ export function plan_interior(rng: { next(): number }, types: TileType[], config } // the blocks a placed tile takes up, both corners in it -interface Box { +export interface Box { min: [number, number, number]; max: [number, number, number]; } @@ -148,8 +150,9 @@ function try_plan(rng: { next(): number }, types: TileType[], config: InteriorPl const used = new Set(); const add = (type: TileType, position: Position, quarters: number) => { - tiles.push({ tile: type.id, position, rotation: quarters * 90 as PlacedTile["rotation"] }); - boxes.push(tile_box(type, position, quarters)); + const box = tile_box(type, position, quarters); + tiles.push({ tile: type.id, position, rotation: quarters * 90 as PlacedTile["rotation"], box }); + boxes.push(box); if (type.one_time) used.add(type.id); // a marker of this tile where it is in the world const placed_marker = (marker: Marker) => { diff --git a/mods/dangerous_business/scripts/main.ts b/mods/dangerous_business/scripts/main.ts index 50bd8d1..9cc68e1 100644 --- a/mods/dangerous_business/scripts/main.ts +++ b/mods/dangerous_business/scripts/main.ts @@ -147,8 +147,13 @@ let interior_tiles: PlacedTile[] = []; // the vents placed in the interior, for monsters to come out of let interior_vents: Marker[] = []; +// takes away the last interior, so the next one goes in where there's nothing. the surface is placed over itself function clear_map(ctx: ServerContext) { - // theres no need to clear the surface but we do need to clear each tile + for (const { box: { min: [x0, y0, z0], max: [x1, y1, z1] } } of interior_tiles) { + ctx.world.fill({ x: x0, y: y0, z: z0 }, { x: x1, y: y1, z: z1 }, "cubi:air"); + } + interior_tiles = []; + interior_vents = []; } function place_surface(ctx: ServerContext) { diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 599e850..d9228aa 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -969,16 +969,12 @@ export class GameServer { } } - // /fill: every block between two corners, both included, replaced with id. like a schematic, they all change before - // any of them hears of it. throws for a box that's too big, and says how many blocks it changed + // /fill and ctx.world.fill: every block between two corners, both included, replaced with id. like a schematic, they + // all change before any of them hears of it. says how many blocks it changed fill(from: Position, to: Position, id: string): number { const [x0, x1] = [Math.min(from.x, to.x), Math.max(from.x, to.x)]; const [y0, y1] = [Math.min(from.y, to.y), Math.max(from.y, to.y)]; const [z0, z1] = [Math.min(from.z, to.z), Math.max(from.z, to.z)]; - const volume = (x1 - x0 + 1) * (y1 - y0 + 1) * (z1 - z0 + 1); - if (volume > MAX_FILL_VOLUME) { - throw new Error(`fill changes up to ${MAX_FILL_VOLUME} blocks, and that box has ${volume}`); - } const placed: Position[] = []; for (let y = y0; y <= y1; y++) { if (!this.world.shape.contains(y)) continue; @@ -1969,12 +1965,14 @@ export class GameServer { argument("block", block_or_air()).executes((context) => { const [from, to] = corners(context); const id = context.get("block"); - let count: number; - try { - count = this.fill(from, to, id); - } catch (e) { - throw new CommandError(`Couldn't fill it: ${(e as Error).message}`); + const volume = (Math.abs(to.x - from.x) + 1) * (Math.abs(to.y - from.y) + 1) * + (Math.abs(to.z - from.z) + 1); + if (volume > MAX_FILL_VOLUME) { + throw new CommandError( + `Couldn't fill it: fill changes up to ${MAX_FILL_VOLUME} blocks, and that box has ${volume}`, + ); } + const count = this.fill(from, to, id); context.source.send_message(`Filled ${count} blocks with ${display_name(id)}`); }), )), diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index c45c5fe..3ac204c 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -911,6 +911,10 @@ export class ModRuntime { game().set_block(x, y, z, id, undefined, state); return true; }, + fill: (from, to, id) => { + if (id !== AIR_ID && !EverythingRegistry.get("blocks", id)) throw new Error(`unknown block ${id}`); + return game().fill(from, to, id); + }, break_block: (x, y, z, options = {}) => { if (game().world.get_block_id(x, y, z) === AIR_ID) return undefined; const how = { harvest: options.harvest === true, collect: options.collect === true }; diff --git a/tests/dangerous_business_interior_test.ts b/tests/dangerous_business_interior_test.ts index 92b91e7..1163a1b 100644 --- a/tests/dangerous_business_interior_test.ts +++ b/tests/dangerous_business_interior_test.ts @@ -67,14 +67,18 @@ function placed(plan: InteriorPlan) { Deno.test("an interior has as many rooms as it's asked for when there's room, starting from main", () => { const { tiles } = plan("rooms"); assertEquals(tiles.length, 30); - assertEquals(tiles[0], { tile: "test:main", position: { x: 100, y: 50, z: -20 }, rotation: 0 }); + const { tile, position, rotation } = tiles[0]; + assertEquals({ tile, position, rotation }, { tile: "test:main", position: { x: 100, y: 50, z: -20 }, rotation: 0 }); assertEquals(tiles.filter((tile) => tile.tile === "test:main").length, 1); }); Deno.test("rooms only share their outer walls", () => { for (const seed of ["a", "b", "c", "d", "e"]) { - const tiles = placed(plan(seed)); + const result = plan(seed); + const tiles = placed(result); for (let i = 0; i < tiles.length; i++) { + // the box the plan keeps is the one its schematic covers + assertEquals(result.tiles[i].box, { min: tiles[i].min, max: tiles[i].max }); for (let j = i + 1; j < tiles.length; j++) { const [a, b] = [tiles[i], tiles[j]]; const inside = [0, 1, 2].every((axis) => a.min[axis] < b.max[axis] && b.min[axis] < a.max[axis]); diff --git a/tests/machines_test.ts b/tests/machines_test.ts index 880f6b2..e2980b7 100644 --- a/tests/machines_test.ts +++ b/tests/machines_test.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // what machines do to the world through server phases: read which way water flows, find items lying around and -// change them, push things, break blocks over time, and use items on blocks like a player would +// change them, push things, break blocks over time, use items on blocks like a player would, and fill whole boxes import { assert, assertEquals, assertThrows } from "@std/assert"; import type { ServerContext } from "cubi/server"; import { AIR_ID } from "@/common/protocol.ts"; @@ -144,6 +144,21 @@ Deno.test("a block breaking over time shows its cracks to players, and machines assertEquals(ctx.world.break_block(2, 99, 0), undefined); }); +Deno.test("a script fills every block between two corners, both included, however big the box", async () => { + const { game, ctx } = await setup(); + // corners in any order, and bigger than /fill takes + assertEquals(ctx.world.fill({ x: 40, y: 99, z: 40 }, { x: 0, y: 40, z: 0 }, "bworld:stone"), 41 * 60 * 41); + assertEquals(ctx.world.fill({ x: 1, y: 98, z: 3 }, { x: 0, y: 99, z: 2 }, "cubi:air"), 8); + for (const [x, y, z] of [[0, 98, 2], [1, 99, 3], [0, 99, 3]]) { + assertEquals(game.world.get_block_id(x, y, z), AIR_ID); + } + for (const [x, y, z] of [[2, 99, 3], [1, 97, 3], [1, 99, 4], [40, 40, 40]]) { + assertEquals(game.world.get_block_id(x, y, z), "bworld:stone"); + } + assertThrows(() => ctx.world.fill({ x: 0, y: 99, z: 0 }, { x: 1, y: 99, z: 1 }, "bworld:nothing")); + assertEquals(game.world.get_block_id(0, 99, 0), "bworld:stone"); +}); + Deno.test("a machine uses items on blocks like a player holding them would", async () => { const { game, ctx } = await setup(); // placing: the block goes in front of the face, and one fewer is left