diff --git a/simulation/blackboxrecorder.cpp b/simulation/blackboxrecorder.cpp index ec59311ce..ff9ae2039 100644 --- a/simulation/blackboxrecorder.cpp +++ b/simulation/blackboxrecorder.cpp @@ -624,17 +624,15 @@ void blackbox_recorder::write_derailed(TDynamicObject const *Vehicle, derailment } void blackbox_recorder::look_at_derailments() { - for (auto *vehicle : simulation::Vehicles.sequence()) { - if (false == vehicle->bEnabled) - continue; + for (auto const &[vehicle, looked] : m_looked) { auto const &motion{vehicle->physics->OffTrack}; - auto const id{vehicle_id(vehicle)}; + auto &entry{*looked}; + auto const id{entry.id}; // the derailment itself first, if it came before the recording did; forgotten once back on the rails if (auto const *notice{vehicle->derailment_notice()}) write_derailed(vehicle, *notice); else - m_vehicles[vehicle].derailed = false; - auto &entry{m_vehicles[vehicle]}; + entry.derailed = false; if (false == motion.active()) { entry.derailment = 0; continue; @@ -667,10 +665,8 @@ void blackbox_recorder::look_at_derailments() { } void blackbox_recorder::look_at_couplers() { - for (auto *vehicle : simulation::Vehicles.sequence()) { - if (false == vehicle->bEnabled) - continue; - auto &entry{m_vehicles[vehicle]}; + for (auto const &[vehicle, looked] : m_looked) { + auto &entry{*looked}; auto const *physics{vehicle->physics}; for (int side = 0; side < 2; ++side) { auto const flags{physics->Couplers[side].CouplingFlag}; @@ -735,12 +731,14 @@ void blackbox_recorder::write_camera() { void blackbox_recorder::look_at_vehicles(bool const Keyframe) { std::vector states; + // the vehicles on the scenery, kept with their entries for the rest of the look + m_looked.clear(); for (auto *vehicle : simulation::Vehicles.sequence()) { if (false == vehicle->bEnabled) continue; - auto const id{vehicle_id(vehicle)}; - auto &entry{m_vehicles[vehicle]}; - auto const state{state_of(*vehicle, id)}; + auto &entry{entry_of(vehicle)}; + m_looked.emplace_back(vehicle, &entry); + auto const state{state_of(*vehicle, entry.id)}; auto const changed{false == entry.listed || glm::distance(state.position, entry.last.position) > listmove || std::abs(state.speed_kmh - entry.last.speed_kmh) > listspeed || state.driving != entry.last.driving}; if (Keyframe || changed) { @@ -812,18 +810,22 @@ void blackbox_recorder::look_at_timetable(TDynamicObject const &Vehicle, std::ui m_writer->timetable(data); } -std::uint32_t blackbox_recorder::vehicle_id(TDynamicObject const *Vehicle) { - if (auto const lookup{m_vehicles.find(Vehicle)}; lookup != m_vehicles.end()) - return lookup->second.id; - - auto &entry{m_vehicles[Vehicle]}; +blackbox_recorder::vehicle_entry &blackbox_recorder::entry_of(TDynamicObject const *Vehicle) { + auto const [lookup, fresh]{m_vehicles.try_emplace(Vehicle)}; + auto &entry{lookup->second}; + if (false == fresh) + return entry; entry.id = m_nextvehicle++; auto const *physics{Vehicle->physics}; auto const *driver{Vehicle->Mechanik}; auto const train{driver != nullptr && driver->TrainName() != "none" ? driver->TrainName() : std::string{}}; m_writer->vehicle({entry.id, text_of(Vehicle->name()), text_of(physics->TypeName), text_of(train), static_cast(physics->Dim.L), static_cast(physics->Dim.W), static_cast(physics->CategoryFlag)}); - return entry.id; + return entry; +} + +std::uint32_t blackbox_recorder::vehicle_id(TDynamicObject const *Vehicle) { + return entry_of(Vehicle).id; } std::uint32_t blackbox_recorder::known_id(TDynamicObject const *Vehicle) { diff --git a/simulation/blackboxrecorder.h b/simulation/blackboxrecorder.h index af635a409..252aedc15 100644 --- a/simulation/blackboxrecorder.h +++ b/simulation/blackboxrecorder.h @@ -115,6 +115,8 @@ private: void write_camera(); // the timetable of the train the user is in, written again when it is another one; none isn't written void look_at_timetable(TDynamicObject const &Vehicle, std::uint32_t Id); + // what is known of the vehicle, its number given (and the vehicle written) on first sight + vehicle_entry &entry_of(TDynamicObject const *Vehicle); // number of the vehicle, given (and the vehicle written) on first sight std::uint32_t vehicle_id(TDynamicObject const *Vehicle); // the number of a vehicle still on the scenery, no_vehicle for none or one taken off it @@ -135,6 +137,8 @@ private: double m_lookdue{0.0}; double m_keyframedue{0.0}; std::unordered_map m_vehicles; + // the vehicles of the current look, in their order, with their entries: each looked up once in a look + std::vector> m_looked; std::uint32_t m_nextvehicle{1}; std::vector m_switches; std::vector m_signals;