[engine] Sun: nextInvitation checks yesterday, today and tomorrow; addDays steps the calendar; NamedPlace

This commit is contained in:
2026-11-02 00:01:50 +01:00
parent fb7171c3a7
commit 1e8d1cda5a
5 changed files with 101 additions and 6 deletions
+26 -1
View File
@@ -11,7 +11,7 @@
* (.agents/features/sun.md D3). The place is a city the player chose, or a guess
* from the device's zone: never GPS (CLAUDE.md §5.6 rule 6).
*/
import { isLocalDay, localDay, type LocalDay } from '../time/localDay.js';
import { addDays, isLocalDay, localDay, type LocalDay } from '../time/localDay.js';
export interface Place {
/** Degrees, north positive. */
@@ -22,6 +22,14 @@ export interface Place {
readonly timeZone: string;
}
/** A place with the name the player knows it by: a town they chose, or the guess. */
export interface NamedPlace extends Place {
readonly name: string;
/** Province, state or region, or ''. */
readonly region: string;
readonly country: string;
}
/**
* One local day of sun. Sunrise and sunset are the ones around this day's solar
* noon, so tonight's sunset may fall after midnight, on the next local day (D2).
@@ -134,6 +142,23 @@ export function sunDay(day: LocalDay, place: Place): SunDay {
};
}
/**
* The next invitation at or after `now`, and the sun's day it belongs to. Yesterday
* is checked first: just after midnight in Reykjavík in June, last evening's sunset
* is still a few minutes away (D2).
*/
export function nextInvitation(now: number, place: Place): { readonly sun: SunDay; readonly at: number } {
if (!Number.isFinite(now)) throw new RangeError(`nextInvitation: now must be a finite instant, got ${String(now)}`);
const today = localDay(now, place.timeZone);
for (const offset of [-1, 0, 1, 2]) {
const sun = sunDay(addDays(today, offset), place);
const at = invitation(sun);
if (at >= now) return { sun, at };
}
// Unreachable: tomorrow's invitation is always after now. Kept so a broken sun fails loudly.
throw new Error(`nextInvitation: no invitation within two days of ${today}`);
}
/**
* When the game invites the player outside: the sunset; in polar day the lowest
* light; in polar night noon, when the twilight is brightest (D4; decided by the