From 19b500f9bebdce6125630d3787c5bb684237a099 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Sat, 7 Nov 2026 17:58:20 +0100 Subject: [PATCH] fix(lights): darken the coupled end with the light programmer from either cab (eu07/maszyna#395) SetLights() gives RaLightsSet() the lights for the vehicle's end towards the head of the consist and the end towards its tail, whichever way the occupied cab faces. The coupling that switches an end off was looked up at the opposite end whenever the cab faced the tail of the consist (e.g. 6Dg driven from its second cab position), so the free end went dark and the end with the train was lit. The per-vehicle choice moves to lights_programmer::vehicle_lights() in vehicle/lightsprogrammer.h, which checks the coupling at the same end it lights, with unit tests for both cabs, both orientations and the train at either end. Driving from a cab facing the head of the consist gives the same lights as before. No data change needed. --- tests/unit/test_lights_programmer.cpp | 93 +++++++++++++++++++++++++++ vehicle/DynObj.cpp | 30 ++++----- vehicle/lightsprogrammer.h | 41 ++++++++++++ 3 files changed, 148 insertions(+), 16 deletions(-) create mode 100644 tests/unit/test_lights_programmer.cpp create mode 100644 vehicle/lightsprogrammer.h diff --git a/tests/unit/test_lights_programmer.cpp b/tests/unit/test_lights_programmer.cpp new file mode 100644 index 000000000..8a31563d8 --- /dev/null +++ b/tests/unit/test_lights_programmer.cpp @@ -0,0 +1,93 @@ +#include "stdafx.h" +#include "doctest.h" + +#include "vehicle/lightsprogrammer.h" + +// the lights the light preset selector (programmer) of the occupied cab gives the vehicles of the consist, +// TDynamicObject::SetLights(): the cab's front lights at the end the cab faces, its back lights at the other +// end, and no lights at an end with another vehicle coupled + +namespace { + +int const pc1{light::headlight_left | light::headlight_upper | light::headlight_right}; +int const tailmarkers{light::redmarker_left | light::redmarker_right}; +std::array const cablights{pc1, tailmarkers}; // Lights[end::front], Lights[end::rear] + +// the lights at the vehicle's own end, placed the way TDynamicObject::RaLightsSet() places them +int lights_at(lights_programmer::consist_lights const &Lights, int const Direction, end const Side) { + auto const headend{Direction > 0 ? end::front : end::rear}; + return Side == headend ? Lights.head : Lights.rear; +} + +struct setup { + int cab; // CabOccupied of the vehicle with the programmer + int direction; // its iDirection + std::array coupled; // vehicle coupled at its end::front / end::rear +}; + +lights_programmer::consist_lights lights_for(setup const &Setup) { + // TDynamicObject::SetLights(): the cab faces the head of the consist when CabOccupied * DirectionGet() >= 0 + auto const cabfacinghead{Setup.cab * (Setup.direction > 0 ? 1 : -1) >= 0}; + return lights_programmer::vehicle_lights(cablights, cabfacinghead, Setup.direction, Setup.coupled); +} + +} // namespace + +TEST_CASE("lights programmer: a lone vehicle gets the cab's front lights ahead of the cab and its back lights behind it") { + for (auto const direction : {0, 1}) { + CAPTURE(direction); + // cab 1 faces end::front + auto const fromcab1{lights_for({1, direction, {false, false}})}; + CHECK(lights_at(fromcab1, direction, end::front) == pc1); + CHECK(lights_at(fromcab1, direction, end::rear) == tailmarkers); + // cab -1 faces end::rear + auto const fromcab2{lights_for({-1, direction, {false, false}})}; + CHECK(lights_at(fromcab2, direction, end::rear) == pc1); + CHECK(lights_at(fromcab2, direction, end::front) == tailmarkers); + } +} + +TEST_CASE("lights programmer: the end with the train coupled goes dark, not the other one (eu07/maszyna#395)") { + // a locomotive driven from either cab, with the train coupled at either end, either way round in the consist. + // the end with the train goes dark, the free end shows the lights meant for it + for (auto const direction : {0, 1}) { + for (auto const cab : {1, -1}) { + for (auto const trainend : {end::front, end::rear}) { + CAPTURE(direction); + CAPTURE(cab); + CAPTURE(trainend); + std::array coupled{false, false}; + coupled[trainend] = true; + auto const freeend{trainend == end::front ? end::rear : end::front}; + auto const cabfront{cab > 0 ? end::front : end::rear}; + + auto const lights{lights_for({cab, direction, coupled})}; + + CHECK(lights_at(lights, direction, trainend) == 0); + CHECK(lights_at(lights, direction, freeend) == (freeend == cabfront ? pc1 : tailmarkers)); + } + } + } +} + +TEST_CASE("lights programmer: 6Dg driven backwards from its second cab position pulls the train with its head lights lit (eu07/maszyna#395)") { + // the reported case: the train coupled behind the driver (end::front, which cab -1 has at its back), the + // locomotive running towards end::rear, the train in tow. the head lights stay lit, the end with the train goes dark; + // before the fix the couplings were looked up at the swapped ends and it was the other way round + auto const lights{lights_for({-1, 1, {true, false}})}; + + CHECK(lights_at(lights, 1, end::rear) == pc1); + CHECK(lights_at(lights, 1, end::front) == 0); +} + +TEST_CASE("lights programmer: a vehicle coupled at both ends stays dark") { + for (auto const direction : {0, 1}) { + for (auto const cab : {1, -1}) { + CAPTURE(direction); + CAPTURE(cab); + auto const lights{lights_for({cab, direction, {true, true}})}; + CHECK(lights.head == 0); + CHECK(lights.rear == 0); + } + } +} diff --git a/vehicle/DynObj.cpp b/vehicle/DynObj.cpp index d5f60e332..ce1f89e72 100644 --- a/vehicle/DynObj.cpp +++ b/vehicle/DynObj.cpp @@ -15,6 +15,8 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "vehicle/DynObj.h" +#include "vehicle/lightsprogrammer.h" + #include "simulation/simulation.h" #include "rendering/lightarray.h" #include "vehicle/Camera.h" @@ -7289,26 +7291,22 @@ void TDynamicObject::SetLights() { auto const isfrontcaboccupied { MoverParameters->CabOccupied * DirectionGet() >= 0 }; int const automaticmarkers { MoverParameters->CabActive == 0 && MoverParameters->InactiveCabFlag & activation::redmarkers ? light::redmarker_left + light::redmarker_right : 0 }; - int const front { ( isfrontcaboccupied ? end::front : end::rear ) }; - int const rear { 1 - front }; auto const lightpos { MoverParameters->LightsPos - 1 }; - auto const frontlights { automaticmarkers > 0 ? automaticmarkers : MoverParameters->Lights[ front ][ lightpos ] }; - auto const rearlights { automaticmarkers > 0 ? automaticmarkers : MoverParameters->Lights[ rear ][ lightpos ] }; + std::array const cablights { + automaticmarkers > 0 ? automaticmarkers : MoverParameters->Lights[ end::front ][ lightpos ], + automaticmarkers > 0 ? automaticmarkers : MoverParameters->Lights[ end::rear ][ lightpos ] }; + // the custom position keeps the lights lit at coupled ends as well + auto const ignorecouplings { MoverParameters->LightsPos == 18 }; auto *vehicle { GetFirstDynamic( MoverParameters->CabOccupied >= 0 ? end::front : end::rear, coupling::control ) }; while( vehicle != nullptr ) { // set lights on given side if there's no coupling with another vehicle, turn them off otherwise - auto const *frontvehicle{(isfrontcaboccupied ? vehicle->Prev(coupling::coupler) : vehicle->Next(coupling::coupler))}; - auto const *rearvehicle{(isfrontcaboccupied ? vehicle->Next(coupling::coupler) : vehicle->Prev(coupling::coupler))}; - - if (MoverParameters->LightsPos == 18) - { - frontvehicle = nullptr; - rearvehicle = nullptr; - } - - vehicle->RaLightsSet( - frontvehicle == nullptr ? frontlights : 0, - rearvehicle == nullptr ? rearlights : 0 ); + auto const iscoupled { [ vehicle, ignorecouplings ]( end const Side ) { + return false == ignorecouplings + && ( vehicle->MoverParameters->Couplers[ Side ].CouplingFlag & coupling::coupler ) != 0 + && vehicle->MoverParameters->Neighbours[ Side ].vehicle != nullptr; } }; + auto const lights { lights_programmer::vehicle_lights( + cablights, isfrontcaboccupied, vehicle->iDirection, { iscoupled( end::front ), iscoupled( end::rear ) } ) }; + vehicle->RaLightsSet( lights.head, lights.rear ); vehicle = isfrontcaboccupied ? vehicle->Next(coupling::control) : vehicle->Prev(coupling::control); } }; diff --git a/vehicle/lightsprogrammer.h b/vehicle/lightsprogrammer.h new file mode 100644 index 000000000..9ae8c2ab0 --- /dev/null +++ b/vehicle/lightsprogrammer.h @@ -0,0 +1,41 @@ +/* +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 + +#include "McZapkie/MOVER.h" + +// the lights the light preset selector (programmer) of the occupied cab gives one vehicle of the consist +namespace lights_programmer { + +// the lights for the vehicle's two ends, in the terms of TDynamicObject::RaLightsSet(): head is the end +// towards the head of the consist (TDynamicObject::Prev()), rear the end towards its tail (Next()) +struct consist_lights { + int head; + int rear; +}; + +// Cablights: the selector's lights for the occupied cab's front and back (Lights[end::front / end::rear]). +// Cabfacinghead: the occupied cab faces the head of the consist. +// Direction: the vehicle's iDirection, 1 when its end::front is towards the head of the consist. +// Coupled: whether another vehicle is coupled at the vehicle's end::front / end::rear; an end with a coupled +// vehicle stays dark +inline consist_lights vehicle_lights(std::array const &Cablights, bool const Cabfacinghead, int const Direction, std::array const &Coupled) { + auto const headlights{Cablights[Cabfacinghead ? end::front : end::rear]}; + auto const rearlights{Cablights[Cabfacinghead ? end::rear : end::front]}; + // the head lights go to the vehicle's end towards the head of the consist whichever way the cab faces, + // so the coupling which keeps them dark is the one at that same end (eu07/maszyna#395) + auto const headend{Direction > 0 ? end::front : end::rear}; + auto const rearend{Direction > 0 ? end::rear : end::front}; + return {Coupled[headend] ? 0 : headlights, Coupled[rearend] ? 0 : rearlights}; +} + +} // namespace lights_programmer