mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-20 09:15:01 +01:00
feat(blackbox): the black box on its own, writing the tapes of the user's vehicles at the end
"blackbox yes" in eu07.ini records the black box with the physics logs on or off, "blackbox no" never; with no entry it goes with "physicslog yes" as before. With the physics logs off, the recorder keeps the line of the physics log of the driver the user is with, every 0.05 s, and writes it to physicslog/<vehicle>.dat when the recording closes: the speed recorder's tape of the vehicles the user was in, without every vehicle keeping a log open and written twenty times a second. The line and the header of the log are made by one function of the driver, which its own log writes too, and the file is opened the same way, so the two are the same to the byte: the readers of these logs (the old Hasler tape viewer, the starter) read them as before.
This commit is contained in:
@@ -42,6 +42,7 @@ namespace {
|
||||
|
||||
// [s] between the samples of the user's train, the looks at everything else and the full looks at the vehicles
|
||||
constexpr double cabinterval{0.1};
|
||||
constexpr double tapeinterval{0.05}; // as the physics logs (deltalog of Driver.cpp)
|
||||
constexpr double lookinterval{1.0};
|
||||
constexpr double slowlook{20.0}; // [ms] a look around taking longer is told in the log, part by part
|
||||
constexpr double keyframeinterval{60.0};
|
||||
@@ -233,7 +234,8 @@ void blackbox_recorder::update(double const Deltatime) {
|
||||
if (false == simulation::is_ready)
|
||||
return;
|
||||
m_started = true;
|
||||
if (false == WriteLogFlag || false == Global.BlackBox || Global.editor_session)
|
||||
// "blackbox yes" or "no" of eu07.ini decides by itself, with no entry it goes with the physics logs
|
||||
if (false == Global.BlackBox.value_or(WriteLogFlag) || Global.editor_session)
|
||||
return;
|
||||
open();
|
||||
}
|
||||
@@ -246,6 +248,11 @@ void blackbox_recorder::update(double const Deltatime) {
|
||||
write_cab();
|
||||
m_cabdue = std::max(m_cabdue + cabinterval, m_time);
|
||||
}
|
||||
// with the physics logs on, each driver writes its own all the time
|
||||
if (false == WriteLogFlag && m_time >= m_tapedue) {
|
||||
keep_tape_line();
|
||||
m_tapedue = std::max(m_tapedue + tapeinterval, m_time);
|
||||
}
|
||||
if (m_time >= m_cameradue) {
|
||||
write_camera();
|
||||
m_cameradue = std::max(m_cameradue + camerainterval, m_time);
|
||||
@@ -273,6 +280,7 @@ void blackbox_recorder::close() {
|
||||
m_writer->end(m_time);
|
||||
m_writer.reset();
|
||||
m_file.close();
|
||||
write_tapes();
|
||||
using milliseconds = std::chrono::duration<double, std::milli>;
|
||||
WriteLog(std::format("Black box: recording closed; {} looks around, {:.3f} ms each on average, the longest {:.3f} ms", m_looks,
|
||||
m_looks > 0 ? milliseconds{m_looktime}.count() / m_looks : 0.0, milliseconds{m_looklongest}.count()));
|
||||
@@ -478,6 +486,38 @@ void blackbox_recorder::write_cab() {
|
||||
m_writer->cab(sample);
|
||||
}
|
||||
|
||||
void blackbox_recorder::keep_tape_line() {
|
||||
auto const *train{simulation::Train};
|
||||
if (train == nullptr || train->Dynamic() == nullptr)
|
||||
return;
|
||||
auto const *vehicle{train->Dynamic()};
|
||||
// the driver of the vehicle, whose log it would be: it keeps the name of its first vehicle, so a change of the cab
|
||||
// in a unit goes on in the same log. in a vehicle without one, the driver of its train
|
||||
auto const *driver{vehicle->Mechanik != nullptr ? vehicle->Mechanik : vehicle->ctOwner};
|
||||
if (driver == nullptr)
|
||||
return;
|
||||
m_tapes[driver->physicslog_name()] += driver->physicslog_line();
|
||||
}
|
||||
|
||||
void blackbox_recorder::write_tapes() {
|
||||
for (auto const &[name, lines] : m_tapes) {
|
||||
// opened as the driver opens its log, so the file is the same to the byte
|
||||
auto const filename{"physicslog/" + name + ".dat"};
|
||||
std::fstream file{filename, std::ios::in | std::ios::out | std::ios::trunc};
|
||||
if (false == file.is_open()) {
|
||||
ErrorLog(std::format("Black box: can't create the physics log \"{}\"", filename));
|
||||
continue;
|
||||
}
|
||||
file << TController::physicslog_header() << lines;
|
||||
file.close();
|
||||
if (file.fail())
|
||||
ErrorLog(std::format("Black box: the physics log \"{}\" wasn't written whole", filename));
|
||||
else
|
||||
WriteLog(std::format("Black box: the physics log \"{}\" written from the recording", filename));
|
||||
}
|
||||
m_tapes.clear();
|
||||
}
|
||||
|
||||
void blackbox_recorder::look_around(bool const Keyframe) {
|
||||
auto const &time{simulation::Time.data()};
|
||||
m_writer->clock(m_time, static_cast<std::uint16_t>(time.wYear), static_cast<std::uint16_t>(simulation::Time.year_day()), second_of_day());
|
||||
|
||||
@@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
@@ -86,6 +87,10 @@ private:
|
||||
void write_map();
|
||||
static std::vector<std::pair<glm::dvec3, float>> stop_point_headings();
|
||||
void write_cab();
|
||||
// with the physics logs off, the line of the log of the driver the user is with, kept for the end
|
||||
void keep_tape_line();
|
||||
// the kept lines, each driver's to its log, physicslog/<vehicle>.dat, as the physics logs would have it
|
||||
void write_tapes();
|
||||
// the clock, the user's vehicle, the vehicles, the switches and the signals
|
||||
void look_around(bool Keyframe);
|
||||
void look_at_vehicles(bool Keyframe);
|
||||
@@ -109,6 +114,9 @@ private:
|
||||
bool m_started{false}; // tried to start, made or not
|
||||
double m_time{0.0};
|
||||
double m_cabdue{0.0};
|
||||
double m_tapedue{0.0};
|
||||
// the lines of the physics log by the name of its file, of the times the user was with its driver
|
||||
std::map<std::string, std::string> m_tapes;
|
||||
double m_lookdue{0.0};
|
||||
double m_keyframedue{0.0};
|
||||
std::unordered_map<TDynamicObject const *, vehicle_entry> m_vehicles;
|
||||
|
||||
Reference in New Issue
Block a user