]> git.tdb.fi Git - r2c2.git/commitdiff
Robustify sequence handling while applying route plans
authorMikko Rasa <tdb@tdb.fi>
Sat, 21 Feb 2015 19:05:28 +0000 (21:05 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 21 Feb 2015 19:05:28 +0000 (21:05 +0200)
Setting current_sequence of all participating routers to zero beforehand
will make sequence points involving them not cleared and avoid the need
to special case everything.

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

index a40e8ffb23d3c369fc70e2e6712115ce44f4bf67..d530687af92fc9612e3f30f7823eafc5425a62ab 100644 (file)
@@ -118,7 +118,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;
                }
        }
@@ -364,7 +364,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);
                }
        }
@@ -527,13 +527,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)
@@ -561,14 +562,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;
+       }
 }
 
 
index ad77f31b914b766c5c91af88208ddb6253dbf784..457bdf78cf59c5dd5fcfb4c49db644f991ec888b 100644 (file)
@@ -118,7 +118,7 @@ private:
        bool create_lead_route();
        bool advance_to_track(RouteList::iterator &, const TrackIter &);
 
-       static void get_routers(Layout &, std::vector<TrainRouter *> &);
+       static void get_routers(Layout &, std::vector<TrainRouter *> &, TrainRoutePlanner * = 0);
        static void start_planning(Layout &);
        static void apply_plan(Layout &, TrainRoutePlanner &);
 };