18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 00:45:29 +01:00
Files
maszyna/vehicle/enginestart.cpp
T
Hirek 84f1aa2515 refactor(physics): the controllers and the switches of the cab are private to the vehicle
The positions of the master and the secondary controller, of the analog
controller and of the lights selector, the occupied cab, the horns, the radio
and its fuse, the radio-stop, the wipers' switch, the dimmer, the mirrors'
button, the end-of-train signalling and the forced resistor fans: fourteen
fields the cab, the AI, the scenery and the tests wrote straight into
physics::Vehicle are private now. The rest of the simulator reads them through
queries named as the fields were (MainCtrlPos(), Radio(), ...) and changes them
through operations (SetMainCtrlPos(), SetRadio(), ...), so every place that
moves a controller past its own stepping (IncMainCtrl()) is named and can be
found; the compiler keeps anything else out. Inside the vehicle they keep
their meaning under camelCase names. The queries return const references, as
the gauges of the cab bind to them.

The .fiz keys are untouched.

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

110 lines
3.3 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/.
*/
#include "stdafx.h"
#include "vehicle/enginestart.h"
#include "physics/vehicle.h"
#include "utilities/utilities.h"
namespace engine_start {
namespace {
// whether the vehicle has an engine the cab can start, the same test the tachometers of the vehicles in multiple make
bool has_engine(physics::Vehicle const &Vehicle) {
return Vehicle.Power > 0.01;
}
// walks from Start through its coupler Side along the control cable, and returns the Number-th vehicle with an engine
// other than Skipped it meets; Number is lowered by the ones it met when it doesn't get that far
physics::Vehicle *find_along(physics::Vehicle const &Start, int const Side, physics::Vehicle const &Skipped, int &Number) {
auto const *vehicle{&Start};
auto side{Side};
// a train can't loop back on itself, the limit only guards against a broken chain of couplers
for (auto steps = 0; steps < 1000; ++steps) {
auto const &coupler{vehicle->Couplers[side]};
if (coupler.Connected == nullptr || false == coupler.CouplingFlag.HasAll(Coupling::control)) {
return nullptr;
}
// onwards through the other end of the next vehicle, whichever way round it stands
side = 1 - coupler.ConnectedNr;
vehicle = coupler.Connected;
if (vehicle == &Skipped || false == has_engine(*vehicle)) {
continue;
}
--Number;
if (Number == 0) {
return coupler.Connected;
}
}
return nullptr;
}
} // namespace
physics::Vehicle *engine(physics::Vehicle const &Occupied, physics::Vehicle &Controlled, int Number) {
if (Number < 1) {
return nullptr;
}
if (Number == 1) {
return &Controlled;
}
// the cab's own vehicle is the first one
auto others{Number - 1};
// behind the cab is the rear end of a vehicle driven from its front cab, and the other way round
int const behind{NumberOf(Occupied.CabOccupied() < 0 ? VehicleEnd::front : VehicleEnd::rear)};
if (auto *found{find_along(Occupied, behind, Controlled, others)}; found != nullptr) {
return found;
}
return find_along(Occupied, 1 - behind, Controlled, others);
}
void remote_switch::update(physics::Vehicle *Engine, bool const Closeheld, bool const Openheld, double const Deltatime, bool const Closesonrelease) {
if (Engine != m_engine) {
// another vehicle under the switch, after a change in the train
m_engine = Engine;
m_heldtime = 0.0;
m_ready = false;
}
if (Engine == nullptr) {
return;
}
if (Openheld || is_running(*Engine)) {
// the switch opening the circuit is held down, or there's nothing left to start
m_heldtime = 0.0;
m_ready = false;
return;
}
if (Closeheld && Engine->MainSwitchCheck()) {
m_heldtime += Deltatime;
}
if (m_heldtime > Engine->InitialCtrlDelay) {
m_ready = true;
}
if (m_ready && false == (Closesonrelease && Closeheld)) {
Engine->MainSwitch(true, range_t::local);
m_ready = false;
m_heldtime = 0.0;
}
if (false == Closeheld) {
m_heldtime = 0.0;
}
}
bool remote_switch::open(physics::Vehicle &Engine) {
return Engine.MainSwitch(false, range_t::local);
}
bool remote_switch::is_running(physics::Vehicle const &Engine) {
return Engine.lineBreaker.IsClosed() || Engine.diesel.startupRequested;
}
} // namespace engine_start