]> 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 87654e4387df997ac5b125dac1643a4bdd1cf47c..e8e367f7255715dc3c7ac2343763ae7da11021bf 100644 (file)
@@ -1,56 +1,98 @@
-#include "control.h"
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <algorithm>
 #include "block.h"
+#include "layout.h"
+#include "route.h"
+#include "trackiter.h"
 #include "tracktype.h"
-#include "trafficmanager.h"
-#include "turnout.h"
-
-using namespace Msp;
 
-#include <iostream>
 using namespace std;
+using namespace Msp;
 
 namespace Marklin {
 
-Block::Block(TrafficManager &tm, Track &start):
-       trfc_mgr(tm),
-       id(next_id++),
+Block::Block(Layout &l, Track &start):
+       layout(l),
+       id(0),
        sensor_id(start.get_sensor_id()),
+       turnout_id(start.get_turnout_id()),
        train(0)
 {
        tracks.insert(&start);
+       start.set_block(this);
 
        list<Track *> queue;
        queue.push_back(&start);
 
        while(!queue.empty())
        {
-               Track *track=queue.front();
+               Track *track = queue.front();
                queue.erase(queue.begin());
 
-               const vector<Track *> &links=track->get_links();
+               const vector<Track *> &links = track->get_links();
                for(unsigned i=0; i<links.size(); ++i)
                        if(links[i] && !tracks.count(links[i]))
                        {
-                               if(links[i]->get_sensor_id()==sensor_id)
+                               if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
                                {
                                        queue.push_back(links[i]);
                                        tracks.insert(links[i]);
+                                       links[i]->set_block(this);
                                }
                                else
                                        endpoints.push_back(Endpoint(track, i));
                        }
        }
 
+       determine_id();
+
        for(unsigned i=0; i<endpoints.size(); ++i)
        {
-               unsigned route=1<<i;
-               endpoints[i].routes|=route;
-               set<Track *> visited;
-               find_routes(*endpoints[i].track, endpoints[i].track_ep, route, visited);
+               unsigned path = 1<<i;
+               endpoints[i].paths |= path;
+               find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path);
        }
+
+       layout.add_block(*this);
+}
+
+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)
+               {
+                       i->link = 0;
+                       blk->break_link(*this);
+               }
+
+       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)
@@ -59,40 +101,23 @@ int Block::get_endpoint_by_link(const Block &other) const
        return -1;
 }
 
-int Block::traverse(unsigned epi) 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);
 
-       while(1)
+       float result = 0;
+       while(t_iter && has_track(*t_iter))
        {
-               unsigned cur_route=0;
-               unsigned tid=track->get_turnout_id();
-               if(tid)
-               {
-                       Turnout &turnout=trfc_mgr.get_control().get_turnout(tid);
-                       cur_route=turnout.get_route();
-               }
+               unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
+               result += t_iter->get_type().get_path_length(path);
 
-               int other_ep=track->traverse(track_ep, cur_route);
-               if(other_ep<0)
-                       return 0;
-
-               for(unsigned i=0; i<endpoints.size(); ++i)
-                       if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
-                               return i;
-
-               Track *next=track->get_link(other_ep);
-               track_ep=next->get_endpoint_by_link(*track);
-               track=next;
-
-               if(tracks.count(track)==0)
-                       return -1;
+               t_iter = t_iter.next(path);
        }
+
+       return result;
 }
 
 void Block::check_link(Block &other)
@@ -105,12 +130,26 @@ void Block::check_link(Block &other)
                for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
                        if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
                        {
-                               i->link=&other;
-                               j->link=this;
+                               i->link = &other;
+                               j->link = this;
+
+                               determine_id();
+                               other.determine_id();
                        }
        }
 }
 
+void Block::break_link(Block &other)
+{
+       for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
+               if(i->link==&other)
+               {
+                       i->link = 0;
+                       other.break_link(*this);
+                       determine_id();
+               }
+}
+
 Block *Block::get_link(unsigned epi) const
 {
        if(epi>=endpoints.size())
@@ -118,71 +157,66 @@ Block *Block::get_link(unsigned epi) const
        return endpoints[epi].link;
 }
 
-bool Block::reserve(const Train *t)
+bool Block::reserve(Train *t)
 {
        if(!t || !train)
        {
-               train=t;
-               if(train)
-                       cout<<"Block "<<this<<" reserved for train "<<train->get_name()<<'\n';
-               else
-                       cout<<"Block "<<this<<" freed\n";
-               trfc_mgr.signal_block_reserved.emit(*this, train);
+               train = t;
+               layout.signal_block_reserved.emit(*this, train);
                return true;
        }
        else
                return false;
 }
 
-void Block::print_debug()
+void Block::find_paths(TrackIter track, unsigned path)
 {
-       cout<<"Block "<<id;
-       if((*tracks.begin())->get_sensor_id())
-               cout<<", sensor="<<(*tracks.begin())->get_sensor_id();
-       cout<<'\n';
-       for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
-       {
-               cout<<"  Endpoint, link=";
-               if(i->link)
-                       cout<<i->link->id;
-               else
-                       cout<<"none";
-               cout<<", routes="<<i->routes<<'\n';
-       }
+       unsigned mask = track.endpoint().paths;
+       for(unsigned i=0; mask>>i; ++i)
+               if(mask&(1<<i))
+               {
+                       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::find_routes(Track &track, unsigned track_ep, unsigned route, set<Track *> &visited)
+void Block::determine_id()
 {
-       visited.insert(&track);
-
-       const vector<Marklin::Endpoint> &eps=track.get_type().get_endpoints();
-       for(unsigned i=0; i<eps.size(); ++i)
+       if(sensor_id)
+               id = 0x1000|sensor_id;
+       else if(turnout_id)
+               id = 0x2000|turnout_id;
+       else if(endpoints.size()==2)
        {
-               if(i==track_ep) continue;
-               Track *link=track.get_link(i);
-               if(!link) continue;
-               if(visited.count(link)) continue;
-               if(!(eps[i].routes&eps[track_ep].routes)) continue;
-
-               if(tracks.count(link))
-                       find_routes(*link, link->get_endpoint_by_link(track), route, visited);
-               else
-               {
-                       for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
-                               if(j->track==&track && j->track_ep==i)
-                                       j->routes|=route;
-               }
+               unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
+               unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
+               if(id2<id1)
+                       swap(id1, id2);
+               id = (id1<<16)|id2;
+       }
+       else if(endpoints.size()==1)
+       {
+               unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
+               id = 0x10000 | id1;
        }
 }
 
-unsigned Block::next_id=1;
-
 
 Block::Endpoint::Endpoint(Track *t, unsigned e):
        track(t),
        track_ep(e),
        link(0),
-       routes(0)
+       paths(0)
 { }
 
 } // namespace Marklin