Start work on fishing minigames,,

This commit is contained in:
2026-10-11 14:10:14 -03:00
parent 592659e57b
commit 21442fb822
7 changed files with 252 additions and 239 deletions
+7 -7
View File
@@ -73,14 +73,14 @@ Biome ids are what features' `biome_at` gives and what spawns list. The base gam
## Biome Tags
Biomes have [tags](../resources/tags.md) too, in `tags/biomes/`. The base game's are like Minecraft's:
Biomes have [tags](../resources/tags.md) too, in `tags/biomes/`. Every base game biome is in one of these four:
| Tag | Biomes |
| --------------------- | ----------------------------------------------------------------------------------------------- |
| `#bworld:is_ocean` | `ocean`, `deep_ocean`, `frozen_ocean`, `deep_frozen_ocean`, `warm_ocean`, `deep_lukewarm_ocean` |
| `#bworld:is_river` | `river`, `frozen_river` |
| `#bworld:is_mountain` | `meadow`, `frozen_peaks`, `jagged_peaks`, `stony_peaks`, `snowy_slopes` |
| `#bworld:is_forest` | `forest`, `dark_forest` |
| Tag | Biomes |
| --------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `#bworld:is_ocean` | `ocean`, `deep_ocean`, `frozen_ocean`, `deep_frozen_ocean`, `warm_ocean`, `deep_lukewarm_ocean`, `beach`, `snowy_beach`, `stony_shore` |
| `#bworld:is_river` | `river`, `frozen_river` |
| `#bworld:is_mountain` | `meadow`, `frozen_peaks`, `jagged_peaks`, `stony_peaks`, `snowy_slopes` |
| `#bworld:is_forest` | `forest`, `dark_forest`, `savanna`, `plains`, `snowy_plains`, `taiga`, `snowy_taiga`, `jungle`, `swamp`, `desert` |
A mod's biome joins one by adding itself to the tag's file, like `tags/biomes/bworld/is_forest.json`, and a script
checks a biome with `ctx.tags.has("biomes", "bworld:is_forest", ctx.world.biome_at(x, z))`.
+219
View File
@@ -0,0 +1,219 @@
// fishing, like minecraft's: right click with a rod to throw its hook forward. when it lands in a block with
// bworld:fishable, like water, it floats there until something bites, and right clicking again before it gets away
// reels it in. a hook that lands on anything else lies there until it's reeled in, with nothing on it
import type { EntityRef, Player, Season, ServerContext } from "bworld/server";
import type { Position } from "bworld/shared";
const FISHING_ROD = "bworld:fishing_rod";
const FISHING_HOOK = "bworld:fishing_hook";
const FISHABLE = "bworld:fishable";
const FISH = "bworld:fish";
// built from every item with bworld:fish, see register_fishing
const FISHING_TABLE = "bworld:gameplay/fishing";
// further than this from the hook, the line snaps
const LINE_LENGTH = 16;
// blocks per tick it's thrown at, and how it falls and slows down in the air each tick, like minecraft's hook
const THROW_SPEED = 1;
const GRAVITY = 0.03;
const DRAG = 0.92;
// it's reeled in if it's still flying after this many ticks
const MAX_FLIGHT = 100;
// half the hook's 0.25 block box: the hook's middle flies along the line, its feet are this much under it
const HOOK_HALF = 0.125;
// the hook's feet under the water's surface, so its red stripe floats right on it (the hook's sprite is 16 pixels
// around the middle of its 0.25 block box, the stripe is 1.5 pixels above that)
const HOOK_DEPTH = 0.125 + 1.5 / 16;
// how far it bobs up and down, and how far under it goes with a bite
const HOOK_BOB = 0.03;
const HOOK_DIP = 0.15;
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];
// what's caught, a loot table. bworld:season, bworld:time and bworld:biome pick fish by where and when it's reeled in
loot_table: string;
}
// an item that's caught by fishing
interface FishParams {
// how often it's caught against the other fish, 1 when not given
weight?: number;
// the seasons it's caught in, every one when not given
seasons?: Season[];
// the hours it's caught at, like bworld:time's, all day when not given
time?: { from: number; to: number };
// the biomes and #biome tags it's caught in, every one when not given
biomes?: string[];
}
interface Cast {
// the hotbar slot the rod is in, switching away reels it in
slot: number;
params: FishingRodParams;
hook: EntityRef;
// the hook's middle
position: Position;
// blocks per tick while it's flying, undefined once it has landed
velocity?: Position;
thrown_at: number;
// the fishable block it floats on, once it lands in one. the line comes out when it's gone
water?: { x: number; y: number; z: number; block: string };
// ticks, when something bites and when it gets away
bite_at?: number;
escape_at?: number;
}
export function register_fishing(ctx: ServerContext) {
// by player id
const casts = new Map<string, Cast>();
const between = ([min, max]: [number, number]) => min + Math.floor(Math.random() * (max - min + 1));
const fishable = new Map<string, boolean>();
const is_fishable = (id: string) => {
let result = fishable.get(id);
if (result === undefined) fishable.set(id, result = FISHABLE in ctx.blocks.components(id));
return result;
};
// takes the line in, the rod in the slot looks uncast again
const stop = (player: Player | undefined, id: string) => {
const cast = casts.get(id);
casts.delete(id);
cast?.hook.remove();
const rod = cast && player?.inventory.get(cast.slot);
if (rod?.id === FISHING_ROD) rod.data = undefined;
};
// moves the hook along its flight for a tick, until it hits something
const fly = (cast: Cast, velocity: Position, now: number) => {
const from = cast.position;
const to = { x: from.x + velocity.x, y: from.y + velocity.y, z: from.z + velocity.z };
const hit = ctx.world.raycast(from, to, { liquids: "any" });
if (!hit) {
cast.position = to;
velocity.x *= DRAG;
velocity.y = velocity.y * DRAG - GRAVITY;
velocity.z *= DRAG;
cast.hook.teleport(to.x, to.y - HOOK_HALF, to.z);
return;
}
cast.velocity = undefined;
cast.position = { ...hit.position };
if (is_fishable(hit.id)) {
// it floats on top, even when it went in by a side under the surface
let y = hit.y;
while (ctx.world.get_block(hit.x, y + 1, hit.z) === hit.id) y++;
cast.water = { x: hit.x, y, z: hit.z, block: hit.id };
cast.position.y = y + 1;
cast.bite_at = now + between(cast.params.wait);
} else {
cast.hook.teleport(hit.position.x, hit.position.y - HOOK_HALF, hit.position.z);
}
};
// fishable blocks are found by their components, see is_fishable
ctx.components.register_block(FISHABLE, {});
// "seasons": ["summer", "autumn"], holds in any of them, at the loot's position. without a position, like a
// table rolled from a command, it holds
ctx.loot.register_condition("bworld:season", (params, context) => {
const seasons = params.seasons as string[];
if (!context.position) return true;
return seasons.includes(ctx.world.season_at(context.position.x, context.position.z));
});
// "biomes": ["bworld:swamp", "#bworld:is_ocean"], holds in any of the biomes or biome tags at the loot's
// position. without a position it holds, like bworld:season
ctx.loot.register_condition("bworld:biome", (params, context) => {
if (!context.position) return true;
const biome = ctx.world.biome_at(context.position.x, context.position.z);
return (params.biomes as string[]).some((entry) =>
entry.startsWith("#") ? ctx.tags.has("biomes", entry, biome) : entry === biome
);
});
// "from": 20, "to": 4, hours on a 24 hour clock, from inclusive and to exclusive. from past to wraps around
// midnight, and fractions are minutes, like 6.5 for 06:30
ctx.loot.register_condition("bworld:time", (params) => {
const from = params.from as number;
const to = params.to as number;
const hour = ctx.world.hour;
return from <= to ? hour >= from && hour < to : hour >= from || hour < to;
});
// every item with bworld:fish is in the fishing table, caught in its seasons, at its hours and in its biomes
ctx.components.register_item<FishParams>(FISH, {});
const entries = ctx.items.ids().flatMap((id) => {
const fish = ctx.items.components(id)[FISH] as FishParams | undefined;
if (!fish) return [];
const conditions = [];
if (fish.seasons) conditions.push({ condition: "bworld:season", seasons: fish.seasons });
if (fish.time) conditions.push({ condition: "bworld:time", ...fish.time });
if (fish.biomes) conditions.push({ condition: "bworld:biome", biomes: fish.biomes });
return [{ item: id, weight: fish.weight ?? 1, conditions }];
});
ctx.loot.register_table(FISHING_TABLE, { pools: entries.length > 0 ? [{ entries }] : [] });
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 loot = ctx.loot.roll(params.loot_table, { position: cast.position, player, tool: rod });
for (const item of loot) player.give_item(item.id, item.count, item.data);
}
stop(player, player.id);
return;
}
const eye = player.eye_position;
const look = player.look_direction;
const position = { x: eye.x + look.x * 0.5, y: eye.y + look.y * 0.5, z: eye.z + look.z * 0.5 };
casts.set(player.id, {
slot: player.selected_slot,
params,
hook: ctx.world.spawn_entity(FISHING_HOOK, position.x, position.y - HOOK_HALF, position.z),
position,
velocity: { x: look.x * THROW_SPEED, y: look.y * THROW_SPEED, z: look.z * THROW_SPEED },
thrown_at: ctx.system.current_tick,
});
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.position;
const distance = Math.hypot(x - cast.position.x, y - cast.position.y, z - cast.position.z);
const water = cast.water;
if (
!player || player.selected_slot !== cast.slot || player.held_item?.id !== FISHING_ROD ||
distance > LINE_LENGTH || cast.hook.removed ||
(water && ctx.world.get_block(water.x, water.y, water.z) !== water.block) ||
(cast.velocity && (now - cast.thrown_at > MAX_FLIGHT || cast.position.y < 0))
) {
stop(player, id);
continue;
}
if (cast.velocity) {
fly(cast, cast.velocity, now);
}
if (!cast.water || cast.bite_at === undefined) {
continue;
}
if (cast.escape_at === undefined && now >= cast.bite_at) {
cast.escape_at = now + between(cast.params.bite);
} 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);
}
const bob = cast.escape_at === undefined ? Math.sin(now / 40 * Math.PI * 2) * HOOK_BOB : -HOOK_DIP;
cast.hook.teleport(cast.position.x, cast.position.y - HOOK_DEPTH + bob, cast.position.z);
}
});
}
+2 -220
View File
@@ -1,8 +1,8 @@
// the base game's block and item behavior, written against the same api as any other mod
import type { BlockRef, Container, EntityRef, Player, Season, ServerContext } from "bworld/server";
import type { Position } from "bworld/shared";
import type { BlockRef, Container, Player, Season, ServerContext } from "bworld/server";
import { register_curios } from "./curios.ts";
import { register_effects } from "./effects.ts";
import { register_fishing } from "./fishing.ts";
export function setup(ctx: ServerContext) {
register_hoeable(ctx);
@@ -612,224 +612,6 @@ function register_buckets(ctx: ServerContext) {
});
}
// fishing, like minecraft's: right click with a rod to throw its hook forward. when it lands in a block with
// bworld:fishable, like water, it floats there until something bites, and right clicking again before it gets away
// reels it in. a hook that lands on anything else lies there until it's reeled in, with nothing on it
const FISHING_ROD = "bworld:fishing_rod";
const FISHING_HOOK = "bworld:fishing_hook";
const FISHABLE = "bworld:fishable";
const FISH = "bworld:fish";
// built from every item with bworld:fish, see register_fishing
const FISHING_TABLE = "bworld:gameplay/fishing";
// further than this from the hook, the line snaps
const LINE_LENGTH = 16;
// blocks per tick it's thrown at, and how it falls and slows down in the air each tick, like minecraft's hook
const THROW_SPEED = 1;
const GRAVITY = 0.03;
const DRAG = 0.92;
// it's reeled in if it's still flying after this many ticks
const MAX_FLIGHT = 100;
// half the hook's 0.25 block box: the hook's middle flies along the line, its feet are this much under it
const HOOK_HALF = 0.125;
// the hook's feet under the water's surface, so its red stripe floats right on it (the hook's sprite is 16 pixels
// around the middle of its 0.25 block box, the stripe is 1.5 pixels above that)
const HOOK_DEPTH = 0.125 + 1.5 / 16;
// how far it bobs up and down, and how far under it goes with a bite
const HOOK_BOB = 0.03;
const HOOK_DIP = 0.15;
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];
// what's caught, a loot table. bworld:season, bworld:time and bworld:biome pick fish by where and when it's reeled in
loot_table: string;
}
// an item that's caught by fishing
interface FishParams {
// how often it's caught against the other fish, 1 when not given
weight?: number;
// the seasons it's caught in, every one when not given
seasons?: Season[];
// the hours it's caught at, like bworld:time's, all day when not given
time?: { from: number; to: number };
// the biomes and #biome tags it's caught in, every one when not given
biomes?: string[];
}
interface Cast {
// the hotbar slot the rod is in, switching away reels it in
slot: number;
params: FishingRodParams;
hook: EntityRef;
// the hook's middle
position: Position;
// blocks per tick while it's flying, undefined once it has landed
velocity?: Position;
thrown_at: number;
// the fishable block it floats on, once it lands in one. the line comes out when it's gone
water?: { x: number; y: number; z: number; block: string };
// 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>();
const between = ([min, max]: [number, number]) => min + Math.floor(Math.random() * (max - min + 1));
const fishable = new Map<string, boolean>();
const is_fishable = (id: string) => {
let result = fishable.get(id);
if (result === undefined) fishable.set(id, result = FISHABLE in ctx.blocks.components(id));
return result;
};
// takes the line in, the rod in the slot looks uncast again
const stop = (player: Player | undefined, id: string) => {
const cast = casts.get(id);
casts.delete(id);
cast?.hook.remove();
const rod = cast && player?.inventory.get(cast.slot);
if (rod?.id === FISHING_ROD) rod.data = undefined;
};
// moves the hook along its flight for a tick, until it hits something
const fly = (cast: Cast, velocity: Position, now: number) => {
const from = cast.position;
const to = { x: from.x + velocity.x, y: from.y + velocity.y, z: from.z + velocity.z };
const hit = ctx.world.raycast(from, to, { liquids: "any" });
if (!hit) {
cast.position = to;
velocity.x *= DRAG;
velocity.y = velocity.y * DRAG - GRAVITY;
velocity.z *= DRAG;
cast.hook.teleport(to.x, to.y - HOOK_HALF, to.z);
return;
}
cast.velocity = undefined;
cast.position = { ...hit.position };
if (is_fishable(hit.id)) {
// it floats on top, even when it went in by a side under the surface
let y = hit.y;
while (ctx.world.get_block(hit.x, y + 1, hit.z) === hit.id) y++;
cast.water = { x: hit.x, y, z: hit.z, block: hit.id };
cast.position.y = y + 1;
cast.bite_at = now + between(cast.params.wait);
} else {
cast.hook.teleport(hit.position.x, hit.position.y - HOOK_HALF, hit.position.z);
}
};
// fishable blocks are found by their components, see is_fishable
ctx.components.register_block(FISHABLE, {});
// "seasons": ["summer", "autumn"], holds in any of them, at the loot's position. without a position, like a
// table rolled from a command, it holds
ctx.loot.register_condition("bworld:season", (params, context) => {
const seasons = params.seasons as string[];
if (!context.position) return true;
return seasons.includes(ctx.world.season_at(context.position.x, context.position.z));
});
// "biomes": ["bworld:swamp", "#bworld:is_ocean"], holds in any of the biomes or biome tags at the loot's
// position. without a position it holds, like bworld:season
ctx.loot.register_condition("bworld:biome", (params, context) => {
if (!context.position) return true;
const biome = ctx.world.biome_at(context.position.x, context.position.z);
return (params.biomes as string[]).some((entry) =>
entry.startsWith("#") ? ctx.tags.has("biomes", entry, biome) : entry === biome
);
});
// "from": 20, "to": 4, hours on a 24 hour clock, from inclusive and to exclusive. from past to wraps around
// midnight, and fractions are minutes, like 6.5 for 06:30
ctx.loot.register_condition("bworld:time", (params) => {
const from = params.from as number;
const to = params.to as number;
const hour = ctx.world.hour;
return from <= to ? hour >= from && hour < to : hour >= from || hour < to;
});
// every item with bworld:fish is in the fishing table, caught in its seasons, at its hours and in its biomes
ctx.components.register_item<FishParams>(FISH, {});
const entries = ctx.items.ids().flatMap((id) => {
const fish = ctx.items.components(id)[FISH] as FishParams | undefined;
if (!fish) return [];
const conditions = [];
if (fish.seasons) conditions.push({ condition: "bworld:season", seasons: fish.seasons });
if (fish.time) conditions.push({ condition: "bworld:time", ...fish.time });
if (fish.biomes) conditions.push({ condition: "bworld:biome", biomes: fish.biomes });
return [{ item: id, weight: fish.weight ?? 1, conditions }];
});
ctx.loot.register_table(FISHING_TABLE, { pools: entries.length > 0 ? [{ entries }] : [] });
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 loot = ctx.loot.roll(params.loot_table, { position: cast.position, player, tool: rod });
for (const item of loot) player.give_item(item.id, item.count, item.data);
}
stop(player, player.id);
return;
}
const eye = player.eye_position;
const look = player.look_direction;
const position = { x: eye.x + look.x * 0.5, y: eye.y + look.y * 0.5, z: eye.z + look.z * 0.5 };
casts.set(player.id, {
slot: player.selected_slot,
params,
hook: ctx.world.spawn_entity(FISHING_HOOK, position.x, position.y - HOOK_HALF, position.z),
position,
velocity: { x: look.x * THROW_SPEED, y: look.y * THROW_SPEED, z: look.z * THROW_SPEED },
thrown_at: ctx.system.current_tick,
});
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.position;
const distance = Math.hypot(x - cast.position.x, y - cast.position.y, z - cast.position.z);
const water = cast.water;
if (
!player || player.selected_slot !== cast.slot || player.held_item?.id !== FISHING_ROD ||
distance > LINE_LENGTH || cast.hook.removed ||
(water && ctx.world.get_block(water.x, water.y, water.z) !== water.block) ||
(cast.velocity && (now - cast.thrown_at > MAX_FLIGHT || cast.position.y < 0))
) {
stop(player, id);
continue;
}
if (cast.velocity) {
fly(cast, cast.velocity, now);
}
if (!cast.water || cast.bite_at === undefined) {
continue;
}
if (cast.escape_at === undefined && now >= cast.bite_at) {
cast.escape_at = now + between(cast.params.bite);
} 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);
}
const bob = cast.escape_at === undefined ? Math.sin(now / 40 * Math.PI * 2) * HOOK_BOB : -HOOK_DIP;
cast.hook.teleport(cast.position.x, cast.position.y - HOOK_DEPTH + bob, cast.position.z);
}
});
}
interface GrowableBlockParams {
// what grows on top of it
block_id: string;
@@ -3,7 +3,15 @@
"tag": {
"values": [
"bworld:forest",
"bworld:dark_forest"
"bworld:dark_forest",
"bworld:savanna",
"bworld:plains",
"bworld:snowy_plains",
"bworld:taiga",
"bworld:snowy_taiga",
"bworld:jungle",
"bworld:swamp",
"bworld:desert"
]
}
}
+4 -1
View File
@@ -7,7 +7,10 @@
"bworld:frozen_ocean",
"bworld:deep_frozen_ocean",
"bworld:warm_ocean",
"bworld:deep_lukewarm_ocean"
"bworld:deep_lukewarm_ocean",
"bworld:beach",
"bworld:snowy_beach",
"bworld:stony_shore"
]
}
}
+10 -9
View File
@@ -43,7 +43,11 @@ async function setup() {
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");
// what they caught: items with bworld:fish
const fish = () =>
inventory().filter((i: Msg) =>
i && EverythingRegistry.get<ItemRegistry>("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++) {
@@ -166,13 +170,10 @@ Deno.test("a fish with biomes is only caught in them, by id or #tag", async () =
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_forest, cod has none
assertEquals(
caught(find((b) => tags.has("biomes", "bworld:is_ocean", b))),
new Set(["bworld:cod", "bworld:salmon"]),
);
assertEquals(
caught(find((b) => !tags.has("biomes", "bworld:is_ocean", b) && !tags.has("biomes", "bworld:is_forest", b))),
new Set(["bworld:cod"]),
// 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"),
);
});
+1 -1
View File
@@ -90,7 +90,7 @@ Deno.test("the base game's tags, from scripts", async () => {
assert(tags.has("biomes", "#bworld:is_ocean", "bworld:deep_frozen_ocean"));
assert(tags.has("biomes", "bworld:is_mountain", "bworld:jagged_peaks"));
assert(tags.has("biomes", "bworld:is_forest", "bworld:dark_forest"));
assert(!tags.has("biomes", "bworld:is_forest", "bworld:plains"));
assert(!tags.has("biomes", "bworld:is_forest", "bworld:river"));
assertEquals(tags.get("items", "c:ingots"), [
"bworld:copper_ingot",
"bworld:tin_ingot",