197 lines
8.5 KiB
TypeScript
197 lines
8.5 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// the crab pot: a block with a model converted from fishermen's trap's fish trap
|
|
import { assert, assertEquals } from "@std/assert";
|
|
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
|
import { bake_block, type ModelJson } from "$/common/block_models.ts";
|
|
import { shape_at } from "$/common/block_shapes.ts";
|
|
import { get_state_value, set_state_value } from "$/common/utils.ts";
|
|
import { ItemStack } from "$/common/inventory.ts";
|
|
import { CYCLE_TICKS } from "$/common/time.ts";
|
|
import { tag_api } from "$/common/tags.ts";
|
|
import { opened_menu, test_game } from "./helpers.ts";
|
|
|
|
Deno.test("a crab pot is a box 10 pixels tall and deep, drawn with its textures, turned by its facing", async () => {
|
|
await test_game("mods");
|
|
const pot = EverythingRegistry.get<BlockRegistry>("blocks", "bworld:crabpot")!;
|
|
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
|
|
const quads = bake_block(pot, 0, models);
|
|
assertEquals(quads.length, 6);
|
|
assertEquals([...new Set(quads.map((q) => q.texture))].sort(), [
|
|
"bworld:crabpot_bottom",
|
|
"bworld:crabpot_front",
|
|
"bworld:crabpot_side",
|
|
"bworld:crabpot_top",
|
|
]);
|
|
assertEquals(shape_at(pot, 0), [[0, 0, 3 / 16, 1, 10 / 16, 13 / 16]]);
|
|
// facing east, turned a quarter
|
|
assertEquals(shape_at(pot, 1 << 16), [[3 / 16, 0, 0, 13 / 16, 10 / 16, 1]]);
|
|
});
|
|
|
|
Deno.test("a crab pot placed in water holds it, and leaves it behind when broken", async () => {
|
|
const t = await test_game("mods");
|
|
t.join(1, "alice");
|
|
for (let x = -2; x <= 2; x++) {
|
|
for (let z = -2; z <= 2; z++) {
|
|
t.game.set_block(x, 99, z, "bworld:stone");
|
|
t.game.set_block(x, 100, z, "bworld:air");
|
|
}
|
|
}
|
|
t.game.set_block(0, 100, 0, "bworld:water");
|
|
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
|
|
t.send(1, { type: "chat", text: "/give bworld:crabpot 2" });
|
|
t.send(1, { type: "select_slot", slot: 0 });
|
|
const waterlogged = (x: number) => {
|
|
const info = t.game.world.get_block_info(x, 100, 0)!;
|
|
return get_state_value(t.game.world.get_block_value(x, 100, 0), info, "waterlogged");
|
|
};
|
|
|
|
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
|
|
assertEquals(t.game.world.get_block_id(0, 100, 0), "bworld:crabpot");
|
|
assertEquals(waterlogged(0), 1);
|
|
// on dry ground it's dry
|
|
t.send(1, { type: "use_block", x: 1, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
|
|
assertEquals(waterlogged(1), 0);
|
|
|
|
t.send(1, { type: "break_block", x: 0, y: 100, z: 0 });
|
|
assertEquals(t.game.world.get_block_id(0, 100, 0), "bworld:water");
|
|
});
|
|
|
|
Deno.test("right clicking a crab pot opens its menu: a bait slot on top and 9 output slots under it", async () => {
|
|
const t = await test_game("mods");
|
|
t.join(1, "alice");
|
|
t.game.set_block(0, 99, 0, "bworld:stone");
|
|
t.game.set_block(0, 100, 0, "bworld:crabpot");
|
|
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
|
|
t.take(1);
|
|
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] });
|
|
const opened = opened_menu(t.take(1));
|
|
assertEquals([opened.menu_type, opened.title], ["bworld:crabpot", "Crab Pot"]);
|
|
// then the player's 36
|
|
assertEquals(opened.slots.length, 1 + 9 + 36);
|
|
assertEquals(opened.slots[0], { x: 79, y: 17, icon: "bworld:empty_slot_bait" });
|
|
assertEquals(
|
|
opened.slots.slice(1, 10).map((s: { x: number; y: number }) => [s.x, s.y]),
|
|
[
|
|
...Array(9).keys(),
|
|
].map((i) => [7 + i * 18, 45]),
|
|
);
|
|
assert(opened.slots.slice(1, 10).every((s: { output?: boolean }) => s.output));
|
|
});
|
|
|
|
Deno.test("crab pots have a freshwater and a saltwater loot table for each bait, one catch a roll", async () => {
|
|
const { game } = await test_game("mods");
|
|
const catches = (table: string) => {
|
|
const items = new Set<string>();
|
|
for (let i = 0; i < 300; i++) {
|
|
const loot = game.mods.loot.roll(table, {});
|
|
assertEquals(loot.length, 1);
|
|
items.add(loot[0].id);
|
|
}
|
|
return [...items].sort();
|
|
};
|
|
assertEquals(catches("bworld:gameplay/crabpot_worms_freshwater"), [
|
|
"bworld:chorus_snail",
|
|
"bworld:crayfish",
|
|
"bworld:leech",
|
|
]);
|
|
assertEquals(catches("bworld:gameplay/crabpot_worms_saltwater"), [
|
|
"bworld:clam",
|
|
"bworld:cold_ocean_hermit_crab",
|
|
"bworld:crab",
|
|
"bworld:frozen_ocean_hermit_crab",
|
|
"bworld:lobster",
|
|
"bworld:lukewarm_ocean_hermit_crab",
|
|
"bworld:ocean_hermit_crab",
|
|
"bworld:shrimp",
|
|
"bworld:warm_ocean_hermit_crab",
|
|
]);
|
|
assertEquals(catches("bworld:gameplay/crabpot_grubs_freshwater"), [
|
|
"bworld:chorus_snail",
|
|
"bworld:crayfish",
|
|
"bworld:leech",
|
|
]);
|
|
assertEquals(catches("bworld:gameplay/crabpot_grubs_saltwater"), SEASHROOMS.toSorted());
|
|
});
|
|
|
|
const SEASHROOMS = ["white", "light_gray", "gray", "black", "brown", "red", "orange", "yellow", "lime", "green"]
|
|
.concat(["cyan", "light_blue", "blue", "purple", "magenta", "pink"])
|
|
.map((color) => `bworld:${color}_seashroom`);
|
|
|
|
Deno.test("every morning a crab pot in water uses its bait to catch something, from its bait and water's table", async () => {
|
|
const t = await test_game("mods");
|
|
t.join(1, "alice");
|
|
const tags = tag_api();
|
|
const saltwater = ["bworld:ocean_hermit_crab", "bworld:cold_ocean_hermit_crab", "bworld:frozen_ocean_hermit_crab"]
|
|
.concat(["bworld:lukewarm_ocean_hermit_crab", "bworld:warm_ocean_hermit_crab", "bworld:clam"])
|
|
.concat(["bworld:lobster", "bworld:shrimp", "bworld:crab"]);
|
|
const freshwater = ["bworld:crayfish", "bworld:chorus_snail", "bworld:leech"];
|
|
// a column whose biome is an ocean, or isn't
|
|
const column = (ocean: boolean) => {
|
|
for (let x = -2048; x <= 2048; x += 32) {
|
|
for (let z = -2048; z <= 2048; z += 32) {
|
|
if (tags.has("biomes", "bworld:is_ocean", t.game.world.biome_at(x, z)) === ocean) return [x, z];
|
|
}
|
|
}
|
|
throw new Error("no such column near spawn");
|
|
};
|
|
// a crab pot high in the air there, opened once so it has its slots, maybe in water, with bait
|
|
const pot = ([x, z]: number[], waterlogged: boolean, bait?: ItemStack) => {
|
|
const y = 150;
|
|
t.game.set_block(x, y, z, "bworld:crabpot");
|
|
const info = t.game.world.get_block_info(x, y, z)!;
|
|
t.game.set_block_state(
|
|
x,
|
|
y,
|
|
z,
|
|
set_state_value(t.game.world.get_block_value(x, y, z), info, "waterlogged", waterlogged ? 1 : 0)! >>> 16,
|
|
);
|
|
t.send(1, { type: "move", x: x + 0.5, y: y - 2, z: z + 2.5, yaw: 0, pitch: 0 });
|
|
t.send(1, { type: "use_block", x, y, z, face: "south", hit: [0.5, 0.5, 1] });
|
|
const data = t.game.world.get_tile(x, y, z)!.mod_data as { container: string };
|
|
const container = t.game.containers.get(data.container)!;
|
|
container.set_item(0, bait);
|
|
return container;
|
|
};
|
|
const next_morning = () => {
|
|
t.game.set_time((t.game.world.day + 1) * CYCLE_TICKS - 1);
|
|
t.game.tick();
|
|
};
|
|
const caught = (container: { get_item(slot: number): ItemStack | undefined }) =>
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9].flatMap((slot) => container.get_item(slot)?.type_id ?? []);
|
|
|
|
const ocean = pot(column(true), true, new ItemStack("bworld:worms", 2));
|
|
const river = pot(column(false), true, new ItemStack("bworld:worms", 1));
|
|
const [dry_x, dry_z] = column(false);
|
|
const dry = pot([dry_x + 3, dry_z], false, new ItemStack("bworld:worms", 1));
|
|
const unbaited = pot([dry_x + 6, dry_z], true, new ItemStack("bworld:dirt", 1));
|
|
const [ocean_x, ocean_z] = column(true);
|
|
const grubs_ocean = pot([ocean_x + 3, ocean_z], true, new ItemStack("bworld:grubs", 1));
|
|
const grubs_river = pot([dry_x + 9, dry_z], true, new ItemStack("bworld:grubs", 1));
|
|
next_morning();
|
|
|
|
assertEquals(caught(ocean).length, 1);
|
|
assert(saltwater.includes(caught(ocean)[0]), caught(ocean)[0]);
|
|
assertEquals(ocean.get_item(0)?.amount, 1);
|
|
assertEquals(caught(river).length, 1);
|
|
assert(freshwater.includes(caught(river)[0]), caught(river)[0]);
|
|
// its last worm is gone
|
|
assertEquals(river.get_item(0), undefined);
|
|
// not in water, or without worms, nothing
|
|
assertEquals([caught(dry), dry.get_item(0)?.amount], [[], 1]);
|
|
assertEquals([caught(unbaited), unbaited.get_item(0)?.type_id], [[], "bworld:dirt"]);
|
|
// grubs catch from their own tables, seashrooms in the ocean
|
|
assertEquals(caught(grubs_ocean).length, 1);
|
|
assert(SEASHROOMS.includes(caught(grubs_ocean)[0]), caught(grubs_ocean)[0]);
|
|
assertEquals(grubs_ocean.get_item(0), undefined);
|
|
assertEquals(caught(grubs_river).length, 1);
|
|
assert(freshwater.includes(caught(grubs_river)[0]), caught(grubs_river)[0]);
|
|
|
|
// with no room for the catch it's let go, and the worm kept
|
|
for (let slot = 1; slot <= 9; slot++) ocean.set_item(slot, new ItemStack("bworld:stone", 64));
|
|
next_morning();
|
|
assertEquals(ocean.get_item(0)?.amount, 1);
|
|
assertEquals(caught(ocean).filter((id) => id !== "bworld:stone"), []);
|
|
});
|