diff --git a/docs/modding/resources/recipes.md b/docs/modding/resources/recipes.md index 00a0f1c..0521302 100644 --- a/docs/modding/resources/recipes.md +++ b/docs/modding/resources/recipes.md @@ -121,13 +121,18 @@ can add one for anything else. It's checked when the server starts, like juicing "type": "bworld:fermentable", "input": { "fluid": { "id": "copper_tools:syrup", "data": { "tree": "bworld:maple" } }, "amount": 1000 }, "output": { "fluid": { "id": "copper_tools:maple_liqueur" }, "amount": 500 }, - "ticks": 2400 + "ticks": 20 } } ``` `input` and `output` are each a fluid with an amount in mB: that much of the input ferments into that much of the -output. `ticks` is how long it takes, 20 to a second. +output. `ticks` is how long each mB of the input takes, 20 to a second. + +The keg ferments what's in its input tank by the first recipe for that fluid, 1 mB every `ticks` ticks, while its output +tank has room for what that makes. The output comes a bit at a time, spread so each recipe's worth of input makes the +whole `output`: the example above makes 1 mB of liqueur for every 2 mB of syrup. It only ferments near a player, like a +furnace smelts. ## Choices diff --git a/mods/bworld/scripts/client.ts b/mods/bworld/scripts/client.ts index f63be27..5ba6d46 100644 --- a/mods/bworld/scripts/client.ts +++ b/mods/bworld/scripts/client.ts @@ -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); }); diff --git a/mods/bworld/scripts/fermenting.ts b/mods/bworld/scripts/fermenting.ts index 014638a..6c5a329 100644 --- a/mods/bworld/scripts/fermenting.ts +++ b/mods/bworld/scripts/fermenting.ts @@ -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 diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index f747ae4..6bd6dd4 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -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("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"); diff --git a/tests/keg_test.ts b/tests/keg_test.ts index f59cdfa..2770bba 100644 --- a/tests/keg_test.ts +++ b/tests/keg_test.ts @@ -5,6 +5,7 @@ import { assertEquals } from "@std/assert"; import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts"; import { ItemStack } from "$/common/inventory.ts"; +import type { FermentableRecipe } from "$/mods/bworld/scripts/fermenting.ts"; import { opened_menu, test_game } from "./helpers.ts"; const JUICE = { id: "bworld:juice" }; @@ -111,3 +112,52 @@ Deno.test("the keg's fluid handler only fills its input and only drains its outp assertEquals(handler().drain(40, "execute"), { fluid: JUICE, amount: 40 }); assertEquals(keg().output, { fluid: JUICE, amount: 60 }); }); + +Deno.test("a keg ferments 1 mB each time its progress reaches the recipe's ticks, a recipe's worth into all its output", async () => { + const { t } = await setup(); + const [recipe] = t.game.recipes.by_type.get("bworld:fermentable") as unknown as FermentableRecipe[]; + const handler = () => t.game.mods.block_fluid_handler(0, 100, 0, "top")!; + const tile = () => t.game.world.get_tile(0, 100, 0)!; + const tanks = () => [handler().fluid_in_tank(0), handler().fluid_in_tank(1)]; + const ticks = (n: number) => { + for (let i = 0; i < n; i++) t.game.tick(); + }; + // what the first mb of a recipe's worth make + const made = (mb: number) => Math.floor(mb * recipe.output.amount / recipe.input.amount); + const input = (amount: number) => amount > 0 ? { fluid: recipe.input.fluid, amount } : undefined; + const output = (amount: number) => amount > 0 ? { fluid: recipe.output.fluid, amount } : undefined; + + const amount = recipe.input.amount; + handler().fill({ fluid: recipe.input.fluid, amount: amount + 10 }, "execute"); + ticks(recipe.ticks - 1); + assertEquals(tanks(), [input(amount + 10), undefined]); + ticks(1); + assertEquals(tanks(), [input(amount + 9), output(made(1))]); + // a recipe's worth makes all of its output + ticks(recipe.ticks * (amount - 1)); + assertEquals(tanks(), [input(10), output(recipe.output.amount)]); + ticks(recipe.ticks * 10 + 1); + assertEquals(tanks(), [undefined, output(recipe.output.amount + made(10))]); + + // with nothing it can ferment, it stops ticking, and filling it wakes it + assertEquals(tile().ticking, false); + handler().fill({ fluid: recipe.input.fluid, amount: 1 }, "execute"); + assertEquals(tile().ticking, undefined); + + // something without a recipe stays as it is + ticks(recipe.ticks + 1); + handler().fill({ fluid: { id: "bworld:wine" }, amount: 100 }, "execute"); + ticks(recipe.ticks * 2); + assertEquals(tanks()[0], { fluid: { id: "bworld:wine" }, amount: 100 }); +}); + +Deno.test("a keg doesn't ferment into an output with another fluid in it", async () => { + const { t } = await setup(); + const [recipe] = t.game.recipes.by_type.get("bworld:fermentable") as unknown as FermentableRecipe[]; + const other = { fluid: { id: "bworld:juice" }, amount: 100 }; + t.game.mods.block_ref(0, 100, 0, "bworld:keg").data = { output: other }; + t.game.mods.block_fluid_handler(0, 100, 0, "top")!.fill(recipe.input, "execute"); + for (let i = 0; i < recipe.ticks * 2; i++) t.game.tick(); + const keg = t.game.world.get_tile(0, 100, 0)!.mod_data as { input?: unknown; output?: unknown }; + assertEquals([keg.input, keg.output], [recipe.input, other]); +});