ludic/packages/ludic.clock/calendar.ludic
2026-09-25 14:02:21 +03:00

45 lines
2.5 KiB
Text

# calendar.ludic - where a day falls: its month, its year, and its season, a pure function of the
# day. The game says how long a month is, how many make a year, and which season each month is.
export function clock_calendar(clock_st: mut ClockState, month_days: int, season_of_month: []int) -> void {
clock_st.cal_month_days = month_days
clock_st.cal_year_months = len(season_of_month)
clock_st.cal_season = season_of_month
}
export function clock_month_days(clock_st: ClockState) -> int { return clock_st.cal_month_days }
export function clock_year_months(clock_st: ClockState) -> int { return clock_st.cal_year_months }
# the month a day falls in, 0 .. months - 1
export function clock_month_of(clock_st: ClockState, day: int) -> int { return ((day - 1) / clock_st.cal_month_days) % clock_st.cal_year_months }
export function clock_year_of(clock_st: ClockState, day: int) -> int { return (day - 1) / (clock_st.cal_month_days * clock_st.cal_year_months) + 1 }
export function clock_day_of_month_of(clock_st: ClockState, day: int) -> int { return (day - 1) % clock_st.cal_month_days + 1 }
export function clock_season_of(clock_st: ClockState, day: int) -> int {
if clock_st.cal_season == null { return 0 }
return clock_st.cal_season[clock_month_of(clock_st, day)]
}
export function clock_month(clock_st: ClockState) -> int { return clock_month_of(clock_st, clock_st.ck_day) }
export function clock_year(clock_st: ClockState) -> int { return clock_year_of(clock_st, clock_st.ck_day) }
export function clock_day_of_month(clock_st: ClockState) -> int { return clock_day_of_month_of(clock_st, clock_st.ck_day) }
export function clock_season(clock_st: ClockState) -> int { return clock_season_of(clock_st, clock_st.ck_day) }
# days from `day` until the season is another; 0 when a year has only one
export function clock_days_to_change_from(clock_st: ClockState, day: int) -> int {
let here = clock_season_of(clock_st, day)
for d in 1 .. clock_st.cal_month_days * clock_st.cal_year_months {
if clock_season_of(clock_st, day + d) != here { return d }
}
return 0
}
export function clock_days_to_change(clock_st: ClockState) -> int { return clock_days_to_change_from(clock_st, clock_st.ck_day) }
# the first day (counted from day one) that falls in a season; -1 when none does
export function clock_first_day_of(clock_st: ClockState, season: int) -> int {
for d in 1 .. clock_st.cal_month_days * clock_st.cal_year_months + 1 {
if clock_season_of(clock_st, d) == season { return d }
}
return -1
}