Files
kos-scripts/lib/stage/able.ks
2025-10-29 15:40:14 -07:00

208 lines
5.0 KiB
Plaintext

@lazyglobal off.
global able is lex(
"launch", launch@,
"burn", burn@,
"orbit", do_orbit@,
"circularize", circularize@,
"yeet", yeet@,
"power_down", power_down@,
"power_up", power_up@
).
local function engine {
local early is ship:partsdubbed("ROE-AJ10Early").
local mid is ship:partsdubbed("ROE-AJ10Mid").
for eng in early {
return eng.
}
for eng in mid {
return eng.
}
}
local function ignite {
rcs on.
lock throttle to 1.0.
wait 3.
stage.
}
local function is_ablestar {
return not ship:partsdubbed("ROE-AJ10Mid"):empty.
}
local function yeet {
parameter hdg, tgt_alt.
power_down().
set warpmode to "physics".
set warp to 3.
if is_ablestar() {
wait until eta:apoapsis < 180.
} else {
wait until eta:apoapsis < 100.
}
set warp to 0.
wait until kuniverse:timewarp:issettled.
power_up().
rcs on.
local target is heading(hdg, ship:obt:trueanomaly - 180, -hdg).
lock steering to target.
if is_ablestar() {
wait until eta:apoapsis < 150.
} else {
wait until eta:apoapsis < 80.
}
lock throttle to 1.0.
wait 3.
stage.
wait until ship:obt:apoapsis > tgt_alt.
rcs off.
lock throttle to 0.0.
unlock steering.
wait 0.
}
local function do_orbit {
parameter hdg, burnout is false.
// power_down().
set warpmode to "physics".
set warp to 3.
wait until eta:apoapsis < 100.
set warp to 0.
wait until kuniverse:timewarp:issettled.
// power_up().
rcs on.
local target is heading(hdg, ship:obt:trueanomaly - 180, -hdg).
lock steering to target.
wait until eta:apoapsis < 80.
lock throttle to 1.0.
wait 3.
stage.
if burnout {
wait until engine():flameout.
} else {
wait until ship:velocity:orbit:mag > circular_vel().
}
// Fine-tune orbital speed w/ RCS fore/aft?
rcs off.
lock throttle to 0.0.
unlock steering.
wait 0.
}
local function circularize {
parameter target_ecc, max_vel_delta is 0.1.
power_down().
set warpmode to "physics".
set warp to 3.
wait until eta:apoapsis < 100.
set warp to 0.
wait until kuniverse:timewarp:issettled.
power_up().
rcs on.
lock steering to ship:prograde.
wait until eta:apoapsis < 80.
local second_burn is engine:ignition.
engine():shutdown().
lock throttle to 1.0.
wait 3.
engine():activate().
wait until ship:velocity:orbit:mag > circular_vel().
lock throttle to 0.0.
lock steering to lookdirup(ship:prograde:forevector, ship:up:forevector).
until false {
local threshold is 0.1.
local p is threshold / max_vel_delta.
local dv is circular_vel() - ship:velocity:orbit:mag.
local vert_vel is ship:verticalspeed * ship:up:forevector.
local vert_vel_d is 0.0 - vert_vel:mag.
local horz_vel_d is circular_vel() - (ship:velocity:orbit - vert_vel):mag.
local horz_factor is horz_vel_d * p.
local vert_factor is vert_vel_d * p.
if ship:obt:eccentricity < target_ecc or (abs(horz_factor) < threshold and abs(vert_factor) < threshold) {
set ship:control:fore to 0.
set ship:control:top to 0.
break.
}
set ship:control:fore to horz_factor.
set ship:control:top to vert_factor.
wait 0.
}
unlock steering.
rcs off.
wait 0.
}
local function burn {
lock steering to heading(hdg, 0, -hdg).
parameter hdg.
ignite().
wait 1.
rcs off.
set warpmode to "physics".
set warp to 3.
wait until ship:thrust = 0.
unlock steering.
unlock throttle.
set warp to 0.
wait until kuniverse:timewarp:issettled.
}
local function launch {
parameter hdg, target_alt.
local pitch_pid is pidloop(0.001, 0, 0, -90, 90, 1).
set pitch_pid:setpoint to target_alt.
local wanted_hdg is ship:facing.
lock steering to wanted_hdg.
ignite().
wait 1.
set warpmode to "physics".
set warp to 3.
until ship:thrust = 0 or ship:obt:periapsis > target_alt or eta:periapsis < eta:apoapsis {
local wanted_pitch is pitch_pid:update(time:seconds, ship:obt:apoapsis).
local neutral_pitch is ship:obt:trueanomaly / 2 - 90.
set wanted_hdg to heading(hdg, wanted_pitch, -hdg).
wait 0.
}
if ship:thrust <> 0 {
lock steering to ship:prograde.
wait until ship:thrust = 0.
}
unlock steering.
unlock throttle.
rcs off.
set warp to 0.
wait until kuniverse:timewarp:issettled.
}
local function power_down {
local avionics is ship:partsdubbed("able-avionics")[0].
local avi_mod is avionics:getmodule("ModuleProceduralAvionics").
avi_mod:doaction("shutdown avionics", true).
}
local function power_up {
local avionics is ship:partsdubbed("able-avionics")[0].
local avi_mod is avionics:getmodule("ModuleProceduralAvionics").
avi_mod:doaction("activate avionics", true).
}