]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/block.cpp
Major architecture rework
[r2c2.git] / source / libmarklin / block.cpp
index a29469c4fd9b97e32751987e3c42ac6f903b078e..51f8ddb0dfe45dbd954f994ba75b1c82f759e290 100644 (file)
@@ -1,24 +1,22 @@
 /* $Id$
 
 This file is part of the MSP Märklin suite
-Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
 #include <algorithm>
-#include "control.h"
 #include "block.h"
+#include "layout.h"
 #include "tracktype.h"
-#include "trafficmanager.h"
-#include "turnout.h"
 
 using namespace std;
 using namespace Msp;
 
 namespace Marklin {
 
-Block::Block(TrafficManager &tm, Track &start):
-       trfc_mgr(tm),
+Block::Block(Layout &l, Track &start):
+       layout(l),
        id(0),
        sensor_id(start.get_sensor_id()),
        turnout_id(start.get_turnout_id()),
@@ -48,10 +46,7 @@ Block::Block(TrafficManager &tm, Track &start):
                        }
        }
 
-       if(sensor_id)
-               id = 0x1000|sensor_id;
-       else if(turnout_id)
-               id = 0x2000|turnout_id;
+       determine_id();
 
        for(unsigned i=0; i<endpoints.size(); ++i)
        {
@@ -60,6 +55,20 @@ Block::Block(TrafficManager &tm, Track &start):
                set<Track *> visited;
                find_paths(*endpoints[i].track, endpoints[i].track_ep, path, visited);
        }
+
+       layout.add_block(*this);
+}
+
+Block::~Block()
+{
+       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
@@ -85,13 +94,7 @@ unsigned Block::traverse(unsigned epi, float *len) const
 
        while(1)
        {
-               unsigned cur_path = 0;
-               unsigned tid = track->get_turnout_id();
-               if(tid)
-               {
-                       Turnout &turnout = trfc_mgr.get_control().get_turnout(tid);
-                       cur_path = turnout.get_path();
-               }
+               unsigned cur_path = track->get_active_path();
 
                if(len)
                        *len += track->get_type().get_path_length(cur_path);
@@ -121,17 +124,22 @@ void Block::check_link(Block &other)
                        {
                                i->link = &other;
                                j->link = this;
+
+                               determine_id();
+                               other.determine_id();
                        }
        }
+}
 
-       if(!sensor_id && !turnout_id && endpoints.size()==2)
-       {
-               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;
-       }
+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
@@ -141,12 +149,12 @@ 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;
-               trfc_mgr.signal_block_reserved.emit(*this, train);
+               layout.signal_block_reserved.emit(*this, train);
                return true;
        }
        else
@@ -177,6 +185,22 @@ void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track
        }
 }
 
+void Block::determine_id()
+{
+       if(sensor_id)
+               id = 0x1000|sensor_id;
+       else if(turnout_id)
+               id = 0x2000|turnout_id;
+       else if(endpoints.size()==2)
+       {
+               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;
+       }
+}
+
 
 Block::Endpoint::Endpoint(Track *t, unsigned e):
        track(t),