mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-16 00:34:11 +01:00
Merge pull request #286 from MaSzyna-EU07/fix/dimmer-position-bounds
fix(lights): keep the headlight dimmer position within the DimmerList
This commit is contained in:
@@ -2306,6 +2306,14 @@ class TMoverParameters
|
||||
// bool modernContainOffPos{true};
|
||||
bool enableModernDimmer{false};
|
||||
bool modernDimmerCanCycle{false};
|
||||
// the given dimmer position brought into the DimmerList, to its first or last entry; 0 for an empty list
|
||||
int clampDimmerPosition(int Position) const;
|
||||
// the entry of the current dimmer position, or a plain one (not off, not dimmed, no high beam) when the
|
||||
// position is outside the DimmerList: the .fiz can give a short or an empty list, or a DefaultPos past its end
|
||||
dimPosition currentDimPosition() const;
|
||||
// moves the dimmer by Step positions within the DimmerList, around to its other end when it can cycle;
|
||||
// a position outside the list is brought into it first. false when the dimmer doesn't move
|
||||
bool stepDimmerPosition(int Step);
|
||||
|
||||
// Barwa reflektora
|
||||
int refR{255}; // Czerwony
|
||||
|
||||
@@ -9508,6 +9508,52 @@ bool TMoverParameters::readDimmerList(std::string const &line)
|
||||
return true;
|
||||
}
|
||||
|
||||
int TMoverParameters::clampDimmerPosition(int const Position) const
|
||||
{
|
||||
if (dimPositions.empty())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return std::clamp(Position, 0, static_cast<int>(dimPositions.size()) - 1);
|
||||
}
|
||||
|
||||
TMoverParameters::dimPosition TMoverParameters::currentDimPosition() const
|
||||
{
|
||||
if (modernDimmerPosition < 0 || modernDimmerPosition >= static_cast<int>(dimPositions.size()))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
return dimPositions[modernDimmerPosition];
|
||||
}
|
||||
|
||||
bool TMoverParameters::stepDimmerPosition(int const Step)
|
||||
{
|
||||
if (dimPositions.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto const last{static_cast<int>(dimPositions.size()) - 1};
|
||||
auto const current{clampDimmerPosition(modernDimmerPosition)};
|
||||
auto next{current + Step};
|
||||
if (next < 0 || next > last)
|
||||
{
|
||||
if (false == modernDimmerCanCycle)
|
||||
{
|
||||
next = current;
|
||||
}
|
||||
else
|
||||
{
|
||||
next = (next < 0 ? last : 0);
|
||||
}
|
||||
}
|
||||
if (next == modernDimmerPosition)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
modernDimmerPosition = next;
|
||||
return true;
|
||||
}
|
||||
|
||||
// parsowanie WWList
|
||||
bool TMoverParameters::readWWList(std::string const &line)
|
||||
{
|
||||
@@ -10309,6 +10355,9 @@ bool TMoverParameters::LoadFIZ_Parse(cParser &fizparser) // NOSONAR the former b
|
||||
result = false;
|
||||
|
||||
// ustawiamy domyslna pozycje dimmera
|
||||
// the DefaultPos of a short or an empty DimmerList would put the dimmer outside of it
|
||||
modernDimmerDefaultPosition = clampDimmerPosition(modernDimmerDefaultPosition);
|
||||
modernDimmerPosition = clampDimmerPosition(modernDimmerPosition);
|
||||
if (!enableModernDimmer)
|
||||
{
|
||||
modernDimmerPosition = modernDimmerDefaultPosition;
|
||||
|
||||
112
tests/unit/test_dimmer.cpp
Normal file
112
tests/unit/test_dimmer.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
#include "vehicle.h"
|
||||
|
||||
using test::definition;
|
||||
using test::vehicle;
|
||||
|
||||
// the headlight dimmer's position indexes the DimmerList of the .fiz. a short or an empty list, or a DefaultPos
|
||||
// past its end, put the dimmer outside the list, where the lights and the cab read past the end of it
|
||||
|
||||
namespace {
|
||||
|
||||
// rows: high beam, dimmed, off
|
||||
nlohmann::ordered_json const plain{"no", "no", "no"};
|
||||
nlohmann::ordered_json const dimmed{"no", "yes", "no"};
|
||||
nlohmann::ordered_json const off{"no", "no", "yes"};
|
||||
|
||||
definition dimmer_list(nlohmann::ordered_json const &Rows, int const Defaultposition, bool const Cycle = false) {
|
||||
return definition::load("cab_vehicle")
|
||||
.set("DimmerList", "rows", Rows)
|
||||
.set("DimmerList", "Size", static_cast<int>(Rows.size()))
|
||||
.set("DimmerList", "DefaultPos", Defaultposition)
|
||||
.set("DimmerList", "Cycle", Cycle ? "yes" : "no");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("dimmer: a position outside the list reads as a plain one, neither off nor dimmed") {
|
||||
vehicle cab{dimmer_list(nlohmann::ordered_json::array({off, dimmed}), 0)};
|
||||
REQUIRE(cab->dimPositions.size() == 2);
|
||||
|
||||
for (auto const position : {-1, 2, 100}) {
|
||||
CAPTURE(position);
|
||||
cab->modernDimmerPosition = position;
|
||||
auto const entry{cab->currentDimPosition()};
|
||||
CHECK_FALSE(entry.isOff);
|
||||
CHECK_FALSE(entry.isDimmed);
|
||||
CHECK_FALSE(entry.isHighBeam);
|
||||
}
|
||||
cab->modernDimmerPosition = 0;
|
||||
CHECK(cab->currentDimPosition().isOff);
|
||||
cab->modernDimmerPosition = 1;
|
||||
CHECK(cab->currentDimPosition().isDimmed);
|
||||
}
|
||||
|
||||
TEST_CASE("dimmer: positions are clamped to the list") {
|
||||
vehicle cab{dimmer_list(nlohmann::ordered_json::array({plain, dimmed, off}), 0)};
|
||||
|
||||
CHECK(cab->clampDimmerPosition(-3) == 0);
|
||||
CHECK(cab->clampDimmerPosition(1) == 1);
|
||||
CHECK(cab->clampDimmerPosition(3) == 2);
|
||||
CHECK(cab->clampDimmerPosition(100) == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("dimmer: a DefaultPos past the end of the list starts the dimmer at its last position") {
|
||||
vehicle cab{dimmer_list(nlohmann::ordered_json::array({plain, dimmed}), 7)};
|
||||
|
||||
CHECK(cab->modernDimmerDefaultPosition == 1);
|
||||
// the cab puts the modern dimmer to its default position when it's entered (TTrain::set_cab_controls())
|
||||
cab->modernDimmerPosition = cab->modernDimmerDefaultPosition;
|
||||
CHECK(cab->currentDimPosition().isDimmed);
|
||||
}
|
||||
|
||||
TEST_CASE("dimmer: without the modern dimmer a DefaultPos past the end of the list puts the dimmer at its last position") {
|
||||
vehicle cab{dimmer_list(nlohmann::ordered_json::array({plain, dimmed}), 7).set("Switches", "ModernDimmer", "No")};
|
||||
REQUIRE_FALSE(cab->enableModernDimmer);
|
||||
|
||||
CHECK(cab->modernDimmerPosition == 1);
|
||||
CHECK(cab->currentDimPosition().isDimmed);
|
||||
}
|
||||
|
||||
TEST_CASE("dimmer: an empty list leaves the dimmer at 0, reading as a plain position, and it doesn't move") {
|
||||
vehicle cab{dimmer_list(nlohmann::ordered_json::array(), 3, true)};
|
||||
REQUIRE(cab->dimPositions.empty());
|
||||
|
||||
CHECK(cab->modernDimmerDefaultPosition == 0);
|
||||
CHECK(cab->modernDimmerPosition == 0);
|
||||
CHECK(cab->clampDimmerPosition(1) == 0);
|
||||
CHECK_FALSE(cab->currentDimPosition().isOff);
|
||||
CHECK_FALSE(cab->stepDimmerPosition(1));
|
||||
CHECK_FALSE(cab->stepDimmerPosition(-1));
|
||||
CHECK(cab->modernDimmerPosition == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("dimmer: stepping stays within the list") {
|
||||
SUBCASE("without cycling it stops at the ends") {
|
||||
vehicle cab{dimmer_list(nlohmann::ordered_json::array({plain, dimmed, off}), 0)};
|
||||
CHECK_FALSE(cab->stepDimmerPosition(-1));
|
||||
CHECK(cab->modernDimmerPosition == 0);
|
||||
CHECK(cab->stepDimmerPosition(1));
|
||||
CHECK(cab->stepDimmerPosition(1));
|
||||
CHECK(cab->modernDimmerPosition == 2);
|
||||
CHECK_FALSE(cab->stepDimmerPosition(1));
|
||||
CHECK(cab->modernDimmerPosition == 2);
|
||||
}
|
||||
SUBCASE("with cycling it goes around to the other end") {
|
||||
vehicle cab{dimmer_list(nlohmann::ordered_json::array({plain, dimmed, off}), 0, true)};
|
||||
CHECK(cab->stepDimmerPosition(-1));
|
||||
CHECK(cab->modernDimmerPosition == 2);
|
||||
CHECK(cab->stepDimmerPosition(1));
|
||||
CHECK(cab->modernDimmerPosition == 0);
|
||||
}
|
||||
SUBCASE("a position past the end steps from the last one") {
|
||||
vehicle cab{dimmer_list(nlohmann::ordered_json::array({plain, dimmed, off}), 0)};
|
||||
cab->modernDimmerPosition = 9;
|
||||
CHECK(cab->stepDimmerPosition(-1));
|
||||
CHECK(cab->modernDimmerPosition == 1);
|
||||
cab->modernDimmerPosition = 9;
|
||||
CHECK(cab->stepDimmerPosition(1)); // can't go further, but comes back into the list
|
||||
CHECK(cab->modernDimmerPosition == 2);
|
||||
}
|
||||
}
|
||||
@@ -7431,9 +7431,7 @@ void TDynamicObject::RaLightsSet(int head, int rear)
|
||||
bool tRight = MoverParameters->iLights[vehicleend] & (light::auxiliary_right | light::headlight_right); // a tu z prawej
|
||||
if (Controller == Humandriver) {
|
||||
|
||||
int ¤tDimPos = MoverParameters->modernDimmerPosition;
|
||||
auto &dimmerPositions = MoverParameters->dimPositions;
|
||||
TMoverParameters::dimPosition dps = dimmerPositions[currentDimPos];
|
||||
auto const dps{MoverParameters->currentDimPosition()};
|
||||
|
||||
// domyślnie
|
||||
HighBeamLights = false;
|
||||
@@ -7495,9 +7493,7 @@ void TDynamicObject::SetLightDimmings()
|
||||
if (Controller == Humandriver)
|
||||
{
|
||||
|
||||
int ¤tDimPos = MoverParameters->modernDimmerPosition;
|
||||
auto &dimmerPositions = MoverParameters->dimPositions;
|
||||
TMoverParameters::dimPosition dps = dimmerPositions[currentDimPos];
|
||||
auto const dps{MoverParameters->currentDimPosition()};
|
||||
|
||||
// domyślnie
|
||||
HighBeamLights = false;
|
||||
|
||||
@@ -723,7 +723,7 @@ std::shared_ptr<dictionary_source> TTrain::GetTrainState(dictionary_source const
|
||||
dict->insert("pant_compressor", mvPantographUnit->PantCompFlag);
|
||||
dict->insert("lights_front", mvOccupied->iLights[end::front]);
|
||||
dict->insert("lights_rear", mvOccupied->iLights[end::rear]);
|
||||
dict->insert("off_from_dimmer", mvOccupied->dimPositions[mvOccupied->modernDimmerPosition].isOff);
|
||||
dict->insert("off_from_dimmer", mvOccupied->currentDimPosition().isOff);
|
||||
dict->insert("lights_compartments", mvOccupied->CompartmentLights.is_active || mvOccupied->CompartmentLights.is_disabled);
|
||||
if (Dynamic()->Mechanik)
|
||||
{
|
||||
@@ -6068,14 +6068,9 @@ void TTrain::OnCommand_modernlightdimmerincrease(TTrain *Train, command_data con
|
||||
if (Command.action == GLFW_PRESS)
|
||||
{
|
||||
// update modern dimmer state
|
||||
|
||||
auto &dimPos = Train->mvOccupied->modernDimmerPosition;
|
||||
if (auto dimCount = Train->mvOccupied->dimPositions.size(); dimPos + 1 < dimCount)
|
||||
dimPos++;
|
||||
else if (Train->mvOccupied->modernDimmerCanCycle)
|
||||
dimPos = 0; // return to 0
|
||||
else
|
||||
return; // już na minimum i nie można cyklicznie
|
||||
if (false == Train->mvOccupied->stepDimmerPosition(1))
|
||||
return; // już na maksimum i nie można cyklicznie
|
||||
auto const dimPos = Train->mvOccupied->modernDimmerPosition;
|
||||
// update lightning
|
||||
// Train->Dynamic()->SetLights();
|
||||
Train->Dynamic()->SetLightDimmings();
|
||||
@@ -6092,18 +6087,9 @@ void TTrain::OnCommand_modernlightdimmerdecrease(TTrain *Train, command_data con
|
||||
|
||||
if (Command.action == GLFW_PRESS)
|
||||
{
|
||||
auto &dimPos = Train->mvOccupied->modernDimmerPosition;
|
||||
auto dimCount = Train->mvOccupied->dimPositions.size();
|
||||
|
||||
if (dimCount == 0)
|
||||
return;
|
||||
|
||||
if (dimPos > 0)
|
||||
dimPos--;
|
||||
else if (Train->mvOccupied->modernDimmerCanCycle)
|
||||
dimPos = static_cast<int>(dimCount - 1); // ostatnia pozycja
|
||||
else
|
||||
if (false == Train->mvOccupied->stepDimmerPosition(-1))
|
||||
return; // już na minimum i nie można cyklicznie
|
||||
auto const dimPos = Train->mvOccupied->modernDimmerPosition;
|
||||
|
||||
// Train->Dynamic()->SetLights();
|
||||
Train->Dynamic()->SetLightDimmings();
|
||||
@@ -6344,7 +6330,7 @@ void TTrain::OnCommand_headlightsdimenable(TTrain *Train, command_data const &Co
|
||||
|
||||
Train->DynamicObject->DimHeadlights = true;
|
||||
*/
|
||||
Train->DynamicObject->MoverParameters->modernDimmerPosition = 1; // ustawiamy modern dimmer na flage przyciemnienia
|
||||
Train->DynamicObject->MoverParameters->modernDimmerPosition = Train->DynamicObject->MoverParameters->clampDimmerPosition(1); // ustawiamy modern dimmer na flage przyciemnienia
|
||||
Train->DynamicObject->SetLightDimmings();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user