-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib_ascent.ks
79 lines (67 loc) · 1.94 KB
/
lib_ascent.ks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@LAZYGLOBAL OFF.
FUNCTION ASCENT_Curve {
PARAMETER StartAlt.
PARAMETER EndAlt.
PARAMETER EndPitch.
PARAMETER Exponent. // 0.5 is a circle.
IF ship:altitude <= StartAlt {
RETURN 90.
}
IF ship:altitude >= EndAlt {
RETURN EndPitch.
}
RETURN MAX(0, MIN(90, 90 - ((Ship:Altitude - StartAlt) / (EndAlt - StartAlt))^Exponent * (90 - EndPitch))).
// LOCAL x IS ((ship:altitude)-(StartAlt))/(EndAlt-StartAlt). // X 0..1
// RETURN ((MAX(0, 1-(((ship:altitude)-(StartAlt))/(EndAlt-StartAlt))^2))^Exponent * (90-EndPitch)) + EndPitch.
}
FUNCTION ASCENT_LimitTWR {
PARAMETER TargetTWR.
LOCAL G IS body:mu / ((ship:altitude + body:radius)^2).
LOCAL MaxTWR IS ship:availablethrust / (G * ship:mass).
IF MaxTWR > 0 {
RETURN MIN(1, TargetTWR/MaxTWR). // values > 1 don't break KOS
}
RETURN 1.
}
FUNCTION ASCENT_TaperTWR {
PARAMETER TargetTWR.
PARAMETER FadeStartAlt.
PARAMETER FadeEndAlt.
IF Ship:Altitude > FadeEndAlt { RETURN 1. }
LOCAL T IS ASCENT_LimitTWR(TargetTWR).
IF Ship:Altitude < FadeStartAlt OR T >= 1 { Return T. }
RETURN T + ((1-T) * ((Ship:Altitude-FadeStartAlt)/(FadeEndAlt-FadeStartAlt))).
}
FUNCTION ASCENT_ApoapsisLimiter {
PARAMETER ap.
PARAMETER threshold IS 1000.
PARAMETER curve IS FALSE.
SET curve TO IIF(IsFalse(curve), threshold, curve).
LOCAL correcting IS FALSE.
FUNCTION delegate {
IF obt:apoapsis >= ap {
SET correcting TO FALSE.
RETURN 0.
}
IF obt:apoapsis < ap-threshold {
SET correcting TO TRUE.
RETURN 1.
}
IF NOT correcting {
RETURN 0.
}
RETURN MAX(0.05, MIN(1, 1-((ap - obt:apoapsis)/curve))).
}
RETURN delegate@.
}
FUNCTION ASCENT_WaitForApoapsis {
PARAMETER minalt.
PARAMETER ap.
PARAMETER threshold IS 1000.
PARAMETER curve IS FALSE.
SET curve TO IIF(IsFalse(curve), threshold, curve).
LOCAL lim IS ASCENT_ApoapsisLimiter(ap, threshold, curve).
LOCK THROTTLE TO lim().
WAIT UNTIL ship:altitude>minalt AND THROTTLE=0 AND obt:apoapsis+threshold>=ap.
LOCK THROTTLE TO 0.
}