bool TrainRouter::set_route(const Route *r)
{
- BlockIter fncb = train.get_first_noncritical_block();
-
- Route *lead = 0;
- if(r && train.is_placed())
- {
- const BlockAllocator &allocator = train.get_block_allocator();
- TrackIter first = allocator.first().track_iter();
- TrackIter next = fncb.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);
- // TODO Check if eclipsed by lead route
if(r)
+ {
routes.push_back(r);
+ create_lead_route();
+ }
destination = 0;
waypoints.clear();
const list<Route *> &planned_routes = planner->get_routes_for(train);
routes.clear();
- Route *lead = create_lead_route(0, planned_routes.front());
- routes.push_back(lead);
-
- list<Route *>::const_iterator begin = planned_routes.begin();
- for(; begin!=planned_routes.end(); ++begin)
- {
- const Route::TrackSet &tracks = (*begin)->get_tracks();
- bool eclipsed = true;
- for(Route::TrackSet::const_iterator i=tracks.begin(); (eclipsed && i!=tracks.end()); ++i)
- eclipsed = lead->has_track(**i);
- if(!eclipsed)
- break;
- }
- routes.insert(routes.end(), begin, planned_routes.end());
+ routes.insert(routes.end(), planned_routes.begin(), planned_routes.end());
+ create_lead_route();
sequence_points = planner->get_sequence_for(train);
current_sequence = 0;
metrics_stale = false;
}
-Route *TrainRouter::create_lead_route(Route *lead, const Route *target)
+bool TrainRouter::create_lead_route()
{
- if(!lead)
+ if(routes.empty() || !train.is_placed())
+ return false;
+
+ BlockIter fncb = train.get_first_noncritical_block();
+ TrackIter next_track = fncb.track_iter();
+ if(!routes.front()->has_track(*next_track.flip()))
{
- lead = new Route(train.get_layout());
- lead->set_name("Lead");
- lead->set_temporary(true);
+ Route *pf = Route::find(next_track, *routes.front());
+ if(!pf)
+ return false;
+
+ routes.push_front(pf);
}
- unsigned target_tracks = 0;
- for(TrackIter i=train.get_block_allocator().first().track_iter(); (target_tracks<2 && i); i=i.next())
+ TrackIter first_track = train.get_block_allocator().first().track_iter();
+ if(!routes.front()->has_track(*first_track))
{
- if(i->get_block().get_train()!=&train)
- break;
- if(target)
+ Route *lead = new Route(train.get_layout());
+ lead->set_name("Lead");
+ lead->set_temporary(true);
+
+ unsigned target_tracks = 0;
+ for(TrackIter i=first_track; (target_tracks<2 && i); i=i.next())
{
- if(target->has_track(*i))
+ if(i->get_block().get_train()!=&train)
+ break;
+ if(routes.front()->has_track(*i))
++target_tracks;
else if(target_tracks>0)
break;
+ lead->add_track(*i);
}
- lead->add_track(*i);
+
+ routes.push_front(lead);
}
- return lead;
+ return true;
}
bool TrainRouter::is_valid_for_track(const Route &route, Track &track) const