362 lines
15 KiB
TypeScript
362 lines
15 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// 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 { container_items, stone_floor, 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");
|
|
stone_floor(t.game);
|
|
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 = () => container_items(t.take(1), "inventory") ?? [];
|
|
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:copper_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:copper_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:copper_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<BlockRegistry>("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<ReturnType<typeof setup>>, 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();
|
|
// they only grow in summer, which starts on day 50
|
|
t.game.set_time(49 * CYCLE_TICKS);
|
|
t.game.tick();
|
|
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");
|
|
});
|
|
|
|
Deno.test("a crop dies into a dead bush the first morning out of its seasons", async () => {
|
|
const t = await setup();
|
|
// carrots grow in spring and autumn. the last morning of spring they grow
|
|
t.game.set_time(47 * CYCLE_TICKS);
|
|
t.game.tick();
|
|
assertEquals(grow(t, 0, "bworld:carrots", 1), [1]);
|
|
// and the first of summer they die, dropping nothing
|
|
t.game.set_block_state(0, 100, 0, 1);
|
|
t.next_morning();
|
|
assertEquals(t.game.world.day, 49);
|
|
assertEquals(t.game.world.get_block_id(0, 101, 0), "bworld:dead_bush");
|
|
assertEquals(t.game.item_entities(), []);
|
|
// still standing on its farmland
|
|
assertEquals(t.game.world.get_block_id(0, 100, 0), "bworld:hoed_dirt");
|
|
});
|
|
|
|
Deno.test("a two block crop dies whole, its top going without dropping anything", async () => {
|
|
const t = await setup();
|
|
t.game.set_block(1, 99, 0, "bworld:dirt");
|
|
t.game.set_block(1, 100, 0, "farmersdelight:rice");
|
|
for (let day = 0; day < 6; day++) t.next_morning();
|
|
assertEquals(t.game.world.get_block_id(1, 101, 0), "farmersdelight:rice_panicles");
|
|
|
|
// rice grows in autumn, dies in winter, winter starts on day 148
|
|
t.game.set_time(147 * CYCLE_TICKS - 1);
|
|
t.game.tick();
|
|
t.game.tick();
|
|
assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:dead_bush");
|
|
assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air");
|
|
assertEquals(t.game.item_entities(), []);
|
|
});
|
|
|
|
Deno.test("beetroots, cotton and strawberries grow, and strawberries and cotton are picked and grow back", async () => {
|
|
const t = await setup();
|
|
// beetroots and strawberries grow in spring
|
|
assertEquals(grow(t, 0, "bworld:beetroots", 6), [1, 2, 3, 4, 5, 7]);
|
|
const strawberries = grow(t, 1, "bworld:strawberries", 8);
|
|
assertEquals(strawberries.at(-2), 4, "not grown a day early");
|
|
assertEquals(strawberries.at(-1), 5);
|
|
|
|
t.use(1, 101, 0);
|
|
const picked = t.game.item_entities().map((e) => e.item.to_data());
|
|
assertEquals(picked.map((item) => item.id), ["bworld:strawberry"]);
|
|
assertEquals(t.state(1, 101, 0, "age"), 2, "back to a young bush");
|
|
|
|
// cotton grows in summer, which starts on day 50
|
|
t.game.set_time(49 * CYCLE_TICKS);
|
|
t.game.tick();
|
|
assertEquals(grow(t, 2, "bworld:cotton", 10), [0, 1, 2, 2, 3, 4, 4, 5, 6, 7]);
|
|
// picked like strawberries, it grows back in 3 days
|
|
t.use(2, 101, 0);
|
|
const cotton = t.game.item_entities().map((e) => e.item.to_data()).filter((item) => item.id === "bworld:cotton");
|
|
assertEquals(cotton.length, 1);
|
|
assert(cotton[0].count >= 1 && cotton[0].count <= 2);
|
|
assertEquals(t.state(2, 101, 0, "age"), 4, "back to a young plant");
|
|
assertEquals(t.game.world.get_block_id(2, 101, 0), "bworld:cotton", "the plant stays");
|
|
for (let day = 0; day < 3; day++) {
|
|
t.game.set_block_state(2, 100, 0, 1);
|
|
t.next_morning();
|
|
}
|
|
assertEquals(t.state(2, 101, 0, "age"), 7);
|
|
});
|
|
|
|
Deno.test("a pumpkin plant becomes a pumpkin the morning it's grown, and a melon breaks into slices", async () => {
|
|
const t = await setup(["bworld:bone_meal 64"]);
|
|
// pumpkins grow in autumn, from day 99
|
|
t.game.set_time(98 * CYCLE_TICKS);
|
|
t.game.tick();
|
|
// every stage shows, the last the day before
|
|
const ages = grow(t, 0, "bworld:pumpkin_plant", 11);
|
|
assertEquals(ages, [0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7]);
|
|
t.game.set_block_state(0, 100, 0, 1);
|
|
t.next_morning();
|
|
assertEquals(t.game.world.get_block_id(0, 101, 0), "bworld:pumpkin");
|
|
assertEquals(t.game.world.get_tile(0, 101, 0), undefined, "no crop data left behind");
|
|
|
|
// bone meal ripens it too
|
|
t.game.set_block(1, 100, 0, "bworld:hoed_dirt");
|
|
t.game.set_block(1, 101, 0, "bworld:melon_plant");
|
|
t.hold("bworld:bone_meal");
|
|
for (let i = 0; i < 12; i++) t.use(1, 101, 0);
|
|
assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:melon");
|
|
t.send(1, { type: "break_block", x: 1, y: 101, z: 0 });
|
|
const slices = t.game.item_entities().map((e) => e.item.to_data());
|
|
assertEquals(slices.map((item) => item.id), ["bworld:melon_slice"]);
|
|
assert(slices[0].count >= 3 && slices[0].count <= 7);
|
|
});
|
|
|
|
Deno.test("blocks without data run on_new_day near players, once a morning", async () => {
|
|
const t = await setup();
|
|
// a cactus without data, like one world generation made
|
|
t.game.set_block(0, 100, 0, "bworld:sand");
|
|
t.game.set_block(0, 101, 0, "bworld:cactus");
|
|
t.game.world.remove_tile(0, 101, 0);
|
|
t.next_morning();
|
|
assertEquals(t.game.world.get_block_id(0, 102, 0), "bworld:cactus", "grew a block");
|
|
assertEquals(t.game.world.get_block_id(0, 103, 0), "bworld:air", "the new block waits for tomorrow");
|
|
|
|
// far from every player, in a chunk that isn't kept, nothing runs
|
|
const far = 16 * 40;
|
|
t.game.set_block(far, 100, 0, "bworld:sand");
|
|
t.game.set_block(far, 101, 0, "bworld:cactus");
|
|
t.game.world.remove_tile(far, 101, 0);
|
|
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"]);
|
|
});
|