Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions RasterPropMonitor/Core/MonitorPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class MonitorPage
public readonly int pageNumber;
public readonly string name = string.Empty;
public readonly bool unlocker;
private const int INVALID_BUTTON = -1;
private string text;
private StringProcessorFormatter[] spf;
private string[] outputLines;
Expand Down Expand Up @@ -80,7 +81,8 @@ public enum BackgroundType
private readonly MonoBehaviour backgroundHandlerModule, pageHandlerModule;
private readonly List<string> techsRequired = new List<string>();
private readonly string fallbackPageName = string.Empty;

private readonly int buttonNextPatch = INVALID_BUTTON;
private readonly int buttonPrevPatch = INVALID_BUTTON;

private struct HandlerSupportMethods
{
Expand Down Expand Up @@ -433,6 +435,16 @@ public MonitorPage(int idNum, ConfigNode node, RasterPropMonitor thatMonitor)
}
}

int intValue = INVALID_BUTTON;
if (node.TryGetValue("buttonNextPatch", ref intValue))
{
buttonNextPatch = intValue;
}

if (node.TryGetValue("buttonPrevPatch", ref intValue))
{
buttonPrevPatch = intValue;
}
}

private static MethodInfo InstantiateHandler(ConfigNode node, RasterPropMonitor ourMonitor, out MonoBehaviour moduleInstance, out HandlerSupportMethods support)
Expand Down Expand Up @@ -622,7 +634,7 @@ public void Active(bool state)
public bool GlobalButtonClick(int buttonID)
{
buttonID = redirectGlobals[buttonID] ?? buttonID;
if (buttonID == -1)
if (buttonID == INVALID_BUTTON)
{
return false;
}
Expand All @@ -637,6 +649,16 @@ public bool GlobalButtonClick(int buttonID)
backgroundHandlerS.buttonClick(buttonID);
actionTaken = true;
}
if (buttonID == buttonNextPatch)
{
ourMonitor.SelectNextPatch();
actionTaken = true;
}
if (buttonID == buttonPrevPatch)
{
ourMonitor.SelectPreviousPatch();
actionTaken = true;
}
return actionTaken;
}

Expand Down
27 changes: 3 additions & 24 deletions RasterPropMonitor/Core/OrbitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,7 @@ public static Vector3d SwappedOrbitNormal(this Orbit o)
//occurs at a true anomaly that a does not actually ever attain
public static double TimeOfAscendingNode(this Orbit a, Orbit b, double UT)
{
if (a.eccentricity >= 1.0)
{
return UT;
}
else
{
return a.TimeOfTrueAnomaly(Orbit.AscendingNodeTrueAnomaly(a, b), UT);
}
return a.TimeOfTrueAnomaly(Orbit.AscendingNodeTrueAnomaly(a, b), UT);
}
//Returns the next time at which a will cross its descending node with b.
//For elliptical orbits this is a time between UT and UT + a.period.
Expand All @@ -62,14 +55,7 @@ public static double TimeOfAscendingNode(this Orbit a, Orbit b, double UT)
//occurs at a true anomaly that a does not actually ever attain
public static double TimeOfDescendingNode(this Orbit a, Orbit b, double UT)
{
if (a.eccentricity >= 1.0)
{
return UT;
}
else
{
return a.TimeOfTrueAnomaly(Orbit.DescendingNodeTrueAnomaly(a, b), UT);
}
return a.TimeOfTrueAnomaly(Orbit.DescendingNodeTrueAnomaly(a, b), UT);
}
//Returns the next time at which the orbiting object will cross the equator
//moving northward, if o is east-moving, or southward, if o is west-moving.
Expand All @@ -80,14 +66,7 @@ public static double TimeOfDescendingNode(this Orbit a, Orbit b, double UT)
//"ascending node" occurs at a true anomaly that o does not actually ever attain.
public static double TimeOfAscendingNodeEquatorial(this Orbit o, double UT)
{
if (o.eccentricity >= 1.0)
{
return UT;
}
else
{
return o.TimeOfTrueAnomaly(o.AscendingNodeEquatorialTrueAnomaly(), UT);
}
return o.TimeOfTrueAnomaly(o.AscendingNodeEquatorialTrueAnomaly(), UT);
}
//Returns the next time at which the orbiting object will cross the equator
//moving southward, if o is east-moving, or northward, if o is west-moving.
Expand Down
151 changes: 150 additions & 1 deletion RasterPropMonitor/Core/RPMCEvaluators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,22 @@ internal VariableEvaluator GetEvaluator(string input, out VariableUpdateType upd
{
return ScienceUtil.GetExperimentBiome(vessel.mainBody, vessel.latitude, vessel.longitude);
};

case "PATCHENCOUNTERBODY":
return (RPMVesselComputer comp) =>
{
if (!JUtil.OrbitMakesSense(vessel)) return string.Empty;
Orbit patch = GetSelectedPatchOrbit();
switch (patch.patchEndTransition)
{
case Orbit.PatchTransitionType.ENCOUNTER:
return patch.nextPatch.referenceBody.bodyName;
case Orbit.PatchTransitionType.ESCAPE:
return patch.referenceBody.bodyName;
}
return string.Empty;
};
case "PATCHORBITBODY":
return (RPMVesselComputer comp) => GetSelectedPatchOrbit().referenceBody.name;
}

