From 7fe9c15b67e0be61eb0aa859e0610378c1c6c71b Mon Sep 17 00:00:00 2001 From: Paula Date: Fri, 9 Oct 2026 22:09:21 -0300 Subject: [PATCH] Seasons based on a calendar --- client/gui/hud.ts | 2 +- client/mods.ts | 2 +- common/time.ts | 11 +++++---- common/worldgen/biomes.ts | 23 +++++++++++++------ docs/modding/worldgen/biomes.md | 13 +++++++---- server/game/entity/entity.ts | 3 +++ server/game/entity/entity_manager.ts | 5 ++++ server/game/mod_runtime.ts | 9 ++++---- server/game/world.ts | 6 ++--- tests/biome_test.ts | 34 ++++++++++++++++++++++++---- tests/season_test.ts | 6 +++-- tests/time_test.ts | 7 +++--- 12 files changed, 88 insertions(+), 33 deletions(-) diff --git a/client/gui/hud.ts b/client/gui/hud.ts index 8d86c4f..4ad8950 100644 --- a/client/gui/hud.ts +++ b/client/gui/hud.ts @@ -155,7 +155,7 @@ export class Hud { `Day ${day}`, `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`, biome === undefined ? "Unknown" : display_name(biome), - biome === undefined ? "Unknown" : capitalize(biome_season(biome)), + biome === undefined ? "Unknown" : capitalize(biome_season(biome, client.level.time)), ]; const padding = INFO_PADDING * GUI_SCALE; diff --git a/client/mods.ts b/client/mods.ts index 4e451f8..4639678 100644 --- a/client/mods.ts +++ b/client/mods.ts @@ -93,7 +93,7 @@ const rules_world: SharedWorld = { 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); + return biome === undefined ? undefined : biome_season(biome, client!.level.time); }, }; diff --git a/common/time.ts b/common/time.ts index 4d8c73e..2307b82 100644 --- a/common/time.ts +++ b/common/time.ts @@ -12,8 +12,6 @@ export const TWILIGHT_TICKS = 60 * TICKS_PER_SECOND; // in game hours are longer during the day than at night, since both halves are 12 hours const DAY_TICKS_PER_HOUR = DAY_TICKS / 12; const NIGHT_TICKS_PER_HOUR = NIGHT_TICKS / 12; -// from midnight to 06:00, to count days from midnight -const TICKS_BEFORE_SIX = 6 * NIGHT_TICKS_PER_HOUR; // named times of day, as ticks into the cycle export const TIMES_OF_DAY: Record = { @@ -33,13 +31,18 @@ export function is_day(time: number): boolean { return time_of_day(time) < DAY_TICKS; } -// the day number, starting at 1 and going up at midnight, and the time on a 24 hour clock +// days since the world started, going up at 06:00: 0 on the first day. crops grow and seasons change on it +export function farming_day(time: number): number { + return Math.floor(time / CYCLE_TICKS); +} + +// the day number, the farming day counted from 1 so it also goes up at 06:00, and the time on a 24 hour clock export function clock(time: number): { day: number; hour: number; minute: number } { const t = time_of_day(time); const hours = t < DAY_TICKS ? 6 + t / DAY_TICKS_PER_HOUR : 18 + (t - DAY_TICKS) / NIGHT_TICKS_PER_HOUR; const total_minutes = Math.floor(hours * 60) % (24 * 60); return { - day: Math.floor((time + TICKS_BEFORE_SIX) / CYCLE_TICKS) + 1, + day: farming_day(time) + 1, hour: Math.floor(total_minutes / 60), minute: total_minutes % 60, }; diff --git a/common/worldgen/biomes.ts b/common/worldgen/biomes.ts index 3f80b44..7a1d8ae 100644 --- a/common/worldgen/biomes.ts +++ b/common/worldgen/biomes.ts @@ -2,11 +2,13 @@ // gets is still code (pick_biome in overworld.ts), these say what a biome looks like once it's picked import { EverythingRegistry } from "../everything_registry.ts"; import { ID_PATTERN } from "../mod_data.ts"; +import { farming_day } from "../time.ts"; import { TREE_KINDS, type TreeKind, type Wood, WOODS } from "./trees.ts"; export const SEASONS = ["spring", "summer", "autumn", "winter"] as const; export type Season = typeof SEASONS[number]; -export const DEFAULT_SEASON: Season = "spring"; +// how long each season lasts. day 1 to 49 is spring, 50 to 98 summer and so on, a year is 196 days +export const SEASON_DAYS = 49; // how a biome covers its ground, like minecraft's surface rules. blocks are ids export interface SurfaceJson { @@ -22,7 +24,7 @@ export interface SurfaceJson { export interface BiomeJson { id: string; - // spring when not set + // a season it's always in, like winter for snowy biomes. when not set it follows the calendar, see calendar_season season?: Season; surface: SurfaceJson; // "#rrggbb", minecraft's grass, foliage and water colors, which blocks pick with "tint". plains' when not set @@ -36,7 +38,7 @@ export interface BiomeJson { // what's registered in "biomes", with the defaults filled in export interface Biome { id: string; - season: Season; + season?: Season; surface: Required; // 0xRRGGBB colors: { grass?: number; foliage?: number; water?: number }; @@ -48,7 +50,7 @@ export function biome_from_json(json: BiomeJson): Biome { const color = (value: string | undefined) => value === undefined ? undefined : parseInt(value.slice(1), 16); return { id: json.id, - season: json.season ?? DEFAULT_SEASON, + season: json.season, surface: { top: surface.top, filler: surface.filler, @@ -74,9 +76,16 @@ export function biome_ids(): string[] { return EverythingRegistry.entries("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 season on a farming day (see farming_day in common/time.ts), which is 0 on the first day and goes up at 06:00 +export function calendar_season(day: number): Season { + const days_into_year = (day % (SEASON_DAYS * 4) + SEASON_DAYS * 4) % (SEASON_DAYS * 4); + return SEASONS[Math.floor(days_into_year / SEASON_DAYS)]; +} + +// a biome's season at a world time: its own when its json sets one, the calendar's otherwise, like for one that +// isn't registered (a removed mod's) +export function biome_season(id: string, time: number): Season { + return get_biome(id)?.season ?? calendar_season(farming_day(time)); } // the logs and leaves a kind of tree grows in a biome diff --git a/docs/modding/worldgen/biomes.md b/docs/modding/worldgen/biomes.md index a76d4b3..5d09510 100644 --- a/docs/modding/worldgen/biomes.md +++ b/docs/modding/worldgen/biomes.md @@ -1,8 +1,8 @@ # Biomes A biome says what the ground looks like where it's picked: the blocks on top, the colors grass, leaves and water are -tinted, which trees grow, and its season. Biomes are data, one file each in a mod's `worldgen/biomes/` folder. The base -game's are in `mods/bworld/worldgen/biomes/`. +tinted, which trees grow, and whether it has a season of its own. Biomes are data, one file each in a mod's +`worldgen/biomes/` folder. The base game's are in `mods/bworld/worldgen/biomes/`. > [!NOTE] > Which biome a column gets is still decided by the engine (`pick_biome` in `common/worldgen/overworld.ts`), from the @@ -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`, see [Seasons](#seasons) | `spring` | +| `season` | `spring`, `summer`, `autumn` or `winter`, always, see [Seasons](#seasons) | the calendar | | `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 | @@ -73,7 +73,12 @@ Biome ids are what features' `biome_at` gives and what spawns list. The base gam ## Seasons -A biome's season is fixed by its json, it doesn't change over the year. Scripts read it where something is: +Seasons follow the calendar, 49 days each: days 1 to 49 are spring, 50 to 98 summer, 99 to 147 autumn and 148 to 196 +winter, then it's spring again. Seasons change at 06:00, when `ctx.world.day` and the clock's day go up, so the first +spring is `ctx.world.day` 0 to 48. A biome that sets `season` is always in it instead, like winter in `snowy_plains` and +summer in `desert`. + +Scripts read the season where something is: | Where | Biome | Season | | ------------------------------------------------ | -------------------------- | --------------------------- | diff --git a/server/game/entity/entity.ts b/server/game/entity/entity.ts index a40a885..e729628 100644 --- a/server/game/entity/entity.ts +++ b/server/game/entity/entity.ts @@ -8,6 +8,7 @@ import { TICKS_PER_SECOND } from "$/common/constants.ts"; import type { BlockRegistry } from "$/common/everything_registry.ts"; import type { ItemStack } from "$/common/inventory.ts"; import type { Shape } from "$/common/block_shapes.ts"; +import type { Season } from "$/common/worldgen/biomes.ts"; import { type Collisions, type LiquidInCell, physics } from "$/common/physics.ts"; import type { EntityData, EntityEvent, EntityInfo } from "$/common/protocol.ts"; import type { ServerPlayer } from "../player.ts"; @@ -72,6 +73,8 @@ export interface EntityLevel { 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; + // that biome's season now, see biome_season in common/worldgen/biomes.ts + season_at(x: number, z: number): Season; add_entity(entity: Entity): void; get_entity(id: string): Entity | undefined; // entities whose feet are within the box around x, y, z diff --git a/server/game/entity/entity_manager.ts b/server/game/entity/entity_manager.ts index c145efa..66c32dd 100644 --- a/server/game/entity/entity_manager.ts +++ b/server/game/entity/entity_manager.ts @@ -4,6 +4,7 @@ import { CHUNK_SIZE } from "$/common/constants.ts"; import { is_within_view } from "$/common/chunk_streaming.ts"; import { liquid_block, liquid_in_cell } from "$/common/utils.ts"; +import { biome_season } from "$/common/worldgen/biomes.ts"; import type { ItemStack } from "$/common/inventory.ts"; import type { EntityEvent, EntityInfo, ServerMessage } from "$/common/protocol.ts"; import type { ServerPlayer } from "../player.ts"; @@ -108,6 +109,10 @@ export class EntityManager implements EntityLevel { return this.#host.world.biome_at(x, z); } + season_at(x: number, z: number) { + return biome_season(this.biome_at(x, z), this.#host.world.time); + } + 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)); diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index 88af2a7..08f337c 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -510,7 +510,7 @@ export class ModRuntime { return game.world.biome_at(x, z); }, get season() { - return biome_season(game.world.biome_at(x, z)); + return biome_season(game.world.biome_at(x, z), game.world.time); }, get data() { return game.world.get_tile(x, y, z)?.mod_data; @@ -714,7 +714,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))), + season_at: (x, z) => + biome_season(game().world.biome_at(Math.floor(x), Math.floor(z)), game().world.time), get seed() { return game().world.seed; }, @@ -1205,7 +1206,7 @@ function player_api(game: GameServer, player: ServerPlayer): Player { return game.world.biome_at(Math.floor(player.x), Math.floor(player.z)); }, get season() { - return biome_season(this.biome); + return biome_season(this.biome, game.world.time); }, inventory, get selected_slot() { @@ -1338,7 +1339,7 @@ export function entity_ref(entity: Entity): EntityRef { return entity.level.biome_at(Math.floor(entity.x), Math.floor(entity.z)); }, get season() { - return biome_season(this.biome); + return entity.level.season_at(Math.floor(entity.x), Math.floor(entity.z)); }, get removed() { return entity.removed; diff --git a/server/game/world.ts b/server/game/world.ts index 0ad0ef0..1c0f6ac 100644 --- a/server/game/world.ts +++ b/server/game/world.ts @@ -10,7 +10,7 @@ import { AIR_ID, BlockChange } from "$/common/protocol.ts"; import { block_value, chunk_key, default_block_value } from "$/common/utils.ts"; import { LruMap } from "./lru.ts"; import { collision_shape_at, EMPTY_SHAPE, type Shape } from "$/common/block_shapes.ts"; -import { CYCLE_TICKS } from "$/common/time.ts"; +import { farming_day } from "$/common/time.ts"; import { encode_level_chunk } from "$/common/chunk_streaming.ts"; import type { ItemData } from "$/common/inventory.ts"; import { CHUNK_VOLUME, ChunkBlocks, decode_chunk, encode_chunk, is_changed, SavedChunk } from "./saves.ts"; @@ -74,9 +74,9 @@ export class ServerWorld { // ticks since 06:00 of day 1, see common/time.ts time = 0; - // days since the world started, going up at 06:00. the farming day, unlike the clock's day that starts at midnight + // days since the world started, see farming_day in common/time.ts get day() { - return Math.floor(this.time / CYCLE_TICKS); + return farming_day(this.time); } // set when anything that gets saved changes diff --git a/tests/biome_test.ts b/tests/biome_test.ts index ed78215..5e03959 100644 --- a/tests/biome_test.ts +++ b/tests/biome_test.ts @@ -1,6 +1,14 @@ // biomes are data, worldgen/biomes/.json in a mod. see common/worldgen/biomes.ts import { assert, assertEquals } from "@std/assert"; -import { biome_from_json, biome_ids, get_biome, validate_biome } from "$/common/worldgen/biomes.ts"; +import { CYCLE_TICKS } from "$/common/time.ts"; +import { + biome_from_json, + biome_ids, + biome_season, + calendar_season, + get_biome, + validate_biome, +} from "$/common/worldgen/biomes.ts"; import { test_game } from "./helpers.ts"; const PLAIN = { id: "my_mod:flat", surface: { top: "bworld:grass", filler: "bworld:dirt", filler_depth: 3 } }; @@ -12,11 +20,29 @@ Deno.test("the bworld mod registers every biome the overworld picks", async () = assertEquals(get_biome("bworld:river")?.surface.underwater_top, "bworld:sand"); }); -Deno.test("a biome is in spring unless it says otherwise", async () => { +Deno.test("seasons last 49 days, starting with spring on day 1 and changing at 06:00", () => { + assertEquals(calendar_season(0), "spring"); + assertEquals(calendar_season(48), "spring"); + assertEquals(calendar_season(49), "summer"); + assertEquals(calendar_season(97), "summer"); + assertEquals(calendar_season(98), "autumn"); + assertEquals(calendar_season(147), "winter"); + assertEquals(calendar_season(195), "winter"); + assertEquals(calendar_season(196), "spring"); +}); + +Deno.test("a biome follows the calendar unless it says otherwise", async () => { await test_game("mods"); - assertEquals(get_biome("bworld:plains")?.season, "spring"); - assertEquals(biome_from_json(PLAIN).season, "spring"); + assertEquals(get_biome("bworld:plains")?.season, undefined); + assertEquals(biome_from_json(PLAIN).season, undefined); assertEquals(biome_from_json({ ...PLAIN, season: "winter" }).season, "winter"); + // 06:00 of day 50 + const day_50 = 49 * CYCLE_TICKS; + assertEquals(biome_season("bworld:plains", 0), "spring"); + assertEquals(biome_season("bworld:plains", day_50 - 1), "spring"); + assertEquals(biome_season("bworld:plains", day_50), "summer"); + assertEquals(biome_season("bworld:snowy_plains", day_50), "winter"); + assertEquals(biome_season("my_mod:removed", day_50), "summer"); }); Deno.test("a biome's surface defaults: underwater is its filler, cliffs aren't bare", () => { diff --git a/tests/season_test.ts b/tests/season_test.ts index 4f1d0f3..73ff871 100644 --- a/tests/season_test.ts +++ b/tests/season_test.ts @@ -1,6 +1,7 @@ // 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 { get_biome } from "$/common/worldgen/biomes.ts"; import { copy_dir, test_game } from "./helpers.ts"; const SCRIPT = ` @@ -16,7 +17,7 @@ export function setup(ctx: ServerContext) { } `; -// bworld's biomes, with every one but the oceans in winter. oceans keep spring by leaving it out +// bworld's biomes, with every one but the oceans in winter. oceans follow the calendar, spring on day 1 function season_mods() { const dir = Deno.makeTempDirSync({ prefix: "bworld_seasons_" }); copy_dir("mods/bworld", `${dir}/bworld`); @@ -64,7 +65,8 @@ Deno.test("blocks, entities, players and the world know their biome's season", a 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"; + // it's day 1, so oceans without their own season are in spring + const season_of = (biome: string) => get_biome(biome)?.season ?? "spring"; // spawn is on dry land const biome = ctx.world.biome_at(0, 0); diff --git a/tests/time_test.ts b/tests/time_test.ts index 12a2a37..ce2068b 100644 --- a/tests/time_test.ts +++ b/tests/time_test.ts @@ -8,9 +8,10 @@ Deno.test("a day is 30 minutes and a night 10, tick 0 is 06:00 of day 1", () => assertEquals(format_clock(0), "Day 1, 06:00"); assertEquals(format_clock(DAY_TICKS / 2), "Day 1, 12:00"); assertEquals(format_clock(DAY_TICKS), "Day 1, 18:00"); - // midnight is halfway through the night, and the day number goes up there + // midnight is halfway through the night, the day number goes up at 06:00 like the farming day assertEquals(format_clock(DAY_TICKS + NIGHT_TICKS / 2 - 1), "Day 1, 23:59"); - assertEquals(format_clock(DAY_TICKS + NIGHT_TICKS / 2), "Day 2, 00:00"); + assertEquals(format_clock(DAY_TICKS + NIGHT_TICKS / 2), "Day 1, 00:00"); + assertEquals(format_clock(CYCLE_TICKS - 1), "Day 1, 05:59"); assertEquals(format_clock(CYCLE_TICKS), "Day 2, 06:00"); assertEquals(clock(3 * CYCLE_TICKS + DAY_TICKS / 4), { day: 4, hour: 9, minute: 0 }); assert(is_day(DAY_TICKS - 1) && !is_day(DAY_TICKS) && is_day(CYCLE_TICKS)); @@ -40,7 +41,7 @@ Deno.test("the server keeps the time, tells players, saves it, and /time changes send(1, { type: "chat", text: "/time set midnight" }); let messages = take(1); assertEquals(messages.find((m) => m.type === "time").time, DAY_TICKS + NIGHT_TICKS / 2); - assert(messages.some((m) => m.text === "alice set the time to Day 2, 00:00")); + assert(messages.some((m) => m.text === "alice set the time to Day 1, 00:00")); // named times are the next one, so time never goes backwards send(1, { type: "chat", text: "/time set day" });