Seasons api

This commit is contained in:
2026-10-08 13:42:18 -03:00
parent 7e7ac8f6f4
commit dd66d6caa7
16 changed files with 231 additions and 10 deletions
+12
View File
@@ -52,6 +52,8 @@ export interface Chunk {
max_y: number;
// only for translucent meshes that have to be sorted again as the camera moves
translucent_sort?: TranslucentSort;
// each column's biome id, x then z
biomes: string[];
// each column's biome grass color, x then z, then each column's foliage color, then water color
biome_colors: Uint32Array;
// from its last mesh, see get_light. undefined until it's been meshed
@@ -189,6 +191,7 @@ export class ClientLevel {
if (chunk) {
// its old mesh stays up until the new one is ready
chunk.blocks = blocks;
chunk.biomes = biomes;
chunk.biome_colors = biome_colors;
this.#mark_dirty(chunk);
} else {
@@ -196,6 +199,7 @@ export class ClientLevel {
x,
z,
blocks,
biomes,
biome_colors,
dirty: true,
mesh_version: 0,
@@ -330,6 +334,14 @@ export class ClientLevel {
return block ? collision_shape_at(block, value) : true;
}
// the column's biome, undefined in unloaded chunks
biome_at(x: number, z: number): string | undefined {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
return this.get_chunk(chunk_x, chunk_z)
?.biomes[(z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)];
}
// the biome's water color at a column as 0xRRGGBB, see "tint". plains' in unloaded chunks
water_color(x: number, z: number) {
const chunk_x = Math.floor(x / CHUNK_SIZE);
+16
View File
@@ -17,6 +17,7 @@ import { GuiModScreen } from "./gui/gui_mod_screens.ts";
import { clear_hooks, expose_engine, loading_mod } from "$/common/hooks.ts";
import { common_engine } from "$/common/engine_modules.ts";
import { client_engine } from "./engine_modules.ts";
import { biome_season } from "$/common/worldgen/biomes.ts";
// each loaded mod's credits file, for the credits screen
export const mod_credits: { name: string; version: string; text: string }[] = [];
@@ -89,6 +90,11 @@ const rules_world: SharedWorld = {
const shape = client?.level.collision_shape(x, y, z) ?? false;
return covers_face(shape === true ? FULL_SHAPE : shape === false ? EMPTY_SHAPE : shape, face);
},
biome_at: (x, z) => client?.level.biome_at(Math.floor(x), Math.floor(z)),
season_at(x, z) {
const biome = this.biome_at(x, z);
return biome === undefined ? undefined : biome_season(biome);
},
};
// this client changed the block at x y z ahead of the server, the blocks beside it change with it the way they will
@@ -267,6 +273,14 @@ function client_context(listing: ModListing): ClientContext {
const { x, y, z } = need_client().player;
return { x, y, z };
},
get biome() {
const { x, z } = need_client().player;
return rules_world.biome_at(x, z);
},
get season() {
const { x, z } = need_client().player;
return rules_world.season_at(x, z);
},
is_swimming: () => need_client().player.swimming,
},
world: {
@@ -275,6 +289,8 @@ function client_context(listing: ModListing): ClientContext {
if (nid === AIR) return AIR_ID;
return EverythingRegistry.get_by_id<BlockRegistry>("blocks", nid)?.id;
},
biome_at: (x, z) => rules_world.biome_at(x, z),
season_at: (x, z) => rules_world.season_at(x, z),
get time() {
return need_client().level.time;
},
+14 -4
View File
@@ -1,7 +1,7 @@
// what client scripts get, see docs/modding/gui/index.md
import type { Id, ItemStack, KeyCode, ModInfo, Position, TagApi } from "./shared.ts";
import type { Id, ItemStack, KeyCode, ModInfo, Position, Season, TagApi } from "./shared.ts";
export type { Id, ItemStack, KeyCode, ModInfo, Position, TagApi, TagRegistry } from "./shared.ts";
export type { Id, ItemStack, KeyCode, ModInfo, Position, Season, TagApi, TagRegistry } from "./shared.ts";
export interface ClientContext {
mod: ModInfo;
@@ -12,11 +12,21 @@ export interface ClientContext {
// the way jei's recipes screen checks options.keyInventory
options: KeyMappings;
net: ClientNet;
// is_swimming: lying flat in water in the 0.8 tall box, like minecraft's isSwimming
player: { readonly name: string; readonly position: Readonly<Position>; is_swimming(): boolean };
// is_swimming: lying flat in water in the 0.8 tall box, like minecraft's isSwimming. biome and season are where
// their feet are, undefined until that chunk loads
player: {
readonly name: string;
readonly position: Readonly<Position>;
readonly biome: Id | undefined;
readonly season: Season | undefined;
is_swimming(): boolean;
};
// read only, what this client sees
world: {
get_block(x: number, y: number, z: number): Id | undefined;
// the column's biome and its season, undefined when the chunk isn't loaded
biome_at(x: number, z: number): Id | undefined;
season_at(x: number, z: number): Season | undefined;
// ticks since 06:00 of day 1, see ServerWorld.time
readonly time: number;
};
+15 -2
View File
@@ -1,10 +1,10 @@
// what server scripts get, see docs/modding/
import type { EventSignal, Face, Id, ItemStack, ModInfo, Position, TagApi } from "./shared.ts";
import type { EventSignal, Face, Id, ItemStack, ModInfo, Position, Season, TagApi } from "./shared.ts";
import type { ArgumentType, CommandNode } from "../commands.ts";
export type { ArgumentType, CommandContext, CommandNode, Suggestion } from "../commands.ts";
export type { EventSignal, Face, Id, ItemStack, ModInfo, Position, TagApi, TagRegistry } from "./shared.ts";
export type { EventSignal, Face, Id, ItemStack, ModInfo, Position, Season, TagApi, TagRegistry } from "./shared.ts";
export interface ServerContext {
mod: ModInfo;
@@ -34,6 +34,9 @@ export interface BlockRef {
readonly x: number;
readonly y: number;
readonly z: number;
// the biome of its column and that biome's season, see ServerWorld.biome_at
readonly biome: Id;
readonly season: Season;
// tile data, any json value. saved with the world, never sent to clients
// deno-lint-ignore no-explicit-any
data: any;
@@ -221,6 +224,10 @@ export interface ServerWorld {
// into, so it goes through plants and other blocks without collision. undefined when nothing is in the way
raycast(from: Position, to: Position, options?: RaycastOptions): RaycastHit | undefined;
is_loaded(x: number, z: number): boolean;
// the column's biome, like "bworld:plains", and that biome's season. in chunks that aren't loaded it's what
// generation makes there, which takes a while the first time
biome_at(x: number, z: number): Id;
season_at(x: number, z: number): Season;
readonly seed: string;
// ticks since 06:00 of day 1. a day is 36000 ticks (06:00 to 18:00), a night 12000
readonly time: number;
@@ -252,6 +259,9 @@ export interface EntityRef {
readonly id: string;
readonly type: Id;
readonly position: Readonly<Position>;
// the biome where its feet are and that biome's season
readonly biome: Id;
readonly season: Season;
readonly removed: boolean;
teleport(x: number, y: number, z: number): void;
remove(): void;
@@ -265,6 +275,9 @@ export interface Player {
// ctx.world.raycast(player.eye_position, ...)
readonly eye_position: Readonly<Position>;
readonly look_direction: Readonly<Position>;
// the biome where their feet are and that biome's season
readonly biome: Id;
readonly season: Season;
readonly inventory: Container; // 36 slots, hotbar is 0-8
readonly selected_slot: number;
readonly held_item: ItemStack | undefined;
+6
View File
@@ -3,6 +3,9 @@
export type Id = string;
// a biome's season, from its json. see docs/modding/worldgen/biomes.md
export type Season = "spring" | "summer" | "autumn" | "winter";
export interface ItemStack {
readonly id: Id;
count: number;
@@ -76,6 +79,9 @@ export interface SharedWorld {
// whether the block's side is a full square things can attach to, like minecraft's isFaceSturdy: its collision
// shape covers the whole face. false for air, plants, water and the sides of a slab
is_face_sturdy(x: number, y: number, z: number, face: Face): boolean;
// the column's biome, like "bworld:plains", and that biome's season. undefined when the chunk isn't loaded
biome_at(x: number, z: number): Id | undefined;
season_at(x: number, z: number): Season | undefined;
}
// a block in the world, for rules
+5
View File
@@ -74,6 +74,11 @@ export function biome_ids(): string[] {
return EverythingRegistry.entries<Biome>("biomes").map(([id]) => id);
}
// a biome's season, spring for one that isn't registered, like a removed mod's
export function biome_season(id: string): Season {
return get_biome(id)?.season ?? DEFAULT_SEASON;
}
// the logs and leaves a kind of tree grows in a biome
export function tree_wood(kind: TreeKind, biome: Biome): Wood {
const wood = biome.trees?.wood[kind];
+5 -3
View File
@@ -69,8 +69,9 @@ component.
| `on_second(block, params, dt)` | Every second, near a player. | |
| `on_new_day(block, params, day)` | Every morning at 06:00, anywhere in the world. | |
`block` is `{ id, x, y, z, data }`, where `data` is the block's [tile data](tiledata.md). `params` is what the block's
JSON lists for this component, and `dt` is in seconds.
`block` is `{ id, x, y, z, biome, season, data }`, where `data` is the block's [tile data](tiledata.md) and `biome` and
`season` are its column's (see [Seasons](../worldgen/biomes.md#seasons)). `params` is what the block's JSON lists for
this component, and `dt` is in seconds.
## Rules
@@ -96,7 +97,8 @@ export function setup(ctx: SharedContext) {
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. | |
Here `block` is `{ id, x, y, z }`, without tile data, which clients never have. `ctx.world` in a shared script has
`get_block`, `get_state`, `set_state` and `is_face_sturdy`, and only works inside rules.
`get_block`, `get_state`, `set_state`, `is_face_sturdy`, `biome_at` and `season_at` (see
[Seasons](../worldgen/biomes.md#seasons)), and only works inside rules.
### Neighbors
+2
View File
@@ -147,6 +147,8 @@ interface EntityRef {
readonly id: string; // this entity's own id
readonly type: string; // its kind, like "bworld:fishing_hook"
readonly position: { x: number; y: number; z: number };
readonly biome: string; // where its feet are, see ../worldgen/biomes.md#seasons
readonly season: Season; // that biome's
readonly removed: boolean;
teleport(x: number, y: number, z: number): void;
remove(): void;
+2
View File
@@ -22,6 +22,8 @@ interface Player {
readonly position: { x: number; y: number; z: number }; // their feet
readonly eye_position: { x: number; y: number; z: number }; // where they see from, lower while swimming
readonly look_direction: { x: number; y: number; z: number }; // which way they look, 1 long
readonly biome: string; // where their feet are, see ../worldgen/biomes.md#seasons
readonly season: Season; // that biome's
readonly inventory: Container; // 36 slots, the hotbar is 0-8
readonly selected_slot: number;
readonly held_item: ItemStack | undefined;
+2
View File
@@ -141,7 +141,9 @@ Client scripts can read what the client knows, to show it:
ctx.player.name;
ctx.player.position; // { x, y, z }
ctx.player.is_swimming(); // see ../entities/players.md#swimming
ctx.player.biome; // and ctx.player.season, see ../worldgen/biomes.md#seasons
ctx.world.get_block(x, y, z); // a block id, what this client sees
ctx.world.biome_at(x, z); // and ctx.world.season_at(x, z), undefined where nothing's loaded
ctx.world.time; // see Time
ctx.items.ids(); // every item, in load order
ctx.items.exists(id);
+28 -1
View File
@@ -41,7 +41,7 @@ game's are in `mods/bworld/worldgen/biomes/`.
| Field | What it does | When not set |
| --------------------------- | ---------------------------------------------------------------------------------------- | ------------ |
| `id` | in your mod's namespace | required |
| `season` | `spring`, `summer`, `autumn` or `winter`. Nothing uses it yet | `spring` |
| `season` | `spring`, `summer`, `autumn` or `winter`, see [Seasons](#seasons) | `spring` |
| `surface.top` | the block on top of the ground | required |
| `surface.filler` | the blocks under it | required |
| `surface.filler_depth` | how many blocks of filler, 0 to 32. Stone is below | required |
@@ -70,3 +70,30 @@ Biome ids are what features' `biome_at` gives and what spawns list. The base gam
`bworld:jagged_peaks` and `bworld:frozen_peaks`.
`/locatebiome <id>` finds the nearest one.
## Seasons
A biome's season is fixed by its json, it doesn't change over the year. Scripts read it where something is:
| Where | Biome | Season |
| ------------------------------------------------ | -------------------------- | --------------------------- |
| a block in a server component, like `on_tick` | `block.biome` | `block.season` |
| an entity from `ctx.world.spawn_entity` | `entity.biome` | `entity.season` |
| a player, on the server | `player.biome` | `player.season` |
| any column, in server, shared and client scripts | `ctx.world.biome_at(x, z)` | `ctx.world.season_at(x, z)` |
| the player on this client | `ctx.player.biome` | `ctx.player.season` |
Entities and players go by the column their feet are in. On the server a column always has a biome: in a chunk that
isn't loaded it's what generation makes there, which takes a while the first time. In shared rules and client scripts
it's what that client has loaded, and `undefined` where it has nothing.
```ts
ctx.components.register_block("my_mod:crop", {
on_new_day(block) {
if (block.season === "winter") return; // doesn't grow in winter
// ...
},
});
```
`Season` is exported from `bworld/server`, `bworld/client` and `bworld/shared`.
+2
View File
@@ -70,6 +70,8 @@ export interface EntityLevel {
// the liquid there and how high it is, for liquids pushing things along
liquid_at(x: number, y: number, z: number): LiquidInCell | undefined;
get_block_info(x: number, y: number, z: number): BlockRegistry | undefined;
// the column's biome, like "bworld:plains"
biome_at(x: number, z: number): string;
add_entity(entity: Entity): void;
get_entity(id: string): Entity | undefined;
// entities whose feet are within the box around x, y, z
+4
View File
@@ -104,6 +104,10 @@ export class EntityManager implements EntityLevel {
return this.#host.world.get_block_info(x, y, z);
}
biome_at(x: number, z: number) {
return this.#host.world.biome_at(x, z);
}
liquid_at(x: number, y: number, z: number) {
const world = this.#host.world;
return liquid_in_cell(world.get_block_value(x, y, z), world.get_block_info(x, y, z));
+23
View File
@@ -60,6 +60,7 @@ import type { EffectJson } from "$/common/mod_data.ts";
import { create_entity } from "./entity/entity_types.ts";
import { attribute_type, type AttributeType, MODIFIER_OPERATIONS } from "./entity/attributes.ts";
import { effect_type, EffectInstance, type EffectType } from "./entity/effects.ts";
import { biome_season } from "$/common/worldgen/biomes.ts";
// a list of handlers that can't break each other: one throwing is logged under its mod and the rest still run
export class Signal<T> {
@@ -249,6 +250,8 @@ export class ModRuntime {
get_state: world.get_state,
set_state: world.set_state,
is_face_sturdy: world.is_face_sturdy,
biome_at: world.biome_at,
season_at: world.season_at,
},
components: {
register_block: (id, rules) => {
@@ -502,6 +505,12 @@ export class ModRuntime {
x,
y,
z,
get biome() {
return game.world.biome_at(x, z);
},
get season() {
return biome_season(game.world.biome_at(x, z));
},
get data() {
return game.world.get_tile(x, y, z)?.mod_data;
},
@@ -703,6 +712,8 @@ export class ModRuntime {
};
},
is_loaded: () => true,
biome_at: (x, z) => game().world.biome_at(Math.floor(x), Math.floor(z)),
season_at: (x, z) => biome_season(game().world.biome_at(Math.floor(x), Math.floor(z))),
get seed() {
return game().world.seed;
},
@@ -1185,6 +1196,12 @@ function player_api(game: GameServer, player: ServerPlayer): Player {
z: -Math.cos(player.yaw) * cos_pitch,
};
},
get biome() {
return game.world.biome_at(Math.floor(player.x), Math.floor(player.z));
},
get season() {
return biome_season(this.biome);
},
inventory,
get selected_slot() {
return player.selected_slot;
@@ -1312,6 +1329,12 @@ export function entity_ref(entity: Entity): EntityRef {
get position() {
return { x: entity.x, y: entity.y, z: entity.z };
},
get biome() {
return entity.level.biome_at(Math.floor(entity.x), Math.floor(entity.z));
},
get season() {
return biome_season(this.biome);
},
get removed() {
return entity.removed;
},
+93
View File
@@ -0,0 +1,93 @@
// blocks, entities, players and the world tell scripts their biome's season, see docs/modding/worldgen/biomes.md
import { assertEquals } from "@std/assert";
import type { ServerContext } from "$/common/mod_api/server.ts";
import { copy_dir, test_game } from "./helpers.ts";
const SCRIPT = `
import type { ServerContext } from "bworld/server";
export function setup(ctx: ServerContext) {
(globalThis as any).season_test = { ctx, created: [] };
ctx.components.register_block("testmod:thermometer", {
on_create(block) {
(globalThis as any).season_test.created.push({ biome: block.biome, season: block.season });
},
});
}
`;
// bworld's biomes, with every one but the oceans in winter. oceans keep spring by leaving it out
function season_mods() {
const dir = Deno.makeTempDirSync({ prefix: "bworld_seasons_" });
copy_dir("mods/bworld", `${dir}/bworld`);
const biomes = `${dir}/bworld/worldgen/biomes`;
for (const { name } of Deno.readDirSync(biomes)) {
if (name.includes("ocean")) continue;
const json = JSON.parse(Deno.readTextFileSync(`${biomes}/${name}`));
json.biome.season = "winter";
Deno.writeTextFileSync(`${biomes}/${name}`, JSON.stringify(json));
}
const mod = `${dir}/testmod`;
Deno.mkdirSync(`${mod}/blocks`, { recursive: true });
Deno.mkdirSync(`${mod}/scripts`);
Deno.writeTextFileSync(
`${mod}/manifest.json`,
JSON.stringify({
format_version: 1,
id: "testmod",
name: "Test",
version: "1.0.0",
dependencies: [{ id: "bworld", version: "*" }],
scripts: { server: "scripts/server.ts" },
}),
);
Deno.writeTextFileSync(
`${mod}/blocks/thermometer.json`,
JSON.stringify({
format_version: 1,
block: {
id: "testmod:thermometer",
textures: "bworld:stone",
mining: { toughness: 1 },
components: { "testmod:thermometer": {} },
},
}),
);
Deno.writeTextFileSync(`${mod}/scripts/server.ts`, SCRIPT);
return dir;
}
Deno.test("blocks, entities, players and the world know their biome's season", async () => {
const dir = season_mods();
try {
const { game, join, send } = await test_game(dir);
const { ctx, created } = (globalThis as unknown as {
season_test: { ctx: ServerContext; created: { biome: string; season: string }[] };
}).season_test;
const season_of = (biome: string) => biome.includes("ocean") ? "spring" : "winter";
// spawn is on dry land
const biome = ctx.world.biome_at(0, 0);
assertEquals(biome, game.world.biome_at(0, 0));
assertEquals(season_of(biome), "winter");
assertEquals(ctx.world.season_at(0.7, 0.2), "winter");
game.set_block(0, 100, 0, "testmod:thermometer");
assertEquals(created, [{ biome, season: season_of(biome) }]);
const chicken = ctx.world.spawn_entity("bworld:chicken", 0.5, 100, 0.5);
assertEquals([chicken.biome, chicken.season], [biome, season_of(biome)]);
join(1, "alice");
send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
const alice = ctx.players.by_name("alice")!;
assertEquals([alice.biome, alice.season], [biome, season_of(biome)]);
// somewhere far off, in a chunk nobody loaded, it's what generation makes there
const far = ctx.world.biome_at(5000, -3000);
assertEquals(far, game.world.biome_at(5000, -3000));
assertEquals(ctx.world.season_at(5000, -3000), season_of(far));
} finally {
Deno.removeSync(dir, { recursive: true });
}
});
+2
View File
@@ -67,6 +67,8 @@ function client_of(t: Game) {
const block = info(id);
return block !== undefined && covers_face(collision_shape_at(block, state << 16), face);
},
biome_at: () => undefined,
season_at: () => undefined,
};
setup_shared({
mod: { id: "bworld", version: "test" },