Biome condition for fishes

This commit is contained in:
2026-10-11 12:16:49 -03:00
parent fa969e0a96
commit e8387392fd
4 changed files with 57 additions and 5 deletions
+5 -2
View File
@@ -141,7 +141,8 @@ The fishing table, `bworld:gameplay/fishing`, isn't a file: the base game builds
"bworld:fish": {
"weight": 20,
"seasons": ["summer", "autumn"],
"time": { "from": 20, "to": 4 }
"time": { "from": 20, "to": 4 },
"biomes": ["bworld:swamp", "#bworld:is_river"]
}
}
```
@@ -150,15 +151,17 @@ The fishing table, `bworld:gameplay/fishing`, isn't a file: the base game builds
| --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `weight` | How often it's caught against the other fish, 1 when not given. |
| `seasons` | The [seasons](../worldgen/biomes.md#seasons) it's caught in where the hook is, every one when not given. |
| `biomes` | The [biomes](../worldgen/biomes.md#biome-ids) and [biome tags](../worldgen/biomes.md#biome-tags) it's caught in where the hook is, every one when not given. |
| `time` | The hours it's caught at on a 24 hour clock, from `from` up to `to`, like `6.5` for 06:30. When `from` is past `to` it wraps around midnight. All day when not given. |
When every fish can be ruled out, nothing is caught, so keep one that's always caught.
Those become the `bworld:season` and `bworld:time` conditions, which any table can use too:
Those become the `bworld:season`, `bworld:biome` and `bworld:time` conditions, which any table can use too:
| `condition` | Fields | Holds when |
| --------------- | ------------ | ----------------------------------------------------------------------------- |
| `bworld:season` | `seasons` | The season where it happens is one of `seasons`, like `["summer", "autumn"]`. |
| `bworld:biome` | `biomes` | The biome where it happens is one of `biomes`, or in one of its `#tags`. |
| `bworld:time` | `from`, `to` | The hour is from `from` up to `to`, the same as the fish's `time`. |
## From a Script
+2 -1
View File
@@ -10,7 +10,8 @@
"components": {
"bworld:fish": {
"weight": 25,
"seasons": ["summer", "autumn", "winter"]
"seasons": ["summer", "autumn", "winter"],
"biomes": ["#bworld:is_ocean", "#bworld:is_river"]
}
},
"base_sell_value": 32
+15 -2
View File
@@ -644,7 +644,7 @@ 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
// 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;
}
@@ -656,6 +656,8 @@ interface FishParams {
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 {
@@ -735,6 +737,16 @@ function register_fishing(ctx: ServerContext) {
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) => {
@@ -744,7 +756,7 @@ function register_fishing(ctx: ServerContext) {
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 and at its hours
// 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;
@@ -752,6 +764,7 @@ function register_fishing(ctx: ServerContext) {
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 }] : [] });
+35
View File
@@ -3,6 +3,9 @@ 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 { tag_api } from "$/common/tags.ts";
import { CYCLE_TICKS } from "$/common/time.ts";
import { biome_season } from "$/common/worldgen/biomes.ts";
import { test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
@@ -141,3 +144,35 @@ Deno.test("a cast rod shows its cast texture", async () => {
assertEquals(item_texture(rod), "bworld:fishing_rod_cast");
assertEquals(EverythingRegistry.get<ItemRegistry>("items", ROD)?.max_stack, 1);
});
Deno.test("a fish with biomes is only caught in them, by id or #tag", async () => {
const t = await test_game("mods");
// summer, when salmon and cod are both caught
while (biome_season("bworld:plains", t.game.world.time) !== "summer") t.game.world.time += CYCLE_TICKS;
const tags = tag_api();
// a column near spawn whose biome matches, in summer or winter
const find = (matches: (biome: string) => boolean) => {
for (let x = -4096; x <= 4096; x += 64) {
for (let z = -4096; z <= 4096; z += 64) {
const biome = t.game.world.biome_at(x, z);
const season = biome_season(biome, t.game.world.time);
if (matches(biome) && (season === "summer" || season === "winter")) return { x, y: 64, z };
}
}
throw new Error("no such biome near spawn");
};
const caught = (position: { x: number; y: number; z: number }) =>
new Set(
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"]),
);
});