]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouter.cpp
Fix critical block logic
[r2c2.git] / source / libr2c2 / trainrouter.cpp
index c39af92c246e9ed30ce78ded29d40832dc8e8925..3d8b9d4b7b42a3c7ee3c69c84a81407232107107 100644 (file)
@@ -2,8 +2,10 @@
 #include "route.h"
 #include "trackiter.h"
 #include "train.h"
+#include "trackchain.h"
+#include "trainroutemetric.h"
+#include "trainrouteplanner.h"
 #include "trainrouter.h"
-#include "zone.h"
 
 using namespace std;
 using namespace Msp;
@@ -13,119 +15,181 @@ namespace R2C2 {
 TrainRouter::TrainRouter(Train &t):
        TrainAI(t),
        priority(0),
-       arriving(false),
-       yielding_to(0)
+       arrival(ON_THE_WAY),
+       waypoints_changed(false),
+       metrics_stale(false),
+       current_sequence(0),
+       sequence_check_pending(false)
 {
        train.get_layout().signal_block_reserved.connect(sigc::mem_fun(this, &TrainRouter::block_reserved));
        train.signal_advanced.connect(sigc::mem_fun(this, &TrainRouter::train_advanced));
+       train.signal_rear_advanced.connect(sigc::mem_fun(this, &TrainRouter::train_rear_advanced));
 }
 
-void TrainRouter::set_priority(int p)
+TrainRouter::~TrainRouter()
 {
-       priority = p;
+       for(vector<TrainRouteMetric *>::iterator i=metrics.begin(); i!=metrics.end(); ++i)
+               delete *i;
 }
 
-void TrainRouter::yield_to(const Train &t)
+void TrainRouter::set_priority(int p)
 {
-       yielding_to = &t;
+       priority = p;
 }
 
 bool TrainRouter::set_route(const Route *r)
 {
-       train.free_noncritical_blocks();
-
-       Route *lead = 0;
-       if(r && train.is_placed())
-       {
-               TrackIter first = train.get_tail_block().track_iter();
-               TrackIter next = train.get_head_block().next().track_iter();
-               if(!r->has_track(*next))
-               {
-                       lead = Route::find(next, *r);
-                       if(!lead)
-                               return false;
-                       create_lead_route(lead, lead);
-               }
-               else if(!r->has_track(*first))
-                       lead = create_lead_route(0, r);
-       }
-
        routes.clear();
-       if(lead)
-               routes.push_back(lead);
        if(r)
+       {
                routes.push_back(r);
-       train.stop_at(0);
-       arriving = false;
+               create_lead_route();
+       }
 
-       train.reserve_more();
+       waypoints.clear();
+       sequence_points.clear();
+       current_sequence = 0;
+       sequence_check_pending = false;
 
-       const Route *route = get_route();
-       signal_route_changed.emit(route);
-       signal_event.emit(Message("route-changed", route));
+       route_changed();
 
        return true;
 }
 
-bool TrainRouter::go_to(Track &to)
+const Route *TrainRouter::get_route() const
 {
-       if(!train.get_speed())
+       if(routes.empty())
+               return 0;
+       return routes.front();
+}
+
+void TrainRouter::use_planned_route()
+{
+       if(!planner || planner->get_result()!=TrainRoutePlanner::COMPLETE)
+               return;
+
+       const list<Route *> &planned_routes = planner->get_routes_for(train);
+
+       routes.clear();
+       routes.insert(routes.end(), planned_routes.begin(), planned_routes.end());
+       create_lead_route();
+
+       sequence_points = planner->get_sequence_for(train);
+       current_sequence = 0;
+       sequence_check_pending = false;
+
+       route_changed();
+}
+
+void TrainRouter::route_changed()
+{
+       BlockIter fncb = train.get_last_critical_block().next();
+
+       arrival = ON_THE_WAY;
+       reserving_route = routes.begin();
+       if(!routes.empty())
        {
-               for(BlockIter i=train.get_tail_block(); (i && i->get_train()==&train); i=i.next())
-                       if(i->has_track(to))
+               /* 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. */
+               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))
                        {
-                               signal_arrived.emit();
-                               signal_event.emit(Message("arrived"));
-                               return set_route(0);
+                               arrival = (allocator.is_block_current(track->get_block()) ? ADVANCED_TO_END : RESERVED_TO_END);
+                               break;
                        }
-       }
+                       if(&track->get_block()==fncb.block())
+                               break;
 
