]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouter.cpp
Don't clear current_sequence in set_route
[r2c2.git] / source / libr2c2 / trainrouter.cpp
index f0870d250f2b7b95293797a2c86a8c72addf56c1..1ada62104cee5724ac208be1e1e57cf7dd18946c 100644 (file)
@@ -48,7 +48,6 @@ bool TrainRouter::set_route(const Route *r)
 
        waypoints.clear();
        sequence_points.clear();
-       current_sequence = 0;
 
        route_changed();
 
@@ -118,7 +117,7 @@ void TrainRouter::route_changed()
                if(!sequence_points.empty())
                {
                        const SequencePoint &sp = sequence_points.front();
-                       if(sp.block==fncb.block() && sp.preceding_train)
+                       if(sp.block==fncb.block() && !sp.is_cleared())
                                state = SEQUENCE_CHECK_PENDING;
                }
        }
@@ -294,11 +293,14 @@ void TrainRouter::block_reserved(Block &block, Train *t)
                        return;
 
                // Are we waiting for the other train to pass a sequence point?
-               SequencePoint &sp = sequence_points.front();
-               if(sp.preceding_train==t && sp.block==&block)
-                       /* The other train's router will advance its sequence on the same
-                       signal and may not have handled it yet. */
-                       state = SEQUENCE_CHECK_PENDING;
+               if(state==WAITING_FOR_SEQUENCE)
+               {
+                       SequencePoint &sp = sequence_points.front();
+                       if(sp.preceding_train==t && sp.block==&block)
+                               /* The other train's router will advance its sequence on the same
+                               signal and may not have handled it yet. */
+                               state = SEQUENCE_CHECK_PENDING;
+               }
 
                return;
        }
@@ -361,7 +363,7 @@ void TrainRouter::block_reserved(Block &block, Train *t)
                SequencePoint &sp = sequence_points.front();
                if(sp.block==&track->get_block() && !sp.is_cleared())
                {
-                       state = WAITING_FOR_SEQUENCE;
+                       state = SEQUENCE_CHECK_PENDING;
                        train.stop_at(&block);
                }
        }
@@ -524,13 +526,14 @@ bool TrainRouter::advance_to_track(RouteList::iterator &route, const TrackIter &
        return false;
 }
 
-void TrainRouter::get_routers(Layout &layout, vector<TrainRouter *> &routers)
+void TrainRouter::get_routers(Layout &layout, vector<TrainRouter *> &routers, TrainRoutePlanner *planner)
 {
        const map<unsigned, Train *> &trains = layout.get_trains();
        routers.reserve(trains.size());
        for(map<unsigned, Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
                if(TrainRouter *router = i->second->get_ai_of_type<TrainRouter>())
-                       routers.push_back(router);
+                       if(!planner || router->planner.get()==planner)
+                               routers.push_back(router);
 }
 
 void TrainRouter::start_planning(Layout &layout)
@@ -558,14 +561,18 @@ void TrainRouter::apply_plan(Layout &layout, TrainRoutePlanner &planner)
                layout.emergency(0, "Route planning failed");
 
        vector<TrainRouter *> routers;
-       get_routers(layout, routers);
+       get_routers(layout, routers, &planner);
 
+       /* Clear sequence counters first to avoid inconsistent state while applying
+       the plan. */
        for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
-               if((*i)->planner.get()==&planner)
-               {
-                       (*i)->use_planned_route();
-                       (*i)->planner = 0;
-               }
+               (*i)->current_sequence = 0;
+
+       for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+       {
+               (*i)->use_planned_route();
+               (*i)->planner = 0;
+       }
 }