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"
21 Block::Block(TrafficManager &tm, Track &start):
24 sensor_id(start.get_sensor_id()),
27 tracks.insert(&start);
30 queue.push_back(&start);
34 Track *track=queue.front();
35 queue.erase(queue.begin());
37 const vector<Track *> &links=track->get_links();
38 for(unsigned i=0; i<links.size(); ++i)
39 if(links[i] && !tracks.count(links[i]))
41 if(links[i]->get_sensor_id()==sensor_id && !links[i]->get_turnout_id() && !track->get_turnout_id())
43 queue.push_back(links[i]);
44 tracks.insert(links[i]);
47 endpoints.push_back(Endpoint(track, i));
51 for(unsigned i=0; i<endpoints.size(); ++i)
54 endpoints[i].routes|=route;
56 find_routes(*endpoints[i].track, endpoints[i].track_ep, route, visited);
60 int Block::get_endpoint_by_link(const Block &other) const
62 for(unsigned i=0; i<endpoints.size(); ++i)
63 if(endpoints[i].link==&other)
69 int Block::traverse(unsigned epi, float *len) const
71 if(epi>=endpoints.size())
72 throw InvalidParameterValue("Endpoint index out of range");
74 const Endpoint &ep=endpoints[epi];
75 Track *track=ep.track;
76 unsigned track_ep=ep.track_ep;
84 unsigned tid=track->get_turnout_id();
87 Turnout &turnout=trfc_mgr.get_control().get_turnout(tid);
88 cur_route=turnout.get_route();
92 *len+=track->get_type().get_route_length(cur_route);
94 int other_ep=track->traverse(track_ep, cur_route);
98 for(unsigned i=0; i<endpoints.size(); ++i)
99 if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
102 Track *next=track->get_link(other_ep);
103 if(tracks.count(next)==0)
105 track_ep=next->get_endpoint_by_link(*track);
110 void Block::check_link(Block &other)
112 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
117 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
118 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
126 Block *Block::get_link(unsigned epi) const
128 if(epi>=endpoints.size())
129 throw InvalidParameterValue("Endpoint index out of range");
130 return endpoints[epi].link;
133 bool Block::reserve(const Train *t)
139 cout<<"Block "<<this<<" reserved for train "<<train->get_name()<<'\n';
141 cout<<"Block "<<this<<" freed\n";
142 trfc_mgr.signal_block_reserved.emit(*this, train);
149 void Block::print_debug()
152 if((*tracks.begin())->get_sensor_id())
153 cout<<", sensor="<<(*tracks.begin())->get_sensor_id();
155 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
157 cout<<" Endpoint, link=";
162 cout<<", routes="<<i->routes<<'\n';
166 void Block::find_routes(Track &track, unsigned track_ep, unsigned route, set<Track *> &visited)
168 visited.insert(&track);
170 const vector<Marklin::Endpoint> &eps=track.get_type().get_endpoints();
171 for(unsigned i=0; i<eps.size(); ++i)
173 if(i==track_ep) continue;
174 Track *link=track.get_link(i);
176 if(visited.count(link)) continue;
177 if(!(eps[i].routes&eps[track_ep].routes)) continue;
179 if(tracks.count(link))
180 find_routes(*link, link->get_endpoint_by_link(track), route, visited);
183 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
184 if(j->track==&track && j->track_ep==i)
190 unsigned Block::next_id=1;
193 Block::Endpoint::Endpoint(Track *t, unsigned e):
200 } // namespace Marklin