Files
kos-scripts/boot/moon-orbiter.ks
2025-10-29 15:40:14 -07:00

170 lines
4.2 KiB
Plaintext

@lazyglobal off.
wait until ship:unpacked.
rcs off.
lock throttle to 0.
runoncepath("0:/lib/common.ks").
runoncepath("0:/lib/moonmath.ks").
runoncepath("0:/lib/stage/atlas.ks").
runoncepath("0:/lib/stage/centaur.ks").
runoncepath("0:/lib/stage/probe.ks").
local state is lex().
local function ship_volume {
return ship:rootpart:getmodule("kosprocessor"):volume.
}
local function phase {
switch to ship_volume().
if not ship_volume():exists("state.json") {
set_phase("prelaunch").
}
set state to readjson("state.json").
return state["phase"].
}
local function set_phase {
parameter new_phase.
switch to ship_volume().
set state["phase"] to new_phase.
writejson(state, "state.json").
kuniverse:quicksaveto("moon-impactor-"+new_phase).
}
if phase() = "prelaunch" {
local target_inclination is max(moon:obt:inclination,ship:latitude).
local target_alt is 500_000.
set state["azimuth"] to launch_azimuth(target_inclination, target_alt).
local launch_params is moonmath:calculate_launch(target_inclination, state["azimuth"], target_alt).
local launch_time is launch_params:launch_time.
set state["moon_altitude"] to launch_params:moon_altitude.
set state["departure_ra"] to launch_params:burn_longitude.
print "Departing for moon at " + state["departure_ra"] + " right ascension".
wait 2.
until launch_time - time:seconds < 10 {
print "Not ready to launch yet, warping...".
warpto(launch_time-9).
wait 0.
wait until warp = 0.
wait until kuniverse:timewarp:issettled.
print "Maybe done warping?".
}
wait until time:seconds > launch_time.
atlas:launch(state["azimuth"], target_alt, 4).
stage.
set_phase("circularize").
}
if phase() = "circularize" {
centaur:orbit(state["azimuth"]).
centaur:power_down().
wait 1.
set_phase("parkingorbit").
}
local function ship_ra {
return mod(ship:longitude + ship:body:rotationangle + 360, 360).
}
if phase() = "parkingorbit" {
local transfer_dv is hohmann_transfer_dv(earth, ship:altitude, state["moon_altitude"]).
local burn_time is centaur:burn_time(transfer_dv / 2).
local burn_angle is (burn_time / ship:obt:period) * 360.
local burn_ra is mod(state["departure_ra"] - burn_angle + 360, 360).
// Just in case our departure point is RIGHT HERE, we want to go around at least once.
set warpmode to "physics".
set warp to 3.
wait until abs(ship_ra() - burn_ra) > 16.
set warp to 0.
wait until kuniverse:timewarp:issettled.
wait 1.
// Rails warp until we're nearish to the burn point
print "Warping to dLongitude < 15".
set warpmode to "rails".
set warp to 2.
wait until abs(ship_ra() - burn_ra) < 15.
set warp to 0.
wait until kuniverse:timewarp:issettled.
wait 1.
// Physics warp until we're nearish-er
print "Warping to dLongitude < 4".
set warpmode to "physics".
set warp to 3.
wait until abs(ship_ra() - burn_ra) < 4.
set warp to 0.
wait until kuniverse:timewarp:issettled.
wait 1.
centaur:power_up().
rcs on.
lock steering to ship:prograde.
wait until ship_ra() >= burn_ra.
print "Moon altitude is: " + state["moon_altitude"].
print "Transfer DV is: " + transfer_dv.
centaur:transfer_burn(state["moon_altitude"]).
wait 1.
set_phase("pretransfer").
}
if phase() = "pretransfer" {
rcs on.
panels on.
stage.
wait 1.
probe:power_up().
lock steering to sun:direction.
wait 1.
wait until steering_locked().
wait 1.
rcs off.
probe:power_down().
wait 1.
set_phase("transfer").
}
if phase() = "transfer" {
set warpmode to "rails".
warpto(eta:transition + time:seconds).
wait until ship:obt:body = moon.
wait until kuniverse:timewarp:issettled.
set_phase("lunar_arrival").
}
if phase() = "lunar_arrival" {
stage.
probe:capture_burn().
set_phase("lunar_deploy").
}
if phase() = "lunar_deploy" {
probe:power_up().
lock steering to sun:direction.
wait 1.
wait until steering_locked().
wait 1.
rcs off.
probe:power_down().
set_phase("done").
}
if phase() <> "done" {
print "Phase logic failed, expected to be in phase '" + phase() + "'".
}
print "Done".