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 Block::Block(TrafficManager &tm, Track &start):
23 sensor_id(start.get_sensor_id()),
24 turnout_id(start.get_turnout_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()==turnout_id)
43 queue.push_back(links[i]);
44 tracks.insert(links[i]);
47 endpoints.push_back(Endpoint(track, i));
52 id = 0x1000|sensor_id;
54 id = 0x2000|turnout_id;
56 for(unsigned i=0; i<endpoints.size(); ++i)
59 endpoints[i].paths |= path;
61 find_paths(*endpoints[i].track, endpoints[i].track_ep, path, visited);
65 int Block::get_endpoint_by_link(const Block &other) const
67 for(unsigned i=0; i<endpoints.size(); ++i)
68 if(endpoints[i].link==&other)
74 unsigned Block::traverse(unsigned epi, float *len) const
76 if(epi>=endpoints.size())
77 throw InvalidParameterValue("Endpoint index out of range");
79 const Endpoint &ep = endpoints[epi];
80 Track *track = ep.track;
81 unsigned track_ep = ep.track_ep;
88 unsigned cur_path = 0;
89 unsigned tid = track->get_turnout_id();
92 Turnout &turnout = trfc_mgr.get_control().get_turnout(tid);
93 cur_path = turnout.get_path();
97 *len += track->get_type().get_path_length(cur_path);
99 unsigned other_ep = track->traverse(track_ep, cur_path);
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))
106 throw LogicError("Block traversal strayed out of the block");
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)
127 if(!sensor_id && !turnout_id && endpoints.size()==2)
129 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
130 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
137 Block *Block::get_link(unsigned epi) const
139 if(epi>=endpoints.size())
140 throw InvalidParameterValue("Endpoint index out of range");
141 return endpoints[epi].link;
144 bool Block::reserve(const Train *t)
149 trfc_mgr.signal_block_reserved.emit(*this, train);
156 void Block::find_paths(Track &track, unsigned track_ep, unsigned path, set<Track *> &visited)
158 visited.insert(&track);
160 const vector<Marklin::Endpoint> &eps = track.get_type().get_endpoints();
161 for(unsigned i=0; i<eps.size(); ++i)
163 if(i==track_ep) continue;
164 Track *link = track.get_link(i);
166 if(visited.count(link)) continue;
167 if(!(eps[i].paths&eps[track_ep].paths)) continue;
169 if(tracks.count(link))
170 find_paths(*link, link->get_endpoint_by_link(track), path, visited);
173 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
174 if(j->track==&track && j->track_ep==i)
181 Block::Endpoint::Endpoint(Track *t, unsigned e):
188 } // namespace Marklin