Seasons based on a calendar
This commit is contained in:
+7
-4
@@ -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<string, number> = {
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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<SurfaceJson>;
|
||||
// 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<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 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
|
||||
|
||||
Reference in New Issue
Block a user