From d259251373f23d0dd4d9f5f2557a257c568dd844 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 23:19:53 +0100 Subject: [PATCH 1/2] feat: cab controls can animate duplicate submodels (eu07/maszyna#527) A control defined again in the same cab section of the .mmd with the block key "duplicate: yes" no longer replaces the control: the first definition stays the control (sounds, type, command feedback), and the duplicate adds a copy with its own submodel, animation type and scale / offset, following the control's value and active state. The duplicate's sound keys are ignored so the control is not heard twice, and clicking any copy works the control. This lets e.g. the ASF cab move its second master controller and reverser with the first ones. Without the key a repeated definition still replaces the earlier one, as some of the game's .mmd files repeat controls with other submodels. A duplicate with no definition of the control loaded from the current cab's models before it is just the control. clear_cab_controls() now also clears ggCompressorListButton, which kept the previous cab's submodel. The rule is in vehicle/gaugeduplicates.h, covered by tests/unit/test_gauge_duplicates.cpp. (eu07/maszyna#527) --- tests/unit/test_gauge_duplicates.cpp | 53 ++++++++++++++++++++++ vehicle/Gauge.cpp | 68 +++++++++++++++++++++++++--- vehicle/Gauge.h | 12 +++++ vehicle/Train.cpp | 6 +++ vehicle/gaugeduplicates.h | 30 ++++++++++++ 5 files changed, 163 insertions(+), 6 deletions(-) create mode 100644 tests/unit/test_gauge_duplicates.cpp create mode 100644 vehicle/gaugeduplicates.h diff --git a/tests/unit/test_gauge_duplicates.cpp b/tests/unit/test_gauge_duplicates.cpp new file mode 100644 index 00000000..7bdbe909 --- /dev/null +++ b/tests/unit/test_gauge_duplicates.cpp @@ -0,0 +1,53 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "vehicle/gaugeduplicates.h" + +// cab controls with more than one submodel (eu07/maszyna#527): a definition of a control in the .mmd marked +// "duplicate: yes" adds a copy of the control already defined in the cab, instead of replacing it. TGauge itself +// (3d models, sounds) is not part of the core library, the rule which decides it is + +namespace { + +// stand-ins for the 3d models of the cab +struct model {}; + +} // namespace + +TEST_CASE("gauge duplicates: the values of the duplicate key which mark a duplicate") { + CHECK(gauge_duplicates::is_duplicate("yes")); + CHECK(gauge_duplicates::is_duplicate("true")); + CHECK(gauge_duplicates::is_duplicate("1")); + CHECK_FALSE(gauge_duplicates::is_duplicate("no")); + CHECK_FALSE(gauge_duplicates::is_duplicate("false")); + CHECK_FALSE(gauge_duplicates::is_duplicate("0")); + CHECK_FALSE(gauge_duplicates::is_duplicate("")); +} + +TEST_CASE("gauge duplicates: a duplicate adds a copy to the control loaded from either model of the cab") { + model cab; + model lowpoly; + CHECK(gauge_duplicates::adds_copy(true, &cab, &cab, &lowpoly)); + CHECK(gauge_duplicates::adds_copy(true, &lowpoly, &cab, &lowpoly)); + // a cab without a low poly interior + CHECK(gauge_duplicates::adds_copy(true, &cab, &cab, nullptr)); +} + +TEST_CASE("gauge duplicates: a definition not marked as a duplicate replaces the control, as before") { + model cab; + model lowpoly; + CHECK_FALSE(gauge_duplicates::adds_copy(false, &cab, &cab, &lowpoly)); + CHECK_FALSE(gauge_duplicates::adds_copy(false, nullptr, &cab, &lowpoly)); +} + +TEST_CASE("gauge duplicates: with nothing loaded in this cab a duplicate is the control itself") { + model cab; + model lowpoly; + model previouscab; + // the control isn't defined before it + CHECK_FALSE(gauge_duplicates::adds_copy(true, nullptr, &cab, &lowpoly)); + CHECK_FALSE(gauge_duplicates::adds_copy(true, nullptr, &cab, nullptr)); + // the control is left over from the cab loaded before + CHECK_FALSE(gauge_duplicates::adds_copy(true, &previouscab, &cab, &lowpoly)); + CHECK_FALSE(gauge_duplicates::adds_copy(true, &previouscab, &cab, nullptr)); +} diff --git a/vehicle/Gauge.cpp b/vehicle/Gauge.cpp index af8e9a06..2190370e 100644 --- a/vehicle/Gauge.cpp +++ b/vehicle/Gauge.cpp @@ -16,6 +16,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "vehicle/Gauge.h" #include "vehicle/gaugepositions.h" +#include "vehicle/gaugeduplicates.h" #include "utilities/parser.h" #include "model/Model3d.h" #include "vehicle/DynObj.h" @@ -109,6 +110,14 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu endvalue = -1; bool interpolatescale { false }; + // the control as defined so far in this cab, which a definition marked as its duplicate adds a copy to + // rather than replacing it (eu07/maszyna#527) + std::optional original; + if( gauge_duplicates::adds_copy( true, m_sourcemodel, Owner->mdKabina, Owner->mdLowPolyInt ) ) { + original = *this; + } + scratch_data scratchpad; + Parser.getTokens(); if( Parser.peek() != "{" ) { // old fixed size config @@ -148,7 +157,6 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu } // new, variable length section { - scratch_data scratchpad; while( true == Load_mapping( Parser, scratchpad ) ) { // all work done by while() } @@ -190,17 +198,20 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu endscale *= static_cast(mul); } TSubModel *submodel { nullptr }; + m_sourcemodel = nullptr; std::array sources { Owner->mdKabina, Owner->mdLowPolyInt }; for( auto const *source : sources ) { if( source != nullptr && ( submodel = source->GetFromName( submodelname ) ) != nullptr ) { // got what we wanted, bail out + m_sourcemodel = source; break; } // there's a possibility the default submodel was named with _off suffix if( source != nullptr && ( submodel = source->GetFromName( submodelname + "_off" ) ) != nullptr ) { // got what we wanted, bail out + m_sourcemodel = source; break; } } @@ -230,6 +241,14 @@ void TGauge::Load( cParser &Parser, TDynamicObject const *Owner, double const mu Init( submodel, submodelon, type, scale, offset, friction, 0, endvalue, endscale, interpolatescale ); + if( scratchpad.duplicate && original ) { + // a duplicate submodel of the control already defined: the control stays as it was, with one more copy + // animated along with it. the duplicate's own sounds, if any, are left out so the control isn't heard twice + auto duplicate { MirrorCopy() }; + *this = std::move( *original ); + m_mirrors.emplace_back( std::move( duplicate ) ); + } + // return md2 != nullptr; // true, gdy podany model zewnętrzny, a w kabinie nie było }; @@ -240,7 +259,12 @@ TGauge::Load_mapping( cParser &Input, TGauge::scratch_data &Scratchpad ) { auto const key { Input.getToken( true, "\n\r\t ,;" ) }; if( true == key.empty() || key == "}" ) { return false; } // if not block end then the key is followed by assigned value or sub-block - if( key == "type:" ) { + if( key == "duplicate:" ) { + // the definition is another submodel of the control defined before it, rather than its replacement + auto const duplicate { Input.getToken( true, "\n\r\t ,;" ) }; + Scratchpad.duplicate = gauge_duplicates::is_duplicate( duplicate ); + } + else if( key == "type:" ) { using enum TGaugeType; auto const gaugetype { Input.getToken( true, "\n\r\t ,;" ) }; m_type = gaugetype == "push" ? push : @@ -376,6 +400,22 @@ void TGauge::Update( bool const Power ) { else { m_soundfxoff.play(); } } m_state = state; + UpdateVisibility(); + // update submodel animations + UpdateAnimation( SubModel ); + UpdateAnimation( SubModelOn ); + // the copies follow the control's value and state, each with its own animation + for( auto &mirror : m_mirrors ) { + mirror.m_value = m_value; + mirror.m_targetvalue = m_targetvalue; + mirror.m_state = m_state; + mirror.UpdateVisibility(); + mirror.UpdateAnimation( mirror.SubModel ); + mirror.UpdateAnimation( mirror.SubModelOn ); + } +}; + +void TGauge::UpdateVisibility() const { // toggle submodel visibility only if the active state submodel is present, // keep the default model always visible otherwise if( SubModelOn != nullptr ) { @@ -384,10 +424,26 @@ void TGauge::Update( bool const Power ) { SubModel->iVisible = !m_state; } } - // update submodel animations - UpdateAnimation( SubModel ); - UpdateAnimation( SubModelOn ); -}; +} + +TGauge TGauge::MirrorCopy() const { + + TGauge mirror; + mirror.SubModel = SubModel; + mirror.SubModelOn = SubModelOn; + mirror.m_animation = m_animation; + mirror.m_friction = m_friction; + mirror.m_targetvalue = m_targetvalue; + mirror.m_value = m_value; + mirror.m_offset = m_offset; + mirror.m_scale = m_scale; + mirror.m_endscale = m_endscale; + mirror.m_endvalue = m_endvalue; + mirror.m_interpolatescale = m_interpolatescale; + mirror.m_state = m_state; + mirror.m_sourcemodel = m_sourcemodel; + return mirror; +} void TGauge::AssignFloat(float *fValue) { diff --git a/vehicle/Gauge.h b/vehicle/Gauge.h index fdef9c58..c78cdb06 100644 --- a/vehicle/Gauge.h +++ b/vehicle/Gauge.h @@ -55,6 +55,9 @@ public: void AssignBool(bool *bValue); void UpdateValue(); void AssignState( bool const *State ); + // copies of the control animated along with it, from its definitions marked duplicate: yes + std::vector const &mirrors() const { + return m_mirrors; } // returns offset of submodel associated with the button from the model centre glm::vec3 model_offset() const; TGaugeType type() const; @@ -74,6 +77,7 @@ private: // types struct scratch_data { std::optional< std::array > soundproofing; + bool duplicate { false }; // the definition is a duplicate submodel of the control defined before }; // methods // imports member data pair from the config file @@ -83,6 +87,12 @@ private: GetScaledValue() const; void UpdateAnimation( TSubModel *Submodel ) const; + // shows the default or the active state submodel as the state says + void + UpdateVisibility() const; + // the copy of this control's submodels and animation, without its sounds and bindings + TGauge + MirrorCopy() const; // members TGaugeAnimation m_animation { TGaugeAnimation::gt_Unknown }; // typ ruchu @@ -112,6 +122,8 @@ private: sound_source m_soundfxincrease { m_soundtemplate }; // sound associated with increasing control's value sound_source m_soundfxdecrease { m_soundtemplate }; // sound associated with decreasing control's value std::map m_soundfxvalues; // sounds associated with specific values + TModel3d const *m_sourcemodel { nullptr }; // the 3d model the submodel was found in by Load() + std::vector m_mirrors; // duplicate submodels of the control (duplicate: yes), animated along with it }; diff --git a/vehicle/Train.cpp b/vehicle/Train.cpp index e30635f4..a9bdfd86 100644 --- a/vehicle/Train.cpp +++ b/vehicle/Train.cpp @@ -59,6 +59,11 @@ void control_mapper::insert(TGauge const &Gauge, std::string const &Label) { m_controlnames.try_emplace(Gauge.SubModelOn, Label); } + // the duplicate submodels of a control (duplicate: yes) work the control as well (eu07/maszyna#527) + for (auto const &mirror : Gauge.mirrors()) + { + insert(mirror, Label); + } m_names.emplace(Label); } @@ -11266,6 +11271,7 @@ void TTrain::clear_cab_controls() ggLeftEndLightButton.Clear(); ggRightEndLightButton.Clear(); ggLightsButton.Clear(); + ggCompressorListButton.Clear(); // hunter-230112 ggRearLeftLightButton.Clear(); ggRearRightLightButton.Clear(); diff --git a/vehicle/gaugeduplicates.h b/vehicle/gaugeduplicates.h new file mode 100644 index 00000000..3a20c930 --- /dev/null +++ b/vehicle/gaugeduplicates.h @@ -0,0 +1,30 @@ +/* +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 + +// cab controls with more than one submodel (eu07/maszyna#527): a definition of a control in the .mmd marked +// "duplicate: yes" adds a copy of the control already defined in the cab, instead of replacing it +namespace gauge_duplicates { + +// whether the value of the duplicate: key marks the definition as a duplicate +inline bool is_duplicate(std::string_view const Value) { + return Value == "yes" || Value == "true" || Value == "1"; +} + +// whether a definition adds a copy to the control rather than replacing it: it is marked as a duplicate, and the +// control is already loaded from one of the 3d models of the cab being loaded (Cab, Lowpoly). a control left over +// from another cab, or not loaded at all (Loadedfrom null), has nothing in this cab to add the copy to +template bool adds_copy(bool const Duplicate, Model_ const *Loadedfrom, Model_ const *Cab, Model_ const *Lowpoly) { + return Duplicate && Loadedfrom != nullptr && (Loadedfrom == Cab || Loadedfrom == Lowpoly); +} + +} // namespace gauge_duplicates From c99a07ddd742c8ca0019c94ac6f964a90fe8fe4b Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sun, 8 Nov 2026 04:08:15 +0100 Subject: [PATCH 2/2] fix: initialize the gauge's parameter type for CI The gauge copies made for the duplicate submodels start from a default gauge, whose parameter type was left uninitialized (Sonar S836). --- vehicle/Gauge.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vehicle/Gauge.h b/vehicle/Gauge.h index c78cdb06..d83668c4 100644 --- a/vehicle/Gauge.h +++ b/vehicle/Gauge.h @@ -105,7 +105,7 @@ 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 }; - char m_datatype; // typ zmiennej parametru: f-float, d-double, i-int + 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ę float *fData;