]> git.tdb.fi Git - r2c2.git/commitdiff
Further improve lead route generation
authorMikko Rasa <tdb@tdb.fi>
Fri, 6 Feb 2015 14:26:10 +0000 (16:26 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 6 Feb 2015 14:48:18 +0000 (16:48 +0200)
The previous version generated nonsense if the first actual route was
completely covered by existing critical blocks.

source/libr2c2/trainrouter.cpp

index 38027dbe896942f6b232b1abcd565393a99d9ca5..266bc6fc3641f4e6a72c069771132cb734f0546b 100644 (file)
@@ -420,7 +420,21 @@ bool TrainRouter::create_lead_route()
 
        BlockIter fncb = train.get_first_noncritical_block();
        TrackIter next_track = fncb.track_iter();
-       if(!routes.front()->has_track(*next_track.flip()))
+
+       unsigned count = 0;
+       for(TrackIter i=next_track.flip(); (i && i->get_block().get_train()==&train); i=i.next())
+       {
+               if(routes.front()->has_track(*i))
+                       ++count;
+               else if(count>0)
+               {
+                       if(count==routes.front()->get_tracks().size())
+                               next_track = i.flip();
+                       break;
+               }
+       }
+
+       if(!routes.front()->has_track(*next_track) && !routes.front()->has_track(*next_track.flip()))
        {
                Route *pf = Route::find(next_track, *routes.front());
                if(!pf)
@@ -429,26 +443,23 @@ bool TrainRouter::create_lead_route()
                routes.push_front(pf);
        }
 
-       TrackIter first_track = train.get_block_allocator().first().track_iter();
-       if(!routes.front()->has_track(*first_track))
+       Route *lead = 0;
+       for(TrackIter i=next_track.flip(); (i && i->get_block().get_train()==&train); i=i.next())
        {
-               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(!lead && !routes.front()->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 = 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());
                }
 
-               routes.push_front(lead);
+               if(lead)
+                       lead->add_track(*i);
        }
 
        return true;