Skip to content

Commit 0b16810

Browse files
committed
Time warp button
1 parent 973f899 commit 0b16810

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

GameData/PlanningNode/Localization/en-us.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Localization
4040
PlanningNode_NextBodyTooltip = Switch to the next celestial body
4141
PlanningNode_ShowForAllCheckboxCaption = Show for all vessels
4242
PlanningNode_ShowForAllCheckboxTooltip = If checked, display this node for all vessels, otherwise only for the current vessel
43+
PlanningNode_WarpToCaption = Warp
44+
PlanningNode_WarpToTooltip = Time warp to this planning node, allowing enough buffer time to set up your departure
4345

4446
PlanningNode_loadingTip1 = Rejecting your reality and substituting my own...
4547
PlanningNode_loadingTip2 = Striking that, reversing it...

GameData/PlanningNode/PlanningNode-Changelog.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ KERBALCHANGELOG
7070
change = Remove Sun from list of allowed start bodies
7171
type = Fix
7272
}
73+
CHANGE
74+
{
75+
change = Button to warp to a planning node with a buffer
76+
type = Add
77+
}
7378
}
7479
VERSION
7580
{

Source/PlanningNodeAddonBase.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ private void onAppLaunchToggleOff()
158158
}
159159
}
160160

161+
#endregion App launcher
162+
161163
private void editNode(PlanningNodeModel toEdit)
162164
{
163165
if (editor != null) {
@@ -193,6 +195,7 @@ private void openDialog(PlanningNodeModel toEdit)
193195
editDialog.PrevNode += () => editNode(PlanningNodesManager.Instance.PrevNode(renderer.vessel, editDialog.editingNode));
194196
editDialog.NextNode += () => editNode(PlanningNodesManager.Instance.NextNode(renderer.vessel, editDialog.editingNode));
195197
editDialog.BodyChanged += OnBodyChanged;
198+
editDialog.WarpTo += WarpTo;
196199
editDialog.Show(launcher.GetAnchor());
197200
} else {
198201
// Already open, just switch to this node
@@ -232,7 +235,41 @@ private void OnGameUnpause()
232235
}
233236
}
234237

235-
#endregion App launcher
238+
private void WarpTo(PlanningNodeModel node)
239+
{
240+
if (TimeWarp.CurrentRate > 1) {
241+
TimeWarp.fetch.CancelAutoWarp();
242+
TimeWarp.SetRate(0, false);
243+
} else {
244+
TimeWarp.fetch.WarpTo(node.burnTime - WarpBuffer(node.origin, (float?)renderer.vessel?.orbit.ApA ?? 100000f));
245+
}
246+
}
247+
248+
/// <summary>
249+
/// Calculate a good buffer for transferring from the given orbit
250+
/// </summary>
251+
/// <param name="parent">Body we're orbiting</param>
252+
/// <param name="apoapsis">Furthest distance from parent</param>
253+
/// <returns>
254+
/// One fourth the orbital period of a transfer orbit from your apoapsis to the edge of the sphere of influence;
255+
/// From LKO this is about 10d, and escaping to Duna takes 3d
256+
/// </returns>
257+
private static float WarpBuffer(CelestialBody parent, float apoapsis)
258+
{
259+
return 0.25f * OrbitalPeriod(parent, (float)parent.sphereOfInfluence, apoapsis);
260+
}
261+
262+
/// <returns>
263+
/// Period of an orbit with the given characteristics.
264+
/// </returns>
265+
/// <param name="parent">Body around which to orbit</param>
266+
/// <param name="apoapsis">Greatest distance from center of parent</param>
267+
/// <param name="periapsis">Smallest distance from center of parent</param>
268+
public static float OrbitalPeriod(CelestialBody parent, float apoapsis, float periapsis)
269+
{
270+
float r = 0.5f * (apoapsis + periapsis);
271+
return 2 * Mathf.PI * Mathf.Sqrt(r * r * r / (float)parent.gravParameter);
272+
}
236273

237274
/// <summary>
238275
/// Manages the drawing of the markers

Source/PlanningNodeEditDialog.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ public PlanningNodeEditDialog(PlanningNodeModel nodeToEdit, bool canEdit)
141141
tooltipText = "PlanningNode_ShowForAllCheckboxTooltip"
142142
}));
143143
}
144+
AddChild(TooltipExtensions.DeferTooltip(new DialogGUIButton(
145+
"PlanningNode_WarpToCaption",
146+
() => WarpTo?.Invoke(editingNode),
147+
buttonWidth, buttonHeight,
148+
false
149+
) {
150+
tooltipText = "PlanningNode_WarpToTooltip"
151+
}));
144152

145153
// Don't try to plot a maneuver from the Sun
146154
for (int i = 0; i < FlightGlobals.Bodies.Count; ++i) {
@@ -186,6 +194,11 @@ public PlanningNodeEditDialog(PlanningNodeModel nodeToEdit, bool canEdit)
186194
/// </summary>
187195
public event Action<CelestialBody> BodyChanged;
188196

197+
/// <summary>
198+
/// Function to call when the user clicks to warp to the current node
199+
/// </summary>
200+
public event Action<PlanningNodeModel> WarpTo;
201+
189202
/// <summary>
190203
/// Create a dialog and display it
191204
/// </summary>

0 commit comments

Comments
 (0)