From: Mikko Rasa Date: Thu, 5 Feb 2015 13:58:45 +0000 (+0200) Subject: Restructure lead route generation X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=462a04be004ab4b0ff3ce14c51a19b50271b17c7;p=r2c2.git Restructure lead route generation All of the relevant logic is now in one place. --- diff --git a/source/libr2c2/trainrouter.cpp b/source/libr2c2/trainrouter.cpp index ff3c664..13b2970 100644 --- a/source/libr2c2/trainrouter.cpp +++ b/source/libr2c2/trainrouter.cpp @@ -40,31 +40,12 @@ void TrainRouter::set_priority(int p) 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(); @@ -234,20 +215,8 @@ void TrainRouter::tick(const Time::TimeDelta &dt) const list &planned_routes = planner->get_routes_for(train); routes.clear(); - Route *lead = create_lead_route(0, planned_routes.front()); - routes.push_back(lead); - - list::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; @@ -434,31 +403,45 @@ void TrainRouter::create_metrics() 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 diff --git a/source/libr2c2/trainrouter.h b/source/libr2c2/trainrouter.h index 2b59603..7a560a1 100644 --- a/source/libr2c2/trainrouter.h +++ b/source/libr2c2/trainrouter.h @@ -105,7 +105,7 @@ private: void train_rear_advanced(Block &); void create_metrics(); - Route *create_lead_route(Route *, const Route *); + bool create_lead_route(); bool is_valid_for_track(const Route &, Track &) const; bool advance_to_track(RouteList::iterator &, Track &);