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) 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;
74 unsigned tid=track->get_turnout_id();
77 Turnout &turnout=trfc_mgr.get_control().get_turnout(tid);
78 cur_route=turnout.get_route();
81 int other_ep=track->traverse(track_ep, cur_route);
85 for(unsigned i=0; i<endpoints.size(); ++i)
86 if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
89 Track *next=track->get_link(other_ep);
90 track_ep=next->get_endpoint_by_link(*track);
93 if(tracks.count(track)==0)
98 void Block::check_link(Block &other)
100 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
105 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
106 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
114 Block *Block::get_link(unsigned epi) const
116 if(epi>=endpoints.size())
117 throw InvalidParameterValue("Endpoint index out of range");
118 return endpoints[epi].link;
121 bool Block::reserve(const Train *t)
127 cout<<"Block "<<this<<" reserved for train "<<train->get_name()<<'\n';
129 cout<<"Block "<<this<<" freed\n";
130 trfc_mgr.signal_block_reserved.emit(*this, train);
137 void Block::print_debug()
140 if((*tracks.begin())->get_sensor_id())
141 cout<<", sensor="<<(*tracks.begin())->get_sensor_id();
143 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
145 cout<<" Endpoint, link=";
150 cout<<", routes="<<i->routes<<'\n';
154 void Block::find_routes(Track &track, unsigned track_ep, unsigned route, set<Track *> &visited)
156 visited.insert(&track);
158 const vector<Marklin::Endpoint> &eps=track.get_type().get_endpoints();
159 for(unsigned i=0; i<eps.size(); ++i)
161 if(i==track_ep) continue;
162 Track *link=track.get_link(i);
164 if(visited.count(link)) continue;
165 if(!(eps[i].routes&eps[track_ep].routes)) continue;
167 if(tracks.count(link))
168 find_routes(*link, link->get_endpoint_by_link(track), route, visited);
171 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
172 if(j->track==&track && j->track_ep==i)
178 unsigned Block::next_id=1;
181 Block::Endpoint::Endpoint(Track *t, unsigned e):
188 } // namespace Marklin