]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/route.cpp
Set correct Z coordinate to vehicle position
[r2c2.git] / source / libmarklin / route.cpp
index 2ff6962fe67551a17be8bde9052595b6933bf171..7ecd473694926ea306067f1993e03ad16b08a0cf 100644 (file)
@@ -19,6 +19,8 @@ namespace {
 
 using namespace Marklin;
 
+typedef std::pair<const Track *, unsigned> Key;
+
 struct Node
 {
        const Track *track;
@@ -63,8 +65,7 @@ struct TrackInRoute
 template<typename Pred>
 list<const Track *> dijkstra(const Track &from, unsigned ep, const Pred &goal)
 {
-       // XXX Fails to find some routes - should use track+ep as key
-       map<const Track *, Node> track_nodes;
+       map<Key, Node> track_nodes;
        priority_queue<Node> nodes;
        Node *final = 0;
 
@@ -75,10 +76,11 @@ list<const Track *> dijkstra(const Track &from, unsigned ep, const Pred &goal)
                Node lowest = nodes.top();
                nodes.pop();
 
-               if(track_nodes.count(lowest.track))
+               Key key(lowest.track, lowest.ep);
+               if(track_nodes.count(key))
                        continue;
 
-               Node &ref = track_nodes[lowest.track] = lowest;
+               Node &ref = track_nodes[key] = lowest;
                if(goal(*lowest.track))
                {
                        final = &ref;
@@ -90,7 +92,11 @@ list<const Track *> dijkstra(const Track &from, unsigned ep, const Pred &goal)
                const vector<Track *> &links = lowest.track->get_links();
                for(unsigned i=0; i<eps.size(); ++i)
                {
-                       if(i==lowest.ep || !links[i] || track_nodes.count(links[i]))
+                       if(i==lowest.ep || !links[i])
+                               continue;
+
+                       unsigned link_ep = links[i]->get_endpoint_by_link(*lowest.track);
+                       if(track_nodes.count(Key(links[i], link_ep)))
                                continue;
 
                        unsigned mask = eps[i].paths&eps[lowest.ep].paths;
@@ -124,6 +130,8 @@ Route *create_route(const Track &from, unsigned ep, const Pred &goal)
        for(list<const Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
                route->add_track(**i);
 
+       route->set_temporary(true);
+
        return route;
 }
 
@@ -134,7 +142,8 @@ namespace Marklin {
 
 Route::Route(Layout &l, const string &n):
        layout(l),
-       name(n)
+       name(n),
+       temporary(false)
 {
        layout.add_route(*this);
        layout.signal_track_removed.connect(sigc::mem_fun(this, &Route::track_removed));
@@ -145,6 +154,11 @@ Route::~Route()
        layout.remove_route(*this);
 }
 
+void Route::set_temporary(bool t)
+{
+       temporary = t;
+}
+
 int Route::get_turnout(unsigned id) const
 {
        map<unsigned, int>::const_iterator i = turnouts.find(id);