18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 05:44:59 +01:00

Merge branch 'master' into feat/renderers-cleanup

This commit is contained in:
2026-11-16 21:44:48 +01:00
264 changed files with 41203 additions and 10041 deletions

View File

@@ -40,6 +40,26 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/uart.h"
#endif
#include <format>
namespace {
// how close to a broken coupler the camera has to be for the driving aid to offer its repair [m]
constexpr float coupler_repair_radius { 5.f };
// the broken coupler nearest to the camera, within reach: the vehicle and its end, or no vehicle
std::pair<TDynamicObject const *, int> broken_coupler_nearby( glm::dvec3 const &Camera ) {
auto const *vehicle { std::get<TDynamicObject *>( simulation::Region->find_vehicle( Camera, coupler_repair_radius, false, true ) ) };
if( vehicle == nullptr || vehicle->MoverParameters == nullptr ) { return { nullptr, end::front }; }
auto const front { glm::distance( vehicle->CouplerPosition( end::front ), Camera ) };
auto const rear { glm::distance( vehicle->CouplerPosition( end::rear ), Camera ) };
auto const nearest { front <= rear ? end::front : end::rear };
if( std::min( front, rear ) > coupler_repair_radius || false == vehicle->MoverParameters->Couplers[ nearest ].broken ) { return { nullptr, end::front }; }
return { vehicle, nearest };
}
} // namespace
void
drivingaid_panel::update() {
@@ -47,6 +67,8 @@ drivingaid_panel::update() {
text_lines.clear();
std::tie( m_brokenvehicle, m_brokenend ) = broken_coupler_nearby( Global.pCamera.Pos );
auto const *train { simulation::Train };
auto const *controlled { ( train ? train->Dynamic() : nullptr ) };
@@ -72,53 +94,7 @@ drivingaid_panel::update() {
}
// next speed limit
auto const speedlimit { static_cast<int>( owner->VelDesired ) };
auto nextspeedlimit { speedlimit };
auto nextspeedlimitdistance { std::numeric_limits<double>::max() };
if( speedlimit != 0 ) { // if we aren't allowed to move then any next speed limit is irrelevant
// nie przekraczać rozkladowej
auto const schedulespeedlimit { (
(owner->OrderCurrentGet() & (Obey_train | Bank)) != 0 && owner->TrainParams.TTVmax > 0.0 ? static_cast<int>( owner->TrainParams.TTVmax ) :
(owner->OrderCurrentGet() & (Obey_train | Bank)) == 0 ? static_cast<int>( owner->fShuntVelocity ) :
-1 ) };
// first take note of any speed change which should occur after passing potential current speed limit
if( owner->VelLimitLastDist.second > 0 ) {
nextspeedlimit = min_speed( schedulespeedlimit, static_cast<int>( owner->VelLimitLastDist.first ) );
nextspeedlimitdistance = owner->VelLimitLastDist.second;
}
// then take into account speed change ahead, compare it with speed after potentially clearing last limit
// lower of these two takes priority; otherwise limit lasts at least until potential last limit is cleared
auto const noactivespeedlimit { owner->VelLimitLastDist.second < 0 };
if( auto const speedatproximitydistance { min_speed( schedulespeedlimit, static_cast<int>( owner->VelNext ) ) }; speedatproximitydistance == nextspeedlimit ) {
if( noactivespeedlimit ) {
nextspeedlimit = speedatproximitydistance;
nextspeedlimitdistance = owner->ActualProximityDist;
}
}
else if( speedatproximitydistance < nextspeedlimit ) {
// if the speed limit ahead is more strict than our current limit, it's important enough to report
if( speedatproximitydistance < owner->VelDesired ) {
nextspeedlimit = speedatproximitydistance;
nextspeedlimitdistance = owner->ActualProximityDist;
}
// otherwise report it only if it's located after our current (lower) limit ends
else if( owner->ActualProximityDist > nextspeedlimitdistance ) {
nextspeedlimit = speedatproximitydistance;
nextspeedlimitdistance = owner->ActualProximityDist;
}
}
else if( noactivespeedlimit ) { // implicit proximity > last, report only if last limit isn't present
nextspeedlimit = speedatproximitydistance;
nextspeedlimitdistance = owner->ActualProximityDist;
}
// HACK: if our current speed limit extends beyond our scan range don't display potentially misleading information about its length
if( nextspeedlimitdistance >= EU07_AI_SPEEDLIMITEXTENDSBEYONDSCANRANGE ) {
nextspeedlimit = speedlimit;
}
// HACK: hide next speed limit if the 'limit' is a vehicle in front of us
else if( owner->ActualProximityDist == std::abs( owner->TrackObstacle() ) ) {
nextspeedlimit = speedlimit;
}
}
auto const [ nextspeedlimit, nextspeedlimitdistance ] { owner->next_speed_limit() };
std::string nextspeedlimittext;
if( nextspeedlimit != speedlimit ) {
std::snprintf(
@@ -223,6 +199,12 @@ drivingaid_panel::render_contents() {
}
}
// only the coupler the camera is next to, and the one of the vehicle it was coupled with
if( m_brokenvehicle != nullptr && ImGui::Button( STR_C( "Repair coupler" ) ) ) {
command_relay commandrelay;
commandrelay.post( user_command::repaircoupler, m_brokenend, 0.0, GLFW_PRESS, 0, glm::vec3( 0.0f ), &m_brokenvehicle->name() );
}
ui_panel::render_contents();
}
@@ -546,6 +528,7 @@ void debug_panel::update()
m_powergridlines.clear();
m_cameralines.clear();
m_rendererlines.clear();
m_couplerlines.clear();
#ifdef WITH_UART
m_uartlines.clear();
#endif
@@ -559,6 +542,7 @@ void debug_panel::update()
update_section_powergrid( m_powergridlines );
update_section_camera( m_cameralines );
update_section_renderer( m_rendererlines );
update_section_coupler( m_couplerlines );
#ifdef WITH_UART
update_section_uart(m_uartlines);
#endif
@@ -608,6 +592,7 @@ debug_panel::render() {
command_relay commandrelay;
commandrelay.post(user_command::resetconsist, 0.0, 0.0, GLFW_PRESS, 0, glm::vec3(0.0f), &m_input.vehicle->name());
}
render_section_coupler();
render_section( "Vehicle Engine", m_enginelines );
render_section( "Vehicle AI", m_ailines );
render_section( "Vehicle Scan Table", m_scantablelines );
@@ -1510,6 +1495,172 @@ debug_panel::update_section_renderer( std::vector<text_line> &Output ) {
}
namespace {
// how far from the camera a coupler shows in the coupler section [m]
constexpr float coupler_pick_radius { 10.f };
std::string_view coupler_type_name( TCouplerType const Type ) {
switch( Type ) {
case TCouplerType::Articulated: { return "articulated"; }
case TCouplerType::Bare: { return "bare"; }
case TCouplerType::Chain: { return "chain"; }
case TCouplerType::Screw: { return "screw"; }
case TCouplerType::Automatic: { return "automatic"; }
default: { return "none"; }
}
}
// the couplerphysics setting of eu07.ini
std::string_view coupler_physics_setting() {
if( Global.coupler_physics_all ) { return "all"; }
return Global.coupler_physics ? "yes" : "no";
}
std::string_view coupler_stop_name( int const Stop ) {
if( Stop > 0 ) { return "draw gear solid"; }
if( Stop < 0 ) { return "buffers solid"; }
return "none";
}
// a value shown in the Scale of its unit (1000 for kN, 0.001 for mm), dragged within Min and Max of that unit
bool edit_value( char const *Label, double &Value, double const Scale, double const Min, double const Max, char const *Format ) {
auto shown { Value / Scale };
if( false == ImGui::DragScalar( Label, ImGuiDataType_Double, &shown, static_cast<float>( ( Max - Min ) / 1000.0 ), &Min, &Max, Format ) ) {
return false;
}
Value = std::clamp( shown, Min, Max ) * Scale;
return true;
}
bool edit_spring( char const *Name, coupler_physics::spring_curve &Curve, double &Unloading ) {
ImGui::PushID( Name );
ImGui::TextUnformatted( Name );
auto changed { edit_value( "Stiffness [kN/m]", Curve.stiffness, 1000.0, 100.0, 50000.0, "%.0f" ) };
changed = edit_value( "Travel [mm]", Curve.travel, 0.001, 5.0, 300.0, "%.0f" ) || changed;
changed = edit_value( "Progression", Curve.progression, 1.0, 0.0, 0.9, "%.2f" ) || changed;
changed = edit_value( "Unloading ratio", Unloading, 1.0, 0.05, 1.0, "%.2f" ) || changed;
ImGui::Text( "End force: %.0f kN", Curve.end_force() / 1000.0 );
ImGui::PopID();
return changed;
}
bool edit_link( coupler_physics::end_parameters &Parameters ) {
auto &link { Parameters.link };
ImGui::PushID( "link" );
ImGui::TextUnformatted( "Link" );
auto changed { ImGui::Checkbox( "Breakable", &link.breakable ) };
changed = edit_value( "Ultimate force [kN]", link.ultimate, 1000.0, 10.0, 5000.0, "%.0f" ) || changed;
changed = edit_value( "Yield force [kN]", link.yield, 1000.0, 10.0, 5000.0, "%.0f" ) || changed;
link.yield = std::min( link.yield, link.ultimate );
// the permanent stretch at which the link breaks, which is what the hardening gives
auto stretch { ( link.ultimate - link.yield ) / link.hardening };
if( edit_value( "Stretch to break [mm]", stretch, 0.001, 1.0, 200.0, "%.1f" ) ) {
link.hardening = std::max( ( link.ultimate - link.yield ) / stretch, 1.0 );
changed = true;
}
changed = edit_value( "Link stiffness [kN/m]", Parameters.link_stiffness, 1000.0, 1000.0, 2000000.0, "%.0f" ) || changed;
changed = edit_value( "Buffers crush force [kN]", Parameters.crush_force, 1000.0, 100.0, 10000.0, "%.0f" ) || changed;
changed = edit_value( "Restitution of a solid hit", Parameters.solid_restitution, 1.0, 0.05, 0.95, "%.2f" ) || changed;
ImGui::PopID();
return changed;
}
bool edit_coupler_parameters( coupler_physics::end_parameters &Parameters ) {
auto changed { edit_value( "Slack [mm]", Parameters.slack, 0.001, -100.0, 300.0, "%.1f" ) };
changed = edit_value( "Damping [kN s/m]", Parameters.damping, 1000.0, 0.0, 1000.0, "%.1f" ) || changed;
changed = edit_spring( "Buffers", Parameters.buffers, Parameters.buffers_unloading ) || changed;
changed = edit_spring( "Draw gear", Parameters.draw_gear, Parameters.draw_gear_unloading ) || changed;
changed = edit_link( Parameters ) || changed;
return changed;
}
} // namespace
void
debug_panel::update_section_coupler( std::vector<text_line> &Output ) {
m_coupler = {};
if( m_input.camera == nullptr ) { return; }
auto const &camera { m_input.camera->Pos };
auto const *vehicle { std::get<TDynamicObject *>( simulation::Region->find_vehicle( camera, coupler_pick_radius, false, true ) ) };
if( vehicle == nullptr || vehicle->MoverParameters == nullptr ) {
Output.emplace_back( std::format( "No coupler within {:.0f} m of the camera", coupler_pick_radius ), Global.UITextColor );
return;
}
auto const nearerfront { glm::distance( vehicle->CouplerPosition( end::front ), camera ) <= glm::distance( vehicle->CouplerPosition( end::rear ), camera ) };
m_coupler = { vehicle, nearerfront ? end::front : end::rear };
auto const &mover { *vehicle->MoverParameters };
auto const &coupler { mover.Couplers[ m_coupler.end ] };
auto const &neighbour { mover.Neighbours[ m_coupler.end ] };
auto const &state { coupler.physics_state };
Output.emplace_back(
std::format( "{}, {} end, {} coupler{}", vehicle->name(), nearerfront ? "front" : "rear", coupler_type_name( coupler.type() ), coupler.has_adapter() ? " (adapter)" : "" ),
Global.UITextColor );
Output.emplace_back(
std::format(
"Model: {} (couplerphysics {}, vehicle {}covered), crash damage {}",
mover.coupler_physics_at( m_coupler.end ) ? "physical" : "legacy",
coupler_physics_setting(),
mover.CouplerPhysics ? "" : "not ",
Global.crash_damage ? "on" : "off" ),
Global.UITextColor );
if( neighbour.vehicle != nullptr ) {
Output.emplace_back(
std::format( "Neighbour: {} ({} end), coupling [{}], gap {:.1f} mm", neighbour.vehicle->name(), neighbour.vehicle_end == end::front ? "front" : "rear", coupler.CouplingFlag, neighbour.distance * 1000.0 ),
Global.UITextColor );
}
else {
Output.emplace_back( "Neighbour: none", Global.UITextColor );
}
Output.emplace_back( std::format( "Force: {:.1f} kN", coupler.CForce / 1000.0 ), Global.UITextColor );
Output.emplace_back(
std::format( "Draw gear: {:.1f} mm, {:.1f} kN Buffers: {:.1f} mm, {:.1f} kN", state.draw_gear.deflection * 1000.0, state.draw_gear.force / 1000.0, state.buffers.deflection * 1000.0, state.buffers.force / 1000.0 ),
Global.UITextColor );
Output.emplace_back(
std::format( "Stretch: {:.1f} mm, stop: {}, overloaded for {:.1f} s", state.elongation * 1000.0, coupler_stop_name( state.stop ), coupler.stretch_duration ),
Global.UITextColor );
if( coupler.broken ) {
Output.emplace_back( "Coupler broken (repair it in the driving aid, F1)", glm::vec4( 1.f, 0.25f, 0.25f, 1.f ) );
}
}
bool
debug_panel::render_section_coupler() {
if( false == render_section( "Coupler", m_couplerlines ) ) { return false; }
// the parameters to try out, in the debug mode, on the simulation itself
if( false == DebugModeFlag || m_coupler.vehicle == nullptr || Application.is_client() ) { return true; }
ImGui::Separator();
ImGui::Checkbox( "Physical coupler model", &Global.coupler_physics );
ImGui::Checkbox( "Physical model for every train", &Global.coupler_physics_all );
ImGui::Checkbox( "Crash damage", &Global.crash_damage );
ImGui::Checkbox( "Apply to the other end of the pair too", &m_couplerbothends );
auto &mover { *m_coupler.vehicle->MoverParameters };
auto &coupler { mover.Couplers[ m_coupler.end ] };
auto const &neighbour { mover.Neighbours[ m_coupler.end ] };
auto *othercoupler { neighbour.vehicle != nullptr ? &neighbour.vehicle->MoverParameters->Couplers[ neighbour.vehicle_end ] : nullptr };
if( auto parameters { coupler.physics }; edit_coupler_parameters( parameters ) ) {
coupler.physics = parameters;
if( m_couplerbothends && othercoupler != nullptr ) {
othercoupler->physics = parameters;
}
}
if( ImGui::Button( "Reset the stretch of the coupling" ) ) {
// both vehicles keep the state of the pair alike
coupler.physics_state.elongation = 0.0;
if( othercoupler != nullptr ) {
othercoupler->physics_state.elongation = 0.0;
}
}
return true;
}
bool
debug_panel::render_section( std::string const &Header, std::vector<text_line> const &Lines ) {