3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
12 #include "tracktype.h"
19 Block::Block(Layout &l, Track &start):
22 sensor_id(start.get_sensor_id()),
23 turnout_id(start.get_turnout_id()),
26 tracks.insert(&start);
29 queue.push_back(&start);
33 Track *track = queue.front();
34 queue.erase(queue.begin());
36 const vector<Track *> &links = track->get_links();
37 for(unsigned i=0; i<links.size(); ++i)
38 if(links[i] && !tracks.count(links[i]))
40 if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
42 queue.push_back(links[i]);
43 tracks.insert(links[i]);
46 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);
60 layout.add_block(*this);
65 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
66 if(Block *blk = i->link)
69 blk->break_link(*this);
72 layout.remove_block(*this);
75 int Block::get_endpoint_by_link(const Block &other) const
77 for(unsigned i=0; i<endpoints.size(); ++i)
78 if(endpoints[i].link==&other)
84 unsigned Block::traverse(unsigned epi, float *len) const
86 return traverse(epi, 0, len);
89 unsigned Block::traverse(unsigned epi, const Route *route, float *len) const
91 if(epi>=endpoints.size())
92 throw InvalidParameterValue("Endpoint index out of range");
94 const Endpoint &ep = endpoints[epi];
95 Track *track = ep.track;
96 unsigned track_ep = ep.track_ep;
104 if(track->get_turnout_id() && route)
105 cur_path = route->get_turnout(track->get_turnout_id());
107 cur_path = track->get_active_path();
110 *len += track->get_type().get_path_length(cur_path);
112 unsigned other_ep = track->traverse(track_ep, cur_path);
113 for(unsigned i=0; i<endpoints.size(); ++i)
114 if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
117 Track *next = track->get_link(other_ep);
118 if(!tracks.count(next))
119 throw LogicError("Block traversal strayed out of the block");
120 track_ep = next->get_endpoint_by_link(*track);
125 void Block::check_link(Block &other)
127 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
132 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
133 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
139 other.determine_id();
144 void Block::break_link(Block &other)
146 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
150 other.break_link(*this);
155 Block *Block::get_link(unsigned epi) const
157 if(epi>=endpoints.size())
158 throw InvalidParameterValue("Endpoint index out of range");
159 return endpoints[epi].link;
162 bool Block::reserve(Train *t)
167 layout.signal_block_reserved.emit(*this, train);
174 void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track *> &visited)
176 visited.insert(&track);
178 const vector<Marklin::Endpoint> &eps = track.get_type().get_endpoints();
179 for(unsigned i=0; i<eps.size(); ++i)
181 if(i==track_ep) continue;
182 Track *link = track.get_link(i);
184 if(visited.count(link)) continue;
185 if(!(eps[i].paths&eps[track_ep].paths)) continue;
187 if(tracks.count(link))
188 find_paths(*link, link->get_endpoint_by_link(track), path, visited);
191 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
192 if(j->track==&track && j->track_ep==i)
198 void Block::determine_id()
201 id = 0x1000|sensor_id;
203 id = 0x2000|turnout_id;
204 else if(endpoints.size()==2)
206 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
207 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
212 else if(endpoints.size()==1)
214 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
220 Block::Endpoint::Endpoint(Track *t, unsigned e):
227 } // namespace Marklin