diff --git a/CLAUDE.md b/CLAUDE.md index c1709be8d..283058cbe 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -27,11 +27,16 @@ Where no GCC is at hand, read the new code for what MSVC lets through and GCC do The same behaviour needs portable code. Don't rely on the order arguments are evaluated in, on the signedness of `char`, on the size of `wchar_t`, or on `long` being 32 bits. -## SonarCloud: every remark on the changed code gets fixed +## SonarCloud: the PR passes the quality gate -SonarCloud (`sonar.yml`, `sonar-project.properties`) analyses each PR. A PR counts as finished only when every remark Sonar raises on -the code it changes is fixed. That means the code smells too, not just the bugs, the vulnerabilities and the quality gate. The analysis -runs on the GCC build, so a PR that fails to build with GCC gets no analysis at all. +SonarCloud (`sonar.yml`, `sonar-project.properties`) analyses each PR. A PR has to pass Sonar's quality gate. The gate rates the new +code: reliability, security and maintainability have to be A, duplication is at most 3% and the security hotspots are reviewed. +Any bug or vulnerability on the new code lowers its rating below A, so each one gets fixed (or gets a NOSONAR when it is a false +positive). Code smells count only together, through the maintainability rating, so a single one rarely fails the gate, but many in a +large PR can. A hotspot is reviewed by marking it in SonarCloud as safe or fixed, which is the user's step there, not a reason to +rewrite the code. A remark that doesn't fail the gate doesn't have to be fixed. Leave large restructurings that only answer such a +remark (splitting a class or a file, rewriting a working algorithm) out of the PR. The analysis runs on the GCC build, so a PR that +fails to build with GCC gets no analysis at all, and so it doesn't pass the gate. Fixing a remark must not change what the code does: @@ -41,7 +46,7 @@ Fixing a remark must not change what the code does: - `// NOSONAR: ` is only for a real false positive, or where a fix would change behaviour. Use it rarely, always with the reason. -Write new code so that it doesn't raise the rules that keep coming up: +To keep the gate passing, write new code so that it doesn't raise the rules that keep coming up: - **S134** (nesting deeper than 3 `if` / `for` / `while` / `switch` / `try`) and **S3776** (cognitive complexity over 25): - use early `continue` / `return`; diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index 199c65a0c..16bb207fa 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -2461,11 +2461,24 @@ class TMoverParameters { return false == ControlPressureSwitch && (HasControlPressureSwitch || BrakePress < 1.0); } - // whether the lamp of the open line contactors is lit, telling the start can be made + // whether the lamp of the open line contactors is lit. on the electric locomotives it tells the start can be made, so + // it goes out with the start cut off; on the diesel-electric ones (the SU45 and SU46 family) it just tells the + // contactors are open, whatever opened them: the master controller at 0, or the pressure switch (eu07/maszyna#263) bool LineContactorsLampLit() const { + if (EngineType == TEngineType::DieselElectric) + { + return false == StLinFlag; + } return false == StLinFlag && MainCircuitLampsEnabled(); } + // whether the cab command opening the main circuit opens it where the only cab switch of the circuit is the button + // closing it: on a diesel that's the starter button, and the command is the one way to stop an engine whose fuel pump + // starts on its own (eu07/maszyna#427). the other vehicles with such a button keep ignoring the command + bool MainCircuitOpensWithStarterOnly() const + { + return EngineType == TEngineType::DieselEngine || EngineType == TEngineType::DieselElectric; + } // whether the pumps a joint oil and fuel pump switch works, those not started automatically, are all switched on bool OilFuelPumpsEnabled() const { diff --git a/rendering/opengl33renderer.cpp b/rendering/opengl33renderer.cpp index c24f750c8..932ac31f2 100644 --- a/rendering/opengl33renderer.cpp +++ b/rendering/opengl33renderer.cpp @@ -2032,12 +2032,12 @@ bool opengl33_renderer::Render(world_environment *Environment) glm::vec4 color(suncolor.x, suncolor.y, suncolor.z, std::clamp(1.5f - Global.Overcast, 0.f, 1.f) * fogfactor); auto const sunvector = Environment->m_sun.getDirection(); - /*float const size = std::lerp( // TODO: expose distance/scale factor from the moon object + float const size = std::lerp( // TODO: expose distance/scale factor from the moon object 0.0325f, 0.0275f, - std::clamp( Environment->m_sun.getAngle(), 0.f, 90.f ) / 90.f );*/ + std::clamp( Environment->m_sun.getAngle(), 0.f, 90.f ) / 90.f ); model_ubs.param[0] = color; - model_ubs.param[1] = glm::vec4(glm::vec3(modelview * glm::vec4(sunvector, 1.0f)), 0.00463f /* size */); + model_ubs.param[1] = glm::vec4(glm::vec3(modelview * glm::vec4(sunvector, 1.0f)), size); model_ubs.param[2] = glm::vec4(0.0f, 1.0f, 1.0f, 0.0f); model_ubo->update(model_ubs); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); @@ -2108,14 +2108,13 @@ bool opengl33_renderer::Render(world_environment *Environment) moonu = 0.0f; } - /* float const size = std::lerp( // TODO: expose distance/scale factor from the moon object 0.0160f, 0.0135f, - std::clamp( Environment->m_moon.getAngle(), 0.f, 90.f ) / 90.f );*/ + std::clamp( Environment->m_moon.getAngle(), 0.f, 90.f ) / 90.f ); model_ubs.param[0] = color; - model_ubs.param[1] = glm::vec4(glm::vec3(modelview * glm::vec4(moonvector, 1.0f)), 0.00451f /* size */); + model_ubs.param[1] = glm::vec4(glm::vec3(modelview * glm::vec4(moonvector, 1.0f)), size); model_ubs.param[2] = glm::vec4(moonu, moonv, 0.333f, 0.0f); model_ubo->update(model_ubs); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); diff --git a/simulation/simulationenvironment.cpp b/simulation/simulationenvironment.cpp index bdc4b0598..11e28b61f 100644 --- a/simulation/simulationenvironment.cpp +++ b/simulation/simulationenvironment.cpp @@ -68,15 +68,15 @@ world_environment::compute_weather() const { Global.AirTemperature > 1 ? "rain:" : "snow:"; - Global.fTurbidity = Global.Overcast <= 0.10 ? 3 : + Global.fTurbidity = Global.Overcast <= 0.10 ? 4 : Global.Overcast <= 0.20 ? 4 : Global.Overcast <= 0.30 ? 5 : Global.Overcast <= 0.40 ? 5 : - Global.Overcast <= 0.50 ? 5 : - Global.Overcast <= 0.60 ? 5 : - Global.Overcast <= 0.70 ? 6 : - Global.Overcast <= 0.80 ? 7 : - Global.Overcast <= 0.90 ? 8 : + Global.Overcast <= 0.50 ? 6 : + Global.Overcast <= 0.60 ? 6 : + Global.Overcast <= 0.70 ? 7 : + Global.Overcast <= 0.80 ? 8 : + Global.Overcast <= 0.90 ? 9 : 9; } diff --git a/tests/unit/data/diesel_electric.json b/tests/unit/data/diesel_electric.json new file mode 100644 index 000000000..bef605880 --- /dev/null +++ b/tests/unit/data/diesel_electric.json @@ -0,0 +1,46 @@ +{ + "_comment": "Diesel-electric locomotive made after the SU46 (303D): the line contactors close on the first power position of the master controller, the pressure switch (on by default) opens them with too low a pressure in the main pipe or the brakes applied.", + "Param": { "Category": "train", "M": 105000, "Mred": 15000, "Vmax": 120, "PWR": 1400, "SandCap": 400 }, + "Dimensions": { "L": 18.99, "H": 4.292, "W": 2.904, "Cx": 0.3 }, + "Wheels": { "D": 1.1, "AIM": 4000, "Tw": 1.435, "Axle": "Co'Co'", "Ad": 1.75, "Bd": 10.3, "Rmin": 100 }, + "Brake": { + "BrakeValve": "LSt", "NBpA": 4, "MBF": 85.0, "MaxBP": 4.4, "LocMaxBP": 4.4, "MaxASBP": 1.3, "RM": 0.71, "BCN": 12, "BCR": 0.114, + "BCD": 0.06, "BCM": 5.84, "HiPP": 5, "LoPP": 3.5, "Vv": 0.9, "MinCP": 7, "MaxCP": 8.5, "CompressorSpeed": 0.05, "BCS": 1, "BSA": 2, + "BM": "P10-Bgu", "CompressorPower": "Converter", "BVV": 200, "BRE": 0.8 + }, + "BuffCoupl": { "CType": "Screw", "kB": 9200, "DmaxB": 0.15, "FmaxB": 740, "kC": 4800, "DmaxC": 0.15, "FmaxC": 780, "beta": 0.58, "AllowedFlag": 103 }, + "Cntrl": { + "BrakeHandle": "FV4a", "LocBrakeHandle": "FD1", "BrakeSystem": "Pneumatic", "BDelay1": 15, "BDelay2": 6, "BDelay3": 36, "BDelay4": 32, + "ASB": "Manual", "LocalBrake": "PneumaticBrake", "MCPN": 15, "SCPN": 4, "AutoRelay": "Yes", "IniCDelay": 0.5, "SCDelay": 0.1, + "BrakeDelays": "GPR", "ConverterStart": "Automatic", "ConverterStartDelay": 10 + }, + "Light": { "Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": 110, "LMaxCurrent": 100 }, + "Engine": { + "AIM": 1.6, "EngineType": "DumbDE", "Trans": "18:62", "Ftmax": 317000, "Flat": 0, "Vhyp": 15.0, "Vadd": 3.8, "Cr": 1.0, "RelayType": 46, + "OilMinPressure": 0.055, "OilMaxPressure": 0.6, "WaterMinTemperature": 40, "WaterMaxTemperature": 92, "WaterCoolingTemperature": 76, + "HeatKW": 0.068599, "HeatKV": 0.3, "HeatKFE": 1, "HeatKFS": 87.76269, "HeatKFO": 26.72716, "HeatKFO2": 57.72 + }, + "Circuit": { "CircuitRes": 0.01, "ImaxLo": 3300, "IminLo": 2500 }, + "_power": "rows: engine speed [rpm], power [kW], generator voltage limit, current limit, one per master controller position", + "WWList": { + "Size": 15, + "rows": [ + [696, 0, 0, 0], + [696, 0, 0, 0], + [696, 80, 350, 1000], + [696, 80, 350, 1000], + [763, 214, 455, 1400], + [830, 321, 500, 1600], + [897, 428, 545, 1800], + [964, 535, 590, 2000], + [1031, 642, 635, 2200], + [1098, 748, 680, 2400], + [1165, 855, 725, 2600], + [1232, 962, 770, 2800], + [1299, 1069, 815, 3000], + [1366, 1176, 865, 3200], + [1433, 1283, 910, 3400], + [1500, 1390, 960, 3600] + ] + } +} diff --git a/tests/unit/data/diesel_shunter.json b/tests/unit/data/diesel_shunter.json new file mode 100644 index 000000000..3cbcdb992 --- /dev/null +++ b/tests/unit/data/diesel_shunter.json @@ -0,0 +1,53 @@ +{ + "_comment": "Diesel shunter with a mechanical gearbox, made after the SM03 (Ls150): the fuel pump starts on its own with the engine, a converter run by the engine feeds the low voltage circuit, the only cab control of the engine is the starter button.", + "Param": { "Category": "train", "M": 23200, "Vmax": 25.68, "PWR": 111.855, "SandCap": 200 }, + "Dimensions": { "L": 6.94, "H": 3.21, "W": 2.97, "Cx": 0.3 }, + "Wheels": { "D": 0.94, "Tw": 1.435, "AIM": 250, "Axle": "B", "Ad": 2.5, "Bd": 2.5, "Rmin": 50, "BearingType": "Roll" }, + "Brake": { + "BrakeValve": "K", "NBpA": 2, "MBF": 85.0, "MaxBP": 3.5, "LocMaxBP": 5.0, "BCN": 1, "BCR": 0.114, "BCD": 0.12, "BCM": 10.0, + "HiPP": 5, "LoPP": 3.5, "Vv": 0.3, "MinCP": 7.0, "MaxCP": 8.0, "CompressorSpeed": 0.0125, "CompressorPower": "Main", + "BCS": 0.8, "BSA": 0, "BM": "P10-Bg", "BVV": 40, "BRE": 0.9 + }, + "BuffCoupl": { "CType": "Screw", "kB": 9200, "DmaxB": 0.15, "FmaxB": 740, "kC": 4800, "DmaxC": 0.15, "FmaxC": 780, "beta": 0.58, "AllowedFlag": 3 }, + "Cntrl": { + "BrakeSystem": "Pneumatic", "BDelay1": 15, "BDelay2": 4, "ASB": "Manual", "LocalBrake": "PneumaticBrake", "ManualBrake": "Yes", + "DynamicBrake": "Passive", "MCPN": 9, "SCPN": 7, "AutoRelay": "No", "IniCDelay": 0.5, "SCDelay": 0.1, "BrakeDelays": "P", + "BrakeHandle": "Knorr", "LocBrakeHandle": "Knorr", "FuelStart": "Automatic", "ConverterStart": "Automatic" + }, + "Light": { "Light": "Generator", "LGeneratorEngine": "ElectricSeriesMotor", "LMaxVoltage": 24, "LMaxCurrent": 100 }, + "Engine": { + "EngineType": "DieselEngine", "Trans": "1:1", "nmin": 900, "nmax": 1500, "nmax_cutoff": 1550, "AIM": 100, + "OilMinPressure": 0.343, "OilMaxPressure": 0.686, "WaterCoolingTemperature": 80, "WaterShutters": "yes", "WaterAuxCircuit": "no", + "HeatKFO": 160, "HeatKFO2": 180, "HeatKFS": 160, "HeatKFE": 7, "HeatKW": 2, "HeatKV": 0.3, "IsTC": "No", "EUS": 0.35, "EDS": 1.0 + }, + "_gears": "rows: gear, ratio, engagement speed, top speed; the even ones neutral", + "MotorParamTable": { + "minVelfullengage": 3.8, "engageDia": 1, "engageMaxForce": 800, "engagefriction": 1, + "rows": [ + [0, 0, 0, -1], + [1, 46.88, -1, 5.73], + [2, 0, 0, -1], + [3, 28.65, 2.9, 9.42], + [4, 0, 0, -1], + [5, 17.12, 4.7, 15.69], + [6, 0, 0, -1], + [7, 10.46, 10.6, 100] + ] + }, + "_throttle": "rows: position, fill, speed governor mode; position 0 idles", + "DList": { + "Size": 9, "Mmax": 784.5, "nMmax": 15, "Mnmax": 686.4, "nmax": 25, "nominalfill": 1, "Mstand": 200.0, + "rows": [ + [0, 0.1, 0], + [1, 0.2, 2], + [2, 0.3, 2], + [3, 0.4, 2], + [4, 0.5, 2], + [5, 0.6, 2], + [6, 0.7, 2], + [7, 0.8, 2], + [8, 0.9, 2], + [9, 1.0, 2] + ] + } +} diff --git a/tests/unit/test_engine_shutdown.cpp b/tests/unit/test_engine_shutdown.cpp new file mode 100644 index 000000000..9230330f4 --- /dev/null +++ b/tests/unit/test_engine_shutdown.cpp @@ -0,0 +1,74 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +// stopping the engine of a diesel shunter like the SM03 (eu07/maszyna#427): its fuel pump starts on its own with the +// engine and the engine runs the converter which feeds the low voltage circuit, so neither the fuel pump switch nor the +// battery switch stop it. the cab has only the starter button, and the command opening the main circuit has to stop it + +namespace { + +// the shunter with its engine started from the cab and idling +test::vehicle running_shunter() { + test::vehicle shunter{test::definition::load("diesel_shunter")}; + shunter->CabOccupied = 1; + shunter->CabActivisation(); + shunter->BatterySwitch(true); + shunter->OilPumpSwitch(true); + shunter.run(20.0); // the oil pressure builds up + shunter->MainSwitch(true); + shunter.run(15.0); + return shunter; +} + +} // namespace + +TEST_CASE("engine shutdown: the shunter starts with the starter button") { + auto shunter{running_shunter()}; + REQUIRE(shunter->EngineType == TEngineType::DieselEngine); + REQUIRE(shunter->FuelPump.start_type == start_t::automatic); + CHECK(shunter->Mains); + CHECK(shunter->FuelPump.is_active); + CHECK(shunter->enrot > 0.9 * shunter->dizel_nmin); +} + +TEST_CASE("engine shutdown: neither the fuel pump switch nor the battery switch stop the engine") { + auto shunter{running_shunter()}; + shunter->FuelPumpSwitchOff(true, range_t::local); // the pump started automatically ignores its switch + shunter.run(10.0); + CHECK(shunter->Mains); + shunter->BatterySwitch(false); + shunter.run(10.0); + CHECK(shunter->Power110vIsAvailable); // the converter run by the engine keeps feeding the fuel pump + CHECK(shunter->Mains); + CHECK(shunter->enrot > 0.9 * shunter->dizel_nmin); +} + +TEST_CASE("engine shutdown: the command opening the main circuit stops the engine and it stays stopped") { + auto shunter{running_shunter()}; + REQUIRE(shunter->MainCircuitOpensWithStarterOnly()); // the cab passes the command on with only the starter button + CHECK(shunter->MainSwitch(false)); + shunter.run(20.0); + CHECK_FALSE(shunter->Mains); + CHECK_FALSE(shunter->FuelPump.is_active); + CHECK(shunter->enrot == 0.0); +} + +TEST_CASE("engine shutdown: the engine starts again after it was stopped") { + auto shunter{running_shunter()}; + shunter->MainSwitch(false); + shunter.run(20.0); + REQUIRE(shunter->enrot == 0.0); + shunter->MainSwitch(true); + shunter.run(15.0); + CHECK(shunter->Mains); + CHECK(shunter->enrot > 0.9 * shunter->dizel_nmin); +} + +TEST_CASE("engine shutdown: vehicles without a diesel keep ignoring the command with only the button closing the circuit") { + test::vehicle wagon{test::definition::load("wagon")}; + CHECK_FALSE(wagon->MainCircuitOpensWithStarterOnly()); + test::vehicle electric{test::definition::load("wagon").set("Engine", "EngineType", "ElectricSeriesMotor")}; + REQUIRE(electric->EngineType == TEngineType::ElectricSeriesMotor); + CHECK_FALSE(electric->MainCircuitOpensWithStarterOnly()); +} diff --git a/tests/unit/test_line_contactors.cpp b/tests/unit/test_line_contactors.cpp index 79c419edf..fd7e24f08 100644 --- a/tests/unit/test_line_contactors.cpp +++ b/tests/unit/test_line_contactors.cpp @@ -57,3 +57,74 @@ TEST_CASE("line contactors: the ground relay and overload lamps follow the same vehicle->MotorConnectorsCheck(); CHECK_FALSE(vehicle->MainCircuitLampsEnabled()); } + +// on the diesel-electric SU45 and SU46 (eu07/maszyna#263) the lamp tells just the line contactors are open: lit with the +// master controller at 0 and with the start cut off by too low a pressure in the main pipe, out on the power positions + +namespace { + +// the diesel-electric locomotive with its engine running and the brakes released, the master controller at 0 +test::vehicle diesel_electric() { + test::vehicle vehicle{test::definition::load("diesel_electric")}; + vehicle->CabOccupied = 1; + vehicle->CabActivisation(); + vehicle->BatterySwitch(true); + vehicle->OilPumpSwitch(true); + vehicle->FuelPumpSwitch(true); + vehicle.run(20.0); // the oil pressure builds up + vehicle.hold_brake_pipe(5.0); // bar + vehicle->MainSwitch(true); + vehicle.run(30.0); + vehicle->DirectionForward(); + vehicle.run(60.0); // the brakes release + return vehicle; +} + +} // namespace + +TEST_CASE("line contactors: on a diesel-electric the lamp is lit at 0 and goes out on a power position") { + auto vehicle{diesel_electric()}; + REQUIRE(vehicle->EngineType == TEngineType::DieselElectric); + REQUIRE(vehicle->Mains); + REQUIRE_FALSE(vehicle->ControlPressureSwitch); + CHECK_FALSE(vehicle->StLinFlag); + CHECK(vehicle->LineContactorsLampLit()); + + vehicle->IncMainCtrl(1); + vehicle.run(3.0); + CHECK(vehicle->StLinFlag); + CHECK_FALSE(vehicle->LineContactorsLampLit()); + + vehicle->DecMainCtrl(1); + vehicle.run(3.0); + CHECK_FALSE(vehicle->StLinFlag); + CHECK(vehicle->LineContactorsLampLit()); +} + +TEST_CASE("line contactors: on a diesel-electric the lamp lights with the contactors opened by too low a main pipe pressure") { + auto vehicle{diesel_electric()}; + vehicle->IncMainCtrl(1); + vehicle.run(1.0); + vehicle->IncMainCtrl(1); + vehicle.run(2.0); + REQUIRE(vehicle->StLinFlag); + + vehicle.hold_brake_pipe(2.5); // bar, below the 3.6 bar of the pressure switch + vehicle.run(10.0); + REQUIRE(vehicle->ControlPressureSwitch); + CHECK_FALSE(vehicle->StLinFlag); + CHECK(vehicle->LineContactorsLampLit()); // it went out here + CHECK_FALSE(vehicle->MainCircuitLampsEnabled()); // the ground relay and overload lamps still go out with the start cut off + + // the pressure back, the contactors stay open on the second position and the lamp stays lit, as it should; + // they close again only on the first one + vehicle.hold_brake_pipe(5.0); + vehicle.run(60.0); + REQUIRE_FALSE(vehicle->ControlPressureSwitch); + CHECK_FALSE(vehicle->StLinFlag); + CHECK(vehicle->LineContactorsLampLit()); + vehicle->DecMainCtrl(1); + vehicle.run(3.0); + CHECK(vehicle->StLinFlag); + CHECK_FALSE(vehicle->LineContactorsLampLit()); +} diff --git a/utilities/color.h b/utilities/color.h index be853a3d4..bbf17e096 100644 --- a/utilities/color.h +++ b/utilities/color.h @@ -15,9 +15,9 @@ glm::vec3 XYZtoRGB( glm::vec3 const &XYZ ) { // M^-1 for Adobe RGB from http://www.brucelindbloom.com/Eqn_RGB_XYZ_Matrix.html - float const mi[ 3 ][ 3 ] = { 2.041369f, -0.969266f, 0.0134474f, -0.5649464f, 1.8760108f, -0.1183897f, -0.3446944f, 0.041556f, 1.0154096f }; +// float const mi[ 3 ][ 3 ] = { 2.041369f, -0.969266f, 0.0134474f, -0.5649464f, 1.8760108f, -0.1183897f, -0.3446944f, 0.041556f, 1.0154096f }; // m^-1 for sRGB: -// float const mi[ 3 ][ 3 ] = { 3.240479f, -0.969256f, 0.055648f, -1.53715f, 1.875991f, -0.204043f, -0.49853f, 0.041556f, 1.057311f }; + float const mi[ 3 ][ 3 ] = { 3.240479f, -0.969256f, 0.055648f, -1.53715f, 1.875991f, -0.204043f, -0.49853f, 0.041556f, 1.057311f }; return glm::vec3{ XYZ.x*mi[ 0 ][ 0 ] + XYZ.y*mi[ 1 ][ 0 ] + XYZ.z*mi[ 2 ][ 0 ], diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index e27dff946..4f9205bc3 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -829,6 +829,45 @@ void TController::TableCheck(double fDistance) auto const passengerstopmaxdistance { 400.0 }; // maximum allowed distance between passenger stop point and consist head +// the margins of the final approach of a train [m], closer than the first of which it stops at once and within the +// second of which it makes its final approach: at a passenger stop, or at a signal and anything else it stops at +std::pair +TController::train_proximity_margins( bool const Passengerstop ) const { + + auto minproximity { 0.0 }; + auto maxproximity { 0.0 }; + if( Passengerstop ) { + // a stop point keeps the margins its W4 parameters (head or middle of the consist, + // platform length) were set up against + minproximity = std::clamp( 5 + iVehicles, 10, 15 ) + ( IsCargoTrain ? 10.0 : 0.0 ); + maxproximity = std::clamp( 10 + iVehicles, 15, 40 ); + } + else { + // a train stops 8-15 m short of the signal whatever its length; closer than the lower + // edge it stops at once, the braking aims at the middle of the window, 3.5 m further out + minproximity = 8.0; + // the final approach begins far enough out to slow from the coasting speed down to the window, + // and at the same distance whether the train is moving or standing, so it does not flip + // between creeping closer and holding still when it comes to a stand short of the window + maxproximity = 50.0; + } + if( IsCargoTrain ) { + // start the final approach earlier for cargo trains, their brakes react more slowly + maxproximity += 10.0; + } + if( ( Global.FrictionWeatherFactor < 1.f ) + && ( iVehicles > 1 ) ) { + // take into account weather conditions + minproximity += 5.0; + maxproximity += 5.0; + } + if( mvOccupied->Vel < 0.1 ) { + // jak stanie za daleko, to niech nie dociąga paru metrów + maxproximity = std::max( maxproximity, 50.0 ); + } + return { minproximity, maxproximity }; +} + TCommandType TController::TableUpdate(double &fVelDes, double &fDist, double &fNext, double &fAcc) { // ustalenie parametrów, zwraca typ komendy, jeśli sygnał podaje prędkość do jazdy // fVelDes - prędkość zadana @@ -1152,14 +1191,20 @@ TController::TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, doub return true; } + // the margins of a passenger stop, whether the train is already making for it or not: the present ones + // depend on this very flag, and a check against them would turn it on and off every other scan + auto const [ stopminproximity, stopmaxproximity ] { + is_train() ? + train_proximity_margins( true ) : + std::pair{ fMinProximityDist, fMaxProximityDist } }; IsAtPassengerStop = Point.fDist <= passengerstopmaxdistance // Ra 2F1I: odległość plus długość pociągu musi być mniejsza od długości // peronu, chyba że pociąg jest dłuższy, to wtedy minimalna. // jeśli długość peronu ((sSpeedTable[i].evEvent->ValueGet(2)) nie podana, // przyjąć odległość fMinProximityDist && ((iDrivigFlags & moveStopCloser) != 0 ? - Point.fDist + fLength + (Point.fMoved - fMinProximityDist * 0.5f) <= std::max(std::abs(Point.evEvent->input_value(2)), - 2.0 * fMaxProximityDist + fLength) : // fmaxproximitydist typically equals ~50 m + Point.fDist + fLength + (Point.fMoved - stopminproximity * 0.5f) <= std::max(std::abs(Point.evEvent->input_value(2)), + 2.0 * stopmaxproximity + fLength) : Point.fDist < Signaldistance); if( !eSignNext ) { @@ -6694,47 +6739,9 @@ TController::determine_proximity_ranges() { // na jaka odleglosc i z jaka predkoscia ma podjechac do przeszkody // jeśli pociąg if( is_train() ) { - if( IsAtPassengerStop ) { - // a stop point keeps the margins its W4 parameters (head or middle of the consist, - // platform length) were set up against - fMinProximityDist = std::clamp( 5 + iVehicles, 10, 15 ); - fMaxProximityDist = std::clamp( 10 + iVehicles, 15, 40 ); - if( IsCargoTrain ) { - fMinProximityDist += 10.0; - } - } - else { - // a train stops 8-15 m short of the signal whatever its length; closer than the lower - // edge it stops at once, the braking aims at the middle of the window, 3.5 m further out - fMinProximityDist = 8.0; - // the final approach begins far enough out to slow from the coasting speed down to the window, - // and at the same distance whether the train is moving or standing, so it does not flip - // between creeping closer and holding still when it comes to a stand short of the window - fMaxProximityDist = 50.0; - } - - if( IsCargoTrain ) { - // start the final approach earlier for cargo trains, their brakes react more slowly - fMaxProximityDist += 10.0; -/* - if( IsHeavyCargoTrain ) { - // cargo trains with high braking threshold may require even larger safety margin - fMaxProximityDist += 20.0; - } -*/ - } - - if (Global.FrictionWeatherFactor < 1.f - && iVehicles > 1 ) { - // take into account weather conditions - fMinProximityDist += 5.0; - fMaxProximityDist += 5.0; - } - - if( mvOccupied->Vel < 0.1 ) { - // jak stanie za daleko, to niech nie dociąga paru metrów - fMaxProximityDist = std::max( fMaxProximityDist, 50.0 ); - } + auto const [ minproximity, maxproximity ] { train_proximity_margins( IsAtPassengerStop ) }; + fMinProximityDist = minproximity; + fMaxProximityDist = maxproximity; if( iDrivigFlags & moveLate ) { // jeśli spóźniony, to gna diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 7698fedb7..6a4db3003 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -315,6 +315,7 @@ private: bool uphill_stop_hold() const; bool uphill_brake_committed( double const Velocity ) const; void control_uphill_start(); + std::pair train_proximity_margins( bool const Passengerstop ) const; void update_timers( double const dt ); void update_logs( double const dt ); diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 201b8972f..e30635f4f 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -3702,13 +3702,14 @@ void TTrain::OnCommand_linebreakeropen(TTrain *Train, command_data const &Comman { Train->ggMainButton.UpdateValue(0.0, Train->dsbSwitch); } - else if (Train->ggMainOnButton.SubModel != nullptr) + else if (Train->ggMainOnButton.SubModel != nullptr && false == Train->mvControlled->MainCircuitOpensWithStarterOnly()) { // NOTE: legacy behaviour, for vehicles equipped only with impulse close switch // it doesn't make any real sense to animate this one, but some people can't get over how there's no visual reaction to their keypress Train->ggMainOnButton.UpdateValue(1.0, Train->dsbSwitch); return; } + // a diesel with only the starter button gets its engine stopped. the button stays put: held down, it would start the engine again // play sound immediately when the switch is hit, not after release Train->fMainRelayTimer = 0.0f; @@ -5234,15 +5235,37 @@ void TTrain::OnCommand_headlighttoggleleft(TTrain *Train, command_data const &Co if (Command.action == GLFW_PRESS) { // only reacting to press, so the switch doesn't flip back and forth if key is held down - if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_left) == 0) + + if (Train->ggLeftEndLightButton.SubModel != nullptr || Train->ggLeftLightButton.SubModel == nullptr) { - // turn on - OnCommand_headlightenableleft(Train, Command); - } - else - { - // turn off - OnCommand_headlightdisableleft(Train, Command); + if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_left) == 0) + { + // turn on + OnCommand_headlightenableleft(Train, Command); + } + else + { + // turn off + OnCommand_headlightdisableleft(Train, Command); + } + } else { + // if the light is controlled by 3-way switch, handle both headlight and redmarker + if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_left) == 0 && + (Train->mvOccupied->iLights[vehicleend] & light::redmarker_left) == 0) + { + // turn on + OnCommand_headlightenableleft(Train, Command); + } + else if (Train->mvOccupied->iLights[vehicleend] & light::headlight_left) + { + // switch to red + OnCommand_redmarkerenableleft(Train, Command); + } + else + { + // disable + OnCommand_redmarkerdisableleft(Train, Command); + } } } } @@ -5317,15 +5340,37 @@ void TTrain::OnCommand_headlighttoggleright(TTrain *Train, command_data const &C if (Command.action == GLFW_PRESS) { // only reacting to press, so the switch doesn't flip back and forth if key is held down - if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_right) == 0) + + if (Train->ggRightEndLightButton.SubModel != nullptr || Train->ggRightLightButton.SubModel == nullptr) { - // turn on - OnCommand_headlightenableright(Train, Command); - } - else - { - // turn off - OnCommand_headlightdisableright(Train, Command); + if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_right) == 0) + { + // turn on + OnCommand_headlightenableright(Train, Command); + } + else + { + // turn off + OnCommand_headlightdisableright(Train, Command); + } + } else { + // if the light is controlled by 3-way switch, handle both headlight and redmarker + if ((Train->mvOccupied->iLights[vehicleend] & light::headlight_right) == 0 && + (Train->mvOccupied->iLights[vehicleend] & light::redmarker_right) == 0) + { + // turn on + OnCommand_headlightenableright(Train, Command); + } + else if (Train->mvOccupied->iLights[vehicleend] & light::headlight_right) + { + // switch to red + OnCommand_redmarkerenableright(Train, Command); + } + else + { + // disable + OnCommand_redmarkerdisableright(Train, Command); + } } } } diff --git a/world/Event.cpp b/world/Event.cpp index fa752078b..624cdd714 100644 --- a/world/Event.cpp +++ b/world/Event.cpp @@ -1025,13 +1025,42 @@ whois_event::run_() { auto *targetcell { static_cast( std::get( target ) ) }; if( targetcell == nullptr ) { continue; } // event effect code + // +48: station after the next one, unused, stop there or passthrough // +40: next station name, unused, stop at next station (duplicate of +0 2nd numeric value) // +32: vehicle name // +24: vehicle type, consist brake level, obstacle distance // +16: load type, load amount, max load amount // +8: destination, direction, engine power // +0: train name, station count, stop on next station - if( m_input.flags & flags::whois_name ) { + if( ( m_input.flags & ( flags::whois_name | flags::whois_load | flags::mode_alt ) ) + == ( flags::whois_name | flags::whois_load ) ) { + // +48 (the whois_name | whois_load combination was unused; +56, same with mode_alt + // set, stays free for a future mode) + // the timetable entry one past the next stop, and whether the train stops there + auto const *owner { ( + m_activator->Mechanik != nullptr && m_activator->Mechanik->primary() ? + m_activator->Mechanik : + m_activator->ctOwner ) }; + auto const *entry { ( + owner != nullptr ? + &owner->TrainTimetable().TimeTableEntryAhead( 1 ) : + nullptr ) }; + auto const aheadname { ( entry != nullptr ? entry->StationName : "none" ) }; + auto const aheadstop { ( entry != nullptr && entry->Ah >= 0 ? 1 : 0 ) }; + + targetcell->UpdateValues( + aheadname, // station after the next one + 0, // unused + aheadstop, // stop there or passthrough + m_input.flags & ( flags::text | flags::value1 | flags::value2 ) ); + + WriteLog( + "Type: WhoIs (" + std::to_string( m_input.flags ) + ") - " + + "[station after next: " + aheadname + "], " + + "[X], " + + "[stop there: " + ( aheadstop != 0 ? "yes" : "no" ) + "]" ); + } + else if( m_input.flags & flags::whois_name ) { // +32 or +40 // next station name if( m_input.flags & flags::mode_alt ) { diff --git a/world/mtable.cpp b/world/mtable.cpp index b69cacfb9..123ae00f6 100644 --- a/world/mtable.cpp +++ b/world/mtable.cpp @@ -77,6 +77,15 @@ bool TTrainParameters::IsLastStop() const { return StationIndex >= StationCount; } +TMTableLine const &TTrainParameters::TimeTableEntryAhead( int Offset ) const +{ // wpis rozkladu Offset pozycji za aktualnym (bez pomijania przelotow); + // poza zakresem 1..StationCount zwraca techniczny wpis zerowy + int const idx { StationIndex + Offset }; + if( ( idx < 1 ) || ( idx > StationCount ) ) + return TimeTable[0]; + return TimeTable[idx]; +} + bool TTrainParameters::IsMaintenance() const { if( StationIndex <= StationCount ) diff --git a/world/mtable.h b/world/mtable.h index 44cda7311..841fea815 100644 --- a/world/mtable.h +++ b/world/mtable.h @@ -70,6 +70,9 @@ class TTrainParameters std::string ShowRelation() const; double WatchMTable(double DistCounter) const; std::string NextStop() const; + // wpis rozkladu Offset pozycji za StationIndex (Offset>=1); poza zakresem 1..StationCount + // zwraca techniczny wpis zerowy (TimeTable[0], StationName=="nowhere", Ah==-1) + TMTableLine const &TimeTableEntryAhead( int Offset ) const; sound_source next_stop_sound() const; sound_source last_stop_sound() const; bool IsStop() const;