Files
bworld/mods/bworld/scripts/shared.ts
T
2026-11-01 21:00:44 -03:00

208 lines
9.4 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the base game's block rules: how blocks are placed and how they change with the blocks beside them. they run on the
// server and on every client, so players see a fence join or a trapdoor turn the moment they place it, and a trapdoor
// or a door open the moment they click it. and the rules of juice and wine, which name and color them by the item
// they're from, and the recipes that make them. and the base game's recipe types
import type { SharedBlock, SharedContext } from "bworld/shared";
import { register_cooking_recipes } from "./cooking_recipes.ts";
import { register_fermenting } from "./fermenting.ts";
import { register_juicing } from "./juicing.ts";
import { register_smelting } from "./smelting.ts";
import { register_smithing } from "./smithing.ts";
import { register_stonecutting } from "./stonecutting.ts";
import { register_verdigris_rules } from "./verdigris/shared.ts";
export function setup(ctx: SharedContext) {
register_fence(ctx);
register_trapdoor(ctx);
register_door(ctx);
register_verdigris_rules(ctx);
register_juice_and_wine(ctx);
register_juicing(ctx);
register_fermenting(ctx);
register_smithing(ctx);
register_smelting(ctx);
register_stonecutting(ctx);
register_cooking_recipes(ctx);
}
// the sides a fence connects on, and the face of the block there it connects to
const FENCE_SIDES = [
{ state: "north", dx: 0, dz: -1, face: "south" },
{ state: "east", dx: 1, dz: 0, face: "west" },
{ state: "south", dx: 0, dz: 1, face: "north" },
{ state: "west", dx: -1, dz: 0, face: "east" },
] as const;
// a fence joins the fences beside it and the full sides of solid blocks, like minecraft's FenceBlock.connectsTo.
// "connects_to" is the tag of fences it joins, so wooden fences don't join other kinds. leaves are full blocks but
// fences don't join them, like minecraft
function register_fence(ctx: SharedContext) {
const sides = (x: number, y: number, z: number, params: { connects_to: string }) => {
const states: Record<string, number> = {};
for (const side of FENCE_SIDES) {
const [nx, nz] = [x + side.dx, z + side.dz];
const id = ctx.world.get_block(nx, y, nz);
const joins = id !== undefined && (
ctx.tags.has("blocks", params.connects_to, id) ||
(!ctx.tags.has("blocks", "bworld:leaves", id) && ctx.world.is_face_sturdy(nx, y, nz, side.face))
);
states[side.state] = joins ? 1 : 0;
}
return states;
};
ctx.components.register_block<{ connects_to: string }>("bworld:fence", {
state_for_placement: (placement, params) => sides(placement.x, placement.y, placement.z, params),
on_neighbor_change(block: SharedBlock, params) {
for (const [state, value] of Object.entries(sides(block.x, block.y, block.z, params))) {
ctx.world.set_state(block.x, block.y, block.z, state, value);
}
},
});
}
// the facing state's values, the way a block's y turns it
const FACINGS = ["north", "east", "south", "west"];
// placed against the side of a block, a trapdoor hangs from that block, in the half of the side that was clicked.
// placed on top of a block it lies in the bottom half, and under one in the top half, swinging open away from the
// player, like minecraft's TrapDoorBlock. it opens and closes when right clicked, like a wooden one, here so the
// player who clicks sees it move right away
function register_trapdoor(ctx: SharedContext) {
ctx.components.register_block("bworld:trapdoor", {
on_interact(block) {
const open = ctx.world.get_state(block.x, block.y, block.z, "open") ?? 0;
return ctx.world.set_state(block.x, block.y, block.z, "open", open ? 0 : 1);
},
state_for_placement(placement): Record<string, number> {
if (placement.face !== "top" && placement.face !== "bottom") {
return { facing: FACINGS.indexOf(placement.face), half: placement.hit[1] > 0.5 ? 1 : 0 };
}
// facing already faces the player
return { half: placement.face === "top" ? 0 : 1 };
},
});
}
// the way along the ground each facing goes, in FACINGS' order
const FACING_STEPS = [[0, -1], [1, 0], [0, 1], [-1, 0]];
// a door is two blocks, its lower half (half 0) and its upper half (half 1), with the same states. it faces the way
// the player placing it looks, and is hinged on the side away from a door of its kind beside it, so the two meet in
// the middle as a double door, like minecraft's DoorBlock.getHinge. without one, on the side of the block the player
// clicked toward. right clicking either half opens or closes both, and the other door of a double door with it. the
// server places the upper half and takes away the other half when one breaks, see register_door in server.ts.
// "pairs_with" is the tag of doors it makes double doors with
interface DoorParams {
pairs_with: string;
}
function register_door(ctx: SharedContext) {
// a door's lower half there, of the kind this one pairs with
const door_at = (x: number, y: number, z: number, params: DoorParams) => {
const id = ctx.world.get_block(x, y, z);
return id !== undefined && ctx.tags.has("blocks", params.pairs_with, id) &&
ctx.world.get_state(x, y, z, "half") === 0;
};
// opens or closes both halves of the door whose lower half is there
const set_open = (x: number, y: number, z: number, open: number) => {
if (ctx.world.get_block(x, y + 1, z) === ctx.world.get_block(x, y, z)) {
ctx.world.set_state(x, y + 1, z, "open", open);
}
return ctx.world.set_state(x, y, z, "open", open);
};
ctx.components.register_block<DoorParams>("bworld:door", {
on_interact(block, params) {
const { x, z } = block;
const y = ctx.world.get_state(x, block.y, z, "half") ? block.y - 1 : block.y;
const state = (name: string, at_x = x, at_z = z) => ctx.world.get_state(at_x, y, at_z, name);
const was_open = state("open");
const open = was_open ? 0 : 1;
// the other door of a double door is beside its free edge, facing the same way, hinged the other way, and
// open or closed like it
const facing = state("facing")!;
const [step_x, step_z] = FACING_STEPS[(facing + (state("hinge") ? 3 : 1)) % 4];
const [px, pz] = [x + step_x, z + step_z];
if (
door_at(px, y, pz, params) && state("facing", px, pz) === facing &&
state("hinge", px, pz) !== state("hinge") && state("open", px, pz) === was_open
) {
set_open(px, y, pz, open);
}
return set_open(x, y, z, open);
},
state_for_placement(placement, params): Record<string, number> {
const facing = FACINGS.indexOf(placement.facing);
const { x, y, z } = placement;
const [left_x, left_z] = FACING_STEPS[(facing + 3) % 4];
const [right_x, right_z] = FACING_STEPS[(facing + 1) % 4];
const door_left = door_at(x + left_x, y, z + left_z, params);
const door_right = door_at(x + right_x, y, z + right_z, params);
if (door_left !== door_right) return { facing, half: 0, hinge: door_left ? 1 : 0 };
const [step_x, step_z] = FACING_STEPS[facing];
const [hit_x, , hit_z] = placement.hit;
const right = (step_x < 0 && hit_z < 0.5) || (step_x > 0 && hit_z > 0.5) ||
(step_z < 0 && hit_x > 0.5) || (step_z > 0 && hit_x < 0.5);
return { facing, half: 0, hinge: right ? 1 : 0 };
},
});
}
// a juiceable item's component, like a strawberry's: juice is the mB one of it makes
export interface JuiceableParams {
juice: number;
color: string;
}
// how much juice the juicer holds, in mB. the juice in it is its "tank" client data, a FluidStack, which its visual
// draws (see register_juicer in server.ts and juicer_visual in client.ts)
export const JUICER_CAPACITY = 1000;
// how many presses make one of what's in it into juice. its visual lowers its press for each one toward the next
export const JUICER_PRESSES = 4;
// what juice sells for, as a multiple of what the items pressed to make it sell for
export const JUICE_VALUE = 1.2;
// what wine sells for, as a multiple of what the juice it's fermented from sells for, mB for mB
export const WINE_VALUE = 2;
// juice is one fluid for every fruit, like create's potion fluid: which fruit sits in its data, { item }. it's named
// after that item, "Strawberry Juice", in the color its bworld:juiceable says, and sells for JUICE_VALUE times the
// items it took. wine is too, fermented from the juice (see fermenting.ts), keeping its fruit: "Strawberry Wine", and
// sells for WINE_VALUE times that juice
function register_juice_and_wine(ctx: SharedContext) {
const item_of = (data: unknown) => {
const item = (data as { item?: unknown } | undefined)?.item;
return typeof item === "string" && ctx.items.exists(item) ? item : undefined;
};
const juiceable = (item: string) => ctx.items.components(item)["bworld:juiceable"] as JuiceableParams | undefined;
const from_fruit = (kind: string) => ({
name(data: unknown) {
const item = item_of(data);
return item && `${ctx.items.name(item)} ${kind}`;
},
color(data: unknown) {
const item = item_of(data);
return item && juiceable(item)?.color;
},
});
// what 1000 mB of the fruit's juice sells for
const juice_value = (data: unknown) => {
const item = item_of(data);
if (!item) return undefined;
const juice = juiceable(item)?.juice;
const value = ctx.items.sell_value(item);
return juice && value !== undefined ? JUICE_VALUE * value * 1000 / juice : undefined;
};
ctx.fluids.register("bworld:juice", { ...from_fruit("Juice"), sell_value: juice_value });
ctx.fluids.register("bworld:wine", {
...from_fruit("Wine"),
sell_value(data) {
const value = juice_value(data);
return value === undefined ? undefined : WINE_VALUE * value;
},
});
}