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 unsigned Block::next_id = 1;
22 Block::Block(TrafficManager &tm, Track &start):
25 sensor_id(start.get_sensor_id()),
26 turnout_id(start.get_turnout_id()),
29 tracks.insert(&start);
32 queue.push_back(&start);
36 Track *track = queue.front();
37 queue.erase(queue.begin());
39 const vector<Track *> &links = track->get_links();
40 for(unsigned i=0; i<links.size(); ++i)
41 if(links[i] && !tracks.count(links[i]))
43 if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
45 queue.push_back(links[i]);
46 tracks.insert(links[i]);
49 endpoints.push_back(Endpoint(track, i));
53 for(unsigned i=0; i<endpoints.size(); ++i)
55 unsigned route = 1<<i;
56 endpoints[i].routes |= route;
58 find_routes(*endpoints[i].track, endpoints[i].track_ep, route, visited);
62 int Block::get_endpoint_by_link(const Block &other) const
64 for(unsigned i=0; i<endpoints.size(); ++i)
65 if(endpoints[i].link==&other)
71 int Block::traverse(unsigned epi, float *len) const
73 if(epi>=endpoints.size())
74 throw InvalidParameterValue("Endpoint index out of range");
76 const Endpoint &ep = endpoints[epi];
77 Track *track = ep.track;
78 unsigned track_ep = ep.track_ep;
85 unsigned cur_route = 0;
86 unsigned tid = track->get_turnout_id();
89 Turnout &turnout = trfc_mgr.get_control().get_turnout(tid);
90 cur_route = turnout.get_route();
94 *len += track->get_type().get_route_length(cur_route);
96 int other_ep = track->traverse(track_ep, cur_route);
100 for(unsigned i=0; i<endpoints.size(); ++i)
101 if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
104 Track *next = track->get_link(other_ep);
105 if(tracks.count(next)==0)
107 track_ep = next->get_endpoint_by_link(*track);
112 void Block::check_link(Block &other)
114 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
119 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
120 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
128 Block *Block::get_link(unsigned epi) const
130 if(epi>=endpoints.size())
131 throw InvalidParameterValue("Endpoint index out of range");
132 return endpoints[epi].link;
135 bool Block::reserve(const Train *t)
140 trfc_mgr.signal_block_reserved.emit(*this, train);
147 void Block::print_debug()
150 if((*tracks.begin())->get_sensor_id())
151 cout<<", sensor="<<(*tracks.begin())->get_sensor_id();
153 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
155 cout<<" Endpoint, link=";
160 cout<<", routes="<<i->routes<<'\n';
164 void Block::find_routes(Track &track, unsigned track_ep, unsigned route, 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].routes&eps[track_ep].routes)) continue;
177 if(tracks.count(link))
178 find_routes(*link, link->get_endpoint_by_link(track), route, visited);
181 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
182 if(j->track==&track && j->track_ep==i)
189 Block::Endpoint::Endpoint(Track *t, unsigned e):
196 } // namespace Marklin