803 lines
29 KiB
TypeScript
803 lines
29 KiB
TypeScript
// the base game's block and item behavior, written against the same api as any other mod
|
|
import type { BlockRef, Container, EntityRef, Player, ServerContext } from "bworld/server";
|
|
import type { Position } from "bworld/shared";
|
|
import { register_curios } from "./curios.ts";
|
|
import { register_effects } from "./effects.ts";
|
|
|
|
export function setup(ctx: ServerContext) {
|
|
register_hoeable(ctx);
|
|
const storage_menu = register_storage(ctx);
|
|
register_shipping_bin(ctx, storage_menu);
|
|
register_furnace(ctx);
|
|
register_smithing_table(ctx);
|
|
register_trapdoor(ctx);
|
|
register_farming(ctx);
|
|
register_fishing(ctx);
|
|
register_buckets(ctx);
|
|
register_curios(ctx);
|
|
register_effects(ctx);
|
|
}
|
|
|
|
// right clicking with a type of tool turns the block into another, like any hoe on grass
|
|
function register_hoeable(ctx: ServerContext) {
|
|
ctx.components.register_block<{ tool: string; into: string }>("bworld:hoeable", {
|
|
on_interact(block, params, player) {
|
|
const held = player.held_item;
|
|
if (!held || ctx.items.tool(held.id) !== params.tool) {
|
|
return false;
|
|
}
|
|
ctx.world.set_block(block.x, block.y, block.z, params.into);
|
|
return true;
|
|
},
|
|
});
|
|
}
|
|
|
|
// a trapdoor opens and closes when right clicked, like a wooden one in minecraft. how it's placed is in shared.ts
|
|
function register_trapdoor(ctx: ServerContext) {
|
|
ctx.components.register_block("bworld:trapdoor", {
|
|
on_interact(block) {
|
|
const open = ctx.world.get_state(block.x, block.y, block.z, "open") ?? 0;
|
|
ctx.world.set_state(block.x, block.y, block.z, "open", open ? 0 : 1);
|
|
return true;
|
|
},
|
|
});
|
|
}
|
|
|
|
// the container a block keeps its items in, made the first time it's needed. blocks placed before tile data
|
|
// existed don't have one yet
|
|
function block_container(ctx: ServerContext, block: BlockRef, size: number): Container {
|
|
const existing = block.data?.container && ctx.containers.get(block.data.container);
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
const container = ctx.containers.create(size);
|
|
block.data = { ...block.data, container: container.id };
|
|
return container;
|
|
}
|
|
|
|
// what was inside falls out, like minecraft's Containers.dropContents
|
|
function drop_contents(ctx: ServerContext, block: BlockRef) {
|
|
const container = block.data?.container && ctx.containers.get(block.data.container);
|
|
if (!container) {
|
|
return;
|
|
}
|
|
for (let slot = 0; slot < container.size; slot++) {
|
|
const item = container.get(slot);
|
|
if (item) {
|
|
ctx.world.drop_item(block.x, block.y, block.z, item);
|
|
}
|
|
}
|
|
ctx.containers.delete(container.id);
|
|
}
|
|
|
|
// the menus' layouts are in gui pixels, like minecraft's: their screens are in scripts/client.ts. the player's
|
|
// inventory goes at the bottom, its rows from INVENTORY_Y and the hotbar under them
|
|
const INVENTORY_Y = 84;
|
|
|
|
interface StorageArgs {
|
|
block: BlockRef;
|
|
rows: number;
|
|
// "Chest" when not set
|
|
title?: string;
|
|
}
|
|
|
|
// a chest, like minecraft's ChestMenu: rows of 9 over the inventory
|
|
function register_storage(ctx: ServerContext) {
|
|
const menu_type = ctx.menus.register<StorageArgs>("bworld:storage", (menu, args) => {
|
|
const { block, rows } = args;
|
|
const container = block_container(ctx, block, rows * 9);
|
|
menu.title = args.title ?? "Chest";
|
|
menu.open_data = { rows };
|
|
for (let slot = 0; slot < rows * 9; slot++) {
|
|
menu.add_slot(container, slot, { x: 7 + (slot % 9) * 18, y: 17 + Math.floor(slot / 9) * 18 });
|
|
}
|
|
menu.add_player_inventory(7, 31 + rows * 18);
|
|
menu.still_valid_near(block);
|
|
});
|
|
|
|
ctx.components.register_block<{ rows: number }>("bworld:storage", {
|
|
on_create(block, params) {
|
|
block_container(ctx, block, params.rows * 9);
|
|
},
|
|
on_interact(block, params, player) {
|
|
menu_type.open(player, { block, rows: params.rows });
|
|
return true;
|
|
},
|
|
on_break(block) {
|
|
drop_contents(ctx, block);
|
|
},
|
|
});
|
|
return menu_type;
|
|
}
|
|
|
|
// a shipping bin, like stardew valley's: a chest that every morning at 06:00 sells everything in it with a
|
|
// base_sell_value, and pays for it in as few coins as it can
|
|
|
|
const COINS = [
|
|
"bworld:cobalt_coin",
|
|
"bworld:gold_coin",
|
|
"bworld:iron_coin",
|
|
"bworld:brass_coin",
|
|
"bworld:nickel_coin",
|
|
"bworld:copper_coin",
|
|
];
|
|
|
|
function register_shipping_bin(ctx: ServerContext, storage_menu: ReturnType<typeof register_storage>) {
|
|
// the biggest first, by their own sell values
|
|
const coin_value = (id: string) => ctx.items.sell_value(id) ?? 0;
|
|
const coins = [...COINS].sort((a, b) => coin_value(b) - coin_value(a));
|
|
|
|
ctx.components.register_block<{ rows: number }>("bworld:shipping_bin", {
|
|
on_create(block, params) {
|
|
block_container(ctx, block, params.rows * 9);
|
|
},
|
|
on_interact(block, params, player) {
|
|
storage_menu.open(player, { block, rows: params.rows, title: "Shipping Bin" });
|
|
return true;
|
|
},
|
|
on_break(block) {
|
|
drop_contents(ctx, block);
|
|
},
|
|
on_new_day(block, params) {
|
|
const container = block_container(ctx, block, params.rows * 9);
|
|
let total = 0;
|
|
for (let slot = 0; slot < container.size; slot++) {
|
|
const item = container.get(slot);
|
|
const value = item && ctx.items.sell_value(item.id);
|
|
if (value === undefined) continue;
|
|
total += value * item!.count;
|
|
container.set(slot, undefined);
|
|
}
|
|
for (const coin of coins) {
|
|
const value = coin_value(coin);
|
|
if (value <= 0) continue;
|
|
let count = Math.floor(total / value);
|
|
total -= count * value;
|
|
while (count > 0) {
|
|
const stack = Math.min(count, ctx.items.max_stack(coin));
|
|
count -= stack;
|
|
// with unsold items in the way, what doesn't fit falls out
|
|
const left = container.add({ id: coin, count: stack });
|
|
if (left) ctx.world.drop_item(block.x, block.y, block.z, left);
|
|
}
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
// furnace
|
|
|
|
// slot 0 is what's smelting, 1 the fuel, 2 the result
|
|
interface FurnaceData {
|
|
container: string;
|
|
// ticks
|
|
progress: number;
|
|
progress_max: number;
|
|
fuel: number;
|
|
fuel_max: number;
|
|
}
|
|
|
|
function register_furnace(ctx: ServerContext) {
|
|
const furnace_data = (block: BlockRef): FurnaceData => {
|
|
const container = block_container(ctx, block, 3);
|
|
block.data = { progress: 0, progress_max: 0, fuel: 0, fuel_max: 0, ...block.data, container: container.id };
|
|
return block.data;
|
|
};
|
|
|
|
// like minecraft's FurnaceMenu, the screen shows the fuel left and the progress with bars
|
|
const menu_type = ctx.menus.register<BlockRef>("bworld:furnace", (menu, block) => {
|
|
const container = ctx.containers.get(furnace_data(block).container)!;
|
|
menu.title = "Furnace";
|
|
menu.add_slot(container, 0, { x: 55, y: 16 });
|
|
menu.add_slot(container, 1, { x: 55, y: 52, filter: "fuel" });
|
|
menu.add_slot(container, 2, { x: 115, y: 34, output_only: true });
|
|
menu.add_player_inventory(7, INVENTORY_Y);
|
|
for (const field of ["progress", "progress_max", "fuel", "fuel_max"] as const) {
|
|
menu.add_data(field, () => (block.data as FurnaceData | undefined)?.[field] ?? 0);
|
|
}
|
|
menu.still_valid_near(block);
|
|
});
|
|
|
|
// there's somewhere for the result to go
|
|
const can_smelt = (container: Container, output: { id: string; count: number }) => {
|
|
const result = container.get(2);
|
|
return !result || (result.id === output.id && result.count < ctx.items.max_stack(result.id));
|
|
};
|
|
|
|
ctx.components.register_block("bworld:furnace", {
|
|
on_create(block) {
|
|
furnace_data(block);
|
|
},
|
|
on_interact(block, _params, player) {
|
|
furnace_data(block);
|
|
menu_type.open(player, block);
|
|
return true;
|
|
},
|
|
on_break(block) {
|
|
drop_contents(ctx, block);
|
|
},
|
|
on_tick(block) {
|
|
const data = furnace_data(block);
|
|
const container = ctx.containers.get(data.container)!;
|
|
const input = container.get(0);
|
|
const recipe = input && ctx.recipes.furnace_result(input.id);
|
|
|
|
if (data.fuel > 0) {
|
|
data.fuel -= 1;
|
|
}
|
|
|
|
if (!recipe || !can_smelt(container, recipe.output)) {
|
|
data.progress = 0;
|
|
} else {
|
|
// refuel
|
|
const fuel = container.get(1);
|
|
if (data.fuel === 0 && fuel && ctx.recipes.is_fuel(fuel.id)) {
|
|
data.fuel = ctx.recipes.fuel_value(fuel.id);
|
|
data.fuel_max = data.fuel;
|
|
fuel.count -= 1;
|
|
}
|
|
|
|
// cook
|
|
if (data.fuel > 0) {
|
|
data.progress_max = recipe.cook_time;
|
|
data.progress += 1;
|
|
if (data.progress >= recipe.cook_time) {
|
|
data.progress = 0;
|
|
const result = container.get(2);
|
|
if (result) {
|
|
result.count += recipe.output.count;
|
|
} else {
|
|
container.set(2, recipe.output);
|
|
}
|
|
input.count -= 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
// burning, so it heats what's on top, like farmer's delight's cooking pot
|
|
const lit = data.fuel > 0 ? 1 : 0;
|
|
if (ctx.world.get_state(block.x, block.y, block.z, "lit") !== lit) {
|
|
ctx.world.set_state(block.x, block.y, block.z, "lit", lit);
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
// smithing table: a tool, some of a material and maybe a third item make a better tool, see the smithing recipes.
|
|
// every player gets their own slots, and what's left in them goes back to the player when they close it
|
|
|
|
const TOOL = 0;
|
|
const MATERIAL = 1;
|
|
const ADDITION = 2;
|
|
const RESULT = 3;
|
|
|
|
function register_smithing_table(ctx: ServerContext) {
|
|
const menu_type = ctx.menus.register<BlockRef>("bworld:smithing_table", (menu, block, player) => {
|
|
const container = ctx.containers.create(4);
|
|
|
|
// the recipe the inputs make, with enough material
|
|
const match = () => {
|
|
const tool = container.get(TOOL);
|
|
const material = container.get(MATERIAL);
|
|
const addition = container.get(ADDITION);
|
|
if (!tool || !material) return undefined;
|
|
const recipe = ctx.recipes.smithing(tool.id, material.id, addition?.id);
|
|
if (!recipe || material.count < recipe.material_count) return undefined;
|
|
return { ...recipe, tool, material, addition };
|
|
};
|
|
|
|
// the result slot shows what taking it would give, the upgraded tool keeps the old one's data
|
|
const show_result = () => {
|
|
const recipe = match();
|
|
const shown = container.get(RESULT);
|
|
if (!recipe) {
|
|
if (shown) container.set(RESULT, undefined);
|
|
} else if (shown?.id !== recipe.result) {
|
|
container.set(RESULT, { id: recipe.result, count: 1, data: recipe.tool.data });
|
|
}
|
|
};
|
|
const stop = container.on_change((slot) => {
|
|
if (slot !== RESULT) show_result();
|
|
});
|
|
|
|
menu.title = "Smithing";
|
|
menu.add_slot(container, TOOL, { x: 25, y: 34 });
|
|
menu.add_slot(container, MATERIAL, { x: 43, y: 34 });
|
|
menu.add_slot(container, ADDITION, { x: 61, y: 34 });
|
|
menu.add_slot(container, RESULT, {
|
|
x: 115,
|
|
y: 34,
|
|
output_only: true,
|
|
// the inputs are used up in the same step, so they can't be taken out after
|
|
on_take(item) {
|
|
const recipe = match();
|
|
if (!recipe || recipe.result !== item.id) return false;
|
|
recipe.tool.count -= 1;
|
|
recipe.material.count -= recipe.material_count;
|
|
if (recipe.addition) recipe.addition.count -= 1;
|
|
return true;
|
|
},
|
|
});
|
|
menu.add_player_inventory(7, INVENTORY_Y);
|
|
menu.still_valid_near(block);
|
|
menu.on_close(() => {
|
|
stop();
|
|
for (const slot of [TOOL, MATERIAL, ADDITION]) {
|
|
const item = container.get(slot);
|
|
if (item) player.give_item(item.id, item.count, item.data);
|
|
}
|
|
ctx.containers.delete(container.id);
|
|
});
|
|
});
|
|
|
|
ctx.components.register_block("bworld:smithing_table", {
|
|
on_interact(block, _params, player) {
|
|
menu_type.open(player, block);
|
|
return true;
|
|
},
|
|
});
|
|
}
|
|
|
|
// farming: java's crop rules on stardew valley's clock. every morning at 06:00 a crop grows one stage if its farmland
|
|
// was watered the day before, then all farmland dries. crops only stand on farmland (their place_on), and pop off when
|
|
// it's dug up
|
|
|
|
const FARMLAND = "bworld:hoed_dirt";
|
|
// every tier of watering can is a tool of this type, and shares the bworld:watering_can component
|
|
const WATERING_CAN = "watering_can";
|
|
|
|
interface WateringCanData {
|
|
water: number;
|
|
max_water: number;
|
|
}
|
|
|
|
// items as they appear in crop params: one id, or several
|
|
type Items = string | string[];
|
|
|
|
// what a crop drops when broken, like the loot tables of minecraft's and farmer's delight's crops
|
|
interface CropDrops {
|
|
// before it's fully grown, like wheat's seeds
|
|
young?: Items;
|
|
// fully grown, like wheat's wheat
|
|
mature?: Items;
|
|
// fully grown, 1 to 4 of this, like the seeds from wheat (minecraft's loot tables: 1 plus 3 tries at 57%)
|
|
bonus?: string;
|
|
}
|
|
|
|
// a crop, like minecraft's CropBlock: its "age" state is its stage, and it grows one day every morning its farmland was
|
|
// watered the day before. see docs/modding/blocks/crops.md
|
|
interface CropParams {
|
|
// watered mornings from planting to fully grown
|
|
days: number;
|
|
// how many stages it shows, ages 0 to stages - 1. its "age" state must fit them
|
|
stages: number;
|
|
drops?: CropDrops;
|
|
// grows back after it's harvested by right clicking, like tomatoes
|
|
regrow_days?: number;
|
|
harvest?: { item: string; count: [number, number] };
|
|
// grows it a day right away, like bone meal
|
|
fertilizer?: string;
|
|
// "farmland" (the default) needs watered farmland under it, "always" grows every morning, like rice in water
|
|
water?: "farmland" | "always";
|
|
// the last stages grow a second block on top, like rice's panicles. the crop stays at supporting_age meanwhile
|
|
upper?: { block: string; stages: number; supporting_age: number };
|
|
// what the spot becomes when it's broken, like water where rice was
|
|
leaves?: string;
|
|
}
|
|
|
|
// the top of a two block crop. it grows with the crop under it, and breaking it breaks the whole plant
|
|
interface CropTopParams {
|
|
stages: number;
|
|
drops?: CropDrops;
|
|
}
|
|
|
|
function register_farming(ctx: ServerContext) {
|
|
const is_can = (id: string | undefined) => id !== undefined && ctx.items.tool(id) === WATERING_CAN;
|
|
// how much water each tier holds, from its component. every stack goes through on_create, so every can's tier is
|
|
// in here before anything reads it
|
|
const capacity = new Map<string, number>();
|
|
|
|
const is_watered = (x: number, y: number, z: number) =>
|
|
ctx.world.get_block(x, y, z) === FARMLAND && ctx.world.get_state(x, y, z, "watered") === 1;
|
|
|
|
// with the watering can the player holds. true when the click was about watering, so nothing gets placed
|
|
const water = (x: number, y: number, z: number, player: Player) => {
|
|
const can = player.held_item;
|
|
if (!can || !is_can(can.id) || ctx.world.get_block(x, y, z) !== FARMLAND) {
|
|
return false;
|
|
}
|
|
const data = can.data as WateringCanData;
|
|
if (is_watered(x, y, z)) {
|
|
return true;
|
|
}
|
|
if (data.water <= 0) {
|
|
player.send_message("The watering can is empty, fill it at some water");
|
|
return true;
|
|
}
|
|
can.data = { ...data, water: data.water - 1 };
|
|
ctx.world.set_state(x, y, z, "watered", 1);
|
|
return true;
|
|
};
|
|
|
|
ctx.components.register_block("bworld:farmland", {
|
|
// block data so on_new_day runs for it
|
|
on_create(block) {
|
|
block.data = {};
|
|
},
|
|
on_interact(block, _params, player) {
|
|
block.data ??= {};
|
|
return water(block.x, block.y, block.z, player);
|
|
},
|
|
on_new_day(block) {
|
|
if (!is_watered(block.x, block.y, block.z)) return;
|
|
// after every crop's on_new_day this morning, which look at it
|
|
ctx.system.run_timeout(() => {
|
|
if (ctx.world.get_block(block.x, block.y, block.z) === FARMLAND) {
|
|
ctx.world.set_state(block.x, block.y, block.z, "watered", 0);
|
|
}
|
|
}, 0);
|
|
},
|
|
});
|
|
|
|
const list = (items: Items | undefined) => items === undefined ? [] : typeof items === "string" ? [items] : items;
|
|
|
|
const drop = (x: number, y: number, z: number, drops: CropDrops | undefined, mature: boolean) => {
|
|
for (const id of list(mature ? drops?.mature : drops?.young)) ctx.world.drop_item(x, y, z, { id, count: 1 });
|
|
if (mature && drops?.bonus) {
|
|
let count = 1;
|
|
for (let i = 0; i < 3; i++) if (Math.random() < 0.5714) count++;
|
|
ctx.world.drop_item(x, y, z, { id: drops.bonus, count });
|
|
}
|
|
};
|
|
|
|
// the stage for how many days it has grown: evenly spread, fully grown on the last day
|
|
const stage_for = (params: CropParams, days: number) => {
|
|
const stages = params.stages + (params.upper?.stages ?? 0);
|
|
return Math.min(stages - 1, Math.floor(days * (stages - 1) / params.days));
|
|
};
|
|
|
|
// days grown, kept in the block's data. crops from before this start at the first day of the stage they show
|
|
const days_of = (block: BlockRef, params: CropParams) => {
|
|
if (typeof block.data?.days === "number") return block.data.days as number;
|
|
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
|
|
let days = 0;
|
|
while (days < params.days && stage_for(params, days) < age) days++;
|
|
return days;
|
|
};
|
|
|
|
// shows the stage for days, growing or taking away the top block for two block crops
|
|
const set_days = (block: BlockRef, params: CropParams, days: number) => {
|
|
block.data = { ...block.data, days };
|
|
const stage = stage_for(params, days);
|
|
const upper = params.upper;
|
|
const above = ctx.world.get_block(block.x, block.y + 1, block.z);
|
|
if (!upper || stage < params.stages) {
|
|
ctx.world.set_state(block.x, block.y, block.z, "age", stage);
|
|
if (upper && above === upper.block) ctx.world.set_block(block.x, block.y + 1, block.z, "bworld:air");
|
|
return;
|
|
}
|
|
// the top needs room, like farmer's delight's rice
|
|
if (above !== upper.block && above !== "bworld:air") {
|
|
ctx.world.set_state(block.x, block.y, block.z, "age", params.stages - 1);
|
|
return;
|
|
}
|
|
ctx.world.set_state(block.x, block.y, block.z, "age", upper.supporting_age);
|
|
if (above !== upper.block) ctx.world.set_block(block.x, block.y + 1, block.z, upper.block);
|
|
ctx.world.set_state(block.x, block.y + 1, block.z, "age", stage - params.stages);
|
|
};
|
|
|
|
const is_grown = (block: BlockRef, params: CropParams) => days_of(block, params) >= params.days;
|
|
|
|
ctx.components.register_block<CropParams>("bworld:crop", {
|
|
// block data so on_new_day runs for it. placed at a later stage (by a script), it has grown that long
|
|
on_create(block, params) {
|
|
block.data = {};
|
|
block.data = { days: days_of(block, params) };
|
|
},
|
|
on_new_day(block, params) {
|
|
const days = days_of(block, params);
|
|
if (days >= params.days) return;
|
|
const watered = params.water === "always" || is_watered(block.x, block.y - 1, block.z);
|
|
if (watered) set_days(block, params, days + 1);
|
|
},
|
|
on_interact(block, params, player) {
|
|
const held = player.held_item;
|
|
// watering a crop waters the farmland under it
|
|
if (is_can(held?.id)) {
|
|
block.data ??= { days: days_of(block, params) };
|
|
return water(block.x, block.y - 1, block.z, player);
|
|
}
|
|
const days = days_of(block, params);
|
|
if (params.fertilizer && held?.id === params.fertilizer) {
|
|
// fully grown, keep the fertilizer
|
|
if (days >= params.days) return false;
|
|
set_days(block, params, days + 1);
|
|
held.count -= 1;
|
|
return true;
|
|
}
|
|
// regrowing crops are picked by hand and grow back, like farmer's delight's tomatoes
|
|
if (params.regrow_days && params.harvest && days >= params.days) {
|
|
const [min, max] = params.harvest.count;
|
|
const count = min + Math.floor(Math.random() * (max - min + 1));
|
|
ctx.world.drop_item(block.x, block.y, block.z, { id: params.harvest.item, count });
|
|
set_days(block, params, Math.max(0, params.days - params.regrow_days));
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
on_break(block, params) {
|
|
drop(block.x, block.y, block.z, params.drops, is_grown(block, params));
|
|
if (params.leaves) {
|
|
const { x, y, z } = block;
|
|
const leaves = params.leaves;
|
|
// after it's gone, it's still there while on_break runs
|
|
ctx.system.run_timeout(() => {
|
|
if (ctx.world.get_block(x, y, z) === "bworld:air") ctx.world.set_block(x, y, z, leaves);
|
|
}, 0);
|
|
}
|
|
},
|
|
});
|
|
|
|
ctx.components.register_block<CropTopParams>("bworld:crop_top", {
|
|
on_break(block, params) {
|
|
const age = ctx.world.get_state(block.x, block.y, block.z, "age") ?? 0;
|
|
drop(block.x, block.y, block.z, params.drops, age >= params.stages - 1);
|
|
// the plant under it goes too, once this is gone
|
|
const { x, y, z } = block;
|
|
const lower = ctx.world.get_block(x, y - 1, z);
|
|
ctx.system.run_timeout(() => {
|
|
if (lower && ctx.world.get_block(x, y - 1, z) === lower) ctx.world.break_block(x, y - 1, z);
|
|
}, 0);
|
|
},
|
|
});
|
|
|
|
// right clicking water with the watering can fills it
|
|
ctx.components.register_block("bworld:water_source", {
|
|
on_interact(_block, _params, player) {
|
|
const can = player.held_item;
|
|
if (!can || !is_can(can.id)) return false;
|
|
// an upgraded can still has its old tier's max_water in its data, so this goes by its own
|
|
const max_water = capacity.get(can.id) ?? (can.data as WateringCanData).max_water;
|
|
can.data = { water: max_water, max_water } satisfies WateringCanData;
|
|
return true;
|
|
},
|
|
});
|
|
|
|
ctx.components.register_item<{ max_water: number }>("bworld:watering_can", {
|
|
on_create(item, params) {
|
|
capacity.set(item.id, params.max_water);
|
|
item.data = { water: 0, max_water: params.max_water } satisfies WateringCanData;
|
|
},
|
|
get_lore(item, params) {
|
|
const data = item.data as WateringCanData;
|
|
return `Water: ${data.water}/${params.max_water}`;
|
|
},
|
|
});
|
|
}
|
|
|
|
// buckets, like minecraft's: right clicking water with a bucket picks it up into a water bucket, which places it back
|
|
// and is a bucket again
|
|
|
|
// right clicking it with a bucket takes it away, giving the filled bucket
|
|
interface BucketableParams {
|
|
into: string;
|
|
}
|
|
|
|
// what a filled bucket is once it has placed its block
|
|
interface FilledBucketParams {
|
|
empty: string;
|
|
}
|
|
|
|
const BUCKET = "bworld:bucket";
|
|
|
|
function register_buckets(ctx: ServerContext) {
|
|
ctx.components.register_block<BucketableParams>("bworld:bucketable", {
|
|
on_interact(block, params, player) {
|
|
const bucket = player.held_item;
|
|
if (bucket?.id !== BUCKET) return false;
|
|
const { x, y, z } = block;
|
|
if (ctx.world.get_block(x, y, z) === block.id) {
|
|
// only a liquid's source fills it, not where it's flowing
|
|
if ((ctx.world.get_state(x, y, z, "level") ?? 0) !== 0) return false;
|
|
ctx.world.set_block(x, y, z, "bworld:air");
|
|
} else {
|
|
// the liquid in a waterlogged block, like a fence in water, which stays
|
|
ctx.world.set_state(x, y, z, "waterlogged", 0);
|
|
}
|
|
bucket.count -= 1;
|
|
// the last one becomes the filled bucket in the hand, from a stack it goes in the inventory
|
|
if (bucket.count <= 0) player.inventory.set(player.selected_slot, { id: params.into, count: 1 });
|
|
else player.give_item(params.into);
|
|
return true;
|
|
},
|
|
});
|
|
|
|
// read from the item's json when it places its block
|
|
ctx.components.register_item<FilledBucketParams>("bworld:filled_bucket", {});
|
|
// the empty bucket for what each player is placing. the event's item is their slot, which placing empties, so it's
|
|
// read before
|
|
const emptying = new Map<string, string>();
|
|
ctx.events.before.block_place.subscribe(({ player, item }) => {
|
|
const params = item && ctx.items.components(item.id)["bworld:filled_bucket"] as FilledBucketParams | undefined;
|
|
if (params) emptying.set(player.id, params.empty);
|
|
else emptying.delete(player.id);
|
|
});
|
|
ctx.events.after.block_place.subscribe(({ player }) => {
|
|
const empty = emptying.get(player.id);
|
|
emptying.delete(player.id);
|
|
if (empty) player.inventory.set(player.selected_slot, { id: empty, count: 1 });
|
|
});
|
|
}
|
|
|
|
// fishing, like minecraft's: right click with a rod to throw its hook forward. when it lands in a block with
|
|
// bworld:fishable, like water, it floats there until something bites, and right clicking again before it gets away
|
|
// reels it in. a hook that lands on anything else lies there until it's reeled in, with nothing on it
|
|
|
|
const FISHING_ROD = "bworld:fishing_rod";
|
|
const FISHING_HOOK = "bworld:fishing_hook";
|
|
const FISHABLE = "bworld:fishable";
|
|
// further than this from the hook, the line snaps
|
|
const LINE_LENGTH = 16;
|
|
// blocks per tick it's thrown at, and how it falls and slows down in the air each tick, like minecraft's hook
|
|
const THROW_SPEED = 1;
|
|
const GRAVITY = 0.03;
|
|
const DRAG = 0.92;
|
|
// it's reeled in if it's still flying after this many ticks
|
|
const MAX_FLIGHT = 100;
|
|
// half the hook's 0.25 block box: the hook's middle flies along the line, its feet are this much under it
|
|
const HOOK_HALF = 0.125;
|
|
// the hook's feet under the water's surface, so its red stripe floats right on it (the hook's sprite is 16 pixels
|
|
// around the middle of its 0.25 block box, the stripe is 1.5 pixels above that)
|
|
const HOOK_DEPTH = 0.125 + 1.5 / 16;
|
|
// how far it bobs up and down, and how far under it goes with a bite
|
|
const HOOK_BOB = 0.03;
|
|
const HOOK_DIP = 0.15;
|
|
|
|
interface FishingRodParams {
|
|
// ticks until something bites, the shortest and longest, like minecraft's 5 to 30 seconds
|
|
wait: [number, number];
|
|
// ticks it stays on the hook before it gets away
|
|
bite: [number, number];
|
|
loot: { item: string; weight: number }[];
|
|
}
|
|
|
|
interface Cast {
|
|
// the hotbar slot the rod is in, switching away reels it in
|
|
slot: number;
|
|
params: FishingRodParams;
|
|
hook: EntityRef;
|
|
// the hook's middle
|
|
position: Position;
|
|
// blocks per tick while it's flying, undefined once it has landed
|
|
velocity?: Position;
|
|
thrown_at: number;
|
|
// the fishable block it floats on, once it lands in one. the line comes out when it's gone
|
|
water?: { x: number; y: number; z: number; block: string };
|
|
// ticks, when something bites and when it gets away
|
|
bite_at?: number;
|
|
escape_at?: number;
|
|
}
|
|
|
|
function register_fishing(ctx: ServerContext) {
|
|
// by player id
|
|
const casts = new Map<string, Cast>();
|
|
|
|
const between = ([min, max]: [number, number]) => min + Math.floor(Math.random() * (max - min + 1));
|
|
|
|
const pick = (loot: FishingRodParams["loot"]) => {
|
|
const total = loot.reduce((sum, entry) => sum + entry.weight, 0);
|
|
let roll = Math.random() * total;
|
|
for (const entry of loot) {
|
|
roll -= entry.weight;
|
|
if (roll < 0) return entry.item;
|
|
}
|
|
return loot.at(-1)?.item;
|
|
};
|
|
|
|
const fishable = new Map<string, boolean>();
|
|
const is_fishable = (id: string) => {
|
|
let result = fishable.get(id);
|
|
if (result === undefined) fishable.set(id, result = FISHABLE in ctx.blocks.components(id));
|
|
return result;
|
|
};
|
|
|
|
// takes the line in, the rod in the slot looks uncast again
|
|
const stop = (player: Player | undefined, id: string) => {
|
|
const cast = casts.get(id);
|
|
casts.delete(id);
|
|
cast?.hook.remove();
|
|
const rod = cast && player?.inventory.get(cast.slot);
|
|
if (rod?.id === FISHING_ROD) rod.data = undefined;
|
|
};
|
|
|
|
// moves the hook along its flight for a tick, until it hits something
|
|
const fly = (cast: Cast, velocity: Position, now: number) => {
|
|
const from = cast.position;
|
|
const to = { x: from.x + velocity.x, y: from.y + velocity.y, z: from.z + velocity.z };
|
|
const hit = ctx.world.raycast(from, to, { liquids: "any" });
|
|
if (!hit) {
|
|
cast.position = to;
|
|
velocity.x *= DRAG;
|
|
velocity.y = velocity.y * DRAG - GRAVITY;
|
|
velocity.z *= DRAG;
|
|
cast.hook.teleport(to.x, to.y - HOOK_HALF, to.z);
|
|
return;
|
|
}
|
|
cast.velocity = undefined;
|
|
cast.position = { ...hit.position };
|
|
if (is_fishable(hit.id)) {
|
|
// it floats on top, even when it went in by a side under the surface
|
|
let y = hit.y;
|
|
while (ctx.world.get_block(hit.x, y + 1, hit.z) === hit.id) y++;
|
|
cast.water = { x: hit.x, y, z: hit.z, block: hit.id };
|
|
cast.position.y = y + 1;
|
|
cast.bite_at = now + between(cast.params.wait);
|
|
} else {
|
|
cast.hook.teleport(hit.position.x, hit.position.y - HOOK_HALF, hit.position.z);
|
|
}
|
|
};
|
|
|
|
// fishable blocks are found by their components, see is_fishable
|
|
ctx.components.register_block(FISHABLE, {});
|
|
|
|
ctx.components.register_item<FishingRodParams>(FISHING_ROD, {
|
|
on_use(rod, params, player) {
|
|
const cast = casts.get(player.id);
|
|
if (cast) {
|
|
if (cast.escape_at !== undefined) {
|
|
const item = pick(params.loot);
|
|
if (item) player.give_item(item);
|
|
}
|
|
stop(player, player.id);
|
|
return;
|
|
}
|
|
const eye = player.eye_position;
|
|
const look = player.look_direction;
|
|
const position = { x: eye.x + look.x * 0.5, y: eye.y + look.y * 0.5, z: eye.z + look.z * 0.5 };
|
|
casts.set(player.id, {
|
|
slot: player.selected_slot,
|
|
params,
|
|
hook: ctx.world.spawn_entity(FISHING_HOOK, position.x, position.y - HOOK_HALF, position.z),
|
|
position,
|
|
velocity: { x: look.x * THROW_SPEED, y: look.y * THROW_SPEED, z: look.z * THROW_SPEED },
|
|
thrown_at: ctx.system.current_tick,
|
|
});
|
|
rod.data = { cast: true };
|
|
},
|
|
});
|
|
|
|
ctx.events.after.tick.subscribe(() => {
|
|
const now = ctx.system.current_tick;
|
|
for (const [id, cast] of casts) {
|
|
const player = ctx.players.get(id);
|
|
const { x, y, z } = player?.position ?? cast.position;
|
|
const distance = Math.hypot(x - cast.position.x, y - cast.position.y, z - cast.position.z);
|
|
const water = cast.water;
|
|
if (
|
|
!player || player.selected_slot !== cast.slot || player.held_item?.id !== FISHING_ROD ||
|
|
distance > LINE_LENGTH || cast.hook.removed ||
|
|
(water && ctx.world.get_block(water.x, water.y, water.z) !== water.block) ||
|
|
(cast.velocity && (now - cast.thrown_at > MAX_FLIGHT || cast.position.y < 0))
|
|
) {
|
|
stop(player, id);
|
|
continue;
|
|
}
|
|
if (cast.velocity) {
|
|
fly(cast, cast.velocity, now);
|
|
}
|
|
if (!cast.water || cast.bite_at === undefined) {
|
|
continue;
|
|
}
|
|
if (cast.escape_at === undefined && now >= cast.bite_at) {
|
|
cast.escape_at = now + between(cast.params.bite);
|
|
} else if (cast.escape_at !== undefined && now >= cast.escape_at) {
|
|
// it got away, wait for another
|
|
cast.escape_at = undefined;
|
|
cast.bite_at = now + between(cast.params.wait);
|
|
}
|
|
const bob = cast.escape_at === undefined ? Math.sin(now / 40 * Math.PI * 2) * HOOK_BOB : -HOOK_DIP;
|
|
cast.hook.teleport(cast.position.x, cast.position.y - HOOK_DEPTH + bob, cast.position.z);
|
|
}
|
|
});
|
|
}
|