global function man_get_dv { parameter p_periapsis, p_apoapsis, p_altitude. local radius is ship:body:radius. local rs is radius + p_altitude. local rp is radius + p_periapsis. local ra is radius + p_apoapsis. local a is (ra + rp) / 2. return sqrt(ship:body:mu * (2/rs - 1/a)). } global function man_get_dv_hohmann { parameter p_altitude, p_target. local vi is man_get_dv(ship:orbit:periapsis, ship:orbit:apoapsis, p_altitude). local vf is 0. if p_altitude < p_target { set vf to man_get_dv(p_altitude, p_target, p_altitude). } else { set vf to man_get_dv(p_target, p_altitude, p_altitude). } return vf - vi. } global function man_node_circularize { parameter p_pos. local dv is 0. local tn is 0. if p_pos = "apo" { set dv to man_get_dv_hohmann(ship:orbit:apoapsis, ship:orbit:apoapsis). set tn to time:seconds + ship:orbit:eta:apoapsis. } else { set dv to man_get_dv_hohmann(ship:orbit:periapsis, ship:orbit:periapsis). set tn to time:seconds + ship:orbit:eta:periapsis. } add node(tn, 0, 0, dv). } global function man_node_exec { local nd is nextNode. lock steering to nd:burnvector. lock accel to ship:maxthrust / ship:mass. lock dur to nd:deltav:mag / accel. wait until nd:eta <= (dur / 2). lock throttle to 1. wait until dur < 0.5. lock steering to ship:facing. lock throttle to 0.25. local dv is nd:deltav:mag. until nd:deltav:mag > dv { set dv to nd:deltav:mag. } lock throttle to 0. unlock dur. unlock accel. remove nd. } global function man_launch { parameter p_direction, p_throttle. print "=> man_launch". lock throttle to p_throttle. lock steering to ship:facing. stage. wait 2. stage. wait until ship:altitude >= 150. print " - Alignment...". lock steering to heading(p_direction, 90). } global function man_gravity_turn { parameter p_direction, p_angle, p_velocity, p_apoapsis. print "=> man_gravity_turn". wait until ship:velocity:surface:mag >= p_velocity. print " - Rotation...". local gt_start is heading(p_direction, 90 - p_angle). lock steering to gt_start. wait until vang(ship:facing:vector, gt_start:vector) < 1. wait until vang(ship:srfprograde:vector, ship:facing:vector) < 1. print " - Follow prograde...". lock steering to heading(p_direction, 90 - vang(ship:up:vector, ship:srfprograde:vector)). wait until ship:orbit:apoapsis >= 0.95 * p_apoapsis. lock throttle to 0.3. wait until ship:orbit:apoapsis >= p_apoapsis. print " - Apoapsis OK". lock throttle to 0. wait 1. stage. wait 1. stage. }