]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/layout.cpp
Fix some problems with loading routes
[r2c2.git] / source / libmarklin / layout.cpp
index 38336ae8d98f5ff6c3dfe3cbd3b4bc0e3c4bb4ab..afd5f03f81affe1c6cf5a8cb30f8077a0e12daed 100644 (file)
@@ -114,9 +114,11 @@ void Layout::check_routes()
 {
        for(map<string, Route *>::iterator i=routes.begin(); i!=routes.end(); ++i)
        {
-               // We must copy the turnout map, since adding tracks to the route will (temporarily) mess it up
+               /* We must copy the turnout map, since adding tracks to the route will
+               (temporarily) mess it up */
                const map<unsigned, int> turnouts = i->second->get_turnouts();
 
+               // Find any turnout in the route
                Track *track = 0;
                unsigned trk_path = 0;
                for(set<Track *>::const_iterator j=tracks.begin(); j!=tracks.end(); ++j)
@@ -133,35 +135,47 @@ void Layout::check_routes()
                if(!track)
                        continue;
 
-               i->second->add_track(*track);
-
+               // Find an applicable endpoint
                const vector<Endpoint> &eps = track->get_type().get_endpoints();
                unsigned ep = 0;
-               for(unsigned j=0; j<eps.size(); ++i)
+               for(unsigned j=0; j<eps.size(); ++j)
                        if(eps[j].paths&(1<<trk_path))
                        {
                                ep = j;
                                break;
                        }
 
-               Track *start = track;
+               Track *start = 0;
                while(1)
                {
+                       // Traverse the track and get the next one
                        unsigned out_ep = track->traverse(ep, trk_path);
                        Track *next = track->get_links()[out_ep];
                        if(!next || next == start)
                                break;
+
                        ep = next->get_endpoint_by_link(*track);
                        if(next->get_type().get_n_paths()>1)
                        {
+                               // Select correct path across the turnout, or break if we hit an unknown turnout
                                map<unsigned, int>::const_iterator j = turnouts.find(next->get_turnout_id());
                                if(j==turnouts.end())
                                        break;
                                trk_path = j->second;
                        }
                        else
+                       {
                                trk_path = 0;
-                       i->second->add_track(*next);
+
+                               /* Start adding tracks when we find the first non-turnout.  This
+                               prevents the occurrence of ambiguities while adding the tracks */
+                               if(!start)
+                                       start = next;
+                       }
+
+                       if(start)
+                               i->second->add_track(*next);
+
                        track = next;
                }
        }