18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 18:47:06 +01:00
Files
maszyna/physics/vehiclecommand.cpp
T
Hirek e5639e9d48 refactor(physics): the commands between the vehicles are VehicleCommand, not text
The vehicles of a train passed their commands to each other as strings
(SendCtrlToNext("MainCtrl", ...)), and RunCommand() found out what to do by
comparing the text with 54 names. A misspelt name compiled and did nothing.

- physics/vehiclecommand.h/.cpp: enum class VehicleCommand, one value for
  each command a vehicle knows, plus none and unknown; ParseVehicleCommand()
  reads a text, VehicleCommandName() gives the name back
- SendCtrlToNext(), SetInternalCommand(), RunCommand() and its groups take
  VehicleCommand; Load= and UnLoad= carry the name of the load as the
  argument of RunCommand()
- the text is read once, where a command comes as text: PutCommand() (the
  events, Lua, the AI). TCommand keeps that text, as the AI reads the
  commands it knows itself (SetVelocity...) from it
- two commands sent but known to no vehicle stay as they were, passed on and
  not done: EpFuseSwitch and SandboxAutoAllow
- removed the unused GetExternalCommand(), CommandOut, ValueOut and
  CommandLast (written, never read)
- the names are the ones the sceneries and scripts have always used, compared
  exactly; new tests check that every command reads back from its name

No change in behaviour: 1144/1144 tests (2 new), golden traces exact against
staging 62f7a29b.
2026-11-27 20:40:13 +01:00

117 lines
3.7 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 "physics/vehiclecommand.h"
#include <algorithm>
#include <array>
#include <utility>
#include "utilities/utilities.h"
namespace
{
using enum VehicleCommand;
// the names of the commands, as the scenery files write them
constexpr std::array<std::pair<VehicleCommand, std::string_view>, 59> commandNames{{
{mainController, "MainCtrl"},
{secondaryController, "ScndCtrl"},
{integratedController, "EIMIC"},
{speedControl, "SpeedCntrl"},
{speedControlParking, "SpeedCtrlUnit.Parking"},
{direction, "Direction"},
{autoRelaySwitch, "AutoRelaySwitch"},
{maxCurrentSwitch, "MaxCurrentSwitch"},
{minCurrentSwitch, "MinCurrentSwitch"},
{relayReset, "RelayReset"},
{cabActivation, "CabActivisation"},
{cabSignal, "CabSignal"},
{departureSignal, "DepartureSignal"},
{tractionMode, "TractionMode"},
{brake, "Brake"},
{emergencyBrake, "Emergency_brake"},
{brakeDelay, "BrakeDelay"},
{brakeReleaser, "BrakeReleaser"},
{springBrakeActivate, "SpringBrakeActivate"},
{epFuseSwitch, "EpFuseSwitch"},
{sandbox, "Sandbox"},
{sandboxAutoAllow, "SandboxAutoAllow"},
{compressorSwitch, "CompressorSwitch"},
{compressorPreset, "CompressorPreset"},
{converterSwitch, "ConverterSwitch"},
{batterySwitch, "BatterySwitch"},
{mainSwitch, "MainSwitch"},
{heatingSwitch, "HeatingSwitch"},
{gensetSwitch, "GensetSwitch"},
{pantographValve, "PantValve"},
{pantographsValve, "PantsValve"},
{pantographsAllDown, "PantAllDown"},
{compartmentLightsSwitch, "CompartmentLightsSwitch"},
{compartmentLightsSwitchOff, "CompartmentLightsSwitchOff"},
{waterPumpBreakerSwitch, "WaterPumpBreakerSwitch"},
{waterPumpSwitch, "WaterPumpSwitch"},
{waterPumpSwitchOff, "WaterPumpSwitchOff"},
{waterHeaterBreakerSwitch, "WaterHeaterBreakerSwitch"},
{waterHeaterSwitch, "WaterHeaterSwitch"},
{waterCircuitsLinkSwitch, "WaterCircuitsLinkSwitch"},
{fuelPumpSwitch, "FuelPumpSwitch"},
{fuelPumpSwitchOff, "FuelPumpSwitchOff"},
{oilPumpSwitch, "OilPumpSwitch"},
{oilPumpSwitchOff, "OilPumpSwitchOff"},
{motorBlowersFrontSwitch, "MotorBlowersFrontSwitch"},
{motorBlowersFrontSwitchOff, "MotorBlowersFrontSwitchOff"},
{motorBlowersRearSwitch, "MotorBlowersRearSwitch"},
{motorBlowersRearSwitchOff, "MotorBlowersRearSwitchOff"},
{doorOpen, "DoorOpen"},
{doorClose, "DoorClose"},
{doorLock, "DoorLock"},
{doorMode, "DoorMode"},
{doorPermit, "DoorPermit"},
{doorStep, "DoorStep"},
{setDamage, "SetDamage"},
{load, "Load="},
{unload, "UnLoad="},
{none, ""},
{unknown, ""},
}};
} // namespace
ParsedVehicleCommand ParseVehicleCommand(std::string const &Text)
{
if (Text.empty())
{
return {none, {}};
}
// Load= and UnLoad= begin the text, the name of the load follows
if (Text.starts_with("Load="))
{
return {load, extract_value("Load", Text)};
}
if (Text.starts_with("UnLoad="))
{
return {unload, extract_value("UnLoad", Text)};
}
auto const named{std::ranges::find(commandNames, std::string_view{Text}, &std::pair<VehicleCommand, std::string_view>::second)};
if (named == commandNames.end() || named->first == load || named->first == unload)
{
return {unknown, {}};
}
return {named->first, {}};
}
std::string_view VehicleCommandName(VehicleCommand const Command)
{
auto const named{std::ranges::find(commandNames, Command, &std::pair<VehicleCommand, std::string_view>::first)};
return named != commandNames.end() ? named->second : std::string_view{};
}