]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouter.cpp
Fix critical block logic
[r2c2.git] / source / libr2c2 / trainrouter.cpp
index 27fca7a56632c7bb1017844393f103192f1c634a..3d8b9d4b7b42a3c7ee3c69c84a81407232107107 100644 (file)
@@ -83,22 +83,23 @@ void TrainRouter::use_planned_route()
 
 void TrainRouter::route_changed()
 {
-       BlockIter fncb = train.get_first_noncritical_block();
+       BlockIter fncb = train.get_last_critical_block().next();
 
+       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
                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))
                        {
-                               already_at_end = true;
+                               arrival = (allocator.is_block_current(track->get_block()) ? ADVANCED_TO_END : RESERVED_TO_END);
                                break;
                        }
                        if(&track->get_block()==fncb.block())
@@ -106,30 +107,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,11 +246,14 @@ 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;
        }
 
-       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()));
@@ -345,7 +353,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);
+               }
        }
 }
 
@@ -353,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();
@@ -364,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))
@@ -419,23 +438,24 @@ bool TrainRouter::create_lead_route()
        if(routes.empty() || !train.is_placed())
                return false;
 
-       BlockIter fncb = train.get_first_noncritical_block();
-       TrackIter next_track = fncb.track_iter();
+       BlockIter lcb = train.get_last_critical_block();
+       TrackIter last_track_rev = lcb.reverse().track_iter();
 
        unsigned count = 0;
-       for(TrackIter i=next_track.flip(); (i && i->get_block().get_train()==&train); i=i.next())
+       for(TrackIter i=last_track_rev; (i && i->get_block().get_train()==&train); i=i.next())
        {
                if(routes.front()->has_track(*i))
                        ++count;
                else if(count>0)
                {
                        if(count==routes.front()->get_tracks().size())
-                               next_track = i.flip();
+                               last_track_rev = i;
                        break;
                }
        }
 
-       if(!routes.front()->has_track(*next_track) && !routes.front()->has_track(*next_track.flip()))
+       TrackIter next_track = last_track_rev.flip();
+       if(!routes.front()->has_track(*last_track_rev) && !routes.front()->has_track(*next_track))
        {
                Route *pf = Route::find(next_track, *routes.front());
                if(!pf)
@@ -445,7 +465,7 @@ bool TrainRouter::create_lead_route()
        }
 
        Route *lead = 0;
-       for(TrackIter i=next_track.flip(); (i && i->get_block().get_train()==&train); i=i.next())
+       for(TrackIter i=last_track_rev; (i && i->get_block().get_train()==&train); i=i.next())
        {
                if(!lead && !routes.front()->has_track(*i))
                {