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 { register_coilhead, register_coilhead_animator } from "./coilhead.ts"; import { register_jester, register_jester_animator } from "./jester.ts"; import { register_scrap } from "./scrap.ts"; import { DOORWAY, MAIN_TILE, type Marker, type PlacedTile, plan_interior, SURFACE_Y, 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); register_coilhead(ctx); register_jester(ctx); register_scrap(ctx); } export function client(ctx: ClientContext) { register_hud(ctx); register_coilhead_animator(ctx); register_jester_animator(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(); 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(); 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[] = []; // 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) { 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) { ctx.schematics.place( "dangerous_business:assurance", { position: { x: -80, y: SURFACE_Y, 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:concrete_brick_wall"; // how likely a vent spot is to get a vent, otherwise it's left empty const VENT_CHANCE = 0.65; const VENT = "dangerous_business:vent"; 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) { const door = connected && rng.next() < DOOR_CHANCE; if (!connected || door) wall_off_doorway(ctx, x, y, z, facing, door); if (door) { 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 if (connected) { ctx.world.set_block(x, y, z, "cubi:air"); } } interior_vents = []; for (const { x, y, z, facing } of plan.vent_spots) { if (rng.next() < VENT_CHANCE) { ctx.world.set_block(x, y, z, VENT, { facing }); interior_vents.push({ x, y, z, facing }); } else { ctx.world.set_block(x, y, z, "cubi:air"); } } interior_tiles = plan.tiles; } // fills the opening a doorway is in, 3 wide and 4 tall like lethal3's or 1 by 2, leaving the doorway itself when a // door goes there. only air and the doorway are filled, so the wall around a smaller opening stays as it is function wall_off_doorway(ctx: ServerContext, x: number, y: number, z: number, facing: number, door: boolean) { // along the wall const [step_x, step_z] = facing % 2 === 0 ? [1, 0] : [0, 1]; for (let side = -1; side <= 1; side++) { for (let up = 0; up < 4; up++) { if (door && side === 0 && up < 2) continue; const [at_x, at_y, at_z] = [x + side * step_x, y + up, z + side * step_z]; const id = ctx.world.get_block(at_x, at_y, at_z); if (id === "cubi:air" || id === DOORWAY) ctx.world.set_block(at_x, at_y, at_z, DOORWAY_WALL); } } }