18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 09:36:48 +01:00
Files
maszyna/vehicle/cab/cabcommands.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

651 lines
18 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 commands of the cab itself: the reverser, the battery, its activation, changing it
#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"
void TTrain::OnCommand_wiperswitchincrease(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
Train->occupied->wiperSwitchPos++;
if (Train->occupied->wiperSwitchPos > Train->occupied->WiperListSize - 1)
Train->occupied->wiperSwitchPos = Train->occupied->WiperListSize - 1;
// Visual feedback
Train->ggWiperSw.UpdateValue(static_cast<float>(Train->occupied->wiperSwitchPos), Train->dsbSwitch);
}
}
void TTrain::OnCommand_wiperswitchdecrease(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
Train->occupied->wiperSwitchPos--;
if (Train->occupied->wiperSwitchPos < 0)
Train->occupied->wiperSwitchPos = 0;
// visual feedback
Train->ggWiperSw.UpdateValue(static_cast<float>(Train->occupied->wiperSwitchPos), Train->dsbSwitch);
}
}
void TTrain::OnCommand_reverserincrease(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
// HACK: master controller position isn't set in occupied vehicle in E(D)MUs
// so we do a manual check in relevant vehicle here
if (false == Train->controlled->EIMDirectionChangeAllow())
{
return;
}
// aktualizacja skrajnych pojazdów w składzie
if (Train->occupied->DirectionForward() && Train->occupied->DirActive && Train->DynamicObject->Mechanik)
{
Train->DynamicObject->Mechanik->DirectionChange();
}
}
}
void TTrain::OnCommand_reverserdecrease(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
// HACK: master controller position isn't set in occupied vehicle in E(D)MUs
// so we do a manual check in relevant vehicle here
if (false == Train->controlled->EIMDirectionChangeAllow())
{
return;
}
// aktualizacja skrajnych pojazdów w składzie
if (Train->occupied->DirectionBackward() && Train->occupied->DirActive && Train->DynamicObject->Mechanik)
{
Train->DynamicObject->Mechanik->DirectionChange();
}
}
}
void TTrain::OnCommand_reverserforwardhigh(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
// HACK: master controller position isn't set in occupied vehicle in E(D)MUs
// so we do a manual check in relevant vehicle here
if (false == Train->controlled->EIMDirectionChangeAllow())
{
return;
}
// HACK: try to move the reverser one position back, in case it's set to "high forward"
OnCommand_reverserdecrease(Train, Command);
if (Train->occupied->DirActive < 1)
{
while (Train->occupied->DirActive < 1 && true == Train->occupied->DirectionForward())
{
// all work is done in the header
}
// aktualizacja skrajnych pojazdów w składzie
if (Train->occupied->DirActive == 1 && Train->DynamicObject->Mechanik)
{
Train->DynamicObject->Mechanik->DirectionChange();
}
}
OnCommand_reverserincrease(Train, Command);
}
}
void TTrain::OnCommand_reverserforward(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
// HACK: master controller position isn't set in occupied vehicle in E(D)MUs
// so we do a manual check in relevant vehicle here
if (false == Train->controlled->EIMDirectionChangeAllow())
{
return;
}
// HACK: try to move the reverser one position back, in case it's set to "high forward"
// OnCommand_reverserdecrease( Train, Command );
// visual feedback
Train->ggDirForwardButton.UpdateValue(1.0, Train->dsbSwitch);
if (Train->occupied->DirActive == 0)
{
while (Train->occupied->DirActive < 1 && true == Train->occupied->DirectionForward())
{
// all work is done in the header
}
// aktualizacja skrajnych pojazdów w składzie
if (Train->occupied->DirActive == 1 && Train->DynamicObject->Mechanik)
{
Train->DynamicObject->Mechanik->DirectionChange();
}
}
}
else if (Command.action == GLFW_RELEASE)
{
// release
// visual feedback
Train->ggDirForwardButton.UpdateValue(0.0, Train->dsbSwitch);
}
}
void TTrain::OnCommand_reverserneutral(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
// HACK: master controller position isn't set in occupied vehicle in E(D)MUs
// so we do a manual check in relevant vehicle here
if (false == Train->controlled->EIMDirectionChangeAllow())
{
return;
}
// visual feedback
Train->ggDirNeutralButton.UpdateValue(1.0, Train->dsbSwitch);
while (Train->occupied->DirActive < 0 && true == Train->occupied->DirectionForward())
{
// all work is done in the header
}
while (Train->occupied->DirActive > 0 && true == Train->occupied->DirectionBackward())
{
// all work is done in the header
}
}
else if (Command.action == GLFW_RELEASE)
{
// release
// visual feedback
Train->ggDirNeutralButton.UpdateValue(0.0, Train->dsbSwitch);
}
}
void TTrain::OnCommand_reverserbackward(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
// HACK: master controller position isn't set in occupied vehicle in E(D)MUs
// so we do a manual check in relevant vehicle here
if (false == Train->controlled->EIMDirectionChangeAllow())
{
return;
}
Train->ggDirBackwardButton.UpdateValue(1.0, Train->dsbSwitch);
if (Train->occupied->DirActive == 0)
{
while (Train->occupied->DirActive > -1 && true == Train->occupied->DirectionBackward())
{
// all work is done in the header
}
// aktualizacja skrajnych pojazdów w składzie
if (Train->occupied->DirActive == -1 && Train->DynamicObject->Mechanik)
{
Train->DynamicObject->Mechanik->DirectionChange();
}
}
}
else if (Command.action == GLFW_RELEASE)
{
// release
// visual feedback
Train->ggDirBackwardButton.UpdateValue(0.0, Train->dsbSwitch);
}
}
void TTrain::OnCommand_alerteracknowledge(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
// visual feedback
Train->ggSecurityResetButton.UpdateValue(1.0, Train->dsbSwitch);
if (Train->occupied->TrainType == train_type::emu || Train->occupied->DirActive != 0)
Train->occupied->SecuritySystem.acknowledge_press();
}
else if (Command.action == GLFW_RELEASE)
{
// visual feedback
Train->ggSecurityResetButton.UpdateValue(0.0);
if (Train->occupied->TrainType == train_type::emu || Train->occupied->DirActive != 0)
Train->occupied->SecuritySystem.acknowledge_release();
}
}
void TTrain::OnCommand_cabsignalacknowledge(TTrain *Train, command_data const &Command)
{
// TODO: visual feedback
if (Command.action == GLFW_PRESS)
{
if (Train->occupied->SecuritySystem.has_separate_acknowledge())
{
Train->occupied->SecuritySystem.cabsignal_reset();
Train->ggSHPResetButton.UpdateValue(1.0, Train->dsbSwitch);
}
}
else if (Command.action == GLFW_RELEASE)
{
Train->ggSHPResetButton.UpdateValue(0.0);
}
}
void TTrain::OnCommand_batterytoggle(TTrain *Train, command_data const &Command)
{
if (Train->allowBatteryToggle || Command.action != GLFW_REPEAT)
{
// keep the switch from flipping back and forth if key is held down
if (false == Train->occupied->Power24vIsAvailable)
{
// turn on
OnCommand_batteryenable(Train, Command);
}
else
{
// turn off
OnCommand_batterydisable(Train, Command);
}
}
}
void TTrain::OnCommand_batteryenable(TTrain *Train, command_data const &Command)
{
if (!Train->occupied->isBatteryButtonImpulse)
{ // regular button behavior
if (Command.action == GLFW_PRESS)
{
// visual feedback
Train->ggBatteryButton.UpdateValue(1.0f, Train->dsbSwitch);
Train->ggBatteryOnButton.UpdateValue(1.0f, Train->dsbSwitch);
Train->occupied->BatterySwitch(true);
Train->allowBatteryToggle = false;
// side-effects
if (Train->occupied->LightsPosNo > 0)
{
Train->Dynamic()->SetLights();
}
}
else if (Command.action == GLFW_RELEASE)
{
if (Train->ggBatteryButton.type() == TGaugeType::push)
{
// return the switch to neutral position
Train->ggBatteryButton.UpdateValue(0.5f);
}
Train->ggBatteryOnButton.UpdateValue(0.0f, Train->dsbSwitch);
Train->allowBatteryToggle = true;
}
}
else // impulse button behavior
{
if (Command.action == GLFW_PRESS)
{
if (Train->occupied->shouldHoldBatteryButton)
{
// jesli przycisk trzeba przytrzymac
Train->ggBatteryButton.UpdateValue(1.0f, Train->dsbSwitch);
Train->ggBatteryOnButton.UpdateValue(1.0f, Train->dsbSwitch);
Train->fBatteryTimer = Train->occupied->BatteryButtonHoldTime; // start timer
}
else
{
// jesli przycisk dziala od razu
Train->occupied->BatterySwitch(true);
Train->allowBatteryToggle = false;
// side-effects
if (Train->occupied->LightsPosNo > 0)
{
Train->Dynamic()->SetLights();
}
// visual feedback
Train->ggBatteryButton.UpdateValue(1.0f, Train->dsbSwitch);
Train->ggBatteryOnButton.UpdateValue(1.0f, Train->dsbSwitch);
}
}
else if (Command.action == GLFW_RELEASE)
{
// visual feedback
Train->ggBatteryButton.UpdateValue(0.0f, Train->dsbSwitch);
Train->ggBatteryOnButton.UpdateValue(0.0f, Train->dsbSwitch);
Train->fBatteryTimer = -1.f; //
Train->allowBatteryToggle = true;
Train->occupied->batterySwAlreadyFired = false;
}
else if (Command.action == GLFW_REPEAT && Train->occupied->shouldHoldBatteryButton)
{
// trzymamy przycisk
if (Train->fBatteryTimer <= 0.0 && Train->occupied->Battery == false && !Train->occupied->batterySwAlreadyFired)
{
Train->occupied->BatterySwitch(true);
Train->occupied->batterySwAlreadyFired = true;
// side-effects
if (Train->occupied->LightsPosNo > 0)
{
Train->Dynamic()->SetLights();
}
Train->allowBatteryToggle = false;
}
}
}
}
void TTrain::OnCommand_batterydisable(TTrain *Train, command_data const &Command)
{
if (!Train->occupied->isBatteryButtonImpulse)
{ // regular button behavior
if (Command.action == GLFW_PRESS)
{
// visual feedback
Train->ggBatteryButton.UpdateValue(0.0f, Train->dsbSwitch);
Train->ggBatteryOffButton.UpdateValue(1.0f, Train->dsbSwitch);
Train->occupied->BatterySwitch(false);
// side-effects
if (Train->occupied->LightsPosNo > 0)
{
Train->Dynamic()->SetLights();
}
}
else if (Command.action == GLFW_RELEASE)
{
if (Train->ggBatteryButton.type() == TGaugeType::push)
{
// return the switch to neutral position
Train->ggBatteryButton.UpdateValue(0.5f);
}
Train->ggBatteryOffButton.UpdateValue(0.0f, Train->dsbSwitch);
}
}
else // impulse button behavior
{
if (Command.action == GLFW_PRESS)
{
if (Train->occupied->shouldHoldBatteryButton)
{
// jesli przycisk trzeba przytrzymac
Train->ggBatteryButton.UpdateValue(1.0f, Train->dsbSwitch);
Train->ggBatteryOffButton.UpdateValue(1.0f, Train->dsbSwitch);
Train->fBatteryTimer = Train->occupied->BatteryButtonHoldTime; // start timer
}
else
{
// jesli przycisk dziala od razu
Train->occupied->BatterySwitch(false);
Train->allowBatteryToggle = false;
// side-effects
if (Train->occupied->LightsPosNo > 0)
{
Train->Dynamic()->SetLights();
}
// visual feedback
Train->ggBatteryButton.UpdateValue(1.0f, Train->dsbSwitch);
Train->ggBatteryOffButton.UpdateValue(1.0f, Train->dsbSwitch);
}
}
else if (Command.action == GLFW_RELEASE)
{
// visual feedback
Train->ggBatteryButton.UpdateValue(0.0f, Train->dsbSwitch);
Train->ggBatteryOffButton.UpdateValue(0.0f, Train->dsbSwitch);
Train->allowBatteryToggle = true;
Train->occupied->batterySwAlreadyFired = false;
}
else if (Command.action == GLFW_REPEAT && Train->occupied->shouldHoldBatteryButton)
{
// trzymamy przycisk
if (Train->fBatteryTimer <= 0.0 && Train->occupied->Battery == true && !Train->occupied->batterySwAlreadyFired)
{
Train->occupied->BatterySwitch(false);
Train->occupied->batterySwAlreadyFired = true;
// side-effects
if (Train->occupied->LightsPosNo > 0)
{
Train->Dynamic()->SetLights();
}
Train->allowBatteryToggle = false;
}
}
}
}
void TTrain::OnCommand_cabactivationtoggle(TTrain *Train, command_data const &Command)
{
if (Command.action != GLFW_REPEAT)
{
// keep the switch from flipping back and forth if key is held down
if (0 == Train->occupied->CabActive)
{
// turn on
OnCommand_cabactivationenable(Train, Command);
}
else
{
// turn off
OnCommand_cabactivationdisable(Train, Command);
}
}
}
void TTrain::OnCommand_cabactivationenable(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
// visual feedback
if (Train->ggCabActivationButton.type() == TGaugeType::push)
{
Train->ggCabActivationButton.UpdateValue(1.0f, Train->dsbSwitch);
}
Train->occupied->CabActivisation();
// side-effects
if (Train->occupied->LightsPosNo > 0)
{
Train->Dynamic()->SetLights();
}
}
else if (Command.action == GLFW_RELEASE && Train->ggCabActivationButton.type() == TGaugeType::push)
{
// return the switch to neutral position
Train->ggCabActivationButton.UpdateValue(0.5f);
}
}
void TTrain::OnCommand_cabactivationdisable(TTrain *Train, command_data const &Command)
{
// TBD, TODO: ewentualnie zablokować z FIZ, np. w samochodach się nie odłącza akumulatora
if (Command.action == GLFW_PRESS)
{
// visual feedback
if (Train->ggCabActivationButton.type() == TGaugeType::push)
{
Train->ggCabActivationButton.UpdateValue(0.0f, Train->dsbSwitch);
}
Train->occupied->CabDeactivisation();
if (Train->occupied->LightsPosNo > 0 && Train->occupied->InactiveCabFlag & activation::redmarkers)
{
Train->Dynamic()->SetLights();
}
}
else if (Command.action == GLFW_RELEASE && Train->ggCabActivationButton.type() == TGaugeType::push)
{
// return the switch to neutral position
Train->ggCabActivationButton.UpdateValue(0.5f);
}
}
void TTrain::OnCommand_cabchangeforward(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
if (auto const movedirection{1}; false == Train->CabChange(movedirection))
{
auto const exitdirection{(movedirection > 0 ? VehicleEnd::front : VehicleEnd::rear)};
if (Train->occupied->Couplers[exitdirection].CouplingFlag.HasAll(Coupling::gangway))
{
// przejscie do nastepnego pojazdu
auto *targetvehicle = exitdirection == VehicleEnd::front ? Train->DynamicObject->PrevConnected() : Train->DynamicObject->NextConnected();
targetvehicle->physics->CabOccupied = Train->occupied->Neighbours[exitdirection].vehicle_end ? -1 : 1;
Train->MoveToVehicle(targetvehicle);
}
}
// HACK: match consist door permit state with the preset in the active cab
if (Train->ggDoorPermitPresetButton.SubModel != nullptr)
{
Train->occupied->ChangeDoorPermitPreset(0);
}
// HACK: update lights state
if (Train->occupied->LightsPosNo > 0)
{
Train->DynamicObject->SetLights();
}
}
}
void TTrain::OnCommand_cabchangebackward(TTrain *Train, command_data const &Command)
{
if (Command.action == GLFW_PRESS)
{
if (auto const movedirection{-1}; false == Train->CabChange(movedirection))
{
// current vehicle doesn't extend any farther in this direction, check if we there's one connected we can move to
auto const exitdirection{(movedirection > 0 ? VehicleEnd::front : VehicleEnd::rear)};
if (Train->occupied->Couplers[exitdirection].CouplingFlag.HasAll(Coupling::gangway))
{
// przejscie do nastepnego pojazdu
auto *targetvehicle = exitdirection == VehicleEnd::front ? Train->DynamicObject->PrevConnected() : Train->DynamicObject->NextConnected();
targetvehicle->physics->CabOccupied = Train->occupied->Neighbours[exitdirection].vehicle_end ? -1 : 1;
Train->MoveToVehicle(targetvehicle);
}
}
// HACK: match consist door permit state with the preset in the active cab
if (Train->ggDoorPermitPresetButton.SubModel != nullptr)
{
Train->occupied->ChangeDoorPermitPreset(0);
}
// HACK: update lights state
if (Train->occupied->LightsPosNo > 0)
{
Train->DynamicObject->SetLights();
}
}
}
void TTrain::OnCommand_vehiclemoveforwards(TTrain *Train, const command_data &Command)
{
if (Command.action == GLFW_RELEASE || !DebugModeFlag)
return;
Train->DynamicObject->move_set(100.0);
}
void TTrain::OnCommand_vehiclemovebackwards(TTrain *Train, const command_data &Command)
{
if (Command.action == GLFW_RELEASE || !DebugModeFlag)
return;
Train->DynamicObject->move_set(-100.0);
}
void TTrain::OnCommand_vehicleboost(TTrain *Train, const command_data &Command)
{
if (Command.action == GLFW_RELEASE || !DebugModeFlag)
return;
double boost = Command.param1 != 0.0 ? Command.param1 : 2.78;
if (Train->DynamicObject == nullptr)
{
return;
}
auto *vehicle{Train->DynamicObject};
while (vehicle)
{
vehicle->physics->V += vehicle->DirectionGet() * boost;
vehicle = vehicle->Next(); // pozostałe też
}
vehicle = Train->DynamicObject->Prev();
while (vehicle)
{
vehicle->physics->V += vehicle->DirectionGet() * boost;
vehicle = vehicle->Prev(); // w drugą stronę też
}
}