]> git.tdb.fi Git - r2c2.git/commitdiff
Better sequence syncing on route change
authorMikko Rasa <tdb@tdb.fi>
Wed, 11 Feb 2015 16:28:49 +0000 (18:28 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 11 Feb 2015 16:30:59 +0000 (18:30 +0200)
Immediately go into the wait state if an uncleared sequence point is
encountered during sync.

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

index 27fca7a56632c7bb1017844393f103192f1c634a..e38e29c3ff95d92627877ce350917b36e3c70303 100644 (file)
@@ -85,8 +85,8 @@ void TrainRouter::route_changed()
 {
        BlockIter fncb = train.get_first_noncritical_block();
 
+       arrival = ON_THE_WAY;
        reserving_route = routes.begin();
-       bool already_at_end = false;
        if(!routes.empty())
        {
                /* Find the route that should be used for the next allocated block.  We
@@ -98,7 +98,7 @@ void TrainRouter::route_changed()
                {
                        if(!advance_to_track(reserving_route, track))
                        {
-                               already_at_end = true;
+                               arrival = RESERVED_TO_END;
                                break;
                        }
                        if(&track->get_block()==fncb.block())
@@ -106,30 +106,34 @@ void TrainRouter::route_changed()
 
                        if(seq_begin!=sequence_points.end() && seq_begin->block==&track->get_block())
                        {
+                               // Assume any sequence points within critical blocks to be cleared
                                current_sequence = seq_begin->sequence_out;
                                ++seq_begin;
                        }
                }
 
                sequence_points.erase(sequence_points.begin(), seq_begin);
-       }
 
-       if(!already_at_end)
-       {
-               // We are not at the end of the route now, but might have been before.
-               arrival = ON_THE_WAY;
-               train.refresh_blocks_from(*fncb);
-               if(!arrival)
-                       train.stop_at(0);
+               if(!sequence_points.empty())
+               {
+                       const SequencePoint &sp = sequence_points.front();
+                       if(sp.block==fncb.block() && !sp.is_cleared())
+                       {
+                               arrival = WAITING_FOR_SEQUENCE;
+                               sequence_check_pending = true;
+                       }
+               }
        }
-       else if(!arrival)
-       {
-               /* If arrival wasn't set before (perhaps because we weren't on a route),
-               set it now. */
-               arrival = RESERVED_TO_END;
+
+       /* Refresh from the first non-critical block to pick up any changes in the
+       route.  Set stop marker first in case an arrival condition was met within the
+       critical blocks. */
+       if(arrival)
                train.stop_at(&*fncb.flip());
-               train.refresh_blocks_from(*fncb);
-       }
+       train.refresh_blocks_from(*fncb);
+       // If no arrival condition was found, clear a possible previous stop marker.
+       if(!arrival)
+               train.stop_at(0);
 
        const Route *route = get_route();
        signal_route_changed.emit(route);
@@ -241,7 +245,10 @@ void TrainRouter::tick(const Time::TimeDelta &dt)
        if(sequence_check_pending)
        {
                if(sequence_points.front().is_cleared())
+               {
+                       arrival = ON_THE_WAY;
                        train.stop_at(0);
+               }
                sequence_check_pending = false;
        }
 
@@ -345,7 +352,10 @@ void TrainRouter::block_reserved(Block &block, Train *t)
        {
                SequencePoint &sp = sequence_points.front();
                if(sp.block==&track->get_block() && !sp.is_cleared())
+               {
+                       arrival = WAITING_FOR_SEQUENCE;
                        train.stop_at(&block);
+               }
        }
 }
 
index 457120944627cb29b059e4622b787f2389927578..542d1ffe8488c3e24d8b8beb2d976723d5075fc7 100644 (file)
@@ -43,6 +43,7 @@ private:
        enum ArrivalState
        {
                ON_THE_WAY,
+               WAITING_FOR_SEQUENCE,
                RESERVED_TO_END,
                ARRIVED
        };