-       train.free_noncritical_blocks();
+                       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;
+                       }
+               }
 
-       TrackIter next = train.get_head_block().next().track_iter();
+               sequence_points.erase(sequence_points.begin(), seq_begin);
 
-       Route *route = Route::find(next, to);
-       if(!route)
-               return false;
-       create_lead_route(route, route);
-       return set_route(route);
+               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;
+                       }
+               }
+       }
+
+       /* 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);
+       // 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);
+       signal_event.emit(Message("route-changed", route));
 }
 
-bool TrainRouter::go_to(const Zone &to)
+void TrainRouter::set_destination(const TrackChain &d)
 {
-       set<Track *> tracks;
-       for(BlockIter i=train.get_tail_block(); (i && i->get_train()==&train); i=i.next())
-               tracks.insert(i->get_tracks().begin(), i->get_tracks().end());
+       if(waypoints.empty())
+               waypoints.push_back(&d);
+       else
+               waypoints.back() = &d;
+       waypoints_changed = true;
+       metrics_stale = true;
+}
 
-       const Zone::TrackSet &ztracks = to.get_tracks();
-       unsigned union_size = 0;
-       for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i)
-               union_size += tracks.count(*i);
+void TrainRouter::add_waypoint(const TrackChain &wp)
+{
+       waypoints.push_back(&wp);
+       waypoints_changed = true;
+       metrics_stale = true;
+}
 
-       if(union_size==tracks.size() || union_size==ztracks.size())
-       {
-               signal_arrived.emit();
-               signal_event.emit(Message("arrived"));
-               return set_route(0);
-       }
+const TrackChain &TrainRouter::get_waypoint(unsigned index) const
+{
+       if(index>=waypoints.size())
+               throw out_of_range("TrainRouter::is_waypoint");
 
-       train.free_noncritical_blocks();
+       return *waypoints[index];
+}
 
-       TrackIter next = train.get_head_block().next().track_iter();
+const TrainRouteMetric &TrainRouter::get_metric(int index) const
+{
+       if(waypoints.empty())
+               throw logic_error("no metrics");
+       else if(metrics_stale)
+               throw logic_error("metrics are stale");
+
+       if(index<0)
+               return *metrics.back();
+       else if(static_cast<unsigned>(index)>=waypoints.size())
+               throw out_of_range("TrainRouter::get_metric");
+       else
+               return *metrics[index];
+}
 
-       Route *route = Route::find(next, to);
-       if(!route)
-               return false;
-       create_lead_route(route, route);
-       route->add_tracks(ztracks);
-       return set_route(route);
+void TrainRouter::set_departure_delay(const Time::TimeDelta &d)
+{
+       delay = d;
+       waypoints_changed = true;
 }
 
-const Route *TrainRouter::get_route() const
+void TrainRouter::set_trip_duration(const Time::TimeDelta &d)
 {
-       if(routes.empty())
-               return 0;
-       return routes.front();
+       duration = d;
 }
 
 void TrainRouter::message(const Message &msg)
@@ -139,33 +203,68 @@ void TrainRouter::message(const Message &msg)
        }
        else if(msg.type=="clear-route")
                set_route(0);
-       else if(msg.type=="go-to-track")
-               go_to(*msg.value.value<Track *>());
-       else if(msg.type=="go-to-zone")
+       else if(msg.type=="set-destination")
+       {
+               if(msg.value.check_type<TrackChain *>())
+                       set_destination(*msg.value.value<TrackChain *>());
+               else
+                       set_destination(*msg.value.value<const TrackChain *>());
+       }
+       else if(msg.type=="add-waypoint")
        {
-               if(msg.value.check_type<Zone *>())
-                       go_to(*msg.value.value<Zone *>());
+               if(msg.value.check_type<TrackChain *>())
+                       add_waypoint(*msg.value.value<TrackChain *>());
                else
-                       go_to(*msg.value.value<const Zone *>());
+                       add_waypoint(*msg.value.value<const TrackChain *>());
        }
+       else if(msg.type=="set-departure-delay")
+               set_departure_delay(msg.value.value<Time::TimeDelta>());
+       else if(msg.type=="set-trip-duration")
+               set_trip_duration(msg.value.value<Time::TimeDelta>());
 }
 
-void TrainRouter::tick(const Time::TimeStamp &, const Time::TimeDelta &)
+void TrainRouter::tick(const Time::TimeDelta &dt)
 {
-       if(arriving && !train.get_speed())
+       if(delay)
        {
-               train.set_active(false);
-               signal_arrived.emit();
-               signal_event.emit(Message("arrived"));
-               set_route(0);
+               delay -= dt;
+               if(delay<Time::zero)
+               {
+                       duration = max(duration+delay, Time::zero);
+                       delay = Time::zero;
+               }
        }
+       else if(duration)
+               duration = max(duration-dt, Time::zero);
+
+       if(waypoints_changed && !planner)
+               start_planning(train.get_layout());
+
+       if(planner && planner->check()!=TrainRoutePlanner::PENDING)
+               apply_plan(train.get_layout(), *planner);
+
+       if(sequence_check_pending)
+       {
+               if(sequence_points.front().is_cleared())
+               {
+                       arrival = ON_THE_WAY;
+                       train.stop_at(0);
+               }
+               sequence_check_pending = false;
+       }
+
+       if(arrival==ADVANCED_TO_END && !train.get_speed())
+       {
+               signal_arrived.emit(waypoints.back());
+               signal_event.emit(Message("arrived", waypoints.back()));
+               arrival = ARRIVED;
+       }
+       else if(arrival==ARRIVED && !train.get_block_allocator().is_active())
+               set_route(0);
 }
 
 void TrainRouter::save(list<DataFile::Statement> &st) const
 {
-       if(!tag.empty())
-               st.push_back((DataFile::Statement("tag"), tag));
-
        st.push_back((DataFile::Statement("priority"), priority));
 
        if(!routes.empty())
@@ -179,168 +278,293 @@ void TrainRouter::save(list<DataFile::Statement> &st) const
 
 void TrainRouter::block_reserved(Block &block, Train *t)
 {
-       if(t!=&train)
+       if(routes.empty())
                return;
 
-       yielding_to = 0;
+       if(t!=&train)
+       {
+               if(!t)
+                       return;
 
-       BlockIter b_iter(&block, t->get_entry_to_block(block));
-       BlockIter b_iter_next;
+               // 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. */
+                       sequence_check_pending = true;
 
