]> git.tdb.fi Git - r2c2.git/commitdiff
Add utility functions to make endpoint path mask operations clearer
authorMikko Rasa <tdb@tdb.fi>
Sat, 23 Feb 2013 10:44:29 +0000 (12:44 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 23 Feb 2013 10:44:29 +0000 (12:44 +0200)
source/libr2c2/route.cpp
source/libr2c2/trackiter.cpp
source/libr2c2/tracktype.cpp
source/libr2c2/tracktype.h
source/libr2c2/train.cpp

index be2eefc258ea8cfc7e135932e9ab325c1611ee96..795ae3215bbcbff77fa7eadc9b0baded96f70dd7 100644 (file)
@@ -198,7 +198,7 @@ void Route::update_turnouts()
                                {
                                        const TrackType::Endpoint &ep = links[j]->get_type().get_endpoint(links[j]->get_endpoint_by_link(**i));
                                        int p = get_turnout(tid2);
-                                       if(p>=0 && !(ep.paths&(1<<p)))
+                                       if(p>=0 && !ep.has_path(p))
                                        {
                                                // The linked track is a turnout and has a path which is incompatible with this endpoint
                                                mask &= ~endpoints[j].paths;
@@ -350,7 +350,7 @@ RouteValidityMask Route::check_validity(Track &trk) const
                        if(path>=0)
                        {
                                // Linking to a turnout with path set is only good if we're continuing that path
-                               if(ep.paths&(1<<path))
+                               if(ep.has_path(path))
                                        result |= ROUTE_LINEAR;
                        }
                        else
@@ -367,14 +367,14 @@ RouteValidityMask Route::check_validity(Track &trk) const
                                                        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)))
+                                                       if(path>=0 && !ep2.has_path(path))
                                                                continue;
                                                }
 
                                                ++count;
 
                                                const TrackType::Endpoint &ep2 = (*i)->get_type().get_endpoint(j);
-                                               if(!(ep.paths&ep2.paths))
+                                               if(!ep.has_common_paths(ep2))
                                                        // Impossible path through the turnout - not good
                                                        result &= ~ROUTE_SMOOTH;
                                        }
@@ -438,10 +438,9 @@ void Route::Loader::finish()
                if(j==turnouts.end())
                        continue;
 
-               unsigned path_mask = 1<<j->second;
                const vector<TrackType::Endpoint> &eps = (*i)->get_type().get_endpoints();
                for(unsigned k=0; k<eps.size(); ++k)
-                       if(eps[k].paths&path_mask)
+                       if(eps[k].has_path(j->second))
                        {
                                Track *link = (*i)->get_link(k);
                                if(!obj.tracks.count(link))
index 64d223e21cc3ce6142061ab7efc670e3fbd715f5..edb0d73473c3de6ab2d08a1d9b56f1a618ab44d6 100644 (file)
@@ -70,7 +70,7 @@ int TrackIter::get_exit(unsigned path) const
        
        // Find an endpoint that's connected to the entry and has the requested path
        for(unsigned i=0; i<eps.size(); ++i)
-               if(i!=_entry && (eps[i].paths&(1<<path)) && (eps[i].paths&eps[_entry].paths))
+               if(i!=_entry && eps[i].has_path(path) && eps[i].has_common_paths(eps[_entry]))
                        return i;
 
        return -1;
index 23c636c6d1cd1689d4a809c7fb23f3929f27c6b7..bf038c52d76c2d19576e5b9a1b30e059a4837306 100644 (file)
@@ -69,7 +69,7 @@ TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
        unsigned part_ep = 0;
        for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
        {
-               if((endpoints[epi].paths&(1<<path)) && i->get_path()!=path)
+               if(endpoints[epi].has_path(path) && i->get_path()!=path)
                        continue;
 
                unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
index a292e5532b625439de120555899ff3637391047b..dd2a7c4ddf29f83c67d8bed648382d449412db46 100644 (file)
@@ -18,6 +18,9 @@ public:
                unsigned paths;
 
                Endpoint(float, float, float, unsigned);
+
+               bool has_path(unsigned p) const { return paths&(1<<p); }
+               bool has_common_paths(const Endpoint &e) const { return paths&e.paths; }
        };
 
        class Loader: public Msp::DataFile::ObjectLoader<TrackType>
index a45675c90c146e078427bb754848b6b907d806d5..eb1133a565e2d94ff4c1f2ea91eae290e8f2fae1 100644 (file)
@@ -674,7 +674,7 @@ void Train::reserve_more()
        else if(&*start==pending_block)
        {
                TrackIter track = start.track_iter();
-               if(!(track.endpoint().paths&(1<<track->get_active_path())))
+               if(!track.endpoint().has_path(track->get_active_path()))
                        return;
        }
 
@@ -736,11 +736,12 @@ void Train::reserve_more()
                {
                        const TrackType::Endpoint &entry_ep = track.endpoint();
                        unsigned path = track->get_active_path();
-                       if(!(entry_ep.paths&(1<<path)))
+                       if(!entry_ep.has_path(path))
                        {
                                const TrackType::Endpoint &exit_ep = track.reverse().endpoint();
-                               if(unsigned mask = entry_ep.paths&exit_ep.paths)
+                               if(entry_ep.has_common_paths(exit_ep))
                                {
+                                       unsigned mask = entry_ep.paths&exit_ep.paths;
                                        for(path=0; mask>1; ++path, mask>>=1) ;
 
                                        track->set_active_path(path);