]> git.tdb.fi Git - r2c2.git/commitdiff
Restructure lead route generation
authorMikko Rasa <tdb@tdb.fi>
Thu, 5 Feb 2015 13:58:45 +0000 (15:58 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 5 Feb 2015 13:58:45 +0000 (15:58 +0200)
All of the relevant logic is now in one place.

source/libr2c2/trainrouter.cpp
source/libr2c2/trainrouter.h

index ff3c66402ad98b84e830cbe04dd77cd0bc0d8cca..13b2970883535f99a9869939f83e32ab81ace7e0 100644 (file)
@@ -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<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;
@@ -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
index 2b59603dff6b9e8ef2753c36917a3335931e64c7..7a560a11904d7475b961198d191b6103585ec311 100644 (file)
@@ -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 &);