18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-21 00:24:57 +01:00

fix: AI wagon inspector cuts out the brake of wagons without G in a train on G (eu07/maszyna#350)

Cw-1 §19.9: in a freight train braked on G, the brake of a vehicle that has
no G setting (e.g. a passenger car with P and R) is cut out. The inspector
put such wagons on P; now it cuts their brake out the way the scenery's '0'
flag does (b_dmg, the distributor's reservoirs emptied), so the brake
releases and stays off.

- The vehicle remembers the inspector cut it out
  (TMoverParameters::BrakeCutOutByInspector); the next inspection puts such
  a brake back and sets it to what the new train needs. A brake cut out for
  any other reason (scenery, player, damage) is never put back; the cab's
  cut-out toggle hands an inspector's cut-out over to the player.
- Locomotives and the vehicle the train is driven from keep a working
  brake (on P). In the long locomotive setting (§19.3b) the train runs on
  P, so a wagon without G stays on P there.
- The braking tables after the settings leave cut-out brakes out.
- §18.3: a car with R whose brake is cut out counts as one on P.
- ai.brakesetting.cw1 no keeps the earlier behaviour: nothing cut out.

The choice and the cutting out stay in vehicle/brakesetting.{h,cpp}, with
unit tests on real wagons.
This commit is contained in:
jerrrrycho
2026-11-08 13:03:06 +01:00
parent 1e650e375a
commit 81f10e4d90
6 changed files with 273 additions and 19 deletions

View File

@@ -2084,9 +2084,7 @@ void TController::AutoRewident()
// 1. Zebranie informacji o składzie pociągu, od czoła do końca
std::vector<brake_setting::vehicle> consist;
for (auto const *d = pVehicles[0]; d != nullptr; d = d->Next()) {
auto const *mover{d->MoverParameters};
// a locomotive has Power > 1, though it may be a dead one at the end of the train
consist.emplace_back(mover->BrakeDelays, mover->Power >= 1.0, mover->Dim.L, mover->TotalMass);
consist.emplace_back(brake_setting::describe(*d->MoverParameters, d == pVehicle));
}
// 2. Określenie typu pociągu i nastaw: by the brake instruction Cw-1 (eu07/maszyna#350), or as the releases before it did
auto const settings{Global.AIBrakeSettingCw1 ? brake_setting::choose(consist) : brake_setting::choose_legacy(consist)};
@@ -2099,9 +2097,8 @@ void TController::AutoRewident()
// don't touch human-controlled vehicle, but others are free game
continue;
}
if (*setting != 0) {
d->MoverParameters->BrakeDelaySwitch(*setting);
}
// the setting, a brake cut out in a train on G (Cw-1 §19.9) or put back when the train no longer needs it cut out
brake_setting::apply(*d->MoverParameters, *setting);
// potentially release manual brake
d->MoverParameters->DecManualBrakeLevel( ManualBrakePosNo );
d->MoverParameters->SpringBrake.Activate = false;
@@ -2145,7 +2142,10 @@ void TController::AutoRewident()
double const velstep = mvOccupied->Vmax * 0.5 / BrakeAccTableSize;
auto *d = pVehicles[0]; // pojazd na czele składu
while (d) {
for( int i = 0; i < BrakeAccTableSize; ++i ) {
// a vehicle with its brake cut out (by the inspector above or for a reason of its own) adds its mass only;
// the inspector of the earlier releases counted its brake in
auto const brakes{false == Global.AIBrakeSettingCw1 || false == brake_setting::is_cut_out(*d->MoverParameters)};
for( int i = 0; i < BrakeAccTableSize && brakes; ++i ) {
fBrake_a0[ i + 1 ] += d->MoverParameters->BrakeForceR( 0.25, velstep*( 1 + 2 * i ) );
fBrake_a1[ i + 1 ] += d->MoverParameters->BrakeForceR( 1.00, velstep*( 1 + 2 * i ) );
}

View File

@@ -2114,6 +2114,8 @@ void TTrain::OnCommand_trainbrakeoperationtoggle(TTrain *Train, command_data con
}
vehicle->MoverParameters->Hamulec->SetBrakeStatus(vehicle->MoverParameters->Hamulec->GetBrakeStatus() ^ b_dmg);
// the brake is the player's now, the wagon inspector won't put it back on its own
vehicle->MoverParameters->BrakeCutOutByInspector = false;
}
}

View File

@@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/.
#include "stdafx.h"
#include "vehicle/brakesetting.h"
#include "McZapkie/MOVER.h"
#include "McZapkie/hamulce.h"
#include <algorithm>
@@ -48,6 +49,7 @@ struct wagon_counts {
int r{0}; // with R: passenger cars of fast trains
int g{0}; // with G but no R: freight wagons
int p{0}; // the rest: passenger cars with P only
int r_cut_out{0}; // of those with R, the ones with their brake cut out
double length{0.0}; // [m]
double mass{0.0}; // [kg]
@@ -60,6 +62,9 @@ struct wagon_counts {
mass += item.mass;
if (has(item.delays, bdelay_R)) {
++r;
if (item.cut_out) {
++r_cut_out;
}
}
else if (has(item.delays, bdelay_G)) {
++g;
@@ -95,9 +100,11 @@ train_setting choose_train_setting(std::vector<vehicle> const &Consist) {
return train_setting::light_engine;
}
if (counts.passenger()) {
// wagons without R run on P, and there may be no more than three of them, fewer than those on R
auto const pwagons{counts.g + counts.p};
return pwagons <= passenger_r_max_p_wagons && counts.r > pwagons ? train_setting::passenger_r : train_setting::passenger_p;
// wagons without R run on P, and a wagon with its brake cut out counts as one on P (Cw-1 §18.3). there may
// be no more than three of them, fewer than those on R
auto const pwagons{counts.g + counts.p + counts.r_cut_out};
auto const rwagons{counts.r - counts.r_cut_out};
return pwagons <= passenger_r_max_p_wagons && rwagons > pwagons ? train_setting::passenger_r : train_setting::passenger_p;
}
if (counts.length > freight_p_max_length || counts.mass > freight_p_max_mass) {
return train_setting::freight_g;
@@ -135,18 +142,27 @@ std::vector<int> choose(std::vector<vehicle> const &Consist) {
break;
}
case train_setting::freight_gp: {
// the wagons are counted from the head of the train, past any locomotive in the middle of it
// the wagons are counted from the head of the train, past any locomotive in the middle of it.
// the train runs on P (Cw-1 §19.3b), so §19.9 doesn't apply: a vehicle without G among the ones
// kept on G brakes on P like the rest of the train, rather than being cut out
auto const ong{item.powered || wagonsfromhead <= freight_gp_wagons_on_g};
settings.emplace_back(ong ? pick(item.delays, bdelay_G, bdelay_P) : pick(item.delays, bdelay_P, bdelay_G));
break;
}
case train_setting::freight_g:
default: {
// NOTE: Cw-1 §19.9 wants the brake of a vehicle without G (a passenger car with P and R only) cut out in a
// train braked on G. It's put on P instead, as before: a brake cut out by the inspector would have to be
// remembered and put back when the train is made up again, and the simulator has no such record yet.
// The same goes for the vehicles kept on G in the long locomotive setting above (freight_gp)
settings.emplace_back(pick(item.delays, bdelay_G, bdelay_P));
if (has(item.delays, bdelay_G)) {
settings.emplace_back(bdelay_G);
}
else if (item.powered || item.driven || item.delays == 0) {
// a locomotive, or the vehicle the train is driven from, keeps a working brake: on P. a vehicle
// which tells no settings at all is left alone
settings.emplace_back(pick(item.delays, bdelay_P, bdelay_P));
}
else {
// a wagon without G (a passenger car with P and R) has its brake cut out (Cw-1 §19.9)
settings.emplace_back(cut_out);
}
break;
}
}
@@ -217,4 +233,47 @@ std::vector<int> choose_legacy(std::vector<vehicle> const &Consist) {
return settings;
}
bool is_cut_out(TMoverParameters const &Vehicle) {
return (Vehicle.Hamulec->GetBrakeStatus() & b_dmg) == b_dmg;
}
vehicle describe(TMoverParameters const &Vehicle, bool const Driven) {
vehicle described;
described.delays = Vehicle.BrakeDelays;
// a locomotive has Power > 1, though it may be a dead one at the end of the train
described.powered = Vehicle.Power >= 1.0;
described.length = Vehicle.Dim.L;
described.mass = Vehicle.TotalMass;
described.cut_out = is_cut_out(Vehicle) && false == Vehicle.BrakeCutOutByInspector;
described.driven = Driven;
return described;
}
void apply(TMoverParameters &Vehicle, int const Setting) {
auto &brake{*Vehicle.Hamulec};
if (Vehicle.BrakeCutOutByInspector && false == is_cut_out(Vehicle)) {
// someone put the brake back in the meantime, it's theirs now
Vehicle.BrakeCutOutByInspector = false;
}
if (Setting == cut_out) {
if (false == is_cut_out(Vehicle)) {
// close the cut-out cock and empty the brake with the releaser, as the scenery's '0' does: the
// distributor, cut off the brake pipe, has no air left to brake with and lets the cylinders go
brake.ForceEmptiness();
brake.SetBrakeStatus(brake.GetBrakeStatus() | b_dmg);
Vehicle.BrakeCutOutByInspector = true;
}
// a brake cut out already, by the inspector or for a reason of its own, stays as it is
return;
}
if (Vehicle.BrakeCutOutByInspector) {
// open the cut-out cock: the distributor charges from the brake pipe again
brake.SetBrakeStatus(brake.GetBrakeStatus() & ~b_dmg);
Vehicle.BrakeCutOutByInspector = false;
}
if (Setting != 0) {
Vehicle.BrakeDelaySwitch(Setting);
}
}
} // namespace brake_setting

View File

@@ -11,6 +11,8 @@ http://mozilla.org/MPL/2.0/.
#include <vector>
class TMoverParameters;
// the brake setting (G/P/R/Mg) the virtual wagon inspector (TController::AutoRewident) chooses for every
// vehicle of a train, by the rules of the brake instruction Cw-1 (Mw56), §18 and §19 (eu07/maszyna#350)
namespace brake_setting {
@@ -21,6 +23,8 @@ struct vehicle {
bool powered{false}; // a locomotive or a powered unit, as opposed to a wagon
double length{0.0}; // [m]
double mass{0.0}; // with its load [kg]
bool cut_out{false}; // the brake is cut out for a reason of its own (damage, the scenario, the player)
bool driven{false}; // the vehicle the train is driven from: its brake is never cut out
};
// the kind of train the inspector takes the consist for, and how it sets the brakes
@@ -36,6 +40,9 @@ enum class train_setting {
// how many wagons next to the locomotive stay on G in a freight_gp train
inline constexpr int freight_gp_wagons_on_g{5};
// the setting choose() gives a vehicle whose brake is to be cut out (Cw-1 §19.9)
inline constexpr int cut_out{-1};
// the train setting by Cw-1. Consist runs from the head of the train to its rear
train_setting choose_train_setting(std::vector<vehicle> const &Consist);
@@ -47,11 +54,28 @@ constexpr bool passenger(train_setting const Setting) {
// the brake setting (bdelay_*) for every vehicle of Consist, in the same order, by Cw-1. a vehicle
// without the setting the train needs gets the other one of G and P it has, and 0 (left as it is)
// when it has neither. Cw-1 §19.9 would have the brake of such a vehicle cut out instead
// when it has neither. In a train on G (freight_g) a wagon without G gets cut_out instead: its brake
// is to be cut out (Cw-1 §19.9). A locomotive and the vehicle the train is driven from keep their
// brake working, on P
std::vector<int> choose(std::vector<vehicle> const &Consist);
// the inspector of the releases before eu07/maszyna#350, kept for ai.brakesetting.cw1 no. the settings
// are requests: a vehicle which doesn't have the one asked for keeps its own
std::vector<int> choose_legacy(std::vector<vehicle> const &Consist);
// the brake of the vehicle is cut out (b_dmg), whoever did it
bool is_cut_out(TMoverParameters const &Vehicle);
// what choose() needs to know of the vehicle. a brake the inspector cut out itself doesn't count as
// cut out: the inspector decides about it anew
vehicle describe(TMoverParameters const &Vehicle, bool Driven);
// the inspector puts the vehicle on Setting, as choose() gives it. cut_out cuts the brake out the way
// the scenery's '0' does: b_dmg, which cuts the distributor off the brake pipe (as the cab's cut-out,
// trainbrakeoperationtoggle, does), and its reservoirs emptied, so the brake releases and stays off.
// The vehicle remembers the inspector did it. Any other setting first puts back a brake the inspector
// cut out (it charges from the brake pipe again), then sets the vehicle to it (0: leaves the setting
// as it is). A brake cut out by anyone else stays cut out
void apply(TMoverParameters &Vehicle, int Setting);
} // namespace brake_setting