// Physical constants, atmosphere model, Keplerian orbital elements, speed of sound, ground tracking, and ascent options. import type { DifficultyLevel, GroundNetworkTier, MaterialSpec, MissionTemplate, Part, PreLaunchHazardEvent, SpaceWeather, } from "../types.js"; export const RHO0 = 1.225; export const SCALE_HEIGHT = 8500; export const MU = 3.986004418e14; export const RE = 6371000; export function airDensity(altM: number): number { if (altM < 0) return RHO0; if (altM > 220000) return 0; return RHO0 * Math.exp(-altM / SCALE_HEIGHT); } export function orbitalElements( rx: number, ry: number, vx: number, vy: number, ) { const rr = Math.hypot(rx, ry); const v2 = vx * vx + vy * vy; const energy = v2 / 2 - MU / rr; const hSpec = rx * vy - ry * vx; const a = energy < -1e-3 ? -MU / (2 * energy) : 1e12; const eSq = Math.max(0, 1 + (2 * energy * hSpec * hSpec) / (MU * MU)); const e = Math.sqrt(eSq); const periapsis = a * (1 - e) - RE; const apoapsis = energy >= 0 ? 1e9 : a * (1 + e) - RE; return { apoapsis, periapsis, energy, e }; } export function speedOfSoundAtAlt(alt: number): number { let tempK = 288.15; if (alt < 11000) tempK = Math.max(216.65, 288.15 - 0.0065 * Math.max(0, alt)); else if (alt < 25000) tempK = 216.65; else if (alt < 47000) tempK = 216.65 + 0.003 * (alt - 25000); else tempK = Math.max(185, 282.65 - 0.002 * (alt - 47000)); return Math.sqrt(1.4 * 287.05 * tempK); } export function computeGroundStation( alt: number, downrange: number, groundTier: GroundNetworkTier, ): string { return alt < 35_000 && downrange < 60_000 ? "PAD-39A OPTICAL/S-BAND" : downrange < 450_000 ? "CAPE CANAVERAL MILA" : downrange < 1_400_000 ? "BERMUDA DOWNRANGE" : alt < 160_000 ? "ATLANTIC TELEMETRY SHIP" : groundTier === "optical-deep-space" ? "DEEP SPACE OPTICAL ARRAY" : groundTier === "global-dsn" ? "GOLDSTONE / MADRID DSN" : "TDRS RELAY NETWORK"; } export interface AscentOptions { dt?: number; maxTime?: number; sampleEvery?: number; weather?: SpaceWeather; activeHazards?: PreLaunchHazardEvent[]; unlockedTechs?: string[]; mission?: Partial | null; difficulty?: DifficultyLevel | string | null; partDefectRiskPct?: number; rollAnomalies?: boolean; priceOf?: (part: Part, material?: MaterialSpec) => number; }