LCOV - code coverage report
Current view: top level - src/microsim - MSVehicle.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 96.4 % 3370 3248
Test Date: 2026-06-15 15:46:12 Functions: 95.2 % 227 216

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    MSVehicle.cpp
      15              : /// @author  Christian Roessel
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Bjoern Hendriks
      18              : /// @author  Daniel Krajzewicz
      19              : /// @author  Thimor Bohn
      20              : /// @author  Friedemann Wesner
      21              : /// @author  Laura Bieker
      22              : /// @author  Clemens Honomichl
      23              : /// @author  Michael Behrisch
      24              : /// @author  Axel Wegener
      25              : /// @author  Christoph Sommer
      26              : /// @author  Leonhard Luecken
      27              : /// @author  Lara Codeca
      28              : /// @author  Mirko Barthauer
      29              : /// @date    Mon, 05 Mar 2001
      30              : ///
      31              : // Representation of a vehicle in the micro simulation
      32              : /****************************************************************************/
      33              : #include <config.h>
      34              : 
      35              : #include <iostream>
      36              : #include <cassert>
      37              : #include <cmath>
      38              : #include <cstdlib>
      39              : #include <algorithm>
      40              : #include <map>
      41              : #include <memory>
      42              : #include <utils/common/ToString.h>
      43              : #include <utils/common/FileHelpers.h>
      44              : #include <utils/router/DijkstraRouter.h>
      45              : #include <utils/common/MsgHandler.h>
      46              : #include <utils/common/RandHelper.h>
      47              : #include <utils/common/StringUtils.h>
      48              : #include <utils/common/StdDefs.h>
      49              : #include <utils/geom/GeomHelper.h>
      50              : #include <utils/iodevices/OutputDevice.h>
      51              : #include <utils/xml/SUMOSAXAttributes.h>
      52              : #include <utils/vehicle/SUMOVehicleParserHelper.h>
      53              : #include <microsim/lcmodels/MSAbstractLaneChangeModel.h>
      54              : #include <microsim/transportables/MSPerson.h>
      55              : #include <microsim/transportables/MSPModel.h>
      56              : #include <microsim/devices/MSDevice_Transportable.h>
      57              : #include <microsim/devices/MSDevice_DriverState.h>
      58              : #include <microsim/devices/MSDevice_Friction.h>
      59              : #include <microsim/devices/MSDevice_Taxi.h>
      60              : #include <microsim/devices/MSDevice_Vehroutes.h>
      61              : #include <microsim/devices/MSDevice_ElecHybrid.h>
      62              : #include <microsim/devices/MSDevice_GLOSA.h>
      63              : #include <microsim/output/MSStopOut.h>
      64              : #include <microsim/trigger/MSChargingStation.h>
      65              : #include <microsim/trigger/MSOverheadWire.h>
      66              : #include <microsim/traffic_lights/MSTrafficLightLogic.h>
      67              : #include <microsim/traffic_lights/MSRailSignalControl.h>
      68              : #include <microsim/lcmodels/MSAbstractLaneChangeModel.h>
      69              : #include <microsim/transportables/MSTransportableControl.h>
      70              : #include <microsim/devices/MSDevice_Transportable.h>
      71              : #include "MSEdgeControl.h"
      72              : #include "MSVehicleControl.h"
      73              : #include "MSInsertionControl.h"
      74              : #include "MSVehicleTransfer.h"
      75              : #include "MSGlobals.h"
      76              : #include "MSJunctionLogic.h"
      77              : #include "MSStop.h"
      78              : #include "MSStoppingPlace.h"
      79              : #include "MSParkingArea.h"
      80              : #include "MSMoveReminder.h"
      81              : #include "MSLane.h"
      82              : #include "MSJunction.h"
      83              : #include "MSEdge.h"
      84              : #include "MSVehicleType.h"
      85              : #include "MSNet.h"
      86              : #include "MSRoute.h"
      87              : #include "MSLeaderInfo.h"
      88              : #include "MSDriverState.h"
      89              : #include "MSVehicle.h"
      90              : 
      91              : 
      92              : //#define DEBUG_PLAN_MOVE
      93              : //#define DEBUG_PLAN_MOVE_LEADERINFO
      94              : //#define DEBUG_CHECKREWINDLINKLANES
      95              : //#define DEBUG_EXEC_MOVE
      96              : //#define DEBUG_FURTHER
      97              : //#define DEBUG_SETFURTHER
      98              : //#define DEBUG_TARGET_LANE
      99              : //#define DEBUG_STOPS
     100              : //#define DEBUG_BESTLANES
     101              : //#define DEBUG_IGNORE_RED
     102              : //#define DEBUG_ACTIONSTEPS
     103              : //#define DEBUG_NEXT_TURN
     104              : //#define DEBUG_TRACI
     105              : //#define DEBUG_REVERSE_BIDI
     106              : //#define DEBUG_EXTRAPOLATE_DEPARTPOS
     107              : //#define DEBUG_REMOTECONTROL
     108              : //#define DEBUG_MOVEREMINDERS
     109              : //#define DEBUG_COND (getID() == "ego")
     110              : //#define DEBUG_COND (true)
     111              : #define DEBUG_COND (isSelected())
     112              : //#define DEBUG_COND2(obj) (obj->getID() == "ego")
     113              : #define DEBUG_COND2(obj) (obj->isSelected())
     114              : 
     115              : //#define PARALLEL_STOPWATCH
     116              : 
     117              : 
     118              : #define STOPPING_PLACE_OFFSET 0.5
     119              : 
     120              : #define CRLL_LOOK_AHEAD 5
     121              : 
     122              : #define JUNCTION_BLOCKAGE_TIME 5 // s
     123              : 
     124              : // @todo Calibrate with real-world values / make configurable
     125              : #define DIST_TO_STOPLINE_EXPECT_PRIORITY 1.0
     126              : 
     127              : #define NUMERICAL_EPS_SPEED (0.1 * NUMERICAL_EPS * TS)
     128              : 
     129              : // ===========================================================================
     130              : // static value definitions
     131              : // ===========================================================================
     132              : std::vector<MSLane*> MSVehicle::myEmptyLaneVector;
     133              : 
     134              : 
     135              : // ===========================================================================
     136              : // method definitions
     137              : // ===========================================================================
     138              : /* -------------------------------------------------------------------------
     139              :  * methods of MSVehicle::State
     140              :  * ----------------------------------------------------------------------- */
     141            0 : MSVehicle::State::State(const State& state) {
     142            0 :     myPos = state.myPos;
     143            0 :     mySpeed = state.mySpeed;
     144            0 :     myPosLat = state.myPosLat;
     145            0 :     myBackPos = state.myBackPos;
     146            0 :     myPreviousSpeed = state.myPreviousSpeed;
     147            0 :     myLastCoveredDist = state.myLastCoveredDist;
     148            0 : }
     149              : 
     150              : 
     151              : MSVehicle::State&
     152      3539838 : MSVehicle::State::operator=(const State& state) {
     153      3539838 :     myPos   = state.myPos;
     154      3539838 :     mySpeed = state.mySpeed;
     155      3539838 :     myPosLat   = state.myPosLat;
     156      3539838 :     myBackPos = state.myBackPos;
     157      3539838 :     myPreviousSpeed = state.myPreviousSpeed;
     158      3539838 :     myLastCoveredDist = state.myLastCoveredDist;
     159      3539838 :     return *this;
     160              : }
     161              : 
     162              : 
     163              : bool
     164            0 : MSVehicle::State::operator!=(const State& state) {
     165            0 :     return (myPos    != state.myPos ||
     166            0 :             mySpeed  != state.mySpeed ||
     167            0 :             myPosLat != state.myPosLat ||
     168            0 :             myLastCoveredDist != state.myLastCoveredDist ||
     169            0 :             myPreviousSpeed != state.myPreviousSpeed ||
     170            0 :             myBackPos != state.myBackPos);
     171              : }
     172              : 
     173              : 
     174      8044460 : MSVehicle::State::State(double pos, double speed, double posLat, double backPos, double previousSpeed) :
     175      8044460 :     myPos(pos), mySpeed(speed), myPosLat(posLat), myBackPos(backPos), myPreviousSpeed(previousSpeed), myLastCoveredDist(SPEED2DIST(speed)) {}
     176              : 
     177              : 
     178              : 
     179              : /* -------------------------------------------------------------------------
     180              :  * methods of MSVehicle::WaitingTimeCollector
     181              :  * ----------------------------------------------------------------------- */
     182      4504622 : MSVehicle::WaitingTimeCollector::WaitingTimeCollector(SUMOTime memory) : myMemorySize(memory) {}
     183              : 
     184              : 
     185              : SUMOTime
     186      1428748 : MSVehicle::WaitingTimeCollector::cumulatedWaitingTime(SUMOTime memorySpan) const {
     187              :     assert(memorySpan <= myMemorySize);
     188      1428748 :     if (memorySpan == -1) {
     189            0 :         memorySpan = myMemorySize;
     190              :     }
     191              :     SUMOTime totalWaitingTime = 0;
     192      5944790 :     for (const auto& interval : myWaitingIntervals) {
     193      4516042 :         if (interval.second >= memorySpan) {
     194       655960 :             if (interval.first >= memorySpan) {
     195              :                 break;
     196              :             } else {
     197       655960 :                 totalWaitingTime += memorySpan - interval.first;
     198              :             }
     199              :         } else {
     200      3860082 :             totalWaitingTime += interval.second - interval.first;
     201              :         }
     202              :     }
     203      1428748 :     return totalWaitingTime;
     204              : }
     205              : 
     206              : 
     207              : void
     208    700505992 : MSVehicle::WaitingTimeCollector::passTime(SUMOTime dt, bool waiting) {
     209              :     auto i = myWaitingIntervals.begin();
     210              :     const auto end = myWaitingIntervals.end();
     211    700505992 :     const bool startNewInterval = i == end || (i->first != 0);
     212   1143825970 :     while (i != end) {
     213    445595769 :         i->first += dt;
     214    445595769 :         if (i->first >= myMemorySize) {
     215              :             break;
     216              :         }
     217    443319978 :         i->second += dt;
     218              :         i++;
     219              :     }
     220              : 
     221              :     // remove intervals beyond memorySize
     222              :     auto d = std::distance(i, end);
     223    702781783 :     while (d > 0) {
     224      2275791 :         myWaitingIntervals.pop_back();
     225      2275791 :         d--;
     226              :     }
     227              : 
     228    700505992 :     if (!waiting) {
     229              :         return;
     230     91831487 :     } else if (!startNewInterval) {
     231     88188055 :         myWaitingIntervals.begin()->first = 0;
     232              :     } else {
     233      7286864 :         myWaitingIntervals.push_front(std::make_pair(0, dt));
     234              :     }
     235              :     return;
     236              : }
     237              : 
     238              : 
     239              : const std::string
     240         2615 : MSVehicle::WaitingTimeCollector::getState() const {
     241         2615 :     std::ostringstream state;
     242         2615 :     state << myMemorySize << " " << myWaitingIntervals.size();
     243         3545 :     for (const auto& interval : myWaitingIntervals) {
     244         1860 :         state << " " << interval.first << " " << interval.second;
     245              :     }
     246         2615 :     return state.str();
     247         2615 : }
     248              : 
     249              : 
     250              : void
     251         3472 : MSVehicle::WaitingTimeCollector::setState(const std::string& state) {
     252         3472 :     std::istringstream is(state);
     253              :     int numIntervals;
     254              :     SUMOTime begin, end;
     255         3472 :     is >> myMemorySize >> numIntervals;
     256         5182 :     while (numIntervals-- > 0) {
     257              :         is >> begin >> end;
     258         1710 :         myWaitingIntervals.emplace_back(begin, end);
     259              :     }
     260         3472 : }
     261              : 
     262              : 
     263              : /* -------------------------------------------------------------------------
     264              :  * methods of MSVehicle::Influencer::GapControlState
     265              :  * ----------------------------------------------------------------------- */
     266              : void
     267           30 : MSVehicle::Influencer::GapControlVehStateListener::vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& /*info*/) {
     268              : //    std::cout << "GapControlVehStateListener::vehicleStateChanged() vehicle=" << vehicle->getID() << ", to=" << to << std::endl;
     269           30 :     switch (to) {
     270            4 :         case MSNet::VehicleState::STARTING_TELEPORT:
     271              :         case MSNet::VehicleState::ARRIVED:
     272              :         case MSNet::VehicleState::STARTING_PARKING: {
     273              :             // Vehicle left road
     274              : //         Look up reference vehicle in refVehMap and in case deactivate corresponding gap control
     275            4 :             const MSVehicle* msVeh = static_cast<const MSVehicle*>(vehicle);
     276              : //        std::cout << "GapControlVehStateListener::vehicleStateChanged() vehicle=" << vehicle->getID() << " left the road." << std::endl;
     277            4 :             if (GapControlState::refVehMap.find(msVeh) != end(GapControlState::refVehMap)) {
     278              : //            std::cout << "GapControlVehStateListener::deactivating ref vehicle=" << vehicle->getID() << std::endl;
     279            4 :                 GapControlState::refVehMap[msVeh]->deactivate();
     280              :             }
     281              :         }
     282            4 :         break;
     283           30 :         default:
     284              :         {};
     285              :             // do nothing, vehicle still on road
     286              :     }
     287           30 : }
     288              : 
     289              : std::map<const MSVehicle*, MSVehicle::Influencer::GapControlState*>
     290              : MSVehicle::Influencer::GapControlState::refVehMap;
     291              : 
     292              : MSVehicle::Influencer::GapControlVehStateListener* MSVehicle::Influencer::GapControlState::myVehStateListener(nullptr);
     293              : 
     294           58 : MSVehicle::Influencer::GapControlState::GapControlState() :
     295           58 :     tauOriginal(-1), tauCurrent(-1), tauTarget(-1), addGapCurrent(-1), addGapTarget(-1),
     296           58 :     remainingDuration(-1), changeRate(-1), maxDecel(-1), referenceVeh(nullptr), active(false), gapAttained(false), prevLeader(nullptr),
     297           58 :     lastUpdate(-1), timeHeadwayIncrement(0.0), spaceHeadwayIncrement(0.0) {}
     298              : 
     299              : 
     300           58 : MSVehicle::Influencer::GapControlState::~GapControlState() {
     301           58 :     deactivate();
     302           58 : }
     303              : 
     304              : void
     305           58 : MSVehicle::Influencer::GapControlState::init() {
     306           58 :     if (MSNet::hasInstance()) {
     307           58 :         if (myVehStateListener == nullptr) {
     308              :             //std::cout << "GapControlState::init()" << std::endl;
     309           58 :             myVehStateListener = new GapControlVehStateListener();
     310           58 :             MSNet::getInstance()->addVehicleStateListener(myVehStateListener);
     311              :         }
     312              :     } else {
     313            0 :         WRITE_ERROR("MSVehicle::Influencer::GapControlState::init(): No MSNet instance found!")
     314              :     }
     315           58 : }
     316              : 
     317              : void
     318        35182 : MSVehicle::Influencer::GapControlState::cleanup() {
     319        35182 :     if (myVehStateListener != nullptr) {
     320           58 :         MSNet::getInstance()->removeVehicleStateListener(myVehStateListener);
     321           58 :         delete myVehStateListener;
     322           58 :         myVehStateListener = nullptr;
     323              :     }
     324        35182 : }
     325              : 
     326              : void
     327           58 : MSVehicle::Influencer::GapControlState::activate(double tauOrig, double tauNew, double additionalGap, double dur, double rate, double decel, const MSVehicle* refVeh) {
     328           58 :     if (MSGlobals::gUseMesoSim) {
     329            0 :         WRITE_ERROR(TL("No gap control available for meso."))
     330              :     } else {
     331              :         // always deactivate control before activating (triggers clean-up of refVehMap)
     332              : //        std::cout << "activate gap control with refVeh=" << (refVeh==nullptr? "NULL" : refVeh->getID()) << std::endl;
     333           58 :         tauOriginal = tauOrig;
     334           58 :         tauCurrent = tauOrig;
     335           58 :         tauTarget = tauNew;
     336           58 :         addGapCurrent = 0.0;
     337           58 :         addGapTarget = additionalGap;
     338           58 :         remainingDuration = dur;
     339           58 :         changeRate = rate;
     340           58 :         maxDecel = decel;
     341           58 :         referenceVeh = refVeh;
     342           58 :         active = true;
     343           58 :         gapAttained = false;
     344           58 :         prevLeader = nullptr;
     345           58 :         lastUpdate = SIMSTEP - DELTA_T;
     346           58 :         timeHeadwayIncrement = changeRate * TS * (tauTarget - tauOriginal);
     347           58 :         spaceHeadwayIncrement = changeRate * TS * addGapTarget;
     348              : 
     349           58 :         if (referenceVeh != nullptr) {
     350              :             // Add refVeh to refVehMap
     351           12 :             GapControlState::refVehMap[referenceVeh] = this;
     352              :         }
     353              :     }
     354           58 : }
     355              : 
     356              : void
     357          116 : MSVehicle::Influencer::GapControlState::deactivate() {
     358          116 :     active = false;
     359          116 :     if (referenceVeh != nullptr) {
     360              :         // Remove corresponding refVehMapEntry if appropriate
     361           12 :         GapControlState::refVehMap.erase(referenceVeh);
     362           12 :         referenceVeh = nullptr;
     363              :     }
     364          116 : }
     365              : 
     366              : 
     367              : /* -------------------------------------------------------------------------
     368              :  * methods of MSVehicle::Influencer
     369              :  * ----------------------------------------------------------------------- */
     370         3437 : MSVehicle::Influencer::Influencer() :
     371              :     myGapControlState(nullptr),
     372         3437 :     myOriginalSpeed(-1),
     373         3437 :     myLatDist(0),
     374         3437 :     mySpeedAdaptationStarted(true),
     375         3437 :     myConsiderSafeVelocity(true),
     376         3437 :     myConsiderSpeedLimit(true),
     377         3437 :     myConsiderMaxAcceleration(true),
     378         3437 :     myConsiderMaxDeceleration(true),
     379         3437 :     myRespectJunctionPriority(true),
     380         3437 :     myEmergencyBrakeRedLight(true),
     381         3437 :     myRespectJunctionLeaderPriority(true),
     382         3437 :     myLastRemoteAccess(-TIME2STEPS(20)),
     383         3437 :     myStrategicLC(LC_NOCONFLICT),
     384         3437 :     myCooperativeLC(LC_NOCONFLICT),
     385         3437 :     mySpeedGainLC(LC_NOCONFLICT),
     386         3437 :     myRightDriveLC(LC_NOCONFLICT),
     387         3437 :     mySublaneLC(LC_NOCONFLICT),
     388         3437 :     myTraciLaneChangePriority(LCP_URGENT),
     389         3437 :     myTraCISignals(-1)
     390         3437 : {}
     391              : 
     392              : 
     393        10311 : MSVehicle::Influencer::~Influencer() {}
     394              : 
     395              : void
     396           58 : MSVehicle::Influencer::init() {
     397           58 :     GapControlState::init();
     398           58 : }
     399              : 
     400              : void
     401        35182 : MSVehicle::Influencer::cleanup() {
     402        35182 :     GapControlState::cleanup();
     403        35182 : }
     404              : 
     405              : void
     406        43300 : MSVehicle::Influencer::setSpeedTimeLine(const std::vector<std::pair<SUMOTime, double> >& speedTimeLine) {
     407        43300 :     mySpeedAdaptationStarted = true;
     408        43300 :     mySpeedTimeLine = speedTimeLine;
     409        43300 : }
     410              : 
     411              : void
     412           58 : MSVehicle::Influencer::activateGapController(double originalTau, double newTimeHeadway, double newSpaceHeadway, double duration, double changeRate, double maxDecel, MSVehicle* refVeh) {
     413           58 :     if (myGapControlState == nullptr) {
     414           58 :         myGapControlState = std::make_shared<GapControlState>();
     415           58 :         init(); // only does things on first call
     416              :     }
     417           58 :     myGapControlState->activate(originalTau, newTimeHeadway, newSpaceHeadway, duration, changeRate, maxDecel, refVeh);
     418           58 : }
     419              : 
     420              : void
     421           10 : MSVehicle::Influencer::deactivateGapController() {
     422           10 :     if (myGapControlState != nullptr && myGapControlState->active) {
     423           10 :         myGapControlState->deactivate();
     424              :     }
     425           10 : }
     426              : 
     427              : void
     428         7604 : MSVehicle::Influencer::setLaneTimeLine(const std::vector<std::pair<SUMOTime, int> >& laneTimeLine) {
     429         7604 :     myLaneTimeLine = laneTimeLine;
     430         7604 : }
     431              : 
     432              : 
     433              : void
     434         8976 : MSVehicle::Influencer::adaptLaneTimeLine(int indexShift) {
     435        19134 :     for (auto& item : myLaneTimeLine) {
     436        10158 :         item.second += indexShift;
     437              :     }
     438         8976 : }
     439              : 
     440              : 
     441              : void
     442         1268 : MSVehicle::Influencer::setSublaneChange(double latDist) {
     443         1268 :     myLatDist = latDist;
     444         1268 : }
     445              : 
     446              : int
     447           68 : MSVehicle::Influencer::getSpeedMode() const {
     448           68 :     return (1 * myConsiderSafeVelocity +
     449           68 :             2 * myConsiderMaxAcceleration +
     450           68 :             4 * myConsiderMaxDeceleration +
     451           68 :             8 * myRespectJunctionPriority +
     452           68 :             16 * myEmergencyBrakeRedLight +
     453           68 :             32 * !myRespectJunctionLeaderPriority + // inverted!
     454           68 :             64 * !myConsiderSpeedLimit // inverted!
     455           68 :            );
     456              : }
     457              : 
     458              : 
     459              : int
     460         1469 : MSVehicle::Influencer::getLaneChangeMode() const {
     461         1469 :     return (1 * myStrategicLC +
     462         1469 :             4 * myCooperativeLC +
     463         1469 :             16 * mySpeedGainLC +
     464         1469 :             64 * myRightDriveLC +
     465         1469 :             256 * myTraciLaneChangePriority +
     466         1469 :             1024 * mySublaneLC);
     467              : }
     468              : 
     469              : SUMOTime
     470           60 : MSVehicle::Influencer::getLaneTimeLineDuration() {
     471              :     SUMOTime duration = -1;
     472          180 :     for (std::vector<std::pair<SUMOTime, int>>::iterator i = myLaneTimeLine.begin(); i != myLaneTimeLine.end(); ++i) {
     473          120 :         if (duration < 0) {
     474           60 :             duration = i->first;
     475              :         } else {
     476           60 :             duration -=  i->first;
     477              :         }
     478              :     }
     479           60 :     return -duration;
     480              : }
     481              : 
     482              : SUMOTime
     483            0 : MSVehicle::Influencer::getLaneTimeLineEnd() {
     484            0 :     if (!myLaneTimeLine.empty()) {
     485            0 :         return myLaneTimeLine.back().first;
     486              :     } else {
     487              :         return -1;
     488              :     }
     489              : }
     490              : 
     491              : 
     492              : double
     493       982880 : MSVehicle::Influencer::influenceSpeed(SUMOTime currentTime, double speed, double vSafe, double vMin, double vMax) {
     494              :     // remove leading commands which are no longer valid
     495       984208 :     while (mySpeedTimeLine.size() == 1 || (mySpeedTimeLine.size() > 1 && currentTime > mySpeedTimeLine[1].first)) {
     496              :         mySpeedTimeLine.erase(mySpeedTimeLine.begin());
     497              :     }
     498              : 
     499       982880 :     if (!(mySpeedTimeLine.size() < 2 || currentTime < mySpeedTimeLine[0].first)) {
     500              :         // Speed advice is active -> compute new speed according to speedTimeLine
     501        53513 :         if (!mySpeedAdaptationStarted) {
     502            0 :             mySpeedTimeLine[0].second = speed;
     503            0 :             mySpeedAdaptationStarted = true;
     504              :         }
     505        53513 :         currentTime += DELTA_T; // start slowing down in the step in which this command was issued (the input value of currentTime still reflects the previous step)
     506       106688 :         const double td = MIN2(1.0, STEPS2TIME(currentTime - mySpeedTimeLine[0].first) / MAX2(TS, STEPS2TIME(mySpeedTimeLine[1].first - mySpeedTimeLine[0].first)));
     507              : 
     508        53513 :         speed = mySpeedTimeLine[0].second - (mySpeedTimeLine[0].second - mySpeedTimeLine[1].second) * td;
     509        53513 :         if (myConsiderSafeVelocity) {
     510              :             speed = MIN2(speed, vSafe);
     511              :         }
     512        53513 :         if (myConsiderMaxAcceleration) {
     513              :             speed = MIN2(speed, vMax);
     514              :         }
     515        53513 :         if (myConsiderMaxDeceleration) {
     516              :             speed = MAX2(speed, vMin);
     517              :         }
     518              :     }
     519       982880 :     return speed;
     520              : }
     521              : 
     522              : double
     523       488774 : MSVehicle::Influencer::gapControlSpeed(SUMOTime currentTime, const SUMOVehicle* veh, double speed, double vSafe, double vMin, double vMax) {
     524              : #ifdef DEBUG_TRACI
     525              :     if DEBUG_COND2(veh) {
     526              :         std::cout << currentTime << " Influencer::gapControlSpeed(): speed=" << speed
     527              :                   << ", vSafe=" << vSafe
     528              :                   << ", vMin=" << vMin
     529              :                   << ", vMax=" << vMax
     530              :                   << std::endl;
     531              :     }
     532              : #endif
     533              :     double gapControlSpeed = speed;
     534       488774 :     if (myGapControlState != nullptr && myGapControlState->active) {
     535              :         // Determine leader and the speed that would be chosen by the gap controller
     536         7772 :         const double currentSpeed = veh->getSpeed();
     537         7772 :         const MSVehicle* msVeh = dynamic_cast<const MSVehicle*>(veh);
     538              :         assert(msVeh != nullptr);
     539         7772 :         const double desiredTargetTimeSpacing = myGapControlState->tauTarget * currentSpeed;
     540              :         std::pair<const MSVehicle*, double> leaderInfo;
     541         7772 :         if (myGapControlState->referenceVeh == nullptr) {
     542              :             // No reference vehicle specified -> use current leader as reference
     543         7340 :             const double brakeGap = msVeh->getBrakeGap(true);
     544        14680 :             leaderInfo = msVeh->getLeader(MAX2(desiredTargetTimeSpacing, myGapControlState->addGapCurrent)  + MAX2(brakeGap, 20.0));
     545              : #ifdef DEBUG_TRACI
     546              :             if DEBUG_COND2(veh) {
     547              :                 std::cout <<  "  ---   no refVeh; myGapControlState->addGapCurrent: " << myGapControlState->addGapCurrent << ", brakeGap: " << brakeGap << " in simstep: " << SIMSTEP << std::endl;
     548              :             }
     549              : #endif
     550              :         } else {
     551              :             // Control gap wrt reference vehicle
     552              :             const MSVehicle* leader = myGapControlState->referenceVeh;
     553          432 :             double dist = msVeh->getDistanceToPosition(leader->getPositionOnLane(), leader->getLane()) - leader->getLength();
     554          432 :             if (dist > 100000) {
     555              :                 // Reference vehicle was not found downstream the ego's route
     556              :                 // Maybe, it is behind the ego vehicle
     557           40 :                 dist = - leader->getDistanceToPosition(msVeh->getPositionOnLane(), msVeh->getLane()) - leader->getLength();
     558              : #ifdef DEBUG_TRACI
     559              :                 if DEBUG_COND2(veh) {
     560              :                     if (dist < -100000) {
     561              :                         // also the ego vehicle is not ahead of the reference vehicle -> no CF-relation
     562              :                         std::cout <<  " Ego and reference vehicle are not in CF relation..." << std::endl;
     563              :                     } else {
     564              :                         std::cout <<  " Reference vehicle is behind ego..." << std::endl;
     565              :                     }
     566              :                 }
     567              : #endif
     568              :             }
     569          432 :             leaderInfo = std::make_pair(leader, dist - msVeh->getVehicleType().getMinGap());
     570              :         }
     571         7772 :         const double fakeDist = MAX2(0.0, leaderInfo.second - myGapControlState->addGapCurrent);
     572              : #ifdef DEBUG_TRACI
     573              :         if DEBUG_COND2(veh) {
     574              :             const double desiredCurrentSpacing = myGapControlState->tauCurrent * currentSpeed;
     575              :             std::cout <<  " Gap control active:"
     576              :                       << " currentSpeed=" << currentSpeed
     577              :                       << ", desiredTargetTimeSpacing=" << desiredTargetTimeSpacing
     578              :                       << ", desiredCurrentSpacing=" << desiredCurrentSpacing
     579              :                       << ", leader=" << (leaderInfo.first == nullptr ? "NULL" : leaderInfo.first->getID())
     580              :                       << ", dist=" << leaderInfo.second
     581              :                       << ", fakeDist=" << fakeDist
     582              :                       << ",\n tauOriginal=" << myGapControlState->tauOriginal
     583              :                       << ", tauTarget=" << myGapControlState->tauTarget
     584              :                       << ", tauCurrent=" << myGapControlState->tauCurrent
     585              :                       << std::endl;
     586              :         }
     587              : #endif
     588         7772 :         if (leaderInfo.first != nullptr) {
     589              :             if (myGapControlState->prevLeader != nullptr && myGapControlState->prevLeader != leaderInfo.first) {
     590              :                 // TODO: The leader changed. What to do?
     591              :             }
     592              :             // Remember leader
     593         7772 :             myGapControlState->prevLeader = leaderInfo.first;
     594              : 
     595              :             // Calculate desired following speed assuming the alternative headway time
     596         7772 :             MSCFModel* cfm = (MSCFModel*) & (msVeh->getVehicleType().getCarFollowModel());
     597         7772 :             const double origTau = cfm->getHeadwayTime();
     598         7772 :             cfm->setHeadwayTime(myGapControlState->tauCurrent);
     599         7772 :             gapControlSpeed = MIN2(gapControlSpeed,
     600         7772 :                                    cfm->followSpeed(msVeh, currentSpeed, fakeDist, leaderInfo.first->getSpeed(), leaderInfo.first->getCurrentApparentDecel(), leaderInfo.first));
     601         7772 :             cfm->setHeadwayTime(origTau);
     602              : #ifdef DEBUG_TRACI
     603              :             if DEBUG_COND2(veh) {
     604              :                 std::cout << " -> gapControlSpeed=" << gapControlSpeed;
     605              :                 if (myGapControlState->maxDecel > 0) {
     606              :                     std::cout << ", with maxDecel bound: " << MAX2(gapControlSpeed, currentSpeed - TS * myGapControlState->maxDecel);
     607              :                 }
     608              :                 std::cout << std::endl;
     609              :             }
     610              : #endif
     611         7772 :             if (myGapControlState->maxDecel > 0) {
     612         2568 :                 gapControlSpeed = MAX2(gapControlSpeed, currentSpeed - TS * myGapControlState->maxDecel);
     613              :             }
     614              :         }
     615              : 
     616              :         // Update gap controller
     617              :         // Check (1) if the gap control has established the desired gap,
     618              :         // and (2) if it has maintained active for the given duration afterwards
     619         7772 :         if (myGapControlState->lastUpdate < currentTime) {
     620              : #ifdef DEBUG_TRACI
     621              :             if DEBUG_COND2(veh) {
     622              :                 std::cout << " Updating GapControlState." << std::endl;
     623              :             }
     624              : #endif
     625         7772 :             if (myGapControlState->tauCurrent == myGapControlState->tauTarget && myGapControlState->addGapCurrent == myGapControlState->addGapTarget) {
     626         2990 :                 if (!myGapControlState->gapAttained) {
     627              :                     // Check if the desired gap was established (add the POSITION_EPS to avoid infinite asymptotic behavior without having established the gap)
     628         4176 :                     myGapControlState->gapAttained = leaderInfo.first == nullptr ||  leaderInfo.second > MAX2(desiredTargetTimeSpacing, myGapControlState->addGapTarget) - POSITION_EPS;
     629              : #ifdef DEBUG_TRACI
     630              :                     if DEBUG_COND2(veh) {
     631              :                         if (myGapControlState->gapAttained) {
     632              :                             std::cout << "   Target gap was established." << std::endl;
     633              :                         }
     634              :                     }
     635              : #endif
     636              :                 } else {
     637              :                     // Count down remaining time if desired gap was established
     638          924 :                     myGapControlState->remainingDuration -= TS;
     639              : #ifdef DEBUG_TRACI
     640              :                     if DEBUG_COND2(veh) {
     641              :                         std::cout << "   Gap control remaining duration: " << myGapControlState->remainingDuration << std::endl;
     642              :                     }
     643              : #endif
     644          924 :                     if (myGapControlState->remainingDuration <= 0) {
     645              : #ifdef DEBUG_TRACI
     646              :                         if DEBUG_COND2(veh) {
     647              :                             std::cout << "   Gap control duration expired, deactivating control." << std::endl;
     648              :                         }
     649              : #endif
     650              :                         // switch off gap control
     651           44 :                         myGapControlState->deactivate();
     652              :                     }
     653              :                 }
     654              :             } else {
     655              :                 // Adjust current headway values
     656         4782 :                 myGapControlState->tauCurrent = MIN2(myGapControlState->tauCurrent + myGapControlState->timeHeadwayIncrement, myGapControlState->tauTarget);
     657         5160 :                 myGapControlState->addGapCurrent = MIN2(myGapControlState->addGapCurrent + myGapControlState->spaceHeadwayIncrement, myGapControlState->addGapTarget);
     658              :             }
     659              :         }
     660         7772 :         if (myConsiderSafeVelocity) {
     661              :             gapControlSpeed = MIN2(gapControlSpeed, vSafe);
     662              :         }
     663         7772 :         if (myConsiderMaxAcceleration) {
     664              :             gapControlSpeed = MIN2(gapControlSpeed, vMax);
     665              :         }
     666         7772 :         if (myConsiderMaxDeceleration) {
     667              :             gapControlSpeed = MAX2(gapControlSpeed, vMin);
     668              :         }
     669              :         return MIN2(speed, gapControlSpeed);
     670              :     } else {
     671              :         return speed;
     672              :     }
     673              : }
     674              : 
     675              : double
     676         7135 : MSVehicle::Influencer::getOriginalSpeed() const {
     677         7135 :     return myOriginalSpeed;
     678              : }
     679              : 
     680              : void
     681       494106 : MSVehicle::Influencer::setOriginalSpeed(double speed) {
     682       494106 :     myOriginalSpeed = speed;
     683       494106 : }
     684              : 
     685              : 
     686              : int
     687      2800329 : MSVehicle::Influencer::influenceChangeDecision(const SUMOTime currentTime, const MSEdge& currentEdge, const int currentLaneIndex, int state) {
     688              :     // remove leading commands which are no longer valid
     689      2800581 :     while (myLaneTimeLine.size() == 1 || (myLaneTimeLine.size() > 1 && currentTime > myLaneTimeLine[1].first)) {
     690              :         myLaneTimeLine.erase(myLaneTimeLine.begin());
     691              :     }
     692              :     ChangeRequest changeRequest = REQUEST_NONE;
     693              :     // do nothing if the time line does not apply for the current time
     694      2800329 :     if (myLaneTimeLine.size() >= 2 && currentTime >= myLaneTimeLine[0].first) {
     695       173425 :         const int destinationLaneIndex = myLaneTimeLine[1].second;
     696       173425 :         if (destinationLaneIndex < (int)currentEdge.getLanes().size()) {
     697       173133 :             if (currentLaneIndex > destinationLaneIndex) {
     698              :                 changeRequest = REQUEST_RIGHT;
     699       172258 :             } else if (currentLaneIndex < destinationLaneIndex) {
     700              :                 changeRequest = REQUEST_LEFT;
     701              :             } else {
     702              :                 changeRequest = REQUEST_HOLD;
     703              :             }
     704          292 :         } else if (currentEdge.getLanes().back()->getOpposite() != nullptr) { // change to opposite direction driving
     705              :             changeRequest = REQUEST_LEFT;
     706          292 :             state = state | LCA_TRACI;
     707              :         }
     708              :     }
     709              :     // check whether the current reason shall be canceled / overridden
     710      2800329 :     if ((state & LCA_WANTS_LANECHANGE_OR_STAY) != 0) {
     711              :         // flags for the current reason
     712              :         LaneChangeMode mode = LC_NEVER;
     713      1586204 :         if ((state & LCA_TRACI) != 0 && myLatDist != 0) {
     714              :             // security checks
     715         2380 :             if ((myTraciLaneChangePriority == LCP_ALWAYS)
     716          552 :                     || (myTraciLaneChangePriority == LCP_NOOVERLAP && (state & LCA_OVERLAPPING) == 0)) {
     717         2252 :                 state &= ~(LCA_BLOCKED | LCA_OVERLAPPING);
     718              :             }
     719              :             // continue sublane change manoeuvre
     720         2380 :             return state;
     721      1583824 :         } else if ((state & LCA_STRATEGIC) != 0) {
     722       484991 :             mode = myStrategicLC;
     723      1098833 :         } else if ((state & LCA_COOPERATIVE) != 0) {
     724           94 :             mode = myCooperativeLC;
     725      1098739 :         } else if ((state & LCA_SPEEDGAIN) != 0) {
     726        41384 :             mode = mySpeedGainLC;
     727      1057355 :         } else if ((state & LCA_KEEPRIGHT) != 0) {
     728         6122 :             mode = myRightDriveLC;
     729      1051233 :         } else if ((state & LCA_SUBLANE) != 0) {
     730      1051231 :             mode = mySublaneLC;
     731            2 :         } else if ((state & LCA_TRACI) != 0) {
     732              :             mode = LC_NEVER;
     733              :         } else {
     734            0 :             WRITE_WARNINGF(TL("Lane change model did not provide a reason for changing (state=%, time=%\n"), toString(state), time2string(currentTime));
     735              :         }
     736      1583822 :         if (mode == LC_NEVER) {
     737              :             // cancel all lcModel requests
     738              :             state &= ~LCA_WANTS_LANECHANGE_OR_STAY;
     739        41909 :             state &= ~LCA_URGENT;
     740        41909 :             if (changeRequest == REQUEST_NONE) {
     741              :                 // also remove all reasons except TRACI
     742        41442 :                 state &= ~LCA_CHANGE_REASONS | LCA_TRACI;
     743              :             }
     744      1541915 :         } else if (mode == LC_NOCONFLICT && changeRequest != REQUEST_NONE) {
     745         5623 :             if (
     746         5623 :                 ((state & LCA_LEFT) != 0 && changeRequest != REQUEST_LEFT) ||
     747         5391 :                 ((state & LCA_RIGHT) != 0 && changeRequest != REQUEST_RIGHT) ||
     748         4943 :                 ((state & LCA_STAY) != 0 && changeRequest != REQUEST_HOLD)) {
     749              :                 // cancel conflicting lcModel request
     750              :                 state &= ~LCA_WANTS_LANECHANGE_OR_STAY;
     751          827 :                 state &= ~LCA_URGENT;
     752              :             }
     753      1536292 :         } else if (mode == LC_ALWAYS) {
     754              :             // ignore any TraCI requests
     755              :             return state;
     756              :         }
     757              :     }
     758              :     // apply traci requests
     759      2792748 :     if (changeRequest == REQUEST_NONE) {
     760      2624904 :         return state;
     761              :     } else {
     762       173035 :         state |= LCA_TRACI;
     763              :         // security checks
     764       173035 :         if ((myTraciLaneChangePriority == LCP_ALWAYS)
     765       171179 :                 || (myTraciLaneChangePriority == LCP_NOOVERLAP && (state & LCA_OVERLAPPING) == 0)) {
     766         2250 :             state &= ~(LCA_BLOCKED | LCA_OVERLAPPING);
     767              :         }
     768       173035 :         if (changeRequest != REQUEST_HOLD && myTraciLaneChangePriority != LCP_OPPORTUNISTIC) {
     769         2094 :             state |= LCA_URGENT;
     770              :         }
     771         2123 :         switch (changeRequest) {
     772              :             case REQUEST_HOLD:
     773       170912 :                 return state | LCA_STAY;
     774         1328 :             case REQUEST_LEFT:
     775         1328 :                 return state | LCA_LEFT;
     776          795 :             case REQUEST_RIGHT:
     777          795 :                 return state | LCA_RIGHT;
     778              :             default:
     779              :                 throw ProcessError(TL("should not happen"));
     780              :         }
     781              :     }
     782              : }
     783              : 
     784              : 
     785              : double
     786          364 : MSVehicle::Influencer::changeRequestRemainingSeconds(const SUMOTime currentTime) const {
     787              :     assert(myLaneTimeLine.size() >= 2);
     788              :     assert(currentTime >= myLaneTimeLine[0].first);
     789          364 :     return STEPS2TIME(myLaneTimeLine[1].first - currentTime);
     790              : }
     791              : 
     792              : 
     793              : void
     794         5618 : MSVehicle::Influencer::setSpeedMode(int speedMode) {
     795         5618 :     myConsiderSafeVelocity = ((speedMode & 1) != 0);
     796         5618 :     myConsiderMaxAcceleration = ((speedMode & 2) != 0);
     797         5618 :     myConsiderMaxDeceleration = ((speedMode & 4) != 0);
     798         5618 :     myRespectJunctionPriority = ((speedMode & 8) != 0);
     799         5618 :     myEmergencyBrakeRedLight = ((speedMode & 16) != 0);
     800         5618 :     myRespectJunctionLeaderPriority = ((speedMode & 32) == 0); // inverted!
     801         5618 :     myConsiderSpeedLimit = ((speedMode & 64) == 0); // inverted!
     802         5618 : }
     803              : 
     804              : 
     805              : void
     806        18369 : MSVehicle::Influencer::setLaneChangeMode(int value) {
     807        18369 :     myStrategicLC = (LaneChangeMode)(value & (1 + 2));
     808        18369 :     myCooperativeLC = (LaneChangeMode)((value & (4 + 8)) >> 2);
     809        18369 :     mySpeedGainLC = (LaneChangeMode)((value & (16 + 32)) >> 4);
     810        18369 :     myRightDriveLC = (LaneChangeMode)((value & (64 + 128)) >> 6);
     811        18369 :     myTraciLaneChangePriority = (TraciLaneChangePriority)((value & (256 + 512)) >> 8);
     812        18369 :     mySublaneLC = (LaneChangeMode)((value & (1024 + 2048)) >> 10);
     813        18369 : }
     814              : 
     815              : 
     816              : void
     817         7347 : MSVehicle::Influencer::setRemoteControlled(Position xyPos, MSLane* l, double pos, double posLat, double angle, int edgeOffset, const ConstMSEdgeVector& route, SUMOTime t) {
     818         7347 :     myRemoteXYPos = xyPos;
     819         7347 :     myRemoteLane = l;
     820         7347 :     myRemotePos = pos;
     821         7347 :     myRemotePosLat = posLat;
     822         7347 :     myRemoteAngle = angle;
     823         7347 :     myRemoteEdgeOffset = edgeOffset;
     824         7347 :     myRemoteRoute = route;
     825         7347 :     myLastRemoteAccess = t;
     826         7347 : }
     827              : 
     828              : 
     829              : bool
     830      1011468 : MSVehicle::Influencer::isRemoteControlled() const {
     831      1011468 :     return myLastRemoteAccess == MSNet::getInstance()->getCurrentTimeStep();
     832              : }
     833              : 
     834              : 
     835              : bool
     836       465620 : MSVehicle::Influencer::isRemoteAffected(SUMOTime t) const {
     837       465620 :     return myLastRemoteAccess >= t - TIME2STEPS(10);
     838              : }
     839              : 
     840              : 
     841              : void
     842       488774 : MSVehicle::Influencer::updateRemoteControlRoute(MSVehicle* v) {
     843       488774 :     if (myRemoteRoute.size() != 0 && myRemoteRoute != v->getRoute().getEdges()) {
     844              :         // only replace route at this time if the vehicle is moving with the flow
     845           79 :         const bool isForward = v->getLane() != 0 && &v->getLane()->getEdge() == myRemoteRoute[0];
     846              : #ifdef DEBUG_REMOTECONTROL
     847              :         std::cout << SIMSTEP << " updateRemoteControlRoute veh=" << v->getID() << " old=" << toString(v->getRoute().getEdges()) << " new=" << toString(myRemoteRoute) << " fwd=" << isForward << "\n";
     848              : #endif
     849              :         if (isForward) {
     850           12 :             v->replaceRouteEdges(myRemoteRoute, -1, 0, "traci:moveToXY", true);
     851           12 :             v->updateBestLanes();
     852              :         }
     853              :     }
     854       488774 : }
     855              : 
     856              : 
     857              : void
     858         7327 : MSVehicle::Influencer::postProcessRemoteControl(MSVehicle* v) {
     859         7327 :     const bool wasOnRoad = v->isOnRoad();
     860         7327 :     const bool withinLane = myRemoteLane != nullptr && fabs(myRemotePosLat) < 0.5 * (myRemoteLane->getWidth() + v->getVehicleType().getWidth());
     861         7327 :     const bool keepLane = wasOnRoad && v->getLane() == myRemoteLane;
     862         7327 :     if (v->isOnRoad() && !(keepLane && withinLane)) {
     863          150 :         if (myRemoteLane != nullptr && &v->getLane()->getEdge() == &myRemoteLane->getEdge()) {
     864              :             // correct odometer which gets incremented via onRemovalFromNet->leaveLane
     865           60 :             v->myOdometer -= v->getLane()->getLength();
     866              :         }
     867          150 :         v->onRemovalFromNet(MSMoveReminder::NOTIFICATION_TELEPORT);
     868          150 :         v->getMutableLane()->removeVehicle(v, MSMoveReminder::NOTIFICATION_TELEPORT, false);
     869              :     }
     870         7327 :     if (myRemoteRoute.size() != 0 && myRemoteRoute != v->getRoute().getEdges()) {
     871              :         // needed for the insertion step
     872              : #ifdef DEBUG_REMOTECONTROL
     873              :         std::cout << SIMSTEP << " postProcessRemoteControl veh=" << v->getID()
     874              :                   << "\n  oldLane=" << Named::getIDSecure(v->getLane())
     875              :                   << " oldRoute=" << toString(v->getRoute().getEdges())
     876              :                   << "\n  newLane=" << Named::getIDSecure(myRemoteLane)
     877              :                   << " newRoute=" << toString(myRemoteRoute)
     878              :                   << " newRouteEdge=" << myRemoteRoute[myRemoteEdgeOffset]->getID()
     879              :                   << "\n";
     880              : #endif
     881              :         // clear any prior stops because they cannot apply to the new route
     882           79 :         const_cast<SUMOVehicleParameter&>(v->getParameter()).stops.clear();
     883          158 :         v->replaceRouteEdges(myRemoteRoute, -1, 0, "traci:moveToXY", true);
     884              :         myRemoteRoute.clear();
     885              :     }
     886         7327 :     v->myCurrEdge = v->getRoute().begin() + myRemoteEdgeOffset;
     887         7327 :     if (myRemoteLane != nullptr && myRemotePos > myRemoteLane->getLength()) {
     888            0 :         myRemotePos = myRemoteLane->getLength();
     889              :     }
     890         7327 :     if (myRemoteLane != nullptr && withinLane) {
     891         7185 :         if (keepLane) {
     892              :             // TODO this handles only the case when the new vehicle is completely on the edge
     893         7012 :             const bool needFurtherUpdate = v->myState.myPos < v->getVehicleType().getLength() && myRemotePos >= v->getVehicleType().getLength();
     894         7012 :             v->myState.myPos = myRemotePos;
     895         7012 :             v->myState.myPosLat = myRemotePosLat;
     896         7012 :             if (needFurtherUpdate) {
     897            4 :                 v->myState.myBackPos = v->updateFurtherLanes(v->myFurtherLanes, v->myFurtherLanesPosLat, std::vector<MSLane*>());
     898              :             }
     899              :         } else {
     900          173 :             MSMoveReminder::Notification notify = v->getDeparture() == NOT_YET_DEPARTED
     901          173 :                                                   ? MSMoveReminder::NOTIFICATION_DEPARTED
     902              :                                                   : MSMoveReminder::NOTIFICATION_TELEPORT_ARRIVED;
     903          173 :             if (!v->isOnRoad()) {
     904          173 :                 MSVehicleTransfer::getInstance()->remove(v);  // TODO may need optimization, this is linear in the number of vehicles in transfer
     905              :             }
     906          173 :             myRemoteLane->forceVehicleInsertion(v, myRemotePos, notify, myRemotePosLat);
     907          173 :             v->updateBestLanes();
     908              :         }
     909         7185 :         if (!wasOnRoad) {
     910           49 :             v->drawOutsideNetwork(false);
     911              :         }
     912              :         //std::cout << "on road network p=" << myRemoteXYPos << " a=" << myRemoteAngle << " l=" << Named::getIDSecure(myRemoteLane) << " pos=" << myRemotePos << " posLat=" << myRemotePosLat << "\n";
     913         7185 :         myRemoteLane->requireCollisionCheck();
     914              :     } else {
     915          142 :         if (v->getDeparture() == NOT_YET_DEPARTED) {
     916            4 :             v->onDepart();
     917              :         }
     918          142 :         v->drawOutsideNetwork(true);
     919              :         // see updateState
     920          142 :         double vNext = v->processTraCISpeedControl(
     921          142 :                            v->getMaxSpeed(), v->getSpeed());
     922          142 :         v->setBrakingSignals(vNext);
     923          142 :         v->myState.myPreviousSpeed = v->getSpeed();
     924          142 :         v->myAcceleration = SPEED2ACCEL(vNext - v->getSpeed());
     925          142 :         v->myState.mySpeed = vNext;
     926          142 :         v->updateWaitingTime(vNext);
     927              :         //std::cout << "outside network p=" << myRemoteXYPos << " a=" << myRemoteAngle << " l=" << Named::getIDSecure(myRemoteLane) << "\n";
     928              :     }
     929              :     // ensure that the position is correct (i.e. when the lanePosition is ambiguous at corners)
     930         7327 :     v->setRemoteState(myRemoteXYPos);
     931         7327 :     v->setAngle(GeomHelper::fromNaviDegree(myRemoteAngle));
     932         7327 : }
     933              : 
     934              : 
     935              : double
     936         7308 : MSVehicle::Influencer::implicitSpeedRemote(const MSVehicle* veh, double oldSpeed) {
     937         7308 :     if (veh->getPosition() == Position::INVALID) {
     938            8 :         return oldSpeed;
     939              :     }
     940         7300 :     double dist = veh->getPosition().distanceTo2D(myRemoteXYPos);
     941         7300 :     if (myRemoteLane != nullptr) {
     942              :         // if the vehicles is frequently placed on a new edge, the route may
     943              :         // consist only of a single edge. In this case the new edge may not be
     944              :         // on the route so distAlongRoute will be double::max.
     945              :         // In this case we still want a sensible speed value
     946         7186 :         const double distAlongRoute = veh->getDistanceToPosition(myRemotePos, myRemoteLane);
     947         7186 :         if (distAlongRoute != std::numeric_limits<double>::max()) {
     948              :             dist = distAlongRoute;
     949              :         }
     950              :     }
     951              :     //std::cout << SIMTIME << " veh=" << veh->getID() << " oldPos=" << veh->getPosition() << " traciPos=" << myRemoteXYPos << " dist=" << dist << "\n";
     952         7300 :     const double minSpeed = myConsiderMaxDeceleration ?
     953         4032 :                             veh->getCarFollowModel().minNextSpeedEmergency(oldSpeed, veh) : 0;
     954         7300 :     const double maxSpeed = (myRemoteLane != nullptr
     955         7300 :                              ? myRemoteLane->getVehicleMaxSpeed(veh)
     956          114 :                              : (veh->getLane() != nullptr
     957          114 :                                 ? veh->getLane()->getVehicleMaxSpeed(veh)
     958            4 :                                 : veh->getMaxSpeed()));
     959         7300 :     return MIN2(maxSpeed, MAX2(minSpeed, DIST2SPEED(dist)));
     960              : }
     961              : 
     962              : 
     963              : double
     964         7174 : MSVehicle::Influencer::implicitDeltaPosRemote(const MSVehicle* veh) {
     965              :     double dist = 0;
     966         7174 :     if (myRemoteLane == nullptr) {
     967            5 :         dist = veh->getPosition().distanceTo2D(myRemoteXYPos);
     968              :     } else {
     969              :         // if the vehicles is frequently placed on a new edge, the route may
     970              :         // consist only of a single edge. In this case the new edge may not be
     971              :         // on the route so getDistanceToPosition will return double::max.
     972              :         // In this case we would rather not move the vehicle in executeMove
     973              :         // (updateState) as it would result in emergency braking
     974         7169 :         dist = veh->getDistanceToPosition(myRemotePos, myRemoteLane);
     975              :     }
     976         7174 :     if (dist == std::numeric_limits<double>::max()) {
     977              :         return 0;
     978              :     } else {
     979         6953 :         if (DIST2SPEED(dist) > veh->getMaxSpeed() * 1.1) {
     980           42 :             WRITE_WARNINGF(TL("Vehicle '%' moved by TraCI from % to % (dist %) with implied speed of % (exceeding maximum speed %). time=%."),
     981              :                            veh->getID(), veh->getPosition(), myRemoteXYPos, dist, DIST2SPEED(dist), veh->getMaxSpeed(), time2string(SIMSTEP));
     982              :             // some sanity check here
     983           14 :             dist = MIN2(dist, SPEED2DIST(veh->getMaxSpeed() * 2));
     984              :         }
     985         6953 :         return dist;
     986              :     }
     987              : }
     988              : 
     989              : 
     990              : /* -------------------------------------------------------------------------
     991              :  * MSVehicle-methods
     992              :  * ----------------------------------------------------------------------- */
     993      4504622 : MSVehicle::MSVehicle(SUMOVehicleParameter* pars, ConstMSRoutePtr route,
     994      4504622 :                      MSVehicleType* type, const double speedFactor) :
     995              :     MSBaseVehicle(pars, route, type, speedFactor),
     996      4504622 :     myWaitingTime(0),
     997      4504622 :     myWaitingTimeCollector(),
     998      4504622 :     myTimeLoss(0),
     999      4504622 :     myState(0, 0, 0, 0, 0),
    1000      4504622 :     myDriverState(nullptr),
    1001      4504622 :     myActionStep(true),
    1002      4504622 :     myLastActionTime(0),
    1003      4504622 :     myLane(nullptr),
    1004      4504622 :     myLaneChangeModel(nullptr),
    1005      4504622 :     myLastBestLanesEdge(nullptr),
    1006      4504622 :     myLastBestLanesInternalLane(nullptr),
    1007      4504622 :     myAcceleration(0),
    1008              :     myNextTurn(0., nullptr),
    1009      4504622 :     mySignals(0),
    1010      4504622 :     myAmOnNet(false),
    1011      4504622 :     myAmIdling(false),
    1012      4504622 :     myHaveToWaitOnNextLink(false),
    1013      4504622 :     myAngle(0),
    1014      4504622 :     myRawAngle(0),
    1015      4504622 :     myLastAngle(INVALID_DOUBLE),
    1016      4504622 :     myStopDist(std::numeric_limits<double>::max()),
    1017      4504622 :     myStopSpeed(std::numeric_limits<double>::max()),
    1018      4504622 :     myCollisionImmunity(-1),
    1019      4504622 :     myCachedPosition(Position::INVALID),
    1020      4504622 :     myJunctionEntryTime(SUMOTime_MAX),
    1021      4504622 :     myJunctionEntryTimeNeverYield(SUMOTime_MAX),
    1022      4504622 :     myJunctionConflictEntryTime(SUMOTime_MAX),
    1023      4504622 :     myTimeSinceStartup(TIME2STEPS(3600 * 24)),
    1024      4504622 :     myHaveStoppedFor(nullptr),
    1025     13513866 :     myInfluencer(nullptr) {
    1026      4504622 :     myCFVariables = type->getCarFollowModel().createVehicleVariables();
    1027      4504622 :     myNextDriveItem = myLFLinkLanes.begin();
    1028      4504622 : }
    1029              : 
    1030              : 
    1031      8366610 : MSVehicle::~MSVehicle() {
    1032      4504546 :     cleanupParkingReservation();
    1033      4504546 :     cleanupFurtherLanes();
    1034      4504546 :     delete myLaneChangeModel;
    1035      4504546 :     if (myType->isVehicleSpecific()) {
    1036          303 :         MSNet::getInstance()->getVehicleControl().removeVType(myType);
    1037              :     }
    1038      4504546 :     delete myInfluencer;
    1039      4504546 :     delete myCFVariables;
    1040     12871156 : }
    1041              : 
    1042              : 
    1043              : void
    1044      4505108 : MSVehicle::cleanupFurtherLanes() {
    1045      4507595 :     for (MSLane* further : myFurtherLanes) {
    1046         2487 :         further->resetPartialOccupation(this);
    1047         2487 :         if (further->getBidiLane() != nullptr
    1048         2487 :                 && (!isRailway(getVClass()) || (further->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    1049            0 :             further->getBidiLane()->resetPartialOccupation(this);
    1050              :         }
    1051              :     }
    1052      4505108 :     if (myLaneChangeModel != nullptr) {
    1053      4505074 :         removeApproachingInformation(myLFLinkLanes);
    1054      4505074 :         myLaneChangeModel->cleanupShadowLane();
    1055      4505074 :         myLaneChangeModel->cleanupTargetLane();
    1056              :         // still needed when calling resetPartialOccupation (getShadowLane) and when removing
    1057              :         // approach information from parallel links
    1058              :     }
    1059              :     myFurtherLanes.clear();
    1060              :     myFurtherLanesPosLat.clear();
    1061      4505108 : }
    1062              : 
    1063              : 
    1064              : void
    1065      3367049 : MSVehicle::onRemovalFromNet(const MSMoveReminder::Notification reason) {
    1066              : #ifdef DEBUG_ACTIONSTEPS
    1067              :     if (DEBUG_COND) {
    1068              :         std::cout << SIMTIME << " Removing vehicle '" << getID() << "' (reason: " << toString(reason) << ")" << std::endl;
    1069              :     }
    1070              : #endif
    1071      3367049 :     MSVehicleTransfer::getInstance()->remove(this);
    1072      3367049 :     removeApproachingInformation(myLFLinkLanes);
    1073      3367049 :     leaveLane(reason);
    1074      3367049 :     if (reason == MSMoveReminder::NOTIFICATION_VAPORIZED_COLLISION) {
    1075          562 :         cleanupFurtherLanes();
    1076              :     }
    1077      3367049 : }
    1078              : 
    1079              : 
    1080              : void
    1081      4504622 : MSVehicle::initDevices() {
    1082      4504622 :     MSBaseVehicle::initDevices();
    1083      4504610 :     myLaneChangeModel = MSAbstractLaneChangeModel::build(myType->getLaneChangeModel(), *this);
    1084      4504588 :     myDriverState = static_cast<MSDevice_DriverState*>(getDevice(typeid(MSDevice_DriverState)));
    1085      4504588 :     myFrictionDevice = static_cast<MSDevice_Friction*>(getDevice(typeid(MSDevice_Friction)));
    1086      4504588 : }
    1087              : 
    1088              : 
    1089              : // ------------ interaction with the route
    1090              : bool
    1091   2239624260 : MSVehicle::hasValidRouteStart(std::string& msg) {
    1092              :     // note: not a const method because getDepartLane may call updateBestLanes
    1093   2239624260 :     if (!(*myCurrEdge)->isTazConnector()) {
    1094   2239295902 :         if (myParameter->departLaneProcedure == DepartLaneDefinition::GIVEN
    1095   2239295902 :                 || (myParameter->departLaneProcedure == DepartLaneDefinition::DEFAULT && MSEdge::getDefaultDepartLaneDefinition() == DepartLaneDefinition::GIVEN)) {
    1096     62537152 :             if ((*myCurrEdge)->getDepartLane(*this) == nullptr) {
    1097          132 :                 msg = "Invalid departlane definition for vehicle '" + getID() + "'.";
    1098           66 :                 if (myParameter->departLane >= (int)(*myCurrEdge)->getLanes().size()) {
    1099           11 :                     myRouteValidity |= ROUTE_START_INVALID_LANE;
    1100              :                 } else {
    1101           55 :                     myRouteValidity |= ROUTE_START_INVALID_PERMISSIONS;
    1102              :                 }
    1103           66 :                 return false;
    1104              :             }
    1105              :         } else {
    1106   2176758750 :             if ((*myCurrEdge)->allowedLanes(getVClass(), ignoreTransientPermissions()) == nullptr) {
    1107          144 :                 msg = "Vehicle '" + getID() + "' is not allowed to depart on any lane of edge '" + (*myCurrEdge)->getID() + "'.";
    1108           72 :                 myRouteValidity |= ROUTE_START_INVALID_PERMISSIONS;
    1109           72 :                 return false;
    1110              :             }
    1111              :         }
    1112   2239295764 :         if (myParameter->departSpeedProcedure == DepartSpeedDefinition::GIVEN && myParameter->departSpeed > myType->getMaxSpeed() + SPEED_EPS) {
    1113           38 :             msg = "Departure speed for vehicle '" + getID() + "' is too high for the vehicle type '" + myType->getID() + "'.";
    1114           19 :             myRouteValidity |= ROUTE_START_INVALID_LANE;
    1115           19 :             return false;
    1116              :         }
    1117              :     }
    1118   2239624103 :     myRouteValidity &= ~(ROUTE_START_INVALID_LANE | ROUTE_START_INVALID_PERMISSIONS);
    1119   2239624103 :     return true;
    1120              : }
    1121              : 
    1122              : 
    1123              : bool
    1124    713659826 : MSVehicle::hasArrived() const {
    1125    713659826 :     return hasArrivedInternal(false);
    1126              : }
    1127              : 
    1128              : 
    1129              : bool
    1130   1436706773 : MSVehicle::hasArrivedInternal(bool oppositeTransformed) const {
    1131   2334126752 :     return ((myCurrEdge == myRoute->end() - 1 || (myParameter->arrivalEdge >= 0 && getRoutePosition() >= myParameter->arrivalEdge))
    1132    539336067 :             && (myStops.empty() || myStops.front().edge != myCurrEdge || myStops.front().getSpeed() > 0)
    1133   1009325648 :             && ((myLaneChangeModel->isOpposite() && !oppositeTransformed) ? myLane->getLength() - myState.myPos : myState.myPos) > MIN2(myLane->getLength(), myArrivalPos) - POSITION_EPS
    1134   1448101071 :             && !isRemoteControlled());
    1135              : }
    1136              : 
    1137              : 
    1138              : bool
    1139      1557117 : MSVehicle::replaceRoute(ConstMSRoutePtr newRoute, const std::string& info, bool onInit, int offset, bool addRouteStops, bool removeStops, std::string* msgReturn) {
    1140      3114234 :     if (MSBaseVehicle::replaceRoute(newRoute, info, onInit, offset, addRouteStops, removeStops, msgReturn)) {
    1141              :         // update best lanes (after stops were added)
    1142      1557099 :         myLastBestLanesEdge = nullptr;
    1143      1557099 :         myLastBestLanesInternalLane = nullptr;
    1144      1557099 :         updateBestLanes(true, onInit ? (*myCurrEdge)->getLanes().front() : 0);
    1145              :         assert(!removeStops || haveValidStopEdges());
    1146      1557099 :         if (myStops.size() == 0) {
    1147      1512627 :             myStopDist = std::numeric_limits<double>::max();
    1148              :         }
    1149      1557099 :         return true;
    1150              :     }
    1151              :     return false;
    1152              : }
    1153              : 
    1154              : 
    1155              : // ------------ Interaction with move reminders
    1156              : void
    1157    700645699 : MSVehicle::workOnMoveReminders(double oldPos, double newPos, double newSpeed) {
    1158              :     // This erasure-idiom works for all stl-sequence-containers
    1159              :     // See Meyers: Effective STL, Item 9
    1160   1854719463 :     for (MoveReminderCont::iterator rem = myMoveReminders.begin(); rem != myMoveReminders.end();) {
    1161              :         // XXX: calling notifyMove with newSpeed seems not the best choice. For the ballistic update, the average speed is calculated and used
    1162              :         //      although a higher order quadrature-formula might be more adequate.
    1163              :         //      For the euler case (where the speed is considered constant for each time step) it is conceivable that
    1164              :         //      the current calculations may lead to systematic errors for large time steps (compared to reality). Refs. #2579
    1165   2308147528 :         if (!rem->first->notifyMove(*this, oldPos + rem->second, newPos + rem->second, MAX2(0., newSpeed))) {
    1166              : #ifdef _DEBUG
    1167              :             if (myTraceMoveReminders) {
    1168              :                 traceMoveReminder("notifyMove", rem->first, rem->second, false);
    1169              :             }
    1170              : #endif
    1171              :             rem = myMoveReminders.erase(rem);
    1172              :         } else {
    1173              : #ifdef _DEBUG
    1174              :             if (myTraceMoveReminders) {
    1175              :                 traceMoveReminder("notifyMove", rem->first, rem->second, true);
    1176              :             }
    1177              : #endif
    1178              :             ++rem;
    1179              :         }
    1180              :     }
    1181    700645699 :     if (myEnergyParams != nullptr) {
    1182              :         // TODO make the vehicle energy params a derived class which is a move reminder
    1183    140021040 :         myEnergyParams->setDynamicValues(isStopped() ? getNextStop().duration : -1, isParking(), getWaitingTime(), getAngleDiff());
    1184              :     }
    1185    700645699 : }
    1186              : 
    1187              : 
    1188              : void
    1189        75012 : MSVehicle::workOnIdleReminders() {
    1190        75012 :     updateWaitingTime(0.);   // cf issue 2233
    1191              : 
    1192              :     // vehicle move reminders
    1193        88380 :     for (const auto& rem : myMoveReminders) {
    1194        13368 :         rem.first->notifyIdle(*this);
    1195              :     }
    1196              : 
    1197              :     // lane move reminders - for aggregated values
    1198       191369 :     for (MSMoveReminder* rem : getLane()->getMoveReminders()) {
    1199       116357 :         rem->notifyIdle(*this);
    1200              :     }
    1201        75012 : }
    1202              : 
    1203              : // XXX: consider renaming...
    1204              : void
    1205     19512384 : MSVehicle::adaptLaneEntering2MoveReminder(const MSLane& enteredLane) {
    1206              :     // save the old work reminders, patching the position information
    1207              :     //  add the information about the new offset to the old lane reminders
    1208     19512384 :     const double oldLaneLength = myLane->getLength();
    1209     55255349 :     for (auto& rem : myMoveReminders) {
    1210     35742965 :         rem.second += oldLaneLength;
    1211              : #ifdef _DEBUG
    1212              : //        if (rem->first==0) std::cout << "Null reminder (?!)" << std::endl;
    1213              : //        std::cout << "Adapted MoveReminder on lane " << ((rem->first->getLane()==0) ? "NULL" : rem->first->getLane()->getID()) <<" position to " << rem->second << std::endl;
    1214              :         if (myTraceMoveReminders) {
    1215              :             traceMoveReminder("adaptedPos", rem.first, rem.second, true);
    1216              :         }
    1217              : #endif
    1218              :     }
    1219     32756895 :     for (MSMoveReminder* const rem : enteredLane.getMoveReminders()) {
    1220     13244511 :         addReminder(rem);
    1221              :     }
    1222     19512384 : }
    1223              : 
    1224              : 
    1225              : // ------------ Other getter methods
    1226              : double
    1227    164907969 : MSVehicle::getSlope() const {
    1228    164907969 :     if (isParking() && getStops().begin()->parkingarea != nullptr) {
    1229         3881 :         return getStops().begin()->parkingarea->getVehicleSlope(*this);
    1230              :     }
    1231    164904088 :     if (myLane == nullptr) {
    1232              :         return 0;
    1233              :     }
    1234    164904088 :     const double posLat = myState.myPosLat; // @todo get rid of the '-'
    1235    164904088 :     Position p1 = getPosition();
    1236    164904088 :     Position p2 = getBackPosition();
    1237              :     if (p2 == Position::INVALID) {
    1238              :         // Handle special case of vehicle's back reaching out of the network
    1239            6 :         if (myFurtherLanes.size() > 0) {
    1240            6 :             p2 = myFurtherLanes.back()->geometryPositionAtOffset(0, -myFurtherLanesPosLat.back());
    1241              :             if (p2 == Position::INVALID) {
    1242              :                 // unsuitable lane geometry
    1243            0 :                 p2 = myLane->geometryPositionAtOffset(0, posLat);
    1244              :             }
    1245              :         } else {
    1246            0 :             p2 = myLane->geometryPositionAtOffset(0, posLat);
    1247              :         }
    1248              :     }
    1249    164904088 :     return (p1 != p2 ? RAD2DEG(p2.slopeTo2D(p1)) : myLane->getShape().slopeDegreeAtOffset(myLane->interpolateLanePosToGeometryPos(getPositionOnLane())));
    1250              : }
    1251              : 
    1252              : 
    1253              : Position
    1254    938142984 : MSVehicle::getPosition(const double offset) const {
    1255    938142984 :     if (myLane == nullptr) {
    1256              :         // when called in the context of GUI-Drawing, the simulation step is already incremented
    1257          145 :         if (myInfluencer != nullptr && myInfluencer->isRemoteAffected(MSNet::getInstance()->getCurrentTimeStep())) {
    1258           40 :             return myCachedPosition;
    1259              :         } else {
    1260          105 :             return Position::INVALID;
    1261              :         }
    1262              :     }
    1263    938142839 :     if (isParking()) {
    1264      3855346 :         if (myInfluencer != nullptr && myInfluencer->getLastAccessTimeStep() > getNextStopParameter()->started) {
    1265          122 :             return myCachedPosition;
    1266              :         }
    1267      3855224 :         if (myStops.begin()->parkingarea != nullptr) {
    1268        22494 :             return myStops.begin()->parkingarea->getVehiclePosition(*this);
    1269              :         } else {
    1270              :             // position beside the road
    1271      3832730 :             PositionVector shp = myLane->getEdge().getLanes()[0]->getShape();
    1272      7665340 :             shp.move2side(SUMO_const_laneWidth * (MSGlobals::gLefthand ? -1 : 1));
    1273      3832730 :             return shp.positionAtOffset(myLane->interpolateLanePosToGeometryPos(getPositionOnLane() + offset));
    1274      3832730 :         }
    1275              :     }
    1276    934287493 :     const bool changingLanes = myLaneChangeModel->isChangingLanes();
    1277   1858427014 :     const double posLat = (MSGlobals::gLefthand ? 1 : -1) * getLateralPositionOnLane();
    1278    934287493 :     if (offset == 0. && !changingLanes) {
    1279              :         if (myCachedPosition == Position::INVALID) {
    1280    705216035 :             myCachedPosition = validatePosition(myLane->geometryPositionAtOffset(myState.myPos, posLat));
    1281    705216035 :             if (MSNet::getInstance()->hasElevation() && MSGlobals::gSublane) {
    1282        61093 :                 interpolateLateralZ(myCachedPosition, myState.myPos, posLat);
    1283              :             }
    1284              :         }
    1285    927521179 :         return myCachedPosition;
    1286              :     }
    1287      6766314 :     Position result = validatePosition(myLane->geometryPositionAtOffset(getPositionOnLane() + offset, posLat), offset);
    1288      6766314 :     interpolateLateralZ(result, getPositionOnLane() + offset, posLat);
    1289      6766314 :     return result;
    1290              : }
    1291              : 
    1292              : 
    1293              : void
    1294      7112629 : MSVehicle::interpolateLateralZ(Position& pos, double offset, double posLat) const {
    1295      7112629 :     const MSLane* shadow = myLaneChangeModel->getShadowLane();
    1296      7112629 :     if (shadow != nullptr && pos != Position::INVALID) {
    1297              :         // ignore negative offset
    1298              :         const Position shadowPos = shadow->geometryPositionAtOffset(MAX2(0.0, offset));
    1299        61437 :         if (shadowPos != Position::INVALID && pos.z() != shadowPos.z()) {
    1300          325 :             const double centerDist = (myLane->getWidth() + shadow->getWidth()) * 0.5;
    1301          325 :             double relOffset = fabs(posLat) / centerDist;
    1302          325 :             double newZ = (1 - relOffset) * pos.z() + relOffset * shadowPos.z();
    1303              :             pos.setz(newZ);
    1304              :         }
    1305              :     }
    1306      7112629 : }
    1307              : 
    1308              : 
    1309              : double
    1310      1110832 : MSVehicle::getDistanceToLeaveJunction() const {
    1311      1110832 :     double result = getLength() - getPositionOnLane();
    1312      1110832 :     if (myLane->isNormal()) {
    1313              :         return MAX2(0.0, result);
    1314              :     }
    1315         5013 :     const MSLane* lane = myLane;
    1316        10026 :     while (lane->isInternal()) {
    1317         5013 :         result += lane->getLength();
    1318         5013 :         lane = lane->getCanonicalSuccessorLane();
    1319              :     }
    1320              :     return result;
    1321              : }
    1322              : 
    1323              : 
    1324              : Position
    1325       103413 : MSVehicle::getPositionAlongBestLanes(double offset) const {
    1326              :     assert(MSGlobals::gUsingInternalLanes);
    1327       103413 :     if (!isOnRoad()) {
    1328            0 :         return Position::INVALID;
    1329              :     }
    1330       103413 :     const std::vector<MSLane*>& bestLanes = getBestLanesContinuation();
    1331              :     auto nextBestLane = bestLanes.begin();
    1332       103413 :     const bool opposite = myLaneChangeModel->isOpposite();
    1333       103413 :     double pos = opposite ? myLane->getLength() - myState.myPos : myState.myPos;
    1334       103413 :     const MSLane* lane = opposite ? myLane->getParallelOpposite() : getLane();
    1335              :     assert(lane != 0);
    1336              :     bool success = true;
    1337              : 
    1338       305336 :     while (offset > 0) {
    1339              :         // take into account lengths along internal lanes
    1340       308706 :         while (lane->isInternal() && offset > 0) {
    1341       106783 :             if (offset > lane->getLength() - pos) {
    1342         3561 :                 offset -= lane->getLength() - pos;
    1343         3561 :                 lane = lane->getLinkCont()[0]->getViaLaneOrLane();
    1344              :                 pos = 0.;
    1345         3561 :                 if (lane == nullptr) {
    1346              :                     success = false;
    1347              :                     offset = 0.;
    1348              :                 }
    1349              :             } else {
    1350       103222 :                 pos += offset;
    1351              :                 offset = 0;
    1352              :             }
    1353              :         }
    1354              :         // set nextBestLane to next non-internal lane
    1355       207050 :         while (nextBestLane != bestLanes.end() && *nextBestLane == nullptr) {
    1356              :             ++nextBestLane;
    1357              :         }
    1358       201923 :         if (offset > 0) {
    1359              :             assert(!lane->isInternal());
    1360              :             assert(lane == *nextBestLane);
    1361        98701 :             if (offset > lane->getLength() - pos) {
    1362        98518 :                 offset -= lane->getLength() - pos;
    1363              :                 ++nextBestLane;
    1364              :                 assert(nextBestLane == bestLanes.end() || *nextBestLane != 0);
    1365        98518 :                 if (nextBestLane == bestLanes.end()) {
    1366              :                     success = false;
    1367              :                     offset = 0.;
    1368              :                 } else {
    1369        98518 :                     const MSLink* link = lane->getLinkTo(*nextBestLane);
    1370              :                     assert(link != nullptr);
    1371              :                     lane = link->getViaLaneOrLane();
    1372              :                     pos = 0.;
    1373              :                 }
    1374              :             } else {
    1375          183 :                 pos += offset;
    1376              :                 offset = 0;
    1377              :             }
    1378              :         }
    1379              : 
    1380              :     }
    1381              : 
    1382       103413 :     if (success) {
    1383       103413 :         return lane->geometryPositionAtOffset(pos, -getLateralPositionOnLane());
    1384              :     } else {
    1385            0 :         return Position::INVALID;
    1386              :     }
    1387              : }
    1388              : 
    1389              : 
    1390              : double
    1391       711190 : MSVehicle::getMaxSpeedOnLane() const {
    1392       711190 :     if (myLane != nullptr) {
    1393       711190 :         return myLane->getVehicleMaxSpeed(this);
    1394              :     }
    1395            0 :     return myType->getMaxSpeed();
    1396              : }
    1397              : 
    1398              : 
    1399              : Position
    1400    711982349 : MSVehicle::validatePosition(Position result, double offset) const {
    1401              :     int furtherIndex = 0;
    1402    711982349 :     double lastLength = getPositionOnLane();
    1403    711982349 :     while (result == Position::INVALID) {
    1404       209643 :         if (furtherIndex >= (int)myFurtherLanes.size()) {
    1405              :             //WRITE_WARNINGF(TL("Could not compute position for vehicle '%', time=%."), getID(), time2string(MSNet::getInstance()->getCurrentTimeStep()));
    1406              :             break;
    1407              :         }
    1408              :         //std::cout << SIMTIME << " veh=" << getID() << " lane=" << myLane->getID() << " pos=" << getPositionOnLane() << " posLat=" << getLateralPositionOnLane() << " offset=" << offset << " result=" << result << " i=" << furtherIndex << " further=" << myFurtherLanes.size() << "\n";
    1409       191620 :         MSLane* further = myFurtherLanes[furtherIndex];
    1410       191620 :         offset += lastLength;
    1411       191620 :         result = further->geometryPositionAtOffset(further->getLength() + offset, -getLateralPositionOnLane());
    1412              :         lastLength = further->getLength();
    1413       191620 :         furtherIndex++;
    1414              :         //std::cout << SIMTIME << "   newResult=" << result << "\n";
    1415              :     }
    1416    711982349 :     return result;
    1417              : }
    1418              : 
    1419              : 
    1420              : ConstMSEdgeVector::const_iterator
    1421       280383 : MSVehicle::getRerouteOrigin() const {
    1422              :     // too close to the next junction, so avoid an emergency brake here
    1423       280383 :     if (myLane != nullptr && (myCurrEdge + 1) != myRoute->end() && !isRailway(getVClass())) {
    1424       218325 :         if (myLane->isInternal()) {
    1425              :             return myCurrEdge + 1;
    1426              :         }
    1427       211588 :         if (myState.myPos > myLane->getLength() - getCarFollowModel().brakeGap(myState.mySpeed, getCarFollowModel().getMaxDecel(), 0.)) {
    1428              :             return myCurrEdge + 1;
    1429              :         }
    1430       209247 :         if (myLane->getEdge().hasChangeProhibitions(getVClass(), myLane->getIndex())) {
    1431              :             return myCurrEdge + 1;
    1432              :         }
    1433              :     }
    1434       271193 :     return myCurrEdge;
    1435              : }
    1436              : 
    1437              : 
    1438              : double
    1439    140051320 : MSVehicle::getAngleDiff() const {
    1440    140051320 :     return myLastAngle == INVALID_DOUBLE ? 0. : GeomHelper::angleDiff(myLastAngle, myAngle);
    1441              : }
    1442              : 
    1443              : double
    1444        30280 : MSVehicle::getCurveRadius() const {
    1445        30280 :     const double angleDiff = getAngleDiff();
    1446              :     return angleDiff == 0
    1447        30280 :         ? std::numeric_limits<double>::max()
    1448        23124 :         : SPEED2DIST(getSpeed()) / fabs(angleDiff);
    1449              : }
    1450              : 
    1451              : 
    1452              : void
    1453      5326008 : MSVehicle::setAngle(double angle, bool straightenFurther) {
    1454              : #ifdef DEBUG_FURTHER
    1455              :     if (DEBUG_COND) {
    1456              :         std::cout << SIMTIME << " veh '" << getID() << " setAngle(" << angle <<  ") straightenFurther=" << straightenFurther << std::endl;
    1457              :     }
    1458              : #endif
    1459      5326008 :     myAngle = angle;
    1460      5326008 :     MSLane* next = myLane;
    1461      5326008 :     if (straightenFurther && myFurtherLanesPosLat.size() > 0) {
    1462       202324 :         for (int i = 0; i < (int)myFurtherLanes.size(); i++) {
    1463       104005 :             MSLane* further = myFurtherLanes[i];
    1464       104005 :             const MSLink* link = further->getLinkTo(next);
    1465       104005 :             if (link  != nullptr) {
    1466       103557 :                 myFurtherLanesPosLat[i] = getLateralPositionOnLane() - link->getLateralShift();
    1467              :                 next = further;
    1468              :             } else {
    1469              :                 break;
    1470              :             }
    1471              :         }
    1472              :     }
    1473      5326008 : }
    1474              : 
    1475              : 
    1476              : void
    1477       451431 : MSVehicle::setActionStepLength(double actionStepLength, bool resetOffset) {
    1478       451431 :     SUMOTime actionStepLengthMillisecs = SUMOVehicleParserHelper::processActionStepLength(actionStepLength);
    1479              :     SUMOTime previousActionStepLength = getActionStepLength();
    1480              :     const bool newActionStepLength = actionStepLengthMillisecs != previousActionStepLength;
    1481       451431 :     if (newActionStepLength) {
    1482            7 :         getSingularType().setActionStepLength(actionStepLengthMillisecs, resetOffset);
    1483            7 :         if (!resetOffset) {
    1484            1 :             updateActionOffset(previousActionStepLength, actionStepLengthMillisecs);
    1485              :         }
    1486              :     }
    1487       451425 :     if (resetOffset) {
    1488            6 :         resetActionOffset();
    1489              :     }
    1490       451431 : }
    1491              : 
    1492              : 
    1493              : bool
    1494    301259861 : MSVehicle::congested() const {
    1495    301259861 :     return myState.mySpeed < (60.0 / 3.6) || myLane->getSpeedLimit() < (60.1 / 3.6);
    1496              : }
    1497              : 
    1498              : 
    1499              : double
    1500    707943711 : MSVehicle::computeAngle() const {
    1501              :     Position p1;
    1502    707943711 :     const double posLat = -myState.myPosLat; // @todo get rid of the '-'
    1503    707943711 :     const double lefthandSign = (MSGlobals::gLefthand ? -1 : 1);
    1504              : 
    1505              :     // if parking manoeuvre is happening then rotate vehicle on each step
    1506    707943711 :     if (MSGlobals::gModelParkingManoeuver && !manoeuvreIsComplete()) {
    1507          450 :         return getAngle() + myManoeuvre.getGUIIncrement();
    1508              :     }
    1509              : 
    1510    707943261 :     if (isParking()) {
    1511        28995 :         if (myStops.begin()->parkingarea != nullptr) {
    1512        15743 :             return myStops.begin()->parkingarea->getVehicleAngle(*this);
    1513              :         } else {
    1514        13252 :             return myLane->getShape().rotationAtOffset(myLane->interpolateLanePosToGeometryPos(getPositionOnLane()));
    1515              :         }
    1516              :     }
    1517    707914266 :     if (myLaneChangeModel->isChangingLanes()) {
    1518              :         // cannot use getPosition() because it already includes the offset to the side and thus messes up the angle
    1519      1147030 :         p1 = myLane->geometryPositionAtOffset(myState.myPos, lefthandSign * posLat);
    1520            9 :         if (p1 == Position::INVALID && myLane->getShape().length2D() == 0. && myLane->isInternal()) {
    1521              :             // workaround: extrapolate the preceding lane shape
    1522            9 :             MSLane* predecessorLane = myLane->getCanonicalPredecessorLane();
    1523            9 :             p1 = predecessorLane->geometryPositionAtOffset(predecessorLane->getLength() + myState.myPos, lefthandSign * posLat);
    1524              :         }
    1525              :     } else {
    1526    706767236 :         p1 = getPosition();
    1527              :     }
    1528              : 
    1529              :     Position p2;
    1530    707914266 :     if (getVehicleType().getParameter().locomotiveLength > 0) {
    1531              :         // articulated vehicle should use the heading of the first part
    1532      1874222 :         const double locoLength = MIN2(getVehicleType().getParameter().locomotiveLength, getLength());
    1533      1874222 :         p2 = getPosition(-locoLength);
    1534              :     } else {
    1535    706040044 :         p2 = getBackPosition();
    1536              :     }
    1537              :     if (p2 == Position::INVALID) {
    1538              :         // Handle special case of vehicle's back reaching out of the network
    1539         2144 :         if (myFurtherLanes.size() > 0) {
    1540          130 :             p2 = myFurtherLanes.back()->geometryPositionAtOffset(0, -myFurtherLanesPosLat.back());
    1541              :             if (p2 == Position::INVALID) {
    1542              :                 // unsuitable lane geometry
    1543           85 :                 p2 = myLane->geometryPositionAtOffset(0, posLat);
    1544              :             }
    1545              :         } else {
    1546         2014 :             p2 = myLane->geometryPositionAtOffset(0, posLat);
    1547              :         }
    1548              :     }
    1549              :     double result = (p1 != p2 ? p2.angleTo2D(p1) :
    1550       104660 :                      myLane->getShape().rotationAtOffset(myLane->interpolateLanePosToGeometryPos(getPositionOnLane())));
    1551              : 
    1552    707914266 :     result += lefthandSign * myLaneChangeModel->calcAngleOffset();
    1553              : 
    1554              : #ifdef DEBUG_FURTHER
    1555              :     if (DEBUG_COND) {
    1556              :         std::cout << SIMTIME << " computeAngle veh=" << getID() << " p1=" << p1 << " p2=" << p2 << " angle=" << RAD2DEG(result) << " naviDegree=" << GeomHelper::naviDegree(result) << "\n";
    1557              :     }
    1558              : #endif
    1559    707914266 :     return result;
    1560              : }
    1561              : 
    1562              : 
    1563              : const Position
    1564    878111591 : MSVehicle::getBackPosition() const {
    1565    878111591 :     const double posLat = MSGlobals::gLefthand ? myState.myPosLat : -myState.myPosLat;
    1566              :     Position result;
    1567    878111591 :     if (myState.myPos >= myType->getLength()) {
    1568              :         // vehicle is fully on the new lane
    1569    860525097 :         result = myLane->geometryPositionAtOffset(myState.myPos - myType->getLength(), posLat);
    1570              :     } else {
    1571     17586494 :         if (myLaneChangeModel->isChangingLanes() && myFurtherLanes.size() > 0 && myLaneChangeModel->getShadowLane(myFurtherLanes.back()) == nullptr) {
    1572              :             // special case where the target lane has no predecessor
    1573              : #ifdef DEBUG_FURTHER
    1574              :             if (DEBUG_COND) {
    1575              :                 std::cout << "    getBackPosition veh=" << getID() << " specialCase using myLane=" << myLane->getID() << " pos=0 posLat=" << myState.myPosLat << " result=" << myLane->geometryPositionAtOffset(0, posLat) << "\n";
    1576              :             }
    1577              : #endif
    1578         1906 :             result = myLane->geometryPositionAtOffset(0, posLat);
    1579              :         } else {
    1580              : #ifdef DEBUG_FURTHER
    1581              :             if (DEBUG_COND) {
    1582              :                 std::cout << "    getBackPosition veh=" << getID() << " myLane=" << myLane->getID() << " further=" << toString(myFurtherLanes) << " myFurtherLanesPosLat=" << toString(myFurtherLanesPosLat) << "\n";
    1583              :             }
    1584              : #endif
    1585     17584588 :             if (myFurtherLanes.size() > 0 && !myLaneChangeModel->isChangingLanes()) {
    1586              :                 // truncate to 0 if vehicle starts on an edge that is shorter than its length
    1587     17198959 :                 const double backPos = MAX2(0.0, getBackPositionOnLane(myFurtherLanes.back()));
    1588     34098833 :                 result = myFurtherLanes.back()->geometryPositionAtOffset(backPos, -myFurtherLanesPosLat.back() * (MSGlobals::gLefthand ? -1 : 1));
    1589              :             } else {
    1590       385629 :                 result = myLane->geometryPositionAtOffset(0, posLat);
    1591              :             }
    1592              :         }
    1593              :     }
    1594    878111591 :     if (MSNet::getInstance()->hasElevation() && MSGlobals::gSublane) {
    1595       285222 :         interpolateLateralZ(result, myState.myPos - myType->getLength(), posLat);
    1596              :     }
    1597    878111591 :     return result;
    1598              : }
    1599              : 
    1600              : 
    1601              : bool
    1602       481107 : MSVehicle::willStop() const {
    1603       481107 :     return !isStopped() && !myStops.empty() && myLane != nullptr && &myStops.front().lane->getEdge() == &myLane->getEdge();
    1604              : }
    1605              : 
    1606              : bool
    1607    371136836 : MSVehicle::isStoppedOnLane() const {
    1608    371136836 :     return isStopped() && myStops.front().lane == myLane;
    1609              : }
    1610              : 
    1611              : bool
    1612     30733037 : MSVehicle::keepStopping(bool afterProcessing) const {
    1613     30733037 :     if (isStopped()) {
    1614              :         // when coming out of vehicleTransfer we must shift the time forward
    1615     36781445 :         return (myStops.front().duration - (afterProcessing ? DELTA_T : 0) > 0 || isStoppedTriggered() || myStops.front().pars.collision
    1616     30481029 :                 || myStops.front().pars.breakDown || (myStops.front().getSpeed() > 0
    1617        35713 :                         && (myState.myPos < MIN2(myStops.front().pars.endPos, myStops.front().lane->getLength() - POSITION_EPS))
    1618        29894 :                         && (myStops.front().pars.parking == ParkingType::ONROAD || getSpeed() >= SUMO_const_haltingSpeed)));
    1619              :     } else {
    1620              :         return false;
    1621              :     }
    1622              : }
    1623              : 
    1624              : 
    1625              : SUMOTime
    1626        15327 : MSVehicle::remainingStopDuration() const {
    1627        15327 :     if (isStopped()) {
    1628        15327 :         return myStops.front().duration;
    1629              :     }
    1630              :     return 0;
    1631              : }
    1632              : 
    1633              : 
    1634              : SUMOTime
    1635    680425583 : MSVehicle::collisionStopTime() const {
    1636    680425583 :     return (myStops.empty() || !myStops.front().pars.collision) ? myCollisionImmunity : MAX2((SUMOTime)0, myStops.front().duration);
    1637              : }
    1638              : 
    1639              : 
    1640              : bool
    1641    680286666 : MSVehicle::brokeDown() const {
    1642    680286666 :     return isStopped() && !myStops.empty() && myStops.front().pars.breakDown;
    1643              : }
    1644              : 
    1645              : 
    1646              : bool
    1647       171344 : MSVehicle::ignoreCollision() const {
    1648       171344 :     return myCollisionImmunity > 0;
    1649              : }
    1650              : 
    1651              : 
    1652              : double
    1653    643945793 : MSVehicle::processNextStop(double currentVelocity) {
    1654    643945793 :     if (myStops.empty()) {
    1655              :         // no stops; pass
    1656              :         return currentVelocity;
    1657              :     }
    1658              : 
    1659              : #ifdef DEBUG_STOPS
    1660              :     if (DEBUG_COND) {
    1661              :         std::cout << "\nPROCESS_NEXT_STOP\n" << SIMTIME << " vehicle '" << getID() << "'" << std::endl;
    1662              :     }
    1663              : #endif
    1664              : 
    1665              :     MSStop& stop = myStops.front();
    1666     41464379 :     const SUMOTime time = MSNet::getInstance()->getCurrentTimeStep();
    1667     41464379 :     if (stop.reached) {
    1668     25851659 :         stop.duration -= getActionStepLength();
    1669     25851659 :         if (getSpeed() > 0) {
    1670              :             // re-enter stopping places to correct waiting position (except for parkingArea since it's place-based)
    1671      3859860 :             if (stop.busstop != nullptr) {
    1672              :                 // let the bus stop know the vehicle
    1673        12967 :                 stop.busstop->enter(this, stop.pars.parking == ParkingType::OFFROAD);
    1674              :             }
    1675      3859860 :             if (stop.containerstop != nullptr) {
    1676              :                 // let the container stop know the vehicle
    1677      3814603 :                 stop.containerstop->enter(this, stop.pars.parking == ParkingType::OFFROAD);
    1678              :             }
    1679      3859860 :             if (stop.chargingStation != nullptr) {
    1680              :                 // let the container stop know the vehicle
    1681         3045 :                 stop.chargingStation->enter(this, stop.pars.parking == ParkingType::OFFROAD);
    1682              :             }
    1683      3859860 :             if (stop.getSpeed() <= 0) {
    1684      3842705 :                 stop.entryPos = getPositionOnLane();
    1685              :             }
    1686              :         }
    1687              : 
    1688              : #ifdef DEBUG_STOPS
    1689              :         if (DEBUG_COND) {
    1690              :             std::cout << SIMTIME << " vehicle '" << getID() << "' reached stop.\n"
    1691              :                       << "Remaining duration: " << STEPS2TIME(stop.duration) << std::endl;
    1692              :             if (stop.getSpeed() > 0) {
    1693              :                 std::cout << " waypointSpeed=" << stop.getSpeed() << " vehPos=" << myState.myPos << " endPos=" << stop.pars.endPos << "\n";
    1694              :             }
    1695              :         }
    1696              : #endif
    1697     25851659 :         if (stop.duration <= 0 && stop.pars.join != "") {
    1698              :             // join this train (part) to another one
    1699        37466 :             MSVehicle* joinVeh = dynamic_cast<MSVehicle*>(MSNet::getInstance()->getVehicleControl().getVehicle(stop.pars.join));
    1700          969 :             if (joinVeh && joinVeh->hasDeparted() && (joinVeh->joinTrainPart(this) || joinVeh->joinTrainPartFront(this))) {
    1701           36 :                 stop.joinTriggered = false;
    1702           36 :                 if (myAmRegisteredAsWaiting) {
    1703           21 :                     MSNet::getInstance()->getVehicleControl().unregisterOneWaiting();
    1704           21 :                     myAmRegisteredAsWaiting = false;
    1705              :                 }
    1706              :                 // avoid collision warning before this vehicle is removed (joinVeh was already made longer)
    1707           36 :                 myCollisionImmunity = TIME2STEPS(100);
    1708              :                 // mark this vehicle as arrived
    1709           36 :                 myArrivalPos = getPositionOnLane();
    1710           36 :                 const_cast<SUMOVehicleParameter*>(myParameter)->arrivalEdge = getRoutePosition();
    1711              :                 // handle transportables that want to continue in the other vehicle
    1712           36 :                 if (myPersonDevice != nullptr) {
    1713            3 :                     myPersonDevice->transferAtSplitOrJoin(joinVeh);
    1714              :                 }
    1715           36 :                 if (myContainerDevice != nullptr) {
    1716            3 :                     myContainerDevice->transferAtSplitOrJoin(joinVeh);
    1717              :                 }
    1718              :             }
    1719              :         }
    1720     25851659 :         boardTransportables(stop);
    1721     22037462 :         if (time > stop.endBoarding) {
    1722              :             // for taxi: cancel customers
    1723       198307 :             MSDevice_Taxi* taxiDevice = static_cast<MSDevice_Taxi*>(getDevice(typeid(MSDevice_Taxi)));
    1724              :             if (taxiDevice != nullptr) {
    1725              :                 // may invalidate stops including the current reference
    1726           64 :                 taxiDevice->cancelCurrentCustomers();
    1727           64 :                 resumeFromStopping();
    1728           64 :                 return currentVelocity;
    1729              :             }
    1730              :         }
    1731     22037398 :         if (!keepStopping() && isOnRoad()) {
    1732              : #ifdef DEBUG_STOPS
    1733              :             if (DEBUG_COND) {
    1734              :                 std::cout << SIMTIME << " vehicle '" << getID() << "' resumes from stopping." << std::endl;
    1735              :             }
    1736              : #endif
    1737        44636 :             resumeFromStopping();
    1738        44636 :             if (isRail() && hasStops()) {
    1739              :                 // stay on the current lane in case of a double stop
    1740         3132 :                 const MSStop& nextStop = getNextStop();
    1741         3132 :                 if (nextStop.edge == myCurrEdge) {
    1742         1079 :                     const double stopSpeed = getCarFollowModel().stopSpeed(this, getSpeed(), nextStop.pars.endPos - myState.myPos);
    1743              :                     //std::cout << SIMTIME << " veh=" << getID() << " resumedFromStopping currentVelocity=" << currentVelocity << " stopSpeed=" << stopSpeed << "\n";
    1744         1079 :                     return stopSpeed;
    1745              :                 }
    1746              :             }
    1747              :         } else {
    1748     21992762 :             if (stop.triggered) {
    1749      3226679 :                 if (getVehicleType().getPersonCapacity() == getPersonNumber()) {
    1750           30 :                     WRITE_WARNINGF(TL("Vehicle '%' ignores triggered stop on lane '%' due to capacity constraints."), getID(), stop.lane->getID());
    1751           10 :                     stop.triggered = false;
    1752      3226669 :                 } else if (!myAmRegisteredAsWaiting && stop.duration <= DELTA_T) {
    1753              :                     // we can only register after waiting for one step. otherwise we might falsely signal a deadlock
    1754         4331 :                     MSNet::getInstance()->getVehicleControl().registerOneWaiting();
    1755         4331 :                     myAmRegisteredAsWaiting = true;
    1756              : #ifdef DEBUG_STOPS
    1757              :                     if (DEBUG_COND) {
    1758              :                         std::cout << SIMTIME << " vehicle '" << getID() << "' registers as waiting for person." << std::endl;
    1759              :                     }
    1760              : #endif
    1761              :                 }
    1762              :             }
    1763     21992762 :             if (stop.containerTriggered) {
    1764        39500 :                 if (getVehicleType().getContainerCapacity() == getContainerNumber()) {
    1765         1332 :                     WRITE_WARNINGF(TL("Vehicle '%' ignores container triggered stop on lane '%' due to capacity constraints."), getID(), stop.lane->getID());
    1766          444 :                     stop.containerTriggered = false;
    1767        39056 :                 } else if (stop.containerTriggered && !myAmRegisteredAsWaiting && stop.duration <= DELTA_T) {
    1768              :                     // we can only register after waiting for one step. otherwise we might falsely signal a deadlock
    1769           92 :                     MSNet::getInstance()->getVehicleControl().registerOneWaiting();
    1770           92 :                     myAmRegisteredAsWaiting = true;
    1771              : #ifdef DEBUG_STOPS
    1772              :                     if (DEBUG_COND) {
    1773              :                         std::cout << SIMTIME << " vehicle '" << getID() << "' registers as waiting for container." << std::endl;
    1774              :                     }
    1775              : #endif
    1776              :                 }
    1777              :             }
    1778              :             // joining only takes place after stop duration is over
    1779     21992762 :             if (stop.joinTriggered && !myAmRegisteredAsWaiting
    1780         7103 :                     && stop.duration <= (stop.pars.extension >= 0 ? -stop.pars.extension : 0)) {
    1781           98 :                 if (stop.pars.extension >= 0) {
    1782          105 :                     WRITE_WARNINGF(TL("Vehicle '%' aborts joining after extension of %s at time %."), getID(), STEPS2TIME(stop.pars.extension), time2string(SIMSTEP));
    1783           35 :                     stop.joinTriggered = false;
    1784              :                 } else {
    1785              :                     // keep stopping indefinitely but ensure that simulation terminates
    1786           63 :                     MSNet::getInstance()->getVehicleControl().registerOneWaiting();
    1787           63 :                     myAmRegisteredAsWaiting = true;
    1788              :                 }
    1789              :             }
    1790     21992762 :             if (stop.getSpeed() > 0) {
    1791              :                 //waypoint mode
    1792       219653 :                 if (stop.duration == 0) {
    1793          243 :                     return stop.getSpeed();
    1794              :                 } else {
    1795              :                     // stop for 'until' (computed in planMove)
    1796              :                     return currentVelocity;
    1797              :                 }
    1798              :             } else {
    1799              :                 // brake
    1800     21773109 :                 if (MSGlobals::gSemiImplicitEulerUpdate || stop.getSpeed() > 0) {
    1801     21504012 :                     return 0;
    1802              :                 } else {
    1803              :                     // ballistic:
    1804       269097 :                     return getSpeed() - getCarFollowModel().getMaxDecel();
    1805              :                 }
    1806              :             }
    1807              :         }
    1808              :     } else {
    1809              : 
    1810              : #ifdef DEBUG_STOPS
    1811              :         if (DEBUG_COND) {
    1812              :             std::cout << SIMTIME << " vehicle '" << getID() << "' hasn't reached next stop." << std::endl;
    1813              :         }
    1814              : #endif
    1815              :         //std::cout << SIMTIME <<  " myStopDist=" << myStopDist << " bGap=" << getBrakeGap(myLane->getVehicleMaxSpeed(this)) << "\n";
    1816     15675239 :         if (stop.pars.onDemand && !stop.skipOnDemand && myStopDist <= getCarFollowModel().brakeGap(myLane->getVehicleMaxSpeed(this))) {
    1817          576 :             MSNet* const net = MSNet::getInstance();
    1818           44 :             const bool noExits = ((myPersonDevice == nullptr || !myPersonDevice->anyLeavingAtStop(stop))
    1819          586 :                                   && (myContainerDevice == nullptr || !myContainerDevice->anyLeavingAtStop(stop)));
    1820           83 :             const bool noEntries = ((!net->hasPersons() || !net->getPersonControl().hasAnyWaiting(stop.getEdge(), this))
    1821          625 :                                     && (!net->hasContainers() || !net->getContainerControl().hasAnyWaiting(stop.getEdge(), this)));
    1822          576 :             if (noExits && noEntries) {
    1823              :                 //std::cout << " skipOnDemand\n";
    1824          508 :                 stop.skipOnDemand = true;
    1825              :                 // bestLanes must be extended past this stop
    1826          508 :                 updateBestLanes(true);
    1827              :             }
    1828              :         }
    1829              :         // is the next stop on the current lane?
    1830     15612720 :         if (stop.edge == myCurrEdge) {
    1831              :             // get the stopping position
    1832      5673431 :             bool useStoppingPlace = stop.busstop != nullptr || stop.containerstop != nullptr || stop.parkingarea != nullptr;
    1833              :             bool fitsOnStoppingPlace = true;
    1834      5673431 :             if (!stop.skipOnDemand) {  // no need to check available space if we skip it anyway
    1835      5667642 :                 if (stop.busstop != nullptr) {
    1836      1710980 :                     fitsOnStoppingPlace &= stop.busstop->fits(myState.myPos, *this);
    1837              :                 }
    1838      5667642 :                 if (stop.containerstop != nullptr) {
    1839        21791 :                     fitsOnStoppingPlace &= stop.containerstop->fits(myState.myPos, *this);
    1840              :                 }
    1841              :                 // if the stop is a parking area we check if there is a free position on the area
    1842      5667642 :                 if (stop.parkingarea != nullptr) {
    1843       689617 :                     fitsOnStoppingPlace &= myState.myPos > stop.parkingarea->getBeginLanePosition();
    1844       689617 :                     if (stop.parkingarea->getOccupancy() >= stop.parkingarea->getCapacity()) {
    1845              :                         fitsOnStoppingPlace = false;
    1846              :                         // trigger potential parkingZoneReroute
    1847       434932 :                         MSParkingArea* oldParkingArea = stop.parkingarea;
    1848       475978 :                         for (MSMoveReminder* rem : myLane->getMoveReminders()) {
    1849        41046 :                             if (rem->isParkingRerouter()) {
    1850        19818 :                                 rem->notifyEnter(*this, MSMoveReminder::NOTIFICATION_PARKING_REROUTE, myLane);
    1851              :                             }
    1852              :                         }
    1853       434932 :                         if (myStops.empty() || myStops.front().parkingarea != oldParkingArea) {
    1854              :                             // rerouted, keep driving
    1855              :                             return currentVelocity;
    1856              :                         }
    1857       254685 :                     } else if (stop.parkingarea->getOccupancyIncludingReservations(this) >= stop.parkingarea->getCapacity()) {
    1858              :                         fitsOnStoppingPlace = false;
    1859       126337 :                     } else if (stop.parkingarea->parkOnRoad() && stop.parkingarea->getLotIndex(this) < 0) {
    1860              :                         fitsOnStoppingPlace = false;
    1861              :                     }
    1862              :                 }
    1863              :             }
    1864      5671679 :             const double targetPos = myState.myPos + myStopDist + (stop.getSpeed() > 0 ? (stop.pars.startPos - stop.pars.endPos) : 0);
    1865      5671679 :             double reachedThreshold = (useStoppingPlace ? targetPos - STOPPING_PLACE_OFFSET : stop.getReachedThreshold()) - NUMERICAL_EPS;
    1866      5671679 :             if (stop.busstop != nullptr && stop.getSpeed() <= 0 && getWaitingTime() > DELTA_T && myLane == stop.lane) {
    1867              :                 // count (long) busStop as reached when fully within and jammed before the designated spot
    1868       818396 :                 reachedThreshold = MIN2(reachedThreshold, stop.pars.startPos + getLength());
    1869              :             }
    1870      5671679 :             const bool posReached = myState.pos() >= reachedThreshold && currentVelocity <= stop.getSpeed() + SUMO_const_haltingSpeed && myLane == stop.lane;
    1871              : #ifdef DEBUG_STOPS
    1872              :             if (DEBUG_COND) {
    1873              :                 std::cout <<  "   pos=" << myState.pos() << " speed=" << currentVelocity << " targetPos=" << targetPos << " fits=" << fitsOnStoppingPlace
    1874              :                           << " reachedThresh=" << reachedThreshold
    1875              :                           << " posReached=" << posReached
    1876              :                           << " myLane=" << Named::getIDSecure(myLane)
    1877              :                           << " stopLane=" << Named::getIDSecure(stop.lane)
    1878              :                           << "\n";
    1879              :             }
    1880              : #endif
    1881      5671679 :             if (posReached && !fitsOnStoppingPlace && MSStopOut::active()) {
    1882         5736 :                 MSStopOut::getInstance()->stopBlocked(this, time);
    1883              :             }
    1884      5671679 :             if (fitsOnStoppingPlace && posReached && (!MSGlobals::gModelParkingManoeuver || myManoeuvre.entryManoeuvreIsComplete(this))) {
    1885              :                 // ok, we may stop (have reached the stop)  and either we are not modelling maneuvering or have completed entry
    1886        56714 :                 stop.reached = true;
    1887        56714 :                 if (!stop.startedFromState) {
    1888        56493 :                     stop.pars.started = time;
    1889              :                 }
    1890              : #ifdef DEBUG_STOPS
    1891              :                 if (DEBUG_COND) {
    1892              :                     std::cout << SIMTIME << " vehicle '" << getID() << "' reached next stop." << std::endl;
    1893              :                 }
    1894              : #endif
    1895        56714 :                 if (MSStopOut::active()) {
    1896         5569 :                     MSStopOut::getInstance()->stopStarted(this, getPersonNumber(), getContainerNumber(), time);
    1897              :                 }
    1898        56714 :                 myLane->getEdge().addWaiting(this);
    1899        56714 :                 MSNet::getInstance()->informVehicleStateListener(this, MSNet::VehicleState::STARTING_STOP);
    1900        56714 :                 MSNet::getInstance()->getVehicleControl().registerStopStarted();
    1901              :                 // compute stopping time
    1902        56714 :                 stop.duration = stop.getMinDuration(time);
    1903        56714 :                 stop.endBoarding = stop.pars.extension >= 0 ? time + stop.duration + stop.pars.extension : SUMOTime_MAX;
    1904        56714 :                 MSDevice_Taxi* taxiDevice = static_cast<MSDevice_Taxi*>(getDevice(typeid(MSDevice_Taxi)));
    1905         4122 :                 if (taxiDevice != nullptr && stop.pars.extension >= 0) {
    1906              :                     // earliestPickupTime is set with waitUntil
    1907           84 :                     stop.endBoarding = MAX2(time, stop.pars.waitUntil) + stop.pars.extension;
    1908              :                 }
    1909        56714 :                 if (stop.getSpeed() > 0) {
    1910              :                     // ignore duration parameter in waypoint mode unless 'until' or 'ended' are set
    1911         3426 :                     if (stop.getUntil() > time) {
    1912          348 :                         stop.duration = stop.getUntil() - time;
    1913              :                     } else {
    1914         3078 :                         stop.duration = 0;
    1915              :                     }
    1916              :                 } else {
    1917        53288 :                     stop.entryPos = getPositionOnLane();
    1918              :                 }
    1919        56714 :                 if (stop.busstop != nullptr) {
    1920              :                     // let the bus stop know the vehicle
    1921        18756 :                     stop.busstop->enter(this, stop.pars.parking == ParkingType::OFFROAD);
    1922              :                 }
    1923        56714 :                 if (stop.containerstop != nullptr) {
    1924              :                     // let the container stop know the vehicle
    1925          571 :                     stop.containerstop->enter(this, stop.pars.parking == ParkingType::OFFROAD);
    1926              :                 }
    1927        56714 :                 if (stop.parkingarea != nullptr && stop.getSpeed() <= 0) {
    1928              :                     // let the parking area know the vehicle
    1929         9825 :                     stop.parkingarea->enter(this, stop.pars.parking == ParkingType::OFFROAD);
    1930              :                 }
    1931        56714 :                 if (stop.chargingStation != nullptr) {
    1932              :                     // let the container stop know the vehicle
    1933         3555 :                     stop.chargingStation->enter(this, stop.pars.parking == ParkingType::OFFROAD);
    1934              :                 }
    1935              : 
    1936        56714 :                 if (stop.pars.tripId != "") {
    1937         2920 :                     ((SUMOVehicleParameter&)getParameter()).setParameter("tripId", stop.pars.tripId);
    1938              :                 }
    1939        56714 :                 if (stop.pars.line != "") {
    1940         1463 :                     ((SUMOVehicleParameter&)getParameter()).line = stop.pars.line;
    1941              :                 }
    1942        56714 :                 if (stop.pars.split != "") {
    1943              :                     // split the train
    1944         1239 :                     MSVehicle* splitVeh = dynamic_cast<MSVehicle*>(MSNet::getInstance()->getVehicleControl().getVehicle(stop.pars.split));
    1945           24 :                     if (splitVeh == nullptr) {
    1946         3645 :                         WRITE_WARNINGF(TL("Vehicle '%' to split from vehicle '%' is not known. time=%."), stop.pars.split, getID(), SIMTIME)
    1947              :                     } else {
    1948           24 :                         MSNet::getInstance()->getInsertionControl().add(splitVeh);
    1949           24 :                         splitVeh->getRoute().getEdges()[0]->removeWaiting(splitVeh);
    1950           24 :                         MSNet::getInstance()->getVehicleControl().unregisterOneWaiting();
    1951           24 :                         const double newLength = MAX2(myType->getLength() - splitVeh->getVehicleType().getLength(),
    1952           24 :                                                       myType->getParameter().locomotiveLength);
    1953           24 :                         getSingularType().setLength(newLength);
    1954              :                         // handle transportables that want to continue in the split part
    1955           24 :                         if (myPersonDevice != nullptr) {
    1956            0 :                             myPersonDevice->transferAtSplitOrJoin(splitVeh);
    1957              :                         }
    1958           24 :                         if (myContainerDevice != nullptr) {
    1959            6 :                             myContainerDevice->transferAtSplitOrJoin(splitVeh);
    1960              :                         }
    1961           24 :                         if (splitVeh->getParameter().departPosProcedure == DepartPosDefinition::SPLIT_FRONT) {
    1962            3 :                             const double backShift = splitVeh->getLength() + getVehicleType().getMinGap();
    1963            3 :                             myState.myPos -= backShift;
    1964            3 :                             myState.myBackPos -= backShift;
    1965              :                         }
    1966              :                     }
    1967              :                 }
    1968              : 
    1969        56714 :                 boardTransportables(stop);
    1970        56710 :                 if (stop.pars.posLat != INVALID_DOUBLE) {
    1971          230 :                     myState.myPosLat = stop.pars.posLat;
    1972              :                 }
    1973              :             }
    1974              :         }
    1975              :     }
    1976              :     return currentVelocity;
    1977              : }
    1978              : 
    1979              : 
    1980              : void
    1981     25908373 : MSVehicle::boardTransportables(MSStop& stop) {
    1982     25908373 :     if (stop.skipOnDemand) {
    1983              :         return;
    1984              :     }
    1985              :     // we have reached the stop
    1986              :     // any waiting persons may board now
    1987     25705167 :     const SUMOTime time = MSNet::getInstance()->getCurrentTimeStep();
    1988     25705167 :     MSNet* const net = MSNet::getInstance();
    1989     25705167 :     const bool boarded = (time <= stop.endBoarding
    1990     25703583 :                           && net->hasPersons()
    1991      1530203 :                           && net->getPersonControl().loadAnyWaiting(&myLane->getEdge(), this, stop.timeToBoardNextPerson, stop.duration)
    1992     25712831 :                           && stop.numExpectedPerson == 0);
    1993              :     // load containers
    1994     25705167 :     const bool loaded = (time <= stop.endBoarding
    1995     25703583 :                          && net->hasContainers()
    1996      3916635 :                          && net->getContainerControl().loadAnyWaiting(&myLane->getEdge(), this, stop.timeToLoadNextContainer, stop.duration)
    1997     25705742 :                          && stop.numExpectedContainer == 0);
    1998              : 
    1999              :     bool unregister = false;
    2000     21890966 :     if (time > stop.endBoarding) {
    2001         1584 :         stop.triggered = false;
    2002         1584 :         stop.containerTriggered = false;
    2003         1584 :         if (myAmRegisteredAsWaiting) {
    2004              :             unregister = true;
    2005          328 :             myAmRegisteredAsWaiting = false;
    2006              :         }
    2007              :     }
    2008     21890966 :     if (boarded) {
    2009              :         // the triggering condition has been fulfilled. Maybe we want to wait a bit longer for additional riders (car pooling)
    2010         7525 :         if (myAmRegisteredAsWaiting) {
    2011              :             unregister = true;
    2012              :         }
    2013         7525 :         stop.triggered = false;
    2014         7525 :         myAmRegisteredAsWaiting = false;
    2015              :     }
    2016     21890966 :     if (loaded) {
    2017              :         // the triggering condition has been fulfilled
    2018          555 :         if (myAmRegisteredAsWaiting) {
    2019              :             unregister = true;
    2020              :         }
    2021          555 :         stop.containerTriggered = false;
    2022          555 :         myAmRegisteredAsWaiting = false;
    2023              :     }
    2024              : 
    2025     21890966 :     if (unregister) {
    2026          412 :         MSNet::getInstance()->getVehicleControl().unregisterOneWaiting();
    2027              : #ifdef DEBUG_STOPS
    2028              :         if (DEBUG_COND) {
    2029              :             std::cout << SIMTIME << " vehicle '" << getID() << "' unregisters as waiting for transportable." << std::endl;
    2030              :         }
    2031              : #endif
    2032              :     }
    2033              : }
    2034              : 
    2035              : bool
    2036          921 : MSVehicle::joinTrainPart(MSVehicle* veh) {
    2037              :     // check if veh is close enough to be joined to the rear of this vehicle
    2038          921 :     MSLane* backLane = myFurtherLanes.size() == 0 ? myLane : myFurtherLanes.back();
    2039          921 :     double gap = getBackPositionOnLane() - veh->getPositionOnLane();
    2040         1143 :     if (isStopped() && myStops.begin()->duration <= DELTA_T && myStops.begin()->joinTriggered && backLane == veh->getLane()
    2041          951 :             && gap >= 0 && gap <= getVehicleType().getMinGap() + 1) {
    2042           15 :         const double newLength = myType->getLength() + veh->getVehicleType().getLength();
    2043           15 :         getSingularType().setLength(newLength);
    2044           15 :         myStops.begin()->joinTriggered = false;
    2045           15 :         if (myAmRegisteredAsWaiting) {
    2046            0 :             MSNet::getInstance()->getVehicleControl().unregisterOneWaiting();
    2047            0 :             myAmRegisteredAsWaiting = false;
    2048              :         }
    2049              :         return true;
    2050              :     } else {
    2051          906 :         return false;
    2052              :     }
    2053              : }
    2054              : 
    2055              : 
    2056              : bool
    2057          906 : MSVehicle::joinTrainPartFront(MSVehicle* veh) {
    2058              :     // check if veh is close enough to be joined to the front of this vehicle
    2059          906 :     MSLane* backLane = veh->myFurtherLanes.size() == 0 ? veh->myLane : veh->myFurtherLanes.back();
    2060          906 :     double gap = veh->getBackPositionOnLane(backLane) - getPositionOnLane();
    2061         1113 :     if (isStopped() && myStops.begin()->duration <= DELTA_T && myStops.begin()->joinTriggered && backLane == getLane()
    2062          930 :             && gap >= 0 && gap <= getVehicleType().getMinGap() + 1) {
    2063              :         double skippedLaneLengths = 0;
    2064           24 :         if (veh->myFurtherLanes.size() > 0) {
    2065            9 :             skippedLaneLengths += getLane()->getLength();
    2066              :             // this vehicle must be moved to the lane of veh
    2067              :             // ensure that lane and furtherLanes of veh match our route
    2068            9 :             int routeIndex = getRoutePosition();
    2069            9 :             if (myLane->isInternal()) {
    2070            0 :                 routeIndex++;
    2071              :             }
    2072           27 :             for (int i = (int)veh->myFurtherLanes.size() - 1; i >= 0; i--) {
    2073           18 :                 MSEdge* edge = &veh->myFurtherLanes[i]->getEdge();
    2074           18 :                 if (edge->isInternal()) {
    2075            9 :                     continue;
    2076              :                 }
    2077            9 :                 if (!edge->isInternal() && edge != myRoute->getEdges()[routeIndex]) {
    2078            0 :                     std::string warn = TL("Cannot join vehicle '%' to vehicle '%' due to incompatible routes. time=%.");
    2079            0 :                     WRITE_WARNINGF(warn, veh->getID(), getID(), time2string(SIMSTEP));
    2080              :                     return false;
    2081              :                 }
    2082            9 :                 routeIndex++;
    2083              :             }
    2084            9 :             if (veh->getCurrentEdge()->getNormalSuccessor() != myRoute->getEdges()[routeIndex]) {
    2085            3 :                 std::string warn = TL("Cannot join vehicle '%' to vehicle '%' due to incompatible routes. time=%.");
    2086            9 :                 WRITE_WARNINGF(warn, veh->getID(), getID(), time2string(SIMSTEP));
    2087              :                 return false;
    2088              :             }
    2089           12 :             for (int i = (int)veh->myFurtherLanes.size() - 2; i >= 0; i--) {
    2090            6 :                 skippedLaneLengths += veh->myFurtherLanes[i]->getLength();
    2091              :             }
    2092              :         }
    2093              : 
    2094           21 :         const double newLength = myType->getLength() + veh->getVehicleType().getLength();
    2095           21 :         getSingularType().setLength(newLength);
    2096              :         // lane will be advanced just as for regular movement
    2097           21 :         myState.myPos = skippedLaneLengths + veh->getPositionOnLane();
    2098           21 :         myStops.begin()->joinTriggered = false;
    2099           21 :         if (myAmRegisteredAsWaiting) {
    2100            6 :             MSNet::getInstance()->getVehicleControl().unregisterOneWaiting();
    2101            6 :             myAmRegisteredAsWaiting = false;
    2102              :         }
    2103           21 :         return true;
    2104              :     } else {
    2105          882 :         return false;
    2106              :     }
    2107              : }
    2108              : 
    2109              : double
    2110      9101442 : MSVehicle::getBrakeGap(bool delayed) const {
    2111      9101442 :     return getCarFollowModel().brakeGap(getSpeed(), getCarFollowModel().getMaxDecel(), delayed ? getCarFollowModel().getHeadwayTime() : 0);
    2112              : }
    2113              : 
    2114              : 
    2115              : bool
    2116    704245042 : MSVehicle::checkActionStep(const SUMOTime t) {
    2117    704245042 :     myActionStep = isActionStep(t);
    2118    704245042 :     if (myActionStep) {
    2119    632683106 :         myLastActionTime = t;
    2120              :     }
    2121    704245042 :     return myActionStep;
    2122              : }
    2123              : 
    2124              : 
    2125              : void
    2126         1479 : MSVehicle::resetActionOffset(const SUMOTime timeUntilNextAction) {
    2127         1479 :     myLastActionTime = MSNet::getInstance()->getCurrentTimeStep() + timeUntilNextAction;
    2128         1479 : }
    2129              : 
    2130              : 
    2131              : void
    2132            1 : MSVehicle::updateActionOffset(const SUMOTime oldActionStepLength, const SUMOTime newActionStepLength) {
    2133            1 :     SUMOTime now = MSNet::getInstance()->getCurrentTimeStep();
    2134            1 :     SUMOTime timeSinceLastAction = now - myLastActionTime;
    2135            1 :     if (timeSinceLastAction == 0) {
    2136              :         // Action was scheduled now, may be delayed be new action step length
    2137              :         timeSinceLastAction = oldActionStepLength;
    2138              :     }
    2139            1 :     if (timeSinceLastAction >= newActionStepLength) {
    2140              :         // Action point required in this step
    2141            0 :         myLastActionTime = now;
    2142              :     } else {
    2143            1 :         SUMOTime timeUntilNextAction = newActionStepLength - timeSinceLastAction;
    2144            1 :         resetActionOffset(timeUntilNextAction);
    2145              :     }
    2146            1 : }
    2147              : 
    2148              : 
    2149              : 
    2150              : void
    2151    704245042 : MSVehicle::planMove(const SUMOTime t, const MSLeaderInfo& ahead, const double lengthsInFront) {
    2152              : #ifdef DEBUG_PLAN_MOVE
    2153              :     if (DEBUG_COND) {
    2154              :         std::cout
    2155              :                 << "\nPLAN_MOVE\n"
    2156              :                 << SIMTIME
    2157              :                 << std::setprecision(gPrecision)
    2158              :                 << " veh=" << getID()
    2159              :                 << " lane=" << myLane->getID()
    2160              :                 << " pos=" << getPositionOnLane()
    2161              :                 << " posLat=" << getLateralPositionOnLane()
    2162              :                 << " speed=" << getSpeed()
    2163              :                 << "\n";
    2164              :     }
    2165              : #endif
    2166              :     // Update the driver state
    2167    704245042 :     if (hasDriverState()) {
    2168       451417 :         myDriverState->update();
    2169       902834 :         setActionStepLength(myDriverState->getDriverState()->getActionStepLength(), false);
    2170              :     }
    2171              : 
    2172    704245042 :     myStopSpeed = getCarFollowModel().maxNextSpeed(myStopSpeed, this);
    2173    704245042 :     if (!checkActionStep(t)) {
    2174              : #ifdef DEBUG_ACTIONSTEPS
    2175              :         if (DEBUG_COND) {
    2176              :             std::cout << STEPS2TIME(t) << " vehicle '" << getID() << "' skips action." << std::endl;
    2177              :         }
    2178              : #endif
    2179              :         // During non-action passed drive items still need to be removed
    2180              :         // @todo rather work with updating myCurrentDriveItem (refs #3714)
    2181     71561936 :         removePassedDriveItems();
    2182     71561936 :         return;
    2183              :     } else {
    2184              : #ifdef DEBUG_ACTIONSTEPS
    2185              :         if (DEBUG_COND) {
    2186              :             std::cout << STEPS2TIME(t) << " vehicle = '" << getID() << "' takes action." << std::endl;
    2187              :         }
    2188              : #endif
    2189              :         myLFLinkLanesPrev.swap(myLFLinkLanes);
    2190    632683106 :         if (myInfluencer != nullptr) {
    2191       488774 :             myInfluencer->updateRemoteControlRoute(this);
    2192              :         }
    2193    632683106 :         planMoveInternal(t, ahead, myLFLinkLanes, myStopDist, myStopSpeed, myNextTurn);
    2194              : #ifdef DEBUG_PLAN_MOVE
    2195              :         if (DEBUG_COND) {
    2196              :             DriveItemVector::iterator i;
    2197              :             for (i = myLFLinkLanes.begin(); i != myLFLinkLanes.end(); ++i) {
    2198              :                 std::cout
    2199              :                         << " vPass=" << (*i).myVLinkPass
    2200              :                         << " vWait=" << (*i).myVLinkWait
    2201              :                         << " linkLane=" << ((*i).myLink == 0 ? "NULL" : (*i).myLink->getViaLaneOrLane()->getID())
    2202              :                         << " request=" << (*i).mySetRequest
    2203              :                         << "\n";
    2204              :             }
    2205              :         }
    2206              : #endif
    2207    632683106 :         checkRewindLinkLanes(lengthsInFront, myLFLinkLanes);
    2208    632683106 :         myNextDriveItem = myLFLinkLanes.begin();
    2209              :         // ideally would only do this with the call inside planMoveInternal - but that needs a const method
    2210              :         //   so this is a kludge here - nuisance as it adds an extra check in a busy loop
    2211    632683106 :         if (MSGlobals::gModelParkingManoeuver) {
    2212         2971 :             if (getManoeuvreType() == MSVehicle::MANOEUVRE_EXIT && manoeuvreIsComplete()) {
    2213           30 :                 setManoeuvreType(MSVehicle::MANOEUVRE_NONE);
    2214              :             }
    2215              :         }
    2216              :     }
    2217    632683106 :     myLaneChangeModel->resetChanged();
    2218              : }
    2219              : 
    2220              : 
    2221              : bool
    2222    174568335 : MSVehicle::brakeForOverlap(const MSLink* link, const MSLane* lane) const {
    2223              :     // @review needed
    2224              :     //const double futurePosLat = getLateralPositionOnLane() + link->getLateralShift();
    2225              :     //const double overlap = getLateralOverlap(futurePosLat, link->getViaLaneOrLane());
    2226              :     //const double edgeWidth = link->getViaLaneOrLane()->getEdge().getWidth();
    2227    174568335 :     const double futurePosLat = getLateralPositionOnLane() + (
    2228    174568335 :                                     lane != myLane && lane->isInternal() ? lane->getIncomingLanes()[0].viaLink->getLateralShift() : 0);
    2229    174568335 :     const double overlap = getLateralOverlap(futurePosLat, lane);
    2230              :     const double edgeWidth = lane->getEdge().getWidth();
    2231              :     const bool result = (overlap > POSITION_EPS
    2232              :                          // do not get stuck on narrow edges
    2233      3101070 :                          && getVehicleType().getWidth() <= edgeWidth
    2234      3095465 :                          && link->getViaLane() == nullptr
    2235              :                          // this is the exit link of a junction. The normal edge should support the shadow
    2236      1507402 :                          && ((myLaneChangeModel->getShadowLane(link->getLane()) == nullptr)
    2237              :                              // the shadow lane must be permitted
    2238      1121041 :                              || !myLaneChangeModel->getShadowLane(link->getLane())->allowsVehicleClass(getVClass())
    2239              :                              // the internal lane after an internal junction has no parallel lane. make sure there is no shadow before continuing
    2240      1059737 :                              || (lane->getEdge().isInternal() && lane->getIncomingLanes()[0].lane->getEdge().isInternal()))
    2241              :                          // ignore situations where the shadow lane is part of a double-connection with the current lane
    2242       468177 :                          && (myLaneChangeModel->getShadowLane() == nullptr
    2243       257418 :                              || myLaneChangeModel->getShadowLane()->getLinkCont().size() == 0
    2244       240126 :                              || myLaneChangeModel->getShadowLane()->getLinkCont().front()->getLane() != link->getLane())
    2245              :                          // emergency vehicles may do some crazy stuff
    2246    174974695 :                          && !myLaneChangeModel->hasBlueLight());
    2247              : 
    2248              : #ifdef DEBUG_PLAN_MOVE
    2249              :     if (DEBUG_COND) {
    2250              :         std::cout << SIMTIME << " veh=" << getID() << " link=" << link->getDescription() << " lane=" << lane->getID()
    2251              :                   << " linkLane=" << link->getLane()->getID()
    2252              :                   << " shadowLane=" << Named::getIDSecure(myLaneChangeModel->getShadowLane())
    2253              :                   << " shift=" << link->getLateralShift()
    2254              :                   << " fpLat=" << futurePosLat << " overlap=" << overlap << " w=" << getVehicleType().getWidth()
    2255              :                   << " shadowLane=" << Named::getIDSecure(myLaneChangeModel->getShadowLane(link->getLane()))
    2256              :                   << " result=" << result << "\n";
    2257              :     }
    2258              : #endif
    2259    174568335 :     return result;
    2260              : }
    2261              : 
    2262              : 
    2263              : 
    2264              : void
    2265    632683106 : MSVehicle::planMoveInternal(const SUMOTime t, MSLeaderInfo ahead, DriveItemVector& lfLinks, double& newStopDist, double& newStopSpeed, std::pair<double, const MSLink*>& nextTurn) const {
    2266              :     lfLinks.clear();
    2267    632683106 :     newStopDist = std::numeric_limits<double>::max();
    2268              :     //
    2269              :     const MSCFModel& cfModel = getCarFollowModel();
    2270    632683106 :     const double vehicleLength = getVehicleType().getLength();
    2271    632683106 :     const double maxV = cfModel.maxNextSpeed(myState.mySpeed, this);
    2272    632683106 :     const double maxVD = MAX2(getMaxSpeed(), MIN2(maxV, getDesiredMaxSpeed()));
    2273    632683106 :     const bool opposite = myLaneChangeModel->isOpposite();
    2274              :     // maxVD is possibly higher than vType-maxSpeed and in this case laneMaxV may be higher as well
    2275    632683106 :     double laneMaxV = myLane->getVehicleMaxSpeed(this, maxVD);
    2276    632683106 :     const double vMinComfortable = cfModel.minNextSpeed(getSpeed(), this);
    2277              :     double lateralShift = 0;
    2278    632683106 :     if (isRail()) {
    2279              :         // speed limits must hold for the whole length of the train
    2280      1781241 :         for (MSLane* l : myFurtherLanes) {
    2281       398450 :             laneMaxV = MIN2(laneMaxV, l->getVehicleMaxSpeed(this, maxVD));
    2282              : #ifdef DEBUG_PLAN_MOVE
    2283              :             if (DEBUG_COND) {
    2284              :                 std::cout << "   laneMaxV=" << laneMaxV << " lane=" << l->getID() << "\n";
    2285              :             }
    2286              : #endif
    2287              :         }
    2288              :     }
    2289              :     //  speed limits are not emergencies (e.g. when the limit changes suddenly due to TraCI or a variableSpeedSignal)
    2290              :     laneMaxV = MAX2(laneMaxV, vMinComfortable);
    2291    633171848 :     if (myInfluencer && !myInfluencer->considerSpeedLimit()) {
    2292              :         laneMaxV = std::numeric_limits<double>::max();
    2293              :     }
    2294              :     // v is the initial maximum velocity of this vehicle in this step
    2295    632683106 :     double v = cfModel.maximumLaneSpeedCF(this, maxV, laneMaxV);
    2296              :     // if we are modelling parking then we dawdle until the manoeuvre is complete - by setting a very low max speed
    2297              :     //   in practice this only applies to exit manoeuvre because entry manoeuvre just delays setting stop.reached - when the vehicle is virtually stopped
    2298    632683106 :     if (MSGlobals::gModelParkingManoeuver && !manoeuvreIsComplete()) {
    2299          420 :         v = NUMERICAL_EPS_SPEED;
    2300              :     }
    2301              : 
    2302    632683106 :     if (myInfluencer != nullptr) {
    2303       488774 :         const double vMin = MAX2(0., cfModel.minNextSpeed(myState.mySpeed, this));
    2304              : #ifdef DEBUG_TRACI
    2305              :         if (DEBUG_COND) {
    2306              :             std::cout << SIMTIME << " veh=" << getID() << " speedBeforeTraci=" << v;
    2307              :         }
    2308              : #endif
    2309       488774 :         v = myInfluencer->influenceSpeed(t, v, v, vMin, maxV);
    2310              : #ifdef DEBUG_TRACI
    2311              :         if (DEBUG_COND) {
    2312              :             std::cout << " influencedSpeed=" << v;
    2313              :         }
    2314              : #endif
    2315       488774 :         v = myInfluencer->gapControlSpeed(t, this, v, v, vMin, maxV);
    2316              : #ifdef DEBUG_TRACI
    2317              :         if (DEBUG_COND) {
    2318              :             std::cout << " gapControlSpeed=" << v << "\n";
    2319              :         }
    2320              : #endif
    2321              :     }
    2322              :     // all links within dist are taken into account (potentially)
    2323    632683106 :     const double dist = SPEED2DIST(maxV) + cfModel.brakeGap(maxV);
    2324              : 
    2325    632683106 :     const std::vector<MSLane*>& bestLaneConts = getBestLanesContinuation();
    2326              : #ifdef DEBUG_PLAN_MOVE
    2327              :     if (DEBUG_COND) {
    2328              :         std::cout << "   dist=" << dist << " bestLaneConts=" << toString(bestLaneConts)
    2329              :                   << "\n   maxV=" << maxV << " laneMaxV=" << laneMaxV << " v=" << v << "\n";
    2330              :     }
    2331              : #endif
    2332              :     assert(bestLaneConts.size() > 0);
    2333              :     bool hadNonInternal = false;
    2334              :     // the distance already "seen"; in the following always up to the end of the current "lane"
    2335    632683106 :     double seen = opposite ? myState.myPos : myLane->getLength() - myState.myPos;
    2336    632683106 :     nextTurn.first = seen;
    2337    632683106 :     nextTurn.second = nullptr;
    2338    632683106 :     bool encounteredTurn = (MSGlobals::gLateralResolution <= 0); // next turn is only needed for sublane
    2339              :     double seenNonInternal = 0;
    2340    632683106 :     double seenInternal = myLane->isInternal() ? seen : 0;
    2341    632683106 :     double vLinkPass = MIN2(cfModel.estimateSpeedAfterDistance(seen, v, cfModel.getMaxAccel()), laneMaxV); // upper bound
    2342              :     int view = 0;
    2343              :     DriveProcessItem* lastLink = nullptr;
    2344              :     bool slowedDownForMinor = false; // whether the vehicle already had to slow down on approach to a minor link
    2345              :     double mustSeeBeforeReversal = 0;
    2346              :     // iterator over subsequent lanes and fill lfLinks until stopping distance or stopped
    2347    632683106 :     const MSLane* lane = opposite ? myLane->getParallelOpposite() : myLane;
    2348              :     assert(lane != 0);
    2349    632683106 :     const MSLane* leaderLane = myLane;
    2350    632683106 :     bool foundRailSignal = !isRail();
    2351              :     bool planningToStop = false;
    2352              : #ifdef PARALLEL_STOPWATCH
    2353              :     myLane->getStopWatch()[0].start();
    2354              : #endif
    2355              : 
    2356              :     // optionally slow down to match arrival time
    2357    632683106 :     const double sfp = getVehicleType().getParameter().speedFactorPremature;
    2358    632672730 :     if (v > vMinComfortable && hasStops() && myStops.front().pars.arrival >= 0 && sfp > 0
    2359         4279 :             && v > myLane->getSpeedLimit() * sfp
    2360    632686142 :             && !myStops.front().reached) {
    2361         2786 :         const double vSlowDown = slowDownForSchedule(vMinComfortable);
    2362         5403 :         v = MIN2(v, vSlowDown);
    2363              :     }
    2364              :     auto stopIt = myStops.begin();
    2365              :     while (true) {
    2366              :         // check leader on lane
    2367              :         //  leader is given for the first edge only
    2368   1217103304 :         if (opposite &&
    2369              :                 (leaderLane->getVehicleNumberWithPartials() > 1
    2370       103588 :                  || (leaderLane != myLane && leaderLane->getVehicleNumber() > 0))) {
    2371       397306 :             ahead.clear();
    2372              :             // find opposite-driving leader that must be respected on the currently looked at lane
    2373              :             // (only looking at one lane at a time)
    2374       397306 :             const double backOffset = leaderLane == myLane ? getPositionOnLane() : leaderLane->getLength();
    2375       397306 :             const double gapOffset = leaderLane == myLane ? 0 : seen - leaderLane->getLength();
    2376       397306 :             const MSLeaderDistanceInfo cands = leaderLane->getFollowersOnConsecutive(this, backOffset, true, backOffset, MSLane::MinorLinkMode::FOLLOW_NEVER);
    2377       397306 :             MSLeaderDistanceInfo oppositeLeaders(leaderLane->getWidth(), this, 0.);
    2378       397306 :             const double minTimeToLeaveLane = MSGlobals::gSublane ? MAX2(TS, (0.5 *  myLane->getWidth() - getLateralPositionOnLane()) / getVehicleType().getMaxSpeedLat()) : TS;
    2379      1055302 :             for (int i = 0; i < cands.numSublanes(); i++) {
    2380       657996 :                 CLeaderDist cand = cands[i];
    2381       657996 :                 if (cand.first != 0) {
    2382       540957 :                     if ((cand.first->myLaneChangeModel->isOpposite() && cand.first->getLaneChangeModel().getShadowLane() != leaderLane)
    2383       541428 :                             || (!cand.first->myLaneChangeModel->isOpposite() && cand.first->getLaneChangeModel().getShadowLane() == leaderLane)) {
    2384              :                         // respect leaders that also drive in the opposite direction (fully or with some overlap)
    2385       351227 :                         oppositeLeaders.addLeader(cand.first, cand.second + gapOffset - getVehicleType().getMinGap() + cand.first->getVehicleType().getMinGap() - cand.first->getVehicleType().getLength());
    2386              :                     } else {
    2387              :                         // avoid frontal collision
    2388       346626 :                         const bool assumeStopped = cand.first->isStopped() || cand.first->getWaitingSeconds() > 1;
    2389       189730 :                         const double predMaxDist = cand.first->getSpeed() + (assumeStopped ? 0 : cand.first->getCarFollowModel().getMaxAccel()) * minTimeToLeaveLane;
    2390       189730 :                         if (cand.second >= 0 && (cand.second - v * minTimeToLeaveLane - predMaxDist < 0 || assumeStopped)) {
    2391        44652 :                             oppositeLeaders.addLeader(cand.first, cand.second + gapOffset - predMaxDist - getVehicleType().getMinGap());
    2392              :                         }
    2393              :                     }
    2394              :                 }
    2395              :             }
    2396              : #ifdef DEBUG_PLAN_MOVE
    2397              :             if (DEBUG_COND) {
    2398              :                 std::cout <<  " leaderLane=" << leaderLane->getID() << " gapOffset=" << gapOffset << " minTimeToLeaveLane=" << minTimeToLeaveLane
    2399              :                           << " cands=" << cands.toString() << " oppositeLeaders=" <<  oppositeLeaders.toString() << "\n";
    2400              :             }
    2401              : #endif
    2402       397306 :             adaptToLeaderDistance(oppositeLeaders, 0, seen, lastLink, v, vLinkPass);
    2403       397306 :         } else {
    2404   1216705998 :             if (MSGlobals::gLateralResolution > 0 && myLaneChangeModel->getShadowLane() == nullptr) {
    2405    197224104 :                 const double rightOL = getRightSideOnLane(lane) + lateralShift;
    2406    197224104 :                 const double leftOL = getLeftSideOnLane(lane) + lateralShift;
    2407              :                 const bool outsideLeft = leftOL > lane->getWidth();
    2408              : #ifdef DEBUG_PLAN_MOVE
    2409              :                 if (DEBUG_COND) {
    2410              :                     std::cout << SIMTIME << " veh=" << getID() << " lane=" << lane->getID() << " rightOL=" << rightOL << " leftOL=" << leftOL << "\n";
    2411              :                 }
    2412              : #endif
    2413    197224104 :                 if (rightOL < 0 || outsideLeft) {
    2414      1311321 :                     MSLeaderInfo outsideLeaders(lane->getWidth());
    2415              :                     // if ego is driving outside lane bounds we must consider
    2416              :                     // potential leaders that are also outside bounds
    2417              :                     int sublaneOffset = 0;
    2418      1311321 :                     if (outsideLeft) {
    2419       540729 :                         sublaneOffset = MIN2(-1, -(int)ceil((leftOL - lane->getWidth()) / MSGlobals::gLateralResolution));
    2420              :                     } else {
    2421       770592 :                         sublaneOffset = MAX2(1, (int)ceil(-rightOL / MSGlobals::gLateralResolution));
    2422              :                     }
    2423      1311321 :                     outsideLeaders.setSublaneOffset(sublaneOffset);
    2424              : #ifdef DEBUG_PLAN_MOVE
    2425              :                     if (DEBUG_COND) {
    2426              :                         std::cout << SIMTIME << " veh=" << getID() << " lane=" << lane->getID() << " sublaneOffset=" << sublaneOffset << " outsideLeft=" << outsideLeft << "\n";
    2427              :                     }
    2428              : #endif
    2429      5432898 :                     for (const MSVehicle* cand : lane->getVehiclesSecure()) {
    2430      1484947 :                         if ((lane != myLane || cand->getPositionOnLane() > getPositionOnLane())
    2431      4752650 :                                 && ((!outsideLeft && cand->getLeftSideOnEdge() < 0)
    2432      3267615 :                                     || (outsideLeft && cand->getLeftSideOnEdge() > lane->getEdge().getWidth()))) {
    2433        97443 :                             outsideLeaders.addLeader(cand, true);
    2434              : #ifdef DEBUG_PLAN_MOVE
    2435              :                             if (DEBUG_COND) {
    2436              :                                 std::cout << " outsideLeader=" << cand->getID() << " ahead=" << outsideLeaders.toString() << "\n";
    2437              :                             }
    2438              : #endif
    2439              :                         }
    2440              :                     }
    2441      1311321 :                     lane->releaseVehicles();
    2442      1311321 :                     if (outsideLeaders.hasVehicles()) {
    2443        26263 :                         adaptToLeaders(outsideLeaders, lateralShift, seen, lastLink, leaderLane, v, vLinkPass);
    2444              :                     }
    2445      1311321 :                 }
    2446              :             }
    2447   1216705998 :             adaptToLeaders(ahead, lateralShift, seen, lastLink, leaderLane, v, vLinkPass);
    2448              :         }
    2449   1217103304 :         if (lastLink != nullptr) {
    2450   1099256838 :             lastLink->myVLinkWait = MIN2(lastLink->myVLinkWait, v);
    2451              :         }
    2452              : #ifdef DEBUG_PLAN_MOVE
    2453              :         if (DEBUG_COND) {
    2454              :             std::cout << "\nv = " << v << "\n";
    2455              : 
    2456              :         }
    2457              : #endif
    2458              :         // XXX efficiently adapt to shadow leaders using neighAhead by iteration over the whole edge in parallel (lanechanger-style)
    2459   1217103304 :         if (myLaneChangeModel->getShadowLane() != nullptr) {
    2460              :             // also slow down for leaders on the shadowLane relative to the current lane
    2461      5038487 :             const MSLane* shadowLane = myLaneChangeModel->getShadowLane(leaderLane);
    2462              :             if (shadowLane != nullptr
    2463      5038487 :                     && (MSGlobals::gLateralResolution > 0 || getLateralOverlap() > POSITION_EPS
    2464              :                         // continous lane change cannot be stopped so we must adapt to the leader on the target lane
    2465       191108 :                         || myLaneChangeModel->getLaneChangeCompletion() < 0.5)) {
    2466      4484490 :                 if ((&shadowLane->getEdge() == &leaderLane->getEdge() || myLaneChangeModel->isOpposite())) {
    2467      4441881 :                     double latOffset = getLane()->getRightSideOnEdge() - myLaneChangeModel->getShadowLane()->getRightSideOnEdge();
    2468      4441881 :                     if (myLaneChangeModel->isOpposite()) {
    2469              :                         // ego posLat is added when retrieving sublanes but it
    2470              :                         // should be negated (subtract twice to compensate)
    2471       138425 :                         latOffset = ((myLane->getWidth() + shadowLane->getWidth()) * 0.5
    2472       138425 :                                      - 2 * getLateralPositionOnLane());
    2473              : 
    2474              :                     }
    2475      4441881 :                     MSLeaderInfo shadowLeaders = shadowLane->getLastVehicleInformation(this, latOffset, lane->getLength() - seen);
    2476              : #ifdef DEBUG_PLAN_MOVE
    2477              :                     if (DEBUG_COND && myLaneChangeModel->isOpposite()) {
    2478              :                         std::cout << SIMTIME << " opposite veh=" << getID() << " shadowLane=" << shadowLane->getID() << " latOffset=" << latOffset << " shadowLeaders=" << shadowLeaders.toString() << "\n";
    2479              :                     }
    2480              : #endif
    2481      4441881 :                     if (myLaneChangeModel->isOpposite()) {
    2482              :                         // ignore oncoming vehicles on the shadow lane
    2483       138425 :                         shadowLeaders.removeOpposite(shadowLane);
    2484              :                     }
    2485      4441881 :                     const double turningDifference = MAX2(0.0, leaderLane->getLength() - shadowLane->getLength());
    2486      4441881 :                     adaptToLeaders(shadowLeaders, latOffset, seen - turningDifference, lastLink, shadowLane, v, vLinkPass);
    2487      4484490 :                 } else if (shadowLane == myLaneChangeModel->getShadowLane() && leaderLane == myLane) {
    2488              :                     // check for leader vehicles driving in the opposite direction on the opposite-direction shadow lane
    2489              :                     // (and thus in the same direction as ego)
    2490        30567 :                     MSLeaderDistanceInfo shadowLeaders = shadowLane->getFollowersOnConsecutive(this, myLane->getOppositePos(getPositionOnLane()), true);
    2491              :                     const double latOffset = 0;
    2492              : #ifdef DEBUG_PLAN_MOVE
    2493              :                     if (DEBUG_COND) {
    2494              :                         std::cout << SIMTIME << " opposite shadows veh=" << getID() << " shadowLane=" << shadowLane->getID()
    2495              :                                   << " latOffset=" << latOffset << " shadowLeaders=" << shadowLeaders.toString() << "\n";
    2496              :                     }
    2497              : #endif
    2498        30567 :                     shadowLeaders.fixOppositeGaps(true);
    2499              : #ifdef DEBUG_PLAN_MOVE
    2500              :                     if (DEBUG_COND) {
    2501              :                         std::cout << "   shadowLeadersFixed=" << shadowLeaders.toString() << "\n";
    2502              :                     }
    2503              : #endif
    2504        30567 :                     adaptToLeaderDistance(shadowLeaders, latOffset, seen, lastLink, v, vLinkPass);
    2505        30567 :                 }
    2506              :             }
    2507              :         }
    2508              :         // adapt to pedestrians on the same lane
    2509   1217103304 :         if (lane->getEdge().getPersons().size() > 0 && lane->hasPedestrians()) {
    2510       193932 :             const double relativePos = lane->getLength() - seen;
    2511              : #ifdef DEBUG_PLAN_MOVE
    2512              :             if (DEBUG_COND) {
    2513              :                 std::cout << SIMTIME << " adapt to pedestrians on lane=" << lane->getID() << " relPos=" << relativePos << "\n";
    2514              :             }
    2515              : #endif
    2516       193932 :             const double stopTime = MAX2(1.0, ceil(getSpeed() / cfModel.getMaxDecel()));
    2517       193932 :             PersonDist leader = lane->nextBlocking(relativePos,
    2518       193932 :                                                    getRightSideOnLane(lane), getRightSideOnLane(lane) + getVehicleType().getWidth(), stopTime);
    2519       193932 :             if (leader.first != 0) {
    2520        20843 :                 const double stopSpeed = cfModel.stopSpeed(this, getSpeed(), leader.second - getVehicleType().getMinGap());
    2521        29576 :                 v = MIN2(v, stopSpeed);
    2522              : #ifdef DEBUG_PLAN_MOVE
    2523              :                 if (DEBUG_COND) {
    2524              :                     std::cout << SIMTIME << "    pedLeader=" << leader.first->getID() << " dist=" << leader.second << " v=" << v << "\n";
    2525              :                 }
    2526              : #endif
    2527              :             }
    2528              :         }
    2529   1217103304 :         if (lane->getBidiLane() != nullptr) {
    2530              :             // adapt to pedestrians on the bidi lane
    2531      5519305 :             const MSLane* bidiLane = lane->getBidiLane();
    2532      5519305 :             if (bidiLane->getEdge().getPersons().size() > 0 && bidiLane->hasPedestrians()) {
    2533         1028 :                 const double relativePos = seen;
    2534              : #ifdef DEBUG_PLAN_MOVE
    2535              :                 if (DEBUG_COND) {
    2536              :                     std::cout << SIMTIME << " adapt to pedestrians on lane=" << lane->getID() << " relPos=" << relativePos << "\n";
    2537              :                 }
    2538              : #endif
    2539         1028 :                 const double stopTime = ceil(getSpeed() / cfModel.getMaxDecel());
    2540         1028 :                 const double leftSideOnLane = bidiLane->getWidth() - getRightSideOnLane(lane);
    2541         1028 :                 PersonDist leader = bidiLane->nextBlocking(relativePos,
    2542         1028 :                                     leftSideOnLane - getVehicleType().getWidth(), leftSideOnLane, stopTime, true);
    2543         1028 :                 if (leader.first != 0) {
    2544          267 :                     const double stopSpeed = cfModel.stopSpeed(this, getSpeed(), leader.second - getVehicleType().getMinGap());
    2545          524 :                     v = MIN2(v, stopSpeed);
    2546              : #ifdef DEBUG_PLAN_MOVE
    2547              :                     if (DEBUG_COND) {
    2548              :                         std::cout << SIMTIME << "    pedLeader=" << leader.first->getID() << " dist=" << leader.second << " v=" << v << "\n";
    2549              :                     }
    2550              : #endif
    2551              :                 }
    2552              :             }
    2553              :         }
    2554              :         // adapt to vehicles blocked from (urgent) lane-changing
    2555   1217103304 :         if (!opposite && lane->getEdge().hasLaneChanger()) {
    2556    592702737 :             const double vHelp = myLaneChangeModel->getCooperativeHelpSpeed(lane, seen);
    2557              : #ifdef DEBUG_PLAN_MOVE
    2558              :             if (DEBUG_COND && vHelp < v) {
    2559              :                 std::cout << SIMTIME << "   applying cooperativeHelpSpeed v=" << vHelp << "\n";
    2560              :             }
    2561              : #endif
    2562    592782145 :             v = MIN2(v, vHelp);
    2563              :         }
    2564              : 
    2565              :         // process all stops and waypoints on the current edge
    2566              :         bool foundRealStop = false;
    2567              :         while (stopIt != myStops.end()
    2568     66675625 :                 && ((&stopIt->lane->getEdge() == &lane->getEdge())
    2569     33772001 :                     || (stopIt->isOpposite && stopIt->lane->getEdge().getOppositeEdge() == &lane->getEdge()))
    2570              :                 // ignore stops that occur later in a looped route
    2571   1272896691 :                 && stopIt->edge == myCurrEdge + view) {
    2572     32848513 :             double stopDist = std::numeric_limits<double>::max();
    2573              :             const MSStop& stop = *stopIt;
    2574              :             const bool isFirstStop = stopIt == myStops.begin();
    2575              :             stopIt++;
    2576     32848513 :             if (!stop.reached || (stop.getSpeed() > 0 && keepStopping())) {
    2577              :                 // we are approaching a stop on the edge; must not drive further
    2578     15179488 :                 bool isWaypoint = stop.getSpeed() > 0;
    2579     15179488 :                 double endPos = stop.getEndPos(*this) + NUMERICAL_EPS;
    2580     15179488 :                 if (stop.parkingarea != nullptr) {
    2581              :                     // leave enough space so parking vehicles can exit
    2582      1661389 :                     const double brakePos = getBrakeGap() + lane->getLength() - seen;
    2583      1661389 :                     endPos = stop.parkingarea->getLastFreePosWithReservation(t, *this, brakePos);
    2584     13518099 :                 } else if (isWaypoint && !stop.reached) {
    2585       107827 :                     endPos = stop.pars.startPos;
    2586              :                 }
    2587     15179488 :                 stopDist = seen + endPos - lane->getLength();
    2588              : #ifdef DEBUG_STOPS
    2589              :                 if (DEBUG_COND) {
    2590              :                     std::cout << SIMTIME << " veh=" << getID() <<  " stopDist=" << stopDist << " stopLane=" << stop.lane->getID() << " stopEndPos=" << endPos << "\n";
    2591              :                 }
    2592              : #endif
    2593              :                 double stopSpeed = laneMaxV;
    2594     15179488 :                 if (isWaypoint) {
    2595              :                     bool waypointWithStop = false;
    2596       123361 :                     if (stop.getUntil() > t) {
    2597              :                         // check if we have to slow down or even stop
    2598              :                         SUMOTime time2end = 0;
    2599         3691 :                         if (stop.reached) {
    2600          702 :                             time2end = TIME2STEPS((stop.pars.endPos - myState.myPos) / stop.getSpeed());
    2601              :                         } else {
    2602         3267 :                             time2end = TIME2STEPS(
    2603              :                                            // time to reach waypoint start
    2604              :                                            stopDist / ((getSpeed() + stop.getSpeed()) / 2)
    2605              :                                            // time to reach waypoint end
    2606              :                                            + (stop.pars.endPos - stop.pars.startPos) / stop.getSpeed());
    2607              :                         }
    2608         3691 :                         if (stop.getUntil() > t + time2end) {
    2609              :                             // we need to stop
    2610              :                             double distToEnd = stopDist;
    2611         3398 :                             if (!stop.reached) {
    2612         2783 :                                 distToEnd += stop.pars.endPos - stop.pars.startPos;
    2613              :                             }
    2614         3398 :                             stopSpeed = MAX2(cfModel.stopSpeed(this, getSpeed(), distToEnd), vMinComfortable);
    2615              :                             waypointWithStop = true;
    2616         3398 :                             if (stopSpeed <= SUMO_const_haltingSpeed) {
    2617          531 :                                 const_cast<MSStop&>(stop).waypointWithStop = true;
    2618              :                             }
    2619              :                         }
    2620              :                     }
    2621       123361 :                     if (stop.reached) {
    2622        14790 :                         stopSpeed = MIN2(stop.getSpeed(), stopSpeed);
    2623        14790 :                         if (myState.myPos >= stop.pars.endPos && !waypointWithStop) {
    2624          278 :                             stopDist = std::numeric_limits<double>::max();
    2625              :                         }
    2626              :                     } else {
    2627       108571 :                         stopSpeed = MIN2(MAX2(cfModel.freeSpeed(this, getSpeed(), stopDist, stop.getSpeed()), vMinComfortable), stopSpeed);
    2628       108571 :                         if (!stop.reached) {
    2629       108571 :                             stopDist += stop.pars.endPos - stop.pars.startPos;
    2630              :                         }
    2631       108571 :                         if (lastLink != nullptr) {
    2632        66550 :                             lastLink->adaptLeaveSpeed(cfModel.freeSpeed(this, vLinkPass, endPos, stop.getSpeed(), false, MSCFModel::CalcReason::FUTURE));
    2633              :                         }
    2634              :                     }
    2635              :                 } else {
    2636     15056127 :                     stopSpeed = cfModel.stopSpeed(this, getSpeed(), stopDist);
    2637     15056127 :                     if (!instantStopping()) {
    2638              :                         // regular stops are not emergencies
    2639              :                         stopSpeed = MAX2(stopSpeed, vMinComfortable);
    2640           20 :                     } else if (myInfluencer && !myInfluencer->hasSpeedTimeLine(SIMSTEP)) {
    2641              :                         std::vector<std::pair<SUMOTime, double> > speedTimeLine;
    2642           20 :                         speedTimeLine.push_back(std::make_pair(SIMSTEP, getSpeed()));
    2643           20 :                         speedTimeLine.push_back(std::make_pair(SIMSTEP + DELTA_T, stopSpeed));
    2644           20 :                         myInfluencer->setSpeedTimeLine(speedTimeLine);
    2645           20 :                     }
    2646     15056127 :                     if (lastLink != nullptr) {
    2647      9254304 :                         lastLink->adaptLeaveSpeed(cfModel.stopSpeed(this, vLinkPass, endPos, MSCFModel::CalcReason::FUTURE));
    2648              :                     }
    2649              :                 }
    2650     15179488 :                 if (stopSpeed < getSpeed() && getSpeed() > SUMO_const_haltingSpeed) {
    2651              :                     // only discount braking-for-stop timeLoss if we are actually braking
    2652       613251 :                     newStopSpeed = MIN2(newStopSpeed, stopSpeed);
    2653     14872776 :                 } else if (getSpeed() < SUMO_const_haltingSpeed) {
    2654              :                     // blocked from entering a stop
    2655      7920427 :                     newStopSpeed = std::numeric_limits<double>::max();
    2656              :                 }
    2657     15179488 :                 v = MIN2(v, stopSpeed);
    2658     15179488 :                 if (lane->isInternal()) {
    2659         7009 :                     std::vector<MSLink*>::const_iterator exitLink = MSLane::succLinkSec(*this, view + 1, *lane, bestLaneConts);
    2660              :                     assert(!lane->isLinkEnd(exitLink));
    2661              :                     bool dummySetRequest;
    2662              :                     double dummyVLinkWait;
    2663         7009 :                     checkLinkLeaderCurrentAndParallel(*exitLink, lane, seen, lastLink, v, vLinkPass, dummyVLinkWait, dummySetRequest);
    2664              :                 }
    2665              : 
    2666              : #ifdef DEBUG_PLAN_MOVE
    2667              :                 if (DEBUG_COND) {
    2668              :                     std::cout << "\n" << SIMTIME << " next stop: distance = " << stopDist << " requires stopSpeed = " << stopSpeed << "\n";
    2669              : 
    2670              :                 }
    2671              : #endif
    2672     15179488 :                 if (isFirstStop) {
    2673     10373585 :                     newStopDist = stopDist;
    2674              :                     // if the vehicle is going to stop we don't need to look further
    2675              :                     // (except for trains that make use of further link-approach registration for safety purposes)
    2676     10373585 :                     if (!isWaypoint) {
    2677              :                         planningToStop = true;
    2678     10285599 :                         if (!isRail()) {
    2679      9961911 :                             lfLinks.emplace_back(v, stopDist);
    2680              :                             foundRealStop = true;
    2681              :                             break;
    2682              :                         }
    2683              :                     }
    2684              :                 }
    2685              :             }
    2686              :         }
    2687              :         if (foundRealStop) {
    2688              :             break;
    2689              :         }
    2690              : 
    2691              :         // move to next lane
    2692              :         //  get the next link used
    2693   1207141393 :         std::vector<MSLink*>::const_iterator link = MSLane::succLinkSec(*this, view + 1, *lane, bestLaneConts);
    2694   1207141393 :         if (lane->isLinkEnd(link) && myLaneChangeModel->hasBlueLight() && myCurrEdge != myRoute->end() - 1) {
    2695              :             // emergency vehicle is on the wrong lane. Obtain the link that it would use from the correct turning lane
    2696              :             const int currentIndex = lane->getIndex();
    2697              :             const MSLane* bestJump = nullptr;
    2698       193555 :             for (const LaneQ& preb : getBestLanes()) {
    2699       127294 :                 if (preb.allowsContinuation &&
    2700              :                         (bestJump == nullptr
    2701         3035 :                          || abs(currentIndex - preb.lane->getIndex()) < abs(currentIndex - bestJump->getIndex()))) {
    2702        67272 :                     bestJump = preb.lane;
    2703              :                 }
    2704              :             }
    2705        66261 :             if (bestJump != nullptr) {
    2706        66261 :                 const MSEdge* nextEdge = *(myCurrEdge + 1);
    2707       122482 :                 for (auto cand_it = bestJump->getLinkCont().begin(); cand_it != bestJump->getLinkCont().end(); cand_it++) {
    2708       117107 :                     if (&(*cand_it)->getLane()->getEdge() == nextEdge) {
    2709              :                         link = cand_it;
    2710              :                         break;
    2711              :                     }
    2712              :                 }
    2713              :             }
    2714              :         }
    2715              : 
    2716              :         // Check whether this is a turn (to save info about the next upcoming turn)
    2717   1207141393 :         if (!encounteredTurn) {
    2718    190711339 :             if (!lane->isLinkEnd(link) && lane->getLinkCont().size() > 1) {
    2719     19915348 :                 LinkDirection linkDir = (*link)->getDirection();
    2720     19915348 :                 switch (linkDir) {
    2721              :                     case LinkDirection::STRAIGHT:
    2722              :                     case LinkDirection::NODIR:
    2723              :                         break;
    2724      7872859 :                     default:
    2725      7872859 :                         nextTurn.first = seen;
    2726      7872859 :                         nextTurn.second = *link;
    2727              :                         encounteredTurn = true;
    2728              : #ifdef DEBUG_NEXT_TURN
    2729              :                         if (DEBUG_COND) {
    2730              :                             std::cout << SIMTIME << " veh '" << getID() << "' nextTurn: " << toString(linkDir)
    2731              :                                       << " at " << nextTurn.first << "m." << std::endl;
    2732              :                         }
    2733              : #endif
    2734              :                 }
    2735              :             }
    2736              :         }
    2737              : 
    2738              :         //  check whether the vehicle is on its final edge
    2739   2080475919 :         if (myCurrEdge + view + 1 == myRoute->end()
    2740   1207141393 :                 || (myParameter->arrivalEdge >= 0 && getRoutePosition() + view == myParameter->arrivalEdge)) {
    2741    333806867 :             const double arrivalSpeed = (myParameter->arrivalSpeedProcedure == ArrivalSpeedDefinition::GIVEN ?
    2742              :                                          myParameter->arrivalSpeed : laneMaxV);
    2743              :             // subtract the arrival speed from the remaining distance so we get one additional driving step with arrival speed
    2744              :             // XXX: This does not work for ballistic update refs #2579
    2745    333806867 :             const double distToArrival = seen + myArrivalPos - lane->getLength() - SPEED2DIST(arrivalSpeed);
    2746    333806867 :             const double va = MAX2(NUMERICAL_EPS, cfModel.freeSpeed(this, getSpeed(), distToArrival, arrivalSpeed));
    2747    333806867 :             v = MIN2(v, va);
    2748    333806867 :             if (lastLink != nullptr) {
    2749              :                 lastLink->adaptLeaveSpeed(va);
    2750              :             }
    2751    333806867 :             lfLinks.push_back(DriveProcessItem(v, seen, lane->getEdge().isFringe() ? 1000 : 0));
    2752    333806867 :             break;
    2753              :         }
    2754              :         // check whether the lane or the shadowLane is a dead end (allow some leeway on intersections)
    2755              :         if (lane->isLinkEnd(link)
    2756    864331447 :                 || (MSGlobals::gSublane && brakeForOverlap(*link, lane))
    2757   1737312604 :                 || (opposite && (*link)->getViaLaneOrLane()->getParallelOpposite() == nullptr
    2758       212444 :                     && !myLaneChangeModel->hasBlueLight())) {
    2759      9608609 :             double va = cfModel.stopSpeed(this, getSpeed(), seen);
    2760      9608609 :             if (lastLink != nullptr) {
    2761              :                 lastLink->adaptLeaveSpeed(va);
    2762              :             }
    2763      9608609 :             if (myLaneChangeModel->getCommittedSpeed() > 0) {
    2764       373286 :                 v = MIN2(myLaneChangeModel->getCommittedSpeed(), v);
    2765              :             } else {
    2766     17936424 :                 v = MIN2(va, v);
    2767              :             }
    2768              : #ifdef DEBUG_PLAN_MOVE
    2769              :             if (DEBUG_COND) {
    2770              :                 std::cout << "   braking for link end lane=" << lane->getID() << " seen=" << seen
    2771              :                           << " overlap=" << getLateralOverlap() << " va=" << va << " committed=" << myLaneChangeModel->getCommittedSpeed() << " v=" << v << "\n";
    2772              : 
    2773              :             }
    2774              : #endif
    2775      9608609 :             if (lane->isLinkEnd(link)) {
    2776      9003079 :                 lfLinks.emplace_back(v, seen);
    2777              :                 break;
    2778              :             }
    2779              :         }
    2780    864331447 :         lateralShift += (*link)->getLateralShift();
    2781    864331447 :         const bool yellowOrRed = (*link)->haveRed() || (*link)->haveYellow();
    2782              :         // We distinguish 3 cases when determining the point at which a vehicle stops:
    2783              :         // - allway_stop: the vehicle should stop close to the stop line but may stop at larger distance
    2784              :         // - red/yellow light: here the vehicle 'knows' that it will have priority eventually and does not need to stop on a precise spot
    2785              :         // - other types of minor links: the vehicle needs to stop as close to the junction as necessary
    2786              :         //   to minimize the time window for passing the junction. If the
    2787              :         //   vehicle 'decides' to accelerate and cannot enter the junction in
    2788              :         //   the next step, new foes may appear and cause a collision (see #1096)
    2789              :         // - major links: stopping point is irrelevant
    2790              :         double laneStopOffset;
    2791    864331447 :         const double majorStopOffset = MAX2(getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_STOPLINE_GAP, DIST_TO_STOPLINE_EXPECT_PRIORITY), lane->getVehicleStopOffset(this));
    2792              :         // override low desired decel at yellow and red
    2793    864331447 :         const double stopDecel = yellowOrRed && !isRail() ? MAX2(MIN2(MSGlobals::gTLSYellowMinDecel, cfModel.getEmergencyDecel()), cfModel.getMaxDecel()) : cfModel.getMaxDecel();
    2794    864331447 :         const double brakeDist = cfModel.brakeGap(myState.mySpeed, stopDecel, 0);
    2795    864331447 :         const bool canBrakeBeforeLaneEnd = seen >= brakeDist;
    2796    864331447 :         const bool canBrakeBeforeStopLine = seen - lane->getVehicleStopOffset(this) >= brakeDist;
    2797    864331447 :         if (yellowOrRed) {
    2798              :             // Wait at red traffic light with full distance if possible
    2799              :             laneStopOffset = majorStopOffset;
    2800    802985507 :         } else if ((*link)->havePriority()) {
    2801              :             // On priority link, we should never stop below visibility distance
    2802    758061632 :             laneStopOffset = MIN2((*link)->getFoeVisibilityDistance() - POSITION_EPS, majorStopOffset);
    2803              :         } else {
    2804     44923875 :             double minorStopOffset = MAX2(lane->getVehicleStopOffset(this),
    2805     44923875 :                                           getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_STOPLINE_CROSSING_GAP, MSPModel::SAFETY_GAP) - (*link)->getDistToFoePedCrossing());
    2806              : #ifdef DEBUG_PLAN_MOVE
    2807              :             if (DEBUG_COND) {
    2808              :                 std::cout << "  minorStopOffset=" << minorStopOffset << " distToFoePedCrossing=" << (*link)->getDistToFoePedCrossing() << "\n";
    2809              :             }
    2810              : #endif
    2811     44923875 :             if ((*link)->getState() == LINKSTATE_ALLWAY_STOP) {
    2812      1425260 :                 minorStopOffset = MAX2(minorStopOffset, getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_STOPLINE_GAP, 0));
    2813              :             } else {
    2814     43498615 :                 minorStopOffset = MAX2(minorStopOffset, getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_STOPLINE_GAP_MINOR, 0));
    2815              :             }
    2816              :             // On minor link, we should likewise never stop below visibility distance
    2817     44923875 :             laneStopOffset = MIN2((*link)->getFoeVisibilityDistance() - POSITION_EPS, minorStopOffset);
    2818              :         }
    2819              : #ifdef DEBUG_PLAN_MOVE
    2820              :         if (DEBUG_COND) {
    2821              :             std::cout << SIMTIME << " veh=" << getID() << " desired stopOffset on lane '" << lane->getID() << "' is " << laneStopOffset << "\n";
    2822              :         }
    2823              : #endif
    2824    864331447 :         if (canBrakeBeforeLaneEnd) {
    2825              :             // avoid emergency braking if possible
    2826    836948055 :             laneStopOffset = MIN2(laneStopOffset, seen - brakeDist);
    2827              :         }
    2828              :         laneStopOffset = MAX2(POSITION_EPS, laneStopOffset);
    2829    864331447 :         double stopDist = MAX2(0., seen - laneStopOffset);
    2830     61345940 :         if (yellowOrRed && getDevice(typeid(MSDevice_GLOSA)) != nullptr
    2831          508 :                 && static_cast<MSDevice_GLOSA*>(getDevice(typeid(MSDevice_GLOSA)))->getOverrideSafety()
    2832    864331447 :                 && static_cast<MSDevice_GLOSA*>(getDevice(typeid(MSDevice_GLOSA)))->isSpeedAdviceActive()) {
    2833              :             stopDist = std::numeric_limits<double>::max();
    2834              :         }
    2835    864331447 :         if (newStopDist != std::numeric_limits<double>::max()) {
    2836              :             stopDist = MAX2(stopDist, newStopDist);
    2837              :         }
    2838              : #ifdef DEBUG_PLAN_MOVE
    2839              :         if (DEBUG_COND) {
    2840              :             std::cout << SIMTIME << " veh=" << getID() << " effective stopOffset on lane '" << lane->getID()
    2841              :                       << "' is " << laneStopOffset << " (-> stopDist=" << stopDist << ")" << std::endl;
    2842              :         }
    2843              : #endif
    2844    864331447 :         if (isRail()
    2845    864331447 :                 && !lane->isInternal()) {
    2846              :             // check for train direction reversal
    2847      3213992 :             if (lane->getBidiLane() != nullptr
    2848      3213992 :                     && (*link)->getLane()->getBidiLane() == lane) {
    2849       631456 :                 double vMustReverse = getCarFollowModel().stopSpeed(this, getSpeed(), seen - POSITION_EPS);
    2850       631456 :                 if (seen < 1) {
    2851         2277 :                     mustSeeBeforeReversal = 2 * seen + getLength();
    2852              :                 }
    2853      1221824 :                 v = MIN2(v, vMustReverse);
    2854              :             }
    2855              :             // signal that is passed in the current step does not count
    2856      6427984 :             foundRailSignal |= ((*link)->getTLLogic() != nullptr
    2857       793762 :                                 && (*link)->getTLLogic()->getLogicType() == TrafficLightType::RAIL_SIGNAL
    2858      3907206 :                                 && seen > SPEED2DIST(v));
    2859              :         }
    2860              : 
    2861    864331447 :         bool canReverseEventually = false;
    2862    864331447 :         const double vReverse = checkReversal(canReverseEventually, laneMaxV, seen);
    2863    864331447 :         v = MIN2(v, vReverse);
    2864              : #ifdef DEBUG_PLAN_MOVE
    2865              :         if (DEBUG_COND) {
    2866              :             std::cout << SIMTIME << " veh=" << getID() << " canReverseEventually=" << canReverseEventually << " v=" << v << "\n";
    2867              :         }
    2868              : #endif
    2869              : 
    2870              :         // check whether we need to slow down in order to finish a continuous lane change
    2871    864331447 :         if (myLaneChangeModel->isChangingLanes()) {
    2872              :             if (    // slow down to finish lane change before a turn lane
    2873       179825 :                 ((*link)->getDirection() == LinkDirection::LEFT || (*link)->getDirection() == LinkDirection::RIGHT) ||
    2874              :                 // slow down to finish lane change before the shadow lane ends
    2875       146767 :                 (myLaneChangeModel->getShadowLane() != nullptr &&
    2876       146767 :                  (*link)->getViaLaneOrLane()->getParallelLane(myLaneChangeModel->getShadowDirection()) == nullptr)) {
    2877              :                 // XXX maybe this is too harsh. Vehicles could cut some corners here
    2878        54636 :                 const double timeRemaining = STEPS2TIME(myLaneChangeModel->remainingTime());
    2879              :                 assert(timeRemaining != 0);
    2880              :                 // XXX: Euler-logic (#860), but I couldn't identify problems from this yet (Leo). Refs. #2575
    2881        54636 :                 const double va = MAX2(cfModel.stopSpeed(this, getSpeed(), seen - POSITION_EPS),
    2882        54636 :                                        (seen - POSITION_EPS) / timeRemaining);
    2883              : #ifdef DEBUG_PLAN_MOVE
    2884              :                 if (DEBUG_COND) {
    2885              :                     std::cout << SIMTIME << " veh=" << getID() << " slowing down to finish continuous change before"
    2886              :                               << " link=" << (*link)->getViaLaneOrLane()->getID()
    2887              :                               << " timeRemaining=" << timeRemaining
    2888              :                               << " v=" << v
    2889              :                               << " va=" << va
    2890              :                               << std::endl;
    2891              :                 }
    2892              : #endif
    2893       108776 :                 v = MIN2(va, v);
    2894              :             }
    2895              :         }
    2896              : 
    2897              :         // - always issue a request to leave the intersection we are currently on
    2898    864331447 :         const bool leavingCurrentIntersection = myLane->getEdge().isInternal() && lastLink == nullptr;
    2899              :         // - do not issue a request to enter an intersection after we already slowed down for an earlier one
    2900    864331447 :         const bool abortRequestAfterMinor = slowedDownForMinor && (*link)->getInternalLaneBefore() == nullptr;
    2901              :         // - even if red, if we cannot break we should issue a request
    2902    864331447 :         bool setRequest = (v > NUMERICAL_EPS_SPEED && !abortRequestAfterMinor) || (leavingCurrentIntersection);
    2903              : 
    2904    864331447 :         double stopSpeed = cfModel.stopSpeed(this, getSpeed(), stopDist, stopDecel, MSCFModel::CalcReason::CURRENT_WAIT);
    2905    864331447 :         double vLinkWait = MIN2(v, stopSpeed);
    2906              : #ifdef DEBUG_PLAN_MOVE
    2907              :         if (DEBUG_COND) {
    2908              :             std::cout
    2909              :                     << " stopDist=" << stopDist
    2910              :                     << " stopDecel=" << stopDecel
    2911              :                     << " vLinkWait=" << vLinkWait
    2912              :                     << " brakeDist=" << brakeDist
    2913              :                     << " seen=" << seen
    2914              :                     << " leaveIntersection=" << leavingCurrentIntersection
    2915              :                     << " setRequest=" << setRequest
    2916              :                     //<< std::setprecision(16)
    2917              :                     //<< " v=" << v
    2918              :                     //<< " speedEps=" << NUMERICAL_EPS_SPEED
    2919              :                     //<< std::setprecision(gPrecision)
    2920              :                     << "\n";
    2921              :         }
    2922              : #endif
    2923              : 
    2924    864331447 :         if (yellowOrRed && canBrakeBeforeStopLine && !ignoreRed(*link, canBrakeBeforeStopLine) && seen >= mustSeeBeforeReversal) {
    2925     61284054 :             if (lane->isInternal()) {
    2926        42351 :                 checkLinkLeaderCurrentAndParallel(*link, lane, seen, lastLink, v, vLinkPass, vLinkWait, setRequest);
    2927              :             }
    2928              :             // arrivalSpeed / arrivalTime when braking for red light is only relevent for rail signal switching
    2929     61284054 :             const SUMOTime arrivalTime = getArrivalTime(t, seen, v, vLinkPass);
    2930              :             // the vehicle is able to brake in front of a yellow/red traffic light
    2931     61284054 :             lfLinks.push_back(DriveProcessItem(*link, v, vLinkWait, false, arrivalTime, vLinkWait, 0, seen, -1));
    2932              :             //lfLinks.push_back(DriveProcessItem(0, vLinkWait, vLinkWait, false, 0, 0, stopDist));
    2933     61284054 :             break;
    2934              :         }
    2935              : 
    2936    803047393 :         const MSLink* entryLink = (*link)->getCorrespondingEntryLink();
    2937    803047393 :         if (entryLink->haveRed() && ignoreRed(*link, canBrakeBeforeStopLine) && STEPS2TIME(t - entryLink->getLastStateChange()) > 2) {
    2938              :             // restrict speed when ignoring a red light
    2939       118114 :             const double redSpeed = MIN2(v, getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_DRIVE_RED_SPEED, v));
    2940       118114 :             const double va = MAX2(redSpeed, cfModel.freeSpeed(this, getSpeed(), seen, redSpeed));
    2941       235780 :             v = MIN2(va, v);
    2942              : #ifdef DEBUG_PLAN_MOVE
    2943              :             if (DEBUG_COND) std::cout
    2944              :                         << "   ignoreRed spent=" << STEPS2TIME(t - (*link)->getLastStateChange())
    2945              :                         << " redSpeed=" << redSpeed
    2946              :                         << " va=" << va
    2947              :                         << " v=" << v
    2948              :                         << "\n";
    2949              : #endif
    2950              :         }
    2951              : 
    2952    803047393 :         checkLinkLeaderCurrentAndParallel(*link, lane, seen, lastLink, v, vLinkPass, vLinkWait, setRequest);
    2953              : 
    2954    803047393 :         if (lastLink != nullptr) {
    2955              :             lastLink->adaptLeaveSpeed(laneMaxV);
    2956              :         }
    2957    803047393 :         double arrivalSpeed = vLinkPass;
    2958              :         // vehicles should decelerate when approaching a minor link
    2959              :         // - unless they are close enough to have clear visibility of all relevant foe lanes and may start to accelerate again
    2960              :         // - and unless they are so close that stopping is impossible (i.e. when a green light turns to yellow when close to the junction)
    2961              : 
    2962              :         // whether the vehicle/driver is close enough to the link to see all possible foes #2123
    2963    803047393 :         const double visibilityDistance = (*link)->getFoeVisibilityDistance();
    2964    803047393 :         const double determinedFoePresence = seen <= visibilityDistance;
    2965              : //        // VARIANT: account for time needed to recognize whether relevant vehicles are on the foe lanes. (Leo)
    2966              : //        double foeRecognitionTime = 0.0;
    2967              : //        double determinedFoePresence = seen < visibilityDistance - myState.mySpeed*foeRecognitionTime;
    2968              : 
    2969              : #ifdef DEBUG_PLAN_MOVE
    2970              :         if (DEBUG_COND) {
    2971              :             std::cout << " approaching link=" << (*link)->getViaLaneOrLane()->getID() << " prio=" << (*link)->havePriority() << " seen=" << seen << " visibilityDistance=" << visibilityDistance << " brakeDist=" << brakeDist << "\n";
    2972              :         }
    2973              : #endif
    2974              : 
    2975    803047393 :         const bool couldBrakeForMinor = !(*link)->havePriority() && brakeDist < seen && !(*link)->lastWasContMajor();
    2976     44417904 :         if (couldBrakeForMinor && !determinedFoePresence) {
    2977              :             // vehicle decelerates just enough to be able to stop if necessary and then accelerates
    2978     41622894 :             double maxSpeedAtVisibilityDist = cfModel.maximumSafeStopSpeed(visibilityDistance, cfModel.getMaxDecel(), myState.mySpeed, false, 0., false);
    2979              :             // XXX: estimateSpeedAfterDistance does not use euler-logic (thus returns a lower value than possible here...)
    2980     41622894 :             double maxArrivalSpeed = cfModel.estimateSpeedAfterDistance(visibilityDistance, maxSpeedAtVisibilityDist, cfModel.getMaxAccel());
    2981     41622894 :             arrivalSpeed = MIN2(vLinkPass, maxArrivalSpeed);
    2982              :             slowedDownForMinor = true;
    2983              : #ifdef DEBUG_PLAN_MOVE
    2984              :             if (DEBUG_COND) {
    2985              :                 std::cout << "   slowedDownForMinor maxSpeedAtVisDist=" << maxSpeedAtVisibilityDist << " maxArrivalSpeed=" << maxArrivalSpeed << " arrivalSpeed=" << arrivalSpeed << "\n";
    2986              :             }
    2987              : #endif
    2988    761424499 :         } else if ((*link)->getState() == LINKSTATE_EQUAL && myWaitingTime > 0) {
    2989              :             // check for deadlock (circular yielding)
    2990              :             //std::cout << SIMTIME << " veh=" << getID() << " check rbl-deadlock\n";
    2991         2810 :             std::pair<const SUMOVehicle*, const MSLink*> blocker = (*link)->getFirstApproachingFoe(*link);
    2992              :             //std::cout << "   blocker=" << Named::getIDSecure(blocker.first) << "\n";
    2993              :             int n = 100;
    2994         5622 :             while (blocker.second != nullptr && blocker.second != *link && n > 0) {
    2995         2812 :                 blocker = blocker.second->getFirstApproachingFoe(*link);
    2996         2812 :                 n--;
    2997              :                 //std::cout << "   blocker=" << Named::getIDSecure(blocker.first) << "\n";
    2998              :             }
    2999         2810 :             if (n == 0) {
    3000            0 :                 WRITE_WARNINGF(TL("Suspicious right_before_left junction '%'."), lane->getEdge().getToJunction()->getID());
    3001              :             }
    3002              :             //std::cout << "   blockerLink=" << blocker.second << " link=" << *link << "\n";
    3003         2810 :             if (blocker.second == *link) {
    3004          489 :                 const double threshold = (*link)->getDirection() == LinkDirection::STRAIGHT ? 0.25 : 0.75;
    3005          489 :                 if (RandHelper::rand(getRNG()) < threshold) {
    3006              :                     //std::cout << "   abort request, threshold=" << threshold << "\n";
    3007          308 :                     setRequest = false;
    3008              :                 }
    3009              :             }
    3010              :         }
    3011              : 
    3012    803047393 :         const SUMOTime arrivalTime = getArrivalTime(t, seen, v, arrivalSpeed);
    3013    803047393 :         if (couldBrakeForMinor && determinedFoePresence && (*link)->getLane()->getEdge().isRoundabout()) {
    3014       865690 :             const bool wasOpened = (*link)->opened(arrivalTime, arrivalSpeed, arrivalSpeed,
    3015       865690 :                                                    getLength(), getImpatience(),
    3016              :                                                    getCarFollowModel().getMaxDecel(),
    3017       865690 :                                                    getWaitingTime(), getLateralPositionOnLane(),
    3018              :                                                    nullptr, false, this);
    3019       865690 :             if (!wasOpened) {
    3020              :                 slowedDownForMinor = true;
    3021              :             }
    3022              : #ifdef DEBUG_PLAN_MOVE
    3023              :             if (DEBUG_COND) {
    3024              :                 std::cout << "   slowedDownForMinor at roundabout=" << (!wasOpened) << "\n";
    3025              :             }
    3026              : #endif
    3027              :         }
    3028              : 
    3029              :         // compute arrival speed and arrival time if vehicle starts braking now
    3030              :         // if stopping is possible, arrivalTime can be arbitrarily large. A small value keeps fractional times (impatience) meaningful
    3031              :         double arrivalSpeedBraking = 0;
    3032    803047393 :         const double bGap = cfModel.brakeGap(v);
    3033    803047393 :         if (seen < bGap && !isStopped() && !planningToStop) { // XXX: should this use the current speed (at least for the ballistic case)? (Leo) Refs. #2575
    3034              :             // vehicle cannot come to a complete stop in time
    3035     56724210 :             if (MSGlobals::gSemiImplicitEulerUpdate) {
    3036     54009753 :                 arrivalSpeedBraking = cfModel.getMinimalArrivalSpeedEuler(seen, v);
    3037              :                 // due to discrete/continuous mismatch (when using Euler update) we have to ensure that braking actually helps
    3038              :                 arrivalSpeedBraking = MIN2(arrivalSpeedBraking, arrivalSpeed);
    3039              :             } else {
    3040      2714457 :                 arrivalSpeedBraking = cfModel.getMinimalArrivalSpeed(seen, myState.mySpeed);
    3041              :             }
    3042              :         }
    3043              : 
    3044              :         // estimate leave speed for passing time computation
    3045              :         // l=linkLength, a=accel, t=continuousTime, v=vLeave
    3046              :         // l=v*t + 0.5*a*t^2, solve for t and multiply with a, then add v
    3047   1189623749 :         const double estimatedLeaveSpeed = MIN2((*link)->getViaLaneOrLane()->getVehicleMaxSpeed(this, maxVD),
    3048    803047393 :                                                 getCarFollowModel().estimateSpeedAfterDistance((*link)->getLength(), arrivalSpeed, getVehicleType().getCarFollowModel().getMaxAccel()));
    3049    803047393 :         lfLinks.push_back(DriveProcessItem(*link, v, vLinkWait, setRequest,
    3050              :                                            arrivalTime, arrivalSpeed,
    3051              :                                            arrivalSpeedBraking,
    3052              :                                            seen, estimatedLeaveSpeed));
    3053    803047393 :         if ((*link)->getViaLane() == nullptr) {
    3054              :             hadNonInternal = true;
    3055              :             ++view;
    3056              :         }
    3057              : #ifdef DEBUG_PLAN_MOVE
    3058              :         if (DEBUG_COND) {
    3059              :             std::cout << "   checkAbort setRequest=" << setRequest << " v=" << v << " seen=" << seen << " dist=" << dist
    3060              :                       << " seenNonInternal=" << seenNonInternal
    3061              :                       << " seenInternal=" << seenInternal << " length=" << vehicleLength << "\n";
    3062              :         }
    3063              : #endif
    3064              :         // we need to look ahead far enough to see available space for checkRewindLinkLanes
    3065    829099540 :         if ((!setRequest || v <= 0 || seen > dist) && hadNonInternal && seenNonInternal > MAX2(vehicleLength * CRLL_LOOK_AHEAD, vehicleLength + seenInternal) && foundRailSignal) {
    3066              :             break;
    3067              :         }
    3068              :         // get the following lane
    3069              :         lane = (*link)->getViaLaneOrLane();
    3070    584630965 :         laneMaxV = lane->getVehicleMaxSpeed(this, maxVD);
    3071    585029787 :         if (myInfluencer && !myInfluencer->considerSpeedLimit()) {
    3072              :             laneMaxV = std::numeric_limits<double>::max();
    3073              :         }
    3074              :         // the link was passed
    3075              :         // compute the velocity to use when the link is not blocked by other vehicles
    3076              :         //  the vehicle shall be not faster when reaching the next lane than allowed
    3077              :         //  speed limits are not emergencies (e.g. when the limit changes suddenly due to TraCI or a variableSpeedSignal)
    3078    584630965 :         const double va = MAX2(cfModel.freeSpeed(this, getSpeed(), seen, laneMaxV), vMinComfortable - NUMERICAL_EPS);
    3079   1159478289 :         v = MIN2(va, v);
    3080              : #ifdef DEBUG_PLAN_MOVE
    3081              :         if (DEBUG_COND) {
    3082              :             std::cout << "   laneMaxV=" << laneMaxV << " freeSpeed=" << va << " v=" << v << "\n";
    3083              :         }
    3084              : #endif
    3085    584630965 :         if (lane->getEdge().isInternal()) {
    3086    255211668 :             seenInternal += lane->getLength();
    3087              :         } else {
    3088    329419297 :             seenNonInternal += lane->getLength();
    3089              :         }
    3090              :         // do not restrict results to the current vehicle to allow caching for the current time step
    3091    584630965 :         leaderLane = opposite ? lane->getParallelOpposite() : lane;
    3092    584630965 :         if (leaderLane == nullptr) {
    3093              : 
    3094              :             break;
    3095              :         }
    3096   1168840396 :         ahead = opposite ? MSLeaderInfo(leaderLane->getWidth()) : leaderLane->getLastVehicleInformation(nullptr, 0);
    3097    584420198 :         seen += lane->getLength();
    3098   1168840396 :         vLinkPass = MIN2(cfModel.estimateSpeedAfterDistance(lane->getLength(), v, cfModel.getMaxAccel()), laneMaxV); // upper bound
    3099              :         lastLink = &lfLinks.back();
    3100    584420198 :     }
    3101              : 
    3102              : //#ifdef DEBUG_PLAN_MOVE
    3103              : //    if(DEBUG_COND){
    3104              : //        std::cout << "planMoveInternal found safe speed v = " << v << std::endl;
    3105              : //    }
    3106              : //#endif
    3107              : 
    3108              : #ifdef PARALLEL_STOPWATCH
    3109              :     myLane->getStopWatch()[0].stop();
    3110              : #endif
    3111    632683106 : }
    3112              : 
    3113              : 
    3114              : double
    3115         2786 : MSVehicle::slowDownForSchedule(double vMinComfortable) const {
    3116         2786 :     const double sfp = getVehicleType().getParameter().speedFactorPremature;
    3117              :     const MSStop& stop = myStops.front();
    3118         2786 :     std::pair<double, double> timeDist = estimateTimeToNextStop();
    3119         2786 :     double arrivalDelay = SIMTIME + timeDist.first - STEPS2TIME(stop.pars.arrival);
    3120         2786 :     double t = STEPS2TIME(stop.pars.arrival - SIMSTEP);
    3121         5572 :     if (stop.pars.hasParameter(toString(SUMO_ATTR_FLEX_ARRIVAL))) {
    3122          150 :         SUMOTime flexStart = string2time(stop.pars.getParameter(toString(SUMO_ATTR_FLEX_ARRIVAL)));
    3123           75 :         arrivalDelay += STEPS2TIME(stop.pars.arrival - flexStart);
    3124           75 :         t = STEPS2TIME(flexStart - SIMSTEP);
    3125         2711 :     } else if (stop.pars.started >= 0 && MSGlobals::gUseStopStarted) {
    3126          200 :         arrivalDelay += STEPS2TIME(stop.pars.arrival - stop.pars.started);
    3127          200 :         t = STEPS2TIME(stop.pars.started - SIMSTEP);
    3128              :     }
    3129         2786 :     if (arrivalDelay < 0 && sfp < getChosenSpeedFactor()) {
    3130              :         // we can slow down to better match the schedule (and increase energy efficiency)
    3131         2721 :         const double vSlowDownMin = MAX2(myLane->getSpeedLimit() * sfp, vMinComfortable);
    3132         2721 :         const double s = timeDist.second;
    3133              :         const double b = getCarFollowModel().getMaxDecel();
    3134              :         // x = speed for arriving in t seconds
    3135              :         // u = time at full speed
    3136              :         // u * x + (t - u) * 0.5 * x = s
    3137              :         // t - u = x / b
    3138              :         // eliminate u, solve x
    3139         2721 :         const double radicand = 4 * t * t * b * b - 8 * s * b;
    3140         2721 :         const double x = radicand >= 0 ? t * b - sqrt(radicand) * 0.5 : vSlowDownMin;
    3141         2721 :         double vSlowDown = x < vSlowDownMin ? vSlowDownMin : x;
    3142              : #ifdef DEBUG_PLAN_MOVE
    3143              :         if (DEBUG_COND) {
    3144              :             std::cout << SIMTIME << " veh=" << getID() << " ad=" << arrivalDelay << " t=" << t << " vsm=" << vSlowDownMin
    3145              :                       << " r=" << radicand << " vs=" << vSlowDown << "\n";
    3146              :         }
    3147              : #endif
    3148         2721 :         return vSlowDown;
    3149           65 :     } else if (arrivalDelay > 0 && sfp > getChosenSpeedFactor()) {
    3150              :         // in principle we could up to catch up with the schedule
    3151              :         // but at this point we can only lower the speed, the
    3152              :         // information would have to be used when computing getVehicleMaxSpeed
    3153              :     }
    3154           65 :     return getMaxSpeed();
    3155              : }
    3156              : 
    3157              : SUMOTime
    3158    864331447 : MSVehicle::getArrivalTime(SUMOTime t, double seen, double v, double arrivalSpeed) const {
    3159              :     const MSCFModel& cfModel = getCarFollowModel();
    3160              :     SUMOTime arrivalTime;
    3161    864331447 :     if (MSGlobals::gSemiImplicitEulerUpdate) {
    3162              :         // @note intuitively it would make sense to compare arrivalSpeed with getSpeed() instead of v
    3163              :         // however, due to the current position update rule (ticket #860) the vehicle moves with v in this step
    3164              :         // subtract DELTA_T because t is the time at the end of this step and the movement is not carried out yet
    3165    807506308 :         arrivalTime = t - DELTA_T + cfModel.getMinimalArrivalTime(seen, v, arrivalSpeed);
    3166              :     } else {
    3167     56825139 :         arrivalTime = t - DELTA_T + cfModel.getMinimalArrivalTime(seen, myState.mySpeed, arrivalSpeed);
    3168              :     }
    3169    864331447 :     if (isStopped()) {
    3170     12153797 :         arrivalTime += MAX2((SUMOTime)0, myStops.front().duration);
    3171              :     }
    3172    864331447 :     return arrivalTime;
    3173              : }
    3174              : 
    3175              : 
    3176              : void
    3177   1222115247 : MSVehicle::adaptToLeaders(const MSLeaderInfo& ahead, double latOffset,
    3178              :                           const double seen, DriveProcessItem* const lastLink,
    3179              :                           const MSLane* const lane, double& v, double& vLinkPass) const {
    3180              :     int rightmost;
    3181              :     int leftmost;
    3182   1222115247 :     ahead.getSubLanes(this, latOffset, rightmost, leftmost);
    3183              : #ifdef DEBUG_PLAN_MOVE
    3184              :     if (DEBUG_COND) std::cout << SIMTIME
    3185              :                                   << "\nADAPT_TO_LEADERS\nveh=" << getID()
    3186              :                                   << " lane=" << lane->getID()
    3187              :                                   << " latOffset=" << latOffset
    3188              :                                   << " rm=" << rightmost
    3189              :                                   << " lm=" << leftmost
    3190              :                                   << " shift=" << ahead.getSublaneOffset()
    3191              :                                   << " ahead=" << ahead.toString()
    3192              :                                   << "\n";
    3193              : #endif
    3194              :     /*
    3195              :     if (myLaneChangeModel->getCommittedSpeed() > 0) {
    3196              :         v = MIN2(v, myLaneChangeModel->getCommittedSpeed());
    3197              :         vLinkPass = MIN2(vLinkPass, myLaneChangeModel->getCommittedSpeed());
    3198              :     #ifdef DEBUG_PLAN_MOVE
    3199              :         if (DEBUG_COND) std::cout << "   hasCommitted=" << myLaneChangeModel->getCommittedSpeed() << "\n";
    3200              :     #endif
    3201              :         return;
    3202              :     }
    3203              :     */
    3204   2996799852 :     for (int sublane = rightmost; sublane <= leftmost; ++sublane) {
    3205   1774684605 :         const MSVehicle* pred = ahead[sublane];
    3206   1774684605 :         if (pred != nullptr && pred != this) {
    3207              :             // @todo avoid multiple adaptations to the same leader
    3208   1295357971 :             const double predBack = pred->getBackPositionOnLane(lane);
    3209              :             double gap = (lastLink == nullptr
    3210   1855995728 :                           ? predBack - myState.myPos - getVehicleType().getMinGap()
    3211    560637757 :                           : predBack + seen - lane->getLength() - getVehicleType().getMinGap());
    3212              :             bool oncoming = false;
    3213   1295357971 :             if (myLaneChangeModel->isOpposite()) {
    3214        26990 :                 if (pred->getLaneChangeModel().isOpposite() || lane == pred->getLaneChangeModel().getShadowLane()) {
    3215              :                     // ego might and leader are driving against lane
    3216              :                     gap = (lastLink == nullptr
    3217            0 :                            ? myState.myPos - predBack - getVehicleType().getMinGap()
    3218            0 :                            : predBack + seen - lane->getLength() - getVehicleType().getMinGap());
    3219              :                 } else {
    3220              :                     // ego and leader are driving in the same direction as lane (shadowlane for ego)
    3221              :                     gap = (lastLink == nullptr
    3222        27710 :                            ? predBack - (myLane->getLength() - myState.myPos) - getVehicleType().getMinGap()
    3223          720 :                            : predBack + seen - lane->getLength() - getVehicleType().getMinGap());
    3224              :                 }
    3225   1295330981 :             } else if (pred->getLaneChangeModel().isOpposite() && pred->getLaneChangeModel().getShadowLane() != lane) {
    3226              :                 // must react to stopped / dangerous oncoming vehicles
    3227       188434 :                 gap += -pred->getVehicleType().getLength() + getVehicleType().getMinGap() - MAX2(getVehicleType().getMinGap(), pred->getVehicleType().getMinGap());
    3228              :                 // try to avoid collision in the next second
    3229       188434 :                 const double predMaxDist = pred->getSpeed() + pred->getCarFollowModel().getMaxAccel();
    3230              : #ifdef DEBUG_PLAN_MOVE
    3231              :                 if (DEBUG_COND) {
    3232              :                     std::cout << "    fixedGap=" << gap << " predMaxDist=" << predMaxDist << "\n";
    3233              :                 }
    3234              : #endif
    3235       188434 :                 if (gap < predMaxDist + getSpeed() || pred->getLane() == lane->getBidiLane()) {
    3236        20609 :                     gap -= predMaxDist;
    3237              :                 }
    3238   1295142547 :             } else if (pred->getLane() == lane->getBidiLane()) {
    3239       618938 :                 gap -= pred->getVehicleType().getLengthWithGap();
    3240              :                 oncoming = true;
    3241              :             }
    3242              : #ifdef DEBUG_PLAN_MOVE
    3243              :             if (DEBUG_COND) {
    3244              :                 std::cout << "     pred=" << pred->getID() << " predLane=" << pred->getLane()->getID() << " predPos=" << pred->getPositionOnLane() << " gap=" << gap << " predBack=" << predBack << " seen=" << seen << " lane=" << lane->getID() << " myLane=" << myLane->getID() << " lastLink=" << (lastLink == nullptr ? "NULL" : lastLink->myLink->getDescription()) << " oncoming=" << oncoming << "\n";
    3245              :             }
    3246              : #endif
    3247       618938 :             if (oncoming && gap >= 0) {
    3248       555464 :                 adaptToOncomingLeader(std::make_pair(pred, gap), lastLink, v, vLinkPass);
    3249              :             } else {
    3250   1294802507 :                 adaptToLeader(std::make_pair(pred, gap), seen, lastLink, v, vLinkPass);
    3251              :             }
    3252              :         }
    3253              :     }
    3254   1222115247 : }
    3255              : 
    3256              : void
    3257       427873 : MSVehicle::adaptToLeaderDistance(const MSLeaderDistanceInfo& ahead, double latOffset,
    3258              :                                  double seen,
    3259              :                                  DriveProcessItem* const lastLink,
    3260              :                                  double& v, double& vLinkPass) const {
    3261              :     int rightmost;
    3262              :     int leftmost;
    3263       427873 :     ahead.getSubLanes(this, latOffset, rightmost, leftmost);
    3264              : #ifdef DEBUG_PLAN_MOVE
    3265              :     if (DEBUG_COND) std::cout << SIMTIME
    3266              :                                   << "\nADAPT_TO_LEADERS_DISTANCE\nveh=" << getID()
    3267              :                                   << " latOffset=" << latOffset
    3268              :                                   << " rm=" << rightmost
    3269              :                                   << " lm=" << leftmost
    3270              :                                   << " ahead=" << ahead.toString()
    3271              :                                   << "\n";
    3272              : #endif
    3273      1048456 :     for (int sublane = rightmost; sublane <= leftmost; ++sublane) {
    3274       620583 :         CLeaderDist predDist = ahead[sublane];
    3275       620583 :         const MSVehicle* pred = predDist.first;
    3276       620583 :         if (pred != nullptr && pred != this) {
    3277              : #ifdef DEBUG_PLAN_MOVE
    3278              :             if (DEBUG_COND) {
    3279              :                 std::cout << "     pred=" << pred->getID() << " predLane=" << pred->getLane()->getID() << " predPos=" << pred->getPositionOnLane() << " gap=" << predDist.second << "\n";
    3280              :             }
    3281              : #endif
    3282       390957 :             adaptToLeader(predDist, seen, lastLink, v, vLinkPass);
    3283              :         }
    3284              :     }
    3285       427873 : }
    3286              : 
    3287              : 
    3288              : void
    3289   1295193464 : MSVehicle::adaptToLeader(const std::pair<const MSVehicle*, double> leaderInfo,
    3290              :                          double seen,
    3291              :                          DriveProcessItem* const lastLink,
    3292              :                          double& v, double& vLinkPass) const {
    3293   1295193464 :     if (leaderInfo.first != 0) {
    3294   1295193464 :         if (ignoreFoe(leaderInfo.first)) {
    3295              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3296              :             if (DEBUG_COND) {
    3297              :                 std::cout << "  foe ignored\n";
    3298              :             }
    3299              : #endif
    3300              :             return;
    3301              :         }
    3302              :         const MSCFModel& cfModel = getCarFollowModel();
    3303              :         double vsafeLeader = 0;
    3304   1295192658 :         if (!MSGlobals::gSemiImplicitEulerUpdate) {
    3305              :             vsafeLeader = -std::numeric_limits<double>::max();
    3306              :         }
    3307              :         bool backOnRoute = true;
    3308   1295192658 :         if (leaderInfo.second < 0 && lastLink != nullptr && lastLink->myLink != nullptr) {
    3309              :             backOnRoute = false;
    3310              :             // this can either be
    3311              :             // a) a merging situation (leader back is is not our route) or
    3312              :             // b) a minGap violation / collision
    3313              :             MSLane* current = lastLink->myLink->getViaLaneOrLane();
    3314       282980 :             if (leaderInfo.first->getBackLane() == current) {
    3315              :                 backOnRoute = true;
    3316              :             } else {
    3317       644140 :                 for (MSLane* lane : getBestLanesContinuation()) {
    3318       565936 :                     if (lane == current) {
    3319              :                         break;
    3320              :                     }
    3321       425566 :                     if (leaderInfo.first->getBackLane() == lane) {
    3322              :                         backOnRoute = true;
    3323              :                     }
    3324              :                 }
    3325              :             }
    3326              : #ifdef DEBUG_PLAN_MOVE
    3327              :             if (DEBUG_COND) {
    3328              :                 std::cout << SIMTIME << " current=" << current->getID() << " leaderBackLane=" << leaderInfo.first->getBackLane()->getID() << " backOnRoute=" << backOnRoute << "\n";
    3329              :             }
    3330              : #endif
    3331       218574 :             if (!backOnRoute) {
    3332       118334 :                 double stopDist = seen - current->getLength() - POSITION_EPS;
    3333       118334 :                 if (lastLink->myLink->getInternalLaneBefore() != nullptr) {
    3334              :                     // do not drive onto the junction conflict area
    3335       101365 :                     stopDist -= lastLink->myLink->getInternalLaneBefore()->getLength();
    3336              :                 }
    3337       118334 :                 vsafeLeader = cfModel.stopSpeed(this, getSpeed(), stopDist);
    3338              :             }
    3339              :         }
    3340       182740 :         if (backOnRoute) {
    3341   1295074324 :             vsafeLeader = cfModel.followSpeed(this, getSpeed(), leaderInfo.second, leaderInfo.first->getSpeed(), leaderInfo.first->getCurrentApparentDecel(), leaderInfo.first);
    3342              :         }
    3343   1295192658 :         if (lastLink != nullptr) {
    3344    560590189 :             const double futureVSafe = cfModel.followSpeed(this, lastLink->accelV, leaderInfo.second, leaderInfo.first->getSpeed(), leaderInfo.first->getCurrentApparentDecel(), leaderInfo.first, MSCFModel::CalcReason::FUTURE);
    3345              :             lastLink->adaptLeaveSpeed(futureVSafe);
    3346              : #ifdef DEBUG_PLAN_MOVE
    3347              :             if (DEBUG_COND) {
    3348              :                 std::cout << "   vlinkpass=" << lastLink->myVLinkPass << " futureVSafe=" << futureVSafe << "\n";
    3349              :             }
    3350              : #endif
    3351              :         }
    3352   1295192658 :         v = MIN2(v, vsafeLeader);
    3353   2231468982 :         vLinkPass = MIN2(vLinkPass, vsafeLeader);
    3354              : #ifdef DEBUG_PLAN_MOVE
    3355              :         if (DEBUG_COND) std::cout
    3356              :                     << SIMTIME
    3357              :                     //std::cout << std::setprecision(10);
    3358              :                     << " veh=" << getID()
    3359              :                     << " lead=" << leaderInfo.first->getID()
    3360              :                     << " leadSpeed=" << leaderInfo.first->getSpeed()
    3361              :                     << " gap=" << leaderInfo.second
    3362              :                     << " leadLane=" << leaderInfo.first->getLane()->getID()
    3363              :                     << " predPos=" << leaderInfo.first->getPositionOnLane()
    3364              :                     << " myLane=" << myLane->getID()
    3365              :                     << " v=" << v
    3366              :                     << " vSafeLeader=" << vsafeLeader
    3367              :                     << " vLinkPass=" << vLinkPass
    3368              :                     << "\n";
    3369              : #endif
    3370              :     }
    3371              : }
    3372              : 
    3373              : 
    3374              : void
    3375     18766198 : MSVehicle::adaptToJunctionLeader(const std::pair<const MSVehicle*, double> leaderInfo,
    3376              :                                  const double seen, DriveProcessItem* const lastLink,
    3377              :                                  const MSLane* const lane, double& v, double& vLinkPass,
    3378              :                                  double distToCrossing) const {
    3379     18766198 :     if (leaderInfo.first != 0) {
    3380     18766198 :         if (ignoreFoe(leaderInfo.first)) {
    3381              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3382              :             if (DEBUG_COND) {
    3383              :                 std::cout << "  junction foe ignored\n";
    3384              :             }
    3385              : #endif
    3386              :             return;
    3387              :         }
    3388              :         const MSCFModel& cfModel = getCarFollowModel();
    3389              :         double vsafeLeader = 0;
    3390     18766187 :         if (!MSGlobals::gSemiImplicitEulerUpdate) {
    3391              :             vsafeLeader = -std::numeric_limits<double>::max();
    3392              :         }
    3393     18766187 :         if (leaderInfo.second >= 0) {
    3394     15578733 :             if (hasDeparted()) {
    3395     15573536 :                 vsafeLeader = cfModel.followSpeed(this, getSpeed(), leaderInfo.second, leaderInfo.first->getSpeed(), leaderInfo.first->getCurrentApparentDecel(), leaderInfo.first);
    3396              :             } else {
    3397              :                 // called in the context of MSLane::isInsertionSuccess
    3398         5197 :                 vsafeLeader = cfModel.insertionFollowSpeed(this, getSpeed(), leaderInfo.second, leaderInfo.first->getSpeed(), leaderInfo.first->getCurrentApparentDecel(), leaderInfo.first);
    3399              :             }
    3400      3187454 :         } else if (leaderInfo.first != this) {
    3401              :             // the leading, in-lapping vehicle is occupying the complete next lane
    3402              :             // stop before entering this lane
    3403      2765291 :             vsafeLeader = cfModel.stopSpeed(this, getSpeed(), seen - lane->getLength() - POSITION_EPS);
    3404              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3405              :             if (DEBUG_COND) {
    3406              :                 std::cout << SIMTIME << " veh=" << getID() << "  stopping before junction: lane=" << lane->getID() << " seen=" << seen
    3407              :                           << " laneLength=" << lane->getLength()
    3408              :                           << " stopDist=" << seen - lane->getLength()  - POSITION_EPS
    3409              :                           << " vsafeLeader=" << vsafeLeader
    3410              :                           << " distToCrossing=" << distToCrossing
    3411              :                           << "\n";
    3412              :             }
    3413              : #endif
    3414              :         }
    3415     18766187 :         if (distToCrossing >= 0) {
    3416              :             // can the leader still stop in the way?
    3417      6048749 :             const double vStop = cfModel.stopSpeed(this, getSpeed(), distToCrossing - getVehicleType().getMinGap());
    3418      6048749 :             if (leaderInfo.first == this) {
    3419              :                 // braking for pedestrian
    3420       410844 :                 const double vStopCrossing = cfModel.stopSpeed(this, getSpeed(), distToCrossing);
    3421              :                 vsafeLeader = vStopCrossing;
    3422              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3423              :                 if (DEBUG_COND) {
    3424              :                     std::cout << "  breaking for pedestrian distToCrossing=" << distToCrossing << " vStopCrossing=" << vStopCrossing << "\n";
    3425              :                 }
    3426              : #endif
    3427       410844 :                 if (lastLink != nullptr) {
    3428              :                     lastLink->adaptStopSpeed(vsafeLeader);
    3429              :                 }
    3430      5637905 :             } else if (leaderInfo.second == -std::numeric_limits<double>::max()) {
    3431              :                 // drive up to the crossing point and stop
    3432              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3433              :                 if (DEBUG_COND) {
    3434              :                     std::cout << "  stop at crossing point for critical leader vStop=" << vStop << "\n";
    3435              :                 };
    3436              : #endif
    3437              :                 vsafeLeader = MAX2(vsafeLeader, vStop);
    3438              :             } else {
    3439      5587982 :                 const double leaderDistToCrossing = distToCrossing - leaderInfo.second;
    3440              :                 // estimate the time at which the leader has gone past the crossing point
    3441      5587982 :                 const double leaderPastCPTime = leaderDistToCrossing / MAX2(leaderInfo.first->getSpeed(), SUMO_const_haltingSpeed);
    3442              :                 // reach distToCrossing after that time
    3443              :                 // avgSpeed * leaderPastCPTime = distToCrossing
    3444              :                 // ballistic: avgSpeed = (getSpeed + vFinal) / 2
    3445      5587982 :                 const double vFinal = MAX2(getSpeed(), 2 * (distToCrossing - getVehicleType().getMinGap()) / leaderPastCPTime - getSpeed());
    3446      5587982 :                 const double v2 = getSpeed() + ACCEL2SPEED((vFinal - getSpeed()) / leaderPastCPTime);
    3447              :                 vsafeLeader = MAX2(vsafeLeader, MIN2(v2, vStop));
    3448              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3449              :                 if (DEBUG_COND) {
    3450              :                     std::cout << "    driving up to the crossing point (distToCrossing=" << distToCrossing << ")"
    3451              :                               << " leaderPastCPTime=" << leaderPastCPTime
    3452              :                               << " vFinal=" << vFinal
    3453              :                               << " v2=" << v2
    3454              :                               << " vStop=" << vStop
    3455              :                               << " vsafeLeader=" << vsafeLeader << "\n";
    3456              :                 }
    3457              : #endif
    3458              :             }
    3459              :         }
    3460     18355343 :         if (lastLink != nullptr) {
    3461              :             lastLink->adaptLeaveSpeed(vsafeLeader);
    3462              :         }
    3463     18766187 :         v = MIN2(v, vsafeLeader);
    3464     35033594 :         vLinkPass = MIN2(vLinkPass, vsafeLeader);
    3465              : #ifdef DEBUG_PLAN_MOVE
    3466              :         if (DEBUG_COND) std::cout
    3467              :                     << SIMTIME
    3468              :                     //std::cout << std::setprecision(10);
    3469              :                     << " veh=" << getID()
    3470              :                     << " lead=" << leaderInfo.first->getID()
    3471              :                     << " leadSpeed=" << leaderInfo.first->getSpeed()
    3472              :                     << " gap=" << leaderInfo.second
    3473              :                     << " leadLane=" << leaderInfo.first->getLane()->getID()
    3474              :                     << " predPos=" << leaderInfo.first->getPositionOnLane()
    3475              :                     << " seen=" << seen
    3476              :                     << " lane=" << lane->getID()
    3477              :                     << " myLane=" << myLane->getID()
    3478              :                     << " dTC=" << distToCrossing
    3479              :                     << " v=" << v
    3480              :                     << " vSafeLeader=" << vsafeLeader
    3481              :                     << " vLinkPass=" << vLinkPass
    3482              :                     << "\n";
    3483              : #endif
    3484              :     }
    3485              : }
    3486              : 
    3487              : 
    3488              : void
    3489       555464 : MSVehicle::adaptToOncomingLeader(const std::pair<const MSVehicle*, double> leaderInfo,
    3490              :                                  DriveProcessItem* const lastLink,
    3491              :                                  double& v, double& vLinkPass) const {
    3492       555464 :     if (leaderInfo.first != 0) {
    3493       555464 :         if (ignoreFoe(leaderInfo.first)) {
    3494              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3495              :             if (DEBUG_COND) {
    3496              :                 std::cout << "  oncoming foe ignored\n";
    3497              :             }
    3498              : #endif
    3499              :             return;
    3500              :         }
    3501              :         const MSCFModel& cfModel = getCarFollowModel();
    3502              :         const MSVehicle* lead = leaderInfo.first;
    3503              :         const MSCFModel& cfModelL = lead->getCarFollowModel();
    3504              :         // assume the leader reacts symmetrically (neither stopping instantly nor ignoring ego)
    3505       555416 :         const double leaderBrakeGap = cfModelL.brakeGap(lead->getSpeed(), cfModelL.getMaxDecel(), 0);
    3506       555416 :         const double egoBrakeGap = cfModel.brakeGap(getSpeed(), cfModel.getMaxDecel(), 0);
    3507       555416 :         const double gapSum = leaderBrakeGap + egoBrakeGap;
    3508              :         // ensure that both vehicles can leave an intersection if they are currently on it
    3509       555416 :         double egoExit = getDistanceToLeaveJunction();
    3510       555416 :         const double leaderExit = lead->getDistanceToLeaveJunction();
    3511              :         double gap = leaderInfo.second;
    3512       555416 :         if (egoExit + leaderExit < gap) {
    3513       461028 :             gap -= egoExit + leaderExit;
    3514              :         } else {
    3515              :             egoExit = 0;
    3516              :         }
    3517              :         // split any distance in excess of brakeGaps evenly
    3518       555416 :         const double freeGap = MAX2(0.0, gap - gapSum);
    3519              :         const double splitGap = MIN2(gap, gapSum);
    3520              :         // assume remaining distance is allocated in proportion to braking distance
    3521       555416 :         const double gapRatio = gapSum > 0 ? egoBrakeGap / gapSum : 0.5;
    3522       555416 :         const double vsafeLeader = cfModel.stopSpeed(this, getSpeed(), splitGap * gapRatio + egoExit + 0.5 * freeGap);
    3523       555416 :         if (lastLink != nullptr) {
    3524        67533 :             const double futureVSafe = cfModel.stopSpeed(this, lastLink->accelV, leaderInfo.second, MSCFModel::CalcReason::FUTURE);
    3525              :             lastLink->adaptLeaveSpeed(futureVSafe);
    3526              : #ifdef DEBUG_PLAN_MOVE
    3527              :             if (DEBUG_COND) {
    3528              :                 std::cout << "   vlinkpass=" << lastLink->myVLinkPass << " futureVSafe=" << futureVSafe << "\n";
    3529              :             }
    3530              : #endif
    3531              :         }
    3532       555416 :         v = MIN2(v, vsafeLeader);
    3533      1097447 :         vLinkPass = MIN2(vLinkPass, vsafeLeader);
    3534              : #ifdef DEBUG_PLAN_MOVE
    3535              :         if (DEBUG_COND) std::cout
    3536              :                     << SIMTIME
    3537              :                     //std::cout << std::setprecision(10);
    3538              :                     << " veh=" << getID()
    3539              :                     << " oncomingLead=" << lead->getID()
    3540              :                     << " leadSpeed=" << lead->getSpeed()
    3541              :                     << " gap=" << leaderInfo.second
    3542              :                     << " gap2=" << gap
    3543              :                     << " gapRatio=" << gapRatio
    3544              :                     << " leadLane=" << lead->getLane()->getID()
    3545              :                     << " predPos=" << lead->getPositionOnLane()
    3546              :                     << " myLane=" << myLane->getID()
    3547              :                     << " v=" << v
    3548              :                     << " vSafeLeader=" << vsafeLeader
    3549              :                     << " vLinkPass=" << vLinkPass
    3550              :                     << "\n";
    3551              : #endif
    3552              :     }
    3553              : }
    3554              : 
    3555              : 
    3556              : void
    3557    803096753 : MSVehicle::checkLinkLeaderCurrentAndParallel(const MSLink* link, const MSLane* lane, double seen,
    3558              :         DriveProcessItem* const lastLink, double& v, double& vLinkPass, double& vLinkWait, bool& setRequest) const {
    3559    803096753 :     if (MSGlobals::gUsingInternalLanes && (myInfluencer == nullptr || myInfluencer->getRespectJunctionLeaderPriority())) {
    3560              :         // we want to pass the link but need to check for foes on internal lanes
    3561    802987599 :         checkLinkLeader(link, lane, seen, lastLink, v, vLinkPass, vLinkWait, setRequest);
    3562    802987599 :         if (myLaneChangeModel->getShadowLane() != nullptr) {
    3563      3164855 :             const MSLink* const parallelLink = link->getParallelLink(myLaneChangeModel->getShadowDirection());
    3564      3164855 :             if (parallelLink != nullptr) {
    3565      2190771 :                 checkLinkLeader(parallelLink, lane, seen, lastLink, v, vLinkPass, vLinkWait, setRequest, true);
    3566              :             }
    3567              :         }
    3568              :     }
    3569              : 
    3570    803096753 : }
    3571              : 
    3572              : void
    3573    805423871 : MSVehicle::checkLinkLeader(const MSLink* link, const MSLane* lane, double seen,
    3574              :                            DriveProcessItem* const lastLink, double& v, double& vLinkPass, double& vLinkWait, bool& setRequest,
    3575              :                            bool isShadowLink) const {
    3576              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3577              :     if (DEBUG_COND) {
    3578              :         gDebugFlag1 = true;    // See MSLink::getLeaderInfo
    3579              :     }
    3580              : #endif
    3581    805423871 :     const MSLink::LinkLeaders linkLeaders = link->getLeaderInfo(this, seen, nullptr, isShadowLink);
    3582              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3583              :     if (DEBUG_COND) {
    3584              :         gDebugFlag1 = false;    // See MSLink::getLeaderInfo
    3585              :     }
    3586              : #endif
    3587    825150656 :     for (MSLink::LinkLeaders::const_iterator it = linkLeaders.begin(); it != linkLeaders.end(); ++it) {
    3588              :         // the vehicle to enter the junction first has priority
    3589     19726785 :         const MSVehicle* leader = (*it).vehAndGap.first;
    3590     19726785 :         if (leader == nullptr) {
    3591              :             // leader is a pedestrian. Passing 'this' as a dummy.
    3592              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3593              :             if (DEBUG_COND) {
    3594              :                 std::cout << SIMTIME << " veh=" << getID() << " is blocked on link to " << link->getViaLaneOrLane()->getID() << " by pedestrian. dist=" << it->distToCrossing << "\n";
    3595              :             }
    3596              : #endif
    3597       422859 :             if (getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_IGNORE_JUNCTION_FOE_PROB, 0) > 0
    3598       422859 :                     && getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_IGNORE_JUNCTION_FOE_PROB, 0) >= RandHelper::rand(getRNG())) {
    3599              : #ifdef DEBUG_PLAN_MOVE
    3600              :                 if (DEBUG_COND) {
    3601              :                     std::cout << SIMTIME << " veh=" << getID() << " is ignoring pedestrian (jmIgnoreJunctionFoeProb)\n";
    3602              :                 }
    3603              : #endif
    3604          696 :                 continue;
    3605              :             }
    3606       422163 :             adaptToJunctionLeader(std::make_pair(this, -1), seen, lastLink, lane, v, vLinkPass, it->distToCrossing);
    3607              :             // if blocked by a pedestrian for too long we must yield our request
    3608       422163 :             if (v < SUMO_const_haltingSpeed && getWaitingTime() > TIME2STEPS(JUNCTION_BLOCKAGE_TIME)) {
    3609        75551 :                 setRequest = false;
    3610              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3611              :                 if (DEBUG_COND) {
    3612              :                     std::cout << "   aborting request\n";
    3613              :                 }
    3614              : #endif
    3615              :             }
    3616     19303926 :         } else if (isLeader(link, leader, (*it).vehAndGap.second) || (*it).inTheWay()) {
    3617     19256361 :             if (getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_IGNORE_JUNCTION_FOE_PROB, 0) > 0
    3618     19256361 :                     && getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_IGNORE_JUNCTION_FOE_PROB, 0) >= RandHelper::rand(getRNG())) {
    3619              : #ifdef DEBUG_PLAN_MOVE
    3620              :                 if (DEBUG_COND) {
    3621              :                     std::cout << SIMTIME << " veh=" << getID() << " is ignoring linkLeader=" << leader->getID() << " (jmIgnoreJunctionFoeProb)\n";
    3622              :                 }
    3623              : #endif
    3624         2181 :                 continue;
    3625              :             }
    3626     26443770 :             if (MSGlobals::gLateralResolution > 0 &&
    3627              :                     // sibling link (XXX: could also be partial occupator where this check fails)
    3628      7189590 :                     &leader->getLane()->getEdge() == &lane->getEdge()) {
    3629              :                 // check for sublane obstruction (trivial for sibling link leaders)
    3630              :                 const MSLane* conflictLane = link->getInternalLaneBefore();
    3631       941105 :                 MSLeaderInfo linkLeadersAhead = MSLeaderInfo(conflictLane->getWidth());
    3632       941105 :                 linkLeadersAhead.addLeader(leader, false, 0); // assume sibling lane has the same geometry as the leader lane
    3633       941105 :                 const double latOffset = isShadowLink ? (getLane()->getRightSideOnEdge() - myLaneChangeModel->getShadowLane()->getRightSideOnEdge()) : 0;
    3634              :                 // leader is neither on lane nor conflictLane (the conflict is only established geometrically)
    3635       941105 :                 adaptToLeaders(linkLeadersAhead, latOffset, seen, lastLink, leader->getLane(), v, vLinkPass);
    3636              : #ifdef DEBUG_PLAN_MOVE
    3637              :                 if (DEBUG_COND) {
    3638              :                     std::cout << SIMTIME << " veh=" << getID()
    3639              :                               << " siblingFoe link=" << link->getViaLaneOrLane()->getID()
    3640              :                               << " isShadowLink=" << isShadowLink
    3641              :                               << " lane=" << lane->getID()
    3642              :                               << " foe=" << leader->getID()
    3643              :                               << " foeLane=" << leader->getLane()->getID()
    3644              :                               << " latOffset=" << latOffset
    3645              :                               << " latOffsetFoe=" << leader->getLatOffset(lane)
    3646              :                               << " linkLeadersAhead=" << linkLeadersAhead.toString()
    3647              :                               << "\n";
    3648              :                 }
    3649              : #endif
    3650       941105 :             } else {
    3651              : #ifdef DEBUG_PLAN_MOVE
    3652              :                 if (DEBUG_COND) {
    3653              :                     std::cout << SIMTIME << " veh=" << getID() << " linkLeader=" << leader->getID() << " gap=" << it->vehAndGap.second
    3654              :                               << " ET=" << myJunctionEntryTime << " lET=" << leader->myJunctionEntryTime
    3655              :                               << " ETN=" << myJunctionEntryTimeNeverYield << " lETN=" << leader->myJunctionEntryTimeNeverYield
    3656              :                               << " CET=" << myJunctionConflictEntryTime << " lCET=" << leader->myJunctionConflictEntryTime
    3657              :                               << "\n";
    3658              :                 }
    3659              : #endif
    3660     18313075 :                 adaptToJunctionLeader(it->vehAndGap, seen, lastLink, lane, v, vLinkPass, it->distToCrossing);
    3661              :             }
    3662     19254180 :             if (lastLink != nullptr) {
    3663              :                 // we are not yet on the junction with this linkLeader.
    3664              :                 // at least we can drive up to the previous link and stop there
    3665     37067068 :                 v = MAX2(v, lastLink->myVLinkWait);
    3666              :             }
    3667              :             // if blocked by a leader from the same or next lane we must yield our request
    3668              :             // also, if blocked by a stopped or blocked leader
    3669     19254180 :             if (v < SUMO_const_haltingSpeed
    3670              :                     //&& leader->getSpeed() < SUMO_const_haltingSpeed
    3671     19254180 :                     && (leader->getLane()->getLogicalPredecessorLane() == myLane->getLogicalPredecessorLane()
    3672     10679225 :                         || leader->getLane()->getLogicalPredecessorLane() == myLane
    3673      8440515 :                         || leader->isStopped()
    3674      8362026 :                         || leader->getWaitingTime() > TIME2STEPS(JUNCTION_BLOCKAGE_TIME))) {
    3675      4459376 :                 setRequest = false;
    3676              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3677              :                 if (DEBUG_COND) {
    3678              :                     std::cout << "   aborting request\n";
    3679              :                 }
    3680              : #endif
    3681      4459376 :                 if (lastLink != nullptr && leader->getLane()->getLogicalPredecessorLane() == myLane) {
    3682              :                     // we are not yet on the junction so must abort that request as well
    3683              :                     // (or maybe we are already on the junction and the leader is a partial occupator beyond)
    3684      2226326 :                     lastLink->mySetRequest = false;
    3685              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3686              :                     if (DEBUG_COND) {
    3687              :                         std::cout << "      aborting previous request\n";
    3688              :                     }
    3689              : #endif
    3690              :                 }
    3691              :             }
    3692              :         }
    3693              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    3694              :         else {
    3695              :             if (DEBUG_COND) {
    3696              :                 std::cout << SIMTIME << " veh=" << getID() << " ignoring leader " << leader->getID() << " gap=" << (*it).vehAndGap.second << " dtC=" << (*it).distToCrossing
    3697              :                           << " ET=" << myJunctionEntryTime << " lET=" << leader->myJunctionEntryTime
    3698              :                           << " ETN=" << myJunctionEntryTimeNeverYield << " lETN=" << leader->myJunctionEntryTimeNeverYield
    3699              :                           << " CET=" << myJunctionConflictEntryTime << " lCET=" << leader->myJunctionConflictEntryTime
    3700              :                           << "\n";
    3701              :             }
    3702              :         }
    3703              : #endif
    3704              :     }
    3705              :     // if this is the link between two internal lanes we may have to slow down for pedestrians
    3706    805423871 :     vLinkWait = MIN2(vLinkWait, v);
    3707    805423871 : }
    3708              : 
    3709              : 
    3710              : double
    3711     99298163 : MSVehicle::getDeltaPos(const double accel) const {
    3712     99298163 :     double vNext = myState.mySpeed + ACCEL2SPEED(accel);
    3713     99298163 :     if (MSGlobals::gSemiImplicitEulerUpdate) {
    3714              :         // apply implicit Euler positional update
    3715            0 :         return SPEED2DIST(MAX2(vNext, 0.));
    3716              :     } else {
    3717              :         // apply ballistic update
    3718     99298163 :         if (vNext >= 0) {
    3719              :             // assume constant acceleration during this time step
    3720     98662328 :             return SPEED2DIST(myState.mySpeed + 0.5 * ACCEL2SPEED(accel));
    3721              :         } else {
    3722              :             // negative vNext indicates a stop within the middle of time step
    3723              :             // The corresponding stop time is s = mySpeed/deceleration \in [0,dt], and the
    3724              :             // covered distance is therefore deltaPos = mySpeed*s - 0.5*deceleration*s^2.
    3725              :             // Here, deceleration = (myState.mySpeed - vNext)/dt is the constant deceleration
    3726              :             // until the vehicle stops.
    3727       635835 :             return -SPEED2DIST(0.5 * myState.mySpeed * myState.mySpeed / ACCEL2SPEED(accel));
    3728              :         }
    3729              :     }
    3730              : }
    3731              : 
    3732              : void
    3733    632683106 : MSVehicle::processLinkApproaches(double& vSafe, double& vSafeMin, double& vSafeMinDist) {
    3734              : 
    3735              :     const MSCFModel& cfModel = getCarFollowModel();
    3736              :     // Speed limit due to zipper merging
    3737              :     double vSafeZipper = std::numeric_limits<double>::max();
    3738              : 
    3739    632683106 :     myHaveToWaitOnNextLink = false;
    3740              :     bool canBrakeVSafeMin = false;
    3741              : 
    3742              :     // Get safe velocities from DriveProcessItems.
    3743              :     assert(myLFLinkLanes.size() != 0 || isRemoteControlled());
    3744   1268927292 :     for (const DriveProcessItem& dpi : myLFLinkLanes) {
    3745   1095219898 :         MSLink* const link = dpi.myLink;
    3746              : 
    3747              : #ifdef DEBUG_EXEC_MOVE
    3748              :         if (DEBUG_COND) {
    3749              :             std::cout
    3750              :                     << SIMTIME
    3751              :                     << " veh=" << getID()
    3752              :                     << " link=" << (link == 0 ? "NULL" : link->getViaLaneOrLane()->getID())
    3753              :                     << " req=" << dpi.mySetRequest
    3754              :                     << " vP=" << dpi.myVLinkPass
    3755              :                     << " vW=" << dpi.myVLinkWait
    3756              :                     << " d=" << dpi.myDistance
    3757              :                     << "\n";
    3758              :             gDebugFlag1 = true; // See MSLink_DEBUG_OPENED
    3759              :         }
    3760              : #endif
    3761              : 
    3762              :         // the vehicle must change the lane on one of the next lanes (XXX: refs to code further below???, Leo)
    3763   1095219898 :         if (link != nullptr && dpi.mySetRequest) {
    3764              : 
    3765              :             const LinkState ls = link->getState();
    3766              :             // vehicles should brake when running onto a yellow light if the distance allows to halt in front
    3767              :             const bool yellow = link->haveYellow();
    3768    657467245 :             const bool canBrake = (dpi.myDistance > cfModel.brakeGap(myState.mySpeed, cfModel.getMaxDecel(), 0.)
    3769    657467245 :                                    || (MSGlobals::gSemiImplicitEulerUpdate && myState.mySpeed < ACCEL2SPEED(cfModel.getMaxDecel())));
    3770              :             assert(link->getLaneBefore() != nullptr);
    3771    657467245 :             const bool beyondStopLine = dpi.myDistance < link->getLaneBefore()->getVehicleStopOffset(this);
    3772    657467245 :             const bool ignoreRedLink = ignoreRed(link, canBrake) || beyondStopLine;
    3773    657467245 :             if (yellow && canBrake && !ignoreRedLink) {
    3774           10 :                 vSafe = dpi.myVLinkWait;
    3775           10 :                 myHaveToWaitOnNextLink = true;
    3776              : #ifdef DEBUG_CHECKREWINDLINKLANES
    3777              :                 if (DEBUG_COND) {
    3778              :                     std::cout << SIMTIME << " veh=" << getID() << " haveToWait (yellow)\n";
    3779              :                 }
    3780              : #endif
    3781     21223059 :                 break;
    3782              :             }
    3783    657467235 :             const bool influencerPrio = (myInfluencer != nullptr && !myInfluencer->getRespectJunctionPriority());
    3784              :             MSLink::BlockingFoes collectFoes;
    3785    657467235 :             bool opened = (yellow || influencerPrio
    3786   1972076809 :                            || link->opened(dpi.myArrivalTime, dpi.myArrivalSpeed, dpi.getLeaveSpeed(),
    3787    657304787 :                                            getVehicleType().getLength(),
    3788    629944111 :                                            canBrake ? getImpatience() : 1,
    3789              :                                            cfModel.getMaxDecel(),
    3790    657304787 :                                            getWaitingTimeFor(link), getLateralPositionOnLane(),
    3791              :                                            ls == LINKSTATE_ZIPPER ? &collectFoes : nullptr,
    3792    657304787 :                                            ignoreRedLink, this, dpi.myDistance));
    3793    651798109 :             if (opened && myLaneChangeModel->getShadowLane() != nullptr) {
    3794      1966049 :                 const MSLink* const parallelLink = dpi.myLink->getParallelLink(myLaneChangeModel->getShadowDirection());
    3795      1966049 :                 if (parallelLink != nullptr) {
    3796      1221043 :                     const double shadowLatPos = getLateralPositionOnLane() - myLaneChangeModel->getShadowDirection() * 0.5 * (
    3797      1221043 :                                                     myLane->getWidth() + myLaneChangeModel->getShadowLane()->getWidth());
    3798      2441342 :                     opened = yellow || influencerPrio || (opened && parallelLink->opened(dpi.myArrivalTime, dpi.myArrivalSpeed, dpi.getLeaveSpeed(),
    3799      1220299 :                                                           getVehicleType().getLength(), getImpatience(),
    3800              :                                                           cfModel.getMaxDecel(),
    3801              :                                                           getWaitingTimeFor(link), shadowLatPos, nullptr,
    3802      1220299 :                                                           ignoreRedLink, this, dpi.myDistance));
    3803              : #ifdef DEBUG_EXEC_MOVE
    3804              :                     if (DEBUG_COND) {
    3805              :                         std::cout << SIMTIME
    3806              :                                   << " veh=" << getID()
    3807              :                                   << " shadowLane=" << myLaneChangeModel->getShadowLane()->getID()
    3808              :                                   << " shadowDir=" << myLaneChangeModel->getShadowDirection()
    3809              :                                   << " parallelLink=" << (parallelLink == 0 ? "NULL" : parallelLink->getViaLaneOrLane()->getID())
    3810              :                                   << " opened=" << opened
    3811              :                                   << "\n";
    3812              :                     }
    3813              : #endif
    3814              :                 }
    3815              :             }
    3816              :             // vehicles should decelerate when approaching a minor link
    3817              : #ifdef DEBUG_EXEC_MOVE
    3818              :             if (DEBUG_COND) {
    3819              :                 std::cout << SIMTIME
    3820              :                           << "   opened=" << opened
    3821              :                           << " influencerPrio=" << influencerPrio
    3822              :                           << " linkPrio=" << link->havePriority()
    3823              :                           << " lastContMajor=" << link->lastWasContMajor()
    3824              :                           << " isCont=" << link->isCont()
    3825              :                           << " ignoreRed=" << ignoreRedLink
    3826              :                           << "\n";
    3827              :             }
    3828              : #endif
    3829              :             double visibilityDistance = link->getFoeVisibilityDistance();
    3830    657467235 :             bool determinedFoePresence = dpi.myDistance <= visibilityDistance;
    3831    657467235 :             if (opened && !influencerPrio && !link->havePriority() && !link->lastWasContMajor() && !link->isCont() && !ignoreRedLink) {
    3832     17852359 :                 if (!determinedFoePresence && (canBrake || !yellow)) {
    3833     16859985 :                     vSafe = dpi.myVLinkWait;
    3834     16859985 :                     myHaveToWaitOnNextLink = true;
    3835              : #ifdef DEBUG_CHECKREWINDLINKLANES
    3836              :                     if (DEBUG_COND) {
    3837              :                         std::cout << SIMTIME << " veh=" << getID() << " haveToWait (minor)\n";
    3838              :                     }
    3839              : #endif
    3840     16859985 :                     break;
    3841              :                 } else {
    3842              :                     // past the point of no return. we need to drive fast enough
    3843              :                     // to make it across the link. However, minor slowdowns
    3844              :                     // should be permissible to follow leading traffic safely
    3845              :                     // basically, this code prevents dawdling
    3846              :                     // (it's harder to do this later using
    3847              :                     // SUMO_ATTR_JM_SIGMA_MINOR because we don't know whether the
    3848              :                     // vehicle is already too close to stop at that part of the code)
    3849              :                     //
    3850              :                     // XXX: There is a problem in subsecond simulation: If we cannot
    3851              :                     // make it across the minor link in one step, new traffic
    3852              :                     // could appear on a major foe link and cause a collision. Refs. #1845, #2123
    3853       992374 :                     vSafeMinDist = dpi.myDistance; // distance that must be covered
    3854       992374 :                     if (MSGlobals::gSemiImplicitEulerUpdate) {
    3855      1811856 :                         vSafeMin = MIN3((double)DIST2SPEED(vSafeMinDist + POSITION_EPS), dpi.myVLinkPass, cfModel.maxNextSafeMin(getSpeed(), this));
    3856              :                     } else {
    3857       172892 :                         vSafeMin = MIN3((double)DIST2SPEED(2 * vSafeMinDist + NUMERICAL_EPS) - getSpeed(), dpi.myVLinkPass, cfModel.maxNextSafeMin(getSpeed(), this));
    3858              :                     }
    3859              :                     canBrakeVSafeMin = canBrake;
    3860              : #ifdef DEBUG_EXEC_MOVE
    3861              :                     if (DEBUG_COND) {
    3862              :                         std::cout << "     vSafeMin=" << vSafeMin << " vSafeMinDist=" << vSafeMinDist << " canBrake=" << canBrake << "\n";
    3863              :                     }
    3864              : #endif
    3865              :                 }
    3866              :             }
    3867              :             // have waited; may pass if opened...
    3868    640607250 :             if (opened) {
    3869    634916754 :                 vSafe = dpi.myVLinkPass;
    3870    634916754 :                 if (vSafe < cfModel.getMaxDecel() && vSafe <= dpi.myVLinkWait && vSafe < cfModel.maxNextSpeed(getSpeed(), this)) {
    3871              :                     // this vehicle is probably not gonna drive across the next junction (heuristic)
    3872     55169797 :                     myHaveToWaitOnNextLink = true;
    3873              : #ifdef DEBUG_CHECKREWINDLINKLANES
    3874              :                     if (DEBUG_COND) {
    3875              :                         std::cout << SIMTIME << " veh=" << getID() << " haveToWait (very slow)\n";
    3876              :                     }
    3877              : #endif
    3878              :                 }
    3879    634916754 :                 if (link->mustStop() && determinedFoePresence && myHaveStoppedFor == nullptr) {
    3880        20820 :                     myHaveStoppedFor = link;
    3881              :                 }
    3882      5690496 :             } else if (link->getState() == LINKSTATE_ZIPPER) {
    3883      1327198 :                 vSafeZipper = MIN2(vSafeZipper,
    3884      1327198 :                                    link->getZipperSpeed(this, dpi.myDistance, dpi.myVLinkPass, dpi.myArrivalTime, &collectFoes));
    3885              :             } else if (!canBrake
    3886              :                        // always brake hard for traffic lights (since an emergency stop is necessary anyway)
    3887         1854 :                        && link->getTLLogic() == nullptr
    3888              :                        // cannot brake even with emergency deceleration
    3889      4364185 :                        && dpi.myDistance < cfModel.brakeGap(myState.mySpeed, cfModel.getEmergencyDecel(), 0.)) {
    3890              : #ifdef DEBUG_EXEC_MOVE
    3891              :                 if (DEBUG_COND) {
    3892              :                     std::cout << SIMTIME << " too fast to brake for closed link\n";
    3893              :                 }
    3894              : #endif
    3895          234 :                 vSafe = dpi.myVLinkPass;
    3896              :             } else {
    3897      4363064 :                 vSafe = dpi.myVLinkWait;
    3898      4363064 :                 myHaveToWaitOnNextLink = true;
    3899              : #ifdef DEBUG_CHECKREWINDLINKLANES
    3900              :                 if (DEBUG_COND) {
    3901              :                     std::cout << SIMTIME << " veh=" << getID() << " haveToWait (closed)\n";
    3902              :                 }
    3903              : #endif
    3904              : #ifdef DEBUG_EXEC_MOVE
    3905              :                 if (DEBUG_COND) {
    3906              :                     std::cout << SIMTIME << " braking for closed link=" << link->getViaLaneOrLane()->getID() << "\n";
    3907              :                 }
    3908              : #endif
    3909      4363064 :                 break;
    3910              :             }
    3911    636244186 :             if (myLane->isInternal() && myJunctionEntryTime == SUMOTime_MAX) {
    3912              :                 // request was renewed, restoring entry time
    3913              :                 // @note: using myJunctionEntryTimeNeverYield could lead to inconsistencies with other vehicles already on the junction
    3914        85211 :                 myJunctionEntryTime = SIMSTEP;;
    3915              :             }
    3916    657467235 :         } else {
    3917    437752653 :             if (link != nullptr && link->getInternalLaneBefore() != nullptr && myLane->isInternal() && link->getJunction() == myLane->getEdge().getToJunction()) {
    3918              :                 // blocked on the junction. yield request so other vehicles may
    3919              :                 // become junction leader
    3920              : #ifdef DEBUG_EXEC_MOVE
    3921              :                 if (DEBUG_COND) {
    3922              :                     std::cout << SIMTIME << " resetting junctionEntryTime at junction '" << link->getJunction()->getID() << "' beause of non-request exitLink\n";
    3923              :                 }
    3924              : #endif
    3925       252052 :                 myJunctionEntryTime = SUMOTime_MAX;
    3926       252052 :                 myJunctionConflictEntryTime = SUMOTime_MAX;
    3927              :             }
    3928              :             // we have: i->link == 0 || !i->setRequest
    3929    437752653 :             vSafe = dpi.myVLinkWait;
    3930    437752653 :             if (link != nullptr || myStopDist < (myLane->getLength() - getPositionOnLane())) {
    3931    107706703 :                 if (vSafe < getSpeed()) {
    3932     16225602 :                     myHaveToWaitOnNextLink = true;
    3933              : #ifdef DEBUG_CHECKREWINDLINKLANES
    3934              :                     if (DEBUG_COND) {
    3935              :                         std::cout << SIMTIME << " veh=" << getID() << " haveToWait (no request, braking) vSafe=" << vSafe << "\n";
    3936              :                     }
    3937              : #endif
    3938     91481101 :                 } else if (vSafe < SUMO_const_haltingSpeed) {
    3939     64070240 :                     myHaveToWaitOnNextLink = true;
    3940              : #ifdef DEBUG_CHECKREWINDLINKLANES
    3941              :                     if (DEBUG_COND) {
    3942              :                         std::cout << SIMTIME << " veh=" << getID() << " haveToWait (no request, stopping)\n";
    3943              :                     }
    3944              : #endif
    3945              :                 }
    3946              :             }
    3947    334580063 :             if (link == nullptr && myLFLinkLanes.size() == 1
    3948    265078770 :                     && getBestLanesContinuation().size() > 1
    3949      1134437 :                     && getBestLanesContinuation()[1]->hadPermissionChanges()
    3950    437889700 :                     && myLane->getFirstAnyVehicle() == this) {
    3951              :                 // temporal lane closing without notification, visible to the
    3952              :                 // vehicle at the front of the queue
    3953        35330 :                 updateBestLanes(true);
    3954              :                 //std::cout << SIMTIME << " veh=" << getID() << " updated bestLanes=" << toString(getBestLanesContinuation()) << "\n";
    3955              :             }
    3956              :             break;
    3957              :         }
    3958              :     }
    3959              : 
    3960              : //#ifdef DEBUG_EXEC_MOVE
    3961              : //    if (DEBUG_COND) {
    3962              : //        std::cout << "\nvCurrent = " << toString(getSpeed(), 24) << "" << std::endl;
    3963              : //        std::cout << "vSafe = " << toString(vSafe, 24) << "" << std::endl;
    3964              : //        std::cout << "vSafeMin = " << toString(vSafeMin, 24) << "" << std::endl;
    3965              : //        std::cout << "vSafeMinDist = " << toString(vSafeMinDist, 24) << "" << std::endl;
    3966              : //
    3967              : //        double gap = getLeader().second;
    3968              : //        std::cout << "gap = " << toString(gap, 24) << std::endl;
    3969              : //        std::cout << "vSafeStoppedLeader = " << toString(getCarFollowModel().stopSpeed(this, getSpeed(), gap, MSCFModel::CalcReason::FUTURE), 24)
    3970              : //                << "\n" << std::endl;
    3971              : //    }
    3972              : //#endif
    3973              : 
    3974    632683106 :     if ((MSGlobals::gSemiImplicitEulerUpdate && vSafe + NUMERICAL_EPS < vSafeMin)
    3975    632457480 :             || (!MSGlobals::gSemiImplicitEulerUpdate && (vSafe + NUMERICAL_EPS < vSafeMin && vSafeMin != 0))) { // this might be good for the euler case as well
    3976              :         // XXX: (Leo) This often called stopSpeed with vSafeMinDist==0 (for the ballistic update), since vSafe can become negative
    3977              :         //      For the Euler update the term '+ NUMERICAL_EPS' prevented a call here... Recheck, consider of -INVALID_SPEED instead of 0 to indicate absence of vSafeMin restrictions. Refs. #2577
    3978              : #ifdef DEBUG_EXEC_MOVE
    3979              :         if (DEBUG_COND) {
    3980              :             std::cout << "vSafeMin Problem? vSafe=" << vSafe << " vSafeMin=" << vSafeMin << " vSafeMinDist=" << vSafeMinDist << std::endl;
    3981              :         }
    3982              : #endif
    3983       266826 :         if (canBrakeVSafeMin && vSafe < getSpeed()) {
    3984              :             // cannot drive across a link so we need to stop before it
    3985       126922 :             vSafe = MIN2(vSafe, MAX2(getCarFollowModel().minNextSpeed(getSpeed(), this),
    3986        63461 :                                      getCarFollowModel().stopSpeed(this, getSpeed(), vSafeMinDist)));
    3987        63461 :             vSafeMin = 0;
    3988        63461 :             myHaveToWaitOnNextLink = true;
    3989              : #ifdef DEBUG_CHECKREWINDLINKLANES
    3990              :             if (DEBUG_COND) {
    3991              :                 std::cout << SIMTIME << " veh=" << getID() << " haveToWait (vSafe=" << vSafe << " < vSafeMin=" << vSafeMin << ")\n";
    3992              :             }
    3993              : #endif
    3994              :         } else {
    3995              :             // if the link is yellow or visibility distance is large
    3996              :             // then we might not make it across the link in one step anyway..
    3997              :             // Possibly, the lane after the intersection has a lower speed limit so
    3998              :             // we really need to drive slower already
    3999              :             // -> keep driving without dawdling
    4000       203365 :             vSafeMin = vSafe;
    4001              :         }
    4002              :     }
    4003              : 
    4004              :     // vehicles inside a roundabout should maintain their requests
    4005    632683106 :     if (myLane->getEdge().isRoundabout()) {
    4006      2669296 :         myHaveToWaitOnNextLink = false;
    4007              :     }
    4008              : 
    4009    632683106 :     vSafe = MIN2(vSafe, vSafeZipper);
    4010    632683106 : }
    4011              : 
    4012              : 
    4013              : double
    4014    700430980 : MSVehicle::processTraCISpeedControl(double vSafe, double vNext) {
    4015    700430980 :     if (myInfluencer != nullptr) {
    4016       494106 :         myInfluencer->setOriginalSpeed(vNext);
    4017              : #ifdef DEBUG_TRACI
    4018              :         if DEBUG_COND2(this) {
    4019              :             std::cout << SIMTIME << " MSVehicle::processTraCISpeedControl() for vehicle '" << getID() << "'"
    4020              :                       << " vSafe=" << vSafe << " (init)vNext=" << vNext << " keepStopping=" << keepStopping();
    4021              :         }
    4022              : #endif
    4023       494106 :         if (myInfluencer->isRemoteControlled()) {
    4024         7308 :             vNext = myInfluencer->implicitSpeedRemote(this, myState.mySpeed);
    4025              :         }
    4026       494106 :         const double vMax = getVehicleType().getCarFollowModel().maxNextSpeed(myState.mySpeed, this);
    4027       494106 :         double vMin = getVehicleType().getCarFollowModel().minNextSpeed(myState.mySpeed, this);
    4028       494106 :         if (MSGlobals::gSemiImplicitEulerUpdate) {
    4029              :             vMin = MAX2(0., vMin);
    4030              :         }
    4031       494106 :         vNext = myInfluencer->influenceSpeed(MSNet::getInstance()->getCurrentTimeStep(), vNext, vSafe, vMin, vMax);
    4032       494106 :         if (keepStopping() && myStops.front().getSpeed() == 0) {
    4033              :             // avoid driving while stopped (unless it's actually a waypoint
    4034         3818 :             vNext = myInfluencer->getOriginalSpeed();
    4035              :         }
    4036              : #ifdef DEBUG_TRACI
    4037              :         if DEBUG_COND2(this) {
    4038              :             std::cout << " (processed)vNext=" << vNext << std::endl;
    4039              :         }
    4040              : #endif
    4041              :     }
    4042    700430980 :     return vNext;
    4043              : }
    4044              : 
    4045              : 
    4046              : void
    4047     71561936 : MSVehicle::removePassedDriveItems() {
    4048              : #ifdef DEBUG_ACTIONSTEPS
    4049              :     if (DEBUG_COND) {
    4050              :         std::cout << SIMTIME << " veh=" << getID() << " removePassedDriveItems()\n"
    4051              :                   << "    Current items: ";
    4052              :         for (auto& j : myLFLinkLanes) {
    4053              :             if (j.myLink == 0) {
    4054              :                 std::cout << "\n    Stop at distance " << j.myDistance;
    4055              :             } else {
    4056              :                 const MSLane* to = j.myLink->getViaLaneOrLane();
    4057              :                 const MSLane* from = j.myLink->getLaneBefore();
    4058              :                 std::cout << "\n    Link at distance " << j.myDistance << ": '"
    4059              :                           << (from == 0 ? "NONE" : from->getID()) << "' -> '" << (to == 0 ? "NONE" : to->getID()) << "'";
    4060              :             }
    4061              :         }
    4062              :         std::cout << "\n    myNextDriveItem: ";
    4063              :         if (myLFLinkLanes.size() != 0) {
    4064              :             if (myNextDriveItem->myLink == 0) {
    4065              :                 std::cout << "\n    Stop at distance " << myNextDriveItem->myDistance;
    4066              :             } else {
    4067              :                 const MSLane* to = myNextDriveItem->myLink->getViaLaneOrLane();
    4068              :                 const MSLane* from = myNextDriveItem->myLink->getLaneBefore();
    4069              :                 std::cout << "\n    Link at distance " << myNextDriveItem->myDistance << ": '"
    4070              :                           << (from == 0 ? "NONE" : from->getID()) << "' -> '" << (to == 0 ? "NONE" : to->getID()) << "'";
    4071              :             }
    4072              :         }
    4073              :         std::cout << std::endl;
    4074              :     }
    4075              : #endif
    4076     71886683 :     for (auto j = myLFLinkLanes.begin(); j != myNextDriveItem; ++j) {
    4077              : #ifdef DEBUG_ACTIONSTEPS
    4078              :         if (DEBUG_COND) {
    4079              :             std::cout << "    Removing item: ";
    4080              :             if (j->myLink == 0) {
    4081              :                 std::cout << "Stop at distance " << j->myDistance;
    4082              :             } else {
    4083              :                 const MSLane* to = j->myLink->getViaLaneOrLane();
    4084              :                 const MSLane* from = j->myLink->getLaneBefore();
    4085              :                 std::cout << "Link at distance " << j->myDistance << ": '"
    4086              :                           << (from == 0 ? "NONE" : from->getID()) << "' -> '" << (to == 0 ? "NONE" : to->getID()) << "'";
    4087              :             }
    4088              :             std::cout << std::endl;
    4089              :         }
    4090              : #endif
    4091       324747 :         if (j->myLink != nullptr) {
    4092       324674 :             j->myLink->removeApproaching(this);
    4093              :         }
    4094              :     }
    4095     71561936 :     myLFLinkLanes.erase(myLFLinkLanes.begin(), myNextDriveItem);
    4096     71561936 :     myNextDriveItem = myLFLinkLanes.begin();
    4097     71561936 : }
    4098              : 
    4099              : 
    4100              : void
    4101      1136082 : MSVehicle::updateDriveItems() {
    4102              : #ifdef DEBUG_ACTIONSTEPS
    4103              :     if (DEBUG_COND) {
    4104              :         std::cout << SIMTIME << " updateDriveItems(), veh='" << getID() << "' (lane: '" << getLane()->getID() << "')\nCurrent drive items:" << std::endl;
    4105              :         for (const auto& dpi : myLFLinkLanes) {
    4106              :             std::cout
    4107              :                     << " vPass=" << dpi.myVLinkPass
    4108              :                     << " vWait=" << dpi.myVLinkWait
    4109              :                     << " linkLane=" << (dpi.myLink == 0 ? "NULL" : dpi.myLink->getViaLaneOrLane()->getID())
    4110              :                     << " request=" << dpi.mySetRequest
    4111              :                     << "\n";
    4112              :         }
    4113              :         std::cout << " myNextDriveItem's linked lane: " << (myNextDriveItem->myLink == 0 ? "NULL" : myNextDriveItem->myLink->getViaLaneOrLane()->getID()) << std::endl;
    4114              :     }
    4115              : #endif
    4116      1136082 :     if (myLFLinkLanes.size() == 0) {
    4117              :         // nothing to update
    4118              :         return;
    4119              :     }
    4120              :     const MSLink* nextPlannedLink = nullptr;
    4121              : //    auto i = myLFLinkLanes.begin();
    4122      1136080 :     auto i = myNextDriveItem;
    4123      2272115 :     while (i != myLFLinkLanes.end() && nextPlannedLink == nullptr) {
    4124      1136035 :         nextPlannedLink = i->myLink;
    4125              :         ++i;
    4126              :     }
    4127              : 
    4128      1136080 :     if (nextPlannedLink == nullptr) {
    4129              :         // No link for upcoming item -> no need for an update
    4130              : #ifdef DEBUG_ACTIONSTEPS
    4131              :         if (DEBUG_COND) {
    4132              :             std::cout << "Found no link-related drive item." << std::endl;
    4133              :         }
    4134              : #endif
    4135              :         return;
    4136              :     }
    4137              : 
    4138       556993 :     if (getLane() == nextPlannedLink->getLaneBefore()) {
    4139              :         // Current lane approaches the stored next link, i.e. no LC happend and no update is required.
    4140              : #ifdef DEBUG_ACTIONSTEPS
    4141              :         if (DEBUG_COND) {
    4142              :             std::cout << "Continuing on planned lane sequence, no update required." << std::endl;
    4143              :         }
    4144              : #endif
    4145              :         return;
    4146              :     }
    4147              :     // Lane must have been changed, determine the change direction
    4148       546988 :     const MSLink* parallelLink = nextPlannedLink->getParallelLink(1);
    4149       546988 :     if (parallelLink != nullptr && parallelLink->getLaneBefore() == getLane()) {
    4150              :         // lcDir = 1;
    4151              :     } else {
    4152       265090 :         parallelLink = nextPlannedLink->getParallelLink(-1);
    4153       265090 :         if (parallelLink != nullptr && parallelLink->getLaneBefore() == getLane()) {
    4154              :             // lcDir = -1;
    4155              :         } else {
    4156              :             // If the vehicle's current lane is not the approaching lane for the next
    4157              :             // drive process item's link, it is expected to lead to a parallel link,
    4158              :             // XXX: What if the lc was an overtaking maneuver and there is no upcoming link?
    4159              :             //      Then a stop item should be scheduled! -> TODO!
    4160              :             //assert(false);
    4161        72842 :             return;
    4162              :         }
    4163              :     }
    4164              : #ifdef DEBUG_ACTIONSTEPS
    4165              :     if (DEBUG_COND) {
    4166              :         std::cout << "Changed lane. Drive items will be updated along the current lane continuation." << std::endl;
    4167              :     }
    4168              : #endif
    4169              :     // Trace link sequence along current best lanes and transfer drive items to the corresponding links
    4170              : //        DriveItemVector::iterator driveItemIt = myLFLinkLanes.begin();
    4171       474146 :     DriveItemVector::iterator driveItemIt = myNextDriveItem;
    4172              :     // In the loop below, lane holds the currently considered lane on the vehicles continuation (including internal lanes)
    4173       474146 :     const MSLane* lane = myLane;
    4174              :     assert(myLane == parallelLink->getLaneBefore());
    4175              :     // *lit is a pointer to the next lane in best continuations for the current lane (always non-internal)
    4176       474146 :     std::vector<MSLane*>::const_iterator bestLaneIt = getBestLanesContinuation().begin() + 1;
    4177              :     // Pointer to the new link for the current drive process item
    4178              :     MSLink* newLink = nullptr;
    4179      1771245 :     while (driveItemIt != myLFLinkLanes.end()) {
    4180      1325838 :         if (driveItemIt->myLink == nullptr) {
    4181              :             // Items not related to a specific link are not updated
    4182              :             // (XXX: when a stop item corresponded to a dead end, which is overcome by the LC that made
    4183              :             //       the update necessary, this may slow down the vehicle's continuation on the new lane...)
    4184              :             ++driveItemIt;
    4185       171458 :             continue;
    4186              :         }
    4187              :         // Continuation links for current best lanes are less than for the former drive items (myLFLinkLanes)
    4188              :         // We just remove the leftover link-items, as they cannot be mapped to new links.
    4189      1154380 :         if (bestLaneIt == getBestLanesContinuation().end()) {
    4190              : #ifdef DEBUG_ACTIONSTEPS
    4191              :             if (DEBUG_COND) {
    4192              :                 std::cout << "Reached end of the new continuation sequence. Erasing leftover link-items." << std::endl;
    4193              :             }
    4194              : #endif
    4195        91322 :             while (driveItemIt != myLFLinkLanes.end()) {
    4196        62583 :                 if (driveItemIt->myLink == nullptr) {
    4197              :                     ++driveItemIt;
    4198        14345 :                     continue;
    4199              :                 } else {
    4200        48238 :                     driveItemIt->myLink->removeApproaching(this);
    4201              :                     driveItemIt = myLFLinkLanes.erase(driveItemIt);
    4202              :                 }
    4203              :             }
    4204              :             break;
    4205              :         }
    4206              :         // Do the actual link-remapping for the item. And un/register approaching information on the corresponding links
    4207      1125641 :         const MSLane* const target = *bestLaneIt;
    4208              :         assert(!target->isInternal());
    4209              :         newLink = nullptr;
    4210      1241082 :         for (MSLink* const link : lane->getLinkCont()) {
    4211      1241082 :             if (link->getLane() == target) {
    4212              :                 newLink = link;
    4213              :                 break;
    4214              :             }
    4215              :         }
    4216              : 
    4217      1125641 :         if (newLink == driveItemIt->myLink) {
    4218              :             // new continuation merged into previous - stop update
    4219              : #ifdef DEBUG_ACTIONSTEPS
    4220              :             if (DEBUG_COND) {
    4221              :                 std::cout << "Old and new continuation sequences merge at link\n"
    4222              :                           << "'" << newLink->getLaneBefore()->getID() << "'->'" << newLink->getViaLaneOrLane()->getID() << "'"
    4223              :                           << "\nNo update beyond merge required." << std::endl;
    4224              :             }
    4225              : #endif
    4226              :             break;
    4227              :         }
    4228              : 
    4229              : #ifdef DEBUG_ACTIONSTEPS
    4230              :         if (DEBUG_COND) {
    4231              :             std::cout << "lane=" << lane->getID() << "\nUpdating link\n    '" << driveItemIt->myLink->getLaneBefore()->getID() << "'->'" << driveItemIt->myLink->getViaLaneOrLane()->getID() << "'"
    4232              :                       << "==> " << "'" << newLink->getLaneBefore()->getID() << "'->'" << newLink->getViaLaneOrLane()->getID() << "'" << std::endl;
    4233              :         }
    4234              : #endif
    4235      1125641 :         newLink->setApproaching(this, driveItemIt->myLink->getApproaching(this));
    4236      1125641 :         driveItemIt->myLink->removeApproaching(this);
    4237      1125641 :         driveItemIt->myLink = newLink;
    4238              :         lane = newLink->getViaLaneOrLane();
    4239              :         ++driveItemIt;
    4240      1125641 :         if (!lane->isInternal()) {
    4241              :             ++bestLaneIt;
    4242              :         }
    4243              :     }
    4244              : #ifdef DEBUG_ACTIONSTEPS
    4245              :     if (DEBUG_COND) {
    4246              :         std::cout << "Updated drive items:" << std::endl;
    4247              :         for (const auto& dpi : myLFLinkLanes) {
    4248              :             std::cout
    4249              :                     << " vPass=" << dpi.myVLinkPass
    4250              :                     << " vWait=" << dpi.myVLinkWait
    4251              :                     << " linkLane=" << (dpi.myLink == 0 ? "NULL" : dpi.myLink->getViaLaneOrLane()->getID())
    4252              :                     << " request=" << dpi.mySetRequest
    4253              :                     << "\n";
    4254              :         }
    4255              :     }
    4256              : #endif
    4257              : }
    4258              : 
    4259              : 
    4260              : void
    4261    700430980 : MSVehicle::setBrakingSignals(double vNext) {
    4262              :     // To avoid casual blinking brake lights at high speeds due to dawdling of the
    4263              :     // leading vehicle, we don't show brake lights when the deceleration could be caused
    4264              :     // by frictional forces and air resistance (i.e. proportional to v^2, coefficient could be adapted further)
    4265    700430980 :     double pseudoFriction = (0.05 +  0.005 * getSpeed()) * getSpeed();
    4266    700430980 :     bool brakelightsOn = vNext < getSpeed() - ACCEL2SPEED(pseudoFriction);
    4267              : 
    4268    700430980 :     if (vNext <= SUMO_const_haltingSpeed) {
    4269              :         brakelightsOn = true;
    4270              :     }
    4271    700430980 :     if (brakelightsOn && !isStopped()) {
    4272              :         switchOnSignal(VEH_SIGNAL_BRAKELIGHT);
    4273              :     } else {
    4274              :         switchOffSignal(VEH_SIGNAL_BRAKELIGHT);
    4275              :     }
    4276    700430980 : }
    4277              : 
    4278              : 
    4279              : void
    4280    700505992 : MSVehicle::updateWaitingTime(double vNext) {
    4281    700505992 :     if (vNext <= SUMO_const_haltingSpeed && (!isStopped() || isIdling()) && myAcceleration <= accelThresholdForWaiting())  {
    4282     91831487 :         myWaitingTime += DELTA_T;
    4283     91831487 :         myWaitingTimeCollector.passTime(DELTA_T, true);
    4284              :     } else {
    4285    608674505 :         myWaitingTime = 0;
    4286    608674505 :         myWaitingTimeCollector.passTime(DELTA_T, false);
    4287    608674505 :         if (hasInfluencer()) {
    4288       269831 :             getInfluencer().setExtraImpatience(0);
    4289              :         }
    4290              :     }
    4291    700505992 : }
    4292              : 
    4293              : 
    4294              : void
    4295    700430838 : MSVehicle::updateTimeLoss(double vNext) {
    4296              :     // update time loss (depends on the updated edge)
    4297    700430838 :     if (!isStopped()) {
    4298              :         // some cfModels (i.e. EIDM may drive faster than predicted by maxNextSpeed)
    4299    689646183 :         const double vmax = MIN2(myLane->getVehicleMaxSpeed(this), MAX2(myStopSpeed, vNext));
    4300    686501912 :         if (vmax > 0) {
    4301    686492995 :             myTimeLoss += TS * (vmax - vNext) / vmax;
    4302              :         }
    4303              :     }
    4304    700430838 : }
    4305              : 
    4306              : 
    4307              : double
    4308   1564977146 : MSVehicle::checkReversal(bool& canReverse, double speedThreshold, double seen) const {
    4309     64418650 :     const bool stopOk = (myStops.empty() || myStops.front().edge != myCurrEdge
    4310   1597326792 :                          || (myStops.front().getSpeed() > 0 && myState.myPos > myStops.front().pars.endPos - 2 * POSITION_EPS));
    4311              : #ifdef DEBUG_REVERSE_BIDI
    4312              :     if (DEBUG_COND) std::cout << SIMTIME  << " checkReversal lane=" << myLane->getID()
    4313              :                                   << " pos=" << myState.myPos
    4314              :                                   << " speed=" << std::setprecision(6) << getPreviousSpeed() << std::setprecision(gPrecision)
    4315              :                                   << " speedThreshold=" << speedThreshold
    4316              :                                   << " seen=" << seen
    4317              :                                   << " isRail=" << isRail()
    4318              :                                   << " speedOk=" << (getPreviousSpeed() <= speedThreshold)
    4319              :                                   << " posOK=" << (myState.myPos <= myLane->getLength())
    4320              :                                   << " normal=" << !myLane->isInternal()
    4321              :                                   << " routeOK=" << ((myCurrEdge + 1) != myRoute->end())
    4322              :                                   << " bidi=" << (myLane->getEdge().getBidiEdge() == *(myCurrEdge + 1))
    4323              :                                   << " stopOk=" << stopOk
    4324              :                                   << "\n";
    4325              : #endif
    4326   1564977146 :     if ((getVClass() & SVC_RAIL_CLASSES) != 0
    4327      7241933 :             && getPreviousSpeed() <= speedThreshold
    4328      6166940 :             && myState.myPos <= myLane->getLength()
    4329      6165842 :             && !myLane->isInternal()
    4330      6092296 :             && (myCurrEdge + 1) != myRoute->end()
    4331      5981243 :             && myLane->getEdge().getBidiEdge() == *(myCurrEdge + 1)
    4332              :             // ensure there are no further stops on this edge
    4333   1565826420 :             && stopOk
    4334              :        ) {
    4335              :         //if (isSelected()) std::cout << "   check1 passed\n";
    4336              : 
    4337              :         // ensure that the vehicle is fully on bidi edges that allow reversal
    4338       180793 :         const int neededFutureRoute = 1 + (int)(MSGlobals::gUsingInternalLanes
    4339              :                                                 ? myFurtherLanes.size()
    4340          504 :                                                 : ceil((double)myFurtherLanes.size() / 2.0));
    4341       180793 :         const int remainingRoute = int(myRoute->end() - myCurrEdge) - 1;
    4342       180793 :         if (remainingRoute < neededFutureRoute) {
    4343              : #ifdef DEBUG_REVERSE_BIDI
    4344              :             if (DEBUG_COND) {
    4345              :                 std::cout << "    fail: remainingEdges=" << ((int)(myRoute->end() - myCurrEdge)) << " further=" << myFurtherLanes.size() << "\n";
    4346              :             }
    4347              : #endif
    4348         3567 :             return getMaxSpeed();
    4349              :         }
    4350              :         //if (isSelected()) std::cout << "   check2 passed\n";
    4351              : 
    4352              :         // ensure that the turn-around connection exists from the current edge to its bidi-edge
    4353       177226 :         const MSEdgeVector& succ = myLane->getEdge().getSuccessors();
    4354       177226 :         if (std::find(succ.begin(), succ.end(), myLane->getEdge().getBidiEdge()) == succ.end()) {
    4355              : #ifdef DEBUG_REVERSE_BIDI
    4356              :             if (DEBUG_COND) {
    4357              :                 std::cout << "    noTurn (bidi=" << myLane->getEdge().getBidiEdge()->getID() << " succ=" << toString(succ) << "\n";
    4358              :             }
    4359              : #endif
    4360          909 :             return getMaxSpeed();
    4361              :         }
    4362              :         //if (isSelected()) std::cout << "   check3 passed\n";
    4363              : 
    4364              :         // ensure that the vehicle front will not move past a stop on the bidi edge of the current edge
    4365       176317 :         if (!myStops.empty() && myStops.front().edge == (myCurrEdge + 1)) {
    4366       160006 :             const double stopPos = myStops.front().getEndPos(*this);
    4367       160006 :             const double brakeDist = getCarFollowModel().brakeGap(getSpeed(), getCarFollowModel().getMaxDecel(), 0);
    4368       160006 :             const double newPos = myLane->getLength() - (getBackPositionOnLane() + brakeDist);
    4369       160006 :             if (newPos > stopPos) {
    4370              : #ifdef DEBUG_REVERSE_BIDI
    4371              :                 if (DEBUG_COND) {
    4372              :                     std::cout << "    reversal would go past stop on " << myLane->getBidiLane()->getID() << "\n";
    4373              :                 }
    4374              : #endif
    4375       158332 :                 if (seen > MAX2(brakeDist, 1.0)) {
    4376       157202 :                     return getMaxSpeed();
    4377              :                 } else {
    4378              : #ifdef DEBUG_REVERSE_BIDI
    4379              :                     if (DEBUG_COND) {
    4380              :                         std::cout << "    train is too long, skipping stop at " << stopPos << " cannot be avoided\n";
    4381              :                     }
    4382              : #endif
    4383              :                 }
    4384              :             }
    4385              :         }
    4386              :         //if (isSelected()) std::cout << "   check4 passed\n";
    4387              : 
    4388              :         // ensure that bidi-edges exist for all further edges
    4389              :         // and that no stops will be skipped when reversing
    4390              :         // and that the train will not be on top of a red rail signal after reversal
    4391        19115 :         const MSLane* bidi = myLane->getBidiLane();
    4392              :         int view = 2;
    4393        38572 :         for (MSLane* further : myFurtherLanes) {
    4394        21893 :             if (!further->getEdge().isInternal()) {
    4395        11393 :                 if (further->getEdge().getBidiEdge() != *(myCurrEdge + view)) {
    4396              : #ifdef DEBUG_REVERSE_BIDI
    4397              :                     if (DEBUG_COND) {
    4398              :                         std::cout << "    noBidi view=" << view << " further=" << further->getID() << " furtherBidi=" << Named::getIDSecure(further->getEdge().getBidiEdge()) << " future=" << (*(myCurrEdge + view))->getID() << "\n";
    4399              :                     }
    4400              : #endif
    4401         2277 :                     return getMaxSpeed();
    4402              :                 }
    4403         9116 :                 const MSLane* nextBidi = further->getBidiLane();
    4404         9116 :                 const MSLink* toNext = bidi->getLinkTo(nextBidi);
    4405         9116 :                 if (toNext == nullptr) {
    4406              :                     // can only happen if the route is invalid
    4407            0 :                     return getMaxSpeed();
    4408              :                 }
    4409         9116 :                 if (toNext->haveRed()) {
    4410              : #ifdef DEBUG_REVERSE_BIDI
    4411              :                     if (DEBUG_COND) {
    4412              :                         std::cout << "    do not reverse on a red signal\n";
    4413              :                     }
    4414              : #endif
    4415            0 :                     return getMaxSpeed();
    4416              :                 }
    4417              :                 bidi = nextBidi;
    4418         9116 :                 if (!myStops.empty() && myStops.front().edge == (myCurrEdge + view)) {
    4419          453 :                     const double brakeDist = getCarFollowModel().brakeGap(getSpeed(), getCarFollowModel().getMaxDecel(), 0);
    4420          453 :                     const double stopPos = myStops.front().getEndPos(*this);
    4421          453 :                     const double newPos = further->getLength() - (getBackPositionOnLane(further) + brakeDist);
    4422          453 :                     if (newPos > stopPos) {
    4423              : #ifdef DEBUG_REVERSE_BIDI
    4424              :                         if (DEBUG_COND) {
    4425              :                             std::cout << "    reversal would go past stop on further-opposite lane " << further->getBidiLane()->getID() << "\n";
    4426              :                         }
    4427              : #endif
    4428          171 :                         if (seen > MAX2(brakeDist, 1.0)) {
    4429          159 :                             canReverse = false;
    4430          159 :                             return getMaxSpeed();
    4431              :                         } else {
    4432              : #ifdef DEBUG_REVERSE_BIDI
    4433              :                             if (DEBUG_COND) {
    4434              :                                 std::cout << "    train is too long, skipping stop at " << stopPos << " cannot be avoided\n";
    4435              :                             }
    4436              : #endif
    4437              :                         }
    4438              :                     }
    4439              :                 }
    4440         8957 :                 view++;
    4441              :             }
    4442              :         }
    4443              :         // reverse as soon as comfortably possible
    4444        16679 :         const double vMinComfortable = getCarFollowModel().minNextSpeed(getSpeed(), this);
    4445              : #ifdef DEBUG_REVERSE_BIDI
    4446              :         if (DEBUG_COND) {
    4447              :             std::cout << SIMTIME << " seen=" << seen  << " vReverseOK=" << vMinComfortable << "\n";
    4448              :         }
    4449              : #endif
    4450        16679 :         canReverse = true;
    4451        16679 :         return vMinComfortable;
    4452              :     }
    4453   1564796353 :     return getMaxSpeed();
    4454              : }
    4455              : 
    4456              : 
    4457              : void
    4458    700645699 : MSVehicle::processLaneAdvances(std::vector<MSLane*>& passedLanes, std::string& emergencyReason) {
    4459    716463700 :     for (std::vector<MSLane*>::reverse_iterator i = myFurtherLanes.rbegin(); i != myFurtherLanes.rend(); ++i) {
    4460     15818001 :         passedLanes.push_back(*i);
    4461              :     }
    4462    700645699 :     if (passedLanes.size() == 0 || passedLanes.back() != myLane) {
    4463    700645699 :         passedLanes.push_back(myLane);
    4464              :     }
    4465              :     // let trains reverse direction
    4466    700645699 :     bool reverseTrain = false;
    4467    700645699 :     checkReversal(reverseTrain);
    4468    700645699 :     if (reverseTrain) {
    4469              :         // Train is 'reversing' so toggle the logical state
    4470          810 :         myAmReversed = !myAmReversed;
    4471              :         // add some slack to ensure that the back of train does appear looped
    4472          810 :         myState.myPos += 2 * (myLane->getLength() - myState.myPos) + myType->getLength() + NUMERICAL_EPS;
    4473          810 :         myState.mySpeed = 0;
    4474              : #ifdef DEBUG_REVERSE_BIDI
    4475              :         if (DEBUG_COND) {
    4476              :             std::cout << SIMTIME << " reversing train=" << getID() << " newPos=" << myState.myPos << "\n";
    4477              :         }
    4478              : #endif
    4479              :     }
    4480              :     // move on lane(s)
    4481    700645699 :     if (myState.myPos > myLane->getLength()) {
    4482              :         // The vehicle has moved at least to the next lane (maybe it passed even more than one)
    4483     19956096 :         if (myCurrEdge != myRoute->end() - 1) {
    4484     16833935 :             MSLane* approachedLane = myLane;
    4485              :             // move the vehicle forward
    4486     16833935 :             myNextDriveItem = myLFLinkLanes.begin();
    4487     36313441 :             while (myNextDriveItem != myLFLinkLanes.end() && approachedLane != nullptr && myState.myPos > approachedLane->getLength()) {
    4488     19498399 :                 const MSLink* link = myNextDriveItem->myLink;
    4489     19498399 :                 const double linkDist = myNextDriveItem->myDistance;
    4490              :                 ++myNextDriveItem;
    4491              :                 // check whether the vehicle was allowed to enter lane
    4492              :                 //  otherwise it is decelerated and we do not need to test for it's
    4493              :                 //  approach on the following lanes when a lane changing is performed
    4494              :                 // proceed to the next lane
    4495     19498399 :                 if (approachedLane->mustCheckJunctionCollisions()) {
    4496              :                     // vehicle moves past approachedLane within a single step, collision checking must still be done
    4497        66538 :                     MSNet::getInstance()->getEdgeControl().checkCollisionForInactive(approachedLane);
    4498              :                 }
    4499     19498399 :                 if (link != nullptr) {
    4500     19494214 :                     if ((getVClass() & SVC_RAIL_CLASSES) != 0
    4501        45932 :                             && !myLane->isInternal()
    4502        24182 :                             && myLane->getBidiLane() != nullptr
    4503        13042 :                             && link->getLane()->getBidiLane() == myLane
    4504     19495021 :                             && !reverseTrain) {
    4505              :                         emergencyReason = " because it must reverse direction";
    4506              :                         approachedLane = nullptr;
    4507              :                         break;
    4508              :                     }
    4509     19494211 :                     if ((getVClass() & SVC_RAIL_CLASSES) != 0
    4510        45929 :                             && myState.myPos < myLane->getLength() + NUMERICAL_EPS
    4511     19494427 :                             && hasStops() && getNextStop().edge == myCurrEdge) {
    4512              :                         // avoid skipping stop due to numerical instability
    4513              :                         // this is a special case for rail vehicles because they
    4514              :                         // continue myLFLinkLanes past stops
    4515          202 :                         approachedLane = myLane;
    4516          202 :                         myState.myPos = myLane->getLength();
    4517          202 :                         break;
    4518              :                     }
    4519     19494009 :                     approachedLane = link->getViaLaneOrLane();
    4520     19494009 :                     if (myInfluencer == nullptr || myInfluencer->getEmergencyBrakeRedLight()) {
    4521     19492412 :                         bool beyondStopLine = linkDist < link->getLaneBefore()->getVehicleStopOffset(this);
    4522     19492412 :                         if (link->haveRed() && !ignoreRed(link, false) && !beyondStopLine && !reverseTrain) {
    4523              :                             emergencyReason = " because of a red traffic light";
    4524              :                             break;
    4525              :                         }
    4526              :                     }
    4527     19493948 :                     if (reverseTrain && approachedLane->isInternal()) {
    4528              :                         // avoid getting stuck on a slow turn-around internal lane
    4529          888 :                         myState.myPos += approachedLane->getLength();
    4530              :                     }
    4531         4185 :                 } else if (myState.myPos < myLane->getLength() + NUMERICAL_EPS) {
    4532              :                     // avoid warning due to numerical instability
    4533          229 :                     approachedLane = myLane;
    4534          229 :                     myState.myPos = myLane->getLength();
    4535         3956 :                 } else if (reverseTrain) {
    4536            0 :                     approachedLane = (*(myCurrEdge + 1))->getLanes()[0];
    4537            0 :                     link = myLane->getLinkTo(approachedLane);
    4538              :                     assert(link != 0);
    4539            0 :                     while (link->getViaLane() != nullptr) {
    4540            0 :                         link = link->getViaLane()->getLinkCont()[0];
    4541              :                     }
    4542              :                     --myNextDriveItem;
    4543              :                 } else {
    4544              :                     emergencyReason = " because there is no connection to the next edge";
    4545              :                     approachedLane = nullptr;
    4546              :                     break;
    4547              :                 }
    4548     19494177 :                 if (approachedLane != myLane && approachedLane != nullptr) {
    4549     19493948 :                     leaveLane(MSMoveReminder::NOTIFICATION_JUNCTION, approachedLane);
    4550     19493948 :                     myState.myPos -= myLane->getLength();
    4551              :                     assert(myState.myPos > 0);
    4552     19493948 :                     enterLaneAtMove(approachedLane);
    4553     19493948 :                     if (link->isEntryLink()) {
    4554      7657882 :                         myJunctionEntryTime = MSNet::getInstance()->getCurrentTimeStep();
    4555      7657882 :                         myJunctionEntryTimeNeverYield = myJunctionEntryTime;
    4556      7657882 :                         myHaveStoppedFor = nullptr;
    4557              :                     }
    4558     19493948 :                     if (link->isConflictEntryLink()) {
    4559      7657266 :                         myJunctionConflictEntryTime = MSNet::getInstance()->getCurrentTimeStep();
    4560              :                         // renew yielded request
    4561      7657266 :                         myJunctionEntryTime = myJunctionEntryTimeNeverYield;
    4562              :                     }
    4563     19493948 :                     if (link->isExitLink()) {
    4564              :                         // passed junction, reset for approaching the next one
    4565      7597362 :                         myJunctionEntryTime = SUMOTime_MAX;
    4566      7597362 :                         myJunctionEntryTimeNeverYield = SUMOTime_MAX;
    4567      7597362 :                         myJunctionConflictEntryTime = SUMOTime_MAX;
    4568              :                     }
    4569              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    4570              :                     if (DEBUG_COND) {
    4571              :                         std::cout << "Update junctionTimes link=" << link->getViaLaneOrLane()->getID()
    4572              :                                   << " entry=" << link->isEntryLink() << " conflict=" << link->isConflictEntryLink() << " exit=" << link->isExitLink()
    4573              :                                   << " ET=" << myJunctionEntryTime
    4574              :                                   << " ETN=" << myJunctionEntryTimeNeverYield
    4575              :                                   << " CET=" << myJunctionConflictEntryTime
    4576              :                                   << "\n";
    4577              :                     }
    4578              : #endif
    4579     19493948 :                     if (hasArrivedInternal()) {
    4580              :                         break;
    4581              :                     }
    4582     19480033 :                     if (myLaneChangeModel->isChangingLanes()) {
    4583         7272 :                         if (link->getDirection() == LinkDirection::LEFT || link->getDirection() == LinkDirection::RIGHT) {
    4584              :                             // abort lane change
    4585           54 :                             WRITE_WARNINGF("Vehicle '%' could not finish continuous lane change (turn lane) time=%.", getID(), time2string(SIMSTEP));
    4586           18 :                             myLaneChangeModel->endLaneChangeManeuver();
    4587              :                         }
    4588              :                     }
    4589     19480033 :                     if (approachedLane->getEdge().isVaporizing()) {
    4590          756 :                         leaveLane(MSMoveReminder::NOTIFICATION_VAPORIZED_VAPORIZER);
    4591              :                         break;
    4592              :                     }
    4593     19479277 :                     passedLanes.push_back(approachedLane);
    4594              :                 }
    4595              :             }
    4596              :             // NOTE: Passed drive items will be erased in the next simstep's planMove()
    4597              : 
    4598              : #ifdef DEBUG_ACTIONSTEPS
    4599              :             if (DEBUG_COND && myNextDriveItem != myLFLinkLanes.begin()) {
    4600              :                 std::cout << "Updated drive items:" << std::endl;
    4601              :                 for (DriveItemVector::iterator i = myLFLinkLanes.begin(); i != myLFLinkLanes.end(); ++i) {
    4602              :                     std::cout
    4603              :                             << " vPass=" << (*i).myVLinkPass
    4604              :                             << " vWait=" << (*i).myVLinkWait
    4605              :                             << " linkLane=" << ((*i).myLink == 0 ? "NULL" : (*i).myLink->getViaLaneOrLane()->getID())
    4606              :                             << " request=" << (*i).mySetRequest
    4607              :                             << "\n";
    4608              :                 }
    4609              :             }
    4610              : #endif
    4611      3122161 :         } else if (!hasArrivedInternal() && myState.myPos < myLane->getLength() + NUMERICAL_EPS) {
    4612              :             // avoid warning due to numerical instability when stopping at the end of the route
    4613           90 :             myState.myPos = myLane->getLength();
    4614              :         }
    4615              : 
    4616              :     }
    4617    700645699 : }
    4618              : 
    4619              : 
    4620              : 
    4621              : bool
    4622    704245042 : MSVehicle::executeMove() {
    4623              : #ifdef DEBUG_EXEC_MOVE
    4624              :     if (DEBUG_COND) {
    4625              :         std::cout << "\nEXECUTE_MOVE\n"
    4626              :                   << SIMTIME
    4627              :                   << " veh=" << getID()
    4628              :                   << " speed=" << getSpeed() // toString(getSpeed(), 24)
    4629              :                   << std::endl;
    4630              :     }
    4631              : #endif
    4632              : 
    4633              : 
    4634              :     // Maximum safe velocity
    4635    704245042 :     double vSafe = std::numeric_limits<double>::max();
    4636              :     // Minimum safe velocity (lower bound).
    4637    704245042 :     double vSafeMin = -std::numeric_limits<double>::max();
    4638              :     // The distance to a link, which should either be crossed this step
    4639              :     // or in front of which we need to stop.
    4640    704245042 :     double vSafeMinDist = 0;
    4641              : 
    4642              :     // myAngle will be subsequently be updated by movement and entering new lanes or sublane-changing
    4643    704245042 :     myLastAngle = myRawAngle;
    4644              : 
    4645    704245042 :     if (myActionStep) {
    4646              :         // Actuate control (i.e. choose bounds for safe speed in current simstep (euler), resp. after current sim step (ballistic))
    4647    632683106 :         processLinkApproaches(vSafe, vSafeMin, vSafeMinDist);
    4648              : #ifdef DEBUG_ACTIONSTEPS
    4649              :         if (DEBUG_COND) {
    4650              :             std::cout << SIMTIME << " vehicle '" << getID() << "'\n"
    4651              :                       "   vsafe from processLinkApproaches(): vsafe " << vSafe << std::endl;
    4652              :         }
    4653              : #endif
    4654              :     } else {
    4655              :         // Continue with current acceleration
    4656     71561936 :         vSafe = getSpeed() + ACCEL2SPEED(myAcceleration);
    4657              : #ifdef DEBUG_ACTIONSTEPS
    4658              :         if (DEBUG_COND) {
    4659              :             std::cout << SIMTIME << " vehicle '" << getID() << "' skips processLinkApproaches()\n"
    4660              :                       "   continues with constant accel " <<  myAcceleration << "...\n"
    4661              :                       << "speed: "  << getSpeed() << " -> " << vSafe << std::endl;
    4662              :         }
    4663              : #endif
    4664              :     }
    4665              : 
    4666              : 
    4667              : //#ifdef DEBUG_EXEC_MOVE
    4668              : //    if (DEBUG_COND) {
    4669              : //        std::cout << "vSafe = " << toString(vSafe,12) << "\n" << std::endl;
    4670              : //    }
    4671              : //#endif
    4672              : 
    4673              :     // Determine vNext = speed after current sim step (ballistic), resp. in current simstep (euler)
    4674              :     // Call to finalizeSpeed applies speed reduction due to dawdling / lane changing but ensures minimum safe speed
    4675    704245042 :     double vNext = vSafe;
    4676              :     const MSCFModel& cfModel = getCarFollowModel();
    4677    704245042 :     const double rawAccel = SPEED2ACCEL(MAX2(vNext, 0.) - myState.mySpeed);
    4678    704245042 :     if (vNext <= SUMO_const_haltingSpeed * TS && myWaitingTime > MSGlobals::gStartupWaitThreshold && rawAccel <= accelThresholdForWaiting() && myActionStep) {
    4679     75495474 :         myTimeSinceStartup = 0;
    4680    628749568 :     } else if (isStopped()) {
    4681              :         // do not apply startupDelay for waypoints
    4682     17731160 :         if (cfModel.startupDelayStopped() && getNextStop().pars.speed <= 0) {
    4683        13772 :             myTimeSinceStartup = DELTA_T;
    4684              :         } else {
    4685              :             // do not apply startupDelay but signal that a stop has taken place
    4686     17717388 :             myTimeSinceStartup = cfModel.getStartupDelay() + DELTA_T;
    4687              :         }
    4688              :     } else {
    4689              :         // identify potential startup (before other effects reduce the speed again)
    4690    611018408 :         myTimeSinceStartup += DELTA_T;
    4691              :     }
    4692    704245042 :     if (myActionStep) {
    4693    632683106 :         vNext = cfModel.finalizeSpeed(this, vSafe);
    4694    628868902 :         if (vNext > 0) {
    4695    586572689 :             vNext = MAX2(vNext, vSafeMin);
    4696              :         }
    4697              :     }
    4698              :     // (Leo) to avoid tiny oscillations (< 1e-10) of vNext in a standing vehicle column (observed for ballistic update), we cap off vNext
    4699              :     //       (We assure to do this only for vNext<<NUMERICAL_EPS since otherwise this would nullify the workaround for #2995
    4700              :     // (Jakob) We also need to make sure to reach a stop at the start of the next edge
    4701    700430838 :     if (fabs(vNext) < NUMERICAL_EPS_SPEED && (myStopDist > POSITION_EPS || (hasStops() && myCurrEdge == getNextStop().edge))) {
    4702              :         vNext = 0.;
    4703              :     }
    4704              : #ifdef DEBUG_EXEC_MOVE
    4705              :     if (DEBUG_COND) {
    4706              :         std::cout << SIMTIME << " finalizeSpeed vSafe=" << vSafe << " vSafeMin=" << (vSafeMin == -std::numeric_limits<double>::max() ? "-Inf" : toString(vSafeMin))
    4707              :                   << " vNext=" << vNext << " (i.e. accel=" << SPEED2ACCEL(vNext - getSpeed()) << ")" << std::endl;
    4708              :     }
    4709              : #endif
    4710              : 
    4711              :     // vNext may be higher than vSafe without implying a bug:
    4712              :     //  - when approaching a green light that suddenly switches to yellow
    4713              :     //  - when using unregulated junctions
    4714              :     //  - when using tau < step-size
    4715              :     //  - when using unsafe car following models
    4716              :     //  - when using TraCI and some speedMode / laneChangeMode settings
    4717              :     //if (vNext > vSafe + NUMERICAL_EPS) {
    4718              :     //    WRITE_WARNING("vehicle '" + getID() + "' cannot brake hard enough to reach safe speed "
    4719              :     //            + toString(vSafe, 4) + ", moving at " + toString(vNext, 4) + " instead. time="
    4720              :     //            + time2string(MSNet::getInstance()->getCurrentTimeStep()) + ".");
    4721              :     //}
    4722              : 
    4723    700430838 :     if (MSGlobals::gSemiImplicitEulerUpdate) {
    4724              :         vNext = MAX2(vNext, 0.);
    4725              :     } else {
    4726              :         // (Leo) Ballistic: negative vNext can be used to indicate a stop within next step.
    4727              :     }
    4728              : 
    4729              :     // Check for speed advices from the traci client
    4730    700430838 :     vNext = processTraCISpeedControl(vSafe, vNext);
    4731              : 
    4732              :     // the acceleration of a vehicle equipped with the elecHybrid device is restricted by the maximal power of the electric drive as well
    4733    700430838 :     MSDevice_ElecHybrid* elecHybridOfVehicle = dynamic_cast<MSDevice_ElecHybrid*>(getDevice(typeid(MSDevice_ElecHybrid)));
    4734          981 :     if (elecHybridOfVehicle != nullptr) {
    4735              :         // this is the consumption given by the car following model-computed acceleration
    4736          981 :         elecHybridOfVehicle->setConsum(elecHybridOfVehicle->consumption(*this, (vNext - this->getSpeed()) / TS, vNext));
    4737              :         // but the maximum power of the electric motor may be lower
    4738              :         // it needs to be converted from [W] to [Wh/s] (3600s / 1h) so that TS can be taken into account
    4739          981 :         double maxPower = getEmissionParameters()->getDoubleOptional(SUMO_ATTR_MAXIMUMPOWER, 100000.) / 3600;
    4740          981 :         if (elecHybridOfVehicle->getConsum() / TS > maxPower) {
    4741              :             // no, we cannot accelerate that fast, recompute the maximum possible acceleration
    4742           70 :             double accel = elecHybridOfVehicle->acceleration(*this, maxPower, this->getSpeed());
    4743              :             // and update the speed of the vehicle
    4744           70 :             vNext = MIN2(vNext, this->getSpeed() + accel * TS);
    4745              :             vNext = MAX2(vNext, 0.);
    4746              :             // and set the vehicle consumption to reflect this
    4747           70 :             elecHybridOfVehicle->setConsum(elecHybridOfVehicle->consumption(*this, (vNext - this->getSpeed()) / TS, vNext));
    4748              :         }
    4749              :     }
    4750              : 
    4751    700430838 :     setBrakingSignals(vNext);
    4752              : 
    4753              :     // update position and speed
    4754    700430838 :     int oldLaneOffset = myLane->getEdge().getNumLanes() - myLane->getIndex();
    4755              :     const MSLane* oldLaneMaybeOpposite = myLane;
    4756    700430838 :     if (myLaneChangeModel->isOpposite()) {
    4757              :         // transform to the forward-direction lane, move and then transform back
    4758       406567 :         myState.myPos = myLane->getOppositePos(myState.myPos);
    4759       406567 :         myLane = myLane->getParallelOpposite();
    4760              :     }
    4761    700430838 :     updateState(vNext);
    4762    700430838 :     updateWaitingTime(vNext);
    4763              : 
    4764              :     // Lanes, which the vehicle touched at some moment of the executed simstep
    4765              :     std::vector<MSLane*> passedLanes;
    4766              :     // remember previous lane (myLane is updated in processLaneAdvances)
    4767    700430838 :     const MSLane* oldLane = myLane;
    4768              :     // Reason for a possible emergency stop
    4769              :     std::string emergencyReason;
    4770    700430838 :     processLaneAdvances(passedLanes, emergencyReason);
    4771              : 
    4772    700430838 :     updateTimeLoss(vNext);
    4773    700430838 :     myCollisionImmunity = MAX2((SUMOTime) - 1, myCollisionImmunity - DELTA_T);
    4774              : 
    4775    700430838 :     if (!hasArrivedInternal() && !myLane->getEdge().isVaporizing()) {
    4776    697113268 :         if (myState.myPos > myLane->getLength()) {
    4777          350 :             if (emergencyReason == "") {
    4778           18 :                 emergencyReason = TL(" for unknown reasons");
    4779              :             }
    4780         1400 :             WRITE_WARNINGF(TL("Vehicle '%' performs emergency stop at the end of lane '%'% (decel=%, offset=%), time=%."),
    4781              :                            getID(), myLane->getID(), emergencyReason, myAcceleration - myState.mySpeed,
    4782              :                            myState.myPos - myLane->getLength(), time2string(SIMSTEP));
    4783          350 :             MSNet::getInstance()->getVehicleControl().registerEmergencyStop();
    4784          350 :             MSNet::getInstance()->informVehicleStateListener(this, MSNet::VehicleState::EMERGENCYSTOP);
    4785          350 :             myState.myPos = myLane->getLength();
    4786          350 :             myState.mySpeed = 0;
    4787          350 :             myAcceleration = 0;
    4788              :         }
    4789    697113268 :         const MSLane* oldBackLane = getBackLane();
    4790    697113268 :         if (myLaneChangeModel->isOpposite()) {
    4791              :             passedLanes.clear(); // ignore back occupation
    4792              :         }
    4793              : #ifdef DEBUG_ACTIONSTEPS
    4794              :         if (DEBUG_COND) {
    4795              :             std::cout << SIMTIME << " veh '" << getID() << "' updates further lanes." << std::endl;
    4796              :         }
    4797              : #endif
    4798    697113268 :         myState.myBackPos = updateFurtherLanes(myFurtherLanes, myFurtherLanesPosLat, passedLanes);
    4799    697113268 :         if (passedLanes.size() > 1 && isRail()) {
    4800       872961 :             for (auto pi = passedLanes.rbegin(); pi != passedLanes.rend(); ++pi) {
    4801       658004 :                 MSLane* pLane = *pi;
    4802       658004 :                 if (pLane != myLane && std::find(myFurtherLanes.begin(), myFurtherLanes.end(), pLane) == myFurtherLanes.end()) {
    4803        45579 :                     leaveLaneBack(MSMoveReminder::NOTIFICATION_JUNCTION, *pi);
    4804              :                 }
    4805              :             }
    4806              :         }
    4807              :         // bestLanes need to be updated before lane changing starts. NOTE: This call is also a presumption for updateDriveItems()
    4808    697113268 :         updateBestLanes();
    4809    697113268 :         if (myLane != oldLane || oldBackLane != getBackLane()) {
    4810     24737521 :             if (myLaneChangeModel->getShadowLane() != nullptr || getLateralOverlap() > POSITION_EPS) {
    4811              :                 // shadow lane must be updated if the front or back lane changed
    4812              :                 // either if we already have a shadowLane or if there is lateral overlap
    4813       553804 :                 myLaneChangeModel->updateShadowLane();
    4814              :             }
    4815     24737521 :             if (MSGlobals::gLateralResolution > 0 && !myLaneChangeModel->isOpposite()) {
    4816              :                 // The vehicles target lane must be also be updated if the front or back lane changed
    4817      4415018 :                 myLaneChangeModel->updateTargetLane();
    4818              :             }
    4819              :         }
    4820    697113268 :         setBlinkerInformation(); // needs updated bestLanes
    4821              :         //change the blue light only for emergency vehicles SUMOVehicleClass
    4822    697113268 :         if (myType->getVehicleClass() == SVC_EMERGENCY) {
    4823        85511 :             setEmergencyBlueLight(MSNet::getInstance()->getCurrentTimeStep());
    4824              :         }
    4825              :         // must be done before angle computation
    4826              :         // State needs to be reset for all vehicles before the next call to MSEdgeControl::changeLanes
    4827    697113268 :         if (myActionStep) {
    4828              :             // check (#2681): Can this be skipped?
    4829    625572612 :             myLaneChangeModel->prepareStep();
    4830              :         } else {
    4831     71540656 :             myLaneChangeModel->resetSpeedLat();
    4832              : #ifdef DEBUG_ACTIONSTEPS
    4833              :             if (DEBUG_COND) {
    4834              :                 std::cout << SIMTIME << " veh '" << getID() << "' skips LCM->prepareStep()." << std::endl;
    4835              :             }
    4836              : #endif
    4837              :         }
    4838    697113268 :         myLaneChangeModel->setPreviousAngleOffset(myLaneChangeModel->getAngleOffset());
    4839    697113268 :         myAngle = computeAngle();
    4840              :     }
    4841              : 
    4842              : #ifdef DEBUG_EXEC_MOVE
    4843              :     if (DEBUG_COND) {
    4844              :         std::cout << SIMTIME << " executeMove finished veh=" << getID() << " lane=" << myLane->getID() << " myPos=" << getPositionOnLane() << " myPosLat=" << getLateralPositionOnLane() << "\n";
    4845              :         gDebugFlag1 = false; // See MSLink_DEBUG_OPENED
    4846              :     }
    4847              : #endif
    4848    700430838 :     if (myLaneChangeModel->isOpposite()) {
    4849              :         // transform back to the opposite-direction lane
    4850              :         MSLane* newOpposite = nullptr;
    4851       406567 :         const MSEdge* newOppositeEdge = myLane->getEdge().getOppositeEdge();
    4852       406567 :         if (newOppositeEdge != nullptr) {
    4853       406517 :             newOpposite = newOppositeEdge->getLanes()[newOppositeEdge->getNumLanes() - MAX2(1, oldLaneOffset)];
    4854              : #ifdef DEBUG_EXEC_MOVE
    4855              :             if (DEBUG_COND) {
    4856              :                 std::cout << SIMTIME << "   newOppositeEdge=" << newOppositeEdge->getID() << " oldLaneOffset=" << oldLaneOffset << " leftMost=" << newOppositeEdge->getNumLanes() - 1 << " newOpposite=" << Named::getIDSecure(newOpposite) << "\n";
    4857              :             }
    4858              : #endif
    4859              :         }
    4860       406517 :         if (newOpposite == nullptr) {
    4861           50 :             if (!myLaneChangeModel->hasBlueLight()) {
    4862              :                 // unusual overtaking at junctions is ok for emergency vehicles
    4863            0 :                 WRITE_WARNINGF(TL("Unexpected end of opposite lane for vehicle '%' at lane '%', time=%."),
    4864              :                                getID(), myLane->getID(), time2string(SIMSTEP));
    4865              :             }
    4866           50 :             myLaneChangeModel->changedToOpposite();
    4867           50 :             if (myState.myPos < getLength()) {
    4868              :                 // further lanes is always cleared during opposite driving
    4869           50 :                 MSLane* oldOpposite = oldLane->getOpposite();
    4870           50 :                 if (oldOpposite != nullptr) {
    4871           50 :                     myFurtherLanes.push_back(oldOpposite);
    4872           50 :                     myFurtherLanesPosLat.push_back(0);
    4873              :                     // small value since the lane is going in the other direction
    4874           50 :                     myState.myBackPos = getLength() - myState.myPos;
    4875           50 :                     myAngle = computeAngle();
    4876              :                 } else {
    4877              :                     SOFT_ASSERT(false);
    4878              :                 }
    4879              :             }
    4880              :         } else {
    4881       406517 :             myState.myPos = myLane->getOppositePos(myState.myPos);
    4882       406517 :             myLane = newOpposite;
    4883              :             oldLane = oldLaneMaybeOpposite;
    4884              :             //std::cout << SIMTIME << " updated myLane=" << Named::getIDSecure(myLane) << " oldLane=" << oldLane->getID() << "\n";
    4885       406517 :             myCachedPosition = Position::INVALID;
    4886       406517 :             myLaneChangeModel->updateShadowLane();
    4887              :         }
    4888              :     }
    4889    700430838 :     workOnMoveReminders(myState.myPos - myState.myLastCoveredDist, myState.myPos, myState.mySpeed);
    4890              :     // store angle before lane changing
    4891    700430838 :     myRawAngle = myAngle;
    4892              :     // Return whether the vehicle did move to another lane
    4893   1400861676 :     return myLane != oldLane;
    4894    700430838 : }
    4895              : 
    4896              : void
    4897       214861 : MSVehicle::executeFractionalMove(double dist) {
    4898       214861 :     myState.myPos += dist;
    4899       214861 :     myState.myLastCoveredDist = dist;
    4900       214861 :     myCachedPosition = Position::INVALID;
    4901              : 
    4902       214861 :     const std::vector<const MSLane*> lanes = getUpcomingLanesUntil(dist);
    4903       214861 :     const SUMOTime t = MSNet::getInstance()->getCurrentTimeStep();
    4904       445727 :     for (int i = 0; i < (int)lanes.size(); i++) {
    4905       230866 :         MSLink* link = nullptr;
    4906       230866 :         if (i + 1 < (int)lanes.size()) {
    4907        16005 :             const MSLane* const to = lanes[i + 1];
    4908        16005 :             const bool internal = to->isInternal();
    4909        16010 :             for (MSLink* const l : lanes[i]->getLinkCont()) {
    4910        16010 :                 if ((internal && l->getViaLane() == to) || (!internal && l->getLane() == to)) {
    4911        16005 :                     link = l;
    4912        16005 :                     break;
    4913              :                 }
    4914              :             }
    4915              :         }
    4916       230866 :         myLFLinkLanes.emplace_back(link, getSpeed(), getSpeed(), true, t, getSpeed(), 0, 0, dist);
    4917              :     }
    4918              :     // minimum execute move:
    4919              :     std::vector<MSLane*> passedLanes;
    4920              :     // Reason for a possible emergency stop
    4921       214861 :     if (lanes.size() > 1) {
    4922         4005 :         myLane->removeVehicle(this, MSMoveReminder::NOTIFICATION_JUNCTION, false);
    4923              :     }
    4924              :     std::string emergencyReason;
    4925       214861 :     processLaneAdvances(passedLanes, emergencyReason);
    4926              : #ifdef DEBUG_EXTRAPOLATE_DEPARTPOS
    4927              :     if (DEBUG_COND) {
    4928              :         std::cout << SIMTIME << " veh=" << getID() << " executeFractionalMove dist=" << dist
    4929              :                   << " passedLanes=" << toString(passedLanes) << " lanes=" << toString(lanes)
    4930              :                   << " finalPos=" << myState.myPos
    4931              :                   << " speed=" << getSpeed()
    4932              :                   << " myFurtherLanes=" << toString(myFurtherLanes)
    4933              :                   << "\n";
    4934              :     }
    4935              : #endif
    4936       214861 :     workOnMoveReminders(myState.myPos - myState.myLastCoveredDist, myState.myPos, myState.mySpeed);
    4937       214861 :     if (lanes.size() > 1) {
    4938         4010 :         for (std::vector<MSLane*>::iterator i = myFurtherLanes.begin(); i != myFurtherLanes.end(); ++i) {
    4939              : #ifdef DEBUG_FURTHER
    4940              :             if (DEBUG_COND) {
    4941              :                 std::cout << SIMTIME << " leaveLane \n";
    4942              :             }
    4943              : #endif
    4944            5 :             (*i)->resetPartialOccupation(this);
    4945              :         }
    4946              :         myFurtherLanes.clear();
    4947              :         myFurtherLanesPosLat.clear();
    4948         4005 :         myLane->forceVehicleInsertion(this, getPositionOnLane(), MSMoveReminder::NOTIFICATION_JUNCTION, getLateralPositionOnLane());
    4949              :     }
    4950       214861 : }
    4951              : 
    4952              : 
    4953              : void
    4954    708598488 : MSVehicle::updateState(double vNext, bool parking) {
    4955              :     // update position and speed
    4956              :     double deltaPos; // positional change
    4957    708598488 :     if (MSGlobals::gSemiImplicitEulerUpdate) {
    4958              :         // euler
    4959    609300325 :         deltaPos = SPEED2DIST(vNext);
    4960              :     } else {
    4961              :         // ballistic
    4962     99298163 :         deltaPos = getDeltaPos(SPEED2ACCEL(vNext - myState.mySpeed));
    4963              :     }
    4964              : 
    4965              :     // the *mean* acceleration during the next step (probably most appropriate for emission calculation)
    4966              :     // NOTE: for the ballistic update vNext may be negative, indicating a stop.
    4967    708598488 :     myAcceleration = SPEED2ACCEL(MAX2(vNext, 0.) - myState.mySpeed);
    4968              : 
    4969              : #ifdef DEBUG_EXEC_MOVE
    4970              :     if (DEBUG_COND) {
    4971              :         std::cout << SIMTIME << " updateState() for veh '" << getID() << "': deltaPos=" << deltaPos
    4972              :                   << " pos=" << myState.myPos << " newPos=" << myState.myPos + deltaPos << std::endl;
    4973              :     }
    4974              : #endif
    4975    708598488 :     double decelPlus = -myAcceleration - getCarFollowModel().getMaxDecel() - NUMERICAL_EPS;
    4976    708598488 :     if (decelPlus > 0) {
    4977       429606 :         const double previousAcceleration = SPEED2ACCEL(myState.mySpeed - myState.myPreviousSpeed);
    4978       429606 :         if (myAcceleration + NUMERICAL_EPS < previousAcceleration) {
    4979              :             // vehicle brakes beyond wished maximum deceleration (only warn at the start of the braking manoeuvre)
    4980       290205 :             decelPlus += 2 * NUMERICAL_EPS;
    4981       290205 :             const double emergencyFraction = decelPlus / MAX2(NUMERICAL_EPS, getCarFollowModel().getEmergencyDecel() - getCarFollowModel().getMaxDecel());
    4982       290205 :             if (emergencyFraction >= MSGlobals::gEmergencyDecelWarningThreshold) {
    4983        95583 :                 WRITE_WARNINGF(TL("Vehicle '%' performs emergency braking on lane '%' with decel=%, wished=%, severity=%, time=%."),
    4984              :                                //+ " decelPlus=" + toString(decelPlus)
    4985              :                                //+ " prevAccel=" + toString(previousAcceleration)
    4986              :                                //+ " reserve=" + toString(MAX2(NUMERICAL_EPS, getCarFollowModel().getEmergencyDecel() - getCarFollowModel().getMaxDecel()))
    4987              :                                getID(), myLane->getID(), -myAcceleration, getCarFollowModel().getMaxDecel(), emergencyFraction, time2string(SIMSTEP));
    4988        31861 :                 MSNet::getInstance()->getVehicleControl().registerEmergencyBraking();
    4989              :             }
    4990              :         }
    4991              :     }
    4992              : 
    4993    708598488 :     myState.myPreviousSpeed = myState.mySpeed;
    4994    708598488 :     myState.mySpeed = MAX2(vNext, 0.);
    4995              : 
    4996    708598488 :     if (isRemoteControlled()) {
    4997         7174 :         deltaPos = myInfluencer->implicitDeltaPosRemote(this);
    4998              :     }
    4999              : 
    5000    708598488 :     myState.myPos += deltaPos;
    5001    708598488 :     myState.myLastCoveredDist = deltaPos;
    5002    708598488 :     myNextTurn.first -= deltaPos;
    5003              : 
    5004    708598488 :     if (!parking) {
    5005    700430838 :         myCachedPosition = Position::INVALID;
    5006              :     }
    5007    708598488 : }
    5008              : 
    5009              : void
    5010      8167650 : MSVehicle::updateParkingState() {
    5011      8167650 :     updateState(0, true);
    5012              :     // deboard while parked
    5013      8167650 :     if (myPersonDevice != nullptr) {
    5014       620078 :         myPersonDevice->notifyMove(*this, getPositionOnLane(), getPositionOnLane(), 0);
    5015              :     }
    5016      8167650 :     if (myContainerDevice != nullptr) {
    5017        59887 :         myContainerDevice->notifyMove(*this, getPositionOnLane(), getPositionOnLane(), 0);
    5018              :     }
    5019     16512753 :     for (MSVehicleDevice* const dev : myDevices) {
    5020      8345103 :         dev->notifyParking();
    5021              :     }
    5022      8167650 : }
    5023              : 
    5024              : 
    5025              : void
    5026        30555 : MSVehicle::replaceVehicleType(const MSVehicleType* type) {
    5027        30555 :     MSBaseVehicle::replaceVehicleType(type);
    5028        30555 :     delete myCFVariables;
    5029        30555 :     myCFVariables = type->getCarFollowModel().createVehicleVariables();
    5030        30555 : }
    5031              : 
    5032              : 
    5033              : const MSLane*
    5034   1378375788 : MSVehicle::getBackLane() const {
    5035   1378375788 :     if (myFurtherLanes.size() > 0) {
    5036     18897540 :         return myFurtherLanes.back();
    5037              :     } else {
    5038   1359478248 :         return myLane;
    5039              :     }
    5040              : }
    5041              : 
    5042              : 
    5043              : double
    5044    702612023 : MSVehicle::updateFurtherLanes(std::vector<MSLane*>& furtherLanes, std::vector<double>& furtherLanesPosLat,
    5045              :                               const std::vector<MSLane*>& passedLanes) {
    5046              : #ifdef DEBUG_SETFURTHER
    5047              :     if (DEBUG_COND) std::cout << SIMTIME << " veh=" << getID()
    5048              :                                   << " updateFurtherLanes oldFurther=" << toString(furtherLanes)
    5049              :                                   << " oldFurtherPosLat=" << toString(furtherLanesPosLat)
    5050              :                                   << " passed=" << toString(passedLanes)
    5051              :                                   << "\n";
    5052              : #endif
    5053    718465772 :     for (MSLane* further : furtherLanes) {
    5054     15853749 :         further->resetPartialOccupation(this);
    5055     15853749 :         if (further->getBidiLane() != nullptr
    5056     15853749 :                 && (!isRailway(getVClass()) || (further->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    5057       101822 :             further->getBidiLane()->resetPartialOccupation(this);
    5058              :         }
    5059              :     }
    5060              : 
    5061              :     std::vector<MSLane*> newFurther;
    5062              :     std::vector<double> newFurtherPosLat;
    5063    702612023 :     double backPosOnPreviousLane = myState.myPos - getLength();
    5064              :     bool widthShift = myFurtherLanesPosLat.size() > myFurtherLanes.size();
    5065    702612023 :     if (passedLanes.size() > 1) {
    5066              :         // There are candidates for further lanes. (passedLanes[-1] is the current lane, or current shadow lane in context of updateShadowLanes())
    5067              :         std::vector<MSLane*>::const_iterator fi = furtherLanes.begin();
    5068              :         std::vector<double>::const_iterator fpi = furtherLanesPosLat.begin();
    5069     44565646 :         for (auto pi = passedLanes.rbegin() + 1; pi != passedLanes.rend() && backPosOnPreviousLane < 0; ++pi) {
    5070              :             // As long as vehicle back reaches into passed lane, add it to the further lanes
    5071     15787352 :             MSLane* further = *pi;
    5072     15787352 :             newFurther.push_back(further);
    5073     15787352 :             backPosOnPreviousLane += further->setPartialOccupation(this);
    5074     15787352 :             if (further->getBidiLane() != nullptr
    5075     15787352 :                     && (!isRailway(getVClass()) || (further->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    5076        99995 :                 further->getBidiLane()->setPartialOccupation(this);
    5077              :             }
    5078     15787352 :             if (fi != furtherLanes.end() && further == *fi) {
    5079              :                 // Lateral position on this lane is already known. Assume constant and use old value.
    5080      5614216 :                 newFurtherPosLat.push_back(*fpi);
    5081              :                 ++fi;
    5082              :                 ++fpi;
    5083              :             } else {
    5084              :                 // The lane *pi was not in furtherLanes before.
    5085              :                 // If it is downstream, we assume as lateral position the current position
    5086              :                 // If it is a new lane upstream (can appear as shadow further in case of LC-maneuvering, e.g.)
    5087              :                 // we assign the last known lateral position.
    5088     10173136 :                 if (newFurtherPosLat.size() == 0) {
    5089      9559522 :                     if (widthShift) {
    5090      1502444 :                         newFurtherPosLat.push_back(myFurtherLanesPosLat.back());
    5091              :                     } else {
    5092      8057078 :                         newFurtherPosLat.push_back(myState.myPosLat);
    5093              :                     }
    5094              :                 } else {
    5095       613614 :                     newFurtherPosLat.push_back(newFurtherPosLat.back());
    5096              :                 }
    5097              :             }
    5098              : #ifdef DEBUG_SETFURTHER
    5099              :             if (DEBUG_COND) {
    5100              :                 std::cout << SIMTIME << " updateFurtherLanes \n"
    5101              :                           << "    further lane '" << further->getID() << "' backPosOnPreviousLane=" << backPosOnPreviousLane
    5102              :                           << std::endl;
    5103              :             }
    5104              : #endif
    5105              :         }
    5106     28778294 :         furtherLanes = newFurther;
    5107     28778294 :         furtherLanesPosLat = newFurtherPosLat;
    5108              :     } else {
    5109              :         furtherLanes.clear();
    5110              :         furtherLanesPosLat.clear();
    5111              :     }
    5112              : #ifdef DEBUG_SETFURTHER
    5113              :     if (DEBUG_COND) std::cout
    5114              :                 << " newFurther=" << toString(furtherLanes)
    5115              :                 << " newFurtherPosLat=" << toString(furtherLanesPosLat)
    5116              :                 << " newBackPos=" << backPosOnPreviousLane
    5117              :                 << "\n";
    5118              : #endif
    5119    702612023 :     return backPosOnPreviousLane;
    5120    702612023 : }
    5121              : 
    5122              : 
    5123              : double
    5124  34742102187 : MSVehicle::getBackPositionOnLane(const MSLane* lane, bool calledByGetPosition) const {
    5125              : #ifdef DEBUG_FURTHER
    5126              :     if (DEBUG_COND) {
    5127              :         std::cout << SIMTIME
    5128              :                   << " getBackPositionOnLane veh=" << getID()
    5129              :                   << " lane=" << Named::getIDSecure(lane)
    5130              :                   << " cbgP=" << calledByGetPosition
    5131              :                   << " pos=" << myState.myPos
    5132              :                   << " backPos=" << myState.myBackPos
    5133              :                   << " myLane=" << myLane->getID()
    5134              :                   << " myLaneBidi=" << Named::getIDSecure(myLane->getBidiLane())
    5135              :                   << " further=" << toString(myFurtherLanes)
    5136              :                   << " furtherPosLat=" << toString(myFurtherLanesPosLat)
    5137              :                   << "\n     shadowLane=" << Named::getIDSecure(myLaneChangeModel->getShadowLane())
    5138              :                   << " shadowFurther=" << toString(myLaneChangeModel->getShadowFurtherLanes())
    5139              :                   << " shadowFurtherPosLat=" << toString(myLaneChangeModel->getShadowFurtherLanesPosLat())
    5140              :                   << "\n     targetLane=" << Named::getIDSecure(myLaneChangeModel->getTargetLane())
    5141              :                   << " furtherTargets=" << toString(myLaneChangeModel->getFurtherTargetLanes())
    5142              :                   << std::endl;
    5143              :     }
    5144              : #endif
    5145  34742102187 :     if (lane == myLane
    5146   8364956111 :             || lane == myLaneChangeModel->getShadowLane()
    5147  39575564742 :             || lane == myLaneChangeModel->getTargetLane()) {
    5148  29910223719 :         if (myLaneChangeModel->isOpposite()) {
    5149    231227727 :             if (lane == myLaneChangeModel->getShadowLane()) {
    5150    199150480 :                 return lane->getLength() - myState.myPos - myType->getLength();
    5151              :             } else {
    5152     36870724 :                 return myState.myPos + (calledByGetPosition ? -1 : 1) * myType->getLength();
    5153              :             }
    5154  29678995992 :         } else if (&lane->getEdge() != &myLane->getEdge()) {
    5155     20781991 :             return lane->getLength() - myState.myPos + (calledByGetPosition ? -1 : 1) * myType->getLength();
    5156              :         } else {
    5157              :             // account for parallel lanes of different lengths in the most conservative manner (i.e. while turning)
    5158  59317134682 :             return myState.myPos - myType->getLength() + MIN2(0.0, lane->getLength() - myLane->getLength());
    5159              :         }
    5160   4831878468 :     } else if (lane == myLane->getBidiLane()) {
    5161     42181345 :         return lane->getLength() - myState.myPos + myType->getLength() * (calledByGetPosition ? -1 : 1);
    5162   4797541219 :     } else if (myFurtherLanes.size() > 0 && lane == myFurtherLanes.back()) {
    5163   4746862923 :         return myState.myBackPos;
    5164     50678296 :     } else if ((myLaneChangeModel->getShadowFurtherLanes().size() > 0 && lane == myLaneChangeModel->getShadowFurtherLanes().back())
    5165     51133019 :                || (myLaneChangeModel->getFurtherTargetLanes().size() > 0 && lane == myLaneChangeModel->getFurtherTargetLanes().back())) {
    5166              :         assert(myFurtherLanes.size() > 0);
    5167     21757321 :         if (lane->getLength() == myFurtherLanes.back()->getLength()) {
    5168     21099230 :             return myState.myBackPos;
    5169              :         } else {
    5170              :             // interpolate
    5171              :             //if (DEBUG_COND) {
    5172              :             //if (myFurtherLanes.back()->getLength() != lane->getLength()) {
    5173              :             //    std::cout << SIMTIME << " veh=" << getID() << " lane=" << lane->getID() << " further=" << myFurtherLanes.back()->getID()
    5174              :             //        << " len=" << lane->getLength() << " fLen=" << myFurtherLanes.back()->getLength()
    5175              :             //        << " backPos=" << myState.myBackPos << " result=" << myState.myBackPos / myFurtherLanes.back()->getLength() * lane->getLength() << "\n";
    5176              :             //}
    5177       658091 :             return myState.myBackPos / myFurtherLanes.back()->getLength() * lane->getLength();
    5178              :         }
    5179              :     } else {
    5180              :         //if (DEBUG_COND) std::cout << SIMTIME << " veh=" << getID() << " myFurtherLanes=" << toString(myFurtherLanes) << "\n";
    5181     28920975 :         double leftLength = myType->getLength() - myState.myPos;
    5182              : 
    5183              :         std::vector<MSLane*>::const_iterator i = myFurtherLanes.begin();
    5184     30930520 :         while (leftLength > 0 && i != myFurtherLanes.end()) {
    5185     30878259 :             leftLength -= (*i)->getLength();
    5186              :             //if (DEBUG_COND) std::cout << " comparing i=" << (*i)->getID() << " lane=" << lane->getID() << "\n";
    5187     30878259 :             if (*i == lane) {
    5188     27827721 :                 return -leftLength;
    5189      3050538 :             } else if (*i == lane->getBidiLane()) {
    5190      1040993 :                 return lane->getLength() + leftLength - (calledByGetPosition ? 2 * myType->getLength() : 0);
    5191              :             }
    5192              :             ++i;
    5193              :         }
    5194              :         //if (DEBUG_COND) std::cout << SIMTIME << " veh=" << getID() << " myShadowFurtherLanes=" << toString(myLaneChangeModel->getShadowFurtherLanes()) << "\n";
    5195        52261 :         leftLength = myType->getLength() - myState.myPos;
    5196        52261 :         i = myLaneChangeModel->getShadowFurtherLanes().begin();
    5197        52261 :         while (leftLength > 0 && i != myLaneChangeModel->getShadowFurtherLanes().end()) {
    5198        52258 :             leftLength -= (*i)->getLength();
    5199              :             //if (DEBUG_COND) std::cout << " comparing i=" << (*i)->getID() << " lane=" << lane->getID() << "\n";
    5200        52258 :             if (*i == lane) {
    5201        23842 :                 return -leftLength;
    5202        28416 :             } else if (*i == lane->getBidiLane()) {
    5203        28416 :                 return lane->getLength() + leftLength - (calledByGetPosition ? 2 * myType->getLength() : 0);
    5204              :             }
    5205              :             ++i;
    5206              :         }
    5207              :         //if (DEBUG_COND) std::cout << SIMTIME << " veh=" << getID() << " myFurtherTargetLanes=" << toString(myLaneChangeModel->getFurtherTargetLanes()) << "\n";
    5208            3 :         leftLength = myType->getLength() - myState.myPos;
    5209              :         i = getFurtherLanes().begin();
    5210            3 :         const std::vector<MSLane*> furtherTargetLanes = myLaneChangeModel->getFurtherTargetLanes();
    5211              :         auto j = furtherTargetLanes.begin();
    5212            3 :         while (leftLength > 0 && j != furtherTargetLanes.end()) {
    5213            1 :             leftLength -= (*i)->getLength();
    5214              :             // if (DEBUG_COND) std::cout << " comparing i=" << (*i)->getID() << " lane=" << lane->getID() << "\n";
    5215            1 :             if (*j == lane) {
    5216            1 :                 return -leftLength;
    5217            0 :             } else if (*j == lane->getBidiLane()) {
    5218            0 :                 return lane->getLength() + leftLength - (calledByGetPosition ? 2 * myType->getLength() : 0);
    5219              :             }
    5220              :             ++i;
    5221              :             ++j;
    5222              :         }
    5223            8 :         WRITE_WARNINGF("Request backPos of vehicle '%' for invalid lane '%' time=%.",
    5224              :                 getID(), Named::getIDSecure(lane), time2string(SIMSTEP))
    5225              :         SOFT_ASSERT(false);
    5226            2 :         return  myState.myBackPos;
    5227            3 :     }
    5228              : }
    5229              : 
    5230              : 
    5231              : double
    5232  28271549514 : MSVehicle::getPositionOnLane(const MSLane* lane) const {
    5233  28271549514 :     return getBackPositionOnLane(lane, true) + myType->getLength();
    5234              : }
    5235              : 
    5236              : 
    5237              : bool
    5238    419514049 : MSVehicle::isFrontOnLane(const MSLane* lane) const {
    5239    419514049 :     return lane == myLane || lane == myLaneChangeModel->getShadowLane() || lane == myLane->getBidiLane();
    5240              : }
    5241              : 
    5242              : 
    5243              : void
    5244    632683106 : MSVehicle::checkRewindLinkLanes(const double lengthsInFront, DriveItemVector& lfLinks) const {
    5245    632683106 :     if (MSGlobals::gUsingInternalLanes && !myLane->getEdge().isRoundabout() && !myLaneChangeModel->isOpposite()) {
    5246    629485839 :         double seenSpace = -lengthsInFront;
    5247              : #ifdef DEBUG_CHECKREWINDLINKLANES
    5248              :         if (DEBUG_COND) {
    5249              :             std::cout << "\nCHECK_REWIND_LINKLANES\n" << " veh=" << getID() << " lengthsInFront=" << lengthsInFront << "\n";
    5250              :         };
    5251              : #endif
    5252    629485839 :         bool foundStopped = false;
    5253              :         // compute available space until a stopped vehicle is found
    5254              :         // this is the sum of non-interal lane length minus in-between vehicle lengths
    5255   1838763895 :         for (int i = 0; i < (int)lfLinks.size(); ++i) {
    5256              :             // skip unset links
    5257   1209278056 :             DriveProcessItem& item = lfLinks[i];
    5258              : #ifdef DEBUG_CHECKREWINDLINKLANES
    5259              :             if (DEBUG_COND) std::cout << SIMTIME
    5260              :                                           << " link=" << (item.myLink == 0 ? "NULL" : item.myLink->getViaLaneOrLane()->getID())
    5261              :                                           << " foundStopped=" << foundStopped;
    5262              : #endif
    5263   1209278056 :             if (item.myLink == nullptr || foundStopped) {
    5264    396757466 :                 if (!foundStopped) {
    5265    344409014 :                     item.availableSpace += seenSpace;
    5266              :                 } else {
    5267     52348452 :                     item.availableSpace = seenSpace;
    5268              :                 }
    5269              : #ifdef DEBUG_CHECKREWINDLINKLANES
    5270              :                 if (DEBUG_COND) {
    5271              :                     std::cout << " avail=" << item.availableSpace << "\n";
    5272              :                 }
    5273              : #endif
    5274    396757466 :                 continue;
    5275              :             }
    5276              :             // get the next lane, determine whether it is an internal lane
    5277              :             const MSLane* approachedLane = item.myLink->getViaLane();
    5278    812520590 :             if (approachedLane != nullptr) {
    5279    443509087 :                 if (keepClear(item.myLink)) {
    5280    141901403 :                     seenSpace = seenSpace - approachedLane->getBruttoVehLenSum();
    5281    141901403 :                     if (approachedLane == myLane) {
    5282        48432 :                         seenSpace += getVehicleType().getLengthWithGap();
    5283              :                     }
    5284              :                 } else {
    5285    301607684 :                     seenSpace = seenSpace + approachedLane->getSpaceTillLastStanding(this, foundStopped);// - approachedLane->getBruttoVehLenSum() + approachedLane->getLength();
    5286              :                 }
    5287    443509087 :                 item.availableSpace = seenSpace;
    5288              : #ifdef DEBUG_CHECKREWINDLINKLANES
    5289              :                 if (DEBUG_COND) std::cout
    5290              :                             << " approached=" << approachedLane->getID()
    5291              :                             << " approachedBrutto=" << approachedLane->getBruttoVehLenSum()
    5292              :                             << " avail=" << item.availableSpace
    5293              :                             << " seenSpace=" << seenSpace
    5294              :                             << " hadStoppedVehicle=" << item.hadStoppedVehicle
    5295              :                             << " lengthsInFront=" << lengthsInFront
    5296              :                             << "\n";
    5297              : #endif
    5298    443509087 :                 continue;
    5299              :             }
    5300              :             approachedLane = item.myLink->getLane();
    5301    369011503 :             const MSVehicle* last = approachedLane->getLastAnyVehicle();
    5302    369011503 :             if (last == nullptr || last == this) {
    5303     61300079 :                 if (approachedLane->getLength() > getVehicleType().getLength()
    5304     61300079 :                         || keepClear(item.myLink)) {
    5305     59002767 :                     seenSpace += approachedLane->getLength();
    5306              :                 }
    5307     61300079 :                 item.availableSpace = seenSpace;
    5308              : #ifdef DEBUG_CHECKREWINDLINKLANES
    5309              :                 if (DEBUG_COND) {
    5310              :                     std::cout << " last=" << Named::getIDSecure(last) << " laneLength=" << approachedLane->getLength() << " avail=" << item.availableSpace << "\n";
    5311              :                 }
    5312              : #endif
    5313              :             } else {
    5314    307711424 :                 bool foundStopped2 = false;
    5315    307711424 :                 double spaceTillLastStanding = approachedLane->getSpaceTillLastStanding(this, foundStopped2);
    5316    307711424 :                 if (approachedLane->getBidiLane() != nullptr) {
    5317       491663 :                     const MSVehicle* oncomingVeh = approachedLane->getBidiLane()->getFirstFullVehicle();
    5318       491663 :                     if (oncomingVeh) {
    5319       334980 :                         const double oncomingGap = approachedLane->getLength() - oncomingVeh->getPositionOnLane();
    5320       334980 :                         const double oncomingBGap = oncomingVeh->getBrakeGap(true);
    5321              :                         // oncoming movement until ego enters the junction
    5322       334980 :                         const double oncomingMove = STEPS2TIME(item.myArrivalTime - SIMSTEP) * oncomingVeh->getSpeed();
    5323       334980 :                         const double spaceTillOncoming = oncomingGap - oncomingBGap - oncomingMove;
    5324              :                         spaceTillLastStanding = MIN2(spaceTillLastStanding, spaceTillOncoming);
    5325       334980 :                         if (spaceTillOncoming <= getVehicleType().getLengthWithGap()) {
    5326        27612 :                             foundStopped = true;
    5327              :                         }
    5328              : #ifdef DEBUG_CHECKREWINDLINKLANES
    5329              :                         if (DEBUG_COND) {
    5330              :                             std::cout << " oVeh=" << oncomingVeh->getID()
    5331              :                                       << " oGap=" << oncomingGap
    5332              :                                       << " bGap=" << oncomingBGap
    5333              :                                       << " mGap=" << oncomingMove
    5334              :                                       << " sto=" << spaceTillOncoming;
    5335              :                         }
    5336              : #endif
    5337              :                     }
    5338              :                 }
    5339    307711424 :                 seenSpace += spaceTillLastStanding;
    5340    307711424 :                 if (foundStopped2) {
    5341     21102517 :                     foundStopped = true;
    5342     21102517 :                     item.hadStoppedVehicle = true;
    5343              :                 }
    5344    307711424 :                 item.availableSpace = seenSpace;
    5345    307711424 :                 if (last->myHaveToWaitOnNextLink || last->isStopped()) {
    5346     30609365 :                     foundStopped = true;
    5347     30609365 :                     item.hadStoppedVehicle = true;
    5348              :                 }
    5349              : #ifdef DEBUG_CHECKREWINDLINKLANES
    5350              :                 if (DEBUG_COND) std::cout
    5351              :                             << " approached=" << approachedLane->getID()
    5352              :                             << " last=" << last->getID()
    5353              :                             << " lastHasToWait=" << last->myHaveToWaitOnNextLink
    5354              :                             << " lastBrakeLight=" << last->signalSet(VEH_SIGNAL_BRAKELIGHT)
    5355              :                             << " lastBrakeGap=" << last->getCarFollowModel().brakeGap(last->getSpeed())
    5356              :                             << " lastGap=" << (last->getBackPositionOnLane(approachedLane) + last->getCarFollowModel().brakeGap(last->getSpeed()) - last->getSpeed() * last->getCarFollowModel().getHeadwayTime()
    5357              :                                                // gap of last up to the next intersection
    5358              :                                                - last->getVehicleType().getMinGap())
    5359              :                             << " stls=" << spaceTillLastStanding
    5360              :                             << " avail=" << item.availableSpace
    5361              :                             << " seenSpace=" << seenSpace
    5362              :                             << " foundStopped=" << foundStopped
    5363              :                             << " foundStopped2=" << foundStopped2
    5364              :                             << "\n";
    5365              : #endif
    5366              :             }
    5367              :         }
    5368              : 
    5369              :         // check which links allow continuation and add pass available to the previous item
    5370   1209278056 :         for (int i = ((int)lfLinks.size() - 1); i > 0; --i) {
    5371    579792217 :             DriveProcessItem& item = lfLinks[i - 1];
    5372    579792217 :             DriveProcessItem& nextItem = lfLinks[i];
    5373    579792217 :             const bool canLeaveJunction = item.myLink->getViaLane() == nullptr || nextItem.myLink == nullptr || nextItem.mySetRequest;
    5374              :             const bool opened = (item.myLink != nullptr
    5375    579792217 :                                  && (canLeaveJunction || (
    5376              :                                          // indirect bicycle turn
    5377     28604886 :                                          nextItem.myLink != nullptr && nextItem.myLink->isInternalJunctionLink() && nextItem.myLink->haveRed()))
    5378    551201393 :                                  && (
    5379    551201393 :                                      item.myLink->havePriority()
    5380     27905133 :                                      || i == 1 // the upcoming link (item 0) is checked in executeMove anyway. No need to use outdata approachData here
    5381      5419014 :                                      || (myInfluencer != nullptr && !myInfluencer->getRespectJunctionPriority())
    5382      5389582 :                                      || item.myLink->opened(item.myArrivalTime, item.myArrivalSpeed,
    5383      5389582 :                                              item.getLeaveSpeed(), getVehicleType().getLength(),
    5384      5389582 :                                              getImpatience(), getCarFollowModel().getMaxDecel(), getWaitingTime(), getLateralPositionOnLane(), nullptr, false, this)));
    5385    579792217 :             bool allowsContinuation = (item.myLink == nullptr || item.myLink->isCont() || opened) && !item.hadStoppedVehicle;
    5386              : #ifdef DEBUG_CHECKREWINDLINKLANES
    5387              :             if (DEBUG_COND) std::cout
    5388              :                         << "   link=" << (item.myLink == 0 ? "NULL" : item.myLink->getViaLaneOrLane()->getID())
    5389              :                         << " canLeave=" << canLeaveJunction
    5390              :                         << " opened=" << opened
    5391              :                         << " allowsContinuation=" << allowsContinuation
    5392              :                         << " foundStopped=" << foundStopped
    5393              :                         << "\n";
    5394              : #endif
    5395    579792217 :             if (!opened && item.myLink != nullptr) {
    5396     29327846 :                 foundStopped = true;
    5397     29327846 :                 if (i > 1) {
    5398      4893485 :                     DriveProcessItem& item2 = lfLinks[i - 2];
    5399      4893485 :                     if (item2.myLink != nullptr && item2.myLink->isCont()) {
    5400              :                         allowsContinuation = true;
    5401              :                     }
    5402              :                 }
    5403              :             }
    5404    576570553 :             if (allowsContinuation) {
    5405    520089552 :                 item.availableSpace = nextItem.availableSpace;
    5406              : #ifdef DEBUG_CHECKREWINDLINKLANES
    5407              :                 if (DEBUG_COND) std::cout
    5408              :                             << "   link=" << (item.myLink == nullptr ? "NULL" : item.myLink->getViaLaneOrLane()->getID())
    5409              :                             << " copy nextAvail=" << nextItem.availableSpace
    5410              :                             << "\n";
    5411              : #endif
    5412              :             }
    5413              :         }
    5414              : 
    5415              :         // find removalBegin
    5416              :         int removalBegin = -1;
    5417    759023907 :         for (int i = 0; foundStopped && i < (int)lfLinks.size() && removalBegin < 0; ++i) {
    5418              :             // skip unset links
    5419    129538068 :             const DriveProcessItem& item = lfLinks[i];
    5420    129538068 :             if (item.myLink == nullptr) {
    5421      6741767 :                 continue;
    5422              :             }
    5423              :             /*
    5424              :             double impatienceCorrection = MAX2(0., double(double(myWaitingTime)));
    5425              :             if (seenSpace<getVehicleType().getLengthWithGap()-impatienceCorrection/10.&&nextSeenNonInternal!=0) {
    5426              :                 removalBegin = lastLinkToInternal;
    5427              :             }
    5428              :             */
    5429              : 
    5430    122796301 :             const double leftSpace = item.availableSpace - getVehicleType().getLengthWithGap();
    5431              : #ifdef DEBUG_CHECKREWINDLINKLANES
    5432              :             if (DEBUG_COND) std::cout
    5433              :                         << SIMTIME
    5434              :                         << " veh=" << getID()
    5435              :                         << " link=" << (item.myLink == 0 ? "NULL" : item.myLink->getViaLaneOrLane()->getID())
    5436              :                         << " avail=" << item.availableSpace
    5437              :                         << " leftSpace=" << leftSpace
    5438              :                         << "\n";
    5439              : #endif
    5440    122796301 :             if (leftSpace < 0/* && item.myLink->willHaveBlockedFoe()*/) {
    5441              :                 double impatienceCorrection = 0;
    5442              :                 /*
    5443              :                 if(item.myLink->getState()==LINKSTATE_MINOR) {
    5444              :                     impatienceCorrection = MAX2(0., STEPS2TIME(myWaitingTime));
    5445              :                 }
    5446              :                 */
    5447              :                 // may ignore keepClear rules
    5448     77650112 :                 if (leftSpace < -impatienceCorrection / 10. && keepClear(item.myLink)) {
    5449              :                     removalBegin = i;
    5450              :                 }
    5451              :                 //removalBegin = i;
    5452              :             }
    5453              :         }
    5454              :         // abort requests
    5455    629485839 :         if (removalBegin != -1 && !(removalBegin == 0 && myLane->getEdge().isInternal())) {
    5456     31046927 :             const double brakeGap = getCarFollowModel().brakeGap(myState.mySpeed, getCarFollowModel().getMaxDecel(), 0.);
    5457    106383688 :             while (removalBegin < (int)(lfLinks.size())) {
    5458     80304155 :                 DriveProcessItem& dpi = lfLinks[removalBegin];
    5459     80304155 :                 if (dpi.myLink == nullptr) {
    5460              :                     break;
    5461              :                 }
    5462     75336761 :                 dpi.myVLinkPass = dpi.myVLinkWait;
    5463              : #ifdef DEBUG_CHECKREWINDLINKLANES
    5464              :                 if (DEBUG_COND) {
    5465              :                     std::cout << " removalBegin=" << removalBegin << " brakeGap=" << brakeGap << " dist=" << dpi.myDistance << " speed=" << myState.mySpeed << " a2s=" << ACCEL2SPEED(getCarFollowModel().getMaxDecel()) << "\n";
    5466              :                 }
    5467              : #endif
    5468     75336761 :                 if (dpi.myDistance >= brakeGap + POSITION_EPS) {
    5469              :                     // always leave junctions after requesting to enter
    5470     75327541 :                     if (!dpi.myLink->isExitLink() || !lfLinks[removalBegin - 1].mySetRequest) {
    5471     75319192 :                         dpi.mySetRequest = false;
    5472              :                     }
    5473              :                 }
    5474     75336761 :                 ++removalBegin;
    5475              :             }
    5476              :         }
    5477              :     }
    5478    632683106 : }
    5479              : 
    5480              : 
    5481              : void
    5482    704245042 : MSVehicle::setApproachingForAllLinks() {
    5483    704245042 :     if (!myActionStep) {
    5484              :         return;
    5485              :     }
    5486    632683106 :     removeApproachingInformation(myLFLinkLanesPrev);
    5487   1849786410 :     for (DriveProcessItem& dpi : myLFLinkLanes) {
    5488   1217103304 :         if (dpi.myLink != nullptr) {
    5489    864331447 :             if (dpi.myLink->getState() == LINKSTATE_ALLWAY_STOP) {
    5490      2850520 :                 dpi.myArrivalTime += (SUMOTime)RandHelper::rand((int)2, getRNG()); // tie braker
    5491              :             }
    5492    864331447 :             dpi.myLink->setApproaching(this, dpi.myArrivalTime, dpi.myArrivalSpeed, dpi.getLeaveSpeed(),
    5493    864331447 :                                        dpi.mySetRequest, dpi.myArrivalSpeedBraking, getWaitingTimeFor(dpi.myLink), dpi.myDistance, getLateralPositionOnLane());
    5494              :         }
    5495              :     }
    5496    632683106 :     if (isRail()) {
    5497      8206561 :         for (DriveProcessItem& dpi : myLFLinkLanes) {
    5498      6823770 :             if (dpi.myLink != nullptr && dpi.myLink->getTLLogic() != nullptr && dpi.myLink->getTLLogic()->getLogicType() == TrafficLightType::RAIL_SIGNAL) {
    5499       693214 :                 MSRailSignalControl::getInstance().notifyApproach(dpi.myLink);
    5500              :             }
    5501              :         }
    5502              :     }
    5503    632683106 :     if (myLaneChangeModel->getShadowLane() != nullptr) {
    5504              :         // register on all shadow links
    5505      7597868 :         for (const DriveProcessItem& dpi : myLFLinkLanes) {
    5506      5038487 :             if (dpi.myLink != nullptr) {
    5507      3444176 :                 MSLink* parallelLink = dpi.myLink->getParallelLink(myLaneChangeModel->getShadowDirection());
    5508      3444176 :                 if (parallelLink == nullptr && getLaneChangeModel().isOpposite() && dpi.myLink->isEntryLink()) {
    5509              :                     // register on opposite direction entry link to warn foes at minor side road
    5510       169638 :                     parallelLink = dpi.myLink->getOppositeDirectionLink();
    5511              :                 }
    5512      3444176 :                 if (parallelLink != nullptr) {
    5513      2450135 :                     const double latOffset = getLane()->getRightSideOnEdge() - myLaneChangeModel->getShadowLane()->getRightSideOnEdge();
    5514      2450135 :                     parallelLink->setApproaching(this, dpi.myArrivalTime, dpi.myArrivalSpeed, dpi.getLeaveSpeed(),
    5515      2450135 :                                                  dpi.mySetRequest, dpi.myArrivalSpeedBraking, getWaitingTimeFor(dpi.myLink), dpi.myDistance,
    5516              :                                                  latOffset);
    5517      2450135 :                     myLaneChangeModel->setShadowApproachingInformation(parallelLink);
    5518              :                 }
    5519              :             }
    5520              :         }
    5521              :     }
    5522              : #ifdef DEBUG_PLAN_MOVE
    5523              :     if (DEBUG_COND) {
    5524              :         std::cout << SIMTIME
    5525              :                   << " veh=" << getID()
    5526              :                   << " after checkRewindLinkLanes\n";
    5527              :         for (DriveProcessItem& dpi : myLFLinkLanes) {
    5528              :             std::cout
    5529              :                     << " vPass=" << dpi.myVLinkPass
    5530              :                     << " vWait=" << dpi.myVLinkWait
    5531              :                     << " linkLane=" << (dpi.myLink == 0 ? "NULL" : dpi.myLink->getViaLaneOrLane()->getID())
    5532              :                     << " request=" << dpi.mySetRequest
    5533              :                     << " atime=" << dpi.myArrivalTime
    5534              :                     << "\n";
    5535              :         }
    5536              :     }
    5537              : #endif
    5538              : }
    5539              : 
    5540              : 
    5541              : void
    5542         1853 : MSVehicle::registerInsertionApproach(MSLink* link, double dist) {
    5543              :     DriveProcessItem dpi(0, dist);
    5544         1853 :     dpi.myLink = link;
    5545         1853 :     const double arrivalSpeedBraking = getCarFollowModel().getMinimalArrivalSpeedEuler(dist, getSpeed());
    5546         1853 :     link->setApproaching(this, SUMOTime_MAX, 0, 0, false, arrivalSpeedBraking, 0, dpi.myDistance, 0);
    5547              :     // ensure cleanup in the next step
    5548         1853 :     myLFLinkLanes.push_back(dpi);
    5549         1853 :     MSRailSignalControl::getInstance().notifyApproach(link);
    5550         1853 : }
    5551              : 
    5552              : 
    5553              : void
    5554     19512384 : MSVehicle::enterLaneAtMove(MSLane* enteredLane, bool onTeleporting) {
    5555     19512384 :     myAmOnNet = !onTeleporting;
    5556              :     // vaporizing edge?
    5557              :     /*
    5558              :     if (enteredLane->getEdge().isVaporizing()) {
    5559              :         // yep, let's do the vaporization...
    5560              :         myLane = enteredLane;
    5561              :         return true;
    5562              :     }
    5563              :     */
    5564              :     // Adjust MoveReminder offset to the next lane
    5565     19512384 :     adaptLaneEntering2MoveReminder(*enteredLane);
    5566              :     // set the entered lane as the current lane
    5567     19512384 :     MSLane* oldLane = myLane;
    5568     19512384 :     myLane = enteredLane;
    5569     19512384 :     myLastBestLanesEdge = nullptr;
    5570              : 
    5571              :     // internal edges are not a part of the route...
    5572     19512384 :     if (!enteredLane->getEdge().isInternal()) {
    5573              :         ++myCurrEdge;
    5574              :         assert(myLaneChangeModel->isOpposite() || haveValidStopEdges());
    5575              :     }
    5576     19512384 :     if (myInfluencer != nullptr) {
    5577         8976 :         myInfluencer->adaptLaneTimeLine(myLane->getIndex() - oldLane->getIndex());
    5578              :     }
    5579     19512384 :     if (!onTeleporting) {
    5580     19493948 :         activateReminders(MSMoveReminder::NOTIFICATION_JUNCTION, enteredLane);
    5581     19493948 :         if (MSGlobals::gLateralResolution > 0) {
    5582      3825647 :             myFurtherLanesPosLat.push_back(myState.myPosLat);
    5583              :             // transform lateral position when the lane width changes
    5584              :             assert(oldLane != nullptr);
    5585      3825647 :             const MSLink* const link = oldLane->getLinkTo(myLane);
    5586      3825647 :             if (link != nullptr) {
    5587      3825606 :                 myState.myPosLat += link->getLateralShift();
    5588              :             } else {
    5589           41 :                 myState.myPosLat += (oldLane->getCenterOnEdge() - myLane->getCanonicalPredecessorLane()->getRightSideOnEdge()) / 2;
    5590              :             }
    5591     15668301 :         } else if (fabs(myState.myPosLat) > NUMERICAL_EPS) {
    5592       243611 :             const double overlap = MAX2(0.0, getLateralOverlap(myState.myPosLat, oldLane));
    5593       243611 :             const double range = (oldLane->getWidth() - getVehicleType().getWidth()) * 0.5 + overlap;
    5594       243611 :             const double range2 = (myLane->getWidth() - getVehicleType().getWidth()) * 0.5 + overlap;
    5595       243611 :             myState.myPosLat *= range2 / range;
    5596              :         }
    5597     19493948 :         if (myLane->getBidiLane() != nullptr && (!isRailway(getVClass()) || (myLane->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    5598              :             // railways don't need to "see" each other when moving in opposite directions on the same track (efficiency)
    5599              :             // (unless the lane is shared with cars)
    5600        27038 :             myLane->getBidiLane()->setPartialOccupation(this);
    5601              :         }
    5602              :     } else {
    5603              :         // normal move() isn't called so reset position here. must be done
    5604              :         // before calling reminders
    5605        18436 :         myState.myPos = 0;
    5606        18436 :         myCachedPosition = Position::INVALID;
    5607        18436 :         activateReminders(MSMoveReminder::NOTIFICATION_TELEPORT, enteredLane);
    5608              :     }
    5609              :     // update via
    5610     19512384 :     if (myParameter->via.size() > 0 &&  myLane->getEdge().getID() == myParameter->via.front()) {
    5611         7244 :         myParameter->via.erase(myParameter->via.begin());
    5612              :     }
    5613     19512384 : }
    5614              : 
    5615              : 
    5616              : void
    5617      1091712 : MSVehicle::enterLaneAtLaneChange(MSLane* enteredLane) {
    5618      1091712 :     myAmOnNet = true;
    5619      1091712 :     myLane = enteredLane;
    5620      1091712 :     myCachedPosition = Position::INVALID;
    5621              :     // need to update myCurrentLaneInBestLanes
    5622      1091712 :     updateBestLanes();
    5623              :     // switch to and activate the new lane's reminders
    5624              :     // keep OldLaneReminders
    5625      1290383 :     for (std::vector< MSMoveReminder* >::const_iterator rem = enteredLane->getMoveReminders().begin(); rem != enteredLane->getMoveReminders().end(); ++rem) {
    5626       198671 :         addReminder(*rem);
    5627              :     }
    5628      1091712 :     activateReminders(MSMoveReminder::NOTIFICATION_LANE_CHANGE, enteredLane);
    5629      1091712 :     MSLane* lane = myLane;
    5630      1091712 :     double leftLength = getVehicleType().getLength() - myState.myPos;
    5631              :     int deleteFurther = 0;
    5632              : #ifdef DEBUG_SETFURTHER
    5633              :     if (DEBUG_COND) {
    5634              :         std::cout << SIMTIME << " enterLaneAtLaneChange entered=" << Named::getIDSecure(enteredLane) << " oldFurther=" << toString(myFurtherLanes) << "\n";
    5635              :     }
    5636              : #endif
    5637      1091712 :     if (myLane->getBidiLane() != nullptr && (!isRailway(getVClass()) || (myLane->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    5638              :         // railways don't need to "see" each other when moving in opposite directions on the same track (efficiency)
    5639              :         // (unless the lane is shared with cars)
    5640        19829 :         myLane->getBidiLane()->setPartialOccupation(this);
    5641              :     }
    5642      1177791 :     for (int i = 0; i < (int)myFurtherLanes.size(); i++) {
    5643        86079 :         if (lane != nullptr) {
    5644        82925 :             lane = lane->getLogicalPredecessorLane(myFurtherLanes[i]->getEdge());
    5645              :         }
    5646              : #ifdef DEBUG_SETFURTHER
    5647              :         if (DEBUG_COND) {
    5648              :             std::cout << "  enterLaneAtLaneChange i=" << i << " lane=" << Named::getIDSecure(lane) << " leftLength=" << leftLength << "\n";
    5649              :         }
    5650              : #endif
    5651        86079 :         if (leftLength > 0) {
    5652        85510 :             if (lane != nullptr) {
    5653        34925 :                 myFurtherLanes[i]->resetPartialOccupation(this);
    5654        34925 :                 if (myFurtherLanes[i]->getBidiLane() != nullptr
    5655        34925 :                         && (!isRailway(getVClass()) || (myFurtherLanes[i]->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    5656           60 :                     myFurtherLanes[i]->getBidiLane()->resetPartialOccupation(this);
    5657              :                 }
    5658              :                 // lane changing onto longer lanes may reduce the number of
    5659              :                 // remaining further lanes
    5660        34925 :                 myFurtherLanes[i] = lane;
    5661        34925 :                 myFurtherLanesPosLat[i] = myState.myPosLat;
    5662        34925 :                 leftLength -= lane->setPartialOccupation(this);
    5663        34925 :                 if (lane->getBidiLane() != nullptr
    5664        34925 :                         && (!isRailway(getVClass()) || (lane->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    5665         1916 :                     lane->getBidiLane()->setPartialOccupation(this);
    5666              :                 }
    5667        34925 :                 myState.myBackPos = -leftLength;
    5668              : #ifdef DEBUG_SETFURTHER
    5669              :                 if (DEBUG_COND) {
    5670              :                     std::cout << SIMTIME << "   newBackPos=" << myState.myBackPos << "\n";
    5671              :                 }
    5672              : #endif
    5673              :             } else {
    5674              :                 // keep the old values, but ensure there is no shadow
    5675        50585 :                 if (myLaneChangeModel->isChangingLanes()) {
    5676           15 :                     myLaneChangeModel->setNoShadowPartialOccupator(myFurtherLanes[i]);
    5677              :                 }
    5678        50585 :                 if (myState.myBackPos < 0) {
    5679           41 :                     myState.myBackPos += myFurtherLanes[i]->getLength();
    5680              :                 }
    5681              : #ifdef DEBUG_SETFURTHER
    5682              :                 if (DEBUG_COND) {
    5683              :                     std::cout << SIMTIME << "   i=" << i << " further=" << myFurtherLanes[i]->getID() << " newBackPos=" << myState.myBackPos << "\n";
    5684              :                 }
    5685              : #endif
    5686              :             }
    5687              :         } else {
    5688          569 :             myFurtherLanes[i]->resetPartialOccupation(this);
    5689          569 :             if (myFurtherLanes[i]->getBidiLane() != nullptr
    5690          569 :                     && (!isRailway(getVClass()) || (myFurtherLanes[i]->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    5691            0 :                 myFurtherLanes[i]->getBidiLane()->resetPartialOccupation(this);
    5692              :             }
    5693          569 :             deleteFurther++;
    5694              :         }
    5695              :     }
    5696      1091712 :     if (deleteFurther > 0) {
    5697              : #ifdef DEBUG_SETFURTHER
    5698              :         if (DEBUG_COND) {
    5699              :             std::cout << SIMTIME << " veh=" << getID() << " shortening myFurtherLanes by " << deleteFurther << "\n";
    5700              :         }
    5701              : #endif
    5702          551 :         myFurtherLanes.erase(myFurtherLanes.end() - deleteFurther, myFurtherLanes.end());
    5703          551 :         myFurtherLanesPosLat.erase(myFurtherLanesPosLat.end() - deleteFurther, myFurtherLanesPosLat.end());
    5704              :     }
    5705              : #ifdef DEBUG_SETFURTHER
    5706              :     if (DEBUG_COND) {
    5707              :         std::cout << SIMTIME << " enterLaneAtLaneChange new furtherLanes=" << toString(myFurtherLanes)
    5708              :                   << " furterLanesPosLat=" << toString(myFurtherLanesPosLat) << "\n";
    5709              :     }
    5710              : #endif
    5711      1091712 :     myAngle = computeAngle();
    5712      1091712 : }
    5713              : 
    5714              : 
    5715              : void
    5716      3540294 : MSVehicle::computeFurtherLanes(MSLane* enteredLane, double pos, bool collision) {
    5717              :     // build the list of lanes the vehicle is lapping into
    5718      3540294 :     if (!myLaneChangeModel->isOpposite()) {
    5719      3518009 :         double leftLength = myType->getLength() - pos;
    5720      3518009 :         MSLane* clane = enteredLane;
    5721      3518009 :         int routeIndex = getRoutePosition();
    5722      3622271 :         while (leftLength > 0) {
    5723       236416 :             if (routeIndex > 0 && clane->getEdge().isNormal()) {
    5724              :                 // get predecessor lane that corresponds to prior route
    5725         4567 :                 routeIndex--;
    5726         4567 :                 const MSEdge* fromRouteEdge = myRoute->getEdges()[routeIndex];
    5727              :                 MSLane* target = clane;
    5728         4567 :                 clane = nullptr;
    5729         6093 :                 for (auto ili : target->getIncomingLanes()) {
    5730         6086 :                     if (ili.lane->getEdge().getNormalBefore() == fromRouteEdge) {
    5731         4560 :                         clane = ili.lane;
    5732         4560 :                         break;
    5733              :                     }
    5734              :                 }
    5735              :             } else {
    5736       231849 :                 clane = clane->getLogicalPredecessorLane();
    5737              :             }
    5738       144601 :             if (clane == nullptr || clane == myLane || clane == myLane->getBidiLane()
    5739       381009 :                     || (clane->isInternal() && (
    5740       121394 :                             clane->getLinkCont()[0]->getDirection() == LinkDirection::TURN
    5741        81063 :                             || clane->getLinkCont()[0]->getDirection() == LinkDirection::TURN_LEFTHAND))) {
    5742              :                 break;
    5743              :             }
    5744       104262 :             if (!collision || std::find(myFurtherLanes.begin(), myFurtherLanes.end(), clane) == myFurtherLanes.end()) {
    5745       103809 :                 myFurtherLanes.push_back(clane);
    5746       103809 :                 myFurtherLanesPosLat.push_back(myState.myPosLat);
    5747       103809 :                 clane->setPartialOccupation(this);
    5748       103809 :                 if (clane->getBidiLane() != nullptr
    5749       103809 :                         && (!isRailway(getVClass()) || (clane->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    5750            5 :                     clane->getBidiLane()->setPartialOccupation(this);
    5751              :                 }
    5752              :             }
    5753       104262 :             leftLength -= clane->getLength();
    5754              :         }
    5755      3518009 :         myState.myBackPos = -leftLength;
    5756              : #ifdef DEBUG_SETFURTHER
    5757              :         if (DEBUG_COND) {
    5758              :             std::cout << SIMTIME << " computeFurtherLanes veh=" << getID() << " pos=" << pos << " myFurtherLanes=" << toString(myFurtherLanes) << " backPos=" << myState.myBackPos << "\n";
    5759              :         }
    5760              : #endif
    5761              :     } else {
    5762              :         // clear partial occupation
    5763        22659 :         for (MSLane* further : myFurtherLanes) {
    5764              : #ifdef DEBUG_SETFURTHER
    5765              :             if (DEBUG_COND) {
    5766              :                 std::cout << SIMTIME << " opposite: resetPartialOccupation " << further->getID() << " \n";
    5767              :             }
    5768              : #endif
    5769          374 :             further->resetPartialOccupation(this);
    5770          374 :             if (further->getBidiLane() != nullptr
    5771          374 :                     && (!isRailway(getVClass()) || (further->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    5772            0 :                 further->getBidiLane()->resetPartialOccupation(this);
    5773              :             }
    5774              :         }
    5775              :         myFurtherLanes.clear();
    5776              :         myFurtherLanesPosLat.clear();
    5777              :     }
    5778      3540294 : }
    5779              : 
    5780              : 
    5781              : void
    5782      3539838 : MSVehicle::enterLaneAtInsertion(MSLane* enteredLane, double pos, double speed, double posLat, MSMoveReminder::Notification notification) {
    5783      3539838 :     myState = State(pos, speed, posLat, pos - getVehicleType().getLength(), hasDeparted() ? myState.myPreviousSpeed : speed);
    5784      3539838 :     if (myDeparture == NOT_YET_DEPARTED) {
    5785      3465446 :         onDepart();
    5786              :     }
    5787      3539838 :     myCachedPosition = Position::INVALID;
    5788              :     assert(myState.myPos >= 0);
    5789              :     assert(myState.mySpeed >= 0);
    5790      3539838 :     myLane = enteredLane;
    5791      3539838 :     myAmOnNet = true;
    5792              :     // schedule action for the next timestep
    5793      3539838 :     myLastActionTime = MSNet::getInstance()->getCurrentTimeStep() + DELTA_T;
    5794      3539838 :     if (notification != MSMoveReminder::NOTIFICATION_TELEPORT) {
    5795      3528008 :         if (notification == MSMoveReminder::NOTIFICATION_PARKING && myInfluencer != nullptr) {
    5796           12 :             drawOutsideNetwork(false);
    5797              :         }
    5798              :         // set and activate the new lane's reminders, teleports already did that at enterLaneAtMove
    5799      7492199 :         for (std::vector< MSMoveReminder* >::const_iterator rem = enteredLane->getMoveReminders().begin(); rem != enteredLane->getMoveReminders().end(); ++rem) {
    5800      3964191 :             addReminder(*rem);
    5801              :         }
    5802      3528008 :         activateReminders(notification, enteredLane);
    5803              :     } else {
    5804        11830 :         myLastBestLanesEdge = nullptr;
    5805        11830 :         myLastBestLanesInternalLane = nullptr;
    5806        11830 :         myLaneChangeModel->resetState();
    5807        12987 :         while (!myStops.empty() && myStops.front().edge == myCurrEdge && &myStops.front().lane->getEdge() == &myLane->getEdge()
    5808        12524 :                 && myStops.front().pars.endPos < pos) {
    5809            0 :             WRITE_WARNINGF(TL("Vehicle '%' skips stop on lane '%' time=%."), getID(), myStops.front().lane->getID(),
    5810              :                            time2string(MSNet::getInstance()->getCurrentTimeStep()));
    5811            0 :             cleanupParkingReservation();
    5812            0 :             myStops.pop_front();
    5813              :         }
    5814              :         // avoid startup-effects after teleport
    5815        11830 :         myTimeSinceStartup = getCarFollowModel().getStartupDelay() + DELTA_T;
    5816        11830 :         myStopSpeed = std::numeric_limits<double>::max();
    5817              :     }
    5818      3539838 :     computeFurtherLanes(enteredLane, pos);
    5819      3539838 :     if (MSGlobals::gLateralResolution > 0) {
    5820       520886 :         myLaneChangeModel->updateShadowLane();
    5821       520886 :         myLaneChangeModel->updateTargetLane();
    5822      3018952 :     } else if (MSGlobals::gLaneChangeDuration > 0) {
    5823        40414 :         myLaneChangeModel->updateShadowLane();
    5824              :     }
    5825      3539838 :     if (notification != MSMoveReminder::NOTIFICATION_LOAD_STATE) {
    5826      3538379 :         myAngle = computeAngle();
    5827      3538379 :         myRawAngle = myAngle;
    5828      3538379 :         if (myLaneChangeModel->isOpposite()) {
    5829        22285 :             myAngle += M_PI;
    5830              :         }
    5831              :     }
    5832      3539838 :     if (MSNet::getInstance()->hasPersons()) {
    5833        58425 :         for (MSLane* further : myFurtherLanes) {
    5834          876 :             if (further->mustCheckJunctionCollisions()) {
    5835            4 :                 MSNet::getInstance()->getEdgeControl().checkCollisionForInactive(further);
    5836              :             }
    5837              :         }
    5838              :     }
    5839      3539838 : }
    5840              : 
    5841              : 
    5842              : void
    5843     24021940 : MSVehicle::leaveLane(const MSMoveReminder::Notification reason, const MSLane* approachedLane) {
    5844     66427012 :     for (MoveReminderCont::iterator rem = myMoveReminders.begin(); rem != myMoveReminders.end();) {
    5845     42405072 :         if (rem->first->notifyLeave(*this, myState.myPos + rem->second, reason, approachedLane)) {
    5846              : #ifdef _DEBUG
    5847              :             if (myTraceMoveReminders) {
    5848              :                 traceMoveReminder("notifyLeave", rem->first, rem->second, true);
    5849              :             }
    5850              : #endif
    5851              :             ++rem;
    5852              :         } else {
    5853              : #ifdef _DEBUG
    5854              :             if (myTraceMoveReminders) {
    5855              :                 traceMoveReminder("notifyLeave", rem->first, rem->second, false);
    5856              :             }
    5857              : #endif
    5858              :             rem = myMoveReminders.erase(rem);
    5859              :         }
    5860              :     }
    5861     24021940 :     if ((reason == MSMoveReminder::NOTIFICATION_JUNCTION
    5862     24021940 :             || reason == MSMoveReminder::NOTIFICATION_TELEPORT
    5863      4515491 :             || reason == MSMoveReminder::NOTIFICATION_TELEPORT_CONTINUATION)
    5864     19512636 :             && myLane != nullptr) {
    5865     19512607 :         myOdometer += getLane()->getLength();
    5866              :     }
    5867     24021911 :     if (myLane != nullptr && myLane->getBidiLane() != nullptr && myAmOnNet
    5868     24094782 :             && (!isRailway(getVClass()) || (myLane->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    5869        48325 :         myLane->getBidiLane()->resetPartialOccupation(this);
    5870              :     }
    5871     24021940 :     if (reason != MSMoveReminder::NOTIFICATION_JUNCTION && reason != MSMoveReminder::NOTIFICATION_LANE_CHANGE) {
    5872              :         // @note. In case of lane change, myFurtherLanes and partial occupation
    5873              :         // are handled in enterLaneAtLaneChange()
    5874      3424874 :         for (MSLane* further : myFurtherLanes) {
    5875              : #ifdef DEBUG_FURTHER
    5876              :             if (DEBUG_COND) {
    5877              :                 std::cout << SIMTIME << " leaveLane \n";
    5878              :             }
    5879              : #endif
    5880        32964 :             further->resetPartialOccupation(this);
    5881        32964 :             if (further->getBidiLane() != nullptr
    5882        32964 :                     && (!isRailway(getVClass()) || (further->getPermissions() & ~SVC_RAIL_CLASSES) != 0)) {
    5883            4 :                 further->getBidiLane()->resetPartialOccupation(this);
    5884              :             }
    5885              :         }
    5886              :         myFurtherLanes.clear();
    5887              :         myFurtherLanesPosLat.clear();
    5888              :     }
    5889      3391910 :     if (reason >= MSMoveReminder::NOTIFICATION_TELEPORT) {
    5890      3391910 :         myAmOnNet = false;
    5891      3391910 :         myWaitingTime = 0;
    5892              :     }
    5893     24021940 :     if (reason != MSMoveReminder::NOTIFICATION_PARKING && resumeFromStopping()) {
    5894           18 :         myStopDist = std::numeric_limits<double>::max();
    5895           18 :         if (myPastStops.back().speed <= 0) {
    5896           54 :             WRITE_WARNINGF(TL("Vehicle '%' aborts stop."), getID());
    5897              :         }
    5898              :     }
    5899     24021940 :     if (reason != MSMoveReminder::NOTIFICATION_PARKING && reason != MSMoveReminder::NOTIFICATION_LANE_CHANGE) {
    5900     22870917 :         while (!myStops.empty() && myStops.front().edge == myCurrEdge && &myStops.front().lane->getEdge() == &myLane->getEdge()) {
    5901         1445 :             if (myStops.front().getSpeed() <= 0) {
    5902         3333 :                 WRITE_WARNINGF(TL("Vehicle '%' skips stop on lane '%' time=%."), getID(), myStops.front().lane->getID(),
    5903              :                                time2string(MSNet::getInstance()->getCurrentTimeStep()));
    5904         1111 :                 cleanupParkingReservation();
    5905         1111 :                 if (MSStopOut::active()) {
    5906              :                     // clean up if stopBlocked was called
    5907           17 :                     MSStopOut::getInstance()->stopNotStarted(this);
    5908              :                 }
    5909         1111 :                 myStops.pop_front();
    5910              :             } else {
    5911              :                 MSStop& stop = myStops.front();
    5912              :                 // passed waypoint at the end of the lane
    5913          334 :                 if (!stop.reached) {
    5914          334 :                     if (MSStopOut::active()) {
    5915           21 :                         MSStopOut::getInstance()->stopStarted(this, getPersonNumber(), getContainerNumber(), MSNet::getInstance()->getCurrentTimeStep());
    5916              :                     }
    5917          334 :                     stop.reached = true;
    5918              :                     // enter stopping place so leaveFrom works as expected
    5919          334 :                     if (stop.busstop != nullptr) {
    5920              :                         // let the bus stop know the vehicle
    5921           25 :                         stop.busstop->enter(this, stop.pars.parking == ParkingType::OFFROAD);
    5922              :                     }
    5923          334 :                     if (stop.containerstop != nullptr) {
    5924              :                         // let the container stop know the vehicle
    5925           13 :                         stop.containerstop->enter(this, stop.pars.parking == ParkingType::OFFROAD);
    5926              :                     }
    5927              :                     // do not enter parkingarea!
    5928          334 :                     if (stop.chargingStation != nullptr) {
    5929              :                         // let the container stop know the vehicle
    5930          122 :                         stop.chargingStation->enter(this, stop.pars.parking == ParkingType::OFFROAD);
    5931              :                     }
    5932              :                 }
    5933          334 :                 resumeFromStopping();
    5934              :             }
    5935         1445 :             myStopDist = std::numeric_limits<double>::max();
    5936              :         }
    5937              :     }
    5938     24021940 : }
    5939              : 
    5940              : 
    5941              : void
    5942        45579 : MSVehicle::leaveLaneBack(const MSMoveReminder::Notification reason, const MSLane* leftLane) {
    5943       185914 :     for (MoveReminderCont::iterator rem = myMoveReminders.begin(); rem != myMoveReminders.end();) {
    5944       140335 :         if (rem->first->notifyLeaveBack(*this, reason, leftLane)) {
    5945              : #ifdef _DEBUG
    5946              :             if (myTraceMoveReminders) {
    5947              :                 traceMoveReminder("notifyLeaveBack", rem->first, rem->second, true);
    5948              :             }
    5949              : #endif
    5950              :             ++rem;
    5951              :         } else {
    5952              : #ifdef _DEBUG
    5953              :             if (myTraceMoveReminders) {
    5954              :                 traceMoveReminder("notifyLeaveBack", rem->first, rem->second, false);
    5955              :             }
    5956              : #endif
    5957              :             rem = myMoveReminders.erase(rem);
    5958              :         }
    5959              :     }
    5960              : #ifdef DEBUG_MOVEREMINDERS
    5961              :     if (DEBUG_COND) {
    5962              :         std::cout << SIMTIME << " veh=" << getID() << " myReminders:";
    5963              :         for (auto rem : myMoveReminders) {
    5964              :             std::cout << rem.first->getDescription() << " ";
    5965              :         }
    5966              :         std::cout << "\n";
    5967              :     }
    5968              : #endif
    5969        45579 : }
    5970              : 
    5971              : 
    5972              : MSAbstractLaneChangeModel&
    5973  10433073385 : MSVehicle::getLaneChangeModel() {
    5974  10433073385 :     return *myLaneChangeModel;
    5975              : }
    5976              : 
    5977              : 
    5978              : const MSAbstractLaneChangeModel&
    5979   4939707440 : MSVehicle::getLaneChangeModel() const {
    5980   4939707440 :     return *myLaneChangeModel;
    5981              : }
    5982              : 
    5983              : bool
    5984       517896 : MSVehicle::isOppositeLane(const MSLane* lane) const {
    5985       517896 :     return (lane->isInternal()
    5986       517896 :             ? & (lane->getLinkCont()[0]->getLane()->getEdge()) != *(myCurrEdge + 1)
    5987       516150 :             : &lane->getEdge() != *myCurrEdge);
    5988              : }
    5989              : 
    5990              : const std::vector<MSVehicle::LaneQ>&
    5991   1346060140 : MSVehicle::getBestLanes() const {
    5992   1346060140 :     return *myBestLanes.begin();
    5993              : }
    5994              : 
    5995              : 
    5996              : void
    5997   1875689277 : MSVehicle::updateBestLanes(bool forceRebuild, const MSLane* startLane) {
    5998              : #ifdef DEBUG_BESTLANES
    5999              :     if (DEBUG_COND) {
    6000              :         std::cout << SIMTIME << " updateBestLanes veh=" << getID() << " force=" << forceRebuild << " startLane1=" << Named::getIDSecure(startLane) << " myLane=" << Named::getIDSecure(myLane) << "\n";
    6001              :     }
    6002              : #endif
    6003   1875689277 :     if (startLane == nullptr) {
    6004    992644786 :         startLane = myLane;
    6005              :     }
    6006              :     assert(startLane != 0);
    6007   1875689277 :     if (myLaneChangeModel->isOpposite()) {
    6008              :         // depending on the calling context, startLane might be the forward lane
    6009              :         // or the reverse-direction lane. In the latter case we need to
    6010              :         // transform it to the forward lane.
    6011       517896 :         if (isOppositeLane(startLane)) {
    6012              :             // use leftmost lane of forward edge
    6013       111097 :             startLane = startLane->getEdge().getOppositeEdge()->getLanes().back();
    6014              :             assert(startLane != 0);
    6015              : #ifdef DEBUG_BESTLANES
    6016              :             if (DEBUG_COND) {
    6017              :                 std::cout << "   startLaneIsOpposite newStartLane=" << startLane->getID() << "\n";
    6018              :             }
    6019              : #endif
    6020              :         }
    6021              :     }
    6022   1875689277 :     if (forceRebuild) {
    6023      1738173 :         myLastBestLanesEdge = nullptr;
    6024      1738173 :         myLastBestLanesInternalLane = nullptr;
    6025              :     }
    6026   1875689277 :     if (myBestLanes.size() > 0 && !forceRebuild && myLastBestLanesEdge == &startLane->getEdge()) {
    6027   1844805965 :         updateOccupancyAndCurrentBestLane(startLane);
    6028              : #ifdef DEBUG_BESTLANES
    6029              :         if (DEBUG_COND) {
    6030              :             std::cout << "  only updateOccupancyAndCurrentBestLane\n";
    6031              :         }
    6032              : #endif
    6033   1844805965 :         return;
    6034              :     }
    6035     30883312 :     if (startLane->getEdge().isInternal()) {
    6036     14787283 :         if (myBestLanes.size() == 0 || forceRebuild) {
    6037              :             // rebuilt from previous non-internal lane (may backtrack twice if behind an internal junction)
    6038         2217 :             updateBestLanes(true, startLane->getLogicalPredecessorLane());
    6039              :         }
    6040     14787283 :         if (myLastBestLanesInternalLane == startLane && !forceRebuild) {
    6041              : #ifdef DEBUG_BESTLANES
    6042              :             if (DEBUG_COND) {
    6043              :                 std::cout << "  nothing to do on internal\n";
    6044              :             }
    6045              : #endif
    6046              :             return;
    6047              :         }
    6048              :         // adapt best lanes to fit the current internal edge:
    6049              :         // keep the entries that are reachable from this edge
    6050      5267641 :         const MSEdge* nextEdge = startLane->getNextNormal();
    6051              :         assert(!nextEdge->isInternal());
    6052     10384038 :         for (std::vector<std::vector<LaneQ> >::iterator it = myBestLanes.begin(); it != myBestLanes.end();) {
    6053              :             std::vector<LaneQ>& lanes = *it;
    6054              :             assert(lanes.size() > 0);
    6055     10384038 :             if (&(lanes[0].lane->getEdge()) == nextEdge) {
    6056              :                 // keep those lanes which are successors of internal lanes from the edge of startLane
    6057      5267641 :                 std::vector<LaneQ> oldLanes = lanes;
    6058              :                 lanes.clear();
    6059              :                 const std::vector<MSLane*>& sourceLanes = startLane->getEdge().getLanes();
    6060     11921085 :                 for (std::vector<MSLane*>::const_iterator it_source = sourceLanes.begin(); it_source != sourceLanes.end(); ++it_source) {
    6061     11225615 :                     for (std::vector<LaneQ>::iterator it_lane = oldLanes.begin(); it_lane != oldLanes.end(); ++it_lane) {
    6062     11225615 :                         if ((*it_source)->getLinkCont()[0]->getLane() == (*it_lane).lane) {
    6063      6653444 :                             lanes.push_back(*it_lane);
    6064              :                             break;
    6065              :                         }
    6066              :                     }
    6067              :                 }
    6068              :                 assert(lanes.size() == startLane->getEdge().getLanes().size());
    6069              :                 // patch invalid bestLaneOffset and updated myCurrentLaneInBestLanes
    6070     11921085 :                 for (int i = 0; i < (int)lanes.size(); ++i) {
    6071      6653444 :                     if (i + lanes[i].bestLaneOffset < 0) {
    6072       105555 :                         lanes[i].bestLaneOffset = -i;
    6073              :                     }
    6074      6653444 :                     if (i + lanes[i].bestLaneOffset >= (int)lanes.size()) {
    6075        26844 :                         lanes[i].bestLaneOffset = (int)lanes.size() - i - 1;
    6076              :                     }
    6077              :                     assert(i + lanes[i].bestLaneOffset >= 0);
    6078              :                     assert(i + lanes[i].bestLaneOffset < (int)lanes.size());
    6079      6653444 :                     if (lanes[i].bestContinuations[0] != 0) {
    6080              :                         // patch length of bestContinuation to match expectations (only once)
    6081      6459839 :                         lanes[i].bestContinuations.insert(lanes[i].bestContinuations.begin(), (MSLane*)nullptr);
    6082              :                     }
    6083      6653444 :                     if (startLane->getLinkCont()[0]->getLane() == lanes[i].lane) {
    6084      5311590 :                         myCurrentLaneInBestLanes = lanes.begin() + i;
    6085              :                     }
    6086              :                     assert(&(lanes[i].lane->getEdge()) == nextEdge);
    6087              :                 }
    6088      5267641 :                 myLastBestLanesInternalLane = startLane;
    6089      5267641 :                 updateOccupancyAndCurrentBestLane(startLane);
    6090              : #ifdef DEBUG_BESTLANES
    6091              :                 if (DEBUG_COND) {
    6092              :                     std::cout << "  updated for internal\n";
    6093              :                 }
    6094              : #endif
    6095              :                 return;
    6096      5267641 :             } else {
    6097              :                 // remove passed edges
    6098      5116397 :                 it = myBestLanes.erase(it);
    6099              :             }
    6100              :         }
    6101              :         assert(false); // should always find the next edge
    6102              :     }
    6103              :     // start rebuilding
    6104     16096029 :     myLastBestLanesInternalLane = nullptr;
    6105     16096029 :     myLastBestLanesEdge = &startLane->getEdge();
    6106              :     myBestLanes.clear();
    6107              : 
    6108              :     // get information about the next stop
    6109     16096029 :     MSRouteIterator nextStopEdge = myRoute->end();
    6110              :     const MSLane* nextStopLane = nullptr;
    6111              :     double nextStopPos = 0;
    6112     16096029 :     if (!myStops.empty()) {
    6113              :         const MSStop& nextStop = myStops.front();
    6114       261713 :         nextStopLane = nextStop.lane;
    6115       261713 :         if (nextStop.isOpposite) {
    6116              :             // target leftmost lane in forward direction
    6117          340 :             nextStopLane = nextStopLane->getEdge().getOppositeEdge()->getLanes().back();
    6118              :         }
    6119       261713 :         nextStopEdge = nextStop.edge;
    6120       261713 :         nextStopPos = nextStop.pars.startPos;
    6121              :     }
    6122              :     // myArrivalTime = -1 in the context of validating departSpeed with departLane=best
    6123     16096029 :     if (myParameter->arrivalLaneProcedure >= ArrivalLaneDefinition::GIVEN && nextStopEdge == myRoute->end() && myArrivalLane >= 0) {
    6124       318440 :         nextStopEdge = (myRoute->end() - 1);
    6125       318440 :         nextStopLane = (*nextStopEdge)->getLanes()[myArrivalLane];
    6126       318440 :         nextStopPos = myArrivalPos;
    6127              :     }
    6128     16096029 :     if (nextStopEdge != myRoute->end()) {
    6129              :         // make sure that the "wrong" lanes get a penalty. (penalty needs to be
    6130              :         // large enough to overcome a magic threshold in MSLaneChangeModel::DK2004.cpp:383)
    6131       580153 :         nextStopPos = MAX2(POSITION_EPS, MIN2((double)nextStopPos, (double)(nextStopLane->getLength() - 2 * POSITION_EPS)));
    6132       580153 :         if (nextStopLane->isInternal()) {
    6133              :             // switch to the correct lane before entering the intersection
    6134          171 :             nextStopPos = (*nextStopEdge)->getLength();
    6135              :         }
    6136              :     }
    6137              : 
    6138              :     // go forward along the next lanes; always look past stops to ensure that we
    6139              :     // know where to go once the stop ends
    6140              :     // trains do not have to deal with lane-changing for stops but their best
    6141              :     // lanes lookahead is needed for rail signal control
    6142              :     int seen = 0;
    6143              :     double seenLength = 0;
    6144              :     bool progress = true;
    6145              :     // bestLanes must cover the braking distance even when at the very end of the current lane to avoid unecessary slow down
    6146     32192058 :     const double maxBrakeDist = startLane->getLength() + getCarFollowModel().getHeadwayTime() * getMaxSpeed() + getCarFollowModel().brakeGap(getMaxSpeed()) + getVehicleType().getMinGap();
    6147     16096029 :     const double lookahead = getLaneChangeModel().getStrategicLookahead();
    6148     81383515 :     for (MSRouteIterator ce = myCurrEdge; progress;) {
    6149              :         std::vector<LaneQ> currentLanes;
    6150              :         const std::vector<MSLane*>* allowed = nullptr;
    6151              :         const MSEdge* nextEdge = nullptr;
    6152     65287486 :         if (ce != myRoute->end() && ce + 1 != myRoute->end()) {
    6153     53315658 :             nextEdge = *(ce + 1);
    6154     53315658 :             allowed = (*ce)->allowedLanes(*nextEdge, myType->getVehicleClass());
    6155              :         }
    6156     65287486 :         const std::vector<MSLane*>& lanes = (*ce)->getLanes();
    6157    165001854 :         for (std::vector<MSLane*>::const_iterator i = lanes.begin(); i != lanes.end(); ++i) {
    6158              :             LaneQ q;
    6159     99714368 :             MSLane* cl = *i;
    6160     99714368 :             q.lane = cl;
    6161     99714368 :             q.bestContinuations.push_back(cl);
    6162     99714368 :             q.bestLaneOffset = 0;
    6163     99714368 :             q.length = cl->allowsVehicleClass(myType->getVehicleClass()) ? (*ce)->getLength() : 0;
    6164     99714368 :             q.currentLength = q.length;
    6165              :             // if all lanes are forbidden (i.e. due to a dynamic closing) we want to express no preference
    6166     99714368 :             q.allowsContinuation = allowed == nullptr || std::find(allowed->begin(), allowed->end(), cl) != allowed->end();
    6167     99714368 :             q.occupation = 0;
    6168     99714368 :             q.nextOccupation = 0;
    6169     99714368 :             currentLanes.push_back(q);
    6170              :         }
    6171              :         //
    6172              :         if (nextStopEdge == ce
    6173              :                 // already past the stop edge
    6174     65287486 :                 && !(ce == myCurrEdge && myLane != nullptr && myLane->isInternal())) {
    6175       573044 :             const MSLane* normalStopLane = nextStopLane->getNormalPredecessorLane();
    6176      1818016 :             for (std::vector<LaneQ>::iterator q = currentLanes.begin(); q != currentLanes.end(); ++q) {
    6177      1244972 :                 if (nextStopLane != nullptr && normalStopLane != (*q).lane) {
    6178       671928 :                     (*q).allowsContinuation = false;
    6179       671928 :                     (*q).length = nextStopPos;
    6180       671928 :                     (*q).currentLength = (*q).length;
    6181              :                 }
    6182              :             }
    6183              :         }
    6184              : 
    6185     65287486 :         myBestLanes.push_back(currentLanes);
    6186     65287486 :         ++seen;
    6187     65287486 :         seenLength += currentLanes[0].lane->getLength();
    6188              :         ++ce;
    6189     65287486 :         if (lookahead >= 0) {
    6190           45 :             progress &= (seen <= 2 || seenLength < lookahead); // custom (but we need to look at least one edge ahead)
    6191              :         } else {
    6192     86791035 :             progress &= (seen <= 4 || seenLength < MAX2(maxBrakeDist, 3000.0)); // motorway
    6193     70153957 :             progress &= (seen <= 8 || seenLength < MAX2(maxBrakeDist, 200.0) || isRailway(getVClass()));  // urban
    6194              :         }
    6195     65287486 :         progress &= ce != myRoute->end();
    6196              :         /*
    6197              :         if(progress) {
    6198              :           progress &= (currentLanes.size()!=1||(*ce)->getLanes().size()!=1);
    6199              :         }
    6200              :         */
    6201     65287486 :     }
    6202              : 
    6203              :     // we are examining the last lane explicitly
    6204     16096029 :     if (myBestLanes.size() != 0) {
    6205              :         double bestLength = -1;
    6206              :         // minimum and maximum lane index with best length
    6207              :         int bestThisIndex = 0;
    6208              :         int bestThisMaxIndex = 0;
    6209              :         int index = 0;
    6210              :         std::vector<LaneQ>& last = myBestLanes.back();
    6211     41861026 :         for (std::vector<LaneQ>::iterator j = last.begin(); j != last.end(); ++j, ++index) {
    6212     25764997 :             if ((*j).length > bestLength) {
    6213              :                 bestLength = (*j).length;
    6214              :                 bestThisIndex = index;
    6215              :                 bestThisMaxIndex = index;
    6216      6118873 :             } else if ((*j).length == bestLength) {
    6217              :                 bestThisMaxIndex = index;
    6218              :             }
    6219              :         }
    6220              :         index = 0;
    6221              :         bool requiredChangeRightForbidden = false;
    6222              :         int requireChangeToLeftForbidden = -1;
    6223     41861026 :         for (std::vector<LaneQ>::iterator j = last.begin(); j != last.end(); ++j, ++index) {
    6224     25764997 :             if ((*j).length < bestLength) {
    6225      3938900 :                 if (abs(bestThisIndex - index) < abs(bestThisMaxIndex - index)) {
    6226       145462 :                     (*j).bestLaneOffset = bestThisIndex - index;
    6227              :                 } else {
    6228      3793438 :                     (*j).bestLaneOffset = bestThisMaxIndex - index;
    6229              :                 }
    6230      3938900 :                 if (!(*j).allowsContinuation) {
    6231       543325 :                     if ((*j).bestLaneOffset < 0 && (!(*j).lane->allowsChangingRight(getVClass())
    6232       240244 :                                 || !(*j).lane->getParallelLane(-1, false)->allowsVehicleClass(getVClass())
    6233       237589 :                                 || requiredChangeRightForbidden)) {
    6234              :                         // this lane and all further lanes to the left cannot be used
    6235              :                         requiredChangeRightForbidden = true;
    6236         2655 :                         (*j).length = 0;
    6237       540670 :                     } else if ((*j).bestLaneOffset > 0 && (!(*j).lane->allowsChangingLeft(getVClass())
    6238       303055 :                                 || !(*j).lane->getParallelLane(1, false)->allowsVehicleClass(getVClass()))) {
    6239              :                         // this lane and all previous lanes to the right cannot be used
    6240         6426 :                         requireChangeToLeftForbidden = (*j).lane->getIndex();
    6241              :                     }
    6242              :                 }
    6243              :             }
    6244              :         }
    6245     16102465 :         for (int i = requireChangeToLeftForbidden; i >= 0; i--) {
    6246         6436 :             if (last[i].bestLaneOffset > 0) {
    6247         6436 :                 last[i].length = 0;
    6248              :             }
    6249              :         }
    6250              : #ifdef DEBUG_BESTLANES
    6251              :         if (DEBUG_COND) {
    6252              :             std::cout << "   last edge=" << last.front().lane->getEdge().getID() << " (bestIndex=" << bestThisIndex << " bestMaxIndex=" << bestThisMaxIndex << "):\n";
    6253              :             std::vector<LaneQ>& laneQs = myBestLanes.back();
    6254              :             for (std::vector<LaneQ>::iterator j = laneQs.begin(); j != laneQs.end(); ++j) {
    6255              :                 std::cout << "     lane=" << (*j).lane->getID() << " length=" << (*j).length << " bestOffset=" << (*j).bestLaneOffset << "\n";
    6256              :             }
    6257              :         }
    6258              : #endif
    6259              :     }
    6260              :     // go backward through the lanes
    6261              :     // track back best lane and compute the best prior lane(s)
    6262     65287486 :     for (std::vector<std::vector<LaneQ> >::reverse_iterator i = myBestLanes.rbegin() + 1; i != myBestLanes.rend(); ++i) {
    6263              :         std::vector<LaneQ>& nextLanes = (*(i - 1));
    6264              :         std::vector<LaneQ>& clanes = (*i);
    6265     49191457 :         MSEdge* const cE = &clanes[0].lane->getEdge();
    6266              :         int index = 0;
    6267              :         double bestConnectedLength = -1;
    6268              :         double bestLength = -1;
    6269    122452088 :         for (const LaneQ& j : nextLanes) {
    6270    146521262 :             if (j.lane->isApproachedFrom(cE) && bestConnectedLength < j.length) {
    6271              :                 bestConnectedLength = j.length;
    6272              :             }
    6273     73260631 :             if (bestLength < j.length) {
    6274              :                 bestLength = j.length;
    6275              :             }
    6276              :         }
    6277              :         // compute index of the best lane (highest length and least offset from the best next lane)
    6278              :         int bestThisIndex = 0;
    6279              :         int bestThisMaxIndex = 0;
    6280     49191457 :         if (bestConnectedLength > 0) {
    6281              :             index = 0;
    6282    123105293 :             for (LaneQ& j : clanes) {
    6283              :                 const LaneQ* bestConnectedNext = nullptr;
    6284     73926427 :                 if (j.allowsContinuation) {
    6285    175699173 :                     for (const LaneQ& m : nextLanes) {
    6286    121131734 :                         if ((m.lane->allowsVehicleClass(getVClass()) || m.lane->hadPermissionChanges())
    6287    111736633 :                                 && m.lane->isApproachedFrom(j.lane, getVClass())) {
    6288     66135789 :                             if (betterContinuation(bestConnectedNext, m)) {
    6289              :                                 bestConnectedNext = &m;
    6290              :                             }
    6291              :                         }
    6292              :                     }
    6293     64019832 :                     if (bestConnectedNext != nullptr) {
    6294     64019824 :                         if (bestConnectedNext->length == bestConnectedLength && abs(bestConnectedNext->bestLaneOffset) < 2) {
    6295     62288057 :                             j.length += bestLength;
    6296              :                         } else {
    6297      1731767 :                             j.length += bestConnectedNext->length;
    6298              :                         }
    6299     64019824 :                         j.bestLaneOffset = bestConnectedNext->bestLaneOffset;
    6300              :                     }
    6301              :                 }
    6302     64019824 :                 if (bestConnectedNext != nullptr && (bestConnectedNext->allowsContinuation || bestConnectedNext->length > 0)) {
    6303     63981825 :                     copy(bestConnectedNext->bestContinuations.begin(), bestConnectedNext->bestContinuations.end(), back_inserter(j.bestContinuations));
    6304              :                 } else {
    6305      9944602 :                     j.allowsContinuation = false;
    6306              :                 }
    6307     73926427 :                 if (clanes[bestThisIndex].length < j.length
    6308     66828253 :                         || (clanes[bestThisIndex].length == j.length && abs(clanes[bestThisIndex].bestLaneOffset) > abs(j.bestLaneOffset))
    6309    203280264 :                         || (clanes[bestThisIndex].length == j.length && abs(clanes[bestThisIndex].bestLaneOffset) == abs(j.bestLaneOffset) &&
    6310     62663007 :                             nextLinkPriority(clanes[bestThisIndex].bestContinuations) < nextLinkPriority(j.bestContinuations))
    6311              :                    ) {
    6312              :                     bestThisIndex = index;
    6313              :                     bestThisMaxIndex = index;
    6314     66678961 :                 } else if (clanes[bestThisIndex].length == j.length
    6315     62651270 :                            && abs(clanes[bestThisIndex].bestLaneOffset) == abs(j.bestLaneOffset)
    6316    129330099 :                            && nextLinkPriority(clanes[bestThisIndex].bestContinuations) == nextLinkPriority(j.bestContinuations)) {
    6317              :                     bestThisMaxIndex = index;
    6318              :                 }
    6319     73926427 :                 index++;
    6320              :             }
    6321              : 
    6322              :             //vehicle with elecHybrid device prefers running under an overhead wire
    6323     49178866 :             if (getDevice(typeid(MSDevice_ElecHybrid)) != nullptr) {
    6324              :                 index = 0;
    6325          491 :                 for (const LaneQ& j : clanes) {
    6326          339 :                     std::string overheadWireSegmentID = MSNet::getInstance()->getStoppingPlaceID(j.lane, j.currentLength / 2., SUMO_TAG_OVERHEAD_WIRE_SEGMENT);
    6327          339 :                     if (overheadWireSegmentID != "") {
    6328              :                         bestThisIndex = index;
    6329              :                         bestThisMaxIndex = index;
    6330              :                     }
    6331          339 :                     index++;
    6332              :                 }
    6333              :             }
    6334              : 
    6335              :         } else {
    6336              :             // only needed in case of disconnected routes
    6337              :             int bestNextIndex = 0;
    6338        12591 :             int bestDistToNeeded = (int) clanes.size();
    6339              :             index = 0;
    6340        35535 :             for (std::vector<LaneQ>::iterator j = clanes.begin(); j != clanes.end(); ++j, ++index) {
    6341        22944 :                 if ((*j).allowsContinuation) {
    6342              :                     int nextIndex = 0;
    6343        56729 :                     for (std::vector<LaneQ>::const_iterator m = nextLanes.begin(); m != nextLanes.end(); ++m, ++nextIndex) {
    6344        34534 :                         if ((*m).lane->isApproachedFrom((*j).lane, getVClass())) {
    6345         5023 :                             if (bestDistToNeeded > abs((*m).bestLaneOffset)) {
    6346              :                                 bestDistToNeeded = abs((*m).bestLaneOffset);
    6347              :                                 bestThisIndex = index;
    6348              :                                 bestThisMaxIndex = index;
    6349              :                                 bestNextIndex = nextIndex;
    6350              :                             }
    6351              :                         }
    6352              :                     }
    6353              :                 }
    6354              :             }
    6355        12591 :             clanes[bestThisIndex].length += nextLanes[bestNextIndex].length;
    6356        12591 :             copy(nextLanes[bestNextIndex].bestContinuations.begin(), nextLanes[bestNextIndex].bestContinuations.end(), back_inserter(clanes[bestThisIndex].bestContinuations));
    6357              : 
    6358              :         }
    6359              :         // set bestLaneOffset for all lanes
    6360              :         index = 0;
    6361              :         bool requiredChangeRightForbidden = false;
    6362              :         int requireChangeToLeftForbidden = -1;
    6363    123140828 :         for (std::vector<LaneQ>::iterator j = clanes.begin(); j != clanes.end(); ++j, ++index) {
    6364     73949371 :             if ((*j).length < clanes[bestThisIndex].length
    6365     62328556 :                     || ((*j).length == clanes[bestThisIndex].length && abs((*j).bestLaneOffset) > abs(clanes[bestThisIndex].bestLaneOffset))
    6366    136277695 :                     || (nextLinkPriority((*j).bestContinuations)) < nextLinkPriority(clanes[bestThisIndex].bestContinuations)
    6367              :                ) {
    6368     11797758 :                 if (abs(bestThisIndex - index) < abs(bestThisMaxIndex - index)) {
    6369       750865 :                     (*j).bestLaneOffset = bestThisIndex - index;
    6370              :                 } else {
    6371     11046893 :                     (*j).bestLaneOffset = bestThisMaxIndex - index;
    6372              :                 }
    6373     11797758 :                 if ((nextLinkPriority((*j).bestContinuations)) < nextLinkPriority(clanes[bestThisIndex].bestContinuations)) {
    6374              :                     // try to move away from the lower-priority lane before it ends
    6375     10112039 :                     (*j).length = (*j).currentLength;
    6376              :                 }
    6377     11797758 :                 if (!(*j).allowsContinuation) {
    6378      9930646 :                     if ((*j).bestLaneOffset < 0 && (!(*j).lane->allowsChangingRight(getVClass())
    6379      2540779 :                                 || !(*j).lane->getParallelLane(-1, false)->allowsVehicleClass(getVClass())
    6380      2526582 :                                 || requiredChangeRightForbidden)) {
    6381              :                         // this lane and all further lanes to the left cannot be used
    6382              :                         requiredChangeRightForbidden = true;
    6383        28511 :                         if ((*j).length == (*j).currentLength) {
    6384        28511 :                             (*j).length = 0;
    6385              :                         }
    6386      9902135 :                     } else if ((*j).bestLaneOffset > 0 && (!(*j).lane->allowsChangingLeft(getVClass())
    6387      7332515 :                                 || !(*j).lane->getParallelLane(1, false)->allowsVehicleClass(getVClass()))) {
    6388              :                         // this lane and all previous lanes to the right cannot be used
    6389       114116 :                         requireChangeToLeftForbidden = (*j).lane->getIndex();
    6390              :                     }
    6391              :                 }
    6392              :             } else {
    6393     62151613 :                 (*j).bestLaneOffset = 0;
    6394              :             }
    6395              :         }
    6396     49323816 :         for (int idx = requireChangeToLeftForbidden; idx >= 0; idx--) {
    6397       132359 :             if (clanes[idx].length == clanes[idx].currentLength) {
    6398       132359 :                 clanes[idx].length = 0;
    6399              :             };
    6400              :         }
    6401              : 
    6402              :         //vehicle with elecHybrid device prefers running under an overhead wire
    6403     49191457 :         if (static_cast<MSDevice_ElecHybrid*>(getDevice(typeid(MSDevice_ElecHybrid))) != 0) {
    6404              :             index = 0;
    6405          152 :             std::string overheadWireID = MSNet::getInstance()->getStoppingPlaceID(clanes[bestThisIndex].lane, (clanes[bestThisIndex].currentLength) / 2, SUMO_TAG_OVERHEAD_WIRE_SEGMENT);
    6406          152 :             if (overheadWireID != "") {
    6407          373 :                 for (std::vector<LaneQ>::iterator j = clanes.begin(); j != clanes.end(); ++j, ++index) {
    6408          261 :                     (*j).bestLaneOffset = bestThisIndex - index;
    6409              :                 }
    6410              :             }
    6411              :         }
    6412              : 
    6413              : #ifdef DEBUG_BESTLANES
    6414              :         if (DEBUG_COND) {
    6415              :             std::cout << "   edge=" << cE->getID() << " (bestIndex=" << bestThisIndex << " bestMaxIndex=" << bestThisMaxIndex << "):\n";
    6416              :             std::vector<LaneQ>& laneQs = clanes;
    6417              :             for (std::vector<LaneQ>::iterator j = laneQs.begin(); j != laneQs.end(); ++j) {
    6418              :                 std::cout << "     lane=" << (*j).lane->getID() << " length=" << (*j).length << " bestOffset=" << (*j).bestLaneOffset << " allowCont=" << (*j).allowsContinuation << "\n";
    6419              :             }
    6420              :         }
    6421              : #endif
    6422              : 
    6423              :     }
    6424     16096029 :     if (myBestLanes.front().front().lane->isInternal()) {
    6425              :         // route starts on an internal lane
    6426           28 :         if (myLane != nullptr) {
    6427              :             startLane = myLane;
    6428              :         } else {
    6429              :             // vehicle not yet departed
    6430           12 :             startLane = myBestLanes.front().front().lane;
    6431              :         }
    6432              :     }
    6433     16096029 :     updateOccupancyAndCurrentBestLane(startLane);
    6434              : #ifdef DEBUG_BESTLANES
    6435              :     if (DEBUG_COND) {
    6436              :         std::cout << SIMTIME << " veh=" << getID() << " bestCont=" << toString(getBestLanesContinuation()) << "\n";
    6437              :     }
    6438              : #endif
    6439              : }
    6440              : 
    6441              : void
    6442          236 : MSVehicle::updateLaneBruttoSum() {
    6443          236 :     if (myLane != nullptr) {
    6444          236 :         myLane->markRecalculateBruttoSum();
    6445              :     }
    6446          236 : }
    6447              : 
    6448              : bool
    6449     66135789 : MSVehicle::betterContinuation(const LaneQ* bestConnectedNext, const LaneQ& m) const {
    6450     66135789 :     if (bestConnectedNext == nullptr) {
    6451              :         return true;
    6452      2115965 :     } else if (m.lane->getBidiLane() != nullptr && bestConnectedNext->lane->getBidiLane() == nullptr) {
    6453              :         return false;
    6454      2115173 :     } else if (bestConnectedNext->lane->getBidiLane() != nullptr && m.lane->getBidiLane() == nullptr) {
    6455              :         return true;
    6456      2115173 :     } else if (bestConnectedNext->length < m.length) {
    6457              :         return true;
    6458      1752602 :     } else if (bestConnectedNext->length == m.length) {
    6459      1227457 :         if (abs(bestConnectedNext->bestLaneOffset) > abs(m.bestLaneOffset)) {
    6460              :             return true;
    6461              :         }
    6462      1061259 :         const double contRight = getVehicleType().getParameter().getLCParam(SUMO_ATTR_LCA_CONTRIGHT, 1);
    6463              :         if (contRight < 1
    6464              :                 // if we don't check for adjacency, the rightmost line will get
    6465              :                 // multiple chances to be better which leads to an uninituitve distribution
    6466         1008 :                 && (m.lane->getIndex() - bestConnectedNext->lane->getIndex()) == 1
    6467      1062040 :                 && RandHelper::rand(getRNG()) > contRight) {
    6468              :             return true;
    6469              :         }
    6470              :     }
    6471              :     return false;
    6472              : }
    6473              : 
    6474              : 
    6475              : int
    6476    398880454 : MSVehicle::nextLinkPriority(const std::vector<MSLane*>& conts) {
    6477    398880454 :     if (conts.size() < 2) {
    6478              :         return -1;
    6479              :     } else {
    6480    363244855 :         const MSLink* const link = conts[0]->getLinkTo(conts[1]);
    6481    363244855 :         if (link != nullptr) {
    6482    363223190 :             return link->havePriority() ? 1 : 0;
    6483              :         } else {
    6484              :             // disconnected route
    6485              :             return -1;
    6486              :         }
    6487              :     }
    6488              : }
    6489              : 
    6490              : 
    6491              : void
    6492   1866169635 : MSVehicle::updateOccupancyAndCurrentBestLane(const MSLane* startLane) {
    6493              :     std::vector<LaneQ>& currLanes = *myBestLanes.begin();
    6494              :     std::vector<LaneQ>::iterator i;
    6495              : #ifdef _DEBUG
    6496              :     bool found = false;
    6497              : #endif
    6498   5337432920 :     for (i = currLanes.begin(); i != currLanes.end(); ++i) {
    6499              :         double nextOccupation = 0;
    6500   7643292237 :         for (std::vector<MSLane*>::const_iterator j = (*i).bestContinuations.begin() + 1; j != (*i).bestContinuations.end(); ++j) {
    6501   4172028952 :             nextOccupation += (*j)->getBruttoVehLenSum();
    6502              :         }
    6503   3471263285 :         (*i).nextOccupation = nextOccupation;
    6504              : #ifdef DEBUG_BESTLANES
    6505              :         if (DEBUG_COND) {
    6506              :             std::cout << "     lane=" << (*i).lane->getID() << " nextOccupation=" << nextOccupation << "\n";
    6507              :         }
    6508              : #endif
    6509   3471263285 :         if ((*i).lane == startLane) {
    6510   1860901994 :             myCurrentLaneInBestLanes = i;
    6511              : #ifdef _DEBUG
    6512              :             found = true;
    6513              : #endif
    6514              :         }
    6515              :     }
    6516              : #ifdef _DEBUG
    6517              :     assert(found || startLane->isInternal());
    6518              : #endif
    6519   1866169635 : }
    6520              : 
    6521              : 
    6522              : const std::vector<MSLane*>&
    6523   2044938741 : MSVehicle::getBestLanesContinuation() const {
    6524   2044938741 :     if (myBestLanes.empty() || myBestLanes[0].empty()) {
    6525              :         return myEmptyLaneVector;
    6526              :     }
    6527   2044938741 :     return (*myCurrentLaneInBestLanes).bestContinuations;
    6528              : }
    6529              : 
    6530              : 
    6531              : const std::vector<MSLane*>&
    6532     69418210 : MSVehicle::getBestLanesContinuation(const MSLane* const l) const {
    6533              :     const MSLane* lane = l;
    6534              :     // XXX: shouldn't this be a "while" to cover more than one internal lane? (Leo) Refs. #2575
    6535     69418210 :     if (lane->getEdge().isInternal()) {
    6536              :         // internal edges are not kept inside the bestLanes structure
    6537      5085169 :         lane = lane->getLinkCont()[0]->getLane();
    6538              :     }
    6539     69418210 :     if (myBestLanes.size() == 0) {
    6540              :         return myEmptyLaneVector;
    6541              :     }
    6542    114590794 :     for (std::vector<LaneQ>::const_iterator i = myBestLanes[0].begin(); i != myBestLanes[0].end(); ++i) {
    6543    114578171 :         if ((*i).lane == lane) {
    6544     69405587 :             return (*i).bestContinuations;
    6545              :         }
    6546              :     }
    6547              :     return myEmptyLaneVector;
    6548              : }
    6549              : 
    6550              : const std::vector<const MSLane*>
    6551       286963 : MSVehicle::getUpcomingLanesUntil(double distance) const {
    6552              :     std::vector<const MSLane*> lanes;
    6553              : 
    6554       286963 :     if (distance <= 0. || hasArrived()) {
    6555              :         // WRITE_WARNINGF(TL("MSVehicle::getUpcomingLanesUntil(): distance ('%') should be greater than 0."), distance);
    6556              :         return lanes;
    6557              :     }
    6558              : 
    6559       286759 :     if (!myLaneChangeModel->isOpposite()) {
    6560       283433 :         distance += getPositionOnLane();
    6561              :     } else {
    6562         3326 :         distance += myLane->getOppositePos(getPositionOnLane());
    6563              :     }
    6564       286759 :     MSLane* lane = myLaneChangeModel->isOpposite() ? myLane->getParallelOpposite() : myLane;
    6565       294787 :     while (lane->isInternal() && (distance > 0.)) {  // include initial internal lanes
    6566         8028 :         lanes.insert(lanes.end(), lane);
    6567         8028 :         distance -= lane->getLength();
    6568        13504 :         lane = lane->getLinkCont().front()->getViaLaneOrLane();
    6569              :     }
    6570              : 
    6571       286759 :     const std::vector<MSLane*>& contLanes = getBestLanesContinuation();
    6572       286759 :     if (contLanes.empty()) {
    6573              :         return lanes;
    6574              :     }
    6575              :     auto contLanesIt = contLanes.begin();
    6576       286759 :     MSRouteIterator routeIt = myCurrEdge;  // keep track of covered edges in myRoute
    6577       613931 :     while (distance > 0.) {
    6578       334947 :         MSLane* l = nullptr;
    6579       334947 :         if (contLanesIt != contLanes.end()) {
    6580       319097 :             l = *contLanesIt;
    6581              :             if (l != nullptr) {
    6582              :                 assert(l->getEdge().getID() == (*routeIt)->getLanes().front()->getEdge().getID());
    6583              :             }
    6584              :             ++contLanesIt;
    6585       319097 :             if (l != nullptr || myLane->isInternal()) {
    6586              :                 ++routeIt;
    6587              :             }
    6588       319097 :             if (l == nullptr) {
    6589         5472 :                 continue;
    6590              :             }
    6591        15850 :         } else if (routeIt != myRoute->end()) {  // bestLanes didn't get us far enough
    6592              :             // choose left-most lane as default (avoid sidewalks, bike lanes etc)
    6593         8962 :             l = (*routeIt)->getLanes().back();
    6594              :             ++routeIt;
    6595              :         } else {  // the search distance goes beyond our route
    6596              :             break;
    6597              :         }
    6598              : 
    6599              :         assert(l != nullptr);
    6600              : 
    6601              :         // insert internal lanes if applicable
    6602       322587 :         const MSLane* internalLane = lanes.size() > 0 ? lanes.back()->getInternalFollowingLane(l) : nullptr;
    6603       366673 :         while ((internalLane != nullptr) && internalLane->isInternal() && (distance > 0.)) {
    6604        44086 :             lanes.insert(lanes.end(), internalLane);
    6605        44086 :             distance -= internalLane->getLength();
    6606        70464 :             internalLane = internalLane->getLinkCont().front()->getViaLaneOrLane();
    6607              :         }
    6608       322587 :         if (distance <= 0.) {
    6609              :             break;
    6610              :         }
    6611              : 
    6612       321700 :         lanes.insert(lanes.end(), l);
    6613       321700 :         distance -= l->getLength();
    6614              :     }
    6615              : 
    6616              :     return lanes;
    6617            0 : }
    6618              : 
    6619              : const std::vector<const MSLane*>
    6620         6699 : MSVehicle::getPastLanesUntil(double distance) const {
    6621              :     std::vector<const MSLane*> lanes;
    6622              : 
    6623         6699 :     if (distance <= 0.) {
    6624              :         // WRITE_WARNINGF(TL("MSVehicle::getPastLanesUntil(): distance ('%') should be greater than 0."), distance);
    6625              :         return lanes;
    6626              :     }
    6627              : 
    6628         6591 :     MSRouteIterator routeIt = myCurrEdge;
    6629         6591 :     if (!myLaneChangeModel->isOpposite()) {
    6630         6567 :         distance += myLane->getLength() - getPositionOnLane();
    6631              :     } else {
    6632           24 :         distance += myLane->getParallelOpposite()->getLength() - myLane->getOppositePos(getPositionOnLane());
    6633              :     }
    6634         6591 :     MSLane* lane = myLaneChangeModel->isOpposite() ? myLane->getParallelOpposite() : myLane;
    6635         6612 :     while (lane->isInternal() && (distance > 0.)) {  // include initial internal lanes
    6636           21 :         lanes.insert(lanes.end(), lane);
    6637           21 :         distance -= lane->getLength();
    6638           21 :         lane = lane->getLogicalPredecessorLane();
    6639              :     }
    6640              : 
    6641        10151 :     while (distance > 0.) {
    6642              :         // choose left-most lane as default (avoid sidewalks, bike lanes etc)
    6643         8809 :         MSLane* l = (*routeIt)->getLanes().back();
    6644              : 
    6645              :         // insert internal lanes if applicable
    6646         8809 :         const MSEdge* internalEdge = lanes.size() > 0 ? (*routeIt)->getInternalFollowingEdge(&(lanes.back()->getEdge()), getVClass()) : nullptr;
    6647         8830 :         const MSLane* internalLane = internalEdge != nullptr ? internalEdge->getLanes().front() : nullptr;
    6648              :         std::vector<const MSLane*> internalLanes;
    6649        11034 :         while ((internalLane != nullptr) && internalLane->isInternal()) {  // collect all internal successor lanes
    6650         2225 :             internalLanes.insert(internalLanes.begin(), internalLane);
    6651         4443 :             internalLane = internalLane->getLinkCont().front()->getViaLaneOrLane();
    6652              :         }
    6653        11034 :         for (auto it = internalLanes.begin(); (it != internalLanes.end()) && (distance > 0.); ++it) {  // check remaining distance in correct order
    6654         2225 :             lanes.insert(lanes.end(), *it);
    6655         2225 :             distance -= (*it)->getLength();
    6656              :         }
    6657         8809 :         if (distance <= 0.) {
    6658              :             break;
    6659              :         }
    6660              : 
    6661         8793 :         lanes.insert(lanes.end(), l);
    6662         8793 :         distance -= l->getLength();
    6663              : 
    6664              :         // NOTE: we're going backwards with the (bi-directional) Iterator
    6665              :         // TODO: consider make reverse_iterator() when moving on to C++14 or later
    6666         8793 :         if (routeIt != myRoute->begin()) {
    6667              :             --routeIt;
    6668              :         } else {  // we went backwards to begin() and already processed the first and final element
    6669              :             break;
    6670              :         }
    6671         8809 :     }
    6672              : 
    6673              :     return lanes;
    6674            0 : }
    6675              : 
    6676              : 
    6677              : const std::vector<MSLane*>
    6678         6363 : MSVehicle::getUpstreamOppositeLanes() const {
    6679         6363 :     const std::vector<const MSLane*> routeLanes = getPastLanesUntil(myLane->getMaximumBrakeDist());
    6680              :     std::vector<MSLane*> result;
    6681        15555 :     for (const MSLane* lane : routeLanes) {
    6682         9924 :         MSLane* opposite = lane->getOpposite();
    6683         9924 :         if (opposite != nullptr) {
    6684         9192 :             result.push_back(opposite);
    6685              :         } else {
    6686              :             break;
    6687              :         }
    6688              :     }
    6689         6363 :     return result;
    6690         6363 : }
    6691              : 
    6692              : 
    6693              : int
    6694    307026677 : MSVehicle::getBestLaneOffset() const {
    6695    307026677 :     if (myBestLanes.empty() || myBestLanes[0].empty()) {
    6696              :         return 0;
    6697              :     } else {
    6698    306729658 :         return (*myCurrentLaneInBestLanes).bestLaneOffset;
    6699              :     }
    6700              : }
    6701              : 
    6702              : double
    6703        23437 : MSVehicle::getBestLaneDist() const {
    6704        23437 :     if (myBestLanes.empty() || myBestLanes[0].empty()) {
    6705              :         return -1;
    6706              :     } else {
    6707        23437 :         return (*myCurrentLaneInBestLanes).length;
    6708              :     }
    6709              : }
    6710              : 
    6711              : 
    6712              : 
    6713              : void
    6714    646489383 : MSVehicle::adaptBestLanesOccupation(int laneIndex, double density) {
    6715              :     std::vector<MSVehicle::LaneQ>& preb = myBestLanes.front();
    6716              :     assert(laneIndex < (int)preb.size());
    6717    646489383 :     preb[laneIndex].occupation = density + preb[laneIndex].nextOccupation;
    6718    646489383 : }
    6719              : 
    6720              : 
    6721              : void
    6722        71272 : MSVehicle::fixPosition() {
    6723        71272 :     if (MSGlobals::gLaneChangeDuration > 0 && !myLaneChangeModel->isChangingLanes()) {
    6724        39640 :         myState.myPosLat = 0;
    6725              :     }
    6726        71272 : }
    6727              : 
    6728              : std::pair<const MSLane*, double>
    6729          303 : MSVehicle::getLanePosAfterDist(double distance) const {
    6730          303 :     if (distance == 0) {
    6731          255 :         return std::make_pair(myLane, getPositionOnLane());
    6732              :     }
    6733           48 :     const std::vector<const MSLane*> lanes = getUpcomingLanesUntil(distance);
    6734           48 :     distance += getPositionOnLane();
    6735           48 :     for (const MSLane* lane : lanes) {
    6736           48 :         if (lane->getLength() > distance) {
    6737              :             return std::make_pair(lane, distance);
    6738              :         }
    6739            0 :         distance -= lane->getLength();
    6740              :     }
    6741            0 :     return std::make_pair(nullptr, -1);
    6742           48 : }
    6743              : 
    6744              : 
    6745              : double
    6746        16093 : MSVehicle::getDistanceToPosition(double destPos, const MSLane* destLane) const {
    6747        16093 :     if (isOnRoad() && destLane != nullptr) {
    6748        16060 :         return myRoute->getDistanceBetween(getPositionOnLane(), destPos, myLane, destLane);
    6749              :     }
    6750              :     return std::numeric_limits<double>::max();
    6751              : }
    6752              : 
    6753              : 
    6754              : std::pair<const MSVehicle* const, double>
    6755     76423867 : MSVehicle::getLeader(double dist, bool considerCrossingFoes) const {
    6756     76423867 :     if (myLane == nullptr) {
    6757            0 :         return std::make_pair(static_cast<const MSVehicle*>(nullptr), -1);
    6758              :     }
    6759     76423867 :     if (dist == 0) {
    6760         2460 :         dist = getCarFollowModel().brakeGap(getSpeed()) + getVehicleType().getMinGap();
    6761              :     }
    6762              :     const MSVehicle* lead = nullptr;
    6763     76423867 :     const MSLane* lane = myLane; // ensure lane does not change between getVehiclesSecure and releaseVehicles;
    6764     76423867 :     const MSLane::VehCont& vehs = lane->getVehiclesSecure();
    6765              :     // vehicle might be outside the road network
    6766     76423867 :     MSLane::VehCont::const_iterator it = std::find(vehs.begin(), vehs.end(), this);
    6767     76423867 :     if (it != vehs.end() && it + 1 != vehs.end()) {
    6768     72701612 :         lead = *(it + 1);
    6769              :     }
    6770     72701612 :     if (lead != nullptr) {
    6771              :         std::pair<const MSVehicle* const, double> result(
    6772     72701612 :             lead, lead->getBackPositionOnLane(myLane) - getPositionOnLane() - getVehicleType().getMinGap());
    6773     72701612 :         lane->releaseVehicles();
    6774     72701612 :         return result;
    6775              :     }
    6776      3722255 :     const double seen = myLane->getLength() - getPositionOnLane();
    6777      3722255 :     const std::vector<MSLane*>& bestLaneConts = getBestLanesContinuation(myLane);
    6778      3722255 :     std::pair<const MSVehicle* const, double> result = myLane->getLeaderOnConsecutive(dist, seen, getSpeed(), *this, bestLaneConts, considerCrossingFoes);
    6779      3722252 :     lane->releaseVehicles();
    6780      3722252 :     return result;
    6781              : }
    6782              : 
    6783              : 
    6784              : std::pair<const MSVehicle* const, double>
    6785      2269650 : MSVehicle::getFollower(double dist) const {
    6786      2269650 :     if (myLane == nullptr) {
    6787            0 :         return std::make_pair(static_cast<const MSVehicle*>(nullptr), -1);
    6788              :     }
    6789      2269650 :     if (dist == 0) {
    6790       810945 :         dist = getCarFollowModel().brakeGap(myLane->getEdge().getSpeedLimit() * 2, 4.5, 0);
    6791              :     }
    6792      2269650 :     return myLane->getFollower(this, getPositionOnLane(), dist, MSLane::MinorLinkMode::FOLLOW_NEVER);
    6793              : }
    6794              : 
    6795              : 
    6796              : double
    6797            0 : MSVehicle::getTimeGapOnLane() const {
    6798              :     // calling getLeader with 0 would induce a dist calculation but we only want to look for the leaders on the current lane
    6799            0 :     std::pair<const MSVehicle* const, double> leaderInfo = getLeader(-1);
    6800            0 :     if (leaderInfo.first == nullptr || getSpeed() == 0) {
    6801            0 :         return -1;
    6802              :     }
    6803            0 :     return (leaderInfo.second + getVehicleType().getMinGap()) / getSpeed();
    6804              : }
    6805              : 
    6806              : 
    6807              : void
    6808      3855195 : MSVehicle::addTransportable(MSTransportable* transportable) {
    6809      3855195 :     MSBaseVehicle::addTransportable(transportable);
    6810        40994 :     if (myStops.size() > 0 && myStops.front().reached) {
    6811        37798 :         if (transportable->isPerson()) {
    6812        37219 :             if (myStops.front().triggered && myStops.front().numExpectedPerson > 0) {
    6813         1831 :                 myStops.front().numExpectedPerson -= (int)myStops.front().pars.awaitedPersons.count(transportable->getID());
    6814              :             }
    6815              :         } else {
    6816          579 :             if (myStops.front().pars.containerTriggered && myStops.front().numExpectedContainer > 0) {
    6817           20 :                 myStops.front().numExpectedContainer -= (int)myStops.front().pars.awaitedContainers.count(transportable->getID());
    6818              :             }
    6819              :         }
    6820              :     }
    6821        40994 : }
    6822              : 
    6823              : 
    6824              : void
    6825    697113268 : MSVehicle::setBlinkerInformation() {
    6826              :     switchOffSignal(VEH_SIGNAL_BLINKER_RIGHT | VEH_SIGNAL_BLINKER_LEFT);
    6827    697113268 :     int state = myLaneChangeModel->getOwnState();
    6828              :     // do not set blinker for sublane changes or when blocked from changing to the right
    6829    697113268 :     const bool blinkerManoeuvre = (((state & LCA_SUBLANE) == 0) && (
    6830    606759842 :                                        (state & LCA_KEEPRIGHT) == 0 || (state & LCA_BLOCKED) == 0));
    6831              :     Signalling left = VEH_SIGNAL_BLINKER_LEFT;
    6832              :     Signalling right = VEH_SIGNAL_BLINKER_RIGHT;
    6833    697113268 :     if (MSGlobals::gLefthand) {
    6834              :         // lane indices increase from left to right
    6835              :         std::swap(left, right);
    6836              :     }
    6837    697113268 :     if ((state & LCA_LEFT) != 0 && blinkerManoeuvre) {
    6838     20068184 :         switchOnSignal(left);
    6839    677045084 :     } else if ((state & LCA_RIGHT) != 0 && blinkerManoeuvre) {
    6840      5554091 :         switchOnSignal(right);
    6841    671490993 :     } else if (myLaneChangeModel->isChangingLanes()) {
    6842       254915 :         if (myLaneChangeModel->getLaneChangeDirection() == 1) {
    6843       165402 :             switchOnSignal(left);
    6844              :         } else {
    6845        89513 :             switchOnSignal(right);
    6846              :         }
    6847              :     } else {
    6848    671236078 :         const MSLane* lane = getLane();
    6849    671236078 :         std::vector<MSLink*>::const_iterator link = MSLane::succLinkSec(*this, 1, *lane, getBestLanesContinuation());
    6850    671236078 :         if (link != lane->getLinkCont().end() && lane->getLength() - getPositionOnLane() < lane->getVehicleMaxSpeed(this) * (double) 7.) {
    6851    169168694 :             switch ((*link)->getDirection()) {
    6852              :                 case LinkDirection::TURN:
    6853              :                 case LinkDirection::LEFT:
    6854              :                 case LinkDirection::PARTLEFT:
    6855              :                     switchOnSignal(VEH_SIGNAL_BLINKER_LEFT);
    6856              :                     break;
    6857              :                 case LinkDirection::RIGHT:
    6858              :                 case LinkDirection::PARTRIGHT:
    6859              :                     switchOnSignal(VEH_SIGNAL_BLINKER_RIGHT);
    6860              :                     break;
    6861              :                 default:
    6862              :                     break;
    6863              :             }
    6864              :         }
    6865              :     }
    6866              :     // stopping related signals
    6867    697113268 :     if (hasStops()
    6868    697113268 :             && (myStops.begin()->reached ||
    6869     15592790 :                 (myStopDist < (myLane->getLength() - getPositionOnLane())
    6870      4396170 :                  && myStopDist < getCarFollowModel().brakeGap(myLane->getVehicleMaxSpeed(this), getCarFollowModel().getMaxDecel(), 3)))) {
    6871     17302801 :         if (myStops.begin()->lane->getIndex() > 0 && myStops.begin()->lane->getParallelLane(-1)->allowsVehicleClass(getVClass())) {
    6872              :             // not stopping on the right. Activate emergency blinkers
    6873              :             switchOnSignal(VEH_SIGNAL_BLINKER_LEFT | VEH_SIGNAL_BLINKER_RIGHT);
    6874     17046850 :         } else if (!myStops.begin()->reached && (myStops.begin()->pars.parking == ParkingType::OFFROAD)) {
    6875              :             // signal upcoming parking stop on the current lane when within braking distance (~2 seconds before braking)
    6876      1632532 :             switchOnSignal(MSGlobals::gLefthand ? VEH_SIGNAL_BLINKER_LEFT : VEH_SIGNAL_BLINKER_RIGHT);
    6877              :         }
    6878              :     }
    6879    697113268 :     if (myInfluencer != nullptr && myInfluencer->getSignals() >= 0) {
    6880           14 :         mySignals = myInfluencer->getSignals();
    6881              :         myInfluencer->setSignals(-1); // overwrite computed signals only once
    6882              :     }
    6883    697113268 : }
    6884              : 
    6885              : void
    6886        85511 : MSVehicle::setEmergencyBlueLight(SUMOTime currentTime) {
    6887              : 
    6888              :     //TODO look if timestep ist SIMSTEP
    6889        85511 :     if (currentTime % 1000 == 0) {
    6890        25719 :         if (signalSet(VEH_SIGNAL_EMERGENCY_BLUE)) {
    6891              :             switchOffSignal(VEH_SIGNAL_EMERGENCY_BLUE);
    6892              :         } else {
    6893              :             switchOnSignal(VEH_SIGNAL_EMERGENCY_BLUE);
    6894              :         }
    6895              :     }
    6896        85511 : }
    6897              : 
    6898              : 
    6899              : int
    6900     22411930 : MSVehicle::getLaneIndex() const {
    6901     22411930 :     return myLane == nullptr ? -1 : myLane->getIndex();
    6902              : }
    6903              : 
    6904              : 
    6905              : void
    6906     14725573 : MSVehicle::setTentativeLaneAndPosition(MSLane* lane, double pos, double posLat) {
    6907     14725573 :     myLane = lane;
    6908     14725573 :     myState.myPos = pos;
    6909     14725573 :     myState.myPosLat = posLat;
    6910     14725573 :     myState.myBackPos = pos - getVehicleType().getLength();
    6911     14725573 : }
    6912              : 
    6913              : 
    6914              : double
    6915    390783077 : MSVehicle::getRightSideOnLane() const {
    6916    390783077 :     return myState.myPosLat + 0.5 * myLane->getWidth() - 0.5 * getVehicleType().getWidth();
    6917              : }
    6918              : 
    6919              : 
    6920              : double
    6921    385500279 : MSVehicle::getLeftSideOnLane() const {
    6922    385500279 :     return myState.myPosLat + 0.5 * myLane->getWidth() + 0.5 * getVehicleType().getWidth();
    6923              : }
    6924              : 
    6925              : 
    6926              : double
    6927    311307831 : MSVehicle::getRightSideOnLane(const MSLane* lane) const {
    6928    311307831 :     return myState.myPosLat + 0.5 * lane->getWidth() - 0.5 * getVehicleType().getWidth();
    6929              : }
    6930              : 
    6931              : 
    6932              : double
    6933    310818619 : MSVehicle::getLeftSideOnLane(const MSLane* lane) const {
    6934    310818619 :     return myState.myPosLat + 0.5 * lane->getWidth() + 0.5 * getVehicleType().getWidth();
    6935              : }
    6936              : 
    6937              : 
    6938              : double
    6939    249636194 : MSVehicle::getRightSideOnEdge(const MSLane* lane) const {
    6940    249636194 :     return getCenterOnEdge(lane) - 0.5 * getVehicleType().getWidth();
    6941              : }
    6942              : 
    6943              : 
    6944              : double
    6945     31192948 : MSVehicle::getLeftSideOnEdge(const MSLane* lane) const {
    6946     31192948 :     return getCenterOnEdge(lane) + 0.5 * getVehicleType().getWidth();
    6947              : }
    6948              : 
    6949              : 
    6950              : double
    6951    728881569 : MSVehicle::getCenterOnEdge(const MSLane* lane) const {
    6952    728881569 :     if (lane == nullptr || &lane->getEdge() == &myLane->getEdge()) {
    6953    728417187 :         return myLane->getRightSideOnEdge() + myState.myPosLat + 0.5 * myLane->getWidth();
    6954       464382 :     } else if (lane == myLaneChangeModel->getShadowLane()) {
    6955        13742 :         if (myLaneChangeModel->isOpposite() && &lane->getEdge() != &myLane->getEdge()) {
    6956        13734 :             return lane->getRightSideOnEdge() + lane->getWidth() - myState.myPosLat + 0.5 * myLane->getWidth();
    6957              :         }
    6958            8 :         if (myLaneChangeModel->getShadowDirection() == -1) {
    6959            0 :             return lane->getRightSideOnEdge() + lane->getWidth() + myState.myPosLat + 0.5 * myLane->getWidth();
    6960              :         } else {
    6961            8 :             return lane->getRightSideOnEdge() - myLane->getWidth() + myState.myPosLat + 0.5 * myLane->getWidth();
    6962              :         }
    6963       450640 :     } else if (lane == myLane->getBidiLane()) {
    6964        17033 :         return lane->getRightSideOnEdge() - myState.myPosLat + 0.5 * lane->getWidth();
    6965              :     } else {
    6966              :         assert(myFurtherLanes.size() == myFurtherLanesPosLat.size());
    6967       510421 :         for (int i = 0; i < (int)myFurtherLanes.size(); ++i) {
    6968       484432 :             if (myFurtherLanes[i] == lane) {
    6969              : #ifdef DEBUG_FURTHER
    6970              :                 if (DEBUG_COND) std::cout << "    getCenterOnEdge veh=" << getID() << " lane=" << lane->getID() << " i=" << i << " furtherLat=" << myFurtherLanesPosLat[i]
    6971              :                                               << " result=" << lane->getRightSideOnEdge() + myFurtherLanesPosLat[i] + 0.5 * lane->getWidth()
    6972              :                                               << "\n";
    6973              : #endif
    6974       407539 :                 return lane->getRightSideOnEdge() + myFurtherLanesPosLat[i] + 0.5 * lane->getWidth();
    6975        76893 :             } else if (myFurtherLanes[i]->getBidiLane() == lane) {
    6976              : #ifdef DEBUG_FURTHER
    6977              :                 if (DEBUG_COND) std::cout << "    getCenterOnEdge veh=" << getID() << " lane=" << lane->getID() << " i=" << i << " furtherLat(bidi)=" << myFurtherLanesPosLat[i]
    6978              :                                               << " result=" << lane->getRightSideOnEdge() + myFurtherLanesPosLat[i] + 0.5 * lane->getWidth()
    6979              :                                               << "\n";
    6980              : #endif
    6981           79 :                 return lane->getRightSideOnEdge() - myFurtherLanesPosLat[i] + 0.5 * lane->getWidth();
    6982              :             }
    6983              :         }
    6984              :         //if (DEBUG_COND) std::cout << SIMTIME << " veh=" << getID() << " myShadowFurtherLanes=" << toString(myLaneChangeModel->getShadowFurtherLanes()) << "\n";
    6985        25989 :         const std::vector<MSLane*>& shadowFurther = myLaneChangeModel->getShadowFurtherLanes();
    6986        26261 :         for (int i = 0; i < (int)shadowFurther.size(); ++i) {
    6987              :             //if (DEBUG_COND) std::cout << " comparing i=" << (*i)->getID() << " lane=" << lane->getID() << "\n";
    6988        26261 :             if (shadowFurther[i] == lane) {
    6989              :                 assert(myLaneChangeModel->getShadowLane() != 0);
    6990        25989 :                 return (lane->getRightSideOnEdge() + myLaneChangeModel->getShadowFurtherLanesPosLat()[i] + 0.5 * lane->getWidth()
    6991        25989 :                         + (myLane->getCenterOnEdge() - myLaneChangeModel->getShadowLane()->getCenterOnEdge()));
    6992              :             }
    6993              :         }
    6994              :         assert(false);
    6995            0 :         throw ProcessError("Request lateral pos of vehicle '" + getID() + "' for invalid lane '" + Named::getIDSecure(lane) + "'");
    6996              :     }
    6997              : }
    6998              : 
    6999              : 
    7000              : double
    7001   3383662500 : MSVehicle::getLatOffset(const MSLane* lane) const {
    7002              :     assert(lane != 0);
    7003   3383662500 :     if (&lane->getEdge() == &myLane->getEdge()) {
    7004   3333721134 :         return myLane->getRightSideOnEdge() - lane->getRightSideOnEdge();
    7005     49941366 :     } else if (myLane->getParallelOpposite() == lane) {
    7006      2133940 :         return (myLane->getWidth() + lane->getWidth()) * 0.5 - 2 * getLateralPositionOnLane();
    7007     47807426 :     } else if (myLane->getBidiLane() == lane) {
    7008       811119 :         return -2 * getLateralPositionOnLane();
    7009              :     } else {
    7010              :         // Check whether the lane is a further lane for the vehicle
    7011     52990992 :         for (int i = 0; i < (int)myFurtherLanes.size(); ++i) {
    7012     52050508 :             if (myFurtherLanes[i] == lane) {
    7013              : #ifdef DEBUG_FURTHER
    7014              :                 if (DEBUG_COND) {
    7015              :                     std::cout << "    getLatOffset veh=" << getID() << " lane=" << lane->getID() << " i=" << i << " posLat=" << myState.myPosLat << " furtherLat=" << myFurtherLanesPosLat[i] << "\n";
    7016              :                 }
    7017              : #endif
    7018     46022467 :                 return myFurtherLanesPosLat[i] - myState.myPosLat;
    7019      6028041 :             } else if (myFurtherLanes[i]->getBidiLane() == lane) {
    7020              : #ifdef DEBUG_FURTHER
    7021              :                 if (DEBUG_COND) {
    7022              :                     std::cout << "    getLatOffset veh=" << getID() << " lane=" << lane->getID() << " i=" << i << " posLat=" << myState.myPosLat << " furtherBidiLat=" << myFurtherLanesPosLat[i] << "\n";
    7023              :                 }
    7024              : #endif
    7025        33356 :                 return -2 * (myFurtherLanesPosLat[i] - myState.myPosLat);
    7026              :             }
    7027              :         }
    7028              : #ifdef DEBUG_FURTHER
    7029              :         if (DEBUG_COND) {
    7030              :             std::cout << SIMTIME << " veh=" << getID() << " myShadowFurtherLanes=" << toString(myLaneChangeModel->getShadowFurtherLanes()) << "\n";
    7031              :         }
    7032              : #endif
    7033              :         // Check whether the lane is a "shadow further lane" for the vehicle
    7034       940484 :         const std::vector<MSLane*>& shadowFurther = myLaneChangeModel->getShadowFurtherLanes();
    7035       951733 :         for (int i = 0; i < (int)shadowFurther.size(); ++i) {
    7036       947756 :             if (shadowFurther[i] == lane) {
    7037              : #ifdef DEBUG_FURTHER
    7038              :                 if (DEBUG_COND) std::cout << "    getLatOffset veh=" << getID()
    7039              :                                               << " shadowLane=" << Named::getIDSecure(myLaneChangeModel->getShadowLane())
    7040              :                                               << " lane=" << lane->getID()
    7041              :                                               << " i=" << i
    7042              :                                               << " posLat=" << myState.myPosLat
    7043              :                                               << " shadowPosLat=" << getLatOffset(myLaneChangeModel->getShadowLane())
    7044              :                                               << " shadowFurtherLat=" << myLaneChangeModel->getShadowFurtherLanesPosLat()[i]
    7045              :                                               <<  "\n";
    7046              : #endif
    7047       935833 :                 return getLatOffset(myLaneChangeModel->getShadowLane()) + myLaneChangeModel->getShadowFurtherLanesPosLat()[i] - myState.myPosLat;
    7048        11923 :             } else if (shadowFurther[i]->getBidiLane() == lane) {
    7049              : #ifdef DEBUG_FURTHER
    7050              :                 if (DEBUG_COND) {
    7051              :                     std::cout << "    getLatOffset veh=" << getID() << " shadowbidilane=" << lane->getID() << " i=" << i << " posLat=" << myState.myPosLat << " furtherBidiLat=" << myFurtherLanesPosLat[i] << "\n";
    7052              :                 }
    7053              : #endif
    7054          674 :                 return -2 * getLatOffset(myLaneChangeModel->getShadowLane()) + myLaneChangeModel->getShadowFurtherLanesPosLat()[i] - myState.myPosLat;
    7055              :             }
    7056              :         }
    7057              :         // Check whether the vehicle issued a maneuverReservation on the lane.
    7058         3977 :         const std::vector<MSLane*>& furtherTargets = myLaneChangeModel->getFurtherTargetLanes();
    7059         5808 :         for (int i = 0; i < (int)myFurtherLanes.size(); ++i) {
    7060              :             // Further target lanes are just neighboring lanes of the vehicle's further lanes, @see MSAbstractLaneChangeModel::updateTargetLane()
    7061         5805 :             MSLane* targetLane = furtherTargets[i];
    7062         5805 :             if (targetLane == lane) {
    7063         3974 :                 const double targetDir = myLaneChangeModel->getManeuverDist() < 0 ? -1. : 1.;
    7064         3974 :                 const double latOffset = myFurtherLanesPosLat[i] - myState.myPosLat + targetDir * 0.5 * (myFurtherLanes[i]->getWidth() + targetLane->getWidth());
    7065              : #ifdef DEBUG_TARGET_LANE
    7066              :                 if (DEBUG_COND) {
    7067              :                     std::cout << "    getLatOffset veh=" << getID()
    7068              :                               << " wrt targetLane=" << Named::getIDSecure(myLaneChangeModel->getTargetLane())
    7069              :                               << "\n    i=" << i
    7070              :                               << " posLat=" << myState.myPosLat
    7071              :                               << " furtherPosLat=" << myFurtherLanesPosLat[i]
    7072              :                               << " maneuverDist=" << myLaneChangeModel->getManeuverDist()
    7073              :                               << " targetDir=" << targetDir
    7074              :                               << " latOffset=" << latOffset
    7075              :                               <<  std::endl;
    7076              :                 }
    7077              : #endif
    7078         3974 :                 return latOffset;
    7079         1831 :             } else if (targetLane != nullptr && targetLane->getBidiLane() == lane) {
    7080            0 :                 const double targetDir = myLaneChangeModel->getManeuverDist() < 0 ? -1. : 1.;
    7081            0 :                 const double latOffset = myFurtherLanesPosLat[i] - myState.myPosLat + targetDir * 0.5 * (myFurtherLanes[i]->getWidth() + targetLane->getWidth());
    7082              : #ifdef DEBUG_FURTHER
    7083              :                 if (DEBUG_COND) {
    7084              :                     std::cout << "    getLatOffset veh=" << getID() << " furthertargetbidilane=" << lane->getID() << " i=" << i << " posLat=" << myState.myPosLat << " furtherBidiLat=" << myFurtherLanesPosLat[i] << "\n";
    7085              :                 }
    7086              : #endif
    7087            0 :                 return -2 * latOffset;
    7088              :             }
    7089              :         }
    7090              :         assert(false);
    7091           18 :         throw ProcessError("Request lateral offset of vehicle '" + getID() + "' for invalid lane '" + Named::getIDSecure(lane) + "'");
    7092              :     }
    7093              : }
    7094              : 
    7095              : 
    7096              : double
    7097     36495943 : MSVehicle::lateralDistanceToLane(const int offset) const {
    7098              :     // compute the distance when changing to the neighboring lane
    7099              :     // (ensure we do not lap into the line behind neighLane since there might be unseen blockers)
    7100              :     assert(offset == 0 || offset == 1 || offset == -1);
    7101              :     assert(myLane != nullptr);
    7102              :     assert(myLane->getParallelLane(offset) != nullptr || myLane->getParallelOpposite() != nullptr);
    7103     36495943 :     const double halfCurrentLaneWidth = 0.5 * myLane->getWidth();
    7104     36495943 :     const double halfVehWidth = 0.5 * (getWidth() + NUMERICAL_EPS);
    7105     36495943 :     const double latPos = getLateralPositionOnLane();
    7106     36495943 :     const double oppositeSign = getLaneChangeModel().isOpposite() ? -1 : 1;
    7107     36495943 :     double leftLimit = halfCurrentLaneWidth - halfVehWidth - oppositeSign * latPos;
    7108     36495943 :     double rightLimit = -halfCurrentLaneWidth + halfVehWidth - oppositeSign * latPos;
    7109              :     double latLaneDist = 0;  // minimum distance to move the vehicle fully onto the new lane
    7110     36495943 :     if (offset == 0) {
    7111            8 :         if (latPos + halfVehWidth > halfCurrentLaneWidth) {
    7112              :             // correct overlapping left
    7113            4 :             latLaneDist = halfCurrentLaneWidth - latPos - halfVehWidth;
    7114            4 :         } else if (latPos - halfVehWidth < -halfCurrentLaneWidth) {
    7115              :             // correct overlapping right
    7116            4 :             latLaneDist = -halfCurrentLaneWidth - latPos + halfVehWidth;
    7117              :         }
    7118            8 :         latLaneDist *= oppositeSign;
    7119     36495935 :     } else if (offset == -1) {
    7120     16840355 :         latLaneDist = rightLimit - (getWidth() + NUMERICAL_EPS);
    7121     19655580 :     } else if (offset == 1) {
    7122     19655580 :         latLaneDist = leftLimit + (getWidth() + NUMERICAL_EPS);
    7123              :     }
    7124              : #ifdef DEBUG_ACTIONSTEPS
    7125              :     if (DEBUG_COND) {
    7126              :         std::cout << SIMTIME
    7127              :                   << " veh=" << getID()
    7128              :                   << " halfCurrentLaneWidth=" << halfCurrentLaneWidth
    7129              :                   << " halfVehWidth=" << halfVehWidth
    7130              :                   << " latPos=" << latPos
    7131              :                   << " latLaneDist=" << latLaneDist
    7132              :                   << " leftLimit=" << leftLimit
    7133              :                   << " rightLimit=" << rightLimit
    7134              :                   << "\n";
    7135              :     }
    7136              : #endif
    7137     36495943 :     return latLaneDist;
    7138              : }
    7139              : 
    7140              : 
    7141              : double
    7142   5129424648 : MSVehicle::getLateralOverlap(double posLat, const MSLane* lane) const {
    7143   5129424648 :     return (fabs(posLat) + 0.5 * getVehicleType().getWidth()
    7144   5129424648 :             - 0.5 * lane->getWidth());
    7145              : }
    7146              : 
    7147              : double
    7148            0 : MSVehicle::getLateralOverlap(const MSLane* lane) const {
    7149            0 :     return getLateralOverlap(getLateralPositionOnLane(), lane);
    7150              : }
    7151              : 
    7152              : double
    7153   4936071204 : MSVehicle::getLateralOverlap() const {
    7154   4936071204 :     return getLateralOverlap(getLateralPositionOnLane(), myLane);
    7155              : }
    7156              : 
    7157              : 
    7158              : void
    7159    640555229 : MSVehicle::removeApproachingInformation(const DriveItemVector& lfLinks) const {
    7160   1860930051 :     for (const DriveProcessItem& dpi : lfLinks) {
    7161   1220374822 :         if (dpi.myLink != nullptr) {
    7162    864044181 :             dpi.myLink->removeApproaching(this);
    7163              :         }
    7164              :     }
    7165              :     // unregister on all shadow links
    7166    640555229 :     myLaneChangeModel->removeShadowApproachingInformation();
    7167    640555229 : }
    7168              : 
    7169              : 
    7170              : bool
    7171       844881 : MSVehicle::unsafeLinkAhead(const MSLane* lane, double zipperDist) const {
    7172              :     // the following links are unsafe:
    7173              :     // - zipper links if they are close enough and have approaching vehicles in the relevant time range
    7174              :     // - unprioritized links if the vehicle is currently approaching a prioritzed link and unable to stop in time
    7175       844881 :     double seen = myLane->getLength() - getPositionOnLane();
    7176       844881 :     const double dist = MAX2(zipperDist, getCarFollowModel().brakeGap(getSpeed(), getCarFollowModel().getMaxDecel(), 0));
    7177       844881 :     if (seen < dist) {
    7178        71169 :         const std::vector<MSLane*>& bestLaneConts = getBestLanesContinuation(lane);
    7179              :         int view = 1;
    7180        71169 :         std::vector<MSLink*>::const_iterator link = MSLane::succLinkSec(*this, view, *lane, bestLaneConts);
    7181              :         DriveItemVector::const_iterator di = myLFLinkLanes.begin();
    7182       118315 :         while (!lane->isLinkEnd(link) && seen <= dist) {
    7183        71762 :             if ((!lane->isInternal()
    7184        49123 :                     && (((*link)->getState() == LINKSTATE_ZIPPER && seen < (*link)->getFoeVisibilityDistance())
    7185        26540 :                         || !(*link)->havePriority()))
    7186        98073 :                     || (lane->isInternal() && zipperDist > 0)) {
    7187              :                 // find the drive item corresponding to this link
    7188              :                 bool found = false;
    7189        52242 :                 while (di != myLFLinkLanes.end() && !found) {
    7190        27063 :                     if ((*di).myLink != nullptr) {
    7191              :                         const MSLane* diPredLane = (*di).myLink->getLaneBefore();
    7192        27059 :                         if (diPredLane != nullptr) {
    7193        27059 :                             if (&diPredLane->getEdge() == &lane->getEdge()) {
    7194              :                                 found = true;
    7195              :                             }
    7196              :                         }
    7197              :                     }
    7198        27063 :                     if (!found) {
    7199              :                         di++;
    7200              :                     }
    7201              :                 }
    7202        25179 :                 if (found) {
    7203        25175 :                     const SUMOTime leaveTime = (*link)->getLeaveTime((*di).myArrivalTime, (*di).myArrivalSpeed,
    7204        25175 :                                                (*di).getLeaveSpeed(), getVehicleType().getLength());
    7205        25175 :                     const MSLink* entry = (*link)->getCorrespondingEntryLink();
    7206              :                     //if (DEBUG_COND) {
    7207              :                     //    std::cout << SIMTIME << " veh=" << getID() << " changeTo=" << Named::getIDSecure(bestLaneConts.front()) << " linkState=" << toString((*link)->getState()) << " seen=" << seen << " dist=" << dist << " zipperDist=" << zipperDist << " aT=" << STEPS2TIME((*di).myArrivalTime) << " lT=" << STEPS2TIME(leaveTime) << "\n";
    7208              :                     //}
    7209        25175 :                     if (entry->hasApproachingFoe((*di).myArrivalTime, leaveTime, (*di).myArrivalSpeed, getCarFollowModel().getMaxDecel())) {
    7210              :                         //std::cout << SIMTIME << " veh=" << getID() << " aborting changeTo=" << Named::getIDSecure(bestLaneConts.front()) << " linkState=" << toString((*link)->getState()) << " seen=" << seen << " dist=" << dist << "\n";
    7211              :                         return true;
    7212              :                     }
    7213              :                 }
    7214              :                 // no drive item is found if the vehicle aborts its request within dist
    7215              :             }
    7216        47146 :             lane = (*link)->getViaLaneOrLane();
    7217        47146 :             if (!lane->getEdge().isInternal()) {
    7218        24642 :                 view++;
    7219              :             }
    7220        47146 :             seen += lane->getLength();
    7221        47146 :             link = MSLane::succLinkSec(*this, view, *lane, bestLaneConts);
    7222              :         }
    7223              :     }
    7224              :     return false;
    7225              : }
    7226              : 
    7227              : 
    7228              : PositionVector
    7229      6906570 : MSVehicle::getBoundingBox(double offset) const {
    7230      6906570 :     PositionVector centerLine;
    7231      6906570 :     Position pos = getPosition();
    7232      6906570 :     centerLine.push_back(pos);
    7233      6906570 :     switch (myType->getGuiShape()) {
    7234        29069 :         case SUMOVehicleShape::BUS_FLEXIBLE:
    7235              :         case SUMOVehicleShape::RAIL:
    7236              :         case SUMOVehicleShape::RAIL_CAR:
    7237              :         case SUMOVehicleShape::RAIL_CARGO:
    7238              :         case SUMOVehicleShape::TRUCK_SEMITRAILER:
    7239              :         case SUMOVehicleShape::TRUCK_1TRAILER: {
    7240        60781 :             for (MSLane* lane : myFurtherLanes) {
    7241        31712 :                 centerLine.push_back(lane->getShape().back());
    7242              :             }
    7243              :             break;
    7244              :         }
    7245              :         default:
    7246              :             break;
    7247              :     }
    7248      6906570 :     double l = getLength();
    7249      6906570 :     Position backPos = getBackPosition();
    7250      6906570 :     if (pos.distanceTo2D(backPos) > l + NUMERICAL_EPS) {
    7251              :         // getBackPosition may not match the visual back in networks without internal lanes
    7252       369172 :         double a = getAngle() + M_PI; // angle pointing backwards
    7253       369172 :         backPos = pos + Position(l * cos(a), l * sin(a));
    7254              :     }
    7255      6906570 :     centerLine.push_back(backPos);
    7256      6906570 :     if (offset != 0) {
    7257         6543 :         centerLine.extrapolate2D(offset);
    7258              :     }
    7259              :     PositionVector result = centerLine;
    7260     13808850 :     result.move2side(MAX2(0.0, 0.5 * myType->getWidth() + offset));
    7261     13808850 :     centerLine.move2side(MIN2(0.0, -0.5 * myType->getWidth() - offset));
    7262      6906570 :     result.append(centerLine.reverse(), POSITION_EPS);
    7263      6906570 :     return result;
    7264      6906570 : }
    7265              : 
    7266              : 
    7267              : PositionVector
    7268        73266 : MSVehicle::getBoundingPoly(double offset) const {
    7269        73266 :     switch (myType->getGuiShape()) {
    7270        72786 :         case SUMOVehicleShape::PASSENGER:
    7271              :         case SUMOVehicleShape::PASSENGER_SEDAN:
    7272              :         case SUMOVehicleShape::PASSENGER_HATCHBACK:
    7273              :         case SUMOVehicleShape::PASSENGER_WAGON:
    7274              :         case SUMOVehicleShape::PASSENGER_VAN: {
    7275              :             // box with corners cut off
    7276        72786 :             PositionVector result;
    7277        72786 :             PositionVector centerLine;
    7278        72786 :             centerLine.push_back(getPosition());
    7279        72786 :             centerLine.push_back(getBackPosition());
    7280        72786 :             if (offset != 0) {
    7281         1600 :                 centerLine.extrapolate2D(offset);
    7282              :             }
    7283              :             PositionVector line1 = centerLine;
    7284              :             PositionVector line2 = centerLine;
    7285       145572 :             line1.move2side(MAX2(0.0, 0.3 * myType->getWidth() + offset));
    7286       145572 :             line2.move2side(MAX2(0.0, 0.5 * myType->getWidth() + offset));
    7287        72786 :             line2.scaleRelative(0.8);
    7288        72786 :             result.push_back(line1[0]);
    7289        72786 :             result.push_back(line2[0]);
    7290        72786 :             result.push_back(line2[1]);
    7291        72786 :             result.push_back(line1[1]);
    7292       145572 :             line1.move2side(MIN2(0.0, -0.6 * myType->getWidth() - offset));
    7293       145572 :             line2.move2side(MIN2(0.0, -1.0 * myType->getWidth() - offset));
    7294        72786 :             result.push_back(line1[1]);
    7295        72786 :             result.push_back(line2[1]);
    7296        72786 :             result.push_back(line2[0]);
    7297        72786 :             result.push_back(line1[0]);
    7298              :             return result;
    7299        72786 :         }
    7300          480 :         default:
    7301          480 :             return getBoundingBox();
    7302              :     }
    7303              : }
    7304              : 
    7305              : 
    7306              : bool
    7307     12747702 : MSVehicle::onFurtherEdge(const MSEdge* edge) const {
    7308     13766107 :     for (std::vector<MSLane*>::const_iterator i = myFurtherLanes.begin(); i != myFurtherLanes.end(); ++i) {
    7309      1606922 :         if (&(*i)->getEdge() == edge) {
    7310              :             return true;
    7311              :         }
    7312              :     }
    7313              :     return false;
    7314              : }
    7315              : 
    7316              : 
    7317              : bool
    7318   7730555524 : MSVehicle::isBidiOn(const MSLane* lane) const {
    7319   7746689449 :     return lane->getBidiLane() != nullptr && (
    7320     16133925 :                myLane == lane->getBidiLane()
    7321     12747702 :                || onFurtherEdge(&lane->getBidiLane()->getEdge()));
    7322              : }
    7323              : 
    7324              : 
    7325              : bool
    7326           16 : MSVehicle::rerouteParkingArea(const std::string& parkingAreaID, std::string& errorMsg) {
    7327              :     // this function is based on MSTriggeredRerouter::rerouteParkingArea in order to keep
    7328              :     // consistency in the behaviour.
    7329              : 
    7330              :     // get vehicle params
    7331           16 :     MSParkingArea* destParkArea = getNextParkingArea();
    7332           16 :     const MSRoute& route = getRoute();
    7333           16 :     const MSEdge* lastEdge = route.getLastEdge();
    7334              : 
    7335           16 :     if (destParkArea == nullptr) {
    7336              :         // not driving towards a parking area
    7337            0 :         errorMsg = "Vehicle " + getID() + " is not driving to a parking area so it cannot be rerouted.";
    7338            0 :         return false;
    7339              :     }
    7340              : 
    7341              :     // if the current route ends at the parking area, the new route will also and at the new area
    7342           16 :     bool newDestination = (&destParkArea->getLane().getEdge() == route.getLastEdge()
    7343            8 :                            && getArrivalPos() >= destParkArea->getBeginLanePosition()
    7344           24 :                            && getArrivalPos() <= destParkArea->getEndLanePosition());
    7345              : 
    7346              :     // retrieve info on the new parking area
    7347           16 :     MSParkingArea* newParkingArea = (MSParkingArea*) MSNet::getInstance()->getStoppingPlace(
    7348              :                                         parkingAreaID, SumoXMLTag::SUMO_TAG_PARKING_AREA);
    7349              : 
    7350           16 :     if (newParkingArea == nullptr) {
    7351            0 :         errorMsg = "Parking area ID " + toString(parkingAreaID) + " not found in the network.";
    7352            0 :         return false;
    7353              :     }
    7354              : 
    7355           16 :     const MSEdge* newEdge = &(newParkingArea->getLane().getEdge());
    7356           16 :     SUMOAbstractRouter<MSEdge, SUMOVehicle>& router = getRouterTT();
    7357              : 
    7358              :     // Compute the route from the current edge to the parking area edge
    7359              :     ConstMSEdgeVector edgesToPark;
    7360           16 :     router.compute(getEdge(), getPositionOnLane(), newEdge, newParkingArea->getEndLanePosition(), this, MSNet::getInstance()->getCurrentTimeStep(), edgesToPark);
    7361              : 
    7362              :     // Compute the route from the parking area edge to the end of the route
    7363              :     ConstMSEdgeVector edgesFromPark;
    7364           16 :     if (!newDestination) {
    7365           12 :         router.compute(newEdge, lastEdge, this, MSNet::getInstance()->getCurrentTimeStep(), edgesFromPark);
    7366              :     } else {
    7367              :         // adapt plans of any riders
    7368            8 :         for (MSTransportable* p : getPersons()) {
    7369            4 :             p->rerouteParkingArea(getNextParkingArea(), newParkingArea);
    7370              :         }
    7371              :     }
    7372              : 
    7373              :     // we have a new destination, let's replace the vehicle route
    7374           16 :     ConstMSEdgeVector edges = edgesToPark;
    7375           16 :     if (edgesFromPark.size() > 0) {
    7376           12 :         edges.insert(edges.end(), edgesFromPark.begin() + 1, edgesFromPark.end());
    7377              :     }
    7378              : 
    7379           16 :     if (newDestination && getParameter().arrivalPosProcedure != ArrivalPosDefinition::DEFAULT) {
    7380            4 :         SUMOVehicleParameter* newParameter = new SUMOVehicleParameter();
    7381            4 :         *newParameter = getParameter();
    7382            4 :         newParameter->arrivalPosProcedure = ArrivalPosDefinition::GIVEN;
    7383            4 :         newParameter->arrivalPos = newParkingArea->getEndLanePosition();
    7384            4 :         replaceParameter(newParameter);
    7385              :     }
    7386           16 :     const double routeCost = router.recomputeCosts(edges, this, MSNet::getInstance()->getCurrentTimeStep());
    7387           16 :     ConstMSEdgeVector prevEdges(myCurrEdge, myRoute->end());
    7388           16 :     const double savings = router.recomputeCosts(prevEdges, this, MSNet::getInstance()->getCurrentTimeStep());
    7389           16 :     if (replaceParkingArea(newParkingArea, errorMsg)) {
    7390           16 :         const bool onInit = myLane == nullptr;
    7391           32 :         replaceRouteEdges(edges, routeCost, savings, "TraCI:" + toString(SUMO_TAG_PARKING_AREA_REROUTE), onInit, false, false);
    7392              :     } else {
    7393            0 :         WRITE_WARNINGF("Vehicle '%' could not reroute to new parkingArea '%' reason=%, time=%.",
    7394              :                 getID(), newParkingArea->getID(), errorMsg, time2string(SIMSTEP));
    7395            0 :         return false;
    7396              :     }
    7397           16 :     return true;
    7398           16 : }
    7399              : 
    7400              : 
    7401              : bool
    7402        43549 : MSVehicle::addTraciStop(SUMOVehicleParameter::Stop stop, std::string& errorMsg) {
    7403        43549 :     const int numStops = (int)myStops.size();
    7404        43549 :     const bool result = MSBaseVehicle::addTraciStop(stop, errorMsg);
    7405        43549 :     if (myLane != nullptr && numStops != (int)myStops.size()) {
    7406        42004 :         updateBestLanes(true);
    7407              :     }
    7408        43549 :     return result;
    7409              : }
    7410              : 
    7411              : 
    7412              : bool
    7413         3248 : MSVehicle::handleCollisionStop(MSStop& stop, const double distToStop) {
    7414         3248 :     if (myCurrEdge == stop.edge && distToStop + POSITION_EPS < getCarFollowModel().brakeGap(myState.mySpeed, getCarFollowModel().getMaxDecel(), 0)) {
    7415         1504 :         if (distToStop < getCarFollowModel().brakeGap(myState.mySpeed, getCarFollowModel().getEmergencyDecel(), 0)) {
    7416         1088 :             double vNew = getCarFollowModel().maximumSafeStopSpeed(distToStop, getCarFollowModel().getMaxDecel(), getSpeed(), false, 0);
    7417              :             //std::cout << SIMTIME << " veh=" << getID() << " v=" << myState.mySpeed << " distToStop=" << distToStop
    7418              :             //    << " vMinNex=" << getCarFollowModel().minNextSpeed(getSpeed(), this)
    7419              :             //    << " bg1=" << getCarFollowModel().brakeGap(myState.mySpeed)
    7420              :             //    << " bg2=" << getCarFollowModel().brakeGap(myState.mySpeed, getCarFollowModel().getEmergencyDecel(), 0)
    7421              :             //    << " vNew=" << vNew
    7422              :             //    << "\n";
    7423         1088 :             myState.mySpeed = MIN2(myState.mySpeed, vNew + ACCEL2SPEED(getCarFollowModel().getEmergencyDecel()));
    7424         1088 :             myState.myPos = MIN2(myState.myPos, stop.pars.endPos);
    7425         1088 :             myCachedPosition = Position::INVALID;
    7426         1088 :             if (myState.myPos < myType->getLength()) {
    7427          456 :                 computeFurtherLanes(myLane, myState.myPos, true);
    7428          456 :                 myAngle = computeAngle();
    7429          456 :                 if (myLaneChangeModel->isOpposite()) {
    7430            0 :                     myAngle += M_PI;
    7431              :                 }
    7432              :             }
    7433              :         }
    7434              :     }
    7435         3248 :     return true;
    7436              : }
    7437              : 
    7438              : 
    7439              : bool
    7440     24054703 : MSVehicle::resumeFromStopping() {
    7441     24054703 :     if (isStopped()) {
    7442        49287 :         if (myAmRegisteredAsWaiting) {
    7443          750 :             MSNet::getInstance()->getVehicleControl().unregisterOneWaiting();
    7444          750 :             myAmRegisteredAsWaiting = false;
    7445              :         }
    7446              :         MSStop& stop = myStops.front();
    7447              :         // we have waited long enough and fulfilled any passenger-requirements
    7448        49287 :         if (stop.busstop != nullptr) {
    7449              :             // inform bus stop about leaving it
    7450        18566 :             stop.busstop->leaveFrom(this);
    7451              :         }
    7452              :         // we have waited long enough and fulfilled any container-requirements
    7453        49287 :         if (stop.containerstop != nullptr) {
    7454              :             // inform container stop about leaving it
    7455          530 :             stop.containerstop->leaveFrom(this);
    7456              :         }
    7457        49287 :         if (stop.parkingarea != nullptr && stop.getSpeed() <= 0) {
    7458              :             // inform parking area about leaving it
    7459         8446 :             stop.parkingarea->leaveFrom(this);
    7460              :         }
    7461        49287 :         if (stop.chargingStation != nullptr) {
    7462              :             // inform charging station about leaving it
    7463         3571 :             stop.chargingStation->leaveFrom(this);
    7464              :         }
    7465              :         // the current stop is no longer valid
    7466        49287 :         myLane->getEdge().removeWaiting(this);
    7467              :         // MSStopOut needs to know whether the stop had a loaded 'ended' value so we call this before replacing the value
    7468        49287 :         if (stop.pars.started == -1) {
    7469              :             // waypoint edge was passed in a single step
    7470          334 :             stop.pars.started = MSNet::getInstance()->getCurrentTimeStep();
    7471              :         }
    7472        49287 :         if (MSStopOut::active()) {
    7473         4248 :             MSStopOut::getInstance()->stopEnded(this, stop);
    7474              :         }
    7475        49287 :         stop.pars.ended = MSNet::getInstance()->getCurrentTimeStep();
    7476       112188 :         for (const auto& rem : myMoveReminders) {
    7477        62901 :             rem.first->notifyStopEnded();
    7478              :         }
    7479        49287 :         if (stop.pars.collision && MSLane::getCollisionAction() == MSLane::COLLISION_ACTION_WARN) {
    7480          425 :             myCollisionImmunity = TIME2STEPS(5); // leave the conflict area
    7481              :         }
    7482        49287 :         if (stop.pars.posLat != INVALID_DOUBLE && MSGlobals::gLateralResolution <= 0) {
    7483              :             // reset lateral position to default
    7484          194 :             myState.myPosLat = 0;
    7485              :         }
    7486        49287 :         const bool wasWaypoint = stop.getSpeed() > 0;
    7487        49287 :         myPastStops.push_back(stop.pars);
    7488        49287 :         myPastStops.back().routeIndex = (int)(stop.edge - myRoute->begin());
    7489        49287 :         myStops.pop_front();
    7490        49287 :         myStopDist = std::numeric_limits<double>::max();
    7491              :         // do not count the stopping time towards gridlock time.
    7492              :         // Other outputs use an independent counter and are not affected.
    7493        49287 :         myWaitingTime = 0;
    7494        49287 :         myStopSpeed = getCarFollowModel().maxNextSpeed(getSpeed(), this);
    7495              :         // maybe the next stop is on the same edge; let's rebuild best lanes
    7496        49287 :         updateBestLanes(true);
    7497              :         // continue as wished...
    7498        49287 :         MSNet::getInstance()->informVehicleStateListener(this, MSNet::VehicleState::ENDING_STOP);
    7499        49287 :         MSNet::getInstance()->getVehicleControl().registerStopEnded();
    7500        49287 :         return !wasWaypoint;
    7501              :     }
    7502              :     return false;
    7503              : }
    7504              : 
    7505              : 
    7506              : MSVehicle::Influencer&
    7507      4510820 : MSVehicle::getInfluencer() {
    7508      4510820 :     if (myInfluencer == nullptr) {
    7509         3437 :         myInfluencer = new Influencer();
    7510              :     }
    7511      4510820 :     return *myInfluencer;
    7512              : }
    7513              : 
    7514              : MSVehicle::BaseInfluencer&
    7515           24 : MSVehicle::getBaseInfluencer() {
    7516           24 :     return getInfluencer();
    7517              : }
    7518              : 
    7519              : 
    7520              : const MSVehicle::Influencer*
    7521            0 : MSVehicle::getInfluencer() const {
    7522            0 :     return myInfluencer;
    7523              : }
    7524              : 
    7525              : const MSVehicle::BaseInfluencer*
    7526       236572 : MSVehicle::getBaseInfluencer() const {
    7527       236572 :     return myInfluencer;
    7528              : }
    7529              : 
    7530              : 
    7531              : double
    7532         4078 : MSVehicle::getSpeedWithoutTraciInfluence() const {
    7533         4078 :     if (myInfluencer != nullptr && myInfluencer->getOriginalSpeed() >= 0) {
    7534              :         // influencer original speed is -1 on initialization
    7535         1655 :         return myInfluencer->getOriginalSpeed();
    7536              :     }
    7537         2423 :     return myState.mySpeed;
    7538              : }
    7539              : 
    7540              : 
    7541              : int
    7542    997918075 : MSVehicle::influenceChangeDecision(int state) {
    7543    997918075 :     if (hasInfluencer()) {
    7544      2800329 :         state = getInfluencer().influenceChangeDecision(
    7545              :                     MSNet::getInstance()->getCurrentTimeStep(),
    7546      2800329 :                     myLane->getEdge(),
    7547              :                     getLaneIndex(),
    7548              :                     state);
    7549              :     }
    7550    997918075 :     return state;
    7551              : }
    7552              : 
    7553              : 
    7554              : void
    7555         7327 : MSVehicle::setRemoteState(Position xyPos) {
    7556         7327 :     myCachedPosition = xyPos;
    7557         7327 : }
    7558              : 
    7559              : 
    7560              : bool
    7561    792139063 : MSVehicle::isRemoteControlled() const {
    7562    792139063 :     return myInfluencer != nullptr && myInfluencer->isRemoteControlled();
    7563              : }
    7564              : 
    7565              : 
    7566              : bool
    7567        20629 : MSVehicle::wasRemoteControlled(SUMOTime lookBack) const {
    7568        20629 :     return myInfluencer != nullptr && myInfluencer->getLastAccessTimeStep() + lookBack >= MSNet::getInstance()->getCurrentTimeStep();
    7569              : }
    7570              : 
    7571              : 
    7572              : bool
    7573    523458896 : MSVehicle::keepClear(const MSLink* link) const {
    7574    523458896 :     if (link->hasFoes() && link->keepClear() /* && item.myLink->willHaveBlockedFoe()*/) {
    7575    174599773 :         const double keepClearTime = getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_IGNORE_KEEPCLEAR_TIME, -1);
    7576              :         //std::cout << SIMTIME << " veh=" << getID() << " keepClearTime=" << keepClearTime << " accWait=" << getAccumulatedWaitingSeconds() << " keepClear=" << (keepClearTime < 0 || getAccumulatedWaitingSeconds() < keepClearTime) << "\n";
    7577    176021955 :         return keepClearTime < 0 || getAccumulatedWaitingSeconds() < keepClearTime;
    7578              :     } else {
    7579              :         return false;
    7580              :     }
    7581              : }
    7582              : 
    7583              : 
    7584              : bool
    7585    725605579 : MSVehicle::ignoreRed(const MSLink* link, bool canBrake) const {
    7586    725605579 :     if ((myInfluencer != nullptr && !myInfluencer->getEmergencyBrakeRedLight())) {
    7587              :         return true;
    7588              :     }
    7589    725298809 :     const double ignoreRedTime = getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_DRIVE_AFTER_RED_TIME, -1);
    7590              : #ifdef DEBUG_IGNORE_RED
    7591              :     if (DEBUG_COND) {
    7592              :         std::cout << SIMTIME << " veh=" << getID() << " link=" << link->getViaLaneOrLane()->getID() << " state=" << toString(link->getState()) << "\n";
    7593              :     }
    7594              : #endif
    7595    725298809 :     if (ignoreRedTime < 0) {
    7596    725293410 :         const double ignoreYellowTime = getVehicleType().getParameter().getJMParam(SUMO_ATTR_JM_DRIVE_AFTER_YELLOW_TIME, 0);
    7597    725293410 :         if (ignoreYellowTime > 0 && link->haveYellow()) {
    7598              :             assert(link->getTLLogic() != 0);
    7599           52 :             const double yellowDuration = STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - link->getLastStateChange());
    7600              :             // when activating ignoreYellow behavior, vehicles will drive if they cannot brake
    7601           92 :             return !canBrake || ignoreYellowTime > yellowDuration;
    7602              :         } else {
    7603              :             return false;
    7604              :         }
    7605         5399 :     } else if (link->haveYellow()) {
    7606              :         // always drive at yellow when ignoring red
    7607              :         return true;
    7608         5243 :     } else if (link->haveRed()) {
    7609              :         assert(link->getTLLogic() != 0);
    7610         3832 :         const double redDuration = STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - link->getLastStateChange());
    7611              : #ifdef DEBUG_IGNORE_RED
    7612              :         if (DEBUG_COND) {
    7613              :             std::cout
    7614              :             // << SIMTIME << " veh=" << getID() << " link=" << link->getViaLaneOrLane()->getID()
    7615              :                     << "   ignoreRedTime=" << ignoreRedTime
    7616              :                     << " spentRed=" << redDuration
    7617              :                     << " canBrake=" << canBrake << "\n";
    7618              :         }
    7619              : #endif
    7620              :         // when activating ignoreRed behavior, vehicles will always drive if they cannot brake
    7621         6356 :         return !canBrake || ignoreRedTime > redDuration;
    7622              :     } else {
    7623              :         return false;
    7624              :     }
    7625              : }
    7626              : 
    7627              : bool
    7628   1314515126 : MSVehicle::ignoreFoe(const SUMOTrafficObject* foe) const {
    7629   1314515126 :     if (!getParameter().wasSet(VEHPARS_CFMODEL_PARAMS_SET)) {
    7630              :         return false;
    7631              :     }
    7632         2548 :     for (const std::string& typeID : StringTokenizer(getParameter().getParameter(toString(SUMO_ATTR_CF_IGNORE_TYPES), "")).getVector()) {
    7633          398 :         if (typeID == foe->getVehicleType().getID()) {
    7634              :             return true;
    7635              :         }
    7636         1274 :     }
    7637         2161 :     for (const std::string& id : StringTokenizer(getParameter().getParameter(toString(SUMO_ATTR_CF_IGNORE_IDS), "")).getVector()) {
    7638          876 :         if (id == foe->getID()) {
    7639              :             return true;
    7640              :         }
    7641          876 :     }
    7642          409 :     return false;
    7643              : }
    7644              : 
    7645              : bool
    7646    549981867 : MSVehicle::passingMinor() const {
    7647              :     // either on an internal lane that was entered via minor link
    7648              :     // or on approach to minor link below visibility distance
    7649    549981867 :     if (myLane == nullptr) {
    7650              :         return false;
    7651              :     }
    7652    549981867 :     if (myLane->getEdge().isInternal()) {
    7653     10304676 :         return !myLane->getIncomingLanes().front().viaLink->havePriority();
    7654    539677191 :     } else if (myLFLinkLanes.size() > 0 && myLFLinkLanes.front().myLink != nullptr) {
    7655              :         MSLink* link = myLFLinkLanes.front().myLink;
    7656    281222398 :         return !link->havePriority() && myLFLinkLanes.front().myDistance <= link->getFoeVisibilityDistance();
    7657              :     }
    7658              :     return false;
    7659              : }
    7660              : 
    7661              : bool
    7662     21433691 : MSVehicle::isLeader(const MSLink* link, const MSVehicle* veh, const double gap) const {
    7663              :     assert(link->fromInternalLane());
    7664     21433691 :     if (veh == nullptr) {
    7665              :         return false;
    7666              :     }
    7667     21433691 :     if (!myLane->isInternal() || myLane->getEdge().getToJunction() != link->getJunction()) {
    7668              :         // if this vehicle is not yet on the junction, every vehicle is a leader
    7669              :         return true;
    7670              :     }
    7671      1900346 :     if (veh->getLaneChangeModel().hasBlueLight()) {
    7672              :         // blue light device automatically gets right of way
    7673              :         return true;
    7674              :     }
    7675      1900078 :     const MSLane* foeLane = veh->getLane();
    7676      1900078 :     if (foeLane->isInternal()) {
    7677      1411996 :         if (foeLane->getEdge().getFromJunction() == link->getJunction()) {
    7678      1393544 :             SUMOTime egoET = myJunctionConflictEntryTime;
    7679      1393544 :             SUMOTime foeET = veh->myJunctionEntryTime;
    7680              :             // check relationship between link and foeLane
    7681      1393544 :             if (foeLane->getNormalPredecessorLane() == link->getInternalLaneBefore()->getNormalPredecessorLane()) {
    7682              :                 // we are entering the junction from the same lane
    7683       451114 :                 egoET = myJunctionEntryTimeNeverYield;
    7684       451114 :                 foeET = veh->myJunctionEntryTimeNeverYield;
    7685       451114 :                 if (link->isExitLinkAfterInternalJunction() && link->getInternalLaneBefore()->getLogicalPredecessorLane()->getEntryLink()->isIndirect()) {
    7686        91633 :                     egoET = myJunctionConflictEntryTime;
    7687              :                 }
    7688              :             } else {
    7689       942430 :                 const MSLink* foeLink = foeLane->getIncomingLanes()[0].viaLink;
    7690       942430 :                 const MSJunctionLogic* logic = link->getJunction()->getLogic();
    7691              :                 assert(logic != nullptr);
    7692              :                 // determine who has right of way
    7693              :                 bool response; // ego response to foe
    7694              :                 bool response2; // foe response to ego
    7695              :                 // attempt 1: tlLinkState
    7696       942430 :                 const MSLink* entry = link->getCorrespondingEntryLink();
    7697       942430 :                 const MSLink* foeEntry = foeLink->getCorrespondingEntryLink();
    7698       942430 :                 if (entry->haveRed() || foeEntry->haveRed()) {
    7699              :                     // ensure that vehicles which are stuck on the intersection may exit
    7700       116645 :                     if (!foeEntry->haveRed() && veh->getSpeed() > SUMO_const_haltingSpeed && gap < 0) {
    7701              :                         // foe might be oncoming, don't drive unless foe can still brake safely
    7702        11901 :                         const double foeNextSpeed = veh->getSpeed() + ACCEL2SPEED(veh->getCarFollowModel().getMaxAccel());
    7703        11901 :                         const double foeBrakeGap = veh->getCarFollowModel().brakeGap(
    7704        11901 :                                                        foeNextSpeed, veh->getCarFollowModel().getMaxDecel(), veh->getCarFollowModel().getHeadwayTime());
    7705              :                         // the minGap was subtracted from gap in MSLink::getLeaderInfo (enlarging the negative gap)
    7706              :                         // so the -2* makes it point in the right direction
    7707        11901 :                         const double foeGap = -gap - veh->getLength() - 2 * getVehicleType().getMinGap();
    7708              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    7709              :                         if (DEBUG_COND) {
    7710              :                             std::cout << " foeGap=" << foeGap << " foeBGap=" << foeBrakeGap << "\n";
    7711              : 
    7712              :                         }
    7713              : #endif
    7714        11901 :                         if (foeGap < foeBrakeGap) {
    7715              :                             response = true;
    7716              :                             response2 = false;
    7717              :                         } else {
    7718              :                             response = false;
    7719              :                             response2 = true;
    7720              :                         }
    7721              :                     } else {
    7722              :                         // let conflict entry time decide
    7723              :                         response = true;
    7724              :                         response2 = true;
    7725              :                     }
    7726       825785 :                 } else if (entry->havePriority() != foeEntry->havePriority()) {
    7727       612431 :                     response = !entry->havePriority();
    7728       612431 :                     response2 = !foeEntry->havePriority();
    7729       213354 :                 } else if (entry->haveYellow() && foeEntry->haveYellow()) {
    7730              :                     // let the faster vehicle keep moving
    7731         5756 :                     response = veh->getSpeed() >= getSpeed();
    7732         5756 :                     response2 = getSpeed() >= veh->getSpeed();
    7733              :                 } else {
    7734              :                     // fallback if pedestrian crossings are involved
    7735       207598 :                     response = logic->getResponseFor(link->getIndex()).test(foeLink->getIndex());
    7736       207598 :                     response2 = logic->getResponseFor(foeLink->getIndex()).test(link->getIndex());
    7737              :                 }
    7738              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    7739              :                 if (DEBUG_COND) {
    7740              :                     std::cout << SIMTIME
    7741              :                               << " foeLane=" << foeLane->getID()
    7742              :                               << " foeLink=" << foeLink->getViaLaneOrLane()->getID()
    7743              :                               << " linkIndex=" << link->getIndex()
    7744              :                               << " foeLinkIndex=" << foeLink->getIndex()
    7745              :                               << " entryState=" << toString(entry->getState())
    7746              :                               << " entryState2=" << toString(foeEntry->getState())
    7747              :                               << " response=" << response
    7748              :                               << " response2=" << response2
    7749              :                               << "\n";
    7750              :                 }
    7751              : #endif
    7752       942430 :                 if (!response) {
    7753              :                     // if we have right of way over the foe, entryTime does not matter
    7754        80175 :                     foeET = veh->myJunctionConflictEntryTime;
    7755        80175 :                     egoET = myJunctionEntryTime;
    7756       862255 :                 } else if (response && response2) {
    7757              :                     // in a mutual conflict scenario, use entry time to avoid deadlock
    7758       128034 :                     foeET = veh->myJunctionConflictEntryTime;
    7759       128034 :                     egoET = myJunctionConflictEntryTime;
    7760              :                 }
    7761              :             }
    7762      1393544 :             if (egoET == foeET) {
    7763              :                 // try to use speed as tie braker
    7764       105497 :                 if (getSpeed() == veh->getSpeed()) {
    7765              :                     // use ID as tie braker
    7766              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    7767              :                     if (DEBUG_COND) {
    7768              :                         std::cout << SIMTIME << " veh=" << getID() << " equal ET " << egoET << " with foe " << veh->getID()
    7769              :                                   << " foeIsLeaderByID=" << (getID() < veh->getID()) << "\n";
    7770              :                     }
    7771              : #endif
    7772        56101 :                     return getID() < veh->getID();
    7773              :                 } else {
    7774              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    7775              :                     if (DEBUG_COND) {
    7776              :                         std::cout << SIMTIME << " veh=" << getID() << " equal ET " << egoET << " with foe " << veh->getID()
    7777              :                                   << " foeIsLeaderBySpeed=" << (getSpeed() < veh->getSpeed())
    7778              :                                   << " v=" << getSpeed() << " foeV=" << veh->getSpeed()
    7779              :                                   << "\n";
    7780              :                     }
    7781              : #endif
    7782        49396 :                     return getSpeed() < veh->getSpeed();
    7783              :                 }
    7784              :             } else {
    7785              :                 // leader was on the junction first
    7786              : #ifdef DEBUG_PLAN_MOVE_LEADERINFO
    7787              :                 if (DEBUG_COND) {
    7788              :                     std::cout << SIMTIME << " veh=" << getID() << " egoET " << egoET << " with foe " << veh->getID()
    7789              :                               << " foeET=" << foeET  << " isLeader=" << (egoET > foeET) << "\n";
    7790              :                 }
    7791              : #endif
    7792      1288047 :                 return egoET > foeET;
    7793              :             }
    7794              :         } else {
    7795              :             // vehicle can only be partially on the junction. Must be a leader
    7796              :             return true;
    7797              :         }
    7798              :     } else {
    7799              :         // vehicle can only be partially on the junction. Must be a leader
    7800              :         return true;
    7801              :     }
    7802              : }
    7803              : 
    7804              : void
    7805         2615 : MSVehicle::saveState(OutputDevice& out) {
    7806         2615 :     MSBaseVehicle::saveState(out);
    7807              :     // here starts the vehicle internal part (see loading)
    7808              :     std::vector<std::string> internals;
    7809         2615 :     internals.push_back(toString(myParameter->parametersSet));
    7810         2615 :     internals.push_back(toString(myDeparture));
    7811         2615 :     internals.push_back(toString(distance(myRoute->begin(), myCurrEdge)));
    7812         2615 :     internals.push_back(toString(myDepartPos));
    7813         2615 :     internals.push_back(toString(myWaitingTime));
    7814         2615 :     internals.push_back(toString(myTimeLoss));
    7815         2615 :     internals.push_back(toString(myLastActionTime));
    7816         2615 :     internals.push_back(toString(isStopped()));
    7817         2615 :     internals.push_back(toString(isStopped() ? myStops.front().duration : 0));
    7818         2615 :     internals.push_back(toString(myPastStops.size()));
    7819         2615 :     out.writeAttr(SUMO_ATTR_STATE, internals);
    7820         2615 :     out.writeAttr(SUMO_ATTR_POSITION, std::vector<double> { myState.myPos, myState.myBackPos, myState.myLastCoveredDist });
    7821         2615 :     out.writeAttr(SUMO_ATTR_SPEED, std::vector<double> { myState.mySpeed, myState.myPreviousSpeed });
    7822         2615 :     out.writeAttr(SUMO_ATTR_ANGLE, GeomHelper::naviDegree(myAngle));
    7823         2615 :     out.writeAttr(SUMO_ATTR_POSITION_LAT, myState.myPosLat);
    7824         2615 :     out.writeAttr(SUMO_ATTR_WAITINGTIME, myWaitingTimeCollector.getState());
    7825         2615 :     if (isStopped() && myStops.front().entryPos != getPositionOnLane()) {
    7826            2 :         out.writeAttr(SUMO_ATTR_ENTRYPOS, myStops.front().entryPos);
    7827              :     }
    7828         2615 :     myLaneChangeModel->saveState(out);
    7829              :     // save past stops
    7830         5678 :     for (SUMOVehicleParameter::Stop stop : myPastStops) {
    7831         3063 :         stop.write(out, false);
    7832              :         // do not write started and ended twice
    7833         3063 :         if ((stop.parametersSet & STOP_STARTED_SET) == 0) {
    7834         3058 :             out.writeAttr(SUMO_ATTR_STARTED, time2string(stop.started));
    7835              :         }
    7836         3063 :         if ((stop.parametersSet & STOP_ENDED_SET) == 0) {
    7837         3058 :             out.writeAttr(SUMO_ATTR_ENDED, time2string(stop.ended));
    7838              :         }
    7839         3063 :         stop.writeParams(out);
    7840         3063 :         out.closeTag();
    7841         3063 :     }
    7842              :     // save upcoming stops
    7843         3115 :     for (MSStop& stop : myStops) {
    7844          500 :         stop.write(out);
    7845              :     }
    7846              :     // save parameters and device states
    7847         2615 :     myParameter->writeParams(out);
    7848         6599 :     for (MSVehicleDevice* const dev : myDevices) {
    7849         3984 :         dev->saveState(out);
    7850              :     }
    7851         2615 :     if (myCFVariables != nullptr) {
    7852          100 :         myCFVariables->saveState(out, getCarFollowModel());
    7853              :     }
    7854         2615 :     out.closeTag();
    7855         2615 : }
    7856              : 
    7857              : void
    7858         3472 : MSVehicle::loadState(const SUMOSAXAttributes& attrs, const SUMOTime offset) {
    7859         3472 :     if (!attrs.hasAttribute(SUMO_ATTR_POSITION)) {
    7860            0 :         throw ProcessError(TL("Error: Invalid vehicles in state (may be a meso state)!"));
    7861              :     }
    7862              :     bool ok;
    7863              :     int routeOffset;
    7864              :     bool stopped;
    7865              :     SUMOTime stopDuration;
    7866              :     int pastStops;
    7867              : 
    7868         3472 :     std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
    7869         3472 :     bis >> myParameter->parametersSet;
    7870         3472 :     bis >> myDeparture;
    7871         3472 :     bis >> routeOffset;
    7872         3472 :     bis >> myDepartPos;
    7873         3472 :     bis >> myWaitingTime;
    7874         3472 :     bis >> myTimeLoss;
    7875         3472 :     bis >> myLastActionTime;
    7876              :     bis >> stopped;
    7877              :     bis >> stopDuration;
    7878         3472 :     bis >> pastStops;
    7879              : 
    7880         3472 :     if (attrs.hasAttribute(SUMO_ATTR_ARRIVALPOS_RANDOMIZED)) {
    7881            4 :         myArrivalPos = attrs.get<double>(SUMO_ATTR_ARRIVALPOS_RANDOMIZED, getID().c_str(), ok);
    7882              :     }
    7883              :     // load stops
    7884              :     myStops.clear();
    7885         3472 :     addStops(!MSGlobals::gCheckRoutes, &myCurrEdge, false);
    7886              : 
    7887         3472 :     if (hasDeparted()) {
    7888         1667 :         myCurrEdge = myRoute->begin() + routeOffset;
    7889         1667 :         myDeparture -= offset;
    7890              :         // fix stops
    7891         4706 :         while (pastStops > 0) {
    7892              :             SUMOVehicleParameter::Stop& pars = const_cast<SUMOVehicleParameter::Stop&>(myStops.front().pars);
    7893              :             // assumed these attributes were only added to restore vehroute-output.exit-times
    7894         3039 :             if (!MSGlobals::gUseStopEnded) {
    7895         3039 :                 pars.parametersSet &= ~STOP_ENDED_SET;
    7896              :             }
    7897         3039 :             if (!MSGlobals::gUseStopStarted) {
    7898         3039 :                 pars.parametersSet &= ~STOP_STARTED_SET;
    7899              :             }
    7900         6103 :             for (const auto& rem : myMoveReminders) {
    7901         3064 :                 rem.first->notifyStopEnded();
    7902              :             }
    7903         3039 :             myPastStops.push_back(myStops.front().pars);
    7904         3039 :             myPastStops.back().routeIndex = (int)(myStops.front().edge - myRoute->begin());
    7905         3039 :             myStops.pop_front();
    7906         3039 :             pastStops--;
    7907              :         }
    7908              :         // see MSBaseVehicle constructor
    7909         1667 :         if (myParameter->wasSet(VEHPARS_FORCE_REROUTE)) {
    7910         1147 :             calculateArrivalParams(true);
    7911              :         }
    7912              :         // a (tentative lane is needed for calling hasArrivedInternal
    7913         1667 :         myLane = (*myCurrEdge)->getLanes()[0];
    7914              :     }
    7915         3472 :     if (getActionStepLength() == DELTA_T && !isActionStep(SIMSTEP)) {
    7916            1 :         myLastActionTime -= (myLastActionTime - SIMSTEP) % DELTA_T;
    7917            3 :         WRITE_WARNINGF(TL("Action steps are out of sync for loaded vehicle '%'."), getID());
    7918              :     }
    7919         3472 :     std::istringstream pis(attrs.getString(SUMO_ATTR_POSITION));
    7920         3472 :     pis >> myState.myPos >> myState.myBackPos >> myState.myLastCoveredDist;
    7921         3472 :     std::istringstream sis(attrs.getString(SUMO_ATTR_SPEED));
    7922         3472 :     sis >> myState.mySpeed >> myState.myPreviousSpeed;
    7923         3472 :     myAcceleration = SPEED2ACCEL(myState.mySpeed - myState.myPreviousSpeed);
    7924         3472 :     myAngle = GeomHelper::fromNaviDegree(attrs.getFloat(SUMO_ATTR_ANGLE));
    7925         3472 :     myRawAngle = myAngle;
    7926         3472 :     myState.myPosLat = attrs.getFloat(SUMO_ATTR_POSITION_LAT);
    7927         3472 :     std::istringstream dis(attrs.getString(SUMO_ATTR_DISTANCE));
    7928         3472 :     dis >> myOdometer >> myNumberReroutes;
    7929         3472 :     myWaitingTimeCollector.setState(attrs.getString(SUMO_ATTR_WAITINGTIME));
    7930         3472 :     if (stopped) {
    7931          221 :         double realPos = getPositionOnLane();
    7932          221 :         double entryPos = attrs.getOpt<double>(SUMO_ATTR_ENTRYPOS, getID().c_str(), ok, realPos);
    7933          221 :         myStops.front().startedFromState = true;
    7934          221 :         if (entryPos != realPos) {
    7935            2 :             myStops.front().entryPos = entryPos;
    7936              :         }
    7937          221 :         myLane = const_cast<MSLane*>(myStops.front().lane);
    7938          221 :         myStopDist = 0;
    7939          221 :         myState.myPos = entryPos; // fake position for replication stop entry which happened before the position was updated
    7940          221 :         processNextStop(getSpeed());
    7941          221 :         myState.myPos = realPos; // reset fake position
    7942          221 :         if (myStops.front().pars.parking != ParkingType::ONROAD) {
    7943              :             // processNextStop is called again during MSVehicleTransfer::loadState
    7944          203 :             stopDuration += getActionStepLength();
    7945              :         }
    7946          221 :         myStops.front().duration = stopDuration;
    7947          221 :         if (!MSGlobals::gUseStopStarted) {
    7948              :             SUMOVehicleParameter::Stop& pars = const_cast<SUMOVehicleParameter::Stop&>(myStops.front().pars);
    7949          221 :             pars.parametersSet &= ~STOP_STARTED_SET;
    7950              :         }
    7951              :     }
    7952         3472 :     myLaneChangeModel->loadState(attrs);
    7953              :     // no need to reset myCachedPosition here since state loading happens directly after creation
    7954         3472 : }
    7955              : 
    7956              : void
    7957           32 : MSVehicle::loadPreviousApproaching(MSLink* link, bool setRequest,
    7958              :                                    SUMOTime arrivalTime, double arrivalSpeed,
    7959              :                                    double arrivalSpeedBraking,
    7960              :                                    double dist, double leaveSpeed) {
    7961              :     // ensure that approach information is reset on the next call to setApproachingForAllLinks
    7962           32 :     myLFLinkLanes.push_back(DriveProcessItem(link, 0, 0, setRequest,
    7963              :                             arrivalTime, arrivalSpeed, arrivalSpeedBraking, dist, leaveSpeed));
    7964              : 
    7965           32 : }
    7966              : 
    7967              : 
    7968              : std::shared_ptr<MSSimpleDriverState>
    7969      2564956 : MSVehicle::getDriverState() const {
    7970      2564956 :     return myDriverState->getDriverState();
    7971              : }
    7972              : 
    7973              : 
    7974              : double
    7975    623098030 : MSVehicle::getFriction() const {
    7976    623098030 :     return myFrictionDevice == nullptr ? 1. : myFrictionDevice->getMeasuredFriction();
    7977              : }
    7978              : 
    7979              : 
    7980              : void
    7981          196 : MSVehicle::setPreviousSpeed(double prevSpeed, double prevAcceleration) {
    7982          196 :     myState.mySpeed = MAX2(0., prevSpeed);
    7983              :     // also retcon acceleration
    7984          196 :     if (prevAcceleration != std::numeric_limits<double>::min()) {
    7985            8 :         myAcceleration = prevAcceleration;
    7986              :     } else {
    7987          188 :         myAcceleration = SPEED2ACCEL(myState.mySpeed - myState.myPreviousSpeed);
    7988              :     }
    7989          196 : }
    7990              : 
    7991              : 
    7992              : double
    7993   1871251018 : MSVehicle::getCurrentApparentDecel() const {
    7994              :     //return MAX2(-myAcceleration, getCarFollowModel().getApparentDecel());
    7995   1871251018 :     return getCarFollowModel().getApparentDecel();
    7996              : }
    7997              : 
    7998              : /****************************************************************************/
    7999              : bool
    8000           32 : MSVehicle::setExitManoeuvre() {
    8001           32 :     return (myManoeuvre.configureExitManoeuvre(this));
    8002              : }
    8003              : 
    8004              : /* -------------------------------------------------------------------------
    8005              :  * methods of MSVehicle::manoeuvre
    8006              :  * ----------------------------------------------------------------------- */
    8007              : 
    8008      4504622 : MSVehicle::Manoeuvre::Manoeuvre() : myManoeuvreStop(""), myManoeuvreStartTime(0), myManoeuvreCompleteTime(0), myManoeuvreType(MSVehicle::MANOEUVRE_NONE), myGUIIncrement(0) {}
    8009              : 
    8010              : 
    8011            0 : MSVehicle::Manoeuvre::Manoeuvre(const Manoeuvre& manoeuvre) {
    8012            0 :     myManoeuvreStop = manoeuvre.myManoeuvreStop;
    8013            0 :     myManoeuvreStartTime = manoeuvre.myManoeuvreStartTime;
    8014            0 :     myManoeuvreCompleteTime = manoeuvre.myManoeuvreCompleteTime;
    8015            0 :     myManoeuvreType = manoeuvre.myManoeuvreType;
    8016            0 :     myGUIIncrement = manoeuvre.myGUIIncrement;
    8017            0 : }
    8018              : 
    8019              : 
    8020              : MSVehicle::Manoeuvre&
    8021            0 : MSVehicle::Manoeuvre::operator=(const Manoeuvre& manoeuvre) {
    8022            0 :     myManoeuvreStop = manoeuvre.myManoeuvreStop;
    8023            0 :     myManoeuvreStartTime = manoeuvre.myManoeuvreStartTime;
    8024            0 :     myManoeuvreCompleteTime = manoeuvre.myManoeuvreCompleteTime;
    8025            0 :     myManoeuvreType = manoeuvre.myManoeuvreType;
    8026            0 :     myGUIIncrement = manoeuvre.myGUIIncrement;
    8027            0 :     return *this;
    8028              : }
    8029              : 
    8030              : 
    8031              : bool
    8032            0 : MSVehicle::Manoeuvre::operator!=(const Manoeuvre& manoeuvre) {
    8033            0 :     return (myManoeuvreStop != manoeuvre.myManoeuvreStop ||
    8034            0 :             myManoeuvreStartTime != manoeuvre.myManoeuvreStartTime ||
    8035            0 :             myManoeuvreCompleteTime != manoeuvre.myManoeuvreCompleteTime ||
    8036            0 :             myManoeuvreType != manoeuvre.myManoeuvreType ||
    8037            0 :             myGUIIncrement != manoeuvre.myGUIIncrement
    8038            0 :            );
    8039              : }
    8040              : 
    8041              : 
    8042              : double
    8043          450 : MSVehicle::Manoeuvre::getGUIIncrement() const {
    8044          450 :     return (myGUIIncrement);
    8045              : }
    8046              : 
    8047              : 
    8048              : MSVehicle::ManoeuvreType
    8049         2971 : MSVehicle::Manoeuvre::getManoeuvreType() const {
    8050         2971 :     return (myManoeuvreType);
    8051              : }
    8052              : 
    8053              : 
    8054              : MSVehicle::ManoeuvreType
    8055         2971 : MSVehicle::getManoeuvreType() const {
    8056         2971 :     return (myManoeuvre.getManoeuvreType());
    8057              : }
    8058              : 
    8059              : 
    8060              : void
    8061           30 : MSVehicle::setManoeuvreType(const MSVehicle::ManoeuvreType mType) {
    8062           30 :     myManoeuvre.setManoeuvreType(mType);
    8063           30 : }
    8064              : 
    8065              : 
    8066              : void
    8067           30 : MSVehicle::Manoeuvre::setManoeuvreType(const MSVehicle::ManoeuvreType mType) {
    8068           30 :     myManoeuvreType = mType;
    8069           30 : }
    8070              : 
    8071              : 
    8072              : bool
    8073           30 : MSVehicle::Manoeuvre::configureEntryManoeuvre(MSVehicle* veh) {
    8074           30 :     if (!veh->hasStops()) {
    8075              :         return false;    // should never happen - checked before call
    8076              :     }
    8077              : 
    8078           30 :     const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
    8079           30 :     const MSStop& stop = veh->getNextStop();
    8080              : 
    8081           30 :     int manoeuverAngle = stop.parkingarea->getLastFreeLotAngle();
    8082           30 :     double GUIAngle = stop.parkingarea->getLastFreeLotGUIAngle();
    8083           30 :     if (abs(GUIAngle) < 0.1) {
    8084              :         GUIAngle = -0.1;    // Wiggle vehicle on parallel entry
    8085              :     }
    8086           30 :     myManoeuvreVehicleID = veh->getID();
    8087           30 :     myManoeuvreStop = stop.parkingarea->getID();
    8088           30 :     myManoeuvreType = MSVehicle::MANOEUVRE_ENTRY;
    8089           30 :     myManoeuvreStartTime = currentTime;
    8090           30 :     myManoeuvreCompleteTime = currentTime + veh->myType->getEntryManoeuvreTime(manoeuverAngle);
    8091           30 :     myGUIIncrement = GUIAngle / (STEPS2TIME(myManoeuvreCompleteTime - myManoeuvreStartTime) / TS);
    8092              : 
    8093              : #ifdef DEBUG_STOPS
    8094              :     if (veh->isSelected()) {
    8095              :         std::cout << "ENTRY manoeuvre start: vehicle=" << veh->getID() << " Manoeuvre Angle=" << manoeuverAngle << " Rotation angle=" << RAD2DEG(GUIAngle) << " Road Angle" << RAD2DEG(veh->getAngle()) << " increment=" << RAD2DEG(myGUIIncrement) << " currentTime=" << currentTime <<
    8096              :                   " endTime=" << myManoeuvreCompleteTime << " manoeuvre time=" << myManoeuvreCompleteTime - currentTime << " parkArea=" << myManoeuvreStop << std::endl;
    8097              :     }
    8098              : #endif
    8099              : 
    8100           30 :     return (true);
    8101              : }
    8102              : 
    8103              : 
    8104              : bool
    8105           32 : MSVehicle::Manoeuvre::configureExitManoeuvre(MSVehicle* veh) {
    8106              :     // At the moment we only want to set for parking areas
    8107           32 :     if (!veh->hasStops()) {
    8108              :         return true;
    8109              :     }
    8110           32 :     if (veh->getNextStop().parkingarea == nullptr) {
    8111              :         return true;
    8112              :     }
    8113              : 
    8114           30 :     if (myManoeuvreType != MSVehicle::MANOEUVRE_NONE) {
    8115              :         return (false);
    8116              :     }
    8117              : 
    8118           30 :     const SUMOTime currentTime = MSNet::getInstance()->getCurrentTimeStep();
    8119              : 
    8120           30 :     int manoeuverAngle = veh->getCurrentParkingArea()->getManoeuverAngle(*veh);
    8121           30 :     double GUIAngle = veh->getCurrentParkingArea()->getGUIAngle(*veh);
    8122           30 :     if (abs(GUIAngle) < 0.1) {
    8123              :         GUIAngle = 0.1;    // Wiggle vehicle on parallel exit
    8124              :     }
    8125              : 
    8126           30 :     myManoeuvreVehicleID = veh->getID();
    8127           30 :     myManoeuvreStop = veh->getCurrentParkingArea()->getID();
    8128           30 :     myManoeuvreType = MSVehicle::MANOEUVRE_EXIT;
    8129           30 :     myManoeuvreStartTime = currentTime;
    8130           30 :     myManoeuvreCompleteTime = currentTime + veh->myType->getExitManoeuvreTime(manoeuverAngle);
    8131           30 :     myGUIIncrement = -GUIAngle / (STEPS2TIME(myManoeuvreCompleteTime - myManoeuvreStartTime) / TS);
    8132           30 :     if (veh->remainingStopDuration() > 0) {
    8133           20 :         myManoeuvreCompleteTime += veh->remainingStopDuration();
    8134              :     }
    8135              : 
    8136              : #ifdef DEBUG_STOPS
    8137              :     if (veh->isSelected()) {
    8138              :         std::cout << "EXIT manoeuvre start: vehicle=" << veh->getID() << " Manoeuvre Angle=" << manoeuverAngle  << " increment=" << RAD2DEG(myGUIIncrement) << " currentTime=" << currentTime
    8139              :                   << " endTime=" << myManoeuvreCompleteTime << " manoeuvre time=" << myManoeuvreCompleteTime - currentTime << " parkArea=" << myManoeuvreStop << std::endl;
    8140              :     }
    8141              : #endif
    8142              : 
    8143              :     return (true);
    8144              : }
    8145              : 
    8146              : 
    8147              : bool
    8148          222 : MSVehicle::Manoeuvre::entryManoeuvreIsComplete(MSVehicle* veh) {
    8149              :     // At the moment we only want to consider parking areas - need to check because we could be setting up a manoeuvre
    8150          222 :     if (!veh->hasStops()) {
    8151              :         return (true);
    8152              :     }
    8153              :     MSStop* currentStop = &veh->myStops.front();
    8154          222 :     if (currentStop->parkingarea == nullptr) {
    8155              :         return true;
    8156          220 :     } else if (currentStop->parkingarea->getID() != myManoeuvreStop || MSVehicle::MANOEUVRE_ENTRY != myManoeuvreType) {
    8157           30 :         if (configureEntryManoeuvre(veh)) {
    8158           30 :             MSNet::getInstance()->informVehicleStateListener(veh, MSNet::VehicleState::MANEUVERING);
    8159           30 :             return (false);
    8160              :         } else { // cannot configure entry so stop trying
    8161              :             return true;
    8162              :         }
    8163          190 :     } else if (MSNet::getInstance()->getCurrentTimeStep() < myManoeuvreCompleteTime) {
    8164              :         return false;
    8165              :     } else { // manoeuvre complete
    8166           30 :         myManoeuvreType = MSVehicle::MANOEUVRE_NONE;
    8167           30 :         return true;
    8168              :     }
    8169              : }
    8170              : 
    8171              : 
    8172              : bool
    8173            0 : MSVehicle::Manoeuvre::manoeuvreIsComplete(const ManoeuvreType checkType) const {
    8174            0 :     if (checkType != myManoeuvreType) {
    8175              :         return true;    // we're not maneuvering / wrong manoeuvre
    8176              :     }
    8177              : 
    8178            0 :     if (MSNet::getInstance()->getCurrentTimeStep() < myManoeuvreCompleteTime) {
    8179              :         return false;
    8180              :     } else {
    8181              :         return true;
    8182              :     }
    8183              : }
    8184              : 
    8185              : 
    8186              : bool
    8187         6266 : MSVehicle::Manoeuvre::manoeuvreIsComplete() const {
    8188         6266 :     return (MSNet::getInstance()->getCurrentTimeStep() >= myManoeuvreCompleteTime);
    8189              : }
    8190              : 
    8191              : 
    8192              : bool
    8193         6266 : MSVehicle::manoeuvreIsComplete() const {
    8194         6266 :     return (myManoeuvre.manoeuvreIsComplete());
    8195              : }
    8196              : 
    8197              : 
    8198              : std::pair<double, double>
    8199         7440 : MSVehicle::estimateTimeToNextStop() const {
    8200         7440 :     if (hasStops()) {
    8201         7440 :         MSLane* lane = myLane;
    8202         7440 :         if (lane == nullptr) {
    8203              :             // not in network
    8204           84 :             lane = getEdge()->getLanes()[0];
    8205              :         }
    8206              :         const MSStop& stop = myStops.front();
    8207              :         auto it = myCurrEdge + 1;
    8208              :         // drive to end of current edge
    8209         7440 :         double dist = (lane->getLength() - getPositionOnLane());
    8210         7440 :         double travelTime = lane->getEdge().getMinimumTravelTime(this) * dist / lane->getLength();
    8211              :         // drive until stop edge
    8212         8804 :         while (it != myRoute->end() && it < stop.edge) {
    8213         1364 :             travelTime += (*it)->getMinimumTravelTime(this);
    8214         1364 :             dist += (*it)->getLength();
    8215              :             it++;
    8216              :         }
    8217              :         // drive up to the stop position
    8218         7440 :         const double stopEdgeDist = stop.pars.endPos - (lane == stop.lane ? lane->getLength() : 0);
    8219         7440 :         dist += stopEdgeDist;
    8220         7440 :         travelTime += stop.lane->getEdge().getMinimumTravelTime(this) * (stopEdgeDist / stop.lane->getLength());
    8221              :         // estimate time loss due to acceleration and deceleration
    8222              :         // maximum speed is limited by available distance:
    8223              :         const double a = getCarFollowModel().getMaxAccel();
    8224              :         const double b = getCarFollowModel().getMaxDecel();
    8225         7440 :         const double c = getSpeed();
    8226              :         const double d = dist;
    8227         7440 :         const double len = getVehicleType().getLength();
    8228         7440 :         const double vs = MIN2(MAX2(stop.getSpeed(), 0.0), stop.lane->getVehicleMaxSpeed(this));
    8229              :         // distAccel = (v - c)^2 / (2a)
    8230              :         // distDecel = (v + vs)*(v - vs) / 2b = (v^2 - vs^2) / (2b)
    8231              :         // distAccel + distDecel < d
    8232         7440 :         const double maxVD = MAX2(c, ((sqrt(MAX2(0.0, pow(2 * c * b, 2) + (4 * ((b * ((a * (2 * d * (b + a) + (vs * vs) - (c * c))) - (b * (c * c))))
    8233        14592 :                                             + pow((a * vs), 2))))) * 0.5) + (c * b)) / (b + a));
    8234         7440 :         it = myCurrEdge;
    8235              :         double v0 = c;
    8236         7440 :         bool v0Stable = getAcceleration() == 0 && v0 > 0;
    8237              :         double timeLossAccel = 0;
    8238              :         double timeLossDecel = 0;
    8239              :         double timeLossLength = 0;
    8240        17742 :         while (it != myRoute->end() && it <= stop.edge) {
    8241        10302 :             double v = MIN2(maxVD, (*it)->getVehicleMaxSpeed(this));
    8242        10302 :             double edgeLength = (it == stop.edge ? stop.pars.endPos : (*it)->getLength()) - (it == myCurrEdge ? getPositionOnLane() : 0);
    8243        10302 :             if (edgeLength <= len && v0Stable && v0 < v) {
    8244              :                 const double lengthDist = MIN2(len, edgeLength);
    8245           20 :                 const double dTL = lengthDist / v0 - lengthDist / v;
    8246              :                 //std::cout << "   e=" << (*it)->getID() << " v0=" << v0 << " v=" << v << " el=" << edgeLength << " lDist=" << lengthDist << " newTLL=" << dTL<< "\n";
    8247           20 :                 timeLossLength += dTL;
    8248              :             }
    8249        10302 :             if (edgeLength > len) {
    8250         9166 :                 const double dv = v - v0;
    8251         9166 :                 if (dv > 0) {
    8252              :                     // timeLossAccel = timeAccel - timeMaxspeed = dv / a - distAccel / v
    8253         6504 :                     const double dTA = dv / a - dv * (v + v0) / (2 * a * v);
    8254              :                     //std::cout << "   e=" << (*it)->getID() << " v0=" << v0 << " v=" << v << " newTLA=" << dTA << "\n";
    8255         6504 :                     timeLossAccel += dTA;
    8256              :                     // time loss from vehicle length
    8257         2662 :                 } else if (dv < 0) {
    8258              :                     // timeLossDecel = timeDecel - timeMaxspeed = dv / b - distDecel / v
    8259          540 :                     const double dTD = -dv / b + dv * (v + v0) / (2 * b * v0);
    8260              :                     //std::cout << "   e=" << (*it)->getID() << " v0=" << v0 << " v=" << v << " newTLD=" << dTD << "\n";
    8261          540 :                     timeLossDecel += dTD;
    8262              :                 }
    8263              :                 v0 = v;
    8264              :                 v0Stable = true;
    8265              :             }
    8266              :             it++;
    8267              :         }
    8268              :         // final deceleration to stop (may also be acceleration or deceleration to waypoint speed)
    8269              :         double v = vs;
    8270         7440 :         const double dv = v - v0;
    8271         7440 :         if (dv > 0) {
    8272              :             // timeLossAccel = timeAccel - timeMaxspeed = dv / a - distAccel / v
    8273          144 :             const double dTA = dv / a - dv * (v + v0) / (2 * a * v);
    8274              :             //std::cout << "  final e=" << (*it)->getID() << " v0=" << v0 << " v=" << v << " newTLA=" << dTA << "\n";
    8275          144 :             timeLossAccel += dTA;
    8276              :             // time loss from vehicle length
    8277         7296 :         } else if (dv < 0) {
    8278              :             // timeLossDecel = timeDecel - timeMaxspeed = dv / b - distDecel / v
    8279         7268 :             const double dTD = -dv / b + dv * (v + v0) / (2 * b * v0);
    8280              :             //std::cout << "  final  e=" << (*it)->getID() << " v0=" << v0 << " v=" << v << " newTLD=" << dTD << "\n";
    8281         7268 :             timeLossDecel += dTD;
    8282              :         }
    8283         7440 :         const double result = travelTime + timeLossAccel + timeLossDecel + timeLossLength;
    8284              :         //std::cout << SIMTIME << " v=" << c << " a=" << a << " b=" << b << " maxVD=" << maxVD << " tt=" << travelTime
    8285              :         //    << " ta=" << timeLossAccel << " td=" << timeLossDecel << " tl=" << timeLossLength << " res=" << result << "\n";
    8286         7440 :         return {MAX2(0.0, result), dist};
    8287              :     } else {
    8288            0 :         return {INVALID_DOUBLE, INVALID_DOUBLE};
    8289              :     }
    8290              : }
    8291              : 
    8292              : 
    8293              : double
    8294         2457 : MSVehicle::getStopDelay() const {
    8295         2457 :     if (hasStops() && myStops.front().pars.until >= 0) {
    8296              :         const MSStop& stop = myStops.front();
    8297         1612 :         SUMOTime estimatedDepart = MSNet::getInstance()->getCurrentTimeStep() - DELTA_T;
    8298         1612 :         if (stop.reached) {
    8299          802 :             return STEPS2TIME(estimatedDepart + stop.duration - stop.pars.until);
    8300              :         }
    8301          810 :         if (stop.pars.duration > 0) {
    8302          608 :             estimatedDepart += stop.pars.duration;
    8303              :         }
    8304          810 :         estimatedDepart += TIME2STEPS(estimateTimeToNextStop().first);
    8305          810 :         const double result = MAX2(0.0, STEPS2TIME(estimatedDepart - stop.pars.until));
    8306          810 :         return result;
    8307              :     } else {
    8308              :         // vehicles cannot drive before 'until' so stop delay can never be
    8309              :         // negative and we can use -1 to signal "undefined"
    8310              :         return -1;
    8311              :     }
    8312              : }
    8313              : 
    8314              : 
    8315              : double
    8316         5510 : MSVehicle::getStopArrivalDelay() const {
    8317         5510 :     if (hasStops() && myStops.front().pars.arrival >= 0) {
    8318              :         const MSStop& stop = myStops.front();
    8319         4334 :         if (stop.reached) {
    8320         1304 :             return STEPS2TIME(stop.pars.started - stop.pars.arrival);
    8321              :         } else {
    8322         3030 :             return STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep()) + estimateTimeToNextStop().first - STEPS2TIME(stop.pars.arrival);
    8323              :         }
    8324              :     } else {
    8325              :         // vehicles can arrive earlier than planned so arrival delay can be negative
    8326              :         return INVALID_DOUBLE;
    8327              :     }
    8328              : }
    8329              : 
    8330              : 
    8331              : const MSEdge*
    8332   3103877583 : MSVehicle::getCurrentEdge() const {
    8333   3103877583 :     return myLane != nullptr ? &myLane->getEdge() : getEdge();
    8334              : }
    8335              : 
    8336              : 
    8337              : const MSEdge*
    8338         3932 : MSVehicle::getNextEdgePtr() const {
    8339         3932 :     if (myLane == nullptr || (myCurrEdge + 1) == myRoute->end()) {
    8340            8 :         return nullptr;
    8341              :     }
    8342         3924 :     if (myLane->isInternal()) {
    8343          568 :         return &myLane->getCanonicalSuccessorLane()->getEdge();
    8344              :     } else {
    8345         3356 :         const MSEdge* nextNormal = succEdge(1);
    8346         3356 :         const MSEdge* nextInternal = myLane->getEdge().getInternalFollowingEdge(nextNormal, getVClass());
    8347         3356 :         return nextInternal ? nextInternal : nextNormal;
    8348              :     }
    8349              : }
    8350              : 
    8351              : 
    8352              : const MSLane*
    8353         1602 : MSVehicle::getPreviousLane(const MSLane* current, int& furtherIndex) const {
    8354         1602 :     if (furtherIndex < (int)myFurtherLanes.size()) {
    8355         1225 :         return myFurtherLanes[furtherIndex++];
    8356              :     } else {
    8357              :         // try to use route information
    8358          377 :         int routeIndex = getRoutePosition();
    8359              :         bool resultInternal;
    8360          377 :         if (MSGlobals::gUsingInternalLanes && MSNet::getInstance()->hasInternalLinks()) {
    8361            0 :             if (myLane->isInternal()) {
    8362            0 :                 if (furtherIndex % 2 == 0) {
    8363            0 :                     routeIndex -= (furtherIndex + 0) / 2;
    8364              :                     resultInternal = false;
    8365              :                 } else {
    8366            0 :                     routeIndex -= (furtherIndex + 1) / 2;
    8367              :                     resultInternal = false;
    8368              :                 }
    8369              :             } else {
    8370            0 :                 if (furtherIndex % 2 != 0) {
    8371            0 :                     routeIndex -= (furtherIndex + 1) / 2;
    8372              :                     resultInternal = false;
    8373              :                 } else {
    8374            0 :                     routeIndex -= (furtherIndex + 2) / 2;
    8375              :                     resultInternal = true;
    8376              :                 }
    8377              :             }
    8378              :         } else {
    8379          377 :             routeIndex -= furtherIndex;
    8380              :             resultInternal = false;
    8381              :         }
    8382          377 :         furtherIndex++;
    8383          377 :         if (routeIndex >= 0) {
    8384          163 :             if (resultInternal) {
    8385            0 :                 const MSEdge* prevNormal = myRoute->getEdges()[routeIndex];
    8386            0 :                 for (MSLane* cand : prevNormal->getLanes()) {
    8387            0 :                     for (MSLink* link : cand->getLinkCont()) {
    8388            0 :                         if (link->getLane() == current) {
    8389            0 :                             if (link->getViaLane() != nullptr) {
    8390              :                                 return link->getViaLane();
    8391              :                             } else {
    8392            0 :                                 return const_cast<MSLane*>(link->getLaneBefore());
    8393              :                             }
    8394              :                         }
    8395              :                     }
    8396              :                 }
    8397              :             } else {
    8398          163 :                 return myRoute->getEdges()[routeIndex]->getLanes()[0];
    8399              :             }
    8400              :         }
    8401              :     }
    8402              :     return current;
    8403              : }
    8404              : 
    8405              : SUMOTime
    8406   1525306668 : MSVehicle::getWaitingTimeFor(const MSLink* link) const {
    8407              :     // this vehicle currently has the highest priority on the allway_stop
    8408   1525306668 :     return link == myHaveStoppedFor ? SUMOTime_MAX : getWaitingTime();
    8409              : }
    8410              : 
    8411              : 
    8412              : void
    8413          694 : MSVehicle::resetApproachOnReroute() {
    8414              :     bool diverged = false;
    8415              :     const ConstMSEdgeVector& route = myRoute->getEdges();
    8416          694 :     int ri = getRoutePosition();
    8417         2926 :     for (const DriveProcessItem& dpi : myLFLinkLanes) {
    8418         2232 :         if (dpi.myLink != nullptr) {
    8419         2229 :             if (!diverged) {
    8420         1996 :                 const MSEdge* next = route[ri + 1];
    8421         1996 :                 if (&dpi.myLink->getLane()->getEdge() != next) {
    8422              :                     diverged = true;
    8423              :                 } else {
    8424         1930 :                     if (dpi.myLink->getViaLane() == nullptr) {
    8425              :                         ri++;
    8426              :                     }
    8427              :                 }
    8428              :             }
    8429              :             if (diverged) {
    8430          299 :                 dpi.myLink->removeApproaching(this);
    8431              :             }
    8432              :         }
    8433              :     }
    8434          694 : }
    8435              : 
    8436              : 
    8437              : bool
    8438     15079290 : MSVehicle::instantStopping() const {
    8439     15079290 :     return myInfluencer && !myInfluencer->considerMaxDeceleration();
    8440              : }
    8441              : 
    8442              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1