/* $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 <iostream>
-#include "control.h"
+#include <algorithm>
#include "block.h"
+#include "layout.h"
+#include "route.h"
#include "tracktype.h"
-#include "trafficmanager.h"
-#include "turnout.h"
using namespace std;
using namespace Msp;
namespace Marklin {
-unsigned Block::next_id = 1;
-
-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)
}
}
+ determine_id();
+
for(unsigned i=0; i<endpoints.size(); ++i)
{
unsigned path = 1<<i;
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
+bool Block::has_track(Track &t) const
+{
+ return tracks.count(&t);
+}
+
+int Block::get_endpoint_by_link(Block &other) const
{
for(unsigned i=0; i<endpoints.size(); ++i)
if(endpoints[i].link==&other)
}
unsigned Block::traverse(unsigned epi, float *len) const
+{
+ return traverse(epi, 0, len);
+}
+
+unsigned Block::traverse(unsigned epi, const Route *route, float *len) const
{
if(epi>=endpoints.size())
throw InvalidParameterValue("Endpoint index out of range");
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();
- }
+ int cur_path = -1;
+ if(track->get_turnout_id() && route)
+ cur_path = route->get_turnout(track->get_turnout_id());
+ if(cur_path==-1)
+ cur_path = track->get_active_path();
if(len)
*len += track->get_type().get_path_length(cur_path);
{
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())
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
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);
}
}
+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;
+ }
+ else if(endpoints.size()==1)
+ {
+ unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
+ id = 0x10000 | id1;
+ }
+}
+
Block::Endpoint::Endpoint(Track *t, unsigned e):
track(t),