mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-19 20:24:59 +01:00
fix(lights): keep the headlight dimmer position within the DimmerList
modernDimmerPosition indexed dimPositions without a range check in RaLightsSet(), SetLightDimmings() and the off_from_dimmer value of the cab's Python state. An empty DimmerList, or a DefaultPos past the end of a short one, read past the end of the vector; the dimmer commands could also leave or put the position outside the list. Reads go through currentDimPosition(), a plain entry (not off, not dimmed) outside the list. DefaultPos and the position are clamped after the .fiz is loaded, the increase/decrease commands step through stepDimmerPosition(), which clamps and does nothing for an empty list, and headlightsdimenable clamps its position 1.
This commit is contained in:
@@ -2286,6 +2286,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
|
||||
|
||||
@@ -9448,6 +9448,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)
|
||||
{
|
||||
@@ -10249,6 +10295,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;
|
||||
|
||||
Reference in New Issue
Block a user