Files
bworld/mods/dangerous_business/scripts/main.ts
T
2026-11-17 22:37:20 -03:00

178 lines
6.3 KiB
TypeScript

import type { ClientContext, Color } from "cubi/client";
import type { ServerContext } from "cubi/server";
import { DOOR, FACILITY_DOOR } from "./shared.ts";
import { Alea } from "@paulaboks/rng";
import { MAIN_TILE, type PlacedTile, plan_interior, tile_type, TILES } from "./interior.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) {
register_facility_door(ctx);
register_door(ctx);
register_death(ctx);
register_commands(ctx);
}
export function client(ctx: ClientContext) {
register_hud(ctx);
}
// a small 5x5 ring in the middle of the screen to aim with, its corners and middle left empty, in gui pixels
function register_hud(ctx: ClientContext) {
const color: Color = [1, 1, 1, 1];
ctx.hud.register("dangerous_business:crosshair", {
on_render(g) {
const x = Math.floor(g.width / 2) - 2;
const y = Math.floor(g.height / 2) - 2;
g.rect(x + 1, y, 3, 1, color);
g.rect(x + 1, y + 4, 3, 1, color);
g.rect(x, y + 1, 1, 3, color);
g.rect(x + 4, y + 1, 1, 3, color);
},
});
}
// 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_facility_door(ctx: ServerContext) {
const is_door = (id: string) => FACILITY_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<string>();
ctx.components.register_block(FACILITY_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);
}
},
});
}
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", "open", "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<string>();
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);
}
},
});
}
// an invisible block that kills whatever walks or falls into it, for the bottoms of pits and the like
function register_death(ctx: ServerContext) {
ctx.components.register_block("dangerous_business:death", {
on_entity_inside(_block, _params, entity) {
entity.kill("death_block");
},
});
}
function register_commands(ctx: ServerContext) {
const { literal, argument, types } = ctx.commands;
ctx.commands.register(
literal("startday")
.describe("Starts a new day")
.executes((context) => {
clear_map(ctx);
place_surface(ctx);
generate_interior(ctx, { size_multiplier: 1 });
context.source.send_message("A new day starts");
}),
);
}
let interior_tiles: PlacedTile[] = [];
function clear_map(ctx: ServerContext) {
// theres no need to clear the surface but we do need to clear each tile
}
function place_surface(ctx: ServerContext) {
ctx.schematics.place(
"dangerous_business:assurance",
{ position: { x: -80, y: 150, z: -80 } },
);
}
// where main's lowest corner goes, under the surface
const INTERIOR_ORIGIN = { x: 0, y: 60, z: 0 };
// how likely a doorway between two rooms is to get a door, otherwise it's left open
const DOOR_CHANCE = 0.8;
const INTERIOR_DOOR = "dangerous_business:factory_door";
const DOORWAY_WALL = "dangerous_business:sheet_wall";
interface InteriorGenerationConfig {
size_multiplier: number;
}
function generate_interior(ctx: ServerContext, config: InteriorGenerationConfig) {
const seed = String(Date.now());
ctx.log(`interior seed ${seed}`);
const rng = new Alea(seed);
const types = Object.entries(TILES).map(([id, rules]) => tile_type(ctx.schematics.get(id)!, rules));
const rooms = Math.floor((21 + rng.next() * 9) * config.size_multiplier);
const plan = plan_interior(rng, types, { main: MAIN_TILE, origin: INTERIOR_ORIGIN, rooms });
for (const { tile, position, rotation } of plan.tiles) {
ctx.schematics.place(tile, { position, rotation });
}
for (const { x, y, z, facing, connected } of plan.doorways) {
if (!connected) {
ctx.world.set_block(x, y, z, DOORWAY_WALL);
ctx.world.set_block(x, y + 1, z, DOORWAY_WALL);
} else if (rng.next() < DOOR_CHANCE) {
ctx.world.set_block(x, y, z, INTERIOR_DOOR, { facing, half: 0 });
ctx.world.set_block(x, y + 1, z, INTERIOR_DOOR, { facing, half: 1 });
} else {
ctx.world.set_block(x, y, z, "cubi:air");
}
}
interior_tiles = plan.tiles;
}