3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
10 #include "tracktype.h"
11 #include "trafficmanager.h"
19 unsigned Block::next_id = 1;
21 Block::Block(TrafficManager &tm, Track &start):
24 sensor_id(start.get_sensor_id()),
25 turnout_id(start.get_turnout_id()),
28 tracks.insert(&start);
31 queue.push_back(&start);
35 Track *track = queue.front();
36 queue.erase(queue.begin());
38 const vector<Track *> &links = track->get_links();
39 for(unsigned i=0; i<links.size(); ++i)
40 if(links[i] && !tracks.count(links[i]))
42 if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
44 queue.push_back(links[i]);
45 tracks.insert(links[i]);
48 endpoints.push_back(Endpoint(track, i));
52 for(unsigned i=0; i<endpoints.size(); ++i)
55 endpoints[i].paths |= path;
57 find_paths(*endpoints[i].track, endpoints[i].track_ep, path, visited);
61 int Block::get_endpoint_by_link(const Block &other) const
63 for(unsigned i=0; i<endpoints.size(); ++i)
64 if(endpoints[i].link==&other)
70 unsigned Block::traverse(unsigned epi, float *len) const
72 if(epi>=endpoints.size())
73 throw InvalidParameterValue("Endpoint index out of range");
75 const Endpoint &ep = endpoints[epi];
76 Track *track = ep.track;
77 unsigned track_ep = ep.track_ep;
84 unsigned cur_path = 0;
85 unsigned tid = track->get_turnout_id();
88 Turnout &turnout = trfc_mgr.get_control().get_turnout(tid);
89 cur_path = turnout.get_path();
93 *len += track->get_type().get_path_length(cur_path);
95 unsigned other_ep = track->traverse(track_ep, cur_path);
96 for(unsigned i=0; i<endpoints.size(); ++i)
97 if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
100 Track *next = track->get_link(other_ep);
101 if(!tracks.count(next))
102 throw LogicError("Block traversal strayed out of the block");
103 track_ep = next->get_endpoint_by_link(*track);
108 void Block::check_link(Block &other)
110 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
115 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
116 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
124 Block *Block::get_link(unsigned epi) const
126 if(epi>=endpoints.size())
127 throw InvalidParameterValue("Endpoint index out of range");
128 return endpoints[epi].link;
131 bool Block::reserve(const Train *t)
136 trfc_mgr.signal_block_reserved.emit(*this, train);
143 void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track *> &visited)
145 visited.insert(&track);
147 const vector<Marklin::Endpoint> &eps = track.get_type().get_endpoints();
148 for(unsigned i=0; i<eps.size(); ++i)
150 if(i==track_ep) continue;
151 Track *link = track.get_link(i);
153 if(visited.count(link)) continue;
154 if(!(eps[i].paths&eps[track_ep].paths)) continue;
156 if(tracks.count(link))
157 find_paths(*link, link->get_endpoint_by_link(track), path, visited);
160 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
161 if(j->track==&track && j->track_ep==i)
168 Block::Endpoint::Endpoint(Track *t, unsigned e):
175 } // namespace Marklin