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 "trackiter.h"
13 #include "tracktype.h"
20 Block::Block(Layout &l, Track &start):
23 sensor_id(start.get_sensor_id()),
24 turnout_id(start.get_turnout_id()),
27 tracks.insert(&start);
28 start.set_block(this);
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]);
46 links[i]->set_block(this);
49 endpoints.push_back(Endpoint(track, i));
55 for(unsigned i=0; i<endpoints.size(); ++i)
58 endpoints[i].paths |= path;
60 find_paths(*endpoints[i].track, endpoints[i].track_ep, path, visited);
63 layout.add_block(*this);
68 set<Track *> trks = tracks;
70 for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
73 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
74 if(Block *blk = i->link)
77 blk->break_link(*this);
80 layout.remove_block(*this);
83 bool Block::has_track(Track &t) const
85 return tracks.count(&t);
88 int Block::get_endpoint_by_link(Block &other) const
90 for(unsigned i=0; i<endpoints.size(); ++i)
91 if(endpoints[i].link==&other)
97 float Block::get_path_length(unsigned entry, const Route *route) const
99 if(entry>=endpoints.size())
100 throw InvalidParameterValue("Endpoint index out of range");
102 TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep);
105 while(t_iter && has_track(*t_iter))
108 if(t_iter->get_turnout_id() && route)
109 path = route->get_turnout(t_iter->get_turnout_id());
111 path = t_iter->get_active_path();
113 result += t_iter->get_type().get_path_length(path);
115 t_iter = t_iter.next(path);
121 void Block::check_link(Block &other)
123 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
128 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
129 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
135 other.determine_id();
140 void Block::break_link(Block &other)
142 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
146 other.break_link(*this);
151 Block *Block::get_link(unsigned epi) const
153 if(epi>=endpoints.size())
154 throw InvalidParameterValue("Endpoint index out of range");
155 return endpoints[epi].link;
158 bool Block::reserve(Train *t)
163 layout.signal_block_reserved.emit(*this, train);
170 void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track *> &visited)
172 visited.insert(&track);
174 const vector<TrackType::Endpoint> &eps = track.get_type().get_endpoints();
175 for(unsigned i=0; i<eps.size(); ++i)
177 if(i==track_ep) continue;
178 Track *link = track.get_link(i);
180 if(visited.count(link)) continue;
181 if(!(eps[i].paths&eps[track_ep].paths)) continue;
183 if(tracks.count(link))
184 find_paths(*link, link->get_endpoint_by_link(track), path, visited);
187 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
188 if(j->track==&track && j->track_ep==i)
194 void Block::determine_id()
197 id = 0x1000|sensor_id;
199 id = 0x2000|turnout_id;
200 else if(endpoints.size()==2)
202 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
203 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
208 else if(endpoints.size()==1)
210 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
216 Block::Endpoint::Endpoint(Track *t, unsigned e):
223 } // namespace Marklin