Make fishing rods not actually target water

This commit is contained in:
2026-10-07 18:00:37 -03:00
parent b128da0631
commit b6b10e35e2
10 changed files with 374 additions and 70 deletions
+40
View File
@@ -7,6 +7,7 @@ import {
EMPTY_SHAPE,
FULL_SHAPE,
model_shape,
raycast_blocks,
type Shape,
} from "$/common/block_shapes.ts";
import { block_from_json, validate_block } from "$/common/mod_data.ts";
@@ -118,3 +119,42 @@ Deno.test("the outline is the edges of the shape's boxes, a little outside them"
// edges two boxes share are drawn once
assertEquals(shape_edges([...SLAB, ...SLAB]).length, 12);
});
// a world of full blocks at the listed positions, and half height slabs at the others listed
function raycast_world(full: number[][], slabs: number[][] = []) {
const has = (list: number[][], x: number, y: number, z: number) =>
list.some(([bx, by, bz]) => bx === x && by === y && bz === z);
return (x: number, y: number, z: number): Shape | undefined =>
has(full, x, y, z) ? FULL_SHAPE : has(slabs, x, y, z) ? [[0, 0, 0, 1, 0.5, 1]] : undefined;
}
Deno.test("raycast_blocks finds the first block along the ray, and the side it went in by", () => {
const world = raycast_world([[5, 0, 0], [8, 0, 0]]);
const east = raycast_blocks([0.5, 0.5, 0.5], [1, 0, 0], 20, world)!;
assertEquals([east.x, east.y, east.z, east.face], [5, 0, 0, "west"]);
assertAlmostEquals(east.distance, 4.5);
const west = raycast_blocks([10.5, 0.5, 0.5], [-1, 0, 0], 20, world)!;
assertEquals([west.x, west.face], [8, "east"]);
assertAlmostEquals(west.distance, 1.5);
assertEquals(raycast_blocks([0.5, 0.5, 0.5], [1, 0, 0], 4, world), undefined);
assertEquals(raycast_blocks([0.5, 0.5, 0.5], [0, 0, 1], 20, world), undefined);
});
Deno.test("raycast_blocks goes diagonally and down onto the top of a block", () => {
// it reaches y 0 at x and z 2.7, on the block's top
const world = raycast_world([[2, -1, 2]]);
const d = 1 / Math.sqrt(3);
const hit = raycast_blocks([0.2, 2.5, 0.2], [d, -d, d], 20, world)!;
assertEquals([hit.x, hit.y, hit.z, hit.face], [2, -1, 2, "top"]);
assertAlmostEquals(hit.distance, 2.5 * Math.sqrt(3));
});
Deno.test("raycast_blocks goes over the empty half of a slab, and not out of a block it starts in", () => {
const world = raycast_world([[0, 0, 0], [4, 0, 0]], [[2, 0, 0]]);
const over = raycast_blocks([0.5, 0.75, 0.5], [1, 0, 0], 20, world)!;
assertEquals([over.x, over.face], [4, "west"]);
const into = raycast_blocks([0.5, 0.25, 0.5], [1, 0, 0], 20, world)!;
assertEquals([into.x, into.face], [2, "west"]);
});
+57 -23
View File
@@ -1,4 +1,4 @@
// fishing: cast into water, wait for a bite, reel it in before it gets away
// 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";
@@ -9,51 +9,71 @@ import { test_game } from "./helpers.ts";
type Msg = any;
const ROD = "bworld:fishing_rod";
// below where the hook floats, even at the bottom of its bobbing, see the bworld mod's fishing
// 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 with water next to them and a rod in hand
// 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 = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone");
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.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 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");
const hooks = () => t.game.entities.all().filter((e) => e.type === "bworld:fishing_hook");
// 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 < 101 - HOOK_UNDER) return true;
if (hooks()[0]?.y < 100 - HOOK_UNDER) return true;
}
return false;
};
return { ...t, slot, rod, cast, reel, fish, wait_for_bite, hooks };
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();
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));
const floating = t.hooks()[0].y;
assert(t.wait_for_bite());
assert(hook.y < floating - 0.1, String(hook.y));
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);
@@ -61,6 +81,14 @@ Deno.test("cast into water, reel in a fish once something bites", async () => {
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();
@@ -69,12 +97,17 @@ Deno.test("reeling in before a bite catches nothing", async () => {
assertEquals(t.rod()?.data, undefined);
});
Deno.test("right clicking air with an uncast rod does nothing", async () => {
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.rod()?.data, undefined);
for (let i = 0; i < 700; i++) t.game.tick();
assertEquals(t.fish().length, 0);
assertEquals(t.rod()?.data, undefined);
});
Deno.test("switching away from the rod takes the line out", async () => {
@@ -92,7 +125,8 @@ Deno.test("switching away from the rod takes the line out", async () => {
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");
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();