// farming: crops grow one stage every morning at 06:00 if their farmland was watered the day before, like stardew // valley, with minecraft's rules for where crops stand and what they drop import { assert, assertEquals } from "@std/assert"; import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts"; import { CYCLE_TICKS } from "$/common/time.ts"; import { get_state_value } from "$/common/utils.ts"; import { test_game } from "./helpers.ts"; // deno-lint-ignore no-explicit-any type Msg = any; // a player on a stone floor at y 99, high above the terrain, with some items async function setup(items: string[] = []) { const t = await test_game("mods"); t.join(1, "alice"); for (let x = -3; x <= 3; x++) { for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone"); } t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 }); for (const item of items) t.send(1, { type: "chat", text: `/give ${item}` }); const inventory = () => t.take(1).filter((m) => m.type === "container" && m.container === "inventory").at(-1)?.items ?? []; const hold = (id: string) => { const slot = inventory_now().findIndex((i: Msg) => i?.id === id); assert(slot >= 0 && slot < 9, `${id} isn't in the hotbar`); t.send(1, { type: "select_slot", slot }); }; // the inventory as the server has it const inventory_now = () => t.game.players()[0].inventory.to_data(); const use = (x: number, y: number, z: number) => t.send(1, { type: "use_block", x, y, z, face: "top" }); const state = (x: number, y: number, z: number, name: string) => { const info = t.game.world.get_block_info(x, y, z)!; return get_state_value(t.game.world.get_block_value(x, y, z), info, name); }; // ticks until the next 06:00 const next_morning = () => { t.game.set_time((t.game.world.day + 1) * CYCLE_TICKS - 1); t.game.tick(); }; return { ...t, inventory, inventory_now, hold, use, state, next_morning }; } Deno.test("hoe, fill the can, plant, water, and the crop grows the next morning", async () => { const t = await setup(["bworld:wood_hoe", "bworld:watering_can", "bworld:wheat_seeds 4"]); t.game.set_block(1, 100, 0, "bworld:grass"); t.game.set_block(0, 100, 1, "bworld:grass"); t.game.set_block(2, 100, 0, "bworld:water"); t.hold("bworld:wood_hoe"); t.use(1, 100, 0); assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:hoed_dirt"); // seeds only go on farmland t.hold("bworld:wheat_seeds"); t.use(0, 100, 1); assertEquals(t.game.world.get_block_id(0, 101, 1), "bworld:air"); t.use(1, 100, 0); assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:wheat"); // a new can is empty, water fills it, and watering the crop waters the farmland under it t.hold("bworld:watering_can"); t.take(1); t.use(1, 101, 0); assert(t.take(1).some((m) => m.type === "chat" && m.text.includes("empty"))); assertEquals(t.state(1, 100, 0, "watered"), 0); t.use(2, 100, 0); t.use(1, 101, 0); assertEquals(t.state(1, 100, 0, "watered"), 1); const can = t.inventory_now().find((i) => i?.id === "bworld:watering_can"); assertEquals(can?.data, { water: 31, max_water: 32 }); // nothing happens during the day for (let i = 0; i < 100; i++) t.game.tick(); assertEquals(t.state(1, 101, 0, "age"), 0); // the next morning it grows, and the farmland dries t.next_morning(); assertEquals(t.state(1, 101, 0, "age"), 1); assertEquals(t.state(1, 100, 0, "watered"), 0); // without watering it doesn't t.next_morning(); assertEquals(t.state(1, 101, 0, "age"), 1); }); Deno.test("fully grown crops drop their produce and extra seeds, young ones only a seed", async () => { const t = await setup(); const wheat = EverythingRegistry.get("blocks", "bworld:wheat")!; const dropped = () => t.game.item_entities().map((e) => e.item.to_data()); t.game.set_block(1, 100, 0, "bworld:hoed_dirt"); t.game.set_block(1, 101, 0, "bworld:wheat"); t.send(1, { type: "break_block", x: 1, y: 101, z: 0 }); assertEquals(dropped(), [{ id: "bworld:wheat_seeds", count: 1 }]); t.game.set_block(2, 100, 0, "bworld:hoed_dirt"); t.game.set_block(2, 101, 0, "bworld:wheat", undefined, 7); assertEquals(get_state_value(t.game.world.get_block_value(2, 101, 0), wheat, "age"), 7); t.send(1, { type: "break_block", x: 2, y: 101, z: 0 }); const ids = dropped().map((i) => i.id); assert(ids.includes("bworld:wheat"), `dropped ${ids}`); assert(ids.filter((id) => id === "bworld:wheat_seeds").length >= 2); }); Deno.test("digging up the farmland pops the crop off with its drops", async () => { const t = await setup(); t.game.set_block(1, 100, 0, "bworld:hoed_dirt"); t.game.set_block(1, 101, 0, "bworld:wheat"); t.send(1, { type: "break_block", x: 1, y: 100, z: 0 }); assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air"); const ids = t.game.item_entities().map((e) => e.item.type_id).sort(); assertEquals(ids, ["bworld:dirt", "bworld:wheat_seeds"]); }); Deno.test("crops grow far from every player, and mornings skipped by /time still come", async () => { const t = await setup(); const days: number[] = []; t.game.mods.after.new_day.for_mod("test").subscribe(({ day }) => days.push(day)); // thousands of blocks from anyone t.game.set_block(5000, 100, 5000, "bworld:hoed_dirt"); t.game.set_block(5000, 101, 5000, "bworld:wheat"); t.game.set_block_state(5000, 100, 5000, 1); t.next_morning(); assertEquals(days, [1]); assertEquals(t.state(5000, 101, 5000, "age"), 1); // three mornings at once, one each t.game.set_time(4 * CYCLE_TICKS); t.game.tick(); assertEquals(days, [1, 2, 3, 4]); // going back is no morning, and the mornings after it come again as time passes them t.game.set_time(CYCLE_TICKS + 10); t.game.tick(); assertEquals(days, [1, 2, 3, 4]); t.game.set_time(2 * CYCLE_TICKS); t.game.tick(); assertEquals(days, [1, 2, 3, 4, 2]); t.game.set_time(4 * CYCLE_TICKS); // the world's day is the same one scripts see assertEquals(t.game.world.day, 4); }); // plants a crop on farmland at x, 100, 0 and waters it every day for days mornings, returning its age each morning function grow(t: Awaited>, x: number, crop: string, days: number) { t.game.set_block(x, 100, 0, "bworld:hoed_dirt"); t.game.set_block(x, 101, 0, crop); const ages: number[] = []; for (let day = 0; day < days; day++) { t.game.set_block_state(x, 100, 0, 1); t.next_morning(); ages.push(t.state(x, 101, 0, "age")!); } return ages; } Deno.test("each crop takes its own number of watered days to grow", async () => { const t = await setup(); // the stage for each morning, spread over its days, fully grown on the last one assertEquals(grow(t, 0, "bworld:carrots", 3), [1, 2, 3]); assertEquals(grow(t, 1, "bworld:potatoes", 6), [0, 1, 1, 2, 2, 3]); assertEquals(grow(t, 2, "bworld:wheat", 7), [1, 2, 3, 4, 5, 6, 7]); assertEquals(grow(t, -1, "farmersdelight:onions", 4), [0, 1, 2, 3]); const cabbage = grow(t, -2, "farmersdelight:cabbages", 12); assertEquals(cabbage.at(-2), 6, "not grown a day early"); assertEquals(cabbage.at(-1), 7); // grown crops stay grown t.game.set_block_state(0, 100, 0, 1); t.next_morning(); assertEquals(t.state(0, 101, 0, "age"), 3); }); Deno.test("potatoes and carrots are planted on farmland, and eaten anywhere else", async () => { const t = await setup(["bworld:potato 3"]); t.game.set_block(1, 100, 0, "bworld:hoed_dirt"); t.game.players()[0].vitals.food = 10; t.hold("bworld:potato"); t.use(1, 100, 0); assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:potatoes"); assertEquals(t.game.players()[0].vitals.food, 10, "planted, not eaten"); // on stone it can't be planted, so it's eaten t.use(0, 99, 1); assertEquals(t.game.world.get_block_id(0, 100, 1), "bworld:air"); assertEquals(t.game.players()[0].vitals.food, 11); }); Deno.test("tomatoes take 11 days, are picked by hand, and grow back in 4", async () => { const t = await setup(); const ages = grow(t, 1, "farmersdelight:tomatoes", 11); assertEquals(ages.at(-1), 7); assertEquals(ages.at(-2), 6); t.use(1, 101, 0); const picked = t.game.item_entities().map((e) => e.item.to_data()); assertEquals(picked.length, 1); assertEquals(picked[0].id, "farmersdelight:tomato"); assert(picked[0].count >= 1 && picked[0].count <= 2); assertEquals(t.game.world.get_block_id(1, 101, 0), "farmersdelight:tomatoes", "the plant stays"); assertEquals(t.state(1, 101, 0, "age"), 4, "back to a young vine"); // 3 watered mornings isn't enough, the 4th is for (let day = 0; day < 4; day++) { t.game.set_block_state(1, 100, 0, 1); t.next_morning(); assertEquals(t.state(1, 101, 0, "age"), day < 3 ? Math.floor((7 + day + 1) * 7 / 11) : 7); } }); Deno.test("rice is planted in water, grows panicles on top without watering, and leaves its water when harvested", async () => { const t = await setup(["farmersdelight:rice 2"]); t.game.set_block(1, 99, 0, "bworld:dirt"); t.game.set_block(1, 100, 0, "bworld:water"); t.game.set_block(2, 100, 0, "bworld:hoed_dirt"); // not on land t.hold("farmersdelight:rice"); t.use(2, 100, 0); assertEquals(t.game.world.get_block_id(2, 101, 0), "bworld:air"); // into the water, standing on the dirt under it t.use(1, 100, 0); assertEquals(t.game.world.get_block_id(1, 100, 0), "farmersdelight:rice"); for (let day = 0; day < 3; day++) t.next_morning(); assertEquals(t.state(1, 100, 0, "age"), 3); assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air"); for (let day = 0; day < 3; day++) t.next_morning(); assertEquals(t.game.world.get_block_id(1, 101, 0), "farmersdelight:rice_panicles"); assertEquals(t.state(1, 101, 0, "age"), 3, "fully grown after 6 days"); assertEquals(t.state(1, 100, 0, "age"), 4, "the stalk holding them up"); // breaking the panicles harvests the whole plant t.send(1, { type: "break_block", x: 1, y: 101, z: 0 }); // the stalk goes the next tick, and its water comes back the one after t.game.tick(); t.game.tick(); const dropped = t.game.item_entities().map((e) => e.item.type_id).sort(); assertEquals(dropped, ["farmersdelight:rice", "farmersdelight:rice_panicle"]); assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:water"); assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air"); });