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)
56 endpoints[i].paths |= path;
58 find_paths(*endpoints[i].track, endpoints[i].track_ep, path, 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 unsigned 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_path = 0;
86 unsigned tid = track->get_turnout_id();
89 Turnout &turnout = trfc_mgr.get_control().get_turnout(tid);
90 cur_path = turnout.get_path();
94 *len += track->get_type().get_path_length(cur_path);
96 unsigned other_ep = track->traverse(track_ep, cur_path);
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))
103 throw LogicError("Block traversal strayed out of the block");
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<<", paths="<<i->paths<<'\n';
161 void Block::find_paths(Track &track, unsigned track_ep, unsigned path, 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].paths&eps[track_ep].paths)) continue;
174 if(tracks.count(link))
175 find_paths(*link, link->get_endpoint_by_link(track), path, visited);
178 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
179 if(j->track==&track && j->track_ep==i)
186 Block::Endpoint::Endpoint(Track *t, unsigned e):
193 } // namespace Marklin