]> git.tdb.fi Git - r2c2.git/commitdiff
Avoid arriving too early if there's an unexpected stop on the last route
authorMikko Rasa <tdb@tdb.fi>
Wed, 11 Feb 2015 18:14:30 +0000 (20:14 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 11 Feb 2015 23:59:24 +0000 (01:59 +0200)
If the final block of the route is allocated while the train is stopped,
arrival could be triggered prematurely.  I'm not entirely certain if this
can happen in absence of certain quirks of the dummy driver, but better
safe than sorry.

source/libr2c2/trainrouter.cpp
source/libr2c2/trainrouter.h

index e38e29c3ff95d92627877ce350917b36e3c70303..d95bdff4bc85c0db6afa45ff55ef3ffe166e0ae0 100644 (file)
@@ -92,13 +92,14 @@ void TrainRouter::route_changed()
                /* Find the route that should be used for the next allocated block.  We
                can't rely on the resync code in block_reserved since we may need to
                clear the stop marker to continue allocation. */
-               TrackIter track = train.get_block_allocator().first().track_iter();
+               const BlockAllocator &allocator = train.get_block_allocator();
+               TrackIter track = allocator.first().track_iter();
                list<SequencePoint>::iterator seq_begin = sequence_points.begin();
                for(; track; track=track.next())
                {
                        if(!advance_to_track(reserving_route, track))
                        {
-                               arrival = RESERVED_TO_END;
+                               arrival = (allocator.is_block_current(track->get_block()) ? ADVANCED_TO_END : RESERVED_TO_END);
                                break;
                        }
                        if(&track->get_block()==fncb.block())
@@ -252,7 +253,7 @@ void TrainRouter::tick(const Time::TimeDelta &dt)
                sequence_check_pending = false;
        }
 
-       if(arrival==RESERVED_TO_END && !train.get_speed())
+       if(arrival==ADVANCED_TO_END && !train.get_speed())
        {
                signal_arrived.emit(waypoints.back());
                signal_event.emit(Message("arrived", waypoints.back()));
@@ -363,7 +364,7 @@ void TrainRouter::train_advanced(Block &block)
 {
        BlockIter b_iter = train.get_block_allocator().iter_for(block);
 
-       if(waypoints.size()>1)
+       if(!waypoints.empty())
        {
                // A waypoint is considered reached when the train has advanced through it.
                const TrackChain &wp = *waypoints.front();
@@ -374,10 +375,18 @@ void TrainRouter::train_advanced(Block &block)
                        {
                                if(!wp.has_track(*t_iter))
                                {
-                                       waypoints.erase(waypoints.begin());
-                                       metrics_stale = true;
-                                       signal_waypoint_reached.emit(&wp);
-                                       signal_event.emit(Message("waypoint-reached", &wp));
+                                       if(waypoints.size()==1)
+                                       {
+                                               if(arrival==RESERVED_TO_END)
+                                                       arrival = ADVANCED_TO_END;
+                                       }
+                                       else
+                                       {
+                                               waypoints.erase(waypoints.begin());
+                                               metrics_stale = true;
+                                               signal_waypoint_reached.emit(&wp);
+                                               signal_event.emit(Message("waypoint-reached", &wp));
+                                       }
                                        break;
                                }
                                else if(!block.has_track(*t_iter))
index 542d1ffe8488c3e24d8b8beb2d976723d5075fc7..b56e93281f7ade4c183edc4b3be223e4c5fe2a60 100644 (file)
@@ -45,6 +45,7 @@ private:
                ON_THE_WAY,
                WAITING_FOR_SEQUENCE,
                RESERVED_TO_END,
+               ADVANCED_TO_END,
                ARRIVED
        };