3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
11 #include "tracktype.h"
12 #include "trafficmanager.h"
20 Block::Block(TrafficManager &tm, Track &start):
23 sensor_id(start.get_sensor_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() && !track->get_turnout_id())
42 queue.push_back(links[i]);
43 tracks.insert(links[i]);
46 endpoints.push_back(Endpoint(track, i));
50 for(unsigned i=0; i<endpoints.size(); ++i)
53 endpoints[i].routes|=route;
55 find_routes(*endpoints[i].track, endpoints[i].track_ep, route, visited);
59 int Block::get_endpoint_by_link(const Block &other) const
61 for(unsigned i=0; i<endpoints.size(); ++i)
62 if(endpoints[i].link==&other)
68 int Block::traverse(unsigned epi, float *len) const
70 if(epi>=endpoints.size())
71 throw InvalidParameterValue("Endpoint index out of range");
73 const Endpoint &ep=endpoints[epi];
74 Track *track=ep.track;
75 unsigned track_ep=ep.track_ep;
83 unsigned tid=track->get_turnout_id();
86 Turnout &turnout=trfc_mgr.get_control().get_turnout(tid);
87 cur_route=turnout.get_route();
91 *len+=track->get_type().get_route_length(cur_route);
93 int other_ep=track->traverse(track_ep, cur_route);
97 for(unsigned i=0; i<endpoints.size(); ++i)
98 if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
101 Track *next=track->get_link(other_ep);
102 if(tracks.count(next)==0)
104 track_ep=next->get_endpoint_by_link(*track);
109 void Block::check_link(Block &other)
111 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
116 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
117 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
125 Block *Block::get_link(unsigned epi) const
127 if(epi>=endpoints.size())
128 throw InvalidParameterValue("Endpoint index out of range");
129 return endpoints[epi].link;
132 bool Block::reserve(const Train *t)
137 trfc_mgr.signal_block_reserved.emit(*this, train);
144 void Block::print_debug()
147 if((*tracks.begin())->get_sensor_id())
148 cout<<", sensor="<<(*tracks.begin())->get_sensor_id();
150 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
152 cout<<" Endpoint, link=";
157 cout<<", routes="<<i->routes<<'\n';
161 void Block::find_routes(Track &track, unsigned track_ep, unsigned route, set<Track *> &visited)
163 visited.insert(&track);
165 const vector<Marklin::Endpoint> &eps=track.get_type().get_endpoints();
166 for(unsigned i=0; i<eps.size(); ++i)
168 if(i==track_ep) continue;
169 Track *link=track.get_link(i);
171 if(visited.count(link)) continue;
172 if(!(eps[i].routes&eps[track_ep].routes)) continue;
174 if(tracks.count(link))
175 find_routes(*link, link->get_endpoint_by_link(track), route, visited);
178 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
179 if(j->track==&track && j->track_ep==i)
185 unsigned Block::next_id=1;
188 Block::Endpoint::Endpoint(Track *t, unsigned e):
195 } // namespace Marklin