Skip to content

Commit dfc9858

Browse files
andymac-2Andrew Pritchard
andauthored
Allow targeting flags, landed vessels (#167)
Co-authored-by: Andrew Pritchard <[email protected]>
1 parent 6edeb6a commit dfc9858

File tree

4 files changed

+41
-57
lines changed

4 files changed

+41
-57
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Please always post your [KSP.log file](https://gist.github.com/JonnyOThan/04c207
99
### New Features
1010

1111
- Add support for scrolling through orbit patches on the MFD (thanks @andymac-2)
12+
- Allow targeting flags and landed vessels (thanks @andymac-2)
1213

1314
### Bug Fixes
1415

@@ -300,4 +301,4 @@ Many thanks to Manul and Zorkinian on the KSP forums for pinpointing the root ca
300301

301302
### Changes
302303

303-
- Updated for KSP 1.8.X
304+
- Updated for KSP 1.8.X

RasterPropMonitor/Core/RPMCEvaluators.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,21 +1714,21 @@ internal NumericVariableEvaluator GetNumericEvaluator(string input, out Variable
17141714
case "TIMETOANWITHTARGETSECS":
17151715
return (RPMVesselComputer comp) =>
17161716
{
1717-
if (comp.target == null || comp.targetOrbit == null)
1717+
if (comp.target == null)
17181718
return double.NaN;
1719-
return vessel.GetOrbit().TimeOfAscendingNode(comp.targetOrbit, Planetarium.GetUniversalTime()) - Planetarium.GetUniversalTime();
1719+
return vessel.GetOrbit().TimeOfAscendingNode(comp.target.GetOrbit(), Planetarium.GetUniversalTime()) - Planetarium.GetUniversalTime();
17201720
};
17211721
case "TIMETODNWITHTARGETSECS":
17221722
return (RPMVesselComputer comp) =>
17231723
{
1724-
if (comp.target == null || comp.targetOrbit == null)
1724+
if (comp.target == null)
17251725
return double.NaN;
1726-
return vessel.GetOrbit().TimeOfDescendingNode(comp.targetOrbit, Planetarium.GetUniversalTime()) - Planetarium.GetUniversalTime();
1726+
return vessel.GetOrbit().TimeOfDescendingNode(comp.target.GetOrbit(), Planetarium.GetUniversalTime()) - Planetarium.GetUniversalTime();
17271727
};
17281728
case "TARGETCLOSESTAPPROACHTIME":
17291729
return (RPMVesselComputer comp) =>
17301730
{
1731-
if (comp.target == null || comp.targetOrbit == null || orbitSensibility == false)
1731+
if (comp.target == null || orbitSensibility == false)
17321732
{
17331733
return double.NaN;
17341734
}
@@ -1742,7 +1742,7 @@ internal NumericVariableEvaluator GetNumericEvaluator(string input, out Variable
17421742
case "TARGETCLOSESTAPPROACHDISTANCE":
17431743
return (RPMVesselComputer comp) =>
17441744
{
1745-
if (comp.target == null || comp.targetOrbit == null || orbitSensibility == false)
1745+
if (comp.target == null || orbitSensibility == false)
17461746
{
17471747
return double.NaN;
17481748
}

RasterPropMonitor/Core/UtilityFunctions.cs

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,41 +1112,19 @@ public static string CurrentBiome(this Vessel thatVessel)
11121112
return "Space over " + thatVessel.mainBody.bodyName;
11131113
}
11141114

1115-
1116-
// Pseudo-orbit for closest approach to a landed object
1117-
public static Orbit OrbitFromSurfacePos(CelestialBody body, double lat, double lon, double alt, double UT)
1115+
public static Vector3d ClosestApproachSrfOrbit(Orbit vesselOrbit, Vessel target, out double UT, out double distance)
11181116
{
1119-
double t0 = Planetarium.GetUniversalTime();
1120-
double angle = body.rotates ? (UT - t0) * 360.0 / body.rotationPeriod : 0;
1121-
1122-
double LAN = (lon + body.rotationAngle + angle - 90.0) % 360.0;
1123-
Orbit orbit = new Orbit(lat, 0, body.Radius + alt, LAN, 90.0, 0, UT, body);
1117+
CelestialBody body = target.mainBody;
11241118

1125-
orbit.pos = orbit.getRelativePositionAtT(0);
1126-
if (body.rotates)
1127-
orbit.vel = Vector3d.Cross(body.zUpAngularVelocity, -orbit.pos);
1128-
else
1129-
orbit.vel = orbit.getOrbitalVelocityAtObT(Time.fixedDeltaTime);
1130-
orbit.h = Vector3d.Cross(orbit.pos, orbit.vel);
1131-
1132-
orbit.StartUT = t0;
1133-
orbit.EndUT = UT + orbit.period;
1134-
if (body.rotates)
1135-
orbit.period = body.rotationPeriod;
1136-
orbit.patchEndTransition = Orbit.PatchTransitionType.FINAL;
1137-
return orbit;
1138-
}
1119+
// longitude and latitude calculations are offset by a different amount every
1120+
// time we load the scene. We can use a zero latitude/longitude to find out what
1121+
// that offset is.
1122+
Vector3d zeroPos = body.GetRelSurfacePosition(0, 0, 0);
1123+
body.GetLatLonAltOrbital(zeroPos, out var zeroLat, out var zeroLon, out var _);
11391124

1140-
public static Orbit ClosestApproachSrfOrbit(Orbit vesselOrbit, Vessel target, out double UT, out double distance)
1141-
{
1142-
return ClosestApproachSrfOrbit(vesselOrbit, target.mainBody, target.latitude, target.longitude, target.altitude, out UT, out distance);
1143-
}
1144-
1145-
public static Orbit ClosestApproachSrfOrbit(Orbit vesselOrbit, CelestialBody body, double lat, double lon, double alt, out double UT, out double distance)
1146-
{
1147-
Vector3d pos = body.GetRelSurfacePosition(lat, lon, alt);
1125+
Vector3d pos = body.GetRelSurfacePosition(target.latitude - zeroLat, target.longitude - zeroLon, target.altitude);
11481126
distance = GetClosestApproach(vesselOrbit, body, pos, out UT);
1149-
return OrbitFromSurfacePos(body, lat, lon, alt, UT);
1127+
return pos;
11501128
}
11511129

11521130
public static double GetClosestApproach(Orbit vesselOrbit, ITargetable target, out double timeAtClosestApproach)
@@ -1171,7 +1149,7 @@ public static double GetClosestApproach(Orbit vesselOrbit, ITargetable target, o
11711149
if (targetVessel.LandedOrSplashed)
11721150
{
11731151
double closestApproach;
1174-
Orbit targetOrbit = JUtil.ClosestApproachSrfOrbit(vesselOrbit, targetVessel, out timeAtClosestApproach, out closestApproach);
1152+
ClosestApproachSrfOrbit(vesselOrbit, targetVessel, out timeAtClosestApproach, out closestApproach);
11751153
return closestApproach;
11761154
}
11771155
else

RasterPropMonitor/Handlers/JSIOrbitDisplay.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private static void DrawOrbitSegment(
186186
Vector3 midStraight = (startVertex + endVertex) * 0.5f;
187187
// Debug.Log($"startTA: {startTA}, endTA: {endTA}, startVertex: {startVertex}, endVertex: {endVertex}, midVertex: {midVertex}, midStraight: {midStraight}");
188188

189-
if (Math.Abs(startTA - endTA) < 0.01 || (midStraight - midVertex).sqrMagnitude < 9.0)
189+
if (Math.Abs(startTA - endTA) < 0.01 || (midStraight - midVertex).sqrMagnitude < 16.0)
190190
{
191191
GL.Vertex3(startVertex.x, startVertex.y, 0.0f);
192192
GL.Vertex3(midVertex.x, midVertex.y, 0.0f);
@@ -569,29 +569,31 @@ public bool RenderOrbit(RenderTexture screen, float cameraAspect)
569569
{
570570
var orbit = (targetVessel != null) ? targetVessel.GetOrbit() : targetBody.GetOrbit();
571571

572-
double tClosestApproach, dClosestApproach;
572+
double tClosestApproach;
573573

574574
if (targetVessel != null && targetVessel.LandedOrSplashed)
575575
{
576-
orbit = JUtil.ClosestApproachSrfOrbit(selectedPatch, targetVessel, out tClosestApproach, out dClosestApproach);
576+
Vector3d position = JUtil.ClosestApproachSrfOrbit(selectedPatch, targetVessel, out tClosestApproach, out double _);
577+
transformedPosition = screenTransform.MultiplyPoint3x4(position);
578+
DrawIcon(transformedPosition.x, transformedPosition.y, targetVessel.vesselType, iconColorTargetValue);
577579
}
578580
else
579581
{
580-
dClosestApproach = JUtil.GetClosestApproach(selectedPatch, orbit, out tClosestApproach);
582+
JUtil.GetClosestApproach(selectedPatch, orbit, out tClosestApproach);
581583

582584
DrawNextPe(orbit, selectedPatch.referenceBody, now, iconColorTargetValue, screenTransform);
583585
DrawNextAp(orbit, selectedPatch.referenceBody, now, iconColorTargetValue, screenTransform);
584-
}
585586

586-
if (targetBody != null)
587-
{
588-
transformedPosition = screenTransform.MultiplyPoint3x4(targetBody.getTruePositionAtUT(now) - selectedPatch.referenceBody.getTruePositionAtUT(now));
589-
DrawIcon(transformedPosition.x, transformedPosition.y, VesselType.Unknown, iconColorTargetValue, MapIcons.OtherIcon.PLANET);
590-
}
591-
else
592-
{
593-
transformedPosition = screenTransform.MultiplyPoint3x4(orbit.SwappedRelativePositionAtUT(now));
594-
DrawIcon(transformedPosition.x, transformedPosition.y, targetVessel.vesselType, iconColorTargetValue);
587+
if (targetBody != null)
588+
{
589+
transformedPosition = screenTransform.MultiplyPoint3x4(targetBody.getTruePositionAtUT(now) - selectedPatch.referenceBody.getTruePositionAtUT(now));
590+
DrawIcon(transformedPosition.x, transformedPosition.y, VesselType.Unknown, iconColorTargetValue, MapIcons.OtherIcon.PLANET);
591+
}
592+
else
593+
{
594+
transformedPosition = screenTransform.MultiplyPoint3x4(orbit.SwappedRelativePositionAtUT(now));
595+
DrawIcon(transformedPosition.x, transformedPosition.y, targetVessel.vesselType, iconColorTargetValue);
596+
}
595597
}
596598

597599
if (selectedPatch.AscendingNodeExists(orbit))
@@ -621,10 +623,13 @@ public bool RenderOrbit(RenderTexture screen, float cameraAspect)
621623
DrawIcon(transformedPosition.x, transformedPosition.y, VesselType.Unknown, iconColorClosestApproachValue, MapIcons.OtherIcon.SHIPATINTERCEPT);
622624
}
623625

624-
// Unconditionally try to draw the closest approach point on
625-
// the target orbit.
626-
transformedPosition = screenTransform.MultiplyPoint3x4(orbit.SwappedRelativePositionAtUT(tClosestApproach));
627-
DrawIcon(transformedPosition.x, transformedPosition.y, VesselType.Unknown, iconColorClosestApproachValue, MapIcons.OtherIcon.TGTATINTERCEPT);
626+
if (!targetVessel.LandedOrSplashed)
627+
{
628+
// Unconditionally try to draw the closest approach point on
629+
// the target orbit.
630+
transformedPosition = screenTransform.MultiplyPoint3x4(orbit.SwappedRelativePositionAtUT(tClosestApproach));
631+
DrawIcon(transformedPosition.x, transformedPosition.y, VesselType.Unknown, iconColorClosestApproachValue, MapIcons.OtherIcon.TGTATINTERCEPT);
632+
}
628633
}
629634
else
630635
{

0 commit comments

Comments
 (0)