18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 11:47:06 +01:00
Files
maszyna/widgets/vehicleparams.cpp
T
Hirek 22ebc41d70 refactor(physics): the parts of the main circuit keep their state to themselves
The line breaker, the line contactors, the protective relays and the converter
(physics/components/maincircuit.h) become classes: what the .fiz defines is in
their public `config`, their state is private, read through queries
(IsClosed(), UndervoltageHolds(), IsRunning()) and changed through operations
(Close(), ResetGround(), Switch()). The logic that concerns only them moves in:
the initialisation of the main circuit, the ground relay, the pressure switch,
the resets of the relays, and the whole step of the converter, which gets what
it needs from the vehicle as plain values (ConverterSupply).

The vehicle still decides when they act (the interlocks, the controller
positions, the voltages); the parts don't take a physics::Vehicle.

No change in behaviour: the golden traces recorded on dfb43c4f match bit for bit.
2026-11-29 09:37:01 +01:00

306 lines
14 KiB
C++

#include "stdafx.h"
#include "widgets/vehicleparams.h"
#include "simulation/simulation.h"
#include "application/driveruipanels.h"
#include "vehicle/Driver.h"
#include "vehicle/Train.h"
#include "physics/vehicle.h"
ui::vehicleparams_panel::vehicleparams_panel(const std::string &vehicle) : ui_panel(std::string(STR("Vehicle parameters")) + ": " + vehicle, false), m_vehicle_name(vehicle)
{
vehicle_mini = GfxRenderer->Fetch_Texture("vehicle_mini");
}
void screen_window_callback(ImGuiSizeCallbackData *data)
{
auto config = static_cast<const global_settings::pythonviewport_config *>(data->UserData);
data->DesiredSize.y = data->DesiredSize.x * (float)config->size.y / (float)config->size.x;
}
void ui::vehicleparams_panel::draw_infobutton(const char *str, ImVec2 pos, const ImVec4 color)
{
if (pos.x != -1.0f)
{
ImVec2 window_size = ImGui::GetWindowSize();
ImGuiStyle &style = ImGui::GetStyle();
ImVec2 text_size = ImGui::CalcTextSize(str);
ImVec2 button_size = ImVec2(text_size.x + style.FramePadding.x * 2.0f, text_size.y + style.FramePadding.y * 2.0f);
pos.x = pos.x * window_size.x / 512.0f - button_size.x / 2.0f;
pos.y = pos.y * window_size.y / 118.0f - button_size.y / 2.0f;
ImGui::SetCursorPos(pos);
}
if ((color.x + color.y + color.z) / 3.0f < 0.5f)
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
else
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.0f, 0.0f, 0.0f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_Button, color);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, color);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, color);
ImGui::Button(str);
ImGui::SameLine();
ImGui::PopStyleColor(4);
}
void ui::vehicleparams_panel::draw_mini(const physics::Vehicle &physics)
{
if (vehicle_mini == null_handle)
return;
auto &tex = GfxRenderer->Texture(vehicle_mini);
tex.create();
ImVec2 size = ImGui::GetContentRegionAvail();
float x = size.x;
float y = x * ((float)tex.get_height() / tex.get_width());
if (ImGui::BeginChild("mini", ImVec2(x, y)))
{
ImGui::Image((ImTextureID)(intptr_t)(tex.get_id()), ImVec2(x, y), ImVec2(0, 1), ImVec2(1, 0));
if (physics.pantographs[VehicleEnd::rear].is_active)
draw_infobutton("╨╨╨", ImVec2(126, 10));
if (physics.pantographs[VehicleEnd::front].is_active)
draw_infobutton("╨╨╨", ImVec2(290, 10));
if (physics.lowVoltage.batteryOn)
draw_infobutton(STR_C("bat."), ImVec2(120, 55));
if (physics.lineBreaker.IsClosed())
draw_infobutton(STR_C("main."));
if (physics.converter.IsRunning())
draw_infobutton(STR_C("conv."));
if (physics.air.compressor.running)
draw_infobutton(STR_C("comp."));
if (physics.WarningSignal)
draw_infobutton(STR_C("horn"), ImVec2(361, 11));
if (physics.iLights[VehicleEnd::front] & light::redmarker_left)
draw_infobutton("o", ImVec2(490, 71), ImVec4(1.0f, 0.0f, 0.0f, 1.0f));
else if (physics.iLights[VehicleEnd::front] & light::headlight_left)
draw_infobutton("O", ImVec2(490, 71), ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
if (physics.iLights[VehicleEnd::front] & light::redmarker_right)
draw_infobutton("o", ImVec2(443, 71), ImVec4(1.0f, 0.0f, 0.0f, 1.0f));
else if (physics.iLights[VehicleEnd::front] & light::headlight_right)
draw_infobutton("O", ImVec2(443, 71), ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
if (physics.iLights[VehicleEnd::front] & light::headlight_upper)
draw_infobutton("O", ImVec2(467, 18), ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
}
ImGui::EndChild();
}
void ui::vehicleparams_panel::render_contents()
{
TDynamicObject *vehicle_ptr = simulation::Vehicles.find(m_vehicle_name);
if (!vehicle_ptr)
{
is_open = false;
return;
}
TTrain *train_ptr = simulation::Trains.find(m_vehicle_name);
if (train_ptr)
{
const TTrain::screenentry_sequence &screens = train_ptr->get_screens();
for (const auto &viewport : Global.python_viewports)
{
for (auto const &entry : screens)
{
if (entry.script != viewport.surface)
continue;
std::string window_name = STR("Screen") + "##" + viewport.surface;
ImGui::SetNextWindowSizeConstraints(ImVec2(200, 200), ImVec2(2500, 2500), screen_window_callback, const_cast<global_settings::pythonviewport_config *>(&viewport));
if (ImGui::Begin(window_name.c_str()))
{
float aspect = (float)viewport.size.y / viewport.size.x;
glm::mat3 proj = glm::translate(glm::scale(glm::mat3(), 1.0f / viewport.scale), viewport.offset);
glm::vec2 uv0 = glm::vec2(proj * glm::vec3(0.0f, 1.0f, 1.0f));
glm::vec2 uv1 = glm::vec2(proj * glm::vec3(1.0f, 0.0f, 1.0f));
ImVec2 size = ImGui::GetContentRegionAvail();
ImGui::Image((ImTextureID)(intptr_t)(entry.rt->shared_tex), size, ImVec2(uv0.x, uv0.y), ImVec2(uv1.x, uv1.y));
}
ImGui::End();
}
}
}
TDynamicObject &vehicle = *vehicle_ptr;
physics::Vehicle &physics = *vehicle.physics;
std::vector<text_line> lines;
std::array<char, 1024> buffer;
auto const isdieselenginepowered{(physics.EngineType == TEngineType::DieselElectric) || (physics.EngineType == TEngineType::DieselEngine)};
auto const isdieselinshuntmode{physics.ShuntMode && physics.EngineType == TEngineType::DieselElectric};
std::snprintf(buffer.data(), buffer.size(), STR_C("Devices: %c%c%c%c%c%c%c%c%c%c%c%c%c%c%s%s\nPower transfers: %.0f@%.0f%s%s%s%.0f@%.0f"),
// devices
(physics.lowVoltage.batteryOn ? 'B' : '.'), (physics.lineBreaker.IsClosed() ? 'M' : '.'), (physics.FuseFlag ? '!' : '.'), (physics.pantographs[VehicleEnd::rear].is_active ? (physics.pantographCircuit.rearVoltage > 0.0 ? 'O' : 'o') : '.'),
(physics.pantographs[VehicleEnd::front].is_active ? (physics.pantographCircuit.frontVoltage > 0.0 ? 'P' : 'p') : '.'), (physics.pantographAir.pressureLockActive ? '!' : (physics.pantographAir.pressureSwitchActive ? '*' : '.')),
(physics.WaterPump.is_active ? 'W' : (false == physics.WaterPump.breaker ? '-' : (physics.WaterPump.is_enabled ? 'w' : '.'))),
(true == physics.WaterHeater.is_damaged ? '!' : (physics.WaterHeater.is_active ? 'H' : (false == physics.WaterHeater.breaker ? '-' : (physics.WaterHeater.is_enabled ? 'h' : '.')))),
(physics.FuelPump.is_active ? 'F' : (physics.FuelPump.is_enabled ? 'f' : '.')), (physics.OilPump.is_active ? 'O' : (physics.OilPump.is_enabled ? 'o' : '.')),
(false == physics.converter.LocalSwitchOn() ? '-' : (physics.converter.IsAllowed() ? (physics.converter.IsRunning() ? 'X' : 'x') : '.')), (physics.converter.IsOverloaded() ? '!' : '.'),
(physics.air.compressor.running ? 'C' : (false == physics.air.compressor.allowLocal ? '-' : ((physics.air.compressor.allow || physics.air.compressor.start == start_t::automatic) ? 'c' : '.'))),
(physics.air.compressor.governorLock ? '!' : '.'), "", std::string(isdieselenginepowered ? STR(" oil pressure: ") + to_string(physics.OilPump.pressure, 2) : "").c_str(),
// power transfers
physics.Couplers[VehicleEnd::front].power_high.voltage, physics.Couplers[VehicleEnd::front].power_high.current, std::string(physics.Couplers[VehicleEnd::front].power_high.is_local ? "" : "-").c_str(),
std::string(vehicle.DirectionGet() ? ":<<:" : ":>>:").c_str(), std::string(physics.Couplers[VehicleEnd::rear].power_high.is_local ? "" : "-").c_str(),
physics.Couplers[VehicleEnd::rear].power_high.voltage, physics.Couplers[VehicleEnd::rear].power_high.current);
ImGui::TextUnformatted(buffer.data());
std::snprintf(buffer.data(), buffer.size(),
STR_C("Controllers:\n master: %d(%d), secondary: %s\nEngine output: %.1f, current: %.0f\nRevolutions:\n engine: %.0f, motors: %.0f\n engine fans: %.0f, motor fans: %.0f+%.0f, "
"cooling fans: %.0f+%.0f"),
// controllers
physics.MainCtrlPos, physics.MainCtrlActualPos,
std::string(isdieselinshuntmode ? to_string(physics.AnPos, 2) + STR(" (shunt mode)") : std::to_string(physics.ScndCtrlPos) + "(" + std::to_string(physics.ScndCtrlActualPos) + ")").c_str(),
// engine
physics.EnginePower, std::abs(physics.TrainType == train_type::emu ? physics.ShowCurrent(0) : physics.Im),
// revolutions
std::abs(physics.enrot) * 60, std::abs(physics.nrot) * physics.Transmision.Ratio * 60, physics.RventRot * 60, std::abs(physics.MotorBlowers[VehicleEnd::front].revolutions),
std::abs(physics.MotorBlowers[VehicleEnd::rear].revolutions), physics.diesel.heat.rpmw, physics.diesel.heat.rpmw2);
ImGui::TextUnformatted(buffer.data());
if (isdieselenginepowered)
{
std::snprintf(buffer.data(), buffer.size(), STR_C("\nTemperatures:\n engine: %.2f, oil: %.2f, water: %.2f%c%.2f"), physics.diesel.heat.Ts, physics.diesel.heat.To, physics.diesel.heat.temperatura1,
(physics.WaterCircuitsLink ? '-' : '|'), physics.diesel.heat.temperatura2);
ImGui::TextUnformatted(buffer.data());
}
std::string brakedelay;
{
std::vector<std::pair<int, std::string>> delays{{bdelay_G, "G"}, {bdelay_P, "P"}, {bdelay_R, "R"}, {bdelay_M, "+Mg"}};
for (auto const &delay : delays)
{
if ((physics.brakes.delaySetting & delay.first) == delay.first)
{
brakedelay += delay.second;
}
}
}
std::snprintf(buffer.data(), buffer.size(),
STR_C("Brakes:\n train: %.2f, independent: %.2f, mode: %d, delay: %s, load flag: %d\nBrake cylinder pressures:\n train: %.2f, independent: %.2f, status: 0x%.2x\nPipe pressures:\n "
"brake: %.2f (hat: %.2f), main: %.2f, control: %.2f\nTank pressures:\n auxiliary: %.2f, main: %.2f, control: %.2f"),
// brakes
physics.brakes.handlePositionSmooth, physics.brakes.independentPosition, physics.brakes.operatingMode, brakedelay.c_str(), physics.brakes.loadSetting,
// cylinders
physics.brakes.cylinderPressure, physics.brakes.independentCylinderPressure, physics.brakes.distributor->GetBrakeStatus(),
// pipes
physics.brakePipe.pressure, physics.brakes.handleCapPosition, physics.mainPipe.pressure, physics.brakes.controlReservoirPressure,
// tanks
physics.brakes.distributor->GetBRP(), physics.air.mainReservoirPressure, physics.brakes.distributor->GetCRP());
ImGui::TextUnformatted(buffer.data());
if (physics.EnginePowerSource.SourceType == TPowerSource::CurrentCollector)
{
std::snprintf(buffer.data(), buffer.size(), STR_C(" pantograph: %.2f%cMT"), physics.pantographAir.pressure, (physics.pantographAir.connectedToMainReservoir ? '-' : '|'));
ImGui::TextUnformatted(buffer.data());
}
std::snprintf(buffer.data(), buffer.size(),
STR_C("Forces:\n tractive: %.1f, brake: %.1f, friction: %.2f%s\nAcceleration:\n tangential: %.2f, normal: %.2f (path radius: %s)\nVelocity: %.2f, distance traveled: %.2f\nPosition: "
"[%.2f, %.2f, %.2f]"),
// forces
physics.Ft * 0.001f *
(physics.CabActive ? physics.CabActive :
vehicle.ctOwner ? vehicle.ctOwner->Controlling()->CabActive :
1) +
0.001f,
physics.Fb * 0.001f, physics.Adhesive(physics.RunningTrack.friction), (physics.SlippingWheels ? " (!)" : ""),
// acceleration
physics.AccSVBased, physics.AccN + 0.001f, std::string(std::abs(physics.RunningShape.R) > 10000.0 ? "~0" : to_string(physics.RunningShape.R, 0)).c_str(),
// velocity
vehicle.GetVelocity(), physics.DistCounter,
// position
vehicle.GetPosition().x, vehicle.GetPosition().y, vehicle.GetPosition().z);
ImGui::TextUnformatted(buffer.data());
std::pair<double, double> TrainsetPowerMeter;
TDynamicObject *vehicle_iter = vehicle_ptr;
while (vehicle_iter)
{
if (vehicle_iter->Next())
vehicle_iter = vehicle_iter->Next();
else
break;
}
while (vehicle_iter)
{
TrainsetPowerMeter.first += vehicle_iter->physics->EnergyMeter.first;
TrainsetPowerMeter.second += vehicle_iter->physics->EnergyMeter.second;
vehicle_iter = vehicle_iter->Prev();
}
if (TrainsetPowerMeter.first != 0.0 || TrainsetPowerMeter.second != 0.0)
{
std::snprintf(buffer.data(), buffer.size(), STR_C("Electricity usage:\n drawn: %.1f kWh\n returned: %.1f kWh\n balance: %.1f kWh"), TrainsetPowerMeter.first, -TrainsetPowerMeter.second,
TrainsetPowerMeter.first + TrainsetPowerMeter.second);
ImGui::TextUnformatted(buffer.data());
}
draw_mini(physics);
if (ImGui::Button(STR_C("Radiostop")))
m_relay.post(user_command::radiostopsend, 0.0, 0.0, GLFW_PRESS, 0, vehicle_ptr->GetPosition());
ImGui::SameLine();
if (ImGui::Button(STR_C("Stop and repair")))
m_relay.post(user_command::resetconsist, 0.0, 0.0, GLFW_PRESS, 0, glm::vec3(0.0f), &vehicle_ptr->name());
ImGui::SameLine();
if (ImGui::Button(STR_C("Reset position")))
{
std::string payload = vehicle_ptr->name() + '%' + vehicle_ptr->initial_track->name();
m_relay.post(user_command::consistteleport, 0.0, 0.0, GLFW_PRESS, 0, glm::vec3(0.0f), &payload);
m_relay.post(user_command::resetconsist, 0.0, 0.0, GLFW_PRESS, 0, glm::vec3(0.0f), &vehicle_ptr->name());
}
if (ImGui::Button(STR_C("Refill main tank")))
m_relay.post(user_command::fillcompressor, 0.0, 0.0, GLFW_PRESS, 0, glm::vec3(0.0f), &vehicle_ptr->name());
ImGui::SameLine();
if (ImGui::Button(STR_C("Rupture main pipe")))
m_relay.post(user_command::pullalarmchain, 0.0, 0.0, GLFW_PRESS, 0, glm::vec3(0.0f), &vehicle_ptr->name());
ImGui::SameLine();
ImGui::Button(STR_C("independent brake releaser"));
if (ImGui::IsItemClicked())
m_relay.post(user_command::consistreleaser, 0.0, 0.0, GLFW_PRESS, 0, glm::vec3(0.0f), &vehicle_ptr->name());
if (ImGui::IsItemDeactivated())
m_relay.post(user_command::consistreleaser, 0.0, 0.0, GLFW_RELEASE, 0, glm::vec3(0.0f), &vehicle_ptr->name());
if (vehicle_ptr->physics->V < 0.01)
{
if (ImGui::Button(STR_C("Move +500m")))
m_relay.post(user_command::dynamicmove, 500.0, 0.0, GLFW_PRESS, 0, glm::vec3(0.0f), &vehicle_ptr->name());
ImGui::SameLine();
if (ImGui::Button(STR_C("Move -500m")))
m_relay.post(user_command::dynamicmove, -500.0, 0.0, GLFW_PRESS, 0, glm::vec3(0.0f), &vehicle_ptr->name());
}
}