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

103 lines
3.2 KiB
Plaintext

@lazyglobal off.
global function steering_locked {
parameter epsilon is 0.1.
local rate_epsilon is epsilon * 0.1.
local angle_good is abs(steeringmanager:pitcherror) < epsilon and abs(steeringmanager:yawerror) < epsilon.
local rate_good is abs(ship:angularvel:x) < rate_epsilon and abs(ship:angularvel:y) < rate_epsilon.
return rate_good and angle_good.
}
global function launch_azimuth {
// from https://www.orbiterwiki.org/wiki/Launch_Azimuth
parameter target_inclination, target_alt.
local inertial_hdg is arcsin(cos(target_inclination) / cos(ship:latitude)).
local orbit_vel is sqrt(ship:body:mu / (target_alt + ship:body:radius)).
local body_vel is 2 * constant:pi * ship:body:radius / ship:body:rotationperiod.
local vxrot is orbit_vel * sin(inertial_hdg) - body_vel * cos(ship:latitude).
local vyrot is orbit_vel * cos(inertial_hdg).
return arctan(vxrot / vyrot).
}
global function ap_prograde {
local nd is node(time:seconds + eta:apoapsis, 0, 0, 1).
add(nd).
local target_hdg is rotatefromto(ship:prograde:forevector, nd:burnvector) * ship:prograde.
remove(nd).
return target_hdg.
}
global function obt_altitude {
if eta:apoapsis > eta:periapsis {
local since_ap is ship:obt:period - eta:apoapsis.
if since_ap < eta:periapsis {
return ship:obt:periapsis.
} else {
return ship:obt:apoapsis.
}
} else {
local since_pe is ship:obt:period - eta:periapsis.
if since_pe < eta:apoapsis {
ship:obt:apoapsis.
} else {
ship:obt:periapsis.
}
}
}
global function target_lan {
parameter target_lan, epsilon is 0.1.
local current_lan is mod(earth:rotationangle - ship:longitude - 90 + 360, 360).
local lan_diff is mod(current_lan - target_lan + 360, 360).
local target is time:now - 60 * (lan_diff / 4).
warpto(target - 10).
wait until time:now > target.
}
global function circular_vel {
parameter obt_alt is ship:altitude.
return sqrt(ship:body:mu / (obt_alt + ship:body:radius)).
}
global function orbital_vel {
parameter obt_alt.
return sqrt(ship:body:mu * (2 / (obt_alt + ship:body:radius) - 1 / ship:obt:semimajoraxis)).
}
global function circularization_dv {
return circular_vel(ship:obt:apoapsis) - orbital_vel(ship:obt:apoapsis).
}
global function burn_time {
parameter dv, engine.
local thrust is engine:possiblethrust.
local exhaust_vel is thrust / engine:maxmassflow.
local initial_mass is ship:mass.
return initial_mass * exhaust_vel * (1 - CONSTANT:E ^ (0 - dv / exhaust_vel)) / thrust.
}
global function hohmann_transfer_time {
parameter b, pe, ap.
local r1 is pe + b:radius.
local r2 is ap + b:radius.
return constant:pi * sqrt((r1+r2)*(r1+r2)*(r1+r2)/(8*b:mu)).
}
global function hohmann_transfer_dv {
parameter b, pe, ap.
local r1 is pe + b:radius.
local r2 is ap + b:radius.
return sqrt(b:mu/r1)*(sqrt(2*r2/(r1 + r2)) - 1).
}
global function predicted_ap {
local vel is ship:velocity:orbit:mag.
local rp is ship:altitude + ship:obt:body:radius.
local a is rp / 2 - ship:obt:body:mu/(vel*vel).
return (2*a - rp) - ship:obt:body:radius.
}