-       RouteList::iterator route = routes.begin();
-       if(advance_route(route, block))
+               return;
+       }
+
+       // Did we reach our next sequence point?
+       if(!sequence_points.empty())
        {
-               // Check if the block is a turnout and set it to proper path
-               if(unsigned tid = block.get_turnout_id())
+               SequencePoint &sp = sequence_points.front();
+               if(sp.block==&block)
                {
-                       int path = (*route)->get_turnout(tid);
-                       if(path>=0)
-                               b_iter.track_iter()->set_active_path(path);
+                       current_sequence = sp.sequence_out;
+                       sequence_points.pop_front();
                }
+       }
+
+       TrackIter track = train.get_block_allocator().iter_for(block).track_iter();
+
+       // Is the block a turnout?  If it is, set it to the correct path.
+       if(unsigned taddr = block.get_turnout_address())
+       {
+               int path = (*reserving_route)->get_turnout(taddr);
+               if(path>=0)
+                       track->set_active_path(path);
+       }
 
-               // Check if the next block is still part of the designated route
-               b_iter_next = b_iter.next(*route);
+       /* If the allocator has released blocks from the front, we may need to
+       resync reserving_route. */
+       if(reserving_route==routes.end() || !(*reserving_route)->has_track(*track))
+       {
+               reserving_route = routes.begin();
+               arrival = ON_THE_WAY;
+               track = t->get_block_allocator().first().track_iter();
+               for(; track; track=track.next())
+               {
+                       if(!advance_to_track(reserving_route, track))
+                               throw logic_error("internal error (reservation outside of route)");
+                       else if(&track->get_block()==&block)
+                               break;
+               }
+       }
 
