Fishing !

This commit is contained in:
2026-09-28 13:33:40 -03:00
parent 2488c6f8dc
commit 84a622c3f1
27 changed files with 367 additions and 21 deletions
+1
View File
@@ -418,6 +418,7 @@ clicked on somewhere it can go and eaten otherwise.
| `id` | required | registry key | The item's id. |
| `name` | from the id | `name` | What players see, like `"Copper Pickaxe"`. Made from the id when not set. |
| `texture` | required | `texture_id` | Texture id. |
| `data_textures` | none | `data_textures` | `{ "cast": "bworld:fishing_rod_cast" }`: a stack whose `data` has that key set is drawn with that texture instead, like a cast fishing rod. The first key that's set wins. |
| `tool` | none | `tool_type` | Tool type used by blocks' `mining.tool`. |
| `places` | none | `block_id` | Block placed when used on a block face. |
| `max_stack` | `64` | new | Largest stack size. |
+1 -8
View File
@@ -7,6 +7,7 @@ import type { ItemStack } from "$/common/inventory.ts";
import { push_vertex, Texture } from "$/client/renderer/mod.ts";
import { block_item_texture, cube_face_texture, cube_face_tint } from "$/common/block_models.ts";
import { item_model } from "./item_model.ts";
import { item_texture } from "$/common/utils.ts";
// how big items on the ground are drawn, minecraft's ground transform
const BLOCK_SCALE = 0.25;
@@ -113,11 +114,3 @@ function push_textured_quad(atlas: Texture, region: { x: number; y: number }, co
push_vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, color[0], color[1], color[2], 1);
}
}
function item_texture(item: ItemStack) {
const texture_id = EverythingRegistry.get<ItemRegistry>("items", item.type_id)?.texture_id;
if (typeof texture_id === "function") {
return texture_id(item);
}
return texture_id ?? "engine:missing";
}
+2 -6
View File
@@ -3,6 +3,7 @@ import { get_sprite_region } from "$/client/sprites.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { AssetManager } from "$/client/assets.ts";
import { ItemStack } from "$/common/inventory.ts";
import { item_texture } from "$/common/utils.ts";
import { block_item_texture, cube_face_texture, cube_face_tint } from "$/common/block_models.ts";
import { draw_text, draw_texture_region, draw_texture_region_skewed, Texture } from "$/client/renderer/mod.ts";
@@ -93,12 +94,7 @@ export function draw_item(item: ItemStack, x: number, y: number) {
// ey its not a bad name
function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y: number) {
let texture_id = "engine:missing";
if (typeof item_info?.texture_id === "string") {
texture_id = item_info.texture_id;
} else if (typeof item_info?.texture_id === "function") {
texture_id = item_info.texture_id(item);
}
const texture_id = item_texture(item, item_info);
const sprite_region = get_sprite_region(texture_id);
draw_texture_region(
AssetManager.instance.get("bworld:textures"),
+2
View File
@@ -158,6 +158,8 @@ export interface FoodProperties {
export interface ItemRegistry<T = unknown | undefined> {
texture_id: string | ((item: ItemStack<T>) => string);
// shown in place of texture_id while the stack's data has that key set, like a cast fishing rod. the first wins
data_textures?: Record<string, string>;
// what players see, like "Iron Ingot". made from the id when not set, see display_name in common/utils.ts
name?: string;
block_id?: string;
+1
View File
@@ -129,6 +129,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
errors.push(`${file}: item ${json.id} is also defined by ${item_owners.get(json.id)!.join(", ")}`);
}
uses(file, json.texture, "texture");
for (const texture of Object.values(json.data_textures ?? {})) uses(file, texture, "texture");
if (json.places) uses(file, json.places, "block");
if (json.food?.returns) uses(file, json.food.returns, "item");
}
+12
View File
@@ -69,6 +69,8 @@ export interface OreJson {
export interface ItemJson {
id: string;
texture: string;
// data key -> texture, see ItemRegistry.data_textures
data_textures?: Record<string, string>;
tool?: string;
places?: string;
max_stack?: number;
@@ -192,6 +194,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson {
throw new Error(`${id} picks its texture with a function, which json can't hold`);
}
const json: ItemJson = { id, texture: item.texture_id };
if (item.data_textures !== undefined) json.data_textures = item.data_textures;
if (item.tool_type !== undefined) json.tool = item.tool_type;
if (item.block_id !== undefined) json.places = item.block_id;
if (item.max_stack !== undefined) json.max_stack = item.max_stack;
@@ -204,6 +207,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson {
export function item_from_json(json: ItemJson): ItemRegistry {
const item: ItemRegistry = { texture_id: json.texture };
if (json.data_textures !== undefined) item.data_textures = json.data_textures;
if (json.tool !== undefined) item.tool_type = json.tool;
if (json.places !== undefined) item.block_id = json.places;
if (json.max_stack !== undefined) item.max_stack = json.max_stack;
@@ -477,6 +481,14 @@ export function validate_item(json: unknown): Problems {
if (!is_object(json)) return ["item must be an object"];
const problems = validate_id(json.id, "id");
problems.push(...validate_id(json.texture, "texture"));
if (json.data_textures !== undefined) {
if (!is_object(json.data_textures)) problems.push("data_textures must be { data key: texture id }");
else {
for (const [key, id] of Object.entries(json.data_textures)) {
problems.push(...validate_id(id, `data_textures.${key}`));
}
}
}
if (json.places !== undefined) problems.push(...validate_id(json.places, "places"));
problems.push(...validate_text(json.name, "name"), ...validate_text(json.lore, "lore"));
if (json.max_stack !== undefined && (!Number.isInteger(json.max_stack) || (json.max_stack as number) < 1)) {
+14
View File
@@ -1,6 +1,7 @@
import { ID_MASK, STATE_SHIFT } from "./constants.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "./everything_registry.ts";
import { block_item_texture, cube_face_texture } from "./block_models.ts";
import type { ItemStack } from "./inventory.ts";
export function point_inside_rec(
point_x: number,
@@ -46,6 +47,19 @@ export function register_block_item(block: BlockRegistry) {
EverythingRegistry.register<ItemRegistry>("items", block.id, item);
}
// the texture a stack is drawn with: one of its data_textures when its data has that key set, or its texture
export function item_texture(
item: ItemStack,
info = EverythingRegistry.get<ItemRegistry>("items", item.type_id),
): string {
if (!info) return "engine:missing";
const data = item.data as Record<string, unknown> | undefined;
for (const [key, texture] of Object.entries(info.data_textures ?? {})) {
if (data?.[key]) return texture;
}
return typeof info.texture_id === "function" ? info.texture_id(item) : info.texture_id;
}
// what players see an item called: its name, or one made from its id, so bworld:iron_ingot is "Iron Ingot"
export function display_name(item_id: string): string {
const name = EverythingRegistry.get<ItemRegistry>("items", item_id)?.name;
+2 -1
View File
@@ -10,7 +10,8 @@
"item": false,
"replaceable": true,
"components": {
"bworld:water_source": {}
"bworld:water_source": {},
"bworld:fishable": {}
}
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"format_version": 1,
"item": {
"id": "bworld:cod",
"texture": "bworld:cod",
"food": {
"nutrition": 2,
"saturation_modifier": 0.1
}
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_cod",
"texture": "bworld:cooked_cod",
"food": {
"nutrition": 5,
"saturation_modifier": 0.6
}
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"format_version": 1,
"item": {
"id": "bworld:cooked_salmon",
"texture": "bworld:cooked_salmon",
"food": {
"nutrition": 6,
"saturation_modifier": 0.8
}
}
}
+22
View File
@@ -0,0 +1,22 @@
{
"format_version": 1,
"item": {
"id": "bworld:fishing_rod",
"texture": "bworld:fishing_rod",
"data_textures": {
"cast": "bworld:fishing_rod_cast"
},
"max_stack": 1,
"lore": "Right click water to cast, and again when something bites.",
"components": {
"bworld:fishing_rod": {
"wait": [100, 600],
"bite": [20, 40],
"loot": [
{ "item": "bworld:cod", "weight": 60 },
{ "item": "bworld:salmon", "weight": 25 }
]
}
}
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"format_version": 1,
"item": {
"id": "bworld:salmon",
"texture": "bworld:salmon",
"food": {
"nutrition": 2,
"saturation_modifier": 0.1
}
}
}
@@ -0,0 +1,19 @@
{
"format_version": 1,
"recipe": {
"type": "shaped",
"pattern": [
" S",
" SW",
"S W"
],
"key": {
"S": "bworld:stick",
"W": "bworld:wheat"
},
"result": {
"id": "bworld:fishing_rod",
"count": 1
}
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"recipe": {
"type": "furnace",
"input": "bworld:cod",
"output": {
"id": "bworld:cooked_cod",
"count": 1
},
"cook_time": 200
}
}
+12
View File
@@ -0,0 +1,12 @@
{
"format_version": 1,
"recipe": {
"type": "furnace",
"input": "bworld:salmon",
"output": {
"id": "bworld:cooked_salmon",
"count": 1
},
"cook_time": 200
}
}
+123
View File
@@ -7,6 +7,7 @@ export function setup(ctx: ServerContext) {
register_furnace(ctx);
register_smithing_table(ctx);
register_farming(ctx);
register_fishing(ctx);
}
// right clicking with a type of tool turns the block into another, like any hoe on grass
@@ -506,3 +507,125 @@ function register_farming(ctx: ServerContext) {
},
});
}
// fishing, like minecraft's without the bobber: right click water with a rod to cast, wait for a bite, and right click
// again before it gets away to reel it in. there's no hook entity yet, so a chat message says when something bites
const FISHING_ROD = "bworld:fishing_rod";
// further than this from where they cast, the line snaps
const LINE_LENGTH = 16;
interface FishingRodParams {
// ticks until something bites, the shortest and longest, like minecraft's 5 to 30 seconds
wait: [number, number];
// ticks it stays on the hook before it gets away
bite: [number, number];
loot: { item: string; weight: number }[];
}
interface Cast {
x: number;
y: number;
z: number;
// the block they cast into, the line comes out when it's gone
block: string;
// the hotbar slot the rod is in, switching away reels it in
slot: number;
params: FishingRodParams;
// ticks, when something bites and when it gets away
bite_at: number;
escape_at?: number;
}
function register_fishing(ctx: ServerContext) {
// by player id
const casts = new Map<string, Cast>();
// the water each player right clicked this tick, for their rod's on_use, which doesn't get the block
const aimed = new Map<string, { x: number; y: number; z: number; block: string; tick: number }>();
const between = ([min, max]: [number, number]) => min + Math.floor(Math.random() * (max - min + 1));
const pick = (loot: FishingRodParams["loot"]) => {
const total = loot.reduce((sum, entry) => sum + entry.weight, 0);
let roll = Math.random() * total;
for (const entry of loot) {
roll -= entry.weight;
if (roll < 0) return entry.item;
}
return loot.at(-1)?.item;
};
// takes the line out of the water, the rod in the slot looks uncast again
const stop = (player: Player | undefined, id: string) => {
const cast = casts.get(id);
casts.delete(id);
const rod = cast && player?.inventory.get(cast.slot);
if (rod?.id === FISHING_ROD) rod.data = undefined;
};
ctx.components.register_block("bworld:fishable", {
on_interact(block, _params, player) {
aimed.set(player.id, {
x: block.x,
y: block.y,
z: block.z,
block: block.id,
tick: ctx.system.current_tick,
});
return false;
},
});
ctx.components.register_item<FishingRodParams>(FISHING_ROD, {
on_use(rod, params, player) {
const cast = casts.get(player.id);
if (cast) {
if (cast.escape_at !== undefined) {
const item = pick(params.loot);
if (item) player.give_item(item);
}
stop(player, player.id);
return;
}
const water = aimed.get(player.id);
aimed.delete(player.id);
if (water?.tick !== ctx.system.current_tick) {
// cast before the server restarted
rod.data = undefined;
return;
}
casts.set(player.id, {
x: water.x,
y: water.y,
z: water.z,
block: water.block,
slot: player.selected_slot,
params,
bite_at: ctx.system.current_tick + between(params.wait),
});
rod.data = { cast: true };
},
});
ctx.events.after.tick.subscribe(() => {
const now = ctx.system.current_tick;
for (const [id, cast] of casts) {
const player = ctx.players.get(id);
const { x, y, z } = player?.position ?? cast;
const distance = Math.hypot(x - cast.x, y - cast.y, z - cast.z);
if (
!player || player.selected_slot !== cast.slot || player.held_item?.id !== FISHING_ROD ||
distance > LINE_LENGTH || ctx.world.get_block(cast.x, cast.y, cast.z) !== cast.block
) {
stop(player, id);
} else if (cast.escape_at === undefined && now >= cast.bite_at) {
cast.escape_at = now + between(cast.params.bite);
player.send_message("Something's biting! Right click to reel it in");
} else if (cast.escape_at !== undefined && now >= cast.escape_at) {
// it got away, wait for another
cast.escape_at = undefined;
cast.bite_at = now + between(cast.params.wait);
}
}
});
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 541 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 421 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 175 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 575 B

+94
View File
@@ -0,0 +1,94 @@
// 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;
};
return { ...t, slot, rod, cast, reel, fish, wait_for_bite };
}
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 });
assert(t.wait_for_bite());
t.reel();
assertEquals(t.fish().length, 1);
assertEquals(t.rod()?.data, undefined);
});
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.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);
});
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);
});
+6 -6
View File
@@ -27,12 +27,12 @@ Deno.test("the base game loads from mods/bworld", async () => {
const bworld = (entries: [string, unknown][]) => entries.filter(([id]) => id.startsWith("bworld:")).length;
// 7 kinds of log, planks and leaves
assertEquals(bworld(EverythingRegistry.entries("blocks")), 42);
// 33 items (20 of them tools) plus the item forms of the 37 blocks that have one
assertEquals(bworld(EverythingRegistry.entries("items")), 70);
// bworld's 15 (4 wood tools, 7 planks), the cooking pot and stove are farmersdelight's
assertEquals(game.recipes.shaped.length, 17);
// every log smelts and burns
assertEquals(game.recipes.furnace.size, 12);
// 38 items (20 of them tools) plus the item forms of the 37 blocks that have one
assertEquals(bworld(EverythingRegistry.entries("items")), 75);
// bworld's 16 (4 wood tools, 7 planks, the fishing rod), the cooking pot and stove are farmersdelight's
assertEquals(game.recipes.shaped.length, 18);
// every log and fish smelts, every log burns
assertEquals(game.recipes.furnace.size, 14);
assertEquals(game.recipes.fuel.size, 8);
assertEquals(EverythingRegistry.get<BlockRegistry>("blocks", "bworld:water")?.replaceable, true);
// still has its code until it's a component