Skip to content

Commit

Permalink
Merge pull request #3477 from pleroy/3471
Browse files Browse the repository at this point in the history
Extend the target vessel prediction when the flight plan gets extended
  • Loading branch information
pleroy authored Dec 18, 2022
2 parents e949e43 + 8af84d1 commit 977a315
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 29 deletions.
52 changes: 30 additions & 22 deletions ksp_plugin/interface_flight_plan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,6 @@ NavigationManoeuvre ToInterfaceNavigationManoeuvre(

} // namespace

Status* __cdecl principia__FlightPlanInsert(Plugin const* const plugin,
char const* const vessel_guid,
Burn const burn,
int const index) {
journal::Method<journal::FlightPlanInsert> m(
{plugin, vessel_guid, burn, index});
CHECK_NOTNULL(plugin);
return m.Return(
ToNewStatus(GetFlightPlan(*plugin, vessel_guid)
.Insert(FromInterfaceBurn(*plugin, burn), index)));
}

int __cdecl principia__FlightPlanCount(Plugin const* const plugin,
char const* const vessel_guid) {
journal::Method<journal::FlightPlanCount> m({plugin, vessel_guid});
Expand Down Expand Up @@ -360,6 +348,19 @@ principia__FlightPlanGetManoeuvreFrenetTrihedron(Plugin const* const plugin,
return m.Return(result);
}

Status* __cdecl principia__FlightPlanInsert(Plugin const* const plugin,
char const* const vessel_guid,
Burn const burn,
int const index) {
journal::Method<journal::FlightPlanInsert> m(
{plugin, vessel_guid, burn, index});
CHECK_NOTNULL(plugin);
auto const status = GetFlightPlan(*plugin, vessel_guid)
.Insert(FromInterfaceBurn(*plugin, burn), index);
plugin->ExtendPredictionForFlightPlan(vessel_guid);
return m.Return(ToNewStatus(status));
}

int __cdecl principia__FlightPlanNumberOfAnomalousManoeuvres(
Plugin const* const plugin,
char const* const vessel_guid) {
Expand Down Expand Up @@ -396,6 +397,7 @@ Status* __cdecl principia__FlightPlanRebase(Plugin const* const plugin,
CHECK_NOTNULL(plugin);
auto const status =
plugin->GetVessel(vessel_guid)->RebaseFlightPlan(mass_in_tonnes * Tonne);
plugin->ExtendPredictionForFlightPlan(vessel_guid);
return m.Return(ToNewStatus(status));
}

Expand All @@ -404,8 +406,9 @@ Status* __cdecl principia__FlightPlanRemove(Plugin const* const plugin,
int const index) {
journal::Method<journal::FlightPlanRemove> m({plugin, vessel_guid, index});
CHECK_NOTNULL(plugin);
return m.Return(
ToNewStatus(GetFlightPlan(*plugin, vessel_guid).Remove(index)));
auto const status = GetFlightPlan(*plugin, vessel_guid).Remove(index);
plugin->ExtendPredictionForFlightPlan(vessel_guid);
return m.Return(ToNewStatus(status));
}

void __cdecl principia__FlightPlanRenderedApsides(
Expand Down Expand Up @@ -533,9 +536,10 @@ Status* __cdecl principia__FlightPlanReplace(Plugin const* const plugin,
burn,
index});
CHECK_NOTNULL(plugin);
return m.Return(
ToNewStatus(GetFlightPlan(*plugin, vessel_guid)
.Replace(FromInterfaceBurn(*plugin, burn), index)));
auto const status = GetFlightPlan(*plugin, vessel_guid)
.Replace(FromInterfaceBurn(*plugin, burn), index);
plugin->ExtendPredictionForFlightPlan(vessel_guid);
return m.Return(ToNewStatus(status));
}

void __cdecl principia__FlightPlanSelect(Plugin const* const plugin,
Expand Down Expand Up @@ -564,9 +568,11 @@ Status* __cdecl principia__FlightPlanSetAdaptiveStepParameters(
CHECK_NOTNULL(plugin);
auto const parameters = FromFlightPlanAdaptiveStepParameters(
flight_plan_adaptive_step_parameters);
return m.Return(ToNewStatus(
auto const status =
GetFlightPlan(*plugin, vessel_guid)
.SetAdaptiveStepParameters(parameters.first, parameters.second)));
.SetAdaptiveStepParameters(parameters.first, parameters.second);
plugin->ExtendPredictionForFlightPlan(vessel_guid);
return m.Return(ToNewStatus(status));
}

Status* __cdecl principia__FlightPlanSetDesiredFinalTime(
Expand All @@ -577,9 +583,11 @@ Status* __cdecl principia__FlightPlanSetDesiredFinalTime(
vessel_guid,
final_time});
CHECK_NOTNULL(plugin);
return m.Return(
ToNewStatus(GetFlightPlan(*plugin, vessel_guid)
.SetDesiredFinalTime(FromGameTime(*plugin, final_time))));
auto const status =
GetFlightPlan(*plugin, vessel_guid)
.SetDesiredFinalTime(FromGameTime(*plugin, final_time));
plugin->ExtendPredictionForFlightPlan(vessel_guid);
return m.Return(ToNewStatus(status));
}

} // namespace interface
Expand Down
50 changes: 45 additions & 5 deletions ksp_plugin/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,13 +895,31 @@ void Plugin::SetPredictionAdaptiveStepParameters(
// If there is a target vessel, it is integrated with the same parameters as
// the given (current) vessel. This makes it possible to plot the prediction
// of the current vessel.
auto& vessel = *FindOrDie(vessels_, vessel_guid);
if (renderer_->HasTargetVessel()) {
renderer_->GetTargetVessel().set_prediction_adaptive_step_parameters(
prediction_adaptive_step_parameters);
}
FindOrDie(vessels_, vessel_guid)
->set_prediction_adaptive_step_parameters(
auto& target_vessel = renderer_->GetTargetVessel();
if (vessel.has_flight_plan()) {
// If there is a target vessel and our vessel has a flight plan, make sure
// that we don't shorten the target vessel's prediction as it would hurt
// our flight plan.
auto const target_prediction_adaptive_step_parameters =
target_vessel.prediction_adaptive_step_parameters();
target_vessel.set_prediction_adaptive_step_parameters(
Ephemeris<Barycentric>::AdaptiveStepParameters(
prediction_adaptive_step_parameters.integrator(),
std::max(target_prediction_adaptive_step_parameters.max_steps(),
prediction_adaptive_step_parameters.max_steps()),
prediction_adaptive_step_parameters
.length_integration_tolerance(),
prediction_adaptive_step_parameters
.speed_integration_tolerance()));
} else {
target_vessel.set_prediction_adaptive_step_parameters(
prediction_adaptive_step_parameters);
}
}
vessel.set_prediction_adaptive_step_parameters(
prediction_adaptive_step_parameters);
}

