From f348734a41a9ae340704fc95f50e059f697ba36e Mon Sep 17 00:00:00 2001 From: Paula Date: Sat, 10 Oct 2026 20:12:40 -0300 Subject: [PATCH] Improve growable component --- mods/bworld/scripts/server.ts | 14 ++++++++++++-- tests/farming_test.ts | 9 +++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index 21c381e..9d4f81f 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -793,8 +793,11 @@ function register_fishing(ctx: ServerContext) { } interface GrowableBlockParams { + // what grows on top of it block_id: string; + // it grows on mornings whose day is a multiple of this days: number; + // how tall a stack of it grows, counting the blocks of it below max_height: number; } @@ -810,9 +813,16 @@ function register_growable_block(ctx: ServerContext) { return; } - // TODO: check max_height + // the stack it's the top of, itself included, is as tall as it gets + let height = 1; + while (height < params.max_height && ctx.world.get_block(block.x, block.y - height, block.z) === block.id) { + height += 1; + } + if (height >= params.max_height) { + return; + } - ctx.world.set_block(block.x, block.y + 1, block.z, "bworld:cactus"); + ctx.world.set_block(block.x, block.y + 1, block.z, params.block_id); }, }); } diff --git a/tests/farming_test.ts b/tests/farming_test.ts index 3233528..bee21a0 100644 --- a/tests/farming_test.ts +++ b/tests/farming_test.ts @@ -350,3 +350,12 @@ Deno.test("blocks without data run on_new_day near players, once a morning", asy t.next_morning(); assertEquals(t.game.world.get_block_id(far, 102, 0), "bworld:air"); }); + +Deno.test("a cactus grows a block a day until it's 4 tall", async () => { + const t = await setup(); + t.game.set_block(0, 100, 0, "bworld:sand"); + t.game.set_block(0, 101, 0, "bworld:cactus"); + for (let day = 0; day < 6; day++) t.next_morning(); + const column = [101, 102, 103, 104, 105].map((y) => t.game.world.get_block_id(0, y, 0)); + assertEquals(column, ["bworld:cactus", "bworld:cactus", "bworld:cactus", "bworld:cactus", "bworld:air"]); +});