mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-16 00:34:11 +01:00
Merge remote-tracking branch 'origin/master' into feat/elf-master-controller-emergency-brake
This commit is contained in:
130
tests/unit/test_gauge_scale.cpp
Normal file
130
tests/unit/test_gauge_scale.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
#include "consist.h"
|
||||
|
||||
#include "utilities/parser.h"
|
||||
#include "vehicle/gaugescale.h"
|
||||
#include "vehicle/mainctrlact.h"
|
||||
|
||||
// the controller position marker of the diesel locomotives (MaSzyna-EU07/maszyna#74): the mainctrlact: gauge of
|
||||
// the ST43 follows its controller, on a scale whose marks are not evenly spaced (scaletable:)
|
||||
|
||||
using test::consist;
|
||||
using test::definition;
|
||||
|
||||
namespace {
|
||||
|
||||
gauge_scale::table read(std::string const &Text) {
|
||||
cParser parser(Text, cParser::buffer_TEXT);
|
||||
return gauge_scale::read(parser);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("gauge scale: a table is read with or without commas, the brackets touching the numbers or not") {
|
||||
for (auto const *text : {"[ 0 0, 1 0.5, 2 0.6 ]", "[0 0 1 0.5 2 0.6]", "[ 0 0,1 0.5,2 0.6]"}) {
|
||||
CAPTURE(text);
|
||||
auto const table{read(text)};
|
||||
CHECK(table.closed);
|
||||
CHECK_FALSE(table.block_end);
|
||||
CHECK_FALSE(table.malformed);
|
||||
REQUIRE(table.points.size() == 3);
|
||||
CHECK(table.points[1].value == doctest::Approx(1.0));
|
||||
CHECK(table.points[1].scaled == doctest::Approx(0.5));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("gauge scale: the table leaves the rest of the gauge's block to be read") {
|
||||
cParser parser("[ 0 0, 1 0.5 ] soundinc: x.wav }", cParser::buffer_TEXT);
|
||||
CHECK(gauge_scale::read(parser).closed);
|
||||
CHECK(parser.getToken<std::string>(true, "\n\r\t ,;") == "soundinc:");
|
||||
}
|
||||
|
||||
TEST_CASE("gauge scale: a table without its closing bracket stops at the end of the block") {
|
||||
auto const table{read("[ 0 0, 1 0.5 }")};
|
||||
CHECK_FALSE(table.closed);
|
||||
CHECK(table.block_end);
|
||||
CHECK(table.points.size() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("gauge scale: a value without what the gauge shows for it, or a word, makes a malformed table") {
|
||||
CHECK(read("[ 0 0, 1 ]").malformed);
|
||||
CHECK(read("[ 0 0, 1 x ]").malformed);
|
||||
CHECK(read("[ 0 0, 1 x ]").points.size() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("gauge scale: the points are put in the order of their values") {
|
||||
auto const table{read("[ 2 0.6, 0 0, 1 0.5 ]")};
|
||||
REQUIRE(table.points.size() == 3);
|
||||
CHECK(table.points[0].value == doctest::Approx(0.0));
|
||||
CHECK(table.points[2].value == doctest::Approx(2.0));
|
||||
}
|
||||
|
||||
TEST_CASE("gauge scale: the gauge shows what the table gives, interpolated between its points") {
|
||||
auto const table{read("[ 0 0, 1 -0.05, 2 -0.07, 4 -0.08 ]")};
|
||||
CHECK(gauge_scale::interpolate(table.points, 0.f) == doctest::Approx(0.0));
|
||||
CHECK(gauge_scale::interpolate(table.points, 1.f) == doctest::Approx(-0.05));
|
||||
CHECK(gauge_scale::interpolate(table.points, 0.5f) == doctest::Approx(-0.025)); // a marker on its way
|
||||
CHECK(gauge_scale::interpolate(table.points, 3.f) == doctest::Approx(-0.075));
|
||||
CHECK(gauge_scale::interpolate(table.points, 4.f) == doctest::Approx(-0.08));
|
||||
// past the ends the marker stays at them
|
||||
CHECK(gauge_scale::interpolate(table.points, -1.f) == doctest::Approx(0.0));
|
||||
CHECK(gauge_scale::interpolate(table.points, 9.f) == doctest::Approx(-0.08));
|
||||
}
|
||||
|
||||
TEST_CASE("gauge scale: two points of the same value make a step") {
|
||||
auto const table{read("[ 0 0, 1 0.1, 1 0.5, 2 0.6 ]")};
|
||||
CHECK(gauge_scale::interpolate(table.points, 0.99f) == doctest::Approx(0.099));
|
||||
CHECK(gauge_scale::interpolate(table.points, 1.f) == doctest::Approx(0.5));
|
||||
CHECK(gauge_scale::interpolate(table.points, 1.5f) == doctest::Approx(0.55));
|
||||
}
|
||||
|
||||
TEST_CASE("main ctrl act: the marker of a diesel locomotive with a scaletable: follows its controller (MaSzyna-EU07/maszyna#74)") {
|
||||
// nothing sets the actual position of a diesel engine's controller, the marker stayed at 0
|
||||
for (auto const *path : {"st44_v2/m62-1151", "sm42_v1/6d1", "su45_v2/301d"}) {
|
||||
CAPTURE(path);
|
||||
consist train;
|
||||
auto &locomotive{train.add(definition::fiz(path), 1).mover()};
|
||||
REQUIRE((locomotive.EngineType == TEngineType::DieselElectric || locomotive.EngineType == TEngineType::DieselEngine));
|
||||
REQUIRE(locomotive.EIMCtrlType == 0);
|
||||
locomotive.MainCtrlPos = 5;
|
||||
CHECK(locomotive.MainCtrlActualPos == 0);
|
||||
CHECK(main_ctrl_act::position(locomotive, true) == doctest::Approx(5.0));
|
||||
// one with a controller of the EIM kind keeps its actual position itself
|
||||
locomotive.EIMCtrlType = 2;
|
||||
CHECK(main_ctrl_act::position(locomotive, true) == doctest::Approx(0.0));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("main ctrl act: the marker of a diesel locomotive without a scaletable: stays still, as before") {
|
||||
// the shipped ST43 cabs define mainctrlact: with a linear scale nobody has seen working; it is not opted in
|
||||
for (auto const *path : {"st44_v2/m62-1151", "sm42_v1/6d1", "su45_v2/301d"}) {
|
||||
CAPTURE(path);
|
||||
consist train;
|
||||
auto &locomotive{train.add(definition::fiz(path), 1).mover()};
|
||||
REQUIRE(locomotive.EIMCtrlType == 0);
|
||||
locomotive.MainCtrlPos = 5;
|
||||
locomotive.ScndCtrlPos = 1;
|
||||
for (auto const coupled : {false, true}) {
|
||||
CAPTURE(coupled);
|
||||
locomotive.CoupledCtrl = coupled;
|
||||
CHECK(main_ctrl_act::position(locomotive, false) == doctest::Approx(0.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("main ctrl act: the marker of an electric locomotive shows its actual position, as before") {
|
||||
for (auto const scaletable : {false, true}) {
|
||||
CAPTURE(scaletable);
|
||||
consist train;
|
||||
auto &locomotive{train.add(definition::fiz("et22_v3/201e-w"), 1).mover()};
|
||||
locomotive.MainCtrlPos = 10;
|
||||
locomotive.MainCtrlActualPos = 3;
|
||||
locomotive.ScndCtrlPos = 2;
|
||||
locomotive.ScndCtrlActualPos = 1;
|
||||
locomotive.CoupledCtrl = false;
|
||||
CHECK(main_ctrl_act::position(locomotive, scaletable) == doctest::Approx(3.0));
|
||||
locomotive.CoupledCtrl = true;
|
||||
CHECK(main_ctrl_act::position(locomotive, scaletable) == doctest::Approx(4.0));
|
||||
}
|
||||
}
|
||||
@@ -121,6 +121,8 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu
|
||||
Parser.getTokens();
|
||||
if( Parser.peek() != "{" ) {
|
||||
// old fixed size config
|
||||
// it has no scaletable:, drop the one of an earlier definition of the same control
|
||||
m_scaletable.clear();
|
||||
Parser >> submodelname;
|
||||
gaugetypename = Parser.getToken<std::string>( true );
|
||||
Parser.getTokens( 3, false );
|
||||
@@ -160,6 +162,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 ) {
|
||||
@@ -300,6 +308,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" ) };
|
||||
@@ -313,6 +325,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 ) {
|
||||
@@ -441,6 +467,7 @@ TGauge TGauge::MirrorCopy() const {
|
||||
mirror.m_endscale = m_endscale;
|
||||
mirror.m_endvalue = m_endvalue;
|
||||
mirror.m_interpolatescale = m_interpolatescale;
|
||||
mirror.m_scaletable = m_scaletable;
|
||||
mirror.m_state = m_state;
|
||||
mirror.m_sourcemodel = m_sourcemodel;
|
||||
return mirror;
|
||||
@@ -503,6 +530,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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -72,6 +73,9 @@ public:
|
||||
return ( static_cast<int>( type() ) & static_cast<int>( TGaugeType::toggle ) ) != 0; }
|
||||
bool is_delayed() const {
|
||||
return ( static_cast<int>( type() ) & static_cast<int>( TGaugeType::delayed ) ) != 0; }
|
||||
// true if the gauge was given a non-uniform scale (scaletable:)
|
||||
bool has_scaletable() const {
|
||||
return false == m_scaletable.empty(); }
|
||||
// members
|
||||
TSubModel *SubModel { nullptr }; // McZapkie-310302: zeby mozna bylo sprawdzac czy zainicjowany poprawnie
|
||||
TSubModel *SubModelOn { nullptr }; // optional submodel visible when the state input is set
|
||||
@@ -81,11 +85,14 @@ private:
|
||||
struct scratch_data {
|
||||
std::optional< std::array<float, 6> > soundproofing;
|
||||
bool duplicate { false }; // the definition is a duplicate submodel of the control defined before
|
||||
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
|
||||
@@ -109,6 +116,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 { '\0' }; // typ zmiennej parametru: f-float, d-double, i-int, b-bool; none until one is assigned
|
||||
union {
|
||||
// wskaźnik na parametr pokazywany przez animację
|
||||
|
||||
@@ -17,6 +17,7 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include "vehicle/horn.h"
|
||||
#include "vehicle/linebreaker.h"
|
||||
#include "vehicle/holdcoillever.h"
|
||||
#include "vehicle/mainctrlact.h"
|
||||
|
||||
#include "utilities/Globals.h"
|
||||
#include "simulation/simulation.h"
|
||||
@@ -9697,10 +9698,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.has_scaletable()));
|
||||
ggMainCtrlAct.Update();
|
||||
}
|
||||
if (ggScndCtrl.SubModel != nullptr)
|
||||
|
||||
118
vehicle/gaugescale.h
Normal file
118
vehicle/gaugescale.h
Normal 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
|
||||
34
vehicle/mainctrlact.h
Normal file
34
vehicle/mainctrlact.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
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;
|
||||
}
|
||||
|
||||
// Scaletable: the cab's gauge has a scaletable:. only such a marker follows the controller; the cabs that define
|
||||
// mainctrlact: without one (the ST43 so far) keep their marker still, as before, instead of moving it on a scale
|
||||
// nobody has checked
|
||||
inline float position(TMoverParameters const &Vehicle, bool const Scaletable) {
|
||||
auto const controller{Scaletable && 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
|
||||
Reference in New Issue
Block a user