void Plugin::UpdatePrediction(std::vector<GUID> const& vessel_guids) const {
Expand Down Expand Up @@ -947,6 +965,28 @@ void Plugin::CreateFlightPlan(GUID const& vessel_guid,
DefaultBurnParameters());
}

void Plugin::ExtendPredictionForFlightPlan(GUID const& vessel_guid) const {
auto& vessel = *FindOrDie(vessels_, vessel_guid);

// If there is a target vessel, and the prediction of the target is too short
// for the flight plan, multiply the number of steps by 4 (one notch in the
// UI). We don't wait for the prediction to be computed, though, so there is
// no guarantee that it will be long enough. Presumably the user will keep
// increasing the flight plan length and get what they want, ultimately.
if (renderer_->HasTargetVessel() && vessel.has_flight_plan()) {
auto& target_vessel = renderer_->GetTargetVessel();
if (target_vessel.prediction()->back().time <
vessel.flight_plan().actual_final_time()) {
auto prediction_adaptive_step_parameters =
target_vessel.prediction_adaptive_step_parameters();
prediction_adaptive_step_parameters.set_max_steps(
prediction_adaptive_step_parameters.max_steps() * 4);
target_vessel.set_prediction_adaptive_step_parameters(
prediction_adaptive_step_parameters);
}
}
}

void Plugin::ComputeAndRenderApsides(
Index const celestial_index,
Trajectory<Barycentric> const& trajectory,
Expand Down
4 changes: 4 additions & 0 deletions ksp_plugin/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ class Plugin {
Instant const& final_time,
Mass const& initial_mass) const;

// If there is a target vessel, increases the size of the prediction to try
// and cover the actual final time of the flight plan.
virtual void ExtendPredictionForFlightPlan(GUID const& vessel_guid) const;

// Computes the apsides of the trajectory defined by |begin| and |end| with
// respect to the celestial with index |celestial_index|.
virtual void ComputeAndRenderApsides(
Expand Down
3 changes: 3 additions & 0 deletions ksp_plugin_test/interface_flight_plan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ using quantities::si::Second;
using quantities::si::Tonne;
using testing_utilities::AlmostEquals;
using ::testing::AllOf;
using ::testing::AnyNumber;
using ::testing::ByMove;
using ::testing::DoAll;
using ::testing::Invoke;
Expand Down Expand Up @@ -147,6 +148,8 @@ TEST_F(InterfaceFlightPlanTest, FlightPlan) {
.WillRepeatedly(Return(true));
EXPECT_CALL(vessel, flight_plan())
.WillRepeatedly(ReturnRef(flight_plan));
EXPECT_CALL(*plugin_, ExtendPredictionForFlightPlan(vessel_guid))
.Times(AnyNumber());

EXPECT_TRUE(principia__FlightPlanExists(plugin_.get(), vessel_guid));

Expand Down
5 changes: 5 additions & 0 deletions ksp_plugin_test/mock_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class MockPlugin : public Plugin {
Mass const& initial_mass),
(const, override));

MOCK_METHOD(void,
ExtendPredictionForFlightPlan,
(GUID const& vessel_guid),
(const, override));

MOCK_METHOD(void,
SetPredictionAdaptiveStepParameters,
(GUID const& vessel_guid,
Expand Down
2 changes: 1 addition & 1 deletion numerics/global_optimization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class MultiLevelSingleLinkage {

Box const box_;
NormType const box_diametre_;
Box::Measure const box_measure_;
typename Box::Measure const box_measure_;
Field<Scalar, Argument> const f_;
Field<Gradient<Scalar, Argument>, Argument> const grad_f_;

Expand Down
2 changes: 1 addition & 1 deletion numerics/global_optimization_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using quantities::Pow;
constexpr int64_t pcp_tree_max_values_per_cell = 20;

template<typename Scalar, typename Argument, int dimensions>
MultiLevelSingleLinkage<Scalar, Argument, dimensions>::Box::Measure
typename MultiLevelSingleLinkage<Scalar, Argument, dimensions>::Box::Measure
MultiLevelSingleLinkage<Scalar, Argument, dimensions>::Box::measure() const {
if constexpr (dimensions == 1) {
return 2 * vertices[0].Norm();
Expand Down

0 comments on commit 977a315

Please sign in to comment.