18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 19:57:06 +01:00
Files
maszyna/physics/vehiclecommand.cpp
T
Hirek 6aca6bc4f6 fix(review): an empty Load= no longer throws, and the stale notes of the review of #531
From the review of #531:
- Load= and UnLoad= without the name of a load threw std::out_of_range as
  soon as ParseVehicleCommand() read them: extract_value() reads past the end
  of the text when nothing but spaces follows the =. Before this branch the
  same exception came later, in the RunCommand() of a vehicle standing at the
  ramp. ParseVehicleCommand() gives an empty name now, which loads and
  unloads nothing (AssignLoad("") and LoadingDone() with no name, as for an
  empty vehicle). extract_value() itself is left as it was: it has hundreds
  of callers and test_utilities.cpp records its exception at the end of the
  text. New checks in test_vehicle_command.cpp.
- compression_at() is an inline function of physics/components/internal.h,
  as square() is, rather than a global function of external linkage defined
  in collisions.cpp; its body is the same
- the comment of train_type::sn61 says what the AI does for it (the master
  controller up to a position with torque after setting the direction)
  rather than that nothing handles it
- tests/unit/COVERAGE.md names the files as they are now (physics/components/,
  vehicle/dynamic/lifecycle.cpp) instead of physics/physics.cpp, a name the
  renaming script left there

The naming of AGENTS.md, which the review asked about, stays as it is.
2026-11-29 04:13:17 +01:00

128 lines
4.1 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, ""},
}};
// the name of the load after Load= or UnLoad= (key) at the start of the text, empty when nothing follows the = but
// spaces: extract_value() would throw there, reading past the end of the text
std::string loadName(std::string_view const key, std::string_view const text)
{
if (text.find_first_not_of(' ', key.size() + 1) == std::string_view::npos)
{
return {};
}
return extract_value(std::string{key}, std::string{text});
}
} // 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, loadName("Load", Text)};
}
if (Text.starts_with("UnLoad="))
{
return {unload, loadName("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{};
}