]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/block.cpp
Pass sensor events through blocks
[r2c2.git] / source / libr2c2 / block.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <algorithm>
9 #include <msp/time/units.h>
10 #include "block.h"
11 #include "driver.h"
12 #include "layout.h"
13 #include "route.h"
14 #include "trackiter.h"
15 #include "tracktype.h"
16
17 using namespace std;
18 using namespace Msp;
19
20 namespace R2C2 {
21
22 Block::Block(Layout &l, Track &start):
23         layout(l),
24         id(0),
25         sensor_id(start.get_sensor_id()),
26         turnout_id(start.get_turnout_id()),
27         state(INACTIVE),
28         train(0)
29 {
30         tracks.insert(&start);
31         start.set_block(this);
32
33         list<Track *> queue;
34         queue.push_back(&start);
35
36         while(!queue.empty())
37         {
38                 Track *track = queue.front();
39                 queue.erase(queue.begin());
40
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]))
44                         {
45                                 if(links[i]->get_sensor_id()==sensor_id && links[i]->get_turnout_id()==turnout_id)
46                                 {
47                                         queue.push_back(links[i]);
48                                         tracks.insert(links[i]);
49                                         links[i]->set_block(this);
50                                 }
51                                 else
52                                         endpoints.push_back(Endpoint(track, i));
53                         }
54         }
55
56         determine_id();
57
58         for(unsigned i=0; i<endpoints.size(); ++i)
59         {
60                 unsigned path = 1<<i;
61                 endpoints[i].paths |= path;
62                 find_paths(TrackIter(endpoints[i].track, endpoints[i].track_ep), path);
63         }
64
65         if(sensor_id && layout.has_driver())
66                 layout.get_driver().signal_sensor.connect(sigc::mem_fun(this, &Block::sensor_event));
67
68         layout.add_block(*this);
69 }
70
71 Block::~Block()
72 {
73         set<Track *> trks = tracks;
74         tracks.clear();
75         for(set<Track *>::iterator i=trks.begin(); i!=trks.end(); ++i)
76                 (*i)->set_block(0);
77
78         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
79                 if(Block *blk = i->link)
80                 {
81                         i->link = 0;
82                         blk->break_link(*this);
83                 }
84
85         layout.remove_block(*this);
86 }
87
88 bool Block::has_track(Track &t) const
89 {
90         return tracks.count(&t);
91 }
92
93 const Block::Endpoint &Block::get_endpoint(unsigned i) const
94 {
95         if(i>=endpoints.size())
96                 throw InvalidParameterValue("Endpoint index out of range");
97
98         return endpoints[i];
99 }
100
101 int Block::get_endpoint_by_link(Block &other) const
102 {
103         for(unsigned i=0; i<endpoints.size(); ++i)
104                 if(endpoints[i].link==&other)
105                         return i;
106
107         return -1;
108 }
109
110 float Block::get_path_length(unsigned entry, const Route *route) const
111 {
112         if(entry>=endpoints.size())
113                 throw InvalidParameterValue("Endpoint index out of range");
114
115         TrackIter t_iter(endpoints[entry].track, endpoints[entry].track_ep);
116
117         float result = 0;
118         while(t_iter && has_track(*t_iter))
119         {
120                 unsigned path = (route ? route->get_path(*t_iter) : t_iter->get_active_path());
121                 result += t_iter->get_type().get_path_length(path);
122
123                 t_iter = t_iter.next(path);
124         }
125
126         return result;
127 }
128
129 void Block::check_link(Block &other)
130 {
131         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
132         {
133                 if(i->link)
134                         continue;
135
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)
138                         {
139                                 i->link = &other;
140                                 j->link = this;
141
142                                 determine_id();
143                                 other.determine_id();
144                         }
145         }
146 }
147
148 void Block::break_link(Block &other)
149 {
150         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
151                 if(i->link==&other)
152                 {
153                         i->link = 0;
154                         other.break_link(*this);
155                         determine_id();
156                 }
157 }
158
159 Block *Block::get_link(unsigned epi) const
160 {
161         if(epi>=endpoints.size())
162                 throw InvalidParameterValue("Endpoint index out of range");
163         return endpoints[epi].link;
164 }
165
166 bool Block::reserve(Train *t)
167 {
168         if(!t || !train)
169         {
170                 train = t;
171                 signal_reserved.emit(train);
172                 return true;
173         }
174         else
175                 return false;
176 }
177
178 void Block::tick(const Time::TimeDelta &dt)
179 {
180         if(state_confirm_timeout)
181         {
182                 state_confirm_timeout -= dt;
183                 if(state_confirm_timeout<=Time::zero)
184                 {
185                         if(state==MAYBE_INACTIVE)
186                                 state = INACTIVE;
187                         else if(state==MAYBE_ACTIVE)
188                                 state = ACTIVE;
189                         state_confirm_timeout = Time::zero;
190                         signal_state_changed.emit(state);
191                 }
192         }
193 }
194
195 void Block::find_paths(TrackIter track, unsigned path)
196 {
197         unsigned mask = track.endpoint().paths;
198         for(unsigned i=0; mask>>i; ++i)
199                 if(mask&(1<<i))
200                 {
201                         TrackIter next = track.next(i);
202                         if(!next)
203                                 continue;
204                         else if(has_track(*next))
205                                 find_paths(track.next(i), path);
206                         else
207                         {
208                                 next = next.flip();
209                                 for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
210                                         if(j->track==next.track() && j->track_ep==next.entry())
211                                                 j->paths |= path;
212                         }
213                 }
214 }
215
216 void Block::determine_id()
217 {
218         if(sensor_id)
219                 id = 0x1000|sensor_id;
220         else if(turnout_id)
221                 id = 0x2000|turnout_id;
222         else if(endpoints.size()==2)
223         {
224                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
225                 unsigned id2 = endpoints[1].link ? endpoints[1].link->get_id() : 1;
226                 if(id2<id1)
227                         swap(id1, id2);
228                 id = (id1<<16)|id2;
229         }
230         else if(endpoints.size()==1)
231         {
232                 unsigned id1 = endpoints[0].link ? endpoints[0].link->get_id() : 1;
233                 id = 0x10000 | id1;
234         }
235 }
236
237 void Block::sensor_event(unsigned addr, bool s)
238 {
239         if(addr==sensor_id)
240         {
241                 if(s && state<MAYBE_ACTIVE)
242                 {
243                         state = MAYBE_ACTIVE;
244                         state_confirm_timeout = 300*Time::msec;
245                         signal_state_changed.emit(state);
246                 }
247                 else if(!s && state>MAYBE_INACTIVE)
248                 {
249                         state = MAYBE_INACTIVE;
250                         state_confirm_timeout = 700*Time::msec;
251                         signal_state_changed.emit(state);
252                 }
253         }
254 }
255
256
257 Block::Endpoint::Endpoint(Track *t, unsigned e):
258         track(t),
259         track_ep(e),
260         link(0),
261         paths(0)
262 { }
263
264 } // namespace R2C2