Files
ksp-kos-scripts/scripts/lander.ks
T

90 lines
2.3 KiB
Plaintext

function groundSlope {
parameter xOffset is 0, yOffset is 0.
local east is vCrs(north:vector, up:vector).
local center is (
ship:position +
(yOffset * north:vector) +
(xOffset * east)
).
local a is body:geopositionof(center + 5 * north:vector).
local b is body:geopositionof(center - 3 * north:vector + 4 * east).
local c is body:geopositionof(center - 3 * north:vector - 4 * east).
local aVec is a:altitudeposition(a:terrainheight).
local bVec is b:altitudeposition(b:terrainheight).
local cVec is c:altitudeposition(c:terrainheight).
local slope is vCrs(cVec - aVec, bVec - aVec):normalized.
// local centerPos is body:geopositionof(center).
// set slopeDraw to vecDraw(
// centerPos:altitudeposition(centerPos:terrainheight),
// slope,
// green,
// "",
// 10,
// true
// ).
return slope.
}
wait until not hasNode.
// Suicide burn calcs
set tval to 0.
lock throttle to tval.
lock steering to ship:srfretrograde.
lock h to ship:bounds:bottomaltradar.
lock v0 to ship:verticalspeed.
lock g to (body:mu / ((body:radius + ship:altitude)^2)).
lock a to ship:maxthrust / ship:mass.
lock d to (v0^2) / (2 * (a - g)).
// wait for burn
until h <= (1.1 * d) {
clearscreen.
print "Altitude AGL : " + round(h) at (4, 0).
print "Vertical Speed : " + round(v0, 1) at (4, 1).
print "Gravity : " + round(g, 2) at (4, 2).
print "Acceleration : " + round(a, 2) at (4, 3).
print "Distance : " + round(d, 2) at (4, 4).
wait 0.
}
unlock d.
unlock a.
set tval to 1.
set steeringLocked to false.
until h < 1 {
if v0 > -4 {
// equalize acceleration to stop downward velocity
set tval to max(0.001, ship:mass * g / ship:maxThrust).
}
else if v0 > -6 and not steeringLocked {
// slow enough that we just want to kill vertical velocity
lock steering to up.
set steeringLocked to true.
}
clearscreen.
print "Altitude AGL : " + round(h) at (4, 0).
print "Vertical Speed : " + round(v0, 1) at (4, 1).
print "Gravity : " + round(g, 2) at (4, 2).
print "Throttle : " + 100 * round(tval, 3) at (4, 3).
wait 0.
}
lock throttle to 0.
unlock throttle.
clearscreen.
print "Holding position".
lock steering to lookDirUp(groundSlope(), ship:facing:upvector).
wait until ship:angularvel:mag < 0.1.
wait 5.
print "Stable".