return null;
Expand Down Expand Up @@ -1600,6 +1615,8 @@ internal NumericVariableEvaluator GetNumericEvaluator(string input, out Variable
}
return double.NaN;
};
case "ANYTARGETEXISTS":
return (RPMVesselComputer comp) => comp.target == null ? -1d : 1d;
case "TARGETEXISTS":
return (RPMVesselComputer comp) =>
{
Expand Down Expand Up @@ -2626,6 +2643,138 @@ internal NumericVariableEvaluator GetNumericEvaluator(string input, out Variable
}
return -1d;
};
case "PATCHINDEX":
return (RPMVesselComputer comp) =>
{
(int index, Orbit _) = GetSelectedPatch();
return index + 1;
};
case "PATCHCOUNT":
return (RPMVesselComputer comp) =>
{
(int index, Orbit _) = GetLastPatch();
return index + 1;
};
case "PATCHALTITUDE":
return (RPMVesselComputer comp) => GetSelectedPatchOrbit().referenceBody.GetAltitude(vessel.CoM);
case "PATCHORBTSPEED":
return (RPMVesselComputer comp) => GetSelectedPatchOrbit().GetVel().magnitude;
case "PATCHAPOAPSIS":
return (RPMVesselComputer comp) => JUtil.OrbitMakesSense(vessel) ? GetSelectedPatchOrbit().ApA : double.NaN;
case "PATCHPERIAPSIS":
return (RPMVesselComputer comp) => JUtil.OrbitMakesSense(vessel) ? GetSelectedPatchOrbit().PeA : double.NaN;
case "PATCHINCLINATION":
return (RPMVesselComputer comp) => JUtil.OrbitMakesSense(vessel) ? GetSelectedPatchOrbit().inclination : double.NaN;
case "PATCHECCENTRICITY":
return (RPMVesselComputer comp) => JUtil.OrbitMakesSense(vessel) ? GetSelectedPatchOrbit().eccentricity : double.NaN;
case "PATCHTIMETOAPSECS":
return (RPMVesselComputer comp) =>
{
if (!JUtil.OrbitMakesSense(vessel)) return double.NaN;
Orbit patch = GetSelectedPatchOrbit();
// When the tracking station is not upgraded, StartUT will not be updated to the current time.
if (patch.StartUT == 0.0) return patch.timeToAp;
return patch.timeToAp + patch.StartUT - Planetarium.GetUniversalTime();
};
case "PATCHTIMETOPESECS":
return (RPMVesselComputer comp) =>
{
if (!JUtil.OrbitMakesSense(vessel)) return double.NaN;
Orbit patch = GetSelectedPatchOrbit();
// When the tracking station is not upgraded, StartUT will not be updated to the current time.
if (patch.StartUT == 0.0) return patch.timeToPe;
return patch.timeToPe + patch.StartUT - Planetarium.GetUniversalTime();
};
case "PATCHORBPERIODSECS":
return (RPMVesselComputer comp) => JUtil.OrbitMakesSense(vessel) ? GetSelectedPatchOrbit().period : double.NaN;
case "PATCHTIMETOANEQUATORIAL":
return (RPMVesselComputer comp) =>
{
Orbit patch = GetSelectedPatchOrbit();
if (!JUtil.OrbitMakesSense(vessel) || !patch.AscendingNodeEquatorialExists())
{
return double.NaN;
}
return patch.TimeOfAscendingNodeEquatorial(Planetarium.GetUniversalTime()) - Planetarium.GetUniversalTime();
};
case "PATCHTIMETODNEQUATORIAL":
return (RPMVesselComputer comp) =>
{
Orbit patch = GetSelectedPatchOrbit();
if (!JUtil.OrbitMakesSense(vessel) || !patch.DescendingNodeEquatorialExists())
{
return double.NaN;
}
return patch.TimeOfDescendingNodeEquatorial(Planetarium.GetUniversalTime()) - Planetarium.GetUniversalTime();
};
case "PATCHFIRST":
return (RPMVesselComputer comp) =>
{
(int index, Orbit _) = GetSelectedPatch();
return index == 0 ? 1.0d : 0.0d;
};
case "PATCHLAST":
return (RPMVesselComputer comp) =>
{
(int selected, Orbit _) = GetSelectedPatch();
(int last, Orbit _) = GetLastPatch();
return selected == last ? 1.0d : 0.0d;
};
case "PATCHNEXTAPSISTYPE":
return (RPMVesselComputer comp) =>
{
Orbit patch = GetSelectedPatchOrbit();
if (patch.eccentricity < 1.0)
{
// Which one will we reach first?
return (patch.timeToPe < patch.timeToAp) ? -1.0 : 1.0;
}

// Ship is hyperbolic. There is no Ap. Have we already
// passed Pe?
return (patch.timeToPe > 0.0) ? -1.0 : 0.0;
};
case "PATCHNEXT_ANDN_EQUATORIAL":
return (RPMVesselComputer comp) =>
{
Orbit patch = GetSelectedPatchOrbit();
double universalTime = Planetarium.GetUniversalTime();
if (!JUtil.OrbitMakesSense(vessel)) return 0.0;

double dnTime = patch.DescendingNodeEquatorialExists() ? patch.TimeOfDescendingNodeEquatorial(universalTime) : double.NaN;
double anTime = patch.AscendingNodeEquatorialExists() ? patch.TimeOfAscendingNodeEquatorial(universalTime) : double.NaN;

if (double.IsNaN(anTime)) return -1.0;
if (double.IsNaN(dnTime)) return 1.0;
return Math.Max(0.0, anTime) < Math.Max(dnTime, 0.0) ? 1.0 : -1.0;
};
case "PATCHENCOUNTEREXISTS":
return (RPMVesselComputer comp) =>
{
if (!JUtil.OrbitMakesSense(vessel)) return 0.0;
Orbit patch = GetSelectedPatchOrbit();
switch (patch.patchEndTransition)
{
case Orbit.PatchTransitionType.ESCAPE:
return -1d;
case Orbit.PatchTransitionType.ENCOUNTER:
return 1d;
default:
return 0.0d;
}
};
case "PATCHENCOUNTERTIME":
return (RPMVesselComputer comp) =>
{
if (!JUtil.OrbitMakesSense(vessel)) return 0.0;
Orbit patch = GetSelectedPatchOrbit();
if (patch.patchEndTransition == Orbit.PatchTransitionType.ENCOUNTER ||
patch.patchEndTransition == Orbit.PatchTransitionType.ESCAPE)
{
return patch.UTsoi - Planetarium.GetUniversalTime();
}
return 0.0;
};
}

