]> git.tdb.fi Git - r2c2.git/commitdiff
Fix and improve Route::check_validity
authorMikko Rasa <tdb@tdb.fi>
Fri, 12 Nov 2010 21:45:26 +0000 (21:45 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 12 Nov 2010 21:45:26 +0000 (21:45 +0000)
source/libmarklin/route.cpp

index befaa6de9245a9dbe5d36f02a3a333f265c14fc1..79886d28c371c793ed223909ef343b79edc0b726 100644 (file)
@@ -180,7 +180,7 @@ void Route::update_turnouts()
                        const vector<Track *> &links = (*i)->get_links();
 
                        // Build a combined path mask from linked endpoints
-                       unsigned mask = 15;
+                       unsigned mask = (*i)->get_type().get_paths();
                        for(unsigned j=0; j<endpoints.size(); ++j)
                        {
                                if(!tracks.count(links[j]))
@@ -329,50 +329,61 @@ void Route::save(list<DataFile::Statement> &st) const
 unsigned Route::check_validity(Track &trk) const
 {
        unsigned result = 4;
-       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+       const vector<Track *> &links = trk.get_links();
+       for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
        {
-               int epi=(*i)->get_endpoint_by_link(trk);
-               if(epi>=0)
+               if(!*i)
+                       continue;
+               if(!tracks.count(*i))
+                       continue;
+
+               // Linked to an existing track - good
+               result |= 1;
+
+               if(unsigned tid = (*i)->get_turnout_id())
                {
-                       // Linked to an existing track - good
-                       result |= 1;
-                       const vector<TrackType::Endpoint> &endpoints = (*i)->get_type().get_endpoints();
-                       if(unsigned tid=(*i)->get_turnout_id())
+                       const TrackType::Endpoint &ep = (*i)->get_type().get_endpoint((*i)->get_endpoint_by_link(trk));
+                       int path = get_turnout(tid);
+                       if(path>=0)
                        {
-                               int r = get_turnout(tid);
-                               if(r>=0)
-                               {
-                                       // Linking to a turnout with path set is only good if we're continuing that path
-                                       if(endpoints[epi].paths&(1<<r))
-                                               result |= 2;
-                               }
-                               else
-                               {
-                                       // Linked to a turnout with no path set - find out other linked tracks 
-                                       unsigned count = 0;
-                                       const vector<Track *> &links = (*i)->get_links();
-                                       int epj = -1;
-                                       for(unsigned k=0; k<endpoints.size(); ++k)
-                                               if(tracks.count(links[k]))
+                               // Linking to a turnout with path set is only good if we're continuing that path
+                               if(ep.paths&(1<<path))
+                                       result |= 2;
+                       }
+                       else
+                       {
+                               // Linked to a turnout with no path set - check other linked tracks 
+                               const vector<Track *> &tlinks = (*i)->get_links();
+                               unsigned count = 0;
+                               for(unsigned j=0; j<tlinks.size(); ++j)
+                                       if(tracks.count(tlinks[j]))
+                                       {
+                                               unsigned tid2 = tlinks[j]->get_turnout_id();
+                                               if(tid2)
                                                {
-                                                       ++count;
-                                                       epj = k;
+                                                       const TrackType::Endpoint &ep2 = tlinks[j]->get_type().get_endpoint(tlinks[j]->get_endpoint_by_link(**i));
+                                                       path = get_turnout(tid2);
+                                                       // Ignore a linked turnout with some other path set
+                                                       if(path>0 && !(ep2.paths&(1<<path)))
+                                                               continue;
                                                }
 
-                                       // Only good if at most one other track is linked to the turnout
-                                       if(count<=1)
-                                       {
-                                               result |= 2;
-                                               if(epj>=0 && !(endpoints[epi].paths&endpoints[epj].paths))
+                                               ++count;
+
+                                               const TrackType::Endpoint &ep2 = (*i)->get_type().get_endpoint(j);
+                                               if(!(ep.paths&ep2.paths))
                                                        // Impossible path through the turnout - not good
                                                        result &= 3;
                                        }
-                               }
+
+                               // Only good if at most one other track is linked to the turnout
+                               if(count<=1)
+                                       result |= 2;
                        }
-                       else
-                               // Linked to something linear - good
-                               result |= 2;
                }
+               else
+                       // Linked to something linear - good
+                       result |= 2;
        }
 
        return result;