3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
11 #include "tracktype.h"
18 Block::Block(Layout &l, Track &start):
21 sensor_id(start.get_sensor_id()),
22 turnout_id(start.get_turnout_id()),
25 tracks.insert(&start);
28 queue.push_back(&start);
32 Track *track = queue.front();
33 queue.erase(queue.begin());
35 const vector<Track *> &links = track->get_links();
36 for(unsigned i=0; i<links.size(); ++i)
37 if(links[i] && !tracks.count(links[i]))
39 if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
41 queue.push_back(links[i]);
42 tracks.insert(links[i]);
45 endpoints.push_back(Endpoint(track, i));
51 for(unsigned i=0; i<endpoints.size(); ++i)
54 endpoints[i].paths |= path;
56 find_paths(*endpoints[i].track, endpoints[i].track_ep, path, visited);
59 layout.add_block(*this);
64 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
65 if(Block *blk = i->link)
68 blk->break_link(*this);
71 layout.remove_block(*this);
74 int Block::get_endpoint_by_link(const Block &other) const
76 for(unsigned i=0; i<endpoints.size(); ++i)
77 if(endpoints[i].link==&other)
83 unsigned Block::traverse(unsigned epi, float *len) const
85 if(epi>=endpoints.size())
86 throw InvalidParameterValue("Endpoint index out of range");
88 const Endpoint &ep = endpoints[epi];
89 Track *track = ep.track;
90 unsigned track_ep = ep.track_ep;
97 unsigned cur_path = track->get_active_path();
100 *len += track->get_type().get_path_length(cur_path);
102 unsigned other_ep = track->traverse(track_ep, cur_path);
103 for(unsigned i=0; i<endpoints.size(); ++i)
104 if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
107 Track *next = track->get_link(other_ep);
108 if(!tracks.count(next))
109 throw LogicError("Block traversal strayed out of the block");
110 track_ep = next->get_endpoint_by_link(*track);
115 void Block::check_link(Block &other)
117 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
122 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
123 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
129 other.determine_id();
134 void Block::break_link(Block &other)
136 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
140 other.break_link(*this);
145 Block *Block::get_link(unsigned epi) const
147 if(epi>=endpoints.size())
148 throw InvalidParameterValue("Endpoint index out of range");
149 return endpoints[epi].link;
152 bool Block::reserve(Train *t)
157 layout.signal_block_reserved.emit(*this, train);
164 void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track *> &visited)
166 visited.insert(&track);
168 const vector<Marklin::Endpoint> &eps = track.get_type().get_endpoints();
169 for(unsigned i=0; i<eps.size(); ++i)
171 if(i==track_ep) continue;
172 Track *link = track.get_link(i);
174 if(visited.count(link)) continue;
175 if(!(eps[i].paths&eps[track_ep].paths)) continue;
177 if(tracks.count(link))
178 find_paths(*link, link->get_endpoint_by_link(track), path, visited);
181 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
182 if(j->track==&track && j->track_ep==i)
188 void Block::determine_id()
191 id = 0x1000|sensor_id;
193 id = 0x2000|turnout_id;
194 else if(endpoints.size()==2)
196 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
197 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
205 Block::Endpoint::Endpoint(Track *t, unsigned e):
212 } // namespace Marklin