Files
bworld/tests/fishing_test.ts
T

144 lines
5.2 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 { 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);
});