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_get_duration { parameter p_dv, p_throttle. local total_thrust is 0. local total_isp is 0. local engs_count is 0. list engines in engs. for eng in engs { if eng:ignition = true and eng:flameout = false { set total_thrust to total_thrust + eng:availablethrust * p_throttle. set total_isp to total_isp + eng:isp. set engs_count to engs_count + 1. } } local f is total_thrust * 1000. local m is ship:mass * 1000. local e is constant():e. local p is total_isp / engs_count. local g is 9.80665. return g * m * p * (1 - e ^ (-p_dv / (g * p))) / f. } 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. } print "=> Planning circularization maneuver : " + round(dv, 1) + " m/s". add node(tn, 0, 0, dv). } global function man_node_exec { local nd is nextNode. print "=> Preparing maneuver". local dur is man_get_duration(nd:deltav:mag, 1). wait until nd:eta <= (dur / 2) + 60. lock steering to nd:burnvector. wait until nd:eta <= (dur / 2). print "=> Executing maneuver". local ti is time:seconds. local tf is ti + dur. lock throttle to 1. wait until tf - time:seconds < 0.25. lock steering to ship:facing. lock throttle to 0.1. local dv is nd:deltav:mag. until nd:deltav:mag > dv { set dv to nd:deltav:mag. } lock throttle to 0. remove nd. } global function man_launch { parameter p_direction, p_throttle. print "=> Launch". lock throttle to p_throttle. lock steering to ship:facing. stage. wait 2. stage. wait until ship:altitude >= 150. lock steering to heading(p_direction, 90). } global function man_gravity_turn { parameter p_direction, p_angle, p_velocity, p_apoapsis. print "=> Gravity Turn". wait until ship:velocity:surface:mag >= p_velocity. 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. 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. lock throttle to 0. wait 1. stage. wait 1. stage. }