return null;
Expand Down
14 changes: 12 additions & 2 deletions RasterPropMonitor/Core/RasterPropMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ public void PageButtonClick(MonitorPage triggeredPage)
}
}

internal void SelectNextPatch()
{
rpmComp.SelectNextPatch();
}

internal void SelectPreviousPatch()
{
rpmComp.SelectPreviousPatch();
}

// Update according to the given refresh rate.
private bool UpdateCheck()
{
Expand Down Expand Up @@ -508,8 +518,8 @@ public void LateUpdate()
firstRenderComplete = false;
textRefreshRequired = true;
}
else if (!activePage.isMutable)
{
else if (!activePage.isMutable)
{
// In case the page is empty and has no camera, the screen is treated as turned off and blanked once.
if (!firstRenderComplete)
{
Expand Down
66 changes: 65 additions & 1 deletion RasterPropMonitor/Core/RasterPropMonitorComputer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
// Data refresh
private int dataUpdateCountdown;
private int refreshDataRate = 60;
private bool timeToUpdate = false;

Check warning on line 97 in RasterPropMonitor/Core/RasterPropMonitorComputer.cs

View workflow job for this annotation

GitHub Actions / build / build

The field 'RasterPropMonitorComputer.timeToUpdate' is assigned but its value is never used

// Diagnostics
private int debug_fixedUpdates = 0;
Expand All @@ -111,6 +111,7 @@

private ExternalVariableHandlers plugins = null;
internal Dictionary<string, Color32> overrideColors = new Dictionary<string, Color32>();
private int selectedPatchIndex;

static readonly Regex x_agmemoRegex = new Regex("^AG([0-9])\\s*=\\s*(.*)\\s*");

Expand Down Expand Up @@ -151,7 +152,7 @@
{
var vc = InstantiateVariableOrNumber(variableName);
vc.onChangeCallbacks += cb;
cb((float)vc.numericValue);
cb(vc.AsFloat());
}

/// <summary>
Expand Down Expand Up @@ -332,6 +333,69 @@
modulesToRestore.Add(module);
}

/// <summary>
/// Find the selected orbital patch. A patch is selected if we are
/// looking at it.
/// </summary>
/// <returns>
/// 1. The count of the patch. 0 for current orbit, 1 for next SOI, and
/// so on
/// 2. The orbit object that represents the patch.
/// </returns>
internal (int, Orbit) GetSelectedPatch()
{
return EffectivePatch(selectedPatchIndex);
}

private Orbit GetSelectedPatchOrbit()
{
(int _, Orbit patch) = GetSelectedPatch();
return patch;
}

internal (int, Orbit) GetLastPatch()
{
return EffectivePatch(1000);
}

internal void SelectNextPatch()
{
(int effectivePatchIndex, _) = GetSelectedPatch();
SelectPatch(effectivePatchIndex + 1);
}

internal void SelectPreviousPatch()
{
(int effectivePatchIndex, _) = GetSelectedPatch();
SelectPatch(effectivePatchIndex - 1);
}

private void SelectPatch(int patchIndex)
{
(int effectivePatchIndex, _) = EffectivePatch(patchIndex);
selectedPatchIndex = effectivePatchIndex;
}

/// <summary>
/// Returns the orbit (patch) and orbit index given a selection.
/// </summary>
/// <returns>true if it's time to update things</returns>
private (int, Orbit) EffectivePatch(int patchIndex)
{
Orbit patch = vessel.orbit;
int effectivePatchIndex = 0;
while (effectivePatchIndex < patchIndex
&& patch.nextPatch != null
&& patch.nextPatch.activePatch
&& (patch.patchEndTransition == Orbit.PatchTransitionType.ENCOUNTER || patch.patchEndTransition == Orbit.PatchTransitionType.ESCAPE))
{
patch = patch.nextPatch;
effectivePatchIndex++;
}

return (effectivePatchIndex, patch);
}

#region Monobehaviour
/// <summary>
/// Configure this computer for operation.
Expand Down
Loading
Loading