144 lines
5.8 KiB
TypeScript
144 lines
5.8 KiB
TypeScript
// 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: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: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<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);
|
|
});
|