-               RouteList::iterator next_route = route;
-               if(!advance_route(next_route, *b_iter_next))
+       /* Keep reserving_route pointing to the route containing the block that is
+       expected to be allocated next. */
+       for(; track; track=track.next((*reserving_route)->get_path(*track)))
+       {
+               if(!advance_to_track(reserving_route, track))
                {
+                       // We've reached the end of the route.  Stop here.
+                       arrival = RESERVED_TO_END;
                        train.stop_at(&block);
                        return;
                }
+               if(&track->get_block()!=&block)
+                       break;
+       }
 
+       // Do we need to wait for another train to pass?
+       if(!sequence_points.empty())
+       {
+               SequencePoint &sp = sequence_points.front();
+               if(sp.block==&track->get_block() && !sp.is_cleared())
+               {
+                       arrival = WAITING_FOR_SEQUENCE;
+                       train.stop_at(&block);
+               }
        }
-       else
-               b_iter_next = b_iter.next();
+}
+
+void TrainRouter::train_advanced(Block &block)
+{
+       BlockIter b_iter = train.get_block_allocator().iter_for(block);
 
-       // Check if there's another train and ask it to free the block if appropriate
-       if(b_iter_next)
+       if(!waypoints.empty())
        {
-               if(Train *other_train = b_iter_next->get_train())
+               // A waypoint is considered reached when the train has advanced through it.
+               const TrackChain &wp = *waypoints.front();
+               TrackIter t_iter = b_iter.track_iter();
+               if(wp.has_track(*t_iter))
                {
-                       /* There's another train ahead of us.  If it wants to exit the block
-                       from the same endpoint we're trying to enter from or the other way
-                       around, treat it as coming towards us.  Otherwise treat it as going
-                       in the same direction. */
-                       int other_entry = other_train->get_entry_to_block(*b_iter_next);
-                       if(other_entry<0)
-                               throw logic_error("block reservation inconsistency");
-
-                       unsigned exit = b_iter_next.reverse().entry();
-                       unsigned other_exit = BlockIter(b_iter_next.block(), other_entry).reverse().entry();
-                       bool entry_conflict = (b_iter_next.entry()==other_exit);
-                       bool exit_conflict = (exit==static_cast<unsigned>(other_entry));
-                       // TODO: speed matching with preceding train
-
-                       // XXX Should invent a better way to get our counterpart from the other train
-                       TrainRouter *other_router = dynamic_cast<TrainRouter *>(other_train->get_tagged_ai("router"));
-                       int other_prio = (other_router ? other_router->get_priority() : 0);
-
-                       if(!entry_conflict && !exit_conflict && other_prio<priority)
+                       for(; t_iter; t_iter=t_iter.next())
                        {
-                               /* Ask a lesser priority train going to the same direction to free
-                               the block for us */
-                               other_train->free_block(*b_iter_next);
-                       }
-                       else if(other_train!=yielding_to && (other_prio<priority || (other_prio==priority && entry_conflict)))
-                       {
-                               /* A lesser priority train is coming at us, we must ask it to free
-                               enough blocks to get clear of it to avoid a potential deadlock */
-                               BlockIter last_contested;
-                               RouteList::iterator j = route;
-                               for(BlockIter i=b_iter_next; (i && i->get_train()==other_train);)
+                               if(!wp.has_track(*t_iter))
                                {
-                                       if(!advance_route(j, *i))
-                                               break;
-                                       last_contested = i;
-                                       i = i.next(*j);
-                               }
-
-                               if(last_contested)
-                               {
-                                       if(other_train->free_block(*last_contested))
-                                               other_router->yield_to(train);
+                                       if(waypoints.size()==1)
+                                       {
+                                               if(arrival==RESERVED_TO_END)
+                                                       arrival = ADVANCED_TO_END;
+                                       }
                                        else
-                                               yield_to(*other_train);
+                                       {
+                                               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))
+                                       break;
                        }
                }
        }
 }
 
