Skip to content

Commit

Permalink
Time warp button
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Feb 18, 2021
1 parent 973f899 commit 0b16810
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 2 additions & 0 deletions GameData/PlanningNode/Localization/en-us.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Localization
PlanningNode_NextBodyTooltip = Switch to the next celestial body
PlanningNode_ShowForAllCheckboxCaption = Show for all vessels
PlanningNode_ShowForAllCheckboxTooltip = If checked, display this node for all vessels, otherwise only for the current vessel
PlanningNode_WarpToCaption = Warp
PlanningNode_WarpToTooltip = Time warp to this planning node, allowing enough buffer time to set up your departure

PlanningNode_loadingTip1 = Rejecting your reality and substituting my own...
PlanningNode_loadingTip2 = Striking that, reversing it...
Expand Down
5 changes: 5 additions & 0 deletions GameData/PlanningNode/PlanningNode-Changelog.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ KERBALCHANGELOG
change = Remove Sun from list of allowed start bodies
type = Fix
}
CHANGE
{
change = Button to warp to a planning node with a buffer
type = Add
}
}
VERSION
{
Expand Down
39 changes: 38 additions & 1 deletion Source/PlanningNodeAddonBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ private void onAppLaunchToggleOff()
}
}

#endregion App launcher

private void editNode(PlanningNodeModel toEdit)
{
if (editor != null) {
Expand Down Expand Up @@ -193,6 +195,7 @@ private void openDialog(PlanningNodeModel toEdit)
editDialog.PrevNode += () => editNode(PlanningNodesManager.Instance.PrevNode(renderer.vessel, editDialog.editingNode));
editDialog.NextNode += () => editNode(PlanningNodesManager.Instance.NextNode(renderer.vessel, editDialog.editingNode));
editDialog.BodyChanged += OnBodyChanged;
editDialog.WarpTo += WarpTo;
editDialog.Show(launcher.GetAnchor());
} else {
// Already open, just switch to this node
Expand Down Expand Up @@ -232,7 +235,41 @@ private void OnGameUnpause()
}
}

#endregion App launcher
private void WarpTo(PlanningNodeModel node)
{
if (TimeWarp.CurrentRate > 1) {
TimeWarp.fetch.CancelAutoWarp();
TimeWarp.SetRate(0, false);
} else {
TimeWarp.fetch.WarpTo(node.burnTime - WarpBuffer(node.origin, (float?)renderer.vessel?.orbit.ApA ?? 100000f));
}
}

/// <summary>
/// Calculate a good buffer for transferring from the given orbit
/// </summary>
/// <param name="parent">Body we're orbiting</param>
/// <param name="apoapsis">Furthest distance from parent</param>
/// <returns>
/// One fourth the orbital period of a transfer orbit from your apoapsis to the edge of the sphere of influence;
/// From LKO this is about 10d, and escaping to Duna takes 3d
/// </returns>
private static float WarpBuffer(CelestialBody parent, float apoapsis)
{
return 0.25f * OrbitalPeriod(parent, (float)parent.sphereOfInfluence, apoapsis);
}

/// <returns>
/// Period of an orbit with the given characteristics.
/// </returns>
/// <param name="parent">Body around which to orbit</param>
/// <param name="apoapsis">Greatest distance from center of parent</param>
/// <param name="periapsis">Smallest distance from center of parent</param>
public static float OrbitalPeriod(CelestialBody parent, float apoapsis, float periapsis)
{
float r = 0.5f * (apoapsis + periapsis);
return 2 * Mathf.PI * Mathf.Sqrt(r * r * r / (float)parent.gravParameter);
}

/// <summary>
/// Manages the drawing of the markers
Expand Down
13 changes: 13 additions & 0 deletions Source/PlanningNodeEditDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ public PlanningNodeEditDialog(PlanningNodeModel nodeToEdit, bool canEdit)
tooltipText = "PlanningNode_ShowForAllCheckboxTooltip"
}));
}
AddChild(TooltipExtensions.DeferTooltip(new DialogGUIButton(
"PlanningNode_WarpToCaption",
() => WarpTo?.Invoke(editingNode),
buttonWidth, buttonHeight,
false
) {
tooltipText = "PlanningNode_WarpToTooltip"
}));

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

/// <summary>
/// Function to call when the user clicks to warp to the current node
/// </summary>
public event Action<PlanningNodeModel> WarpTo;

/// <summary>
/// Create a dialog and display it
/// </summary>
Expand Down

0 comments on commit 0b16810

Please sign in to comment.