diff --git a/utilities/Classes.h b/utilities/Classes.h index ee4442982..e78077c1d 100644 --- a/utilities/Classes.h +++ b/utilities/Classes.h @@ -92,6 +92,7 @@ enum class TCommandType // cm_Shunt, // unused? cm_EmergencyBrake, cm_SecuritySystemMagnet, + cm_SignalAreaEnd, // end of the interlocking area of the signal passed last cm_Command // komenda pobierana z komórki }; diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index dda2d8875..790d6f699 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -32,7 +32,9 @@ http://mozilla.org/MPL/2.0/. #include "world/station.h" #include "input/keyboardinput.h" +#include #include +#include #define LOGVELOCITY 0 #define LOGORDERS 1 @@ -293,6 +295,11 @@ void TSpeedPos::CommandCheck() case TCommandType::cm_SecuritySystemMagnet: fVelNext = -1; break; + case TCommandType::cm_SignalAreaEnd: + // imposes no speed of its own, it only ends the reduced speed of the signal passed last + fVelNext = -1; + iFlags |= spSignalAreaEnd; + break; default: // inna komenda w evencie skanowanym powoduje zatrzymanie i wysłanie tej komendy // nie manewrowa, nie przystanek, nie zatrzymać na SBL @@ -861,6 +868,13 @@ bool is_signal_at_stop( TSpeedPos const &Point ) && Point.evEvent != nullptr; } +void clear_signal_area_end( TSpeedPos &Point ) +{ + if( TestFlag( Point.iFlags, spSignalAreaEnd ) ) { + Point.Clear(); + } +} + // the position is a signal, a main or a shunting one: the aspect comes from its memory cell. putvalues events // set speed limits by the track, and the flags they get don't make them signals bool is_signal( TSpeedPos const &Point ) @@ -970,6 +984,7 @@ TCommandType TController::TableUpdate(double &fVelDes, double &fDist, double &fN mvOccupied->SecuritySystem.set_cabsignal_lock(false); // te flagi są ustawiane tutaj, w razie potrzeby iDrivigFlags &= ~(moveTrackEnd | moveSwitchFound | moveSignalFound | /*moveSpeedLimitFound*/ moveStopPointFound ); + SignalAreaEndDist = -1.0; update_point_limits(); for( std::size_t idx = 0; idx < sSpeedTable.size(); ++idx ) { @@ -1156,8 +1171,10 @@ TCommandType TController::TableUpdate(double &fVelDes, double &fDist, double &fN VelLimitLastDist.second = EU07_AI_SPEEDLIMITEXTENDSBEYONDSCANRANGE; } // take into account the effect switches have on duration of signal-imposed speed limit, in calculation of speed limit end point - if( VelSignalLast >= 0.0 && SwitchClearDist >= 0.0 ) { - VelLimitLastDist.second = std::max( VelLimitLastDist.second, SwitchClearDist ); + // a reduced speed ends where the interlocking area does, if that is marked, and not past the switches farther on + if( auto const signallimitend { VelSignalLast > 0.0 && SignalAreaEndDist >= 0.0 ? SignalAreaEndDist : SwitchClearDist }; + VelSignalLast >= 0.0 && signallimitend >= 0.0 ) { + VelLimitLastDist.second = std::max( VelLimitLastDist.second, signallimitend ); } //analiza spisanych z tabelki ograniczeń i nadpisanie aktualnego @@ -1488,6 +1505,104 @@ TController::TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, doub return false; // } +// a road speed sign (W27/W27a) passed by the head stays in the table until the rear is past it too. not for cars: their +// passed events leave the table at the latest when the rear passes them (TableCheck), before the table update sees that +bool +TController::is_road_velocity_held( TSpeedPos const &Point ) const { + + return TestFlag( Point.iFlags, spRoadVel ) + && mvOccupied->CategoryFlag != 2 + && Point.fDist >= -fLength; +} + +// a passed event is handled once and then dropped from the table, except a road speed sign held for the rear +void +TController::TableDropPassedEvent( TSpeedPos &Point ) const { + + if( is_road_velocity_held( Point ) ) { + Point.iFlags |= spRoadVelHeld; + return; + } + Point.Clear(); +} + +// the lowest road speed of the signs past the given one that the head has passed: held until the rear is past them too, +// or passed in this very tick and not marked as held yet; -1 if there's none +double +TController::held_road_velocity_beyond( TSpeedPos const &Point ) const { + + auto lowest { -1.0 }; + for( auto const &held : sSpeedTable ) { + if( ( held.iFlags & ( spEnabled | spRoadVel ) ) != ( spEnabled | spRoadVel ) + || held.fDist >= 0.0 + || held.fDist <= Point.fDist ) { + continue; + } + lowest = min_speed( lowest, held.fVelNext ); + } + return lowest; +} + +// a road speed (W27/W27a) lower than the one in force holds once the head passes the sign, a higher one only once the +// whole train is past it (a car takes it from the head). the first pass after the head handles the sign as before, the +// entry is then only held for the rear: returns true for it, so the rest of the table update leaves it out +bool +TController::TableUpdateRoadVelocity( TSpeedPos &Point ) { + + if( Point.fDist >= 0.0 ) { return false; } + + if( mvOccupied->CategoryFlag == 2 ) { + VelRoad = Point.fVelNext; + return false; + } + + auto const isrearpast { Point.fDist < -fLength }; + if( isrearpast ) { + // the rear past it, but not past a sign after it the head has passed too: none of those may be exceeded, so a + // lower one (W27a 60 within a train's length after a 120) stays in force + VelRoad = min_speed( Point.fVelNext, held_road_velocity_beyond( Point ) ); + } + else if( Point.fVelNext == min_speed( Point.fVelNext, VelRoad ) ) { + VelRoad = Point.fVelNext; + } + if( false == TestFlag( Point.iFlags, spRoadVelHeld ) ) { + return false; + } + if( isrearpast ) { + Point.Clear(); + } + return true; +} + +// a reduced speed shown by a signal holds to the end of the interlocking area the signal guards (Ie-1), and that end is +// marked on the main track with a SignalAreaEnd event. on a track without the marker the speed holds as it did before: +// until the next signal, or until no signal, switch or stop point is left in the scan +void +TController::TableUpdateSignalAreaEnd( TSpeedPos &Point ) { + + // the switches lie in front of the marker, so the whole train has left them once its rear end is past it + if( Point.fDist >= -fLength ) { + if( SignalAreaEndDist < 0.0 + && TestFlag( OrderCurrentGet(), Obey_train ) ) { + // the table is sorted by distance, so the first marker is the nearest one + SignalAreaEndDist = Point.fDist + fLength; + } + return; + } + + // in shunting the marker stays, a train order given before the next signal still finds it + if( false == TestFlag( OrderCurrentGet(), Obey_train ) ) { return; } + + // a passed signal at stop (0) keeps holding the train, only a reduced speed ends here + if( VelSignalLast > 0.0 ) { + if( Global.iWriteLogEnabled & 8 ) { + WriteLog( "Speed table update for " + OwnerName() + ", left the interlocking area at " + Point.evEvent->m_name ); + } + VelSignalLast = -1.0; + } + Point.Clear(); +} + bool TController::point_limit_latched( basic_event const *Cell ) const { return std::any_of( @@ -1663,6 +1778,12 @@ glm::dvec3 TController::travel_direction() const { bool TController::TableUpdateEvent( double &Velocity, TCommandType &Command, TSpeedPos &Point, double &Signaldistance, int const Pointindex ) { + if( TestFlag( Point.iFlags, spSignalAreaEnd ) ) { + // not a signal: it neither sends a command nor counts as the end of a speed limit in the scan + TableUpdateSignalAreaEnd( Point ); + return true; + } + if( TestFlag( Point.iFlags, spPointVelocity ) ) { // PointVelocity: no signal, no command, only the limit at its point // done once the head passes the point @@ -1840,6 +1961,9 @@ TController::TableUpdateEvent( double &Velocity, TCommandType &Command, TSpeedPo // for ai-driven vehicles always play by the rules else { VelSignalLast = Point.fVelNext; //minięty daje prędkość obowiązującą + // a marker before this signal ends the interlocking area of an earlier signal, not the area of this one + // (a train longer than the way between the two, or one which started standing over the marker) + std::ranges::for_each( sSpeedTable | std::views::take( Pointindex ), clear_signal_area_end ); } } else { @@ -1865,12 +1989,13 @@ TController::TableUpdateEvent( double &Velocity, TCommandType &Command, TSpeedPo } } else if ( Point.iFlags & spRoadVel) - { // to W6 - if ( Point.fDist < 0) - VelRoad = Point.fVelNext; + { // to W27/W27a + if( TableUpdateRoadVelocity( Point ) ) { + return true; + } } else if ( Point.iFlags & spSectionVel && Point.fDist < 0) - { // to W27 + { // to W9 // teraz trzeba sprawdzić inne warunki if (Point.fSectionVelocityDist == 0.0) { @@ -1992,7 +2117,7 @@ TController::TableUpdateEvent( double &Velocity, TCommandType &Command, TSpeedPo } // jeśli nie jest ograniczeniem prędkości to można usunąć // (nie mogą być usuwane w skanowaniu) - Point.Clear(); + TableDropPassedEvent( Point ); } } else if (Point.evEvent->is_command()) diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 69f5cffc0..b30701911 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -147,7 +147,9 @@ enum TSpeedPosFlag spRoadVel = 0x8000, // zadanie prędkości drogowej spSectionVel = 0x20000, // odcinek z ograniczeniem spProximityVelocity = 0x40000, // odcinek z ograniczeniem i podaną jego długościa - spPointVelocity = 0x80000 // ograniczenie dla czoła w punkcie fPointDist za wpisem (PointVelocity) + spPointVelocity = 0x80000, // ograniczenie dla czoła w punkcie fPointDist za wpisem (PointVelocity) + spRoadVelHeld = 0x100000, // a road speed sign passed by the head, handled, kept until the rear is past it + spSignalAreaEnd = 0x200000 // end of the interlocking area of the signal passed last // spDontApplySpeedLimit = 0x10000 // this point won't apply its speed limit. potentially set by the scanning vehicle }; @@ -448,6 +450,7 @@ private: double ActualProximityDist = 1.0; // odległość brana pod uwagę przy wyliczaniu prędkości i przyspieszenia std::pair VelLimitLastDist { 0.0, 0.0 }; // distance to velocity change point double SwitchClearDist { 0.0 }; // distance to point after farthest detected switch + double SignalAreaEndDist { -1.0 }; // distance the train has to go to be past the nearest end of an interlocking area, -1 when none is in the scan int iDirection = 0; // kierunek jazdy względem sprzęgów pojazdu, w którym siedzi AI (1=przód,-1=tył) int iDirectionOrder = 0; //żadany kierunek jazdy (służy do zmiany kierunku) std::optional iDirectionBackup; // consist direction to be restored after coupling/uncoupling and similar direction-changing operations @@ -567,6 +570,11 @@ private: TCommandType TableUpdate( double &fVelDes, double &fDist, double &fNext, double &fAcc ); bool TableUpdateStopPoint( TCommandType &Command, TSpeedPos &Point, double const Signaldistance ); bool TableUpdateEvent( double &Velocity, TCommandType &Command, TSpeedPos &Point, double &Signaldistance, int const Pointindex ); + bool TableUpdateRoadVelocity( TSpeedPos &Point ); + double held_road_velocity_beyond( TSpeedPos const &Point ) const; + bool is_road_velocity_held( TSpeedPos const &Point ) const; + void TableDropPassedEvent( TSpeedPos &Point ) const; + void TableUpdateSignalAreaEnd( TSpeedPos &Point ); // keeps the PointVelocity limit of a speed table entry whose cell the head has just passed void latch_point_limit( TSpeedPos const &Point ); // moves the latched point limits by the movement of the consist, drops those passed and those read anew diff --git a/world/Event.cpp b/world/Event.cpp index 533328152..7b870493f 100644 --- a/world/Event.cpp +++ b/world/Event.cpp @@ -749,6 +749,11 @@ putvalues_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) m_input.command_type = TCommandType::cm_SecuritySystemMagnet; m_passive = true; } + else if( token == "SignalAreaEnd" ) { + // scanned, so the train sees where the interlocking area of the signal it passed ends + m_input.command_type = TCommandType::cm_SignalAreaEnd; + m_passive = true; + } else { m_input.command_type = TCommandType::cm_Unknown; }