diff --git a/McZapkie/MOVER.h b/McZapkie/MOVER.h index f7c983bbf..c6c856597 100644 --- a/McZapkie/MOVER.h +++ b/McZapkie/MOVER.h @@ -10,7 +10,9 @@ http://mozilla.org/MPL/2.0/. #pragma once //--------------------------------------------------------------------------- // Q: 20160805 - odlaczenie pliku fizyki .pas od kompilacji +#include #include +#include #include "hamulce.h" #include "scripting/ladderlogic.h" @@ -340,6 +342,21 @@ enum light highbeamlight_right = 1 << 10 }; +/// The cab interior light a position of the light preset selector sets (eu07/maszyna#529). +struct lights_preset_cablight +{ + /// Bits of the optional third column of a LightsList row. + enum flag + { + /// The cab light is on. + light_on = 1 << 0, + /// The cab light is dimmed. + light_dimmed = 1 << 1 + }; + bool light{false}; + bool dim{false}; +}; + /// Door operation method (who controls them and how) — mutually exclusive. enum control_t { @@ -1720,6 +1737,13 @@ class TMoverParameters int LightsDefPos = 1; bool LightsWrap = false; int Lights[2][18]; // pozycje świateł, przód - tył, 1 .. 16 (17 do lightsset) + // the cab interior light each light preset sets, from the optional third column of LightsList (bits of + // lights_preset_cablight::flag); -1: the preset leaves the cab light as it is (eu07/maszyna#529) + std::array LightsCabLight{[] { + std::array positions; + positions.fill(-1); + return positions; + }()}; int ScndInMain{0}; /*zaleznosc bocznika od nastawnika*/ bool MBrake = false; /*Czy jest hamulec reczny*/ double maxTachoSpeed = {0.0}; // maksymalna predkosc na tarczce predkosciomierza analogowego @@ -2642,6 +2666,8 @@ class TMoverParameters bool LoadFIZ_Text(std::string const &Text); // the same, from text in the .fiz format bool CheckLocomotiveParameters(bool ReadyFlag, int Dir); std::string EngineDescription(int what) const; + // the cab interior light the light preset at given position (1-based, as LightsPos) sets, if it sets one + std::optional LightsPresetCabLight(int Position) const; private: bool LoadFIZ_Parse(cParser &Parser); // reads the definition from either of the above diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index 0dec03ea9..e707ede20 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -17,6 +17,7 @@ http://mozilla.org/MPL/2.0/. #include "utilities/parser.h" #include "simulation/simulation.h" +#include #include #include //--------------------------------------------------------------------------- @@ -9599,10 +9600,38 @@ bool TMoverParameters::readLightsList(std::string const &Input) return false; } parser >> Lights[0][idx] >> Lights[1][idx]; + // optional third column: the cab interior light the preset sets (eu07/maszyna#529). anything but a whole number + // (some rows carry stray notes after the two columns) leaves the cab light to its own switch + LightsCabLight[idx] = -1; + if (true == parser.getTokens(1, false)) + { + std::string cablight; + parser >> cablight; + int flags{-1}; + auto const [end, error]{std::from_chars(cablight.data(), cablight.data() + cablight.size(), flags)}; + if (error == std::errc{} && end == cablight.data() + cablight.size() && flags >= 0) + { + LightsCabLight[idx] = flags; + } + } return true; } +std::optional TMoverParameters::LightsPresetCabLight(int const Position) const +{ + if (Position < 1 || Position > static_cast(LightsCabLight.size())) + { + return std::nullopt; + } + auto const flags{LightsCabLight[Position - 1]}; + if (flags < 0) + { + return std::nullopt; + } + return lights_preset_cablight{(flags & lights_preset_cablight::light_on) != 0, (flags & lights_preset_cablight::light_dimmed) != 0}; +} + bool TMoverParameters::readCompressorList(std::string const &Input) { diff --git a/tests/unit/test_lights_preset_cablight.cpp b/tests/unit/test_lights_preset_cablight.cpp new file mode 100644 index 000000000..7c64c7d5a --- /dev/null +++ b/tests/unit/test_lights_preset_cablight.cpp @@ -0,0 +1,96 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" + +// the cab interior light set by the positions of the light preset selector: an optional third column of LightsList, +// 1 the cab light on, 2 dimmed, 0 off; a row without it leaves the cab light to its own switch (eu07/maszyna#529) + +using test::definition; +using test::vehicle; + +namespace { + +auto const shunting{light::headlight_left | light::headlight_right}; + +} // namespace + +TEST_CASE("lights preset cab light: a table without the column sets no position's cab light") { + vehicle wagon{definition::load("wagon") + .set("LightsList", "Size", 2) + .set("LightsList", "Default", 1) + .set("LightsList", "rows", {{0, 0}, {shunting, shunting}})}; + + REQUIRE(wagon->LightsPosNo == 2); + CHECK(wagon->Lights[end::front][1] == shunting); + CHECK_FALSE(wagon->LightsPresetCabLight(1).has_value()); + CHECK_FALSE(wagon->LightsPresetCabLight(2).has_value()); +} + +TEST_CASE("lights preset cab light: the third column turns the cab light off, on, or on dimmed") { + // off - shunting - shunting with the cab lit - shunting with the cab lit dimmed - shunting leaving the cab light + vehicle wagon{definition::load("wagon") + .set("LightsList", "Size", 5) + .set("LightsList", "Default", 1) + .set("LightsList", "rows", {{0, 0, 0}, {shunting, shunting}, {shunting, shunting, 1}, {shunting, shunting, 3}, {shunting, 0, -1}})}; + + REQUIRE(wagon->LightsPosNo == 5); + // the light columns read as before + CHECK(wagon->Lights[end::front][2] == shunting); + CHECK(wagon->Lights[end::rear][2] == shunting); + CHECK(wagon->Lights[end::rear][4] == 0); + + auto const off{wagon->LightsPresetCabLight(1)}; + REQUIRE(off.has_value()); + CHECK_FALSE(off->light); + CHECK_FALSE(off->dim); + + CHECK_FALSE(wagon->LightsPresetCabLight(2).has_value()); + + auto const lit{wagon->LightsPresetCabLight(3)}; + REQUIRE(lit.has_value()); + CHECK(lit->light); + CHECK_FALSE(lit->dim); + + auto const dimmed{wagon->LightsPresetCabLight(4)}; + REQUIRE(dimmed.has_value()); + CHECK(dimmed->light); + CHECK(dimmed->dim); + + // a negative value is the same as no value + CHECK_FALSE(wagon->LightsPresetCabLight(5).has_value()); +} + +TEST_CASE("lights preset cab light: positions outside the table, and the lightsset custom one, set nothing") { + vehicle wagon{definition::load("wagon") + .set("LightsList", "Size", 1) + .set("LightsList", "Default", 1) + .set("LightsList", "rows", {{shunting, shunting, 1}})}; + + CHECK(wagon->LightsPresetCabLight(1).has_value()); + CHECK_FALSE(wagon->LightsPresetCabLight(0).has_value()); + CHECK_FALSE(wagon->LightsPresetCabLight(2).has_value()); + CHECK_FALSE(wagon->LightsPresetCabLight(18).has_value()); + CHECK_FALSE(wagon->LightsPresetCabLight(19).has_value()); +} + +TEST_CASE("lights preset cab light: a stray note after the two columns is not read as the cab light") { + // some vehicles of the game carry rows like "38 38 / A" + auto text{definition::load("wagon") + .set("LightsList", "Size", 2) + .set("LightsList", "Default", 1) + .set("LightsList", "rows", {{0, 0}, {38, 38}}) + .text()}; + auto const list{text.find("LightsList")}; + REQUIRE(list != std::string::npos); + auto const row{text.find("38", list)}; + REQUIRE(row != std::string::npos); + auto const rowend{text.find('\n', row)}; + REQUIRE(rowend != std::string::npos); + text.insert(rowend, " / A"); + TMoverParameters wagon(0.0, "wagon", "test", 0); + REQUIRE(wagon.LoadFIZ_Text(text)); + REQUIRE(wagon.LightsPosNo == 2); + CHECK(wagon.Lights[end::front][1] == 38); + CHECK(wagon.Lights[end::rear][1] == 38); + CHECK_FALSE(wagon.LightsPresetCabLight(2).has_value()); +} diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 63ec0cda5..1ff884e23 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -5329,6 +5329,39 @@ void TTrain::OnCommand_universalrelayreset(TTrain *Train, command_data const &Co } } +// the cab interior light set by the light preset just selected, if the preset sets one (eu07/maszyna#529). +// the cab light switch (cablight_sw:) is bound to the light state and follows it by itself +void TTrain::set_cab_light_from_lights_preset() +{ + auto const cablight{mvOccupied->LightsPresetCabLight(mvOccupied->LightsPos)}; + if (false == cablight.has_value()) + { + return; + } + auto const set{[&cablight](TCab &Cab) { + Cab.bLight = cablight->light; + Cab.bLightDim = cablight->dim; + }}; + if (false == DynamicObject->JointCabs) + { + // vehicles with separate cabs get separate lighting switch states + set(Cabine[iCabn]); + } + else + { + // joint virtual cabs share lighting switch states + for (auto &cab : Cabine) + { + set(cab); + } + } + // the dimmer switch is driven by commands only, move it along + if (ggCabLightDimButton.SubModel != nullptr) + { + ggCabLightDimButton.UpdateValue(cablight->dim ? 1.f : 0.f); + } +} + void TTrain::OnCommand_lightspresetactivatenext(TTrain *Train, command_data const &Command) { @@ -5350,6 +5383,7 @@ void TTrain::OnCommand_lightspresetactivatenext(TTrain *Train, command_data cons Train->mvOccupied->LightsPos = false == restartcycle ? Train->mvOccupied->LightsPos + 1 : 1; // wrap mode Train->Dynamic()->SetLights(); + Train->set_cab_light_from_lights_preset(); // visual feedback if (Train->ggLightsButton.SubModel != nullptr) { @@ -5387,6 +5421,7 @@ void TTrain::OnCommand_lightspresetactivateprevious(TTrain *Train, command_data Train->mvOccupied->LightsPos = false == restartcycle ? Train->mvOccupied->LightsPos - 1 : Train->mvOccupied->LightsPosNo; // wrap mode Train->Dynamic()->SetLights(); + Train->set_cab_light_from_lights_preset(); // visual feedback if (Train->ggLightsButton.SubModel != nullptr) { diff --git a/vehicle/Train.h b/vehicle/Train.h index 8e57a1021..0048787e5 100644 --- a/vehicle/Train.h +++ b/vehicle/Train.h @@ -190,6 +190,8 @@ class TTrain { // sets cabin controls based on current state of the vehicle // NOTE: we can get rid of this function once we have per-cab persistent state void set_cab_controls( int const Cab ); + // sets the cab interior light as the selected light preset says, if it says anything (eu07/maszyna#529) + void set_cab_light_from_lights_preset(); // initializes a gauge matching provided label. returns: true if the label was found, false otherwise bool initialize_gauge(cParser &Parser, std::string const &Label, int const Cabindex); // initializes a button matching provided label. returns: true if the label was found, false otherwise