Keg
This commit is contained in:
@@ -29,8 +29,8 @@ export function setup(ctx: ClientContext) {
|
||||
ctx.ui.register_menu_screen("bworld:storage", chest_screen);
|
||||
ctx.ui.register_menu_screen("bworld:furnace", furnace_screen);
|
||||
ctx.ui.register_menu_screen("bworld:crabpot", (menu) => container_screen(menu, HEIGHT));
|
||||
ctx.ui.register_menu_screen("bworld:worm_bin", input_output_screen);
|
||||
ctx.ui.register_menu_screen("bworld:keg", input_output_screen);
|
||||
ctx.ui.register_menu_screen("bworld:worm_bin", worm_bin_screen);
|
||||
ctx.ui.register_menu_screen("bworld:keg", keg_screen);
|
||||
ctx.ui.register_menu_screen("bworld:smithing_table", smithing_screen);
|
||||
ctx.ui.register_menu_screen("bworld:stonecutter", (menu) => stonecutter_screen(ctx, menu));
|
||||
ctx.ui.register_menu_screen("bworld:fishing_minigame", fishing_minigame_screen);
|
||||
@@ -131,8 +131,16 @@ function furnace_screen(menu: ClientMenu): MenuScreen {
|
||||
});
|
||||
}
|
||||
|
||||
// its input on the left, an arrow to what it makes on the right, like the worm bin's slots and the keg's tanks
|
||||
function input_output_screen(menu: ClientMenu): MenuScreen {
|
||||
// its input tank on the left, and an arrow to its output tank on the right that fills as a recipe's worth ferments
|
||||
function keg_screen(menu: ClientMenu): MenuScreen {
|
||||
return container_screen(menu, HEIGHT, (g) => {
|
||||
const { progress, progress_max } = menu.data;
|
||||
g.progress("bworld:arrow_empty", "bworld:arrow_full", 85, 34, 18, 18, progress / progress_max);
|
||||
});
|
||||
}
|
||||
|
||||
// its input on the left, an arrow to what it makes on the right
|
||||
function worm_bin_screen(menu: ClientMenu): MenuScreen {
|
||||
return container_screen(menu, HEIGHT, (g) => {
|
||||
g.texture("bworld:arrow_empty", 85, 34, 18, 18);
|
||||
});
|
||||
|
||||
@@ -11,7 +11,7 @@ import type { JuiceableParams } from "./shared.ts";
|
||||
export const FERMENTABLE = "bworld:fermentable";
|
||||
// how much of a juice one recipe ferments, and how long it takes for now
|
||||
const FERMENTED_AMOUNT = 1000;
|
||||
const FERMENT_TICKS = 1;
|
||||
const FERMENT_TICKS = 40;
|
||||
|
||||
export interface FermentableRecipe {
|
||||
// how much of which fluid ferments, into what
|
||||
|
||||
@@ -928,17 +928,30 @@ function register_juicer(ctx: ServerContext) {
|
||||
}
|
||||
|
||||
// the keg holds an input tank and an output tank of KEG_CAPACITY mB each, in its tile data. its screen shows both, and
|
||||
// its fluid handler, from every side, only fills the input and only drains the output
|
||||
// its fluid handler, from every side, only fills the input and only drains the output. it ferments the input by the
|
||||
// first bworld:fermentable recipe for its fluid, 1 mB each time its progress reaches the recipe's ticks, while there's
|
||||
// room in the output for what that makes. the output is spread over a recipe's worth of input, so that much always
|
||||
// makes the recipe's whole output, whatever their amounts. it only ticks while it has something to ferment, and wakes
|
||||
// when either tank changes
|
||||
const KEG_CAPACITY = 4000;
|
||||
interface KegData {
|
||||
input?: FluidStack;
|
||||
output?: FluidStack;
|
||||
// ticks toward fermenting the next mB, up to its recipe's ticks
|
||||
progress?: number;
|
||||
// mB of the input fermented toward a recipe's worth, which its screen's arrow shows
|
||||
fermented?: number;
|
||||
}
|
||||
type KegTank = "input" | "output";
|
||||
|
||||
function register_keg(ctx: ServerContext) {
|
||||
// checked now, so a broken recipe stops the server rather than the keg later
|
||||
fermentable_recipes(ctx);
|
||||
const tank = (block: BlockRef, which: keyof KegData) =>
|
||||
const recipes = fermentable_recipes(ctx);
|
||||
const recipe_for = (block: BlockRef) => {
|
||||
const fluid = (block.data as KegData | undefined)?.input?.fluid;
|
||||
return fluid && recipes.find((recipe) => ctx.fluids.same(recipe.input.fluid, fluid));
|
||||
};
|
||||
const tank = (block: BlockRef, which: KegTank) =>
|
||||
ctx.fluids.tank_handler({
|
||||
tanks: 1,
|
||||
capacity: KEG_CAPACITY,
|
||||
@@ -948,14 +961,18 @@ function register_keg(ctx: ServerContext) {
|
||||
},
|
||||
set: ([fluid]) => {
|
||||
block.data = { ...block.data, [which]: fluid } satisfies KegData;
|
||||
block.set_ticking(true);
|
||||
},
|
||||
});
|
||||
|
||||
// like the furnace's, the screen fills its arrow as it ferments
|
||||
const menu_type = ctx.menus.register<BlockRef>("bworld:keg", (menu, block) => {
|
||||
menu.title = "Keg";
|
||||
menu.add_fluid_slot(tank(block, "input"), 0, { x: 55, y: 34 });
|
||||
menu.add_fluid_slot(tank(block, "output"), 0, { x: 115, y: 34, output_only: true });
|
||||
menu.add_player_inventory(7, INVENTORY_Y);
|
||||
menu.add_data("progress", () => (block.data as KegData | undefined)?.fermented ?? 0);
|
||||
menu.add_data("progress_max", () => recipe_for(block)?.input.amount ?? 0);
|
||||
menu.still_valid_near(block);
|
||||
});
|
||||
|
||||
@@ -964,6 +981,39 @@ function register_keg(ctx: ServerContext) {
|
||||
menu_type.open(player, block);
|
||||
return true;
|
||||
},
|
||||
on_tick(block) {
|
||||
const input = tank(block, "input");
|
||||
const output = tank(block, "output");
|
||||
const recipe = recipe_for(block);
|
||||
if (!recipe) {
|
||||
block.data = { ...block.data, progress: undefined, fermented: undefined } satisfies KegData;
|
||||
block.set_ticking(false);
|
||||
return;
|
||||
}
|
||||
const data = block.data as KegData;
|
||||
const fermented = data.fermented ?? 0;
|
||||
// what the next mB makes, the output's share of a recipe's worth rounded so they add up to all of it
|
||||
const share = (mb: number) => Math.floor(mb * recipe.output.amount / recipe.input.amount);
|
||||
const made = { fluid: recipe.output.fluid, amount: share(fermented + 1) - share(fermented) };
|
||||
const next = { fluid: recipe.input.fluid, amount: 1 };
|
||||
if (!input.drain(next, "simulate") || (made.amount > 0 && output.fill(made, "simulate") < made.amount)) {
|
||||
block.data = { ...data, progress: undefined } satisfies KegData;
|
||||
block.set_ticking(false);
|
||||
return;
|
||||
}
|
||||
const progress = (data.progress ?? 0) + 1;
|
||||
if (progress < recipe.ticks) {
|
||||
block.data = { ...data, progress } satisfies KegData;
|
||||
return;
|
||||
}
|
||||
input.drain(next, "execute");
|
||||
if (made.amount > 0) output.fill(made, "execute");
|
||||
block.data = {
|
||||
...block.data,
|
||||
progress: 0,
|
||||
fermented: (fermented + 1) % recipe.input.amount,
|
||||
} satisfies KegData;
|
||||
},
|
||||
fluid_handler(block) {
|
||||
const input = tank(block, "input");
|
||||
const output = tank(block, "output");
|
||||
|
||||
Reference in New Issue
Block a user