]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouter.cpp
Fix lead route generation
[r2c2.git] / source / libr2c2 / trainrouter.cpp
index 93dbedf544c27b029a56dc352f1f9e6e502be4ad..5691ac9a30ccdbdcc15e506d3581f2be26cddc31 100644 (file)
@@ -80,6 +80,17 @@ bool TrainRouter::set_route(const Route *r)
        return true;
 }
 
+bool TrainRouter::add_route(const Route &r)
+{
+       if(routes.empty())
+               return set_route(&r);
+
+       // TODO Check that it can be reached from previous routes
+       routes.push_back(&r);
+
+       return true;
+}
+
 void TrainRouter::add_wait(Block &block, Train *tr)
 {
        Wait wait;
@@ -332,17 +343,21 @@ Route *TrainRouter::create_lead_route(Route *lead, const Route *target)
                lead->set_temporary(true);
        }
 
-       set<Track *> tracks;
-       for(BlockIter i=train.get_block_allocator().first(); (i && i->get_train()==&train); i=i.next())
+       bool target_reached = false;
+       for(TrackIter i=train.get_block_allocator().first().track_iter(); i; i=i.next())
        {
-               const set<Track *> &btracks = i->get_tracks();
-               for(set<Track *>::const_iterator j=btracks.begin(); j!=btracks.end(); ++j)
-                       if(!target || !target->has_track(**j))
-                               tracks.insert(*j);
+               if(i->get_block().get_train()!=&train)
+                       break;
+               if(target)
+               {
+                       if(target->has_track(*i))
+                               target_reached = true;
+                       else if(target_reached)
+                               break;
+               }
+               lead->add_track(*i);
        }
 
-       lead->add_tracks(tracks);
-
        return lead;
 }