Proper season and time conditions for fishing

This commit is contained in:
2026-10-08 15:49:46 -03:00
parent 9c5a15542a
commit aa0d2f73d0
11 changed files with 86 additions and 76 deletions
+2
View File
@@ -233,6 +233,8 @@ export interface ServerWorld {
readonly time: number;
// days since the world started, going up at 06:00 each morning: 0 on the first day
readonly day: number;
// the time on a 24 hour clock, in hours: 14.5 is 14:30. hours go by faster at night, see common/time.ts
readonly hour: number;
set_time(time: number): void;
}
+2
View File
@@ -32,6 +32,8 @@ For work every tick, subscribe to the `tick` event. For work on a block, use a
`ctx.world.day` is the number of days since the world started, 0 on the first day, going up at 06:00. Every morning at
06:00, blocks' `on_new_day` runs, then the `new_day` event fires. That's the clock crops grow on.
`ctx.world.hour` is the time on a 24 hour clock, in hours: `14.5` is 14:30.
`ctx.world.set_time(ticks)` changes the time for everyone. Moving it forward past mornings runs each of them, up to the
last 64. Moving it back doesn't repeat mornings.
+24 -1
View File
@@ -126,11 +126,34 @@ does nothing unless they hold.
| [Blocks](../blocks/index.md) | `"loot_table"` in the block's json, rolled when it breaks, with its states and the tool. `"drops"` is the shorthand for one of an item. |
| [Mobs](../entities/index.md) | `"loot_table"` in the entity's json, rolled when it dies. `killed_by_player` holds when a player killed it. |
| [Crops](../blocks/crops.md) | Their block's `loot_table`, with `block_state` on `age` for grown or not. `harvest` is a table for picking. |
| Fishing | The fishing rod's `loot_table`, `bworld:gameplay/fishing`. |
| Fishing | The fishing rod's `loot_table`, `bworld:gameplay/fishing`, rolled when it's reeled in. See [Fishing](#fishing). |
| Scripts | `ctx.loot.roll` and `ctx.loot.fill`, for anything else, like chests in structures. |
`check-mods` checks that every item, tag and table a table names exists, and that the tables blocks and mobs name do.
## Fishing
The base game adds two conditions for picking fish by where and when they're reeled in:
| `condition` | Fields | Holds when |
| --------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------- |
| `bworld:season` | `seasons` | The [season](../worldgen/biomes.md#seasons) where it happens is one of `seasons`, like `["summer", "autumn"]`. |
| `bworld:time` | `from`, `to` | The hour on a 24 hour clock is from `from` up to `to`, like `6.5` for 06:30. When `from` is past `to` it wraps around midnight. |
A fish without a `bworld:season` is caught in every season, and one without `bworld:time` all day:
```json
{
"item": "my_mod:catfish",
"conditions": [
{ "condition": "bworld:season", "seasons": ["summer", "autumn"] },
{ "condition": "bworld:time", "from": 20, "to": 4 }
]
}
```
When every fish can be ruled out, nothing is caught, so keep one without conditions.
## From a Script
Server scripts roll tables, fill containers, and add conditions, functions and changes of their own:
+2 -1
View File
@@ -17,7 +17,8 @@
"bite": [
20,
40
]
],
"loot_table": "bworld:gameplay/fishing"
}
}
}
@@ -0,0 +1,31 @@
{
"format_version": 1,
"loot_table": {
"pools": [
{
"entries": [
{
"item": "bworld:cod",
"weight": 60,
"conditions": [
{
"condition": "bworld:season",
"seasons": ["spring", "summer", "winter"]
}
]
},
{
"item": "bworld:salmon",
"weight": 25,
"conditions": [
{
"condition": "bworld:season",
"seasons": ["summer", "autumn", "winter"]
}
]
}
]
}
]
}
}
@@ -1,15 +0,0 @@
{
"format_version": 1,
"loot_table": {
"pools": [
{
"entries": [
{
"item": "bworld:salmon",
"weight": 60
}
]
}
]
}
}
@@ -1,15 +0,0 @@
{
"format_version": 1,
"loot_table": {
"pools": [
{
"entries": [
{
"item": "bworld:cod",
"weight": 60
}
]
}
]
}
}
@@ -1,19 +0,0 @@
{
"format_version": 1,
"loot_table": {
"pools": [
{
"entries": [
{
"item": "bworld:cod",
"weight": 60
},
{
"item": "bworld:salmon",
"weight": 25
}
]
}
]
}
}
@@ -1,19 +0,0 @@
{
"format_version": 1,
"loot_table": {
"pools": [
{
"entries": [
{
"item": "bworld:cod",
"weight": 60
},
{
"item": "bworld:salmon",
"weight": 25
}
]
}
]
}
}
+20 -6
View File
@@ -628,6 +628,8 @@ interface FishingRodParams {
wait: [number, number];
// ticks it stays on the hook before it gets away
bite: [number, number];
// what's caught, a loot table. bworld:season and bworld:time pick fish by where and when it's reeled in
loot_table: string;
}
interface Cast {
@@ -699,17 +701,29 @@ function register_fishing(ctx: ServerContext) {
// 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));
});
// "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;
});
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 season = ctx.world.season_at(
cast.hook.position.x,
cast.hook.position.z,
);
const loot_table = `bworld:gameplay/fishing_${season}`;
const loot = ctx.loot.roll(loot_table, { position: cast.position, player, tool: rod });
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);
+5
View File
@@ -6,6 +6,7 @@ import { block_states, get_state_value, is_waterlogged, liquid_block, set_state_
import { LootTables } from "./loot.ts";
import { collision_shape_at, covers_face, raycast_blocks, type Shape } from "$/common/block_shapes.ts";
import { AIR, ID_MASK } from "$/common/constants.ts";
import { clock } from "$/common/time.ts";
import { BlockRules } from "$/common/block_rules.ts";
import type { SharedContext } from "$/common/mod_api/shared.ts";
import { tag_api } from "$/common/tags.ts";
@@ -723,6 +724,10 @@ export class ModRuntime {
get day() {
return game().world.day;
},
get hour() {
const { hour, minute } = clock(game().world.time);
return hour + minute / 60;
},
set_time: (time) => {
if (!Number.isInteger(time) || time < 0) {
throw new Error(`time must be a whole number of ticks, not ${time}`);