diff --git a/McZapkie/Mover.cpp b/McZapkie/Mover.cpp index b1735d0c5..5143b359a 100644 --- a/McZapkie/Mover.cpp +++ b/McZapkie/Mover.cpp @@ -13759,7 +13759,11 @@ void TMoverParameters::LoadFIZ_EngineInductionMotor(std::string const &Input) extract_value(eimc[eimc_f_DU], "DU", Input, ""); extract_value(eimc[eimc_f_I0], "I0", Input, ""); extract_value(eimc[eimc_f_cfu], "fcfu", Input, ""); - extract_value(eimc[eimc_f_cfuH], "fcfuH", Input, std::to_string(eimc[eimc_f_cfu])); + if (false == extract_value(eimc[eimc_f_cfuH], "fcfuH", Input, "")) + { + // without its own figure the dynamic brake takes the one of the traction + eimc[eimc_f_cfuH] = eimc[eimc_f_cfu]; + } extract_value(eimc[eimc_p_F0], "F0", Input, ""); extract_value(eimc[eimc_p_a1], "a1", Input, ""); extract_value(eimc[eimc_p_Pmax], "Pmax", Input, ""); diff --git a/tests/unit/test_fcfuh_default.cpp b/tests/unit/test_fcfuh_default.cpp new file mode 100644 index 000000000..437164060 --- /dev/null +++ b/tests/unit/test_fcfuh_default.cpp @@ -0,0 +1,153 @@ +#include "stdafx.h" +#include "doctest.h" +#include "vehicle.h" +#include "utilities/utilities.h" + +#include +#include +#include +#include +#include +#include + +using test::definition; +using test::vehicle; + +// the figure of the voltage by the frequency of an induction motor drive in the dynamic brake (fcfuH) takes the +// one of the traction (fcfu) when the definition has none. the loader took it by writing fcfu out with +// std::to_string() and reading the text back as the default of fcfuH (eu07/maszyna#99); it assigns it now + +namespace { + +// a figure read from a line the way TMoverParameters::LoadFIZ_EngineInductionMotor() reads fcfu +double read_fcfu(std::string const &Text) { + double fcfu{0.0}; + extract_value(fcfu, "fcfu", "fcfu=" + Text, ""); + return fcfu; +} + +// fcfuH missing, the way the loader took it before: the figure written out with six decimals in the C locale +// (the game never sets another) and read back +double fcfuh_before(double const Fcfu) { + double fcfuh{-1.0}; + extract_value(fcfuh, "fcfuH", "", std::to_string(Fcfu)); + return fcfuh; +} + +// the bits of the value, which tell -0.0 from 0.0 too +std::uint64_t bits(double const Value) { + std::uint64_t result; + std::memcpy(&result, &Value, sizeof(result)); + return result; +} + +// the value with all its digits, for the message +std::string exact(double const Value) { + char text[64]; + std::snprintf(text, sizeof(text), "%.17g", Value); + return text; +} + +// a generator of its own (splitmix64), for the same numbers on every platform +struct numbers { + std::uint64_t state{0x2545f4914f6cdd1dull}; + std::uint64_t next() { + auto value{state += 0x9e3779b97f4a7c15ull}; + value = (value ^ (value >> 30)) * 0xbf58476d1ce4e5b9ull; + value = (value ^ (value >> 27)) * 0x94d049bb133111ebull; + return value ^ (value >> 31); + } +}; + +// the decimal text of Integer / 10^Decimals, as a definition could hold it +std::string decimal(std::uint64_t const Integer, int const Decimals, bool const Negative) { + auto digits{std::to_string(Integer)}; + if (Decimals > 0) { + if (digits.size() <= static_cast(Decimals)) { + digits.insert(0, Decimals + 1 - digits.size(), '0'); + } + digits.insert(digits.size() - Decimals, "."); + } + return (Negative ? "-" : "") + digits; +} + +} // namespace + +TEST_CASE("fcfuH: the figure of the traction is the same, assigned or written out and read back, for every figure of up to six decimals") { + // any decimal figure of up to six decimals: the value read from it lies within half a millionth of it, so + // std::to_string() writes the figure itself and the text reads back to the very same value. checked over the + // figures the game's definitions use (33.5 to 81.5) and the ones around them, and over many others, of up + // to six decimals and up to 18 digits, of either sign + auto compared{0}; + auto const check{[&](std::string const &Text) { + auto const fcfu{read_fcfu(Text)}; + auto const before{fcfuh_before(fcfu)}; + if (bits(before) != bits(fcfu)) { + FAIL_CHECK("fcfu=" << Text << " was " << exact(before) << " as fcfuH, now " << exact(fcfu)); + } + ++compared; + }}; + // every figure of up to two decimals, from -10 to 200 + for (auto hundredths{-1000}; hundredths <= 20000; ++hundredths) { + check(decimal(static_cast(std::abs(hundredths)), 2, hundredths < 0)); + } + // every figure of up to six decimals around the ones of the game's definitions + for (auto const figure : {33.5, 40.1, 43.7, 45.18, 46.8, 47.6, 48.9, 49.0, 81.5}) { + auto const millionths{static_cast(figure * 1e6 + 0.5)}; + for (auto offset{-2000}; offset <= 2000; ++offset) { + check(decimal(static_cast(millionths + offset), 6, false)); + } + } + // figures of 0 to 6 decimals and 1 to 18 digits + numbers random; + for (auto decimals{0}; decimals <= 6; ++decimals) { + for (auto digits{1}; digits <= 18; ++digits) { + std::uint64_t limit{1}; + for (auto digit{0}; digit < digits; ++digit) { + limit *= 10; + } + for (auto sample{0}; sample < 400; ++sample) { + check(decimal(random.next() % limit, decimals, (sample & 1) != 0)); + } + } + } + // the way of writing it doesn't matter, only the value + for (auto const text : {"4.76e1", "47.60", "+47.6", "0047.6", "476e-1", "0", "-0", "1e15", "-1e-6", "1e300"}) { + check(text); + } + // nor does a figure the stream can't read, which reads as 0 + for (auto const text : {"abc", "-", "x47.6"}) { + check(text); + } + CHECK(compared > 70000); +} + +TEST_CASE("fcfuH: where the two ways part, figures no definition has") { + // no definition of the game has such a figure (they have one or two decimals, from 33.5 to 81.5). more than + // six decimals: std::to_string() rounded the value to six, the figure is taken as it's written now + CHECK(fcfuh_before(read_fcfu("0.1234567")) == read_fcfu("0.123457")); + CHECK(fcfuh_before(read_fcfu("1e-7")) == 0.0); + CHECK(read_fcfu("1e-7") == 1e-7); + // a figure past the largest value reads as infinite (with MSVC; the largest value with GCC): written out it + // was "inf", which the stream doesn't read, so the dynamic brake got 0 where it gets the traction's figure now + auto const huge{read_fcfu("1e999")}; + CHECK(huge > 1e300); + if (std::isinf(huge)) { + CHECK(fcfuh_before(huge) == 0.0); + } +} + +TEST_CASE("fcfuH: the loader gives the dynamic brake the traction's figure when the definition has none") { + for (auto const fcfu : {"43.7", "45.18", "49", "0.000001", "1e2"}) { + CAPTURE(fcfu); + // the EU47's definition has fcfu only + vehicle eu47{definition::fiz("e186_v2/eu47").set("Engine", "fcfu", fcfu)}; + CHECK(eu47->eimc[eimc_f_cfu] == read_fcfu(fcfu)); + CHECK(bits(eu47->eimc[eimc_f_cfuH]) == bits(eu47->eimc[eimc_f_cfu])); + CHECK(bits(eu47->eimc[eimc_f_cfuH]) == bits(fcfuh_before(read_fcfu(fcfu)))); + // its own figure is kept + vehicle own{definition::fiz("e186_v2/eu47").set("Engine", "fcfu", fcfu).set("Engine", "fcfuH", "40.1")}; + CHECK(own->eimc[eimc_f_cfu] == read_fcfu(fcfu)); + CHECK(own->eimc[eimc_f_cfuH] == read_fcfu("40.1")); + } +} diff --git a/tests/unit/test_linebreaker.cpp b/tests/unit/test_linebreaker.cpp index 126e3307c..9b36b817f 100644 --- a/tests/unit/test_linebreaker.cpp +++ b/tests/unit/test_linebreaker.cpp @@ -9,6 +9,7 @@ using test::consist; using test::definition; +using linebreaker::state; namespace { @@ -16,7 +17,7 @@ namespace { // the cab keeps of the line breaker follows the vehicle, the breaker closes while the lever is on and can close struct lever { TMoverParameters &mover; - int breaker{0}; // as TTrain::m_linebreakerstate: 0 open, 1 closed + state breaker{state::open}; // as TTrain::m_linebreakerstate bool on{false}; void press() { @@ -26,8 +27,8 @@ struct lever { break; case linebreaker::toggle::open: on = false; - if (breaker == 1 && mover.MainSwitch(false)) { - breaker = 0; + if (breaker == state::closed && mover.MainSwitch(false)) { + breaker = state::open; } break; default: @@ -36,14 +37,14 @@ struct lever { } // TTrain::Update() void update() { - if (breaker == 0 && mover.Mains) { - breaker = 1; + if (breaker == state::open && mover.Mains) { + breaker = state::closed; } - if (breaker == 1 && false == mover.Mains) { - breaker = 0; + if (breaker == state::closed && false == mover.Mains) { + breaker = state::open; } - if (on && breaker == 0 && mover.MainSwitchCheck()) { - breaker = mover.MainSwitch(true) ? 1 : 0; + if (on && breaker == state::open && mover.MainSwitchCheck()) { + breaker = mover.MainSwitch(true) ? state::closed : state::open; } } }; @@ -53,15 +54,15 @@ struct lever { TEST_CASE("line breaker: a press of the toggle closes an open breaker and opens a closed one") { using enum linebreaker::toggle; for (auto const latching : {false, true}) { - CHECK(linebreaker::pressed(0, latching, false) == close); - CHECK(linebreaker::pressed(1, latching, true) == open); - CHECK(linebreaker::pressed(1, latching, false) == open); + CHECK(linebreaker::pressed(state::open, latching, false) == close); + CHECK(linebreaker::pressed(state::closed, latching, true) == open); + CHECK(linebreaker::pressed(state::closed, latching, false) == open); // freshly closed, the cab finishes closing it first - CHECK(linebreaker::pressed(2, latching, true) == none); + CHECK(linebreaker::pressed(state::closing, latching, true) == none); } // a lever left on over an open breaker goes off; push buttons have no position to keep - CHECK(linebreaker::pressed(0, true, true) == open); - CHECK(linebreaker::pressed(0, false, true) == close); + CHECK(linebreaker::pressed(state::open, true, true) == open); + CHECK(linebreaker::pressed(state::open, false, true) == close); } TEST_CASE("line breaker: the EP05's lever goes off with the converter overload relay tripped (eu07/maszyna#394)") { @@ -72,7 +73,7 @@ TEST_CASE("line breaker: the EP05's lever goes off with the converter overload r REQUIRE(test::start_up(driver, line)); REQUIRE(ep05.Mains); REQUIRE(ep05.ConverterFlag); - lever ws{ep05, 1, true}; + lever ws{ep05, state::closed, true}; // the converter overload relay knocks the line breaker out, as the cab does it when the converter is // switched on together with the compressor; the lever stays on @@ -80,7 +81,7 @@ TEST_CASE("line breaker: the EP05's lever goes off with the converter overload r ep05.MainSwitch(false, range_t::local); ws.update(); REQUIRE_FALSE(ep05.Mains); - REQUIRE(ws.breaker == 0); + REQUIRE(ws.breaker == state::open); CHECK(ws.on); // with the relay tripped the breaker doesn't close test::drive(driver, line, 5.0); @@ -106,7 +107,7 @@ TEST_CASE("line breaker: the EP05's lever goes off with the converter overload r ws.press(); ws.update(); CHECK(ep05.Mains); - CHECK(ws.breaker == 1); + CHECK(ws.breaker == state::closed); // and the next press opens it ws.press(); CHECK_FALSE(ws.on); diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index 8bf948096..3f2cbebae 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -3748,7 +3748,7 @@ void TTrain::OnCommand_linebreakertoggle(TTrain *Train, command_data const &Comm { // only impulse switches react to release events // NOTE: we presume dedicated state switch is of impulse type - if (Train->m_linebreakerstate == 0) + if (Train->m_linebreakerstate == linebreaker::state::open) { // ...after opening circuit, or holding for too short time to close it OnCommand_linebreakeropen(Train, Command); @@ -3801,14 +3801,14 @@ void TTrain::OnCommand_linebreakeropen(TTrain *Train, command_data const &Comman // play sound immediately when the switch is hit, not after release Train->fMainRelayTimer = 0.0f; - if (Train->m_linebreakerstate == 0) + if (Train->m_linebreakerstate == linebreaker::state::open) { return; } // already in the desired state if (true == Train->mvControlled->MainSwitch(false, Train->mvControlled->MainSwitchRange())) { - Train->m_linebreakerstate = 0; + Train->m_linebreakerstate = linebreaker::state::open; if (Train->mvControlled->MainSwitchRange() != range_t::local) { // the engines of the vehicles in multiple stopped along: their two-state switches go off too, left on @@ -3883,7 +3883,7 @@ void TTrain::OnCommand_linebreakerclose(TTrain *Train, command_data const &Comma Train->ggMainButton.UpdateValue(0.5, Train->dsbSwitch); } - if (Train->m_linebreakerstate == 1) + if (Train->m_linebreakerstate == linebreaker::state::closed) { return; } // already in the desired state @@ -3891,10 +3891,10 @@ void TTrain::OnCommand_linebreakerclose(TTrain *Train, command_data const &Comma // we don't need to start the diesel twice, but the other types (with impulse switch setup) still need to be launched // NOTE: this behaviour should depend on MainOnButton presence and type_delayed // TODO: change it when/if vehicle definition files get their proper switch types - if (Train->m_linebreakerstate == 2 && Train->mvControlled->EngineType == TEngineType::ElectricSeriesMotor) + if (Train->m_linebreakerstate == linebreaker::state::closing && Train->mvControlled->EngineType == TEngineType::ElectricSeriesMotor) { // try to finalize state change of the line breaker, set the state based on the outcome - Train->m_linebreakerstate = Train->mvControlled->MainSwitch(true, Train->mvControlled->MainSwitchRange()) ? 1 : 0; + Train->m_linebreakerstate = Train->mvControlled->MainSwitch(true, Train->mvControlled->MainSwitchRange()) ? linebreaker::state::closed : linebreaker::state::open; } // on button release reset the closing timer Train->fMainRelayTimer = 0.0f; @@ -8762,16 +8762,16 @@ bool TTrain::Update(double const Deltatime) { // train state update // line breaker: - if (m_linebreakerstate == 0 && true == mvControlled->Mains) + if (m_linebreakerstate == linebreaker::state::open && true == mvControlled->Mains) { // crude way to sync state of the linebreaker with ai-issued commands - m_linebreakerstate = 1; + m_linebreakerstate = linebreaker::state::closed; } - if (m_linebreakerstate == 1 && false == (mvControlled->Mains || mvControlled->dizel_startup)) + if (m_linebreakerstate == linebreaker::state::closed && false == (mvControlled->Mains || mvControlled->dizel_startup)) { // crude way to catch cases where the main was knocked out // because the state of the line breaker isn't changed to match, we need to do it here manually - m_linebreakerstate = 0; + m_linebreakerstate = linebreaker::state::open; } if (is_hold_coil_lever(ggMainOffButton) && hold_coil_lever::cuts_off(ggMainOffButton.GetDesiredValue())) { @@ -8781,10 +8781,10 @@ bool TTrain::Update(double const Deltatime) // put the lever back on to match, like the line breaker state above ggMainOffButton.UpdateValue(0.0); } - else if (m_linebreakerstate == 2) + else if (m_linebreakerstate == linebreaker::state::closing) { // the hold coil was cut off while the closing button was held, the line breaker won't close on its release - m_linebreakerstate = 0; + m_linebreakerstate = linebreaker::state::open; } } @@ -8808,18 +8808,18 @@ bool TTrain::Update(double const Deltatime) // if the button disconnecting the line breaker is down prevent the timer from accumulating fMainRelayTimer = 0.0f; } - if (m_linebreakerstate == 0 && fMainRelayTimer > mvControlled->InitialCtrlDelay) + if (m_linebreakerstate == linebreaker::state::open && fMainRelayTimer > mvControlled->InitialCtrlDelay) { // wlaczanie WSa z opoznieniem // mark the line breaker as ready to close; for electric series vehicles with impulse switch the setup is completed on button release - m_linebreakerstate = 2; + m_linebreakerstate = linebreaker::state::closing; } // for diesels and/or vehicles with toggle switch setup we complete the engine start here // TODO: make it a test for main_on_bt of type push_delayed instead - if (m_linebreakerstate == 2 && (ggMainOnButton.SubModel == nullptr || mvControlled->EngineType != TEngineType::ElectricSeriesMotor)) + if (m_linebreakerstate == linebreaker::state::closing && (ggMainOnButton.SubModel == nullptr || mvControlled->EngineType != TEngineType::ElectricSeriesMotor)) { // try to finalize state change of the line breaker, set the state based on the outcome - m_linebreakerstate = mvControlled->MainSwitch(true, mvControlled->MainSwitchRange()) ? 1 : 0; + m_linebreakerstate = mvControlled->MainSwitch(true, mvControlled->MainSwitchRange()) ? linebreaker::state::closed : linebreaker::state::open; } // the switches of the other vehicles in multiple update_remote_linebreakers(Deltatime); @@ -9393,10 +9393,10 @@ bool TTrain::Update(double const Deltatime) btLampkaSHP.Turn(mvOccupied->SecuritySystem.is_cabsignal_blinking()); btLampkaCzuwakaSHP.Turn(btLampkaSHP.GetValue() || btLampkaCzuwaka.GetValue()); - btLampkaWylSzybki.Turn(m_linebreakerstate == 2 || mvControlled->Mains); - btLampkaWylSzybkiOff.Turn(m_linebreakerstate != 2 && !mvControlled->Mains); - btLampkaMainBreakerReady.Turn(mvControlled->MainsInitTimeCountdown <= 0.0 && m_linebreakerstate != 2 && !mvControlled->Mains); - btLampkaMainBreakerBlinkingIfReady.Turn(m_linebreakerstate == 2 || mvControlled->Mains || mvControlled->MainsInitTimeCountdown < 0.0 && simulation::Time.data().wMilliseconds > 500); + btLampkaWylSzybki.Turn(m_linebreakerstate == linebreaker::state::closing || mvControlled->Mains); + btLampkaWylSzybkiOff.Turn(m_linebreakerstate != linebreaker::state::closing && !mvControlled->Mains); + btLampkaMainBreakerReady.Turn(mvControlled->MainsInitTimeCountdown <= 0.0 && m_linebreakerstate != linebreaker::state::closing && !mvControlled->Mains); + btLampkaMainBreakerBlinkingIfReady.Turn(m_linebreakerstate == linebreaker::state::closing || mvControlled->Mains || mvControlled->MainsInitTimeCountdown < 0.0 && simulation::Time.data().wMilliseconds > 500); btLampkaPrzetw.Turn(mvOccupied->Power110vIsAvailable); btLampkaPrzetwOff.Turn(!mvOccupied->Power110vIsAvailable); @@ -11808,7 +11808,7 @@ void TTrain::set_cab_controls(int const Cab) // line breaker if (ggMainButton.SubModel != nullptr) { // instead of single main button there can be on/off pair - ggMainButton.PutValue(ggMainButton.type() == TGaugeType::push ? 0.5f : m_linebreakerstate > 0 ? 1.f : 0.f); + ggMainButton.PutValue(ggMainButton.type() == TGaugeType::push ? 0.5f : m_linebreakerstate != linebreaker::state::open ? 1.f : 0.f); } // line breakers of the other vehicles in multiple for (std::size_t index = 0; index < m_remotelinebreakers.size(); ++index) diff --git a/vehicle/Train.h b/vehicle/Train.h index e6cc0f3ba..c079c3f18 100644 --- a/vehicle/Train.h +++ b/vehicle/Train.h @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include "vehicle/Button.h" #include "vehicle/Gauge.h" #include "vehicle/enginestart.h" +#include "vehicle/linebreaker.h" #include "audio/sound.h" #include "scripting/PyInt.h" #include "input/command.h" @@ -553,7 +554,7 @@ class TTrain { TMoverParameters *mvThird { nullptr }; // trzeci człon (SN61) TMoverParameters *mvPantographUnit { nullptr }; // nearest pantograph equipped unit // helper variable, to prevent immediate switch between closing and opening line breaker circuit - int m_linebreakerstate { 0 }; // 0: open, 1: closed, 2: freshly closed (and yes this is awful way to go about it) + linebreaker::state m_linebreakerstate { linebreaker::state::open }; static const commandhandler_map m_commandhandlers; control_mapper m_controlmapper; diff --git a/vehicle/linebreaker.cpp b/vehicle/linebreaker.cpp index 97eae0325..cc9b2f008 100644 --- a/vehicle/linebreaker.cpp +++ b/vehicle/linebreaker.cpp @@ -12,11 +12,11 @@ http://mozilla.org/MPL/2.0/. namespace linebreaker { -toggle pressed(int const Breakerstate, bool const Latchinglever, bool const Leveron) { - if (Breakerstate == 1) { +toggle pressed(state const Breakerstate, bool const Latchinglever, bool const Leveron) { + if (Breakerstate == state::closed) { return toggle::open; } - if (Breakerstate != 0) { + if (Breakerstate != state::open) { return toggle::none; } // the line breaker is open; a lever left on goes off first diff --git a/vehicle/linebreaker.h b/vehicle/linebreaker.h index 370e992cc..e87312dd1 100644 --- a/vehicle/linebreaker.h +++ b/vehicle/linebreaker.h @@ -12,6 +12,13 @@ http://mozilla.org/MPL/2.0/. // the line breaker (main switch, WS) controls of the cab namespace linebreaker { +// the state the cab keeps of the line breaker, besides the vehicle's own (TMoverParameters::Mains) +enum class state : int { + open, + closed, + closing // the closing control was held long enough; the line breaker closes when it's released, or right away +}; + enum class toggle : int { none, // the press does nothing close, // the control goes to its on position, and the line breaker closes if it can @@ -19,10 +26,10 @@ enum class toggle : int { }; // what a press of the line breaker toggle does. Breakerstate is the state the cab keeps of the line -// breaker (0: open, 1: closed, 2: freshly closed). Latchinglever is a single two-state switch which -// stays where it was put, Leveron that it stands in its on position. such a lever doesn't follow the -// line breaker: knocked out (by the converter overload relay, say) the breaker opens with the lever -// left on, and the lever has to go off before it can be put on again (eu07/maszyna#394) -toggle pressed(int Breakerstate, bool Latchinglever, bool Leveron); +// breaker. Latchinglever is a single two-state switch which stays where it was put, Leveron that it +// stands in its on position. such a lever doesn't follow the line breaker: knocked out (by the converter +// overload relay, say) the breaker opens with the lever left on, and the lever has to go off before it +// can be put on again (eu07/maszyna#394) +toggle pressed(state Breakerstate, bool Latchinglever, bool Leveron); } // namespace linebreaker