#include <algorithm>
#include "block.h"
#include "layout.h"
+#include "route.h"
+#include "trackiter.h"
#include "tracktype.h"
using namespace std;
train(0)
{
tracks.insert(&start);
+ start.set_block(this);
list<Track *> queue;
queue.push_back(&start);
{
queue.push_back(links[i]);
tracks.insert(links[i]);
+ links[i]->set_block(this);
}
else
endpoints.push_back(Endpoint(track, i));
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)
{
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)
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;
-
- if(len)
- *len = 0;
+ TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep);
- while(1)
+ float result = 0;
+ while(t_iter && has_track(*t_iter))
{
- unsigned cur_path = track->get_active_path();
+ int path = -1;
+ if(t_iter->get_turnout_id() && route)
+ path = route->get_turnout(t_iter->get_turnout_id());
+ if(path==-1)
+ path = t_iter->get_active_path();
- if(len)
- *len += track->get_type().get_path_length(cur_path);
+ result += t_iter->get_type().get_path_length(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;
-
- 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)
{
visited.insert(&track);
- const vector<Marklin::Endpoint> &eps = track.get_type().get_endpoints();
+ const vector<TrackType::Endpoint> &eps = track.get_type().get_endpoints();
for(unsigned i=0; i<eps.size(); ++i)
{
if(i==track_ep) continue;
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;
+ }
}