3 This file is part of R²C²
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
12 #include "trackiter.h"
13 #include "tracktype.h"
20 Block::Block(Layout &l, Track &start):
23 sensor_id(start.get_sensor_id()),
24 turnout_id(start.get_turnout_id()),
27 tracks.insert(&start);
28 start.set_block(this);
31 queue.push_back(&start);
35 Track *track = queue.front();
36 queue.erase(queue.begin());
38 const vector<Track *> &links = track->get_links();
39 for(unsigned i=0; i<links.size(); ++i)
40 if(links[i] && !tracks.count(links[i]))
42 if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
44 queue.push_back(links[i]);
45 tracks.insert(links[i]);
46 links[i]->set_block(this);
49 endpoints.push_back(Endpoint(track, i));
55 for(unsigned i=0; i<endpoints.size(); ++i)
58 endpoints[i].paths |= path;
59 find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path);
62 layout.add_block(*this);
67 set<Track *> trks = tracks;
69 for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
72 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
73 if(Block *blk = i->link)
76 blk->break_link(*this);
79 layout.remove_block(*this);
82 bool Block::has_track(Track &t) const
84 return tracks.count(&t);
87 const Block::Endpoint &Block::get_endpoint(unsigned i) const
89 if(i>=endpoints.size())
90 throw InvalidParameterValue("Endpoint index out of range");
95 int Block::get_endpoint_by_link(Block &other) const
97 for(unsigned i=0; i<endpoints.size(); ++i)
98 if(endpoints[i].link==&other)
104 float Block::get_path_length(unsigned entry, const Route *route) const
106 if(entry>=endpoints.size())
107 throw InvalidParameterValue("Endpoint index out of range");
109 TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep);
112 while(t_iter && has_track(*t_iter))
114 unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
115 result += t_iter->get_type().get_path_length(path);
117 t_iter = t_iter.next(path);
123 void Block::check_link(Block &other)
125 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
130 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
131 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
137 other.determine_id();
142 void Block::break_link(Block &other)
144 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
148 other.break_link(*this);
153 Block *Block::get_link(unsigned epi) const
155 if(epi>=endpoints.size())
156 throw InvalidParameterValue("Endpoint index out of range");
157 return endpoints[epi].link;
160 bool Block::reserve(Train *t)
165 layout.signal_block_reserved.emit(*this, train);
172 void Block::find_paths(TrackIter track, unsigned path)
174 unsigned mask = track.endpoint().paths;
175 for(unsigned i=0; mask>>i; ++i)
178 TrackIter next = track.next(i);
181 else if(has_track(*next))
182 find_paths(track.next(i), path);
186 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
187 if(j->track==next.track() && j->track_ep==next.entry())
193 void Block::determine_id()
196 id = 0x1000|sensor_id;
198 id = 0x2000|turnout_id;
199 else if(endpoints.size()==2)
201 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
202 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
207 else if(endpoints.size()==1)
209 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
215 Block::Endpoint::Endpoint(Track *t, unsigned e):