18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 23:17:56 +01:00
Files
maszyna/vehicle/cab/train.cpp
T
Hirek d0387753f8 refactor(physics): TMoverParameters becomes physics::Vehicle, the mover is gone
The physics of a vehicle loses the name it had from the Pascal of 2001. Done by
tools/rename/mover_to_physics.py, committed with it:

  physics/mover.h                  physics/vehicle.h
  TMoverParameters                 physics::Vehicle (in namespace physics)
  TDynamicObject::MoverParameters  physics
  mvOccupied, mvControlled         occupied, controlled (TTrain, TController)
  mvSecond, mvThird                secondUnit, thirdUnit
  mvPantographUnit                 pantographUnit
  mover, Mover, movers             physics, Physics, vehicles (locals, params)
  m_mover, othermover, currentMover, from_movers, renderMoverEventSounds
                                   m_physics, otherPhysics, currentPhysics,
                                   from_vehicles, renderPhysicsEventSounds

The code and the comments are renamed, the string literals are not: no text
the game shows, logs or looks up has changed (none had the names). The movers
of the editor, the tracks a slide moves, keep their name.

By hand: two member functions of TDynamicObject (gather_off_track_terrain,
begin_off_track) had a local mover aliasing their own MoverParameters, which
the rename made physics { *physics }; the alias is gone, they use physics->.
No other function has a renamed local meeting a renamed member (checked over
all the functions of the code before the rename).

For the branches made before it: run the script on the branch (python
tools/rename/mover_to_physics.py, from the root of the repository) before
merging it with this one, and the names don't conflict. It can be run again;
the second time it changes nothing.

The class is the same as it was; breaking it into its parts (braking,
electrics, controls, load) and making its data private are the next stages.

No change in behaviour: 1149/1149 tests, golden traces exact against staging
62f7a29b.
2026-11-28 23:56:46 +01:00

