18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 06:55:02 +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:
maj00r
2026-11-14 00:04:55 +01:00
parent 0488519f39
commit 5ca2f7e38f
7 changed files with 126 additions and 54 deletions

View File

@@ -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());