diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index c9cef64..8803a8f 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -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; } diff --git a/docs/modding/misc/time.md b/docs/modding/misc/time.md index 8c33075..9a4c859 100644 --- a/docs/modding/misc/time.md +++ b/docs/modding/misc/time.md @@ -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. diff --git a/docs/modding/resources/loot_tables.md b/docs/modding/resources/loot_tables.md index 300cceb..0bd8988 100644 --- a/docs/modding/resources/loot_tables.md +++ b/docs/modding/resources/loot_tables.md @@ -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: diff --git a/mods/bworld/items/fishing_rod.json b/mods/bworld/items/fishing_rod.json index f0c10f7..e4d549b 100644 --- a/mods/bworld/items/fishing_rod.json +++ b/mods/bworld/items/fishing_rod.json @@ -17,7 +17,8 @@ "bite": [ 20, 40 - ] + ], + "loot_table": "bworld:gameplay/fishing" } } } diff --git a/mods/bworld/loot_tables/gameplay/fishing.json b/mods/bworld/loot_tables/gameplay/fishing.json new file mode 100644 index 0000000..54a5d12 --- /dev/null +++ b/mods/bworld/loot_tables/gameplay/fishing.json @@ -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"] + } + ] + } + ] + } + ] + } +} diff --git a/mods/bworld/loot_tables/gameplay/fishing_autumn.json b/mods/bworld/loot_tables/gameplay/fishing_autumn.json deleted file mode 100644 index baae394..0000000 --- a/mods/bworld/loot_tables/gameplay/fishing_autumn.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "format_version": 1, - "loot_table": { - "pools": [ - { - "entries": [ - { - "item": "bworld:salmon", - "weight": 60 - } - ] - } - ] - } -} diff --git a/mods/bworld/loot_tables/gameplay/fishing_spring.json b/mods/bworld/loot_tables/gameplay/fishing_spring.json deleted file mode 100644 index 3a3bea5..0000000 --- a/mods/bworld/loot_tables/gameplay/fishing_spring.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "format_version": 1, - "loot_table": { - "pools": [ - { - "entries": [ - { - "item": "bworld:cod", - "weight": 60 - } - ] - } - ] - } -} diff --git a/mods/bworld/loot_tables/gameplay/fishing_summer.json b/mods/bworld/loot_tables/gameplay/fishing_summer.json deleted file mode 100644 index 3f9d9c5..0000000 --- a/mods/bworld/loot_tables/gameplay/fishing_summer.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "format_version": 1, - "loot_table": { - "pools": [ - { - "entries": [ - { - "item": "bworld:cod", - "weight": 60 - }, - { - "item": "bworld:salmon", - "weight": 25 - } - ] - } - ] - } -} diff --git a/mods/bworld/loot_tables/gameplay/fishing_winter.json b/mods/bworld/loot_tables/gameplay/fishing_winter.json deleted file mode 100644 index 3f9d9c5..0000000 --- a/mods/bworld/loot_tables/gameplay/fishing_winter.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "format_version": 1, - "loot_table": { - "pools": [ - { - "entries": [ - { - "item": "bworld:cod", - "weight": 60 - }, - { - "item": "bworld:salmon", - "weight": 25 - } - ] - } - ] - } -} diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index 84cbe0f..ca10494 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -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(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); diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index e5fd858..88af2a7 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -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}`);