3 This file is part of R²C²
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <msp/time/units.h>
14 #include "trackiter.h"
15 #include "tracktype.h"
22 Block::Block(Layout &l, Track &start):
25 sensor_id(start.get_sensor_id()),
26 turnout_id(start.get_turnout_id()),
30 tracks.insert(&start);
31 start.set_block(this);
34 queue.push_back(&start);
38 Track *track = queue.front();
39 queue.erase(queue.begin());
41 const vector<Track *> &links = track->get_links();
42 for(unsigned i=0; i<links.size(); ++i)
43 if(links[i] && !tracks.count(links[i]))
45 if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
47 queue.push_back(links[i]);
48 tracks.insert(links[i]);
49 links[i]->set_block(this);
52 endpoints.push_back(Endpoint(track, i));
58 for(unsigned i=0; i<endpoints.size(); ++i)
61 endpoints[i].paths |= path;
62 find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path);
65 if(sensor_id && layout.has_driver())
66 layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Block::sensor_event));
68 layout.add_block(*this);
73 set<Track *> trks = tracks;
75 for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
78 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
79 if(Block *blk = i->link)
82 blk->break_link(*this);
85 layout.remove_block(*this);
88 bool Block::has_track(Track &t) const
90 return tracks.count(&t);
93 const Block::Endpoint &Block::get_endpoint(unsigned i) const
95 if(i>=endpoints.size())
96 throw InvalidParameterValue("Endpoint index out of range");
101 int Block::get_endpoint_by_link(Block &other) const
103 for(unsigned i=0; i<endpoints.size(); ++i)
104 if(endpoints[i].link==&other)
110 float Block::get_path_length(unsigned entry, const Route *route) const
112 if(entry>=endpoints.size())
113 throw InvalidParameterValue("Endpoint index out of range");
115 TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep);
118 while(t_iter && has_track(*t_iter))
120 unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
121 result += t_iter->get_type().get_path_length(path);
123 t_iter = t_iter.next(path);
129 void Block::check_link(Block &other)
131 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
136 for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
137 if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
143 other.determine_id();
148 void Block::break_link(Block &other)
150 for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
154 other.break_link(*this);
159 Block *Block::get_link(unsigned epi) const
161 if(epi>=endpoints.size())
162 throw InvalidParameterValue("Endpoint index out of range");
163 return endpoints[epi].link;
166 bool Block::reserve(Train *t)
171 signal_reserved.emit(train);
178 void Block::tick(const Time::TimeDelta &dt)
180 if(state_confirm_timeout)
182 state_confirm_timeout -= dt;
183 if(state_confirm_timeout<=Time::zero)
185 if(state==MAYBE_INACTIVE)
187 else if(state==MAYBE_ACTIVE)
189 state_confirm_timeout = Time::zero;
190 signal_state_changed.emit(state);
195 void Block::find_paths(TrackIter track, unsigned path)
197 unsigned mask = track.endpoint().paths;
198 for(unsigned i=0; mask>>i; ++i)
201 TrackIter next = track.next(i);
204 else if(has_track(*next))
205 find_paths(track.next(i), path);
209 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
210 if(j->track==next.track() && j->track_ep==next.entry())
216 void Block::determine_id()
219 id = 0x1000|sensor_id;
221 id = 0x2000|turnout_id;
222 else if(endpoints.size()==2)
224 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
225 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
230 else if(endpoints.size()==1)
232 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
237 void Block::sensor_event(unsigned addr, bool s)
241 if(s && state<MAYBE_ACTIVE)
243 state = MAYBE_ACTIVE;
244 state_confirm_timeout = 300*Time::msec;
245 signal_state_changed.emit(state);
247 else if(!s && state>MAYBE_INACTIVE)
249 state = MAYBE_INACTIVE;
250 state_confirm_timeout = 700*Time::msec;
251 signal_state_changed.emit(state);
257 Block::Endpoint::Endpoint(Track *t, unsigned e):