18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 21:34:58 +01:00

feat: controller position marker of the diesel locomotives (#74)

The mainctrlact: gauge showed MainCtrlActualPos, which nothing sets for
DieselEngine/DieselElectric without an EIM controller, so the ST43's marker
never moved. For those it now follows MainCtrlPos (vehicle/mainctrlact.h);
every other vehicle reads what it did before.

New optional gauge key scaletable: [ value scaled, ... ] in the block form
gives a non-uniform scale, interpolated linearly between its points, in the
units of value * scale + offset; without it gauges behave as before.

Assets: none needed. To get the true ST43 scale, the line
"mainctrlact: stery_1 rot -0.016 0 0.0" of dynamic/pkp/st43_v1/*.mmd is to
become a block with a scaletable: of the 25 positions read off the cab's
scale; until then the marker moves linearly with -0.016 turn per position.
This commit is contained in:
jerrrrycho
2026-11-07 19:01:13 +01:00
parent 939e42b611
commit cf2092700e
6 changed files with 294 additions and 4 deletions

View File

@@ -152,6 +152,12 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu
while( true == Load_mapping( Parser, scratchpad ) ) {
// all work done by while()
}
// the values of the table are in the units the config uses for the scale, which the multiplier converts the gauge's value to
for( auto &point : scratchpad.scaletable ) {
point.value = static_cast<float>( point.value / mul );
}
gauge_scale::sort( scratchpad.scaletable );
m_scaletable = std::move( scratchpad.scaletable );
// post-deserialization cleanup
// set provided custom soundproofing to assigned sounds (for sounds without their own custom soundproofing)
if( scratchpad.soundproofing ) {
@@ -275,6 +281,10 @@ TGauge::Load_mapping( cParser &Input, TGauge::scratch_data &Scratchpad ) {
>> soundproofing[ 5 ];
Scratchpad.soundproofing = soundproofing;
}
else if( key == "scaletable:" ) {
// non-uniform scale in format [ value1 scaled1, value2 scaled2, ... ]
return Load_scaletable( Input, Scratchpad );
}
else if( key.starts_with("sound") ) {
// sounds assigned to specific gauge values, defined by key soundX: where X = value
auto const indexstart { key.find_first_of( "-1234567890" ) };
@@ -288,6 +298,20 @@ TGauge::Load_mapping( cParser &Input, TGauge::scratch_data &Scratchpad ) {
return true; // return value marks a key: value pair was extracted, nothing about whether it's recognized
}
// imports the optional non-uniform scale (MaSzyna-EU07/maszyna#74); returns false if it ran into the end of the block
bool
TGauge::Load_scaletable( cParser &Input, TGauge::scratch_data &Scratchpad ) {
auto table { gauge_scale::read( Input ) };
if( table.malformed || false == table.closed || table.points.size() < 2 ) {
ErrorLog( "Bad model: malformed scaletable of a gauge, it needs [ value scaled, value scaled, ... ] with at least two pairs", logtype::model );
}
if( table.points.size() >= 2 ) {
Scratchpad.scaletable = std::move( table.points );
}
return false == table.block_end;
}
// ustawienie wartości docelowej. plays provided fallback sound, if no sound was defined in the control itself
bool
TGauge::UpdateValue( float fNewDesired, std::optional<sound_source> &Fallbacksound ) {
@@ -446,6 +470,9 @@ void TGauge::AssignState( bool const *State ) {
float TGauge::GetScaledValue() const {
if( false == m_scaletable.empty() ) {
return gauge_scale::interpolate( m_scaletable, m_value );
}
return false == m_interpolatescale ? m_value * m_scale + m_offset : m_value * std::lerp(m_scale, m_endscale, std::clamp(m_value / m_endvalue, 0.f, 1.f)) + m_offset;
}

View File

@@ -11,6 +11,7 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/Classes.h"
#include "audio/sound.h"
#include "vehicle/gaugescale.h"
enum class TGaugeAnimation {
// typ ruchu
@@ -74,11 +75,14 @@ private:
// types
struct scratch_data {
std::optional< std::array<float, 6> > soundproofing;
std::vector<gauge_scale::point> scaletable; // as read, in the units of the config
};
// methods
// imports member data pair from the config file
bool
Load_mapping( cParser &Input, TGauge::scratch_data &Scratchpad );
static bool
Load_scaletable( cParser &Input, TGauge::scratch_data &Scratchpad );
float
GetScaledValue() const;
void
@@ -95,6 +99,8 @@ private:
float m_endscale { -1.f }; // scale applied to the value at the end of accepted value range
float m_endvalue { -1.f }; // end value of accepted value range
bool m_interpolatescale { false };
std::vector<gauge_scale::point> m_scaletable; // optional non-uniform scale, sorted by value; replaces scale and offset
char m_datatype; // typ zmiennej parametru: f-float, d-double, i-int
union {
// wskaźnik na parametr pokazywany przez animację

View File

@@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/.
#include "stdafx.h"
#include "vehicle/Train.h"
#include "vehicle/horn.h"
#include "vehicle/mainctrlact.h"
#include "utilities/Globals.h"
#include "simulation/simulation.h"
@@ -9336,10 +9337,7 @@ bool TTrain::Update(double const Deltatime)
}
if (ggMainCtrlAct.SubModel != nullptr)
{
if (mvControlled->CoupledCtrl)
ggMainCtrlAct.UpdateValue(static_cast<float>(double(mvControlled->MainCtrlActualPos + mvControlled->ScndCtrlActualPos)));
else
ggMainCtrlAct.UpdateValue(static_cast<float>(double(mvControlled->MainCtrlActualPos)));
ggMainCtrlAct.UpdateValue(main_ctrl_act::position(*mvControlled));
ggMainCtrlAct.Update();
}
if (ggScndCtrl.SubModel != nullptr)

118
vehicle/gaugescale.h Normal file
View File

@@ -0,0 +1,118 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "utilities/parser.h"
#include <algorithm>
#include <charconv>
#include <cmath>
#include <iterator>
#include <optional>
#include <string>
#include <string_view>
#include <system_error>
#include <vector>
// a non-uniform scale of a cab gauge (MaSzyna-EU07/maszyna#74), such as the marker of the controller position of
// the ST43, whose marks are not evenly spaced: the gauge shows what a table gives for its value, interpolated
// linearly between the points of the table, instead of value * scale + offset
namespace gauge_scale {
struct point {
float value; // the value of the gauge
float scaled; // what the gauge shows for it, in the units of value * scale + offset (turns for rot, metres for mov)
};
// puts the points in the order of their values; points of the same value keep their order, so two of them make a step
inline void sort(std::vector<point> &Points) {
std::ranges::stable_sort(Points, {}, &point::value);
}
// what the gauge shows for the Value, from the Points sorted by value, which can't be empty;
// below the first point and above the last one the gauge stays at their ends of the scale
inline float interpolate(std::vector<point> const &Points, float const Value) {
auto const upper{std::ranges::upper_bound(Points, Value, {}, &point::value)};
if (upper == Points.begin()) {
return Points.front().scaled;
}
if (upper == Points.end()) {
return Points.back().scaled;
}
auto const &from{*std::prev(upper)};
auto const &to{*upper};
// from.value <= Value < to.value
return std::lerp(from.scaled, to.scaled, (Value - from.value) / (to.value - from.value));
}
// a table as read from the config
struct table {
std::vector<point> points; // sorted by value
bool closed{false}; // it ended with its "]"
bool block_end{false}; // the "}" of the gauge's block came in place of the "]", and was read
bool malformed{false}; // something that isn't a number, or a value without what the gauge shows for it
};
// the number Text holds, nothing when it holds anything else
inline std::optional<float> number(std::string_view const Text) {
float value{};
auto const *last{Text.data() + Text.size()};
if (auto const [end, error]{std::from_chars(Text.data(), last, value)}; error != std::errc{} || end != last) {
return std::nullopt;
}
return value;
}
// what came with a token of a table
enum class token_end { more, closed, block_end, file_end };
// reads one token of a table into Numbers
inline token_end read_token(cParser &Input, std::vector<float> &Numbers, bool &Malformed) {
auto token{Input.getToken<std::string>(false, "\n\r\t ,;")};
if (token.empty()) {
return token_end::file_end;
}
if (token == "}") {
return token_end::block_end;
}
auto const closes{token.ends_with(']')};
std::erase(token, '[');
std::erase(token, ']');
if (auto const value{number(token)}; value) {
Numbers.push_back(*value);
}
else if (false == token.empty()) {
Malformed = true;
}
return closes ? token_end::closed : token_end::more;
}
// reads a table written as [ value scaled, value scaled, ... ], pairs of a value of the gauge and what it shows for
// it; the brackets may touch the numbers, the commas are optional
inline table read(cParser &Input) {
table result;
std::vector<float> numbers;
auto end{token_end::more};
while (end == token_end::more) {
end = read_token(Input, numbers, result.malformed);
}
result.closed = end == token_end::closed;
result.block_end = end == token_end::block_end;
if (numbers.size() % 2 != 0) {
result.malformed = true;
}
for (std::size_t index{0}; index + 1 < numbers.size(); index += 2) {
result.points.emplace_back(numbers[index], numbers[index + 1]);
}
sort(result.points);
return result;
}
} // namespace gauge_scale

31
vehicle/mainctrlact.h Normal file
View File

@@ -0,0 +1,31 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "McZapkie/MOVER.h"
// the position the controller position marker of the cab (mainctrlact:) shows
namespace main_ctrl_act {
// the diesel engines work the controller position straight away, they have no camshaft or relays of their own that
// would follow it, and nothing sets their MainCtrlActualPos; only with a controller of the EIM kind (EIMCtrlType)
// it is kept up to date (CheckEIMIC). so their marker shows the position of the controller (MaSzyna-EU07/maszyna#74)
inline bool follows_controller(TMoverParameters const &Vehicle) {
return (Vehicle.EngineType == TEngineType::DieselEngine || Vehicle.EngineType == TEngineType::DieselElectric) && Vehicle.EIMCtrlType == 0;
}
inline float position(TMoverParameters const &Vehicle) {
auto const controller{follows_controller(Vehicle)};
auto const main{controller ? Vehicle.MainCtrlPos : Vehicle.MainCtrlActualPos};
auto const scnd{controller ? Vehicle.ScndCtrlPos : Vehicle.ScndCtrlActualPos};
return static_cast<float>(Vehicle.CoupledCtrl ? main + scnd : main);
}
} // namespace main_ctrl_act