2 #include <msp/time/units.h>
15 Block::Block(Layout &l, Track &start):
18 sensor_id(start.get_sensor_id()),
19 turnout_id(start.get_turnout_id()),
23 tracks.insert(&start);
24 start.set_block(this);
27 queue.push_back(&start);
31 Track *track = queue.front();
32 queue.erase(queue.begin());
34 const vector<Track *> &links = track->get_links();
35 for(unsigned i=0; i<links.size(); ++i)
36 if(links[i] && !tracks.count(links[i]))
38 if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
40 queue.push_back(links[i]);
41 tracks.insert(links[i]);
42 links[i]->set_block(this);
45 endpoints.push_back(Endpoint(track, i));
51 for(unsigned i=0; i<endpoints.size(); ++i)
54 endpoints[i].paths |= path;
55 find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path);
58 if(sensor_id && layout.has_driver())
59 layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Block::sensor_event));
61 layout.add_block(*this);
66 set<Track *> trks = tracks;
68 for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
71 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
72 if(Block *blk = i->link)
75 blk->break_link(*this);
78 layout.remove_block(*this);
81 bool Block::has_track(Track &t) const
83 return tracks.count(&t);
86 const Block::Endpoint &Block::get_endpoint(unsigned i) const
88 if(i>=endpoints.size())
89 throw out_of_range("Block::get_endpoint");
94 int Block::get_endpoint_by_link(Block &other) const
96 for(unsigned i=0; i<endpoints.size(); ++i)
97 if(endpoints[i].link==&other)
103 float Block::get_path_length(unsigned entry, const Route *route) const
105 if(entry>=endpoints.size())
106 throw out_of_range("Block::get_path_length");
108 TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep);
111 while(t_iter && has_track(*t_iter))
113 unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
114 result += t_iter->get_type().get_path_length(path);
116 t_iter = t_iter.next(path);
122 void Block::check_link(Block &other)
124 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
129 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
130 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
136 other.determine_id();
141 void Block::break_link(Block &other)
143 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
147 other.break_link(*this);
152 Block *Block::get_link(unsigned epi) const
154 if(epi>=endpoints.size())
155 throw out_of_range("Block::get_link");
156 return endpoints[epi].link;
159 bool Block::reserve(Train *t)
164 signal_reserved.emit(train);
171 void Block::tick(const Time::TimeDelta &dt)
173 if(state_confirm_timeout)
175 state_confirm_timeout -= dt;
176 if(state_confirm_timeout<=Time::zero)
178 if(state==MAYBE_INACTIVE)
180 else if(state==MAYBE_ACTIVE)
182 state_confirm_timeout = Time::zero;
183 signal_state_changed.emit(state);
188 void Block::find_paths(TrackIter track, unsigned path)
190 unsigned mask = track.endpoint().paths;
191 for(unsigned i=0; mask>>i; ++i)
194 TrackIter next = track.next(i);
197 else if(has_track(*next))
198 find_paths(track.next(i), path);
202 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
203 if(j->track==next.track() && j->track_ep==next.entry())
209 void Block::determine_id()
212 id = 0x1000|sensor_id;
214 id = 0x2000|turnout_id;
215 else if(endpoints.size()==2)
217 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
218 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
223 else if(endpoints.size()==1)
225 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
230 void Block::sensor_event(unsigned addr, bool s)
234 if(s && state<MAYBE_ACTIVE)
236 state = MAYBE_ACTIVE;
237 state_confirm_timeout = 300*Time::msec;
238 signal_state_changed.emit(state);
240 else if(!s && state>MAYBE_INACTIVE)
242 state = MAYBE_INACTIVE;
243 state_confirm_timeout = 700*Time::msec;
244 signal_state_changed.emit(state);
250 Block::Endpoint::Endpoint(Track *t, unsigned e):