Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep the prediction parameters of the vessels that have disappeared #2455

Merged
merged 3 commits into from Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 23 additions & 5 deletions ksp_plugin/plugin.cpp
Expand Up @@ -370,20 +370,28 @@ void Plugin::InsertOrKeepVessel(GUID const& vessel_guid,
CHECK(!initializing_);
not_null<Celestial const*> parent =
FindOrDie(celestials_, parent_index).get();
auto it = vessels_.find(vessel_guid);
if (it == vessels_.end()) {
std::tie(it, inserted) =
auto vit = vessels_.find(vessel_guid);
if (vit == vessels_.end()) {
// Restore the zombie parameters if we have some, otherwise use the default.
auto prediction_parameters = DefaultPredictionParameters();
auto const pit =
zombie_prediction_adaptive_step_parameters_.find(vessel_guid);
if (pit != zombie_prediction_adaptive_step_parameters_.end()) {
prediction_parameters = pit->second;
zombie_prediction_adaptive_step_parameters_.erase(pit);
}
std::tie(vit, inserted) =
vessels_.emplace(
vessel_guid,
make_not_null_unique<Vessel>(vessel_guid,
vessel_name,
parent,
ephemeris_.get(),
DefaultPredictionParameters()));
prediction_parameters));
} else {
inserted = false;
}
not_null<Vessel*> const vessel = it->second.get();
not_null<Vessel*> const vessel = vit->second.get();
if (vessel->name() != vessel_name) {
vessel->set_name(vessel_name);
}
Expand Down Expand Up @@ -538,6 +546,8 @@ void Plugin::FreeVesselsAndPartsAndCollectPileUps(Time const& Δt) {
loaded_vessels_.erase(vessel);
LOG(INFO) << "Removing vessel " << vessel->ShortDebugString();
renderer_->ClearTargetVesselIf(vessel);
zombie_prediction_adaptive_step_parameters_.insert_or_assign(
vessel->guid(), vessel->prediction_adaptive_step_parameters());
it = vessels_.erase(it);
}
}
Expand Down Expand Up @@ -579,6 +589,8 @@ void Plugin::FreeVesselsAndPartsAndCollectPileUps(Time const& Δt) {
loaded_vessels_.erase(vessel);
LOG(INFO) << "Removing grounded vessel " << vessel->ShortDebugString();
renderer_->ClearTargetVesselIf(vessel);
zombie_prediction_adaptive_step_parameters_.insert_or_assign(
vessel->guid(), vessel->prediction_adaptive_step_parameters());
CHECK_EQ(vessels_.erase(vessel->guid()), 1);
}
}
Expand Down Expand Up @@ -1272,6 +1284,12 @@ void Plugin::WriteToMessage(
not_null<Vessel*> const vessel = pair.second;
(*message->mutable_part_id_to_vessel())[part_id] = vessel_to_guid[vessel];
}
for (auto const& [guid, parameters] :
zombie_prediction_adaptive_step_parameters_) {
auto* const zombie_message = message->add_zombie();
zombie_message->set_guid(guid);
parameters.WriteToMessage(zombie_message->mutable_prediction_parameters());
}

ephemeris_->WriteToMessage(message->mutable_ephemeris());

Expand Down
7 changes: 7 additions & 0 deletions ksp_plugin/plugin.hpp
Expand Up @@ -527,6 +527,13 @@ class Plugin {
VesselSet loaded_vessels_;
// The vessels that will be kept during the next call to |AdvanceTime|.
VesselConstSet kept_vessels_;
// Contains the adaptive step parameters for the vessel that existed in the
// past but are no longer known to the plugin. Useful to avoid losing the
// parameters, e.g., when a vessel hits the ground.
// NOTE(phl): This is a leaky map, in the sense that we don't remove deleted
// vessels from it. Hopefully it's small enough that we don't case.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tyop

std::map<GUID, Ephemeris<Barycentric>::AdaptiveStepParameters>
zombie_prediction_adaptive_step_parameters_;

friend class NavballFrameField;
friend class TestablePlugin;
Expand Down
15 changes: 10 additions & 5 deletions serialization/ksp_plugin.proto
Expand Up @@ -91,6 +91,12 @@ message PileUp {
}

message Plugin {
message CelestialParenthood {
required int32 index = 1;
optional int32 parent_index = 2;
// Added in Catalan.
optional int32 ephemeris_index = 3;
}
message VesselAndProperties {
required string guid = 1;
required Vessel vessel = 2;
Expand All @@ -101,11 +107,9 @@ message Plugin {
reserved 4;
reserved "dirty";
}
message CelestialParenthood {
required int32 index = 1;
optional int32 parent_index = 2;
// Added in Catalan.
optional int32 ephemeris_index = 3;
message ZombieAndProperties {
required string guid = 1;
required Ephemeris.AdaptiveStepParameters prediction_parameters = 2;
}
repeated VesselAndProperties vessel = 1;
map<fixed32, string> part_id_to_vessel = 16;
Expand All @@ -120,6 +124,7 @@ message Plugin {
optional DynamicFrame pre_cauchy_plotting_frame = 11;
repeated PileUp pile_up = 17;
optional Renderer renderer = 18; // Added in Cauchy.
repeated ZombieAndProperties zombie = 19; // Added in Frege.

// Pre-Cardano.
reserved 3;
Expand Down