// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // fishing: throw the hook into water, wait for a bite, reel it in before it gets away import { assert, assertAlmostEquals, 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 { menu_data, opened_menu, 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" }); // what they caught: items with bworld:fish const fish = () => inventory().filter((i: Msg) => i && EverythingRegistry.get("items", i.id)?.components?.["bworld:fish"] !== undefined ); // 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(); await with_minigames(async () => { 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); }, null); }); 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("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, 64, 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_river assert(caught(find((b) => tags.has("biomes", "bworld:is_ocean", b))).has("bworld:salmon")); assert( !caught(find((b) => !tags.has("biomes", "bworld:is_ocean", b) && !tags.has("biomes", "bworld:is_river", b))) .has("bworld:salmon"), ); }); // every fish plays the minigame until the test is done, or none with null async function with_minigames(test: () => Promise, minigame: string | null = "test") { const fishes = EverythingRegistry.entries("items") .map(([, item]) => item.components?.["bworld:fish"] as { minigame?: string } | undefined) .filter((fish) => fish !== undefined); const before = fishes.map((fish) => fish.minigame); const set = (fish: { minigame?: string }, value: string | undefined) => { if (value === undefined) delete fish.minigame; else fish.minigame = value; }; fishes.forEach((fish) => set(fish, minigame ?? undefined)); try { await test(); } finally { fishes.forEach((fish, i) => set(fish, before[i])); } } Deno.test("a fish with a minigame is caught once it's won, in its menu", async () => { const t = await setup(); await with_minigames(async () => { t.cast(); assert(t.wait_for_bite()); t.take(1); t.reel(); const opened = opened_menu(t.take(1)); assertEquals(opened?.menu_type, "bworld:fishing_minigame"); assertEquals(opened.open_data.minigame, "test"); assertEquals(t.fish().length, 0); t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); assertEquals(t.fish().length, 0); t.send(1, { type: "menu_button", menu: opened.id, button: 0 }); assertEquals(t.fish().length, 1); assertEquals(t.fish()[0]?.id, opened.open_data.fish.id); t.game.tick(); assert(t.take(1).some((m: Msg) => m.type === "close_menu")); // once t.send(1, { type: "menu_button", menu: opened.id, button: 0 }); assertEquals(t.fish().length, 1); }); }); Deno.test("closing a fish's minigame lets it get away", async () => { const t = await setup(); await with_minigames(async () => { t.cast(); assert(t.wait_for_bite()); t.take(1); t.reel(); const opened = opened_menu(t.take(1)); t.send(1, { type: "close_menu", menu: opened.id }); t.send(1, { type: "menu_button", menu: opened.id, button: 0 }); assertEquals(t.fish().length, 0); }); }); Deno.test("/fish reels in a fish without casting, its minigame and all", async () => { const t = await setup(); await with_minigames(async () => { t.send(1, { type: "chat", text: "/fish bworld:cod" }); assertEquals(t.fish().map((i: Msg) => i.id), ["bworld:cod"]); }, null); t.send(1, { type: "chat", text: "/fish stone" }); assertEquals(t.fish().length, 1); assert(t.take(1).some((m: Msg) => JSON.stringify(m).includes("bworld:stone isn't a fish"))); await with_minigames(async () => { t.send(1, { type: "chat", text: "/fish salmon" }); const opened = opened_menu(t.take(1)); assertEquals(opened?.menu_type, "bworld:fishing_minigame"); assertEquals(opened.open_data.fish, { id: "bworld:salmon", count: 1 }); t.send(1, { type: "menu_button", menu: opened.id, button: 0 }); assertEquals(t.fish().map((i: Msg) => i.id).sort(), ["bworld:cod", "bworld:salmon"]); }); }); // cod's bworld:fish difficulty until the test is done, or none with undefined async function with_difficulty(difficulty: number | undefined, test: () => Promise) { const fish = EverythingRegistry.get("items", "bworld:cod")!.components!["bworld:fish"] as { difficulty?: number; }; const before = fish.difficulty; if (difficulty === undefined) delete fish.difficulty; else fish.difficulty = difficulty; try { await test(); } finally { if (before === undefined) delete fish.difficulty; else fish.difficulty = before; } } Deno.test("the hold minigame fills while the mouse is held and empties by the fish's difficulty, landing it when full", async () => { const t = await setup(); const progress = () => menu_data(t.take(1))?.progress; await with_minigames(() => with_difficulty(100, async () => { t.send(1, { type: "chat", text: "/fish cod" }); const opened = opened_menu(t.take(1)); assertEquals(opened.data, { progress: 100, max: 200 }); assertEquals(opened.open_data.difficulty, 100); t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); for (let i = 0; i < 50; i++) t.game.tick(); assertEquals(progress(), 150); // let go for a bit: at 100 it empties twice as fast as it fills t.send(1, { type: "menu_button", menu: opened.id, button: 0 }); for (let i = 0; i < 10; i++) t.game.tick(); assertEquals(progress(), 130); t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); for (let i = 0; i < 69; i++) t.game.tick(); assertEquals(t.fish().length, 0); t.game.tick(); assertEquals(t.fish().map((i: Msg) => i.id), ["bworld:cod"]); t.game.tick(); assert(t.take(1).some((m: Msg) => m.type === "close_menu")); }), "hold"); }); Deno.test("the hold minigame empties 1.1 seconds a second at the default difficulty, losing the fish when empty", async () => { const t = await setup(); await with_minigames(() => with_difficulty(undefined, async () => { t.send(1, { type: "chat", text: "/fish cod" }); const opened = opened_menu(t.take(1)); assertEquals(opened.open_data.difficulty, 10); for (let i = 0; i < 10; i++) t.game.tick(); assertAlmostEquals(menu_data(t.take(1))?.progress, 89); // 100 / 1.1 is 90.9 ticks for (let i = 0; i < 80; i++) t.game.tick(); assert(!t.take(1).some((m: Msg) => m.type === "close_menu")); for (let i = 0; i < 2; i++) t.game.tick(); assert(t.take(1).some((m: Msg) => m.type === "close_menu")); t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); for (let i = 0; i < 200; i++) t.game.tick(); assertEquals(t.fish().length, 0); }), "hold"); }); // with Math.random at 0.99, the valley minigame's fish never moves, staying at the bottom async function with_still_fish(test: () => Promise) { const random = Math.random; Math.random = () => 0.99; try { await test(); } finally { Math.random = random; } } Deno.test("the valley minigame loses the fish while the bar is off it", async () => { const t = await setup(); await with_minigames(() => with_still_fish(async () => { t.send(1, { type: "chat", text: "/fish cod" }); const opened = opened_menu(t.take(1)); assertEquals(opened.data, { bar: 0, bar_size: 36, fish: 0, points: 24, max_points: 120 }); // the bar goes up to the top, away from the fish t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); let closed = false; for (let i = 0; i < 200 && !closed; i++) { t.game.tick(); closed = t.take(1).some((m: Msg) => m.type === "close_menu"); } assert(closed); assertEquals(t.fish().length, 0); }), "valley"); }); Deno.test("the valley minigame lands the fish when the bar stays on it", async () => { const t = await setup(); await with_minigames(() => with_still_fish(async () => { t.send(1, { type: "chat", text: "/fish cod" }); // the bar stays at the bottom with the fish. 96 points to go, one a tick for (let i = 0; i < 95; i++) t.game.tick(); assertEquals(t.fish().length, 0); t.game.tick(); assertEquals(t.fish().map((i: Msg) => i.id), ["bworld:cod"]); }), "valley"); }); Deno.test("harder valley fish move around more", async () => { const t = await setup(); // how far the fish moves a tick on average at a difficulty, over a few minigames of up to 10 seconds const speed = async (difficulty: number) => { let moved = 0; let ticks = 0; await with_difficulty(difficulty, async () => { for (let game = 0; game < 5; game++) { t.send(1, { type: "chat", text: "/fish cod" }); let fish = opened_menu(t.take(1)).data.fish; for (let i = 0; i < 200; i++) { t.game.tick(); const messages = t.take(1); const now = menu_data(messages)?.fish ?? fish; moved += Math.abs(now - fish); fish = now; ticks++; if (messages.some((m: Msg) => m.type === "close_menu")) break; } } }); return moved / ticks; }; await with_minigames(async () => { const easy = await speed(5); const hard = await speed(100); assert(hard > easy * 2, `${hard} isn't much more than ${easy}`); }, "valley"); }); Deno.test("the ring minigame lands the fish when clicked with the green ring in the gray one", async () => { const t = await setup(); // at difficulty 0 it grows 1 a tick await with_minigames(() => with_difficulty(0, async () => { const open = () => { t.send(1, { type: "chat", text: "/fish cod" }); return opened_menu(t.take(1)); }; const closes = () => t.take(1).some((m: Msg) => m.type === "close_menu"); // too early: the green ring starts at 4, the gray one is from 44 to 52 let opened = open(); assertEquals(opened.data, { radius: 4, rings: 1, checked: 0, ring_radius: 48, ring_spacing: 16, ring_width: 8, }); for (let i = 0; i < 30; i++) t.game.tick(); t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); t.game.tick(); assert(closes()); assertEquals(t.fish().length, 0); // too late: past 52 it's gone opened = open(); for (let i = 0; i < 48; i++) t.game.tick(); assert(!closes()); t.game.tick(); t.game.tick(); assert(closes()); assertEquals(t.fish().length, 0); // in it opened = open(); for (let i = 0; i < 44; i++) t.game.tick(); assertEquals(menu_data(t.take(1))?.radius, 48); t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); assertEquals(t.fish().map((i: Msg) => i.id), ["bworld:cod"]); }), "ring"); }); Deno.test("the ring minigame's green ring grows faster the harder the fish is", async () => { const t = await setup(); let radius = 0; // how big it is after 10 ticks, from 4 const after_10_ticks = (difficulty: number | undefined) => with_difficulty(difficulty, async () => { t.send(1, { type: "chat", text: "/fish cod" }); t.take(1); for (let i = 0; i < 10; i++) t.game.tick(); radius = menu_data(t.take(1))?.radius; }); await with_minigames(async () => { await after_10_ticks(undefined); assertAlmostEquals(radius, 4 + 11); await after_10_ticks(100); assertEquals(radius, 4 + 20); }, "ring"); }); Deno.test("the ring minigame has a ring for every 30 difficulty", async () => { const t = await setup(); const rings: number[] = []; await with_minigames(async () => { for (const difficulty of [0, 30, 31, 60, 61, 90, 91, 120]) { await with_difficulty(difficulty, async () => { t.send(1, { type: "chat", text: "/fish cod" }); rings.push(opened_menu(t.take(1)).data.rings); }); } }, "ring"); assertEquals(rings, [1, 1, 2, 2, 3, 3, 4, 4]); }); Deno.test("the ring minigame needs every ring checked, and missing one loses it", async () => { const t = await setup(); const closes = () => t.take(1).some((m: Msg) => m.type === "close_menu"); // at 60 there are 2 rings, at 48 and 64, and the green ring grows 1.6 a tick from 4 await with_minigames(() => with_difficulty(60, async () => { t.send(1, { type: "chat", text: "/fish cod" }); let opened = opened_menu(t.take(1)); for (let i = 0; i < 26; i++) t.game.tick(); // 45.6 t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); assertEquals(menu_data(t.take(1))?.checked, 1); assertEquals(t.fish().length, 0); for (let i = 0; i < 11; i++) t.game.tick(); // 63.2 t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); assertEquals(t.fish().map((i: Msg) => i.id), ["bworld:cod"]); // checking the first and letting the green ring past the second t.send(1, { type: "chat", text: "/fish cod" }); opened = opened_menu(t.take(1)); for (let i = 0; i < 26; i++) t.game.tick(); t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); for (let i = 0; i < 15; i++) t.game.tick(); // 71.2 assert(closes()); assertEquals(t.fish().length, 1); // clicking twice in the first: the second click isn't in the second ring t.send(1, { type: "chat", text: "/fish cod" }); opened = opened_menu(t.take(1)); for (let i = 0; i < 26; i++) t.game.tick(); t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); t.game.tick(); assert(closes()); assertEquals(t.fish().length, 1); }), "ring"); }); Deno.test("the maze minigame lands the fish when its screen says the mouse got through, and loses it on a wall", async () => { const t = await setup(); const closes = () => t.take(1).some((m: Msg) => m.type === "close_menu"); await with_minigames(async () => { t.send(1, { type: "chat", text: "/fish cod" }); let opened = opened_menu(t.take(1)); t.send(1, { type: "menu_button", menu: opened.id, button: 0 }); t.game.tick(); assert(closes()); assertEquals(t.fish().length, 0); t.send(1, { type: "chat", text: "/fish cod" }); opened = opened_menu(t.take(1)); t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); assertEquals(t.fish().map((i: Msg) => i.id), ["bworld:cod"]); }, "maze"); }); Deno.test("the maze minigame loses the fish after 8 seconds, whatever the difficulty", async () => { const t = await setup(); const closes = () => t.take(1).some((m: Msg) => m.type === "close_menu"); await with_minigames(async () => { for (const difficulty of [0, 100]) { await with_difficulty(difficulty, async () => { t.send(1, { type: "chat", text: "/fish cod" }); assertEquals(opened_menu(t.take(1)).data, { ticks_left: 160, time: 160 }); for (let i = 0; i < 159; i++) t.game.tick(); assert(!closes()); t.game.tick(); t.game.tick(); assert(closes()); assertEquals(t.fish().length, 0); }); } }, "maze"); }); Deno.test("the chess minigame lands the fish when its screen says the mate was played, and has the maze's time", async () => { const t = await setup(); const closes = () => t.take(1).some((m: Msg) => m.type === "close_menu"); await with_minigames(async () => { t.send(1, { type: "chat", text: "/fish cod" }); let opened = opened_menu(t.take(1)); assertEquals(opened.data, { ticks_left: 160, time: 160 }); t.send(1, { type: "menu_button", menu: opened.id, button: 0 }); t.game.tick(); assert(closes()); assertEquals(t.fish().length, 0); t.send(1, { type: "chat", text: "/fish cod" }); opened = opened_menu(t.take(1)); t.send(1, { type: "menu_button", menu: opened.id, button: 1 }); assertEquals(t.fish().map((i: Msg) => i.id), ["bworld:cod"]); t.send(1, { type: "chat", text: "/fish cod" }); t.take(1); for (let i = 0; i < 161; i++) t.game.tick(); assert(closes()); assertEquals(t.fish().length, 1); }, "chess"); }); Deno.test("fishing in lava catches the lava fish, from lava's own table", async () => { const t = await setup(); const lava_fish = [ "bworld:lava_eel", "bworld:magma_slimefish", "bworld:mother_moon", "bworld:prismfish", "bworld:totemfish", "bworld:true_goldfish", "bworld:vampire_squid", ]; // the pond is lava instead for (let x = -3; x <= 3; x++) { for (let z = -9; z <= -3; z++) t.game.set_block(x, 99, z, "bworld:lava"); } await with_minigames(async () => { for (let i = 0; i < 5; i++) { t.cast(); assertEquals(t.hooks().length, 1); assert(t.wait_for_bite()); t.reel(); t.game.tick(); } // the same fish twice stacks in one slot const caught = t.fish().map((i: Msg) => i.id); assertEquals(t.fish().reduce((count: number, i: Msg) => count + i.count, 0), 5); assert(caught.every((id: string) => lava_fish.includes(id)), caught.join(", ")); }, null); // and no lava fish in water's table const water = EverythingRegistry.get<{ pools: { entries: { item: string }[] }[] }>( "loot_tables", "bworld:gameplay/fishing", )!; assert(water.pools[0].entries.every((e) => !lava_fish.includes(e.item))); });