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

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.
This commit is contained in:
jerrrrycho
2026-11-07 17:58:20 +01:00
parent 939e42b611
commit 19b500f9be
3 changed files with 148 additions and 16 deletions

View File

@@ -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<int, 2> 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);
}
};

View File

@@ -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 <array>
#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<int, 2> const &Cablights, bool const Cabfacinghead, int const Direction, std::array<bool, 2> 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