-void TrainRouter::train_advanced(Block &block)
+void TrainRouter::train_rear_advanced(Block &block)
 {
-       // Check if we've reached the next route
-       if(routes.size()>1)
+       Track &track = *train.get_block_allocator().iter_for(block).endpoint().track;
+
+       // Drop any routes that are now completely behind the train.
+       for(RouteList::iterator i=routes.begin(); i!=routes.end(); ++i)
+               if((*i)->has_track(track))
+               {
+                       if(i!=routes.begin())
+                       {
+                               routes.erase(routes.begin(), i);
+                               const Route *route = get_route();
+                               signal_route_changed.emit(route);
+                               signal_event.emit(Message("route-changed", route));
+                       }
+                       break;
+               }
+}
+
+void TrainRouter::create_metrics()
+{
+       for(vector<TrainRouteMetric *>::iterator i=metrics.begin(); i!=metrics.end(); ++i)
+               delete *i;
+       metrics.clear();
+
+       metrics_stale = false;
+
+       if(waypoints.empty())
+               return;
+
+       for(vector<const TrackChain *>::const_iterator i=waypoints.begin(); i!=waypoints.end(); ++i)
+               metrics.push_back(new TrainRouteMetric(**i));
+
+       for(unsigned i=metrics.size()-1; i-->0; )
+               metrics[i]->chain_to(*metrics[i+1]);
+}
+
+bool TrainRouter::create_lead_route()
+{
+       if(routes.empty() || !train.is_placed())
+               return false;
+
+       BlockIter lcb = train.get_last_critical_block();
+       TrackIter last_track_rev = lcb.reverse().track_iter();
+
+       unsigned count = 0;
+       for(TrackIter i=last_track_rev; (i && i->get_block().get_train()==&train); i=i.next())
        {
-               unsigned entry = train.get_entry_to_block(block);
-               Track &track = *block.get_endpoint(entry).track;
-               const Route &route = **++routes.begin();
-               if(route.has_track(track))
+               if(routes.front()->has_track(*i))
+                       ++count;
+               else if(count>0)
                {
-                       routes.pop_front();
-                       // XXX Exceptions?
-                       signal_event.emit(Message("route-changed", get_route()));
+                       if(count==routes.front()->get_tracks().size())
+                               last_track_rev = i;
+                       break;
                }
        }
 
-       if(!routes.empty())
+       TrackIter next_track = last_track_rev.flip();
+       if(!routes.front()->has_track(*last_track_rev) && !routes.front()->has_track(*next_track))
        {
-               BlockIter iter(&block, train.get_entry_to_block(block));
-               iter = iter.next();
-               if(iter && !is_on_route(*iter))
-                       arriving = true;
+               Route *pf = Route::find(next_track, *routes.front());
+               if(!pf)
+                       return false;
+
+               routes.push_front(pf);
        }
+
+       Route *lead = 0;
+       for(TrackIter i=last_track_rev; (i && i->get_block().get_train()==&train); i=i.next())
+       {
+               if(!lead && !routes.front()->has_track(*i))
+               {
+                       lead = new Route(train.get_layout());
+                       lead->set_name("Lead");
+                       lead->set_temporary(true);
+                       routes.push_front(lead);
+
+                       TrackIter j = i.flip();
+                       lead->add_track(*j);
+                       lead->add_track(*j.next());
+               }
+
+               if(lead)
+                       lead->add_track(*i);
+       }
+
+       return true;
 }
 