945 lines
33 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 train the user drives: construction, its state, taking a vehicle, the trains of the scenery
#include "stdafx.h"
#include "vehicle/Train.h"
#include "vehicle/horn.h"
#include "vehicle/linebreaker.h"
#include "vehicle/holdcoillever.h"
#include "vehicle/compartmentlightsswitch.h"
#include "vehicle/odometer.h"
#include "vehicle/mainctrlact.h"
#include "utilities/Globals.h"
#include "simulation/simulation.h"
#include "simulation/blackboxrecorder.h"
#include "world/Event.h"
#include "simulation/simulationtime.h"
#include "vehicle/Camera.h"
#include "utilities/Logs.h"
#include "model/MdlMngr.h"
#include "model/Model3d.h"
#include "utilities/Timer.h"
#include "vehicle/Driver.h"
#include "vehicle/DynObj.h"
#include "world/mtable.h"
#include "Console.h"
#include "application/application.h"
#include "rendering/renderer.h"
#include "physics/vehicle.h"
#include <future>
#include <thread>
#include <cmath>
#include <algorithm>
#include <format>
#include "vehicle/cab/internal.h"
std::vector<std::string> const TTrain::fPress_labels = {
"ch1: ", "ch2: ", "ch3: ", "ch4: ", "ch5: ", "ch6: ", "ch7: ", "ch8: ", "ch9: ", "ch0: "};
TTrain::TTrain()
{
// McZapkie-240302 - przyda sie do tachometru
// asMessage="";
DynamicSet(nullptr); // ustawia wszystkie mv*
//-----
//
for (int i = 0; i < 8; i++)
{
bMains[i] = false;
fCntVol[i] = 0.0f;
bPants[i][0] = false;
bPants[i][1] = false;
bFuse[i] = false;
bBatt[i] = false;
bConv[i] = false;
bComp[i][0] = false;
bComp[i][1] = false;
// bComp[ i ][ 2 ] = false;
// bComp[ i ][ 3 ] = false;
bHeat[i] = false;
}
bCompressors.clear();
for (int i = 0; i < 9; ++i)
for (int j = 0; j < 10; ++j)
{
fEIMParams[i][j] = 0.0;
fDieselParams[i][j] = 0.0;
}
for (int i = 0; i < 20; ++i)
{
for (auto &press : fPress[i])
press = 0.0;
bBrakes[i][0] = false;
bBrakes[i][1] = false;
}
}
TTrain::~TTrain() = default;
bool TTrain::Init(TDynamicObject *NewDynamicObject, bool e3d)
{ // powiązanie ręcznego sterowania kabiną z pojazdem
if (NewDynamicObject->Mechanik == nullptr)
{
/*
ErrorLog( "Bad config: can't take control of inactive vehicle \"" + NewDynamicObject->asName + "\"" );
return false;
*/
auto const activecab{(NewDynamicObject->physics->CabOccupied > 0 ? "1" : NewDynamicObject->physics->CabOccupied < 0 ? "2" : "p")};
NewDynamicObject->create_controller(activecab, NewDynamicObject->ctOwner != nullptr);
}
DynamicSet(NewDynamicObject);
if (!e3d && DynamicObject->Mechanik == nullptr)
return false;
DynamicObject->MechInside = true;
fMainRelayTimer = 0; // Hunter, do k...y nędzy, ustawiaj wartości początkowe zmiennych!
iCabn = occupied->CabOccupied > 0 ? 1 : occupied->CabOccupied < 0 ? 2 : 0;
{
Global.CurrentMaxTextureSize = Global.iMaxCabTextureSize;
auto const filename{occupied->TypeName + ".mmd"};
LoadMMediaFile(filename);
InitializeCab(occupied->CabOccupied, filename);
Global.CurrentMaxTextureSize = Global.iMaxTextureSize;
if (DynamicObject->Controller == Humandriver)
{
// McZapkie-030303: mozliwosc wyswietlania kabiny, w przyszlosci dac opcje w mmd
DynamicObject->bDisplayCab = true;
}
}
// Ra: taka proteza - przesłanie kierunku do członów connected
/*
if (controlled->DirActive > 0)
{ // było do przodu
controlled->DirectionBackward();
controlled->DirectionForward();
}
else if (controlled->DirActive < 0)
{
controlled->DirectionForward();
controlled->DirectionBackward();
}
*/
if (false == DynamicObject->Mechanik->AIControllFlag)
{
DynamicObject->Mechanik->sync_consist_reversers();
}
return true;
}
std::shared_ptr<dictionary_source> TTrain::GetTrainState(dictionary_source const &Extraparameters)
{
if (occupied == nullptr || controlled == nullptr)
{
return nullptr;
}
auto dict = std::make_shared<dictionary_source>(Extraparameters);
if (dict == nullptr)
{
return nullptr;
}
dict->insert("name", DynamicObject->asName);
dict->insert("cab", occupied->CabOccupied);
dict->insert("cabactive", occupied->CabActive);
dict->insert("master", occupied->CabMaster);
// basic systems state data
dict->insert("battery", occupied->Power24vIsAvailable);
dict->insert("linebreaker", controlled->Mains);
dict->insert("main_init", controlled->MainsInitTimeCountdown < controlled->MainsInitTime && controlled->MainsInitTimeCountdown > 0.0);
dict->insert("main_ready", false == controlled->Mains && fHVoltage > 0.0 && controlled->MainsInitTimeCountdown <= 0.0);
dict->insert("converter", occupied->Power110vIsAvailable);
dict->insert("converter_overload", controlled->ConvOvldFlag);
dict->insert("compress", controlled->CompressorFlag);
dict->insert("pant_compressor", pantographUnit->PantCompFlag);
dict->insert("lights_front", occupied->iLights[VehicleEnd::front]);
dict->insert("lights_rear", occupied->iLights[VehicleEnd::rear]);
dict->insert("off_from_dimmer", occupied->currentDimPosition().isOff);
dict->insert("lights_compartments", occupied->CompartmentLights.is_active || occupied->CompartmentLights.is_disabled);
if (Dynamic()->Mechanik)
{
auto const *controller{Dynamic()->Mechanik};
auto const cabmodifier{cab_to_end() == VehicleEnd::front ? 1 : -1};
auto const traindirection{controller->Direction() * cabmodifier};
auto const *frontvehicle{controller->Vehicle(traindirection >= 0 ? VehicleEnd::front : VehicleEnd::rear)};
auto const *rearvehicle{controller->Vehicle(traindirection >= 0 ? VehicleEnd::rear : VehicleEnd::front)};
auto const frontvehicledirection{(frontvehicle->DirectionGet() == controller->Vehicle()->DirectionGet() ? 1 : -1)};
auto const rearvehicledirection{(rearvehicle->DirectionGet() == controller->Vehicle()->DirectionGet() ? 1 : -1)};
auto const fronttrainlights{frontvehicle->physics->iLights[frontvehicledirection * cabmodifier >= 0 ? VehicleEnd::front : VehicleEnd::rear]};
auto const reartrainlights{rearvehicle->physics->iLights[rearvehicledirection * cabmodifier >= 0 ? VehicleEnd::rear : VehicleEnd::front]};
dict->insert("lights_train_front", fronttrainlights);
dict->insert("lights_train_rear", reartrainlights);
}
else
{
// fallback, in the unlikely case we lose the controller
dict->insert("lights_train_front", occupied->iLights[VehicleEnd::front]);
dict->insert("lights_train_rear", occupied->iLights[VehicleEnd::rear]);
}
// reverser
dict->insert("direction", occupied->DirActive);
// throttle
dict->insert("mainctrl_pos", controlled->MainCtrlPos);
dict->insert("mainctrl_pos_count", controlled->MainCtrlPosNo);
dict->insert("main_ctrl_actual_pos", controlled->MainCtrlActualPos);
dict->insert("scndctrl_pos", controlled->ScndCtrlPos);
dict->insert("dynamicbrake_pos", controlled->DynamicBrakeCtrlPos);
dict->insert("scnd_ctrl_actual_pos", controlled->ScndCtrlActualPos);
dict->insert("brakectrl_pos", controlled->fBrakeCtrlPos);
dict->insert("localbrake_pos", controlled->LocalBrakePosA);
dict->insert("new_speed", occupied->NewSpeed);
dict->insert("speedctrl", occupied->SpeedCtrlValue);
dict->insert("speedctrlpower", occupied->SpeedCtrlUnit.DesiredPower);
dict->insert("speedctrlactive", occupied->SpeedCtrlUnit.IsActive);
dict->insert("speedctrlstandby", occupied->SpeedCtrlUnit.Standby);
// brakes
dict->insert("manual_brake", occupied->ManualBrakePos > 0);
bool const bEP = controlled->LocHandle->GetCP() > 0.2 || fEIMParams[0][5] > 0.01;
dict->insert("dir_brake", bEP);
bool bPN{false};
auto const &brakevalve{*occupied->Hamulec};
if (auto const &brakevalvetype{typeid(brakevalve)}; brakevalvetype == typeid(TLSt) || brakevalvetype == typeid(TEStED))
{
TBrake *temp_ham = occupied->Hamulec.get();
bPN = static_cast<TLSt *>(temp_ham)->GetEDBCP() > 0.2;
}
dict->insert("indir_brake", bPN);
dict->insert("emergency_brake", occupied->AlarmChainFlag);
dict->insert("brake_delay_flag", occupied->BrakeDelayFlag);
dict->insert("brake_op_mode_flag", occupied->BrakeOpModeFlag);
dict->insert("pipelock", occupied->LockPipe);
// other controls
dict->insert("ca", occupied->SecuritySystem.is_vigilance_blinking());
dict->insert("shp", occupied->SecuritySystem.is_cabsignal_blinking());
dict->insert("distance_counter", m_distancecounter);
dict->insert("pantpress", std::abs(pantographUnit->PantPress));
dict->insert("universal3", InstrumentLightActive);
for (auto idx = 0; idx < ggUniversals.size(); idx++)
{
if (idx != 3)
{
dict->insert("universal" + std::to_string(idx), ggUniversals[idx].GetValue() > 0.5);
}
}
dict->insert("radio", occupied->Radio);
dict->insert("radio_fuse", occupied->RadioFuse);
dict->insert("radio_channel", RadioChannel());
dict->insert("radio_volume", m_radiovolume);
dict->insert("door_lock", occupied->Doors.lock_enabled);
dict->insert("door_step", occupied->Doors.step_enabled);
dict->insert("door_permit_left", occupied->Doors.instances[VehicleSide::left].open_permit);
dict->insert("door_permit_right", occupied->Doors.instances[VehicleSide::right].open_permit);
// movement data
dict->insert("velocity", std::abs(occupied->Vel));
dict->insert("tractionforce", std::abs(occupied->Ft));
dict->insert("slipping_wheels", occupied->SlippingWheels);
dict->insert("sanding", occupied->SandDose);
dict->insert("odometer", occupied->DistCounter); // distance run in the session, km
dict->insert("odometer_total", occupied->Odometer.km); // the vehicle's odometer, kept between the sessions
// electric current data
dict->insert("traction_voltage", std::abs(pantographUnit->PantographVoltage));
dict->insert("voltage", std::abs(controlled->EngineVoltage));
dict->insert("im", std::abs(controlled->Im));
dict->insert("fuse", controlled->FuseFlag);
dict->insert("epfuse", occupied->EpFuse);
dict->insert("power_drawn", occupied->EnergyMeter.first);
dict->insert("power_returned", occupied->EnergyMeter.second);
// induction motor state data
std::array<char const *, 10> const TXTT{"fd", "fdt", "fdb", "pd", "pdt", "pdb", "itothv", "1", "2", "3"};
std::array<char const *, 10> const TXTC{"fr", "frt", "frb", "pr", "prt", "prb", "im", "vm", "ihv", "uhv"};
std::array<char const *, 10> const TXTD{"enrot", "nrot", "fill_des", "fill_real", "clutch_des", "clutch_real", "water_temp", "oil_press", "engine_temp", "retarder_fill"};
std::array<char const *, 7> const TXTP{"bc", "bp", "sp", "cp", "rp", "mass", "spring"};
char const *TXTB[2] = {"spring_active", "spring_shutoff"};
for (int j = 0; j < 10; ++j)
dict->insert("eimp_t_" + std::string(TXTT[j]), fEIMParams[0][j]);
for (int i = 0; i < 8; ++i)
{
auto const idx{std::to_string(i + 1)};
for (int j = 0; j < 10; ++j)
dict->insert("eimp_c" + idx + "_" + std::string(TXTC[j]), fEIMParams[i + 1][j]);
for (int j = 0; j < 10; ++j)
dict->insert("diesel_param_" + idx + "_" + std::string(TXTD[j]), fDieselParams[i + 1][j]);
dict->insert("eimp_c" + idx + "_ms", bMains[i]);
dict->insert("eimp_c" + idx + "_cv", fCntVol[i]);
dict->insert("eimp_c" + idx + "_fuse", bFuse[i]);
dict->insert("eimp_c" + idx + "_batt", bBatt[i]);
dict->insert("eimp_c" + idx + "_conv", bConv[i]);
dict->insert("eimp_c" + idx + "_heat", bHeat[i]);
dict->insert("eimp_u" + idx + "_pf", bPants[i][0]);
dict->insert("eimp_u" + idx + "_pr", bPants[i][1]);
dict->insert("eimp_u" + idx + "_comp_a", bComp[i][0]);
dict->insert("eimp_u" + idx + "_comp_w", bComp[i][1]);
}
dict->insert("compressors_no", (int)bCompressors.size());
for (int i = 0; i < bCompressors.size(); i++)
{
auto const idx{std::to_string(i + 1)};
dict->insert("compressors_" + idx + "_allow", std::get<0>(bCompressors[i]));
dict->insert("compressors_" + idx + "_work", std::get<1>(bCompressors[i]));
dict->insert("compressors_" + idx + "_car_no", std::get<2>(bCompressors[i]));
}
bool kier = DynamicObject->DirectionGet() * occupied->CabOccupied > 0;
TDynamicObject *p = DynamicObject->GetFirstDynamic(occupied->CabOccupied < 0 ? VehicleEnd::rear : VehicleEnd::front, Coupling::control);
int in = 0;
while (p && in < 8)
{
if (p->physics->inductionMotor.traction.maxPower > 1)
{
in++;
dict->insert("eimp_c" + std::to_string(in) + "_invno", p->physics->inductionMotor.inverterCount);
for (int j = 0; j < p->physics->inductionMotor.inverterCount; j++)
{
dict->insert("eimp_c" + std::to_string(in) + "_inv" + std::to_string(j + 1) + "_act", p->physics->inductionMotor.inverters[j].IsActive);
dict->insert("eimp_c" + std::to_string(in) + "_inv" + std::to_string(j + 1) + "_error", p->physics->inductionMotor.inverters[j].Error);
dict->insert("eimp_c" + std::to_string(in) + "_inv" + std::to_string(j + 1) + "_allow", p->physics->inductionMotor.inverters[j].Activate);
}
}
p = kier ? p->Next(Coupling::control) : p->Prev(Coupling::control);
}
for (int i = 0; i < 20; ++i)
{
for (int j = 0; j < 7; ++j)
{
dict->insert("eimp_pn" + std::to_string(i + 1) + "_" + TXTP[j], fPress[i][j]);
}
for (int j = 0; j < 2; ++j)
{
dict->insert("brakes_" + std::to_string(i + 1) + "_" + TXTB[j], bBrakes[i][j]);
}
}
// multi-unit state data
dict->insert("car_no", iCarNo);
dict->insert("power_no", iPowerNo);
dict->insert("unit_no", iUnitNo);
for (int i = 0; i < 20; i++)
{
auto const caridx{std::to_string(i + 1)};
dict->insert("doors_" + caridx, bDoors[i][0]);
dict->insert("doors_l_" + caridx, bDoors[i][1]);
dict->insert("doors_r_" + caridx, bDoors[i][2]);
dict->insert("doorstep_l_" + caridx, bDoors[i][3]);
dict->insert("doorstep_r_" + caridx, bDoors[i][4]);
dict->insert("doors_no_" + caridx, iDoorNo[i]);
dict->insert("code_" + caridx, std::to_string(iUnits[i]) + cCode[i]);
dict->insert("car_name" + caridx, asCarName[i]);
dict->insert("slip_" + caridx, bSlip[i]);
}
// ai state data
auto const *vehicledriver{(DynamicObject->ctOwner != nullptr ? DynamicObject->ctOwner : DynamicObject->Mechanik)};
dict->insert("velocity_desired", vehicledriver->VelDesired);
dict->insert("velroad", vehicledriver->VelRoad);
dict->insert("vellimitlast", vehicledriver->VelLimitLast);
dict->insert("velsignallast", vehicledriver->VelSignalLast);
dict->insert("velsignalnext", vehicledriver->VelSignalNext);
dict->insert("velnext", vehicledriver->VelNext);
dict->insert("actualproximitydist", vehicledriver->ActualProximityDist);
// train data
vehicledriver->TrainTimetable().serialize(dict.get());
dict->insert("train_atpassengerstop", vehicledriver->IsAtPassengerStop);
dict->insert("train_length", vehicledriver->fLength);
// world state data
dict->insert("scenario", Global.SceneryFile);
dict->insert("hours", static_cast<int>(simulation::Time.data().wHour));
dict->insert("minutes", static_cast<int>(simulation::Time.data().wMinute));
dict->insert("seconds", static_cast<int>(simulation::Time.second()));
dict->insert("air_temperature", Global.AirTemperature);
dict->insert("light_level", Global.fLuminance - std::max(0.f, Global.Overcast - 1.f));
return dict;
}
TTrain::state_t TTrain::get_state() const
{
return {
btLampkaSHP.GetValue(),
btLampkaCzuwaka.GetValue(),
btLampkaRadioStop.GetValue(),
btLampkaOpory.GetValue(),
btLampkaWylSzybki.GetValue(),
btLampkaPrzekRozn.GetValue(),
btLampkaNadmSil.GetValue(),
btLampkaStyczn.GetValue(),
btLampkaPoslizg.GetValue(),
btLampkaNadmPrzetw.GetValue(),
btLampkaPrzetwOff.GetValue(),
btLampkaNadmSpr.GetValue(),
btLampkaNadmWent.GetValue(),
btLampkaWysRozr.GetValue(),
btLampkaOgrzewanieSkladu.GetValue(),
static_cast<std::uint8_t>(iCabn),
btHaslerBrakes.GetValue(),
btHaslerCurrent.GetValue(),
occupied->SecuritySystem.is_beeping(),
btLampkaHVoltageB.GetValue(),
fTachoVelocity,
static_cast<float>(occupied->Compressor),
static_cast<float>(occupied->PipePress),
static_cast<float>(occupied->BrakePress),
static_cast<float>(pantographUnit->PantPress),
fHVoltage,
{fHCurrent[controlled->TrainType == train_type::emu ? 0 : 1], fHCurrent[2], fHCurrent[3]},
ggLVoltage.GetValue(),
occupied->DistCounter,
static_cast<std::uint8_t>(RadioChannel()),
btLampkaSpringBrakeActive.GetValue(),
btLampkaNapNastHam.GetValue(),
occupied->DirActive > 0,
occupied->DirActive < 0,
occupied->Doors.instances[occupied->CabOccupied < 0 ? VehicleSide::right : VehicleSide::left].open_permit,
occupied->Doors.instances[occupied->CabOccupied < 0 ? VehicleSide::right : VehicleSide::left].is_open,
occupied->Doors.instances[occupied->CabOccupied < 0 ? VehicleSide::left : VehicleSide::right].open_permit,
occupied->Doors.instances[occupied->CabOccupied < 0 ? VehicleSide::left : VehicleSide::right].is_open,
occupied->Doors.step_enabled,
occupied->Power24vIsAvailable,
0,
occupied->LockPipe,
btLampkaRadioMessage.GetValue(),
};
}
bool TTrain::is_eztoer() const
{
return controlled->TrainType == train_type::emu && occupied->BrakeSubsystem == TBrakeSubSystem::ss_ESt && controlled->Power24vIsAvailable == true && controlled->EpFuse == true &&
controlled->DirActive != 0; // od yB
}
// physics master controller to specified position
void TTrain::set_master_controller(double const Position)
{
auto positionchange{std::min<int>(static_cast<int>(Position), controlled->CoupledCtrl ? controlled->MainCtrlPosNo + controlled->ScndCtrlPosNo : controlled->MainCtrlPosNo) -
(controlled->CoupledCtrl ? controlled->MainCtrlPos + controlled->ScndCtrlPos : controlled->MainCtrlPos)};
while (positionchange < 0 && true == controlled->DecMainCtrl(1))
{
++positionchange;
}
while (positionchange > 0 && true == controlled->IncMainCtrl(1))
{
--positionchange;
}
}
// moves train brake lever to specified position, potentially emits switch sound if conditions are met
void TTrain::set_train_brake(double const Position)
{
auto const originalbrakeposition{static_cast<int>(100.0 * occupied->fBrakeCtrlPos)};
occupied->BrakeLevelSet(Position);
if (static_cast<int>(100.0 * occupied->fBrakeCtrlPos) == originalbrakeposition)
{
return;
}
// sound feedback if the lever movement activates one of the switches
if (true == is_eztoer() && false == ((originalbrakeposition / 100 == 0 || originalbrakeposition / 100 >= 5) && (occupied->BrakeCtrlPos == 0 || occupied->BrakeCtrlPos >= 5)) && dsbPneumaticSwitch)
{
dsbPneumaticSwitch->play();
}
}
void TTrain::zero_charging_train_brake()
{
using enum TBrakeHandle;
if (occupied->BrakeCtrlPos == -1 && DynamicObject->Controller != AIdriver && Global.iFeedbackMode < 3 &&
(occupied->BrakeHandle == FVel6 || occupied->BrakeHandle == MHZ_EN57 || occupied->BrakeHandle == MHZ_K8P))
{
// Odskakiwanie hamulce EP
set_train_brake(0);
}
}
void TTrain::set_train_brake_speed(TDynamicObject *Vehicle, int const Speed)
{
// visual feedback
// TODO: add setting indicator to vehicle class, for external lever/indicator
if (true == Vehicle->physics->BrakeDelaySwitch(Speed) && Vehicle == DynamicObject)
{
if (ggBrakeProfileCtrl.SubModel != nullptr)
{
ggBrakeProfileCtrl.UpdateValue((occupied->BrakeDelayFlag & bdelay_R) != 0 ? 2.0 : static_cast<float>(occupied->BrakeDelayFlag - 1), dsbSwitch);
}
if (ggBrakeProfileG.SubModel != nullptr)
{
ggBrakeProfileG.UpdateValue(occupied->BrakeDelayFlag == bdelay_G ? 1.0 : 0.0, dsbSwitch);
}
if (ggBrakeProfileR.SubModel != nullptr)
{
ggBrakeProfileR.UpdateValue((occupied->BrakeDelayFlag & bdelay_R) != 0 ? 1.0 : 0.0, dsbSwitch);
}
}
}
void TTrain::set_paired_open_motor_connectors_button(bool const State)
{
if (controlled->TrainType == train_type::et41 || controlled->TrainType == train_type::et42)
{
// crude implementation of the button affecting entire unit for multi-unit engines
// TODO: rework it into part of standard command propagation system
if (controlled->Couplers[VehicleEnd::front].Connected != nullptr && true == controlled->Couplers[VehicleEnd::front].CouplingFlag.HasAll(Coupling::permanent))
{
controlled->Couplers[VehicleEnd::front].Connected->StLinSwitchOff = State;
}
if (controlled->Couplers[VehicleEnd::rear].Connected != nullptr && true == controlled->Couplers[VehicleEnd::rear].CouplingFlag.HasAll(Coupling::permanent))
{
controlled->Couplers[VehicleEnd::rear].Connected->StLinSwitchOff = State;
}
}
}
// locates nearest vehicle belonging to the consist
TDynamicObject *TTrain::find_nearest_consist_vehicle(bool freefly, glm::vec3 pos) const
{
if (!freefly)
return DynamicObject;
auto couplerindex{-2}; // scan for vehicle, not any specific coupler
auto *vehicle{DynamicObject->ABuScanNearestObject(pos, DynamicObject->GetTrack(), 1, 1500, couplerindex)};
if (vehicle == nullptr)
vehicle = DynamicObject->ABuScanNearestObject(pos, DynamicObject->GetTrack(), -1, 1500, couplerindex);
// TBD, TODO: perform owner test for the located vehicle
return vehicle;
}
// cab movement update, fixed step part
void TTrain::UpdateCab()
{
// Ra: przesiadka, jeśli AI zmieniło kabinę (a człon?)...
if (DynamicObject->Mechanik // może nie być?
&& DynamicObject->Mechanik->AIControllFlag && iCabn != ( // numer kabiny (-1: kabina B)
occupied->CabOccupied == -1 ? 2 : occupied->CabOccupied))
{
InitializeCab(occupied->CabOccupied, occupied->TypeName + ".mmd");
}
iCabn = occupied->CabOccupied == -1 ? 2 : occupied->CabOccupied;
}
void TTrain::add_distance(double const Distance)
{
auto const meterenabled{m_distancecounter >= 0 && (occupied->Power24vIsAvailable || occupied->Power110vIsAvailable)};
if (true == meterenabled)
{
m_distancecounter += static_cast<float>(Distance * Occupied()->CabOccupied);
}
else
{
m_distancecounter = -1.f;
}
}
bool TTrain::CabChange(int iDirection)
{ // McZapkie-090902: zmiana kabiny 1->0->2 i z powrotem
if (DynamicObject->Mechanik == nullptr || true == DynamicObject->Mechanik->AIControllFlag)
{
// jeśli prowadzi AI albo jest w innym członie
// jak AI prowadzi, to nie można mu mieszać
if (std::abs(occupied->CabOccupied + iDirection) > 1)
return false; // ewentualna zmiana pojazdu
occupied->CabOccupied += iDirection;
}
else
{ // jeśli pojazd prowadzony ręcznie albo wcale (wagon)
occupied->CabDeactivisationAuto();
if (occupied->ChangeCab(iDirection) && InitializeCab(occupied->CabOccupied, occupied->TypeName + ".mmd"))
{
// zmiana kabiny w ramach tego samego pojazdu
occupied->CabActivisationAuto(); // załączenie rozrządu (wirtualne kabiny)
DynamicObject->Mechanik->DirectionChange();
return true; // udało się zmienić kabinę
}
// aktywizacja poprzedniej, bo jeszcze nie wiadomo, czy jakiś pojazd jest
occupied->CabActivisationAuto();
}
return false; // ewentualna zmiana pojazdu
}
void TTrain::DynamicSet(TDynamicObject *d)
{ // taka proteza: chcę podłączyć
// kabinę EN57 bezpośrednio z
// silnikowym, aby nie robić tego
// przez ukrotnienie
// drugi silnikowy i tak musi być ukrotniony, podobnie jak kolejna jednostka
// problem się robi ze światłami, które będą zapalane w silnikowym, ale muszą
// świecić się w rozrządczych
// dla EZT światła czołowe będą "zapalane w silnikowym", ale widziane z
// rozrządczych
// również wczytywanie MMD powinno dotyczyć aktualnego członu
// problematyczna może być kwestia wybranej kabiny (w silnikowym...)
// jeśli silnikowy będzie zapięty odwrotnie (tzn. -1), to i tak powinno
// jeździć dobrze
// również hamowanie wykonuje się zaworem w członie, a nie w silnikowym...
DynamicObject = d; // jedyne miejsce zmiany
controlled = d ? DynamicObject->physics : nullptr; // albo silnikowy w EZT
occupied = controlled;
if (DynamicObject == nullptr)
{
return;
}
controlled = DynamicObject->FindPowered()->physics;
secondUnit = nullptr; // gdyby się nic nie znalazło
if (occupied->Power > 1.0) // dwuczłonowe lub ukrotnienia, żeby nie szukać każdorazowo
{
if (occupied->Couplers[1].Connected != nullptr && occupied->Couplers[1].AllowedFlag.HasAny(Coupling::control))
{ // gdy jest człon od sprzęgu 1, a sprzęg łączony
// warsztatowo (powiedzmy)
if (occupied->Couplers[1].Connected->Power > 1.0) // ten drugi ma moc
secondUnit = occupied->Couplers[1].Connected; // wskaźnik na drugiego
}
else if (occupied->Couplers[0].Connected != nullptr && occupied->Couplers[0].AllowedFlag.HasAny(Coupling::control))
{ // gdy jest człon od sprzęgu 0, a sprzęg łączony
// warsztatowo (powiedzmy)
if (occupied->Couplers[0].Connected->Power > 1.0) // ale ten drugi ma moc
secondUnit = occupied->Couplers[0].Connected; // wskaźnik na drugiego
}
}
// cache nearest unit equipped with pantographs
{
auto *lookup{DynamicObject->FindPantographCarrier()};
// HACK: set pointer to existing vehicle to avoid error checking all over the place
pantographUnit = lookup != nullptr ? lookup->physics : controlled;
}
};
void TTrain::MoveToVehicle(TDynamicObject *target)
{
// > Ra: to nie może być tak robione, to zbytnia proteza jest
// indeed, too much hacks...
// TODO: cleanup
TTrain *target_train = simulation::Trains.find(target->name());
if (target_train)
{
// let's try to destroy this TTrain and move to already existing one
if (!Dynamic()->Mechanik || !Dynamic()->Mechanik->AIControllFlag)
{
// tylko jeśli ręcznie prowadzony
// jeśli prowadzi AI, to mu nie robimy dywersji!
Occupied()->CabDeactivisation();
Occupied()->CabOccupied = 0;
Occupied()->BrakeLevelSet(Occupied()->Handle->GetPos(bh_NP)); // rozwala sterowanie hamulcem GF 04-2016
Occupied()->MainCtrlPos = Occupied()->MainCtrlNoPowerPos();
Occupied()->ScndCtrlPos = 0;
Dynamic()->MechInside = false;
Dynamic()->Controller = AIdriver;
Dynamic()->bDisplayCab = false;
Dynamic()->ABuSetModelShake({});
if (Dynamic()->Mechanik)
Dynamic()->Mechanik->MoveTo(target);
target_train->Occupied()->LimPipePress = target_train->Occupied()->PipePress;
target_train->Occupied()->CabActivisationAuto(true); // załączenie rozrządu (wirtualne kabiny)
target_train->Dynamic()->MechInside = true;
if (target_train->Dynamic()->Mechanik)
{
target_train->Dynamic()->Controller = target_train->Dynamic()->Mechanik->AIControllFlag;
target_train->Dynamic()->Mechanik->DirectionChange();
}
else
{
target_train->Dynamic()->Controller = Humandriver;
}
}
else
{
target_train->Dynamic()->bDisplayCab = false;
target_train->Dynamic()->ABuSetModelShake({});
}
target_train->Dynamic()->ABuSetModelShake({}); // zerowanie przesunięcia przed powrotem?
// potentially move player
if (simulation::Train == this)
{
simulation::Train = target_train;
// our local driver may potentially be in external view mode, in which case we shouldn't activate cab visualization
target_train->Dynamic()->bDisplayCab |= !FreeFlyModeFlag;
}
// delete this TTrain
pending_delete = true;
}
else
{
// move this TTrain to other dynamic
// remove TTrain from global list, we're going to change dynamic anyway
simulation::Trains.detach(Dynamic()->name());
if (!Dynamic()->Mechanik || !Dynamic()->Mechanik->AIControllFlag)
{
// tylko jeśli ręcznie prowadzony
// jeśli prowadzi AI, to mu nie robimy dywersji!
Occupied()->CabDeactivisation();
Occupied()->CabOccupied = 0;
Occupied()->BrakeLevelSet(Occupied()->Handle->GetPos(bh_NP)); // rozwala sterowanie hamulcem GF 04-2016
Occupied()->MainCtrlPos = Occupied()->MainCtrlNoPowerPos();
Occupied()->ScndCtrlPos = 0;
Dynamic()->MechInside = false;
Dynamic()->Controller = AIdriver;
Dynamic()->bDisplayCab = false;
Dynamic()->ABuSetModelShake({});
if (Dynamic()->Mechanik)
Dynamic()->Mechanik->MoveTo(target);
DynamicSet(target);
Dynamic()->MechInside = true;
if (Dynamic()->Mechanik)
{
Dynamic()->Controller = Dynamic()->Mechanik->AIControllFlag;
Dynamic()->Mechanik->DirectionChange();
}
else
{
Dynamic()->Controller = Humandriver;
}
Occupied()->LimPipePress = Occupied()->PipePress;
Occupied()->CabActivisationAuto(true); // załączenie rozrządu (wirtualne kabiny)
}
else
{
Dynamic()->bDisplayCab = false;
Dynamic()->ABuSetModelShake({});
DynamicSet(target);
}
{
auto const filename{Occupied()->TypeName + ".mmd"};
LoadMMediaFile(filename);
InitializeCab(Occupied()->CabActive, filename);
}
Dynamic()->ABuSetModelShake({}); // zerowanie przesunięcia przed powrotem?
if (simulation::Train == this)
{
// our local driver may potentially be in external view mode, in which case we shouldn't activate cab visualization
Dynamic()->bDisplayCab |= !FreeFlyModeFlag;
}
// add it back with updated dynamic name
simulation::Trains.insert(this);
}
}
// checks whether specified point is within boundaries of the active cab
bool TTrain::point_inside(glm::dvec3 const Point) const
{
return Point.x >= Cabine[iCabn].CabPos1.x && Point.x <= Cabine[iCabn].CabPos2.x && Point.y >= Cabine[iCabn].CabPos1.y + 0.5 && Point.y <= Cabine[iCabn].CabPos2.y + 1.8 &&
Point.z >= Cabine[iCabn].CabPos1.z && Point.z <= Cabine[iCabn].CabPos2.z;
}
glm::dvec3 TTrain::clamp_inside(glm::dvec3 const &Point) const
{
if (DebugModeFlag)
{
return Point;
}
// the bounds come from the .mmd as written, and some cabs have them reversed (the B cars of SA134 and 21xM,
// ET40, 754...); std::clamp is undefined for those and on MSVC makes the camera jump between the two bounds
// every frame, as its result is fed back here. safe_clamp keeps the camera on the upper bound, as 26.05 did
return {safe_clamp(Point.x, (double)Cabine[iCabn].CabPos1.x, (double)Cabine[iCabn].CabPos2.x), safe_clamp(Point.y, (double)Cabine[iCabn].CabPos1.y + 0.5, (double)Cabine[iCabn].CabPos2.y + 1.8),
safe_clamp(Point.z, (double)Cabine[iCabn].CabPos1.z, (double)Cabine[iCabn].CabPos2.z)};
}
void TTrain::radio_message(sound_source *Message, int const Channel)
{
if (auto const soundrange{Message->range()}; soundrange > 0 && glm::length2(Message->location() - glm::dvec3{DynamicObject->GetPosition()}) > sq(soundrange))
{
// skip message playback if the receiver is outside of the emitter's range
return;
}
// NOTE: we initiate playback of all sounds in range, in case the user switches on the radio or tunes to the right channel mid-play
m_radiomessages.emplace_back(Channel, std::make_shared<sound_source>(m_radiosound));
// assign sound to the template and play it
auto &message = *m_radiomessages.back().second.get();
auto const radioenabled{occupied->RadioPowered()};
auto const volume{true == radioenabled && Dynamic()->Mechanik != nullptr && Channel == RadioChannel() ? 1.0 : 0.0};
message.copy_sounds(*Message).gain(static_cast<float>(volume)).play();
}
uint16_t TTrain::id()
{
if (vid == 0)
{
vid = ++simulation::prev_train_id;
WriteLog("net: assigning id " + std::to_string(vid) + " to vehicle " + Dynamic()->name(), logtype::net);
}
return vid;
}
namespace
{
// updates trains in range [Start, End)
void update_trains(std::deque<TTrain *> const &Trains, std::size_t const Start, std::size_t const End, double const Dt)
{
for (std::size_t j = Start; j < End; ++j)
{
if (Trains[j])
Trains[j]->Update(Dt);
}
}
} // namespace
void train_table::updateAsync(double dt)
{
const int threads = std::max(1, Global.trainThreads);
const size_t total = m_items.size();
const size_t chunkSize = (total + threads - 1) / threads;
auto const instance{TSubModel::iInstance};
std::vector<std::jthread> workers;
std::vector<std::future<void>> results; // przekazują wyjątki z wątków do głównego wątku
workers.reserve(threads);
results.reserve(threads);
for (int i = 0; i < threads; ++i)
{
const size_t start = i * chunkSize;
const size_t end = std::min(start + chunkSize, total);
if (start >= end)
break; // brak więcej danych
// the instance whose animations the gauges set (TSubModel::iInstance, per thread since the walks of the vulkan renderer went on
// several threads) is the one the main thread set for the driven vehicle
std::packaged_task<void()> task([this, start, end, dt, instance]() {
TSubModel::iInstance = instance;
update_trains(m_items, start, end, dt);
});
results.emplace_back(task.get_future());
workers.emplace_back(std::move(task));
}
// Poczekaj aż wszystkie wątki skończą
for (auto &worker : workers)
worker.join();
for (auto &result : results)
result.get();
// Teraz kasowanie (tylko w głównym wątku)
for (TTrain *train : m_items)
{
if (!train)
continue;
if (train->pending_delete)
{
purge(train->Dynamic()->name());
if (simulation::Train == train)
simulation::Train = nullptr;
}
else if (simulation::Train != train && Global.network_servers.empty() && !Global.network_client)
{
purge(train->Dynamic()->name());
}
}
}
void train_table::update(double dt)
{
for (TTrain *train : m_items)
{
if (!train)
continue;
train->Update(dt);
if (train->pending_delete)
{
purge(train->Dynamic()->name());
if (simulation::Train == train)
simulation::Train = nullptr;
}
// for single-player destroy non-player trains
else if (simulation::Train != train && Global.network_servers.empty() && !Global.network_client)
{
purge(train->Dynamic()->name());
}
}
}
TTrain *train_table::find_id(std::uint16_t const Id) const
{
if (Id == 0)
{
return nullptr;
}
for (TTrain *train : m_items)
{
if (!train)
{
continue;
}
if (train->id() == Id)
{
return train;
}
}
return nullptr;
}