Files
bworld/tests/fishing_test.ts
T
2026-10-11 12:16:49 -03:00

179 lines
6.6 KiB
TypeScript

// fishing: throw the hook into water, wait for a bite, reel it in before it gets away
import { assert, assertEquals } from "@std/assert";
import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { ItemStack } from "$/common/inventory.ts";
import { item_texture } from "$/common/utils.ts";
import { tag_api } from "$/common/tags.ts";
import { CYCLE_TICKS } from "$/common/time.ts";
import { biome_season } from "$/common/worldgen/biomes.ts";
import { test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
const ROD = "bworld:fishing_rod";
// below where the hook floats on the pond's surface at y 100, even at the bottom of its bobbing, see the bworld mod's
// fishing
const HOOK_UNDER = 0.3;
// looking north and down: a throw lands in the pond. steeper, it lands on the stone in front of the player
const AT_POND = -0.5;
const AT_FLOOR = -1.3;
// a player on a stone floor at y 99, looking north (yaw 0) at a pond in it from z -3 to -9, with a rod in hand
async function setup() {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -3; x <= 3; x++) {
for (let z = -10; z <= 3; z++) {
t.game.set_block(x, 99, z, z <= -3 && z >= -9 ? "bworld:water" : "bworld:stone");
if (z <= -3 && z >= -9) t.game.set_block(x, 98, z, "bworld:stone");
}
}
t.send(1, { type: "chat", text: `/give ${ROD}` });
const inventory = () => t.game.players()[0].inventory.to_data();
const slot = inventory().findIndex((i: Msg) => i?.id === ROD);
t.send(1, { type: "select_slot", slot });
const rod = () => inventory()[slot];
const hooks = () => t.game.entities.all().filter((e) => e.type === "bworld:fishing_hook");
const aim = (pitch: number) => t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch });
// throws the hook and lets it fly until it lands
const cast = (pitch = AT_POND) => {
aim(pitch);
t.send(1, { type: "use_item" });
for (let i = 0; i < 20; i++) t.game.tick();
};
const reel = () => t.send(1, { type: "use_item" });
const fish = () => inventory().filter((i: Msg) => i?.id === "bworld:cod" || i?.id === "bworld:salmon");
// ticks until something bites, which pulls the hook under the water. false if nothing did in 35 seconds
const wait_for_bite = () => {
for (let i = 0; i < 700; i++) {
t.game.tick();
if (hooks()[0]?.y < 100 - HOOK_UNDER) return true;
}
return false;
};
return { ...t, slot, rod, aim, cast, reel, fish, wait_for_bite, hooks };
}
Deno.test("the hook flies forward from the player and floats where it lands in water", async () => {
const t = await setup();
t.aim(AT_POND);
t.send(1, { type: "use_item" });
assertEquals(t.rod()?.data, { cast: true });
assertEquals(t.hooks().length, 1);
const hook = t.hooks()[0];
const start = hook.z;
t.game.tick();
assert(hook.z < start, `${hook.z} isn't north of ${start}`);
for (let i = 0; i < 20; i++) t.game.tick();
assert(hook.z <= -2 && hook.z >= -9, String(hook.z));
assert(Math.abs(hook.x - 0.5) < 0.01, String(hook.x));
assert(hook.y > 100 - HOOK_UNDER && hook.y < 100, String(hook.y));
});
Deno.test("cast into water, reel in a fish once something bites", async () => {
const t = await setup();
t.cast();
const floating = t.hooks()[0].y;
assert(t.wait_for_bite());
assert(t.hooks()[0].y < floating - 0.1, String(t.hooks()[0].y));
t.reel();
assertEquals(t.fish().length, 1);
assertEquals(t.rod()?.data, undefined);
t.game.tick();
assertEquals(t.hooks().length, 0);
});
Deno.test("right clicking a block throws the hook too", async () => {
const t = await setup();
t.aim(AT_POND);
t.send(1, { type: "use_block", x: 0, y: 99, z: -1, face: "top" });
assertEquals(t.rod()?.data, { cast: true });
assertEquals(t.hooks().length, 1);
});
Deno.test("reeling in before a bite catches nothing", async () => {
const t = await setup();
t.cast();
t.reel();
assertEquals(t.fish().length, 0);
assertEquals(t.rod()?.data, undefined);
});
Deno.test("a hook that lands on stone lies there, and nothing bites", async () => {
const t = await setup();
t.cast(AT_FLOOR);
const hook = t.hooks()[0];
assert(hook.z > -3, `${hook.z} is in the pond`);
assert(Math.abs(hook.y + 0.125 - 100) < 0.01, String(hook.y));
assert(!t.wait_for_bite());
assertEquals(t.rod()?.data, { cast: true });
t.reel();
assertEquals(t.fish().length, 0);
assertEquals(t.rod()?.data, undefined);
});
Deno.test("switching away from the rod takes the line out", async () => {
const t = await setup();
t.cast();
t.send(1, { type: "select_slot", slot: (t.slot + 1) % 9 });
t.game.tick();
assertEquals(t.rod()?.data, undefined);
t.game.tick();
assertEquals(t.hooks().length, 0);
t.send(1, { type: "select_slot", slot: t.slot });
assert(!t.wait_for_bite());
});
Deno.test("the line comes out when the water is gone", async () => {
const t = await setup();
t.cast();
const hook = t.hooks()[0];
t.game.set_block(Math.floor(hook.x), 99, Math.floor(hook.z), "bworld:stone");
t.game.tick();
assertEquals(t.rod()?.data, undefined);
t.game.tick();
assertEquals(t.hooks().length, 0);
});
Deno.test("a cast rod shows its cast texture", async () => {
await test_game("mods");
const rod = new ItemStack(ROD, 1);
assertEquals(item_texture(rod), "bworld:fishing_rod");
rod.data = { cast: true };
assertEquals(item_texture(rod), "bworld:fishing_rod_cast");
assertEquals(EverythingRegistry.get<ItemRegistry>("items", ROD)?.max_stack, 1);
});
Deno.test("a fish with biomes is only caught in them, by id or #tag", async () => {
const t = await test_game("mods");
// summer, when salmon and cod are both caught
while (biome_season("bworld:plains", t.game.world.time) !== "summer") t.game.world.time += CYCLE_TICKS;
const tags = tag_api();
// a column near spawn whose biome matches, in summer or winter
const find = (matches: (biome: string) => boolean) => {
for (let x = -4096; x <= 4096; x += 64) {
for (let z = -4096; z <= 4096; z += 64) {
const biome = t.game.world.biome_at(x, z);
const season = biome_season(biome, t.game.world.time);
if (matches(biome) && (season === "summer" || season === "winter")) return { x, y: 64, z };
}
}
throw new Error("no such biome near spawn");
};
const caught = (position: { x: number; y: number; z: number }) =>
new Set(
Array.from({ length: 200 }, () => t.game.mods.loot.roll("bworld:gameplay/fishing", { position })).flat()
.map((item) => item.id),
);
// salmon's biomes are #bworld:is_ocean and #bworld:is_forest, cod has none
assertEquals(
caught(find((b) => tags.has("biomes", "bworld:is_ocean", b))),
new Set(["bworld:cod", "bworld:salmon"]),
);
assertEquals(
caught(find((b) => !tags.has("biomes", "bworld:is_ocean", b) && !tags.has("biomes", "bworld:is_forest", b))),
new Set(["bworld:cod"]),
);
});