@lazyglobal off. // Returns the throttle value you should use to achieve the // target TWR. If TWR can't be achieved, returns 1.0. (full throttle) function ThrottleToTWR { parameter twr is 1.5. local m is SHIP:MASS. local g is 9.81. if HAS_GRAV_SENSOR = true { set g to SHIP:SENSORS:GRAV:MAG. } return min((twr*m*g)/SHIP:AVAILABLETHRUST, 1.0). } // Calculates the ship's current TWR. function TWR { local m is ship:mass. local g is 9.81. // fallback to this approximation that doesn't deal with // altitude change. if HAS_GRAV_SENSOR = true { set g to SHIP:SENSORS:GRAV:MAG. } local t is THROTTLE * SHIP:AVAILABLETHRUST. return t/(m*g). } // Check for various sensors and set appropriate global constants. // Currently only checks for grav sensor, as that's the only one used by this library. global HAS_GRAV_SENSOR is false. function SensorCheck { local SensorList is 0. list SENSORS in SensorList. for s in SensorList { if s:type = "grav" { print "Gravometric sensor detected". set HAS_GRAV_SENSOR to true. return. } } set HAS_GRAV_SENSOR to false. } // adapted from https://github.com/sporadisk/KSP-KOS-scripts/blob/master/modules/GravityTurn.ks // todo: I don't think I actually like this code... function GravityTurn { parameter TargetDirection is 90. parameter TargetApoapsis is 80000. parameter Factor is 1. local remFactor is (2 / Factor). local remHeight is (SHIP:APOAPSIS/TargetApoapsis). local angle is (90 * (1 - (remHeight ^ remFactor))). return heading(TargetDirection, angle, 90). }