-const Route *TrainRouter::get_route_for_block(const Block &block) const
+bool TrainRouter::is_valid_for_track(const Route &route, const TrackIter &track) const
 {
-       const set<Track *> &tracks = block.get_tracks();
-       for(RouteList::const_iterator i=routes.begin(); i!=routes.end(); ++i)
-               for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
-                       if((*i)->has_track(**j))
-                               return *i;
-
-       return 0;
+       if(!route.has_track(*track))
+               return false;
+       if(track->get_type().is_turnout() && route.get_turnout(track->get_turnout_address())<0 && route.has_track(*track.flip()))
+               return false;
+       return true;
 }
 
-Route *TrainRouter::create_lead_route(Route *lead, const Route *target)
+bool TrainRouter::advance_to_track(RouteList::iterator &route, const TrackIter &track)
 {
-       if(!lead)
+       if(!is_valid_for_track(**route, track))
        {
-               lead = new Route(train.get_layout());
-               lead->set_name("Lead");
-               lead->set_temporary(true);
+               ++route;
+               if(route==routes.end())
+                       return false;
+               if(!is_valid_for_track(**route, track))
+                       throw logic_error("internal error (routes are not continuous)");
        }
 
-       set<Track *> tracks;
-       for(BlockIter i=train.get_tail_block(); (i && i->get_train()==&train); i=i.next())
+       return true;
+}
+
+void TrainRouter::get_routers(Layout &layout, vector<TrainRouter *> &routers)
+{
+       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);
+}
+
+void TrainRouter::start_planning(Layout &layout)
+{
+       vector<TrainRouter *> routers;
+       get_routers(layout, routers);
+
+       for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+               if((*i)->metrics_stale)
+                       (*i)->create_metrics();
+
+       RefPtr<TrainRoutePlanner> planner = new TrainRoutePlanner(layout);
+       for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
        {
-               const set<Track *> &btracks = i->get_tracks();
-               for(set<Track *>::const_iterator j=btracks.begin(); j!=btracks.end(); ++j)
-                       if(!target || !target->has_track(**j))
-                               tracks.insert(*j);
+               (*i)->waypoints_changed = false;
+               (*i)->planner = planner;
        }
 
-       lead->add_tracks(tracks);
-
-       return lead;
+       planner->plan_async();
 }
 
-bool TrainRouter::advance_route(RouteList::iterator &iter, const Block &block)
+void TrainRouter::apply_plan(Layout &layout, TrainRoutePlanner &planner)
 {
-       const set<Track *> &tracks = block.get_tracks();
-       for(; iter!=routes.end(); ++iter)
-               for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
-                       if((*iter)->has_track(**j))
-                               return true;
+       vector<TrainRouter *> routers;
+       get_routers(layout, routers);
 
-       return false;
+       for(vector<TrainRouter *>::const_iterator i=routers.begin(); i!=routers.end(); ++i)
+               if((*i)->planner.get()==&planner)
+               {
+                       (*i)->use_planned_route();
+                       (*i)->planner = 0;
+               }
 }
 
-bool TrainRouter::is_on_route(const Block &block)
+
+TrainRouter::SequencePoint::SequencePoint(Block &b, unsigned o):
+       block(&b),
+       preceding_train(0),
+       sequence_in(0),
+       sequence_out(o)
+{ }
+
+bool TrainRouter::SequencePoint::is_cleared() const
 {
-       RouteList::iterator iter = routes.begin();
-       return advance_route(iter, block);
+       if(!preceding_train)
+               return true;
+
+       TrainRouter *router = preceding_train->get_ai_of_type<TrainRouter>();
+       return router->get_current_sequence()>=sequence_in;
 }
 
 
@@ -349,7 +573,6 @@ TrainRouter::Loader::Loader(TrainRouter &r):
 {
        add("priority", &TrainRouter::priority);
        add("route",    &Loader::route);
-       add("tag",      &Loader::tag);
 }
 
 void TrainRouter::Loader::route(const string &n)
@@ -357,9 +580,4 @@ void TrainRouter::Loader::route(const string &n)
        obj.set_route(&obj.train.get_layout().get_route(n));
 }
 
-void TrainRouter::Loader::tag(const string &t)
-{
-       obj.set_tag(t);
-}
-
 } // namespace R2C2