// fishing: cast 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 { test_game } from "./helpers.ts"; // deno-lint-ignore no-explicit-any type Msg = any; const ROD = "bworld:fishing_rod"; // a player on a stone floor at y 99 with water next to them and 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 = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone"); } t.game.set_block(2, 100, 0, "bworld:water"); t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 }); 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 cast = () => t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top" }); 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, false if nothing did in 35 seconds const wait_for_bite = () => { t.take(1); for (let i = 0; i < 700; i++) { t.game.tick(); if (t.take(1).some((m: Msg) => m.type === "chat" && m.text.includes("biting"))) return true; } return false; }; const hooks = () => t.game.entities.all().filter((e) => e.type === "bworld:fishing_hook"); return { ...t, slot, rod, cast, reel, fish, wait_for_bite, hooks }; } Deno.test("cast into water, reel in a fish once something bites", async () => { const t = await setup(); t.cast(); assertEquals(t.rod()?.data, { cast: true }); // the hook floats on the water, and goes under when something bites assertEquals(t.hooks().length, 1); const hook = t.hooks()[0]; assertEquals([hook.x, hook.z], [2.5, 0.5]); const floating = hook.y; assert(floating > 100.7 && floating < 101, String(floating)); assert(t.wait_for_bite()); assert(hook.y < floating - 0.1, String(hook.y)); t.reel(); assertEquals(t.fish().length, 1); assertEquals(t.rod()?.data, undefined); t.game.tick(); assertEquals(t.hooks().length, 0); }); 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("right clicking air with an uncast rod does nothing", async () => { const t = await setup(); t.reel(); assertEquals(t.rod()?.data, undefined); for (let i = 0; i < 700; i++) t.game.tick(); assertEquals(t.fish().length, 0); }); 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(); t.game.set_block(2, 100, 0, "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); });