18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 14:16:48 +01:00
Files
maszyna/vehicle/dynamic/lights.cpp
T
Hirek 242474dd11 refactor(physics): the lights, the load and the event flags are private to the vehicle
- The lights switched on at each end (iLights) are LitLights(), changed
  through SetLitLights(): the cab's switches, the AI's light patterns and the
  scenery's toggles of the end signals all go through it.
- The exchange of the load, which TDynamicObject ran by setting the status, the
  amount and the mass itself, goes through UnloadStep(), LoadStep(),
  FinishLoadExchange() and AcknowledgeLoadExchange(), with the same arithmetic;
  the change of the kind of the load is taken once (TakeLoadTypeChange()).
- The events the physics reports for the sounds to follow up are raised and
  cleared by name (RaiseEventFlag(), ClearEventFlag(), ClearSoundFlags()).

No change in behaviour: the golden traces recorded on dfb43c4f match bit for bit.
2026-11-29 12:33:13 +01:00

314 lines
12 KiB
C++

/*
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/.
*/
/*
MaSzyna EU07 locomotive simulator
Copyright (C) 2001-2004 Marcin Wozniak, Maciej Czapkiewicz and others
*/
// the lights of a vehicle and the signals they make
#include "stdafx.h"
#include "vehicle/DynObj.h"
#include "physics/vehicle.h"
#include "vehicle/lightsprogrammer.h"
#include "simulation/simulation.h"
#include "simulation/blackboxrecorder.h"
#include "rendering/lightarray.h"
#include "vehicle/Camera.h"
#include "vehicle/Train.h"
#include "vehicle/odometer.h"
#include "vehicle/pulleemode.h"
#include "vehicle/Driver.h"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <format>
#include <random>
#include <iterator>
#include <numbers>
#include <optional>
#include <string_view>
#include <unordered_set>
#include <utility>
#include <vector>
#include "utilities/Globals.h"
#include "utilities/Timer.h"
#include "utilities/workerpool.h"
#include "utilities/Logs.h"
#include "utilities/glmHelpers.h"
#include "Console.h"
#include "world/Traction.h"
#include "audio/sound.h"
#include "model/MdlMngr.h"
#include "model/Model3d.h"
#include "vehicle/doorwings.h"
#include "rendering/renderer.h"
#include "editor/editorTerrainStreamer.hpp"
#include "application/uitranscripts.h"
#include "input/messaging.h"
#include "application/application.h"
#include "vehicle/Driver.h"
#include "vehicle/dynamic/internal.h"
void TDynamicObject::SetLights() {
auto const isfrontcaboccupied { physics->CabOccupied() * DirectionGet() >= 0 };
int const automaticmarkers { physics->CabActive == 0 && physics->InactiveCabFlag & activation::redmarkers
? light::redmarker_left + light::redmarker_right : 0 };
auto const lightpos { physics->LightsPos() - 1 };
ByEnd<int> const cablights {
automaticmarkers > 0 ? automaticmarkers : physics->Lights[ VehicleEnd::front ][ lightpos ],
automaticmarkers > 0 ? automaticmarkers : physics->Lights[ VehicleEnd::rear ][ lightpos ] };
// the custom position keeps the lights lit at coupled ends as well
auto const ignorecouplings { physics->LightsPos() == 18 };
auto *vehicle { GetFirstDynamic( physics->CabOccupied() >= 0 ? VehicleEnd::front : VehicleEnd::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 iscoupled { [ vehicle, ignorecouplings ]( VehicleEnd const Side ) {
return false == ignorecouplings
&& vehicle->physics->Couplers[ Side ].CouplingFlag.HasAny(Coupling::coupler)
&& vehicle->physics->Neighbours[ Side ].vehicle != nullptr; } };
auto const lights { lights_programmer::vehicle_lights(
cablights, isfrontcaboccupied, vehicle->iDirection, { iscoupled( VehicleEnd::front ), iscoupled( VehicleEnd::rear ) } ) };
vehicle->RaLightsSet( lights.head, lights.rear );
vehicle = isfrontcaboccupied ? vehicle->Next(Coupling::control) : vehicle->Prev(Coupling::control);
}
};
void TDynamicObject::RaLightsSet(int head, int rear)
{ // zapalenie świateł z przodu i z
// tyłu, zależne od kierunku
// pojazdu
if (!physics)
return; // może tego nie być na początku
if (rear == ( light::redmarker_left | light::redmarker_right | light::rearendsignals ) )
{ // jeśli koniec pociągu, to trzeba ustalić, czy
// jest tam czynna lokomotywa
// EN57 może nie mieć końcówek od środka członu
if (physics->Power > 1.0) // jeśli ma moc napędową
if (!physics->DirActive) // jeśli nie ma ustawionego kierunku
{ // jeśli ma zarówno światła jak i końcówki, ustalić, czy jest w stanie aktywnym
// np. lokomotywa na zimno będzie mieć końcówki a nie światła
rear = light::rearendsignals; // tablice blaszane
// trzeba to uzależnić od "załączenia baterii" w pojeździe
}
if( rear == ( light::redmarker_left | light::redmarker_right | light::rearendsignals ) ) // jeśli nadal obydwie możliwości
if( iInventory[
( iDirection ?
VehicleEnd::rear :
VehicleEnd::front ) ] & ( light::redmarker_left | light::redmarker_right ) ) {
// czy ma jakieś światła czerowone od danej strony
rear = light::redmarker_left | light::redmarker_right; // dwa światła czerwone
}
else {
rear = light::rearendsignals; // tablice blaszane
}
}
// w zależności od kierunku pojazdu w składzie
if( head >= 0 ) {
auto const vehicleend { iDirection > 0 ? VehicleEnd::front : VehicleEnd::rear };
physics->SetLitLights( vehicleend, head & iInventory[vehicleend] );
bool tLeft = physics->LitLights()[vehicleend] & (light::auxiliary_left | light::headlight_left); // roboczo czy jakiekolwiek swiatlo z lewej jest zapalone
bool tRight = physics->LitLights()[vehicleend] & (light::auxiliary_right | light::headlight_right); // a tu z prawej
if (Controller == Humandriver) {
auto const dps{physics->currentDimPosition()};
// domyślnie
HighBeamLights = false;
DimHeadlights = false;
// obsługa OFF
if (dps.isOff)
{
//physics->SetLitLights(vehicleend, physics->LitLights()[vehicleend] & (0 | light::rearendsignals)); // zostawiamy tylko tabliczki jesli sa
HighBeamLights = false;
DimHeadlights = false;
// the headlights at the end of the active cab: A at the front, B at the rear
if( auto const cabend { CabEnd( physics->CabActive ) }; cabend.has_value() ) {
( *cabend == VehicleEnd::front ? HeadlightsAoff : HeadlightsBoff ) = true;
}
}
else
{
if( auto const cabend { CabEnd( physics->CabActive ) }; cabend.has_value() ) {
( *cabend == VehicleEnd::front ? HeadlightsAoff : HeadlightsBoff ) = false;
}
}
// obsługa przyciemnionych
DimHeadlights = dps.isDimmed;
// obsługa długich
if (dps.isHighBeam)
{
HighBeamLights = true;
}
}
}
if( rear >= 0 ) {
auto const vehicleend{ iDirection > 0 ? VehicleEnd::rear : VehicleEnd::front };
physics->SetLitLights( vehicleend, rear & iInventory[vehicleend] );
}
};
void TDynamicObject::SetLightDimmings()
{
if (Controller == Humandriver)
{
auto const dps{physics->currentDimPosition()};
// domyślnie
HighBeamLights = false;
DimHeadlights = false;
// obsługa OFF: the headlights at the end of the active cab, as RaLightsSet() picks them: A at the front, B at the
// rear, none without an active cab
if (dps.isOff)
{
HighBeamLights = false;
DimHeadlights = false;
if (auto const cabend{CabEnd(physics->CabActive)}; cabend.has_value())
{
(*cabend == VehicleEnd::front ? HeadlightsAoff : HeadlightsBoff) = true;
}
}
else
{
if (auto const cabend{CabEnd(physics->CabActive)}; cabend.has_value())
{
(*cabend == VehicleEnd::front ? HeadlightsAoff : HeadlightsBoff) = false;
}
}
// obsługa przyciemnionych
DimHeadlights = dps.isDimmed;
// obsługa długich
if (dps.isHighBeam)
{
// obsługa długich
if (dps.isHighBeam)
{
HighBeamLights = true;
}
}
}
}
bool TDynamicObject::has_signal_pc1_on() const {
auto const vehicleend { iDirection > 0 ? VehicleEnd::front : VehicleEnd::rear };
auto const equippedlights { iInventory[ vehicleend ] };
auto const pattern { equippedlights & ( light::headlight_left | light::headlight_right | light::headlight_upper ) };
auto const patternfallback { equippedlights & ( light::auxiliary_left | light::auxiliary_right | light::headlight_upper ) };
auto const hasauxiliarylights { ( equippedlights & ( light::auxiliary_left | light::auxiliary_right ) ) != 0 };
auto const activelights { physics->LitLights()[ vehicleend ] };
return (pattern != 0 && activelights == pattern) || (hasauxiliarylights && activelights == patternfallback) ||
(pattern == 0 && patternfallback == 0 && activelights == light::rearendsignals); // pc4
}
bool TDynamicObject::has_signal_pc2_on() const {
auto const vehicleend { iDirection > 0 ? VehicleEnd::front : VehicleEnd::rear };
auto const equippedlights { iInventory[ vehicleend ] };
auto const pattern { equippedlights & ( light::redmarker_left | light::headlight_right | light::headlight_upper ) };
auto const patternfallback { equippedlights & ( light::redmarker_left | light::auxiliary_right | light::headlight_upper ) };
auto const hasauxiliarylights { ( equippedlights & ( light::auxiliary_left | light::auxiliary_right ) ) != 0 };
auto const activelights { physics->LitLights()[ vehicleend ] };
return activelights == pattern || (hasauxiliarylights && activelights == patternfallback);
}
bool TDynamicObject::has_signal_pc5_on() const {
auto const vehicleend { iDirection > 0 ? VehicleEnd::rear : VehicleEnd::front };
auto const equippedlights { iInventory[ vehicleend ] };
auto const pattern { equippedlights & ( light::redmarker_left | light::redmarker_right ) };
auto const patternfallback { equippedlights & light::rearendsignals };
auto const activelights { physics->LitLights()[ vehicleend ] };
return (pattern != 0 && activelights == pattern) || (patternfallback != 0 && activelights == patternfallback);
}
bool TDynamicObject::has_signal_on( VehicleEnd const Side, int const Pattern ) const {
/*
auto const vehicleend { iDirection > 0 ? Side : 1 - Side };
auto const pattern { iInventory[ vehicleend ] & Pattern };
return ( physics->LitLights()[ vehicleend ] == pattern );
*/
return physics->LitLights()[Side] == (Pattern & iInventory[Side]);
}
TDimension const &TDynamicObject::Dimensions() const {
return physics->Dim;
}
double TDynamicObject::Speed() const {
return physics->V;
}
int TDynamicObject::ActiveCab() const {
return physics->CabActive;
}
float TDynamicObject::CompartmentLightIntensity() const {
return physics->CompartmentLights.intensity;
}
double TDynamicObject::CouplerAdapterHeight( VehicleEnd const End ) const {
return physics->Couplers[ End ].adapter_height;
}
double TDynamicObject::CouplerAdapterLength( VehicleEnd const End ) const {
return physics->Couplers[ End ].adapter_length;
}
void TDynamicObject::ExhaustScales( float &Velocity, float &Opacity ) const {
Opacity = static_cast<float>(physics->diesel.fill);
if (physics->EngineType == TEngineType::DieselElectric)
{
if (physics->diesel.spinningUp == true)
{
Velocity = static_cast<float>(0.38*(((physics->enrot)/2)*0.5));
}
else
{
Velocity = static_cast<float>((((physics->enrot)/60)*0.6) * (physics->EnginePower *0.03));
}
}
}
bool TDynamicObject::LowVoltagePowered() const {
return ( true == physics->lowVoltage.Is24vAvailable() ) || ( true == physics->lowVoltage.Is110vAvailable() );
}
int TDynamicObject::LightsSwitchedOn( VehicleEnd const End ) const {
return physics->LitLights()[ End ];
}
double TDynamicObject::HeadlightsMultiplier() const {
if( DimHeadlights && !HighBeamLights ) { return physics->dimMultiplier; } // tylko przyciemnione
if( !DimHeadlights && !HighBeamLights ) { return physics->normMultiplier; } // normalne
if( DimHeadlights && HighBeamLights ) { return physics->highDimMultiplier; } // przyciemnione dlugie
return physics->highMultiplier; // dlugie zwykle
}
glm::vec3 TDynamicObject::HeadlightsColor() const {
return {
static_cast<float>(physics->refR) / 255.0f,
static_cast<float>(physics->refG) / 255.0f,
static_cast<float>(physics->refB) / 255.0f };
}