134 lines
4.8 KiB
TypeScript
134 lines
4.8 KiB
TypeScript
import type { ClientContext } from "cubi/client";
|
|
import type { ServerContext } from "cubi/server";
|
|
import { DOOR, FACILITY_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) {
|
|
register_facility_door(ctx);
|
|
register_door(ctx);
|
|
register_commands(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_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);
|
|
// it took its open state from the lower half, but it can be the half that's powered
|
|
if (ctx.world.get_state(x, y + 1, z, "powered")) {
|
|
ctx.world.set_state(x, y, z, "open", 1);
|
|
ctx.world.set_state(x, y + 1, z, "open", 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);
|
|
}
|
|
},
|
|
on_state_change(block, _params, before) {
|
|
const { x, y, z } = block;
|
|
const powered = ctx.world.get_state(x, y, z, "powered");
|
|
if (powered === before.powered) return;
|
|
const other = ctx.world.get_state(x, y, z, "half") ? y - 1 : y + 1;
|
|
const paired = ctx.world.get_block(x, other, z) === block.id;
|
|
const other_powered = paired && ctx.world.get_state(x, other, z, "powered") === 1;
|
|
const open = powered === 1 || other_powered;
|
|
if (open === (before.powered === 1 || other_powered)) return;
|
|
ctx.world.set_state(x, y, z, "open", open ? 1 : 0);
|
|
if (paired) ctx.world.set_state(x, other, z, "open", open ? 1 : 0);
|
|
},
|
|
});
|
|
}
|
|
|
|
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);
|
|
context.source.send_message("A new day starts");
|
|
}),
|
|
);
|
|
}
|
|
|
|
const interior_tiles = [];
|
|
|
|
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 } },
|
|
);
|
|
}
|
|
|
|
function generate_interior(ctx: ServerContext) {
|
|
}
|