From: Mikko Rasa Date: Sat, 21 Feb 2015 19:05:28 +0000 (+0200) Subject: Robustify sequence handling while applying route plans X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=8066ed50f94d046b0e009e24315ca7980439bb8c;p=r2c2.git Robustify sequence handling while applying route plans 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. --- diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index a40e8ff..d530687 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -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 &routers) +void TrainRouter::get_routers(Layout &layout, vector &routers, TrainRoutePlanner *planner) { const map &trains = layout.get_trains(); routers.reserve(trains.size()); for(map::const_iterator i=trains.begin(); i!=trains.end(); ++i) if(TrainRouter *router = i->second->get_ai_of_type()) - 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 routers; - get_routers(layout, routers); + get_routers(layout, routers, &planner); + /* Clear sequence counters first to avoid inconsistent state while applying + the plan. */ for(vector::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::const_iterator i=routers.begin(); i!=routers.end(); ++i) + { + (*i)->use_planned_route(); + (*i)->planner = 0; + } } diff --git a/source/libr2c2/trainrouter.h b/source/libr2c2/trainrouter.h index ad77f31..457bdf7 100644 --- a/source/libr2c2/trainrouter.h +++ b/source/libr2c2/trainrouter.h @@ -118,7 +118,7 @@ private: bool create_lead_route(); bool advance_to_track(RouteList::iterator &, const TrackIter &); - static void get_routers(Layout &, std::vector &); + static void get_routers(Layout &, std::vector &, TrainRoutePlanner * = 0); static void start_planning(Layout &); static void apply_plan(Layout &, TrainRoutePlanner &); };