Skip to content

Commit

Permalink
Merge pull request #3859 from pleroy/FlightPlan
Browse files Browse the repository at this point in the history
Support for collisions in the flight plan
  • Loading branch information
pleroy committed Jan 28, 2024
2 parents f831573 + d76b120 commit a6c5c21
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 32 deletions.
49 changes: 46 additions & 3 deletions ksp_plugin_adapter/ksp_plugin_adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2294,7 +2294,8 @@ select PartCentredForceHolder.
}
}

private TQP? RenderedCollision(string vessel_guid, CelestialBody centre) {
private TQP? RenderedPredictionCollision(string vessel_guid,
CelestialBody centre) {
var executor = plugin_.CollisionNewPredictionExecutor(
celestial_index: centre.flightGlobalsIndex,
sun_world_position: (XYZ)Planetarium.fetch.Sun.position,
Expand All @@ -2321,6 +2322,34 @@ select PartCentredForceHolder.
}
}

private TQP? RenderedFlightPlanCollision(string vessel_guid,
CelestialBody centre) {
var executor = plugin_.CollisionNewFlightPlanExecutor(
celestial_index: centre.flightGlobalsIndex,
sun_world_position: (XYZ)Planetarium.fetch.Sun.position,
// TODO(phl): This should be much larger, if it is limited at all.
max_points: MapNodePool.MaxNodesPerProvenance,
vessel_guid: vessel_guid);

for (;;) {
bool more = executor.CollisionGetLatitudeLongitude(
out double trial_latitude,
out double trial_longitude);
if (!more) {
if (plugin_.CollisionDeleteExecutor(ref executor, out TQP collision)) {
return collision;
} else {
return null;
}
}
executor.CollisionSetRadius(centre.TerrainAltitude(
trial_latitude,
trial_longitude,
allowNegative: !centre.ocean) +
centre.Radius);
}
}

private void RenderPredictionMarkers(string vessel_guid,
XYZ sun_world_position) {
if (plotting_frame_selector_.target_frame_selected &&
Expand Down Expand Up @@ -2360,7 +2389,7 @@ out DisposableIterator
var centre = plotting_frame_selector_.Centre();
var centre_index = centre.flightGlobalsIndex;
if (plotting_frame_selector_.IsSurfaceFrame()) {
TQP? collision = RenderedCollision(vessel_guid, centre);
TQP? collision = RenderedPredictionCollision(vessel_guid, centre);
if (collision.HasValue) {
map_node_pool_.RenderMarkers(new[] {collision.Value },
new MapNodePool.Provenance(
Expand Down Expand Up @@ -2451,7 +2480,21 @@ out DisposableIterator
plotting_frame_selector_);
} else {
if (plotting_frame_selector_.Centre() != null) {
var centre_index = plotting_frame_selector_.Centre().flightGlobalsIndex;
var centre = plotting_frame_selector_.Centre();
var centre_index = centre.flightGlobalsIndex;
if (plotting_frame_selector_.IsSurfaceFrame()) {
TQP? collision = RenderedFlightPlanCollision(vessel_guid, centre);
if (collision.HasValue) {
map_node_pool_.RenderMarkers(new[] {collision.Value },
new MapNodePool.Provenance(
vessel_guid,
MapNodePool.NodeSource.
FlightPlan,
MapObject.ObjectType.
PatchTransition),
plotting_frame_selector_);
}
}
plugin_.FlightPlanRenderedApsides(vessel_guid,
centre_index,
sun_world_position,
Expand Down
38 changes: 9 additions & 29 deletions ksp_plugin_adapter/map_node_pool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,6 @@ public class SingleProvenancePool {
time = marker.t,
associated_map_object = associated_map_object,
};
if (provenance.type == MapObject.ObjectType.Periapsis) {
var centre = reference_frame.Centre();
if (provenance.source == NodeSource.FlightPlan ||
!reference_frame.IsSurfaceFrame()) {
// TODO(phl): For the flight plan, preserve the old behaviour for now.
// Also, the terrain-based markers only make sense for the surface
// frame.
if (centre.GetAltitude(node_properties.world_position) < 0) {
node_properties.object_type = MapObject.ObjectType.PatchTransition;
node_properties.colour = XKCDColors.Orange;
}
}
}

if (pool.nodes_used == pool.nodes.Count) {
pool.nodes.Add(MakePoolNode());
Expand Down Expand Up @@ -314,25 +301,18 @@ public class SingleProvenancePool {
source,
celestial.GetAltitude(position).FormatN(0));
caption.captionLine1 = "";
// TODO(phl): Add support for the flight plan.
if (properties.source == NodeSource.Prediction) {
caption.captionLine2 = L10N.CacheFormat(
"#Principia_MapNode_ImpactCaptionLine2",
speed.FormatN(0));
} else {
caption.captionLine2 = "";
}
caption.captionLine2 = L10N.CacheFormat(
"#Principia_MapNode_ImpactCaptionLine2",
speed.FormatN(0));
break;
}
}
// TODO(phl): Add support for the flight plan.
if (properties.object_type != MapObject.ObjectType.PatchTransition ||
properties.source == NodeSource.Prediction) {
caption.captionLine1 =
"T" + new PrincipiaTimeSpan(
Planetarium.GetUniversalTime() - properties.time).
Format(with_leading_zeroes: false, with_seconds: true);
}
caption.captionLine1 = "T" +
new PrincipiaTimeSpan(
Planetarium.GetUniversalTime() -
properties.time).
Format(with_leading_zeroes: false,
with_seconds: true);
// The font used by map nodes does not support the minus sign, so we
// fall back to the hyphen-minus.
string minus = Culture.culture.NumberFormat.NegativeSign;
Expand Down

0 comments on commit a6c5c21

Please sign in to comment.