Fill on serverworld and clear out tiles on new day
This commit is contained in:
@@ -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<string, number>): 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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<string>();
|
||||
|
||||
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) => {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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<string, ServerCommandSource>("block", block_or_air()).executes((context) => {
|
||||
const [from, to] = corners(context);
|
||||
const id = context.get<string>("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)}`);
|
||||
}),
|
||||
)),
|
||||
|
||||
@@ -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 };
|
||||
|
||||
@@ -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]);
|
||||
|
||||
+16
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user