]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/block.cpp
Make LCD output selectable at runtime through an extra I/O pin
[r2c2.git] / source / libmarklin / block.cpp
index aa16fffc6ba5b6e3f04879f0e3d5fdb206b809c3..e8e367f7255715dc3c7ac2343763ae7da11021bf 100644 (file)
@@ -8,6 +8,8 @@ Distributed under the GPL
 #include <algorithm>
 #include "block.h"
 #include "layout.h"
+#include "route.h"
+#include "trackiter.h"
 #include "tracktype.h"
 
 using namespace std;
@@ -23,6 +25,7 @@ Block::Block(Layout &l, Track &start):
        train(0)
 {
        tracks.insert(&start);
+       start.set_block(this);
 
        list<Track *> queue;
        queue.push_back(&start);
@@ -40,6 +43,7 @@ Block::Block(Layout &l, Track &start):
                                {
                                        queue.push_back(links[i]);
                                        tracks.insert(links[i]);
+                                       links[i]->set_block(this);
                                }
                                else
                                        endpoints.push_back(Endpoint(track, i));
@@ -52,8 +56,7 @@ Block::Block(Layout &l, Track &start):
        {
                unsigned path = 1<<i;
                endpoints[i].paths |= path;
-               set<Track *> visited;
-               find_paths(*endpoints[i].track, endpoints[i].track_ep, path, visited);
+               find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path);
        }
 
        layout.add_block(*this);
@@ -61,6 +64,11 @@ Block::Block(Layout &l, Track &start):
 
 Block::~Block()
 {
+       set<Track *> trks = tracks;
+       tracks.clear();
+       for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
+               (*i)->set_block(0);
+
        for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
                if(Block *blk = i->link)
                {
@@ -71,7 +79,20 @@ Block::~Block()
        layout.remove_block(*this);
 }
 
-int Block::get_endpoint_by_link(const Block &other) const
+bool Block::has_track(Track &t) const
+{
+       return tracks.count(&t);
+}
+
+const Block::Endpoint &Block::get_endpoint(unsigned i) const
+{
+       if(i>=endpoints.size())
+               throw InvalidParameterValue("Endpoint index out of range");
+
+       return endpoints[i];
+}
+
+int Block::get_endpoint_by_link(Block &other) const
 {
        for(unsigned i=0; i<endpoints.size(); ++i)
                if(endpoints[i].link==&other)
@@ -80,36 +101,23 @@ int Block::get_endpoint_by_link(const Block &other) const
        return -1;
 }
 
-unsigned Block::traverse(unsigned epi, float *len) const
+float Block::get_path_length(unsigned entry, const Route *route) const
 {
-       if(epi>=endpoints.size())
+       if(entry>=endpoints.size())
                throw InvalidParameterValue("Endpoint index out of range");
 
-       const Endpoint &ep = endpoints[epi];
-       Track *track = ep.track;
-       unsigned track_ep = ep.track_ep;
+       TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep);
 
-       if(len)
-               *len = 0;
-
-       while(1)
+       float result = 0;
+       while(t_iter && has_track(*t_iter))
        {
-               unsigned cur_path = track->get_active_path();
-
-               if(len)
-                       *len += track->get_type().get_path_length(cur_path);
-
-               unsigned other_ep = track->traverse(track_ep, cur_path);
-               for(unsigned i=0; i<endpoints.size(); ++i)
-                       if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
-                               return i;
+               unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
+               result += t_iter->get_type().get_path_length(path);
 
-               Track *next = track->get_link(other_ep);
-               if(!tracks.count(next))
-                       throw LogicError("Block traversal strayed out of the block");
-               track_ep = next->get_endpoint_by_link(*track);
-               track = next;
+               t_iter = t_iter.next(path);
        }
+
+       return result;
 }
 
 void Block::check_link(Block &other)
@@ -161,28 +169,25 @@ bool Block::reserve(Train *t)
                return false;
 }
 
-void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track *> &visited)
+void Block::find_paths(TrackIter track, unsigned path)
 {
-       visited.insert(&track);
-
-       const vector<Marklin::Endpoint> &eps = track.get_type().get_endpoints();
-       for(unsigned i=0; i<eps.size(); ++i)
-       {
-               if(i==track_ep) continue;
-               Track *link = track.get_link(i);
-               if(!link) continue;
-               if(visited.count(link)) continue;
-               if(!(eps[i].paths&eps[track_ep].paths)) continue;
-
-               if(tracks.count(link))
-                       find_paths(*link, link->get_endpoint_by_link(track), path, visited);
-               else
+       unsigned mask = track.endpoint().paths;
+       for(unsigned i=0; mask>>i; ++i)
+               if(mask&(1<<i))
                {
-                       for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
-                               if(j->track==&track && j->track_ep==i)
-                                       j->paths |= path;
+                       TrackIter next = track.next(i);
+                       if(!next)
+                               continue;
+                       else if(has_track(*next))
+                               find_paths(track.next(i), path);
+                       else
+                       {
+                               next = next.flip();
+                               for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
+                                       if(j->track==next.track() && j->track_ep==next.entry())
+                                               j->paths |= path;
+                       }
                }
-       }
 }
 
 void Block::determine_id()