]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/block.cpp
Get rid of Train::try_reserve and listen to signal_block_reserved instead
[r2c2.git] / source / libmarklin / block.cpp
index e9c170c1ccb7140911666f3ca3704892a440af90..a29469c4fd9b97e32751987e3c42ac6f903b078e 100644 (file)
@@ -5,7 +5,7 @@ Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
-#include <iostream>
+#include <algorithm>
 #include "control.h"
 #include "block.h"
 #include "tracktype.h"
@@ -17,11 +17,9 @@ using namespace Msp;
 
 namespace Marklin {
 
-unsigned Block::next_id = 1;
-
 Block::Block(TrafficManager &tm, Track &start):
        trfc_mgr(tm),
-       id(next_id++),
+       id(0),
        sensor_id(start.get_sensor_id()),
        turnout_id(start.get_turnout_id()),
        train(0)
@@ -50,6 +48,11 @@ Block::Block(TrafficManager &tm, Track &start):
                        }
        }
 
+       if(sensor_id)
+               id = 0x1000|sensor_id;
+       else if(turnout_id)
+               id = 0x2000|turnout_id;
+
        for(unsigned i=0; i<endpoints.size(); ++i)
        {
                unsigned path = 1<<i;
@@ -120,6 +123,15 @@ void Block::check_link(Block &other)
                                j->link = this;
                        }
        }
+
+       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;
+       }
 }
 
 Block *Block::get_link(unsigned epi) const
@@ -141,23 +153,6 @@ bool Block::reserve(const Train *t)
                return false;
 }
 
-void Block::print_debug()
-{
-       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<<", paths="<<i->paths<<'\n';
-       }
-}
-
 void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track *> &visited)
 {
        visited.insert(&track);