4 #include "trafficmanager.h"
14 Block::Block(TrafficManager &tm, Track &start):
17 sensor_id(start.get_sensor_id()),
20 tracks.insert(&start);
23 queue.push_back(&start);
27 Track *track=queue.front();
28 queue.erase(queue.begin());
30 const vector<Track *> &links=track->get_links();
31 for(unsigned i=0; i<links.size(); ++i)
32 if(links[i] && !tracks.count(links[i]))
34 if(links[i]->get_sensor_id()==sensor_id)
36 queue.push_back(links[i]);
37 tracks.insert(links[i]);
40 endpoints.push_back(Endpoint(track, i));
44 for(unsigned i=0; i<endpoints.size(); ++i)
47 endpoints[i].routes|=route;
49 find_routes(*endpoints[i].track, endpoints[i].track_ep, route, visited);
53 int Block::get_endpoint_by_link(const Block &other) const
55 for(unsigned i=0; i<endpoints.size(); ++i)
56 if(endpoints[i].link==&other)
62 int Block::traverse(unsigned epi, float *len) const
64 if(epi>=endpoints.size())
65 throw InvalidParameterValue("Endpoint index out of range");
67 const Endpoint &ep=endpoints[epi];
68 Track *track=ep.track;
69 unsigned track_ep=ep.track_ep;
77 unsigned tid=track->get_turnout_id();
80 Turnout &turnout=trfc_mgr.get_control().get_turnout(tid);
81 cur_route=turnout.get_route();
85 *len+=track->get_type().get_route_length(cur_route);
87 int other_ep=track->traverse(track_ep, cur_route);
91 for(unsigned i=0; i<endpoints.size(); ++i)
92 if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
95 Track *next=track->get_link(other_ep);
96 track_ep=next->get_endpoint_by_link(*track);
99 if(tracks.count(track)==0)
104 void Block::check_link(Block &other)
106 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
111 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
112 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
120 Block *Block::get_link(unsigned epi) const
122 if(epi>=endpoints.size())
123 throw InvalidParameterValue("Endpoint index out of range");
124 return endpoints[epi].link;
127 bool Block::reserve(const Train *t)
133 cout<<"Block "<<this<<" reserved for train "<<train->get_name()<<'\n';
135 cout<<"Block "<<this<<" freed\n";
136 trfc_mgr.signal_block_reserved.emit(*this, train);
143 void Block::print_debug()
146 if((*tracks.begin())->get_sensor_id())
147 cout<<", sensor="<<(*tracks.begin())->get_sensor_id();
149 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
151 cout<<" Endpoint, link=";
156 cout<<", routes="<<i->routes<<'\n';
160 void Block::find_routes(Track &track, unsigned track_ep, unsigned route, set<Track *> &visited)
162 visited.insert(&track);
164 const vector<Marklin::Endpoint> &eps=track.get_type().get_endpoints();
165 for(unsigned i=0; i<eps.size(); ++i)
167 if(i==track_ep) continue;
168 Track *link=track.get_link(i);
170 if(visited.count(link)) continue;
171 if(!(eps[i].routes&eps[track_ep].routes)) continue;
173 if(tracks.count(link))
174 find_routes(*link, link->get_endpoint_by_link(track), route, visited);
177 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
178 if(j->track==&track && j->track_ep==i)
184 unsigned Block::next_id=1;
187 Block::Endpoint::Endpoint(Track *t, unsigned e):
194 } // namespace Marklin