mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-29 20:57:32 +01:00
Keep one primary per consist and let a secondary AI claim it.
A driver without movePrimary no longer just idles. UpdateSecondary() cuts BrakeValveActive so a secondary AI stops fighting the primary over the brake pipe, and - as long as no driver in the consist has iEngineActive - keeps scanning ahead of CabOccupied, so it can take movePrimary and start the engine once a signal permits. scan_route() orients by CabOccupied when DirActive is 0, and ClaimConsistPrimary() clears movePrimary on every other driver in the consist, then calls TableClear() and CheckVehicles(), so sSpeedTable is rebuilt for the new front and ctOwner follows it. - walk the consist with FirstFind()/Neighbour() instead of Next()/Prev(), which stopped at the starting vehicle and mishandled reversed members; - ReassignConsistPrimary() picks a single driver after any coupling change, both from TDynamicObject::couple() and from the AI path; - IsOccupiedByAnotherConsist() decides membership with BelongsToConsist() and takes a signed distance, so a consist left behind no longer cuts the scan short. DistanceToAnotherConsist() hands back the gap it already computes, so OtherConsistAtSignal() counts a foreign vehicle only while it stands closer than the signal itself, instead of comparing track starts; - TSpeedPos::Set() marks an entry with spNotOurShuntSignal when a spShuntSemaphor was already permitting as the entry was added. A shunting signal does not address its permission to a particular consist, so a dormant driver does not wake up on a permission it did not see light up; the flag drops once the signal shows stop, so its own turn is never blocked. spSemaphor is excluded: a semaphore sets a route for us and permits us even when found already lit. The check reads SemPermitIndex - the entry that produced the permitting command - because SemNextIndex can point at an earlier signal. The flag is also stamped whenever the permission turns out to belong to a consist standing in front of us - both where TableUpdateEvent() already knows the signal is for the vehicle ahead, and in check_route_ahead() - so the addressee pulling away does not hand the still-lit signal over to whoever waited behind it; - the scantable debug panel lists the flags by name (spNotOurShuntSignal shows as not-ours) and folds the nearest vehicle ahead into the table by its own distance.
This commit is contained in:
@@ -1616,6 +1616,8 @@ int TDynamicObject::Dettach(int dir)
|
||||
d = d->Next(); // i w drugą stronę
|
||||
}
|
||||
}
|
||||
// the neighbour before uncoupling - afterwards Neighbours no longer points at it
|
||||
auto *const severedneighbour { MoverParameters->Neighbours[ dir ].vehicle };
|
||||
if( MoverParameters->Couplers[ dir ].CouplingFlag ) {
|
||||
// odczepianie, o ile coś podłączone
|
||||
MoverParameters->Dettach( dir );
|
||||
@@ -1628,10 +1630,54 @@ int TDynamicObject::Dettach(int dir)
|
||||
};
|
||||
*/
|
||||
}
|
||||
// after uncoupling every part of the consist must have at most one primary
|
||||
ReassignConsistPrimary();
|
||||
if( severedneighbour != nullptr ) {
|
||||
severedneighbour->ReassignConsistPrimary();
|
||||
}
|
||||
// sprzęg po rozłączaniu (czego się nie da odpiąć
|
||||
return MoverParameters->Couplers[dir].CouplingFlag;
|
||||
}
|
||||
|
||||
void TDynamicObject::ReassignConsistPrimary()
|
||||
{ // picks the primary; ClaimConsistPrimary does the actual primary / valve / table change
|
||||
std::vector<TController *> drivers;
|
||||
TController *existingprimary = nullptr;
|
||||
int primarycount = 0;
|
||||
// walk from the front; Next()/Prev() are not enough, reversed members need the coupler side flipped
|
||||
int side { end::front };
|
||||
auto *front { FirstFind( side ) };
|
||||
if( front == nullptr ) { front = this; }
|
||||
auto dir { 1 - side };
|
||||
auto *farend { front };
|
||||
for( auto *d = front; d != nullptr; d = d->Neighbour( dir ) )
|
||||
{
|
||||
if( d->Mechanik != nullptr )
|
||||
{
|
||||
drivers.emplace_back( d->Mechanik );
|
||||
if( d->Mechanik->primary() ) {
|
||||
++primarycount;
|
||||
existingprimary = d->Mechanik;
|
||||
}
|
||||
}
|
||||
farend = d;
|
||||
}
|
||||
if( drivers.empty() ) { return; }
|
||||
// a human-driven cab takes priority, so the AI does not take the consist from them
|
||||
TController *humandriver = nullptr;
|
||||
for( auto *drv : drivers ) {
|
||||
if( false == drv->AIControllFlag ) { humandriver = drv; break; }
|
||||
}
|
||||
// with a single primary it stays; otherwise: the human, the end vehicles, the first on the list
|
||||
TController *newprimary =
|
||||
( primarycount == 1 ) ? existingprimary :
|
||||
( humandriver != nullptr ) ? humandriver :
|
||||
( front->Mechanik != nullptr ) ? front->Mechanik :
|
||||
( farend->Mechanik != nullptr ) ? farend->Mechanik :
|
||||
drivers.front();
|
||||
newprimary->ClaimConsistPrimary();
|
||||
}
|
||||
|
||||
void
|
||||
TDynamicObject::couple( int const Side ) {
|
||||
|
||||
@@ -1653,6 +1699,7 @@ TDynamicObject::couple( int const Side ) {
|
||||
Side, neighbour.vehicle_end,
|
||||
othervehicleparams,
|
||||
coupling::coupler ) ) {
|
||||
ReassignConsistPrimary(); // coupled consists are one consist, with one primary
|
||||
// one coupling type per key press
|
||||
return;
|
||||
}
|
||||
@@ -3118,6 +3165,7 @@ bool TDynamicObject::Update(double dt, double dt1)
|
||||
MoverParameters->InsideConsist = false;
|
||||
}
|
||||
if( TestFlag( MoverParameters->AIFlag, sound::attachcoupler ) ) {
|
||||
ReassignConsistPrimary(); // coupled consists are one consist, with one primary
|
||||
auto *driver{ ctOwner ? ctOwner : Mechanik };
|
||||
if( driver != nullptr ) {
|
||||
driver->CheckVehicles( Connect );
|
||||
|
||||
Reference in New Issue
Block a user