18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 07:07:05 +01:00

refactor(ai): the orders of the AI are DriverOrder and DriverOrders, not the bits of TOrders

The orders of the AI driver (TOrders) were unscoped bits, summed (an order
carries Change_direction on top of Shunt), masked, and compared as numbers
(order < Obey_train, order >= Shunt).

- vehicle/driverorders.h: enum class DriverOrder (waitForOrders,
  prepareEngine, releaseEngine, changeDirection, connect, disconnect, shunt,
  looseShunt, obeyTrain, bank, jumpToFirstOrder) and DriverOrders, an
  EnumSet of them. A small header, so brakesetting.h names the orders without
  Driver.h.
- the comparisons of the numbers are the sets they meant: the orders from
  shunt up are movingOrders, those from obeyTrain up trainOrders;
  SingleOrder() gives the order to a switch, where a sum of several matches
  no case, as it did
- the texts of the scenery commands (Shunt, Obey_train...) and of the
  translations are unchanged; only the C++ names changed

No change in behaviour: 1144/1144 tests, golden traces exact against staging
62f7a29b.
This commit is contained in:
2026-11-28 13:43:45 +01:00
parent 54d3a1040a
commit b35a6129bc
10 changed files with 311 additions and 276 deletions
+2 -2
View File
@@ -605,7 +605,7 @@ TController::cue_action( driver_hint const Action, float const Actionparameter )
hint(
Action,
[this](float const /*Parameter*/) {
return std::abs(mvControlling->Ft) > 30.0 || (OrderCurrentGet() & Disconnect) == 0; } );
return std::abs(mvControlling->Ft) > 30.0 || false == OrderCurrentGet().HasAny(DriverOrder::disconnect); } );
break;
}
@@ -836,7 +836,7 @@ TController::cue_action( driver_hint const Action, float const Actionparameter )
hint(
Action,
[this](float const /*Parameter*/) {
return IsConsistBraked /*|| ( ( OrderCurrentGet() & Disconnect ) == 0 ) */; } );
return IsConsistBraked /*|| ( false == OrderCurrentGet().HasAny(DriverOrder::disconnect) ) */; } );
break;
}
case driver_hint::brakingforcedecrease: {
+2 -2
View File
@@ -436,8 +436,8 @@ void blackbox_recorder::write_cab() {
sample.lateral_acceleration = static_cast<float>(occupied->AccN);
if (owner != nullptr && owner->is_train()) {
auto const order{owner->order_current()};
sample.movement = (order & Obey_train) != 0 ? blackbox::movements::train :
(order & (Shunt | Loose_shunt | Connect)) != 0 ? blackbox::movements::shunting : blackbox::movements::other;
sample.movement = order.HasAny(DriverOrder::obeyTrain) ? blackbox::movements::train :
order.HasAny(DriverOrder::shunt | DriverOrder::looseShunt | DriverOrder::connect) ? blackbox::movements::shunting : blackbox::movements::other;
sample.light_hint_head = static_cast<std::int16_t>(owner->light_hint(end::front));
sample.light_hint_tail = static_cast<std::int16_t>(owner->light_hint(end::rear));
}
+25 -25
View File
@@ -456,45 +456,45 @@ TEST_CASE("brake setting: a train creeping to a stop counts as standing, one on
}
TEST_CASE("brake setting: a bank engine's run counts as a train, shunting doesn't") {
CHECK(brake_setting::runs_as_train(Obey_train));
CHECK(brake_setting::runs_as_train(Bank));
CHECK(brake_setting::runs_as_train(Obey_train | Connect));
CHECK(false == brake_setting::runs_as_train(Shunt));
CHECK(false == brake_setting::runs_as_train(Loose_shunt));
CHECK(false == brake_setting::runs_as_train(Connect));
CHECK(false == brake_setting::runs_as_train(Wait_for_orders));
CHECK(brake_setting::runs_as_train(DriverOrder::obeyTrain));
CHECK(brake_setting::runs_as_train(DriverOrder::bank));
CHECK(brake_setting::runs_as_train(DriverOrder::obeyTrain | DriverOrder::connect));
CHECK(false == brake_setting::runs_as_train(DriverOrder::shunt));
CHECK(false == brake_setting::runs_as_train(DriverOrder::looseShunt));
CHECK(false == brake_setting::runs_as_train(DriverOrder::connect));
CHECK(false == brake_setting::runs_as_train(DriverOrder::waitForOrders));
// a heavy consist on G banked: the wagon without G has its brake cut out, as in a train
test::vehicle car{passenger_car_definition()};
charge_and_apply(car);
CHECK(inspect_heavy_train(car, brake_setting::runs_as_train(Bank)) == brake_setting::cut_out);
CHECK(inspect_heavy_train(car, brake_setting::runs_as_train(DriverOrder::bank)) == brake_setting::cut_out);
CHECK(brake_setting::is_cut_out(car.mover()));
// shunted: it's back on P
CHECK(inspect_heavy_train(car, brake_setting::runs_as_train(Shunt)) == P);
CHECK(inspect_heavy_train(car, brake_setting::runs_as_train(DriverOrder::shunt)) == P);
CHECK(false == brake_setting::is_cut_out(car.mover()));
}
TEST_CASE("brake setting: the AI looks at its train under the orders it drives it under, Connect included") {
CHECK(brake_setting::inspects_train(Shunt, false));
CHECK(brake_setting::inspects_train(Loose_shunt, false));
CHECK(brake_setting::inspects_train(Obey_train, false));
CHECK(brake_setting::inspects_train(Bank, false));
CHECK(brake_setting::inspects_train(DriverOrder::shunt, false));
CHECK(brake_setting::inspects_train(DriverOrder::looseShunt, false));
CHECK(brake_setting::inspects_train(DriverOrder::obeyTrain, false));
CHECK(brake_setting::inspects_train(DriverOrder::bank, false));
// the run up to the wagons: a locomotive standing cold has its hand brake to release first
CHECK(brake_setting::inspects_train(Connect, false));
CHECK(brake_setting::inspects_train(Connect | Change_direction, false));
CHECK(brake_setting::inspects_train(DriverOrder::connect, false));
CHECK(brake_setting::inspects_train(DriverOrder::connect | DriverOrder::changeDirection, false));
// but not at the coupling: the wagons just coupled wait for the next order, which may leave some of them
CHECK(false == brake_setting::inspects_train(Connect, true));
CHECK(false == brake_setting::inspects_train(Connect | Change_direction, true));
CHECK(false == brake_setting::inspects_train(DriverOrder::connect, true));
CHECK(false == brake_setting::inspects_train(DriverOrder::connect | DriverOrder::changeDirection, true));
// a train run with a coupling on it is still a train run
CHECK(brake_setting::inspects_train(Obey_train | Connect, true));
CHECK(brake_setting::inspects_train(Shunt, true));
CHECK(brake_setting::inspects_train(DriverOrder::obeyTrain | DriverOrder::connect, true));
CHECK(brake_setting::inspects_train(DriverOrder::shunt, true));
// standing, starting or stopping the engine, turning round, uncoupling: nothing to look at
CHECK(false == brake_setting::inspects_train(Wait_for_orders, false));
CHECK(false == brake_setting::inspects_train(Prepare_engine, false));
CHECK(false == brake_setting::inspects_train(Release_engine, false));
CHECK(false == brake_setting::inspects_train(Change_direction, false));
CHECK(false == brake_setting::inspects_train(Disconnect, false));
CHECK(false == brake_setting::inspects_train(Jump_to_first_order, false));
CHECK(false == brake_setting::inspects_train(DriverOrder::waitForOrders, false));
CHECK(false == brake_setting::inspects_train(DriverOrder::prepareEngine, false));
CHECK(false == brake_setting::inspects_train(DriverOrder::releaseEngine, false));
CHECK(false == brake_setting::inspects_train(DriverOrder::changeDirection, false));
CHECK(false == brake_setting::inspects_train(DriverOrder::disconnect, false));
CHECK(false == brake_setting::inspects_train(DriverOrder::jumpToFirstOrder, false));
}
TEST_CASE("brake setting: R+Mg brakes as hard as R at speed, the rapid stage of an LSt kept") {
+201 -201
View File
File diff suppressed because it is too large Load Diff
+19 -35
View File
@@ -14,6 +14,7 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/Classes.h"
#include "utilities/utilities.h"
#include "physics/vehicletypes.h"
#include "vehicle/driverorders.h"
#include "audio/sound.h"
#include "vehicle/DynObj.h"
#include "world/mtable.h"
@@ -34,23 +35,6 @@ auto const EU07_AI_UPHILLRELEASETIME = 60.0; // [s] the longest an uphill start
auto const EU07_AI_SPEEDLIMITEXTENDSBEYONDSCANRANGE = 10000.0;
auto const EU07_AI_SANDEDSTARTVELOCITY = 15.0; // [km/h] a start that slipped keeps the sand on up to this speed
enum TOrders
{ // rozkazy dla AI
Wait_for_orders = 0, // czekanie na dostarczenie następnych rozkazów
// operacje tymczasowe
Prepare_engine = 1 << 0, // włączenie silnika
Release_engine = 1 << 1, // wyłączenie silnika
Change_direction = 1 << 2, // zmiana kierunku (bez skanowania sygnalizacji)
Connect = 1 << 3, // podłączanie wagonów (z częściowym skanowaniem sygnalizacji)
Disconnect = 1 << 4, // odłączanie wagonów (bez skanowania sygnalizacji)
// jazda
Shunt = 1 << 5, // tryb manewrowy
Loose_shunt = 1 << 6, // coupling-free shunting mode
Obey_train = 1 << 7, // tryb pociągowy
Bank = 1 << 8, // assist mode
// others
Jump_to_first_order = 1 << 9, // zapęlenie do pierwszej pozycji (po co?)
};
// the signals the map asks the driver to show (SetSignal)
enum TSignals
@@ -181,7 +165,7 @@ class TSpeedPos
public:
TSpeedPos(TTrack *track, double dist, int flag);
TSpeedPos(basic_event *event, double dist, double length, TOrders order);
TSpeedPos(basic_event *event, double dist, double length, DriverOrders order);
TSpeedPos() = default;
void Clear();
bool Update();
@@ -191,11 +175,11 @@ class TSpeedPos
UpdateDistance( double dist ) {
fDist -= dist; }
// Standing: the train stands, so an event found behind its head wasn't passed by it
bool Set(basic_event *e, double d, double length, TOrders order = Wait_for_orders, bool Standing = false);
bool Set(basic_event *e, double d, double length, DriverOrders order = DriverOrder::waitForOrders, bool Standing = false);
void Set(TTrack *t, double d, int f);
std::string TableText() const;
std::string GetName() const;
bool IsProperSemaphor(TOrders order = Wait_for_orders);
bool IsProperSemaphor(DriverOrders order = DriverOrder::waitForOrders);
};
//----------------------------------------------------------------------------
@@ -281,7 +265,7 @@ public:
// the light pattern the timetable or the scenery asks for at an end of the train, -1 the usual one
int light_hint( int const End ) const { return m_lighthints[ End ]; }
// the order the driver goes by now: a train run, shunting and the like
TOrders order_current() const { return OrderCurrentGet(); }
DriverOrders order_current() const { return OrderCurrentGet(); }
private:
// tells the black box of the train at the station of its timetable it makes for, at the stop point given
void record_station( TSpeedPos const &Point, std::uint8_t Kind ) const;
@@ -396,7 +380,7 @@ private:
// how far short of a stop the AI aims: in train running the middle of the 8-15 m window before a signal,
// otherwise (a W4 stop point, shunting, coupling) half the minimum proximity distance, as before
double stopping_margin() const {
return ( ( OrderCurrentGet() & Obey_train ) != 0 ) && !IsAtPassengerStop ?
return ( OrderCurrentGet().HasAny(DriverOrder::obeyTrain) ) && !IsAtPassengerStop ?
fMinProximityDist + 3.5 :
0.5 * fMinProximityDist; }
void apply_independent_brake_only();
@@ -551,20 +535,20 @@ private:
void RecognizeCommand(); // odczytuje komende przekazana lokomotywie
void JumpToNextOrder( bool const Skipmergedchangedirection = false );
void JumpToFirstOrder();
void OrderPush( TOrders NewOrder );
void OrderNext( TOrders NewOrder );
inline TOrders OrderCurrentGet() const;
inline void OrderCurrentSet( TOrders const Order );
inline TOrders OrderNextGet() const;
void OrderPush( DriverOrders NewOrder );
void OrderNext( DriverOrders NewOrder );
inline DriverOrders OrderCurrentGet() const;
inline void OrderCurrentSet( DriverOrders const Order );
inline DriverOrders OrderNextGet() const;
void OrderCheck();
void OrdersInit( double fVel );
void OrdersClear();
void OrdersDump( std::string const Neworder, bool const Verbose = true );
std::string Order2Str( TOrders Order ) const;
std::string Order2Str( DriverOrders Order ) const;
// members
std::string m_assignment;
glm::dvec3 vCommandLocation; // polozenie wskaznika, sygnalizatora lub innego obiektu do ktorego odnosi sie komenda // NOTE: not used
TOrders OrderList[ maxorders ]; // lista rozkazów
DriverOrders OrderList[ maxorders ]; // lista rozkazów
int OrderPos = 0,
OrderTop = 0; // rozkaz aktualny oraz wolne miejsce do wstawiania nowych
@@ -692,7 +676,7 @@ private:
// consist
// methods
public:
bool CheckVehicles( TOrders user = Wait_for_orders );
bool CheckVehicles( DriverOrders user = DriverOrder::waitForOrders );
private:
bool PrepareEngine();
// tells in the log, once, of standing with the pantographs up and no voltage (eu07/maszyna#421)
@@ -778,23 +762,23 @@ private:
};
inline TOrders TController::OrderCurrentGet() const {
inline DriverOrders TController::OrderCurrentGet() const {
if( OrderPos < 0 || OrderPos >= maxorders ) {
return Wait_for_orders;
return DriverOrder::waitForOrders;
}
return OrderList[ OrderPos ];
}
inline void TController::OrderCurrentSet( TOrders const Order ) {
inline void TController::OrderCurrentSet( DriverOrders const Order ) {
if( OrderPos < 0 || OrderPos >= maxorders ) {
return;
}
OrderList[ OrderPos ] = Order;
}
inline TOrders TController::OrderNextGet() const {
inline DriverOrders TController::OrderNextGet() const {
if( OrderPos < 0 || OrderPos >= maxorders ) {
return Wait_for_orders;
return DriverOrder::waitForOrders;
}
// the order list wraps around, same as in JumpToNextOrder()
return OrderList[ ( OrderPos + 1 ) % maxorders ];
+1 -1
View File
@@ -3789,7 +3789,7 @@ bool TDynamicObject::Update(double dt, double dt1)
if( TestFlag( MoverParameters->AIFlag, sound::attachcoupler ) ) {
auto *driver{ ctOwner ? ctOwner : Mechanik };
if( driver != nullptr ) {
driver->CheckVehicles( Connect );
driver->CheckVehicles( DriverOrder::connect );
}
ClearFlag( MoverParameters->AIFlag, sound::attachcoupler );
}
+4 -4
View File
@@ -8167,7 +8167,7 @@ void TTrain::OnCommand_nearestcarcouplingincrease(TTrain *Train, command_data co
if (Train->DynamicObject->Mechanik)
{
// aktualizacja flag kierunku w składzie
Train->DynamicObject->Mechanik->CheckVehicles(Connect);
Train->DynamicObject->Mechanik->CheckVehicles(DriverOrder::connect);
}
}
}
@@ -8191,7 +8191,7 @@ void TTrain::OnCommand_nearestcarcouplingdisconnect(TTrain *Train, command_data
if (Train->DynamicObject->Mechanik)
{
// aktualizacja flag kierunku w składzie
Train->DynamicObject->Mechanik->CheckVehicles(Disconnect);
Train->DynamicObject->Mechanik->CheckVehicles(DriverOrder::disconnect);
}
}
}
@@ -8255,7 +8255,7 @@ void TTrain::OnCommand_occupiedcarcouplingdisconnect(TTrain *Train, command_data
if (Train->DynamicObject->Mechanik)
{
// aktualizacja flag kierunku w składzie
Train->DynamicObject->Mechanik->CheckVehicles(Disconnect);
Train->DynamicObject->Mechanik->CheckVehicles(DriverOrder::disconnect);
}
}
}
@@ -8287,7 +8287,7 @@ void TTrain::OnCommand_occupiedcarcouplingdisconnectback(TTrain *Train, command_
if (Train->DynamicObject->Mechanik)
{
// aktualizacja flag kierunku w składzie
Train->DynamicObject->Mechanik->CheckVehicles(Disconnect);
Train->DynamicObject->Mechanik->CheckVehicles(DriverOrder::disconnect);
}
}
}
+4 -4
View File
@@ -173,12 +173,12 @@ std::vector<int> choose(std::vector<vehicle> const &Consist, bool const Train) {
return settings;
}
bool runs_as_train(int const Orders) {
return (Orders & (Obey_train | Bank)) != 0;
bool runs_as_train(DriverOrders const Orders) {
return Orders.HasAny(DriverOrder::obeyTrain | DriverOrder::bank);
}
bool inspects_train(int const Orders, bool const Coupled) {
return (Orders & (Shunt | Loose_shunt | Obey_train | Bank)) != 0 || ((Orders & Connect) != 0 && false == Coupled);
bool inspects_train(DriverOrders const Orders, bool const Coupled) {
return Orders.HasAny(DriverOrder::shunt | DriverOrder::looseShunt | DriverOrder::obeyTrain | DriverOrder::bank) || (Orders.HasAny(DriverOrder::connect) && false == Coupled);
}
bool at_standstill(double const Velocity) {
+4 -2
View File
@@ -11,6 +11,8 @@ http://mozilla.org/MPL/2.0/.
#include <vector>
#include "vehicle/driverorders.h"
class TMoverParameters;
// the brake setting (G/P/R/Mg) the virtual wagon inspector (TController::AutoRewident) chooses for every
@@ -63,7 +65,7 @@ std::vector<int> choose(std::vector<vehicle> const &Consist, bool Train);
// the AI runs the consist as a train, rather than shunting it: Obey_train, or Bank, which it drives as a
// train run too (with the braking of a train). Orders is the order of the AI (TOrders)
bool runs_as_train(int Orders);
bool runs_as_train(DriverOrders Orders);
// the AI has the inspector look at its train (TController::AutoRewident(): the brake settings, and the hand and
// spring brakes of the train released) under the orders it drives the train under: shunting, loose shunting, the
@@ -72,7 +74,7 @@ bool runs_as_train(int Orders);
// (CheckVehicles(Connect)); Connect doesn't count for it, so the wagons just coupled keep their hand brakes until
// the order after the coupling looks at the new train, as before: those of them to be left standing again
// (Shunt x,-y) stay braked. Orders is the order of the AI (TOrders)
bool inspects_train(int Orders, bool Coupled);
bool inspects_train(DriverOrders Orders, bool Coupled);
// the train stands for the inspector: slower than the AI takes for a deliberate movement (EU07_AI_MOVEMENT).
// the AI looks at the train while it still creeps to a stop (at a W4, or switching from shunting to the train
+49
View File
@@ -0,0 +1,49 @@
/*
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/.
*/
// the orders of the AI driver: a temporary operation (preparing the engine, changing the direction, coupling) and the
// way it drives (shunting, a train run), which an order can carry together (changeDirection on top of shunt)
#pragma once
#include "utilities/enumset.h"
enum class DriverOrder : unsigned
{
waitForOrders = 0, // czekanie na dostarczenie następnych rozkazów
// operacje tymczasowe
prepareEngine = 1 << 0, // włączenie silnika
releaseEngine = 1 << 1, // wyłączenie silnika
changeDirection = 1 << 2, // zmiana kierunku (bez skanowania sygnalizacji)
connect = 1 << 3, // podłączanie wagonów (z częściowym skanowaniem sygnalizacji)
disconnect = 1 << 4, // odłączanie wagonów (bez skanowania sygnalizacji)
// jazda
shunt = 1 << 5, // tryb manewrowy
looseShunt = 1 << 6, // coupling-free shunting mode
obeyTrain = 1 << 7, // tryb pociągowy
bank = 1 << 8, // assist mode
// others
jumpToFirstOrder = 1 << 9, // zapętlenie do pierwszej pozycji
};
template <> struct IsEnumSetKind<DriverOrder> : std::true_type
{
};
using DriverOrders = EnumSet<DriverOrder>;
// the orders the old numbers of which were from shunt up (32): they move the train, by a way of driving or the jump
inline constexpr DriverOrders movingOrders{DriverOrder::shunt | DriverOrder::looseShunt | DriverOrder::obeyTrain | DriverOrder::bank |
DriverOrder::jumpToFirstOrder};
// the orders the old numbers of which were from obeyTrain up (128): the train runs, not shunting
inline constexpr DriverOrders trainOrders{DriverOrder::obeyTrain | DriverOrder::bank | DriverOrder::jumpToFirstOrder};
// the order as a single value, for a switch: a sum of several of them matches none of the cases
constexpr DriverOrder SingleOrder(DriverOrders const orders)
{
return static_cast<DriverOrder>(orders.Bits());
}