if(r && !cur_blocks.empty())
{
+ BlockRef &first = cur_blocks.front();
BlockRef &last = (rsv_blocks.empty() ? cur_blocks.back() : rsv_blocks.back());
BlockRef next = last.next();
- const Block::Endpoint &ep = next.block->get_endpoints()[next.entry];
- if(!r->get_tracks().count(ep.track))
- routes.push_front(Route::find(*ep.track, ep.track_ep, *r));
-
- /* XXX This is sort of a hack, but it keeps divert() happy. Need to come
- up with a better solution when there is time. */
- routes.push_front(create_lead_route());
+ const Block::Endpoint &first_ep = first.block->get_endpoints()[first.entry];
+ const Block::Endpoint &next_ep = next.block->get_endpoints()[next.entry];
+ if(!r->get_tracks().count(next_ep.track))
+ {
+ Route *lead = Route::find(*next_ep.track, next_ep.track_ep, *r);
+ create_lead_route(lead, lead);
+ routes.push_front(lead);
+ }
+ else if(!r->get_tracks().count(first_ep.track))
+ routes.push_front(create_lead_route(0, r));
}
reserve_more();
BlockRef next = last.next();
const Block::Endpoint &ep = next.block->get_endpoints()[next.entry];
- set_route(Route::find(*ep.track, ep.track_ep, to));
+ Route *route = Route::find(*ep.track, ep.track_ep, to);
+ create_lead_route(route, route);
+ set_route(route);
}
bool Train::divert(Track &from)
return true;
}
-Route *Train::create_lead_route()
+Route *Train::create_lead_route(Route *lead, const Route *target)
{
- Route *lead = new Route(layout);
- lead->set_name("Lead");
- lead->set_temporary(true);
+ if(!lead)
+ {
+ lead = new Route(layout);
+ lead->set_name("Lead");
+ lead->set_temporary(true);
+ }
+ set<const Track *> tracks;
for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=rsv_blocks.end(); )
{
- // XXX Make Route eat non-const tracks to get rid of this idiocy and various const_casts
const set<Track *> &btracks = i->block->get_tracks();
- set<const Track *> tracks(btracks.begin(), btracks.end());
- lead->add_tracks(tracks);
+ for(set<Track *>::const_iterator j=btracks.begin(); j!=btracks.end(); ++j)
+ if(!target || !target->get_tracks().count(*j))
+ tracks.insert(*j);
if(++i==cur_blocks.end())
i = rsv_blocks.begin();
}
+ lead->add_tracks(tracks);
+
return lead;
}