From 7a27df3f7509897f7372e174a60c728b7a703937 Mon Sep 17 00:00:00 2001 From: pleroy Date: Sun, 15 Oct 2023 10:24:58 +0200 Subject: [PATCH 1/9] Interface changes and first UI changes. --- ksp_plugin/interface_flight_plan.cpp | 33 ++++++---- ksp_plugin_adapter/burn_editor.cs | 5 +- ksp_plugin_adapter/flight_planner.cs | 98 +++++++++++++++++++--------- serialization/journal.proto | 61 +++++++++-------- 4 files changed, 118 insertions(+), 79 deletions(-) diff --git a/ksp_plugin/interface_flight_plan.cpp b/ksp_plugin/interface_flight_plan.cpp index 8c81931f18..d655438b77 100644 --- a/ksp_plugin/interface_flight_plan.cpp +++ b/ksp_plugin/interface_flight_plan.cpp @@ -595,7 +595,7 @@ void __cdecl principia__FlightPlanOptimizeManoeuvre( int const manœuvre_index, int const celestial_index, double const distance, - double const inclination_in_degrees, + double const* const inclination_in_degrees, NavigationFrameParameters const navigation_frame_parameters) { journal::Method m( {plugin, @@ -613,20 +613,25 @@ void __cdecl principia__FlightPlanOptimizeManoeuvre( old_driver->Interrupt(); } } + + std::vector factories = { + FlightPlanOptimizer::ForCelestialDistance( + /*celestial=*/&plugin->GetCelestial(celestial_index), + /*target_distance=*/distance * Metre), + FlightPlanOptimizer::ForΔv()}; + std::vector weights = {1, 1e3}; + if (inclination_in_degrees != nullptr) { + factories.push_back(FlightPlanOptimizer::ForInclination( + &plugin->GetCelestial(celestial_index), + [plugin, navigation_frame_parameters]() { + return NewNavigationFrame(*plugin, navigation_frame_parameters); + }, + *inclination_in_degrees * Degree)); + weights.push_back(1e6); + } + vessel.MakeFlightPlanOptimizationDriver( - FlightPlanOptimizer::LinearCombination( - {FlightPlanOptimizer::ForCelestialDistance( - /*celestial=*/&plugin->GetCelestial(celestial_index), - /*target_distance=*/distance * Metre), - FlightPlanOptimizer::ForInclination( - &plugin->GetCelestial(celestial_index), - [plugin, navigation_frame_parameters]() { - return NewNavigationFrame(*plugin, - navigation_frame_parameters); - }, - inclination_in_degrees * Degree), - FlightPlanOptimizer::ForΔv()}, - {1, 1e6, 1e3})); + FlightPlanOptimizer::LinearCombination(factories, weights)); const FlightPlanOptimizationDriver::Parameters parameters{ .index = manœuvre_index, diff --git a/ksp_plugin_adapter/burn_editor.cs b/ksp_plugin_adapter/burn_editor.cs index ffd0063dbd..500845faf4 100644 --- a/ksp_plugin_adapter/burn_editor.cs +++ b/ksp_plugin_adapter/burn_editor.cs @@ -245,8 +245,9 @@ public Event Render( index == 0 ? L10N.CacheFormat( "#Principia_BurnEditor_TimeBase_StartOfFlightPlan") - : L10N.CacheFormat("#Principia_BurnEditor_TimeBase_EndOfManœuvre", - index), + : L10N.CacheFormat( + "#Principia_BurnEditor_TimeBase_EndOfManœuvre", + index), style : new UnityEngine.GUIStyle(UnityEngine.GUI.skin.label){ alignment = UnityEngine.TextAnchor.UpperLeft }); diff --git a/ksp_plugin_adapter/flight_planner.cs b/ksp_plugin_adapter/flight_planner.cs index 204a69643c..28ae214ba3 100644 --- a/ksp_plugin_adapter/flight_planner.cs +++ b/ksp_plugin_adapter/flight_planner.cs @@ -379,37 +379,67 @@ private void RenderFlightPlan(string vessel_guid) { } } - if (burn_editors_.Count > 0) { - RenderUpcomingEvents(); - } + Style.HorizontalLine(); + using (new UnityEngine.GUILayout.VerticalScope()) { + UnityEngine.GUILayout.TextArea( + L10N.CacheFormat("#Principia_FlightPlan_Optimization")); + + using (new UnityEngine.GUILayout.HorizontalScope()) { + UnityEngine.GUILayout.Label( + L10N.CacheFormat("#Principia_FlightPlan_TargetAltitude")); + string text = UnityEngine.GUILayout.TextField( + optimization_altitude_.FormatN(0), + GUILayoutWidth(3)); + UnityEngine.GUILayout.Label( + text : L10N.CacheFormat("#Principia_FlightPlan_LengthUnit"), + options : GUILayoutWidth(2)); + if (double.TryParse(text, + System.Globalization.NumberStyles.Any, + Culture.culture, + out double candidate)) { + if (candidate >= 0 && candidate < double.PositiveInfinity) { + optimization_altitude_ = candidate; + } + } + } - if (requested_editor_focus_index_ is int requested_focus) { - requested_editor_focus_index_ = null; - for (int i = 0; i < burn_editors_.Count; ++i) { - burn_editors_[i].minimized = requested_focus != i; + using (new UnityEngine.GUILayout.HorizontalScope()) { + UnityEngine.GUILayout.Label( + L10N.CacheFormat("#Principia_FlightPlan_TargetInclination")); + bool optimize_inclination = UnityEngine.GUILayout.Toggle( + optimization_inclination_in_degrees_.HasValue, + L10N.CacheFormat("#Principia_FlightPlan_OptimizeInclination")); + string text = UnityEngine.GUILayout.TextField( + optimize_inclination + ? optimization_inclination_in_degrees_.Value.FormatN(0) + : "", + GUILayoutWidth(3)); + UnityEngine.GUILayout.Label( + text: L10N.CacheFormat("#Principia_FlightPlan_AngleUnit"), + options: GUILayoutWidth(2)); + if (!optimize_inclination) { + optimization_inclination_in_degrees_ = null; + } else if (double.TryParse(text, + System.Globalization.NumberStyles.Any, + Culture.culture, + out double candidate)) { + optimization_inclination_in_degrees_ = + Math.Max(Math.Min(180, candidate), -180); + } + } } - ScheduleShrink(); - } - using (new UnityEngine.GUILayout.HorizontalScope()) { - UnityEngine.GUILayout.Label("Target flyby altitude (m):"); - string text = UnityEngine.GUILayout.TextField(optimization_altitude.FormatN(0), GUILayoutWidth(3)); - if (double.TryParse(text, System.Globalization.NumberStyles.Any, Culture.culture, out double candidate)) { - if (candidate >= 0 && candidate < double.PositiveInfinity) { - optimization_altitude = candidate; - } + if (burn_editors_.Count > 0) { + RenderUpcomingEvents(); } - } - using (new UnityEngine.GUILayout.HorizontalScope()) { - UnityEngine.GUILayout.Label("Target flyby inclination wrt plotting reference plane (°):"); - string text = UnityEngine.GUILayout.TextField(optimization_inclination_in_degrees.FormatN(0), GUILayoutWidth(3)); - if (double.TryParse(text, System.Globalization.NumberStyles.Any, Culture.culture, out double candidate)) { - if (candidate >= -180 && candidate <= 180) { - optimization_inclination_in_degrees = candidate; + if (requested_editor_focus_index_ is int requested_focus) { + requested_editor_focus_index_ = null; + for (int i = 0; i < burn_editors_.Count; ++i) { + burn_editors_[i].minimized = requested_focus != i; } + ScheduleShrink(); } - } // Compute the final times for each manœuvre before displaying them. var final_times = new List(); @@ -426,16 +456,20 @@ private void RenderFlightPlan(string vessel_guid) { return; } Style.HorizontalLine(); - if (adapter_.plotting_frame_selector_.Centre() is CelestialBody centre) { + if (adapter_.plotting_frame_selector_. + Centre() is CelestialBody centre) { if (plugin.FlightPlanOptimizationInProgress(vessel_guid)) { UnityEngine.GUILayout.Button("Optimizing…"); - } else if (UnityEngine.GUILayout.Button($"Optimize {centre.Name()} flyby")) { + } else if (UnityEngine.GUILayout.Button( + $"Optimize {centre.Name()} flyby")) { plugin.FlightPlanOptimizeManoeuvre( - vessel_guid, i, + vessel_guid, + i, centre.flightGlobalsIndex, - centre.Radius + optimization_altitude, - optimization_inclination_in_degrees, - (NavigationFrameParameters)adapter_.plotting_frame_selector_.FrameParameters()); + centre.Radius + optimization_altitude_, + optimization_inclination_in_degrees_, + (NavigationFrameParameters)adapter_.plotting_frame_selector_. + FrameParameters()); } } else { UnityEngine.GUILayout.Button("Change plotting frame to optimize"); @@ -843,8 +877,8 @@ private void UpdateAdaptiveStepParameters( private const int max_flight_plans = 10; - private double optimization_altitude = 10e3; - private double optimization_inclination_in_degrees = 0; + private double optimization_altitude_ = 10e3; + private double? optimization_inclination_in_degrees_ = 0; } } // namespace ksp_plugin_adapter diff --git a/serialization/journal.proto b/serialization/journal.proto index 5d45f621f8..363ab6315d 100644 --- a/serialization/journal.proto +++ b/serialization/journal.proto @@ -982,27 +982,25 @@ message FlightPlanInsert { optional Return return = 3; } -message FlightPlanOptimizeManoeuvre { +message FlightPlanNumberOfAnomalousManoeuvres { extend Method { - optional FlightPlanOptimizeManoeuvre extension = 5189; + optional FlightPlanNumberOfAnomalousManoeuvres extension = 5159; } message In { required fixed64 plugin = 1 [(pointer_to) = "Plugin const", (is_subject) = true]; required string vessel_guid = 2; - required int32 manoeuvre_index = 3; - required int32 celestial_index = 4; - required double distance = 5; - required double inclination_in_degrees = 6; - required NavigationFrameParameters navigation_frame_parameters = 7; + } + message Return { + required int32 result = 1; } optional In in = 1; + optional Return return = 3; } - -message FlightPlanNumberOfAnomalousManoeuvres { +message FlightPlanNumberOfManoeuvres { extend Method { - optional FlightPlanNumberOfAnomalousManoeuvres extension = 5159; + optional FlightPlanNumberOfManoeuvres extension = 5038; } message In { required fixed64 plugin = 1 [(pointer_to) = "Plugin const", @@ -1016,9 +1014,9 @@ message FlightPlanNumberOfAnomalousManoeuvres { optional Return return = 3; } -message FlightPlanNumberOfManoeuvres { +message FlightPlanNumberOfSegments { extend Method { - optional FlightPlanNumberOfManoeuvres extension = 5038; + optional FlightPlanNumberOfSegments extension = 5070; } message In { required fixed64 plugin = 1 [(pointer_to) = "Plugin const", @@ -1032,9 +1030,9 @@ message FlightPlanNumberOfManoeuvres { optional Return return = 3; } -message FlightPlanNumberOfSegments { +message FlightPlanOptimizationInProgress { extend Method { - optional FlightPlanNumberOfSegments extension = 5070; + optional FlightPlanOptimizationInProgress extension = 5188; } message In { required fixed64 plugin = 1 [(pointer_to) = "Plugin const", @@ -1042,12 +1040,29 @@ message FlightPlanNumberOfSegments { required string vessel_guid = 2; } message Return { - required int32 result = 1; + required bool result = 1; } optional In in = 1; optional Return return = 3; } +message FlightPlanOptimizeManoeuvre { + extend Method { + optional FlightPlanOptimizeManoeuvre extension = 5189; + } + message In { + required fixed64 plugin = 1 [(pointer_to) = "Plugin const", + (is_subject) = true]; + required string vessel_guid = 2; + required int32 manoeuvre_index = 3; + required int32 celestial_index = 4; + required double distance = 5; + optional double inclination_in_degrees = 6; + required NavigationFrameParameters navigation_frame_parameters = 7; + } + optional In in = 1; +} + message FlightPlanRebase { extend Method { optional FlightPlanRebase extension = 5173; @@ -1220,22 +1235,6 @@ message FlightPlanSelected { optional Return return = 3; } -message FlightPlanOptimizationInProgress { - extend Method { - optional FlightPlanOptimizationInProgress extension = 5188; - } - message In { - required fixed64 plugin = 1 [(pointer_to) = "Plugin const", - (is_subject) = true]; - required string vessel_guid = 2; - } - message Return { - required bool result = 1; - } - optional In in = 1; - optional Return return = 3; -} - message FlightPlanSetAdaptiveStepParameters { extend Method { optional FlightPlanSetAdaptiveStepParameters extension = 5080; From bfdd359806b79c34a3d4b9ca6a2c3d20e028f90b Mon Sep 17 00:00:00 2001 From: pleroy Date: Sun, 15 Oct 2023 11:10:43 +0200 Subject: [PATCH 2/9] Localization and start looking at the burn. --- ksp_plugin_adapter/burn_editor.cs | 20 +++ ksp_plugin_adapter/flight_planner.cs | 144 +++++++++++++--------- ksp_plugin_adapter/localization/en-us.cfg | 8 ++ 3 files changed, 112 insertions(+), 60 deletions(-) diff --git a/ksp_plugin_adapter/burn_editor.cs b/ksp_plugin_adapter/burn_editor.cs index 500845faf4..9a9ec7854d 100644 --- a/ksp_plugin_adapter/burn_editor.cs +++ b/ksp_plugin_adapter/burn_editor.cs @@ -260,6 +260,26 @@ public Event Render( UnityEngine.GUILayout.Label(L10N.CacheFormat( "#Principia_BurnEditor_Duration", duration_.ToString("0.0"))); + if (adapter_.plotting_frame_selector_. + Centre() is CelestialBody centre) { + if (plugin.FlightPlanOptimizationInProgress(vessel_.id.ToString())) { + UnityEngine.GUILayout.Button("Optimizing…"); + } else if (UnityEngine.GUILayout.Button( + "#Principia_BurnEditor_Optimize")) { + plugin.FlightPlanOptimizeManoeuvre(vessel_.id.ToString(), + index, + centre.flightGlobalsIndex, + centre.Radius + + optimization_altitude_, + optimization_inclination_in_degrees_, + (NavigationFrameParameters) + adapter_. + plotting_frame_selector_. + FrameParameters()); + } + } else { + UnityEngine.GUILayout.Button("Change plotting frame to optimize"); + } } UnityEngine.GUILayout.Label(engine_warning_, Style.Warning(UnityEngine.GUI.skin.label)); diff --git a/ksp_plugin_adapter/flight_planner.cs b/ksp_plugin_adapter/flight_planner.cs index 28ae214ba3..7c4b0c51f8 100644 --- a/ksp_plugin_adapter/flight_planner.cs +++ b/ksp_plugin_adapter/flight_planner.cs @@ -1,4 +1,5 @@ -using System; +using Steamworks; +using System; using System.Collections.Generic; using System.Linq; @@ -380,51 +381,74 @@ private void RenderFlightPlan(string vessel_guid) { } Style.HorizontalLine(); - using (new UnityEngine.GUILayout.VerticalScope()) { - UnityEngine.GUILayout.TextArea( - L10N.CacheFormat("#Principia_FlightPlan_Optimization")); - + // There is no Layout/Repaint trouble here because the frame is selected + // in another window. + if (adapter_.plotting_frame_selector_. + Centre() is CelestialBody centre) { using (new UnityEngine.GUILayout.HorizontalScope()) { + ////valign UnityEngine.GUILayout.Label( - L10N.CacheFormat("#Principia_FlightPlan_TargetAltitude")); - string text = UnityEngine.GUILayout.TextField( - optimization_altitude_.FormatN(0), - GUILayoutWidth(3)); - UnityEngine.GUILayout.Label( - text : L10N.CacheFormat("#Principia_FlightPlan_LengthUnit"), - options : GUILayoutWidth(2)); - if (double.TryParse(text, - System.Globalization.NumberStyles.Any, - Culture.culture, - out double candidate)) { - if (candidate >= 0 && candidate < double.PositiveInfinity) { - optimization_altitude_ = candidate; + L10N.CelestialString("#Principia_FlightPlan_Optimization", + new[]{centre}), + style : Style.Aligned(UnityEngine.TextAnchor.LowerLeft, + UnityEngine.GUI.skin.label)); + using (new UnityEngine.GUILayout.VerticalScope()) { + using (new UnityEngine.GUILayout.HorizontalScope()) { + UnityEngine.GUILayout.Label( + L10N.CacheFormat("#Principia_FlightPlan_TargetAltitude"), + style : Style.RightAligned(UnityEngine.GUI.skin.label)); + string text = UnityEngine.GUILayout.TextField( + optimization_altitude_.FormatN(0), + GUILayoutWidth(3)); + UnityEngine.GUILayout.Label( + text : L10N.CacheFormat( + "#Principia_FlightPlan_AltitudeUnit"), + options : GUILayoutWidth(2)); + if (double.TryParse(text, + System.Globalization.NumberStyles.Any, + Culture.culture, + out double candidate)) { + if (candidate >= 0 && candidate < double.PositiveInfinity) { + optimization_altitude_ = candidate; + } + } } - } - } - using (new UnityEngine.GUILayout.HorizontalScope()) { - UnityEngine.GUILayout.Label( - L10N.CacheFormat("#Principia_FlightPlan_TargetInclination")); - bool optimize_inclination = UnityEngine.GUILayout.Toggle( - optimization_inclination_in_degrees_.HasValue, - L10N.CacheFormat("#Principia_FlightPlan_OptimizeInclination")); - string text = UnityEngine.GUILayout.TextField( - optimize_inclination - ? optimization_inclination_in_degrees_.Value.FormatN(0) - : "", - GUILayoutWidth(3)); - UnityEngine.GUILayout.Label( - text: L10N.CacheFormat("#Principia_FlightPlan_AngleUnit"), - options: GUILayoutWidth(2)); - if (!optimize_inclination) { - optimization_inclination_in_degrees_ = null; - } else if (double.TryParse(text, - System.Globalization.NumberStyles.Any, - Culture.culture, - out double candidate)) { - optimization_inclination_in_degrees_ = - Math.Max(Math.Min(180, candidate), -180); + using (new UnityEngine.GUILayout.HorizontalScope()) { + UnityEngine.GUILayout.Label( + L10N.CacheFormat("#Principia_FlightPlan_TargetInclination"), + style : Style.RightAligned(UnityEngine.GUI.skin.label)); + bool optimize_inclination = UnityEngine.GUILayout.Toggle( + optimization_inclination_in_degrees_.HasValue, + L10N.CacheFormat( + "#Principia_FlightPlan_OptimizeInclination", + GUILayoutWidth(1)), + style : Style.RightAligned(UnityEngine.GUI.skin.toggle)); + string text = UnityEngine.GUILayout.TextField( + optimization_inclination_in_degrees_.HasValue + ? optimization_inclination_in_degrees_.Value.FormatN(0) + : L10N.CacheFormat( + "#Principia_FlightPlan_DontOptimizeInclination"), + GUILayoutWidth(3)); + UnityEngine.GUILayout.Label( + text: L10N.CacheFormat( + "#Principia_FlightPlan_InclinationUnit"), + options: GUILayoutWidth(2)); + if (!optimize_inclination) { + optimization_inclination_in_degrees_ = null; + } else if (text == + L10N.CacheFormat( + "#Principia_FlightPlan_DontOptimizeInclination")) { + optimization_inclination_in_degrees_ = 0; + } else if (double.TryParse(text, + System.Globalization.NumberStyles. + Any, + Culture.culture, + out double candidate)) { + optimization_inclination_in_degrees_ = + Math.Max(Math.Min(180, candidate), -180); + } + } } } } @@ -456,24 +480,24 @@ private void RenderFlightPlan(string vessel_guid) { return; } Style.HorizontalLine(); - if (adapter_.plotting_frame_selector_. - Centre() is CelestialBody centre) { - if (plugin.FlightPlanOptimizationInProgress(vessel_guid)) { - UnityEngine.GUILayout.Button("Optimizing…"); - } else if (UnityEngine.GUILayout.Button( - $"Optimize {centre.Name()} flyby")) { - plugin.FlightPlanOptimizeManoeuvre( - vessel_guid, - i, - centre.flightGlobalsIndex, - centre.Radius + optimization_altitude_, - optimization_inclination_in_degrees_, - (NavigationFrameParameters)adapter_.plotting_frame_selector_. - FrameParameters()); - } - } else { - UnityEngine.GUILayout.Button("Change plotting frame to optimize"); - } + //if (adapter_.plotting_frame_selector_. + // Centre() is CelestialBody centre) { + // if (plugin.FlightPlanOptimizationInProgress(vessel_guid)) { + // UnityEngine.GUILayout.Button("Optimizing…"); + // } else if (UnityEngine.GUILayout.Button( + // $"Optimize {centre.Name()} flyby")) { + // plugin.FlightPlanOptimizeManoeuvre( + // vessel_guid, + // i, + // centre.flightGlobalsIndex, + // centre.Radius + optimization_altitude_, + // optimization_inclination_in_degrees_, + // (NavigationFrameParameters)adapter_.plotting_frame_selector_. + // FrameParameters()); + // } + //} else { + // UnityEngine.GUILayout.Button("Change plotting frame to optimize"); + //} BurnEditor burn = burn_editors_[i]; switch (burn.Render( header : diff --git a/ksp_plugin_adapter/localization/en-us.cfg b/ksp_plugin_adapter/localization/en-us.cfg index 17d0155058..f9da88ba2a 100644 --- a/ksp_plugin_adapter/localization/en-us.cfg +++ b/ksp_plugin_adapter/localization/en-us.cfg @@ -170,6 +170,13 @@ Localization { #Principia_FlightPlan_Delete = Delete flight plan #Principia_FlightPlan_Rebase = Rebase #Principia_FlightPlan_Duplicate = Duplicate + #Principia_FlightPlan_Optimization = Optimization parameters for <<1>> flyby + #Principia_FlightPlan_TargetAltitude = Altitude + #Principia_FlightPlan_AltitudeUnit = m + #Principia_FlightPlan_TargetInclination = Inclination + #Principia_FlightPlan_OptimizeInclination = + #Principia_FlightPlan_DontOptimizeInclination = OFF + #Principia_FlightPlan_InclinationUnit = ° #Principia_FlightPlan_ManœuvreHeader = Manœuvre #<<1>> #Principia_FlightPlan_UpcomingManœuvre = Upcoming manœuvre #<<1>>: #Principia_FlightPlan_IgnitionCountdown = Ignition <<1>> @@ -227,6 +234,7 @@ Localization { #Principia_BurnEditor_TimeBase_EndOfManœuvre = Time base: end of manœuvre #<<1>> // <<1>> index #Principia_BurnEditor_Δv = Manœuvre Δv : <<1>> m/s // <<1>>: Δv().ToString("0.000"). #Principia_BurnEditor_Duration = Duration : <<1>> s // <<1>>: duration_.ToString("0.0"). + #Principia_BurnEditor_Optimize = Optimize #Principia_BurnEditor_Warning_NoActiveEngines = No active engines, falling back to RCS.\u0020 #Principia_BurnEditor_Warning_NoActiveRCS = No active RCS, modeling as instant burn.\u0020 From 8b01d1133d6d868e7072e007ae7e5534e8f32baf Mon Sep 17 00:00:00 2001 From: pleroy Date: Sun, 15 Oct 2023 12:26:49 +0200 Subject: [PATCH 3/9] A big restructuring with "optimize" in the burn. --- ksp_plugin/interface_flight_plan.cpp | 49 ++++++++++++++++------------ ksp_plugin/vessel.cpp | 24 ++++++++++---- ksp_plugin/vessel.hpp | 10 ++++-- ksp_plugin_adapter/burn_editor.cs | 16 +++------ ksp_plugin_adapter/flight_planner.cs | 30 +++++++++++++++++ serialization/journal.proto | 30 ++++++++++++----- 6 files changed, 109 insertions(+), 50 deletions(-) diff --git a/ksp_plugin/interface_flight_plan.cpp b/ksp_plugin/interface_flight_plan.cpp index d655438b77..db3bac18be 100644 --- a/ksp_plugin/interface_flight_plan.cpp +++ b/ksp_plugin/interface_flight_plan.cpp @@ -578,51 +578,43 @@ int __cdecl principia__FlightPlanSelected(Plugin const* const plugin, return m.Return(plugin->GetVessel(vessel_guid)->selected_flight_plan_index()); } -bool __cdecl principia__FlightPlanOptimizationInProgress( +bool __cdecl principia__FlightPlanOptimizationDriverInProgress( Plugin const* const plugin, char const* const vessel_guid) { journal::Method m( {plugin, vessel_guid}); CHECK_NOTNULL(plugin); - auto const* driver = - plugin->GetVessel(vessel_guid)->flight_plan_optimization_driver(); - return m.Return(driver != nullptr && !driver->done()); + auto& vessel = *plugin->GetVessel(vessel_guid); + return m.Return(vessel.FlightPlanOptimizationDriverInProgress()); } -void __cdecl principia__FlightPlanOptimizeManoeuvre( +void __cdecl principia__FlightPlanOptimizationDriverMake( Plugin const* const plugin, char const* const vessel_guid, - int const manœuvre_index, int const celestial_index, double const distance, double const* const inclination_in_degrees, NavigationFrameParameters const navigation_frame_parameters) { - journal::Method m( + journal::Method m( {plugin, vessel_guid, - manœuvre_index, - celestial_index, distance, inclination_in_degrees, + celestial_index, navigation_frame_parameters}); CHECK_NOTNULL(plugin); auto& vessel = *plugin->GetVessel(vessel_guid); - { - auto* const old_driver = vessel.flight_plan_optimization_driver(); - if (old_driver != nullptr) { - old_driver->Interrupt(); - } - } + const auto& celestial = plugin->GetCelestial(celestial_index); std::vector factories = { FlightPlanOptimizer::ForCelestialDistance( - /*celestial=*/&plugin->GetCelestial(celestial_index), + /*celestial=*/&celestial, /*target_distance=*/distance * Metre), FlightPlanOptimizer::ForΔv()}; std::vector weights = {1, 1e3}; if (inclination_in_degrees != nullptr) { factories.push_back(FlightPlanOptimizer::ForInclination( - &plugin->GetCelestial(celestial_index), + &celestial, [plugin, navigation_frame_parameters]() { return NewNavigationFrame(*plugin, navigation_frame_parameters); }, @@ -633,10 +625,25 @@ void __cdecl principia__FlightPlanOptimizeManoeuvre( vessel.MakeFlightPlanOptimizationDriver( FlightPlanOptimizer::LinearCombination(factories, weights)); - const FlightPlanOptimizationDriver::Parameters parameters{ - .index = manœuvre_index, - .Δv_tolerance = 1 * Micro(Metre) / Second}; - vessel.flight_plan_optimization_driver()->RequestOptimization(parameters); + return m.Return(); +} + +// REMOVE BEFORE FLIGHT: Sort the functions. +void __cdecl principia__FlightPlanOptimizationDriverStart( + Plugin const* const plugin, + char const* const vessel_guid, + int const manœuvre_index) { + journal::Method m( + {plugin, + vessel_guid, + manœuvre_index}); + CHECK_NOTNULL(plugin); + auto& vessel = *plugin->GetVessel(vessel_guid); + + vessel.StartFlightPlanOptimizationDriver( + {.index = manœuvre_index, + .Δv_tolerance = 1 * Micro(Metre) / Second}); + return m.Return(); } diff --git a/ksp_plugin/vessel.cpp b/ksp_plugin/vessel.cpp index b5e7cb5228..85c95d4dba 100644 --- a/ksp_plugin/vessel.cpp +++ b/ksp_plugin/vessel.cpp @@ -351,10 +351,28 @@ void Vessel::MakeFlightPlanOptimizationDriver( CHECK(has_deserialized_flight_plan()); auto& [flight_plan, optimization_driver] = std::get(selected_flight_plan()); + if (optimization_driver != nullptr) { + optimization_driver->Interrupt(); + } optimization_driver = make_not_null_unique( *flight_plan, std::move(metric_factory)); } +void Vessel::StartFlightPlanOptimizationDriver( + FlightPlanOptimizationDriver::Parameters const& parameters) { + CHECK(has_deserialized_flight_plan()); + auto const& driver = std::get(selected_flight_plan()) + .optimization_driver; + CHECK_NOTNULL(driver); + driver->RequestOptimization(parameters); +} + +bool Vessel::FlightPlanOptimizationDriverInProgress() const { + auto const& driver = std::get(selected_flight_plan()) + .optimization_driver; + return driver != nullptr && !driver->done(); +} + bool Vessel::UpdateFlightPlanFromOptimization() { CHECK(has_deserialized_flight_plan()); auto& [flight_plan, optimization_driver] = @@ -371,12 +389,6 @@ bool Vessel::UpdateFlightPlanFromOptimization() { return false; } -FlightPlanOptimizationDriver* Vessel::flight_plan_optimization_driver() { - CHECK(has_deserialized_flight_plan()); - return std::get(selected_flight_plan()) - .optimization_driver.get(); -} - void Vessel::ReadFlightPlanFromMessage() { if (!flight_plans_.empty() && std::holds_alternative( diff --git a/ksp_plugin/vessel.hpp b/ksp_plugin/vessel.hpp index 9b2d71db4a..f47bcc97e2 100644 --- a/ksp_plugin/vessel.hpp +++ b/ksp_plugin/vessel.hpp @@ -186,12 +186,18 @@ class Vessel { // flight plan or the flight plan has not been deserialized. virtual FlightPlan& flight_plan() const; + // Construct a new driver for the given metric (but doesn't start it). If + // there is a driver currently running it is interrupted and destroyed. virtual void MakeFlightPlanOptimizationDriver( FlightPlanOptimizer::MetricFactory metric_factory); - virtual bool UpdateFlightPlanFromOptimization(); + // Starts an optimization with the given parameters. + virtual void StartFlightPlanOptimizationDriver( + FlightPlanOptimizationDriver::Parameters const& parameters); + + virtual bool FlightPlanOptimizationDriverInProgress() const; - virtual FlightPlanOptimizationDriver* flight_plan_optimization_driver(); + virtual bool UpdateFlightPlanFromOptimization(); // Deserializes the flight plan if it is held lazily by this object. Does // nothing if there is no such flight plan. If |has_flight_plan| returns diff --git a/ksp_plugin_adapter/burn_editor.cs b/ksp_plugin_adapter/burn_editor.cs index 9a9ec7854d..166516609b 100644 --- a/ksp_plugin_adapter/burn_editor.cs +++ b/ksp_plugin_adapter/burn_editor.cs @@ -260,25 +260,17 @@ public Event Render( UnityEngine.GUILayout.Label(L10N.CacheFormat( "#Principia_BurnEditor_Duration", duration_.ToString("0.0"))); + if (adapter_.plotting_frame_selector_. Centre() is CelestialBody centre) { if (plugin.FlightPlanOptimizationInProgress(vessel_.id.ToString())) { UnityEngine.GUILayout.Button("Optimizing…"); } else if (UnityEngine.GUILayout.Button( "#Principia_BurnEditor_Optimize")) { - plugin.FlightPlanOptimizeManoeuvre(vessel_.id.ToString(), - index, - centre.flightGlobalsIndex, - centre.Radius + - optimization_altitude_, - optimization_inclination_in_degrees_, - (NavigationFrameParameters) - adapter_. - plotting_frame_selector_. - FrameParameters()); + plugin.FlightPlanOptimizationDriverStart( + vessel_.id.ToString(), + index); } - } else { - UnityEngine.GUILayout.Button("Change plotting frame to optimize"); } } UnityEngine.GUILayout.Label(engine_warning_, diff --git a/ksp_plugin_adapter/flight_planner.cs b/ksp_plugin_adapter/flight_planner.cs index 7c4b0c51f8..075894455b 100644 --- a/ksp_plugin_adapter/flight_planner.cs +++ b/ksp_plugin_adapter/flight_planner.cs @@ -393,6 +393,13 @@ private void RenderFlightPlan(string vessel_guid) { style : Style.Aligned(UnityEngine.TextAnchor.LowerLeft, UnityEngine.GUI.skin.label)); using (new UnityEngine.GUILayout.VerticalScope()) { + double optimization_altitude = optimization_altitude_; + double? optimization_inclination_in_degrees = + optimization_inclination_in_degrees_; + IReferenceFrameParameters + optimization_reference_frame_parameters = + optimization_reference_frame_parameters_; + using (new UnityEngine.GUILayout.HorizontalScope()) { UnityEngine.GUILayout.Label( L10N.CacheFormat("#Principia_FlightPlan_TargetAltitude"), @@ -449,6 +456,27 @@ private void RenderFlightPlan(string vessel_guid) { Math.Max(Math.Min(180, candidate), -180); } } + + if (optimization_altitude_ != optimization_altitude || + optimization_inclination_in_degrees_ != + optimization_inclination_in_degrees || + optimization_reference_frame_parameters_ != + (NavigationFrameParameters)adapter_.plotting_frame_selector_. + FrameParameters()) { + plugin.FlightPlanOptimizationDriverMake( + vessel_guid, + centre.Radius + optimization_altitude_, + optimization_inclination_in_degrees_, + centre.flightGlobalsIndex, + (NavigationFrameParameters)adapter_. + plotting_frame_selector_.FrameParameters()); + optimization_altitude_ = optimization_altitude; + optimization_inclination_in_degrees_ = + optimization_inclination_in_degrees; + optimization_reference_frame_parameters_ = + (NavigationFrameParameters)adapter_. + plotting_frame_selector_.FrameParameters(); + } } } } @@ -903,6 +931,8 @@ private void UpdateAdaptiveStepParameters( private double optimization_altitude_ = 10e3; private double? optimization_inclination_in_degrees_ = 0; + private NavigationFrameParameters optimization_reference_frame_parameters_ = + null; } } // namespace ksp_plugin_adapter diff --git a/serialization/journal.proto b/serialization/journal.proto index 363ab6315d..1aa17b8d92 100644 --- a/serialization/journal.proto +++ b/serialization/journal.proto @@ -238,7 +238,7 @@ message OrbitAnalysis { } message Method { - extensions 5000 to 5999; // Last used: 5187. + extensions 5000 to 5999; // Last used: 5191. } message AdvanceTime { @@ -1030,9 +1030,9 @@ message FlightPlanNumberOfSegments { optional Return return = 3; } -message FlightPlanOptimizationInProgress { +message FlightPlanOptimizationDriverInProgress { extend Method { - optional FlightPlanOptimizationInProgress extension = 5188; + optional FlightPlanOptimizationDriverInProgress extension = 5188; } message In { required fixed64 plugin = 1 [(pointer_to) = "Plugin const", @@ -1046,19 +1046,31 @@ message FlightPlanOptimizationInProgress { optional Return return = 3; } -message FlightPlanOptimizeManoeuvre { +message FlightPlanOptimizationDriverMake { extend Method { - optional FlightPlanOptimizeManoeuvre extension = 5189; + optional FlightPlanOptimizationDriverMake extension = 5191; + } + message In { + required fixed64 plugin = 1 [(pointer_to) = "Plugin const", + (is_subject) = true]; + required string vessel_guid = 2; + required double distance = 3; + optional double inclination_in_degrees = 4; + required int32 celestial_index = 5; + required NavigationFrameParameters navigation_frame_parameters = 6; + } + optional In in = 1; +} + +message FlightPlanOptimizationDriverStart { + extend Method { + optional FlightPlanOptimizationDriverStart extension = 5189; } message In { required fixed64 plugin = 1 [(pointer_to) = "Plugin const", (is_subject) = true]; required string vessel_guid = 2; required int32 manoeuvre_index = 3; - required int32 celestial_index = 4; - required double distance = 5; - optional double inclination_in_degrees = 6; - required NavigationFrameParameters navigation_frame_parameters = 7; } optional In in = 1; } From e1fae8167b9fa691571f5b68eb6e59bf82d067b4 Mon Sep 17 00:00:00 2001 From: pleroy Date: Sun, 15 Oct 2023 14:38:41 +0200 Subject: [PATCH 4/9] A UI that more or less works. --- ksp_plugin/interface_flight_plan.cpp | 4 +- ksp_plugin_adapter/burn_editor.cs | 12 ++--- ksp_plugin_adapter/flight_planner.cs | 64 +++++++++++++---------- ksp_plugin_adapter/interface.cs | 28 +++++++--- ksp_plugin_adapter/localization/en-us.cfg | 1 + ksp_plugin_adapter/style.cs | 9 ++++ ksp_plugin_adapter/window_renderer.cs | 8 +++ 7 files changed, 83 insertions(+), 43 deletions(-) diff --git a/ksp_plugin/interface_flight_plan.cpp b/ksp_plugin/interface_flight_plan.cpp index db3bac18be..bd1da6a15a 100644 --- a/ksp_plugin/interface_flight_plan.cpp +++ b/ksp_plugin/interface_flight_plan.cpp @@ -581,7 +581,7 @@ int __cdecl principia__FlightPlanSelected(Plugin const* const plugin, bool __cdecl principia__FlightPlanOptimizationDriverInProgress( Plugin const* const plugin, char const* const vessel_guid) { - journal::Method m( + journal::Method m( {plugin, vessel_guid}); CHECK_NOTNULL(plugin); auto& vessel = *plugin->GetVessel(vessel_guid); @@ -591,9 +591,9 @@ bool __cdecl principia__FlightPlanOptimizationDriverInProgress( void __cdecl principia__FlightPlanOptimizationDriverMake( Plugin const* const plugin, char const* const vessel_guid, - int const celestial_index, double const distance, double const* const inclination_in_degrees, + int const celestial_index, NavigationFrameParameters const navigation_frame_parameters) { journal::Method m( {plugin, diff --git a/ksp_plugin_adapter/burn_editor.cs b/ksp_plugin_adapter/burn_editor.cs index 166516609b..2a88d35e52 100644 --- a/ksp_plugin_adapter/burn_editor.cs +++ b/ksp_plugin_adapter/burn_editor.cs @@ -263,13 +263,13 @@ public Event Render( if (adapter_.plotting_frame_selector_. Centre() is CelestialBody centre) { - if (plugin.FlightPlanOptimizationInProgress(vessel_.id.ToString())) { - UnityEngine.GUILayout.Button("Optimizing…"); + string vessel_guid = vessel_.id.ToString(); + if (plugin.FlightPlanOptimizationDriverInProgress(vessel_guid)) { + UnityEngine.GUILayout.Label( + L10N.CacheFormat("#Principia_BurnEditor_Optimizing")); } else if (UnityEngine.GUILayout.Button( - "#Principia_BurnEditor_Optimize")) { - plugin.FlightPlanOptimizationDriverStart( - vessel_.id.ToString(), - index); + L10N.CacheFormat("#Principia_BurnEditor_Optimize"))) { + plugin.FlightPlanOptimizationDriverStart(vessel_guid, index); } } } diff --git a/ksp_plugin_adapter/flight_planner.cs b/ksp_plugin_adapter/flight_planner.cs index 075894455b..81b79aaa77 100644 --- a/ksp_plugin_adapter/flight_planner.cs +++ b/ksp_plugin_adapter/flight_planner.cs @@ -380,60 +380,60 @@ private void RenderFlightPlan(string vessel_guid) { } } - Style.HorizontalLine(); // There is no Layout/Repaint trouble here because the frame is selected // in another window. if (adapter_.plotting_frame_selector_. Centre() is CelestialBody centre) { + Style.HorizontalLine(); using (new UnityEngine.GUILayout.HorizontalScope()) { ////valign UnityEngine.GUILayout.Label( L10N.CelestialString("#Principia_FlightPlan_Optimization", - new[]{centre}), - style : Style.Aligned(UnityEngine.TextAnchor.LowerLeft, - UnityEngine.GUI.skin.label)); + new[]{ centre }), + style: Style.MiddleLeftAligned(UnityEngine.GUI.skin.label, + Height(2))); using (new UnityEngine.GUILayout.VerticalScope()) { double optimization_altitude = optimization_altitude_; double? optimization_inclination_in_degrees = optimization_inclination_in_degrees_; - IReferenceFrameParameters - optimization_reference_frame_parameters = - optimization_reference_frame_parameters_; + UnityEngine.Debug.LogError("1"); using (new UnityEngine.GUILayout.HorizontalScope()) { UnityEngine.GUILayout.Label( - L10N.CacheFormat("#Principia_FlightPlan_TargetAltitude"), - style : Style.RightAligned(UnityEngine.GUI.skin.label)); + L10N.CacheFormat("#Principia_FlightPlan_TargetAltitude")); string text = UnityEngine.GUILayout.TextField( - optimization_altitude_.FormatN(0), + optimization_altitude.FormatN(0), GUILayoutWidth(3)); UnityEngine.GUILayout.Label( text : L10N.CacheFormat( "#Principia_FlightPlan_AltitudeUnit"), options : GUILayoutWidth(2)); + UnityEngine.Debug.LogError("2"); if (double.TryParse(text, System.Globalization.NumberStyles.Any, Culture.culture, out double candidate)) { if (candidate >= 0 && candidate < double.PositiveInfinity) { - optimization_altitude_ = candidate; + optimization_altitude = candidate; } + UnityEngine.Debug.LogError("3 " + text + " " + + optimization_altitude); } } using (new UnityEngine.GUILayout.HorizontalScope()) { + UnityEngine.Debug.LogError("4"); UnityEngine.GUILayout.Label( - L10N.CacheFormat("#Principia_FlightPlan_TargetInclination"), - style : Style.RightAligned(UnityEngine.GUI.skin.label)); + L10N.CacheFormat("#Principia_FlightPlan_TargetInclination")); bool optimize_inclination = UnityEngine.GUILayout.Toggle( - optimization_inclination_in_degrees_.HasValue, + optimization_inclination_in_degrees.HasValue, L10N.CacheFormat( "#Principia_FlightPlan_OptimizeInclination", - GUILayoutWidth(1)), + GUILayoutWidth(0.2f)), style : Style.RightAligned(UnityEngine.GUI.skin.toggle)); string text = UnityEngine.GUILayout.TextField( - optimization_inclination_in_degrees_.HasValue - ? optimization_inclination_in_degrees_.Value.FormatN(0) + optimization_inclination_in_degrees.HasValue + ? optimization_inclination_in_degrees.Value.FormatN(0) : L10N.CacheFormat( "#Principia_FlightPlan_DontOptimizeInclination"), GUILayoutWidth(3)); @@ -441,42 +441,50 @@ private void RenderFlightPlan(string vessel_guid) { text: L10N.CacheFormat( "#Principia_FlightPlan_InclinationUnit"), options: GUILayoutWidth(2)); + UnityEngine.Debug.LogError("5"); if (!optimize_inclination) { - optimization_inclination_in_degrees_ = null; + optimization_inclination_in_degrees = null; } else if (text == L10N.CacheFormat( "#Principia_FlightPlan_DontOptimizeInclination")) { - optimization_inclination_in_degrees_ = 0; + optimization_inclination_in_degrees = 0; } else if (double.TryParse(text, System.Globalization.NumberStyles. Any, Culture.culture, out double candidate)) { - optimization_inclination_in_degrees_ = + optimization_inclination_in_degrees = Math.Max(Math.Min(180, candidate), -180); } + UnityEngine.Debug.LogError("6"); } + // If any of the parameters changed (that includes a change of + // plotting frame in another window) recreated the optimization + // driver. This interrupts any optimization that might be + // running, to avoid confusing results. if (optimization_altitude_ != optimization_altitude || optimization_inclination_in_degrees_ != optimization_inclination_in_degrees || optimization_reference_frame_parameters_ != (NavigationFrameParameters)adapter_.plotting_frame_selector_. FrameParameters()) { - plugin.FlightPlanOptimizationDriverMake( - vessel_guid, - centre.Radius + optimization_altitude_, - optimization_inclination_in_degrees_, - centre.flightGlobalsIndex, - (NavigationFrameParameters)adapter_. - plotting_frame_selector_.FrameParameters()); + UnityEngine.Debug.LogError("7"); optimization_altitude_ = optimization_altitude; optimization_inclination_in_degrees_ = optimization_inclination_in_degrees; optimization_reference_frame_parameters_ = (NavigationFrameParameters)adapter_. plotting_frame_selector_.FrameParameters(); + plugin.FlightPlanOptimizationDriverMake( + vessel_guid, + centre.Radius + optimization_altitude_, + optimization_inclination_in_degrees_, + centre.flightGlobalsIndex, + optimization_reference_frame_parameters_); + UnityEngine.Debug.LogError("8"); } + UnityEngine.Debug.LogError("8a"); } } } @@ -527,6 +535,7 @@ private void RenderFlightPlan(string vessel_guid) { // UnityEngine.GUILayout.Button("Change plotting frame to optimize"); //} BurnEditor burn = burn_editors_[i]; + UnityEngine.Debug.LogError("9"); switch (burn.Render( header : L10N.CacheFormat("#Principia_FlightPlan_ManœuvreHeader", i + 1), @@ -558,6 +567,7 @@ private void RenderFlightPlan(string vessel_guid) { } } } + UnityEngine.Debug.LogError("10"); Style.HorizontalLine(); if (RenderCoast(burn_editors_.Count, orbital_period: out _)) { return; diff --git a/ksp_plugin_adapter/interface.cs b/ksp_plugin_adapter/interface.cs index e06d5d3589..ecbd566782 100644 --- a/ksp_plugin_adapter/interface.cs +++ b/ksp_plugin_adapter/interface.cs @@ -113,10 +113,16 @@ public override int GetHashCode() => public static bool operator ==(PlottingFrameParameters left, IReferenceFrameParameters right) { - return left.Extension == right.Extension && - left.centre_index == right.CentreIndex && - left.PrimaryIndices.SequenceEqual(right.PrimaryIndices) && - left.SecondaryIndices.SequenceEqual(right.SecondaryIndices); + if ((object)left == null && (object)right == null) { + return true; + } else if ((object)left == null || (object)right == null) { + return false; + } else { + return left.Extension == right.Extension && + left.centre_index == right.CentreIndex && + left.PrimaryIndices.SequenceEqual(right.PrimaryIndices) && + left.SecondaryIndices.SequenceEqual(right.SecondaryIndices); + } } public static bool operator !=(PlottingFrameParameters left, @@ -165,10 +171,16 @@ public override int GetHashCode() => public static bool operator ==(NavigationFrameParameters left, IReferenceFrameParameters right) { - return left.Extension == right.Extension && - left.CentreIndex == right.CentreIndex && - left.PrimaryIndices.SequenceEqual(right.PrimaryIndices) && - left.SecondaryIndices.SequenceEqual(right.SecondaryIndices); + if ((object)left == null && (object)right == null) { + return true; + } else if ((object)left == null || (object)right == null) { + return false; + } else { + return left.Extension == right.Extension && + left.CentreIndex == right.CentreIndex && + left.PrimaryIndices.SequenceEqual(right.PrimaryIndices) && + left.SecondaryIndices.SequenceEqual(right.SecondaryIndices); + } } public static bool operator !=(NavigationFrameParameters left, diff --git a/ksp_plugin_adapter/localization/en-us.cfg b/ksp_plugin_adapter/localization/en-us.cfg index f9da88ba2a..3d19d466cc 100644 --- a/ksp_plugin_adapter/localization/en-us.cfg +++ b/ksp_plugin_adapter/localization/en-us.cfg @@ -234,6 +234,7 @@ Localization { #Principia_BurnEditor_TimeBase_EndOfManœuvre = Time base: end of manœuvre #<<1>> // <<1>> index #Principia_BurnEditor_Δv = Manœuvre Δv : <<1>> m/s // <<1>>: Δv().ToString("0.000"). #Principia_BurnEditor_Duration = Duration : <<1>> s // <<1>>: duration_.ToString("0.0"). + #Principia_BurnEditor_Optimizing = Optimizing… #Principia_BurnEditor_Optimize = Optimize #Principia_BurnEditor_Warning_NoActiveEngines = No active engines, falling back to RCS.\u0020 #Principia_BurnEditor_Warning_NoActiveRCS = No active RCS, modeling as instant burn.\u0020 diff --git a/ksp_plugin_adapter/style.cs b/ksp_plugin_adapter/style.cs index dc1d9c50db..afa3b3db70 100644 --- a/ksp_plugin_adapter/style.cs +++ b/ksp_plugin_adapter/style.cs @@ -91,6 +91,15 @@ public static UnityEngine.GUIStyle Aligned(UnityEngine.TextAnchor alignment, return aligned_style; } + public static UnityEngine.GUIStyle MiddleLeftAligned( + UnityEngine.GUIStyle style, + float height) { + return new UnityEngine.GUIStyle(style){ + alignment = UnityEngine.TextAnchor.MiddleLeft, + fixedHeight = height; + } + } + public static UnityEngine.GUIStyle RightAligned(UnityEngine.GUIStyle style) { return Aligned(UnityEngine.TextAnchor.MiddleRight, style); } diff --git a/ksp_plugin_adapter/window_renderer.cs b/ksp_plugin_adapter/window_renderer.cs index 1c3af98770..12bc68b2a2 100644 --- a/ksp_plugin_adapter/window_renderer.cs +++ b/ksp_plugin_adapter/window_renderer.cs @@ -25,6 +25,14 @@ protected UnityEngine.GUILayoutOption GUILayoutWidth(int units) { return UnityEngine.GUILayout.Width(Width(units)); } + protected UnityEngine.GUILayoutOption GUILayoutWidth(float units) { + return UnityEngine.GUILayout.Width(Width(units)); + } + + protected float Height(float units) { + return unit_ * units; + } + protected float Width(float units) { return unit_ * units; } From 5b491d687cbc6caa4520d329b7802bcce1591fd1 Mon Sep 17 00:00:00 2001 From: pleroy Date: Sun, 15 Oct 2023 15:17:25 +0200 Subject: [PATCH 5/9] Pixel tweaking. --- ksp_plugin/interface_flight_plan.cpp | 9 +++++++-- ksp_plugin/vessel.cpp | 11 +++++++++-- ksp_plugin/vessel.hpp | 7 ++++++- ksp_plugin_adapter/burn_editor.cs | 10 ++++++++-- ksp_plugin_adapter/flight_planner.cs | 23 ++++++++++++++--------- ksp_plugin_adapter/localization/en-us.cfg | 8 +++++--- ksp_plugin_adapter/style.cs | 4 ++-- serialization/journal.proto | 2 +- 8 files changed, 52 insertions(+), 22 deletions(-) diff --git a/ksp_plugin/interface_flight_plan.cpp b/ksp_plugin/interface_flight_plan.cpp index bd1da6a15a..80da57a7e5 100644 --- a/ksp_plugin/interface_flight_plan.cpp +++ b/ksp_plugin/interface_flight_plan.cpp @@ -578,14 +578,19 @@ int __cdecl principia__FlightPlanSelected(Plugin const* const plugin, return m.Return(plugin->GetVessel(vessel_guid)->selected_flight_plan_index()); } -bool __cdecl principia__FlightPlanOptimizationDriverInProgress( +int __cdecl principia__FlightPlanOptimizationDriverInProgress( Plugin const* const plugin, char const* const vessel_guid) { journal::Method m( {plugin, vessel_guid}); CHECK_NOTNULL(plugin); auto& vessel = *plugin->GetVessel(vessel_guid); - return m.Return(vessel.FlightPlanOptimizationDriverInProgress()); + auto const maybe_parameters = vessel.FlightPlanOptimizationDriverInProgress(); + if (maybe_parameters.has_value()) { + return m.Return(maybe_parameters->index); + } else { + return m.Return(-1); + } } void __cdecl principia__FlightPlanOptimizationDriverMake( diff --git a/ksp_plugin/vessel.cpp b/ksp_plugin/vessel.cpp index 85c95d4dba..75c3c2427b 100644 --- a/ksp_plugin/vessel.cpp +++ b/ksp_plugin/vessel.cpp @@ -364,13 +364,20 @@ void Vessel::StartFlightPlanOptimizationDriver( auto const& driver = std::get(selected_flight_plan()) .optimization_driver; CHECK_NOTNULL(driver); + last_optimization_parameters_ = parameters; driver->RequestOptimization(parameters); } -bool Vessel::FlightPlanOptimizationDriverInProgress() const { +std::optional +Vessel::FlightPlanOptimizationDriverInProgress() const { auto const& driver = std::get(selected_flight_plan()) .optimization_driver; - return driver != nullptr && !driver->done(); + if (driver != nullptr && !driver->done()) { + CHECK(last_optimization_parameters_.has_value()); + return last_optimization_parameters_; + } else { + return std::nullopt; + } } bool Vessel::UpdateFlightPlanFromOptimization() { diff --git a/ksp_plugin/vessel.hpp b/ksp_plugin/vessel.hpp index f47bcc97e2..a9c2c0c1d7 100644 --- a/ksp_plugin/vessel.hpp +++ b/ksp_plugin/vessel.hpp @@ -195,7 +195,10 @@ class Vessel { virtual void StartFlightPlanOptimizationDriver( FlightPlanOptimizationDriver::Parameters const& parameters); - virtual bool FlightPlanOptimizationDriverInProgress() const; + // If an optimization is in progress, returns the parameters of the + // optimization. + virtual std::optional + FlightPlanOptimizationDriverInProgress() const; virtual bool UpdateFlightPlanFromOptimization(); @@ -439,6 +442,8 @@ class Vessel { std::vector flight_plans_; int selected_flight_plan_index_ = -1; + std::optional + last_optimization_parameters_; std::optional orbit_analyser_; diff --git a/ksp_plugin_adapter/burn_editor.cs b/ksp_plugin_adapter/burn_editor.cs index 2a88d35e52..fac65dfcdd 100644 --- a/ksp_plugin_adapter/burn_editor.cs +++ b/ksp_plugin_adapter/burn_editor.cs @@ -264,9 +264,15 @@ public Event Render( if (adapter_.plotting_frame_selector_. Centre() is CelestialBody centre) { string vessel_guid = vessel_.id.ToString(); - if (plugin.FlightPlanOptimizationDriverInProgress(vessel_guid)) { + int optimized_index = + plugin.FlightPlanOptimizationDriverInProgress(vessel_guid); + UnityEngine.Debug.LogError(optimized_index + " " + index); + if (optimized_index == index) { UnityEngine.GUILayout.Label( - L10N.CacheFormat("#Principia_BurnEditor_Optimizing")); + L10N.CacheFormat("#Principia_BurnEditor_OptimizingThis")); + } else if (optimized_index != -1) { + UnityEngine.GUILayout.Label( + L10N.CacheFormat("#Principia_BurnEditor_OptimizingOther")); } else if (UnityEngine.GUILayout.Button( L10N.CacheFormat("#Principia_BurnEditor_Optimize"))) { plugin.FlightPlanOptimizationDriverStart(vessel_guid, index); diff --git a/ksp_plugin_adapter/flight_planner.cs b/ksp_plugin_adapter/flight_planner.cs index 81b79aaa77..d3c1d38fd6 100644 --- a/ksp_plugin_adapter/flight_planner.cs +++ b/ksp_plugin_adapter/flight_planner.cs @@ -407,6 +407,9 @@ private void RenderFlightPlan(string vessel_guid) { UnityEngine.GUILayout.Label( text : L10N.CacheFormat( "#Principia_FlightPlan_AltitudeUnit"), + options : GUILayoutWidth(1)); + UnityEngine.GUILayout.Label( + text : "", options : GUILayoutWidth(2)); UnityEngine.Debug.LogError("2"); if (double.TryParse(text, @@ -425,28 +428,30 @@ private void RenderFlightPlan(string vessel_guid) { UnityEngine.Debug.LogError("4"); UnityEngine.GUILayout.Label( L10N.CacheFormat("#Principia_FlightPlan_TargetInclination")); - bool optimize_inclination = UnityEngine.GUILayout.Toggle( - optimization_inclination_in_degrees.HasValue, - L10N.CacheFormat( - "#Principia_FlightPlan_OptimizeInclination", - GUILayoutWidth(0.2f)), - style : Style.RightAligned(UnityEngine.GUI.skin.toggle)); string text = UnityEngine.GUILayout.TextField( optimization_inclination_in_degrees.HasValue ? optimization_inclination_in_degrees.Value.FormatN(0) : L10N.CacheFormat( - "#Principia_FlightPlan_DontOptimizeInclination"), + "#Principia_FlightPlan_OptimizeInclinationNoText"), GUILayoutWidth(3)); UnityEngine.GUILayout.Label( text: L10N.CacheFormat( "#Principia_FlightPlan_InclinationUnit"), - options: GUILayoutWidth(2)); + options: GUILayoutWidth(1)); + bool optimize_inclination = UnityEngine.GUILayout.Toggle( + optimization_inclination_in_degrees.HasValue, + optimization_inclination_in_degrees.HasValue + ? L10N.CacheFormat( + "#Principia_FlightPlan_OptimizeInclinationOn") + : L10N.CacheFormat( + "#Principia_FlightPlan_OptimizeInclinationOff"), + GUILayoutWidth(2)); UnityEngine.Debug.LogError("5"); if (!optimize_inclination) { optimization_inclination_in_degrees = null; } else if (text == L10N.CacheFormat( - "#Principia_FlightPlan_DontOptimizeInclination")) { + "#Principia_FlightPlan_OptimizeInclinationNoText")) { optimization_inclination_in_degrees = 0; } else if (double.TryParse(text, System.Globalization.NumberStyles. diff --git a/ksp_plugin_adapter/localization/en-us.cfg b/ksp_plugin_adapter/localization/en-us.cfg index 3d19d466cc..5412d0d9c0 100644 --- a/ksp_plugin_adapter/localization/en-us.cfg +++ b/ksp_plugin_adapter/localization/en-us.cfg @@ -174,8 +174,9 @@ Localization { #Principia_FlightPlan_TargetAltitude = Altitude #Principia_FlightPlan_AltitudeUnit = m #Principia_FlightPlan_TargetInclination = Inclination - #Principia_FlightPlan_OptimizeInclination = - #Principia_FlightPlan_DontOptimizeInclination = OFF + #Principia_FlightPlan_OptimizeInclinationOn = On + #Principia_FlightPlan_OptimizeInclinationOff = Off + #Principia_FlightPlan_OptimizeInclinationNoText = #Principia_FlightPlan_InclinationUnit = ° #Principia_FlightPlan_ManœuvreHeader = Manœuvre #<<1>> #Principia_FlightPlan_UpcomingManœuvre = Upcoming manœuvre #<<1>>: @@ -234,7 +235,8 @@ Localization { #Principia_BurnEditor_TimeBase_EndOfManœuvre = Time base: end of manœuvre #<<1>> // <<1>> index #Principia_BurnEditor_Δv = Manœuvre Δv : <<1>> m/s // <<1>>: Δv().ToString("0.000"). #Principia_BurnEditor_Duration = Duration : <<1>> s // <<1>>: duration_.ToString("0.0"). - #Principia_BurnEditor_Optimizing = Optimizing… + #Principia_BurnEditor_OptimizingThis = Optimizing… + #Principia_BurnEditor_OptimizingOther = #Principia_BurnEditor_Optimize = Optimize #Principia_BurnEditor_Warning_NoActiveEngines = No active engines, falling back to RCS.\u0020 #Principia_BurnEditor_Warning_NoActiveRCS = No active RCS, modeling as instant burn.\u0020 diff --git a/ksp_plugin_adapter/style.cs b/ksp_plugin_adapter/style.cs index afa3b3db70..6253b79d61 100644 --- a/ksp_plugin_adapter/style.cs +++ b/ksp_plugin_adapter/style.cs @@ -96,8 +96,8 @@ public static UnityEngine.GUIStyle MiddleLeftAligned( float height) { return new UnityEngine.GUIStyle(style){ alignment = UnityEngine.TextAnchor.MiddleLeft, - fixedHeight = height; - } + fixedHeight = height + }; } public static UnityEngine.GUIStyle RightAligned(UnityEngine.GUIStyle style) { diff --git a/serialization/journal.proto b/serialization/journal.proto index 1aa17b8d92..accfe8a4e8 100644 --- a/serialization/journal.proto +++ b/serialization/journal.proto @@ -1040,7 +1040,7 @@ message FlightPlanOptimizationDriverInProgress { required string vessel_guid = 2; } message Return { - required bool result = 1; + required int32 manoeuvre_index = 2; } optional In in = 1; optional Return return = 3; From 284f3b66b153c6ca3eee21617306f67b693a9d99 Mon Sep 17 00:00:00 2001 From: pleroy Date: Sun, 15 Oct 2023 15:20:46 +0200 Subject: [PATCH 6/9] Remove traces. --- ksp_plugin_adapter/burn_editor.cs | 1 - ksp_plugin_adapter/flight_planner.cs | 30 ---------------------------- 2 files changed, 31 deletions(-) diff --git a/ksp_plugin_adapter/burn_editor.cs b/ksp_plugin_adapter/burn_editor.cs index fac65dfcdd..64f8c3b718 100644 --- a/ksp_plugin_adapter/burn_editor.cs +++ b/ksp_plugin_adapter/burn_editor.cs @@ -266,7 +266,6 @@ public Event Render( string vessel_guid = vessel_.id.ToString(); int optimized_index = plugin.FlightPlanOptimizationDriverInProgress(vessel_guid); - UnityEngine.Debug.LogError(optimized_index + " " + index); if (optimized_index == index) { UnityEngine.GUILayout.Label( L10N.CacheFormat("#Principia_BurnEditor_OptimizingThis")); diff --git a/ksp_plugin_adapter/flight_planner.cs b/ksp_plugin_adapter/flight_planner.cs index d3c1d38fd6..808006a4a0 100644 --- a/ksp_plugin_adapter/flight_planner.cs +++ b/ksp_plugin_adapter/flight_planner.cs @@ -396,7 +396,6 @@ private void RenderFlightPlan(string vessel_guid) { double optimization_altitude = optimization_altitude_; double? optimization_inclination_in_degrees = optimization_inclination_in_degrees_; - UnityEngine.Debug.LogError("1"); using (new UnityEngine.GUILayout.HorizontalScope()) { UnityEngine.GUILayout.Label( @@ -411,7 +410,6 @@ private void RenderFlightPlan(string vessel_guid) { UnityEngine.GUILayout.Label( text : "", options : GUILayoutWidth(2)); - UnityEngine.Debug.LogError("2"); if (double.TryParse(text, System.Globalization.NumberStyles.Any, Culture.culture, @@ -419,13 +417,10 @@ private void RenderFlightPlan(string vessel_guid) { if (candidate >= 0 && candidate < double.PositiveInfinity) { optimization_altitude = candidate; } - UnityEngine.Debug.LogError("3 " + text + " " + - optimization_altitude); } } using (new UnityEngine.GUILayout.HorizontalScope()) { - UnityEngine.Debug.LogError("4"); UnityEngine.GUILayout.Label( L10N.CacheFormat("#Principia_FlightPlan_TargetInclination")); string text = UnityEngine.GUILayout.TextField( @@ -446,7 +441,6 @@ private void RenderFlightPlan(string vessel_guid) { : L10N.CacheFormat( "#Principia_FlightPlan_OptimizeInclinationOff"), GUILayoutWidth(2)); - UnityEngine.Debug.LogError("5"); if (!optimize_inclination) { optimization_inclination_in_degrees = null; } else if (text == @@ -461,7 +455,6 @@ private void RenderFlightPlan(string vessel_guid) { optimization_inclination_in_degrees = Math.Max(Math.Min(180, candidate), -180); } - UnityEngine.Debug.LogError("6"); } // If any of the parameters changed (that includes a change of @@ -474,7 +467,6 @@ private void RenderFlightPlan(string vessel_guid) { optimization_reference_frame_parameters_ != (NavigationFrameParameters)adapter_.plotting_frame_selector_. FrameParameters()) { - UnityEngine.Debug.LogError("7"); optimization_altitude_ = optimization_altitude; optimization_inclination_in_degrees_ = optimization_inclination_in_degrees; @@ -487,9 +479,7 @@ private void RenderFlightPlan(string vessel_guid) { optimization_inclination_in_degrees_, centre.flightGlobalsIndex, optimization_reference_frame_parameters_); - UnityEngine.Debug.LogError("8"); } - UnityEngine.Debug.LogError("8a"); } } } @@ -521,26 +511,7 @@ private void RenderFlightPlan(string vessel_guid) { return; } Style.HorizontalLine(); - //if (adapter_.plotting_frame_selector_. - // Centre() is CelestialBody centre) { - // if (plugin.FlightPlanOptimizationInProgress(vessel_guid)) { - // UnityEngine.GUILayout.Button("Optimizing…"); - // } else if (UnityEngine.GUILayout.Button( - // $"Optimize {centre.Name()} flyby")) { - // plugin.FlightPlanOptimizeManoeuvre( - // vessel_guid, - // i, - // centre.flightGlobalsIndex, - // centre.Radius + optimization_altitude_, - // optimization_inclination_in_degrees_, - // (NavigationFrameParameters)adapter_.plotting_frame_selector_. - // FrameParameters()); - // } - //} else { - // UnityEngine.GUILayout.Button("Change plotting frame to optimize"); - //} BurnEditor burn = burn_editors_[i]; - UnityEngine.Debug.LogError("9"); switch (burn.Render( header : L10N.CacheFormat("#Principia_FlightPlan_ManœuvreHeader", i + 1), @@ -572,7 +543,6 @@ private void RenderFlightPlan(string vessel_guid) { } } } - UnityEngine.Debug.LogError("10"); Style.HorizontalLine(); if (RenderCoast(burn_editors_.Count, orbital_period: out _)) { return; From d37e2f5bbae85676cbb904dbcaec0ebcd7a9a734 Mon Sep 17 00:00:00 2001 From: pleroy Date: Sun, 15 Oct 2023 15:35:57 +0200 Subject: [PATCH 7/9] Readying. --- ksp_plugin/vessel.hpp | 2 +- ksp_plugin_adapter/flight_planner.cs | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/ksp_plugin/vessel.hpp b/ksp_plugin/vessel.hpp index a9c2c0c1d7..56ced21a9b 100644 --- a/ksp_plugin/vessel.hpp +++ b/ksp_plugin/vessel.hpp @@ -186,7 +186,7 @@ class Vessel { // flight plan or the flight plan has not been deserialized. virtual FlightPlan& flight_plan() const; - // Construct a new driver for the given metric (but doesn't start it). If + // Constructs a new driver for the given metric (but doesn't start it). If // there is a driver currently running it is interrupted and destroyed. virtual void MakeFlightPlanOptimizationDriver( FlightPlanOptimizer::MetricFactory metric_factory); diff --git a/ksp_plugin_adapter/flight_planner.cs b/ksp_plugin_adapter/flight_planner.cs index 808006a4a0..3122607a59 100644 --- a/ksp_plugin_adapter/flight_planner.cs +++ b/ksp_plugin_adapter/flight_planner.cs @@ -1,5 +1,4 @@ -using Steamworks; -using System; +using System; using System.Collections.Generic; using System.Linq; @@ -386,7 +385,6 @@ private void RenderFlightPlan(string vessel_guid) { Centre() is CelestialBody centre) { Style.HorizontalLine(); using (new UnityEngine.GUILayout.HorizontalScope()) { - ////valign UnityEngine.GUILayout.Label( L10N.CelestialString("#Principia_FlightPlan_Optimization", new[]{ centre }), @@ -458,7 +456,7 @@ private void RenderFlightPlan(string vessel_guid) { } // If any of the parameters changed (that includes a change of - // plotting frame in another window) recreated the optimization + // plotting frame in another window), recreate the optimization // driver. This interrupts any optimization that might be // running, to avoid confusing results. if (optimization_altitude_ != optimization_altitude || From 6df09c7f6b709d35f7ea43c80a59fecc7d9c55c2 Mon Sep 17 00:00:00 2001 From: pleroy Date: Sun, 15 Oct 2023 15:39:16 +0200 Subject: [PATCH 8/9] Lint. --- ksp_plugin/interface_flight_plan.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/ksp_plugin/interface_flight_plan.cpp b/ksp_plugin/interface_flight_plan.cpp index 80da57a7e5..75e81ffb4a 100644 --- a/ksp_plugin/interface_flight_plan.cpp +++ b/ksp_plugin/interface_flight_plan.cpp @@ -1,5 +1,6 @@ #include "ksp_plugin/interface.hpp" +#include #include #include "base/not_null.hpp" From 62bbee8949b3d71495ed49992558af88bb419757 Mon Sep 17 00:00:00 2001 From: pleroy Date: Sun, 15 Oct 2023 16:29:20 +0200 Subject: [PATCH 9/9] Reorder. --- ksp_plugin/interface_flight_plan.cpp | 167 +++++++++++++-------------- 1 file changed, 83 insertions(+), 84 deletions(-) diff --git a/ksp_plugin/interface_flight_plan.cpp b/ksp_plugin/interface_flight_plan.cpp index 75e81ffb4a..405d2b4e39 100644 --- a/ksp_plugin/interface_flight_plan.cpp +++ b/ksp_plugin/interface_flight_plan.cpp @@ -221,6 +221,16 @@ bool __cdecl principia__FlightPlanExists( return m.Return(plugin->GetVessel(vessel_guid)->has_flight_plan()); } +double __cdecl principia__FlightPlanGetActualFinalTime( + Plugin const* const plugin, + char const* const vessel_guid) { + journal::Method m( + {plugin, vessel_guid}); + return m.Return( + ToGameTime(*plugin, + GetFlightPlan(*plugin, vessel_guid).actual_final_time())); +} + FlightPlanAdaptiveStepParameters __cdecl principia__FlightPlanGetAdaptiveStepParameters( Plugin const* const plugin, @@ -234,16 +244,6 @@ principia__FlightPlanGetAdaptiveStepParameters( flight_plan.generalized_adaptive_step_parameters())); } -double __cdecl principia__FlightPlanGetActualFinalTime( - Plugin const* const plugin, - char const* const vessel_guid) { - journal::Method m( - {plugin, vessel_guid}); - return m.Return( - ToGameTime(*plugin, - GetFlightPlan(*plugin, vessel_guid).actual_final_time())); -} - OrbitAnalysis* __cdecl principia__FlightPlanGetCoastAnalysis( Plugin const* const plugin, char const* const vessel_guid, @@ -410,6 +410,79 @@ int __cdecl principia__FlightPlanNumberOfSegments( return m.Return(GetFlightPlan(*plugin, vessel_guid).number_of_segments()); } +int __cdecl principia__FlightPlanOptimizationDriverInProgress( + Plugin const* const plugin, + char const* const vessel_guid) { + journal::Method m( + {plugin, vessel_guid}); + CHECK_NOTNULL(plugin); + auto& vessel = *plugin->GetVessel(vessel_guid); + auto const maybe_parameters = vessel.FlightPlanOptimizationDriverInProgress(); + if (maybe_parameters.has_value()) { + return m.Return(maybe_parameters->index); + } else { + return m.Return(-1); + } +} + +void __cdecl principia__FlightPlanOptimizationDriverMake( + Plugin const* const plugin, + char const* const vessel_guid, + double const distance, + double const* const inclination_in_degrees, + int const celestial_index, + NavigationFrameParameters const navigation_frame_parameters) { + journal::Method m( + {plugin, + vessel_guid, + distance, + inclination_in_degrees, + celestial_index, + navigation_frame_parameters}); + CHECK_NOTNULL(plugin); + auto& vessel = *plugin->GetVessel(vessel_guid); + const auto& celestial = plugin->GetCelestial(celestial_index); + + std::vector factories = { + FlightPlanOptimizer::ForCelestialDistance( + /*celestial=*/&celestial, + /*target_distance=*/distance * Metre), + FlightPlanOptimizer::ForΔv()}; + std::vector weights = {1, 1e3}; + if (inclination_in_degrees != nullptr) { + factories.push_back(FlightPlanOptimizer::ForInclination( + &celestial, + [plugin, navigation_frame_parameters]() { + return NewNavigationFrame(*plugin, navigation_frame_parameters); + }, + *inclination_in_degrees * Degree)); + weights.push_back(1e6); + } + + vessel.MakeFlightPlanOptimizationDriver( + FlightPlanOptimizer::LinearCombination(factories, weights)); + + return m.Return(); +} + +void __cdecl principia__FlightPlanOptimizationDriverStart( + Plugin const* const plugin, + char const* const vessel_guid, + int const manœuvre_index) { + journal::Method m( + {plugin, + vessel_guid, + manœuvre_index}); + CHECK_NOTNULL(plugin); + auto& vessel = *plugin->GetVessel(vessel_guid); + + vessel.StartFlightPlanOptimizationDriver( + {.index = manœuvre_index, + .Δv_tolerance = 1 * Micro(Metre) / Second}); + + return m.Return(); +} + Status* __cdecl principia__FlightPlanRebase(Plugin const* const plugin, char const* const vessel_guid, double const mass_in_tonnes) { @@ -579,80 +652,6 @@ int __cdecl principia__FlightPlanSelected(Plugin const* const plugin, return m.Return(plugin->GetVessel(vessel_guid)->selected_flight_plan_index()); } -int __cdecl principia__FlightPlanOptimizationDriverInProgress( - Plugin const* const plugin, - char const* const vessel_guid) { - journal::Method m( - {plugin, vessel_guid}); - CHECK_NOTNULL(plugin); - auto& vessel = *plugin->GetVessel(vessel_guid); - auto const maybe_parameters = vessel.FlightPlanOptimizationDriverInProgress(); - if (maybe_parameters.has_value()) { - return m.Return(maybe_parameters->index); - } else { - return m.Return(-1); - } -} - -void __cdecl principia__FlightPlanOptimizationDriverMake( - Plugin const* const plugin, - char const* const vessel_guid, - double const distance, - double const* const inclination_in_degrees, - int const celestial_index, - NavigationFrameParameters const navigation_frame_parameters) { - journal::Method m( - {plugin, - vessel_guid, - distance, - inclination_in_degrees, - celestial_index, - navigation_frame_parameters}); - CHECK_NOTNULL(plugin); - auto& vessel = *plugin->GetVessel(vessel_guid); - const auto& celestial = plugin->GetCelestial(celestial_index); - - std::vector factories = { - FlightPlanOptimizer::ForCelestialDistance( - /*celestial=*/&celestial, - /*target_distance=*/distance * Metre), - FlightPlanOptimizer::ForΔv()}; - std::vector weights = {1, 1e3}; - if (inclination_in_degrees != nullptr) { - factories.push_back(FlightPlanOptimizer::ForInclination( - &celestial, - [plugin, navigation_frame_parameters]() { - return NewNavigationFrame(*plugin, navigation_frame_parameters); - }, - *inclination_in_degrees * Degree)); - weights.push_back(1e6); - } - - vessel.MakeFlightPlanOptimizationDriver( - FlightPlanOptimizer::LinearCombination(factories, weights)); - - return m.Return(); -} - -// REMOVE BEFORE FLIGHT: Sort the functions. -void __cdecl principia__FlightPlanOptimizationDriverStart( - Plugin const* const plugin, - char const* const vessel_guid, - int const manœuvre_index) { - journal::Method m( - {plugin, - vessel_guid, - manœuvre_index}); - CHECK_NOTNULL(plugin); - auto& vessel = *plugin->GetVessel(vessel_guid); - - vessel.StartFlightPlanOptimizationDriver( - {.index = manœuvre_index, - .Δv_tolerance = 1 * Micro(Metre) / Second}); - - return m.Return(); -} - Status* __cdecl principia__FlightPlanSetAdaptiveStepParameters( Plugin const* const plugin, char const* const vessel_guid,